Hex Artifact Content
Not logged in

Artifact 78b516735165072e986247f4667a84a5870fd889:

File src/sqlite3.c part of check-in [c287665ba8] - Update the SQLite amalgamation file to version 3.5.0. by drh on 2007-09-14 14:59:38. Also file src/sqlite3.c part of check-in [f76192b245] - Pulled the latest CLI, website, and sqlite changes into the importer branch. by aku on 2007-09-17 01:00:32.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 20 42  ersion 3.5.0.  B
00b0: 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c 20  y combining all 
00c0: 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 43  the individual C
00d0: 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74 6f   code files into
00e0: 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c 65   this .** single
00f0: 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68 65   large file, the
0100: 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61 6e   entire code can
0110: 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73 20   be compiled as 
0120: 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69 6f  a one translatio
0130: 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69 73  n.** unit.  This
0140: 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f 6d   allows many com
0150: 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70 74  pilers to do opt
0160: 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74 20  imizations that 
0170: 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20  would not be.** 
0180: 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65 20  possible if the 
0190: 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70 69  files were compi
01a0: 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 20  led separately. 
01b0: 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70   Performance imp
01c0: 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20  rovements.** of 
01d0: 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65 20  5% are more are 
01e0: 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77 68  commonly seen wh
01f0: 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  en SQLite is com
0200: 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67 6c  piled as a singl
0210: 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e  e.** translation
0220: 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   unit..**.** Thi
0230: 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79 6f  s file is all yo
0240: 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69 6c  u need to compil
0250: 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75 73  e SQLite.  To us
0260: 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68 65  e SQLite in othe
0270: 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20 79  r.** programs, y
0280: 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69 6c  ou need this fil
0290: 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69 74  e and the "sqlit
02a0: 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69 6c  e3.h" header fil
02b0: 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a 2a  e that defines.*
02c0: 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69 6e  * the programmin
02d0: 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  g interface to t
02e0: 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
02f0: 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20 6e  y.  (If you do n
0300: 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65 20  ot have .** the 
0310: 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61 64  "sqlite3.h" head
0320: 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64 2c  er file at hand,
0330: 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20 61   you will find a
0340: 20 63 6f 70 79 20 69 6e 20 74 68 65 20 66 69 72   copy in the fir
0350: 73 74 0a 2a 2a 20 33 35 33 39 20 6c 69 6e 65 73  st.** 3539 lines
0360: 20 70 61 73 74 20 74 68 69 73 20 68 65 61 64 65   past this heade
0370: 72 20 63 6f 6d 6d 65 6e 74 2e 29 20 20 41 64 64  r comment.)  Add
0380: 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20 66 69 6c  itional code fil
0390: 65 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6e 65 65  es may be.** nee
03a0: 64 65 64 20 69 66 20 79 6f 75 20 77 61 6e 74 20  ded if you want 
03b0: 61 20 77 72 61 70 70 65 72 20 74 6f 20 69 6e 74  a wrapper to int
03c0: 65 72 66 61 63 65 20 53 51 4c 69 74 65 20 77 69  erface SQLite wi
03d0: 74 68 20 79 6f 75 72 20 63 68 6f 69 63 65 20 6f  th your choice o
03e0: 66 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 69 6e 67  f.** programming
03f0: 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 20   language.  The 
0400: 63 6f 64 65 20 66 6f 72 20 74 68 65 20 22 73 71  code for the "sq
0410: 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e 64 2d 6c  lite3" command-l
0420: 69 6e 65 20 73 68 65 6c 6c 0a 2a 2a 20 69 73 20  ine shell.** is 
0430: 61 6c 73 6f 20 69 6e 20 61 20 73 65 70 61 72 61  also in a separa
0440: 74 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 66  te file.  This f
0450: 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c  ile contains onl
0460: 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 63  y code for the c
0470: 6f 72 65 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69  ore.** SQLite li
0480: 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  brary..**.** Thi
0490: 73 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77  s amalgamation w
04a0: 61 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e 20  as generated on 
04b0: 32 30 30 37 2d 30 39 2d 31 34 20 31 34 3a 35 38  2007-09-14 14:58
04c0: 3a 31 30 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66  :10 UTC..*/.#def
04d0: 69 6e 65 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47  ine SQLITE_AMALG
04e0: 41 4d 41 54 49 4f 4e 20 31 0a 23 69 66 6e 64 65  AMATION 1.#ifnde
04f0: 66 20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f SQLITE_PRIVATE
0500: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
0510: 5f 50 52 49 56 41 54 45 20 73 74 61 74 69 63 0a  _PRIVATE static.
0520: 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
0530: 51 4c 49 54 45 5f 41 50 49 0a 23 20 64 65 66 69  QLITE_API.# defi
0540: 6e 65 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 65  ne SQLITE_API.#e
0550: 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif./**********
0560: 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
0570: 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a  sqlite3.h ******
0580: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0590: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05a0: 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
05b0: 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
05c0: 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
05d0: 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
05e0: 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
05f0: 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
0600: 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
0610: 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
0620: 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
0630: 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
0640: 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
0650: 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
0660: 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
0670: 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
0680: 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
0690: 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
06a0: 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
06b0: 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
06c0: 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
06d0: 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
06e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
06f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0700: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
0720: 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
0730: 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  defines the inte
0740: 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 53  rface that the S
0750: 51 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a  QLite library.**
0760: 20 70 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69   presents to cli
0770: 65 6e 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49  ent programs.  I
0780: 66 20 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20  f a C-function, 
0790: 73 74 72 75 63 74 75 72 65 2c 20 64 61 74 61 74  structure, datat
07a0: 79 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74  ype,.** or const
07b0: 61 6e 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64  ant definition d
07c0: 6f 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69  oes not appear i
07d0: 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65  n this file, the
07e0: 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61  n it is.** not a
07f0: 20 70 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f   published API o
0800: 66 20 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62  f SQLite, is sub
0810: 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77  ject to change w
0820: 69 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65  ithout.** notice
0830: 2c 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74  , and should not
0840: 20 62 65 20 72 65 66 65 72 65 6e 63 65 64 20 62   be referenced b
0850: 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20  y programs that 
0860: 75 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a  use SQLite..**.*
0870: 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65  * Some of the de
0880: 66 69 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61  finitions that a
0890: 72 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  re in this file 
08a0: 61 72 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a  are marked as.**
08b0: 20 22 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e   "experimental".
08c0: 20 20 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69    Experimental i
08d0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f  nterfaces are no
08e0: 72 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65  rmally new.** fe
08f0: 61 74 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20  atures recently 
0900: 61 64 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e  added to SQLite.
0910: 20 20 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69    We do not anti
0920: 63 69 70 61 74 65 20 63 68 61 6e 67 65 73 20 0a  cipate changes .
0930: 2a 2a 20 74 6f 20 65 78 70 65 72 69 6d 65 6e 74  ** to experiment
0940: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 62 75  al interfaces bu
0950: 74 20 72 65 73 65 72 76 65 20 74 6f 20 6d 61 6b  t reserve to mak
0960: 65 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20  e minor changes 
0970: 69 66 0a 2a 2a 20 65 78 70 65 72 69 65 6e 63 65  if.** experience
0980: 20 66 72 6f 6d 20 75 73 65 20 22 69 6e 20 74 68   from use "in th
0990: 65 20 77 69 6c 64 22 20 73 75 67 67 65 73 74 20  e wild" suggest 
09a0: 73 75 63 68 20 63 68 61 6e 67 65 73 20 61 72 65  such changes are
09b0: 20 70 72 75 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   prudent..**.** 
09c0: 54 68 65 20 6f 66 66 69 63 69 61 6c 20 43 2d 6c  The official C-l
09d0: 61 6e 67 75 61 67 65 20 41 50 49 20 64 6f 63 75  anguage API docu
09e0: 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 53 51  mentation for SQ
09f0: 4c 69 74 65 20 69 73 20 64 65 72 69 76 65 64 0a  Lite is derived.
0a00: 2a 2a 20 66 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73  ** from comments
0a10: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 20   in this file.  
0a20: 54 68 69 73 20 66 69 6c 65 20 69 73 20 74 68 65  This file is the
0a30: 20 61 75 74 68 6f 72 69 74 61 74 69 76 65 20 73   authoritative s
0a40: 6f 75 72 63 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20  ource.** on how 
0a50: 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
0a60: 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f  s are suppose to
0a70: 20 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20   operate..**.** 
0a80: 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 69 73  The name of this
0a90: 20 66 69 6c 65 20 75 6e 64 65 72 20 63 6f 6e 66   file under conf
0aa0: 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e 61 67 65  iguration manage
0ab0: 6d 65 6e 74 20 69 73 20 22 73 71 6c 69 74 65 2e  ment is "sqlite.
0ac0: 68 2e 69 6e 22 2e 0a 2a 2a 20 54 68 65 20 6d 61  h.in"..** The ma
0ad0: 6b 65 66 69 6c 65 20 6d 61 6b 65 73 20 73 6f 6d  kefile makes som
0ae0: 65 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20  e minor changes 
0af0: 74 6f 20 74 68 69 73 20 66 69 6c 65 20 28 73 75  to this file (su
0b00: 63 68 20 61 73 20 69 6e 73 65 72 74 69 6e 67 0a  ch as inserting.
0b10: 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e  ** the version n
0b20: 75 6d 62 65 72 29 20 61 6e 64 20 63 68 61 6e 67  umber) and chang
0b30: 65 73 20 69 74 73 20 6e 61 6d 65 20 74 6f 20 22  es its name to "
0b40: 73 71 6c 69 74 65 33 2e 68 22 20 61 73 0a 2a 2a  sqlite3.h" as.**
0b50: 20 70 61 72 74 20 6f 66 20 74 68 65 20 62 75 69   part of the bui
0b60: 6c 64 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a  ld process..**.*
0b70: 2a 20 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69  * @(#) $Id: sqli
0b80: 74 65 2e 68 2e 69 6e 2c 76 20 31 2e 32 35 39 20  te.h.in,v 1.259 
0b90: 32 30 30 37 2f 30 39 2f 30 34 20 32 32 3a 33 31  2007/09/04 22:31
0ba0: 3a 33 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :37 drh Exp $.*/
0bb0: 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  .#ifndef _SQLITE
0bc0: 33 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51  3_H_.#define _SQ
0bd0: 4c 49 54 45 33 5f 48 5f 0a 23 69 6e 63 6c 75 64  LITE3_H_.#includ
0be0: 65 20 3c 73 74 64 61 72 67 2e 68 3e 20 20 20 20  e <stdarg.h>    
0bf0: 20 2f 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 74   /* Needed for t
0c00: 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  he definition of
0c10: 20 76 61 5f 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a   va_list */../*.
0c20: 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20  ** Make sure we 
0c30: 63 61 6e 20 63 61 6c 6c 20 74 68 69 73 20 73 74  can call this st
0c40: 75 66 66 20 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f  uff from C++..*/
0c50: 0a 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43  .#if 0.extern "C
0c60: 22 20 7b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  " {.#endif.../*.
0c70: 2a 2a 20 41 64 64 20 74 68 65 20 61 62 69 6c 69  ** Add the abili
0c80: 74 79 20 74 6f 20 6f 76 65 72 72 69 64 65 20 27  ty to override '
0c90: 65 78 74 65 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64  extern'.*/.#ifnd
0ca0: 65 66 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e  ef SQLITE_EXTERN
0cb0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
0cc0: 5f 45 58 54 45 52 4e 20 65 78 74 65 72 6e 0a 23  _EXTERN extern.#
0cd0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  endif../*.** Mak
0ce0: 65 20 73 75 72 65 20 74 68 65 73 65 20 73 79 6d  e sure these sym
0cf0: 62 6f 6c 73 20 77 68 65 72 65 20 6e 6f 74 20 64  bols where not d
0d00: 65 66 69 6e 65 64 20 62 79 20 73 6f 6d 65 20 70  efined by some p
0d10: 72 65 76 69 6f 75 73 20 68 65 61 64 65 72 0a 2a  revious header.*
0d20: 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65  * file..*/.#ifde
0d30: 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  f SQLITE_VERSION
0d40: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
0d50: 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66 0a 23  VERSION.#endif.#
0d60: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52  ifdef SQLITE_VER
0d70: 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20 75 6e  SION_NUMBER.# un
0d80: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49  def SQLITE_VERSI
0d90: 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64 69 66  ON_NUMBER.#endif
0da0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
0db0: 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 4c  : Compile-Time L
0dc0: 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e  ibrary Version N
0dd0: 75 6d 62 65 72 73 0a 2a 2a 0a 2a 2a 20 54 68 65  umbers.**.** The
0de0: 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
0df0: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69  SQLite library i
0e00: 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  s contained in t
0e10: 68 65 20 73 71 6c 69 74 65 33 2e 68 0a 2a 2a 20  he sqlite3.h.** 
0e20: 68 65 61 64 65 72 20 66 69 6c 65 20 69 6e 20 61  header file in a
0e30: 20 23 64 65 66 69 6e 65 20 6e 61 6d 65 64 20 53   #define named S
0e40: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 2e 20 20  QLITE_VERSION.  
0e50: 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49  The SQLITE_VERSI
0e60: 4f 4e 0a 2a 2a 20 6d 61 63 72 6f 20 72 65 73 6f  ON.** macro reso
0e70: 6c 76 65 73 20 74 6f 20 61 20 73 74 72 69 6e 67  lves to a string
0e80: 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a   constant..**.**
0e90: 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74   The format of t
0ea0: 68 65 20 76 65 72 73 69 6f 6e 20 73 74 72 69 6e  he version strin
0eb0: 67 20 69 73 20 22 58 2e 59 2e 5a 22 2c 20 77 68  g is "X.Y.Z", wh
0ec0: 65 72 65 0a 2a 2a 20 58 20 69 73 20 74 68 65 20  ere.** X is the 
0ed0: 6d 61 6a 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75  major version nu
0ee0: 6d 62 65 72 2c 20 59 20 69 73 20 74 68 65 20 6d  mber, Y is the m
0ef0: 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d  inor version num
0f00: 62 65 72 20 61 6e 64 20 5a 0a 2a 2a 20 69 73 20  ber and Z.** is 
0f10: 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d 62  the release numb
0f20: 65 72 2e 20 20 54 68 65 20 58 2e 59 2e 5a 20 6d  er.  The X.Y.Z m
0f30: 69 67 68 74 20 62 65 20 66 6f 6c 6c 6f 77 65 64  ight be followed
0f40: 20 62 79 20 22 61 6c 70 68 61 22 20 6f 72 20 22   by "alpha" or "
0f50: 62 65 74 61 22 2e 0a 2a 2a 20 46 6f 72 20 65 78  beta"..** For ex
0f60: 61 6d 70 6c 65 20 22 33 2e 31 2e 31 62 65 74 61  ample "3.1.1beta
0f70: 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 58 20 76  "..**.** The X v
0f80: 61 6c 75 65 20 69 73 20 61 6c 77 61 79 73 20 33  alue is always 3
0f90: 20 69 6e 20 53 51 4c 69 74 65 2e 20 20 54 68 65   in SQLite.  The
0fa0: 20 58 20 76 61 6c 75 65 20 6f 6e 6c 79 20 63 68   X value only ch
0fb0: 61 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20 62 61  anges when.** ba
0fc0: 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
0fd0: 69 6c 69 74 79 20 69 73 20 62 72 6f 6b 65 6e 20  ility is broken 
0fe0: 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 74 6f  and we intend to
0ff0: 20 6e 65 76 65 72 20 62 72 65 61 6b 0a 2a 2a 20   never break.** 
1000: 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
1010: 69 62 69 6c 69 74 79 2e 20 20 54 68 65 20 59 20  ibility.  The Y 
1020: 76 61 6c 75 65 20 6f 6e 6c 79 20 63 68 61 6e 67  value only chang
1030: 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 65  es when.** there
1040: 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 75   are major featu
1050: 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20  re enhancements 
1060: 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72 64  that are forward
1070: 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20  s compatible.** 
1080: 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 64  but not backward
1090: 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 20 20 54  s compatible.  T
10a0: 68 65 20 5a 20 76 61 6c 75 65 20 69 73 20 69 6e  he Z value is in
10b0: 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a  cremented with.*
10c0: 2a 20 65 61 63 68 20 72 65 6c 65 61 73 65 20 62  * each release b
10d0: 75 74 20 72 65 73 65 74 73 20 62 61 63 6b 20 74  ut resets back t
10e0: 6f 20 30 20 77 68 65 6e 20 59 20 69 73 20 69 6e  o 0 when Y is in
10f0: 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a  cremented..**.**
1100: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53   The SQLITE_VERS
1110: 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61 6e  ION_NUMBER is an
1120: 20 69 6e 74 65 67 65 72 20 77 69 74 68 20 74 68   integer with th
1130: 65 20 76 61 6c 75 65 20 0a 2a 2a 20 28 58 2a 31  e value .** (X*1
1140: 30 30 30 30 30 30 20 2b 20 59 2a 31 30 30 30 20  000000 + Y*1000 
1150: 2b 20 5a 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c  + Z). For exampl
1160: 65 2c 20 66 6f 72 20 76 65 72 73 69 6f 6e 20 22  e, for version "
1170: 33 2e 31 2e 31 62 65 74 61 22 2c 20 0a 2a 2a 20  3.1.1beta", .** 
1180: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e  SQLITE_VERSION_N
1190: 55 4d 42 45 52 20 69 73 20 73 65 74 20 74 6f 20  UMBER is set to 
11a0: 33 30 30 31 30 30 31 2e 20 54 6f 20 64 65 74 65  3001001. To dete
11b0: 63 74 20 69 66 20 74 68 65 79 20 61 72 65 20 75  ct if they are u
11c0: 73 69 6e 67 20 0a 2a 2a 20 76 65 72 73 69 6f 6e  sing .** version
11d0: 20 33 2e 31 2e 31 20 6f 72 20 67 72 65 61 74 65   3.1.1 or greate
11e0: 72 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d  r at compile tim
11f0: 65 2c 20 70 72 6f 67 72 61 6d 73 20 6d 61 79 20  e, programs may 
1200: 75 73 65 20 74 68 65 20 74 65 73 74 20 0a 2a 2a  use the test .**
1210: 20 28 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e   (SQLITE_VERSION
1220: 5f 4e 55 4d 42 45 52 3e 3d 33 30 30 31 30 30 31  _NUMBER>=3001001
1230: 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  )..**.** See als
1240: 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76  o: [sqlite3_libv
1250: 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73  ersion()] and [s
1260: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
1270: 6e 5f 6e 75 6d 62 65 72 28 29 5d 2e 0a 2a 2f 0a  n_number()]..*/.
1280: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56  #define SQLITE_V
1290: 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20 20 22  ERSION         "
12a0: 33 2e 35 2e 30 22 0a 23 64 65 66 69 6e 65 20 53  3.5.0".#define S
12b0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
12c0: 4d 42 45 52 20 33 30 30 35 30 30 30 0a 0a 2f 2a  MBER 3005000../*
12d0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75  .** CAPI3REF: Ru
12e0: 6e 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56  n-Time Library V
12f0: 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 0a 2a  ersion Numbers.*
1300: 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
1310: 6e 65 73 20 72 65 74 75 72 6e 20 76 61 6c 75 65  nes return value
1320: 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  s equivalent to 
1330: 74 68 65 20 68 65 61 64 65 72 20 63 6f 6e 73 74  the header const
1340: 61 6e 74 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ants.** [SQLITE_
1350: 56 45 52 53 49 4f 4e 5d 20 61 6e 64 20 5b 53 51  VERSION] and [SQ
1360: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d  LITE_VERSION_NUM
1370: 42 45 52 5d 2e 20 20 54 68 65 20 76 61 6c 75 65  BER].  The value
1380: 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79  s returned.** by
1390: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 73 20 73   this routines s
13a0: 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 64 69  hould only be di
13b0: 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65  fferent from the
13c0: 20 68 65 61 64 65 72 20 76 61 6c 75 65 73 0a 2a   header values.*
13d0: 2a 20 69 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65  * if you compile
13e0: 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 75 73   your program us
13f0: 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 33 2e 68  ing an sqlite3.h
1400: 20 68 65 61 64 65 72 20 66 72 6f 6d 20 61 0a 2a   header from a.*
1410: 2a 20 64 69 66 66 65 72 65 6e 74 20 76 65 72 73  * different vers
1420: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68  ion of SQLite th
1430: 61 74 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6f  at the version o
1440: 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 79 6f  f the library yo
1450: 75 0a 2a 2a 20 6c 69 6e 6b 20 61 67 61 69 6e 73  u.** link agains
1460: 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  t..**.** The sql
1470: 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73  ite3_version[] s
1480: 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 63  tring constant c
1490: 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 74  ontains the text
14a0: 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49   of the.** [SQLI
14b0: 54 45 5f 56 45 52 53 49 4f 4e 5d 20 73 74 72 69  TE_VERSION] stri
14c0: 6e 67 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  ng.  The sqlite3
14d0: 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75  _libversion() fu
14e0: 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a 2a  nction returns.*
14f0: 2a 20 61 20 70 6f 69 6e 65 72 20 74 6f 20 74 68  * a poiner to th
1500: 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  e sqlite3_versio
1510: 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74  n[] string const
1520: 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69  ant.  The functi
1530: 6f 6e 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65  on.** is provide
1540: 64 20 66 6f 72 20 44 4c 4c 20 75 73 65 72 73 20  d for DLL users 
1550: 77 68 6f 20 63 61 6e 20 6f 6e 6c 79 20 61 63 63  who can only acc
1560: 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e  ess functions an
1570: 64 20 6e 6f 74 0a 2a 2a 20 63 6f 6e 73 74 61 6e  d not.** constan
1580: 74 73 20 77 69 74 68 69 6e 20 74 68 65 20 44 4c  ts within the DL
1590: 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 45 58 54  L..*/.SQLITE_EXT
15a0: 45 52 4e 20 63 6f 6e 73 74 20 63 68 61 72 20 73  ERN const char s
15b0: 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d  qlite3_version[]
15c0: 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
15d0: 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
15e0: 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64  _libversion(void
15f0: 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
1600: 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  t sqlite3_libver
1610: 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64  sion_number(void
1620: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
1630: 45 46 3a 20 54 65 73 74 20 54 6f 20 53 65 65 20  EF: Test To See 
1640: 49 66 20 54 68 65 20 4c 69 62 72 61 72 79 20 49  If The Library I
1650: 73 20 54 68 72 65 61 64 73 61 66 65 0a 2a 2a 0a  s Threadsafe.**.
1660: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1670: 72 65 74 75 72 6e 73 20 54 52 55 45 20 28 6e 6f  returns TRUE (no
1680: 6e 7a 65 72 6f 29 20 69 66 20 53 51 4c 69 74 65  nzero) if SQLite
1690: 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69   was compiled wi
16a0: 74 68 0a 2a 2a 20 61 6c 6c 20 6f 66 20 69 74 73  th.** all of its
16b0: 20 6d 75 74 65 78 65 73 20 65 6e 61 62 6c 65 64   mutexes enabled
16c0: 20 61 6e 64 20 69 73 20 74 68 75 73 20 74 68 72   and is thus thr
16d0: 65 61 64 73 61 66 65 2e 20 20 49 74 20 72 65 74  eadsafe.  It ret
16e0: 75 72 6e 73 0a 2a 2a 20 7a 65 72 6f 20 69 66 20  urns.** zero if 
16f0: 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 62  the particular b
1700: 75 69 6c 64 20 69 73 20 66 6f 72 20 73 69 6e 67  uild is for sing
1710: 6c 65 2d 74 68 72 65 61 64 65 64 20 6f 70 65 72  le-threaded oper
1720: 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a  ation.** only..*
1730: 2a 0a 2a 2a 20 52 65 61 6c 6c 79 20 61 6c 6c 20  *.** Really all 
1740: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  this routine doe
1750: 73 20 69 73 20 72 65 74 75 72 6e 20 74 72 75 65  s is return true
1760: 20 69 66 20 53 51 4c 69 74 65 20 77 61 73 20 63   if SQLite was c
1770: 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20  ompiled.** with 
1780: 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 54 48 52  the -DSQLITE_THR
1790: 45 41 44 53 41 46 45 3d 31 20 6f 70 74 69 6f 6e  EADSAFE=1 option
17a0: 20 61 6e 64 20 66 61 6c 73 65 20 69 66 0a 2a 2a   and false if.**
17b0: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 2d   compiled with -
17c0: 44 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  DSQLITE_THREADSA
17d0: 46 45 3d 30 2e 20 20 49 66 20 53 51 4c 69 74 65  FE=0.  If SQLite
17e0: 20 75 73 65 73 20 61 6e 0a 2a 2a 20 61 70 70 6c   uses an.** appl
17f0: 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
1800: 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2c  mutex subsystem,
1810: 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65   malloc subsyste
1820: 6d 2c 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20  m, collating.** 
1830: 73 65 71 75 65 6e 63 65 2c 20 56 46 53 2c 20 53  sequence, VFS, S
1840: 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 20 70 72 6f  QL function, pro
1850: 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 2c 20  gress callback, 
1860: 63 6f 6d 6d 69 74 20 68 6f 6f 6b 2c 0a 2a 2a 20  commit hook,.** 
1870: 65 78 74 65 6e 73 69 6f 6e 2c 20 6f 72 20 6f 74  extension, or ot
1880: 68 65 72 20 61 63 63 65 73 73 6f 72 69 65 73 20  her accessories 
1890: 61 6e 64 20 74 68 65 73 65 20 61 64 64 2d 6f 6e  and these add-on
18a0: 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 68 72  s are not.** thr
18b0: 65 61 64 73 61 66 65 2c 20 74 68 65 6e 20 63 6c  eadsafe, then cl
18c0: 65 61 72 6c 79 20 74 68 65 20 63 6f 6d 62 69 6e  early the combin
18d0: 61 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 62  ation will not b
18e0: 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20  e threadsafe.** 
18f0: 65 69 74 68 65 72 2e 20 20 48 65 6e 63 65 2c 20  either.  Hence, 
1900: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76  this routine nev
1910: 65 72 20 72 65 70 6f 72 74 73 20 74 68 61 74 20  er reports that 
1920: 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 69  the library.** i
1930: 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  s guaranteed to 
1940: 62 65 20 74 68 72 65 61 64 73 61 66 65 2c 20 6f  be threadsafe, o
1950: 6e 6c 79 20 77 68 65 6e 20 69 74 20 69 73 20 67  nly when it is g
1960: 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 0a 2a 2a  uaranteed not.**
1970: 20 74 6f 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   to be..**.** Th
1980: 69 73 20 69 73 20 61 6e 20 65 78 70 65 72 69 6d  is is an experim
1990: 65 6e 74 61 6c 20 41 50 49 20 61 6e 64 20 6d 61  ental API and ma
19a0: 79 20 67 6f 20 61 77 61 79 20 6f 72 20 63 68 61  y go away or cha
19b0: 6e 67 65 20 69 6e 20 66 75 74 75 72 65 0a 2a 2a  nge in future.**
19c0: 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2f 0a 53 51   releases..*/.SQ
19d0: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
19e0: 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28  ite3_threadsafe(
19f0: 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  void);../*.** CA
1a00: 50 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 65  PI3REF: Database
1a10: 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 48 61 6e 64   Connection Hand
1a20: 6c 65 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f 70  le.**.** Each op
1a30: 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  en SQLite databa
1a40: 73 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65  se is represente
1a50: 64 20 62 79 20 70 6f 69 6e 74 65 72 20 74 6f 20  d by pointer to 
1a60: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
1a70: 68 65 0a 2a 2a 20 6f 70 61 71 75 65 20 73 74 72  he.** opaque str
1a80: 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 73 71  ucture named "sq
1a90: 6c 69 74 65 33 22 2e 20 20 49 74 20 69 73 20 75  lite3".  It is u
1aa0: 73 65 66 75 6c 20 74 6f 20 74 68 69 6e 6b 20 6f  seful to think o
1ab0: 66 20 61 6e 20 73 71 6c 69 74 65 33 0a 2a 2a 20  f an sqlite3.** 
1ac0: 70 6f 69 6e 74 65 72 20 61 73 20 61 6e 20 6f 62  pointer as an ob
1ad0: 6a 65 63 74 2e 20 20 54 68 65 20 5b 73 71 6c 69  ject.  The [sqli
1ae0: 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71  te3_open()], [sq
1af0: 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c  lite3_open16()],
1b00: 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   and.** [sqlite3
1b10: 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65  _open_v2()] inte
1b20: 72 66 61 63 65 73 20 61 72 65 20 69 74 73 20 63  rfaces are its c
1b30: 6f 6e 73 74 72 75 63 74 6f 72 73 0a 2a 2a 20 61  onstructors.** a
1b40: 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73  nd [sqlite3_clos
1b50: 65 28 29 5d 20 69 73 20 69 74 73 20 64 65 73 74  e()] is its dest
1b60: 72 75 63 74 6f 72 2e 20 20 54 68 65 72 65 20 61  ructor.  There a
1b70: 72 65 20 6d 61 6e 79 20 6f 74 68 65 72 20 69 6e  re many other in
1b80: 74 65 72 66 61 63 65 73 0a 2a 2a 20 28 73 75 63  terfaces.** (suc
1b90: 68 20 61 73 20 5b 73 71 6c 69 74 65 33 5f 70 72  h as [sqlite3_pr
1ba0: 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71  epare_v2()], [sq
1bb0: 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
1bc0: 63 74 69 6f 6e 28 29 5d 2c 20 61 6e 64 0a 2a 2a  ction()], and.**
1bd0: 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74   [sqlite3_busy_t
1be0: 69 6d 65 6f 75 74 28 29 5d 20 74 6f 20 6e 61 6d  imeout()] to nam
1bf0: 65 20 62 75 74 20 74 68 72 65 65 29 20 74 68 61  e but three) tha
1c00: 74 20 61 72 65 20 6d 65 74 68 6f 64 73 20 6f 6e  t are methods on
1c10: 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e   this.** object.
1c20: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
1c30: 63 74 20 73 71 6c 69 74 65 33 20 73 71 6c 69 74  ct sqlite3 sqlit
1c40: 65 33 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  e3;.../*.** CAPI
1c50: 33 52 45 46 3a 20 36 34 2d 42 69 74 20 49 6e 74  3REF: 64-Bit Int
1c60: 65 67 65 72 20 54 79 70 65 73 0a 2a 2a 0a 2a 2a  eger Types.**.**
1c70: 20 53 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20   Some compilers 
1c80: 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74  do not support t
1c90: 68 65 20 22 6c 6f 6e 67 20 6c 6f 6e 67 22 20 64  he "long long" d
1ca0: 61 74 61 74 79 70 65 2e 20 20 53 6f 20 77 65 20  atatype.  So we 
1cb0: 68 61 76 65 0a 2a 2a 20 74 6f 20 64 6f 20 63 6f  have.** to do co
1cc0: 6d 70 69 6c 65 72 2d 73 70 65 63 69 66 69 63 20  mpiler-specific 
1cd0: 74 79 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d  typedefs for 64-
1ce0: 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75  bit signed and u
1cf0: 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73  nsigned integers
1d00: 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 4c  ..**.** Many SQL
1d10: 69 74 65 20 69 6e 74 65 72 66 61 63 65 20 66 75  ite interface fu
1d20: 6e 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 20  nctions require 
1d30: 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  a 64-bit integer
1d40: 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 20 54   arguments..** T
1d50: 68 6f 73 65 20 69 6e 74 65 72 66 61 63 65 73 20  hose interfaces 
1d60: 61 72 65 20 64 65 63 6c 61 72 65 64 20 75 73 69  are declared usi
1d70: 6e 67 20 74 68 69 73 20 74 79 70 65 64 65 66 2e  ng this typedef.
1d80: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1d90: 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20 20 74  E_INT64_TYPE.  t
1da0: 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f 49 4e  ypedef SQLITE_IN
1db0: 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65 5f  T64_TYPE sqlite_
1dc0: 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66  int64;.  typedef
1dd0: 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49 54 45   unsigned SQLITE
1de0: 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69  _INT64_TYPE sqli
1df0: 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 69 66  te_uint64;.#elif
1e00: 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
1e10: 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  R) || defined(__
1e20: 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20 74 79  BORLANDC__).  ty
1e30: 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20 73 71  pedef __int64 sq
1e40: 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79  lite_int64;.  ty
1e50: 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 5f  pedef unsigned _
1e60: 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 69  _int64 sqlite_ui
1e70: 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20 74 79  nt64;.#else.  ty
1e80: 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67 20  pedef long long 
1e90: 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  int sqlite_int64
1ea0: 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 69  ;.  typedef unsi
1eb0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69  gned long long i
1ec0: 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  nt sqlite_uint64
1ed0: 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65 66  ;.#endif.typedef
1ee0: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71   sqlite_int64 sq
1ef0: 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74 79 70  lite3_int64;.typ
1f00: 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e 74  edef sqlite_uint
1f10: 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36  64 sqlite3_uint6
1f20: 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d  4;../*.** If com
1f30: 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 6f  piling for a pro
1f40: 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63 6b  cessor that lack
1f50: 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  s floating point
1f60: 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62   support,.** sub
1f70: 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72 20  stitute integer 
1f80: 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69  for floating-poi
1f90: 6e 74 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  nt.*/.#ifdef SQL
1fa0: 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
1fb0: 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65  G_POINT.# define
1fc0: 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f   double sqlite3_
1fd0: 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a  int64.#endif../*
1fe0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6c  .** CAPI3REF: Cl
1ff0: 6f 73 69 6e 67 20 41 20 44 61 74 61 62 61 73 65  osing A Database
2000: 20 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 0a 2a   Connection.**.*
2010: 2a 20 43 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  * Call this func
2020: 74 69 6f 6e 20 77 69 74 68 20 61 20 70 6f 69 6e  tion with a poin
2030: 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 74 75  ter to a structu
2040: 72 65 20 74 68 61 74 20 77 61 73 20 70 72 65 76  re that was prev
2050: 69 6f 75 73 6c 79 0a 2a 2a 20 72 65 74 75 72 6e  iously.** return
2060: 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
2070: 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74  _open()], [sqlit
2080: 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72  e3_open16()], or
2090: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  .** [sqlite3_ope
20a0: 6e 5f 76 32 28 29 5d 20 61 6e 64 20 74 68 65 20  n_v2()] and the 
20b0: 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61  corresponding da
20c0: 74 61 62 61 73 65 20 77 69 6c 6c 20 62 79 0a 2a  tabase will by.*
20d0: 2a 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  * closed..**.** 
20e0: 41 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  All SQL statemen
20f0: 74 73 20 70 72 65 70 61 72 65 64 20 75 73 69 6e  ts prepared usin
2100: 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  g [sqlite3_prepa
2110: 72 65 5f 76 32 28 29 5d 20 6f 72 0a 2a 2a 20 5b  re_v2()] or.** [
2120: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
2130: 36 5f 76 32 28 29 5d 20 6d 75 73 74 20 62 65 20  6_v2()] must be 
2140: 64 65 73 74 72 6f 79 65 64 20 75 73 69 6e 67 20  destroyed using 
2150: 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
2160: 65 28 29 5d 0a 2a 2a 20 62 65 66 6f 72 65 20 74  e()].** before t
2170: 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2180: 61 6c 6c 65 64 2e 20 4f 74 68 65 72 77 69 73 65  alled. Otherwise
2190: 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  , SQLITE_BUSY is
21a0: 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
21b0: 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  e.** database co
21c0: 6e 6e 65 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73  nnection remains
21d0: 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 73   open..**.** Pas
21e0: 73 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  sing this routin
21f0: 65 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  e a database con
2200: 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 68 61 73  nection that has
2210: 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a   already been.**
2220: 20 63 6c 6f 73 65 64 20 72 65 73 75 6c 74 73 20   closed results 
2230: 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68  in undefined beh
2240: 61 76 69 6f 72 2e 20 20 49 66 20 6f 74 68 65 72  avior.  If other
2250: 20 69 6e 74 65 72 66 61 63 65 73 20 74 68 61 74   interfaces that
2260: 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 74 68  .** reference th
2270: 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
2280: 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 72 65 20 70  connection are p
2290: 65 6e 64 69 6e 67 20 28 65 69 74 68 65 72 20 69  ending (either i
22a0: 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68  n the.** same th
22b0: 72 65 61 64 20 6f 72 20 69 6e 20 64 69 66 66 65  read or in diffe
22c0: 72 65 6e 74 20 74 68 72 65 61 64 73 29 20 77 68  rent threads) wh
22d0: 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
22e0: 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68  is called,.** th
22f0: 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  en the behavior 
2300: 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  is undefined and
2310: 20 69 73 20 61 6c 6d 6f 73 74 20 63 65 72 74 61   is almost certa
2320: 69 6e 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65  inly undesirable
2330: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2340: 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73  int sqlite3_clos
2350: 65 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 0a 2f  e(sqlite3 *);../
2360: 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 66 6f  *.** The type fo
2370: 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  r a callback fun
2380: 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 69  ction..** This i
2390: 73 20 6c 65 67 61 63 79 20 61 6e 64 20 64 65 70  s legacy and dep
23a0: 72 65 63 61 74 65 64 2e 20 20 49 74 20 69 73 20  recated.  It is 
23b0: 69 6e 63 6c 75 64 65 64 20 66 6f 72 20 68 69 73  included for his
23c0: 74 6f 72 69 63 61 6c 0a 2a 2a 20 63 6f 6d 70 61  torical.** compa
23d0: 74 69 62 69 6c 69 74 79 20 61 6e 64 20 69 73 20  tibility and is 
23e0: 6e 6f 74 20 64 6f 63 75 6d 65 6e 74 65 64 2e 0a  not documented..
23f0: 2a 2f 0a 74 79 70 65 64 65 66 20 69 6e 74 20 28  */.typedef int (
2400: 2a 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63  *sqlite3_callbac
2410: 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61  k)(void*,int,cha
2420: 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a 0a 2f  r**, char**);../
2430: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
2440: 6e 65 2d 53 74 65 70 20 51 75 65 72 79 20 45 78  ne-Step Query Ex
2450: 65 63 75 74 69 6f 6e 20 49 6e 74 65 72 66 61 63  ecution Interfac
2460: 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  e.**.** This int
2470: 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74  erface is used t
2480: 6f 20 64 6f 20 61 20 6f 6e 65 2d 74 69 6d 65 20  o do a one-time 
2490: 65 76 61 6c 75 61 74 61 74 69 6f 6e 20 6f 66 20  evaluatation of 
24a0: 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20  zero.** or more 
24b0: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  SQL statements. 
24c0: 20 55 54 46 2d 38 20 74 65 78 74 20 6f 66 20 74   UTF-8 text of t
24d0: 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
24e0: 73 20 74 6f 0a 2a 2a 20 62 65 20 65 76 61 6c 75  s to.** be evalu
24f0: 74 65 64 20 69 73 20 70 61 73 73 65 64 20 69 6e  ted is passed in
2500: 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   as the second p
2510: 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 73  arameter.  The s
2520: 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 61 72 65  tatements.** are
2530: 20 70 72 65 70 61 72 65 64 20 6f 6e 65 20 62 79   prepared one by
2540: 20 6f 6e 65 20 75 73 69 6e 67 20 5b 73 71 6c 69   one using [sqli
2550: 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 2c 20  te3_prepare()], 
2560: 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 75 73 69  evaluated.** usi
2570: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ng [sqlite3_step
2580: 28 29 5d 2c 20 74 68 65 6e 20 64 65 73 74 72 6f  ()], then destro
2590: 79 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  yed using [sqlit
25a0: 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a  e3_finalize()]..
25b0: 2a 2a 0a 2a 2a 20 49 66 20 6f 6e 65 20 6f 72 20  **.** If one or 
25c0: 6d 6f 72 65 20 6f 66 20 74 68 65 20 53 51 4c 20  more of the SQL 
25d0: 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 71  statements are q
25e0: 75 65 72 69 65 73 2c 20 74 68 65 6e 0a 2a 2a 20  ueries, then.** 
25f0: 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
2600: 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
2610: 62 79 20 74 68 65 20 33 72 64 20 70 61 72 61 6d  by the 3rd param
2620: 65 74 65 72 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b  eter is.** invok
2630: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
2640: 20 72 6f 77 20 6f 66 20 74 68 65 20 71 75 65 72   row of the quer
2650: 79 20 72 65 73 75 6c 74 2e 20 20 54 68 69 73 20  y result.  This 
2660: 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 73 68 6f 75  callback.** shou
2670: 6c 64 20 6e 6f 72 6d 61 6c 6c 79 20 72 65 74 75  ld normally retu
2680: 72 6e 20 30 2e 20 20 49 66 20 74 68 65 20 63 61  rn 0.  If the ca
2690: 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 61  llback returns a
26a0: 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 76 61 6c   non-zero.** val
26b0: 75 65 20 74 68 65 6e 20 74 68 65 20 71 75 65 72  ue then the quer
26c0: 79 20 69 73 20 61 62 6f 72 74 65 64 2c 20 61 6c  y is aborted, al
26d0: 6c 20 73 75 62 73 65 71 75 65 6e 74 20 53 51 4c  l subsequent SQL
26e0: 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 61   statements.** a
26f0: 72 65 20 73 6b 69 70 70 65 64 20 61 6e 64 20 74  re skipped and t
2700: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  he sqlite3_exec(
2710: 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  ) function retur
2720: 6e 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 41  ns the [SQLITE_A
2730: 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  BORT]..**.** The
2740: 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2750: 6f 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65  o this interface
2760: 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79   is an arbitrary
2770: 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73   pointer that is
2780: 0a 2a 2a 20 70 61 73 73 65 64 20 74 68 72 6f 75  .** passed throu
2790: 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  gh to the callba
27a0: 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 69  ck function as i
27b0: 74 73 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  ts first paramet
27c0: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 32 6e  er..**.** The 2n
27d0: 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  d parameter to t
27e0: 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
27f0: 74 69 6f 6e 20 69 73 20 74 68 65 20 6e 75 6d 62  tion is the numb
2800: 65 72 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 73  er of.** columns
2810: 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 72 65   in the query re
2820: 73 75 6c 74 2e 20 20 54 68 65 20 33 72 64 20 70  sult.  The 3rd p
2830: 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
2840: 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 61  callback.** is a
2850: 6e 20 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e  n array of strin
2860: 67 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 76  gs holding the v
2870: 61 6c 75 65 73 20 66 6f 72 20 65 61 63 68 20 63  alues for each c
2880: 6f 6c 75 6d 6e 0a 2a 2a 20 61 73 20 65 78 74 72  olumn.** as extr
2890: 61 63 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c  acted using [sql
28a0: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
28b0: 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20  ()]..** The 4th 
28c0: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
28d0: 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20   callback is an 
28e0: 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 73  array of strings
28f0: 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 75 73 69  .** obtained usi
2900: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ng [sqlite3_colu
2910: 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61 6e 64 20 68  mn_name()] and h
2920: 6f 6c 64 69 6e 67 0a 2a 2a 20 74 68 65 20 6e 61  olding.** the na
2930: 6d 65 73 20 6f 66 20 65 61 63 68 20 63 6f 6c 75  mes of each colu
2940: 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  mn..**.** The ca
2950: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
2960: 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 65 76 65  may be NULL, eve
2970: 6e 20 66 6f 72 20 71 75 65 72 69 65 73 2e 20 20  n for queries.  
2980: 41 20 4e 55 4c 4c 0a 2a 2a 20 63 61 6c 6c 62 61  A NULL.** callba
2990: 63 6b 20 69 73 20 6e 6f 74 20 61 6e 20 65 72 72  ck is not an err
29a0: 6f 72 2e 20 20 49 74 20 6a 75 73 74 20 6d 65 61  or.  It just mea
29b0: 6e 73 20 74 68 61 74 20 6e 6f 20 63 61 6c 6c 62  ns that no callb
29c0: 61 63 6b 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 69  ack.** will be i
29d0: 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  nvoked..**.** If
29e0: 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
29f0: 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 20 6f   while parsing o
2a00: 72 20 65 76 61 6c 75 61 74 69 6e 67 20 74 68 65  r evaluating the
2a10: 20 53 51 4c 20 28 62 75 74 0a 2a 2a 20 6e 6f 74   SQL (but.** not
2a20: 20 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e 67   while executing
2a30: 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 29 20 74   the callback) t
2a40: 68 65 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61  hen an appropria
2a50: 74 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73  te error.** mess
2a60: 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  age is written i
2a70: 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
2a80: 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ned from [sqlite
2a90: 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 61 6e 64 0a  3_malloc()] and.
2aa0: 2a 2a 20 2a 65 72 72 6d 73 67 20 69 73 20 6d 61  ** *errmsg is ma
2ab0: 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  de to point to t
2ac0: 68 61 74 20 6d 65 73 73 61 67 65 2e 20 20 54 68  hat message.  Th
2ad0: 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
2ae0: 6f 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73  on.** is respons
2af0: 69 62 6c 65 20 66 6f 72 20 66 72 65 65 69 6e 67  ible for freeing
2b00: 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 69 6e   the memory usin
2b10: 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  g [sqlite3_free(
2b20: 29 5d 2e 0a 2a 2a 20 49 66 20 65 72 72 6d 73 67  )]..** If errmsg
2b30: 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 6e 6f 20  ==NULL, then no 
2b40: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73  error message is
2b50: 20 65 76 65 72 20 77 72 69 74 74 65 6e 2e 0a 2a   ever written..*
2b60: 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  *.** The return 
2b70: 76 61 6c 75 65 20 69 73 20 69 73 20 53 51 4c 49  value is is SQLI
2b80: 54 45 5f 4f 4b 20 69 66 20 74 68 65 72 65 20 61  TE_OK if there a
2b90: 72 65 20 6e 6f 20 65 72 72 6f 72 73 20 61 6e 64  re no errors and
2ba0: 0a 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b  .** some other [
2bb0: 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 74 75  SQLITE_OK | retu
2bc0: 72 6e 20 63 6f 64 65 5d 20 69 66 20 74 68 65 72  rn code] if ther
2bd0: 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 20 20  e is an error.  
2be0: 0a 2a 2a 20 54 68 65 20 70 61 72 74 69 63 75 6c  .** The particul
2bf0: 61 72 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  ar return value 
2c00: 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 74  depends on the t
2c10: 79 70 65 20 6f 66 20 65 72 72 6f 72 2e 20 0a 2a  ype of error. .*
2c20: 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  *.*/.SQLITE_API 
2c30: 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63  int sqlite3_exec
2c40: 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 20  (.  sqlite3*,   
2c50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c70: 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61  * An open databa
2c80: 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  se */.  const ch
2c90: 61 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 20  ar *sql,        
2ca0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb0: 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20     /* SQL to be 
2cc0: 65 76 61 6c 75 74 65 64 20 2a 2f 0a 20 20 69 6e  evaluted */.  in
2cd0: 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f  t (*callback)(vo
2ce0: 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63  id*,int,char**,c
2cf0: 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c  har**),  /* Call
2d00: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  back function */
2d10: 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20  .  void *,      
2d20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d40: 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f   1st argument to
2d50: 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63   callback */.  c
2d60: 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20  har **errmsg    
2d70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d80: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
2d90: 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68  or msg written h
2da0: 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ere */.);../*.**
2db0: 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c   CAPI3REF: Resul
2dc0: 74 20 43 6f 64 65 73 0a 2a 2a 20 4b 45 59 57 4f  t Codes.** KEYWO
2dd0: 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  RDS: SQLITE_OK.*
2de0: 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 65  *.** Many SQLite
2df0: 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
2e00: 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73  n an integer res
2e10: 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68  ult code from th
2e20: 65 20 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 61  e set shown.** a
2e30: 62 6f 76 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  bove in order to
2e40: 20 69 6e 64 69 63 61 74 65 73 20 73 75 63 63 65   indicates succe
2e50: 73 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a  ss or failure..*
2e60: 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20  *.** The result 
2e70: 63 6f 64 65 73 20 61 62 6f 76 65 20 61 72 65 20  codes above are 
2e80: 74 68 65 20 6f 6e 6c 79 20 6f 6e 65 73 20 72 65  the only ones re
2e90: 74 75 72 6e 65 64 20 62 79 20 53 51 4c 69 74 65  turned by SQLite
2ea0: 20 69 6e 20 69 74 73 0a 2a 2a 20 64 65 66 61 75   in its.** defau
2eb0: 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  lt configuration
2ec0: 2e 20 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20  .  However, the 
2ed0: 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65  [sqlite3_extende
2ee0: 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29  d_result_codes()
2ef0: 5d 0a 2a 2a 20 41 50 49 20 63 61 6e 20 62 65 20  ].** API can be 
2f00: 75 73 65 64 20 74 6f 20 73 65 74 20 61 20 64 61  used to set a da
2f10: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 6f 69  tabase connectoi
2f20: 6e 20 74 6f 20 72 65 74 75 72 6e 20 6d 6f 72 65  n to return more
2f30: 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20 72 65 73   detailed.** res
2f40: 75 6c 74 20 63 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a  ult codes..**.**
2f50: 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49   See also: [SQLI
2f60: 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c 20  TE_IOERR_READ | 
2f70: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
2f80: 63 6f 64 65 73 5d 0a 2a 2a 0a 2a 2f 0a 23 64 65  codes].**.*/.#de
2f90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20  fine SQLITE_OK  
2fa0: 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20           0   /* 
2fb0: 53 75 63 63 65 73 73 66 75 6c 20 72 65 73 75 6c  Successful resul
2fc0: 74 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e  t */./* beginnin
2fd0: 67 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73  g-of-error-codes
2fe0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
2ff0: 54 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20 20  TE_ERROR        
3000: 31 20 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f 72  1   /* SQL error
3010: 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61   or missing data
3020: 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  base */.#define 
3030: 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20  SQLITE_INTERNAL 
3040: 20 20 20 20 32 20 20 20 2f 2a 20 4e 4f 54 20 55      2   /* NOT U
3050: 53 45 44 2e 20 49 6e 74 65 72 6e 61 6c 20 6c 6f  SED. Internal lo
3060: 67 69 63 20 65 72 72 6f 72 20 69 6e 20 53 51 4c  gic error in SQL
3070: 69 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ite */.#define S
3080: 51 4c 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20  QLITE_PERM      
3090: 20 20 20 33 20 20 20 2f 2a 20 41 63 63 65 73 73     3   /* Access
30a0: 20 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69   permission deni
30b0: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
30c0: 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20  LITE_ABORT      
30d0: 20 20 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63    4   /* Callbac
30e0: 6b 20 72 6f 75 74 69 6e 65 20 72 65 71 75 65 73  k routine reques
30f0: 74 65 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a  ted an abort */.
3100: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42  #define SQLITE_B
3110: 55 53 59 20 20 20 20 20 20 20 20 20 35 20 20 20  USY         5   
3120: 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
3130: 66 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a  file is locked *
3140: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3150: 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 36 20  _LOCKED       6 
3160: 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20    /* A table in 
3170: 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
3180: 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e  locked */.#defin
3190: 65 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20  e SQLITE_NOMEM  
31a0: 20 20 20 20 20 20 37 20 20 20 2f 2a 20 41 20 6d        7   /* A m
31b0: 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 2a  alloc() failed *
31c0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
31d0: 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20  _READONLY     8 
31e0: 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20    /* Attempt to 
31f0: 77 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79  write a readonly
3200: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65   database */.#de
3210: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  fine SQLITE_INTE
3220: 52 52 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20  RRUPT    9   /* 
3230: 4f 70 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e  Operation termin
3240: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ated by sqlite3_
3250: 69 6e 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 64  interrupt()*/.#d
3260: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
3270: 52 52 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a  RR       10   /*
3280: 20 53 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69   Some kind of di
3290: 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  sk I/O error occ
32a0: 75 72 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  urred */.#define
32b0: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
32c0: 20 20 20 20 31 31 20 20 20 2f 2a 20 54 68 65 20      11   /* The 
32d0: 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d  database disk im
32e0: 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64  age is malformed
32f0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3300: 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31  TE_NOTFOUND    1
3310: 32 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e  2   /* NOT USED.
3320: 20 54 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 64   Table or record
3330: 20 6e 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64   not found */.#d
3340: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c  efine SQLITE_FUL
3350: 4c 20 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a  L        13   /*
3360: 20 49 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65   Insertion faile
3370: 64 20 62 65 63 61 75 73 65 20 64 61 74 61 62 61  d because databa
3380: 73 65 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64  se is full */.#d
3390: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e  efine SQLITE_CAN
33a0: 54 4f 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a  TOPEN    14   /*
33b0: 20 55 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20   Unable to open 
33c0: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
33d0: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
33e0: 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20  ITE_PROTOCOL    
33f0: 31 35 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44  15   /* NOT USED
3400: 2e 20 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20  . Database lock 
3410: 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a  protocol error *
3420: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3430: 5f 45 4d 50 54 59 20 20 20 20 20 20 20 31 36 20  _EMPTY       16 
3440: 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73    /* Database is
3450: 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e   empty */.#defin
3460: 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  e SQLITE_SCHEMA 
3470: 20 20 20 20 20 31 37 20 20 20 2f 2a 20 54 68 65       17   /* The
3480: 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
3490: 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66   changed */.#def
34a0: 69 6e 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49  ine SQLITE_TOOBI
34b0: 47 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20 53  G      18   /* S
34c0: 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78  tring or BLOB ex
34d0: 63 65 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 74  ceeds size limit
34e0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
34f0: 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 31  TE_CONSTRAINT  1
3500: 39 20 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 65  9   /* Abort due
3510: 20 74 6f 20 63 6f 6e 74 72 61 69 6e 74 20 76 69   to contraint vi
3520: 6f 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  olation */.#defi
3530: 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54  ne SQLITE_MISMAT
3540: 43 48 20 20 20 20 32 30 20 20 20 2f 2a 20 44 61  CH    20   /* Da
3550: 74 61 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68  ta type mismatch
3560: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3570: 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20 32  TE_MISUSE      2
3580: 31 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 75  1   /* Library u
3590: 73 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20  sed incorrectly 
35a0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
35b0: 45 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 32  E_NOLFS       22
35c0: 20 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 65     /* Uses OS fe
35d0: 61 74 75 72 65 73 20 6e 6f 74 20 73 75 70 70 6f  atures not suppo
35e0: 72 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a  rted on host */.
35f0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
3600: 55 54 48 20 20 20 20 20 20 20 20 32 33 20 20 20  UTH        23   
3610: 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e  /* Authorization
3620: 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69   denied */.#defi
3630: 6e 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54  ne SQLITE_FORMAT
3640: 20 20 20 20 20 20 32 34 20 20 20 2f 2a 20 41 75        24   /* Au
3650: 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
3660: 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f   format error */
3670: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3680: 52 41 4e 47 45 20 20 20 20 20 20 20 32 35 20 20  RANGE       25  
3690: 20 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65   /* 2nd paramete
36a0: 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e  r to sqlite3_bin
36b0: 64 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a  d out of range *
36c0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
36d0: 5f 4e 4f 54 41 44 42 20 20 20 20 20 20 32 36 20  _NOTADB      26 
36e0: 20 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64    /* File opened
36f0: 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 64   that is not a d
3700: 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
3710: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
3720: 4f 57 20 20 20 20 20 20 20 20 20 31 30 30 20 20  OW         100  
3730: 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  /* sqlite3_step(
3740: 29 20 68 61 73 20 61 6e 6f 74 68 65 72 20 72 6f  ) has another ro
3750: 77 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 69  w ready */.#defi
3760: 6e 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20  ne SQLITE_DONE  
3770: 20 20 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71        101  /* sq
3780: 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 73  lite3_step() has
3790: 20 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74   finished execut
37a0: 69 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66  ing */./* end-of
37b0: 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a  -error-codes */.
37c0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
37d0: 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74   Extended Result
37e0: 20 43 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 49 6e 20   Codes.**.** In 
37f0: 69 74 73 20 64 65 66 61 75 6c 74 20 63 6f 6e 66  its default conf
3800: 69 67 75 72 61 74 69 6f 6e 2c 20 53 51 4c 69 74  iguration, SQLit
3810: 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 72  e API routines r
3820: 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 32 36 20  eturn one of 26 
3830: 69 6e 74 65 67 65 72 0a 2a 2a 20 72 65 73 75 6c  integer.** resul
3840: 74 20 63 6f 64 65 73 20 64 65 73 63 72 69 62 65  t codes describe
3850: 64 20 61 74 20 72 65 73 75 6c 74 2d 63 6f 64 65  d at result-code
3860: 73 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 78 70  s.  However, exp
3870: 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 6f 77  erience has show
3880: 6e 20 74 68 61 74 0a 2a 2a 20 6d 61 6e 79 20 6f  n that.** many o
3890: 66 20 74 68 65 73 65 20 72 65 73 75 6c 74 20 63  f these result c
38a0: 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63 6f 75  odes are too cou
38b0: 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20 54 68  rse-grained.  Th
38c0: 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64  ey do not provid
38d0: 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66  e as.** much inf
38e0: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 70  ormation about p
38f0: 72 6f 62 6c 65 6d 73 20 61 73 20 75 73 65 72 73  roblems as users
3900: 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 6e   might like.  In
3910: 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a 2a   an effort to.**
3920: 20 61 64 64 72 65 73 73 20 74 68 69 73 2c 20 6e   address this, n
3930: 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  ewer versions of
3940: 20 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f 6e   SQLite (version
3950: 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 72   3.3.8 and later
3960: 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 70  ) include.** sup
3970: 70 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69 6f  port for additio
3980: 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 73  nal result codes
3990: 20 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d 6f   that provide mo
39a0: 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66 6f  re detailed info
39b0: 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74  rmation.** about
39c0: 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 65 78   errors.  The ex
39d0: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
39e0: 64 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20  des are enabled 
39f0: 28 6f 72 20 64 69 73 61 62 6c 65 64 29 20 66 6f  (or disabled) fo
3a00: 72 20 0a 2a 2a 20 65 61 63 68 20 64 61 74 61 62  r .** each datab
3a10: 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
3a20: 6e 20 75 73 69 6e 67 20 74 68 65 20 5b 73 71 6c  n using the [sql
3a30: 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
3a40: 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 20 41 50  sult_codes()] AP
3a50: 49 2e 0a 2a 2a 20 0a 2a 2a 20 53 6f 6d 65 20 6f  I..** .** Some o
3a60: 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  f the available 
3a70: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
3a80: 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74 65 64  codes are listed
3a90: 20 61 62 6f 76 65 2e 0a 2a 2a 20 57 65 20 65 78   above..** We ex
3aa0: 70 65 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20  pect the number 
3ab0: 6f 66 20 65 78 74 65 6e 64 65 64 20 72 65 73 75  of extended resu
3ac0: 6c 74 20 63 6f 64 65 73 20 77 69 6c 6c 20 62 65  lt codes will be
3ad0: 20 65 78 70 61 6e 64 0a 2a 2a 20 6f 76 65 72 20   expand.** over 
3ae0: 74 69 6d 65 2e 20 20 53 6f 66 74 77 61 72 65 20  time.  Software 
3af0: 74 68 61 74 20 75 73 65 73 20 65 78 74 65 6e 64  that uses extend
3b00: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ed result codes 
3b10: 73 68 6f 75 6c 64 20 65 78 70 65 63 74 0a 2a 2a  should expect.**
3b20: 20 74 6f 20 73 65 65 20 6e 65 77 20 72 65 73 75   to see new resu
3b30: 6c 74 20 63 6f 64 65 73 20 69 6e 20 66 75 74 75  lt codes in futu
3b40: 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53  re releases of S
3b50: 51 4c 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  QLite..** .** Th
3b60: 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20  e symbolic name 
3b70: 66 6f 72 20 61 6e 20 65 78 74 65 6e 64 65 64 20  for an extended 
3b80: 72 65 73 75 6c 74 20 63 6f 64 65 20 61 6c 77 61  result code alwa
3b90: 79 73 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65  ys contains a re
3ba0: 6c 61 74 65 64 0a 2a 2a 20 70 72 69 6d 61 72 79  lated.** primary
3bb0: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 61 73 20   result code as 
3bc0: 61 20 70 72 65 66 69 78 2e 20 20 50 72 69 6d 61  a prefix.  Prima
3bd0: 72 79 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ry result codes 
3be0: 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65  contain a single
3bf0: 0a 2a 2a 20 22 5f 22 20 63 68 61 72 61 63 74 65  .** "_" characte
3c00: 72 2e 20 20 45 78 74 65 6e 64 65 64 20 72 65 73  r.  Extended res
3c10: 75 6c 74 20 63 6f 64 65 73 20 63 6f 6e 74 61 69  ult codes contai
3c20: 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 22 5f  n two or more "_
3c30: 22 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a  " characters..**
3c40: 20 54 68 65 20 6e 75 6d 65 72 69 63 20 76 61 6c   The numeric val
3c50: 75 65 20 6f 66 20 61 6e 20 65 78 74 65 6e 64 65  ue of an extende
3c60: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 20 63 61  d result code ca
3c70: 6e 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74  n be converted t
3c80: 6f 20 69 74 73 0a 2a 2a 20 63 6f 72 72 65 73 70  o its.** corresp
3c90: 6f 6e 64 69 6e 67 20 70 72 69 6d 61 72 79 20 72  onding primary r
3ca0: 65 73 75 6c 74 20 63 6f 64 65 20 62 79 20 6d 61  esult code by ma
3cb0: 73 6b 69 6e 67 20 6f 66 66 20 74 68 65 20 6c 6f  sking off the lo
3cc0: 77 65 72 20 38 20 62 79 74 65 73 2e 0a 2a 2a 0a  wer 8 bytes..**.
3cd0: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b  ** The SQLITE_OK
3ce0: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c   result code wil
3cf0: 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e  l never be exten
3d00: 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c  ded.  It will al
3d10: 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74  ways.** be exact
3d20: 6c 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66  ly zero..*/.#def
3d30: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
3d40: 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 28  _READ          (
3d50: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
3d60: 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  1<<8)).#define S
3d70: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
3d80: 54 5f 52 45 41 44 20 20 20 20 28 53 51 4c 49 54  T_READ    (SQLIT
3d90: 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c 3c 38 29  E_IOERR | (2<<8)
3da0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
3db0: 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 20 20 20  _IOERR_WRITE    
3dc0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
3dd0: 52 52 20 7c 20 28 33 3c 3c 38 29 29 0a 23 64 65  RR | (3<<8)).#de
3de0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52  fine SQLITE_IOER
3df0: 52 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20 20  R_FSYNC         
3e00: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
3e10: 28 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (4<<8)).#define 
3e20: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52  SQLITE_IOERR_DIR
3e30: 5f 46 53 59 4e 43 20 20 20 20 20 28 53 51 4c 49  _FSYNC     (SQLI
3e40: 54 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38  TE_IOERR | (5<<8
3e50: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
3e60: 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45  E_IOERR_TRUNCATE
3e70: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
3e80: 45 52 52 20 7c 20 28 36 3c 3c 38 29 29 0a 23 64  ERR | (6<<8)).#d
3e90: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
3ea0: 52 52 5f 46 53 54 41 54 20 20 20 20 20 20 20 20  RR_FSTAT        
3eb0: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
3ec0: 20 28 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (7<<8)).#define
3ed0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
3ee0: 4c 4f 43 4b 20 20 20 20 20 20 20 20 28 53 51 4c  LOCK        (SQL
3ef0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c  ITE_IOERR | (8<<
3f00: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
3f10: 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20  TE_IOERR_RDLOCK 
3f20: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
3f30: 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23  OERR | (9<<8)).#
3f40: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
3f50: 45 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20 20  ERR_DELETE      
3f60: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
3f70: 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64 65 66 69  | (10<<8)).#defi
3f80: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
3f90: 42 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 28 53  BLOCKED       (S
3fa0: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31  QLITE_IOERR | (1
3fb0: 31 3c 3c 38 29 29 0a 0a 2f 2a 0a 2a 2a 20 43 41  1<<8))../*.** CA
3fc0: 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20 46 6f  PI3REF: Flags Fo
3fd0: 72 20 46 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72  r File Open Oper
3fe0: 61 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 43 6f 6d  ations.**.** Com
3ff0: 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  bination of the 
4000: 66 6f 6c 6c 6f 77 69 6e 67 20 62 69 74 20 76 61  following bit va
4010: 6c 75 65 73 20 61 72 65 20 75 73 65 64 20 61 73  lues are used as
4020: 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72   the.** third ar
4030: 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73  gument to the [s
4040: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
4050: 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 0a  ] interface and.
4060: 2a 2a 20 61 73 20 66 6f 75 72 74 68 20 61 72 67  ** as fourth arg
4070: 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78 4f 70  ument to the xOp
4080: 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  en method of the
4090: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  .** [sqlite3_vfs
40a0: 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2f 0a  ] object..**.*/.
40b0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
40c0: 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20  PEN_READONLY    
40d0: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31 0a       0x00000001.
40e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
40f0: 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 20 20  PEN_READWRITE   
4100: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 32 0a       0x00000002.
4110: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
4120: 50 45 4e 5f 43 52 45 41 54 45 20 20 20 20 20 20  PEN_CREATE      
4130: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 34 0a       0x00000004.
4140: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
4150: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
4160: 45 20 20 20 20 30 78 30 30 30 30 30 30 30 38 0a  E    0x00000008.
4170: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
4180: 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 20 20  PEN_EXCLUSIVE   
4190: 20 20 20 20 20 30 78 30 30 30 30 30 30 31 30 0a       0x00000010.
41a0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
41b0: 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20  PEN_MAIN_DB     
41c0: 20 20 20 20 20 30 78 30 30 30 30 30 31 30 30 0a       0x00000100.
41d0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
41e0: 50 45 4e 5f 54 45 4d 50 5f 44 42 20 20 20 20 20  PEN_TEMP_DB     
41f0: 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30 0a       0x00000200.
4200: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
4210: 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42  PEN_TRANSIENT_DB
4220: 20 20 20 20 20 30 78 30 30 30 30 30 34 30 30 0a       0x00000400.
4230: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
4240: 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
4250: 20 20 20 20 20 30 78 30 30 30 30 30 38 30 30 0a       0x00000800.
4260: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
4270: 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c  PEN_TEMP_JOURNAL
4280: 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30 0a       0x00001000.
4290: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
42a0: 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20  PEN_SUBJOURNAL  
42b0: 20 20 20 20 20 30 78 30 30 30 30 32 30 30 30 0a       0x00002000.
42c0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
42d0: 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e  PEN_MASTER_JOURN
42e0: 41 4c 20 20 20 30 78 30 30 30 30 34 30 30 30 0a  AL   0x00004000.
42f0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4300: 20 44 65 76 69 63 65 20 43 68 61 72 61 63 74 65   Device Characte
4310: 72 69 73 74 69 63 73 0a 2a 2a 0a 2a 2a 20 54 68  ristics.**.** Th
4320: 65 20 78 44 65 76 69 63 65 43 61 70 61 62 69 6c  e xDeviceCapabil
4330: 69 74 69 65 73 20 6d 65 74 68 6f 64 20 6f 66 20  ities method of 
4340: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
4350: 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65  methods].** obje
4360: 63 74 20 72 65 74 75 72 6e 73 20 61 6e 20 69 6e  ct returns an in
4370: 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 61  teger which is a
4380: 20 76 65 63 74 6f 72 20 6f 66 20 74 68 65 20 66   vector of the f
4390: 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 62 69 74 20  ollowing.** bit 
43a0: 76 61 6c 75 65 73 20 65 78 70 72 65 73 73 69 6e  values expressin
43b0: 67 20 49 2f 4f 20 63 68 61 72 61 63 74 65 72 69  g I/O characteri
43c0: 73 74 69 63 73 20 6f 66 20 74 68 65 20 6d 61 73  stics of the mas
43d0: 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20 64 65 76  s storage.** dev
43e0: 69 63 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ice that holds t
43f0: 68 65 20 66 69 6c 65 20 74 68 61 74 20 74 68 65  he file that the
4400: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
4410: 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65 72 73 20  hods].** refers 
4420: 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  to..**.** The SQ
4430: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
4440: 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73  C property means
4450: 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73   that all writes
4460: 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20   of.** any size 
4470: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65  are atomic.  The
4480: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
4490: 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a  OMICnnn values.*
44a0: 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74  * mean that writ
44b0: 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61  es of blocks tha
44c0: 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20  t are nnn bytes 
44d0: 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61  in size and.** a
44e0: 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e  re aligned to an
44f0: 20 61 64 64 72 65 73 73 20 77 68 69 63 68 20 69   address which i
4500: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c  s an integer mul
4510: 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20  tiple of.** nnn 
4520: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65  are atomic.  The
4530: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41   SQLITE_IOCAP_SA
4540: 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20  FE_APPEND value 
4550: 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68  means.** that wh
4560: 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 65 6e  en data is appen
4570: 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74  ded to a file, t
4580: 68 65 20 64 61 74 61 20 69 73 20 61 70 70 65 6e  he data is appen
4590: 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65  ded.** first the
45a0: 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  n the size of th
45b0: 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64  e file is extend
45c0: 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74  ed, never the ot
45d0: 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e  her.** way aroun
45e0: 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  d.  The SQLITE_I
45f0: 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20  OCAP_SEQUENTIAL 
4600: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74  property means t
4610: 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  hat.** informati
4620: 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  on is written to
4630: 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d   disk in the sam
4640: 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73  e order as calls
4650: 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e  .** to xWrite().
4660: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
4670: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20  TE_IOCAP_ATOMIC 
4680: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
4690: 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  001.#define SQLI
46a0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35  TE_IOCAP_ATOMIC5
46b0: 31 32 20 20 20 20 20 20 20 30 78 30 30 30 30 30  12       0x00000
46c0: 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  002.#define SQLI
46d0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31  TE_IOCAP_ATOMIC1
46e0: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  K        0x00000
46f0: 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  004.#define SQLI
4700: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32  TE_IOCAP_ATOMIC2
4710: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  K        0x00000
4720: 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  008.#define SQLI
4730: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34  TE_IOCAP_ATOMIC4
4740: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  K        0x00000
4750: 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  010.#define SQLI
4760: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38  TE_IOCAP_ATOMIC8
4770: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  K        0x00000
4780: 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  020.#define SQLI
4790: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31  TE_IOCAP_ATOMIC1
47a0: 36 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30  6K       0x00000
47b0: 30 34 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  040.#define SQLI
47c0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33  TE_IOCAP_ATOMIC3
47d0: 32 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30  2K       0x00000
47e0: 30 38 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  080.#define SQLI
47f0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36  TE_IOCAP_ATOMIC6
4800: 34 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30  4K       0x00000
4810: 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  100.#define SQLI
4820: 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50  TE_IOCAP_SAFE_AP
4830: 50 45 4e 44 20 20 20 20 20 30 78 30 30 30 30 30  PEND     0x00000
4840: 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  200.#define SQLI
4850: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
4860: 49 41 4c 20 20 20 20 20 20 30 78 30 30 30 30 30  IAL      0x00000
4870: 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  400../*.** CAPI3
4880: 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e  REF: File Lockin
4890: 67 20 4c 65 76 65 6c 73 0a 2a 2a 0a 2a 2a 20 53  g Levels.**.** S
48a0: 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f  QLite uses one o
48b0: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
48c0: 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 61  integer values a
48d0: 73 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  s the second.** 
48e0: 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c  argument to call
48f0: 73 20 69 74 20 6d 61 6b 65 73 20 74 6f 20 74 68  s it makes to th
4900: 65 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55  e xLock() and xU
4910: 6e 6c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 73 0a  nlock() methods.
4920: 2a 2a 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65  ** of an [sqlite
4930: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62  3_io_methods] ob
4940: 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ject..*/.#define
4950: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e   SQLITE_LOCK_NON
4960: 45 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65  E          0.#de
4970: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  fine SQLITE_LOCK
4980: 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 20 31  _SHARED        1
4990: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
49a0: 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 20 20 20  LOCK_RESERVED   
49b0: 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c     2.#define SQL
49c0: 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47  ITE_LOCK_PENDING
49d0: 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65         3.#define
49e0: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43   SQLITE_LOCK_EXC
49f0: 4c 55 53 49 56 45 20 20 20 20 20 34 0a 0a 2f 2a  LUSIVE     4../*
4a00: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 79  .** CAPI3REF: Sy
4a10: 6e 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 20 54 79  nchronization Ty
4a20: 70 65 20 46 6c 61 67 73 0a 2a 2a 0a 2a 2a 20 57  pe Flags.**.** W
4a30: 68 65 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b  hen SQLite invok
4a40: 65 73 20 74 68 65 20 78 53 79 6e 63 28 29 20 6d  es the xSync() m
4a50: 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73 71 6c  ethod of an [sql
4a60: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d  ite3_io_methods]
4a70: 0a 2a 2a 20 6f 62 6a 65 63 74 20 69 74 20 75 73  .** object it us
4a80: 65 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  es a combination
4a90: 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
4aa0: 67 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  g integer values
4ab0: 20 61 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e   as.** the secon
4ac0: 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  d argument..**.*
4ad0: 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54  * When the SQLIT
4ae0: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20  E_SYNC_DATAONLY 
4af0: 66 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69 74  flag is used, it
4b00: 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a   means that the.
4b10: 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f  ** sync operatio
4b20: 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20  n only needs to 
4b30: 66 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61  flush data to ma
4b40: 73 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f  ss storage.  Ino
4b50: 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  de.** informatio
4b60: 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c  n need not be fl
4b70: 75 73 68 65 64 2e 20 20 54 68 65 20 53 51 4c 49  ushed.  The SQLI
4b80: 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6d  TE_SYNC_NORMAL m
4b90: 65 61 6e 73 20 0a 2a 2a 20 74 6f 20 75 73 65 20  eans .** to use 
4ba0: 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 73  normal fsync() s
4bb0: 65 6d 61 6e 74 69 63 73 2e 20 20 54 68 65 20 53  emantics.  The S
4bc0: 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20  QLITE_SYNC_FULL 
4bd0: 66 6c 61 67 20 6d 65 61 6e 73 20 0a 2a 2a 20 74  flag means .** t
4be0: 6f 20 75 73 65 20 4d 61 63 20 4f 53 2d 58 20 73  o use Mac OS-X s
4bf0: 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 20 69 6e  tyle fullsync in
4c00: 73 74 65 61 64 20 6f 66 20 66 73 79 6e 63 28 29  stead of fsync()
4c10: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
4c20: 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20  ITE_SYNC_NORMAL 
4c30: 20 20 20 20 20 20 20 30 78 30 30 30 30 32 0a 23         0x00002.#
4c40: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59  define SQLITE_SY
4c50: 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 20  NC_FULL         
4c60: 20 30 78 30 30 30 30 33 0a 23 64 65 66 69 6e 65   0x00003.#define
4c70: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54   SQLITE_SYNC_DAT
4c80: 41 4f 4e 4c 59 20 20 20 20 20 20 30 78 30 30 30  AONLY      0x000
4c90: 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  10.../*.** CAPI3
4ca0: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63  REF: OS Interfac
4cb0: 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61 6e 64  e Open File Hand
4cc0: 6c 65 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 73 71 6c  le.**.** An [sql
4cd0: 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63  ite3_file] objec
4ce0: 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  t represents an 
4cf0: 6f 70 65 6e 20 66 69 6c 65 20 69 6e 20 74 68 65  open file in the
4d00: 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65   OS.** interface
4d10: 20 6c 61 79 65 72 2e 20 20 49 6e 64 69 76 69 64   layer.  Individ
4d20: 75 61 6c 20 4f 53 20 69 6e 74 65 72 66 61 63 65  ual OS interface
4d30: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
4d40: 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f   will.** want to
4d50: 20 73 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f   subclass this o
4d60: 62 6a 65 63 74 20 62 79 20 61 70 70 65 6e 64 69  bject by appendi
4d70: 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69  ng additional fi
4d80: 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69  elds.** for thei
4d90: 72 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20  r own use.  The 
4da0: 70 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69  pMethods entry i
4db0: 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
4dc0: 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f  n.** [sqlite3_io
4dd0: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74  _methods] object
4de0: 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65   that defines me
4df0: 74 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72  thods for perfor
4e00: 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72  ming.** I/O oper
4e10: 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70  ations on the op
4e20: 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65  en file..*/.type
4e30: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
4e40: 65 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f  e3_file sqlite3_
4e50: 66 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c  file;.struct sql
4e60: 69 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f  ite3_file {.  co
4e70: 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74  nst struct sqlit
4e80: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70  e3_io_methods *p
4e90: 4d 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74  Methods;  /* Met
4ea0: 68 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e  hods for an open
4eb0: 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   file */.};../*.
4ec0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20  ** CAPI3REF: OS 
4ed0: 49 6e 74 65 72 66 61 63 65 20 46 69 6c 65 20 56  Interface File V
4ee0: 69 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f  irtual Methods O
4ef0: 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 45 76 65 72  bject.**.** Ever
4f00: 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 79  y file opened by
4f10: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   the [sqlite3_vf
4f20: 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20  s] xOpen method 
4f30: 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74  contains a point
4f40: 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 69 6e 73 74  er to.** an inst
4f50: 61 6e 63 65 20 6f 66 20 74 68 65 20 74 68 69 73  ance of the this
4f60: 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 6f   object.  This o
4f70: 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68  bject defines th
4f80: 65 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 75 73 65  e.** methods use
4f90: 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72  d to perform var
4fa0: 69 6f 75 73 20 6f 70 65 72 61 74 69 6f 6e 73 20  ious operations 
4fb0: 61 67 61 69 6e 73 74 20 74 68 65 20 6f 70 65 6e  against the open
4fc0: 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   file..**.** The
4fd0: 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20   flags argument 
4fe0: 74 6f 20 78 53 79 6e 63 20 6d 61 79 20 62 65 20  to xSync may be 
4ff0: 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 53  one of [SQLITE_S
5000: 59 4e 43 5f 4e 4f 52 4d 41 4c 5d 20 6f 72 0a 2a  YNC_NORMAL] or.*
5010: 2a 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46  * [SQLITE_SYNC_F
5020: 55 4c 4c 5d 2e 20 20 54 68 65 20 66 69 72 73 74  ULL].  The first
5030: 20 63 68 6f 69 63 65 20 69 73 20 74 68 65 20 6e   choice is the n
5040: 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 2e 0a 2a  ormal fsync()..*
5050: 20 20 54 68 65 20 73 65 63 6f 6e 64 20 63 68 6f    The second cho
5060: 69 63 65 20 69 73 20 61 6e 0a 2a 2a 20 4f 53 2d  ice is an.** OS-
5070: 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63  X style fullsync
5080: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 53 59  .  The SQLITE_SY
5090: 4e 43 5f 44 41 54 41 20 66 6c 61 67 20 6d 61 79  NC_DATA flag may
50a0: 20 62 65 20 4f 52 65 64 20 69 6e 20 74 6f 0a 2a   be ORed in to.*
50b0: 2a 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  * indicate that 
50c0: 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66  only the data of
50d0: 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 6e 6f   the file and no
50e0: 74 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 64  t its inode need
50f0: 73 20 74 6f 20 62 65 0a 2a 2a 20 73 79 6e 63 65  s to be.** synce
5100: 64 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 69 6e  d..** .** The in
5110: 74 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20  teger values to 
5120: 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c  xLock() and xUnl
5130: 6f 63 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f 66  ock() are one of
5140: 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
5150: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f   [SQLITE_LOCK_NO
5160: 4e 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  NE],.** <li> [SQ
5170: 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44  LITE_LOCK_SHARED
5180: 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  ],.** <li> [SQLI
5190: 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44  TE_LOCK_RESERVED
51a0: 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  ],.** <li> [SQLI
51b0: 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d  TE_LOCK_PENDING]
51c0: 2c 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  , or.** <li> [SQ
51d0: 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53  LITE_LOCK_EXCLUS
51e0: 49 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  IVE]..** </ul>.*
51f0: 2a 20 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61  * xLock() increa
5200: 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55  ses the lock. xU
5210: 6e 6c 6f 63 6b 28 29 20 64 65 63 72 65 61 73 65  nlock() decrease
5220: 73 20 74 68 65 20 6c 6f 63 6b 2e 20 20 0a 2a 2a  s the lock.  .**
5230: 20 54 68 65 20 78 43 68 65 63 6b 52 65 73 65 72   The xCheckReser
5240: 76 65 64 4c 6f 63 6b 28 29 20 6d 65 74 68 6f 64  vedLock() method
5250: 20 6c 6f 6f 6b 73 0a 2a 2a 20 74 6f 20 73 65 65   looks.** to see
5260: 20 69 66 20 61 6e 79 20 64 61 74 61 62 61 73 65   if any database
5270: 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 65 69 74   connection, eit
5280: 68 65 72 20 69 6e 20 74 68 69 73 0a 2a 2a 20 70  her in this.** p
5290: 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 73 6f 6d  rocess or in som
52a0: 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2c  e other process,
52b0: 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e 20 52   is holding an R
52c0: 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50 45 4e 44  ESERVED,.** PEND
52d0: 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55 53 49 56  ING, or EXCLUSIV
52e0: 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69  E lock on the fi
52f0: 6c 65 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20  le.  It returns 
5300: 74 72 75 65 0a 2a 2a 20 69 66 20 73 75 63 68 20  true.** if such 
5310: 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 61 6e  a lock exists an
5320: 64 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a  d false if not..
5330: 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 69 6c 65  ** .** The xFile
5340: 43 6f 6e 74 72 6f 6c 28 29 20 6d 65 74 68 6f 64  Control() method
5350: 20 69 73 20 61 20 67 65 6e 65 72 69 63 20 69 6e   is a generic in
5360: 74 65 72 66 61 63 65 20 74 68 61 74 20 61 6c 6c  terface that all
5370: 6f 77 73 20 63 75 73 74 6f 6d 0a 2a 2a 20 56 46  ows custom.** VF
5380: 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  S implementation
5390: 73 20 74 6f 20 64 69 72 65 63 74 6c 79 20 63 6f  s to directly co
53a0: 6e 74 72 6f 6c 20 61 6e 20 6f 70 65 6e 20 66 69  ntrol an open fi
53b0: 6c 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  le using the.** 
53c0: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f  [sqlite3_file_co
53d0: 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61  ntrol()] interfa
53e0: 63 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ce.  The second 
53f0: 22 6f 70 22 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  "op" argument.**
5400: 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f   is an integer o
5410: 70 63 6f 64 65 2e 20 20 20 54 68 65 20 74 68 69  pcode.   The thi
5420: 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69  rd.** argument i
5430: 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69 6e  s a generic poin
5440: 74 65 72 20 77 68 69 63 68 20 69 73 20 69 6e 74  ter which is int
5450: 65 6e 64 65 64 20 74 6f 20 62 65 20 61 20 70 6f  ended to be a po
5460: 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 73 74  inter.** to a st
5470: 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79  ructure that may
5480: 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65 6e   contain argumen
5490: 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20 77  ts or space in w
54a0: 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74 65  hich to.** write
54b0: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e 20   return values. 
54c0: 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73 20   Potential uses 
54d0: 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  for xFileControl
54e0: 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 66  () might be.** f
54f0: 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61 62  unctions to enab
5500: 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 6b  le blocking lock
5510: 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73 2c  s with timeouts,
5520: 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a   to change the.*
5530: 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65  * locking strate
5540: 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20  gy (for example 
5550: 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 20  to use dot-file 
5560: 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 69  locks), to inqui
5570: 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 20  re.** about the 
5580: 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 6b  status of a lock
5590: 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 74  , or to break st
55a0: 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20  ale locks.  The 
55b0: 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 72  SQLite.** core r
55c0: 65 73 65 72 76 65 73 20 6f 70 63 6f 64 65 73 20  eserves opcodes 
55d0: 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20 66 6f  less than 100 fo
55e0: 72 20 69 74 73 20 6f 77 6e 20 75 73 65 2e 20 0a  r its own use. .
55f0: 2a 2a 20 41 20 5b 53 51 4c 49 54 45 5f 46 43 4e  ** A [SQLITE_FCN
5600: 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 7c 20 6c  TL_LOCKSTATE | l
5610: 69 73 74 20 6f 66 20 6f 70 63 6f 64 65 73 5d 20  ist of opcodes] 
5620: 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20 69 73  less than 100 is
5630: 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 41   available..** A
5640: 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
5650: 20 64 65 66 69 6e 65 20 61 20 63 75 73 74 6f 6d   define a custom
5660: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65   xFileControl me
5670: 74 68 6f 64 20 73 68 6f 75 6c 64 20 75 73 65 20  thod should use 
5680: 6f 70 63 6f 64 65 73 20 0a 2a 2a 20 67 72 65 61  opcodes .** grea
5690: 74 65 72 20 74 68 61 6e 20 31 30 30 20 74 6f 20  ter than 100 to 
56a0: 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63 74 73 2e  avoid conflicts.
56b0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 65 63 74  .**.** The xSect
56c0: 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 20  orSize() method 
56d0: 72 65 74 75 72 6e 73 20 74 68 65 20 73 65 63 74  returns the sect
56e0: 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a  or size of the.*
56f0: 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 75 6e  * device that un
5700: 64 65 72 6c 69 65 73 20 74 68 65 20 66 69 6c 65  derlies the file
5710: 2e 20 20 54 68 65 20 73 65 63 74 6f 72 20 73 69  .  The sector si
5720: 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 6d 69 6e  ze is the.** min
5730: 69 6d 75 6d 20 77 72 69 74 65 20 74 68 61 74 20  imum write that 
5740: 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64  can be performed
5750: 20 77 69 74 68 6f 75 74 20 64 69 73 74 75 72 62   without disturb
5760: 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 62 79 74  ing.** other byt
5770: 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20  es in the file. 
5780: 20 54 68 65 20 78 44 65 76 69 63 65 43 68 61 72   The xDeviceChar
5790: 61 63 74 65 72 69 73 74 69 63 73 28 29 0a 2a 2a  acteristics().**
57a0: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
57b0: 61 20 62 69 74 20 76 65 63 74 6f 72 20 64 65 73  a bit vector des
57c0: 63 72 69 62 69 6e 67 20 62 65 68 61 76 69 6f 72  cribing behavior
57d0: 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65  s of the.** unde
57e0: 72 6c 79 69 6e 67 20 64 65 76 69 63 65 3a 0a 2a  rlying device:.*
57f0: 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
5800: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
5810: 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c 6c 69 3e 20  ATOMIC].** <li> 
5820: 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54  [SQLITE_IOCAP_AT
5830: 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c 6c 69 3e  OMIC512].** <li>
5840: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41   [SQLITE_IOCAP_A
5850: 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c 6c 69 3e  TOMIC1K].** <li>
5860: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41   [SQLITE_IOCAP_A
5870: 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e  TOMIC2K].** <li>
5880: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41   [SQLITE_IOCAP_A
5890: 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e  TOMIC4K].** <li>
58a0: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41   [SQLITE_IOCAP_A
58b0: 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c 6c 69 3e  TOMIC8K].** <li>
58c0: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41   [SQLITE_IOCAP_A
58d0: 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20 3c 6c 69  TOMIC16K].** <li
58e0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
58f0: 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a 20 3c 6c  ATOMIC32K].** <l
5900: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
5910: 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a 2a 20 3c  _ATOMIC64K].** <
5920: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
5930: 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 5d 0a 2a  P_SAFE_APPEND].*
5940: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
5950: 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 5d  OCAP_SEQUENTIAL]
5960: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
5970: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
5980: 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79  _ATOMIC property
5990: 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20   means that all 
59a0: 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79  writes of.** any
59b0: 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63   size are atomic
59c0: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f  .  The SQLITE_IO
59d0: 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61  CAP_ATOMICnnn va
59e0: 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61  lues.** mean tha
59f0: 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63  t writes of bloc
5a00: 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20  ks that are nnn 
5a10: 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e  bytes in size an
5a20: 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 64  d.** are aligned
5a30: 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 77   to an address w
5a40: 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 67  hich is an integ
5a50: 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a  er multiple of.*
5a60: 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63  * nnn are atomic
5a70: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f  .  The SQLITE_IO
5a80: 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20  CAP_SAFE_APPEND 
5a90: 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74  value means.** t
5aa0: 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 73  hat when data is
5ab0: 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 66   appended to a f
5ac0: 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 73  ile, the data is
5ad0: 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72   appended.** fir
5ae0: 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65  st then the size
5af0: 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   of the file is 
5b00: 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 20  extended, never 
5b10: 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79  the other.** way
5b20: 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 51   around.  The SQ
5b30: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45  LITE_IOCAP_SEQUE
5b40: 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 6d  NTIAL property m
5b50: 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66  eans that.** inf
5b60: 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 74  ormation is writ
5b70: 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 74  ten to disk in t
5b80: 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73  he same order as
5b90: 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72   calls.** to xWr
5ba0: 69 74 65 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65  ite()..*/.typede
5bb0: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
5bc0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69  _io_methods sqli
5bd0: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a  te3_io_methods;.
5be0: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
5bf0: 6f 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e  o_methods {.  in
5c00: 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e  t iVersion;.  in
5c10: 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69  t (*xClose)(sqli
5c20: 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e  te3_file*);.  in
5c30: 74 20 28 2a 78 52 65 61 64 29 28 73 71 6c 69 74  t (*xRead)(sqlit
5c40: 65 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c  e3_file*, void*,
5c50: 20 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74   int iAmt, sqlit
5c60: 65 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b  e3_int64 iOfst);
5c70: 0a 20 20 69 6e 74 20 28 2a 78 57 72 69 74 65 29  .  int (*xWrite)
5c80: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
5c90: 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
5ca0: 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69   iAmt, sqlite3_i
5cb0: 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69  nt64 iOfst);.  i
5cc0: 6e 74 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28  nt (*xTruncate)(
5cd0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73  sqlite3_file*, s
5ce0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 69 7a  qlite3_int64 siz
5cf0: 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e  e);.  int (*xSyn
5d00: 63 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  c)(sqlite3_file*
5d10: 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20  , int flags);.  
5d20: 69 6e 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 29  int (*xFileSize)
5d30: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
5d40: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
5d50: 53 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78  Size);.  int (*x
5d60: 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69  Lock)(sqlite3_fi
5d70: 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74  le*, int);.  int
5d80: 20 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69   (*xUnlock)(sqli
5d90: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b  te3_file*, int);
5da0: 0a 20 20 69 6e 74 20 28 2a 78 43 68 65 63 6b 52  .  int (*xCheckR
5db0: 65 73 65 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c  eservedLock)(sql
5dc0: 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69  ite3_file*);.  i
5dd0: 6e 74 20 28 2a 78 46 69 6c 65 43 6f 6e 74 72 6f  nt (*xFileContro
5de0: 6c 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  l)(sqlite3_file*
5df0: 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
5e00: 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78  pArg);.  int (*x
5e10: 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c 69  SectorSize)(sqli
5e20: 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e  te3_file*);.  in
5e30: 74 20 28 2a 78 44 65 76 69 63 65 43 68 61 72 61  t (*xDeviceChara
5e40: 63 74 65 72 69 73 74 69 63 73 29 28 73 71 6c 69  cteristics)(sqli
5e50: 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a  te3_file*);.  /*
5e60: 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 74 68   Additional meth
5e70: 6f 64 73 20 6d 61 79 20 62 65 20 61 64 64 65 64  ods may be added
5e80: 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
5e90: 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ses */.};../*.**
5ea0: 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 6e 64   CAPI3REF: Stand
5eb0: 61 72 64 20 46 69 6c 65 20 43 6f 6e 74 72 6f 6c  ard File Control
5ec0: 20 4f 70 63 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 54   Opcodes.**.** T
5ed0: 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
5ee0: 73 74 61 6e 74 73 20 61 72 65 20 6f 70 63 6f 64  stants are opcod
5ef0: 65 73 20 66 6f 72 20 74 68 65 20 78 46 69 6c 65  es for the xFile
5f00: 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0a 2a  Control method.*
5f10: 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  * of the [sqlite
5f20: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62  3_io_methods] ob
5f30: 6a 65 63 74 20 61 6e 64 20 74 6f 20 74 68 65 20  ject and to the 
5f40: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f  [sqlite3_file_co
5f50: 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65  ntrol()].** inte
5f60: 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rface..**.** The
5f70: 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c   [SQLITE_FCNTL_L
5f80: 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63 6f 64 65  OCKSTATE] opcode
5f90: 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62   is used for deb
5fa0: 75 67 67 69 6e 67 2e 20 20 54 68 69 73 0a 2a 2a  ugging.  This.**
5fb0: 20 6f 70 63 6f 64 65 20 63 61 73 65 73 20 74 68   opcode cases th
5fc0: 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d  e xFileControl m
5fd0: 65 74 68 6f 64 20 74 6f 20 77 72 69 74 65 20 74  ethod to write t
5fe0: 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
5ff0: 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20   of.** the lock 
6000: 28 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f  (one of [SQLITE_
6010: 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c  LOCK_NONE], [SQL
6020: 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d  ITE_LOCK_SHARED]
6030: 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  ,.** [SQLITE_LOC
6040: 4b 5f 52 45 53 45 52 56 45 44 5d 2c 20 5b 53 51  K_RESERVED], [SQ
6050: 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e  LITE_LOCK_PENDIN
6060: 47 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c  G], or [SQLITE_L
6070: 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a  OCK_EXCLUSIVE]).
6080: 2a 2a 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67  ** into an integ
6090: 65 72 20 74 68 61 74 20 74 68 65 20 70 41 72 67  er that the pArg
60a0: 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73   argument points
60b0: 20 74 6f 2e 20 20 54 68 69 73 20 63 61 70 61 62   to.  This capab
60c0: 69 6c 69 74 79 0a 2a 2a 20 69 73 20 75 73 65 64  ility.** is used
60d0: 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 20   during testing 
60e0: 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74  and only needs t
60f0: 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 20 77  o be supported w
6100: 68 65 6e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  hen SQLITE_TEST.
6110: 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  ** is defined..*
6120: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
6130: 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
6140: 20 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a          1../*.**
6150: 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
6160: 20 48 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 54 68   Handle.**.** Th
6170: 65 20 6d 75 74 65 78 20 6d 6f 64 75 6c 65 20 77  e mutex module w
6180: 69 74 68 69 6e 20 53 51 4c 69 74 65 20 64 65 66  ithin SQLite def
6190: 69 6e 65 73 20 5b 73 71 6c 69 74 65 33 5f 6d 75  ines [sqlite3_mu
61a0: 74 65 78 5d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a  tex] to be an.**
61b0: 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66   abstract type f
61c0: 6f 72 20 61 20 6d 75 74 65 78 20 6f 62 6a 65 63  or a mutex objec
61d0: 74 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63  t.  The SQLite c
61e0: 6f 72 65 20 6e 65 76 65 72 20 6c 6f 6f 6b 73 0a  ore never looks.
61f0: 2a 2a 20 61 74 20 74 68 65 20 69 6e 74 65 72 6e  ** at the intern
6200: 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  al representatio
6210: 6e 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33  n of an [sqlite3
6220: 5f 6d 75 74 65 78 5d 2e 20 20 49 74 20 6f 6e 6c  _mutex].  It onl
6230: 79 0a 2a 2a 20 64 65 61 6c 73 20 77 69 74 68 20  y.** deals with 
6240: 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  pointers to the 
6250: 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20  [sqlite3_mutex] 
6260: 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75  object..**.** Mu
6270: 74 65 78 65 73 20 61 72 65 20 63 72 65 61 74 65  texes are create
6280: 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
6290: 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 2e  _mutex_alloc()].
62a0: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
62b0: 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ct sqlite3_mutex
62c0: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 3b 0a   sqlite3_mutex;.
62d0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
62e0: 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 4f 62   OS Interface Ob
62f0: 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  ject.**.** An in
6300: 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
6310: 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68  bject defines th
6320: 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 77  e interface betw
6330: 65 65 6e 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74  een the.** SQLit
6340: 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20 75  e core and the u
6350: 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
6360: 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68 65  ing system.  The
6370: 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68 65   "vfs".** in the
6380: 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62 6a   name of the obj
6390: 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20 22  ect stands for "
63a0: 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73  virtual file sys
63b0: 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tem"..**.** The 
63c0: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69  iVersion field i
63d0: 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75  s initially 1 bu
63e0: 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20  t may be larger 
63f0: 66 6f 72 20 66 75 74 75 72 65 0a 2a 2a 20 76 65  for future.** ve
6400: 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
6410: 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 69  .  Additional fi
6420: 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65  elds may be appe
6430: 6e 64 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20  nded to this.** 
6440: 6f 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20  object when the 
6450: 69 56 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 69  iVersion value i
6460: 73 20 69 6e 63 72 65 61 73 65 64 2e 0a 2a 2a 0a  s increased..**.
6470: 2a 2a 20 54 68 65 20 73 7a 4f 73 46 69 6c 65 20  ** The szOsFile 
6480: 66 69 65 6c 64 20 69 73 20 74 68 65 20 73 69 7a  field is the siz
6490: 65 20 6f 66 20 74 68 65 20 73 75 62 63 6c 61 73  e of the subclas
64a0: 73 65 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  sed [sqlite3_fil
64b0: 65 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  e].** structure 
64c0: 75 73 65 64 20 62 79 20 74 68 69 73 20 56 46 53  used by this VFS
64d0: 2e 20 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73  .  mxPathname is
64e0: 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e   the maximum len
64f0: 67 74 68 20 6f 66 0a 2a 2a 20 61 20 70 61 74 68  gth of.** a path
6500: 6e 61 6d 65 20 69 6e 20 74 68 69 73 20 56 46 53  name in this VFS
6510: 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ..**.** Register
6520: 65 64 20 76 66 73 20 6d 6f 64 75 6c 65 73 20 61  ed vfs modules a
6530: 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e  re kept on a lin
6540: 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20  ked list formed 
6550: 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20  by.** the pNext 
6560: 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73  pointer.  The [s
6570: 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
6580: 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73  ter()].** and [s
6590: 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67  qlite3_vfs_unreg
65a0: 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61  ister()] interfa
65b0: 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20  ces manage this 
65c0: 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72  list.** in a thr
65d0: 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54  ead-safe way.  T
65e0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f  he [sqlite3_vfs_
65f0: 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63  find()] interfac
6600: 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68  e.** searches th
6610: 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  e list..**.** Th
6620: 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 69 73  e pNext field is
6630: 20 74 68 65 20 6f 6e 6c 79 20 66 69 65 6c 64 73   the only fields
6640: 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f   in the sqlite3_
6650: 76 66 73 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  vfs .** structur
6660: 65 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69  e that SQLite wi
6670: 6c 6c 20 65 76 65 72 20 6d 6f 64 69 66 79 2e 20  ll ever modify. 
6680: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c   SQLite will onl
6690: 79 20 61 63 63 65 73 73 0a 2a 2a 20 6f 72 20 6d  y access.** or m
66a0: 6f 64 69 66 79 20 74 68 69 73 20 66 69 65 6c 64  odify this field
66b0: 20 77 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 61   while holding a
66c0: 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 61 74   particular stat
66d0: 69 63 20 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65  ic mutex..** The
66e0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f   application sho
66f0: 75 6c 64 20 6e 65 76 65 72 20 6d 6f 64 69 66 79  uld never modify
6700: 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68 69 6e   anything within
6710: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73   the sqlite3_vfs
6720: 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e 63 65 20  .** object once 
6730: 74 68 65 20 6f 62 6a 65 63 74 20 68 61 73 20 62  the object has b
6740: 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 2e 0a  een registered..
6750: 2a 2a 0a 2a 2a 20 54 68 65 20 7a 4e 61 6d 65 20  **.** The zName 
6760: 66 69 65 6c 64 20 68 6f 6c 64 73 20 74 68 65 20  field holds the 
6770: 6e 61 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20  name of the VFS 
6780: 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 6e 61 6d  module.  The nam
6790: 65 20 6d 75 73 74 0a 2a 2a 20 62 65 20 75 6e 69  e must.** be uni
67a0: 71 75 65 20 61 63 72 6f 73 73 20 61 6c 6c 20 56  que across all V
67b0: 46 53 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a  FS modules..**.*
67c0: 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 67 75  * SQLite will gu
67d0: 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65  arantee that the
67e0: 20 7a 46 69 6c 65 6e 61 6d 65 20 73 74 72 69 6e   zFilename strin
67f0: 67 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78  g passed to.** x
6800: 4f 70 65 6e 28 29 20 69 73 20 61 20 66 75 6c 6c  Open() is a full
6810: 20 70 61 74 68 6e 61 6d 65 20 61 73 20 67 65 6e   pathname as gen
6820: 65 72 61 74 65 64 20 62 79 20 78 46 75 6c 6c 50  erated by xFullP
6830: 61 74 68 6e 61 6d 65 28 29 20 61 6e 64 0a 2a 2a  athname() and.**
6840: 20 74 68 61 74 20 74 68 65 20 73 74 72 69 6e 67   that the string
6850: 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 61   will be valid a
6860: 6e 64 20 75 6e 63 68 61 6e 67 65 64 20 75 6e 74  nd unchanged unt
6870: 69 6c 20 78 43 6c 6f 73 65 28 29 20 69 73 0a 2a  il xClose() is.*
6880: 2a 20 63 61 6c 6c 65 64 2e 20 20 53 6f 20 74 68  * called.  So th
6890: 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d  e [sqlite3_file]
68a0: 20 63 61 6e 20 73 74 6f 72 65 20 61 20 70 6f 69   can store a poi
68b0: 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 66  nter to the.** f
68c0: 69 6c 65 6e 61 6d 65 20 69 66 20 69 74 20 6e 65  ilename if it ne
68d0: 65 64 73 20 74 6f 20 72 65 6d 65 6d 62 65 72 20  eds to remember 
68e0: 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 66 6f 72  the filename for
68f0: 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a 2a 2a   some reason..**
6900: 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72  .** The flags ar
6910: 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28  gument to xOpen(
6920: 29 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  ) is a copy of t
6930: 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e  he flags argumen
6940: 74 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33  t.** to [sqlite3
6950: 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 20 49 66  _open_v2()].  If
6960: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
6970: 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ] or [sqlite3_op
6980: 65 6e 31 36 28 29 5d 0a 2a 2a 20 69 73 20 75 73  en16()].** is us
6990: 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73 20 69  ed, then flags i
69a0: 73 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  s [SQLITE_OPEN_R
69b0: 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c  EADWRITE] | [SQL
69c0: 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d  ITE_OPEN_CREATE]
69d0: 2e 0a 2a 2a 20 49 66 20 78 4f 70 65 6e 28 29 20  ..** If xOpen() 
69e0: 6f 70 65 6e 73 20 61 20 66 69 6c 65 20 72 65 61  opens a file rea
69f0: 64 2d 6f 6e 6c 79 20 74 68 65 6e 20 69 74 20 73  d-only then it s
6a00: 65 74 73 20 2a 70 4f 75 74 46 6c 61 67 73 20 74  ets *pOutFlags t
6a10: 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 5b 53 51  o.** include [SQ
6a20: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
6a30: 4c 59 5d 2e 20 20 4f 74 68 65 72 20 62 69 74 73  LY].  Other bits
6a40: 20 69 6e 20 2a 70 4f 75 74 46 6c 61 67 73 20 6d   in *pOutFlags m
6a50: 61 79 20 62 65 0a 2a 2a 20 73 65 74 2e 0a 2a 2a  ay be.** set..**
6a60: 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c   .** SQLite will
6a70: 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66   also add one of
6a80: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
6a90: 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65  lags to the xOpe
6aa0: 6e 28 29 0a 2a 2a 20 63 61 6c 6c 2c 20 64 65 70  n().** call, dep
6ab0: 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 62  ending on the ob
6ac0: 6a 65 63 74 20 62 65 69 6e 67 20 6f 70 65 6e 65  ject being opene
6ad0: 64 3a 0a 2a 2a 20 0a 2a 2a 20 3c 75 6c 3e 0a 2a  d:.** .** <ul>.*
6ae0: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f  * <li>  [SQLITE_
6af0: 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 5d 0a 2a 2a  OPEN_MAIN_DB].**
6b00: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f   <li>  [SQLITE_O
6b10: 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
6b20: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49  ].** <li>  [SQLI
6b30: 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 5d  TE_OPEN_TEMP_DB]
6b40: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
6b50: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52  E_OPEN_TEMP_JOUR
6b60: 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  NAL].** <li>  [S
6b70: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53  QLITE_OPEN_TRANS
6b80: 49 45 4e 54 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e  IENT_DB].** <li>
6b90: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53    [SQLITE_OPEN_S
6ba0: 55 42 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c  UBJOURNAL].** <l
6bb0: 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  i>  [SQLITE_OPEN
6bc0: 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 5d  _MASTER_JOURNAL]
6bd0: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
6be0: 54 68 65 20 66 69 6c 65 20 49 2f 4f 20 69 6d 70  The file I/O imp
6bf0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20  lementation can 
6c00: 75 73 65 20 74 68 65 20 6f 62 6a 65 63 74 20 74  use the object t
6c10: 79 70 65 20 66 6c 61 67 73 20 74 6f 0a 2a 2a 20  ype flags to.** 
6c20: 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 20  changes the way 
6c30: 69 74 20 64 65 61 6c 73 20 77 69 74 68 20 66 69  it deals with fi
6c40: 6c 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  les.  For exampl
6c50: 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  e, an applicatio
6c60: 6e 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e  n.** that does n
6c70: 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 63 72  ot care about cr
6c80: 61 73 68 20 72 65 63 6f 76 65 72 79 20 6f 72 20  ash recovery or 
6c90: 72 6f 6c 6c 62 61 63 6b 2c 20 6d 69 67 68 74 20  rollback, might 
6ca0: 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f 70 65 6e  make.** the open
6cb0: 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   of a journal fi
6cc0: 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 57 72 69  le a no-op.  Wri
6cd0: 74 65 73 20 74 6f 20 74 68 69 73 20 6a 6f 75 72  tes to this jour
6ce0: 6e 61 6c 20 61 72 65 0a 2a 2a 20 61 6c 73 6f 20  nal are.** also 
6cf0: 61 20 6e 6f 2d 6f 70 2e 20 20 41 6e 79 20 61 74  a no-op.  Any at
6d00: 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68  tempt to read th
6d10: 65 20 6a 6f 75 72 6e 61 6c 20 72 65 74 75 72 6e  e journal return
6d20: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2e 0a 2a   SQLITE_IOERR..*
6d30: 2a 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65  * Or the impleme
6d40: 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 65  ntation might re
6d50: 63 6f 67 6e 69 7a 65 20 74 68 65 20 61 20 64 61  cognize the a da
6d60: 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c  tabase file will
6d70: 0a 2a 2a 20 62 65 20 64 6f 69 6e 67 20 70 61 67  .** be doing pag
6d80: 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 6f 72  e-aligned sector
6d90: 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65   reads and write
6da0: 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 20 6f 72  s in a random or
6db0: 64 65 72 0a 2a 2a 20 61 6e 64 20 73 65 74 20 75  der.** and set u
6dc0: 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73 79 73  p its I/O subsys
6dd0: 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e  tem accordingly.
6de0: 0a 2a 2a 20 0a 2a 2a 20 53 51 4c 69 74 65 20 6d  .** .** SQLite m
6df0: 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f 6e  ight also add on
6e00: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
6e10: 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 20  ng flags to the 
6e20: 78 4f 70 65 6e 0a 2a 2a 20 6d 65 74 68 6f 64 3a  xOpen.** method:
6e30: 0a 2a 2a 20 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  .** .** <ul>.** 
6e40: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45  <li> [SQLITE_OPE
6e50: 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d  N_DELETEONCLOSE]
6e60: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
6e70: 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d  _OPEN_EXCLUSIVE]
6e80: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 0a 2a 2a  .** </ul>.** .**
6e90: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45   The [SQLITE_OPE
6ea0: 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d  N_DELETEONCLOSE]
6eb0: 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20   flag means the 
6ec0: 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a  file should be.*
6ed0: 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 69  * deleted when i
6ee0: 74 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 54 68  t is closed.  Th
6ef0: 69 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62  is will always b
6f00: 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50 20 0a  e set for TEMP .
6f10: 2a 2a 20 64 61 74 61 62 61 73 65 73 20 61 6e 64  ** databases and
6f20: 20 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f   journals and fo
6f30: 72 20 73 75 62 6a 6f 75 72 6e 61 6c 73 2e 20 20  r subjournals.  
6f40: 54 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  The .** [SQLITE_
6f50: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20  OPEN_EXCLUSIVE] 
6f60: 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66  flag means the f
6f70: 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 6f 70  ile should be op
6f80: 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 65 78 63 6c  ened.** for excl
6f90: 75 73 69 76 65 20 61 63 63 65 73 73 2e 20 20 54  usive access.  T
6fa0: 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 20  his flag is set 
6fb0: 66 6f 72 20 61 6c 6c 20 66 69 6c 65 73 20 65 78  for all files ex
6fc0: 63 65 70 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20  cept.** for the 
6fd0: 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
6fe0: 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61 63 65  le..** .** Space
6ff0: 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 20 5b 73   to hold the  [s
7000: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 73 74 72  qlite3_file] str
7010: 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73  ucture passed as
7020: 20 74 68 65 20 74 68 69 72 64 20 0a 2a 2a 20 61   the third .** a
7030: 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e  rgument to xOpen
7040: 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79   is allocated by
7050: 20 63 61 6c 6c 65 72 20 28 74 68 65 20 53 51 4c   caller (the SQL
7060: 69 74 65 20 63 6f 72 65 29 2e 20 0a 2a 2a 20 73  ite core). .** s
7070: 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 20 61 72  zOsFile bytes ar
7080: 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  e allocated for 
7090: 74 68 69 73 20 6f 62 6a 65 63 74 2e 20 20 54 68  this object.  Th
70a0: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 0a 2a  e xOpen method.*
70b0: 2a 20 66 69 6c 6c 73 20 69 6e 20 74 68 65 20 61  * fills in the a
70c0: 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a  llocated space..
70d0: 2a 2a 20 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  ** .** The flags
70e0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 41 63   argument to xAc
70f0: 63 65 73 73 28 29 20 6d 61 79 20 62 65 20 5b 53  cess() may be [S
7100: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
7110: 53 54 53 5d 20 0a 2a 2a 20 74 6f 20 74 65 73 74  STS] .** to test
7120: 20 66 6f 72 20 74 68 65 20 65 78 69 73 74 61 6e   for the existan
7130: 63 65 20 6f 66 20 61 20 66 69 6c 65 2c 0a 2a 2a  ce of a file,.**
7140: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45   or [SQLITE_ACCE
7150: 53 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74 6f  SS_READWRITE] to
7160: 20 74 65 73 74 20 74 6f 20 73 65 65 0a 2a 2a 20   test to see.** 
7170: 69 66 20 61 20 66 69 6c 65 20 69 73 20 72 65 61  if a file is rea
7180: 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62  dable and writab
7190: 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41  le, or [SQLITE_A
71a0: 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74  CCESS_READ].** t
71b0: 6f 20 74 65 73 74 20 74 6f 20 73 65 65 20 69 66  o test to see if
71c0: 20 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c 65   a file is at le
71d0: 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20 20 54  ast readable.  T
71e0: 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 61  he file can be a
71f0: 20 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a   .** directory..
7200: 2a 2a 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  ** .** SQLite wi
7210: 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61  ll always alloca
7220: 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61  te at least mxPa
7230: 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 20 66 6f  thname+1 byte fo
7240: 72 0a 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 20  r.** the output 
7250: 62 75 66 66 65 72 73 20 66 6f 72 20 78 47 65 74  buffers for xGet
7260: 54 65 6d 70 4e 61 6d 65 20 61 6e 64 20 78 46 75  TempName and xFu
7270: 6c 6c 50 61 74 68 6e 61 6d 65 2e 0a 2a 2a 20 0a  llPathname..** .
7280: 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65  ** The xRandomne
7290: 73 73 28 29 2c 20 78 53 6c 65 65 70 28 29 2c 20  ss(), xSleep(), 
72a0: 61 6e 64 20 78 43 75 72 72 65 6e 74 54 69 6d 65  and xCurrentTime
72b0: 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  () interfaces.**
72c0: 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c   are not strictl
72d0: 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20  y a part of the 
72e0: 66 69 6c 65 73 79 73 74 65 6d 2c 20 62 75 74 20  filesystem, but 
72f0: 74 68 65 79 20 61 72 65 0a 2a 2a 20 69 6e 63 6c  they are.** incl
7300: 75 64 65 64 20 69 6e 20 74 68 65 20 56 46 53 20  uded in the VFS 
7310: 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 63 6f  structure for co
7320: 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a 2a 20 54  mpleteness..** T
7330: 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 28 29  he xRandomness()
7340: 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70   function attemp
7350: 74 73 20 74 6f 20 72 65 74 75 72 6e 20 6e 42 79  ts to return nBy
7360: 74 65 73 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20  tes bytes.** of 
7370: 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20 72 61 6e  good-quality ran
7380: 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 4f 75  domness into zOu
7390: 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  t.  The return v
73a0: 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 61  alue is.** the a
73b0: 63 74 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  ctual number of 
73c0: 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e  bytes of randomn
73d0: 65 73 73 20 6f 62 74 61 69 6e 65 64 2e 20 20 54  ess obtained.  T
73e0: 68 65 0a 2a 2a 20 78 53 6c 65 65 70 28 29 20 6d  he.** xSleep() m
73f0: 65 74 68 6f 64 20 63 61 75 73 65 20 74 68 65 20  ethod cause the 
7400: 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 74  calling thread t
7410: 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0a 2a  o sleep for at.*
7420: 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d 62  * least the numb
7430: 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e  er of microsecon
7440: 64 73 20 67 69 76 65 6e 2e 20 20 54 68 65 20 78  ds given.  The x
7450: 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0a 2a 2a  CurrentTime().**
7460: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
7470: 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d  a Julian Day Num
7480: 62 65 72 20 66 6f 72 20 74 68 65 20 63 75 72 72  ber for the curr
7490: 65 6e 74 20 64 61 74 65 20 61 6e 64 0a 2a 2a 20  ent date and.** 
74a0: 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  time..*/.typedef
74b0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
74c0: 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 3b  vfs sqlite3_vfs;
74d0: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
74e0: 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72  vfs {.  int iVer
74f0: 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  sion;           
7500: 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65   /* Structure ve
7510: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  rsion number */.
7520: 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b 20    int szOsFile; 
7530: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
7540: 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 64  ze of subclassed
7550: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2f   sqlite3_file */
7560: 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 6d  .  int mxPathnam
7570: 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  e;          /* M
7580: 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 68  aximum file path
7590: 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  name length */. 
75a0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e   sqlite3_vfs *pN
75b0: 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
75c0: 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53  t registered VFS
75d0: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
75e0: 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f   *zName;       /
75f0: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 76  * Name of this v
7600: 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74  irtual file syst
7610: 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  em */.  void *pA
7620: 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  ppData;         
7630: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
7640: 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 69  pplication-speci
7650: 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  fic data */.  in
7660: 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74  t (*xOpen)(sqlit
7670: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
7680: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c 69  har *zName, sqli
7690: 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 20  te3_file*,.     
76a0: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c            int fl
76b0: 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 6c  ags, int *pOutFl
76c0: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  ags);.  int (*xD
76d0: 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f 76  elete)(sqlite3_v
76e0: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
76f0: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e 63  *zName, int sync
7700: 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 41  Dir);.  int (*xA
7710: 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f 76  ccess)(sqlite3_v
7720: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
7730: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67  *zName, int flag
7740: 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 74  s);.  int (*xGet
7750: 54 65 6d 70 4e 61 6d 65 29 28 73 71 6c 69 74 65  TempName)(sqlite
7760: 33 5f 76 66 73 2a 2c 20 63 68 61 72 20 2a 7a 4f  3_vfs*, char *zO
7770: 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75  ut);.  int (*xFu
7780: 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73 71 6c 69  llPathname)(sqli
7790: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20  te3_vfs*, const 
77a0: 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 68 61  char *zName, cha
77b0: 72 20 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64  r *zOut);.  void
77c0: 20 2a 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c   *(*xDlOpen)(sql
77d0: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74  ite3_vfs*, const
77e0: 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
77f0: 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45  );.  void (*xDlE
7800: 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66  rror)(sqlite3_vf
7810: 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63  s*, int nByte, c
7820: 68 61 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20  har *zErrMsg);. 
7830: 20 76 6f 69 64 20 2a 28 2a 78 44 6c 53 79 6d 29   void *(*xDlSym)
7840: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f  (sqlite3_vfs*,vo
7850: 69 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  id*, const char 
7860: 2a 7a 53 79 6d 62 6f 6c 29 3b 0a 20 20 76 6f 69  *zSymbol);.  voi
7870: 64 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73 71  d (*xDlClose)(sq
7880: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69 64  lite3_vfs*, void
7890: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61 6e  *);.  int (*xRan
78a0: 64 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65 33  domness)(sqlite3
78b0: 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65  _vfs*, int nByte
78c0: 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20  , char *zOut);. 
78d0: 20 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28 73   int (*xSleep)(s
78e0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74  qlite3_vfs*, int
78f0: 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b 0a   microseconds);.
7900: 20 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e 74    int (*xCurrent
7910: 54 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 66  Time)(sqlite3_vf
7920: 73 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20 20  s*, double*);.  
7930: 2f 2a 20 4e 65 77 20 66 69 65 6c 64 73 20 6d 61  /* New fields ma
7940: 79 20 62 65 20 61 70 70 65 6e 64 65 64 20 69 6e  y be appended in
7950: 20 66 69 67 75 72 65 20 76 65 72 73 69 6f 6e 73   figure versions
7960: 2e 20 20 54 68 65 20 69 56 65 72 73 69 6f 6e 0a  .  The iVersion.
7970: 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20    ** value will 
7980: 69 6e 63 72 65 6d 65 6e 74 20 77 68 65 6e 65 76  increment whenev
7990: 65 72 20 74 68 69 73 20 68 61 70 70 65 6e 73 2e  er this happens.
79a0: 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.};../*.** CA
79b0: 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20 66 6f  PI3REF: Flags fo
79c0: 72 20 74 68 65 20 78 41 63 63 65 73 73 20 56 46  r the xAccess VF
79d0: 53 20 6d 65 74 68 6f 64 0a 2a 2a 0a 2a 2a 20 54  S method.**.** T
79e0: 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
79f0: 73 74 61 6e 74 73 20 63 61 6e 20 62 65 20 75 73  stants can be us
7a00: 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64 20  ed as the third 
7a10: 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20  parameter to.** 
7a20: 74 68 65 20 78 41 63 63 65 73 73 20 6d 65 74 68  the xAccess meth
7a30: 6f 64 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65  od of an [sqlite
7a40: 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 20 20  3_vfs] object.  
7a50: 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a  They determine.*
7a60: 2a 20 74 68 65 20 6b 69 6e 64 20 6f 66 20 77 68  * the kind of wh
7a70: 61 74 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69  at kind of permi
7a80: 73 73 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65  ssions the xAcce
7a90: 73 73 20 6d 65 74 68 6f 64 20 69 73 0a 2a 2a 20  ss method is.** 
7aa0: 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 20 20 57 69  looking for.  Wi
7ab0: 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  th SQLITE_ACCESS
7ac0: 5f 45 58 49 53 54 53 2c 20 74 68 65 20 78 41 63  _EXISTS, the xAc
7ad0: 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 73  cess method.** s
7ae0: 69 6d 70 6c 79 20 63 68 65 63 6b 73 20 74 6f 20  imply checks to 
7af0: 73 65 65 20 69 66 20 74 68 65 20 66 69 6c 65 20  see if the file 
7b00: 65 78 69 73 74 73 2e 20 20 57 69 74 68 20 53 51  exists.  With SQ
7b10: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
7b20: 57 52 49 54 45 2c 0a 2a 2a 20 74 68 65 20 78 41  WRITE,.** the xA
7b30: 63 63 65 73 73 20 6d 65 74 68 6f 64 20 63 68 65  ccess method che
7b40: 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74 68  cks to see if th
7b50: 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68 20 72  e file is both r
7b60: 65 61 64 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 77  eadable.** and w
7b70: 72 69 74 61 62 6c 65 2e 20 20 57 69 74 68 20 53  ritable.  With S
7b80: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
7b90: 44 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65  D the xAccess me
7ba0: 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 20 74  thod.** checks t
7bb0: 6f 20 73 65 65 20 69 66 20 74 68 65 20 66 69 6c  o see if the fil
7bc0: 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a  e is readable..*
7bd0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7be0: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 20  _ACCESS_EXISTS  
7bf0: 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    0.#define SQLI
7c00: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
7c10: 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51  ITE 1.#define SQ
7c20: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
7c30: 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43        2../*.** C
7c40: 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20  API3REF: Enable 
7c50: 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e  Or Disable Exten
7c60: 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73  ded Result Codes
7c70: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
7c80: 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64  ine enables or d
7c90: 69 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a 20 5b  isables the.** [
7ca0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
7cb0: 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73  D | extended res
7cc0: 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75  ult codes] featu
7cd0: 72 65 2e 0a 2a 2a 20 42 79 20 64 65 66 61 75 6c  re..** By defaul
7ce0: 74 2c 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f  t, SQLite API ro
7cf0: 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e  utines return on
7d00: 65 20 6f 66 20 6f 6e 6c 79 20 32 36 20 69 6e 74  e of only 26 int
7d10: 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  eger.** [SQLITE_
7d20: 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65  OK | result code
7d30: 73 5d 2e 20 20 57 68 65 6e 20 65 78 74 65 6e 64  s].  When extend
7d40: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 0a  ed result codes.
7d50: 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62  ** are enabled b
7d60: 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  y this routine, 
7d70: 74 68 65 20 72 65 70 65 74 6f 69 72 65 20 6f 66  the repetoire of
7d80: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 63 61   result codes ca
7d90: 6e 20 62 65 0a 2a 2a 20 6d 75 63 68 20 6c 61 72  n be.** much lar
7da0: 67 65 72 20 61 6e 64 20 63 61 6e 20 28 68 6f 70  ger and can (hop
7db0: 65 66 75 6c 6c 79 29 20 70 72 6f 76 69 64 65 20  efully) provide 
7dc0: 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e  more detailed in
7dd0: 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f  formation.** abo
7de0: 75 74 20 74 68 65 20 63 61 75 73 65 20 6f 66 20  ut the cause of 
7df0: 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  an error..**.** 
7e00: 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
7e10: 65 6e 74 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e  ent is a boolean
7e20: 20 76 61 6c 75 65 20 74 68 61 74 20 74 75 72 6e   value that turn
7e30: 73 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  s extended resul
7e40: 74 0a 2a 2a 20 63 6f 64 65 73 20 6f 6e 20 61 6e  t.** codes on an
7e50: 64 20 6f 66 66 2e 20 20 45 78 74 65 6e 64 65 64  d off.  Extended
7e60: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72   result codes ar
7e70: 65 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74  e off by default
7e80: 20 66 6f 72 0a 2a 2a 20 62 61 63 6b 77 61 72 64   for.** backward
7e90: 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  s compatibility 
7ea0: 77 69 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69  with older versi
7eb0: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ons of SQLite..*
7ec0: 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
7ed0: 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
7ee0: 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 73  d_result_codes(s
7ef0: 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 6e 6f  qlite3*, int ono
7f00: 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ff);../*.** CAPI
7f10: 33 52 45 46 3a 20 4c 61 73 74 20 49 6e 73 65 72  3REF: Last Inser
7f20: 74 20 52 6f 77 69 64 0a 2a 2a 0a 2a 2a 20 45 61  t Rowid.**.** Ea
7f30: 63 68 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53  ch entry in an S
7f40: 51 4c 69 74 65 20 74 61 62 6c 65 20 68 61 73 20  QLite table has 
7f50: 61 20 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20  a unique 64-bit 
7f60: 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b  signed integer k
7f70: 65 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 74 68 65  ey.** called the
7f80: 20 22 72 6f 77 69 64 22 2e 20 54 68 65 20 72 6f   "rowid". The ro
7f90: 77 69 64 20 69 73 20 61 6c 77 61 79 73 20 61 76  wid is always av
7fa0: 61 69 6c 61 62 6c 65 20 61 73 20 61 6e 20 75 6e  ailable as an un
7fb0: 64 65 63 6c 61 72 65 64 0a 2a 2a 20 63 6f 6c 75  declared.** colu
7fc0: 6d 6e 20 6e 61 6d 65 64 20 52 4f 57 49 44 2c 20  mn named ROWID, 
7fd0: 4f 49 44 2c 20 6f 72 20 5f 52 4f 57 49 44 5f 2e  OID, or _ROWID_.
7fe0: 20 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 68    If the table h
7ff0: 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a  as a column of.*
8000: 2a 20 74 79 70 65 20 49 4e 54 45 47 45 52 20 50  * type INTEGER P
8010: 52 49 4d 41 52 59 20 4b 45 59 20 74 68 65 6e 20  RIMARY KEY then 
8020: 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61  that column is a
8030: 6e 6f 74 68 65 72 20 61 6e 20 61 6c 69 61 73 20  nother an alias 
8040: 66 6f 72 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64  for the.** rowid
8050: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
8060: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
8070: 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6d 6f   rowid of the mo
8080: 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54  st recent INSERT
8090: 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 64 61 74   into.** the dat
80a0: 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65 20 64  abase from the d
80b0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
80c0: 6f 6e 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  on given in the 
80d0: 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d 65  first .** argume
80e0: 6e 74 2e 20 20 49 66 20 6e 6f 20 69 6e 73 65 72  nt.  If no inser
80f0: 74 73 20 68 61 76 65 20 65 76 65 72 20 6f 63 63  ts have ever occ
8100: 75 72 72 65 64 20 6f 6e 20 74 68 69 73 20 64 61  urred on this da
8110: 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
8120: 74 69 6f 6e 2c 20 7a 65 72 6f 20 69 73 20 72 65  tion, zero is re
8130: 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
8140: 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 72   an INSERT occur
8150: 73 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67  s within a trigg
8160: 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  er, then the row
8170: 69 64 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6e 73  id of the.** ins
8180: 65 72 74 65 64 20 72 6f 77 20 69 73 20 72 65 74  erted row is ret
8190: 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f  urned by this ro
81a0: 75 74 69 6e 65 20 61 73 20 6c 6f 6e 67 20 61 73  utine as long as
81b0: 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20   the trigger.** 
81c0: 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 42 75 74  is running.  But
81d0: 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65   once the trigge
81e0: 72 20 74 65 72 6d 69 6e 61 74 65 73 2c 20 74 68  r terminates, th
81f0: 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
8200: 0a 2a 2a 20 62 79 20 74 68 69 73 20 72 6f 75 74  .** by this rout
8210: 69 6e 65 20 72 65 76 65 72 74 73 20 74 6f 20 74  ine reverts to t
8220: 68 65 20 6c 61 73 74 20 76 61 6c 75 65 20 69 6e  he last value in
8230: 73 65 72 74 65 64 20 62 65 66 6f 72 65 20 74 68  serted before th
8240: 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69 72  e.** trigger fir
8250: 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f  ed..**.** If ano
8260: 74 68 65 72 20 74 68 72 65 61 64 20 64 6f 65 73  ther thread does
8270: 20 61 20 6e 65 77 20 69 6e 73 65 72 74 20 6f 6e   a new insert on
8280: 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
8290: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
82a0: 20 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74   while this rout
82b0: 69 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 20 61  ine is running a
82c0: 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65 73 20  nd thus changes 
82d0: 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20  the last insert 
82e0: 72 6f 77 69 64 2c 0a 2a 2a 20 74 68 65 6e 20 74  rowid,.** then t
82f0: 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
8300: 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
8310: 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
8320: 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
8330: 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
8340: 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
8350: 77 69 64 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a  wid(sqlite3*);..
8360: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
8370: 43 6f 75 6e 74 20 54 68 65 20 4e 75 6d 62 65 72  Count The Number
8380: 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65   Of Rows Modifie
8390: 64 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  d.**.** This fun
83a0: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
83b0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61  e number of data
83c0: 62 61 73 65 20 72 6f 77 73 20 74 68 61 74 20 77  base rows that w
83d0: 65 72 65 20 63 68 61 6e 67 65 64 0a 2a 2a 20 28  ere changed.** (
83e0: 6f 72 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64  or inserted or d
83f0: 65 6c 65 74 65 64 29 20 62 79 20 74 68 65 20 6d  eleted) by the m
8400: 6f 73 74 20 72 65 63 65 6e 74 20 53 51 4c 20 73  ost recent SQL s
8410: 74 61 74 65 6d 65 6e 74 2e 20 20 4f 6e 6c 79 0a  tatement.  Only.
8420: 2a 2a 20 63 68 61 6e 67 65 73 20 74 68 61 74 20  ** changes that 
8430: 61 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65  are directly spe
8440: 63 69 66 69 65 64 20 62 79 20 74 68 65 20 49 4e  cified by the IN
8450: 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
8460: 0a 2a 2a 20 44 45 4c 45 54 45 20 73 74 61 74 65  .** DELETE state
8470: 6d 65 6e 74 20 61 72 65 20 63 6f 75 6e 74 65 64  ment are counted
8480: 2e 20 20 41 75 78 69 6c 69 61 72 79 20 63 68 61  .  Auxiliary cha
8490: 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 0a 2a  nges caused by.*
84a0: 2a 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6e  * triggers are n
84b0: 6f 74 20 63 6f 75 6e 74 65 64 2e 20 20 55 73 65  ot counted.  Use
84c0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f   the [sqlite3_to
84d0: 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 66  tal_changes()] f
84e0: 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 66 69  unction.** to fi
84f0: 6e 64 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  nd the total num
8500: 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 69  ber of changes i
8510: 6e 63 6c 75 64 69 6e 67 20 63 68 61 6e 67 65 73  ncluding changes
8520: 20 63 61 75 73 65 64 20 62 79 20 74 72 69 67 67   caused by trigg
8530: 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69  ers..**.** Withi
8540: 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20  n the body of a 
8550: 74 72 69 67 67 65 72 2c 20 74 68 65 20 73 71 6c  trigger, the sql
8560: 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 69  ite3_changes() i
8570: 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 0a  nterface can be.
8580: 2a 2a 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 6e  ** called to fin
8590: 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  d the number of.
85a0: 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  ** changes in th
85b0: 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
85c0: 63 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 54  completed INSERT
85d0: 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c  , UPDATE, or DEL
85e0: 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ETE.** statement
85f0: 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79   within the body
8600: 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2e   of the trigger.
8610: 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 63 68 61 6e 67  .**.** All chang
8620: 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 2c 20  es are counted, 
8630: 65 76 65 6e 20 69 66 20 74 68 65 79 20 77 65 72  even if they wer
8640: 65 20 6c 61 74 65 72 20 75 6e 64 6f 6e 65 20 62  e later undone b
8650: 79 20 61 0a 2a 2a 20 52 4f 4c 4c 42 41 43 4b 20  y a.** ROLLBACK 
8660: 6f 72 20 41 42 4f 52 54 2e 20 20 45 78 63 65 70  or ABORT.  Excep
8670: 74 2c 20 63 68 61 6e 67 65 73 20 61 73 73 6f 63  t, changes assoc
8680: 69 61 74 65 64 20 77 69 74 68 20 63 72 65 61 74  iated with creat
8690: 69 6e 67 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 70  ing and.** dropp
86a0: 69 6e 67 20 74 61 62 6c 65 73 20 61 72 65 20 6e  ing tables are n
86b0: 6f 74 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a  ot counted..**.*
86c0: 2a 20 49 66 20 61 20 63 61 6c 6c 62 61 63 6b 20  * If a callback 
86d0: 69 6e 76 6f 6b 65 73 20 5b 73 71 6c 69 74 65 33  invokes [sqlite3
86e0: 5f 65 78 65 63 28 29 5d 20 6f 72 20 5b 73 71 6c  _exec()] or [sql
86f0: 69 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65 63  ite3_step()] rec
8700: 75 72 73 69 76 65 6c 79 2c 0a 2a 2a 20 74 68 65  ursively,.** the
8710: 6e 20 74 68 65 20 63 68 61 6e 67 65 73 20 69 6e  n the changes in
8720: 20 74 68 65 20 69 6e 6e 65 72 2c 20 72 65 63 75   the inner, recu
8730: 72 73 69 76 65 20 63 61 6c 6c 20 61 72 65 20 63  rsive call are c
8740: 6f 75 6e 74 65 64 20 74 6f 67 65 74 68 65 72 0a  ounted together.
8750: 2a 2a 20 77 69 74 68 20 74 68 65 20 63 68 61 6e  ** with the chan
8760: 67 65 73 20 69 6e 20 74 68 65 20 6f 75 74 65 72  ges in the outer
8770: 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   call..**.** SQL
8780: 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ite implements t
8790: 68 65 20 63 6f 6d 6d 61 6e 64 20 22 44 45 4c 45  he command "DELE
87a0: 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 22 20 77  TE FROM table" w
87b0: 69 74 68 6f 75 74 20 61 20 57 48 45 52 45 20 63  ithout a WHERE c
87c0: 6c 61 75 73 65 0a 2a 2a 20 62 79 20 64 72 6f 70  lause.** by drop
87d0: 70 69 6e 67 20 61 6e 64 20 72 65 63 72 65 61 74  ping and recreat
87e0: 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20  ing the table.  
87f0: 28 54 68 69 73 20 69 73 20 6d 75 63 68 20 66 61  (This is much fa
8800: 73 74 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 0a  ster than going.
8810: 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e 64 20 64  ** through and d
8820: 65 6c 65 74 69 6e 67 20 69 6e 64 69 76 69 64 75  eleting individu
8830: 61 6c 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d  al elements from
8840: 20 74 68 65 20 74 61 62 6c 65 2e 29 20 20 42 65   the table.)  Be
8850: 63 61 75 73 65 20 6f 66 0a 2a 2a 20 74 68 69 73  cause of.** this
8860: 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74   optimization, t
8870: 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20  he change count 
8880: 66 6f 72 20 22 44 45 4c 45 54 45 20 46 52 4f 4d  for "DELETE FROM
8890: 20 74 61 62 6c 65 22 20 77 69 6c 6c 20 62 65 0a   table" will be.
88a0: 2a 2a 20 7a 65 72 6f 20 72 65 67 61 72 64 6c 65  ** zero regardle
88b0: 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  ss of the number
88c0: 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 74 68 61   of elements tha
88d0: 74 20 77 65 72 65 20 6f 72 69 67 69 6e 61 6c 6c  t were originall
88e0: 79 20 69 6e 20 74 68 65 0a 2a 2a 20 74 61 62 6c  y in the.** tabl
88f0: 65 2e 20 54 6f 20 67 65 74 20 61 6e 20 61 63 63  e. To get an acc
8900: 75 72 61 74 65 20 63 6f 75 6e 74 20 6f 66 20 74  urate count of t
8910: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
8920: 73 20 64 65 6c 65 74 65 64 2c 20 75 73 65 0a 2a  s deleted, use.*
8930: 2a 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74  * "DELETE FROM t
8940: 61 62 6c 65 20 57 48 45 52 45 20 31 22 20 69 6e  able WHERE 1" in
8950: 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  stead..**.** If 
8960: 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d  another thread m
8970: 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20  akes changes on 
8980: 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
8990: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
89a0: 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69  while this routi
89b0: 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68  ne is running th
89c0: 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  en the return va
89d0: 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74  lue of this rout
89e0: 69 6e 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  ine.** is undefi
89f0: 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ned..*/.SQLITE_A
8a00: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
8a10: 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29  hanges(sqlite3*)
8a20: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
8a30: 46 3a 20 54 6f 74 61 6c 20 4e 75 6d 62 65 72 20  F: Total Number 
8a40: 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64  Of Rows Modified
8a50: 0a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  .***.** This fun
8a60: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
8a70: 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61  e number of data
8a80: 62 61 73 65 20 72 6f 77 73 20 74 68 61 74 20 68  base rows that h
8a90: 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6d 6f 64 69  ave been.** modi
8aa0: 66 69 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20  fied by INSERT, 
8ab0: 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
8ac0: 20 73 74 61 74 65 6d 65 6e 74 73 20 73 69 6e 63   statements sinc
8ad0: 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  e the database h
8ae0: 61 6e 64 6c 65 0a 2a 2a 20 77 61 73 20 6f 70 65  andle.** was ope
8af0: 6e 65 64 2e 20 54 68 69 73 20 69 6e 63 6c 75 64  ned. This includ
8b00: 65 73 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52  es UPDATE, INSER
8b10: 54 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61  T and DELETE sta
8b20: 74 65 6d 65 6e 74 73 20 65 78 65 63 75 74 65 64  tements executed
8b30: 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74  .** as part of t
8b40: 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e  rigger programs.
8b50: 20 41 6c 6c 20 63 68 61 6e 67 65 73 20 61 72 65   All changes are
8b60: 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e   counted as soon
8b70: 20 61 73 20 74 68 65 0a 2a 2a 20 73 74 61 74 65   as the.** state
8b80: 6d 65 6e 74 20 74 68 61 74 20 6d 61 6b 65 73 20  ment that makes 
8b90: 74 68 65 6d 20 69 73 20 63 6f 6d 70 6c 65 74 65  them is complete
8ba0: 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 61 74  d (when the stat
8bb0: 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 0a  ement handle is.
8bc0: 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71  ** passed to [sq
8bd0: 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
8be0: 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  r [sqlite3_final
8bf0: 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53  ize()])..**.** S
8c00: 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c  ee also the [sql
8c10: 69 74 65 33 5f 63 68 61 6e 67 65 28 29 5d 20 69  ite3_change()] i
8c20: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
8c30: 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74  SQLite implement
8c40: 73 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22 44  s the command "D
8c50: 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
8c60: 22 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52  " without a WHER
8c70: 45 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 20 64  E clause.** by d
8c80: 72 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 63 72  ropping and recr
8c90: 65 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65  eating the table
8ca0: 2e 20 20 28 54 68 69 73 20 69 73 20 6d 75 63 68  .  (This is much
8cb0: 20 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f 69   faster than goi
8cc0: 6e 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e  ng.** through an
8cd0: 64 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69 76  d deleting indiv
8ce0: 69 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20 66  idual elements f
8cf0: 6f 72 6d 20 74 68 65 20 74 61 62 6c 65 2e 29 20  orm the table.) 
8d00: 20 42 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20 74   Because of.** t
8d10: 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
8d20: 2c 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75  , the change cou
8d30: 6e 74 20 66 6f 72 20 22 44 45 4c 45 54 45 20 46  nt for "DELETE F
8d40: 52 4f 4d 20 74 61 62 6c 65 22 20 77 69 6c 6c 20  ROM table" will 
8d50: 62 65 0a 2a 2a 20 7a 65 72 6f 20 72 65 67 61 72  be.** zero regar
8d60: 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d  dless of the num
8d70: 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
8d80: 74 68 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e  that were origin
8d90: 61 6c 6c 79 20 69 6e 20 74 68 65 0a 2a 2a 20 74  ally in the.** t
8da0: 61 62 6c 65 2e 20 54 6f 20 67 65 74 20 61 6e 20  able. To get an 
8db0: 61 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20 6f  accurate count o
8dc0: 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
8dd0: 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75 73  rows deleted, us
8de0: 65 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52 4f  e.** "DELETE FRO
8df0: 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 31 22  M table WHERE 1"
8e00: 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   instead..**.** 
8e10: 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  If another threa
8e20: 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20  d makes changes 
8e30: 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  on the same data
8e40: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
8e50: 2a 2a 20 77 68 69 6c 65 20 74 68 69 73 20 72 6f  ** while this ro
8e60: 75 74 69 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67  utine is running
8e70: 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
8e80: 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72   value of this r
8e90: 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 75 6e 64  outine.** is und
8ea0: 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
8eb0: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
8ec0: 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
8ed0: 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
8ee0: 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 74 65  * CAPI3REF: Inte
8ef0: 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52 75 6e  rrupt A Long-Run
8f00: 6e 69 6e 67 20 51 75 65 72 79 0a 2a 2a 0a 2a 2a  ning Query.**.**
8f10: 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   This function c
8f20: 61 75 73 65 73 20 61 6e 79 20 70 65 6e 64 69 6e  auses any pendin
8f30: 67 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61  g database opera
8f40: 74 69 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e  tion to abort an
8f50: 64 0a 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 69  d.** return at i
8f60: 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f  ts earliest oppo
8f70: 72 74 75 6e 69 74 79 2e 20 20 54 68 69 73 20 72  rtunity.  This r
8f80: 6f 75 74 69 6e 65 20 69 73 20 74 79 70 69 63 61  outine is typica
8f90: 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e  lly.** called in
8fa0: 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 75   response to a u
8fb0: 73 65 72 20 61 63 74 69 6f 6e 20 73 75 63 68 20  ser action such 
8fc0: 61 73 20 70 72 65 73 73 69 6e 67 20 22 43 61 6e  as pressing "Can
8fd0: 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d  cel".** or Ctrl-
8fe0: 43 20 77 68 65 72 65 20 74 68 65 20 75 73 65 72  C where the user
8ff0: 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75   wants a long qu
9000: 65 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f  ery operation to
9010: 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61   halt.** immedia
9020: 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  tely..**.** It i
9030: 73 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74  s safe to call t
9040: 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d  his routine from
9050: 20 61 20 74 68 72 65 61 64 20 64 69 66 66 65 72   a thread differ
9060: 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ent from the.** 
9070: 74 68 72 65 61 64 20 74 68 61 74 20 69 73 20 63  thread that is c
9080: 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67  urrently running
9090: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 70   the database op
90a0: 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 74  eration.  But it
90b0: 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61 66 65 20  .** is not safe 
90c0: 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75  to call this rou
90d0: 74 69 6e 65 20 77 69 74 68 20 61 20 64 61 74 61  tine with a data
90e0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
90f0: 74 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65  that.** is close
9100: 64 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 65  d or might close
9110: 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f   before sqlite3_
9120: 69 6e 74 65 72 72 75 70 74 28 29 20 72 65 74 75  interrupt() retu
9130: 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53  rns..**.** The S
9140: 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  QL operation tha
9150: 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  t is interrupted
9160: 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51   will return [SQ
9170: 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e  LITE_INTERRUPT].
9180: 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 72  .** If an interr
9190: 75 70 74 65 64 20 6f 70 65 72 61 74 69 6f 6e 20  upted operation 
91a0: 77 61 73 20 61 6e 20 75 70 64 61 74 65 20 74 68  was an update th
91b0: 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e 0a  at is inside an.
91c0: 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e  ** explicit tran
91d0: 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  saction, then th
91e0: 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63  e entire transac
91f0: 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f 6c  tion will be rol
9200: 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 61 75 74 6f  led.** back auto
9210: 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2f 0a 53 51  matically..*/.SQ
9220: 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
9230: 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
9240: 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
9250: 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 74 65  * CAPI3REF: Dete
9260: 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51 4c 20  rmine If An SQL 
9270: 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43 6f 6d  Statement Is Com
9280: 70 6c 65 74 65 0a 2a 2a 0a 2a 2a 20 54 68 65 73  plete.**.** Thes
9290: 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  e functions retu
92a0: 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 67  rn true if the g
92b0: 69 76 65 6e 20 69 6e 70 75 74 20 73 74 72 69 6e  iven input strin
92c0: 67 20 63 6f 6d 70 72 69 73 65 73 0a 2a 2a 20 6f  g comprises.** o
92d0: 6e 65 20 6f 72 20 6d 6f 72 65 20 63 6f 6d 70 6c  ne or more compl
92e0: 65 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ete SQL statemen
92f0: 74 73 2e 20 46 6f 72 20 74 68 65 20 73 71 6c 69  ts. For the sqli
9300: 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 63  te3_complete() c
9310: 61 6c 6c 2c 0a 2a 2a 20 74 68 65 20 70 61 72 61  all,.** the para
9320: 6d 65 74 65 72 20 6d 75 73 74 20 62 65 20 61 20  meter must be a 
9330: 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  nul-terminated U
9340: 54 46 2d 38 20 73 74 72 69 6e 67 2e 20 46 6f 72  TF-8 string. For
9350: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  .** sqlite3_comp
9360: 6c 65 74 65 31 36 28 29 2c 20 61 20 6e 75 6c 2d  lete16(), a nul-
9370: 74 65 72 6d 69 6e 61 74 65 64 20 6d 61 63 68 69  terminated machi
9380: 6e 65 20 62 79 74 65 20 6f 72 64 65 72 20 55 54  ne byte order UT
9390: 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69  F-16 string.** i
93a0: 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a  s required..**.*
93b0: 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
93c0: 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20   are useful for 
93d0: 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e 70  command-line inp
93e0: 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ut to determine 
93f0: 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  if the.** curren
9400: 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78 74  tly entered text
9410: 20 66 6f 72 6d 73 20 6f 6e 65 20 6f 72 20 6d 6f   forms one or mo
9420: 72 65 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20  re complete SQL 
9430: 73 74 61 74 65 6d 65 6e 74 73 20 6f 72 0a 2a 2a  statements or.**
9440: 20 69 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69   if additional i
9450: 6e 70 75 74 20 69 73 20 6e 65 65 64 65 64 20 62  nput is needed b
9460: 65 66 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68  efore sending th
9470: 65 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 74  e statements int
9480: 6f 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20  o.** SQLite for 
9490: 70 61 72 73 69 6e 67 2e 20 54 68 65 20 61 6c 67  parsing. The alg
94a0: 6f 72 69 74 68 6d 20 69 73 20 73 69 6d 70 6c 65  orithm is simple
94b0: 2e 20 20 49 66 20 74 68 65 20 0a 2a 2a 20 6c 61  .  If the .** la
94c0: 73 74 20 74 6f 6b 65 6e 20 6f 74 68 65 72 20 74  st token other t
94d0: 68 61 6e 20 73 70 61 63 65 73 20 61 6e 64 20 63  han spaces and c
94e0: 6f 6d 6d 65 6e 74 73 20 69 73 20 61 20 73 65 6d  omments is a sem
94f0: 69 63 6f 6c 6f 6e 2c 20 74 68 65 6e 20 72 65 74  icolon, then ret
9500: 75 72 6e 20 0a 2a 2a 20 74 72 75 65 2e 20 20 41  urn .** true.  A
9510: 63 74 75 61 6c 6c 79 2c 20 74 68 65 20 61 6c 67  ctually, the alg
9520: 6f 72 69 74 68 6d 20 69 73 20 61 20 6c 69 74 74  orithm is a litt
9530: 6c 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61  le more complica
9540: 74 65 64 20 74 68 61 6e 20 74 68 61 74 0a 2a 2a  ted than that.**
9550: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 65 61   in order to dea
9560: 6c 20 77 69 74 68 20 74 72 69 67 67 65 72 73 2c  l with triggers,
9570: 20 62 75 74 20 74 68 65 20 62 61 73 69 63 20 69   but the basic i
9580: 64 65 61 20 69 73 20 74 68 65 20 73 61 6d 65 3a  dea is the same:
9590: 20 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65    the.** stateme
95a0: 6e 74 20 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65  nt is not comple
95b0: 74 65 20 75 6e 6c 65 73 73 20 69 74 20 65 6e 64  te unless it end
95c0: 73 20 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f 6e  s in a semicolon
95d0: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
95e0: 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  int sqlite3_comp
95f0: 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20  lete(const char 
9600: 2a 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  *sql);.SQLITE_AP
9610: 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
9620: 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20 76  mplete16(const v
9630: 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a  oid *sql);../*.*
9640: 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69  * CAPI3REF: Regi
9650: 73 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b 20  ster A Callback 
9660: 54 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45  To Handle SQLITE
9670: 5f 42 55 53 59 20 45 72 72 6f 72 73 0a 2a 2a 0a  _BUSY Errors.**.
9680: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
9690: 69 64 65 6e 74 69 66 69 65 73 20 61 20 63 61 6c  identifies a cal
96a0: 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74  lback function t
96b0: 68 61 74 20 6d 69 67 68 74 20 62 65 20 69 6e 76  hat might be inv
96c0: 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 65 76 65 72  oked.** whenever
96d0: 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
96e0: 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 64 61  ade to open a da
96f0: 74 61 62 61 73 65 20 74 61 62 6c 65 20 0a 2a 2a  tabase table .**
9700: 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68   that another th
9710: 72 65 61 64 20 6f 72 20 70 72 6f 63 65 73 73 20  read or process 
9720: 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a 20 49  has locked..** I
9730: 66 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62  f the busy callb
9740: 61 63 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  ack is NULL, the
9750: 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 0a  n [SQLITE_BUSY].
9760: 2a 2a 20 28 6f 72 20 73 6f 6d 65 74 69 6d 65 73  ** (or sometimes
9770: 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
9780: 4c 4f 43 4b 45 44 5d 29 0a 2a 2a 20 69 73 20 72  LOCKED]).** is r
9790: 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74  eturned immediat
97a0: 65 6c 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74  ely upon encount
97b0: 65 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 0a  ering the lock..
97c0: 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20 63  ** If the busy c
97d0: 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 4e  allback is not N
97e0: 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ULL, then the.**
97f0: 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 62   callback will b
9800: 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 74  e invoked with t
9810: 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54  wo arguments.  T
9820: 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75  he.** first argu
9830: 6d 65 6e 74 20 74 6f 20 74 68 65 20 68 61 6e 64  ment to the hand
9840: 6c 65 72 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ler is a copy of
9850: 20 74 68 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74   the void* point
9860: 65 72 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74  er which.** is t
9870: 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
9880: 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  t to this routin
9890: 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61  e.  The second a
98a0: 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68  rgument to.** th
98b0: 65 20 68 61 6e 64 6c 65 72 20 69 73 20 74 68 65  e handler is the
98c0: 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
98d0: 20 74 68 61 74 20 74 68 65 20 62 75 73 79 20 68   that the busy h
98e0: 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20 62 65  andler has.** be
98f0: 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20 74  en invoked for t
9900: 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76 65 6e  his locking even
9910: 74 2e 20 49 66 20 74 68 65 0a 2a 2a 20 62 75 73  t. If the.** bus
9920: 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  y callback retur
9930: 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f 20 61 64  ns 0, then no ad
9940: 64 69 74 69 6f 6e 61 6c 20 61 74 74 65 6d 70 74  ditional attempt
9950: 73 20 61 72 65 20 6d 61 64 65 20 74 6f 0a 2a 2a  s are made to.**
9960: 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
9970: 62 61 73 65 20 61 6e 64 20 5b 53 51 4c 49 54 45  base and [SQLITE
9980: 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54  _BUSY] or [SQLIT
9990: 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
99a0: 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
99b0: 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   If the callback
99c0: 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
99d0: 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 65 72 20  o, then another 
99e0: 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
99f0: 74 6f 20 6f 70 65 6e 20 74 68 65 0a 2a 2a 20 64  to open the.** d
9a00: 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61 64  atabase for read
9a10: 69 6e 67 20 61 6e 64 20 74 68 65 20 63 79 63 6c  ing and the cycl
9a20: 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a  e repeats..**.**
9a30: 20 54 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66   The presence of
9a40: 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20   a busy handler 
9a50: 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61 6e 74  does not guarant
9a60: 65 65 20 74 68 61 74 0a 2a 2a 20 69 74 20 77 69  ee that.** it wi
9a70: 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  ll be invoked wh
9a80: 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f 63 6b  en there is lock
9a90: 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 0a 2a 2a 20   contention..** 
9aa0: 49 66 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d  If SQLite determ
9ab0: 69 6e 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69  ines that invoki
9ac0: 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ng the busy hand
9ad0: 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 75 6c 74  ler could result
9ae0: 20 69 6e 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63   in.** a deadloc
9af0: 6b 2c 20 69 74 20 77 69 6c 6c 20 72 65 74 75 72  k, it will retur
9b00: 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  n [SQLITE_BUSY] 
9b10: 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 43 6f 6e 73  instead..** Cons
9b20: 69 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20  ider a scenario 
9b30: 77 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73  where one proces
9b40: 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72  s is holding a r
9b50: 65 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a  ead lock that.**
9b60: 20 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f   it is trying to
9b70: 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65   promote to a re
9b80: 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a  served lock and.
9b90: 2a 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63  ** a second proc
9ba0: 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  ess is holding a
9bb0: 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74   reserved lock t
9bc0: 68 61 74 20 69 74 20 69 73 20 74 72 79 69 6e 67  hat it is trying
9bd0: 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  .** to promote t
9be0: 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  o an exclusive l
9bf0: 6f 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20  ock.  The first 
9c00: 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70  process cannot p
9c10: 72 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73  roceed.** becaus
9c20: 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20  e it is blocked 
9c30: 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e  by the second an
9c40: 64 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f  d the second pro
9c50: 63 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70  cess cannot.** p
9c60: 72 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 69  roceed because i
9c70: 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
9c80: 74 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 62  the first.  If b
9c90: 6f 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a  oth processes.**
9ca0: 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   invoke the busy
9cb0: 20 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68   handlers, neith
9cc0: 65 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79  er will make any
9cd0: 20 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72   progress.  Ther
9ce0: 65 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65  efore,.** SQLite
9cf0: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
9d00: 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66  _BUSY] for the f
9d10: 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f  irst process, ho
9d20: 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a  ping that this.*
9d30: 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68  * will induce th
9d40: 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20  e first process 
9d50: 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 72  to release its r
9d60: 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c  ead lock and all
9d70: 6f 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  ow.** the second
9d80: 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63   process to proc
9d90: 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  eed..**.** The d
9da0: 65 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c  efault busy call
9db0: 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a  back is NULL..**
9dc0: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
9dd0: 42 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63  BUSY] error is c
9de0: 6f 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c  onverted to [SQL
9df0: 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
9e00: 44 5d 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74  D] when.** SQLit
9e10: 65 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64  e is in the midd
9e20: 6c 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72  le of a large tr
9e30: 61 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20  ansaction where 
9e40: 61 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67  all the.** chang
9e50: 65 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20  es will not fit 
9e60: 69 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  into the in-memo
9e70: 72 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74  ry cache.  SQLit
9e80: 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64  e will.** alread
9e90: 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45  y hold a RESERVE
9ea0: 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  D lock on the da
9eb0: 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74  tabase file, but
9ec0: 20 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20   it needs.** to 
9ed0: 70 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63  promote this loc
9ee0: 6b 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73  k to EXCLUSIVE s
9ef0: 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 70  o that it can sp
9f00: 69 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67  ill cache.** pag
9f10: 65 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  es into the data
9f20: 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75  base file withou
9f30: 74 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72  t harm to concur
9f40: 72 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e  rent.** readers.
9f50: 20 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c    If it is unabl
9f60: 65 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65  e to promote the
9f70: 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20   lock, then the 
9f80: 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63  in-memory.** cac
9f90: 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20  he will be left 
9fa0: 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
9fb0: 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20  nt state and so 
9fc0: 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64  the error.** cod
9fd0: 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72  e is promoted fr
9fe0: 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c  om the relativel
9ff0: 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45  y benign [SQLITE
a000: 5f 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65  _BUSY] to.** the
a010: 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51   more severe [SQ
a020: 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
a030: 45 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72  ED].  This error
a040: 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a   code promotion.
a050: 2a 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74  ** forces an aut
a060: 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20  omatic rollback 
a070: 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20  of the changes. 
a080: 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72  See the.** <a hr
a090: 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 73  ef="http://www.s
a0a0: 71 6c 69 74 65 2e 6f 72 67 2f 63 76 73 74 72 61  qlite.org/cvstra
a0b0: 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72 75 70 74  c/wiki?p=Corrupt
a0c0: 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79  ionFollowingBusy
a0d0: 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f 72 72 75  Error">.** Corru
a0e0: 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75  ptionFollowingBu
a0f0: 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77 69 6b 69  syError</a> wiki
a100: 20 70 61 67 65 20 66 6f 72 20 61 20 64 69 73 63   page for a disc
a110: 75 73 73 69 6f 6e 20 6f 66 20 77 68 79 0a 2a 2a  ussion of why.**
a120: 20 74 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61   this is importa
a130: 6e 74 2e 0a 2a 2a 09 0a 2a 2a 20 53 71 6c 69 74  nt..**..** Sqlit
a140: 65 20 69 73 20 72 65 2d 65 6e 74 72 61 6e 74 2c  e is re-entrant,
a150: 20 73 6f 20 74 68 65 20 62 75 73 79 20 68 61 6e   so the busy han
a160: 64 6c 65 72 20 6d 61 79 20 73 74 61 72 74 20 61  dler may start a
a170: 20 6e 65 77 20 71 75 65 72 79 2e 20 0a 2a 2a 20   new query. .** 
a180: 28 49 74 20 69 73 20 6e 6f 74 20 63 6c 65 61 72  (It is not clear
a190: 20 77 68 79 20 61 6e 79 6f 6e 65 20 77 6f 75 6c   why anyone woul
a1a0: 64 20 65 76 65 72 79 20 77 61 6e 74 20 74 6f 20  d every want to 
a1b0: 64 6f 20 74 68 69 73 2c 20 62 75 74 20 69 74 0a  do this, but it.
a1c0: 2a 2a 20 69 73 20 61 6c 6c 6f 77 65 64 2c 20 69  ** is allowed, i
a1d0: 6e 20 74 68 65 6f 72 79 2e 29 20 20 42 75 74 20  n theory.)  But 
a1e0: 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
a1f0: 20 6d 61 79 20 6e 6f 74 20 63 6c 6f 73 65 20 74   may not close t
a200: 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  he.** database. 
a210: 20 43 6c 6f 73 69 6e 67 20 74 68 65 20 64 61 74   Closing the dat
a220: 61 62 61 73 65 20 66 72 6f 6d 20 61 20 62 75 73  abase from a bus
a230: 79 20 68 61 6e 64 6c 65 72 20 77 69 6c 6c 20 64  y handler will d
a240: 65 6c 65 74 65 20 0a 2a 2a 20 64 61 74 61 20 73  elete .** data s
a250: 74 72 75 63 74 75 72 65 73 20 6f 75 74 20 66 72  tructures out fr
a260: 6f 6d 20 75 6e 64 65 72 20 74 68 65 20 65 78 65  om under the exe
a270: 63 75 74 69 6e 67 20 71 75 65 72 79 20 61 6e 64  cuting query and
a280: 20 77 69 6c 6c 20 0a 2a 2a 20 70 72 6f 62 61 62   will .** probab
a290: 6c 79 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73  ly result in a s
a2a0: 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c  egmentation faul
a2b0: 74 20 6f 72 20 6f 74 68 65 72 20 72 75 6e 74 69  t or other runti
a2c0: 6d 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  me error..**.** 
a2d0: 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62  There can only b
a2e0: 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20  e a single busy 
a2f0: 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 64 20  handler defined 
a300: 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
a310: 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  e.** connection.
a320: 20 20 53 65 74 74 69 6e 67 20 61 20 6e 65 77 20    Setting a new 
a330: 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63 6c 65  busy handler cle
a340: 61 72 73 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ars any previous
a350: 20 6f 6e 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68   one..** Note th
a360: 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  at calling [sqli
a370: 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
a380: 28 29 5d 20 77 69 6c 6c 20 61 6c 73 6f 20 73 65  ()] will also se
a390: 74 20 6f 72 20 63 6c 65 61 72 0a 2a 2a 20 74 68  t or clear.** th
a3a0: 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a  e busy handler..
a3b0: 2a 2a 0a 2a 2a 20 57 68 65 6e 20 6f 70 65 72 61  **.** When opera
a3c0: 74 69 6e 67 20 69 6e 20 5b 73 71 6c 69 74 65 33  ting in [sqlite3
a3d0: 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
a3e0: 61 63 68 65 20 7c 20 73 68 61 72 65 64 20 63 61  ache | shared ca
a3f0: 63 68 65 20 6d 6f 64 65 5d 2c 0a 2a 2a 20 6f 6e  che mode],.** on
a400: 6c 79 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79  ly a single busy
a410: 20 68 61 6e 64 6c 65 72 20 63 61 6e 20 62 65 20   handler can be 
a420: 64 65 66 69 6e 65 64 20 66 6f 72 20 65 61 63 68  defined for each
a430: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
a440: 2a 2a 20 53 6f 20 69 66 20 74 77 6f 20 64 61 74  ** So if two dat
a450: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
a460: 73 20 73 68 61 72 65 20 61 20 73 69 6e 67 6c 65  s share a single
a470: 20 63 61 63 68 65 2c 20 74 68 65 6e 20 63 68 61   cache, then cha
a480: 6e 67 69 6e 67 0a 2a 2a 20 74 68 65 20 62 75 73  nging.** the bus
a490: 79 20 68 61 6e 64 6c 65 72 20 6f 6e 20 6f 6e 65  y handler on one
a4a0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c   connection will
a4b0: 20 61 6c 73 6f 20 63 68 61 6e 67 65 20 74 68 65   also change the
a4c0: 20 62 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72   busy.** handler
a4d0: 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 63 6f   in the other co
a4e0: 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 62  nnection.  The b
a4f0: 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20 69  usy handler is i
a500: 6e 76 6f 6b 65 64 0a 2a 2a 20 69 6e 20 74 68 65  nvoked.** in the
a510: 20 74 68 72 65 61 64 20 74 68 61 74 20 77 61 73   thread that was
a520: 20 72 75 6e 6e 69 6e 67 20 77 68 65 6e 20 74 68   running when th
a530: 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 77 61  e SQLITE_BUSY wa
a540: 73 20 68 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s hit..*/.SQLITE
a550: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
a560: 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71  _busy_handler(sq
a570: 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76  lite3*, int(*)(v
a580: 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a  oid*,int), void*
a590: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
a5a0: 45 46 3a 20 53 65 74 20 41 20 42 75 73 79 20 54  EF: Set A Busy T
a5b0: 69 6d 65 6f 75 74 0a 2a 2a 0a 2a 2a 20 54 68 69  imeout.**.** Thi
a5c0: 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61  s routine sets a
a5d0: 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 74 68   busy handler th
a5e0: 61 74 20 73 6c 65 65 70 73 20 66 6f 72 20 61 20  at sleeps for a 
a5f0: 77 68 69 6c 65 20 77 68 65 6e 20 61 0a 2a 2a 20  while when a.** 
a600: 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e  table is locked.
a610: 20 20 54 68 65 20 68 61 6e 64 6c 65 72 20 77 69    The handler wi
a620: 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c  ll sleep multipl
a630: 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 0a 2a  e times until .*
a640: 2a 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20  * at least "ms" 
a650: 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20  milliseconds of 
a660: 73 6c 65 65 70 69 6e 67 20 68 61 76 65 20 62 65  sleeping have be
a670: 65 6e 20 64 6f 6e 65 2e 20 20 41 66 74 65 72 0a  en done.  After.
a680: 2a 2a 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 63  ** "ms" millisec
a690: 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e 67  onds of sleeping
a6a0: 2c 20 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65  , the handler re
a6b0: 74 75 72 6e 73 20 30 20 77 68 69 63 68 0a 2a 2a  turns 0 which.**
a6c0: 20 63 61 75 73 65 73 20 5b 73 71 6c 69 74 65 33   causes [sqlite3
a6d0: 5f 73 74 65 70 28 29 5d 20 74 6f 20 72 65 74 75  _step()] to retu
a6e0: 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  rn [SQLITE_BUSY]
a6f0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52   or [SQLITE_IOER
a700: 52 5f 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a  R_BLOCKED]..**.*
a710: 2a 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  * Calling this r
a720: 6f 75 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61  outine with an a
a730: 72 67 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61  rgument less tha
a740: 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65  n or equal to ze
a750: 72 6f 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20  ro.** turns off 
a760: 61 6c 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72  all busy handler
a770: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63  s..**.** There c
a780: 61 6e 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e  an only be a sin
a790: 67 6c 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  gle busy handler
a7a0: 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61   for a particula
a7b0: 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  r database.** co
a7c0: 6e 6e 65 63 74 69 6f 6e 2e 20 20 49 66 20 61 6e  nnection.  If an
a7d0: 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c  other busy handl
a7e0: 65 72 20 77 61 73 20 64 65 66 69 6e 65 64 20 20  er was defined  
a7f0: 0a 2a 2a 20 28 75 73 69 6e 67 20 5b 73 71 6c 69  .** (using [sqli
a800: 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
a810: 28 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61  ()]) prior to ca
a820: 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f  lling.** this ro
a830: 75 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65  utine, that othe
a840: 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69  r busy handler i
a850: 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 53 51  s cleared..*/.SQ
a860: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
a870: 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75  ite3_busy_timeou
a880: 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20  t(sqlite3*, int 
a890: 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ms);../*.** CAPI
a8a0: 33 52 45 46 3a 20 43 6f 6e 76 65 6e 69 65 6e 63  3REF: Convenienc
a8b0: 65 20 52 6f 75 74 69 6e 65 73 20 46 6f 72 20 52  e Routines For R
a8c0: 75 6e 6e 69 6e 67 20 51 75 65 72 69 65 73 0a 2a  unning Queries.*
a8d0: 2a 0a 2a 2a 20 54 68 69 73 20 6e 65 78 74 20 72  *.** This next r
a8e0: 6f 75 74 69 6e 65 20 69 73 20 61 20 63 6f 6e 76  outine is a conv
a8f0: 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 20  enience wrapper 
a900: 61 72 6f 75 6e 64 20 5b 73 71 6c 69 74 65 33 5f  around [sqlite3_
a910: 65 78 65 63 28 29 5d 2e 0a 2a 2a 20 49 6e 73 74  exec()]..** Inst
a920: 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20  ead of invoking 
a930: 61 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20  a user-supplied 
a940: 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61 63  callback for eac
a950: 68 20 72 6f 77 20 6f 66 20 74 68 65 0a 2a 2a 20  h row of the.** 
a960: 72 65 73 75 6c 74 2c 20 74 68 69 73 20 72 6f 75  result, this rou
a970: 74 69 6e 65 20 72 65 6d 65 6d 62 65 72 73 20 65  tine remembers e
a980: 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 72  ach row of the r
a990: 65 73 75 6c 74 20 69 6e 20 6d 65 6d 6f 72 79 0a  esult in memory.
a9a0: 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ** obtained from
a9b0: 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
a9c0: 28 29 5d 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  ()], then return
a9d0: 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 72 65 73  s all of the res
a9e0: 75 6c 74 20 61 66 74 65 72 20 74 68 65 0a 2a 2a  ult after the.**
a9f0: 20 71 75 65 72 79 20 68 61 73 20 66 69 6e 69 73   query has finis
aa00: 68 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 41 73 20 61  hed. .**.** As a
aa10: 6e 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f  n example, suppo
aa20: 73 65 20 74 68 65 20 71 75 65 72 79 20 72 65 73  se the query res
aa30: 75 6c 74 20 77 68 65 72 65 20 74 68 69 73 20 74  ult where this t
aa40: 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  able:.**.** <blo
aa50: 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
aa60: 20 20 20 20 20 20 20 20 4e 61 6d 65 20 20 20 20          Name    
aa70: 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20 20 20 20      | Age.**    
aa80: 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      ------------
aa90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
aaa0: 20 20 20 20 20 20 41 6c 69 63 65 20 20 20 20 20        Alice     
aab0: 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20 20 20 20    | 43.**       
aac0: 20 42 6f 62 20 20 20 20 20 20 20 20 20 7c 20 32   Bob         | 2
aad0: 38 0a 2a 2a 20 20 20 20 20 20 20 20 43 69 6e 64  8.**        Cind
aae0: 79 20 20 20 20 20 20 20 7c 20 32 31 0a 2a 2a 20  y       | 21.** 
aaf0: 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
ab00: 74 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  te>.**.** If the
ab10: 20 33 72 64 20 61 72 67 75 6d 65 6e 74 20 77 65   3rd argument we
ab20: 72 65 20 26 61 7a 52 65 73 75 6c 74 20 74 68 65  re &azResult the
ab30: 6e 20 61 66 74 65 72 20 74 68 65 20 66 75 6e 63  n after the func
ab40: 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a 2a 2a 20  tion returns.** 
ab50: 61 7a 52 65 73 75 6c 74 20 77 69 6c 6c 20 63 6f  azResult will co
ab60: 6e 74 61 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  ntain the follow
ab70: 69 6e 67 20 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20  ing data:.**.** 
ab80: 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
ab90: 3e 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65  >.**        azRe
aba0: 73 75 6c 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e  sult&#91;0] = "N
abb0: 61 6d 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20  ame";.**        
abc0: 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 31 5d 20  azResult&#91;1] 
abd0: 3d 20 22 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20  = "Age";.**     
abe0: 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b     azResult&#91;
abf0: 32 5d 20 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a  2] = "Alice";.**
ac00: 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
ac10: 26 23 39 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a  &#91;3] = "43";.
ac20: 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
ac30: 6c 74 26 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62  lt&#91;4] = "Bob
ac40: 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
ac50: 65 73 75 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22  esult&#91;5] = "
ac60: 32 38 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61  28";.**        a
ac70: 7a 52 65 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d  zResult&#91;6] =
ac80: 20 22 43 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20   "Cindy";.**    
ac90: 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
aca0: 3b 37 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c  ;7] = "21";.** <
acb0: 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74  /pre></blockquot
acc0: 65 3e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65 20  e>.**.** Notice 
acd0: 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 6e  that there is an
ace0: 20 65 78 74 72 61 20 72 6f 77 20 6f 66 20 64 61   extra row of da
acf0: 74 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ta containing th
ad00: 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 68 65 61 64  e column.** head
ad10: 65 72 73 2e 20 20 42 75 74 20 74 68 65 20 2a 6e  ers.  But the *n
ad20: 72 6f 77 20 72 65 74 75 72 6e 20 76 61 6c 75 65  row return value
ad30: 20 69 73 20 73 74 69 6c 6c 20 33 2e 20 20 2a 6e   is still 3.  *n
ad40: 63 6f 6c 75 6d 6e 20 69 73 0a 2a 2a 20 73 65 74  column is.** set
ad50: 20 74 6f 20 32 2e 20 20 49 6e 20 67 65 6e 65 72   to 2.  In gener
ad60: 61 6c 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  al, the number o
ad70: 66 20 76 61 6c 75 65 73 20 69 6e 73 65 72 74 65  f values inserte
ad80: 64 20 69 6e 74 6f 20 61 7a 52 65 73 75 6c 74 0a  d into azResult.
ad90: 2a 2a 20 77 69 6c 6c 20 62 65 20 28 28 2a 6e 72  ** will be ((*nr
ada0: 6f 77 29 20 2b 20 31 29 2a 28 2a 6e 63 6f 6c 75  ow) + 1)*(*ncolu
adb0: 6d 6e 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72  mn)..**.** After
adc0: 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   the calling fun
add0: 63 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 73 68  ction has finish
ade0: 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 65 73  ed using the res
adf0: 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c 64 20 0a  ult, it should .
ae00: 2a 2a 20 70 61 73 73 20 74 68 65 20 72 65 73 75  ** pass the resu
ae10: 6c 74 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20  lt data pointer 
ae20: 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  to sqlite3_free_
ae30: 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64 65 72  table() in order
ae40: 20 74 6f 20 0a 2a 2a 20 72 65 6c 65 61 73 65 20   to .** release 
ae50: 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20  the memory that 
ae60: 77 61 73 20 6d 61 6c 6c 6f 63 2d 65 64 2e 20 20  was malloc-ed.  
ae70: 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77  Because of the w
ae80: 61 79 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69  ay the .** [sqli
ae90: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61  te3_malloc()] ha
aea0: 70 70 65 6e 73 2c 20 74 68 65 20 63 61 6c 6c 69  ppens, the calli
aeb0: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  ng function must
aec0: 20 6e 6f 74 20 74 72 79 20 74 6f 20 63 61 6c 6c   not try to call
aed0: 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72   .** [sqlite3_fr
aee0: 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79 2e 20  ee()] directly. 
aef0: 20 4f 6e 6c 79 20 5b 73 71 6c 69 74 65 33 5f 66   Only [sqlite3_f
af00: 72 65 65 5f 74 61 62 6c 65 28 29 5d 20 69 73 20  ree_table()] is 
af10: 61 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 65 20  able to release 
af20: 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 70  .** the memory p
af30: 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66 65  roperly and safe
af40: 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ly..**.** The re
af50: 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
af60: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
af70: 65 20 73 61 6d 65 20 61 73 20 66 72 6f 6d 20 5b  e same as from [
af80: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 2e  sqlite3_exec()].
af90: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
afa0: 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  nt sqlite3_get_t
afb0: 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 2a  able(.  sqlite3*
afc0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
afd0: 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61  * An open databa
afe0: 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  se */.  const ch
aff0: 61 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 2f  ar *sql,       /
b000: 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 78 65 63  * SQL to be exec
b010: 75 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  uted */.  char *
b020: 2a 2a 72 65 73 75 6c 74 70 2c 20 20 20 20 20 20  **resultp,      
b030: 20 2f 2a 20 52 65 73 75 6c 74 20 77 72 69 74 74   /* Result writt
b040: 65 6e 20 74 6f 20 61 20 63 68 61 72 20 2a 5b 5d  en to a char *[]
b050: 20 20 74 68 61 74 20 74 68 69 73 20 70 6f 69 6e    that this poin
b060: 74 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 2a  ts to */.  int *
b070: 6e 72 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20  nrow,           
b080: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
b090: 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69 74 74  esult rows writt
b0a0: 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  en here */.  int
b0b0: 20 2a 6e 63 6f 6c 75 6d 6e 2c 20 20 20 20 20 20   *ncolumn,      
b0c0: 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
b0d0: 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
b0e0: 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
b0f0: 20 20 63 68 61 72 20 2a 2a 65 72 72 6d 73 67 20    char **errmsg 
b100: 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
b110: 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65  r msg written he
b120: 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  re */.);.SQLITE_
b130: 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
b140: 5f 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72  _free_table(char
b150: 20 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a   **result);../*.
b160: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72  ** CAPI3REF: For
b170: 6d 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72  matted String Pr
b180: 69 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73  inting Functions
b190: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
b1a0: 74 69 6e 65 73 20 61 72 65 20 77 6f 72 6b 61 6c  tines are workal
b1b0: 69 6b 65 73 20 6f 66 20 74 68 65 20 22 70 72 69  ikes of the "pri
b1c0: 6e 74 66 28 29 22 20 66 61 6d 69 6c 79 20 6f 66  ntf()" family of
b1d0: 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 66 72   functions.** fr
b1e0: 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  om the standard 
b1f0: 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  C library..**.**
b200: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 70 72   The sqlite3_mpr
b210: 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74  intf() and sqlit
b220: 65 33 5f 76 6d 70 72 69 6e 74 66 28 29 20 72 6f  e3_vmprintf() ro
b230: 75 74 69 6e 65 73 20 77 72 69 74 65 20 74 68 65  utines write the
b240: 69 72 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e  ir.** results in
b250: 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
b260: 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
b270: 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54  _malloc()]..** T
b280: 68 65 20 73 74 72 69 6e 67 73 20 72 65 74 75 72  he strings retur
b290: 6e 65 64 20 62 79 20 74 68 65 73 65 20 74 77 6f  ned by these two
b2a0: 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64   routines should
b2b0: 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20   be.** released 
b2c0: 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  by [sqlite3_free
b2d0: 28 29 5d 2e 20 20 42 6f 74 68 20 72 6f 75 74 69  ()].  Both routi
b2e0: 6e 65 73 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  nes return a.** 
b2f0: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 20  NULL pointer if 
b300: 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
b310: 29 5d 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  )] is unable to 
b320: 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 0a  allocate enough.
b330: 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c  ** memory to hol
b340: 64 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20  d the resulting 
b350: 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e  string..**.** In
b360: 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
b370: 66 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73  f() routine is s
b380: 69 6d 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69  imilar to "snpri
b390: 6e 74 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 74  ntf()" from.** t
b3a0: 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
b3b0: 62 72 61 72 79 2e 20 20 54 68 65 20 72 65 73 75  brary.  The resu
b3c0: 6c 74 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  lt is written in
b3d0: 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72  to the.** buffer
b3e0: 20 73 75 70 70 6c 69 65 64 20 61 73 20 74 68 65   supplied as the
b3f0: 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
b400: 72 20 77 68 6f 73 65 20 73 69 7a 65 20 69 73 20  r whose size is 
b410: 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20  given by.** the 
b420: 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e  first parameter.
b430: 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20    Note that the 
b440: 6f 72 64 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20  order of the.** 
b450: 66 69 72 73 74 20 74 77 6f 20 70 61 72 61 6d 65  first two parame
b460: 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64  ters is reversed
b470: 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29   from snprintf()
b480: 2e 20 20 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a  .  This is an.**
b490: 20 68 69 73 74 6f 72 69 63 61 6c 20 61 63 63 69   historical acci
b4a0: 64 65 6e 74 20 74 68 61 74 20 63 61 6e 6e 6f 74  dent that cannot
b4b0: 20 62 65 20 66 69 78 65 64 20 77 69 74 68 6f 75   be fixed withou
b4c0: 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61  t breaking.** ba
b4d0: 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
b4e0: 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 61 6c 73  ility.  Note als
b4f0: 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 73  o that sqlite3_s
b500: 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 65 74  nprintf().** ret
b510: 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
b520: 6f 20 69 74 73 20 62 75 66 66 65 72 20 69 6e 73  o its buffer ins
b530: 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75 6d 62  tead of the numb
b540: 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74  er of.** charact
b550: 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77 72 69  ers actually wri
b560: 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 62 75  tten into the bu
b570: 66 66 65 72 2e 20 20 57 65 20 61 64 6d 69 74 20  ffer.  We admit 
b580: 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  that.** the numb
b590: 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
b5a0: 20 77 72 69 74 74 65 6e 20 77 6f 75 6c 64 20 62   written would b
b5b0: 65 20 61 20 6d 6f 72 65 20 75 73 65 66 75 6c 20  e a more useful 
b5c0: 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20  return.** value 
b5d0: 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 63 68  but we cannot ch
b5e0: 61 6e 67 65 20 74 68 65 20 69 6d 70 6c 65 6d 65  ange the impleme
b5f0: 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74  ntation of sqlit
b600: 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a  e3_snprintf().**
b610: 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 62 72 65   now without bre
b620: 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c  aking compatibil
b630: 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c 6f  ity..**.** As lo
b640: 6e 67 20 61 73 20 74 68 65 20 62 75 66 66 65 72  ng as the buffer
b650: 20 73 69 7a 65 20 69 73 20 67 72 65 61 74 65 72   size is greater
b660: 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71 6c 69   than zero, sqli
b670: 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a  te3_snprintf().*
b680: 2a 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  * guarantees tha
b690: 74 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20  t the buffer is 
b6a0: 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d  always zero-term
b6b0: 69 6e 61 74 65 64 2e 20 20 54 68 65 20 66 69 72  inated.  The fir
b6c0: 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  st.** parameter 
b6d0: 22 6e 22 20 69 73 20 74 68 65 20 74 6f 74 61 6c  "n" is the total
b6e0: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66   size of the buf
b6f0: 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 73  fer, including s
b700: 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20  pace for.** the 
b710: 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e  zero terminator.
b720: 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 65 73 74    So the longest
b730: 20 73 74 72 69 6e 67 20 74 68 61 74 20 63 61 6e   string that can
b740: 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a   be completely.*
b750: 2a 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 62  * written will b
b760: 65 20 6e 2d 31 20 63 68 61 72 61 63 74 65 72 73  e n-1 characters
b770: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
b780: 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65  utines all imple
b790: 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 69 74 69  ment some additi
b7a0: 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0a  onal formatting.
b7b0: 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 20  ** options that 
b7c0: 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20 63  are useful for c
b7d0: 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 51 4c 20  onstructing SQL 
b7e0: 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 41  statements..** A
b7f0: 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20  ll of the usual 
b800: 70 72 69 6e 74 66 20 66 6f 72 6d 61 74 74 69 6e  printf formattin
b810: 67 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e  g options apply.
b820: 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74    In addition, t
b830: 68 65 72 65 0a 2a 2a 20 69 73 20 61 72 65 20 22  here.** is are "
b840: 25 71 22 2c 20 22 25 51 22 2c 20 61 6e 64 20 22  %q", "%Q", and "
b850: 25 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a  %z" options..**.
b860: 2a 2a 20 54 68 65 20 25 71 20 6f 70 74 69 6f 6e  ** The %q option
b870: 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 73 20 69   works like %s i
b880: 6e 20 74 68 61 74 20 69 74 20 73 75 62 73 74 69  n that it substi
b890: 74 75 74 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72  tutes a null-ter
b8a0: 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e  minated.** strin
b8b0: 67 20 66 72 6f 6d 20 74 68 65 20 61 72 67 75 6d  g from the argum
b8c0: 65 6e 74 20 6c 69 73 74 2e 20 20 42 75 74 20 25  ent list.  But %
b8d0: 71 20 61 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65  q also doubles e
b8e0: 76 65 72 79 20 27 5c 27 27 20 63 68 61 72 61 63  very '\'' charac
b8f0: 74 65 72 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65  ter..** %q is de
b900: 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 69  signed for use i
b910: 6e 73 69 64 65 20 61 20 73 74 72 69 6e 67 20 6c  nside a string l
b920: 69 74 65 72 61 6c 2e 20 20 42 79 20 64 6f 75 62  iteral.  By doub
b930: 6c 69 6e 67 20 65 61 63 68 20 27 5c 27 27 0a 2a  ling each '\''.*
b940: 2a 20 63 68 61 72 61 63 74 65 72 20 69 74 20 65  * character it e
b950: 73 63 61 70 65 73 20 74 68 61 74 20 63 68 61 72  scapes that char
b960: 61 63 74 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73  acter and allows
b970: 20 69 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74   it to be insert
b980: 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73  ed into.** the s
b990: 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  tring..**.** For
b9a0: 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20 73 6f 6d   example, so som
b9b0: 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c  e string variabl
b9c0: 65 20 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20  e contains text 
b9d0: 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
b9e0: 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
b9f0: 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54  re>.**  char *zT
ba00: 65 78 74 20 3d 20 22 49 74 27 73 20 61 20 68 61  ext = "It's a ha
ba10: 70 70 79 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f  ppy day!";.** </
ba20: 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
ba30: 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20  >.**.** One can 
ba40: 75 73 65 20 74 68 69 73 20 74 65 78 74 20 69 6e  use this text in
ba50: 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
ba60: 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  t as follows:.**
ba70: 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
ba80: 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a  <pre>.**  char *
ba90: 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSQL = sqlite3_m
baa0: 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49  printf("INSERT I
bab0: 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53  NTO table VALUES
bac0: 28 27 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b  ('%q')", zText);
bad0: 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65  .**  sqlite3_exe
bae0: 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30  c(db, zSQL, 0, 0
baf0: 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65  , 0);.**  sqlite
bb00: 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a  3_free(zSQL);.**
bb10: 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75   </pre></blockqu
bb20: 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75  ote>.**.** Becau
bb30: 73 65 20 74 68 65 20 25 71 20 66 6f 72 6d 61 74  se the %q format
bb40: 20 73 74 72 69 6e 67 20 69 73 20 75 73 65 64 2c   string is used,
bb50: 20 74 68 65 20 27 5c 27 27 20 63 68 61 72 61 63   the '\'' charac
bb60: 74 65 72 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20  ter in zText.** 
bb70: 69 73 20 65 73 63 61 70 65 64 20 61 6e 64 20 74  is escaped and t
bb80: 68 65 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64  he SQL generated
bb90: 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a   is as follows:.
bba0: 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
bbb0: 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45  e><pre>.**  INSE
bbc0: 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56  RT INTO table1 V
bbd0: 41 4c 55 45 53 28 27 49 74 27 27 73 20 61 20 68  ALUES('It''s a h
bbe0: 61 70 70 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c  appy day!').** <
bbf0: 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74  /pre></blockquot
bc00: 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  e>.**.** This is
bc10: 20 63 6f 72 72 65 63 74 2e 20 20 48 61 64 20 77   correct.  Had w
bc20: 65 20 75 73 65 64 20 25 73 20 69 6e 73 74 65 61  e used %s instea
bc30: 64 20 6f 66 20 25 71 2c 20 74 68 65 20 67 65 6e  d of %q, the gen
bc40: 65 72 61 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f  erated SQL.** wo
bc50: 75 6c 64 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20  uld have looked 
bc60: 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
bc70: 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72   <blockquote><pr
bc80: 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e  e>.**  INSERT IN
bc90: 54 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53  TO table1 VALUES
bca0: 28 27 49 74 27 73 20 61 20 68 61 70 70 79 20 64  ('It's a happy d
bcb0: 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e  ay!');.** </pre>
bcc0: 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
bcd0: 0a 2a 2a 20 54 68 69 73 20 73 65 63 6f 6e 64 20  .** This second 
bce0: 65 78 61 6d 70 6c 65 20 69 73 20 61 6e 20 53 51  example is an SQ
bcf0: 4c 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 20  L syntax error. 
bd00: 20 41 73 20 61 20 67 65 6e 65 72 61 6c 20 72 75   As a general ru
bd10: 6c 65 20 79 6f 75 0a 2a 2a 20 73 68 6f 75 6c 64  le you.** should
bd20: 20 61 6c 77 61 79 73 20 75 73 65 20 25 71 20 69   always use %q i
bd30: 6e 73 74 65 61 64 20 6f 66 20 25 73 20 77 68 65  nstead of %s whe
bd40: 6e 20 69 6e 73 65 72 74 69 6e 67 20 74 65 78 74  n inserting text
bd50: 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 0a   into a string .
bd60: 2a 2a 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a  ** literal..**.*
bd70: 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20  * The %Q option 
bd80: 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78  works like %q ex
bd90: 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64  cept it also add
bda0: 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20  s single quotes 
bdb0: 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75  around.** the ou
bdc0: 74 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74  tside of the tot
bdd0: 61 6c 20 73 74 72 69 6e 67 2e 20 20 4f 72 20 69  al string.  Or i
bde0: 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  f the parameter 
bdf0: 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a  in the argument.
be00: 2a 2a 20 6c 69 73 74 20 69 73 20 61 20 4e 55 4c  ** list is a NUL
be10: 4c 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73 75  L pointer, %Q su
be20: 62 73 74 69 74 75 74 65 73 20 74 68 65 20 74 65  bstitutes the te
be30: 78 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f  xt "NULL" (witho
be40: 75 74 20 73 69 6e 67 6c 65 0a 2a 2a 20 71 75 6f  ut single.** quo
be50: 74 65 73 29 20 69 6e 20 70 6c 61 63 65 20 6f 66  tes) in place of
be60: 20 74 68 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20   the %Q option. 
be70: 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65   So, for example
be80: 2c 20 6f 6e 65 20 63 6f 75 6c 64 20 73 61 79 3a  , one could say:
be90: 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
bea0: 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61  te><pre>.**  cha
beb0: 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65  r *zSQL = sqlite
bec0: 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 52  3_mprintf("INSER
bed0: 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c  T INTO table VAL
bee0: 55 45 53 28 25 51 29 22 2c 20 7a 54 65 78 74 29  UES(%Q)", zText)
bef0: 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78  ;.**  sqlite3_ex
bf00: 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20  ec(db, zSQL, 0, 
bf10: 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74  0, 0);.**  sqlit
bf20: 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a  e3_free(zSQL);.*
bf30: 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
bf40: 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  uote>.**.** The 
bf50: 63 6f 64 65 20 61 62 6f 76 65 20 77 69 6c 6c 20  code above will 
bf60: 72 65 6e 64 65 72 20 61 20 63 6f 72 72 65 63 74  render a correct
bf70: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69   SQL statement i
bf80: 6e 20 74 68 65 20 7a 53 51 4c 0a 2a 2a 20 76 61  n the zSQL.** va
bf90: 72 69 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74  riable even if t
bfa0: 68 65 20 7a 54 65 78 74 20 76 61 72 69 61 62 6c  he zText variabl
bfb0: 65 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  e is a NULL poin
bfc0: 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22  ter..**.** The "
bfd0: 25 7a 22 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f  %z" formatting o
bfe0: 70 74 69 6f 6e 20 77 6f 72 6b 73 20 65 78 61 63  ption works exac
bff0: 74 6c 79 20 6c 69 6b 65 20 22 25 73 22 20 77 69  tly like "%s" wi
c000: 74 68 20 74 68 65 0a 2a 2a 20 61 64 64 69 74 69  th the.** additi
c010: 6f 6e 20 74 68 61 74 20 61 66 74 65 72 20 74 68  on that after th
c020: 65 20 73 74 72 69 6e 67 20 68 61 73 20 62 65 65  e string has bee
c030: 6e 20 72 65 61 64 20 61 6e 64 20 63 6f 70 69 65  n read and copie
c040: 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 72 65  d into.** the re
c050: 73 75 6c 74 2c 20 5b 73 71 6c 69 74 65 33 5f 66  sult, [sqlite3_f
c060: 72 65 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64  ree()] is called
c070: 20 6f 6e 20 74 68 65 20 69 6e 70 75 74 20 73 74   on the input st
c080: 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ring..*/.SQLITE_
c090: 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
c0a0: 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20  3_mprintf(const 
c0b0: 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49  char*,...);.SQLI
c0c0: 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
c0d0: 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f  ite3_vmprintf(co
c0e0: 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69  nst char*, va_li
c0f0: 73 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  st);.SQLITE_API 
c100: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e  char *sqlite3_sn
c110: 70 72 69 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a  printf(int,char*
c120: 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e  ,const char*, ..
c130: 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
c140: 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f  REF: Memory Allo
c150: 63 61 74 69 6f 6e 20 53 75 62 73 79 73 74 65 6d  cation Subsystem
c160: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74  .**.** The SQLit
c170: 65 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73  e core uses thes
c180: 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73  e three routines
c190: 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20   for all of its 
c1a0: 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20  own.** internal 
c1b0: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
c1c0: 6e 20 6e 65 65 64 73 2e 20 28 53 65 65 20 74 68  n needs. (See th
c1d0: 65 20 65 78 63 65 70 74 69 6f 6e 20 62 65 6c 6f  e exception belo
c1e0: 77 2e 29 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  w.).** The defau
c1f0: 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  lt implementatio
c200: 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f  n.** of the memo
c210: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
c220: 62 73 79 73 74 65 6d 20 75 73 65 73 20 74 68 65  bsystem uses the
c230: 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61 6c 6c   malloc(), reall
c240: 6f 63 28 29 0a 2a 2a 20 61 6e 64 20 66 72 65 65  oc().** and free
c250: 28 29 20 70 72 6f 76 69 64 65 64 20 62 79 20 74  () provided by t
c260: 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
c270: 62 72 61 72 79 2e 20 20 48 6f 77 65 76 65 72 2c  brary.  However,
c280: 20 69 66 20 0a 2a 2a 20 53 51 4c 69 74 65 20 69   if .** SQLite i
c290: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
c2a0: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 43 20  the following C 
c2b0: 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63  preprocessor mac
c2c0: 72 6f 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  ro.**.** <blockq
c2d0: 75 6f 74 65 3e 20 53 51 4c 49 54 45 5f 4f 4d 49  uote> SQLITE_OMI
c2e0: 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54  T_MEMORY_ALLOCAT
c2f0: 49 4f 4e 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  ION </blockquote
c300: 3e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 6f 20  >.**.** then no 
c310: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
c320: 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74  s provided for t
c330: 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 62 79  hese routines by
c340: 0a 2a 2a 20 53 51 4c 69 74 65 2e 20 20 54 68 65  .** SQLite.  The
c350: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 61   application tha
c360: 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20  t links against 
c370: 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 65 78 70  SQLite is.** exp
c380: 65 63 74 65 64 20 74 6f 20 70 72 6f 76 69 64 65  ected to provide
c390: 20 69 74 73 20 6f 77 6e 20 69 6d 70 6c 65 6d 65   its own impleme
c3a0: 6e 74 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65  ntation.  If the
c3b0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20   application.** 
c3c0: 64 6f 65 73 20 70 72 6f 76 69 64 65 20 69 74 73  does provide its
c3d0: 20 6f 77 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74   own implementat
c3e0: 69 6f 6e 20 66 6f 72 20 74 68 65 73 65 20 72 6f  ion for these ro
c3f0: 75 74 69 6e 65 73 2c 20 74 68 65 6e 0a 2a 2a 20  utines, then.** 
c400: 69 74 20 6d 75 73 74 20 61 6c 73 6f 20 70 72 6f  it must also pro
c410: 76 69 64 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e  vide an implemen
c420: 74 61 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 5b  tations for.** [
c430: 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61  sqlite3_memory_a
c440: 6c 61 72 6d 28 29 5d 2c 20 5b 73 71 6c 69 74 65  larm()], [sqlite
c450: 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d  3_memory_used()]
c460: 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  , and.** [sqlite
c470: 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
c480: 65 72 28 29 5d 2e 20 20 54 68 65 20 61 6c 74 65  er()].  The alte
c490: 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e  rnative implemen
c4a0: 74 61 74 69 6f 6e 73 0a 2a 2a 20 66 6f 72 20 74  tations.** for t
c4b0: 68 65 73 65 20 6c 61 73 74 20 74 68 72 65 65 20  hese last three 
c4c0: 72 6f 75 74 69 6e 65 73 20 6e 65 65 64 20 6e 6f  routines need no
c4d0: 74 20 61 63 74 75 61 6c 6c 79 20 77 6f 72 6b 2c  t actually work,
c4e0: 20 62 75 74 0a 2a 2a 20 73 74 75 62 20 66 75 6e   but.** stub fun
c4f0: 63 74 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20  ctions at least 
c500: 61 72 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74  are needed to st
c510: 61 74 69 73 66 79 20 74 68 65 20 6c 69 6e 6b 65  atisfy the linke
c520: 72 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 6e 65 76  r..** SQLite nev
c530: 65 72 20 63 61 6c 6c 73 20 5b 73 71 6c 69 74 65  er calls [sqlite
c540: 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
c550: 65 72 28 29 5d 20 69 74 73 65 6c 66 2c 20 62 75  er()] itself, bu
c560: 74 0a 2a 2a 20 74 68 65 20 73 79 6d 62 6f 6c 20  t.** the symbol 
c570: 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61  is included in a
c580: 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f   table as part o
c590: 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  f the.** [sqlite
c5a0: 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
c5b0: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 20  ()] interface.  
c5c0: 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  The.** [sqlite3_
c5d0: 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 29 5d 20  memory_alarm()] 
c5e0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  and [sqlite3_mem
c5f0: 6f 72 79 5f 75 73 65 64 28 29 5d 20 69 6e 74 65  ory_used()] inte
c600: 72 66 61 63 65 73 0a 2a 2a 20 61 72 65 20 63 61  rfaces.** are ca
c610: 6c 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  lled by [sqlite3
c620: 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
c630: 28 29 5d 20 61 6e 64 20 77 6f 72 6b 69 6e 67 20  ()] and working 
c640: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a  implementations.
c650: 2a 2a 20 6f 66 20 62 6f 74 68 20 72 6f 75 74 69  ** of both routi
c660: 6e 65 73 20 6d 75 73 74 20 62 65 20 70 72 6f 76  nes must be prov
c670: 69 64 65 64 20 69 66 20 5b 73 71 6c 69 74 65 33  ided if [sqlite3
c680: 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
c690: 28 29 5d 0a 2a 2a 20 69 73 20 74 6f 20 6f 70 65  ()].** is to ope
c6a0: 72 61 74 65 20 63 6f 72 72 65 63 74 6c 79 2e 0a  rate correctly..
c6b0: 2a 2a 0a 2a 2a 20 3c 62 3e 45 78 63 65 70 74 69  **.** <b>Excepti
c6c0: 6f 6e 3a 3c 2f 62 3e 20 54 68 65 20 77 69 6e 64  on:</b> The wind
c6d0: 6f 77 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65  ows OS interface
c6e0: 20 6c 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20   layer calls.** 
c6f0: 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f  the system mallo
c700: 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 64  c() and free() d
c710: 69 72 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e  irectly when con
c720: 76 65 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e  verting.** filen
c730: 61 6d 65 73 20 62 65 74 77 65 65 6e 20 74 68 65  ames between the
c740: 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20   UTF-8 encoding 
c750: 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a  used by SQLite.*
c760: 2a 20 61 6e 64 20 77 68 61 74 65 76 65 72 20 66  * and whatever f
c770: 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67  ilename encoding
c780: 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
c790: 70 61 72 74 69 63 75 6c 61 72 20 77 69 6e 64 6f  particular windo
c7a0: 77 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69  ws.** installati
c7b0: 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f  on.  Memory allo
c7c0: 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72  cation errors ar
c7d0: 65 20 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a  e detected, but.
c7e0: 2a 2a 20 74 68 65 79 20 61 72 65 20 72 65 70 6f  ** they are repo
c7f0: 72 74 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51  rted back as [SQ
c800: 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f  LITE_CANTOPEN] o
c810: 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45  r.** [SQLITE_IOE
c820: 52 52 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20  RR] rather than 
c830: 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a  [SQLITE_NOMEM]..
c840: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
c850: 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  id *sqlite3_mall
c860: 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  oc(int);.SQLITE_
c870: 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
c880: 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c  3_realloc(void*,
c890: 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50   int);.SQLITE_AP
c8a0: 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66  I void sqlite3_f
c8b0: 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a  ree(void*);../*.
c8c0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65 6d  ** CAPI3REF: Mem
c8d0: 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72 20 53 74  ory Allocator St
c8e0: 61 74 69 73 74 69 63 73 0a 2a 2a 0a 2a 2a 20 49  atistics.**.** I
c8f0: 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 74 68  n addition to th
c900: 65 20 62 61 73 69 63 20 74 68 72 65 65 20 61 6c  e basic three al
c910: 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  location routine
c920: 73 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d  s .** [sqlite3_m
c930: 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74  alloc()], [sqlit
c940: 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 20  e3_free()], and 
c950: 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
c960: 28 29 5d 2c 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f  ()],.** the memo
c970: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
c980: 62 73 79 73 74 65 6d 20 69 6e 63 6c 75 64 65 64  bsystem included
c990: 20 77 69 74 68 20 74 68 65 20 53 51 4c 69 74 65   with the SQLite
c9a0: 0a 2a 2a 20 73 6f 75 72 63 65 73 20 70 72 6f 76  .** sources prov
c9b0: 69 64 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  ides the interfa
c9c0: 63 65 73 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e  ces shown below.
c9d0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
c9e0: 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 72 6f   of these two ro
c9f0: 75 74 69 6e 65 73 20 72 65 74 75 72 6e 73 20 74  utines returns t
ca00: 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  he amount of mem
ca10: 6f 72 79 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c  ory .** currentl
ca20: 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 28 6d  y outstanding (m
ca30: 61 6c 6c 6f 63 65 64 20 62 75 74 20 6e 6f 74 20  alloced but not 
ca40: 66 72 65 65 64 29 2e 20 20 54 68 65 20 73 65 63  freed).  The sec
ca50: 6f 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74  ond.** returns t
ca60: 68 65 20 6c 61 72 67 65 73 74 20 69 6e 73 74 61  he largest insta
ca70: 6e 74 61 6e 65 6f 75 73 20 61 6d 6f 75 6e 74 20  ntaneous amount 
ca80: 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a  of outstanding.*
ca90: 2a 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 20 68  * memory.  The h
caa0: 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 69 73  ighwater mark is
cab0: 20 72 65 73 65 74 20 69 66 20 74 68 65 20 61 72   reset if the ar
cac0: 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 72 75  gument is.** tru
cad0: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70  e..**.** The imp
cae0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
caf0: 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e  hese routines in
cb00: 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65   the SQLite core
cb10: 0a 2a 2a 20 69 73 20 6f 6d 69 74 74 65 64 20 69  .** is omitted i
cb20: 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
cb30: 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  n is compiled wi
cb40: 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  th the.** SQLITE
cb50: 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c  _OMIT_MEMORY_ALL
cb60: 4f 43 41 54 49 4f 4e 20 6d 61 63 72 6f 20 64 65  OCATION macro de
cb70: 66 69 6e 65 64 2e 20 20 49 6e 20 74 68 61 74 20  fined.  In that 
cb80: 63 61 73 65 2c 0a 2a 2a 20 74 68 65 20 61 70 70  case,.** the app
cb90: 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 6c 69  lication that li
cba0: 6e 6b 73 20 53 51 4c 69 74 65 20 6d 75 73 74 20  nks SQLite must 
cbb0: 70 72 6f 76 69 64 65 20 69 74 73 20 6f 77 6e 0a  provide its own.
cbc0: 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69  ** alternative i
cbd0: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20  mplementation.  
cbe0: 53 65 65 20 74 68 65 20 64 6f 63 75 6d 65 6e 74  See the document
cbf0: 61 74 69 6f 6e 20 6f 6e 0a 2a 2a 20 5b 73 71 6c  ation on.** [sql
cc00: 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66  ite3_malloc()] f
cc10: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
cc20: 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  formation..*/.SQ
cc30: 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
cc40: 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d  _int64 sqlite3_m
cc50: 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29  emory_used(void)
cc60: 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ;.SQLITE_API sql
cc70: 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
cc80: 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
cc90: 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61  ter(int resetFla
cca0: 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  g);../*.** CAPI3
ccb0: 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f  REF: Memory Allo
ccc0: 63 61 74 69 6f 6e 20 41 6c 61 72 6d 73 0a 2a 2a  cation Alarms.**
ccd0: 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
cce0: 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 5d 20 72  _memory_alarm] r
ccf0: 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
cd00: 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 20  o register.** a 
cd10: 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 6d 65 6d 6f  callback on memo
cd20: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 76  ry allocation ev
cd30: 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ents..**.** This
cd40: 20 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65   routine registe
cd50: 72 73 20 6f 72 20 63 6c 65 61 72 73 20 61 20 63  rs or clears a c
cd60: 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 66 69  allbacks that fi
cd70: 72 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  res when.** the 
cd80: 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
cd90: 20 61 6c 6c 6f 63 61 74 65 64 20 65 78 63 65 65   allocated excee
cda0: 64 73 20 69 54 68 72 65 73 68 6f 6c 64 2e 20 20  ds iThreshold.  
cdb0: 4f 6e 6c 79 0a 2a 2a 20 61 20 73 69 6e 67 6c 65  Only.** a single
cdc0: 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65   callback can be
cdd0: 20 72 65 67 69 73 74 65 72 65 64 20 61 74 20 61   registered at a
cde0: 20 74 69 6d 65 2e 20 20 45 61 63 68 20 63 61 6c   time.  Each cal
cdf0: 6c 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33  l.** to [sqlite3
ce00: 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 29 5d  _memory_alarm()]
ce10: 20 6f 76 65 72 77 72 69 74 65 73 20 74 68 65 20   overwrites the 
ce20: 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 62 61 63  previous callbac
ce30: 6b 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61  k..** The callba
ce40: 63 6b 20 69 73 20 64 69 73 61 62 6c 65 64 20 62  ck is disabled b
ce50: 79 20 73 65 74 74 69 6e 67 20 78 43 61 6c 6c 62  y setting xCallb
ce60: 61 63 6b 20 74 6f 20 61 20 4e 55 4c 4c 0a 2a 2a  ack to a NULL.**
ce70: 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 0a 2a 2a   pointer..** .**
ce80: 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 20   The parameters 
ce90: 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
cea0: 61 72 65 20 74 68 65 20 70 41 72 67 20 76 61 6c  are the pArg val
ceb0: 75 65 2c 20 74 68 65 20 0a 2a 2a 20 61 6d 6f 75  ue, the .** amou
cec0: 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72  nt of memory cur
ced0: 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2c 20 61  rently in use, a
cee0: 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  nd the size of t
cef0: 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  he.** allocation
cf00: 20 74 68 61 74 20 70 72 6f 76 6f 6b 65 64 20 74   that provoked t
cf10: 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 54 68  he callback.  Th
cf20: 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a  e callback will.
cf30: 2a 2a 20 70 72 65 73 75 6d 61 62 6c 79 20 69 6e  ** presumably in
cf40: 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 66 72  voke [sqlite3_fr
cf50: 65 65 28 29 5d 20 74 6f 20 66 72 65 65 20 75 70  ee()] to free up
cf60: 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 2e 0a 2a   memory space..*
cf70: 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 6d  * The callback m
cf80: 61 79 20 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74  ay invoke [sqlit
cf90: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20  e3_malloc()] or 
cfa0: 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
cfb0: 28 29 5d 0a 2a 2a 20 62 75 74 20 69 66 20 69 74  ()].** but if it
cfc0: 20 64 6f 65 73 2c 20 6e 6f 20 61 64 64 69 74 69   does, no additi
cfd0: 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 77  onal callbacks w
cfe0: 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 62  ill be invoked b
cff0: 79 0a 2a 2a 20 74 68 65 20 72 65 63 75 72 73 69  y.** the recursi
d000: 76 65 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20  ve calls..**.** 
d010: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66  The [sqlite3_sof
d020: 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 5d 20  t_heap_limit()] 
d030: 69 6e 74 65 72 66 61 63 65 20 77 6f 72 6b 73 20  interface works 
d040: 62 79 20 72 65 67 69 73 74 65 72 69 6e 67 0a 2a  by registering.*
d050: 2a 20 61 20 6d 65 6d 6f 72 79 20 61 6c 61 72 6d  * a memory alarm
d060: 20 61 74 20 74 68 65 20 73 6f 66 74 20 68 65 61   at the soft hea
d070: 70 20 6c 69 6d 69 74 20 61 6e 64 20 69 6e 76 6f  p limit and invo
d080: 6b 69 6e 67 20 0a 2a 2a 20 5b 73 71 6c 69 74 65  king .** [sqlite
d090: 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
d0a0: 28 29 5d 20 69 6e 20 74 68 65 20 61 6c 61 72 6d  ()] in the alarm
d0b0: 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 70 70 6c   callback.  Appl
d0c0: 69 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 6f 67 72  ication.** progr
d0d0: 61 6d 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61  ams should not a
d0e0: 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68  ttempt to use th
d0f0: 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  e [sqlite3_memor
d100: 79 5f 61 6c 61 72 6d 28 29 5d 0a 2a 2a 20 69 6e  y_alarm()].** in
d110: 74 65 72 66 61 63 65 20 62 65 63 61 75 73 65 20  terface because 
d120: 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 69 6e  doing so will in
d130: 74 65 72 66 65 72 65 20 77 69 74 68 20 74 68 65  terfere with the
d140: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66  .** [sqlite3_sof
d150: 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 5d 20  t_heap_limit()] 
d160: 6d 6f 64 75 6c 65 2e 20 20 54 68 69 73 20 69 6e  module.  This in
d170: 74 65 72 66 61 63 65 20 69 73 20 65 78 70 6f 73  terface is expos
d180: 65 64 0a 2a 2a 20 6f 6e 6c 79 20 73 6f 20 74 68  ed.** only so th
d190: 61 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  at applications 
d1a0: 63 61 6e 20 70 72 6f 76 69 64 65 20 74 68 65 69  can provide thei
d1b0: 72 20 6f 77 6e 0a 2a 2a 20 61 6c 74 65 72 6e 61  r own.** alterna
d1c0: 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tive implementat
d1d0: 69 6f 6e 20 77 68 65 6e 20 74 68 65 20 53 51 4c  ion when the SQL
d1e0: 69 74 65 20 63 6f 72 65 20 69 73 0a 2a 2a 20 63  ite core is.** c
d1f0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c  ompiled with SQL
d200: 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f  ITE_OMIT_MEMORY_
d210: 41 4c 4c 4f 43 41 54 49 4f 4e 2e 0a 2a 2f 0a 53  ALLOCATION..*/.S
d220: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
d230: 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61  lite3_memory_ala
d240: 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c  rm(.  void(*xCal
d250: 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72  lback)(void *pAr
d260: 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  g, sqlite3_int64
d270: 20 75 73 65 64 2c 20 69 6e 74 20 4e 29 2c 0a 20   used, int N),. 
d280: 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73   void *pArg,.  s
d290: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68  qlite3_int64 iTh
d2a0: 72 65 73 68 6f 6c 64 0a 29 3b 0a 0a 0a 2f 2a 0a  reshold.);.../*.
d2b0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d  ** CAPI3REF: Com
d2c0: 70 69 6c 65 2d 54 69 6d 65 20 41 75 74 68 6f 72  pile-Time Author
d2d0: 69 7a 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b  ization Callback
d2e0: 73 0a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s.***.** This ro
d2f0: 75 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20  utine registers 
d300: 61 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  a authorizer cal
d310: 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 53  lback with the S
d320: 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20  QLite library.  
d330: 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a  .** The authoriz
d340: 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  er callback is i
d350: 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 73 74  nvoked as SQL st
d360: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 62 65 69  atements are bei
d370: 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 62  ng compiled.** b
d380: 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  y [sqlite3_prepa
d390: 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72  re()] or its var
d3a0: 69 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 5f 70  iants [sqlite3_p
d3b0: 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a 2a 2a  repare_v2()],.**
d3c0: 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
d3d0: 65 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  e16()] and [sqli
d3e0: 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
d3f0: 28 29 5d 2e 20 20 41 74 20 76 61 72 69 6f 75 73  ()].  At various
d400: 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75 72 69 6e  .** points durin
d410: 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f  g the compilatio
d420: 6e 20 70 72 6f 63 65 73 73 2c 20 61 73 20 6c 6f  n process, as lo
d430: 67 69 63 20 69 73 20 62 65 69 6e 67 20 63 72 65  gic is being cre
d440: 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65 72 66 6f  ated.** to perfo
d450: 72 6d 20 76 61 72 69 6f 75 73 20 61 63 74 69 6f  rm various actio
d460: 6e 73 2c 20 74 68 65 20 61 75 74 68 6f 72 69 7a  ns, the authoriz
d470: 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  er callback is i
d480: 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 73 65 65  nvoked to.** see
d490: 20 69 66 20 74 68 6f 73 65 20 61 63 74 69 6f 6e   if those action
d4a0: 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 20 20  s are allowed.  
d4b0: 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  The authorizer c
d4c0: 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 0a 2a  allback should.*
d4d0: 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * return SQLITE_
d4e0: 4f 4b 20 74 6f 20 61 6c 6c 6f 77 20 74 68 65 20  OK to allow the 
d4f0: 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49 54 45 5f  action, [SQLITE_
d500: 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69 73 61 6c  IGNORE] to disal
d510: 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70 65 63 69  low the.** speci
d520: 66 69 63 20 61 63 74 69 6f 6e 20 62 75 74 20 61  fic action but a
d530: 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20 73 74 61  llow the SQL sta
d540: 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e 74 69 6e  tement to contin
d550: 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70  ue to be.** comp
d560: 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c 49 54 45  iled, or [SQLITE
d570: 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75 73 65 20  _DENY] to cause 
d580: 74 68 65 20 65 6e 74 69 72 65 20 53 51 4c 20 73  the entire SQL s
d590: 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a  tatement to be.*
d5a0: 2a 20 72 65 6a 65 63 74 65 64 20 77 69 74 68 20  * rejected with 
d5b0: 61 6e 20 65 72 72 6f 72 2e 20 20 0a 2a 2a 0a 2a  an error.  .**.*
d5c0: 2a 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  * Depending on t
d5d0: 68 65 20 61 63 74 69 6f 6e 2c 20 74 68 65 20 5b  he action, the [
d5e0: 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 61  SQLITE_IGNORE] a
d5f0: 6e 64 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d  nd [SQLITE_DENY]
d600: 20 72 65 74 75 72 6e 0a 2a 2a 20 63 6f 64 65 73   return.** codes
d610: 20 6d 69 67 68 74 20 6d 65 61 6e 20 73 6f 6d 65   might mean some
d620: 74 68 69 6e 67 20 64 69 66 66 65 72 65 6e 74 20  thing different 
d630: 6f 72 20 74 68 65 79 20 6d 69 67 68 74 20 6d 65  or they might me
d640: 61 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74  an the same.** t
d650: 68 69 6e 67 2e 20 20 49 66 20 74 68 65 20 61 63  hing.  If the ac
d660: 74 69 6f 6e 20 69 73 2c 20 66 6f 72 20 65 78 61  tion is, for exa
d670: 6d 70 6c 65 2c 20 74 6f 20 70 65 72 66 6f 72 6d  mple, to perform
d680: 20 61 20 64 65 6c 65 74 65 20 6f 70 65 72 74 69   a delete operti
d690: 6f 6e 2c 0a 2a 2a 20 74 68 65 6e 20 5b 53 51 4c  on,.** then [SQL
d6a0: 49 54 45 5f 49 47 4e 4f 52 45 5d 20 61 6e 64 20  ITE_IGNORE] and 
d6b0: 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20 62 6f  [SQLITE_DENY] bo
d6c0: 74 68 20 63 61 75 73 65 20 74 68 65 20 73 74 61  th cause the sta
d6d0: 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61 74 69  tement compilati
d6e0: 6f 6e 0a 2a 2a 20 74 6f 20 66 61 69 6c 20 77 69  on.** to fail wi
d6f0: 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 42 75  th an error.  Bu
d700: 74 20 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20  t if the action 
d710: 69 73 20 74 6f 20 72 65 61 64 20 61 20 73 70 65  is to read a spe
d720: 63 69 66 69 63 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  cific column.** 
d730: 66 72 6f 6d 20 61 20 73 70 65 63 69 66 69 63 20  from a specific 
d740: 74 61 62 6c 65 2c 20 74 68 65 6e 20 5b 53 51 4c  table, then [SQL
d750: 49 54 45 5f 44 45 4e 59 5d 20 77 69 6c 6c 20 63  ITE_DENY] will c
d760: 61 75 73 65 20 74 68 65 20 65 6e 74 69 72 65 0a  ause the entire.
d770: 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  ** statement to 
d780: 66 61 69 6c 20 62 75 74 20 5b 53 51 4c 49 54 45  fail but [SQLITE
d790: 5f 49 47 4e 4f 52 45 5d 20 77 69 6c 6c 20 63 61  _IGNORE] will ca
d7a0: 75 73 65 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65  use a NULL value
d7b0: 20 74 6f 20 62 65 0a 2a 2a 20 72 65 61 64 20 69   to be.** read i
d7c0: 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 61 63  nstead of the ac
d7d0: 74 75 61 6c 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  tual column valu
d7e0: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  e..**.** The fir
d7f0: 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
d800: 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  the authorizer c
d810: 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70  allback is a cop
d820: 79 20 6f 66 0a 2a 2a 20 74 68 65 20 74 68 69 72  y of.** the thir
d830: 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  d parameter to t
d840: 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  he sqlite3_set_a
d850: 75 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65  uthorizer() inte
d860: 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 73 65  rface..** The se
d870: 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74  cond parameter t
d880: 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  o the callback i
d890: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a  s an integer .**
d8a0: 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59 20 7c 20   [SQLITE_COPY | 
d8b0: 61 63 74 69 6f 6e 20 63 6f 64 65 5d 20 74 68 61  action code] tha
d8c0: 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
d8d0: 70 61 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f  particular actio
d8e0: 6e 0a 2a 2a 20 74 6f 20 62 65 20 61 75 74 68 6f  n.** to be autho
d8f0: 72 69 7a 65 64 2e 20 20 54 68 65 20 61 76 61 69  rized.  The avai
d900: 6c 61 62 6c 65 20 61 63 74 69 6f 6e 20 63 6f 64  lable action cod
d910: 65 73 20 61 72 65 0a 2a 2a 20 5b 53 51 4c 49 54  es are.** [SQLIT
d920: 45 5f 43 4f 50 59 20 7c 20 64 6f 63 75 6d 65 6e  E_COPY | documen
d930: 74 65 64 20 73 65 70 61 72 61 74 65 6c 79 5d 2e  ted separately].
d940: 20 20 54 68 65 20 74 68 69 72 64 20 74 68 72 6f    The third thro
d950: 75 67 68 20 73 69 78 74 68 0a 2a 2a 20 70 61 72  ugh sixth.** par
d960: 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65 20 63  ameters to the c
d970: 61 6c 6c 62 61 63 6b 20 61 72 65 20 73 74 72 69  allback are stri
d980: 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ngs that contain
d990: 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64   additional.** d
d9a0: 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74 68 65  etails about the
d9b0: 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75   action to be au
d9c0: 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20  thorized..**.** 
d9d0: 41 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73  An authorizer is
d9e0: 20 75 73 65 64 20 77 68 65 6e 20 70 72 65 70 61   used when prepa
d9f0: 72 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65  ring SQL stateme
da00: 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e 74 72  nts from an untr
da10: 75 73 74 65 64 0a 2a 2a 20 73 6f 75 72 63 65 2c  usted.** source,
da20: 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
da30: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
da40: 74 73 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 6f  ts do not try to
da50: 20 61 63 63 65 73 73 20 64 61 74 61 0a 2a 2a 20   access data.** 
da60: 74 68 61 74 20 74 68 65 79 20 61 72 65 20 6e 6f  that they are no
da70: 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65  t allowed to see
da80: 2c 20 6f 72 20 74 68 61 74 20 74 68 65 79 20 64  , or that they d
da90: 6f 20 6e 6f 74 20 74 72 79 20 74 6f 0a 2a 2a 20  o not try to.** 
daa0: 65 78 65 63 75 74 65 20 6d 61 6c 69 63 69 6f 75  execute maliciou
dab0: 73 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61  s statements tha
dac0: 74 20 64 61 6d 61 67 65 20 74 68 65 20 64 61 74  t damage the dat
dad0: 61 62 61 73 65 2e 20 20 46 6f 72 0a 2a 2a 20 65  abase.  For.** e
dae0: 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69  xample, an appli
daf0: 63 61 74 69 6f 6e 20 6d 61 79 20 61 6c 6c 6f 77  cation may allow
db00: 20 61 20 75 73 65 72 20 74 6f 20 65 6e 74 65 72   a user to enter
db10: 20 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 53 51   arbitrary.** SQ
db20: 4c 20 71 75 65 72 69 65 73 20 66 6f 72 20 65 76  L queries for ev
db30: 61 6c 75 61 74 69 6f 6e 20 62 79 20 61 20 64 61  aluation by a da
db40: 74 61 62 61 73 65 2e 20 20 42 75 74 20 74 68 65  tabase.  But the
db50: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65   application doe
db60: 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 68  s.** not want th
db70: 65 20 75 73 65 72 20 74 6f 20 62 65 20 61 62 6c  e user to be abl
db80: 65 20 74 6f 20 6d 61 6b 65 20 61 72 62 69 74 72  e to make arbitr
db90: 61 72 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74  ary changes to t
dba0: 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  he.** database. 
dbb0: 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 63   An authorizer c
dbc0: 6f 75 6c 64 20 74 68 65 6e 20 62 65 20 70 75 74  ould then be put
dbd0: 20 69 6e 20 70 6c 61 63 65 20 77 68 69 6c 65 20   in place while 
dbe0: 74 68 65 0a 2a 2a 20 75 73 65 72 2d 65 6e 74 65  the.** user-ente
dbf0: 72 65 64 20 53 51 4c 20 69 73 20 62 65 69 6e 67  red SQL is being
dc00: 20 70 72 65 70 61 72 65 64 20 74 68 61 74 20 64   prepared that d
dc10: 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74 68  isallows everyth
dc20: 69 6e 67 0a 2a 2a 20 65 78 63 65 70 74 20 53 45  ing.** except SE
dc30: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  LECT statements.
dc40: 20 20 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 61 20    .**.** Only a 
dc50: 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69 7a 65  single authorize
dc60: 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c 61 63  r can be in plac
dc70: 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20  e on a database 
dc80: 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 74  connection.** at
dc90: 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68 20 63   a time.  Each c
dca0: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  all to sqlite3_s
dcb0: 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 6f 76  et_authorizer ov
dcc0: 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a 20 70  errides the.** p
dcd0: 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20 20 41  revious call.  A
dce0: 20 4e 55 4c 4c 20 61 75 74 68 6f 72 69 7a 65 72   NULL authorizer
dcf0: 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 61   means that no a
dd00: 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20  uthorization.** 
dd10: 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
dd20: 6b 65 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c  ked.  The defaul
dd30: 74 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20  t authorizer is 
dd40: 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  NULL..**.** Note
dd50: 20 74 68 61 74 20 74 68 65 20 61 75 74 68 6f 72   that the author
dd60: 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73  izer callback is
dd70: 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 64 75   invoked only du
dd80: 72 69 6e 67 20 0a 2a 2a 20 5b 73 71 6c 69 74 65  ring .** [sqlite
dd90: 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20  3_prepare()] or 
dda0: 69 74 73 20 76 61 72 69 61 6e 74 73 2e 20 20 41  its variants.  A
ddb0: 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 69 73 20  uthorization is 
ddc0: 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64  not.** performed
ddd0: 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e   during statemen
dde0: 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 69 6e 20  t evaluation in 
ddf0: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
de00: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
de10: 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  int sqlite3_set_
de20: 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71  authorizer(.  sq
de30: 6c 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a  lite3*,.  int (*
de40: 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
de50: 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
de60: 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
de70: 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
de80: 29 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72  ),.  void *pUser
de90: 44 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  Data.);../*.** C
dea0: 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69  API3REF: Authori
deb0: 7a 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73  zer Return Codes
dec0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  .**.** The [sqli
ded0: 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
dee0: 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20  er | authorizer 
def0: 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
df00: 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 72  n] must.** retur
df10: 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  n either [SQLITE
df20: 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74  _OK] or one of t
df30: 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 6e  hese two constan
df40: 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74  ts in order.** t
df50: 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 20  o signal SQLite 
df60: 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
df70: 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72  he action is per
df80: 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 65  mitted.  See the
df90: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 74  .** [sqlite3_set
dfa0: 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75  _authorizer | au
dfb0: 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 6e  thorizer documen
dfc0: 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 69  tation] for addi
dfd0: 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  tional.** inform
dfe0: 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ation..*/.#defin
dff0: 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 20  e SQLITE_DENY   
e000: 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 65  1   /* Abort the
e010: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77   SQL statement w
e020: 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ith an error */.
e030: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
e040: 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f 6e  GNORE 2   /* Don
e050: 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 2c  't allow access,
e060: 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 72   but don't gener
e070: 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ate an error */.
e080: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
e090: 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 69   Authorizer Acti
e0a0: 6f 6e 20 43 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 54  on Codes.**.** T
e0b0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  he [sqlite3_set_
e0c0: 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20 69 6e  authorizer()] in
e0d0: 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72  terface register
e0e0: 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  s a callback fun
e0f0: 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 69 73  ction.** that is
e100: 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 75 74 68   invoked to auth
e110: 6f 72 69 7a 65 72 20 63 65 72 74 61 69 6e 20 53  orizer certain S
e120: 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 63 74  QL statement act
e130: 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 73 65  ions.  The.** se
e140: 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74  cond parameter t
e150: 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  o the callback i
e160: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64  s an integer cod
e170: 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65 73  e that specifies
e180: 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f 6e 20  .** what action 
e190: 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69  is being authori
e1a0: 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 65 20  zed.  These are 
e1b0: 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 74 69  the integer acti
e1c0: 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a 2a 2a  on codes that.**
e1d0: 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
e1e0: 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 65 20  callback may be 
e1f0: 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  passed..**.** Th
e200: 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20  ese action code 
e210: 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 20 77  values signify w
e220: 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 65 72  hat kind of oper
e230: 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 0a  ation is to be .
e240: 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20  ** authorized.  
e250: 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20  The 3rd and 4th 
e260: 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
e270: 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
e280: 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63  callback.** func
e290: 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 72  tion will be par
e2a0: 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20  ameters or NULL 
e2b0: 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69  depending on whi
e2c0: 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 63  ch of these.** c
e2d0: 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73 20  odes is used as 
e2e0: 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
e2f0: 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 70  eter.  The 5th p
e300: 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 0a  arameter to the.
e310: 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  ** authorizer ca
e320: 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 61  llback is the na
e330: 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
e340: 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d  se ("main", "tem
e350: 70 22 2c 20 0a 2a 2a 20 65 74 63 2e 29 20 69 66  p", .** etc.) if
e360: 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68   applicable.  Th
e370: 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 6th parameter 
e380: 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  to the authorize
e390: 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73  r callback.** is
e3a0: 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
e3b0: 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67   inner-most trig
e3c0: 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74  ger or view that
e3d0: 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
e3e0: 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73  for.** the acces
e3f0: 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c  s attempt or NUL
e400: 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73  L if this access
e410: 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65   attempt is dire
e420: 63 74 6c 79 20 66 72 6f 6d 20 0a 2a 2a 20 74 6f  ctly from .** to
e430: 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65  p-level SQL code
e440: 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..*/./**********
e450: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e460: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e470: 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  * 3rd **********
e480: 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ** 4th *********
e490: 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  **/.#define SQLI
e4a0: 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 20  TE_CREATE_INDEX 
e4b0: 20 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20           1   /* 
e4c0: 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20  Index Name      
e4d0: 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
e4e0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
e4f0: 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 20 20  E_CREATE_TABLE  
e500: 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 54          2   /* T
e510: 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
e520: 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
e530: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
e540: 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44  _CREATE_TEMP_IND
e550: 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20 49 6e  EX     3   /* In
e560: 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61  dex Name      Ta
e570: 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
e580: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
e590: 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c  CREATE_TEMP_TABL
e5a0: 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54 61 62  E     4   /* Tab
e5b0: 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c  le Name      NUL
e5c0: 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
e5d0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
e5e0: 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47  REATE_TEMP_TRIGG
e5f0: 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 69 67  ER   5   /* Trig
e600: 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
e610: 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
e620: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
e630: 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 20 20  EATE_TEMP_VIEW  
e640: 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 77 20      6   /* View 
e650: 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20  Name       NULL 
e660: 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
e670: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45  efine SQLITE_CRE
e680: 41 54 45 5f 54 52 49 47 47 45 52 20 20 20 20 20  ATE_TRIGGER     
e690: 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67 67 65     7   /* Trigge
e6a0: 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20  r Name    Table 
e6b0: 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
e6c0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41  fine SQLITE_CREA
e6d0: 54 45 5f 56 49 45 57 20 20 20 20 20 20 20 20 20  TE_VIEW         
e6e0: 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20 4e 61    8   /* View Na
e6f0: 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  me       NULL   
e700: 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
e710: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c 45 54  ine SQLITE_DELET
e720: 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
e730: 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61   9   /* Table Na
e740: 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
e750: 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
e760: 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49  ne SQLITE_DROP_I
e770: 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 31  NDEX           1
e780: 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d  0   /* Index Nam
e790: 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  e      Table Nam
e7a0: 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
e7b0: 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41  e SQLITE_DROP_TA
e7c0: 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 31 31  BLE           11
e7d0: 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
e7e0: 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
e7f0: 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
e800: 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
e810: 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31 32 20  P_INDEX      12 
e820: 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20    /* Index Name 
e830: 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20       Table Name 
e840: 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
e850: 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
e860: 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33 20 20  _TABLE      13  
e870: 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20   /* Table Name  
e880: 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
e890: 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
e8a0: 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
e8b0: 54 52 49 47 47 45 52 20 20 20 20 31 34 20 20 20  TRIGGER    14   
e8c0: 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20  /* Trigger Name 
e8d0: 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
e8e0: 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
e8f0: 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56  LITE_DROP_TEMP_V
e900: 49 45 57 20 20 20 20 20 20 20 31 35 20 20 20 2f  IEW       15   /
e910: 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20  * View Name     
e920: 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
e930: 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
e940: 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52  ITE_DROP_TRIGGER
e950: 20 20 20 20 20 20 20 20 20 31 36 20 20 20 2f 2a           16   /*
e960: 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20   Trigger Name   
e970: 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
e980: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
e990: 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20 20 20  TE_DROP_VIEW    
e9a0: 20 20 20 20 20 20 20 20 31 37 20 20 20 2f 2a 20          17   /* 
e9b0: 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20  View Name       
e9c0: 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
e9d0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
e9e0: 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20  E_INSERT        
e9f0: 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20 54         18   /* T
ea00: 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
ea10: 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
ea20: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
ea30: 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 20 20  _PRAGMA         
ea40: 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 50 72        19   /* Pr
ea50: 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20 31 73  agma Name     1s
ea60: 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20 2a 2f  t arg or NULL */
ea70: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
ea80: 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20  READ            
ea90: 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54 61 62       20   /* Tab
eaa0: 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c  le Name      Col
eab0: 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a  umn Name     */.
eac0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
ead0: 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20  ELECT           
eae0: 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 4c 4c      21   /* NULL
eaf0: 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
eb00: 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
eb10: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 52  define SQLITE_TR
eb20: 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20  ANSACTION       
eb30: 20 20 20 32 32 20 20 20 2f 2a 20 4e 55 4c 4c 20     22   /* NULL 
eb40: 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20             NULL 
eb50: 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
eb60: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 50 44  efine SQLITE_UPD
eb70: 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ATE             
eb80: 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c 65 20    23   /* Table 
eb90: 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e  Name      Column
eba0: 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65   Name     */.#de
ebb0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54 54 41  fine SQLITE_ATTA
ebc0: 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CH              
ebd0: 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d   24   /* Filenam
ebe0: 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  e        NULL   
ebf0: 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
ec00: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54 41 43  ine SQLITE_DETAC
ec10: 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
ec20: 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  25   /* Database
ec30: 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20 20 20   Name   NULL    
ec40: 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
ec50: 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f  ne SQLITE_ALTER_
ec60: 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 32  TABLE          2
ec70: 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  6   /* Database 
ec80: 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e 61 6d  Name   Table Nam
ec90: 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
eca0: 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 45 58  e SQLITE_REINDEX
ecb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 37                27
ecc0: 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65     /* Index Name
ecd0: 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
ece0: 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
ecf0: 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 20   SQLITE_ANALYZE 
ed00: 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38 20               28 
ed10: 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
ed20: 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
ed30: 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
ed40: 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 54  SQLITE_CREATE_VT
ed50: 41 42 4c 45 20 20 20 20 20 20 20 20 32 39 20 20  ABLE        29  
ed60: 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20   /* Table Name  
ed70: 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20      Module Name 
ed80: 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
ed90: 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c  QLITE_DROP_VTABL
eda0: 45 20 20 20 20 20 20 20 20 20 20 33 30 20 20 20  E          30   
edb0: 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20  /* Table Name   
edc0: 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 20     Module Name  
edd0: 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
ede0: 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20 20 20  LITE_FUNCTION   
edf0: 20 20 20 20 20 20 20 20 20 20 33 31 20 20 20 2f            31   /
ee00: 2a 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d 65 20  * Function Name 
ee10: 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
ee20: 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
ee30: 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20 20  ITE_COPY        
ee40: 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a            0   /*
ee50: 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64 20   No longer used 
ee60: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  */../*.** CAPI3R
ee70: 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64 20  EF: Tracing And 
ee80: 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74 69  Profiling Functi
ee90: 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ons.**.** These 
eea0: 72 6f 75 74 69 6e 65 73 20 72 65 67 69 73 74 65  routines registe
eeb0: 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  r callback funct
eec0: 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 62 65  ions that can be
eed0: 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 72 61   used for.** tra
eee0: 63 69 6e 67 20 61 6e 64 20 70 72 6f 66 69 6c 69  cing and profili
eef0: 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e  ng the execution
ef00: 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   of SQL statemen
ef10: 74 73 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62  ts..** The callb
ef20: 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67  ack function reg
ef30: 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74  istered by sqlit
ef40: 65 33 5f 74 72 61 63 65 28 29 20 69 73 20 69 6e  e3_trace() is in
ef50: 76 6f 6b 65 64 0a 2a 2a 20 61 74 20 74 68 65 20  voked.** at the 
ef60: 66 69 72 73 74 20 5b 73 71 6c 69 74 65 33 5f 73  first [sqlite3_s
ef70: 74 65 70 28 29 5d 20 66 6f 72 20 74 68 65 20 65  tep()] for the e
ef80: 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 61 6e 20  valuation of an 
ef90: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  SQL statement..*
efa0: 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  * The callback f
efb0: 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72  unction register
efc0: 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 70 72  ed by sqlite3_pr
efd0: 6f 66 69 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b  ofile() is invok
efe0: 65 64 0a 2a 2a 20 61 73 20 65 61 63 68 20 53 51  ed.** as each SQ
eff0: 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69 6e 69  L statement fini
f000: 73 68 65 73 20 61 6e 64 20 69 6e 63 6c 75 64 65  shes and include
f010: 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  s.** information
f020: 20 6f 6e 20 68 6f 77 20 6c 6f 6e 67 20 74 68 61   on how long tha
f030: 74 20 73 74 61 74 65 6d 65 6e 74 20 72 61 6e 2e  t statement ran.
f040: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
f050: 65 33 5f 70 72 6f 66 69 6c 65 28 29 20 41 50 49  e3_profile() API
f060: 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f   is currently co
f070: 6e 73 69 64 65 72 65 64 20 65 78 70 65 72 69 6d  nsidered experim
f080: 65 6e 74 61 6c 20 61 6e 64 0a 2a 2a 20 69 73 20  ental and.** is 
f090: 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
f0a0: 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
f0b0: 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 74   void *sqlite3_t
f0c0: 72 61 63 65 28 73 71 6c 69 74 65 33 2a 2c 20 76  race(sqlite3*, v
f0d0: 6f 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f 69  oid(*xTrace)(voi
f0e0: 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c  d*,const char*),
f0f0: 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
f100: 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
f110: 33 5f 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65  3_profile(sqlite
f120: 33 2a 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72  3*,.   void(*xPr
f130: 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  ofile)(void*,con
f140: 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33  st char*,sqlite3
f150: 5f 75 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29  _uint64), void*)
f160: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
f170: 46 3a 20 51 75 65 72 79 20 50 72 6f 67 72 65 73  F: Query Progres
f180: 73 20 43 61 6c 6c 62 61 63 6b 73 0a 2a 2a 0a 2a  s Callbacks.**.*
f190: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
f1a0: 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c  onfigures a call
f1b0: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20  back function - 
f1c0: 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
f1d0: 6c 62 61 63 6b 20 2d 20 74 68 61 74 0a 2a 2a 20  lback - that.** 
f1e0: 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f  is invoked perio
f1f0: 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c  dically during l
f200: 6f 6e 67 20 72 75 6e 6e 69 6e 67 20 63 61 6c 6c  ong running call
f210: 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78  s to [sqlite3_ex
f220: 65 63 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  ec()],.** [sqlit
f230: 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 20 5b  e3_step()] and [
f240: 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
f250: 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d 70 6c  e()].  An exampl
f260: 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 0a  e use for this .
f270: 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  ** interface is 
f280: 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 70  to keep a GUI up
f290: 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c  dated during a l
f2a0: 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a  arge query..**.*
f2b0: 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63  * The progress c
f2c0: 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
f2d0: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 76 65 72  ed once for ever
f2e0: 79 20 4e 20 76 69 72 74 75 61 6c 20 6d 61 63 68  y N virtual mach
f2f0: 69 6e 65 20 6f 70 63 6f 64 65 73 2c 0a 2a 2a 20  ine opcodes,.** 
f300: 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73  where N is the s
f310: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
f320: 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
f330: 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63 61   The progress ca
f340: 6c 6c 62 61 63 6b 0a 2a 2a 20 69 74 73 65 6c 66  llback.** itself
f350: 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
f360: 79 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75  y the third argu
f370: 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
f380: 63 74 69 6f 6e 2e 20 54 68 65 20 66 6f 75 72 74  ction. The fourt
f390: 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  h.** argument to
f3a0: 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
f3b0: 73 20 61 20 76 6f 69 64 20 70 6f 69 6e 74 65 72  s a void pointer
f3c0: 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 70   passed to the p
f3d0: 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
f3e0: 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 65 61 63  .** function eac
f3f0: 68 20 74 69 6d 65 20 69 74 20 69 73 20 69 6e 76  h time it is inv
f400: 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  oked..**.** If a
f410: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
f420: 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69  3_exec()], [sqli
f430: 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 6f 72 20  te3_step()], or 
f440: 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  [sqlite3_get_tab
f450: 6c 65 28 29 5d 0a 2a 2a 20 72 65 73 75 6c 74 73  le()].** results
f460: 20 69 6e 20 66 65 77 65 72 20 74 68 61 6e 20 4e   in fewer than N
f470: 20 6f 70 63 6f 64 65 73 20 62 65 69 6e 67 20 65   opcodes being e
f480: 78 65 63 75 74 65 64 2c 20 74 68 65 6e 20 74 68  xecuted, then th
f490: 65 20 70 72 6f 67 72 65 73 73 20 0a 2a 2a 20 63  e progress .** c
f4a0: 61 6c 6c 62 61 63 6b 20 69 73 20 6e 65 76 65 72  allback is never
f4b0: 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 0a 2a 2a   invoked..** .**
f4c0: 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70   Only a single p
f4d0: 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
f4e0: 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65   function may be
f4f0: 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20   registered for 
f500: 65 61 63 68 0a 2a 2a 20 6f 70 65 6e 20 64 61 74  each.** open dat
f510: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
f520: 2e 20 20 45 76 65 72 79 20 63 61 6c 6c 20 74 6f  .  Every call to
f530: 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
f540: 73 5f 68 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 6f  s_handler().** o
f550: 76 65 72 77 72 69 74 65 73 20 74 68 65 20 72 65  verwrites the re
f560: 73 75 6c 74 73 20 6f 66 20 74 68 65 20 70 72 65  sults of the pre
f570: 76 69 6f 75 73 20 63 61 6c 6c 2e 0a 2a 2a 20 54  vious call..** T
f580: 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 70 72 6f  o remove the pro
f590: 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 61  gress callback a
f5a0: 6c 74 6f 67 65 74 68 65 72 2c 20 70 61 73 73 20  ltogether, pass 
f5b0: 4e 55 4c 4c 20 61 73 20 74 68 65 20 74 68 69 72  NULL as the thir
f5c0: 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  d.** argument to
f5d0: 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
f5e0: 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f  **.** If the pro
f5f0: 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72  gress callback r
f600: 65 74 75 72 6e 73 20 61 20 72 65 73 75 6c 74 20  eturns a result 
f610: 6f 74 68 65 72 20 74 68 61 6e 20 30 2c 20 74 68  other than 0, th
f620: 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 0a  en the current .
f630: 2a 2a 20 71 75 65 72 79 20 69 73 20 69 6d 6d 65  ** query is imme
f640: 64 69 61 74 65 6c 79 20 74 65 72 6d 69 6e 61 74  diately terminat
f650: 65 64 20 61 6e 64 20 61 6e 79 20 64 61 74 61 62  ed and any datab
f660: 61 73 65 20 63 68 61 6e 67 65 73 20 72 6f 6c 6c  ase changes roll
f670: 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20  ed back..** The 
f680: 63 6f 6e 74 61 69 6e 69 6e 67 20 5b 73 71 6c 69  containing [sqli
f690: 74 65 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71  te3_exec()], [sq
f6a0: 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 6f  lite3_step()], o
f6b0: 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65  r.** [sqlite3_ge
f6c0: 74 5f 74 61 62 6c 65 28 29 5d 20 63 61 6c 6c 20  t_table()] call 
f6d0: 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49  returns SQLITE_I
f6e0: 4e 54 45 52 52 55 50 54 2e 20 20 20 54 68 69 73  NTERRUPT.   This
f6f0: 20 66 65 61 74 75 72 65 0a 2a 2a 20 63 61 6e 20   feature.** can 
f700: 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65 78 61  be used, for exa
f710: 6d 70 6c 65 2c 20 74 6f 20 69 6d 70 6c 65 6d 65  mple, to impleme
f720: 6e 74 20 74 68 65 20 22 43 61 6e 63 65 6c 22 20  nt the "Cancel" 
f730: 62 75 74 74 6f 6e 20 6f 6e 20 61 0a 2a 2a 20 70  button on a.** p
f740: 72 6f 67 72 65 73 73 20 64 69 61 6c 6f 67 20 62  rogress dialog b
f750: 6f 78 20 69 6e 20 61 20 47 55 49 2e 0a 2a 2f 0a  ox in a GUI..*/.
f760: 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
f770: 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
f780: 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33  _handler(sqlite3
f790: 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76  *, int, int(*)(v
f7a0: 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  oid*), void*);..
f7b0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
f7c0: 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61  Opening A New Da
f7d0: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
f7e0: 6e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  n.**.** Open the
f7f0: 20 73 71 6c 69 74 65 20 64 61 74 61 62 61 73 65   sqlite database
f800: 20 66 69 6c 65 20 22 66 69 6c 65 6e 61 6d 65 22   file "filename"
f810: 2e 20 20 54 68 65 20 22 66 69 6c 65 6e 61 6d 65  .  The "filename
f820: 22 20 69 73 20 55 54 46 2d 38 0a 2a 2a 20 65 6e  " is UTF-8.** en
f830: 63 6f 64 65 64 20 66 6f 72 20 5b 73 71 6c 69 74  coded for [sqlit
f840: 65 33 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 5b  e3_open()] and [
f850: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
f860: 29 5d 20 61 6e 64 20 55 54 46 2d 31 36 20 65 6e  )] and UTF-16 en
f870: 63 6f 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20  coded.** in the 
f880: 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
f890: 72 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f  r for [sqlite3_o
f8a0: 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 41 6e 20  pen16()]..** An 
f8b0: 5b 73 71 6c 69 74 65 33 2a 5d 20 68 61 6e 64 6c  [sqlite3*] handl
f8c0: 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  e is returned in
f8d0: 20 2a 70 70 44 62 2c 20 65 76 65 6e 0a 2a 2a 20   *ppDb, even.** 
f8e0: 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
f8f0: 72 73 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  rs. If the datab
f900: 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 6f  ase is opened (o
f910: 72 20 63 72 65 61 74 65 64 29 20 73 75 63 63 65  r created) succe
f920: 73 73 66 75 6c 6c 79 2c 0a 2a 2a 20 74 68 65 6e  ssfully,.** then
f930: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20   [SQLITE_OK] is 
f940: 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
f950: 69 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  ise an error cod
f960: 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54  e is returned. T
f970: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65  he.** [sqlite3_e
f980: 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71 6c  rrmsg()] or [sql
f990: 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d  ite3_errmsg16()]
f9a0: 20 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 62    routines can b
f9b0: 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  e used to obtain
f9c0: 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c  .** an English l
f9d0: 61 6e 67 75 61 67 65 20 64 65 73 63 72 69 70 74  anguage descript
f9e0: 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ion of the error
f9f0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ..**.** The defa
fa00: 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ult encoding for
fa10: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 69   the database wi
fa20: 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a  ll be UTF-8 if.*
fa30: 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  * [sqlite3_open(
fa40: 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f  )] or [sqlite3_o
fa50: 70 65 6e 5f 76 32 28 29 5d 20 69 73 20 63 61 6c  pen_v2()] is cal
fa60: 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 46 2d 31  led and.** UTF-1
fa70: 36 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 6f 70  6 if [sqlite3_op
fa80: 65 6e 31 36 28 29 5d 20 69 73 20 75 73 65 64 2e  en16()] is used.
fa90: 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f  .**.** Whether o
faa0: 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f  r not an error o
fab0: 63 63 75 72 73 20 77 68 65 6e 20 69 74 20 69 73  ccurs when it is
fac0: 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f 75 72 63   opened, resourc
fad0: 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  es associated.**
fae0: 20 77 69 74 68 20 74 68 65 20 5b 73 71 6c 69 74   with the [sqlit
faf0: 65 33 2a 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75  e3*] handle shou
fb00: 6c 64 20 62 65 20 72 65 6c 65 61 73 65 64 20 62  ld be released b
fb10: 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 0a  y passing it to.
fb20: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73  ** [sqlite3_clos
fb30: 65 28 29 5d 20 77 68 65 6e 20 69 74 20 69 73 20  e()] when it is 
fb40: 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72  no longer requir
fb50: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73  ed..**.** The [s
fb60: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
fb70: 5d 20 69 6e 74 65 72 66 61 63 65 20 77 6f 72 6b  ] interface work
fb80: 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f  s like [sqlite3_
fb90: 6f 70 65 6e 28 29 5d 20 65 78 63 65 70 74 20 74  open()] except t
fba0: 68 61 74 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20  hat.** provides 
fbb0: 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 70  two additional p
fbc0: 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 61 64  arameters for ad
fbd0: 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 72 6f 6c  ditional control
fbe0: 20 6f 76 65 72 20 74 68 65 0a 2a 2a 20 6e 65 77   over the.** new
fbf0: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
fc00: 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c 61 67 73  tion.  The flags
fc10: 20 70 61 72 61 6d 65 74 65 72 20 63 61 6e 20 62   parameter can b
fc20: 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20  e one of:.**.** 
fc30: 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  <ol>.** <li>  [S
fc40: 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
fc50: 4e 4c 59 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  NLY].** <li>  [S
fc60: 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
fc70: 52 49 54 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b  RITE].** <li>  [
fc80: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
fc90: 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45  WRITE] | [SQLITE
fca0: 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 0a 2a 2a  _OPEN_CREATE].**
fcb0: 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ol>.**.** The
fcc0: 20 66 69 72 73 74 20 76 61 6c 75 65 20 6f 70 65   first value ope
fcd0: 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ns the database 
fce0: 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 49 66 20 74  read-only.  If t
fcf0: 68 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73  he database does
fd00: 0a 2a 2a 20 6e 6f 74 20 70 72 65 76 69 6f 75 73  .** not previous
fd10: 6c 79 20 65 78 69 73 74 2c 20 61 6e 20 65 72 72  ly exist, an err
fd20: 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  or is returned. 
fd30: 20 54 68 65 20 73 65 63 6f 6e 64 20 6f 70 74 69   The second opti
fd40: 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 74 68 65 20  on opens.** the 
fd50: 64 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61  database for rea
fd60: 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67  ding and writing
fd70: 20 69 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72   if possible, or
fd80: 20 72 65 61 64 69 6e 67 20 6f 6e 6c 79 20 69 66   reading only if
fd90: 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 6c 65 20  .** if the file 
fda0: 69 73 20 77 72 69 74 65 20 70 72 6f 74 65 63 74  is write protect
fdb0: 65 64 2e 20 20 49 6e 20 65 69 74 68 65 72 20 63  ed.  In either c
fdc0: 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ase the database
fdd0: 20 6d 75 73 74 20 61 6c 72 65 61 64 79 0a 2a 2a   must already.**
fde0: 20 65 78 69 73 74 20 6f 72 20 61 6e 20 65 72 72   exist or an err
fdf0: 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  or is returned. 
fe00: 20 54 68 65 20 74 68 69 72 64 20 6f 70 74 69 6f   The third optio
fe10: 6e 20 6f 70 65 6e 73 20 74 68 65 20 64 61 74 61  n opens the data
fe20: 62 61 73 65 0a 2a 2a 20 66 6f 72 20 72 65 61 64  base.** for read
fe30: 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20  ing and writing 
fe40: 61 6e 64 20 63 72 65 61 74 65 73 20 69 74 20 69  and creates it i
fe50: 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c  f it does not al
fe60: 72 65 61 64 79 20 65 78 69 73 74 2e 0a 2a 2a 20  ready exist..** 
fe70: 54 68 65 20 74 68 69 72 64 20 6f 70 74 69 6f 6e  The third option
fe80: 73 20 69 73 20 62 65 68 61 76 69 6f 72 20 74 68  s is behavior th
fe90: 61 74 20 69 73 20 61 6c 77 61 79 73 20 75 73 65  at is always use
fea0: 64 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f  d for [sqlite3_o
feb0: 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73  pen()].** and [s
fec0: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
fed0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  ..**.** If the f
fee0: 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d  ilename is ":mem
fef0: 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 6e 20 70  ory:", then an p
ff00: 72 69 76 61 74 65 0a 2a 2a 20 69 6e 2d 6d 65 6d  rivate.** in-mem
ff10: 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73 20  ory database is 
ff20: 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 65 20  created for the 
ff30: 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69  connection.  Thi
ff40: 73 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64  s in-memory.** d
ff50: 61 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e  atabase will van
ff60: 69 73 68 20 77 68 65 6e 20 74 68 65 20 64 61 74  ish when the dat
ff70: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
ff80: 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 46 75 74   is closed.  Fut
ff90: 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 6f  ure.** version o
ffa0: 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20 6d  f SQLite might m
ffb0: 61 6b 65 20 75 73 65 20 6f 66 20 61 64 64 69 74  ake use of addit
ffc0: 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c 20 66 69  ional special fi
ffd0: 6c 65 6e 61 6d 65 73 0a 2a 2a 20 74 68 61 74 20  lenames.** that 
ffe0: 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 22  begin with the "
fff0: 3a 22 20 63 68 61 72 61 63 74 65 72 2e 20 20 49  :" character.  I
10000 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  t is recommended
10010 20 74 68 61 74 20 0a 2a 2a 20 77 68 65 6e 20 61   that .** when a
10020 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61   database filena
10030 6d 65 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 62  me really does b
10040 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 22 3a 22  egin with.** ":"
10050 20 74 68 61 74 20 79 6f 75 20 70 72 65 66 69 78   that you prefix
10060 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69   the filename wi
10070 74 68 20 61 20 70 61 74 68 6e 61 6d 65 20 6c 69  th a pathname li
10080 6b 65 20 22 2e 2f 22 20 74 6f 0a 2a 2a 20 61 76  ke "./" to.** av
10090 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e 0a 2a  oid ambiguity..*
100a0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65  *.** If the file
100b0 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 79  name is an empty
100c0 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20   string, then a 
100d0 70 72 69 76 61 74 65 20 74 65 6d 70 6f 72 61 72  private temporar
100e0 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 74  y.** on-disk dat
100f0 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72  abase will be cr
10100 65 61 74 65 64 2e 20 20 54 68 69 73 20 70 72 69  eated.  This pri
10110 76 61 74 65 20 64 61 74 61 62 61 73 65 20 77 69  vate database wi
10120 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74  ll be.** automat
10130 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 61  ically deleted a
10140 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 64 61  s soon as the da
10150 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
10160 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  n is closed..**.
10170 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61  ** The fourth pa
10180 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
10190 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
101a0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a  the name of the.
101b0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  ** [sqlite3_vfs]
101c0 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66   object that def
101d0 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74 69  ines the operati
101e0 6e 67 20 73 79 73 74 65 6d 20 0a 2a 2a 20 69 6e  ng system .** in
101f0 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65  terface that the
10200 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f   new database co
10210 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  nnection should 
10220 75 73 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  use.  If the.** 
10230 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
10240 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
10250 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 66 61  er then the defa
10260 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  ult [sqlite3_vfs
10270 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 69 73 20 75  ].** object is u
10280 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f  sed..**.** <b>No
10290 74 65 20 74 6f 20 77 69 6e 64 6f 77 73 20 75 73  te to windows us
102a0 65 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e  ers:</b>  The en
102b0 63 6f 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20  coding used for 
102c0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  the filename arg
102d0 75 6d 65 6e 74 0a 2a 2a 20 6f 66 20 5b 73 71 6c  ument.** of [sql
102e0 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 61 6e 64  ite3_open()] and
102f0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
10300 32 28 29 5d 20 6d 75 73 74 20 62 65 20 55 54 46  2()] must be UTF
10310 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65 72  -8, not whatever
10320 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73 20  .** codepage is 
10330 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65  currently define
10340 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63 6f  d.  Filenames co
10350 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e 61  ntaining interna
10360 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61 63  tional.** charac
10370 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f 6e  ters must be con
10380 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38 20  verted to UTF-8 
10390 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e 67  prior to passing
103a0 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 5b 73   them into.** [s
103b0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 6f  qlite3_open()] o
103c0 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  r [sqlite3_open_
103d0 76 32 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  v2()]..*/.SQLITE
103e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
103f0 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63  _open(.  const c
10400 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20  har *filename,  
10410 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
10420 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f  ename (UTF-8) */
10430 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44  .  sqlite3 **ppD
10440 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  b          /* OU
10450 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e  T: SQLite db han
10460 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  dle */.);.SQLITE
10470 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
10480 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74  _open16(.  const
10490 20 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c   void *filename,
104a0 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
104b0 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29  ilename (UTF-16)
104c0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a   */.  sqlite3 **
104d0 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a  ppDb          /*
104e0 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20   OUT: SQLite db 
104f0 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c  handle */.);.SQL
10500 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
10510 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63  te3_open_v2(.  c
10520 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e  onst char *filen
10530 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61  ame,   /* Databa
10540 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46  se filename (UTF
10550 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
10560 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20   **ppDb,        
10570 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20   /* OUT: SQLite 
10580 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  db handle */.  i
10590 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
105a0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
105b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
105c0 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20  *zVfs        /* 
105d0 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75  Name of VFS modu
105e0 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a  le to use */.);.
105f0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
10600 20 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64   Error Codes And
10610 20 4d 65 73 73 61 67 65 73 0a 2a 2a 0a 2a 2a 20   Messages.**.** 
10620 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 63  The sqlite3_errc
10630 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65 20  ode() interface 
10640 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 65  returns the nume
10650 72 69 63 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  ric.** [SQLITE_O
10660 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 5d  K | result code]
10670 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52   or [SQLITE_IOER
10680 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65  R_READ | extende
10690 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 0a 2a  d result code].*
106a0 2a 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72  * for the most r
106b0 65 63 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c  ecent failed sql
106c0 69 74 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 20  ite3_* API call 
106d0 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
106e0 74 68 20 5b 73 71 6c 69 74 65 33 5d 20 68 61 6e  th [sqlite3] han
106f0 64 6c 65 20 27 64 62 27 2e 20 20 49 66 20 61 20  dle 'db'.  If a 
10700 70 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66  prior API call f
10710 61 69 6c 65 64 20 62 75 74 20 74 68 65 0a 2a 2a  ailed but the.**
10720 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49   most recent API
10730 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c   call succeeded,
10740 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
10750 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 65  e from sqlite3_e
10760 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 73 20 75  rrcode().** is u
10770 6e 64 65 66 69 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a  ndefined. .**.**
10780 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72   The sqlite3_err
10790 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  msg() and sqlite
107a0 33 5f 65 72 72 6d 73 67 31 36 28 29 20 72 65 74  3_errmsg16() ret
107b0 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67  urn English-lang
107c0 61 75 67 65 0a 2a 2a 20 74 65 78 74 20 74 68 61  auge.** text tha
107d0 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  t describes the 
107e0 65 72 72 6f 72 2c 20 61 73 20 65 69 74 68 65 72  error, as either
107f0 20 55 54 46 38 20 6f 72 20 55 54 46 31 36 20 72   UTF8 or UTF16 r
10800 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20  espectively..** 
10810 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74  Memory to hold t
10820 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
10830 20 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67   string is manag
10840 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 20  ed internally.  
10850 54 68 65 20 0a 2a 2a 20 73 74 72 69 6e 67 20 6d  The .** string m
10860 61 79 20 62 65 20 6f 76 65 72 77 72 69 74 74 65  ay be overwritte
10870 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64  n or deallocated
10880 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63   by subsequent c
10890 61 6c 6c 73 20 74 6f 20 53 51 4c 69 74 65 0a 2a  alls to SQLite.*
108a0 2a 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63  * interface func
108b0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c  tions..**.** Cal
108c0 6c 73 20 74 6f 20 6d 61 6e 79 20 73 71 6c 69 74  ls to many sqlit
108d0 65 33 5f 2a 20 66 75 6e 63 74 69 6f 6e 73 20 73  e3_* functions s
108e0 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  et the error cod
108f0 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74  e and string ret
10900 75 72 6e 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c  urned.** by [sql
10910 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c  ite3_errcode()],
10920 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
10930 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
10940 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a  3_errmsg16()].**
10950 20 28 6f 76 65 72 77 72 69 74 69 6e 67 20 74 68   (overwriting th
10960 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  e previous value
10970 73 29 2e 20 4e 6f 74 65 20 74 68 61 74 20 63 61  s). Note that ca
10980 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
10990 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 5b  errcode()],.** [
109a0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
109b0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
109c0 65 72 72 6d 73 67 31 36 28 29 5d 20 74 68 65 6d  errmsg16()] them
109d0 73 65 6c 76 65 73 20 64 6f 20 6e 6f 74 20 61 66  selves do not af
109e0 66 65 63 74 20 74 68 65 0a 2a 2a 20 72 65 73 75  fect the.** resu
109f0 6c 74 73 20 6f 66 20 66 75 74 75 72 65 20 69 6e  lts of future in
10a00 76 6f 63 61 74 69 6f 6e 73 2e 20 20 43 61 6c 6c  vocations.  Call
10a10 73 20 74 6f 20 41 50 49 20 72 6f 75 74 69 6e 65  s to API routine
10a20 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 72 65  s that do not re
10a30 74 75 72 6e 0a 2a 2a 20 61 6e 20 65 72 72 6f 72  turn.** an error
10a40 20 63 6f 64 65 20 28 65 78 61 6d 70 6c 65 3a 20   code (example: 
10a50 5b 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f  [sqlite3_data_co
10a60 75 6e 74 28 29 5d 29 20 64 6f 20 6e 6f 74 0a 2a  unt()]) do not.*
10a70 2a 20 63 68 61 6e 67 65 20 74 68 65 20 65 72 72  * change the err
10a80 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64  or code returned
10a90 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
10aa0 2e 20 20 49 6e 74 65 72 66 61 63 65 73 20 74 68  .  Interfaces th
10ab0 61 74 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 61 73  at are.** not as
10ac0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
10ad0 73 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73  specific databas
10ae0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 65 78  e connection (ex
10af0 61 6d 70 6c 65 73 3a 0a 2a 2a 20 5b 73 71 6c 69  amples:.** [sqli
10b00 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d 20 6f  te3_mprintf()] o
10b10 72 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  r [sqlite3_enabl
10b20 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 29  e_shared_cache()
10b30 5d 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 0a  ] do not change.
10b40 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f  ** the return co
10b50 64 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 73 73 75  de.  .**.** Assu
10b60 6d 69 6e 67 20 6e 6f 20 6f 74 68 65 72 20 69 6e  ming no other in
10b70 74 65 72 76 65 6e 69 6e 67 20 73 71 6c 69 74 65  tervening sqlite
10b80 33 5f 2a 20 41 50 49 20 63 61 6c 6c 73 20 61 72  3_* API calls ar
10b90 65 20 6d 61 64 65 2c 20 74 68 65 20 65 72 72 6f  e made, the erro
10ba0 72 0a 2a 2a 20 63 6f 64 65 20 72 65 74 75 72 6e  r.** code return
10bb0 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
10bc0 69 6f 6e 20 69 73 20 61 73 73 6f 63 69 61 74 65  ion is associate
10bd0 64 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  d with the same 
10be0 65 72 72 6f 72 20 61 73 0a 2a 2a 20 74 68 65 20  error as.** the 
10bf0 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64  strings returned
10c00 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   by [sqlite3_err
10c10 6d 73 67 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  msg()] and [sqli
10c20 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 2e  te3_errmsg16()].
10c30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
10c40 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nt sqlite3_errco
10c50 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  de(sqlite3 *db);
10c60 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
10c70 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
10c80 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a 29  errmsg(sqlite3*)
10c90 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
10ca0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
10cb0 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65  _errmsg16(sqlite
10cc0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
10cd0 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74 65 6d  3REF: SQL Statem
10ce0 65 6e 74 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a  ent Object.**.**
10cf0 20 49 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   Instance of thi
10d00 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65  s object represe
10d10 6e 74 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74  nt single SQL st
10d20 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 0a  atements.  This.
10d30 2a 2a 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20  ** is variously 
10d40 6b 6e 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70  known as a "prep
10d50 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20  ared statement" 
10d60 6f 72 20 61 20 0a 2a 2a 20 22 63 6f 6d 70 69 6c  or a .** "compil
10d70 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
10d80 22 20 6f 72 20 73 69 6d 70 6c 79 20 61 73 20 61  " or simply as a
10d90 20 22 73 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a   "statement"..**
10da0 20 0a 2a 2a 20 54 68 65 20 6c 69 66 65 20 6f 66   .** The life of
10db0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 62 6a   a statement obj
10dc0 65 63 74 20 67 6f 65 73 20 73 6f 6d 65 74 68 69  ect goes somethi
10dd0 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ng like this:.**
10de0 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ol>.** <li>
10df0 20 43 72 65 61 74 65 20 74 68 65 20 6f 62 6a 65   Create the obje
10e00 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ct using [sqlite
10e10 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
10e20 6f 72 20 61 20 72 65 6c 61 74 65 64 0a 2a 2a 20  or a related.** 
10e30 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a       function..*
10e40 2a 20 3c 6c 69 3e 20 42 69 6e 64 20 76 61 6c 75  * <li> Bind valu
10e50 65 73 20 74 6f 20 68 6f 73 74 20 70 61 72 61 6d  es to host param
10e60 65 74 65 72 73 20 75 73 69 6e 67 0a 2a 2a 20 20  eters using.**  
10e70 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e      [sqlite3_bin
10e80 64 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33  d_blob | sqlite3
10e90 5f 62 69 6e 64 5f 2a 20 69 6e 74 65 72 66 61 63  _bind_* interfac
10ea0 65 73 5d 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e  es]..** <li> Run
10eb0 20 74 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c   the SQL by call
10ec0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ing [sqlite3_ste
10ed0 70 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  p()] one or more
10ee0 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20   times..** <li> 
10ef0 52 65 73 65 74 20 74 68 65 20 73 74 61 74 65 6d  Reset the statem
10f00 65 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ent using [sqlit
10f10 65 33 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e  e3_reset()] then
10f20 20 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20   go back.**     
10f30 20 74 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20   to step 2.  Do 
10f40 74 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72  this zero or mor
10f50 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e  e times..** <li>
10f60 20 44 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a   Destroy the obj
10f70 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ect using [sqlit
10f80 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a  e3_finalize()]..
10f90 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52  ** </ol>.**.** R
10fa0 65 66 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74  efer to document
10fb0 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64  ation on individ
10fc0 75 61 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76  ual methods abov
10fd0 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
10fe0 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  .** information.
10ff0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
11000 63 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ct sqlite3_stmt 
11010 73 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f  sqlite3_stmt;../
11020 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
11030 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20  ompiling An SQL 
11040 53 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20  Statement.**.** 
11050 54 6f 20 65 78 65 63 75 74 65 20 61 6e 20 53 51  To execute an SQ
11060 4c 20 71 75 65 72 79 2c 20 69 74 20 6d 75 73 74  L query, it must
11070 20 66 69 72 73 74 20 62 65 20 63 6f 6d 70 69 6c   first be compil
11080 65 64 20 69 6e 74 6f 20 61 20 62 79 74 65 2d 63  ed into a byte-c
11090 6f 64 65 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75  ode.** program u
110a0 73 69 6e 67 20 6f 6e 65 20 6f 66 20 74 68 65 73  sing one of thes
110b0 65 20 72 6f 75 74 69 6e 65 73 2e 20 0a 2a 2a 0a  e routines. .**.
110c0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
110d0 75 6d 65 6e 74 20 22 64 62 22 20 69 73 20 61 6e  ument "db" is an
110e0 20 5b 73 71 6c 69 74 65 33 20 7c 20 53 51 4c 69   [sqlite3 | SQLi
110f0 74 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  te database hand
11100 6c 65 5d 20 0a 2a 2a 20 6f 62 74 61 69 6e 65 64  le] .** obtained
11110 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 61   from a prior ca
11120 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f  ll to [sqlite3_o
11130 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  pen()], [sqlite3
11140 5f 6f 70 65 6e 5f 76 32 28 29 5d 0a 2a 2a 20 6f  _open_v2()].** o
11150 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  r [sqlite3_open1
11160 36 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63  6()]..** The sec
11170 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 22 7a 53  ond argument "zS
11180 71 6c 22 20 69 73 20 74 68 65 20 73 74 61 74 65  ql" is the state
11190 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69  ment to be compi
111a0 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a 20  led, encoded.** 
111b0 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38 20  as either UTF-8 
111c0 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65 20  or UTF-16.  The 
111d0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
111e0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72  ) and sqlite3_pr
111f0 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69 6e  epare_v2().** in
11200 74 65 72 66 61 63 65 73 20 75 73 65 73 20 55 54  terfaces uses UT
11210 46 2d 38 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  F-8 and sqlite3_
11220 70 72 65 70 61 72 65 31 36 28 29 20 61 6e 64 20  prepare16() and 
11230 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
11240 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55 54  6_v2().** use UT
11250 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  F-16..**.** If t
11260 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 6e  he nByte argumen
11270 74 20 69 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61  t is less.** tha
11280 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71  n zero, then zSq
11290 6c 20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20  l is read up to 
112a0 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 74  the first zero t
112b0 65 72 6d 69 6e 61 74 6f 72 2e 20 20 49 66 0a 2a  erminator.  If.*
112c0 2a 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e  * nByte is non-n
112d0 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74  egative, then it
112e0 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
112f0 6e 75 6d 62 65 72 20 6f 66 20 0a 2a 2a 20 62 79  number of .** by
11300 74 65 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53  tes read from zS
11310 71 6c 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 20  ql.  When nByte 
11320 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c  is non-negative,
11330 20 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72   the.** zSql str
11340 69 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74 68  ing ends at eith
11350 65 72 20 74 68 65 20 66 69 72 73 74 20 27 5c 30  er the first '\0
11360 30 30 27 20 63 68 61 72 61 63 74 65 72 20 6f 72  00' character or
11370 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 6e   .** until the n
11380 42 79 74 65 2d 74 68 20 62 79 74 65 2c 20 77 68  Byte-th byte, wh
11390 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69  ichever comes fi
113a0 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 2a 70 7a 54 61  rst..**.** *pzTa
113b0 69 6c 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f  il is made to po
113c0 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74  int to the first
113d0 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65   byte past the e
113e0 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 0a  nd of the first.
113f0 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
11400 20 69 6e 20 7a 53 71 6c 2e 20 20 54 68 69 73 20   in zSql.  This 
11410 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6d  routine only com
11420 70 69 6c 65 73 20 74 68 65 20 66 69 72 73 74 20  piles the first 
11430 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 69 6e 20  statement.** in 
11440 7a 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c  zSql, so *pzTail
11450 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
11460 67 20 74 6f 20 77 68 61 74 20 72 65 6d 61 69 6e  g to what remain
11470 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a  s uncompiled..**
11480 0a 2a 2a 20 2a 70 70 53 74 6d 74 20 69 73 20 6c  .** *ppStmt is l
11490 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  eft pointing to 
114a0 61 20 63 6f 6d 70 69 6c 65 64 20 0a 2a 2a 20 5b  a compiled .** [
114b0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 7c 20 53  sqlite3_stmt | S
114c0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 73 74 72  QL statement str
114d0 75 63 74 75 72 65 5d 20 74 68 61 74 20 63 61 6e  ucture] that can
114e0 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20   be.** executed 
114f0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  using [sqlite3_s
11500 74 65 70 28 29 5d 2e 20 20 4f 72 20 69 66 20 74  tep()].  Or if t
11510 68 65 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72  here is an error
11520 2c 20 2a 70 70 53 74 6d 74 20 6d 61 79 20 62 65  , *ppStmt may be
11530 0a 2a 2a 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e  .** set to NULL.
11540 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 74    If the input t
11550 65 78 74 20 63 6f 6e 74 61 69 6e 65 64 20 6e 6f  ext contained no
11560 20 53 51 4c 20 28 69 66 20 74 68 65 20 69 6e 70   SQL (if the inp
11570 75 74 20 69 73 20 61 6e 64 0a 2a 2a 20 65 6d 70  ut is and.** emp
11580 74 79 20 73 74 72 69 6e 67 20 6f 72 20 61 20 63  ty string or a c
11590 6f 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a 70 70  omment) then *pp
115a0 53 74 6d 74 20 69 73 20 73 65 74 20 74 6f 20 4e  Stmt is set to N
115b0 55 4c 4c 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  ULL.  The callin
115c0 67 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 20 69  g.** procedure i
115d0 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
115e0 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63  r deleting the c
115f0 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74  ompiled SQL stat
11600 65 6d 65 6e 74 0a 2a 2a 20 75 73 69 6e 67 20 5b  ement.** using [
11610 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
11620 28 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73  ()] after it has
11630 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69   finished with i
11640 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63  t..**.** On succ
11650 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  ess, [SQLITE_OK]
11660 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f   is returned.  O
11670 74 68 65 72 77 69 73 65 20 61 6e 20 0a 2a 2a 20  therwise an .** 
11680 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 7c 20  [SQLITE_ERROR | 
11690 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72  error code] is r
116a0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
116b0 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  he sqlite3_prepa
116c0 72 65 5f 76 32 28 29 20 61 6e 64 20 73 71 6c 69  re_v2() and sqli
116d0 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
116e0 28 29 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  () interfaces ar
116f0 65 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64  e.** recommended
11700 20 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f   for all new pro
11710 67 72 61 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f  grams. The two o
11720 6c 64 65 72 20 69 6e 74 65 72 66 61 63 65 73 20  lder interfaces 
11730 61 72 65 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20  are retained.** 
11740 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f  for backwards co
11750 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 62 75 74  mpatibility, but
11760 20 74 68 65 69 72 20 75 73 65 20 69 73 20 64 69   their use is di
11770 73 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e  scouraged..** In
11780 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66   the "v2" interf
11790 61 63 65 73 2c 20 74 68 65 20 70 72 65 70 61 72  aces, the prepar
117a0 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  ed statement.** 
117b0 74 68 61 74 20 69 73 20 72 65 74 75 72 6e 65 64  that is returned
117c0 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73   (the [sqlite3_s
117d0 74 6d 74 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e  tmt] object) con
117e0 74 61 69 6e 73 20 61 20 63 6f 70 79 20 6f 66 20  tains a copy of 
117f0 74 68 65 20 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c  the .** original
11800 20 53 51 4c 20 74 65 78 74 2e 20 54 68 69 73 20   SQL text. This 
11810 63 61 75 73 65 73 20 74 68 65 20 5b 73 71 6c 69  causes the [sqli
11820 74 65 33 5f 73 74 65 70 28 29 5d 20 69 6e 74 65  te3_step()] inte
11830 72 66 61 63 65 20 74 6f 0a 2a 2a 20 62 65 68 61  rface to.** beha
11840 76 65 20 61 20 64 69 66 66 65 72 65 6e 74 6c 79  ve a differently
11850 20 69 6e 20 74 77 6f 20 77 61 79 73 3a 0a 2a 2a   in two ways:.**
11860 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ol>.** <li>
11870 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62  .** If the datab
11880 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ase schema chang
11890 65 73 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72  es, instead of r
118a0 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45  eturning [SQLITE
118b0 5f 53 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a  _SCHEMA] as it.*
118c0 2a 20 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f  * always used to
118d0 20 64 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74   do, [sqlite3_st
118e0 65 70 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d  ep()] will autom
118f0 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69  atically recompi
11900 6c 65 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74  le the SQL.** st
11910 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20  atement and try 
11920 74 6f 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e  to run it again.
11930 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 20    If the schema 
11940 68 61 73 20 63 68 61 6e 67 65 64 20 69 6e 20 61  has changed in a
11950 20 77 61 79 0a 2a 2a 20 74 68 61 74 20 6d 61 6b   way.** that mak
11960 65 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  es the statement
11970 20 6e 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64   no longer valid
11980 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  , [sqlite3_step(
11990 29 5d 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a  )] will still.**
119a0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
119b0 53 43 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e  SCHEMA].  But un
119c0 6c 69 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20  like the legacy 
119d0 62 65 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54  behavior, [SQLIT
119e0 45 5f 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20  E_SCHEMA] is.** 
119f0 6e 6f 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f  now a fatal erro
11a00 72 2e 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c  r.  Calling [sql
11a10 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
11a20 29 5d 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f  )] again will no
11a30 74 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72  t make the.** er
11a40 72 6f 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f  ror go away.  No
11a50 74 65 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33  te: use [sqlite3
11a60 5f 65 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69  _errmsg()] to fi
11a70 6e 64 20 74 68 65 20 74 65 78 74 20 6f 66 20 74  nd the text of t
11a80 68 65 20 70 61 72 73 69 6e 67 0a 2a 2a 20 65 72  he parsing.** er
11a90 72 6f 72 20 74 68 61 74 20 72 65 73 75 6c 74 73  ror that results
11aa0 20 69 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53   in an [SQLITE_S
11ab0 43 48 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a  CHEMA] return..*
11ac0 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c  * </li>.**.** <l
11ad0 69 3e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72  i>.** When an er
11ae0 72 6f 72 20 6f 63 63 75 72 73 2c 20 0a 2a 2a 20  ror occurs, .** 
11af0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
11b00 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 6f 6e 65   will return one
11b10 20 6f 66 20 74 68 65 20 64 65 74 61 69 6c 65 64   of the detailed
11b20 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52   .** [SQLITE_ERR
11b30 4f 52 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65  OR | result code
11b40 73 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  s] or.** [SQLITE
11b50 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c 20 65 78  _IOERR_READ | ex
11b60 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
11b70 64 65 73 5d 20 73 75 63 68 20 61 73 20 64 69 72  des] such as dir
11b80 65 63 74 6c 79 2e 0a 2a 2a 20 54 68 65 20 6c 65  ectly..** The le
11b90 67 61 63 79 20 62 65 68 61 76 69 6f 72 20 77 61  gacy behavior wa
11ba0 73 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f  s that [sqlite3_
11bb0 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e  step()] would on
11bc0 6c 79 20 72 65 74 75 72 6e 20 61 20 67 65 6e 65  ly return a gene
11bd0 72 69 63 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45  ric.** [SQLITE_E
11be0 52 52 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64  RROR] result cod
11bf0 65 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20  e and you would 
11c00 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 73  have to make a s
11c10 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 0a 2a 2a  econd call to.**
11c20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
11c30 29 5d 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66  )] in order to f
11c40 69 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69  ind the underlyi
11c50 6e 67 20 63 61 75 73 65 20 6f 66 20 74 68 65 20  ng cause of the 
11c60 70 72 6f 62 6c 65 6d 2e 0a 2a 2a 20 57 69 74 68  problem..** With
11c70 20 74 68 65 20 22 76 32 22 20 70 72 65 70 61 72   the "v2" prepar
11c80 65 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68  e interfaces, th
11c90 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 61  e underlying rea
11ca0 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 72 6f  son for the erro
11cb0 72 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  r is.** returned
11cc0 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a   immediately..**
11cd0 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a   </li>.** </ol>.
11ce0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
11cf0 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
11d00 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
11d10 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
11d20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
11d30 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
11d40 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20  *zSql,       /* 
11d50 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55  SQL statement, U
11d60 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  TF-8 encoded */.
11d70 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
11d80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
11d90 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
11da0 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
11db0 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
11dc0 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55  **ppStmt,  /* OU
11dd0 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
11de0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
11df0 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
11e00 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
11e10 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69   to unused porti
11e20 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b  on of zSql */.);
11e30 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
11e40 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
11e50 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
11e60 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
11e70 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
11e80 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
11e90 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
11ea0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
11eb0 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f  UTF-8 encoded */
11ec0 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20  .  int nByte,   
11ed0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
11ee0 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
11ef0 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
11f00 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
11f10 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f   **ppStmt,  /* O
11f20 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61  UT: Statement ha
11f30 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
11f40 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
11f50 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65    /* OUT: Pointe
11f60 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74  r to unused port
11f70 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29  ion of zSql */.)
11f80 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
11f90 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
11fa0 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  16(.  sqlite3 *d
11fb0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
11fc0 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
11fd0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
11fe0 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
11ff0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
12000 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a  UTF-16 encoded *
12010 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20  /.  int nByte,  
12020 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
12030 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
12040 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
12050 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
12060 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20  t **ppStmt,  /* 
12070 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68  OUT: Statement h
12080 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
12090 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20   void **pzTail  
120a0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74     /* OUT: Point
120b0 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72  er to unused por
120c0 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a  tion of zSql */.
120d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
120e0 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
120f0 65 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  e16_v2(.  sqlite
12100 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
12110 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
12120 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
12130 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20  void *zSql,     
12140 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    /* SQL stateme
12150 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  nt, UTF-16 encod
12160 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  ed */.  int nByt
12170 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
12180 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
12190 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
121a0 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
121b0 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
121c0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
121d0 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  nt handle */.  c
121e0 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61  onst void **pzTa
121f0 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50  il     /* OUT: P
12200 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64  ointer to unused
12210 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c   portion of zSql
12220 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.);../*.** CA
12230 50 49 33 52 45 46 3a 20 20 44 79 6e 61 6d 69 63  PI3REF:  Dynamic
12240 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65  ally Typed Value
12250 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 53 51   Object.**.** SQ
12260 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d 69  Lite uses dynami
12270 63 20 74 79 70 69 6e 67 20 66 6f 72 20 74 68 65  c typing for the
12280 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65   values it store
12290 73 2e 20 20 56 61 6c 75 65 73 20 63 61 6e 20 0a  s.  Values can .
122a0 2a 2a 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20  ** be integers, 
122b0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
122c0 61 6c 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20  alues, strings, 
122d0 42 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 20  BLOBs, or NULL. 
122e0 20 57 68 65 6e 0a 2a 2a 20 70 61 73 73 69 6e 67   When.** passing
122f0 20 61 72 6f 75 6e 64 20 76 61 6c 75 65 73 20 69   around values i
12300 6e 74 65 72 6e 61 6c 6c 79 2c 20 65 61 63 68 20  nternally, each 
12310 76 61 6c 75 65 20 69 73 20 72 65 70 72 65 73 65  value is represe
12320 6e 74 65 64 20 61 73 0a 2a 2a 20 61 6e 20 69 6e  nted as.** an in
12330 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71  stance of the sq
12340 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
12350 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ct..*/.typedef s
12360 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65  truct Mem sqlite
12370 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20  3_value;../*.** 
12380 43 41 50 49 33 52 45 46 3a 20 20 53 51 4c 20 46  CAPI3REF:  SQL F
12390 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20  unction Context 
123a0 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65  Object.**.** The
123b0 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63   context in whic
123c0 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f  h an SQL functio
123d0 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73 74  n executes is st
123e0 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 71  ored in an.** sq
123f0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62  lite3_context ob
12400 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65 72  ject.  A pointer
12410 20 74 6f 20 73 75 63 68 20 61 6e 20 6f 62 6a 65   to such an obje
12420 63 74 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 72  ct is the.** fir
12430 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
12440 75 73 65 72 2d 64 65 66 69 6e 65 64 20 53 51 4c  user-defined SQL
12450 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74   functions..*/.t
12460 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
12470 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71  lite3_context sq
12480 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a  lite3_context;..
12490 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
124a0 20 42 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20   Binding Values 
124b0 54 6f 20 50 72 65 70 61 72 65 64 20 53 74 61 74  To Prepared Stat
124c0 65 6d 65 6e 74 73 0a 2a 2a 0a 2a 2a 20 49 6e 20  ements.**.** In 
124d0 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 73 20  the SQL strings 
124e0 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65  input to [sqlite
124f0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
12500 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
12510 2c 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ,.** one or more
12520 20 6c 69 74 65 72 61 6c 73 20 63 61 6e 20 62 65   literals can be
12530 20 72 65 70 6c 61 63 65 20 62 79 20 61 20 70 61   replace by a pa
12540 72 61 6d 65 74 65 72 20 69 6e 20 6f 6e 65 20 6f  rameter in one o
12550 66 20 74 68 65 73 65 0a 2a 2a 20 66 6f 72 6d 73  f these.** forms
12560 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
12570 3c 6c 69 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20  <li>  ?.** <li> 
12580 20 3f 4e 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a   ?NNN.** <li>  :
12590 41 41 41 0a 2a 2a 20 3c 6c 69 3e 20 20 40 41 41  AAA.** <li>  @AA
125a0 41 0a 2a 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a  A.** <li>  $VVV.
125b0 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49  ** </ul>.**.** I
125c0 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  n the parameter 
125d0 66 6f 72 6d 73 20 73 68 6f 77 6e 20 61 62 6f 76  forms shown abov
125e0 65 20 4e 4e 4e 20 69 73 20 61 6e 20 69 6e 74 65  e NNN is an inte
125f0 67 65 72 20 6c 69 74 65 72 61 6c 2c 0a 2a 2a 20  ger literal,.** 
12600 41 41 41 20 69 73 20 61 6e 20 61 6c 70 68 61 6e  AAA is an alphan
12610 75 6d 65 72 69 63 20 69 64 65 6e 74 69 66 69 65  umeric identifie
12620 72 20 61 6e 64 20 56 56 56 20 69 73 20 61 20 76  r and VVV is a v
12630 61 72 69 61 62 6c 65 20 6e 61 6d 65 20 61 63 63  ariable name acc
12640 6f 72 64 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65  ording.** to the
12650 20 73 79 6e 74 61 78 20 72 75 6c 65 73 20 6f 66   syntax rules of
12660 20 74 68 65 20 54 43 4c 20 70 72 6f 67 72 61 6d   the TCL program
12670 6d 69 6e 67 20 6c 61 6e 67 75 61 67 65 2e 0a 2a  ming language..*
12680 2a 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66 20  * The values of 
12690 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73  these parameters
126a0 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 22 68   (also called "h
126b0 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 6e 61  ost parameter na
126c0 6d 65 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20  mes").** can be 
126d0 73 65 74 20 75 73 69 6e 67 20 74 68 65 20 73 71  set using the sq
126e0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72  lite3_bind_*() r
126f0 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20  outines defined 
12700 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  here..**.** The 
12710 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
12720 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69  o the sqlite3_bi
12730 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  nd_*() routines 
12740 61 6c 77 61 79 73 20 69 73 20 61 20 70 6f 69 6e  always is a poin
12750 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73  ter.** to the [s
12760 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a  qlite3_stmt] obj
12770 65 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f  ect returned fro
12780 6d 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  m [sqlite3_prepa
12790 72 65 5f 76 32 28 29 5d 20 6f 72 0a 2a 2a 20 69  re_v2()] or.** i
127a0 74 73 20 76 61 72 69 61 6e 74 73 2e 20 20 54 68  ts variants.  Th
127b0 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75  e second.** argu
127c0 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65  ment is the inde
127d0 78 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74  x of the paramet
127e0 65 72 20 74 6f 20 62 65 20 73 65 74 2e 20 20 54  er to be set.  T
127f0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
12800 65 72 20 68 61 73 0a 2a 2a 20 61 6e 20 69 6e 64  er has.** an ind
12810 65 78 20 6f 66 20 31 2e 20 57 68 65 6e 20 74 68  ex of 1. When th
12820 65 20 73 61 6d 65 20 6e 61 6d 65 64 20 70 61 72  e same named par
12830 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 6d  ameter is used m
12840 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73  ore than once, s
12850 65 63 6f 6e 64 0a 2a 2a 20 61 6e 64 20 73 75 62  econd.** and sub
12860 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72  sequent.** occur
12870 72 65 6e 63 65 73 20 68 61 76 65 20 74 68 65 20  rences have the 
12880 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68  same index as th
12890 65 20 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e  e first occurren
128a0 63 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20 66  ce.  The index f
128b0 6f 72 0a 2a 2a 20 6e 61 6d 65 64 20 70 61 72 61  or.** named para
128c0 6d 65 74 65 72 73 20 63 61 6e 20 62 65 20 6c 6f  meters can be lo
128d0 6f 6b 65 64 20 75 70 20 75 73 69 6e 67 20 74 68  oked up using th
128e0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  e.** [sqlite3_bi
128f0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
12900 65 28 29 5d 20 41 50 49 20 69 66 20 64 65 73 69  e()] API if desi
12910 72 65 64 2e 20 20 54 68 65 20 69 6e 64 65 78 20  red.  The index 
12920 66 6f 72 20 22 3f 4e 4e 4e 22 0a 2a 2a 20 70 61  for "?NNN".** pa
12930 72 61 6d 65 74 65 73 20 69 73 20 74 68 65 20 76  rametes is the v
12940 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20  alue of NNN..** 
12950 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75  The NNN value mu
12960 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20  st be between 1 
12970 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d  and the compile-
12980 74 69 6d 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65  time.** paramete
12990 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52  r SQLITE_MAX_VAR
129a0 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 28 64 65  IABLE_NUMBER (de
129b0 66 61 75 6c 74 20 76 61 6c 75 65 3a 20 39 39 39  fault value: 999
129c0 29 2e 0a 2a 2a 20 53 65 65 20 3c 61 20 68 72 65  )..** See <a hre
129d0 66 3d 22 6c 69 6d 69 74 73 2e 68 74 6d 6c 22 3e  f="limits.html">
129e0 6c 69 6d 69 74 73 2e 68 74 6d 6c 3c 2f 61 3e 20  limits.html</a> 
129f0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
12a00 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nformation..**.*
12a10 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75  * The third argu
12a20 6d 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75  ment is the valu
12a30 65 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65  e to bind to the
12a40 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
12a50 2a 20 49 6e 20 74 68 6f 73 65 0a 2a 2a 20 72 6f  * In those.** ro
12a60 75 74 69 6e 65 73 20 74 68 61 74 20 68 61 76 65  utines that have
12a70 20 61 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65   a fourth argume
12a80 6e 74 2c 20 69 74 73 20 76 61 6c 75 65 20 69 73  nt, its value is
12a90 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
12aa0 79 74 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 70  ytes.** in the p
12ab0 61 72 61 6d 65 74 65 72 2e 20 20 54 6f 20 62 65  arameter.  To be
12ac0 20 63 6c 65 61 72 3a 20 74 68 65 20 76 61 6c 75   clear: the valu
12ad0 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  e is the number 
12ae0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 0a  of bytes in the.
12af0 2a 2a 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74  ** string, not t
12b00 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
12b10 72 61 63 74 65 72 73 2e 20 20 54 68 65 20 6e 75  racters.  The nu
12b20 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73  mber.** of bytes
12b30 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
12b40 65 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69  e the zero-termi
12b50 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e 64  nator at the end
12b60 20 6f 66 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20   of strings..** 
12b70 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61  If the fourth pa
12b80 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74  rameter is negat
12b90 69 76 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20  ive, the length 
12ba0 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  of the string is
12bb0 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  .** number of by
12bc0 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69  tes up to the fi
12bd0 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  rst zero termina
12be0 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  tor..**.** The f
12bf0 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  ifth argument to
12c00 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
12c10 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 69  ob(), sqlite3_bi
12c20 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a  nd_text(), and.*
12c30 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  * sqlite3_bind_t
12c40 65 78 74 31 36 28 29 20 69 73 20 61 20 64 65 73  ext16() is a des
12c50 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f 20  tructor used to 
12c60 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 42  dispose of the B
12c70 4c 4f 42 20 6f 72 0a 2a 2a 20 74 65 78 74 20 61  LOB or.** text a
12c80 66 74 65 72 20 53 51 4c 69 74 65 20 68 61 73 20  fter SQLite has 
12c90 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74  finished with it
12ca0 2e 20 20 49 66 20 74 68 65 20 66 69 66 74 68 20  .  If the fifth 
12cb0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a  argument is the.
12cc0 2a 2a 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65  ** special value
12cd0 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d   [SQLITE_STATIC]
12ce0 2c 20 74 68 65 6e 20 74 68 65 20 6c 69 62 72 61  , then the libra
12cf0 72 79 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ry assumes that 
12d00 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  the information.
12d10 2a 2a 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c  ** is in static,
12d20 20 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65   unmanaged space
12d30 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65   and does not ne
12d40 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 20  ed to be freed. 
12d50 20 49 66 20 74 68 65 0a 2a 2a 20 66 69 66 74 68   If the.** fifth
12d60 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68   argument has th
12d70 65 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f  e value [SQLITE_
12d80 54 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e  TRANSIENT], then
12d90 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74   SQLite makes it
12da0 73 0a 2a 2a 20 6f 77 6e 20 70 72 69 76 61 74 65  s.** own private
12db0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74   copy of the dat
12dc0 61 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62  a immediately, b
12dd0 65 66 6f 72 65 20 74 68 65 20 73 71 6c 69 74 65  efore the sqlite
12de0 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 72 6f  3_bind_*().** ro
12df0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a  utine returns..*
12e00 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
12e10 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29  _bind_zeroblob()
12e20 20 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61   routine binds a
12e30 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20   BLOB of length 
12e40 6e 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c  n that.** is fil
12e50 6c 65 64 20 77 69 74 68 20 7a 65 72 6f 73 2e 20  led with zeros. 
12e60 20 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73   A zeroblob uses
12e70 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20   a fixed amount 
12e80 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75  of memory.** (ju
12e90 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f  st an integer to
12ea0 20 68 6f 6c 64 20 69 74 20 73 69 7a 65 29 20 77   hold it size) w
12eb0 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67  hile it is being
12ec0 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a   processed..** Z
12ed0 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74  eroblobs are int
12ee0 65 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20 61  ended to serve a
12ef0 73 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 73 20  s place-holders 
12f00 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a  for BLOBs whose.
12f10 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61  ** content is la
12f20 74 65 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e  ter written usin
12f30 67 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  g .** [sqlite3_b
12f40 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65  lob_open | incre
12f50 6d 65 6e 74 20 42 4c 4f 42 20 49 2f 4f 5d 20 72  ment BLOB I/O] r
12f60 6f 75 74 69 6e 65 73 2e 20 20 41 20 6e 65 67 61  outines.  A nega
12f70 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65 20 66 6f  tive.** value fo
12f80 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72  r the zeroblob r
12f90 65 73 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f  esults in a zero
12fa0 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a  -length BLOB..**
12fb0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
12fc0 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65  bind_*() routine
12fd0 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  s must be called
12fe0 20 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74   after.** [sqlit
12ff0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
13000 20 28 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e   (and its varian
13010 74 73 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ts) or [sqlite3_
13020 72 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20  reset()] and.** 
13030 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f  before [sqlite3_
13040 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64  step()]..** Bind
13050 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65  ings are not cle
13060 61 72 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c  ared by the [sql
13070 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f  ite3_reset()] ro
13080 75 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e  utine..** Unboun
13090 64 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  d parameters are
130a0 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
130b0 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  NULL..**.** Thes
130c0 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
130d0 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e  n [SQLITE_OK] on
130e0 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65   success or an e
130f0 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20  rror code if.** 
13100 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72  anything goes wr
13110 6f 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41  ong.  [SQLITE_RA
13120 4e 47 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64  NGE] is returned
13130 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   if the paramete
13140 72 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75  r.** index is ou
13150 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51  t of range.  [SQ
13160 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72  LITE_NOMEM] is r
13170 65 74 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f  eturned if mallo
13180 63 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c  c fails..** [SQL
13190 49 54 45 5f 4d 49 53 55 53 45 5d 20 69 73 20 72  ITE_MISUSE] is r
131a0 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 73 65  eturned if these
131b0 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61   routines are ca
131c0 6c 6c 65 64 20 6f 6e 20 61 20 76 69 72 74 75 61  lled on a virtua
131d0 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 74 68 61  l.** machine tha
131e0 74 20 69 73 20 74 68 65 20 77 72 6f 6e 67 20 73  t is the wrong s
131f0 74 61 74 65 20 6f 72 20 77 68 69 63 68 20 68 61  tate or which ha
13200 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66  s already been f
13210 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c  inalized..*/.SQL
13220 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
13230 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71  te3_bind_blob(sq
13240 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
13250 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
13260 6e 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f  nt n, void(*)(vo
13270 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
13280 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
13290 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  nd_double(sqlite
132a0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f  3_stmt*, int, do
132b0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50  uble);.SQLITE_AP
132c0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
132d0 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73  nd_int(sqlite3_s
132e0 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  tmt*, int, int);
132f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
13300 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
13310 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  64(sqlite3_stmt*
13320 2c 20 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69  , int, sqlite3_i
13330 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50  nt64);.SQLITE_AP
13340 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
13350 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f  nd_null(sqlite3_
13360 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  stmt*, int);.SQL
13370 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
13380 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 73 71  te3_bind_text(sq
13390 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
133a0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69  , const char*, i
133b0 6e 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f  nt n, void(*)(vo
133c0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
133d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
133e0 6e 64 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65  nd_text16(sqlite
133f0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
13400 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  nst void*, int, 
13410 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
13420 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
13430 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c  sqlite3_bind_val
13440 75 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  ue(sqlite3_stmt*
13450 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c  , int, const sql
13460 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
13470 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
13480 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c  ite3_bind_zerobl
13490 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  ob(sqlite3_stmt*
134a0 2c 20 69 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a  , int, int n);..
134b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
134c0 4e 75 6d 62 65 72 20 4f 66 20 48 6f 73 74 20 50  Number Of Host P
134d0 61 72 61 6d 65 74 65 72 73 0a 2a 2a 0a 2a 2a 20  arameters.**.** 
134e0 52 65 74 75 72 6e 20 74 68 65 20 6c 61 72 67 65  Return the large
134f0 73 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  st host paramete
13500 72 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20 70  r index in the p
13510 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65  recompiled state
13520 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 61 73  ment given.** as
13530 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 20 20   the argument.  
13540 57 68 65 6e 20 74 68 65 20 68 6f 73 74 20 70 61  When the host pa
13550 72 61 6d 65 74 65 72 73 20 61 72 65 20 6f 66 20  rameters are of 
13560 74 68 65 20 66 6f 72 6d 73 20 6c 69 6b 65 20 22  the forms like "
13570 3a 41 41 41 22 0a 2a 2a 20 6f 72 20 22 3f 22 2c  :AAA".** or "?",
13580 20 74 68 65 6e 20 74 68 65 79 20 61 72 65 20 61   then they are a
13590 73 73 69 67 6e 65 64 20 73 65 71 75 65 6e 74 69  ssigned sequenti
135a0 61 6c 20 69 6e 63 72 65 61 73 69 6e 67 20 6e 75  al increasing nu
135b0 6d 62 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 0a  mbers beginning.
135c0 2a 2a 20 77 69 74 68 20 6f 6e 65 2c 20 73 6f 20  ** with one, so 
135d0 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
135e0 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ed is the number
135f0 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 2e 20   of parameters. 
13600 20 48 6f 77 65 76 65 72 0a 2a 2a 20 69 66 20 74   However.** if t
13610 68 65 20 73 61 6d 65 20 68 6f 73 74 20 70 61 72  he same host par
13620 61 6d 65 74 65 72 20 6e 61 6d 65 20 69 73 20 75  ameter name is u
13630 73 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  sed multiple tim
13640 65 73 2c 20 65 61 63 68 20 6f 63 63 75 72 72 61  es, each occurra
13650 6e 63 65 0a 2a 2a 20 69 73 20 67 69 76 65 6e 20  nce.** is given 
13660 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 2c  the same number,
13670 20 73 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65   so the value re
13680 74 75 72 6e 65 64 20 69 6e 20 74 68 61 74 20 63  turned in that c
13690 61 73 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ase is the numbe
136a0 72 0a 2a 2a 20 6f 66 20 75 6e 69 71 75 65 20 68  r.** of unique h
136b0 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 6e 61  ost parameter na
136c0 6d 65 73 2e 20 20 49 66 20 68 6f 73 74 20 70 61  mes.  If host pa
136d0 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20  rameters of the 
136e0 66 6f 72 6d 20 22 3f 4e 4e 4e 22 0a 2a 2a 20 61  form "?NNN".** a
136f0 72 65 20 75 73 65 64 20 28 77 68 65 72 65 20 4e  re used (where N
13700 4e 4e 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  NN is an integer
13710 29 20 74 68 65 6e 20 74 68 65 72 65 20 6d 69 67  ) then there mig
13720 68 74 20 62 65 20 67 61 70 73 20 69 6e 20 74 68  ht be gaps in th
13730 65 0a 2a 2a 20 6e 75 6d 62 65 72 69 6e 67 20 61  e.** numbering a
13740 6e 64 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  nd the value ret
13750 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 69 6e  urned by this in
13760 74 65 72 66 61 63 65 20 69 73 20 74 68 65 20 69  terface is the i
13770 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 68  ndex of the.** h
13780 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 77 69  ost parameter wi
13790 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 69  th the largest i
137a0 6e 64 65 78 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  ndex value..**.*
137b0 2a 20 54 68 65 20 70 72 65 70 61 72 65 64 20 73  * The prepared s
137c0 74 61 74 65 6d 65 6e 74 20 6d 75 73 74 20 6e 6f  tatement must no
137d0 74 20 62 65 20 5b 73 71 6c 69 74 65 33 5f 66 69  t be [sqlite3_fi
137e0 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a  nalize | finaliz
137f0 65 64 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20  ed].** prior to 
13800 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
13810 75 72 6e 6e 69 6e 67 2e 20 20 4f 74 68 65 72 77  urnning.  Otherw
13820 69 73 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ise the results 
13830 61 72 65 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a  are undefined.**
13840 20 61 6e 64 20 70 72 6f 62 61 62 6c 79 20 75 6e   and probably un
13850 64 65 73 69 72 61 62 6c 65 2e 0a 2a 2f 0a 53 51  desirable..*/.SQ
13860 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
13870 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
13880 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ter_count(sqlite
13890 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  3_stmt*);../*.**
138a0 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20   CAPI3REF: Name 
138b0 4f 66 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65  Of A Host Parame
138c0 74 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ter.**.** This r
138d0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
138e0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
138f0 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 68  name of the n-th
13900 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 61 20   parameter in a 
13910 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  .** [sqlite3_stm
13920 74 20 7c 20 70 72 65 70 61 72 65 64 20 73 74 61  t | prepared sta
13930 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 48 6f 73 74  tement]..** Host
13940 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74   parameters of t
13950 68 65 20 66 6f 72 6d 20 22 3a 41 41 41 22 20 6f  he form ":AAA" o
13960 72 20 22 40 41 41 41 22 20 6f 72 20 22 24 56 56  r "@AAA" or "$VV
13970 56 22 20 68 61 76 65 20 61 20 6e 61 6d 65 0a 2a  V" have a name.*
13980 2a 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73  * which is the s
13990 74 72 69 6e 67 20 22 3a 41 41 41 22 20 6f 72 20  tring ":AAA" or 
139a0 22 40 41 41 41 22 20 6f 72 20 22 24 56 56 56 22  "@AAA" or "$VVV"
139b0 2e 20 20 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  .  .** In other 
139c0 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74 69  words, the initi
139d0 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f 72  al ":" or "$" or
139e0 20 22 40 22 0a 2a 2a 20 69 73 20 69 6e 63 6c 75   "@".** is inclu
139f0 64 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  ded as part of t
13a00 68 65 20 6e 61 6d 65 2e 0a 2a 2a 20 50 61 72 61  he name..** Para
13a10 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f  meters of the fo
13a20 72 6d 20 22 3f 22 20 6f 72 20 22 3f 4e 4e 4e 22  rm "?" or "?NNN"
13a30 20 68 61 76 65 20 6e 6f 20 6e 61 6d 65 2e 0a 2a   have no name..*
13a40 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62  *.** The first b
13a50 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 68  ound parameter h
13a60 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
13a70 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  , not 0..**.** I
13a80 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 73  f the value n is
13a90 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72   out of range or
13aa0 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 72   if the n-th par
13ab0 61 6d 65 74 65 72 20 69 73 20 6e 61 6d 65 6c 65  ameter is namele
13ac0 73 73 2c 0a 2a 2a 20 74 68 65 6e 20 4e 55 4c 4c  ss,.** then NULL
13ad0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
13ae0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
13af0 6e 67 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20  ng is always in 
13b00 74 68 65 0a 2a 2a 20 55 54 46 2d 38 20 65 6e 63  the.** UTF-8 enc
13b10 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68  oding even if th
13b20 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65  e named paramete
13b30 72 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79  r was originally
13b40 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 61 73   specified.** as
13b50 20 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69   UTF-16 in [sqli
13b60 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
13b70 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
13b80 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2f  pare16_v2()]..*/
13b90 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
13ba0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
13bb0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e  bind_parameter_n
13bc0 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
13bd0 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *, int);../*.** 
13be0 43 41 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20  CAPI3REF: Index 
13bf0 4f 66 20 41 20 50 61 72 61 6d 65 74 65 72 20 57  Of A Parameter W
13c00 69 74 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65  ith A Given Name
13c10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
13c20 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
13c30 69 6e 64 65 78 20 6f 66 20 61 20 68 6f 73 74 20  index of a host 
13c40 70 61 72 61 6d 65 74 65 72 20 77 69 74 68 20 74  parameter with t
13c50 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 0a 2a  he given name..*
13c60 2a 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 20  * The name must 
13c70 6d 61 74 63 68 20 65 78 61 63 74 6c 79 2e 20 20  match exactly.  
13c80 49 66 20 6e 6f 20 70 61 72 61 6d 65 74 65 72 20  If no parameter 
13c90 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e  with the given n
13ca0 61 6d 65 20 69 73 20 0a 2a 2a 20 66 6f 75 6e 64  ame is .** found
13cb0 2c 20 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72  , return 0.  Par
13cc0 61 6d 65 74 65 72 20 6e 61 6d 65 73 20 6d 75 73  ameter names mus
13cd0 74 20 62 65 20 55 54 46 38 2e 0a 2a 2f 0a 53 51  t be UTF8..*/.SQ
13ce0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
13cf0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
13d00 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65  ter_index(sqlite
13d10 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20 63  3_stmt*, const c
13d20 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a  har *zName);../*
13d30 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
13d40 73 65 74 20 41 6c 6c 20 42 69 6e 64 69 6e 67 73  set All Bindings
13d50 20 4f 6e 20 41 20 50 72 65 70 61 72 65 64 20 53   On A Prepared S
13d60 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 43  tatement.**.** C
13d70 6f 6e 74 72 61 72 79 20 74 6f 20 74 68 65 20 69  ontrary to the i
13d80 6e 74 75 69 74 69 6f 6e 20 6f 66 20 6d 61 6e 79  ntuition of many
13d90 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  , [sqlite3_reset
13da0 28 29 5d 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  ()] does not.** 
13db0 72 65 73 65 74 20 74 68 65 20 5b 73 71 6c 69 74  reset the [sqlit
13dc0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62  e3_bind_blob | b
13dd0 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 61 20 0a 2a  indings] on a .*
13de0 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  * [sqlite3_stmt 
13df0 7c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  | prepared state
13e00 6d 65 6e 74 5d 2e 20 20 55 73 65 20 74 68 69 73  ment].  Use this
13e10 20 72 6f 75 74 69 6e 65 20 74 6f 0a 2a 2a 20 72   routine to.** r
13e20 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20 70 61  eset all host pa
13e30 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c 4c  rameters to NULL
13e40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
13e50 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61  int sqlite3_clea
13e60 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
13e70 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a  e3_stmt*);../*.*
13e80 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62  * CAPI3REF: Numb
13e90 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49 6e  er Of Columns In
13ea0 20 41 20 52 65 73 75 6c 74 20 53 65 74 0a 2a 2a   A Result Set.**
13eb0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
13ec0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
13ed0 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
13ee0 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  et returned by t
13ef0 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  he .** [sqlite3_
13f00 73 74 6d 74 20 7c 20 63 6f 6d 70 69 6c 65 64 20  stmt | compiled 
13f10 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20  SQL statement]. 
13f20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
13f30 75 72 6e 73 20 30 0a 2a 2a 20 69 66 20 70 53 74  urns 0.** if pSt
13f40 6d 74 20 69 73 20 61 6e 20 53 51 4c 20 73 74 61  mt is an SQL sta
13f50 74 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73  tement that does
13f60 20 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61   not return data
13f70 20 28 66 6f 72 20 0a 2a 2a 20 65 78 61 6d 70 6c   (for .** exampl
13f80 65 20 61 6e 20 55 50 44 41 54 45 29 2e 0a 2a 2f  e an UPDATE)..*/
13f90 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
13fa0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
13fb0 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  ount(sqlite3_stm
13fc0 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a  t *pStmt);../*.*
13fd0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 75  * CAPI3REF: Colu
13fe0 6d 6e 20 4e 61 6d 65 73 20 49 6e 20 41 20 52 65  mn Names In A Re
13ff0 73 75 6c 74 20 53 65 74 0a 2a 2a 0a 2a 2a 20 54  sult Set.**.** T
14000 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65  hese routines re
14010 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73  turn the name as
14020 73 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74  signed to a part
14030 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a  icular column.**
14040 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
14050 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  et of a SELECT s
14060 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73  tatement.  The s
14070 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
14080 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63  me().** interfac
14090 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
140a0 74 65 72 20 74 6f 20 61 20 55 54 46 38 20 73 74  ter to a UTF8 st
140b0 72 69 6e 67 20 61 6e 64 20 73 71 6c 69 74 65 33  ring and sqlite3
140c0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29  _column_name16()
140d0 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 70 6f  .** returns a po
140e0 69 6e 74 65 72 20 74 6f 20 61 20 55 54 46 31 36  inter to a UTF16
140f0 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 66 69   string.  The fi
14100 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  rst parameter is
14110 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
14120 5f 73 74 6d 74 20 7c 20 70 72 65 70 61 72 65 64  _stmt | prepared
14130 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74   statement] that
14140 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
14150 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
14160 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
14170 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
14180 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 20   column number. 
14190 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63   The left-most c
141a0 6f 6c 75 6d 6e 20 69 73 0a 2a 2a 20 6e 75 6d 62  olumn is.** numb
141b0 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 0..**.** The 
141c0 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
141d0 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64  pointer is valid
141e0 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68   until either th
141f0 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  e .** [sqlite3_s
14200 74 6d 74 20 7c 20 70 72 65 70 61 72 65 64 20 73  tmt | prepared s
14210 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64 65 73  tatement] is des
14220 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74  troyed by [sqlit
14230 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a  e3_finalize()].*
14240 2a 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 6e  * or until the n
14250 65 78 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  ext call sqlite3
14260 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 6f  _column_name() o
14270 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  r sqlite3_column
14280 5f 6e 61 6d 65 31 36 28 29 0a 2a 2a 20 6f 6e 20  _name16().** on 
14290 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 2e  the same column.
142a0 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65  .**.** If sqlite
142b0 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  3_malloc() fails
142c0 20 64 75 72 69 6e 67 20 74 68 65 20 70 72 6f 63   during the proc
142d0 65 73 73 69 6e 67 20 6f 66 20 65 69 74 68 65 72  essing of either
142e0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 6f 72   routine.** (for
142f0 20 65 78 61 6d 70 6c 65 20 64 75 72 69 6e 67 20   example during 
14300 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f  a conversion fro
14310 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31  m UTF-8 to UTF-1
14320 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 4e 55 4c  6) then a.** NUL
14330 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74  L pointer is ret
14340 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
14350 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
14360 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
14370 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
14380 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49  t*, int N);.SQLI
14390 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
143a0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
143b0 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33  n_name16(sqlite3
143c0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a  _stmt*, int N);.
143d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
143e0 20 53 6f 75 72 63 65 20 4f 66 20 44 61 74 61 20   Source Of Data 
143f0 49 6e 20 41 20 51 75 65 72 79 20 52 65 73 75 6c  In A Query Resul
14400 74 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  t.**.** These ro
14410 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 61  utines provide a
14420 20 6d 65 61 6e 73 20 74 6f 20 64 65 74 65 72 6d   means to determ
14430 69 6e 65 20 77 68 61 74 20 63 6f 6c 75 6d 6e 20  ine what column 
14440 6f 66 20 77 68 61 74 0a 2a 2a 20 74 61 62 6c 65  of what.** table
14450 20 69 6e 20 77 68 69 63 68 20 64 61 74 61 62 61   in which databa
14460 73 65 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61  se a result of a
14470 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
14480 74 20 63 6f 6d 65 73 20 66 72 6f 6d 2e 0a 2a 2a  t comes from..**
14490 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
144a0 20 64 61 74 61 62 61 73 65 20 6f 72 20 74 61 62   database or tab
144b0 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 63 61 6e  le or column can
144c0 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 73 0a   be returned as.
144d0 2a 2a 20 65 69 74 68 65 72 20 61 20 55 54 46 38  ** either a UTF8
144e0 20 6f 72 20 55 54 46 31 36 20 73 74 72 69 6e 67   or UTF16 string
144f0 2e 20 20 54 68 65 20 5f 64 61 74 61 62 61 73 65  .  The _database
14500 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  _ routines retur
14510 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  n.** the databas
14520 65 20 6e 61 6d 65 2c 20 74 68 65 20 5f 74 61 62  e name, the _tab
14530 6c 65 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74  le_ routines ret
14540 75 72 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61  urn the table na
14550 6d 65 2c 20 61 6e 64 0a 2a 2a 20 74 68 65 20 6f  me, and.** the o
14560 72 69 67 69 6e 5f 20 72 6f 75 74 69 6e 65 73 20  rigin_ routines 
14570 72 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d  return the colum
14580 6e 20 6e 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 72  n name..** The r
14590 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
145a0 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 0a 2a 2a  s valid until.**
145b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
145c0 6d 74 20 7c 20 70 72 65 70 61 72 65 64 20 73 74  mt | prepared st
145d0 61 74 65 6d 65 6e 74 5d 20 69 73 20 64 65 73 74  atement] is dest
145e0 72 6f 79 65 64 20 75 73 69 6e 67 0a 2a 2a 20 5b  royed using.** [
145f0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
14600 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65  ()] or until the
14610 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f   same informatio
14620 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 0a 2a  n is requested.*
14630 2a 20 61 67 61 69 6e 20 69 6e 20 61 20 64 69 66  * again in a dif
14640 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67 2e  ferent encoding.
14650 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 73  .**.** The names
14660 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 74 68   returned are th
14670 65 20 6f 72 69 67 69 6e 61 6c 20 75 6e 2d 61 6c  e original un-al
14680 69 61 73 65 64 20 6e 61 6d 65 73 20 6f 66 20 74  iased names of t
14690 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2c 20  he.** database, 
146a0 74 61 62 6c 65 2c 20 61 6e 64 20 63 6f 6c 75 6d  table, and colum
146b0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  n..**.** The fir
146c0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
146d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c  he following cal
146e0 6c 73 20 69 73 20 61 20 0a 2a 2a 20 5b 73 71 6c  ls is a .** [sql
146f0 69 74 65 33 5f 73 74 6d 74 20 7c 20 63 6f 6d 70  ite3_stmt | comp
14700 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  iled SQL stateme
14710 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 66 75  nt]..** These fu
14720 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 69  nctions return i
14730 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
14740 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
14750 72 65 74 75 72 6e 65 64 20 62 79 20 0a 2a 2a 20  returned by .** 
14760 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 77  the statement, w
14770 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65  here N is the se
14780 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72  cond function ar
14790 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  gument..**.** If
147a0 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
147b0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
147c0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e 20  statement is an 
147d0 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72  expression.** or
147e0 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 73   subquery and is
147f0 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61   not a column va
14800 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66  lue, then all of
14810 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
14820 0a 2a 2a 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  .** return NULL.
14830 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79   Otherwise, they
14840 20 72 65 74 75 72 6e 20 74 68 65 20 0a 2a 2a 20   return the .** 
14850 6e 61 6d 65 20 6f 66 20 74 68 65 20 61 74 74 61  name of the atta
14860 63 68 65 64 20 64 61 74 61 62 61 73 65 2c 20 74  ched database, t
14870 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20  able and column 
14880 74 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c  that query resul
14890 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 77 61 73 20  t.** column was 
148a0 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a  extracted from..
148b0 2a 2a 0a 2a 2a 20 41 73 20 77 69 74 68 20 61 6c  **.** As with al
148c0 6c 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 41  l other SQLite A
148d0 50 49 73 2c 20 74 68 6f 73 65 20 70 6f 73 74 66  PIs, those postf
148e0 69 78 65 64 20 77 69 74 68 20 22 31 36 22 20 72  ixed with "16" r
148f0 65 74 75 72 6e 20 55 54 46 2d 31 36 0a 2a 2a 20  eturn UTF-16.** 
14900 65 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 73 2c  encoded strings,
14910 20 74 68 65 20 6f 74 68 65 72 20 66 75 6e 63 74   the other funct
14920 69 6f 6e 73 20 72 65 74 75 72 6e 20 55 54 46 2d  ions return UTF-
14930 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 41  8..**.** These A
14940 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61 76 61  PIs are only ava
14950 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c 69  ilable if the li
14960 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c  brary was compil
14970 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20  ed with the .** 
14980 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
14990 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 70 72  LUMN_METADATA pr
149a0 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f  eprocessor symbo
149b0 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  l defined..**.**
149c0 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   If two or more 
149d0 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65  threads call one
149e0 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 73   or more of thes
149f0 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 6e  e routines again
14a00 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70  st the same.** p
14a10 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
14a20 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 20  t and column at 
14a30 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68  the same time th
14a40 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  en the results a
14a50 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e  re.** undefined.
14a60 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
14a70 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
14a80 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
14a90 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  se_name(sqlite3_
14aa0 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  stmt*,int);.SQLI
14ab0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
14ac0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
14ad0 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
14ae0 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
14af0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
14b00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
14b10 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
14b20 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  e_name(sqlite3_s
14b30 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  tmt*,int);.SQLIT
14b40 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
14b50 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
14b60 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71  _table_name16(sq
14b70 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
14b80 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
14b90 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
14ba0 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
14bb0 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
14bc0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  *,int);.SQLITE_A
14bd0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
14be0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
14bf0 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69  igin_name16(sqli
14c00 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
14c10 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
14c20 20 44 65 63 6c 61 72 65 64 20 44 61 74 61 74 79   Declared Dataty
14c30 70 65 20 4f 66 20 41 20 51 75 65 72 79 20 52 65  pe Of A Query Re
14c40 73 75 6c 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  sult.**.** The f
14c50 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69  irst parameter i
14c60 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  s a [sqlite3_stm
14c70 74 20 7c 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c  t | compiled SQL
14c80 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 0a 2a 2a   statement]. .**
14c90 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65   If this stateme
14ca0 6e 74 20 69 73 20 61 20 53 45 4c 45 43 54 20 73  nt is a SELECT s
14cb0 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65  tatement and the
14cc0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   Nth column of t
14cd0 68 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  he .** returned 
14ce0 72 65 73 75 6c 74 20 73 65 74 20 20 6f 66 20 74  result set  of t
14cf0 68 61 74 20 53 45 4c 45 43 54 20 69 73 20 61 20  hat SELECT is a 
14d00 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f  table column (no
14d10 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  t an.** expressi
14d20 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 29 20  on or subquery) 
14d30 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65  then the declare
14d40 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 74 61  d type of the ta
14d50 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73  ble.** column is
14d60 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68   returned. If th
14d70 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20  e Nth column of 
14d80 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69  the result set i
14d90 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  s an.** expressi
14da0 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20  on or subquery, 
14db0 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  then a NULL poin
14dc0 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
14dd0 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
14de0 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61 79   string is alway
14df0 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e  s UTF-8 encoded.
14e00 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e   For example, in
14e10 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
14e20 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43   schema:.**.** C
14e30 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63  REATE TABLE t1(c
14e40 31 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a  1 VARIANT);.**.*
14e50 2a 20 41 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77  * And the follow
14e60 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  ing statement co
14e70 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 45  mpiled:.**.** SE
14e80 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63 31 20  LECT c1 + 1, c1 
14e90 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 54  FROM t1;.**.** T
14ea0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
14eb0 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68   would return th
14ec0 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e  e string "VARIAN
14ed0 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  T" for the secon
14ee0 64 0a 2a 2a 20 72 65 73 75 6c 74 20 63 6f 6c 75  d.** result colu
14ef0 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61  mn (i==1), and a
14f00 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f   NULL pointer fo
14f10 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75  r the first resu
14f20 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 28 69 3d  lt column.** (i=
14f30 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  =0)..**.** SQLit
14f40 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20 72  e uses dynamic r
14f50 75 6e 2d 74 69 6d 65 20 74 79 70 69 6e 67 2e 20  un-time typing. 
14f60 20 53 6f 20 6a 75 73 74 20 62 65 63 61 75 73 65   So just because
14f70 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20   a column.** is 
14f80 64 65 63 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74  declared to cont
14f90 61 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72  ain a particular
14fa0 20 74 79 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d   type does not m
14fb0 65 61 6e 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ean that the.** 
14fc0 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74  data stored in t
14fd0 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66  hat column is of
14fe0 20 74 68 65 20 64 65 63 6c 61 72 65 64 20 74 79   the declared ty
14ff0 70 65 2e 20 20 53 51 4c 69 74 65 20 69 73 0a 2a  pe.  SQLite is.*
15000 2a 20 73 74 72 6f 6e 67 6c 79 20 74 79 70 65 64  * strongly typed
15010 2c 20 62 75 74 20 74 68 65 20 74 79 70 69 6e 67  , but the typing
15020 20 69 73 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20   is dynamic not 
15030 73 74 61 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a  static.  Type.**
15040 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77   is associated w
15050 69 74 68 20 69 6e 64 69 76 69 64 75 61 6c 20 76  ith individual v
15060 61 6c 75 65 73 2c 20 6e 6f 74 20 77 69 74 68 20  alues, not with 
15070 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a  the containers.*
15080 2a 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74  * used to hold t
15090 68 6f 73 65 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a  hose values..*/.
150a0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
150b0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
150c0 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73  olumn_decltype(s
150d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 69  qlite3_stmt *, i
150e0 6e 74 20 69 29 3b 0a 53 51 4c 49 54 45 5f 41 50  nt i);.SQLITE_AP
150f0 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
15100 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
15110 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f  ltype16(sqlite3_
15120 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 20  stmt*,int);../* 
15130 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20 45  .** CAPI3REF:  E
15140 76 61 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53  valuate An SQL S
15150 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 41  tatement.**.** A
15160 66 74 65 72 20 61 6e 20 5b 73 71 6c 69 74 65 33  fter an [sqlite3
15170 5f 73 74 6d 74 20 7c 20 53 51 4c 20 73 74 61 74  _stmt | SQL stat
15180 65 6d 65 6e 74 5d 20 68 61 73 20 62 65 65 6e 20  ement] has been 
15190 70 72 65 70 61 72 65 64 20 77 69 74 68 20 61 20  prepared with a 
151a0 63 61 6c 6c 0a 2a 2a 20 74 6f 20 65 69 74 68 65  call.** to eithe
151b0 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
151c0 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c  re_v2()] or [sql
151d0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
151e0 32 28 29 5d 20 6f 72 20 74 6f 20 6f 6e 65 20 6f  2()] or to one o
151f0 66 0a 2a 2a 20 74 68 65 20 6c 65 67 61 63 79 20  f.** the legacy 
15200 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69  interfaces [sqli
15210 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f  te3_prepare()] o
15220 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
15230 72 65 31 36 28 29 5d 2c 0a 2a 2a 20 74 68 65 6e  re16()],.** then
15240 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   this function m
15250 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e  ust be called on
15260 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20  e or more times 
15270 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20  to evaluate the 
15280 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  .** statement..*
15290 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73  *.** The details
152a0 20 6f 66 20 74 68 65 20 62 65 68 61 76 69 6f 72   of the behavior
152b0 20 6f 66 20 74 68 69 73 20 73 71 6c 69 74 65 33   of this sqlite3
152c0 5f 73 74 65 70 28 29 20 69 6e 74 65 72 66 61 63  _step() interfac
152d0 65 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77  e depend.** on w
152e0 68 65 74 68 65 72 20 74 68 65 20 73 74 61 74 65  hether the state
152f0 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 65  ment was prepare
15300 64 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 65  d using the newe
15310 72 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  r "v2" interface
15320 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
15330 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b  pare_v2()] and [
15340 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
15350 36 5f 76 32 28 29 5d 20 6f 72 20 74 68 65 20 6f  6_v2()] or the o
15360 6c 64 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69  lder legacy.** i
15370 6e 74 65 72 66 61 63 65 20 5b 73 71 6c 69 74 65  nterface [sqlite
15380 33 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64  3_prepare()] and
15390 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
153a0 65 31 36 28 29 5d 2e 20 20 54 68 65 20 75 73 65  e16()].  The use
153b0 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22   of the.** new "
153c0 76 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 73  v2" interface is
153d0 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72   recommended for
153e0 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e   new application
153f0 73 20 62 75 74 20 74 68 65 20 6c 65 67 61 63 79  s but the legacy
15400 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 77 69  .** interface wi
15410 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62  ll continue to b
15420 65 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a  e supported..**.
15430 2a 2a 20 49 6e 20 74 68 65 20 6c 61 67 61 63 79  ** In the lagacy
15440 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 20   interface, the 
15450 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69 6c  return value wil
15460 6c 20 62 65 20 65 69 74 68 65 72 20 5b 53 51 4c  l be either [SQL
15470 49 54 45 5f 42 55 53 59 5d 2c 20 0a 2a 2a 20 5b  ITE_BUSY], .** [
15480 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53  SQLITE_DONE], [S
15490 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c  QLITE_ROW], [SQL
154a0 49 54 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b  ITE_ERROR], or [
154b0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a  SQLITE_MISUSE]..
154c0 2a 2a 20 57 69 74 68 20 74 68 65 20 22 76 32 22  ** With the "v2"
154d0 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20   interface, any 
154e0 6f 66 20 74 68 65 20 6f 74 68 65 72 20 5b 53 51  of the other [SQ
154f0 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 73 75 6c 74  LITE_OK | result
15500 20 63 6f 64 65 5d 0a 2a 2a 20 6f 72 20 5b 53 51   code].** or [SQ
15510 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20  LITE_IOERR_READ 
15520 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  | extended resul
15530 74 20 63 6f 64 65 5d 20 6d 69 67 68 74 20 62 65  t code] might be
15540 20 72 65 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20   returned as.** 
15550 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c  well..**.** [SQL
15560 49 54 45 5f 42 55 53 59 5d 20 6d 65 61 6e 73 20  ITE_BUSY] means 
15570 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
15580 65 20 65 6e 67 69 6e 65 20 77 61 73 20 75 6e 61  e engine was una
15590 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74  ble to acquire t
155a0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6c  he.** database l
155b0 6f 63 6b 73 20 69 74 20 6e 65 65 64 73 20 74 6f  ocks it needs to
155c0 20 64 6f 20 69 74 73 20 6a 6f 62 2e 20 20 49 66   do its job.  If
155d0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
155e0 73 20 61 20 43 4f 4d 4d 49 54 0a 2a 2a 20 6f 72  s a COMMIT.** or
155f0 20 6f 63 63 75 72 73 20 6f 75 74 73 69 64 65 20   occurs outside 
15600 6f 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 74  of an explicit t
15610 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e  ransaction, then
15620 20 79 6f 75 20 63 61 6e 20 72 65 74 72 79 20 74   you can retry t
15630 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e  he.** statement.
15640 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65    If the stateme
15650 6e 74 20 69 73 20 6e 6f 74 20 61 20 43 4f 4d 4d  nt is not a COMM
15660 49 54 20 61 6e 64 20 6f 63 63 75 72 73 20 77 69  IT and occurs wi
15670 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69 63  thin a.** explic
15680 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  it transaction t
15690 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72  hen you should r
156a0 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e  ollback the tran
156b0 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a  saction before.*
156c0 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a  * continuing..**
156d0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45  .** [SQLITE_DONE
156e0 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  ] means that the
156f0 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66   statement has f
15700 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e  inished executin
15710 67 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c  g.** successfull
15720 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  y.  sqlite3_step
15730 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  () should not be
15740 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e   called again on
15750 20 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a   this virtual.**
15760 20 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74   machine without
15770 20 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b   first calling [
15780 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
15790 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69   to reset the vi
157a0 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
157b0 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69   back to its ini
157c0 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a  tial state..**.*
157d0 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61  * If the SQL sta
157e0 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65  tement being exe
157f0 63 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e  cuted returns an
15800 79 20 64 61 74 61 2c 20 74 68 65 6e 20 0a 2a 2a  y data, then .**
15810 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 69 73   [SQLITE_ROW] is
15820 20 72 65 74 75 72 6e 65 64 20 65 61 63 68 20 74   returned each t
15830 69 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66  ime a new row of
15840 20 64 61 74 61 20 69 73 20 72 65 61 64 79 0a 2a   data is ready.*
15850 2a 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  * for processing
15860 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
15870 54 68 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62  The values may b
15880 65 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67  e accessed using
15890 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33  .** the [sqlite3
158a0 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 7c 20 63 6f  _column_int | co
158b0 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63  lumn access func
158c0 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74  tions]..** sqlit
158d0 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c  e3_step() is cal
158e0 6c 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 74  led again to ret
158f0 72 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 72  rieve the next r
15900 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 0a  ow of data..** .
15910 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ** [SQLITE_ERROR
15920 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72  ] means that a r
15930 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73  un-time error (s
15940 75 63 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61  uch as a constra
15950 69 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e  int.** violation
15960 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20  ) has occurred. 
15970 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
15980 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61  should not be ca
15990 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a  lled again on.**
159a0 20 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e   the VM. More in
159b0 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65  formation may be
159c0 20 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e   found by callin
159d0 67 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  g [sqlite3_errms
159e0 67 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68  g()]..** With th
159f0 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61  e legacy interfa
15a00 63 65 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69  ce, a more speci
15a10 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28  fic error code (
15a20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 20 5b 53 51 4c  example:.** [SQL
15a30 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2c 20  ITE_INTERRUPT], 
15a40 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2c  [SQLITE_SCHEMA],
15a50 20 5b 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54   [SQLITE_CORRUPT
15a60 5d 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29  ], and so forth)
15a70 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 62 74 61 69  .** can be obtai
15a80 6e 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b  ned by calling [
15a90 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
15aa0 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69   on the.** [sqli
15ab0 74 65 33 5f 73 74 6d 74 20 7c 20 70 72 65 70 61  te3_stmt | prepa
15ac0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20  red statement]. 
15ad0 20 49 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74   In the "v2" int
15ae0 65 72 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d  erface,.** the m
15af0 6f 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72  ore specific err
15b00 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
15b10 6e 65 64 20 64 69 72 65 63 74 6c 79 20 62 79 20  ned directly by 
15b20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a  sqlite3_step()..
15b30 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49  **.** [SQLITE_MI
15b40 53 55 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74  SUSE] means that
15b50 20 74 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e   the this routin
15b60 65 20 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61  e was called ina
15b70 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a  ppropriately..**
15b80 20 50 65 72 68 61 70 73 20 69 74 20 77 61 73 20   Perhaps it was 
15b90 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b 73 71 6c  called on a [sql
15ba0 69 74 65 33 5f 73 74 6d 74 20 7c 20 70 72 65 70  ite3_stmt | prep
15bb0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
15bc0 74 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65  that has.** alre
15bd0 61 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74 65  ady been [sqlite
15be0 33 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e  3_finalize | fin
15bf0 61 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e  alized] or on on
15c00 65 20 74 68 61 74 20 68 61 64 20 0a 2a 2a 20 70  e that had .** p
15c10 72 65 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e  reviously return
15c20 65 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ed [SQLITE_ERROR
15c30 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e  ] or [SQLITE_DON
15c40 45 5d 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64  E].  Or it could
15c50 0a 2a 2a 20 62 65 20 74 68 65 20 63 61 73 65 20  .** be the case 
15c60 74 68 61 74 20 74 68 65 20 73 61 6d 65 20 64 61  that the same da
15c70 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
15c80 6e 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20  n is being used 
15c90 62 79 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72  by two or.** mor
15ca0 65 20 74 68 72 65 61 64 73 20 61 74 20 74 68 65  e threads at the
15cb0 20 73 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20   same moment in 
15cc0 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47  time..**.** <b>G
15cd0 6f 6f 66 79 20 49 6e 74 65 72 66 61 63 65 20 41  oofy Interface A
15ce0 6c 65 72 74 3a 3c 2f 62 3e 0a 2a 2a 20 49 6e 20  lert:</b>.** In 
15cf0 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72  the legacy inter
15d00 66 61 63 65 2c 20 0a 2a 2a 20 74 68 65 20 73 71  face, .** the sq
15d10 6c 69 74 65 33 5f 73 74 65 70 28 29 20 41 50 49  lite3_step() API
15d20 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
15d30 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20  a generic error 
15d40 63 6f 64 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45  code,.** [SQLITE
15d50 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69  _ERROR], followi
15d60 6e 67 20 61 6e 79 20 65 72 72 6f 72 20 6f 74 68  ng any error oth
15d70 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f  er than [SQLITE_
15d80 42 55 53 59 5d 0a 2a 2a 20 61 6e 64 20 5b 53 51  BUSY].** and [SQ
15d90 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59  LITE_MISUSE].  Y
15da0 6f 75 20 6d 75 73 74 20 63 61 6c 6c 20 5b 73 71  ou must call [sq
15db0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
15dc0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  r.** [sqlite3_fi
15dd0 6e 61 6c 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64  nalize()] in ord
15de0 65 72 20 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f  er to find one o
15df0 66 20 74 68 65 20 73 70 65 63 69 66 69 63 0a 2a  f the specific.*
15e00 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  * [SQLITE_ERROR 
15e10 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20  | result codes] 
15e20 74 68 61 74 20 62 65 74 74 65 72 20 64 65 73 63  that better desc
15e30 72 69 62 65 73 20 74 68 65 20 65 72 72 6f 72 2e  ribes the error.
15e40 0a 2a 2a 20 57 65 20 61 64 6d 69 74 20 74 68 61  .** We admit tha
15e50 74 20 74 68 69 73 20 69 73 20 61 20 67 6f 6f 66  t this is a goof
15e60 79 20 64 65 73 69 67 6e 2e 20 20 54 68 65 20 70  y design.  The p
15e70 72 6f 62 6c 65 6d 20 68 61 73 20 62 65 65 6e 20  roblem has been 
15e80 66 69 78 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  fixed.** with th
15e90 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  e "v2" interface
15ea0 2e 20 20 49 66 20 79 6f 75 20 70 72 65 70 61 72  .  If you prepar
15eb0 65 20 61 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51  e all of your SQ
15ec0 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  L statements.** 
15ed0 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71  using either [sq
15ee0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
15ef0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
15f00 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20  prepare16_v2()] 
15f10 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68  instead.** of th
15f20 65 20 6c 65 67 61 63 79 20 5b 73 71 6c 69 74 65  e legacy [sqlite
15f30 33 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64  3_prepare()] and
15f40 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
15f50 65 31 36 28 29 5d 2c 20 74 68 65 6e 20 74 68 65  e16()], then the
15f60 20 0a 2a 2a 20 6d 6f 72 65 20 73 70 65 63 69 66   .** more specif
15f70 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ic [SQLITE_ERROR
15f80 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d   | result codes]
15f90 20 61 72 65 20 72 65 74 75 72 6e 65 64 20 64 69   are returned di
15fa0 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c  rectly.** by sql
15fb0 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 54 68  ite3_step().  Th
15fc0 65 20 75 73 65 20 6f 66 20 74 68 65 20 22 76 32  e use of the "v2
15fd0 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72  " interface is r
15fe0 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2f 0a 53  ecommended..*/.S
15ff0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
16000 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69 74  lite3_step(sqlit
16010 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a  e3_stmt*);../*.*
16020 2a 20 43 41 50 49 33 52 45 46 3a 0a 2a 2a 0a 2a  * CAPI3REF:.**.*
16030 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
16040 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e  ber of values in
16050 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
16060 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
16070 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  et..**.** After 
16080 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
16090 65 33 5f 73 74 65 70 28 29 5d 20 74 68 61 74 20  e3_step()] that 
160a0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
160b0 52 4f 57 5d 2c 20 74 68 69 73 20 72 6f 75 74 69  ROW], this routi
160c0 6e 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72  ne.** will retur
160d0 6e 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  n the same value
160e0 20 61 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33   as the [sqlite3
160f0 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 5d  _column_count()]
16100 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 41 66   function..** Af
16110 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ter [sqlite3_ste
16120 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65  p()] has returne
16130 64 20 61 6e 20 5b 53 51 4c 49 54 45 5f 44 4f 4e  d an [SQLITE_DON
16140 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  E], [SQLITE_BUSY
16150 5d 2c 20 6f 72 0a 2a 2a 20 61 20 5b 53 51 4c 49  ], or.** a [SQLI
16160 54 45 5f 45 52 52 4f 52 20 7c 20 65 72 72 6f 72  TE_ERROR | error
16170 20 63 6f 64 65 5d 2c 20 6f 72 20 62 65 66 6f 72   code], or befor
16180 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e [sqlite3_step(
16190 29 5d 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20  )] has been .** 
161a0 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 5b 73  called on the [s
161b0 71 6c 69 74 65 33 5f 73 74 6d 74 20 7c 20 70 72  qlite3_stmt | pr
161c0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
161d0 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  ] for the first 
161e0 74 69 6d 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f  time,.** this ro
161f0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a 65  utine returns ze
16200 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ro..*/.SQLITE_AP
16210 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 61  I int sqlite3_da
16220 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  ta_count(sqlite3
16230 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a  _stmt *pStmt);..
16240 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
16250 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74 61  Fundamental Data
16260 74 79 70 65 73 0a 2a 2a 0a 2a 2a 20 45 76 65 72  types.**.** Ever
16270 79 20 76 61 6c 75 65 20 69 6e 20 53 51 4c 69 74  y value in SQLit
16280 65 20 68 61 73 20 6f 6e 65 20 6f 66 20 66 69 76  e has one of fiv
16290 65 20 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61  e fundamental da
162a0 74 61 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c  tatypes:.**.** <
162b0 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62  ul>.** <li> 64-b
162c0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
162d0 72 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74  r.** <li> 64-bit
162e0 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70   IEEE floating p
162f0 6f 69 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c  oint number.** <
16300 6c 69 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c  li> string.** <l
16310 69 3e 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20  i> BLOB.** <li> 
16320 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  NULL.** </ul>.**
16330 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
16340 6e 74 73 20 61 72 65 20 63 6f 64 65 73 20 66 6f  nts are codes fo
16350 72 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20  r each of those 
16360 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  types..**.** Not
16370 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 49 54  e that the SQLIT
16380 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 6e 74 20  E_TEXT constant 
16390 77 61 73 20 61 6c 73 6f 20 75 73 65 64 20 69 6e  was also used in
163a0 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
163b0 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70 6c  2.** for a compl
163c0 65 74 65 6c 79 20 64 69 66 66 65 72 65 6e 74 20  etely different 
163d0 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 74 77 61  meaning.  Softwa
163e0 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67  re that links ag
163f0 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a 20 53 51  ainst both.** SQ
16400 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 20 61  Lite version 2 a
16410 6e 64 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  nd SQLite versio
16420 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 65 20 53  n 3 should use S
16430 51 4c 49 54 45 33 5f 54 45 58 54 20 6e 6f 74 0a  QLITE3_TEXT not.
16440 2a 2a 20 53 51 4c 49 54 45 5f 54 45 58 54 2e 0a  ** SQLITE_TEXT..
16450 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
16460 45 5f 49 4e 54 45 47 45 52 20 20 31 0a 23 64 65  E_INTEGER  1.#de
16470 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41  fine SQLITE_FLOA
16480 54 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53  T    2.#define S
16490 51 4c 49 54 45 5f 42 4c 4f 42 20 20 20 20 20 34  QLITE_BLOB     4
164a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
164b0 4e 55 4c 4c 20 20 20 20 20 35 0a 23 69 66 64 65  NULL     5.#ifde
164c0 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 20  f SQLITE_TEXT.# 
164d0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58  undef SQLITE_TEX
164e0 54 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  T.#else.# define
164f0 20 53 51 4c 49 54 45 5f 54 45 58 54 20 20 20 20   SQLITE_TEXT    
16500 20 33 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e   3.#endif.#defin
16510 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 20 20  e SQLITE3_TEXT  
16520 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49     3../*.** CAPI
16530 33 52 45 46 3a 20 52 65 73 75 6c 74 73 20 56 61  3REF: Results Va
16540 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 75 65 72  lues From A Quer
16550 79 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  y.**.** These ro
16560 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e  utines return in
16570 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 0a  formation about.
16580 2a 2a 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75  ** a single colu
16590 6d 6e 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  mn of the curren
165a0 74 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20  t result row of 
165b0 61 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76 65  a query.  In eve
165c0 72 79 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 66  ry.** case the f
165d0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
165e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
165f0 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  e .** [sqlite3_s
16600 74 6d 74 20 7c 20 53 51 4c 20 73 74 61 74 65 6d  tmt | SQL statem
16610 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 65 69  ent] that is bei
16620 6e 67 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20  ng.** evaluated 
16630 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74  (the [sqlite3_st
16640 6d 74 2a 5d 20 74 68 61 74 20 77 61 73 20 72 65  mt*] that was re
16650 74 75 72 6e 65 64 20 66 72 6f 6d 20 0a 2a 2a 20  turned from .** 
16660 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
16670 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66  _v2()] or one of
16680 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 61   its variants) a
16690 6e 64 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  nd.** the second
166a0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
166b0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f   index of the co
166c0 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69  lumn for which i
166d0 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20 73  nformation .** s
166e0 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65  hould be returne
166f0 64 2e 20 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73  d.  The left-mos
16700 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
16710 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 68 61  result set.** ha
16720 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 2e  s an index of 0.
16730 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51  .**.** If the SQ
16740 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e  L statement is n
16750 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  ot currently poi
16760 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f  nt to a valid ro
16770 77 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20  w, or if the.** 
16780 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  the column index
16790 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
167a0 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
167b0 75 6e 64 65 66 69 6e 65 64 2e 20 0a 2a 2a 20 54  undefined. .** T
167c0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61  hese routines ma
167d0 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64  y only be called
167e0 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72   when the most r
167f0 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a  ecent call to.**
16800 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
16810 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b  ] has returned [
16820 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20  SQLITE_ROW] and 
16830 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69  neither.** [sqli
16840 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72  te3_reset()] nor
16850 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
16860 7a 65 28 29 5d 20 68 61 73 20 62 65 65 6e 20 63  ze()] has been c
16870 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 6c 79  all subsequently
16880 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74  ..** If any of t
16890 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
168a0 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 5b  e called after [
168b0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
168c0 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
168d0 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61  finalize()] or a
168e0 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74  fter [sqlite3_st
168f0 65 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e  ep()] has return
16900 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20  ed.** something 
16910 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49  other than [SQLI
16920 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 73  TE_ROW], the res
16930 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e  ults are undefin
16940 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74  ed..** If [sqlit
16950 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73  e3_step()] or [s
16960 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
16970 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  or [sqlite3_fina
16980 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 63  lize()].** are c
16990 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 66  alled from a dif
169a0 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 77 68  ferent thread wh
169b0 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65  ile any of these
169c0 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65   routines.** are
169d0 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 74   pending, then t
169e0 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75  he results are u
169f0 6e 64 65 66 69 6e 65 64 2e 20 20 0a 2a 2a 0a 2a  ndefined.  .**.*
16a00 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  * The sqlite3_co
16a10 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 74  lumn_type() rout
16a20 69 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20  ine returns .** 
16a30 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20  [SQLITE_INTEGER 
16a40 7c 20 64 61 74 61 74 79 70 65 20 63 6f 64 65 5d  | datatype code]
16a50 20 66 6f 72 20 74 68 65 20 69 6e 69 74 69 61 6c   for the initial
16a60 20 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 6f 66   data type.** of
16a70 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75   the result colu
16a80 6d 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65  mn.  The returne
16a90 64 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f  d value is one o
16aa0 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  f [SQLITE_INTEGE
16ab0 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46  R],.** [SQLITE_F
16ac0 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54  LOAT], [SQLITE_T
16ad0 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 4c  EXT], [SQLITE_BL
16ae0 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  OB], or [SQLITE_
16af0 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 6c 75  NULL].  The valu
16b00 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79  e.** returned by
16b10 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
16b20 74 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 20 6d  type() is only m
16b30 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e 6f 20  eaningful if no 
16b40 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69  type.** conversi
16b50 6f 6e 73 20 68 61 76 65 20 6f 63 63 75 72 72 65  ons have occurre
16b60 64 20 61 73 20 64 65 73 63 72 69 62 65 64 20 62  d as described b
16b70 65 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 20 74  elow.  After a t
16b80 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a  ype conversion,.
16b90 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  ** the value ret
16ba0 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
16bb0 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69  _column_type() i
16bc0 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 46 75  s undefined.  Fu
16bd0 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 73  ture.** versions
16be0 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 63   of SQLite may c
16bf0 68 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69  hange the behavi
16c00 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 63 6f  or of sqlite3_co
16c10 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a 20 66  lumn_type().** f
16c20 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 65 20  ollowing a type 
16c30 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a  conversion..**.*
16c40 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20  * If the result 
16c50 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 54 46  is a BLOB or UTF
16c60 2d 38 20 73 74 72 69 6e 67 20 74 68 65 6e 20 74  -8 string then t
16c70 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  he sqlite3_colum
16c80 6e 5f 62 79 74 65 73 28 29 20 0a 2a 2a 20 72 6f  n_bytes() .** ro
16c90 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
16ca0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
16cb0 73 20 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f  s in that BLOB o
16cc0 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 20  r string..** If 
16cd0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20  the result is a 
16ce0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 74  UTF-16 string, t
16cf0 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  hen sqlite3_colu
16d00 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 65  mn_bytes() conve
16d10 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e  rts.** the strin
16d20 67 20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 74  g to UTF-8 and t
16d30 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  hen returns the 
16d40 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e  number of bytes.
16d50 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c  .** If the resul
16d60 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20 76  t is a numeric v
16d70 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74 65  alue then sqlite
16d80 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
16d90 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 65   uses.** [sqlite
16da0 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f  3_snprintf()] to
16db0 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 76 61   convert that va
16dc0 6c 75 65 20 74 6f 20 61 20 55 54 46 2d 38 20 73  lue to a UTF-8 s
16dd0 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e  tring and return
16de0 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  s.** the number 
16df0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 74  of bytes in that
16e00 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 20   string..** The 
16e10 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 64  value returned d
16e20 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20  oes not include 
16e30 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  the zero termina
16e40 74 6f 72 20 61 74 20 74 68 65 20 65 6e 64 0a 2a  tor at the end.*
16e50 2a 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e  * of the string.
16e60 20 20 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74    For clarity: t
16e70 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
16e80 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  d is the number 
16e90 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74  of.** bytes in t
16ea0 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74  he string, not t
16eb0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
16ec0 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 53  racters..**.** S
16ed0 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20  trings returned 
16ee0 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  by sqlite3_colum
16ef0 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c  n_text() and sql
16f00 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
16f10 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 7a 65  16(),.** even ze
16f20 72 6f 2d 6c 65 6e 67 74 68 20 73 74 72 69 6e 67  ro-length string
16f30 73 2c 20 61 72 65 20 61 6c 77 61 79 73 20 7a 65  s, are always ze
16f40 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 20  ro terminated.  
16f50 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  The return.** va
16f60 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  lue from sqlite3
16f70 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66  _column_blob() f
16f80 6f 72 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68  or a zero-length
16f90 20 62 6c 6f 62 20 69 73 20 61 6e 20 61 72 62 69   blob is an arbi
16fa0 74 72 61 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72  trary.** pointer
16fb0 2c 20 70 6f 73 73 69 62 6c 79 20 65 76 65 6e 20  , possibly even 
16fc0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
16fd0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
16fe0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
16ff0 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69  () routine is si
17000 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33  milar to sqlite3
17010 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a  _column_bytes().
17020 2a 2a 20 62 75 74 20 6c 65 61 76 65 73 20 74 68  ** but leaves th
17030 65 20 72 65 73 75 6c 74 20 69 6e 20 55 54 46 2d  e result in UTF-
17040 31 36 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54  16 instead of UT
17050 46 2d 38 2e 20 20 0a 2a 2a 20 54 68 65 20 7a 65  F-8.  .** The ze
17060 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73  ro terminator is
17070 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e   not included in
17080 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a   this count..**.
17090 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
170a0 73 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e  s attempt to con
170b0 76 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 77  vert the value w
170c0 68 65 72 65 20 61 70 70 72 6f 70 72 69 61 74 65  here appropriate
170d0 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c  .  For.** exampl
170e0 65 2c 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e  e, if the intern
170f0 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  al representatio
17100 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61  n is FLOAT and a
17110 20 74 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20   text result.** 
17120 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73  is requested, [s
17130 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
17140 29 5d 20 69 73 20 75 73 65 64 20 69 6e 74 65 72  )] is used inter
17150 6e 61 6c 6c 79 20 74 6f 20 64 6f 20 74 68 65 20  nally to do the 
17160 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 61 75  conversion.** au
17170 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68  tomatically.  Th
17180 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
17190 65 20 64 65 74 61 69 6c 73 20 74 68 65 20 63 6f  e details the co
171a0 6e 76 65 72 73 69 6f 6e 73 20 74 68 61 74 0a 2a  nversions that.*
171b0 2a 20 61 72 65 20 61 70 70 6c 69 65 64 3a 0a 2a  * are applied:.*
171c0 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
171d0 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64  >.** <table bord
171e0 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c  er="1">.** <tr><
171f0 74 68 3e 20 49 6e 74 65 72 6e 61 6c 3c 62 72 3e  th> Internal<br>
17200 54 79 70 65 20 3c 74 68 3e 20 52 65 71 75 65 73  Type <th> Reques
17210 74 65 64 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e  ted<br>Type <th>
17220 20 20 43 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a    Conversion.**.
17230 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
17240 4c 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45  L    <td> INTEGE
17250 52 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20  R   <td> Result 
17260 69 73 20 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  is 0.** <tr><td>
17270 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20    NULL    <td>  
17280 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 52 65  FLOAT    <td> Re
17290 73 75 6c 74 20 69 73 20 30 2e 30 0a 2a 2a 20 3c  sult is 0.0.** <
172a0 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20  tr><td>  NULL   
172b0 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20   <td>   TEXT    
172c0 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 4e  <td> Result is N
172d0 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c  ULL pointer.** <
172e0 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20  tr><td>  NULL   
172f0 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20   <td>   BLOB    
17300 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 4e  <td> Result is N
17310 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c  ULL pointer.** <
17320 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20  tr><td> INTEGER 
17330 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
17340 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f  <td> Convert fro
17350 6d 20 69 6e 74 65 67 65 72 20 74 6f 20 66 6c 6f  m integer to flo
17360 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49  at.** <tr><td> I
17370 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 54  NTEGER  <td>   T
17380 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53 43 49  EXT    <td> ASCI
17390 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74  I rendering of t
173a0 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74  he integer.** <t
173b0 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20  r><td> INTEGER  
173c0 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c  <td>   BLOB    <
173d0 74 64 3e 20 53 61 6d 65 20 61 73 20 66 6f 72 20  td> Same as for 
173e0 49 4e 54 45 47 45 52 2d 3e 54 45 58 54 0a 2a 2a  INTEGER->TEXT.**
173f0 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54   <tr><td>  FLOAT
17400 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20     <td> INTEGER 
17410 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66    <td> Convert f
17420 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 69 6e 74  rom float to int
17430 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  eger.** <tr><td>
17440 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20    FLOAT   <td>  
17450 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53   TEXT    <td> AS
17460 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66  CII rendering of
17470 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74   the float.** <t
17480 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20  r><td>  FLOAT   
17490 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c  <td>   BLOB    <
174a0 74 64 3e 20 53 61 6d 65 20 61 73 20 46 4c 4f 41  td> Same as FLOA
174b0 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c  T->TEXT.** <tr><
174c0 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64  td>  TEXT    <td
174d0 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e  > INTEGER   <td>
174e0 20 55 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c   Use atoi().** <
174f0 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20  tr><td>  TEXT   
17500 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
17510 3c 74 64 3e 20 55 73 65 20 61 74 6f 66 28 29 0a  <td> Use atof().
17520 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58  ** <tr><td>  TEX
17530 54 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42  T    <td>   BLOB
17540 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63 68 61 6e      <td> No chan
17550 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ge.** <tr><td>  
17560 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 49 4e 54  BLOB    <td> INT
17570 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76  EGER   <td> Conv
17580 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e  ert to TEXT then
17590 20 75 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c   use atoi().** <
175a0 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20  tr><td>  BLOB   
175b0 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
175c0 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20  <td> Convert to 
175d0 54 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74  TEXT then use at
175e0 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  of().** <tr><td>
175f0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20    BLOB    <td>  
17600 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 64   TEXT    <td> Ad
17610 64 20 61 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  d a zero termina
17620 74 6f 72 20 69 66 20 6e 65 65 64 65 64 0a 2a 2a  tor if needed.**
17630 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62   </table>.** </b
17640 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
17650 20 54 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65   The table above
17660 20 6d 61 6b 65 73 20 72 65 66 65 72 65 6e 63 65   makes reference
17670 20 74 6f 20 73 74 61 6e 64 61 72 64 20 43 20 6c   to standard C l
17680 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73  ibrary functions
17690 20 61 74 6f 69 28 29 0a 2a 2a 20 61 6e 64 20 61   atoi().** and a
176a0 74 6f 66 28 29 2e 20 20 53 51 4c 69 74 65 20 64  tof().  SQLite d
176b0 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 75  oes not really u
176c0 73 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  se these functio
176d0 6e 73 2e 20 20 49 74 20 68 61 73 20 69 74 73 0a  ns.  It has its.
176e0 2a 2a 20 6f 6e 20 65 71 75 61 76 61 6c 65 6e 74  ** on equavalent
176f0 20 69 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e   internal routin
17700 65 73 2e 20 20 54 68 65 20 61 74 6f 69 28 29 20  es.  The atoi() 
17710 61 6e 64 20 61 74 6f 66 28 29 20 6e 61 6d 65 73  and atof() names
17720 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 69 6e 20   are.** used in 
17730 74 68 65 20 74 61 62 6c 65 20 66 6f 72 20 62 72  the table for br
17740 65 76 69 74 79 20 61 6e 64 20 62 65 63 61 75 73  evity and becaus
17750 65 20 74 68 65 79 20 61 72 65 20 66 61 6d 69 6c  e they are famil
17760 69 61 72 20 74 6f 20 6d 6f 73 74 0a 2a 2a 20 43  iar to most.** C
17770 20 70 72 6f 67 72 61 6d 6d 65 72 73 2e 0a 2a 2a   programmers..**
17780 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68  .** Note that wh
17790 65 6e 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69  en type conversi
177a0 6f 6e 73 20 6f 63 63 75 72 2c 20 70 6f 69 6e 74  ons occur, point
177b0 65 72 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ers returned by 
177c0 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 73 20 74  prior.** calls t
177d0 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  o sqlite3_column
177e0 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33  _blob(), sqlite3
177f0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 20  _column_text(), 
17800 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  and/or.** sqlite
17810 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
17820 29 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 64  ) may be invalid
17830 61 74 65 64 2e 20 0a 2a 2a 20 54 79 70 65 20 63  ated. .** Type c
17840 6f 6e 76 65 72 73 69 6f 6e 73 20 61 6e 64 20 70  onversions and p
17850 6f 69 6e 74 65 72 20 69 6e 76 61 6c 69 64 61 74  ointer invalidat
17860 69 6f 6e 73 20 6d 69 67 68 74 20 6f 63 63 75 72  ions might occur
17870 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  .** in the follo
17880 77 69 6e 67 20 63 61 73 65 73 3a 0a 2a 2a 0a 2a  wing cases:.**.*
17890 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 3c 70  * <ul>.** <li><p
178a0 3e 20 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63  >  The initial c
178b0 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f 42  ontent is a BLOB
178c0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c   and sqlite3_col
178d0 75 6d 6e 5f 74 65 78 74 28 29 20 0a 2a 2a 20 20  umn_text() .**  
178e0 20 20 20 20 20 20 20 20 6f 72 20 73 71 6c 69 74          or sqlit
178f0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
17900 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41  () is called.  A
17910 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72   zero-terminator
17920 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 20   might.**       
17930 20 20 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64     need to be ad
17940 64 65 64 20 74 6f 20 74 68 65 20 73 74 72 69 6e  ded to the strin
17950 67 2e 3c 2f 70 3e 3c 2f 6c 69 3e 0a 2a 2a 0a 2a  g.</p></li>.**.*
17960 2a 20 3c 6c 69 3e 3c 70 3e 20 20 54 68 65 20 69  * <li><p>  The i
17970 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69  nitial content i
17980 73 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64  s UTF-8 text and
17990 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
179a0 62 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20  bytes16() or.** 
179b0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
179c0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29  _column_text16()
179d0 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65   is called.  The
179e0 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65   content must be
179f0 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20   converted.**   
17a00 20 20 20 20 20 20 20 74 6f 20 55 54 46 2d 31 36         to UTF-16
17a10 2e 3c 2f 70 3e 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a  .</p></li>.**.**
17a20 20 3c 6c 69 3e 3c 70 3e 20 20 54 68 65 20 69 6e   <li><p>  The in
17a30 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73  itial content is
17a40 20 55 54 46 2d 31 36 20 74 65 78 74 20 61 6e 64   UTF-16 text and
17a50 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
17a60 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20  bytes() or.**   
17a70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63         sqlite3_c
17a80 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 69 73 20  olumn_text() is 
17a90 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e  called.  The con
17aa0 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e  tent must be con
17ab0 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 20  verted.**       
17ac0 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f 70 3e     to UTF-8.</p>
17ad0 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  </li>.** </ul>.*
17ae0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73  *.** Conversions
17af0 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62   between UTF-16b
17b00 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61  e and UTF-16le a
17b10 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69  re always done i
17b20 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a  n place and do.*
17b30 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65  * not invalidate
17b40 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72   a prior pointer
17b50 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72  , though of cour
17b60 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  se the content o
17b70 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20  f the buffer.** 
17b80 74 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70  that the prior p
17b90 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 6f  ointer points to
17ba0 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20   will have been 
17bb0 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72  modified.  Other
17bc0 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e   kinds.** of con
17bd0 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65  version are done
17be0 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69   in place when i
17bf0 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62  t is possible, b
17c00 75 74 20 73 6f 6d 65 74 69 6d 65 20 69 74 20 69  ut sometime it i
17c10 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c  s.** not possibl
17c20 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 65 20 63  e and in those c
17c30 61 73 65 73 20 70 72 69 6f 72 20 70 6f 69 6e 74  ases prior point
17c40 65 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61  ers are invalida
17c50 74 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ted.  .**.** The
17c60 20 73 61 66 65 73 74 20 61 6e 64 20 65 61 73 69   safest and easi
17c70 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 20  est to remember 
17c80 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 6e 76  policy is to inv
17c90 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e  oke these routin
17ca0 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 20  es.** in one of 
17cb0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 61  the following wa
17cc0 79 73 3a 0a 2a 2a 0a 2a 2a 20 20 3c 75 6c 3e 0a  ys:.**.**  <ul>.
17cd0 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f  **  <li>sqlite3_
17ce0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f  column_text() fo
17cf0 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65  llowed by sqlite
17d00 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
17d10 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71  </li>.**  <li>sq
17d20 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
17d30 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  b() followed by 
17d40 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
17d50 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20  ytes()</li>.**  
17d60 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75  <li>sqlite3_colu
17d70 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c 6c  mn_text16() foll
17d80 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  owed by sqlite3_
17d90 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
17da0 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 2f 75 6c 3e 0a  </li>.**  </ul>.
17db0 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
17dc0 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64  ords, you should
17dd0 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f   call sqlite3_co
17de0 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 20 73 71 6c  lumn_text(), sql
17df0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
17e00 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65  (),.** or sqlite
17e10 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
17e20 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65  ) first to force
17e30 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f   the result into
17e40 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20   the desired.** 
17e50 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76  format, then inv
17e60 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  oke sqlite3_colu
17e70 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 20 73 71  mn_bytes() or sq
17e80 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
17e90 65 73 31 36 28 29 20 74 6f 0a 2a 2a 20 66 69 6e  es16() to.** fin
17ea0 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  d the size of th
17eb0 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f  e result.  Do no
17ec0 74 20 6d 69 78 20 63 61 6c 6c 20 74 6f 20 73 71  t mix call to sq
17ed0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
17ee0 74 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  t() or.** sqlite
17ef0 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20  3_column_blob() 
17f00 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71  with calls to sq
17f10 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
17f20 65 73 31 36 28 29 2e 20 20 41 6e 64 20 64 6f 20  es16().  And do 
17f30 6e 6f 74 0a 2a 2a 20 6d 69 78 20 63 61 6c 6c 73  not.** mix calls
17f40 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
17f50 6d 6e 5f 74 65 78 74 31 36 28 29 20 77 69 74 68  mn_text16() with
17f60 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
17f70 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
17f80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ..**.** The poin
17f90 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 61 72  ters returned ar
17fa0 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 61 20  e valid until a 
17fb0 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20  type conversion 
17fc0 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 65 73  occurs as.** des
17fd0 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20 6f 72  cribed above, or
17fe0 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f   until [sqlite3_
17ff0 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69  step()] or [sqli
18000 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a  te3_reset()] or.
18010 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  ** [sqlite3_fina
18020 6c 69 7a 65 28 29 5d 20 69 73 20 63 61 6c 6c 65  lize()] is calle
18030 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 73  d.  The memory s
18040 70 61 63 65 20 75 73 65 64 20 74 6f 20 68 6f 6c  pace used to hol
18050 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 6e 64  d strings.** and
18060 20 62 6c 6f 62 73 20 69 73 20 66 72 65 65 64 20   blobs is freed 
18070 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20  automatically.  
18080 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61  Do <b>not</b> pa
18090 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20  ss the pointers 
180a0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c  returned.** [sql
180b0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
180c0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 6f  ()], [sqlite3_co
180d0 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 65 74  lumn_text()], et
180e0 63 2e 20 69 6e 74 6f 20 0a 2a 2a 20 5b 73 71 6c  c. into .** [sql
180f0 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a  ite3_free()]..**
18100 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  .** If a memory 
18110 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
18120 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74   occurs during t
18130 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66  he evaluation of
18140 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65   any.** of these
18150 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 66   routines, a def
18160 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20 72 65  ault value is re
18170 74 75 72 6e 65 64 2e 20 20 54 68 65 20 64 65 66  turned.  The def
18180 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 73  ault value.** is
18190 20 65 69 74 68 65 72 20 74 68 65 20 69 6e 74 65   either the inte
181a0 67 65 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 74  ger 0, the float
181b0 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72  ing point number
181c0 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c 0a   0.0, or a NULL.
181d0 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 62  ** pointer.  Sub
181e0 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
181f0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64   [sqlite3_errcod
18200 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e  e()] will return
18210 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  .** [SQLITE_NOME
18220 4d 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  M]..*/.SQLITE_AP
18230 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
18240 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
18250 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  b(sqlite3_stmt*,
18260 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
18270 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
18280 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
18290 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
182a0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
182b0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
182c0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
182d0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
182e0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
182f0 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69  _API double sqli
18300 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c  te3_column_doubl
18310 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
18320 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
18330 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
18340 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71  e3_column_int(sq
18350 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
18360 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
18370 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  PI sqlite3_int64
18380 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
18390 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74  int64(sqlite3_st
183a0 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
183b0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
183c0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
183d0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
183e0 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ext(sqlite3_stmt
183f0 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51  *, int iCol);.SQ
18400 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
18410 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
18420 75 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74  umn_text16(sqlit
18430 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
18440 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ol);.SQLITE_API 
18450 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
18460 6d 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  mn_type(sqlite3_
18470 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
18480 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ;.SQLITE_API sql
18490 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69  ite3_value *sqli
184a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65  te3_column_value
184b0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
184c0 69 6e 74 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a  int iCol);../*.*
184d0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 73 74  * CAPI3REF: Dest
184e0 72 6f 79 20 41 20 50 72 65 70 61 72 65 64 20 53  roy A Prepared S
184f0 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 0a  tatement Object.
18500 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
18510 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 6e  3_finalize() fun
18520 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
18530 74 6f 20 64 65 6c 65 74 65 20 61 20 0a 2a 2a 20  to delete a .** 
18540 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 7c 20  [sqlite3_stmt | 
18550 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
18560 74 65 6d 65 6e 74 5d 2e 20 49 66 20 74 68 65 20  tement]. If the 
18570 73 74 61 74 65 6d 65 6e 74 20 77 61 73 0a 2a 2a  statement was.**
18580 20 65 78 65 63 75 74 65 64 20 73 75 63 63 65 73   executed succes
18590 73 66 75 6c 6c 79 2c 20 6f 72 20 6e 6f 74 20 65  sfully, or not e
185a0 78 65 63 75 74 65 64 20 61 74 20 61 6c 6c 2c 20  xecuted at all, 
185b0 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  then SQLITE_OK i
185c0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49  s returned..** I
185d0 66 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  f execution of t
185e0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69  he statement fai
185f0 6c 65 64 20 74 68 65 6e 20 61 6e 20 0a 2a 2a 20  led then an .** 
18600 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 7c 20  [SQLITE_ERROR | 
18610 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 5b  error code] or [
18620 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
18630 44 20 7c 20 65 78 74 65 6e 64 65 64 20 65 72 72  D | extended err
18640 6f 72 20 63 6f 64 65 5d 0a 2a 2a 20 69 73 20 72  or code].** is r
18650 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20  eturned. .**.** 
18660 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e  This routine can
18670 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e   be called at an
18680 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 74  y point during t
18690 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  he execution of 
186a0 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  the.** [sqlite3_
186b0 73 74 6d 74 20 7c 20 76 69 72 74 75 61 6c 20 6d  stmt | virtual m
186c0 61 63 68 69 6e 65 5d 2e 20 20 49 66 20 74 68 65  achine].  If the
186d0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
186e0 20 68 61 73 20 6e 6f 74 20 0a 2a 2a 20 63 6f 6d   has not .** com
186f0 70 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f 6e  pleted execution
18700 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
18710 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  ne is called, th
18720 61 74 20 69 73 20 6c 69 6b 65 0a 2a 2a 20 65 6e  at is like.** en
18730 63 6f 75 6e 74 65 72 69 6e 67 20 61 6e 20 65 72  countering an er
18740 72 6f 72 20 6f 72 20 61 6e 20 69 6e 74 65 72 72  ror or an interr
18750 75 70 74 2e 20 20 28 53 65 65 20 5b 73 71 6c 69  upt.  (See [sqli
18760 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 5d  te3_interrupt()]
18770 2e 29 20 0a 2a 2a 20 49 6e 63 6f 6d 70 6c 65 74  .) .** Incomplet
18780 65 20 75 70 64 61 74 65 73 20 6d 61 79 20 62 65  e updates may be
18790 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64   rolled back and
187a0 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 63 61   transactions ca
187b0 6e 63 65 6c 6c 65 64 2c 20 20 0a 2a 2a 20 64 65  ncelled,  .** de
187c0 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 63  pending on the c
187d0 69 72 63 75 6d 73 74 61 6e 63 65 73 2c 20 61 6e  ircumstances, an
187e0 64 20 74 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54  d the .** [SQLIT
187f0 45 5f 45 52 52 4f 52 20 7c 20 72 65 73 75 6c 74  E_ERROR | result
18800 20 63 6f 64 65 5d 20 72 65 74 75 72 6e 65 64 20   code] returned 
18810 77 69 6c 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f  will be [SQLITE_
18820 41 42 4f 52 54 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  ABORT]..*/.SQLIT
18830 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
18840 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  3_finalize(sqlit
18850 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
18860 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
18870 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 61 72  : Reset A Prepar
18880 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a  ed Statement Obj
18890 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ect.**.** The sq
188a0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 66 75  lite3_reset() fu
188b0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
188c0 20 74 6f 20 72 65 73 65 74 20 61 20 0a 2a 2a 20   to reset a .** 
188d0 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 7c 20  [sqlite3_stmt | 
188e0 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
188f0 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 2e 0a  tement] object..
18900 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 27 73 20  ** back to it's 
18910 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2c 20 72  initial state, r
18920 65 61 64 79 20 74 6f 20 62 65 20 72 65 2d 65 78  eady to be re-ex
18930 65 63 75 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 53  ecuted..** Any S
18940 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72  QL statement var
18950 69 61 62 6c 65 73 20 74 68 61 74 20 68 61 64 20  iables that had 
18960 76 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f 20  values bound to 
18970 74 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74 68  them using.** th
18980 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  e [sqlite3_bind_
18990 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 62  blob | sqlite3_b
189a0 69 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65 74  ind_*() API] ret
189b0 61 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65 73  ain their values
189c0 2e 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74 65  ..** Use [sqlite
189d0 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73  3_clear_bindings
189e0 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65  ()] to reset the
189f0 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 53 51   bindings..*/.SQ
18a00 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
18a10 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74  ite3_reset(sqlit
18a20 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
18a30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
18a40 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65 64 65  : Create Or Rede
18a50 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74 69 6f  fine SQL Functio
18a60 6e 73 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ns.**.** The fol
18a70 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74  lowing two funct
18a80 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f  ions are used to
18a90 20 61 64 64 20 53 51 4c 20 66 75 6e 63 74 69 6f   add SQL functio
18aa0 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73  ns or aggregates
18ab0 0a 2a 2a 20 6f 72 20 74 6f 20 72 65 64 65 66 69  .** or to redefi
18ac0 6e 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  ne the behavior 
18ad0 6f 66 20 65 78 69 73 74 69 6e 67 20 53 51 4c 20  of existing SQL 
18ae0 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67  functions or agg
18af0 72 65 67 61 74 65 73 2e 20 20 54 68 65 0a 2a 2a  regates.  The.**
18b00 20 64 69 66 66 65 72 65 6e 63 65 20 6f 6e 6c 79   difference only
18b10 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f   between the two
18b20 20 69 73 20 74 68 61 74 20 74 68 65 20 73 65 63   is that the sec
18b30 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74  ond parameter, t
18b40 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68  he.** name of th
18b50 65 20 28 73 63 61 6c 61 72 29 20 66 75 6e 63 74  e (scalar) funct
18b60 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65  ion or aggregate
18b70 2c 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20  , is encoded in 
18b80 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c  UTF-8 for.** sql
18b90 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
18ba0 74 69 6f 6e 28 29 20 61 6e 64 20 55 54 46 2d 31  tion() and UTF-1
18bb0 36 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72  6 for sqlite3_cr
18bc0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
18bd0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  )..**.** The fir
18be0 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  st argument is t
18bf0 68 65 20 5b 73 71 6c 69 74 65 33 20 7c 20 64 61  he [sqlite3 | da
18c00 74 61 62 61 73 65 20 68 61 6e 64 6c 65 5d 20 74  tabase handle] t
18c10 68 61 74 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a  hat holds the.**
18c20 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72   SQL function or
18c30 20 61 67 67 72 65 67 61 74 65 20 69 73 20 74 6f   aggregate is to
18c40 20 62 65 20 61 64 64 65 64 20 6f 72 20 72 65 64   be added or red
18c50 65 66 69 6e 65 64 2e 20 49 66 20 61 20 73 69 6e  efined. If a sin
18c60 67 6c 65 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75  gle.** program u
18c70 73 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ses more than on
18c80 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
18c90 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68  e internally, th
18ca0 65 6e 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69  en SQL.** functi
18cb0 6f 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65  ons or aggregate
18cc0 73 20 6d 75 73 74 20 62 65 20 61 64 64 65 64 20  s must be added 
18cd0 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 74 6f 20  individually to 
18ce0 65 61 63 68 20 64 61 74 61 62 61 73 65 0a 2a 2a  each database.**
18cf0 20 68 61 6e 64 6c 65 20 77 69 74 68 20 77 68 69   handle with whi
18d00 63 68 20 74 68 65 79 20 77 69 6c 6c 20 62 65 20  ch they will be 
18d10 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  used..**.** The 
18d20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
18d30 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
18d40 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
18d50 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 0a 2a   to be created.*
18d60 2a 20 6f 72 20 72 65 64 65 66 69 6e 65 64 2e 0a  * or redefined..
18d70 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66  ** The length of
18d80 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d   the name is lim
18d90 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65  ited to 255 byte
18da0 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 20  s, exclusive of 
18db0 74 68 65 20 0a 2a 2a 20 7a 65 72 6f 2d 74 65 72  the .** zero-ter
18dc0 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74  minator.  Note t
18dd0 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e  hat the name len
18de0 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20  gth limit is in 
18df0 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68  bytes, not.** ch
18e00 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61  aracters.  Any a
18e10 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65  ttempt to create
18e20 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
18e30 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a   a longer name.*
18e40 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e  * will result in
18e50 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   an SQLITE_ERROR
18e60 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68   error..**.** Th
18e70 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
18e80 72 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  r is the number 
18e90 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61  of arguments tha
18ea0 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  t the SQL functi
18eb0 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61  on or.** aggrega
18ec0 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69  te takes. If thi
18ed0 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  s parameter is n
18ee0 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68  egative, then th
18ef0 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f  e SQL function o
18f00 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 20 6d  r.** aggregate m
18f10 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75 6d 62  ay take any numb
18f20 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e  er of arguments.
18f30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74  .**.** The fourt
18f40 68 20 70 61 72 61 6d 65 74 65 72 2c 20 65 54 65  h parameter, eTe
18f50 78 74 52 65 70 2c 20 73 70 65 63 69 66 69 65 73  xtRep, specifies
18f60 20 77 68 61 74 20 0a 2a 2a 20 5b 53 51 4c 49 54   what .** [SQLIT
18f70 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 65 6e  E_UTF8 | text en
18f80 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 51 4c  coding] this SQL
18f90 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72   function prefer
18fa0 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 61 72  s for.** its par
18fb0 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 53 51  ameters.  Any SQ
18fc0 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  L function imple
18fd0 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64  mentation should
18fe0 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f 72 6b   be able to work
18ff0 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 55 54  .** work with UT
19000 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20 6f  F-8, UTF-16le, o
19010 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75 74  r UTF-16be.  But
19020 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61   some implementa
19030 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a 20  tions may be.** 
19040 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 77  more efficient w
19050 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e 67  ith one encoding
19060 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20 20   than another.  
19070 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f  It is allowed to
19080 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  .** invoke sqlit
19090 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
190a0 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  on() or sqlite3_
190b0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
190c0 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20  6() multiple.** 
190d0 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 20 73  times with the s
190e0 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62 75 74  ame function but
190f0 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74 20   with different 
19100 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78 74 52  values of eTextR
19110 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c 74  ep..** When mult
19120 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  iple implementat
19130 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
19140 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 61 76   function are av
19150 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74 65 0a  ailable, SQLite.
19160 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74 68 65  ** will pick the
19170 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f 6c 76   one that involv
19180 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d 6f  es the least amo
19190 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f 6e 76  unt of data conv
191a0 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68  ersion..** If th
191b0 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69  ere is only a si
191c0 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ngle implementat
191d0 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20 6e  ion which does n
191e0 6f 74 20 63 61 72 65 20 77 68 61 74 0a 2a 2a 20  ot care what.** 
191f0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 69 73  text encoding is
19200 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20   used, then the 
19210 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 20  fourth argument 
19220 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 5b 53 51  should be.** [SQ
19230 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a  LITE_ANY]..**.**
19240 20 54 68 65 20 66 69 66 74 68 20 70 61 72 61 6d   The fifth param
19250 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74  eter is an arbit
19260 72 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54  rary pointer.  T
19270 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
19280 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63  n.** of the func
19290 74 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63  tion can gain ac
192a0 63 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69  cess to this poi
192b0 6e 74 65 72 20 75 73 69 6e 67 0a 2a 2a 20 5b 73  nter using.** [s
192c0 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
192d0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ()]..**.** The s
192e0 65 76 65 6e 74 68 2c 20 65 69 67 68 74 68 20 61  eventh, eighth a
192f0 6e 64 20 6e 69 6e 74 68 20 70 61 72 61 6d 65 74  nd ninth paramet
19300 65 72 73 2c 20 78 46 75 6e 63 2c 20 78 53 74 65  ers, xFunc, xSte
19310 70 20 61 6e 64 20 78 46 69 6e 61 6c 2c 20 61 72  p and xFinal, ar
19320 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f  e.** pointers to
19330 20 43 2d 6c 61 6e 67 75 61 67 65 20 66 75 6e 63   C-language func
19340 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
19350 6d 65 6e 74 20 74 68 65 20 53 51 4c 0a 2a 2a 20  ment the SQL.** 
19360 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72  function or aggr
19370 65 67 61 74 65 2e 20 41 20 73 63 61 6c 61 72 20  egate. A scalar 
19380 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71  SQL function req
19390 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65  uires an impleme
193a0 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68  ntation of.** th
193b0 65 20 78 46 75 6e 63 20 63 61 6c 6c 62 61 63 6b  e xFunc callback
193c0 20 6f 6e 6c 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e   only, NULL poin
193d0 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 70  ters should be p
193e0 61 73 73 65 64 20 61 73 20 74 68 65 20 78 53 74  assed as the xSt
193f0 65 70 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c  ep.** and xFinal
19400 20 70 61 72 61 6d 65 74 65 72 73 2e 20 41 6e 20   parameters. An 
19410 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75  aggregate SQL fu
19420 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20  nction requires 
19430 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  an implementatio
19440 6e 0a 2a 2a 20 6f 66 20 78 53 74 65 70 20 61 6e  n.** of xStep an
19450 64 20 78 46 69 6e 61 6c 20 61 6e 64 20 4e 55 4c  d xFinal and NUL
19460 4c 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73  L should be pass
19470 65 64 20 66 6f 72 20 78 46 75 6e 63 2e 20 54 6f  ed for xFunc. To
19480 20 64 65 6c 65 74 65 20 61 6e 0a 2a 2a 20 65 78   delete an.** ex
19490 69 73 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74  isting SQL funct
194a0 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65  ion or aggregate
194b0 2c 20 70 61 73 73 20 4e 55 4c 4c 20 66 6f 72 20  , pass NULL for 
194c0 61 6c 6c 20 74 68 72 65 65 20 66 75 6e 63 74 69  all three functi
194d0 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 0a  on.** callback..
194e0 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 70 65 72 6d  **.** It is perm
194f0 69 74 74 65 64 20 74 6f 20 72 65 67 69 73 74 65  itted to registe
19500 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65  r multiple imple
19510 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
19520 65 20 73 61 6d 65 0a 2a 2a 20 66 75 6e 63 74 69  e same.** functi
19530 6f 6e 73 20 77 69 74 68 20 74 68 65 20 73 61 6d  ons with the sam
19540 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74 68 20  e name but with 
19550 65 69 74 68 65 72 20 64 69 66 66 65 72 69 6e 67  either differing
19560 20 6e 75 6d 62 65 72 73 20 6f 66 0a 2a 2a 20 61   numbers of.** a
19570 72 67 75 6d 65 6e 74 73 20 6f 72 20 64 69 66 66  rguments or diff
19580 65 72 69 6e 67 20 70 65 72 66 65 72 72 65 64 20  ering perferred 
19590 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 2e 20  text encodings. 
195a0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65   SQLite will use
195b0 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  .** the implemen
195c0 74 61 74 69 6f 6e 20 6d 6f 73 74 20 63 6c 6f 73  tation most clos
195d0 65 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65 20  ely matches the 
195e0 77 61 79 20 69 6e 20 77 68 69 63 68 20 74 68 65  way in which the
195f0 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  .** SQL function
19600 20 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c   is used..*/.SQL
19610 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
19620 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
19630 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ion(.  sqlite3 *
19640 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
19650 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20  zFunctionName,. 
19660 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74   int nArg,.  int
19670 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69   eTextRep,.  voi
19680 64 2a 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  d*,.  void (*xFu
19690 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
196a0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
196b0 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
196c0 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
196d0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
196e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
196f0 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
19700 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
19710 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  xt*).);.SQLITE_A
19720 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
19730 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
19740 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20  (.  sqlite3*,.  
19750 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e  const void *zFun
19760 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74  ctionName,.  int
19770 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65   nArg,.  int eTe
19780 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 2a 2c 0a  xtRep,.  void*,.
19790 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
197a0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
197b0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
197c0 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  ue**),.  void (*
197d0 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
197e0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
197f0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
19800 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
19810 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
19820 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
19830 52 45 46 3a 20 54 65 78 74 20 45 6e 63 6f 64 69  REF: Text Encodi
19840 6e 67 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ngs.**.** These 
19850 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 65 20  constant define 
19860 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20 74 68  integer codes th
19870 61 74 20 72 65 70 72 65 73 65 6e 74 20 74 68 65  at represent the
19880 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65 78 74   various.** text
19890 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 70 70 6f   encodings suppo
198a0 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a  rted by SQLite..
198b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
198c0 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 20 20  E_UTF8          
198d0 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
198e0 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 20 20  E_UTF16LE       
198f0 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
19900 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 20 20  E_UTF16BE       
19910 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   3.#define SQLIT
19920 45 5f 55 54 46 31 36 20 20 20 20 20 20 20 20 20  E_UTF16         
19930 20 34 20 20 20 20 2f 2a 20 55 73 65 20 6e 61 74   4    /* Use nat
19940 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 2a  ive byte order *
19950 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
19960 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 20  _ANY            
19970 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f  5    /* sqlite3_
19980 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
19990 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
199a0 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
199b0 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 73 71  GNED  8    /* sq
199c0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
199d0 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 0a  lation only */..
199e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
199f0 4f 62 73 6f 6c 65 74 65 20 46 75 6e 63 74 69 6f  Obsolete Functio
19a00 6e 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66  ns.**.** These f
19a10 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 61 6c 6c  unctions are all
19a20 20 6e 6f 77 20 6f 62 73 6f 6c 65 74 65 2e 20 20   now obsolete.  
19a30 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e  In order to main
19a40 74 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64  tain.** backward
19a50 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  s compatibility 
19a60 77 69 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c  with older code,
19a70 20 77 65 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20   we continue to 
19a80 73 75 70 70 6f 72 74 0a 2a 2a 20 74 68 65 73 65  support.** these
19a90 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 48 6f 77   functions.  How
19aa0 65 76 65 72 2c 20 6e 65 77 20 64 65 76 65 6c 6f  ever, new develo
19ab0 70 6d 65 6e 74 20 70 72 6f 6a 65 63 74 73 20 73  pment projects s
19ac0 68 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74  hould avoid.** t
19ad0 68 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 20  he use of these 
19ae0 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68  functions.  To h
19af0 65 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65  elp encourage pe
19b00 6f 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a  ople to avoid.**
19b10 20 75 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e   using these fun
19b20 63 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e  ctions, we are n
19b30 6f 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c  ot going to tell
19b40 20 79 6f 75 20 77 61 6e 74 20 74 68 65 79 20 64   you want they d
19b50 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  o..*/.SQLITE_API
19b60 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67   int sqlite3_agg
19b70 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c  regate_count(sql
19b80 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
19b90 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
19ba0 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28 73  qlite3_expired(s
19bb0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53  qlite3_stmt*);.S
19bc0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
19bd0 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62  lite3_transfer_b
19be0 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f  indings(sqlite3_
19bf0 73 74 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73  stmt*, sqlite3_s
19c00 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  tmt*);.SQLITE_AP
19c10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c  I int sqlite3_gl
19c20 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69  obal_recover(voi
19c30 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  d);.SQLITE_API v
19c40 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72 65  oid sqlite3_thre
19c50 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 29  ad_cleanup(void)
19c60 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
19c70 46 3a 20 4f 62 74 61 69 6e 69 6e 67 20 53 51 4c  F: Obtaining SQL
19c80 20 46 75 6e 63 74 69 6f 6e 20 50 61 72 61 6d 65   Function Parame
19c90 74 65 72 20 56 61 6c 75 65 73 0a 2a 2a 0a 2a 2a  ter Values.**.**
19ca0 20 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20   The C-language 
19cb0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
19cc0 66 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  f SQL functions 
19cd0 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75  and aggregates u
19ce0 73 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20  ses.** this set 
19cf0 6f 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75  of interface rou
19d00 74 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20  tines to access 
19d10 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61  the parameter va
19d20 6c 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66  lues on.** the f
19d30 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65  unction or aggre
19d40 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  gate..**.** The 
19d50 78 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61  xFunc (for scala
19d60 72 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20  r functions) or 
19d70 78 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65  xStep (for aggre
19d80 67 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72  gates) parameter
19d90 73 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33  s.** to [sqlite3
19da0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
19db0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
19dc0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
19dd0 31 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20  16()].** define 
19de0 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69  callbacks that i
19df0 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c  mplement the SQL
19e00 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61   functions and a
19e10 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68  ggregates..** Th
19e20 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 4th parameter 
19e30 74 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63  to these callbac
19e40 6b 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f  ks is an array o
19e50 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a  f pointers to.**
19e60 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
19e70 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72 65   objects.  There
19e80 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 33   is one [sqlite3
19e90 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66  _value] object f
19ea0 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 6d  or.** each param
19eb0 65 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c 20  eter to the SQL 
19ec0 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65  function.  These
19ed0 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73   routines are us
19ee0 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63 74  ed to.** extract
19ef0 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65   values from the
19f00 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
19f10 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20   objects..**.** 
19f20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77  These routines w
19f30 6f 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  ork just like th
19f40 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
19f50 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  .** [sqlite3_col
19f60 75 6d 6e 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74  umn_blob | sqlit
19f70 65 33 5f 63 6f 6c 75 6d 6e 5f 2a 20 72 6f 75 74  e3_column_* rout
19f80 69 6e 65 73 5d 20 65 78 63 65 70 74 20 74 68 61  ines] except tha
19f90 74 20 0a 2a 2a 20 74 68 65 73 65 20 72 6f 75 74  t .** these rout
19fa0 69 6e 65 73 20 74 61 6b 65 20 61 20 73 69 6e 67  ines take a sing
19fb0 6c 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  le [sqlite3_valu
19fc0 65 2a 5d 20 70 6f 69 6e 74 65 72 20 69 6e 73 74  e*] pointer inst
19fd0 65 61 64 0a 2a 2a 20 6f 66 20 61 6e 20 5b 73 71  ead.** of an [sq
19fe0 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70 6f 69  lite3_stmt*] poi
19ff0 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 6e 74 65  nter and an inte
1a000 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  ger column numbe
1a010 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  r..**.** The sql
1a020 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1a030 36 28 29 20 69 6e 74 65 72 66 61 63 65 20 65 78  6() interface ex
1a040 74 72 61 63 74 73 20 61 20 55 54 46 31 36 20 73  tracts a UTF16 s
1a050 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20  tring.** in the 
1a060 6e 61 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65  native byte-orde
1a070 72 20 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61  r of the host ma
1a080 63 68 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73  chine.  The.** s
1a090 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1a0a0 74 31 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69  t16be() and sqli
1a0b0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1a0c0 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a  le() interfaces.
1a0d0 2a 2a 20 65 78 74 72 61 63 74 20 55 54 46 31 36  ** extract UTF16
1a0e0 20 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d   strings as big-
1a0f0 65 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c  endian and littl
1a100 65 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74  e-endian respect
1a110 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ively..**.** The
1a120 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
1a130 75 6d 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e  umeric_type() in
1a140 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73  terface attempts
1a150 20 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d   to apply.** num
1a160 65 72 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f  eric affinity to
1a170 20 74 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69   the value.  Thi
1a180 73 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20  s means that an 
1a190 61 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61  attempt is.** ma
1a1a0 64 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68  de to convert th
1a1b0 65 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e  e value to an in
1a1c0 74 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e  teger or floatin
1a1d0 67 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20  g point.  If.** 
1a1e0 73 75 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f  such a conversio
1a1f0 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69  n is possible wi
1a200 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e  thout loss of in
1a210 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 72  formation (in or
1a220 64 65 72 0a 2a 2a 20 77 6f 72 64 73 20 69 66 20  der.** words if 
1a230 74 68 65 20 76 61 6c 75 65 20 69 73 20 6f 72 69  the value is ori
1a240 67 69 6e 61 6c 20 61 20 73 74 72 69 6e 67 20 74  ginal a string t
1a250 68 61 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61  hat looks like a
1a260 20 6e 75 6d 62 65 72 29 0a 2a 2a 20 74 68 65 6e   number).** then
1a270 20 69 74 20 69 73 20 64 6f 6e 65 2e 20 20 4f 74   it is done.  Ot
1a280 68 65 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65  herwise no conve
1a290 72 73 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54  rsion occurs.  T
1a2a0 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49  he .** [SQLITE_I
1a2b0 4e 54 45 47 45 52 20 7c 20 64 61 74 61 74 79 70  NTEGER | datatyp
1a2c0 65 5d 20 61 66 74 65 72 20 63 6f 6e 76 65 72 73  e] after convers
1a2d0 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ion is returned.
1a2e0 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73 65 20 70 61  .**.** Please pa
1a2f0 79 20 70 61 72 74 69 63 75 6c 61 72 20 61 74 74  y particular att
1a300 65 6e 74 69 6f 6e 20 74 6f 20 74 68 65 20 66 61  ention to the fa
1a310 63 74 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e  ct that the poin
1a320 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 72  ter that.** is r
1a330 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71  eturned from [sq
1a340 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
1a350 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61  ()], [sqlite3_va
1a360 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a  lue_text()], or.
1a370 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ** [sqlite3_valu
1a380 65 5f 74 65 78 74 31 36 28 29 5d 20 63 61 6e 20  e_text16()] can 
1a390 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62  be invalidated b
1a3a0 79 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63  y a subsequent c
1a3b0 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74  all to.** [sqlit
1a3c0 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29  e3_value_bytes()
1a3d0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ], [sqlite3_valu
1a3e0 65 5f 62 79 74 65 73 31 36 28 29 5d 2c 20 5b 73  e_bytes16()], [s
1a3f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1a400 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c  t()],.** or [sql
1a410 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1a420 36 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  6()].  .**.** Th
1a430 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 73  ese routines mus
1a440 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  t be called from
1a450 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
1a460 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66   as.** the SQL f
1a470 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75 70  unction that sup
1a480 70 6c 69 65 64 20 74 68 65 20 73 71 6c 69 74 65  plied the sqlite
1a490 33 5f 76 61 6c 75 65 2a 20 70 61 72 61 6d 65 74  3_value* paramet
1a4a0 65 72 73 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74  ers..** Or, if t
1a4b0 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  he sqlite3_value
1a4c0 2a 20 61 72 67 75 6d 65 6e 74 20 63 6f 6d 65 73  * argument comes
1a4d0 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74   from the [sqlit
1a4e0 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
1a4f0 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c  )].** interface,
1a500 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74   then these rout
1a510 69 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 20 63  ines should be c
1a520 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73  alled from the s
1a530 61 6d 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 68  ame thread.** th
1a540 61 74 20 72 61 6e 20 5b 73 71 6c 69 74 65 33 5f  at ran [sqlite3_
1a550 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 2e  column_value()].
1a560 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
1a570 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1a580 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71  e3_value_blob(sq
1a590 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
1a5a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1a5b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1a5c0 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  s(sqlite3_value*
1a5d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
1a5e0 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
1a5f0 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f  bytes16(sqlite3_
1a600 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
1a610 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
1a620 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
1a630 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
1a640 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1a650 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
1a660 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t(sqlite3_value*
1a670 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  );.SQLITE_API sq
1a680 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
1a690 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
1a6a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
1a6b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1a6c0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1a6d0 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  *sqlite3_value_t
1a6e0 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ext(sqlite3_valu
1a6f0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
1a700 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
1a710 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1a720 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
1a730 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
1a740 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1a750 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28  _value_text16le(
1a760 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
1a770 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1a780 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
1a790 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73  value_text16be(s
1a7a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
1a7b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1a7c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
1a7d0 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  e(sqlite3_value*
1a7e0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
1a7f0 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
1a800 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c  numeric_type(sql
1a810 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f  ite3_value*);../
1a820 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
1a830 62 74 61 69 6e 20 41 67 67 72 65 67 61 74 65 20  btain Aggregate 
1a840 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74  Function Context
1a850 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  .**.** The imple
1a860 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67  mentation of agg
1a870 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74  regate SQL funct
1a880 69 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f  ions use this ro
1a890 75 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74  utine to allocat
1a8a0 65 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65  e.** a structure
1a8b0 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65   for storing the
1a8c0 69 72 20 73 74 61 74 65 2e 20 20 54 68 65 20 66  ir state.  The f
1a8d0 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72  irst time this r
1a8e0 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c  outine.** is cal
1a8f0 6c 65 64 20 66 6f 72 20 61 20 70 61 72 74 69 63  led for a partic
1a900 75 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c 20  ular aggregate, 
1a910 61 20 6e 65 77 20 73 74 72 75 63 74 75 72 65 20  a new structure 
1a920 6f 66 20 73 69 7a 65 20 6e 42 79 74 65 73 0a 2a  of size nBytes.*
1a930 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20  * is allocated, 
1a940 7a 65 72 6f 65 64 2c 20 61 6e 64 20 72 65 74 75  zeroed, and retu
1a950 72 6e 65 64 2e 20 20 4f 6e 20 73 75 62 73 65 71  rned.  On subseq
1a960 75 65 6e 74 20 63 61 6c 6c 73 20 28 66 6f 72 20  uent calls (for 
1a970 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 67 67 72  the.** same aggr
1a980 65 67 61 74 65 20 69 6e 73 74 61 6e 63 65 29 20  egate instance) 
1a990 74 68 65 20 73 61 6d 65 20 62 75 66 66 65 72 20  the same buffer 
1a9a0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68  is returned.  Th
1a9b0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1a9c0 0a 2a 2a 20 6f 66 20 74 68 65 20 61 67 67 72 65  .** of the aggre
1a9d0 67 61 74 65 20 63 61 6e 20 75 73 65 20 74 68 65  gate can use the
1a9e0 20 72 65 74 75 72 6e 65 64 20 62 75 66 66 65 72   returned buffer
1a9f0 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 64   to accumulate d
1aa00 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62  ata..**.** The b
1aa10 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  uffer allocated 
1aa20 69 73 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74  is freed automat
1aa30 69 63 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65  ically by SQLite
1aa40 20 77 68 61 6e 20 74 68 65 20 61 67 67 72 65 67   whan the aggreg
1aa50 61 74 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e  ate.** query con
1aa60 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  cludes..**.** Th
1aa70 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
1aa80 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f  r should be a co
1aa90 70 79 20 6f 66 20 74 68 65 20 0a 2a 2a 20 5b 73  py of the .** [s
1aaa0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c  qlite3_context |
1aab0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f   SQL function co
1aac0 6e 74 65 78 74 5d 20 74 68 61 74 20 69 73 20 74  ntext] that is t
1aad0 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 61  he first.** para
1aae0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63 61 6c  meter to the cal
1aaf0 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 74 68  lback routine th
1ab00 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
1ab10 65 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 66  e aggregate.** f
1ab20 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  unction..**.** T
1ab30 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
1ab40 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
1ab50 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
1ab60 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  in which.** the 
1ab70 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75  aggregate SQL fu
1ab80 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
1ab90 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  g..*/.SQLITE_API
1aba0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61   void *sqlite3_a
1abb0 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
1abc0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1abd0 2a 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a  *, int nBytes);.
1abe0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1abf0 20 55 73 65 72 20 44 61 74 61 20 46 6f 72 20 46   User Data For F
1ac00 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54  unctions.**.** T
1ac10 68 65 20 70 55 73 65 72 44 61 74 61 20 70 61 72  he pUserData par
1ac20 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 5b 73  ameter to the [s
1ac30 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
1ac40 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64  nction()].** and
1ac50 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
1ac60 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72  _function16()] r
1ac70 6f 75 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20  outines.** used 
1ac80 74 6f 20 72 65 67 69 73 74 65 72 20 75 73 65 72  to register user
1ac90 20 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 61 76   functions is av
1aca0 61 69 6c 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 68  ailable to.** th
1acb0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1acc0 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
1acd0 20 75 73 69 6e 67 20 74 68 69 73 20 63 61 6c 6c   using this call
1ace0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1acf0 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c  tine must be cal
1ad00 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d  led from the sam
1ad10 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63  e thread in whic
1ad20 68 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e  h.** the SQL fun
1ad30 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67  ction is running
1ad40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1ad50 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73  void *sqlite3_us
1ad60 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f  er_data(sqlite3_
1ad70 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a  context*);../*.*
1ad80 2a 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63  * CAPI3REF: Func
1ad90 74 69 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44  tion Auxiliary D
1ada0 61 74 61 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ata.**.** The fo
1adb0 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63  llowing two func
1adc0 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65  tions may be use
1add0 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20  d by scalar SQL 
1ade0 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20  functions to.** 
1adf0 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 2d 64  associate meta-d
1ae00 61 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e  ata with argumen
1ae10 74 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65  t values. If the
1ae20 20 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70   same value is p
1ae30 61 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74  assed to.** mult
1ae40 69 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73  iple invocations
1ae50 20 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c   of the same SQL
1ae60 20 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67   function during
1ae70 20 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e   query execution
1ae80 2c 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20  , under.** some 
1ae90 63 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68  circumstances th
1aea0 65 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74  e associated met
1aeb0 61 2d 64 61 74 61 20 6d 61 79 20 62 65 20 70 72  a-data may be pr
1aec0 65 73 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61  eserved. This ma
1aed0 79 0a 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f  y.** be used, fo
1aee0 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64  r example, to ad
1aef0 64 20 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72  d a regular-expr
1af00 65 73 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20  ession matching 
1af10 73 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69  scalar.** functi
1af20 6f 6e 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64  on. The compiled
1af30 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
1af40 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69  regular expressi
1af50 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a  on is stored as.
1af60 2a 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73 73  ** meta-data ass
1af70 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
1af80 20 53 51 4c 20 76 61 6c 75 65 20 70 61 73 73 65   SQL value passe
1af90 64 20 61 73 20 74 68 65 20 72 65 67 75 6c 61 72  d as the regular
1afa0 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70   expression.** p
1afb0 61 74 74 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d  attern.  The com
1afc0 70 69 6c 65 64 20 72 65 67 75 6c 61 72 20 65 78  piled regular ex
1afd0 70 72 65 73 73 69 6f 6e 20 63 61 6e 20 62 65 20  pression can be 
1afe0 72 65 75 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70  reused on multip
1aff0 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e  le.** invocation
1b000 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75  s of the same fu
1b010 6e 63 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 74  nction so that t
1b020 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 74 74  he original patt
1b030 65 72 6e 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f  ern string.** do
1b040 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
1b050 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20  e recompiled on 
1b060 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e  each invocation.
1b070 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1b080 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29  e3_get_auxdata()
1b090 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
1b0a0 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
1b0b0 74 68 65 20 6d 65 74 61 2d 64 61 74 61 0a 2a 2a  the meta-data.**
1b0c0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1b0d0 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e   the Nth argumen
1b0e0 74 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 63  t value to the c
1b0f0 75 72 72 65 6e 74 20 53 51 4c 20 66 75 6e 63 74  urrent SQL funct
1b100 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 2c 20 77 68 65  ion.** call, whe
1b110 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f  re N is the seco
1b120 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66  nd parameter. If
1b130 20 6e 6f 20 6d 65 74 61 2d 64 61 74 61 20 68 61   no meta-data ha
1b140 73 20 62 65 65 6e 20 73 65 74 20 66 6f 72 0a 2a  s been set for.*
1b150 2a 20 74 68 61 74 20 76 61 6c 75 65 2c 20 74 68  * that value, th
1b160 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  en a NULL pointe
1b170 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  r is returned..*
1b180 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
1b190 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69  _set_auxdata() i
1b1a0 73 20 75 73 65 64 20 74 6f 20 61 73 73 6f 63 69  s used to associ
1b1b0 61 74 65 20 6d 65 74 61 2d 64 61 74 61 20 77 69  ate meta-data wi
1b1c0 74 68 20 61 6e 20 53 51 4c 0a 2a 2a 20 66 75 6e  th an SQL.** fun
1b1d0 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 20  ction argument. 
1b1e0 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  The third parame
1b1f0 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ter is a pointer
1b200 20 74 6f 20 74 68 65 20 6d 65 74 61 2d 64 61 74   to the meta-dat
1b210 61 0a 2a 2a 20 74 6f 20 62 65 20 61 73 73 6f 63  a.** to be assoc
1b220 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 4e  iated with the N
1b230 74 68 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  th user function
1b240 20 61 72 67 75 6d 65 6e 74 20 76 61 6c 75 65 2e   argument value.
1b250 20 54 68 65 20 66 6f 75 72 74 68 0a 2a 2a 20 70   The fourth.** p
1b260 61 72 61 6d 65 74 65 72 20 73 70 65 63 69 66 69  arameter specifi
1b270 65 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  es a destructor 
1b280 74 68 61 74 20 77 69 6c 6c 20 62 65 20 63 61 6c  that will be cal
1b290 6c 65 64 20 6f 6e 20 74 68 65 20 6d 65 74 61 2d  led on the meta-
1b2a0 0a 2a 2a 20 64 61 74 61 20 70 6f 69 6e 74 65 72  .** data pointer
1b2b0 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74 20 77   to release it w
1b2c0 68 65 6e 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e  hen it is no lon
1b2d0 67 65 72 20 72 65 71 75 69 72 65 64 2e 20 49 66  ger required. If
1b2e0 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 72 75 63   the .** destruc
1b2f0 74 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20  tor is NULL, it 
1b300 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 2e 0a  is not invoked..
1b310 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63 74 69 63  **.** In practic
1b320 65 2c 20 6d 65 74 61 2d 64 61 74 61 20 69 73 20  e, meta-data is 
1b330 70 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65  preserved betwee
1b340 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  n function calls
1b350 20 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 73 69   for.** expressi
1b360 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e  ons that are con
1b370 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65  stant at compile
1b380 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63 6c   time. This incl
1b390 75 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a 2a 20  udes literal.** 
1b3a0 76 61 6c 75 65 73 20 61 6e 64 20 53 51 4c 20 76  values and SQL v
1b3b0 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  ariables..**.** 
1b3c0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d  These routines m
1b3d0 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72  ust be called fr
1b3e0 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  om the same thre
1b3f0 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74  ad in which.** t
1b400 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
1b410 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 53  is running..*/.S
1b420 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
1b430 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
1b440 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ata(sqlite3_cont
1b450 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ext*, int);.SQLI
1b460 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1b470 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28  te3_set_auxdata(
1b480 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
1b490 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2c 20 76 6f  , int, void*, vo
1b4a0 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  id (*)(void*));.
1b4b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1b4c0 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69  : Constants Defi
1b4d0 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73  ning Special Des
1b4e0 74 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72  tructor Behavior
1b4f0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72 65  .**.** These are
1b500 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 20 66   special value f
1b510 6f 72 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  or the destructo
1b520 72 20 74 68 61 74 20 69 73 20 70 61 73 73 65 64  r that is passed
1b530 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a 20 66 69   in as the.** fi
1b540 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  nal argument to 
1b550 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73  routines like [s
1b560 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
1b570 6f 62 28 29 5d 2e 20 20 49 66 20 74 68 65 20 64  ob()].  If the d
1b580 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67  estructor.** arg
1b590 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f  ument is SQLITE_
1b5a0 53 54 41 54 49 43 2c 20 69 74 20 6d 65 61 6e 73  STATIC, it means
1b5b0 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e   that the conten
1b5c0 74 20 70 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e  t pointer is con
1b5d0 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c  stant.** and wil
1b5e0 6c 20 6e 65 76 65 72 20 63 68 61 6e 67 65 2e 20  l never change. 
1b5f0 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65   It does not nee
1b600 64 20 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65  d to be destroye
1b610 64 2e 20 20 54 68 65 20 0a 2a 2a 20 53 51 4c 49  d.  The .** SQLI
1b620 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76 61 6c  TE_TRANSIENT val
1b630 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ue means that th
1b640 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 6c  e content will l
1b650 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69 6e 0a  ikely change in.
1b660 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75 74 75  ** the near futu
1b670 72 65 20 61 6e 64 20 74 68 61 74 20 53 51 4c 69  re and that SQLi
1b680 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69  te should make i
1b690 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63  ts own private c
1b6a0 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  opy of.** the co
1b6b0 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72 65 74  ntent before ret
1b6c0 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  urning..**.** Th
1b6d0 65 20 74 79 70 65 64 65 66 20 69 73 20 6e 65 63  e typedef is nec
1b6e0 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b 20 61  essary to work a
1b6f0 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73 20 69  round problems i
1b700 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b  n certain.** C++
1b710 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53 65 65   compilers.  See
1b720 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e 0a 2a   ticket #2191..*
1b730 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 20 28  /.typedef void (
1b740 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63  *sqlite3_destruc
1b750 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64 2a 29  tor_type)(void*)
1b760 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ;.#define SQLITE
1b770 5f 53 54 41 54 49 43 20 20 20 20 20 20 28 28 73  _STATIC      ((s
1b780 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f  qlite3_destructo
1b790 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66 69 6e  r_type)0).#defin
1b7a0 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  e SQLITE_TRANSIE
1b7b0 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64  NT   ((sqlite3_d
1b7c0 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 2d  estructor_type)-
1b7d0 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  1)../*.** CAPI3R
1b7e0 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68 65 20  EF: Setting The 
1b7f0 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53 51 4c  Result Of An SQL
1b800 20 46 75 6e 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20   Function.**.** 
1b810 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
1b820 72 65 20 75 73 65 64 20 62 79 20 74 68 65 20 78  re used by the x
1b830 46 75 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63  Func or xFinal c
1b840 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a  allbacks that.**
1b850 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66   implement SQL f
1b860 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67  unctions and agg
1b870 72 65 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a  regates.  See.**
1b880 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
1b890 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64  _function()] and
1b8a0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
1b8b0 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a  _function16()].*
1b8c0 2a 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  * for additional
1b8d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
1b8e0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69  .** These functi
1b8f0 6f 6e 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75  ons work very mu
1b900 63 68 20 6c 69 6b 65 20 74 68 65 20 0a 2a 2a 20  ch like the .** 
1b910 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
1b920 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e  ob | sqlite3_bin
1b930 64 5f 2a 5d 20 66 61 6d 69 6c 79 20 6f 66 20 66  d_*] family of f
1b940 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 0a 2a 2a  unctions used.**
1b950 20 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20   to bind values 
1b960 74 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  to host paramete
1b970 72 73 20 69 6e 20 70 72 65 70 61 72 65 64 20 73  rs in prepared s
1b980 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65  tatements..** Re
1b990 66 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 5b 73  fer to the.** [s
1b9a0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
1b9b0 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   | sqlite3_bind_
1b9c0 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d  * documentation]
1b9d0 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e   for.** addition
1b9e0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
1b9f0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
1ba00 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
1ba10 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73   and sqlite3_res
1ba20 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 66 75  ult_error16() fu
1ba30 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63 61 75 73 65  nctions.** cause
1ba40 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   the implemented
1ba50 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 6f   SQL function to
1ba60 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74   throw an except
1ba70 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 70 61 72  ion.  The.** par
1ba80 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
1ba90 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
1baa0 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75   or sqlite3_resu
1bab0 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20  lt_error16().** 
1bac0 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61  is the text of a
1bad0 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e  n error message.
1bae0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1baf0 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f 62 69 67  e3_result_toobig
1bb00 28 29 20 63 61 75 73 65 20 74 68 65 20 66 75 6e  () cause the fun
1bb10 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ction implementa
1bb20 74 69 6f 6e 0a 2a 2a 20 74 6f 20 74 68 72 6f 77  tion.** to throw
1bb30 20 61 6e 64 20 65 72 72 6f 72 20 69 6e 64 69 63   and error indic
1bb40 61 74 69 6e 67 20 74 68 61 74 20 61 20 73 74 72  ating that a str
1bb50 69 6e 67 20 6f 72 20 42 4c 4f 42 20 69 73 20 74  ing or BLOB is t
1bb60 6f 20 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 72 65 70  o long.** to rep
1bb70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  resent..**.** Th
1bb80 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 73  ese routines mus
1bb90 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  t be called from
1bba0 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
1bbb0 20 74 68 72 65 61 64 20 61 73 0a 2a 2a 20 74 68   thread as.** th
1bbc0 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 61  e SQL function a
1bbd0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
1bbe0 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74  he [sqlite3_cont
1bbf0 65 78 74 5d 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  ext] pointer..*/
1bc00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1bc10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1bc20 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e  blob(sqlite3_con
1bc30 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  text*, const voi
1bc40 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29  d*, int, void(*)
1bc50 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45  (void*));.SQLITE
1bc60 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1bc70 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
1bc80 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
1bc90 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54  , double);.SQLIT
1bca0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1bcb0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
1bcc0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
1bcd0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69  , const char*, i
1bce0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
1bcf0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1bd00 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69  ult_error16(sqli
1bd10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
1bd20 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b  nst void*, int);
1bd30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1bd40 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1bd50 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c  error_toobig(sql
1bd60 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
1bd70 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1bd80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
1bd90 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74  rror_nomem(sqlit
1bda0 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51  e3_context*);.SQ
1bdb0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1bdc0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
1bdd0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1bde0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
1bdf0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1be00 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71  _result_int64(sq
1be10 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
1be20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a  sqlite3_int64);.
1be30 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1be40 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
1be50 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ull(sqlite3_cont
1be60 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ext*);.SQLITE_AP
1be70 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1be80 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74  esult_text(sqlit
1be90 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
1bea0 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76  st char*, int, v
1beb0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
1bec0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1bed0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
1bee0 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f  ext16(sqlite3_co
1bef0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f  ntext*, const vo
1bf00 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a  id*, int, void(*
1bf10 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
1bf20 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1bf30 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
1bf40 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  le(sqlite3_conte
1bf50 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  xt*, const void*
1bf60 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f  , int,void(*)(vo
1bf70 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
1bf80 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1bf90 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73  esult_text16be(s
1bfa0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
1bfb0 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
1bfc0 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
1bfd0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
1bfe0 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1bff0 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  t_value(sqlite3_
1c000 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65  context*, sqlite
1c010 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
1c020 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1c030 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c  e3_result_zerobl
1c040 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ob(sqlite3_conte
1c050 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a  xt*, int n);../*
1c060 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
1c070 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69  fine New Collati
1c080 6e 67 20 53 65 71 75 65 6e 63 65 73 0a 2a 2a 0a  ng Sequences.**.
1c090 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f  ** These functio
1c0a0 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  ns are used to a
1c0b0 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e  dd new collation
1c0c0 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68   sequences to th
1c0d0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 2a 5d 20  e.** [sqlite3*] 
1c0e0 68 61 6e 64 6c 65 20 73 70 65 63 69 66 69 65 64  handle specified
1c0f0 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
1c100 67 75 6d 65 6e 74 2e 20 0a 2a 2a 0a 2a 2a 20 54  gument. .**.** T
1c110 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e  he name of the n
1c120 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ew collation seq
1c130 75 65 6e 63 65 20 69 73 20 73 70 65 63 69 66 69  uence is specifi
1c140 65 64 20 61 73 20 61 20 55 54 46 2d 38 20 73 74  ed as a UTF-8 st
1c150 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73 71 6c 69  ring.** for sqli
1c160 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
1c170 74 69 6f 6e 28 29 20 61 6e 64 20 73 71 6c 69 74  tion() and sqlit
1c180 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
1c190 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61 6e 64 20  ion_v2().** and 
1c1a0 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20  a UTF-16 string 
1c1b0 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61  for sqlite3_crea
1c1c0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29  te_collation16()
1c1d0 2e 20 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 0a  .  In all cases.
1c1e0 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73 20 70  ** the name is p
1c1f0 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
1c200 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72 67  ond function arg
1c210 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ument..**.** The
1c220 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
1c230 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 74  must be one of t
1c240 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53 51  he constants [SQ
1c250 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20 5b  LITE_UTF8],.** [
1c260 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 20  SQLITE_UTF16LE] 
1c270 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  or [SQLITE_UTF16
1c280 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  BE], indicating 
1c290 74 68 61 74 20 74 68 65 20 75 73 65 72 2d 73 75  that the user-su
1c2a0 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74 69 6e  pplied.** routin
1c2b0 65 20 65 78 70 65 63 74 73 20 74 6f 20 62 65 20  e expects to be 
1c2c0 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72 73 20  passed pointers 
1c2d0 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63 6f 64  to strings encod
1c2e0 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 2c 0a  ed using UTF-8,.
1c2f0 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65  ** UTF-16 little
1c300 2d 65 6e 64 69 61 6e 20 6f 72 20 55 54 46 2d 31  -endian or UTF-1
1c310 36 20 62 69 67 2d 65 6e 64 69 61 6e 20 72 65 73  6 big-endian res
1c320 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a  pectively..**.**
1c330 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
1c340 65 20 75 73 65 72 20 73 75 70 70 6c 69 65 64 20  e user supplied 
1c350 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20  routine must be 
1c360 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
1c370 66 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e  fth.** argument.
1c380 20 49 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20   If it is NULL, 
1c390 74 68 69 73 20 69 73 20 74 68 65 20 73 61 6d 65  this is the same
1c3a0 20 61 73 20 64 65 6c 65 74 69 6e 67 20 74 68 65   as deleting the
1c3b0 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65   collation.** se
1c3c0 71 75 65 6e 63 65 20 28 73 6f 20 74 68 61 74 20  quence (so that 
1c3d0 53 51 4c 69 74 65 20 63 61 6e 6e 6f 74 20 63 61  SQLite cannot ca
1c3e0 6c 6c 20 69 74 20 61 6e 79 6d 6f 72 65 29 2e 20  ll it anymore). 
1c3f0 45 61 63 68 20 74 69 6d 65 20 74 68 65 20 75 73  Each time the us
1c400 65 72 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 66  er.** supplied f
1c410 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
1c420 65 64 2c 20 69 74 20 69 73 20 70 61 73 73 65 64  ed, it is passed
1c430 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76   a copy of the v
1c440 6f 69 64 2a 20 70 61 73 73 65 64 20 61 73 0a 2a  oid* passed as.*
1c450 2a 20 74 68 65 20 66 6f 75 72 74 68 20 61 72 67  * the fourth arg
1c460 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
1c470 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
1c480 6e 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  n() or.** sqlite
1c490 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
1c4a0 6f 6e 31 36 28 29 20 61 73 20 69 74 73 20 66 69  on16() as its fi
1c4b0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  rst parameter..*
1c4c0 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69  *.** The remaini
1c4d0 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ng arguments to 
1c4e0 74 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69 65  the user-supplie
1c4f0 64 20 72 6f 75 74 69 6e 65 20 61 72 65 20 74 77  d routine are tw
1c500 6f 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 65 61  o strings,.** ea
1c510 63 68 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  ch represented b
1c520 79 20 61 20 5b 6c 65 6e 67 74 68 2c 20 64 61 74  y a [length, dat
1c530 61 5d 20 70 61 69 72 20 61 6e 64 20 65 6e 63 6f  a] pair and enco
1c540 64 65 64 20 69 6e 20 74 68 65 20 65 6e 63 6f 64  ded in the encod
1c550 69 6e 67 0a 2a 2a 20 74 68 61 74 20 77 61 73 20  ing.** that was 
1c560 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74 68  passed as the th
1c570 69 72 64 20 61 72 67 75 6d 65 6e 74 20 77 68 65  ird argument whe
1c580 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  n the collation 
1c590 73 65 71 75 65 6e 63 65 20 77 61 73 0a 2a 2a 20  sequence was.** 
1c5a0 72 65 67 69 73 74 65 72 65 64 2e 20 54 68 65 20  registered. The 
1c5b0 75 73 65 72 20 72 6f 75 74 69 6e 65 20 73 68 6f  user routine sho
1c5c0 75 6c 64 20 72 65 74 75 72 6e 20 6e 65 67 61 74  uld return negat
1c5d0 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73  ive, zero or pos
1c5e0 69 74 69 76 65 20 69 66 0a 2a 2a 20 74 68 65 20  itive if.** the 
1c5f0 66 69 72 73 74 20 73 74 72 69 6e 67 20 69 73 20  first string is 
1c600 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c  less than, equal
1c610 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20   to, or greater 
1c620 74 68 61 6e 20 74 68 65 20 73 65 63 6f 6e 64 0a  than the second.
1c630 2a 2a 20 73 74 72 69 6e 67 2e 20 69 2e 65 2e 20  ** string. i.e. 
1c640 28 53 54 52 49 4e 47 31 20 2d 20 53 54 52 49 4e  (STRING1 - STRIN
1c650 47 32 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  G2)..**.** The s
1c660 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
1c670 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 20 77 6f 72  llation_v2() wor
1c680 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f  ks like sqlite3_
1c690 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
1c6a0 28 29 0a 2a 2a 20 65 78 63 61 70 74 20 74 68 61  ().** excapt tha
1c6b0 74 20 69 74 20 74 61 6b 65 73 20 61 6e 20 65 78  t it takes an ex
1c6c0 74 72 61 20 61 72 67 75 6d 65 6e 74 20 77 68 69  tra argument whi
1c6d0 63 68 20 69 73 20 61 20 64 65 73 74 72 75 63 74  ch is a destruct
1c6e0 6f 72 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f  or for.** the co
1c6f0 6c 6c 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65  llation.  The de
1c700 73 74 72 75 63 74 6f 72 20 69 73 20 63 61 6c 6c  structor is call
1c710 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c  ed when the coll
1c720 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 64 65 73 74  ation is.** dest
1c730 72 6f 79 65 64 20 61 6e 64 20 69 73 20 70 61 73  royed and is pas
1c740 73 65 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68  sed a copy of th
1c750 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  e fourth paramet
1c760 65 72 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72  er void* pointer
1c770 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74  .** of the sqlit
1c780 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
1c790 69 6f 6e 5f 76 32 28 29 2e 20 20 43 6f 6c 6c 61  ion_v2().  Colla
1c7a0 74 69 6f 6e 73 20 61 72 65 20 64 65 73 74 72 6f  tions are destro
1c7b0 79 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 79  yed when.** they
1c7c0 20 61 72 65 20 6f 76 65 72 72 69 64 64 65 6e 20   are overridden 
1c7d0 62 79 20 6c 61 74 65 72 20 63 61 6c 6c 73 20 74  by later calls t
1c7e0 6f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  o the collation 
1c7f0 63 72 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  creation functio
1c800 6e 73 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68  ns.** or when th
1c810 65 20 5b 73 71 6c 69 74 65 33 2a 5d 20 64 61 74  e [sqlite3*] dat
1c820 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69 73 20  abase handle is 
1c830 63 6c 6f 73 65 64 20 75 73 69 6e 67 20 5b 73 71  closed using [sq
1c840 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a  lite3_close()]..
1c850 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
1c860 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
1c870 6f 6e 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63  on_v2() interfac
1c880 65 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61  e is experimenta
1c890 6c 20 61 6e 64 0a 2a 2a 20 73 75 62 6a 65 63 74  l and.** subject
1c8a0 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 66 75   to change in fu
1c8b0 74 75 72 65 20 72 65 6c 65 61 73 65 73 2e 20 20  ture releases.  
1c8c0 54 68 65 20 6f 74 68 65 72 20 63 6f 6c 6c 61 74  The other collat
1c8d0 69 6f 6e 20 63 72 65 61 74 69 6f 6e 0a 2a 2a 20  ion creation.** 
1c8e0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 73 74  functions are st
1c8f0 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
1c900 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1c910 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
1c920 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20  (.  sqlite3*, . 
1c930 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1c940 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74  me, .  int eText
1c950 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20  Rep, .  void*,. 
1c960 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28   int(*xCompare)(
1c970 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
1c980 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
1c990 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45  void*).);.SQLITE
1c9a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1c9b0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
1c9c0 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 2a  n_v2(.  sqlite3*
1c9d0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
1c9e0 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65  *zName, .  int e
1c9f0 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64  TextRep, .  void
1ca00 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  *,.  int(*xCompa
1ca10 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
1ca20 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
1ca30 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f  nst void*),.  vo
1ca40 69 64 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  id(*xDestroy)(vo
1ca50 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  id*).);.SQLITE_A
1ca60 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
1ca70 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
1ca80 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  6(.  sqlite3*, .
1ca90 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
1caa0 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78  ame, .  int eTex
1cab0 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a  tRep, .  void*,.
1cac0 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
1cad0 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
1cae0 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
1caf0 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a   void*).);../*.*
1cb00 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 6c  * CAPI3REF: Coll
1cb10 61 74 69 6f 6e 20 4e 65 65 64 65 64 20 43 61 6c  ation Needed Cal
1cb20 6c 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20 54 6f 20  lbacks.**.** To 
1cb30 61 76 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20  avoid having to 
1cb40 72 65 67 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c  register all col
1cb50 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
1cb60 20 62 65 66 6f 72 65 20 61 20 64 61 74 61 62 61   before a databa
1cb70 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  se.** can be use
1cb80 64 2c 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c  d, a single call
1cb90 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61  back function ma
1cba0 79 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20  y be registered 
1cbb0 77 69 74 68 20 74 68 65 0a 2a 2a 20 64 61 74 61  with the.** data
1cbc0 62 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 62  base handle to b
1cbd0 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 65  e called wheneve
1cbe0 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20 63  r an undefined c
1cbf0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1cc00 65 20 69 73 0a 2a 2a 20 72 65 71 75 69 72 65 64  e is.** required
1cc10 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  ..**.** If the f
1cc20 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69 73  unction is regis
1cc30 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20  tered using the 
1cc40 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
1cc50 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c 0a  n_needed() API,.
1cc60 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70 61  ** then it is pa
1cc70 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20 6f  ssed the names o
1cc80 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c  f undefined coll
1cc90 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20  ation sequences 
1cca0 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65 6e  as strings.** en
1ccb0 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 20  coded in UTF-8. 
1ccc0 49 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  If sqlite3_colla
1ccd0 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29 20  tion_needed16() 
1cce0 69 73 20 75 73 65 64 2c 20 74 68 65 20 6e 61 6d  is used, the nam
1ccf0 65 73 0a 2a 2a 20 61 72 65 20 70 61 73 73 65 64  es.** are passed
1cd00 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 6d 61   as UTF-16 in ma
1cd10 63 68 69 6e 65 20 6e 61 74 69 76 65 20 62 79 74  chine native byt
1cd20 65 20 6f 72 64 65 72 2e 20 41 20 63 61 6c 6c 20  e order. A call 
1cd30 74 6f 20 65 69 74 68 65 72 0a 2a 2a 20 66 75 6e  to either.** fun
1cd40 63 74 69 6f 6e 20 72 65 70 6c 61 63 65 73 20 61  ction replaces a
1cd50 6e 79 20 65 78 69 73 74 69 6e 67 20 63 61 6c 6c  ny existing call
1cd60 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  back..**.** When
1cd70 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73   the callback is
1cd80 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 20 66 69   invoked, the fi
1cd90 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73  rst argument pas
1cda0 73 65 64 20 69 73 20 61 20 63 6f 70 79 0a 2a 2a  sed is a copy.**
1cdb0 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   of the second a
1cdc0 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
1cdd0 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
1cde0 64 65 64 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69  ded() or.** sqli
1cdf0 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
1ce00 65 64 65 64 31 36 28 29 2e 20 54 68 65 20 73 65  eded16(). The se
1ce10 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
1ce20 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
1ce30 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 74 68 69   handle. The thi
1ce40 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  rd argument is o
1ce50 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54  ne of [SQLITE_UT
1ce60 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46  F8], [SQLITE_UTF
1ce70 31 36 42 45 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51  16BE], or.** [SQ
1ce80 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69  LITE_UTF16LE], i
1ce90 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f  ndicating the mo
1cea0 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72  st desirable for
1ceb0 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  m of the collati
1cec0 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66  on.** sequence f
1ced0 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64  unction required
1cee0 2e 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72  . The fourth par
1cef0 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e 61  ameter is the na
1cf00 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 71  me of the.** req
1cf10 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  uired collation 
1cf20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  sequence..**.** 
1cf30 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  The callback fun
1cf40 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 67  ction should reg
1cf50 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72 65  ister the desire
1cf60 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69 6e  d collation usin
1cf70 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72  g.** [sqlite3_cr
1cf80 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
1cf90 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  ], [sqlite3_crea
1cfa0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29  te_collation16()
1cfb0 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  ], or.** [sqlite
1cfc0 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
1cfd0 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2f 0a 53 51 4c  on_v2()]..*/.SQL
1cfe0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1cff0 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
1d000 65 64 65 64 28 0a 20 20 73 71 6c 69 74 65 33 2a  eded(.  sqlite3*
1d010 2c 20 0a 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76  , .  void*, .  v
1d020 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c  oid(*)(void*,sql
1d030 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52  ite3*,int eTextR
1d040 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a  ep,const char*).
1d050 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
1d060 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  t sqlite3_collat
1d070 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20 20  ion_needed16(.  
1d080 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69  sqlite3*, .  voi
1d090 64 2a 2c 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f  d*,.  void(*)(vo
1d0a0 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
1d0b0 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
1d0c0 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  void*).);../*.**
1d0d0 20 53 70 65 63 69 66 79 20 74 68 65 20 6b 65 79   Specify the key
1d0e0 20 66 6f 72 20 61 6e 20 65 6e 63 72 79 70 74 65   for an encrypte
1d0f0 64 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  d database.  Thi
1d100 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64  s routine should
1d110 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69   be.** called ri
1d120 67 68 74 20 61 66 74 65 72 20 73 71 6c 69 74 65  ght after sqlite
1d130 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20  3_open()..**.** 
1d140 54 68 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c  The code to impl
1d150 65 6d 65 6e 74 20 74 68 69 73 20 41 50 49 20 69  ement this API i
1d160 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  s not available 
1d170 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20 72 65  in the public re
1d180 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69  lease.** of SQLi
1d190 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  te..*/.SQLITE_AP
1d1a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65  I int sqlite3_ke
1d1b0 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  y(.  sqlite3 *db
1d1c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1d1d0 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
1d1e0 74 6f 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f  to be rekeyed */
1d1f0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
1d200 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20  Key, int nKey   
1d210 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a    /* The key */.
1d220 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  );../*.** Change
1d230 20 74 68 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f   the key on an o
1d240 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20 20 49  pen database.  I
1d250 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61  f the current da
1d260 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a  tabase is not.**
1d270 20 65 6e 63 72 79 70 74 65 64 2c 20 74 68 69 73   encrypted, this
1d280 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e   routine will en
1d290 63 72 79 70 74 20 69 74 2e 20 20 49 66 20 70 4e  crypt it.  If pN
1d2a0 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30  ew==0 or nNew==0
1d2b0 2c 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  , the.** databas
1d2c0 65 20 69 73 20 64 65 63 72 79 70 74 65 64 2e 0a  e is decrypted..
1d2d0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74  **.** The code t
1d2e0 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73  o implement this
1d2f0 20 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69   API is not avai
1d300 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62  lable in the pub
1d310 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f  lic release.** o
1d320 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c  f SQLite..*/.SQL
1d330 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1d340 74 65 33 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c  te3_rekey(.  sql
1d350 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
1d360 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
1d370 61 74 61 62 61 73 65 20 74 6f 20 62 65 20 72 65  atabase to be re
1d380 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  keyed */.  const
1d390 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
1d3a0 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65   nKey     /* The
1d3b0 20 6e 65 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a   new key */.);..
1d3c0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1d3d0 20 53 75 73 70 65 6e 64 20 45 78 65 63 75 74 69   Suspend Executi
1d3e0 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74 20 54  on For A Short T
1d3f0 69 6d 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ime.**.** This f
1d400 75 6e 63 74 69 6f 6e 20 63 61 75 73 65 73 20 74  unction causes t
1d410 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61  he current threa
1d420 64 20 74 6f 20 73 75 73 70 65 6e 64 20 65 78 65  d to suspend exe
1d430 63 75 74 69 6f 6e 0a 2a 2a 20 61 20 6e 75 6d 62  cution.** a numb
1d440 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e  er of millisecon
1d450 64 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  ds specified in 
1d460 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  its parameter..*
1d470 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72  *.** If the oper
1d480 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 6f 65  ating system doe
1d490 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 6c  s not support sl
1d4a0 65 65 70 20 72 65 71 75 65 73 74 73 20 77 69 74  eep requests wit
1d4b0 68 20 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e  h .** millisecon
1d4c0 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f  d time resolutio
1d4d0 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d 65  n, then the time
1d4e0 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64   will be rounded
1d4f0 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 6e   up to .** the n
1d500 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54  earest second. T
1d510 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c  he number of mil
1d520 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65  liseconds of sle
1d530 65 70 20 61 63 74 75 61 6c 6c 79 20 0a 2a 2a 20  ep actually .** 
1d540 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74  requested from t
1d550 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
1d560 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  tem is returned.
1d570 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d  .**.** SQLite im
1d580 70 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e  plements this in
1d590 74 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69  terface by calli
1d5a0 6e 67 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a  ng the xSleep().
1d5b0 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  ** method of the
1d5c0 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65   default [sqlite
1d5d0 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a  3_vfs] object..*
1d5e0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1d5f0 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 69   sqlite3_sleep(i
1d600 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  nt);../*.** CAPI
1d610 33 52 45 46 3a 20 20 4e 61 6d 65 20 4f 66 20 54  3REF:  Name Of T
1d620 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e  he Folder Holdin
1d630 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69 6c 65  g Temporary File
1d640 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20  s.**.** If this 
1d650 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1d660 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74  is made to point
1d670 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69   to a string whi
1d680 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6e 61 6d  ch is.** the nam
1d690 65 20 6f 66 20 61 20 66 6f 6c 64 65 72 20 28 61  e of a folder (a
1d6a0 2e 6b 61 2e 20 64 69 72 65 63 74 6f 72 79 29 2c  .ka. directory),
1d6b0 20 74 68 65 6e 20 61 6c 6c 20 74 65 6d 70 6f 72   then all tempor
1d6c0 61 72 79 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65  ary files.** cre
1d6d0 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 77  ated by SQLite w
1d6e0 69 6c 6c 20 62 65 20 70 6c 61 63 65 64 20 69 6e  ill be placed in
1d6f0 20 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 2e   that directory.
1d700 20 20 49 66 20 74 68 69 73 20 76 61 72 69 61 62    If this variab
1d710 6c 65 0a 2a 2a 20 69 73 20 4e 55 4c 4c 20 70 6f  le.** is NULL po
1d720 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69  inter, then SQLi
1d730 74 65 20 64 6f 65 73 20 61 20 73 65 61 72 63 68  te does a search
1d740 20 66 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69   for an appropri
1d750 61 74 65 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a  ate temporary.**
1d760 20 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2e   file directory.
1d770 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74  .**.** It is not
1d780 20 73 61 66 65 20 74 6f 20 6d 6f 64 69 66 79 20   safe to modify 
1d790 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 6f 6e  this variable on
1d7a0 63 65 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  ce a database co
1d7b0 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20  nnection.** has 
1d7c0 62 65 65 6e 20 6f 70 65 6e 65 64 2e 20 20 49 74  been opened.  It
1d7d0 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61   is intended tha
1d7e0 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  t this variable 
1d7f0 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61  be set once.** a
1d800 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73  s part of proces
1d810 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  s initialization
1d820 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20   and before any 
1d830 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
1d840 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76  .** routines hav
1d850 65 20 62 65 65 6e 20 63 61 6c 6c 20 61 6e 64 20  e been call and 
1d860 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64  remain unchanged
1d870 20 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2f 0a   thereafter..*/.
1d880 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 63 68  SQLITE_EXTERN ch
1d890 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d 70  ar *sqlite3_temp
1d8a0 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a 2f 2a 0a  _directory;../*.
1d8b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20 54 65  ** CAPI3REF:  Te
1d8c0 73 74 20 54 6f 20 53 65 65 20 49 66 20 54 68 65  st To See If The
1d8d0 20 44 61 74 61 62 61 73 65 20 49 73 20 49 6e 20   Database Is In 
1d8e0 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64 65  Auto-Commit Mode
1d8f0 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73  .**.** Test to s
1d900 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ee whether or no
1d910 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  t the database c
1d920 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
1d930 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f  autocommit.** mo
1d940 64 65 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45  de.  Return TRUE
1d950 20 69 66 20 69 74 20 69 73 20 61 6e 64 20 46 41   if it is and FA
1d960 4c 53 45 20 69 66 20 6e 6f 74 2e 20 20 41 75 74  LSE if not.  Aut
1d970 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
1d980 6f 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75 6c 74  on.** by default
1d990 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 69 73  .  Autocommit is
1d9a0 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 42   disabled by a B
1d9b0 45 47 49 4e 20 73 74 61 74 65 6d 65 6e 74 20 61  EGIN statement a
1d9c0 6e 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a 2a 20  nd reenabled.** 
1d9d0 62 79 20 74 68 65 20 6e 65 78 74 20 43 4f 4d 4d  by the next COMM
1d9e0 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e 0a  IT or ROLLBACK..
1d9f0 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72  **.** If another
1da00 20 74 68 72 65 61 64 20 63 68 61 6e 67 65 73 20   thread changes 
1da10 74 68 65 20 61 75 74 6f 63 6f 6d 6d 69 74 20 73  the autocommit s
1da20 74 61 74 75 73 20 6f 66 20 74 68 65 20 64 61 74  tatus of the dat
1da30 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
1da40 69 6f 6e 20 77 68 69 6c 65 20 74 68 69 73 20 72  ion while this r
1da50 6f 75 74 69 6e 65 20 69 73 20 72 75 6e 6e 69 6e  outine is runnin
1da60 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74 75  g, then the retu
1da70 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 75  rn value.** is u
1da80 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  ndefined..*/.SQL
1da90 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1daa0 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d  te3_get_autocomm
1dab0 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f  it(sqlite3*);../
1dac0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20  *.** CAPI3REF:  
1dad0 46 69 6e 64 20 54 68 65 20 44 61 74 61 62 61 73  Find The Databas
1dae0 65 20 48 61 6e 64 6c 65 20 41 73 73 6f 63 69 61  e Handle Associa
1daf0 74 65 64 20 57 69 74 68 20 41 20 50 72 65 70 61  ted With A Prepa
1db00 72 65 64 20 53 74 61 74 65 6d 65 6e 74 0a 2a 2a  red Statement.**
1db10 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 5b  .** Return the [
1db20 73 71 6c 69 74 65 33 2a 5d 20 64 61 74 61 62 61  sqlite3*] databa
1db30 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68 69  se handle to whi
1db40 63 68 20 61 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ch a.** [sqlite3
1db50 5f 73 74 6d 74 20 7c 20 70 72 65 70 61 72 65 64  _stmt | prepared
1db60 20 73 74 61 74 65 6d 65 6e 74 5d 20 62 65 6c 6f   statement] belo
1db70 6e 67 73 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  ngs..** This is 
1db80 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
1db90 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61  e handle that wa
1dba0 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61  s.** the first a
1dbb0 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b  rgument to the [
1dbc0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
1dbd0 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72  v2()] or its var
1dbe0 69 61 6e 74 73 0a 2a 2a 20 74 68 61 74 20 77 61  iants.** that wa
1dbf0 73 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  s used to create
1dc00 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
1dc10 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63  n the first plac
1dc20 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
1dc30 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
1dc40 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69  3_db_handle(sqli
1dc50 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 0a 2f 2a  te3_stmt*);.../*
1dc60 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f  .** CAPI3REF: Co
1dc70 6d 6d 69 74 20 41 6e 64 20 52 6f 6c 6c 62 61 63  mmit And Rollbac
1dc80 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43  k Notification C
1dc90 61 6c 6c 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20 54  allbacks.**.** T
1dca0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  hese routines.**
1dcb0 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61   register callba
1dcc0 63 6b 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  ck functions to 
1dcd0 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65  be invoked whene
1dce0 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ver a transactio
1dcf0 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74 65  n.** is committe
1dd00 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b  d or rolled back
1dd10 2e 20 20 54 68 65 20 70 41 72 67 20 61 72 67 75  .  The pArg argu
1dd20 6d 65 6e 74 20 69 73 20 70 61 73 73 65 64 20 74  ment is passed t
1dd30 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65  hrough.** to the
1dd40 20 63 61 6c 6c 62 61 63 6b 2e 20 20 49 66 20 74   callback.  If t
1dd50 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 61  he callback on a
1dd60 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 66 75 6e   commit hook fun
1dd70 63 74 69 6f 6e 20 0a 2a 2a 20 72 65 74 75 72 6e  ction .** return
1dd80 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1dd90 20 74 68 65 20 63 6f 6d 6d 69 74 20 69 73 20 63   the commit is c
1dda0 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20  onverted into a 
1ddb0 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  rollback..**.** 
1ddc0 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74  If another funct
1ddd0 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75 73  ion was previous
1dde0 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20 69  ly registered, i
1ddf0 74 73 20 70 41 72 67 20 76 61 6c 75 65 20 69 73  ts pArg value is
1de00 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74   returned..** Ot
1de10 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 73 20  herwise NULL is 
1de20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
1de30 52 65 67 69 73 74 65 72 69 6e 67 20 61 20 4e 55  Registering a NU
1de40 4c 4c 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61  LL function disa
1de50 62 6c 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63  bles the callbac
1de60 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  k..**.** For the
1de70 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69   purposes of thi
1de80 73 20 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63  s API, a transac
1de90 74 69 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20  tion is said to 
1dea0 68 61 76 65 20 62 65 65 6e 20 0a 2a 2a 20 72 6f  have been .** ro
1deb0 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20  lled back if an 
1dec0 65 78 70 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41  explicit "ROLLBA
1ded0 43 4b 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73  CK" statement is
1dee0 20 65 78 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a   executed, or.**
1def0 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e   an error or con
1df00 73 74 72 61 69 6e 74 20 63 61 75 73 65 73 20 61  straint causes a
1df10 6e 20 69 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62  n implicit rollb
1df20 61 63 6b 20 74 6f 20 6f 63 63 75 72 2e 20 54 68  ack to occur. Th
1df30 65 20 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69  e .** callback i
1df40 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66  s not invoked if
1df50 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
1df60 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
1df70 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 62  rolled.** back b
1df80 65 63 61 75 73 65 20 74 68 65 20 64 61 74 61 62  ecause the datab
1df90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
1dfa0 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  s closed..**.** 
1dfb0 54 68 65 73 65 20 61 72 65 20 65 78 70 65 72 69  These are experi
1dfc0 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65  mental interface
1dfd0 73 20 61 6e 64 20 61 72 65 20 73 75 62 6a 65 63  s and are subjec
1dfe0 74 20 74 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  t to change..*/.
1dff0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1e000 2a 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f  *sqlite3_commit_
1e010 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69  hook(sqlite3*, i
1e020 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f  nt(*)(void*), vo
1e030 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  id*);.SQLITE_API
1e040 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72   void *sqlite3_r
1e050 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c  ollback_hook(sql
1e060 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76  ite3*, void(*)(v
1e070 6f 69 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a  oid *), void*);.
1e080 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1e090 20 44 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74   Data Change Not
1e0a0 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61  ification Callba
1e0b0 63 6b 73 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74  cks.**.** Regist
1e0c0 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75  er a callback fu
1e0d0 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
1e0e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1e0f0 69 6f 6e 20 69 64 65 6e 74 69 66 69 65 64 20 62  ion identified b
1e100 79 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20  y the .** first 
1e110 61 72 67 75 6d 65 6e 74 20 74 6f 20 62 65 20 69  argument to be i
1e120 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20  nvoked whenever 
1e130 61 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64  a row is updated
1e140 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65  , inserted or de
1e150 6c 65 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61  leted..** Any ca
1e160 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 61 20  llback set by a 
1e170 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f  previous call to
1e180 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   this function f
1e190 6f 72 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20  or the same .** 
1e1a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1e1b0 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65  ion is overridde
1e1c0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  n..**.** The sec
1e1d0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
1e1e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1e1f0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76   function to inv
1e200 6f 6b 65 20 77 68 65 6e 20 61 20 0a 2a 2a 20 72  oke when a .** r
1e210 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20 69  ow is updated, i
1e220 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74  nserted or delet
1e230 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72  ed. The first ar
1e240 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 63 61  gument to the ca
1e250 6c 6c 62 61 63 6b 20 69 73 0a 2a 2a 20 61 20 63  llback is.** a c
1e260 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 72 64  opy of the third
1e270 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
1e280 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b  ite3_update_hook
1e290 28 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 63  (). The second c
1e2a0 61 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 72 67 75  allback .** argu
1e2b0 6d 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20 53  ment is one of S
1e2c0 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 51  QLITE_INSERT, SQ
1e2d0 4c 49 54 45 5f 44 45 4c 45 54 45 20 6f 72 20 53  LITE_DELETE or S
1e2e0 51 4c 49 54 45 5f 55 50 44 41 54 45 2c 20 64 65  QLITE_UPDATE, de
1e2f0 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68  pending.** on th
1e300 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74  e operation that
1e310 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c   caused the call
1e320 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b  back to be invok
1e330 65 64 2e 20 54 68 65 20 74 68 69 72 64 20 61 6e  ed. The third an
1e340 64 20 0a 2a 2a 20 66 6f 75 72 74 68 20 61 72 67  d .** fourth arg
1e350 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61  uments to the ca
1e360 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70  llback contain p
1e370 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 64  ointers to the d
1e380 61 74 61 62 61 73 65 20 61 6e 64 20 0a 2a 2a 20  atabase and .** 
1e390 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61  table name conta
1e3a0 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63 74  ining the affect
1e3b0 65 64 20 72 6f 77 2e 20 54 68 65 20 66 69 6e 61  ed row. The fina
1e3c0 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 6d  l callback param
1e3d0 65 74 65 72 20 69 73 20 0a 2a 2a 20 74 68 65 20  eter is .** the 
1e3e0 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77  rowid of the row
1e3f0 2e 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f 66  . In the case of
1e400 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68 69 73   an update, this
1e410 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 61 66   is the rowid af
1e420 74 65 72 20 0a 2a 2a 20 74 68 65 20 75 70 64 61  ter .** the upda
1e430 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a  te takes place..
1e440 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65  **.** The update
1e450 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76   hook is not inv
1e460 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e  oked when intern
1e470 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73  al system tables
1e480 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64   are.** modified
1e490 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61   (i.e. sqlite_ma
1e4a0 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f  ster and sqlite_
1e4b0 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a  sequence)..**.**
1e4c0 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63   If another func
1e4d0 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75  tion was previou
1e4e0 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20  sly registered, 
1e4f0 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 20 69  its pArg value i
1e500 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f  s returned..** O
1e510 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 73  therwise NULL is
1e520 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
1e530 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
1e540 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
1e550 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  ok(.  sqlite3*, 
1e560 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20  .  void(*)(void 
1e570 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73  *,int ,char cons
1e580 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a  t *,char const *
1e590 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c  ,sqlite3_int64),
1e5a0 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a  .  void*.);../*.
1e5b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20 45 6e  ** CAPI3REF:  En
1e5c0 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20  able Or Disable 
1e5d0 53 68 61 72 65 64 20 50 61 67 65 72 20 43 61 63  Shared Pager Cac
1e5e0 68 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  he.**.** This ro
1e5f0 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72  utine enables or
1e600 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 73 68   disables the sh
1e610 61 72 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74  aring of the dat
1e620 61 62 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 61  abase cache.** a
1e630 6e 64 20 73 63 68 65 6d 61 20 64 61 74 61 20 73  nd schema data s
1e640 74 72 75 63 74 75 72 65 73 20 62 65 74 77 65 65  tructures betwee
1e650 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f  n connections to
1e660 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
1e670 73 65 2e 0a 2a 2a 20 53 68 61 72 69 6e 67 20 69  se..** Sharing i
1e680 73 20 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65  s enabled if the
1e690 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75   argument is tru
1e6a0 65 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69  e and disabled i
1e6b0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a  f the argument.*
1e6c0 2a 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a  * is false..**.*
1e6d0 2a 20 42 65 67 69 6e 6e 69 6e 67 20 69 6e 20 53  * Beginning in S
1e6e0 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e  QLite version 3.
1e6f0 35 2e 30 2c 20 63 61 63 68 65 20 73 68 61 72 69  5.0, cache shari
1e700 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 61 6e  ng is enabled an
1e710 64 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 66 6f  d disabled.** fo
1e720 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 6f 63  r an entire proc
1e730 65 73 73 2e 20 20 49 6e 20 70 72 69 6f 72 20 76  ess.  In prior v
1e740 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
1e750 65 2c 20 73 68 61 72 69 6e 67 20 77 61 73 0a 2a  e, sharing was.*
1e760 2a 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73  * enabled or dis
1e770 61 62 6c 65 64 20 66 6f 72 20 65 61 63 68 20 74  abled for each t
1e780 68 72 65 61 64 20 73 65 70 61 72 61 74 65 6c 79  hread separately
1e790 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68  ..**.** The cach
1e7a0 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 20 73  e sharing mode s
1e7b0 65 74 20 62 79 20 74 68 69 73 20 69 6e 74 65 72  et by this inter
1e7c0 66 61 63 65 20 65 66 66 65 63 74 73 20 61 6c 6c  face effects all
1e7d0 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63   subsequent.** c
1e7e0 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
1e7f0 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74  _open()], [sqlit
1e800 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61  e3_open_v2()], a
1e810 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  nd [sqlite3_open
1e820 31 36 28 29 5d 2e 0a 2a 2a 20 45 78 69 73 74 69  16()]..** Existi
1e830 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ng database conn
1e840 65 63 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65  ections continue
1e850 20 75 73 65 20 74 68 65 20 73 68 61 72 69 6e 67   use the sharing
1e860 20 6d 6f 64 65 20 74 68 61 74 20 77 61 73 0a 2a   mode that was.*
1e870 2a 20 69 6e 20 65 66 66 65 63 74 20 61 74 20 74  * in effect at t
1e880 68 65 20 74 69 6d 65 20 74 68 65 79 20 77 65 72  he time they wer
1e890 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  e opened..**.** 
1e8a0 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63  Virtual tables c
1e8b0 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69  annot be used wi
1e8c0 74 68 20 61 20 73 68 61 72 65 64 20 63 61 63 68  th a shared cach
1e8d0 65 2e 20 20 57 68 65 6e 20 73 68 61 72 65 64 0a  e.  When shared.
1e8e0 2a 2a 20 63 61 63 68 65 20 69 73 20 65 6e 61 62  ** cache is enab
1e8f0 6c 65 64 2c 20 74 68 65 20 5b 73 71 6c 69 74 65  led, the [sqlite
1e900 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28  3_create_module(
1e910 29 5d 20 41 50 49 20 75 73 65 64 20 74 6f 20 72  )] API used to r
1e920 65 67 69 73 74 65 72 0a 2a 2a 20 76 69 72 74 75  egister.** virtu
1e930 61 6c 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 61  al tables will a
1e940 6c 77 61 79 73 20 72 65 74 75 72 6e 20 61 6e 20  lways return an 
1e950 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  error..**.** Thi
1e960 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
1e970 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66  s [SQLITE_OK] if
1e980 20 73 68 61 72 65 64 20 63 61 63 68 65 20 77 61   shared cache wa
1e990 73 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 6f 72 20  s.** enabled or 
1e9a0 64 69 73 61 62 6c 65 64 20 73 75 63 63 65 73 73  disabled success
1e9b0 66 75 6c 6c 79 2e 20 20 41 6e 20 5b 53 51 4c 49  fully.  An [SQLI
1e9c0 54 45 5f 45 52 52 4f 52 20 7c 20 65 72 72 6f 72  TE_ERROR | error
1e9d0 20 63 6f 64 65 5d 0a 2a 2a 20 69 73 20 72 65 74   code].** is ret
1e9e0 75 72 6e 65 64 20 6f 74 68 65 72 77 69 73 65 2e  urned otherwise.
1e9f0 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 20 63 61  .**.** Shared ca
1ea00 63 68 65 20 69 73 20 64 69 73 61 62 6c 65 64 20  che is disabled 
1ea10 62 79 20 64 65 66 61 75 6c 74 2e 20 20 42 75 74  by default.  But
1ea20 20 74 68 69 73 20 6d 69 67 68 74 20 63 68 61 6e   this might chan
1ea30 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20  ge in.** future 
1ea40 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
1ea50 74 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  te.  Application
1ea60 73 20 74 68 61 74 20 63 61 72 65 20 61 62 6f 75  s that care abou
1ea70 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68  t shared.** cach
1ea80 65 20 73 65 74 74 69 6e 67 20 73 68 6f 75 6c 64  e setting should
1ea90 20 73 65 74 20 69 74 20 65 78 70 6c 69 63 69 74   set it explicit
1eaa0 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ly..*/.SQLITE_AP
1eab0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e  I int sqlite3_en
1eac0 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
1ead0 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  e(int);../*.** C
1eae0 41 50 49 33 52 45 46 3a 20 20 41 74 74 65 6d 70  API3REF:  Attemp
1eaf0 74 20 54 6f 20 46 72 65 65 20 48 65 61 70 20 4d  t To Free Heap M
1eb00 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 41 74 74 65  emory.**.** Atte
1eb10 6d 70 74 20 74 6f 20 66 72 65 65 20 4e 20 62 79  mpt to free N by
1eb20 74 65 73 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f  tes of heap memo
1eb30 72 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69  ry by deallocati
1eb40 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c  ng non-essential
1eb50 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  .** memory alloc
1eb60 61 74 69 6f 6e 73 20 68 65 6c 64 20 62 79 20 74  ations held by t
1eb70 68 65 20 64 61 74 61 62 61 73 65 20 6c 69 62 72  he database libr
1eb80 61 72 79 20 28 65 78 61 6d 70 6c 65 3a 20 6d 65  ary (example: me
1eb90 6d 6f 72 79 20 0a 2a 2a 20 75 73 65 64 20 74 6f  mory .** used to
1eba0 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65 20   cache database 
1ebb0 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 65  pages to improve
1ebc0 20 70 65 72 66 6f 72 6d 61 6e 63 65 29 2e 0a 2a   performance)..*
1ebd0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1ebe0 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
1ebf0 5f 6d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f  _memory(int);../
1ec00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20  *.** CAPI3REF:  
1ec10 49 6d 70 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f  Impose A Limit O
1ec20 6e 20 48 65 61 70 20 53 69 7a 65 0a 2a 2a 0a 2a  n Heap Size.**.*
1ec30 2a 20 50 6c 61 63 65 20 61 20 22 73 6f 66 74 22  * Place a "soft"
1ec40 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 61 6d   limit on the am
1ec50 6f 75 6e 74 20 6f 66 20 68 65 61 70 20 6d 65 6d  ount of heap mem
1ec60 6f 72 79 20 74 68 61 74 20 6d 61 79 20 62 65 20  ory that may be 
1ec70 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20  allocated.** by 
1ec80 53 51 4c 69 74 65 2e 20 20 49 66 20 61 6e 20 69  SQLite.  If an i
1ec90 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 69  nternal allocati
1eca0 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 20  on is requested 
1ecb0 0a 2a 2a 20 74 68 61 74 20 77 6f 75 6c 64 20 65  .** that would e
1ecc0 78 63 65 65 64 20 74 68 65 20 73 70 65 63 69 66  xceed the specif
1ecd0 69 65 64 20 6c 69 6d 69 74 2c 20 5b 73 71 6c 69  ied limit, [sqli
1ece0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
1ecf0 72 79 28 29 5d 20 69 73 0a 2a 2a 20 69 6e 76 6f  ry()] is.** invo
1ed00 6b 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ked one or more 
1ed10 74 69 6d 65 73 20 74 6f 20 66 72 65 65 20 75 70  times to free up
1ed20 20 73 6f 6d 65 20 73 70 61 63 65 20 62 65 66 6f   some space befo
1ed30 72 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  re the allocatio
1ed40 6e 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 0a 2a 2a  n.** is made..**
1ed50 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74 20 69 73  .** The limit is
1ed60 20 63 61 6c 6c 65 64 20 22 73 6f 66 74 22 2c 20   called "soft", 
1ed70 62 65 63 61 75 73 65 20 69 66 20 5b 73 71 6c 69  because if [sqli
1ed80 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
1ed90 72 79 28 29 5d 20 63 61 6e 6e 6f 74 0a 2a 2a 20  ry()] cannot.** 
1eda0 66 72 65 65 20 73 75 66 66 69 63 69 65 6e 74 20  free sufficient 
1edb0 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65 76 65 6e  memory to preven
1edc0 74 20 74 68 65 20 6c 69 6d 69 74 20 66 72 6f 6d  t the limit from
1edd0 20 62 65 69 6e 67 20 65 78 63 65 65 64 65 64 2c   being exceeded,
1ede0 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 69  .** the memory i
1edf0 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79 77  s allocated anyw
1ee00 61 79 20 61 6e 64 20 74 68 65 20 63 75 72 72 65  ay and the curre
1ee10 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f  nt operation pro
1ee20 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e  ceeds..**.** A n
1ee30 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20  egative or zero 
1ee40 76 61 6c 75 65 20 66 6f 72 20 4e 20 6d 65 61 6e  value for N mean
1ee50 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20  s that there is 
1ee60 6e 6f 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  no soft heap lim
1ee70 69 74 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74  it and.** [sqlit
1ee80 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
1ee90 79 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62  y()] will only b
1eea0 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65  e called when me
1eeb0 6d 6f 72 79 20 69 73 20 65 78 68 61 75 73 74 65  mory is exhauste
1eec0 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  d..** The defaul
1eed0 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
1eee0 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20  soft heap limit 
1eef0 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53  is zero..**.** S
1ef00 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 62 65  QLite makes a be
1ef10 73 74 20 65 66 66 6f 72 74 20 74 6f 20 68 6f 6e  st effort to hon
1ef20 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  or the soft heap
1ef30 20 6c 69 6d 69 74 2e 20 20 42 75 74 20 69 66 20   limit.  But if 
1ef40 69 74 0a 2a 2a 20 69 73 20 75 6e 61 62 6c 65 20  it.** is unable 
1ef50 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79  to reduce memory
1ef60 20 75 73 61 67 65 20 62 65 6c 6f 77 20 74 68 65   usage below the
1ef70 20 73 6f 66 74 20 6c 69 6d 69 74 2c 20 65 78 65   soft limit, exe
1ef80 63 75 74 69 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 63  cution will.** c
1ef90 6f 6e 74 69 6e 75 65 20 77 69 74 68 6f 75 74 20  ontinue without 
1efa0 65 72 72 6f 72 20 6f 72 20 6e 6f 74 69 66 69 63  error or notific
1efb0 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20  ation.  This is 
1efc0 77 68 79 20 74 68 65 20 6c 69 6d 69 74 20 69 73  why the limit is
1efd0 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 20 22 73   .** called a "s
1efe0 6f 66 74 22 20 6c 69 6d 69 74 2e 20 20 49 74 20  oft" limit.  It 
1eff0 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79  is advisory only
1f000 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 66 74  ..**.** The soft
1f010 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 69   heap limit is i
1f020 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67  mplemented using
1f030 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65   the [sqlite3_me
1f040 6d 6f 72 79 5f 61 6c 61 72 6d 28 29 5d 0a 2a 2a  mory_alarm()].**
1f050 20 69 6e 74 65 72 66 61 63 65 2e 20 20 4f 6e 6c   interface.  Onl
1f060 79 20 61 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72  y a single memor
1f070 79 20 61 6c 61 72 6d 20 69 73 20 61 76 61 69 6c  y alarm is avail
1f080 61 62 6c 65 20 69 6e 20 74 68 65 20 64 65 66 61  able in the defa
1f090 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ult.** implement
1f0a0 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 6d 65 61  ation.  This mea
1f0b0 6e 73 20 74 68 61 74 20 69 66 20 74 68 65 20 61  ns that if the a
1f0c0 70 70 6c 69 63 61 74 69 6f 6e 20 61 6c 73 6f 20  pplication also 
1f0d0 75 73 65 73 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f  uses the.** memo
1f0e0 72 79 20 61 6c 61 72 6d 20 69 6e 74 65 72 66 61  ry alarm interfa
1f0f0 63 65 20 69 74 20 77 69 6c 6c 20 69 6e 74 65 72  ce it will inter
1f100 66 65 72 65 20 77 69 74 68 20 74 68 65 20 6f 70  fere with the op
1f110 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a  eration of the.*
1f120 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  * soft heap limi
1f130 74 20 61 6e 64 20 75 6e 64 65 66 69 6e 65 64 20  t and undefined 
1f140 62 65 68 61 76 69 6f 72 20 77 69 6c 6c 20 72 65  behavior will re
1f150 73 75 6c 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 72  sult.  .**.** Pr
1f160 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65  ior to SQLite ve
1f170 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69  rsion 3.5.0, thi
1f180 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63  s routine only c
1f190 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d  onstrained the m
1f1a0 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
1f1b0 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74  ed by a single t
1f1c0 68 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65  hread - the same
1f1d0 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68   thread in which
1f1e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
1f1f0 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e   runs.  Beginnin
1f200 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65  g with SQLite ve
1f210 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65  rsion 3.5.0, the
1f220 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
1f230 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74   is.** applied t
1f240 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 20  o all threads.  
1f250 54 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66  The value specif
1f260 69 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74  ied for the soft
1f270 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69   heap limit.** i
1f280 73 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64  s an upper bound
1f290 20 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65   on the total me
1f2a0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1f2b0 66 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e  for all threads.
1f2c0 20 20 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20    In.** version 
1f2d0 33 2e 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e  3.5.0 there is n
1f2e0 6f 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20  o mechanism for 
1f2f0 6c 69 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61  limiting the hea
1f300 70 20 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69  p usage for.** i
1f310 6e 64 69 76 69 64 75 61 6c 20 74 68 72 65 61 64  ndividual thread
1f320 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
1f330 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f   void sqlite3_so
1f340 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e  ft_heap_limit(in
1f350 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
1f360 52 45 46 3a 20 20 45 78 74 72 61 63 74 20 4d 65  REF:  Extract Me
1f370 74 61 64 61 74 61 20 41 62 6f 75 74 20 41 20 43  tadata About A C
1f380 6f 6c 75 6d 6e 20 4f 66 20 41 20 54 61 62 6c 65  olumn Of A Table
1f390 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1f3a0 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 6d  ine.** returns m
1f3b0 65 74 61 2d 64 61 74 61 20 61 62 6f 75 74 20 61  eta-data about a
1f3c0 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e   specific column
1f3d0 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 20 64   of a specific d
1f3e0 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65  atabase.** table
1f3f0 20 61 63 63 65 73 73 69 62 6c 65 20 75 73 69 6e   accessible usin
1f400 67 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  g the connection
1f410 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61   handle passed a
1f420 73 20 74 68 65 20 66 69 72 73 74 20 66 75 6e 63  s the first func
1f430 74 69 6f 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e  tion .** argumen
1f440 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c  t..**.** The col
1f450 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69 65  umn is identifie
1f460 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 2c  d by the second,
1f470 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74   third and fourt
1f480 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  h parameters to 
1f490 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
1f4a0 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61  n. The second pa
1f4b0 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65  rameter is eithe
1f4c0 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  r the name of th
1f4d0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69  e database.** (i
1f4e0 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d  .e. "main", "tem
1f4f0 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65  p" or an attache
1f500 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74  d database) cont
1f510 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69  aining the speci
1f520 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72  fied.** table or
1f530 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20   NULL. If it is 
1f540 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61  NULL, then all a
1f550 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1f560 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a  s are searched.*
1f570 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  * for the table 
1f580 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61  using the same a
1f590 6c 67 6f 72 69 74 68 6d 20 61 73 20 74 68 65 20  lgorithm as the 
1f5a0 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20  database engine 
1f5b0 75 73 65 73 20 74 6f 20 0a 2a 2a 20 72 65 73 6f  uses to .** reso
1f5c0 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  lve unqualified 
1f5d0 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73  table references
1f5e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72  ..**.** The thir
1f5f0 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72  d and fourth par
1f600 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20  ameters to this 
1f610 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65  function are the
1f620 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
1f630 6e 20 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68  n .** name of th
1f640 65 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e  e desired column
1f650 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  , respectively. 
1f660 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65  Neither of these
1f670 20 70 61 72 61 6d 65 74 65 72 73 20 0a 2a 2a 20   parameters .** 
1f680 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a  may be NULL..**.
1f690 2a 2a 20 4d 65 74 61 20 69 6e 66 6f 72 6d 61 74  ** Meta informat
1f6a0 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 20  ion is returned 
1f6b0 62 79 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68  by writing to th
1f6c0 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
1f6d0 6e 73 20 70 61 73 73 65 64 20 61 73 0a 2a 2a 20  ns passed as.** 
1f6e0 74 68 65 20 35 74 68 20 61 6e 64 20 73 75 62 73  the 5th and subs
1f6f0 65 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72  equent parameter
1f700 73 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  s to this functi
1f710 6f 6e 2e 20 41 6e 79 20 6f 66 20 74 68 65 73 65  on. Any of these
1f720 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6d   .** arguments m
1f730 61 79 20 62 65 20 4e 55 4c 4c 2c 20 69 6e 20 77  ay be NULL, in w
1f740 68 69 63 68 20 63 61 73 65 20 74 68 65 20 63 6f  hich case the co
1f750 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 6c 65 6d  rresponding elem
1f760 65 6e 74 20 6f 66 20 6d 65 74 61 20 0a 2a 2a 20  ent of meta .** 
1f770 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6f  information is o
1f780 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c  mmitted..**.** <
1f790 70 72 65 3e 0a 2a 2a 20 50 61 72 61 6d 65 74 65  pre>.** Paramete
1f7a0 72 20 20 20 20 20 4f 75 74 70 75 74 20 54 79 70  r     Output Typ
1f7b0 65 20 20 20 20 20 20 44 65 73 63 72 69 70 74 69  e      Descripti
1f7c0 6f 6e 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  on.** ----------
1f7d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1f7e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
1f7f0 20 20 35 74 68 20 20 20 20 20 20 20 20 20 63 6f    5th         co
1f800 6e 73 74 20 63 68 61 72 2a 20 20 20 20 20 20 44  nst char*      D
1f810 61 74 61 20 74 79 70 65 0a 2a 2a 20 20 20 36 74  ata type.**   6t
1f820 68 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  h         const 
1f830 63 68 61 72 2a 20 20 20 20 20 20 4e 61 6d 65 20  char*      Name 
1f840 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  of the default c
1f850 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1f860 65 20 0a 2a 2a 20 20 20 37 74 68 20 20 20 20 20  e .**   7th     
1f870 20 20 20 20 69 6e 74 20 20 20 20 20 20 20 20 20      int         
1f880 20 20 20 20 20 54 72 75 65 20 69 66 20 74 68 65       True if the
1f890 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f   column has a NO
1f8a0 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
1f8b0 74 0a 2a 2a 20 20 20 38 74 68 20 20 20 20 20 20  t.**   8th      
1f8c0 20 20 20 69 6e 74 20 20 20 20 20 20 20 20 20 20     int          
1f8d0 20 20 20 20 54 72 75 65 20 69 66 20 74 68 65 20      True if the 
1f8e0 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f  column is part o
1f8f0 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  f the PRIMARY KE
1f900 59 0a 2a 2a 20 20 20 39 74 68 20 20 20 20 20 20  Y.**   9th      
1f910 20 20 20 69 6e 74 20 20 20 20 20 20 20 20 20 20     int          
1f920 20 20 20 20 54 72 75 65 20 69 66 20 74 68 65 20      True if the 
1f930 63 6f 6c 75 6d 6e 20 69 73 20 41 55 54 4f 49 4e  column is AUTOIN
1f940 43 52 45 4d 45 4e 54 0a 2a 2a 20 3c 2f 70 72 65  CREMENT.** </pre
1f950 3e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  >.**.**.** The m
1f960 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f  emory pointed to
1f970 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74 65   by the characte
1f980 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72  r pointers retur
1f990 6e 65 64 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20  ned for the .** 
1f9a0 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
1f9b0 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   and collation s
1f9c0 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64  equence is valid
1f9d0 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20   only until the 
1f9e0 6e 65 78 74 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f  next .** call to
1f9f0 20 61 6e 79 20 73 71 6c 69 74 65 20 41 50 49 20   any sqlite API 
1fa00 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
1fa10 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64  If the specified
1fa20 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c   table is actual
1fa30 6c 79 20 61 20 76 69 65 77 2c 20 74 68 65 6e 20  ly a view, then 
1fa40 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  an error is retu
1fa50 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rned..**.** If t
1fa60 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c  he specified col
1fa70 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20  umn is "rowid", 
1fa80 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64  "oid" or "_rowid
1fa90 5f 22 20 61 6e 64 20 61 6e 20 0a 2a 2a 20 49 4e  _" and an .** IN
1faa0 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
1fab0 59 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 65 65  Y column has bee
1fac0 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63  n explicitly dec
1fad0 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  lared, then the 
1fae0 6f 75 74 70 75 74 20 0a 2a 2a 20 70 61 72 61 6d  output .** param
1faf0 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66 6f  eters are set fo
1fb00 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c 79  r the explicitly
1fb10 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e   declared column
1fb20 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  . If there is no
1fb30 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 64  .** explicitly d
1fb40 65 63 6c 61 72 65 64 20 49 50 4b 20 63 6f 6c 75  eclared IPK colu
1fb50 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74  mn, then the out
1fb60 70 75 74 20 70 61 72 61 6d 65 74 65 72 73 20 61  put parameters a
1fb70 72 65 20 73 65 74 20 61 73 20 0a 2a 2a 20 66 6f  re set as .** fo
1fb80 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72  llows:.**.** <pr
1fb90 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 20 74  e>.**     data t
1fba0 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0a 2a  ype: "INTEGER".*
1fbb0 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e 20  *     collation 
1fbc0 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41 52  sequence: "BINAR
1fbd0 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e 75  Y".**     not nu
1fbe0 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 72 69  ll: 0.**     pri
1fbf0 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a 20 20  mary key: 1.**  
1fc00 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d 65 6e     auto incremen
1fc10 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a 2a  t: 0.** </pre>.*
1fc20 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1fc30 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 20  on may load one 
1fc40 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73 20  or more schemas 
1fc50 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66 69  from database fi
1fc60 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72  les. If an.** er
1fc70 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
1fc80 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c 20  g this process, 
1fc90 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 65 73  or if the reques
1fca0 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c  ted table or col
1fcb0 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65  umn.** cannot be
1fcc0 20 66 6f 75 6e 64 2c 20 61 6e 20 53 51 4c 49 54   found, an SQLIT
1fcd0 45 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  E error code is 
1fce0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20  returned and an 
1fcf0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a  error message.**
1fd00 20 6c 65 66 74 20 69 6e 20 74 68 65 20 64 61 74   left in the dat
1fd10 61 62 61 73 65 20 68 61 6e 64 6c 65 20 28 74 6f  abase handle (to
1fd20 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73   be retrieved us
1fd30 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  ing sqlite3_errm
1fd40 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  sg())..**.** Thi
1fd50 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61 76  s API is only av
1fd60 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c  ailable if the l
1fd70 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69  ibrary was compi
1fd80 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20  led with the.** 
1fd90 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
1fda0 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 70 72  LUMN_METADATA pr
1fdb0 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f  eprocessor symbo
1fdc0 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51  l defined..*/.SQ
1fdd0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1fde0 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d  ite3_table_colum
1fdf0 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71  n_metadata(.  sq
1fe00 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
1fe10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
1fe20 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a  nection handle *
1fe30 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1fe40 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20  zDbName,        
1fe50 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65  /* Database name
1fe60 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f   or NULL */.  co
1fe70 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
1fe80 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62  Name,     /* Tab
1fe90 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e  le name */.  con
1fea0 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e  st char *zColumn
1feb0 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75  Name,    /* Colu
1fec0 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61  mn name */.  cha
1fed0 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61  r const **pzData
1fee0 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50  Type,    /* OUTP
1fef0 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74  UT: Declared dat
1ff00 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72  a type */.  char
1ff10 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53   const **pzCollS
1ff20 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55  eq,     /* OUTPU
1ff30 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  T: Collation seq
1ff40 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  uence name */.  
1ff50 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20  int *pNotNull,  
1ff60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1ff70 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e  UTPUT: True if N
1ff80 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
1ff90 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69  nt exists */.  i
1ffa0 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c  nt *pPrimaryKey,
1ffb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
1ffc0 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f  TPUT: True if co
1ffd0 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20  lumn part of PK 
1ffe0 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69  */.  int *pAutoi
1fff0 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nc              
20000 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
20010 20 69 66 20 63 6f 6c 75 6d 73 20 69 73 20 61 75   if colums is au
20020 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a  to-increment */.
20030 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
20040 45 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74 65  EF: Load An Exte
20050 6e 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 41 74 74 65  nsion.**.** Atte
20060 6d 70 74 20 74 6f 20 6c 6f 61 64 20 61 6e 20 53  mpt to load an S
20070 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20  QLite extension 
20080 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65  library containe
20090 64 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a  d in the file.**
200a0 20 7a 46 69 6c 65 2e 20 20 54 68 65 20 65 6e 74   zFile.  The ent
200b0 72 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f  ry point is zPro
200c0 63 2e 20 20 7a 50 72 6f 63 20 6d 61 79 20 62 65  c.  zProc may be
200d0 20 30 20 69 6e 20 77 68 69 63 68 20 63 61 73 65   0 in which case
200e0 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20   the.** name of 
200f0 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20  the entry point 
20100 64 65 66 61 75 6c 74 73 20 74 6f 20 22 73 71 6c  defaults to "sql
20110 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
20120 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  nit"..**.** Retu
20130 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f  rn [SQLITE_OK] o
20140 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53  n success and [S
20150 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20  QLITE_ERROR] if 
20160 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
20170 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rong..**.** If a
20180 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61  n error occurs a
20190 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e  nd pzErrMsg is n
201a0 6f 74 20 30 2c 20 74 68 65 6e 20 66 69 6c 6c 20  ot 0, then fill 
201b0 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20 0a  *pzErrMsg with .
201c0 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ** error message
201d0 20 74 65 78 74 2e 20 20 54 68 65 20 63 61 6c 6c   text.  The call
201e0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  ing function sho
201f0 75 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d 65  uld free this me
20200 6d 6f 72 79 0a 2a 2a 20 62 79 20 63 61 6c 6c 69  mory.** by calli
20210 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  ng [sqlite3_free
20220 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e  ()]..**.** Exten
20230 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 75 73  sion loading mus
20240 74 20 62 65 20 65 6e 61 62 6c 65 64 20 75 73 69  t be enabled usi
20250 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62  ng [sqlite3_enab
20260 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le_load_extensio
20270 6e 28 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f  n()].** prior to
20280 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 41 50   calling this AP
20290 49 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 77 69  I or an error wi
202a0 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a  ll be returned..
202b0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
202c0 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  t sqlite3_load_e
202d0 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69  xtension(.  sqli
202e0 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
202f0 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78    /* Load the ex
20300 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69  tension into thi
20310 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
20320 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
20330 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20   char *zFile,   
20340 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
20350 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63  shared library c
20360 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73  ontaining extens
20370 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
20380 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f  har *zProc,    /
20390 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20  * Entry point.  
203a0 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69  Derived from zFi
203b0 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61  le if 0 */.  cha
203c0 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20  r **pzErrMsg    
203d0 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20     /* Put error 
203e0 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20  message here if 
203f0 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  not 0 */.);../*.
20400 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20 45 6e  ** CAPI3REF:  En
20410 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20  able Or Disable 
20420 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e  Extension Loadin
20430 67 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f  g.**.** So as no
20440 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69  t to open securi
20450 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65  ty holes in olde
20460 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74  r applications t
20470 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65  hat are.** unpre
20480 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77 69  pared to deal wi
20490 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  th extension loa
204a0 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 6d  ding, and as a m
204b0 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 6e  eans of disablin
204c0 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c  g.** extension l
204d0 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 76 61  oading while eva
204e0 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 6e 74  luating user-ent
204f0 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 66 6f  ered SQL, the fo
20500 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 41 50 49 20 69  llowing.** API i
20510 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 74 75  s provided to tu
20520 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  rn the [sqlite3_
20530 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29  load_extension()
20540 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61  ] mechanism on a
20550 6e 64 0a 2a 2a 20 6f 66 66 2e 20 20 49 74 20 69  nd.** off.  It i
20560 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74  s off by default
20570 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 31  .  See ticket #1
20580 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 20  863..**.** Call 
20590 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74  this routine wit
205a0 68 20 6f 6e 6f 66 66 3d 3d 31 20 74 6f 20 74 75  h onoff==1 to tu
205b0 72 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  rn extension loa
205c0 64 69 6e 67 20 6f 6e 0a 2a 2a 20 61 6e 64 20 63  ding on.** and c
205d0 61 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e 6f 66  all it with onof
205e0 66 3d 3d 30 20 74 6f 20 74 75 72 6e 20 69 74 20  f==0 to turn it 
205f0 62 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a  back off again..
20600 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
20610 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65  t sqlite3_enable
20620 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
20630 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
20640 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20   onoff);../*.** 
20650 43 41 50 49 33 52 45 46 3a 20 4d 61 6b 65 20 41  CAPI3REF: Make A
20660 72 72 61 6e 67 65 6d 65 6e 74 73 20 54 6f 20 41  rrangements To A
20670 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61  utomatically Loa
20680 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 0a 2a  d An Extension.*
20690 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 6e  *.** Register an
206a0 20 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79   extension entry
206b0 20 70 6f 69 6e 74 20 74 68 61 74 20 69 73 20 61   point that is a
206c0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76  utomatically inv
206d0 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 65 76 65 72  oked.** whenever
206e0 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
206f0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70  connection is op
20700 65 6e 65 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 73  ened using.** [s
20710 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
20720 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
20730 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  )], or [sqlite3_
20740 6f 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a  open_v2()]..**.*
20750 2a 20 54 68 69 73 20 41 50 49 20 63 61 6e 20 62  * This API can b
20760 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 70 72 6f  e invoked at pro
20770 67 72 61 6d 20 73 74 61 72 74 75 70 20 69 6e 20  gram startup in 
20780 6f 72 64 65 72 20 74 6f 20 72 65 67 69 73 74 65  order to registe
20790 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  r.** one or more
207a0 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b   statically link
207b0 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 68  ed extensions th
207c0 61 74 20 77 69 6c 6c 20 62 65 20 61 76 61 69 6c  at will be avail
207d0 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e  able.** to all n
207e0 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ew database conn
207f0 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 44  ections..**.** D
20800 75 70 6c 69 63 61 74 65 20 65 78 74 65 6e 73 69  uplicate extensi
20810 6f 6e 73 20 61 72 65 20 64 65 74 65 63 74 65 64  ons are detected
20820 20 73 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73   so calling this
20830 20 72 6f 75 74 69 6e 65 20 6d 75 6c 74 69 70 6c   routine multipl
20840 65 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68 20  e.** times with 
20850 74 68 65 20 73 61 6d 65 20 65 78 74 65 6e 73 69  the same extensi
20860 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 0a  on is harmless..
20870 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
20880 6e 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e  ne stores a poin
20890 74 65 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e  ter to the exten
208a0 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79  sion in an array
208b0 0a 2a 2a 20 74 68 61 74 20 69 73 20 6f 62 74 61  .** that is obta
208c0 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
208d0 28 29 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20  ().  If you run 
208e0 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 0a 2a 2a  a memory leak.**
208f0 20 63 68 65 63 6b 65 72 20 6f 6e 20 79 6f 75 72   checker on your
20900 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20   program and it 
20910 72 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62  reports a leak b
20920 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 0a 2a  ecause of this.*
20930 2a 20 61 72 72 61 79 2c 20 74 68 65 6e 20 69 6e  * array, then in
20940 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 61 75  voke [sqlite3_au
20950 74 6f 6d 61 74 69 63 5f 65 78 74 65 6e 73 69 6f  tomatic_extensio
20960 6e 5f 72 65 73 65 74 28 29 5d 20 70 72 69 6f 72  n_reset()] prior
20970 0a 2a 2a 20 74 6f 20 73 68 75 74 64 6f 77 6e 20  .** to shutdown 
20980 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 6d 6f  to free the memo
20990 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 75 74 6f 6d 61  ry..**.** Automa
209a0 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 61  tic extensions a
209b0 70 70 6c 79 20 61 63 72 6f 73 73 20 61 6c 6c 20  pply across all 
209c0 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 54  threads..**.** T
209d0 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73  his interface is
209e0 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e   experimental an
209f0 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20  d is subject to 
20a00 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d  change or.** rem
20a10 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 65 20 72  oval in future r
20a20 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74  eleases of SQLit
20a30 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
20a40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74   int sqlite3_aut
20a50 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64  o_extension(void
20a60 20 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 3b 0a   *xEntryPoint);.
20a70 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
20a80 3a 20 52 65 73 65 74 20 41 75 74 6f 6d 61 74 69  : Reset Automati
20a90 63 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64  c Extension Load
20aa0 69 6e 67 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62 6c  ing.**.** Disabl
20ab0 65 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79  e all previously
20ac0 20 72 65 67 69 73 74 65 72 65 64 20 61 75 74 6f   registered auto
20ad0 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73  matic extensions
20ae0 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69  .  This.** routi
20af0 6e 65 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66  ne undoes the ef
20b00 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 69 6f  fect of all prio
20b10 72 20 5b 73 71 6c 69 74 65 33 5f 61 75 74 6f 6d  r [sqlite3_autom
20b20 61 74 69 63 5f 65 78 74 65 6e 73 69 6f 6e 28 29  atic_extension()
20b30 5d 0a 2a 2a 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a  ].** calls..**.*
20b40 2a 20 54 68 69 73 20 63 61 6c 6c 20 64 69 73 61  * This call disa
20b50 62 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 20 65  bled automatic e
20b60 78 74 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c  xtensions in all
20b70 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20   threads..**.** 
20b80 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69  This interface i
20b90 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61  s experimental a
20ba0 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f  nd is subject to
20bb0 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65   change or.** re
20bc0 6d 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 65 20  moval in future 
20bd0 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
20be0 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  te..*/.SQLITE_AP
20bf0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
20c00 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73  eset_auto_extens
20c10 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 0a 2f 2a 0a  ion(void);.../*.
20c20 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ****** EXPERIMEN
20c30 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f  TAL - subject to
20c40 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20   change without 
20c50 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  notice *********
20c60 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  *****.**.** The 
20c70 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  interface to the
20c80 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
20c90 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 72 72  echanism is curr
20ca0 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64  ently considered
20cb0 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69  .** to be experi
20cc0 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74  mental.  The int
20cd0 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61  erface might cha
20ce0 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69  nge in incompati
20cf0 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20  ble ways..** If 
20d00 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65  this is a proble
20d10 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f  m for you, do no
20d20 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66  t use the interf
20d30 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65  ace at this time
20d40 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
20d50 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
20d60 65 63 68 61 6e 69 73 6d 20 73 74 61 62 6c 69 7a  echanism stabliz
20d70 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c  es, we will decl
20d80 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72  are the.** inter
20d90 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70  face fixed, supp
20da0 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74  ort it indefinit
20db0 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20  ely, and remove 
20dc0 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f  this comment..*/
20dd0 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72  ../*.** Structur
20de0 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76  es used by the v
20df0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74  irtual table int
20e00 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 65  erface.*/.typede
20e10 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
20e20 5f 76 74 61 62 20 73 71 6c 69 74 65 33 5f 76 74  _vtab sqlite3_vt
20e30 61 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ab;.typedef stru
20e40 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
20e50 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 6e  _info sqlite3_in
20e60 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 65  dex_info;.typede
20e70 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
20e80 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 73 71 6c  _vtab_cursor sql
20e90 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
20ea0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
20eb0 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
20ec0 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b 0a  sqlite3_module;.
20ed0 0a 2f 2a 0a 2a 2a 20 41 20 6d 6f 64 75 6c 65 20  ./*.** A module 
20ee0 69 73 20 61 20 63 6c 61 73 73 20 6f 66 20 76 69  is a class of vi
20ef0 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 45  rtual tables.  E
20f00 61 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 64 65  ach module is de
20f10 66 69 6e 65 64 0a 2a 2a 20 62 79 20 61 6e 20 69  fined.** by an i
20f20 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
20f30 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
20f40 72 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  re.  This struct
20f50 75 72 65 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20  ure consists.** 
20f60 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68 6f 64  mostly of method
20f70 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
20f80 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
20f90 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69  te3_module {.  i
20fa0 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69  nt iVersion;.  i
20fb0 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 71  nt (*xCreate)(sq
20fc0 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41  lite3*, void *pA
20fd0 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ux,.            
20fe0 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e     int argc, con
20ff0 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
21000 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rgv,.           
21010 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
21020 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a   **ppVTab, char*
21030 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e  *);.  int (*xCon
21040 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20  nect)(sqlite3*, 
21050 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20  void *pAux,.    
21060 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61             int a
21070 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
21080 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20  *const*argv,.   
21090 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
210a0 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61  te3_vtab **ppVTa
210b0 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e  b, char**);.  in
210c0 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28  t (*xBestIndex)(
210d0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
210e0 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  Tab, sqlite3_ind
210f0 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74  ex_info*);.  int
21100 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28   (*xDisconnect)(
21110 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
21120 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  Tab);.  int (*xD
21130 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
21140 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20  vtab *pVTab);.  
21150 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c  int (*xOpen)(sql
21160 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
21170 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  , sqlite3_vtab_c
21180 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72  ursor **ppCursor
21190 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73  );.  int (*xClos
211a0 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  e)(sqlite3_vtab_
211b0 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20  cursor*);.  int 
211c0 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74  (*xFilter)(sqlit
211d0 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c  e3_vtab_cursor*,
211e0 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
211f0 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
21200 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21210 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
21220 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
21230 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29  ;.  int (*xNext)
21240 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
21250 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  rsor*);.  int (*
21260 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74  xEof)(sqlite3_vt
21270 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69  ab_cursor*);.  i
21280 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71  nt (*xColumn)(sq
21290 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
212a0 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  r*, sqlite3_cont
212b0 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e  ext*, int);.  in
212c0 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69  t (*xRowid)(sqli
212d0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
212e0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
212f0 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20  *pRowid);.  int 
21300 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 74  (*xUpdate)(sqlit
21310 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20  e3_vtab *, int, 
21320 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
21330 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
21340 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67  *);.  int (*xBeg
21350 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  in)(sqlite3_vtab
21360 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20   *pVTab);.  int 
21370 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33  (*xSync)(sqlite3
21380 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
21390 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28   int (*xCommit)(
213a0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
213b0 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52  Tab);.  int (*xR
213c0 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33  ollback)(sqlite3
213d0 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
213e0 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63   int (*xFindFunc
213f0 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  tion)(sqlite3_vt
21400 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e  ab *pVtab, int n
21410 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Arg, const char 
21420 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  *zName,.        
21430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
21440 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73  oid (**pxFunc)(s
21450 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
21460 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
21470 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20  e**),.          
21480 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69               voi
21490 64 20 2a 2a 70 70 41 72 67 29 3b 0a 0a 20 20 69  d **ppArg);..  i
214a0 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71  nt (*xRename)(sq
214b0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
214c0 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
214d0 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  New);.};../*.** 
214e0 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  The sqlite3_inde
214f0 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
21500 20 61 6e 64 20 69 74 73 20 73 75 62 73 74 72 75   and its substru
21510 63 74 75 72 65 73 20 69 73 20 75 73 65 64 20 74  ctures is used t
21520 6f 0a 2a 2a 20 70 61 73 73 20 69 6e 66 6f 72 6d  o.** pass inform
21530 61 74 69 6f 6e 20 69 6e 74 6f 20 61 6e 64 20 72  ation into and r
21540 65 63 65 69 76 65 20 74 68 65 20 72 65 70 6c 79  eceive the reply
21550 20 66 72 6f 6d 20 74 68 65 20 78 42 65 73 74 49   from the xBestI
21560 6e 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f  ndex.** method o
21570 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 6d 6f 64  f an sqlite3_mod
21580 75 6c 65 2e 20 20 54 68 65 20 66 69 65 6c 64 73  ule.  The fields
21590 20 75 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a   under **Inputs*
215a0 2a 20 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70  * are the.** inp
215b0 75 74 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65  uts to xBestInde
215c0 78 20 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f  x and are read-o
215d0 6e 6c 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78  nly.  xBestIndex
215e0 20 69 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20   inserts its.** 
215f0 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65  results into the
21600 20 2a 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65   **Outputs** fie
21610 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  lds..**.** The a
21620 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72  Constraint[] arr
21630 61 79 20 72 65 63 6f 72 64 73 20 57 48 45 52 45  ay records WHERE
21640 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69   clause constrai
21650 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f  nts of the.** fo
21660 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  rm:.**.**       
21670 20 20 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72    column OP expr
21680 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 4f 50 20  .**.** Where OP 
21690 69 73 20 3d 2c 20 3c 2c 20 3c 3d 2c 20 3e 2c 20  is =, <, <=, >, 
216a0 6f 72 20 3e 3d 2e 20 20 54 68 65 20 70 61 72 74  or >=.  The part
216b0 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f 72 20  icular operator 
216c0 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  is stored.** in 
216d0 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 6f 70  aConstraint[].op
216e0 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  .  The index of 
216f0 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73 74  the column is st
21700 6f 72 65 64 20 69 6e 20 0a 2a 2a 20 61 43 6f 6e  ored in .** aCon
21710 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d  straint[].iColum
21720 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  n.  aConstraint[
21730 5d 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55 45  ].usable is TRUE
21740 20 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 20   if the.** expr 
21750 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  on the right-han
21760 64 20 73 69 64 65 20 63 61 6e 20 62 65 20 65 76  d side can be ev
21770 61 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68 75  aluated (and thu
21780 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  s the constraint
21790 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20 61  .** is usable) a
217a0 6e 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 63  nd false if it c
217b0 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  annot..**.** The
217c0 20 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f 6d   optimizer autom
217d0 61 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74 73  atically inverts
217e0 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f   terms of the fo
217f0 72 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c 75  rm "expr OP colu
21800 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 73  mn".** and makes
21810 20 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69 63   other simplific
21820 61 74 69 6e 6f 73 20 74 6f 20 74 68 65 20 57 48  atinos to the WH
21830 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61 6e  ERE clause in an
21840 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 67   attempt to.** g
21850 65 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 45  et as many WHERE
21860 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e   clause terms in
21870 74 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f 77  to the form show
21880 6e 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 69  n above as possi
21890 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f 6e  ble..** The aCon
218a0 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20  straint[] array 
218b0 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48 45  only reports WHE
218c0 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  RE clause terms 
218d0 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a  in the correct.*
218e0 2a 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66 65  * form that refe
218f0 72 20 74 6f 20 74 68 65 20 70 61 72 74 69 63 75  r to the particu
21900 6c 61 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c  lar virtual tabl
21910 65 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 2e  e being queried.
21920 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69  .**.** Informati
21930 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 44  on about the ORD
21940 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ER BY clause is 
21950 73 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65 72  stored in aOrder
21960 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74 65  By[]..** Each te
21970 72 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20 72  rm of aOrderBy r
21980 65 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20  ecords a column 
21990 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
219a0 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  clause..**.** Th
219b0 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74  e xBestIndex met
219c0 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 43  hod must fill aC
219d0 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d  onstraintUsage[]
219e0 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f   with informatio
219f0 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 20  n.** about what 
21a00 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 61  parameters to pa
21a10 73 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 20  ss to xFilter.  
21a20 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 74  If argvIndex>0 t
21a30 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68 74  hen.** the right
21a40 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
21a50 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
21a60 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73  aConstraint[] is
21a70 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e   evaluated.** an
21a80 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61 72  d becomes the ar
21a90 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72 79  gvIndex-th entry
21aa0 20 69 6e 20 61 72 67 76 2e 20 20 49 66 20 61 43   in argv.  If aC
21ab0 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d  onstraintUsage[]
21ac0 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 75 65  .omit.** is true
21ad0 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74  , then the const
21ae0 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d 65 64  raint is assumed
21af0 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 61 6e   to be fully han
21b00 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 76  dled by the.** v
21b10 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64  irtual table and
21b20 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 20   is not checked 
21b30 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74 65 2e  again by SQLite.
21b40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78 4e 75  .**.** The idxNu
21b50 6d 20 61 6e 64 20 69 64 78 50 74 72 20 76 61 6c  m and idxPtr val
21b60 75 65 73 20 61 72 65 20 72 65 63 6f 72 64 65 64  ues are recorded
21b70 20 61 6e 64 20 70 61 73 73 65 64 20 69 6e 74 6f   and passed into
21b80 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 20 73 71 6c   xFilter..** sql
21b90 69 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 75  ite3_free() is u
21ba0 73 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 50  sed to free idxP
21bb0 74 72 20 69 66 20 6e 65 65 64 54 6f 46 72 65 65  tr if needToFree
21bc0 49 64 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a  IdxPtr is true..
21bd0 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42  **.** The orderB
21be0 79 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20  yConsumed means 
21bf0 74 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d  that output from
21c00 20 78 46 69 6c 74 65 72 20 77 69 6c 6c 20 6f 63   xFilter will oc
21c10 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63 6f  cur in.** the co
21c20 72 72 65 63 74 20 6f 72 64 65 72 20 74 6f 20 73  rrect order to s
21c30 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52  atisfy the ORDER
21c40 20 42 59 20 63 6c 61 75 73 65 20 73 6f 20 74 68   BY clause so th
21c50 61 74 20 6e 6f 20 73 65 70 61 72 61 74 65 0a 2a  at no separate.*
21c60 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 70 20 69  * sorting step i
21c70 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a  s required..**.*
21c80 2a 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 43  * The estimatedC
21c90 6f 73 74 20 76 61 6c 75 65 20 69 73 20 61 6e 20  ost value is an 
21ca0 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
21cb0 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 20 74 68  cost of doing th
21cc0 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20  e.** particular 
21cd0 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c 20  lookup.  A full 
21ce0 73 63 61 6e 20 6f 66 20 61 20 74 61 62 6c 65 20  scan of a table 
21cf0 77 69 74 68 20 4e 20 65 6e 74 72 69 65 73 20 73  with N entries s
21d00 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61 20  hould have.** a 
21d10 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41 20 62 69  cost of N.  A bi
21d20 6e 61 72 79 20 73 65 61 72 63 68 20 6f 66 20 61  nary search of a
21d30 20 74 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74 72   table of N entr
21d40 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20  ies should have 
21d50 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70 70  a.** cost of app
21d60 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28 4e  roximately log(N
21d70 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c  )..*/.struct sql
21d80 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
21d90 7b 0a 20 20 2f 2a 20 49 6e 70 75 74 73 20 2a 2f  {.  /* Inputs */
21da0 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f  .  const int nCo
21db0 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 2f 2a  nstraint;     /*
21dc0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
21dd0 65 73 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e  es in aConstrain
21de0 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 74 72  t */.  const str
21df0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
21e00 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20  x_constraint {. 
21e10 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b      int iColumn;
21e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21e30 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d   Column on left-
21e40 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 6f 6e  hand side of con
21e50 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20  straint */.     
21e60 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70  unsigned char op
21e70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ;         /* Con
21e80 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 6f 72  straint operator
21e90 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65   */.     unsigne
21ea0 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b 20 20  d char usable;  
21eb0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
21ec0 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  is constraint is
21ed0 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20   usable */.     
21ee0 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 74 3b  int iTermOffset;
21ef0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
21f00 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78  d internally - x
21f10 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 6c 64  BestIndex should
21f20 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a   ignore */.  } *
21f30 63 6f 6e 73 74 20 61 43 6f 6e 73 74 72 61 69 6e  const aConstrain
21f40 74 3b 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65  t;      /* Table
21f50 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 73 65   of WHERE clause
21f60 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
21f70 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4f 72 64    const int nOrd
21f80 65 72 42 79 3b 20 20 20 20 20 20 20 20 2f 2a 20  erBy;        /* 
21f90 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  Number of terms 
21fa0 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  in the ORDER BY 
21fb0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 63 6f 6e 73  clause */.  cons
21fc0 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  t struct sqlite3
21fd0 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 7b  _index_orderby {
21fe0 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d  .     int iColum
21ff0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
22000 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
22010 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65   */.     unsigne
22020 64 20 63 68 61 72 20 64 65 73 63 3b 20 20 20 20  d char desc;    
22030 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44     /* True for D
22040 45 53 43 2e 20 20 46 61 6c 73 65 20 66 6f 72 20  ESC.  False for 
22050 41 53 43 2e 20 2a 2f 0a 20 20 7d 20 2a 63 6f 6e  ASC. */.  } *con
22060 73 74 20 61 4f 72 64 65 72 42 79 3b 20 20 20 20  st aOrderBy;    
22070 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45       /* The ORDE
22080 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 0a  R BY clause */..
22090 20 20 2f 2a 20 4f 75 74 70 75 74 73 20 2a 2f 0a    /* Outputs */.
220a0 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
220b0 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
220c0 74 5f 75 73 61 67 65 20 7b 0a 20 20 20 20 69 6e  t_usage {.    in
220d0 74 20 61 72 67 76 49 6e 64 65 78 3b 20 20 20 20  t argvIndex;    
220e0 20 20 20 20 20 20 20 2f 2a 20 69 66 20 3e 30 2c         /* if >0,
220f0 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 70   constraint is p
22100 61 72 74 20 6f 66 20 61 72 67 76 20 74 6f 20 78  art of argv to x
22110 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20 20 75 6e  Filter */.    un
22120 73 69 67 6e 65 64 20 63 68 61 72 20 6f 6d 69 74  signed char omit
22130 3b 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74  ;      /* Do not
22140 20 63 6f 64 65 20 61 20 74 65 73 74 20 66 6f 72   code a test for
22150 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
22160 20 2a 2f 0a 20 20 7d 20 2a 63 6f 6e 73 74 20 61   */.  } *const a
22170 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b  ConstraintUsage;
22180 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20 20  .  int idxNum;  
22190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
221a0 20 4e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20   Number used to 
221b0 69 64 65 6e 74 69 66 79 20 74 68 65 20 69 6e 64  identify the ind
221c0 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 2a 69 64  ex */.  char *id
221d0 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20  xStr;           
221e0 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c 20 70 6f     /* String, po
221f0 73 73 69 62 6c 79 20 6f 62 74 61 69 6e 65 64 20  ssibly obtained 
22200 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
22210 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 65  loc */.  int nee
22220 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 20 20  dToFreeIdxStr;  
22230 20 20 20 20 2f 2a 20 46 72 65 65 20 69 64 78 53      /* Free idxS
22240 74 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  tr using sqlite3
22250 5f 66 72 65 65 28 29 20 69 66 20 74 72 75 65 20  _free() if true 
22260 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42 79  */.  int orderBy
22270 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 20 20 20  Consumed;       
22280 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 70 75  /* True if outpu
22290 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 72 64  t is already ord
222a0 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  ered */.  double
222b0 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b 20   estimatedCost; 
222c0 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65       /* Estimate
222d0 64 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20  d cost of using 
222e0 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b  this index */.};
222f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
22300 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
22310 5f 45 51 20 20 20 20 32 0a 23 64 65 66 69 6e 65  _EQ    2.#define
22320 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
22330 4e 53 54 52 41 49 4e 54 5f 47 54 20 20 20 20 34  NSTRAINT_GT    4
22340 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
22350 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
22360 5f 4c 45 20 20 20 20 38 0a 23 64 65 66 69 6e 65  _LE    8.#define
22370 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
22380 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20 20 20 31  NSTRAINT_LT    1
22390 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  6.#define SQLITE
223a0 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
223b0 54 5f 47 45 20 20 20 20 33 32 0a 23 64 65 66 69  T_GE    32.#defi
223c0 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ne SQLITE_INDEX_
223d0 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48  CONSTRAINT_MATCH
223e0 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   64../*.** This 
223f0 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
22400 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65  to register a ne
22410 77 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 77 69  w module name wi
22420 74 68 20 61 6e 20 53 51 4c 69 74 65 0a 2a 2a 20  th an SQLite.** 
22430 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 4d 6f 64  connection.  Mod
22440 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 74 20 62  ule names must b
22450 65 20 72 65 67 69 73 74 65 72 65 64 20 62 65 66  e registered bef
22460 6f 72 65 20 63 72 65 61 74 69 6e 67 20 6e 65 77  ore creating new
22470 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c  .** virtual tabl
22480 65 73 20 6f 6e 20 74 68 65 20 6d 6f 64 75 6c 65  es on the module
22490 2c 20 6f 72 20 62 65 66 6f 72 65 20 75 73 69 6e  , or before usin
224a0 67 20 70 72 65 65 78 69 73 74 69 6e 67 20 76 69  g preexisting vi
224b0 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 73 20  rtual.** tables 
224c0 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a  of the module..*
224d0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
224e0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
224f0 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  module(.  sqlite
22500 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
22510 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63       /* SQLite c
22520 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67  onnection to reg
22530 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74  ister module wit
22540 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  h */.  const cha
22550 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
22560 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
22570 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
22580 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
22590 65 20 2a 2c 20 20 20 20 2f 2a 20 4d 65 74 68 6f  e *,    /* Metho
225a0 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c  ds for the modul
225b0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 20 20 20  e */.  void *   
225c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
225d0 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61    /* Client data
225e0 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
225f0 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  nnect */.);../*.
22600 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
22610 69 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20  is identical to 
22620 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  the sqlite3_crea
22630 74 65 5f 6d 6f 64 75 6c 65 28 29 20 6d 65 74 68  te_module() meth
22640 6f 64 20 61 62 6f 76 65 2c 0a 2a 2a 20 65 78 63  od above,.** exc
22650 65 70 74 20 74 68 61 74 20 69 74 20 61 6c 6c 6f  ept that it allo
22660 77 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  ws a destructor 
22670 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 73  function to be s
22680 70 65 63 69 66 69 65 64 2e 20 49 74 20 69 73 0a  pecified. It is.
22690 2a 2a 20 65 76 65 6e 20 6d 6f 72 65 20 65 78 70  ** even more exp
226a0 65 72 69 6d 65 6e 74 61 6c 20 74 68 61 6e 20 74  erimental than t
226b0 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 76  he rest of the v
226c0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 41 50  irtual tables AP
226d0 49 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  I..*/.SQLITE_API
226e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
226f0 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a 20  ate_module_v2(. 
22700 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
22710 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
22720 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  QLite connection
22730 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d 6f 64   to register mod
22740 75 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 6f  ule with */.  co
22750 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
22760 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
22770 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a   of the module *
22780 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  /.  const sqlite
22790 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20 20 20 20 2f  3_module *,    /
227a0 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 68  * Methods for th
227b0 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f  e module */.  vo
227c0 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 20 20  id *,           
227d0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 65           /* Clie
227e0 6e 74 20 64 61 74 61 20 66 6f 72 20 78 43 72 65  nt data for xCre
227f0 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a  ate/xConnect */.
22800 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79    void(*xDestroy
22810 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a 20  )(void*)     /* 
22820 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f  Module destructo
22830 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 3b  r function */.);
22840 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72 79 20 6d 6f  ../*.** Every mo
22850 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
22860 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c  ion uses a subcl
22870 61 73 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  ass of the follo
22880 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a  wing structure.*
22890 2a 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20  * to describe a 
228a0 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61  particular insta
228b0 6e 63 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  nce of the modul
228c0 65 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73  e.  Each subclas
228d0 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 79  s will.** be tay
228e0 6c 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65  lored to the spe
228f0 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74  cific needs of t
22900 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  he module implem
22910 65 6e 74 61 74 69 6f 6e 2e 20 20 20 54 68 65 0a  entation.   The.
22920 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68  ** purpose of th
22930 69 73 20 73 75 70 65 72 63 6c 61 73 73 20 69 73  is superclass is
22940 20 74 6f 20 64 65 66 69 6e 65 20 63 65 72 74 61   to define certa
22950 69 6e 20 66 69 65 6c 64 73 20 74 68 61 74 20 61  in fields that a
22960 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 74 6f 20  re common.** to 
22970 61 6c 6c 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  all module imple
22980 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  mentations..**.*
22990 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73  * Virtual tables
229a0 20 6d 65 74 68 6f 64 73 20 63 61 6e 20 73 65 74   methods can set
229b0 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
229c0 65 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 61  e by assigning a
229d0 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 62 74 61 69  .** string obtai
229e0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
229f0 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20 7a 45  _mprintf() to zE
22a00 72 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68  rrMsg.  The meth
22a10 6f 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b  od should.** tak
22a20 65 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20  e care that any 
22a30 70 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20  prior string is 
22a40 66 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20  freed by a call 
22a50 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  to sqlite3_free(
22a60 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73  ).** prior to as
22a70 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74  signing a new st
22a80 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e  ring to zErrMsg.
22a90 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f    After the erro
22aa0 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20  r message.** is 
22ab0 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20  delivered up to 
22ac0 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69  the client appli
22ad0 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69  cation, the stri
22ae0 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d  ng will be autom
22af0 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65  atically.** free
22b00 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65  d by sqlite3_fre
22b10 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72  e() and the zErr
22b20 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62  Msg field will b
22b30 65 20 7a 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a  e zeroed.  Note.
22b40 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f  ** that sqlite3_
22b50 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71  mprintf() and sq
22b60 6c 69 74 65 33 5f 66 72 65 65 28 29 20 61 72 65  lite3_free() are
22b70 20 75 73 65 64 20 6f 6e 20 74 68 65 20 7a 45 72   used on the zEr
22b80 72 4d 73 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69  rMsg field.** si
22b90 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  nce virtual tabl
22ba0 65 73 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20  es are commonly 
22bb0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c  implemented in l
22bc0 6f 61 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f  oadable extensio
22bd0 6e 73 20 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e  ns which.** do n
22be0 6f 74 20 68 61 76 65 20 61 63 63 65 73 73 20 74  ot have access t
22bf0 6f 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  o sqlite3MPrintf
22c00 28 29 20 6f 72 20 73 71 6c 69 74 65 33 46 72 65  () or sqlite3Fre
22c10 65 28 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73  e()..*/.struct s
22c20 71 6c 69 74 65 33 5f 76 74 61 62 20 7b 0a 20 20  qlite3_vtab {.  
22c30 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
22c40 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20  dule *pModule;  
22c50 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f  /* The module fo
22c60 72 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 74  r this virtual t
22c70 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  able */.  int nR
22c80 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
22c90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
22ca0 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a  d internally */.
22cb0 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b    char *zErrMsg;
22cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22cd0 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61    /* Error messa
22ce0 67 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ge from sqlite3_
22cf0 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 20 20 2f  mprintf() */.  /
22d00 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * Virtual table 
22d10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
22d20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61  will typically a
22d30 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69  dd additional fi
22d40 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 45  elds */.};../* E
22d50 76 65 72 79 20 6d 6f 64 75 6c 65 20 69 6d 70 6c  very module impl
22d60 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20  ementation uses 
22d70 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68  a subclass of th
22d80 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
22d90 63 74 75 72 65 0a 2a 2a 20 74 6f 20 64 65 73 63  cture.** to desc
22da0 72 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61  ribe cursors tha
22db0 74 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65  t point into the
22dc0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61   virtual table a
22dd0 6e 64 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74  nd are used.** t
22de0 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  o loop through t
22df0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
22e00 2e 20 20 43 75 72 73 6f 72 73 20 61 72 65 20 63  .  Cursors are c
22e10 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 65  reated using the
22e20 0a 2a 2a 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  .** xOpen method
22e30 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20   of the module. 
22e40 20 45 61 63 68 20 6d 6f 64 75 6c 65 20 69 6d 70   Each module imp
22e50 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c  lementation will
22e60 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 63   define.** the c
22e70 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 75 72 73  ontent of a curs
22e80 6f 72 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  or structure to 
22e90 73 75 69 74 20 69 74 73 20 6f 77 6e 20 6e 65 65  suit its own nee
22ea0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  ds..**.** This s
22eb0 75 70 65 72 63 6c 61 73 73 20 65 78 69 73 74 73  uperclass exists
22ec0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 65 66   in order to def
22ed0 69 6e 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68  ine fields of th
22ee0 65 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a  e cursor that.**
22ef0 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61   are common to a
22f00 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ll implementatio
22f10 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  ns..*/.struct sq
22f20 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
22f30 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  r {.  sqlite3_vt
22f40 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20  ab *pVtab;      
22f50 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
22f60 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f 72 20   of this cursor 
22f70 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20  */.  /* Virtual 
22f80 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
22f90 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63  tions will typic
22fa0 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f  ally add additio
22fb0 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b  nal fields */.};
22fc0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65  ../*.** The xCre
22fd0 61 74 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74  ate and xConnect
22fe0 20 6d 65 74 68 6f 64 73 20 6f 66 20 61 20 6d 6f   methods of a mo
22ff0 64 75 6c 65 20 75 73 65 20 74 68 65 20 66 6f 6c  dule use the fol
23000 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 74 6f  lowing API.** to
23010 20 64 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72   declare the for
23020 6d 61 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61  mat (the names a
23030 6e 64 20 64 61 74 61 74 79 70 65 73 20 6f 66 20  nd datatypes of 
23040 74 68 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a  the columns) of.
23050 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ** the virtual t
23060 61 62 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65  ables they imple
23070 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
23080 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
23090 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73 71 6c  declare_vtab(sql
230a0 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
230b0 72 20 2a 7a 43 72 65 61 74 65 54 61 62 6c 65 29  r *zCreateTable)
230c0 3b 0a 0a 2f 2a 0a 2a 2a 20 56 69 72 74 75 61 6c  ;../*.** Virtual
230d0 20 74 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76   tables can prov
230e0 69 64 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20  ide alternative 
230f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
23100 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  of functions.** 
23110 75 73 69 6e 67 20 74 68 65 20 78 46 69 6e 64 46  using the xFindF
23120 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 2e 20  unction method. 
23130 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73   But global vers
23140 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75  ions of those fu
23150 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20  nctions.** must 
23160 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74  exist in order t
23170 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e  o be overloaded.
23180 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20  .**.** This API 
23190 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 6c 6f  makes sure a glo
231a0 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 61  bal version of a
231b0 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61   function with a
231c0 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 6e   particular.** n
231d0 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f  ame and number o
231e0 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 78 69  f parameters exi
231f0 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 68  sts.  If no such
23200 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73   function exists
23210 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20  .** before this 
23220 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 61  API is called, a
23230 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73   new function is
23240 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 69   created.  The i
23250 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  mplementation.**
23260 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75 6e 63   of the new func
23270 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 73  tion always caus
23280 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  es an exception 
23290 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 53  to be thrown.  S
232a0 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 6e  o.** the new fun
232b0 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f  ction is not goo
232c0 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 62  d for anything b
232d0 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73 20 6f  y itself.  Its o
232e0 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69  nly.** purpose i
232f0 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63 65 2d  s to be a place-
23300 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  holder function 
23310 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65 72  that can be over
23320 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 76 69 72  loaded.** by vir
23330 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a  tual tables..**.
23340 2a 2a 20 54 68 69 73 20 41 50 49 20 73 68 6f 75  ** This API shou
23350 6c 64 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64  ld be considered
23360 20 70 61 72 74 20 6f 66 20 74 68 65 20 76 69 72   part of the vir
23370 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 72  tual table inter
23380 66 61 63 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69  face,.** which i
23390 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61  s experimental a
233a0 6e 64 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  nd subject to ch
233b0 61 6e 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ange..*/.SQLITE_
233c0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
233d0 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f  overload_functio
233e0 6e 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73  n(sqlite3*, cons
233f0 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d  t char *zFuncNam
23400 65 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f  e, int nArg);../
23410 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61  *.** The interfa
23420 63 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61  ce to the virtua
23430 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73  l-table mechanis
23440 6d 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20  m defined above 
23450 28 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61  (back up.** to a
23460 20 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61   comment remarka
23470 62 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74  bly similar to t
23480 68 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72  his one) is curr
23490 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64  ently considered
234a0 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69  .** to be experi
234b0 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74  mental.  The int
234c0 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61  erface might cha
234d0 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69  nge in incompati
234e0 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20  ble ways..** If 
234f0 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65  this is a proble
23500 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f  m for you, do no
23510 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66  t use the interf
23520 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65  ace at this time
23530 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
23540 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
23550 65 63 68 61 6e 69 73 6d 20 73 74 61 62 6c 69 7a  echanism stabliz
23560 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c  es, we will decl
23570 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72  are the.** inter
23580 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70  face fixed, supp
23590 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74  ort it indefinit
235a0 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20  ely, and remove 
235b0 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a  this comment..**
235c0 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45  .****** EXPERIME
235d0 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74  NTAL - subject t
235e0 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74  o change without
235f0 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a   notice ********
23600 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ******.*/../*.**
23610 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e   CAPI3REF: A Han
23620 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42  dle To An Open B
23630 4c 4f 42 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73  LOB.**.** An ins
23640 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
23650 6c 6f 77 69 6e 67 20 6f 70 61 71 75 65 20 73 74  lowing opaque st
23660 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20  ructure is used 
23670 74 6f 20 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74  to .** represent
23680 20 61 6e 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 2e   an blob-handle.
23690 20 20 41 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20    A blob-handle 
236a0 69 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a  is created by.**
236b0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
236c0 70 65 6e 28 29 5d 20 61 6e 64 20 64 65 73 74 72  pen()] and destr
236d0 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  oyed by [sqlite3
236e0 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a  _blob_close()]..
236f0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
23700 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e 64  blob_read()] and
23710 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77   [sqlite3_blob_w
23720 72 69 74 65 28 29 5d 20 69 6e 74 65 72 66 61 63  rite()] interfac
23730 65 73 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  es.** can be use
23740 64 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  d to read or wri
23750 74 65 20 73 6d 61 6c 6c 20 73 75 62 73 65 63 74  te small subsect
23760 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 6c 6f 62  ions of the blob
23770 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  ..** The [sqlite
23780 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20  3_blob_bytes()] 
23790 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
237a0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
237b0 65 0a 2a 2a 20 62 6c 6f 62 20 69 6e 20 62 79 74  e.** blob in byt
237c0 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  es..*/.typedef s
237d0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 6c  truct sqlite3_bl
237e0 6f 62 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 3b  ob sqlite3_blob;
237f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
23800 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46 6f  : Open A BLOB Fo
23810 72 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f  r Incremental I/
23820 4f 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 68  O.**.** Open a h
23830 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 62 6c 6f  andle to the blo
23840 62 20 6c 6f 63 61 74 65 64 20 69 6e 20 72 6f 77  b located in row
23850 20 69 52 6f 77 2c 2c 20 63 6f 6c 75 6d 6e 20 7a   iRow,, column z
23860 43 6f 6c 75 6d 6e 2c 20 0a 2a 2a 20 74 61 62 6c  Column, .** tabl
23870 65 20 7a 54 61 62 6c 65 20 69 6e 20 64 61 74 61  e zTable in data
23880 62 61 73 65 20 7a 44 62 2e 20 69 2e 65 2e 20 74  base zDb. i.e. t
23890 68 65 20 73 61 6d 65 20 62 6c 6f 62 20 74 68 61  he same blob tha
238a0 74 20 77 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65  t would.** be se
238b0 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a  lected by:.**.**
238c0 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45   <pre>.**     SE
238d0 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f  LECT zColumn FRO
238e0 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45  M zDb.zTable WHE
238f0 52 45 20 72 6f 77 69 64 20 3d 20 69 52 6f 77 3b  RE rowid = iRow;
23900 0a 2a 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a  .** </pre>.**.**
23910 20 49 66 20 74 68 65 20 66 6c 61 67 73 20 70 61   If the flags pa
23920 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a  rameter is non-z
23930 65 72 6f 2c 20 74 68 65 20 62 6c 6f 62 20 69 73  ero, the blob is
23940 20 6f 70 65 6e 65 64 20 66 6f 72 20 0a 2a 2a 20   opened for .** 
23950 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 61  read and write a
23960 63 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20  ccess. If it is 
23970 7a 65 72 6f 2c 20 74 68 65 20 62 6c 6f 62 20 69  zero, the blob i
23980 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61  s opened for rea
23990 64 20 0a 2a 2a 20 61 63 63 65 73 73 2e 0a 2a 2a  d .** access..**
239a0 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20  .** On success, 
239b0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72  [SQLITE_OK] is r
239c0 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20  eturned and the 
239d0 6e 65 77 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  new .** [sqlite3
239e0 5f 62 6c 6f 62 20 7c 20 62 6c 6f 62 20 68 61 6e  _blob | blob han
239f0 64 6c 65 5d 20 69 73 20 77 72 69 74 74 65 6e 20  dle] is written 
23a00 74 6f 20 2a 70 70 42 6c 6f 62 2e 0a 2a 2a 20 4f  to *ppBlob..** O
23a10 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f  therwise an erro
23a20 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
23a30 65 64 20 61 6e 64 20 0a 2a 2a 20 61 6e 79 20 76  ed and .** any v
23a40 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74 6f 20  alue written to 
23a50 2a 70 70 42 6c 6f 62 20 73 68 6f 75 6c 64 20 6e  *ppBlob should n
23a60 6f 74 20 62 65 20 75 73 65 64 20 62 79 20 74 68  ot be used by th
23a70 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 54 68 69  e caller..** Thi
23a80 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20  s function sets 
23a90 74 68 65 20 64 61 74 61 62 61 73 65 2d 68 61 6e  the database-han
23aa0 64 6c 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  dle error code a
23ab0 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20 61 63  nd message.** ac
23ac0 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73 71  cessible via [sq
23ad0 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d  lite3_errcode()]
23ae0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72   and [sqlite3_er
23af0 72 6d 73 67 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49  rmsg()]..*/.SQLI
23b00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
23b10 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20  e3_blob_open(.  
23b20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73  sqlite3*,.  cons
23b30 74 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63  t char *zDb,.  c
23b40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c  onst char *zTabl
23b50 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e,.  const char 
23b60 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69  *zColumn,.  sqli
23b70 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a  te3_int64 iRow,.
23b80 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73    int flags,.  s
23b90 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70  qlite3_blob **pp
23ba0 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  Blob.);../*.** C
23bb0 41 50 49 33 52 45 46 3a 20 20 43 6c 6f 73 65 20  API3REF:  Close 
23bc0 41 20 42 4c 4f 42 20 48 61 6e 64 6c 65 0a 2a 2a  A BLOB Handle.**
23bd0 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 65  .** Close an ope
23be0 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  n [sqlite3_blob 
23bf0 7c 20 62 6c 6f 62 20 68 61 6e 64 6c 65 5d 2e 0a  | blob handle]..
23c00 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
23c10 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63  t sqlite3_blob_c
23c20 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f  lose(sqlite3_blo
23c30 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  b *);../*.** CAP
23c40 49 33 52 45 46 3a 20 20 52 65 74 75 72 6e 20 54  I3REF:  Return T
23c50 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70  he Size Of An Op
23c60 65 6e 20 42 4c 4f 42 0a 2a 2a 0a 2a 2a 20 52 65  en BLOB.**.** Re
23c70 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e  turn the size in
23c80 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 62 6c   bytes of the bl
23c90 6f 62 20 61 63 63 65 73 73 69 62 6c 65 20 76 69  ob accessible vi
23ca0 61 20 74 68 65 20 6f 70 65 6e 20 0a 2a 2a 20 5b  a the open .** [
23cb0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20 62  sqlite3_blob | b
23cc0 6c 6f 62 2d 68 61 6e 64 6c 65 5d 20 70 61 73 73  lob-handle] pass
23cd0 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
23ce0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
23cf0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
23d00 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f  b_bytes(sqlite3_
23d10 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  blob *);../*.** 
23d20 43 41 50 49 33 52 45 46 3a 20 20 52 65 61 64 20  CAPI3REF:  Read 
23d30 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f 42  Data From A BLOB
23d40 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 0a 2a   Incrementally.*
23d50 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
23d60 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  on is used to re
23d70 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 6e 20  ad data from an 
23d80 6f 70 65 6e 20 0a 2a 2a 20 5b 73 71 6c 69 74 65  open .** [sqlite
23d90 33 5f 62 6c 6f 62 20 7c 20 62 6c 6f 62 2d 68 61  3_blob | blob-ha
23da0 6e 64 6c 65 5d 20 69 6e 74 6f 20 61 20 63 61 6c  ndle] into a cal
23db0 6c 65 72 20 73 75 70 70 6c 69 65 64 20 62 75 66  ler supplied buf
23dc0 66 65 72 2e 0a 2a 2a 20 6e 20 62 79 74 65 73 20  fer..** n bytes 
23dd0 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69  of data are copi
23de0 65 64 20 69 6e 74 6f 20 62 75 66 66 65 72 0a 2a  ed into buffer.*
23df0 2a 20 7a 20 66 72 6f 6d 20 74 68 65 20 6f 70 65  * z from the ope
23e00 6e 20 62 6c 6f 62 2c 20 73 74 61 72 74 69 6e 67  n blob, starting
23e10 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73   at offset iOffs
23e20 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  et..**.** On suc
23e30 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  cess, SQLITE_OK 
23e40 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  is returned. Oth
23e50 65 72 77 69 73 65 2c 20 61 6e 20 0a 2a 2a 20 5b  erwise, an .** [
23e60 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 7c 20 53  SQLITE_ERROR | S
23e70 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
23e80 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49  ] or an.** [SQLI
23e90 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c 20  TE_IOERR_READ | 
23ea0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
23eb0 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
23ec0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
23ed0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
23ee0 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c  _read(sqlite3_bl
23ef0 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 7a 2c 20 69  ob *, void *z, i
23f00 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65  nt n, int iOffse
23f10 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
23f20 52 45 46 3a 20 20 57 72 69 74 65 20 44 61 74 61  REF:  Write Data
23f30 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20 49 6e 63   Into A BLOB Inc
23f40 72 65 6d 65 6e 74 61 6c 6c 79 0a 2a 2a 0a 2a 2a  rementally.**.**
23f50 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
23f60 73 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20  s used to write 
23f70 64 61 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 65  data into an ope
23f80 6e 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  n .** [sqlite3_b
23f90 6c 6f 62 20 7c 20 62 6c 6f 62 2d 68 61 6e 64 6c  lob | blob-handl
23fa0 65 5d 20 66 72 6f 6d 20 61 20 75 73 65 72 20 73  e] from a user s
23fb0 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e 0a  upplied buffer..
23fc0 2a 2a 20 6e 20 62 79 74 65 73 20 6f 66 20 64 61  ** n bytes of da
23fd0 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72  ta are copied fr
23fe0 6f 6d 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a  om the buffer.**
23ff0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a   pointed to by z
24000 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e 20 62   into the open b
24010 6c 6f 62 2c 20 73 74 61 72 74 69 6e 67 20 61 74  lob, starting at
24020 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e   offset iOffset.
24030 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 73  .**.** If the [s
24040 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20 62 6c  qlite3_blob | bl
24050 6f 62 2d 68 61 6e 64 6c 65 5d 20 70 61 73 73 65  ob-handle] passe
24060 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
24070 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 6e  rgument.** was n
24080 6f 74 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  ot opened for wr
24090 69 74 69 6e 67 20 28 74 68 65 20 66 6c 61 67 73  iting (the flags
240a0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
240b0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
240c0 28 29 5d 0a 2a 2a 2a 20 77 61 73 20 7a 65 72 6f  ()].*** was zero
240d0 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ), this function
240e0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
240f0 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a  _READONLY]..**.*
24100 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
24110 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66 79 20  may only modify 
24120 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
24130 74 68 65 20 62 6c 6f 62 2c 20 69 74 20 69 73 0a  the blob, it is.
24140 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  ** not possible 
24150 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  to increase the 
24160 73 69 7a 65 20 6f 66 20 61 20 62 6c 6f 62 20 75  size of a blob u
24170 73 69 6e 67 20 74 68 69 73 20 41 50 49 2e 20 49  sing this API. I
24180 66 0a 2a 2a 20 6f 66 66 73 65 74 20 69 4f 66 66  f.** offset iOff
24190 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e  set is less than
241a0 20 6e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68   n bytes from th
241b0 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 6c 6f  e end of the blo
241c0 62 2c 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45  b, .** [SQLITE_E
241d0 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65  RROR] is returne
241e0 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73  d and no data is
241f0 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20   written..**.** 
24200 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49  On success, SQLI
24210 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
24220 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e  d. Otherwise, an
24230 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52   .** [SQLITE_ERR
24240 4f 52 20 7c 20 53 51 4c 69 74 65 20 65 72 72 6f  OR | SQLite erro
24250 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 0a 2a 2a  r code] or an.**
24260 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52   [SQLITE_IOERR_R
24270 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64 20 65  EAD | extended e
24280 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
24290 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
242a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
242b0 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c  3_blob_write(sql
242c0 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 63 6f 6e  ite3_blob *, con
242d0 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20  st void *z, int 
242e0 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 3b  n, int iOffset);
242f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
24300 3a 20 20 56 69 72 74 75 61 6c 20 46 69 6c 65 20  :  Virtual File 
24310 53 79 73 74 65 6d 20 4f 62 6a 65 63 74 73 0a 2a  System Objects.*
24320 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c 20 66  *.** A virtual f
24330 69 6c 65 73 79 73 74 65 6d 20 28 56 46 53 29 20  ilesystem (VFS) 
24340 69 73 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76  is an [sqlite3_v
24350 66 73 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68  fs] object.** th
24360 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20 74  at SQLite uses t
24370 6f 20 69 6e 74 65 72 61 63 74 0a 2a 2a 20 77 69  o interact.** wi
24380 74 68 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  th the underlyin
24390 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  g operating syst
243a0 65 6d 2e 20 20 4d 6f 73 74 20 62 75 69 6c 64 73  em.  Most builds
243b0 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20   come with a.** 
243c0 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56  single default V
243d0 46 53 20 74 68 61 74 20 69 73 20 61 70 70 72 6f  FS that is appro
243e0 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 68  priate for the h
243f0 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a  ost computer..**
24400 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e 20 62   New VFSes can b
24410 65 20 72 65 67 69 73 74 65 72 65 64 20 61 6e 64  e registered and
24420 20 65 78 69 73 74 69 6e 67 20 56 46 53 65 73 20   existing VFSes 
24430 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65  can be unregiste
24440 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  red..** The foll
24450 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73  owing interfaces
24460 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a   are provided..*
24470 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
24480 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 65  _vfs_find() inte
24490 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
244a0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 46 53  pointer to a VFS
244b0 20 67 69 76 65 6e 20 69 74 73 0a 2a 2a 20 6e 61   given its.** na
244c0 6d 65 2e 20 20 4e 61 6d 65 73 20 61 72 65 20 63  me.  Names are c
244d0 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 20 20  ase sensitive.  
244e0 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  If there is no m
244f0 61 74 63 68 2c 20 61 20 4e 55 4c 4c 0a 2a 2a 20  atch, a NULL.** 
24500 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72  pointer is retur
24510 6e 65 64 2e 20 20 49 66 20 7a 56 66 73 4e 61 6d  ned.  If zVfsNam
24520 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74  e is NULL then t
24530 68 65 20 64 65 66 61 75 6c 74 20 0a 2a 2a 20 56  he default .** V
24540 46 53 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  FS is returned..
24550 2a 2a 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20  **.** New VFSes 
24560 61 72 65 20 72 65 67 69 73 74 65 72 65 64 20 77  are registered w
24570 69 74 68 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ith sqlite3_vfs_
24580 72 65 67 69 73 74 65 72 28 29 2e 20 20 45 61 63  register().  Eac
24590 68 0a 2a 2a 20 6e 65 77 20 56 46 53 20 62 65 63  h.** new VFS bec
245a0 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75 6c 74  omes the default
245b0 20 56 46 53 20 69 66 20 74 68 65 20 6d 61 6b 65   VFS if the make
245c0 44 66 6c 74 20 66 6c 61 67 20 69 73 20 73 65 74  Dflt flag is set
245d0 2e 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 56 46  ..** The same VF
245e0 53 20 63 61 6e 20 62 65 20 72 65 67 69 73 74 65  S can be registe
245f0 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  red multiple tim
24600 65 73 20 77 69 74 68 6f 75 74 20 69 6e 6a 75 72  es without injur
24610 79 2e 0a 2a 2a 20 54 6f 20 6d 61 6b 65 20 61 6e  y..** To make an
24620 20 65 78 69 73 74 69 6e 67 20 56 46 53 20 69 6e   existing VFS in
24630 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 56  to the default V
24640 46 53 2c 20 72 65 67 69 73 74 65 72 20 69 74 20  FS, register it 
24650 61 67 61 69 6e 0a 2a 2a 20 77 69 74 68 20 74 68  again.** with th
24660 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20  e makeDflt flag 
24670 73 65 74 2e 20 20 49 66 20 74 77 6f 20 64 69 66  set.  If two dif
24680 66 65 72 65 6e 74 20 56 46 53 65 73 20 77 69 74  ferent VFSes wit
24690 68 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6e 61  h the.** same na
246a0 6d 65 20 61 72 65 20 72 65 67 69 73 74 65 72 65  me are registere
246b0 64 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  d, the behavior 
246c0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49  is undefined.  I
246d0 66 20 61 0a 2a 2a 20 56 46 53 20 69 73 20 72 65  f a.** VFS is re
246e0 67 69 73 74 65 72 65 64 20 77 69 74 68 20 61 20  gistered with a 
246f0 6e 61 6d 65 20 74 68 61 74 20 69 73 20 4e 55 4c  name that is NUL
24700 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74  L or an empty st
24710 72 69 6e 67 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ring,.** then th
24720 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e  e behavior is un
24730 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 0a 2a 2a 20  defined..** .** 
24740 55 6e 72 65 67 69 73 74 65 72 20 61 20 56 46 53  Unregister a VFS
24750 20 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 65   with the sqlite
24760 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
24770 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  () interface..**
24780 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
24790 56 46 53 20 69 73 20 75 6e 72 65 67 69 73 74 65  VFS is unregiste
247a0 72 65 64 2c 20 61 6e 6f 74 68 65 72 20 56 46 53  red, another VFS
247b0 20 69 73 20 63 68 6f 73 65 6e 20 61 73 0a 2a 2a   is chosen as.**
247c0 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54   the default.  T
247d0 68 65 20 63 68 6f 69 63 65 20 66 6f 72 20 74 68  he choice for th
247e0 65 20 6e 65 77 20 56 46 53 20 69 73 20 61 72 62  e new VFS is arb
247f0 69 74 72 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  itrary..*/.SQLIT
24800 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66  E_API sqlite3_vf
24810 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66  s *sqlite3_vfs_f
24820 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ind(const char *
24830 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51 4c 49 54  zVfsName);.SQLIT
24840 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
24850 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 73  3_vfs_register(s
24860 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74  qlite3_vfs*, int
24870 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53 51 4c 49   makeDflt);.SQLI
24880 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
24890 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
248a0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b  r(sqlite3_vfs*);
248b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
248c0 3a 20 4d 75 74 65 78 65 73 0a 2a 2a 0a 2a 2a 20  : Mutexes.**.** 
248d0 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20  The SQLite core 
248e0 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74 69  uses these routi
248f0 6e 65 73 20 66 6f 72 20 74 68 72 65 61 64 0a 2a  nes for thread.*
24900 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f  * synchronizatio
24910 6e 2e 20 20 54 68 6f 75 67 68 20 74 68 65 79 20  n.  Though they 
24920 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72  are intended for
24930 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65   internal.** use
24940 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f 64 65   by SQLite, code
24950 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69   that links agai
24960 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a  nst SQLite is.**
24970 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 75 73   permitted to us
24980 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72  e any of these r
24990 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  outines..**.** T
249a0 68 65 20 53 51 4c 69 74 65 20 73 6f 75 72 63 65  he SQLite source
249b0 20 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73 20 6d   code contains m
249c0 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e  ultiple implemen
249d0 74 61 74 69 6f 6e 73 20 0a 2a 2a 20 6f 66 20 74  tations .** of t
249e0 68 65 73 65 20 6d 75 74 65 78 20 72 6f 75 74 69  hese mutex routi
249f0 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 6f 70 72  nes.  An appropr
24a00 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  iate implementat
24a10 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63 74  ion.** is select
24a20 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
24a30 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
24a40 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
24a50 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
24a60 6f 6e 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ons are availabl
24a70 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20  e in the SQLite 
24a80 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  core:.**.** <ul>
24a90 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54  .** <li>   SQLIT
24aa0 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20 3c  E_MUTEX_OS2.** <
24ab0 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  li>   SQLITE_MUT
24ac0 45 58 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c 6c  EX_PTHREAD.** <l
24ad0 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  i>   SQLITE_MUTE
24ae0 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20  X_W32.** <li>   
24af0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
24b00 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  P.** </ul>.**.**
24b10 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45   The SQLITE_MUTE
24b20 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e 74  X_NOOP implement
24b30 61 74 69 6f 6e 20 69 73 20 61 20 73 65 74 20 6f  ation is a set o
24b40 66 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 74  f routines .** t
24b50 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c  hat does no real
24b60 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 69 73 20   locking and is 
24b70 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
24b80 75 73 65 20 69 6e 20 0a 2a 2a 20 61 20 73 69 6e  use in .** a sin
24b90 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 70  gle-threaded app
24ba0 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 53  lication.  The S
24bb0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 2c  QLITE_MUTEX_OS2,
24bc0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
24bd0 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20 53 51  _PTHREAD, and SQ
24be0 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 69  LITE_MUTEX_W32 i
24bf0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a  mplementations.*
24c00 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69 61 74  * are appropriat
24c10 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 6f 73 2f  e for use on os/
24c20 32 2c 20 75 6e 69 78 2c 20 61 6e 64 20 77 69 6e  2, unix, and win
24c30 64 6f 77 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20  dows..** .** If 
24c40 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
24c50 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
24c60 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 20  TE_MUTEX_APPDEF 
24c70 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20  preprocessor.** 
24c80 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28 77  macro defined (w
24c90 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55  ith "-DSQLITE_MU
24ca0 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c 20  TEX_APPDEF=1"), 
24cb0 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a  then no mutex.**
24cc0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
24cd0 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74 68  is included with
24ce0 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 54   the library.  T
24cf0 68 65 0a 2a 2a 20 6d 75 74 65 78 20 69 6e 74 65  he.** mutex inte
24d00 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 64  rface routines d
24d10 65 66 69 6e 65 64 20 68 65 72 65 20 62 65 63 6f  efined here beco
24d20 6d 65 20 65 78 74 65 72 6e 61 6c 0a 2a 2a 20 72  me external.** r
24d30 65 66 65 72 65 6e 63 65 73 20 69 6e 20 74 68 65  eferences in the
24d40 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
24d50 66 6f 72 20 77 68 69 63 68 20 69 6d 70 6c 65 6d  for which implem
24d60 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73  entations.** mus
24d70 74 20 62 65 20 70 72 6f 76 69 64 65 64 20 62 79  t be provided by
24d80 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
24d90 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69 74 79  .  This facility
24da0 20 61 6c 6c 6f 77 73 20 61 6e 0a 2a 2a 20 61 70   allows an.** ap
24db0 70 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 6c  plication that l
24dc0 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 53 51 4c  inks against SQL
24dd0 69 74 65 20 74 6f 20 70 72 6f 76 69 64 65 20 69  ite to provide i
24de0 74 73 20 6f 77 6e 20 6d 75 74 65 78 0a 2a 2a 20  ts own mutex.** 
24df0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
24e00 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
24e10 20 6d 6f 64 69 66 79 20 74 68 65 20 53 51 4c 69   modify the SQLi
24e20 74 65 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  te core..**.** T
24e30 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
24e40 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65  _alloc() routine
24e50 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77   allocates a new
24e60 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65  .** mutex and re
24e70 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
24e80 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65  to it.  If it re
24e90 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  turns NULL.** th
24ea0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  at means that a 
24eb0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20  mutex could not 
24ec0 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53  be allocated.  S
24ed0 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e  QLite.** will un
24ee0 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61  wind its stack a
24ef0 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  nd return an err
24f00 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e  or.  The argumen
24f10 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  t.** to sqlite3_
24f20 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73  mutex_alloc() is
24f30 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e   one of these in
24f40 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a  teger constants:
24f50 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
24f60 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
24f70 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20  X_FAST.** <li>  
24f80 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
24f90 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20  URSIVE.** <li>  
24fa0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
24fb0 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c  TIC_MASTER.** <l
24fc0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
24fd0 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c  _STATIC_MEM.** <
24fe0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
24ff0 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a  X_STATIC_MEM2.**
25000 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
25010 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a  TEX_STATIC_PRNG.
25020 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
25030 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
25040 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
25050 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f  The first two co
25060 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71  nstants cause sq
25070 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
25080 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a  c() to create.**
25090 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54   a new mutex.  T
250a0 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20  he new mutex is 
250b0 72 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53  recursive when S
250c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
250d0 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64  RSIVE.** is used
250e0 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61   but not necessa
250f0 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c  rily so when SQL
25100 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
25110 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d  s used..** The m
25120 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
25130 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  ion does not nee
25140 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74  d to make a dist
25150 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65  inction.** betwe
25160 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  en SQLITE_MUTEX_
25170 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51  RECURSIVE and SQ
25180 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
25190 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f  if it does.** no
251a0 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20  t want to.  But 
251b0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79  SQLite will only
251c0 20 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72   request a recur
251d0 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a  sive mutex in.**
251e0 20 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20   cases where it 
251f0 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65  really needs one
25200 2e 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e  .  If a faster n
25210 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74  on-recursive mut
25220 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ex.** implementa
25230 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c  tion is availabl
25240 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c  e on the host pl
25250 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65  atform, the mute
25260 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d  x subsystem.** m
25270 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68  ight return such
25280 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70   a mutex in resp
25290 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d  onse to SQLITE_M
252a0 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a  UTEX_FAST..**.**
252b0 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77   The other allow
252c0 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  ed parameters to
252d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
252e0 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75  lloc() each retu
252f0 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
25300 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65  to a static pree
25310 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20  xisting mutex.  
25320 46 6f 75 72 20 73 74 61 74 69 63 20 6d 75 74 65  Four static mute
25330 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20  xes are.** used 
25340 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  by the current v
25350 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  ersion of SQLite
25360 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f  .  Future versio
25370 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20  ns of SQLite.** 
25380 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e  may add addition
25390 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  al static mutexe
253a0 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78  s.  Static mutex
253b0 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72  es are for inter
253c0 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51  nal.** use by SQ
253d0 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c  Lite only.  Appl
253e0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73  ications that us
253f0 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73  e SQLite mutexes
25400 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f   should.** use o
25410 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20  nly the dynamic 
25420 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64  mutexes returned
25430 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   by SQLITE_MUTEX
25440 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49  _FAST or.** SQLI
25450 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
25460 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  VE..**.** Note t
25470 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68  hat if one of th
25480 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20  e dynamic mutex 
25490 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49  parameters (SQLI
254a0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a  TE_MUTEX_FAST.**
254b0 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   or SQLITE_MUTEX
254c0 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75  _RECURSIVE) is u
254d0 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33  sed then sqlite3
254e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a  _mutex_alloc().*
254f0 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66  * returns a diff
25500 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65  erent mutex on e
25510 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20  very call.  But 
25520 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 20 0a  for the static .
25530 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20  ** mutex types, 
25540 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69  the same mutex i
25550 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76  s returned on ev
25560 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61  ery call that ha
25570 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79  s.** the same ty
25580 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a  pe number..**.**
25590 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
255a0 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74 69 6e  ex_free() routin
255b0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
255c0 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c  previously.** al
255d0 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69 63 20  located dynamic 
255e0 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69  mutex.  SQLite i
255f0 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61  s careful to dea
25600 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a  llocate every.**
25610 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74   dynamic mutex t
25620 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73  hat it allocates
25630 2e 20 20 54 68 65 20 64 79 6e 61 6d 69 63 20 6d  .  The dynamic m
25640 75 74 65 78 65 73 20 6d 75 73 74 20 6e 6f 74 20  utexes must not 
25650 62 65 20 69 6e 20 0a 2a 2a 20 75 73 65 20 77 68  be in .** use wh
25660 65 6e 20 74 68 65 79 20 61 72 65 20 64 65 61 6c  en they are deal
25670 6c 6f 63 61 74 65 64 2e 20 20 41 74 74 65 6d 70  located.  Attemp
25680 74 69 6e 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61  ting to dealloca
25690 74 65 20 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d  te a static.** m
256a0 75 74 65 78 20 72 65 73 75 6c 74 73 20 69 6e 20  utex results in 
256b0 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69  undefined behavi
256c0 6f 72 2e 20 20 53 51 4c 69 74 65 20 6e 65 76 65  or.  SQLite neve
256d0 72 20 64 65 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a  r deallocates.**
256e0 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 2e   a static mutex.
256f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
25700 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
25710 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
25720 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
25730 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20  s attempt.** to 
25740 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20  enter a mutex.  
25750 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  If another threa
25760 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74  d is already wit
25770 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a  hin the mutex,.*
25780 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
25790 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f  enter() will blo
257a0 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  ck and sqlite3_m
257b0 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20  utex_try() will 
257c0 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
257d0 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69  _BUSY.  The sqli
257e0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
257f0 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
25800 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75  s SQLITE_OK.** u
25810 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65  pon successful e
25820 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63  ntry.  Mutexes c
25830 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c  reated using SQL
25840 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
25850 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e  IVE can.** be en
25860 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74  tered multiple t
25870 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65  imes by the same
25880 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63   thread.  In suc
25890 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20  h cases the,.** 
258a0 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78  mutex must be ex
258b0 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75  ited an equal nu
258c0 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65  mber of times be
258d0 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72  fore another thr
258e0 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72  ead.** can enter
258f0 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74  .  If the same t
25900 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65  hread tries to e
25910 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b  nter any other k
25920 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20  ind of mutex.** 
25930 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
25940 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
25950 75 6e 64 65 66 69 6e 65 64 2e 20 20 20 53 51 4c  undefined.   SQL
25960 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 65  ite will never e
25970 78 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 20 62  xhibit.** such b
25980 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20 6f  ehavior in its o
25990 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78 65  wn use of mutexe
259a0 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79  s..**.** Some sy
259b0 73 74 65 6d 73 20 28 65 78 3a 20 77 69 6e 64 6f  stems (ex: windo
259c0 77 73 39 35 29 20 64 6f 20 6e 6f 74 20 74 68 65  ws95) do not the
259d0 20 6f 70 65 72 61 74 69 6f 6e 20 69 6d 70 6c 65   operation imple
259e0 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c  mented by.** sql
259f0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
25a00 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79 73 74  .  On those syst
25a10 65 6d 73 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74  ems, sqlite3_mut
25a20 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 0a 2a 2a  ex_try() will.**
25a30 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 53   always return S
25a40 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65  QLITE_BUSY.  The
25a50 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c   SQLite core onl
25a60 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73  y ever uses.** s
25a70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
25a80 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a  () as an optimiz
25a90 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73  ation so this is
25aa0 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61   acceptable beha
25ab0 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  vior..**.** The 
25ac0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
25ad0 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78  ave() routine ex
25ae0 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74  its a mutex that
25af0 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
25b00 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68  ly entered by th
25b10 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
25b20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  The behavior.** 
25b30 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20  is undefined if 
25b40 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  the mutex is not
25b50 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
25b60 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c  ed by the.** cal
25b70 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 20 69  ling thread or i
25b80 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
25b90 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69  allocated.  SQLi
25ba0 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72  te will.** never
25bb0 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2a 0a 2a   do either..**.*
25bc0 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
25bd0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
25be0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
25bf0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d  mutex_notheld()]
25c00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
25c10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73  sqlite3_mutex *s
25c20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
25c30 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  oc(int);.SQLITE_
25c40 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
25c50 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69  _mutex_free(sqli
25c60 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c  te3_mutex*);.SQL
25c70 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
25c80 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
25c90 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
25ca0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
25cb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
25cc0 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ry(sqlite3_mutex
25cd0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
25ce0 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
25cf0 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f  x_leave(sqlite3_
25d00 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  mutex*);../*.** 
25d10 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20  CAPI3REF: Mutex 
25d20 56 65 72 69 66 63 61 74 69 6f 6e 20 52 6f 75 74  Verifcation Rout
25d30 69 6e 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ines.**.** The s
25d40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
25d50 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
25d60 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
25d70 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20  routines.** are 
25d80 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
25d90 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
25da0 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68   statements.  Th
25db0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a 2a  e SQLite core.**
25dc0 20 6e 65 76 65 72 20 75 73 65 73 20 74 68 65 73   never uses thes
25dd0 65 20 72 6f 75 74 69 6e 65 73 20 65 78 63 65 70  e routines excep
25de0 74 20 69 6e 73 69 64 65 20 61 6e 20 61 73 73 65  t inside an asse
25df0 72 74 28 29 20 61 6e 64 20 61 70 70 6c 69 63 61  rt() and applica
25e00 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 64 76  tions.** are adv
25e10 69 73 65 64 20 74 6f 20 66 6f 6c 6c 6f 77 20 74  ised to follow t
25e20 68 65 20 6c 65 61 64 20 6f 66 20 74 68 65 20 63  he lead of the c
25e30 6f 72 65 2e 20 20 54 68 65 20 63 6f 72 65 20 6f  ore.  The core o
25e40 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20  nly.** provides 
25e50 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
25e60 66 6f 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e  for these routin
25e70 65 73 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f  es when it is co
25e80 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74  mpiled.** with t
25e90 68 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  he SQLITE_DEBUG 
25ea0 66 6c 61 67 2e 20 20 45 78 74 65 72 6e 61 6c 20  flag.  External 
25eb0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
25ec0 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c  tions.** are onl
25ed0 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 72  y required to pr
25ee0 6f 76 69 64 65 20 74 68 65 73 65 20 72 6f 75 74  ovide these rout
25ef0 69 6e 65 73 20 69 66 20 53 51 4c 49 54 45 5f 44  ines if SQLITE_D
25f00 45 42 55 47 20 69 73 0a 2a 2a 20 64 65 66 69 6e  EBUG is.** defin
25f10 65 64 20 61 6e 64 20 69 66 20 4e 44 45 42 55 47  ed and if NDEBUG
25f20 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e   is not defined.
25f30 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
25f40 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 74  tines should ret
25f50 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
25f60 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 61  mutex in their a
25f70 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 65  rgument.** is he
25f80 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c 20  ld or not held, 
25f90 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 79  respectively, by
25fa0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72   the calling thr
25fb0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ead..**.** The i
25fc0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
25fd0 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f   not required to
25fe0 20 70 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f   provided versio
25ff0 6e 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72  ns of these.** r
26000 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 63 74  outines that act
26010 75 61 6c 6c 79 20 77 6f 72 6b 2e 0a 2a 2a 20 49  ually work..** I
26020 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  f the implementa
26030 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 70 72  tion does not pr
26040 6f 76 69 64 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a  ovide working.**
26050 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
26060 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 69 74 20  se routines, it 
26070 73 68 6f 75 6c 64 20 61 74 20 6c 65 61 73 74 20  should at least 
26080 70 72 6f 76 69 64 65 20 73 74 75 62 73 0a 2a 2a  provide stubs.**
26090 20 74 68 61 74 20 61 6c 77 61 79 73 20 72 65 74   that always ret
260a0 75 72 6e 20 74 72 75 65 20 73 6f 20 74 68 61 74  urn true so that
260b0 20 6f 6e 65 20 64 6f 65 73 20 6e 6f 74 20 67 65   one does not ge
260c0 74 20 73 70 75 72 69 6f 75 73 0a 2a 2a 20 61 73  t spurious.** as
260d0 73 65 72 74 69 6f 6e 20 66 61 69 6c 75 72 65 73  sertion failures
260e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61  ..**.** If the a
260f0 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
26100 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
26110 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
26120 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 6f  r then.** the ro
26130 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74  utine should ret
26140 75 72 6e 20 31 2e 20 20 54 68 69 73 20 73 65 65  urn 1.  This see
26150 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e 74 75 69  ms counter-intui
26160 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a 20 63 6c  tive since.** cl
26170 65 61 72 6c 79 20 74 68 65 20 6d 75 74 65 78 20  early the mutex 
26180 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c 64 20 69  cannot be held i
26190 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78  f it does not ex
261a0 69 73 74 2e 20 20 42 75 74 20 74 68 65 0a 2a 2a  ist.  But the.**
261b0 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 65 20   the reason the 
261c0 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f 74 20 65  mutex does not e
261d0 78 69 73 74 20 69 73 20 62 65 63 61 75 73 65 20  xist is because 
261e0 74 68 65 20 62 75 69 6c 64 20 69 73 20 6e 6f 74  the build is not
261f0 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74 65 78 65  .** using mutexe
26200 73 2e 20 20 41 6e 64 20 77 65 20 64 6f 20 6e 6f  s.  And we do no
26210 74 20 77 61 6e 74 20 74 68 65 20 61 73 73 65 72  t want the asser
26220 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  t() containing t
26230 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71  he.** call to sq
26240 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
26250 28 29 20 74 6f 20 66 61 69 6c 2c 20 73 6f 20 61  () to fail, so a
26260 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e   non-zero return
26270 20 69 73 0a 2a 2a 20 74 68 65 20 61 70 70 72 6f   is.** the appro
26280 70 72 69 61 74 65 20 74 68 69 6e 67 20 74 6f 20  priate thing to 
26290 64 6f 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  do.  The sqlite3
262a0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
262b0 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73   .** interface s
262c0 68 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72  hould also retur
262d0 6e 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61  n 1 when given a
262e0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
262f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
26300 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
26310 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
26320 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  x*);.SQLITE_API 
26330 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  int sqlite3_mute
26340 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65  x_notheld(sqlite
26350 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a  3_mutex*);../*.*
26360 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
26370 78 20 54 79 70 65 73 0a 2a 2a 0a 2a 2a 20 54 68  x Types.**.** Th
26380 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  e [sqlite3_mutex
26390 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72 66  _alloc()] interf
263a0 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ace takes a sing
263b0 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77  le argument.** w
263c0 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20 74  hich is one of t
263d0 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
263e0 73 74 61 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 69  stants..*/.#defi
263f0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
26400 46 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20  FAST            
26410 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   0.#define SQLIT
26420 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
26430 45 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69  E        1.#defi
26440 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
26450 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 20 20  STATIC_MASTER   
26460 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
26470 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
26480 45 4d 20 20 20 20 20 20 20 33 20 20 2f 2a 20 73  EM       3  /* s
26490 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
264a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
264b0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
264c0 45 4d 32 20 20 20 20 20 20 34 20 20 2f 2a 20 73  EM2      4  /* s
264d0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
264e0 65 6d 6f 72 79 28 29 20 2a 2f 0a 23 64 65 66 69  emory() */.#defi
264f0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
26500 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20  STATIC_PRNG     
26510 20 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72   5  /* sqlite3_r
26520 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66 69  andom() */.#defi
26530 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
26540 53 54 41 54 49 43 5f 4c 52 55 20 20 20 20 20 20  STATIC_LRU      
26550 20 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20   6  /* lru page 
26560 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  list */../*.** C
26570 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76  API3REF: Low-Lev
26580 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61  el Control Of Da
26590 74 61 62 61 73 65 20 46 69 6c 65 73 0a 2a 2a 0a  tabase Files.**.
265a0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
265b0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20  file_control()] 
265c0 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20  interface makes 
265d0 61 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f  a direct call to
265e0 20 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e   the.** xFileCon
265f0 74 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20  trol method for 
26600 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
26610 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
26620 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
26630 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  th a particular 
26640 64 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 66  database identif
26650 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e  ied by the secon
26660 64 20 61 72 67 75 6d 65 6e 74 2e 20 20 54 68 65  d argument.  The
26670 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  .** name of the 
26680 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20  database is the 
26690 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f  name assigned to
266a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 79   the database by
266b0 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d   the.** <a href=
266c0 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d  "lang_attach.htm
266d0 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e 20 53 51  l">ATTACH</a> SQ
266e0 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f  L command that o
266f0 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20 64 61 74  pened the.** dat
26700 61 62 61 73 65 2e 20 20 54 6f 20 63 6f 6e 74 72  abase.  To contr
26710 6f 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ol the main data
26720 62 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74  base file, use t
26730 68 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a  he name "main".*
26740 2a 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  * or a NULL poin
26750 74 65 72 2e 20 20 54 68 65 20 74 68 69 72 64 20  ter.  The third 
26760 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d  and fourth param
26770 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 72 6f  eters to this ro
26780 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73  utine.** are pas
26790 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 68 72  sed directly thr
267a0 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65 63 6f  ough to the seco
267b0 6e 64 20 61 6e 64 20 74 68 69 72 64 20 70 61 72  nd and third par
267c0 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68  ameters of.** th
267d0 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d  e xFileControl m
267e0 65 74 68 6f 64 2e 20 20 54 68 65 20 72 65 74 75  ethod.  The retu
267f0 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  rn value of the 
26800 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20  xFileControl.** 
26810 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74  method becomes t
26820 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
26830 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  of this routine.
26840 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65  .**.** If the se
26850 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28  cond parameter (
26860 7a 44 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e 6f  zDbName) does no
26870 74 20 6d 61 74 63 68 20 74 68 65 20 6e 61 6d 65  t match the name
26880 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e 20   of any.** open 
26890 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
268a0 68 65 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  hen SQLITE_ERROR
268b0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
268c0 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64  his error.** cod
268d0 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62 65  e is not remembe
268e0 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74  red and will not
268f0 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79 20   be recalled by 
26900 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65  [sqlite3_errcode
26910 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74  ()].** or [sqlit
26920 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 20 54  e3_errmsg()].  T
26930 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 78 46  he underlying xF
26940 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f  ileControl metho
26950 64 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20  d might.** also 
26960 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
26970 52 4f 52 2e 20 20 54 68 65 72 65 20 69 73 20 6e  ROR.  There is n
26980 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69 6e 67  o way to disting
26990 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a 2a 20  uish between.** 
269a0 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a 44 62  an incorrect zDb
269b0 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c 49  Name and an SQLI
269c0 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72 6e 20  TE_ERROR return 
269d0 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79  from the underly
269e0 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74  ing.** xFileCont
269f0 72 6f 6c 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a  rol method..**.*
26a00 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c  * See also: [SQL
26a10 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54  ITE_FCNTL_LOCKST
26a20 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ATE].*/.SQLITE_A
26a30 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
26a40 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69  ile_control(sqli
26a50 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  te3*, const char
26a60 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f   *zDbName, int o
26a70 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a  p, void*);../*.*
26a80 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20  * Undo the hack 
26a90 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c  that converts fl
26aa0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70  oating point typ
26ab0 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f  es to integer fo
26ac0 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70  r.** builds on p
26ad0 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75  rocessors withou
26ae0 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  t floating point
26af0 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66   support..*/.#if
26b00 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
26b10 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23  FLOATING_POINT.#
26b20 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65   undef double.#e
26b30 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f  ndif..#if 0.}  /
26b40 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78  * End of the 'ex
26b50 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20  tern "C"' block 
26b60 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  */.#endif.#endif
26b70 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
26b80 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33  * End of sqlite3
26b90 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
26ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bc0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
26bd0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74  * Begin file dat
26be0 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
26bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c10 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74  /./*.** 2003 Oct
26c20 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68  ober 31.**.** Th
26c30 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
26c40 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
26c50 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
26c60 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
26c70 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
26c80 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
26c90 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
26ca0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
26cb0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
26cc0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
26cd0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
26ce0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
26cf0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
26d00 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
26d10 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
26d20 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
26d30 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
26d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26d80 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
26d90 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
26da0 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61   C functions tha
26db0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65  t implement date
26dc0 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e   and time.** fun
26dd0 63 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74  ctions for SQLit
26de0 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  e.  .**.** There
26df0 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70   is only one exp
26e00 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20  orted symbol in 
26e10 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20  this file - the 
26e20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69  function.** sqli
26e30 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54  te3RegisterDateT
26e40 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66  imeFunctions() f
26e50 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74  ound at the bott
26e60 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a  om of the file..
26e70 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64  ** All other cod
26e80 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65  e has file scope
26e90 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61 74  ..**.** $Id: dat
26ea0 65 2e 63 2c 76 20 31 2e 37 33 20 32 30 30 37 2f  e.c,v 1.73 2007/
26eb0 30 39 2f 31 32 20 31 37 3a 30 31 3a 34 35 20 64  09/12 17:01:45 d
26ec0 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
26ed0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72  .**.** SQLite pr
26ee0 6f 63 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65  ocesses all time
26ef0 73 20 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a  s and dates as J
26f00 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72  ulian Day number
26f10 73 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73  s.  The.** dates
26f20 20 61 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73   and times are s
26f30 74 6f 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d  tored as the num
26f40 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63  ber of days sinc
26f50 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65  e noon.** in Gre
26f60 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62  enwich on Novemb
26f70 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e  er 24, 4714 B.C.
26f80 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
26f90 65 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63  e Gregorian.** c
26fa0 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20  alendar system. 
26fb0 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30  .**.** 1970-01-0
26fc0 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44  1 00:00:00 is JD
26fd0 20 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30   2440587.5.** 20
26fe0 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30  00-01-01 00:00:0
26ff0 30 20 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e  0 is JD 2451544.
27000 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  5.**.** This imp
27010 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72  lemention requir
27020 65 73 20 79 65 61 72 73 20 74 6f 20 62 65 20 65  es years to be e
27030 78 70 72 65 73 73 65 64 20 61 73 20 61 20 34 2d  xpressed as a 4-
27040 64 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20  digit number.** 
27050 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74  which means that
27060 20 6f 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77   only dates betw
27070 65 65 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61  een 0000-01-01 a
27080 6e 64 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61  nd 9999-12-31 ca
27090 6e 0a 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e  n.** be represen
270a0 74 65 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68  ted, even though
270b0 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   julian day numb
270c0 65 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68  ers allow a much
270d0 20 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20   wider.** range 
270e0 6f 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  of dates..**.** 
270f0 54 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61  The Gregorian ca
27100 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73  lendar system is
27110 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61   used for all da
27120 74 65 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a  tes and times,.*
27130 2a 20 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61  * even those tha
27140 74 20 70 72 65 64 61 74 65 20 74 68 65 20 47 72  t predate the Gr
27150 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72  egorian calendar
27160 2e 20 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73  .  Historians us
27170 75 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65  ually.** use the
27180 20 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72   Julian calendar
27190 20 66 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72   for dates prior
271a0 20 74 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61   to 1582-10-15 a
271b0 6e 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64  nd for some.** d
271c0 61 74 65 73 20 61 66 74 65 72 77 61 72 64 73 2c  ates afterwards,
271d0 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f   depending on lo
271e0 63 61 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66  cale.  Beware of
271f0 20 74 68 69 73 20 64 69 66 66 65 72 65 6e 63 65   this difference
27200 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76  ..**.** The conv
27210 65 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d  ersion algorithm
27220 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65  s are implemente
27230 64 20 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72  d based on descr
27240 69 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68  iptions.** in th
27250 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74  e following text
27260 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61  :.**.**      Jea
27270 6e 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20  n Meeus.**      
27280 41 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67  Astronomical Alg
27290 6f 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69  orithms, 2nd Edi
272a0 74 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20  tion, 1998.**   
272b0 20 20 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36     ISBM 0-943396
272c0 2d 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69  -61-1.**      Wi
272d0 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63  llmann-Bell, Inc
272e0 0a 2a 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e  .**      Richmon
272f0 64 2c 20 56 69 72 67 69 6e 69 61 20 28 55 53 41  d, Virginia (USA
27300 29 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ).*/./**********
27310 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c  **** Include sql
27320 69 74 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20  iteInt.h in the 
27330 6d 69 64 64 6c 65 20 6f 66 20 64 61 74 65 2e 63  middle of date.c
27340 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
27350 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
27360 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
27370 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
27380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273a0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
273b0 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
273c0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
273d0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
273e0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
273f0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
27400 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
27410 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
27420 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
27430 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
27440 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
27450 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
27460 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
27470 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
27480 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
27490 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
274a0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
274b0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
274c0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
274d0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
274e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
274f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 49  ***********.** I
27520 6e 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63  nternal interfac
27530 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 66 6f  e definitions fo
27540 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  r SQLite..**.** 
27550 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65  @(#) $Id: sqlite
27560 49 6e 74 2e 68 2c 76 20 31 2e 36 30 38 20 32 30  Int.h,v 1.608 20
27570 30 37 2f 30 39 2f 30 33 20 31 35 3a 31 39 3a 33  07/09/03 15:19:3
27580 35 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  5 drh Exp $.*/.#
27590 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 49 4e  ifndef _SQLITEIN
275a0 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51  T_H_.#define _SQ
275b0 4c 49 54 45 49 4e 54 5f 48 5f 0a 2f 2a 2a 2a 2a  LITEINT_H_./****
275c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
275d0 64 65 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68  de sqliteLimit.h
275e0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
275f0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
27600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
27610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
27620 20 66 69 6c 65 20 73 71 6c 69 74 65 4c 69 6d 69   file sqliteLimi
27630 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
27640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
27660 20 32 30 30 37 20 4d 61 79 20 37 0a 2a 2a 0a 2a   2007 May 7.**.*
27670 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
27680 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
27690 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
276a0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
276b0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
276c0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
276d0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
276e0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
276f0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
27700 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
27710 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
27720 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
27730 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
27740 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
27750 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
27760 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
27770 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
27780 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
27790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a  **********.** .*
277d0 2a 20 54 68 69 73 20 66 69 6c 65 20 64 65 66 69  * This file defi
277e0 6e 65 73 20 76 61 72 69 6f 75 73 20 6c 69 6d 69  nes various limi
277f0 74 73 20 6f 66 20 77 68 61 74 20 53 51 4c 69 74  ts of what SQLit
27800 65 20 63 61 6e 20 70 72 6f 63 65 73 73 2e 0a 2a  e can process..*
27810 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73  *.** @(#) $Id: s
27820 71 6c 69 74 65 4c 69 6d 69 74 2e 68 2c 76 20 31  qliteLimit.h,v 1
27830 2e 32 20 32 30 30 37 2f 30 38 2f 32 34 20 31 31  .2 2007/08/24 11
27840 3a 35 32 3a 32 39 20 64 61 6e 69 65 6c 6b 31 39  :52:29 danielk19
27850 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  77 Exp $.*/../*.
27860 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c  ** The maximum l
27870 65 6e 67 74 68 20 6f 66 20 61 20 54 45 58 54 20  ength of a TEXT 
27880 6f 72 20 42 4c 4f 42 20 69 6e 20 62 79 74 65 73  or BLOB in bytes
27890 2e 20 20 20 54 68 69 73 20 61 6c 73 6f 0a 2a 2a  .   This also.**
278a0 20 6c 69 6d 69 74 73 20 74 68 65 20 73 69 7a 65   limits the size
278b0 20 6f 66 20 61 20 72 6f 77 20 69 6e 20 61 20 74   of a row in a t
278c0 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a  able or index..*
278d0 2a 0a 2a 2a 20 54 68 65 20 68 61 72 64 20 6c 69  *.** The hard li
278e0 6d 69 74 20 69 73 20 74 68 65 20 61 62 69 6c 69  mit is the abili
278f0 74 79 20 6f 66 20 61 20 33 32 2d 62 69 74 20 73  ty of a 32-bit s
27900 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
27910 20 74 6f 20 63 6f 75 6e 74 20 74 68 65 20 73 69   to count the si
27920 7a 65 3a 20 32 5e 33 31 2d 31 20 6f 72 20 32 31  ze: 2^31-1 or 21
27930 34 37 34 38 33 36 34 37 2e 0a 2a 2f 0a 23 69 66  47483647..*/.#if
27940 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
27950 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20  LENGTH.# define 
27960 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
27970 48 20 31 30 30 30 30 30 30 30 30 30 0a 23 65 6e  H 1000000000.#en
27980 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
27990 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  is the maximum n
279a0 75 6d 62 65 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20  umber of.**.**  
279b0 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61    * Columns in a
279c0 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 2a 20 43   table.**    * C
279d0 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64  olumns in an ind
279e0 65 78 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d  ex.**    * Colum
279f0 6e 73 20 69 6e 20 61 20 76 69 65 77 0a 2a 2a 20  ns in a view.** 
27a00 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68     * Terms in th
27a10 65 20 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20  e SET clause of 
27a20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
27a30 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d  ent.**    * Term
27a40 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
27a50 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20  set of a SELECT 
27a60 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20  statement.**    
27a70 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 47  * Terms in the G
27a80 52 4f 55 50 20 42 59 20 6f 72 20 4f 52 44 45 52  ROUP BY or ORDER
27a90 20 42 59 20 63 6c 61 75 73 65 73 20 6f 66 20 61   BY clauses of a
27aa0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
27ab0 74 2e 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73  t..**    * Terms
27ac0 20 69 6e 20 74 68 65 20 56 41 4c 55 45 53 20 63   in the VALUES c
27ad0 6c 61 75 73 65 20 6f 66 20 61 6e 20 49 4e 53 45  lause of an INSE
27ae0 52 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a  RT statement.**.
27af0 2a 2a 20 54 68 65 20 68 61 72 64 20 75 70 70 65  ** The hard uppe
27b00 72 20 6c 69 6d 69 74 20 68 65 72 65 20 69 73 20  r limit here is 
27b10 33 32 36 37 36 2e 20 20 4d 6f 73 74 20 64 61 74  32676.  Most dat
27b20 61 62 61 73 65 20 70 65 6f 70 6c 65 20 77 69 6c  abase people wil
27b30 6c 0a 2a 2a 20 74 65 6c 6c 20 79 6f 75 20 74 68  l.** tell you th
27b40 61 74 20 69 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72  at in a well-nor
27b50 6d 61 6c 69 7a 65 64 20 64 61 74 61 62 61 73 65  malized database
27b60 2c 20 79 6f 75 20 75 73 75 61 6c 6c 79 20 73 68  , you usually sh
27b70 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 68 61 76 65  ould.** not have
27b80 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f 7a   more than a doz
27b90 65 6e 20 6f 72 20 73 6f 20 63 6f 6c 75 6d 6e 73  en or so columns
27ba0 20 69 6e 20 61 6e 79 20 74 61 62 6c 65 2e 20 20   in any table.  
27bb0 41 6e 64 20 69 66 0a 2a 2a 20 74 68 61 74 20 69  And if.** that i
27bc0 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 72  s the case, ther
27bd0 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e  e is no point in
27be0 20 68 61 76 69 6e 67 20 6d 6f 72 65 20 74 68 61   having more tha
27bf0 6e 20 61 20 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e  n a few.** dozen
27c00 20 76 61 6c 75 65 73 20 69 6e 20 61 6e 79 20 6f   values in any o
27c10 66 20 74 68 65 20 6f 74 68 65 72 20 73 69 74 75  f the other situ
27c20 61 74 69 6f 6e 73 20 64 65 73 63 72 69 62 65 64  ations described
27c30 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64   above..*/.#ifnd
27c40 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  ef SQLITE_MAX_CO
27c50 4c 55 4d 4e 0a 23 20 64 65 66 69 6e 65 20 53 51  LUMN.# define SQ
27c60 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20  LITE_MAX_COLUMN 
27c70 32 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  2000.#endif../*.
27c80 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c  ** The maximum l
27c90 65 6e 67 74 68 20 6f 66 20 61 20 73 69 6e 67 6c  ength of a singl
27ca0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
27cb0 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 20 54 68 65  in bytes..** The
27cc0 20 68 61 72 64 20 6c 69 6d 69 74 20 68 65 72 65   hard limit here
27cd0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
27ce0 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
27cf0 48 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  H..*/.#ifndef SQ
27d00 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e  LITE_MAX_SQL_LEN
27d10 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
27d20 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
27d30 54 48 20 31 30 30 30 30 30 30 0a 23 65 6e 64 69  TH 1000000.#endi
27d40 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  f../*.** The max
27d50 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e  imum depth of an
27d60 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
27d70 2e 20 54 68 69 73 20 69 73 20 6c 69 6d 69 74 65  . This is limite
27d80 64 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65 20 65 78  d to .** some ex
27d90 74 65 6e 74 20 62 79 20 53 51 4c 49 54 45 5f 4d  tent by SQLITE_M
27da0 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2e 20 42  AX_SQL_LENGTH. B
27db0 75 74 20 73 6f 6d 65 74 69 6d 65 20 79 6f 75 20  ut sometime you 
27dc0 6d 69 67 68 74 20 0a 2a 2a 20 77 61 6e 74 20 74  might .** want t
27dd0 6f 20 70 6c 61 63 65 20 6d 6f 72 65 20 73 65 76  o place more sev
27de0 65 72 65 20 6c 69 6d 69 74 73 20 6f 6e 20 74 68  ere limits on th
27df0 65 20 63 6f 6d 70 6c 65 78 69 74 79 20 6f 66 20  e complexity of 
27e00 61 6e 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  an .** expressio
27e10 6e 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20  n. A value of 0 
27e20 28 74 68 65 20 64 65 66 61 75 6c 74 29 20 6d 65  (the default) me
27e30 61 6e 73 20 64 6f 20 6e 6f 74 20 65 6e 66 6f 72  ans do not enfor
27e40 63 65 0a 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 61  ce.** any limita
27e50 74 69 6f 6e 20 6f 6e 20 65 78 70 72 65 73 73 69  tion on expressi
27e60 6f 6e 20 74 72 65 65 20 64 65 70 74 68 2e 0a 2a  on tree depth..*
27e70 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
27e80 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 0a  _MAX_EXPR_DEPTH.
27e90 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
27ea0 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 20 31  MAX_EXPR_DEPTH 1
27eb0 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
27ec0 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  * The maximum nu
27ed0 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e  mber of terms in
27ee0 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
27ef0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
27f00 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   The code genera
27f10 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64  tor for compound
27f20 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
27f30 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c  ts does one.** l
27f40 65 76 65 6c 20 6f 66 20 72 65 63 75 72 73 69 6f  evel of recursio
27f50 6e 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e  n for each term.
27f60 20 20 41 20 73 74 61 63 6b 20 6f 76 65 72 66 6c    A stack overfl
27f70 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a 2a  ow can result.**
27f80 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   if the number o
27f90 66 20 74 65 72 6d 73 20 69 73 20 74 6f 6f 20 6c  f terms is too l
27fa0 61 72 67 65 2e 20 20 49 6e 20 70 72 61 63 74 69  arge.  In practi
27fb0 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20  ce, most SQL.** 
27fc0 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 65 20 74  never has more t
27fd0 68 61 6e 20 33 20 6f 72 20 34 20 74 65 72 6d 73  han 3 or 4 terms
27fe0 2e 20 20 55 73 65 20 61 20 76 61 6c 75 65 20 6f  .  Use a value o
27ff0 66 20 30 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a  f 0 to disable.*
28000 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20 74  * any limit on t
28010 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72  he number of ter
28020 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 74  ms in a compount
28030 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e   SELECT..*/.#ifn
28040 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  def SQLITE_MAX_C
28050 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 23  OMPOUND_SELECT.#
28060 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
28070 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  AX_COMPOUND_SELE
28080 43 54 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f  CT 500.#endif../
28090 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
280a0 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64   number of opcod
280b0 65 73 20 69 6e 20 61 20 56 44 42 45 20 70 72 6f  es in a VDBE pro
280c0 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72  gram..** Not cur
280d0 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64 2e  rently enforced.
280e0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
280f0 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a 23  TE_MAX_VDBE_OP.#
28100 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
28110 41 58 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 30  AX_VDBE_OP 25000
28120 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
28130 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
28140 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
28150 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f  o an SQL functio
28160 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  n..*/.#ifndef SQ
28170 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f  LITE_MAX_FUNCTIO
28180 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e 65 20 53  N_ARG.# define S
28190 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49  QLITE_MAX_FUNCTI
281a0 4f 4e 5f 41 52 47 20 31 30 30 0a 23 65 6e 64 69  ON_ARG 100.#endi
281b0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  f../*.** The max
281c0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69  imum number of i
281d0 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74  n-memory pages t
281e0 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 6d 61  o use for the ma
281f0 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74  in database.** t
28200 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 74 65 6d  able and for tem
28210 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20  porary tables.  
28220 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  The SQLITE_DEFAU
28230 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a 2f  LT_CACHE_SIZE.*/
28240 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28250 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
28260 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ZE.# define SQLI
28270 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  TE_DEFAULT_CACHE
28280 5f 53 49 5a 45 20 20 32 30 30 30 0a 23 65 6e 64  _SIZE  2000.#end
28290 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
282a0 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43  E_DEFAULT_TEMP_C
282b0 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 69  ACHE_SIZE.# defi
282c0 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
282d0 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a  T_TEMP_CACHE_SIZ
282e0 45 20 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f  E  500.#endif../
282f0 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
28300 20 6e 75 6d 62 65 72 20 6f 66 20 61 74 74 61 63   number of attac
28310 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 20 20  hed databases.  
28320 54 68 69 73 20 6d 75 73 74 20 62 65 20 61 74 20  This must be at 
28330 6c 65 61 73 74 20 32 0a 2a 2a 20 69 6e 20 6f 72  least 2.** in or
28340 64 65 72 20 74 6f 20 73 75 70 70 6f 72 74 20 74  der to support t
28350 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
28360 20 66 69 6c 65 20 28 30 29 20 61 6e 64 20 74 68   file (0) and th
28370 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f 0a 2a  e file used to.*
28380 2a 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79  * hold temporary
28390 20 74 61 62 6c 65 73 20 28 31 29 2e 20 20 41 6e   tables (1).  An
283a0 64 20 69 74 20 6d 75 73 74 20 62 65 20 6c 65 73  d it must be les
283b0 73 20 74 68 61 6e 20 33 32 20 62 65 63 61 75 73  s than 32 becaus
283c0 65 0a 2a 2a 20 77 65 20 75 73 65 20 61 20 62 69  e.** we use a bi
283d0 74 6d 61 73 6b 20 6f 66 20 64 61 74 61 62 61 73  tmask of databas
283e0 65 73 20 77 69 74 68 20 61 20 75 33 32 20 69 6e  es with a u32 in
283f0 20 70 6c 61 63 65 73 20 28 66 6f 72 20 65 78 61   places (for exa
28400 6d 70 6c 65 0a 2a 2a 20 74 68 65 20 50 61 72 73  mple.** the Pars
28410 65 2e 63 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65  e.cookieMask fie
28420 6c 64 29 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ld)..*/.#ifndef 
28430 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
28440 48 45 44 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  HED.# define SQL
28450 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44  ITE_MAX_ATTACHED
28460 20 31 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a   10.#endif.../*.
28470 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 76  ** The maximum v
28480 61 6c 75 65 20 6f 66 20 61 20 3f 6e 6e 6e 20 77  alue of a ?nnn w
28490 69 6c 64 63 61 72 64 20 74 68 61 74 20 74 68 65  ildcard that the
284a0 20 70 61 72 73 65 72 20 77 69 6c 6c 20 61 63 63   parser will acc
284b0 65 70 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ept..*/.#ifndef 
284c0 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
284d0 42 4c 45 5f 4e 55 4d 42 45 52 0a 23 20 64 65 66  BLE_NUMBER.# def
284e0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  ine SQLITE_MAX_V
284f0 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 39  ARIABLE_NUMBER 9
28500 39 39 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  99.#endif../*.**
28510 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69 7a   The default siz
28520 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  e of a database 
28530 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  page..*/.#ifndef
28540 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
28550 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69  PAGE_SIZE.# defi
28560 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
28570 54 5f 50 41 47 45 5f 53 49 5a 45 20 31 30 32 34  T_PAGE_SIZE 1024
28580 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f  .#endif../*.** O
28590 72 64 69 6e 61 72 69 6c 79 2c 20 69 66 20 6e 6f  rdinarily, if no
285a0 20 76 61 6c 75 65 20 69 73 20 65 78 70 6c 69 63   value is explic
285b0 69 74 6c 79 20 70 72 6f 76 69 64 65 64 2c 20 53  itly provided, S
285c0 51 4c 69 74 65 20 63 72 65 61 74 65 73 20 64 61  QLite creates da
285d0 74 61 62 61 73 65 73 0a 2a 2a 20 77 69 74 68 20  tabases.** with 
285e0 70 61 67 65 20 73 69 7a 65 20 53 51 4c 49 54 45  page size SQLITE
285f0 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
28600 5a 45 2e 20 48 6f 77 65 76 65 72 2c 20 62 61 73  ZE. However, bas
28610 65 64 20 6f 6e 20 63 65 72 74 61 69 6e 0a 2a 2a  ed on certain.**
28620 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65   device characte
28630 72 69 73 74 69 63 73 20 28 73 65 63 74 6f 72 2d  ristics (sector-
28640 73 69 7a 65 20 61 6e 64 20 61 74 6f 6d 69 63 20  size and atomic 
28650 77 72 69 74 65 28 29 20 73 75 70 70 6f 72 74 29  write() support)
28660 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 79 20  ,.** SQLite may 
28670 63 68 6f 6f 73 65 20 61 20 6c 61 72 67 65 72 20  choose a larger 
28680 76 61 6c 75 65 2e 20 54 68 69 73 20 63 6f 6e 73  value. This cons
28690 74 61 6e 74 20 69 73 20 74 68 65 20 6d 61 78 69  tant is the maxi
286a0 6d 75 6d 20 76 61 6c 75 65 0a 2a 2a 20 53 51 4c  mum value.** SQL
286b0 69 74 65 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20  ite will choose 
286c0 6f 6e 20 69 74 27 73 20 6f 77 6e 2e 0a 2a 2f 0a  on it's own..*/.
286d0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
286e0 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
286f0 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
28700 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
28710 5f 50 41 47 45 5f 53 49 5a 45 20 38 31 39 32 0a  _PAGE_SIZE 8192.
28720 23 65 6e 64 69 66 0a 0a 2f 2a 20 4d 61 78 69 6d  #endif../* Maxim
28730 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 20 20 54  um page size.  T
28740 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f  he upper bound o
28750 6e 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20  n this value is 
28760 33 32 37 36 38 2e 20 20 54 68 69 73 20 61 20 6c  32768.  This a l
28770 69 6d 69 74 0a 2a 2a 20 69 6d 70 6f 73 65 64 20  imit.** imposed 
28780 62 79 20 74 68 65 20 6e 65 63 65 73 73 69 74 79  by the necessity
28790 20 6f 66 20 73 74 6f 72 69 6e 67 20 74 68 65 20   of storing the 
287a0 76 61 6c 75 65 20 69 6e 20 61 20 32 2d 62 79 74  value in a 2-byt
287b0 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  e unsigned integ
287c0 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 61  er.** and the fa
287d0 63 74 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ct that the page
287e0 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20   size must be a 
287f0 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2f 0a 23  power of 2..*/.#
28800 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41  ifndef SQLITE_MA
28810 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65  X_PAGE_SIZE.# de
28820 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
28830 50 41 47 45 5f 53 49 5a 45 20 33 32 37 36 38 0a  PAGE_SIZE 32768.
28840 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
28850 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
28860 70 61 67 65 73 20 69 6e 20 6f 6e 65 20 64 61 74  pages in one dat
28870 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
28880 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c 79  * This is really
28890 20 6a 75 73 74 20 74 68 65 20 64 65 66 61 75 6c   just the defaul
288a0 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
288b0 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 70  max_page_count p
288c0 72 61 67 6d 61 2e 0a 2a 2a 20 54 68 69 73 20 76  ragma..** This v
288d0 61 6c 75 65 20 63 61 6e 20 62 65 20 6c 6f 77 65  alue can be lowe
288e0 72 65 64 20 28 6f 72 20 72 61 69 73 65 64 29 20  red (or raised) 
288f0 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69 6e  at run-time usin
28900 67 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 6d 61  g that the.** ma
28910 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 6d 61 63  x_page_count mac
28920 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ro..*/.#ifndef S
28930 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43  QLITE_MAX_PAGE_C
28940 4f 55 4e 54 0a 23 20 64 65 66 69 6e 65 20 53 51  OUNT.# define SQ
28950 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f  LITE_MAX_PAGE_CO
28960 55 4e 54 20 31 30 37 33 37 34 31 38 32 33 0a 23  UNT 1073741823.#
28970 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78  endif../*.** Max
28980 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69 6e 20  imum length (in 
28990 62 79 74 65 73 29 20 6f 66 20 74 68 65 20 70 61  bytes) of the pa
289a0 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b 45 20  ttern in a LIKE 
289b0 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65 72 61  or GLOB.** opera
289c0 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  tor..*/.#ifndef 
289d0 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f  SQLITE_MAX_LIKE_
289e0 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0a 23  PATTERN_LENGTH.#
289f0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
28a00 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
28a10 4c 45 4e 47 54 48 20 35 30 30 30 30 0a 23 65 6e  LENGTH 50000.#en
28a20 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
28a30 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69  **** End of sqli
28a40 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a  teLimit.h ******
28a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28a70 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
28a80 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
28a90 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
28aa0 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
28ab0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
28ac0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  ***/../*.** For 
28ad0 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73  testing purposes
28ae0 2c 20 74 68 65 20 76 61 72 69 6f 75 73 20 73 69  , the various si
28af0 7a 65 20 6c 69 6d 69 74 20 63 6f 6e 73 74 61 6e  ze limit constan
28b00 74 73 20 61 72 65 20 72 65 61 6c 6c 79 0a 2a 2a  ts are really.**
28b10 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 74 20   variables that 
28b20 77 65 20 63 61 6e 20 6d 6f 64 69 66 79 20 69 6e  we can modify in
28b30 20 74 68 65 20 74 65 73 74 66 69 78 74 75 72 65   the testfixture
28b40 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
28b50 54 45 5f 54 45 53 54 0a 20 20 23 75 6e 64 65 66  TE_TEST.  #undef
28b60 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
28b70 54 48 0a 20 20 23 75 6e 64 65 66 20 53 51 4c 49  TH.  #undef SQLI
28b80 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20  TE_MAX_COLUMN.  
28b90 23 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41  #undef SQLITE_MA
28ba0 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 0a 20 20 23  X_SQL_LENGTH.  #
28bb0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  undef SQLITE_MAX
28bc0 5f 45 58 50 52 5f 44 45 50 54 48 0a 20 20 23 75  _EXPR_DEPTH.  #u
28bd0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
28be0 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
28bf0 20 20 23 75 6e 64 65 66 20 53 51 4c 49 54 45 5f    #undef SQLITE_
28c00 4d 41 58 5f 56 44 42 45 5f 4f 50 0a 20 20 23 75  MAX_VDBE_OP.  #u
28c10 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
28c20 46 55 4e 43 54 49 4f 4e 5f 41 52 47 0a 20 20 23  FUNCTION_ARG.  #
28c30 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  undef SQLITE_MAX
28c40 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
28c50 0a 20 20 23 75 6e 64 65 66 20 53 51 4c 49 54 45  .  #undef SQLITE
28c60 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20  _MAX_PAGE_SIZE. 
28c70 20 23 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d   #undef SQLITE_M
28c80 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 20 20  AX_PAGE_COUNT.  
28c90 23 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41  #undef SQLITE_MA
28ca0 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
28cb0 45 4e 47 54 48 0a 0a 20 20 23 64 65 66 69 6e 65  ENGTH..  #define
28cc0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
28cd0 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TH              
28ce0 73 71 6c 69 74 65 33 4d 41 58 5f 4c 45 4e 47 54  sqlite3MAX_LENGT
28cf0 48 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49  H.  #define SQLI
28d00 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 20 20  TE_MAX_COLUMN   
28d10 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28d20 65 33 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 23  e3MAX_COLUMN.  #
28d30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
28d40 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 20 20 20  X_SQL_LENGTH    
28d50 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 41 58        sqlite3MAX
28d60 5f 53 51 4c 5f 4c 45 4e 47 54 48 0a 20 20 23 64  _SQL_LENGTH.  #d
28d70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
28d80 5f 45 58 50 52 5f 44 45 50 54 48 20 20 20 20 20  _EXPR_DEPTH     
28d90 20 20 20 20 20 73 71 6c 69 74 65 33 4d 41 58 5f       sqlite3MAX_
28da0 45 58 50 52 5f 44 45 50 54 48 0a 20 20 23 64 65  EXPR_DEPTH.  #de
28db0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
28dc0 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20  COMPOUND_SELECT 
28dd0 20 20 20 20 73 71 6c 69 74 65 33 4d 41 58 5f 43      sqlite3MAX_C
28de0 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20  OMPOUND_SELECT. 
28df0 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
28e00 4d 41 58 5f 56 44 42 45 5f 4f 50 20 20 20 20 20  MAX_VDBE_OP     
28e10 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
28e20 41 58 5f 56 44 42 45 5f 4f 50 0a 20 20 23 64 65  AX_VDBE_OP.  #de
28e30 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
28e40 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20  FUNCTION_ARG    
28e50 20 20 20 20 73 71 6c 69 74 65 33 4d 41 58 5f 46      sqlite3MAX_F
28e60 55 4e 43 54 49 4f 4e 5f 41 52 47 0a 20 20 23 64  UNCTION_ARG.  #d
28e70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
28e80 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
28e90 20 20 20 20 20 73 71 6c 69 74 65 33 4d 41 58 5f       sqlite3MAX_
28ea0 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a  VARIABLE_NUMBER.
28eb0 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45    #define SQLITE
28ec0 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 20  _MAX_PAGE_SIZE  
28ed0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28ee0 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20  MAX_PAGE_SIZE.  
28ef0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
28f00 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 20 20 20  AX_PAGE_COUNT   
28f10 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 41         sqlite3MA
28f20 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 20 20 23  X_PAGE_COUNT.  #
28f30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
28f40 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
28f50 45 4e 47 54 48 20 73 71 6c 69 74 65 33 4d 41 58  ENGTH sqlite3MAX
28f60 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
28f70 4e 47 54 48 0a 0a 20 20 65 78 74 65 72 6e 20 69  NGTH..  extern i
28f80 6e 74 20 73 71 6c 69 74 65 33 4d 41 58 5f 4c 45  nt sqlite3MAX_LE
28f90 4e 47 54 48 3b 0a 20 20 65 78 74 65 72 6e 20 69  NGTH;.  extern i
28fa0 6e 74 20 73 71 6c 69 74 65 33 4d 41 58 5f 43 4f  nt sqlite3MAX_CO
28fb0 4c 55 4d 4e 3b 0a 20 20 65 78 74 65 72 6e 20 69  LUMN;.  extern i
28fc0 6e 74 20 73 71 6c 69 74 65 33 4d 41 58 5f 53 51  nt sqlite3MAX_SQ
28fd0 4c 5f 4c 45 4e 47 54 48 3b 0a 20 20 65 78 74 65  L_LENGTH;.  exte
28fe0 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 41  rn int sqlite3MA
28ff0 58 5f 45 58 50 52 5f 44 45 50 54 48 3b 0a 20 20  X_EXPR_DEPTH;.  
29000 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
29010 65 33 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53  e3MAX_COMPOUND_S
29020 45 4c 45 43 54 3b 0a 20 20 65 78 74 65 72 6e 20  ELECT;.  extern 
29030 69 6e 74 20 73 71 6c 69 74 65 33 4d 41 58 5f 56  int sqlite3MAX_V
29040 44 42 45 5f 4f 50 3b 0a 20 20 65 78 74 65 72 6e  DBE_OP;.  extern
29050 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 41 58 5f   int sqlite3MAX_
29060 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3b 0a 20 20  FUNCTION_ARG;.  
29070 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
29080 65 33 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e  e3MAX_VARIABLE_N
29090 55 4d 42 45 52 3b 0a 20 20 65 78 74 65 72 6e 20  UMBER;.  extern 
290a0 69 6e 74 20 73 71 6c 69 74 65 33 4d 41 58 5f 50  int sqlite3MAX_P
290b0 41 47 45 5f 53 49 5a 45 3b 0a 20 20 65 78 74 65  AGE_SIZE;.  exte
290c0 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 41  rn int sqlite3MA
290d0 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20  X_PAGE_COUNT;.  
290e0 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
290f0 65 33 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45  e3MAX_LIKE_PATTE
29100 52 4e 5f 4c 45 4e 47 54 48 3b 0a 23 65 6e 64 69  RN_LENGTH;.#endi
29110 66 0a 0a 23 64 65 66 69 6e 65 20 5f 58 4f 50 45  f..#define _XOPE
29120 4e 5f 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a  N_SOURCE 500  /*
29130 20 4e 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c   Needed to enabl
29140 65 20 70 74 68 72 65 61 64 20 72 65 63 75 72 73  e pthread recurs
29150 69 76 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 0a  ive mutexes */..
29160 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
29170 54 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e  TE_TCL) || defin
29180 65 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c  ed(TCLSH).# incl
29190 75 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64  ude <tcl.h>.#end
291a0 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70  if../*.** Many p
291b0 65 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e  eople are failin
291c0 67 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55  g to set -DNDEBU
291d0 47 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69  G=1 when compili
291e0 6e 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65  ng SQLite..** Se
291f0 74 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b  tting NDEBUG mak
29200 65 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c  es the code smal
29210 6c 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74  ler and run fast
29220 65 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c  er.  So the foll
29230 6f 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61  owing.** lines a
29240 72 65 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f  re added to auto
29250 6d 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44  matically set ND
29260 45 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20  EBUG unless the 
29270 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31  -DSQLITE_DEBUG=1
29280 0a 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65  .** option is se
29290 74 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 20  t.  Thus NDEBUG 
292a0 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69  becomes an opt-i
292b0 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e  n rather than an
292c0 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74   opt-out.** feat
292d0 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ure..*/.#if !def
292e0 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20  ined(NDEBUG) && 
292f0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
29300 44 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65  DEBUG) .# define
29310 20 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66   NDEBUG 1.#endif
29320 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
29330 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 6d 61  TE_THREADSAFE ma
29340 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69  cro must be defi
29350 6e 65 64 20 61 73 20 65 69 74 68 65 72 20 30 20  ned as either 0 
29360 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76  or 1..** Older v
29370 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
29380 65 20 75 73 65 64 20 61 6e 20 6f 70 74 69 6f 6e  e used an option
29390 61 6c 20 54 48 52 45 41 44 53 41 46 45 20 6d 61  al THREADSAFE ma
293a0 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75 70 70 6f  cro..** We suppo
293b0 72 74 20 74 68 61 74 20 66 6f 72 20 6c 65 67 61  rt that for lega
293c0 63 79 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  cy.*/.#if !defin
293d0 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ed(SQLITE_THREAD
293e0 53 41 46 45 29 0a 23 69 66 20 64 65 66 69 6e 65  SAFE).#if define
293f0 64 28 54 48 52 45 41 44 53 41 46 45 29 0a 23 20  d(THREADSAFE).# 
29400 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48  define SQLITE_TH
29410 52 45 41 44 53 41 46 45 20 54 48 52 45 41 44 53  READSAFE THREADS
29420 41 46 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  AFE.#else.# defi
29430 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ne SQLITE_THREAD
29440 53 41 46 45 20 31 0a 23 65 6e 64 69 66 0a 23 65  SAFE 1.#endif.#e
29450 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  ndif../*.** Thes
29460 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c  e #defines shoul
29470 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69  d enable >2GB fi
29480 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f  le support on Po
29490 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e  six if the.** un
294a0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
294b0 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  ng system suppor
294c0 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f  ts it.  If the O
294d0 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65  S lacks.** large
294e0 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 6f   file support, o
294f0 72 20 69 66 20 74 68 65 20 4f 53 20 69 73 20 77  r if the OS is w
29500 69 6e 64 6f 77 73 2c 20 74 68 65 73 65 20 73 68  indows, these sh
29510 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a  ould be no-ops..
29520 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65  **.** Large file
29530 20 73 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20   support can be 
29540 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74  disabled using t
29550 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41  he -DSQLITE_DISA
29560 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a  BLE_LFS switch.*
29570 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  * on the compile
29580 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20  r command line. 
29590 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61   This is necessa
295a0 72 79 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f  ry if you are co
295b0 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20  mpiling.** on a 
295c0 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28  recent machine (
295d0 65 78 3a 20 52 65 64 48 61 74 20 37 2e 32 29 20  ex: RedHat 7.2) 
295e0 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75  but you want you
295f0 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a  r code to work.*
29600 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61  * on an older ma
29610 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61  chine (ex: RedHa
29620 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20  t 6.0).  If you 
29630 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 48 61  compile on RedHa
29640 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74  t 7.2.** without
29650 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46   this option, LF
29660 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 75  S is enable.  Bu
29670 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 65  t LFS does not e
29680 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72 6e  xist in the kern
29690 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74 20  el.** in RedHat 
296a0 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64 65  6.0, so the code
296b0 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65   won't work.  He
296c0 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d  nce, for maximum
296d0 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61   binary.** porta
296e0 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75 6c  bility you shoul
296f0 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a  d omit LFS..**.*
29700 2a 20 53 69 6d 69 6c 61 72 20 69 73 20 74 72 75  * Similar is tru
29710 65 20 66 6f 72 20 4d 61 63 4f 53 2e 20 20 4c 46  e for MacOS.  LF
29720 53 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72  S is only suppor
29730 74 65 64 20 6f 6e 20 4d 61 63 4f 53 20 39 20 61  ted on MacOS 9 a
29740 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69 66  nd later..*/.#if
29750 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41  ndef SQLITE_DISA
29760 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65  BLE_LFS.# define
29770 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20   _LARGE_FILE    
29780 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46     1.# ifndef _F
29790 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a  ILE_OFFSET_BITS.
297a0 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45  #   define _FILE
297b0 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a  _OFFSET_BITS 64.
297c0 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
297d0 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52   _LARGEFILE_SOUR
297e0 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  CE 1.#endif../**
297f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
29800 6c 75 64 65 20 68 61 73 68 2e 68 20 69 6e 20 74  lude hash.h in t
29810 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
29820 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
29830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
29840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
29850 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 68 20 2a  in file hash.h *
29860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
29890 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
298a0 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 22.**.** The a
298b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
298c0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
298d0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
298e0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
298f0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
29900 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
29910 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
29920 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
29930 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
29940 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
29950 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
29960 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
29970 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
29980 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
29990 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
299a0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
299b0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
299c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
299d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
299e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
299f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a00 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ***.** This is t
29a10 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66  he header file f
29a20 6f 72 20 74 68 65 20 67 65 6e 65 72 69 63 20 68  or the generic h
29a30 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d  ash-table implem
29a40 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20  enation.** used 
29a50 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  in SQLite..**.**
29a60 20 24 49 64 3a 20 68 61 73 68 2e 68 2c 76 20 31   $Id: hash.h,v 1
29a70 2e 31 31 20 32 30 30 37 2f 30 39 2f 30 34 20 31  .11 2007/09/04 1
29a80 34 3a 33 31 3a 34 37 20 64 61 6e 69 65 6c 6b 31  4:31:47 danielk1
29a90 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  977 Exp $.*/.#if
29aa0 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 48 41 53  ndef _SQLITE_HAS
29ab0 48 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51  H_H_.#define _SQ
29ac0 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a  LITE_HASH_H_../*
29ad0 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
29ae0 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75  tions of structu
29af0 72 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20  res. */.typedef 
29b00 73 74 72 75 63 74 20 48 61 73 68 20 48 61 73 68  struct Hash Hash
29b10 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
29b20 20 48 61 73 68 45 6c 65 6d 20 48 61 73 68 45 6c   HashElem HashEl
29b30 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65  em;../* A comple
29b40 74 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  te hash table is
29b50 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
29b60 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
29b70 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20  ructure..** The 
29b80 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69  internals of thi
29b90 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
29ba0 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f  intended to be o
29bb0 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a  paque -- client.
29bc0 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e  ** code should n
29bd0 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63  ot attempt to ac
29be0 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 74  cess or modify t
29bf0 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69  he fields of thi
29c00 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64  s structure.** d
29c10 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 65  irectly.  Change
29c20 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
29c30 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74 68  only by using th
29c40 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77  e routines below
29c50 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 6d 61  ..** However, ma
29c60 6e 79 20 6f 66 20 74 68 65 20 22 70 72 6f 63 65  ny of the "proce
29c70 64 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e 63  dures" and "func
29c80 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66  tions" for modif
29c90 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65  ying and.** acce
29ca0 73 73 69 6e 67 20 74 68 69 73 20 73 74 72 75 63  ssing this struc
29cb0 74 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79 20  ture are really 
29cc0 6d 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 61  macros, so we ca
29cd0 6e 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a  n't really make.
29ce0 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ** this structur
29cf0 65 20 6f 70 61 71 75 65 2e 0a 2a 2f 0a 73 74 72  e opaque..*/.str
29d00 75 63 74 20 48 61 73 68 20 7b 0a 20 20 63 68 61  uct Hash {.  cha
29d10 72 20 6b 65 79 43 6c 61 73 73 3b 20 20 20 20 20  r keyClass;     
29d20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 48       /* SQLITE_H
29d30 41 53 48 5f 49 4e 54 2c 20 5f 50 4f 49 4e 54 45  ASH_INT, _POINTE
29d40 52 2c 20 5f 53 54 52 49 4e 47 2c 20 5f 42 49 4e  R, _STRING, _BIN
29d50 41 52 59 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f  ARY */.  char co
29d60 70 79 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20  pyKey;          
29d70 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 70 79   /* True if copy
29d80 20 6f 66 20 6b 65 79 20 6d 61 64 65 20 6f 6e 20   of key made on 
29d90 69 6e 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20  insert */.  int 
29da0 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20  count;          
29db0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
29dc0 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73   entries in this
29dd0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
29de0 68 74 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20  htsize;         
29df0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
29e00 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20   buckets in the 
29e10 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
29e20 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b  HashElem *first;
29e30 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
29e40 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
29e50 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  the array */.  s
29e60 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20 20 20  truct _ht {     
29e70 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61         /* the ha
29e80 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  sh table */.    
29e90 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
29ea0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
29eb0 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69  er of entries wi
29ec0 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a  th this hash */.
29ed0 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 63 68      HashElem *ch
29ee0 61 69 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ain;         /* 
29ef0 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74  Pointer to first
29f00 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 69 73   entry with this
29f10 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74   hash */.  } *ht
29f20 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c  ;.};../* Each el
29f30 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73  ement in the has
29f40 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e  h table is an in
29f50 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
29f60 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75  llowing .** stru
29f70 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d  cture.  All elem
29f80 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ents are stored 
29f90 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62  on a single doub
29fa0 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a  ly-linked list..
29fb0 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69  **.** Again, thi
29fc0 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69  s structure is i
29fd0 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70  ntended to be op
29fe0 61 71 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e  aque, but it can
29ff0 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20  't really.** be 
2a000 6f 70 61 71 75 65 20 62 65 63 61 75 73 65 20 69  opaque because i
2a010 74 20 69 73 20 75 73 65 64 20 62 79 20 6d 61 63  t is used by mac
2a020 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48  ros..*/.struct H
2a030 61 73 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73 68  ashElem {.  Hash
2a040 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65  Elem *next, *pre
2a050 76 3b 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64  v;   /* Next and
2a060 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e   previous elemen
2a070 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ts in the table 
2a080 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b  */.  void *data;
2a090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a0a0 20 44 61 74 61 20 61 73 73 6f 63 69 61 74 65 64   Data associated
2a0b0 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65   with this eleme
2a0c0 6e 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b  nt */.  void *pK
2a0d0 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20  ey; int nKey;   
2a0e0 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74   /* Key associat
2a0f0 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65  ed with this ele
2a100 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ment */.};../*.*
2a110 2a 20 54 68 65 72 65 20 61 72 65 20 34 20 64 69  * There are 4 di
2a120 66 66 65 72 65 6e 74 20 6d 6f 64 65 73 20 6f 66  fferent modes of
2a130 20 6f 70 65 72 61 74 69 6f 6e 20 66 6f 72 20 61   operation for a
2a140 20 68 61 73 68 20 74 61 62 6c 65 3a 0a 2a 2a 0a   hash table:.**.
2a150 2a 2a 20 20 20 53 51 4c 49 54 45 5f 48 41 53 48  **   SQLITE_HASH
2a160 5f 49 4e 54 20 20 20 20 20 20 20 20 20 6e 4b 65  _INT         nKe
2a170 79 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65  y is used as the
2a180 20 6b 65 79 20 61 6e 64 20 70 4b 65 79 20 69 73   key and pKey is
2a190 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   ignored..**.** 
2a1a0 20 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 50 4f    SQLITE_HASH_PO
2a1b0 49 4e 54 45 52 20 20 20 20 20 70 4b 65 79 20 69  INTER     pKey i
2a1c0 73 20 75 73 65 64 20 61 73 20 74 68 65 20 6b 65  s used as the ke
2a1d0 79 20 61 6e 64 20 6e 4b 65 79 20 69 73 20 69 67  y and nKey is ig
2a1e0 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 53  nored..**.**   S
2a1f0 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e  QLITE_HASH_STRIN
2a200 47 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e  G      pKey poin
2a210 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 74  ts to a string t
2a220 68 61 74 20 69 73 20 6e 4b 65 79 20 62 79 74 65  hat is nKey byte
2a230 73 20 6c 6f 6e 67 0a 2a 2a 20 20 20 20 20 20 20  s long.**       
2a240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a250 20 20 20 20 28 69 6e 63 6c 75 64 69 6e 67 20 74      (including t
2a260 68 65 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74  he null-terminat
2a270 6f 72 2c 20 69 66 20 61 6e 79 29 2e 20 20 43 61  or, if any).  Ca
2a280 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  se.**           
2a290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a2a0 69 73 20 69 67 6e 6f 72 65 64 20 69 6e 20 63 6f  is ignored in co
2a2b0 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  mparisons..**.**
2a2c0 20 20 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 42     SQLITE_HASH_B
2a2d0 49 4e 41 52 59 20 20 20 20 20 20 70 4b 65 79 20  INARY      pKey 
2a2e0 70 6f 69 6e 74 73 20 74 6f 20 62 69 6e 61 72 79  points to binary
2a2f0 20 64 61 74 61 20 6e 4b 65 79 20 62 79 74 65 73   data nKey bytes
2a300 20 6c 6f 6e 67 2e 20 0a 2a 2a 20 20 20 20 20 20   long. .**      
2a310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a320 20 20 20 20 20 6d 65 6d 63 6d 70 28 29 20 69 73       memcmp() is
2a330 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65   used to compare
2a340 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63   keys..**.** A c
2a350 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69  opy of the key i
2a360 73 20 6d 61 64 65 20 66 6f 72 20 53 51 4c 49 54  s made for SQLIT
2a370 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 61 6e  E_HASH_STRING an
2a380 64 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 42 49  d SQLITE_HASH_BI
2a390 4e 41 52 59 0a 2a 2a 20 69 66 20 74 68 65 20 63  NARY.** if the c
2a3a0 6f 70 79 4b 65 79 20 70 61 72 61 6d 65 74 65 72  opyKey parameter
2a3b0 20 74 6f 20 48 61 73 68 49 6e 69 74 20 69 73 20   to HashInit is 
2a3c0 31 2e 20 20 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69  1.  .*/./* #defi
2a3d0 6e 65 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 49  ne SQLITE_HASH_I
2a3e0 4e 54 20 20 20 20 20 20 20 31 20 2f 2f 20 4e 4f  NT       1 // NO
2a3f0 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 20 23 64 65  T USED */./* #de
2a400 66 69 6e 65 20 53 51 4c 49 54 45 5f 48 41 53 48  fine SQLITE_HASH
2a410 5f 50 4f 49 4e 54 45 52 20 20 20 32 20 2f 2f 20  _POINTER   2 // 
2a420 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 23 64 65 66  NOT USED */.#def
2a430 69 6e 65 20 53 51 4c 49 54 45 5f 48 41 53 48 5f  ine SQLITE_HASH_
2a440 53 54 52 49 4e 47 20 20 20 20 33 0a 23 64 65 66  STRING    3.#def
2a450 69 6e 65 20 53 51 4c 49 54 45 5f 48 41 53 48 5f  ine SQLITE_HASH_
2a460 42 49 4e 41 52 59 20 20 20 20 34 0a 0a 2f 2a 0a  BINARY    4../*.
2a470 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e  ** Access routin
2a480 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20  es.  To delete, 
2a490 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f  insert a NULL po
2a4a0 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
2a4b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2a4c0 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 61  lite3HashInit(Ha
2a4d0 73 68 2a 2c 20 69 6e 74 20 6b 65 79 74 79 70 65  sh*, int keytype
2a4e0 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 3b 0a  , int copyKey);.
2a4f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2a500 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68  oid *sqlite3Hash
2a510 49 6e 73 65 72 74 28 48 61 73 68 2a 2c 20 63 6f  Insert(Hash*, co
2a520 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
2a530 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a  int nKey, void *
2a540 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50  pData);.SQLITE_P
2a550 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
2a560 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e  ite3HashFind(con
2a570 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20  st Hash*, const 
2a580 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
2a590 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nKey);.SQLITE_PR
2a5a0 49 56 41 54 45 20 48 61 73 68 45 6c 65 6d 20 2a  IVATE HashElem *
2a5b0 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 45  sqlite3HashFindE
2a5c0 6c 65 6d 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c  lem(const Hash*,
2a5d0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
2a5e0 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51  y, int nKey);.SQ
2a5f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2a600 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65  d sqlite3HashCle
2a610 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a  ar(Hash*);../*.*
2a620 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f  * Macros for loo
2a630 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c  ping over all el
2a640 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68  ements of a hash
2a650 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69   table.  The idi
2a660 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68  om is.** like th
2a670 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68  is:.**.**   Hash
2a680 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65   h;.**   HashEle
2a690 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a  m *p;.**   ....*
2a6a0 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65  *   for(p=sqlite
2a6b0 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70  HashFirst(&h); p
2a6c0 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; p=sqliteHashNe
2a6d0 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53  xt(p)){.**     S
2a6e0 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44  omeStructure *pD
2a6f0 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68  ata = sqliteHash
2a700 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20  Data(p);.**     
2a710 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  // do something 
2a720 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20  with pData.**   
2a730 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  }.*/.#define sql
2a740 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29 20  iteHashFirst(H) 
2a750 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64   ((H)->first).#d
2a760 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
2a770 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e  Next(E)   ((E)->
2a780 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71  next).#define sq
2a790 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29 20  liteHashData(E) 
2a7a0 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23 64    ((E)->data).#d
2a7b0 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
2a7c0 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e  Key(E)    ((E)->
2a7d0 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 73 71  pKey).#define sq
2a7e0 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65 28  liteHashKeysize(
2a7f0 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a  E) ((E)->nKey)..
2a800 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20  /*.** Number of 
2a810 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61 73  entries in a has
2a820 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66 69  h table.*/.#defi
2a830 6e 65 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75  ne sqliteHashCou
2a840 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75  nt(H)  ((H)->cou
2a850 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  nt)..#endif /* _
2a860 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a  SQLITE_HASH_H_ *
2a870 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2a880 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 68  ** End of hash.h
2a890 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2a8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a8c0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2a8d0 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
2a8e0 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
2a8f0 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
2a900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a910 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2a920 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 73 65  ** Include parse
2a930 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
2a940 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
2a950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a960 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2a970 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61  ** Begin file pa
2a980 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  rse.h **********
2a990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a9b0 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45  */.#define TK_SE
2a9c0 4d 49 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MI              
2a9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
2a9e0 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41  #define TK_EXPLA
2a9f0 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IN              
2aa00 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65             2.#de
2aa10 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20 20  fine TK_QUERY   
2aa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa30 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e          3.#defin
2aa40 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20  e TK_PLAN       
2aa50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa60 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 54       4.#define T
2aa70 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20 20  K_BEGIN         
2aa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa90 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54    5.#define TK_T
2aaa0 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20  RANSACTION      
2aab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
2aac0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45  .#define TK_DEFE
2aad0 52 52 45 44 20 20 20 20 20 20 20 20 20 20 20 20  RRED            
2aae0 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64              7.#d
2aaf0 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41  efine TK_IMMEDIA
2ab00 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
2ab10 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69           8.#defi
2ab20 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 20  ne TK_EXCLUSIVE 
2ab30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ab40 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20        9.#define 
2ab50 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 20 20  TK_COMMIT       
2ab60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ab70 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    10.#define TK_
2ab80 45 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20  END             
2ab90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
2aba0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c  1.#define TK_ROL
2abb0 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 20 20  LBACK           
2abc0 20 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 23              12.#
2abd0 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54 45  define TK_CREATE
2abe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abf0 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66           13.#def
2ac00 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20 20  ine TK_TABLE    
2ac10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ac20 20 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65        14.#define
2ac30 20 54 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20   TK_IF          
2ac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ac50 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 54 4b     15.#define TK
2ac60 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20  _NOT            
2ac70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ac80 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58  16.#define TK_EX
2ac90 49 53 54 53 20 20 20 20 20 20 20 20 20 20 20 20  ISTS            
2aca0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a               17.
2acb0 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20  #define TK_TEMP 
2acc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2acd0 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65            18.#de
2ace0 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20  fine TK_LP      
2acf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad00 20 20 20 20 20 20 20 31 39 0a 23 64 65 66 69 6e         19.#defin
2ad10 65 20 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20  e TK_RP         
2ad20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad30 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 54      20.#define T
2ad40 4b 5f 41 53 20 20 20 20 20 20 20 20 20 20 20 20  K_AS            
2ad50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad60 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   21.#define TK_C
2ad70 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 20 20 20  OMMA            
2ad80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 32                22
2ad90 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20  .#define TK_ID  
2ada0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adb0 20 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64             23.#d
2adc0 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20  efine TK_ABORT  
2add0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ade0 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 69          24.#defi
2adf0 6e 65 20 54 4b 5f 41 46 54 45 52 20 20 20 20 20  ne TK_AFTER     
2ae00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae10 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20       25.#define 
2ae20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20  TK_ANALYZE      
2ae30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae40 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    26.#define TK_
2ae50 41 53 43 20 20 20 20 20 20 20 20 20 20 20 20 20  ASC             
2ae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
2ae70 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 54 54  7.#define TK_ATT
2ae80 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20  ACH             
2ae90 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23              28.#
2aea0 64 65 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45  define TK_BEFORE
2aeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aec0 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66           29.#def
2aed0 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20  ine TK_CASCADE  
2aee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aef0 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 65        30.#define
2af00 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20   TK_CAST        
2af10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20 54 4b     31.#define TK
2af30 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 20 20 20  _CONFLICT       
2af40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af50 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 41  32.#define TK_DA
2af60 54 41 42 41 53 45 20 20 20 20 20 20 20 20 20 20  TABASE          
2af70 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a               33.
2af80 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53 43 20  #define TK_DESC 
2af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afa0 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 64 65            34.#de
2afb0 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48 20 20  fine TK_DETACH  
2afc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afd0 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e         35.#defin
2afe0 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20 20 20  e TK_EACH       
2aff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b000 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 54      36.#define T
2b010 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 20 20 20  K_FAIL          
2b020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b030 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46   37.#define TK_F
2b040 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OR              
2b050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38                38
2b060 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f  .#define TK_IGNO
2b070 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RE              
2b080 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64             39.#d
2b090 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c  efine TK_INITIAL
2b0a0 4c 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LY              
2b0b0 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 66 69          40.#defi
2b0c0 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 20 20 20  ne TK_INSTEAD   
2b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b0e0 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20       41.#define 
2b0f0 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20  TK_LIKE_KW      
2b100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b110 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f    42.#define TK_
2b120 4d 41 54 43 48 20 20 20 20 20 20 20 20 20 20 20  MATCH           
2b130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
2b140 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59  3.#define TK_KEY
2b150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b160 20 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23              44.#
2b170 64 65 66 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20  define TK_OF    
2b180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b190 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 66           45.#def
2b1a0 69 6e 65 20 54 4b 5f 4f 46 46 53 45 54 20 20 20  ine TK_OFFSET   
2b1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b1c0 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65        46.#define
2b1d0 20 54 4b 5f 50 52 41 47 4d 41 20 20 20 20 20 20   TK_PRAGMA      
2b1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b1f0 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20 54 4b     47.#define TK
2b200 5f 52 41 49 53 45 20 20 20 20 20 20 20 20 20 20  _RAISE          
2b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b220 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  48.#define TK_RE
2b230 50 4c 41 43 45 20 20 20 20 20 20 20 20 20 20 20  PLACE           
2b240 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a               49.
2b250 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 53 54 52  #define TK_RESTR
2b260 49 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ICT             
2b270 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64 65            50.#de
2b280 66 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20 20  fine TK_ROW     
2b290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2a0 20 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e         51.#defin
2b2b0 65 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20 20  e TK_TRIGGER    
2b2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2d0 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 54      52.#define T
2b2e0 4b 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20 20  K_VACUUM        
2b2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b300 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56   53.#define TK_V
2b310 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20 20  IEW             
2b320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34                54
2b330 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 52 54  .#define TK_VIRT
2b340 55 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  UAL             
2b350 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 23 64             55.#d
2b360 65 66 69 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58  efine TK_REINDEX
2b370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b380 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69          56.#defi
2b390 6e 65 20 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20  ne TK_RENAME    
2b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3b0 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20       57.#define 
2b3c0 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 20 20 20  TK_CTIME_KW     
2b3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3e0 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f    58.#define TK_
2b3f0 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 20  ANY             
2b400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
2b410 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20  9.#define TK_OR 
2b420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b430 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23              60.#
2b440 64 65 66 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20  define TK_AND   
2b450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b460 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 66           61.#def
2b470 69 6e 65 20 54 4b 5f 49 53 20 20 20 20 20 20 20  ine TK_IS       
2b480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b490 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 65        62.#define
2b4a0 20 54 4b 5f 42 45 54 57 45 45 4e 20 20 20 20 20   TK_BETWEEN     
2b4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4c0 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 54 4b     63.#define TK
2b4d0 5f 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  _IN             
2b4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4f0 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53  64.#define TK_IS
2b500 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
2b510 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a               65.
2b520 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55  #define TK_NOTNU
2b530 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LL              
2b540 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 64 65            66.#de
2b550 66 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20 20  fine TK_NE      
2b560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b570 20 20 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e         67.#defin
2b580 65 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20  e TK_EQ         
2b590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5a0 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20 54      68.#define T
2b5b0 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20  K_GT            
2b5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5d0 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c   69.#define TK_L
2b5e0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
2b5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30                70
2b600 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20  .#define TK_LT  
2b610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b620 20 20 20 20 20 20 20 20 20 20 20 37 31 0a 23 64             71.#d
2b630 65 66 69 6e 65 20 54 4b 5f 47 45 20 20 20 20 20  efine TK_GE     
2b640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b650 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69          72.#defi
2b660 6e 65 20 54 4b 5f 45 53 43 41 50 45 20 20 20 20  ne TK_ESCAPE    
2b670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b680 20 20 20 20 20 37 33 0a 23 64 65 66 69 6e 65 20       73.#define 
2b690 54 4b 5f 42 49 54 41 4e 44 20 20 20 20 20 20 20  TK_BITAND       
2b6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6b0 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    74.#define TK_
2b6c0 42 49 54 4f 52 20 20 20 20 20 20 20 20 20 20 20  BITOR           
2b6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
2b6e0 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48  5.#define TK_LSH
2b6f0 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20  IFT             
2b700 20 20 20 20 20 20 20 20 20 20 20 20 37 36 0a 23              76.#
2b710 64 65 66 69 6e 65 20 54 4b 5f 52 53 48 49 46 54  define TK_RSHIFT
2b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b730 20 20 20 20 20 20 20 20 20 37 37 0a 23 64 65 66           77.#def
2b740 69 6e 65 20 54 4b 5f 50 4c 55 53 20 20 20 20 20  ine TK_PLUS     
2b750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b760 20 20 20 20 20 20 37 38 0a 23 64 65 66 69 6e 65        78.#define
2b770 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 20 20 20   TK_MINUS       
2b780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b790 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20 54 4b     79.#define TK
2b7a0 5f 53 54 41 52 20 20 20 20 20 20 20 20 20 20 20  _STAR           
2b7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b7c0 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c  80.#define TK_SL
2b7d0 41 53 48 20 20 20 20 20 20 20 20 20 20 20 20 20  ASH             
2b7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31 0a               81.
2b7f0 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20  #define TK_REM  
2b800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b810 20 20 20 20 20 20 20 20 20 20 38 32 0a 23 64 65            82.#de
2b820 66 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20  fine TK_CONCAT  
2b830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b840 20 20 20 20 20 20 20 38 33 0a 23 64 65 66 69 6e         83.#defin
2b850 65 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20  e TK_COLLATE    
2b860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b870 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20 54      84.#define T
2b880 4b 5f 55 4d 49 4e 55 53 20 20 20 20 20 20 20 20  K_UMINUS        
2b890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8a0 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55   85.#define TK_U
2b8b0 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 20  PLUS            
2b8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36                86
2b8d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4e  .#define TK_BITN
2b8e0 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OT              
2b8f0 20 20 20 20 20 20 20 20 20 20 20 38 37 0a 23 64             87.#d
2b900 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47 20  efine TK_STRING 
2b910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b920 20 20 20 20 20 20 20 20 38 38 0a 23 64 65 66 69          88.#defi
2b930 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20  ne TK_JOIN_KW   
2b940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b950 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 20       89.#define 
2b960 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 20  TK_CONSTRAINT   
2b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b980 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    90.#define TK_
2b990 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20 20  DEFAULT         
2b9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
2b9b0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55 4c  1.#define TK_NUL
2b9c0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
2b9d0 20 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23              92.#
2b9e0 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52  define TK_PRIMAR
2b9f0 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
2ba00 20 20 20 20 20 20 20 20 20 39 33 0a 23 64 65 66           93.#def
2ba10 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20 20  ine TK_UNIQUE   
2ba20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba30 20 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e 65        94.#define
2ba40 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 20 20   TK_CHECK       
2ba50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba60 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 54 4b     95.#define TK
2ba70 5f 52 45 46 45 52 45 4e 43 45 53 20 20 20 20 20  _REFERENCES     
2ba80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba90 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55  96.#define TK_AU
2baa0 54 4f 49 4e 43 52 20 20 20 20 20 20 20 20 20 20  TOINCR          
2bab0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a               97.
2bac0 23 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20  #define TK_ON   
2bad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bae0 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 65            98.#de
2baf0 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20  fine TK_DELETE  
2bb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb10 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69 6e         99.#defin
2bb20 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20 20  e TK_UPDATE     
2bb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb40 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20      100.#define 
2bb50 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20  TK_INSERT       
2bb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb70 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 54 4b    101.#define TK
2bb80 5f 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20  _SET            
2bb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bba0 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44  102.#define TK_D
2bbb0 45 46 45 52 52 41 42 4c 45 20 20 20 20 20 20 20  EFERRABLE       
2bbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
2bbd0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52  3.#define TK_FOR
2bbe0 45 49 47 4e 20 20 20 20 20 20 20 20 20 20 20 20  EIGN            
2bbf0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34 0a              104.
2bc00 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20  #define TK_DROP 
2bc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64            105.#d
2bc30 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20  efine TK_UNION  
2bc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc50 20 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66          106.#def
2bc60 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20  ine TK_ALL      
2bc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc80 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 69 6e        107.#defin
2bc90 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20 20 20  e TK_EXCEPT     
2bca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bcb0 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20      108.#define 
2bcc0 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 20 20 20  TK_INTERSECT    
2bcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bce0 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 54 4b    109.#define TK
2bcf0 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20  _SELECT         
2bd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bd10 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44  110.#define TK_D
2bd20 49 53 54 49 4e 43 54 20 20 20 20 20 20 20 20 20  ISTINCT         
2bd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
2bd40 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54  1.#define TK_DOT
2bd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bd60 20 20 20 20 20 20 20 20 20 20 20 20 31 31 32 0a              112.
2bd70 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20  #define TK_FROM 
2bd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bd90 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64            113.#d
2bda0 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20  efine TK_JOIN   
2bdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdc0 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 66          114.#def
2bdd0 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20 20 20 20  ine TK_USING    
2bde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdf0 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e        115.#defin
2be00 65 20 54 4b 5f 4f 52 44 45 52 20 20 20 20 20 20  e TK_ORDER      
2be10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65 20      116.#define 
2be30 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 20 20  TK_BY           
2be40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be50 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 54 4b    117.#define TK
2be60 5f 47 52 4f 55 50 20 20 20 20 20 20 20 20 20 20  _GROUP          
2be70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be80 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48  118.#define TK_H
2be90 41 56 49 4e 47 20 20 20 20 20 20 20 20 20 20 20  AVING           
2bea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
2beb0 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d  9.#define TK_LIM
2bec0 49 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IT              
2bed0 20 20 20 20 20 20 20 20 20 20 20 20 31 32 30 0a              120.
2bee0 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45  #define TK_WHERE
2bef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf00 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 23 64            121.#d
2bf10 65 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20  efine TK_INTO   
2bf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf30 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66          122.#def
2bf40 69 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20  ine TK_VALUES   
2bf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf60 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e        123.#defin
2bf70 65 20 54 4b 5f 49 4e 54 45 47 45 52 20 20 20 20  e TK_INTEGER    
2bf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf90 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20      124.#define 
2bfa0 54 4b 5f 46 4c 4f 41 54 20 20 20 20 20 20 20 20  TK_FLOAT        
2bfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bfc0 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20 54 4b    125.#define TK
2bfd0 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20  _BLOB           
2bfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bff0 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52  126.#define TK_R
2c000 45 47 49 53 54 45 52 20 20 20 20 20 20 20 20 20  EGISTER         
2c010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
2c020 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 52  7.#define TK_VAR
2c030 49 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20  IABLE           
2c040 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38 0a              128.
2c050 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 45 20  #define TK_CASE 
2c060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c070 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 23 64            129.#d
2c080 65 66 69 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20  efine TK_WHEN   
2c090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0a0 20 20 20 20 20 20 20 20 31 33 30 0a 23 64 65 66          130.#def
2c0b0 69 6e 65 20 54 4b 5f 54 48 45 4e 20 20 20 20 20  ine TK_THEN     
2c0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0d0 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e        131.#defin
2c0e0 65 20 54 4b 5f 45 4c 53 45 20 20 20 20 20 20 20  e TK_ELSE       
2c0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c100 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20      132.#define 
2c110 54 4b 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20  TK_INDEX        
2c120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c130 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 54 4b    133.#define TK
2c140 5f 41 4c 54 45 52 20 20 20 20 20 20 20 20 20 20  _ALTER          
2c150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c160 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  134.#define TK_T
2c170 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  O               
2c180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
2c190 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 44  5.#define TK_ADD
2c1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1b0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a              136.
2c1c0 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d  #define TK_COLUM
2c1d0 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20  NKW             
2c1e0 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 23 64            137.#d
2c1f0 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 54  efine TK_TO_TEXT
2c200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c210 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66          138.#def
2c220 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20  ine TK_TO_BLOB  
2c230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c240 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69 6e        139.#defin
2c250 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 20  e TK_TO_NUMERIC 
2c260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c270 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65 20      140.#define 
2c280 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20 20  TK_TO_INT       
2c290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2a0 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20 54 4b    141.#define TK
2c2b0 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20 20 20 20  _TO_REAL        
2c2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2d0 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45  142.#define TK_E
2c2e0 4e 44 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20  ND_OF_FILE      
2c2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
2c300 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c  3.#define TK_ILL
2c310 45 47 41 4c 20 20 20 20 20 20 20 20 20 20 20 20  EGAL            
2c320 20 20 20 20 20 20 20 20 20 20 20 20 31 34 34 0a              144.
2c330 23 64 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45  #define TK_SPACE
2c340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c350 20 20 20 20 20 20 20 20 20 20 31 34 35 0a 23 64            145.#d
2c360 65 66 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45  efine TK_UNCLOSE
2c370 44 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20  D_STRING        
2c380 20 20 20 20 20 20 20 20 31 34 36 0a 23 64 65 66          146.#def
2c390 69 6e 65 20 54 4b 5f 43 4f 4d 4d 45 4e 54 20 20  ine TK_COMMENT  
2c3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3b0 20 20 20 20 20 20 31 34 37 0a 23 64 65 66 69 6e        147.#defin
2c3c0 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 20  e TK_FUNCTION   
2c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3e0 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e 65 20      148.#define 
2c3f0 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20  TK_COLUMN       
2c400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c410 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20 54 4b    149.#define TK
2c420 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20 20  _AGG_FUNCTION   
2c430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c440 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41  150.#define TK_A
2c450 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20  GG_COLUMN       
2c460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
2c470 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e  1.#define TK_CON
2c480 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20 20  ST_FUNC         
2c490 20 20 20 20 20 20 20 20 20 20 20 20 31 35 32 0a              152.
2c4a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2c4b0 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 68 20   End of parse.h 
2c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2c4f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2c500 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
2c510 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
2c520 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
2c530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2c540 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f  .#include <stdio
2c550 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74  .h>.#include <st
2c560 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  dlib.h>.#include
2c570 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63   <string.h>.#inc
2c580 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a  lude <assert.h>.
2c590 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 64 65 66  #include <stddef
2c5a0 2e 68 3e 0a 0a 23 64 65 66 69 6e 65 20 73 71 6c  .h>..#define sql
2c5b0 69 74 65 33 5f 69 73 6e 61 6e 28 58 29 20 20 28  ite3_isnan(X)  (
2c5c0 28 58 29 21 3d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a  (X)!=(X))../*.**
2c5d0 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f   If compiling fo
2c5e0 72 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68  r a processor th
2c5f0 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e  at lacks floatin
2c600 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c  g point support,
2c610 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20 69  .** substitute i
2c620 6e 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74  nteger for float
2c630 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66  ing-point.*/.#if
2c640 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2c650 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23  FLOATING_POINT.#
2c660 20 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73   define double s
2c670 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 64 65  qlite_int64.# de
2c680 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f  fine LONGDOUBLE_
2c690 54 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36  TYPE sqlite_int6
2c6a0 34 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54  4.# ifndef SQLIT
2c6b0 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20 64 65  E_BIG_DBL.#   de
2c6c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f  fine SQLITE_BIG_
2c6d0 44 42 4c 20 28 30 78 37 66 66 66 66 66 66 66 66  DBL (0x7ffffffff
2c6e0 66 66 66 66 66 66 66 29 0a 23 20 65 6e 64 69 66  fffffff).# endif
2c6f0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
2c700 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46  _OMIT_DATETIME_F
2c710 55 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65 20  UNCS 1.# define 
2c720 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
2c730 45 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  E 1.# undef SQLI
2c740 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f  TE_MIXED_ENDIAN_
2c750 36 34 42 49 54 5f 46 4c 4f 41 54 0a 23 65 6e 64  64BIT_FLOAT.#end
2c760 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
2c770 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69  E_BIG_DBL.# defi
2c780 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42  ne SQLITE_BIG_DB
2c790 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a  L (1e99).#endif.
2c7a0 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50  ./*.** OMIT_TEMP
2c7b0 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 69  DB is set to 1 i
2c7c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
2c7d0 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c  MPDB is defined,
2c7e0 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61   or 0.** afterwa
2c7f0 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20  rd. Having this 
2c800 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20  macro allows us 
2c810 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 63  to cause the C c
2c820 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f  ompiler .** to o
2c830 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 79  mit code used by
2c840 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74   TEMP tables wit
2c850 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64  hout messy #ifnd
2c860 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  ef statements..*
2c870 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
2c880 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66  OMIT_TEMPDB.#def
2c890 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20  ine OMIT_TEMPDB 
2c8a0 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  1.#else.#define 
2c8b0 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65  OMIT_TEMPDB 0.#e
2c8c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ndif../*.** If t
2c8d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
2c8e0 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20  ro is set to 1, 
2c8f0 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73  then NULL values
2c900 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a   are considered.
2c910 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e  ** distinct when
2c920 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65   determining whe
2c930 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20  ther or not two 
2c940 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20  entries are the 
2c950 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49  same.** in a UNI
2c960 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73  QUE index.  This
2c970 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 74   is the way Post
2c980 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20  greSQL, Oracle, 
2c990 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f  DB2, MySQL,.** O
2c9a0 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62  CELOT, and Fireb
2c9b0 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54  ird all work.  T
2c9c0 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 78  he SQL92 spec ex
2c9d0 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 68  plicitly says th
2c9e0 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79  is.** is the way
2c9f0 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 70   things are supp
2ca00 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a  ose to work..**.
2ca10 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** If the follow
2ca20 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74  ing macro is set
2ca30 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73   to 0, the NULLs
2ca40 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20   are indistinct 
2ca50 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20  for.** a UNIQUE 
2ca60 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20  index.  In this 
2ca70 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e  mode, you can on
2ca80 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65  ly have a single
2ca90 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66   NULL entry.** f
2caa0 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c  or a column decl
2cab0 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68  ared UNIQUE.  Th
2cac0 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49 6e  is is the way In
2cad0 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53  formix and SQL S
2cae0 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a  erver.** work..*
2caf0 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44  /.#define NULL_D
2cb00 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51  ISTINCT_FOR_UNIQ
2cb10 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  UE 1../*.** The 
2cb20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75  "file format" nu
2cb30 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67  mber is an integ
2cb40 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65  er that is incre
2cb50 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a  mented whenever.
2cb60 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65  ** the VDBE-leve
2cb70 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68  l file format ch
2cb80 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c  anges.  The foll
2cb90 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66  owing macros def
2cba0 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64  ine the.** the d
2cbb0 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d  efault file form
2cbc0 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62  at for new datab
2cbd0 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78  ases and the max
2cbe0 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74  imum file format
2cbf0 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62  .** that the lib
2cc00 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a  rary can read..*
2cc10 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
2cc20 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  _MAX_FILE_FORMAT
2cc30 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   4.#ifndef SQLIT
2cc40 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46  E_DEFAULT_FILE_F
2cc50 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53  ORMAT.# define S
2cc60 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49  QLITE_DEFAULT_FI
2cc70 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64  LE_FORMAT 1.#end
2cc80 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64  if../*.** Provid
2cc90 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75  e a default valu
2cca0 65 20 66 6f 72 20 54 45 4d 50 5f 53 54 4f 52 45  e for TEMP_STORE
2ccb0 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 20 6e   in case it is n
2ccc0 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  ot specified.** 
2ccd0 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c  on the command-l
2cce0 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 54  ine.*/.#ifndef T
2ccf0 45 4d 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69  EMP_STORE.# defi
2cd00 6e 65 20 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a  ne TEMP_STORE 1.
2cd10 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43  #endif../*.** GC
2cd20 43 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e  C does not defin
2cd30 65 20 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29  e the offsetof()
2cd40 20 6d 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20   macro so we'll 
2cd50 68 61 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a  have to do it.**
2cd60 20 6f 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23   ourselves..*/.#
2cd70 69 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a  ifndef offsetof.
2cd80 23 64 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66  #define offsetof
2cd90 28 53 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44  (STRUCTURE,FIELD
2cda0 29 20 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29  ) ((int)((char*)
2cdb0 26 28 28 53 54 52 55 43 54 55 52 45 2a 29 30 29  &((STRUCTURE*)0)
2cdc0 2d 3e 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66  ->FIELD)).#endif
2cdd0 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
2cde0 20 73 65 65 20 69 66 20 74 68 69 73 20 6d 61 63   see if this mac
2cdf0 68 69 6e 65 20 75 73 65 73 20 45 42 43 44 49 43  hine uses EBCDIC
2ce00 2e 20 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65  .  (Yes, believe
2ce10 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74   it or.** not, t
2ce20 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d  here are still m
2ce30 61 63 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72  achines out ther
2ce40 65 20 74 68 61 74 20 75 73 65 20 45 42 43 44 49  e that use EBCDI
2ce50 43 2e 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d  C.).*/.#if 'A' =
2ce60 3d 20 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e  = '\301'.# defin
2ce70 65 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20  e SQLITE_EBCDIC 
2ce80 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  1.#else.# define
2ce90 20 53 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a   SQLITE_ASCII 1.
2cea0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
2ceb0 74 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20  tegers of known 
2cec0 73 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79  sizes.  These ty
2ced0 70 65 64 65 66 73 20 6d 69 67 68 74 20 63 68 61  pedefs might cha
2cee0 6e 67 65 20 66 6f 72 20 61 72 63 68 69 74 65 63  nge for architec
2cef0 74 75 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74  tures.** where t
2cf00 68 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20  he sizes very.  
2cf10 50 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63  Preprocessor mac
2cf20 72 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ros are availabl
2cf30 65 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a  e so that the.**
2cf40 20 74 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f   types can be co
2cf50 6e 76 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66  nveniently redef
2cf60 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d  ined at compile-
2cf70 74 79 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73  type.  Like this
2cf80 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
2cf90 63 63 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59  cc '-DUINTPTR_TY
2cfa0 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74  PE=long long int
2cfb0 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ' ....*/.#ifndef
2cfc0 20 55 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 64   UINT32_TYPE.# d
2cfd0 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 50  efine UINT32_TYP
2cfe0 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a 23  E unsigned int.#
2cff0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49  endif.#ifndef UI
2d000 4e 54 31 36 5f 54 59 50 45 0a 23 20 64 65 66 69  NT16_TYPE.# defi
2d010 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ne UINT16_TYPE u
2d020 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
2d030 74 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  t.#endif.#ifndef
2d040 20 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 64 65   INT16_TYPE.# de
2d050 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20  fine INT16_TYPE 
2d060 73 68 6f 72 74 20 69 6e 74 0a 23 65 6e 64 69 66  short int.#endif
2d070 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f 54  .#ifndef UINT8_T
2d080 59 50 45 0a 23 20 64 65 66 69 6e 65 20 55 49 4e  YPE.# define UIN
2d090 54 38 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64  T8_TYPE unsigned
2d0a0 20 63 68 61 72 0a 23 65 6e 64 69 66 0a 23 69 66   char.#endif.#if
2d0b0 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45 0a 23  ndef INT8_TYPE.#
2d0c0 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50   define INT8_TYP
2d0d0 45 20 73 69 67 6e 65 64 20 63 68 61 72 0a 23 65  E signed char.#e
2d0e0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f 4e  ndif.#ifndef LON
2d0f0 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20 64  GDOUBLE_TYPE.# d
2d100 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45  efine LONGDOUBLE
2d110 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62 6c  _TYPE long doubl
2d120 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65 66  e.#endif.typedef
2d130 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 36   sqlite_int64 i6
2d140 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 38  4;          /* 8
2d150 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74  -byte signed int
2d160 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20  eger */.typedef 
2d170 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75 36  sqlite_uint64 u6
2d180 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38 2d  4;         /* 8-
2d190 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e  byte unsigned in
2d1a0 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66  teger */.typedef
2d1b0 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33 32   UINT32_TYPE u32
2d1c0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34  ;           /* 4
2d1d0 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69  -byte unsigned i
2d1e0 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
2d1f0 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 31  f UINT16_TYPE u1
2d200 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  6;           /* 
2d210 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  2-byte unsigned 
2d220 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
2d230 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69 31  ef INT16_TYPE i1
2d240 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  6;            /*
2d250 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   2-byte signed i
2d260 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
2d270 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38 3b  f UINT8_TYPE u8;
2d280 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d290 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  1-byte unsigned 
2d2a0 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
2d2b0 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 69 38  ef UINT8_TYPE i8
2d2c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2d2d0 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   1-byte signed i
2d2e0 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  nteger */../*.**
2d2f0 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72   Macros to deter
2d300 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68 65  mine whether the
2d310 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67 20   machine is big 
2d320 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e  or little endian
2d330 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 61  ,.** evaluated a
2d340 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69  t runtime..*/.#i
2d350 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c  fdef SQLITE_AMAL
2d360 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f  GAMATION.SQLITE_
2d370 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e  PRIVATE const in
2d380 74 20 73 71 6c 69 74 65 33 4f 6e 65 3b 0a 23 65  t sqlite3One;.#e
2d390 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  lse.SQLITE_PRIVA
2d3a0 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c  TE const int sql
2d3b0 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69 66 0a  ite3one;.#endif.
2d3c0 23 69 66 20 64 65 66 69 6e 65 64 28 69 33 38 36  #if defined(i386
2d3d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69  ) || defined(__i
2d3e0 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  386__) || define
2d3f0 64 28 5f 4d 5f 49 58 38 36 29 0a 23 20 64 65 66  d(_M_IX86).# def
2d400 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e  ine SQLITE_BIGEN
2d410 44 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69  DIAN    0.# defi
2d420 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45  ne SQLITE_LITTLE
2d430 45 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e  ENDIAN 1.# defin
2d440 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  e SQLITE_UTF16NA
2d450 54 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46  TIVE  SQLITE_UTF
2d460 31 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66  16LE.#else.# def
2d470 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e  ine SQLITE_BIGEN
2d480 44 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20  DIAN    (*(char 
2d490 2a 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d  *)(&sqlite3one)=
2d4a0 3d 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  =0).# define SQL
2d4b0 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e  ITE_LITTLEENDIAN
2d4c0 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c   (*(char *)(&sql
2d4d0 69 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64  ite3one)==1).# d
2d4e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46  efine SQLITE_UTF
2d4f0 31 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45  16NATIVE (SQLITE
2d500 5f 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54  _BIGENDIAN?SQLIT
2d510 45 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45  E_UTF16BE:SQLITE
2d520 5f 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66  _UTF16LE).#endif
2d530 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
2d540 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
2d550 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69  wing structure i
2d560 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
2d570 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72  the busy-handler
2d580 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72  .** callback for
2d590 20 61 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20   a given sqlite 
2d5a0 68 61 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54  handle. .**.** T
2d5b0 68 65 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61  he sqlite.busyHa
2d5c0 6e 64 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20  ndler member of 
2d5d0 74 68 65 20 73 71 6c 69 74 65 20 73 74 72 75 63  the sqlite struc
2d5e0 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62  t contains the b
2d5f0 75 73 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20  usy.** callback 
2d600 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
2d610 20 68 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61   handle. Each pa
2d620 67 65 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74  ger opened via t
2d630 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e  he sqlite.** han
2d640 64 6c 65 20 69 73 20 70 61 73 73 65 64 20 61 20  dle is passed a 
2d650 70 6f 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74  pointer to sqlit
2d660 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54  e.busyHandler. T
2d670 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a  he busy-handler.
2d680 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63  ** callback is c
2d690 75 72 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64  urrently invoked
2d6a0 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69   only from withi
2d6b0 6e 20 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79  n pager.c..*/.ty
2d6c0 70 65 64 65 66 20 73 74 72 75 63 74 20 42 75 73  pedef struct Bus
2d6d0 79 48 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e  yHandler BusyHan
2d6e0 64 6c 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73  dler;.struct Bus
2d6f0 79 48 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74  yHandler {.  int
2d700 20 28 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a   (*xFunc)(void *
2d710 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62  ,int);  /* The b
2d720 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  usy callback */.
2d730 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20    void *pArg;   
2d740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d750 46 69 72 73 74 20 61 72 67 20 74 6f 20 62 75 73  First arg to bus
2d760 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  y callback */.  
2d770 69 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20  int nBusy;      
2d780 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2d790 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65  cremented with e
2d7a0 61 63 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f  ach busy call */
2d7b0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72  .};../*.** Defer
2d7c0 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e 68   sourcing vdbe.h
2d7d0 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e 74   and btree.h unt
2d7e0 69 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 38  il after the "u8
2d7f0 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48  " and .** "BusyH
2d800 61 6e 64 6c 65 72 20 74 79 70 65 64 65 66 73 2e  andler typedefs.
2d810 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
2d820 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65  *** Include btre
2d830 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  e.h in the middl
2d840 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
2d850 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2d860 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2d870 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
2d880 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tree.h *********
2d890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d8b0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
2d8c0 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
2d8d0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2d8e0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2d8f0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2d900 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2d910 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2d920 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2d930 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2d940 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2d950 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2d960 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2d970 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2d980 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2d990 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2d9a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2d9b0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2d9c0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2d9d0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2d9e0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2d9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2da00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2da10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2da20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
2da30 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64  is header file d
2da40 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
2da50 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71  face that the sq
2da60 6c 69 74 65 20 42 2d 54 72 65 65 20 66 69 6c 65  lite B-Tree file
2da70 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 20  .** subsystem.  
2da80 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20  See comments in 
2da90 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20  the source code 
2daa0 66 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64  for a detailed d
2dab0 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 20 6f 66  escription.** of
2dac0 20 77 68 61 74 20 65 61 63 68 20 69 6e 74 65 72   what each inter
2dad0 66 61 63 65 20 72 6f 75 74 69 6e 65 20 64 6f 65  face routine doe
2dae0 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  s..**.** @(#) $I
2daf0 64 3a 20 62 74 72 65 65 2e 68 2c 76 20 31 2e 39  d: btree.h,v 1.9
2db00 33 20 32 30 30 37 2f 30 39 2f 30 33 20 31 35 3a  3 2007/09/03 15:
2db10 31 39 3a 33 35 20 64 72 68 20 45 78 70 20 24 0a  19:35 drh Exp $.
2db20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54 52 45  */.#ifndef _BTRE
2db30 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54  E_H_.#define _BT
2db40 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a  REE_H_../* TODO:
2db50 20 54 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e   This definition
2db60 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75 64 65   is just include
2db70 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c  d so other modul
2db80 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a  es compile. It.*
2db90 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  * needs to be re
2dba0 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66  visited..*/.#def
2dbb0 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52  ine SQLITE_N_BTR
2dbc0 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a  EE_META 10../*.*
2dbd0 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 73 20  * If defined as 
2dbe0 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76  non-zero, auto-v
2dbf0 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64  acuum is enabled
2dc00 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68   by default. Oth
2dc10 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73  erwise.** it mus
2dc20 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66  t be turned on f
2dc30 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65  or each database
2dc40 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61   using "PRAGMA a
2dc50 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e  uto_vacuum = 1".
2dc60 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
2dc70 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56  TE_DEFAULT_AUTOV
2dc80 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e 65 20  ACUUM.  #define 
2dc90 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41  SQLITE_DEFAULT_A
2dca0 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64  UTOVACUUM 0.#end
2dcb0 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54 52 45  if..#define BTRE
2dcc0 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e  E_AUTOVACUUM_NON
2dcd0 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  E 0        /* Do
2dce0 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61 63   not do auto-vac
2dcf0 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  uum */.#define B
2dd00 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
2dd10 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20 2f 2a  FULL 1        /*
2dd20 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61   Do full auto-va
2dd30 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cuum */.#define 
2dd40 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
2dd50 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20 20 2f  _INCR 2        /
2dd60 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61  * Incremental va
2dd70 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46  cuum */../*.** F
2dd80 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
2dd90 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65  ons of structure
2dda0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2ddb0 63 74 20 42 74 72 65 65 20 42 74 72 65 65 3b 0a  ct Btree Btree;.
2ddc0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
2ddd0 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 6f 72  tCursor BtCursor
2dde0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2ddf0 20 42 74 53 68 61 72 65 64 20 42 74 53 68 61 72   BtShared BtShar
2de00 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ed;.typedef stru
2de10 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  ct BtreeMutexArr
2de20 61 79 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  ay BtreeMutexArr
2de30 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ay;../*.** This 
2de40 73 74 72 75 63 74 75 72 65 20 72 65 63 6f 72 64  structure record
2de50 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42 74 72  s all of the Btr
2de60 65 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ees that need to
2de70 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78   hold.** a mutex
2de80 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74 65 72   before we enter
2de90 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
2dea0 28 29 2e 20 20 54 68 65 20 42 74 72 65 65 73 20  ().  The Btrees 
2deb0 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65  are.** are place
2dec0 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e  d in aBtree[] in
2ded0 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72 65 65   order of aBtree
2dee0 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74 20 77  []->pBt.  That w
2def0 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c  ay,.** we can al
2df00 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e  ways lock and un
2df10 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75  lock them all qu
2df20 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74  ickly..*/.struct
2df30 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79   BtreeMutexArray
2df40 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b   {.  int nMutex;
2df50 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72 65 65  .  Btree *aBtree
2df60 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41  [SQLITE_MAX_ATTA
2df70 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51  CHED+1];.};...SQ
2df80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2df90 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65   sqlite3BtreeOpe
2dfa0 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  n(.  const char 
2dfb0 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a  *zFilename,   /*
2dfc0 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
2dfd0 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a  e file to open *
2dfe0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  /.  sqlite3 *db,
2dff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e000 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62  Associated datab
2e010 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2e020 2f 0a 20 20 42 74 72 65 65 20 2a 2a 2c 20 20 20  /.  Btree **,   
2e030 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e040 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 65  Return open Btre
2e050 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  e* here */.  int
2e060 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
2e070 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a        /* Flags *
2e080 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73  /.  int vfsFlags
2e090 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e0a0 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  Flags passed thr
2e0b0 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70 65 6e  ough to VFS open
2e0c0 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66   */.);../* The f
2e0d0 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74  lags parameter t
2e0e0 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70  o sqlite3BtreeOp
2e0f0 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 62 69  en can be the bi
2e100 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68 65 0a  twise or of the.
2e110 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  ** following val
2e120 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a  ues..**.** NOTE:
2e130 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d    These values m
2e140 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f  ust match the co
2e150 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 45  rresponding PAGE
2e160 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20  R_ values in.** 
2e170 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66  pager.h..*/.#def
2e180 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a  ine BTREE_OMIT_J
2e190 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f  OURNAL  1  /* Do
2e1a0 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c   not use journal
2e1b0 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a  .  No argument *
2e1c0 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
2e1d0 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32 20  NO_READLOCK   2 
2e1e0 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63   /* Omit readloc
2e1f0 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66  ks on readonly f
2e200 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  iles */.#define 
2e210 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20  BTREE_MEMORY    
2e220 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d      4  /* In-mem
2e230 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72 67 75  ory DB.  No argu
2e240 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ment */.#define 
2e250 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20  BTREE_READONLY  
2e260 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74      8  /* Open t
2e270 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 72  he database in r
2e280 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f  ead-only mode */
2e290 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52  .#define BTREE_R
2e2a0 45 41 44 57 52 49 54 45 20 20 20 20 31 36 20 20  EADWRITE    16  
2e2b0 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68  /* Open for both
2e2c0 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69   reading and wri
2e2d0 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ting */.#define 
2e2e0 42 54 52 45 45 5f 43 52 45 41 54 45 20 20 20 20  BTREE_CREATE    
2e2f0 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61 74 65     32  /* Create
2e300 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 66   the database if
2e310 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69   it does not exi
2e320 73 74 20 2a 2f 0a 0a 2f 2a 20 41 64 64 69 74 69  st */../* Additi
2e330 6f 6e 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20  onal values for 
2e340 74 68 65 20 34 74 68 20 61 72 67 75 6d 65 6e 74  the 4th argument
2e350 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72 65 65   of sqlite3Btree
2e360 4f 70 65 6e 20 74 68 61 74 0a 2a 2a 20 61 72 65  Open that.** are
2e370 20 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20   not associated 
2e380 77 69 74 68 20 50 41 47 45 52 5f 20 76 61 6c 75  with PAGER_ valu
2e390 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42  es..*/.#define B
2e3a0 54 52 45 45 5f 50 52 49 56 41 54 45 20 20 20 20  TREE_PRIVATE    
2e3b0 20 20 36 34 20 20 2f 2a 20 4e 65 76 65 72 20 73    64  /* Never s
2e3c0 68 61 72 65 20 77 69 74 68 20 6f 74 68 65 72 20  hare with other 
2e3d0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 0a  connections */..
2e3e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2e3f0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
2e400 6c 6f 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51  lose(Btree*);.SQ
2e410 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2e420 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
2e430 42 75 73 79 48 61 6e 64 6c 65 72 28 42 74 72 65  BusyHandler(Btre
2e440 65 2a 2c 42 75 73 79 48 61 6e 64 6c 65 72 2a 29  e*,BusyHandler*)
2e450 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
2e460 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
2e470 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74  eSetCacheSize(Bt
2e480 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ree*,int);.SQLIT
2e490 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2e4a0 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66  lite3BtreeSetSaf
2e4b0 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a 2c  etyLevel(Btree*,
2e4c0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
2e4d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2e4e0 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73  ite3BtreeSyncDis
2e4f0 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a 53  abled(Btree*);.S
2e500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2e510 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
2e520 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a  tPageSize(Btree*
2e530 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
2e540 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2e550 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
2e560 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0a 53  eSize(Btree*);.S
2e570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2e580 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61  t sqlite3BtreeMa
2e590 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65  xPageCount(Btree
2e5a0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
2e5b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e5c0 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76  e3BtreeGetReserv
2e5d0 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  e(Btree*);.SQLIT
2e5e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2e5f0 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74  lite3BtreeSetAut
2e600 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 2c  oVacuum(Btree *,
2e610 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
2e620 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2e630 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63  3BtreeGetAutoVac
2e640 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 53 51  uum(Btree *);.SQ
2e650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2e660 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
2e670 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 2c 69  inTrans(Btree*,i
2e680 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
2e690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
2e6a0 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
2e6b0 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e 73 74  ne(Btree*, const
2e6c0 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 3b   char *zMaster);
2e6d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2e6e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
2e6f0 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42  CommitPhaseTwo(B
2e700 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
2e710 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e720 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74  e3BtreeCommit(Bt
2e730 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
2e740 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2e750 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42  3BtreeRollback(B
2e760 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
2e770 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e780 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74  e3BtreeBeginStmt
2e790 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
2e7a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2e7b0 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53  ite3BtreeCommitS
2e7c0 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  tmt(Btree*);.SQL
2e7d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2e7e0 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
2e7f0 62 61 63 6b 53 74 6d 74 28 42 74 72 65 65 2a 29  backStmt(Btree*)
2e800 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
2e810 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
2e820 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72  eCreateTable(Btr
2e830 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66  ee*, int*, int f
2e840 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  lags);.SQLITE_PR
2e850 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2e860 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
2e870 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
2e880 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2e890 74 65 33 42 74 72 65 65 49 73 49 6e 53 74 6d 74  te3BtreeIsInStmt
2e8a0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
2e8b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2e8c0 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61  ite3BtreeIsInRea
2e8d0 64 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a  dTrans(Btree*);.
2e8e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2e8f0 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65  oid *sqlite3Btre
2e900 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 2c  eSchema(Btree *,
2e910 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f   int, void(*)(vo
2e920 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  id *));.SQLITE_P
2e930 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e940 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63  e3BtreeSchemaLoc
2e950 6b 65 64 28 42 74 72 65 65 20 2a 29 3b 0a 53 51  ked(Btree *);.SQ
2e960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2e970 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63   sqlite3BtreeLoc
2e980 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 2c 20  kTable(Btree *, 
2e990 69 6e 74 2c 20 75 38 29 3b 0a 0a 53 51 4c 49 54  int, u8);..SQLIT
2e9a0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
2e9b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72  char *sqlite3Btr
2e9c0 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 42 74  eeGetFilename(Bt
2e9d0 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ree *);.SQLITE_P
2e9e0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
2e9f0 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47  r *sqlite3BtreeG
2ea00 65 74 44 69 72 6e 61 6d 65 28 42 74 72 65 65 20  etDirname(Btree 
2ea10 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
2ea20 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
2ea30 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f  qlite3BtreeGetJo
2ea40 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20  urnalname(Btree 
2ea50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
2ea60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
2ea70 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65  reeCopyFile(Btre
2ea80 65 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a  e *, Btree *);..
2ea90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2eaa0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
2eab0 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20  ncrVacuum(Btree 
2eac0 2a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67  *);../* The flag
2ead0 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  s parameter to s
2eae0 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
2eaf0 65 54 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68  eTable can be th
2eb00 65 20 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20  e bitwise OR.** 
2eb10 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2eb20 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69   flags:.*/.#defi
2eb30 6e 65 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20  ne BTREE_INTKEY 
2eb40 20 20 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c      1    /* Tabl
2eb50 65 20 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69  e has only 64-bi
2eb60 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
2eb70 20 6b 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65   keys */.#define
2eb80 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20   BTREE_ZERODATA 
2eb90 20 20 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20    2    /* Table 
2eba0 68 61 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20  has keys only - 
2ebb0 6e 6f 20 64 61 74 61 20 2a 2f 0a 23 64 65 66 69  no data */.#defi
2ebc0 6e 65 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54  ne BTREE_LEAFDAT
2ebd0 41 20 20 20 34 20 20 20 20 2f 2a 20 44 61 74 61  A   4    /* Data
2ebe0 20 73 74 6f 72 65 64 20 69 6e 20 6c 65 61 76 65   stored in leave
2ebf0 73 20 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73  s only.  Implies
2ec00 20 49 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49   INTKEY */..SQLI
2ec10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2ec20 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54  qlite3BtreeDropT
2ec30 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74  able(Btree*, int
2ec40 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
2ec50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2ec60 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62  te3BtreeClearTab
2ec70 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 29 3b  le(Btree*, int);
2ec80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ec90 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
2eca0 47 65 74 4d 65 74 61 28 42 74 72 65 65 2a 2c 20  GetMeta(Btree*, 
2ecb0 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a 70 56  int idx, u32 *pV
2ecc0 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  alue);.SQLITE_PR
2ecd0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2ece0 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
2ecf0 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 69 64 78  (Btree*, int idx
2ed00 2c 20 75 33 32 20 76 61 6c 75 65 29 3b 0a 53 51  , u32 value);.SQ
2ed10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2ed20 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72  d sqlite3BtreeTr
2ed30 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72  ipAllCursors(Btr
2ed40 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49  ee*, int);..SQLI
2ed50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2ed60 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
2ed70 72 28 0a 20 20 42 74 72 65 65 2a 2c 20 20 20 20  r(.  Btree*,    
2ed80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 54 72            /* BTr
2eda0 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61  ee containing ta
2edb0 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20  ble to open */. 
2edc0 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20   int iTable,    
2edd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ede0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2edf0 66 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20  f root page */. 
2ee00 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20   int wrFlag,    
2ee10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 77        /* 1 for w
2ee30 72 69 74 69 6e 67 2e 20 20 30 20 66 6f 72 20 72  riting.  0 for r
2ee40 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 69 6e  ead-only */.  in
2ee50 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  t(*)(void*,int,c
2ee60 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
2ee70 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 20 20 2f 2a  onst void*),  /*
2ee80 20 4b 65 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20   Key comparison 
2ee90 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
2eea0 69 64 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20  id*,            
2eeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eec0 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
2eed0 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61 72 65 20  ment to compare 
2eee0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74  function */.  Bt
2eef0 43 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f  Cursor **ppCurso
2ef00 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
2ef10 20 20 20 2f 2a 20 52 65 74 75 72 6e 65 64 20 63     /* Returned c
2ef20 75 72 73 6f 72 20 2a 2f 0a 29 3b 0a 0a 53 51 4c  ursor */.);..SQL
2ef30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2ef40 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
2ef50 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  eCursor(BtCursor
2ef60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
2ef70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
2ef80 72 65 65 4d 6f 76 65 74 6f 28 42 74 43 75 72 73  reeMoveto(BtCurs
2ef90 6f 72 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 20 2a  or*,const void *
2efa0 70 4b 65 79 2c 69 36 34 20 6e 4b 65 79 2c 69 6e  pKey,i64 nKey,in
2efb0 74 20 62 69 61 73 2c 69 6e 74 20 2a 70 52 65 73  t bias,int *pRes
2efc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
2efd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
2efe0 65 65 44 65 6c 65 74 65 28 42 74 43 75 72 73 6f  eeDelete(BtCurso
2eff0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
2f000 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
2f010 74 72 65 65 49 6e 73 65 72 74 28 42 74 43 75 72  treeInsert(BtCur
2f020 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  sor*, const void
2f030 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79   *pKey, i64 nKey
2f040 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2f050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f060 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a      const void *
2f070 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
2f080 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2f090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f0a0 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 69      int nZero, i
2f0b0 6e 74 20 62 69 61 73 29 3b 0a 53 51 4c 49 54 45  nt bias);.SQLITE
2f0c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2f0d0 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42  ite3BtreeFirst(B
2f0e0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
2f0f0 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Res);.SQLITE_PRI
2f100 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2f110 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 73  BtreeLast(BtCurs
2f120 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b  or*, int *pRes);
2f130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2f140 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
2f150 4e 65 78 74 28 42 74 43 75 72 73 6f 72 2a 2c 20  Next(BtCursor*, 
2f160 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49  int *pRes);.SQLI
2f170 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2f180 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 42  qlite3BtreeEof(B
2f190 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54  tCursor*);.SQLIT
2f1a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2f1b0 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 28  lite3BtreeFlags(
2f1c0 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49  BtCursor*);.SQLI
2f1d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2f1e0 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69  qlite3BtreePrevi
2f1f0 6f 75 73 28 42 74 43 75 72 73 6f 72 2a 2c 20 69  ous(BtCursor*, i
2f200 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54  nt *pRes);.SQLIT
2f210 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2f220 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a  lite3BtreeKeySiz
2f230 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 34  e(BtCursor*, i64
2f240 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45   *pSize);.SQLITE
2f250 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2f260 69 74 65 33 42 74 72 65 65 4b 65 79 28 42 74 43  ite3BtreeKey(BtC
2f270 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73  ursor*, u32 offs
2f280 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69  et, u32 amt, voi
2f290 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
2f2a0 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  ATE sqlite3 *sql
2f2b0 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 44  ite3BtreeCursorD
2f2c0 62 28 63 6f 6e 73 74 20 42 74 43 75 72 73 6f 72  b(const BtCursor
2f2d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
2f2e0 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
2f2f0 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65  qlite3BtreeKeyFe
2f300 74 63 68 28 42 74 43 75 72 73 6f 72 2a 2c 20 69  tch(BtCursor*, i
2f310 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54  nt *pAmt);.SQLIT
2f320 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
2f330 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72  void *sqlite3Btr
2f340 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43 75  eeDataFetch(BtCu
2f350 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74  rsor*, int *pAmt
2f360 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
2f370 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
2f380 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72  eeDataSize(BtCur
2f390 73 6f 72 2a 2c 20 75 33 32 20 2a 70 53 69 7a 65  sor*, u32 *pSize
2f3a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
2f3b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
2f3c0 65 65 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a  eeData(BtCursor*
2f3d0 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33  , u32 offset, u3
2f3e0 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 0a  2 amt, void*);..
2f3f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
2f400 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65  har *sqlite3Btre
2f410 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28  eIntegrityCheck(
2f420 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61 52 6f  Btree*, int *aRo
2f430 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 69  ot, int nRoot, i
2f440 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  nt, int*);.SQLIT
2f450 45 5f 50 52 49 56 41 54 45 20 73 74 72 75 63 74  E_PRIVATE struct
2f460 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42   Pager *sqlite3B
2f470 74 72 65 65 50 61 67 65 72 28 42 74 72 65 65 2a  treePager(Btree*
2f480 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
2f490 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
2f4a0 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72  reePutData(BtCur
2f4b0 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74  sor*, u32 offset
2f4c0 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a  , u32 amt, void*
2f4d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
2f4e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
2f4f0 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77  reeCacheOverflow
2f500 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a 0a 23  (BtCursor *);..#
2f510 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
2f520 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
2f530 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
2f540 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 74 43 75  eCursorInfo(BtCu
2f550 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74  rsor*, int*, int
2f560 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
2f570 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
2f580 72 65 65 43 75 72 73 6f 72 4c 69 73 74 28 42 74  reeCursorList(Bt
2f590 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
2f5a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2f5b0 33 42 74 72 65 65 50 61 67 65 44 75 6d 70 28 42  3BtreePageDump(B
2f5c0 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 20  tree*, int, int 
2f5d0 72 65 63 75 72 73 69 76 65 29 3b 0a 23 65 6e 64  recursive);.#end
2f5e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  if../*.** If we 
2f5f0 61 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68  are not using sh
2f600 61 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e  ared cache, then
2f610 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
2f620 64 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65  d to.** use mute
2f630 78 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  xes to access th
2f640 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  e BtShared struc
2f650 74 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20  tures.  So make 
2f660 74 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64  the.** Enter and
2f670 20 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65   Leave procedure
2f680 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66  s no-ops..*/.#if
2f690 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2f6a0 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
2f6b0 48 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48  HE) && SQLITE_TH
2f6c0 52 45 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f  READSAFE.SQLITE_
2f6d0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
2f6e0 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
2f6f0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
2f700 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
2f710 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
2f720 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  e(Btree*);.SQLIT
2f730 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
2f740 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
2f750 73 4d 75 74 65 78 28 42 74 72 65 65 2a 29 3b 0a  sMutex(Btree*);.
2f760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
2f770 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
2f780 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74  eeEnterCursor(Bt
2f790 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45  Cursor*);.SQLITE
2f7a0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
2f7b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
2f7c0 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  eCursor(BtCursor
2f7d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
2f7e0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
2f7f0 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73  3BtreeEnterAll(s
2f800 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
2f810 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
2f820 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
2f830 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a  eAll(sqlite3*);.
2f840 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
2f850 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
2f860 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
2f870 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
2f880 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
2f890 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75  d sqlite3BtreeMu
2f8a0 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 42 74  texArrayEnter(Bt
2f8b0 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b  reeMutexArray*);
2f8c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2f8d0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74    void sqlite3Bt
2f8e0 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
2f8f0 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  ve(BtreeMutexArr
2f900 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ay*);.SQLITE_PRI
2f910 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
2f920 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
2f930 61 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75  ayInsert(BtreeMu
2f940 74 65 78 41 72 72 61 79 2a 2c 20 42 74 72 65 65  texArray*, Btree
2f950 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  *);.#else.# defi
2f960 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  ne sqlite3BtreeE
2f970 6e 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65  nter(X).# define
2f980 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
2f990 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ve(X).# define s
2f9a0 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
2f9b0 4d 75 74 65 78 28 58 29 20 31 0a 23 20 64 65 66  Mutex(X) 1.# def
2f9c0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
2f9d0 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29 0a 23  EnterCursor(X).#
2f9e0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
2f9f0 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28  treeLeaveCursor(
2fa00 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  X).# define sqli
2fa10 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
2fa20 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (X).# define sql
2fa30 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
2fa40 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71  l(X).# define sq
2fa50 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
2fa60 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23  llMutexes(X) 1.#
2fa70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
2fa80 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e  treeMutexArrayEn
2fa90 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20  ter(X).# define 
2faa0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
2fab0 78 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23  xArrayLeave(X).#
2fac0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
2fad0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e  treeMutexArrayIn
2fae0 73 65 72 74 28 58 2c 59 29 0a 23 65 6e 64 69 66  sert(X,Y).#endif
2faf0 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54  ...#endif /* _BT
2fb00 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  REE_H_ */../****
2fb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2fb20 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a  f btree.h ******
2fb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2fb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
2fb70 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
2fb80 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
2fb90 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
2fba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2fbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
2fbc0 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65  de vdbe.h in the
2fbd0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
2fbe0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
2fbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2fc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2fc10 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a   file vdbe.h ***
2fc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
2fc50 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
2fc60 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
2fc70 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2fc80 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2fc90 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2fca0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
2fcb0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2fcc0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
2fcd0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
2fce0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
2fcf0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
2fd00 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
2fd10 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
2fd20 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
2fd30 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
2fd40 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
2fd50 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
2fd60 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2fd70 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2fd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fdc0 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65  *.** Header file
2fdd0 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c   for the Virtual
2fde0 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65   DataBase Engine
2fdf0 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68   (VDBE).**.** Th
2fe00 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65  is header define
2fe10 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
2fe20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64  to the virtual d
2fe30 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a  atabase engine.*
2fe40 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20  * or VDBE.  The 
2fe50 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20  VDBE implements 
2fe60 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68  an abstract mach
2fe70 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a  ine that runs a.
2fe80 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61  ** simple progra
2fe90 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20  m to access and 
2fea0 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72  modify the under
2feb0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a  lying database..
2fec0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e  **.** $Id: vdbe.
2fed0 68 2c 76 20 31 2e 31 31 33 20 32 30 30 37 2f 30  h,v 1.113 2007/0
2fee0 38 2f 33 30 20 30 31 3a 31 39 3a 35 39 20 64 72  8/30 01:19:59 dr
2fef0 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  h Exp $.*/.#ifnd
2ff00 65 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f  ef _SQLITE_VDBE_
2ff10 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49  H_.#define _SQLI
2ff20 54 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a  TE_VDBE_H_../*.*
2ff30 2a 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20  * A single VDBE 
2ff40 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72  is an opaque str
2ff50 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64  ucture named "Vd
2ff60 62 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69  be".  Only routi
2ff70 6e 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f  nes.** in the so
2ff80 75 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65  urce file sqlite
2ff90 56 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77  Vdbe.c are allow
2ffa0 65 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e  ed to see the in
2ffb0 73 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73  sides.** of this
2ffc0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74   structure..*/.t
2ffd0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64  ypedef struct Vd
2ffe0 62 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20  be Vdbe;../*.** 
2fff0 41 20 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63  A single instruc
30000 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 69 72 74  tion of the virt
30010 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20  ual machine has 
30020 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64  an opcode.** and
30030 20 61 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65   as many as thre
30040 65 20 6f 70 65 72 61 6e 64 73 2e 20 20 54 68 65  e operands.  The
30050 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
30060 72 65 63 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61  recorded.** as a
30070 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
30080 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
30090 63 74 75 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74  cture:.*/.struct
300a0 20 56 64 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f   VdbeOp {.  u8 o
300b0 70 63 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  pcode;          
300c0 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f  /* What operatio
300d0 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a  n to perform */.
300e0 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
300f0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f        /* First o
30100 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
30110 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p2;             
30120 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65  /* Second parame
30130 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a  ter (often the j
30140 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29  ump destination)
30150 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 33 3b 20   */.  char *p3; 
30160 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
30170 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a  rd parameter */.
30180 20 20 69 6e 74 20 70 33 74 79 70 65 3b 20 20 20    int p3type;   
30190 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
301a0 74 68 65 20 50 33 5f 78 78 78 20 63 6f 6e 73 74  the P3_xxx const
301b0 61 6e 74 73 20 64 65 66 69 6e 65 64 20 62 65 6c  ants defined bel
301c0 6f 77 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 42  ow */.#ifdef VDB
301d0 45 5f 50 52 4f 46 49 4c 45 0a 20 20 69 6e 74 20  E_PROFILE.  int 
301e0 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  cnt;            
301f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d  /* Number of tim
30200 65 73 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  es this instruct
30210 69 6f 6e 20 77 61 73 20 65 78 65 63 75 74 65 64  ion was executed
30220 20 2a 2f 0a 20 20 6c 6f 6e 67 20 6c 6f 6e 67 20   */.  long long 
30230 63 79 63 6c 65 73 3b 20 20 20 2f 2a 20 54 6f 74  cycles;   /* Tot
30240 61 6c 20 74 69 6d 65 20 73 70 65 6e 64 20 65 78  al time spend ex
30250 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73  ecuting this ins
30260 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64  truction */.#end
30270 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  if.};.typedef st
30280 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62 65  ruct VdbeOp Vdbe
30290 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d 61  Op;../*.** A sma
302a0 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20  ller version of 
302b0 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72 20  VdbeOp used for 
302c0 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69 73  the VdbeAddOpLis
302d0 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63  t() function bec
302e0 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65 73  ause.** it takes
302f0 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e 0a   up less space..
30300 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70  */.struct VdbeOp
30310 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63 6f  List {.  u8 opco
30320 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  de;          /* 
30330 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74  What operation t
30340 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73  o perform */.  s
30350 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20 20  igned char p1;  
30360 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65 72     /* First oper
30370 61 6e 64 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69  and */.  short i
30380 6e 74 20 70 32 3b 20 20 20 20 20 20 20 2f 2a 20  nt p2;       /* 
30390 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  Second parameter
303a0 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d 70   (often the jump
303b0 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a 2f   destination) */
303c0 0a 20 20 63 68 61 72 20 2a 70 33 3b 20 20 20 20  .  char *p3;    
303d0 20 20 20 20 20 20 20 2f 2a 20 54 68 69 72 64 20         /* Third 
303e0 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b 0a  parameter */.};.
303f0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56  typedef struct V
30400 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f 70  dbeOpList VdbeOp
30410 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  List;../*.** All
30420 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 56  owed values of V
30430 64 62 65 4f 70 2e 70 33 74 79 70 65 0a 2a 2f 0a  dbeOp.p3type.*/.
30440 23 64 65 66 69 6e 65 20 50 33 5f 4e 4f 54 55 53  #define P3_NOTUS
30450 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65  ED    0   /* The
30460 20 50 33 20 70 61 72 61 6d 65 74 65 72 20 69 73   P3 parameter is
30470 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64 65   not used */.#de
30480 66 69 6e 65 20 50 33 5f 44 59 4e 41 4d 49 43 20  fine P3_DYNAMIC 
30490 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74 65   (-1)  /* Pointe
304a0 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f 62  r to a string ob
304b0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
304c0 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64  teMalloc() */.#d
304d0 65 66 69 6e 65 20 50 33 5f 53 54 41 54 49 43 20  efine P3_STATIC 
304e0 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e 74    (-2)  /* Point
304f0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 73  er to a static s
30500 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65  tring */.#define
30510 20 50 33 5f 43 4f 4c 4c 53 45 51 20 20 28 2d 34   P3_COLLSEQ  (-4
30520 29 20 20 2f 2a 20 50 33 20 69 73 20 61 20 70 6f  )  /* P3 is a po
30530 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c 53  inter to a CollS
30540 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  eq structure */.
30550 23 64 65 66 69 6e 65 20 50 33 5f 46 55 4e 43 44  #define P3_FUNCD
30560 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 33 20  EF  (-5)  /* P3 
30570 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
30580 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74  a FuncDef struct
30590 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
305a0 33 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29 20  3_KEYINFO  (-6) 
305b0 20 2f 2a 20 50 33 20 69 73 20 61 20 70 6f 69 6e   /* P3 is a poin
305c0 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  ter to a KeyInfo
305d0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
305e0 65 66 69 6e 65 20 50 33 5f 56 44 42 45 46 55 4e  efine P3_VDBEFUN
305f0 43 20 28 2d 37 29 20 20 2f 2a 20 50 33 20 69 73  C (-7)  /* P3 is
30600 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
30610 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74 75  VdbeFunc structu
30620 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 33  re */.#define P3
30630 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20 20  _MEM      (-8)  
30640 2f 2a 20 50 33 20 69 73 20 61 20 70 6f 69 6e 74  /* P3 is a point
30650 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20 20  er to a Mem*    
30660 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65  structure */.#de
30670 66 69 6e 65 20 50 33 5f 54 52 41 4e 53 49 45 4e  fine P3_TRANSIEN
30680 54 20 28 2d 39 29 20 2f 2a 20 50 33 20 69 73 20  T (-9) /* P3 is 
30690 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 74  a pointer to a t
306a0 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67 20  ransient string 
306b0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 33 5f 56 54  */.#define P3_VT
306c0 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a 20  AB     (-10) /* 
306d0 50 33 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P3 is a pointer 
306e0 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  to an sqlite3_vt
306f0 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  ab structure */.
30700 23 64 65 66 69 6e 65 20 50 33 5f 4d 50 52 49 4e  #define P3_MPRIN
30710 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 33 20  TF  (-11) /* P3 
30720 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74 61  is a string obta
30730 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
30740 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 0a  3_mprintf() */..
30750 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20 61  /* When adding a
30760 20 50 33 20 61 72 67 75 6d 65 6e 74 20 75 73 69   P3 argument usi
30770 6e 67 20 50 33 5f 4b 45 59 49 4e 46 4f 2c 20 61  ng P3_KEYINFO, a
30780 20 63 6f 70 79 20 6f 66 20 74 68 65 20 4b 65 79   copy of the Key
30790 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a  Info structure.*
307a0 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 68 61 74  * is made.  That
307b0 20 63 6f 70 79 20 69 73 20 66 72 65 65 64 20 77   copy is freed w
307c0 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 20  hen the Vdbe is 
307d0 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 75 74 20  finalized.  But 
307e0 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65  if the.** argume
307f0 6e 74 20 69 73 20 50 33 5f 4b 45 59 49 4e 46 4f  nt is P3_KEYINFO
30800 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 20 70 61  _HANDOFF, the pa
30810 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 65 72 20  ssed in pointer 
30820 69 73 20 75 73 65 64 2e 20 20 49 74 20 73 74 69  is used.  It sti
30830 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 65 65 64  ll.** gets freed
30840 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69   when the Vdbe i
30850 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 6f 20 69  s finalized so i
30860 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c 64 20 62  t still should b
30870 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  e obtained.** fr
30880 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69  om a single sqli
30890 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 42 75 74  teMalloc().  But
308a0 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d 61 64 65   no copy is made
308b0 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67   and the calling
308c0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  .** function sho
308d0 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 20 74 6f  uld *not* try to
308e0 20 66 72 65 65 20 74 68 65 20 4b 65 79 49 6e 66   free the KeyInf
308f0 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 33  o..*/.#define P3
30900 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
30910 20 28 2d 39 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   (-9)../*.** The
30920 20 56 64 62 65 2e 61 43 6f 6c 4e 61 6d 65 20 61   Vdbe.aColName a
30930 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 35 6e  rray contains 5n
30940 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 73 2c   Mem structures,
30950 20 77 68 65 72 65 20 6e 20 69 73 20 74 68 65 20   where n is the 
30960 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  .** number of co
30970 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 72 65  lumns of data re
30980 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 73 74  turned by the st
30990 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66  atement..*/.#def
309a0 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45  ine COLNAME_NAME
309b0 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43       0.#define C
309c0 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 20  OLNAME_DECLTYPE 
309d0 31 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  1.#define COLNAM
309e0 45 5f 44 41 54 41 42 41 53 45 20 32 0a 23 64 65  E_DATABASE 2.#de
309f0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 54 41 42  fine COLNAME_TAB
30a00 4c 45 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20  LE    3.#define 
30a10 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 20 20  COLNAME_COLUMN  
30a20 20 34 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41   4.#define COLNA
30a30 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20  ME_N        5   
30a40 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
30a50 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62  COLNAME_xxx symb
30a60 6f 6c 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ols */../*.** Th
30a70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
30a80 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72 65 6c  o converts a rel
30a90 61 74 69 76 65 20 61 64 64 72 65 73 73 20 69 6e  ative address in
30aa0 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a 2a 2a   the p2 field.**
30ab0 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 74 72   of a VdbeOp str
30ac0 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 6e 65  ucture into a ne
30ad0 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 73 6f  gative number so
30ae0 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 74 65   that .** sqlite
30af0 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 29  3VdbeAddOpList()
30b00 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 65 20   knows that the 
30b10 61 64 64 72 65 73 73 20 69 73 20 72 65 6c 61 74  address is relat
30b20 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a 2a 2a  ive.  Calling.**
30b30 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61 69 6e   the macro again
30b40 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 61 64   restores the ad
30b50 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  dress..*/.#defin
30b60 65 20 41 44 44 52 28 58 29 20 20 28 2d 31 2d 28  e ADDR(X)  (-1-(
30b70 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  X))../*.** The m
30b80 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 74 68  akefile scans th
30b90 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20  e vdbe.c source 
30ba0 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74 65 73  file and creates
30bb0 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e 68 22   the "opcodes.h"
30bc0 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65 20  .** header file 
30bd0 74 68 61 74 20 64 65 66 69 6e 65 73 20 61 20 6e  that defines a n
30be0 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68 20 6f  umber for each o
30bf0 70 63 6f 64 65 20 75 73 65 64 20 62 79 20 74 68  pcode used by th
30c00 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  e VDBE..*/./****
30c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
30c20 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 6e 20  de opcodes.h in 
30c30 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 64  the middle of vd
30c40 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.h ***********
30c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
30c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
30c70 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e 68 20   file opcodes.h 
30c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75  *********/./* Au
30cb0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
30cc0 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 65  rated.  Do not e
30cd0 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 74 68  dit */./* See th
30ce0 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20  e mkopcodeh.awk 
30cf0 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 61 69  script for detai
30d00 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  ls */.#define OP
30d10 5f 4d 65 6d 4c 6f 61 64 20 20 20 20 20 20 20 20  _MemLoad        
30d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d30 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
30d40 4f 50 5f 56 4e 65 78 74 20 20 20 20 20 20 20 20  OP_VNext        
30d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d60 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
30d70 65 20 4f 50 5f 48 65 78 42 6c 6f 62 20 20 20 20  e OP_HexBlob    
30d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d90 20 20 20 20 20 20 20 20 31 32 36 20 20 20 2f 2a          126   /*
30da0 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 4c 4f 42   same as TK_BLOB
30db0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
30dc0 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20  OP_Column       
30dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30de0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e          3.#defin
30df0 65 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 20 20  e OP_SetCookie  
30e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e10 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66            4.#def
30e20 69 6e 65 20 4f 50 5f 49 66 4d 65 6d 50 6f 73 20  ine OP_IfMemPos 
30e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e40 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64              5.#d
30e50 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20 20 20  efine OP_Real   
30e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e70 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35 20              125 
30e80 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
30e90 46 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64 65 66  FLOAT    */.#def
30ea0 69 6e 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 20  ine OP_Sequence 
30eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ec0 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64              6.#d
30ed0 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 47 74 20  efine OP_MoveGt 
30ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a                7.
30f00 23 64 65 66 69 6e 65 20 4f 50 5f 47 65 20 20 20  #define OP_Ge   
30f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
30f30 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  2   /* same as T
30f40 4b 5f 47 45 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_GE       */.#d
30f50 65 66 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 79 20  efine OP_RowKey 
30f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a                8.
30f80 23 64 65 66 69 6e 65 20 4f 50 5f 45 71 20 20 20  #define OP_Eq   
30f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
30fb0 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  8   /* same as T
30fc0 4b 5f 45 51 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_EQ       */.#d
30fd0 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 57 72 69  efine OP_OpenWri
30fe0 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
30ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 0a                9.
31000 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75  #define OP_NotNu
31010 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
31020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
31030 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  6   /* same as T
31040 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64  K_NOTNULL  */.#d
31050 65 66 69 6e 65 20 4f 50 5f 49 66 20 20 20 20 20  efine OP_If     
31060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31070 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 0a               10.
31080 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 49 6e 74  #define OP_ToInt
31090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
310a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
310b0 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  1   /* same as T
310c0 4b 5f 54 4f 5f 49 4e 54 20 20 20 2a 2f 0a 23 64  K_TO_INT   */.#d
310d0 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 38  efine OP_String8
310e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
310f0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 20               88 
31100 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
31110 53 54 52 49 4e 47 20 20 20 2a 2f 0a 23 64 65 66  STRING   */.#def
31120 69 6e 65 20 4f 50 5f 50 6f 70 20 20 20 20 20 20  ine OP_Pop      
31130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31140 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64             11.#d
31150 65 66 69 6e 65 20 4f 50 5f 56 52 6f 77 69 64 20  efine OP_VRowid 
31160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31170 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 0a               12.
31180 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c 53  #define OP_CollS
31190 65 71 20 20 20 20 20 20 20 20 20 20 20 20 20 20  eq              
311a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
311b0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65  3.#define OP_Ope
311c0 6e 52 65 61 64 20 20 20 20 20 20 20 20 20 20 20  nRead           
311d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
311e0 20 31 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45   14.#define OP_E
311f0 78 70 69 72 65 20 20 20 20 20 20 20 20 20 20 20  xpire           
31200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31210 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 4f 50     15.#define OP
31220 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20 20 20  _AutoCommit     
31230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31240 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 20       17.#define 
31250 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20 20 20  OP_Gt           
31260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31270 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a 20 73         69   /* s
31280 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20 20 20  ame as TK_GT    
31290 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
312a0 5f 49 6e 74 65 67 72 69 74 79 43 6b 20 20 20 20  _IntegrityCk    
312b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
312c0 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e 65 20       18.#define 
312d0 4f 50 5f 53 6f 72 74 20 20 20 20 20 20 20 20 20  OP_Sort         
312e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
312f0 20 20 20 20 20 20 20 31 39 0a 23 64 65 66 69 6e         19.#defin
31300 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20  e OP_Function   
31310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31320 20 20 20 20 20 20 20 20 20 32 30 0a 23 64 65 66           20.#def
31330 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20  ine OP_And      
31340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31350 20 20 20 20 20 20 20 20 20 20 20 36 31 20 20 20             61   
31360 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e  /* same as TK_AN
31370 44 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  D      */.#defin
31380 65 20 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20  e OP_Subtract   
31390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
313a0 20 20 20 20 20 20 20 20 20 37 39 20 20 20 2f 2a           79   /*
313b0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55   same as TK_MINU
313c0 53 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  S    */.#define 
313d0 4f 50 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20  OP_Noop         
313e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
313f0 20 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e         21.#defin
31400 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20  e OP_Return     
31410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31420 20 20 20 20 20 20 20 20 20 32 32 0a 23 64 65 66           22.#def
31430 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72  ine OP_Remainder
31440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31450 20 20 20 20 20 20 20 20 20 20 20 38 32 20 20 20             82   
31460 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45  /* same as TK_RE
31470 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  M      */.#defin
31480 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20  e OP_NewRowid   
31490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
314a0 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66           23.#def
314b0 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20  ine OP_Multiply 
314c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
314d0 20 20 20 20 20 20 20 20 20 20 20 38 30 20 20 20             80   
314e0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54  /* same as TK_ST
314f0 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  AR     */.#defin
31500 65 20 4f 50 5f 49 66 4d 65 6d 4e 65 67 20 20 20  e OP_IfMemNeg   
31510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31520 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66           24.#def
31530 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20  ine OP_Variable 
31540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31550 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 23 64             25.#d
31560 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20  efine OP_String 
31570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31580 20 20 20 20 20 20 20 20 20 20 20 20 20 32 36 0a               26.
31590 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41  #define OP_RealA
315a0 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20  ffinity         
315b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
315c0 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65  7.#define OP_VRe
315d0 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
315e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
315f0 20 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50   28.#define OP_P
31600 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20  arseSchema      
31610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31620 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20 4f 50     29.#define OP
31630 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20  _VOpen          
31640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31650 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20       30.#define 
31660 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20  OP_Close        
31670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31680 20 20 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e         31.#defin
31690 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78  e OP_CreateIndex
316a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316b0 20 20 20 20 20 20 20 20 20 33 32 0a 23 64 65 66           32.#def
316c0 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20  ine OP_IsUnique 
316d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316e0 20 20 20 20 20 20 20 20 20 20 20 33 33 0a 23 64             33.#d
316f0 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e  efine OP_NotFoun
31700 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
31710 20 20 20 20 20 20 20 20 20 20 20 20 20 33 34 0a               34.
31720 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34  #define OP_Int64
31730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
31750 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73  5.#define OP_Mus
31760 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 20  tBeInt          
31770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31780 20 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48   36.#define OP_H
31790 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  alt             
317a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
317b0 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 4f 50     37.#define OP
317c0 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  _Rowid          
317d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
317e0 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20       38.#define 
317f0 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20  OP_IdxLT        
31800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31810 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69 6e         39.#defin
31820 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20  e OP_AddImm     
31830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31840 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 66           40.#def
31850 69 6e 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74  ine OP_Statement
31860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31870 20 20 20 20 20 20 20 20 20 20 20 34 31 0a 23 64             41.#d
31880 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61  efine OP_RowData
31890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318a0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 32 0a               42.
318b0 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61  #define OP_MemMa
318c0 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
318d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
318e0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 75 73  3.#define OP_Pus
318f0 68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h               
31900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31910 20 34 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f   44.#define OP_O
31920 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
31930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31940 20 20 20 36 30 20 20 20 2f 2a 20 73 61 6d 65 20     60   /* same 
31950 61 73 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 2a  as TK_OR       *
31960 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  /.#define OP_Not
31970 45 78 69 73 74 73 20 20 20 20 20 20 20 20 20 20  Exists          
31980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31990 20 34 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d   45.#define OP_M
319a0 65 6d 49 6e 63 72 20 20 20 20 20 20 20 20 20 20  emIncr          
319b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
319c0 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20 4f 50     46.#define OP
319d0 5f 47 6f 73 75 62 20 20 20 20 20 20 20 20 20 20  _Gosub          
319e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
319f0 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20       47.#define 
31a00 4f 50 5f 44 69 76 69 64 65 20 20 20 20 20 20 20  OP_Divide       
31a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31a20 20 20 20 20 20 20 20 38 31 20 20 20 2f 2a 20 73         81   /* s
31a30 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 20  ame as TK_SLASH 
31a40 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
31a50 5f 49 6e 74 65 67 65 72 20 20 20 20 20 20 20 20  _Integer        
31a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31a70 20 20 20 20 20 34 38 0a 23 64 65 66 69 6e 65 20       48.#define 
31a80 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 20 20 20  OP_ToNumeric    
31a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31aa0 20 20 20 20 20 20 31 34 30 20 20 20 2f 2a 20 73        140   /* s
31ab0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d  ame as TK_TO_NUM
31ac0 45 52 49 43 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ERIC*/.#define O
31ad0 50 5f 4d 65 6d 49 6e 74 20 20 20 20 20 20 20 20  P_MemInt        
31ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31af0 20 20 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65        49.#define
31b00 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 20 20   OP_Prev        
31b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31b20 20 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69          50.#defi
31b30 6e 65 20 4f 50 5f 43 6f 6e 63 61 74 20 20 20 20  ne OP_Concat    
31b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31b50 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f            83   /
31b60 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e  * same as TK_CON
31b70 43 41 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  CAT   */.#define
31b80 20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20 20 20   OP_BitAnd      
31b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ba0 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a 20          74   /* 
31bb0 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 4e  same as TK_BITAN
31bc0 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  D   */.#define O
31bd0 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20  P_VColumn       
31be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31bf0 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65        51.#define
31c00 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20   OP_CreateTable 
31c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c20 20 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 69          52.#defi
31c30 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 20  ne OP_Last      
31c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c50 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65            53.#de
31c60 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20  fine OP_IsNull  
31c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c80 20 20 20 20 20 20 20 20 20 20 20 20 36 35 20 20              65  
31c90 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49   /* same as TK_I
31ca0 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69  SNULL   */.#defi
31cb0 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d  ne OP_IncrVacuum
31cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31cd0 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64 65            54.#de
31ce0 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64  fine OP_IdxRowid
31cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31d00 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 23              55.#
31d10 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 49 64  define OP_MakeId
31d20 78 52 65 63 20 20 20 20 20 20 20 20 20 20 20 20  xRec            
31d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 36                56
31d40 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66  .#define OP_Shif
31d50 74 52 69 67 68 74 20 20 20 20 20 20 20 20 20 20  tRight          
31d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31d70 37 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  77   /* same as 
31d80 54 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23  TK_RSHIFT   */.#
31d90 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43  define OP_ResetC
31da0 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  ount            
31db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37                57
31dc0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f  .#define OP_Fifo
31dd0 57 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20  Write           
31de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31df0 35 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 61  58.#define OP_Ca
31e00 6c 6c 62 61 63 6b 20 20 20 20 20 20 20 20 20 20  llback          
31e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e20 20 20 35 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f    59.#define OP_
31e30 43 6f 6e 74 65 78 74 50 75 73 68 20 20 20 20 20  ContextPush     
31e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e50 20 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f      62.#define O
31e60 50 5f 44 72 6f 70 54 72 69 67 67 65 72 20 20 20  P_DropTrigger   
31e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e80 20 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65        63.#define
31e90 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20   OP_DropIndex   
31ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31eb0 20 20 20 20 20 20 20 20 36 34 0a 23 64 65 66 69          64.#defi
31ec0 6e 65 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20  ne OP_IdxGE     
31ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ee0 20 20 20 20 20 20 20 20 20 20 37 33 0a 23 64 65            73.#de
31ef0 66 69 6e 65 20 4f 50 5f 49 64 78 44 65 6c 65 74  fine OP_IdxDelet
31f00 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
31f10 20 20 20 20 20 20 20 20 20 20 20 20 38 34 0a 23              84.#
31f20 64 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d  define OP_Vacuum
31f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36                86
31f50 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65  .#define OP_Move
31f60 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Le              
31f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f80 38 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66  89.#define OP_If
31f90 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Not             
31fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fb0 20 20 39 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f    90.#define OP_
31fc0 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20  DropTable       
31fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fe0 20 20 20 20 39 31 0a 23 64 65 66 69 6e 65 20 4f      91.#define O
31ff0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20  P_MakeRecord    
32000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32010 20 20 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65        92.#define
32020 20 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20   OP_ToBlob      
32030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32040 20 20 20 20 20 20 20 31 33 39 20 20 20 2f 2a 20         139   /* 
32050 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c  same as TK_TO_BL
32060 4f 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  OB  */.#define O
32070 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20 20  P_Delete        
32080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32090 20 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65        93.#define
320a0 20 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20   OP_AggFinal    
320b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
320c0 20 20 20 20 20 20 20 20 39 34 0a 23 64 65 66 69          94.#defi
320d0 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20  ne OP_ShiftLeft 
320e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
320f0 20 20 20 20 20 20 20 20 20 20 37 36 20 20 20 2f            76   /
32100 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48  * same as TK_LSH
32110 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  IFT   */.#define
32120 20 4f 50 5f 44 75 70 20 20 20 20 20 20 20 20 20   OP_Dup         
32130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32140 20 20 20 20 20 20 20 20 39 35 0a 23 64 65 66 69          95.#defi
32150 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20  ne OP_Goto      
32160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32170 20 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 65            96.#de
32180 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63  fine OP_TableLoc
32190 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k               
321a0 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a 23              97.#
321b0 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f 52 65  define OP_FifoRe
321c0 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ad              
321d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 38                98
321e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 65 61  .#define OP_Clea
321f0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
32200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32210 39 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64  99.#define OP_Id
32220 78 47 54 20 20 20 20 20 20 20 20 20 20 20 20 20  xGT             
32230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32240 20 31 30 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f   100.#define OP_
32250 4d 6f 76 65 4c 74 20 20 20 20 20 20 20 20 20 20  MoveLt          
32260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32270 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 4f     101.#define O
32280 50 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20 20  P_Le            
32290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322a0 20 20 20 20 20 20 37 30 20 20 20 2f 2a 20 73 61        70   /* sa
322b0 6d 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20 20  me as TK_LE     
322c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
322d0 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20  VerifyCookie    
322e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322f0 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f     102.#define O
32300 50 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20  P_AggStep       
32310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32320 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65       103.#define
32330 20 4f 50 5f 50 75 6c 6c 20 20 20 20 20 20 20 20   OP_Pull        
32340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32350 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 66 69         104.#defi
32360 6e 65 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20  ne OP_ToText    
32370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32380 20 20 20 20 20 20 20 20 20 31 33 38 20 20 20 2f           138   /
32390 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
323a0 54 45 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65  TEXT  */.#define
323b0 20 4f 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20   OP_Not         
323c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
323d0 20 20 20 20 20 20 20 20 31 36 20 20 20 2f 2a 20          16   /* 
323e0 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20  same as TK_NOT  
323f0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
32400 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20 20 20  P_ToReal        
32410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32420 20 20 20 20 20 31 34 32 20 20 20 2f 2a 20 73 61       142   /* sa
32430 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c  me as TK_TO_REAL
32440 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
32450 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 20 20  SetNumColumns   
32460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32470 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65 20 4f     105.#define O
32480 50 5f 41 62 73 56 61 6c 75 65 20 20 20 20 20 20  P_AbsValue      
32490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
324a0 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65       106.#define
324b0 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20   OP_Transaction 
324c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
324d0 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 69         107.#defi
324e0 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20  ne OP_VFilter   
324f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32500 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 65           108.#de
32510 66 69 6e 65 20 4f 50 5f 4e 65 67 61 74 69 76 65  fine OP_Negative
32520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32530 20 20 20 20 20 20 20 20 20 20 20 20 38 35 20 20              85  
32540 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 55   /* same as TK_U
32550 4d 49 4e 55 53 20 20 20 2a 2f 0a 23 64 65 66 69  MINUS   */.#defi
32560 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20 20 20  ne OP_Ne        
32570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32580 20 20 20 20 20 20 20 20 20 20 36 37 20 20 20 2f            67   /
32590 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20  * same as TK_NE 
325a0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
325b0 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 20 20   OP_VDestroy    
325c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325d0 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 66 69         109.#defi
325e0 6e 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70  ne OP_ContextPop
325f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32600 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64 65           110.#de
32610 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20  fine OP_BitOr   
32620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32630 20 20 20 20 20 20 20 20 20 20 20 20 37 35 20 20              75  
32640 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42   /* same as TK_B
32650 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69  ITOR    */.#defi
32660 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20  ne OP_Next      
32670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32680 20 20 20 20 20 20 20 20 20 31 31 31 0a 23 64 65           111.#de
32690 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65 72  fine OP_IdxInser
326a0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
326b0 20 20 20 20 20 20 20 20 20 20 20 31 31 32 0a 23             112.#
326c0 64 65 66 69 6e 65 20 4f 50 5f 44 69 73 74 69 6e  define OP_Distin
326d0 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ct              
326e0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 33               113
326f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20  .#define OP_Lt  
32700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32720 37 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  71   /* same as 
32730 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23  TK_LT       */.#
32740 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74  define OP_Insert
32750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32760 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 34               114
32770 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74  .#define OP_Dest
32780 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20  roy             
32790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
327a0 31 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65  15.#define OP_Re
327b0 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20  adCookie        
327c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
327d0 20 31 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f   116.#define OP_
327e0 46 6f 72 63 65 49 6e 74 20 20 20 20 20 20 20 20  ForceInt        
327f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32800 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 4f     117.#define O
32810 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20  P_LoadAnalysis  
32820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32830 20 20 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65       118.#define
32840 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20 20 20   OP_Explain     
32850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32860 20 20 20 20 20 20 20 31 31 39 0a 23 64 65 66 69         119.#defi
32870 6e 65 20 4f 50 5f 49 66 4d 65 6d 5a 65 72 6f 20  ne OP_IfMemZero 
32880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32890 20 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65           120.#de
328a0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75  fine OP_OpenPseu
328b0 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  do              
328c0 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 23             121.#
328d0 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45 70  define OP_OpenEp
328e0 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20 20  hemeral         
328f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 32               122
32900 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c  .#define OP_Null
32910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
32930 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 6c  23.#define OP_Bl
32940 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ob              
32950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32960 20 31 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f   124.#define OP_
32970 41 64 64 20 20 20 20 20 20 20 20 20 20 20 20 20  Add             
32980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32990 20 20 20 20 37 38 20 20 20 2f 2a 20 73 61 6d 65      78   /* same
329a0 20 61 73 20 54 4b 5f 50 4c 55 53 20 20 20 20 20   as TK_PLUS     
329b0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65  */.#define OP_Me
329c0 6d 53 74 6f 72 65 20 20 20 20 20 20 20 20 20 20  mStore          
329d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329e0 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f   127.#define OP_
329f0 52 65 77 69 6e 64 20 20 20 20 20 20 20 20 20 20  Rewind          
32a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a10 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f     128.#define O
32a20 50 5f 4d 6f 76 65 47 65 20 20 20 20 20 20 20 20  P_MoveGe        
32a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a40 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65       129.#define
32a50 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 20 20 20   OP_VBegin      
32a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a70 20 20 20 20 20 20 20 31 33 30 0a 23 64 65 66 69         130.#defi
32a80 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20 20 20  ne OP_VUpdate   
32a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32aa0 20 20 20 20 20 20 20 20 20 31 33 31 0a 23 64 65           131.#de
32ab0 66 69 6e 65 20 4f 50 5f 42 69 74 4e 6f 74 20 20  fine OP_BitNot  
32ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ad0 20 20 20 20 20 20 20 20 20 20 20 20 38 37 20 20              87  
32ae0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42   /* same as TK_B
32af0 49 54 4e 4f 54 20 20 20 2a 2f 0a 23 64 65 66 69  ITNOT   */.#defi
32b00 6e 65 20 4f 50 5f 56 43 72 65 61 74 65 20 20 20  ne OP_VCreate   
32b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b20 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65           132.#de
32b30 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 6f 76 65 20  fine OP_MemMove 
32b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b50 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23             133.#
32b60 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4e 75 6c  define OP_MemNul
32b70 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
32b80 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34               134
32b90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 6e  .#define OP_Foun
32ba0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
32bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
32bc0 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75  35.#define OP_Nu
32bd0 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20 20 20  llRow           
32be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bf0 20 31 33 36 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c   136../* The fol
32c00 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 65 20 76 61  lowing opcode va
32c10 6c 75 65 73 20 61 72 65 20 6e 65 76 65 72 20 75  lues are never u
32c20 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  sed */.#define O
32c30 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20  P_NotUsed_137   
32c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c50 20 20 20 20 20 31 33 37 0a 0a 2f 2a 20 4f 70 63       137../* Opc
32c60 6f 64 65 73 20 74 68 61 74 20 61 72 65 20 67 75  odes that are gu
32c70 61 72 61 6e 74 65 65 64 20 74 6f 20 6e 65 76 65  aranteed to neve
32c80 72 20 70 75 73 68 20 61 20 76 61 6c 75 65 20 6f  r push a value o
32c90 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a  nto the stack.**
32ca0 20 63 6f 6e 74 61 69 6e 20 61 20 31 20 74 68 65   contain a 1 the
32cb0 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ir corresponding
32cc0 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65   position of the
32cd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 73 6b 0a   following mask.
32ce0 2a 2a 20 73 65 74 2e 20 20 53 65 65 20 74 68 65  ** set.  See the
32cf0 20 6f 70 63 6f 64 65 4e 6f 50 75 73 68 28 29 20   opcodeNoPush() 
32d00 66 75 6e 63 74 69 6f 6e 20 69 6e 20 76 64 62 65  function in vdbe
32d10 61 75 78 2e 63 20 20 2a 2f 0a 23 64 65 66 69 6e  aux.c  */.#defin
32d20 65 20 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 30 20  e NOPUSH_MASK_0 
32d30 30 78 65 65 62 34 0a 23 64 65 66 69 6e 65 20 4e  0xeeb4.#define N
32d40 4f 50 55 53 48 5f 4d 41 53 4b 5f 31 20 30 78 66  OPUSH_MASK_1 0xf
32d50 39 36 62 0a 23 64 65 66 69 6e 65 20 4e 4f 50 55  96b.#define NOPU
32d60 53 48 5f 4d 41 53 4b 5f 32 20 30 78 66 62 62 36  SH_MASK_2 0xfbb6
32d70 0a 23 64 65 66 69 6e 65 20 4e 4f 50 55 53 48 5f  .#define NOPUSH_
32d80 4d 41 53 4b 5f 33 20 30 78 66 65 36 34 0a 23 64  MASK_3 0xfe64.#d
32d90 65 66 69 6e 65 20 4e 4f 50 55 53 48 5f 4d 41 53  efine NOPUSH_MAS
32da0 4b 5f 34 20 30 78 66 66 66 66 0a 23 64 65 66 69  K_4 0xffff.#defi
32db0 6e 65 20 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 35  ne NOPUSH_MASK_5
32dc0 20 30 78 36 65 66 37 0a 23 64 65 66 69 6e 65 20   0x6ef7.#define 
32dd0 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 36 20 30 78  NOPUSH_MASK_6 0x
32de0 66 62 66 62 0a 23 64 65 66 69 6e 65 20 4e 4f 50  fbfb.#define NOP
32df0 55 53 48 5f 4d 41 53 4b 5f 37 20 30 78 38 37 36  USH_MASK_7 0x876
32e00 37 0a 23 64 65 66 69 6e 65 20 4e 4f 50 55 53 48  7.#define NOPUSH
32e10 5f 4d 41 53 4b 5f 38 20 30 78 37 64 39 66 0a 23  _MASK_8 0x7d9f.#
32e20 64 65 66 69 6e 65 20 4e 4f 50 55 53 48 5f 4d 41  define NOPUSH_MA
32e30 53 4b 5f 39 20 30 78 30 30 30 30 0a 0a 2f 2a 2a  SK_9 0x0000../**
32e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
32e50 20 6f 66 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a   of opcodes.h **
32e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
32e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
32ea0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
32eb0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62   left off in vdb
32ec0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
32ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
32ee0 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 65 73 20 66  .** Prototypes f
32ef0 6f 72 20 74 68 65 20 56 44 42 45 20 69 6e 74 65  or the VDBE inte
32f00 72 66 61 63 65 2e 20 20 53 65 65 20 63 6f 6d 6d  rface.  See comm
32f10 65 6e 74 73 20 6f 6e 20 74 68 65 20 69 6d 70 6c  ents on the impl
32f20 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 6f  ementation.** fo
32f30 72 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20  r a description 
32f40 6f 66 20 77 68 61 74 20 65 61 63 68 20 6f 66 20  of what each of 
32f50 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64  these routines d
32f60 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  oes..*/.SQLITE_P
32f70 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c  RIVATE Vdbe *sql
32f80 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73  ite3VdbeCreate(s
32f90 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
32fa0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
32fb0 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 56 64  ite3VdbeAddOp(Vd
32fc0 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29  be*,int,int,int)
32fd0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
32fe0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
32ff0 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op3(Vdbe*,int,in
33000 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  t,int,const char
33010 20 2a 7a 50 33 2c 69 6e 74 29 3b 0a 53 51 4c 49   *zP3,int);.SQLI
33020 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
33030 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
33040 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 6e  ist(Vdbe*, int n
33050 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63  Op, VdbeOpList c
33060 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c 49  onst *aOp);.SQLI
33070 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
33080 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
33090 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 61  eP1(Vdbe*, int a
330a0 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 51  ddr, int P1);.SQ
330b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
330c0 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
330d0 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e 74  ngeP2(Vdbe*, int
330e0 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b 0a   addr, int P2);.
330f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
33100 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4a  oid sqlite3VdbeJ
33110 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c 20 69  umpHere(Vdbe*, i
33120 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 54 45  nt addr);.SQLITE
33130 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
33140 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54  lite3VdbeChangeT
33150 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69 6e 74  oNoop(Vdbe*, int
33160 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b 0a 53   addr, int N);.S
33170 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
33180 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
33190 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 69 6e  angeP3(Vdbe*, in
331a0 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 68  t addr, const ch
331b0 61 72 20 2a 7a 50 31 2c 20 69 6e 74 20 4e 29 3b  ar *zP1, int N);
331c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
331d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
331e0 55 73 65 73 42 74 72 65 65 28 56 64 62 65 2a 2c  UsesBtree(Vdbe*,
331f0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
33200 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71  IVATE VdbeOp *sq
33210 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 56  lite3VdbeGetOp(V
33220 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  dbe*, int);.SQLI
33230 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
33240 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
33250 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  bel(Vdbe*);.SQLI
33260 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
33270 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
33280 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  e(Vdbe*);.SQLITE
33290 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
332a0 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
332b0 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74  dy(Vdbe*,int,int
332c0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
332d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
332e0 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
332f0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  e(Vdbe*);.SQLITE
33300 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
33310 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
33320 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 69 6e 74  Label(Vdbe*, int
33330 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
33340 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
33350 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 64 62  eCurrentAddr(Vdb
33360 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  e*);.#ifdef SQLI
33370 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
33380 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
33390 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28  qlite3VdbeTrace(
333a0 56 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65  Vdbe*,FILE*);.#e
333b0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
333c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
333d0 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73  VdbeResetStepRes
333e0 75 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  ult(Vdbe*);.SQLI
333f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
33400 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28  qlite3VdbeReset(
33410 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
33420 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
33430 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
33440 73 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51  s(Vdbe*,int);.SQ
33450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
33460 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
33470 6f 6c 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e  olName(Vdbe*, in
33480 74 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  t, int, const ch
33490 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ar *, int);.SQLI
334a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
334b0 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74  sqlite3VdbeCount
334c0 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b 0a  Changes(Vdbe*);.
334d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
334e0 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56  qlite3 *sqlite3V
334f0 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53 51  dbeDb(Vdbe*);.SQ
33500 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
33510 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
33520 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74  Sql(Vdbe*, const
33530 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
33540 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
33550 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
33560 69 74 65 33 56 64 62 65 47 65 74 53 71 6c 28 56  ite3VdbeGetSql(V
33570 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
33580 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
33590 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 2a  e3VdbeSwap(Vdbe*
335a0 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66 6e 64 65  ,Vdbe*);..#ifnde
335b0 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  f NDEBUG.SQLITE_
335c0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
335d0 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e  qlite3VdbeCommen
335e0 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63  t(Vdbe*, const c
335f0 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65  har*, ...);.# de
33600 66 69 6e 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74  fine VdbeComment
33610 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65  (X)  sqlite3Vdbe
33620 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c 73 65 0a  Comment X.#else.
33630 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 6f 6d  # define VdbeCom
33640 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a  ment(X).#endif..
33650 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
33660 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
33670 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.h **********
33680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
336a0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
336b0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
336c0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
336d0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
336e0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
336f0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
33700 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
33710 70 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d  pager.h in the m
33720 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
33730 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
33740 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
33750 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
33760 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a  le pager.h *****
33770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33790 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
337a0 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
337b0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
337c0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
337d0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
337e0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
337f0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
33800 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
33810 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
33820 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
33830 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
33840 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
33850 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
33860 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
33870 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
33880 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
33890 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
338a0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
338b0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
338c0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
338d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
33910 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
33920 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  le defines the i
33930 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68  nterface that th
33940 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61  e sqlite page ca
33950 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d  che.** subsystem
33960 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68  .  The page cach
33970 65 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64  e subsystem read
33980 73 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66  s and writes a f
33990 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74  ile a page.** at
339a0 20 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76   a time and prov
339b0 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66  ides a journal f
339c0 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  or rollback..**.
339d0 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67  ** @(#) $Id: pag
339e0 65 72 2e 68 2c 76 20 31 2e 36 37 20 32 30 30 37  er.h,v 1.67 2007
339f0 2f 30 39 2f 30 33 20 31 35 3a 31 39 3a 33 35 20  /09/03 15:19:35 
33a00 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  drh Exp $.*/..#i
33a10 66 6e 64 65 66 20 5f 50 41 47 45 52 5f 48 5f 0a  fndef _PAGER_H_.
33a20 23 64 65 66 69 6e 65 20 5f 50 41 47 45 52 5f 48  #define _PAGER_H
33a30 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70  _../*.** The typ
33a40 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  e used to repres
33a50 65 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65  ent a page numbe
33a60 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61  r.  The first pa
33a70 67 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20  ge in a file.** 
33a80 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31  is called page 1
33a90 2e 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20  .  0 is used to 
33aa0 72 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61  represent "not a
33ab0 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64   page"..*/.typed
33ac0 65 66 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  ef unsigned int 
33ad0 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  Pgno;../*.** Eac
33ae0 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20 6d  h open file is m
33af0 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70 61  anaged by a sepa
33b00 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f 66  rate instance of
33b10 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74 72   the "Pager" str
33b20 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64  ucture..*/.typed
33b30 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 20  ef struct Pager 
33b40 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48 61  Pager;../*.** Ha
33b50 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70 61  ndle type for pa
33b60 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ges..*/.typedef 
33b70 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62 50  struct PgHdr DbP
33b80 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  age;../*.** Allo
33b90 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74  wed values for t
33ba0 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
33bb0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67  er to sqlite3Pag
33bc0 65 72 4f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20  erOpen()..**.** 
33bd0 4e 4f 54 45 3a 20 54 68 69 73 20 76 61 6c 75 65  NOTE: This value
33be0 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65  s must match the
33bf0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42   corresponding B
33c00 54 52 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20  TREE_ values in 
33c10 62 74 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66  btree.h..*/.#def
33c20 69 6e 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a  ine PAGER_OMIT_J
33c30 4f 55 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20  OURNAL  0x0001  
33c40 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20    /* Do not use 
33c50 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
33c60 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  al */.#define PA
33c70 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20  GER_NO_READLOCK 
33c80 20 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f    0x0002    /* O
33c90 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e  mit readlocks on
33ca0 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20   readonly files 
33cb0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20  */../*.** Valid 
33cc0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73  values for the s
33cd0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
33ce0 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f  o sqlite3PagerLo
33cf0 63 6b 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a  ckingMode()..*/.
33d00 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f  #define PAGER_LO
33d10 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20  CKINGMODE_QUERY 
33d20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20       -1.#define 
33d30 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
33d40 45 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a  E_NORMAL      0.
33d50 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f  #define PAGER_LO
33d60 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
33d70 49 56 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 53  IVE   1../*.** S
33d80 65 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 63  ee source code c
33d90 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 61 20 64 65  omments for a de
33da0 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 69  tailed descripti
33db0 6f 6e 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  on of the follow
33dc0 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 3a  ing.** routines:
33dd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
33de0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
33df0 67 65 72 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  gerOpen(sqlite3_
33e00 76 66 73 20 2a 2c 20 50 61 67 65 72 20 2a 2a 70  vfs *, Pager **p
33e10 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68  pPager, const ch
33e20 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c 69 6e 74  ar*, int,int,int
33e30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
33e40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
33e50 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65  gerSetBusyhandle
33e60 72 28 50 61 67 65 72 2a 2c 20 42 75 73 79 48 61  r(Pager*, BusyHa
33e70 6e 64 6c 65 72 20 2a 70 42 75 73 79 48 61 6e 64  ndler *pBusyHand
33e80 6c 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ler);.SQLITE_PRI
33e90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
33ea0 33 50 61 67 65 72 53 65 74 44 65 73 74 72 75 63  3PagerSetDestruc
33eb0 74 6f 72 28 50 61 67 65 72 2a 2c 20 76 6f 69 64  tor(Pager*, void
33ec0 28 2a 29 28 44 62 50 61 67 65 2a 2c 69 6e 74 29  (*)(DbPage*,int)
33ed0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
33ee0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
33ef0 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28 50  gerSetReiniter(P
33f00 61 67 65 72 2a 2c 20 76 6f 69 64 28 2a 29 28 44  ager*, void(*)(D
33f10 62 50 61 67 65 2a 2c 69 6e 74 29 29 3b 0a 53 51  bPage*,int));.SQ
33f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
33f30 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
33f40 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 2a 2c  Pagesize(Pager*,
33f50 20 75 31 36 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   u16*);.SQLITE_P
33f60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
33f70 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f  e3PagerMaxPageCo
33f80 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 29  unt(Pager*, int)
33f90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
33fa0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
33fb0 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28  rReadFileheader(
33fc0 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e 73  Pager*, int, uns
33fd0 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 53 51  igned char*);.SQ
33fe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
33ff0 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  d sqlite3PagerSe
34000 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72  tCachesize(Pager
34010 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
34020 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
34030 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61  te3PagerClose(Pa
34040 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 51  ger *pPager);.SQ
34050 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
34060 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
34070 75 69 72 65 28 50 61 67 65 72 20 2a 70 50 61 67  uire(Pager *pPag
34080 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44  er, Pgno pgno, D
34090 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20  bPage **ppPage, 
340a0 69 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 64  int clrFlag);.#d
340b0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 67  efine sqlite3Pag
340c0 65 72 47 65 74 28 41 2c 42 2c 43 29 20 73 71 6c  erGet(A,B,C) sql
340d0 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
340e0 28 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 45  (A,B,C,0).SQLITE
340f0 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20  _PRIVATE DbPage 
34100 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f  *sqlite3PagerLoo
34110 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65  kup(Pager *pPage
34120 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53  r, Pgno pgno);.S
34130 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
34140 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  t sqlite3PagerRe
34150 66 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49  f(DbPage*);.SQLI
34160 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
34170 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
34180 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54  (DbPage*);.SQLIT
34190 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
341a0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
341b0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
341c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
341d0 69 74 65 33 50 61 67 65 72 4f 76 65 72 77 72 69  ite3PagerOverwri
341e0 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  te(Pager *pPager
341f0 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 76 6f 69  , Pgno pgno, voi
34200 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
34210 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
34220 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61  agerPagecount(Pa
34230 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
34240 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
34250 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 50  3PagerTruncate(P
34260 61 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a 53 51 4c  ager*,Pgno);.SQL
34270 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
34280 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69  sqlite3PagerBegi
34290 6e 28 44 62 50 61 67 65 2a 2c 20 69 6e 74 20 65  n(DbPage*, int e
342a0 78 46 6c 61 67 29 3b 0a 53 51 4c 49 54 45 5f 50  xFlag);.SQLITE_P
342b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
342c0 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
342d0 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c 63 6f 6e  seOne(Pager*,con
342e0 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
342f0 2c 20 50 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f  , Pgno);.SQLITE_
34300 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
34310 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68  te3PagerCommitPh
34320 61 73 65 54 77 6f 28 50 61 67 65 72 2a 29 3b 0a  aseTwo(Pager*);.
34330 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
34340 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
34350 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 2a 29 3b  ollback(Pager*);
34360 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
34370 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
34380 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65 72  Isreadonly(Pager
34390 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
343a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
343b0 67 65 72 53 74 6d 74 42 65 67 69 6e 28 50 61 67  gerStmtBegin(Pag
343c0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
343d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
343e0 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28  PagerStmtCommit(
343f0 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
34400 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
34410 74 65 33 50 61 67 65 72 53 74 6d 74 52 6f 6c 6c  te3PagerStmtRoll
34420 62 61 63 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51  back(Pager*);.SQ
34430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
34440 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  d sqlite3PagerDo
34450 6e 74 52 6f 6c 6c 62 61 63 6b 28 44 62 50 61 67  ntRollback(DbPag
34460 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
34470 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
34480 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 44  PagerDontWrite(D
34490 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
344a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
344b0 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
344c0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
344d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
344e0 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66  lite3PagerSetSaf
344f0 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c  etyLevel(Pager*,
34500 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
34510 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
34520 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65  har *sqlite3Page
34530 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a  rFilename(Pager*
34540 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
34550 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  E const sqlite3_
34560 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65  vfs *sqlite3Page
34570 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51  rVfs(Pager*);.SQ
34580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
34590 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74  ite3_file *sqlit
345a0 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65  e3PagerFile(Page
345b0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
345c0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
345d0 73 71 6c 69 74 65 33 50 61 67 65 72 44 69 72 6e  sqlite3PagerDirn
345e0 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  ame(Pager*);.SQL
345f0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
34600 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50  t char *sqlite3P
34610 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  agerJournalname(
34620 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
34630 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
34640 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50  te3PagerNosync(P
34650 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
34660 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
34670 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28  e3PagerMovepage(
34680 50 61 67 65 72 2a 2c 44 62 50 61 67 65 2a 2c 50  Pager*,DbPage*,P
34690 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  gno);.SQLITE_PRI
346a0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
346b0 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 44  e3PagerGetData(D
346c0 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 54  bPage *); .SQLIT
346d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
346e0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
346f0 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b 20  xtra(DbPage *); 
34700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
34710 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
34720 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65  LockingMode(Page
34730 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20  r *, int);..#if 
34740 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
34750 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
34760 41 47 45 4d 45 4e 54 29 20 26 26 20 21 64 65 66  AGEMENT) && !def
34770 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
34780 5f 44 49 53 4b 49 4f 29 0a 53 51 4c 49 54 45 5f  _DISKIO).SQLITE_
34790 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
347a0 6c 69 74 65 33 50 61 67 65 72 52 65 6c 65 61 73  lite3PagerReleas
347b0 65 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65  eMemory(int);.#e
347c0 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
347d0 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 53 51  ITE_HAS_CODEC.SQ
347e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
347f0 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
34800 53 65 74 43 6f 64 65 63 28 50 61 67 65 72 2a 2c  SetCodec(Pager*,
34810 76 6f 69 64 2a 28 2a 29 28 76 6f 69 64 2a 2c 76  void*(*)(void*,v
34820 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 76  oid*,Pgno,int),v
34830 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  oid*);.#endif..#
34840 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
34850 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  UG) || defined(S
34860 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49  QLITE_TEST).SQLI
34870 54 45 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e  TE_PRIVATE   Pgn
34880 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  o sqlite3PagerPa
34890 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a  genumber(DbPage*
348a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
348b0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50  E   int sqlite3P
348c0 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
348d0 44 62 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66  DbPage*);.#endif
348e0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
348f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
34900 41 54 45 20 20 20 69 6e 74 20 2a 73 71 6c 69 74  ATE   int *sqlit
34910 65 33 50 61 67 65 72 53 74 61 74 73 28 50 61 67  e3PagerStats(Pag
34920 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
34930 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
34940 74 65 33 50 61 67 65 72 52 65 66 64 75 6d 70 28  te3PagerRefdump(
34950 50 61 67 65 72 2a 29 3b 0a 20 20 69 6e 74 20 70  Pager*);.  int p
34960 61 67 65 72 33 5f 72 65 66 69 6e 66 6f 5f 65 6e  ager3_refinfo_en
34970 61 62 6c 65 3b 0a 23 65 6e 64 69 66 0a 0a 23 69  able;.#endif..#i
34980 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
34990 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69  .void disable_si
349a0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
349b0 73 28 76 6f 69 64 29 3b 0a 76 6f 69 64 20 65 6e  s(void);.void en
349c0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
349d0 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a  o_errors(void);.
349e0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64  #else.# define d
349f0 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  isable_simulated
34a00 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64  _io_errors().# d
34a10 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d  efine enable_sim
34a20 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
34a30 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  ().#endif..#endi
34a40 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48 5f 20 2a  f /* _PAGER_H_ *
34a50 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
34a60 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 72 2e  ** End of pager.
34a70 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
34a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34aa0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
34ab0 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
34ac0 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
34ad0 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
34ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34af0 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20  */.../*.** Name 
34b00 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 61  of the master da
34b10 74 61 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54  tabase table.  T
34b20 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61  he master databa
34b30 73 65 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61  se table.** is a
34b40 20 73 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74   special table t
34b50 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61  hat holds the na
34b60 6d 65 73 20 61 6e 64 20 61 74 74 72 69 62 75 74  mes and attribut
34b70 65 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65  es of all.** use
34b80 72 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64  r tables and ind
34b90 69 63 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ices..*/.#define
34ba0 20 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20   MASTER_NAME    
34bb0 20 20 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65     "sqlite_maste
34bc0 72 22 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f  r".#define TEMP_
34bd0 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71  MASTER_NAME  "sq
34be0 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72  lite_temp_master
34bf0 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f  "../*.** The roo
34c00 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61  t-page of the ma
34c10 73 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61  ster database ta
34c20 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ble..*/.#define 
34c30 4d 41 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20  MASTER_ROOT     
34c40 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e    1../*.** The n
34c50 61 6d 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d  ame of the schem
34c60 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66  a table..*/.#def
34c70 69 6e 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  ine SCHEMA_TABLE
34c80 28 78 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d  (x)  ((!OMIT_TEM
34c90 50 44 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d  PDB)&&(x==1)?TEM
34ca0 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41  P_MASTER_NAME:MA
34cb0 53 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a  STER_NAME)../*.*
34cc0 2a 20 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  * A convenience 
34cd0 6d 61 63 72 6f 20 74 68 61 74 20 72 65 74 75 72  macro that retur
34ce0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
34cf0 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20   elements in.** 
34d00 61 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65  an array..*/.#de
34d10 66 69 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58  fine ArraySize(X
34d20 29 20 20 20 20 28 73 69 7a 65 6f 66 28 58 29 2f  )    (sizeof(X)/
34d30 73 69 7a 65 6f 66 28 58 5b 30 5d 29 29 0a 0a 2f  sizeof(X[0]))../
34d40 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66  *.** Forward ref
34d50 65 72 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63  erences to struc
34d60 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66  tures.*/.typedef
34d70 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20   struct AggInfo 
34d80 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  AggInfo;.typedef
34d90 20 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74   struct AuthCont
34da0 65 78 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b  ext AuthContext;
34db0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
34dc0 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71 3b  CollSeq CollSeq;
34dd0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
34de0 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a 74  Column Column;.t
34df0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 62  ypedef struct Db
34e00 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72   Db;.typedef str
34e10 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 65 6d  uct Schema Schem
34e20 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  a;.typedef struc
34e30 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79 70  t Expr Expr;.typ
34e40 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70 72  edef struct Expr
34e50 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a 74  List ExprList;.t
34e60 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 4b  ypedef struct FK
34e70 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 64 65 66  ey FKey;.typedef
34e80 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20   struct FuncDef 
34e90 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 64 65 66  FuncDef;.typedef
34ea0 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 49   struct IdList I
34eb0 64 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73  dList;.typedef s
34ec0 74 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64 65  truct Index Inde
34ed0 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  x;.typedef struc
34ee0 74 20 4b 65 79 43 6c 61 73 73 20 4b 65 79 43 6c  t KeyClass KeyCl
34ef0 61 73 73 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ass;.typedef str
34f00 75 63 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49  uct KeyInfo KeyI
34f10 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72  nfo;.typedef str
34f20 75 63 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c  uct Module Modul
34f30 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
34f40 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61  t NameContext Na
34f50 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64  meContext;.typed
34f60 65 66 20 73 74 72 75 63 74 20 50 61 72 73 65 20  ef struct Parse 
34f70 50 61 72 73 65 3b 0a 74 79 70 65 64 65 66 20 73  Parse;.typedef s
34f80 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 65 6c  truct Select Sel
34f90 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ect;.typedef str
34fa0 75 63 74 20 53 72 63 4c 69 73 74 20 53 72 63 4c  uct SrcList SrcL
34fb0 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ist;.typedef str
34fc0 75 63 74 20 54 61 62 6c 65 20 54 61 62 6c 65 3b  uct Table Table;
34fd0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
34fe0 54 61 62 6c 65 4c 6f 63 6b 20 54 61 62 6c 65 4c  TableLock TableL
34ff0 6f 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ock;.typedef str
35000 75 63 74 20 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b  uct Token Token;
35010 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
35020 54 72 69 67 67 65 72 53 74 61 63 6b 20 54 72 69  TriggerStack Tri
35030 67 67 65 72 53 74 61 63 6b 3b 0a 74 79 70 65 64  ggerStack;.typed
35040 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ef struct Trigge
35050 72 53 74 65 70 20 54 72 69 67 67 65 72 53 74 65  rStep TriggerSte
35060 70 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  p;.typedef struc
35070 74 20 54 72 69 67 67 65 72 20 54 72 69 67 67 65  t Trigger Trigge
35080 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
35090 74 20 57 68 65 72 65 49 6e 66 6f 20 57 68 65 72  t WhereInfo Wher
350a0 65 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73  eInfo;.typedef s
350b0 74 72 75 63 74 20 57 68 65 72 65 4c 65 76 65 6c  truct WhereLevel
350c0 20 57 68 65 72 65 4c 65 76 65 6c 3b 0a 0a 2f 2a   WhereLevel;../*
350d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
350e0 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20 74 68  clude os.h in th
350f0 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
35100 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
35110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
35120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
35130 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20 2a 2a  gin file os.h **
35140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
35170 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
35180 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 16.**.** The 
35190 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
351a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
351b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
351c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
351d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
351e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
351f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
35200 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
35210 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
35220 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
35230 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
35240 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
35250 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
35260 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
35270 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
35280 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
35290 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
352a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
352b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
352c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
352d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
352e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
352f0 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
35300 20 28 74 6f 67 65 74 68 65 72 20 77 69 74 68 20   (together with 
35310 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 20 73  is companion C s
35320 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 6c 65 0a  ource-code file.
35330 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 65 6d  ** "os.c") attem
35340 70 74 20 74 6f 20 61 62 73 74 72 61 63 74 20 74  pt to abstract t
35350 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70  he underlying op
35360 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73  erating system s
35370 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 53 51  o that.** the SQ
35380 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c  Lite library wil
35390 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68 20 50  l work on both P
353a0 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 6f 77 73  OSIX and windows
353b0 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20   systems..**.** 
353c0 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
353d0 20 69 73 20 23 69 6e 63 6c 75 64 65 2d 65 64 20   is #include-ed 
353e0 62 79 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 61  by sqliteInt.h a
353f0 6e 64 20 74 68 75 73 20 65 6e 64 73 20 75 70 0a  nd thus ends up.
35400 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75 64 65  ** being include
35410 64 20 62 79 20 65 76 65 72 79 20 73 6f 75 72 63  d by every sourc
35420 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64  e file..*/.#ifnd
35430 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f  ef _SQLITE_OS_H_
35440 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
35450 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69  _OS_H_../*.** Fi
35460 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 61  gure out if we a
35470 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
35480 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f  Unix, Windows, o
35490 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  r some other.** 
354a0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
354b0 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f 6c  .  After the fol
354c0 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20  lowing block of 
354d0 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 6f  preprocess macro
354e0 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 4f 53 5f  s,.** all of OS_
354f0 55 4e 49 58 2c 20 4f 53 5f 57 49 4e 2c 20 4f 53  UNIX, OS_WIN, OS
35500 5f 4f 53 32 2c 20 61 6e 64 20 4f 53 5f 4f 54 48  _OS2, and OS_OTH
35510 45 52 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 20  ER will defined 
35520 74 6f 20 65 69 74 68 65 72 0a 2a 2a 20 31 20 6f  to either.** 1 o
35530 72 20 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65  r 0.  One of the
35540 20 66 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e   four will be 1.
35550 20 20 54 68 65 20 6f 74 68 65 72 20 74 68 72 65    The other thre
35560 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f 0a  e will be 0..*/.
35570 23 69 66 20 64 65 66 69 6e 65 64 28 4f 53 5f 4f  #if defined(OS_O
35580 54 48 45 52 29 0a 23 20 69 66 20 4f 53 5f 4f 54  THER).# if OS_OT
35590 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66  HER==1.#   undef
355a0 20 4f 53 5f 55 4e 49 58 0a 23 20 20 20 64 65 66   OS_UNIX.#   def
355b0 69 6e 65 20 4f 53 5f 55 4e 49 58 20 30 0a 23 20  ine OS_UNIX 0.# 
355c0 20 20 75 6e 64 65 66 20 4f 53 5f 57 49 4e 0a 23    undef OS_WIN.#
355d0 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 57 49 4e     define OS_WIN
355e0 20 30 0a 23 20 20 20 75 6e 64 65 66 20 4f 53 5f   0.#   undef OS_
355f0 4f 53 32 0a 23 20 20 20 64 65 66 69 6e 65 20 4f  OS2.#   define O
35600 53 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23  S_OS2 0.# else.#
35610 20 20 20 75 6e 64 65 66 20 4f 53 5f 4f 54 48 45     undef OS_OTHE
35620 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  R.# endif.#endif
35630 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4f 53  .#if !defined(OS
35640 5f 55 4e 49 58 29 20 26 26 20 21 64 65 66 69 6e  _UNIX) && !defin
35650 65 64 28 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64  ed(OS_OTHER).# d
35660 65 66 69 6e 65 20 4f 53 5f 4f 54 48 45 52 20 30  efine OS_OTHER 0
35670 0a 23 20 69 66 6e 64 65 66 20 4f 53 5f 57 49 4e  .# ifndef OS_WIN
35680 0a 23 20 20 20 69 66 20 64 65 66 69 6e 65 64 28  .#   if defined(
35690 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e  _WIN32) || defin
356a0 65 64 28 57 49 4e 33 32 29 20 7c 7c 20 64 65 66  ed(WIN32) || def
356b0 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29  ined(__CYGWIN__)
356c0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49   || defined(__MI
356d0 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69  NGW32__) || defi
356e0 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f  ned(__BORLANDC__
356f0 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 4f  ).#     define O
35700 53 5f 57 49 4e 20 31 0a 23 20 20 20 20 20 64 65  S_WIN 1.#     de
35710 66 69 6e 65 20 4f 53 5f 55 4e 49 58 20 30 0a 23  fine OS_UNIX 0.#
35720 20 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 4f       define OS_O
35730 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20 64 65  S2 0.#   elif de
35740 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 20 7c  fined(__EMX__) |
35750 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 29 20  | defined(_OS2) 
35760 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32 29 20  || defined(OS2) 
35770 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 5f  || defined(_OS2_
35780 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4f  ) || defined(__O
35790 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69  S2__).#     defi
357a0 6e 65 20 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20  ne OS_WIN 0.#   
357b0 20 20 64 65 66 69 6e 65 20 4f 53 5f 55 4e 49 58    define OS_UNIX
357c0 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20   0.#     define 
357d0 4f 53 5f 4f 53 32 20 31 0a 23 20 20 20 65 6c 73  OS_OS2 1.#   els
357e0 65 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 4f  e.#     define O
357f0 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65  S_WIN 0.#     de
35800 66 69 6e 65 20 4f 53 5f 55 4e 49 58 20 31 0a 23  fine OS_UNIX 1.#
35810 20 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 4f       define OS_O
35820 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 20  S2 0.#  endif.# 
35830 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 4f  else.#  define O
35840 53 5f 55 4e 49 58 20 30 0a 23 20 20 64 65 66 69  S_UNIX 0.#  defi
35850 6e 65 20 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e  ne OS_OS2 0.# en
35860 64 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64  dif.#else.# ifnd
35870 65 66 20 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66  ef OS_WIN.#  def
35880 69 6e 65 20 4f 53 5f 57 49 4e 20 30 0a 23 20 65  ine OS_WIN 0.# e
35890 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f  ndif.#endif..../
358a0 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20  *.** Define the 
358b0 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20  maximum size of 
358c0 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
358d0 6e 61 6d 65 0a 2a 2f 0a 23 69 66 20 4f 53 5f 57  name.*/.#if OS_W
358e0 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77 69  IN.# include <wi
358f0 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69 6e  ndows.h>.# defin
35900 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d  e SQLITE_TEMPNAM
35910 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54 48  E_SIZE (MAX_PATH
35920 2b 35 30 29 0a 23 65 6c 69 66 20 4f 53 5f 4f 53  +50).#elif OS_OS
35930 32 0a 23 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f  2.# if (__GNUC__
35940 20 3e 20 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f   > 3 || __GNUC__
35950 20 3d 3d 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f   == 3 && __GNUC_
35960 4d 49 4e 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26  MINOR__ >= 3) &&
35970 20 64 65 66 69 6e 65 64 28 4f 53 32 5f 48 49 47   defined(OS2_HIG
35980 48 5f 4d 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63  H_MEMORY).#  inc
35990 6c 75 64 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e  lude <os2safe.h>
359a0 20 2f 2a 20 68 61 73 20 74 6f 20 62 65 20 69 6e   /* has to be in
359b0 63 6c 75 64 65 64 20 62 65 66 6f 72 65 20 6f 73  cluded before os
359c0 32 2e 68 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20  2.h for linking 
359d0 74 6f 20 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64  to work */.# end
359e0 69 66 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c  if.# define INCL
359f0 5f 44 4f 53 44 41 54 45 54 49 4d 45 0a 23 20 64  _DOSDATETIME.# d
35a00 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49  efine INCL_DOSFI
35a10 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49  LEMGR.# define I
35a20 4e 43 4c 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20  NCL_DOSERRORS.# 
35a30 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d  define INCL_DOSM
35a40 49 53 43 0a 23 20 64 65 66 69 6e 65 20 49 4e 43  ISC.# define INC
35a50 4c 5f 44 4f 53 50 52 4f 43 45 53 53 0a 23 20 64  L_DOSPROCESS.# d
35a60 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f  efine INCL_DOSMO
35a70 44 55 4c 45 4d 47 52 0a 23 20 69 6e 63 6c 75 64  DULEMGR.# includ
35a80 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 64 65 66 69  e <os2.h>.# defi
35a90 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41  ne SQLITE_TEMPNA
35aa0 4d 45 5f 53 49 5a 45 20 28 43 43 48 4d 41 58 50  ME_SIZE (CCHMAXP
35ab0 41 54 48 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23  ATHCOMP).#else.#
35ac0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54   define SQLITE_T
35ad0 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30  EMPNAME_SIZE 200
35ae0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74  .#endif../* If t
35af0 68 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20  he SET_FULLSYNC 
35b00 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 64 65 66  macro is not def
35b10 69 6e 65 64 20 61 62 6f 76 65 2c 20 74 68 65 6e  ined above, then
35b20 20 6d 61 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f   make it.** a no
35b30 2d 6f 70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  -op.*/.#ifndef S
35b40 45 54 5f 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65  ET_FULLSYNC.# de
35b50 66 69 6e 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e  fine SET_FULLSYN
35b60 43 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f  C(x,y).#endif../
35b70 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  *.** The default
35b80 20 73 69 7a 65 20 6f 66 20 61 20 64 69 73 6b 20   size of a disk 
35b90 73 65 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65  sector.*/.#ifnde
35ba0 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  f SQLITE_DEFAULT
35bb0 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64  _SECTOR_SIZE.# d
35bc0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
35bd0 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45  AULT_SECTOR_SIZE
35be0 20 35 31 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   512.#endif../*.
35bf0 2a 2a 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c  ** Temporary fil
35c00 65 73 20 61 72 65 20 6e 61 6d 65 64 20 73 74 61  es are named sta
35c10 72 74 69 6e 67 20 77 69 74 68 20 74 68 69 73 20  rting with this 
35c20 70 72 65 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20  prefix followed 
35c30 62 79 20 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20  by 16 random.** 
35c40 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 63 68 61  alphanumeric cha
35c50 72 61 63 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20  racters, and no 
35c60 66 69 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20  file extension. 
35c70 54 68 65 79 20 61 72 65 20 73 74 6f 72 65 64 20  They are stored 
35c80 69 6e 20 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73  in the.** OS's s
35c90 74 61 6e 64 61 72 64 20 74 65 6d 70 6f 72 61 72  tandard temporar
35ca0 79 20 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79  y file directory
35cb0 2c 20 61 6e 64 20 61 72 65 20 64 65 6c 65 74 65  , and are delete
35cc0 64 20 70 72 69 6f 72 20 74 6f 20 65 78 69 74 2e  d prior to exit.
35cd0 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 20 69 73  .** If sqlite is
35ce0 20 62 65 69 6e 67 20 65 6d 62 65 64 64 65 64 20   being embedded 
35cf0 69 6e 20 61 6e 6f 74 68 65 72 20 70 72 6f 67 72  in another progr
35d00 61 6d 2c 20 79 6f 75 20 6d 61 79 20 77 69 73 68  am, you may wish
35d10 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a   to change the.*
35d20 2a 20 70 72 65 66 69 78 20 74 6f 20 72 65 66 6c  * prefix to refl
35d30 65 63 74 20 79 6f 75 72 20 70 72 6f 67 72 61 6d  ect your program
35d40 27 73 20 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74  's name, so that
35d50 20 69 66 20 79 6f 75 72 20 70 72 6f 67 72 61 6d   if your program
35d60 20 65 78 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74   exits.** premat
35d70 75 72 65 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f  urely, old tempo
35d80 72 61 72 79 20 66 69 6c 65 73 20 63 61 6e 20 62  rary files can b
35d90 65 20 65 61 73 69 6c 79 20 69 64 65 6e 74 69 66  e easily identif
35da0 69 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65  ied. This can be
35db0 20 64 6f 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d   done.** using -
35dc0 44 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c  DSQLITE_TEMP_FIL
35dd0 45 5f 50 52 45 46 49 58 3d 6d 79 70 72 65 66 69  E_PREFIX=myprefi
35de0 78 5f 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c  x_ on the compil
35df0 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e  er command line.
35e00 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33  .**.** 2006-10-3
35e10 31 3a 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  1:  The default 
35e20 70 72 65 66 69 78 20 75 73 65 64 20 74 6f 20 62  prefix used to b
35e30 65 20 22 73 71 6c 69 74 65 5f 22 2e 20 20 42 75  e "sqlite_".  Bu
35e40 74 20 74 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65  t then.** Mcafee
35e50 20 73 74 61 72 74 65 64 20 75 73 69 6e 67 20 53   started using S
35e60 51 4c 69 74 65 20 69 6e 20 74 68 65 69 72 20 61  QLite in their a
35e70 6e 74 69 2d 76 69 72 75 73 20 70 72 6f 64 75 63  nti-virus produc
35e80 74 20 61 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72  t and it.** star
35e90 74 65 64 20 70 75 74 74 69 6e 67 20 66 69 6c 65  ted putting file
35ea0 73 20 77 69 74 68 20 74 68 65 20 22 73 71 6c 69  s with the "sqli
35eb0 74 65 22 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  te" name in the 
35ec0 63 3a 2f 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a  c:/temp folder..
35ed0 2a 2a 20 54 68 69 73 20 61 6e 6e 6f 79 65 64 20  ** This annoyed 
35ee0 6d 61 6e 79 20 77 69 6e 64 6f 77 73 20 75 73 65  many windows use
35ef0 72 73 2e 20 20 54 68 6f 73 65 20 75 73 65 72 73  rs.  Those users
35f00 20 77 6f 75 6c 64 20 74 68 65 6e 20 64 6f 20 61   would then do a
35f10 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72   .** Google sear
35f20 63 68 20 66 6f 72 20 22 73 71 6c 69 74 65 22 2c  ch for "sqlite",
35f30 20 66 69 6e 64 20 74 68 65 20 74 65 6c 65 70 68   find the teleph
35f40 6f 6e 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 74  one numbers of t
35f50 68 65 0a 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73  he.** developers
35f60 20 61 6e 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b   and call to wak
35f70 65 20 74 68 65 6d 20 75 70 20 61 74 20 6e 69 67  e them up at nig
35f80 68 74 20 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e  ht and complain.
35f90 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 65 61  .** For this rea
35fa0 73 6f 6e 2c 20 74 68 65 20 64 65 66 61 75 6c 74  son, the default
35fb0 20 6e 61 6d 65 20 70 72 65 66 69 78 20 69 73 20   name prefix is 
35fc0 63 68 61 6e 67 65 64 20 74 6f 20 62 65 20 22 73  changed to be "s
35fd0 71 6c 69 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c  qlite" .** spell
35fe0 65 64 20 62 61 63 6b 77 61 72 64 73 2e 20 20 53  ed backwards.  S
35ff0 6f 20 74 68 65 20 74 65 6d 70 20 66 69 6c 65 73  o the temp files
36000 20 61 72 65 20 73 74 69 6c 6c 20 69 64 65 6e 74   are still ident
36010 69 66 69 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e  ified, but.** an
36020 79 62 6f 64 79 20 73 6d 61 72 74 20 65 6e 6f 75  ybody smart enou
36030 67 68 20 74 6f 20 66 69 67 75 72 65 20 6f 75 74  gh to figure out
36040 20 74 68 65 20 63 6f 64 65 20 69 73 20 61 6c 73   the code is als
36050 6f 20 6c 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a  o likely smart.*
36060 2a 20 65 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77  * enough to know
36070 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 74 68   that calling th
36080 65 20 64 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c  e developer will
36090 20 6e 6f 74 20 68 65 6c 70 20 67 65 74 20 72 69   not help get ri
360a0 64 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65  d.** of the file
360b0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
360c0 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52  ITE_TEMP_FILE_PR
360d0 45 46 49 58 0a 23 20 64 65 66 69 6e 65 20 53 51  EFIX.# define SQ
360e0 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
360f0 52 45 46 49 58 20 22 65 74 69 6c 71 73 5f 22 0a  REFIX "etilqs_".
36100 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
36110 20 75 73 69 6e 67 20 61 6e 20 61 6c 74 65 72 6e   using an altern
36120 61 74 69 76 65 20 4f 53 20 69 6e 74 65 72 66 61  ative OS interfa
36130 63 65 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74  ce, then we must
36140 20 68 61 76 65 20 61 6e 20 22 6f 73 5f 6f 74 68   have an "os_oth
36150 65 72 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72 20  er.h".** header 
36160 66 69 6c 65 20 61 76 61 69 6c 61 62 6c 65 20 66  file available f
36170 6f 72 20 74 68 61 74 20 69 6e 74 65 72 66 61 63  or that interfac
36180 65 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 20 74  e.  Presumably t
36190 68 65 20 22 6f 73 5f 6f 74 68 65 72 2e 68 22 0a  he "os_other.h".
361a0 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65 20 63  ** header file c
361b0 6f 6e 74 61 69 6e 73 20 23 64 65 66 69 6e 65 73  ontains #defines
361c0 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 6f 73   similar to thos
361d0 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 20  e above..*/.#if 
361e0 4f 53 5f 4f 54 48 45 52 0a 23 20 69 6e 63 6c 75  OS_OTHER.# inclu
361f0 64 65 20 22 6f 73 5f 6f 74 68 65 72 2e 68 22 0a  de "os_other.h".
36200 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54  #endif.../*.** T
36210 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  he following val
36220 75 65 73 20 6d 61 79 20 62 65 20 70 61 73 73 65  ues may be passe
36230 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
36240 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73  argument to.** s
36250 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20  qlite3OsLock(). 
36260 54 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b  The various lock
36270 73 20 65 78 68 69 62 69 74 20 74 68 65 20 66 6f  s exhibit the fo
36280 6c 6c 6f 77 69 6e 67 20 73 65 6d 61 6e 74 69 63  llowing semantic
36290 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45 44 3a  s:.**.** SHARED:
362a0 20 20 20 20 41 6e 79 20 6e 75 6d 62 65 72 20 6f      Any number o
362b0 66 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20  f processes may 
362c0 68 6f 6c 64 20 61 20 53 48 41 52 45 44 20 6c 6f  hold a SHARED lo
362d0 63 6b 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c  ck simultaneousl
362e0 79 2e 0a 2a 2a 20 52 45 53 45 52 56 45 44 3a 20  y..** RESERVED: 
362f0 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73   A single proces
36300 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 52 45 53  s may hold a RES
36310 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 61 20  ERVED lock on a 
36320 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20  file at.**      
36330 20 20 20 20 20 20 61 6e 79 20 74 69 6d 65 2e 20        any time. 
36340 4f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20  Other processes 
36350 6d 61 79 20 68 6f 6c 64 20 61 6e 64 20 6f 62 74  may hold and obt
36360 61 69 6e 20 6e 65 77 20 53 48 41 52 45 44 20 6c  ain new SHARED l
36370 6f 63 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47  ocks..** PENDING
36380 3a 20 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f  :   A single pro
36390 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20  cess may hold a 
363a0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20  PENDING lock on 
363b0 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20  a file at.**    
363c0 20 20 20 20 20 20 20 20 61 6e 79 20 6f 6e 65 20          any one 
363d0 74 69 6d 65 2e 20 45 78 69 73 74 69 6e 67 20 53  time. Existing S
363e0 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20  HARED locks may 
363f0 70 65 72 73 69 73 74 2c 20 62 75 74 20 6e 6f 20  persist, but no 
36400 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  new.**          
36410 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d    SHARED locks m
36420 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62  ay be obtained b
36430 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  y other processe
36440 73 2e 0a 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a  s..** EXCLUSIVE:
36450 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   An EXCLUSIVE lo
36460 63 6b 20 70 72 65 63 6c 75 64 65 73 20 61 6c 6c  ck precludes all
36470 20 6f 74 68 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a   other locks..**
36480 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  .** PENDING_LOCK
36490 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 61 73 73   may not be pass
364a0 65 64 20 64 69 72 65 63 74 6c 79 20 74 6f 20 73  ed directly to s
364b0 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20  qlite3OsLock(). 
364c0 49 6e 73 74 65 61 64 2c 20 61 0a 2a 2a 20 70 72  Instead, a.** pr
364d0 6f 63 65 73 73 20 74 68 61 74 20 72 65 71 75 65  ocess that reque
364e0 73 74 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45  sts an EXCLUSIVE
364f0 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74 75 61 6c   lock may actual
36500 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44  ly obtain a PEND
36510 49 4e 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69  ING.** lock. Thi
36520 73 20 63 61 6e 20 62 65 20 75 70 67 72 61 64 65  s can be upgrade
36530 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56  d to an EXCLUSIV
36540 45 20 6c 6f 63 6b 20 62 79 20 61 20 73 75 62 73  E lock by a subs
36550 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a  equent call to.*
36560 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  * sqlite3OsLock(
36570 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4f  )..*/.#define NO
36580 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0a  _LOCK         0.
36590 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 4c  #define SHARED_L
365a0 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e  OCK     1.#defin
365b0 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  e RESERVED_LOCK 
365c0 20 20 32 0a 23 64 65 66 69 6e 65 20 50 45 4e 44    2.#define PEND
365d0 49 4e 47 5f 4c 4f 43 4b 20 20 20 20 33 0a 23 64  ING_LOCK    3.#d
365e0 65 66 69 6e 65 20 45 58 43 4c 55 53 49 56 45 5f  efine EXCLUSIVE_
365f0 4c 4f 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46  LOCK  4../*.** F
36600 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65  ile Locking Note
36610 73 3a 20 20 28 4d 6f 73 74 6c 79 20 61 62 6f 75  s:  (Mostly abou
36620 74 20 77 69 6e 64 6f 77 73 20 62 75 74 20 61 6c  t windows but al
36630 73 6f 20 73 6f 6d 65 20 69 6e 66 6f 20 66 6f 72  so some info for
36640 20 55 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20   Unix).**.** We 
36650 63 61 6e 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46  cannot use LockF
36660 69 6c 65 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63  ileEx() or Unloc
36670 6b 46 69 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e  kFileEx() on Win
36680 39 35 2f 39 38 2f 4d 45 20 62 65 63 61 75 73 65  95/98/ME because
36690 0a 2a 2a 20 74 68 6f 73 65 20 66 75 6e 63 74 69  .** those functi
366a0 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  ons are not avai
366b0 6c 61 62 6c 65 2e 20 20 53 6f 20 77 65 20 75 73  lable.  So we us
366c0 65 20 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28  e only LockFile(
366d0 29 20 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46  ) and.** UnlockF
366e0 69 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63  ile()..**.** Loc
366f0 6b 46 69 6c 65 28 29 20 70 72 65 76 65 6e 74 73  kFile() prevents
36700 20 6e 6f 74 20 6a 75 73 74 20 77 72 69 74 69 6e   not just writin
36710 67 20 62 75 74 20 61 6c 73 6f 20 72 65 61 64 69  g but also readi
36720 6e 67 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63  ng by other proc
36730 65 73 73 65 73 2e 0a 2a 2a 20 41 20 53 48 41 52  esses..** A SHAR
36740 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69  ED_LOCK is obtai
36750 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61  ned by locking a
36760 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79   single randomly
36770 2d 63 68 6f 73 65 6e 20 0a 2a 2a 20 62 79 74 65  -chosen .** byte
36780 20 6f 75 74 20 6f 66 20 61 20 73 70 65 63 69 66   out of a specif
36790 69 63 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65  ic range of byte
367a0 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62 79 74 65  s. The lock byte
367b0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 61 74 20   is obtained at 
367c0 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73 6f 20 74 77  .** random so tw
367d0 6f 20 73 65 70 61 72 61 74 65 20 72 65 61 64 65  o separate reade
367e0 72 73 20 63 61 6e 20 70 72 6f 62 61 62 6c 79 20  rs can probably 
367f0 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 20  access the file 
36800 61 74 20 74 68 65 20 0a 2a 2a 20 73 61 6d 65 20  at the .** same 
36810 74 69 6d 65 2c 20 75 6e 6c 65 73 73 20 74 68 65  time, unless the
36820 79 20 61 72 65 20 75 6e 6c 75 63 6b 79 20 61 6e  y are unlucky an
36830 64 20 63 68 6f 6f 73 65 20 74 68 65 20 73 61 6d  d choose the sam
36840 65 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20  e lock byte..** 
36850 41 6e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  An EXCLUSIVE_LOC
36860 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79  K is obtained by
36870 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 62 79 74   locking all byt
36880 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e  es in the range.
36890 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e  .** There can on
368a0 6c 79 20 62 65 20 6f 6e 65 20 77 72 69 74 65 72  ly be one writer
368b0 2e 20 20 41 20 52 45 53 45 52 56 45 44 5f 4c 4f  .  A RESERVED_LO
368c0 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62  CK is obtained b
368d0 79 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61 20 73  y locking.** a s
368e0 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 74 68  ingle byte of th
368f0 65 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 64  e file that is d
36900 65 73 69 67 6e 61 74 65 64 20 61 73 20 74 68 65  esignated as the
36910 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 62   reserved lock b
36920 79 74 65 2e 0a 2a 2a 20 41 20 50 45 4e 44 49 4e  yte..** A PENDIN
36930 47 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e  G_LOCK is obtain
36940 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20  ed by locking a 
36950 64 65 73 69 67 6e 61 74 65 64 20 62 79 74 65 20  designated byte 
36960 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a  different from.*
36970 2a 20 74 68 65 20 52 45 53 45 52 56 45 44 5f 4c  * the RESERVED_L
36980 4f 43 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20  OCK byte..**.** 
36990 4f 6e 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73  On WinNT/2K/XP s
369a0 79 73 74 65 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65  ystems, LockFile
369b0 45 78 28 29 20 61 6e 64 20 55 6e 6c 6f 63 6b 46  Ex() and UnlockF
369c0 69 6c 65 45 78 28 29 20 61 72 65 20 61 76 61 69  ileEx() are avai
369d0 6c 61 62 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20  lable,.** which 
369e0 6d 65 61 6e 73 20 77 65 20 63 61 6e 20 75 73 65  means we can use
369f0 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c   reader/writer l
36a00 6f 63 6b 73 2e 20 20 57 68 65 6e 20 72 65 61 64  ocks.  When read
36a10 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 0a  er/writer locks.
36a20 2a 2a 20 61 72 65 20 75 73 65 64 2c 20 74 68 65  ** are used, the
36a30 20 6c 6f 63 6b 20 69 73 20 70 6c 61 63 65 64 20   lock is placed 
36a40 6f 6e 20 74 68 65 20 73 61 6d 65 20 72 61 6e 67  on the same rang
36a50 65 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20  e of bytes that 
36a60 69 73 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 70  is used.** for p
36a70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 6c 6f 63  robabilistic loc
36a80 6b 69 6e 67 20 69 6e 20 57 69 6e 39 35 2f 39 38  king in Win95/98
36a90 2f 4d 45 2e 20 20 48 65 6e 63 65 2c 20 74 68 65  /ME.  Hence, the
36aa0 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 0a   locking scheme.
36ab0 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f 72 74 20  ** will support 
36ac0 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39  two or more Win9
36ad0 35 20 72 65 61 64 65 72 73 20 6f 72 20 74 77 6f  5 readers or two
36ae0 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72   or more WinNT r
36af0 65 61 64 65 72 73 2e 0a 2a 2a 20 42 75 74 20 61  eaders..** But a
36b00 20 73 69 6e 67 6c 65 20 57 69 6e 39 35 20 72 65   single Win95 re
36b10 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f  ader will lock o
36b20 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20 72 65 61  ut all WinNT rea
36b30 64 65 72 73 20 61 6e 64 20 61 20 73 69 6e 67 6c  ders and a singl
36b40 65 0a 2a 2a 20 57 69 6e 4e 54 20 72 65 61 64 65  e.** WinNT reade
36b50 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20  r will lock out 
36b60 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e 39 35 20  all other Win95 
36b70 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54  readers..**.** T
36b80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 23 64 65  he following #de
36b90 66 69 6e 65 73 20 73 70 65 63 69 66 79 20 74 68  fines specify th
36ba0 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73  e range of bytes
36bb0 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e   used for lockin
36bc0 67 2e 0a 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a  g..** SHARED_SIZ
36bd0 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  E is the number 
36be0 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62  of bytes availab
36bf0 6c 65 20 69 6e 20 74 68 65 20 70 6f 6f 6c 20 66  le in the pool f
36c00 72 6f 6d 20 77 68 69 63 68 0a 2a 2a 20 61 20 72  rom which.** a r
36c10 61 6e 64 6f 6d 20 62 79 74 65 20 69 73 20 73 65  andom byte is se
36c20 6c 65 63 74 65 64 20 66 6f 72 20 61 20 73 68 61  lected for a sha
36c30 72 65 64 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70  red lock.  The p
36c40 6f 6f 6c 20 6f 66 20 62 79 74 65 73 20 66 6f 72  ool of bytes for
36c50 0a 2a 2a 20 73 68 61 72 65 64 20 6c 6f 63 6b 73  .** shared locks
36c60 20 62 65 67 69 6e 73 20 61 74 20 53 48 41 52 45   begins at SHARE
36c70 44 5f 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20  D_FIRST. .**.** 
36c80 54 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 61  These #defines a
36c90 72 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  re available in 
36ca0 73 71 6c 69 74 65 5f 61 75 78 2e 68 20 73 6f 20  sqlite_aux.h so 
36cb0 74 68 61 74 20 61 64 61 70 74 6f 72 73 20 66 6f  that adaptors fo
36cc0 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6e 67 20  r.** connecting 
36cd0 53 51 4c 69 74 65 20 74 6f 20 6f 74 68 65 72 20  SQLite to other 
36ce0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
36cf0 73 20 63 61 6e 20 75 73 65 20 74 68 65 20 73 61  s can use the sa
36d00 6d 65 20 62 79 74 65 0a 2a 2a 20 72 61 6e 67 65  me byte.** range
36d10 73 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20  s for locking.  
36d20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c 20 74  In particular, t
36d30 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20  he same locking 
36d40 73 74 72 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20  strategy and.** 
36d50 62 79 74 65 20 72 61 6e 67 65 73 20 61 72 65 20  byte ranges are 
36d60 75 73 65 64 20 66 6f 72 20 55 6e 69 78 2e 20 20  used for Unix.  
36d70 54 68 69 73 20 6c 65 61 76 65 73 20 6f 70 65 6e  This leaves open
36d80 20 74 68 65 20 70 6f 73 73 69 62 6c 69 74 79 20   the possiblity 
36d90 6f 66 20 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69  of having.** cli
36da0 65 6e 74 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77  ents on win95, w
36db0 69 6e 4e 54 2c 20 61 6e 64 20 75 6e 69 78 20 61  inNT, and unix a
36dc0 6c 6c 20 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68  ll talking to th
36dd0 65 20 73 61 6d 65 20 73 68 61 72 65 64 20 66 69  e same shared fi
36de0 6c 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f  le.** and all lo
36df0 63 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e  cking correctly.
36e00 20 20 54 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64    To do so would
36e10 20 72 65 71 75 69 72 65 20 74 68 61 74 20 73 61   require that sa
36e20 6d 62 61 20 28 6f 72 20 77 68 61 74 65 76 65 72  mba (or whatever
36e30 0a 2a 2a 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e  .** tool is bein
36e40 67 20 75 73 65 64 20 66 6f 72 20 66 69 6c 65 20  g used for file 
36e50 73 68 61 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65  sharing) impleme
36e60 6e 74 73 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63  nts locks correc
36e70 74 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77  tly between.** w
36e80 69 6e 64 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e  indows and unix.
36e90 20 20 49 27 6d 20 67 75 65 73 73 69 6e 67 20 74    I'm guessing t
36ea0 68 61 74 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79  hat isn't likely
36eb0 20 74 6f 20 68 61 70 70 65 6e 2c 20 62 75 74 20   to happen, but 
36ec0 62 79 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  by.** using the 
36ed0 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e  same locking ran
36ee0 67 65 20 77 65 20 61 72 65 20 61 74 20 6c 65 61  ge we are at lea
36ef0 73 74 20 6f 70 65 6e 20 74 6f 20 74 68 65 20 70  st open to the p
36f00 6f 73 73 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a  ossibility..**.*
36f10 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e  * Locking in win
36f20 64 6f 77 73 20 69 73 20 6d 61 6e 64 69 74 6f 72  dows is manditor
36f30 79 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61  y.  For this rea
36f40 73 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73  son, we cannot s
36f50 74 6f 72 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64  tore.** actual d
36f60 61 74 61 20 69 6e 20 74 68 65 20 62 79 74 65 73  ata in the bytes
36f70 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e   used for lockin
36f80 67 2e 20 20 54 68 65 20 70 61 67 65 72 20 6e 65  g.  The pager ne
36f90 76 65 72 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a  ver allocates.**
36fa0 20 74 68 65 20 70 61 67 65 73 20 69 6e 76 6f 6c   the pages invol
36fb0 76 65 64 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74  ved in locking t
36fc0 68 65 72 65 66 6f 72 65 2e 20 20 53 48 41 52 45  herefore.  SHARE
36fd0 44 5f 53 49 5a 45 20 69 73 20 73 65 6c 65 63 74  D_SIZE is select
36fe0 65 64 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c  ed so.** that al
36ff0 6c 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74  l locks will fit
37000 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67   on a single pag
37010 65 20 65 76 65 6e 20 61 74 20 74 68 65 20 6d 69  e even at the mi
37020 6e 69 6d 75 6d 20 70 61 67 65 20 73 69 7a 65 2e  nimum page size.
37030 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  .** PENDING_BYTE
37040 20 64 65 66 69 6e 65 73 20 74 68 65 20 62 65 67   defines the beg
37050 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f  inning of the lo
37060 63 6b 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74  cks.  By default
37070 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a   PENDING_BYTE.**
37080 20 69 73 20 73 65 74 20 68 69 67 68 20 73 6f 20   is set high so 
37090 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 68 61  that we don't ha
370a0 76 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  ve to allocate a
370b0 6e 20 75 6e 75 73 65 64 20 70 61 67 65 20 65 78  n unused page ex
370c0 63 65 70 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79  cept.** for very
370d0 20 6c 61 72 67 65 20 64 61 74 61 62 61 73 65 73   large databases
370e0 2e 20 20 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c  .  But one shoul
370f0 64 20 74 65 73 74 20 74 68 65 20 70 61 67 65 20  d test the page 
37100 73 6b 69 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a  skipping logic .
37110 2a 2a 20 62 79 20 73 65 74 74 69 6e 67 20 50 45  ** by setting PE
37120 4e 44 49 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61  NDING_BYTE low a
37130 6e 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 65  nd running the e
37140 6e 74 69 72 65 20 72 65 67 72 65 73 73 69 6f 6e  ntire regression
37150 20 73 75 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68   suite..**.** Ch
37160 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65  anging the value
37170 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45   of PENDING_BYTE
37180 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 73 75   results in a su
37190 62 74 6c 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c  btly incompatibl
371a0 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74  e.** file format
371b0 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20  .  Depending on 
371c0 68 6f 77 20 69 74 20 69 73 20 63 68 61 6e 67 65  how it is change
371d0 64 2c 20 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74  d, you might not
371e0 20 6e 6f 74 69 63 65 0a 2a 2a 20 74 68 65 20 69   notice.** the i
371f0 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 72  ncompatibility r
37200 69 67 68 74 20 61 77 61 79 2c 20 65 76 65 6e 20  ight away, even 
37210 72 75 6e 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72  running a full r
37220 65 67 72 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a  egression test..
37230 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c  ** The default l
37240 6f 63 61 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49  ocation of PENDI
37250 4e 47 5f 42 59 54 45 20 69 73 20 74 68 65 20 66  NG_BYTE is the f
37260 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
37270 68 65 0a 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61  he.** 1GB bounda
37280 72 79 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65  ry..**.*/.#ifnde
37290 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 23 64  f SQLITE_TEST.#d
372a0 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59  efine PENDING_BY
372b0 54 45 20 20 20 20 20 20 30 78 34 30 30 30 30 30  TE      0x400000
372c0 30 30 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74  00  /* First byt
372d0 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62  e past the 1GB b
372e0 6f 75 6e 64 61 72 79 20 2a 2f 0a 23 65 6c 73 65  oundary */.#else
372f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65  .SQLITE_API exte
37300 72 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  rn unsigned int 
37310 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f  sqlite3_pending_
37320 62 79 74 65 3b 0a 23 64 65 66 69 6e 65 20 50 45  byte;.#define PE
37330 4e 44 49 4e 47 5f 42 59 54 45 20 73 71 6c 69 74  NDING_BYTE sqlit
37340 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65 0a  e3_pending_byte.
37350 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20  #endif..#define 
37360 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 20 20  RESERVED_BYTE   
37370 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b    (PENDING_BYTE+
37380 31 29 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45  1).#define SHARE
37390 44 5f 46 49 52 53 54 20 20 20 20 20 20 28 50 45  D_FIRST      (PE
373a0 4e 44 49 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64  NDING_BYTE+2).#d
373b0 65 66 69 6e 65 20 53 48 41 52 45 44 5f 53 49 5a  efine SHARED_SIZ
373c0 45 20 20 20 20 20 20 20 35 31 30 0a 0a 2f 2a 20  E       510../* 
373d0 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f  .** Functions fo
373e0 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69  r accessing sqli
373f0 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
37400 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
37410 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
37420 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  sClose(sqlite3_f
37430 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ile*);.SQLITE_PR
37440 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
37450 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f  3OsRead(sqlite3_
37460 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e  file*, void*, in
37470 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65  t amt, i64 offse
37480 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
37490 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
374a0 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69  Write(sqlite3_fi
374b0 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  le*, const void*
374c0 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f  , int amt, i64 o
374d0 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50  ffset);.SQLITE_P
374e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
374f0 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c  e3OsTruncate(sql
37500 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20  ite3_file*, i64 
37510 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  size);.SQLITE_PR
37520 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
37530 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  3OsSync(sqlite3_
37540 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  file*, int);.SQL
37550 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
37560 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
37570 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  e(sqlite3_file*,
37580 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51   i64 *pSize);.SQ
37590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
375a0 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73   sqlite3OsLock(s
375b0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
375c0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
375d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
375e0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
375f0 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ile*, int);.SQLI
37600 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
37610 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73  qlite3OsCheckRes
37620 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
37630 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 53 51 4c  3_file *id);.SQL
37640 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
37650 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e  sqlite3OsFileCon
37660 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c  trol(sqlite3_fil
37670 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 53  e*,int,void*);.S
37680 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
37690 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  t sqlite3OsSecto
376a0 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
376b0 6c 65 20 2a 69 64 29 3b 0a 53 51 4c 49 54 45 5f  le *id);.SQLITE_
376c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
376d0 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61  te3OsDeviceChara
376e0 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74  cteristics(sqlit
376f0 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 0a 2f  e3_file *id);../
37700 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20  * .** Functions 
37710 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 73 71  for accessing sq
37720 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64  lite3_vfs method
37730 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s .*/.SQLITE_PRI
37740 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
37750 4f 73 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  OsOpen(sqlite3_v
37760 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  fs *, const char
37770 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   *, sqlite3_file
37780 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a  *, int, int *);.
37790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
377a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65  nt sqlite3OsDele
377b0 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  te(sqlite3_vfs *
377c0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
377d0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
377e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
377f0 4f 73 41 63 63 65 73 73 28 73 71 6c 69 74 65 33  OsAccess(sqlite3
37800 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68  _vfs *, const ch
37810 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ar *, int);.SQLI
37820 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
37830 71 6c 69 74 65 33 4f 73 47 65 74 54 65 6d 70 4e  qlite3OsGetTempN
37840 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ame(sqlite3_vfs 
37850 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49  *, char *);.SQLI
37860 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
37870 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68  qlite3OsFullPath
37880 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73  name(sqlite3_vfs
37890 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
378a0 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  , char *);.SQLIT
378b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
378c0 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28  sqlite3OsDlOpen(
378d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63  sqlite3_vfs *, c
378e0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
378f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
37900 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72  d sqlite3OsDlErr
37910 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  or(sqlite3_vfs *
37920 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a  , int, char *);.
37930 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
37940 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c  oid *sqlite3OsDl
37950 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20  Sym(sqlite3_vfs 
37960 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74  *, void *, const
37970 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
37980 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
37990 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73  lite3OsDlClose(s
379a0 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f  qlite3_vfs *, vo
379b0 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  id *);.SQLITE_PR
379c0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
379d0 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71  3OsRandomness(sq
379e0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74  lite3_vfs *, int
379f0 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  , char *);.SQLIT
37a00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
37a10 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c  lite3OsSleep(sql
37a20 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29  ite3_vfs *, int)
37a30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
37a40 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75   int sqlite3OsCu
37a50 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65  rrentTime(sqlite
37a60 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a  3_vfs *, double*
37a70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e  );../*.** Conven
37a80 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73 20  ience functions 
37a90 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20  for opening and 
37aa0 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75 73  closing files us
37ab0 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ing .** sqlite3_
37ac0 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61  malloc() to obta
37ad0 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  in space for the
37ae0 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72   file-handle str
37af0 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
37b00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
37b10 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f  lite3OsOpenMallo
37b20 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  c(sqlite3_vfs *,
37b30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73   const char *, s
37b40 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20  qlite3_file **, 
37b50 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  int,int*);.SQLIT
37b60 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
37b70 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
37b80 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
37b90 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 4f 53  ;../*.** Each OS
37ba0 2d 73 70 65 63 69 66 69 63 20 62 61 63 6b 65 6e  -specific backen
37bb0 64 20 64 65 66 69 6e 65 73 20 61 6e 20 69 6e 73  d defines an ins
37bc0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
37bd0 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74  lowing.** struct
37be0 75 72 65 20 66 6f 72 20 72 65 74 75 72 6e 69 6e  ure for returnin
37bf0 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  g a pointer to i
37c00 74 73 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 20  ts sqlite3_vfs. 
37c10 20 49 66 20 4f 53 5f 4f 54 48 45 52 0a 2a 2a 20   If OS_OTHER.** 
37c20 69 73 20 64 65 66 69 6e 65 64 20 28 6d 65 61 6e  is defined (mean
37c30 69 6e 67 20 74 68 61 74 20 74 68 65 20 61 70 70  ing that the app
37c40 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
37c50 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61   OS interface la
37c60 79 65 72 0a 2a 2a 20 69 73 20 75 73 65 64 29 20  yer.** is used) 
37c70 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f  then there is no
37c80 20 64 65 66 61 75 6c 74 20 56 46 53 2e 20 20 20   default VFS.   
37c90 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
37ca0 6d 75 73 74 0a 2a 2a 20 72 65 67 69 73 74 65 72  must.** register
37cb0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 56 46 53   one or more VFS
37cc0 20 73 74 72 75 63 74 75 72 65 73 20 75 73 69 6e   structures usin
37cd0 67 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65  g sqlite3_vfs_re
37ce0 67 69 73 74 65 72 28 29 0a 2a 2a 20 62 65 66 6f  gister().** befo
37cf0 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  re attempting to
37d00 20 75 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a   use SQLite..*/.
37d10 23 69 66 20 4f 53 5f 55 4e 49 58 20 7c 7c 20 4f  #if OS_UNIX || O
37d20 53 5f 57 49 4e 20 7c 7c 20 4f 53 5f 4f 53 32 0a  S_WIN || OS_OS2.
37d30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
37d40 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
37d50 74 65 33 4f 73 44 65 66 61 75 6c 74 56 66 73 28  te3OsDefaultVfs(
37d60 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64  void);.#else.# d
37d70 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 44  efine sqlite3OsD
37d80 65 66 61 75 6c 74 56 66 73 28 58 29 20 30 0a 23  efaultVfs(X) 0.#
37d90 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
37da0 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a   _SQLITE_OS_H_ *
37db0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
37dc0 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a  ** End of os.h *
37dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37e00 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
37e10 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
37e20 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
37e30 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
37e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37e50 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
37e60 2a 2a 20 49 6e 63 6c 75 64 65 20 6d 75 74 65 78  ** Include mutex
37e70 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
37e80 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
37e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ea0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
37eb0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75  ** Begin file mu
37ec0 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tex.h **********
37ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ef0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75  */./*.** 2007 Au
37f00 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68  gust 28.**.** Th
37f10 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
37f20 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
37f30 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
37f40 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
37f50 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
37f60 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
37f70 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
37f80 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
37f90 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
37fa0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
37fb0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
37fc0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
37fd0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
37fe0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
37ff0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
38000 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
38010 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
38020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38060 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
38070 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
38080 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65  the common heade
38090 72 20 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20  r for all mutex 
380a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
380b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 49 6e  .** The sqliteIn
380c0 74 2e 68 20 68 65 61 64 65 72 20 23 69 6e 63 6c  t.h header #incl
380d0 75 64 65 73 20 74 68 69 73 20 66 69 6c 65 20 73  udes this file s
380e0 6f 20 74 68 61 74 20 69 74 20 69 73 20 61 76 61  o that it is ava
380f0 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c  ilable.** to all
38100 20 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 20 20   source files.  
38110 57 65 20 62 72 65 61 6b 20 69 74 20 6f 75 74 20  We break it out 
38120 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20  in an effort to 
38130 6b 65 65 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a  keep the code.**
38140 20 62 65 74 74 65 72 20 6f 72 67 61 6e 69 7a 65   better organize
38150 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20  d..**.** NOTE:  
38160 73 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f  source files sho
38170 75 6c 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75  uld *not* #inclu
38180 64 65 20 74 68 69 73 20 68 65 61 64 65 72 20 66  de this header f
38190 69 6c 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a  ile directly..**
381a0 20 53 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68   Source files sh
381b0 6f 75 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 68  ould #include th
381c0 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 66 69  e sqliteInt.h fi
381d0 6c 65 20 61 6e 64 20 6c 65 74 20 74 68 61 74 20  le and let that 
381e0 66 69 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20  file.** include 
381f0 74 68 69 73 20 6f 6e 65 20 69 6e 64 69 72 65 63  this one indirec
38200 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  tly..**.** $Id: 
38210 6d 75 74 65 78 2e 68 2c 76 20 31 2e 32 20 32 30  mutex.h,v 1.2 20
38220 30 37 2f 30 38 2f 33 30 20 31 34 3a 31 30 3a 33  07/08/30 14:10:3
38230 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  0 drh Exp $.*/..
38240 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
38250 55 54 45 58 5f 41 50 50 44 45 46 0a 2f 2a 0a 2a  UTEX_APPDEF./*.*
38260 2a 20 49 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  * If SQLITE_MUTE
38270 58 5f 41 50 50 44 45 46 20 69 73 20 64 65 66 69  X_APPDEF is defi
38280 6e 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 77  ned, then this w
38290 68 6f 6c 65 20 6d 6f 64 75 6c 65 20 69 73 0a 2a  hole module is.*
382a0 2a 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 65 71  * omitted and eq
382b0 75 69 76 61 6c 65 6e 74 20 66 75 6e 63 74 69 6f  uivalent functio
382c0 6e 61 6c 69 74 79 20 6d 75 73 74 20 62 65 20 70  nality must be p
382d0 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 0a 2a  rovided by the.*
382e0 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68  * application th
382f0 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74  at links against
38300 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
38310 61 72 79 2e 0a 2a 2f 0a 23 65 6c 73 65 0a 2f 2a  ary..*/.#else./*
38320 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77  .** Figure out w
38330 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  hat version of t
38340 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20  he code to use. 
38350 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65   The choices are
38360 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
38370 4d 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20  MUTEX_NOOP      
38380 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68     For single-th
38390 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69  readed applicati
383a0 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  ons that.**     
383b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383c0 20 20 20 20 20 20 20 20 64 6f 20 6e 6f 74 20 64          do not d
383d0 65 73 69 72 65 20 65 72 72 6f 72 20 63 68 65 63  esire error chec
383e0 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51  king..**.**   SQ
383f0 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 5f  LITE_MUTEX_NOOP_
38400 44 45 42 55 47 20 20 20 46 6f 72 20 73 69 6e 67  DEBUG   For sing
38410 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c  le-threaded appl
38420 69 63 61 74 69 6f 6e 73 20 77 69 74 68 0a 2a 2a  ications with.**
38430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38440 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72               err
38450 6f 72 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 68  or checking to h
38460 65 6c 70 20 76 65 72 69 66 79 20 74 68 61 74 20  elp verify that 
38470 6d 75 74 65 78 65 73 0a 2a 2a 20 20 20 20 20 20  mutexes.**      
38480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38490 20 20 20 20 20 20 20 61 72 65 20 62 65 69 6e 67         are being
384a0 20 75 73 65 64 20 63 6f 72 72 65 63 74 6c 79 20   used correctly 
384b0 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79  even though they
384c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
384d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
384e0 61 72 65 20 6e 6f 74 20 6e 65 65 64 65 64 2e 20  are not needed. 
384f0 20 55 73 65 64 20 77 68 65 6e 20 53 51 4c 49 54   Used when SQLIT
38500 45 5f 44 45 42 55 47 20 69 73 0a 2a 2a 20 20 20  E_DEBUG is.**   
38510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38520 20 20 20 20 20 20 20 20 20 20 64 65 66 69 6e 65            define
38530 64 20 6f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65  d on single-thre
38540 61 64 65 64 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a  aded builds..**.
38550 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  **   SQLITE_MUTE
38560 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46  X_PTHREADS     F
38570 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65  or multi-threade
38580 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f  d applications o
38590 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20  n Unix..**.**   
385a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32  SQLITE_MUTEX_W32
385b0 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75            For mu
385c0 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70  lti-threaded app
385d0 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e  lications on Win
385e0 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  32..**.**   SQLI
385f0 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20  TE_MUTEX_OS2    
38600 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d        For multi-
38610 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
38620 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a  tions on OS/2..*
38630 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
38640 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 31 20 20 20  _MUTEX_NOOP 1   
38650 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 2a  /* The default *
38660 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
38670 4c 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 21  LITE_DEBUG) && !
38680 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
38690 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45  E.# undef SQLITE
386a0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 64 65  _MUTEX_NOOP.# de
386b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
386c0 58 5f 4e 4f 4f 50 5f 44 45 42 55 47 0a 23 65 6e  X_NOOP_DEBUG.#en
386d0 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28  dif.#if defined(
386e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
386f0 50 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52  P) && SQLITE_THR
38700 45 41 44 53 41 46 45 20 26 26 20 4f 53 5f 55 4e  EADSAFE && OS_UN
38710 49 58 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  IX.# undef SQLIT
38720 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 64  E_MUTEX_NOOP.# d
38730 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
38740 45 58 5f 50 54 48 52 45 41 44 53 0a 23 65 6e 64  EX_PTHREADS.#end
38750 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  if.#if defined(S
38760 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50  QLITE_MUTEX_NOOP
38770 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45  ) && SQLITE_THRE
38780 41 44 53 41 46 45 20 26 26 20 4f 53 5f 57 49 4e  ADSAFE && OS_WIN
38790 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
387a0 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 64 65 66  MUTEX_NOOP.# def
387b0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
387c0 5f 57 33 32 0a 23 65 6e 64 69 66 0a 23 69 66 20  _W32.#endif.#if 
387d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
387e0 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 53 51  UTEX_NOOP) && SQ
387f0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
38800 26 26 20 4f 53 5f 4f 53 32 0a 23 20 75 6e 64 65  && OS_OS2.# unde
38810 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e  f SQLITE_MUTEX_N
38820 4f 4f 50 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  OOP.# define SQL
38830 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 65  ITE_MUTEX_OS2.#e
38840 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
38850 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 2f  ITE_MUTEX_NOOP./
38860 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20  *.** If this is 
38870 61 20 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e  a no-op implemen
38880 74 61 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e  tation, implemen
38890 74 20 65 76 65 72 79 74 68 69 6e 67 20 61 73 20  t everything as 
388a0 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69  macros..*/.#defi
388b0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
388c0 5f 61 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73  _alloc(X)    ((s
388d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29  qlite3_mutex*)8)
388e0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
388f0 5f 6d 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23  _mutex_free(X).#
38900 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
38910 75 74 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64  utex_enter(X).#d
38920 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
38930 74 65 78 5f 74 72 79 28 58 29 20 20 20 20 20 20  tex_try(X)      
38940 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e  SQLITE_OK.#defin
38950 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
38960 6c 65 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65  leave(X).#define
38970 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
38980 65 6c 64 28 58 29 20 20 20 20 20 31 0a 23 64 65  eld(X)     1.#de
38990 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
389a0 65 78 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31  ex_notheld(X)  1
389b0 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
389c0 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  /* SQLITE_MUTEX_
389d0 41 50 50 44 45 46 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  APPDEF */../****
389e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
389f0 66 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a  f mutex.h ******
38a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
38a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
38a40 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
38a50 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
38a60 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
38a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
38a80 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65 20  * Each database 
38a90 66 69 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73  file to be acces
38aa0 73 65 64 20 62 79 20 74 68 65 20 73 79 73 74 65  sed by the syste
38ab0 6d 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  m is an instance
38ac0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
38ad0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20  wing structure. 
38ae0 20 54 68 65 72 65 20 61 72 65 20 6e 6f 72 6d 61   There are norma
38af0 6c 6c 79 20 74 77 6f 20 6f 66 20 74 68 65 73 65  lly two of these
38b00 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 69   structures.** i
38b10 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 44 62  n the sqlite.aDb
38b20 5b 5d 20 61 72 72 61 79 2e 20 20 61 44 62 5b 30  [] array.  aDb[0
38b30 5d 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61  ] is the main da
38b40 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 0a  tabase file and.
38b50 2a 2a 20 61 44 62 5b 31 5d 20 69 73 20 74 68 65  ** aDb[1] is the
38b60 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75   database file u
38b70 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 65 6d 70  sed to hold temp
38b80 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 41  orary tables.  A
38b90 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74  dditional.** dat
38ba0 61 62 61 73 65 73 20 6d 61 79 20 62 65 20 61 74  abases may be at
38bb0 74 61 63 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  tached..*/.struc
38bc0 74 20 44 62 20 7b 0a 20 20 63 68 61 72 20 2a 7a  t Db {.  char *z
38bd0 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Name;         /*
38be0 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 64 61   Name of this da
38bf0 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 74 72 65  tabase */.  Btre
38c00 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20  e *pBt;         
38c10 20 2f 2a 20 54 68 65 20 42 2a 54 72 65 65 20 73   /* The B*Tree s
38c20 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 69  tructure for thi
38c30 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  s database file 
38c40 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 3b  */.  u8 inTrans;
38c50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 3a 20            /* 0: 
38c60 6e 6f 74 20 77 72 69 74 61 62 6c 65 2e 20 20 31  not writable.  1
38c70 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  : Transaction.  
38c80 32 3a 20 43 68 65 63 6b 70 6f 69 6e 74 20 2a 2f  2: Checkpoint */
38c90 0a 20 20 75 38 20 73 61 66 65 74 79 5f 6c 65 76  .  u8 safety_lev
38ca0 65 6c 3b 20 20 20 20 20 2f 2a 20 48 6f 77 20 61  el;     /* How a
38cb0 67 67 72 65 73 73 69 76 65 20 61 74 20 73 79 6e  ggressive at syn
38cc0 63 68 69 6e 67 20 64 61 74 61 20 74 6f 20 64 69  ching data to di
38cd0 73 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  sk */.  void *pA
38ce0 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ux;             
38cf0 20 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64    /* Auxiliary d
38d00 61 74 61 2e 20 20 55 73 75 61 6c 6c 79 20 4e 55  ata.  Usually NU
38d10 4c 4c 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  LL */.  void (*x
38d20 46 72 65 65 41 75 78 29 28 76 6f 69 64 2a 29 3b  FreeAux)(void*);
38d30 20 20 2f 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20    /* Routine to 
38d40 66 72 65 65 20 70 41 75 78 20 2a 2f 0a 20 20 53  free pAux */.  S
38d50 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20  chema *pSchema; 
38d60 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
38d70 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  o database schem
38d80 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61 72  a (possibly shar
38d90 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ed) */.};../*.**
38da0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
38db0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
38dc0 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 20 61  ructure stores a
38dd0 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
38de0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
38df0 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c 20   are no virtual 
38e00 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 65  tables configure
38e10 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d 61  d in this schema
38e20 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 2e  , the.** Schema.
38e30 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 20 73  db variable is s
38e40 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 65  et to NULL. Afte
38e50 72 20 74 68 65 20 66 69 72 73 74 20 76 69 72 74  r the first virt
38e60 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73  ual table.** has
38e70 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74 20   been added, it 
38e80 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
38e90 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
38ea0 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 75  connection .** u
38eb0 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  sed to create th
38ec0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e  e connection. On
38ed0 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  ce a virtual tab
38ee0 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61  le has been.** a
38ef0 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 68 65  dded to the Sche
38f00 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  ma structure and
38f10 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20 76   the Schema.db v
38f20 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 65  ariable populate
38f30 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 74  d, .** only that
38f40 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
38f50 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 65  tion may use the
38f60 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70 61   Schema to prepa
38f70 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  re .** statement
38f80 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 68  s..*/.struct Sch
38f90 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 65  ema {.  int sche
38fa0 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20  ma_cookie;   /* 
38fb0 44 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  Database schema 
38fc0 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 66  version number f
38fd0 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  or this file */.
38fe0 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b 20    Hash tblHash; 
38ff0 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61         /* All ta
39000 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79 20  bles indexed by 
39010 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 69  name */.  Hash i
39020 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f  dxHash;        /
39030 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 6e  * All (named) in
39040 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 62 79  dices indexed by
39050 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20   name */.  Hash 
39060 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 20 20  trigHash;       
39070 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20  /* All triggers 
39080 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20  indexed by name 
39090 2a 2f 0a 20 20 48 61 73 68 20 61 46 4b 65 79 3b  */.  Hash aFKey;
390a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
390b0 65 69 67 6e 20 6b 65 79 73 20 69 6e 64 65 78 65  eign keys indexe
390c0 64 20 62 79 20 74 6f 2d 74 61 62 6c 65 20 2a 2f  d by to-table */
390d0 0a 20 20 54 61 62 6c 65 20 2a 70 53 65 71 54 61  .  Table *pSeqTa
390e0 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73  b;      /* The s
390f0 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
39100 61 62 6c 65 20 75 73 65 64 20 62 79 20 41 55 54  able used by AUT
39110 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20  OINCREMENT */.  
39120 75 38 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20  u8 file_format; 
39130 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 66       /* Schema f
39140 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e 20 66 6f  ormat version fo
39150 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20  r this file */. 
39160 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20   u8 enc;        
39170 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 65 6e        /* Text en
39180 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 74  coding used by t
39190 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  his database */.
391a0 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20    u16 flags;    
391b0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
391c0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
391d0 74 68 69 73 20 73 63 68 65 6d 61 20 2a 2f 0a 20  this schema */. 
391e0 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 3b   int cache_size;
391f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
39200 6f 66 20 70 61 67 65 73 20 74 6f 20 75 73 65 20  of pages to use 
39210 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a  in the cache */.
39220 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
39230 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
39240 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
39250 20 20 20 20 20 20 20 20 2f 2a 20 22 4f 77 6e 65          /* "Owne
39260 72 22 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 53  r" connection. S
39270 65 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65  ee comment above
39280 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
39290 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f  *.** These macro
392a0 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
392b0 20 74 65 73 74 2c 20 73 65 74 2c 20 6f 72 20 63   test, set, or c
392c0 6c 65 61 72 20 62 69 74 73 20 69 6e 20 74 68 65  lear bits in the
392d0 20 0a 2a 2a 20 44 62 2e 66 6c 61 67 73 20 66 69   .** Db.flags fi
392e0 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  eld..*/.#define 
392f0 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 44 2c  DbHasProperty(D,
39300 49 2c 50 29 20 20 20 20 20 28 28 28 44 29 2d 3e  I,P)     (((D)->
39310 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[I].pSchema->
39320 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 50 29 29  flags&(P))==(P))
39330 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 41 6e  .#define DbHasAn
39340 79 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29  yProperty(D,I,P)
39350 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e    (((D)->aDb[I].
39360 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28  pSchema->flags&(
39370 50 29 29 21 3d 30 29 0a 23 64 65 66 69 6e 65 20  P))!=0).#define 
39380 44 62 53 65 74 50 72 6f 70 65 72 74 79 28 44 2c  DbSetProperty(D,
39390 49 2c 50 29 20 20 20 20 20 28 44 29 2d 3e 61 44  I,P)     (D)->aD
393a0 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c  b[I].pSchema->fl
393b0 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e 65  ags|=(P).#define
393c0 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79   DbClearProperty
393d0 28 44 2c 49 2c 50 29 20 20 20 28 44 29 2d 3e 61  (D,I,P)   (D)->a
393e0 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66  Db[I].pSchema->f
393f0 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a  lags&=~(P)../*.*
39400 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
39410 20 66 6f 72 20 74 68 65 20 44 42 2e 66 6c 61 67   for the DB.flag
39420 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54  s field..**.** T
39430 68 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  he DB_SchemaLoad
39440 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 61  ed flag is set a
39450 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73  fter the databas
39460 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 65  e schema has bee
39470 6e 0a 2a 2a 20 72 65 61 64 20 69 6e 74 6f 20 69  n.** read into i
39480 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62  nternal hash tab
39490 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 42 5f 55 6e  les..**.** DB_Un
394a0 72 65 73 65 74 56 69 65 77 73 20 6d 65 61 6e 73  resetViews means
394b0 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f 72   that one or mor
394c0 65 20 76 69 65 77 73 20 68 61 76 65 20 63 6f 6c  e views have col
394d0 75 6d 6e 20 6e 61 6d 65 73 20 74 68 61 74 0a 2a  umn names that.*
394e0 2a 20 68 61 76 65 20 62 65 65 6e 20 66 69 6c 6c  * have been fill
394f0 65 64 20 6f 75 74 2e 20 20 49 66 20 74 68 65 20  ed out.  If the 
39500 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20  schema changes, 
39510 74 68 65 73 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  these column nam
39520 65 73 20 6d 69 67 68 74 0a 2a 2a 20 63 68 61 6e  es might.** chan
39530 67 65 73 20 61 6e 64 20 73 6f 20 74 68 65 20 76  ges and so the v
39540 69 65 77 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f  iew will need to
39550 20 62 65 20 72 65 73 65 74 2e 0a 2a 2f 0a 23 64   be reset..*/.#d
39560 65 66 69 6e 65 20 44 42 5f 53 63 68 65 6d 61 4c  efine DB_SchemaL
39570 6f 61 64 65 64 20 20 20 20 30 78 30 30 30 31 20  oaded    0x0001 
39580 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61 20 68   /* The schema h
39590 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 2a  as been loaded *
395a0 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 55 6e 72  /.#define DB_Unr
395b0 65 73 65 74 56 69 65 77 73 20 20 20 20 30 78 30  esetViews    0x0
395c0 30 30 32 20 20 2f 2a 20 53 6f 6d 65 20 76 69 65  002  /* Some vie
395d0 77 73 20 68 61 76 65 20 64 65 66 69 6e 65 64 20  ws have defined 
395e0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a  column names */.
395f0 23 64 65 66 69 6e 65 20 44 42 5f 45 6d 70 74 79  #define DB_Empty
39600 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30             0x000
39610 34 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 69  4  /* The file i
39620 73 20 65 6d 70 74 79 20 28 6c 65 6e 67 74 68 20  s empty (length 
39630 30 20 62 79 74 65 73 29 20 2a 2f 0a 0a 0a 2f 2a  0 bytes) */.../*
39640 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73  .** Each databas
39650 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
39660 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
39670 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
39680 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 61  ** The sqlite.la
39690 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73 20  stRowid records 
396a0 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20  the last insert 
396b0 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64 20  rowid generated 
396c0 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 20  by an.** insert 
396d0 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 65  statement.  Inse
396e0 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f 20  rts on views do 
396f0 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73 20 76  not affect its v
39700 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 74  alue.  Each.** t
39710 72 69 67 67 65 72 20 68 61 73 20 69 74 73 20 6f  rigger has its o
39720 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 74  wn context, so t
39730 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63 61  hat lastRowid ca
39740 6e 20 62 65 20 75 70 64 61 74 65 64 20 69 6e 73  n be updated ins
39750 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 20  ide.** triggers 
39760 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20 70  as usual.  The p
39770 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77 69  revious value wi
39780 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 6f  ll be restored o
39790 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 72 0a  nce the trigger.
397a0 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e 20  ** exits.  Upon 
397b0 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f 72  entering a befor
397c0 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 20  e or instead of 
397d0 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f 77  trigger, lastRow
397e0 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67  id is no.** long
397f0 65 72 20 28 73 69 6e 63 65 20 61 66 74 65 72 20  er (since after 
39800 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 20  version 2.8.12) 
39810 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a  reset to -1..**.
39820 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e 43  ** The sqlite.nC
39830 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 63  hange does not c
39840 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69 74  ount changes wit
39850 68 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e 64  hin triggers and
39860 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e   keeps no.** con
39870 74 65 78 74 2e 20 20 49 74 20 69 73 20 72 65 73  text.  It is res
39880 65 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 73  et at start of s
39890 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a 20  qlite3_exec..** 
398a0 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 61  The sqlite.lsCha
398b0 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73 20 74  nge represents t
398c0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
398d0 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65  nges made by the
398e0 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 2c   last.** insert,
398f0 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c 65   update, or dele
39900 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49  te statement.  I
39910 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 61  t remains consta
39920 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68  nt throughout th
39930 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 61  e.** length of a
39940 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 69   statement and i
39950 73 20 74 68 65 6e 20 75 70 64 61 74 65 64 20 62  s then updated b
39960 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e 20  y OP_SetCounts. 
39970 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 63   It keeps a.** c
39980 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75 73  ontext stack jus
39990 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 64  t like lastRowid
399a0 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 75   so that the cou
399b0 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a  nt of changes.**
399c0 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65   within a trigge
399d0 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f 75  r is not seen ou
399e0 74 73 69 64 65 20 74 68 65 20 74 72 69 67 67 65  tside the trigge
399f0 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20 76  r.  Changes to v
39a00 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61  iews do not.** a
39a10 66 66 65 63 74 20 74 68 65 20 76 61 6c 75 65 20  ffect the value 
39a20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 20  of lsChange..** 
39a30 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68 61  The sqlite.csCha
39a40 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20  nge keeps track 
39a50 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
39a60 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65 73   current changes
39a70 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c   (since.** the l
39a80 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20 61  ast statement) a
39a90 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 75 70  nd is used to up
39aa0 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 68  date sqlite_lsCh
39ab0 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ange..**.** The 
39ac0 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73  member variables
39ad0 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 2c   sqlite.errCode,
39ae0 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 20   sqlite.zErrMsg 
39af0 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d  and sqlite.zErrM
39b00 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 68  sg16.** store th
39b10 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  e most recent er
39b20 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66  ror code and, if
39b30 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 72   applicable, str
39b40 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65  ing. The.** inte
39b50 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 71  rnal function sq
39b60 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69 73 20  lite3Error() is 
39b70 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 65 73  used to set thes
39b80 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 63  e variables.** c
39b90 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a  onsistently..*/.
39ba0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 7b  struct sqlite3 {
39bb0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
39bc0 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20  pVfs;           
39bd0 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 65   /* OS Interface
39be0 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20   */.  int nDb;  
39bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39c00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
39c10 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65 6e   backends curren
39c20 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20  tly in use */.  
39c30 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20 20  Db *aDb;        
39c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39c50 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a 2f   All backends */
39c60 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20  .  int flags;   
39c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39c80 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 6f 75 73   /* Miscellanous
39c90 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f   flags. See belo
39ca0 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46  w */.  int openF
39cb0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
39cc0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61       /* Flags pa
39cd0 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f  ssed to sqlite3_
39ce0 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20  vfs.xOpen() */. 
39cf0 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20   int errCode;   
39d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39d10 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
39d20 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45  ror code (SQLITE
39d30 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72  _*) */.  int err
39d40 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Mask;           
39d50 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75         /* & resu
39d60 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20 74 68  lt codes with th
39d70 69 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  is before return
39d80 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f  ing */.  u8 auto
39d90 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20  Commit;         
39da0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 75         /* The au
39db0 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20  to-commit flag. 
39dc0 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 74 6f  */.  u8 temp_sto
39dd0 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  re;             
39de0 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a     /* 1: file 2:
39df0 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 61 75   memory 0: defau
39e00 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f  lt */.  u8 mallo
39e10 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 20 20  cFailed;        
39e20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
39e30 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 61 20   we have seen a 
39e40 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a  malloc failure *
39e50 2f 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 3b 20  /.  int nTable; 
39e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39e70 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
39e80 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74  ables in the dat
39e90 61 62 61 73 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53  abase */.  CollS
39ea0 65 71 20 2a 70 44 66 6c 74 43 6f 6c 6c 3b 20 20  eq *pDfltColl;  
39eb0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
39ec0 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e  default collatin
39ed0 67 20 73 65 71 75 65 6e 63 65 20 28 42 49 4e 41  g sequence (BINA
39ee0 52 59 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73  RY) */.  i64 las
39ef0 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  tRowid;         
39f00 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20         /* ROWID 
39f10 6f 66 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69  of most recent i
39f20 6e 73 65 72 74 20 28 73 65 65 20 61 62 6f 76 65  nsert (see above
39f30 29 20 2a 2f 0a 20 20 69 36 34 20 70 72 69 6f 72  ) */.  i64 prior
39f40 4e 65 77 52 6f 77 69 64 3b 20 20 20 20 20 20 20  NewRowid;       
39f50 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 61 6e       /* Last ran
39f60 64 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65 64 20  domly generated 
39f70 52 4f 57 49 44 20 2a 2f 0a 20 20 69 6e 74 20 6d  ROWID */.  int m
39f80 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20  agic;           
39f90 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69           /* Magi
39fa0 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74  c number for det
39fb0 65 63 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75  ect library misu
39fc0 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61  se */.  int nCha
39fd0 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  nge;            
39fe0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72        /* Value r
39ff0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
3a000 65 33 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a  e3_changes() */.
3a010 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e    int nTotalChan
3a020 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
3a030 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65  /* Value returne
3a040 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74  d by sqlite3_tot
3a050 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a  al_changes() */.
3a060 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
3a070 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  *mutex;         
3a080 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75  /* Connection mu
3a090 74 65 78 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  tex */.  struct 
3a0a0 73 71 6c 69 74 65 33 49 6e 69 74 49 6e 66 6f 20  sqlite3InitInfo 
3a0b0 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  {      /* Inform
3a0c0 61 74 69 6f 6e 20 75 73 65 64 20 64 75 72 69 6e  ation used durin
3a0d0 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  g initialization
3a0e0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b   */.    int iDb;
3a0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a100 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b      /* When back
3a110 20 69 73 20 62 65 69 6e 67 20 69 6e 69 74 69 61   is being initia
3a120 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74  lized */.    int
3a130 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20 20 20   newTnum;       
3a140 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
3a150 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 62 65  page of table be
3a160 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  ing initialized 
3a170 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 79 3b 20  */.    u8 busy; 
3a180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a190 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 63 75     /* TRUE if cu
3a1a0 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69  rrently initiali
3a1b0 7a 69 6e 67 20 2a 2f 0a 20 20 7d 20 69 6e 69 74  zing */.  } init
3a1c0 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 69  ;.  int nExtensi
3a1d0 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  on;             
3a1e0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c    /* Number of l
3a1f0 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73  oaded extensions
3a200 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78   */.  void **aEx
3a210 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20  tension;        
3a220 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
3a230 73 68 61 72 65 64 20 6c 69 62 72 61 72 61 79 20  shared libraray 
3a240 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72  handles */.  str
3a250 75 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b  uct Vdbe *pVdbe;
3a260 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69             /* Li
3a270 73 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72  st of active vir
3a280 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f  tual machines */
3a290 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62  .  int activeVdb
3a2a0 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  eCnt;           
3a2b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 64   /* Number of vd
3a2c0 62 65 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78  bes currently ex
3a2d0 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69  ecuting */.  voi
3a2e0 64 20 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64  d (*xTrace)(void
3a2f0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20  *,const char*); 
3a300 20 20 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20         /* Trace 
3a310 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
3a320 69 64 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20  id *pTraceArg;  
3a330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a340 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
3a350 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61 63 65  ent to the trace
3a360 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
3a370 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28  oid (*xProfile)(
3a380 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
3a390 2a 2c 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66  *,u64);  /* Prof
3a3a0 69 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a  iling function *
3a3b0 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69  /.  void *pProfi
3a3c0 6c 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  leArg;          
3a3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a3e0 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f   Argument to pro
3a3f0 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  file function */
3a400 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74  .  void *pCommit
3a410 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
3a420 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
3a430 20 74 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62   to xCommitCallb
3a440 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e  ack() */   .  in
3a450 74 20 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62  t (*xCommitCallb
3a460 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20  ack)(void*);    
3a470 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76  /* Invoked at ev
3a480 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20  ery commit. */. 
3a490 20 76 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b   void *pRollback
3a4a0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
3a4b0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
3a4c0 6f 20 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  o xRollbackCallb
3a4d0 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f  ack() */   .  vo
3a4e0 69 64 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61  id (*xRollbackCa
3a4f0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20  llback)(void*); 
3a500 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76  /* Invoked at ev
3a510 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20  ery commit. */. 
3a520 20 76 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72   void *pUpdateAr
3a530 67 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64  g;.  void (*xUpd
3a540 61 74 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  ateCallback)(voi
3a550 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  d*,int, const ch
3a560 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
3a570 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20  sqlite_int64);. 
3a580 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64   void(*xCollNeed
3a590 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  ed)(void*,sqlite
3a5a0 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
3a5b0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
3a5c0 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65  void(*xCollNeede
3a5d0 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  d16)(void*,sqlit
3a5e0 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
3a5f0 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20  ,const void*);. 
3a600 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64   void *pCollNeed
3a610 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33  edArg;.  sqlite3
3a620 5f 76 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20  _value *pErr;   
3a630 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72         /* Most r
3a640 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73  ecent error mess
3a650 61 67 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  age */.  char *z
3a660 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20  ErrMsg;         
3a670 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72         /* Most r
3a680 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73  ecent error mess
3a690 61 67 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64  age (UTF-8 encod
3a6a0 65 64 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ed) */.  char *z
3a6b0 45 72 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20  ErrMsg16;       
3a6c0 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72         /* Most r
3a6d0 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73  ecent error mess
3a6e0 61 67 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f  age (UTF-16 enco
3a6f0 64 65 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20  ded) */.  union 
3a700 7b 0a 20 20 20 20 69 6e 74 20 69 73 49 6e 74 65  {.    int isInte
3a710 72 72 75 70 74 65 64 3b 20 20 20 20 20 20 20 20  rrupted;        
3a720 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c    /* True if sql
3a730 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68  ite3_interrupt h
3a740 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a  as been called *
3a750 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74  /.    double not
3a760 55 73 65 64 31 3b 20 20 20 20 20 20 20 20 20 20  Used1;          
3a770 20 20 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20    /* Spacer */. 
3a780 20 7d 20 75 31 3b 0a 23 69 66 6e 64 65 66 20 53   } u1;.#ifndef S
3a790 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
3a7a0 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28  RIZATION.  int (
3a7b0 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e  *xAuth)(void*,in
3a7c0 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
3a7d0 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
3a7e0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
3a7f0 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  *);.            
3a800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a810 20 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75      /* Access au
3a820 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63  thorization func
3a830 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
3a840 70 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20  pAuthArg;       
3a850 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61          /* 1st a
3a860 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61  rgument to the a
3a870 63 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74  ccess auth funct
3a880 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ion */.#endif.#i
3a890 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3a8a0 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
3a8b0 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f  ACK.  int (*xPro
3a8c0 67 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20  gress)(void *); 
3a8d0 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72      /* The progr
3a8e0 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ess callback */.
3a8f0 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73    void *pProgres
3a900 73 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  sArg;           
3a910 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
3a920 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c  he progress call
3a930 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50  back */.  int nP
3a940 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20  rogressOps;     
3a950 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3a960 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72  r of opcodes for
3a970 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
3a980 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  ck */.#endif.#if
3a990 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3a9a0 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
3a9b0 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20  Hash aModule;   
3a9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a9d0 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71   populated by sq
3a9e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
3a9f0 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65  ule() */.  Table
3aa00 20 2a 70 56 54 61 62 3b 20 20 20 20 20 20 20 20   *pVTab;        
3aa10 20 20 20 20 20 20 20 20 20 2f 2a 20 76 74 61 62           /* vtab
3aa20 20 77 69 74 68 20 61 63 74 69 76 65 20 43 6f 6e   with active Con
3aa30 6e 65 63 74 2f 43 72 65 61 74 65 20 6d 65 74 68  nect/Create meth
3aa40 6f 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  od */.  sqlite3_
3aa50 76 74 61 62 20 2a 2a 61 56 54 72 61 6e 73 3b 20  vtab **aVTrans; 
3aa60 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c        /* Virtual
3aa70 20 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65   tables with ope
3aa80 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a  n transactions *
3aa90 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b  /.  int nVTrans;
3aaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3aab0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73    /* Allocated s
3aac0 69 7a 65 20 6f 66 20 61 56 54 72 61 6e 73 20 2a  ize of aVTrans *
3aad0 2f 0a 23 65 6e 64 69 66 0a 20 20 48 61 73 68 20  /.#endif.  Hash 
3aae0 61 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20  aFunc;          
3aaf0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20           /* All 
3ab00 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63  functions that c
3ab10 61 6e 20 62 65 20 69 6e 20 53 51 4c 20 65 78 70  an be in SQL exp
3ab20 72 73 20 2a 2f 0a 20 20 48 61 73 68 20 61 43 6f  rs */.  Hash aCo
3ab30 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 20 20 20  llSeq;          
3ab40 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 6f 6c        /* All col
3ab50 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
3ab60 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c 65   */.  BusyHandle
3ab70 72 20 62 75 73 79 48 61 6e 64 6c 65 72 3b 20 20  r busyHandler;  
3ab80 20 20 20 20 2f 2a 20 42 75 73 79 20 63 61 6c 6c      /* Busy call
3ab90 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 62 75  back */.  int bu
3aba0 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 20 20  syTimeout;      
3abb0 20 20 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20          /* Busy 
3abc0 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 74 2c  handler timeout,
3abd0 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20 44 62   in msec */.  Db
3abe0 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b 20 20   aDbStatic[2];  
3abf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
3ac00 74 61 74 69 63 20 73 70 61 63 65 20 66 6f 72 20  tatic space for 
3ac10 74 68 65 20 32 20 64 65 66 61 75 6c 74 20 62 61  the 2 default ba
3ac20 63 6b 65 6e 64 73 20 2a 2f 0a 23 69 66 64 65 66  ckends */.#ifdef
3ac30 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 73 71   SQLITE_SSE.  sq
3ac40 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 65 74  lite3_stmt *pFet
3ac50 63 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ch;         /* U
3ac60 73 65 64 20 62 79 20 53 53 45 20 74 6f 20 66 65  sed by SSE to fe
3ac70 74 63 68 20 73 74 6f 72 65 64 20 73 74 61 74 65  tch stored state
3ac80 6d 65 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  ments */.#endif.
3ac90 20 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64    u8 dfltLockMod
3aca0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
3acb0 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63 6b 69  /* Default locki
3acc0 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74 74 61  ng-mode for atta
3acd0 63 68 65 64 20 64 62 73 20 2a 2f 0a 7d 3b 0a 0a  ched dbs */.};..
3ace0 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 74 6f  /*.** A macro to
3acf0 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 65 6e   discover the en
3ad00 63 6f 64 69 6e 67 20 6f 66 20 61 20 64 61 74 61  coding of a data
3ad10 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  base..*/.#define
3ad20 20 45 4e 43 28 64 62 29 20 28 28 64 62 29 2d 3e   ENC(db) ((db)->
3ad30 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[0].pSchema->
3ad40 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73  enc)../*.** Poss
3ad50 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20  ible values for 
3ad60 74 68 65 20 73 71 6c 69 74 65 2e 66 6c 61 67 73  the sqlite.flags
3ad70 20 61 6e 64 20 6f 72 20 44 62 2e 66 6c 61 67 73   and or Db.flags
3ad80 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f   fields..**.** O
3ad90 6e 20 73 71 6c 69 74 65 2e 66 6c 61 67 73 2c 20  n sqlite.flags, 
3ada0 74 68 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61  the SQLITE_InTra
3adb0 6e 73 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74  ns value means t
3adc0 68 61 74 20 77 65 20 68 61 76 65 0a 2a 2a 20 65  hat we have.** e
3add0 78 65 63 75 74 65 64 20 61 20 42 45 47 49 4e 2e  xecuted a BEGIN.
3ade0 20 20 4f 6e 20 44 62 2e 66 6c 61 67 73 2c 20 53    On Db.flags, S
3adf0 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 6d 65  QLITE_InTrans me
3ae00 61 6e 73 20 61 20 73 74 61 74 65 6d 65 6e 74 0a  ans a statement.
3ae10 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
3ae20 73 20 61 63 74 69 76 65 20 6f 6e 20 74 68 61 74  s active on that
3ae30 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
3ae40 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64  base file..*/.#d
3ae50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62  efine SQLITE_Vdb
3ae60 65 54 72 61 63 65 20 20 20 20 20 20 30 78 30 30  eTrace      0x00
3ae70 30 30 30 30 30 31 20 20 2f 2a 20 54 72 75 65 20  000001  /* True 
3ae80 74 6f 20 74 72 61 63 65 20 56 44 42 45 20 65 78  to trace VDBE ex
3ae90 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  ecution */.#defi
3aea0 6e 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e  ne SQLITE_InTran
3aeb0 73 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  s        0x00000
3aec0 30 30 38 20 20 2f 2a 20 54 72 75 65 20 69 66 20  008  /* True if 
3aed0 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
3aee0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3aef0 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
3af00 20 20 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a    0x00000010  /*
3af10 20 55 6e 63 6f 6d 6d 69 74 74 65 64 20 48 61 73   Uncommitted Has
3af20 68 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20  h table changes 
3af30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3af40 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20  E_FullColNames  
3af50 20 30 78 30 30 30 30 30 30 32 30 20 20 2f 2a 20   0x00000020  /* 
3af60 53 68 6f 77 20 66 75 6c 6c 20 63 6f 6c 75 6d 6e  Show full column
3af70 20 6e 61 6d 65 73 20 6f 6e 20 53 45 4c 45 43 54   names on SELECT
3af80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3af90 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
3afa0 20 20 30 78 30 30 30 30 30 30 34 30 20 20 2f 2a    0x00000040  /*
3afb0 20 53 68 6f 77 20 73 68 6f 72 74 20 63 6f 6c 75   Show short colu
3afc0 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65  mns names */.#de
3afd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e  fine SQLITE_Coun
3afe0 74 52 6f 77 73 20 20 20 20 20 20 30 78 30 30 30  tRows      0x000
3aff0 30 30 30 38 30 20 20 2f 2a 20 43 6f 75 6e 74 20  00080  /* Count 
3b000 72 6f 77 73 20 63 68 61 6e 67 65 64 20 62 79 20  rows changed by 
3b010 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 20 20 20 20  INSERT, */.     
3b020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b040 20 20 20 20 20 2f 2a 20 20 20 44 45 4c 45 54 45       /*   DELETE
3b050 2c 20 6f 72 20 55 50 44 41 54 45 20 61 6e 64 20  , or UPDATE and 
3b060 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 20 20  return */.      
3b070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b090 20 20 20 20 2f 2a 20 20 20 74 68 65 20 63 6f 75      /*   the cou
3b0a0 6e 74 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 62  nt using a callb
3b0b0 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ack. */.#define 
3b0c0 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62  SQLITE_NullCallb
3b0d0 61 63 6b 20 20 20 30 78 30 30 30 30 30 31 30 30  ack   0x00000100
3b0e0 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
3b0f0 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 69 66  callback once if
3b100 20 74 68 65 20 2a 2f 0a 20 20 20 20 20 20 20 20   the */.        
3b110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b130 20 20 2f 2a 20 20 20 72 65 73 75 6c 74 20 73 65    /*   result se
3b140 74 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64  t is empty */.#d
3b150 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 71 6c  efine SQLITE_Sql
3b160 54 72 61 63 65 20 20 20 20 20 20 20 30 78 30 30  Trace       0x00
3b170 30 30 30 32 30 30 20 20 2f 2a 20 44 65 62 75 67  000200  /* Debug
3b180 20 70 72 69 6e 74 20 53 51 4c 20 61 73 20 69 74   print SQL as it
3b190 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 23 64 65   executes */.#de
3b1a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 65  fine SQLITE_Vdbe
3b1b0 4c 69 73 74 69 6e 67 20 20 20 20 30 78 30 30 30  Listing    0x000
3b1c0 30 30 34 30 30 20 20 2f 2a 20 44 65 62 75 67 20  00400  /* Debug 
3b1d0 6c 69 73 74 69 6e 67 73 20 6f 66 20 56 44 42 45  listings of VDBE
3b1e0 20 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 23 64 65   programs */.#de
3b1f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 72 69 74  fine SQLITE_Writ
3b200 65 53 63 68 65 6d 61 20 20 20 20 30 78 30 30 30  eSchema    0x000
3b210 30 30 38 30 30 20 20 2f 2a 20 4f 4b 20 74 6f 20  00800  /* OK to 
3b220 75 70 64 61 74 65 20 53 51 4c 49 54 45 5f 4d 41  update SQLITE_MA
3b230 53 54 45 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20  STER */.#define 
3b240 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63  SQLITE_NoReadloc
3b250 6b 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30  k     0x00001000
3b260 20 20 2f 2a 20 52 65 61 64 6c 6f 63 6b 73 20 61    /* Readlocks a
3b270 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65 6e 20  re omitted when 
3b280 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3b290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b2a0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 63             ** ac
3b2b0 63 65 73 73 69 6e 67 20 72 65 61 64 2d 6f 6e 6c  cessing read-onl
3b2c0 79 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 23  y databases */.#
3b2d0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 67  define SQLITE_Ig
3b2e0 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30 78 30  noreChecks   0x0
3b2f0 30 30 30 32 30 30 30 20 20 2f 2a 20 44 6f 20 6e  0002000  /* Do n
3b300 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65 63 6b  ot enforce check
3b310 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
3b320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
3b330 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 30  eadUncommitted 0
3b340 78 30 30 30 30 34 30 30 30 20 2f 2a 20 46 6f 72  x00004000 /* For
3b350 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
3b360 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  de */.#define SQ
3b370 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46  LITE_LegacyFileF
3b380 6d 74 20 20 30 78 30 30 30 30 38 30 30 30 20 20  mt  0x00008000  
3b390 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 64 61  /* Create new da
3b3a0 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72 6d 61  tabases in forma
3b3b0 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t 1 */.#define S
3b3c0 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20  QLITE_FullFSync 
3b3d0 20 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20       0x00010000 
3b3e0 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 73 79   /* Use full fsy
3b3f0 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b 65 6e  nc on the backen
3b400 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
3b410 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
3b420 6e 20 20 30 78 30 30 30 32 30 30 30 30 20 20 2f  n  0x00020000  /
3b430 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f 65 78  * Enable load_ex
3b440 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a 23 64 65 66  tension */..#def
3b450 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76  ine SQLITE_Recov
3b460 65 72 79 4d 6f 64 65 20 20 20 30 78 30 30 30 34  eryMode   0x0004
3b470 30 30 30 30 20 20 2f 2a 20 49 67 6e 6f 72 65 20  0000  /* Ignore 
3b480 73 63 68 65 6d 61 20 65 72 72 6f 72 73 20 2a 2f  schema errors */
3b490 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3b4a0 53 68 61 72 65 64 43 61 63 68 65 20 20 20 20 30  SharedCache    0
3b4b0 78 30 30 30 38 30 30 30 30 20 20 2f 2a 20 43 61  x00080000  /* Ca
3b4c0 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 65  che sharing is e
3b4d0 6e 61 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e  nabled */.#defin
3b4e0 65 20 53 51 4c 49 54 45 5f 56 74 61 62 20 20 20  e SQLITE_Vtab   
3b4f0 20 20 20 20 20 20 20 20 30 78 30 30 31 30 30 30          0x001000
3b500 30 30 20 20 2f 2a 20 54 68 65 72 65 20 65 78 69  00  /* There exi
3b510 73 74 73 20 61 20 76 69 72 74 75 61 6c 20 74 61  sts a virtual ta
3b520 62 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f  ble */../*.** Po
3b530 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
3b540 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67  r the sqlite.mag
3b550 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65  ic field..** The
3b560 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74   numbers are obt
3b570 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20  ained at random 
3b580 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63  and have no spec
3b590 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68  ial meaning, oth
3b5a0 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67  er.** than being
3b5b0 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f   distinct from o
3b5c0 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23  ne another..*/.#
3b5d0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
3b5e0 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61  GIC_OPEN     0xa
3b5f0 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61  029a697  /* Data
3b600 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a  base is open */.
3b610 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
3b620 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78  AGIC_CLOSED   0x
3b630 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74  9f3c2d33  /* Dat
3b640 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20  abase is closed 
3b650 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3b660 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20 20  E_MAGIC_BUSY    
3b670 20 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a 20   0xf03b7906  /* 
3b680 44 61 74 61 62 61 73 65 20 63 75 72 72 65 6e 74  Database current
3b690 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64 65  ly in use */.#de
3b6a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49  fine SQLITE_MAGI
3b6b0 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35 33  C_ERROR    0xb53
3b6c0 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51 4c  57930  /* An SQL
3b6d0 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72  ITE_MISUSE error
3b6e0 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a   occurred */../*
3b6f0 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75 6e  .** Each SQL fun
3b700 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 64  ction is defined
3b710 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   by an instance 
3b720 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
3b730 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
3b740 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69  A pointer to thi
3b750 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73  s structure is s
3b760 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c  tored in the sql
3b770 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61 73  ite.aFunc.** has
3b780 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6d  h table.  When m
3b790 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f 6e  ultiple function
3b7a0 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20  s have the same 
3b7b0 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20 74  name, the hash t
3b7c0 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  able.** points t
3b7d0 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  o a linked list 
3b7e0 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75  of these structu
3b7f0 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46  res..*/.struct F
3b800 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20 6e  uncDef {.  i16 n
3b810 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
3b820 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67  /* Number of arg
3b830 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e  uments.  -1 mean
3b840 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a 20  s unlimited */. 
3b850 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20 20   u8 iPrefEnc;   
3b860 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72 72        /* Preferr
3b870 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ed text encoding
3b880 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 31   (SQLITE_UTF8, 1
3b890 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20 20  6LE, 16BE) */.  
3b8a0 75 38 20 6e 65 65 64 43 6f 6c 6c 53 65 71 3b 20  u8 needCollSeq; 
3b8b0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3b8c0 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f  sqlite3GetFuncCo
3b8d0 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 20 62 65  llSeq() might be
3b8e0 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 75 38 20   called */.  u8 
3b8f0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
3b900 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e    /* Some combin
3b910 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f  ation of SQLITE_
3b920 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64  FUNC_* */.  void
3b930 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20 20   *pUserData;    
3b940 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70 61   /* User data pa
3b950 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e  rameter */.  Fun
3b960 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20  cDef *pNext;    
3b970 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69    /* Next functi
3b980 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d  on with same nam
3b990 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46  e */.  void (*xF
3b9a0 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
3b9b0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
3b9c0 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52  3_value**); /* R
3b9d0 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  egular function 
3b9e0 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65  */.  void (*xSte
3b9f0 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
3ba00 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
3ba10 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67  value**); /* Agg
3ba20 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20  regate step */. 
3ba30 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a   void (*xFinaliz
3ba40 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
3ba50 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20  xt*);           
3ba60 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74       /* Aggregat
3ba70 65 20 66 69 6e 69 61 6c 69 7a 65 72 20 2a 2f 0a  e finializer */.
3ba80 20 20 63 68 61 72 20 7a 4e 61 6d 65 5b 31 5d 3b    char zName[1];
3ba90 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61         /* SQL na
3baa0 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  me of the functi
3bab0 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 4c 41 53  on.  MUST BE LAS
3bac0 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45  T */.};../*.** E
3bad0 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c  ach SQLite modul
3bae0 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e (virtual table
3baf0 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73 20   definition) is 
3bb00 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a 2a  defined by an.**
3bb10 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
3bb20 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
3bb30 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e 20  ture, stored in 
3bb40 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f 64  the sqlite3.aMod
3bb50 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c  ule.** hash tabl
3bb60 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f 64  e..*/.struct Mod
3bb70 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  ule {.  const sq
3bb80 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
3bb90 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20  odule;       /* 
3bba0 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65 72  Callback pointer
3bbb0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
3bbc0 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
3bbd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
3bbe0 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63 72  ame passed to cr
3bbf0 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f  eate_module() */
3bc00 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20  .  void *pAux;  
3bc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3bc20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78 20          /* pAux 
3bc30 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74 65  passed to create
3bc40 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76  _module() */.  v
3bc50 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28  oid (*xDestroy)(
3bc60 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20 20  void *);        
3bc70 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65      /* Module de
3bc80 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
3bc90 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50  n */.};../*.** P
3bca0 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66  ossible values f
3bcb0 6f 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73  or FuncDef.flags
3bcc0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
3bcd0 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 30  TE_FUNC_LIKE   0
3bce0 78 30 31 20 20 2f 2a 20 43 61 6e 64 69 64 61 74  x01  /* Candidat
3bcf0 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f  e for the LIKE o
3bd00 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23  ptimization */.#
3bd10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
3bd20 4e 43 5f 43 41 53 45 20 20 20 30 78 30 32 20 20  NC_CASE   0x02  
3bd30 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69 76  /* Case-sensitiv
3bd40 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e 63  e LIKE-type func
3bd50 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tion */.#define 
3bd60 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45  SQLITE_FUNC_EPHE
3bd70 4d 20 20 30 78 30 34 20 20 2f 2a 20 45 70 68 65  M  0x04  /* Ephe
3bd80 72 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65 20  rmeral.  Delete 
3bd90 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 0a 2f 2a  with VDBE */../*
3bda0 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
3bdb0 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d  about each colum
3bdc0 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c  n of an SQL tabl
3bdd0 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20  e is held in an 
3bde0 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74  instance.** of t
3bdf0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
3be00 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20  /.struct Column 
3be10 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
3be20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
3be30 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  this column */. 
3be40 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20   Expr *pDflt;   
3be50 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c    /* Default val
3be60 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d  ue of this colum
3be70 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79  n */.  char *zTy
3be80 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61 20  pe;     /* Data 
3be90 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 63 6f  type for this co
3bea0 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  lumn */.  char *
3beb0 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f  zColl;     /* Co
3bec0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
3bed0 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20  .  If NULL, use 
3bee0 74 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20  the default */. 
3bef0 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20   u8 notNull;    
3bf00 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
3bf10 72 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c  re is a NOT NULL
3bf20 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20   constraint */. 
3bf30 20 75 38 20 69 73 50 72 69 6d 4b 65 79 3b 20 20   u8 isPrimKey;  
3bf40 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
3bf50 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74  s column is part
3bf60 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20   of the PRIMARY 
3bf70 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72 20 61 66  KEY */.  char af
3bf80 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65  finity;   /* One
3bf90 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41   of the SQLITE_A
3bfa0 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f  FF_... values */
3bfb0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3bfc0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
3bfd0 45 0a 20 20 75 38 20 69 73 48 69 64 64 65 6e 3b  E.  u8 isHidden;
3bfe0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3bff0 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27  this column is '
3c000 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69  hidden' */.#endi
3c010 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43  f.};../*.** A "C
3c020 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e 63  ollating Sequenc
3c030 65 22 20 69 73 20 64 65 66 69 6e 65 64 20 62 79  e" is defined by
3c040 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
3c050 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
3c060 20 73 74 72 75 63 74 75 72 65 2e 20 43 6f 6e 63   structure. Conc
3c070 65 70 74 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c  eptually, a coll
3c080 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63  ating sequence c
3c090 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 6e 61 6d  onsists of a nam
3c0a0 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61  e and.** a compa
3c0b0 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68  rison routine th
3c0c0 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f  at defines the o
3c0d0 72 64 65 72 20 6f 66 20 74 68 61 74 20 73 65 71  rder of that seq
3c0e0 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  uence..**.** The
3c0f0 72 65 20 6d 61 79 20 74 77 6f 20 73 65 70 65 72  re may two seper
3c100 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ate implementati
3c110 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61  ons of the colla
3c120 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f  tion function, o
3c130 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72 6f 63 65  ne.** that proce
3c140 73 73 65 73 20 74 65 78 74 20 69 6e 20 55 54 46  sses text in UTF
3c150 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c  -8 encoding (Col
3c160 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e 64 20 61  lSeq.xCmp) and a
3c170 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a 2a 20 70  nother that.** p
3c180 72 6f 63 65 73 73 65 73 20 74 65 78 74 20 65 6e  rocesses text en
3c190 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 31 36 20  coded in UTF-16 
3c1a0 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29  (CollSeq.xCmp16)
3c1b0 2c 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 68  , using the mach
3c1c0 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79  ine.** native by
3c1d0 74 65 20 6f 72 64 65 72 2e 20 57 68 65 6e 20 61  te order. When a
3c1e0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3c1f0 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20  nce is invoked, 
3c200 53 51 4c 69 74 65 20 73 65 6c 65 63 74 73 0a 2a  SQLite selects.*
3c210 2a 20 74 68 65 20 76 65 72 73 69 6f 6e 20 74 68  * the version th
3c220 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20  at will require 
3c230 74 68 65 20 6c 65 61 73 74 20 65 78 70 65 6e 73  the least expens
3c240 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  ive encoding.** 
3c250 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66  translations, if
3c260 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   any..**.** The 
3c270 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 20 6d 65  CollSeq.pUser me
3c280 6d 62 65 72 20 76 61 72 69 61 62 6c 65 20 69 73  mber variable is
3c290 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d 65   an extra parame
3c2a0 74 65 72 20 74 68 61 74 20 70 61 73 73 65 64 20  ter that passed 
3c2b0 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72  in.** as the fir
3c2c0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
3c2d0 68 65 20 55 54 46 2d 38 20 63 6f 6d 70 61 72 69  he UTF-8 compari
3c2e0 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43  son function, xC
3c2f0 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70  mp..** CollSeq.p
3c300 55 73 65 72 31 36 20 69 73 20 74 68 65 20 65 71  User16 is the eq
3c310 75 69 76 61 6c 65 6e 74 20 66 6f 72 20 74 68 65  uivalent for the
3c320 20 55 54 46 2d 31 36 20 63 6f 6d 70 61 72 69 73   UTF-16 comparis
3c330 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20  on function,.** 
3c340 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66  xCmp16..**.** If
3c350 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43   both CollSeq.xC
3c360 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78  mp and CollSeq.x
3c370 43 6d 70 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20  Cmp16 are NULL, 
3c380 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
3c390 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73  e.** collating s
3c3a0 65 71 75 65 6e 63 65 20 69 73 20 75 6e 64 65 66  equence is undef
3c3b0 69 6e 65 64 2e 20 20 49 6e 64 69 63 65 73 20 62  ined.  Indices b
3c3c0 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66  uilt on an undef
3c3d0 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e  ined.** collatin
3c3e0 67 20 73 65 71 75 65 6e 63 65 20 6d 61 79 20 6e  g sequence may n
3c3f0 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72  ot be read or wr
3c400 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  itten..*/.struct
3c410 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61   CollSeq {.  cha
3c420 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
3c430 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
3c440 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
3c450 65 6e 63 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f  ence, UTF-8 enco
3c460 64 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b  ded */.  u8 enc;
3c470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3c480 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  * Text encoding 
3c490 68 61 6e 64 6c 65 64 20 62 79 20 78 43 6d 70 28  handled by xCmp(
3c4a0 29 20 2a 2f 0a 20 20 75 38 20 74 79 70 65 3b 20  ) */.  u8 type; 
3c4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3c4c0 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54  One of the SQLIT
3c4d0 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65  E_COLL_... value
3c4e0 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69  s below */.  voi
3c4f0 64 20 2a 70 55 73 65 72 3b 20 20 20 20 20 20 20  d *pUser;       
3c500 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
3c510 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28 29 20 2a  ment to xCmp() *
3c520 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29 28  /.  int (*xCmp)(
3c530 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74  void*,int, const
3c540 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e   void*, int, con
3c550 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  st void*);.  voi
3c560 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
3c570 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72  ;  /* Destructor
3c580 20 66 6f 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b   for pUser */.};
3c590 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
3c5a0 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65  values of CollSe
3c5b0 71 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66  q flags:.*/.#def
3c5c0 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  ine SQLITE_COLL_
3c5d0 42 49 4e 41 52 59 20 20 31 20 20 2f 2a 20 54 68  BINARY  1  /* Th
3c5e0 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 63 6d 70  e default memcmp
3c5f0 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  () collating seq
3c600 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65  uence */.#define
3c610 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43   SQLITE_COLL_NOC
3c620 41 53 45 20 20 32 20 20 2f 2a 20 54 68 65 20 62  ASE  2  /* The b
3c630 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 20 63  uilt-in NOCASE c
3c640 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
3c650 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
3c660 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 53 45  ITE_COLL_REVERSE
3c670 20 33 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74   3  /* The built
3c680 2d 69 6e 20 52 45 56 45 52 53 45 20 63 6f 6c 6c  -in REVERSE coll
3c690 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a  ating sequence *
3c6a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3c6b0 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20 30 20  _COLL_USER    0 
3c6c0 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 75 73   /* Any other us
3c6d0 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61  er-defined colla
3c6e0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
3c6f0 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 20 6f  ../*.** A sort o
3c700 72 64 65 72 20 63 61 6e 20 62 65 20 65 69 74 68  rder can be eith
3c710 65 72 20 41 53 43 20 6f 72 20 44 45 53 43 2e 0a  er ASC or DESC..
3c720 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3c730 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 20 20 30  E_SO_ASC       0
3c740 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63    /* Sort in asc
3c750 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a  ending order */.
3c760 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3c770 4f 5f 44 45 53 43 20 20 20 20 20 20 31 20 20 2f  O_DESC      1  /
3c780 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64  * Sort in ascend
3c790 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a  ing order */../*
3c7a0 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e  .** Column affin
3c7b0 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a  ity types..**.**
3c7c0 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 68   These used to h
3c7d0 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d  ave mnemonic nam
3c7e0 65 20 6c 69 6b 65 20 27 69 27 20 66 6f 72 20 53  e like 'i' for S
3c7f0 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
3c800 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66 6f 72  R and.** 't' for
3c810 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
3c820 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 61  .  But we can sa
3c830 76 65 20 61 20 6c 69 74 74 6c 65 20 73 70 61 63  ve a little spac
3c840 65 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a 2a 2a  e and improve.**
3c850 20 74 68 65 20 73 70 65 65 64 20 61 20 6c 69 74   the speed a lit
3c860 74 6c 65 20 62 79 20 6e 75 6d 62 65 72 20 74 68  tle by number th
3c870 65 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75  e values consecu
3c880 74 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20  tively.  .**.** 
3c890 42 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20  But rather than 
3c8a0 73 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 20  start with 0 or 
3c8b0 31 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 68  1, we begin with
3c8c0 20 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c   'a'.  That way,
3c8d0 0a 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c  .** when multipl
3c8e0 65 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73  e affinity types
3c8f0 20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65   are concatenate
3c900 64 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20  d into a string 
3c910 61 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74  and.** used as t
3c920 68 65 20 50 33 20 6f 70 65 72 61 6e 64 2c 20 74  he P3 operand, t
3c930 68 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65  hey will be more
3c940 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   readable..**.**
3c950 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20   Note also that 
3c960 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65  the numeric type
3c970 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f  s are grouped to
3c980 67 65 74 68 65 72 20 73 6f 20 74 68 61 74 20 74  gether so that t
3c990 65 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20  esting.** for a 
3c9a0 6e 75 6d 65 72 69 63 20 74 79 70 65 20 69 73 20  numeric type is 
3c9b0 61 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69  a single compari
3c9c0 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  son..*/.#define 
3c9d0 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
3c9e0 20 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20      'a'.#define 
3c9f0 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
3ca00 20 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20      'b'.#define 
3ca10 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
3ca20 49 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20  IC  'c'.#define 
3ca30 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
3ca40 45 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20  ER  'd'.#define 
3ca50 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20  SQLITE_AFF_REAL 
3ca60 20 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65      'e'..#define
3ca70 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69   sqlite3IsNumeri
3ca80 63 41 66 66 69 6e 69 74 79 28 58 29 20 20 28 28  cAffinity(X)  ((
3ca90 58 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  X)>=SQLITE_AFF_N
3caa0 55 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 45  UMERIC)../*.** E
3cab0 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69 73  ach SQL table is
3cac0 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20   represented in 
3cad0 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e 73  memory by an ins
3cae0 74 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20  tance of the.** 
3caf0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
3cb00 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65  ure..**.** Table
3cb10 2e 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61  .zName is the na
3cb20 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  me of the table.
3cb30 20 20 54 68 65 20 63 61 73 65 20 6f 66 20 74 68    The case of th
3cb40 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 52  e original.** CR
3cb50 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
3cb60 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c 20  ment is stored, 
3cb70 62 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74 20  but case is not 
3cb80 73 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 0a  significant for.
3cb90 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a  ** comparisons..
3cba0 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f 6c  **.** Table.nCol
3cbb0 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
3cbc0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69  f columns in thi
3cbd0 73 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 2e  s table.  Table.
3cbe0 61 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f 69  aCol is a.** poi
3cbf0 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79  nter to an array
3cc00 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75 63   of Column struc
3cc10 74 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20 65  tures, one for e
3cc20 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  ach column..**.*
3cc30 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 68  * If the table h
3cc40 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  as an INTEGER PR
3cc50 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20  IMARY KEY, then 
3cc60 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 74  Table.iPKey is t
3cc70 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74  he index of.** t
3cc80 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69  he column that i
3cc90 73 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f 74  s that key.   Ot
3cca0 68 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69 50  herwise Table.iP
3ccb0 4b 65 79 20 69 73 20 6e 65 67 61 74 69 76 65 2e  Key is negative.
3ccc0 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20 74    Note.** that t
3ccd0 68 65 20 64 61 74 61 74 79 70 65 20 6f 66 20 74  he datatype of t
3cce0 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6d  he PRIMARY KEY m
3ccf0 75 73 74 20 62 65 20 49 4e 54 45 47 45 52 20 66  ust be INTEGER f
3cd00 6f 72 20 74 68 69 73 20 66 69 65 6c 64 20 74 6f  or this field to
3cd10 0a 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e 20  .** be set.  An 
3cd20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
3cd30 4b 45 59 20 69 73 20 75 73 65 64 20 61 73 20 74  KEY is used as t
3cd40 68 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61 63  he rowid for eac
3cd50 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 20  h row of.** the 
3cd60 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61 62  table.  If a tab
3cd70 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 45  le has no INTEGE
3cd80 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74  R PRIMARY KEY, t
3cd90 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77  hen a random row
3cda0 69 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61 74  id.** is generat
3cdb0 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20  ed for each row 
3cdc0 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54  of the table.  T
3cdd0 61 62 6c 65 2e 68 61 73 50 72 69 6d 4b 65 79 20  able.hasPrimKey 
3cde0 69 73 20 74 72 75 65 20 69 66 0a 2a 2a 20 74 68  is true if.** th
3cdf0 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20  e table has any 
3ce00 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54  PRIMARY KEY, INT
3ce10 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73  EGER or otherwis
3ce20 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74  e..**.** Table.t
3ce30 6e 75 6d 20 69 73 20 74 68 65 20 70 61 67 65 20  num is the page 
3ce40 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72  number for the r
3ce50 6f 6f 74 20 42 54 72 65 65 20 70 61 67 65 20 6f  oot BTree page o
3ce60 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74  f the table in t
3ce70 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
3ce80 69 6c 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69  ile.  If Table.i
3ce90 44 62 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  Db is the index 
3cea0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
3ceb0 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a  table backend.**
3cec0 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d   in sqlite.aDb[]
3ced0 2e 20 20 30 20 69 73 20 66 6f 72 20 74 68 65 20  .  0 is for the 
3cee0 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e  main database an
3cef0 64 20 31 20 69 73 20 66 6f 72 20 74 68 65 20 66  d 1 is for the f
3cf00 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64  ile that.** hold
3cf10 73 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  s temporary tabl
3cf20 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20  es and indices. 
3cf30 20 49 66 20 54 61 62 6c 65 2e 69 73 45 70 68 65   If Table.isEphe
3cf40 6d 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68  m.** is true, th
3cf50 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  en the table is 
3cf60 73 74 6f 72 65 64 20 69 6e 20 61 20 66 69 6c 65  stored in a file
3cf70 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74   that is automat
3cf80 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a  ically deleted.*
3cf90 2a 20 77 68 65 6e 20 74 68 65 20 56 44 42 45 20  * when the VDBE 
3cfa0 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 74 61  cursor to the ta
3cfb0 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 20 20  ble is closed.  
3cfc0 49 6e 20 74 68 69 73 20 63 61 73 65 20 54 61 62  In this case Tab
3cfd0 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65  le.tnum .** refe
3cfe0 72 73 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  rs VDBE cursor n
3cff0 75 6d 62 65 72 20 74 68 61 74 20 68 6f 6c 64 73  umber that holds
3d000 20 74 68 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c   the table open,
3d010 20 6e 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f 74   not to the root
3d020 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  .** page number.
3d030 20 20 54 72 61 6e 73 69 65 6e 74 20 74 61 62 6c    Transient tabl
3d040 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 68  es are used to h
3d050 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 73 20  old the results 
3d060 6f 66 20 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72  of a.** sub-quer
3d070 79 20 74 68 61 74 20 61 70 70 65 61 72 73 20 69  y that appears i
3d080 6e 73 74 65 61 64 20 6f 66 20 61 20 72 65 61 6c  nstead of a real
3d090 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74   table name in t
3d0a0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a  he FROM clause .
3d0b0 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  ** of a SELECT s
3d0c0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72  tatement..*/.str
3d0d0 75 63 74 20 54 61 62 6c 65 20 7b 0a 20 20 63 68  uct Table {.  ch
3d0e0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f  ar *zName;     /
3d0f0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
3d100 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
3d110 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  l;        /* Num
3d120 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
3d130 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  n this table */.
3d140 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20    Column *aCol; 
3d150 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
3d160 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c  n about each col
3d170 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b  umn */.  int iPK
3d180 65 79 3b 20 20 20 20 20 20 20 2f 2a 20 49 66 20  ey;       /* If 
3d190 6e 6f 74 20 6c 65 73 73 20 74 68 65 6e 20 30 2c  not less then 0,
3d1a0 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79 5d   use aCol[iPKey]
3d1b0 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79 20   as the primary 
3d1c0 6b 65 79 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  key */.  Index *
3d1d0 70 49 6e 64 65 78 3b 20 20 20 2f 2a 20 4c 69 73  pIndex;   /* Lis
3d1e0 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 73  t of SQL indexes
3d1f0 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20   on this table. 
3d200 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20  */.  int tnum;  
3d210 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42 54        /* Root BT
3d220 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 74 68 69  ree node for thi
3d230 73 20 74 61 62 6c 65 20 28 73 65 65 20 6e 6f 74  s table (see not
3d240 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53 65  e above) */.  Se
3d250 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 2f  lect *pSelect; /
3d260 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 6c 65  * NULL for table
3d270 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 64 65  s.  Points to de
3d280 66 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 76 69  finition if a vi
3d290 65 77 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ew. */.  int nRe
3d2a0 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  f;          /* N
3d2b0 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72  umber of pointer
3d2c0 73 20 74 6f 20 74 68 69 73 20 54 61 62 6c 65 20  s to this Table 
3d2d0 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
3d2e0 72 69 67 67 65 72 3b 20 2f 2a 20 4c 69 73 74 20  rigger; /* List 
3d2f0 6f 66 20 53 51 4c 20 74 72 69 67 67 65 72 73 20  of SQL triggers 
3d300 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  on this table */
3d310 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20  .  FKey *pFKey; 
3d320 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20        /* Linked 
3d330 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65  list of all fore
3d340 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73  ign keys in this
3d350 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
3d360 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 2f   *zColAff;     /
3d370 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e  * String definin
3d380 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f  g the affinity o
3d390 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f  f each column */
3d3a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3d3b0 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70  OMIT_CHECK.  Exp
3d3c0 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20  r *pCheck;      
3d3d0 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 61 6c  /* The AND of al
3d3e0 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  l CHECK constrai
3d3f0 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  nts */.#endif.#i
3d400 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3d410 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 69  T_ALTERTABLE.  i
3d420 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b  nt addColOffset;
3d430 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 43    /* Offset in C
3d440 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
3d450 65 6d 65 6e 74 20 74 6f 20 61 64 64 20 61 20 6e  ement to add a n
3d460 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e  ew column */.#en
3d470 64 69 66 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c  dif.  u8 readOnl
3d480 79 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  y;     /* True i
3d490 66 20 74 68 69 73 20 74 61 62 6c 65 20 73 68 6f  f this table sho
3d4a0 75 6c 64 20 6e 6f 74 20 62 65 20 77 72 69 74 74  uld not be writt
3d4b0 65 6e 20 62 79 20 74 68 65 20 75 73 65 72 20 2a  en by the user *
3d4c0 2f 0a 20 20 75 38 20 69 73 45 70 68 65 6d 3b 20  /.  u8 isEphem; 
3d4d0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3d4e0 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 4f 50  created using OP
3d4f0 5f 4f 70 65 6e 45 70 68 65 72 6d 65 72 61 6c 20  _OpenEphermeral 
3d500 2a 2f 0a 20 20 75 38 20 68 61 73 50 72 69 6d 4b  */.  u8 hasPrimK
3d510 65 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ey;   /* True if
3d520 20 74 68 65 72 65 20 65 78 69 73 74 73 20 61 20   there exists a 
3d530 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20  primary key */. 
3d540 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20   u8 keyConf;    
3d550 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
3d560 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71 75  in case of uniqu
3d570 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f  eness conflict o
3d580 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 75 38 20  n iPKey */.  u8 
3d590 61 75 74 6f 49 6e 63 3b 20 20 20 20 20 20 2f 2a  autoInc;      /*
3d5a0 20 54 72 75 65 20 69 66 20 74 68 65 20 69 6e 74   True if the int
3d5b0 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79  eger primary key
3d5c0 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e   is autoincremen
3d5d0 74 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  t */.#ifndef SQL
3d5e0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
3d5f0 54 41 42 4c 45 0a 20 20 75 38 20 69 73 56 69 72  TABLE.  u8 isVir
3d600 74 75 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  tual;           
3d610 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
3d620 73 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74  s is a virtual t
3d630 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 73 43  able */.  u8 isC
3d640 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20  ommit;          
3d650 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65      /* True once
3d660 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c   the CREATE TABL
3d670 45 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 6d 69  E has been commi
3d680 74 74 65 64 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65  tted */.  Module
3d690 20 2a 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 20   *pMod;         
3d6a0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
3d6b0 6f 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  o the implementa
3d6c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75  tion of the modu
3d6d0 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  le */.  sqlite3_
3d6e0 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20  vtab *pVtab;    
3d6f0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
3d700 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e 73 74 61  the module insta
3d710 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f  nce */.  int nMo
3d720 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 20 20  duleArg;        
3d730 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
3d740 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
3d750 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61   module */.  cha
3d760 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b  r **azModuleArg;
3d770 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f         /* Text o
3d780 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 72 67  f all module arg
3d790 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 75 6c 65  s. [0] is module
3d7a0 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 66 0a   name */.#endif.
3d7b0 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
3d7c0 61 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 65 73  a;.};../*.** Tes
3d7d0 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72  t to see whether
3d7e0 20 6f 72 20 6e 6f 74 20 61 20 74 61 62 6c 65 20   or not a table 
3d7f0 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  is a virtual tab
3d800 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20  le.  This is.** 
3d810 64 6f 6e 65 20 61 73 20 61 20 6d 61 63 72 6f 20  done as a macro 
3d820 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20  so that it will 
3d830 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74  be optimized out
3d840 20 77 68 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a   when virtual.**
3d850 20 74 61 62 6c 65 20 73 75 70 70 6f 72 74 20 69   table support i
3d860 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74  s omitted from t
3d870 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66  he build..*/.#if
3d880 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3d890 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20  _VIRTUALTABLE.# 
3d8a0 20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61   define IsVirtua
3d8b0 6c 28 58 29 20 20 20 20 20 20 28 28 58 29 2d 3e  l(X)      ((X)->
3d8c0 69 73 56 69 72 74 75 61 6c 29 0a 23 20 20 64 65  isVirtual).#  de
3d8d0 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c  fine IsHiddenCol
3d8e0 75 6d 6e 28 58 29 20 28 28 58 29 2d 3e 69 73 48  umn(X) ((X)->isH
3d8f0 69 64 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20 20  idden).#else.#  
3d900 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c  define IsVirtual
3d910 28 58 29 20 20 20 20 20 20 30 0a 23 20 20 64 65  (X)      0.#  de
3d920 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c  fine IsHiddenCol
3d930 75 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a  umn(X) 0.#endif.
3d940 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65  ./*.** Each fore
3d950 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
3d960 6e 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  nt is an instanc
3d970 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
3d980 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
3d990 0a 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65  .** A foreign ke
3d9a0 79 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  y is associated 
3d9b0 77 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e  with two tables.
3d9c0 20 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62    The "from" tab
3d9d0 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62  le is.** the tab
3d9e0 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  le that contains
3d9f0 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20   the REFERENCES 
3da00 63 6c 61 75 73 65 20 74 68 61 74 20 63 72 65 61  clause that crea
3da10 74 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a  tes the foreign.
3da20 2a 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f  ** key.  The "to
3da30 22 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 74  " table is the t
3da40 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d  able that is nam
3da50 65 64 20 69 6e 20 74 68 65 20 52 45 46 45 52 45  ed in the REFERE
3da60 4e 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20  NCES clause..** 
3da70 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65 78  Consider this ex
3da80 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ample:.**.**    
3da90 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 65 78   CREATE TABLE ex
3daa0 31 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e  1(.**       a IN
3dab0 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
3dac0 59 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e  Y,.**       b IN
3dad0 54 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54  TEGER CONSTRAINT
3dae0 20 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20   fk1 REFERENCES 
3daf0 65 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b  ex2(x).**     );
3db00 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69  .**.** For forei
3db10 67 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68  gn key "fk1", th
3db20 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20  e from-table is 
3db30 22 65 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f  "ex1" and the to
3db40 2d 74 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e  -table is "ex2".
3db50 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45  .**.** Each REFE
3db60 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65  RENCES clause ge
3db70 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61  nerates an insta
3db80 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
3db90 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a  wing structure.*
3dba0 2a 20 77 68 69 63 68 20 69 73 20 61 74 74 61 63  * which is attac
3dbb0 68 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d  hed to the from-
3dbc0 74 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74  table.  The to-t
3dbd0 61 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78  able need not ex
3dbe0 69 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  ist when.** the 
3dbf0 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72  from-table is cr
3dc00 65 61 74 65 64 2e 20 20 54 68 65 20 65 78 69 73  eated.  The exis
3dc10 74 61 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d  tance of the to-
3dc20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65  table is not che
3dc30 63 6b 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 61 6e  cked.** until an
3dc40 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
3dc50 20 74 6f 20 69 6e 73 65 72 74 20 64 61 74 61 20   to insert data 
3dc60 69 6e 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74 61  into the from-ta
3dc70 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ble..**.** The s
3dc80 71 6c 69 74 65 2e 61 46 4b 65 79 20 68 61 73 68  qlite.aFKey hash
3dc90 20 74 61 62 6c 65 20 73 74 6f 72 65 73 20 70 6f   table stores po
3dca0 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73  inters to this s
3dcb0 74 72 75 63 74 75 72 65 0a 2a 2a 20 67 69 76 65  tructure.** give
3dcc0 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  n the name of a 
3dcd0 74 6f 2d 74 61 62 6c 65 2e 20 20 46 6f 72 20 65  to-table.  For e
3dce0 61 63 68 20 74 6f 2d 74 61 62 6c 65 2c 20 61 6c  ach to-table, al
3dcf0 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 0a 2a  l foreign keys.*
3dd00 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
3dd10 68 20 74 68 61 74 20 74 61 62 6c 65 20 61 72 65  h that table are
3dd20 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   on a linked lis
3dd30 74 20 75 73 69 6e 67 20 74 68 65 20 46 4b 65 79  t using the FKey
3dd40 2e 70 4e 65 78 74 54 6f 0a 2a 2a 20 66 69 65 6c  .pNextTo.** fiel
3dd50 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 4b 65  d..*/.struct FKe
3dd60 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 46 72  y {.  Table *pFr
3dd70 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 74  om;     /* The t
3dd80 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 73 74 61  able that consta
3dd90 69 6e 73 20 74 68 65 20 52 45 46 45 52 45 4e 43  ins the REFERENC
3dda0 45 53 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 46  ES clause */.  F
3ddb0 4b 65 79 20 2a 70 4e 65 78 74 46 72 6f 6d 3b 20  Key *pNextFrom; 
3ddc0 20 2f 2a 20 4e 65 78 74 20 66 6f 72 65 69 67 6e   /* Next foreign
3ddd0 20 6b 65 79 20 69 6e 20 70 46 72 6f 6d 20 2a 2f   key in pFrom */
3dde0 0a 20 20 63 68 61 72 20 2a 7a 54 6f 3b 20 20 20  .  char *zTo;   
3ddf0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
3de00 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 6b  table that the k
3de10 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a  ey points to */.
3de20 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 54 6f 3b    FKey *pNextTo;
3de30 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 65      /* Next fore
3de40 69 67 6e 20 6b 65 79 20 74 68 61 74 20 70 6f 69  ign key that poi
3de50 6e 74 73 20 74 6f 20 7a 54 6f 20 2a 2f 0a 20 20  nts to zTo */.  
3de60 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20  int nCol;       
3de70 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
3de80 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 6b  olumns in this k
3de90 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  ey */.  struct s
3dea0 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70  ColMap {  /* Map
3deb0 70 69 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  ping of columns 
3dec0 69 6e 20 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75  in pFrom to colu
3ded0 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20  mns in zTo */.  
3dee0 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20    int iFrom;    
3def0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
3df00 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d   column in pFrom
3df10 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 43   */.    char *zC
3df20 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  ol;        /* Na
3df30 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20  me of column in 
3df40 7a 54 6f 2e 20 20 49 66 20 30 20 75 73 65 20 50  zTo.  If 0 use P
3df50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20  RIMARY KEY */.  
3df60 7d 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20  } *aCol;        
3df70 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66    /* One entry f
3df80 6f 72 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20  or each of nCol 
3df90 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a 20 20 75 38  column s */.  u8
3dfa0 20 69 73 44 65 66 65 72 72 65 64 3b 20 20 20 20   isDeferred;    
3dfb0 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 6e 73 74  /* True if const
3dfc0 72 61 69 6e 74 20 63 68 65 63 6b 69 6e 67 20 69  raint checking i
3dfd0 73 20 64 65 66 65 72 72 65 64 20 74 69 6c 6c 20  s deferred till 
3dfe0 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 75 38 20 75  COMMIT */.  u8 u
3dff0 70 64 61 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a  pdateConf;    /*
3e000 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20   How to resolve 
3e010 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f  conflicts that o
3e020 63 63 75 72 20 6f 6e 20 55 50 44 41 54 45 20 2a  ccur on UPDATE *
3e030 2f 0a 20 20 75 38 20 64 65 6c 65 74 65 43 6f 6e  /.  u8 deleteCon
3e040 66 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20  f;    /* How to 
3e050 72 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74  resolve conflict
3e060 73 20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20  s that occur on 
3e070 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 69  DELETE */.  u8 i
3e080 6e 73 65 72 74 43 6f 6e 66 3b 20 20 20 20 2f 2a  nsertConf;    /*
3e090 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20   How to resolve 
3e0a0 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f  conflicts that o
3e0b0 63 63 75 72 20 6f 6e 20 49 4e 53 45 52 54 20 2a  ccur on INSERT *
3e0c0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69  /.};../*.** SQLi
3e0d0 74 65 20 73 75 70 70 6f 72 74 73 20 6d 61 6e 79  te supports many
3e0e0 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20   different ways 
3e0f0 74 6f 20 72 65 73 6f 6c 76 65 20 61 20 63 6f 6e  to resolve a con
3e100 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e  traint.** error.
3e110 20 20 52 4f 4c 4c 42 41 43 4b 20 70 72 6f 63 65    ROLLBACK proce
3e120 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74  ssing means that
3e130 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69   a constraint vi
3e140 6f 6c 61 74 69 6f 6e 0a 2a 2a 20 63 61 75 73 65  olation.** cause
3e150 73 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  s the operation 
3e160 69 6e 20 70 72 6f 63 65 73 73 20 74 6f 20 66 61  in process to fa
3e170 69 6c 20 61 6e 64 20 66 6f 72 20 74 68 65 20 63  il and for the c
3e180 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
3e190 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c  on.** to be roll
3e1a0 65 64 20 62 61 63 6b 2e 20 20 41 42 4f 52 54 20  ed back.  ABORT 
3e1b0 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73  processing means
3e1c0 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69   the operation i
3e1d0 6e 20 70 72 6f 63 65 73 73 0a 2a 2a 20 66 61 69  n process.** fai
3e1e0 6c 73 20 61 6e 64 20 61 6e 79 20 70 72 69 6f 72  ls and any prior
3e1f0 20 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 74 68   changes from th
3e200 61 74 20 6f 6e 65 20 6f 70 65 72 61 74 69 6f 6e  at one operation
3e210 20 61 72 65 20 62 61 63 6b 65 64 20 6f 75 74 2c   are backed out,
3e220 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 72 61 6e  .** but the tran
3e230 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72  saction is not r
3e240 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 46 41 49  olled back.  FAI
3e250 4c 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61  L processing mea
3e260 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6f  ns that.** the o
3e270 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67  peration in prog
3e280 72 65 73 73 20 73 74 6f 70 73 20 61 6e 64 20 72  ress stops and r
3e290 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20  eturns an error 
3e2a0 63 6f 64 65 2e 20 20 42 75 74 20 70 72 69 6f 72  code.  But prior
3e2b0 0a 2a 2a 20 63 68 61 6e 67 65 73 20 64 75 65 20  .** changes due 
3e2c0 74 6f 20 74 68 65 20 73 61 6d 65 20 6f 70 65 72  to the same oper
3e2d0 61 74 69 6f 6e 20 61 72 65 20 6e 6f 74 20 62 61  ation are not ba
3e2e0 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 6e 6f 20  cked out and no 
3e2f0 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75  rollback.** occu
3e300 72 73 2e 20 20 49 47 4e 4f 52 45 20 6d 65 61 6e  rs.  IGNORE mean
3e310 73 20 74 68 61 74 20 74 68 65 20 70 61 72 74 69  s that the parti
3e320 63 75 6c 61 72 20 72 6f 77 20 74 68 61 74 20 63  cular row that c
3e330 61 75 73 65 64 20 74 68 65 20 63 6f 6e 73 74 72  aused the constr
3e340 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 69 73  aint.** error is
3e350 20 6e 6f 74 20 69 6e 73 65 72 74 65 64 20 6f 72   not inserted or
3e360 20 75 70 64 61 74 65 64 2e 20 20 50 72 6f 63 65   updated.  Proce
3e370 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20  ssing continues 
3e380 61 6e 64 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20  and no error.** 
3e390 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 52 45  is returned.  RE
3e3a0 50 4c 41 43 45 20 6d 65 61 6e 73 20 74 68 61 74  PLACE means that
3e3b0 20 70 72 65 65 78 69 73 74 69 6e 67 20 64 61 74   preexisting dat
3e3c0 61 62 61 73 65 20 72 6f 77 73 20 74 68 61 74 20  abase rows that 
3e3d0 63 61 75 73 65 64 0a 2a 2a 20 61 20 55 4e 49 51  caused.** a UNIQ
3e3e0 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69  UE constraint vi
3e3f0 6f 6c 61 74 69 6f 6e 20 61 72 65 20 72 65 6d 6f  olation are remo
3e400 76 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ved so that the 
3e410 6e 65 77 20 69 6e 73 65 72 74 20 6f 72 0a 2a 2a  new insert or.**
3e420 20 75 70 64 61 74 65 20 63 61 6e 20 70 72 6f 63   update can proc
3e430 65 65 64 2e 20 20 50 72 6f 63 65 73 73 69 6e 67  eed.  Processing
3e440 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e   continues and n
3e450 6f 20 65 72 72 6f 72 20 69 73 20 72 65 70 6f 72  o error is repor
3e460 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 45 53 54 52  ted..**.** RESTR
3e470 49 43 54 2c 20 53 45 54 4e 55 4c 4c 2c 20 61 6e  ICT, SETNULL, an
3e480 64 20 43 41 53 43 41 44 45 20 61 63 74 69 6f 6e  d CASCADE action
3e490 73 20 61 70 70 6c 79 20 6f 6e 6c 79 20 74 6f 20  s apply only to 
3e4a0 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2a  foreign keys..**
3e4b0 20 52 45 53 54 52 49 43 54 20 69 73 20 74 68 65   RESTRICT is the
3e4c0 20 73 61 6d 65 20 61 73 20 41 42 4f 52 54 20 66   same as ABORT f
3e4d0 6f 72 20 49 4d 4d 45 44 49 41 54 45 20 66 6f 72  or IMMEDIATE for
3e4e0 65 69 67 6e 20 6b 65 79 73 20 61 6e 64 20 74 68  eign keys and th
3e4f0 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20 52 4f 4c  e.** same as ROL
3e500 4c 42 41 43 4b 20 66 6f 72 20 44 45 46 45 52 52  LBACK for DEFERR
3e510 45 44 20 6b 65 79 73 2e 20 20 53 45 54 4e 55 4c  ED keys.  SETNUL
3e520 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  L means that the
3e530 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20   foreign.** key 
3e540 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20  is set to NULL. 
3e550 20 43 41 53 43 41 44 45 20 6d 65 61 6e 73 20 74   CASCADE means t
3e560 68 61 74 20 61 20 44 45 4c 45 54 45 20 6f 72 20  hat a DELETE or 
3e570 55 50 44 41 54 45 20 6f 66 20 74 68 65 0a 2a 2a  UPDATE of the.**
3e580 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c   referenced tabl
3e590 65 20 72 6f 77 20 69 73 20 70 72 6f 70 61 67 61  e row is propaga
3e5a0 74 65 64 20 69 6e 74 6f 20 74 68 65 20 72 6f 77  ted into the row
3e5b0 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 0a   that holds the.
3e5c0 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 0a  ** foreign key..
3e5d0 2a 2a 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ** .** The follo
3e5e0 77 69 6e 67 20 73 79 6d 62 6f 6c 69 63 20 76 61  wing symbolic va
3e5f0 6c 75 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  lues are used to
3e600 20 72 65 63 6f 72 64 20 77 68 69 63 68 20 74 79   record which ty
3e610 70 65 0a 2a 2a 20 6f 66 20 61 63 74 69 6f 6e 20  pe.** of action 
3e620 74 6f 20 74 61 6b 65 2e 0a 2a 2f 0a 23 64 65 66  to take..*/.#def
3e630 69 6e 65 20 4f 45 5f 4e 6f 6e 65 20 20 20 20 20  ine OE_None     
3e640 30 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20  0   /* There is 
3e650 6e 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f  no constraint to
3e660 20 63 68 65 63 6b 20 2a 2f 0a 23 64 65 66 69 6e   check */.#defin
3e670 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 31 20  e OE_Rollback 1 
3e680 20 20 2f 2a 20 46 61 69 6c 20 74 68 65 20 6f 70    /* Fail the op
3e690 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 6f 6c 6c  eration and roll
3e6a0 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63  back the transac
3e6b0 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tion */.#define 
3e6c0 4f 45 5f 41 62 6f 72 74 20 20 20 20 32 20 20 20  OE_Abort    2   
3e6d0 2f 2a 20 42 61 63 6b 20 6f 75 74 20 63 68 61 6e  /* Back out chan
3e6e0 67 65 73 20 62 75 74 20 64 6f 20 6e 6f 20 72 6f  ges but do no ro
3e6f0 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74 69  llback transacti
3e700 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45  on */.#define OE
3e710 5f 46 61 69 6c 20 20 20 20 20 33 20 20 20 2f 2a  _Fail     3   /*
3e720 20 53 74 6f 70 20 74 68 65 20 6f 70 65 72 61 74   Stop the operat
3e730 69 6f 6e 20 62 75 74 20 6c 65 61 76 65 20 61 6c  ion but leave al
3e740 6c 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20  l prior changes 
3e750 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 49 67  */.#define OE_Ig
3e760 6e 6f 72 65 20 20 20 34 20 20 20 2f 2a 20 49 67  nore   4   /* Ig
3e770 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e 20  nore the error. 
3e780 44 6f 20 6e 6f 74 20 64 6f 20 74 68 65 20 49 4e  Do not do the IN
3e790 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20 2a  SERT or UPDATE *
3e7a0 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 65 70  /.#define OE_Rep
3e7b0 6c 61 63 65 20 20 35 20 20 20 2f 2a 20 44 65 6c  lace  5   /* Del
3e7c0 65 74 65 20 65 78 69 73 74 69 6e 67 20 72 65 63  ete existing rec
3e7d0 6f 72 64 2c 20 74 68 65 6e 20 64 6f 20 49 4e 53  ord, then do INS
3e7e0 45 52 54 20 6f 72 20 55 50 44 41 54 45 20 2a 2f  ERT or UPDATE */
3e7f0 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 65 73  ..#define OE_Res
3e800 74 72 69 63 74 20 36 20 20 20 2f 2a 20 4f 45 5f  trict 6   /* OE_
3e810 41 62 6f 72 74 20 66 6f 72 20 49 4d 4d 45 44 49  Abort for IMMEDI
3e820 41 54 45 2c 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b  ATE, OE_Rollback
3e830 20 66 6f 72 20 44 45 46 45 52 52 45 44 20 2a 2f   for DEFERRED */
3e840 0a 23 64 65 66 69 6e 65 20 4f 45 5f 53 65 74 4e  .#define OE_SetN
3e850 75 6c 6c 20 20 37 20 20 20 2f 2a 20 53 65 74 20  ull  7   /* Set 
3e860 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
3e870 76 61 6c 75 65 20 74 6f 20 4e 55 4c 4c 20 2a 2f  value to NULL */
3e880 0a 23 64 65 66 69 6e 65 20 4f 45 5f 53 65 74 44  .#define OE_SetD
3e890 66 6c 74 20 20 38 20 20 20 2f 2a 20 53 65 74 20  flt  8   /* Set 
3e8a0 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
3e8b0 76 61 6c 75 65 20 74 6f 20 69 74 73 20 64 65 66  value to its def
3e8c0 61 75 6c 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ault */.#define 
3e8d0 4f 45 5f 43 61 73 63 61 64 65 20 20 39 20 20 20  OE_Cascade  9   
3e8e0 2f 2a 20 43 61 73 63 61 64 65 20 74 68 65 20 63  /* Cascade the c
3e8f0 68 61 6e 67 65 73 20 2a 2f 0a 0a 23 64 65 66 69  hanges */..#defi
3e900 6e 65 20 4f 45 5f 44 65 66 61 75 6c 74 20 20 39  ne OE_Default  9
3e910 39 20 20 2f 2a 20 44 6f 20 77 68 61 74 65 76 65  9  /* Do whateve
3e920 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 61 63  r the default ac
3e930 74 69 6f 6e 20 69 73 20 2a 2f 0a 0a 0a 2f 2a 0a  tion is */.../*.
3e940 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
3e950 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3e960 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73  structure is pas
3e970 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
3e980 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  .** argument to 
3e990 73 71 6c 69 74 65 33 56 64 62 65 4b 65 79 43 6f  sqlite3VdbeKeyCo
3e9a0 6d 70 61 72 65 20 61 6e 64 20 69 73 20 75 73 65  mpare and is use
3e9b0 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65  d to control the
3e9c0 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20   .** comparison 
3e9d0 6f 66 20 74 68 65 20 74 77 6f 20 69 6e 64 65 78  of the two index
3e9e0 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   keys..**.** If 
3e9f0 74 68 65 20 4b 65 79 49 6e 66 6f 2e 69 6e 63 72  the KeyInfo.incr
3ea00 4b 65 79 20 76 61 6c 75 65 20 69 73 20 74 72 75  Key value is tru
3ea10 65 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 61 72  e and the compar
3ea20 69 73 6f 6e 20 77 6f 75 6c 64 0a 2a 2a 20 6f 74  ison would.** ot
3ea30 68 65 72 77 69 73 65 20 62 65 20 65 71 75 61 6c  herwise be equal
3ea40 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 61 20  , then return a 
3ea50 72 65 73 75 6c 74 20 61 73 20 69 66 20 74 68 65  result as if the
3ea60 20 73 65 63 6f 6e 64 20 6b 65 79 0a 2a 2a 20 77   second key.** w
3ea70 65 72 65 20 6c 61 72 67 65 72 2e 0a 2a 2f 0a 73  ere larger..*/.s
3ea80 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a  truct KeyInfo {.
3ea90 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
3eaa0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
3eab0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3eac0 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20   */.  u8 enc;   
3ead0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
3eae0 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65  t encoding - one
3eaf0 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74 66   of the TEXT_Utf
3eb00 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38  * values */.  u8
3eb10 20 69 6e 63 72 4b 65 79 3b 20 20 20 20 20 20 20   incrKey;       
3eb20 20 20 2f 2a 20 49 6e 63 72 65 61 73 65 20 32 6e    /* Increase 2n
3eb30 64 20 6b 65 79 20 62 79 20 65 70 73 69 6c 6f 6e  d key by epsilon
3eb40 20 62 65 66 6f 72 65 20 63 6f 6d 70 61 72 69 73   before comparis
3eb50 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65  on */.  int nFie
3eb60 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ld;         /* N
3eb70 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
3eb80 20 69 6e 20 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20   in aColl[] */. 
3eb90 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b   u8 *aSortOrder;
3eba0 20 20 20 20 20 2f 2a 20 49 66 20 64 65 66 69 6e       /* If defin
3ebb0 65 64 20 61 6e 20 61 53 6f 72 74 4f 72 64 65 72  ed an aSortOrder
3ebc0 5b 69 5d 20 69 73 20 74 72 75 65 2c 20 73 6f 72  [i] is true, sor
3ebd0 74 20 44 45 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c  t DESC */.  Coll
3ebe0 53 65 71 20 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20  Seq *aColl[1];  
3ebf0 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
3ec00 75 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74  uence for each t
3ec10 65 72 6d 20 6f 66 20 74 68 65 20 6b 65 79 20 2a  erm of the key *
3ec20 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  /.};../*.** Each
3ec30 20 53 51 4c 20 69 6e 64 65 78 20 69 73 20 72 65   SQL index is re
3ec40 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d  presented in mem
3ec50 6f 72 79 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73  ory by an.** ins
3ec60 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
3ec70 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
3ec80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75  ..**.** The colu
3ec90 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  mns of the table
3eca0 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65 20   that are to be 
3ecb0 69 6e 64 65 78 65 64 20 61 72 65 20 64 65 73 63  indexed are desc
3ecc0 72 69 62 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  ribed.** by the 
3ecd0 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64  aiColumn[] field
3ece0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
3ecf0 72 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  re.  For example
3ed00 2c 20 73 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20  , suppose.** we 
3ed10 68 61 76 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  have the followi
3ed20 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64  ng table and ind
3ed30 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52  ex:.**.**     CR
3ed40 45 41 54 45 20 54 41 42 4c 45 20 45 78 31 28 63  EATE TABLE Ex1(c
3ed50 31 20 69 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63  1 int, c2 int, c
3ed60 33 20 74 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20  3 text);.**     
3ed70 43 52 45 41 54 45 20 49 4e 44 45 58 20 45 78 32  CREATE INDEX Ex2
3ed80 20 4f 4e 20 45 78 31 28 63 33 2c 63 31 29 3b 0a   ON Ex1(c3,c1);.
3ed90 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 54 61 62  **.** In the Tab
3eda0 6c 65 20 73 74 72 75 63 74 75 72 65 20 64 65 73  le structure des
3edb0 63 72 69 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f  cribing Ex1, nCo
3edc0 6c 3d 3d 33 20 62 65 63 61 75 73 65 20 74 68 65  l==3 because the
3edd0 72 65 20 61 72 65 0a 2a 2a 20 74 68 72 65 65 20  re are.** three 
3ede0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74  columns in the t
3edf0 61 62 6c 65 2e 20 20 49 6e 20 74 68 65 20 49 6e  able.  In the In
3ee00 64 65 78 20 73 74 72 75 63 74 75 72 65 20 64 65  dex structure de
3ee10 73 63 72 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c  scribing.** Ex2,
3ee20 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63   nColumn==2 sinc
3ee30 65 20 32 20 6f 66 20 74 68 65 20 33 20 63 6f 6c  e 2 of the 3 col
3ee40 75 6d 6e 73 20 6f 66 20 45 78 31 20 61 72 65 20  umns of Ex1 are 
3ee50 69 6e 64 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20  indexed..** The 
3ee60 76 61 6c 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d  value of aiColum
3ee70 6e 20 69 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69  n is {2, 0}.  ai
3ee80 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63  Column[0]==2 bec
3ee90 61 75 73 65 20 74 68 65 20 0a 2a 2a 20 66 69 72  ause the .** fir
3eea0 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20  st column to be 
3eeb0 69 6e 64 65 78 65 64 20 28 63 33 29 20 68 61 73  indexed (c3) has
3eec0 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 32 20 69   an index of 2 i
3eed0 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a  n Ex1.aCol[]..**
3eee0 20 54 68 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75   The second colu
3eef0 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  mn to be indexed
3ef00 20 28 63 31 29 20 68 61 73 20 61 6e 20 69 6e 64   (c1) has an ind
3ef10 65 78 20 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78  ex of 0 in.** Ex
3ef20 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20  1.aCol[], hence 
3ef30 45 78 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d  Ex2.aiColumn[1]=
3ef40 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e  =0..**.** The In
3ef50 64 65 78 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c  dex.onError fiel
3ef60 64 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  d determines whe
3ef70 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
3ef80 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a  indexed columns.
3ef90 2a 2a 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75  ** must be uniqu
3efa0 65 20 61 6e 64 20 77 68 61 74 20 74 6f 20 64 6f  e and what to do
3efb0 20 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74   if they are not
3efc0 2e 20 20 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e  .  When Index.on
3efd0 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a  Error=OE_None,.*
3efe0 2a 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20  * it means this 
3eff0 69 73 20 6e 6f 74 20 61 20 75 6e 69 71 75 65 20  is not a unique 
3f000 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 69 73  index.  Otherwis
3f010 65 20 69 74 20 69 73 20 61 20 75 6e 69 71 75 65  e it is a unique
3f020 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68   index.** and th
3f030 65 20 76 61 6c 75 65 20 6f 66 20 49 6e 64 65 78  e value of Index
3f040 2e 6f 6e 45 72 72 6f 72 20 69 6e 64 69 63 61 74  .onError indicat
3f050 65 20 74 68 65 20 77 68 69 63 68 20 63 6f 6e 66  e the which conf
3f060 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
3f070 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f  .** algorithm to
3f080 20 65 6d 70 6c 6f 79 20 77 68 65 6e 65 76 65 72   employ whenever
3f090 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
3f0a0 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 61 20  ade to insert a 
3f0b0 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c  non-unique.** el
3f0c0 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74  ement..*/.struct
3f0d0 20 49 6e 64 65 78 20 7b 0a 20 20 63 68 61 72 20   Index {.  char 
3f0e0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e  *zName;     /* N
3f0f0 61 6d 65 20 6f 66 20 74 68 69 73 20 69 6e 64 65  ame of this inde
3f100 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  x */.  int nColu
3f110 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  mn;     /* Numbe
3f120 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
3f130 74 68 65 20 74 61 62 6c 65 20 75 73 65 64 20 62  the table used b
3f140 79 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a  y this index */.
3f150 20 20 69 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b    int *aiColumn;
3f160 20 20 20 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75     /* Which colu
3f170 6d 6e 73 20 61 72 65 20 75 73 65 64 20 62 79 20  mns are used by 
3f180 74 68 69 73 20 69 6e 64 65 78 2e 20 20 31 73 74  this index.  1st
3f190 20 69 73 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67   is 0 */.  unsig
3f1a0 6e 65 64 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f  ned *aiRowEst; /
3f1b0 2a 20 52 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c  * Result of ANAL
3f1c0 59 5a 45 3a 20 45 73 74 2e 20 72 6f 77 73 20 73  YZE: Est. rows s
3f1d0 65 6c 65 63 74 65 64 20 62 79 20 65 61 63 68 20  elected by each 
3f1e0 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c  column */.  Tabl
3f1f0 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20  e *pTable;   /* 
3f200 54 68 65 20 53 51 4c 20 74 61 62 6c 65 20 62 65  The SQL table be
3f210 69 6e 67 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20  ing indexed */. 
3f220 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 20   int tnum;      
3f230 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69    /* Page contai
3f240 6e 69 6e 67 20 72 6f 6f 74 20 6f 66 20 74 68 69  ning root of thi
3f250 73 20 69 6e 64 65 78 20 69 6e 20 64 61 74 61 62  s index in datab
3f260 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38  ase file */.  u8
3f270 20 6f 6e 45 72 72 6f 72 3b 20 20 20 20 20 20 2f   onError;      /
3f280 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49  * OE_Abort, OE_I
3f290 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63  gnore, OE_Replac
3f2a0 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f  e, or OE_None */
3f2b0 0a 20 20 75 38 20 61 75 74 6f 49 6e 64 65 78 3b  .  u8 autoIndex;
3f2c0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
3f2d0 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
3f2e0 63 72 65 61 74 65 64 20 28 65 78 3a 20 62 79 20  created (ex: by 
3f2f0 55 4e 49 51 55 45 29 20 2a 2f 0a 20 20 63 68 61  UNIQUE) */.  cha
3f300 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a  r *zColAff;   /*
3f310 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67   String defining
3f320 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
3f330 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   each column */.
3f340 20 20 49 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20    Index *pNext; 
3f350 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69     /* The next i
3f360 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  ndex associated 
3f370 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 74 61  with the same ta
3f380 62 6c 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20  ble */.  Schema 
3f390 2a 70 53 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68  *pSchema; /* Sch
3f3a0 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ema containing t
3f3b0 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75  his index */.  u
3f3c0 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20  8 *aSortOrder;  
3f3d0 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 69 7a 65  /* Array of size
3f3e0 20 49 6e 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20   Index.nColumn. 
3f3f0 54 72 75 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73  True==DESC, Fals
3f400 65 3d 3d 41 53 43 20 2a 2f 0a 20 20 63 68 61 72  e==ASC */.  char
3f410 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20   **azColl;   /* 
3f420 41 72 72 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69  Array of collati
3f430 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
3f440 73 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d  s for index */.}
3f450 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f  ;../*.** Each to
3f460 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f  ken coming out o
3f470 66 20 74 68 65 20 6c 65 78 65 72 20 69 73 20 61  f the lexer is a
3f480 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a  n instance of.**
3f490 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
3f4a0 20 20 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73    Tokens are als
3f4b0 6f 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f  o used as part o
3f4c0 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e  f an expression.
3f4d0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54  .**.** Note if T
3f4e0 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54  oken.z==0 then T
3f4f0 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b  oken.dyn and Tok
3f500 65 6e 2e 6e 20 61 72 65 20 75 6e 64 65 66 69 6e  en.n are undefin
3f510 65 64 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f  ed and.** may co
3f520 6e 74 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c  ntain random val
3f530 75 65 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b  ues.  Do not mak
3f540 65 20 61 6e 79 20 61 73 73 75 70 74 69 6f 6e 73  e any assuptions
3f550 20 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e   about Token.dyn
3f560 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20  .** and Token.n 
3f570 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e  when Token.z==0.
3f580 0a 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e  .*/.struct Token
3f590 20 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67   {.  const unsig
3f5a0 6e 65 64 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20  ned char *z; /* 
3f5b0 54 65 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65  Text of the toke
3f5c0 6e 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72  n.  Not NULL-ter
3f5d0 6d 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e  minated! */.  un
3f5e0 73 69 67 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b  signed dyn  : 1;
3f5f0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
3f600 72 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72  r malloced memor
3f610 79 2c 20 66 61 6c 73 65 20 66 6f 72 20 73 74 61  y, false for sta
3f620 74 69 63 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  tic */.  unsigne
3f630 64 20 6e 20 20 20 20 3a 20 33 31 3b 20 20 20 20  d n    : 31;    
3f640 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
3f650 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73  aracters in this
3f660 20 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   token */.};../*
3f670 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
3f680 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
3f690 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72  e contains infor
3f6a0 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f  mation needed to
3f6b0 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64   generate.** cod
3f6c0 65 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74  e for a SELECT t
3f6d0 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67  hat contains agg
3f6e0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
3f6f0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e  ..**.** If Expr.
3f700 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  op==TK_AGG_COLUM
3f710 4e 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43  N or TK_AGG_FUNC
3f720 54 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70  TION then Expr.p
3f730 41 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20  AggInfo is a.** 
3f740 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
3f750 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
3f760 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65  Expr.iColumn fie
3f770 6c 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  ld is the index 
3f780 69 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43  in.** AggInfo.aC
3f790 6f 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e  ol[] or AggInfo.
3f7a0 61 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72  aFunc[] of infor
3f7b0 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f  mation needed to
3f7c0 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64   generate.** cod
3f7d0 65 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e  e for that node.
3f7e0 0a 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70  .**.** AggInfo.p
3f7f0 47 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49  GroupBy and AggI
3f800 6e 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20  nfo.aFunc.pExpr 
3f810 70 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20  point to fields 
3f820 77 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72  within the.** or
3f830 69 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74  iginal Select st
3f840 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73  ructure that des
3f850 63 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43  cribes the SELEC
3f860 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  T statement.  Th
3f870 65 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f  ese.** fields do
3f880 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
3f890 66 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c  freed when deall
3f8a0 6f 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49  ocating the AggI
3f8b0 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  nfo structure..*
3f8c0 2f 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f  /.struct AggInfo
3f8d0 20 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f   {.  u8 directMo
3f8e0 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  de;          /* 
3f8f0 44 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67  Direct rendering
3f900 20 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65   mode means take
3f910 20 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20   data directly. 
3f920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f930 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d           ** from
3f940 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72   source tables r
3f950 61 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20  ather than from 
3f960 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a  accumulators */.
3f970 20 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49    u8 useSortingI
3f980 64 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20  dx;       /* In 
3f990 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66  direct mode, ref
3f9a0 65 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69  erence the sorti
3f9b0 6e 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a  ng index rather.
3f9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f9d0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61            ** tha
3f9e0 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62  n the source tab
3f9f0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74  le */.  int sort
3fa00 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20  ingIdx;         
3fa10 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
3fa20 20 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20   of the sorting 
3fa30 69 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c  index */.  ExprL
3fa40 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20  ist *pGroupBy;  
3fa50 20 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20     /* The group 
3fa60 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  by clause */.  i
3fa70 6e 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  nt nSortingColum
3fa80 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n;     /* Number
3fa90 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
3faa0 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
3fab0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67   */.  struct Agg
3fac0 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a  Info_col {    /*
3fad0 20 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   For each column
3fae0 20 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20   used in source 
3faf0 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61  tables */.    Ta
3fb00 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
3fb10 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
3fb20 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e   table */.    in
3fb30 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20  t iTable;       
3fb40 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
3fb50 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73   number of the s
3fb60 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20  ource table */. 
3fb70 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20     int iColumn; 
3fb80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
3fb90 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74  olumn number wit
3fba0 68 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74  hin the source t
3fbb0 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  able */.    int 
3fbc0 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20  iSorterColumn;  
3fbd0 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
3fbe0 75 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72  umber in the sor
3fbf0 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20  ting index */.  
3fc00 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20    int iMem;     
3fc10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
3fc20 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68  mory location th
3fc30 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d  at acts as accum
3fc40 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78  ulator */.    Ex
3fc50 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
3fc60 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72         /* The or
3fc70 69 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f  iginal expressio
3fc80 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a  n */.  } *aCol;.
3fc90 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
3fca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3fcb0 62 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72  ber of used entr
3fcc0 69 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f  ies in aCol[] */
3fcd0 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c  .  int nColumnAl
3fce0 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  loc;       /* Nu
3fcf0 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
3fd00 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c  located for aCol
3fd10 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63  [] */.  int nAcc
3fd20 75 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20  umulator;       
3fd30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
3fd40 75 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74  umns that show t
3fd50 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75  hrough to the ou
3fd60 74 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20  tput..          
3fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fd80 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f  ** Additional co
3fd90 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f  lumns are used o
3fda0 6e 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72  nly as parameter
3fdb0 73 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20  s to.           
3fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
3fdd0 2a 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  * aggregate func
3fde0 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63  tions */.  struc
3fdf0 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b  t AggInfo_func {
3fe00 20 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61     /* For each a
3fe10 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
3fe20 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70  n */.    Expr *p
3fe30 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
3fe40 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
3fe50 65 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e  encoding the fun
3fe60 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e  ction */.    Fun
3fe70 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20  cDef *pFunc;    
3fe80 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67        /* The agg
3fe90 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
3fea0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
3feb0 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20  /.    int iMem; 
3fec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3fed0 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  * Memory locatio
3fee0 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61  n that acts as a
3fef0 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20  ccumulator */.  
3ff00 20 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b    int iDistinct;
3ff10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70             /* Ep
3ff20 68 65 72 6d 65 72 61 6c 20 74 61 62 6c 65 20 75  hermeral table u
3ff30 73 65 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44  sed to enforce D
3ff40 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a  ISTINCT */.  } *
3ff50 61 46 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75  aFunc;.  int nFu
3ff60 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
3ff70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
3ff80 74 72 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d  tries in aFunc[]
3ff90 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41   */.  int nFuncA
3ffa0 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a  lloc;         /*
3ffb0 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   Number of slots
3ffc0 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61   allocated for a
3ffd0 46 75 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  Func[] */.};../*
3ffe0 0a 2a 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f 66  .** Each node of
3fff0 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69   an expression i
40000 6e 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65  n the parse tree
40010 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a   is an instance.
40020 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ** of this struc
40030 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72  ture..**.** Expr
40040 2e 6f 70 20 69 73 20 74 68 65 20 6f 70 63 6f 64  .op is the opcod
40050 65 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72 20  e.  The integer 
40060 70 61 72 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64  parser token cod
40070 65 73 20 61 72 65 20 72 65 75 73 65 64 0a 2a 2a  es are reused.**
40080 20 61 73 20 6f 70 63 6f 64 65 73 20 68 65 72 65   as opcodes here
40090 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
400a0 74 68 65 20 70 61 72 73 65 72 20 64 65 66 69 6e  the parser defin
400b0 65 73 20 54 4b 5f 47 45 20 74 6f 20 62 65 20 61  es TK_GE to be a
400c0 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64  n integer.** cod
400d0 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  e representing t
400e0 68 65 20 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72  he ">=" operator
400f0 2e 20 20 54 68 69 73 20 73 61 6d 65 20 69 6e 74  .  This same int
40100 65 67 65 72 20 63 6f 64 65 20 69 73 20 72 65 75  eger code is reu
40110 73 65 64 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73  sed.** to repres
40120 65 6e 74 20 74 68 65 20 67 72 65 61 74 65 72 2d  ent the greater-
40130 74 68 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f  than-or-equal-to
40140 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 74 68 65   operator in the
40150 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74   expression.** t
40160 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e  ree..**.** Expr.
40170 70 52 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e  pRight and Expr.
40180 70 4c 65 66 74 20 61 72 65 20 73 75 62 65 78 70  pLeft are subexp
40190 72 65 73 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e  ressions.  Expr.
401a0 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 0a  pList is a list.
401b0 2a 2a 20 6f 66 20 61 72 67 75 6d 65 6e 74 20 69  ** of argument i
401c0 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
401d0 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a   is a function..
401e0 2a 2a 0a 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e  **.** Expr.token
401f0 20 69 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72   is the operator
40200 20 74 6f 6b 65 6e 20 66 6f 72 20 74 68 69 73 20   token for this 
40210 6e 6f 64 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20  node.  For some 
40220 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74  expressions.** t
40230 68 61 74 20 68 61 76 65 20 73 75 62 65 78 70 72  hat have subexpr
40240 65 73 73 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f  essions, Expr.to
40250 6b 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 63  ken can be the c
40260 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 74 68 61  omplete text tha
40270 74 20 67 61 76 65 0a 2a 2a 20 72 69 73 65 20 74  t gave.** rise t
40280 6f 20 74 68 65 20 45 78 70 72 2e 20 20 49 6e 20  o the Expr.  In 
40290 74 68 65 20 6c 61 74 74 65 72 20 63 61 73 65 2c  the latter case,
402a0 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61   the token is ma
402b0 72 6b 65 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a  rked as being.**
402c0 20 61 20 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65   a compound toke
402d0 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72  n..**.** An expr
402e0 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f  ession of the fo
402f0 72 6d 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72  rm ID or ID.ID r
40300 65 66 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d  efers to a colum
40310 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a  n in a table..**
40320 20 46 6f 72 20 73 75 63 68 20 65 78 70 72 65 73   For such expres
40330 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69  sions, Expr.op i
40340 73 20 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55  s set to TK_COLU
40350 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62  MN and Expr.iTab
40360 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74  le is.** the int
40370 65 67 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62  eger cursor numb
40380 65 72 20 6f 66 20 61 20 56 44 42 45 20 63 75 72  er of a VDBE cur
40390 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  sor pointing to 
403a0 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a  that table and.*
403b0 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69  * Expr.iColumn i
403c0 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d  s the column num
403d0 62 65 72 20 66 6f 72 20 74 68 65 20 73 70 65 63  ber for the spec
403e0 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66  ific column.  If
403f0 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69   the.** expressi
40400 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 61 20  on is used as a 
40410 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67  result in an agg
40420 72 65 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74  regate SELECT, t
40430 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65  hen the.** value
40440 20 69 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20   is also stored 
40450 69 6e 20 74 68 65 20 45 78 70 72 2e 69 41 67 67  in the Expr.iAgg
40460 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61   column in the a
40470 67 67 72 65 67 61 74 65 20 73 6f 20 74 68 61 74  ggregate so that
40480 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63  .** it can be ac
40490 63 65 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c  cessed after all
404a0 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20   aggregates are 
404b0 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  computed..**.** 
404c0 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
404d0 6e 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c  n is a function,
404e0 20 74 68 65 20 45 78 70 72 2e 69 54 61 62 6c 65   the Expr.iTable
404f0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63   is an integer c
40500 6f 64 65 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74  ode.** represent
40510 69 6e 67 20 77 68 69 63 68 20 66 75 6e 63 74 69  ing which functi
40520 6f 6e 2e 20 20 49 66 20 74 68 65 20 65 78 70 72  on.  If the expr
40530 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62  ession is an unb
40540 6f 75 6e 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a  ound variable.**
40550 20 6d 61 72 6b 65 72 20 28 61 20 71 75 65 73 74   marker (a quest
40560 69 6f 6e 20 6d 61 72 6b 20 63 68 61 72 61 63 74  ion mark charact
40570 65 72 20 27 3f 27 20 69 6e 20 74 68 65 20 6f 72  er '?' in the or
40580 69 67 69 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e  iginal SQL) then
40590 20 74 68 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61   the.** Expr.iTa
405a0 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 69 6e  ble holds the in
405b0 64 65 78 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  dex number for t
405c0 68 61 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a  hat variable..**
405d0 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
405e0 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 71 75  ssion is a subqu
405f0 65 72 79 20 74 68 65 6e 20 45 78 70 72 2e 69 43  ery then Expr.iC
40600 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69  olumn holds an i
40610 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67 69 73 74  nteger.** regist
40620 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69  er number contai
40630 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ning the result 
40640 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  of the subquery.
40650 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 75 62 71    If the.** subq
40660 75 65 72 79 20 67 69 76 65 73 20 61 20 63 6f 6e  uery gives a con
40670 73 74 61 6e 74 20 72 65 73 75 6c 74 2c 20 74 68  stant result, th
40680 65 6e 20 69 54 61 62 6c 65 20 69 73 20 2d 31 2e  en iTable is -1.
40690 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72    If the subquer
406a0 79 0a 2a 2a 20 67 69 76 65 73 20 61 20 64 69 66  y.** gives a dif
406b0 66 65 72 65 6e 74 20 61 6e 73 77 65 72 20 61 74  ferent answer at
406c0 20 64 69 66 66 65 72 65 6e 74 20 74 69 6d 65 73   different times
406d0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e   during statemen
406e0 74 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20  t processing.** 
406f0 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 74  then iTable is t
40700 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20  he address of a 
40710 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
40720 63 6f 6d 70 75 74 65 73 20 74 68 65 20 73 75 62  computes the sub
40730 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  query..**.** The
40740 20 45 78 70 72 2e 70 53 65 6c 65 63 74 20 66 69   Expr.pSelect fi
40750 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  eld points to a 
40760 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
40770 2e 20 20 54 68 65 20 53 45 4c 45 43 54 20 6d 69  .  The SELECT mi
40780 67 68 74 0a 2a 2a 20 62 65 20 74 68 65 20 72 69  ght.** be the ri
40790 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61  ght operand of a
407a0 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20  n IN operator.  
407b0 4f 72 2c 20 69 66 20 61 20 73 63 61 6c 61 72 20  Or, if a scalar 
407c0 53 45 4c 45 43 54 20 61 70 70 65 61 72 73 0a 2a  SELECT appears.*
407d0 2a 20 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69  * in an expressi
407e0 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73  on the opcode is
407f0 20 54 4b 5f 53 45 4c 45 43 54 20 61 6e 64 20 45   TK_SELECT and E
40800 78 70 72 2e 70 53 65 6c 65 63 74 20 69 73 20 74  xpr.pSelect is t
40810 68 65 20 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61  he only.** opera
40820 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  nd..**.** If the
40830 20 45 78 70 72 20 69 73 20 6f 66 20 74 79 70 65   Expr is of type
40840 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20   OP_Column, and 
40850 74 68 65 20 74 61 62 6c 65 20 69 74 20 69 73 20  the table it is 
40860 73 65 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a  selecting from.*
40870 2a 20 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c  * is a disk tabl
40880 65 20 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22  e or the "old.*"
40890 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74   pseudo-table, t
408a0 68 65 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20  hen pTab points 
408b0 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73  to the.** corres
408c0 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65  ponding table de
408d0 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72  finition..*/.str
408e0 75 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20  uct Expr {.  u8 
408f0 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
40900 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e      /* Operation
40910 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68   performed by th
40920 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61  is node */.  cha
40930 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20  r affinity;     
40940 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e      /* The affin
40950 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ity of the colum
40960 6e 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20  n or 0 if not a 
40970 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20  column */.  u16 
40980 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
40990 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c     /* Various fl
409a0 61 67 73 2e 20 20 53 65 65 20 62 65 6c 6f 77 20  ags.  See below 
409b0 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  */.  CollSeq *pC
409c0 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  oll;        /* T
409d0 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70  he collation typ
409e0 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e of the column 
409f0 6f 72 20 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a  or 0 */.  Expr *
40a00 70 4c 65 66 74 2c 20 2a 70 52 69 67 68 74 3b 20  pLeft, *pRight; 
40a10 20 2f 2a 20 4c 65 66 74 20 61 6e 64 20 72 69 67   /* Left and rig
40a20 68 74 20 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20  ht subnodes */. 
40a30 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
40a40 3b 20 20 20 20 20 20 20 2f 2a 20 41 20 6c 69 73  ;       /* A lis
40a50 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
40a60 20 75 73 65 64 20 61 73 20 66 75 6e 63 74 69 6f   used as functio
40a70 6e 20 61 72 67 75 6d 65 6e 74 73 0a 20 20 20 20  n arguments.    
40a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40a90 20 20 20 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c       ** or in "<
40aa0 65 78 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d  expr> IN (<expr-
40ab0 6c 69 73 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65  list)" */.  Toke
40ac0 6e 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20  n token;        
40ad0 20 20 20 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64     /* An operand
40ae0 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65   token */.  Toke
40af0 6e 20 73 70 61 6e 3b 20 20 20 20 20 20 20 20 20  n span;         
40b00 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74     /* Complete t
40b10 65 78 74 20 6f 66 20 74 68 65 20 65 78 70 72 65  ext of the expre
40b20 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ssion */.  int i
40b30 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20  Table, iColumn; 
40b40 20 20 2f 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b    /* When op==TK
40b50 5f 43 4f 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68  _COLUMN, then th
40b60 69 73 20 65 78 70 72 20 6e 6f 64 65 20 6d 65 61  is expr node mea
40b70 6e 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 20  ns the.         
40b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40b90 2a 2a 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69  ** iColumn-th fi
40ba0 65 6c 64 20 6f 66 20 74 68 65 20 69 54 61 62 6c  eld of the iTabl
40bb0 65 2d 74 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20  e-th table. */. 
40bc0 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e   AggInfo *pAggIn
40bd0 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20  fo;     /* Used 
40be0 62 79 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  by TK_AGG_COLUMN
40bf0 20 61 6e 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43   and TK_AGG_FUNC
40c00 54 49 4f 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41  TION */.  int iA
40c10 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gg;             
40c20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20   /* Which entry 
40c30 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  in pAggInfo->aCo
40c40 6c 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d  l[] or ->aFunc[]
40c50 20 2a 2f 0a 20 20 69 6e 74 20 69 52 69 67 68 74   */.  int iRight
40c60 4a 6f 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20  JoinTable;   /* 
40c70 49 66 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20  If EP_FromJoin, 
40c80 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
40c90 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20  of the join */. 
40ca0 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
40cb0 3b 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20  ;       /* When 
40cc0 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
40cd0 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20  s a sub-select. 
40ce0 20 41 6c 73 6f 20 74 68 65 0a 20 20 20 20 20 20   Also the.      
40cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40d00 20 20 20 2a 2a 20 72 69 67 68 74 20 73 69 64 65     ** right side
40d10 20 6f 66 20 22 3c 65 78 70 72 3e 20 49 4e 20 28   of "<expr> IN (
40d20 3c 73 65 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20  <select>)" */.  
40d30 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
40d40 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
40d50 66 6f 72 20 4f 50 5f 43 6f 6c 75 6d 6e 20 65 78  for OP_Column ex
40d60 70 72 65 73 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20  pressions. */.  
40d70 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b  Schema *pSchema;
40d80 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
40d90 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 53 51 4c  ITE_TEST) || SQL
40da0 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
40db0 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 69 67  TH>0.  int nHeig
40dc0 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ht;           /*
40dd0 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 20 74   Height of the t
40de0 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
40df0 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64 69  is node */.#endi
40e00 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  f.};../*.** The 
40e10 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68  following are th
40e20 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69  e meanings of bi
40e30 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66  ts in the Expr.f
40e40 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
40e50 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d 4a 6f  define EP_FromJo
40e60 69 6e 20 20 20 20 20 30 78 30 31 20 20 2f 2a 20  in     0x01  /* 
40e70 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 4f 4e  Originated in ON
40e80 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
40e90 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23 64   of a join */.#d
40ea0 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20 20 20  efine EP_Agg    
40eb0 20 20 20 20 20 20 30 78 30 32 20 20 2f 2a 20 43        0x02  /* C
40ec0 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d  ontains one or m
40ed0 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 66 75  ore aggregate fu
40ee0 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69  nctions */.#defi
40ef0 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 20  ne EP_Resolved  
40f00 20 20 20 30 78 30 34 20 20 2f 2a 20 49 44 73 20     0x04  /* IDs 
40f10 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76  have been resolv
40f20 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a 2f  ed to COLUMNs */
40f30 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 72 6f  .#define EP_Erro
40f40 72 20 20 20 20 20 20 20 20 30 78 30 38 20 20 2f  r        0x08  /
40f50 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e  * Expression con
40f60 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
40f70 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66  e errors */.#def
40f80 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 74 20  ine EP_Distinct 
40f90 20 20 20 20 30 78 31 30 20 20 2f 2a 20 41 67 67      0x10  /* Agg
40fa0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
40fb0 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 6b 65  with DISTINCT ke
40fc0 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65  yword */.#define
40fd0 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20 20 20   EP_VarSelect   
40fe0 20 30 78 32 30 20 20 2f 2a 20 70 53 65 6c 65 63   0x20  /* pSelec
40ff0 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65 64 2c  t is correlated,
41000 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 2a 2f   not constant */
41010 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 65 71 75  .#define EP_Dequ
41020 6f 74 65 64 20 20 20 20 20 30 78 34 30 20 20 2f  oted     0x40  /
41030 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 73 74  * True if the st
41040 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20 64 65  ring has been de
41050 71 75 6f 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e  quoted */.#defin
41060 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 20 20  e EP_InfixFunc  
41070 20 20 30 78 38 30 20 20 2f 2a 20 54 72 75 65 20    0x80  /* True 
41080 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 75 6e  for an infix fun
41090 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f  ction: LIKE, GLO
410a0 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66 69 6e  B, etc */.#defin
410b0 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  e EP_ExpCollate 
410c0 20 30 78 31 30 30 20 20 2f 2a 20 43 6f 6c 6c 61   0x100  /* Colla
410d0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70  ting sequence sp
410e0 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 69 74  ecified explicit
410f0 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ly */../*.** The
41100 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65  se macros can be
41110 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73   used to test, s
41120 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74  et, or clear bit
41130 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70  s in the .** Exp
41140 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a  r.flags field..*
41150 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61  /.#define ExprHa
41160 73 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  sProperty(E,P)  
41170 20 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26     (((E)->flags&
41180 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69  (P))==(P)).#defi
41190 6e 65 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ne ExprHasAnyPro
411a0 70 65 72 74 79 28 45 2c 50 29 20 20 28 28 28 45  perty(E,P)  (((E
411b0 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30  )->flags&(P))!=0
411c0 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 65  ).#define ExprSe
411d0 74 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  tProperty(E,P)  
411e0 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28     (E)->flags|=(
411f0 50 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43  P).#define ExprC
41200 6c 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50  learProperty(E,P
41210 29 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d  )   (E)->flags&=
41220 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69  ~(P)../*.** A li
41230 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
41240 73 2e 20 20 45 61 63 68 20 65 78 70 72 65 73 73  s.  Each express
41250 69 6f 6e 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c  ion may optional
41260 6c 79 20 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d  ly have a.** nam
41270 65 2e 20 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65  e.  An expr/name
41280 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e   combination can
41290 20 62 65 20 75 73 65 64 20 69 6e 20 73 65 76 65   be used in seve
412a0 72 61 6c 20 77 61 79 73 2c 20 73 75 63 68 0a 2a  ral ways, such.*
412b0 2a 20 61 73 20 74 68 65 20 6c 69 73 74 20 6f 66  * as the list of
412c0 20 22 65 78 70 72 20 41 53 20 49 44 22 20 66 69   "expr AS ID" fi
412d0 65 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  elds following a
412e0 20 22 53 45 4c 45 43 54 22 20 6f 72 20 69 6e 20   "SELECT" or in 
412f0 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22  the.** list of "
41300 49 44 20 3d 20 65 78 70 72 22 20 69 74 65 6d 73  ID = expr" items
41310 20 69 6e 20 61 6e 20 55 50 44 41 54 45 2e 20 20   in an UPDATE.  
41320 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73  A list of expres
41330 73 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73  sions can.** als
41340 6f 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65  o be used as the
41350 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66   argument to a f
41360 75 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63  unction, in whic
41370 68 20 63 61 73 65 20 74 68 65 20 61 2e 7a 4e 61  h case the a.zNa
41380 6d 65 0a 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e  me.** field is n
41390 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75  ot used..*/.stru
413a0 63 74 20 45 78 70 72 4c 69 73 74 20 7b 0a 20 20  ct ExprList {.  
413b0 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20  int nExpr;      
413c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
413d0 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20   of expressions 
413e0 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  on the list */. 
413f0 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   int nAlloc;    
41400 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
41410 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c  r of entries all
41420 6f 63 61 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a  ocated below */.
41430 20 20 69 6e 74 20 69 45 43 75 72 73 6f 72 3b 20    int iECursor; 
41440 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
41450 20 43 75 72 73 6f 72 20 61 73 73 6f 63 69 61 74   Cursor associat
41460 65 64 20 77 69 74 68 20 74 68 69 73 20 45 78 70  ed with this Exp
41470 72 4c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63  rList */.  struc
41480 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
41490 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  {.    Expr *pExp
414a0 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
414b0 54 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72  The list of expr
414c0 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63  essions */.    c
414d0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
414e0 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61        /* Token a
414f0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
41500 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  his expression *
41510 2f 0a 20 20 20 20 75 38 20 73 6f 72 74 4f 72 64  /.    u8 sortOrd
41520 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er;          /* 
41530 31 20 66 6f 72 20 44 45 53 43 20 6f 72 20 30 20  1 for DESC or 0 
41540 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20 20 20 75  for ASC */.    u
41550 38 20 69 73 41 67 67 3b 20 20 20 20 20 20 20 20  8 isAgg;        
41560 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
41570 20 74 68 69 73 20 69 73 20 61 6e 20 61 67 67 72   this is an aggr
41580 65 67 61 74 65 20 6c 69 6b 65 20 63 6f 75 6e 74  egate like count
41590 28 2a 29 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f  (*) */.    u8 do
415a0 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ne;             
415b0 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69    /* A flag to i
415c0 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f  ndicate when pro
415d0 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73  cessing is finis
415e0 68 65 64 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20  hed */.  } *a;  
415f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41600 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72  /* One entry for
41610 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
41620 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
41630 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
41640 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20  s structure can 
41650 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c 69  hold a simple li
41660 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72  st of identifier
41670 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 68  s,.** such as th
41680 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 69  e list "a,b,c" i
41690 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
416a0 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  statements:.**.*
416b0 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  *      INSERT IN
416c0 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c 55  TO t(a,b,c) VALU
416d0 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20  ES ...;.**      
416e0 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 78  CREATE INDEX idx
416f0 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a   ON t(a,b,c);.**
41700 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49        CREATE TRI
41710 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 45  GGER trig BEFORE
41720 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c 62   UPDATE ON t(a,b
41730 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54  ,c) ...;.**.** T
41740 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 20  he IdList.a.idx 
41750 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 68  field is used wh
41760 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 65  en the IdList re
41770 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 73  presents the lis
41780 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e  t of.** column n
41790 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 62  ames after a tab
417a0 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e  le name in an IN
417b0 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  SERT statement. 
417c0 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   In the statemen
417d0 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45  t.**.**     INSE
417e0 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29  RT INTO t(a,b,c)
417f0 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61   ....**.** If "a
41800 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 6f  " is the k-th co
41810 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 74  lumn of table "t
41820 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e 61  ", then IdList.a
41830 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73  [0].idx==k..*/.s
41840 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a 20  truct IdList {. 
41850 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69   struct IdList_i
41860 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a  tem {.    char *
41870 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e  zName;      /* N
41880 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e 74  ame of the ident
41890 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  ifier */.    int
418a0 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f   idx;          /
418b0 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20  * Index in some 
418c0 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20  Table.aCol[] of 
418d0 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a  a column named z
418e0 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a  Name */.  } *a;.
418f0 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20    int nId;      
41900 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
41910 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 74  identifiers on t
41920 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  he list */.  int
41930 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a   nAlloc;      /*
41940 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
41950 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
41960 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b   a[] below */.};
41970 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d  ../*.** The bitm
41980 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 66  ask datatype def
41990 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 73  ined below is us
419a0 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 6f  ed for various o
419b0 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a  ptimizations..**
419c0 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 69  .** Changing thi
419d0 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 20  s from a 64-bit 
419e0 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 65  to a 32-bit type
419f0 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d 62   limits the numb
41a00 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20  er of.** tables 
41a10 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20  in a join to 32 
41a20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 20  instead of 64.  
41a30 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 75  But it also redu
41a40 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20  ces the size.** 
41a50 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 62  of the library b
41a60 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 69  y 738 bytes on i
41a70 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  x86..*/.typedef 
41a80 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a  u64 Bitmask;../*
41a90 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
41aa0 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63  g structure desc
41ab0 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 63  ribes the FROM c
41ac0 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
41ad0 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  T statement..** 
41ae0 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 75  Each table or su
41af0 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52  bquery in the FR
41b00 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73  OM clause is a s
41b10 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 20  eparate element 
41b20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 73  of.** the SrcLis
41b30 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a  t.a[] array..**.
41b40 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 69  ** With the addi
41b50 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65  tion of multiple
41b60 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
41b70 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
41b80 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 61   structure.** ca
41b90 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74  n also be used t
41ba0 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72  o describe a par
41bb0 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 75  ticular table su
41bc0 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 20  ch as the table 
41bd0 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66  that.** is modif
41be0 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54  ied by an INSERT
41bf0 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44  , DELETE, or UPD
41c00 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ATE statement.  
41c10 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c 2c  In standard SQL,
41c20 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c 65  .** such a table
41c30 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 6c   must be a simpl
41c40 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 74  e name: ID.  But
41c50 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 20   in SQLite, the 
41c60 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77  table can.** now
41c70 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 62   be identified b
41c80 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 6d  y a database nam
41c90 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 74  e, a dot, then t
41ca0 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49  he table name: I
41cb0 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  D.ID..**.** The 
41cc0 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 20  jointype starts 
41cd0 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 20  out showing the 
41ce0 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 65  join type betwee
41cf0 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  n the current ta
41d00 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  ble.** and the n
41d10 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 65  ext table on the
41d20 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 73   list.  The pars
41d30 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c 69  er builds the li
41d40 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20  st this way..** 
41d50 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c 69  But sqlite3SrcLi
41d60 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
41d70 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 74  ) later shifts t
41d80 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20  he jointypes so 
41d90 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69  that each.** joi
41da0 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 20  ntype expresses 
41db0 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e  the join between
41dc0 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74   the table and t
41dd0 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 6c  he previous tabl
41de0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 63  e..*/.struct Src
41df0 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 72  List {.  i16 nSr
41e00 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  c;        /* Num
41e10 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f 72  ber of tables or
41e20 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74   subqueries in t
41e30 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
41e40 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20  /.  i16 nAlloc; 
41e50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
41e60 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
41e70 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77  ted in a[] below
41e80 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
41e90 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20  List_item {.    
41ea0 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b  char *zDatabase;
41eb0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
41ec0 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68  abase holding th
41ed0 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  is table */.    
41ee0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
41ef0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
41f00 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68   table */.    ch
41f10 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20  ar *zAlias;     
41f20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 20  /* The "B" part 
41f30 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 68  of a "A AS B" ph
41f40 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20  rase.  zName is 
41f50 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 54  the "A" */.    T
41f60 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
41f70 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c 65   /* An SQL table
41f80 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
41f90 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53  o zName */.    S
41fa0 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
41fb0 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
41fc0 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 70  tement used in p
41fd0 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20  lace of a table 
41fe0 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 69  name */.    u8 i
41ff0 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a  sPopulated;   /*
42000 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   Temporary table
42010 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
42020 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 6c   SELECT is popul
42030 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a  ated */.    u8 j
42040 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f 2a  ointype;      /*
42050 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65   Type of join be
42060 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 20  tween this able 
42070 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73  and the previous
42080 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75 72   */.    int iCur
42090 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  sor;      /* The
420a0 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
420b0 62 65 72 20 75 73 65 64 20 74 6f 20 61 63 63 65  ber used to acce
420c0 73 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  ss this table */
420d0 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e 3b 20  .    Expr *pOn; 
420e0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e         /* The ON
420f0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69   clause of a joi
42100 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 74 20  n */.    IdList 
42110 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 54 68  *pUsing;   /* Th
42120 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f  e USING clause o
42130 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20  f a join */.    
42140 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 64 3b  Bitmask colUsed;
42150 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c 3c 4e    /* Bit N (1<<N
42160 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d 6e 20  ) set if column 
42170 4e 20 6f 72 20 70 54 61 62 20 69 73 20 75 73 65  N or pTab is use
42180 64 20 2a 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20  d */.  } a[1];  
42190 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
421a0 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68  e entry for each
421b0 20 69 64 65 6e 74 69 66 69 65 72 20 6f 6e 20 74   identifier on t
421c0 68 65 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f  he list */.};../
421d0 2a 0a 2a 2a 20 50 65 72 6d 69 74 74 65 64 20 76  *.** Permitted v
421e0 61 6c 75 65 73 20 6f 66 20 74 68 65 20 53 72 63  alues of the Src
421f0 4c 69 73 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20  List.a.jointype 
42200 66 69 65 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65  field.*/.#define
42210 20 4a 54 5f 49 4e 4e 45 52 20 20 20 20 20 30 78   JT_INNER     0x
42220 30 30 30 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b  0001    /* Any k
42230 69 6e 64 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20  ind of inner or 
42240 63 72 6f 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64  cross join */.#d
42250 65 66 69 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20  efine JT_CROSS  
42260 20 20 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20     0x0002    /* 
42270 45 78 70 6c 69 63 69 74 20 75 73 65 20 6f 66 20  Explicit use of 
42280 74 68 65 20 43 52 4f 53 53 20 6b 65 79 77 6f 72  the CROSS keywor
42290 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f  d */.#define JT_
422a0 4e 41 54 55 52 41 4c 20 20 20 30 78 30 30 30 34  NATURAL   0x0004
422b0 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
422c0 61 20 22 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e  a "natural" join
422d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c   */.#define JT_L
422e0 45 46 54 20 20 20 20 20 20 30 78 30 30 30 38 20  EFT      0x0008 
422f0 20 20 20 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72     /* Left outer
42300 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   join */.#define
42310 20 4a 54 5f 52 49 47 48 54 20 20 20 20 20 30 78   JT_RIGHT     0x
42320 30 30 31 30 20 20 20 20 2f 2a 20 52 69 67 68 74  0010    /* Right
42330 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23   outer join */.#
42340 64 65 66 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20  define JT_OUTER 
42350 20 20 20 20 30 78 30 30 32 30 20 20 20 20 2f 2a      0x0020    /*
42360 20 54 68 65 20 22 4f 55 54 45 52 22 20 6b 65 79   The "OUTER" key
42370 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
42380 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 45 52  */.#define JT_ER
42390 52 4f 52 20 20 20 20 20 30 78 30 30 34 30 20 20  ROR     0x0040  
423a0 20 20 2f 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20    /* unknown or 
423b0 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e  unsupported join
423c0 20 74 79 70 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   type */../*.** 
423d0 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20  For each nested 
423e0 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20  loop in a WHERE 
423f0 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74  clause implement
42400 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65  ation, the Where
42410 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72  Info.** structur
42420 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  e contains a sin
42430 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  gle instance of 
42440 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20  this structure. 
42450 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   This structure.
42460 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  ** is intended t
42470 6f 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65  o be private the
42480 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64   the where.c mod
42490 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e  ule and should n
424a0 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20  ot be.** access 
424b0 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f  or modified by o
424c0 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a  ther modules..**
424d0 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f  .** The pIdxInfo
424e0 20 61 6e 64 20 70 42 65 73 74 49 64 78 20 66 69   and pBestIdx fi
424f0 65 6c 64 73 20 61 72 65 20 75 73 65 64 20 74 6f  elds are used to
42500 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 62   help pick the b
42510 65 73 74 0a 2a 2a 20 69 6e 64 65 78 20 6f 6e 20  est.** index on 
42520 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
42530 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 70    The pIdxInfo p
42540 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 20  ointer contains 
42550 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f  indexing.** info
42560 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  rmation for the 
42570 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 68  i-th table in th
42580 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 65  e FROM clause be
42590 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 2e  fore reordering.
425a0 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 78  .** All the pIdx
425b0 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 72  Info pointers ar
425c0 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 65  e freed by where
425d0 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 68  InfoFree() in wh
425e0 65 72 65 2e 63 2e 0a 2a 2a 20 54 68 65 20 70 42  ere.c..** The pB
425f0 65 73 74 49 64 78 20 70 6f 69 6e 74 65 72 20 69  estIdx pointer i
42600 73 20 61 20 63 6f 70 79 20 6f 66 20 70 49 64 78  s a copy of pIdx
42610 49 6e 66 6f 20 66 6f 72 20 74 68 65 20 69 2d 74  Info for the i-t
42620 68 20 74 61 62 6c 65 20 61 66 74 65 72 0a 2a 2a  h table after.**
42630 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 72 64   FROM clause ord
42640 65 72 69 6e 67 2e 20 20 54 68 69 73 20 69 73 20  ering.  This is 
42650 61 20 6c 69 74 74 6c 65 20 63 6f 6e 66 75 73 69  a little confusi
42660 6e 67 20 73 6f 20 49 20 77 69 6c 6c 20 72 65 70  ng so I will rep
42670 65 61 74 0a 2a 2a 20 69 74 20 69 6e 20 64 69 66  eat.** it in dif
42680 66 65 72 65 6e 74 20 77 6f 72 64 73 2e 20 20 57  ferent words.  W
42690 68 65 72 65 49 6e 66 6f 2e 61 5b 69 5d 2e 70 49  hereInfo.a[i].pI
426a0 64 78 49 6e 66 6f 20 69 73 20 69 6e 64 65 78 20  dxInfo is index 
426b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20  information .** 
426c0 66 6f 72 20 57 68 65 72 65 49 6e 66 6f 2e 70 54  for WhereInfo.pT
426d0 61 62 4c 69 73 74 2e 61 5b 69 5d 2e 20 20 57 68  abList.a[i].  Wh
426e0 65 72 65 49 6e 66 6f 2e 61 5b 69 5d 2e 70 42 65  ereInfo.a[i].pBe
426f0 73 74 49 6e 66 6f 20 69 73 20 74 68 65 0a 2a 2a  stInfo is the.**
42700 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69   index informati
42710 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20  on for the i-th 
42720 6c 6f 6f 70 20 6f 66 20 74 68 65 20 6a 6f 69 6e  loop of the join
42730 2e 20 20 70 42 65 73 74 49 6e 66 6f 20 69 73 20  .  pBestInfo is 
42740 61 6c 77 61 79 73 0a 2a 2a 20 65 69 74 68 65 72  always.** either
42750 20 4e 55 4c 4c 20 6f 72 20 61 20 63 6f 70 79 20   NULL or a copy 
42760 6f 66 20 73 6f 6d 65 20 70 49 64 78 49 6e 66 6f  of some pIdxInfo
42770 2e 20 20 53 6f 20 66 6f 72 20 63 6c 65 61 6e 75  .  So for cleanu
42780 70 20 69 74 20 69 73 20 0a 2a 2a 20 73 75 66 66  p it is .** suff
42790 69 63 69 65 6e 74 20 74 6f 20 66 72 65 65 20 61  icient to free a
427a0 6c 6c 20 6f 66 20 74 68 65 20 70 49 64 78 49 6e  ll of the pIdxIn
427b0 66 6f 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2a 20  fo pointers..** 
427c0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65  .*/.struct Where
427d0 4c 65 76 65 6c 20 7b 0a 20 20 69 6e 74 20 69 46  Level {.  int iF
427e0 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  rom;            
427f0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69  /* Which entry i
42800 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
42810 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
42820 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
42830 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65 64  Flags associated
42840 20 77 69 74 68 20 74 68 69 73 20 6c 65 76 65 6c   with this level
42850 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 3b 20   */.  int iMem; 
42860 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
42870 69 72 73 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  irst memory cell
42880 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6c 65   used by this le
42890 76 65 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65  vel */.  int iLe
428a0 66 74 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f  ftJoin;        /
428b0 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73  * Memory cell us
428c0 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
428d0 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
428e0 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
428f0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
42900 64 65 78 20 75 73 65 64 2e 20 20 4e 55 4c 4c 20  dex used.  NULL 
42910 69 66 20 6e 6f 20 69 6e 64 65 78 20 2a 2f 0a 20  if no index */. 
42920 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 20 20   int iTabCur;   
42930 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
42940 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 74  BE cursor used t
42950 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 61 62  o access the tab
42960 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78  le */.  int iIdx
42970 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Cur;          /*
42980 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
42990 20 75 73 65 64 20 74 6f 20 61 63 65 73 73 73 20   used to acesss 
429a0 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 62 72  pIdx */.  int br
429b0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
429c0 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
429d0 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
429e0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e   loop */.  int n
429f0 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
42a00 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
42a10 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 74 20   start the next 
42a20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 2a  IN combination *
42a30 2f 0a 20 20 69 6e 74 20 63 6f 6e 74 3b 20 20 20  /.  int cont;   
42a40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
42a50 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e  p here to contin
42a60 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74  ue with the next
42a70 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a 20   loop cycle */. 
42a80 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20   int top;       
42a90 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
42aa0 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 69  instruction of i
42ab0 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 20 6c  nterior of the l
42ac0 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c  oop */.  int op,
42ad0 20 70 31 2c 20 70 32 3b 20 20 20 20 20 20 20 2f   p1, p2;       /
42ae0 2a 20 4f 70 63 6f 64 65 20 75 73 65 64 20 74 6f  * Opcode used to
42af0 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c   terminate the l
42b00 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71  oop */.  int nEq
42b10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
42b20 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f  * Number of == o
42b30 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73  r IN constraints
42b40 20 6f 6e 20 74 68 69 73 20 6c 6f 6f 70 20 2a 2f   on this loop */
42b50 0a 20 20 69 6e 74 20 6e 49 6e 3b 20 20 20 20 20  .  int nIn;     
42b60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
42b70 65 72 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f  er of IN operato
42b80 72 73 20 63 6f 6e 73 74 72 61 69 6e 69 6e 67 20  rs constraining 
42b90 74 68 69 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73  this loop */.  s
42ba0 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20  truct InLoop {. 
42bb0 20 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20     int iCur;    
42bc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
42bd0 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65   VDBE cursor use
42be0 64 20 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65  d by this IN ope
42bf0 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74  rator */.    int
42c00 20 74 6f 70 41 64 64 72 3b 20 20 20 20 20 20 20   topAddr;       
42c10 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68      /* Top of th
42c20 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 7d  e IN loop */.  }
42c30 20 2a 61 49 6e 4c 6f 6f 70 3b 20 20 20 20 20 20   *aInLoop;      
42c40 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
42c50 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e  ion about each n
42c60 65 73 74 65 64 20 49 4e 20 6f 70 65 72 61 74 6f  ested IN operato
42c70 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  r */.  sqlite3_i
42c80 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 42 65 73 74  ndex_info *pBest
42c90 49 64 78 3b 20 20 2f 2a 20 49 6e 64 65 78 20 69  Idx;  /* Index i
42ca0 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
42cb0 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 0a 20 20  his level */..  
42cc0 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
42cd0 20 66 69 65 6c 64 20 69 73 20 72 65 61 6c 6c 79   field is really
42ce0 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65   not part of the
42cf0 20 63 75 72 72 65 6e 74 20 6c 65 76 65 6c 2e 20   current level. 
42d00 20 42 75 74 0a 20 20 2a 2a 20 77 65 20 6e 65 65   But.  ** we nee
42d10 64 20 61 20 70 6c 61 63 65 20 74 6f 20 63 61 63  d a place to cac
42d20 68 65 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61  he index informa
42d30 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 61  tion for each ta
42d40 62 6c 65 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ble in the.  ** 
42d50 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20  FROM clause and 
42d60 74 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73  the WhereLevel s
42d70 74 72 75 63 74 75 72 65 20 69 73 20 61 20 63 6f  tructure is a co
42d80 6e 76 65 6e 69 65 6e 74 20 70 6c 61 63 65 2e 0a  nvenient place..
42d90 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69    */.  sqlite3_i
42da0 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49  ndex_info *pIdxI
42db0 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 65 78 20 69  nfo;  /* Index i
42dc0 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 20 73 6f 75  nfo for n-th sou
42dd0 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a  rce table */.};.
42de0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45  ./*.** The WHERE
42df0 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69   clause processi
42e00 6e 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74  ng routine has t
42e10 77 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a  wo halves.  The.
42e20 2a 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f  ** first part do
42e30 65 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  es the start of 
42e40 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61  the WHERE loop a
42e50 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a  nd the second.**
42e60 20 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74   half does the t
42e70 61 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45  ail of the WHERE
42e80 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61   loop.  An insta
42e90 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73  nce of.** this s
42ea0 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75  tructure is retu
42eb0 72 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73  rned by the firs
42ec0 74 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65  t half and passe
42ed0 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65  d.** into the se
42ee0 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76  cond half to giv
42ef0 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74  e some continuit
42f00 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  y..*/.struct Whe
42f10 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65  reInfo {.  Parse
42f20 20 2a 70 50 61 72 73 65 3b 0a 20 20 53 72 63 4c   *pParse;.  SrcL
42f30 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20  ist *pTabList;  
42f40 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
42f50 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 2a  es in the join *
42f60 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20  /.  int iTop;   
42f70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
42f80 76 65 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f  very beginning o
42f90 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70  f the WHERE loop
42fa0 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69   */.  int iConti
42fb0 6e 75 65 3b 20 20 20 20 20 20 20 2f 2a 20 4a 75  nue;       /* Ju
42fc0 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69  mp here to conti
42fd0 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 72 65  nue with next re
42fe0 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42  cord */.  int iB
42ff0 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 2f  reak;          /
43000 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62  * Jump here to b
43010 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20  reak out of the 
43020 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c  loop */.  int nL
43030 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f  evel;          /
43040 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74  * Number of nest
43050 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 71 6c  ed loop */.  sql
43060 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
43070 2a 2a 61 70 49 6e 66 6f 3b 20 20 2f 2a 20 41 72  **apInfo;  /* Ar
43080 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ray of pointers 
43090 74 6f 20 69 6e 64 65 78 20 69 6e 66 6f 20 73 74  to index info st
430a0 72 75 63 74 75 72 65 73 20 2a 2f 0a 20 20 57 68  ructures */.  Wh
430b0 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20  ereLevel a[1];  
430c0 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
430d0 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73  n about each nes
430e0 74 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48  t loop in the WH
430f0 45 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ERE */.};../*.**
43100 20 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64   A NameContext d
43110 65 66 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74  efines a context
43120 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73   in which to res
43130 6f 6c 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63  olve table and c
43140 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20  olumn.** names. 
43150 20 54 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e   The context con
43160 73 69 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20  sists of a list 
43170 6f 66 20 74 61 62 6c 65 73 20 28 74 68 65 20 70  of tables (the p
43180 53 72 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61  SrcList) field a
43190 6e 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20  nd.** a list of 
431a0 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e  named expression
431b0 20 28 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20   (pEList).  The 
431c0 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e  named expression
431d0 20 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20   list may.** be 
431e0 4e 55 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20  NULL.  The pSrc 
431f0 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
43200 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
43210 66 20 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a  f a SELECT or.**
43220 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65   to the table be
43230 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20  ing operated on 
43240 62 79 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  by INSERT, UPDAT
43250 45 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54  E, or DELETE.  T
43260 68 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72  he.** pEList cor
43270 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20  responds to the 
43280 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20  result set of a 
43290 53 45 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55  SELECT and is NU
432a0 4c 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20  LL for.** other 
432b0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
432c0 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63  * NameContexts c
432d0 61 6e 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57  an be nested.  W
432e0 68 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61  hen resolving na
432f0 6d 65 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d  mes, the inner-m
43300 6f 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20  ost .** context 
43310 69 73 20 73 65 61 72 63 68 65 64 20 66 69 72 73  is searched firs
43320 74 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20  t.  If no match 
43330 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65  is found, the ne
43340 78 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74  xt outer.** cont
43350 65 78 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20  ext is checked. 
43360 20 49 66 20 74 68 65 72 65 20 69 73 20 73 74 69   If there is sti
43370 6c 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65  ll no match, the
43380 20 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a   next context.**
43390 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68   is checked.  Th
433a0 69 73 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69  is process conti
433b0 6e 75 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65  nues until eithe
433c0 72 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75  r a match is fou
433d0 6e 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e  nd.** or all con
433e0 74 65 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e  texts are check.
433f0 20 20 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69    When a match i
43400 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65  s found, the nRe
43410 66 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74  f member of.** t
43420 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61  he context conta
43430 69 6e 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20  ining the match 
43440 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20  is incremented. 
43450 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71  .**.** Each subq
43460 75 65 72 79 20 67 65 74 73 20 61 20 6e 65 77 20  uery gets a new 
43470 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68  NameContext.  Th
43480 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f  e pNext field po
43490 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e  ints to the.** N
434a0 61 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68  ameContext in th
434b0 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20  e parent query. 
434c0 20 54 68 75 73 20 74 68 65 20 70 72 6f 63 65 73   Thus the proces
434d0 73 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68  s of scanning th
434e0 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74  e.** NameContext
434f0 20 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64   list correspond
43500 73 20 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74  s to searching t
43510 68 72 6f 75 67 68 20 73 75 63 63 65 73 73 69 76  hrough successiv
43520 65 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62  ely outer.** sub
43530 71 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20  queries looking 
43540 66 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a  for a match..*/.
43550 73 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65  struct NameConte
43560 78 74 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  xt {.  Parse *pP
43570 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54  arse;       /* T
43580 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53  he parser */.  S
43590 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74  rcList *pSrcList
435a0 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f  ;   /* One or mo
435b0 72 65 20 74 61 62 6c 65 73 20 75 73 65 64 20 74  re tables used t
435c0 6f 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20  o resolve names 
435d0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
435e0 45 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74  EList;    /* Opt
435f0 69 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61  ional list of na
43600 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20  med expressions 
43610 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
43620 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
43630 62 65 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73  ber of names res
43640 6f 6c 76 65 64 20 62 79 20 74 68 69 73 20 63 6f  olved by this co
43650 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  ntext */.  int n
43660 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Err;            
43670 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72  /* Number of err
43680 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20  ors encountered 
43690 77 68 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20  while resolving 
436a0 6e 61 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c  names */.  u8 al
436b0 6c 6f 77 41 67 67 3b 20 20 20 20 20 20 20 20 20  lowAgg;         
436c0 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e  /* Aggregate fun
436d0 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68  ctions allowed h
436e0 65 72 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41  ere */.  u8 hasA
436f0 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gg;           /*
43700 20 54 72 75 65 20 69 66 20 61 67 67 72 65 67 61   True if aggrega
43710 74 65 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a  tes are seen */.
43720 20 20 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20    u8 isCheck;   
43730 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
43740 66 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65  f resolving name
43750 73 20 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e  s in a CHECK con
43760 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74  straint */.  int
43770 20 6e 44 65 70 74 68 3b 20 20 20 20 20 20 20 20   nDepth;        
43780 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75    /* Depth of su
43790 62 71 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e  bquery recursion
437a0 2e 20 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72  . 1 for no recur
437b0 73 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66  sion */.  AggInf
437c0 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f  o *pAggInfo;   /
437d0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * Information ab
437e0 6f 75 74 20 61 67 67 72 65 67 61 74 65 73 20 61  out aggregates a
437f0 74 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a  t this level */.
43800 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
43810 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f  Next;  /* Next o
43820 75 74 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78  uter name contex
43830 74 2e 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74  t.  NULL for out
43840 65 72 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ermost */.};../*
43850 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
43860 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
43870 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
43880 69 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74  ins all informat
43890 69 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f  ion.** needed to
438a0 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   generate code f
438b0 6f 72 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45  or a single SELE
438c0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
438d0 0a 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65  .** nLimit is se
438e0 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65  t to -1 if there
438f0 20 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61   is no LIMIT cla
43900 75 73 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73  use.  nOffset is
43910 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66   set to 0..** If
43920 20 74 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49   there is a LIMI
43930 54 20 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61  T clause, the pa
43940 72 73 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74  rser sets nLimit
43950 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
43960 20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e   the.** limit an
43970 64 20 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65  d nOffset to the
43980 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66   value of the of
43990 66 73 65 74 20 28 6f 72 20 30 20 69 66 20 74 68  fset (or 0 if th
439a0 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66  ere is not.** of
439b0 66 73 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65  fset).  But late
439c0 72 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64  r on, nLimit and
439d0 20 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20   nOffset become 
439e0 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  the memory locat
439f0 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56  ions.** in the V
43a00 44 42 45 20 74 68 61 74 20 72 65 63 6f 72 64 20  DBE that record 
43a10 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66  the limit and of
43a20 66 73 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a  fset counters..*
43a30 2a 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68  *.** addrOpenEph
43a40 6d 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74  m[] entries cont
43a50 61 69 6e 20 74 68 65 20 61 64 64 72 65 73 73 20  ain the address 
43a60 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  of OP_OpenEpheme
43a70 72 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20  ral opcodes..** 
43a80 54 68 65 73 65 20 61 64 64 72 65 73 73 65 73 20  These addresses 
43a90 6d 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 73  must be stored s
43aa0 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f  o that we can go
43ab0 20 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69   back and fill i
43ac0 6e 0a 2a 2a 20 74 68 65 20 50 33 5f 4b 45 59 49  n.** the P3_KEYI
43ad0 4e 46 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d  NFO and P2 param
43ae0 65 74 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65  eters later.  Ne
43af0 69 74 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66  ither the KeyInf
43b00 6f 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d  o nor.** the num
43b10 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
43b20 6e 20 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70  n P2 can be comp
43b30 75 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65  uted at the same
43b40 20 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20   time.** as the 
43b50 4f 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74  OP_OpenEphm inst
43b60 72 75 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64  ruction is coded
43b70 20 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20   because not.** 
43b80 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69  enough informati
43b90 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d  on about the com
43ba0 70 6f 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b  pound query is k
43bb0 6e 6f 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69  nown at that poi
43bc0 6e 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e  nt..** The KeyIn
43bd0 66 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54  fo for addrOpenT
43be0 72 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63  ran[0] and [1] c
43bf0 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e  ontains collatin
43c00 67 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66  g sequences.** f
43c10 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  or the result se
43c20 74 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20  t.  The KeyInfo 
43c30 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e  for addrOpenTran
43c40 5b 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c  [2] contains col
43c50 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e  lating.** sequen
43c60 63 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45  ces for the ORDE
43c70 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a  R BY clause..*/.
43c80 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a  struct Select {.
43c90 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
43ca0 73 74 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  st;      /* The 
43cb0 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65  fields of the re
43cc0 73 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b  sult */.  u8 op;
43cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ce0 20 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55   /* One of: TK_U
43cf0 4e 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49  NION TK_ALL TK_I
43d00 4e 54 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45  NTERSECT TK_EXCE
43d10 50 54 20 2a 2f 0a 20 20 75 38 20 69 73 44 69 73  PT */.  u8 isDis
43d20 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 20 2f  tinct;         /
43d30 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44 49  * True if the DI
43d40 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69  STINCT keyword i
43d50 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 75  s present */.  u
43d60 38 20 69 73 52 65 73 6f 6c 76 65 64 3b 20 20 20  8 isResolved;   
43d70 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e        /* True on
43d80 63 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ce sqlite3Select
43d90 52 65 73 6f 6c 76 65 28 29 20 68 61 73 20 72 75  Resolve() has ru
43da0 6e 2e 20 2a 2f 0a 20 20 75 38 20 69 73 41 67 67  n. */.  u8 isAgg
43db0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
43dc0 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
43dd0 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 71  s an aggregate q
43de0 75 65 72 79 20 2a 2f 0a 20 20 75 38 20 75 73 65  uery */.  u8 use
43df0 73 45 70 68 6d 3b 20 20 20 20 20 20 20 20 20 20  sEphm;          
43e00 20 2f 2a 20 54 72 75 65 20 69 66 20 75 73 65 73   /* True if uses
43e10 20 61 6e 20 4f 70 65 6e 45 70 68 65 6d 65 72 61   an OpenEphemera
43e20 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 75 38  l opcode */.  u8
43e30 20 64 69 73 61 6c 6c 6f 77 4f 72 64 65 72 42 79   disallowOrderBy
43e40 3b 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61  ;    /* Do not a
43e50 6c 6c 6f 77 20 61 6e 20 4f 52 44 45 52 20 42 59  llow an ORDER BY
43e60 20 74 6f 20 62 65 20 61 74 74 61 63 68 65 64 20   to be attached 
43e70 69 66 20 54 52 55 45 20 2a 2f 0a 20 20 63 68 61  if TRUE */.  cha
43e80 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20  r affinity;     
43e90 20 20 20 20 2f 2a 20 4d 61 6b 65 52 65 63 6f 72      /* MakeRecor
43ea0 64 20 77 69 74 68 20 74 68 69 73 20 61 66 66 69  d with this affi
43eb0 6e 69 74 79 20 66 6f 72 20 53 52 54 5f 53 65 74  nity for SRT_Set
43ec0 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
43ed0 53 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Src;         /* 
43ee0 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
43ef0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
43f00 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e;          /* T
43f10 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
43f20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
43f30 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20 54  GroupBy;    /* T
43f40 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
43f50 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48  se */.  Expr *pH
43f60 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f  aving;         /
43f70 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61  * The HAVING cla
43f80 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
43f90 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20  t *pOrderBy;    
43fa0 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
43fb0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65  clause */.  Sele
43fc0 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20  ct *pPrior;     
43fd0 20 20 20 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65     /* Prior sele
43fe0 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ct in a compound
43ff0 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   select statemen
44000 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
44010 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a  Rightmost;    /*
44020 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65   Right-most sele
44030 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ct in a compound
44040 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   select statemen
44050 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69  t */.  Expr *pLi
44060 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  mit;          /*
44070 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69 6f   LIMIT expressio
44080 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f  n. NULL means no
44090 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70  t used. */.  Exp
440a0 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 20  r *pOffset;     
440b0 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 78      /* OFFSET ex
440c0 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d  pression. NULL m
440d0 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a  eans not used. *
440e0 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20  /.  int iLimit, 
440f0 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65  iOffset;   /* Me
44100 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 68  mory registers h
44110 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f  olding LIMIT & O
44120 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20 2a  FFSET counters *
44130 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65 6e  /.  int addrOpen
44140 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50  Ephm[3];   /* OP
44150 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64  _OpenEphem opcod
44160 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68  es related to th
44170 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a  is select */.};.
44180 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  ./*.** The resul
44190 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20 63  ts of a select c
441a0 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 74 65  an be distribute
441b0 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79  d in several way
441c0 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  s..*/.#define SR
441d0 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 20 31  T_Union        1
441e0 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c    /* Store resul
441f0 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20  t as keys in an 
44200 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65  index */.#define
44210 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 20   SRT_Except     
44220 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72    2  /* Remove r
44230 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49  esult from a UNI
44240 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66  ON index */.#def
44250 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72 64 20  ine SRT_Discard 
44260 20 20 20 20 20 33 20 20 2f 2a 20 44 6f 20 6e 6f       3  /* Do no
44270 74 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c  t save the resul
44280 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f 0a 0a  ts anywhere */..
44290 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
442a0 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f 72 65  clause is ignore
442b0 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  d for all of the
442c0 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66 69 6e   above */.#defin
442d0 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72  e IgnorableOrder
442e0 62 79 28 58 29 20 28 58 3c 3d 53 52 54 5f 44 69  by(X) (X<=SRT_Di
442f0 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20  scard)..#define 
44300 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20 20 20 20  SRT_Callback    
44310 20 34 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 61 20   4  /* Invoke a 
44320 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 65 61  callback with ea
44330 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74  ch row of result
44340 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
44350 4d 65 6d 20 20 20 20 20 20 20 20 20 20 35 20 20  Mem          5  
44360 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20  /* Store result 
44370 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  in a memory cell
44380 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
44390 53 65 74 20 20 20 20 20 20 20 20 20 20 36 20 20  Set          6  
443a0 2f 2a 20 53 74 6f 72 65 20 6e 6f 6e 2d 6e 75 6c  /* Store non-nul
443b0 6c 20 72 65 73 75 6c 74 73 20 61 73 20 6b 65 79  l results as key
443c0 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f  s in an index */
443d0 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54 61 62  .#define SRT_Tab
443e0 6c 65 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20  le        7  /* 
443f0 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73 20  Store result as 
44400 64 61 74 61 20 77 69 74 68 20 61 6e 20 61 75 74  data with an aut
44410 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a 2f 0a  omatic rowid */.
44420 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70 68 65  #define SRT_Ephe
44430 6d 54 61 62 20 20 20 20 20 38 20 20 2f 2a 20 43  mTab     8  /* C
44440 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e 74 20  reate transient 
44450 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20 6c 69  tab and store li
44460 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a 2f 0a  ke SRT_Table */.
44470 23 64 65 66 69 6e 65 20 53 52 54 5f 53 75 62 72  #define SRT_Subr
44480 6f 75 74 69 6e 65 20 20 20 39 20 20 2f 2a 20 43  outine   9  /* C
44490 61 6c 6c 20 61 20 73 75 62 72 6f 75 74 69 6e 65  all a subroutine
444a0 20 74 6f 20 68 61 6e 64 6c 65 20 72 65 73 75 6c   to handle resul
444b0 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  ts */.#define SR
444c0 54 5f 45 78 69 73 74 73 20 20 20 20 20 20 31 30  T_Exists      10
444d0 20 20 2f 2a 20 53 74 6f 72 65 20 31 20 69 66 20    /* Store 1 if 
444e0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f  the result is no
444f0 74 20 65 6d 70 74 79 20 2a 2f 0a 0a 2f 2a 0a 2a  t empty */../*.*
44500 2a 20 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20  * An SQL parser 
44510 63 6f 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79  context.  A copy
44520 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
44530 72 65 20 69 73 20 70 61 73 73 65 64 20 74 68 72  re is passed thr
44540 6f 75 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73  ough.** the pars
44550 65 72 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f  er and down into
44560 20 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20   all the parser 
44570 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69  action routine i
44580 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61  n order to.** ca
44590 72 72 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72  rry around infor
445a0 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67  mation that is g
445b0 6c 6f 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74  lobal to the ent
445c0 69 72 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a  ire parse..**.**
445d0 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69   The structure i
445e0 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74  s divided into t
445f0 77 6f 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20  wo parts.  When 
44600 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63  the parser and c
44610 6f 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20  ode.** generate 
44620 63 61 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20  call themselves 
44630 72 65 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65  recursively, the
44640 20 66 69 72 73 74 20 70 61 72 74 20 6f 66 20 74   first part of t
44650 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  he structure.** 
44660 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20  is constant but 
44670 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20  the second part 
44680 69 73 20 72 65 73 65 74 20 61 74 20 74 68 65 20  is reset at the 
44690 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e  beginning and en
446a0 64 20 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63  d of.** each rec
446b0 75 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ursion..**.** Th
446c0 65 20 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64  e nTableLock and
446d0 20 61 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69   aTableLock vari
446e0 61 62 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75  ables are only u
446f0 73 65 64 20 69 66 20 74 68 65 20 73 68 61 72 65  sed if the share
44700 64 2d 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74  d-cache .** feat
44710 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28  ure is enabled (
44720 69 66 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d  if sqlite3Tsd()-
44730 3e 75 73 65 53 68 61 72 65 64 44 61 74 61 20 69  >useSharedData i
44740 73 20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72  s true). They ar
44750 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f  e.** used to sto
44760 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61  re the set of ta
44770 62 6c 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72  ble-locks requir
44780 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d  ed by the statem
44790 65 6e 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d  ent being.** com
447a0 70 69 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20  piled. Function 
447b0 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
447c0 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 64  () is used to ad
447d0 64 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65  d entries to the
447e0 0a 2a 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72  .** list..*/.str
447f0 75 63 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71  uct Parse {.  sq
44800 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
44810 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64     /* The main d
44820 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
44830 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
44840 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
44850 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
44860 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63  execution */.  c
44870 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20  har *zErrMsg;   
44880 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20      /* An error 
44890 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62  message */.  Vdb
448a0 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20  e *pVdbe;       
448b0 20 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66    /* An engine f
448c0 6f 72 20 65 78 65 63 75 74 69 6e 67 20 64 61 74  or executing dat
448d0 61 62 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a  abase bytecode *
448e0 2f 0a 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53  /.  u8 colNamesS
448f0 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45  et;      /* TRUE
44900 20 61 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e   after OP_Column
44910 4e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73  Name has been is
44920 73 75 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f  sued to pVdbe */
44930 0a 20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b  .  u8 nameClash;
44940 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72          /* A per
44950 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d  manent table nam
44960 65 20 63 6c 61 73 68 65 73 20 77 69 74 68 20 74  e clashes with t
44970 65 6d 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a  emp table name *
44980 2f 0a 20 20 75 38 20 63 68 65 63 6b 53 63 68 65  /.  u8 checkSche
44990 6d 61 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73  ma;      /* Caus
449a0 65 73 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  es schema cookie
449b0 20 63 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20   check after an 
449c0 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65  error */.  u8 ne
449d0 73 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  sted;           
449e0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73  /* Number of nes
449f0 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65  ted calls to the
44a00 20 70 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e   parser/code gen
44a10 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70  erator */.  u8 p
44a20 61 72 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20  arseError;      
44a30 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 61   /* True after a
44a40 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20   parsing error. 
44a50 20 54 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f   Ticket #1794 */
44a60 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20  .  int nErr;    
44a70 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
44a80 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e  r of errors seen
44a90 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 3b 20   */.  int nTab; 
44aa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
44ab0 6d 62 65 72 20 6f 66 20 70 72 65 76 69 6f 75 73  mber of previous
44ac0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 56 44 42  ly allocated VDB
44ad0 45 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69  E cursors */.  i
44ae0 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt nMem;        
44af0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
44b00 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 75 73   memory cells us
44b10 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69  ed so far */.  i
44b20 6e 74 20 6e 53 65 74 3b 20 20 20 20 20 20 20 20  nt nSet;        
44b30 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
44b40 20 73 65 74 73 20 75 73 65 64 20 73 6f 20 66 61   sets used so fa
44b50 72 20 2a 2f 0a 20 20 69 6e 74 20 63 6b 4f 66 66  r */.  int ckOff
44b60 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  set;        /* S
44b70 74 61 63 6b 20 6f 66 66 73 65 74 20 74 6f 20 64  tack offset to d
44b80 61 74 61 20 75 73 65 64 20 62 79 20 43 48 45 43  ata used by CHEC
44b90 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  K constraints */
44ba0 0a 20 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b  .  u32 writeMask
44bb0 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74  ;       /* Start
44bc0 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63   a write transac
44bd0 74 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61  tion on these da
44be0 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32  tabases */.  u32
44bf0 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20   cookieMask;    
44c00 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20    /* Bitmask of 
44c10 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20  schema verified 
44c20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 69  databases */.  i
44c30 6e 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b 20 20  nt cookieGoto;  
44c40 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
44c50 66 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 6f 6f  f OP_Goto to coo
44c60 6b 69 65 20 76 65 72 69 66 69 65 72 20 73 75 62  kie verifier sub
44c70 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
44c80 20 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 51 4c   cookieValue[SQL
44c90 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44  ITE_MAX_ATTACHED
44ca0 2b 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 73 20  +2];  /* Values 
44cb0 6f 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20 76 65  of cookies to ve
44cc0 72 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65 66 20  rify */.#ifndef 
44cd0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
44ce0 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e  ED_CACHE.  int n
44cf0 54 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20  TableLock;      
44d00 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c    /* Number of l
44d10 6f 63 6b 73 20 69 6e 20 61 54 61 62 6c 65 4c 6f  ocks in aTableLo
44d20 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f 63  ck */.  TableLoc
44d30 6b 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b 20 2f  k *aTableLock; /
44d40 2a 20 52 65 71 75 69 72 65 64 20 74 61 62 6c 65  * Required table
44d50 20 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61 72 65   locks for share
44d60 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a  d-cache mode */.
44d70 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 62 6f  #endif..  /* Abo
44d80 76 65 20 69 73 20 63 6f 6e 73 74 61 6e 74 20 62  ve is constant b
44d90 65 74 77 65 65 6e 20 72 65 63 75 72 73 69 6f 6e  etween recursion
44da0 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 72 65 73  s.  Below is res
44db0 65 74 20 62 65 66 6f 72 65 20 61 6e 64 20 61 66  et before and af
44dc0 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 20 72 65  ter.  ** each re
44dd0 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20 69 6e  cursion */..  in
44de0 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20  t nVar;         
44df0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
44e00 27 3f 27 20 76 61 72 69 61 62 6c 65 73 20 73 65  '?' variables se
44e10 65 6e 20 69 6e 20 74 68 65 20 53 51 4c 20 73 6f  en in the SQL so
44e20 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 56   far */.  int nV
44e30 61 72 45 78 70 72 3b 20 20 20 20 20 20 20 20 2f  arExpr;        /
44e40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 64  * Number of used
44e50 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 45   slots in apVarE
44e60 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  xpr[] */.  int n
44e70 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20 20 20  VarExprAlloc;   
44e80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c  /* Number of all
44e90 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 69 6e 20  ocated slots in 
44ea0 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20  apVarExpr[] */. 
44eb0 20 45 78 70 72 20 2a 2a 61 70 56 61 72 45 78 70   Expr **apVarExp
44ec0 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  r;    /* Pointer
44ed0 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 20 24 61  s to :aaa and $a
44ee0 61 61 61 20 77 69 6c 64 63 61 72 64 20 65 78 70  aaa wildcard exp
44ef0 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 75 38  ressions */.  u8
44f00 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 20   explain;       
44f10 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
44f20 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 20 69  e EXPLAIN flag i
44f30 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20 71  s found on the q
44f40 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  uery */.  Token 
44f50 73 45 72 72 54 6f 6b 65 6e 3b 20 20 20 20 20 2f  sErrToken;     /
44f60 2a 20 54 68 65 20 74 6f 6b 65 6e 20 61 74 20 77  * The token at w
44f70 68 69 63 68 20 74 68 65 20 65 72 72 6f 72 20 6f  hich the error o
44f80 63 63 75 72 72 65 64 20 2a 2f 0a 20 20 54 6f 6b  ccurred */.  Tok
44f90 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20  en sNameToken;  
44fa0 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20    /* Token with 
44fb0 75 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 65  unqualified sche
44fc0 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ma object name *
44fd0 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54  /.  Token sLastT
44fe0 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20  oken;    /* The 
44ff0 6c 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65  last token parse
45000 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
45010 72 20 2a 7a 53 71 6c 3b 20 20 20 20 2f 2a 20 41  r *zSql;    /* A
45020 6c 6c 20 53 51 4c 20 74 65 78 74 20 2a 2f 0a 20  ll SQL text */. 
45030 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
45040 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c 20 53 51 4c  il;   /* All SQL
45050 20 74 65 78 74 20 70 61 73 74 20 74 68 65 20 6c   text past the l
45060 61 73 74 20 73 65 6d 69 63 6f 6c 6f 6e 20 70 61  ast semicolon pa
45070 72 73 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20  rsed */.  Table 
45080 2a 70 4e 65 77 54 61 62 6c 65 3b 20 20 20 20 2f  *pNewTable;    /
45090 2a 20 41 20 74 61 62 6c 65 20 62 65 69 6e 67 20  * A table being 
450a0 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 43  constructed by C
450b0 52 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20  REATE TABLE */. 
450c0 20 54 72 69 67 67 65 72 20 2a 70 4e 65 77 54 72   Trigger *pNewTr
450d0 69 67 67 65 72 3b 20 20 20 20 20 2f 2a 20 54 72  igger;     /* Tr
450e0 69 67 67 65 72 20 75 6e 64 65 72 20 63 6f 6e 73  igger under cons
450f0 74 72 75 63 74 20 62 79 20 61 20 43 52 45 41 54  truct by a CREAT
45100 45 20 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 54  E TRIGGER */.  T
45110 72 69 67 67 65 72 53 74 61 63 6b 20 2a 74 72 69  riggerStack *tri
45120 67 53 74 61 63 6b 3b 20 20 2f 2a 20 54 72 69 67  gStack;  /* Trig
45130 67 65 72 20 61 63 74 69 6f 6e 73 20 62 65 69 6e  ger actions bein
45140 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 63 6f 6e  g coded */.  con
45150 73 74 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f  st char *zAuthCo
45160 6e 74 65 78 74 3b 20 2f 2a 20 54 68 65 20 36 74  ntext; /* The 6t
45170 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 64  h parameter to d
45180 62 2d 3e 78 41 75 74 68 20 63 61 6c 6c 62 61 63  b->xAuth callbac
45190 6b 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ks */.#ifndef SQ
451a0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
451b0 4c 54 41 42 4c 45 0a 20 20 54 6f 6b 65 6e 20 73  LTABLE.  Token s
451c0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
451d0 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20      /* Complete 
451e0 74 65 78 74 20 6f 66 20 61 20 6d 6f 64 75 6c 65  text of a module
451f0 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 75   argument */.  u
45200 38 20 64 65 63 6c 61 72 65 56 74 61 62 3b 20 20  8 declareVtab;  
45210 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
45220 65 20 69 66 20 69 6e 73 69 64 65 20 73 71 6c 69  e if inside sqli
45230 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
45240 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  () */.  Table *p
45250 56 69 72 74 75 61 6c 4c 6f 63 6b 3b 20 20 20 20  VirtualLock;    
45260 20 20 20 2f 2a 20 52 65 71 75 69 72 65 20 76 69     /* Require vi
45270 72 74 75 61 6c 20 74 61 62 6c 65 20 6c 6f 63 6b  rtual table lock
45280 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   on this table *
45290 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66  /.#endif.#if def
452a0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
452b0 29 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ) || SQLITE_MAX_
452c0 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 69  EXPR_DEPTH>0.  i
452d0 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20 20  nt nHeight;     
452e0 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73         /* Expres
452f0 73 69 6f 6e 20 74 72 65 65 20 68 65 69 67 68 74  sion tree height
45300 20 6f 66 20 63 75 72 72 65 6e 74 20 73 75 62 2d   of current sub-
45310 73 65 6c 65 63 74 20 2a 2f 0a 23 65 6e 64 69 66  select */.#endif
45320 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .};..#ifdef SQLI
45330 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
45340 41 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20 49  ABLE.  #define I
45350 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 30  N_DECLARE_VTAB 0
45360 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
45370 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
45380 20 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72   (pParse->declar
45390 65 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a 2f  eVtab).#endif../
453a0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
453b0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
453c0 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20  g structure can 
453d0 62 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20 61  be declared on a
453e0 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64 0a   stack and used.
453f0 2a 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20 50  ** to save the P
45400 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78  arse.zAuthContex
45410 74 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74 20  t value so that 
45420 69 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72  it can be restor
45430 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 72  ed later..*/.str
45440 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20  uct AuthContext 
45450 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
45460 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20 20  zAuthContext;   
45470 2f 2a 20 50 75 74 20 73 61 76 65 64 20 50 61 72  /* Put saved Par
45480 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20  se.zAuthContext 
45490 68 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65 20  here */.  Parse 
454a0 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20  *pParse;        
454b0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61 72        /* The Par
454c0 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  se structure */.
454d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69 65  };../*.** Bitfie
454e0 6c 64 20 66 6c 61 67 73 20 66 6f 72 20 50 32 20  ld flags for P2 
454f0 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73 65  value in OP_Inse
45500 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74 65  rt and OP_Delete
45510 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c  .*/.#define OPFL
45520 41 47 5f 4e 43 48 41 4e 47 45 20 20 20 31 20 20  AG_NCHANGE   1  
45530 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61    /* Set to upda
45540 74 65 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a  te db->nChange *
45550 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47  /.#define OPFLAG
45560 5f 4c 41 53 54 52 4f 57 49 44 20 32 20 20 20 20  _LASTROWID 2    
45570 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65  /* Set to update
45580 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a   db->lastRowid *
45590 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47  /.#define OPFLAG
455a0 5f 49 53 55 50 44 41 54 45 20 20 34 20 20 20 20  _ISUPDATE  4    
455b0 2f 2a 20 54 68 69 73 20 4f 50 5f 49 6e 73 65 72  /* This OP_Inser
455c0 74 20 69 73 20 61 6e 20 73 71 6c 20 55 50 44 41  t is an sql UPDA
455d0 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  TE */.#define OP
455e0 46 4c 41 47 5f 41 50 50 45 4e 44 20 20 20 20 38  FLAG_APPEND    8
455f0 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c      /* This is l
45600 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61  ikely to be an a
45610 70 70 65 6e 64 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20  ppend */../*. * 
45620 45 61 63 68 20 74 72 69 67 67 65 72 20 70 72 65  Each trigger pre
45630 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61  sent in the data
45640 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73  base schema is s
45650 74 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74  tored as an inst
45660 61 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63  ance of. * struc
45670 74 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20  t Trigger. . *. 
45680 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e  * Pointers to in
45690 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63  stances of struc
456a0 74 20 54 72 69 67 67 65 72 20 61 72 65 20 73 74  t Trigger are st
456b0 6f 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73  ored in two ways
456c0 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22  .. * 1. In the "
456d0 74 72 69 67 48 61 73 68 22 20 68 61 73 68 20 74  trigHash" hash t
456e0 61 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68  able (part of th
456f0 65 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20  e sqlite3* that 
45700 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a  represents the .
45710 20 2a 20 20 20 20 64 61 74 61 62 61 73 65 29 2e   *    database).
45720 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69   This allows Tri
45730 67 67 65 72 20 73 74 72 75 63 74 75 72 65 73 20  gger structures 
45740 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20  to be retrieved 
45750 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41  by name.. * 2. A
45760 6c 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f  ll triggers asso
45770 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69  ciated with a si
45780 6e 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20  ngle table form 
45790 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75  a linked list, u
457a0 73 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70  sing the. *    p
457b0 4e 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73  Next member of s
457c0 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 41  truct Trigger. A
457d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
457e0 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
457f0 20 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65   the. *    linke
45800 64 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64  d list is stored
45810 20 61 73 20 74 68 65 20 22 70 54 72 69 67 67 65   as the "pTrigge
45820 72 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65  r" member of the
45830 20 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20   associated. *  
45840 20 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a    struct Table..
45850 20 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f   *. * The "step_
45860 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69  list" member poi
45870 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74  nts to the first
45880 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69   element of a li
45890 6e 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e  nked list. * con
458a0 74 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20  taining the SQL 
458b0 73 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69  statements speci
458c0 66 69 65 64 20 61 73 20 74 68 65 20 74 72 69 67  fied as the trig
458d0 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f  ger program.. */
458e0 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20  .struct Trigger 
458f0 7b 0a 20 20 63 68 61 72 20 2a 6e 61 6d 65 3b 20  {.  char *name; 
45900 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
45910 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
45920 72 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20  rigger          
45930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f                */
45940 0a 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20  .  char *table; 
45950 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
45960 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20  e table or view 
45970 74 6f 20 77 68 69 63 68 20 74 68 65 20 74 72 69  to which the tri
45980 67 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a  gger applies */.
45990 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20    u8 op;        
459a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
459b0 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54   of TK_DELETE, T
459c0 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53  K_UPDATE, TK_INS
459d0 45 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ERT         */. 
459e0 20 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20   u8 tr_tm;      
459f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
45a00 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  of TRIGGER_BEFOR
45a10 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52  E, TRIGGER_AFTER
45a20 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
45a30 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
45a40 20 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65   The WHEN clause
45a50 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 69 6f   of the expresio
45a60 6e 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20  n (may be NULL) 
45a70 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f  */.  IdList *pCo
45a80 6c 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20  lumns;       /* 
45a90 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50  If this is an UP
45aa0 44 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d  DATE OF <column-
45ab0 6c 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20  list> trigger,. 
45ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45ad0 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
45ae0 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73  <column-list> is
45af0 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a   stored here */.
45b00 20 20 54 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65    Token nameToke
45b10 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b  n;        /* Tok
45b20 65 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 4e  en containing zN
45b30 61 6d 65 2e 20 55 73 65 20 64 75 72 69 6e 67 20  ame. Use during 
45b40 70 61 72 73 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a  parsing only */.
45b50 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
45b60 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68  a;        /* Sch
45b70 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ema containing t
45b80 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  he trigger */.  
45b90 53 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65  Schema *pTabSche
45ba0 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d  ma;     /* Schem
45bb0 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  a containing the
45bc0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67   table */.  Trig
45bd0 67 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69  gerStep *step_li
45be0 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74  st; /* Link list
45bf0 20 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67   of trigger prog
45c00 72 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20  ram steps       
45c10 20 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67        */.  Trigg
45c20 65 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  er *pNext;      
45c30 20 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67     /* Next trigg
45c40 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  er associated wi
45c50 74 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  th the table */.
45c60 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67  };../*.** A trig
45c70 67 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20  ger is either a 
45c80 42 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54  BEFORE or an AFT
45c90 45 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65  ER trigger.  The
45ca0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74   following const
45cb0 61 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e  ants.** determin
45cc0 65 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20  e which. .**.** 
45cd0 49 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c  If there are mul
45ce0 74 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20  tiple triggers, 
45cf0 79 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d  you might of som
45d00 65 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d  e BEFORE and som
45d10 65 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74  e AFTER..** In t
45d20 68 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63  hat cases, the c
45d30 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63  onstants below c
45d40 61 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74  an be ORed toget
45d50 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  her..*/.#define 
45d60 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20  TRIGGER_BEFORE  
45d70 31 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45  1.#define TRIGGE
45d80 52 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a  R_AFTER   2../*.
45d90 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f   * An instance o
45da0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  f struct Trigger
45db0 53 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20  Step is used to 
45dc0 73 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53  store a single S
45dd0 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20  QL statement. * 
45de0 74 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f  that is a part o
45df0 66 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67  f a trigger-prog
45e00 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74  ram. . *. * Inst
45e10 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20  ances of struct 
45e20 54 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20  TriggerStep are 
45e30 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67  stored in a sing
45e40 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28  ly linked list (
45e50 6c 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20  linked. * using 
45e60 74 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62  the "pNext" memb
45e70 65 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62  er) referenced b
45e80 79 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74  y the "step_list
45e90 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20  " member of the 
45ea0 0a 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73  . * associated s
45eb0 74 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e  truct Trigger in
45ec0 73 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73  stance. The firs
45ed0 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  t element of the
45ee0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a   linked list is.
45ef0 20 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65   * the first ste
45f00 70 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  p of the trigger
45f10 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a  -program.. * . *
45f20 20 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72   The "op" member
45f30 20 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68   indicates wheth
45f40 65 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45  er this is a "DE
45f50 4c 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c  LETE", "INSERT",
45f60 20 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20   "UPDATE" or. * 
45f70 22 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65  "SELECT" stateme
45f80 6e 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73  nt. The meanings
45f90 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65   of the other me
45fa0 6d 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69  mbers is determi
45fb0 6e 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76  ned by the . * v
45fc0 61 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20  alue of "op" as 
45fd0 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28  follows:. *. * (
45fe0 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29  op == TK_INSERT)
45ff0 0a 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e  . * orconf    ->
46000 20 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43   stores the ON C
46010 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68  ONFLICT algorith
46020 6d 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d  m. * pSelect   -
46030 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  > If this is an 
46040 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20  INSERT INTO ... 
46050 53 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65  SELECT ... state
46060 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20  ment, then. *   
46070 20 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20             this 
46080 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72  stores a pointer
46090 20 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73   to the SELECT s
460a0 74 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77  tatement. Otherw
460b0 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72  ise NULL.. * tar
460c0 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65  get    -> A toke
460d0 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61  n holding the na
460e0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
460f0 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a  to insert into..
46100 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20   * pExprList -> 
46110 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e  If this is an IN
46120 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41  SERT INTO ... VA
46130 4c 55 45 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65  LUES ... stateme
46140 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20  nt, then. *     
46150 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74           this st
46160 6f 72 65 73 20 76 61 6c 75 65 73 20 74 6f 20 62  ores values to b
46170 65 20 69 6e 73 65 72 74 65 64 2e 20 4f 74 68 65  e inserted. Othe
46180 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70  rwise NULL.. * p
46190 49 64 4c 69 73 74 20 20 20 2d 3e 20 49 66 20 74  IdList   -> If t
461a0 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
461b0 20 49 4e 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75   INTO ... (<colu
461c0 6d 6e 2d 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45  mn-names>) VALUE
461d0 53 20 2e 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20  S ... . *       
461e0 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74         statement
461f0 2c 20 74 68 65 6e 20 74 68 69 73 20 73 74 6f 72  , then this stor
46200 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61  es the column-na
46210 6d 65 73 20 74 6f 20 62 65 0a 20 2a 20 20 20 20  mes to be. *    
46220 20 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74            insert
46230 65 64 20 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28  ed into.. *. * (
46240 6f 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29  op == TK_DELETE)
46250 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e  . * target    ->
46260 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67   A token holding
46270 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
46280 20 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65   table to delete
46290 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65   from.. * pWhere
462a0 20 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45      -> The WHERE
462b0 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44   clause of the D
462c0 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20  ELETE statement 
462d0 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66  if one is specif
462e0 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20  ied.. *         
462f0 20 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e       Otherwise N
46300 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20  ULL.. * . * (op 
46310 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a  == TK_UPDATE). *
46320 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20   target    -> A 
46330 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68  token holding th
46340 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
46350 62 6c 65 20 74 6f 20 75 70 64 61 74 65 20 72 6f  ble to update ro
46360 77 73 20 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65  ws of.. * pWhere
46370 20 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45      -> The WHERE
46380 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55   clause of the U
46390 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
463a0 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66  if one is specif
463b0 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20  ied.. *         
463c0 20 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e       Otherwise N
463d0 55 4c 4c 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73  ULL.. * pExprLis
463e0 74 20 2d 3e 20 41 20 6c 69 73 74 20 6f 66 20 74  t -> A list of t
463f0 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70  he columns to up
46400 64 61 74 65 20 61 6e 64 20 74 68 65 20 65 78 70  date and the exp
46410 72 65 73 73 69 6f 6e 73 20 74 6f 20 75 70 64 61  ressions to upda
46420 74 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  te. *           
46430 20 20 20 74 68 65 6d 20 74 6f 2e 20 53 65 65 20     them to. See 
46440 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 29 20  sqlite3Update() 
46450 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  documentation of
46460 20 22 70 43 68 61 6e 67 65 73 22 0a 20 2a 20 20   "pChanges". *  
46470 20 20 20 20 20 20 20 20 20 20 20 20 61 72 67 75              argu
46480 6d 65 6e 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74  ment.. * . */.st
46490 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70  ruct TriggerStep
464a0 20 7b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20   {.  int op;    
464b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
464c0 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54   of TK_DELETE, T
464d0 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53  K_UPDATE, TK_INS
464e0 45 52 54 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a  ERT, TK_SELECT *
464f0 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20  /.  int orconf; 
46500 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52           /* OE_R
46510 6f 6c 6c 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a  ollback etc. */.
46520 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
46530 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72  ;      /* The tr
46540 69 67 67 65 72 20 74 68 61 74 20 74 68 69 73 20  igger that this 
46550 73 74 65 70 20 69 73 20 61 20 70 61 72 74 20 6f  step is a part o
46560 66 20 2a 2f 0a 0a 20 20 53 65 6c 65 63 74 20 2a  f */..  Select *
46570 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20  pSelect;     /* 
46580 56 61 6c 69 64 20 66 6f 72 20 53 45 4c 45 43 54  Valid for SELECT
46590 20 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a   and sometimes .
465a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
465b0 20 20 20 20 20 20 20 20 20 20 49 4e 53 45 52 54            INSERT
465c0 20 73 74 65 70 73 20 28 77 68 65 6e 20 70 45 78   steps (when pEx
465d0 70 72 4c 69 73 74 20 3d 3d 20 30 29 20 2a 2f 0a  prList == 0) */.
465e0 20 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20    Token target; 
465f0 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20         /* Valid 
46600 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 41  for DELETE, UPDA
46610 54 45 2c 20 49 4e 53 45 52 54 20 73 74 65 70 73  TE, INSERT steps
46620 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
46630 72 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61  re;        /* Va
46640 6c 69 64 20 66 6f 72 20 44 45 4c 45 54 45 2c 20  lid for DELETE, 
46650 55 50 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a  UPDATE steps */.
46660 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70    ExprList *pExp
46670 72 4c 69 73 74 3b 20 2f 2a 20 56 61 6c 69 64 20  rList; /* Valid 
46680 66 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65  for UPDATE state
46690 6d 65 6e 74 73 20 61 6e 64 20 73 6f 6d 65 74 69  ments and someti
466a0 6d 65 73 20 0a 20 20 20 20 20 20 20 20 20 20 20  mes .           
466b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
466c0 49 4e 53 45 52 54 20 73 74 65 70 73 20 28 77 68  INSERT steps (wh
466d0 65 6e 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 29  en pSelect == 0)
466e0 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 49 64           */.  Id
466f0 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20  List *pIdList;  
46700 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20     /* Valid for 
46710 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
46720 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 54 72 69 67  s only */.  Trig
46730 67 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b 20  gerStep *pNext; 
46740 20 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65 20   /* Next in the 
46750 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 54  link-list */.  T
46760 72 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61 73  riggerStep *pLas
46770 74 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65 6d  t;  /* Last elem
46780 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 74  ent in link-list
46790 2e 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74 20  . Valid for 1st 
467a0 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a  elem only */.};.
467b0 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*. * An instan
467c0 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69  ce of struct Tri
467d0 67 67 65 72 53 74 61 63 6b 20 73 74 6f 72 65 73  ggerStack stores
467e0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 72 65 71   information req
467f0 75 69 72 65 64 20 64 75 72 69 6e 67 20 63 6f 64  uired during cod
46800 65 0a 20 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20  e. * generation 
46810 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67  of a single trig
46820 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 57 68 69  ger program. Whi
46830 6c 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70  le the trigger p
46840 72 6f 67 72 61 6d 20 69 73 20 62 65 69 6e 67 0a  rogram is being.
46850 20 2a 20 63 6f 64 65 64 2c 20 69 74 73 20 61 73   * coded, its as
46860 73 6f 63 69 61 74 65 64 20 54 72 69 67 67 65 72  sociated Trigger
46870 53 74 61 63 6b 20 69 6e 73 74 61 6e 63 65 20 69  Stack instance i
46880 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
46890 74 68 65 0a 20 2a 20 22 70 54 72 69 67 67 65 72  the. * "pTrigger
468a0 53 74 61 63 6b 22 20 6d 65 6d 62 65 72 20 6f 66  Stack" member of
468b0 20 74 68 65 20 50 61 72 73 65 20 73 74 72 75 63   the Parse struc
468c0 74 75 72 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20  ture.. *. * The 
468d0 70 54 61 62 20 6d 65 6d 62 65 72 20 70 6f 69 6e  pTab member poin
468e0 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ts to the table 
468f0 74 68 61 74 20 74 72 69 67 67 65 72 73 20 61 72  that triggers ar
46900 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e  e being coded on
46910 2e 20 54 68 65 20 0a 20 2a 20 6e 65 77 49 64 78  . The . * newIdx
46920 20 6d 65 6d 62 65 72 20 63 6f 6e 74 61 69 6e 73   member contains
46930 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
46940 65 20 76 64 62 65 20 63 75 72 73 6f 72 20 74 68  e vdbe cursor th
46950 61 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65  at points at the
46960 20 74 65 6d 70 0a 20 2a 20 74 61 62 6c 65 20 74   temp. * table t
46970 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 6e  hat stores the n
46980 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e  ew.* references.
46990 20 49 66 20 6e 65 77 2e 2a 20 72 65 66 65 72 65   If new.* refere
469a0 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 76 61 6c  nces are not val
469b0 69 64 0a 20 2a 20 66 6f 72 20 74 68 65 20 74 72  id. * for the tr
469c0 69 67 67 65 72 20 62 65 69 6e 67 20 63 6f 64 65  igger being code
469d0 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61  d (for example a
469e0 6e 20 4f 4e 20 44 45 4c 45 54 45 20 74 72 69 67  n ON DELETE trig
469f0 67 65 72 29 2c 20 74 68 65 6e 20 6e 65 77 49 64  ger), then newId
46a00 78 0a 20 2a 20 69 73 20 73 65 74 20 74 6f 20 2d  x. * is set to -
46a10 31 2e 20 54 68 65 20 6f 6c 64 49 64 78 20 6d 65  1. The oldIdx me
46a20 6d 62 65 72 20 69 73 20 61 6e 61 6c 6f 67 6f 75  mber is analogou
46a30 73 20 74 6f 20 6e 65 77 49 64 78 2c 20 66 6f 72  s to newIdx, for
46a40 20 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63 65   old.* reference
46a50 73 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 4f 4e 20  s.. *. * The ON 
46a60 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20  CONFLICT policy 
46a70 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  to be used for t
46a80 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
46a90 61 6d 20 73 74 65 70 73 20 69 73 20 73 74 6f 72  am steps is stor
46aa0 65 64 20 0a 20 2a 20 61 73 20 74 68 65 20 6f 72  ed . * as the or
46ab0 63 6f 6e 66 20 6d 65 6d 62 65 72 2e 20 49 66 20  conf member. If 
46ac0 74 68 69 73 20 69 73 20 4f 45 5f 44 65 66 61 75  this is OE_Defau
46ad0 6c 74 2c 20 74 68 65 6e 20 74 68 65 20 4f 4e 20  lt, then the ON 
46ae0 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 20  CONFLICT clause 
46af0 0a 20 2a 20 73 70 65 63 69 66 69 65 64 20 66 6f  . * specified fo
46b00 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72 69  r individual tri
46b10 67 67 65 72 73 20 73 74 65 70 73 20 69 73 20 75  ggers steps is u
46b20 73 65 64 2e 0a 20 2a 0a 20 2a 20 73 74 72 75 63  sed.. *. * struc
46b30 74 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 68  t TriggerStack h
46b40 61 73 20 61 20 22 70 4e 65 78 74 22 20 6d 65 6d  as a "pNext" mem
46b50 62 65 72 2c 20 74 6f 20 61 6c 6c 6f 77 20 6c 69  ber, to allow li
46b60 6e 6b 65 64 20 6c 69 73 74 73 20 74 6f 20 62 65  nked lists to be
46b70 0a 20 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 2e  . * constructed.
46b80 20 57 68 65 6e 20 63 6f 64 69 6e 67 20 6e 65 73   When coding nes
46b90 74 65 64 20 74 72 69 67 67 65 72 73 20 28 74 72  ted triggers (tr
46ba0 69 67 67 65 72 73 20 66 69 72 65 64 20 62 79 20  iggers fired by 
46bb0 6f 74 68 65 72 20 74 72 69 67 67 65 72 73 29 0a  other triggers).
46bc0 20 2a 20 65 61 63 68 20 6e 65 73 74 65 64 20 74   * each nested t
46bd0 72 69 67 67 65 72 20 73 74 6f 72 65 73 20 69 74  rigger stores it
46be0 73 20 70 61 72 65 6e 74 20 74 72 69 67 67 65 72  s parent trigger
46bf0 27 73 20 54 72 69 67 67 65 72 53 74 61 63 6b 20  's TriggerStack 
46c00 61 73 20 74 68 65 20 22 70 4e 65 78 74 22 20 0a  as the "pNext" .
46c10 20 2a 20 70 6f 69 6e 74 65 72 2e 20 4f 6e 63 65   * pointer. Once
46c20 20 74 68 65 20 6e 65 73 74 65 64 20 74 72 69 67   the nested trig
46c30 67 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 64  ger has been cod
46c40 65 64 2c 20 74 68 65 20 70 4e 65 78 74 20 76 61  ed, the pNext va
46c50 6c 75 65 20 69 73 20 72 65 73 74 6f 72 65 64 0a  lue is restored.
46c60 20 2a 20 74 6f 20 74 68 65 20 70 54 72 69 67 67   * to the pTrigg
46c70 65 72 53 74 61 63 6b 20 6d 65 6d 62 65 72 20 6f  erStack member o
46c80 66 20 74 68 65 20 50 61 72 73 65 20 73 74 75 63  f the Parse stuc
46c90 74 75 72 65 20 61 6e 64 20 63 6f 64 69 6e 67 20  ture and coding 
46ca0 6f 66 20 74 68 65 20 70 61 72 65 6e 74 0a 20 2a  of the parent. *
46cb0 20 74 72 69 67 67 65 72 20 63 6f 6e 74 69 6e 75   trigger continu
46cc0 65 73 2e 0a 20 2a 0a 20 2a 20 42 65 66 6f 72 65  es.. *. * Before
46cd0 20 61 20 6e 65 73 74 65 64 20 74 72 69 67 67 65   a nested trigge
46ce0 72 20 69 73 20 63 6f 64 65 64 2c 20 74 68 65 20  r is coded, the 
46cf0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 70 6f 69 6e  linked list poin
46d00 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 0a 20  ted to by the . 
46d10 2a 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 20  * pTriggerStack 
46d20 69 73 20 73 63 61 6e 6e 65 64 20 74 6f 20 65 6e  is scanned to en
46d30 73 75 72 65 20 74 68 61 74 20 74 68 65 20 74 72  sure that the tr
46d40 69 67 67 65 72 20 69 73 20 6e 6f 74 20 61 62 6f  igger is not abo
46d50 75 74 20 74 6f 20 62 65 20 63 6f 64 65 64 0a 20  ut to be coded. 
46d60 2a 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 49  * recursively. I
46d70 66 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e  f this condition
46d80 20 69 73 20 64 65 74 65 63 74 65 64 2c 20 74 68   is detected, th
46d90 65 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72  e nested trigger
46da0 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 2e 0a 20   is not coded.. 
46db0 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65  */.struct Trigge
46dc0 72 53 74 61 63 6b 20 7b 0a 20 20 54 61 62 6c 65  rStack {.  Table
46dd0 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
46de0 2f 2a 20 54 61 62 6c 65 20 74 68 61 74 20 74 72  /* Table that tr
46df0 69 67 67 65 72 73 20 61 72 65 20 63 75 72 72 65  iggers are curre
46e00 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 64 65 64  ntly being coded
46e10 20 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77   on */.  int new
46e20 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Idx;          /*
46e30 20 49 6e 64 65 78 20 6f 66 20 76 64 62 65 20 63   Index of vdbe c
46e40 75 72 73 6f 72 20 74 6f 20 22 6e 65 77 22 20 74  ursor to "new" t
46e50 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  emp table */.  i
46e60 6e 74 20 6f 6c 64 49 64 78 3b 20 20 20 20 20 20  nt oldIdx;      
46e70 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
46e80 76 64 62 65 20 63 75 72 73 6f 72 20 74 6f 20 22  vdbe cursor to "
46e90 6f 6c 64 22 20 74 65 6d 70 20 74 61 62 6c 65 20  old" temp table 
46ea0 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b  */.  int orconf;
46eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
46ec0 72 65 6e 74 20 6f 72 63 6f 6e 66 20 70 6f 6c 69  rent orconf poli
46ed0 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f  cy */.  int igno
46ee0 72 65 4a 75 6d 70 3b 20 20 20 20 20 20 2f 2a 20  reJump;      /* 
46ef0 77 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74 6f  where to jump to
46f00 20 66 6f 72 20 61 20 52 41 49 53 45 28 49 47 4e   for a RAISE(IGN
46f10 4f 52 45 29 20 2a 2f 0a 20 20 54 72 69 67 67 65  ORE) */.  Trigge
46f20 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 2f  r *pTrigger;   /
46f30 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 63 75  * The trigger cu
46f40 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f  rrently being co
46f50 64 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ded */.  Trigger
46f60 53 74 61 63 6b 20 2a 70 4e 65 78 74 3b 20 2f 2a  Stack *pNext; /*
46f70 20 4e 65 78 74 20 74 72 69 67 67 65 72 20 64 6f   Next trigger do
46f80 77 6e 20 6f 6e 20 74 68 65 20 74 72 69 67 67 65  wn on the trigge
46f90 72 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b 0a 0a 2f  r stack */.};../
46fa0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
46fb0 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ng structure con
46fc0 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tains informatio
46fd0 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71  n used by the sq
46fe0 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a 20 72 6f  liteFix....** ro
46ff0 75 74 69 6e 65 73 20 61 73 20 74 68 65 79 20 77  utines as they w
47000 61 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 72  alk the parse tr
47010 65 65 20 74 6f 20 6d 61 6b 65 20 64 61 74 61 62  ee to make datab
47020 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 0a 2a  ase references.*
47030 2a 20 65 78 70 6c 69 63 69 74 2e 20 20 0a 2a 2f  * explicit.  .*/
47040 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
47050 44 62 46 69 78 65 72 20 44 62 46 69 78 65 72 3b  DbFixer DbFixer;
47060 0a 73 74 72 75 63 74 20 44 62 46 69 78 65 72 20  .struct DbFixer 
47070 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
47080 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  e;      /* The p
47090 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20  arsing context. 
470a0 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   Error messages 
470b0 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
470c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
470d0 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75  b;    /* Make su
470e0 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 73 20 61  re all objects a
470f0 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  re contained in 
47100 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
47110 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
47120 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 65 20 6f  Type;  /* Type o
47130 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20  f the container 
47140 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72  - used for error
47150 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 63   messages */.  c
47160 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  onst Token *pNam
47170 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  e; /* Name of th
47180 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73  e container - us
47190 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73  ed for error mes
471a0 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  sages */.};../*.
471b0 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
471c0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
471d0 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e  s used to commun
471e0 69 63 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  icate informatio
471f0 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65  n.** from sqlite
47200 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f 50 61 72  3Init and OP_Par
47210 73 65 53 63 68 65 6d 61 20 69 6e 74 6f 20 74 68  seSchema into th
47220 65 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c  e sqlite3InitCal
47230 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65  lback..*/.typede
47240 66 20 73 74 72 75 63 74 20 7b 0a 20 20 73 71 6c  f struct {.  sql
47250 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
47260 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
47270 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a   being initializ
47280 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  ed */.  int iDb;
47290 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30              /* 0
472a0 20 66 6f 72 20 6d 61 69 6e 20 64 61 74 61 62 61   for main databa
472b0 73 65 2e 20 20 31 20 66 6f 72 20 54 45 4d 50 2c  se.  1 for TEMP,
472c0 20 32 2e 2e 20 66 6f 72 20 41 54 54 41 43 48 65   2.. for ATTACHe
472d0 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  d */.  char **pz
472e0 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a 20 45 72  ErrMsg;    /* Er
472f0 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 6f 72  ror message stor
47300 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  ed here */.  int
47310 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
47320 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
47330 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 7d  stored here */.}
47340 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f 2a 0a 2a   InitData;../*.*
47350 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70  * Assuming zIn p
47360 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72  oints to the fir
47370 73 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46  st byte of a UTF
47380 2d 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a  -8 character,.**
47390 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20   advance zIn to 
473a0 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72  point to the fir
473b0 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e  st byte of the n
473c0 65 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63  ext UTF-8 charac
473d0 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ter..*/.#define 
473e0 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
473f0 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20  (zIn) {         
47400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
47410 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29  .  if( (*(zIn++)
47420 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20  )>=0xc0 ){      
47430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47440 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68          \.    wh
47450 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63  ile( (*zIn & 0xc
47460 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b  0)==0x80 ){ zIn+
47470 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  +; }            
47480 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
47490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474b0 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f            \.}../
474c0 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
474d0 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63  CORRUPT_BKPT mac
474e0 72 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72  ro can be either
474f0 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72   a constant (for
47500 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62   production.** b
47510 75 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63  uilds) or a func
47520 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64  tion call (for d
47530 65 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69  ebugging).  If i
47540 74 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20  t is a function 
47550 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f  call,.** it allo
47560 77 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  ws the operator 
47570 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f  to set a breakpo
47580 69 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20  int at the spot 
47590 77 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a  where database.*
475a0 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20  * corruption is 
475b0 66 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a  first detected..
475c0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
475d0 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
475e0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
475f0 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29  te3Corrupt(void)
47600 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ;.# define SQLIT
47610 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73  E_CORRUPT_BKPT s
47620 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a  qlite3Corrupt().
47630 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
47640 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
47650 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  PT SQLITE_CORRUP
47660 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  T.#endif../*.** 
47670 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f  Internal functio
47680 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a  n prototypes.*/.
47690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
476a0 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  nt sqlite3StrICm
476b0 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  p(const char *, 
476c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53  const char *);.S
476d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
476e0 74 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  t sqlite3StrNICm
476f0 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  p(const char *, 
47700 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e  const char *, in
47710 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
47720 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
47730 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61  Number(const cha
47740 72 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 0a  r*, int*, u8);..
47750 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
47760 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c  oid *sqlite3Mall
47770 6f 63 5a 65 72 6f 28 75 6e 73 69 67 6e 65 64 29  ocZero(unsigned)
47780 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
47790 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
477a0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74  MallocZero(sqlit
477b0 65 33 2a 2c 20 75 6e 73 69 67 6e 65 64 29 3b 0a  e3*, unsigned);.
477c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
477d0 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61  oid *sqlite3DbMa
477e0 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a  llocRaw(sqlite3*
477f0 2c 20 75 6e 73 69 67 6e 65 64 29 3b 0a 53 51 4c  , unsigned);.SQL
47800 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
47810 20 2a 73 71 6c 69 74 65 33 53 74 72 44 75 70 28   *sqlite3StrDup(
47820 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
47830 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
47840 72 20 2a 73 71 6c 69 74 65 33 53 74 72 4e 44 75  r *sqlite3StrNDu
47850 70 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69  p(const char*, i
47860 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
47870 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
47880 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65  3DbStrDup(sqlite
47890 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  3*,const char*);
478a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
478b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53  char *sqlite3DbS
478c0 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  trNDup(sqlite3*,
478d0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
478e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
478f0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44  E void *sqlite3D
47900 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73  bReallocOrFree(s
47910 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a  qlite3 *, void *
47920 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
47930 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
47940 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71  ite3DbRealloc(sq
47950 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c  lite3 *, void *,
47960 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
47970 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
47980 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69  ite3MPrintf(sqli
47990 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
479a0 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50  , ...);.SQLITE_P
479b0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
479c0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c  ite3VMPrintf(sql
479d0 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
479e0 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 23 69 66  *, va_list);.#if
479f0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
47a00 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64  TEST) || defined
47a10 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 53  (SQLITE_DEBUG).S
47a20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
47a30 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 62 75  void sqlite3Debu
47a40 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  gPrintf(const ch
47a50 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54  ar*, ...);.SQLIT
47a60 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
47a70 20 2a 73 71 6c 69 74 65 33 54 65 78 74 54 6f 50   *sqlite3TextToP
47a80 74 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  tr(const char*);
47a90 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
47aa0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
47ab0 74 65 33 53 65 74 53 74 72 69 6e 67 28 63 68 61  te3SetString(cha
47ac0 72 20 2a 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49  r **, ...);.SQLI
47ad0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
47ae0 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
47af0 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68  Parse*, const ch
47b00 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54  ar*, ...);.SQLIT
47b10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
47b20 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72  qlite3ErrorClear
47b30 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
47b40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
47b50 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 61  lite3Dequote(cha
47b60 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
47b70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
47b80 44 65 71 75 6f 74 65 45 78 70 72 28 73 71 6c 69  DequoteExpr(sqli
47b90 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  te3*, Expr*);.SQ
47ba0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
47bb0 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43   sqlite3KeywordC
47bc0 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ode(const unsign
47bd0 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a  ed char*, int);.
47be0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
47bf0 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72  nt sqlite3RunPar
47c00 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73  ser(Parse*, cons
47c10 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20 2a 2a  t char*, char **
47c20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
47c30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69  E void sqlite3Fi
47c40 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 73 65  nishCoding(Parse
47c50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
47c60 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
47c70 45 78 70 72 28 73 71 6c 69 74 65 33 2a 2c 20 69  Expr(sqlite3*, i
47c80 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a  nt, Expr*, Expr*
47c90 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b  , const Token*);
47ca0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
47cb0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78  Expr *sqlite3PEx
47cc0 70 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  pr(Parse*, int, 
47cd0 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f  Expr*, Expr*, co
47ce0 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  nst Token*);.SQL
47cf0 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
47d00 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73 74 65   *sqlite3Registe
47d10 72 45 78 70 72 28 50 61 72 73 65 2a 2c 54 6f 6b  rExpr(Parse*,Tok
47d20 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
47d30 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
47d40 65 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65  e3ExprAnd(sqlite
47d50 33 2a 2c 45 78 70 72 2a 2c 20 45 78 70 72 2a 29  3*,Expr*, Expr*)
47d60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
47d70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
47d80 72 53 70 61 6e 28 45 78 70 72 2a 2c 54 6f 6b 65  rSpan(Expr*,Toke
47d90 6e 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  n*,Token*);.SQLI
47da0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
47db0 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63  *sqlite3ExprFunc
47dc0 74 69 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70 72  tion(Parse*,Expr
47dd0 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  List*, Token*);.
47de0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
47df0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
47e00 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50  ssignVarNumber(P
47e10 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  arse*, Expr*);.S
47e20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
47e30 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  id sqlite3ExprDe
47e40 6c 65 74 65 28 45 78 70 72 2a 29 3b 0a 53 51 4c  lete(Expr*);.SQL
47e50 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
47e60 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70  List *sqlite3Exp
47e70 72 4c 69 73 74 41 70 70 65 6e 64 28 50 61 72 73  rListAppend(Pars
47e80 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78 70  e*,ExprList*,Exp
47e90 72 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  r*,Token*);.SQLI
47ea0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
47eb0 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
47ec0 65 6c 65 74 65 28 45 78 70 72 4c 69 73 74 2a 29  elete(ExprList*)
47ed0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
47ee0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
47ef0 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a  (sqlite3*, char*
47f00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
47f10 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
47f20 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a  itCallback(void*
47f30 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63  , int, char**, c
47f40 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har**);.SQLITE_P
47f50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
47f60 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a  te3Pragma(Parse*
47f70 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54  ,Token*,Token*,T
47f80 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  oken*,int);.SQLI
47f90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
47fa0 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
47fb0 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74  rnalSchema(sqlit
47fc0 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e3*, int);.SQLIT
47fd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
47fe0 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65  qlite3BeginParse
47ff0 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Parse*,int);.SQ
48000 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
48010 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49  d sqlite3CommitI
48020 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73  nternalChanges(s
48030 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
48040 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a  _PRIVATE Table *
48050 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74  sqlite3ResultSet
48060 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c  OfSelect(Parse*,
48070 63 68 61 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a  char*,Select*);.
48080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
48090 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d  oid sqlite3OpenM
480a0 61 73 74 65 72 54 61 62 6c 65 28 50 61 72 73 65  asterTable(Parse
480b0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
480c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
480d0 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
480e0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f  Parse*,Token*,To
480f0 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ken*,int,int,int
48100 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
48110 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
48120 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73  e3AddColumn(Pars
48130 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  e*,Token*);.SQLI
48140 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
48150 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c  sqlite3AddNotNul
48160 6c 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  l(Parse*, int);.
48170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
48180 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 50 72  oid sqlite3AddPr
48190 69 6d 61 72 79 4b 65 79 28 50 61 72 73 65 2a 2c  imaryKey(Parse*,
481a0 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c   ExprList*, int,
481b0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
481c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
481d0 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43  sqlite3AddCheckC
481e0 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73 65 2a  onstraint(Parse*
481f0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
48200 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
48210 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79  lite3AddColumnTy
48220 70 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a  pe(Parse*,Token*
48230 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
48240 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
48250 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 50 61  dDefaultValue(Pa
48260 72 73 65 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c  rse*,Expr*);.SQL
48270 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
48280 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61   sqlite3AddColla
48290 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20 63  teType(Parse*, c
482a0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29  onst char*, int)
482b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
482c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64   void sqlite3End
482d0 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f 6b  Table(Parse*,Tok
482e0 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63  en*,Token*,Selec
482f0 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  t*);..SQLITE_PRI
48300 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
48310 33 43 72 65 61 74 65 56 69 65 77 28 50 61 72 73  3CreateView(Pars
48320 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
48330 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c  ,Token*,Select*,
48340 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 21  int,int);..#if !
48350 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
48360 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
48370 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
48380 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a  T_VIRTUALTABLE).
48390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
483a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77   int sqlite3View
483b0 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50  GetColumnNames(P
483c0 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23  arse*,Table*);.#
483d0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
483e0 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
483f0 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0a 23  mnNames(A,B) 0.#
48400 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
48410 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
48420 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73  e3DropTable(Pars
48430 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
48440 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
48450 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
48460 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
48470 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Table*);.SQLITE_
48480 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
48490 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 65  ite3Insert(Parse
484a0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
484b0 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c  rList*, Select*,
484c0 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a   IdList*, int);.
484d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
484e0 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61  oid *sqlite3Arra
484f0 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65  yAllocate(sqlite
48500 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  3*,void*,int,int
48510 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29  ,int*,int*,int*)
48520 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
48530 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
48540 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  IdListAppend(sql
48550 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20  ite3*, IdList*, 
48560 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
48570 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
48580 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49  te3IdListIndex(I
48590 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61  dList*,const cha
485a0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
485b0 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
485c0 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
485d0 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c  d(sqlite3*, SrcL
485e0 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ist*, Token*, To
485f0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
48600 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
48610 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
48620 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73  endFromTerm(Pars
48630 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f  e*, SrcList*, To
48640 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ken*, Token*, To
48650 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20  ken*,.          
48660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48670 20 20 20 20 20 20 20 20 20 20 20 20 53 65 6c 65              Sele
48680 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69  ct*, Expr*, IdLi
48690 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
486a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
486b0 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69  3SrcListShiftJoi
486c0 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b  nType(SrcList*);
486d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
486e0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
486f0 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
48700 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
48710 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
48720 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49  TE void sqlite3I
48730 64 4c 69 73 74 44 65 6c 65 74 65 28 49 64 4c 69  dListDelete(IdLi
48740 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
48750 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
48760 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 53  3SrcListDelete(S
48770 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
48780 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
48790 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
487a0 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54  (Parse*,Token*,T
487b0 6f 6b 65 6e 2a 2c 53 72 63 4c 69 73 74 2a 2c 45  oken*,SrcList*,E
487c0 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 54 6f 6b  xprList*,int,Tok
487d0 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  en*,.           
487e0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b               Tok
487f0 65 6e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  en*, int, int);.
48800 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
48810 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49  oid sqlite3DropI
48820 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 53 72 63  ndex(Parse*, Src
48830 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  List*, int);.SQL
48840 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
48850 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 50 61  sqlite3Select(Pa
48860 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 69  rse*, Select*, i
48870 6e 74 2c 20 69 6e 74 2c 20 53 65 6c 65 63 74 2a  nt, int, Select*
48880 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 63 68 61  , int, int*, cha
48890 72 20 2a 61 66 66 29 3b 0a 53 51 4c 49 54 45 5f  r *aff);.SQLITE_
488a0 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a  PRIVATE Select *
488b0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
488c0 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74  (Parse*,ExprList
488d0 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a  *,SrcList*,Expr*
488e0 2c 45 78 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20  ,ExprList*,.    
488f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48900 20 20 20 20 20 45 78 70 72 2a 2c 45 78 70 72 4c       Expr*,ExprL
48910 69 73 74 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45  ist*,int,Expr*,E
48920 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
48930 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
48940 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 53  e3SelectDelete(S
48950 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
48960 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
48970 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
48980 6b 75 70 28 50 61 72 73 65 2a 2c 20 53 72 63 4c  kup(Parse*, SrcL
48990 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
489a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
489b0 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73  3IsReadOnly(Pars
489c0 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29  e*, Table*, int)
489d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
489e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65   void sqlite3Ope
489f0 6e 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69  nTable(Parse*, i
48a00 6e 74 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62  nt iCur, int iDb
48a10 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a  , Table*, int);.
48a20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
48a30 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
48a40 65 46 72 6f 6d 28 50 61 72 73 65 2a 2c 20 53 72  eFrom(Parse*, Sr
48a50 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 29 3b 0a  cList*, Expr*);.
48a60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
48a70 6f 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74  oid sqlite3Updat
48a80 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73  e(Parse*, SrcLis
48a90 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 45  t*, ExprList*, E
48aa0 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  xpr*, int);.SQLI
48ab0 54 45 5f 50 52 49 56 41 54 45 20 57 68 65 72 65  TE_PRIVATE Where
48ac0 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65  Info *sqlite3Whe
48ad0 72 65 42 65 67 69 6e 28 50 61 72 73 65 2a 2c 20  reBegin(Parse*, 
48ae0 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c  SrcList*, Expr*,
48af0 20 45 78 70 72 4c 69 73 74 2a 2a 29 3b 0a 53 51   ExprList**);.SQ
48b00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
48b10 64 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e  d sqlite3WhereEn
48b20 64 28 57 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53  d(WhereInfo*);.S
48b30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
48b40 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
48b50 64 65 47 65 74 43 6f 6c 75 6d 6e 28 56 64 62 65  deGetColumn(Vdbe
48b60 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
48b70 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
48b80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
48b90 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 2a  3ExprCode(Parse*
48ba0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
48bb0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
48bc0 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64  lite3ExprCodeAnd
48bd0 43 61 63 68 65 28 50 61 72 73 65 2a 2c 20 45 78  Cache(Parse*, Ex
48be0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
48bf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
48c00 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74  ExprCodeExprList
48c10 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73  (Parse*, ExprLis
48c20 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
48c30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
48c40 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73 65  ExprIfTrue(Parse
48c50 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69  *, Expr*, int, i
48c60 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
48c70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
48c80 45 78 70 72 49 66 46 61 6c 73 65 28 50 61 72 73  ExprIfFalse(Pars
48c90 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20  e*, Expr*, int, 
48ca0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
48cb0 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
48cc0 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c  te3FindTable(sql
48cd0 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
48ce0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  *, const char*);
48cf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
48d00 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f  Table *sqlite3Lo
48d10 63 61 74 65 54 61 62 6c 65 28 50 61 72 73 65 2a  cateTable(Parse*
48d20 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f  ,const char*, co
48d30 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
48d40 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78  TE_PRIVATE Index
48d50 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64   *sqlite3FindInd
48d60 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  ex(sqlite3*,cons
48d70 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
48d80 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
48d90 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
48da0 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74  e3UnlinkAndDelet
48db0 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c  eTable(sqlite3*,
48dc0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  int,const char*)
48dd0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
48de0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c   void sqlite3Unl
48df0 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65  inkAndDeleteInde
48e00 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63  x(sqlite3*,int,c
48e10 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
48e20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
48e30 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50   sqlite3Vacuum(P
48e40 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
48e50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
48e60 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72  e3RunVacuum(char
48e70 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53  **, sqlite3*);.S
48e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
48e90 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46  ar *sqlite3NameF
48ea0 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33  romToken(sqlite3
48eb0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
48ec0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
48ed0 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
48ee0 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b  e(Expr*, Expr*);
48ef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
48f00 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 52  int sqlite3ExprR
48f10 65 73 6f 6c 76 65 4e 61 6d 65 73 28 4e 61 6d 65  esolveNames(Name
48f20 43 6f 6e 74 65 78 74 20 2a 2c 20 45 78 70 72 20  Context *, Expr 
48f30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
48f40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
48f50 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  prAnalyzeAggrega
48f60 74 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  tes(NameContext*
48f70 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
48f80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
48f90 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
48fa0 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65  ggList(NameConte
48fb0 78 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a  xt*,ExprList*);.
48fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
48fd0 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56  dbe *sqlite3GetV
48fe0 64 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  dbe(Parse*);.SQL
48ff0 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
49000 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49   *sqlite3CreateI
49010 64 45 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63  dExpr(Parse *, c
49020 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
49030 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
49040 20 73 71 6c 69 74 65 33 52 61 6e 64 6f 6d 6e 65   sqlite3Randomne
49050 73 73 28 69 6e 74 2c 20 76 6f 69 64 2a 29 3b 0a  ss(int, void*);.
49060 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
49070 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62  oid sqlite3Rollb
49080 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29  ackAll(sqlite3*)
49090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
490a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
490b0 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61  eVerifySchema(Pa
490c0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
490d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
490e0 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e  sqlite3BeginTran
490f0 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20  saction(Parse*, 
49100 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
49110 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
49120 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69  3CommitTransacti
49130 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  on(Parse*);.SQLI
49140 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
49150 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54  sqlite3RollbackT
49160 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65  ransaction(Parse
49170 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
49180 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
49190 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70  prIsConstant(Exp
491a0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
491b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
491c0 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74  xprIsConstantNot
491d0 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c  Join(Expr*);.SQL
491e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
491f0 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
49200 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28  stantOrFunction(
49210 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
49220 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
49230 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
49240 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51  Expr*, int*);.SQ
49250 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
49260 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
49270 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
49280 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
49290 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
492a0 65 52 6f 77 44 65 6c 65 74 65 28 73 71 6c 69 74  eRowDelete(sqlit
492b0 65 33 2a 2c 20 56 64 62 65 2a 2c 20 54 61 62 6c  e3*, Vdbe*, Tabl
492c0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  e*, int, int);.S
492d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
492e0 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  id sqlite3Genera
492f0 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65  teRowIndexDelete
49300 28 56 64 62 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  (Vdbe*, Table*, 
49310 69 6e 74 2c 20 63 68 61 72 2a 29 3b 0a 53 51 4c  int, char*);.SQL
49320 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
49330 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
49340 49 6e 64 65 78 4b 65 79 28 56 64 62 65 2a 2c 20  IndexKey(Vdbe*, 
49350 49 6e 64 65 78 2a 2c 20 69 6e 74 29 3b 0a 53 51  Index*, int);.SQ
49360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
49370 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
49380 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b  eConstraintCheck
49390 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c  s(Parse*,Table*,
493a0 69 6e 74 2c 63 68 61 72 2a 2c 69 6e 74 2c 69 6e  int,char*,int,in
493b0 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  t,int,int);.SQLI
493c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
493d0 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49  sqlite3CompleteI
493e0 6e 73 65 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c  nsertion(Parse*,
493f0 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 63 68   Table*, int, ch
49400 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  ar*, int, int, i
49410 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
49420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
49430 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e  lite3OpenTableAn
49440 64 49 6e 64 69 63 65 73 28 50 61 72 73 65 2a 2c  dIndices(Parse*,
49450 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e   Table*, int, in
49460 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
49470 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
49480 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
49490 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  on(Parse*, int, 
494a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
494b0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
494c0 65 33 45 78 70 72 44 75 70 28 73 71 6c 69 74 65  e3ExprDup(sqlite
494d0 33 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  3*,Expr*);.SQLIT
494e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
494f0 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28  qlite3TokenCopy(
49500 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c  sqlite3*,Token*,
49510 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
49520 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
49530 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
49540 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45  stDup(sqlite3*,E
49550 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  xprList*);.SQLIT
49560 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73  E_PRIVATE SrcLis
49570 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  t *sqlite3SrcLis
49580 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 72  tDup(sqlite3*,Sr
49590 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  cList*);.SQLITE_
495a0 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a  PRIVATE IdList *
495b0 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70  sqlite3IdListDup
495c0 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c 69 73 74  (sqlite3*,IdList
495d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
495e0 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
495f0 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69  e3SelectDup(sqli
49600 74 65 33 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53  te3*,Select*);.S
49610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75  QLITE_PRIVATE Fu
49620 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69  ncDef *sqlite3Fi
49630 6e 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74  ndFunction(sqlit
49640 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
49650 69 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b  int,int,u8,int);
49660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
49670 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69  void sqlite3Regi
49680 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74  sterBuiltinFunct
49690 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ions(sqlite3*);.
496a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
496b0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
496c0 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74  terDateTimeFunct
496d0 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ions(sqlite3*);.
496e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
496f0 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
49700 4f 6e 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  On(sqlite3*);.SQ
49710 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
49720 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
49730 66 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  f(sqlite3*);.SQL
49740 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
49750 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
49760 63 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ck(sqlite3*);.SQ
49770 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
49780 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43  d sqlite3ChangeC
49790 6f 6f 6b 69 65 28 73 71 6c 69 74 65 33 2a 2c 20  ookie(sqlite3*, 
497a0 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69  Vdbe*, int);..#i
497b0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
497c0 54 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45  T_TRIGGER.SQLITE
497d0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
497e0 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67  sqlite3BeginTrig
497f0 67 65 72 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  ger(Parse*, Toke
49800 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e  n*,Token*,int,in
49810 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 4c 69 73  t,IdList*,SrcLis
49820 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t*,.            
49830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
49840 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 29 3b 0a  xpr*,int, int);.
49850 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
49860 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e   void sqlite3Fin
49870 69 73 68 54 72 69 67 67 65 72 28 50 61 72 73 65  ishTrigger(Parse
49880 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 2c  *, TriggerStep*,
49890 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
498a0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
498b0 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
498c0 65 72 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  er(Parse*, SrcLi
498d0 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  st*, int);.SQLIT
498e0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
498f0 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
49900 67 65 72 50 74 72 28 50 61 72 73 65 2a 2c 20 54  gerPtr(Parse*, T
49910 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  rigger*);.SQLITE
49920 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
49930 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
49940 69 73 74 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  ist(Parse*, Tabl
49950 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73  e*, int, ExprLis
49960 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
49970 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
49980 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
49990 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70  Parse*, int, Exp
499a0 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 62  rList*, int, Tab
499b0 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  le *, int, int, 
499c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
499d0 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 2c              int,
499e0 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 71   int);.  void sq
499f0 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73  liteViewTriggers
49a00 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
49a10 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70   Expr*, int, Exp
49a20 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
49a30 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
49a40 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
49a50 67 65 72 53 74 65 70 28 54 72 69 67 67 65 72 53  gerStep(TriggerS
49a60 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  tep*);.SQLITE_PR
49a70 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53  IVATE   TriggerS
49a80 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67  tep *sqlite3Trig
49a90 67 65 72 53 65 6c 65 63 74 53 74 65 70 28 73 71  gerSelectStep(sq
49aa0 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 29 3b  lite3*,Select*);
49ab0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
49ac0 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73    TriggerStep *s
49ad0 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73  qlite3TriggerIns
49ae0 65 72 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a  ertStep(sqlite3*
49af0 2c 54 6f 6b 65 6e 2a 2c 20 49 64 4c 69 73 74 2a  ,Token*, IdList*
49b00 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
49b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49b20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 4c 69            ExprLi
49b30 73 74 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 29  st*,Select*,int)
49b40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
49b50 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a     TriggerStep *
49b60 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 70  sqlite3TriggerUp
49b70 64 61 74 65 53 74 65 70 28 73 71 6c 69 74 65 33  dateStep(sqlite3
49b80 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70 72 4c 69 73  *,Token*,ExprLis
49b90 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b  t*, Expr*, int);
49ba0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
49bb0 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73    TriggerStep *s
49bc0 71 6c 69 74 65 33 54 72 69 67 67 65 72 44 65 6c  qlite3TriggerDel
49bd0 65 74 65 53 74 65 70 28 73 71 6c 69 74 65 33 2a  eteStep(sqlite3*
49be0 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 2a 29 3b  ,Token*, Expr*);
49bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
49c00 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65    void sqlite3De
49c10 6c 65 74 65 54 72 69 67 67 65 72 28 54 72 69 67  leteTrigger(Trig
49c20 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
49c30 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
49c40 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
49c50 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 74  eteTrigger(sqlit
49c60 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  e3*,int,const ch
49c70 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  ar*);.#else.# de
49c80 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67  fine sqlite3Trig
49c90 67 65 72 73 45 78 69 73 74 28 41 2c 42 2c 43 2c  gersExist(A,B,C,
49ca0 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e  D,E,F) 0.# defin
49cb0 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  e sqlite3DeleteT
49cc0 72 69 67 67 65 72 28 41 29 0a 23 20 64 65 66 69  rigger(A).# defi
49cd0 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  ne sqlite3DropTr
49ce0 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a 23 20  iggerPtr(A,B).# 
49cf0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55 6e  define sqlite3Un
49d00 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69  linkAndDeleteTri
49d10 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 64 65  gger(A,B,C).# de
49d20 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64 65  fine sqlite3Code
49d30 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 2c 43  RowTrigger(A,B,C
49d40 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 29 20 30 0a  ,D,E,F,G,H,I) 0.
49d50 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
49d60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
49d70 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65  e3JoinType(Parse
49d80 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
49d90 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
49da0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
49db0 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72  sqlite3CreateFor
49dc0 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20  eignKey(Parse*, 
49dd0 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e  ExprList*, Token
49de0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
49df0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
49e00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
49e10 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50  eferForeignKey(P
49e20 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66  arse*, int);.#if
49e30 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
49e40 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53  _AUTHORIZATION.S
49e50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
49e60 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
49e70 52 65 61 64 28 50 61 72 73 65 2a 2c 45 78 70 72  Read(Parse*,Expr
49e80 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c  *,SrcList*);.SQL
49e90 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
49ea0 74 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  t sqlite3AuthChe
49eb0 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63  ck(Parse*,int, c
49ec0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
49ed0 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
49ee0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
49ef0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
49f00 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
49f10 75 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68  ush(Parse*, Auth
49f20 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  Context*, const 
49f30 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
49f40 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
49f50 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
49f60 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a  Pop(AuthContext*
49f70 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
49f80 65 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61  e sqlite3AuthRea
49f90 64 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e  d(a,b,c).# defin
49fa0 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  e sqlite3AuthChe
49fb0 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20  ck(a,b,c,d,e)   
49fc0 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 66   SQLITE_OK.# def
49fd0 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43  ine sqlite3AuthC
49fe0 6f 6e 74 65 78 74 50 75 73 68 28 61 2c 62 2c 63  ontextPush(a,b,c
49ff0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
4a000 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70  e3AuthContextPop
4a010 28 61 29 20 20 28 28 76 6f 69 64 29 28 61 29 29  (a)  ((void)(a))
4a020 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
4a030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4a040 74 65 33 41 74 74 61 63 68 28 50 61 72 73 65 2a  te3Attach(Parse*
4a050 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20  , Expr*, Expr*, 
4a060 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
4a070 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4a080 74 65 33 44 65 74 61 63 68 28 50 61 72 73 65 2a  te3Detach(Parse*
4a090 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
4a0a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4a0b0 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79  ite3BtreeFactory
4a0c0 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 2a  (const sqlite3 *
4a0d0 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
4a0e0 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20  zFilename,.     
4a0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a100 20 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61    int omitJourna
4a110 6c 2c 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 69  l, int nCache, i
4a120 6e 74 20 66 6c 61 67 73 2c 20 42 74 72 65 65 20  nt flags, Btree 
4a130 2a 2a 70 70 42 74 72 65 65 29 3b 0a 53 51 4c 49  **ppBtree);.SQLI
4a140 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4a150 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 44 62  qlite3FixInit(Db
4a160 46 69 78 65 72 2a 2c 20 50 61 72 73 65 2a 2c 20  Fixer*, Parse*, 
4a170 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  int, const char*
4a180 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b  , const Token*);
4a190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4a1a0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72  int sqlite3FixSr
4a1b0 63 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c 20  cList(DbFixer*, 
4a1c0 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
4a1d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4a1e0 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 44  lite3FixSelect(D
4a1f0 62 46 69 78 65 72 2a 2c 20 53 65 6c 65 63 74 2a  bFixer*, Select*
4a200 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4a210 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
4a220 45 78 70 72 28 44 62 46 69 78 65 72 2a 2c 20 45  Expr(DbFixer*, E
4a230 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
4a240 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4a250 33 46 69 78 45 78 70 72 4c 69 73 74 28 44 62 46  3FixExprList(DbF
4a260 69 78 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a  ixer*, ExprList*
4a270 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4a280 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
4a290 54 72 69 67 67 65 72 53 74 65 70 28 44 62 46 69  TriggerStep(DbFi
4a2a0 78 65 72 2a 2c 20 54 72 69 67 67 65 72 53 74 65  xer*, TriggerSte
4a2b0 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  p*);.SQLITE_PRIV
4a2c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41  ATE int sqlite3A
4a2d0 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  toF(const char *
4a2e0 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c  z, double*);.SQL
4a2f0 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71  ITE_API char *sq
4a300 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 69  lite3_snprintf(i
4a310 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  nt,char*,const c
4a320 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54  har*,...);.SQLIT
4a330 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4a340 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f  lite3GetInt32(co
4a350 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2a  nst char *, int*
4a360 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4a370 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74  E int sqlite3Fit
4a380 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20  sIn64Bits(const 
4a390 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  char *);.SQLITE_
4a3a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4a3b0 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28  te3Utf16ByteLen(
4a3c0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74  const void *pDat
4a3d0 61 2c 20 69 6e 74 20 6e 43 68 61 72 29 3b 0a 53  a, int nChar);.S
4a3e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4a3f0 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61  t sqlite3Utf8Cha
4a400 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  rLen(const char 
4a410 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 42 79 74  *pData, int nByt
4a420 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
4a430 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
4a440 66 38 52 65 61 64 28 63 6f 6e 73 74 20 75 38 2a  f8Read(const u8*
4a450 2c 20 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e  , const u8*, con
4a460 73 74 20 75 38 2a 2a 29 3b 0a 53 51 4c 49 54 45  st u8**);.SQLITE
4a470 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4a480 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e  ite3PutVarint(un
4a490 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75  signed char *, u
4a4a0 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
4a4b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
4a4c0 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 75  etVarint(const u
4a4d0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20  nsigned char *, 
4a4e0 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  u64 *);.SQLITE_P
4a4f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4a500 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f  e3GetVarint32(co
4a510 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
4a520 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53 51 4c  r *, u32 *);.SQL
4a530 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4a540 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
4a550 28 75 36 34 20 76 29 3b 0a 53 51 4c 49 54 45 5f  (u64 v);.SQLITE_
4a560 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4a570 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
4a580 79 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e 64  yStr(Vdbe *, Ind
4a590 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ex *);.SQLITE_PR
4a5a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4a5b0 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53  e3TableAffinityS
4a5c0 74 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65  tr(Vdbe *, Table
4a5d0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
4a5e0 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
4a5f0 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
4a600 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
4a610 72 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 5f  r aff2);.SQLITE_
4a620 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4a630 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79  te3IndexAffinity
4a640 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20  Ok(Expr *pExpr, 
4a650 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74  char idx_affinit
4a660 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
4a670 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45  TE char sqlite3E
4a680 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72  xprAffinity(Expr
4a690 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45   *pExpr);.SQLITE
4a6a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4a6b0 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74  ite3Atoi64(const
4a6c0 20 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a 53   char*, i64*);.S
4a6d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4a6e0 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  id sqlite3Error(
4a6f0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63  sqlite3*, int, c
4a700 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
4a710 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4a720 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78  void *sqlite3Hex
4a730 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a 2c  ToBlob(sqlite3*,
4a740 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 3b   const char *z);
4a750 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4a760 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61  int sqlite3TwoPa
4a770 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20  rtName(Parse *, 
4a780 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a  Token *, Token *
4a790 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c  , Token **);.SQL
4a7a0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
4a7b0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45  t char *sqlite3E
4a7c0 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49  rrStr(int);.SQLI
4a7d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4a7e0 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
4a7f0 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b  (Parse *pParse);
4a800 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4a810 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
4a820 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69  FindCollSeq(sqli
4a830 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e  te3*,u8 enc, con
4a840 73 74 20 63 68 61 72 20 2a 2c 69 6e 74 2c 69 6e  st char *,int,in
4a850 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4a860 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
4a870 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
4a880 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
4a890 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
4a8a0 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 3b 0a 53  e, int nName);.S
4a8b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
4a8c0 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78  llSeq *sqlite3Ex
4a8d0 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  prCollSeq(Parse 
4a8e0 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
4a8f0 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Expr);.SQLITE_PR
4a900 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
4a910 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50  te3ExprSetColl(P
4a920 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
4a930 70 72 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a  pr *, Token *);.
4a940 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4a950 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43  nt sqlite3CheckC
4a960 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20  ollSeq(Parse *, 
4a970 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49  CollSeq *);.SQLI
4a980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4a990 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63  qlite3CheckObjec
4a9a0 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63  tName(Parse *, c
4a9b0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
4a9c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4a9d0 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
4a9e0 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20  Changes(sqlite3 
4a9f0 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45  *, int);..SQLITE
4aa00 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
4aa10 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75  oid *sqlite3Valu
4aa20 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61  eText(sqlite3_va
4aa30 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54  lue*, u8);.SQLIT
4aa40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4aa50 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
4aa60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20  sqlite3_value*, 
4aa70 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  u8);.SQLITE_PRIV
4aa80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4aa90 56 61 6c 75 65 53 65 74 53 74 72 28 73 71 6c 69  ValueSetStr(sqli
4aaa0 74 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 2c  te3_value*, int,
4aab0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 38   const void *,u8
4aac0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
4aad0 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 28             void(
4aae0 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
4aaf0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4ab00 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
4ab10 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
4ab20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4ab30 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
4ab40 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
4ab50 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49  sqlite3 *);.SQLI
4ab60 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
4ab70 2a 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38  *sqlite3Utf16to8
4ab80 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73  (sqlite3 *, cons
4ab90 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53  t void*, int);.S
4aba0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4abb0 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  t sqlite3ValueFr
4abc0 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a  omExpr(sqlite3 *
4abd0 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75 38  , Expr *, u8, u8
4abe0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
4abf0 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
4ac00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4ac10 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69  ValueApplyAffini
4ac20 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ty(sqlite3_value
4ac30 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69 66   *, u8, u8);.#if
4ac40 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c  ndef SQLITE_AMAL
4ac50 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f  GAMATION.SQLITE_
4ac60 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e  PRIVATE const un
4ac70 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69  signed char sqli
4ac80 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
4ac90 5d 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  ];.#endif.SQLITE
4aca0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4acb0 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76  lite3RootPageMov
4acc0 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74  ed(Db*, int, int
4acd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4ace0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
4acf0 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 54 6f  index(Parse*, To
4ad00 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  ken*, Token*);.S
4ad10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4ad20 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  id sqlite3AlterF
4ad30 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33  unctions(sqlite3
4ad40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4ad50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
4ad60 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28  lterRenameTable(
4ad70 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
4ad80 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
4ad90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4ada0 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f  lite3GetToken(co
4adb0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
4adc0 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c  r *, int *);.SQL
4add0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4ade0 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
4adf0 72 73 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73  rse(Parse*, cons
4ae00 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53  t char*, ...);.S
4ae10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4ae20 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65  id sqlite3Expire
4ae30 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
4ae40 74 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ts(sqlite3*);.SQ
4ae50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4ae60 64 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62  d sqlite3CodeSub
4ae70 73 65 6c 65 63 74 28 50 61 72 73 65 20 2a 2c 20  select(Parse *, 
4ae80 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  Expr *);.SQLITE_
4ae90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4aea0 74 65 33 53 65 6c 65 63 74 52 65 73 6f 6c 76 65  te3SelectResolve
4aeb0 28 50 61 72 73 65 20 2a 2c 20 53 65 6c 65 63 74  (Parse *, Select
4aec0 20 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20   *, NameContext 
4aed0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4aee0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
4aef0 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62  olumnDefault(Vdb
4af00 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e  e *, Table *, in
4af10 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4af20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
4af30 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c  lterFinishAddCol
4af40 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b  umn(Parse *, Tok
4af50 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  en *);.SQLITE_PR
4af60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4af70 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43  e3AlterBeginAddC
4af80 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53  olumn(Parse *, S
4af90 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54  rcList *);.SQLIT
4afa0 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
4afb0 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c  q *sqlite3GetCol
4afc0 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 43  lSeq(sqlite3*, C
4afd0 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20  ollSeq *, const 
4afe0 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  char *, int);.SQ
4aff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
4b000 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74  r sqlite3Affinit
4b010 79 54 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b 65  yType(const Toke
4b020 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
4b030 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4b040 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20  Analyze(Parse*, 
4b050 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Token*, Token*);
4b060 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4b070 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b  int sqlite3Invok
4b080 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73  eBusyHandler(Bus
4b090 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49  yHandler*);.SQLI
4b0a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4b0b0 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c  qlite3FindDb(sql
4b0c0 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  ite3*, Token*);.
4b0d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4b0e0 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73  nt sqlite3Analys
4b0f0 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c  isLoad(sqlite3*,
4b100 69 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54 45  int iDB);.SQLITE
4b110 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4b120 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45  lite3DefaultRowE
4b130 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49  st(Index*);.SQLI
4b140 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4b150 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c  sqlite3RegisterL
4b160 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c  ikeFunctions(sql
4b170 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ite3*, int);.SQL
4b180 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4b190 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e  sqlite3IsLikeFun
4b1a0 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45  ction(sqlite3*,E
4b1b0 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29  xpr*,int*,char*)
4b1c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4b1d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74   void sqlite3Att
4b1e0 61 63 68 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c  achFunctions(sql
4b1f0 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  ite3 *);.SQLITE_
4b200 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4b210 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46  ite3MinimumFileF
4b220 6f 72 6d 61 74 28 50 61 72 73 65 2a 2c 20 69 6e  ormat(Parse*, in
4b230 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
4b240 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4b250 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 76  ite3SchemaFree(v
4b260 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oid *);.SQLITE_P
4b270 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73  RIVATE Schema *s
4b280 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28  qlite3SchemaGet(
4b290 73 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 65 65  sqlite3 *, Btree
4b2a0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
4b2b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
4b2c0 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c  chemaToIndex(sql
4b2d0 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61  ite3 *db, Schema
4b2e0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
4b2f0 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c  ATE KeyInfo *sql
4b300 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
4b310 28 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 78 20  (Parse *, Index 
4b320 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4b330 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72  TE int sqlite3Cr
4b340 65 61 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33  eateFunc(sqlite3
4b350 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
4b360 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64  , int, int, void
4b370 20 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 29 28   *, .  void (*)(
4b380 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
4b390 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
4b3a0 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  ue **),.  void (
4b3b0 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  *)(sqlite3_conte
4b3c0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
4b3d0 76 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20  value **), void 
4b3e0 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (*)(sqlite3_cont
4b3f0 65 78 74 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  ext*));.SQLITE_P
4b400 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4b410 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65  e3ApiExit(sqlite
4b420 33 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c  3 *db, int);.SQL
4b430 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4b440 20 73 71 6c 69 74 65 33 41 62 6f 72 74 4f 74 68   sqlite3AbortOth
4b450 65 72 41 63 74 69 76 65 56 64 62 65 73 28 73 71  erActiveVdbes(sq
4b460 6c 69 74 65 33 20 2a 2c 20 56 64 62 65 20 2a 29  lite3 *, Vdbe *)
4b470 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4b480 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e   int sqlite3Open
4b490 54 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72  TempDatabase(Par
4b4a0 73 65 20 2a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 54  se *);.../*.** T
4b4b0 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  he interface to 
4b4c0 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 61  the LEMON-genera
4b4d0 74 65 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 51  ted parser.*/.SQ
4b4e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4b4f0 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72  d *sqlite3Parser
4b500 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 73  Alloc(void*(*)(s
4b510 69 7a 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 5f  ize_t));.SQLITE_
4b520 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4b530 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28 76  ite3ParserFree(v
4b540 6f 69 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f  oid*, void(*)(vo
4b550 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  id*));.SQLITE_PR
4b560 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4b570 65 33 50 61 72 73 65 72 28 76 6f 69 64 2a 2c 20  e3Parser(void*, 
4b580 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 73  int, Token, Pars
4b590 65 2a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  e*);..#ifndef SQ
4b5a0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
4b5b0 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f  XTENSION.SQLITE_
4b5c0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
4b5d0 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e  qlite3CloseExten
4b5e0 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b  sions(sqlite3*);
4b5f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4b600 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74    int sqlite3Aut
4b610 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28  oLoadExtensions(
4b620 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65  sqlite3*);.#else
4b630 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
4b640 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73  3CloseExtensions
4b650 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (X).# define sql
4b660 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65  ite3AutoLoadExte
4b670 6e 73 69 6f 6e 73 28 58 29 20 20 53 51 4c 49 54  nsions(X)  SQLIT
4b680 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  E_OK.#endif..#if
4b690 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
4b6a0 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51  _SHARED_CACHE.SQ
4b6b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4b6c0 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65  oid sqlite3Table
4b6d0 4c 6f 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e  Lock(Parse *, in
4b6e0 74 2c 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73  t, int, u8, cons
4b6f0 74 20 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65  t char *);.#else
4b700 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
4b710 65 33 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c  e3TableLock(v,w,
4b720 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23  x,y,z).#endif..#
4b730 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
4b740 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
4b750 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74     int sqlite3Ut
4b760 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63  f8To8(unsigned c
4b770 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f  har*);.#endif../
4b780 2a 0a 2a 2a 20 54 68 65 20 4d 61 6c 6c 6f 63 44  *.** The MallocD
4b790 69 73 61 6c 6c 6f 77 28 29 20 61 6e 64 20 4d 61  isallow() and Ma
4b7a0 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 72 6f 75 74  llocAllow() rout
4b7b0 69 6e 65 73 20 61 72 65 20 6c 69 6b 65 20 61 73  ines are like as
4b7c0 73 65 72 74 73 2e 0a 2a 2a 20 43 61 6c 6c 20 74  serts..** Call t
4b7d0 68 65 6d 20 61 72 6f 75 6e 64 20 61 20 73 65 63  hem around a sec
4b7e0 74 69 6f 6e 20 6f 66 20 63 6f 64 65 20 74 68 61  tion of code tha
4b7f0 74 20 79 6f 75 20 64 6f 20 6e 6f 74 20 65 78 70  t you do not exp
4b800 65 63 74 20 74 6f 20 64 6f 0a 2a 2a 20 61 6e 79  ect to do.** any
4b810 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
4b820 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  on..*/.#ifdef SQ
4b830 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 53 51  LITE_MEMDEBUG.SQ
4b840 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4b850 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  oid sqlite3Mallo
4b860 63 44 69 73 61 6c 6c 6f 77 28 76 6f 69 64 29 3b  cDisallow(void);
4b870 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4b880 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61    void sqlite3Ma
4b890 6c 6c 6f 63 41 6c 6c 6f 77 28 76 6f 69 64 29 3b  llocAllow(void);
4b8a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4b8b0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61    void sqlite3Ma
4b8c0 6c 6c 6f 63 42 65 6e 69 67 6e 46 61 69 6c 75 72  llocBenignFailur
4b8d0 65 28 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20  e(int);.#else.# 
4b8e0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 61  define sqlite3Ma
4b8f0 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 0a 23  llocDisallow().#
4b900 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d   define sqlite3M
4b910 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 0a 23 20 64  allocAllow().# d
4b920 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 61 6c  efine sqlite3Mal
4b930 6c 6f 63 42 65 6e 69 67 6e 46 61 69 6c 75 72 65  locBenignFailure
4b940 28 78 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66  (x).#endif...#if
4b950 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
4b960 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20  VIRTUALTABLE.#  
4b970 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74  define sqlite3Vt
4b980 61 62 43 6c 65 61 72 28 58 29 0a 23 20 20 64 65  abClear(X).#  de
4b990 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
4b9a0 53 79 6e 63 28 58 2c 59 29 20 28 59 29 0a 23 20  Sync(X,Y) (Y).# 
4b9b0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
4b9c0 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23  tabRollback(X).#
4b9d0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
4b9e0 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 65  VtabCommit(X).#e
4b9f0 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  lse.SQLITE_PRIVA
4ba00 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74  TE    void sqlit
4ba10 65 33 56 74 61 62 43 6c 65 61 72 28 54 61 62 6c  e3VtabClear(Tabl
4ba20 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
4ba30 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74  ATE    int sqlit
4ba40 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74  e3VtabSync(sqlit
4ba50 65 33 20 2a 64 62 2c 20 69 6e 74 20 72 63 29 3b  e3 *db, int rc);
4ba60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4ba70 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74     int sqlite3Vt
4ba80 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74  abRollback(sqlit
4ba90 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f  e3 *db);.SQLITE_
4baa0 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73  PRIVATE    int s
4bab0 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
4bac0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23  (sqlite3 *db);.#
4bad0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
4bae0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4baf0 33 56 74 61 62 4c 6f 63 6b 28 73 71 6c 69 74 65  3VtabLock(sqlite
4bb00 33 5f 76 74 61 62 2a 29 3b 0a 53 51 4c 49 54 45  3_vtab*);.SQLITE
4bb10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4bb20 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
4bb30 73 71 6c 69 74 65 33 2a 2c 20 73 71 6c 69 74 65  sqlite3*, sqlite
4bb40 33 5f 76 74 61 62 2a 29 3b 0a 53 51 4c 49 54 45  3_vtab*);.SQLITE
4bb50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4bb60 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61  lite3VtabBeginPa
4bb70 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  rse(Parse*, Toke
4bb80 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65  n*, Token*, Toke
4bb90 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
4bba0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4bbb0 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28  VtabFinishParse(
4bbc0 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Parse*, Token*);
4bbd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4bbe0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
4bbf0 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a 29 3b  ArgInit(Parse*);
4bc00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4bc10 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
4bc20 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 65 2a  ArgExtend(Parse*
4bc30 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
4bc40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4bc50 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65  lite3VtabCallCre
4bc60 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ate(sqlite3*, in
4bc70 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  t, const char *,
4bc80 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54   char **);.SQLIT
4bc90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4bca0 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e  lite3VtabCallCon
4bcb0 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54 61 62  nect(Parse*, Tab
4bcc0 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  le*);.SQLITE_PRI
4bcd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4bce0 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28  VtabCallDestroy(
4bcf0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63  sqlite3*, int, c
4bd00 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
4bd10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4bd20 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
4bd30 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 73 71 6c  n(sqlite3 *, sql
4bd40 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a 53 51  ite3_vtab *);.SQ
4bd50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e  LITE_PRIVATE Fun
4bd60 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61  cDef *sqlite3Vta
4bd70 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f  bOverloadFunctio
4bd80 6e 28 73 71 6c 69 74 65 33 20 2a 2c 46 75 6e 63  n(sqlite3 *,Func
4bd90 44 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67 2c 20  Def*, int nArg, 
4bda0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
4bdb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4bdc0 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69  te3InvalidFuncti
4bdd0 6f 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  on(sqlite3_conte
4bde0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
4bdf0 76 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49 54 45  value**);.SQLITE
4be00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4be10 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64  ite3Reprepare(Vd
4be20 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
4be30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4be40 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65  3ExprListCheckLe
4be50 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45 78 70  ngth(Parse*, Exp
4be60 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e  rList*, int, con
4be70 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
4be80 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
4be90 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79  q *sqlite3Binary
4bea0 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 50  CompareCollSeq(P
4beb0 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20  arse *, Expr *, 
4bec0 45 78 70 72 20 2a 29 3b 0a 0a 23 69 66 64 65 66  Expr *);..#ifdef
4bed0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
4bee0 54 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49  TOMIC_WRITE.SQLI
4bef0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
4bf00 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f   sqlite3JournalO
4bf10 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20  pen(sqlite3_vfs 
4bf20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
4bf30 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c   sqlite3_file *,
4bf40 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
4bf50 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
4bf60 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
4bf70 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ize(sqlite3_vfs 
4bf80 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4bf90 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
4bfa0 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71  JournalCreate(sq
4bfb0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23  lite3_file *);.#
4bfc0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73  else.  #define s
4bfd0 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a  qlite3JournalSiz
4bfe0 65 28 70 56 66 73 29 20 28 28 70 56 66 73 29 2d  e(pVfs) ((pVfs)-
4bff0 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69  >szOsFile).#endi
4c000 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  f..#if defined(S
4c010 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 53  QLITE_TEST) || S
4c020 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
4c030 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52  EPTH>0.SQLITE_PR
4c040 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
4c050 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
4c060 74 28 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54  t(Expr *);.SQLIT
4c070 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
4c080 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
4c090 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a  rHeight(Select *
4c0a0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
4c0b0 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 53 65  ne sqlite3ExprSe
4c0c0 74 48 65 69 67 68 74 28 78 29 0a 23 65 6e 64 69  tHeight(x).#endi
4c0d0 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
4c0e0 45 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74  E u32 sqlite3Get
4c0f0 34 62 79 74 65 28 63 6f 6e 73 74 20 75 38 2a 29  4byte(const u8*)
4c100 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c110 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 75 74   void sqlite3Put
4c120 34 62 79 74 65 28 75 38 2a 2c 20 75 33 32 29 3b  4byte(u8*, u32);
4c130 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
4c140 53 53 45 0a 23 69 6e 63 6c 75 64 65 20 22 73 73  SSE.#include "ss
4c150 65 49 6e 74 2e 68 22 0a 23 65 6e 64 69 66 0a 0a  eInt.h".#endif..
4c160 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
4c170 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
4c180 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
4c190 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c  3ParserTrace(FIL
4c1a0 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e  E*, char *);.#en
4c1b0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  dif../*.** If th
4c1c0 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20  e SQLITE_ENABLE 
4c1d0 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 74  IOTRACE exists t
4c1e0 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76  hen the global v
4c1f0 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74  ariable.** sqlit
4c200 65 33 5f 69 6f 5f 74 72 61 63 65 20 69 73 20 61  e3_io_trace is a
4c210 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72   pointer to a pr
4c220 69 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e  intf-like routin
4c230 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69  e used to.** pri
4c240 6e 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d  nt I/O tracing m
4c250 65 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66  essages. .*/.#if
4c260 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
4c270 45 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69  E_IOTRACE.# defi
4c280 6e 65 20 49 4f 54 52 41 43 45 28 41 29 20 20 69  ne IOTRACE(A)  i
4c290 66 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 74 72  f( sqlite3_io_tr
4c2a0 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 5f 69  ace ){ sqlite3_i
4c2b0 6f 5f 74 72 61 63 65 20 41 3b 20 7d 0a 53 51 4c  o_trace A; }.SQL
4c2c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4c2d0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f  id sqlite3VdbeIO
4c2e0 54 72 61 63 65 53 71 6c 28 56 64 62 65 2a 29 3b  TraceSql(Vdbe*);
4c2f0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
4c300 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64 65 66  IOTRACE(A).# def
4c310 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 49  ine sqlite3VdbeI
4c320 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e  OTraceSql(X).#en
4c330 64 69 66 0a 53 51 4c 49 54 45 5f 45 58 54 45 52  dif.SQLITE_EXTER
4c340 4e 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33  N void (*sqlite3
4c350 5f 69 6f 5f 74 72 61 63 65 29 28 63 6f 6e 73 74  _io_trace)(const
4c360 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 0a 23 65   char*,...);..#e
4c370 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
4c380 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
4c390 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
4c3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c3c0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
4c3d0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
4c3e0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
4c3f0 66 66 20 69 6e 20 64 61 74 65 2e 63 20 2a 2a 2a  ff in date.c ***
4c400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c410 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c  ****/.#include <
4c420 63 74 79 70 65 2e 68 3e 0a 23 69 6e 63 6c 75 64  ctype.h>.#includ
4c430 65 20 3c 74 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e  e <time.h>..#ifn
4c440 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
4c450 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 0a  DATETIME_FUNCS..
4c460 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72  /*.** A structur
4c470 65 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20  e for holding a 
4c480 73 69 6e 67 6c 65 20 64 61 74 65 20 61 6e 64 20  single date and 
4c490 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  time..*/.typedef
4c4a0 20 73 74 72 75 63 74 20 44 61 74 65 54 69 6d 65   struct DateTime
4c4b0 20 44 61 74 65 54 69 6d 65 3b 0a 73 74 72 75 63   DateTime;.struc
4c4c0 74 20 44 61 74 65 54 69 6d 65 20 7b 0a 20 20 64  t DateTime {.  d
4c4d0 6f 75 62 6c 65 20 72 4a 44 3b 20 20 20 20 20 20  ouble rJD;      
4c4e0 2f 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61  /* The julian da
4c4f0 79 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e  y number */.  in
4c500 74 20 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 2f  t Y, M, D;     /
4c510 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61  * Year, month, a
4c520 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20  nd day */.  int 
4c530 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20  h, m;        /* 
4c540 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 73  Hour and minutes
4c550 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20   */.  int tz;   
4c560 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f         /* Timezo
4c570 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e  ne offset in min
4c580 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  utes */.  double
4c590 20 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65   s;        /* Se
4c5a0 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72 20  conds */.  char 
4c5b0 76 61 6c 69 64 59 4d 44 3b 20 20 20 2f 2a 20 54  validYMD;   /* T
4c5c0 72 75 65 20 69 66 20 59 2c 4d 2c 44 20 61 72 65  rue if Y,M,D are
4c5d0 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72   valid */.  char
4c5e0 20 76 61 6c 69 64 48 4d 53 3b 20 20 20 2f 2a 20   validHMS;   /* 
4c5f0 54 72 75 65 20 69 66 20 68 2c 6d 2c 73 20 61 72  True if h,m,s ar
4c600 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61  e valid */.  cha
4c610 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 20 2f 2a  r validJD;    /*
4c620 20 54 72 75 65 20 69 66 20 72 4a 44 20 69 73 20   True if rJD is 
4c630 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20  valid */.  char 
4c640 76 61 6c 69 64 54 5a 3b 20 20 20 20 2f 2a 20 54  validTZ;    /* T
4c650 72 75 65 20 69 66 20 74 7a 20 69 73 20 76 61 6c  rue if tz is val
4c660 69 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a  id */.};.../*.**
4c670 20 43 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69   Convert zDate i
4c680 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  nto one or more 
4c690 69 6e 74 65 67 65 72 73 2e 20 20 41 64 64 69 74  integers.  Addit
4c6a0 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a  ional arguments.
4c6b0 2a 2a 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70  ** come in group
4c6c0 73 20 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77  s of 5 as follow
4c6d0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e  s:.**.**       N
4c6e0 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66         number of
4c6f0 20 64 69 67 69 74 73 20 69 6e 20 74 68 65 20 69   digits in the i
4c700 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20  nteger.**       
4c710 6d 69 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20  min     minimum 
4c720 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66  allowed value of
4c730 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   the integer.** 
4c740 20 20 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61        max     ma
4c750 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61  ximum allowed va
4c760 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67  lue of the integ
4c770 65 72 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74  er.**       next
4c780 43 20 20 20 66 69 72 73 74 20 63 68 61 72 61 63  C   first charac
4c790 74 65 72 20 61 66 74 65 72 20 74 68 65 20 69 6e  ter after the in
4c7a0 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70  teger.**       p
4c7b0 56 61 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20  Val    where to 
4c7c0 77 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 65  write the intege
4c7d0 72 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  rs value..**.** 
4c7e0 43 6f 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74  Conversions cont
4c7f0 69 6e 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77  inue until one w
4c800 69 74 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20  ith nextC==0 is 
4c810 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20  encountered..** 
4c820 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  The function ret
4c830 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
4c840 6f 66 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f  of successful co
4c850 6e 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  nversions..*/.st
4c860 61 74 69 63 20 69 6e 74 20 67 65 74 44 69 67 69  atic int getDigi
4c870 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ts(const char *z
4c880 44 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  Date, ...){.  va
4c890 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20  _list ap;.  int 
4c8a0 76 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20  val;.  int N;.  
4c8b0 69 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d  int min;.  int m
4c8c0 61 78 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b  ax;.  int nextC;
4c8d0 0a 20 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20  .  int *pVal;.  
4c8e0 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76  int cnt = 0;.  v
4c8f0 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74  a_start(ap, zDat
4c900 65 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20  e);.  do{.    N 
4c910 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
4c920 29 3b 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f  );.    min = va_
4c930 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
4c940 20 20 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61    max = va_arg(a
4c950 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78  p, int);.    nex
4c960 74 43 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  tC = va_arg(ap, 
4c970 69 6e 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d  int);.    pVal =
4c980 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a   va_arg(ap, int*
4c990 29 3b 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a  );.    val = 0;.
4c9a0 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29      while( N-- )
4c9b0 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 64  {.      if( !isd
4c9c0 69 67 69 74 28 2a 28 75 38 2a 29 7a 44 61 74 65  igit(*(u8*)zDate
4c9d0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  ) ){.        got
4c9e0 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b  o end_getDigits;
4c9f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76  .      }.      v
4ca00 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a  al = val*10 + *z
4ca10 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20  Date - '0';.    
4ca20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d    zDate++;.    }
4ca30 0a 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e  .    if( val<min
4ca40 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28   || val>max || (
4ca50 6e 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74  nextC!=0 && next
4ca60 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20  C!=*zDate) ){.  
4ca70 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74      goto end_get
4ca80 44 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20  Digits;.    }.  
4ca90 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20    *pVal = val;. 
4caa0 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
4cab0 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28  cnt++;.  }while(
4cac0 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65   nextC );.end_ge
4cad0 74 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e  tDigits:.  va_en
4cae0 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  d(ap);.  return 
4caf0 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  cnt;.}../*.** Re
4cb00 61 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d  ad text from z[]
4cb10 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74   and convert int
4cb20 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  o a floating poi
4cb30 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75  nt number.  Retu
4cb40 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  rn.** the number
4cb50 20 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65   of digits conve
4cb60 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rted..*/.#define
4cb70 20 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65   getValue sqlite
4cb80 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  3AtoF../*.** Par
4cb90 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78  se a timezone ex
4cba0 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65  tension on the e
4cbb0 6e 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d  nd of a date-tim
4cbc0 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73  e..** The extens
4cbd0 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ion is of the fo
4cbe0 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  rm:.**.**       
4cbf0 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a   (+/-)HH:MM.**.*
4cc00 2a 20 49 66 20 74 68 65 20 70 61 72 73 65 20 69  * If the parse i
4cc10 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 77 72  s successful, wr
4cc20 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
4cc30 66 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20  f minutes.** of 
4cc40 63 68 61 6e 67 65 20 69 6e 20 2a 70 6e 4d 69 6e  change in *pnMin
4cc50 20 61 6e 64 20 72 65 74 75 72 6e 20 30 2e 20 20   and return 0.  
4cc60 49 66 20 61 20 70 61 72 73 65 72 20 65 72 72 6f  If a parser erro
4cc70 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 72 65 74  r occurs,.** ret
4cc80 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d  urn 0..**.** A m
4cc90 69 73 73 69 6e 67 20 73 70 65 63 69 66 69 65 72  issing specifier
4cca0 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72   is not consider
4ccb0 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  ed an error..*/.
4ccc0 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65  static int parse
4ccd0 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 63  Timezone(const c
4cce0 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65  har *zDate, Date
4ccf0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Time *p){.  int 
4cd00 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  sgn = 0;.  int n
4cd10 48 72 2c 20 6e 4d 6e 3b 0a 20 20 77 68 69 6c 65  Hr, nMn;.  while
4cd20 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a 29  ( isspace(*(u8*)
4cd30 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b  zDate) ){ zDate+
4cd40 2b 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30  +; }.  p->tz = 0
4cd50 3b 0a 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d  ;.  if( *zDate==
4cd60 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d  '-' ){.    sgn =
4cd70 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   -1;.  }else if(
4cd80 20 2a 7a 44 61 74 65 3d 3d 27 2b 27 20 29 7b 0a   *zDate=='+' ){.
4cd90 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20      sgn = +1;.  
4cda0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
4cdb0 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 20 20 7d  n *zDate!=0;.  }
4cdc0 0a 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66  .  zDate++;.  if
4cdd0 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74  ( getDigits(zDat
4cde0 65 2c 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27  e, 2, 0, 14, ':'
4cdf0 2c 20 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39  , &nHr, 2, 0, 59
4ce00 2c 20 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b  , 0, &nMn)!=2 ){
4ce10 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
4ce20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b   }.  zDate += 5;
4ce30 0a 20 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28  .  p->tz = sgn*(
4ce40 6e 4d 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 20  nMn + nHr*60);. 
4ce50 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28   while( isspace(
4ce60 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20  *(u8*)zDate) ){ 
4ce70 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74  zDate++; }.  ret
4ce80 75 72 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d  urn *zDate!=0;.}
4ce90 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69  ../*.** Parse ti
4cea0 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  mes of the form 
4ceb0 48 48 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53  HH:MM or HH:MM:S
4cec0 53 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46  S or HH:MM:SS.FF
4ced0 46 46 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d  FF..** The HH, M
4cee0 4d 2c 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65  M, and SS must e
4cef0 61 63 68 20 62 65 20 65 78 61 63 74 6c 79 20 32  ach be exactly 2
4cf00 20 64 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a   digits.  The.**
4cf10 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f   fractional seco
4cf20 6e 64 73 20 46 46 46 46 20 63 61 6e 20 62 65 20  nds FFFF can be 
4cf30 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69  one or more digi
4cf40 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ts..**.** Return
4cf50 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61   1 if there is a
4cf60 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61   parsing error a
4cf70 6e 64 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e  nd 0 on success.
4cf80 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
4cf90 61 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74  arseHhMmSs(const
4cfa0 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61   char *zDate, Da
4cfb0 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
4cfc0 74 20 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75  t h, m, s;.  dou
4cfd0 62 6c 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20  ble ms = 0.0;.  
4cfe0 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44  if( getDigits(zD
4cff0 61 74 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27  ate, 2, 0, 24, '
4d000 3a 27 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39  :', &h, 2, 0, 59
4d010 2c 20 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20  , 0, &m)!=2 ){. 
4d020 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
4d030 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20  .  zDate += 5;. 
4d040 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27   if( *zDate==':'
4d050 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b   ){.    zDate++;
4d060 0a 20 20 20 20 69 66 28 20 67 65 74 44 69 67 69  .    if( getDigi
4d070 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20  ts(zDate, 2, 0, 
4d080 35 39 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b  59, 0, &s)!=1 ){
4d090 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
4d0a0 0a 20 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65  .    }.    zDate
4d0b0 20 2b 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a   += 2;.    if( *
4d0c0 7a 44 61 74 65 3d 3d 27 2e 27 20 26 26 20 69 73  zDate=='.' && is
4d0d0 64 69 67 69 74 28 28 75 38 29 7a 44 61 74 65 5b  digit((u8)zDate[
4d0e0 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75  1]) ){.      dou
4d0f0 62 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30  ble rScale = 1.0
4d100 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b  ;.      zDate++;
4d110 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73  .      while( is
4d120 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 44 61 74  digit(*(u8*)zDat
4d130 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73  e) ){.        ms
4d140 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44   = ms*10.0 + *zD
4d150 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  ate - '0';.     
4d160 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e     rScale *= 10.
4d170 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65  0;.        zDate
4d180 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
4d190 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a    ms /= rScale;.
4d1a0 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
4d1b0 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20     s = 0;.  }.  
4d1c0 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a  p->validJD = 0;.
4d1d0 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20    p->validHMS = 
4d1e0 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20  1;.  p->h = h;. 
4d1f0 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e   p->m = m;.  p->
4d200 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66  s = s + ms;.  if
4d210 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28  ( parseTimezone(
4d220 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75  zDate, p) ) retu
4d230 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64  rn 1;.  p->valid
4d240 54 5a 20 3d 20 70 2d 3e 74 7a 21 3d 30 3b 0a 20  TZ = p->tz!=0;. 
4d250 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
4d260 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d  .** Convert from
4d270 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
4d280 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e 20 64  M:SS to julian d
4d290 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73 20 61  ay.  We always a
4d2a0 73 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 74 68  ssume.** that th
4d2b0 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 73 20  e YYYY-MM-DD is 
4d2c0 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
4d2d0 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e   Gregorian calen
4d2e0 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72  dar..**.** Refer
4d2f0 65 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 61 67  ence:  Meeus pag
4d300 65 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 20 76  e 61.*/.static v
4d310 6f 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 44 61  oid computeJD(Da
4d320 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
4d330 74 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 42 2c  t Y, M, D, A, B,
4d340 20 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 28 20   X1, X2;..  if( 
4d350 70 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 65 74  p->validJD ) ret
4d360 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61  urn;.  if( p->va
4d370 6c 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 59 20  lidYMD ){.    Y 
4d380 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 3d 20  = p->Y;.    M = 
4d390 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 70 2d  p->M;.    D = p-
4d3a0 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  >D;.  }else{.   
4d3b0 20 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a 20 49   Y = 2000;  /* I
4d3c0 66 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 66 69  f no YMD specifi
4d3d0 65 64 2c 20 61 73 73 75 6d 65 20 32 30 30 30 2d  ed, assume 2000-
4d3e0 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 4d 20  Jan-01 */.    M 
4d3f0 3d 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 3b 0a  = 1;.    D = 1;.
4d400 20 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 20 29    }.  if( M<=2 )
4d410 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 20 4d  {.    Y--;.    M
4d420 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 41 20   += 12;.  }.  A 
4d430 3d 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d 20 32  = Y/100;.  B = 2
4d440 20 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 20 20   - A + (A/4);.  
4d450 58 31 20 3d 20 33 36 35 2e 32 35 2a 28 59 2b 34  X1 = 365.25*(Y+4
4d460 37 31 36 29 3b 0a 20 20 58 32 20 3d 20 33 30 2e  716);.  X2 = 30.
4d470 36 30 30 31 2a 28 4d 2b 31 29 3b 0a 20 20 70 2d  6001*(M+1);.  p-
4d480 3e 72 4a 44 20 3d 20 58 31 20 2b 20 58 32 20 2b  >rJD = X1 + X2 +
4d490 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 3b   D + B - 1524.5;
4d4a0 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20  .  p->validJD = 
4d4b0 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69  1;.  if( p->vali
4d4c0 64 48 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 72  dHMS ){.    p->r
4d4d0 4a 44 20 2b 3d 20 28 70 2d 3e 68 2a 33 36 30 30  JD += (p->h*3600
4d4e0 2e 30 20 2b 20 70 2d 3e 6d 2a 36 30 2e 30 20 2b  .0 + p->m*60.0 +
4d4f0 20 70 2d 3e 73 29 2f 38 36 34 30 30 2e 30 3b 0a   p->s)/86400.0;.
4d500 20 20 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64      if( p->valid
4d510 54 5a 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72  TZ ){.      p->r
4d520 4a 44 20 2d 3d 20 70 2d 3e 74 7a 2a 36 30 2f 38  JD -= p->tz*60/8
4d530 36 34 30 30 2e 30 3b 0a 20 20 20 20 20 20 70 2d  6400.0;.      p-
4d540 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20  >validYMD = 0;. 
4d550 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53       p->validHMS
4d560 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76   = 0;.      p->v
4d570 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20  alidTZ = 0;.    
4d580 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  }.  }.}../*.** P
4d590 61 72 73 65 20 64 61 74 65 73 20 6f 66 20 74 68  arse dates of th
4d5a0 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
4d5b0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
4d5c0 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20 20 20  M:SS.FFF.**     
4d5d0 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d  YYYY-MM-DD HH:MM
4d5e0 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d  :SS.**     YYYY-
4d5f0 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a 20 20  MM-DD HH:MM.**  
4d600 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2a     YYYY-MM-DD.**
4d610 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 72 65  .** Write the re
4d620 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 44 61  sult into the Da
4d630 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65  teTime structure
4d640 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a 2a 2a   and return 0.**
4d650 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20   on success and 
4d660 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73  1 if the input s
4d670 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 20 77  tring is not a w
4d680 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 64 61  ell-formed.** da
4d690 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  te..*/.static in
4d6a0 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28  t parseYyyyMmDd(
4d6b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74  const char *zDat
4d6c0 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  e, DateTime *p){
4d6d0 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20  .  int Y, M, D, 
4d6e0 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44 61 74  neg;..  if( zDat
4d6f0 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  e[0]=='-' ){.   
4d700 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 6e 65   zDate++;.    ne
4d710 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  g = 1;.  }else{.
4d720 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d      neg = 0;.  }
4d730 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73  .  if( getDigits
4d740 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39 39 2c  (zDate,4,0,9999,
4d750 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c 27 2d  '-',&Y,2,1,12,'-
4d760 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c 26 44  ',&M,2,1,31,0,&D
4d770 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=3 ){.    retu
4d780 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74  rn 1;.  }.  zDat
4d790 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65  e += 10;.  while
4d7a0 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a 29  ( isspace(*(u8*)
4d7b0 7a 44 61 74 65 29 20 7c 7c 20 27 54 27 3d 3d 2a  zDate) || 'T'==*
4d7c0 28 75 38 2a 29 7a 44 61 74 65 20 29 7b 20 7a 44  (u8*)zDate ){ zD
4d7d0 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 70  ate++; }.  if( p
4d7e0 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 65  arseHhMmSs(zDate
4d7f0 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  , p)==0 ){.    /
4d800 2a 20 57 65 20 67 6f 74 20 74 68 65 20 74 69 6d  * We got the tim
4d810 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28  e */.  }else if(
4d820 20 2a 7a 44 61 74 65 3d 3d 30 20 29 7b 0a 20 20   *zDate==0 ){.  
4d830 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20    p->validHMS = 
4d840 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
4d850 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
4d860 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a  p->validJD = 0;.
4d870 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20    p->validYMD = 
4d880 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 6e 65 67 20  1;.  p->Y = neg 
4d890 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 70 2d 3e 4d  ? -Y : Y;.  p->M
4d8a0 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 20 3d 20 44   = M;.  p->D = D
4d8b0 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64  ;.  if( p->valid
4d8c0 54 5a 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74  TZ ){.    comput
4d8d0 65 4a 44 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65  eJD(p);.  }.  re
4d8e0 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
4d8f0 20 41 74 74 65 6d 70 74 20 74 6f 20 70 61 72 73   Attempt to pars
4d900 65 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  e the given stri
4d910 6e 67 20 69 6e 74 6f 20 61 20 4a 75 6c 69 61 6e  ng into a Julian
4d920 20 44 61 79 20 4e 75 6d 62 65 72 2e 20 20 52 65   Day Number.  Re
4d930 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  turn.** the numb
4d940 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a  er of errors..**
4d950 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
4d960 67 20 61 72 65 20 61 63 63 65 70 74 61 62 6c 65  g are acceptable
4d970 20 66 6f 72 6d 73 20 66 6f 72 20 74 68 65 20 69   forms for the i
4d980 6e 70 75 74 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a  nput string:.**.
4d990 2a 2a 20 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d  **      YYYY-MM-
4d9a0 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20  DD HH:MM:SS.FFF 
4d9b0 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a 2a 20 20 20 20   +/-HH:MM.**    
4d9c0 20 20 44 44 44 44 2e 44 44 20 0a 2a 2a 20 20 20    DDDD.DD .**   
4d9d0 20 20 20 6e 6f 77 0a 2a 2a 0a 2a 2a 20 49 6e 20     now.**.** In 
4d9e0 74 68 65 20 66 69 72 73 74 20 66 6f 72 6d 2c 20  the first form, 
4d9f0 74 68 65 20 2b 2f 2d 48 48 3a 4d 4d 20 69 73 20  the +/-HH:MM is 
4da00 61 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e  always optional.
4da10 20 20 54 68 65 20 66 72 61 63 74 69 6f 6e 61 6c    The fractional
4da20 0a 2a 2a 20 73 65 63 6f 6e 64 73 20 65 78 74 65  .** seconds exte
4da30 6e 73 69 6f 6e 20 28 74 68 65 20 22 2e 46 46 46  nsion (the ".FFF
4da40 22 29 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 20  ") is optional. 
4da50 20 54 68 65 20 73 65 63 6f 6e 64 73 20 70 6f 72   The seconds por
4da60 74 69 6f 6e 0a 2a 2a 20 28 22 3a 53 53 2e 46 46  tion.** (":SS.FF
4da70 46 22 29 20 69 73 20 6f 70 74 69 6f 6e 2e 20 20  F") is option.  
4da80 54 68 65 20 79 65 61 72 20 61 6e 64 20 64 61 74  The year and dat
4da90 65 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64  e can be omitted
4daa0 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74   as long.** as t
4dab0 68 65 72 65 20 69 73 20 61 20 74 69 6d 65 20 73  here is a time s
4dac0 74 72 69 6e 67 2e 20 20 54 68 65 20 74 69 6d 65  tring.  The time
4dad0 20 73 74 72 69 6e 67 20 63 61 6e 20 62 65 20 6f   string can be o
4dae0 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a  mitted as long.*
4daf0 2a 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20  * as there is a 
4db00 79 65 61 72 20 61 6e 64 20 64 61 74 65 2e 0a 2a  year and date..*
4db10 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
4db20 73 65 44 61 74 65 4f 72 54 69 6d 65 28 0a 20 20  seDateOrTime(.  
4db30 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
4db40 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e  *context, .  con
4db50 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20  st char *zDate, 
4db60 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29  .  DateTime *p.)
4db70 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  {.  memset(p, 0,
4db80 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20   sizeof(*p));.  
4db90 69 66 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44  if( parseYyyyMmD
4dba0 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b  d(zDate,p)==0 ){
4dbb0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
4dbc0 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72 73 65   }else if( parse
4dbd0 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29  HhMmSs(zDate, p)
4dbe0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
4dbf0 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 0;.  }else if(
4dc00 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
4dc10 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29  zDate,"now")==0)
4dc20 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a  {.    double r;.
4dc30 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72      sqlite3OsCur
4dc40 72 65 6e 74 54 69 6d 65 28 28 73 71 6c 69 74 65  rentTime((sqlite
4dc50 33 5f 76 66 73 20 2a 29 73 71 6c 69 74 65 33 5f  3_vfs *)sqlite3_
4dc60 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
4dc70 74 29 2c 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e  t), &r);.    p->
4dc80 72 4a 44 20 3d 20 72 3b 0a 20 20 20 20 70 2d 3e  rJD = r;.    p->
4dc90 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 20  validJD = 1;.   
4dca0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
4dcb0 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 49 73  se if( sqlite3Is
4dcc0 4e 75 6d 62 65 72 28 7a 44 61 74 65 2c 20 30 2c  Number(zDate, 0,
4dcd0 20 53 51 4c 49 54 45 5f 55 54 46 38 29 20 29 7b   SQLITE_UTF8) ){
4dce0 0a 20 20 20 20 67 65 74 56 61 6c 75 65 28 7a 44  .    getValue(zD
4dcf0 61 74 65 2c 20 26 70 2d 3e 72 4a 44 29 3b 0a 20  ate, &p->rJD);. 
4dd00 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20     p->validJD = 
4dd10 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  1;.    return 0;
4dd20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
4dd30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  .}../*.** Comput
4dd40 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e 74  e the Year, Mont
4dd50 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d 20  h, and Day from 
4dd60 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e  the julian day n
4dd70 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  umber..*/.static
4dd80 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44   void computeYMD
4dd90 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20  (DateTime *p){. 
4dda0 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 2c   int Z, A, B, C,
4ddb0 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 28   D, E, X1;.  if(
4ddc0 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 72   p->validYMD ) r
4ddd0 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 2d  eturn;.  if( !p-
4dde0 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20  >validJD ){.    
4ddf0 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20  p->Y = 2000;.   
4de00 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 70   p->M = 1;.    p
4de10 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  ->D = 1;.  }else
4de20 7b 0a 20 20 20 20 5a 20 3d 20 70 2d 3e 72 4a 44  {.    Z = p->rJD
4de30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 41 20 3d 20   + 0.5;.    A = 
4de40 28 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29  (Z - 1867216.25)
4de50 2f 33 36 35 32 34 2e 32 35 3b 0a 20 20 20 20 41  /36524.25;.    A
4de60 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28   = Z + 1 + A - (
4de70 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20  A/4);.    B = A 
4de80 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20  + 1524;.    C = 
4de90 28 42 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e  (B - 122.1)/365.
4dea0 32 35 3b 0a 20 20 20 20 44 20 3d 20 33 36 35 2e  25;.    D = 365.
4deb0 32 35 2a 43 3b 0a 20 20 20 20 45 20 3d 20 28 42  25*C;.    E = (B
4dec0 2d 44 29 2f 33 30 2e 36 30 30 31 3b 0a 20 20 20  -D)/30.6001;.   
4ded0 20 58 31 20 3d 20 33 30 2e 36 30 30 31 2a 45 3b   X1 = 30.6001*E;
4dee0 0a 20 20 20 20 70 2d 3e 44 20 3d 20 42 20 2d 20  .    p->D = B - 
4def0 44 20 2d 20 58 31 3b 0a 20 20 20 20 70 2d 3e 4d  D - X1;.    p->M
4df00 20 3d 20 45 3c 31 34 20 3f 20 45 2d 31 20 3a 20   = E<14 ? E-1 : 
4df10 45 2d 31 33 3b 0a 20 20 20 20 70 2d 3e 59 20 3d  E-13;.    p->Y =
4df20 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 2d 20 34 37   p->M>2 ? C - 47
4df30 31 36 20 3a 20 43 20 2d 20 34 37 31 35 3b 0a 20  16 : C - 4715;. 
4df40 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44   }.  p->validYMD
4df50 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 1;.}../*.** C
4df60 6f 6d 70 75 74 65 20 74 68 65 20 48 6f 75 72 2c  ompute the Hour,
4df70 20 4d 69 6e 75 74 65 2c 20 61 6e 64 20 53 65 63   Minute, and Sec
4df80 6f 6e 64 73 20 66 72 6f 6d 20 74 68 65 20 6a 75  onds from the ju
4df90 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e  lian day number.
4dfa0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
4dfb0 63 6f 6d 70 75 74 65 48 4d 53 28 44 61 74 65 54  computeHMS(DateT
4dfc0 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 5a  ime *p){.  int Z
4dfd0 2c 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61  , s;.  if( p->va
4dfe0 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e 3b  lidHMS ) return;
4dff0 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b  .  computeJD(p);
4e000 0a 20 20 5a 20 3d 20 70 2d 3e 72 4a 44 20 2b 20  .  Z = p->rJD + 
4e010 30 2e 35 3b 0a 20 20 73 20 3d 20 28 70 2d 3e 72  0.5;.  s = (p->r
4e020 4a 44 20 2b 20 30 2e 35 20 2d 20 5a 29 2a 38 36  JD + 0.5 - Z)*86
4e030 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 3b 0a  400000.0 + 0.5;.
4e040 20 20 70 2d 3e 73 20 3d 20 30 2e 30 30 31 2a 73    p->s = 0.001*s
4e050 3b 0a 20 20 73 20 3d 20 70 2d 3e 73 3b 0a 20 20  ;.  s = p->s;.  
4e060 70 2d 3e 73 20 2d 3d 20 73 3b 0a 20 20 70 2d 3e  p->s -= s;.  p->
4e070 68 20 3d 20 73 2f 33 36 30 30 3b 0a 20 20 73 20  h = s/3600;.  s 
4e080 2d 3d 20 70 2d 3e 68 2a 33 36 30 30 3b 0a 20 20  -= p->h*3600;.  
4e090 70 2d 3e 6d 20 3d 20 73 2f 36 30 3b 0a 20 20 70  p->m = s/60;.  p
4e0a0 2d 3e 73 20 2b 3d 20 73 20 2d 20 70 2d 3e 6d 2a  ->s += s - p->m*
4e0b0 36 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d  60;.  p->validHM
4e0c0 53 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  S = 1;.}../*.** 
4e0d0 43 6f 6d 70 75 74 65 20 62 6f 74 68 20 59 4d 44  Compute both YMD
4e0e0 20 61 6e 64 20 48 4d 53 0a 2a 2f 0a 73 74 61 74   and HMS.*/.stat
4e0f0 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59  ic void computeY
4e100 4d 44 5f 48 4d 53 28 44 61 74 65 54 69 6d 65 20  MD_HMS(DateTime 
4e110 2a 70 29 7b 0a 20 20 63 6f 6d 70 75 74 65 59 4d  *p){.  computeYM
4e120 44 28 70 29 3b 0a 20 20 63 6f 6d 70 75 74 65 48  D(p);.  computeH
4e130 4d 53 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  MS(p);.}../*.** 
4e140 43 6c 65 61 72 20 74 68 65 20 59 4d 44 20 61 6e  Clear the YMD an
4e150 64 20 48 4d 53 20 61 6e 64 20 74 68 65 20 54 5a  d HMS and the TZ
4e160 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
4e170 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
4e180 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
4e190 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b  p->validYMD = 0;
4e1a0 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d  .  p->validHMS =
4e1b0 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a   0;.  p->validTZ
4e1c0 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 0;.}../*.** C
4e1d0 6f 6d 70 75 74 65 20 74 68 65 20 64 69 66 66 65  ompute the diffe
4e1e0 72 65 6e 63 65 20 28 69 6e 20 64 61 79 73 29 20  rence (in days) 
4e1f0 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d  between localtim
4e200 65 20 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e 61  e and UTC (a.k.a
4e210 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 68  . GMT).** for th
4e220 65 20 74 69 6d 65 20 76 61 6c 75 65 20 70 20 77  e time value p w
4e230 68 65 72 65 20 70 20 69 73 20 69 6e 20 55 54 43  here p is in UTC
4e240 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f 75 62  ..*/.static doub
4e250 6c 65 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73  le localtimeOffs
4e260 65 74 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  et(DateTime *p){
4e270 0a 20 20 44 61 74 65 54 69 6d 65 20 78 2c 20 79  .  DateTime x, y
4e280 3b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20  ;.  time_t t;.  
4e290 78 20 3d 20 2a 70 3b 0a 20 20 63 6f 6d 70 75 74  x = *p;.  comput
4e2a0 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20  eYMD_HMS(&x);.  
4e2b0 69 66 28 20 78 2e 59 3c 31 39 37 31 20 7c 7c 20  if( x.Y<1971 || 
4e2c0 78 2e 59 3e 3d 32 30 33 38 20 29 7b 0a 20 20 20  x.Y>=2038 ){.   
4e2d0 20 78 2e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20   x.Y = 2000;.   
4e2e0 20 78 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 78 2e   x.M = 1;.    x.
4e2f0 44 20 3d 20 31 3b 0a 20 20 20 20 78 2e 68 20 3d  D = 1;.    x.h =
4e300 20 30 3b 0a 20 20 20 20 78 2e 6d 20 3d 20 30 3b   0;.    x.m = 0;
4e310 0a 20 20 20 20 78 2e 73 20 3d 20 30 2e 30 3b 0a  .    x.s = 0.0;.
4e320 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 69    } else {.    i
4e330 6e 74 20 73 20 3d 20 78 2e 73 20 2b 20 30 2e 35  nt s = x.s + 0.5
4e340 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a 20  ;.    x.s = s;. 
4e350 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a 20   }.  x.tz = 0;. 
4e360 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a   x.validJD = 0;.
4e370 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b    computeJD(&x);
4e380 0a 20 20 74 20 3d 20 28 78 2e 72 4a 44 2d 32 34  .  t = (x.rJD-24
4e390 34 30 35 38 37 2e 35 29 2a 38 36 34 30 30 2e 30  40587.5)*86400.0
4e3a0 20 2b 20 30 2e 35 3b 0a 23 69 66 64 65 66 20 48   + 0.5;.#ifdef H
4e3b0 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a  AVE_LOCALTIME_R.
4e3c0 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
4e3d0 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f  m sLocal;.    lo
4e3e0 63 61 6c 74 69 6d 65 5f 72 28 26 74 2c 20 26 73  caltime_r(&t, &s
4e3f0 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 79 2e 59 20  Local);.    y.Y 
4e400 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72  = sLocal.tm_year
4e410 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d   + 1900;.    y.M
4e420 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e   = sLocal.tm_mon
4e430 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20   + 1;.    y.D = 
4e440 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a  sLocal.tm_mday;.
4e450 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c      y.h = sLocal
4e460 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e  .tm_hour;.    y.
4e470 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69  m = sLocal.tm_mi
4e480 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f  n;.    y.s = sLo
4e490 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a  cal.tm_sec;.  }.
4e4a0 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74  #else.  {.    st
4e4b0 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20  ruct tm *pTm;.  
4e4c0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
4e4d0 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  enter(sqlite3_mu
4e4e0 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45  tex_alloc(SQLITE
4e4f0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
4e500 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20  STER));.    pTm 
4e510 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29 3b  = localtime(&t);
4e520 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d 3e  .    y.Y = pTm->
4e530 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a  tm_year + 1900;.
4e540 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74      y.M = pTm->t
4e550 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79  m_mon + 1;.    y
4e560 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61  .D = pTm->tm_mda
4e570 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70 54 6d  y;.    y.h = pTm
4e580 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79  ->tm_hour;.    y
4e590 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e  .m = pTm->tm_min
4e5a0 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54 6d 2d  ;.    y.s = pTm-
4e5b0 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71 6c  >tm_sec;.    sql
4e5c0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
4e5d0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  (sqlite3_mutex_a
4e5e0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
4e5f0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
4e600 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
4e610 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a  y.validYMD = 1;.
4e620 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31    y.validHMS = 1
4e630 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20  ;.  y.validJD = 
4e640 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d  0;.  y.validTZ =
4e650 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28   0;.  computeJD(
4e660 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e  &y);.  return y.
4e670 72 4a 44 20 2d 20 78 2e 72 4a 44 3b 0a 7d 0a 0a  rJD - x.rJD;.}..
4e680 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20  /*.** Process a 
4e690 6d 6f 64 69 66 69 65 72 20 74 6f 20 61 20 64 61  modifier to a da
4e6a0 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 20 20  te-time stamp.  
4e6b0 54 68 65 20 6d 6f 64 69 66 69 65 72 73 20 61 72  The modifiers ar
4e6c0 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  e.** as follows:
4e6d0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64  .**.**     NNN d
4e6e0 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68  ays.**     NNN h
4e6f0 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20  ours.**     NNN 
4e700 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e  minutes.**     N
4e710 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a  NN.NNNN seconds.
4e720 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68  **     NNN month
4e730 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61  s.**     NNN yea
4e740 72 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20  rs.**     start 
4e750 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20  of month.**     
4e760 73 74 61 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a  start of year.**
4e770 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 77 65       start of we
4e780 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20  ek.**     start 
4e790 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 77 65  of day.**     we
4e7a0 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75  ekday N.**     u
4e7b0 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20  nixepoch.**     
4e7c0 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20  localtime.**    
4e7d0 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72   utc.**.** Retur
4e7e0 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61  n 0 on success a
4e7f0 6e 64 20 31 20 69 66 20 74 68 65 72 65 20 69 73  nd 1 if there is
4e800 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72   any kind of err
4e810 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
4e820 74 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28  t parseModifier(
4e830 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64  const char *zMod
4e840 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  , DateTime *p){.
4e850 20 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20    int rc = 1;.  
4e860 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20  int n;.  double 
4e870 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42  r;.  char *z, zB
4e880 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42  uf[30];.  z = zB
4e890 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e  uf;.  for(n=0; n
4e8a0 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31 20  <sizeof(zBuf)-1 
4e8b0 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29  && zMod[n]; n++)
4e8c0 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 74 6f 6c  {.    z[n] = tol
4e8d0 6f 77 65 72 28 7a 4d 6f 64 5b 6e 5d 29 3b 0a 20  ower(zMod[n]);. 
4e8e0 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20   }.  z[n] = 0;. 
4e8f0 20 73 77 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b   switch( z[0] ){
4e900 0a 20 20 20 20 63 61 73 65 20 27 6c 27 3a 20 7b  .    case 'l': {
4e910 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 6c 6f 63  .      /*    loc
4e920 61 6c 74 69 6d 65 0a 20 20 20 20 20 20 2a 2a 0a  altime.      **.
4e930 20 20 20 20 20 20 2a 2a 20 41 73 73 75 6d 69 6e        ** Assumin
4e940 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69  g the current ti
4e950 6d 65 20 76 61 6c 75 65 20 69 73 20 55 54 43 20  me value is UTC 
4e960 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 73 68  (a.k.a. GMT), sh
4e970 69 66 74 20 69 74 20 74 6f 0a 20 20 20 20 20 20  ift it to.      
4e980 2a 2a 20 73 68 6f 77 20 6c 6f 63 61 6c 20 74 69  ** show local ti
4e990 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  me..      */.   
4e9a0 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c     if( strcmp(z,
4e9b0 20 22 6c 6f 63 61 6c 74 69 6d 65 22 29 3d 3d 30   "localtime")==0
4e9c0 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   ){.        comp
4e9d0 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20  uteJD(p);.      
4e9e0 20 20 70 2d 3e 72 4a 44 20 2b 3d 20 6c 6f 63 61    p->rJD += loca
4e9f0 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a  ltimeOffset(p);.
4ea00 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44          clearYMD
4ea10 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20  _HMS_TZ(p);.    
4ea20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
4ea30 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
4ea40 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
4ea50 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a  'u': {.      /*.
4ea60 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69 78        **    unix
4ea70 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20  epoch.      **. 
4ea80 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74 68       ** Treat th
4ea90 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
4eaa0 6f 66 20 70 2d 3e 72 4a 44 20 61 73 20 74 68 65  of p->rJD as the
4eab0 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20   number of.     
4eac0 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e 63   ** seconds sinc
4ead0 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72 74  e 1970.  Convert
4eae0 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69 61   to a real julia
4eaf0 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20  n day number..  
4eb00 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
4eb10 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78   strcmp(z, "unix
4eb20 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70 2d  epoch")==0 && p-
4eb30 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20  >validJD ){.    
4eb40 20 20 20 20 70 2d 3e 72 4a 44 20 3d 20 70 2d 3e      p->rJD = p->
4eb50 72 4a 44 2f 38 36 34 30 30 2e 30 20 2b 20 32 34  rJD/86400.0 + 24
4eb60 34 30 35 38 37 2e 35 3b 0a 20 20 20 20 20 20 20  40587.5;.       
4eb70 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
4eb80 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
4eb90 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
4eba0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22   if( strcmp(z, "
4ebb0 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  utc")==0 ){.    
4ebc0 20 20 20 20 64 6f 75 62 6c 65 20 63 31 3b 0a 20      double c1;. 
4ebd0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
4ebe0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 31 20  (p);.        c1 
4ebf0 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65  = localtimeOffse
4ec00 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  t(p);.        p-
4ec10 3e 72 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 20 20  >rJD -= c1;.    
4ec20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53      clearYMD_HMS
4ec30 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20  _TZ(p);.        
4ec40 70 2d 3e 72 4a 44 20 2b 3d 20 63 31 20 2d 20 6c  p->rJD += c1 - l
4ec50 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70  ocaltimeOffset(p
4ec60 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
4ec70 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
4ec80 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
4ec90 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20    case 'w': {.  
4eca0 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20      /*.      ** 
4ecb0 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20     weekday N.   
4ecc0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d     **.      ** M
4ecd0 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20  ove the date to 
4ece0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e  the same time on
4ecf0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72   the next occurr
4ed00 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a  ence of.      **
4ed10 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65   weekday N where
4ed20 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d   0==Sunday, 1==M
4ed30 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f  onday, and so fo
4ed40 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20  rth.  If the.   
4ed50 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c     ** date is al
4ed60 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70  ready on the app
4ed70 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79  ropriate weekday
4ed80 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  , this is a no-o
4ed90 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  p..      */.    
4eda0 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c    if( strncmp(z,
4edb0 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d   "weekday ", 8)=
4edc0 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26  =0 && getValue(&
4edd0 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20  z[8],&r)>0.     
4ede0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
4edf0 6e 3d 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20  n=r)==r && n>=0 
4ee00 26 26 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20  && r<7 ){.      
4ee10 20 20 69 6e 74 20 5a 3b 0a 20 20 20 20 20 20 20    int Z;.       
4ee20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
4ee30 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76  p);.        p->v
4ee40 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20  alidTZ = 0;.    
4ee50 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
4ee60 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   0;.        comp
4ee70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20  uteJD(p);.      
4ee80 20 20 5a 20 3d 20 70 2d 3e 72 4a 44 20 2b 20 31    Z = p->rJD + 1
4ee90 2e 35 3b 0a 20 20 20 20 20 20 20 20 5a 20 25 3d  .5;.        Z %=
4eea0 20 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   7;.        if( 
4eeb0 5a 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20  Z>n ) Z -= 7;.  
4eec0 20 20 20 20 20 20 70 2d 3e 72 4a 44 20 2b 3d 20        p->rJD += 
4eed0 6e 20 2d 20 5a 3b 0a 20 20 20 20 20 20 20 20 63  n - Z;.        c
4eee0 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70  learYMD_HMS_TZ(p
4eef0 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
4ef00 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
4ef10 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
4ef20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20    case 's': {.  
4ef30 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20      /*.      ** 
4ef40 20 20 20 73 74 61 72 74 20 6f 66 20 54 54 54 54     start of TTTT
4ef50 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  T.      **.     
4ef60 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74   ** Move the dat
4ef70 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20 74  e backwards to t
4ef80 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
4ef90 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 79 2c  the current day,
4efa0 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f 6e  .      ** or mon
4efb0 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20 20  th or year..    
4efc0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73    */.      if( s
4efd0 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72 74  trncmp(z, "start
4efe0 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20 62   of ", 9)!=0 ) b
4eff0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b 3d  reak;.      z +=
4f000 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 74   9;.      comput
4f010 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 70  eYMD(p);.      p
4f020 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a  ->validHMS = 1;.
4f030 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d 3e        p->h = p->
4f040 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  m = 0;.      p->
4f050 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 70  s = 0.0;.      p
4f060 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20  ->validTZ = 0;. 
4f070 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20       p->validJD 
4f080 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73  = 0;.      if( s
4f090 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29  trcmp(z,"month")
4f0a0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
4f0b0 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ->D = 1;.       
4f0c0 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d   rc = 0;.      }
4f0d0 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28  else if( strcmp(
4f0e0 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a  z,"year")==0 ){.
4f0f0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59          computeY
4f100 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70  MD(p);.        p
4f110 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ->M = 1;.       
4f120 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20   p->D = 1;.     
4f130 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
4f140 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d   }else if( strcm
4f150 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b  p(z,"day")==0 ){
4f160 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
4f170 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
4f180 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
4f190 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63 61  case '+':.    ca
4f1a0 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73 65  se '-':.    case
4f1b0 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '0':.    case '
4f1c0 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32 27  1':.    case '2'
4f1d0 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a 0a  :.    case '3':.
4f1e0 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20 20      case '4':.  
4f1f0 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20 20    case '5':.    
4f200 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63 61  case '6':.    ca
4f210 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73 65  se '7':.    case
4f220 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '8':.    case '
4f230 39 27 3a 20 7b 0a 20 20 20 20 20 20 6e 20 3d 20  9': {.      n = 
4f240 67 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b  getValue(z, &r);
4f250 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
4f260 3e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28  >=1 );.      if(
4f270 20 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20   z[n]==':' ){.  
4f280 20 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66        /* A modif
4f290 69 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ier of the form 
4f2a0 28 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46  (+|-)HH:MM:SS.FF
4f2b0 46 20 61 64 64 73 20 28 6f 72 20 73 75 62 74 72  F adds (or subtr
4f2c0 61 63 74 73 29 20 74 68 65 0a 20 20 20 20 20 20  acts) the.      
4f2d0 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e    ** specified n
4f2e0 75 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c 20  umber of hours, 
4f2f0 6d 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64 73  minutes, seconds
4f300 2c 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c  , and fractional
4f310 20 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20 20   seconds.       
4f320 20 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65 2e   ** to the time.
4f330 20 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61 79    The ".FFF" may
4f340 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68   be omitted.  Th
4f350 65 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79 20  e ":SS.FFF" may 
4f360 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d  be.        ** om
4f370 69 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a  itted..        *
4f380 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  /.        const 
4f390 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20  char *z2 = z;.  
4f3a0 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 74        DateTime t
4f3b0 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 64  x;.        int d
4f3c0 61 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ay;.        if( 
4f3d0 21 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a  !isdigit(*(u8*)z
4f3e0 32 29 20 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20  2) ) z2++;.     
4f3f0 20 20 20 6d 65 6d 73 65 74 28 26 74 78 2c 20 30     memset(&tx, 0
4f400 2c 20 73 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20  , sizeof(tx));. 
4f410 20 20 20 20 20 20 20 69 66 28 20 70 61 72 73 65         if( parse
4f420 48 68 4d 6d 53 73 28 7a 32 2c 20 26 74 78 29 20  HhMmSs(z2, &tx) 
4f430 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
4f440 20 63 6f 6d 70 75 74 65 4a 44 28 26 74 78 29 3b   computeJD(&tx);
4f450 0a 20 20 20 20 20 20 20 20 74 78 2e 72 4a 44 20  .        tx.rJD 
4f460 2d 3d 20 30 2e 35 3b 0a 20 20 20 20 20 20 20 20  -= 0.5;.        
4f470 64 61 79 20 3d 20 28 69 6e 74 29 74 78 2e 72 4a  day = (int)tx.rJ
4f480 44 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 72 4a  D;.        tx.rJ
4f490 44 20 2d 3d 20 64 61 79 3b 0a 20 20 20 20 20 20  D -= day;.      
4f4a0 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27 20    if( z[0]=='-' 
4f4b0 29 20 74 78 2e 72 4a 44 20 3d 20 2d 74 78 2e 72  ) tx.rJD = -tx.r
4f4c0 4a 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70  JD;.        comp
4f4d0 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20  uteJD(p);.      
4f4e0 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54    clearYMD_HMS_T
4f4f0 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  Z(p);.        p-
4f500 3e 72 4a 44 20 2b 3d 20 74 78 2e 72 4a 44 3b 0a  >rJD += tx.rJD;.
4f510 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a          rc = 0;.
4f520 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
4f530 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20 2b       }.      z +
4f540 3d 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  = n;.      while
4f550 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a 29  ( isspace(*(u8*)
4f560 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 20  z) ) z++;.      
4f570 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  n = strlen(z);. 
4f580 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c 7c       if( n>10 ||
4f590 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20 20   n<3 ) break;.  
4f5a0 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d 3d      if( z[n-1]==
4f5b0 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d 20  's' ){ z[n-1] = 
4f5c0 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20 20  0; n--; }.      
4f5d0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
4f5e0 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
4f5f0 20 20 69 66 28 20 6e 3d 3d 33 20 26 26 20 73 74    if( n==3 && st
4f600 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30  rcmp(z,"day")==0
4f610 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72   ){.        p->r
4f620 4a 44 20 2b 3d 20 72 3b 0a 20 20 20 20 20 20 7d  JD += r;.      }
4f630 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 26  else if( n==4 &&
4f640 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72 22   strcmp(z,"hour"
4f650 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
4f660 70 2d 3e 72 4a 44 20 2b 3d 20 72 2f 32 34 2e 30  p->rJD += r/24.0
4f670 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
4f680 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70  ( n==6 && strcmp
4f690 28 7a 2c 22 6d 69 6e 75 74 65 22 29 3d 3d 30 20  (z,"minute")==0 
4f6a0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 4a  ){.        p->rJ
4f6b0 44 20 2b 3d 20 72 2f 28 32 34 2e 30 2a 36 30 2e  D += r/(24.0*60.
4f6c0 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  0);.      }else 
4f6d0 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63  if( n==6 && strc
4f6e0 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d  mp(z,"second")==
4f6f0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  0 ){.        p->
4f700 72 4a 44 20 2b 3d 20 72 2f 28 32 34 2e 30 2a 36  rJD += r/(24.0*6
4f710 30 2e 30 2a 36 30 2e 30 29 3b 0a 20 20 20 20 20  0.0*60.0);.     
4f720 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 35 20   }else if( n==5 
4f730 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e  && strcmp(z,"mon
4f740 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  th")==0 ){.     
4f750 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 20 20     int x, y;.   
4f760 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
4f770 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
4f780 70 2d 3e 4d 20 2b 3d 20 72 3b 0a 20 20 20 20 20  p->M += r;.     
4f790 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20     x = p->M>0 ? 
4f7a0 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 70  (p->M-1)/12 : (p
4f7b0 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20 20  ->M-12)/12;.    
4f7c0 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20      p->Y += x;. 
4f7d0 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 78         p->M -= x
4f7e0 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  *12;.        p->
4f7f0 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20  validJD = 0;.   
4f800 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70       computeJD(p
4f810 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20 72  );.        y = r
4f820 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 21  ;.        if( y!
4f830 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =r ){.          
4f840 70 2d 3e 72 4a 44 20 2b 3d 20 28 72 20 2d 20 79  p->rJD += (r - y
4f850 29 2a 33 30 2e 30 3b 0a 20 20 20 20 20 20 20 20  )*30.0;.        
4f860 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
4f870 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d 70  ( n==4 && strcmp
4f880 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b  (z,"year")==0 ){
4f890 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
4f8a0 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20  YMD_HMS(p);.    
4f8b0 20 20 20 20 70 2d 3e 59 20 2b 3d 20 72 3b 0a 20      p->Y += r;. 
4f8c0 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a         p->validJ
4f8d0 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63  D = 0;.        c
4f8e0 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20  omputeJD(p);.   
4f8f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
4f900 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20    rc = 1;.      
4f910 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44  }.      clearYMD
4f920 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20  _HMS_TZ(p);.    
4f930 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
4f940 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
4f950 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
4f960 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
4f970 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65  ;.}../*.** Proce
4f980 73 73 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e  ss time function
4f990 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 61 72 67   arguments.  arg
4f9a0 76 5b 30 5d 20 69 73 20 61 20 64 61 74 65 2d 74  v[0] is a date-t
4f9b0 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 72  ime stamp..** ar
4f9c0 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77  gv[1] and follow
4f9d0 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 69 65 72  ing are modifier
4f9e0 73 2e 20 20 50 61 72 73 65 20 74 68 65 6d 20 61  s.  Parse them a
4f9f0 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20  ll and write.** 
4fa00 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74 69  the resulting ti
4fa10 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 61 74 65  me into the Date
4fa20 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20 70  Time structure p
4fa30 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a 2a 20 6f  .  Return 0.** o
4fa40 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 20  n success and 1 
4fa50 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
4fa60 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 73 74 61 74   errors..*/.stat
4fa70 69 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20  ic int isDate(. 
4fa80 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
4fa90 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e   *context, .  in
4faa0 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74  t argc, .  sqlit
4fab0 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c  e3_value **argv,
4fac0 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a   .  DateTime *p.
4fad0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f  ){.  int i;.  co
4fae0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
4faf0 72 20 2a 7a 3b 0a 20 20 69 66 28 20 61 72 67 63  r *z;.  if( argc
4fb00 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==0 ) return 1;.
4fb10 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    z = sqlite3_va
4fb20 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
4fb30 29 3b 0a 20 20 69 66 28 20 21 7a 20 7c 7c 20 70  );.  if( !z || p
4fb40 61 72 73 65 44 61 74 65 4f 72 54 69 6d 65 28 63  arseDateOrTime(c
4fb50 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a  ontext, (char*)z
4fb60 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , p) ){.    retu
4fb70 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  rn 1;.  }.  for(
4fb80 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b  i=1; i<argc; i++
4fb90 29 7b 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20  ){.    if( (z = 
4fba0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
4fbb0 78 74 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20  xt(argv[i]))==0 
4fbc0 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72  || parseModifier
4fbd0 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b  ((char*)z, p) ){
4fbe0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
4fbf0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
4fc00 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
4fc10 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
4fc20 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
4fc30 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 64 61  t the various da
4fc40 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63  te and time func
4fc50 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69  tions.** of SQLi
4fc60 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20  te..*/../*.**   
4fc70 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45   julianday( TIME
4fc80 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
4fc90 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
4fca0 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  urn the julian d
4fcb0 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ay number of the
4fcc0 20 64 61 74 65 20 73 70 65 63 69 66 69 65 64 20   date specified 
4fcd0 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73  in the arguments
4fce0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
4fcf0 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20  juliandayFunc(. 
4fd00 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
4fd10 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
4fd20 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
4fd30 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
4fd40 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20  .  DateTime x;. 
4fd50 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74   if( isDate(cont
4fd60 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  ext, argc, argv,
4fd70 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63   &x)==0 ){.    c
4fd80 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20  omputeJD(&x);.  
4fd90 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
4fda0 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c  _double(context,
4fdb0 20 78 2e 72 4a 44 29 3b 0a 20 20 7d 0a 7d 0a 0a   x.rJD);.  }.}..
4fdc0 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d  /*.**    datetim
4fdd0 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d  e( TIMESTRING, M
4fde0 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a  OD, MOD, ...).**
4fdf0 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d  .** Return YYYY-
4fe00 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a  MM-DD HH:MM:SS.*
4fe10 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  /.static void da
4fe20 74 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71  tetimeFunc(.  sq
4fe30 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
4fe40 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
4fe50 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
4fe60 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
4fe70 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66  DateTime x;.  if
4fe80 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74  ( isDate(context
4fe90 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78  , argc, argv, &x
4fea0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72  )==0 ){.    char
4feb0 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20   zBuf[100];.    
4fec0 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26  computeYMD_HMS(&
4fed0 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  x);.    sqlite3_
4fee0 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
4fef0 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30  zBuf), zBuf, "%0
4ff00 34 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32  4d-%02d-%02d %02
4ff10 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20  d:%02d:%02d",.  
4ff20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff30 20 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44     x.Y, x.M, x.D
4ff40 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74  , x.h, x.m, (int
4ff50 29 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c  )(x.s));.    sql
4ff60 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
4ff70 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20  (context, zBuf, 
4ff80 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
4ff90 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  IENT);.  }.}../*
4ffa0 0a 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d  .**    time( TIM
4ffb0 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f  ESTRING, MOD, MO
4ffc0 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65  D, ...).**.** Re
4ffd0 74 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f  turn HH:MM:SS.*/
4ffe0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d  .static void tim
4fff0 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
50000 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
50010 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
50020 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
50030 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54  *argv.){.  DateT
50040 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44  ime x;.  if( isD
50050 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  ate(context, arg
50060 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20  c, argv, &x)==0 
50070 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  ){.    char zBuf
50080 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75  [100];.    compu
50090 74 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73  teHMS(&x);.    s
500a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
500b0 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42  sizeof(zBuf), zB
500c0 75 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25  uf, "%02d:%02d:%
500d0 30 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20  02d", x.h, x.m, 
500e0 28 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73  (int)x.s);.    s
500f0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
50100 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66  xt(context, zBuf
50110 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41  , -1, SQLITE_TRA
50120 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a  NSIENT);.  }.}..
50130 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54  /*.**    date( T
50140 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20  IMESTRING, MOD, 
50150 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20  MOD, ...).**.** 
50160 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44  Return YYYY-MM-D
50170 44 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  D.*/.static void
50180 20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c   dateFunc(.  sql
50190 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
501a0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
501b0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
501c0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
501d0 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28  ateTime x;.  if(
501e0 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c   isDate(context,
501f0 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29   argc, argv, &x)
50200 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ==0 ){.    char 
50210 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63  zBuf[100];.    c
50220 6f 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20  omputeYMD(&x);. 
50230 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
50240 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29  ntf(sizeof(zBuf)
50250 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30  , zBuf, "%04d-%0
50260 32 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78  2d-%02d", x.Y, x
50270 2e 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71  .M, x.D);.    sq
50280 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
50290 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
502a0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
502b0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  SIENT);.  }.}../
502c0 2a 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65  *.**    strftime
502d0 28 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54  ( FORMAT, TIMEST
502e0 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
502f0 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
50300 6e 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72  n a string descr
50310 69 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20  ibed by FORMAT. 
50320 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20   Conversions as 
50330 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
50340 20 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74   %d  day of mont
50350 68 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72  h.**   %f  ** fr
50360 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73  actional seconds
50370 20 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48    SS.SSS.**   %H
50380 20 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20    hour 00-24.** 
50390 20 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61    %j  day of yea
503a0 72 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25  r 000-366.**   %
503b0 4a 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79  J  ** Julian day
503c0 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20   number.**   %m 
503d0 20 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20   month 01-12.** 
503e0 20 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d    %M  minute 00-
503f0 35 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f  59.**   %s  seco
50400 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30  nds since 1970-0
50410 31 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65  1-01.**   %S  se
50420 63 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20  conds 00-59.**  
50430 20 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b   %w  day of week
50440 20 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a   0-6  sunday==0.
50450 2a 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66  **   %W  week of
50460 20 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20   year 00-53.**  
50470 20 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39   %Y  year 0000-9
50480 39 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a  999.**   %%  %.*
50490 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74  /.static void st
504a0 72 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71  rftimeFunc(.  sq
504b0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
504c0 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
504d0 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
504e0 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
504f0 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36  DateTime x;.  u6
50500 34 20 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  4 n;.  int i, j;
50510 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 6f  .  char *z;.  co
50520 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d  nst char *zFmt =
50530 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
50540 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
50550 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61  (argv[0]);.  cha
50560 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69  r zBuf[100];.  i
50570 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73  f( zFmt==0 || is
50580 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  Date(context, ar
50590 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78  gc-1, argv+1, &x
505a0 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  ) ) return;.  fo
505b0 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46 6d 74  r(i=0, n=1; zFmt
505c0 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a  [i]; i++, n++){.
505d0 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d      if( zFmt[i]=
505e0 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 73 77  ='%' ){.      sw
505f0 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31 5d 20  itch( zFmt[i+1] 
50600 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  ){.        case 
50610 27 64 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  'd':.        cas
50620 65 20 27 48 27 3a 0a 20 20 20 20 20 20 20 20 63  e 'H':.        c
50630 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 20 20 20  ase 'm':.       
50640 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 20 20 20   case 'M':.     
50650 20 20 20 63 61 73 65 20 27 53 27 3a 0a 20 20 20     case 'S':.   
50660 20 20 20 20 20 63 61 73 65 20 27 57 27 3a 0a 20       case 'W':. 
50670 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20           n++;.  
50680 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c 6c 20          /* fall 
50690 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20  thru */.        
506a0 63 61 73 65 20 27 77 27 3a 0a 20 20 20 20 20 20  case 'w':.      
506b0 20 20 63 61 73 65 20 27 25 27 3a 0a 20 20 20 20    case '%':.    
506c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
506d0 20 20 20 20 20 63 61 73 65 20 27 66 27 3a 0a 20       case 'f':. 
506e0 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b           n += 8;
506f0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
50700 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
50710 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20  j':.          n 
50720 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20  += 3;.          
50730 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
50740 61 73 65 20 27 59 27 3a 0a 20 20 20 20 20 20 20  ase 'Y':.       
50750 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20     n += 8;.     
50760 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
50770 20 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20      case 's':.  
50780 20 20 20 20 20 20 63 61 73 65 20 27 4a 27 3a 0a        case 'J':.
50790 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 35            n += 5
507a0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  0;.          bre
507b0 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61  ak;.        defa
507c0 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 20 20 72  ult:.          r
507d0 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 52 4f 52  eturn;  /* ERROR
507e0 2e 20 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c  .  return a NULL
507f0 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   */.      }.    
50800 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d    i++;.    }.  }
50810 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f 66 28  .  if( n<sizeof(
50820 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 7a 20 3d  zBuf) ){.    z =
50830 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 65 20 69   zBuf;.  }else i
50840 66 28 20 6e 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  f( n>SQLITE_MAX_
50850 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71  LENGTH ){.    sq
50860 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
50870 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
50880 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
50890 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d    }else{.    z =
508a0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
508b0 20 6e 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d   n );.    if( z=
508c0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d  =0 ) return;.  }
508d0 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29  .  computeJD(&x)
508e0 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48  ;.  computeYMD_H
508f0 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d  MS(&x);.  for(i=
50900 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b  j=0; zFmt[i]; i+
50910 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74  +){.    if( zFmt
50920 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20 20  [i]!='%' ){.    
50930 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b    z[j++] = zFmt[
50940 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  i];.    }else{. 
50950 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
50960 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20  switch( zFmt[i] 
50970 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  ){.        case 
50980 27 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e  'd':  sqlite3_sn
50990 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c  printf(3, &z[j],
509a0 22 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d  "%02d",x.D); j+=
509b0 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  2; break;.      
509c0 20 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20 20    case 'f': {.  
509d0 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20 73          double s
509e0 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20 20   = x.s;.        
509f0 20 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20 29    if( s>59.999 )
50a00 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20   s = 59.999;.   
50a10 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
50a20 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d  nprintf(7, &z[j]
50a30 2c 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20  ,"%06.3f", s);. 
50a40 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 74           j += st
50a50 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20  rlen(&z[j]);.   
50a60 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
50a70 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
50a80 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c 69 74  case 'H':  sqlit
50a90 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26  e3_snprintf(3, &
50aa0 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 68 29  z[j],"%02d",x.h)
50ab0 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20  ; j+=2; break;. 
50ac0 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 3a         case 'W':
50ad0 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 2a 2f   /* Fall thru */
50ae0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6a  .        case 'j
50af0 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ': {.          i
50b00 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20 20 20  nt nDay;        
50b10 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
50b20 66 20 64 61 79 73 20 73 69 6e 63 65 20 31 73 74  f days since 1st
50b30 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a 2f 0a   day of year */.
50b40 20 20 20 20 20 20 20 20 20 20 44 61 74 65 54 69            DateTi
50b50 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20 20 20  me y = x;.      
50b60 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20      y.validJD = 
50b70 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 4d  0;.          y.M
50b80 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
50b90 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  y.D = 1;.       
50ba0 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29     computeJD(&y)
50bb0 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 61 79  ;.          nDay
50bc0 20 3d 20 78 2e 72 4a 44 20 2d 20 79 2e 72 4a 44   = x.rJD - y.rJD
50bd0 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20 20 20 20   + 0.5;.        
50be0 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27    if( zFmt[i]=='
50bf0 57 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  W' ){.          
50c00 20 20 69 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30    int wd;   /* 0
50c10 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54 75 65 73 64  =Monday, 1=Tuesd
50c20 61 79 2c 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79  ay, ... 6=Sunday
50c30 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
50c40 77 64 20 3d 20 28 28 69 6e 74 29 28 78 2e 72 4a  wd = ((int)(x.rJ
50c50 44 2b 30 2e 35 29 29 20 25 20 37 3b 0a 20 20 20  D+0.5)) % 7;.   
50c60 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
50c70 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b  _snprintf(3, &z[
50c80 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44 61 79 2b  j],"%02d",(nDay+
50c90 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20 20 20 20  7-wd)/7);.      
50ca0 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b 0a 20 20        j += 2;.  
50cb0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
50cc0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
50cd0 65 33 5f 73 6e 70 72 69 6e 74 66 28 34 2c 20 26  e3_snprintf(4, &
50ce0 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e 44 61 79  z[j],"%03d",nDay
50cf0 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  +1);.           
50d00 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20   j += 3;.       
50d10 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
50d20 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
50d30 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27          case 'J'
50d40 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  : {.          sq
50d50 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 32  lite3_snprintf(2
50d60 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 36 67 22  0, &z[j],"%.16g"
50d70 2c 78 2e 72 4a 44 29 3b 0a 20 20 20 20 20 20 20  ,x.rJD);.       
50d80 20 20 20 6a 2b 3d 73 74 72 6c 65 6e 28 26 7a 5b     j+=strlen(&z[
50d90 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  j]);.          b
50da0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
50db0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27          case 'm'
50dc0 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
50dd0 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30  ntf(3, &z[j],"%0
50de0 32 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20  2d",x.M); j+=2; 
50df0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
50e00 61 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74 65  ase 'M':  sqlite
50e10 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a  3_snprintf(3, &z
50e20 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b  [j],"%02d",x.m);
50e30 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20   j+=2; break;.  
50e40 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20        case 's': 
50e50 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
50e60 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c  te3_snprintf(30,
50e70 26 7a 5b 6a 5d 2c 22 25 64 22 2c 0a 20 20 20 20  &z[j],"%d",.    
50e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e90 20 20 20 20 20 20 20 28 69 6e 74 29 28 28 78 2e         (int)((x.
50ea0 72 4a 44 2d 32 34 34 30 35 38 37 2e 35 29 2a 38  rJD-2440587.5)*8
50eb0 36 34 30 30 2e 30 20 2b 20 30 2e 35 29 29 3b 0a  6400.0 + 0.5));.
50ec0 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73            j += s
50ed0 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b 0a 20 20  trlen(&z[j]);.  
50ee0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
50ef0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
50f00 20 63 61 73 65 20 27 53 27 3a 20 20 73 71 6c 69   case 'S':  sqli
50f10 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 26  te3_snprintf(3,&
50f20 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 69 6e 74  z[j],"%02d",(int
50f30 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20 62 72 65  )x.s); j+=2; bre
50f40 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
50f50 20 27 77 27 3a 20 20 7a 5b 6a 2b 2b 5d 20 3d 20   'w':  z[j++] = 
50f60 28 28 28 69 6e 74 29 28 78 2e 72 4a 44 2b 31 2e  (((int)(x.rJD+1.
50f70 35 29 29 20 25 20 37 29 20 2b 20 27 30 27 3b 20  5)) % 7) + '0'; 
50f80 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
50f90 61 73 65 20 27 59 27 3a 20 20 73 71 6c 69 74 65  ase 'Y':  sqlite
50fa0 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b  3_snprintf(5,&z[
50fb0 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b 20  j],"%04d",x.Y); 
50fc0 6a 2b 3d 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29  j+=strlen(&z[j])
50fd0 3b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20  ;break;.        
50fe0 63 61 73 65 20 27 25 27 3a 20 20 7a 5b 6a 2b 2b  case '%':  z[j++
50ff0 5d 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a  ] = '%'; break;.
51000 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
51010 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20  }.  z[j] = 0;.  
51020 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
51030 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20  ext(context, z, 
51040 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
51050 49 45 4e 54 29 3b 0a 20 20 69 66 28 20 7a 21 3d  IENT);.  if( z!=
51060 7a 42 75 66 20 29 7b 0a 20 20 20 20 73 71 6c 69  zBuf ){.    sqli
51070 74 65 33 5f 66 72 65 65 28 7a 29 3b 0a 20 20 7d  te3_free(z);.  }
51080 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e  .}../*.** curren
51090 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a 2a 2a 20 54  t_time().**.** T
510a0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
510b0 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61  urns the same va
510c0 6c 75 65 20 61 73 20 74 69 6d 65 28 27 6e 6f 77  lue as time('now
510d0 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ')..*/.static vo
510e0 69 64 20 63 74 69 6d 65 46 75 6e 63 28 0a 20 20  id ctimeFunc(.  
510f0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
51100 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
51110 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
51120 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
51130 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
51140 2a 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56  *pVal = sqlite3V
51150 61 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20 69 66  alueNew(0);.  if
51160 28 20 70 56 61 6c 20 29 7b 0a 20 20 20 20 73 71  ( pVal ){.    sq
51170 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
51180 28 70 56 61 6c 2c 20 2d 31 2c 20 22 6e 6f 77 22  (pVal, -1, "now"
51190 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
511a0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
511b0 20 20 20 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74     timeFunc(cont
511c0 65 78 74 2c 20 31 2c 20 26 70 56 61 6c 29 3b 0a  ext, 1, &pVal);.
511d0 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
511e0 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a  Free(pVal);.  }.
511f0 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74  }../*.** current
51200 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68  _date().**.** Th
51210 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
51220 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c  rns the same val
51230 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27  ue as date('now'
51240 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
51250 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73  d cdateFunc(.  s
51260 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
51270 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
51280 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
51290 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
512a0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
512b0 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61  pVal = sqlite3Va
512c0 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20 69 66 28  lueNew(0);.  if(
512d0 20 70 56 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c   pVal ){.    sql
512e0 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
512f0 70 56 61 6c 2c 20 2d 31 2c 20 22 6e 6f 77 22 2c  pVal, -1, "now",
51300 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
51310 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
51320 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65    dateFunc(conte
51330 78 74 2c 20 31 2c 20 26 70 56 61 6c 29 3b 0a 20  xt, 1, &pVal);. 
51340 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46     sqlite3ValueF
51350 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d  ree(pVal);.  }.}
51360 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f  ../*.** current_
51370 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a  timestamp().**.*
51380 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
51390 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65  returns the same
513a0 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 74 69   value as dateti
513b0 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74  me('now')..*/.st
513c0 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73  atic void ctimes
513d0 74 61 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69  tampFunc(.  sqli
513e0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
513f0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
51400 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
51410 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71  e **argv.){.  sq
51420 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
51430 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  l = sqlite3Value
51440 4e 65 77 28 30 29 3b 0a 20 20 69 66 28 20 70 56  New(0);.  if( pV
51450 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  al ){.    sqlite
51460 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61  3ValueSetStr(pVa
51470 6c 2c 20 2d 31 2c 20 22 6e 6f 77 22 2c 20 53 51  l, -1, "now", SQ
51480 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
51490 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 64  E_STATIC);.    d
514a0 61 74 65 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74  atetimeFunc(cont
514b0 65 78 74 2c 20 31 2c 20 26 70 56 61 6c 29 3b 0a  ext, 1, &pVal);.
514c0 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
514d0 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a  Free(pVal);.  }.
514e0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
514f0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
51500 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 29  _DATETIME_FUNCS)
51510 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
51520 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45  TE_OMIT_DATETIME
51530 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 66 20  _FUNCS./*.** If 
51540 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63  the library is c
51550 6f 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20  ompiled to omit 
51560 74 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64  the full-scale d
51570 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20  ate and time.** 
51580 68 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 65 74  handling (to get
51590 20 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72   a smaller binar
515a0 79 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  y), the followin
515b0 67 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f  g minimal versio
515c0 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63  n.** of the func
515d0 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f 74 69  tions current_ti
515e0 6d 65 28 29 2c 20 63 75 72 72 65 6e 74 5f 64 61  me(), current_da
515f0 74 65 28 29 20 61 6e 64 20 63 75 72 72 65 6e 74  te() and current
51600 5f 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 20  _timestamp().** 
51610 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 73  are included ins
51620 74 65 61 64 2e 20 54 68 69 73 20 69 73 20 74 6f  tead. This is to
51630 20 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d 6e 20   support column 
51640 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 68 61  declarations tha
51650 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 44 45  t.** include "DE
51660 46 41 55 4c 54 20 43 55 52 52 45 4e 54 5f 54 49  FAULT CURRENT_TI
51670 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54  ME" etc..**.** T
51680 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  his function use
51690 73 20 74 68 65 20 43 2d 6c 69 62 72 61 72 79 20  s the C-library 
516a0 66 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 28 29  functions time()
516b0 2c 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 61 6e  , gmtime().** an
516c0 64 20 73 74 72 66 74 69 6d 65 28 29 2e 20 54 68  d strftime(). Th
516d0 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  e format string 
516e0 74 6f 20 70 61 73 73 20 74 6f 20 73 74 72 66 74  to pass to strft
516f0 69 6d 65 28 29 20 69 73 20 73 75 70 70 6c 69 65  ime() is supplie
51700 64 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 65 72  d.** as the user
51710 2d 64 61 74 61 20 66 6f 72 20 74 68 65 20 66 75  -data for the fu
51720 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
51730 63 20 76 6f 69 64 20 63 75 72 72 65 6e 74 54 69  c void currentTi
51740 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  meFunc(.  sqlite
51750 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
51760 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
51770 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
51780 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 6d 65  **argv.){.  time
51790 5f 74 20 74 3b 0a 20 20 63 68 61 72 20 2a 7a 46  _t t;.  char *zF
517a0 6f 72 6d 61 74 20 3d 20 28 63 68 61 72 20 2a 29  ormat = (char *)
517b0 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
517c0 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68  a(context);.  ch
517d0 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20  ar zBuf[20];..  
517e0 74 69 6d 65 28 26 74 29 3b 0a 23 69 66 64 65 66  time(&t);.#ifdef
517f0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 7b   SQLITE_TEST.  {
51800 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20  .    extern int 
51810 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
51820 74 69 6d 65 3b 20 20 2f 2a 20 53 65 65 20 6f 73  time;  /* See os
51830 5f 58 58 58 2e 63 20 2a 2f 0a 20 20 20 20 69 66  _XXX.c */.    if
51840 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  ( sqlite3_curren
51850 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 20 20  t_time ){.      
51860 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72  t = sqlite3_curr
51870 65 6e 74 5f 74 69 6d 65 3b 0a 20 20 20 20 7d 0a  ent_time;.    }.
51880 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    }.#endif..#ifd
51890 65 66 20 48 41 56 45 5f 47 4d 54 49 4d 45 5f 52  ef HAVE_GMTIME_R
518a0 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20  .  {.    struct 
518b0 74 6d 20 73 4e 6f 77 3b 0a 20 20 20 20 67 6d 74  tm sNow;.    gmt
518c0 69 6d 65 5f 72 28 26 74 2c 20 26 73 4e 6f 77 29  ime_r(&t, &sNow)
518d0 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a  ;.    strftime(z
518e0 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74  Buf, 20, zFormat
518f0 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65  , &sNow);.  }.#e
51900 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  lse.  {.    stru
51910 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20  ct tm *pTm;.    
51920 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
51930 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ter(sqlite3_mute
51940 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  x_alloc(SQLITE_M
51950 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
51960 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20  ER));.    pTm = 
51970 67 6d 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20  gmtime(&t);.    
51980 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32  strftime(zBuf, 2
51990 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 6d 29  0, zFormat, pTm)
519a0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
519b0 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
519c0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51  3_mutex_alloc(SQ
519d0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
519e0 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a  C_MASTER));.  }.
519f0 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
51a00 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
51a10 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c  ntext, zBuf, -1,
51a20 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
51a30 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  T);.}.#endif../*
51a40 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
51a50 6e 20 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c  n registered all
51a60 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43 20   of the above C 
51a70 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c  functions as SQL
51a80 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  .** functions.  
51a90 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74  This should be t
51aa0 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20  he only routine 
51ab0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74  in this file wit
51ac0 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69  h.** external li
51ad0 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nkage..*/.SQLITE
51ae0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
51af0 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74  lite3RegisterDat
51b00 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 73  eTimeFunctions(s
51b10 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 23 69 66  qlite3 *db){.#if
51b20 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
51b30 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a  _DATETIME_FUNCS.
51b40 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
51b50 74 72 75 63 74 20 7b 0a 20 20 20 20 20 63 68 61  truct {.     cha
51b60 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 69  r *zName;.     i
51b70 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 20 76 6f  nt nArg;.     vo
51b80 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
51b90 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
51ba0 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
51bb0 29 3b 0a 20 20 7d 20 61 46 75 6e 63 73 5b 5d 20  );.  } aFuncs[] 
51bc0 3d 20 7b 0a 20 20 20 20 7b 20 22 6a 75 6c 69 61  = {.    { "julia
51bd0 6e 64 61 79 22 2c 20 2d 31 2c 20 6a 75 6c 69 61  nday", -1, julia
51be0 6e 64 61 79 46 75 6e 63 20 20 20 7d 2c 0a 20 20  ndayFunc   },.  
51bf0 20 20 7b 20 22 64 61 74 65 22 2c 20 20 20 20 20    { "date",     
51c00 20 2d 31 2c 20 64 61 74 65 46 75 6e 63 20 20 20   -1, dateFunc   
51c10 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 74       },.    { "t
51c20 69 6d 65 22 2c 20 20 20 20 20 20 2d 31 2c 20 74  ime",      -1, t
51c30 69 6d 65 46 75 6e 63 20 20 20 20 20 20 20 20 7d  imeFunc        }
51c40 2c 0a 20 20 20 20 7b 20 22 64 61 74 65 74 69 6d  ,.    { "datetim
51c50 65 22 2c 20 20 2d 31 2c 20 64 61 74 65 74 69 6d  e",  -1, datetim
51c60 65 46 75 6e 63 20 20 20 20 7d 2c 0a 20 20 20 20  eFunc    },.    
51c70 7b 20 22 73 74 72 66 74 69 6d 65 22 2c 20 20 2d  { "strftime",  -
51c80 31 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20  1, strftimeFunc 
51c90 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 63 75 72     },.    { "cur
51ca0 72 65 6e 74 5f 74 69 6d 65 22 2c 20 20 20 20 20  rent_time",     
51cb0 20 20 30 2c 20 63 74 69 6d 65 46 75 6e 63 20 20    0, ctimeFunc  
51cc0 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 63 75      },.    { "cu
51cd0 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 22  rrent_timestamp"
51ce0 2c 20 20 30 2c 20 63 74 69 6d 65 73 74 61 6d 70  ,  0, ctimestamp
51cf0 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 63  Func },.    { "c
51d00 75 72 72 65 6e 74 5f 64 61 74 65 22 2c 20 20 20  urrent_date",   
51d10 20 20 20 20 30 2c 20 63 64 61 74 65 46 75 6e 63      0, cdateFunc
51d20 20 20 20 20 20 20 7d 2c 0a 20 20 7d 3b 0a 20 20        },.  };.  
51d30 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 28 69 3d  int i;..  for(i=
51d40 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 46 75 6e  0; i<sizeof(aFun
51d50 63 73 29 2f 73 69 7a 65 6f 66 28 61 46 75 6e 63  cs)/sizeof(aFunc
51d60 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  s[0]); i++){.   
51d70 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
51d80 6e 63 28 64 62 2c 20 61 46 75 6e 63 73 5b 69 5d  nc(db, aFuncs[i]
51d90 2e 7a 4e 61 6d 65 2c 20 61 46 75 6e 63 73 5b 69  .zName, aFuncs[i
51da0 5d 2e 6e 41 72 67 2c 0a 20 20 20 20 20 20 20 20  ].nArg,.        
51db0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 28 76 6f  SQLITE_UTF8, (vo
51dc0 69 64 20 2a 29 28 64 62 2d 3e 70 56 66 73 29 2c  id *)(db->pVfs),
51dd0 20 61 46 75 6e 63 73 5b 69 5d 2e 78 46 75 6e 63   aFuncs[i].xFunc
51de0 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6c  , 0, 0);.  }.#el
51df0 73 65 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  se.  static cons
51e00 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20  t struct {.     
51e10 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20  char *zName;.   
51e20 20 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 3b    char *zFormat;
51e30 0a 20 20 7d 20 61 46 75 6e 63 73 5b 5d 20 3d 20  .  } aFuncs[] = 
51e40 7b 0a 20 20 20 20 7b 20 22 63 75 72 72 65 6e 74  {.    { "current
51e50 5f 74 69 6d 65 22 2c 20 22 25 48 3a 25 4d 3a 25  _time", "%H:%M:%
51e60 53 22 20 7d 2c 0a 20 20 20 20 7b 20 22 63 75 72  S" },.    { "cur
51e70 72 65 6e 74 5f 64 61 74 65 22 2c 20 22 25 59 2d  rent_date", "%Y-
51e80 25 6d 2d 25 64 22 20 7d 2c 0a 20 20 20 20 7b 20  %m-%d" },.    { 
51e90 22 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61  "current_timesta
51ea0 6d 70 22 2c 20 22 25 59 2d 25 6d 2d 25 64 20 25  mp", "%Y-%m-%d %
51eb0 48 3a 25 4d 3a 25 53 22 20 7d 0a 20 20 7d 3b 0a  H:%M:%S" }.  };.
51ec0 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 28    int i;..  for(
51ed0 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 46  i=0; i<sizeof(aF
51ee0 75 6e 63 73 29 2f 73 69 7a 65 6f 66 28 61 46 75  uncs)/sizeof(aFu
51ef0 6e 63 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20  ncs[0]); i++){. 
51f00 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65     sqlite3Create
51f10 46 75 6e 63 28 64 62 2c 20 61 46 75 6e 63 73 5b  Func(db, aFuncs[
51f20 69 5d 2e 7a 4e 61 6d 65 2c 20 30 2c 20 53 51 4c  i].zName, 0, SQL
51f30 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20 20 20  ITE_UTF8, .     
51f40 20 20 20 61 46 75 6e 63 73 5b 69 5d 2e 7a 46 6f     aFuncs[i].zFo
51f50 72 6d 61 74 2c 20 63 75 72 72 65 6e 74 54 69 6d  rmat, currentTim
51f60 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20  eFunc, 0, 0);.  
51f70 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a  }.#endif.}../***
51f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
51f90 6f 66 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a  of date.c ******
51fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
51fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
51fe0 6e 20 66 69 6c 65 20 6f 73 2e 63 20 2a 2a 2a 2a  n file os.c ****
51ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 2f 2a 0a  **********/. /*.
52020 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d 62 65 72  ** 2005 November
52030 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   29.**.** The au
52040 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
52050 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
52060 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
52070 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
52080 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
52090 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
520a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
520b0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
520c0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
520d0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
520e0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
520f0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
52100 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
52110 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
52120 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
52130 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
52140 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
52150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52190 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
521a0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
521b0 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 63 6f   OS interface co
521c0 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
521d0 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 72 63 68  n to all.** arch
521e0 69 74 65 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64  itectures..*/.#d
521f0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53  efine _SQLITE_OS
52200 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51  _C_ 1.#undef _SQ
52210 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a  LITE_OS_C_../*.*
52220 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
52230 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e  routines are con
52240 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72  venience wrapper
52250 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73  s around methods
52260 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74  .** of the sqlit
52270 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20  e3_file object. 
52280 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20   This is mostly 
52290 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 20 73  just syntactic s
522a0 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20  ugar. All.** of 
522b0 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f  this would be co
522c0 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74  mpletely automat
522d0 69 63 20 69 66 20 53 51 4c 69 74 65 20 77 65 72  ic if SQLite wer
522e0 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a  e coded using.**
522f0 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20   C++ instead of 
52300 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a  plain old C..*/.
52310 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
52320 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73  nt sqlite3OsClos
52330 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
52340 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pId){.  int rc =
52350 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
52360 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20  ( pId->pMethods 
52370 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d  ){.    rc = pId-
52380 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73  >pMethods->xClos
52390 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d  e(pId);.    pId-
523a0 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20  >pMethods = 0;. 
523b0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
523c0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
523d0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65   int sqlite3OsRe
523e0 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ad(sqlite3_file 
523f0 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c  *id, void *pBuf,
52400 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66   int amt, i64 of
52410 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  fset){.  return 
52420 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52  id->pMethods->xR
52430 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 61 6d  ead(id, pBuf, am
52440 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51  t, offset);.}.SQ
52450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
52460 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
52470 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
52480 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42  , const void *pB
52490 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34  uf, int amt, i64
524a0 20 6f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75   offset){.  retu
524b0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
524c0 3e 78 57 72 69 74 65 28 69 64 2c 20 70 42 75 66  >xWrite(id, pBuf
524d0 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a  , amt, offset);.
524e0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
524f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72   int sqlite3OsTr
52500 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66  uncate(sqlite3_f
52510 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 73 69 7a  ile *id, i64 siz
52520 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  e){.  return id-
52530 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e  >pMethods->xTrun
52540 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a  cate(id, size);.
52550 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
52560 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79   int sqlite3OsSy
52570 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  nc(sqlite3_file 
52580 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  *id, int flags){
52590 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
525a0 65 74 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64  ethods->xSync(id
525b0 2c 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49  , flags);.}.SQLI
525c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
525d0 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
525e0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
525f0 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a  d, i64 *pSize){.
52600 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
52610 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65  thods->xFileSize
52620 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53  (id, pSize);.}.S
52630 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
52640 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  t sqlite3OsLock(
52650 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
52660 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b  , int lockType){
52670 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
52680 65 74 68 6f 64 73 2d 3e 78 4c 6f 63 6b 28 69 64  ethods->xLock(id
52690 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53  , lockType);.}.S
526a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
526b0 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63  t sqlite3OsUnloc
526c0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
526d0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65  id, int lockType
526e0 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  ){.  return id->
526f0 70 4d 65 74 68 6f 64 73 2d 3e 78 55 6e 6c 6f 63  pMethods->xUnloc
52700 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b  k(id, lockType);
52710 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
52720 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
52730 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
52740 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
52750 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  d){.  return id-
52760 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68 65 63  >pMethods->xChec
52770 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 69 64  kReservedLock(id
52780 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
52790 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
527a0 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c  sFileControl(sql
527b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
527c0 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
527d0 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  g){.  return id-
527e0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65  >pMethods->xFile
527f0 43 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41  Control(id,op,pA
52800 72 67 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  rg);.}..#ifdef S
52810 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20  QLITE_TEST.  /* 
52820 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  The following tw
52830 6f 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  o variables are 
52840 75 73 65 64 20 74 6f 20 6f 76 65 72 72 69 64 65  used to override
52850 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75   the values retu
52860 72 6e 65 64 0a 20 20 2a 2a 20 62 79 20 74 68 65  rned.  ** by the
52870 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 61   xSectorSize() a
52880 6e 64 20 78 44 65 76 69 63 65 43 68 61 72 61 63  nd xDeviceCharac
52890 74 65 72 69 73 74 69 63 73 28 29 20 76 66 73 20  teristics() vfs 
528a0 6d 65 74 68 6f 64 73 20 66 6f 72 0a 20 20 2a 2a  methods for.  **
528b0 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65   testing purpose
528c0 73 2e 20 54 68 65 79 20 61 72 65 20 75 73 75 61  s. They are usua
528d0 6c 6c 79 20 73 65 74 20 62 79 20 61 20 74 65 73  lly set by a tes
528e0 74 20 63 6f 6d 6d 61 6e 64 20 69 6d 70 6c 65 6d  t command implem
528f0 65 6e 74 65 64 0a 20 20 2a 2a 20 69 6e 20 74 65  ented.  ** in te
52900 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 69 6e  st6.c..  */.  in
52910 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 73  t sqlite3_test_s
52920 65 63 74 6f 72 5f 73 69 7a 65 20 3d 20 30 3b 0a  ector_size = 0;.
52930 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65    int sqlite3_te
52940 73 74 5f 64 65 76 69 63 65 5f 63 68 61 72 61 63  st_device_charac
52950 74 65 72 69 73 74 69 63 73 20 3d 20 30 3b 0a 20  teristics = 0;. 
52960 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65   int sqlite3OsDe
52970 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
52980 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ics(sqlite3_file
52990 20 2a 69 64 29 7b 0a 20 20 20 20 69 6e 74 20 64   *id){.    int d
529a0 63 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  c = id->pMethods
529b0 2d 3e 78 44 65 76 69 63 65 43 68 61 72 61 63 74  ->xDeviceCharact
529c0 65 72 69 73 74 69 63 73 28 69 64 29 3b 0a 20 20  eristics(id);.  
529d0 20 20 72 65 74 75 72 6e 20 64 63 20 7c 20 73 71    return dc | sq
529e0 6c 69 74 65 33 5f 74 65 73 74 5f 64 65 76 69 63  lite3_test_devic
529f0 65 5f 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e_characteristic
52a00 73 3b 0a 20 20 7d 0a 20 20 69 6e 74 20 73 71 6c  s;.  }.  int sql
52a10 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65  ite3OsSectorSize
52a20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
52a30 64 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  d){.    if( sqli
52a40 74 65 33 5f 74 65 73 74 5f 73 65 63 74 6f 72 5f  te3_test_sector_
52a50 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  size==0 ){.     
52a60 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69   int (*xSectorSi
52a70 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ze)(sqlite3_file
52a80 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f 64  *) = id->pMethod
52a90 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b 0a  s->xSectorSize;.
52aa0 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 78 53        return (xS
52ab0 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63  ectorSize ? xSec
52ac0 74 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51  torSize(id) : SQ
52ad0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
52ae0 54 4f 52 5f 53 49 5a 45 29 3b 0a 20 20 20 20 7d  TOR_SIZE);.    }
52af0 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
52b00 74 65 33 5f 74 65 73 74 5f 73 65 63 74 6f 72 5f  te3_test_sector_
52b10 73 69 7a 65 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  size;.  }.#else.
52b20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53    int sqlite3OsS
52b30 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65  ectorSize(sqlite
52b40 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 20  3_file *id){.   
52b50 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69   int (*xSectorSi
52b60 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ze)(sqlite3_file
52b70 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f 64  *) = id->pMethod
52b80 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b 0a  s->xSectorSize;.
52b90 20 20 20 20 72 65 74 75 72 6e 20 28 78 53 65 63      return (xSec
52ba0 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 74 6f  torSize ? xSecto
52bb0 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51 4c 49  rSize(id) : SQLI
52bc0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
52bd0 52 5f 53 49 5a 45 29 3b 0a 20 20 7d 0a 20 20 69  R_SIZE);.  }.  i
52be0 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69  nt sqlite3OsDevi
52bf0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
52c00 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
52c10 69 64 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  id){.    return 
52c20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 44  id->pMethods->xD
52c30 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
52c40 74 69 63 73 28 69 64 29 3b 0a 20 20 7d 0a 23 65  tics(id);.  }.#e
52c50 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
52c60 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f  next group of ro
52c70 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65  utines are conve
52c80 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20  nience wrappers 
52c90 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46  around the.** VF
52ca0 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51  S methods..*/.SQ
52cb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
52cc0 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a   sqlite3OsOpen(.
52cd0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
52ce0 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  Vfs, .  const ch
52cf0 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71  ar *zPath, .  sq
52d00 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
52d10 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c  e, .  int flags,
52d20 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f   .  int *pFlagsO
52d30 75 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ut.){.  return p
52d40 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c  Vfs->xOpen(pVfs,
52d50 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66   zPath, pFile, f
52d60 6c 61 67 73 2c 20 70 46 6c 61 67 73 4f 75 74 29  lags, pFlagsOut)
52d70 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
52d80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
52d90 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76  Delete(sqlite3_v
52da0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20  fs *pVfs, const 
52db0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74  char *zPath, int
52dc0 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65 74   dirSync){.  ret
52dd0 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 74  urn pVfs->xDelet
52de0 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 64  e(pVfs, zPath, d
52df0 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54  irSync);.}.SQLIT
52e00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
52e10 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 73 71  lite3OsAccess(sq
52e20 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
52e30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
52e40 74 68 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  th, int flags){.
52e50 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
52e60 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 50 61  Access(pVfs, zPa
52e70 74 68 2c 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51  th, flags);.}.SQ
52e80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
52e90 20 73 71 6c 69 74 65 33 4f 73 47 65 74 54 65 6d   sqlite3OsGetTem
52ea0 70 4e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  pName(sqlite3_vf
52eb0 73 20 2a 70 56 66 73 2c 20 63 68 61 72 20 2a 7a  s *pVfs, char *z
52ec0 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 74 75 72  BufOut){.  retur
52ed0 6e 20 70 56 66 73 2d 3e 78 47 65 74 54 65 6d 70  n pVfs->xGetTemp
52ee0 4e 61 6d 65 28 70 56 66 73 2c 20 7a 42 75 66 4f  Name(pVfs, zBufO
52ef0 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ut);.}.SQLITE_PR
52f00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
52f10 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  3OsFullPathname(
52f20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
52f30 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  s, const char *z
52f40 50 61 74 68 2c 20 63 68 61 72 20 2a 7a 50 61 74  Path, char *zPat
52f50 68 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  hOut){.  return 
52f60 70 56 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e  pVfs->xFullPathn
52f70 61 6d 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c  ame(pVfs, zPath,
52f80 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 53 51   zPathOut);.}.SQ
52f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
52fa0 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70  d *sqlite3OsDlOp
52fb0 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  en(sqlite3_vfs *
52fc0 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72  pVfs, const char
52fd0 20 2a 7a 50 61 74 68 29 7b 0a 20 20 72 65 74 75   *zPath){.  retu
52fe0 72 6e 20 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e  rn pVfs->xDlOpen
52ff0 28 70 56 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d  (pVfs, zPath);.}
53000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53010 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c  void sqlite3OsDl
53020 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
53030 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79  s *pVfs, int nBy
53040 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  te, char *zBufOu
53050 74 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 45  t){.  pVfs->xDlE
53060 72 72 6f 72 28 70 56 66 73 2c 20 6e 42 79 74 65  rror(pVfs, nByte
53070 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51  , zBufOut);.}.SQ
53080 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
53090 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79  d *sqlite3OsDlSy
530a0 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  m(sqlite3_vfs *p
530b0 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64  Vfs, void *pHand
530c0 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  le, const char *
530d0 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 72 65 74 75  zSymbol){.  retu
530e0 72 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28  rn pVfs->xDlSym(
530f0 70 56 66 73 2c 20 70 48 61 6e 64 6c 65 2c 20 7a  pVfs, pHandle, z
53100 53 79 6d 62 6f 6c 29 3b 0a 7d 0a 53 51 4c 49 54  Symbol);.}.SQLIT
53110 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
53120 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28  qlite3OsDlClose(
53130 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
53140 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  s, void *pHandle
53150 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43 6c  ){.  pVfs->xDlCl
53160 6f 73 65 28 70 56 66 73 2c 20 70 48 61 6e 64 6c  ose(pVfs, pHandl
53170 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  e);.}.SQLITE_PRI
53180 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53190 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  OsRandomness(sql
531a0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
531b0 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20  int nByte, char 
531c0 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 74  *zBufOut){.  ret
531d0 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 6f  urn pVfs->xRando
531e0 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79 74  mness(pVfs, nByt
531f0 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53  e, zBufOut);.}.S
53200 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53210 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70  t sqlite3OsSleep
53220 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
53230 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 7b  fs, int nMicro){
53240 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e  .  return pVfs->
53250 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d 69  xSleep(pVfs, nMi
53260 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  cro);.}.SQLITE_P
53270 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
53280 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28  e3OsCurrentTime(
53290 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
532a0 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d 65  s, double *pTime
532b0 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  Out){.  return p
532c0 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 6d  Vfs->xCurrentTim
532d0 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 74  e(pVfs, pTimeOut
532e0 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
532f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53300 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20  OsOpenMalloc(.  
53310 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
53320 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  s, .  const char
53330 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c 69   *zFile, .  sqli
53340 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 6c  te3_file **ppFil
53350 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c  e, .  int flags,
53360 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67  .  int *pOutFlag
53370 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  s.){.  int rc = 
53380 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
53390 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
533a0 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 28  ile;.  pFile = (
533b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73  sqlite3_file *)s
533c0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 56  qlite3_malloc(pV
533d0 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20  fs->szOsFile);. 
533e0 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20   if( pFile ){.  
533f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
53400 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65  Open(pVfs, zFile
53410 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20  , pFile, flags, 
53420 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20  pOutFlags);.    
53430 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
53440 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
53450 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a  e3_free(pFile);.
53460 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
53470 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65   *ppFile = pFile
53480 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
53490 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
534a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
534b0 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
534c0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
534d0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  File){.  int rc 
534e0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
534f0 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  f( pFile ){.    
53500 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c  rc = sqlite3OsCl
53510 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ose(pFile);.    
53520 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69  sqlite3_free(pFi
53530 6c 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  le);.  }.  retur
53540 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
53550 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 72  he list of all r
53560 65 67 69 73 74 65 72 65 64 20 56 46 53 20 69 6d  egistered VFS im
53570 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 20  plementations.  
53580 54 68 69 73 20 6c 69 73 74 20 69 73 0a 2a 2a 20  This list is.** 
53590 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 74  initialized to t
535a0 68 65 20 73 69 6e 67 6c 65 20 56 46 53 20 72 65  he single VFS re
535b0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
535c0 33 4f 73 44 65 66 61 75 6c 74 56 66 73 28 29 0a  3OsDefaultVfs().
535d0 2a 2a 20 75 70 6f 6e 20 74 68 65 20 66 69 72 73  ** upon the firs
535e0 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  t call to sqlite
535f0 33 5f 76 66 73 5f 66 69 6e 64 28 29 2e 0a 2a 2f  3_vfs_find()..*/
53600 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
53610 76 66 73 20 2a 76 66 73 4c 69 73 74 20 3d 20 30  vfs *vfsList = 0
53620 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20  ;../*.** Locate 
53630 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20  a VFS by name.  
53640 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69  If no name is gi
53650 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75  ven, simply retu
53660 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  rn the.** first 
53670 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e  VFS on the list.
53680 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
53690 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
536a0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e  te3_vfs_find(con
536b0 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29 7b 0a  st char *zVfs){.
536c0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
536d0 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33  *mutex = sqlite3
536e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c  _mutex_alloc(SQL
536f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
53700 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c 69  _MASTER);.  sqli
53710 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 0a 20  te3_vfs *pVfs;. 
53720 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 49 6e   static int isIn
53730 69 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  it = 0;.  sqlite
53740 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
53750 74 65 78 29 3b 0a 20 20 69 66 28 20 21 69 73 49  tex);.  if( !isI
53760 6e 69 74 20 29 7b 0a 20 20 20 20 76 66 73 4c 69  nit ){.    vfsLi
53770 73 74 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65  st = sqlite3OsDe
53780 66 61 75 6c 74 56 66 73 28 29 3b 0a 20 20 20 20  faultVfs();.    
53790 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a  isInit = 1;.  }.
537a0 20 20 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73    for(pVfs = vfs
537b0 4c 69 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73  List; pVfs; pVfs
537c0 3d 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pVfs->pNext){. 
537d0 20 20 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29     if( zVfs==0 )
537e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20   break;.    if( 
537f0 73 74 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66  strcmp(zVfs, pVf
53800 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62  s->zName)==0 ) b
53810 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  reak;.  }.  sqli
53820 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
53830 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
53840 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pVfs;.}../*.** 
53850 55 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f  Unlink a VFS fro
53860 6d 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  m the linked lis
53870 74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  t.*/.static void
53880 20 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74   vfsUnlink(sqlit
53890 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20  e3_vfs *pVfs){. 
538a0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
538b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69  _mutex_held(sqli
538c0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
538d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
538e0 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a  TIC_MASTER)) );.
538f0 20 20 69 66 28 20 76 66 73 4c 69 73 74 3d 3d 70    if( vfsList==p
53900 56 66 73 20 29 7b 0a 20 20 20 20 76 66 73 4c 69  Vfs ){.    vfsLi
53910 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74  st = pVfs->pNext
53920 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
53930 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d 20  qlite3_vfs *p = 
53940 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68 69  vfsList;.    whi
53950 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26 20  le( p->pNext && 
53960 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20 29  p->pNext!=pVfs )
53970 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70  {.      p = p->p
53980 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
53990 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56  if( p->pNext==pV
539a0 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  fs ){.      p->p
539b0 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65  Next = pVfs->pNe
539c0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  xt;.    }.  }.}.
539d0 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
539e0 61 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73  a VFS with the s
539f0 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68 61  ystem.  It is ha
53a00 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73 74  rmless to regist
53a10 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56  er the same.** V
53a20 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  FS multiple time
53a30 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53 20  s.  The new VFS 
53a40 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 61  becomes the defa
53a50 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74 20  ult if makeDflt 
53a60 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53  is.** true..*/.S
53a70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
53a80 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
53a90 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  er(sqlite3_vfs *
53aa0 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66  pVfs, int makeDf
53ab0 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  lt){.  sqlite3_m
53ac0 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71  utex *mutex = sq
53ad0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
53ae0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
53af0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20  TATIC_MASTER);. 
53b00 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e   sqlite3_vfs_fin
53b10 64 28 30 29 3b 20 20 2f 2a 20 4d 61 6b 65 20 73  d(0);  /* Make s
53b20 75 72 65 20 77 65 20 61 72 65 20 69 6e 69 74 69  ure we are initi
53b30 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69  alized */.  sqli
53b40 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
53b50 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c  mutex);.  vfsUnl
53b60 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 69 66 28  ink(pVfs);.  if(
53b70 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 76 66 73   makeDflt || vfs
53b80 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  List==0 ){.    p
53b90 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73  Vfs->pNext = vfs
53ba0 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 4c 69 73  List;.    vfsLis
53bb0 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 65 6c 73  t = pVfs;.  }els
53bc0 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65  e{.    pVfs->pNe
53bd0 78 74 20 3d 20 76 66 73 4c 69 73 74 2d 3e 70 4e  xt = vfsList->pN
53be0 65 78 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 74  ext;.    vfsList
53bf0 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 3b 0a  ->pNext = pVfs;.
53c00 20 20 7d 0a 20 20 61 73 73 65 72 74 28 76 66 73    }.  assert(vfs
53c10 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
53c20 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
53c30 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
53c40 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
53c50 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20 56  * Unregister a V
53c60 46 53 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  FS so that it is
53c70 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 63 65 73   no longer acces
53c80 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sible..*/.SQLITE
53c90 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
53ca0 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28  _vfs_unregister(
53cb0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
53cc0 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  s){.  sqlite3_mu
53cd0 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c  tex *mutex = sql
53ce0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
53cf0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
53d00 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20  ATIC_MASTER);.  
53d10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
53d20 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66  ter(mutex);.  vf
53d30 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20  sUnlink(pVfs);. 
53d40 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 29   assert(vfsList)
53d50 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
53d60 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
53d70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
53d80 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  OK;.}../********
53d90 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
53da0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
53db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53dd0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
53de0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
53df0 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e mem1.c *******
53e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53e20 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
53e30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a  7 August 14.**.*
53e40 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
53e50 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
53e60 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
53e70 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
53e80 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
53e90 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
53ea0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
53eb0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
53ec0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
53ed0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
53ee0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
53ef0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
53f00 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
53f10 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
53f20 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
53f30 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
53f40 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
53f50 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
53f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
53fa0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
53fb0 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
53fc0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
53fd0 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  a memory.** allo
53fe0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
53ff0 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69   for use by SQLi
54000 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  te.  .**.** $Id:
54010 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 31 30 20 32   mem1.c,v 1.10 2
54020 30 30 37 2f 30 39 2f 30 32 20 31 37 3a 35 30 3a  007/09/02 17:50:
54030 33 35 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  35 drh Exp $.*/.
54040 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  ./*.** This vers
54050 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
54060 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74  y allocator is t
54070 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20  he default.  It 
54080 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20  is.** used when 
54090 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20  no other memory 
540a0 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65  allocator is spe
540b0 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d  cified using com
540c0 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63  pile-time.** mac
540d0 72 6f 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ros..*/.#if !def
540e0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44  ined(SQLITE_MEMD
540f0 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65  EBUG) && !define
54100 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45  d(SQLITE_OMIT_ME
54110 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54 49 4f 4e 29  MORY_ALLOCATION)
54120 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 77 69 6c 6c 20  ../*.** We will 
54130 65 76 65 6e 74 75 61 6c 6c 79 20 63 6f 6e 73 74  eventually const
54140 72 75 63 74 20 6d 75 6c 74 69 70 6c 65 20 6d 65  ruct multiple me
54150 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
54160 73 75 62 73 79 73 74 65 6d 73 0a 2a 2a 20 73 75  subsystems.** su
54170 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 69  itable for use i
54180 6e 20 76 61 72 69 6f 75 73 20 63 6f 6e 74 65 78  n various contex
54190 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  ts:.**.**    *  
541a0 4e 6f 72 6d 61 6c 20 6d 75 6c 74 69 2d 74 68 72  Normal multi-thr
541b0 65 61 64 65 64 20 62 75 69 6c 64 73 0a 2a 2a 20  eaded builds.** 
541c0 20 20 20 2a 20 20 4e 6f 72 6d 61 6c 20 73 69 6e     *  Normal sin
541d0 67 6c 65 2d 74 68 72 65 61 64 65 64 20 62 75 69  gle-threaded bui
541e0 6c 64 73 0a 2a 2a 20 20 20 20 2a 20 20 44 65 62  lds.**    *  Deb
541f0 75 67 67 69 6e 67 20 62 75 69 6c 64 73 0a 2a 2a  ugging builds.**
54200 0a 2a 2a 20 54 68 69 73 20 69 6e 69 74 69 61 6c  .** This initial
54210 20 76 65 72 73 69 6f 6e 20 69 73 20 73 75 69 74   version is suit
54220 61 62 6c 65 20 66 6f 72 20 75 73 65 20 69 6e 20  able for use in 
54230 6e 6f 72 6d 61 6c 20 6d 75 6c 74 69 2d 74 68 72  normal multi-thr
54240 65 61 64 65 64 0a 2a 2a 20 62 75 69 6c 64 73 2e  eaded.** builds.
54250 20 20 57 65 20 65 6e 76 69 73 69 6f 6e 20 74 68    We envision th
54260 61 74 20 61 6c 74 65 72 6e 61 74 69 76 65 20 76  at alternative v
54270 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20  ersions will be 
54280 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 73 65 70  stored in.** sep
54290 61 72 61 74 65 20 73 6f 75 72 63 65 20 66 69 6c  arate source fil
542a0 65 73 2e 20 20 23 69 66 64 65 66 73 20 77 69 6c  es.  #ifdefs wil
542b0 6c 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c  l be used to sel
542c0 65 63 74 20 74 68 65 20 63 6f 64 65 20 66 72 6f  ect the code fro
542d0 6d 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20  m.** one of the 
542e0 76 61 72 69 6f 75 73 20 6d 65 6d 4e 2e 63 20 73  various memN.c s
542f0 6f 75 72 63 65 20 66 69 6c 65 73 20 66 6f 72 20  ource files for 
54300 75 73 65 20 69 6e 20 61 6e 79 20 67 69 76 65 6e  use in any given
54310 20 62 75 69 6c 64 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a   build..*/../*.*
54320 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61  * All of the sta
54330 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73  tic variables us
54340 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c  ed by this modul
54350 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a  e are collected.
54360 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ** into a single
54370 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64   structure named
54380 20 22 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73   "mem".  This is
54390 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20   to keep the.** 
543a0 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73  static variables
543b0 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74   organized and t
543c0 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61  o reduce namespa
543d0 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20  ce pollution.** 
543e0 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65  when this module
543f0 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74   is combined wit
54400 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61  h other in the a
54410 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  malgamation..*/.
54420 73 74 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a  static struct {.
54430 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c    /*.  ** The al
54440 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64  arm callback and
54450 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20   its arguments. 
54460 20 54 68 65 20 6d 65 6d 2e 6d 75 74 65 78 20 6c   The mem.mutex l
54470 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65  ock will.  ** be
54480 20 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65 20   held while the 
54490 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e  callback is runn
544a0 69 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65 20  ing.  Recursive 
544b0 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20  calls into.  ** 
544c0 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79  the memory subsy
544d0 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64  stem are allowed
544e0 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c  , but no new cal
544f0 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20  lbacks will be. 
54500 20 2a 2a 20 69 73 73 75 65 64 2e 20 20 54 68 65   ** issued.  The
54510 20 61 6c 61 72 6d 42 75 73 79 20 76 61 72 69 61   alarmBusy varia
54520 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 70 72  ble is set to pr
54530 65 76 65 6e 74 20 72 65 63 75 72 73 69 76 65 0a  event recursive.
54540 20 20 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 2e 0a    ** callbacks..
54550 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69    */.  sqlite3_i
54560 6e 74 36 34 20 61 6c 61 72 6d 54 68 72 65 73 68  nt64 alarmThresh
54570 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c  old;.  void (*al
54580 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  armCallback)(voi
54590 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  d*, sqlite3_int6
545a0 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a  4,int);.  void *
545b0 61 6c 61 72 6d 41 72 67 3b 0a 20 20 69 6e 74 20  alarmArg;.  int 
545c0 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20  alarmBusy;.  .  
545d0 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f  /*.  ** Mutex to
545e0 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20   control access 
545f0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  to the memory al
54600 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
54610 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  em..  */.  sqlit
54620 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
54630 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 75  .  .  /*.  ** Cu
54640 72 72 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e  rrent allocation
54650 20 61 6e 64 20 68 69 67 68 2d 77 61 74 65 72 20   and high-water 
54660 6d 61 72 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  mark..  */.  sql
54670 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73  ite3_int64 nowUs
54680 65 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  ed;.  sqlite3_in
54690 74 36 34 20 6d 78 55 73 65 64 3b 0a 20 20 0a 20  t64 mxUsed;.  . 
546a0 0a 7d 20 6d 65 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 45  .} mem;../*.** E
546b0 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 20 6d  nter the mutex m
546c0 65 6d 2e 6d 75 74 65 78 2e 20 41 6c 6c 6f 63 61  em.mutex. Alloca
546d0 74 65 20 69 74 20 69 66 20 69 74 20 69 73 20 6e  te it if it is n
546e0 6f 74 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  ot already alloc
546f0 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ated..*/.static 
54700 76 6f 69 64 20 65 6e 74 65 72 4d 65 6d 28 76 6f  void enterMem(vo
54710 69 64 29 7b 0a 20 20 69 66 28 20 6d 65 6d 2e 6d  id){.  if( mem.m
54720 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d  utex==0 ){.    m
54730 65 6d 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  em.mutex = sqlit
54740 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
54750 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
54760 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73  IC_MEM);.  }.  s
54770 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
54780 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 7d  er(mem.mutex);.}
54790 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
547a0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  he amount of mem
547b0 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68  ory currently ch
547c0 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51  ecked out..*/.SQ
547d0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
547e0 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d  _int64 sqlite3_m
547f0 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29  emory_used(void)
54800 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
54810 34 20 6e 3b 0a 20 20 65 6e 74 65 72 4d 65 6d 28  4 n;.  enterMem(
54820 29 3b 0a 20 20 6e 20 3d 20 6d 65 6d 2e 6e 6f 77  );.  n = mem.now
54830 55 73 65 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Used;.  sqlite3_
54840 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e  mutex_leave(mem.
54850 6d 75 74 65 78 29 3b 20 20 0a 20 20 72 65 74 75  mutex);  .  retu
54860 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn n;.}../*.** R
54870 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75  eturn the maximu
54880 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  m amount of memo
54890 72 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72  ry that has ever
548a0 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64   been.** checked
548b0 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65   out since eithe
548c0 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  r the beginning 
548d0 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a  of this process.
548e0 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20  ** or since the 
548f0 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 65  most recent rese
54900 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
54910 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
54920 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
54930 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65  ghwater(int rese
54940 74 46 6c 61 67 29 7b 0a 20 20 73 71 6c 69 74 65  tFlag){.  sqlite
54950 33 5f 69 6e 74 36 34 20 6e 3b 0a 20 20 65 6e 74  3_int64 n;.  ent
54960 65 72 4d 65 6d 28 29 3b 0a 20 20 6e 20 3d 20 6d  erMem();.  n = m
54970 65 6d 2e 6d 78 55 73 65 64 3b 0a 20 20 69 66 28  em.mxUsed;.  if(
54980 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20   resetFlag ){.  
54990 20 20 6d 65 6d 2e 6d 78 55 73 65 64 20 3d 20 6d    mem.mxUsed = m
549a0 65 6d 2e 6e 6f 77 55 73 65 64 3b 0a 20 20 7d 0a  em.nowUsed;.  }.
549b0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
549c0 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
549d0 3b 20 20 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a  ;  .  return n;.
549e0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
549f0 74 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61  the alarm callba
54a00 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ck.*/.SQLITE_API
54a10 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d   int sqlite3_mem
54a20 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f 69  ory_alarm(.  voi
54a30 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f  d(*xCallback)(vo
54a40 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65  id *pArg, sqlite
54a50 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74  3_int64 used,int
54a60 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72   N),.  void *pAr
54a70 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  g,.  sqlite3_int
54a80 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b  64 iThreshold.){
54a90 0a 20 20 65 6e 74 65 72 4d 65 6d 28 29 3b 0a 20  .  enterMem();. 
54aa0 20 6d 65 6d 2e 61 6c 61 72 6d 43 61 6c 6c 62 61   mem.alarmCallba
54ab0 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a  ck = xCallback;.
54ac0 20 20 6d 65 6d 2e 61 6c 61 72 6d 41 72 67 20 3d    mem.alarmArg =
54ad0 20 70 41 72 67 3b 0a 20 20 6d 65 6d 2e 61 6c 61   pArg;.  mem.ala
54ae0 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54  rmThreshold = iT
54af0 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69  hreshold;.  sqli
54b00 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
54b10 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65  mem.mutex);.  re
54b20 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
54b30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72  }../*.** Trigger
54b40 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73   the alarm .*/.s
54b50 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
54b60 65 33 4d 65 6d 73 79 73 41 6c 61 72 6d 28 69 6e  e3MemsysAlarm(in
54b70 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64  t nByte){.  void
54b80 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f   (*xCallback)(vo
54b90 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36  id*,sqlite3_int6
54ba0 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65  4,int);.  sqlite
54bb0 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b  3_int64 nowUsed;
54bc0 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20  .  void *pArg;. 
54bd0 20 69 66 28 20 6d 65 6d 2e 61 6c 61 72 6d 43 61   if( mem.alarmCa
54be0 6c 6c 62 61 63 6b 3d 3d 30 20 7c 7c 20 6d 65 6d  llback==0 || mem
54bf0 2e 61 6c 61 72 6d 42 75 73 79 20 20 29 20 72 65  .alarmBusy  ) re
54c00 74 75 72 6e 3b 0a 20 20 6d 65 6d 2e 61 6c 61 72  turn;.  mem.alar
54c10 6d 42 75 73 79 20 3d 20 31 3b 0a 20 20 78 43 61  mBusy = 1;.  xCa
54c20 6c 6c 62 61 63 6b 20 3d 20 6d 65 6d 2e 61 6c 61  llback = mem.ala
54c30 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f  rmCallback;.  no
54c40 77 55 73 65 64 20 3d 20 6d 65 6d 2e 6e 6f 77 55  wUsed = mem.nowU
54c50 73 65 64 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65  sed;.  pArg = me
54c60 6d 2e 61 6c 61 72 6d 41 72 67 3b 0a 20 20 73 71  m.alarmArg;.  sq
54c70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
54c80 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  e(mem.mutex);.  
54c90 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20  xCallback(pArg, 
54ca0 6e 6f 77 55 73 65 64 2c 20 6e 42 79 74 65 29 3b  nowUsed, nByte);
54cb0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
54cc0 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78  _enter(mem.mutex
54cd0 29 3b 0a 20 20 6d 65 6d 2e 61 6c 61 72 6d 42 75  );.  mem.alarmBu
54ce0 73 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  sy = 0;.}../*.**
54cf0 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73   Allocate nBytes
54d00 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 53 51   of memory.*/.SQ
54d10 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
54d20 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e  qlite3_malloc(in
54d30 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c  t nBytes){.  sql
54d40 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20  ite3_int64 *p = 
54d50 30 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e  0;.  if( nBytes>
54d60 30 20 29 7b 0a 20 20 20 20 65 6e 74 65 72 4d 65  0 ){.    enterMe
54d70 6d 28 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d  m();.    if( mem
54d80 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 21 3d  .alarmCallback!=
54d90 30 20 26 26 20 6d 65 6d 2e 6e 6f 77 55 73 65 64  0 && mem.nowUsed
54da0 2b 6e 42 79 74 65 73 3e 3d 6d 65 6d 2e 61 6c 61  +nBytes>=mem.ala
54db0 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20  rmThreshold ){. 
54dc0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 73       sqlite3Mems
54dd0 79 73 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b  ysAlarm(nBytes);
54de0 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 6d  .    }.    p = m
54df0 61 6c 6c 6f 63 28 6e 42 79 74 65 73 2b 38 29 3b  alloc(nBytes+8);
54e00 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  .    if( p==0 ){
54e10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  .      sqlite3Me
54e20 6d 73 79 73 41 6c 61 72 6d 28 6e 42 79 74 65 73  msysAlarm(nBytes
54e30 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 6d 61 6c  );.      p = mal
54e40 6c 6f 63 28 6e 42 79 74 65 73 2b 38 29 3b 0a 20  loc(nBytes+8);. 
54e50 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 20 29     }.    if( p )
54e60 7b 0a 20 20 20 20 20 20 70 5b 30 5d 20 3d 20 6e  {.      p[0] = n
54e70 42 79 74 65 73 3b 0a 20 20 20 20 20 20 70 2b 2b  Bytes;.      p++
54e80 3b 0a 20 20 20 20 20 20 6d 65 6d 2e 6e 6f 77 55  ;.      mem.nowU
54e90 73 65 64 20 2b 3d 20 6e 42 79 74 65 73 3b 0a 20  sed += nBytes;. 
54ea0 20 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 6f 77       if( mem.now
54eb0 55 73 65 64 3e 6d 65 6d 2e 6d 78 55 73 65 64 20  Used>mem.mxUsed 
54ec0 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 6d  ){.        mem.m
54ed0 78 55 73 65 64 20 3d 20 6d 65 6d 2e 6e 6f 77 55  xUsed = mem.nowU
54ee0 73 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  sed;.      }.   
54ef0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d   }.    sqlite3_m
54f00 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d  utex_leave(mem.m
54f10 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
54f20 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d  urn (void*)p; .}
54f30 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d  ../*.** Free mem
54f40 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ory..*/.SQLITE_A
54f50 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
54f60 66 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f  free(void *pPrio
54f70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  r){.  sqlite3_in
54f80 74 36 34 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 42  t64 *p;.  int nB
54f90 79 74 65 3b 0a 20 20 69 66 28 20 70 50 72 69 6f  yte;.  if( pPrio
54fa0 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  r==0 ){.    retu
54fb0 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rn;.  }.  assert
54fc0 28 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 29  ( mem.mutex!=0 )
54fd0 3b 0a 20 20 70 20 3d 20 70 50 72 69 6f 72 3b 0a  ;.  p = pPrior;.
54fe0 20 20 70 2d 2d 3b 0a 20 20 6e 42 79 74 65 20 3d    p--;.  nByte =
54ff0 20 28 69 6e 74 29 2a 70 3b 0a 20 20 73 71 6c 69   (int)*p;.  sqli
55000 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
55010 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65  mem.mutex);.  me
55020 6d 2e 6e 6f 77 55 73 65 64 20 2d 3d 20 6e 42 79  m.nowUsed -= nBy
55030 74 65 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 20  te;.  free(p);. 
55040 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
55050 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b  eave(mem.mutex);
55060 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e    .}../*.** Chan
55070 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  ge the size of a
55080 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72  n existing memor
55090 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a  y allocation.*/.
550a0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
550b0 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  *sqlite3_realloc
550c0 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69  (void *pPrior, i
550d0 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e  nt nBytes){.  in
550e0 74 20 6e 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65  t nOld;.  sqlite
550f0 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69 66  3_int64 *p;.  if
55100 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20  ( pPrior==0 ){. 
55110 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
55120 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29  3_malloc(nBytes)
55130 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74  ;.  }.  if( nByt
55140 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  es<=0 ){.    sql
55150 69 74 65 33 5f 66 72 65 65 28 70 50 72 69 6f 72  ite3_free(pPrior
55160 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
55170 0a 20 20 7d 0a 20 20 70 20 3d 20 70 50 72 69 6f  .  }.  p = pPrio
55180 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 6e 4f 6c 64  r;.  p--;.  nOld
55190 20 3d 20 28 69 6e 74 29 70 5b 30 5d 3b 0a 20 20   = (int)p[0];.  
551a0 61 73 73 65 72 74 28 20 6d 65 6d 2e 6d 75 74 65  assert( mem.mute
551b0 78 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65  x!=0 );.  sqlite
551c0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
551d0 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  m.mutex);.  if( 
551e0 6d 65 6d 2e 6e 6f 77 55 73 65 64 2b 6e 42 79 74  mem.nowUsed+nByt
551f0 65 73 2d 6e 4f 6c 64 3e 3d 6d 65 6d 2e 61 6c 61  es-nOld>=mem.ala
55200 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20  rmThreshold ){. 
55210 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73     sqlite3Memsys
55220 41 6c 61 72 6d 28 6e 42 79 74 65 73 2d 6e 4f 6c  Alarm(nBytes-nOl
55230 64 29 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 72 65  d);.  }.  p = re
55240 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 74 65 73 2b  alloc(p, nBytes+
55250 38 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  8);.  if( p==0 )
55260 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  {.    sqlite3Mem
55270 73 79 73 41 6c 61 72 6d 28 6e 42 79 74 65 73 29  sysAlarm(nBytes)
55280 3b 0a 20 20 20 20 70 20 3d 20 72 65 61 6c 6c 6f  ;.    p = reallo
55290 63 28 70 2c 20 6e 42 79 74 65 73 2b 38 29 3b 0a  c(p, nBytes+8);.
552a0 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20    }.  if( p ){. 
552b0 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 73     p[0] = nBytes
552c0 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 20 20 6d  ;.    p++;.    m
552d0 65 6d 2e 6e 6f 77 55 73 65 64 20 2b 3d 20 6e 42  em.nowUsed += nB
552e0 79 74 65 73 2d 6e 4f 6c 64 3b 0a 20 20 20 20 69  ytes-nOld;.    i
552f0 66 28 20 6d 65 6d 2e 6e 6f 77 55 73 65 64 3e 6d  f( mem.nowUsed>m
55300 65 6d 2e 6d 78 55 73 65 64 20 29 7b 0a 20 20 20  em.mxUsed ){.   
55310 20 20 20 6d 65 6d 2e 6d 78 55 73 65 64 20 3d 20     mem.mxUsed = 
55320 6d 65 6d 2e 6e 6f 77 55 73 65 64 3b 0a 20 20 20  mem.nowUsed;.   
55330 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
55340 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
55350 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  .mutex);.  retur
55360 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 23  n (void*)p;.}..#
55370 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45  endif /* !SQLITE
55380 5f 4d 45 4d 44 45 42 55 47 20 26 26 20 21 53 51  _MEMDEBUG && !SQ
55390 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59  LITE_OMIT_MEMORY
553a0 5f 41 4c 4c 4f 43 41 54 49 4f 4e 20 2a 2f 0a 0a  _ALLOCATION */..
553b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
553c0 45 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a 2a  End of mem1.c **
553d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
553e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
553f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
55400 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
55410 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32 2e  Begin file mem2.
55420 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
55430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
55450 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
55460 74 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 15.**.** The a
55470 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
55480 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
55490 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
554a0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
554b0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
554c0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
554d0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
554e0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
554f0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
55500 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
55510 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
55520 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
55530 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
55540 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
55550 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
55560 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
55570 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
55580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
555a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
555b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
555c0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
555d0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
555e0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
555f0 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72  mplement a memor
55600 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
55610 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73  subsystem for us
55620 65 20 62 79 20 53 51 4c 69 74 65 2e 20 20 0a 2a  e by SQLite.  .*
55630 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 32 2e 63  *.** $Id: mem2.c
55640 2c 76 20 31 2e 31 33 20 32 30 30 37 2f 30 39 2f  ,v 1.13 2007/09/
55650 30 31 20 30 39 3a 30 32 3a 35 34 20 64 61 6e 69  01 09:02:54 dani
55660 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
55670 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
55680 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
55690 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
556a0 75 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65  used only if the
556b0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45  .** SQLITE_MEMDE
556c0 42 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66  BUG macro is def
556d0 69 6e 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f  ined and SQLITE_
556e0 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f  OMIT_MEMORY_ALLO
556f0 43 41 54 49 4f 4e 0a 2a 2a 20 69 73 20 6e 6f 74  CATION.** is not
55700 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66   defined..*/.#if
55710 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
55720 4d 45 4d 44 45 42 55 47 29 20 26 26 20 21 64 65  MEMDEBUG) && !de
55730 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
55740 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54  T_MEMORY_ALLOCAT
55750 49 4f 4e 29 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 77  ION)../*.** We w
55760 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 63  ill eventually c
55770 6f 6e 73 74 72 75 63 74 20 6d 75 6c 74 69 70 6c  onstruct multipl
55780 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
55790 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 73 0a 2a  ion subsystems.*
557a0 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75  * suitable for u
557b0 73 65 20 69 6e 20 76 61 72 69 6f 75 73 20 63 6f  se in various co
557c0 6e 74 65 78 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ntexts:.**.**   
557d0 20 2a 20 20 4e 6f 72 6d 61 6c 20 6d 75 6c 74 69   *  Normal multi
557e0 2d 74 68 72 65 61 64 65 64 20 62 75 69 6c 64 73  -threaded builds
557f0 0a 2a 2a 20 20 20 20 2a 20 20 4e 6f 72 6d 61 6c  .**    *  Normal
55800 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64   single-threaded
55810 20 62 75 69 6c 64 73 0a 2a 2a 20 20 20 20 2a 20   builds.**    * 
55820 20 44 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64   Debugging build
55830 73 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  s.**.** This ver
55840 73 69 6f 6e 20 69 73 20 73 75 69 74 61 62 6c 65  sion is suitable
55850 20 66 6f 72 20 75 73 65 20 69 6e 20 64 65 62 75   for use in debu
55860 67 67 69 6e 67 20 62 75 69 6c 64 73 2e 0a 2a 2a  gging builds..**
55870 0a 2a 2a 20 46 65 61 74 75 72 65 73 3a 0a 2a 2a  .** Features:.**
55880 0a 2a 2a 20 20 20 20 2a 20 45 76 65 72 79 20 61  .**    * Every a
55890 6c 6c 6f 63 61 74 65 20 68 61 73 20 67 75 61 72  llocate has guar
558a0 64 73 20 61 74 20 62 6f 74 68 20 65 6e 64 73 2e  ds at both ends.
558b0 0a 2a 2a 20 20 20 20 2a 20 4e 65 77 20 61 6c 6c  .**    * New all
558c0 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 69 6e 69  ocations are ini
558d0 74 69 61 6c 69 7a 65 64 20 77 69 74 68 20 72 61  tialized with ra
558e0 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 20 20 20 2a  ndomness.**    *
558f0 20 41 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65   Allocations are
55900 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74   overwritten wit
55910 68 20 72 61 6e 64 6f 6d 6e 65 73 73 20 77 68 65  h randomness whe
55920 6e 20 66 72 65 65 64 0a 2a 2a 20 20 20 20 2a 20  n freed.**    * 
55930 4f 70 74 69 6f 6e 61 6c 20 6c 6f 67 73 20 6f 66  Optional logs of
55940 20 6d 61 6c 6c 6f 63 20 61 63 74 69 76 69 74 79   malloc activity
55950 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 20 20   generated.**   
55960 20 2a 20 53 75 6d 6d 61 72 79 20 6f 66 20 6f 75   * Summary of ou
55970 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61  tstanding alloca
55980 74 69 6f 6e 73 20 77 69 74 68 20 62 61 63 6b 74  tions with backt
55990 72 61 63 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20  races to the.** 
559a0 20 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 61 6c       point of al
559b0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 20 20 20  location..**    
559c0 2a 20 54 68 65 20 61 62 69 6c 69 74 79 20 74 6f  * The ability to
559d0 20 73 69 6d 75 6c 61 74 65 20 6d 65 6d 6f 72 79   simulate memory
559e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
559f0 75 72 65 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ure.*/../*.** Th
55a00 65 20 62 61 63 6b 74 72 61 63 65 20 66 75 6e 63  e backtrace func
55a10 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c  tionality is onl
55a20 79 20 61 76 61 69 6c 61 62 6c 65 20 77 69 74 68  y available with
55a30 20 47 4c 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66   GLIBC.*/.#ifdef
55a40 20 5f 5f 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74   __GLIBC__.  ext
55a50 65 72 6e 20 69 6e 74 20 62 61 63 6b 74 72 61 63  ern int backtrac
55a60 65 28 76 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20  e(void**,int);. 
55a70 20 65 78 74 65 72 6e 20 76 6f 69 64 20 62 61 63   extern void bac
55a80 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66  ktrace_symbols_f
55a90 64 28 76 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e  d(void*const*,in
55aa0 74 2c 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20  t,int);.#else.# 
55ab0 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65  define backtrace
55ac0 28 41 2c 42 29 20 30 0a 23 20 64 65 66 69 6e 65  (A,B) 0.# define
55ad0 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f   backtrace_symbo
55ae0 6c 73 5f 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e  ls_fd(A,B,C).#en
55af0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  dif../*.** Each 
55b00 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
55b10 6e 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69  n looks like thi
55b20 73 3a 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d  s:.**.**  ------
55b30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55b40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55b50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55b60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55b70 2d 2d 0a 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c  --.**  | Title |
55b80 20 20 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e    backtrace poin
55b90 74 65 72 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b  ters |  MemBlock
55ba0 48 64 72 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f  Hdr |  allocatio
55bb0 6e 20 7c 20 20 45 6e 64 47 75 61 72 64 20 7c 0a  n |  EndGuard |.
55bc0 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **  ------------
55bd0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55be0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55bf0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55c00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a  ------------.**.
55c10 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  ** The applicati
55c20 6f 6e 20 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c  on code sees onl
55c30 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  y a pointer to t
55c40 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20  he allocation.  
55c50 57 65 20 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61  We have.** to ba
55c60 63 6b 20 75 70 20 66 72 6f 6d 20 74 68 65 20 61  ck up from the a
55c70 6c 6c 6f 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65  llocation pointe
55c80 72 20 74 6f 20 66 69 6e 64 20 74 68 65 20 4d 65  r to find the Me
55c90 6d 42 6c 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a  mBlockHdr.  The.
55ca0 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74  ** MemBlockHdr t
55cb0 65 6c 6c 73 20 75 73 20 74 68 65 20 73 69 7a 65  ells us the size
55cc0 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
55cd0 6f 6e 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  on and the numbe
55ce0 72 20 6f 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63  r of.** backtrac
55cf0 65 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65  e pointers.  The
55d00 72 65 20 69 73 20 61 6c 73 6f 20 61 20 67 75 61  re is also a gua
55d10 72 64 20 77 6f 72 64 20 61 74 20 74 68 65 20 65  rd word at the e
55d20 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d  nd of the.** Mem
55d30 42 6c 6f 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72  BlockHdr..*/.str
55d40 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
55d50 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
55d60 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20 2a  ockHdr *pNext, *
55d70 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65  pPrev;  /* Linke
55d80 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e  d list of all un
55d90 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a  freed memory */.
55da0 20 20 69 6e 74 20 69 53 69 7a 65 3b 20 20 20 20    int iSize;    
55db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55dc0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
55dd0 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e   this allocation
55de0 20 2a 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b   */.  char nBack
55df0 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20  trace;          
55e00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
55e10 62 65 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65  ber of backtrace
55e20 73 20 6f 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20  s on this alloc 
55e30 2a 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74  */.  char nBackt
55e40 72 61 63 65 53 6c 6f 74 73 3b 20 20 20 20 20 20  raceSlots;      
55e50 20 20 20 20 20 20 20 20 20 2f 2a 20 41 76 61 69           /* Avai
55e60 6c 61 62 6c 65 20 62 61 63 6b 74 72 61 63 65 20  lable backtrace 
55e70 73 6c 6f 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74  slots */.  short
55e80 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20   nTitle;        
55e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
55ea0 2a 20 42 79 74 65 73 20 6f 66 20 74 69 74 6c 65  * Bytes of title
55eb0 3b 20 69 6e 63 6c 75 64 65 73 20 27 5c 30 27 20  ; includes '\0' 
55ec0 2a 2f 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75  */.  int iForeGu
55ed0 61 72 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ard;            
55ee0 20 20 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72           /* Guar
55ef0 64 20 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74  d word for sanit
55f00 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47  y */.};../*.** G
55f10 75 61 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64  uard words.*/.#d
55f20 65 66 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20  efine FOREGUARD 
55f30 30 78 38 30 46 35 45 31 35 33 0a 23 64 65 66 69  0x80F5E153.#defi
55f40 6e 65 20 52 45 41 52 47 55 41 52 44 20 30 78 45  ne REARGUARD 0xE
55f50 34 36 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 41  4676B53../*.** A
55f60 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63  ll of the static
55f70 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
55f80 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  by this module a
55f90 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20  re collected.** 
55fa0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74  into a single st
55fb0 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d  ructure named "m
55fc0 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f  em".  This is to
55fd0 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61   keep the.** sta
55fe0 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72  tic variables or
55ff0 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72  ganized and to r
56000 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20  educe namespace 
56010 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65  pollution.** whe
56020 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  n this module is
56030 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f   combined with o
56040 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c  ther in the amal
56050 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  gamation..*/.sta
56060 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 2f  tic struct {.  /
56070 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d  *.  ** The alarm
56080 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74   callback and it
56090 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68  s arguments.  Th
560a0 65 20 6d 65 6d 2e 6d 75 74 65 78 20 6c 6f 63 6b  e mem.mutex lock
560b0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65   will.  ** be he
560c0 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c  ld while the cal
560d0 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67  lback is running
560e0 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c  .  Recursive cal
560f0 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  ls into.  ** the
56100 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65   memory subsyste
56110 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62  m are allowed, b
56120 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61  ut no new callba
56130 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a  cks will be.  **
56140 20 69 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c   issued.  The al
56150 61 72 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65  armBusy variable
56160 20 69 73 20 73 65 74 20 74 6f 20 70 72 65 76 65   is set to preve
56170 6e 74 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a  nt recursive.  *
56180 2a 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a  * callbacks..  *
56190 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
561a0 34 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64  4 alarmThreshold
561b0 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d  ;.  void (*alarm
561c0 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  Callback)(void*,
561d0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20   sqlite3_int64, 
561e0 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c  int);.  void *al
561f0 61 72 6d 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c  armArg;.  int al
56200 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a  armBusy;.  .  /*
56210 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63  .  ** Mutex to c
56220 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f  ontrol access to
56230 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
56240 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
56250 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
56260 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20  _mutex *mutex;. 
56270 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 75 72 72   .  /*.  ** Curr
56280 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  ent allocation a
56290 6e 64 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61  nd high-water ma
562a0 72 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  rk..  */.  sqlit
562b0 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64  e3_int64 nowUsed
562c0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
562d0 34 20 6d 78 55 73 65 64 3b 0a 20 20 0a 20 20 2f  4 mxUsed;.  .  /
562e0 2a 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20  *.  ** Head and 
562f0 74 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64  tail of a linked
56300 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74   list of all out
56310 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74  standing allocat
56320 69 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75  ions.  */.  stru
56330 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
56340 70 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74  pFirst;.  struct
56350 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c   MemBlockHdr *pL
56360 61 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  ast;.  .  /*.  *
56370 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
56380 6c 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72  levels of backtr
56390 61 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e  ace to save in n
563a0 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a  ew allocations..
563b0 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b    */.  int nBack
563c0 74 72 61 63 65 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  trace;..  /*.  *
563d0 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f 20  * Title text to 
563e0 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 20  insert in front 
563f0 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20  of each block.  
56400 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b  */.  int nTitle;
56410 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
56420 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 61   of zTitle to sa
56430 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 5c  ve.  Includes '\
56440 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20 2a  0' and padding *
56450 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65 5b  /.  char zTitle[
56460 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69  100];  /* The ti
56470 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f  tle text */..  /
56480 2a 0a 20 20 2a 2a 20 54 68 65 73 65 20 76 61 6c  *.  ** These val
56490 75 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  ues are used to 
564a0 73 69 6d 75 6c 61 74 65 20 6d 61 6c 6c 6f 63 20  simulate malloc 
564b0 66 61 69 6c 75 72 65 73 2e 20 20 57 68 65 6e 0a  failures.  When.
564c0 20 20 2a 2a 20 69 46 61 69 6c 20 69 73 20 31 2c    ** iFail is 1,
564d0 20 73 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c   simulate a mall
564e0 6f 63 20 66 61 69 6c 75 72 65 73 20 61 6e 64 20  oc failures and 
564f0 72 65 73 65 74 20 74 68 65 20 76 61 6c 75 65 0a  reset the value.
56500 20 20 2a 2a 20 74 6f 20 69 52 65 73 65 74 2e 0a    ** to iReset..
56510 20 20 2a 2f 0a 20 20 69 6e 74 20 69 46 61 69 6c    */.  int iFail
56520 3b 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e  ;    /* Decremen
56530 74 20 61 6e 64 20 66 61 69 6c 20 6d 61 6c 6c 6f  t and fail mallo
56540 63 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 31  c when this is 1
56550 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 73 65 74   */.  int iReset
56560 3b 20 20 20 2f 2a 20 57 68 65 6e 20 6d 61 6c 6c  ;   /* When mall
56570 6f 63 20 66 61 69 6c 73 20 73 65 74 20 69 69 46  oc fails set iiF
56580 61 69 6c 20 74 6f 20 74 68 69 73 20 76 61 6c 75  ail to this valu
56590 65 20 2a 2f 0a 20 20 69 6e 74 20 69 46 61 69 6c  e */.  int iFail
565a0 43 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Cnt;         /* 
565b0 4e 75 6d 62 65 72 20 6f 66 20 66 61 69 6c 75 72  Number of failur
565c0 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 6e  es */.  int iBen
565d0 69 67 6e 46 61 69 6c 43 6e 74 3b 20 20 20 2f 2a  ignFailCnt;   /*
565e0 20 4e 75 6d 62 65 72 20 6f 66 20 62 65 6e 69 67   Number of benig
565f0 6e 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20  n failures */.  
56600 69 6e 74 20 69 4e 65 78 74 49 73 42 65 6e 69 67  int iNextIsBenig
56610 6e 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  n;    /* True if
56620 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74   the next call t
56630 6f 20 6d 61 6c 6c 6f 63 20 6d 61 79 20 66 61 69  o malloc may fai
56640 6c 20 62 65 6e 69 67 6e 6c 79 20 2a 2f 0a 0a 20  l benignly */.. 
56650 20 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65   /* .  ** sqlite
56660 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28  3MallocDisallow(
56670 29 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65  ) increments the
56680 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74   following count
56690 65 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  er..  ** sqlite3
566a0 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65  MallocAllow() de
566b0 63 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a  crements it..  *
566c0 2f 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77  /.  int disallow
566d0 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f  ; /* Do not allo
566e0 77 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  w memory allocat
566f0 69 6f 6e 20 2a 2f 0a 20 20 0a 20 20 0a 7d 20 6d  ion */.  .  .} m
56700 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65  em;.../*.** Ente
56710 72 20 74 68 65 20 6d 75 74 65 78 20 6d 65 6d 2e  r the mutex mem.
56720 6d 75 74 65 78 2e 20 41 6c 6c 6f 63 61 74 65 20  mutex. Allocate 
56730 69 74 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20  it if it is not 
56740 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65  already allocate
56750 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
56760 64 20 65 6e 74 65 72 4d 65 6d 28 76 6f 69 64 29  d enterMem(void)
56770 7b 0a 20 20 69 66 28 20 6d 65 6d 2e 6d 75 74 65  {.  if( mem.mute
56780 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e  x==0 ){.    mem.
56790 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f  mutex = sqlite3_
567a0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49  mutex_alloc(SQLI
567b0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
567c0 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  MEM);.  }.  sqli
567d0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
567e0 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f  mem.mutex);.}../
567f0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
56800 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
56810 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b   currently check
56820 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54  ed out..*/.SQLIT
56830 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e  E_API sqlite3_in
56840 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  t64 sqlite3_memo
56850 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20  ry_used(void){. 
56860 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e   sqlite3_int64 n
56870 3b 0a 20 20 65 6e 74 65 72 4d 65 6d 28 29 3b 0a  ;.  enterMem();.
56880 20 20 6e 20 3d 20 6d 65 6d 2e 6e 6f 77 55 73 65    n = mem.nowUse
56890 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
568a0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74  ex_leave(mem.mut
568b0 65 78 29 3b 20 20 0a 20 20 72 65 74 75 72 6e 20  ex);  .  return 
568c0 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  n;.}../*.** Retu
568d0 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61  rn the maximum a
568e0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
568f0 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62 65  that has ever be
56900 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75  en.** checked ou
56910 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 74  t since either t
56920 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
56930 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20  this process.** 
56940 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73  or since the mos
56950 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a  t recent reset..
56960 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
56970 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
56980 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77  te3_memory_highw
56990 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c  ater(int resetFl
569a0 61 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ag){.  sqlite3_i
569b0 6e 74 36 34 20 6e 3b 0a 20 20 65 6e 74 65 72 4d  nt64 n;.  enterM
569c0 65 6d 28 29 3b 0a 20 20 6e 20 3d 20 6d 65 6d 2e  em();.  n = mem.
569d0 6d 78 55 73 65 64 3b 0a 20 20 69 66 28 20 72 65  mxUsed;.  if( re
569e0 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 6d  setFlag ){.    m
569f0 65 6d 2e 6d 78 55 73 65 64 20 3d 20 6d 65 6d 2e  em.mxUsed = mem.
56a00 6e 6f 77 55 73 65 64 3b 0a 20 20 7d 0a 20 20 73  nowUsed;.  }.  s
56a10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
56a20 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20  ve(mem.mutex);  
56a30 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
56a40 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
56a50 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a   alarm callback.
56a60 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
56a70 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  t sqlite3_memory
56a80 5f 61 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a  _alarm(.  void(*
56a90 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20  xCallback)(void 
56aa0 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 69  *pArg, sqlite3_i
56ab0 6e 74 36 34 20 75 73 65 64 2c 20 69 6e 74 20 4e  nt64 used, int N
56ac0 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c  ),.  void *pArg,
56ad0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
56ae0 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20   iThreshold.){. 
56af0 20 65 6e 74 65 72 4d 65 6d 28 29 3b 0a 20 20 6d   enterMem();.  m
56b00 65 6d 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  em.alarmCallback
56b10 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20   = xCallback;.  
56b20 6d 65 6d 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70  mem.alarmArg = p
56b30 41 72 67 3b 0a 20 20 6d 65 6d 2e 61 6c 61 72 6d  Arg;.  mem.alarm
56b40 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72  Threshold = iThr
56b50 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65  eshold;.  sqlite
56b60 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
56b70 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  m.mutex);.  retu
56b80 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
56b90 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72 20 74  ./*.** Trigger t
56ba0 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73 74 61  he alarm .*/.sta
56bb0 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
56bc0 4d 65 6d 73 79 73 41 6c 61 72 6d 28 69 6e 74 20  MemsysAlarm(int 
56bd0 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64 20 28  nByte){.  void (
56be0 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  *xCallback)(void
56bf0 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c  *,sqlite3_int64,
56c00 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  int);.  sqlite3_
56c10 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b 0a 20  int64 nowUsed;. 
56c20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 69   void *pArg;.  i
56c30 66 28 20 6d 65 6d 2e 61 6c 61 72 6d 43 61 6c 6c  f( mem.alarmCall
56c40 62 61 63 6b 3d 3d 30 20 7c 7c 20 6d 65 6d 2e 61  back==0 || mem.a
56c50 6c 61 72 6d 42 75 73 79 20 20 29 20 72 65 74 75  larmBusy  ) retu
56c60 72 6e 3b 0a 20 20 6d 65 6d 2e 61 6c 61 72 6d 42  rn;.  mem.alarmB
56c70 75 73 79 20 3d 20 31 3b 0a 20 20 78 43 61 6c 6c  usy = 1;.  xCall
56c80 62 61 63 6b 20 3d 20 6d 65 6d 2e 61 6c 61 72 6d  back = mem.alarm
56c90 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55  Callback;.  nowU
56ca0 73 65 64 20 3d 20 6d 65 6d 2e 6e 6f 77 55 73 65  sed = mem.nowUse
56cb0 64 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d 2e  d;.  pArg = mem.
56cc0 61 6c 61 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69  alarmArg;.  sqli
56cd0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
56ce0 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43  mem.mutex);.  xC
56cf0 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f  allback(pArg, no
56d00 77 55 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20  wUsed, nByte);. 
56d10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
56d20 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b  nter(mem.mutex);
56d30 0a 20 20 6d 65 6d 2e 61 6c 61 72 6d 42 75 73 79  .  mem.alarmBusy
56d40 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47   = 0;.}../*.** G
56d50 69 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69  iven an allocati
56d60 6f 6e 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d  on, find the Mem
56d70 42 6c 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61  BlockHdr for tha
56d80 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  t allocation..**
56d90 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
56da0 20 63 68 65 63 6b 73 20 74 68 65 20 67 75 61 72   checks the guar
56db0 64 73 20 61 74 20 65 69 74 68 65 72 20 65 6e 64  ds at either end
56dc0 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
56dd0 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65  on and.** if the
56de0 79 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20  y are incorrect 
56df0 69 74 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73  it asserts..*/.s
56e00 74 61 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d  tatic struct Mem
56e10 42 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65  BlockHdr *sqlite
56e20 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72  3MemsysGetHeader
56e30 28 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69  (void *pAllocati
56e40 6f 6e 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65  on){.  struct Me
56e50 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20  mBlockHdr *p;.  
56e60 69 6e 74 20 2a 70 49 6e 74 3b 0a 0a 20 20 70 20  int *pInt;..  p 
56e70 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  = (struct MemBlo
56e80 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69  ckHdr*)pAllocati
56e90 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73  on;.  p--;.  ass
56ea0 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61  ert( p->iForeGua
56eb0 72 64 3d 3d 46 4f 52 45 47 55 41 52 44 20 29 3b  rd==FOREGUARD );
56ec0 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 69  .  assert( (p->i
56ed0 53 69 7a 65 20 26 20 33 29 3d 3d 30 20 29 3b 0a  Size & 3)==0 );.
56ee0 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70    pInt = (int*)p
56ef0 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73  Allocation;.  as
56f00 73 65 72 74 28 20 70 49 6e 74 5b 70 2d 3e 69 53  sert( pInt[p->iS
56f10 69 7a 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d  ize/sizeof(int)]
56f20 3d 3d 52 45 41 52 47 55 41 52 44 20 29 3b 0a 20  ==REARGUARD );. 
56f30 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
56f40 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
56f50 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   is called once 
56f60 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61  the first time a
56f70 20 73 69 6d 75 6c 61 74 65 64 20 6d 65 6d 6f 72   simulated memor
56f80 79 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63  y.** failure occ
56f90 75 72 73 2e 20 20 54 68 65 20 73 6f 6c 65 20 70  urs.  The sole p
56fa0 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 72  urpose of this r
56fb0 6f 75 74 69 6e 65 20 69 73 20 74 6f 20 70 72 6f  outine is to pro
56fc0 76 69 64 65 0a 2a 2a 20 61 20 63 6f 6e 76 65 6e  vide.** a conven
56fd0 69 65 6e 74 20 70 6c 61 63 65 20 74 6f 20 73 65  ient place to se
56fe0 74 20 61 20 64 65 62 75 67 67 65 72 20 62 72 65  t a debugger bre
56ff0 61 6b 70 6f 69 6e 74 20 77 68 65 6e 20 64 65 62  akpoint when deb
57000 75 67 67 69 6e 67 0a 2a 2a 20 65 72 72 6f 72 73  ugging.** errors
57010 20 72 65 6c 61 74 65 64 20 74 6f 20 6d 61 6c 6c   related to mall
57020 6f 63 28 29 20 66 61 69 6c 75 72 65 73 2e 0a 2a  oc() failures..*
57030 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
57040 6c 69 74 65 33 4d 65 6d 73 79 73 46 61 69 6c 65  lite3MemsysFaile
57050 64 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 2e 69  d(void){.  mem.i
57060 46 61 69 6c 43 6e 74 20 3d 20 30 3b 0a 20 20 6d  FailCnt = 0;.  m
57070 65 6d 2e 69 42 65 6e 69 67 6e 46 61 69 6c 43 6e  em.iBenignFailCn
57080 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t = 0;.}../*.** 
57090 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62  Allocate nByte b
570a0 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a  ytes of memory..
570b0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
570c0 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  id *sqlite3_mall
570d0 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  oc(int nByte){. 
570e0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
570f0 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69  Hdr *pHdr;.  voi
57100 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 20  d **pBt;.  char 
57110 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b  *z;.  int *pInt;
57120 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a  .  void *p = 0;.
57130 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 3b    int totalSize;
57140 0a 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 30 20  ..  if( nByte>0 
57150 29 7b 0a 20 20 20 20 65 6e 74 65 72 4d 65 6d 28  ){.    enterMem(
57160 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  );.    assert( m
57170 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29  em.disallow==0 )
57180 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 61 6c  ;.    if( mem.al
57190 61 72 6d 43 61 6c 6c 62 61 63 6b 21 3d 30 20 26  armCallback!=0 &
571a0 26 20 6d 65 6d 2e 6e 6f 77 55 73 65 64 2b 6e 42  & mem.nowUsed+nB
571b0 79 74 65 3e 3d 6d 65 6d 2e 61 6c 61 72 6d 54 68  yte>=mem.alarmTh
571c0 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20  reshold ){.     
571d0 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 41 6c   sqlite3MemsysAl
571e0 61 72 6d 28 6e 42 79 74 65 29 3b 0a 20 20 20 20  arm(nByte);.    
571f0 7d 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 28 6e  }.    nByte = (n
57200 42 79 74 65 2b 33 29 26 7e 33 3b 0a 20 20 20 20  Byte+3)&~3;.    
57210 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 42 79 74  totalSize = nByt
57220 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72  e + sizeof(*pHdr
57230 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20  ) + sizeof(int) 
57240 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +.              
57250 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63     mem.nBacktrac
57260 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20  e*sizeof(void*) 
57270 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20  + mem.nTitle;.  
57280 20 20 69 66 28 20 6d 65 6d 2e 69 46 61 69 6c 3e    if( mem.iFail>
57290 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d  0 ){.      if( m
572a0 65 6d 2e 69 46 61 69 6c 3d 3d 31 20 29 7b 0a 20  em.iFail==1 ){. 
572b0 20 20 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20         p = 0;.  
572c0 20 20 20 20 20 20 6d 65 6d 2e 69 46 61 69 6c 20        mem.iFail 
572d0 3d 20 6d 65 6d 2e 69 52 65 73 65 74 3b 0a 20 20  = mem.iReset;.  
572e0 20 20 20 20 20 20 69 66 28 20 6d 65 6d 2e 69 46        if( mem.iF
572f0 61 69 6c 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  ailCnt==0 ){.   
57300 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65         sqlite3Me
57310 6d 73 79 73 46 61 69 6c 65 64 28 29 3b 20 20 2f  msysFailed();  /
57320 2a 20 41 20 70 6c 61 63 65 20 74 6f 20 73 65 74  * A place to set
57330 20 61 20 62 72 65 61 6b 70 6f 69 6e 74 20 2a 2f   a breakpoint */
57340 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
57350 20 20 20 6d 65 6d 2e 69 46 61 69 6c 43 6e 74 2b     mem.iFailCnt+
57360 2b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6d  +;.        if( m
57370 65 6d 2e 69 4e 65 78 74 49 73 42 65 6e 69 67 6e  em.iNextIsBenign
57380 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65   ){.          me
57390 6d 2e 69 42 65 6e 69 67 6e 46 61 69 6c 43 6e 74  m.iBenignFailCnt
573a0 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ++;.        }.  
573b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
573c0 20 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f     p = malloc(to
573d0 74 61 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  talSize);.      
573e0 20 20 6d 65 6d 2e 69 46 61 69 6c 2d 2d 3b 0a 20    mem.iFail--;. 
573f0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
57400 7b 0a 20 20 20 20 20 20 70 20 3d 20 6d 61 6c 6c  {.      p = mall
57410 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20  oc(totalSize);. 
57420 20 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b       if( p==0 ){
57430 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
57440 4d 65 6d 73 79 73 41 6c 61 72 6d 28 6e 42 79 74  MemsysAlarm(nByt
57450 65 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20  e);.        p = 
57460 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65  malloc(totalSize
57470 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
57480 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
57490 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20 20 20      z = p;.     
574a0 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26   pBt = (void**)&
574b0 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20  z[mem.nTitle];. 
574c0 20 20 20 20 20 70 48 64 72 20 3d 20 28 73 74 72       pHdr = (str
574d0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a  uct MemBlockHdr*
574e0 29 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74  )&pBt[mem.nBackt
574f0 72 61 63 65 5d 3b 0a 20 20 20 20 20 20 70 48 64  race];.      pHd
57500 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  r->pNext = 0;.  
57510 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 20      pHdr->pPrev 
57520 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20 20  = mem.pLast;.   
57530 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 74     if( mem.pLast
57540 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e   ){.        mem.
57550 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70  pLast->pNext = p
57560 48 64 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  Hdr;.      }else
57570 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 70 46  {.        mem.pF
57580 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20  irst = pHdr;.   
57590 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 2e 70     }.      mem.p
575a0 4c 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20  Last = pHdr;.   
575b0 20 20 20 70 48 64 72 2d 3e 69 46 6f 72 65 47 75     pHdr->iForeGu
575c0 61 72 64 20 3d 20 46 4f 52 45 47 55 41 52 44 3b  ard = FOREGUARD;
575d0 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61  .      pHdr->nBa
575e0 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 3d 20 6d  cktraceSlots = m
575f0 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20  em.nBacktrace;. 
57600 20 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 6c       pHdr->nTitl
57610 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a  e = mem.nTitle;.
57620 20 20 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42        if( mem.nB
57630 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20  acktrace ){.    
57640 20 20 20 20 76 6f 69 64 20 2a 61 41 64 64 72 5b      void *aAddr[
57650 34 30 5d 3b 0a 20 20 20 20 20 20 20 20 70 48 64  40];.        pHd
57660 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20  r->nBacktrace = 
57670 62 61 63 6b 74 72 61 63 65 28 61 41 64 64 72 2c  backtrace(aAddr,
57680 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2b   mem.nBacktrace+
57690 31 29 2d 31 3b 0a 20 20 20 20 20 20 20 20 6d 65  1)-1;.        me
576a0 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 72  mcpy(pBt, &aAddr
576b0 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b  [1], pHdr->nBack
576c0 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69  trace*sizeof(voi
576d0 64 2a 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  d*));.      }els
576e0 65 7b 0a 20 20 20 20 20 20 20 20 70 48 64 72 2d  e{.        pHdr-
576f0 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 30 3b  >nBacktrace = 0;
57700 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
57710 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b  f( mem.nTitle ){
57720 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
57730 7a 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d  z, mem.zTitle, m
57740 65 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20  em.nTitle);.    
57750 20 20 7d 0a 20 20 20 20 20 20 70 48 64 72 2d 3e    }.      pHdr->
57760 69 53 69 7a 65 20 3d 20 6e 42 79 74 65 3b 0a 20  iSize = nByte;. 
57770 20 20 20 20 20 70 49 6e 74 20 3d 20 28 69 6e 74       pInt = (int
57780 2a 29 26 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20  *)&pHdr[1];.    
57790 20 20 70 49 6e 74 5b 6e 42 79 74 65 2f 73 69 7a    pInt[nByte/siz
577a0 65 6f 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52  eof(int)] = REAR
577b0 47 55 41 52 44 3b 0a 20 20 20 20 20 20 6d 65 6d  GUARD;.      mem
577c0 73 65 74 28 70 49 6e 74 2c 20 30 78 36 35 2c 20  set(pInt, 0x65, 
577d0 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 6d 65  nByte);.      me
577e0 6d 2e 6e 6f 77 55 73 65 64 20 2b 3d 20 6e 42 79  m.nowUsed += nBy
577f0 74 65 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65  te;.      if( me
57800 6d 2e 6e 6f 77 55 73 65 64 3e 6d 65 6d 2e 6d 78  m.nowUsed>mem.mx
57810 55 73 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  Used ){.        
57820 6d 65 6d 2e 6d 78 55 73 65 64 20 3d 20 6d 65 6d  mem.mxUsed = mem
57830 2e 6e 6f 77 55 73 65 64 3b 0a 20 20 20 20 20 20  .nowUsed;.      
57840 7d 0a 20 20 20 20 20 20 70 20 3d 20 28 76 6f 69  }.      p = (voi
57850 64 2a 29 70 49 6e 74 3b 0a 20 20 20 20 7d 0a 20  d*)pInt;.    }. 
57860 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
57870 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
57880 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 2e 69 4e 65  );.  }.  mem.iNe
57890 78 74 49 73 42 65 6e 69 67 6e 20 3d 20 30 3b 0a  xtIsBenign = 0;.
578a0 20 20 72 65 74 75 72 6e 20 70 3b 20 0a 7d 0a 0a    return p; .}..
578b0 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72  /*.** Free memor
578c0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
578d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72   void sqlite3_fr
578e0 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29  ee(void *pPrior)
578f0 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
57900 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20  ockHdr *pHdr;.  
57910 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68  void **pBt;.  ch
57920 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 70 50 72  ar *z;.  if( pPr
57930 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ior==0 ){.    re
57940 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65  turn;.  }.  asse
57950 72 74 28 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30  rt( mem.mutex!=0
57960 20 29 3b 0a 20 20 70 48 64 72 20 3d 20 73 71 6c   );.  pHdr = sql
57970 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61  ite3MemsysGetHea
57980 64 65 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70  der(pPrior);.  p
57990 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64  Bt = (void**)pHd
579a0 72 3b 0a 20 20 70 42 74 20 2d 3d 20 70 48 64 72  r;.  pBt -= pHdr
579b0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  ->nBacktraceSlot
579c0 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  s;.  sqlite3_mut
579d0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74  ex_enter(mem.mut
579e0 65 78 29 3b 0a 20 20 6d 65 6d 2e 6e 6f 77 55 73  ex);.  mem.nowUs
579f0 65 64 20 2d 3d 20 70 48 64 72 2d 3e 69 53 69 7a  ed -= pHdr->iSiz
57a00 65 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70  e;.  if( pHdr->p
57a10 50 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73 65  Prev ){.    asse
57a20 72 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76 2d  rt( pHdr->pPrev-
57a30 3e 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b 0a  >pNext==pHdr );.
57a40 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 2d      pHdr->pPrev-
57a50 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e 70  >pNext = pHdr->p
57a60 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Next;.  }else{. 
57a70 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70     assert( mem.p
57a80 46 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20  First==pHdr );. 
57a90 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20     mem.pFirst = 
57aa0 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  pHdr->pNext;.  }
57ab0 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e 65  .  if( pHdr->pNe
57ac0 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  xt ){.    assert
57ad0 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70  ( pHdr->pNext->p
57ae0 50 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20 20  Prev==pHdr );.  
57af0 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70    pHdr->pNext->p
57b00 50 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 72  Prev = pHdr->pPr
57b10 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ev;.  }else{.   
57b20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c 61   assert( mem.pLa
57b30 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20  st==pHdr );.    
57b40 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72  mem.pLast = pHdr
57b50 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 7a  ->pPrev;.  }.  z
57b60 20 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a 20   = (char*)pBt;. 
57b70 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69 74   z -= pHdr->nTit
57b80 6c 65 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c 20  le;.  memset(z, 
57b90 30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f 69  0x2b, sizeof(voi
57ba0 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b 74  d*)*pHdr->nBackt
57bb0 72 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a 65  raceSlots + size
57bc0 6f 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20 20  of(*pHdr) +.    
57bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 48                pH
57be0 64 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a 65  dr->iSize + size
57bf0 6f 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d 3e  of(int) + pHdr->
57c00 6e 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65 28  nTitle);.  free(
57c10 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  z);.  sqlite3_mu
57c20 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75  tex_leave(mem.mu
57c30 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  tex);  .}../*.**
57c40 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65   Change the size
57c50 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   of an existing 
57c60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
57c70 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69  n..**.** For thi
57c80 73 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70 6c  s debugging impl
57c90 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20 2a  ementation, we *
57ca0 61 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20 63  always* make a c
57cb0 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c  opy of the.** al
57cc0 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61 20  location into a 
57cd0 6e 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65 6d  new place in mem
57ce0 6f 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77 61  ory.  In this wa
57cf0 79 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 69  y, if the .** hi
57d00 67 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 20  gher level code 
57d10 69 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72  is using pointer
57d20 20 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f   to the old allo
57d30 63 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a 2a  cation, it is .*
57d40 2a 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65  * much more like
57d50 6c 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 20  ly to break and 
57d60 77 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 65  we are much more
57d70 20 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 0a   liking to find.
57d80 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f  ** the error..*/
57d90 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
57da0 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   *sqlite3_reallo
57db0 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20  c(void *pPrior, 
57dc0 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74  int nByte){.  st
57dd0 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  ruct MemBlockHdr
57de0 20 2a 70 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69   *pOldHdr;.  voi
57df0 64 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70  d *pNew;.  if( p
57e00 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20  Prior==0 ){.    
57e10 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d  return sqlite3_m
57e20 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  alloc(nByte);.  
57e30 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30  }.  if( nByte<=0
57e40 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
57e50 66 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  free(pPrior);.  
57e60 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
57e70 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69    assert( mem.di
57e80 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70  sallow==0 );.  p
57e90 4f 6c 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33  OldHdr = sqlite3
57ea0 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28  MemsysGetHeader(
57eb0 70 50 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20  pPrior);.  pNew 
57ec0 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
57ed0 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70  (nByte);.  if( p
57ee0 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  New ){.    memcp
57ef0 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20  y(pNew, pPrior, 
57f00 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69  nByte<pOldHdr->i
57f10 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70  Size ? nByte : p
57f20 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a  OldHdr->iSize);.
57f30 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f      if( nByte>pO
57f40 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a  ldHdr->iSize ){.
57f50 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28        memset(&((
57f60 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64  char*)pNew)[pOld
57f70 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32  Hdr->iSize], 0x2
57f80 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48  b, nByte - pOldH
57f90 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20  dr->iSize);.    
57fa0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
57fb0 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a  ee(pPrior);.  }.
57fc0 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
57fd0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
57fe0 6e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 74 72  number of backtr
57ff0 61 63 65 20 6c 65 76 65 6c 73 20 6b 65 70 74 20  ace levels kept 
58000 66 6f 72 20 65 61 63 68 20 61 6c 6c 6f 63 61 74  for each allocat
58010 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20  ion..** A value 
58020 6f 66 20 7a 65 72 6f 20 74 75 72 6e 73 20 6f 66  of zero turns of
58030 20 62 61 63 6b 74 72 61 63 69 6e 67 2e 20 20 54   backtracing.  T
58040 68 65 20 6e 75 6d 62 65 72 20 69 73 20 61 6c 77  he number is alw
58050 61 79 73 20 72 6f 75 6e 64 65 64 0a 2a 2a 20 75  ays rounded.** u
58060 70 20 74 6f 20 61 20 6d 75 6c 74 69 70 6c 65 20  p to a multiple 
58070 6f 66 20 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  of 2..*/.SQLITE_
58080 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
58090 5f 6d 65 6d 64 65 62 75 67 5f 62 61 63 6b 74 72  _memdebug_backtr
580a0 61 63 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a  ace(int depth){.
580b0 20 20 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b    if( depth<0 ){
580c0 20 64 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20   depth = 0; }.  
580d0 69 66 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20  if( depth>20 ){ 
580e0 64 65 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20  depth = 20; }.  
580f0 64 65 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31  depth = (depth+1
58100 29 26 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42  )&0xfe;.  mem.nB
58110 61 63 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68  acktrace = depth
58120 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
58130 68 65 20 74 69 74 6c 65 20 73 74 72 69 6e 67 20  he title string 
58140 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 61  for subsequent a
58150 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53  llocations..*/.S
58160 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
58170 71 6c 69 74 65 33 5f 6d 65 6d 64 65 62 75 67 5f  qlite3_memdebug_
58180 73 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63  settitle(const c
58190 68 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20  har *zTitle){.  
581a0 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a  int n = strlen(z
581b0 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 65 6e  Title) + 1;.  en
581c0 74 65 72 4d 65 6d 28 29 3b 0a 20 20 69 66 28 20  terMem();.  if( 
581d0 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54  n>=sizeof(mem.zT
581e0 69 74 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65  itle) ) n = size
581f0 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31  of(mem.zTitle)-1
58200 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a  ;.  memcpy(mem.z
58210 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e  Title, zTitle, n
58220 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b  );.  mem.zTitle[
58230 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54  n] = 0;.  mem.nT
58240 69 74 6c 65 20 3d 20 28 6e 2b 33 29 26 7e 33 3b  itle = (n+3)&~3;
58250 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
58260 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
58270 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  );.}../*.** Open
58280 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61   the file indica
58290 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20  ted and write a 
582a0 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65  log of all unfre
582b0 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c  ed memory .** al
582c0 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74  locations into t
582d0 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49  hat log..*/.SQLI
582e0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
582f0 74 65 33 5f 6d 65 6d 64 65 62 75 67 5f 64 75 6d  te3_memdebug_dum
58300 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  p(const char *zF
58310 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45  ilename){.  FILE
58320 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20   *out;.  struct 
58330 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64  MemBlockHdr *pHd
58340 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b  r;.  void **pBt;
58350 0a 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a  .  out = fopen(z
58360 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a  Filename, "w");.
58370 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a    if( out==0 ){.
58380 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
58390 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74  rr, "** Unable t
583a0 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20  o output memory 
583b0 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67  debug output log
583c0 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20  : %s **\n",.    
583d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
583e0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20  zFilename);.    
583f0 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f  return;.  }.  fo
58400 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73  r(pHdr=mem.pFirs
58410 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48  t; pHdr; pHdr=pH
58420 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dr->pNext){.    
58430 63 68 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a  char *z = (char*
58440 29 70 48 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20  )pHdr;.    z -= 
58450 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
58460 53 6c 6f 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69  Slots*sizeof(voi
58470 64 2a 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74  d*) + pHdr->nTit
58480 6c 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  le;.    fprintf(
58490 6f 75 74 2c 20 22 2a 2a 2a 2a 20 25 64 20 62 79  out, "**** %d by
584a0 74 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25  tes at %p from %
584b0 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20  s ****\n", .    
584c0 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53          pHdr->iS
584d0 69 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70  ize, &pHdr[1], p
584e0 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20  Hdr->nTitle ? z 
584f0 3a 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66  : "???");.    if
58500 28 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61  ( pHdr->nBacktra
58510 63 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75  ce ){.      fflu
58520 73 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70  sh(out);.      p
58530 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64  Bt = (void**)pHd
58540 72 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20  r;.      pBt -= 
58550 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
58560 53 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63  Slots;.      bac
58570 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66  ktrace_symbols_f
58580 64 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61  d(pBt, pHdr->nBa
58590 63 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28  cktrace, fileno(
585a0 6f 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72  out));.      fpr
585b0 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
585c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 63 6c  .    }.  }.  fcl
585d0 6f 73 65 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a  ose(out);.}../*.
585e0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
585f0 69 73 20 75 73 65 64 20 74 6f 20 73 69 6d 75 6c  is used to simul
58600 61 74 65 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  ate malloc failu
58610 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72  res..**.** After
58620 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f   calling this ro
58630 75 74 69 6e 65 2c 20 74 68 65 72 65 20 77 69 6c  utine, there wil
58640 6c 20 62 65 20 69 46 61 69 6c 20 73 75 63 63 65  l be iFail succe
58650 73 73 66 75 6c 0a 2a 2a 20 6d 65 6d 6f 72 79 20  ssful.** memory 
58660 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 6e 64 20  allocations and 
58670 74 68 65 6e 20 61 20 66 61 69 6c 75 72 65 2e 20  then a failure. 
58680 20 49 66 20 69 52 65 70 65 61 74 20 69 73 20 31   If iRepeat is 1
58690 0a 2a 2a 20 61 6c 6c 20 73 75 62 73 65 71 75 65  .** all subseque
586a0 6e 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  nt memory alloca
586b0 74 69 6f 6e 73 20 77 69 6c 6c 20 66 61 69 6c 2e  tions will fail.
586c0 20 20 49 66 20 69 52 65 70 65 61 74 20 69 73 0a    If iRepeat is.
586d0 2a 2a 20 30 2c 20 6f 6e 6c 79 20 61 20 73 69 6e  ** 0, only a sin
586e0 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77  gle allocation w
586f0 69 6c 6c 20 66 61 69 6c 2e 20 20 49 66 20 69 52  ill fail.  If iR
58700 65 70 65 61 74 20 69 73 20 6e 65 67 61 74 69 76  epeat is negativ
58710 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 72  e.** then the pr
58720 65 76 69 6f 75 73 20 73 65 74 74 69 6e 67 20 66  evious setting f
58730 6f 72 20 69 52 65 70 65 61 74 20 69 73 20 75 6e  or iRepeat is un
58740 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 45  changed..**.** E
58750 61 63 68 20 63 61 6c 6c 20 74 6f 20 74 68 69 73  ach call to this
58760 20 72 6f 75 74 69 6e 65 20 6f 76 65 72 72 69 64   routine overrid
58770 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e  es the previous.
58780 20 20 54 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20    To disable.** 
58790 74 68 65 20 73 69 6d 75 6c 61 74 65 64 20 61 6c  the simulated al
587a0 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72 65  location failure
587b0 20 6d 65 63 68 61 6e 69 73 6d 2c 20 73 65 74 20   mechanism, set 
587c0 69 46 61 69 6c 20 74 6f 20 2d 31 2e 0a 2a 2a 0a  iFail to -1..**.
587d0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
587e0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
587f0 65 72 20 6f 66 20 73 69 6d 75 6c 61 74 65 64 20  er of simulated 
58800 66 61 69 6c 75 72 65 73 20 74 68 61 74 20 68 61  failures that ha
58810 76 65 0a 2a 2a 20 6f 63 63 75 72 72 65 64 20 73  ve.** occurred s
58820 69 6e 63 65 20 74 68 65 20 70 72 65 76 69 6f 75  ince the previou
58830 73 20 63 61 6c 6c 2e 0a 2a 2f 0a 53 51 4c 49 54  s call..*/.SQLIT
58840 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
58850 33 5f 6d 65 6d 64 65 62 75 67 5f 66 61 69 6c 28  3_memdebug_fail(
58860 69 6e 74 20 69 46 61 69 6c 2c 20 69 6e 74 20 69  int iFail, int i
58870 52 65 70 65 61 74 2c 20 69 6e 74 20 2a 70 69 42  Repeat, int *piB
58880 65 6e 69 67 6e 29 7b 0a 20 20 69 6e 74 20 6e 20  enign){.  int n 
58890 3d 20 6d 65 6d 2e 69 46 61 69 6c 43 6e 74 3b 0a  = mem.iFailCnt;.
588a0 20 20 69 66 28 20 70 69 42 65 6e 69 67 6e 20 29    if( piBenign )
588b0 7b 0a 20 20 20 20 2a 70 69 42 65 6e 69 67 6e 20  {.    *piBenign 
588c0 3d 20 6d 65 6d 2e 69 42 65 6e 69 67 6e 46 61 69  = mem.iBenignFai
588d0 6c 43 6e 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 2e  lCnt;.  }.  mem.
588e0 69 46 61 69 6c 20 3d 20 69 46 61 69 6c 2b 31 3b  iFail = iFail+1;
588f0 0a 20 20 69 66 28 20 69 52 65 70 65 61 74 3e 3d  .  if( iRepeat>=
58900 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 69 52 65  0 ){.    mem.iRe
58910 73 65 74 20 3d 20 69 52 65 70 65 61 74 3b 0a 20  set = iRepeat;. 
58920 20 7d 0a 20 20 6d 65 6d 2e 69 46 61 69 6c 43 6e   }.  mem.iFailCn
58930 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 69 42 65  t = 0;.  mem.iBe
58940 6e 69 67 6e 46 61 69 6c 43 6e 74 20 3d 20 30 3b  nignFailCnt = 0;
58950 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
58960 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
58970 71 6c 69 74 65 33 5f 6d 65 6d 64 65 62 75 67 5f  qlite3_memdebug_
58980 70 65 6e 64 69 6e 67 28 29 7b 0a 20 20 72 65 74  pending(){.  ret
58990 75 72 6e 20 28 6d 65 6d 2e 69 46 61 69 6c 2d 31  urn (mem.iFail-1
589a0 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
589b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
589c0 33 4d 61 6c 6c 6f 63 42 65 6e 69 67 6e 46 61 69  3MallocBenignFai
589d0 6c 75 72 65 28 69 6e 74 20 69 73 42 65 6e 69 67  lure(int isBenig
589e0 6e 29 7b 0a 20 20 69 66 28 20 69 73 42 65 6e 69  n){.  if( isBeni
589f0 67 6e 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 69 4e  gn ){.    mem.iN
58a00 65 78 74 49 73 42 65 6e 69 67 6e 20 3d 20 31 3b  extIsBenign = 1;
58a10 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
58a20 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20  e following two 
58a30 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
58a40 64 20 74 6f 20 61 73 73 65 72 74 20 74 68 61 74  d to assert that
58a50 20 6e 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c   no memory.** al
58a60 6c 6f 63 61 74 69 6f 6e 73 20 6f 63 63 75 72 20  locations occur 
58a70 62 65 74 77 65 65 6e 20 6f 6e 65 20 63 61 6c 6c  between one call
58a80 20 61 6e 64 20 74 68 65 20 6e 65 78 74 2e 20 20   and the next.  
58a90 54 68 65 20 75 73 65 20 6f 66 0a 2a 2a 20 74 68  The use of.** th
58aa0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 65  ese routines doe
58ab0 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  s not change the
58ac0 20 63 6f 6d 70 75 74 65 64 20 72 65 73 75 6c 74   computed result
58ad0 73 20 69 6e 20 61 6e 79 20 77 61 79 2e 0a 2a 2a  s in any way..**
58ae0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
58af0 61 72 65 20 6c 69 6b 65 20 61 73 73 65 72 74 73  are like asserts
58b00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
58b10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
58b20 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 76  MallocDisallow(v
58b30 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20  oid){.  assert( 
58b40 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 29 3b 0a  mem.mutex!=0 );.
58b50 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
58b60 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29  enter(mem.mutex)
58b70 3b 0a 20 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77  ;.  mem.disallow
58b80 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ++;.  sqlite3_mu
58b90 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75  tex_leave(mem.mu
58ba0 74 65 78 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  tex);.}.SQLITE_P
58bb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
58bc0 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 76  te3MallocAllow(v
58bd0 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20  oid){.  assert( 
58be0 6d 65 6d 2e 6d 75 74 65 78 20 29 3b 0a 20 20 73  mem.mutex );.  s
58bf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
58c00 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20  er(mem.mutex);. 
58c10 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73   assert( mem.dis
58c20 61 6c 6c 6f 77 3e 30 20 29 3b 0a 20 20 6d 65 6d  allow>0 );.  mem
58c30 2e 64 69 73 61 6c 6c 6f 77 2d 2d 3b 0a 20 20 73  .disallow--;.  s
58c40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
58c50 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 7d  ve(mem.mutex);.}
58c60 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
58c70 54 45 5f 4d 45 4d 44 45 42 55 47 20 26 26 20 21  TE_MEMDEBUG && !
58c80 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
58c90 52 59 5f 41 4c 4c 4f 43 41 54 49 4f 4e 20 2a 2f  RY_ALLOCATION */
58ca0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
58cb0 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 32 2e 63 20  * End of mem2.c 
58cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58cf0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
58d00 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74  * Begin file mut
58d10 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.c ***********
58d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58d40 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67  /./*.** 2007 Aug
58d50 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 14.**.** The
58d60 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
58d70 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
58d80 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
58d90 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
58da0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
58db0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
58dc0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
58dd0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
58de0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
58df0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
58e00 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
58e10 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
58e20 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
58e30 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
58e40 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
58e50 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
58e60 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
58e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58eb0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
58ec0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
58ed0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
58ee0 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
58ef0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d  es..**.** The im
58f00 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20  plementation in 
58f10 74 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e  this file does n
58f20 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d  ot provide any m
58f30 75 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69  utual.** exclusi
58f40 6f 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73  on and is thus s
58f50 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20  uitable for use 
58f60 6f 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74  only in applicat
58f70 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65  ions.** that use
58f80 20 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e   SQLite in a sin
58f90 67 6c 65 20 74 68 72 65 61 64 2e 20 20 42 75 74  gle thread.  But
58fa0 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   this implementa
58fb0 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 64 6f 20  tion.** does do 
58fc0 61 20 6c 6f 74 20 6f 66 20 65 72 72 6f 72 20 63  a lot of error c
58fd0 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 74 65 78  hecking on mutex
58fe0 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  es to make sure 
58ff0 74 68 65 79 0a 2a 2a 20 61 72 65 20 63 61 6c 6c  they.** are call
59000 65 64 20 63 6f 72 72 65 63 74 6c 79 20 61 6e 64  ed correctly and
59010 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20   at appropriate 
59020 74 69 6d 65 73 2e 20 20 48 65 6e 63 65 2c 20 74  times.  Hence, t
59030 68 69 73 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  his.** implement
59040 61 74 69 6f 6e 20 69 73 20 73 75 69 74 61 62 6c  ation is suitabl
59050 65 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a  e for testing..*
59060 2a 20 64 65 62 75 67 67 69 6e 67 20 70 75 72 70  * debugging purp
59070 6f 73 65 73 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  oses.**.** $Id: 
59080 6d 75 74 65 78 2e 63 2c 76 20 31 2e 31 36 20 32  mutex.c,v 1.16 2
59090 30 30 37 2f 30 39 2f 31 30 20 31 36 3a 31 33 3a  007/09/10 16:13:
590a0 30 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  00 danielk1977 E
590b0 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20  xp $.*/..#ifdef 
590c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
590d0 50 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 49 6e  P_DEBUG./*.** In
590e0 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   this implementa
590f0 74 69 6f 6e 2c 20 6d 75 74 65 78 65 73 20 64 6f  tion, mutexes do
59100 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79   not provide any
59110 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f   mutual exclusio
59120 6e 2e 0a 2a 2a 20 42 75 74 20 74 68 65 20 65 72  n..** But the er
59130 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 73 20  ror checking is 
59140 70 72 6f 76 69 64 65 64 2e 20 20 54 68 69 73 20  provided.  This 
59150 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
59160 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20  s useful.** for 
59170 74 65 73 74 20 70 75 72 70 6f 73 65 73 2e 0a 2a  test purposes..*
59180 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74  /../*.** The mut
59190 65 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72  ex object.*/.str
591a0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  uct sqlite3_mute
591b0 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20  x {.  int id;   
591c0 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74    /* The mutex t
591d0 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  ype */.  int cnt
591e0 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  ;    /* Number o
591f0 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75  f entries withou
59200 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61  t a matching lea
59210 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ve */.};../*.** 
59220 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
59230 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
59240 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
59250 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
59260 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
59270 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72   to it.  If it r
59280 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74  eturns NULL.** t
59290 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
592a0 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
592b0 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a   be allocated. .
592c0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
592d0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c  lite3_mutex *sql
592e0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
592f0 28 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 74  (int id){.  stat
59300 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
59310 20 61 53 74 61 74 69 63 5b 35 5d 3b 0a 20 20 73   aStatic[5];.  s
59320 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e  qlite3_mutex *pN
59330 65 77 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68  ew = 0;.  switch
59340 28 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65  ( id ){.    case
59350 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
59360 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ST:.    case SQL
59370 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
59380 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65  IVE: {.      pNe
59390 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  w = sqlite3_mall
593a0 6f 63 28 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  oc(sizeof(*pNew)
593b0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65  );.      if( pNe
593c0 77 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65  w ){.        pNe
593d0 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20  w->id = id;.    
593e0 20 20 20 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20      pNew->cnt = 
593f0 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
59400 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
59410 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
59420 20 20 20 61 73 73 65 72 74 28 20 69 64 2d 32 20     assert( id-2 
59430 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73  >= 0 );.      as
59440 73 65 72 74 28 20 69 64 2d 32 20 3c 20 73 69 7a  sert( id-2 < siz
59450 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 69 7a  eof(aStatic)/siz
59460 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d 29 20  eof(aStatic[0]) 
59470 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20  );.      pNew = 
59480 26 61 53 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a  &aStatic[id-2];.
59490 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d        pNew->id =
594a0 20 69 64 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   id;.      break
594b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
594c0 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
594d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
594e0 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70   deallocates a p
594f0 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61  reviously alloca
59500 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  ted mutex..*/.SQ
59510 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
59520 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
59530 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
59540 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
59550 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
59560 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  cnt==0 );.  asse
59570 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
59580 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20  E_MUTEX_FAST || 
59590 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
595a0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b  TEX_RECURSIVE );
595b0 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
595c0 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  p);.}../*.** The
595d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
595e0 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74  nter() and sqlit
595f0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72  e3_mutex_try() r
59600 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a  outines attempt.
59610 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75  ** to enter a mu
59620 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72  tex.  If another
59630 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61   thread is alrea
59640 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75  dy within the mu
59650 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  tex,.** sqlite3_
59660 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69  mutex_enter() wi
59670 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c  ll block and sql
59680 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
59690 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20   will return.** 
596a0 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68  SQLITE_BUSY.  Th
596b0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
596c0 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20  try() interface 
596d0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
596e0 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73  K.** upon succes
596f0 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74  sful entry.  Mut
59700 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69  exes created usi
59710 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ng SQLITE_MUTEX_
59720 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a  RECURSIVE can.**
59730 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74   be entered mult
59740 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68  iple times by th
59750 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
59760 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68  In such cases th
59770 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74  e,.** mutex must
59780 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71   be exited an eq
59790 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  ual number of ti
597a0 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68  mes before anoth
597b0 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e  er thread.** can
597c0 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20   enter.  If the 
597d0 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65  same thread trie
597e0 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f  s to enter any o
597f0 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74  ther kind of mut
59800 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20  ex.** more than 
59810 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69  once, the behavi
59820 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  or is undefined.
59830 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
59840 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
59850 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f  x_enter(sqlite3_
59860 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
59870 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65  ert( p );.  asse
59880 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
59890 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
598a0 45 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  E || sqlite3_mut
598b0 65 78 5f 6e 6f 74 68 65 6c 64 28 70 29 20 29 3b  ex_notheld(p) );
598c0 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 53  .  p->cnt++;.}.S
598d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
598e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
598f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
59900 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29  ){.  assert( p )
59910 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
59920 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
59930 52 45 43 55 52 53 49 56 45 20 7c 7c 20 73 71 6c  RECURSIVE || sql
59940 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
59950 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e  ld(p) );.  p->cn
59960 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  t++;.  return SQ
59970 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
59980 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
59990 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74  tex_leave() rout
599a0 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65  ine exits a mute
599b0 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72  x that was.** pr
599c0 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64  eviously entered
599d0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72   by the same thr
599e0 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69  ead.  The behavi
599f0 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e  or.** is undefin
59a00 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20  ed if the mutex 
59a10 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
59a20 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69   entered or.** i
59a30 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
59a40 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69  allocated.  SQLi
59a50 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f  te will never do
59a60 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49   either..*/.SQLI
59a70 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
59a80 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
59a90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
59aa0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29  ){.  assert( p )
59ab0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
59ac0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
59ad0 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2d 2d 3b  ) );.  p->cnt--;
59ae0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
59af0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
59b00 45 43 55 52 53 49 56 45 20 7c 7c 20 73 71 6c 69  ECURSIVE || sqli
59b10 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
59b20 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  d(p) );.}../*.**
59b30 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
59b40 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71  ex_held() and sq
59b50 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
59b60 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72  eld() routine ar
59b70 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f  e.** intended fo
59b80 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 73  r use inside ass
59b90 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
59ba0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
59bb0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  int sqlite3_mute
59bc0 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  x_held(sqlite3_m
59bd0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
59be0 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e  rn p==0 || p->cn
59bf0 74 3e 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  t>0;.}.SQLITE_AP
59c00 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75  I int sqlite3_mu
59c10 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69  tex_notheld(sqli
59c20 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
59c30 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
59c40 70 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 23 65 6e  p->cnt==0;.}.#en
59c50 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55  dif /* SQLITE_MU
59c60 54 45 58 5f 4e 4f 4f 50 5f 44 45 42 55 47 20 2a  TEX_NOOP_DEBUG *
59c70 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
59c80 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e  ** End of mutex.
59c90 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
59ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59cc0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
59cd0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75  ** Begin file mu
59ce0 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a  tex_os2.c ******
59cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59d10 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75  */./*.** 2007 Au
59d20 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68  gust 28.**.** Th
59d30 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
59d40 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
59d50 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
59d60 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
59d70 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
59d80 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
59d90 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
59da0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
59db0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
59dc0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
59dd0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
59de0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
59df0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
59e00 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
59e10 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
59e20 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
59e30 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
59e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59e80 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
59e90 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
59ea0 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61   C functions tha
59eb0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65  t implement mute
59ec0 78 65 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a  xes for OS/2.**.
59ed0 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73  ** $Id: mutex_os
59ee0 32 2e 63 2c 76 20 31 2e 31 20 32 30 30 37 2f 30  2.c,v 1.1 2007/0
59ef0 38 2f 32 38 20 31 36 3a 33 34 3a 34 33 20 64 72  8/28 16:34:43 dr
59f00 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a  h Exp $.*/.../*.
59f10 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
59f20 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
59f30 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f   used if SQLITE_
59f40 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66  MUTEX_OS2 is def
59f50 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65  ined..** See the
59f60 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f   mutex.h file fo
59f70 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69  r details..*/.#i
59f80 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
59f90 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 20 46 49 58  X_OS2../**** FIX
59fa0 20 4d 45 3a 0a 2a 2a 2a 2a 2a 20 54 68 69 73 20   ME:.***** This 
59fb0 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 20 6e  is currently a n
59fc0 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74  o-op implementat
59fd0 69 6f 6e 20 73 75 69 74 61 62 6c 65 20 66 6f 72  ion suitable for
59fe0 20 75 73 65 0a 2a 2a 2a 2a 2a 20 69 6e 20 73 69   use.***** in si
59ff0 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70  ngle-threaded ap
5a000 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 6c 79 2e  plications only.
5a010 20 20 53 6f 6d 65 62 6f 64 79 20 70 6c 65 61 73    Somebody pleas
5a020 65 20 72 65 70 6c 61 63 65 0a 2a 2a 2a 2a 2a 20  e replace.***** 
5a030 74 68 69 73 20 77 69 74 68 20 61 20 72 65 61 6c  this with a real
5a040 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
5a050 61 74 69 6f 6e 20 66 6f 72 20 4f 53 2f 32 2e 0a  ation for OS/2..
5a060 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ****/../*.** The
5a070 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2f   mutex object.*/
5a080 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
5a090 6d 75 74 65 78 20 7b 0a 20 20 69 6e 74 20 69 64  mutex {.  int id
5a0a0 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 75 74  ;     /* The mut
5a0b0 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74  ex type */.  int
5a0c0 20 63 6e 74 3b 20 20 20 20 2f 2a 20 4e 75 6d 62   cnt;    /* Numb
5a0d0 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69  er of entries wi
5a0e0 74 68 6f 75 74 20 61 20 6d 61 74 63 68 69 6e 67  thout a matching
5a0f0 20 6c 65 61 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   leave */.};../*
5a100 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
5a110 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f  mutex_alloc() ro
5a120 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20  utine allocates 
5a130 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61  a new.** mutex a
5a140 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
5a150 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20  nter to it.  If 
5a160 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a  it returns NULL.
5a170 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  ** that means th
5a180 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64  at a mutex could
5a190 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65   not be allocate
5a1a0 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  d. .*/.SQLITE_AP
5a1b0 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
5a1c0 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
5a1d0 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20  lloc(int id){.  
5a1e0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
5a1f0 75 74 65 78 20 61 53 74 61 74 69 63 5b 34 5d 3b  utex aStatic[4];
5a200 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
5a210 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 73 77   *pNew = 0;.  sw
5a220 69 74 63 68 28 20 69 64 20 29 7b 0a 20 20 20 20  itch( id ){.    
5a230 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  case SQLITE_MUTE
5a240 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65  X_FAST:.    case
5a250 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
5a260 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20  CURSIVE: {.     
5a270 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f   pNew = sqlite3_
5a280 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70  malloc(sizeof(*p
5a290 4e 65 77 29 29 3b 0a 20 20 20 20 20 20 69 66 28  New));.      if(
5a2a0 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20   pNew ){.       
5a2b0 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b 0a   pNew->id = id;.
5a2c0 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 63 6e          pNew->cn
5a2d0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  t = 0;.      }. 
5a2e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
5a2f0 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
5a300 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
5a310 64 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20  d-2 >= 0 );.    
5a320 20 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 3c    assert( id-2 <
5a330 20 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29   sizeof(aStatic)
5a340 2f 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b  /sizeof(aStatic[
5a350 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65  0]) );.      pNe
5a360 77 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d  w = &aStatic[id-
5a370 32 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  2];.      pNew->
5a380 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62  id = id;.      b
5a390 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
5a3a0 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
5a3b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
5a3c0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73  tine deallocates
5a3d0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c   a previously al
5a3e0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a  located mutex..*
5a3f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
5a400 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
5a410 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  free(sqlite3_mut
5a420 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
5a430 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ( p );.  assert(
5a440 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20   p->cnt==0 );.  
5a450 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
5a460 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
5a470 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54   || p->id==SQLIT
5a480 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
5a490 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  E );.  sqlite3_f
5a4a0 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(p);.}../*.**
5a4b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
5a4c0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73  ex_enter() and s
5a4d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
5a4e0 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65  () routines atte
5a4f0 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  mpt.** to enter 
5a500 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f  a mutex.  If ano
5a510 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61  ther thread is a
5a520 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68  lready within th
5a530 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69  e mutex,.** sqli
5a540 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
5a550 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64  ) will block and
5a560 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
5a570 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e  ry() will return
5a580 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  .** SQLITE_BUSY.
5a590 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75    The sqlite3_mu
5a5a0 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66  tex_try() interf
5a5b0 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ace returns SQLI
5a5c0 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75  TE_OK.** upon su
5a5d0 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20  ccessful entry. 
5a5e0 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64   Mutexes created
5a5f0 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55   using SQLITE_MU
5a600 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
5a610 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20  n.** be entered 
5a620 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62  multiple times b
5a630 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  y the same threa
5a640 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65  d.  In such case
5a650 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20  s the,.** mutex 
5a660 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61  must be exited a
5a670 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f  n equal number o
5a680 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61  f times before a
5a690 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a  nother thread.**
5a6a0 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20   can enter.  If 
5a6b0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
5a6c0 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61  tries to enter a
5a6d0 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66  ny other kind of
5a6e0 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74   mutex.** more t
5a6f0 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65  han once, the be
5a700 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
5a710 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ned..*/.SQLITE_A
5a720 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
5a730 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69  mutex_enter(sqli
5a740 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
5a750 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20   assert( p );.  
5a760 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
5a770 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
5a780 52 53 49 56 45 20 7c 7c 20 73 71 6c 69 74 65 33  RSIVE || sqlite3
5a790 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70  _mutex_notheld(p
5a7a0 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b  ) );.  p->cnt++;
5a7b0 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
5a7c0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
5a7d0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  try(sqlite3_mute
5a7e0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
5a7f0 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20   p );.  assert( 
5a800 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
5a810 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
5a820 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
5a830 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70  otheld(p) );.  p
5a840 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 75 72  ->cnt++;.  retur
5a850 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
5a860 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
5a870 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20  3_mutex_leave() 
5a880 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20  routine exits a 
5a890 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a  mutex that was.*
5a8a0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74  * previously ent
5a8b0 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65  ered by the same
5a8c0 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65   thread.  The be
5a8d0 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64  havior.** is und
5a8e0 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75  efined if the mu
5a8f0 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65  tex is not curre
5a900 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a  ntly entered or.
5a910 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  ** is not curren
5a920 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20  tly allocated.  
5a930 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65  SQLite will neve
5a940 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a  r do either..*/.
5a950 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
5a960 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
5a970 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
5a980 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
5a990 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20   p );.  assert( 
5a9a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
5a9b0 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e  ld(p) );.  p->cn
5a9c0 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t--;.  assert( p
5a9d0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
5a9e0 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20  EX_RECURSIVE || 
5a9f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
5aa00 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f  theld(p) );.}../
5aa10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
5aa20 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e  _mutex_held() an
5aa30 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
5aa40 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e  notheld() routin
5aa50 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65  e are.** intende
5aa60 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
5aa70 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
5aa80 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
5aa90 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
5aaa0 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
5aab0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
5aac0 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
5aad0 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 53 51 4c 49 54  ->cnt>0;.}.SQLIT
5aae0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
5aaf0 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
5ab00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
5ab10 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  ){.  return p==0
5ab20 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d   || p->cnt==0;.}
5ab30 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
5ab40 45 5f 4d 55 54 45 58 5f 4f 53 32 20 2a 2f 0a 0a  E_MUTEX_OS2 */..
5ab50 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
5ab60 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6f 73 32  End of mutex_os2
5ab70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
5ab80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ab90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
5aba0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
5abb0 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78  Begin file mutex
5abc0 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _unix.c ********
5abd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5abe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
5abf0 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
5ac00 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 28.**.** The a
5ac10 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
5ac20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
5ac30 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
5ac40 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
5ac50 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
5ac60 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
5ac70 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
5ac80 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
5ac90 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
5aca0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
5acb0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
5acc0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
5acd0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
5ace0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
5acf0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
5ad00 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
5ad10 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
5ad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ad30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ad40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ad50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ad60 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
5ad70 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
5ad80 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
5ad90 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73  mplement mutexes
5ada0 20 66 6f 72 20 70 74 68 72 65 61 64 73 0a 2a 2a   for pthreads.**
5adb0 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 75  .** $Id: mutex_u
5adc0 6e 69 78 2e 63 2c 76 20 31 2e 32 20 32 30 30 37  nix.c,v 1.2 2007
5add0 2f 30 38 2f 32 38 20 32 32 3a 32 34 3a 33 35 20  /08/28 22:24:35 
5ade0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
5adf0 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
5ae00 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
5ae10 79 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65  y used if we are
5ae20 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 72 65 61   compiling threa
5ae30 64 73 61 66 65 0a 2a 2a 20 75 6e 64 65 72 20 75  dsafe.** under u
5ae40 6e 69 78 20 77 69 74 68 20 70 74 68 72 65 61 64  nix with pthread
5ae50 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  s..**.** Note th
5ae60 61 74 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e  at this implemen
5ae70 74 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20  tation requires 
5ae80 61 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 74 68  a version of pth
5ae90 72 65 61 64 73 20 74 68 61 74 0a 2a 2a 20 73 75  reads that.** su
5aea0 70 70 6f 72 74 73 20 72 65 63 75 72 73 69 76 65  pports recursive
5aeb0 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 69 66   mutexes..*/.#if
5aec0 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
5aed0 5f 50 54 48 52 45 41 44 53 0a 0a 23 69 6e 63 6c  _PTHREADS..#incl
5aee0 75 64 65 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a  ude <pthread.h>.
5aef0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75  ./*.** Each recu
5af00 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61  rsive mutex is a
5af10 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
5af20 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
5af30 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  cture..*/.struct
5af40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b   sqlite3_mutex {
5af50 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  .  pthread_mutex
5af60 5f 74 20 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a  _t mutex;     /*
5af70 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69   Mutex controlli
5af80 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20  ng the lock */. 
5af90 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20   int id;        
5afa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
5afb0 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69  utex type */.  i
5afc0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
5afd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
5afe0 62 65 72 20 6f 66 20 65 6e 74 72 61 6e 63 65 73  ber of entrances
5aff0 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 74 20   */.  pthread_t 
5b000 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20  owner;          
5b010 20 2f 2a 20 54 68 72 65 61 64 20 74 68 61 74 20   /* Thread that 
5b020 69 73 20 77 69 74 68 69 6e 20 74 68 69 73 20 6d  is within this m
5b030 75 74 65 78 20 2a 2f 0a 23 69 66 64 65 66 20 53  utex */.#ifdef S
5b040 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 6e  QLITE_DEBUG.  in
5b050 74 20 74 72 61 63 65 3b 20 20 20 20 20 20 20 20  t trace;        
5b060 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
5b070 20 74 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65   to trace change
5b080 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  s */.#endif.};..
5b090 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
5b0a0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
5b0b0 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
5b0c0 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78  s a new.** mutex
5b0d0 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70   and returns a p
5b0e0 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49  ointer to it.  I
5b0f0 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c  f it returns NUL
5b100 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20  L.** that means 
5b110 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75  that a mutex cou
5b120 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61  ld not be alloca
5b130 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20  ted.  SQLite.** 
5b140 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20  will unwind its 
5b150 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e  stack and return
5b160 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20   an error.  The 
5b170 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73  argument.** to s
5b180 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
5b190 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74  oc() is one of t
5b1a0 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
5b1b0 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75  stants:.**.** <u
5b1c0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  l>.** <li>  SQLI
5b1d0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a  TE_MUTEX_FAST.**
5b1e0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
5b1f0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
5b200 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
5b210 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
5b220 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  R.** <li>  SQLIT
5b230 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
5b240 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  EM.** <li>  SQLI
5b250 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
5b260 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  MEM2.** <li>  SQ
5b270 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
5b280 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20  C_PRNG.** <li>  
5b290 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
5b2a0 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 2f 75 6c 3e  TIC_LRU.** </ul>
5b2b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
5b2c0 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63   two constants c
5b2d0 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  ause sqlite3_mut
5b2e0 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72  ex_alloc() to cr
5b2f0 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75  eate.** a new mu
5b300 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75  tex.  The new mu
5b310 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65  tex is recursive
5b320 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
5b330 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
5b340 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20  is used but not 
5b350 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77  necessarily so w
5b360 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
5b370 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a  _FAST is used..*
5b380 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c  * The mutex impl
5b390 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20  ementation does 
5b3a0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65  not need to make
5b3b0 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a   a distinction.*
5b3c0 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45  * between SQLITE
5b3d0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
5b3e0 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45   and SQLITE_MUTE
5b3f0 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65  X_FAST if it doe
5b400 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f  s.** not want to
5b410 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69  .  But SQLite wi
5b420 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20  ll only request 
5b430 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
5b440 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68  x in.** cases wh
5b450 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65  ere it really ne
5b460 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66  eds one.  If a f
5b470 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73  aster non-recurs
5b480 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70  ive mutex.** imp
5b490 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
5b4a0 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
5b4b0 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74  host platform, t
5b4c0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
5b4d0 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75  em.** might retu
5b4e0 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20  rn such a mutex 
5b4f0 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53  in response to S
5b500 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
5b510 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65  ..**.** The othe
5b520 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65  r allowed parame
5b530 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  ters to sqlite3_
5b540 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61  mutex_alloc() ea
5b550 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  ch return.** a p
5b560 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
5b570 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d  ic preexisting m
5b580 75 74 65 78 2e 20 20 54 68 72 65 65 20 73 74 61  utex.  Three sta
5b590 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a  tic mutexes are.
5b5a0 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63  ** used by the c
5b5b0 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f  urrent version o
5b5c0 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72  f SQLite.  Futur
5b5d0 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
5b5e0 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20  Lite.** may add 
5b5f0 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
5b600 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74  c mutexes.  Stat
5b610 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66  ic mutexes are f
5b620 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
5b630 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c  se by SQLite onl
5b640 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  y.  Applications
5b650 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
5b660 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a   mutexes should.
5b670 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20  ** use only the 
5b680 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20  dynamic mutexes 
5b690 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49  returned by SQLI
5b6a0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72  TE_MUTEX_FAST or
5b6b0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
5b6c0 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a  _RECURSIVE..**.*
5b6d0 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f  * Note that if o
5b6e0 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69  ne of the dynami
5b6f0 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65  c mutex paramete
5b700 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  rs (SQLITE_MUTEX
5b710 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49  _FAST.** or SQLI
5b720 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
5b730 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e  VE) is used then
5b740 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
5b750 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e  lloc().** return
5b760 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75  s a different mu
5b770 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  tex on every cal
5b780 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20  l.  But for the 
5b790 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78  static .** mutex
5b7a0 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65   types, the same
5b7b0 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e   mutex is return
5b7c0 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ed on every call
5b7d0 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65   that has.** the
5b7e0 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65   same type numbe
5b7f0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
5b800 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
5b810 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
5b820 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a  loc(int iType){.
5b830 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
5b840 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74  _mutex staticMut
5b850 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b  exes[] = {.    {
5b860 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49   PTHREAD_MUTEX_I
5b870 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20  NITIALIZER, },. 
5b880 20 20 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54     { PTHREAD_MUT
5b890 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20  EX_INITIALIZER, 
5b8a0 7d 2c 0a 20 20 20 20 7b 20 50 54 48 52 45 41 44  },.    { PTHREAD
5b8b0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
5b8c0 45 52 2c 20 7d 2c 0a 20 20 20 20 7b 20 50 54 48  ER, },.    { PTH
5b8d0 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49  READ_MUTEX_INITI
5b8e0 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 7b  ALIZER, },.    {
5b8f0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49   PTHREAD_MUTEX_I
5b900 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20  NITIALIZER, },. 
5b910 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   };.  sqlite3_mu
5b920 74 65 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68  tex *p;.  switch
5b930 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63  ( iType ){.    c
5b940 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
5b950 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
5b960 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
5b970 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
5b980 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
5b990 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
5b9a0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74  pthread_mutexatt
5b9b0 72 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74  r_t recursiveAtt
5b9c0 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  r;.        pthre
5b9d0 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69  ad_mutexattr_ini
5b9e0 74 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72  t(&recursiveAttr
5b9f0 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  );.        pthre
5ba00 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74  ad_mutexattr_set
5ba10 74 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41  type(&recursiveA
5ba20 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54  ttr, PTHREAD_MUT
5ba30 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20  EX_RECURSIVE);. 
5ba40 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
5ba50 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75  utex_init(&p->mu
5ba60 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41  tex, &recursiveA
5ba70 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74  ttr);.        pt
5ba80 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f  hread_mutexattr_
5ba90 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69  destroy(&recursi
5baa0 76 65 41 74 74 72 29 3b 0a 20 20 20 20 20 20 20  veAttr);.       
5bab0 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
5bac0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
5bad0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
5bae0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
5baf0 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 70  _FAST: {.      p
5bb00 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
5bb10 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29  Zero( sizeof(*p)
5bb20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20   );.      if( p 
5bb30 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64  ){.        p->id
5bb40 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
5bb50 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
5bb60 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
5bb70 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
5bb80 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
5bb90 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
5bba0 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
5bbb0 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20  e-2 >= 0 );.    
5bbc0 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d    assert( iType-
5bbd0 32 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 69  2 < sizeof(stati
5bbe0 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66  cMutexes)/sizeof
5bbf0 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30  (staticMutexes[0
5bc00 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20  ]) );.      p = 
5bc10 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69  &staticMutexes[i
5bc20 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70  Type-2];.      p
5bc30 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20  ->id = iType;.  
5bc40 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
5bc50 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
5bc60 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
5bc70 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
5bc80 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
5bc90 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75  .** allocated mu
5bca0 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20  tex.  SQLite is 
5bcb0 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c  careful to deall
5bcc0 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d  ocate every.** m
5bcd0 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c  utex that it all
5bce0 6f 63 61 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ocates..*/.SQLIT
5bcf0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
5bd00 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71  e3_mutex_free(sq
5bd10 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
5bd20 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a  .  assert( p );.
5bd30 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
5bd40 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  f==0 );.  assert
5bd50 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
5bd60 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d  MUTEX_FAST || p-
5bd70 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
5bd80 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20  X_RECURSIVE );. 
5bd90 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 64   pthread_mutex_d
5bda0 65 73 74 72 6f 79 28 26 70 2d 3e 6d 75 74 65 78  estroy(&p->mutex
5bdb0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
5bdc0 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  e(p);.}../*.** T
5bdd0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
5bde0 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c  _enter() and sql
5bdf0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
5be00 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70   routines attemp
5be10 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20  t.** to enter a 
5be20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68  mutex.  If anoth
5be30 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72  er thread is alr
5be40 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20  eady within the 
5be50 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65  mutex,.** sqlite
5be60 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20  3_mutex_enter() 
5be70 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73  will block and s
5be80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
5be90 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  () will return.*
5bea0 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  * SQLITE_BUSY.  
5beb0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
5bec0 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63  x_try() interfac
5bed0 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
5bee0 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63  _OK.** upon succ
5bef0 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d  essful entry.  M
5bf00 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75  utexes created u
5bf10 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45  sing SQLITE_MUTE
5bf20 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a  X_RECURSIVE can.
5bf30 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75  ** be entered mu
5bf40 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20  ltiple times by 
5bf50 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
5bf60 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20    In such cases 
5bf70 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75  the,.** mutex mu
5bf80 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20  st be exited an 
5bf90 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  equal number of 
5bfa0 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f  times before ano
5bfb0 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63  ther thread.** c
5bfc0 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68  an enter.  If th
5bfd0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72  e same thread tr
5bfe0 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79  ies to enter any
5bff0 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d   other kind of m
5c000 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61  utex.** more tha
5c010 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61  n once, the beha
5c020 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
5c030 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
5c040 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
5c050 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
5c060 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
5c070 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73  ssert( p );.  as
5c080 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
5c090 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
5c0a0 49 56 45 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  IVE || sqlite3_m
5c0b0 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 29 20  utex_notheld(p) 
5c0c0 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74  );.  pthread_mut
5c0d0 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  ex_lock(&p->mute
5c0e0 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d  x);.  p->owner =
5c0f0 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b   pthread_self();
5c100 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69  .  p->nRef++;.#i
5c110 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
5c120 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65  G.  if( p->trace
5c130 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22   ){.    printf("
5c140 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28  enter mutex %p (
5c150 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64  %d) with nRef=%d
5c160 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65  \n", p, p->trace
5c170 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a  , p->nRef);.  }.
5c180 23 65 6e 64 69 66 0a 7d 0a 53 51 4c 49 54 45 5f  #endif.}.SQLITE_
5c190 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
5c1a0 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65  mutex_try(sqlite
5c1b0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
5c1c0 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
5c1d0 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20   p );.  assert( 
5c1e0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
5c1f0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
5c200 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
5c210 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 69  otheld(p) );.  i
5c220 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  f( pthread_mutex
5c230 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  _trylock(&p->mut
5c240 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d  ex)==0 ){.    p-
5c250 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64  >owner = pthread
5c260 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e  _self();.    p->
5c270 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d  nRef++;.    rc =
5c280 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64   SQLITE_OK;.#ifd
5c290 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
5c2a0 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65      if( p->trace
5c2b0 20 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 66   ){.      printf
5c2c0 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70  ("enter mutex %p
5c2d0 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d   (%d) with nRef=
5c2e0 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61  %d\n", p, p->tra
5c2f0 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20  ce, p->nRef);.  
5c300 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c    }.#endif.  }el
5c310 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  se{.    rc = SQL
5c320 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
5c330 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
5c340 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
5c350 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f  mutex_leave() ro
5c360 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75  utine exits a mu
5c370 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20  tex that was.** 
5c380 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
5c390 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
5c3a0 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61  hread.  The beha
5c3b0 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66  vior.** is undef
5c3c0 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
5c3d0 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  x is not current
5c3e0 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a  ly entered or.**
5c3f0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
5c400 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51  y allocated.  SQ
5c410 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
5c420 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 53 51  do either..*/.SQ
5c430 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
5c440 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
5c450 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
5c460 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
5c470 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
5c480 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
5c490 28 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66  (p) );.  p->nRef
5c4a0 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  --;.  assert( p-
5c4b0 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69  >nRef==0 || p->i
5c4c0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
5c4d0 52 45 43 55 52 53 49 56 45 20 29 3b 0a 23 69 66  RECURSIVE );.#if
5c4e0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
5c4f0 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20  .  if( p->trace 
5c500 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 6c  ){.    printf("l
5c510 65 61 76 65 20 6d 75 74 65 78 20 25 70 20 28 25  eave mutex %p (%
5c520 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c  d) with nRef=%d\
5c530 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c  n", p, p->trace,
5c540 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23   p->nRef);.  }.#
5c550 65 6e 64 69 66 0a 20 20 70 74 68 72 65 61 64 5f  endif.  pthread_
5c560 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d  mutex_unlock(&p-
5c570 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  >mutex);.}../*.*
5c580 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
5c590 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73  tex_held() and s
5c5a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
5c5b0 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61  held() routine a
5c5c0 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66  re.** intended f
5c5d0 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69  or use only insi
5c5e0 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
5c5f0 65 6d 65 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65  ements.  On some
5c600 20 70 6c 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74   platforms,.** t
5c610 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 72 61  here might be ra
5c620 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68  ce conditions th
5c630 61 74 20 63 61 6e 20 63 61 75 73 65 20 74 68 65  at can cause the
5c640 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a  se routines to.*
5c650 2a 20 64 65 6c 69 76 65 72 20 69 6e 63 6f 72 72  * deliver incorr
5c660 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 49 6e  ect results.  In
5c670 20 70 61 72 74 69 63 75 6c 61 72 2c 20 69 66 20   particular, if 
5c680 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20  pthread_equal() 
5c690 69 73 0a 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f  is.** not an ato
5c6a0 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74  mic operation, t
5c6b0 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e  hen these routin
5c6c0 65 73 20 6d 69 67 68 74 20 64 65 6c 69 76 65 72  es might deliver
5c6d0 79 0a 2a 2a 20 69 6e 63 6f 72 72 65 63 74 20 72  y.** incorrect r
5c6e0 65 73 75 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74  esults.  On most
5c6f0 20 70 6c 61 74 66 6f 72 6d 73 2c 20 70 74 68 72   platforms, pthr
5c700 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 61  ead_equal() is a
5c710 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20   .** comparison 
5c720 6f 66 20 74 77 6f 20 69 6e 74 65 67 65 72 73 20  of two integers 
5c730 61 6e 64 20 69 73 20 74 68 65 72 65 66 6f 72 65  and is therefore
5c740 20 61 74 6f 6d 69 63 2e 20 20 42 75 74 20 77 65   atomic.  But we
5c750 20 61 72 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61   are.** told tha
5c760 74 20 48 50 55 58 20 69 73 20 6e 6f 74 20 73 75  t HPUX is not su
5c770 63 68 20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20  ch a platform.  
5c780 49 66 20 73 6f 2c 20 74 68 65 6e 20 74 68 65 73  If so, then thes
5c790 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69  e routines.** wi
5c7a0 6c 6c 20 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f  ll not always wo
5c7b0 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20  rk correctly on 
5c7c0 48 50 55 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74  HPUX..**.** On t
5c7d0 68 6f 73 65 20 70 6c 61 74 66 6f 72 6d 73 20 77  hose platforms w
5c7e0 68 65 72 65 20 70 74 68 72 65 61 64 5f 65 71 75  here pthread_equ
5c7f0 61 6c 28 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d  al() is not atom
5c800 69 63 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68  ic, SQLite.** sh
5c810 6f 75 6c 64 20 62 65 20 63 6f 6d 70 69 6c 65 64  ould be compiled
5c820 20 77 69 74 68 6f 75 74 20 2d 44 53 51 4c 49 54   without -DSQLIT
5c830 45 5f 44 45 42 55 47 20 61 6e 64 20 77 69 74 68  E_DEBUG and with
5c840 20 2d 44 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20   -DNDEBUG to.** 
5c850 6d 61 6b 65 20 73 75 72 65 20 6e 6f 20 61 73 73  make sure no ass
5c860 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
5c870 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 61   are evaluated a
5c880 6e 64 20 68 65 6e 63 65 20 74 68 65 73 65 0a 2a  nd hence these.*
5c890 2a 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6e  * routines are n
5c8a0 65 76 65 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  ever called..*/.
5c8b0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
5c8c0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
5c8d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
5c8e0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
5c8f0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  p){.  return p==
5c900 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21 3d 30  0 || (p->nRef!=0
5c910 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61   && pthread_equa
5c920 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72  l(p->owner, pthr
5c930 65 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a  ead_self()));.}.
5c940 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
5c950 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
5c960 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
5c970 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ex *p){.  return
5c980 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66   p==0 || p->nRef
5c990 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65  ==0 || pthread_e
5c9a0 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70  qual(p->owner, p
5c9b0 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d  thread_self())==
5c9c0 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64  0;.}.#endif.#end
5c9d0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
5c9e0 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f  EX_PTHREAD */../
5c9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
5ca00 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78  nd of mutex_unix
5ca10 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
5ca20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ca30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
5ca40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
5ca50 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
5ca60 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  w32.c **********
5ca70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ca80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
5ca90 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74  *.** 2007 August
5caa0 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   14.**.** The au
5cab0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
5cac0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
5cad0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
5cae0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
5caf0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
5cb00 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
5cb10 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
5cb20 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
5cb30 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
5cb40 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
5cb50 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
5cb60 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
5cb70 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
5cb80 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
5cb90 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
5cba0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
5cbb0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
5cbc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5cbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5cbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5cbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5cc00 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
5cc10 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
5cc20 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
5cc30 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20  plement mutexes 
5cc40 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20  for win32.**.** 
5cc50 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e 63  $Id: mutex_w32.c
5cc60 2c 76 20 31 2e 34 20 32 30 30 37 2f 30 39 2f 30  ,v 1.4 2007/09/0
5cc70 35 20 31 34 3a 33 30 3a 34 32 20 64 72 68 20 45  5 14:30:42 drh E
5cc80 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
5cc90 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
5cca0 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65  file is only use
5ccb0 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70  d if we are comp
5ccc0 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72 65 61  iling multithrea
5ccd0 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33  ded.** on a win3
5cce0 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66  2 system..*/.#if
5ccf0 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
5cd00 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  _W32../*.** Each
5cd10 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
5cd20 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
5cd30 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
5cd40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
5cd50 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75  truct sqlite3_mu
5cd60 74 65 78 20 7b 0a 20 20 43 52 49 54 49 43 41 4c  tex {.  CRITICAL
5cd70 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20  _SECTION mutex; 
5cd80 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74     /* Mutex cont
5cd90 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b  rolling the lock
5cda0 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 20 20   */.  int id;   
5cdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cdc0 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a   /* Mutex type *
5cdd0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  /.  int nRef;   
5cde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5cdf0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 65  * Number of ente
5ce00 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52  rances */.  DWOR
5ce10 44 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 20  D owner;        
5ce20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64         /* Thread
5ce30 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75   holding this mu
5ce40 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  tex */.};../*.**
5ce50 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f   Return true (no
5ce60 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72  n-zero) if we ar
5ce70 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20  e running under 
5ce80 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69  WinNT, Win2K, Wi
5ce90 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45  nXP,.** or WinCE
5cea0 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20  .  Return false 
5ceb0 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35  (zero) for Win95
5cec0 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d  , Win98, or WinM
5ced0 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73  E..**.** Here is
5cee0 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20   an interesting 
5cef0 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69  observation:  Wi
5cf00 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20  n95, Win98, and 
5cf10 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68  WinME lack.** th
5cf20 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41  e LockFileEx() A
5cf30 50 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20  PI.  But we can 
5cf40 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79  still statically
5cf50 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68   link against th
5cf60 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e  at.** API as lon
5cf70 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61  g as we don't ca
5cf80 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e  ll it win runnin
5cf90 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20  g Win95/98/ME.  
5cfa0 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69  A call to.** thi
5cfb0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
5cfc0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  d to determine i
5cfd0 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69  f the host is Wi
5cfe0 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20  n95/98/ME or.** 
5cff0 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74  WinNT/2K/XP so t
5d000 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77  hat we will know
5d010 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
5d020 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61  we can safely ca
5d030 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69  ll.** the LockFi
5d040 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23  leEx() API..*/.#
5d050 69 66 20 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65  if OS_WINCE.# de
5d060 66 69 6e 65 20 6d 75 74 65 78 49 73 4e 54 28 29  fine mutexIsNT()
5d070 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 73 74    (1).#else.  st
5d080 61 74 69 63 20 69 6e 74 20 6d 75 74 65 78 49 73  atic int mutexIs
5d090 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 73 74  NT(void){.    st
5d0a0 61 74 69 63 20 69 6e 74 20 6f 73 54 79 70 65 20  atic int osType 
5d0b0 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6f 73 54  = 0;.    if( osT
5d0c0 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ype==0 ){.      
5d0d0 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49  OSVERSIONINFO sI
5d0e0 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f  nfo;.      sInfo
5d0f0 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f  .dwOSVersionInfo
5d100 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49  Size = sizeof(sI
5d110 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56  nfo);.      GetV
5d120 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29  ersionEx(&sInfo)
5d130 3b 0a 20 20 20 20 20 20 6f 73 54 79 70 65 20 3d  ;.      osType =
5d140 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72   sInfo.dwPlatfor
5d150 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52  mId==VER_PLATFOR
5d160 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a  M_WIN32_NT ? 2 :
5d170 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65   1;.    }.    re
5d180 74 75 72 6e 20 6f 73 54 79 70 65 3d 3d 32 3b 0a  turn osType==2;.
5d190 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53    }.#endif /* OS
5d1a0 5f 57 49 4e 43 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  _WINCE */.../*.*
5d1b0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
5d1c0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74  tex_alloc() rout
5d1d0 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ine allocates a 
5d1e0 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64  new.** mutex and
5d1f0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
5d200 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74  er to it.  If it
5d210 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a   returns NULL.**
5d220 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
5d230 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e   a mutex could n
5d240 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e  ot be allocated.
5d250 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c    SQLite.** will
5d260 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63   unwind its stac
5d270 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20  k and return an 
5d280 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75  error.  The argu
5d290 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  ment.** to sqlit
5d2a0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
5d2b0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65   is one of these
5d2c0 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
5d2d0 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  ts:.**.** <ul>.*
5d2e0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
5d2f0 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20  UTEX_FAST       
5d300 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69          0.** <li
5d310 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
5d320 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20  RECURSIVE       
5d330 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51     1.** <li>  SQ
5d340 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
5d350 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a  C_MASTER      2.
5d360 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
5d370 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
5d380 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c           3.** <l
5d390 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
5d3a0 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20  _STATIC_PRNG    
5d3b0 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a      4.** </ul>.*
5d3c0 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
5d3d0 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75  wo constants cau
5d3e0 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  se sqlite3_mutex
5d3f0 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61  _alloc() to crea
5d400 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65  te.** a new mute
5d410 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65  x.  The new mute
5d420 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77  x is recursive w
5d430 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
5d440 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73  _RECURSIVE.** is
5d450 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65   used but not ne
5d460 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65  cessarily so whe
5d470 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  n SQLITE_MUTEX_F
5d480 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20  AST is used..** 
5d490 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  The mutex implem
5d4a0 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  entation does no
5d4b0 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61  t need to make a
5d4c0 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20   distinction.** 
5d4d0 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d  between SQLITE_M
5d4e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61  UTEX_RECURSIVE a
5d4f0 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  nd SQLITE_MUTEX_
5d500 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a  FAST if it does.
5d510 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20  ** not want to. 
5d520 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c   But SQLite will
5d530 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20   only request a 
5d540 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
5d550 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72  in.** cases wher
5d560 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64  e it really need
5d570 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73  s one.  If a fas
5d580 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76  ter non-recursiv
5d590 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65  e mutex.** imple
5d5a0 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61  mentation is ava
5d5b0 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f  ilable on the ho
5d5c0 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65  st platform, the
5d5d0 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
5d5e0 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e  .** might return
5d5f0 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e   such a mutex in
5d600 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c   response to SQL
5d610 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a  ITE_MUTEX_FAST..
5d620 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20  **.** The other 
5d630 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65  allowed paramete
5d640 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  rs to sqlite3_mu
5d650 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68  tex_alloc() each
5d660 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   return.** a poi
5d670 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63  nter to a static
5d680 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74   preexisting mut
5d690 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 74 69  ex.  Three stati
5d6a0 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a  c mutexes are.**
5d6b0 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72   used by the cur
5d6c0 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20  rent version of 
5d6d0 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20  SQLite.  Future 
5d6e0 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
5d6f0 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64  te.** may add ad
5d700 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20  ditional static 
5d710 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63  mutexes.  Static
5d720 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72   mutexes are for
5d730 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65   internal.** use
5d740 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e   by SQLite only.
5d750 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74    Applications t
5d760 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d  hat use SQLite m
5d770 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a  utexes should.**
5d780 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79   use only the dy
5d790 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65  namic mutexes re
5d7a0 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45  turned by SQLITE
5d7b0 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a  _MUTEX_FAST or.*
5d7c0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  * SQLITE_MUTEX_R
5d7d0 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20  ECURSIVE..**.** 
5d7e0 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65  Note that if one
5d7f0 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20   of the dynamic 
5d800 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73  mutex parameters
5d810 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46   (SQLITE_MUTEX_F
5d820 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45  AST.** or SQLITE
5d830 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
5d840 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73  ) is used then s
5d850 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
5d860 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20  oc().** returns 
5d870 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65  a different mute
5d880 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e  x on every call.
5d890 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74    But for the st
5d8a0 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74  atic .** mutex t
5d8b0 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d  ypes, the same m
5d8c0 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64  utex is returned
5d8d0 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74   on every call t
5d8e0 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73  hat has.** the s
5d8f0 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e  ame type number.
5d900 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
5d910 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
5d920 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
5d930 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20  c(int iType){.  
5d940 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
5d950 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 69 54 79  ;..  switch( iTy
5d960 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  pe ){.    case S
5d970 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
5d980 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  :.    case SQLIT
5d990 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
5d9a0 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73  E: {.      p = s
5d9b0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
5d9c0 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a  ( sizeof(*p) );.
5d9d0 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20        if( p ){. 
5d9e0 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69         p->id = i
5d9f0 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 49 6e  Type;.        In
5da00 69 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c  itializeCritical
5da10 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
5da20 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  x);.      }.    
5da30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
5da40 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
5da50 20 20 20 20 73 74 61 74 69 63 20 73 71 6c 69 74      static sqlit
5da60 65 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d  e3_mutex staticM
5da70 75 74 65 78 65 73 5b 35 5d 3b 0a 20 20 20 20 20  utexes[5];.     
5da80 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 49 6e   static int isIn
5da90 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68  it = 0;.      wh
5daa0 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b 0a  ile( !isInit ){.
5dab0 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 6c          static l
5dac0 6f 6e 67 20 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20  ong lock = 0;.  
5dad0 20 20 20 20 20 20 69 66 28 20 49 6e 74 65 72 6c        if( Interl
5dae0 6f 63 6b 65 64 49 6e 63 72 65 6d 65 6e 74 28 26  ockedIncrement(&
5daf0 6c 6f 63 6b 29 3d 3d 31 20 29 7b 0a 20 20 20 20  lock)==1 ){.    
5db00 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
5db10 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
5db20 69 3c 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d  i<sizeof(staticM
5db30 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73  utexes)/sizeof(s
5db40 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29  taticMutexes[0])
5db50 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
5db60 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72      InitializeCr
5db70 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 73  iticalSection(&s
5db80 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e  taticMutexes[i].
5db90 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20  mutex);.        
5dba0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 73    }.          is
5dbb0 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  Init = 1;.      
5dbc0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
5dbd0 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 20 20     Sleep(1);.   
5dbe0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
5dbf0 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79       assert( iTy
5dc00 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20  pe-2 >= 0 );.   
5dc10 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65     assert( iType
5dc20 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 74  -2 < sizeof(stat
5dc30 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f  icMutexes)/sizeo
5dc40 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b  f(staticMutexes[
5dc50 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d  0]) );.      p =
5dc60 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b   &staticMutexes[
5dc70 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20  iType-2];.      
5dc80 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
5dc90 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
5dca0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
5dcb0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ;.}.../*.** This
5dcc0 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63   routine dealloc
5dcd0 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c  ates a previousl
5dce0 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d  y.** allocated m
5dcf0 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73  utex.  SQLite is
5dd00 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c   careful to deal
5dd10 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20  locate every.** 
5dd20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c  mutex that it al
5dd30 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49  locates..*/.SQLI
5dd40 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
5dd50 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73  te3_mutex_free(s
5dd60 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
5dd70 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b  {.  assert( p );
5dd80 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
5dd90 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ef==0 );.  asser
5dda0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
5ddb0 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70  _MUTEX_FAST || p
5ddc0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
5ddd0 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a  EX_RECURSIVE );.
5dde0 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c    DeleteCritical
5ddf0 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
5de00 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  x);.  sqlite3_fr
5de10 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ee(p);.}../*.** 
5de20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
5de30 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
5de40 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
5de50 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
5de60 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61  pt.** to enter a
5de70 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74   mutex.  If anot
5de80 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
5de90 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
5dea0 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
5deb0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
5dec0 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
5ded0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
5dee0 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
5def0 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
5df00 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
5df10 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61  ex_try() interfa
5df20 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ce returns SQLIT
5df30 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  E_OK.** upon suc
5df40 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
5df50 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
5df60 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54  using SQLITE_MUT
5df70 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
5df80 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
5df90 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
5dfa0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
5dfb0 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
5dfc0 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d   the,.** mutex m
5dfd0 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e  ust be exited an
5dfe0 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66   equal number of
5dff0 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e   times before an
5e000 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
5e010 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74  can enter.  If t
5e020 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
5e030 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
5e040 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20  y other kind of 
5e050 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68  mutex.** more th
5e060 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
5e070 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
5e080 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
5e090 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d  I void sqlite3_m
5e0a0 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
5e0b0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
5e0c0 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61  assert( p );.  a
5e0d0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
5e0e0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
5e0f0 53 49 56 45 20 7c 7c 20 73 71 6c 69 74 65 33 5f  SIVE || sqlite3_
5e100 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 29  mutex_notheld(p)
5e110 20 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69   );.  EnterCriti
5e120 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d  calSection(&p->m
5e130 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65  utex);.  p->owne
5e140 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68  r = GetCurrentTh
5e150 72 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e  readId(); .  p->
5e160 6e 52 65 66 2b 2b 3b 0a 7d 0a 53 51 4c 49 54 45  nRef++;.}.SQLITE
5e170 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
5e180 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74  _mutex_try(sqlit
5e190 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
5e1a0 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
5e1b0 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ( p );.  assert(
5e1c0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
5e1d0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
5e1e0 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
5e1f0 6e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  notheld(p) );.  
5e200 69 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20  if( mutexIsNT() 
5e210 26 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69  && TryEnterCriti
5e220 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d  calSection(&p->m
5e230 75 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e  utex) ){.    p->
5e240 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65  owner = GetCurre
5e250 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20  ntThreadId();.  
5e260 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20    p->nRef++;.   
5e270 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
5e280 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
5e290 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
5e2a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
5e2b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
5e2c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
5e2d0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74  e() routine exit
5e2e0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77  s a mutex that w
5e2f0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  as.** previously
5e300 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20   entered by the 
5e310 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68  same thread.  Th
5e320 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
5e330 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
5e340 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
5e350 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
5e360 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75   or.** is not cu
5e370 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65  rrently allocate
5e380 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  d.  SQLite will 
5e390 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e  never do either.
5e3a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
5e3b0 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
5e3c0 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f  x_leave(sqlite3_
5e3d0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
5e3e0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
5e3f0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f  ;.  assert( p->o
5e400 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74  wner==GetCurrent
5e410 54 68 72 65 61 64 49 64 28 29 20 29 3b 0a 20 20  ThreadId() );.  
5e420 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73  p->nRef--;.  ass
5e430 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
5e440 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
5e450 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
5e460 20 29 3b 0a 20 20 4c 65 61 76 65 43 72 69 74 69   );.  LeaveCriti
5e470 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d  calSection(&p->m
5e480 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  utex);.}../*.** 
5e490 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
5e4a0 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c  x_held() and sql
5e4b0 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
5e4c0 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65  ld() routine are
5e4d0 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72  .** intended for
5e4e0 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65   use only inside
5e4f0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
5e500 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
5e510 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
5e520 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
5e530 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
5e540 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28  return p==0 || (
5e550 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d  p->nRef!=0 && p-
5e560 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65  >owner==GetCurre
5e570 6e 74 54 68 72 65 61 64 49 64 28 29 29 3b 0a 7d  ntThreadId());.}
5e580 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
5e590 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
5e5a0 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  theld(sqlite3_mu
5e5b0 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  tex *p){.  retur
5e5c0 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65  n p==0 || p->nRe
5e5d0 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72  f==0 || p->owner
5e5e0 21 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  !=GetCurrentThre
5e5f0 61 64 49 64 28 29 3b 0a 7d 0a 23 65 6e 64 69 66  adId();.}.#endif
5e600 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   /* SQLITE_MUTEX
5e610 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _W32 */../******
5e620 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
5e630 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a  mutex_w32.c ****
5e640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e660 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
5e670 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
5e680 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a  ile malloc.c ***
5e690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e6b0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
5e6c0 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
5e6d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
5e6e0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
5e6f0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
5e700 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
5e710 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
5e720 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
5e730 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
5e740 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
5e750 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
5e760 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
5e770 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
5e780 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
5e790 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
5e7a0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
5e7b0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
5e7c0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
5e7d0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
5e7e0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
5e7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
5e830 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** Memory alloca
5e840 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75  tion functions u
5e850 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73  sed throughout s
5e860 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  qlite..**.**.** 
5e870 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 2c 76 20  $Id: malloc.c,v 
5e880 31 2e 31 33 20 32 30 30 37 2f 30 38 2f 32 39 20  1.13 2007/08/29 
5e890 31 34 3a 30 36 3a 32 33 20 64 61 6e 69 65 6c 6b  14:06:23 danielk
5e8a0 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f  1977 Exp $.*/../
5e8b0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
5e8c0 65 20 72 75 6e 73 20 77 68 65 6e 20 74 68 65 20  e runs when the 
5e8d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
5e8e0 20 73 65 65 73 20 74 68 61 74 20 74 68 65 0a 2a   sees that the.*
5e8f0 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61  * total memory a
5e900 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 62 6f  llocation is abo
5e910 75 74 20 74 6f 20 65 78 63 65 65 64 20 74 68 65  ut to exceed the
5e920 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a 20 6c 69   soft heap.** li
5e930 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  mit..*/.static v
5e940 6f 69 64 20 73 6f 66 74 48 65 61 70 4c 69 6d 69  oid softHeapLimi
5e950 74 45 6e 66 6f 72 63 65 72 28 0a 20 20 76 6f 69  tEnforcer(.  voi
5e960 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73  d *NotUsed, .  s
5e970 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 6e 55  qlite3_int64 inU
5e980 73 65 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53  se,.  int allocS
5e990 69 7a 65 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33  ize.){.  sqlite3
5e9a0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
5e9b0 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f  allocSize);.}../
5e9c0 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66  *.** Set the sof
5e9d0 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69  t heap-size limi
5e9e0 74 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  t for the curren
5e9f0 74 20 74 68 72 65 61 64 2e 20 50 61 73 73 69 6e  t thread. Passin
5ea00 67 20 61 0a 2a 2a 20 7a 65 72 6f 20 6f 72 20 6e  g a.** zero or n
5ea10 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 6e  egative value in
5ea20 64 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d 69 74  dicates no limit
5ea30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
5ea40 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66  void sqlite3_sof
5ea50 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74  t_heap_limit(int
5ea60 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 75   n){.  sqlite3_u
5ea70 69 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a 20 20  int64 iLimit;.  
5ea80 69 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20 20 69  int overage;.  i
5ea90 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 69 4c  f( n<0 ){.    iL
5eaa0 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  imit = 0;.  }els
5eab0 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20  e{.    iLimit = 
5eac0 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 4c 69  n;.  }.  if( iLi
5ead0 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  mit>0 ){.    sql
5eae0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72  ite3_memory_alar
5eaf0 6d 28 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45  m(softHeapLimitE
5eb00 6e 66 6f 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d  nforcer, 0, iLim
5eb10 69 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  it);.  }else{.  
5eb20 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79    sqlite3_memory
5eb30 5f 61 6c 61 72 6d 28 30 2c 20 30 2c 20 30 29 3b  _alarm(0, 0, 0);
5eb40 0a 20 20 7d 0a 20 20 6f 76 65 72 61 67 65 20 3d  .  }.  overage =
5eb50 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f   sqlite3_memory_
5eb60 75 73 65 64 28 29 20 2d 20 6e 3b 0a 20 20 69 66  used() - n;.  if
5eb70 28 20 6f 76 65 72 61 67 65 3e 30 20 29 7b 0a 20  ( overage>0 ){. 
5eb80 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61     sqlite3_relea
5eb90 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 65 72 61 67  se_memory(overag
5eba0 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
5ebb0 20 52 65 6c 65 61 73 65 20 6d 65 6d 6f 72 79 20   Release memory 
5ebc0 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65 20 69  held by SQLite i
5ebd0 6e 73 74 61 6e 63 65 73 20 63 72 65 61 74 65 64  nstances created
5ebe0 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20   by the current 
5ebf0 74 68 72 65 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54  thread..*/.SQLIT
5ec00 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
5ec10 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
5ec20 28 69 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20  (int n){.#ifdef 
5ec30 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
5ec40 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
5ec50 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
5ec60 50 61 67 65 72 52 65 6c 65 61 73 65 4d 65 6d 6f  PagerReleaseMemo
5ec70 72 79 28 6e 29 3b 0a 23 65 6c 73 65 0a 20 20 72  ry(n);.#else.  r
5ec80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
5ec90 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a  .#endif.}.../*.*
5eca0 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a  * Allocate and z
5ecb0 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a  ero memory..*/ .
5ecc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5ecd0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c  oid *sqlite3Mall
5ece0 6f 63 5a 65 72 6f 28 75 6e 73 69 67 6e 65 64 20  ocZero(unsigned 
5ecf0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20  n){.  void *p = 
5ed00 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
5ed10 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
5ed20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e    memset(p, 0, n
5ed30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
5ed40 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  p;.}../*.** Allo
5ed50 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65  cate and zero me
5ed60 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c  mory.  If the al
5ed70 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
5ed80 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c  make.** the mall
5ed90 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e  ocFailed flag in
5eda0 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
5edb0 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
5edc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
5edd0 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  *sqlite3DbMalloc
5ede0 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62  Zero(sqlite3 *db
5edf0 2c 20 75 6e 73 69 67 6e 65 64 20 6e 29 7b 0a 20  , unsigned n){. 
5ee00 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74   void *p = sqlit
5ee10 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
5ee20 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  , n);.  if( p ){
5ee30 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  .    memset(p, 0
5ee40 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , n);.  }.  retu
5ee50 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn p;.}../*.** A
5ee60 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f  llocate and zero
5ee70 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65   memory.  If the
5ee80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
5ee90 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d  s, make.** the m
5eea0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
5eeb0 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   in the connecti
5eec0 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  on pointer..*/.S
5eed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
5eee0 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c  id *sqlite3DbMal
5eef0 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 20 2a  locRaw(sqlite3 *
5ef00 64 62 2c 20 75 6e 73 69 67 6e 65 64 20 6e 29 7b  db, unsigned n){
5ef10 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a  .  void *p = 0;.
5ef20 20 20 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d    if( !db || db-
5ef30 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30  >mallocFailed==0
5ef40 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69   ){.    p = sqli
5ef50 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20  te3_malloc(n);. 
5ef60 20 20 20 69 66 28 20 21 70 20 26 26 20 64 62 20     if( !p && db 
5ef70 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
5ef80 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
5ef90 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
5efa0 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n p;.}../*.** Re
5efb0 73 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f  size the block o
5efc0 66 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64  f memory pointed
5efd0 20 74 6f 20 62 79 20 70 20 74 6f 20 6e 20 62 79   to by p to n by
5efe0 74 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72  tes. If the.** r
5eff0 65 73 69 7a 65 20 66 61 69 6c 73 2c 20 73 65 74  esize fails, set
5f000 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65   the mallocFaile
5f010 64 20 66 6c 61 67 20 69 6e 74 68 65 20 63 6f 6e  d flag inthe con
5f020 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a  nection object..
5f030 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
5f040 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44  E void *sqlite3D
5f050 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33  bRealloc(sqlite3
5f060 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69   *db, void *p, i
5f070 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70  nt n){.  void *p
5f080 4e 65 77 20 3d 20 30 3b 0a 20 20 69 66 28 20 64  New = 0;.  if( d
5f090 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d  b->mallocFailed=
5f0a0 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d  =0 ){.    pNew =
5f0b0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
5f0c0 28 70 2c 20 6e 29 3b 0a 20 20 20 20 69 66 28 20  (p, n);.    if( 
5f0d0 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 64  !pNew ){.      d
5f0e0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
5f0f0 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 1;.    }.  }. 
5f100 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
5f110 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
5f120 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e 20  o reallocate p. 
5f130 20 49 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 61   If the realloca
5f140 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e  tion fails, then
5f150 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20 73   free p.** and s
5f160 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69  et the mallocFai
5f170 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20  led flag in the 
5f180 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
5f190 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
5f1a0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
5f1b0 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
5f1c0 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ree(sqlite3 *db,
5f1d0 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29   void *p, int n)
5f1e0 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a  {.  void *pNew;.
5f1f0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
5f200 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 2c  DbRealloc(db, p,
5f210 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77   n);.  if( !pNew
5f220 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
5f230 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72  free(p);.  }.  r
5f240 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
5f250 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79  *.** Make a copy
5f260 20 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e 20   of a string in 
5f270 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
5f280 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  from sqliteMallo
5f290 63 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66  c(). These .** f
5f2a0 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71  unctions call sq
5f2b0 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29  lite3MallocRaw()
5f2c0 20 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65 61   directly instea
5f2d0 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  d of sqliteMallo
5f2e0 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20  c(). This.** is 
5f2f0 62 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65 6d  because when mem
5f300 6f 72 79 20 64 65 62 75 67 67 69 6e 67 20 69 73  ory debugging is
5f310 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73   turned on, thes
5f320 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20  e two functions 
5f330 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76  are .** called v
5f340 69 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20 72  ia macros that r
5f350 65 63 6f 72 64 20 74 68 65 20 63 75 72 72 65 6e  ecord the curren
5f360 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20  t file and line 
5f370 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a  number in the.**
5f380 20 54 68 72 65 61 64 44 61 74 61 20 73 74 72 75   ThreadData stru
5f390 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
5f3a0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
5f3b0 71 6c 69 74 65 33 53 74 72 44 75 70 28 63 6f 6e  qlite3StrDup(con
5f3c0 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63  st char *z){.  c
5f3d0 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 6e 74  har *zNew;.  int
5f3e0 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29   n;.  if( z==0 )
5f3f0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 20 3d   return 0;.  n =
5f400 20 73 74 72 6c 65 6e 28 7a 29 2b 31 3b 0a 20 20   strlen(z)+1;.  
5f410 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zNew = sqlite3_m
5f420 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20  alloc(n);.  if( 
5f430 7a 4e 65 77 20 29 20 6d 65 6d 63 70 79 28 7a 4e  zNew ) memcpy(zN
5f440 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 72 65 74  ew, z, n);.  ret
5f450 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49  urn zNew;.}.SQLI
5f460 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
5f470 2a 73 71 6c 69 74 65 33 53 74 72 4e 44 75 70 28  *sqlite3StrNDup(
5f480 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
5f490 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a  nt n){.  char *z
5f4a0 4e 65 77 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  New;.  if( z==0 
5f4b0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a 4e  ) return 0;.  zN
5f4c0 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ew = sqlite3_mal
5f4d0 6c 6f 63 28 6e 2b 31 29 3b 0a 20 20 69 66 28 20  loc(n+1);.  if( 
5f4e0 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63  zNew ){.    memc
5f4f0 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a  py(zNew, z, n);.
5f500 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30 3b      zNew[n] = 0;
5f510 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e  .  }.  return zN
5f520 65 77 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  ew;.}..SQLITE_PR
5f530 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
5f540 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69  te3DbStrDup(sqli
5f550 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
5f560 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20  har *z){.  char 
5f570 2a 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 53  *zNew = sqlite3S
5f580 74 72 44 75 70 28 7a 29 3b 0a 20 20 69 66 28 20  trDup(z);.  if( 
5f590 7a 20 26 26 20 21 7a 4e 65 77 20 29 7b 0a 20 20  z && !zNew ){.  
5f5a0 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
5f5b0 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65  ed = 1;.  }.  re
5f5c0 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c  turn zNew;.}.SQL
5f5d0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
5f5e0 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44   *sqlite3DbStrND
5f5f0 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
5f600 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
5f610 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a  nt n){.  char *z
5f620 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 53 74 72  New = sqlite3Str
5f630 4e 44 75 70 28 7a 2c 20 6e 29 3b 0a 20 20 69 66  NDup(z, n);.  if
5f640 28 20 7a 20 26 26 20 21 7a 4e 65 77 20 29 7b 0a  ( z && !zNew ){.
5f650 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
5f660 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  iled = 1;.  }.  
5f670 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a  return zNew;.}..
5f680 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73  /*.** Create a s
5f690 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 32  tring from the 2
5f6a0 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
5f6b0 74 20 61 72 67 75 6d 65 6e 74 73 20 28 75 70 20  t arguments (up 
5f6c0 74 6f 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  to the.** first 
5f6d0 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 29 2c 20  NULL argument), 
5f6e0 73 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67  store the string
5f6f0 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   in memory obtai
5f700 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69  ned from.** sqli
5f710 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d  teMalloc() and m
5f720 61 6b 65 20 74 68 65 20 70 6f 69 6e 74 65 72 20  ake the pointer 
5f730 69 6e 64 69 63 61 74 65 64 20 62 79 20 74 68 65  indicated by the
5f740 20 31 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   1st argument.**
5f750 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73   point to that s
5f760 74 72 69 6e 67 2e 20 20 54 68 65 20 31 73 74 20  tring.  The 1st 
5f770 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 65 69  argument must ei
5f780 74 68 65 72 20 62 65 20 4e 55 4c 4c 20 6f 72 20  ther be NULL or 
5f790 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d  .** point to mem
5f7a0 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
5f7b0 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  m sqliteMalloc()
5f7c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
5f7d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5f7e0 53 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a  SetString(char *
5f7f0 2a 70 7a 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  *pz, ...){.  va_
5f800 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 6e  list ap;.  int n
5f810 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Byte;.  const ch
5f820 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 2a 7a  ar *z;.  char *z
5f830 52 65 73 75 6c 74 3b 0a 0a 20 20 61 73 73 65 72  Result;..  asser
5f840 74 28 20 70 7a 21 3d 30 20 29 3b 0a 20 20 6e 42  t( pz!=0 );.  nB
5f850 79 74 65 20 3d 20 31 3b 0a 20 20 76 61 5f 73 74  yte = 1;.  va_st
5f860 61 72 74 28 61 70 2c 20 70 7a 29 3b 0a 20 20 77  art(ap, pz);.  w
5f870 68 69 6c 65 28 20 28 7a 20 3d 20 76 61 5f 61 72  hile( (z = va_ar
5f880 67 28 61 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  g(ap, const char
5f890 2a 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 6e 42  *))!=0 ){.    nB
5f8a0 79 74 65 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 29  yte += strlen(z)
5f8b0 3b 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61  ;.  }.  va_end(a
5f8c0 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  p);.  sqlite3_fr
5f8d0 65 65 28 2a 70 7a 29 3b 0a 20 20 2a 70 7a 20 3d  ee(*pz);.  *pz =
5f8e0 20 7a 52 65 73 75 6c 74 20 3d 20 73 71 6c 69 74   zResult = sqlit
5f8f0 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29  e3_malloc(nByte)
5f900 3b 0a 20 20 69 66 28 20 7a 52 65 73 75 6c 74 3d  ;.  if( zResult=
5f910 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
5f920 3b 0a 20 20 7d 0a 20 20 2a 7a 52 65 73 75 6c 74  ;.  }.  *zResult
5f930 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74   = 0;.  va_start
5f940 28 61 70 2c 20 70 7a 29 3b 0a 20 20 77 68 69 6c  (ap, pz);.  whil
5f950 65 28 20 28 7a 20 3d 20 76 61 5f 61 72 67 28 61  e( (z = va_arg(a
5f960 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 29  p, const char*))
5f970 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  !=0 ){.    int n
5f980 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20   = strlen(z);.  
5f990 20 20 6d 65 6d 63 70 79 28 7a 52 65 73 75 6c 74    memcpy(zResult
5f9a0 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 52 65  , z, n);.    zRe
5f9b0 73 75 6c 74 20 2b 3d 20 6e 3b 0a 20 20 7d 0a 20  sult += n;.  }. 
5f9c0 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 30 3b   zResult[0] = 0;
5f9d0 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 7d  .  va_end(ap);.}
5f9e0 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
5f9f0 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63  nction must be c
5fa00 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 65 78 69  alled before exi
5fa10 74 69 6e 67 20 61 6e 79 20 41 50 49 20 66 75 6e  ting any API fun
5fa20 63 74 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a 20  ction (i.e. .** 
5fa30 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f  returning contro
5fa40 6c 20 74 6f 20 74 68 65 20 75 73 65 72 29 20 74  l to the user) t
5fa50 68 61 74 20 68 61 73 20 63 61 6c 6c 65 64 20 73  hat has called s
5fa60 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f 72  qlite3_malloc or
5fa70 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  .** sqlite3_real
5fa80 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  loc..**.** The r
5fa90 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73  eturned value is
5faa0 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 63 6f 70 79   normally a copy
5fab0 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   of the second a
5fac0 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 0a  rgument to this.
5fad0 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 48 6f 77  ** function. How
5fae0 65 76 65 72 2c 20 69 66 20 61 20 6d 61 6c 6c 6f  ever, if a mallo
5faf0 63 28 29 20 66 61 69 6c 75 72 65 20 68 61 73 20  c() failure has 
5fb00 6f 63 63 75 72 65 64 20 73 69 6e 63 65 20 74 68  occured since th
5fb10 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 69 6e  e previous.** in
5fb20 76 6f 63 61 74 69 6f 6e 20 53 51 4c 49 54 45 5f  vocation SQLITE_
5fb30 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65  NOMEM is returne
5fb40 64 20 69 6e 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a  d instead. .**.*
5fb50 2a 20 49 66 20 74 68 65 20 66 69 72 73 74 20 61  * If the first a
5fb60 72 67 75 6d 65 6e 74 2c 20 64 62 2c 20 69 73 20  rgument, db, is 
5fb70 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 61 20 6d  not NULL and a m
5fb80 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 68 61  alloc() error ha
5fb90 73 20 6f 63 63 75 72 65 64 2c 0a 2a 2a 20 74 68  s occured,.** th
5fba0 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  en the connectio
5fbb0 6e 20 65 72 72 6f 72 2d 63 6f 64 65 20 28 74 68  n error-code (th
5fbc0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
5fbd0 20 62 79 20 73 71 6c 69 74 65 33 5f 65 72 72 63   by sqlite3_errc
5fbe0 6f 64 65 28 29 29 0a 2a 2a 20 69 73 20 73 65 74  ode()).** is set
5fbf0 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   to SQLITE_NOMEM
5fc00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
5fc10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41  ATE int sqlite3A
5fc20 70 69 45 78 69 74 28 73 71 6c 69 74 65 33 2a 20  piExit(sqlite3* 
5fc30 64 62 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f  db, int rc){.  /
5fc40 2a 20 49 66 20 74 68 65 20 64 62 20 68 61 6e 64  * If the db hand
5fc50 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  le is not NULL, 
5fc60 74 68 65 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c  then we must hol
5fc70 64 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  d the connection
5fc80 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74   handle.  ** mut
5fc90 65 78 20 68 65 72 65 2e 20 4f 74 68 65 72 77 69  ex here. Otherwi
5fca0 73 65 20 74 68 65 20 72 65 61 64 20 28 61 6e 64  se the read (and
5fcb0 20 70 6f 73 73 69 62 6c 65 20 77 72 69 74 65 29   possible write)
5fcc0 20 6f 66 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   of db->mallocFa
5fcd0 69 6c 65 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e  iled .  ** is un
5fce0 73 61 66 65 2c 20 61 73 20 69 73 20 74 68 65 20  safe, as is the 
5fcf0 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45  call to sqlite3E
5fd00 72 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61  rror()..  */.  a
5fd10 73 73 65 72 74 28 20 21 64 62 20 7c 7c 20 73 71  ssert( !db || sq
5fd20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
5fd30 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
5fd40 20 69 66 28 20 64 62 20 26 26 20 64 62 2d 3e 6d   if( db && db->m
5fd50 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
5fd60 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
5fd70 64 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  db, SQLITE_NOMEM
5fd80 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6d 61  , 0);.    db->ma
5fd90 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
5fda0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
5fdb0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74  NOMEM;.  }.  ret
5fdc0 75 72 6e 20 72 63 20 26 20 28 64 62 20 3f 20 64  urn rc & (db ? d
5fdd0 62 2d 3e 65 72 72 4d 61 73 6b 20 3a 20 30 78 66  b->errMask : 0xf
5fde0 66 29 3b 0a 7d 0a 20 0a 0a 2f 2a 2a 2a 2a 2a 2a  f);.}. ../******
5fdf0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
5fe00 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a  malloc.c *******
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 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
5fe40 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
5fe50 69 6c 65 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a  ile printf.c ***
5fe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54  *******/./*.** T
5fe90 68 65 20 22 70 72 69 6e 74 66 22 20 63 6f 64 65  he "printf" code
5fea0 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64 61   that follows da
5feb0 74 65 73 20 66 72 6f 6d 20 74 68 65 20 31 39 38  tes from the 198
5fec0 30 27 73 2e 20 20 49 74 20 69 73 20 69 6e 0a 2a  0's.  It is in.*
5fed0 2a 20 74 68 65 20 70 75 62 6c 69 63 20 64 6f 6d  * the public dom
5fee0 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 67 69 6e  ain.  The origin
5fef0 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20  al comments are 
5ff00 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20 66 6f  included here fo
5ff10 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 6e 65 73  r.** completenes
5ff20 73 2e 20 20 54 68 65 79 20 61 72 65 20 76 65 72  s.  They are ver
5ff30 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 62 75  y out-of-date bu
5ff40 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 66 75  t might be usefu
5ff50 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69 73 74 6f  l as.** an histo
5ff60 72 69 63 61 6c 20 72 65 66 65 72 65 6e 63 65 2e  rical reference.
5ff70 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 22 65    Most of the "e
5ff80 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20 68 61 76  nhancements" hav
5ff90 65 20 62 65 65 6e 20 62 61 63 6b 65 64 0a 2a 2a  e been backed.**
5ffa0 20 6f 75 74 20 73 6f 20 74 68 61 74 20 74 68 65   out so that the
5ffb0 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69   functionality i
5ffc0 73 20 6e 6f 77 20 74 68 65 20 73 61 6d 65 20 61  s now the same a
5ffd0 73 20 73 74 61 6e 64 61 72 64 20 70 72 69 6e 74  s standard print
5ffe0 66 28 29 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  f()..**.********
5fff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60030 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  **.**.** The fol
60040 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69  lowing modules i
60050 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65  s an enhanced re
60060 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68  placement for th
60070 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f  e "printf" subro
60080 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20  utines.** found 
60090 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  in the standard 
600a0 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  C library.  The 
600b0 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63  following enhanc
600c0 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75  ements are.** su
600d0 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  pported:.**.**  
600e0 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61      +  Additiona
600f0 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  l functions.  Th
60100 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f  e standard set o
60110 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74  f "printf" funct
60120 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ions.**         
60130 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c  includes printf,
60140 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74   fprintf, sprint
60150 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72  f, vprintf, vfpr
60160 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20  intf, and.**    
60170 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20       vsprintf.  
60180 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73  This module adds
60190 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
601a0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
601b0 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57  *  snprintf -- W
601c0 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74  orks like sprint
601d0 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65 78  f, but has an ex
601e0 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  tra argument.** 
601f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60200 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20 69           which i
60210 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
60220 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65 6e  e buffer written
60230 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   to..**.**      
60240 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20       *  mprintf 
60250 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73  --  Similar to s
60260 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20  printf.  Writes 
60270 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79  output to memory
60280 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
60290 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62 74               obt
602a0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
602b0 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  c..**.**        
602c0 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d     *  xprintf --
602d0 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69    Calls a functi
602e0 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  on to dispose of
602f0 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20   output..**.**  
60300 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69           *  npri
60310 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75  ntf --  No outpu
60320 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20 74  t, but returns t
60330 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
60340 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20  racters.**      
60350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60360 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68      that would h
60370 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20  ave been output 
60380 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a  by printf..**.**
60390 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41 20             *  A 
603a0 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20  v- version (ex: 
603b0 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76  vsnprintf) of ev
603c0 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ery function is 
603d0 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  also.**         
603e0 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a       supplied..*
603f0 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66  *.**      +  A f
60400 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f  ew extensions to
60410 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20   the formatting 
60420 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70  notation are sup
60430 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  ported:.**.**   
60440 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 22          *  The "
60450 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72  =" flag (similar
60460 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20   to "-") causes 
60470 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65  the output to be
60480 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
60490 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20   be centered in 
604a0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c  the appropriatel
604b0 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a  y sized field..*
604c0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
604d0 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f    The %b field o
604e0 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65  utputs an intege
604f0 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61  r in binary nota
60500 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  tion..**.**     
60510 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63 20        *  The %c 
60520 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74  field now accept
60530 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20  s a precision.  
60540 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f 75  The character ou
60550 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  tput.**         
60560 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65 64       is repeated
60570 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
60580 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65 63  f times the prec
60590 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e  ision specifies.
605a0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
605b0 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c 64   *  The %' field
605c0 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20   works like %c, 
605d0 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74 73  but takes as its
605e0 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a   character the.*
605f0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  *              n
60600 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f 66  ext character of
60610 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69   the format stri
60620 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74  ng, instead of t
60630 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20  he next.**      
60640 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74          argument
60650 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
60660 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22   printf("%.78'-"
60670 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e  )  prints 78 min
60680 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  us.**           
60690 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61     signs, the sa
606a0 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22 25  me as  printf("%
606b0 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a  .78c",'-')..**.*
606c0 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63  *      +  When c
606d0 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43  ompiled using GC
606e0 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68  C on a SPARC, th
606f0 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72  is version of pr
60700 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20  intf is.**      
60710 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20 74     faster than t
60720 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74  he library print
60730 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31  f for SUN OS 4.1
60740 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20  ..**.**      +  
60750 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  All functions ar
60760 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e  e fully reentran
60770 74 2e 0a 2a 2a 0a 2a 2f 0a 23 69 6e 63 6c 75 64  t..**.*/.#includ
60780 65 20 3c 6d 61 74 68 2e 68 3e 0a 0a 2f 2a 0a 2a  e <math.h>../*.*
60790 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70  * Conversion typ
607a0 65 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72  es fall into var
607b0 69 6f 75 73 20 63 61 74 65 67 6f 72 69 65 73 20  ious categories 
607c0 61 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68  as defined by th
607d0 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65  e.** following e
607e0 6e 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23  numeration..*/.#
607f0 64 65 66 69 6e 65 20 65 74 52 41 44 49 58 20 20  define etRADIX  
60800 20 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65       1 /* Intege
60810 72 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78  r types.  %d, %x
60820 2c 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72  , %o, and so for
60830 74 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  th */.#define et
60840 46 4c 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a  FLOAT       2 /*
60850 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e   Floating point.
60860 20 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20    %f */.#define 
60870 65 74 45 58 50 20 20 20 20 20 20 20 20 20 33 20  etEXP         3 
60880 2f 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c  /* Exponentional
60890 20 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e   notation. %e an
608a0 64 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20  d %E */.#define 
608b0 65 74 47 45 4e 45 52 49 43 20 20 20 20 20 34 20  etGENERIC     4 
608c0 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65  /* Floating or e
608d0 78 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65  xponential, depe
608e0 6e 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e  nding on exponen
608f0 74 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65  t. %g */.#define
60900 20 65 74 53 49 5a 45 20 20 20 20 20 20 20 20 35   etSIZE        5
60910 20 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65   /* Return numbe
60920 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
60930 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72  processed so far
60940 2e 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  . %n */.#define 
60950 65 74 53 54 52 49 4e 47 20 20 20 20 20 20 36 20  etSTRING      6 
60960 2f 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a  /* Strings. %s *
60970 2f 0a 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53  /.#define etDYNS
60980 54 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e  TRING   7 /* Dyn
60990 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
609a0 65 64 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a  ed strings. %z *
609b0 2f 0a 23 64 65 66 69 6e 65 20 65 74 50 45 52 43  /.#define etPERC
609c0 45 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72  ENT     8 /* Per
609d0 63 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20  cent symbol. %% 
609e0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41  */.#define etCHA
609f0 52 58 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68  RX       9 /* Ch
60a00 61 72 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a  aracters. %c */.
60a10 2f 2a 20 54 68 65 20 72 65 73 74 20 61 72 65 20  /* The rest are 
60a20 65 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20  extensions, not 
60a30 6e 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69  normally found i
60a40 6e 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64  n printf() */.#d
60a50 65 66 69 6e 65 20 65 74 43 48 41 52 4c 49 54 20  efine etCHARLIT 
60a60 20 20 20 31 30 20 2f 2a 20 4c 69 74 65 72 61 6c     10 /* Literal
60a70 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 25 27   characters.  %'
60a80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51   */.#define etSQ
60a90 4c 45 53 43 41 50 45 20 20 31 31 20 2f 2a 20 53  LESCAPE  11 /* S
60aa0 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27 27  trings with '\''
60ab0 20 64 6f 75 62 6c 65 64 2e 20 20 25 71 20 2a 2f   doubled.  %q */
60ac0 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53  .#define etSQLES
60ad0 43 41 50 45 32 20 31 32 20 2f 2a 20 53 74 72 69  CAPE2 12 /* Stri
60ae0 6e 67 73 20 77 69 74 68 20 27 5c 27 27 20 64 6f  ngs with '\'' do
60af0 75 62 6c 65 64 20 61 6e 64 20 65 6e 63 6c 6f 73  ubled and enclos
60b00 65 64 20 69 6e 20 27 27 2c 0a 20 20 20 20 20 20  ed in '',.      
60b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60b20 20 20 20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72      NULL pointer
60b30 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 53 51  s replaced by SQ
60b40 4c 20 4e 55 4c 4c 2e 20 20 25 51 20 2a 2f 0a 23  L NULL.  %Q */.#
60b50 64 65 66 69 6e 65 20 65 74 54 4f 4b 45 4e 20 20  define etTOKEN  
60b60 20 20 20 20 31 33 20 2f 2a 20 61 20 70 6f 69 6e      13 /* a poin
60b70 74 65 72 20 74 6f 20 61 20 54 6f 6b 65 6e 20 73  ter to a Token s
60b80 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66  tructure */.#def
60b90 69 6e 65 20 65 74 53 52 43 4c 49 53 54 20 20 20  ine etSRCLIST   
60ba0 20 31 34 20 2f 2a 20 61 20 70 6f 69 6e 74 65 72   14 /* a pointer
60bb0 20 74 6f 20 61 20 53 72 63 4c 69 73 74 20 2a 2f   to a SrcList */
60bc0 0a 23 64 65 66 69 6e 65 20 65 74 50 4f 49 4e 54  .#define etPOINT
60bd0 45 52 20 20 20 20 31 35 20 2f 2a 20 54 68 65 20  ER    15 /* The 
60be0 25 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f  %p conversion */
60bf0 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53  .#define etSQLES
60c00 43 41 50 45 33 20 31 36 20 2f 2a 20 25 77 20 2d  CAPE3 16 /* %w -
60c10 3e 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27  > Strings with '
60c20 5c 22 27 20 64 6f 75 62 6c 65 64 20 2a 2f 0a 0a  \"' doubled */..
60c30 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 74  ./*.** An "etByt
60c40 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 75  e" is an 8-bit u
60c50 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a 2a  nsigned value..*
60c60 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e  /.typedef unsign
60c70 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b 0a  ed char etByte;.
60c80 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 6c  ./*.** Each buil
60c90 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63  tin conversion c
60ca0 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 68  haracter (ex: th
60cb0 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 69  e 'd' in "%d") i
60cc0 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62  s described.** b
60cd0 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
60ce0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
60cf0 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65  tructure.*/.type
60d00 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 6e  def struct et_in
60d10 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  fo {   /* Inform
60d20 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68  ation about each
60d30 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a 2f   format field */
60d40 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 3b  .  char fmttype;
60d50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
60d60 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20  he format field 
60d70 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a 20  code letter */. 
60d80 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 20   etByte base;   
60d90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
60da0 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 20   base for radix 
60db0 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20  conversion */.  
60dc0 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 20  etByte flags;   
60dd0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
60de0 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 5f  or more of FLAG_
60df0 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77   constants below
60e00 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 70   */.  etByte typ
60e10 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
60e20 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 72  * Conversion par
60e30 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 74  adigm */.  etByt
60e40 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 20  e charset;      
60e50 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
60e60 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 20  to aDigits[] of 
60e70 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69 6e  the digits strin
60e80 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 72  g */.  etByte pr
60e90 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
60ea0 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61  /* Offset into a
60eb0 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 20  Prefix[] of the 
60ec0 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a 2f  prefix string */
60ed0 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a  .} et_info;../*.
60ee0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
60ef0 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 6c  s for et_info.fl
60f00 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  ags.*/.#define F
60f10 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 20  LAG_SIGNED  1   
60f20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
60f30 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 72   value to conver
60f40 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a 23  t is signed */.#
60f50 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 45  define FLAG_INTE
60f60 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 75  RN  2     /* Tru
60f70 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e 61  e if for interna
60f80 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64  l use only */.#d
60f90 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 4e  efine FLAG_STRIN
60fa0 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c 6f  G  4     /* Allo
60fb0 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 69  w infinity preci
60fc0 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  sion */.../*.** 
60fd0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  The following ta
60fe0 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 20  ble is searched 
60ff0 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 20  linearly, so it 
61000 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 74  is good to put t
61010 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 75  he.** most frequ
61020 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 65  ently used conve
61030 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 73  rsion types firs
61040 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  t..*/.static con
61050 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73 5b  st char aDigits[
61060 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 41  ] = "0123456789A
61070 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39 61  BCDEF0123456789a
61080 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 63  bcdef";.static c
61090 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 69  onst char aPrefi
610a0 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 30  x[] = "-x0\000X0
610b0 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  ";.static const 
610c0 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b  et_info fmtinfo[
610d0 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c 20  ] = {.  {  'd', 
610e0 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20  10, 1, etRADIX, 
610f0 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20       0,  0 },.  
61100 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 65  {  's',  0, 4, e
61110 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c 20  tSTRING,     0, 
61120 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 20   0 },.  {  'g', 
61130 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43   0, 1, etGENERIC
61140 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20  ,    30, 0 },.  
61150 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 65  {  'z',  0, 4, e
61160 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c 20  tDYNSTRING,  0, 
61170 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 20   0 },.  {  'q', 
61180 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41   0, 4, etSQLESCA
61190 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20  PE,  0,  0 },.  
611a0 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 65  {  'Q',  0, 4, e
611b0 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c 20  tSQLESCAPE2, 0, 
611c0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 20   0 },.  {  'w', 
611d0 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41   0, 4, etSQLESCA
611e0 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20  PE3, 0,  0 },.  
611f0 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 65  {  'c',  0, 0, e
61200 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c 20  tCHARX,      0, 
61210 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 20   0 },.  {  'o', 
61220 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20   8, 0, etRADIX, 
61230 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 20       0,  2 },.  
61240 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 65  {  'u', 10, 0, e
61250 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20  tRADIX,      0, 
61260 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 20   0 },.  {  'x', 
61270 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20  16, 0, etRADIX, 
61280 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 20       16, 1 },.  
61290 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 65  {  'X', 16, 0, e
612a0 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20  tRADIX,      0, 
612b0 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 51   4 },.#ifndef SQ
612c0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
612d0 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 66  NG_POINT.  {  'f
612e0 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f 41  ',  0, 1, etFLOA
612f0 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  T,      0,  0 },
61300 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 31  .  {  'e',  0, 1
61310 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20  , etEXP,        
61320 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 45  30, 0 },.  {  'E
61330 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c  ',  0, 1, etEXP,
61340 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d 2c          14, 0 },
61350 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 31  .  {  'G',  0, 1
61360 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20  , etGENERIC,    
61370 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a  14, 0 },.#endif.
61380 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 2c    {  'i', 10, 1,
61390 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30   etRADIX,      0
613a0 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 27  ,  0 },.  {  'n'
613b0 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 2c  ,  0, 0, etSIZE,
613c0 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a         0,  0 },.
613d0 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 2c    {  '%',  0, 0,
613e0 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 30   etPERCENT,    0
613f0 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 27  ,  0 },.  {  'p'
61400 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e 54  , 16, 0, etPOINT
61410 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c 0a  ER,    0,  1 },.
61420 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 2c    {  'T',  0, 2,
61430 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 30   etTOKEN,      0
61440 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 27  ,  0 },.  {  'S'
61450 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c 49  ,  0, 2, etSRCLI
61460 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  ST,    0,  0 },.
61470 7d 3b 0a 23 64 65 66 69 6e 65 20 65 74 4e 49 4e  };.#define etNIN
61480 46 4f 20 20 28 73 69 7a 65 6f 66 28 66 6d 74 69  FO  (sizeof(fmti
61490 6e 66 6f 29 2f 73 69 7a 65 6f 66 28 66 6d 74 69  nfo)/sizeof(fmti
614a0 6e 66 6f 5b 30 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20  nfo[0]))../*.** 
614b0 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  If SQLITE_OMIT_F
614c0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 69 73  LOATING_POINT is
614d0 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 6e   defined, then n
614e0 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c 6f 61 74  one of the float
614f0 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e  ing point.** con
61500 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20 77 6f  versions will wo
61510 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  rk..*/.#ifndef S
61520 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
61530 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20  ING_POINT./*.** 
61540 22 2a 76 61 6c 22 20 69 73 20 61 20 64 6f 75 62  "*val" is a doub
61550 6c 65 20 73 75 63 68 20 74 68 61 74 20 30 2e 31  le such that 0.1
61560 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30 2e 30 0a   <= *val < 10.0.
61570 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 73  ** Return the as
61580 63 69 69 20 63 6f 64 65 20 66 6f 72 20 74 68 65  cii code for the
61590 20 6c 65 61 64 69 6e 67 20 64 69 67 69 74 20 6f   leading digit o
615a0 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20  f *val, then.** 
615b0 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61 6c 22 20  multiply "*val" 
615c0 62 79 20 31 30 2e 30 20 74 6f 20 72 65 6e 6f 72  by 10.0 to renor
615d0 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78  malize..**.** Ex
615e0 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20 20 69 6e  ample:.**     in
615f0 70 75 74 3a 20 20 20 20 20 2a 76 61 6c 20 3d 20  put:     *val = 
61600 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20 20 20 6f  3.14159.**     o
61610 75 74 70 75 74 3a 20 20 20 20 2a 76 61 6c 20 3d  utput:    *val =
61620 20 31 2e 34 31 35 39 20 20 20 20 66 75 6e 63 74   1.4159    funct
61630 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 27 33 27  ion return = '3'
61640 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74  .**.** The count
61650 65 72 20 2a 63 6e 74 20 69 73 20 69 6e 63 72 65  er *cnt is incre
61660 6d 65 6e 74 65 64 20 65 61 63 68 20 74 69 6d 65  mented each time
61670 2e 20 20 41 66 74 65 72 20 63 6f 75 6e 74 65 72  .  After counter
61680 20 65 78 63 65 65 64 73 0a 2a 2a 20 31 36 20 28   exceeds.** 16 (
61690 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69  the number of si
616a0 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73  gnificant digits
616b0 20 69 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f   in a 64-bit flo
616c0 61 74 29 20 27 30 27 20 69 73 0a 2a 2a 20 61 6c  at) '0' is.** al
616d0 77 61 79 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  ways returned..*
616e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 74 5f  /.static int et_
616f0 67 65 74 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55  getdigit(LONGDOU
61700 42 4c 45 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69  BLE_TYPE *val, i
61710 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20  nt *cnt){.  int 
61720 64 69 67 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55  digit;.  LONGDOU
61730 42 4c 45 5f 54 59 50 45 20 64 3b 0a 20 20 69 66  BLE_TYPE d;.  if
61740 28 20 28 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36  ( (*cnt)++ >= 16
61750 20 29 20 72 65 74 75 72 6e 20 27 30 27 3b 0a 20   ) return '0';. 
61760 20 64 69 67 69 74 20 3d 20 28 69 6e 74 29 2a 76   digit = (int)*v
61770 61 6c 3b 0a 20 20 64 20 3d 20 64 69 67 69 74 3b  al;.  d = digit;
61780 0a 20 20 64 69 67 69 74 20 2b 3d 20 27 30 27 3b  .  digit += '0';
61790 0a 20 20 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20  .  *val = (*val 
617a0 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74  - d)*10.0;.  ret
617b0 75 72 6e 20 64 69 67 69 74 3b 0a 7d 0a 23 65 6e  urn digit;.}.#en
617c0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
617d0 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
617e0 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d  T */../*.** On m
617f0 61 63 68 69 6e 65 73 20 77 69 74 68 20 61 20 73  achines with a s
61800 6d 61 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c  mall stack size,
61810 20 79 6f 75 20 63 61 6e 20 72 65 64 65 66 69 6e   you can redefin
61820 65 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f  e the.** SQLITE_
61830 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74  PRINT_BUF_SIZE t
61840 6f 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 33  o be less than 3
61850 35 30 2e 20 20 42 75 74 20 62 65 77 61 72 65 20  50.  But beware 
61860 2d 20 66 6f 72 0a 2a 2a 20 73 6d 61 6c 6c 65 72  - for.** smaller
61870 20 76 61 6c 75 65 73 20 73 6f 6d 65 20 25 66 20   values some %f 
61880 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6d 61 79 20  conversions may 
61890 67 6f 20 69 6e 74 6f 20 61 6e 20 69 6e 66 69 6e  go into an infin
618a0 69 74 65 20 6c 6f 6f 70 2e 0a 2a 2f 0a 23 69 66  ite loop..*/.#if
618b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 49 4e  ndef SQLITE_PRIN
618c0 54 5f 42 55 46 5f 53 49 5a 45 0a 23 20 64 65 66  T_BUF_SIZE.# def
618d0 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54  ine SQLITE_PRINT
618e0 5f 42 55 46 5f 53 49 5a 45 20 33 35 30 0a 23 65  _BUF_SIZE 350.#e
618f0 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 65 74 42  ndif.#define etB
61900 55 46 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 52  UFSIZE SQLITE_PR
61910 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a  INT_BUF_SIZE  /*
61920 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74   Size of the out
61930 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f  put buffer */../
61940 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 72  *.** The root pr
61950 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69  ogram.  All vari
61960 61 74 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73  ations call this
61970 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50   core..**.** INP
61980 55 54 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20  UTS:.**   func  
61990 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74   This is a point
619a0 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
619b0 20 74 61 6b 69 6e 67 20 74 68 72 65 65 20 61 72   taking three ar
619c0 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20  guments.**      
619d0 20 20 20 20 20 20 31 2e 20 41 20 70 6f 69 6e 74        1. A point
619e0 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20  er to anything. 
619f0 20 53 61 6d 65 20 61 73 20 74 68 65 20 22 61 72   Same as the "ar
61a00 67 22 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  g" parameter..**
61a10 20 20 20 20 20 20 20 20 20 20 20 20 32 2e 20 41              2. A
61a20 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
61a30 6c 69 73 74 20 6f 66 20 63 68 61 72 61 63 74 65  list of characte
61a40 72 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a  rs to be output.
61a50 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
61a60 20 28 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69 73   (Note, this lis
61a70 74 20 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65  t is NOT null te
61a80 72 6d 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20  rminated.).**   
61a90 20 20 20 20 20 20 20 20 20 33 2e 20 41 6e 20 69           3. An i
61aa0 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66  nteger number of
61ab0 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 62   characters to b
61ac0 65 20 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20  e output..**    
61ad0 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65             (Note
61ae0 3a 20 54 68 69 73 20 6e 75 6d 62 65 72 20 6d 69  : This number mi
61af0 67 68 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a  ght be zero.).**
61b00 0a 2a 2a 20 20 20 61 72 67 20 20 20 20 54 68 69  .**   arg    Thi
61b10 73 20 69 73 20 74 68 65 20 70 6f 69 6e 74 65 72  s is the pointer
61b20 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 77 68 69   to anything whi
61b30 63 68 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65  ch will be passe
61b40 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  d as the.**     
61b50 20 20 20 20 20 66 69 72 73 74 20 61 72 67 75 6d       first argum
61b60 65 6e 74 20 74 6f 20 22 66 75 6e 63 22 2e 20 20  ent to "func".  
61b70 55 73 65 20 69 74 20 66 6f 72 20 77 68 61 74 65  Use it for whate
61b80 76 65 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a  ver you like..**
61b90 0a 2a 2a 20 20 20 66 6d 74 20 20 20 20 54 68 69  .**   fmt    Thi
61ba0 73 20 69 73 20 74 68 65 20 66 6f 72 6d 61 74 20  s is the format 
61bb0 73 74 72 69 6e 67 2c 20 61 73 20 69 6e 20 74 68  string, as in th
61bc0 65 20 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a  e usual print..*
61bd0 2a 0a 2a 2a 20 20 20 61 70 20 20 20 20 20 54 68  *.**   ap     Th
61be0 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  is is a pointer 
61bf0 74 6f 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67  to a list of arg
61c00 75 6d 65 6e 74 73 2e 20 20 53 61 6d 65 20 61 73  uments.  Same as
61c10 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   in.**          
61c20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f  vfprint..**.** O
61c30 55 54 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20  UTPUTS:.**      
61c40 20 20 20 20 54 68 65 20 72 65 74 75 72 6e 20 76      The return v
61c50 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74 61  alue is the tota
61c60 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  l number of char
61c70 61 63 74 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a  acters sent to.*
61c80 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66  *          the f
61c90 75 6e 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20  unction "func". 
61ca0 20 52 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61   Returns -1 on a
61cb0 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f   error..**.** No
61cc0 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 65  te that the orde
61cd0 72 20 69 6e 20 77 68 69 63 68 20 61 75 74 6f 6d  r in which autom
61ce0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 61  atic variables a
61cf0 72 65 20 64 65 63 6c 61 72 65 64 20 62 65 6c 6f  re declared belo
61d00 77 0a 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61  w.** seems to ma
61d10 6b 65 20 61 20 62 69 67 20 64 69 66 66 65 72 65  ke a big differe
61d20 6e 63 65 20 69 6e 20 64 65 74 65 72 6d 69 6e 69  nce in determini
61d30 6e 67 20 68 6f 77 20 66 61 73 74 20 74 68 69 73  ng how fast this
61d40 20 62 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72   beast.** will r
61d50 75 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  un..*/.static in
61d60 74 20 76 78 70 72 69 6e 74 66 28 0a 20 20 76 6f  t vxprintf(.  vo
61d70 69 64 20 28 2a 66 75 6e 63 29 28 76 6f 69 64 2a  id (*func)(void*
61d80 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
61d90 29 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 73 75 6d  ),     /* Consum
61da0 65 72 20 6f 66 20 74 65 78 74 20 2a 2f 0a 20 20  er of text */.  
61db0 76 6f 69 64 20 2a 61 72 67 2c 20 20 20 20 20 20  void *arg,      
61dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61dd0 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
61de0 6d 65 6e 74 20 74 6f 20 74 68 65 20 63 6f 6e 73  ment to the cons
61df0 75 6d 65 72 20 2a 2f 0a 20 20 69 6e 74 20 75 73  umer */.  int us
61e00 65 45 78 74 65 6e 64 65 64 2c 20 20 20 20 20 20  eExtended,      
61e10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
61e20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 64 20 25  Allow extended %
61e30 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a  -conversions */.
61e40 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d    const char *fm
61e50 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
61e60 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 74 20 73       /* Format s
61e70 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f 6c 69  tring */.  va_li
61e80 73 74 20 61 70 20 20 20 20 20 20 20 20 20 20 20  st ap           
61e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
61ea0 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b   arguments */.){
61eb0 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20  .  int c;       
61ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
61ed0 20 4e 65 78 74 20 63 68 61 72 61 63 74 65 72 20   Next character 
61ee0 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74  in the format st
61ef0 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ring */.  char *
61f00 62 75 66 70 74 3b 20 20 20 20 20 20 20 20 20 20  bufpt;          
61f10 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
61f20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  to the conversio
61f30 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  n buffer */.  in
61f40 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 20 20 20  t precision;    
61f50 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 63           /* Prec
61f60 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72  ision of the cur
61f70 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20  rent field */.  
61f80 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20 20 20  int length;     
61f90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
61fa0 6e 67 74 68 20 6f 66 20 74 68 65 20 66 69 65 6c  ngth of the fiel
61fb0 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 20  d */.  int idx; 
61fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61fd0 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c 20 70    /* A general p
61fe0 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f 75 6e  urpose loop coun
61ff0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75  ter */.  int cou
62000 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
62010 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
62020 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
62030 73 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e  s output */.  in
62040 74 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20  t width;        
62050 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74           /* Widt
62060 68 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  h of the current
62070 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79   field */.  etBy
62080 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74  te flag_leftjust
62090 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69  ify;   /* True i
620a0 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72  f "-" flag is pr
620b0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
620c0 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b  e flag_plussign;
620d0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
620e0 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65   "+" flag is pre
620f0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
62100 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b   flag_blanksign;
62110 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
62120 22 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  " " flag is pres
62130 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
62140 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
62150 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22  rm; /* True if "
62160 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  #" flag is prese
62170 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
62180 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20  lag_altform2;   
62190 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21     /* True if "!
621a0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
621b0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
621c0 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20  ag_zeropad;     
621d0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65    /* True if fie
621e0 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e  ld width constan
621f0 74 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65  t starts with ze
62200 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  ro */.  etByte f
62210 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20  lag_long;       
62220 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c     /* True if "l
62230 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
62240 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
62250 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20  ag_longlong;    
62260 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
62270 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72   "ll" flag is pr
62280 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
62290 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20  e done;         
622a0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65        /* Loop te
622b0 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a  rmination flag *
622c0 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  /.  sqlite_uint6
622d0 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f  4 longvalue;   /
622e0 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65  * Value for inte
622f0 67 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c  ger types */.  L
62300 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72  ONGDOUBLE_TYPE r
62310 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c  ealvalue; /* Val
62320 75 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65  ue for real type
62330 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f  s */.  const et_
62340 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20  info *infop;    
62350 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
62360 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
62370 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a  info structure *
62380 2f 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42  /.  char buf[etB
62390 55 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f  UFSIZE];       /
623a0 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66  * Conversion buf
623b0 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72  fer */.  char pr
623c0 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
623d0 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68      /* Prefix ch
623e0 61 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72  aracter.  "+" or
623f0 20 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27   "-" or " " or '
62400 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65  \0'. */.  etByte
62410 20 65 72 72 6f 72 66 6c 61 67 20 3d 20 30 3b 20   errorflag = 0; 
62420 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
62430 61 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  an error is enco
62440 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 65 74 42  untered */.  etB
62450 79 74 65 20 78 74 79 70 65 3b 20 20 20 20 20 20  yte xtype;      
62460 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
62470 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a  rsion paradigm *
62480 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61  /.  char *zExtra
62490 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
624a0 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72 79 20 75  * Extra memory u
624b0 73 65 64 20 66 6f 72 20 65 74 54 43 4c 45 53 43  sed for etTCLESC
624c0 41 50 45 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  APE conversions 
624d0 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
624e0 74 20 63 68 61 72 20 73 70 61 63 65 73 5b 5d 20  t char spaces[] 
624f0 3d 0a 20 20 20 22 20 20 20 20 20 20 20 20 20 20  =.   "          
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 20                  
62520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
62540 3b 0a 23 64 65 66 69 6e 65 20 65 74 53 50 41 43  ;.#define etSPAC
62550 45 53 49 5a 45 20 28 73 69 7a 65 6f 66 28 73 70  ESIZE (sizeof(sp
62560 61 63 65 73 29 2d 31 29 0a 23 69 66 6e 64 65 66  aces)-1).#ifndef
62570 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
62580 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 6e  ATING_POINT.  in
62590 74 20 20 65 78 70 2c 20 65 32 3b 20 20 20 20 20  t  exp, e2;     
625a0 20 20 20 20 20 20 20 20 20 2f 2a 20 65 78 70 6f           /* expo
625b0 6e 65 6e 74 20 6f 66 20 72 65 61 6c 20 6e 75 6d  nent of real num
625c0 62 65 72 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  bers */.  double
625d0 20 72 6f 75 6e 64 65 72 3b 20 20 20 20 20 20 20   rounder;       
625e0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72       /* Used for
625f0 20 72 6f 75 6e 64 69 6e 67 20 66 6c 6f 61 74 69   rounding floati
62600 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20  ng point values 
62610 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
62620 5f 64 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  _dp;            
62630 2f 2a 20 54 72 75 65 20 69 66 20 64 65 63 69 6d  /* True if decim
62640 61 6c 20 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20  al point should 
62650 62 65 20 73 68 6f 77 6e 20 2a 2f 0a 20 20 65 74  be shown */.  et
62660 42 79 74 65 20 66 6c 61 67 5f 72 74 7a 3b 20 20  Byte flag_rtz;  
62670 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
62680 20 69 66 20 74 72 61 69 6c 69 6e 67 20 7a 65 72   if trailing zer
62690 6f 73 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6d  os should be rem
626a0 6f 76 65 64 20 2a 2f 0a 20 20 65 74 42 79 74 65  oved */.  etByte
626b0 20 66 6c 61 67 5f 65 78 70 3b 20 20 20 20 20 20   flag_exp;      
626c0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
626d0 66 6f 72 63 65 20 64 69 73 70 6c 61 79 20 6f 66  force display of
626e0 20 74 68 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f   the exponent */
626f0 0a 20 20 69 6e 74 20 6e 73 64 3b 20 20 20 20 20  .  int nsd;     
62700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
62710 20 4e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69   Number of signi
62720 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 72 65  ficant digits re
62730 74 75 72 6e 65 64 20 2a 2f 0a 23 65 6e 64 69 66  turned */.#endif
62740 0a 0a 20 20 66 75 6e 63 28 61 72 67 2c 22 22 2c  ..  func(arg,"",
62750 30 29 3b 0a 20 20 63 6f 75 6e 74 20 3d 20 6c 65  0);.  count = le
62760 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62 75 66 70  ngth = 0;.  bufp
62770 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28  t = 0;.  for(; (
62780 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b  c=(*fmt))!=0; ++
62790 66 6d 74 29 7b 0a 20 20 20 20 69 66 28 20 63 21  fmt){.    if( c!
627a0 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 69 6e  ='%' ){.      in
627b0 74 20 61 6d 74 3b 0a 20 20 20 20 20 20 62 75 66  t amt;.      buf
627c0 70 74 20 3d 20 28 63 68 61 72 20 2a 29 66 6d 74  pt = (char *)fmt
627d0 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d 20 31 3b  ;.      amt = 1;
627e0 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63  .      while( (c
627f0 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20  =(*++fmt))!='%' 
62800 26 26 20 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b  && c!=0 ) amt++;
62810 0a 20 20 20 20 20 20 28 2a 66 75 6e 63 29 28 61  .      (*func)(a
62820 72 67 2c 62 75 66 70 74 2c 61 6d 74 29 3b 0a 20  rg,bufpt,amt);. 
62830 20 20 20 20 20 63 6f 75 6e 74 20 2b 3d 20 61 6d       count += am
62840 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  t;.      if( c==
62850 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d  0 ) break;.    }
62860 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a 2b 2b  .    if( (c=(*++
62870 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  fmt))==0 ){.    
62880 20 20 65 72 72 6f 72 66 6c 61 67 20 3d 20 31 3b    errorflag = 1;
62890 0a 20 20 20 20 20 20 28 2a 66 75 6e 63 29 28 61  .      (*func)(a
628a0 72 67 2c 22 25 22 2c 31 29 3b 0a 20 20 20 20 20  rg,"%",1);.     
628b0 20 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20 20 20   count++;.      
628c0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
628d0 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61   /* Find out wha
628e0 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65 73  t flags are pres
628f0 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 5f  ent */.    flag_
62900 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66 6c  leftjustify = fl
62910 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 6c  ag_plussign = fl
62920 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 0a  ag_blanksign = .
62930 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e       flag_altern
62940 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f 61  ateform = flag_a
62950 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f 7a  ltform2 = flag_z
62960 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 20  eropad = 0;.    
62970 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 6f  done = 0;.    do
62980 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20  {.      switch( 
62990 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  c ){.        cas
629a0 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65  e '-':   flag_le
629b0 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 20  ftjustify = 1;  
629c0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
629d0 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66 6c    case '+':   fl
629e0 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b  ag_plussign = 1;
629f0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
62a00 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27 3a         case ' ':
62a10 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67     flag_blanksig
62a20 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72 65  n = 1;       bre
62a30 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
62a40 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74   '#':   flag_alt
62a50 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20  ernateform = 1; 
62a60 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
62a70 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c 61   case '!':   fla
62a80 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b 20  g_altform2 = 1; 
62a90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
62aa0 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a 20        case '0': 
62ab0 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d    flag_zeropad =
62ac0 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65 61   1;         brea
62ad0 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61 75  k;.        defau
62ae0 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 3b  lt:    done = 1;
62af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62b00 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
62b10 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f 6e      }while( !don
62b20 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 29  e && (c=(*++fmt)
62b30 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 47  )!=0 );.    /* G
62b40 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69 64  et the field wid
62b50 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 20  th */.    width 
62b60 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d  = 0;.    if( c==
62b70 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69 64  '*' ){.      wid
62b80 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69  th = va_arg(ap,i
62b90 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 77  nt);.      if( w
62ba0 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 20  idth<0 ){.      
62bb0 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69    flag_leftjusti
62bc0 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  fy = 1;.        
62bd0 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b 0a  width = -width;.
62be0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 20        }.      c 
62bf0 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d 65  = *++fmt;.    }e
62c00 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  lse{.      while
62c10 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27  ( c>='0' && c<='
62c20 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 69  9' ){.        wi
62c30 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20 2b  dth = width*10 +
62c40 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20   c - '0';.      
62c50 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20    c = *++fmt;.  
62c60 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
62c70 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42 55  if( width > etBU
62c80 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 20  FSIZE-10 ){.    
62c90 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46 53    width = etBUFS
62ca0 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 20  IZE-10;.    }.  
62cb0 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72 65    /* Get the pre
62cc0 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66  cision */.    if
62cd0 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20  ( c=='.' ){.    
62ce0 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b    precision = 0;
62cf0 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d  .      c = *++fm
62d00 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  t;.      if( c==
62d10 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 70  '*' ){.        p
62d20 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 72  recision = va_ar
62d30 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20  g(ap,int);.     
62d40 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e     if( precision
62d50 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d  <0 ) precision =
62d60 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20   -precision;.   
62d70 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b       c = *++fmt;
62d80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
62d90 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d        while( c>=
62da0 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b  '0' && c<='9' ){
62db0 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 69  .          preci
62dc0 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e  sion = precision
62dd0 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20  *10 + c - '0';. 
62de0 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b           c = *++
62df0 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  fmt;.        }. 
62e00 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
62e10 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f  {.      precisio
62e20 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20  n = -1;.    }.  
62e30 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6e    /* Get the con
62e40 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f 64  version type mod
62e50 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 28  ifier */.    if(
62e60 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20   c=='l' ){.     
62e70 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b 0a   flag_long = 1;.
62e80 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
62e90 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27  ;.      if( c=='
62ea0 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c  l' ){.        fl
62eb0 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 3b  ag_longlong = 1;
62ec0 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b  .        c = *++
62ed0 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  fmt;.      }else
62ee0 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c  {.        flag_l
62ef0 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20  onglong = 0;.   
62f00 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
62f10 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20        flag_long 
62f20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20  = flag_longlong 
62f30 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  = 0;.    }.    /
62f40 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66 6f  * Fetch the info
62f50 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66   entry for the f
62f60 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 6f  ield */.    info
62f70 70 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69  p = 0;.    for(i
62f80 64 78 3d 30 3b 20 69 64 78 3c 65 74 4e 49 4e 46  dx=0; idx<etNINF
62f90 4f 3b 20 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20  O; idx++){.     
62fa0 20 69 66 28 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b   if( c==fmtinfo[
62fb0 69 64 78 5d 2e 66 6d 74 74 79 70 65 20 29 7b 0a  idx].fmttype ){.
62fc0 20 20 20 20 20 20 20 20 69 6e 66 6f 70 20 3d 20          infop = 
62fd0 26 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20  &fmtinfo[idx];. 
62fe0 20 20 20 20 20 20 20 69 66 28 20 75 73 65 45 78         if( useEx
62ff0 74 65 6e 64 65 64 20 7c 7c 20 28 69 6e 66 6f 70  tended || (infop
63000 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 49  ->flags & FLAG_I
63010 4e 54 45 52 4e 29 3d 3d 30 20 29 7b 0a 20 20 20  NTERN)==0 ){.   
63020 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 69         xtype = i
63030 6e 66 6f 70 2d 3e 74 79 70 65 3b 0a 20 20 20 20  nfop->type;.    
63040 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
63050 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a       return -1;.
63060 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
63070 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
63080 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 78 74 72  .    }.    zExtr
63090 61 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 69  a = 0;.    if( i
630a0 6e 66 6f 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nfop==0 ){.     
630b0 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20   return -1;.    
630c0 7d 0a 0a 0a 20 20 20 20 2f 2a 20 4c 69 6d 69 74  }...    /* Limit
630d0 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20 74   the precision t
630e0 6f 20 70 72 65 76 65 6e 74 20 6f 76 65 72 66 6c  o prevent overfl
630f0 6f 77 69 6e 67 20 62 75 66 5b 5d 20 64 75 72 69  owing buf[] duri
63100 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f  ng conversion */
63110 0a 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69  .    if( precisi
63120 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2d 34 30 20  on>etBUFSIZE-40 
63130 26 26 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73  && (infop->flags
63140 20 26 20 46 4c 41 47 5f 53 54 52 49 4e 47 29 3d   & FLAG_STRING)=
63150 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63  =0 ){.      prec
63160 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a  ision = etBUFSIZ
63170 45 2d 34 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  E-40;.    }..   
63180 20 2f 2a 0a 20 20 20 20 2a 2a 20 41 74 20 74 68   /*.    ** At th
63190 69 73 20 70 6f 69 6e 74 2c 20 76 61 72 69 61 62  is point, variab
631a0 6c 65 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69  les are initiali
631b0 7a 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  zed as follows:.
631c0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
631d0 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
631e0 72 6d 20 20 20 20 20 20 20 20 20 20 54 52 55 45  rm          TRUE
631f0 20 69 66 20 61 20 27 23 27 20 69 73 20 70 72 65   if a '#' is pre
63200 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66  sent..    **   f
63210 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 20 20 20  lag_altform2    
63220 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20             TRUE 
63230 69 66 20 61 20 27 21 27 20 69 73 20 70 72 65 73  if a '!' is pres
63240 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c  ent..    **   fl
63250 61 67 5f 70 6c 75 73 73 69 67 6e 20 20 20 20 20  ag_plussign     
63260 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69            TRUE i
63270 66 20 61 20 27 2b 27 20 69 73 20 70 72 65 73 65  f a '+' is prese
63280 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61  nt..    **   fla
63290 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 20 20  g_leftjustify   
632a0 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66           TRUE if
632b0 20 61 20 27 2d 27 20 69 73 20 70 72 65 73 65 6e   a '-' is presen
632c0 74 20 6f 72 20 69 66 20 74 68 65 0a 20 20 20 20  t or if the.    
632d0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
632e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
632f0 20 66 69 65 6c 64 20 77 69 64 74 68 20 77 61 73   field width was
63300 20 6e 65 67 61 74 69 76 65 2e 0a 20 20 20 20 2a   negative..    *
63310 2a 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  *   flag_zeropad
63320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63330 54 52 55 45 20 69 66 20 74 68 65 20 77 69 64 74  TRUE if the widt
63340 68 20 62 65 67 61 6e 20 77 69 74 68 20 30 2e 0a  h began with 0..
63350 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f      **   flag_lo
63360 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ng              
63370 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65       TRUE if the
63380 20 6c 65 74 74 65 72 20 27 6c 27 20 28 65 6c 6c   letter 'l' (ell
63390 29 20 70 72 65 66 69 78 65 64 0a 20 20 20 20 2a  ) prefixed.    *
633a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
633b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
633c0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63  the conversion c
633d0 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a  haracter..    **
633e0 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67     flag_longlong
633f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
63400 52 55 45 20 69 66 20 74 68 65 20 6c 65 74 74 65  RUE if the lette
63410 72 20 27 6c 6c 27 20 28 65 6c 6c 20 65 6c 6c 29  r 'll' (ell ell)
63420 20 70 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a   prefixed.    **
63430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
63450 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68  he conversion ch
63460 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
63470 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e    flag_blanksign
63480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52                TR
63490 55 45 20 69 66 20 61 20 27 20 27 20 69 73 20 70  UE if a ' ' is p
634a0 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20  resent..    **  
634b0 20 77 69 64 74 68 20 20 20 20 20 20 20 20 20 20   width          
634c0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65               The
634d0 20 73 70 65 63 69 66 69 65 64 20 66 69 65 6c 64   specified field
634e0 20 77 69 64 74 68 2e 20 20 54 68 69 73 20 69 73   width.  This is
634f0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
63500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63510 20 20 20 20 20 20 61 6c 77 61 79 73 20 6e 6f 6e        always non
63520 2d 6e 65 67 61 74 69 76 65 2e 20 20 5a 65 72 6f  -negative.  Zero
63530 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e   is the default.
63540 0a 20 20 20 20 2a 2a 20 20 20 70 72 65 63 69 73  .    **   precis
63550 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
63560 20 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66        The specif
63570 69 65 64 20 70 72 65 63 69 73 69 6f 6e 2e 20 20  ied precision.  
63580 54 68 65 20 64 65 66 61 75 6c 74 0a 20 20 20 20  The default.    
63590 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
635a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
635b0 20 69 73 20 2d 31 2e 0a 20 20 20 20 2a 2a 20 20   is -1..    **  
635c0 20 78 74 79 70 65 20 20 20 20 20 20 20 20 20 20   xtype          
635d0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65               The
635e0 20 63 6c 61 73 73 20 6f 66 20 74 68 65 20 63 6f   class of the co
635f0 6e 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a 2a  nversion..    **
63600 20 20 20 69 6e 66 6f 70 20 20 20 20 20 20 20 20     infop        
63610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
63620 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70  ointer to the ap
63630 70 72 6f 70 72 69 61 74 65 20 69 6e 66 6f 20 73  propriate info s
63640 74 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  truct..    */.  
63650 20 20 73 77 69 74 63 68 28 20 78 74 79 70 65 20    switch( xtype 
63660 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  ){.      case et
63670 50 4f 49 4e 54 45 52 3a 0a 20 20 20 20 20 20 20  POINTER:.       
63680 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d   flag_longlong =
63690 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d   sizeof(char*)==
636a0 73 69 7a 65 6f 66 28 69 36 34 29 3b 0a 20 20 20  sizeof(i64);.   
636b0 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d       flag_long =
636c0 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d   sizeof(char*)==
636d0 73 69 7a 65 6f 66 28 6c 6f 6e 67 20 69 6e 74 29  sizeof(long int)
636e0 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c  ;.        /* Fal
636f0 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74  l through into t
63700 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a  he next case */.
63710 20 20 20 20 20 20 63 61 73 65 20 65 74 52 41 44        case etRAD
63720 49 58 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20  IX:.        if( 
63730 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46  infop->flags & F
63740 4c 41 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20  LAG_SIGNED ){.  
63750 20 20 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20          i64 v;. 
63760 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61           if( fla
63770 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 20 20 20 76  g_longlong )   v
63780 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 36 34   = va_arg(ap,i64
63790 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73  );.          els
637a0 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20  e if( flag_long 
637b0 29 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61 70  )  v = va_arg(ap
637c0 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20  ,long int);.    
637d0 20 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20        else      
637e0 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20              v = 
637f0 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a  va_arg(ap,int);.
63800 20 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c            if( v<
63810 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
63820 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b   longvalue = -v;
63830 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65  .            pre
63840 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20  fix = '-';.     
63850 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
63860 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75          longvalu
63870 65 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20  e = v;.         
63880 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73     if( flag_plus
63890 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72  sign )        pr
638a0 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20  efix = '+';.    
638b0 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28          else if(
638c0 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20   flag_blanksign 
638d0 29 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b  )  prefix = ' ';
638e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73  .            els
638f0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
63900 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
63910 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   0;.          }.
63920 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
63930 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61           if( fla
63940 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 20 20 20 6c  g_longlong )   l
63950 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72  ongvalue = va_ar
63960 67 28 61 70 2c 75 36 34 29 3b 0a 20 20 20 20 20  g(ap,u64);.     
63970 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c       else if( fl
63980 61 67 5f 6c 6f 6e 67 20 29 20 20 6c 6f 6e 67 76  ag_long )  longv
63990 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70  alue = va_arg(ap
639a0 2c 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 69  ,unsigned long i
639b0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65  nt);.          e
639c0 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
639d0 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
639e0 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67   va_arg(ap,unsig
639f0 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ned int);.      
63a00 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a      prefix = 0;.
63a10 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
63a20 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d    if( longvalue=
63a30 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e  =0 ) flag_altern
63a40 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20  ateform = 0;.   
63a50 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65       if( flag_ze
63a60 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73 69  ropad && precisi
63a70 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69 78  on<width-(prefix
63a80 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  !=0) ){.        
63a90 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 69    precision = wi
63aa0 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 3b  dth-(prefix!=0);
63ab0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
63ac0 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66 5b     bufpt = &buf[
63ad0 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20  etBUFSIZE-1];.  
63ae0 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
63af0 20 20 72 65 67 69 73 74 65 72 20 63 6f 6e 73 74    register const
63b00 20 63 68 61 72 20 2a 63 73 65 74 3b 20 20 20 20   char *cset;    
63b10 20 20 2f 2a 20 55 73 65 20 72 65 67 69 73 74 65    /* Use registe
63b20 72 73 20 66 6f 72 20 73 70 65 65 64 20 2a 2f 0a  rs for speed */.
63b30 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74            regist
63b40 65 72 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20  er int base;.   
63b50 20 20 20 20 20 20 20 63 73 65 74 20 3d 20 26 61         cset = &a
63b60 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68  Digits[infop->ch
63b70 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20  arset];.        
63b80 20 20 62 61 73 65 20 3d 20 69 6e 66 6f 70 2d 3e    base = infop->
63b90 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  base;.          
63ba0 64 6f 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  do{             
63bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
63bd0 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 73 63 69   Convert to asci
63be0 69 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  i */.           
63bf0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 63 73   *(--bufpt) = cs
63c00 65 74 5b 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73  et[longvalue%bas
63c10 65 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e];.            
63c20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67  longvalue = long
63c30 76 61 6c 75 65 2f 62 61 73 65 3b 0a 20 20 20 20  value/base;.    
63c40 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f        }while( lo
63c50 6e 67 76 61 6c 75 65 3e 30 20 29 3b 0a 20 20 20  ngvalue>0 );.   
63c60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c       }.        l
63c70 65 6e 67 74 68 20 3d 20 26 62 75 66 5b 65 74 42  ength = &buf[etB
63c80 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 3b  UFSIZE-1]-bufpt;
63c90 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78  .        for(idx
63ca0 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74  =precision-lengt
63cb0 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29  h; idx>0; idx--)
63cc0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d  {.          *(--
63cd0 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20 20  bufpt) = '0';   
63ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63cf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 72            /* Zer
63d00 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 20  o pad */.       
63d10 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70   }.        if( p
63d20 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 70  refix ) *(--bufp
63d30 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20 20  t) = prefix;    
63d40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
63d50 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20  d sign */.      
63d60 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 72    if( flag_alter
63d70 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f  nateform && info
63d80 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20 20  p->prefix ){    
63d90 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 20    /* Add "0" or 
63da0 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 20  "0x" */.        
63db0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72    const char *pr
63dc0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  e;.          cha
63dd0 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 70  r x;.          p
63de0 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69 6e  re = &aPrefix[in
63df0 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20  fop->prefix];.  
63e00 20 20 20 20 20 20 20 20 69 66 28 20 2a 62 75 66          if( *buf
63e10 70 74 21 3d 70 72 65 5b 30 5d 20 29 7b 0a 20 20  pt!=pre[0] ){.  
63e20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20            for(; 
63e30 28 78 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70  (x=(*pre))!=0; p
63e40 72 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29  re++) *(--bufpt)
63e50 20 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20   = x;.          
63e60 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
63e70 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 26 62 75      length = &bu
63e80 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62  f[etBUFSIZE-1]-b
63e90 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20 62 72  ufpt;.        br
63ea0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
63eb0 65 74 46 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63  etFLOAT:.      c
63ec0 61 73 65 20 65 74 45 58 50 3a 0a 20 20 20 20 20  ase etEXP:.     
63ed0 20 63 61 73 65 20 65 74 47 45 4e 45 52 49 43 3a   case etGENERIC:
63ee0 0a 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c  .        realval
63ef0 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 64  ue = va_arg(ap,d
63f00 6f 75 62 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20  ouble);.#ifndef 
63f10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
63f20 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20  TING_POINT.     
63f30 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e     if( precision
63f40 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d  <0 ) precision =
63f50 20 36 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53   6;         /* S
63f60 65 74 20 64 65 66 61 75 6c 74 20 70 72 65 63 69  et default preci
63f70 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  sion */.        
63f80 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74  if( precision>et
63f90 42 55 46 53 49 5a 45 2f 32 2d 31 30 20 29 20 70  BUFSIZE/2-10 ) p
63fa0 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46  recision = etBUF
63fb0 53 49 5a 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20  SIZE/2-10;.     
63fc0 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65     if( realvalue
63fd0 3c 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  <0.0 ){.        
63fe0 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72    realvalue = -r
63ff0 65 61 6c 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  ealvalue;.      
64000 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27      prefix = '-'
64010 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
64020 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66  .          if( f
64030 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20  lag_plussign )  
64040 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
64050 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '+';.          
64060 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c  else if( flag_bl
64070 61 6e 6b 73 69 67 6e 20 29 20 20 20 20 70 72 65  anksign )    pre
64080 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20  fix = ' ';.     
64090 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20       else       
640a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
640b0 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20    prefix = 0;.  
640c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
640d0 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e  if( xtype==etGEN
640e0 45 52 49 43 20 26 26 20 70 72 65 63 69 73 69 6f  ERIC && precisio
640f0 6e 3e 30 20 29 20 70 72 65 63 69 73 69 6f 6e 2d  n>0 ) precision-
64100 2d 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20  -;.#if 0.       
64110 20 2f 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72   /* Rounding wor
64120 6b 73 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e  ks like BSD when
64130 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e   the constant 0.
64140 34 39 39 39 20 69 73 20 75 73 65 64 2e 20 20 57  4999 is used.  W
64150 69 65 72 64 21 20 2a 2f 0a 20 20 20 20 20 20 20  ierd! */.       
64160 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69   for(idx=precisi
64170 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39  on, rounder=0.49
64180 39 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d  99; idx>0; idx--
64190 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b  , rounder*=0.1);
641a0 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f  .#else.        /
641b0 2a 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20  * It makes more 
641c0 73 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e 35  sense to use 0.5
641d0 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28   */.        for(
641e0 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72  idx=precision, r
641f0 6f 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e  ounder=0.5; idx>
64200 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65  0; idx--, rounde
64210 72 2a 3d 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66  r*=0.1){}.#endif
64220 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
64230 70 65 3d 3d 65 74 46 4c 4f 41 54 20 29 20 72 65  pe==etFLOAT ) re
64240 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64  alvalue += round
64250 65 72 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e  er;.        /* N
64260 6f 72 6d 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c  ormalize realval
64270 75 65 20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e  ue to within 10.
64280 30 20 3e 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d  0 > realvalue >=
64290 20 31 2e 30 20 2a 2f 0a 20 20 20 20 20 20 20 20   1.0 */.        
642a0 65 78 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  exp = 0;.       
642b0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 73 6e   if( sqlite3_isn
642c0 61 6e 28 72 65 61 6c 76 61 6c 75 65 29 20 29 7b  an(realvalue) ){
642d0 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74  .          bufpt
642e0 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20   = "NaN";.      
642f0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a      length = 3;.
64300 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
64310 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
64320 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65     if( realvalue
64330 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  >0.0 ){.        
64340 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c    while( realval
64350 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70 3c  ue>=1e32 && exp<
64360 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75  =350 ){ realvalu
64370 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b  e *= 1e-32; exp+
64380 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  =32; }.         
64390 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75   while( realvalu
643a0 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d 33  e>=1e8 && exp<=3
643b0 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20  50 ){ realvalue 
643c0 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b  *= 1e-8; exp+=8;
643d0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
643e0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31  le( realvalue>=1
643f0 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30 20  0.0 && exp<=350 
64400 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
64410 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20  0.1; exp++; }.  
64420 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
64430 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 26 26  ealvalue<1e-8 &&
64440 20 65 78 70 3e 3d 2d 33 35 30 20 29 7b 20 72 65   exp>=-350 ){ re
64450 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20  alvalue *= 1e8; 
64460 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20  exp-=8; }.      
64470 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76      while( realv
64480 61 6c 75 65 3c 31 2e 30 20 26 26 20 65 78 70 3e  alue<1.0 && exp>
64490 3d 2d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c  =-350 ){ realval
644a0 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78 70 2d  ue *= 10.0; exp-
644b0 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69  -; }.          i
644c0 66 28 20 65 78 70 3e 33 35 30 20 7c 7c 20 65 78  f( exp>350 || ex
644d0 70 3c 2d 33 35 30 20 29 7b 0a 20 20 20 20 20 20  p<-350 ){.      
644e0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78        if( prefix
644f0 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20  =='-' ){.       
64500 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22         bufpt = "
64510 2d 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20  -Inf";.         
64520 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 72 65     }else if( pre
64530 66 69 78 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20  fix=='+' ){.    
64540 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20            bufpt 
64550 3d 20 22 2b 49 6e 66 22 3b 0a 20 20 20 20 20 20  = "+Inf";.      
64560 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
64570 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74             bufpt
64580 20 3d 20 22 49 6e 66 22 3b 0a 20 20 20 20 20 20   = "Inf";.      
64590 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
645a0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 74 72      length = str
645b0 6c 65 6e 28 62 75 66 70 74 29 3b 0a 20 20 20 20  len(bufpt);.    
645c0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
645d0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
645e0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66     }.        buf
645f0 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20  pt = buf;.      
64600 20 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    /*.        ** 
64610 49 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70  If the field typ
64620 65 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20  e is etGENERIC, 
64630 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20  then convert to 
64640 65 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20  either etEXP.   
64650 20 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f       ** or etFLO
64660 41 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61  AT, as appropria
64670 74 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  te..        */. 
64680 20 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20         flag_exp 
64690 3d 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a  = xtype==etEXP;.
646a0 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70          if( xtyp
646b0 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20  e!=etFLOAT ){.  
646c0 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75          realvalu
646d0 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20  e += rounder;.  
646e0 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c          if( real
646f0 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72  value>=10.0 ){ r
64700 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b  ealvalue *= 0.1;
64710 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20   exp++; }.      
64720 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
64730 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43  xtype==etGENERIC
64740 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c   ){.          fl
64750 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61  ag_rtz = !flag_a
64760 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20  lternateform;.  
64770 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c          if( exp<
64780 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73  -4 || exp>precis
64790 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ion ){.         
647a0 20 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50     xtype = etEXP
647b0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
647c0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  e{.            p
647d0 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69  recision = preci
647e0 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20  sion - exp;.    
647f0 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20          xtype = 
64800 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20  etFLOAT;.       
64810 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
64820 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c  se{.          fl
64830 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20  ag_rtz = 0;.    
64840 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
64850 28 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29  ( xtype==etEXP )
64860 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d  {.          e2 =
64870 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   0;.        }els
64880 65 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20  e{.          e2 
64890 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d  = exp;.        }
648a0 0a 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30  .        nsd = 0
648b0 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64  ;.        flag_d
648c0 70 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30  p = (precision>0
648d0 29 20 7c 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61  ) | flag_alterna
648e0 74 65 66 6f 72 6d 20 7c 20 66 6c 61 67 5f 61 6c  teform | flag_al
648f0 74 66 6f 72 6d 32 3b 0a 20 20 20 20 20 20 20 20  tform2;.        
64900 2f 2a 20 54 68 65 20 73 69 67 6e 20 69 6e 20 66  /* The sign in f
64910 72 6f 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62  ront of the numb
64920 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  er */.        if
64930 28 20 70 72 65 66 69 78 20 29 7b 0a 20 20 20 20  ( prefix ){.    
64940 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
64950 20 3d 20 70 72 65 66 69 78 3b 0a 20 20 20 20 20   = prefix;.     
64960 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
64970 44 69 67 69 74 73 20 70 72 69 6f 72 20 74 6f 20  Digits prior to 
64980 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e  the decimal poin
64990 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  t */.        if(
649a0 20 65 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 20   e2<0 ){.       
649b0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
649c0 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  '0';.        }el
649d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  se{.          fo
649e0 72 28 3b 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29  r(; e2>=0; e2--)
649f0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28  {.            *(
64a00 62 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65  bufpt++) = et_ge
64a10 74 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75  tdigit(&realvalu
64a20 65 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20  e,&nsd);.       
64a30 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
64a40 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65         /* The de
64a50 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20  cimal point */. 
64a60 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
64a70 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  dp ){.          
64a80 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2e 27  *(bufpt++) = '.'
64a90 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
64aa0 20 20 20 20 2f 2a 20 22 30 22 20 64 69 67 69 74      /* "0" digit
64ab0 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69  s after the deci
64ac0 6d 61 6c 20 70 6f 69 6e 74 20 62 75 74 20 62 65  mal point but be
64ad0 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 20  fore the first. 
64ae0 20 20 20 20 20 20 20 2a 2a 20 73 69 67 6e 69 66         ** signif
64af0 69 63 61 6e 74 20 64 69 67 69 74 20 6f 66 20 74  icant digit of t
64b00 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20  he number */.   
64b10 20 20 20 20 20 66 6f 72 28 65 32 2b 2b 3b 20 65       for(e2++; e
64b20 32 3c 30 20 26 26 20 70 72 65 63 69 73 69 6f 6e  2<0 && precision
64b30 3e 30 3b 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c  >0; precision--,
64b40 20 65 32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   e2++){.        
64b50 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
64b60 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0';.        }.  
64b70 20 20 20 20 20 20 2f 2a 20 53 69 67 6e 69 66 69        /* Signifi
64b80 63 61 6e 74 20 64 69 67 69 74 73 20 61 66 74 65  cant digits afte
64b90 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f  r the decimal po
64ba0 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 77  int */.        w
64bb0 68 69 6c 65 28 20 28 70 72 65 63 69 73 69 6f 6e  hile( (precision
64bc0 2d 2d 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  --)>0 ){.       
64bd0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
64be0 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61  et_getdigit(&rea
64bf0 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20  lvalue,&nsd);.  
64c00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
64c10 2f 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c 69  /* Remove traili
64c20 6e 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68 65  ng zeros and the
64c30 20 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69 74   "." if no digit
64c40 73 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e 22  s follow the "."
64c50 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
64c60 66 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c 61 67  flag_rtz && flag
64c70 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _dp ){.         
64c80 20 77 68 69 6c 65 28 20 62 75 66 70 74 5b 2d 31   while( bufpt[-1
64c90 5d 3d 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75 66  ]=='0' ) *(--buf
64ca0 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  pt) = 0;.       
64cb0 20 20 20 61 73 73 65 72 74 28 20 62 75 66 70 74     assert( bufpt
64cc0 3e 62 75 66 20 29 3b 0a 20 20 20 20 20 20 20 20  >buf );.        
64cd0 20 20 69 66 28 20 62 75 66 70 74 5b 2d 31 5d 3d    if( bufpt[-1]=
64ce0 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='.' ){.        
64cf0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74      if( flag_alt
64d00 66 6f 72 6d 32 20 29 7b 0a 20 20 20 20 20 20 20  form2 ){.       
64d10 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
64d20 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  ) = '0';.       
64d30 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
64d40 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75            *(--bu
64d50 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20  fpt) = 0;.      
64d60 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
64d70 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
64d80 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65        /* Add the
64d90 20 22 65 4e 4e 4e 22 20 73 75 66 66 69 78 20 2a   "eNNN" suffix *
64da0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c  /.        if( fl
64db0 61 67 5f 65 78 70 20 7c 7c 20 28 78 74 79 70 65  ag_exp || (xtype
64dc0 3d 3d 65 74 45 58 50 20 26 26 20 65 78 70 29 20  ==etEXP && exp) 
64dd0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  ){.          *(b
64de0 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74  ufpt++) = aDigit
64df0 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74  s[infop->charset
64e00 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
64e10 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20   exp<0 ){.      
64e20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
64e30 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65   = '-'; exp = -e
64e40 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  xp;.          }e
64e50 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
64e60 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b   *(bufpt++) = '+
64e70 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  ';.          }. 
64e80 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70           if( exp
64e90 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20  >=100 ){.       
64ea0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
64eb0 3d 20 28 65 78 70 2f 31 30 30 29 2b 27 30 27 3b  = (exp/100)+'0';
64ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64ed0 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74 20 2a  /* 100's digit *
64ee0 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 78  /.            ex
64ef0 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20  p %= 100;.      
64f00 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
64f10 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 78 70  *(bufpt++) = exp
64f20 2f 31 30 2b 27 30 27 3b 20 20 20 20 20 20 20 20  /10+'0';        
64f30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
64f40 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20  10's digit */.  
64f50 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
64f60 2b 29 20 3d 20 65 78 70 25 31 30 2b 27 30 27 3b  +) = exp%10+'0';
64f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64f80 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69       /* 1's digi
64f90 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  t */.        }. 
64fa0 20 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20         *bufpt = 
64fb0 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  0;..        /* T
64fc0 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d  he converted num
64fd0 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20  ber is in buf[] 
64fe0 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  and zero termina
64ff0 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a  ted. Output it..
65000 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20          ** Note 
65010 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20  that the number 
65020 69 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20  is in the usual 
65030 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72  order, not rever
65040 73 65 64 20 61 73 20 77 69 74 68 0a 20 20 20 20  sed as with.    
65050 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63      ** integer c
65060 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20  onversions. */. 
65070 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
65080 62 75 66 70 74 2d 62 75 66 3b 0a 20 20 20 20 20  bufpt-buf;.     
65090 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
650a0 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63  .        /* Spec
650b0 69 61 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c  ial case:  Add l
650c0 65 61 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20  eading zeros if 
650d0 74 68 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  the flag_zeropad
650e0 20 66 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20   flag is.       
650f0 20 2a 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61   ** set and we a
65100 72 65 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74  re not left just
65110 69 66 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20  ified */.       
65120 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61   if( flag_zeropa
65130 64 20 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a  d && !flag_leftj
65140 75 73 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68  ustify && length
65150 20 3c 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20   < width){.     
65160 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
65170 20 20 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d        int nPad =
65180 20 77 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b   width - length;
65190 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69  .          for(i
651a0 3d 77 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b  =width; i>=nPad;
651b0 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20   i--){.         
651c0 20 20 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75     bufpt[i] = bu
651d0 66 70 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20  fpt[i-nPad];.   
651e0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
651f0 20 20 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30     i = prefix!=0
65200 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  ;.          whil
65210 65 28 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70  e( nPad-- ) bufp
65220 74 5b 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20  t[i++] = '0';.  
65230 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
65240 20 77 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20   width;.        
65250 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
65260 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
65270 73 65 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20  se etSIZE:.     
65280 20 20 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69     *(va_arg(ap,i
65290 6e 74 2a 29 29 20 3d 20 63 6f 75 6e 74 3b 0a 20  nt*)) = count;. 
652a0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
652b0 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20  width = 0;.     
652c0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
652d0 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a 0a  case etPERCENT:.
652e0 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d          buf[0] =
652f0 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62 75   '%';.        bu
65300 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20  fpt = buf;.     
65310 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20     length = 1;. 
65320 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
65330 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52 4c      case etCHARL
65340 49 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  IT:.      case e
65350 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20 20  tCHARX:.        
65360 63 20 3d 20 62 75 66 5b 30 5d 20 3d 20 28 78 74  c = buf[0] = (xt
65370 79 70 65 3d 3d 65 74 43 48 41 52 58 20 3f 20 76  ype==etCHARX ? v
65380 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 20 3a 20  a_arg(ap,int) : 
65390 2a 2b 2b 66 6d 74 29 3b 0a 20 20 20 20 20 20 20  *++fmt);.       
653a0 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d   if( precision>=
653b0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  0 ){.          f
653c0 6f 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72  or(idx=1; idx<pr
653d0 65 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20  ecision; idx++) 
653e0 62 75 66 5b 69 64 78 5d 20 3d 20 63 3b 0a 20 20  buf[idx] = c;.  
653f0 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
65400 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20   precision;.    
65410 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
65420 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b 0a       length =1;.
65430 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
65440 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20    bufpt = buf;. 
65450 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
65460 20 20 20 20 63 61 73 65 20 65 74 53 54 52 49 4e      case etSTRIN
65470 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  G:.      case et
65480 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20 20  DYNSTRING:.     
65490 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f 61 72     bufpt = va_ar
654a0 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 20  g(ap,char*);.   
654b0 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d       if( bufpt==
654c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  0 ){.          b
654d0 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20 20 20  ufpt = "";.     
654e0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78 74 79     }else if( xty
654f0 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47 20  pe==etDYNSTRING 
65500 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45 78  ){.          zEx
65510 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20 20 20  tra = bufpt;.   
65520 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c       }.        l
65530 65 6e 67 74 68 20 3d 20 73 74 72 6c 65 6e 28 62  ength = strlen(b
65540 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 69  ufpt);.        i
65550 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20  f( precision>=0 
65560 26 26 20 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e  && precision<len
65570 67 74 68 20 29 20 6c 65 6e 67 74 68 20 3d 20 70  gth ) length = p
65580 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20  recision;.      
65590 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
655a0 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a  ase etSQLESCAPE:
655b0 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51  .      case etSQ
655c0 4c 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20  LESCAPE2:.      
655d0 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45  case etSQLESCAPE
655e0 33 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  3: {.        int
655f0 20 69 2c 20 6a 2c 20 6e 2c 20 63 68 2c 20 69 73   i, j, n, ch, is
65600 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e  null;.        in
65610 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 20  t needQuote;.   
65620 20 20 20 20 20 63 68 61 72 20 71 20 3d 20 28 28       char q = ((
65630 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41  xtype==etSQLESCA
65640 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20  PE3)?'"':'\''); 
65650 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 72 61    /* Quote chara
65660 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  cter */.        
65670 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d 20 76  char *escarg = v
65680 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b  a_arg(ap,char*);
65690 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c 6c 20  .        isnull 
656a0 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20  = escarg==0;.   
656b0 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c 6c 20       if( isnull 
656c0 29 20 65 73 63 61 72 67 20 3d 20 28 78 74 79 70  ) escarg = (xtyp
656d0 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 20  e==etSQLESCAPE2 
656e0 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c  ? "NULL" : "(NUL
656f0 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20 66 6f  L)");.        fo
65700 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63  r(i=n=0; (ch=esc
65710 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  arg[i])!=0; i++)
65720 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
65730 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20 20  ch==q )  n++;.  
65740 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
65750 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69 73 6e  needQuote = !isn
65760 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d 65 74  ull && xtype==et
65770 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 20 20  SQLESCAPE2;.    
65780 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 20 2b      n += i + 1 +
65790 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a 20 20   needQuote*2;.  
657a0 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74 42 55        if( n>etBU
657b0 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20  FSIZE ){.       
657c0 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78 74 72     bufpt = zExtr
657d0 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  a = sqlite3_mall
657e0 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20  oc( n );.       
657f0 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20     if( bufpt==0 
65800 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20  ) return -1;.   
65810 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
65820 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75        bufpt = bu
65830 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  f;.        }.   
65840 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20       j = 0;.    
65850 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74      if( needQuot
65860 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d  e ) bufpt[j++] =
65870 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   q;.        for(
65880 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67 5b  i=0; (ch=escarg[
65890 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20  i])!=0; i++){.  
658a0 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 2b          bufpt[j+
658b0 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20  +] = ch;.       
658c0 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 62     if( ch==q ) b
658d0 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a  ufpt[j++] = ch;.
658e0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
658f0 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20    if( needQuote 
65900 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71  ) bufpt[j++] = q
65910 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 5b  ;.        bufpt[
65920 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  j] = 0;.        
65930 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20  length = j;.    
65940 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 63 69      /* The preci
65950 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65 64 20  sion is ignored 
65960 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a  on %q and %Q */.
65970 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70          /* if( p
65980 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70  recision>=0 && p
65990 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20  recision<length 
659a0 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 69  ) length = preci
659b0 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20  sion; */.       
659c0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
659d0 20 20 20 20 20 20 63 61 73 65 20 65 74 54 4f 4b        case etTOK
659e0 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 54 6f  EN: {.        To
659f0 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61  ken *pToken = va
65a00 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29  _arg(ap, Token*)
65a10 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54  ;.        if( pT
65a20 6f 6b 65 6e 20 26 26 20 70 54 6f 6b 65 6e 2d 3e  oken && pToken->
65a30 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 28  z ){.          (
65a40 2a 66 75 6e 63 29 28 61 72 67 2c 20 28 63 68 61  *func)(arg, (cha
65a50 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54  r*)pToken->z, pT
65a60 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20  oken->n);.      
65a70 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67    }.        leng
65a80 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a  th = width = 0;.
65a90 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
65aa0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
65ab0 65 20 65 74 53 52 43 4c 49 53 54 3a 20 7b 0a 20  e etSRCLIST: {. 
65ac0 20 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a         SrcList *
65ad0 70 53 72 63 20 3d 20 76 61 5f 61 72 67 28 61 70  pSrc = va_arg(ap
65ae0 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 20 20 20  , SrcList*);.   
65af0 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 76 61 5f       int k = va_
65b00 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
65b10 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63        struct Src
65b20 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
65b30 20 3d 20 26 70 53 72 63 2d 3e 61 5b 6b 5d 3b 0a   = &pSrc->a[k];.
65b40 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
65b50 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 72 63 2d 3e  k>=0 && k<pSrc->
65b60 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20 20 20  nSrc );.        
65b70 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61  if( pItem->zData
65b80 62 61 73 65 20 26 26 20 70 49 74 65 6d 2d 3e 7a  base && pItem->z
65b90 44 61 74 61 62 61 73 65 5b 30 5d 20 29 7b 0a 20  Database[0] ){. 
65ba0 20 20 20 20 20 20 20 20 20 28 2a 66 75 6e 63 29           (*func)
65bb0 28 61 72 67 2c 20 70 49 74 65 6d 2d 3e 7a 44 61  (arg, pItem->zDa
65bc0 74 61 62 61 73 65 2c 20 73 74 72 6c 65 6e 28 70  tabase, strlen(p
65bd0 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29  Item->zDatabase)
65be0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 28 2a 66  );.          (*f
65bf0 75 6e 63 29 28 61 72 67 2c 20 22 2e 22 2c 20 31  unc)(arg, ".", 1
65c00 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
65c10 20 20 20 20 20 28 2a 66 75 6e 63 29 28 61 72 67       (*func)(arg
65c20 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20  , pItem->zName, 
65c30 73 74 72 6c 65 6e 28 70 49 74 65 6d 2d 3e 7a 4e  strlen(pItem->zN
65c40 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 20 20 6c  ame));.        l
65c50 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20  ength = width = 
65c60 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  0;.        break
65c70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 2f  ;.      }.    }/
65c80 2a 20 45 6e 64 20 73 77 69 74 63 68 20 6f 76 65  * End switch ove
65c90 72 20 74 68 65 20 66 6f 72 6d 61 74 20 74 79 70  r the format typ
65ca0 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20 20 20 20  e */.    /*.    
65cb0 2a 2a 20 54 68 65 20 74 65 78 74 20 6f 66 20 74  ** The text of t
65cc0 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  he conversion is
65cd0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 22   pointed to by "
65ce0 62 75 66 70 74 22 20 61 6e 64 20 69 73 0a 20 20  bufpt" and is.  
65cf0 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22 20 63 68    ** "length" ch
65d00 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20  aracters long.  
65d10 54 68 65 20 66 69 65 6c 64 20 77 69 64 74 68 20  The field width 
65d20 69 73 20 22 77 69 64 74 68 22 2e 20 20 44 6f 0a  is "width".  Do.
65d30 20 20 20 20 2a 2a 20 74 68 65 20 6f 75 74 70 75      ** the outpu
65d40 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  t..    */.    if
65d50 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74  ( !flag_leftjust
65d60 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72 65 67  ify ){.      reg
65d70 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61 63 65  ister int nspace
65d80 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65 20 3d  ;.      nspace =
65d90 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20   width-length;. 
65da0 20 20 20 20 20 69 66 28 20 6e 73 70 61 63 65 3e       if( nspace>
65db0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 75  0 ){.        cou
65dc0 6e 74 20 2b 3d 20 6e 73 70 61 63 65 3b 0a 20 20  nt += nspace;.  
65dd0 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 73 70        while( nsp
65de0 61 63 65 3e 3d 65 74 53 50 41 43 45 53 49 5a 45  ace>=etSPACESIZE
65df0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 28 2a   ){.          (*
65e00 66 75 6e 63 29 28 61 72 67 2c 73 70 61 63 65 73  func)(arg,spaces
65e10 2c 65 74 53 50 41 43 45 53 49 5a 45 29 3b 0a 20  ,etSPACESIZE);. 
65e20 20 20 20 20 20 20 20 20 20 6e 73 70 61 63 65 20           nspace 
65e30 2d 3d 20 65 74 53 50 41 43 45 53 49 5a 45 3b 0a  -= etSPACESIZE;.
65e40 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
65e50 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29    if( nspace>0 )
65e60 20 28 2a 66 75 6e 63 29 28 61 72 67 2c 73 70 61   (*func)(arg,spa
65e70 63 65 73 2c 6e 73 70 61 63 65 29 3b 0a 20 20 20  ces,nspace);.   
65e80 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
65e90 66 28 20 6c 65 6e 67 74 68 3e 30 20 29 7b 0a 20  f( length>0 ){. 
65ea0 20 20 20 20 20 28 2a 66 75 6e 63 29 28 61 72 67       (*func)(arg
65eb0 2c 62 75 66 70 74 2c 6c 65 6e 67 74 68 29 3b 0a  ,bufpt,length);.
65ec0 20 20 20 20 20 20 63 6f 75 6e 74 20 2b 3d 20 6c        count += l
65ed0 65 6e 67 74 68 3b 0a 20 20 20 20 7d 0a 20 20 20  ength;.    }.   
65ee0 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 6a 75   if( flag_leftju
65ef0 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72  stify ){.      r
65f00 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61  egister int nspa
65f10 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65  ce;.      nspace
65f20 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b   = width-length;
65f30 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63  .      if( nspac
65f40 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  e>0 ){.        c
65f50 6f 75 6e 74 20 2b 3d 20 6e 73 70 61 63 65 3b 0a  ount += nspace;.
65f60 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e          while( n
65f70 73 70 61 63 65 3e 3d 65 74 53 50 41 43 45 53 49  space>=etSPACESI
65f80 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ZE ){.          
65f90 28 2a 66 75 6e 63 29 28 61 72 67 2c 73 70 61 63  (*func)(arg,spac
65fa0 65 73 2c 65 74 53 50 41 43 45 53 49 5a 45 29 3b  es,etSPACESIZE);
65fb0 0a 20 20 20 20 20 20 20 20 20 20 6e 73 70 61 63  .          nspac
65fc0 65 20 2d 3d 20 65 74 53 50 41 43 45 53 49 5a 45  e -= etSPACESIZE
65fd0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
65fe0 20 20 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30      if( nspace>0
65ff0 20 29 20 28 2a 66 75 6e 63 29 28 61 72 67 2c 73   ) (*func)(arg,s
66000 70 61 63 65 73 2c 6e 73 70 61 63 65 29 3b 0a 20  paces,nspace);. 
66010 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
66020 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20   if( zExtra ){. 
66030 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
66040 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d  e(zExtra);.    }
66050 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c  .  }/* End for l
66060 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72  oop over the for
66070 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  mat string */.  
66080 72 65 74 75 72 6e 20 65 72 72 6f 72 66 6c 61 67  return errorflag
66090 20 3f 20 2d 31 20 3a 20 63 6f 75 6e 74 3b 0a 7d   ? -1 : count;.}
660a0 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74   /* End of funct
660b0 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 20 54 68 69 73  ion */.../* This
660c0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
660d0 65 64 20 74 6f 20 73 74 6f 72 65 20 73 74 61 74  ed to store stat
660e0 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  e information ab
660f0 6f 75 74 20 74 68 65 0a 2a 2a 20 77 72 69 74 65  out the.** write
66100 20 74 6f 20 6d 65 6d 6f 72 79 20 74 68 61 74 20   to memory that 
66110 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  is currently in 
66120 70 72 6f 67 72 65 73 73 2e 0a 2a 2f 0a 73 74 72  progress..*/.str
66130 75 63 74 20 73 67 4d 70 72 69 6e 74 66 20 7b 0a  uct sgMprintf {.
66140 20 20 63 68 61 72 20 2a 7a 42 61 73 65 3b 20 20    char *zBase;  
66150 20 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c     /* A base all
66160 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61  ocation */.  cha
66170 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 2f 2a  r *zText;     /*
66180 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c   The string coll
66190 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a  ected so far */.
661a0 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20    int  nChar;   
661b0 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
661c0 74 68 65 20 73 74 72 69 6e 67 20 73 6f 20 66 61  the string so fa
661d0 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 54 6f 74  r */.  int  nTot
661e0 61 6c 3b 20 20 20 20 20 2f 2a 20 4f 75 74 70 75  al;     /* Outpu
661f0 74 20 73 69 7a 65 20 69 66 20 75 6e 63 6f 6e 73  t size if uncons
66200 74 72 61 69 6e 65 64 20 2a 2f 0a 20 20 69 6e 74  trained */.  int
66210 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a    nAlloc;     /*
66220 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65   Amount of space
66230 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 7a 54   allocated in zT
66240 65 78 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28  ext */.  void *(
66250 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a  *xRealloc)(void*
66260 2c 69 6e 74 29 3b 20 20 2f 2a 20 46 75 6e 63 74  ,int);  /* Funct
66270 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 61 6c  ion used to real
66280 6c 6f 63 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20  loc memory */.  
66290 69 6e 74 20 20 69 4d 61 6c 6c 6f 63 46 61 69 6c  int  iMallocFail
662a0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ed;            /
662b0 2a 20 54 72 75 65 20 69 66 20 78 52 65 61 6c 6c  * True if xReall
662c0 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 20  oc() has failed 
662d0 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 54 68  */.};../* .** Th
662e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  is function impl
662f0 65 6d 65 6e 74 73 20 74 68 65 20 63 61 6c 6c 62  ements the callb
66300 61 63 6b 20 66 72 6f 6d 20 76 78 70 72 69 6e 74  ack from vxprint
66310 66 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  f. .**.** This r
66320 6f 75 74 69 6e 65 20 61 64 64 20 6e 4e 65 77 43  outine add nNewC
66330 68 61 72 20 63 68 61 72 61 63 74 65 72 73 20 6f  har characters o
66340 66 20 74 65 78 74 20 69 6e 20 7a 4e 65 77 54 65  f text in zNewTe
66350 78 74 20 74 6f 0a 2a 2a 20 74 68 65 20 73 67 4d  xt to.** the sgM
66360 70 72 69 6e 74 66 20 73 74 72 75 63 74 75 72 65  printf structure
66370 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 22   pointed to by "
66380 61 72 67 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  arg"..*/.static 
66390 76 6f 69 64 20 6d 6f 75 74 28 76 6f 69 64 20 2a  void mout(void *
663a0 61 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  arg, const char 
663b0 2a 7a 4e 65 77 54 65 78 74 2c 20 69 6e 74 20 6e  *zNewText, int n
663c0 4e 65 77 43 68 61 72 29 7b 0a 20 20 73 74 72 75  NewChar){.  stru
663d0 63 74 20 73 67 4d 70 72 69 6e 74 66 20 2a 70 4d  ct sgMprintf *pM
663e0 20 3d 20 28 73 74 72 75 63 74 20 73 67 4d 70 72   = (struct sgMpr
663f0 69 6e 74 66 2a 29 61 72 67 3b 0a 20 20 69 66 28  intf*)arg;.  if(
66400 20 70 4d 2d 3e 69 4d 61 6c 6c 6f 63 46 61 69 6c   pM->iMallocFail
66410 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  ed ) return;.  p
66420 4d 2d 3e 6e 54 6f 74 61 6c 20 2b 3d 20 6e 4e 65  M->nTotal += nNe
66430 77 43 68 61 72 3b 0a 20 20 69 66 28 20 70 4d 2d  wChar;.  if( pM-
66440 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 69 66  >zText ){.    if
66450 28 20 70 4d 2d 3e 6e 43 68 61 72 20 2b 20 6e 4e  ( pM->nChar + nN
66460 65 77 43 68 61 72 20 2b 20 31 20 3e 20 70 4d 2d  ewChar + 1 > pM-
66470 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  >nAlloc ){.     
66480 20 69 66 28 20 70 4d 2d 3e 78 52 65 61 6c 6c 6f   if( pM->xReallo
66490 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c==0 ){.        
664a0 6e 4e 65 77 43 68 61 72 20 3d 20 20 70 4d 2d 3e  nNewChar =  pM->
664b0 6e 41 6c 6c 6f 63 20 2d 20 70 4d 2d 3e 6e 43 68  nAlloc - pM->nCh
664c0 61 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 7d 65  ar - 1;.      }e
664d0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
664e0 20 6e 41 6c 6c 6f 63 20 3d 20 70 4d 2d 3e 6e 43   nAlloc = pM->nC
664f0 68 61 72 20 2b 20 6e 4e 65 77 43 68 61 72 2a 32  har + nNewChar*2
66500 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66   + 1;.        if
66510 28 20 70 4d 2d 3e 7a 54 65 78 74 3d 3d 70 4d 2d  ( pM->zText==pM-
66520 3e 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20  >zBase ){.      
66530 20 20 20 20 70 4d 2d 3e 7a 54 65 78 74 20 3d 20      pM->zText = 
66540 70 4d 2d 3e 78 52 65 61 6c 6c 6f 63 28 30 2c 20  pM->xRealloc(0, 
66550 6e 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20  nAlloc);.       
66560 20 20 20 69 66 28 20 70 4d 2d 3e 7a 54 65 78 74     if( pM->zText
66570 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
66580 20 20 20 70 4d 2d 3e 6e 41 6c 6c 6f 63 20 3d 20     pM->nAlloc = 
66590 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  0;.            p
665a0 4d 2d 3e 69 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  M->iMallocFailed
665b0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
665c0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
665d0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d      }else if( pM
665e0 2d 3e 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20  ->nChar ){.     
665f0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4d         memcpy(pM
66600 2d 3e 7a 54 65 78 74 2c 20 70 4d 2d 3e 7a 42 61  ->zText, pM->zBa
66610 73 65 2c 20 70 4d 2d 3e 6e 43 68 61 72 29 3b 0a  se, pM->nChar);.
66620 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
66630 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
66640 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b       char *zNew;
66650 0a 20 20 20 20 20 20 20 20 20 20 7a 4e 65 77 20  .          zNew 
66660 3d 20 70 4d 2d 3e 78 52 65 61 6c 6c 6f 63 28 70  = pM->xRealloc(p
66670 4d 2d 3e 7a 54 65 78 74 2c 20 6e 41 6c 6c 6f 63  M->zText, nAlloc
66680 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
66690 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20   zNew ){.       
666a0 20 20 20 20 20 70 4d 2d 3e 7a 54 65 78 74 20 3d       pM->zText =
666b0 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 20   zNew;.         
666c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
666d0 20 20 20 20 70 4d 2d 3e 69 4d 61 6c 6c 6f 63 46      pM->iMallocF
666e0 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
666f0 20 20 20 20 20 20 20 70 4d 2d 3e 78 52 65 61 6c         pM->xReal
66700 6c 6f 63 28 70 4d 2d 3e 7a 54 65 78 74 2c 20 30  loc(pM->zText, 0
66710 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
66720 4d 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20  M->zText = 0;.  
66730 20 20 20 20 20 20 20 20 20 20 70 4d 2d 3e 6e 41            pM->nA
66740 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20  lloc = 0;.      
66750 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
66760 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
66770 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 2d 3e    }.        pM->
66780 6e 41 6c 6c 6f 63 20 3d 20 6e 41 6c 6c 6f 63 3b  nAlloc = nAlloc;
66790 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
667a0 20 20 20 69 66 28 20 6e 4e 65 77 43 68 61 72 3e     if( nNewChar>
667b0 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  0 ){.      memcp
667c0 79 28 26 70 4d 2d 3e 7a 54 65 78 74 5b 70 4d 2d  y(&pM->zText[pM-
667d0 3e 6e 43 68 61 72 5d 2c 20 7a 4e 65 77 54 65 78  >nChar], zNewTex
667e0 74 2c 20 6e 4e 65 77 43 68 61 72 29 3b 0a 20 20  t, nNewChar);.  
667f0 20 20 20 20 70 4d 2d 3e 6e 43 68 61 72 20 2b 3d      pM->nChar +=
66800 20 6e 4e 65 77 43 68 61 72 3b 0a 20 20 20 20 7d   nNewChar;.    }
66810 0a 20 20 20 20 70 4d 2d 3e 7a 54 65 78 74 5b 70  .    pM->zText[p
66820 4d 2d 3e 6e 43 68 61 72 5d 20 3d 20 30 3b 0a 20  M->nChar] = 0;. 
66830 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
66840 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 77 72   routine is a wr
66850 61 70 70 65 72 20 61 72 6f 75 6e 64 20 78 70 72  apper around xpr
66860 69 6e 74 66 28 29 20 74 68 61 74 20 69 6e 76 6f  intf() that invo
66870 6b 65 73 20 6d 6f 75 74 28 29 20 61 73 0a 2a 2a  kes mout() as.**
66880 20 74 68 65 20 63 6f 6e 73 75 6d 65 72 2e 20 20   the consumer.  
66890 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
668a0 2a 62 61 73 65 5f 76 70 72 69 6e 74 66 28 0a 20  *base_vprintf(. 
668b0 20 76 6f 69 64 20 2a 28 2a 78 52 65 61 6c 6c 6f   void *(*xReallo
668c0 63 29 28 76 6f 69 64 2a 2c 20 69 6e 74 29 2c 20  c)(void*, int), 
668d0 20 2f 2a 20 72 65 61 6c 6c 6f 63 28 29 20 66 75   /* realloc() fu
668e0 6e 63 74 69 6f 6e 2e 20 4d 61 79 20 62 65 20 4e  nction. May be N
668f0 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65  ULL */.  int use
66900 49 6e 74 65 72 6e 61 6c 2c 20 20 20 20 20 20 20  Internal,       
66910 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20           /* Use 
66920 69 6e 74 65 72 6e 61 6c 20 25 2d 63 6f 6e 76 65  internal %-conve
66930 72 73 69 6f 6e 73 20 69 66 20 74 72 75 65 20 2a  rsions if true *
66940 2f 0a 20 20 63 68 61 72 20 2a 7a 49 6e 69 74 42  /.  char *zInitB
66950 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  uf,             
66960 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 6c 79      /* Initially
66970 20 77 72 69 74 65 20 68 65 72 65 2c 20 62 65 66   write here, bef
66980 6f 72 65 20 6d 61 6c 6c 6f 63 69 6e 67 20 2a 2f  ore mallocing */
66990 0a 20 20 69 6e 74 20 6e 49 6e 69 74 42 75 66 2c  .  int nInitBuf,
669a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
669b0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 49     /* Size of zI
669c0 6e 69 74 42 75 66 5b 5d 20 2a 2f 0a 20 20 63 6f  nitBuf[] */.  co
669d0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
669e0 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t,            /*
669f0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a   format string *
66a00 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 20 20  /.  va_list ap  
66a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66a20 20 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73      /* arguments
66a30 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
66a40 73 67 4d 70 72 69 6e 74 66 20 73 4d 3b 0a 20 20  sgMprintf sM;.  
66a50 73 4d 2e 7a 42 61 73 65 20 3d 20 73 4d 2e 7a 54  sM.zBase = sM.zT
66a60 65 78 74 20 3d 20 7a 49 6e 69 74 42 75 66 3b 0a  ext = zInitBuf;.
66a70 20 20 73 4d 2e 6e 43 68 61 72 20 3d 20 73 4d 2e    sM.nChar = sM.
66a80 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 73 4d  nTotal = 0;.  sM
66a90 2e 6e 41 6c 6c 6f 63 20 3d 20 6e 49 6e 69 74 42  .nAlloc = nInitB
66aa0 75 66 3b 0a 20 20 73 4d 2e 78 52 65 61 6c 6c 6f  uf;.  sM.xReallo
66ab0 63 20 3d 20 78 52 65 61 6c 6c 6f 63 3b 0a 20 20  c = xRealloc;.  
66ac0 73 4d 2e 69 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  sM.iMallocFailed
66ad0 20 3d 20 30 3b 0a 20 20 76 78 70 72 69 6e 74 66   = 0;.  vxprintf
66ae0 28 6d 6f 75 74 2c 20 26 73 4d 2c 20 75 73 65 49  (mout, &sM, useI
66af0 6e 74 65 72 6e 61 6c 2c 20 7a 46 6f 72 6d 61 74  nternal, zFormat
66b00 2c 20 61 70 29 3b 0a 20 20 61 73 73 65 72 74 28  , ap);.  assert(
66b10 73 4d 2e 69 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  sM.iMallocFailed
66b20 3d 3d 30 20 7c 7c 20 73 4d 2e 7a 54 65 78 74 3d  ==0 || sM.zText=
66b30 3d 30 29 3b 0a 20 20 69 66 28 20 78 52 65 61 6c  =0);.  if( xReal
66b40 6c 6f 63 20 26 26 20 21 73 4d 2e 69 4d 61 6c 6c  loc && !sM.iMall
66b50 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
66b60 69 66 28 20 73 4d 2e 7a 54 65 78 74 3d 3d 73 4d  if( sM.zText==sM
66b70 2e 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20  .zBase ){.      
66b80 73 4d 2e 7a 54 65 78 74 20 3d 20 78 52 65 61 6c  sM.zText = xReal
66b90 6c 6f 63 28 30 2c 20 73 4d 2e 6e 43 68 61 72 2b  loc(0, sM.nChar+
66ba0 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 4d  1);.      if( sM
66bb0 2e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20  .zText ){.      
66bc0 20 20 6d 65 6d 63 70 79 28 73 4d 2e 7a 54 65 78    memcpy(sM.zTex
66bd0 74 2c 20 73 4d 2e 7a 42 61 73 65 2c 20 73 4d 2e  t, sM.zBase, sM.
66be0 6e 43 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20  nChar+1);.      
66bf0 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  }.    }else if( 
66c00 73 4d 2e 6e 41 6c 6c 6f 63 3e 73 4d 2e 6e 43 68  sM.nAlloc>sM.nCh
66c10 61 72 2b 31 30 20 29 7b 0a 20 20 20 20 20 20 63  ar+10 ){.      c
66c20 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 20  har *zNew;.     
66c30 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 42 65   sqlite3MallocBe
66c40 6e 69 67 6e 46 61 69 6c 75 72 65 28 31 29 3b 0a  nignFailure(1);.
66c50 20 20 20 20 20 20 7a 4e 65 77 20 3d 20 78 52 65        zNew = xRe
66c60 61 6c 6c 6f 63 28 73 4d 2e 7a 54 65 78 74 2c 20  alloc(sM.zText, 
66c70 73 4d 2e 6e 43 68 61 72 2b 31 29 3b 0a 20 20 20  sM.nChar+1);.   
66c80 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20     if( zNew ){. 
66c90 20 20 20 20 20 20 20 73 4d 2e 7a 54 65 78 74 20         sM.zText 
66ca0 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a  = zNew;.      }.
66cb0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
66cc0 72 6e 20 73 4d 2e 7a 54 65 78 74 3b 0a 7d 0a 0a  rn sM.zText;.}..
66cd0 2f 2a 0a 2a 2a 20 52 65 61 6c 6c 6f 63 20 74 68  /*.** Realloc th
66ce0 61 74 20 69 73 20 61 20 72 65 61 6c 20 66 75 6e  at is a real fun
66cf0 63 74 69 6f 6e 2c 20 6e 6f 74 20 61 20 6d 61 63  ction, not a mac
66d00 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ro..*/.static vo
66d10 69 64 20 2a 70 72 69 6e 74 66 5f 72 65 61 6c 6c  id *printf_reall
66d20 6f 63 28 76 6f 69 64 20 2a 6f 6c 64 2c 20 69 6e  oc(void *old, in
66d30 74 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 72  t size){.  retur
66d40 6e 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  n sqlite3_reallo
66d50 63 28 6f 6c 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a  c(old, size);.}.
66d60 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74  ./*.** Print int
66d70 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
66d80 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c  d from sqliteMal
66d90 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 65 20  loc().  Use the 
66da0 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f  internal.** %-co
66db0 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69  nversion extensi
66dc0 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
66dd0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
66de0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c  ite3VMPrintf(sql
66df0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
66e00 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76  char *zFormat, v
66e10 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 63 68  a_list ap){.  ch
66e20 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42  ar *z;.  char zB
66e30 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54  ase[SQLITE_PRINT
66e40 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 7a 20  _BUF_SIZE];.  z 
66e50 3d 20 62 61 73 65 5f 76 70 72 69 6e 74 66 28 70  = base_vprintf(p
66e60 72 69 6e 74 66 5f 72 65 61 6c 6c 6f 63 2c 20 31  rintf_realloc, 1
66e70 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 28  , zBase, sizeof(
66e80 7a 42 61 73 65 29 2c 20 7a 46 6f 72 6d 61 74 2c  zBase), zFormat,
66e90 20 61 70 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30   ap);.  if( z==0
66ea0 20 26 26 20 64 62 21 3d 30 20 29 7b 0a 20 20 20   && db!=0 ){.   
66eb0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
66ec0 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  d = 1;.  }.  ret
66ed0 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
66ee0 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
66ef0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
66f00 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20  sqliteMalloc(). 
66f10 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61   Use the interna
66f20 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f  l.** %-conversio
66f30 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f  n extensions..*/
66f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
66f50 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72  char *sqlite3MPr
66f60 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62  intf(sqlite3 *db
66f70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
66f80 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76  ormat, ...){.  v
66f90 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61  a_list ap;.  cha
66fa0 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61  r *z;.  char zBa
66fb0 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f  se[SQLITE_PRINT_
66fc0 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 76 61 5f  BUF_SIZE];.  va_
66fd0 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
66fe0 74 29 3b 0a 20 20 7a 20 3d 20 62 61 73 65 5f 76  t);.  z = base_v
66ff0 70 72 69 6e 74 66 28 70 72 69 6e 74 66 5f 72 65  printf(printf_re
67000 61 6c 6c 6f 63 2c 20 31 2c 20 7a 42 61 73 65 2c  alloc, 1, zBase,
67010 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20   sizeof(zBase), 
67020 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
67030 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66  va_end(ap);.  if
67040 28 20 7a 3d 3d 30 20 26 26 20 64 62 21 3d 30 20  ( z==0 && db!=0 
67050 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
67060 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
67070 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
67080 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f  /*.** Print into
67090 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
670a0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
670b0 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 74 20 74 68  lloc().  Omit th
670c0 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d  e internal.** %-
670d0 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e  conversion exten
670e0 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sions..*/.SQLITE
670f0 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74  _API char *sqlit
67100 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73  e3_vmprintf(cons
67110 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
67120 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20   va_list ap){.  
67130 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54  char zBase[SQLIT
67140 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45  E_PRINT_BUF_SIZE
67150 5d 3b 0a 20 20 72 65 74 75 72 6e 20 62 61 73 65  ];.  return base
67160 5f 76 70 72 69 6e 74 66 28 73 71 6c 69 74 65 33  _vprintf(sqlite3
67170 5f 72 65 61 6c 6c 6f 63 2c 20 30 2c 20 7a 42 61  _realloc, 0, zBa
67180 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 65  se, sizeof(zBase
67190 29 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  ), zFormat, ap);
671a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  .}../*.** Print 
671b0 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
671c0 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
671d0 33 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f  3_malloc()().  O
671e0 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  mit the internal
671f0 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  .** %-conversion
67200 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
67210 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
67220 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66  *sqlite3_mprintf
67230 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f  (const char *zFo
67240 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
67250 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
67260 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28   *z;.  va_start(
67270 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
67280 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d 70 72  z = sqlite3_vmpr
67290 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70  intf(zFormat, ap
672a0 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
672b0 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
672c0 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6e  /*.** sqlite3_sn
672d0 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 20 6c  printf() works l
672e0 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 20 65  ike snprintf() e
672f0 78 63 65 70 74 20 74 68 61 74 20 69 74 20 69 67  xcept that it ig
67300 6e 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63 75 72  nores the.** cur
67310 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65 74 74  rent locale sett
67320 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 69  ings.  This is i
67330 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53 51 4c  mportant for SQL
67340 69 74 65 20 62 65 63 61 75 73 65 20 77 65 0a 2a  ite because we.*
67350 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74  * are not able t
67360 6f 20 75 73 65 20 61 20 22 2c 22 20 61 73 20 74  o use a "," as t
67370 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
67380 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 22 2e 22   in place of "."
67390 20 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64   as.** specified
673a0 20 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c 65 73   by some locales
673b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
673c0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e  char *sqlite3_sn
673d0 70 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20 63 68  printf(int n, ch
673e0 61 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73 74 20  ar *zBuf, const 
673f0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
67400 2e 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  ..){.  char *z;.
67410 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 0a 20    va_list ap;.. 
67420 20 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20   if( n<=0 ){.   
67430 20 72 65 74 75 72 6e 20 7a 42 75 66 3b 0a 20 20   return zBuf;.  
67440 7d 0a 20 20 7a 42 75 66 5b 30 5d 20 3d 20 30 3b  }.  zBuf[0] = 0;
67450 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a  .  va_start(ap,z
67460 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 62  Format);.  z = b
67470 61 73 65 5f 76 70 72 69 6e 74 66 28 30 2c 20 30  ase_vprintf(0, 0
67480 2c 20 7a 42 75 66 2c 20 6e 2c 20 7a 46 6f 72 6d  , zBuf, n, zForm
67490 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
674a0 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  d(ap);.  return 
674b0 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65  z;.}..#if define
674c0 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c  d(SQLITE_TEST) |
674d0 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
674e0 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e  _DEBUG) || defin
674f0 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  ed(SQLITE_MEMDEB
67500 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 73  UG)./*.** A vers
67510 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29 20  ion of printf() 
67520 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64 73  that understands
67530 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f 72   %lld.  Used for
67540 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 54   debugging..** T
67550 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69 6c  he printf() buil
67560 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72 73  t into some vers
67570 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20  ions of windows 
67580 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73 74  does not underst
67590 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20  and %lld.** and 
675a0 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f 75  segfaults if you
675b0 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67 20   give it a long 
675c0 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c  long int..*/.SQL
675d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
675e0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
675f0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ntf(const char *
67600 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
67610 20 65 78 74 65 72 6e 20 69 6e 74 20 67 65 74 70   extern int getp
67620 69 64 28 76 6f 69 64 29 3b 0a 20 20 76 61 5f 6c  id(void);.  va_l
67630 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 7a  ist ap;.  char z
67640 42 75 66 5b 35 30 30 5d 3b 0a 20 20 76 61 5f 73  Buf[500];.  va_s
67650 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
67660 29 3b 0a 20 20 62 61 73 65 5f 76 70 72 69 6e 74  );.  base_vprint
67670 66 28 30 2c 20 30 2c 20 7a 42 75 66 2c 20 73 69  f(0, 0, zBuf, si
67680 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 46 6f 72  zeof(zBuf), zFor
67690 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
676a0 6e 64 28 61 70 29 3b 0a 20 20 66 70 72 69 6e 74  nd(ap);.  fprint
676b0 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a  f(stdout,"%s", z
676c0 42 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73  Buf);.  fflush(s
676d0 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66  tdout);.}.#endif
676e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
676f0 2a 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e  * End of printf.
67700 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
67710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67730 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
67740 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e  * Begin file ran
67750 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dom.c **********
67760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67780 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
67790 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
677a0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
677b0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
677c0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
677d0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
677e0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
677f0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
67800 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
67810 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
67820 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
67830 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
67840 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
67850 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
67860 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
67870 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
67880 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
67890 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
678a0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
678b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
678c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
678d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
678e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
678f0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
67900 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
67910 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
67920 20 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d   a pseudo-random
67930 20 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72   number.** gener
67940 61 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20  ator (PRNG) for 
67950 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61  SQLite..**.** Ra
67960 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65  ndom numbers are
67970 20 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66   used by some of
67980 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61   the database ba
67990 63 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a  ckends in order.
679a0 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72  ** to generate r
679b0 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65  andom integer ke
679c0 79 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72  ys for tables or
679d0 20 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65   random filename
679e0 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61  s..**.** $Id: ra
679f0 6e 64 6f 6d 2e 63 2c 76 20 31 2e 32 30 20 32 30  ndom.c,v 1.20 20
67a00 30 37 2f 30 38 2f 32 31 20 31 33 3a 35 31 3a 32  07/08/21 13:51:2
67a10 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  3 drh Exp $.*/..
67a20 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 73 69 6e  ./*.** Get a sin
67a30 67 6c 65 20 38 2d 62 69 74 20 72 61 6e 64 6f 6d  gle 8-bit random
67a40 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20   value from the 
67a50 52 43 34 20 50 52 4e 47 2e 20 20 54 68 65 20 4d  RC4 PRNG.  The M
67a60 75 74 65 78 0a 2a 2a 20 6d 75 73 74 20 62 65 20  utex.** must be 
67a70 68 65 6c 64 20 77 68 69 6c 65 20 65 78 65 63 75  held while execu
67a80 74 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  ting this routin
67a90 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20 6e 6f 74  e..**.** Why not
67aa0 20 6a 75 73 74 20 75 73 65 20 61 20 6c 69 62 72   just use a libr
67ab0 61 72 79 20 72 61 6e 64 6f 6d 20 67 65 6e 65 72  ary random gener
67ac0 61 74 6f 72 20 6c 69 6b 65 20 6c 72 61 6e 64 34  ator like lrand4
67ad0 38 28 29 20 66 6f 72 20 74 68 69 73 3f 0a 2a 2a  8() for this?.**
67ae0 20 42 65 63 61 75 73 65 20 74 68 65 20 4f 50 5f   Because the OP_
67af0 4e 65 77 52 6f 77 69 64 20 6f 70 63 6f 64 65 20  NewRowid opcode 
67b00 69 6e 20 74 68 65 20 56 44 42 45 20 64 65 70 65  in the VDBE depe
67b10 6e 64 73 20 6f 6e 20 68 61 76 69 6e 67 20 61 20  nds on having a 
67b20 76 65 72 79 0a 2a 2a 20 67 6f 6f 64 20 73 6f 75  very.** good sou
67b30 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75  rce of random nu
67b40 6d 62 65 72 73 2e 20 20 54 68 65 20 6c 72 61 6e  mbers.  The lran
67b50 64 34 38 28 29 20 6c 69 62 72 61 72 79 20 66 75  d48() library fu
67b60 6e 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 77 65  nction may.** we
67b70 6c 6c 20 62 65 20 67 6f 6f 64 20 65 6e 6f 75 67  ll be good enoug
67b80 68 2e 20 20 42 75 74 20 6d 61 79 62 65 20 6e 6f  h.  But maybe no
67b90 74 2e 20 20 4f 72 20 6d 61 79 62 65 20 6c 72 61  t.  Or maybe lra
67ba0 6e 64 34 38 28 29 20 68 61 73 20 73 6f 6d 65 0a  nd48() has some.
67bb0 2a 2a 20 73 75 62 74 6c 65 20 70 72 6f 62 6c 65  ** subtle proble
67bc0 6d 73 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65  ms on some syste
67bd0 6d 73 20 74 68 61 74 20 63 6f 75 6c 64 20 63 61  ms that could ca
67be0 75 73 65 20 70 72 6f 62 6c 65 6d 73 2e 20 20 49  use problems.  I
67bf0 74 20 69 73 20 68 61 72 64 0a 2a 2a 20 74 6f 20  t is hard.** to 
67c00 6b 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e 69 6d 69  know.  To minimi
67c10 7a 65 20 74 68 65 20 72 69 73 6b 20 6f 66 20 70  ze the risk of p
67c20 72 6f 62 6c 65 6d 73 20 64 75 65 20 74 6f 20 62  roblems due to b
67c30 61 64 20 6c 72 61 6e 64 34 38 28 29 0a 2a 2a 20  ad lrand48().** 
67c40 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2c  implementations,
67c50 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 69   SQLite uses thi
67c60 73 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  s random number 
67c70 67 65 6e 65 72 61 74 6f 72 20 62 61 73 65 64 0a  generator based.
67c80 2a 2a 20 6f 6e 20 52 43 34 2c 20 77 68 69 63 68  ** on RC4, which
67c90 20 77 65 20 6b 6e 6f 77 20 77 6f 72 6b 73 20 76   we know works v
67ca0 65 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ery well..**.** 
67cb0 28 4c 61 74 65 72 29 3a 20 20 41 63 74 75 61 6c  (Later):  Actual
67cc0 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20  ly, OP_NewRowid 
67cd0 64 6f 65 73 20 6e 6f 74 20 64 65 70 65 6e 64 20  does not depend 
67ce0 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75 72 63 65  on a good source
67cf0 20 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d 6e 65 73   of.** randomnes
67d00 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20 42 75 74  s any more.  But
67d10 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 74   we will leave t
67d20 68 69 73 20 63 6f 64 65 20 69 6e 20 61 6c 6c 20  his code in all 
67d30 74 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a 73 74 61  the same..*/.sta
67d40 74 69 63 20 69 6e 74 20 72 61 6e 64 6f 6d 42 79  tic int randomBy
67d50 74 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69  te(void){.  unsi
67d60 67 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a 20 20  gned char t;..  
67d70 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73  /* All threads s
67d80 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61  hare a single ra
67d90 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
67da0 72 61 74 6f 72 2e 0a 20 20 2a 2a 20 54 68 69 73  rator..  ** This
67db0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74 68   structure is th
67dc0 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  e current state 
67dd0 6f 66 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72  of the generator
67de0 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  ..  */.  static 
67df0 73 74 72 75 63 74 20 7b 0a 20 20 20 20 75 6e 73  struct {.    uns
67e00 69 67 6e 65 64 20 63 68 61 72 20 69 73 49 6e 69  igned char isIni
67e10 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
67e20 72 75 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a  rue if initializ
67e30 65 64 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ed */.    unsign
67e40 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20 20  ed char i, j;   
67e50 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
67e60 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20  e variables */. 
67e70 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
67e80 20 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20   s[256];        
67e90 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61    /* State varia
67ea0 62 6c 65 73 20 2a 2f 0a 20 20 7d 20 70 72 6e 67  bles */.  } prng
67eb0 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ;..  /* Initiali
67ec0 7a 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  ze the state of 
67ed0 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  the random numbe
67ee0 72 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65  r generator once
67ef0 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74  ,.  ** the first
67f00 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69   time this routi
67f10 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54  ne is called.  T
67f20 68 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f  he seed value do
67f30 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64  es.  ** not need
67f40 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f   to contain a lo
67f50 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20  t of randomness 
67f60 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74  since we are not
67f70 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20  .  ** trying to 
67f80 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70  do secure encryp
67f90 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67  tion or anything
67fa0 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20   like that....  
67fb0 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20  **.  ** Nothing 
67fc0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20  in this file or 
67fd0 61 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e  anywhere else in
67fe0 20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79   SQLite does any
67ff0 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e   kind of.  ** en
68000 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52  cryption.  The R
68010 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20  C4 algorithm is 
68020 62 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20  being used as a 
68030 50 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e  PRNG (pseudo-ran
68040 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20  dom.  ** number 
68050 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61  generator) not a
68060 73 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20  s an encryption 
68070 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69  device..  */.  i
68080 66 28 20 21 70 72 6e 67 2e 69 73 49 6e 69 74 20  f( !prng.isInit 
68090 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
680a0 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b 0a 20    char k[256];. 
680b0 20 20 20 70 72 6e 67 2e 6a 20 3d 20 30 3b 0a 20     prng.j = 0;. 
680c0 20 20 20 70 72 6e 67 2e 69 20 3d 20 30 3b 0a 20     prng.i = 0;. 
680d0 20 20 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64     sqlite3OsRand
680e0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76  omness(sqlite3_v
680f0 66 73 5f 66 69 6e 64 28 30 29 2c 20 32 35 36 2c  fs_find(0), 256,
68100 20 6b 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30   k);.    for(i=0
68110 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20  ; i<256; i++){. 
68120 20 20 20 20 20 70 72 6e 67 2e 73 5b 69 5d 20 3d       prng.s[i] =
68130 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f   i;.    }.    fo
68140 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b  r(i=0; i<256; i+
68150 2b 29 7b 0a 20 20 20 20 20 20 70 72 6e 67 2e 6a  +){.      prng.j
68160 20 2b 3d 20 70 72 6e 67 2e 73 5b 69 5d 20 2b 20   += prng.s[i] + 
68170 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74 20 3d 20  k[i];.      t = 
68180 70 72 6e 67 2e 73 5b 70 72 6e 67 2e 6a 5d 3b 0a  prng.s[prng.j];.
68190 20 20 20 20 20 20 70 72 6e 67 2e 73 5b 70 72 6e        prng.s[prn
681a0 67 2e 6a 5d 20 3d 20 70 72 6e 67 2e 73 5b 69 5d  g.j] = prng.s[i]
681b0 3b 0a 20 20 20 20 20 20 70 72 6e 67 2e 73 5b 69  ;.      prng.s[i
681c0 5d 20 3d 20 74 3b 0a 20 20 20 20 7d 0a 20 20 20  ] = t;.    }.   
681d0 20 70 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20 31   prng.isInit = 1
681e0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65  ;.  }..  /* Gene
681f0 72 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  rate and return 
68200 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62 79  single random by
68210 74 65 0a 20 20 2a 2f 0a 20 20 70 72 6e 67 2e 69  te.  */.  prng.i
68220 2b 2b 3b 0a 20 20 74 20 3d 20 70 72 6e 67 2e 73  ++;.  t = prng.s
68230 5b 70 72 6e 67 2e 69 5d 3b 0a 20 20 70 72 6e 67  [prng.i];.  prng
68240 2e 6a 20 2b 3d 20 74 3b 0a 20 20 70 72 6e 67 2e  .j += t;.  prng.
68250 73 5b 70 72 6e 67 2e 69 5d 20 3d 20 70 72 6e 67  s[prng.i] = prng
68260 2e 73 5b 70 72 6e 67 2e 6a 5d 3b 0a 20 20 70 72  .s[prng.j];.  pr
68270 6e 67 2e 73 5b 70 72 6e 67 2e 6a 5d 20 3d 20 74  ng.s[prng.j] = t
68280 3b 0a 20 20 74 20 2b 3d 20 70 72 6e 67 2e 73 5b  ;.  t += prng.s[
68290 70 72 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72  prng.i];.  retur
682a0 6e 20 70 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a  n prng.s[t];.}..
682b0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72  /*.** Return N r
682c0 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a  andom bytes..*/.
682d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
682e0 6f 69 64 20 73 71 6c 69 74 65 33 52 61 6e 64 6f  oid sqlite3Rando
682f0 6d 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69  mness(int N, voi
68300 64 20 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69  d *pBuf){.  unsi
68310 67 6e 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20  gned char *zBuf 
68320 3d 20 70 42 75 66 3b 0a 20 20 73 74 61 74 69 63  = pBuf;.  static
68330 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
68340 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 69 66 28  mutex = 0;.  if(
68350 20 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20   mutex==0 ){.   
68360 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33   mutex = sqlite3
68370 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c  _mutex_alloc(SQL
68380 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
68390 5f 50 52 4e 47 29 3b 0a 20 20 7d 0a 20 20 73 71  _PRNG);.  }.  sq
683a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
683b0 72 28 6d 75 74 65 78 29 3b 0a 20 20 77 68 69 6c  r(mutex);.  whil
683c0 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28  e( N-- ){.    *(
683d0 7a 42 75 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d  zBuf++) = random
683e0 42 79 74 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71  Byte();.  }.  sq
683f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
68400 65 28 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 2a  e(mutex);.}../**
68410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
68420 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a   of random.c ***
68430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
68460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
68470 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a  in file utf.c **
68480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
684a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
684b0 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33  ** 2004 April 13
684c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
684d0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
684e0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
684f0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
68500 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
68510 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
68520 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
68530 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
68540 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
68550 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
68560 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
68570 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
68580 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
68590 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
685a0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
685b0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
685c0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
685d0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
685e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
685f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
68620 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
68630 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75  tains routines u
68640 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65  sed to translate
68650 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20   between UTF-8, 
68660 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d  .** UTF-16, UTF-
68670 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36  16BE, and UTF-16
68680 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75  LE..**.** $Id: u
68690 74 66 2e 63 2c 76 20 31 2e 35 38 20 32 30 30 37  tf.c,v 1.58 2007
686a0 2f 30 39 2f 31 32 20 31 37 3a 30 31 3a 34 35 20  /09/12 17:01:45 
686b0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
686c0 24 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e  $.**.** Notes on
686d0 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20   UTF-8:.**.**   
686e0 42 79 74 65 2d 30 20 20 20 20 42 79 74 65 2d 31  Byte-0    Byte-1
686f0 20 20 20 20 42 79 74 65 2d 32 20 20 20 20 42 79      Byte-2    By
68700 74 65 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a  te-3    Value.**
68710 20 20 30 78 78 78 78 78 78 78 20 20 20 20 20 20    0xxxxxxx      
68720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68730 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30             00000
68740 30 30 30 20 30 30 30 30 30 30 30 30 20 30 78 78  000 00000000 0xx
68750 78 78 78 78 78 0a 2a 2a 20 20 31 31 30 79 79 79  xxxxx.**  110yyy
68760 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20  yy  10xxxxxx    
68770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68780 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30     00000000 0000
68790 30 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a  0yyy yyxxxxxx.**
687a0 20 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79    1110zzzz  10yy
687b0 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20  yyyy  10xxxxxx  
687c0 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30             00000
687d0 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78  000 zzzzyyyy yyx
687e0 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 31 30 75  xxxxx.**  11110u
687f0 75 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30  uu  10uuzzzz  10
68800 79 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78  yyyyyy  10xxxxxx
68810 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a     000uuuuu zzzz
68820 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a  yyyy yyxxxxxx.**
68830 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20  .**.** Notes on 
68840 55 54 46 2d 31 36 3a 20 20 28 77 69 74 68 20 77  UTF-16:  (with w
68850 77 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a  www+1==uuuuu).**
68860 0a 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 20  .**      Word-0 
68870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57 6f                Wo
68880 72 64 2d 31 20 20 20 20 20 20 20 20 20 20 56 61  rd-1          Va
68890 6c 75 65 0a 2a 2a 20 20 31 31 30 31 31 30 77 77  lue.**  110110ww
688a0 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31   wwzzzzyy   1101
688b0 31 31 79 79 20 79 79 78 78 78 78 78 78 20 20 20  11yy yyxxxxxx   
688c0 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79   000uuuuu zzzzyy
688d0 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20  yy yyxxxxxx.**  
688e0 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78  zzzzyyyy yyxxxxx
688f0 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
68900 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30           0000000
68910 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78  0 zzzzyyyy yyxxx
68920 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d  xxx.**.**.** BOM
68930 20 6f 72 20 42 79 74 65 20 4f 72 64 65 72 20 4d   or Byte Order M
68940 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66  ark:.**     0xff
68950 20 30 78 66 65 20 20 20 6c 69 74 74 6c 65 2d 65   0xfe   little-e
68960 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c  ndian utf-16 fol
68970 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78 66 65  lows.**     0xfe
68980 20 30 78 66 66 20 20 20 62 69 67 2d 65 6e 64 69   0xff   big-endi
68990 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77  an utf-16 follow
689a0 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  s.**.*/./*******
689b0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
689c0 76 64 62 65 49 6e 74 2e 68 20 69 6e 20 74 68 65  vdbeInt.h in the
689d0 20 6d 69 64 64 6c 65 20 6f 66 20 75 74 66 2e 63   middle of utf.c
689e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
689f0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
68a00 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
68a10 6c 65 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a  le vdbeInt.h ***
68a20 2a 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 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
68a50 30 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a  03 September 6.*
68a60 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
68a70 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
68a80 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
68a90 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
68aa0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
68ab0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
68ac0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
68ad0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
68ae0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
68af0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
68b00 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
68b10 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
68b20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
68b30 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
68b40 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
68b50 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
68b60 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
68b70 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
68b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 0a 2a 2a  *************.**
68bc0 20 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61   This is the hea
68bd0 64 65 72 20 66 69 6c 65 20 66 6f 72 20 69 6e 66  der file for inf
68be0 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73  ormation that is
68bf0 20 70 72 69 76 61 74 65 20 74 6f 20 74 68 65 0a   private to the.
68c00 2a 2a 20 56 44 42 45 2e 20 20 54 68 69 73 20 69  ** VDBE.  This i
68c10 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20  nformation used 
68c20 74 6f 20 61 6c 6c 20 62 65 20 61 74 20 74 68 65  to all be at the
68c30 20 74 6f 70 20 6f 66 20 74 68 65 20 73 69 6e 67   top of the sing
68c40 6c 65 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64  le.** source cod
68c50 65 20 66 69 6c 65 20 22 76 64 62 65 2e 63 22 2e  e file "vdbe.c".
68c60 20 20 57 68 65 6e 20 74 68 61 74 20 66 69 6c 65    When that file
68c70 20 62 65 63 61 6d 65 20 74 6f 6f 20 62 69 67 20   became too big 
68c80 28 6f 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69  (over.** 6000 li
68c90 6e 65 73 20 6c 6f 6e 67 29 20 69 74 20 77 61 73  nes long) it was
68ca0 20 73 70 6c 69 74 20 75 70 20 69 6e 74 6f 20 73   split up into s
68cb0 65 76 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66  everal smaller f
68cc0 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73  iles and.** this
68cd0 20 68 65 61 64 65 72 20 69 6e 66 6f 72 6d 61 74   header informat
68ce0 69 6f 6e 20 77 61 73 20 66 61 63 74 6f 72 65 64  ion was factored
68cf0 20 6f 75 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66   out..*/.#ifndef
68d00 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 23 64 65   _VDBEINT_H_.#de
68d10 66 69 6e 65 20 5f 56 44 42 45 49 4e 54 5f 48 5f  fine _VDBEINT_H_
68d20 0a 0a 2f 2a 0a 2a 2a 20 69 6e 74 54 6f 4b 65 79  ../*.** intToKey
68d30 28 29 20 61 6e 64 20 6b 65 79 54 6f 49 6e 74 28  () and keyToInt(
68d40 29 20 75 73 65 64 20 74 6f 20 74 72 61 6e 73 66  ) used to transf
68d50 6f 72 6d 20 74 68 65 20 72 6f 77 69 64 2e 20 20  orm the rowid.  
68d60 42 75 74 20 77 69 74 68 0a 2a 2a 20 74 68 65 20  But with.** the 
68d70 6c 61 74 65 73 74 20 76 65 72 73 69 6f 6e 73 20  latest versions 
68d80 6f 66 20 74 68 65 20 64 65 73 69 67 6e 20 74 68  of the design th
68d90 65 79 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a  ey are no-ops..*
68da0 2f 0a 23 64 65 66 69 6e 65 20 6b 65 79 54 6f 49  /.#define keyToI
68db0 6e 74 28 58 29 20 20 20 28 58 29 0a 23 64 65 66  nt(X)   (X).#def
68dc0 69 6e 65 20 69 6e 74 54 6f 4b 65 79 28 58 29 20  ine intToKey(X) 
68dd0 20 20 28 58 29 0a 0a 0a 2f 2a 0a 2a 2a 20 53 51    (X).../*.** SQ
68de0 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 65 64 20  L is translated 
68df0 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20  into a sequence 
68e00 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  of instructions 
68e10 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65  to be.** execute
68e20 64 20 62 79 20 61 20 76 69 72 74 75 61 6c 20 6d  d by a virtual m
68e30 61 63 68 69 6e 65 2e 20 20 45 61 63 68 20 69 6e  achine.  Each in
68e40 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6e 20  struction is an 
68e50 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74  instance.** of t
68e60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
68e70 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64  ucture..*/.typed
68e80 65 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70  ef struct VdbeOp
68e90 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c   Op;../*.** Bool
68ea0 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a 74 79  ean values.*/.ty
68eb0 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63  pedef unsigned c
68ec0 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a  har Bool;../*.**
68ed0 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 20 70   A cursor is a p
68ee0 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20 73 69  ointer into a si
68ef0 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74 68 69  ngle BTree withi
68f00 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  n a database fil
68f10 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72  e..** The cursor
68f20 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61 20 42   can seek to a B
68f30 54 72 65 65 20 65 6e 74 72 79 20 77 69 74 68 20  Tree entry with 
68f40 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79  a particular key
68f50 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65  , or.** loop ove
68f60 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f 66  r all entries of
68f70 20 74 68 65 20 42 74 72 65 65 2e 20 20 59 6f 75   the Btree.  You
68f80 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65 72 74   can also insert
68f90 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20 65 6e   new BTree.** en
68fa0 74 72 69 65 73 20 6f 72 20 72 65 74 72 69 65 76  tries or retriev
68fb0 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64 61 74  e the key or dat
68fc0 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79  a from the entry
68fd0 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
68fe0 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79  .** is currently
68ff0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a   pointing to..**
69000 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f   .** Every curso
69010 72 20 74 68 61 74 20 74 68 65 20 76 69 72 74 75  r that the virtu
69020 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20 6f  al machine has o
69030 70 65 6e 20 69 73 20 72 65 70 72 65 73 65 6e 74  pen is represent
69040 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74  ed by an.** inst
69050 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
69060 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
69070 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 43 75  .**.** If the Cu
69080 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52 6f  rsor.isTriggerRo
69090 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 74  w flag is set it
690a0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73   means that this
690b0 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65   cursor is.** re
690c0 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f  ally a single ro
690d0 77 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74  w that represent
690e0 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44  s the NEW or OLD
690f0 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66   pseudo-table of
69100 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67 65  .** a row trigge
69110 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f 72  r.  The data for
69120 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f 72   the row is stor
69130 65 64 20 69 6e 20 43 75 72 73 6f 72 2e 70 44 61  ed in Cursor.pDa
69140 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 6f  ta and.** the ro
69150 77 69 64 20 69 73 20 69 6e 20 43 75 72 73 6f 72  wid is in Cursor
69160 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74  .iKey..*/.struct
69170 20 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 43 75   Cursor {.  BtCu
69180 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20  rsor *pCursor;  
69190 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
691a0 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 65  structure of the
691b0 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e   backend */.  in
691c0 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
691d0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
691e0 63 75 72 73 6f 72 20 64 61 74 61 62 61 73 65 20  cursor database 
691f0 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28 6f 72  in db->aDb[] (or
69200 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61   -1) */.  i64 la
69210 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20  stRowid;        
69220 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 20 66 72  /* Last rowid fr
69230 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 4e 65 78  om a Next or Nex
69240 74 49 64 78 20 6f 70 65 72 61 74 69 6f 6e 20 2a  tIdx operation *
69250 2f 0a 20 20 69 36 34 20 6e 65 78 74 52 6f 77 69  /.  i64 nextRowi
69260 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  d;        /* Nex
69270 74 20 72 6f 77 69 64 20 72 65 74 75 72 6e 65 64  t rowid returned
69280 20 62 79 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20   by OP_NewRowid 
69290 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f 65 64  */.  Bool zeroed
692a0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ;          /* Tr
692b0 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f 75 74  ue if zeroed out
692c0 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 20 72   and ready for r
692d0 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c 20 72  euse */.  Bool r
692e0 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 20 20  owidIsValid;    
692f0 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 74 52  /* True if lastR
69300 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 2a 2f  owid is valid */
69310 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 74 3b  .  Bool atFirst;
69320 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
69330 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   if pointing to 
69340 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20  first entry */. 
69350 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f 6d 52   Bool useRandomR
69360 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 72 61  owid;  /* Genera
69370 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75  te new record nu
69380 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e 64 6f  mbers semi-rando
69390 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e 75  mly */.  Bool nu
693a0 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 20 2f  llRow;         /
693b0 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 69  * True if pointi
693c0 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 74 68  ng to a row with
693d0 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 42 6f   no data */.  Bo
693e0 6f 6c 20 6e 65 78 74 52 6f 77 69 64 56 61 6c 69  ol nextRowidVali
693f0 64 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74  d;  /* True if t
69400 68 65 20 6e 65 78 74 52 6f 77 69 64 20 66 69 65  he nextRowid fie
69410 6c 64 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20  ld is valid */. 
69420 20 42 6f 6f 6c 20 70 73 65 75 64 6f 54 61 62 6c   Bool pseudoTabl
69430 65 3b 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69  e;     /* This i
69440 73 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70  s a NEW or OLD p
69450 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 6f 66 20  seudo-tables of 
69460 61 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 42  a trigger */.  B
69470 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65  ool deferredMove
69480 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74  to;  /* A call t
69490 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  o sqlite3BtreeMo
694a0 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64  veto() is needed
694b0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62   */.  Bool isTab
694c0 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  le;         /* T
694d0 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72  rue if a table r
694e0 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72  equiring integer
694f0 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20   keys */.  Bool 
69500 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  isIndex;        
69510 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69   /* True if an i
69520 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ndex containing 
69530 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64  keys only - no d
69540 61 74 61 20 2a 2f 0a 20 20 75 38 20 62 6f 67 75  ata */.  u8 bogu
69550 73 49 6e 63 72 4b 65 79 3b 20 20 20 20 20 20 2f  sIncrKey;      /
69560 2a 20 53 6f 6d 65 74 68 69 6e 67 20 66 6f 72 20  * Something for 
69570 70 49 6e 63 72 4b 65 79 20 74 6f 20 70 6f 69 6e  pIncrKey to poin
69580 74 20 74 6f 20 69 66 20 70 4b 65 79 49 6e 66 6f  t to if pKeyInfo
69590 3d 3d 30 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76  ==0 */.  i64 mov
695a0 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f  etoTarget;     /
695b0 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
695c0 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74  e deferred sqlit
695d0 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20  e3BtreeMoveto() 
695e0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b  */.  Btree *pBt;
695f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
69600 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64  parate file hold
69610 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ing temporary ta
69620 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61  ble */.  int nDa
69630 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ta;            /
69640 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
69650 73 20 69 6e 20 70 44 61 74 61 20 2a 2f 0a 20 20  s in pData */.  
69660 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20  char *pData;    
69670 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f        /* Data fo
69680 72 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70  r a NEW or OLD p
69690 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20  seudo-table */. 
696a0 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20   i64 iKey;      
696b0 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 66 6f         /* Key fo
696c0 72 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44  r the NEW or OLD
696d0 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 72 6f   pseudo-table ro
696e0 77 20 2a 2f 0a 20 20 75 38 20 2a 70 49 6e 63 72  w */.  u8 *pIncr
696f0 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Key;         /* 
69700 50 6f 69 6e 74 65 72 20 74 6f 20 70 4b 65 79 49  Pointer to pKeyI
69710 6e 66 6f 2d 3e 69 6e 63 72 4b 65 79 20 2a 2f 0a  nfo->incrKey */.
69720 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
69730 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f 20  nfo;    /* Info 
69740 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79 73  about index keys
69750 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65 78   needed by index
69760 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e   cursors */.  in
69770 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20  t nField;       
69780 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
69790 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 68   fields in the h
697a0 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 73  eader */.  i64 s
697b0 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 20  eqCount;        
697c0 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 6f 75   /* Sequence cou
697d0 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  nter */.  sqlite
697e0 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
697f0 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f 2a 20  VtabCursor;  /* 
69800 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20 61  The cursor for a
69810 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
69820 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  /.  const sqlite
69830 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
69840 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65  e;     /* Module
69850 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 74 61   for cursor pVta
69860 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f 2a  bCursor */..  /*
69870 20 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74   Cached informat
69880 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65  ion about the he
69890 61 64 65 72 20 66 6f 72 20 74 68 65 20 64 61 74  ader for the dat
698a0 61 20 72 65 63 6f 72 64 20 74 68 61 74 20 74 68  a record that th
698b0 65 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73  e.  ** cursor is
698c0 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
698d0 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61  ing to.  Only va
698e0 6c 69 64 20 69 66 20 63 61 63 68 65 56 61 6c 69  lid if cacheVali
698f0 64 20 69 73 20 74 72 75 65 2e 0a 20 20 2a 2a 20  d is true..  ** 
69900 61 52 6f 77 20 6d 69 67 68 74 20 70 6f 69 6e 74  aRow might point
69910 20 74 6f 20 28 65 70 68 65 6d 65 72 61 6c 29 20   to (ephemeral) 
69920 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  data for the cur
69930 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20 69 74 20  rent row, or it 
69940 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55  might.  ** be NU
69950 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 63  LL..  */.  int c
69960 61 63 68 65 53 74 61 74 75 73 3b 20 20 20 20 20  acheStatus;     
69970 20 2f 2a 20 43 61 63 68 65 20 69 73 20 76 61 6c   /* Cache is val
69980 69 64 20 69 66 20 74 68 69 73 20 6d 61 74 63 68  id if this match
69990 65 73 20 56 64 62 65 2e 63 61 63 68 65 43 74 72  es Vdbe.cacheCtr
699a0 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61   */.  int payloa
699b0 64 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54  dSize;      /* T
699c0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62  otal number of b
699d0 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ytes in the reco
699e0 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79  rd */.  u32 *aTy
699f0 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  pe;           /*
69a00 20 54 79 70 65 20 76 61 6c 75 65 73 20 66 6f 72   Type values for
69a10 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20   all entries in 
69a20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
69a30 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20  u32 *aOffset;   
69a40 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20        /* Cached 
69a50 6f 66 66 73 65 74 73 20 74 6f 20 74 68 65 20 73  offsets to the s
69a60 74 61 72 74 20 6f 66 20 65 61 63 68 20 63 6f 6c  tart of each col
69a70 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a 20 20 75  umns data */.  u
69a80 38 20 2a 61 52 6f 77 3b 20 20 20 20 20 20 20 20  8 *aRow;        
69a90 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72       /* Data for
69aa0 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
69ab0 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20  , if all on one 
69ac0 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64  page */.};.typed
69ad0 65 66 20 73 74 72 75 63 74 20 43 75 72 73 6f 72  ef struct Cursor
69ae0 20 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20   Cursor;../*.** 
69af0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
69b00 6f 66 20 73 74 72 69 6e 67 20 73 74 6f 72 61 67  of string storag
69b10 65 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c  e space availabl
69b20 65 20 74 6f 20 65 61 63 68 20 73 74 61 63 6b 0a  e to each stack.
69b30 2a 2a 20 6c 61 79 65 72 20 77 69 74 68 6f 75 74  ** layer without
69b40 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6c 6c 6f   having to mallo
69b50 63 2e 20 20 4e 42 46 53 20 69 73 20 73 68 6f 72  c.  NBFS is shor
69b60 74 20 66 6f 72 20 4e 75 6d 62 65 72 20 6f 66 20  t for Number of 
69b70 42 79 74 65 73 0a 2a 2a 20 46 6f 72 20 53 74 72  Bytes.** For Str
69b80 69 6e 67 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ings..*/.#define
69b90 20 4e 42 46 53 20 33 32 0a 0a 2f 2a 0a 2a 2a 20   NBFS 32../*.** 
69ba0 41 20 76 61 6c 75 65 20 66 6f 72 20 43 75 72 73  A value for Curs
69bb0 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74 68  or.cacheValid th
69bc0 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61 63  at means the cac
69bd0 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 76  he is always inv
69be0 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  alid..*/.#define
69bf0 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a 0a   CACHE_STALE 0..
69c00 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c 79  /*.** Internally
69c10 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69 70  , the vdbe manip
69c20 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61 6c  ulates nearly al
69c30 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73 20  l SQL values as 
69c40 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  Mem.** structure
69c50 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 75  s. Each Mem stru
69c60 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 6c  ct may cache mul
69c70 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 61  tiple representa
69c80 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a 2a  tions (string,.*
69c90 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 20  * integer etc.) 
69ca0 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  of the same valu
69cb0 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 6e 64  e.  A value (and
69cc0 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d 20 73   therefore Mem s
69cd0 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 61 73  tructure).** has
69ce0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70   the following p
69cf0 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a  roperties:.**.**
69d00 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 73 20   Each value has 
69d10 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 2e  a manifest type.
69d20 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 74 79   The manifest ty
69d30 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  pe of the value 
69d40 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 20 4d  stored.** in a M
69d50 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 65 74  em struct is ret
69d60 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d 65 6d  urned by the Mem
69d70 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 72 6f  Type(Mem*) macro
69d80 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a 2a 2a  . The type is.**
69d90 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e   one of SQLITE_N
69da0 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45  ULL, SQLITE_INTE
69db0 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 41 4c  GER, SQLITE_REAL
69dc0 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 6f 72  , SQLITE_TEXT or
69dd0 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f 42 2e  .** SQLITE_BLOB.
69de0 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 20 7b  .*/.struct Mem {
69df0 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69  .  union {.    i
69e00 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  64 i;           
69e10 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
69e20 6c 75 65 2e 20 4f 72 20 46 75 6e 63 44 65 66 2a  lue. Or FuncDef*
69e30 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d   when flags==MEM
69e40 5f 41 67 67 20 2a 2f 0a 20 20 20 20 46 75 6e 63  _Agg */.    Func
69e50 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20  Def *pDef;      
69e60 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 65  /* Used only whe
69e70 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67  n flags==MEM_Agg
69e80 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 6f 75   */.  } u;.  dou
69e90 62 6c 65 20 72 3b 20 20 20 20 20 20 20 20 20 20  ble r;          
69ea0 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 2a   /* Real value *
69eb0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
69ec0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
69ed0 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61  ssociated databa
69ee0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
69ef0 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20  .  char *z;     
69f00 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67         /* String
69f10 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 20 2a   or BLOB value *
69f20 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20  /.  int n;      
69f30 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
69f40 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
69f50 69 6e 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c  in string value,
69f60 20 69 6e 63 6c 75 64 69 6e 67 20 27 5c 30 27 20   including '\0' 
69f70 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20  */.  u16 flags; 
69f80 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65           /* Some
69f90 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20   combination of 
69fa0 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74  MEM_Null, MEM_St
69fb0 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 63 2e  r, MEM_Dyn, etc.
69fc0 20 2a 2f 0a 20 20 75 38 20 20 74 79 70 65 3b 20   */.  u8  type; 
69fd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
69fe0 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c   of SQLITE_NULL,
69ff0 20 53 51 4c 49 54 45 5f 54 45 58 54 2c 20 53 51   SQLITE_TEXT, SQ
6a000 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 65 74  LITE_INTEGER, et
6a010 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e 63 3b 20  c */.  u8  enc; 
6a020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51             /* SQ
6a030 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
6a040 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54  E_UTF16BE, SQLIT
6a050 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a 20 20 76  E_UTF16LE */.  v
6a060 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
6a070 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e 6f 74 20   *);  /* If not 
6a080 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68 69 73 20  null, call this 
6a090 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65  function to dele
6a0a0 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 20 63 68  te Mem.z */.  ch
6a0b0 61 72 20 7a 53 68 6f 72 74 5b 4e 42 46 53 5d 3b  ar zShort[NBFS];
6a0c0 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 73    /* Space for s
6a0d0 68 6f 72 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a  hort strings */.
6a0e0 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  };.typedef struc
6a0f0 74 20 4d 65 6d 20 4d 65 6d 3b 0a 0a 2f 2a 20 4f  t Mem Mem;../* O
6a100 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
6a110 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67  e following flag
6a120 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e 64  s are set to ind
6a130 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 4f  icate the validO
6a140 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74  K.** representat
6a150 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c 75  ions of the valu
6a160 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
6a170 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a  Mem struct..**.*
6a180 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 6c  * If the MEM_Nul
6a190 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  l flag is set, t
6a1a0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
6a1b0 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c   an SQL NULL val
6a1c0 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20  ue..** No other 
6a1d0 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74  flags may be set
6a1e0 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a   in this case..*
6a1f0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f  *.** If the MEM_
6a200 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20  Str flag is set 
6a210 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74  then Mem.z point
6a220 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65  s at a string re
6a230 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a  presentation..**
6a240 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73   Usually this is
6a250 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20   encoded in the 
6a260 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63  same unicode enc
6a270 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69  oding as the mai
6a280 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 73  n.** database (s
6a290 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 63  ee below for exc
6a2a0 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 65  eptions). If the
6a2b0 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 69   MEM_Term flag i
6a2c0 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 74  s also.** set, t
6a2d0 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 69  hen the string i
6a2e0 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  s nul terminated
6a2f0 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 6e  . The MEM_Int an
6a300 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66  d MEM_Real .** f
6a310 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 74  lags may coexist
6a320 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 74   with the MEM_St
6a330 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75  r flag..**.** Mu
6a340 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65 20  ltiple of these 
6a350 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65 61  values can appea
6a360 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e 20  r in Mem.flags. 
6a370 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a   But only one.**
6a380 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 61   at a time can a
6a390 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79 70  ppear in Mem.typ
6a3a0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  e..*/.#define ME
6a3b0 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30 30  M_Null      0x00
6a3c0 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  01   /* Value is
6a3d0 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
6a3e0 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20 30   MEM_Str       0
6a3f0 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75 65  x0002   /* Value
6a400 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f 0a   is a string */.
6a410 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74 20  #define MEM_Int 
6a420 20 20 20 20 20 20 30 78 30 30 30 34 20 20 20 2f        0x0004   /
6a430 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69 6e  * Value is an in
6a440 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
6a450 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20 30   MEM_Real      0
6a460 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75 65  x0008   /* Value
6a470 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62 65   is a real numbe
6a480 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  r */.#define MEM
6a490 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30 31  _Blob      0x001
6a4a0 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  0   /* Value is 
6a4b0 61 20 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 20 57 68  a BLOB */../* Wh
6a4c0 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e 74 61  enever Mem conta
6a4d0 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74 72 69  ins a valid stri
6a4e0 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70 72 65  ng or blob repre
6a4f0 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65 20 6f  sentation, one o
6a500 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69  f.** the followi
6a510 6e 67 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65  ng flags must be
6a520 20 73 65 74 20 74 6f 20 64 65 74 65 72 6d 69 6e   set to determin
6a530 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e  e the memory man
6a540 61 67 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c 69 63  agement.** polic
6a550 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20 54 68  y for Mem.z.  Th
6a560 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20  e MEM_Term flag 
6a570 74 65 6c 6c 73 20 75 73 20 77 68 65 74 68 65 72  tells us whether
6a580 20 6f 72 20 6e 6f 74 20 74 68 65 0a 2a 2a 20 73   or not the.** s
6a590 74 72 69 6e 67 20 69 73 20 5c 30 30 30 20 6f 72  tring is \000 or
6a5a0 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74   \u0000 terminat
6a5b0 65 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  ed.*/.#define ME
6a5c0 4d 5f 54 65 72 6d 20 20 20 20 20 20 30 78 30 30  M_Term      0x00
6a5d0 32 30 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 72  20   /* String r
6a5e0 65 70 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e  ep is nul termin
6a5f0 61 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ated */.#define 
6a600 4d 45 4d 5f 44 79 6e 20 20 20 20 20 20 20 30 78  MEM_Dyn       0x
6a610 30 30 34 30 20 20 20 2f 2a 20 4e 65 65 64 20 74  0040   /* Need t
6a620 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 46 72 65  o call sqliteFre
6a630 65 28 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a  e() on Mem.z */.
6a640 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 61 74  #define MEM_Stat
6a650 69 63 20 20 20 20 30 78 30 30 38 30 20 20 20 2f  ic    0x0080   /
6a660 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74  * Mem.z points t
6a670 6f 20 61 20 73 74 61 74 69 63 20 73 74 72 69 6e  o a static strin
6a680 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  g */.#define MEM
6a690 5f 45 70 68 65 6d 20 20 20 20 20 30 78 30 31 30  _Ephem     0x010
6a6a0 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69  0   /* Mem.z poi
6a6b0 6e 74 73 20 74 6f 20 61 6e 20 65 70 68 65 6d 65  nts to an epheme
6a6c0 72 61 6c 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64  ral string */.#d
6a6d0 65 66 69 6e 65 20 4d 45 4d 5f 53 68 6f 72 74 20  efine MEM_Short 
6a6e0 20 20 20 20 30 78 30 32 30 30 20 20 20 2f 2a 20      0x0200   /* 
6a6f0 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20  Mem.z points to 
6a700 4d 65 6d 2e 7a 53 68 6f 72 74 20 2a 2f 0a 23 64  Mem.zShort */.#d
6a710 65 66 69 6e 65 20 4d 45 4d 5f 41 67 67 20 20 20  efine MEM_Agg   
6a720 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20      0x0400   /* 
6a730 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20  Mem.z points to 
6a740 61 6e 20 61 67 67 20 66 75 6e 63 74 69 6f 6e 20  an agg function 
6a750 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 64 65 66 69  context */.#defi
6a760 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 20 20 20 20  ne MEM_Zero     
6a770 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d   0x0800   /* Mem
6a780 2e 69 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e  .i contains coun
6a790 74 20 6f 66 20 30 73 20 61 70 70 65 6e 64 65 64  t of 0s appended
6a7a0 20 74 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23 69 66   to blob */..#if
6a7b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
6a7c0 49 4e 43 52 42 4c 4f 42 0a 20 20 23 75 6e 64 65  INCRBLOB.  #unde
6a7d0 66 20 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23 64 65  f MEM_Zero.  #de
6a7e0 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 30 78  fine MEM_Zero 0x
6a7f0 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  0000.#endif.../*
6a800 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a   A VdbeFunc is j
6a810 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64  ust a FuncDef (d
6a820 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65  efined in sqlite
6a830 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74  Int.h) that cont
6a840 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e  ains.** addition
6a850 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  al information a
6a860 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69  bout auxiliary i
6a870 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64  nformation bound
6a880 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a   to arguments.**
6a890 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
6a8a0 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
6a8b0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
6a8c0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78   sqlite3_get_aux
6a8d0 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71  data().** and sq
6a8e0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
6a8f0 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22  a() APIs.  The "
6a900 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65  auxdata" is some
6a910 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a   auxiliary data.
6a920 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61  ** that can be a
6a930 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
6a940 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65   constant argume
6a950 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  nt to a function
6a960 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77  .  This.** allow
6a970 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68  s functions such
6a980 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20   as "regexp" to 
6a990 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f  compile their co
6a9a0 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a  nstant regular.*
6a9b0 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67  * expression arg
6a9c0 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72  ument once and r
6a9d0 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c  eused the compil
6a9e0 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74  ed code for mult
6a9f0 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  iple.** invocati
6aa00 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  ons..*/.struct V
6aa10 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63  dbeFunc {.  Func
6aa20 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20  Def *pFunc;     
6aa30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6aa40 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74   definition of t
6aa50 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
6aa60 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20   int nAux;      
6aa70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6aa80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
6aa90 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ies allocated fo
6aaa0 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73  r apAux[] */.  s
6aab0 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a  truct AuxData {.
6aac0 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20      void *pAux; 
6aad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6aae0 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f    /* Aux data fo
6aaf0 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d  r the i-th argum
6ab00 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20  ent */.    void 
6ab10 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20  (*xDelete)(void 
6ab20 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74  *);      /* Dest
6ab30 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61  ructor for the a
6ab40 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61  ux data */.  } a
6ab50 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20  pAux[1];        
6ab60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
6ab70 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20  e slot for each 
6ab80 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
6ab90 74 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  t */.};.typedef 
6aba0 73 74 72 75 63 74 20 56 64 62 65 46 75 6e 63 20  struct VdbeFunc 
6abb0 56 64 62 65 46 75 6e 63 3b 0a 0a 2f 2a 0a 2a 2a  VdbeFunc;../*.**
6abc0 20 54 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61   The "context" a
6abd0 72 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e  rgument for a in
6abe0 73 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69  stallable functi
6abf0 6f 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74  on.  A pointer t
6ac00 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
6ac10 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
6ac20 72 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20  re is the first 
6ac30 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
6ac40 72 6f 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a  routines used.**
6ac50 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53   implement the S
6ac60 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  QL functions..**
6ac70 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74  .** There is a t
6ac80 79 70 65 64 65 66 20 66 6f 72 20 74 68 69 73 20  ypedef for this 
6ac90 73 74 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c  structure in sql
6aca0 69 74 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72  ite.h.  So all r
6acb0 6f 75 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e  outines,.** even
6acc0 20 74 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65   the public inte
6acd0 72 66 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c  rface to SQLite,
6ace0 20 63 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74   can use a point
6acf0 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63  er to this struc
6ad00 74 75 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69  ture..** But thi
6ad10 73 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e  s file is the on
6ad20 6c 79 20 70 6c 61 63 65 20 77 68 65 72 65 20 74  ly place where t
6ad30 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61  he internal deta
6ad40 69 6c 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73  ils of this.** s
6ad50 74 72 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f  tructure are kno
6ad60 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  wn..**.** This s
6ad70 74 72 75 63 74 75 72 65 20 69 73 20 64 65 66 69  tructure is defi
6ad80 6e 65 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64  ned inside of vd
6ad90 62 65 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20  beInt.h because 
6ada0 69 74 20 75 73 65 73 20 73 75 62 73 74 72 75 63  it uses substruc
6adb0 74 75 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77  tures.** (Mem) w
6adc0 68 69 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65  hich are only de
6add0 66 69 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a  fined there..*/.
6ade0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63  struct sqlite3_c
6adf0 6f 6e 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44  ontext {.  FuncD
6ae00 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20  ef *pFunc;      
6ae10 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66   /* Pointer to f
6ae20 75 6e 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74  unction informat
6ae30 69 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49  ion.  MUST BE FI
6ae40 52 53 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e  RST */.  VdbeFun
6ae50 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f  c *pVdbeFunc;  /
6ae60 2a 20 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c  * Auxilary data,
6ae70 20 69 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a   if created. */.
6ae80 20 20 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20    Mem s;        
6ae90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
6aea0 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73  eturn value is s
6aeb0 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20  tored here */.  
6aec0 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20  Mem *pMem;      
6aed0 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
6aee0 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f  cell used to sto
6aef0 72 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e  re aggregate con
6af00 74 65 78 74 20 2a 2f 0a 20 20 75 38 20 69 73 45  text */.  u8 isE
6af10 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  rror;           
6af20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 66  /* Set to true f
6af30 6f 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20  or an error */. 
6af40 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
6af50 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74         /* Collat
6af60 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
6af70 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 65 74 20  };../*.** A Set 
6af80 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
6af90 64 20 66 6f 72 20 71 75 69 63 6b 20 74 65 73 74  d for quick test
6afa0 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 61 20  ing to see if a 
6afb0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 61 72 74  value.** is part
6afc0 20 6f 66 20 61 20 73 6d 61 6c 6c 20 73 65 74 2e   of a small set.
6afd0 20 20 53 65 74 73 20 61 72 65 20 75 73 65 64 20    Sets are used 
6afe0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 64  to implement cod
6aff0 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 69 73 3a 0a  e like.** this:.
6b000 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 2e  **            x.
6b010 79 20 49 4e 20 28 27 68 69 27 2c 27 68 6f 6f 27  y IN ('hi','hoo'
6b020 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 79 70 65 64  ,'hum').*/.typed
6b030 65 66 20 73 74 72 75 63 74 20 53 65 74 20 53 65  ef struct Set Se
6b040 74 3b 0a 73 74 72 75 63 74 20 53 65 74 20 7b 0a  t;.struct Set {.
6b050 20 20 48 61 73 68 20 68 61 73 68 3b 20 20 20 20    Hash hash;    
6b060 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 65           /* A se
6b070 74 20 69 73 20 6a 75 73 74 20 61 20 68 61 73 68  t is just a hash
6b080 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68   table */.  Hash
6b090 45 6c 65 6d 20 2a 70 72 65 76 3b 20 20 20 20 20  Elem *prev;     
6b0a0 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 6c 79     /* Previously
6b0b0 20 61 63 63 65 73 73 65 64 20 68 61 73 68 20 65   accessed hash e
6b0c0 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  lemen */.};../*.
6b0d0 2a 2a 20 41 20 46 69 66 6f 50 61 67 65 20 73 74  ** A FifoPage st
6b0e0 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61 20  ructure holds a 
6b0f0 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 76  single page of v
6b100 61 6c 76 65 73 2e 20 20 50 61 67 65 73 20 61 72  alves.  Pages ar
6b110 65 20 61 72 72 61 6e 67 65 64 0a 2a 2a 20 69 6e  e arranged.** in
6b120 20 61 20 6c 69 73 74 2e 0a 2a 2f 0a 74 79 70 65   a list..*/.type
6b130 64 65 66 20 73 74 72 75 63 74 20 46 69 66 6f 50  def struct FifoP
6b140 61 67 65 20 46 69 66 6f 50 61 67 65 3b 0a 73 74  age FifoPage;.st
6b150 72 75 63 74 20 46 69 66 6f 50 61 67 65 20 7b 0a  ruct FifoPage {.
6b160 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20 20 20    int nSlot;    
6b170 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6b180 66 20 65 6e 74 72 69 65 73 20 61 53 6c 6f 74 5b  f entries aSlot[
6b190 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 57 72 69 74  ] */.  int iWrit
6b1a0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 75 73  e;        /* Pus
6b1b0 68 20 74 68 65 20 6e 65 78 74 20 76 61 6c 75 65  h the next value
6b1c0 20 69 6e 74 6f 20 74 68 69 73 20 65 6e 74 72 79   into this entry
6b1d0 20 69 6e 20 61 53 6c 6f 74 5b 5d 20 2a 2f 0a 20   in aSlot[] */. 
6b1e0 20 69 6e 74 20 69 52 65 61 64 3b 20 20 20 20 20   int iRead;     
6b1f0 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
6b200 6e 65 78 74 20 76 61 6c 75 65 20 66 72 6f 6d 20  next value from 
6b210 74 68 69 73 20 65 6e 74 72 79 20 69 6e 20 61 53  this entry in aS
6b220 6c 6f 74 5b 5d 20 2a 2f 0a 20 20 46 69 66 6f 50  lot[] */.  FifoP
6b230 61 67 65 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a  age *pNext;   /*
6b240 20 4e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68   Next page in th
6b250 65 20 66 69 66 6f 20 2a 2f 0a 20 20 69 36 34 20  e fifo */.  i64 
6b260 61 53 6c 6f 74 5b 31 5d 3b 20 20 20 20 20 20 2f  aSlot[1];      /
6b270 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 6c  * One or more sl
6b280 6f 74 73 20 66 6f 72 20 72 6f 77 69 64 20 76 61  ots for rowid va
6b290 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  lues */.};../*.*
6b2a0 2a 20 54 68 65 20 46 69 66 6f 20 73 74 72 75 63  * The Fifo struc
6b2b0 74 75 72 65 20 69 73 20 74 79 70 65 64 65 66 2d  ture is typedef-
6b2c0 65 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 2e  ed in vdbeInt.h.
6b2d0 20 20 42 75 74 20 74 68 65 20 69 6d 70 6c 65 6d    But the implem
6b2e0 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74  entation.** of t
6b2f0 68 61 74 20 73 74 72 75 63 74 75 72 65 20 69 73  hat structure is
6b300 20 70 72 69 76 61 74 65 20 74 6f 20 74 68 69 73   private to this
6b310 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   file..**.** The
6b320 20 46 69 66 6f 20 73 74 72 75 63 74 75 72 65 20   Fifo structure 
6b330 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 6e  describes the en
6b340 74 69 72 65 20 66 69 66 6f 2e 20 20 0a 2a 2f 0a  tire fifo.  .*/.
6b350 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
6b360 69 66 6f 20 46 69 66 6f 3b 0a 73 74 72 75 63 74  ifo Fifo;.struct
6b370 20 46 69 66 6f 20 7b 0a 20 20 69 6e 74 20 6e 45   Fifo {.  int nE
6b380 6e 74 72 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  ntry;         /*
6b390 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
6b3a0 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20 46 69   entries */.  Fi
6b3b0 66 6f 50 61 67 65 20 2a 70 46 69 72 73 74 3b 20  foPage *pFirst; 
6b3c0 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20    /* First page 
6b3d0 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  on the list */. 
6b3e0 20 46 69 66 6f 50 61 67 65 20 2a 70 4c 61 73 74   FifoPage *pLast
6b3f0 3b 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 61 67  ;    /* Last pag
6b400 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f  e on the list */
6b410 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f 6e  .};../*.** A Con
6b420 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 20  text stores the 
6b430 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69  last insert rowi
6b440 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 74  d, the last stat
6b450 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f 75  ement change cou
6b460 6e 74 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63  nt,.** and the c
6b470 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74  urrent statement
6b480 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 28 69   change count (i
6b490 2e 65 2e 20 63 68 61 6e 67 65 73 20 73 69 6e 63  .e. changes sinc
6b4a0 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74  e last statement
6b4b0 29 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e  )..** The curren
6b4c0 74 20 6b 65 79 6c 69 73 74 20 69 73 20 61 6c 73  t keylist is als
6b4d0 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  o stored in the 
6b4e0 63 6f 6e 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 6d  context..** Elem
6b4f0 65 6e 74 73 20 6f 66 20 43 6f 6e 74 65 78 74 20  ents of Context 
6b500 73 74 72 75 63 74 75 72 65 20 74 79 70 65 20 6d  structure type m
6b510 61 6b 65 20 75 70 20 74 68 65 20 43 6f 6e 74 65  ake up the Conte
6b520 78 74 53 74 61 63 6b 2c 20 77 68 69 63 68 20 69  xtStack, which i
6b530 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 62 79 20  s.** updated by 
6b540 74 68 65 20 43 6f 6e 74 65 78 74 50 75 73 68 20  the ContextPush 
6b550 61 6e 64 20 43 6f 6e 74 65 78 74 50 6f 70 20 6f  and ContextPop o
6b560 70 63 6f 64 65 73 20 28 75 73 65 64 20 62 79 20  pcodes (used by 
6b570 74 72 69 67 67 65 72 73 29 2e 0a 2a 2a 20 54 68  triggers)..** Th
6b580 65 20 63 6f 6e 74 65 78 74 20 69 73 20 70 75 73  e context is pus
6b590 68 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75  hed before execu
6b5a0 74 69 6e 67 20 61 20 74 72 69 67 67 65 72 20 61  ting a trigger a
6b5b0 20 70 6f 70 70 65 64 20 77 68 65 6e 20 74 68 65   popped when the
6b5c0 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69 6e 69  .** trigger fini
6b5d0 73 68 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  shes..*/.typedef
6b5e0 20 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74 20   struct Context 
6b5f0 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20  Context;.struct 
6b600 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 36 34 20  Context {.  i64 
6b610 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 2f 2a  lastRowid;    /*
6b620 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77   Last insert row
6b630 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 74  id (sqlite3.last
6b640 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20  Rowid) */.  int 
6b650 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a  nChange;      /*
6b660 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67   Statement chang
6b670 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 65  es (Vdbe.nChange
6b680 73 29 20 20 20 20 20 2a 2f 0a 20 20 46 69 66 6f  s)     */.  Fifo
6b690 20 73 46 69 66 6f 3b 20 20 20 20 20 20 20 2f 2a   sFifo;       /*
6b6a0 20 52 65 63 6f 72 64 73 20 74 68 61 74 20 77 69   Records that wi
6b6b0 6c 6c 20 70 61 72 74 69 63 69 70 61 74 65 20 69  ll participate i
6b6c0 6e 20 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50  n a DELETE or UP
6b6d0 44 41 54 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  DATE */.};../*.*
6b6e0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
6b6f0 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   the virtual mac
6b700 68 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75  hine.  This stru
6b710 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74  cture contains t
6b720 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73  he complete.** s
6b730 74 61 74 65 20 6f 66 20 74 68 65 20 76 69 72 74  tate of the virt
6b740 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a  ual machine..**.
6b750 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f  ** The "sqlite3_
6b760 73 74 6d 74 22 20 73 74 72 75 63 74 75 72 65 20  stmt" structure 
6b770 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20  pointer that is 
6b780 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
6b790 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a  te3_compile().**
6b7a0 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69   is really a poi
6b7b0 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
6b7c0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
6b7d0 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cture..**.** The
6b7e0 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68   Vdbe.inVtabMeth
6b7f0 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73  od variable is s
6b800 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66  et to non-zero f
6b810 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20  or the duration 
6b820 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61  of.** any virtua
6b830 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69  l table method i
6b840 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20  nvocations made 
6b850 62 79 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  by the vdbe prog
6b860 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65  ram. It is.** se
6b870 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74  t to 2 for xDest
6b880 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73  roy method calls
6b890 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f   and 1 for all o
6b8a0 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68  ther methods. Th
6b8b0 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69  is.** variable i
6b8c0 73 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70  s used for two p
6b8d0 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f  urposes: to allo
6b8e0 77 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f  w xDestroy metho
6b8f0 64 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a  ds to execute.**
6b900 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74   "DROP TABLE" st
6b910 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20  atements and to 
6b920 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73  prevent some nas
6b930 74 79 20 73 69 64 65 20 65 66 66 65 63 74 73 20  ty side effects 
6b940 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69  of.** malloc fai
6b950 6c 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65  lure when SQLite
6b960 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75   is invoked recu
6b970 72 73 69 76 65 6c 79 20 62 79 20 61 20 76 69 72  rsively by a vir
6b980 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d  tual table .** m
6b990 65 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a  ethod function..
6b9a0 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b  */.struct Vdbe {
6b9b0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
6b9c0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68         /* The wh
6b9d0 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ole database */.
6b9e0 20 20 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70    Vdbe *pPrev,*p
6b9f0 4e 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b 65 64 20  Next; /* Linked 
6ba00 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 69  list of VDBEs wi
6ba10 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 65  th the same Vdbe
6ba20 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70  .db */.  int nOp
6ba30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
6ba40 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75  Number of instru
6ba50 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72  ctions in the pr
6ba60 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  ogram */.  int n
6ba70 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f  OpAlloc;       /
6ba80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  * Number of slot
6ba90 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
6baa0 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61  aOp[] */.  Op *a
6bab0 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Op;            /
6bac0 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
6bad0 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
6bae0 69 6e 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f  ine's program */
6baf0 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20  .  int nLabel;  
6bb00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6bb10 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 20   of labels used 
6bb20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 41  */.  int nLabelA
6bb30 6c 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e 75 6d 62  lloc;    /* Numb
6bb40 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f  er of slots allo
6bb50 63 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b  cated in aLabel[
6bb60 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62  ] */.  int *aLab
6bb70 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 70  el;        /* Sp
6bb80 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
6bb90 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20  labels */.  Mem 
6bba0 2a 61 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20  *aStack;        
6bbb0 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e 64 20 73  /* The operand s
6bbc0 74 61 63 6b 2c 20 65 78 63 65 70 74 20 73 74 72  tack, except str
6bbd0 69 6e 67 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  ing values */.  
6bbe0 4d 65 6d 20 2a 70 54 6f 73 3b 20 20 20 20 20 20  Mem *pTos;      
6bbf0 20 20 20 20 2f 2a 20 54 6f 70 20 65 6e 74 72 79      /* Top entry
6bc00 20 69 6e 20 74 68 65 20 6f 70 65 72 61 6e 64 20   in the operand 
6bc10 73 74 61 63 6b 20 2a 2f 0a 20 20 4d 65 6d 20 2a  stack */.  Mem *
6bc20 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 2f  *apArg;        /
6bc30 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63  * Arguments to c
6bc40 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69  urrently executi
6bc50 6e 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  ng user function
6bc60 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e   */.  Mem *aColN
6bc70 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ame;      /* Col
6bc80 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 74  umn names to ret
6bc90 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75  urn */.  int nCu
6bca0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  rsor;        /* 
6bcb0 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20  Number of slots 
6bcc0 69 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20  in apCsr[] */.  
6bcd0 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20  Cursor **apCsr; 
6bce0 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65      /* One eleme
6bcf0 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72 61 79  nt of this array
6bd00 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63   for each open c
6bd10 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  ursor */.  int n
6bd20 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Var;           /
6bd30 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
6bd40 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a 2f  ies in aVar[] */
6bd50 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20 20  .  Mem *aVar;   
6bd60 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73         /* Values
6bd70 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69   for the OP_Vari
6bd80 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a  able opcode. */.
6bd90 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20    char **azVar; 
6bda0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
6bdb0 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20   variables */.  
6bdc0 69 6e 74 20 6f 6b 56 61 72 3b 20 20 20 20 20 20  int okVar;      
6bdd0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
6bde0 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e 20  zVar[] has been 
6bdf0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
6be00 20 69 6e 74 20 6d 61 67 69 63 3b 20 20 20 20 20   int magic;     
6be10 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69           /* Magi
6be20 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 61 6e  c number for san
6be30 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
6be40 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20    int nMem;     
6be50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
6be60 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f  ber of memory lo
6be70 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c  cations currentl
6be80 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20  y allocated */. 
6be90 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20   Mem *aMem;     
6bea0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
6beb0 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
6bec0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 6c 6c 62   */.  int nCallb
6bed0 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ack;          /*
6bee0 20 4e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 62   Number of callb
6bef0 61 63 6b 73 20 69 6e 76 6f 6b 65 64 20 73 6f 20  acks invoked so 
6bf00 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63  far */.  int cac
6bf10 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20  heCtr;          
6bf20 20 2f 2a 20 43 75 72 73 6f 72 20 72 6f 77 20 63   /* Cursor row c
6bf30 61 63 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20  ache generation 
6bf40 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 46 69 66  counter */.  Fif
6bf50 6f 20 73 46 69 66 6f 3b 20 20 20 20 20 20 20 20  o sFifo;        
6bf60 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f       /* A list o
6bf70 66 20 52 4f 57 49 44 73 20 2a 2f 0a 20 20 69 6e  f ROWIDs */.  in
6bf80 74 20 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f  t contextStackTo
6bf90 70 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f  p;    /* Index o
6bfa0 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e  f top element in
6bfb0 20 74 68 65 20 63 6f 6e 74 65 78 74 20 73 74 61   the context sta
6bfc0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 74  ck */.  int cont
6bfd0 65 78 74 53 74 61 63 6b 44 65 70 74 68 3b 20 20  extStackDepth;  
6bfe0 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  /* The size of t
6bff0 68 65 20 22 63 6f 6e 74 65 78 74 22 20 73 74 61  he "context" sta
6c000 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74 65 78 74 20  ck */.  Context 
6c010 2a 63 6f 6e 74 65 78 74 53 74 61 63 6b 3b 20 20  *contextStack;  
6c020 2f 2a 20 53 74 61 63 6b 20 75 73 65 64 20 62 79  /* Stack used by
6c030 20 6f 70 63 6f 64 65 73 20 43 6f 6e 74 65 78 74   opcodes Context
6c040 50 75 73 68 20 26 20 43 6f 6e 74 65 78 74 50 6f  Push & ContextPo
6c050 70 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20  p*/.  int pc;   
6c060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6c070 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75   The program cou
6c080 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63  nter */.  int rc
6c090 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6c0a0 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
6c0b0 74 75 72 6e 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  turn */.  unsign
6c0c0 65 64 20 75 6e 69 71 75 65 43 6e 74 3b 20 20 20  ed uniqueCnt;   
6c0d0 20 20 2f 2a 20 55 73 65 64 20 62 79 20 4f 50 5f    /* Used by OP_
6c0e0 4d 61 6b 65 52 65 63 6f 72 64 20 77 68 65 6e 20  MakeRecord when 
6c0f0 50 32 21 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 65  P2!=0 */.  int e
6c100 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20  rrorAction;     
6c110 20 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61     /* Recovery a
6c120 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63  ction to do in c
6c130 61 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20  ase of an error 
6c140 2a 2f 0a 20 20 69 6e 74 20 69 6e 54 65 6d 70 54  */.  int inTempT
6c150 72 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20  rans;        /* 
6c160 54 72 75 65 20 69 66 20 74 65 6d 70 20 64 61 74  True if temp dat
6c170 61 62 61 73 65 20 69 73 20 74 72 61 6e 73 61 63  abase is transac
6c180 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20  tioned */.  int 
6c190 72 65 74 75 72 6e 53 74 61 63 6b 5b 32 35 5d 3b  returnStack[25];
6c1a0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64      /* Return ad
6c1b0 64 72 65 73 73 20 73 74 61 63 6b 20 66 6f 72 20  dress stack for 
6c1c0 4f 50 5f 47 6f 73 75 62 20 26 20 4f 50 5f 52 65  OP_Gosub & OP_Re
6c1d0 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 65  turn */.  int re
6c1e0 74 75 72 6e 44 65 70 74 68 3b 20 20 20 20 20 20  turnDepth;      
6c1f0 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65 64    /* Next unused
6c200 20 65 6c 65 6d 65 6e 74 20 69 6e 20 72 65 74 75   element in retu
6c210 72 6e 53 74 61 63 6b 5b 5d 20 2a 2f 0a 20 20 69  rnStack[] */.  i
6c220 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b 20 20  nt nResColumn;  
6c230 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6c240 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f   of columns in o
6c250 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  ne row of the re
6c260 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 68  sult set */.  ch
6c270 61 72 20 2a 2a 61 7a 52 65 73 43 6f 6c 75 6d 6e  ar **azResColumn
6c280 3b 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20  ;     /* Values 
6c290 66 6f 72 20 6f 6e 65 20 72 6f 77 20 6f 66 20 72  for one row of r
6c2a0 65 73 75 6c 74 20 2a 2f 20 0a 20 20 69 6e 74 20  esult */ .  int 
6c2b0 70 6f 70 53 74 61 63 6b 3b 20 20 20 20 20 20 20  popStack;       
6c2c0 20 20 20 20 2f 2a 20 50 6f 70 20 74 68 65 20 73      /* Pop the s
6c2d0 74 61 63 6b 20 74 68 69 73 20 6d 75 63 68 20 6f  tack this much o
6c2e0 6e 20 65 6e 74 72 79 20 74 6f 20 56 64 62 65 45  n entry to VdbeE
6c2f0 78 65 63 28 29 20 2a 2f 0a 20 20 63 68 61 72 20  xec() */.  char 
6c300 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20  *zErrMsg;       
6c310 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
6c320 61 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 65  age written here
6c330 20 2a 2f 0a 20 20 75 38 20 72 65 73 4f 6e 53 74   */.  u8 resOnSt
6c340 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ack;          /*
6c350 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61   True if there a
6c360 72 65 20 72 65 73 75 6c 74 20 76 61 6c 75 65 73  re result values
6c370 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 2a 2f   on the stack */
6c380 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20  .  u8 explain;  
6c390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
6c3a0 75 65 20 69 66 20 45 58 50 4c 41 49 4e 20 70 72  ue if EXPLAIN pr
6c3b0 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20 63 6f 6d  esent on SQL com
6c3c0 6d 61 6e 64 20 2a 2f 0a 20 20 75 38 20 63 68 61  mand */.  u8 cha
6c3d0 6e 67 65 43 6e 74 4f 6e 3b 20 20 20 20 20 20 20  ngeCntOn;       
6c3e0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 70 64    /* True to upd
6c3f0 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63  ate the change-c
6c400 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 61  ounter */.  u8 a
6c410 62 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20 20  borted;         
6c420 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 52      /* True if R
6c430 4f 4c 4c 42 41 43 4b 20 69 6e 20 61 6e 6f 74 68  OLLBACK in anoth
6c440 65 72 20 56 4d 20 63 61 75 73 65 73 20 61 6e 20  er VM causes an 
6c450 61 62 6f 72 74 20 2a 2f 0a 20 20 75 38 20 65 78  abort */.  u8 ex
6c460 70 69 72 65 64 3b 20 20 20 20 20 20 20 20 20 20  pired;          
6c470 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
6c480 65 20 56 4d 20 6e 65 65 64 73 20 74 6f 20 62 65  e VM needs to be
6c490 20 72 65 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20   recompiled */. 
6c4a0 20 75 38 20 6d 69 6e 57 72 69 74 65 46 69 6c 65   u8 minWriteFile
6c4b0 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69  Format;  /* Mini
6c4c0 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  mum file format 
6c4d0 66 6f 72 20 77 72 69 74 61 62 6c 65 20 64 61 74  for writable dat
6c4e0 61 62 61 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20  abase files */. 
6c4f0 20 75 38 20 69 6e 56 74 61 62 4d 65 74 68 6f 64   u8 inVtabMethod
6c500 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20  ;        /* See 
6c510 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a  comments above *
6c520 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b  /.  int nChange;
6c530 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
6c540 75 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e  umber of db chan
6c550 67 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c  ges made since l
6c560 61 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69  ast reset */.  i
6c570 36 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20  64 startTime;   
6c580 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77         /* Time w
6c590 68 65 6e 20 71 75 65 72 79 20 73 74 61 72 74 65  hen query starte
6c5a0 64 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f  d - used for pro
6c5b0 66 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  filing */.  int 
6c5c0 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 20 20  btreeMask;      
6c5d0 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f      /* Bitmask o
6c5e0 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72  f db->aDb[] entr
6c5f0 69 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a  ies referenced *
6c600 2f 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72  /.  BtreeMutexAr
6c610 72 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41  ray aMutex; /* A
6c620 6e 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65  n array of Btree
6c630 20 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e   used here and n
6c640 65 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a  eeding locks */.
6c650 20 20 69 6e 74 20 6e 53 71 6c 3b 20 20 20 20 20    int nSql;     
6c660 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6c670 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 53  r of bytes in zS
6c680 71 6c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  ql */.  char *zS
6c690 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ql;           /*
6c6a0 20 54 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c   Text of the SQL
6c6b0 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
6c6c0 67 65 6e 65 72 61 74 65 64 20 74 68 69 73 20 2a  generated this *
6c6d0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
6c6e0 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 72  DEBUG.  FILE *tr
6c6f0 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 57  ace;        /* W
6c700 72 69 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f  rite an executio
6c710 6e 20 74 72 61 63 65 20 68 65 72 65 2c 20 69 66  n trace here, if
6c720 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e   not NULL */.#en
6c730 64 69 66 0a 20 20 69 6e 74 20 6f 70 65 6e 65 64  dif.  int opened
6c740 53 74 61 74 65 6d 65 6e 74 3b 20 20 2f 2a 20 54  Statement;  /* T
6c750 72 75 65 20 69 66 20 74 68 69 73 20 56 4d 20 68  rue if this VM h
6c760 61 73 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74  as opened a stat
6c770 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f  ement journal */
6c780 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
6c790 53 45 0a 20 20 69 6e 74 20 66 65 74 63 68 49 64  SE.  int fetchId
6c7a0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74  ;          /* St
6c7b0 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20 75  atement number u
6c7c0 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66  sed by sqlite3_f
6c7d0 65 74 63 68 5f 73 74 61 74 65 6d 65 6e 74 20 2a  etch_statement *
6c7e0 2f 0a 20 20 69 6e 74 20 6c 72 75 3b 20 20 20 20  /.  int lru;    
6c7f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75            /* Cou
6c800 6e 74 65 72 20 75 73 65 64 20 66 6f 72 20 4c 52  nter used for LR
6c810 55 20 63 61 63 68 65 20 72 65 70 6c 61 63 65 6d  U cache replacem
6c820 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  ent */.#endif.};
6c830 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
6c840 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77 65  owing are allowe
6c850 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56 64 62  d values for Vdb
6c860 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66 69  e.magic.*/.#defi
6c870 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  ne VDBE_MAGIC_IN
6c880 49 54 20 20 20 20 20 30 78 32 36 62 63 65 61 61  IT     0x26bceaa
6c890 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e 67  5    /* Building
6c8a0 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20   a VDBE program 
6c8b0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
6c8c0 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20 20 30  MAGIC_RUN      0
6c8d0 78 62 64 66 32 30 64 61 33 20 20 20 20 2f 2a 20  xbdf20da3    /* 
6c8e0 56 44 42 45 20 69 73 20 72 65 61 64 79 20 74 6f  VDBE is ready to
6c8f0 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64 65 66   execute */.#def
6c900 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 48  ine VDBE_MAGIC_H
6c910 41 4c 54 20 20 20 20 20 30 78 35 31 39 63 32 39  ALT     0x519c29
6c920 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20 68 61  73    /* VDBE ha
6c930 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63  s completed exec
6c940 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ution */.#define
6c950 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 44   VDBE_MAGIC_DEAD
6c960 20 20 20 20 20 30 78 62 36 30 36 63 33 63 38 20       0xb606c3c8 
6c970 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 68     /* The VDBE h
6c980 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61  as been dealloca
6c990 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 75  ted */../*.** Fu
6c9a0 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65  nction prototype
6c9b0 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  s.*/.SQLITE_PRIV
6c9c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6c9d0 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 56  VdbeFreeCursor(V
6c9e0 64 62 65 20 2a 2c 20 43 75 72 73 6f 72 2a 29 3b  dbe *, Cursor*);
6c9f0 0a 76 6f 69 64 20 73 71 6c 69 74 65 56 64 62 65  .void sqliteVdbe
6ca00 50 6f 70 53 74 61 63 6b 28 56 64 62 65 2a 2c 69  PopStack(Vdbe*,i
6ca10 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6ca20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
6ca30 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
6ca40 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65  Cursor*);.#if de
6ca50 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
6ca60 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56  UG) || defined(V
6ca70 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c  DBE_PROFILE).SQL
6ca80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6ca90 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
6caa0 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20  tOp(FILE*, int, 
6cab0 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c  Op*);.#endif.SQL
6cac0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6cad0 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
6cae0 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53  lTypeLen(u32);.S
6caf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
6cb00 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  2 sqlite3VdbeSer
6cb10 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e  ialType(Mem*, in
6cb20 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6cb30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
6cb40 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69  beSerialPut(unsi
6cb50 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c  gned char*, int,
6cb60 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c   Mem*, int);.SQL
6cb70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6cb80 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
6cb90 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67  lGet(const unsig
6cba0 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20  ned char*, u32, 
6cbb0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
6cbc0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6cbd0 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44  e3VdbeDeleteAuxD
6cbe0 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69  ata(VdbeFunc*, i
6cbf0 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65  nt);..int sqlite
6cc00 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65  2BtreeKeyCompare
6cc10 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e  (BtCursor *, con
6cc20 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20  st void *, int, 
6cc30 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c  int, int *);.SQL
6cc40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6cc50 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65  sqlite3VdbeIdxKe
6cc60 79 43 6f 6d 70 61 72 65 28 43 75 72 73 6f 72 2a  yCompare(Cursor*
6cc70 2c 69 6e 74 2c 63 6f 6e 73 74 20 75 6e 73 69 67  ,int,const unsig
6cc80 6e 65 64 20 63 68 61 72 2a 2c 69 6e 74 2a 29 3b  ned char*,int*);
6cc90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6cca0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
6ccb0 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72  dxRowid(BtCursor
6ccc0 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49   *, i64 *);.SQLI
6ccd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6cce0 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
6ccf0 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e  (const Mem*, con
6cd00 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43  st Mem*, const C
6cd10 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45  ollSeq*);.SQLITE
6cd20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6cd30 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f  ite3VdbeRecordCo
6cd40 6d 70 61 72 65 28 76 6f 69 64 2a 2c 69 6e 74 2c  mpare(void*,int,
6cd50 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
6cd60 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 53   const void*);.S
6cd70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6cd80 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  t sqlite3VdbeIdx
6cd90 52 6f 77 69 64 4c 65 6e 28 63 6f 6e 73 74 20 75  RowidLen(const u
6cda0 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  8*);.SQLITE_PRIV
6cdb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
6cdc0 64 62 65 45 78 65 63 28 56 64 62 65 2a 29 3b 0a  dbeExec(Vdbe*);.
6cdd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6cde0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69  nt sqlite3VdbeLi
6cdf0 73 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  st(Vdbe*);.SQLIT
6ce00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6ce10 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64  lite3VdbeHalt(Vd
6ce20 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
6ce30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6ce40 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
6ce50 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a  ng(Mem *, int);.
6ce60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6ce70 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
6ce80 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b 0a 53  mTooBig(Mem*);.S
6ce90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6cea0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
6ceb0 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74  Copy(Mem*, const
6cec0 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   Mem*);.SQLITE_P
6ced0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6cee0 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f  te3VdbeMemShallo
6cef0 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73  wCopy(Mem*, cons
6cf00 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51  t Mem*, int);.SQ
6cf10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6cf20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
6cf30 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b  ove(Mem*, Mem*);
6cf40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6cf50 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
6cf60 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d  emNulTerminate(M
6cf70 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
6cf80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6cf90 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65  VdbeMemSetStr(Me
6cfa0 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  m*, const char*,
6cfb0 20 69 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a   int, u8, void(*
6cfc0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
6cfd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6cfe0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
6cff0 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29  Int64(Mem*, i64)
6d000 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6d010 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
6d020 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65  eMemSetDouble(Me
6d030 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c  m*, double);.SQL
6d040 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6d050 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
6d060 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51  etNull(Mem*);.SQ
6d070 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6d080 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
6d090 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a  SetZeroBlob(Mem*
6d0a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
6d0b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6d0c0 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
6d0d0 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c  eable(Mem*);.SQL
6d0e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6d0f0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 44 79  sqlite3VdbeMemDy
6d100 6e 61 6d 69 63 69 66 79 28 4d 65 6d 2a 29 3b 0a  namicify(Mem*);.
6d110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6d120 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
6d130 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c  mStringify(Mem*,
6d140 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6d150 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65  IVATE i64 sqlite
6d160 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65  3VdbeIntValue(Me
6d170 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
6d180 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
6d190 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
6d1a0 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
6d1b0 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71  RIVATE double sq
6d1c0 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
6d1d0 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ue(Mem*);.SQLITE
6d1e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6d1f0 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
6d200 41 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a  Affinity(Mem*);.
6d210 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6d220 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
6d230 6d 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a  mRealify(Mem*);.
6d240 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6d250 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
6d260 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b  mNumerify(Mem*);
6d270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d280 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
6d290 65 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75  emFromBtree(BtCu
6d2a0 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e  rsor*,int,int,in
6d2b0 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  t,Mem*);.SQLITE_
6d2c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6d2d0 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
6d2e0 73 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49  se(Mem *p);.SQLI
6d2f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6d300 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e  qlite3VdbeMemFin
6d310 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e 63  alize(Mem*, Func
6d320 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Def*);.SQLITE_PR
6d330 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
6d340 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e   *sqlite3OpcodeN
6d350 61 6d 65 28 69 6e 74 29 3b 0a 0a 23 69 66 6e 64  ame(int);..#ifnd
6d360 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45  ef NDEBUG.SQLITE
6d370 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
6d380 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 61  sqlite3VdbeMemSa
6d390 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  nity(Mem*);.SQLI
6d3a0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
6d3b0 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f   sqlite3VdbeOpco
6d3c0 64 65 4e 6f 50 75 73 68 28 75 38 29 3b 0a 23 65  deNoPush(u8);.#e
6d3d0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
6d3e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
6d3f0 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28  dbeMemTranslate(
6d400 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65  Mem*, u8);.#ifde
6d410 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53  f SQLITE_DEBUG.S
6d420 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6d430 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
6d440 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b  PrintSql(Vdbe*);
6d450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d460 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
6d470 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
6d480 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72  (Mem *pMem, char
6d490 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a   *zBuf);.#endif.
6d4a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6d4b0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
6d4c0 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a  mHandleBom(Mem *
6d4d0 70 4d 65 6d 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pMem);.SQLITE_PR
6d4e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6d4f0 65 33 56 64 62 65 46 69 66 6f 49 6e 69 74 28 46  e3VdbeFifoInit(F
6d500 69 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ifo*);.SQLITE_PR
6d510 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6d520 33 56 64 62 65 46 69 66 6f 50 75 73 68 28 46 69  3VdbeFifoPush(Fi
6d530 66 6f 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54  fo*, i64);.SQLIT
6d540 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d550 6c 69 74 65 33 56 64 62 65 46 69 66 6f 50 6f 70  lite3VdbeFifoPop
6d560 28 46 69 66 6f 2a 2c 20 69 36 34 2a 29 3b 0a 53  (Fifo*, i64*);.S
6d570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6d580 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  id sqlite3VdbeFi
6d590 66 6f 43 6c 65 61 72 28 46 69 66 6f 2a 29 3b 0a  foClear(Fifo*);.
6d5a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
6d5b0 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51  OMIT_INCRBLOB.SQ
6d5c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
6d5d0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
6d5e0 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20  mExpandBlob(Mem 
6d5f0 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  *);.#else.  #def
6d600 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ine sqlite3VdbeM
6d610 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 78 29 20  emExpandBlob(x) 
6d620 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66  SQLITE_OK.#endif
6d630 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
6d640 69 6e 65 64 28 5f 56 44 42 45 49 4e 54 5f 48 5f  ined(_VDBEINT_H_
6d650 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
6d660 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
6d670 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
6d680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6d690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6d6a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
6d6b0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
6d6c0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
6d6d0 66 66 20 69 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a  ff in utf.c ****
6d6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6d6f0 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ****/../*.** The
6d700 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74   following const
6d710 61 6e 74 20 76 61 6c 75 65 20 69 73 20 75 73 65  ant value is use
6d720 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f  d by the SQLITE_
6d730 42 49 47 45 4e 44 49 41 4e 20 61 6e 64 0a 2a 2a  BIGENDIAN and.**
6d740 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e   SQLITE_LITTLEEN
6d750 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  DIAN macros..*/.
6d760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
6d770 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33  onst int sqlite3
6d780 6f 6e 65 20 3d 20 31 3b 0a 0a 2f 2a 0a 2a 2a 20  one = 1;../*.** 
6d790 54 68 69 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c  This lookup tabl
6d7a0 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 65 6c  e is used to hel
6d7b0 70 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72  p decode the fir
6d7c0 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 61 20  st byte of.** a 
6d7d0 6d 75 6c 74 69 2d 62 79 74 65 20 55 54 46 38 20  multi-byte UTF8 
6d7e0 63 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 73 74  character..*/.st
6d7f0 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
6d800 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33  ned char sqlite3
6d810 55 74 66 54 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a  UtfTrans1[] = {.
6d820 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78    0x00, 0x01, 0x
6d830 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20  02, 0x03, 0x04, 
6d840 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37  0x05, 0x06, 0x07
6d850 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20  ,.  0x08, 0x09, 
6d860 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63  0x0a, 0x0b, 0x0c
6d870 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78  , 0x0d, 0x0e, 0x
6d880 30 66 2c 0a 20 20 30 78 31 30 2c 20 30 78 31 31  0f,.  0x10, 0x11
6d890 2c 20 30 78 31 32 2c 20 30 78 31 33 2c 20 30 78  , 0x12, 0x13, 0x
6d8a0 31 34 2c 20 30 78 31 35 2c 20 30 78 31 36 2c 20  14, 0x15, 0x16, 
6d8b0 30 78 31 37 2c 0a 20 20 30 78 31 38 2c 20 30 78  0x17,.  0x18, 0x
6d8c0 31 39 2c 20 30 78 31 61 2c 20 30 78 31 62 2c 20  19, 0x1a, 0x1b, 
6d8d0 30 78 31 63 2c 20 30 78 31 64 2c 20 30 78 31 65  0x1c, 0x1d, 0x1e
6d8e0 2c 20 30 78 31 66 2c 0a 20 20 30 78 30 30 2c 20  , 0x1f,.  0x00, 
6d8f0 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
6d900 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78  , 0x04, 0x05, 0x
6d910 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38  06, 0x07,.  0x08
6d920 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78  , 0x09, 0x0a, 0x
6d930 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20  0b, 0x0c, 0x0d, 
6d940 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78  0x0e, 0x0f,.  0x
6d950 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  00, 0x01, 0x02, 
6d960 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35  0x03, 0x04, 0x05
6d970 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20  , 0x06, 0x07,.  
6d980 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32  0x00, 0x01, 0x02
6d990 2c 20 30 78 30 33 2c 20 30 78 30 30 2c 20 30 78  , 0x03, 0x00, 0x
6d9a0 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 0a  01, 0x00, 0x00,.
6d9b0 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65 20 57 52 49  };...#define WRI
6d9c0 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29  TE_UTF8(zOut, c)
6d9d0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
6d9e0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
6d9f0 69 66 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b  if( c<0x00080 ){
6da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
6da30 2b 2b 20 3d 20 28 63 26 30 78 46 46 29 3b 20 20  ++ = (c&0xFF);  
6da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
6da60 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
6da70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da90 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20         \.  else 
6daa0 69 66 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b  if( c<0x00800 ){
6dab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dad0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
6dae0 30 78 43 30 20 2b 20 28 28 63 3e 3e 36 29 26 30  0xC0 + ((c>>6)&0
6daf0 78 31 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x1F);           
6db00 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
6db10 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
6db20 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  (c & 0x3F);     
6db30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6db40 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
6db50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6db60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6db70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65             \.  e
6db80 6c 73 65 20 69 66 28 20 63 3c 30 78 31 30 30 30  lse if( c<0x1000
6db90 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
6dba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dbb0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
6dbc0 2b 20 3d 20 30 78 45 30 20 2b 20 28 28 63 3e 3e  + = 0xE0 + ((c>>
6dbd0 31 32 29 26 30 78 30 46 29 3b 20 20 20 20 20 20  12)&0x0F);      
6dbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
6dbf0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
6dc00 30 20 2b 20 28 28 63 3e 3e 36 29 20 26 20 30 78  0 + ((c>>6) & 0x
6dc10 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
6dc20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
6dc30 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 63 20  t++ = 0x80 + (c 
6dc40 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
6dc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6dc60 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20  .  }else{       
6dc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dc90 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
6dca0 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 28  Out++ = 0xF0 + (
6dcb0 28 63 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b  (c>>18) & 0x07);
6dcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dcd0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
6dce0 20 30 78 38 30 20 2b 20 28 28 63 3e 3e 31 32 29   0x80 + ((c>>12)
6dcf0 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
6dd00 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
6dd10 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b  *zOut++ = 0x80 +
6dd20 20 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29   ((c>>6) & 0x3F)
6dd30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6dd40 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
6dd50 20 3d 20 30 78 38 30 20 2b 20 28 63 20 26 20 30   = 0x80 + (c & 0
6dd60 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
6dd70 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
6dd80 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
6dd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ddb0 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e       \.}..#defin
6ddc0 65 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28  e WRITE_UTF16LE(
6ddd0 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20  zOut, c) {      
6dde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ddf0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
6de00 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20  ( c<=0xFFFF ){  
6de10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6de20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6de30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
6de40 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30    *zOut++ = (c&0
6de50 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
6de60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6de70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
6de80 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28      *zOut++ = ((
6de90 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20  c>>8)&0x00FF);  
6dea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6deb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dec0 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20  \.  }else{      
6ded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6def0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6df00 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
6df10 3d 20 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30  = (((c>>10)&0x00
6df20 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30  3F) + (((c-0x100
6df30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29  00)>>10)&0x00C0)
6df40 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b  );  \.    *zOut+
6df50 2b 20 3d 20 28 30 78 30 30 44 38 20 2b 20 28 28  + = (0x00D8 + ((
6df60 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29  (c-0x10000)>>18)
6df70 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20  &0x03));        
6df80 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
6df90 74 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29  t++ = (c&0x00FF)
6dfa0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6dfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dfc0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
6dfd0 4f 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 43 20  Out++ = (0x00DC 
6dfe0 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29  + ((c>>8)&0x03))
6dff0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6e000 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20            \.  } 
6e010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e040 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
6e050 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55  .#define WRITE_U
6e060 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20  TF16BE(zOut, c) 
6e070 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
6e080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e090 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46   \.  if( c<=0xFF
6e0a0 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  FF ){           
6e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e0d0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
6e0e0 20 3d 20 28 28 63 3e 3e 38 29 26 30 78 30 30 46   = ((c>>8)&0x00F
6e0f0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
6e100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e110 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
6e120 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29 3b  ++ = (c&0x00FF);
6e130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e150 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65         \.  }else
6e160 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
6e170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e190 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
6e1a0 7a 4f 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 38  zOut++ = (0x00D8
6e1b0 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29   + (((c-0x10000)
6e1c0 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20  >>18)&0x03));   
6e1d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
6e1e0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28 28 63 3e   *zOut++ = (((c>
6e1f0 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28  >10)&0x003F) + (
6e200 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30  ((c-0x10000)>>10
6e210 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20  )&0x00C0));  \. 
6e220 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30 78     *zOut++ = (0x
6e230 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30  00DC + ((c>>8)&0
6e240 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20  x03));          
6e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6e260 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
6e270 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  c&0x00FF);      
6e280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e2a0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
6e2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e2e0 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20     \.}..#define 
6e2f0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e  READ_UTF16LE(zIn
6e300 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20  , c){           
6e310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
6e330 20 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20    c = (*zIn++); 
6e340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e370 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28        \.  c += (
6e380 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20  (*zIn++)<<8);   
6e390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
6e3c0 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20    if( c>=0xD800 
6e3d0 26 26 20 63 3c 30 78 45 30 30 30 20 29 7b 20 20  && c<0xE000 ){  
6e3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e400 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63       \.    int c
6e410 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20  2 = (*zIn++);   
6e420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e440 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
6e450 20 20 20 63 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b     c2 += ((*zIn+
6e460 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20  +)<<8);         
6e470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e490 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28       \.    c = (
6e4a0 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63  c2&0x03FF) + ((c
6e4b0 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20  &0x003F)<<10) + 
6e4c0 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30  (((c&0x03C0)+0x0
6e4d0 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20  040)<<10);   \. 
6e4e0 20 20 20 69 66 28 20 28 63 20 26 20 30 78 46 46     if( (c & 0xFF
6e4f0 46 46 30 30 30 30 29 3d 3d 30 20 29 20 63 20 3d  FF0000)==0 ) c =
6e500 20 30 78 46 46 46 44 3b 20 20 20 20 20 20 20 20   0xFFFD;        
6e510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e520 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
6e530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e560 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
6e570 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55  ..#define READ_U
6e580 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20  TF16BE(zIn, c){ 
6e590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e5b0 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20          \.  c = 
6e5c0 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20  ((*zIn++)<<8);  
6e5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e600 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b  \.  c += (*zIn++
6e610 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
6e620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e640 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
6e650 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30  c>=0xD800 && c<0
6e660 78 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20  xE000 ){        
6e670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6e690 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28  .    int c2 = ((
6e6a0 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20  *zIn++)<<8);    
6e6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e6d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20         \.    c2 
6e6e0 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20  += (*zIn++);    
6e6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6e720 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30  .    c = (c2&0x0
6e730 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33  3FF) + ((c&0x003
6e740 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30  F)<<10) + (((c&0
6e750 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c  x03C0)+0x0040)<<
6e760 31 30 29 3b 20 20 20 5c 0a 20 20 20 20 69 66 28  10);   \.    if(
6e770 20 28 63 20 26 20 30 78 46 46 46 46 30 30 30 30   (c & 0xFFFF0000
6e780 29 3d 3d 30 20 29 20 63 20 3d 20 30 78 46 46 46  )==0 ) c = 0xFFF
6e790 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D;              
6e7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6e7b0 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  .  }            
6e7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e7f0 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a         \.}../*.*
6e800 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69  * Translate a si
6e810 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61  ngle UTF-8 chara
6e820 63 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68  cter.  Return th
6e830 65 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 2e  e unicode value.
6e840 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 72  .**.** During tr
6e850 61 6e 73 6c 61 74 69 6f 6e 2c 20 61 73 73 75 6d  anslation, assum
6e860 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65 20  e that the byte 
6e870 74 68 61 74 20 7a 54 65 72 6d 20 70 6f 69 6e 74  that zTerm point
6e880 73 0a 2a 2a 20 69 73 20 61 20 30 78 30 30 2e 0a  s.** is a 0x00..
6e890 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 70 6f  **.** Write a po
6e8a0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78  inter to the nex
6e8b0 74 20 75 6e 72 65 61 64 20 62 79 74 65 20 62 61  t unread byte ba
6e8c0 63 6b 20 69 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e  ck into *pzNext.
6e8d0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20  .**.** Notes On 
6e8e0 49 6e 76 61 6c 69 64 20 55 54 46 2d 38 3a 0a 2a  Invalid UTF-8:.*
6e8f0 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f  *.**  *  This ro
6e900 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f  utine never allo
6e910 77 73 20 61 20 37 2d 62 69 74 20 63 68 61 72 61  ws a 7-bit chara
6e920 63 74 65 72 20 28 30 78 30 30 20 74 68 72 6f 75  cter (0x00 throu
6e930 67 68 20 30 78 37 66 29 20 74 6f 0a 2a 2a 20 20  gh 0x7f) to.**  
6e940 20 20 20 62 65 20 65 6e 63 6f 64 65 64 20 61 73     be encoded as
6e950 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68   a multi-byte ch
6e960 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 6d 75  aracter.  Any mu
6e970 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74  lti-byte charact
6e980 65 72 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 61  er that.**     a
6e990 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64  ttempts to encod
6e9a0 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65  e a value betwee
6e9b0 6e 20 30 78 30 30 20 61 6e 64 20 30 78 37 66 20  n 0x00 and 0x7f 
6e9c0 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30  is rendered as 0
6e9d0 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20  xfffd..**.**  * 
6e9e0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65   This routine ne
6e9f0 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 55 54 46  ver allows a UTF
6ea00 31 36 20 73 75 72 72 6f 67 61 74 65 20 76 61 6c  16 surrogate val
6ea10 75 65 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 64  ue to be encoded
6ea20 2e 0a 2a 2a 20 20 20 20 20 49 66 20 61 20 6d 75  ..**     If a mu
6ea30 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74  lti-byte charact
6ea40 65 72 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65  er attempts to e
6ea50 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65  ncode a value be
6ea60 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 30 78 64  tween.**     0xd
6ea70 38 30 30 20 61 6e 64 20 30 78 65 30 30 30 20 74  800 and 0xe000 t
6ea80 68 65 6e 20 69 74 20 69 73 20 72 65 6e 64 65 72  hen it is render
6ea90 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a  ed as 0xfffd..**
6eaa0 0a 2a 2a 20 20 2a 20 20 42 79 74 65 73 20 69 6e  .**  *  Bytes in
6eab0 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 30 78   the range of 0x
6eac0 38 30 20 74 68 72 6f 75 67 68 20 30 78 62 66 20  80 through 0xbf 
6ead0 77 68 69 63 68 20 6f 63 63 75 72 20 61 73 20 74  which occur as t
6eae0 68 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20  he first.**     
6eaf0 62 79 74 65 20 6f 66 20 61 20 63 68 61 72 61 63  byte of a charac
6eb00 74 65 72 20 61 72 65 20 69 6e 74 65 72 70 72 65  ter are interpre
6eb10 74 65 64 20 61 73 20 73 69 6e 67 6c 65 2d 62 79  ted as single-by
6eb20 74 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a  te characters.**
6eb30 20 20 20 20 20 61 6e 64 20 72 65 6e 64 65 72 65       and rendere
6eb40 64 20 61 73 20 74 68 65 6d 73 65 6c 76 65 73 20  d as themselves 
6eb50 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79  even though they
6eb60 20 61 72 65 20 74 65 63 68 6e 69 63 61 6c 6c 79   are technically
6eb70 0a 2a 2a 20 20 20 20 20 69 6e 76 61 6c 69 64 20  .**     invalid 
6eb80 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a  characters..**.*
6eb90 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69  *  *  This routi
6eba0 6e 65 20 61 63 63 65 70 74 73 20 61 6e 20 69 6e  ne accepts an in
6ebb0 66 69 6e 69 74 65 20 6e 75 6d 62 65 72 20 6f 66  finite number of
6ebc0 20 64 69 66 66 65 72 65 6e 74 20 55 54 46 38 20   different UTF8 
6ebd0 65 6e 63 6f 64 69 6e 67 73 0a 2a 2a 20 20 20 20  encodings.**    
6ebe0 20 66 6f 72 20 75 6e 69 63 6f 64 65 20 76 61 6c   for unicode val
6ebf0 75 65 73 20 30 78 38 30 20 61 6e 64 20 67 72 65  ues 0x80 and gre
6ec00 61 74 65 72 2e 20 20 49 74 20 64 6f 20 6e 6f 74  ater.  It do not
6ec10 20 63 68 61 6e 67 65 20 6f 76 65 72 2d 6c 65 6e   change over-len
6ec20 67 74 68 0a 2a 2a 20 20 20 20 20 65 6e 63 6f 64  gth.**     encod
6ec30 69 6e 67 73 20 74 6f 20 30 78 66 66 66 64 20 61  ings to 0xfffd a
6ec40 73 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 72  s some systems r
6ec50 65 63 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 53 51 4c  ecommend..*/.SQL
6ec60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6ec70 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
6ec80 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
6ec90 64 20 63 68 61 72 20 2a 7a 2c 20 20 20 20 20 20  d char *z,      
6eca0 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
6ecb0 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63   of UTF-8 charac
6ecc0 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75  ter */.  const u
6ecd0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54  nsigned char *zT
6ece0 65 72 6d 2c 20 20 20 20 20 2f 2a 20 50 72 65 74  erm,     /* Pret
6ecf0 65 6e 64 20 74 68 69 73 20 62 79 74 65 20 69 73  end this byte is
6ed00 20 30 78 30 30 20 2a 2f 0a 20 20 63 6f 6e 73 74   0x00 */.  const
6ed10 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
6ed20 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72  *pzNext    /* Wr
6ed30 69 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70  ite first byte p
6ed40 61 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68  ast UTF-8 char h
6ed50 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
6ed60 63 20 3d 20 2a 28 7a 2b 2b 29 3b 0a 20 20 69 66  c = *(z++);.  if
6ed70 28 20 63 3e 3d 30 78 63 30 20 29 7b 0a 20 20 20  ( c>=0xc0 ){.   
6ed80 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 54   c = sqlite3UtfT
6ed90 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 0a 20  rans1[c-0xc0];. 
6eda0 20 20 20 77 68 69 6c 65 28 20 7a 21 3d 7a 54 65     while( z!=zTe
6edb0 72 6d 20 26 26 20 28 2a 7a 20 26 20 30 78 63 30  rm && (*z & 0xc0
6edc0 29 3d 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20  )==0x80 ){.     
6edd0 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30   c = (c<<6) + (0
6ede0 78 33 66 20 26 20 2a 28 7a 2b 2b 29 29 3b 0a 20  x3f & *(z++));. 
6edf0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 3c 30     }.    if( c<0
6ee00 78 38 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28  x80.        || (
6ee10 63 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d 30  c&0xFFFFF800)==0
6ee20 78 44 38 30 30 0a 20 20 20 20 20 20 20 20 7c 7c  xD800.        ||
6ee30 20 28 63 26 30 78 46 46 46 46 46 46 46 45 29 3d   (c&0xFFFFFFFE)=
6ee40 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20 3d 20  =0xFFFE ){  c = 
6ee50 30 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20  0xFFFD; }.  }.  
6ee60 2a 70 7a 4e 65 78 74 20 3d 20 7a 3b 0a 20 20 72  *pzNext = z;.  r
6ee70 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 2f 2a  eturn c;.}..../*
6ee80 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53  .** If the TRANS
6ee90 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f  LATE_TRACE macro
6eea0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
6eeb0 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d   value of each M
6eec0 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64  em is.** printed
6eed0 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68   on stderr on th
6eee0 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f  e way into and o
6eef0 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62  ut of sqlite3Vdb
6ef00 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e  eMemTranslate().
6ef10 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20  .*/ ./* #define 
6ef20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20  TRANSLATE_TRACE 
6ef30 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  1 */..#ifndef SQ
6ef40 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
6ef50 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
6ef60 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68  ne transforms th
6ef70 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20  e internal text 
6ef80 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79  encoding used by
6ef90 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69   pMem to.** desi
6efa0 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e  redEnc. It is an
6efb0 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74   error if the st
6efc0 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20  ring is already 
6efd0 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a  of the desired.*
6efe0 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69  * encoding, or i
6eff0 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74  f *pMem does not
6f000 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e   contain a strin
6f010 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49  g value..*/.SQLI
6f020 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6f030 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61  qlite3VdbeMemTra
6f040 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d  nslate(Mem *pMem
6f050 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29  , u8 desiredEnc)
6f060 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
6f070 72 20 7a 53 68 6f 72 74 5b 4e 42 46 53 5d 3b 20  r zShort[NBFS]; 
6f080 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 68 6f  /* Temporary sho
6f090 72 74 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  rt output buffer
6f0a0 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20   */.  int len;  
6f0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6f0c0 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e    /* Maximum len
6f0d0 67 74 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74  gth of output st
6f0e0 72 69 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f  ring in bytes */
6f0f0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
6f100 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20   *zOut;         
6f110 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
6f120 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75  ut buffer */.  u
6f130 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
6f140 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
6f150 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74       /* Input it
6f160 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69  erator */.  unsi
6f170 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d  gned char *zTerm
6f180 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6f190 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75    /* End of inpu
6f1a0 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  t */.  unsigned 
6f1b0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
6f1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6f1d0 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f 72 20  Output iterator 
6f1e0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
6f1f0 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  t c;..  assert( 
6f200 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
6f210 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
6f220 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
6f230 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
6f240 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
6f250 53 74 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Str );.  assert(
6f260 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69   pMem->enc!=desi
6f270 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65  redEnc );.  asse
6f280 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30  rt( pMem->enc!=0
6f290 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
6f2a0 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66  em->n>=0 );..#if
6f2b0 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41   defined(TRANSLA
6f2c0 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66  TE_TRACE) && def
6f2d0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
6f2e0 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20  G).  {.    char 
6f2f0 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73  zBuf[100];.    s
6f300 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65  qlite3VdbeMemPre
6f310 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a  ttyPrint(pMem, z
6f320 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  Buf);.    fprint
6f330 66 28 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54  f(stderr, "INPUT
6f340 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b  :  %s\n", zBuf);
6f350 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
6f360 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61  * If the transla
6f370 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20  tion is between 
6f380 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e  UTF-16 little an
6f390 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68  d big endian, th
6f3a0 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61  en .  ** all tha
6f3b0 74 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73  t is required is
6f3c0 20 74 6f 20 73 77 61 70 20 74 68 65 20 62 79 74   to swap the byt
6f3d0 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61  e order. This ca
6f3e0 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20  se is handled.  
6f3f0 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66  ** differently f
6f400 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a  rom the others..
6f410 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d    */.  if( pMem-
6f420 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46  >enc!=SQLITE_UTF
6f430 38 20 26 26 20 64 65 73 69 72 65 64 45 6e 63 21  8 && desiredEnc!
6f440 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a  =SQLITE_UTF8 ){.
6f450 20 20 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20      u8 temp;.   
6f460 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20   int rc;.    rc 
6f470 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
6f480 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d  MakeWriteable(pM
6f490 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  em);.    if( rc!
6f4a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
6f4b0 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
6f4c0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
6f4d0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
6f4e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
6f4f0 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29  .    zIn = (u8*)
6f500 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65  pMem->z;.    zTe
6f510 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e  rm = &zIn[pMem->
6f520 6e 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a  n];.    while( z
6f530 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20  In<zTerm ){.    
6f540 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20    temp = *zIn;. 
6f550 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49       *zIn = *(zI
6f560 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b  n+1);.      zIn+
6f570 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20  +;.      *zIn++ 
6f580 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20  = temp;.    }.  
6f590 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65    pMem->enc = de
6f5a0 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f  siredEnc;.    go
6f5b0 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74  to translate_out
6f5c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
6f5d0 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d  len to the maxim
6f5e0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  um number of byt
6f5f0 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74  es required in t
6f600 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  he output buffer
6f610 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72  . */.  if( desir
6f620 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
6f630 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65  F8 ){.    /* Whe
6f640 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f  n converting fro
6f650 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61  m UTF-16, the ma
6f660 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73  ximum growth res
6f670 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a  ults from.    **
6f680 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32   translating a 2
6f690 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20  -byte character 
6f6a0 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d  to a 4-byte UTF-
6f6b0 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20  8 character..   
6f6c0 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74   ** A single byt
6f6d0 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  e is required fo
6f6e0 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72  r the output str
6f6f0 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74  ing.    ** nul-t
6f700 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a  erminator..    *
6f710 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d  /.    len = pMem
6f720 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a 20 20 7d  ->n * 2 + 1;.  }
6f730 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 68 65  else{.    /* Whe
6f740 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f  n converting fro
6f750 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31  m UTF-8 to UTF-1
6f760 36 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72  6 the maximum gr
6f770 6f 77 74 68 20 69 73 20 63 61 75 73 65 64 0a 20  owth is caused. 
6f780 20 20 20 2a 2a 20 77 68 65 6e 20 61 20 31 2d 62     ** when a 1-b
6f790 79 74 65 20 55 54 46 2d 38 20 63 68 61 72 61 63  yte UTF-8 charac
6f7a0 74 65 72 20 69 73 20 74 72 61 6e 73 6c 61 74 65  ter is translate
6f7b0 64 20 69 6e 74 6f 20 61 20 32 2d 62 79 74 65 20  d into a 2-byte 
6f7c0 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a 20 63 68  UTF-16.    ** ch
6f7d0 61 72 61 63 74 65 72 2e 20 54 77 6f 20 62 79 74  aracter. Two byt
6f7e0 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64 20  es are required 
6f7f0 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  in the output bu
6f800 66 66 65 72 20 66 6f 72 20 74 68 65 0a 20 20 20  ffer for the.   
6f810 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74   ** nul-terminat
6f820 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c  or..    */.    l
6f830 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32  en = pMem->n * 2
6f840 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   + 2;.  }..  /* 
6f850 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69 6e 74  Set zIn to point
6f860 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
6f870 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65   the input buffe
6f880 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f 20 70  r and zTerm to p
6f890 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62 79 74 65  oint 1.  ** byte
6f8a0 20 70 61 73 74 20 74 68 65 20 65 6e 64 2e 0a 20   past the end.. 
6f8b0 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c   **.  ** Variabl
6f8c0 65 20 7a 4f 75 74 20 69 73 20 73 65 74 20 74 6f  e zOut is set to
6f8d0 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6f 75   point at the ou
6f8e0 74 70 75 74 20 62 75 66 66 65 72 2e 20 54 68 69  tput buffer. Thi
6f8f0 73 20 6d 61 79 20 62 65 20 73 70 61 63 65 0a 20  s may be space. 
6f900 20 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   ** obtained fro
6f910 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
6f920 28 29 2c 20 6f 72 20 4d 65 6d 2e 7a 53 68 6f 72  (), or Mem.zShor
6f930 74 2c 20 69 66 20 69 74 20 6c 61 72 67 65 20 65  t, if it large e
6f940 6e 6f 75 67 68 20 61 6e 64 0a 20 20 2a 2a 20 6e  nough and.  ** n
6f950 6f 74 20 69 6e 20 75 73 65 2c 20 6f 72 20 74 68  ot in use, or th
6f960 65 20 7a 53 68 6f 72 74 20 61 72 72 61 79 20 6f  e zShort array o
6f970 6e 20 74 68 65 20 73 74 61 63 6b 20 28 73 65 65  n the stack (see
6f980 20 61 62 6f 76 65 29 2e 0a 20 20 2a 2f 0a 20 20   above)..  */.  
6f990 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d  zIn = (u8*)pMem-
6f9a0 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26 7a  >z;.  zTerm = &z
6f9b0 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 69  In[pMem->n];.  i
6f9c0 66 28 20 6c 65 6e 3e 4e 42 46 53 20 29 7b 0a 20  f( len>NBFS ){. 
6f9d0 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65     zOut = sqlite
6f9e0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65  3DbMallocRaw(pMe
6f9f0 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 20  m->db, len);.   
6fa00 20 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20   if( !zOut ){.  
6fa10 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
6fa20 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
6fa30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4f 75 74   }else{.    zOut
6fa40 20 3d 20 7a 53 68 6f 72 74 3b 0a 20 20 7d 0a 20   = zShort;.  }. 
6fa50 20 7a 20 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66   z = zOut;..  if
6fa60 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c  ( pMem->enc==SQL
6fa70 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20  ITE_UTF8 ){.    
6fa80 69 66 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d  if( desiredEnc==
6fa90 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29  SQLITE_UTF16LE )
6faa0 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38  {.      /* UTF-8
6fab0 20 2d 3e 20 55 54 46 2d 31 36 20 4c 69 74 74 6c   -> UTF-16 Littl
6fac0 65 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20  e-endian */.    
6fad0 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65    while( zIn<zTe
6fae0 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20  rm ){.        c 
6faf0 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
6fb00 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63  d(zIn, zTerm, (c
6fb10 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b  onst u8**)&zIn);
6fb20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
6fb30 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20  TF16LE(z, c);.  
6fb40 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
6fb50 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
6fb60 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54  esiredEnc==SQLIT
6fb70 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20  E_UTF16BE );.   
6fb80 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55     /* UTF-8 -> U
6fb90 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e  TF-16 Big-endian
6fba0 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
6fbb0 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
6fbc0 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
6fbd0 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a  3Utf8Read(zIn, z
6fbe0 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a  Term, (const u8*
6fbf0 2a 29 26 7a 49 6e 29 3b 0a 20 20 20 20 20 20 20  *)&zIn);.       
6fc00 20 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a   WRITE_UTF16BE(z
6fc10 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , c);.      }.  
6fc20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20    }.    pMem->n 
6fc30 3d 20 7a 20 2d 20 7a 4f 75 74 3b 0a 20 20 20 20  = z - zOut;.    
6fc40 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  *z++ = 0;.  }els
6fc50 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  e{.    assert( d
6fc60 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54  esiredEnc==SQLIT
6fc70 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66  E_UTF8 );.    if
6fc80 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c  ( pMem->enc==SQL
6fc90 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20  ITE_UTF16LE ){. 
6fca0 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c       /* UTF-16 L
6fcb0 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20  ittle-endian -> 
6fcc0 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77  UTF-8 */.      w
6fcd0 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20  hile( zIn<zTerm 
6fce0 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f  ){.        READ_
6fcf0 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b  UTF16LE(zIn, c);
6fd00 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f   .        WRITE_
6fd10 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20  UTF8(z, c);.    
6fd20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
6fd30 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c       /* UTF-16 L
6fd40 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20  ittle-endian -> 
6fd50 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77  UTF-8 */.      w
6fd60 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20  hile( zIn<zTerm 
6fd70 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f  ){.        READ_
6fd80 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b  UTF16BE(zIn, c);
6fd90 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f   .        WRITE_
6fda0 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20  UTF8(z, c);.    
6fdb0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d    }.    }.    pM
6fdc0 65 6d 2d 3e 6e 20 3d 20 7a 20 2d 20 7a 4f 75 74  em->n = z - zOut
6fdd0 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a  ;.  }.  *z = 0;.
6fde0 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
6fdf0 3e 6e 2b 28 64 65 73 69 72 65 64 45 6e 63 3d 3d  >n+(desiredEnc==
6fe00 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29  SQLITE_UTF8?1:2)
6fe10 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 71 6c  )<=len );..  sql
6fe20 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
6fe30 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d  se(pMem);.  pMem
6fe40 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
6fe50 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c  _Static|MEM_Dyn|
6fe60 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 68  MEM_Ephem|MEM_Sh
6fe70 6f 72 74 29 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e  ort);.  pMem->en
6fe80 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b 0a  c = desiredEnc;.
6fe90 20 20 69 66 28 20 7a 4f 75 74 3d 3d 7a 53 68 6f    if( zOut==zSho
6fea0 72 74 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  rt ){.    memcpy
6feb0 28 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74 2c 20 7a  (pMem->zShort, z
6fec0 4f 75 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 7a  Out, len);.    z
6fed0 4f 75 74 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d  Out = (u8*)pMem-
6fee0 3e 7a 53 68 6f 72 74 3b 0a 20 20 20 20 70 4d 65  >zShort;.    pMe
6fef0 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d  m->flags |= (MEM
6ff00 5f 54 65 72 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29  _Term|MEM_Short)
6ff10 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
6ff20 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d  Mem->flags |= (M
6ff30 45 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29  EM_Term|MEM_Dyn)
6ff40 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 20  ;.  }.  pMem->z 
6ff50 3d 20 28 63 68 61 72 2a 29 7a 4f 75 74 3b 0a 0a  = (char*)zOut;..
6ff60 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a 0a 23  translate_out:.#
6ff70 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53  if defined(TRANS
6ff80 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 64  LATE_TRACE) && d
6ff90 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
6ffa0 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61  BUG).  {.    cha
6ffb0 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20  r zBuf[100];.   
6ffc0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50   sqlite3VdbeMemP
6ffd0 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c  rettyPrint(pMem,
6ffe0 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69   zBuf);.    fpri
6fff0 6e 74 66 28 73 74 64 65 72 72 2c 20 22 4f 55 54  ntf(stderr, "OUT
70000 50 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66  PUT: %s\n", zBuf
70010 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
70020 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
70030 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
70040 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 66  routine checks f
70050 6f 72 20 61 20 62 79 74 65 2d 6f 72 64 65 72 20  or a byte-order 
70060 6d 61 72 6b 20 61 74 20 74 68 65 20 62 65 67 69  mark at the begi
70070 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a 2a 2a  nning of the .**
70080 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 73   UTF-16 string s
70090 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 2e 20  tored in *pMem. 
700a0 49 66 20 6f 6e 65 20 69 73 20 70 72 65 73 65 6e  If one is presen
700b0 74 2c 20 69 74 20 69 73 20 72 65 6d 6f 76 65 64  t, it is removed
700c0 20 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e 63 6f   and.** the enco
700d0 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20  ding of the Mem 
700e0 61 64 6a 75 73 74 65 64 2e 20 54 68 69 73 20 72  adjusted. This r
700f0 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20  outine does not 
70100 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65 2d 73  do any.** byte-s
70110 77 61 70 70 69 6e 67 2c 20 69 74 20 6a 75 73 74  wapping, it just
70120 20 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20 61 70   sets Mem.enc ap
70130 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a  propriately..**.
70140 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f  ** The allocatio
70150 6e 20 28 73 74 61 74 69 63 2c 20 64 79 6e 61 6d  n (static, dynam
70160 69 63 20 65 74 63 2e 29 20 61 6e 64 20 65 6e 63  ic etc.) and enc
70170 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d  oding of the Mem
70180 20 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61 6e 67   may be.** chang
70190 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
701a0 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
701b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
701c0 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42  e3VdbeMemHandleB
701d0 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  om(Mem *pMem){. 
701e0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
701f0 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 3d 20  _OK;.  u8 bom = 
70200 30 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e  0;..  if( pMem->
70210 6e 3c 30 20 7c 7c 20 70 4d 65 6d 2d 3e 6e 3e 31  n<0 || pMem->n>1
70220 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20   ){.    u8 b1 = 
70230 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a  *(u8 *)pMem->z;.
70240 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28      u8 b2 = *(((
70250 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20  u8 *)pMem->z) + 
70260 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d  1);.    if( b1==
70270 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46  0xFE && b2==0xFF
70280 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20   ){.      bom = 
70290 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a  SQLITE_UTF16BE;.
702a0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31      }.    if( b1
702b0 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78  ==0xFF && b2==0x
702c0 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20  FE ){.      bom 
702d0 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  = SQLITE_UTF16LE
702e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20  ;.    }.  }.  . 
702f0 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20   if( bom ){.    
70300 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
70310 20 69 73 20 63 61 6c 6c 65 64 20 61 73 20 73 6f   is called as so
70320 6f 6e 20 61 73 20 61 20 73 74 72 69 6e 67 20 69  on as a string i
70330 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 4d 65  s stored in a Me
70340 6d 2a 2c 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20  m*,.    ** from 
70350 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 56 64  within sqlite3Vd
70360 62 65 4d 65 6d 53 65 74 53 74 72 28 29 2e 20 41  beMemSetStr(). A
70370 74 20 74 68 61 74 20 70 6f 69 6e 74 20 69 74 20  t that point it 
70380 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a  is not possible.
70390 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 73      ** for the s
703a0 74 72 69 6e 67 20 74 6f 20 62 65 20 73 74 6f 72  tring to be stor
703b0 65 64 20 69 6e 20 4d 65 6d 2e 7a 53 68 6f 72 74  ed in Mem.zShort
703c0 2c 20 6f 72 20 66 6f 72 20 69 74 20 74 6f 20 62  , or for it to b
703d0 65 20 73 74 6f 72 65 64 0a 20 20 20 20 2a 2a 20  e stored.    ** 
703e0 69 6e 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72  in dynamic memor
703f0 79 20 77 69 74 68 20 6e 6f 20 64 65 73 74 72 75  y with no destru
70400 63 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ctor..    */.   
70410 20 61 73 73 65 72 74 28 20 21 28 70 4d 65 6d 2d   assert( !(pMem-
70420 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 68 6f 72 74  >flags&MEM_Short
70430 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
70440 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d   !(pMem->flags&M
70450 45 4d 5f 44 79 6e 29 20 7c 7c 20 70 4d 65 6d 2d  EM_Dyn) || pMem-
70460 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20 69 66 28  >xDel );.    if(
70470 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
70480 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20  EM_Dyn ){.      
70490 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
704a0 64 2a 29 20 3d 20 70 4d 65 6d 2d 3e 78 44 65 6c  d*) = pMem->xDel
704b0 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 20  ;.      char *z 
704c0 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 20  = pMem->z;.     
704d0 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20   pMem->z = 0;.  
704e0 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d      pMem->xDel =
704f0 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   0;.      rc = s
70500 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
70510 53 74 72 28 70 4d 65 6d 2c 20 26 7a 5b 32 5d 2c  Str(pMem, &z[2],
70520 20 70 4d 65 6d 2d 3e 6e 2d 32 2c 20 62 6f 6d 2c   pMem->n-2, bom,
70530 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49   .          SQLI
70540 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
70550 20 20 20 20 20 78 44 65 6c 28 7a 29 3b 0a 20 20       xDel(z);.  
70560 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
70570 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
70580 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 26  emSetStr(pMem, &
70590 70 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d  pMem->z[2], pMem
705a0 2d 3e 6e 2d 32 2c 20 62 6f 6d 2c 20 0a 20 20 20  ->n-2, bom, .   
705b0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 54 52         SQLITE_TR
705c0 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d 0a  ANSIENT);.    }.
705d0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
705e0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
705f0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
70600 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61  /../*.** pZ is a
70610 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75   UTF-8 encoded u
70620 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49  nicode string. I
70630 66 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20  f nByte is less 
70640 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65  than zero,.** re
70650 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
70660 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61  of unicode chara
70670 63 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74  cters in pZ up t
70680 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75  o (but not inclu
70690 64 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72  ding).** the fir
706a0 73 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 66  st 0x00 byte. If
706b0 20 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65   nByte is not le
706c0 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65  ss than zero, re
706d0 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  turn the.** numb
706e0 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68  er of unicode ch
706f0 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20  aracters in the 
70700 66 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70  first nByte of p
70710 5a 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20  Z (or up to .** 
70720 74 68 65 20 66 69 72 73 74 20 30 78 30 30 2c 20  the first 0x00, 
70730 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20  whichever comes 
70740 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54  first)..*/.SQLIT
70750 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
70760 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e  lite3Utf8CharLen
70770 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e  (const char *zIn
70780 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
70790 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e  int r = 0;.  con
707a0 73 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73  st u8 *z = (cons
707b0 74 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e  t u8*)zIn;.  con
707c0 73 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20  st u8 *zTerm;.  
707d0 69 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a  if( nByte>=0 ){.
707e0 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e      zTerm = &z[n
707f0 42 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Byte];.  }else{.
70800 20 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e      zTerm = (con
70810 73 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d  st u8*)(-1);.  }
70820 0a 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54  .  assert( z<=zT
70830 65 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  erm );.  while( 
70840 2a 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d  *z!=0 && z<zTerm
70850 20 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53   ){.    SQLITE_S
70860 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20  KIP_UTF8(z);.   
70870 20 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75   r++;.  }.  retu
70880 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73  rn r;.}../* This
70890 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69   test function i
708a0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
708b0 75 73 65 64 20 62 79 20 74 68 65 20 61 75 74 6f  used by the auto
708c0 6d 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 65  mated test-suite
708d0 2e 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69  . .** Hence it i
708e0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
708f0 20 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73   in debug builds
70900 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
70910 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26  (SQLITE_TEST) &&
70920 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
70930 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61  DEBUG)./*.** Tra
70940 6e 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20  nslate UTF-8 to 
70950 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  UTF-8..**.** Thi
70960 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74  s has the effect
70970 20 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20   of making sure 
70980 74 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 20  that the string 
70990 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a  is well-formed.*
709a0 2a 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64  * UTF-8.  Miscod
709b0 65 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72  ed characters ar
709c0 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a  e removed..**.**
709d0 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e   The translation
709e0 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63   is done in-plac
709f0 65 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 69  e (since it is i
70a00 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68  mpossible for th
70a10 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46  e.** correct UTF
70a20 2d 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62  -8 encoding to b
70a30 65 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20  e longer than a 
70a40 6d 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69  malformed encodi
70a50 6e 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ng)..*/.SQLITE_P
70a60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
70a70 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e  e3Utf8To8(unsign
70a80 65 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20  ed char *zIn){. 
70a90 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
70aa0 7a 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e  zOut = zIn;.  un
70ab0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74  signed char *zSt
70ac0 61 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73  art = zIn;.  uns
70ad0 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72  igned char *zTer
70ae0 6d 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20 77  m;.  u32 c;..  w
70af0 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b 0a  hile( zIn[0] ){.
70b00 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
70b10 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 65  tf8Read(zIn, zTe
70b20 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29  rm, (const u8**)
70b30 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63  &zIn);.    if( c
70b40 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20  !=0xfffd ){.    
70b50 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75    WRITE_UTF8(zOu
70b60 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  t, c);.    }.  }
70b70 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20  .  *zOut = 0;.  
70b80 72 65 74 75 72 6e 20 7a 4f 75 74 20 2d 20 7a 53  return zOut - zS
70b90 74 61 72 74 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  tart;.}.#endif..
70ba0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
70bb0 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20  MIT_UTF16./*.** 
70bc0 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31 36  Convert a UTF-16
70bd0 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6e   string in the n
70be0 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 69  ative encoding i
70bf0 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 69  nto a UTF-8 stri
70c00 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f  ng..** Memory to
70c10 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38 20   hold the UTF-8 
70c20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e  string is obtain
70c30 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
70c40 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 0a  malloc and must.
70c50 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20 74  ** be freed by t
70c60 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
70c70 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c 20  ion..**.** NULL 
70c80 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
70c90 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f 63  here is an alloc
70ca0 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  ation error..*/.
70cb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
70cc0 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31  har *sqlite3Utf1
70cd0 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 62  6to8(sqlite3 *db
70ce0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c  , const void *z,
70cf0 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d   int nByte){.  M
70d00 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 26  em m;.  memset(&
70d10 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29  m, 0, sizeof(m))
70d20 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a 20  ;.  m.db = db;. 
70d30 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
70d40 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 79  etStr(&m, z, nBy
70d50 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  te, SQLITE_UTF16
70d60 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53  NATIVE, SQLITE_S
70d70 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65  TATIC);.  sqlite
70d80 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
70d90 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f 55  ing(&m, SQLITE_U
70da0 54 46 38 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TF8);.  assert( 
70db0 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54  (m.flags & MEM_T
70dc0 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d  erm)!=0 || db->m
70dd0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
70de0 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67   assert( (m.flag
70df0 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20  s & MEM_Str)!=0 
70e00 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
70e10 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  led );.  return 
70e20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44  (m.flags & MEM_D
70e30 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73  yn)!=0 ? m.z : s
70e40 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
70e50 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  b, m.z);.}../*.*
70e60 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 31 36  * pZ is a UTF-16
70e70 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65   encoded unicode
70e80 20 73 74 72 69 6e 67 2e 20 49 66 20 6e 43 68 61   string. If nCha
70e90 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a  r is less than z
70ea0 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74  ero,.** return t
70eb0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
70ec0 65 73 20 75 70 20 74 6f 20 28 62 75 74 20 6e 6f  es up to (but no
70ed0 74 20 69 6e 63 6c 75 64 69 6e 67 29 2c 20 74 68  t including), th
70ee0 65 20 66 69 72 73 74 20 70 61 69 72 0a 2a 2a 20  e first pair.** 
70ef0 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 30  of consecutive 0
70f00 78 30 30 20 62 79 74 65 73 20 69 6e 20 70 5a 2e  x00 bytes in pZ.
70f10 20 49 66 20 6e 43 68 61 72 20 69 73 20 6e 6f 74   If nChar is not
70f20 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
70f30 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20  .** then return 
70f40 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
70f50 74 65 73 20 69 6e 20 74 68 65 20 66 69 72 73 74  tes in the first
70f60 20 6e 43 68 61 72 20 75 6e 69 63 6f 64 65 20 63   nChar unicode c
70f70 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 69 6e 20  haracters.** in 
70f80 70 5a 20 28 6f 72 20 75 70 20 75 6e 74 69 6c 20  pZ (or up until 
70f90 74 68 65 20 66 69 72 73 74 20 70 61 69 72 20 6f  the first pair o
70fa0 66 20 30 78 30 30 20 62 79 74 65 73 2c 20 77 68  f 0x00 bytes, wh
70fb0 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69  ichever comes fi
70fc0 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rst)..*/.SQLITE_
70fd0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
70fe0 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28  te3Utf16ByteLen(
70ff0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e 2c  const void *zIn,
71000 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 75   int nChar){.  u
71010 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 20 3d 20  nsigned int c = 
71020 31 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  1;.  char const 
71030 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20  *z = zIn;.  int 
71040 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 4c  n = 0;.  if( SQL
71050 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d  ITE_UTF16NATIVE=
71060 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
71070 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e 67 20  ){.    /* Using 
71080 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 5f 55  an "if (SQLITE_U
71090 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49  TF16NATIVE==SQLI
710a0 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 6f 6e  TE_UTF16BE)" con
710b0 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 20 20  struct here.    
710c0 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 72 20  ** and in other 
710d0 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 66 69  parts of this fi
710e0 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 61 74  le means that at
710f0 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 6c 6c   one branch will
71100 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20 63  .    ** not be c
71110 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 72 61  overed by covera
71120 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 61 6e  ge testing on an
71130 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e 20 42  y single host. B
71140 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 20 20  ut coverage.    
71150 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 6c  ** will be compl
71160 65 74 65 20 69 66 20 74 68 65 20 74 65 73 74 73  ete if the tests
71170 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f 74 68   are run on both
71180 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e   a little-endian
71190 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 69 67   and .    ** big
711a0 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 42 65  -endian host. Be
711b0 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 20 55  cause both the U
711c0 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 20 53  TF16NATIVE and S
711d0 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a 20 20  QLITE_UTF16BE.  
711e0 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 65 20    ** macros are 
711f0 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70  constant at comp
71200 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 6f 6d  ile time the com
71210 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 72 6d  piler can determ
71220 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 63 68  ine.    ** which
71230 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 65 20   branch will be 
71240 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 73 20  followed. It is 
71250 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 6d 65  therefore assume
71260 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 69 6d  d that no runtim
71270 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c 74 79  e.    ** penalty
71280 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 68 69   is paid for thi
71290 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74  s "if" statement
712a0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69  ..    */.    whi
712b0 6c 65 28 20 63 20 26 26 20 28 28 6e 43 68 61 72  le( c && ((nChar
712c0 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29 20  <0) || n<nChar) 
712d0 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54  ){.      READ_UT
712e0 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16BE(z, c);.   
712f0 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     n++;.    }.  
71300 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65  }else{.    while
71310 28 20 63 20 26 26 20 28 28 6e 43 68 61 72 3c 30  ( c && ((nChar<0
71320 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29 20 29 7b  ) || n<nChar) ){
71330 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31  .      READ_UTF1
71340 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20  6LE(z, c);.     
71350 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   n++;.    }.  }.
71360 20 20 72 65 74 75 72 6e 20 28 7a 2d 28 63 68 61    return (z-(cha
71370 72 20 63 6f 6e 73 74 20 2a 29 7a 49 6e 29 2d 28  r const *)zIn)-(
71380 28 63 3d 3d 30 29 3f 32 3a 30 29 3b 0a 7d 0a 0a  (c==0)?2:0);.}..
71390 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
713a0 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54  TE_TEST)./*.** T
713b0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
713c0 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54  alled from the T
713d0 43 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e  CL test function
713e0 20 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66   "translate_self
713f0 74 65 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 65  test"..** It che
71400 63 6b 73 20 74 68 61 74 20 74 68 65 20 70 72 69  cks that the pri
71410 6d 69 74 69 76 65 73 20 66 6f 72 20 73 65 72 69  mitives for seri
71420 61 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65  alizing and dese
71430 72 69 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 61  rializing.** cha
71440 72 61 63 74 65 72 73 20 69 6e 20 65 61 63 68 20  racters in each 
71450 65 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e 76  encoding are inv
71460 65 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f 74  erses of each ot
71470 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  her..*/.SQLITE_P
71480 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71490 74 65 33 55 74 66 53 65 6c 66 54 65 73 74 28 29  te3UtfSelfTest()
714a0 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
714b0 20 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65   i, t;.  unsigne
714c0 64 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b  d char zBuf[20];
714d0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
714e0 20 2a 7a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20   *z;.  unsigned 
714f0 63 68 61 72 20 2a 7a 54 65 72 6d 3b 0a 20 20 69  char *zTerm;.  i
71500 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64  nt n;.  unsigned
71510 20 69 6e 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69   int c;..  for(i
71520 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30  =0; i<0x00110000
71530 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20  ; i++){.    z = 
71540 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f  zBuf;.    WRITE_
71550 55 54 46 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20  UTF8(z, i);.    
71560 6e 20 3d 20 7a 2d 7a 42 75 66 3b 0a 20 20 20 20  n = z-zBuf;.    
71570 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 54  z[0] = 0;.    zT
71580 65 72 6d 20 3d 20 7a 3b 0a 20 20 20 20 7a 20 3d  erm = z;.    z =
71590 20 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73   zBuf;.    c = s
715a0 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
715b0 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20  , zTerm, (const 
715c0 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20  u8**)&z);.    t 
715d0 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d  = i;.    if( i>=
715e0 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44  0xD800 && i<=0xD
715f0 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 44  FFF ) t = 0xFFFD
71600 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 46  ;.    if( (i&0xF
71610 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45  FFFFFFE)==0xFFFE
71620 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20   ) t = 0xFFFD;. 
71630 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20     assert( c==t 
71640 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
71650 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20  z-zBuf)==n );.  
71660 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30  }.  for(i=0; i<0
71670 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b  x00110000; i++){
71680 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38  .    if( i>=0xD8
71690 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 29  00 && i<0xE000 )
716a0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a   continue;.    z
716b0 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49   = zBuf;.    WRI
716c0 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29  TE_UTF16LE(z, i)
716d0 3b 0a 20 20 20 20 6e 20 3d 20 7a 2d 7a 42 75 66  ;.    n = z-zBuf
716e0 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a  ;.    z[0] = 0;.
716f0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
71700 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a    READ_UTF16LE(z
71710 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , c);.    assert
71720 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73  ( c==i );.    as
71730 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d  sert( (z-zBuf)==
71740 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  n );.  }.  for(i
71750 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30  =0; i<0x00110000
71760 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
71770 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30  i>=0xD800 && i<0
71780 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65  xE000 ) continue
71790 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a  ;.    z = zBuf;.
717a0 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 42      WRITE_UTF16B
717b0 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d  E(z, i);.    n =
717c0 20 7a 2d 7a 42 75 66 3b 0a 20 20 20 20 7a 5b 30   z-zBuf;.    z[0
717d0 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a  ] = 0;.    z = z
717e0 42 75 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54  Buf;.    READ_UT
717f0 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16BE(z, c);.   
71800 20 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b   assert( c==i );
71810 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d  .    assert( (z-
71820 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a  zBuf)==n );.  }.
71830 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
71840 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 69  TE_TEST */.#endi
71850 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
71860 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _UTF16 */../****
71870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
71880 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  f utf.c ********
71890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
718a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
718b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
718c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
718d0 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a 2a   file util.c ***
718e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
718f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
71910 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
71920 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
71930 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
71940 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
71950 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
71960 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
71970 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
71980 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
71990 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
719a0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
719b0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
719c0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
719d0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
719e0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
719f0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
71a00 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
71a10 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
71a20 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
71a30 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
71a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71a80 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 6e  *.** Utility fun
71a90 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f  ctions used thro
71aa0 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a  ughout sqlite..*
71ab0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
71ac0 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e  ontains function
71ad0 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e 67  s for allocating
71ae0 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 69   memory, compari
71af0 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 61  ng.** strings, a
71b00 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 68  nd stuff like th
71b10 61 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75  at..**.** $Id: u
71b20 74 69 6c 2e 63 2c 76 20 31 2e 32 31 32 20 32 30  til.c,v 1.212 20
71b30 30 37 2f 30 39 2f 30 31 20 31 30 3a 30 31 3a 31  07/09/01 10:01:1
71b40 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  3 danielk1977 Ex
71b50 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 53  p $.*/.../*.** S
71b60 65 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  et the most rece
71b70 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  nt error code an
71b80 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 66  d error string f
71b90 6f 72 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a  or the sqlite.**
71ba0 20 68 61 6e 64 6c 65 20 22 64 62 22 2e 20 54 68   handle "db". Th
71bb0 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
71bc0 73 65 74 20 74 6f 20 22 65 72 72 5f 63 6f 64 65  set to "err_code
71bd0 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69  "..**.** If it i
71be0 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69  s not NULL, stri
71bf0 6e 67 20 7a 46 6f 72 6d 61 74 20 73 70 65 63 69  ng zFormat speci
71c00 66 69 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20  fies the format 
71c10 6f 66 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20  of the.** error 
71c20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 73 74  string in the st
71c30 79 6c 65 20 6f 66 20 74 68 65 20 70 72 69 6e 74  yle of the print
71c40 66 20 66 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65  f functions: The
71c50 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f   following.** fo
71c60 72 6d 61 74 20 63 68 61 72 61 63 74 65 72 73 20  rmat characters 
71c70 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a  are allowed:.**.
71c80 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20 20 20  **      %s      
71c90 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e 67 0a  Insert a string.
71ca0 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20 20 20  **      %z      
71cb0 41 20 73 74 72 69 6e 67 20 74 68 61 74 20 73 68  A string that sh
71cc0 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20 61 66  ould be freed af
71cd0 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 20 20  ter use.**      
71ce0 25 64 20 20 20 20 20 20 49 6e 73 65 72 74 20 61  %d      Insert a
71cf0 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  n integer.**    
71d00 20 20 25 54 20 20 20 20 20 20 49 6e 73 65 72 74    %T      Insert
71d10 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20   a token.**     
71d20 20 25 53 20 20 20 20 20 20 49 6e 73 65 72 74 20   %S      Insert 
71d30 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e  the first elemen
71d40 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74 0a 2a  t of a SrcList.*
71d50 2a 0a 2a 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64  *.** zFormat and
71d60 20 61 6e 79 20 73 74 72 69 6e 67 20 74 6f 6b 65   any string toke
71d70 6e 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69  ns that follow i
71d80 74 20 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f  t are assumed to
71d90 20 62 65 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69   be.** encoded i
71da0 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54  n UTF-8..**.** T
71db0 6f 20 63 6c 65 61 72 20 74 68 65 20 6d 6f 73 74  o clear the most
71dc0 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 66 6f   recent error fo
71dd0 72 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 20  r sqlite handle 
71de0 22 64 62 22 2c 20 73 71 6c 69 74 65 33 45 72 72  "db", sqlite3Err
71df0 6f 72 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  or.** should be 
71e00 63 61 6c 6c 65 64 20 77 69 74 68 20 65 72 72 5f  called with err_
71e10 63 6f 64 65 20 73 65 74 20 74 6f 20 53 51 4c 49  code set to SQLI
71e20 54 45 5f 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61  TE_OK and zForma
71e30 74 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c  t set.** to NULL
71e40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
71e50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71e60 45 72 72 6f 72 28 73 71 6c 69 74 65 33 20 2a 64  Error(sqlite3 *d
71e70 62 2c 20 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c  b, int err_code,
71e80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
71e90 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66  rmat, ...){.  if
71ea0 28 20 64 62 20 26 26 20 28 64 62 2d 3e 70 45 72  ( db && (db->pEr
71eb0 72 20 7c 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d  r || (db->pErr =
71ec0 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
71ed0 28 64 62 29 29 21 3d 30 29 20 29 7b 0a 20 20 20  (db))!=0) ){.   
71ee0 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65   db->errCode = e
71ef0 72 72 5f 63 6f 64 65 3b 0a 20 20 20 20 69 66 28  rr_code;.    if(
71f00 20 7a 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20   zFormat ){.    
71f10 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20    char *z;.     
71f20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20   va_list ap;.   
71f30 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20     va_start(ap, 
71f40 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 20 20  zFormat);.      
71f50 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  z = sqlite3VMPri
71f60 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
71f70 20 61 70 29 3b 0a 20 20 20 20 20 20 76 61 5f 65   ap);.      va_e
71f80 6e 64 28 61 70 29 3b 0a 20 20 20 20 20 20 73 71  nd(ap);.      sq
71f90 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
71fa0 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a  (db->pErr, -1, z
71fb0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73  , SQLITE_UTF8, s
71fc0 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
71fd0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
71fe0 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
71ff0 72 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30  r(db->pErr, 0, 0
72000 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
72010 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
72020 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
72030 2a 20 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d  * Add an error m
72040 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65  essage to pParse
72050 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e  ->zErrMsg and in
72060 63 72 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e  crement pParse->
72070 6e 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c  nErr..** The fol
72080 6c 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e  lowing formattin
72090 67 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  g characters are
720a0 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20   allowed:.**.** 
720b0 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73       %s      Ins
720c0 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20  ert a string.** 
720d0 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73       %z      A s
720e0 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c  tring that shoul
720f0 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72  d be freed after
72100 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20   use.**      %d 
72110 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69       Insert an i
72120 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25  nteger.**      %
72130 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20  T      Insert a 
72140 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53  token.**      %S
72150 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65        Insert the
72160 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
72170 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a  f a SrcList.**.*
72180 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
72190 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74  should be used t
721a0 6f 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72  o report any err
721b0 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77  or that occurs w
721c0 68 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69  hilst.** compili
721d0 6e 67 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  ng an SQL statem
721e0 65 6e 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e  ent (i.e. within
721f0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
72200 28 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74  ()). The.** last
72210 20 74 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74   thing the sqlit
72220 65 33 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e  e3_prepare() fun
72230 63 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f  ction does is co
72240 70 79 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20  py the error.** 
72250 73 74 6f 72 65 64 20 62 79 20 74 68 69 73 20 66  stored by this f
72260 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  unction into the
72270 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
72280 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72   using sqlite3Er
72290 72 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69  ror()..** Functi
722a0 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  on sqlite3Error(
722b0 29 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  ) should be used
722c0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e   during statemen
722d0 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28  t execution.** (
722e0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65  sqlite3_step() e
722f0 74 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tc.)..*/.SQLITE_
72300 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
72310 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72  ite3ErrorMsg(Par
72320 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
72330 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
72340 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
72350 20 61 70 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e   ap;.  pParse->n
72360 45 72 72 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  Err++;.  sqlite3
72370 5f 66 72 65 65 28 70 50 61 72 73 65 2d 3e 7a 45  _free(pParse->zE
72380 72 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61  rrMsg);.  va_sta
72390 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
723a0 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  .  pParse->zErrM
723b0 73 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72  sg = sqlite3VMPr
723c0 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62 2c  intf(pParse->db,
723d0 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
723e0 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69   va_end(ap);.  i
723f0 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  f( pParse->rc==S
72400 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
72410 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
72420 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 7d  ITE_ERROR;.  }.}
72430 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
72440 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
72450 69 6e 20 70 50 61 72 73 65 2c 20 69 66 20 61 6e  in pParse, if an
72460 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  y.*/.SQLITE_PRIV
72470 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
72480 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65  ErrorClear(Parse
72490 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
724a0 69 74 65 33 5f 66 72 65 65 28 70 50 61 72 73 65  ite3_free(pParse
724b0 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50  ->zErrMsg);.  pP
724c0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
724d0 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  0;.  pParse->nEr
724e0 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r = 0;.}../*.** 
724f0 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73  Convert an SQL-s
72500 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69  tyle quoted stri
72510 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c  ng into a normal
72520 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76   string by remov
72530 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65  ing.** the quote
72540 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68   characters.  Th
72550 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
72560 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20  done in-place.  
72570 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20  If the.** input 
72580 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77  does not begin w
72590 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72  ith a quote char
725a0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73  acter, then this
725b0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61   routine.** is a
725c0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 32 30   no-op..**.** 20
725d0 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 73 20  02-Feb-14: This 
725e0 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 65 6e  routine is exten
725f0 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 4d 53  ded to remove MS
72600 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a 2a 2a  -Access style.**
72610 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d 20 61   brackets from a
72620 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 72 73  round identifers
72630 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 20  .  For example: 
72640 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 6f 6d   "[a-b-c]" becom
72650 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a  es.** "a-b-c"..*
72660 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
72670 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 71   void sqlite3Deq
72680 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20  uote(char *z){. 
72690 20 69 6e 74 20 71 75 6f 74 65 3b 0a 20 20 69 6e   int quote;.  in
726a0 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a 3d  t i, j;.  if( z=
726b0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 71  =0 ) return;.  q
726c0 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73  uote = z[0];.  s
726d0 77 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b 0a  witch( quote ){.
726e0 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20 20      case '\'':  
726f0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
72700 27 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20  '"':   break;.  
72710 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 62 72    case '`':   br
72720 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  eak;            
72730 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c      /* For MySQL
72740 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a   compatibility *
72750 2f 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20  /.    case '[': 
72760 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20    quote = ']';  
72770 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d  break;  /* For M
72780 53 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d 70  S SqlServer comp
72790 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20  atibility */.   
727a0 20 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65 74   default:    ret
727b0 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  urn;.  }.  for(i
727c0 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69  =1, j=0; z[i]; i
727d0 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69  ++){.    if( z[i
727e0 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20  ]==quote ){.    
727f0 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75    if( z[i+1]==qu
72800 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ote ){.        z
72810 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20  [j++] = quote;. 
72820 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20         i++;.    
72830 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
72840 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20 20   z[j++] = 0;.   
72850 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
72860 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
72870 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b       z[j++] = z[
72880 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  i];.    }.  }.}.
72890 0a 2f 2a 20 41 6e 20 61 72 72 61 79 20 74 6f 20  ./* An array to 
728a0 6d 61 70 20 61 6c 6c 20 75 70 70 65 72 2d 63 61  map all upper-ca
728b0 73 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  se characters in
728c0 74 6f 20 74 68 65 69 72 20 63 6f 72 72 65 73 70  to their corresp
728d0 6f 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d  onding.** lower-
728e0 63 61 73 65 20 63 68 61 72 61 63 74 65 72 2e 20  case character. 
728f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
72900 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  TE const unsigne
72910 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70  d char sqlite3Up
72920 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b  perToLower[] = {
72930 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41  .#ifdef SQLITE_A
72940 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20 20 31  SCII.      0,  1
72950 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35  ,  2,  3,  4,  5
72960 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39  ,  6,  7,  8,  9
72970 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33  , 10, 11, 12, 13
72980 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 31 37  , 14, 15, 16, 17
72990 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 20 32  ,.     18, 19, 2
729a0 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32  0, 21, 22, 23, 2
729b0 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32  4, 25, 26, 27, 2
729c0 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 33  8, 29, 30, 31, 3
729d0 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 0a 20  2, 33, 34, 35,. 
729e0 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38 2c 20      36, 37, 38, 
729f0 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20  39, 40, 41, 42, 
72a00 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20  43, 44, 45, 46, 
72a10 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 2c 20  47, 48, 49, 50, 
72a20 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 20 20  51, 52, 53,.    
72a30 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c   54, 55, 56, 57,
72a40 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c   58, 59, 60, 61,
72a50 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 37 2c   62, 63, 64, 97,
72a60 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c   98, 99,100,101,
72a70 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 30 34  102,103,.    104
72a80 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 30 38  ,105,106,107,108
72a90 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32  ,109,110,111,112
72aa0 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36  ,113,114,115,116
72ab0 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30  ,117,118,119,120
72ac0 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20 39  ,121,.    122, 9
72ad0 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39  1, 92, 93, 94, 9
72ae0 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 20 39  5, 96, 97, 98, 9
72af0 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30  9,100,101,102,10
72b00 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30  3,104,105,106,10
72b10 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 2c 31  7,.    108,109,1
72b20 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31  10,111,112,113,1
72b30 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31  14,115,116,117,1
72b40 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 31  18,119,120,121,1
72b50 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 0a  22,123,124,125,.
72b60 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32 38 2c      126,127,128,
72b70 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c  129,130,131,132,
72b80 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c  133,134,135,136,
72b90 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c  137,138,139,140,
72ba0 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 20 20  141,142,143,.   
72bb0 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37   144,145,146,147
72bc0 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31  ,148,149,150,151
72bd0 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35  ,152,153,154,155
72be0 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 35 39  ,156,157,158,159
72bf0 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 31 36  ,160,161,.    16
72c00 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36  2,163,164,165,16
72c10 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37  6,167,168,169,17
72c20 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 31 37  0,171,172,173,17
72c30 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 31 37  4,175,176,177,17
72c40 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 2c 31  8,179,.    180,1
72c50 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31  81,182,183,184,1
72c60 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31  85,186,187,188,1
72c70 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 2c 31  89,190,191,192,1
72c80 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 2c 31  93,194,195,196,1
72c90 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 39 2c  97,.    198,199,
72ca0 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 33 2c  200,201,202,203,
72cb0 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c  204,205,206,207,
72cc0 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 31 2c  208,209,210,211,
72cd0 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 35 2c  212,213,214,215,
72ce0 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32 31 38  .    216,217,218
72cf0 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32  ,219,220,221,222
72d00 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 32 36  ,223,224,225,226
72d10 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 33 30  ,227,228,229,230
72d20 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 20 20  ,231,232,233,.  
72d30 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 32 33    234,235,236,23
72d40 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 32 34  7,238,239,240,24
72d50 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34  1,242,243,244,24
72d60 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34  5,246,247,248,24
72d70 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 20 32  9,250,251,.    2
72d80 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 0a 23  52,253,254,255.#
72d90 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
72da0 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20 20 20  ITE_EBCDIC.     
72db0 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20   0,  1,  2,  3, 
72dc0 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20   4,  5,  6,  7, 
72dd0 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20   8,  9, 10, 11, 
72de0 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20  12, 13, 14, 15, 
72df0 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 31 36  /* 0x */.     16
72e00 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 32 30  , 17, 18, 19, 20
72e10 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34  , 21, 22, 23, 24
72e20 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38  , 25, 26, 27, 28
72e30 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 2f 2a  , 29, 30, 31, /*
72e40 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 2c 20   1x */.     32, 
72e50 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 2c 20  33, 34, 35, 36, 
72e60 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20  37, 38, 39, 40, 
72e70 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20  41, 42, 43, 44, 
72e80 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a 20 32  45, 46, 47, /* 2
72e90 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 34 39  x */.     48, 49
72ea0 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33  , 50, 51, 52, 53
72eb0 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37  , 54, 55, 56, 57
72ec0 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31  , 58, 59, 60, 61
72ed0 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 78 20  , 62, 63, /* 3x 
72ee0 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 2c 20  */.     64, 65, 
72ef0 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20  66, 67, 68, 69, 
72f00 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 20  70, 71, 72, 73, 
72f10 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 2c 20  74, 75, 76, 77, 
72f20 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 2a 2f  78, 79, /* 4x */
72f30 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20 38 32  .     80, 81, 82
72f40 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36  , 83, 84, 85, 86
72f50 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 39 30  , 87, 88, 89, 90
72f60 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34  , 91, 92, 93, 94
72f70 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20  , 95, /* 5x */. 
72f80 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 2c 20      96, 97, 66, 
72f90 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20  67, 68, 69, 70, 
72fa0 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 2c 31  71, 72, 73,106,1
72fb0 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31  07,108,109,110,1
72fc0 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  11, /* 6x */.   
72fd0 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 38 33   112, 81, 82, 83
72fe0 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37  , 84, 85, 86, 87
72ff0 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 32 33  , 88, 89,122,123
73000 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 32 37  ,124,125,126,127
73010 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20 31  , /* 7x */.    1
73020 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31  28,129,130,131,1
73030 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31  32,133,134,135,1
73040 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31  36,137,138,139,1
73050 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 20  40,141,142,143, 
73060 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 34 34  /* 8x */.    144
73070 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38  ,145,146,147,148
73080 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32  ,149,150,151,152
73090 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36  ,153,154,155,156
730a0 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 2f 2a  ,157,156,159, /*
730b0 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 2c 31   9x */.    160,1
730c0 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31  61,162,163,164,1
730d0 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31  65,166,167,168,1
730e0 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 2c 31  69,170,171,140,1
730f0 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a 20 41  41,142,175, /* A
73100 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 37 37  x */.    176,177
73110 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 38 31  ,178,179,180,181
73120 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35  ,182,183,184,185
73130 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39  ,186,187,188,189
73140 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 78 20  ,190,191, /* Bx 
73150 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 2c 31  */.    192,129,1
73160 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31  30,131,132,133,1
73170 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 32  34,135,136,137,2
73180 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32  02,203,204,205,2
73190 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 2a 2f  06,207, /* Cx */
731a0 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31 34 36  .    208,145,146
731b0 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30  ,147,148,149,150
731c0 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 31 38  ,151,152,153,218
731d0 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32  ,219,220,221,222
731e0 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20  ,223, /* Dx */. 
731f0 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32 2c 31     224,225,162,1
73200 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31  63,164,165,166,1
73210 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 2c 32  67,168,169,232,2
73220 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32  03,204,205,206,2
73230 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20  07, /* Ex */.   
73240 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32   239,240,241,242
73250 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36  ,243,244,245,246
73260 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 31 39  ,247,248,249,219
73270 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 35 35  ,220,221,222,255
73280 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 64 69  , /* Fx */.#endi
73290 66 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 55 70 70  f.};.#define Upp
732a0 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65  erToLower sqlite
732b0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f  3UpperToLower../
732c0 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d  *.** Some system
732d0 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29  s have stricmp()
732e0 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73  .  Others have s
732f0 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65  trcasecmp().  Be
73300 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69  cause.** there i
73310 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79  s no consistency
73320 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65  , we will define
73330 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c   our own..*/.SQL
73340 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
73350 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63  sqlite3StrICmp(c
73360 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
73370 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
73380 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65  ight){.  registe
73390 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
733a0 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
733b0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
733c0 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
733d0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
733e0 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61  ght;.  while( *a
733f0 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f  !=0 && UpperToLo
73400 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f  wer[*a]==UpperTo
73410 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b  Lower[*b]){ a++;
73420 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e   b++; }.  return
73430 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
73440 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ] - UpperToLower
73450 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  [*b];.}.SQLITE_P
73460 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73470 65 33 53 74 72 4e 49 43 6d 70 28 63 6f 6e 73 74  e3StrNICmp(const
73480 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f   char *zLeft, co
73490 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74  nst char *zRight
734a0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69  , int N){.  regi
734b0 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 63 68  ster unsigned ch
734c0 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d  ar *a, *b;.  a =
734d0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
734e0 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28  *)zLeft;.  b = (
734f0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
73500 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c 65 28  zRight;.  while(
73510 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d   N-- > 0 && *a!=
73520 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f 77 65  0 && UpperToLowe
73530 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f  r[*a]==UpperToLo
73540 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62  wer[*b]){ a++; b
73550 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e  ++; }.  return N
73560 3c 30 20 3f 20 30 20 3a 20 55 70 70 65 72 54 6f  <0 ? 0 : UpperTo
73570 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65  Lower[*a] - Uppe
73580 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a  rToLower[*b];.}.
73590 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
735a0 55 45 20 69 66 20 7a 20 69 73 20 61 20 70 75 72  UE if z is a pur
735b0 65 20 6e 75 6d 65 72 69 63 20 73 74 72 69 6e 67  e numeric string
735c0 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20  .  Return FALSE 
735d0 69 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67  if the.** string
735e0 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63 68   contains any ch
735f0 61 72 61 63 74 65 72 20 77 68 69 63 68 20 69 73  aracter which is
73600 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6e   not part of a n
73610 75 6d 62 65 72 2e 20 49 66 0a 2a 2a 20 74 68 65  umber. If.** the
73620 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6d 65 72   string is numer
73630 69 63 20 61 6e 64 20 63 6f 6e 74 61 69 6e 73 20  ic and contains 
73640 74 68 65 20 27 2e 27 20 63 68 61 72 61 63 74 65  the '.' characte
73650 72 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 0a  r, set *realnum.
73660 2a 2a 20 74 6f 20 54 52 55 45 20 28 6f 74 68 65  ** to TRUE (othe
73670 72 77 69 73 65 20 46 41 4c 53 45 29 2e 0a 2a 2a  rwise FALSE)..**
73680 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20 73 74 72  .** An empty str
73690 69 6e 67 20 69 73 20 63 6f 6e 73 69 64 65 72 65  ing is considere
736a0 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a  d non-numeric..*
736b0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
736c0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75   int sqlite3IsNu
736d0 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20  mber(const char 
736e0 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d  *z, int *realnum
736f0 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74  , u8 enc){.  int
73700 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51   incr = (enc==SQ
73710 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a  LITE_UTF8?1:2);.
73720 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54    if( enc==SQLIT
73730 45 5f 55 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b  E_UTF16BE ) z++;
73740 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c  .  if( *z=='-' |
73750 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d  | *z=='+' ) z +=
73760 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 21 69 73   incr;.  if( !is
73770 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29  digit(*(u8*)z) )
73780 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
73790 20 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b    }.  z += incr;
737a0 0a 20 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 29  .  if( realnum )
737b0 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20   *realnum = 0;. 
737c0 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28   while( isdigit(
737d0 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a 20 2b 3d  *(u8*)z) ){ z +=
737e0 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a   incr; }.  if( *
737f0 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20  z=='.' ){.    z 
73800 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28  += incr;.    if(
73810 20 21 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29   !isdigit(*(u8*)
73820 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  z) ) return 0;. 
73830 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69     while( isdigi
73840 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a 20  t(*(u8*)z) ){ z 
73850 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 69  += incr; }.    i
73860 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65  f( realnum ) *re
73870 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20  alnum = 1;.  }. 
73880 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20   if( *z=='e' || 
73890 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a  *z=='E' ){.    z
738a0 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66   += incr;.    if
738b0 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d  ( *z=='+' || *z=
738c0 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72  ='-' ) z += incr
738d0 3b 0a 20 20 20 20 69 66 28 20 21 69 73 64 69 67  ;.    if( !isdig
738e0 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 20 72 65  it(*(u8*)z) ) re
738f0 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c  turn 0;.    whil
73900 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a  e( isdigit(*(u8*
73910 29 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72  )z) ){ z += incr
73920 3b 20 7d 0a 20 20 20 20 69 66 28 20 72 65 61 6c  ; }.    if( real
73930 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d 20 3d  num ) *realnum =
73940 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
73950 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   *z==0;.}../*.**
73960 20 54 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20   The string z[] 
73970 69 73 20 61 6e 20 61 73 63 69 69 20 72 65 70 72  is an ascii repr
73980 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20  esentation of a 
73990 72 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  real number..** 
739a0 43 6f 6e 76 65 72 74 20 74 68 69 73 20 73 74 72  Convert this str
739b0 69 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e  ing to a double.
739c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
739d0 69 6e 65 20 61 73 73 75 6d 65 73 20 74 68 61 74  ine assumes that
739e0 20 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61   z[] really is a
739f0 20 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20   valid number.  
73a00 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c  If it.** is not,
73a10 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75   the result is u
73a20 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
73a30 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
73a40 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20  used instead of 
73a50 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66  the library atof
73a60 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61  () function beca
73a70 75 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61  use.** the libra
73a80 72 79 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20  ry atof() might 
73a90 77 61 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20  want to use "," 
73aa0 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70  as the decimal p
73ab0 6f 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20  oint instead.** 
73ac0 6f 66 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67  of "." depending
73ad0 20 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69   on how locale i
73ae0 73 20 73 65 74 2e 20 20 42 75 74 20 74 68 61 74  s set.  But that
73af0 20 77 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f   would cause pro
73b00 62 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c  blems.** for SQL
73b10 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69  .  So this routi
73b20 6e 65 20 61 6c 77 61 79 73 20 75 73 65 73 20 22  ne always uses "
73b30 2e 22 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  ." regardless of
73b40 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   locale..*/.SQLI
73b50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
73b60 71 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74  qlite3AtoF(const
73b70 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65   char *z, double
73b80 20 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e   *pResult){.#ifn
73b90 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
73ba0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
73bb0 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 0a 20   int sign = 1;. 
73bc0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65   const char *zBe
73bd0 67 69 6e 20 3d 20 7a 3b 0a 20 20 4c 4f 4e 47 44  gin = z;.  LONGD
73be0 4f 55 42 4c 45 5f 54 59 50 45 20 76 31 20 3d 20  OUBLE_TYPE v1 = 
73bf0 30 2e 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  0.0;.  while( is
73c00 73 70 61 63 65 28 2a 28 75 38 2a 29 7a 29 20 29  space(*(u8*)z) )
73c10 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d   z++;.  if( *z==
73c20 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e 20  '-' ){.    sign 
73c30 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  = -1;.    z++;. 
73c40 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27   }else if( *z=='
73c50 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  +' ){.    z++;. 
73c60 20 7d 0a 20 20 77 68 69 6c 65 28 20 69 73 64 69   }.  while( isdi
73c70 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a  git(*(u8*)z) ){.
73c80 20 20 20 20 76 31 20 3d 20 76 31 2a 31 30 2e 30      v1 = v1*10.0
73c90 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20   + (*z - '0');. 
73ca0 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66     z++;.  }.  if
73cb0 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  ( *z=='.' ){.   
73cc0 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
73cd0 20 64 69 76 69 73 6f 72 20 3d 20 31 2e 30 3b 0a   divisor = 1.0;.
73ce0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 77 68 69      z++;.    whi
73cf0 6c 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38  le( isdigit(*(u8
73d00 2a 29 7a 29 20 29 7b 0a 20 20 20 20 20 20 76 31  *)z) ){.      v1
73d10 20 3d 20 76 31 2a 31 30 2e 30 20 2b 20 28 2a 7a   = v1*10.0 + (*z
73d20 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 64   - '0');.      d
73d30 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a  ivisor *= 10.0;.
73d40 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d        z++;.    }
73d50 0a 20 20 20 20 76 31 20 2f 3d 20 64 69 76 69 73  .    v1 /= divis
73d60 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a  or;.  }.  if( *z
73d70 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27  =='e' || *z=='E'
73d80 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 73 69 67   ){.    int esig
73d90 6e 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20 65  n = 1;.    int e
73da0 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 4c 4f 4e  val = 0;.    LON
73db0 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 63 61  GDOUBLE_TYPE sca
73dc0 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b  le = 1.0;.    z+
73dd0 2b 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27  +;.    if( *z=='
73de0 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73 69 67  -' ){.      esig
73df0 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a 2b  n = -1;.      z+
73e00 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  +;.    }else if(
73e10 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20   *z=='+' ){.    
73e20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20    z++;.    }.   
73e30 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28   while( isdigit(
73e40 2a 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20  *(u8*)z) ){.    
73e50 20 20 65 76 61 6c 20 3d 20 65 76 61 6c 2a 31 30    eval = eval*10
73e60 20 2b 20 2a 7a 20 2d 20 27 30 27 3b 0a 20 20 20   + *z - '0';.   
73e70 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     z++;.    }.  
73e80 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 36    while( eval>=6
73e90 34 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e  4 ){ scale *= 1.
73ea0 30 65 2b 36 34 3b 20 65 76 61 6c 20 2d 3d 20 36  0e+64; eval -= 6
73eb0 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20  4; }.    while( 
73ec0 65 76 61 6c 3e 3d 31 36 20 29 7b 20 73 63 61 6c  eval>=16 ){ scal
73ed0 65 20 2a 3d 20 31 2e 30 65 2b 31 36 3b 20 65 76  e *= 1.0e+16; ev
73ee0 61 6c 20 2d 3d 20 31 36 3b 20 7d 0a 20 20 20 20  al -= 16; }.    
73ef0 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 34 20 29  while( eval>=4 )
73f00 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b  { scale *= 1.0e+
73f10 34 3b 20 65 76 61 6c 20 2d 3d 20 34 3b 20 7d 0a  4; eval -= 4; }.
73f20 20 20 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e      while( eval>
73f30 3d 31 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31  =1 ){ scale *= 1
73f40 2e 30 65 2b 31 3b 20 65 76 61 6c 20 2d 3d 20 31  .0e+1; eval -= 1
73f50 3b 20 7d 0a 20 20 20 20 69 66 28 20 65 73 69 67  ; }.    if( esig
73f60 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 76 31 20  n<0 ){.      v1 
73f70 2f 3d 20 73 63 61 6c 65 3b 0a 20 20 20 20 7d 65  /= scale;.    }e
73f80 6c 73 65 7b 0a 20 20 20 20 20 20 76 31 20 2a 3d  lse{.      v1 *=
73f90 20 73 63 61 6c 65 3b 0a 20 20 20 20 7d 0a 20 20   scale;.    }.  
73fa0 7d 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 73  }.  *pResult = s
73fb0 69 67 6e 3c 30 20 3f 20 2d 76 31 20 3a 20 76 31  ign<0 ? -v1 : v1
73fc0 3b 0a 20 20 72 65 74 75 72 6e 20 7a 20 2d 20 7a  ;.  return z - z
73fd0 42 65 67 69 6e 3b 0a 23 65 6c 73 65 0a 20 20 72  Begin;.#else.  r
73fe0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 74 6f  eturn sqlite3Ato
73ff0 69 36 34 28 7a 2c 20 70 52 65 73 75 6c 74 29 3b  i64(z, pResult);
74000 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
74010 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
74020 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a  POINT */.}../*.*
74030 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 31 39  * Compare the 19
74040 2d 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e  -character strin
74050 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73 74 20 74  g zNum against t
74060 68 65 20 74 65 78 74 20 72 65 70 72 65 73 65 6e  he text represen
74070 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c 75 65 20  tation.** value 
74080 32 5e 36 33 3a 20 20 39 32 32 33 33 37 32 30 33  2^63:  922337203
74090 36 38 35 34 37 37 35 38 30 38 2e 20 20 52 65 74  6854775808.  Ret
740a0 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65  urn negative, ze
740b0 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 0a  ro, or positive.
740c0 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73 20 6c 65  ** if zNum is le
740d0 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74  ss than, equal t
740e0 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 68  o, or greater th
740f0 61 6e 20 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a  an the string..*
74100 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 65 6d 63  *.** Unlike memc
74110 6d 70 28 29 20 74 68 69 73 20 72 6f 75 74 69 6e  mp() this routin
74120 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  e is guaranteed 
74130 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 64 69  to return the di
74140 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69 6e 20 74  fference.** in t
74150 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65  he values of the
74160 20 6c 61 73 74 20 64 69 67 69 74 20 69 66 20 74   last digit if t
74170 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e  he only differen
74180 63 65 20 69 73 20 69 6e 20 74 68 65 0a 2a 2a 20  ce is in the.** 
74190 6c 61 73 74 20 64 69 67 69 74 2e 20 20 53 6f 2c  last digit.  So,
741a0 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a   for example,.**
741b0 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70 61 72 65  .**      compare
741c0 32 70 6f 77 36 33 28 22 39 32 32 33 33 37 32 30  2pow63("92233720
741d0 33 36 38 35 34 37 37 35 38 30 30 22 29 0a 2a 2a  36854775800").**
741e0 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  .** will return 
741f0 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  -8..*/.static in
74200 74 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28  t compare2pow63(
74210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d  const char *zNum
74220 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 63 20  ){.  int c;.  c 
74230 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d 2c 22 39  = memcmp(zNum,"9
74240 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
74250 30 22 2c 31 38 29 3b 0a 20 20 69 66 28 20 63 3d  0",18);.  if( c=
74260 3d 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 7a 4e  =0 ){.    c = zN
74270 75 6d 5b 31 38 5d 20 2d 20 27 38 27 3b 0a 20 20  um[18] - '8';.  
74280 7d 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a  }.  return c;.}.
74290 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
742a0 52 55 45 20 69 66 20 7a 4e 75 6d 20 69 73 20 61  RUE if zNum is a
742b0 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
742c0 6e 74 65 67 65 72 20 61 6e 64 20 77 72 69 74 65  nteger and write
742d0 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
742e0 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 74   the integer int
742f0 6f 20 2a 70 4e 75 6d 2e 20 20 49 66 20 7a 4e 75  o *pNum.  If zNu
74300 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  m is not an inte
74310 67 65 72 0a 2a 2a 20 6f 72 20 69 73 20 61 6e 20  ger.** or is an 
74320 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20  integer that is 
74330 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 62 65 20  too large to be 
74340 65 78 70 72 65 73 73 65 64 20 77 69 74 68 20 36  expressed with 6
74350 34 20 62 69 74 73 2c 0a 2a 2a 20 74 68 65 6e 20  4 bits,.** then 
74360 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a  return false..**
74370 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f  .** When this ro
74380 75 74 69 6e 65 20 77 61 73 20 6f 72 69 67 69 6e  utine was origin
74390 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 74 20  ally written it 
743a0 64 65 61 6c 74 20 77 69 74 68 20 6f 6e 6c 79 0a  dealt with only.
743b0 2a 2a 20 33 32 2d 62 69 74 20 6e 75 6d 62 65 72  ** 32-bit number
743c0 73 2e 20 20 41 74 20 74 68 61 74 20 74 69 6d 65  s.  At that time
743d0 2c 20 69 74 20 77 61 73 20 6d 75 63 68 20 66 61  , it was much fa
743e0 73 74 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a  ster than the.**
743f0 20 61 74 6f 69 28 29 20 6c 69 62 72 61 72 79 20   atoi() library 
74400 72 6f 75 74 69 6e 65 20 69 6e 20 52 65 64 48 61  routine in RedHa
74410 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45  t 7.2..*/.SQLITE
74420 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
74430 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74  ite3Atoi64(const
74440 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34   char *zNum, i64
74450 20 2a 70 4e 75 6d 29 7b 0a 20 20 69 36 34 20 76   *pNum){.  i64 v
74460 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 65 67 3b   = 0;.  int neg;
74470 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 77  .  int i, c;.  w
74480 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28  hile( isspace(*(
74490 75 38 2a 29 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d  u8*)zNum) ) zNum
744a0 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d  ++;.  if( *zNum=
744b0 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20  ='-' ){.    neg 
744c0 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b  = 1;.    zNum++;
744d0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e  .  }else if( *zN
744e0 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e  um=='+' ){.    n
744f0 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d  eg = 0;.    zNum
74500 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ++;.  }else{.   
74510 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20   neg = 0;.  }.  
74520 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d  while( zNum[0]==
74530 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d  '0' ){ zNum++; }
74540 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c 65   /* Skip over le
74550 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63  ading zeros. Tic
74560 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 66  ket #2454 */.  f
74570 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b  or(i=0; (c=zNum[
74580 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27  i])>='0' && c<='
74590 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20  9'; i++){.    v 
745a0 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27  = v*10 + c - '0'
745b0 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20  ;.  }.  *pNum = 
745c0 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20  neg ? -v : v;.  
745d0 69 66 28 20 63 21 3d 30 20 7c 7c 20 69 3d 3d 30  if( c!=0 || i==0
745e0 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20 20   || i>19 ){.    
745f0 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74 79  /* zNum is empty
74600 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e   or contains non
74610 2d 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f 72  -numeric text or
74620 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a   is longer.    *
74630 2a 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73  * than 19 digits
74640 20 28 74 68 75 73 20 67 75 61 72 61 6e 74 69 6e   (thus guarantin
74650 67 20 74 68 61 74 20 69 74 20 69 73 20 74 6f 6f  g that it is too
74660 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20 72   large) */.    r
74670 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
74680 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20   if( i<19 ){.   
74690 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39   /* Less than 19
746a0 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b   digits, so we k
746b0 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73  now that it fits
746c0 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20   in 64 bits */. 
746d0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
746e0 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39 2d  else{.    /* 19-
746f0 64 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d 75  digit numbers mu
74700 73 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72 20  st be no larger 
74710 74 68 61 6e 20 39 32 32 33 33 37 32 30 33 36 38  than 92233720368
74720 35 34 37 37 35 38 30 37 20 69 66 20 70 6f 73 69  54775807 if posi
74730 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20 39  tive.    ** or 9
74740 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
74750 30 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20  08 if negative. 
74760 20 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33 33   Note that 92233
74770 37 32 30 33 36 38 35 34 36 36 35 38 30 38 0a 20  72036854665808. 
74780 20 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20 2a     ** is 2^63. *
74790 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d  /.    return com
747a0 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29  pare2pow63(zNum)
747b0 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  <neg;.  }.}../*.
747c0 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 4e  ** The string zN
747d0 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e  um represents an
747e0 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65 72 65   integer.  There
747f0 20 6d 69 67 68 74 20 62 65 20 73 6f 6d 65 20 6f   might be some o
74800 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  ther.** informat
74810 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ion following th
74820 65 20 69 6e 74 65 67 65 72 20 74 6f 6f 2c 20 62  e integer too, b
74830 75 74 20 74 68 61 74 20 70 61 72 74 20 69 73 20  ut that part is 
74840 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 49 66 20 74  ignored..** If t
74850 68 65 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  he integer that 
74860 74 68 65 20 70 72 65 66 69 78 20 6f 66 20 7a 4e  the prefix of zN
74870 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 77 69  um represents wi
74880 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a 2a 20 36  ll fit in a.** 6
74890 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
748a0 65 67 65 72 2c 20 72 65 74 75 72 6e 20 54 52 55  eger, return TRU
748b0 45 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65  E.  Otherwise re
748c0 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a  turn FALSE..**.*
748d0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
748e0 65 74 75 72 6e 73 20 46 41 4c 53 45 20 66 6f 72  eturns FALSE for
748f0 20 74 68 65 20 73 74 72 69 6e 67 20 2d 39 32 32   the string -922
74900 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38  3372036854775808
74910 20 65 76 65 6e 20 74 68 61 74 0a 2a 2a 20 74 68   even that.** th
74920 61 74 20 6e 75 6d 62 65 72 20 77 69 6c 6c 2c 20  at number will, 
74930 69 6e 20 74 68 65 6f 72 79 20 66 69 74 20 69 6e  in theory fit in
74940 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65   a 64-bit intege
74950 72 2e 20 20 50 6f 73 69 74 69 76 65 0a 2a 2a 20  r.  Positive.** 
74960 39 32 32 33 33 37 33 30 33 36 38 35 34 37 37 35  9223373036854775
74970 38 30 38 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  808 will not fit
74980 20 69 6e 20 36 34 20 62 69 74 73 2e 20 20 53 6f   in 64 bits.  So
74990 20 69 74 20 73 65 65 6d 73 20 73 61 66 65 72 20   it seems safer 
749a0 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 66 61 6c  to return.** fal
749b0 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
749c0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
749d0 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63 6f  3FitsIn64Bits(co
749e0 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 29 7b  nst char *zNum){
749f0 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69  .  int i, c;.  i
74a00 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66  nt neg = 0;.  if
74a10 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a  ( *zNum=='-' ){.
74a20 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20      neg = 1;.   
74a30 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65   zNum++;.  }else
74a40 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20   if( *zNum=='+' 
74a50 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20  ){.    zNum++;. 
74a60 20 7d 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e 75   }.  while( *zNu
74a70 6d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 4e  m=='0' ){.    zN
74a80 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 20  um++;   /* Skip 
74a90 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 20  leading zeros.  
74aa0 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a  Ticket #2454 */.
74ab0 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28    }.  for(i=0; (
74ac0 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20  c=zNum[i])>='0' 
74ad0 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b  && c<='9'; i++){
74ae0 7d 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a  }.  if( i<19 ){.
74af0 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65      /* Guarantee
74b00 64 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73  d to fit if less
74b10 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20   than 19 digits 
74b20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  */.    return 1;
74b30 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31  .  }else if( i>1
74b40 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72  9 ){.    /* Guar
74b50 61 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f  anteed to be too
74b60 20 62 69 67 20 69 66 20 67 72 65 61 74 65 72 20   big if greater 
74b70 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a  than 19 digits *
74b80 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
74b90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
74ba0 43 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20  Compare against 
74bb0 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74  2^63. */.    ret
74bc0 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36  urn compare2pow6
74bd0 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d  3(zNum)<neg;.  }
74be0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75  .}../*.** If zNu
74bf0 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  m represents an 
74c00 69 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 6c  integer that wil
74c10 6c 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73  l fit in 32-bits
74c20 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70  , then set.** *p
74c30 56 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e  Value to that in
74c40 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e  teger and return
74c50 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73   true.  Otherwis
74c60 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a  e return false..
74c70 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75  **.** Any non-nu
74c80 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73  meric characters
74c90 20 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20   that following 
74ca0 7a 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64  zNum are ignored
74cb0 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66  ..** This is dif
74cc0 66 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69  ferent from sqli
74cd0 74 65 33 41 74 6f 69 36 34 28 29 20 77 68 69 63  te3Atoi64() whic
74ce0 68 20 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a  h requires the.*
74cf0 2a 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74  * input number t
74d00 6f 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  o be zero-termin
74d10 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
74d20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
74d30 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73  te3GetInt32(cons
74d40 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e  t char *zNum, in
74d50 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71  t *pValue){.  sq
74d60 6c 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30  lite_int64 v = 0
74d70 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20  ;.  int i, c;.  
74d80 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69  int neg = 0;.  i
74d90 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20  f( zNum[0]=='-' 
74da0 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a  ){.    neg = 1;.
74db0 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65      zNum++;.  }e
74dc0 6c 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d  lse if( zNum[0]=
74dd0 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d  ='+' ){.    zNum
74de0 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28  ++;.  }.  while(
74df0 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20   zNum[0]=='0' ) 
74e00 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d  zNum++;.  for(i=
74e10 30 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20  0; i<11 && (c = 
74e20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d  zNum[i] - '0')>=
74e30 30 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b  0 && c<=9; i++){
74e40 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20  .    v = v*10 + 
74e50 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  c;.  }..  /* The
74e60 20 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c   longest decimal
74e70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
74e80 6f 66 20 61 20 33 32 20 62 69 74 20 69 6e 74 65  of a 32 bit inte
74e90 67 65 72 20 69 73 20 31 30 20 64 69 67 69 74 73  ger is 10 digits
74ea0 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
74eb0 20 20 20 20 20 20 20 20 31 32 33 34 35 36 37 38          12345678
74ec0 39 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31  90.  **     2^31
74ed0 20 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20   -> 2147483648. 
74ee0 20 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29   */.  if( i>10 )
74ef0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
74f00 20 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e    }.  if( v-neg>
74f10 32 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20  2147483647 ){.  
74f20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
74f30 20 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20    if( neg ){.   
74f40 20 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a   v = -v;.  }.  *
74f50 70 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b  pValue = (int)v;
74f60 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
74f70 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d  /*.** Check to m
74f80 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65  ake sure we have
74f90 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f 69 6e   a valid db poin
74fa0 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74 20  ter.  This test 
74fb0 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 72  is not.** foolpr
74fc0 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65 73 20  oof but it does 
74fd0 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d 65 61  provide some mea
74fe0 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63 74 69  sure of protecti
74ff0 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 6d 69  on against.** mi
75000 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e 74 65  suse of the inte
75010 72 66 61 63 65 20 73 75 63 68 20 61 73 20 70 61  rface such as pa
75020 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f 69 6e  ssing in db poin
75030 74 65 72 73 20 74 68 61 74 20 61 72 65 0a 2a 2a  ters that are.**
75040 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68 20 68   NULL or which h
75050 61 76 65 20 62 65 65 6e 20 70 72 65 76 69 6f 75  ave been previou
75060 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49 66 20  sly closed.  If 
75070 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
75080 75 72 6e 73 0a 2a 2a 20 54 52 55 45 20 69 74 20  urns.** TRUE it 
75090 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64  means that the d
750a0 62 20 70 6f 69 6e 74 65 72 20 69 73 20 69 6e 76  b pointer is inv
750b0 61 6c 69 64 20 61 6e 64 20 73 68 6f 75 6c 64 20  alid and should 
750c0 6e 6f 74 20 62 65 0a 2a 2a 20 64 65 72 65 66 65  not be.** derefe
750d0 72 65 6e 63 65 64 20 66 6f 72 20 61 6e 79 20 72  renced for any r
750e0 65 61 73 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c  eason.  The call
750f0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  ing function sho
75100 75 6c 64 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51  uld invoke.** SQ
75110 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65  LITE_MISUSE imme
75120 64 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  diately..*/.SQLI
75130 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
75140 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
75150 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  k(sqlite3 *db){.
75160 20 20 69 6e 74 20 6d 61 67 69 63 3b 0a 20 20 69    int magic;.  i
75170 66 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72  f( db==0 ) retur
75180 6e 20 31 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64  n 1;.  magic = d
75190 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20  b->magic;.  if( 
751a0 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41  magic!=SQLITE_MA
751b0 47 49 43 5f 43 4c 4f 53 45 44 20 26 26 0a 20 20  GIC_CLOSED &&.  
751c0 20 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51         magic!=SQ
751d0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
751e0 26 26 0a 20 20 20 20 20 20 20 20 20 6d 61 67 69  &&.         magi
751f0 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c!=SQLITE_MAGIC_
75200 42 55 53 59 20 29 20 72 65 74 75 72 6e 20 31 3b  BUSY ) return 1;
75210 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
75220 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62  /*.** The variab
75230 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
75240 72 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73  r encoding is as
75250 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
75260 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20  KEY:.**         
75270 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 20  A = 0xxxxxxx    
75280 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61  7 bits of data a
75290 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a  nd one flag bit.
752a0 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 31  **         B = 1
752b0 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74  xxxxxxx    7 bit
752c0 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e  s of data and on
752d0 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20  e flag bit.**   
752e0 20 20 20 20 20 20 43 20 3d 20 78 78 78 78 78 78        C = xxxxxx
752f0 78 78 20 20 20 20 38 20 62 69 74 73 20 6f 66 20  xx    8 bits of 
75300 64 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69  data.**.**  7 bi
75310 74 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74  ts - A.** 14 bit
75320 73 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74  s - BA.** 21 bit
75330 73 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69  s - BBA.** 28 bi
75340 74 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20  ts - BBBA.** 35 
75350 62 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20  bits - BBBBA.** 
75360 34 32 20 62 69 74 73 20 2d 20 42 42 42 42 42 41  42 bits - BBBBBA
75370 0a 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42  .** 49 bits - BB
75380 42 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73  BBBBA.** 56 bits
75390 20 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36   - BBBBBBBA.** 6
753a0 34 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42  4 bits - BBBBBBB
753b0 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  BC.*/../*.** Wri
753c0 74 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 69  te a 64-bit vari
753d0 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
753e0 67 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74  ger to memory st
753f0 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a  arting at p[0]..
75400 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66  ** The length of
75410 20 64 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c   data write will
75420 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e   be between 1 an
75430 64 20 39 20 62 79 74 65 73 2e 20 20 54 68 65 20  d 9 bytes.  The 
75440 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74  number.** of byt
75450 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65  es written is re
75460 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20  turned..**.** A 
75470 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
75480 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73  integer consists
75490 20 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20   of the lower 7 
754a0 62 69 74 73 20 6f 66 20 65 61 63 68 20 62 79 74  bits of each byt
754b0 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74  e.** for all byt
754c0 65 73 20 74 68 61 74 20 68 61 76 65 20 74 68 65  es that have the
754d0 20 38 74 68 20 62 69 74 20 73 65 74 20 61 6e 64   8th bit set and
754e0 20 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20 74   one byte with t
754f0 68 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c  he 8th.** bit cl
75500 65 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66  ear.  Except, if
75510 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 39   we get to the 9
75520 74 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72  th byte, it stor
75530 65 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38  es the full.** 8
75540 20 62 69 74 73 20 61 6e 64 20 69 73 20 74 68 65   bits and is the
75550 20 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53   last byte..*/.S
75560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
75570 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69  t sqlite3PutVari
75580 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  nt(unsigned char
75590 20 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69   *p, u64 v){.  i
755a0 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38  nt i, j, n;.  u8
755b0 20 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20   buf[10];.  if( 
755c0 76 20 26 20 28 28 28 75 36 34 29 30 78 66 66 30  v & (((u64)0xff0
755d0 30 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20  00000)<<32) ){. 
755e0 20 20 20 70 5b 38 5d 20 3d 20 76 3b 0a 20 20 20     p[8] = v;.   
755f0 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 6f   v >>= 8;.    fo
75600 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d  r(i=7; i>=0; i--
75610 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20 3d 20  ){.      p[i] = 
75620 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38  (v & 0x7f) | 0x8
75630 30 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37  0;.      v >>= 7
75640 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
75650 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20  rn 9;.  }    .  
75660 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20  n = 0;.  do{.   
75670 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 76 20 26   buf[n++] = (v &
75680 20 30 78 37 66 29 20 7c 20 30 78 38 30 3b 0a 20   0x7f) | 0x80;. 
75690 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77     v >>= 7;.  }w
756a0 68 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20  hile( v!=0 );.  
756b0 62 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a  buf[0] &= 0x7f;.
756c0 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29    assert( n<=9 )
756d0 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e  ;.  for(i=0, j=n
756e0 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69  -1; j>=0; j--, i
756f0 2b 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20  ++){.    p[i] = 
75700 62 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65  buf[j];.  }.  re
75710 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
75720 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76   Read a 64-bit v
75730 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
75740 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f  nteger from memo
75750 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ry starting at p
75760 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74  [0]..** Return t
75770 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
75780 65 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 61  es read.  The va
75790 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  lue is stored in
757a0 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   *v..*/.SQLITE_P
757b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
757c0 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73  e3GetVarint(cons
757d0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
757e0 2a 70 2c 20 75 36 34 20 2a 76 29 7b 0a 20 20 75  *p, u64 *v){.  u
757f0 33 32 20 78 3b 0a 20 20 75 36 34 20 78 36 34 3b  32 x;.  u64 x64;
75800 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69  .  int n;.  unsi
75810 67 6e 65 64 20 63 68 61 72 20 63 3b 0a 20 20 69  gned char c;.  i
75820 66 28 20 28 28 63 20 3d 20 70 5b 30 5d 29 20 26  f( ((c = p[0]) &
75830 20 30 78 38 30 29 3d 3d 30 20 29 7b 0a 20 20 20   0x80)==0 ){.   
75840 20 2a 76 20 3d 20 63 3b 0a 20 20 20 20 72 65 74   *v = c;.    ret
75850 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 78 20 3d  urn 1;.  }.  x =
75860 20 63 20 26 20 30 78 37 66 3b 0a 20 20 69 66 28   c & 0x7f;.  if(
75870 20 28 28 63 20 3d 20 70 5b 31 5d 29 20 26 20 30   ((c = p[1]) & 0
75880 78 38 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  x80)==0 ){.    *
75890 76 20 3d 20 28 78 3c 3c 37 29 20 7c 20 63 3b 0a  v = (x<<7) | c;.
758a0 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
758b0 7d 0a 20 20 78 20 3d 20 28 78 3c 3c 37 29 20 7c  }.  x = (x<<7) |
758c0 20 28 63 26 30 78 37 66 29 3b 0a 20 20 69 66 28   (c&0x7f);.  if(
758d0 20 28 28 63 20 3d 20 70 5b 32 5d 29 20 26 20 30   ((c = p[2]) & 0
758e0 78 38 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  x80)==0 ){.    *
758f0 76 20 3d 20 28 78 3c 3c 37 29 20 7c 20 63 3b 0a  v = (x<<7) | c;.
75900 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20      return 3;.  
75910 7d 0a 20 20 78 20 3d 20 28 78 3c 3c 37 29 20 7c  }.  x = (x<<7) |
75920 20 28 63 26 30 78 37 66 29 3b 0a 20 20 69 66 28   (c&0x7f);.  if(
75930 20 28 28 63 20 3d 20 70 5b 33 5d 29 20 26 20 30   ((c = p[3]) & 0
75940 78 38 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  x80)==0 ){.    *
75950 76 20 3d 20 28 78 3c 3c 37 29 20 7c 20 63 3b 0a  v = (x<<7) | c;.
75960 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20      return 4;.  
75970 7d 0a 20 20 78 36 34 20 3d 20 28 78 3c 3c 37 29  }.  x64 = (x<<7)
75980 20 7c 20 28 63 26 30 78 37 66 29 3b 0a 20 20 6e   | (c&0x7f);.  n
75990 20 3d 20 34 3b 0a 20 20 64 6f 7b 0a 20 20 20 20   = 4;.  do{.    
759a0 63 20 3d 20 70 5b 6e 2b 2b 5d 3b 0a 20 20 20 20  c = p[n++];.    
759b0 69 66 28 20 6e 3d 3d 39 20 29 7b 0a 20 20 20 20  if( n==9 ){.    
759c0 20 20 78 36 34 20 3d 20 28 78 36 34 3c 3c 38 29    x64 = (x64<<8)
759d0 20 7c 20 63 3b 0a 20 20 20 20 20 20 62 72 65 61   | c;.      brea
759e0 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 78 36 34  k;.    }.    x64
759f0 20 3d 20 28 78 36 34 3c 3c 37 29 20 7c 20 28 63   = (x64<<7) | (c
75a00 26 30 78 37 66 29 3b 0a 20 20 7d 77 68 69 6c 65  &0x7f);.  }while
75a10 28 20 28 63 20 26 20 30 78 38 30 29 21 3d 30 20  ( (c & 0x80)!=0 
75a20 29 3b 0a 20 20 2a 76 20 3d 20 78 36 34 3b 0a 20  );.  *v = x64;. 
75a30 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a   return n;.}../*
75a40 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69  .** Read a 32-bi
75a50 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
75a60 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d  h integer from m
75a70 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
75a80 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72  t p[0]..** Retur
75a90 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
75aa0 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65  bytes read.  The
75ab0 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64   value is stored
75ac0 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54   in *v..*/.SQLIT
75ad0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
75ae0 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
75af0 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
75b00 63 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29  char *p, u32 *v)
75b10 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20 69 6e 74  {.  u32 x;.  int
75b20 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   n;.  unsigned c
75b30 68 61 72 20 63 3b 0a 20 20 69 66 28 20 28 28 73  har c;.  if( ((s
75b40 69 67 6e 65 64 20 63 68 61 72 2a 29 70 29 5b 30  igned char*)p)[0
75b50 5d 3e 3d 30 20 29 7b 0a 20 20 20 20 2a 76 20 3d  ]>=0 ){.    *v =
75b60 20 70 5b 30 5d 3b 0a 20 20 20 20 72 65 74 75 72   p[0];.    retur
75b70 6e 20 31 3b 0a 20 20 7d 0a 20 20 78 20 3d 20 70  n 1;.  }.  x = p
75b80 5b 30 5d 20 26 20 30 78 37 66 3b 0a 20 20 69 66  [0] & 0x7f;.  if
75b90 28 20 28 28 73 69 67 6e 65 64 20 63 68 61 72 2a  ( ((signed char*
75ba0 29 70 29 5b 31 5d 3e 3d 30 20 29 7b 0a 20 20 20  )p)[1]>=0 ){.   
75bb0 20 2a 76 20 3d 20 28 78 3c 3c 37 29 20 7c 20 70   *v = (x<<7) | p
75bc0 5b 31 5d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  [1];.    return 
75bd0 32 3b 0a 20 20 7d 0a 20 20 78 20 3d 20 28 78 3c  2;.  }.  x = (x<
75be0 3c 37 29 20 7c 20 28 70 5b 31 5d 20 26 20 30 78  <7) | (p[1] & 0x
75bf0 37 66 29 3b 0a 20 20 6e 20 3d 20 32 3b 0a 20 20  7f);.  n = 2;.  
75c00 64 6f 7b 0a 20 20 20 20 78 20 3d 20 28 78 3c 3c  do{.    x = (x<<
75c10 37 29 20 7c 20 28 28 63 20 3d 20 70 5b 6e 2b 2b  7) | ((c = p[n++
75c20 5d 29 26 30 78 37 66 29 3b 0a 20 20 7d 77 68 69  ])&0x7f);.  }whi
75c30 6c 65 28 20 28 63 20 26 20 30 78 38 30 29 21 3d  le( (c & 0x80)!=
75c40 30 20 26 26 20 6e 3c 39 20 29 3b 0a 20 20 2a 76  0 && n<9 );.  *v
75c50 20 3d 20 78 3b 0a 20 20 72 65 74 75 72 6e 20 6e   = x;.  return n
75c60 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
75c70 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
75c80 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20  bytes that will 
75c90 62 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f  be needed to sto
75ca0 72 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20  re the given.** 
75cb0 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a  64-bit integer..
75cc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
75cd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72  E int sqlite3Var
75ce0 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20  intLen(u64 v){. 
75cf0 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f   int i = 0;.  do
75d00 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76  {.    i++;.    v
75d10 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65   >>= 7;.  }while
75d20 28 20 76 21 3d 30 20 26 26 20 69 3c 39 20 29 3b  ( v!=0 && i<9 );
75d30 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a  .  return i;.}..
75d40 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77  ./*.** Read or w
75d50 72 69 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65  rite a four-byte
75d60 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
75d70 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51  ger value..*/.SQ
75d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
75d90 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
75da0 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20  (const u8 *p){. 
75db0 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32   return (p[0]<<2
75dc0 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20  4) | (p[1]<<16) 
75dd0 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b  | (p[2]<<8) | p[
75de0 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  3];.}.SQLITE_PRI
75df0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
75e00 33 50 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e  3Put4byte(unsign
75e10 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20  ed char *p, u32 
75e20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 76 3e 3e  v){.  p[0] = v>>
75e30 32 34 3b 0a 20 20 70 5b 31 5d 20 3d 20 76 3e 3e  24;.  p[1] = v>>
75e40 31 36 3b 0a 20 20 70 5b 32 5d 20 3d 20 76 3e 3e  16;.  p[2] = v>>
75e50 38 3b 0a 20 20 70 5b 33 5d 20 3d 20 76 3b 0a 7d  8;.  p[3] = v;.}
75e60 0a 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ....#if !defined
75e70 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f  (SQLITE_OMIT_BLO
75e80 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65  B_LITERAL) || de
75e90 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53  fined(SQLITE_HAS
75ea0 5f 43 4f 44 45 43 29 20 5c 0a 20 20 20 20 7c 7c  _CODEC) \.    ||
75eb0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
75ec0 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e  TEST)./*.** Tran
75ed0 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 62  slate a single b
75ee0 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f 20  yte of Hex into 
75ef0 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73  an integer..*/.s
75f00 74 61 74 69 63 20 69 6e 74 20 68 65 78 54 6f 49  tatic int hexToI
75f10 6e 74 28 69 6e 74 20 68 29 7b 0a 20 20 69 66 28  nt(int h){.  if(
75f20 20 68 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 39   h>='0' && h<='9
75f30 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ' ){.    return 
75f40 68 20 2d 20 27 30 27 3b 0a 20 20 7d 65 6c 73 65  h - '0';.  }else
75f50 20 69 66 28 20 68 3e 3d 27 61 27 20 26 26 20 68   if( h>='a' && h
75f60 3c 3d 27 66 27 20 29 7b 0a 20 20 20 20 72 65 74  <='f' ){.    ret
75f70 75 72 6e 20 68 20 2d 20 27 61 27 20 2b 20 31 30  urn h - 'a' + 10
75f80 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
75f90 73 73 65 72 74 28 20 68 3e 3d 27 41 27 20 26 26  ssert( h>='A' &&
75fa0 20 68 3c 3d 27 46 27 20 29 3b 0a 20 20 20 20 72   h<='F' );.    r
75fb0 65 74 75 72 6e 20 68 20 2d 20 27 41 27 20 2b 20  eturn h - 'A' + 
75fc0 31 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  10;.  }.}.#endif
75fd0 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
75fe0 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c  _BLOB_LITERAL ||
75ff0 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
76000 43 20 7c 7c 20 53 51 4c 49 54 45 5f 54 45 53 54  C || SQLITE_TEST
76010 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65   */..#if !define
76020 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c  d(SQLITE_OMIT_BL
76030 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64  OB_LITERAL) || d
76040 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41  efined(SQLITE_HA
76050 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43  S_CODEC)./*.** C
76060 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69  onvert a BLOB li
76070 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72  teral of the for
76080 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 69 6e  m "x'hhhhhh'" in
76090 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a  to its binary.**
760a0 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20   value.  Return 
760b0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73  a pointer to its
760c0 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20   binary value.  
760d0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
760e0 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75  e.** binary valu
760f0 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69  e has been obtai
76100 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20  ned from malloc 
76110 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65  and must be free
76120 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c  d by.** the call
76130 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  ing routine..*/.
76140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
76150 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54  oid *sqlite3HexT
76160 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64  oBlob(sqlite3 *d
76170 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
76180 29 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62  ){.  char *zBlob
76190 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
761a0 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a   n = strlen(z);.
761b0 20 20 69 66 28 20 6e 25 32 20 29 20 72 65 74 75    if( n%2 ) retu
761c0 72 6e 20 30 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d  rn 0;..  zBlob =
761d0 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
761e0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
761f0 6e 2f 32 29 3b 0a 20 20 69 66 28 20 7a 42 6c 6f  n/2);.  if( zBlo
76200 62 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  b ){.    for(i=0
76210 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20  ; i<n; i+=2){.  
76220 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d      zBlob[i/2] =
76230 20 28 68 65 78 54 6f 49 6e 74 28 7a 5b 69 5d 29   (hexToInt(z[i])
76240 3c 3c 34 29 20 7c 20 68 65 78 54 6f 49 6e 74 28  <<4) | hexToInt(
76250 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  z[i+1]);.    }. 
76260 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f   }.  return zBlo
76270 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  b;.}.#endif /* !
76280 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
76290 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49  _LITERAL || SQLI
762a0 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a  TE_HAS_CODEC */.
762b0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
762c0 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 20  he sqlite.magic 
762d0 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49  from SQLITE_MAGI
762e0 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49 54 45  C_OPEN to SQLITE
762f0 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20  _MAGIC_BUSY..** 
76300 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
76310 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68  (non-zero) if th
76320 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20  e magic was not 
76330 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
76340 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72  N.** when this r
76350 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
76360 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
76370 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77  tine is called w
76380 68 65 6e 20 65 6e 74 65 72 69 6e 67 20 61 6e 20  hen entering an 
76390 53 51 4c 69 74 65 20 41 50 49 2e 20 20 54 68 65  SQLite API.  The
763a0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
763b0 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69  EN.** value indi
763c0 63 61 74 65 73 20 74 68 61 74 20 74 68 65 20 64  cates that the d
763d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
763e0 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74  on passed into t
763f0 68 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65  he API is.** ope
76400 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 62 65 69  n and is not bei
76410 6e 67 20 75 73 65 64 20 62 79 20 61 6e 6f 74 68  ng used by anoth
76420 65 72 20 74 68 72 65 61 64 2e 20 20 42 79 20 63  er thread.  By c
76430 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75  hanging the valu
76440 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d  e.** to SQLITE_M
76450 41 47 49 43 5f 42 55 53 59 20 77 65 20 69 6e 64  AGIC_BUSY we ind
76460 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 63  icate that the c
76470 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
76480 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53  use..** sqlite3S
76490 61 66 65 74 79 4f 66 66 28 29 20 62 65 6c 6f 77  afetyOff() below
764a0 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 68 65   will change the
764b0 20 76 61 6c 75 65 20 62 61 63 6b 20 74 6f 20 53   value back to S
764c0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
764d0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 50 49  .** when the API
764e0 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54   exits. .**.** T
764f0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
76500 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 74 65   attempt to dete
76510 63 74 20 69 66 20 74 77 6f 20 74 68 72 65 61 64  ct if two thread
76520 73 20 75 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d  s use the.** sam
76530 65 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65  e sqlite* pointe
76540 72 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  r at the same ti
76550 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 20  me.  There is a 
76560 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69  race .** conditi
76570 6f 6e 20 73 6f 20 69 74 20 69 73 20 70 6f 73 73  on so it is poss
76580 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 65 72  ible that the er
76590 72 6f 72 20 69 73 20 6e 6f 74 20 64 65 74 65 63  ror is not detec
765a0 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61  ted..** But usua
765b0 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  lly the problem 
765c0 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20 20 54  will be seen.  T
765d0 68 65 20 72 65 73 75 6c 74 20 77 69 6c 6c 20 62  he result will b
765e0 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68  e an.** error wh
765f0 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20  ich can be used 
76600 74 6f 20 64 65 62 75 67 20 74 68 65 20 61 70 70  to debug the app
76610 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73  lication that is
76620 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69 74 65  .** using SQLite
76630 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a   incorrectly..**
76640 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 30 32 3a  .** Ticket #202:
76650 20 20 49 66 20 64 62 2d 3e 6d 61 67 69 63 20 69    If db->magic i
76660 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6f 70  s not a valid op
76670 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65 20 63  en value, take c
76680 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f  are not.** to mo
76690 64 69 66 79 20 74 68 65 20 64 62 20 73 74 72 75  dify the db stru
766a0 63 74 75 72 65 20 61 74 20 61 6c 6c 2e 20 20 49  cture at all.  I
766b0 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20  t could be that 
766c0 64 62 20 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a  db is a stale.**
766d0 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74   pointer.  In ot
766e0 68 65 72 20 77 6f 72 64 73 2c 20 69 74 20 63 6f  her words, it co
766f0 75 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 72  uld be that ther
76700 65 20 68 61 73 20 62 65 65 6e 20 61 20 70 72 69  e has been a pri
76710 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71  or.** call to sq
76720 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 20  lite3_close(db) 
76730 61 6e 64 20 64 62 20 68 61 73 20 62 65 65 6e 20  and db has been 
76740 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e  deallocated.  An
76750 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77  d we do.** not w
76760 61 6e 74 20 74 6f 20 77 72 69 74 65 20 69 6e 74  ant to write int
76770 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65  o deallocated me
76780 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mory..*/.SQLITE_
76790 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
767a0 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69  te3SafetyOn(sqli
767b0 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20  te3 *db){.  if( 
767c0 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
767d0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a  E_MAGIC_OPEN ){.
767e0 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20      db->magic = 
767f0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
76800 59 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  Y;.    return 0;
76810 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d  .  }else if( db-
76820 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d  >magic==SQLITE_M
76830 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20  AGIC_BUSY ){.   
76840 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
76850 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b  ITE_MAGIC_ERROR;
76860 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e  .    db->u1.isIn
76870 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20  terrupted = 1;. 
76880 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
76890 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
768a0 68 65 20 6d 61 67 69 63 20 66 72 6f 6d 20 53 51  he magic from SQ
768b0 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
768c0 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  to SQLITE_MAGIC_
768d0 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  OPEN..** Return 
768e0 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65  an error (non-ze
768f0 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 69 63  ro) if the magic
76900 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f   was not SQLITE_
76910 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a 20 77 68  MAGIC_BUSY.** wh
76920 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
76930 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51  is called..*/.SQ
76940 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
76950 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
76960 66 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  f(sqlite3 *db){.
76970 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d    if( db->magic=
76980 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55  =SQLITE_MAGIC_BU
76990 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  SY ){.    db->ma
769a0 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
769b0 49 43 5f 4f 50 45 4e 3b 0a 20 20 20 20 72 65 74  IC_OPEN;.    ret
769c0 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 7b  urn 0;.  }else {
769d0 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  .    db->magic =
769e0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52   SQLITE_MAGIC_ER
769f0 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e  ROR;.    db->u1.
76a00 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20  isInterrupted = 
76a10 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  1;.    return 1;
76a20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  .  }.}../*******
76a30 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75  ******* End of u
76a40 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  til.c **********
76a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76a70 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
76a80 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
76a90 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a  le hash.c ******
76aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76ac0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
76ad0 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a  01 September 22.
76ae0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
76af0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
76b00 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
76b10 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
76b20 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
76b30 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
76b40 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
76b50 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
76b60 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
76b70 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
76b80 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
76b90 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
76ba0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
76bb0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
76bc0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
76bd0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
76be0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
76bf0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
76c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
76c40 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d  * This is the im
76c50 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
76c60 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62  generic hash-tab
76c70 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53  les.** used in S
76c80 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  QLite..**.** $Id
76c90 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 32 34 20  : hash.c,v 1.24 
76ca0 32 30 30 37 2f 30 39 2f 30 34 20 31 34 3a 33 31  2007/09/04 14:31
76cb0 3a 34 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :47 danielk1977 
76cc0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 54 75 72  Exp $.*/../* Tur
76cd0 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e  n bulk memory in
76ce0 74 6f 20 61 20 68 61 73 68 20 74 61 62 6c 65 20  to a hash table 
76cf0 6f 62 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61  object by initia
76d00 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  lizing the.** fi
76d10 65 6c 64 73 20 6f 66 20 74 68 65 20 48 61 73 68  elds of the Hash
76d20 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
76d30 2a 20 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f  * "pNew" is a po
76d40 69 6e 74 65 72 20 74 6f 20 74 68 65 20 68 61 73  inter to the has
76d50 68 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20  h table that is 
76d60 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  to be initialize
76d70 64 2e 0a 2a 2a 20 6b 65 79 43 6c 61 73 73 20 69  d..** keyClass i
76d80 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e  s one of the con
76d90 73 74 61 6e 74 73 20 53 51 4c 49 54 45 5f 48 41  stants SQLITE_HA
76da0 53 48 5f 49 4e 54 2c 20 53 51 4c 49 54 45 5f 48  SH_INT, SQLITE_H
76db0 41 53 48 5f 50 4f 49 4e 54 45 52 2c 0a 2a 2a 20  ASH_POINTER,.** 
76dc0 53 51 4c 49 54 45 5f 48 41 53 48 5f 42 49 4e 41  SQLITE_HASH_BINA
76dd0 52 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f 48 41  RY, or SQLITE_HA
76de0 53 48 5f 53 54 52 49 4e 47 2e 20 20 54 68 65 20  SH_STRING.  The 
76df0 76 61 6c 75 65 20 6f 66 20 6b 65 79 43 6c 61 73  value of keyClas
76e00 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 73  s .** determines
76e10 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6b 65   what kind of ke
76e20 79 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  y the hash table
76e30 20 77 69 6c 6c 20 75 73 65 2e 20 20 22 63 6f 70   will use.  "cop
76e40 79 4b 65 79 22 20 69 73 0a 2a 2a 20 74 72 75 65  yKey" is.** true
76e50 20 69 66 20 74 68 65 20 68 61 73 68 20 74 61 62   if the hash tab
76e60 6c 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69  le should make i
76e70 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63  ts own private c
76e80 6f 70 79 20 6f 66 20 6b 65 79 73 20 61 6e 64 0a  opy of keys and.
76e90 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 73  ** false if it s
76ea0 68 6f 75 6c 64 20 6a 75 73 74 20 75 73 65 20 74  hould just use t
76eb0 68 65 20 73 75 70 70 6c 69 65 64 20 70 6f 69 6e  he supplied poin
76ec0 74 65 72 2e 20 20 43 6f 70 79 4b 65 79 20 6f 6e  ter.  CopyKey on
76ed0 6c 79 20 6d 61 6b 65 73 0a 2a 2a 20 73 65 6e 73  ly makes.** sens
76ee0 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 48 41 53  e for SQLITE_HAS
76ef0 48 5f 53 54 52 49 4e 47 20 61 6e 64 20 53 51 4c  H_STRING and SQL
76f00 49 54 45 5f 48 41 53 48 5f 42 49 4e 41 52 59 20  ITE_HASH_BINARY 
76f10 61 6e 64 20 69 73 20 69 67 6e 6f 72 65 64 0a 2a  and is ignored.*
76f20 2a 20 66 6f 72 20 6f 74 68 65 72 20 6b 65 79 20  * for other key 
76f30 63 6c 61 73 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49  classes..*/.SQLI
76f40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
76f50 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
76f60 48 61 73 68 20 2a 70 4e 65 77 2c 20 69 6e 74 20  Hash *pNew, int 
76f70 6b 65 79 43 6c 61 73 73 2c 20 69 6e 74 20 63 6f  keyClass, int co
76f80 70 79 4b 65 79 29 7b 0a 20 20 61 73 73 65 72 74  pyKey){.  assert
76f90 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20 61  ( pNew!=0 );.  a
76fa0 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3e  ssert( keyClass>
76fb0 3d 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52  =SQLITE_HASH_STR
76fc0 49 4e 47 20 26 26 20 6b 65 79 43 6c 61 73 73 3c  ING && keyClass<
76fd0 3d 53 51 4c 49 54 45 5f 48 41 53 48 5f 42 49 4e  =SQLITE_HASH_BIN
76fe0 41 52 59 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 6b  ARY );.  pNew->k
76ff0 65 79 43 6c 61 73 73 20 3d 20 6b 65 79 43 6c 61  eyClass = keyCla
77000 73 73 3b 0a 23 69 66 20 30 0a 20 20 69 66 28 20  ss;.#if 0.  if( 
77010 6b 65 79 43 6c 61 73 73 3d 3d 53 51 4c 49 54 45  keyClass==SQLITE
77020 5f 48 41 53 48 5f 50 4f 49 4e 54 45 52 20 7c 7c  _HASH_POINTER ||
77030 20 6b 65 79 43 6c 61 73 73 3d 3d 53 51 4c 49 54   keyClass==SQLIT
77040 45 5f 48 41 53 48 5f 49 4e 54 20 29 20 63 6f 70  E_HASH_INT ) cop
77050 79 4b 65 79 20 3d 20 30 3b 0a 23 65 6e 64 69 66  yKey = 0;.#endif
77060 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 4b 65 79  .  pNew->copyKey
77070 20 3d 20 63 6f 70 79 4b 65 79 3b 0a 20 20 70 4e   = copyKey;.  pN
77080 65 77 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20  ew->first = 0;. 
77090 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d 20 30   pNew->count = 0
770a0 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 73 69 7a 65  ;.  pNew->htsize
770b0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74   = 0;.  pNew->ht
770c0 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f   = 0;.}../* Remo
770d0 76 65 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66  ve all entries f
770e0 72 6f 6d 20 61 20 68 61 73 68 20 74 61 62 6c 65  rom a hash table
770f0 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d  .  Reclaim all m
77100 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74  emory..** Call t
77110 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 64  his routine to d
77120 65 6c 65 74 65 20 61 20 68 61 73 68 20 74 61 62  elete a hash tab
77130 6c 65 20 6f 72 20 74 6f 20 72 65 73 65 74 20 61  le or to reset a
77140 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 74   hash table.** t
77150 6f 20 74 68 65 20 65 6d 70 74 79 20 73 74 61 74  o the empty stat
77160 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
77170 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
77180 33 48 61 73 68 43 6c 65 61 72 28 48 61 73 68 20  3HashClear(Hash 
77190 2a 70 48 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  *pH){.  HashElem
771a0 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20   *elem;         
771b0 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
771c0 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73  ver all elements
771d0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
771e0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d  ..  assert( pH!=
771f0 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 70 48  0 );.  elem = pH
77200 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48 2d 3e 66  ->first;.  pH->f
77210 69 72 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20  irst = 0;.  if( 
77220 70 48 2d 3e 68 74 20 29 20 73 71 6c 69 74 65 33  pH->ht ) sqlite3
77230 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20  _free(pH->ht);. 
77240 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70   pH->ht = 0;.  p
77250 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20  H->htsize = 0;. 
77260 20 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a   while( elem ){.
77270 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65      HashElem *ne
77280 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e  xt_elem = elem->
77290 6e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48  next;.    if( pH
772a0 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65  ->copyKey && ele
772b0 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20  m->pKey ){.     
772c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c   sqlite3_free(el
772d0 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 7d  em->pKey);.    }
772e0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
772f0 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65  e(elem);.    ele
77300 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20  m = next_elem;. 
77310 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d   }.  pH->count =
77320 20 30 3b 0a 7d 0a 0a 23 69 66 20 30 20 2f 2a 20   0;.}..#if 0 /* 
77330 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 0a 2a  NOT USED */./*.*
77340 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61  * Hash and compa
77350 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20  rison functions 
77360 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73  when the mode is
77370 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 49 4e 54   SQLITE_HASH_INT
77380 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
77390 6e 74 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69  ntHash(const voi
773a0 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
773b0 79 29 7b 0a 20 20 72 65 74 75 72 6e 20 6e 4b 65  y){.  return nKe
773c0 79 20 5e 20 28 6e 4b 65 79 3c 3c 38 29 20 5e 20  y ^ (nKey<<8) ^ 
773d0 28 6e 4b 65 79 3e 3e 38 29 3b 0a 7d 0a 73 74 61  (nKey>>8);.}.sta
773e0 74 69 63 20 69 6e 74 20 69 6e 74 43 6f 6d 70 61  tic int intCompa
773f0 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  re(const void *p
77400 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f  Key1, int n1, co
77410 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c  nst void *pKey2,
77420 20 69 6e 74 20 6e 32 29 7b 0a 20 20 72 65 74 75   int n2){.  retu
77430 72 6e 20 6e 32 20 2d 20 6e 31 3b 0a 7d 0a 23 65  rn n2 - n1;.}.#e
77440 6e 64 69 66 0a 0a 23 69 66 20 30 20 2f 2a 20 4e  ndif..#if 0 /* N
77450 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 0a 2a 2a  OT USED */./*.**
77460 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72   Hash and compar
77470 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77  ison functions w
77480 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20  hen the mode is 
77490 53 51 4c 49 54 45 5f 48 41 53 48 5f 50 4f 49 4e  SQLITE_HASH_POIN
774a0 54 45 52 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  TER.*/.static in
774b0 74 20 70 74 72 48 61 73 68 28 63 6f 6e 73 74 20  t ptrHash(const 
774c0 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
774d0 6e 4b 65 79 29 7b 0a 20 20 75 70 74 72 20 78 20  nKey){.  uptr x 
774e0 3d 20 41 64 64 72 28 70 4b 65 79 29 3b 0a 20 20  = Addr(pKey);.  
774f0 72 65 74 75 72 6e 20 78 20 5e 20 28 78 3c 3c 38  return x ^ (x<<8
77500 29 20 5e 20 28 78 3e 3e 38 29 3b 0a 7d 0a 73 74  ) ^ (x>>8);.}.st
77510 61 74 69 63 20 69 6e 74 20 70 74 72 43 6f 6d 70  atic int ptrComp
77520 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  are(const void *
77530 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63  pKey1, int n1, c
77540 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
77550 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28  , int n2){.  if(
77560 20 70 4b 65 79 31 3d 3d 70 4b 65 79 32 20 29 20   pKey1==pKey2 ) 
77570 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
77580 70 4b 65 79 31 3c 70 4b 65 79 32 20 29 20 72 65  pKey1<pKey2 ) re
77590 74 75 72 6e 20 2d 31 3b 0a 20 20 72 65 74 75 72  turn -1;.  retur
775a0 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  n 1;.}.#endif../
775b0 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f  *.** Hash and co
775c0 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
775d0 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65  ns when the mode
775e0 20 69 73 20 53 51 4c 49 54 45 5f 48 41 53 48 5f   is SQLITE_HASH_
775f0 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 63  STRING.*/.static
77600 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f 6e   int strHash(con
77610 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
77620 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e 73  nt nKey){.  cons
77630 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e  t char *z = (con
77640 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a  st char *)pKey;.
77650 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 69    int h = 0;.  i
77660 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b 65  f( nKey<=0 ) nKe
77670 79 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  y = strlen(z);. 
77680 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30   while( nKey > 0
77690 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c    ){.    h = (h<
776a0 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65  <3) ^ h ^ sqlite
776b0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75  3UpperToLower[(u
776c0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b  nsigned char)*z+
776d0 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a  +];.    nKey--;.
776e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20 26    }.  return h &
776f0 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73   0x7fffffff;.}.s
77700 74 61 74 69 63 20 69 6e 74 20 73 74 72 43 6f 6d  tatic int strCom
77710 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20  pare(const void 
77720 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20  *pKey1, int n1, 
77730 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
77740 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66  2, int n2){.  if
77750 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72  ( n1!=n2 ) retur
77760 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  n 1;.  return sq
77770 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63  lite3StrNICmp((c
77780 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79 31  onst char*)pKey1
77790 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b  ,(const char*)pK
777a0 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ey2,n1);.}../*.*
777b0 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61  * Hash and compa
777c0 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20  rison functions 
777d0 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73  when the mode is
777e0 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 42 49 4e   SQLITE_HASH_BIN
777f0 41 52 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ARY.*/.static in
77800 74 20 62 69 6e 48 61 73 68 28 63 6f 6e 73 74 20  t binHash(const 
77810 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
77820 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 20 3d  nKey){.  int h =
77830 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   0;.  const char
77840 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61   *z = (const cha
77850 72 20 2a 29 70 4b 65 79 3b 0a 20 20 77 68 69 6c  r *)pKey;.  whil
77860 65 28 20 6e 4b 65 79 2d 2d 20 3e 20 30 20 29 7b  e( nKey-- > 0 ){
77870 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20  .    h = (h<<3) 
77880 5e 20 68 20 5e 20 2a 28 7a 2b 2b 29 3b 0a 20 20  ^ h ^ *(z++);.  
77890 7d 0a 20 20 72 65 74 75 72 6e 20 68 20 26 20 30  }.  return h & 0
778a0 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61  x7fffffff;.}.sta
778b0 74 69 63 20 69 6e 74 20 62 69 6e 43 6f 6d 70 61  tic int binCompa
778c0 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  re(const void *p
778d0 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f  Key1, int n1, co
778e0 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c  nst void *pKey2,
778f0 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28 20   int n2){.  if( 
77900 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e 20  n1!=n2 ) return 
77910 31 3b 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d 63  1;.  return memc
77920 6d 70 28 70 4b 65 79 31 2c 70 4b 65 79 32 2c 6e  mp(pKey1,pKey2,n
77930 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  1);.}../*.** Ret
77940 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
77950 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
77960 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 67   hash function g
77970 69 76 65 6e 20 74 68 65 20 6b 65 79 20 63 6c 61  iven the key cla
77980 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20  ss..**.** The C 
77990 73 79 6e 74 61 78 20 69 6e 20 74 68 69 73 20 66  syntax in this f
779a0 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
779b0 6f 6e 20 6d 61 79 20 62 65 20 75 6e 66 61 6d 69  on may be unfami
779c0 6c 61 72 20 74 6f 20 73 6f 6d 65 20 0a 2a 2a 20  lar to some .** 
779d0 70 72 6f 67 72 61 6d 6d 65 72 73 2c 20 73 6f 20  programmers, so 
779e0 77 65 20 70 72 6f 76 69 64 65 20 74 68 65 20 66  we provide the f
779f0 6f 6c 6c 6f 77 69 6e 67 20 61 64 64 69 74 69 6f  ollowing additio
77a00 6e 61 6c 20 65 78 70 6c 61 6e 61 74 69 6f 6e 3a  nal explanation:
77a10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20  .**.** The name 
77a20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  of the function 
77a30 69 73 20 22 68 61 73 68 46 75 6e 63 74 69 6f 6e  is "hashFunction
77a40 22 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  ".  The function
77a50 20 74 61 6b 65 73 20 61 0a 2a 2a 20 73 69 6e 67   takes a.** sing
77a60 6c 65 20 70 61 72 61 6d 65 74 65 72 20 22 6b 65  le parameter "ke
77a70 79 43 6c 61 73 73 22 2e 20 20 54 68 65 20 72 65  yClass".  The re
77a80 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 68 61  turn value of ha
77a90 73 68 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20  shFunction().** 
77aa0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
77ab0 61 6e 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e  another function
77ac0 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c  .  Specifically,
77ad0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
77ae0 65 0a 2a 2a 20 6f 66 20 68 61 73 68 46 75 6e 63  e.** of hashFunc
77af0 74 69 6f 6e 28 29 20 69 73 20 61 20 70 6f 69 6e  tion() is a poin
77b00 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
77b10 6e 20 74 68 61 74 20 74 61 6b 65 73 20 74 77 6f  n that takes two
77b20 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 77   parameters.** w
77b30 69 74 68 20 74 79 70 65 73 20 22 63 6f 6e 73 74  ith types "const
77b40 20 76 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e 74   void*" and "int
77b50 22 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e  " and returns an
77b60 20 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74 69   "int"..*/.stati
77b70 63 20 69 6e 74 20 28 2a 68 61 73 68 46 75 6e 63  c int (*hashFunc
77b80 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73  tion(int keyClas
77b90 73 29 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  s))(const void*,
77ba0 69 6e 74 29 7b 0a 23 69 66 20 30 20 20 2f 2a 20  int){.#if 0  /* 
77bb0 48 41 53 48 5f 49 4e 54 20 61 6e 64 20 48 41 53  HASH_INT and HAS
77bc0 48 5f 50 4f 49 4e 54 45 52 20 61 72 65 20 6e 65  H_POINTER are ne
77bd0 76 65 72 20 75 73 65 64 20 2a 2f 0a 20 20 73 77  ver used */.  sw
77be0 69 74 63 68 28 20 6b 65 79 43 6c 61 73 73 20 29  itch( keyClass )
77bf0 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
77c00 45 5f 48 41 53 48 5f 49 4e 54 3a 20 20 20 20 20  E_HASH_INT:     
77c10 72 65 74 75 72 6e 20 26 69 6e 74 48 61 73 68 3b  return &intHash;
77c20 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
77c30 5f 48 41 53 48 5f 50 4f 49 4e 54 45 52 3a 20 72  _HASH_POINTER: r
77c40 65 74 75 72 6e 20 26 70 74 72 48 61 73 68 3b 0a  eturn &ptrHash;.
77c50 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
77c60 48 41 53 48 5f 53 54 52 49 4e 47 3a 20 20 72 65  HASH_STRING:  re
77c70 74 75 72 6e 20 26 73 74 72 48 61 73 68 3b 0a 20  turn &strHash;. 
77c80 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 48     case SQLITE_H
77c90 41 53 48 5f 42 49 4e 41 52 59 3a 20 20 72 65 74  ASH_BINARY:  ret
77ca0 75 72 6e 20 26 62 69 6e 48 61 73 68 3b 3b 0a 20  urn &binHash;;. 
77cb0 20 20 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61     default: brea
77cc0 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  k;.  }.  return 
77cd0 30 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 6b  0;.#else.  if( k
77ce0 65 79 43 6c 61 73 73 3d 3d 53 51 4c 49 54 45 5f  eyClass==SQLITE_
77cf0 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20  HASH_STRING ){. 
77d00 20 20 20 72 65 74 75 72 6e 20 26 73 74 72 48 61     return &strHa
77d10 73 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  sh;.  }else{.   
77d20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73   assert( keyClas
77d30 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53 48 5f 42  s==SQLITE_HASH_B
77d40 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65 74  INARY );.    ret
77d50 75 72 6e 20 26 62 69 6e 48 61 73 68 3b 0a 20 20  urn &binHash;.  
77d60 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
77d70 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
77d80 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  er to the approp
77d90 72 69 61 74 65 20 68 61 73 68 20 66 75 6e 63 74  riate hash funct
77da0 69 6f 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65  ion given the ke
77db0 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46  y class..**.** F
77dc0 6f 72 20 68 65 6c 70 20 69 6e 20 69 6e 74 65 72  or help in inter
77dd0 70 72 65 74 65 64 20 74 68 65 20 6f 62 73 63 75  preted the obscu
77de0 72 65 20 43 20 63 6f 64 65 20 69 6e 20 74 68 65  re C code in the
77df0 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
77e00 74 69 6f 6e 2c 0a 2a 2a 20 73 65 65 20 74 68 65  tion,.** see the
77e10 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20   header comment 
77e20 6f 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  on the previous 
77e30 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
77e40 74 69 63 20 69 6e 74 20 28 2a 63 6f 6d 70 61 72  tic int (*compar
77e50 65 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65  eFunction(int ke
77e60 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76  yClass))(const v
77e70 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
77e80 6f 69 64 2a 2c 69 6e 74 29 7b 0a 23 69 66 20 30  oid*,int){.#if 0
77e90 20 2f 2a 20 48 41 53 48 5f 49 4e 54 20 61 6e 64   /* HASH_INT and
77ea0 20 48 41 53 48 5f 50 4f 49 4e 54 45 52 20 61 72   HASH_POINTER ar
77eb0 65 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a  e never used */.
77ec0 20 20 73 77 69 74 63 68 28 20 6b 65 79 43 6c 61    switch( keyCla
77ed0 73 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  ss ){.    case S
77ee0 51 4c 49 54 45 5f 48 41 53 48 5f 49 4e 54 3a 20  QLITE_HASH_INT: 
77ef0 20 20 20 20 72 65 74 75 72 6e 20 26 69 6e 74 43      return &intC
77f00 6f 6d 70 61 72 65 3b 0a 20 20 20 20 63 61 73 65  ompare;.    case
77f10 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 50 4f 49   SQLITE_HASH_POI
77f20 4e 54 45 52 3a 20 72 65 74 75 72 6e 20 26 70 74  NTER: return &pt
77f30 72 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 63 61  rCompare;.    ca
77f40 73 65 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53  se SQLITE_HASH_S
77f50 54 52 49 4e 47 3a 20 20 72 65 74 75 72 6e 20 26  TRING:  return &
77f60 73 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20  strCompare;.    
77f70 63 61 73 65 20 53 51 4c 49 54 45 5f 48 41 53 48  case SQLITE_HASH
77f80 5f 42 49 4e 41 52 59 3a 20 20 72 65 74 75 72 6e  _BINARY:  return
77f90 20 26 62 69 6e 43 6f 6d 70 61 72 65 3b 0a 20 20   &binCompare;.  
77fa0 20 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61 6b    default: break
77fb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
77fc0 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 6b 65  ;.#else.  if( ke
77fd0 79 43 6c 61 73 73 3d 3d 53 51 4c 49 54 45 5f 48  yClass==SQLITE_H
77fe0 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20  ASH_STRING ){.  
77ff0 20 20 72 65 74 75 72 6e 20 26 73 74 72 43 6f 6d    return &strCom
78000 70 61 72 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pare;.  }else{. 
78010 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c     assert( keyCl
78020 61 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53 48  ass==SQLITE_HASH
78030 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72  _BINARY );.    r
78040 65 74 75 72 6e 20 26 62 69 6e 43 6f 6d 70 61 72  eturn &binCompar
78050 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  e;.  }.#endif.}.
78060 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d  ./* Link an elem
78070 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ent into the has
78080 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69  h table.*/.stati
78090 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45 6c 65  c void insertEle
780a0 6d 65 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 48  ment(.  Hash *pH
780b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
780c0 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68  * The complete h
780d0 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73  ash table */.  s
780e0 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72  truct _ht *pEntr
780f0 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74  y,    /* The ent
78100 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e  ry into which pN
78110 65 77 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a  ew is inserted *
78120 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e  /.  HashElem *pN
78130 65 77 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ew         /* Th
78140 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20  e element to be 
78150 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20  inserted */.){. 
78160 20 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64   HashElem *pHead
78170 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74  ;       /* First
78180 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79   element already
78190 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20   in pEntry */.  
781a0 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e  pHead = pEntry->
781b0 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 48 65  chain;.  if( pHe
781c0 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  ad ){.    pNew->
781d0 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20  next = pHead;.  
781e0 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70    pNew->prev = p
781f0 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20  Head->prev;.    
78200 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20  if( pHead->prev 
78210 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e  ){ pHead->prev->
78220 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20  next = pNew; }. 
78230 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20     else         
78240 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20      { pH->first 
78250 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48  = pNew; }.    pH
78260 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77  ead->prev = pNew
78270 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
78280 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e  New->next = pH->
78290 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70  first;.    if( p
782a0 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e  H->first ){ pH->
782b0 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e  first->prev = pN
782c0 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e  ew; }.    pNew->
782d0 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48  prev = 0;.    pH
782e0 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a  ->first = pNew;.
782f0 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f    }.  pEntry->co
78300 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d  unt++;.  pEntry-
78310 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d  >chain = pNew;.}
78320 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65  .../* Resize the
78330 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74   hash table so t
78340 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20  hat it cantains 
78350 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65  "new_size" bucke
78360 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65  ts..** "new_size
78370 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  " must be a powe
78380 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68 61 73  r of 2.  The has
78390 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61  h table might fa
783a0 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65  il .** to resize
783b0 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   if sqlite3_mall
783c0 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73  oc() fails..*/.s
783d0 74 61 74 69 63 20 76 6f 69 64 20 72 65 68 61 73  tatic void rehas
783e0 68 28 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20  h(Hash *pH, int 
783f0 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72  new_size){.  str
78400 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b  uct _ht *new_ht;
78410 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
78420 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c  he new hash tabl
78430 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
78440 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65  *elem, *next_ele
78450 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f  m;    /* For loo
78460 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69  ping over existi
78470 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  ng elements */. 
78480 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f   int (*xHash)(co
78490 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
784a0 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e 63  /* The hash func
784b0 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72  tion */..  asser
784c0 74 28 20 28 6e 65 77 5f 73 69 7a 65 20 26 20 28  t( (new_size & (
784d0 6e 65 77 5f 73 69 7a 65 2d 31 29 29 3d 3d 30 20  new_size-1))==0 
784e0 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 69  );..  /* There i
784f0 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  s a call to sqli
78500 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 6e 73  te3_malloc() ins
78510 69 64 65 20 72 65 68 61 73 68 28 29 2e 20 49 66  ide rehash(). If
78520 20 74 68 65 72 65 20 69 73 0a 20 20 2a 2a 20 61   there is.  ** a
78530 6c 72 65 61 64 79 20 61 6e 20 61 6c 6c 6f 63 61  lready an alloca
78540 74 69 6f 6e 20 61 74 20 70 48 2d 3e 68 74 2c 20  tion at pH->ht, 
78550 74 68 65 6e 20 69 66 20 74 68 69 73 20 6d 61 6c  then if this mal
78560 6c 6f 63 28 29 20 66 61 69 6c 73 20 69 74 0a 20  loc() fails it. 
78570 20 2a 2a 20 69 73 20 62 65 6e 69 67 6e 20 28 73   ** is benign (s
78580 69 6e 63 65 20 66 61 69 6c 69 6e 67 20 74 6f 20  ince failing to 
78590 72 65 73 69 7a 65 20 61 20 68 61 73 68 20 74 61  resize a hash ta
785a0 62 6c 65 20 69 73 20 61 20 70 65 72 66 6f 72 6d  ble is a perform
785b0 61 6e 63 65 0a 20 20 2a 2a 20 68 69 74 20 6f 6e  ance.  ** hit on
785c0 6c 79 2c 20 6e 6f 74 20 61 20 66 61 74 61 6c 20  ly, not a fatal 
785d0 65 72 72 6f 72 29 2e 0a 20 20 2a 2f 0a 20 20 73  error)..  */.  s
785e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 42 65 6e 69  qlite3MallocBeni
785f0 67 6e 46 61 69 6c 75 72 65 28 70 48 2d 3e 68 74  gnFailure(pH->ht
78600 73 69 7a 65 3e 30 29 3b 0a 0a 20 20 6e 65 77 5f  size>0);..  new_
78610 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f 68 74  ht = (struct _ht
78620 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   *)sqlite3Malloc
78630 5a 65 72 6f 28 20 6e 65 77 5f 73 69 7a 65 2a 73  Zero( new_size*s
78640 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74  izeof(struct _ht
78650 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 68  ) );.  if( new_h
78660 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
78670 20 69 66 28 20 70 48 2d 3e 68 74 20 29 20 73 71   if( pH->ht ) sq
78680 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e 68  lite3_free(pH->h
78690 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e  t);.  pH->ht = n
786a0 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73  ew_ht;.  pH->hts
786b0 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a  ize = new_size;.
786c0 20 20 78 48 61 73 68 20 3d 20 68 61 73 68 46 75    xHash = hashFu
786d0 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c  nction(pH->keyCl
786e0 61 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d  ass);.  for(elem
786f0 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e  =pH->first, pH->
78700 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65  first=0; elem; e
78710 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29  lem = next_elem)
78720 7b 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 28 2a  {.    int h = (*
78730 78 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70 4b 65  xHash)(elem->pKe
78740 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26  y, elem->nKey) &
78750 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b 0a 20   (new_size-1);. 
78760 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65     next_elem = e
78770 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69  lem->next;.    i
78780 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c  nsertElement(pH,
78790 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65   &new_ht[h], ele
787a0 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 54 68  m);.  }.}../* Th
787b0 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72  is function (for
787c0 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e   internal use on
787d0 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65  ly) locates an e
787e0 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20  lement in an.** 
787f0 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20  hash table that 
78800 6d 61 74 63 68 65 73 20 74 68 65 20 67 69 76 65  matches the give
78810 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68  n key.  The hash
78820 20 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61   for this key ha
78830 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65  s.** already bee
78840 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69  n computed and i
78850 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
78860 34 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  4th parameter..*
78870 2f 0a 73 74 61 74 69 63 20 48 61 73 68 45 6c 65  /.static HashEle
78880 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69  m *findElementGi
78890 76 65 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 74  venHash(.  const
788a0 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 2f   Hash *pH,     /
788b0 2a 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 73  * The pH to be s
788c0 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e  earched */.  con
788d0 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20  st void *pKey,  
788e0 20 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61   /* The key we a
788f0 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72  re searching for
78900 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a   */.  int nKey,.
78910 20 20 69 6e 74 20 68 20 20 20 20 20 20 20 20 20    int h         
78920 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73        /* The has
78930 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20  h for this key. 
78940 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  */.){.  HashElem
78950 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20   *elem;         
78960 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
78970 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20  o loop thru the 
78980 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a  element list */.
78990 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20    int count;    
789a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
789b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
789c0 65 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74  ements left to t
789d0 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  est */.  int (*x
789e0 43 6f 6d 70 61 72 65 29 28 63 6f 6e 73 74 20 76  Compare)(const v
789f0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
78a00 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 63  oid*,int);  /* c
78a10 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
78a20 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d  on */..  if( pH-
78a30 3e 68 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63  >ht ){.    struc
78a40 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d 20  t _ht *pEntry = 
78a50 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20  &pH->ht[h];.    
78a60 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63  elem = pEntry->c
78a70 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20  hain;.    count 
78a80 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b  = pEntry->count;
78a90 0a 20 20 20 20 78 43 6f 6d 70 61 72 65 20 3d 20  .    xCompare = 
78aa0 63 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28  compareFunction(
78ab0 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20  pH->keyClass);. 
78ac0 20 20 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d     while( count-
78ad0 2d 20 26 26 20 65 6c 65 6d 20 29 7b 0a 20 20 20  - && elem ){.   
78ae0 20 20 20 69 66 28 20 28 2a 78 43 6f 6d 70 61 72     if( (*xCompar
78af0 65 29 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c  e)(elem->pKey,el
78b00 65 6d 2d 3e 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b  em->nKey,pKey,nK
78b10 65 79 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20  ey)==0 ){ .     
78b20 20 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a     return elem;.
78b30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 6c        }.      el
78b40 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  em = elem->next;
78b50 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
78b60 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d  urn 0;.}../* Rem
78b70 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74  ove a single ent
78b80 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  ry from the hash
78b90 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70   table given a p
78ba0 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a  ointer to that.*
78bb0 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20  * element and a 
78bc0 68 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d  hash on the elem
78bd0 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74  ent's key..*/.st
78be0 61 74 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 65  atic void remove
78bf0 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68  ElementGivenHash
78c00 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20  (.  Hash *pH,   
78c10 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20        /* The pH 
78c20 63 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d  containing "elem
78c30 22 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a  " */.  HashElem*
78c40 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20   elem,   /* The 
78c50 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65  element to be re
78c60 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70  moved from the p
78c70 48 20 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20  H */.  int h    
78c80 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68           /* Hash
78c90 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65   value for the e
78ca0 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73  lement */.){.  s
78cb0 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72  truct _ht *pEntr
78cc0 79 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70  y;.  if( elem->p
78cd0 72 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d  rev ){.    elem-
78ce0 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c  >prev->next = el
78cf0 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c  em->next; .  }el
78d00 73 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73  se{.    pH->firs
78d10 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  t = elem->next;.
78d20 20 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e    }.  if( elem->
78d30 6e 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d  next ){.    elem
78d40 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65  ->next->prev = e
78d50 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20  lem->prev;.  }. 
78d60 20 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68   pEntry = &pH->h
78d70 74 5b 68 5d 3b 0a 20 20 69 66 28 20 70 45 6e 74  t[h];.  if( pEnt
78d80 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20  ry->chain==elem 
78d90 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63  ){.    pEntry->c
78da0 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  hain = elem->nex
78db0 74 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d  t;.  }.  pEntry-
78dc0 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20  >count--;.  if( 
78dd0 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d 30  pEntry->count<=0
78de0 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e   ){.    pEntry->
78df0 63 68 61 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20  chain = 0;.  }. 
78e00 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79   if( pH->copyKey
78e10 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
78e20 66 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29  free(elem->pKey)
78e30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
78e40 66 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20  free( elem );.  
78e50 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69  pH->count--;.  i
78e60 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20  f( pH->count<=0 
78e70 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
78e80 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20  H->first==0 );. 
78e90 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63     assert( pH->c
78ea0 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73  ount==0 );.    s
78eb0 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28  qlite3HashClear(
78ec0 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41  pH);.  }.}../* A
78ed0 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65  ttempt to locate
78ee0 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74   an element of t
78ef0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48  he hash table pH
78f00 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74   with a key.** t
78f10 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79  hat matches pKey
78f20 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 61  ,nKey.  Return a
78f30 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
78f40 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a 2a  corresponding .*
78f50 2a 20 48 61 73 68 45 6c 65 6d 20 73 74 72 75 63  * HashElem struc
78f60 74 75 72 65 20 66 6f 72 20 74 68 69 73 20 65 6c  ture for this el
78f70 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 20 66  ement if it is f
78f80 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 0a 2a 2a  ound, or NULL.**
78f90 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53   otherwise..*/.S
78fa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 48 61  QLITE_PRIVATE Ha
78fb0 73 68 45 6c 65 6d 20 2a 73 71 6c 69 74 65 33 48  shElem *sqlite3H
78fc0 61 73 68 46 69 6e 64 45 6c 65 6d 28 63 6f 6e 73  ashFindElem(cons
78fd0 74 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73  t Hash *pH, cons
78fe0 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
78ff0 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68  t nKey){.  int h
79000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
79010 20 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a   A hash on key *
79020 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  /.  HashElem *el
79030 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c  em;    /* The el
79040 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68  ement that match
79050 65 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  es key */.  int 
79060 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76  (*xHash)(const v
79070 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54  oid*,int);  /* T
79080 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  he hash function
79090 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 3d 3d 30   */..  if( pH==0
790a0 20 7c 7c 20 70 48 2d 3e 68 74 3d 3d 30 20 29 20   || pH->ht==0 ) 
790b0 72 65 74 75 72 6e 20 30 3b 0a 20 20 78 48 61 73  return 0;.  xHas
790c0 68 20 3d 20 68 61 73 68 46 75 6e 63 74 69 6f 6e  h = hashFunction
790d0 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a  (pH->keyClass);.
790e0 20 20 61 73 73 65 72 74 28 20 78 48 61 73 68 21    assert( xHash!
790f0 3d 30 20 29 3b 0a 20 20 68 20 3d 20 28 2a 78 48  =0 );.  h = (*xH
79100 61 73 68 29 28 70 4b 65 79 2c 6e 4b 65 79 29 3b  ash)(pKey,nKey);
79110 0a 20 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e  .  assert( (pH->
79120 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74  htsize & (pH->ht
79130 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20  size-1))==0 );. 
79140 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d   elem = findElem
79150 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c  entGivenHash(pH,
79160 70 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 26 20 28  pKey,nKey, h & (
79170 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3b 0a  pH->htsize-1));.
79180 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 7d    return elem;.}
79190 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  ../* Attempt to 
791a0 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e  locate an elemen
791b0 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61  t of the hash ta
791c0 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b 65  ble pH with a ke
791d0 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65  y.** that matche
791e0 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65  s pKey,nKey.  Re
791f0 74 75 72 6e 20 74 68 65 20 64 61 74 61 20 66 6f  turn the data fo
79200 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69  r this element i
79210 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64  f it is.** found
79220 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65  , or NULL if the
79230 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a  re is no match..
79240 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
79250 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48  E void *sqlite3H
79260 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61  ashFind(const Ha
79270 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f  sh *pH, const vo
79280 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
79290 65 79 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  ey){.  HashElem 
792a0 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65  *elem;    /* The
792b0 20 65 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d 61   element that ma
792c0 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a 20 20 65  tches key */.  e
792d0 6c 65 6d 20 3d 20 73 71 6c 69 74 65 33 48 61 73  lem = sqlite3Has
792e0 68 46 69 6e 64 45 6c 65 6d 28 70 48 2c 20 70 4b  hFindElem(pH, pK
792f0 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 72 65 74  ey, nKey);.  ret
79300 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d  urn elem ? elem-
79310 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  >data : 0;.}../*
79320 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65   Insert an eleme
79330 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68  nt into the hash
79340 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20   table pH.  The 
79350 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79  key is pKey,nKey
79360 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61  .** and the data
79370 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a   is "data"..**.*
79380 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20  * If no element 
79390 65 78 69 73 74 73 20 77 69 74 68 20 61 20 6d 61  exists with a ma
793a0 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e  tching key, then
793b0 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e   a new.** elemen
793c0 74 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 41  t is created.  A
793d0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79   copy of the key
793e0 20 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20   is made if the 
793f0 63 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20  copyKey.** flag 
79400 69 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73  is set.  NULL is
79410 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
79420 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d   If another elem
79430 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ent already exis
79440 74 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  ts with the same
79450 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a   key, then the.*
79460 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61  * new data repla
79470 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61  ces the old data
79480 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74   and the old dat
79490 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  a is returned..*
794a0 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74  * The key is not
794b0 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20   copied in this 
794c0 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20  instance.  If a 
794d0 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68  malloc fails, th
794e0 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61  en.** the new da
794f0 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  ta is returned a
79500 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  nd the hash tabl
79510 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  e is unchanged..
79520 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61  **.** If the "da
79530 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ta" parameter to
79540 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
79550 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  s NULL, then the
79560 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72  .** element corr
79570 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65  esponding to "ke
79580 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72  y" is removed fr
79590 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
795a0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
795b0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
795c0 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 73  e3HashInsert(Has
795d0 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69  h *pH, const voi
795e0 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
795f0 79 2c 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a  y, void *data){.
79600 20 20 69 6e 74 20 68 72 61 77 3b 20 20 20 20 20    int hraw;     
79610 20 20 20 20 20 20 20 20 2f 2a 20 52 61 77 20 68          /* Raw h
79620 61 73 68 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ash value of the
79630 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 68 3b   key */.  int h;
79640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79650 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66 20 74  /* the hash of t
79660 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61  he key modulo ha
79670 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f  sh table size */
79680 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
79690 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64  m;       /* Used
796a0 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68   to loop thru th
796b0 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a  e element list *
796c0 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65  /.  HashElem *ne
796d0 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77  w_elem;   /* New
796e0 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64 20 74   element added t
796f0 6f 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e  o the pH */.  in
79700 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74  t (*xHash)(const
79710 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a   void*,int);  /*
79720 20 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69   The hash functi
79730 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  on */..  assert(
79740 20 70 48 21 3d 30 20 29 3b 0a 20 20 78 48 61 73   pH!=0 );.  xHas
79750 68 20 3d 20 68 61 73 68 46 75 6e 63 74 69 6f 6e  h = hashFunction
79760 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a  (pH->keyClass);.
79770 20 20 61 73 73 65 72 74 28 20 78 48 61 73 68 21    assert( xHash!
79780 3d 30 20 29 3b 0a 20 20 68 72 61 77 20 3d 20 28  =0 );.  hraw = (
79790 2a 78 48 61 73 68 29 28 70 4b 65 79 2c 20 6e 4b  *xHash)(pKey, nK
797a0 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ey);.  assert( (
797b0 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70 48  pH->htsize & (pH
797c0 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20  ->htsize-1))==0 
797d0 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 26 20  );.  h = hraw & 
797e0 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a  (pH->htsize-1);.
797f0 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65    elem = findEle
79800 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48  mentGivenHash(pH
79810 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20  ,pKey,nKey,h);. 
79820 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20   if( elem ){.   
79830 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 20   void *old_data 
79840 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20 20  = elem->data;.  
79850 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 7b    if( data==0 ){
79860 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 45 6c 65  .      removeEle
79870 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48  mentGivenHash(pH
79880 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20 20 7d 65  ,elem,h);.    }e
79890 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c 65 6d 2d  lse{.      elem-
798a0 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20  >data = data;.  
798b0 20 20 20 20 69 66 28 20 21 70 48 2d 3e 63 6f 70      if( !pH->cop
798c0 79 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  yKey ){.        
798d0 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f  elem->pKey = (vo
798e0 69 64 20 2a 29 70 4b 65 79 3b 0a 20 20 20 20 20  id *)pKey;.     
798f0 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
79900 6e 4b 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79  nKey==elem->nKey
79910 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
79920 75 72 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20  urn old_data;.  
79930 7d 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20  }.  if( data==0 
79940 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 65  ) return 0;.  ne
79950 77 5f 65 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c  w_elem = (HashEl
79960 65 6d 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  em*)sqlite3_mall
79970 6f 63 28 20 73 69 7a 65 6f 66 28 48 61 73 68 45  oc( sizeof(HashE
79980 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28 20 6e 65  lem) );.  if( ne
79990 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75  w_elem==0 ) retu
799a0 72 6e 20 64 61 74 61 3b 0a 20 20 69 66 28 20 70  rn data;.  if( p
799b0 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 70 4b  H->copyKey && pK
799c0 65 79 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 77  ey!=0 ){.    new
799d0 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 73 71  _elem->pKey = sq
799e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 4b  lite3_malloc( nK
799f0 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 65  ey );.    if( ne
79a00 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 3d 3d 30 20  w_elem->pKey==0 
79a10 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
79a20 5f 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3b  _free(new_elem);
79a30 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 64 61  .      return da
79a40 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65  ta;.    }.    me
79a50 6d 63 70 79 28 28 76 6f 69 64 2a 29 6e 65 77 5f  mcpy((void*)new_
79a60 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 70 4b 65 79  elem->pKey, pKey
79a70 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65  , nKey);.  }else
79a80 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e  {.    new_elem->
79a90 70 4b 65 79 20 3d 20 28 76 6f 69 64 2a 29 70 4b  pKey = (void*)pK
79aa0 65 79 3b 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c  ey;.  }.  new_el
79ab0 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b  em->nKey = nKey;
79ac0 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a  .  pH->count++;.
79ad0 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65    if( pH->htsize
79ae0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 68 61 73  ==0 ){.    rehas
79af0 68 28 70 48 2c 38 29 3b 0a 20 20 20 20 69 66 28  h(pH,8);.    if(
79b00 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29   pH->htsize==0 )
79b10 7b 0a 20 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e  {.      pH->coun
79b20 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  t = 0;.      if(
79b30 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a   pH->copyKey ){.
79b40 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
79b50 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 2d 3e 70  free(new_elem->p
79b60 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Key);.      }.  
79b70 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
79b80 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20  (new_elem);.    
79b90 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20    return data;. 
79ba0 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
79bb0 48 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68  H->count > pH->h
79bc0 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 68  tsize ){.    reh
79bd0 61 73 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a  ash(pH,pH->htsiz
79be0 65 2a 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  e*2);.  }.  asse
79bf0 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30  rt( pH->htsize>0
79c00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
79c10 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d  H->htsize & (pH-
79c20 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29  >htsize-1))==0 )
79c30 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 26 20 28  ;.  h = hraw & (
79c40 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20  pH->htsize-1);. 
79c50 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70   insertElement(p
79c60 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e  H, &pH->ht[h], n
79c70 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f  ew_elem);.  new_
79c80 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74  elem->data = dat
79c90 61 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  a;.  return 0;.}
79ca0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
79cb0 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20  * End of hash.c 
79cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79cf0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
79d00 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63  * Begin file opc
79d10 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  odes.c *********
79d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79d40 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c  /./* Automatical
79d50 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44  ly generated.  D
79d60 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a  o not edit */./*
79d70 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64   See the mkopcod
79d80 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f  ec.awk script fo
79d90 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69  r details. */.#i
79da0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
79db0 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20  E_OMIT_EXPLAIN) 
79dc0 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  || !defined(NDEB
79dd0 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56  UG) || defined(V
79de0 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20  DBE_PROFILE) || 
79df0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
79e00 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG).SQLITE_PRI
79e10 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
79e20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61  *sqlite3OpcodeNa
79e30 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74  me(int i){. stat
79e40 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
79e50 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20  onst azName[] = 
79e60 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20  { "?",.     /*  
79e70 20 31 20 2a 2f 20 22 4d 65 6d 4c 6f 61 64 22 2c   1 */ "MemLoad",
79e80 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20  .     /*   2 */ 
79e90 22 56 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a  "VNext",.     /*
79ea0 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22     3 */ "Column"
79eb0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f  ,.     /*   4 */
79ec0 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 20   "SetCookie",.  
79ed0 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 49 66     /*   5 */ "If
79ee0 4d 65 6d 50 6f 73 22 2c 0a 20 20 20 20 20 2f 2a  MemPos",.     /*
79ef0 20 20 20 36 20 2a 2f 20 22 53 65 71 75 65 6e 63     6 */ "Sequenc
79f00 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20  e",.     /*   7 
79f10 2a 2f 20 22 4d 6f 76 65 47 74 22 2c 0a 20 20 20  */ "MoveGt",.   
79f20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 6f 77    /*   8 */ "Row
79f30 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20  Key",.     /*   
79f40 39 20 2a 2f 20 22 4f 70 65 6e 57 72 69 74 65 22  9 */ "OpenWrite"
79f50 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f  ,.     /*  10 */
79f60 20 22 49 66 22 2c 0a 20 20 20 20 20 2f 2a 20 20   "If",.     /*  
79f70 31 31 20 2a 2f 20 22 50 6f 70 22 2c 0a 20 20 20  11 */ "Pop",.   
79f80 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 56 52 6f    /*  12 */ "VRo
79f90 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  wid",.     /*  1
79fa0 33 20 2a 2f 20 22 43 6f 6c 6c 53 65 71 22 2c 0a  3 */ "CollSeq",.
79fb0 20 20 20 20 20 2f 2a 20 20 31 34 20 2a 2f 20 22       /*  14 */ "
79fc0 4f 70 65 6e 52 65 61 64 22 2c 0a 20 20 20 20 20  OpenRead",.     
79fd0 2f 2a 20 20 31 35 20 2a 2f 20 22 45 78 70 69 72  /*  15 */ "Expir
79fe0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20  e",.     /*  16 
79ff0 2a 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f  */ "Not",.     /
7a000 2a 20 20 31 37 20 2a 2f 20 22 41 75 74 6f 43 6f  *  17 */ "AutoCo
7a010 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  mmit",.     /*  
7a020 31 38 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79  18 */ "Integrity
7a030 43 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39  Ck",.     /*  19
7a040 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20   */ "Sort",.    
7a050 20 2f 2a 20 20 32 30 20 2a 2f 20 22 46 75 6e 63   /*  20 */ "Func
7a060 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20  tion",.     /*  
7a070 32 31 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20  21 */ "Noop",.  
7a080 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 52 65     /*  22 */ "Re
7a090 74 75 72 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20  turn",.     /*  
7a0a0 32 33 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64 22  23 */ "NewRowid"
7a0b0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a 2f  ,.     /*  24 */
7a0c0 20 22 49 66 4d 65 6d 4e 65 67 22 2c 0a 20 20 20   "IfMemNeg",.   
7a0d0 20 20 2f 2a 20 20 32 35 20 2a 2f 20 22 56 61 72    /*  25 */ "Var
7a0e0 69 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20  iable",.     /* 
7a0f0 20 32 36 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c   26 */ "String",
7a100 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20  .     /*  27 */ 
7a110 22 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a  "RealAffinity",.
7a120 20 20 20 20 20 2f 2a 20 20 32 38 20 2a 2f 20 22       /*  28 */ "
7a130 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f  VRename",.     /
7a140 2a 20 20 32 39 20 2a 2f 20 22 50 61 72 73 65 53  *  29 */ "ParseS
7a150 63 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20  chema",.     /* 
7a160 20 33 30 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a   30 */ "VOpen",.
7a170 20 20 20 20 20 2f 2a 20 20 33 31 20 2a 2f 20 22       /*  31 */ "
7a180 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20  Close",.     /* 
7a190 20 33 32 20 2a 2f 20 22 43 72 65 61 74 65 49 6e   32 */ "CreateIn
7a1a0 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  dex",.     /*  3
7a1b0 33 20 2a 2f 20 22 49 73 55 6e 69 71 75 65 22 2c  3 */ "IsUnique",
7a1c0 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a 2f 20  .     /*  34 */ 
7a1d0 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20  "NotFound",.    
7a1e0 20 2f 2a 20 20 33 35 20 2a 2f 20 22 49 6e 74 36   /*  35 */ "Int6
7a1f0 34 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20  4",.     /*  36 
7a200 2a 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a  */ "MustBeInt",.
7a210 20 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22       /*  37 */ "
7a220 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Halt",.     /*  
7a230 33 38 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20  38 */ "Rowid",. 
7a240 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 49      /*  39 */ "I
7a250 64 78 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20  dxLT",.     /*  
7a260 34 30 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a  40 */ "AddImm",.
7a270 20 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22       /*  41 */ "
7a280 53 74 61 74 65 6d 65 6e 74 22 2c 0a 20 20 20 20  Statement",.    
7a290 20 2f 2a 20 20 34 32 20 2a 2f 20 22 52 6f 77 44   /*  42 */ "RowD
7a2a0 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  ata",.     /*  4
7a2b0 33 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20  3 */ "MemMax",. 
7a2c0 20 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22 50      /*  44 */ "P
7a2d0 75 73 68 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  ush",.     /*  4
7a2e0 35 20 2a 2f 20 22 4e 6f 74 45 78 69 73 74 73 22  5 */ "NotExists"
7a2f0 2c 0a 20 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f  ,.     /*  46 */
7a300 20 22 4d 65 6d 49 6e 63 72 22 2c 0a 20 20 20 20   "MemIncr",.    
7a310 20 2f 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75   /*  47 */ "Gosu
7a320 62 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20  b",.     /*  48 
7a330 2a 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20  */ "Integer",.  
7a340 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 4d 65     /*  49 */ "Me
7a350 6d 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  mInt",.     /*  
7a360 35 30 20 2a 2f 20 22 50 72 65 76 22 2c 0a 20 20  50 */ "Prev",.  
7a370 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22 56 43     /*  51 */ "VC
7a380 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20  olumn",.     /* 
7a390 20 35 32 20 2a 2f 20 22 43 72 65 61 74 65 54 61   52 */ "CreateTa
7a3a0 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35  ble",.     /*  5
7a3b0 33 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20  3 */ "Last",.   
7a3c0 20 20 2f 2a 20 20 35 34 20 2a 2f 20 22 49 6e 63    /*  54 */ "Inc
7a3d0 72 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f  rVacuum",.     /
7a3e0 2a 20 20 35 35 20 2a 2f 20 22 49 64 78 52 6f 77  *  55 */ "IdxRow
7a3f0 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36  id",.     /*  56
7a400 20 2a 2f 20 22 4d 61 6b 65 49 64 78 52 65 63 22   */ "MakeIdxRec"
7a410 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f  ,.     /*  57 */
7a420 20 22 52 65 73 65 74 43 6f 75 6e 74 22 2c 0a 20   "ResetCount",. 
7a430 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 46      /*  58 */ "F
7a440 69 66 6f 57 72 69 74 65 22 2c 0a 20 20 20 20 20  ifoWrite",.     
7a450 2f 2a 20 20 35 39 20 2a 2f 20 22 43 61 6c 6c 62  /*  59 */ "Callb
7a460 61 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  ack",.     /*  6
7a470 30 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20 20  0 */ "Or",.     
7a480 2f 2a 20 20 36 31 20 2a 2f 20 22 41 6e 64 22 2c  /*  61 */ "And",
7a490 0a 20 20 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20  .     /*  62 */ 
7a4a0 22 43 6f 6e 74 65 78 74 50 75 73 68 22 2c 0a 20  "ContextPush",. 
7a4b0 20 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 44      /*  63 */ "D
7a4c0 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 20  ropTrigger",.   
7a4d0 20 20 2f 2a 20 20 36 34 20 2a 2f 20 22 44 72 6f    /*  64 */ "Dro
7a4e0 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a  pIndex",.     /*
7a4f0 20 20 36 35 20 2a 2f 20 22 49 73 4e 75 6c 6c 22    65 */ "IsNull"
7a500 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 2a 2f  ,.     /*  66 */
7a510 20 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20   "NotNull",.    
7a520 20 2f 2a 20 20 36 37 20 2a 2f 20 22 4e 65 22 2c   /*  67 */ "Ne",
7a530 0a 20 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f 20  .     /*  68 */ 
7a540 22 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  "Eq",.     /*  6
7a550 39 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20  9 */ "Gt",.     
7a560 2f 2a 20 20 37 30 20 2a 2f 20 22 4c 65 22 2c 0a  /*  70 */ "Le",.
7a570 20 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22       /*  71 */ "
7a580 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32  Lt",.     /*  72
7a590 20 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f   */ "Ge",.     /
7a5a0 2a 20 20 37 33 20 2a 2f 20 22 49 64 78 47 45 22  *  73 */ "IdxGE"
7a5b0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f  ,.     /*  74 */
7a5c0 20 22 42 69 74 41 6e 64 22 2c 0a 20 20 20 20 20   "BitAnd",.     
7a5d0 2f 2a 20 20 37 35 20 2a 2f 20 22 42 69 74 4f 72  /*  75 */ "BitOr
7a5e0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 36 20 2a  ",.     /*  76 *
7a5f0 2f 20 22 53 68 69 66 74 4c 65 66 74 22 2c 0a 20  / "ShiftLeft",. 
7a600 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22 53      /*  77 */ "S
7a610 68 69 66 74 52 69 67 68 74 22 2c 0a 20 20 20 20  hiftRight",.    
7a620 20 2f 2a 20 20 37 38 20 2a 2f 20 22 41 64 64 22   /*  78 */ "Add"
7a630 2c 0a 20 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f  ,.     /*  79 */
7a640 20 22 53 75 62 74 72 61 63 74 22 2c 0a 20 20 20   "Subtract",.   
7a650 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 4d 75 6c    /*  80 */ "Mul
7a660 74 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f 2a 20  tiply",.     /* 
7a670 20 38 31 20 2a 2f 20 22 44 69 76 69 64 65 22 2c   81 */ "Divide",
7a680 0a 20 20 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20  .     /*  82 */ 
7a690 22 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20 20 20  "Remainder",.   
7a6a0 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 43 6f 6e    /*  83 */ "Con
7a6b0 63 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  cat",.     /*  8
7a6c0 34 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22  4 */ "IdxDelete"
7a6d0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f  ,.     /*  85 */
7a6e0 20 22 4e 65 67 61 74 69 76 65 22 2c 0a 20 20 20   "Negative",.   
7a6f0 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22 56 61 63    /*  86 */ "Vac
7a700 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  uum",.     /*  8
7a710 37 20 2a 2f 20 22 42 69 74 4e 6f 74 22 2c 0a 20  7 */ "BitNot",. 
7a720 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22 53      /*  88 */ "S
7a730 74 72 69 6e 67 38 22 2c 0a 20 20 20 20 20 2f 2a  tring8",.     /*
7a740 20 20 38 39 20 2a 2f 20 22 4d 6f 76 65 4c 65 22    89 */ "MoveLe"
7a750 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a 2f  ,.     /*  90 */
7a760 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f   "IfNot",.     /
7a770 2a 20 20 39 31 20 2a 2f 20 22 44 72 6f 70 54 61  *  91 */ "DropTa
7a780 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  ble",.     /*  9
7a790 32 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64  2 */ "MakeRecord
7a7a0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a  ",.     /*  93 *
7a7b0 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20 20 20  / "Delete",.    
7a7c0 20 2f 2a 20 20 39 34 20 2a 2f 20 22 41 67 67 46   /*  94 */ "AggF
7a7d0 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20  inal",.     /*  
7a7e0 39 35 20 2a 2f 20 22 44 75 70 22 2c 0a 20 20 20  95 */ "Dup",.   
7a7f0 20 20 2f 2a 20 20 39 36 20 2a 2f 20 22 47 6f 74    /*  96 */ "Got
7a800 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37 20  o",.     /*  97 
7a810 2a 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c 0a  */ "TableLock",.
7a820 20 20 20 20 20 2f 2a 20 20 39 38 20 2a 2f 20 22       /*  98 */ "
7a830 46 69 66 6f 52 65 61 64 22 2c 0a 20 20 20 20 20  FifoRead",.     
7a840 2f 2a 20 20 39 39 20 2a 2f 20 22 43 6c 65 61 72  /*  99 */ "Clear
7a850 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 30 20 2a  ",.     /* 100 *
7a860 2f 20 22 49 64 78 47 54 22 2c 0a 20 20 20 20 20  / "IdxGT",.     
7a870 2f 2a 20 31 30 31 20 2a 2f 20 22 4d 6f 76 65 4c  /* 101 */ "MoveL
7a880 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 32 20  t",.     /* 102 
7a890 2a 2f 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65  */ "VerifyCookie
7a8a0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20 2a  ",.     /* 103 *
7a8b0 2f 20 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20  / "AggStep",.   
7a8c0 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 50 75 6c    /* 104 */ "Pul
7a8d0 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 35 20  l",.     /* 105 
7a8e0 2a 2f 20 22 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e  */ "SetNumColumn
7a8f0 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20  s",.     /* 106 
7a900 2a 2f 20 22 41 62 73 56 61 6c 75 65 22 2c 0a 20  */ "AbsValue",. 
7a910 20 20 20 20 2f 2a 20 31 30 37 20 2a 2f 20 22 54      /* 107 */ "T
7a920 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20 20  ransaction",.   
7a930 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22 56 46 69    /* 108 */ "VFi
7a940 6c 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31  lter",.     /* 1
7a950 30 39 20 2a 2f 20 22 56 44 65 73 74 72 6f 79 22  09 */ "VDestroy"
7a960 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30 20 2a 2f  ,.     /* 110 */
7a970 20 22 43 6f 6e 74 65 78 74 50 6f 70 22 2c 0a 20   "ContextPop",. 
7a980 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 4e      /* 111 */ "N
7a990 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  ext",.     /* 11
7a9a0 32 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 22  2 */ "IdxInsert"
7a9b0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 33 20 2a 2f  ,.     /* 113 */
7a9c0 20 22 44 69 73 74 69 6e 63 74 22 2c 0a 20 20 20   "Distinct",.   
7a9d0 20 20 2f 2a 20 31 31 34 20 2a 2f 20 22 49 6e 73    /* 114 */ "Ins
7a9e0 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  ert",.     /* 11
7a9f0 35 20 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a  5 */ "Destroy",.
7aa00 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22       /* 116 */ "
7aa10 52 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20  ReadCookie",.   
7aa20 20 20 2f 2a 20 31 31 37 20 2a 2f 20 22 46 6f 72    /* 117 */ "For
7aa30 63 65 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20  ceInt",.     /* 
7aa40 31 31 38 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 6c  118 */ "LoadAnal
7aa50 79 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31  ysis",.     /* 1
7aa60 31 39 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22 2c  19 */ "Explain",
7aa70 0a 20 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f 20  .     /* 120 */ 
7aa80 22 49 66 4d 65 6d 5a 65 72 6f 22 2c 0a 20 20 20  "IfMemZero",.   
7aa90 20 20 2f 2a 20 31 32 31 20 2a 2f 20 22 4f 70 65    /* 121 */ "Ope
7aaa0 6e 50 73 65 75 64 6f 22 2c 0a 20 20 20 20 20 2f  nPseudo",.     /
7aab0 2a 20 31 32 32 20 2a 2f 20 22 4f 70 65 6e 45 70  * 122 */ "OpenEp
7aac0 68 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20 2f  hemeral",.     /
7aad0 2a 20 31 32 33 20 2a 2f 20 22 4e 75 6c 6c 22 2c  * 123 */ "Null",
7aae0 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f 20  .     /* 124 */ 
7aaf0 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20  "Blob",.     /* 
7ab00 31 32 35 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20  125 */ "Real",. 
7ab10 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 48      /* 126 */ "H
7ab20 65 78 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a  exBlob",.     /*
7ab30 20 31 32 37 20 2a 2f 20 22 4d 65 6d 53 74 6f 72   127 */ "MemStor
7ab40 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20  e",.     /* 128 
7ab50 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20  */ "Rewind",.   
7ab60 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 4d 6f 76    /* 129 */ "Mov
7ab70 65 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  eGe",.     /* 13
7ab80 30 20 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20  0 */ "VBegin",. 
7ab90 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 56      /* 131 */ "V
7aba0 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a  Update",.     /*
7abb0 20 31 33 32 20 2a 2f 20 22 56 43 72 65 61 74 65   132 */ "VCreate
7abc0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a  ",.     /* 133 *
7abd0 2f 20 22 4d 65 6d 4d 6f 76 65 22 2c 0a 20 20 20  / "MemMove",.   
7abe0 20 20 2f 2a 20 31 33 34 20 2a 2f 20 22 4d 65 6d    /* 134 */ "Mem
7abf0 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Null",.     /* 1
7ac00 33 35 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20  35 */ "Found",. 
7ac10 20 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e      /* 136 */ "N
7ac20 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a  ullRow",.     /*
7ac30 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64   137 */ "NotUsed
7ac40 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31  _137",.     /* 1
7ac50 33 38 20 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a  38 */ "ToText",.
7ac60 20 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20 22       /* 139 */ "
7ac70 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a  ToBlob",.     /*
7ac80 20 31 34 30 20 2a 2f 20 22 54 6f 4e 75 6d 65 72   140 */ "ToNumer
7ac90 69 63 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31  ic",.     /* 141
7aca0 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20   */ "ToInt",.   
7acb0 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 52    /* 142 */ "ToR
7acc0 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74  eal",.  };.  ret
7acd0 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d  urn azName[i];.}
7ace0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
7acf0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
7ad00 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a  opcodes.c ******
7ad10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ad30 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
7ad40 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
7ad50 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a  ile os_os2.c ***
7ad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ad70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ad80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
7ad90 30 30 36 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a  006 Feb 14.**.**
7ada0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
7adb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
7adc0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
7add0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
7ade0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
7adf0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
7ae00 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
7ae10 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
7ae20 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
7ae30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
7ae40 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
7ae50 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
7ae60 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
7ae70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
7ae80 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
7ae90 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
7aea0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
7aeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7aec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7aed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7aee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7aef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
7af00 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
7af10 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61  ontains code tha
7af20 74 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f  t is specific to
7af30 20 4f 53 2f 32 2e 0a 2a 2f 0a 0a 0a 23 69 66 20   OS/2..*/...#if 
7af40 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 4d 61  OS_OS2../*.** Ma
7af50 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74  cros used to det
7af60 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
7af70 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72  r not to use thr
7af80 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  eads..*/.#if def
7af90 69 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29  ined(THREADSAFE)
7afa0 20 26 26 20 54 48 52 45 41 44 53 41 46 45 0a 23   && THREADSAFE.#
7afb0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
7afc0 53 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e  S2_THREADS 1.#en
7afd0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75  dif../*.** Inclu
7afe0 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  de code that is 
7aff0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73  common to all os
7b000 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a  _*.c files.*/./*
7b010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
7b020 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  clude os_common.
7b030 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
7b040 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a  of os_os2.c ****
7b050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
7b060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
7b070 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d  gin file os_comm
7b080 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
7b090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
7b0b0 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a  .** 2004 May 22.
7b0c0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
7b0d0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
7b0e0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
7b0f0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
7b100 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
7b110 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
7b120 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
7b130 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
7b140 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
7b150 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
7b160 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
7b170 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
7b180 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
7b190 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
7b1a0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
7b1b0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
7b1c0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
7b1d0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
7b1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b220 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
7b230 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63  ile contains mac
7b240 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65  ros and a little
7b250 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61   bit of code tha
7b260 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a  t is common to.*
7b270 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61  * all of the pla
7b280 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66  tform-specific f
7b290 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e  iles (os_*.c) an
7b2a0 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69  d is #included i
7b2b0 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c  nto those.** fil
7b2c0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
7b2d0 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69  ile should be #i
7b2e0 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f  ncluded by the o
7b2f0 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79  s_*.c files only
7b300 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a  .  It is not a.*
7b310 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  * general purpos
7b320 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a  e header file..*
7b330 2f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  /../*.** At leas
7b340 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
7b350 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
7b360 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
7b370 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
7b380 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
7b390 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
7b3a0 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
7b3b0 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
7b3c0 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
7b3d0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
7b3e0 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
7b3f0 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
7b400 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
7b410 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
7b420 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
7b430 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
7b440 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
7b450 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
7b460 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
7b470 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20  .#endif.../*. * 
7b480 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 74 68  When testing, th
7b490 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  is global variab
7b4a0 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20 6c 6f  le stores the lo
7b4b0 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a  cation of the. *
7b4c0 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e   pending-byte in
7b4d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
7b4e0 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53  le.. */.#ifdef S
7b4f0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
7b500 45 5f 41 50 49 20 75 6e 73 69 67 6e 65 64 20 69  E_API unsigned i
7b510 6e 74 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69  nt sqlite3_pendi
7b520 6e 67 5f 62 79 74 65 20 3d 20 30 78 34 30 30 30  ng_byte = 0x4000
7b530 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69  0000;.#endif..#i
7b540 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
7b550 47 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  G.SQLITE_API int
7b560 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63   sqlite3_os_trac
7b570 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f  e = 0;.#define O
7b580 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20  STRACE1(X)      
7b590 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f     if( sqlite3_o
7b5a0 73 5f 74 72 61 63 65 20 29 20 73 71 6c 69 74 65  s_trace ) sqlite
7b5b0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a  3DebugPrintf(X).
7b5c0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
7b5d0 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20  (X,Y)       if( 
7b5e0 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65  sqlite3_os_trace
7b5f0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
7b600 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69  rintf(X,Y).#defi
7b610 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
7b620 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  Z)     if( sqlit
7b630 65 33 5f 6f 73 5f 74 72 61 63 65 20 29 20 73 71  e3_os_trace ) sq
7b640 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
7b650 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
7b660 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
7b670 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  )   if( sqlite3_
7b680 6f 73 5f 74 72 61 63 65 20 29 20 73 71 6c 69 74  os_trace ) sqlit
7b690 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
7b6a0 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f  Y,Z,A).#define O
7b6b0 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c  STRACE5(X,Y,Z,A,
7b6c0 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f  B) if( sqlite3_o
7b6d0 73 5f 74 72 61 63 65 20 29 20 73 71 6c 69 74 65  s_trace ) sqlite
7b6e0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
7b6f0 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20  ,Z,A,B).#define 
7b700 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41  OSTRACE6(X,Y,Z,A
7b710 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73  ,B,C) \.    if(s
7b720 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65 29  qlite3_os_trace)
7b730 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
7b740 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29  ntf(X,Y,Z,A,B,C)
7b750 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
7b760 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  7(X,Y,Z,A,B,C,D)
7b770 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65   \.    if(sqlite
7b780 33 5f 6f 73 5f 74 72 61 63 65 29 20 73 71 6c 69  3_os_trace) sqli
7b790 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
7b7a0 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65  ,Y,Z,A,B,C,D).#e
7b7b0 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  lse.#define OSTR
7b7c0 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20  ACE1(X).#define 
7b7d0 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64  OSTRACE2(X,Y).#d
7b7e0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58  efine OSTRACE3(X
7b7f0 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Y,Z).#define OS
7b800 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a  TRACE4(X,Y,Z,A).
7b810 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35  #define OSTRACE5
7b820 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66  (X,Y,Z,A,B).#def
7b830 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59  ine OSTRACE6(X,Y
7b840 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e  ,Z,A,B,C).#defin
7b850 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a  e OSTRACE7(X,Y,Z
7b860 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66  ,A,B,C,D).#endif
7b870 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
7b880 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74  or performance t
7b890 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c  racing.  Normall
7b8a0 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f  y turned off.  O
7b8b0 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20  nly works.** on 
7b8c0 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a  i486 hardware..*
7b8d0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
7b8e0 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43  PERFORMANCE_TRAC
7b8f0 45 0a 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 75 6e 73  E.__inline__ uns
7b900 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
7b910 69 6e 74 20 68 77 74 69 6d 65 28 76 6f 69 64 29  int hwtime(void)
7b920 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  {.  unsigned lon
7b930 67 20 6c 6f 6e 67 20 69 6e 74 20 78 3b 0a 20 20  g long int x;.  
7b940 5f 5f 61 73 6d 5f 5f 28 22 72 64 74 73 63 5c 6e  __asm__("rdtsc\n
7b950 5c 74 22 0a 20 20 20 20 20 20 20 20 20 20 22 6d  \t".          "m
7b960 6f 76 20 25 25 65 64 78 2c 20 25 25 65 63 78 5c  ov %%edx, %%ecx\
7b970 6e 5c 74 22 0a 20 20 20 20 20 20 20 20 20 20 3a  n\t".          :
7b980 22 3d 41 22 20 28 78 29 29 3b 0a 20 20 72 65 74  "=A" (x));.  ret
7b990 75 72 6e 20 78 3b 0a 7d 0a 73 74 61 74 69 63 20  urn x;.}.static 
7b9a0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
7b9b0 6e 67 20 69 6e 74 20 67 5f 73 74 61 72 74 3b 0a  ng int g_start;.
7b9c0 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20  static unsigned 
7b9d0 69 6e 74 20 65 6c 61 70 73 65 3b 0a 23 64 65 66  int elapse;.#def
7b9e0 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20  ine TIMER_START 
7b9f0 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 68 77        g_start=hw
7ba00 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54  time().#define T
7ba10 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20  IMER_END        
7ba20 20 65 6c 61 70 73 65 3d 68 77 74 69 6d 65 28 29   elapse=hwtime()
7ba30 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65  -g_start.#define
7ba40 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
7ba50 20 20 20 65 6c 61 70 73 65 0a 23 65 6c 73 65 0a     elapse.#else.
7ba60 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54  #define TIMER_ST
7ba70 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  ART.#define TIME
7ba80 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49  R_END.#define TI
7ba90 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20  MER_ELAPSED     
7baa0 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
7bab0 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  If we compile wi
7bac0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45  th the SQLITE_TE
7bad0 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68  ST macro set, th
7bae0 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  en the following
7baf0 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64   block.** of cod
7bb00 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74  e will give us t
7bb10 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  he ability to si
7bb20 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f  mulate a disk I/
7bb30 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a  O error.  This.*
7bb40 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  * is used for te
7bb50 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65  sting the I/O re
7bb60 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f  covery logic..*/
7bb70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
7bb80 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
7bb90 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
7bba0 72 6f 72 5f 68 69 74 20 3d 20 30 3b 0a 53 51 4c  ror_hit = 0;.SQL
7bbb0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
7bbc0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
7bbd0 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45  ding = 0;.SQLITE
7bbe0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
7bbf0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73  _io_error_persis
7bc00 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50  t = 0;.SQLITE_AP
7bc10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
7bc20 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d  skfull_pending =
7bc30 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
7bc40 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
7bc50 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  ull = 0;.#define
7bc60 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
7bc70 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20  (CODE)  \.  if( 
7bc80 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
7bc90 5f 70 65 6e 64 69 6e 67 20 7c 7c 20 73 71 6c 69  _pending || sqli
7bca0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
7bcb0 20 29 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71   ) \.     if( sq
7bcc0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
7bcd0 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 5c 0a  ending-- == 1 \.
7bce0 20 20 20 20 20 20 20 20 20 7c 7c 20 28 73 71 6c           || (sql
7bcf0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
7bd00 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33  rsist && sqlite3
7bd10 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 29  _io_error_hit) )
7bd20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
7bd30 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72     { local_ioerr
7bd40 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74  (); CODE; }.stat
7bd50 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f  ic void local_io
7bd60 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45  err(){.  IOTRACE
7bd70 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20  (("IOERR\n"));. 
7bd80 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
7bd90 72 5f 68 69 74 20 3d 20 31 3b 0a 7d 0a 23 64 65  r_hit = 1;.}.#de
7bda0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
7bdb0 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29  kfullError(CODE)
7bdc0 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65   \.   if( sqlite
7bdd0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
7bde0 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28  ng ){ \.     if(
7bdf0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
7be00 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29  l_pending == 1 )
7be10 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c  { \.       local
7be20 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20  _ioerr(); \.    
7be30 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66     sqlite3_diskf
7be40 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  ull = 1; \.     
7be50 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
7be60 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20  or_hit = 1; \.  
7be70 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20       CODE; \.   
7be80 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20    }else{ \.     
7be90 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75    sqlite3_diskfu
7bea0 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a  ll_pending--; \.
7beb0 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65       } \.   }.#e
7bec0 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  lse.#define Simu
7bed0 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23  lateIOError(A).#
7bee0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
7bef0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a  iskfullError(A).
7bf00 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68  #endif../*.** Wh
7bf10 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70  en testing, keep
7bf20 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20   a count of the 
7bf30 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66  number of open f
7bf40 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  iles..*/.#ifdef 
7bf50 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
7bf60 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
7bf70 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75  e3_open_file_cou
7bf80 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  nt = 0;.#define 
7bf90 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20  OpenCounter(X)  
7bfa0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c  sqlite3_open_fil
7bfb0 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c  e_count+=(X).#el
7bfc0 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43  se.#define OpenC
7bfd0 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66  ounter(X).#endif
7bfe0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
7bff0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d  * End of os_comm
7c000 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
7c010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c030 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
7c040 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
7c050 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
7c060 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a  n os_os2.c *****
7c070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c080 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32  /../*.** The os2
7c090 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69  File structure i
7c0a0 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 4f 73  s subclass of Os
7c0b0 46 69 6c 65 20 73 70 65 63 69 66 69 63 20 66 6f  File specific fo
7c0c0 72 20 74 68 65 20 4f 53 2f 32 0a 2a 2a 20 70 72  r the OS/2.** pr
7c0d0 6f 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72 2e  otability layer.
7c0e0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
7c0f0 63 74 20 6f 73 32 46 69 6c 65 20 6f 73 32 46 69  ct os2File os2Fi
7c100 6c 65 3b 0a 73 74 72 75 63 74 20 6f 73 32 46 69  le;.struct os2Fi
7c110 6c 65 20 7b 0a 20 20 49 6f 4d 65 74 68 6f 64 20  le {.  IoMethod 
7c120 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f 64 3b 20  const *pMethod; 
7c130 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66   /* Always the f
7c140 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20  irst entry */.  
7c150 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20  HFILE h;        
7c160 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e            /* Han
7c170 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e  dle for accessin
7c180 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  g the file */.  
7c190 69 6e 74 20 64 65 6c 4f 6e 43 6c 6f 73 65 3b 20  int delOnClose; 
7c1a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
7c1b0 65 20 69 66 20 66 69 6c 65 20 69 73 20 74 6f 20  e if file is to 
7c1c0 62 65 20 64 65 6c 65 74 65 64 20 6f 6e 20 63 6c  be deleted on cl
7c1d0 6f 73 65 20 2a 2f 0a 20 20 63 68 61 72 2a 20 70  ose */.  char* p
7c1e0 61 74 68 54 6f 44 65 6c 3b 20 20 20 20 20 20 20  athToDel;       
7c1f0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69     /* Name of fi
7c200 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 6f 6e 20  le to delete on 
7c210 63 6c 6f 73 65 20 2a 2f 0a 20 20 75 6e 73 69 67  close */.  unsig
7c220 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70  ned char locktyp
7c230 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20  e;   /* Type of 
7c240 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68  lock currently h
7c250 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65  eld on this file
7c260 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 6f   */.};../*.** Do
7c270 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 61 6e 79   not include any
7c280 20 6f 66 20 74 68 65 20 46 69 6c 65 20 49 2f 4f   of the File I/O
7c290 20 69 6e 74 65 72 66 61 63 65 20 70 72 6f 63 65   interface proce
7c2a0 64 75 72 65 73 20 69 66 20 74 68 65 0a 2a 2a 20  dures if the.** 
7c2b0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
7c2c0 49 4f 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69  IO macro is defi
7c2d0 6e 65 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20  ned (indicating 
7c2e0 74 68 61 74 20 74 68 65 72 65 20 64 61 74 61 62  that there datab
7c2f0 61 73 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 69  ase.** will be i
7c300 6e 2d 6d 65 6d 6f 72 79 20 6f 6e 6c 79 29 0a 2a  n-memory only).*
7c310 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
7c320 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a  _OMIT_DISKIO../*
7c330 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e  .** Delete the n
7c340 61 6d 65 64 20 66 69 6c 65 0a 2a 2f 0a 53 51 4c  amed file.*/.SQL
7c350 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7c360 73 71 6c 69 74 65 33 4f 73 32 44 65 6c 65 74 65  sqlite3Os2Delete
7c370 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  ( const char *zF
7c380 69 6c 65 6e 61 6d 65 20 29 7b 0a 20 20 41 50 49  ilename ){.  API
7c390 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f  RET rc = NO_ERRO
7c3a0 52 3b 0a 0a 20 20 72 63 20 3d 20 44 6f 73 44 65  R;..  rc = DosDe
7c3b0 6c 65 74 65 28 20 28 50 53 5a 29 7a 46 69 6c 65  lete( (PSZ)zFile
7c3c0 6e 61 6d 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  name );.  OSTRAC
7c3d0 45 32 28 20 22 44 45 4c 45 54 45 20 5c 22 25 73  E2( "DELETE \"%s
7c3e0 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65  \"\n", zFilename
7c3f0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20   );.  return rc 
7c400 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51  == NO_ERROR ? SQ
7c410 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
7c420 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _IOERR;.}../*.**
7c430 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
7c440 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 20 65  the named file e
7c450 78 69 73 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  xists..*/.SQLITE
7c460 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
7c470 69 74 65 33 4f 73 32 46 69 6c 65 45 78 69 73 74  ite3Os2FileExist
7c480 73 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  s( const char *z
7c490 46 69 6c 65 6e 61 6d 65 20 29 7b 0a 20 20 46 49  Filename ){.  FI
7c4a0 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 33 43  LESTATUS3 fsts3C
7c4b0 6f 6e 66 69 67 49 6e 66 6f 3b 0a 20 20 6d 65 6d  onfigInfo;.  mem
7c4c0 73 65 74 28 26 66 73 74 73 33 43 6f 6e 66 69 67  set(&fsts3Config
7c4d0 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Info, 0, sizeof(
7c4e0 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29  fsts3ConfigInfo)
7c4f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 44 6f 73 51  );.  return DosQ
7c500 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20 28 50  ueryPathInfo( (P
7c510 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 2c 20 46 49  SZ)zFilename, FI
7c520 4c 5f 53 54 41 4e 44 41 52 44 2c 0a 20 20 20 20  L_STANDARD,.    
7c530 20 20 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67      &fsts3Config
7c540 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c  Info, sizeof(FIL
7c550 45 53 54 41 54 55 53 33 29 20 29 20 3d 3d 20 4e  ESTATUS3) ) == N
7c560 4f 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 20 46  O_ERROR;.}../* F
7c570 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
7c580 6f 6e 20 2a 2f 0a 69 6e 74 20 61 6c 6c 6f 63 61  on */.int alloca
7c590 74 65 4f 73 32 46 69 6c 65 28 20 6f 73 32 46 69  teOs2File( os2Fi
7c5a0 6c 65 20 2a 70 49 6e 69 74 2c 20 4f 73 46 69 6c  le *pInit, OsFil
7c5b0 65 20 2a 2a 70 6c 64 20 29 3b 0a 0a 2f 2a 0a 2a  e **pld );../*.*
7c5c0 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6f 70 65  * Attempt to ope
7c5d0 6e 20 61 20 66 69 6c 65 20 66 6f 72 20 62 6f 74  n a file for bot
7c5e0 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72  h reading and wr
7c5f0 69 74 69 6e 67 2e 20 20 49 66 20 74 68 61 74 0a  iting.  If that.
7c600 2a 2a 20 66 61 69 6c 73 2c 20 74 72 79 20 6f 70  ** fails, try op
7c610 65 6e 69 6e 67 20 69 74 20 72 65 61 64 2d 6f 6e  ening it read-on
7c620 6c 79 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65  ly.  If the file
7c630 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
7c640 0a 2a 2a 20 74 72 79 20 74 6f 20 63 72 65 61 74  .** try to creat
7c650 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73  e it..**.** On s
7c660 75 63 63 65 73 73 2c 20 61 20 68 61 6e 64 6c 65  uccess, a handle
7c670 20 66 6f 72 20 74 68 65 20 6f 70 65 6e 20 66 69   for the open fi
7c680 6c 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  le is written to
7c690 20 2a 69 64 0a 2a 2a 20 61 6e 64 20 2a 70 52 65   *id.** and *pRe
7c6a0 61 64 6f 6e 6c 79 20 69 73 20 73 65 74 20 74 6f  adonly is set to
7c6b0 20 30 20 69 66 20 74 68 65 20 66 69 6c 65 20 77   0 if the file w
7c6c0 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65  as opened for re
7c6d0 61 64 69 6e 67 20 61 6e 64 0a 2a 2a 20 77 72 69  ading and.** wri
7c6e0 74 69 6e 67 20 6f 72 20 31 20 69 66 20 74 68 65  ting or 1 if the
7c6f0 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 64   file was opened
7c700 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54 68 65   read-only.  The
7c710 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
7c720 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  s.** SQLITE_OK..
7c730 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 69 6c 75 72 65  **.** On failure
7c740 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72  , the function r
7c750 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43 41  eturns SQLITE_CA
7c760 4e 54 4f 50 45 4e 20 61 6e 64 20 6c 65 61 76 65  NTOPEN and leave
7c770 73 0a 2a 2a 20 2a 69 64 20 61 6e 64 20 2a 70 52  s.** *id and *pR
7c780 65 61 64 6f 6e 6c 79 20 75 6e 63 68 61 6e 67 65  eadonly unchange
7c790 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
7c7a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7c7b0 4f 73 32 4f 70 65 6e 52 65 61 64 57 72 69 74 65  Os2OpenReadWrite
7c7c0 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
7c7d0 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 4f 73 46  zFilename,.  OsF
7c7e0 69 6c 65 20 2a 2a 70 6c 64 2c 0a 20 20 69 6e 74  ile **pld,.  int
7c7f0 20 2a 70 52 65 61 64 6f 6e 6c 79 0a 29 7b 0a 20   *pReadonly.){. 
7c800 20 6f 73 32 46 69 6c 65 20 20 66 3b 0a 20 20 48   os2File  f;.  H
7c810 46 49 4c 45 20 20 20 20 68 66 3b 0a 20 20 55 4c  FILE    hf;.  UL
7c820 4f 4e 47 20 20 20 20 75 6c 41 63 74 69 6f 6e 3b  ONG    ulAction;
7c830 0a 20 20 41 50 49 52 45 54 20 20 20 72 63 20 3d  .  APIRET   rc =
7c840 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61 73   NO_ERROR;..  as
7c850 73 65 72 74 28 20 2a 70 6c 64 20 3d 3d 20 30 20  sert( *pld == 0 
7c860 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4f 70 65  );.  rc = DosOpe
7c870 6e 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d  n( (PSZ)zFilenam
7c880 65 2c 20 26 68 66 2c 20 26 75 6c 41 63 74 69 6f  e, &hf, &ulActio
7c890 6e 2c 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 20  n, 0L,.         
7c8a0 20 20 20 46 49 4c 45 5f 41 52 43 48 49 56 45 44     FILE_ARCHIVED
7c8b0 20 7c 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 2c 0a   | FILE_NORMAL,.
7c8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7c8d0 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41  OPEN_ACTION_CREA
7c8e0 54 45 5f 49 46 5f 4e 45 57 20 7c 20 4f 50 45 4e  TE_IF_NEW | OPEN
7c8f0 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f  _ACTION_OPEN_IF_
7c900 45 58 49 53 54 53 2c 0a 20 20 20 20 20 20 20 20  EXISTS,.        
7c910 20 20 20 20 20 20 20 20 4f 50 45 4e 5f 46 4c 41          OPEN_FLA
7c920 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52  GS_FAIL_ON_ERROR
7c930 20 7c 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41   | OPEN_FLAGS_RA
7c940 4e 44 4f 4d 20 7c 0a 20 20 20 20 20 20 20 20 20  NDOM |.         
7c950 20 20 20 20 20 20 20 20 20 20 20 4f 50 45 4e 5f             OPEN_
7c960 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 20 7c  SHARE_DENYNONE |
7c970 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41   OPEN_ACCESS_REA
7c980 44 57 52 49 54 45 2c 20 28 50 45 41 4f 50 32 29  DWRITE, (PEAOP2)
7c990 4e 55 4c 4c 20 29 3b 0a 20 20 69 66 28 20 72 63  NULL );.  if( rc
7c9a0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
7c9b0 20 20 20 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e      rc = DosOpen
7c9c0 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65  ( (PSZ)zFilename
7c9d0 2c 20 26 68 66 2c 20 26 75 6c 41 63 74 69 6f 6e  , &hf, &ulAction
7c9e0 2c 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0L,.          
7c9f0 20 20 46 49 4c 45 5f 41 52 43 48 49 56 45 44 20    FILE_ARCHIVED 
7ca00 7c 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 2c 0a 20  | FILE_NORMAL,. 
7ca10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f                 O
7ca20 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54  PEN_ACTION_CREAT
7ca30 45 5f 49 46 5f 4e 45 57 20 7c 20 4f 50 45 4e 5f  E_IF_NEW | OPEN_
7ca40 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45  ACTION_OPEN_IF_E
7ca50 58 49 53 54 53 2c 0a 20 20 20 20 20 20 20 20 20  XISTS,.         
7ca60 20 20 20 20 20 20 20 4f 50 45 4e 5f 46 4c 41 47         OPEN_FLAG
7ca70 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 20  S_FAIL_ON_ERROR 
7ca80 7c 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41 4e  | OPEN_FLAGS_RAN
7ca90 44 4f 4d 20 7c 0a 20 20 20 20 20 20 20 20 20 20  DOM |.          
7caa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
7cab0 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 57 52 49  EN_SHARE_DENYWRI
7cac0 54 45 20 7c 20 4f 50 45 4e 5f 41 43 43 45 53 53  TE | OPEN_ACCESS
7cad0 5f 52 45 41 44 4f 4e 4c 59 2c 20 28 50 45 41 4f  _READONLY, (PEAO
7cae0 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 69  P2)NULL );.    i
7caf0 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f  f( rc != NO_ERRO
7cb00 52 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  R ){.        ret
7cb10 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  urn SQLITE_CANTO
7cb20 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  PEN;.    }.    *
7cb30 70 52 65 61 64 6f 6e 6c 79 20 3d 20 31 3b 0a 20  pReadonly = 1;. 
7cb40 20 7d 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 2a   }.  else{.    *
7cb50 70 52 65 61 64 6f 6e 6c 79 20 3d 20 30 3b 0a 20  pReadonly = 0;. 
7cb60 20 7d 0a 20 20 66 2e 68 20 3d 20 68 66 3b 0a 20   }.  f.h = hf;. 
7cb70 20 66 2e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f   f.locktype = NO
7cb80 5f 4c 4f 43 4b 3b 0a 20 20 66 2e 64 65 6c 4f 6e  _LOCK;.  f.delOn
7cb90 43 6c 6f 73 65 20 3d 20 30 3b 0a 20 20 66 2e 70  Close = 0;.  f.p
7cba0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
7cbb0 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b  .  OpenCounter(+
7cbc0 31 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20  1);.  OSTRACE3( 
7cbd0 22 4f 50 45 4e 20 52 2f 57 20 25 64 20 5c 22 25  "OPEN R/W %d \"%
7cbe0 73 5c 22 5c 6e 22 2c 20 68 66 2c 20 7a 46 69 6c  s\"\n", hf, zFil
7cbf0 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72  ename );.  retur
7cc00 6e 20 61 6c 6c 6f 63 61 74 65 4f 73 32 46 69 6c  n allocateOs2Fil
7cc10 65 28 20 26 66 2c 20 70 6c 64 20 29 3b 0a 7d 0a  e( &f, pld );.}.
7cc20 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
7cc30 74 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69  to open a new fi
7cc40 6c 65 20 66 6f 72 20 65 78 63 6c 75 73 69 76 65  le for exclusive
7cc50 20 61 63 63 65 73 73 20 62 79 20 74 68 69 73 20   access by this 
7cc60 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 54 68 65 20  process..** The 
7cc70 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 70 65  file will be ope
7cc80 6e 65 64 20 66 6f 72 20 62 6f 74 68 20 72 65 61  ned for both rea
7cc90 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67  ding and writing
7cca0 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 61  .  To avoid.** a
7ccb0 20 70 6f 74 65 6e 74 69 61 6c 20 73 65 63 75 72   potential secur
7ccc0 69 74 79 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20  ity problem, we 
7ccd0 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 65  do not allow the
7cce0 20 66 69 6c 65 20 74 6f 20 68 61 76 65 0a 2a 2a   file to have.**
7ccf0 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73   previously exis
7cd00 74 65 64 2e 20 20 4e 6f 72 20 64 6f 20 77 65 20  ted.  Nor do we 
7cd10 61 6c 6c 6f 77 20 74 68 65 20 66 69 6c 65 20 74  allow the file t
7cd20 6f 20 62 65 20 61 20 73 79 6d 62 6f 6c 69 63 0a  o be a symbolic.
7cd30 2a 2a 20 6c 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 49  ** link..**.** I
7cd40 66 20 64 65 6c 46 6c 61 67 20 69 73 20 74 72 75  f delFlag is tru
7cd50 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 72 72  e, then make arr
7cd60 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 61 75 74  angements to aut
7cd70 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74  omatically delet
7cd80 65 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 77 68  e.** the file wh
7cd90 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e  en it is closed.
7cda0 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73  .**.** On succes
7cdb0 73 2c 20 77 72 69 74 65 20 74 68 65 20 66 69 6c  s, write the fil
7cdc0 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f 20 2a 69  e handle into *i
7cdd0 64 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  d and return SQL
7cde0 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  ITE_OK..**.** On
7cdf0 20 66 61 69 6c 75 72 65 2c 20 72 65 74 75 72 6e   failure, return
7ce00 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
7ce10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
7ce20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
7ce30 73 32 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28  s2OpenExclusive(
7ce40 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
7ce50 6c 65 6e 61 6d 65 2c 20 4f 73 46 69 6c 65 20 2a  lename, OsFile *
7ce60 2a 70 6c 64 2c 20 69 6e 74 20 64 65 6c 46 6c 61  *pld, int delFla
7ce70 67 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 20  g ){.  os2File  
7ce80 66 3b 0a 20 20 48 46 49 4c 45 20 20 20 20 68 66  f;.  HFILE    hf
7ce90 3b 0a 20 20 55 4c 4f 4e 47 20 20 20 20 75 6c 41  ;.  ULONG    ulA
7cea0 63 74 69 6f 6e 3b 0a 20 20 41 50 49 52 45 54 20  ction;.  APIRET 
7ceb0 20 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b    rc = NO_ERROR;
7cec0 0a 0a 20 20 61 73 73 65 72 74 28 20 2a 70 6c 64  ..  assert( *pld
7ced0 20 3d 3d 20 30 20 29 3b 0a 20 20 72 63 20 3d 20   == 0 );.  rc = 
7cee0 44 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 46  DosOpen( (PSZ)zF
7cef0 69 6c 65 6e 61 6d 65 2c 20 26 68 66 2c 20 26 75  ilename, &hf, &u
7cf00 6c 41 63 74 69 6f 6e 2c 20 30 4c 2c 20 46 49 4c  lAction, 0L, FIL
7cf10 45 5f 4e 4f 52 4d 41 4c 2c 0a 20 20 20 20 20 20  E_NORMAL,.      
7cf20 20 20 20 20 20 20 4f 50 45 4e 5f 41 43 54 49 4f        OPEN_ACTIO
7cf30 4e 5f 43 52 45 41 54 45 5f 49 46 5f 4e 45 57 20  N_CREATE_IF_NEW 
7cf40 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 52 45  | OPEN_ACTION_RE
7cf50 50 4c 41 43 45 5f 49 46 5f 45 58 49 53 54 53 2c  PLACE_IF_EXISTS,
7cf60 0a 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 45  .            OPE
7cf70 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f  N_FLAGS_FAIL_ON_
7cf80 45 52 52 4f 52 20 7c 20 4f 50 45 4e 5f 46 4c 41  ERROR | OPEN_FLA
7cf90 47 53 5f 52 41 4e 44 4f 4d 20 7c 0a 20 20 20 20  GS_RANDOM |.    
7cfa0 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 45 4e              OPEN
7cfb0 5f 53 48 41 52 45 5f 44 45 4e 59 52 45 41 44 57  _SHARE_DENYREADW
7cfc0 52 49 54 45 20 7c 20 4f 50 45 4e 5f 41 43 43 45  RITE | OPEN_ACCE
7cfd0 53 53 5f 52 45 41 44 57 52 49 54 45 2c 20 28 50  SS_READWRITE, (P
7cfe0 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20  EAOP2)NULL );.  
7cff0 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52  if( rc != NO_ERR
7d000 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
7d010 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
7d020 3b 0a 20 20 7d 0a 0a 20 20 66 2e 68 20 3d 20 68  ;.  }..  f.h = h
7d030 66 3b 0a 20 20 66 2e 6c 6f 63 6b 74 79 70 65 20  f;.  f.locktype 
7d040 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 66 2e 64  = NO_LOCK;.  f.d
7d050 65 6c 4f 6e 43 6c 6f 73 65 20 3d 20 64 65 6c 46  elOnClose = delF
7d060 6c 61 67 20 3f 20 31 20 3a 20 30 3b 0a 20 20 66  lag ? 1 : 0;.  f
7d070 2e 70 61 74 68 54 6f 44 65 6c 20 3d 20 64 65 6c  .pathToDel = del
7d080 46 6c 61 67 20 3f 20 73 71 6c 69 74 65 33 4f 73  Flag ? sqlite3Os
7d090 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 7a 46  FullPathname( zF
7d0a0 69 6c 65 6e 61 6d 65 20 29 20 3a 20 4e 55 4c 4c  ilename ) : NULL
7d0b0 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  ;.  OpenCounter(
7d0c0 20 2b 31 20 29 3b 0a 20 20 69 66 28 20 64 65 6c   +1 );.  if( del
7d0d0 46 6c 61 67 20 29 20 44 6f 73 46 6f 72 63 65 44  Flag ) DosForceD
7d0e0 65 6c 65 74 65 28 20 28 50 53 5a 29 73 71 6c 69  elete( (PSZ)sqli
7d0f0 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d  te3OsFullPathnam
7d100 65 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 20 29  e( zFilename ) )
7d110 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 4f  ;.  OSTRACE3( "O
7d120 50 45 4e 20 45 58 20 25 64 20 5c 22 25 73 5c 22  PEN EX %d \"%s\"
7d130 5c 6e 22 2c 20 68 66 2c 20 73 71 6c 69 74 65 33  \n", hf, sqlite3
7d140 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 28  OsFullPathname (
7d150 20 7a 46 69 6c 65 6e 61 6d 65 20 29 20 29 3b 0a   zFilename ) );.
7d160 20 20 72 65 74 75 72 6e 20 61 6c 6c 6f 63 61 74    return allocat
7d170 65 4f 73 32 46 69 6c 65 28 20 26 66 2c 20 70 6c  eOs2File( &f, pl
7d180 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74  d );.}../*.** At
7d190 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20  tempt to open a 
7d1a0 6e 65 77 20 66 69 6c 65 20 66 6f 72 20 72 65 61  new file for rea
7d1b0 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2e 0a 2a  d-only access..*
7d1c0 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c  *.** On success,
7d1d0 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20   write the file 
7d1e0 68 61 6e 64 6c 65 20 69 6e 74 6f 20 2a 69 64 20  handle into *id 
7d1f0 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
7d200 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66  E_OK..**.** On f
7d210 61 69 6c 75 72 65 2c 20 72 65 74 75 72 6e 20 53  ailure, return S
7d220 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 2e 0a  QLITE_CANTOPEN..
7d230 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
7d240 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 32  E int sqlite3Os2
7d250 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 20 63 6f  OpenReadOnly( co
7d260 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
7d270 61 6d 65 2c 20 4f 73 46 69 6c 65 20 2a 2a 70 6c  ame, OsFile **pl
7d280 64 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 20  d ){.  os2File  
7d290 66 3b 0a 20 20 48 46 49 4c 45 20 20 20 20 68 66  f;.  HFILE    hf
7d2a0 3b 0a 20 20 55 4c 4f 4e 47 20 20 20 20 75 6c 41  ;.  ULONG    ulA
7d2b0 63 74 69 6f 6e 3b 0a 20 20 41 50 49 52 45 54 20  ction;.  APIRET 
7d2c0 20 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b    rc = NO_ERROR;
7d2d0 0a 0a 20 20 61 73 73 65 72 74 28 20 2a 70 6c 64  ..  assert( *pld
7d2e0 20 3d 3d 20 30 20 29 3b 0a 20 20 72 63 20 3d 20   == 0 );.  rc = 
7d2f0 44 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 46  DosOpen( (PSZ)zF
7d300 69 6c 65 6e 61 6d 65 2c 20 26 68 66 2c 20 26 75  ilename, &hf, &u
7d310 6c 41 63 74 69 6f 6e 2c 20 30 4c 2c 0a 20 20 20  lAction, 0L,.   
7d320 20 20 20 20 20 20 20 20 20 46 49 4c 45 5f 4e 4f           FILE_NO
7d330 52 4d 41 4c 2c 20 4f 50 45 4e 5f 41 43 54 49 4f  RMAL, OPEN_ACTIO
7d340 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53  N_OPEN_IF_EXISTS
7d350 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 4f 50  ,.            OP
7d360 45 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e  EN_FLAGS_FAIL_ON
7d370 5f 45 52 52 4f 52 20 7c 20 4f 50 45 4e 5f 46 4c  _ERROR | OPEN_FL
7d380 41 47 53 5f 52 41 4e 44 4f 4d 20 7c 0a 20 20 20  AGS_RANDOM |.   
7d390 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 45               OPE
7d3a0 4e 5f 53 48 41 52 45 5f 44 45 4e 59 57 52 49 54  N_SHARE_DENYWRIT
7d3b0 45 20 7c 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f  E | OPEN_ACCESS_
7d3c0 52 45 41 44 4f 4e 4c 59 2c 20 28 50 45 41 4f 50  READONLY, (PEAOP
7d3d0 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 69 66 28 20  2)NULL );.  if( 
7d3e0 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  rc != NO_ERROR )
7d3f0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
7d400 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20  ITE_CANTOPEN;.  
7d410 7d 0a 20 20 66 2e 68 20 3d 20 68 66 3b 0a 20 20  }.  f.h = hf;.  
7d420 66 2e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f  f.locktype = NO_
7d430 4c 4f 43 4b 3b 0a 20 20 66 2e 64 65 6c 4f 6e 43  LOCK;.  f.delOnC
7d440 6c 6f 73 65 20 3d 20 30 3b 0a 20 20 66 2e 70 61  lose = 0;.  f.pa
7d450 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a  thToDel = NULL;.
7d460 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 20 2b    OpenCounter( +
7d470 31 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  1 );.  OSTRACE3(
7d480 20 22 4f 50 45 4e 20 52 4f 20 25 64 20 5c 22 25   "OPEN RO %d \"%
7d490 73 5c 22 5c 6e 22 2c 20 68 66 2c 20 7a 46 69 6c  s\"\n", hf, zFil
7d4a0 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72  ename );.  retur
7d4b0 6e 20 61 6c 6c 6f 63 61 74 65 4f 73 32 46 69 6c  n allocateOs2Fil
7d4c0 65 28 20 26 66 2c 20 70 6c 64 20 29 3b 0a 7d 0a  e( &f, pld );.}.
7d4d0 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
7d4e0 6f 20 6f 70 65 6e 20 61 20 66 69 6c 65 20 64 65  o open a file de
7d4f0 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65  scriptor for the
7d500 20 64 69 72 65 63 74 6f 72 79 20 74 68 61 74 20   directory that 
7d510 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 66 69  contains a.** fi
7d520 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65 20 64  le.  This file d
7d530 65 73 63 72 69 70 74 6f 72 20 63 61 6e 20 62 65  escriptor can be
7d540 20 75 73 65 64 20 74 6f 20 66 73 79 6e 63 28 29   used to fsync()
7d550 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a 2a   the directory.*
7d560 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61  * in order to ma
7d570 6b 65 20 73 75 72 65 20 74 68 65 20 63 72 65 61  ke sure the crea
7d580 74 69 6f 6e 20 6f 66 20 61 20 6e 65 77 20 66 69  tion of a new fi
7d590 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 77  le is actually w
7d5a0 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 64 69 73  ritten.** to dis
7d5b0 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  k..**.** This ro
7d5c0 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 6d 65  utine is only me
7d5d0 61 6e 69 6e 67 66 75 6c 20 66 6f 72 20 55 6e 69  aningful for Uni
7d5e0 78 2e 20 20 49 74 20 69 73 20 61 20 6e 6f 2d 6f  x.  It is a no-o
7d5f0 70 20 75 6e 64 65 72 0a 2a 2a 20 4f 53 2f 32 20  p under.** OS/2 
7d600 73 69 6e 63 65 20 4f 53 2f 32 20 64 6f 65 73 20  since OS/2 does 
7d610 6e 6f 74 20 73 75 70 70 6f 72 74 20 68 61 72 64  not support hard
7d620 20 6c 69 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e   links..**.** On
7d630 20 73 75 63 63 65 73 73 2c 20 61 20 68 61 6e 64   success, a hand
7d640 6c 65 20 66 6f 72 20 61 20 70 72 65 76 69 6f 75  le for a previou
7d650 73 6c 79 20 6f 70 65 6e 20 66 69 6c 65 20 69 73  sly open file is
7d660 20 61 74 20 2a 69 64 20 69 73 0a 2a 2a 20 75 70   at *id is.** up
7d670 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e  dated with the n
7d680 65 77 20 64 69 72 65 63 74 6f 72 79 20 66 69 6c  ew directory fil
7d690 65 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64  e descriptor and
7d6a0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 0a 2a 2a   SQLITE_OK is.**
7d6b0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
7d6c0 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20 74 68 65   On failure, the
7d6d0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
7d6e0 73 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  s SQLITE_CANTOPE
7d6f0 4e 20 61 6e 64 20 6c 65 61 76 65 73 0a 2a 2a 20  N and leaves.** 
7d700 2a 69 64 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a  *id unchanged..*
7d710 2f 0a 69 6e 74 20 6f 73 32 4f 70 65 6e 44 69 72  /.int os2OpenDir
7d720 65 63 74 6f 72 79 28 0a 20 20 4f 73 46 69 6c 65  ectory(.  OsFile
7d730 20 2a 69 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68   *id,.  const ch
7d740 61 72 20 2a 7a 44 69 72 6e 61 6d 65 0a 29 7b 0a  ar *zDirname.){.
7d750 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
7d760 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  OK;.}../*.** Cre
7d770 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ate a temporary 
7d780 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75  file name in zBu
7d790 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65  f.  zBuf must be
7d7a0 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a   big enough to.*
7d7b0 2a 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20  * hold at least 
7d7c0 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f  SQLITE_TEMPNAME_
7d7d0 53 49 5a 45 20 63 68 61 72 61 63 74 65 72 73 2e  SIZE characters.
7d7e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
7d7f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
7d800 32 54 65 6d 70 46 69 6c 65 4e 61 6d 65 28 20 63  2TempFileName( c
7d810 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20 73  har *zBuf ){.  s
7d820 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
7d830 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72 73  gned char zChars
7d840 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66  [] =.    "abcdef
7d850 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76  ghijklmnopqrstuv
7d860 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45  wxyz".    "ABCDE
7d870 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55  FGHIJKLMNOPQRSTU
7d880 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33  VWXYZ".    "0123
7d890 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 74 20 69  456789";.  int i
7d8a0 2c 20 6a 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70  , j;.  PSZ zTemp
7d8b0 50 61 74 68 20 3d 20 30 3b 0a 20 20 69 66 28 20  Path = 0;.  if( 
7d8c0 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a  DosScanEnv( (PSZ
7d8d0 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50  )"TEMP", &zTempP
7d8e0 61 74 68 20 29 20 29 7b 0a 20 20 20 20 69 66 28  ath ) ){.    if(
7d8f0 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53   DosScanEnv( (PS
7d900 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70 50  Z)"TMP", &zTempP
7d910 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 69  ath ) ){.      i
7d920 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28  f( DosScanEnv( (
7d930 50 53 5a 29 22 54 4d 50 44 49 52 22 2c 20 26 7a  PSZ)"TMPDIR", &z
7d940 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20  TempPath ) ){.  
7d950 20 20 20 20 20 20 20 20 20 55 4c 4f 4e 47 20 75           ULONG u
7d960 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30 2c 20 75  lDriveNum = 0, u
7d970 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 3b 0a 20  lDriveMap = 0;. 
7d980 20 20 20 20 20 20 20 20 20 20 44 6f 73 51 75 65            DosQue
7d990 72 79 43 75 72 72 65 6e 74 44 69 73 6b 28 20 26  ryCurrentDisk( &
7d9a0 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 75 6c 44  ulDriveNum, &ulD
7d9b0 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 20 20 20  riveMap );.     
7d9c0 20 20 20 20 20 20 73 70 72 69 6e 74 66 28 20 28        sprintf( (
7d9d0 63 68 61 72 2a 29 7a 54 65 6d 70 50 61 74 68 2c  char*)zTempPath,
7d9e0 20 22 25 63 3a 22 2c 20 28 63 68 61 72 29 28 20   "%c:", (char)( 
7d9f0 27 41 27 20 2b 20 75 6c 44 72 69 76 65 4e 75 6d  'A' + ulDriveNum
7da00 20 2d 20 31 20 29 20 29 3b 0a 20 20 20 20 20 20   - 1 ) );.      
7da10 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  }.    }.  }.  /*
7da20 20 73 74 72 69 70 20 6f 66 66 20 61 20 74 72 61   strip off a tra
7da30 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72  iling slashes or
7da40 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74   backslashes, ot
7da50 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64  herwise we would
7da60 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74   get *.   * mult
7da70 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68  iple (back)slash
7da80 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20  es which causes 
7da90 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69  DosOpen() to fai
7daa0 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
7dab0 2a 2f 0a 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28  */.  j = strlen(
7dac0 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 77 68  zTempPath);.  wh
7dad0 69 6c 65 28 20 6a 20 3e 20 30 20 26 26 20 7a 54  ile( j > 0 && zT
7dae0 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20  empPath[j-1] == 
7daf0 27 5c 5c 27 20 7c 7c 20 7a 54 65 6d 70 50 61 74  '\\' || zTempPat
7db00 68 5b 6a 2d 31 5d 20 3d 3d 20 27 2f 27 20 29 7b  h[j-1] == '/' ){
7db10 0a 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 7d 0a  .      j--;.  }.
7db20 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d 20 3d    zTempPath[j] =
7db30 20 27 5c 30 27 3b 0a 20 20 66 6f 72 28 3b 3b 29   '\0';.  for(;;)
7db40 7b 0a 20 20 20 20 20 20 73 70 72 69 6e 74 66 28  {.      sprintf(
7db50 20 7a 42 75 66 2c 20 22 25 73 5c 5c 22 54 45 4d   zBuf, "%s\\"TEM
7db60 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
7db70 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 20 20  TempPath );.    
7db80 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28 20 7a 42    j = strlen( zB
7db90 75 66 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  uf );.      sqli
7dba0 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28 20 31  te3Randomness( 1
7dbb0 35 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20  5, &zBuf[j] );. 
7dbc0 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30 3b       for( i = 0;
7dbd0 20 69 20 3c 20 31 35 3b 20 69 2b 2b 2c 20 6a 2b   i < 15; i++, j+
7dbe0 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  + ){.        zBu
7dbf0 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68  f[j] = (char)zCh
7dc00 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20  ars[ ((unsigned 
7dc10 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73  char)zBuf[j])%(s
7dc20 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29  izeof(zChars)-1)
7dc30 20 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   ];.      }.    
7dc40 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20    zBuf[j] = 0;. 
7dc50 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
7dc60 33 4f 73 46 69 6c 65 45 78 69 73 74 73 28 20 7a  3OsFileExists( z
7dc70 42 75 66 20 29 20 29 20 62 72 65 61 6b 3b 0a 20  Buf ) ) break;. 
7dc80 20 7d 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22   }.  OSTRACE2( "
7dc90 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25  TEMP FILENAME: %
7dca0 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20  s\n", zBuf );.  
7dcb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
7dcc0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
7dcd0 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 69 6e 74 20   a file..*/.int 
7dce0 6f 73 32 43 6c 6f 73 65 28 20 4f 73 46 69 6c 65  os2Close( OsFile
7dcf0 20 2a 2a 70 6c 64 20 29 7b 0a 20 20 6f 73 32 46   **pld ){.  os2F
7dd00 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 41 50  ile *pFile;.  AP
7dd10 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52  IRET rc = NO_ERR
7dd20 4f 52 3b 0a 20 20 69 66 28 20 70 6c 64 20 26 26  OR;.  if( pld &&
7dd30 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69   (pFile = (os2Fi
7dd40 6c 65 2a 29 2a 70 6c 64 29 20 21 3d 20 30 20 29  le*)*pld) != 0 )
7dd50 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 20  {.    OSTRACE2( 
7dd60 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46  "CLOSE %d\n", pF
7dd70 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63  ile->h );.    rc
7dd80 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 69   = DosClose( pFi
7dd90 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 69  le->h );.    pFi
7dda0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e  le->locktype = N
7ddb0 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20  O_LOCK;.    if( 
7ddc0 70 46 69 6c 65 2d 3e 64 65 6c 4f 6e 43 6c 6f 73  pFile->delOnClos
7ddd0 65 20 21 3d 20 30 20 29 7b 0a 20 20 20 20 20 20  e != 0 ){.      
7dde0 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 44    rc = DosForceD
7ddf0 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69 6c  elete( (PSZ)pFil
7de00 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a  e->pathToDel );.
7de10 20 20 20 20 7d 0a 20 20 20 20 2a 70 6c 64 20 3d      }.    *pld =
7de20 20 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e   0;.    OpenCoun
7de30 74 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a  ter( -1 );.  }..
7de40 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e    return rc == N
7de50 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45  O_ERROR ? SQLITE
7de60 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
7de70 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  RR;.}../*.** Rea
7de80 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69  d data from a fi
7de90 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72  le into a buffer
7dea0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
7deb0 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79  _OK if all.** by
7dec0 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75  tes were read su
7ded0 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53  ccessfully and S
7dee0 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61  QLITE_IOERR if a
7def0 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20  nything goes.** 
7df00 77 72 6f 6e 67 2e 0a 2a 2f 0a 69 6e 74 20 6f 73  wrong..*/.int os
7df10 32 52 65 61 64 28 20 4f 73 46 69 6c 65 20 2a 69  2Read( OsFile *i
7df20 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69  d, void *pBuf, i
7df30 6e 74 20 61 6d 74 20 29 7b 0a 20 20 55 4c 4f 4e  nt amt ){.  ULON
7df40 47 20 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28  G got;.  assert(
7df50 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75   id!=0 );.  Simu
7df60 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
7df70 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
7df80 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20   );.  OSTRACE3( 
7df90 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64  "READ %d lock=%d
7dfa0 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29  \n", ((os2File*)
7dfb0 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32 46 69 6c  id)->h, ((os2Fil
7dfc0 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65  e*)id)->locktype
7dfd0 20 29 3b 0a 20 20 44 6f 73 52 65 61 64 28 20 28   );.  DosRead( (
7dfe0 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68  (os2File*)id)->h
7dff0 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f  , pBuf, amt, &go
7e000 74 20 29 3b 0a 20 20 69 66 20 28 67 6f 74 20 3d  t );.  if (got =
7e010 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 29 0a 20 20  = (ULONG)amt).  
7e020 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
7e030 4f 4b 3b 0a 20 20 65 6c 73 65 20 69 66 20 28 67  OK;.  else if (g
7e040 6f 74 20 3d 3d 20 30 29 0a 20 20 20 20 72 65 74  ot == 0).    ret
7e050 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
7e060 5f 52 45 41 44 3b 0a 20 20 65 6c 73 65 20 7b 0a  _READ;.  else {.
7e070 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68      memset(&((ch
7e080 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20  ar*)pBuf)[got], 
7e090 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20  0, amt-got);.   
7e0a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
7e0b0 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b  OERR_SHORT_READ;
7e0c0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  .  }.}../*.** Wr
7e0d0 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ite data from a 
7e0e0 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69  buffer into a fi
7e0f0 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  le.  Return SQLI
7e100 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
7e110 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65  .** or some othe
7e120 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20  r error code on 
7e130 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 69 6e 74 20  failure..*/.int 
7e140 6f 73 32 57 72 69 74 65 28 20 4f 73 46 69 6c 65  os2Write( OsFile
7e150 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *id, const void
7e160 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 20   *pBuf, int amt 
7e170 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  ){.  APIRET rc =
7e180 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f   NO_ERROR;.  ULO
7e190 4e 47 20 77 72 6f 74 65 3b 0a 20 20 61 73 73 65  NG wrote;.  asse
7e1a0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53  rt( id!=0 );.  S
7e1b0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
7e1c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
7e1d0 45 52 52 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  ERR );.  Simulat
7e1e0 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20  eDiskfullError( 
7e1f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
7e200 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  LL );.  OSTRACE3
7e210 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b  ( "WRITE %d lock
7e220 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c  =%d\n", ((os2Fil
7e230 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32  e*)id)->h, ((os2
7e240 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74  File*)id)->lockt
7e250 79 70 65 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  ype );.  while( 
7e260 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20  amt > 0 &&.     
7e270 20 28 72 63 20 3d 20 44 6f 73 57 72 69 74 65 28   (rc = DosWrite(
7e280 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d   ((os2File*)id)-
7e290 3e 68 2c 20 28 50 56 4f 49 44 29 70 42 75 66 2c  >h, (PVOID)pBuf,
7e2a0 20 61 6d 74 2c 20 26 77 72 6f 74 65 20 29 29 20   amt, &wrote )) 
7e2b0 26 26 20 77 72 6f 74 65 20 3e 20 30 20 29 7b 0a  && wrote > 0 ){.
7e2c0 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f        amt -= wro
7e2d0 74 65 3b 0a 20 20 20 20 20 20 70 42 75 66 20 3d  te;.      pBuf =
7e2e0 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b   &((char*)pBuf)[
7e2f0 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72  wrote];.  }..  r
7e300 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f  eturn ( rc != NO
7e310 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20  _ERROR || amt > 
7e320 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53  (int)wrote ) ? S
7e330 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c  QLITE_FULL : SQL
7e340 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
7e350 20 4d 6f 76 65 20 74 68 65 20 72 65 61 64 2f 77   Move the read/w
7e360 72 69 74 65 20 70 6f 69 6e 74 65 72 20 69 6e 20  rite pointer in 
7e370 61 20 66 69 6c 65 2e 0a 2a 2f 0a 69 6e 74 20 6f  a file..*/.int o
7e380 73 32 53 65 65 6b 28 20 4f 73 46 69 6c 65 20 2a  s2Seek( OsFile *
7e390 69 64 2c 20 69 36 34 20 6f 66 66 73 65 74 20 29  id, i64 offset )
7e3a0 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  {.  APIRET rc = 
7e3b0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e  NO_ERROR;.  ULON
7e3c0 47 20 66 69 6c 65 50 6f 69 6e 74 65 72 20 3d 20  G filePointer = 
7e3d0 30 4c 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  0L;.  assert( id
7e3e0 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f  !=0 );.  rc = Do
7e3f0 73 53 65 74 46 69 6c 65 50 74 72 28 20 28 28 6f  sSetFilePtr( ((o
7e400 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  s2File*)id)->h, 
7e410 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 45 47  offset, FILE_BEG
7e420 49 4e 2c 20 26 66 69 6c 65 50 6f 69 6e 74 65 72  IN, &filePointer
7e430 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20   );.  OSTRACE3( 
7e440 22 53 45 45 4b 20 25 64 20 25 6c 6c 64 5c 6e 22  "SEEK %d %lld\n"
7e450 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29  , ((os2File*)id)
7e460 2d 3e 68 2c 20 6f 66 66 73 65 74 20 29 3b 0a 20  ->h, offset );. 
7e470 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
7e480 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
7e490 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
7e4a0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  R;.}../*.** Make
7e4b0 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73   sure all writes
7e4c0 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72   to a particular
7e4d0 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74   file are commit
7e4e0 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a  ted to disk..*/.
7e4f0 69 6e 74 20 6f 73 32 53 79 6e 63 28 20 4f 73 46  int os2Sync( OsF
7e500 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 64 61 74  ile *id, int dat
7e510 61 4f 6e 6c 79 20 29 7b 0a 20 20 61 73 73 65 72  aOnly ){.  asser
7e520 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53  t( id!=0 );.  OS
7e530 54 52 41 43 45 33 28 20 22 53 59 4e 43 20 25 64  TRACE3( "SYNC %d
7e540 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f   lock=%d\n", ((o
7e550 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  s2File*)id)->h, 
7e560 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e  ((os2File*)id)->
7e570 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 72 65  locktype );.  re
7e580 74 75 72 6e 20 44 6f 73 52 65 73 65 74 42 75 66  turn DosResetBuf
7e590 66 65 72 28 20 28 28 6f 73 32 46 69 6c 65 2a 29  fer( ((os2File*)
7e5a0 69 64 29 2d 3e 68 20 29 20 3d 3d 20 4e 4f 5f 45  id)->h ) == NO_E
7e5b0 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  RROR ? SQLITE_OK
7e5c0 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
7e5d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74  .}../*.** Sync t
7e5e0 68 65 20 64 69 72 65 63 74 6f 72 79 20 7a 44 69  he directory zDi
7e5f0 72 6e 61 6d 65 2e 20 54 68 69 73 20 69 73 20 61  rname. This is a
7e600 20 6e 6f 2d 6f 70 20 6f 6e 20 6f 70 65 72 61 74   no-op on operat
7e610 69 6e 67 20 73 79 73 74 65 6d 73 20 6f 74 68 65  ing systems othe
7e620 72 0a 2a 2a 20 74 68 61 6e 20 55 4e 49 58 2e 0a  r.** than UNIX..
7e630 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
7e640 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 32  E int sqlite3Os2
7e650 53 79 6e 63 44 69 72 65 63 74 6f 72 79 28 20 63  SyncDirectory( c
7e660 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 6e  onst char *zDirn
7e670 61 6d 65 20 29 7b 0a 20 20 53 69 6d 75 6c 61 74  ame ){.  Simulat
7e680 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
7e690 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b   SQLITE_IOERR );
7e6a0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
7e6b0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72  _OK;.}../*.** Tr
7e6c0 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66  uncate an open f
7e6d0 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69  ile to a specifi
7e6e0 65 64 20 73 69 7a 65 0a 2a 2f 0a 69 6e 74 20 6f  ed size.*/.int o
7e6f0 73 32 54 72 75 6e 63 61 74 65 28 20 4f 73 46 69  s2Truncate( OsFi
7e700 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74  le *id, i64 nByt
7e710 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63  e ){.  APIRET rc
7e720 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55   = NO_ERROR;.  U
7e730 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d  LONG upperBits =
7e740 20 6e 42 79 74 65 3e 3e 33 32 3b 0a 20 20 61 73   nByte>>32;.  as
7e750 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
7e760 20 4f 53 54 52 41 43 45 33 28 20 22 54 52 55 4e   OSTRACE3( "TRUN
7e770 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c  CATE %d %lld\n",
7e780 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d   ((os2File*)id)-
7e790 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 53  >h, nByte );.  S
7e7a0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
7e7b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
7e7c0 45 52 52 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f  ERR );.  rc = Do
7e7d0 73 53 65 74 46 69 6c 65 50 74 72 28 20 28 28 6f  sSetFilePtr( ((o
7e7e0 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  s2File*)id)->h, 
7e7f0 6e 42 79 74 65 2c 20 46 49 4c 45 5f 42 45 47 49  nByte, FILE_BEGI
7e800 4e 2c 20 26 75 70 70 65 72 42 69 74 73 20 29 3b  N, &upperBits );
7e810 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f  .  if( rc != NO_
7e820 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74  ERROR ){.    ret
7e830 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
7e840 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 44 6f 73  ;.  }.  rc = Dos
7e850 53 65 74 46 69 6c 65 50 74 72 28 20 28 28 6f 73  SetFilePtr( ((os
7e860 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 30  2File*)id)->h, 0
7e870 4c 2c 20 46 49 4c 45 5f 45 4e 44 2c 20 26 75 70  L, FILE_END, &up
7e880 70 65 72 42 69 74 73 20 29 3b 0a 20 20 72 65 74  perBits );.  ret
7e890 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52  urn rc == NO_ERR
7e8a0 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  OR ? SQLITE_OK :
7e8b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d   SQLITE_IOERR;.}
7e8c0 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e  ../*.** Determin
7e8d0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  e the current si
7e8e0 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20  ze of a file in 
7e8f0 62 79 74 65 73 0a 2a 2f 0a 69 6e 74 20 6f 73 32  bytes.*/.int os2
7e900 46 69 6c 65 53 69 7a 65 28 20 4f 73 46 69 6c 65  FileSize( OsFile
7e910 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a 65   *id, i64 *pSize
7e920 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20   ){.  APIRET rc 
7e930 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49  = NO_ERROR;.  FI
7e940 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 33 46  LESTATUS3 fsts3F
7e950 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65  ileInfo;.  memse
7e960 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f  t(&fsts3FileInfo
7e970 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73  , 0, sizeof(fsts
7e980 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61  3FileInfo));.  a
7e990 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
7e9a0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
7e9b0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
7e9c0 5f 49 4f 45 52 52 20 29 3b 0a 20 20 72 63 20 3d  _IOERR );.  rc =
7e9d0 20 44 6f 73 51 75 65 72 79 46 69 6c 65 49 6e 66   DosQueryFileInf
7e9e0 6f 28 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64  o( ((os2File*)id
7e9f0 29 2d 3e 68 2c 20 46 49 4c 5f 53 54 41 4e 44 41  )->h, FIL_STANDA
7ea00 52 44 2c 20 26 66 73 74 73 33 46 69 6c 65 49 6e  RD, &fsts3FileIn
7ea10 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53  fo, sizeof(FILES
7ea20 54 41 54 55 53 33 29 20 29 3b 0a 20 20 69 66 28  TATUS3) );.  if(
7ea30 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc == NO_ERROR 
7ea40 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20  ){.    *pSize = 
7ea50 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2e 63 62  fsts3FileInfo.cb
7ea60 46 69 6c 65 3b 0a 20 20 20 20 72 65 74 75 72 6e  File;.    return
7ea70 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
7ea80 20 20 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75    else{.    retu
7ea90 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
7eaa0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63  .  }.}../*.** Ac
7eab0 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20 6c  quire a reader l
7eac0 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ock..*/.static i
7ead0 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 20  nt getReadLock( 
7eae0 6f 73 32 46 69 6c 65 20 2a 69 64 20 29 7b 0a 20  os2File *id ){. 
7eaf0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
7eb00 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
7eb10 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6d   UnlockArea;.  m
7eb20 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c  emset(&LockArea,
7eb30 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41   0, sizeof(LockA
7eb40 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  rea));.  memset(
7eb50 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  &UnlockArea, 0, 
7eb60 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65  sizeof(UnlockAre
7eb70 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e  a));.  LockArea.
7eb80 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44  lOffset = SHARED
7eb90 5f 46 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72  _FIRST;.  LockAr
7eba0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52  ea.lRange = SHAR
7ebb0 45 44 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63  ED_SIZE;.  Unloc
7ebc0 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
7ebd0 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61  0L;.  UnlockArea
7ebe0 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
7ebf0 72 65 74 75 72 6e 20 44 6f 73 53 65 74 46 69 6c  return DosSetFil
7ec00 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20 26  eLocks( id->h, &
7ec10 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
7ec20 6b 41 72 65 61 2c 20 32 30 30 30 4c 2c 20 31 4c  kArea, 2000L, 1L
7ec30 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64   );.}../*.** Und
7ec40 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a  o a readlock.*/.
7ec50 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63  static int unloc
7ec60 6b 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69  kReadLock( os2Fi
7ec70 6c 65 20 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45  le *id ){.  FILE
7ec80 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a  LOCK  LockArea,.
7ec90 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f              Unlo
7eca0 63 6b 41 72 65 61 3b 0a 20 20 6d 65 6d 73 65 74  ckArea;.  memset
7ecb0 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73  (&LockArea, 0, s
7ecc0 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29  izeof(LockArea))
7ecd0 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f  ;.  memset(&Unlo
7ece0 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
7ecf0 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a  f(UnlockArea));.
7ed00 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
7ed10 65 74 20 3d 20 30 4c 3b 0a 20 20 4c 6f 63 6b 41  et = 0L;.  LockA
7ed20 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b  rea.lRange = 0L;
7ed30 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f  .  UnlockArea.lO
7ed40 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
7ed50 49 52 53 54 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72  IRST;.  UnlockAr
7ed60 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52  ea.lRange = SHAR
7ed70 45 44 5f 53 49 5a 45 3b 0a 20 20 72 65 74 75 72  ED_SIZE;.  retur
7ed80 6e 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  n DosSetFileLock
7ed90 73 28 20 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f 63  s( id->h, &Unloc
7eda0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
7edb0 2c 20 32 30 30 30 4c 2c 20 31 4c 20 29 3b 0a 7d  , 2000L, 1L );.}
7edc0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
7edd0 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
7ede0 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  MAS./*.** Check 
7edf0 74 68 61 74 20 61 20 67 69 76 65 6e 20 70 61 74  that a given pat
7ee00 68 6e 61 6d 65 20 69 73 20 61 20 64 69 72 65 63  hname is a direc
7ee10 74 6f 72 79 20 61 6e 64 20 69 73 20 77 72 69 74  tory and is writ
7ee20 61 62 6c 65 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  able.**.*/.SQLIT
7ee30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7ee40 6c 69 74 65 33 4f 73 32 49 73 44 69 72 57 72 69  lite3Os2IsDirWri
7ee50 74 61 62 6c 65 28 20 63 68 61 72 20 2a 7a 44 69  table( char *zDi
7ee60 72 6e 61 6d 65 20 29 7b 0a 20 20 46 49 4c 45 53  rname ){.  FILES
7ee70 54 41 54 55 53 33 20 66 73 74 73 33 43 6f 6e 66  TATUS3 fsts3Conf
7ee80 69 67 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54  igInfo;.  APIRET
7ee90 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
7eea0 20 20 6d 65 6d 73 65 74 28 26 66 73 74 73 33 43    memset(&fsts3C
7eeb0 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73 69  onfigInfo, 0, si
7eec0 7a 65 6f 66 28 66 73 74 73 33 43 6f 6e 66 69 67  zeof(fsts3Config
7eed0 49 6e 66 6f 29 29 3b 0a 20 20 69 66 28 20 7a 44  Info));.  if( zD
7eee0 69 72 6e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75  irname==0 ) retu
7eef0 72 6e 20 30 3b 0a 20 20 69 66 28 20 73 74 72 6c  rn 0;.  if( strl
7ef00 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3e 43 43 48  en(zDirname)>CCH
7ef10 4d 41 58 50 41 54 48 20 29 20 72 65 74 75 72 6e  MAXPATH ) return
7ef20 20 30 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75   0;.  rc = DosQu
7ef30 65 72 79 50 61 74 68 49 6e 66 6f 28 20 28 50 53  eryPathInfo( (PS
7ef40 5a 29 7a 44 69 72 6e 61 6d 65 2c 20 46 49 4c 5f  Z)zDirname, FIL_
7ef50 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33  STANDARD, &fsts3
7ef60 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a 65  ConfigInfo, size
7ef70 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20  of(FILESTATUS3) 
7ef80 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e  );.  if( rc != N
7ef90 4f 5f 45 52 52 4f 52 20 29 20 72 65 74 75 72 6e  O_ERROR ) return
7efa0 20 30 3b 0a 20 20 69 66 28 20 28 66 73 74 73 33   0;.  if( (fsts3
7efb0 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46  ConfigInfo.attrF
7efc0 69 6c 65 20 26 20 46 49 4c 45 5f 44 49 52 45 43  ile & FILE_DIREC
7efd0 54 4f 52 59 29 20 21 3d 20 46 49 4c 45 5f 44 49  TORY) != FILE_DI
7efe0 52 45 43 54 4f 52 59 20 29 20 72 65 74 75 72 6e  RECTORY ) return
7eff0 20 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b   0;..  return 1;
7f000 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
7f010 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
7f020 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  RAGMAS */../*.**
7f030 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77   Lock the file w
7f040 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65  ith the lock spe
7f050 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65  cified by parame
7f060 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f  ter locktype - o
7f070 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ne.** of the fol
7f080 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
7f090 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43    (1) SHARED_LOC
7f0a0 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53  K.**     (2) RES
7f0b0 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  ERVED_LOCK.**   
7f0c0 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f    (3) PENDING_LO
7f0d0 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58  CK.**     (4) EX
7f0e0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a  CLUSIVE_LOCK.**.
7f0f0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65  ** Sometimes whe
7f100 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65  n requesting one
7f110 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64   lock state, add
7f120 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61  itional lock sta
7f130 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72  tes.** are inser
7f140 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20  ted in between. 
7f150 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67   The locking mig
7f160 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f  ht fail on one o
7f170 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74  f the later.** t
7f180 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69  ransitions leavi
7f190 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ng the lock stat
7f1a0 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
7f1b0 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64   what it started
7f1c0 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68   but.** still sh
7f1d0 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e  ort of its goal.
7f1e0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
7f1f0 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20  chart shows the 
7f200 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73  allowed.** trans
7f210 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69  itions and the i
7f220 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64  nserted intermed
7f230 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  iate states:.**.
7f240 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d  **    UNLOCKED -
7f250 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53  > SHARED.**    S
7f260 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45  HARED -> RESERVE
7f270 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
7f280 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
7f290 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52  XCLUSIVE.**    R
7f2a0 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44  ESERVED -> (PEND
7f2b0 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
7f2c0 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20  E.**    PENDING 
7f2d0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a  -> EXCLUSIVE.**.
7f2e0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
7f2f0 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61  will only increa
7f300 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65 20  se a lock.  The 
7f310 6f 73 32 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 74  os2Unlock() rout
7f320 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 61 6c  ine.** erases al
7f330 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 65 20  l locks at once 
7f340 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 20 69  and returns us i
7f350 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6c 6f  mmediately to lo
7f360 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a  cking level 0..*
7f370 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
7f380 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 74 68  ible to lower th
7f390 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
7f3a0 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 74 69  one step at a ti
7f3b0 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74  me.  You.** must
7f3c0 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 6f 20   go straight to 
7f3d0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e  locking level 0.
7f3e0 0a 2a 2f 0a 69 6e 74 20 6f 73 32 4c 6f 63 6b 28  .*/.int os2Lock(
7f3f0 20 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74   OsFile *id, int
7f400 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 41   locktype ){.  A
7f410 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c 49 54  PIRET rc = SQLIT
7f420 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75  E_OK;    /* Retu
7f430 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62  rn code from sub
7f440 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 41 50  routines */.  AP
7f450 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52  IRET res = NO_ER
7f460 52 4f 52 3b 20 20 20 20 2f 2a 20 52 65 73 75 6c  ROR;    /* Resul
7f470 74 20 6f 66 20 61 6e 20 4f 53 2f 32 20 6c 6f 63  t of an OS/2 loc
7f480 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  k call */.  int 
7f490 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20  newLocktype;    
7f4a0 20 20 20 2f 2a 20 53 65 74 20 69 64 2d 3e 6c 6f     /* Set id->lo
7f4b0 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20 76  cktype to this v
7f4c0 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69 74  alue before exit
7f4d0 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74  ing */.  int got
7f4e0 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b  PendingLock = 0;
7f4f0 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 63  /* True if we ac
7f500 71 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47  quired a PENDING
7f510 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20   lock this time 
7f520 2a 2f 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c  */.  FILELOCK  L
7f530 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20  ockArea,.       
7f540 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b       UnlockArea;
7f550 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
7f560 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
7f570 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b  ;.  memset(&Lock
7f580 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
7f590 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65  LockArea));.  me
7f5a0 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
7f5b0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
7f5c0 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65  ckArea));.  asse
7f5d0 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a  rt( pFile!=0 );.
7f5e0 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c 4f 43    OSTRACE4( "LOC
7f5f0 4b 20 25 64 20 25 64 20 77 61 73 20 25 64 5c 6e  K %d %d was %d\n
7f600 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
7f610 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f  ktype, pFile->lo
7f620 63 6b 74 79 70 65 20 29 3b 0a 0a 20 20 2f 2a 20  cktype );..  /* 
7f630 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65  If there is alre
7f640 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68  ady a lock of th
7f650 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20  is type or more 
7f660 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74  restrictive on t
7f670 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20  he.  ** OsFile, 
7f680 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27  do nothing. Don'
7f690 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f  t use the end_lo
7f6a0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
7f6b0 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73  s.  ** sqlite3Os
7f6c0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73  EnterMutex() has
7f6d0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  n't been called 
7f6e0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  yet..  */.  if( 
7f6f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
7f700 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
7f710 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
7f720 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  K;.  }..  /* Mak
7f730 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69  e sure the locki
7f740 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63  ng sequence is c
7f750 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73  orrect.  */.  as
7f760 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
7f770 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c  ktype!=NO_LOCK |
7f780 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  | locktype==SHAR
7f790 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
7f7a0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50  ert( locktype!=P
7f7b0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20  ENDING_LOCK );. 
7f7c0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
7f7d0 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e!=RESERVED_LOCK
7f7e0 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   || pFile->lockt
7f7f0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
7f800 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74   );..  /* Lock t
7f810 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  he PENDING_LOCK 
7f820 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20  byte if we need 
7f830 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e  to acquire a PEN
7f840 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a  DING lock or.  *
7f850 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e  * a SHARED lock.
7f860 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75    If we are acqu
7f870 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c  iring a SHARED l
7f880 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69  ock, the acquisi
7f890 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65  tion of.  ** the
7f8a0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79   PENDING_LOCK by
7f8b0 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e  te is temporary.
7f8c0 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74  .  */.  newLockt
7f8d0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63  ype = pFile->loc
7f8e0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69  ktype;.  if( pFi
7f8f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  le->locktype==NO
7f900 5f 4c 4f 43 4b 0a 20 20 20 7c 7c 20 28 6c 6f 63  _LOCK.   || (loc
7f910 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
7f920 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e  _LOCK && pFile->
7f930 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56  locktype==RESERV
7f940 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20  ED_LOCK).  ){.  
7f950 20 20 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 0a    int cnt = 3;..
7f960 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66      LockArea.lOf
7f970 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42  fset = PENDING_B
7f980 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  YTE;.    LockAre
7f990 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20  a.lRange = 1L;. 
7f9a0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
7f9b0 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
7f9c0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
7f9d0 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 77 68 69  e = 0L;..    whi
7f9e0 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28  le( cnt-->0 && (
7f9f0 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
7fa00 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
7fa10 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
7fa20 6f 63 6b 41 72 65 61 2c 20 32 30 30 30 4c 2c 20  ockArea, 2000L, 
7fa30 31 4c 29 20 29 21 3d 4e 4f 5f 45 52 52 4f 52 20  1L) )!=NO_ERROR 
7fa40 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72 79 20  ){.      /* Try 
7fa50 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74 20 74  3 times to get t
7fa60 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e  he pending lock.
7fa70 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f    The pending lo
7fa80 63 6b 20 6d 69 67 68 74 20 62 65 0a 20 20 20 20  ck might be.    
7fa90 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 61 6e 6f    ** held by ano
7faa0 74 68 65 72 20 72 65 61 64 65 72 20 70 72 6f 63  ther reader proc
7fab0 65 73 73 20 77 68 6f 20 77 69 6c 6c 20 72 65 6c  ess who will rel
7fac0 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e 74 61 72  ease it momentar
7fad0 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ily..      */.  
7fae0 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 63      OSTRACE2( "c
7faf0 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20 50  ould not get a P
7fb00 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e 74  ENDING lock. cnt
7fb10 3d 25 64 5c 6e 22 2c 20 63 6e 74 20 29 3b 0a 20  =%d\n", cnt );. 
7fb20 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 31 29       DosSleep(1)
7fb30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50  ;.    }.    gotP
7fb40 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73  endingLock = res
7fb50 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
7fb60 69 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63  ire a shared loc
7fb70 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
7fb80 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
7fb90 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20  CK && res ){.   
7fba0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
7fbb0 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
7fbc0 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67  K );.    res = g
7fbd0 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65  etReadLock(pFile
7fbe0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d  );.    if( res =
7fbf0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
7fc00 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
7fc10 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
7fc20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
7fc30 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45  cquire a RESERVE
7fc40 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  D lock.  */.  if
7fc50 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45  ( locktype==RESE
7fc60 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73  RVED_LOCK && res
7fc70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
7fc80 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
7fc90 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
7fca0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66      LockArea.lOf
7fcb0 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f  fset = RESERVED_
7fcc0 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  BYTE;.    LockAr
7fcd0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a  ea.lRange = 1L;.
7fce0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
7fcf0 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20  Offset = 0L;.   
7fd00 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
7fd10 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73  ge = 0L;.    res
7fd20 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
7fd30 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55  ks( pFile->h, &U
7fd40 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b  nlockArea, &Lock
7fd50 41 72 65 61 2c 20 32 30 30 30 4c 2c 20 31 4c 20  Area, 2000L, 1L 
7fd60 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d  );.    if( res =
7fd70 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
7fd80 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
7fd90 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b  = RESERVED_LOCK;
7fda0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
7fdb0 20 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49   Acquire a PENDI
7fdc0 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  NG lock.  */.  i
7fdd0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
7fde0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72  LUSIVE_LOCK && r
7fdf0 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63  es ){.    newLoc
7fe00 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f  ktype = PENDING_
7fe10 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e  LOCK;.    gotPen
7fe20 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  dingLock = 0;.  
7fe30 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
7fe40 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
7fe50 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
7fe60 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
7fe70 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
7fe80 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
7fe90 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41  e->locktype>=SHA
7fea0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
7feb0 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64  res = unlockRead
7fec0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
7fed0 20 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65   OSTRACE2( "unre
7fee0 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20  adlock = %d\n", 
7fef0 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41  res );.    LockA
7ff00 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48  rea.lOffset = SH
7ff10 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
7ff20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
7ff30 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
7ff40 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
7ff50 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
7ff60 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
7ff70 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20  e = 0L;.    res 
7ff80 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
7ff90 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
7ffa0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
7ffb0 72 65 61 2c 20 32 30 30 30 4c 2c 20 31 4c 20 29  rea, 2000L, 1L )
7ffc0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d  ;.    if( res ==
7ffd0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
7ffe0 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
7fff0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b   EXCLUSIVE_LOCK;
80000 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
80010 20 20 4f 53 54 52 41 43 45 32 28 20 22 65 72 72    OSTRACE2( "err
80020 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c  or-code = %d\n",
80030 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20   res );.    }.  
80040 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72  }..  /* If we ar
80050 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44  e holding a PEND
80060 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75  ING lock that ou
80070 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73  ght to be releas
80080 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65  ed, then.  ** re
80090 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20  lease it now..  
800a0 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64  */.  if( gotPend
800b0 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74  ingLock && lockt
800c0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
800d0 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61   ){.    LockArea
800e0 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20  .lOffset = 0L;. 
800f0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e     LockArea.lRan
80100 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c  ge = 0L;.    Unl
80110 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
80120 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
80130 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
80140 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20  Range = 1L;.    
80150 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
80160 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
80170 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
80180 61 2c 20 32 30 30 30 4c 2c 20 31 4c 20 29 3b 0a  a, 2000L, 1L );.
80190 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65    }..  /* Update
801a0 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
801b0 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20  e lock has held 
801c0 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  in the file desc
801d0 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a  riptor then.  **
801e0 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72   return the appr
801f0 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63  opriate result c
80200 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ode..  */.  if( 
80210 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
80220 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
80230 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
80240 20 20 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c      OSTRACE4( "L
80250 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72  OCK FAILED %d tr
80260 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20  ying for %d but 
80270 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  got %d\n", pFile
80280 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
80290 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63  locktype, newLoc
802a0 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72 63 20  ktype );.    rc 
802b0 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
802c0 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   }.  pFile->lock
802d0 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79  type = newLockty
802e0 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  pe;.  return rc;
802f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
80300 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
80310 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45   there is a RESE
80320 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  RVED lock held o
80330 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  n the specified.
80340 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
80350 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
80360 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
80370 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65  lock is held, re
80380 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f  turn.** non-zero
80390 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f  , otherwise zero
803a0 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 43 68 65 63  ..*/.int os2Chec
803b0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 20 4f  kReservedLock( O
803c0 73 46 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41  sFile *id ){.  A
803d0 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
803e0 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  ROR;.  os2File *
803f0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
80400 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20  *)id;.  assert( 
80410 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66  pFile!=0 );.  if
80420 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
80430 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
80440 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a   ){.    rc = 1;.
80450 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54      OSTRACE3( "T
80460 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
80470 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46  d (local)\n", pF
80480 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 20  ile->h, rc );.  
80490 7d 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c  }else{.    FILEL
804a0 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20  OCK  LockArea,. 
804b0 20 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c               Unl
804c0 6f 63 6b 41 72 65 61 3b 0a 20 20 20 20 6d 65 6d  ockArea;.    mem
804d0 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30  set(&LockArea, 0
804e0 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65  , sizeof(LockAre
804f0 61 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  a));.    memset(
80500 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  &UnlockArea, 0, 
80510 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65  sizeof(UnlockAre
80520 61 29 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  a));.    LockAre
80530 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45  a.lOffset = RESE
80540 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c  RVED_BYTE;.    L
80550 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
80560 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41   1L;.    UnlockA
80570 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
80580 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
80590 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
805a0 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69 6c    rc = DosSetFil
805b0 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
805c0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
805d0 4c 6f 63 6b 41 72 65 61 2c 20 32 30 30 30 4c 2c  LockArea, 2000L,
805e0 20 31 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72   1L );.    if( r
805f0 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  c == NO_ERROR ){
80600 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  .      LockArea.
80610 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
80620 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
80630 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20  nge = 0L;.      
80640 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
80650 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  et = RESERVED_BY
80660 54 45 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b  TE;.      Unlock
80670 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c  Area.lRange = 1L
80680 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 44 6f 73  ;.      rc = Dos
80690 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
806a0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
806b0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
806c0 32 30 30 30 4c 2c 20 31 4c 20 29 3b 0a 20 20 20  2000L, 1L );.   
806d0 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28   }.    OSTRACE3(
806e0 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25   "TEST WR-LOCK %
806f0 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22  d %d (remote)\n"
80700 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29  , pFile->h, rc )
80710 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
80720 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65  c;.}../*.** Lowe
80730 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  r the locking le
80740 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63  vel on file desc
80750 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63  riptor id to loc
80760 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65  ktype.  locktype
80770 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68  .** must be eith
80780 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48  er NO_LOCK or SH
80790 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a  ARED_LOCK..**.**
807a0 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   If the locking 
807b0 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c  level of the fil
807c0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
807d0 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65  already at or be
807e0 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65  low.** the reque
807f0 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  sted locking lev
80800 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  el, this routine
80810 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
80820 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  ** It is not pos
80830 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72  sible for this r
80840 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69  outine to fail i
80850 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
80860 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c  ument.** is NO_L
80870 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63  OCK.  If the sec
80880 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
80890 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e  SHARED_LOCK then
808a0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
808b0 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51   might return SQ
808c0 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 69  LITE_IOERR;.*/.i
808d0 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20 4f 73  nt os2Unlock( Os
808e0 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  File *id, int lo
808f0 63 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e 74 20  cktype ){.  int 
80900 74 79 70 65 3b 0a 20 20 41 50 49 52 45 54 20 72  type;.  APIRET r
80910 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
80920 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20   os2File *pFile 
80930 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a  = (os2File*)id;.
80940 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b    FILELOCK  Lock
80950 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Area,.          
80960 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20    UnlockArea;.  
80970 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61  memset(&LockArea
80980 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b  , 0, sizeof(Lock
80990 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Area));.  memset
809a0 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
809b0 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
809c0 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ea));.  assert( 
809d0 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73  pFile!=0 );.  as
809e0 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d  sert( locktype<=
809f0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
80a00 20 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c 4f   OSTRACE4( "UNLO
80a10 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20  CK %d to %d was 
80a20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
80a30 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65   locktype, pFile
80a40 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
80a50 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  type = pFile->lo
80a60 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79  cktype;.  if( ty
80a70 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe>=EXCLUSIVE_LO
80a80 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72  CK ){.    LockAr
80a90 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
80aa0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  .    LockArea.lR
80ab0 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55  ange = 0L;.    U
80ac0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
80ad0 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
80ae0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
80af0 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44  .lRange = SHARED
80b00 5f 53 49 5a 45 3b 0a 20 20 20 20 44 6f 73 53 65  _SIZE;.    DosSe
80b10 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c  tFileLocks( pFil
80b20 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65  e->h, &UnlockAre
80b30 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 32 30  a, &LockArea, 20
80b40 30 30 4c 2c 20 31 4c 20 29 3b 0a 20 20 20 20 69  00L, 1L );.    i
80b50 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
80b60 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65 74 52  RED_LOCK && getR
80b70 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 21  eadLock(pFile) !
80b80 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
80b90 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75      /* This shou
80ba0 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e  ld never happen.
80bb0 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61    We should alwa
80bc0 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20  ys be able to.  
80bd0 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65      ** reacquire
80be0 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a   the read lock *
80bf0 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  /.      rc = SQL
80c00 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d  ITE_IOERR;.    }
80c10 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e  .  }.  if( type>
80c20 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29  =RESERVED_LOCK )
80c30 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  {.    LockArea.l
80c40 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20  Offset = 0L;.   
80c50 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65   LockArea.lRange
80c60 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
80c70 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
80c80 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20  RESERVED_BYTE;. 
80c90 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52     UnlockArea.lR
80ca0 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 44  ange = 1L;.    D
80cb0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
80cc0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
80cd0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
80ce0 2c 20 32 30 30 30 4c 2c 20 31 4c 20 29 3b 0a 20  , 2000L, 1L );. 
80cf0 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70   }.  if( locktyp
80d00 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79  e==NO_LOCK && ty
80d10 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe>=SHARED_LOCK 
80d20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61  ){.    unlockRea
80d30 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20  dLock(pFile);.  
80d40 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45  }.  if( type>=PE
80d50 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20  NDING_LOCK ){.  
80d60 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
80d70 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
80d80 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
80d90 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
80da0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44  a.lOffset = PEND
80db0 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e  ING_BYTE;.    Un
80dc0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
80dd0 3d 20 31 4c 3b 0a 20 20 20 20 44 6f 73 53 65 74  = 1L;.    DosSet
80de0 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65  FileLocks( pFile
80df0 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61  ->h, &UnlockArea
80e00 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 32 30 30  , &LockArea, 200
80e10 30 4c 2c 20 31 4c 20 29 3b 0a 20 20 7d 0a 20 20  0L, 1L );.  }.  
80e20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
80e30 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65  = locktype;.  re
80e40 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
80e50 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76  * Turn a relativ
80e60 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20  e pathname into 
80e70 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e  a full pathname.
80e80 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
80e90 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 66 75 6c  er.** to the ful
80ea0 6c 20 70 61 74 68 6e 61 6d 65 20 73 74 6f 72 65  l pathname store
80eb0 64 20 69 6e 20 73 70 61 63 65 20 6f 62 74 61 69  d in space obtai
80ec0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
80ed0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 54 68 65 20  alloc()..** The 
80ee0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
80ef0 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
80f00 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 69 73  for freeing this
80f10 20 73 70 61 63 65 20 6f 6e 63 65 20 69 74 0a 2a   space once it.*
80f20 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e  * is no longer n
80f30 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eeded..*/.SQLITE
80f40 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
80f50 71 6c 69 74 65 33 4f 73 32 46 75 6c 6c 50 61 74  qlite3Os2FullPat
80f60 68 6e 61 6d 65 28 20 63 6f 6e 73 74 20 63 68 61  hname( const cha
80f70 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 29 7b 0a  r *zRelative ){.
80f80 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 3d 20    char *zFull = 
80f90 30 3b 0a 20 20 69 66 28 20 73 74 72 63 68 72 28  0;.  if( strchr(
80fa0 7a 52 65 6c 61 74 69 76 65 2c 20 27 3a 27 29 20  zRelative, ':') 
80fb0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
80fc0 74 53 74 72 69 6e 67 28 20 26 7a 46 75 6c 6c 2c  tString( &zFull,
80fd0 20 7a 52 65 6c 61 74 69 76 65 2c 20 28 63 68 61   zRelative, (cha
80fe0 72 2a 29 30 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  r*)0 );.  }else{
80ff0 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 44 72 69  .    ULONG ulDri
81000 76 65 4e 75 6d 20 3d 20 30 3b 0a 20 20 20 20 55  veNum = 0;.    U
81010 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4d 61 70 20  LONG ulDriveMap 
81020 3d 20 30 3b 0a 20 20 20 20 55 4c 4f 4e 47 20 63  = 0;.    ULONG c
81030 62 7a 42 75 66 4c 65 6e 20 3d 20 53 51 4c 49 54  bzBufLen = SQLIT
81040 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 3b  E_TEMPNAME_SIZE;
81050 0a 20 20 20 20 63 68 61 72 20 7a 44 72 69 76 65  .    char zDrive
81060 5b 32 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  [2];.    char *z
81070 42 75 66 66 3b 0a 0a 20 20 20 20 7a 42 75 66 66  Buff;..    zBuff
81080 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28   = sqliteMalloc(
81090 20 63 62 7a 42 75 66 4c 65 6e 20 29 3b 0a 20 20   cbzBufLen );.  
810a0 20 20 69 66 28 20 7a 42 75 66 66 20 21 3d 20 30    if( zBuff != 0
810b0 20 29 7b 0a 20 20 20 20 20 20 44 6f 73 51 75 65   ){.      DosQue
810c0 72 79 43 75 72 72 65 6e 74 44 69 73 6b 28 20 26  ryCurrentDisk( &
810d0 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 75 6c 44  ulDriveNum, &ulD
810e0 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 20 20 20  riveMap );.     
810f0 20 69 66 28 20 44 6f 73 51 75 65 72 79 43 75 72   if( DosQueryCur
81100 72 65 6e 74 44 69 72 28 20 75 6c 44 72 69 76 65  rentDir( ulDrive
81110 4e 75 6d 2c 20 28 50 42 59 54 45 29 7a 42 75 66  Num, (PBYTE)zBuf
81120 66 2c 20 26 63 62 7a 42 75 66 4c 65 6e 20 29 20  f, &cbzBufLen ) 
81130 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
81140 20 20 20 20 20 20 20 73 70 72 69 6e 74 66 28 20         sprintf( 
81150 7a 44 72 69 76 65 2c 20 22 25 63 22 2c 20 28 63  zDrive, "%c", (c
81160 68 61 72 29 28 27 41 27 20 2b 20 75 6c 44 72 69  har)('A' + ulDri
81170 76 65 4e 75 6d 20 2d 20 31 29 20 29 3b 0a 20 20  veNum - 1) );.  
81180 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74        sqlite3Set
81190 53 74 72 69 6e 67 28 20 26 7a 46 75 6c 6c 2c 20  String( &zFull, 
811a0 7a 44 72 69 76 65 2c 20 22 3a 5c 5c 22 2c 20 7a  zDrive, ":\\", z
811b0 42 75 66 66 2c 0a 20 20 20 20 20 20 20 20 20 20  Buff,.          
811c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
811d0 22 5c 5c 22 2c 20 7a 52 65 6c 61 74 69 76 65 2c  "\\", zRelative,
811e0 20 28 63 68 61 72 2a 29 30 20 29 3b 0a 20 20 20   (char*)0 );.   
811f0 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
81200 65 46 72 65 65 28 20 7a 42 75 66 66 20 29 3b 0a  eFree( zBuff );.
81210 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
81220 72 6e 20 7a 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  rn zFull;.}../*.
81230 2a 2a 20 54 68 65 20 66 75 6c 6c 53 79 6e 63 20  ** The fullSync 
81240 6f 70 74 69 6f 6e 20 69 73 20 6d 65 61 6e 69 6e  option is meanin
81250 67 6c 65 73 73 20 6f 6e 20 6f 73 32 2c 20 6f 72  gless on os2, or
81260 20 63 6f 72 72 65 63 74 20 6d 65 20 69 66 20 49   correct me if I
81270 27 6d 20 77 72 6f 6e 67 2e 20 20 54 68 69 73 20  'm wrong.  This 
81280 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 46  is a no-op..** F
81290 72 6f 6d 20 6f 73 5f 75 6e 69 78 2e 63 3a 20 43  rom os_unix.c: C
812a0 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
812b0 6f 66 20 74 68 65 20 66 75 6c 6c 73 79 6e 63 20  of the fullsync 
812c0 66 6c 61 67 20 69 6e 20 74 68 65 20 67 69 76 65  flag in the give
812d0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
812e0 72 2e 0a 2a 2a 20 46 72 6f 6d 20 6f 73 5f 75 6e  r..** From os_un
812f0 69 78 2e 63 3a 20 28 28 75 6e 69 78 46 69 6c 65  ix.c: ((unixFile
81300 2a 29 69 64 29 2d 3e 66 75 6c 6c 53 79 6e 63 20  *)id)->fullSync 
81310 3d 20 76 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 76  = v;.*/.static v
81320 6f 69 64 20 6f 73 32 53 65 74 46 75 6c 6c 53 79  oid os2SetFullSy
81330 6e 63 28 20 4f 73 46 69 6c 65 20 2a 69 64 2c 20  nc( OsFile *id, 
81340 69 6e 74 20 76 20 29 7b 0a 20 20 72 65 74 75 72  int v ){.  retur
81350 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  n;.}../*.** Retu
81360 72 6e 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  rn the underlyin
81370 67 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f  g file handle fo
81380 72 20 61 6e 20 4f 73 46 69 6c 65 0a 2a 2f 0a 73  r an OsFile.*/.s
81390 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 69 6c  tatic int os2Fil
813a0 65 48 61 6e 64 6c 65 28 20 4f 73 46 69 6c 65 20  eHandle( OsFile 
813b0 2a 69 64 20 29 7b 0a 20 20 72 65 74 75 72 6e 20  *id ){.  return 
813c0 28 69 6e 74 29 28 28 6f 73 32 46 69 6c 65 2a 29  (int)((os2File*)
813d0 69 64 29 2d 3e 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  id)->h;.}../*.**
813e0 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67   Return an integ
813f0 65 72 20 74 68 61 74 20 69 6e 64 69 63 65 73 20  er that indices 
81400 74 68 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b  the type of lock
81410 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 0a   currently held.
81420 2a 2a 20 62 79 20 74 68 69 73 20 68 61 6e 64 6c  ** by this handl
81430 65 2e 20 20 28 55 73 65 64 20 66 6f 72 20 74 65  e.  (Used for te
81440 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73  sting and analys
81450 69 73 20 6f 6e 6c 79 2e 29 0a 2a 2f 0a 73 74 61  is only.).*/.sta
81460 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 53  tic int os2LockS
81470 74 61 74 65 28 20 4f 73 46 69 6c 65 20 2a 69 64  tate( OsFile *id
81480 20 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 28 6f   ){.  return ((o
81490 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63  s2File*)id)->loc
814a0 6b 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ktype;.}../*.** 
814b0 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f  Return the secto
814c0 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  r size in bytes 
814d0 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
814e0 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66  g block device f
814f0 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66  or.** the specif
81500 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69  ied file. This i
81510 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  s almost always 
81520 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d  512 bytes, but m
81530 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20  ay be.** larger 
81540 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73  for some devices
81550 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  ..**.** SQLite c
81560 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73  ode assumes this
81570 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74   function cannot
81580 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61   fail. It also a
81590 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69  ssumes that.** i
815a0 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20  f two files are 
815b0 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73  created in the s
815c0 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  ame file-system 
815d0 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a  directory (i.e..
815e0 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e  ** a database an
815f0 64 20 69 74 27 73 20 6a 6f 75 72 6e 61 6c 20 66  d it's journal f
81600 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65  ile) that the se
81610 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62  ctor size will b
81620 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f  e the.** same fo
81630 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69  r both..*/.stati
81640 63 20 69 6e 74 20 6f 73 32 53 65 63 74 6f 72 53  c int os2SectorS
81650 69 7a 65 28 4f 73 46 69 6c 65 20 2a 69 64 29 7b  ize(OsFile *id){
81660 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
81670 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
81680 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  SIZE;.}../*.** T
81690 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e  his vector defin
816a0 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f  es all the metho
816b0 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72  ds that can oper
816c0 61 74 65 20 6f 6e 20 61 6e 20 4f 73 46 69 6c 65  ate on an OsFile
816d0 0a 2a 2a 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a  .** for os2..*/.
816e0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 49 6f 4d  static const IoM
816f0 65 74 68 6f 64 20 73 71 6c 69 74 65 33 4f 73 32  ethod sqlite3Os2
81700 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 6f  IoMethod = {.  o
81710 73 32 43 6c 6f 73 65 2c 0a 20 20 6f 73 32 4f 70  s2Close,.  os2Op
81720 65 6e 44 69 72 65 63 74 6f 72 79 2c 0a 20 20 6f  enDirectory,.  o
81730 73 32 52 65 61 64 2c 0a 20 20 6f 73 32 57 72 69  s2Read,.  os2Wri
81740 74 65 2c 0a 20 20 6f 73 32 53 65 65 6b 2c 0a 20  te,.  os2Seek,. 
81750 20 6f 73 32 54 72 75 6e 63 61 74 65 2c 0a 20 20   os2Truncate,.  
81760 6f 73 32 53 79 6e 63 2c 0a 20 20 6f 73 32 53 65  os2Sync,.  os2Se
81770 74 46 75 6c 6c 53 79 6e 63 2c 0a 20 20 6f 73 32  tFullSync,.  os2
81780 46 69 6c 65 48 61 6e 64 6c 65 2c 0a 20 20 6f 73  FileHandle,.  os
81790 32 46 69 6c 65 53 69 7a 65 2c 0a 20 20 6f 73 32  2FileSize,.  os2
817a0 4c 6f 63 6b 2c 0a 20 20 6f 73 32 55 6e 6c 6f 63  Lock,.  os2Unloc
817b0 6b 2c 0a 20 20 6f 73 32 4c 6f 63 6b 53 74 61 74  k,.  os2LockStat
817c0 65 2c 0a 20 20 6f 73 32 43 68 65 63 6b 52 65 73  e,.  os2CheckRes
817d0 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 6f 73 32  ervedLock,.  os2
817e0 53 65 63 74 6f 72 53 69 7a 65 2c 0a 7d 3b 0a 0a  SectorSize,.};..
817f0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d  /*.** Allocate m
81800 65 6d 6f 72 79 20 66 6f 72 20 61 6e 20 4f 73 46  emory for an OsF
81810 69 6c 65 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65  ile.  Initialize
81820 20 74 68 65 20 6e 65 77 20 4f 73 46 69 6c 65 0a   the new OsFile.
81830 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  ** to the value 
81840 67 69 76 65 6e 20 69 6e 20 70 49 6e 69 74 20 61  given in pInit a
81850 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  nd return a poin
81860 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a  ter to the new.*
81870 2a 20 4f 73 46 69 6c 65 2e 20 20 49 66 20 77 65  * OsFile.  If we
81880 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f   run out of memo
81890 72 79 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 69  ry, close the fi
818a0 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55  le and return NU
818b0 4c 4c 2e 0a 2a 2f 0a 69 6e 74 20 61 6c 6c 6f 63  LL..*/.int alloc
818c0 61 74 65 4f 73 32 46 69 6c 65 28 20 6f 73 32 46  ateOs2File( os2F
818d0 69 6c 65 20 2a 70 49 6e 69 74 2c 20 4f 73 46 69  ile *pInit, OsFi
818e0 6c 65 20 2a 2a 70 6c 64 20 29 7b 0a 20 20 6f 73  le **pld ){.  os
818f0 32 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 70  2File *pNew;.  p
81900 4e 65 77 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c  New = sqliteMall
81910 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77  oc( sizeof(*pNew
81920 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  ) );.  if( pNew=
81930 3d 30 20 29 7b 0a 20 20 20 20 44 6f 73 43 6c 6f  =0 ){.    DosClo
81940 73 65 28 20 70 49 6e 69 74 2d 3e 68 20 29 3b 0a  se( pInit->h );.
81950 20 20 20 20 2a 70 6c 64 20 3d 20 30 3b 0a 20 20      *pld = 0;.  
81960 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
81970 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NOMEM;.  }else{.
81980 20 20 20 20 2a 70 4e 65 77 20 3d 20 2a 70 49 6e      *pNew = *pIn
81990 69 74 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4d  it;.    pNew->pM
819a0 65 74 68 6f 64 20 3d 20 26 73 71 6c 69 74 65 33  ethod = &sqlite3
819b0 4f 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 20  Os2IoMethod;.   
819c0 20 70 4e 65 77 2d 3e 6c 6f 63 6b 74 79 70 65 20   pNew->locktype 
819d0 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 2a  = NO_LOCK;.    *
819e0 70 6c 64 20 3d 20 28 4f 73 46 69 6c 65 2a 29 70  pld = (OsFile*)p
819f0 4e 65 77 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75  New;.    OpenCou
81a00 6e 74 65 72 28 2b 31 29 3b 0a 20 20 20 20 72 65  nter(+1);.    re
81a10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
81a20 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
81a30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53   SQLITE_OMIT_DIS
81a40 4b 49 4f 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  KIO */./********
81a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81a90 2a 2a 2a 0a 2a 2a 20 45 76 65 72 79 74 68 69 6e  ***.** Everythin
81aa0 67 20 61 62 6f 76 65 20 64 65 61 6c 73 20 77 69  g above deals wi
81ab0 74 68 20 66 69 6c 65 20 49 2f 4f 2e 20 20 45 76  th file I/O.  Ev
81ac0 65 72 79 74 68 69 6e 67 20 74 68 61 74 20 66 6f  erything that fo
81ad0 6c 6c 6f 77 73 20 64 65 61 6c 73 0a 2a 2a 20 77  llows deals.** w
81ae0 69 74 68 20 6f 74 68 65 72 20 6d 69 73 63 65 6c  ith other miscel
81af0 6c 61 6e 6f 75 73 20 61 73 70 65 63 74 73 20 6f  lanous aspects o
81b00 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  f the operating 
81b10 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65  system interface
81b20 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
81b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
81b70 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
81b80 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
81b90 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  ON./*.** Interfa
81ba0 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
81bb0 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
81bc0 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
81bd0 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
81be0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
81bf0 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
81c00 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
81c10 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ary..*/.SQLITE_P
81c20 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
81c30 69 74 65 33 4f 73 32 44 6c 6f 70 65 6e 28 63 6f  ite3Os2Dlopen(co
81c40 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
81c50 61 6d 65 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f  ame){.  UCHAR lo
81c60 61 64 45 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d  adErr[256];.  HM
81c70 4f 44 55 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50  ODULE hmod;.  AP
81c80 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20  IRET rc;.  rc = 
81c90 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 50  DosLoadModule((P
81ca0 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a 65  SZ)loadErr, size
81cb0 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46 69  of(loadErr), zFi
81cc0 6c 65 6e 61 6d 65 2c 20 26 68 6d 6f 64 29 3b 0a  lename, &hmod);.
81cd0 20 20 69 66 20 28 72 63 20 21 3d 20 4e 4f 5f 45    if (rc != NO_E
81ce0 52 52 4f 52 29 20 72 65 74 75 72 6e 20 30 3b 0a  RROR) return 0;.
81cf0 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
81d00 68 6d 6f 64 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  hmod;.}.SQLITE_P
81d10 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
81d20 69 74 65 33 4f 73 32 44 6c 73 79 6d 28 76 6f 69  ite3Os2Dlsym(voi
81d30 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73  d *pHandle, cons
81d40 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29  t char *zSymbol)
81d50 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41  {.  PFN pfn;.  A
81d60 50 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d  PIRET rc;.  rc =
81d70 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64   DosQueryProcAdd
81d80 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64  r((HMODULE)pHand
81d90 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c  le, 0L, zSymbol,
81da0 20 26 70 66 6e 29 3b 0a 20 20 69 66 20 28 72 63   &pfn);.  if (rc
81db0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 7b 0a   != NO_ERROR) {.
81dc0 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79      /* if the sy
81dd0 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20  mbol itself was 
81de0 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63  not found, searc
81df0 68 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20  h again for the 
81e00 73 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62  same.     * symb
81e10 6f 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61  ol with an extra
81e20 20 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61   underscore, tha
81e30 74 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65  t might be neede
81e40 64 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20  d depending.    
81e50 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e   * on the callin
81e60 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a  g convention */.
81e70 20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f      char _zSymbo
81e80 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20  l[256] = "_";.  
81e90 20 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62    strncat(_zSymb
81ea0 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35  ol, zSymbol, 255
81eb0 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51  );.    rc = DosQ
81ec0 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d  ueryProcAddr((HM
81ed0 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30  ODULE)pHandle, 0
81ee0 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66  L, _zSymbol, &pf
81ef0 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 20 28 72 63  n);.  }.  if (rc
81f00 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 72 65   != NO_ERROR) re
81f10 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e  turn 0;.  return
81f20 20 28 76 6f 69 64 20 2a 29 70 66 6e 3b 0a 7d 0a   (void *)pfn;.}.
81f30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
81f40 6e 74 20 73 71 6c 69 74 65 33 4f 73 32 44 6c 63  nt sqlite3Os2Dlc
81f50 6c 6f 73 65 28 76 6f 69 64 20 2a 70 48 61 6e 64  lose(void *pHand
81f60 6c 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 44 6f  le){.  return Do
81f70 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f  sFreeModule((HMO
81f80 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d  DULE)pHandle);.}
81f90 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
81fa0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
81fb0 4e 53 49 4f 4e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  NSION */.../*.**
81fc0 20 47 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   Get information
81fd0 20 74 6f 20 73 65 65 64 20 74 68 65 20 72 61 6e   to seed the ran
81fe0 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72  dom number gener
81ff0 61 74 6f 72 2e 20 20 54 68 65 20 73 65 65 64 0a  ator.  The seed.
82000 2a 2a 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ** is written in
82010 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 7a 42  to the buffer zB
82020 75 66 5b 32 35 36 5d 2e 20 20 54 68 65 20 63 61  uf[256].  The ca
82030 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d  lling function m
82040 75 73 74 0a 2a 2a 20 73 75 70 70 6c 79 20 61 20  ust.** supply a 
82050 73 75 66 66 69 63 69 65 6e 74 6c 79 20 6c 61 72  sufficiently lar
82060 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51  ge buffer..*/.SQ
82070 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
82080 20 73 71 6c 69 74 65 33 4f 73 32 52 61 6e 64 6f   sqlite3Os2Rando
82090 6d 53 65 65 64 28 20 63 68 61 72 20 2a 7a 42 75  mSeed( char *zBu
820a0 66 20 29 7b 0a 20 20 2f 2a 20 57 65 20 68 61 76  f ){.  /* We hav
820b0 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20  e to initialize 
820c0 7a 42 75 66 20 74 6f 20 70 72 65 76 65 6e 74 20  zBuf to prevent 
820d0 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 72 65  valgrind from re
820e0 70 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 65 72 72  porting.  ** err
820f0 6f 72 73 2e 20 20 54 68 65 20 72 65 70 6f 72 74  ors.  The report
82100 73 20 69 73 73 75 65 64 20 62 79 20 76 61 6c 67  s issued by valg
82110 72 69 6e 64 20 61 72 65 20 69 6e 63 6f 72 72 65  rind are incorre
82120 63 74 20 2d 20 77 65 20 77 6f 75 6c 64 0a 20 20  ct - we would.  
82130 2a 2a 20 70 72 65 66 65 72 20 74 68 61 74 20 74  ** prefer that t
82140 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65  he randomness be
82150 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61   increased by ma
82160 6b 69 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0a  king use of the.
82170 20 20 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a    ** uninitializ
82180 65 64 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66  ed space in zBuf
82190 20 2d 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20   - but valgrind 
821a0 65 72 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77  errors tend to w
821b0 6f 72 72 79 0a 20 20 2a 2a 20 73 6f 6d 65 20 75  orry.  ** some u
821c0 73 65 72 73 2e 20 20 52 61 74 68 65 72 20 74 68  sers.  Rather th
821d0 61 6e 20 61 72 67 75 65 2c 20 69 74 20 73 65 65  an argue, it see
821e0 6d 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74  ms easier just t
821f0 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a  o initialize.  *
82200 2a 20 74 68 65 20 77 68 6f 6c 65 20 61 72 72 61  * the whole arra
82210 79 20 61 6e 64 20 73 69 6c 65 6e 63 65 20 76 61  y and silence va
82220 6c 67 72 69 6e 64 2c 20 65 76 65 6e 20 69 66 20  lgrind, even if 
82230 74 68 61 74 20 6d 65 61 6e 73 20 6c 65 73 73 20  that means less 
82240 72 61 6e 64 6f 6d 6e 65 73 73 0a 20 20 2a 2a 20  randomness.  ** 
82250 69 6e 20 74 68 65 20 72 61 6e 64 6f 6d 20 73 65  in the random se
82260 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68  ed..  **.  ** Wh
82270 65 6e 20 74 65 73 74 69 6e 67 2c 20 69 6e 69 74  en testing, init
82280 69 61 6c 69 7a 69 6e 67 20 7a 42 75 66 5b 5d 20  ializing zBuf[] 
82290 74 6f 20 7a 65 72 6f 20 69 73 20 61 6c 6c 20 77  to zero is all w
822a0 65 20 64 6f 2e 20 20 54 68 61 74 20 6d 65 61 6e  e do.  That mean
822b0 73 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20 61  s.  ** that we a
822c0 6c 77 61 79 73 20 75 73 65 20 74 68 65 20 73 61  lways use the sa
822d0 6d 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  me random number
822e0 20 73 65 71 75 65 6e 63 65 2e 20 54 68 69 73 20   sequence. This 
822f0 6d 61 6b 65 73 20 74 68 65 0a 20 20 2a 2a 20 74  makes the.  ** t
82300 65 73 74 73 20 72 65 70 65 61 74 61 62 6c 65 2e  ests repeatable.
82310 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 20  .  */.  memset( 
82320 7a 42 75 66 2c 20 30 2c 20 32 35 36 20 29 3b 0a  zBuf, 0, 256 );.
82330 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65    DosGetDateTime
82340 28 20 28 50 44 41 54 45 54 49 4d 45 29 7a 42 75  ( (PDATETIME)zBu
82350 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  f );.  return SQ
82360 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
82370 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69  * Sleep for a li
82380 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74  ttle while.  Ret
82390 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f  urn the amount o
823a0 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f  f time slept..*/
823b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
823c0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 32 53 6c  int sqlite3Os2Sl
823d0 65 65 70 28 20 69 6e 74 20 6d 73 20 29 7b 0a 20  eep( int ms ){. 
823e0 20 44 6f 73 53 6c 65 65 70 28 20 6d 73 20 29 3b   DosSleep( ms );
823f0 0a 20 20 72 65 74 75 72 6e 20 6d 73 3b 0a 7d 0a  .  return ms;.}.
82400 0a 2f 2a 0a 2a 2a 20 53 74 61 74 69 63 20 76 61  ./*.** Static va
82410 72 69 61 62 6c 65 73 20 75 73 65 64 20 66 6f 72  riables used for
82420 20 74 68 72 65 61 64 20 73 79 6e 63 68 72 6f 6e   thread synchron
82430 69 7a 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69  ization.*/.stati
82440 63 20 69 6e 74 20 69 6e 4d 75 74 65 78 20 3d 20  c int inMutex = 
82450 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  0;.#ifdef SQLITE
82460 5f 4f 53 32 5f 54 48 52 45 41 44 53 0a 73 74 61  _OS2_THREADS.sta
82470 74 69 63 20 55 4c 4f 4e 47 20 6d 75 74 65 78 4f  tic ULONG mutexO
82480 77 6e 65 72 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a  wner;.#endif../*
82490 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
824a0 67 20 70 61 69 72 20 6f 66 20 72 6f 75 74 69 6e  g pair of routin
824b0 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74  es implement mut
824c0 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20 66 6f  ual exclusion fo
824d0 72 0a 2a 2a 20 6d 75 6c 74 69 2d 74 68 72 65 61  r.** multi-threa
824e0 64 65 64 20 70 72 6f 63 65 73 73 65 73 2e 20 20  ded processes.  
824f0 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 74 68  Only a single th
82500 72 65 61 64 20 69 73 20 61 6c 6c 6f 77 65 64 20  read is allowed 
82510 74 6f 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 63  to.** executed c
82520 6f 64 65 20 74 68 61 74 20 69 73 20 73 75 72 72  ode that is surr
82530 6f 75 6e 64 65 64 20 62 79 20 45 6e 74 65 72 4d  ounded by EnterM
82540 75 74 65 78 28 29 20 61 6e 64 20 4c 65 61 76 65  utex() and Leave
82550 4d 75 74 65 78 28 29 2e 0a 2a 2a 0a 2a 2a 20 53  Mutex()..**.** S
82560 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 6c 79 20  QLite uses only 
82570 61 20 73 69 6e 67 6c 65 20 4d 75 74 65 78 2e 20  a single Mutex. 
82580 20 54 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 75   There is not mu
82590 63 68 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 63  ch critical.** c
825a0 6f 64 65 20 61 6e 64 20 77 68 61 74 20 6c 69 74  ode and what lit
825b0 74 6c 65 20 74 68 65 72 65 20 69 73 20 65 78 65  tle there is exe
825c0 63 75 74 65 73 20 71 75 69 63 6b 6c 79 20 61 6e  cutes quickly an
825d0 64 20 77 69 74 68 6f 75 74 20 62 6c 6f 63 6b 69  d without blocki
825e0 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
825f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
82600 65 33 4f 73 32 45 6e 74 65 72 4d 75 74 65 78 28  e3Os2EnterMutex(
82610 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
82620 5f 4f 53 32 5f 54 48 52 45 41 44 53 0a 20 20 50  _OS2_THREADS.  P
82630 54 49 42 20 70 74 69 62 3b 0a 20 20 44 6f 73 45  TIB ptib;.  DosE
82640 6e 74 65 72 43 72 69 74 53 65 63 28 29 3b 0a 20  nterCritSec();. 
82650 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
82660 73 28 20 26 70 74 69 62 2c 20 4e 55 4c 4c 20 29  s( &ptib, NULL )
82670 3b 0a 20 20 6d 75 74 65 78 4f 77 6e 65 72 20 3d  ;.  mutexOwner =
82680 20 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32   ptib->tib_ptib2
82690 2d 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 23 65  ->tib2_ultid;.#e
826a0 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 21  ndif.  assert( !
826b0 69 6e 4d 75 74 65 78 20 29 3b 0a 20 20 69 6e 4d  inMutex );.  inM
826c0 75 74 65 78 20 3d 20 31 3b 0a 7d 0a 53 51 4c 49  utex = 1;.}.SQLI
826d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
826e0 73 71 6c 69 74 65 33 4f 73 32 4c 65 61 76 65 4d  sqlite3Os2LeaveM
826f0 75 74 65 78 28 29 7b 0a 23 69 66 64 65 66 20 53  utex(){.#ifdef S
82700 51 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45 41 44  QLITE_OS2_THREAD
82710 53 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 23  S.  PTIB ptib;.#
82720 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20  endif.  assert( 
82730 69 6e 4d 75 74 65 78 20 29 3b 0a 20 20 69 6e 4d  inMutex );.  inM
82740 75 74 65 78 20 3d 20 30 3b 0a 23 69 66 64 65 66  utex = 0;.#ifdef
82750 20 53 51 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45   SQLITE_OS2_THRE
82760 41 44 53 0a 20 20 44 6f 73 47 65 74 49 6e 66 6f  ADS.  DosGetInfo
82770 42 6c 6f 63 6b 73 28 20 26 70 74 69 62 2c 20 4e  Blocks( &ptib, N
82780 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ULL );.  assert(
82790 20 6d 75 74 65 78 4f 77 6e 65 72 20 3d 3d 20 70   mutexOwner == p
827a0 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e  tib->tib_ptib2->
827b0 74 69 62 32 5f 75 6c 74 69 64 20 29 3b 0a 20 20  tib2_ultid );.  
827c0 44 6f 73 45 78 69 74 43 72 69 74 53 65 63 28 29  DosExitCritSec()
827d0 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
827e0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
827f0 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 63 75   the mutex is cu
82800 72 72 65 6e 74 6c 79 20 68 65 6c 64 2e 0a 2a 2a  rrently held..**
82810 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 73 54  .** If the thisT
82820 68 72 65 61 64 4f 6e 6c 79 20 70 61 72 61 6d 65  hreadOnly parame
82830 74 65 72 20 69 73 20 74 72 75 65 2c 20 72 65 74  ter is true, ret
82840 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 64 20  urn true if and 
82850 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 63  only if the.** c
82860 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 68 6f  alling thread ho
82870 6c 64 73 20 74 68 65 20 6d 75 74 65 78 2e 20 20  lds the mutex.  
82880 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  If the parameter
82890 20 69 73 20 66 61 6c 73 65 2c 20 72 65 74 75 72   is false, retur
828a0 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 61 6e 79  n.** true if any
828b0 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68   thread holds th
828c0 65 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49  e mutex..*/.SQLI
828d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
828e0 71 6c 69 74 65 33 4f 73 32 49 6e 4d 75 74 65 78  qlite3Os2InMutex
828f0 28 20 69 6e 74 20 74 68 69 73 54 68 72 65 61 64  ( int thisThread
82900 4f 6e 6c 79 20 29 7b 0a 23 69 66 64 65 66 20 53  Only ){.#ifdef S
82910 51 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45 41 44  QLITE_OS2_THREAD
82920 53 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20  S.  PTIB ptib;. 
82930 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
82940 73 28 20 26 70 74 69 62 2c 20 4e 55 4c 4c 20 29  s( &ptib, NULL )
82950 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e 4d 75 74  ;.  return inMut
82960 65 78 3e 30 20 26 26 20 28 74 68 69 73 54 68 72  ex>0 && (thisThr
82970 65 61 64 4f 6e 6c 79 3d 3d 30 20 7c 7c 20 6d 75  eadOnly==0 || mu
82980 74 65 78 4f 77 6e 65 72 3d 3d 70 74 69 62 2d 3e  texOwner==ptib->
82990 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f  tib_ptib2->tib2_
829a0 75 6c 74 69 64 29 3b 0a 23 65 6c 73 65 0a 20 20  ultid);.#else.  
829b0 72 65 74 75 72 6e 20 69 6e 4d 75 74 65 78 3e 30  return inMutex>0
829c0 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
829d0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
829e0 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74  variable, if set
829f0 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
82a00 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 68  alue, becomes th
82a10 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75  e result.** retu
82a20 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  rned from sqlite
82a30 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29  3OsCurrentTime()
82a40 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
82a50 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a  for testing..*/.
82a60 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
82a70 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
82a80 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  t sqlite3_curren
82a90 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64  t_time = 0;.#end
82aa0 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  if../*.** Find t
82ab0 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  he current time 
82ac0 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f  (in Universal Co
82ad0 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e  ordinated Time).
82ae0 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63    Write the.** c
82af0 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20  urrent time and 
82b00 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e  date as a Julian
82b10 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f   Day number into
82b20 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72   *prNow and.** r
82b30 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e  eturn 0.  Return
82b40 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61   1 if the time a
82b50 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62  nd date cannot b
82b60 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49  e found..*/.SQLI
82b70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
82b80 71 6c 69 74 65 33 4f 73 32 43 75 72 72 65 6e 74  qlite3Os2Current
82b90 54 69 6d 65 28 20 64 6f 75 62 6c 65 20 2a 70 72  Time( double *pr
82ba0 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20  Now ){.  double 
82bb0 6e 6f 77 3b 0a 20 20 55 53 48 4f 52 54 20 73 65  now;.  USHORT se
82bc0 63 6f 6e 64 2c 20 6d 69 6e 75 74 65 2c 20 68 6f  cond, minute, ho
82bd0 75 72 2c 0a 20 20 20 20 20 20 20 20 20 64 61 79  ur,.         day
82be0 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 72 3b 0a 20  , month, year;. 
82bf0 20 44 41 54 45 54 49 4d 45 20 64 74 3b 0a 20 20   DATETIME dt;.  
82c00 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65 28 20  DosGetDateTime( 
82c10 26 64 74 20 29 3b 0a 20 20 73 65 63 6f 6e 64 20  &dt );.  second 
82c20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 73 65 63  = (USHORT)dt.sec
82c30 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75 74 65 20 3d  onds;.  minute =
82c40 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 69 6e 75   (USHORT)dt.minu
82c50 74 65 73 20 2b 20 64 74 2e 74 69 6d 65 7a 6f 6e  tes + dt.timezon
82c60 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28 55 53 48  e;.  hour = (USH
82c70 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b 0a 20 20  ORT)dt.hours;.  
82c80 64 61 79 20 3d 20 28 55 53 48 4f 52 54 29 64 74  day = (USHORT)dt
82c90 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68 20 3d 20  .day;.  month = 
82ca0 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f 6e 74 68  (USHORT)dt.month
82cb0 3b 0a 20 20 79 65 61 72 20 3d 20 28 55 53 48 4f  ;.  year = (USHO
82cc0 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a 20 20 2f  RT)dt.year;..  /
82cd0 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e 73 20 66  * Calculations f
82ce0 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61  rom http://www.a
82cf0 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b  stro.keele.ac.uk
82d00 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f  /~rno/Astronomy/
82d10 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20 20 68 74  hjd.html.     ht
82d20 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b  tp://www.astro.k
82d30 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f  eele.ac.uk/~rno/
82d40 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2d 30 2e  Astronomy/hjd-0.
82d50 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43 61 6c 63  1.c */.  /* Calc
82d60 75 6c 61 74 65 20 74 68 65 20 4a 75 6c 69 61 6e  ulate the Julian
82d70 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f 77 20 3d   days */.  now =
82d80 20 64 61 79 20 2d 20 33 32 30 37 36 20 2b 0a 20   day - 32076 +. 
82d90 20 20 20 31 34 36 31 2a 28 79 65 61 72 20 2b 20     1461*(year + 
82da0 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20  4800 + (month - 
82db0 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20 20 20 20  14)/12)/4 +.    
82dc0 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20 32 20 2d  367*(month - 2 -
82dd0 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32   (month - 14)/12
82de0 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20 20 33 2a  *12)/12 -.    3*
82df0 28 28 79 65 61 72 20 2b 20 34 39 30 30 20 2b 20  ((year + 4900 + 
82e00 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29  (month - 14)/12)
82e10 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 20 41  /100)/4;..  /* A
82e20 64 64 20 74 68 65 20 66 72 61 63 74 69 6f 6e 61  dd the fractiona
82e30 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73 20 61 6e  l hours, mins an
82e40 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 6e  d seconds */.  n
82e50 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b 20 31 32  ow += (hour + 12
82e60 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e 6f 77 20  .0)/24.0;.  now 
82e70 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34 30 2e 30  += minute/1440.0
82e80 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65 63 6f 6e  ;.  now += secon
82e90 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20 2a 70 72  d/86400.0;.  *pr
82ea0 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69 66 64 65  Now = now;.#ifde
82eb0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
82ec0 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72  if( sqlite3_curr
82ed0 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20  ent_time ){.    
82ee0 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33  *prNow = sqlite3
82ef0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36  _current_time/86
82f00 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e  400.0 + 2440587.
82f10 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  5;.  }.#endif.  
82f20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
82f30 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20  ** Remember the 
82f40 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64  number of thread
82f50 2d 73 70 65 63 69 66 69 63 2d 64 61 74 61 20 62  -specific-data b
82f60 6c 6f 63 6b 73 20 61 6c 6c 6f 63 61 74 65 64 2e  locks allocated.
82f70 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 74 6f 20  .** Use this to 
82f80 76 65 72 69 66 79 20 74 68 61 74 20 77 65 20 61  verify that we a
82f90 72 65 20 6e 6f 74 20 6c 65 61 6b 69 6e 67 20 74  re not leaking t
82fa0 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 2d 64  hread-specific-d
82fb0 61 74 61 2e 0a 2a 2a 20 54 69 63 6b 65 74 20 23  ata..** Ticket #
82fc0 31 36 30 31 0a 2a 2f 0a 23 69 66 64 65 66 20 53  1601.*/.#ifdef S
82fd0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
82fe0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
82ff0 33 5f 74 73 64 5f 63 6f 75 6e 74 20 3d 20 30 3b  3_tsd_count = 0;
83000 0a 23 20 64 65 66 69 6e 65 20 54 53 44 5f 43 4f  .# define TSD_CO
83010 55 4e 54 45 52 5f 49 4e 43 52 20 49 6e 74 65 72  UNTER_INCR Inter
83020 6c 6f 63 6b 65 64 49 6e 63 72 65 6d 65 6e 74 28  lockedIncrement(
83030 20 26 73 71 6c 69 74 65 33 5f 74 73 64 5f 63 6f   &sqlite3_tsd_co
83040 75 6e 74 20 29 0a 23 20 64 65 66 69 6e 65 20 54  unt ).# define T
83050 53 44 5f 43 4f 55 4e 54 45 52 5f 44 45 43 52 20  SD_COUNTER_DECR 
83060 49 6e 74 65 72 6c 6f 63 6b 65 64 44 65 63 72 65  InterlockedDecre
83070 6d 65 6e 74 28 20 26 73 71 6c 69 74 65 33 5f 74  ment( &sqlite3_t
83080 73 64 5f 63 6f 75 6e 74 20 29 0a 23 65 6c 73 65  sd_count ).#else
83090 0a 23 20 64 65 66 69 6e 65 20 54 53 44 5f 43 4f  .# define TSD_CO
830a0 55 4e 54 45 52 5f 49 4e 43 52 20 20 2f 2a 20 6e  UNTER_INCR  /* n
830b0 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 6e 65  o-op */.# define
830c0 20 54 53 44 5f 43 4f 55 4e 54 45 52 5f 44 45 43   TSD_COUNTER_DEC
830d0 52 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 23  R  /* no-op */.#
830e0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
830f0 63 61 6c 6c 65 64 20 77 69 74 68 20 61 6c 6c 6f  called with allo
83100 63 61 74 65 46 6c 61 67 3e 31 2c 20 74 68 65 6e  cateFlag>1, then
83110 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
83120 72 20 74 6f 20 74 68 72 65 61 64 0a 2a 2a 20 73  r to thread.** s
83130 70 65 63 69 66 69 63 20 64 61 74 61 20 66 6f 72  pecific data for
83140 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
83150 65 61 64 2e 20 20 41 6c 6c 6f 63 61 74 65 20 61  ead.  Allocate a
83160 6e 64 20 7a 65 72 6f 20 74 68 65 0a 2a 2a 20 74  nd zero the.** t
83170 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 20 64  hread-specific d
83180 61 74 61 20 69 66 20 69 74 20 64 6f 65 73 20 6e  ata if it does n
83190 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
831a0 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a   necessary..**.*
831b0 2a 20 49 66 20 63 61 6c 6c 65 64 20 77 69 74 68  * If called with
831c0 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67 3d 3d 30   allocateFlag==0
831d0 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 74 68 65  , then check the
831e0 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 0a   current thread.
831f0 2a 2a 20 73 70 65 63 69 66 69 63 20 64 61 74 61  ** specific data
83200 2e 20 20 52 65 74 75 72 6e 20 69 74 20 69 66 20  .  Return it if 
83210 69 74 20 65 78 69 73 74 73 2e 20 20 49 66 20 69  it exists.  If i
83220 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  t does not exist
83230 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e  ,.** then return
83240 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   NULL..**.** If 
83250 63 61 6c 6c 65 64 20 77 69 74 68 20 61 6c 6c 6f  called with allo
83260 63 61 74 65 46 6c 61 67 3c 30 2c 20 63 68 65 63  cateFlag<0, chec
83270 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
83280 74 68 72 65 61 64 20 73 70 65 63 69 66 69 63 0a  thread specific.
83290 2a 2a 20 64 61 74 61 20 69 73 20 61 6c 6c 6f 63  ** data is alloc
832a0 61 74 65 64 20 61 6e 64 20 69 73 20 61 6c 6c 20  ated and is all 
832b0 7a 65 72 6f 2e 20 20 49 66 20 69 74 20 69 73 20  zero.  If it is 
832c0 74 68 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20  then deallocate 
832d0 69 74 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  it..** Return a 
832e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74  pointer to the t
832f0 68 72 65 61 64 20 73 70 65 63 69 66 69 63 20 64  hread specific d
83300 61 74 61 20 6f 72 20 4e 55 4c 4c 20 69 66 20 69  ata or NULL if i
83310 74 20 69 73 0a 2a 2a 20 75 6e 61 6c 6c 6f 63 61  t is.** unalloca
83320 74 65 64 20 6f 72 20 67 65 74 73 20 64 65 61 6c  ted or gets deal
83330 6c 6f 63 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  located..*/.SQLI
83340 54 45 5f 50 52 49 56 41 54 45 20 54 68 72 65 61  TE_PRIVATE Threa
83350 64 44 61 74 61 20 2a 73 71 6c 69 74 65 33 4f 73  dData *sqlite3Os
83360 32 54 68 72 65 61 64 53 70 65 63 69 66 69 63 44  2ThreadSpecificD
83370 61 74 61 28 20 69 6e 74 20 61 6c 6c 6f 63 61 74  ata( int allocat
83380 65 46 6c 61 67 20 29 7b 0a 20 20 73 74 61 74 69  eFlag ){.  stati
83390 63 20 54 68 72 65 61 64 44 61 74 61 20 2a 2a 73  c ThreadData **s
833a0 5f 70 70 54 73 64 20 3d 20 4e 55 4c 4c 3b 0a 20  _ppTsd = NULL;. 
833b0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 54 68   static const Th
833c0 72 65 61 64 44 61 74 61 20 7a 65 72 6f 44 61 74  readData zeroDat
833d0 61 20 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 20  a = {0, 0, 0};. 
833e0 20 54 68 72 65 61 64 44 61 74 61 20 2a 70 54 73   ThreadData *pTs
833f0 64 3b 0a 0a 20 20 69 66 28 20 21 73 5f 70 70 54  d;..  if( !s_ppT
83400 73 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  sd ){.    sqlite
83410 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  3OsEnterMutex();
83420 0a 20 20 20 20 69 66 28 20 21 73 5f 70 70 54 73  .    if( !s_ppTs
83430 64 20 29 7b 0a 20 20 20 20 20 20 50 55 4c 4f 4e  d ){.      PULON
83440 47 20 70 75 6c 3b 0a 20 20 20 20 20 20 41 50 49  G pul;.      API
83450 52 45 54 20 72 63 20 3d 20 44 6f 73 41 6c 6c 6f  RET rc = DosAllo
83460 63 54 68 72 65 61 64 4c 6f 63 61 6c 4d 65 6d 6f  cThreadLocalMemo
83470 72 79 28 31 2c 20 26 70 75 6c 29 3b 0a 20 20 20  ry(1, &pul);.   
83480 20 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f     if( rc != NO_
83490 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 20  ERROR ){.       
834a0 20 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d   sqlite3OsLeaveM
834b0 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 20 20  utex();.        
834c0 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
834d0 7d 0a 20 20 20 20 20 20 73 5f 70 70 54 73 64 20  }.      s_ppTsd 
834e0 3d 20 28 54 68 72 65 61 64 44 61 74 61 20 2a 2a  = (ThreadData **
834f0 29 70 75 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20  )pul;.    }.    
83500 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75  sqlite3OsLeaveMu
83510 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 70 54 73  tex();.  }.  pTs
83520 64 20 3d 20 2a 73 5f 70 70 54 73 64 3b 0a 20 20  d = *s_ppTsd;.  
83530 69 66 28 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67  if( allocateFlag
83540 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70  >0 ){.    if( !p
83550 54 73 64 20 29 7b 0a 20 20 20 20 20 20 70 54 73  Tsd ){.      pTs
83560 64 20 3d 20 73 71 6c 69 74 65 33 4f 73 4d 61 6c  d = sqlite3OsMal
83570 6c 6f 63 28 20 73 69 7a 65 6f 66 28 7a 65 72 6f  loc( sizeof(zero
83580 44 61 74 61 29 20 29 3b 0a 20 20 20 20 20 20 69  Data) );.      i
83590 66 28 20 70 54 73 64 20 29 7b 0a 20 20 20 20 20  f( pTsd ){.     
835a0 20 20 20 2a 70 54 73 64 20 3d 20 7a 65 72 6f 44     *pTsd = zeroD
835b0 61 74 61 3b 0a 20 20 20 20 20 20 20 20 2a 73 5f  ata;.        *s_
835c0 70 70 54 73 64 20 3d 20 70 54 73 64 3b 0a 20 20  ppTsd = pTsd;.  
835d0 20 20 20 20 20 20 54 53 44 5f 43 4f 55 4e 54 45        TSD_COUNTE
835e0 52 5f 49 4e 43 52 3b 0a 20 20 20 20 20 20 7d 0a  R_INCR;.      }.
835f0 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
83600 28 20 70 54 73 64 21 3d 30 20 26 26 20 61 6c 6c  ( pTsd!=0 && all
83610 6f 63 61 74 65 46 6c 61 67 3c 30 0a 20 20 20 20  ocateFlag<0.    
83620 20 20 20 20 20 20 20 20 20 20 26 26 20 6d 65 6d            && mem
83630 63 6d 70 28 20 70 54 73 64 2c 20 26 7a 65 72 6f  cmp( pTsd, &zero
83640 44 61 74 61 2c 20 73 69 7a 65 6f 66 28 54 68 72  Data, sizeof(Thr
83650 65 61 64 44 61 74 61 29 20 29 3d 3d 30 20 29 7b  eadData) )==0 ){
83660 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 72  .    sqlite3OsFr
83670 65 65 28 70 54 73 64 29 3b 0a 20 20 20 20 2a 73  ee(pTsd);.    *s
83680 5f 70 70 54 73 64 20 3d 20 4e 55 4c 4c 3b 0a 20  _ppTsd = NULL;. 
83690 20 20 20 54 53 44 5f 43 4f 55 4e 54 45 52 5f 44     TSD_COUNTER_D
836a0 45 43 52 3b 0a 20 20 20 20 70 54 73 64 20 3d 20  ECR;.    pTsd = 
836b0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
836c0 70 54 73 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  pTsd;.}.#endif /
836d0 2a 20 4f 53 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a  * OS_OS2 */../**
836e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
836f0 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a   of os_os2.c ***
83700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
83730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
83740 69 6e 20 66 69 6c 65 20 6f 73 5f 75 6e 69 78 2e  in file os_unix.
83750 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
83760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
83780 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a  ** 2004 May 22.*
83790 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
837a0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
837b0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
837c0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
837d0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
837e0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
837f0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
83800 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
83810 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
83820 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
83830 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
83840 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
83850 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
83860 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
83870 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
83880 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
83890 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
838a0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
838b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838f0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
83900 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
83910 20 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69   that is specifi
83920 63 20 74 6f 20 55 6e 69 78 20 73 79 73 74 65 6d  c to Unix system
83930 73 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 55 4e 49  s..*/.#if OS_UNI
83940 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  X              /
83950 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 75  * This file is u
83960 73 65 64 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79  sed on unix only
83970 20 2a 2f 0a 0a 2f 2a 20 23 64 65 66 69 6e 65 20   */../* #define 
83980 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
83990 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 20 2a 2f  CKING_STYLE 0 */
839a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
839b0 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
839c0 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
839d0 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20  upport on Posix 
839e0 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
839f0 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
83a00 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
83a10 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
83a20 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
83a30 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 65  e support, these
83a40 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70   should be no-op
83a50 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66  s..**.** Large f
83a60 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20  ile support can 
83a70 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e  be disabled usin
83a80 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44  g the -DSQLITE_D
83a90 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63  ISABLE_LFS switc
83aa0 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70  h.** on the comp
83ab0 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e  iler command lin
83ac0 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65  e.  This is nece
83ad0 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 65  ssary if you are
83ae0 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e   compiling.** on
83af0 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e   a recent machin
83b00 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e  e (ex: RedHat 7.
83b10 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20  2) but you want 
83b20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72  your code to wor
83b30 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72  k.** on an older
83b40 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65   machine (ex: Re
83b50 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79  dHat 6.0).  If y
83b60 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65  ou compile on Re
83b70 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68  dHat 7.2.** with
83b80 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c  out this option,
83b90 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20   LFS is enable. 
83ba0 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f   But LFS does no
83bb0 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b  t exist in the k
83bc0 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48  ernel.** in RedH
83bd0 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63  at 6.0, so the c
83be0 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20  ode won't work. 
83bf0 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69   Hence, for maxi
83c00 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f  mum binary.** po
83c10 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68  rtability you sh
83c20 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a  ould omit LFS..*
83c30 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
83c40 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64  _DISABLE_LFS.# d
83c50 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c  efine _LARGE_FIL
83c60 45 20 20 20 20 20 20 20 31 0a 23 20 69 66 6e 64  E       1.# ifnd
83c70 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f  ef _FILE_OFFSET_
83c80 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e 65 20  BITS.#   define 
83c90 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54  _FILE_OFFSET_BIT
83ca0 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 20 64  S 64.# endif.# d
83cb0 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45  efine _LARGEFILE
83cc0 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 69 66  _SOURCE 1.#endif
83cd0 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72 64  ../*.** standard
83ce0 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e 0a   include files..
83cf0 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  */.#include <sys
83d00 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75  /types.h>.#inclu
83d10 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e 0a  de <sys/stat.h>.
83d20 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c 2e  #include <fcntl.
83d30 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
83d40 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  std.h>.#include 
83d50 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23 69 6e  <sys/time.h>.#in
83d60 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a  clude <errno.h>.
83d70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
83d80 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
83d90 4c 45 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  LE.#include <sys
83da0 2f 69 6f 63 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75  /ioctl.h>.#inclu
83db0 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e  de <sys/param.h>
83dc0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d  .#include <sys/m
83dd0 6f 75 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 20 2f  ount.h>.#endif /
83de0 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
83df0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
83e00 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72  ../*.** If we ar
83e10 65 20 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73  e to be thread-s
83e20 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65  afe, include the
83e30 20 70 74 68 72 65 61 64 73 20 68 65 61 64 65 72   pthreads header
83e40 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 74   and define.** t
83e50 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54  he SQLITE_UNIX_T
83e60 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f  HREADS macro..*/
83e70 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
83e80 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20  ADSAFE.# define 
83e90 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45  SQLITE_UNIX_THRE
83ea0 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ADS 1.#endif../*
83eb0 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d  .** Default perm
83ec0 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65  issions when cre
83ed0 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69 6c 65  ating a new file
83ee0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
83ef0 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
83f00 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65  PERMISSIONS.# de
83f10 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
83f20 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53  ULT_FILE_PERMISS
83f30 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 69 66  IONS 0644.#endif
83f40 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
83f50 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 2d 6c  supported path-l
83f60 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ength..*/.#defin
83f70 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 35  e MAX_PATHNAME 5
83f80 31 32 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75  12.../*.** The u
83f90 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72  nixFile structur
83fa0 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66  e is subclass of
83fb0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70   sqlite3_file sp
83fc0 65 63 69 66 69 63 20 66 6f 72 20 74 68 65 20 75  ecific for the u
83fd0 6e 69 78 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69  nix.** protabili
83fe0 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70  ty layer..*/.typ
83ff0 65 64 65 66 20 73 74 72 75 63 74 20 75 6e 69 78  edef struct unix
84000 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73  File unixFile;.s
84010 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 7b  truct unixFile {
84020 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  .  sqlite3_io_me
84030 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65  thods const *pMe
84040 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73  thod;  /* Always
84050 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
84060 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
84070 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 6e 20 74  E_TEST.  /* In t
84080 65 73 74 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61  est mode, increa
84090 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  se the size of t
840a0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 20  his structure a 
840b0 62 69 74 20 73 6f 20 74 68 61 74 20 0a 20 20 2a  bit so that .  *
840c0 2a 20 69 74 20 69 73 20 6c 61 72 67 65 72 20 74  * it is larger t
840d0 68 61 6e 20 74 68 65 20 73 74 72 75 63 74 20 43  han the struct C
840e0 72 61 73 68 46 69 6c 65 20 64 65 66 69 6e 65 64  rashFile defined
840f0 20 69 6e 20 74 65 73 74 36 2e 63 2e 0a 20 20 2a   in test6.c..  *
84100 2f 0a 20 20 63 68 61 72 20 61 50 61 64 64 69 6e  /.  char aPaddin
84110 67 5b 33 32 5d 3b 0a 23 65 6e 64 69 66 0a 20 20  g[32];.#endif.  
84120 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a  struct openCnt *
84130 70 4f 70 65 6e 3b 20 20 20 20 2f 2a 20 49 6e 66  pOpen;    /* Inf
84140 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e  o about all open
84150 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e   fd's on this in
84160 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ode */.  struct 
84170 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b  lockInfo *pLock;
84180 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74     /* Info about
84190 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73 20 69   locks on this i
841a0 6e 6f 64 65 20 2a 2f 0a 23 69 66 64 65 66 20 53  node */.#ifdef S
841b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
841c0 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 76 6f 69  KING_STYLE.  voi
841d0 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  d *lockingContex
841e0 74 3b 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e  t;     /* Lockin
841f0 67 20 73 74 79 6c 65 20 73 70 65 63 69 66 69 63  g style specific
84200 20 73 74 61 74 65 20 2a 2f 0a 23 65 6e 64 69 66   state */.#endif
84210 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
84220 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
84230 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20  */.  int h;     
84240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
84250 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72  * The file descr
84260 69 70 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67  iptor */.  unsig
84270 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70  ned char locktyp
84280 65 3b 20 20 20 2f 2a 20 54 68 65 20 74 79 70 65  e;   /* The type
84290 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e   of lock held on
842a0 20 74 68 69 73 20 66 64 20 2a 2f 0a 20 20 69 6e   this fd */.  in
842b0 74 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20  t dirfd;        
842c0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
842d0 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74  descriptor for t
842e0 68 65 20 64 69 72 65 63 74 6f 72 79 20 2a 2f 0a  he directory */.
842f0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
84300 44 53 41 46 45 0a 20 20 70 74 68 72 65 61 64 5f  DSAFE.  pthread_
84310 74 20 74 69 64 3b 20 20 20 20 20 20 20 20 20 20  t tid;          
84320 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64 20    /* The thread 
84330 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69 73  that "owns" this
84340 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e   unixFile */.#en
84350 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e  dif.};../*.** In
84360 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20  clude code that 
84370 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  is common to all
84380 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f   os_*.c files.*/
84390 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
843a0 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d   Include os_comm
843b0 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  on.h in the midd
843c0 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20  le of os_unix.c 
843d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
843e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
843f0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63   Begin file os_c
84400 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
84410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
84430 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
84440 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
84450 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
84460 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
84470 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
84480 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
84490 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
844a0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
844b0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
844c0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
844d0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
844e0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
844f0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
84500 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
84510 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
84520 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
84530 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
84540 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
84550 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
84560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
845a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
845b0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
845c0 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74  macros and a lit
845d0 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20  tle bit of code 
845e0 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
845f0 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20  o.** all of the 
84600 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69  platform-specifi
84610 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29  c files (os_*.c)
84620 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65   and is #include
84630 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20  d into those.** 
84640 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  files..**.** Thi
84650 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  s file should be
84660 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68   #included by th
84670 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f  e os_*.c files o
84680 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  nly.  It is not 
84690 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72  a.** general pur
846a0 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65  pose header file
846b0 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c  ..*/../*.** At l
846c0 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61  east two bugs ha
846d0 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65  ve slipped in be
846e0 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64  cause we changed
846f0 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55   the MEMORY_DEBU
84700 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51  G.** macro to SQ
84710 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73  LITE_DEBUG and s
84720 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69  ome older makefi
84730 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74  les have not yet
84740 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69   made the.** swi
84750 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  tch.  The follow
84760 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  ing code should 
84770 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c  catch this probl
84780 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  em at compile-ti
84790 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45  me..*/.#ifdef ME
847a0 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72  MORY_DEBUG.# err
847b0 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44  or "The MEMORY_D
847c0 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62  EBUG macro is ob
847d0 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c  solete.  Use SQL
847e0 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61  ITE_DEBUG instea
847f0 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  d.".#endif.../*.
84800 20 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c   * When testing,
84810 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72   this global var
84820 69 61 62 6c 65 20 73 74 6f 72 65 73 20 74 68 65  iable stores the
84830 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65   location of the
84840 0a 20 2a 20 70 65 6e 64 69 6e 67 2d 62 79 74 65  . * pending-byte
84850 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
84860 20 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65   file.. */.#ifde
84870 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
84880 4c 49 54 45 5f 41 50 49 20 75 6e 73 69 67 6e 65  LITE_API unsigne
84890 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 65  d int sqlite3_pe
848a0 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78 34  nding_byte = 0x4
848b0 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a  0000000;.#endif.
848c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
848d0 45 42 55 47 0a 53 51 4c 49 54 45 5f 41 50 49 20  EBUG.SQLITE_API 
848e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  int sqlite3_os_t
848f0 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e  race = 0;.#defin
84900 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 20  e OSTRACE1(X)   
84910 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
84920 33 5f 6f 73 5f 74 72 61 63 65 20 29 20 73 71 6c  3_os_trace ) sql
84930 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
84940 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  X).#define OSTRA
84950 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69  CE2(X,Y)       i
84960 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72  f( sqlite3_os_tr
84970 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
84980 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64  ugPrintf(X,Y).#d
84990 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58  efine OSTRACE3(X
849a0 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71  ,Y,Z)     if( sq
849b0 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65 20 29  lite3_os_trace )
849c0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
849d0 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69  ntf(X,Y,Z).#defi
849e0 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c  ne OSTRACE4(X,Y,
849f0 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74  Z,A)   if( sqlit
84a00 65 33 5f 6f 73 5f 74 72 61 63 65 20 29 20 73 71  e3_os_trace ) sq
84a10 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
84a20 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
84a30 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
84a40 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65  ,A,B) if( sqlite
84a50 33 5f 6f 73 5f 74 72 61 63 65 20 29 20 73 71 6c  3_os_trace ) sql
84a60 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
84a70 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
84a80 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
84a90 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69  Z,A,B,C) \.    i
84aa0 66 28 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61  f(sqlite3_os_tra
84ab0 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
84ac0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
84ad0 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,C).#define OSTR
84ae0 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE7(X,Y,Z,A,B,C
84af0 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c  ,D) \.    if(sql
84b00 69 74 65 33 5f 6f 73 5f 74 72 61 63 65 29 20 73  ite3_os_trace) s
84b10 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
84b20 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  f(X,Y,Z,A,B,C,D)
84b30 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
84b40 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69  STRACE1(X).#defi
84b50 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
84b60 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
84b70 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65  3(X,Y,Z).#define
84b80 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c   OSTRACE4(X,Y,Z,
84b90 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
84ba0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  CE5(X,Y,Z,A,B).#
84bb0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
84bc0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
84bd0 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
84be0 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e  Y,Z,A,B,C,D).#en
84bf0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  dif../*.** Macro
84c00 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  s for performanc
84c10 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d  e tracing.  Norm
84c20 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e  ally turned off.
84c30 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20    Only works.** 
84c40 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65  on i486 hardware
84c50 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
84c60 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54  TE_PERFORMANCE_T
84c70 52 41 43 45 0a 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  RACE.__inline__ 
84c80 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
84c90 6e 67 20 69 6e 74 20 68 77 74 69 6d 65 28 76 6f  ng int hwtime(vo
84ca0 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  id){.  unsigned 
84cb0 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 78 3b  long long int x;
84cc0 0a 20 20 5f 5f 61 73 6d 5f 5f 28 22 72 64 74 73  .  __asm__("rdts
84cd0 63 5c 6e 5c 74 22 0a 20 20 20 20 20 20 20 20 20  c\n\t".         
84ce0 20 22 6d 6f 76 20 25 25 65 64 78 2c 20 25 25 65   "mov %%edx, %%e
84cf0 63 78 5c 6e 5c 74 22 0a 20 20 20 20 20 20 20 20  cx\n\t".        
84d00 20 20 3a 22 3d 41 22 20 28 78 29 29 3b 0a 20 20    :"=A" (x));.  
84d10 72 65 74 75 72 6e 20 78 3b 0a 7d 0a 73 74 61 74  return x;.}.stat
84d20 69 63 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ic unsigned long
84d30 20 6c 6f 6e 67 20 69 6e 74 20 67 5f 73 74 61 72   long int g_star
84d40 74 3b 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e  t;.static unsign
84d50 65 64 20 69 6e 74 20 65 6c 61 70 73 65 3b 0a 23  ed int elapse;.#
84d60 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41  define TIMER_STA
84d70 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74  RT       g_start
84d80 3d 68 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e  =hwtime().#defin
84d90 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20  e TIMER_END     
84da0 20 20 20 20 65 6c 61 70 73 65 3d 68 77 74 69 6d      elapse=hwtim
84db0 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66  e()-g_start.#def
84dc0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
84dd0 44 20 20 20 20 20 65 6c 61 70 73 65 0a 23 65 6c  D     elapse.#el
84de0 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  se.#define TIMER
84df0 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54  _START.#define T
84e00 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65  IMER_END.#define
84e10 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
84e20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a     0.#endif../*.
84e30 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65  ** If we compile
84e40 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
84e50 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c  _TEST macro set,
84e60 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77   then the follow
84e70 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20  ing block.** of 
84e80 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75  code will give u
84e90 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f  s the ability to
84ea0 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b   simulate a disk
84eb0 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69   I/O error.  Thi
84ec0 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72  s.** is used for
84ed0 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f   testing the I/O
84ee0 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e   recovery logic.
84ef0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
84f00 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
84f10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
84f20 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 0a  _error_hit = 0;.
84f30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
84f40 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
84f50 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c  pending = 0;.SQL
84f60 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
84f70 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72  te3_io_error_per
84f80 73 69 73 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45  sist = 0;.SQLITE
84f90 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
84fa0 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
84fb0 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50  g = 0;.SQLITE_AP
84fc0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
84fd0 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66  skfull = 0;.#def
84fe0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
84ff0 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69  ror(CODE)  \.  i
85000 66 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  f( sqlite3_io_er
85010 72 6f 72 5f 70 65 6e 64 69 6e 67 20 7c 7c 20 73  ror_pending || s
85020 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
85030 68 69 74 20 29 20 5c 0a 20 20 20 20 20 69 66 28  hit ) \.     if(
85040 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
85050 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31  r_pending-- == 1
85060 20 5c 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28   \.         || (
85070 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
85080 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69  _persist && sqli
85090 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
850a0 29 20 29 20 5c 0a 20 20 20 20 20 20 20 20 20 20  ) ) \.          
850b0 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f        { local_io
850c0 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73  err(); CODE; }.s
850d0 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c  tatic void local
850e0 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52  _ioerr(){.  IOTR
850f0 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29  ACE(("IOERR\n"))
85100 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  ;.  sqlite3_io_e
85110 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 0a 7d 0a  rror_hit = 1;.}.
85120 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
85130 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f  DiskfullError(CO
85140 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c  DE) \.   if( sql
85150 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
85160 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20  nding ){ \.     
85170 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  if( sqlite3_disk
85180 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20  full_pending == 
85190 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f  1 ){ \.       lo
851a0 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20  cal_ioerr(); \. 
851b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69        sqlite3_di
851c0 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20  skfull = 1; \.  
851d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f       sqlite3_io_
851e0 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c  error_hit = 1; \
851f0 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a  .       CODE; \.
85200 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20       }else{ \.  
85210 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
85220 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b  kfull_pending--;
85230 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d   \.     } \.   }
85240 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53  .#else.#define S
85250 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41  imulateIOError(A
85260 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
85270 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
85280 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  A).#endif../*.**
85290 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b   When testing, k
852a0 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74  eep a count of t
852b0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65  he number of ope
852c0 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64  n files..*/.#ifd
852d0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
852e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
852f0 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
85300 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69  count = 0;.#defi
85310 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
85320 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  )  sqlite3_open_
85330 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a  file_count+=(X).
85340 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70  #else.#define Op
85350 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e  enCounter(X).#en
85360 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
85370 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63  **** End of os_c
85380 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
85390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
853a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
853b0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
853c0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
853d0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
853e0 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a  f in os_unix.c *
853f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85400 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69  ***/../*.** Defi
85410 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f  ne various macro
85420 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 69  s that are missi
85430 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73  ng from some sys
85440 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tems..*/.#ifndef
85450 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64   O_LARGEFILE.# d
85460 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c  efine O_LARGEFIL
85470 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  E 0.#endif.#ifde
85480 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  f SQLITE_DISABLE
85490 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c  _LFS.# undef O_L
854a0 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e  ARGEFILE.# defin
854b0 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a  e O_LARGEFILE 0.
854c0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f  #endif.#ifndef O
854d0 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69  _NOFOLLOW.# defi
854e0 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a  ne O_NOFOLLOW 0.
854f0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f  #endif.#ifndef O
85500 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65  _BINARY.# define
85510 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64   O_BINARY 0.#end
85520 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a  if../*.** The DJ
85530 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76  GPP compiler env
85540 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d  ironment looks m
85550 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c  ostly like Unix,
85560 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73   but it.** lacks
85570 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73   the fcntl() sys
85580 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65  tem call.  So re
85590 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74  define fcntl() t
855a0 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a  o be something.*
855b0 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 75  * that always su
855c0 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65  cceeds.  This me
855d0 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67  ans that locking
855e0 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20   does not occur 
855f0 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20  under.** DJGPP. 
85600 20 42 75 74 20 69 74 27 73 20 44 4f 53 20 2d 20   But it's DOS - 
85610 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 78 70  what did you exp
85620 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 5f  ect?.*/.#ifdef _
85630 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 6e  _DJGPP__.# defin
85640 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 30  e fcntl(A,B,C) 0
85650 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
85660 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 72  he threadid macr
85670 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68  o resolves to th
85680 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74  e thread-id or t
85690 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a  o 0.  Used for.*
856a0 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  * testing and de
856b0 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f  bugging only..*/
856c0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
856d0 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74  ADSAFE.#define t
856e0 68 72 65 61 64 69 64 20 70 74 68 72 65 61 64 5f  hreadid pthread_
856f0 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65  self().#else.#de
85700 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a  fine threadid 0.
85710 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65  #endif../*.** Se
85720 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65 20 75  t or check the u
85730 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69 65 6c  nixFile.tid fiel
85740 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64 20 69  d.  This field i
85750 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20 75 6e  s set when an un
85760 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 69 72  ixFile.** is fir
85770 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c 6c 20  st opened.  All 
85780 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 73 20  subsequent uses 
85790 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20  of the unixFile 
857a0 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 0a  verify that the.
857b0 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64 20 69  ** same thread i
857c0 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20 74  s operating on t
857d0 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 53 6f  he unixFile.  So
857e0 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  me operating sys
857f0 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61  tems do.** not a
85800 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 62 65  llow locks to be
85810 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6f   overridden by o
85820 74 68 65 72 20 74 68 72 65 61 64 73 20 61 6e 64  ther threads and
85830 20 74 68 61 74 20 72 65 73 74 72 69 63 74 69 6f   that restrictio
85840 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20  n.** means that 
85850 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73  sqlite3* databas
85860 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e 6f 74  e handles cannot
85870 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f   be moved from o
85880 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 6f 20  ne thread.** to 
85890 61 6e 6f 74 68 65 72 2e 20 20 54 68 69 73 20 6c  another.  This l
858a0 6f 67 69 63 20 6d 61 6b 65 73 20 73 75 72 65 20  ogic makes sure 
858b0 61 20 75 73 65 72 20 64 6f 65 73 20 6e 6f 74 20  a user does not 
858c0 74 72 79 20 74 6f 20 64 6f 20 74 68 61 74 0a 2a  try to do that.*
858d0 2a 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a 2a 2a  * by mistake..**
858e0 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e  .** Version 3.3.
858f0 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3a 20  1 (2006-01-15): 
85900 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 62 65   unixFile can be
85910 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20   moved from one 
85920 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f  thread to.** ano
85930 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 73 20  ther as long as 
85940 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 6f  we are running o
85950 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61 74 20  n a system that 
85960 73 75 70 70 6f 72 74 73 20 74 68 72 65 61 64 73  supports threads
85970 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 20 65  .** overriding e
85980 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
85990 20 28 77 68 69 63 68 20 6e 6f 77 20 74 68 65 20   (which now the 
859a0 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61  most common beha
859b0 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e  vior).** or if n
859c0 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64  o locks are held
859d0 2e 20 20 42 75 74 20 74 68 65 20 75 6e 69 78 46  .  But the unixF
859e0 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20  ile.pLock field 
859f0 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72  needs to be.** r
85a00 65 63 6f 6d 70 75 74 65 64 20 62 65 63 61 75 73  ecomputed becaus
85a10 65 20 69 74 73 20 6b 65 79 20 69 6e 63 6c 75 64  e its key includ
85a20 65 73 20 74 68 65 20 74 68 72 65 61 64 2d 69 64  es the thread-id
85a30 2e 20 20 53 65 65 20 74 68 65 20 0a 2a 2a 20 74  .  See the .** t
85a40 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70  ransferOwnership
85a50 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f  () function belo
85a60 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  w for additional
85a70 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a   information.*/.
85a80 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
85a90 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53  DSAFE.# define S
85aa0 45 54 5f 54 48 52 45 41 44 49 44 28 58 29 20 20  ET_THREADID(X)  
85ab0 20 28 58 29 2d 3e 74 69 64 20 3d 20 70 74 68 72   (X)->tid = pthr
85ac0 65 61 64 5f 73 65 6c 66 28 29 0a 23 20 64 65 66  ead_self().# def
85ad0 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44  ine CHECK_THREAD
85ae0 49 44 28 58 29 20 28 74 68 72 65 61 64 73 4f 76  ID(X) (threadsOv
85af0 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
85b00 4c 6f 63 6b 73 3d 3d 30 20 26 26 20 5c 0a 20 20  Locks==0 && \.  
85b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85b20 20 20 20 20 20 20 20 20 20 20 21 70 74 68 72 65            !pthre
85b30 61 64 5f 65 71 75 61 6c 28 28 58 29 2d 3e 74 69  ad_equal((X)->ti
85b40 64 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28  d, pthread_self(
85b50 29 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ))).#else.# defi
85b60 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49 44 28  ne SET_THREADID(
85b70 58 29 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43  X).# define CHEC
85b80 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20 30 0a  K_THREADID(X) 0.
85b90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 48 65  #endif../*.** He
85ba0 72 65 20 69 73 20 74 68 65 20 64 69 72 74 20 6f  re is the dirt o
85bb0 6e 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79  n POSIX advisory
85bc0 20 6c 6f 63 6b 73 3a 20 20 41 4e 53 49 20 53 54   locks:  ANSI ST
85bd0 44 20 31 30 30 33 2e 31 20 28 31 39 39 36 29 0a  D 1003.1 (1996).
85be0 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32  ** section 6.5.2
85bf0 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74 68 72  .2 lines 483 thr
85c00 6f 75 67 68 20 34 39 30 20 73 70 65 63 69 66 79  ough 490 specify
85c10 20 74 68 61 74 20 77 68 65 6e 20 61 20 70 72 6f   that when a pro
85c20 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20  cess.** sets or 
85c30 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74  clears a lock, t
85c40 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76  hat operation ov
85c50 65 72 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f  errides any prio
85c60 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62  r locks set.** b
85c70 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  y the same proce
85c80 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74  ss.  It does not
85c90 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20   explicitly say 
85ca0 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70  so, but this imp
85cb0 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 20  lies.** that it 
85cc0 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20  overrides locks 
85cd0 73 65 74 20 62 79 20 74 68 65 20 73 61 6d 65 20  set by the same 
85ce0 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20  process using a 
85cf0 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c  different.** fil
85d00 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43  e descriptor.  C
85d10 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74 65 73  onsider this tes
85d20 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  t case:.**.**   
85d30 20 20 20 20 69 6e 74 20 66 64 31 20 3d 20 6f 70      int fd1 = op
85d40 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f  en("./file1", O_
85d50 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36  RDWR|O_CREAT, 06
85d60 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e  44);.**       in
85d70 74 20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f  t fd2 = open("./
85d80 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f  file2", O_RDWR|O
85d90 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a  _CREAT, 0644);.*
85da0 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66  *.** Suppose ./f
85db0 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32  ile1 and ./file2
85dc0 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20   are really the 
85dd0 73 61 6d 65 20 66 69 6c 65 20 28 62 65 63 61 75  same file (becau
85de0 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68  se.** one is a h
85df0 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20  ard or symbolic 
85e00 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 65  link to the othe
85e10 72 29 20 74 68 65 6e 20 69 66 20 79 6f 75 20 73  r) then if you s
85e20 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69  et.** an exclusi
85e30 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20  ve lock on fd1, 
85e40 74 68 65 6e 20 74 72 79 20 74 6f 20 67 65 74 20  then try to get 
85e50 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
85e60 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20  k.** on fd2, it 
85e70 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20  works.  I would 
85e80 68 61 76 65 20 65 78 70 65 63 74 65 64 20 74 68  have expected th
85e90 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f  e second lock to
85ea0 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 74  .** fail since t
85eb0 68 65 72 65 20 77 61 73 20 61 6c 72 65 61 64 79  here was already
85ec0 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66   a lock on the f
85ed0 69 6c 65 20 64 75 65 20 74 6f 20 66 64 31 2e 0a  ile due to fd1..
85ee0 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20  ** But not so.  
85ef0 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73  Since both locks
85f00 20 63 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 73   came from the s
85f10 61 6d 65 20 70 72 6f 63 65 73 73 2c 20 74 68 65  ame process, the
85f20 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 72  .** second overr
85f30 69 64 65 73 20 74 68 65 20 66 69 72 73 74 2c 20  ides the first, 
85f40 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79  even though they
85f50 20 77 65 72 65 20 6f 6e 20 64 69 66 66 65 72 65   were on differe
85f60 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72  nt.** file descr
85f70 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e  iptors opened on
85f80 20 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20   different file 
85f90 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 6d  names..**.** Bum
85fa0 6d 65 72 2e 20 20 49 66 20 79 6f 75 20 61 73 6b  mer.  If you ask
85fb0 20 6d 65 2c 20 74 68 69 73 20 69 73 20 62 72 6f   me, this is bro
85fc0 6b 65 6e 2e 20 20 42 61 64 6c 79 20 62 72 6f 6b  ken.  Badly brok
85fd0 65 6e 2e 20 20 49 74 20 6d 65 61 6e 73 0a 2a 2a  en.  It means.**
85fe0 20 74 68 61 74 20 77 65 20 63 61 6e 6e 6f 74 20   that we cannot 
85ff0 75 73 65 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20  use POSIX locks 
86000 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 66  to synchronize f
86010 69 6c 65 20 61 63 63 65 73 73 20 61 6d 6f 6e 67  ile access among
86020 0a 2a 2a 20 63 6f 6d 70 65 74 69 6e 67 20 74 68  .** competing th
86030 72 65 61 64 73 20 6f 66 20 74 68 65 20 73 61 6d  reads of the sam
86040 65 20 70 72 6f 63 65 73 73 2e 20 20 50 4f 53 49  e process.  POSI
86050 58 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 77 6f 72  X locks will wor
86060 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f 20 73 79 6e  k fine.** to syn
86070 63 68 72 6f 6e 69 7a 65 20 61 63 63 65 73 73 20  chronize access 
86080 66 6f 72 20 74 68 72 65 61 64 73 20 69 6e 20 73  for threads in s
86090 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65  eparate processe
860a0 73 2c 20 62 75 74 20 6e 6f 74 0a 2a 2a 20 74 68  s, but not.** th
860b0 72 65 61 64 73 20 77 69 74 68 69 6e 20 74 68 65  reads within the
860c0 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 0a 2a   same process..*
860d0 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f  *.** To work aro
860e0 75 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c  und the problem,
860f0 20 53 51 4c 69 74 65 20 68 61 73 20 74 6f 20 6d   SQLite has to m
86100 61 6e 61 67 65 20 66 69 6c 65 20 6c 6f 63 6b 73  anage file locks
86110 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 6f   internally.** o
86120 6e 20 69 74 73 20 6f 77 6e 2e 20 20 57 68 65 6e  n its own.  When
86130 65 76 65 72 20 61 20 6e 65 77 20 64 61 74 61 62  ever a new datab
86140 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 77  ase is opened, w
86150 65 20 68 61 76 65 20 74 6f 20 66 69 6e 64 20 74  e have to find t
86160 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 69  he.** specific i
86170 6e 6f 64 65 20 6f 66 20 74 68 65 20 64 61 74 61  node of the data
86180 62 61 73 65 20 66 69 6c 65 20 28 74 68 65 20 69  base file (the i
86190 6e 6f 64 65 20 69 73 20 64 65 74 65 72 6d 69 6e  node is determin
861a0 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 74 5f  ed by the.** st_
861b0 64 65 76 20 61 6e 64 20 73 74 5f 69 6e 6f 20 66  dev and st_ino f
861c0 69 65 6c 64 73 20 6f 66 20 74 68 65 20 73 74 61  ields of the sta
861d0 74 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  t structure that
861e0 20 66 73 74 61 74 28 29 20 66 69 6c 6c 73 20 69   fstat() fills i
861f0 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 65 63 6b 20  n).** and check 
86200 66 6f 72 20 6c 6f 63 6b 73 20 61 6c 72 65 61 64  for locks alread
86210 79 20 65 78 69 73 74 69 6e 67 20 6f 6e 20 74 68  y existing on th
86220 61 74 20 69 6e 6f 64 65 2e 20 20 57 68 65 6e 20  at inode.  When 
86230 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 63 72 65  locks are.** cre
86240 61 74 65 64 20 6f 72 20 72 65 6d 6f 76 65 64 2c  ated or removed,
86250 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 6f 6b   we have to look
86260 20 61 74 20 6f 75 72 20 6f 77 6e 20 69 6e 74 65   at our own inte
86270 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 20 74  rnal record of t
86280 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 74 6f 20 73  he.** locks to s
86290 65 65 20 69 66 20 61 6e 6f 74 68 65 72 20 74 68  ee if another th
862a0 72 65 61 64 20 68 61 73 20 70 72 65 76 69 6f 75  read has previou
862b0 73 6c 79 20 73 65 74 20 61 20 6c 6f 63 6b 20 6f  sly set a lock o
862c0 6e 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 69  n that same.** i
862d0 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  node..**.** The 
862e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72  sqlite3_file str
862f0 75 63 74 75 72 65 20 66 6f 72 20 50 4f 53 49 58  ucture for POSIX
86300 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75   is no longer ju
86310 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 66 69  st an integer fi
86320 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72  le.** descriptor
86330 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 61 20 73  .  It is now a s
86340 74 72 75 63 74 75 72 65 20 74 68 61 74 20 68 6f  tructure that ho
86350 6c 64 73 20 74 68 65 20 69 6e 74 65 67 65 72 20  lds the integer 
86360 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74  file.** descript
86370 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72  or and a pointer
86380 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20   to a structure 
86390 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
863a0 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c  he internal.** l
863b0 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72 72  ocks on the corr
863c0 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e  esponding inode.
863d0 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6c    There is one l
863e0 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65  ocking structure
863f0 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20 73  .** per inode, s
86400 6f 20 69 66 20 74 68 65 20 73 61 6d 65 20 69 6e  o if the same in
86410 6f 64 65 20 69 73 20 6f 70 65 6e 65 64 20 74 77  ode is opened tw
86420 69 63 65 2c 20 62 6f 74 68 20 75 6e 69 78 46 69  ice, both unixFi
86430 6c 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a  le structures.**
86440 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 61   point to the sa
86450 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63  me locking struc
86460 74 75 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69  ture.  The locki
86470 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  ng structure kee
86480 70 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63  ps.** a referenc
86490 65 20 63 6f 75 6e 74 20 28 73 6f 20 77 65 20 77  e count (so we w
864a0 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f  ill know when to
864b0 20 64 65 6c 65 74 65 20 69 74 29 20 61 6e 64 20   delete it) and 
864c0 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64  a "cnt".** field
864d0 20 74 68 61 74 20 74 65 6c 6c 73 20 75 73 20 69   that tells us i
864e0 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b  ts internal lock
864f0 20 73 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30   status.  cnt==0
86500 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69   means the.** fi
86510 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20  le is unlocked. 
86520 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74   cnt==-1 means t
86530 68 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 65  he file has an e
86540 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a  xclusive lock..*
86550 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68  * cnt>0 means th
86560 65 72 65 20 61 72 65 20 63 6e 74 20 73 68 61 72  ere are cnt shar
86570 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20  ed locks on the 
86580 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20  file..**.** Any 
86590 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20  attempt to lock 
865a0 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65  or unlock a file
865b0 20 66 69 72 73 74 20 63 68 65 63 6b 73 20 74 68   first checks th
865c0 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72  e locking.** str
865d0 75 63 74 75 72 65 2e 20 20 54 68 65 20 66 63 6e  ucture.  The fcn
865e0 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c  tl() system call
865f0 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64   is only invoked
86600 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f   to set a .** PO
86610 53 49 58 20 6c 6f 63 6b 20 69 66 20 74 68 65 20  SIX lock if the 
86620 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74  internal lock st
86630 72 75 63 74 75 72 65 20 74 72 61 6e 73 69 74 69  ructure transiti
86640 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61  ons between.** a
86650 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 75   locked and an u
86660 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a  nlocked state..*
86670 2a 0a 2a 2a 20 32 30 30 34 2d 4a 61 6e 2d 31 31  *.** 2004-Jan-11
86680 3a 0a 2a 2a 20 4d 6f 72 65 20 72 65 63 65 6e 74  :.** More recent
86690 20 64 69 73 63 6f 76 65 72 69 65 73 20 61 62 6f   discoveries abo
866a0 75 74 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72  ut POSIX advisor
866b0 79 20 6c 6f 63 6b 73 2e 20 20 28 54 68 65 20 6d  y locks.  (The m
866c0 6f 72 65 0a 2a 2a 20 49 20 64 69 73 63 6f 76 65  ore.** I discove
866d0 72 2c 20 74 68 65 20 6d 6f 72 65 20 49 20 72 65  r, the more I re
866e0 61 6c 69 7a 65 20 74 68 65 20 61 20 50 4f 53 49  alize the a POSI
866f0 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  X advisory locks
86700 20 61 72 65 0a 2a 2a 20 61 6e 20 61 62 6f 6d 69   are.** an abomi
86710 6e 61 74 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 49  nation.).**.** I
86720 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 69  f you close a fi
86730 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68  le descriptor th
86740 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66  at points to a f
86750 69 6c 65 20 74 68 61 74 20 68 61 73 20 6c 6f 63  ile that has loc
86760 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73  ks,.** all locks
86770 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68   on that file th
86780 61 74 20 61 72 65 20 6f 77 6e 65 64 20 62 79 20  at are owned by 
86790 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63  the current proc
867a0 65 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61  ess are.** relea
867b0 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72  sed.  To work ar
867c0 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65  ound this proble
867d0 6d 2c 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65  m, each unixFile
867e0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
867f0 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  ins.** a pointer
86800 20 74 6f 20 61 6e 20 6f 70 65 6e 43 6e 74 20 73   to an openCnt s
86810 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65  tructure.  There
86820 20 69 73 20 6f 6e 65 20 6f 70 65 6e 43 6e 74 20   is one openCnt 
86830 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65 72  structure.** per
86840 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20 77 68 69   open inode, whi
86850 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6d 75  ch means that mu
86860 6c 74 69 70 6c 65 20 75 6e 69 78 46 69 6c 65 20  ltiple unixFile 
86870 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20 61 20 73  can point to a s
86880 69 6e 67 6c 65 0a 2a 2a 20 6f 70 65 6e 43 6e 74  ingle.** openCnt
86890 2e 20 20 57 68 65 6e 20 61 6e 20 61 74 74 65 6d  .  When an attem
868a0 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c  pt is made to cl
868b0 6f 73 65 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c  ose an unixFile,
868c0 20 69 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a   if there are.**
868d0 20 6f 74 68 65 72 20 75 6e 69 78 46 69 6c 65 20   other unixFile 
868e0 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  open on the same
868f0 20 69 6e 6f 64 65 20 74 68 61 74 20 61 72 65 20   inode that are 
86900 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74  holding locks, t
86910 68 65 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c  he call.** to cl
86920 6f 73 65 28 29 20 74 68 65 20 66 69 6c 65 20 64  ose() the file d
86930 65 73 63 72 69 70 74 6f 72 20 69 73 20 64 65 66  escriptor is def
86940 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20  erred until all 
86950 6f 66 20 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65  of the locks cle
86960 61 72 2e 0a 2a 2a 20 54 68 65 20 6f 70 65 6e 43  ar..** The openC
86970 6e 74 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  nt structure kee
86980 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c  ps a list of fil
86990 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 68  e descriptors th
869a0 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65  at need to.** be
869b0 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74   closed and that
869c0 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20   list is walked 
869d0 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68  (and cleared) wh
869e0 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b  en the last lock
869f0 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a  .** clears..**.*
86a00 2a 20 46 69 72 73 74 2c 20 75 6e 64 65 72 20 4c  * First, under L
86a10 69 6e 75 78 20 74 68 72 65 61 64 73 2c 20 62 65  inux threads, be
86a20 63 61 75 73 65 20 65 61 63 68 20 74 68 72 65 61  cause each threa
86a30 64 20 68 61 73 20 61 20 73 65 70 61 72 61 74 65  d has a separate
86a40 0a 2a 2a 20 70 72 6f 63 65 73 73 20 49 44 2c 20  .** process ID, 
86a50 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e 73 20  lock operations 
86a60 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 64 6f  in one thread do
86a70 20 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 6c 6f   not override lo
86a80 63 6b 73 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61  cks.** to the sa
86a90 6d 65 20 66 69 6c 65 20 69 6e 20 6f 74 68 65 72  me file in other
86aa0 20 74 68 72 65 61 64 73 2e 20 20 4c 69 6e 75 78   threads.  Linux
86ab0 20 74 68 72 65 61 64 73 20 62 65 68 61 76 65 20   threads behave 
86ac0 6c 69 6b 65 0a 2a 2a 20 73 65 70 61 72 61 74 65  like.** separate
86ad0 20 70 72 6f 63 65 73 73 65 73 20 69 6e 20 74 68   processes in th
86ae0 69 73 20 72 65 73 70 65 63 74 2e 20 20 42 75 74  is respect.  But
86af0 2c 20 69 66 20 79 6f 75 20 63 6c 6f 73 65 20 61  , if you close a
86b00 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70   file.** descrip
86b10 74 6f 72 20 69 6e 20 6c 69 6e 75 78 20 74 68 72  tor in linux thr
86b20 65 61 64 73 2c 20 61 6c 6c 20 6c 6f 63 6b 73 20  eads, all locks 
86b30 61 72 65 20 63 6c 65 61 72 65 64 2c 20 65 76 65  are cleared, eve
86b40 6e 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 6e 20 6f 74  n locks.** on ot
86b50 68 65 72 20 74 68 72 65 61 64 73 20 61 6e 64 20  her threads and 
86b60 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20  even though the 
86b70 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 68 61  other threads ha
86b80 76 65 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20  ve different.** 
86b90 70 72 6f 63 65 73 73 20 49 44 73 2e 20 20 4c 69  process IDs.  Li
86ba0 6e 75 78 20 74 68 72 65 61 64 73 20 69 73 20 69  nux threads is i
86bb0 6e 63 6f 6e 73 69 73 74 65 6e 74 20 69 6e 20 74  nconsistent in t
86bc0 68 69 73 20 72 65 73 70 65 63 74 2e 0a 2a 2a 20  his respect..** 
86bd0 28 49 27 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74  (I'm beginning t
86be0 6f 20 74 68 69 6e 6b 20 74 68 61 74 20 6c 69 6e  o think that lin
86bf0 75 78 20 74 68 72 65 61 64 73 20 69 73 20 61 6e  ux threads is an
86c00 20 61 62 6f 6d 69 6e 61 74 69 6f 6e 20 74 6f 6f   abomination too
86c10 2e 29 0a 2a 2a 20 54 68 65 20 63 6f 6e 73 65 71  .).** The conseq
86c20 75 65 6e 63 65 20 6f 66 20 74 68 69 73 20 61 6c  uence of this al
86c30 6c 20 69 73 20 74 68 61 74 20 74 68 65 20 68 61  l is that the ha
86c40 73 68 20 74 61 62 6c 65 20 66 6f 72 20 74 68 65  sh table for the
86c50 20 6c 6f 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72   lockInfo.** str
86c60 75 63 74 75 72 65 20 68 61 73 20 74 6f 20 69 6e  ucture has to in
86c70 63 6c 75 64 65 20 74 68 65 20 70 72 6f 63 65 73  clude the proces
86c80 73 20 69 64 20 61 73 20 70 61 72 74 20 6f 66 20  s id as part of 
86c90 69 74 73 20 6b 65 79 20 62 65 63 61 75 73 65 0a  its key because.
86ca0 2a 2a 20 6c 6f 63 6b 73 20 69 6e 20 64 69 66 66  ** locks in diff
86cb0 65 72 65 6e 74 20 74 68 72 65 61 64 73 20 61 72  erent threads ar
86cc0 65 20 74 72 65 61 74 65 64 20 61 73 20 64 69 73  e treated as dis
86cd0 74 69 6e 63 74 2e 20 20 42 75 74 20 74 68 65 20  tinct.  But the 
86ce0 0a 2a 2a 20 6f 70 65 6e 43 6e 74 20 73 74 72 75  .** openCnt stru
86cf0 63 74 75 72 65 20 73 68 6f 75 6c 64 20 6e 6f 74  cture should not
86d00 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70 72 6f   include the pro
86d10 63 65 73 73 20 69 64 20 69 6e 20 69 74 73 0a 2a  cess id in its.*
86d20 2a 20 6b 65 79 20 62 65 63 61 75 73 65 20 63 6c  * key because cl
86d30 6f 73 65 28 29 20 63 6c 65 61 72 73 20 6c 6f 63  ose() clears loc
86d40 6b 20 6f 6e 20 61 6c 6c 20 74 68 72 65 61 64 73  k on all threads
86d50 2c 20 6e 6f 74 20 6a 75 73 74 20 74 68 65 20 63  , not just the c
86d60 75 72 72 65 6e 74 0a 2a 2a 20 74 68 72 65 61 64  urrent.** thread
86d70 2e 20 20 57 65 72 65 20 69 74 20 6e 6f 74 20 66  .  Were it not f
86d80 6f 72 20 74 68 69 73 20 67 6f 6f 66 69 6e 65 73  or this goofines
86d90 73 20 69 6e 20 6c 69 6e 75 78 20 74 68 72 65 61  s in linux threa
86da0 64 73 2c 20 77 65 20 63 6f 75 6c 64 0a 2a 2a 20  ds, we could.** 
86db0 63 6f 6d 62 69 6e 65 20 74 68 65 20 6c 6f 63 6b  combine the lock
86dc0 49 6e 66 6f 20 61 6e 64 20 6f 70 65 6e 43 6e 74  Info and openCnt
86dd0 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 74 6f   structures into
86de0 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74   a single struct
86df0 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 34 2d  ure..**.** 2004-
86e00 4a 75 6e 2d 32 38 3a 0a 2a 2a 20 4f 6e 20 73 6f  Jun-28:.** On so
86e10 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 6c  me versions of l
86e20 69 6e 75 78 2c 20 74 68 72 65 61 64 73 20 63 61  inux, threads ca
86e30 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  n override each 
86e40 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 0a 2a 2a  others locks..**
86e50 20 4f 6e 20 6f 74 68 65 72 73 20 6e 6f 74 2e 20   On others not. 
86e60 20 53 6f 6d 65 74 69 6d 65 73 20 79 6f 75 20 63   Sometimes you c
86e70 61 6e 20 63 68 61 6e 67 65 20 74 68 65 20 62 65  an change the be
86e80 68 61 76 69 6f 72 20 6f 6e 20 74 68 65 20 73 61  havior on the sa
86e90 6d 65 0a 2a 2a 20 73 79 73 74 65 6d 20 62 79 20  me.** system by 
86ea0 73 65 74 74 69 6e 67 20 74 68 65 20 4c 44 5f 41  setting the LD_A
86eb0 53 53 55 4d 45 5f 4b 45 52 4e 45 4c 20 65 6e 76  SSUME_KERNEL env
86ec0 69 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c  ironment variabl
86ed0 65 2e 20 20 54 68 65 0a 2a 2a 20 50 4f 53 49 58  e.  The.** POSIX
86ee0 20 73 74 61 6e 64 61 72 64 20 69 73 20 73 69 6c   standard is sil
86ef0 65 6e 74 20 61 73 20 74 6f 20 77 68 69 63 68 20  ent as to which 
86f00 62 65 68 61 76 69 6f 72 20 69 73 20 63 6f 72 72  behavior is corr
86f10 65 63 74 2c 20 61 73 20 66 61 72 0a 2a 2a 20 61  ect, as far.** a
86f20 73 20 49 20 63 61 6e 20 74 65 6c 6c 2c 20 73 6f  s I can tell, so
86f30 20 6f 74 68 65 72 20 76 65 72 73 69 6f 6e 73 20   other versions 
86f40 6f 66 20 75 6e 69 78 20 6d 69 67 68 74 20 73 68  of unix might sh
86f50 6f 77 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 69  ow the same.** i
86f60 6e 63 6f 6e 73 69 73 74 65 6e 63 79 2e 20 20 54  nconsistency.  T
86f70 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 74 74 6c  here is no littl
86f80 65 20 64 6f 75 62 74 20 69 6e 20 6d 79 20 6d 69  e doubt in my mi
86f90 6e 64 20 74 68 61 74 20 70 6f 73 69 78 0a 2a 2a  nd that posix.**
86fa0 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20   advisory locks 
86fb0 61 6e 64 20 6c 69 6e 75 78 20 74 68 72 65 61 64  and linux thread
86fc0 73 20 61 72 65 20 70 72 6f 66 6f 75 6e 64 6c 79  s are profoundly
86fd0 20 62 72 6f 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54   broken..**.** T
86fe0 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  o work around th
86ff0 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 69 65  e inconsistencie
87000 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 74 65  s, we have to te
87010 73 74 20 61 74 20 72 75 6e 74 69 6d 65 20 0a 2a  st at runtime .*
87020 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * whether or not
87030 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65   threads can ove
87040 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
87050 73 20 6c 6f 63 6b 73 2e 20 20 54 68 69 73 20 74  s locks.  This t
87060 65 73 74 0a 2a 2a 20 69 73 20 72 75 6e 20 6f 6e  est.** is run on
87070 63 65 2c 20 74 68 65 20 66 69 72 73 74 20 74 69  ce, the first ti
87080 6d 65 20 61 6e 79 20 6c 6f 63 6b 20 69 73 20 61  me any lock is a
87090 74 74 65 6d 70 74 65 64 2e 20 20 41 20 73 74 61  ttempted.  A sta
870a0 74 69 63 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65  tic .** variable
870b0 20 69 73 20 73 65 74 20 74 6f 20 72 65 63 6f 72   is set to recor
870c0 64 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  d the results of
870d0 20 74 68 69 73 20 74 65 73 74 20 66 6f 72 20 66   this test for f
870e0 75 74 75 72 65 0a 2a 2a 20 75 73 65 2e 0a 2a 2f  uture.** use..*/
870f0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
87100 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
87110 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73  wing structure s
87120 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79  erves as the key
87130 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61   used.** to loca
87140 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  te a particular 
87150 6c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75  lockInfo structu
87160 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f  re given its ino
87170 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 72  de..**.** If thr
87180 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72  eads cannot over
87190 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
871a0 20 6c 6f 63 6b 73 2c 20 74 68 65 6e 20 77 65 20   locks, then we 
871b0 73 65 74 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 4b  set the.** lockK
871c0 65 79 2e 74 69 64 20 66 69 65 6c 64 20 74 6f 20  ey.tid field to 
871d0 74 68 65 20 74 68 72 65 61 64 20 49 44 2e 20 20  the thread ID.  
871e0 49 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f  If threads can o
871f0 76 65 72 72 69 64 65 0a 2a 2a 20 65 61 63 68 20  verride.** each 
87200 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 74 68 65  others locks the
87210 6e 20 74 69 64 20 69 73 20 61 6c 77 61 79 73 20  n tid is always 
87220 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 74 69  set to zero.  ti
87230 64 20 69 73 20 6f 6d 69 74 74 65 64 0a 2a 2a 20  d is omitted.** 
87240 69 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  if we compile wi
87250 74 68 6f 75 74 20 74 68 72 65 61 64 69 6e 67 20  thout threading 
87260 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 73 74 72 75  support..*/.stru
87270 63 74 20 6c 6f 63 6b 4b 65 79 20 7b 0a 20 20 64  ct lockKey {.  d
87280 65 76 5f 74 20 64 65 76 3b 20 20 20 20 20 20 20  ev_t dev;       
87290 2f 2a 20 44 65 76 69 63 65 20 6e 75 6d 62 65 72  /* Device number
872a0 20 2a 2f 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b   */.  ino_t ino;
872b0 20 20 20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 20         /* Inode 
872c0 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 53 51  number */.#if SQ
872d0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
872e0 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b    pthread_t tid;
872f0 20 20 20 2f 2a 20 54 68 72 65 61 64 20 49 44 20     /* Thread ID 
87300 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 72 65 61  or zero if threa
87310 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20  ds can override 
87320 65 61 63 68 20 6f 74 68 65 72 20 2a 2f 0a 23 65  each other */.#e
87330 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ndif.};../*.** A
87340 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
87350 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
87360 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74  cture is allocat
87370 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e  ed for each open
87380 0a 2a 2a 20 69 6e 6f 64 65 20 6f 6e 20 65 61 63  .** inode on eac
87390 68 20 74 68 72 65 61 64 20 77 69 74 68 20 61 20  h thread with a 
873a0 64 69 66 66 65 72 65 6e 74 20 70 72 6f 63 65 73  different proces
873b0 73 20 49 44 2e 20 20 28 54 68 72 65 61 64 73 20  s ID.  (Threads 
873c0 68 61 76 65 0a 2a 2a 20 64 69 66 66 65 72 65 6e  have.** differen
873d0 74 20 70 72 6f 63 65 73 73 20 49 44 73 20 6f 6e  t process IDs on
873e0 20 6c 69 6e 75 78 2c 20 62 75 74 20 6e 6f 74 20   linux, but not 
873f0 6f 6e 20 6d 6f 73 74 20 6f 74 68 65 72 20 75 6e  on most other un
87400 69 78 65 73 2e 29 0a 2a 2a 0a 2a 2a 20 41 20 73  ixes.).**.** A s
87410 69 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61 6e 20  ingle inode can 
87420 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 66 69  have multiple fi
87430 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20  le descriptors, 
87440 73 6f 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65  so each unixFile
87450 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f  .** structure co
87460 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
87470 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
87480 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 61  of this object a
87490 6e 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63  nd this.** objec
874a0 74 20 6b 65 65 70 73 20 61 20 63 6f 75 6e 74 20  t keeps a count 
874b0 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
874c0 20 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e 74 69   unixFile pointi
874d0 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73 74 72  ng to it..*/.str
874e0 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 7b 0a 20  uct lockInfo {. 
874f0 20 73 74 72 75 63 74 20 6c 6f 63 6b 4b 65 79 20   struct lockKey 
87500 6b 65 79 3b 20 20 2f 2a 20 54 68 65 20 6c 6f 6f  key;  /* The loo
87510 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  kup key */.  int
87520 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20   cnt;           
87530 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53    /* Number of S
87540 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c 64  HARED locks held
87550 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74 79   */.  int lockty
87560 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e  pe;        /* On
87570 65 20 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b  e of SHARED_LOCK
87580 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  , RESERVED_LOCK 
87590 65 74 63 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  etc. */.  int nR
875a0 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ef;            /
875b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e  * Number of poin
875c0 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72  ters to this str
875d0 75 63 74 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ucture */.};../*
875e0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
875f0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
87600 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65   structure serve
87610 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65  s as the key use
87620 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61  d.** to locate a
87630 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 6e   particular open
87640 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 67 69  Cnt structure gi
87650 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 20 20  ven its inode.  
87660 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 73  This.** is the s
87670 61 6d 65 20 61 73 20 74 68 65 20 6c 6f 63 6b 4b  ame as the lockK
87680 65 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74  ey except that t
87690 68 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20  he thread ID is 
876a0 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75  omitted..*/.stru
876b0 63 74 20 6f 70 65 6e 4b 65 79 20 7b 0a 20 20 64  ct openKey {.  d
876c0 65 76 5f 74 20 64 65 76 3b 20 20 20 2f 2a 20 44  ev_t dev;   /* D
876d0 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  evice number */.
876e0 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 2f    ino_t ino;   /
876f0 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a  * Inode number *
87700 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
87710 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
87720 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
87730 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  re is allocated 
87740 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a  for each open.**
87750 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74   inode.  This st
87760 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
87770 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ack of the numbe
87780 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68  r of locks on th
87790 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66  at.** inode.  If
877a0 20 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65   a close is atte
877b0 6d 70 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e  mpted against an
877c0 20 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68   inode that is h
877d0 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c  olding.** locks,
877e0 20 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65   the close is de
877f0 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c  ferred until all
87800 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20   locks clear by 
87810 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  adding the.** fi
87820 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
87830 20 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68   be closed to th
87840 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a  e pending list..
87850 2a 2f 0a 73 74 72 75 63 74 20 6f 70 65 6e 43 6e  */.struct openCn
87860 74 20 7b 0a 20 20 73 74 72 75 63 74 20 6f 70 65  t {.  struct ope
87870 6e 4b 65 79 20 6b 65 79 3b 20 20 20 2f 2a 20 54  nKey key;   /* T
87880 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f  he lookup key */
87890 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
878a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
878b0 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74  er of pointers t
878c0 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
878d0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b   */.  int nLock;
878e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
878f0 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e  umber of outstan
87900 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20  ding locks */.  
87910 69 6e 74 20 6e 50 65 6e 64 69 6e 67 3b 20 20 20  int nPending;   
87920 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
87930 6f 66 20 70 65 6e 64 69 6e 67 20 63 6c 6f 73 65  of pending close
87940 28 29 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f  () operations */
87950 0a 20 20 69 6e 74 20 2a 61 50 65 6e 64 69 6e 67  .  int *aPending
87960 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c  ;        /* Mall
87970 6f 63 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69  oced space holdi
87980 6e 67 20 66 64 27 73 20 61 77 61 69 74 69 6e 67  ng fd's awaiting
87990 20 61 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 7d 3b   a close() */.};
879a0 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 73 65 20 68  ../* .** These h
879b0 61 73 68 20 74 61 62 6c 65 73 20 6d 61 70 20 69  ash tables map i
879c0 6e 6f 64 65 73 20 61 6e 64 20 66 69 6c 65 20 64  nodes and file d
879d0 65 73 63 72 69 70 74 6f 72 73 20 28 72 65 61 6c  escriptors (real
879e0 6c 79 2c 20 6c 6f 63 6b 4b 65 79 20 61 6e 64 0a  ly, lockKey and.
879f0 2a 2a 20 6f 70 65 6e 4b 65 79 20 73 74 72 75 63  ** openKey struc
87a00 74 75 72 65 73 29 20 69 6e 74 6f 20 6c 6f 63 6b  tures) into lock
87a10 49 6e 66 6f 20 61 6e 64 20 6f 70 65 6e 43 6e 74  Info and openCnt
87a20 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 41 63   structures.  Ac
87a30 63 65 73 73 20 74 6f 20 0a 2a 2a 20 74 68 65 73  cess to .** thes
87a40 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 6d 75  e hash tables mu
87a50 73 74 20 62 65 20 70 72 6f 74 65 63 74 65 64 20  st be protected 
87a60 62 79 20 61 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73  by a mutex..*/.s
87a70 74 61 74 69 63 20 48 61 73 68 20 6c 6f 63 6b 48  tatic Hash lockH
87a80 61 73 68 20 3d 20 7b 53 51 4c 49 54 45 5f 48 41  ash = {SQLITE_HA
87a90 53 48 5f 42 49 4e 41 52 59 2c 20 30 2c 20 30 2c  SH_BINARY, 0, 0,
87aa0 20 30 2c 20 30 2c 20 30 7d 3b 0a 73 74 61 74 69   0, 0, 0};.stati
87ab0 63 20 48 61 73 68 20 6f 70 65 6e 48 61 73 68 20  c Hash openHash 
87ac0 3d 20 7b 53 51 4c 49 54 45 5f 48 41 53 48 5f 42  = {SQLITE_HASH_B
87ad0 49 4e 41 52 59 2c 20 30 2c 20 30 2c 20 30 2c 20  INARY, 0, 0, 0, 
87ae0 30 2c 20 30 7d 3b 0a 0a 23 69 66 64 65 66 20 53  0, 0};..#ifdef S
87af0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
87b00 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a  KING_STYLE./*.**
87b10 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79   The locking sty
87b20 6c 65 73 20 61 72 65 20 61 73 73 6f 63 69 61 74  les are associat
87b30 65 64 20 77 69 74 68 20 74 68 65 20 64 69 66 66  ed with the diff
87b40 65 72 65 6e 74 20 66 69 6c 65 20 6c 6f 63 6b 69  erent file locki
87b50 6e 67 0a 2a 2a 20 63 61 70 61 62 69 6c 69 74 69  ng.** capabiliti
87b60 65 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20  es supported by 
87b70 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20 73  different file s
87b80 79 73 74 65 6d 73 2e 20 20 0a 2a 2a 0a 2a 2a 20  ystems.  .**.** 
87b90 50 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20 73 74  POSIX locking st
87ba0 79 6c 65 20 66 75 6c 6c 79 20 73 75 70 70 6f 72  yle fully suppor
87bb0 74 73 20 73 68 61 72 65 64 20 61 6e 64 20 65 78  ts shared and ex
87bc0 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 6e  clusive byte-ran
87bd0 67 65 20 6c 6f 63 6b 73 20 0a 2a 2a 20 41 44 50  ge locks .** ADP
87be0 20 6c 6f 63 6b 69 6e 67 20 6f 6e 6c 79 20 73 75   locking only su
87bf0 70 70 6f 72 74 73 20 65 78 63 6c 75 73 69 76 65  pports exclusive
87c00 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b   byte-range lock
87c10 73 0a 2a 2a 20 46 4c 4f 43 4b 20 6f 6e 6c 79 20  s.** FLOCK only 
87c20 73 75 70 70 6f 72 74 73 20 61 20 73 69 6e 67 6c  supports a singl
87c30 65 20 66 69 6c 65 2d 67 6c 6f 62 61 6c 20 65 78  e file-global ex
87c40 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20  clusive lock.** 
87c50 44 4f 54 4c 4f 43 4b 20 69 73 6e 27 74 20 61 20  DOTLOCK isn't a 
87c60 74 72 75 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79  true locking sty
87c70 6c 65 2c 20 69 74 20 72 65 66 65 72 73 20 74 6f  le, it refers to
87c80 20 74 68 65 20 75 73 65 20 6f 66 20 61 20 73 70   the use of a sp
87c90 65 63 69 61 6c 0a 2a 2a 20 20 20 66 69 6c 65 20  ecial.**   file 
87ca0 6e 61 6d 65 64 20 74 68 65 20 73 61 6d 65 20 61  named the same a
87cb0 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
87cc0 69 6c 65 20 77 69 74 68 20 61 20 27 2e 6c 6f 63  ile with a '.loc
87cd0 6b 27 20 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68  k' extension, th
87ce0 69 73 0a 2a 2a 20 20 20 63 61 6e 20 62 65 20 75  is.**   can be u
87cf0 73 65 64 20 6f 6e 20 66 69 6c 65 20 73 79 73 74  sed on file syst
87d00 65 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20  ems that do not 
87d10 6f 66 66 65 72 20 61 6e 79 20 72 65 6c 69 61 62  offer any reliab
87d20 6c 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a  le file locking.
87d30 2a 2a 20 4e 4f 20 6c 6f 63 6b 69 6e 67 20 6d 65  ** NO locking me
87d40 61 6e 73 20 74 68 61 74 20 6e 6f 20 6c 6f 63 6b  ans that no lock
87d50 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 74 74 65  ing will be atte
87d60 6d 70 74 65 64 2c 20 74 68 69 73 20 69 73 20 6f  mpted, this is o
87d70 6e 6c 79 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20  nly used for.** 
87d80 20 20 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65    read-only file
87d90 20 73 79 73 74 65 6d 73 20 63 75 72 72 65 6e 74   systems current
87da0 6c 79 0a 2a 2a 20 55 4e 53 55 50 50 4f 52 54 45  ly.** UNSUPPORTE
87db0 44 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 20  D means that no 
87dc0 6c 6f 63 6b 69 6e 67 20 77 69 6c 6c 20 62 65 20  locking will be 
87dd0 61 74 74 65 6d 70 74 65 64 2c 20 74 68 69 73 20  attempted, this 
87de0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72  is only used for
87df0 0a 2a 2a 20 20 20 66 69 6c 65 20 73 79 73 74 65  .**   file syste
87e00 6d 73 20 74 68 61 74 20 61 72 65 20 6b 6e 6f 77  ms that are know
87e10 6e 20 74 6f 20 62 65 20 75 6e 73 75 70 70 6f 72  n to be unsuppor
87e20 74 65 64 0a 2a 2f 0a 74 79 70 65 64 65 66 20 65  ted.*/.typedef e
87e30 6e 75 6d 20 7b 0a 20 20 20 20 20 20 20 20 70 6f  num {.        po
87e40 73 69 78 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  sixLockingStyle 
87e50 3d 20 30 2c 20 20 20 20 20 20 20 2f 2a 20 73 74  = 0,       /* st
87e60 61 6e 64 61 72 64 20 70 6f 73 69 78 2d 61 64 76  andard posix-adv
87e70 69 73 6f 72 79 20 6c 6f 63 6b 73 20 2a 2f 0a 20  isory locks */. 
87e80 20 20 20 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e         afpLockin
87e90 67 53 74 79 6c 65 2c 20 20 20 20 20 20 20 20 20  gStyle,         
87ea0 20 20 20 20 2f 2a 20 75 73 65 20 61 66 70 20 6c      /* use afp l
87eb0 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  ocks */.        
87ec0 66 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79 6c  flockLockingStyl
87ed0 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
87ee0 75 73 65 20 66 6c 6f 63 6b 28 29 20 2a 2f 0a 20  use flock() */. 
87ef0 20 20 20 20 20 20 20 64 6f 74 6c 6f 63 6b 4c 6f         dotlockLo
87f00 63 6b 69 6e 67 53 74 79 6c 65 2c 20 20 20 20 20  ckingStyle,     
87f10 20 20 20 20 2f 2a 20 75 73 65 20 3c 66 69 6c 65      /* use <file
87f20 3e 2e 6c 6f 63 6b 20 66 69 6c 65 73 20 2a 2f 0a  >.lock files */.
87f30 20 20 20 20 20 20 20 20 6e 6f 4c 6f 63 6b 69 6e          noLockin
87f40 67 53 74 79 6c 65 2c 20 20 20 20 20 20 20 20 20  gStyle,         
87f50 20 20 20 20 20 2f 2a 20 75 73 65 66 75 6c 20 66       /* useful f
87f60 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c  or read-only fil
87f70 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20  e system */.    
87f80 20 20 20 20 75 6e 73 75 70 70 6f 72 74 65 64 4c      unsupportedL
87f90 6f 63 6b 69 6e 67 53 74 79 6c 65 20 20 20 20 20  ockingStyle     
87fa0 20 2f 2a 20 69 6e 64 69 63 61 74 65 73 20 75 6e   /* indicates un
87fb0 73 75 70 70 6f 72 74 65 64 20 66 69 6c 65 20 73  supported file s
87fc0 79 73 74 65 6d 20 2a 2f 0a 7d 20 73 71 6c 69 74  ystem */.} sqlit
87fd0 65 33 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a  e3LockingStyle;.
87fe0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
87ff0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
88000 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  STYLE */../*.** 
88010 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73  Helper functions
88020 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72   to obtain and r
88030 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c  elinquish the gl
88040 6f 62 61 6c 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73  obal mutex..*/.s
88050 74 61 74 69 63 20 76 6f 69 64 20 65 6e 74 65 72  tatic void enter
88060 4d 75 74 65 78 28 29 7b 0a 20 20 73 71 6c 69 74  Mutex(){.  sqlit
88070 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
88080 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
88090 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
880a0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b  STATIC_MASTER));
880b0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  .}.static void l
880c0 65 61 76 65 4d 75 74 65 78 28 29 7b 0a 20 20 73  eaveMutex(){.  s
880d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
880e0 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
880f0 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  _alloc(SQLITE_MU
88100 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
88110 52 29 29 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49  R));.}..#if SQLI
88120 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 2f 2a  TE_THREADSAFE./*
88130 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c  .** This variabl
88140 65 20 72 65 63 6f 72 64 73 20 77 68 65 74 68 65  e records whethe
88150 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73  r or not threads
88160 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61   can override ea
88170 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f 63  ch others.** loc
88180 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a 20  ks..**.**    0: 
88190 20 4e 6f 2e 20 20 54 68 72 65 61 64 73 20 63 61   No.  Threads ca
881a0 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65 61  nnot override ea
881b0 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e  ch others locks.
881c0 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65 73 2e 20  .**    1:  Yes. 
881d0 20 54 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65   Threads can ove
881e0 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
881f0 73 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 2d 31  s locks..**   -1
88200 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b 6e 6f 77  :  We don't know
88210 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73   yet..**.** On s
88220 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 77 65 20  ome systems, we 
88230 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d  know at compile-
88240 74 69 6d 65 20 69 66 20 74 68 72 65 61 64 73 20  time if threads 
88250 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63  can override eac
88260 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c 6f 63 6b  h.** others lock
88270 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79 73  s.  On those sys
88280 74 65 6d 73 2c 20 74 68 65 20 53 51 4c 49 54 45  tems, the SQLITE
88290 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
882a0 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20 77  _LOCK macro.** w
882b0 69 6c 6c 20 62 65 20 73 65 74 20 61 70 70 72 6f  ill be set appro
882c0 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e 20 6f 74  priately.  On ot
882d0 68 65 72 20 73 79 73 74 65 6d 73 2c 20 77 65 20  her systems, we 
882e0 68 61 76 65 20 74 6f 20 63 68 65 63 6b 20 61 74  have to check at
882f0 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 20 4f 6e  .** runtime.  On
88300 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 73 79   these latter sy
88310 73 74 65 6d 73 2c 20 53 51 4c 54 49 45 5f 54 48  stems, SQLTIE_TH
88320 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
88330 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e  CK is.** undefin
88340 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76  ed..**.** This v
88350 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c 79  ariable normally
88360 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20   has file scope 
88370 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75 72 69 6e  only.  But durin
88380 67 20 74 65 73 74 69 6e 67 2c 20 77 65 20 6d 61  g testing, we ma
88390 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c 6f 62 61  ke.** it a globa
883a0 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20 74 65  l so that the te
883b0 73 74 20 63 6f 64 65 20 63 61 6e 20 63 68 61 6e  st code can chan
883c0 67 65 20 69 74 73 20 76 61 6c 75 65 20 69 6e 20  ge its value in 
883d0 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 0a  order to verify.
883e0 2a 2a 20 74 68 61 74 20 74 68 65 20 72 69 67 68  ** that the righ
883f0 74 20 73 74 75 66 66 20 68 61 70 70 65 6e 73 20  t stuff happens 
88400 69 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e 0a  in either case..
88410 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
88420 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44  E_THREAD_OVERRID
88430 45 5f 4c 4f 43 4b 0a 23 20 64 65 66 69 6e 65 20  E_LOCK.# define 
88440 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56  SQLITE_THREAD_OV
88450 45 52 52 49 44 45 5f 4c 4f 43 4b 20 2d 31 0a 23  ERRIDE_LOCK -1.#
88460 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
88470 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 74 68 72  ITE_TEST.int thr
88480 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
88490 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51  OthersLocks = SQ
884a0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52  LITE_THREAD_OVER
884b0 52 49 44 45 5f 4c 4f 43 4b 3b 0a 23 65 6c 73 65  RIDE_LOCK;.#else
884c0 0a 73 74 61 74 69 63 20 69 6e 74 20 74 68 72 65  .static int thre
884d0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
884e0 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c  thersLocks = SQL
884f0 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52  ITE_THREAD_OVERR
88500 49 44 45 5f 4c 4f 43 4b 3b 0a 23 65 6e 64 69 66  IDE_LOCK;.#endif
88510 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ../*.** This str
88520 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66  ucture holds inf
88530 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20  ormation passed 
88540 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 20  into individual 
88550 74 65 73 74 0a 2a 2a 20 74 68 72 65 61 64 73 20  test.** threads 
88560 62 79 20 74 68 65 20 74 65 73 74 54 68 72 65 61  by the testThrea
88570 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72  dLockingBehavior
88580 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73  () routine..*/.s
88590 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74  truct threadTest
885a0 44 61 74 61 20 7b 0a 20 20 69 6e 74 20 66 64 3b  Data {.  int fd;
885b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
885c0 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65 20 6c 6f  /* File to be lo
885d0 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74  cked */.  struct
885e0 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20   flock lock;    
885f0 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20   /* The locking 
88600 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69  operation */.  i
88610 6e 74 20 72 65 73 75 6c 74 3b 20 20 20 20 20 20  nt result;      
88620 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
88630 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6f  of the locking o
88640 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a  peration */.};..
88650 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f  #ifdef SQLITE_LO
88660 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20 50  CK_TRACE./*.** P
88670 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d 61  rint out informa
88680 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 6c  tion about all l
88690 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ocking operation
886a0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
886b0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
886c0 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e  r troubleshootin
886d0 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 69  g locks on multi
886e0 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61 74  threaded.** plat
886f0 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20 62  forms.  Enable b
88700 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68  y compiling with
88710 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c 4f   the -DSQLITE_LO
88720 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d 6d  CK_TRACE.** comm
88730 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e 20  and-line option 
88740 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e  on the compiler.
88750 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 20 6e    This code is n
88760 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e 65  ormally.** turne
88770 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63  d off..*/.static
88780 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28 69   int lockTrace(i
88790 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20 73  nt fd, int op, s
887a0 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29 7b  truct flock *p){
887b0 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d 65  .  char *zOpName
887c0 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74 20  , *zType;.  int 
887d0 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45 72  s;.  int savedEr
887e0 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 46  rno;.  if( op==F
887f0 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f  _GETLK ){.    zO
88800 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22 3b  pName = "GETLK";
88810 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d  .  }else if( op=
88820 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20 20  =F_SETLK ){.    
88830 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c 4b  zOpName = "SETLK
88840 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ";.  }else{.    
88850 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70  s = fcntl(fd, op
88860 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , p);.    sqlite
88870 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63  3DebugPrintf("fc
88880 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20 25  ntl unknown %d %
88890 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70 2c  d %d\n", fd, op,
888a0 20 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   s);.    return 
888b0 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  s;.  }.  if( p->
888c0 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20  l_type==F_RDLCK 
888d0 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22  ){.    zType = "
888e0 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20  RDLCK";.  }else 
888f0 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46  if( p->l_type==F
88900 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54  _WRLCK ){.    zT
88910 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20  ype = "WRLCK";. 
88920 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f   }else if( p->l_
88930 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b  type==F_UNLCK ){
88940 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e  .    zType = "UN
88950 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  LCK";.  }else{. 
88960 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
88970 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d    }.  assert( p-
88980 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b 5f  >l_whence==SEEK_
88990 53 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63 6e  SET );.  s = fcn
889a0 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20  tl(fd, op, p);. 
889b0 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65 72   savedErrno = er
889c0 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  rno;.  sqlite3De
889d0 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c  bugPrintf("fcntl
889e0 20 25 64 20 25 64 20 25 73 20 25 73 20 25 64 20   %d %d %s %s %d 
889f0 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20  %d %d %d\n",.   
88a00 20 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c 20    threadid, fd, 
88a10 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c 20  zOpName, zType, 
88a20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74 2c  (int)p->l_start,
88a30 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a   (int)p->l_len,.
88a40 20 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f 70       (int)p->l_p
88a50 69 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73 3d  id, s);.  if( s=
88a60 3d 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f 53  =(-1) && op==F_S
88a70 45 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74 79  ETLK && (p->l_ty
88a80 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20 70  pe==F_RDLCK || p
88a90 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43  ->l_type==F_WRLC
88aa0 4b 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  K) ){.    struct
88ab0 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20 6c   flock l2;.    l
88ac0 32 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e 74  2 = *p;.    fcnt
88ad0 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26  l(fd, F_GETLK, &
88ae0 6c 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32 2e  l2);.    if( l2.
88af0 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20  l_type==F_RDLCK 
88b00 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  ){.      zType =
88b10 20 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d 65   "RDLCK";.    }e
88b20 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70  lse if( l2.l_typ
88b30 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20  e==F_WRLCK ){.  
88b40 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c      zType = "WRL
88b50 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  CK";.    }else i
88b60 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f  f( l2.l_type==F_
88b70 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a  UNLCK ){.      z
88b80 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a  Type = "UNLCK";.
88b90 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
88ba0 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20   assert( 0 );.  
88bb0 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
88bc0 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74  ebugPrintf("fcnt
88bd0 6c 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f 6e  l-failure-reason
88be0 3a 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e 22  : %s %d %d %d\n"
88bf0 2c 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c 20  ,.       zType, 
88c00 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74 2c  (int)l2.l_start,
88c10 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c 20   (int)l2.l_len, 
88c20 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b 0a  (int)l2.l_pid);.
88c30 20 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73 61    }.  errno = sa
88c40 76 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74 75  vedErrno;.  retu
88c50 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65 20  rn s;.}.#define 
88c60 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65 0a  fcntl lockTrace.
88c70 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
88c80 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a  _LOCK_TRACE */..
88c90 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 54 68  /*.** The testTh
88ca0 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76  readLockingBehav
88cb0 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 6c 61  ior() routine la
88cc0 75 6e 63 68 65 73 20 74 77 6f 20 73 65 70 61 72  unches two separ
88cd0 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 73 20 6f  ate.** threads o
88ce0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  n this routine. 
88cf0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74   This routine at
88d00 74 65 6d 70 74 73 20 74 6f 20 6c 6f 63 6b 20 61  tempts to lock a
88d10 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70   file.** descrip
88d20 74 6f 72 20 74 68 65 6e 20 72 65 74 75 72 6e 73  tor then returns
88d30 2e 20 20 54 68 65 20 73 75 63 63 65 73 73 20 6f  .  The success o
88d40 72 20 66 61 69 6c 75 72 65 20 6f 66 20 74 68 61  r failure of tha
88d50 74 20 61 74 74 65 6d 70 74 0a 2a 2a 20 61 6c 6c  t attempt.** all
88d60 6f 77 73 20 74 68 65 20 74 65 73 74 54 68 72 65  ows the testThre
88d70 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f  adLockingBehavio
88d80 72 28 29 20 70 72 6f 63 65 64 75 72 65 20 74 6f  r() procedure to
88d90 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68   determine.** wh
88da0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72  ether or not thr
88db0 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64  eads can overrid
88dc0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  e each others lo
88dd0 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  cks..*/.static v
88de0 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69  oid *threadLocki
88df0 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72  ngTest(void *pAr
88e00 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72  g){.  struct thr
88e10 65 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61  eadTestData *pDa
88e20 74 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72  ta = (struct thr
88e30 65 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72  eadTestData*)pAr
88e40 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75  g;.  pData->resu
88e50 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61  lt = fcntl(pData
88e60 2d 3e 66 64 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  ->fd, F_SETLK, &
88e70 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20  pData->lock);.  
88e80 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 0a  return pArg;.}..
88e90 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65  /*.** This proce
88ea0 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f  dure attempts to
88eb0 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   determine wheth
88ec0 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64  er or not thread
88ed0 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64  s.** can overrid
88ee0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  e each others lo
88ef0 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 68  cks then sets th
88f00 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65  e .** threadsOve
88f10 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
88f20 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70  ocks variable ap
88f30 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a  propriately..*/.
88f40 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74  static void test
88f50 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
88f60 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69  avior(int fd_ori
88f70 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20  g){.  int fd;.  
88f80 73 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73  struct threadTes
88f90 74 44 61 74 61 20 64 5b 32 5d 3b 0a 20 20 70 74  tData d[2];.  pt
88fa0 68 72 65 61 64 5f 74 20 74 5b 32 5d 3b 0a 0a 20  hread_t t[2];.. 
88fb0 20 66 64 20 3d 20 64 75 70 28 66 64 5f 6f 72 69   fd = dup(fd_ori
88fc0 67 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29  g);.  if( fd<0 )
88fd0 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65   return;.  memse
88fe0 74 28 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64  t(d, 0, sizeof(d
88ff0 29 29 3b 0a 20 20 64 5b 30 5d 2e 66 64 20 3d 20  ));.  d[0].fd = 
89000 66 64 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e  fd;.  d[0].lock.
89010 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b  l_type = F_RDLCK
89020 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c 5f  ;.  d[0].lock.l_
89030 6c 65 6e 20 3d 20 31 3b 0a 20 20 64 5b 30 5d 2e  len = 1;.  d[0].
89040 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 30  lock.l_start = 0
89050 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c 5f  ;.  d[0].lock.l_
89060 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45  whence = SEEK_SE
89070 54 3b 0a 20 20 64 5b 31 5d 20 3d 20 64 5b 30 5d  T;.  d[1] = d[0]
89080 3b 0a 20 20 64 5b 31 5d 2e 6c 6f 63 6b 2e 6c 5f  ;.  d[1].lock.l_
89090 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a  type = F_WRLCK;.
890a0 20 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65    pthread_create
890b0 28 26 74 5b 30 5d 2c 20 30 2c 20 74 68 72 65 61  (&t[0], 0, threa
890c0 64 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20 26 64  dLockingTest, &d
890d0 5b 30 5d 29 3b 0a 20 20 70 74 68 72 65 61 64 5f  [0]);.  pthread_
890e0 63 72 65 61 74 65 28 26 74 5b 31 5d 2c 20 30 2c  create(&t[1], 0,
890f0 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65   threadLockingTe
89100 73 74 2c 20 26 64 5b 31 5d 29 3b 0a 20 20 70 74  st, &d[1]);.  pt
89110 68 72 65 61 64 5f 6a 6f 69 6e 28 74 5b 30 5d 2c  hread_join(t[0],
89120 20 30 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6a   0);.  pthread_j
89130 6f 69 6e 28 74 5b 31 5d 2c 20 30 29 3b 0a 20 20  oin(t[1], 0);.  
89140 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 74 68 72  close(fd);.  thr
89150 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
89160 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 20 64  OthersLocks =  d
89170 5b 30 5d 2e 72 65 73 75 6c 74 3d 3d 30 20 26 26  [0].result==0 &&
89180 20 64 5b 31 5d 2e 72 65 73 75 6c 74 3d 3d 30 3b   d[1].result==0;
89190 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
891a0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a  ITE_THREADSAFE *
891b0 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  /../*.** Release
891c0 20 61 20 6c 6f 63 6b 49 6e 66 6f 20 73 74 72 75   a lockInfo stru
891d0 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79  cture previously
891e0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69   allocated by fi
891f0 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f  ndLockInfo()..*/
89200 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c  .static void rel
89210 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 73 74 72  easeLockInfo(str
89220 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c  uct lockInfo *pL
89230 6f 63 6b 29 7b 0a 20 20 69 66 20 28 70 4c 6f 63  ock){.  if (pLoc
89240 6b 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 72  k == NULL).    r
89250 65 74 75 72 6e 3b 0a 20 20 70 4c 6f 63 6b 2d 3e  eturn;.  pLock->
89260 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 4c  nRef--;.  if( pL
89270 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ock->nRef==0 ){.
89280 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
89290 6e 73 65 72 74 28 26 6c 6f 63 6b 48 61 73 68 2c  nsert(&lockHash,
892a0 20 26 70 4c 6f 63 6b 2d 3e 6b 65 79 2c 20 73 69   &pLock->key, si
892b0 7a 65 6f 66 28 70 4c 6f 63 6b 2d 3e 6b 65 79 29  zeof(pLock->key)
892c0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
892d0 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20  3_free(pLock);. 
892e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65   }.}../*.** Rele
892f0 61 73 65 20 61 20 6f 70 65 6e 43 6e 74 20 73 74  ase a openCnt st
89300 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73  ructure previous
89310 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ly allocated by 
89320 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a  findLockInfo()..
89330 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
89340 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74  eleaseOpenCnt(st
89350 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a 70 4f  ruct openCnt *pO
89360 70 65 6e 29 7b 0a 20 20 69 66 20 28 70 4f 70 65  pen){.  if (pOpe
89370 6e 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 72  n == NULL).    r
89380 65 74 75 72 6e 3b 0a 20 20 70 4f 70 65 6e 2d 3e  eturn;.  pOpen->
89390 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 4f  nRef--;.  if( pO
893a0 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  pen->nRef==0 ){.
893b0 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
893c0 6e 73 65 72 74 28 26 6f 70 65 6e 48 61 73 68 2c  nsert(&openHash,
893d0 20 26 70 4f 70 65 6e 2d 3e 6b 65 79 2c 20 73 69   &pOpen->key, si
893e0 7a 65 6f 66 28 70 4f 70 65 6e 2d 3e 6b 65 79 29  zeof(pOpen->key)
893f0 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 70  , 0);.    free(p
89400 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 29 3b  Open->aPending);
89410 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
89420 65 28 70 4f 70 65 6e 29 3b 0a 20 20 7d 0a 7d 0a  e(pOpen);.  }.}.
89430 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
89440 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
89450 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 65 73 74 73 20  YLE./*.** Tests 
89460 61 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63  a byte-range loc
89470 6b 69 6e 67 20 71 75 65 72 79 20 74 6f 20 73 65  king query to se
89480 65 20 69 66 20 62 79 74 65 20 72 61 6e 67 65 20  e if byte range 
89490 6c 6f 63 6b 73 20 61 72 65 20 0a 2a 2a 20 73 75  locks are .** su
894a0 70 70 6f 72 74 65 64 2c 20 69 66 20 6e 6f 74 20  pported, if not 
894b0 77 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20  we fall back to 
894c0 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74  dotlockLockingSt
894d0 79 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  yle..*/.static s
894e0 71 6c 69 74 65 33 4c 6f 63 6b 69 6e 67 53 74 79  qlite3LockingSty
894f0 6c 65 20 73 71 6c 69 74 65 33 54 65 73 74 4c 6f  le sqlite3TestLo
89500 63 6b 69 6e 67 53 74 79 6c 65 28 0a 20 20 63 6f  ckingStyle(.  co
89510 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50 61  nst char *filePa
89520 74 68 2c 20 0a 20 20 69 6e 74 20 66 64 0a 29 7b  th, .  int fd.){
89530 0a 20 20 2f 2a 20 74 65 73 74 20 62 79 74 65 2d  .  /* test byte-
89540 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73 69 6e 67  range lock using
89550 20 66 63 6e 74 6c 20 2a 2f 0a 20 20 73 74 72 75   fcntl */.  stru
89560 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66  ct flock lockInf
89570 6f 3b 0a 20 20 0a 20 20 6c 6f 63 6b 49 6e 66 6f  o;.  .  lockInfo
89580 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f  .l_len = 1;.  lo
89590 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d  ckInfo.l_start =
895a0 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c   0;.  lockInfo.l
895b0 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53  _whence = SEEK_S
895c0 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c  ET;.  lockInfo.l
895d0 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b  _type = F_RDLCK;
895e0 0a 20 20 0a 20 20 69 66 28 20 66 63 6e 74 6c 28  .  .  if( fcntl(
895f0 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f  fd, F_GETLK, &lo
89600 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a  ckInfo)!=-1 ) {.
89610 20 20 20 20 72 65 74 75 72 6e 20 70 6f 73 69 78      return posix
89620 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20  LockingStyle;.  
89630 7d 20 0a 20 20 0a 20 20 2f 2a 20 74 65 73 74 69  } .  .  /* testi
89640 6e 67 20 66 6f 72 20 66 6c 6f 63 6b 20 63 61 6e  ng for flock can
89650 20 67 69 76 65 20 66 61 6c 73 65 20 70 6f 73 69   give false posi
89660 74 69 76 65 73 2e 20 20 53 6f 20 69 66 20 69 66  tives.  So if if
89670 20 74 68 65 20 61 62 6f 76 65 20 74 65 73 74 0a   the above test.
89680 20 20 2a 2a 20 66 61 69 6c 73 2c 20 74 68 65 6e    ** fails, then
89690 20 77 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f   we fall back to
896a0 20 75 73 69 6e 67 20 64 6f 74 2d 6c 6f 63 6b 20   using dot-lock 
896b0 73 74 79 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 20  style locking.. 
896c0 20 2a 2f 20 20 0a 20 20 72 65 74 75 72 6e 20 64   */  .  return d
896d0 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79  otlockLockingSty
896e0 6c 65 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 45 78  le;.}../* .** Ex
896f0 61 6d 69 6e 65 73 20 74 68 65 20 66 5f 66 73 74  amines the f_fst
89700 79 70 65 6e 61 6d 65 20 65 6e 74 72 79 20 69 6e  ypename entry in
89710 20 74 68 65 20 73 74 61 74 66 73 20 73 74 72 75   the statfs stru
89720 63 74 75 72 65 20 61 73 20 72 65 74 75 72 6e 65  cture as returne
89730 64 20 62 79 20 0a 2a 2a 20 73 74 61 74 28 29 20  d by .** stat() 
89740 66 6f 72 20 74 68 65 20 66 69 6c 65 20 73 79 73  for the file sys
89750 74 65 6d 20 68 6f 73 74 69 6e 67 20 74 68 65 20  tem hosting the 
89760 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61  database file, a
89770 73 73 69 67 6e 73 20 74 68 65 20 0a 2a 2a 20 61  ssigns the .** a
89780 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69  ppropriate locki
89790 6e 67 20 73 74 79 6c 65 20 62 61 73 65 64 20 6f  ng style based o
897a0 6e 20 69 74 27 73 20 76 61 6c 75 65 2e 20 20 54  n it's value.  T
897b0 68 65 73 65 20 76 61 6c 75 65 73 20 61 6e 64 20  hese values and 
897c0 0a 2a 2a 20 61 73 73 69 67 6e 6d 65 6e 74 73 20  .** assignments 
897d0 61 72 65 20 62 61 73 65 64 20 6f 6e 20 44 61 72  are based on Dar
897e0 77 69 6e 2f 4f 53 58 20 62 65 68 61 76 69 6f 72  win/OSX behavior
897f0 20 61 6e 64 20 68 61 76 65 20 6e 6f 74 20 62 65   and have not be
89800 65 6e 20 74 65 73 74 65 64 20 6f 6e 20 0a 2a 2a  en tested on .**
89810 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2e 0a   other systems..
89820 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  */.static sqlite
89830 33 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 73 71  3LockingStyle sq
89840 6c 69 74 65 33 44 65 74 65 63 74 4c 6f 63 6b 69  lite3DetectLocki
89850 6e 67 53 74 79 6c 65 28 0a 20 20 63 6f 6e 73 74  ngStyle(.  const
89860 20 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 2c   char *filePath,
89870 20 0a 20 20 69 6e 74 20 66 64 0a 29 7b 0a 0a 23   .  int fd.){..#
89880 69 66 64 65 66 20 53 51 4c 49 54 45 5f 46 49 58  ifdef SQLITE_FIX
89890 45 44 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  ED_LOCKING_STYLE
898a0 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74  .  return (sqlit
898b0 65 33 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 29 53  e3LockingStyle)S
898c0 51 4c 49 54 45 5f 46 49 58 45 44 5f 4c 4f 43 4b  QLITE_FIXED_LOCK
898d0 49 4e 47 5f 53 54 59 4c 45 3b 0a 23 65 6c 73 65  ING_STYLE;.#else
898e0 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 66 73  .  struct statfs
898f0 20 66 73 49 6e 66 6f 3b 0a 0a 20 20 69 66 20 28   fsInfo;..  if (
89900 73 74 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c  statfs(filePath,
89910 20 26 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31 29   &fsInfo) == -1)
89920 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
89930 74 65 33 54 65 73 74 4c 6f 63 6b 69 6e 67 53 74  te3TestLockingSt
89940 79 6c 65 28 66 69 6c 65 50 61 74 68 2c 20 66 64  yle(filePath, fd
89950 29 3b 0a 20 20 0a 20 20 69 66 20 28 66 73 49 6e  );.  .  if (fsIn
89960 66 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54  fo.f_flags & MNT
89970 5f 52 44 4f 4e 4c 59 29 0a 20 20 20 20 72 65 74  _RDONLY).    ret
89980 75 72 6e 20 6e 6f 4c 6f 63 6b 69 6e 67 53 74 79  urn noLockingSty
89990 6c 65 3b 0a 20 20 0a 20 20 69 66 28 20 28 21 73  le;.  .  if( (!s
899a0 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66  trcmp(fsInfo.f_f
899b0 73 74 79 70 65 6e 61 6d 65 2c 20 22 68 66 73 22  stypename, "hfs"
899c0 29 29 20 7c 7c 0a 20 20 20 20 28 21 73 74 72 63  )) ||.    (!strc
899d0 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79  mp(fsInfo.f_fsty
899e0 70 65 6e 61 6d 65 2c 20 22 75 66 73 22 29 29 20  pename, "ufs")) 
899f0 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
89a00 20 20 72 65 74 75 72 6e 20 70 6f 73 69 78 4c 6f    return posixLo
89a10 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 0a 20  ckingStyle;.  . 
89a20 20 69 66 28 21 73 74 72 63 6d 70 28 66 73 49 6e   if(!strcmp(fsIn
89a30 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c  fo.f_fstypename,
89a40 20 22 61 66 70 66 73 22 29 29 0a 20 20 20 20 72   "afpfs")).    r
89a50 65 74 75 72 6e 20 61 66 70 4c 6f 63 6b 69 6e 67  eturn afpLocking
89a60 53 74 79 6c 65 3b 0a 20 20 0a 20 20 69 66 28 21  Style;.  .  if(!
89a70 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f  strcmp(fsInfo.f_
89a80 66 73 74 79 70 65 6e 61 6d 65 2c 20 22 6e 66 73  fstypename, "nfs
89a90 22 29 29 20 0a 20 20 20 20 72 65 74 75 72 6e 20  ")) .    return 
89aa0 73 71 6c 69 74 65 33 54 65 73 74 4c 6f 63 6b 69  sqlite3TestLocki
89ab0 6e 67 53 74 79 6c 65 28 66 69 6c 65 50 61 74 68  ngStyle(filePath
89ac0 2c 20 66 64 29 3b 0a 20 20 0a 20 20 69 66 28 21  , fd);.  .  if(!
89ad0 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f  strcmp(fsInfo.f_
89ae0 66 73 74 79 70 65 6e 61 6d 65 2c 20 22 73 6d 62  fstypename, "smb
89af0 66 73 22 29 29 0a 20 20 20 20 72 65 74 75 72 6e  fs")).    return
89b00 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79   flockLockingSty
89b10 6c 65 3b 0a 20 20 0a 20 20 69 66 28 21 73 74 72  le;.  .  if(!str
89b20 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74  cmp(fsInfo.f_fst
89b30 79 70 65 6e 61 6d 65 2c 20 22 6d 73 64 6f 73 22  ypename, "msdos"
89b40 29 29 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f  )).    return do
89b50 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79 6c  tlockLockingStyl
89b60 65 3b 0a 20 20 0a 20 20 69 66 28 21 73 74 72 63  e;.  .  if(!strc
89b70 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79  mp(fsInfo.f_fsty
89b80 70 65 6e 61 6d 65 2c 20 22 77 65 62 64 61 76 22  pename, "webdav"
89b90 29 29 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e  )).    return un
89ba0 73 75 70 70 6f 72 74 65 64 4c 6f 63 6b 69 6e 67  supportedLocking
89bb0 53 74 79 6c 65 3b 0a 20 20 0a 20 20 72 65 74 75  Style;.  .  retu
89bc0 72 6e 20 73 71 6c 69 74 65 33 54 65 73 74 4c 6f  rn sqlite3TestLo
89bd0 63 6b 69 6e 67 53 74 79 6c 65 28 66 69 6c 65 50  ckingStyle(fileP
89be0 61 74 68 2c 20 66 64 29 3b 20 20 0a 23 65 6e 64  ath, fd);  .#end
89bf0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 46 49 58  if /* SQLITE_FIX
89c00 45 44 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  ED_LOCKING_STYLE
89c10 20 2a 2f 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a   */.}..#endif /*
89c20 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
89c30 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
89c40 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 66  ./*.** Given a f
89c50 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2c 20  ile descriptor, 
89c60 6c 6f 63 61 74 65 20 6c 6f 63 6b 49 6e 66 6f 20  locate lockInfo 
89c70 61 6e 64 20 6f 70 65 6e 43 6e 74 20 73 74 72 75  and openCnt stru
89c80 63 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64  ctures that.** d
89c90 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66 69  escribes that fi
89ca0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  le descriptor.  
89cb0 43 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20  Create new ones 
89cc0 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54  if necessary.  T
89cd0 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c  he.** return val
89ce0 75 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69  ues might be uni
89cf0 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e  nitialized if an
89d00 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
89d10 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
89d20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
89d30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
89d40 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20  findLockInfo(.  
89d50 69 6e 74 20 66 64 2c 20 20 20 20 20 20 20 20 20  int fd,         
89d60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
89d70 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  The file descrip
89d80 74 6f 72 20 75 73 65 64 20 69 6e 20 74 68 65 20  tor used in the 
89d90 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  key */.  struct 
89da0 6c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70 4c 6f 63  lockInfo **ppLoc
89db0 6b 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20  k,    /* Return 
89dc0 74 68 65 20 6c 6f 63 6b 49 6e 66 6f 20 73 74 72  the lockInfo str
89dd0 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20  ucture here */. 
89de0 20 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20   struct openCnt 
89df0 2a 2a 70 70 4f 70 65 6e 20 20 20 20 20 20 2f 2a  **ppOpen      /*
89e00 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 65 6e   Return the open
89e10 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65  Cnt structure he
89e20 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
89e30 63 3b 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b  c;.  struct lock
89e40 4b 65 79 20 6b 65 79 31 3b 0a 20 20 73 74 72 75  Key key1;.  stru
89e50 63 74 20 6f 70 65 6e 4b 65 79 20 6b 65 79 32 3b  ct openKey key2;
89e60 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73  .  struct stat s
89e70 74 61 74 62 75 66 3b 0a 20 20 73 74 72 75 63 74  tatbuf;.  struct
89e80 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b   lockInfo *pLock
89e90 3b 0a 20 20 73 74 72 75 63 74 20 6f 70 65 6e 43  ;.  struct openC
89ea0 6e 74 20 2a 70 4f 70 65 6e 3b 0a 20 20 72 63 20  nt *pOpen;.  rc 
89eb0 3d 20 66 73 74 61 74 28 66 64 2c 20 26 73 74 61  = fstat(fd, &sta
89ec0 74 62 75 66 29 3b 0a 20 20 69 66 28 20 72 63 21  tbuf);.  if( rc!
89ed0 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a  =0 ) return 1;..
89ee0 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 31 2c 20    memset(&key1, 
89ef0 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79 31 29 29  0, sizeof(key1))
89f00 3b 0a 20 20 6b 65 79 31 2e 64 65 76 20 3d 20 73  ;.  key1.dev = s
89f10 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 20  tatbuf.st_dev;. 
89f20 20 6b 65 79 31 2e 69 6e 6f 20 3d 20 73 74 61 74   key1.ino = stat
89f30 62 75 66 2e 73 74 5f 69 6e 6f 3b 0a 23 69 66 20  buf.st_ino;.#if 
89f40 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
89f50 45 0a 20 20 69 66 28 20 74 68 72 65 61 64 73 4f  E.  if( threadsO
89f60 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
89f70 73 4c 6f 63 6b 73 3c 30 20 29 7b 0a 20 20 20 20  sLocks<0 ){.    
89f80 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e  testThreadLockin
89f90 67 42 65 68 61 76 69 6f 72 28 66 64 29 3b 0a 20  gBehavior(fd);. 
89fa0 20 7d 0a 20 20 6b 65 79 31 2e 74 69 64 20 3d 20   }.  key1.tid = 
89fb0 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
89fc0 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3f  achOthersLocks ?
89fd0 20 30 20 3a 20 70 74 68 72 65 61 64 5f 73 65 6c   0 : pthread_sel
89fe0 66 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 65  f();.#endif.  me
89ff0 6d 73 65 74 28 26 6b 65 79 32 2c 20 30 2c 20 73  mset(&key2, 0, s
8a000 69 7a 65 6f 66 28 6b 65 79 32 29 29 3b 0a 20 20  izeof(key2));.  
8a010 6b 65 79 32 2e 64 65 76 20 3d 20 73 74 61 74 62  key2.dev = statb
8a020 75 66 2e 73 74 5f 64 65 76 3b 0a 20 20 6b 65 79  uf.st_dev;.  key
8a030 32 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e  2.ino = statbuf.
8a040 73 74 5f 69 6e 6f 3b 0a 20 20 70 4c 6f 63 6b 20  st_ino;.  pLock 
8a050 3d 20 28 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e  = (struct lockIn
8a060 66 6f 2a 29 73 71 6c 69 74 65 33 48 61 73 68 46  fo*)sqlite3HashF
8a070 69 6e 64 28 26 6c 6f 63 6b 48 61 73 68 2c 20 26  ind(&lockHash, &
8a080 6b 65 79 31 2c 20 73 69 7a 65 6f 66 28 6b 65 79  key1, sizeof(key
8a090 31 29 29 3b 0a 20 20 69 66 28 20 70 4c 6f 63 6b  1));.  if( pLock
8a0a0 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63  ==0 ){.    struc
8a0b0 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4f 6c 64  t lockInfo *pOld
8a0c0 3b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 73 71  ;.    pLock = sq
8a0d0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69  lite3_malloc( si
8a0e0 7a 65 6f 66 28 2a 70 4c 6f 63 6b 29 20 29 3b 0a  zeof(*pLock) );.
8a0f0 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30      if( pLock==0
8a100 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 31   ){.      rc = 1
8a110 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  ;.      goto exi
8a120 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a  t_findlockinfo;.
8a130 20 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d      }.    pLock-
8a140 3e 6b 65 79 20 3d 20 6b 65 79 31 3b 0a 20 20 20  >key = key1;.   
8a150 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d 20 31   pLock->nRef = 1
8a160 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74  ;.    pLock->cnt
8a170 20 3d 20 30 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d   = 0;.    pLock-
8a180 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 30 3b 0a 20  >locktype = 0;. 
8a190 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65     pOld = sqlite
8a1a0 33 48 61 73 68 49 6e 73 65 72 74 28 26 6c 6f 63  3HashInsert(&loc
8a1b0 6b 48 61 73 68 2c 20 26 70 4c 6f 63 6b 2d 3e 6b  kHash, &pLock->k
8a1c0 65 79 2c 20 73 69 7a 65 6f 66 28 6b 65 79 31 29  ey, sizeof(key1)
8a1d0 2c 20 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 69 66  , pLock);.    if
8a1e0 28 20 70 4f 6c 64 21 3d 30 20 29 7b 0a 20 20 20  ( pOld!=0 ){.   
8a1f0 20 20 20 61 73 73 65 72 74 28 20 70 4f 6c 64 3d     assert( pOld=
8a200 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20  =pLock );.      
8a210 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f  sqlite3_free(pLo
8a220 63 6b 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ck);.      rc = 
8a230 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  1;.      goto ex
8a240 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b  it_findlockinfo;
8a250 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
8a260 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b      pLock->nRef+
8a270 2b 3b 0a 20 20 7d 0a 20 20 2a 70 70 4c 6f 63 6b  +;.  }.  *ppLock
8a280 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 69 66 28 20   = pLock;.  if( 
8a290 70 70 4f 70 65 6e 21 3d 30 20 29 7b 0a 20 20 20  ppOpen!=0 ){.   
8a2a0 20 70 4f 70 65 6e 20 3d 20 28 73 74 72 75 63 74   pOpen = (struct
8a2b0 20 6f 70 65 6e 43 6e 74 2a 29 73 71 6c 69 74 65   openCnt*)sqlite
8a2c0 33 48 61 73 68 46 69 6e 64 28 26 6f 70 65 6e 48  3HashFind(&openH
8a2d0 61 73 68 2c 20 26 6b 65 79 32 2c 20 73 69 7a 65  ash, &key2, size
8a2e0 6f 66 28 6b 65 79 32 29 29 3b 0a 20 20 20 20 69  of(key2));.    i
8a2f0 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20  f( pOpen==0 ){. 
8a300 20 20 20 20 20 73 74 72 75 63 74 20 6f 70 65 6e       struct open
8a310 43 6e 74 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 20  Cnt *pOld;.     
8a320 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33   pOpen = sqlite3
8a330 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  _malloc( sizeof(
8a340 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20  *pOpen) );.     
8a350 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b   if( pOpen==0 ){
8a360 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
8a370 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b  LockInfo(pLock);
8a380 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b  .        rc = 1;
8a390 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78  .        goto ex
8a3a0 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b  it_findlockinfo;
8a3b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
8a3c0 4f 70 65 6e 2d 3e 6b 65 79 20 3d 20 6b 65 79 32  Open->key = key2
8a3d0 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e  ;.      pOpen->n
8a3e0 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  Ref = 1;.      p
8a3f0 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b  Open->nLock = 0;
8a400 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50  .      pOpen->nP
8a410 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20  ending = 0;.    
8a420 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e    pOpen->aPendin
8a430 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 6c  g = 0;.      pOl
8a440 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  d = sqlite3HashI
8a450 6e 73 65 72 74 28 26 6f 70 65 6e 48 61 73 68 2c  nsert(&openHash,
8a460 20 26 70 4f 70 65 6e 2d 3e 6b 65 79 2c 20 73 69   &pOpen->key, si
8a470 7a 65 6f 66 28 6b 65 79 32 29 2c 20 70 4f 70 65  zeof(key2), pOpe
8a480 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f  n);.      if( pO
8a490 6c 64 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ld!=0 ){.       
8a4a0 20 61 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d 70   assert( pOld==p
8a4b0 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20  Open );.        
8a4c0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70  sqlite3_free(pOp
8a4d0 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c  en);.        rel
8a4e0 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f  easeLockInfo(pLo
8a4f0 63 6b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ck);.        rc 
8a500 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  = 1;.        got
8a510 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69  o exit_findlocki
8a520 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nfo;.      }.   
8a530 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4f   }else{.      pO
8a540 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20  pen->nRef++;.   
8a550 20 7d 0a 20 20 20 20 2a 70 70 4f 70 65 6e 20 3d   }.    *ppOpen =
8a560 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a 0a 65 78 69   pOpen;.  }..exi
8a570 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3a 0a  t_findlockinfo:.
8a580 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
8a590 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
8a5a0 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72  BUG./*.** Helper
8a5b0 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 72   function for pr
8a5c0 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 61 63 65  inting out trace
8a5d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
8a5e0 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 62  m debugging.** b
8a5f0 69 6e 61 72 69 65 73 2e 20 54 68 69 73 20 72 65  inaries. This re
8a600 74 75 72 6e 73 20 74 68 65 20 73 74 72 69 6e 67  turns the string
8a610 20 72 65 70 72 65 73 65 74 61 74 69 6f 6e 20 6f   represetation o
8a620 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 0a 2a  f the supplied.*
8a630 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d 74  * integer lock-t
8a640 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ype..*/.static c
8a650 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b 74  onst char *lockt
8a660 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c 6f 63 6b  ypeName(int lock
8a670 74 79 70 65 29 7b 0a 20 20 73 77 69 74 63 68 28  type){.  switch(
8a680 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 63   locktype ){.  c
8a690 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72 65 74  ase NO_LOCK: ret
8a6a0 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 20 63 61  urn "NONE";.  ca
8a6b0 73 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3a 20  se SHARED_LOCK: 
8a6c0 72 65 74 75 72 6e 20 22 53 48 41 52 45 44 22 3b  return "SHARED";
8a6d0 0a 20 20 63 61 73 65 20 52 45 53 45 52 56 45 44  .  case RESERVED
8a6e0 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 52  _LOCK: return "R
8a6f0 45 53 45 52 56 45 44 22 3b 0a 20 20 63 61 73 65  ESERVED";.  case
8a700 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3a 20 72   PENDING_LOCK: r
8a710 65 74 75 72 6e 20 22 50 45 4e 44 49 4e 47 22 3b  eturn "PENDING";
8a720 0a 20 20 63 61 73 65 20 45 58 43 4c 55 53 49 56  .  case EXCLUSIV
8a730 45 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22  E_LOCK: return "
8a740 45 58 43 4c 55 53 49 56 45 22 3b 0a 20 20 7d 0a  EXCLUSIVE";.  }.
8a750 20 20 72 65 74 75 72 6e 20 22 45 52 52 4f 52 22    return "ERROR"
8a760 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
8a770 2a 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72  * If we are curr
8a780 65 6e 74 6c 79 20 69 6e 20 61 20 64 69 66 66 65  ently in a diffe
8a790 72 65 6e 74 20 74 68 72 65 61 64 20 74 68 61 6e  rent thread than
8a7a0 20 74 68 65 20 74 68 72 65 61 64 20 74 68 61 74   the thread that
8a7b0 20 74 68 65 0a 2a 2a 20 75 6e 69 78 46 69 6c 65   the.** unixFile
8a7c0 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67   argument belong
8a7d0 73 20 74 6f 2c 20 74 68 65 6e 20 74 72 61 6e 73  s to, then trans
8a7e0 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66  fer ownership of
8a7f0 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a   the unixFile.**
8a800 20 6f 76 65 72 20 74 6f 20 74 68 65 20 63 75 72   over to the cur
8a810 72 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a  rent thread..**.
8a820 2a 2a 20 41 20 75 6e 69 78 46 69 6c 65 20 69 73  ** A unixFile is
8a830 20 6f 6e 6c 79 20 6f 77 6e 65 64 20 62 79 20 61   only owned by a
8a840 20 74 68 72 65 61 64 20 6f 6e 20 73 79 73 74 65   thread on syste
8a850 6d 73 20 77 68 65 72 65 20 6f 6e 65 20 74 68 72  ms where one thr
8a860 65 61 64 20 69 73 0a 2a 2a 20 75 6e 61 62 6c 65  ead is.** unable
8a870 20 74 6f 20 6f 76 65 72 72 69 64 65 20 6c 6f 63   to override loc
8a880 6b 73 20 63 72 65 61 74 65 64 20 62 79 20 61 20  ks created by a 
8a890 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
8a8a0 2e 20 20 52 65 64 48 61 74 39 20 69 73 0a 2a 2a  .  RedHat9 is.**
8a8b0 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 73   an example of s
8a8c0 75 63 68 20 61 20 73 79 73 74 65 6d 2e 0a 2a 2a  uch a system..**
8a8d0 0a 2a 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72  .** Ownership tr
8a8e0 61 6e 73 66 65 72 20 69 73 20 6f 6e 6c 79 20 61  ansfer is only a
8a8f0 6c 6c 6f 77 65 64 20 69 66 20 74 68 65 20 75 6e  llowed if the un
8a900 69 78 46 69 6c 65 20 69 73 20 63 75 72 72 65 6e  ixFile is curren
8a910 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2a  tly unlocked..**
8a920 20 49 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65   If the unixFile
8a930 20 69 73 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61   is locked and a
8a940 6e 20 6f 77 6e 65 72 73 68 69 70 20 69 73 20 77  n ownership is w
8a950 72 6f 6e 67 2c 20 74 68 65 6e 20 72 65 74 75 72  rong, then retur
8a960 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55  n.** SQLITE_MISU
8a970 53 45 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 20 69  SE.  SQLITE_OK i
8a980 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 65 76  s returned if ev
8a990 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2e 0a  erything works..
8a9a0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
8a9b0 52 45 41 44 53 41 46 45 0a 73 74 61 74 69 63 20  READSAFE.static 
8a9c0 69 6e 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65  int transferOwne
8a9d0 72 73 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a  rship(unixFile *
8a9e0 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63  pFile){.  int rc
8a9f0 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 20 68 53  ;.  pthread_t hS
8aa00 65 6c 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61  elf;.  if( threa
8aa10 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
8aa20 68 65 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20  hersLocks ){.   
8aa30 20 2f 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72   /* Ownership tr
8aa40 61 6e 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64  ansfers not need
8aa50 65 64 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65  ed on this syste
8aa60 6d 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  m */.    return 
8aa70 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
8aa80 20 68 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64   hSelf = pthread
8aa90 5f 73 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70  _self();.  if( p
8aaa0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69  thread_equal(pFi
8aab0 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20  le->tid, hSelf) 
8aac0 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65  ){.    /* We are
8aad0 20 73 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61   still in the sa
8aae0 6d 65 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20  me thread */.   
8aaf0 20 4f 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72   OSTRACE1("No-tr
8ab00 61 6e 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72  ansfer, same thr
8ab10 65 61 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74  ead\n");.    ret
8ab20 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
8ab30 20 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e   }.  if( pFile->
8ab40 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
8ab50 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63  K ){.    /* We c
8ab60 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e  annot change own
8ab70 65 72 73 68 69 70 20 77 68 69 6c 65 20 77 65 20  ership while we 
8ab80 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f  are holding a lo
8ab90 63 6b 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ck! */.    retur
8aba0 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
8abb0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28  .  }.  OSTRACE4(
8abc0 22 54 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73  "Transfer owners
8abd0 68 69 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25  hip of %d from %
8abe0 64 20 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20  d to %d\n",.    
8abf0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68          pFile->h
8ac00 2c 20 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53  , pFile->tid, hS
8ac10 65 6c 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74  elf);.  pFile->t
8ac20 69 64 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66  id = hSelf;.  if
8ac30 20 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21   (pFile->pLock !
8ac40 3d 20 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65  = NULL) {.    re
8ac50 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46  leaseLockInfo(pF
8ac60 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20  ile->pLock);.   
8ac70 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e   rc = findLockIn
8ac80 66 6f 28 70 46 69 6c 65 2d 3e 68 2c 20 26 70 46  fo(pFile->h, &pF
8ac90 69 6c 65 2d 3e 70 4c 6f 63 6b 2c 20 30 29 3b 0a  ile->pLock, 0);.
8aca0 20 20 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f      OSTRACE5("LO
8acb0 43 4b 20 20 20 20 25 64 20 69 73 20 6e 6f 77 20  CK    %d is now 
8acc0 25 73 28 25 73 2c 25 64 29 5c 6e 22 2c 20 70 46  %s(%s,%d)\n", pF
8acd0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
8ace0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
8acf0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29  pFile->locktype)
8ad00 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63  ,.           loc
8ad10 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d  ktypeName(pFile-
8ad20 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65  >pLock->locktype
8ad30 29 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d  ), pFile->pLock-
8ad40 3e 63 6e 74 29 3b 0a 20 20 20 20 72 65 74 75 72  >cnt);.    retur
8ad50 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  n rc;.  } else {
8ad60 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
8ad70 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 6c  TE_OK;.  }.}.#el
8ad80 73 65 0a 20 20 2f 2a 20 4f 6e 20 73 69 6e 67 6c  se.  /* On singl
8ad90 65 2d 74 68 72 65 61 64 65 64 20 62 75 69 6c 64  e-threaded build
8ada0 73 2c 20 6f 77 6e 65 72 73 68 69 70 20 74 72 61  s, ownership tra
8adb0 6e 73 66 65 72 20 69 73 20 61 20 6e 6f 2d 6f 70  nsfer is a no-op
8adc0 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 74 72 61   */.# define tra
8add0 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 58  nsferOwnership(X
8ade0 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64  ) SQLITE_OK.#end
8adf0 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74  if../*.** Seek t
8ae00 6f 20 74 68 65 20 6f 66 66 73 65 74 20 70 61 73  o the offset pas
8ae10 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
8ae20 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 6e  d argument, then
8ae30 20 72 65 61 64 20 63 6e 74 20 0a 2a 2a 20 62 79   read cnt .** by
8ae40 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 20 52  tes into pBuf. R
8ae50 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
8ae60 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c   of bytes actual
8ae70 6c 79 20 72 65 61 64 2e 0a 2a 2f 0a 73 74 61 74  ly read..*/.stat
8ae80 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 52 65  ic int seekAndRe
8ae90 61 64 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c  ad(unixFile *id,
8aea0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f   sqlite3_int64 o
8aeb0 66 66 73 65 74 2c 20 76 6f 69 64 20 2a 70 42 75  ffset, void *pBu
8aec0 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69  f, int cnt){.  i
8aed0 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65  nt got;.  i64 ne
8aee0 77 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d 45 52  wOffset;.  TIMER
8aef0 5f 53 54 41 52 54 3b 0a 23 69 66 20 64 65 66 69  _START;.#if defi
8af00 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 0a 20  ned(USE_PREAD). 
8af10 20 67 6f 74 20 3d 20 70 72 65 61 64 28 69 64 2d   got = pread(id-
8af20 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
8af30 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61  ffset);.  Simula
8af40 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d  teIOError( got =
8af50 20 2d 31 20 29 3b 0a 23 65 6c 69 66 20 64 65 66   -1 );.#elif def
8af60 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34  ined(USE_PREAD64
8af70 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 61 64 36  ).  got = pread6
8af80 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  4(id->h, pBuf, c
8af90 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 53  nt, offset);.  S
8afa0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
8afb0 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 73  got = -1 );.#els
8afc0 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20  e.  newOffset = 
8afd0 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66  lseek(id->h, off
8afe0 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a  set, SEEK_SET);.
8aff0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
8b000 72 28 20 6e 65 77 4f 66 66 73 65 74 2d 2d 20 29  r( newOffset-- )
8b010 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65  ;.  if( newOffse
8b020 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20  t!=offset ){.   
8b030 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a   return -1;.  }.
8b040 20 20 67 6f 74 20 3d 20 72 65 61 64 28 69 64 2d    got = read(id-
8b050 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a  >h, pBuf, cnt);.
8b060 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45  #endif.  TIMER_E
8b070 4e 44 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22  ND;.  OSTRACE5("
8b080 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64  READ    %-3d %5d
8b090 20 25 37 6c 6c 64 20 25 64 5c 6e 22 2c 20 69 64   %7lld %d\n", id
8b0a0 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74  ->h, got, offset
8b0b0 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29  , TIMER_ELAPSED)
8b0c0 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a  ;.  return got;.
8b0d0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61  }../*.** Read da
8b0e0 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69  ta from a file i
8b0f0 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52  nto a buffer.  R
8b100 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
8b110 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20  if all.** bytes 
8b120 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73  were read succes
8b130 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54  sfully and SQLIT
8b140 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68  E_IOERR if anyth
8b150 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e  ing goes.** wron
8b160 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
8b170 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73 71 6c   unixRead(.  sql
8b180 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a  ite3_file *id, .
8b190 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20    void *pBuf, . 
8b1a0 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69   int amt,.  sqli
8b1b0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
8b1c0 0a 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20  .){.  int got;. 
8b1d0 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20   assert( id );. 
8b1e0 20 67 6f 74 20 3d 20 73 65 65 6b 41 6e 64 52 65   got = seekAndRe
8b1f0 61 64 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  ad((unixFile*)id
8b200 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20  , offset, pBuf, 
8b210 61 6d 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d  amt);.  if( got=
8b220 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75  =amt ){.    retu
8b230 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
8b240 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20  }else if( got<0 
8b250 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
8b260 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b  LITE_IOERR_READ;
8b270 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65  .  }else{.    me
8b280 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42  mset(&((char*)pB
8b290 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74  uf)[got], 0, amt
8b2a0 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72  -got);.    retur
8b2b0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  n SQLITE_IOERR_S
8b2c0 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d  HORT_READ;.  }.}
8b2d0 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20  ../*.** Seek to 
8b2e0 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69 64  the offset in id
8b2f0 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72 65  ->offset then re
8b300 61 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e 74  ad cnt bytes int
8b310 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75 72  o pBuf..** Retur
8b320 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
8b330 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72  bytes actually r
8b340 65 61 64 2e 20 20 55 70 64 61 74 65 20 74 68 65  ead.  Update the
8b350 20 6f 66 66 73 65 74 2e 0a 2a 2f 0a 73 74 61 74   offset..*/.stat
8b360 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72  ic int seekAndWr
8b370 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64  ite(unixFile *id
8b380 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f  , i64 offset, co
8b390 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  nst void *pBuf, 
8b3a0 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20  int cnt){.  int 
8b3b0 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66  got;.  i64 newOf
8b3c0 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54  fset;.  TIMER_ST
8b3d0 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  ART;.#if defined
8b3e0 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f  (USE_PREAD).  go
8b3f0 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68  t = pwrite(id->h
8b400 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66  , pBuf, cnt, off
8b410 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69  set);.#elif defi
8b420 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29  ned(USE_PREAD64)
8b430 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36  .  got = pwrite6
8b440 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  4(id->h, pBuf, c
8b450 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c  nt, offset);.#el
8b460 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d  se.  newOffset =
8b470 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66   lseek(id->h, of
8b480 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b  fset, SEEK_SET);
8b490 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74  .  if( newOffset
8b4a0 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  !=offset ){.    
8b4b0 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20  return -1;.  }. 
8b4c0 20 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d   got = write(id-
8b4d0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a  >h, pBuf, cnt);.
8b4e0 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45  #endif.  TIMER_E
8b4f0 4e 44 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22  ND;.  OSTRACE5("
8b500 57 52 49 54 45 20 20 20 25 2d 33 64 20 25 35 64  WRITE   %-3d %5d
8b510 20 25 37 6c 6c 64 20 25 64 5c 6e 22 2c 20 69 64   %7lld %d\n", id
8b520 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74  ->h, got, offset
8b530 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29  , TIMER_ELAPSED)
8b540 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a  ;.  return got;.
8b550 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  }.../*.** Write 
8b560 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66  data from a buff
8b570 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20  er into a file. 
8b580 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
8b590 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20  K on success.** 
8b5a0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72  or some other er
8b5b0 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c  ror code on fail
8b5c0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
8b5d0 6e 74 20 75 6e 69 78 57 72 69 74 65 28 0a 20 20  nt unixWrite(.  
8b5e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
8b5f0 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
8b600 2a 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d  *pBuf, .  int am
8b610 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  t,.  sqlite3_int
8b620 36 34 20 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20  64 offset .){.  
8b630 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20  int wrote = 0;. 
8b640 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20   assert( id );. 
8b650 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29   assert( amt>0 )
8b660 3b 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30  ;.  while( amt>0
8b670 20 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65   && (wrote = see
8b680 6b 41 6e 64 57 72 69 74 65 28 28 75 6e 69 78 46  kAndWrite((unixF
8b690 69 6c 65 2a 29 69 64 2c 20 6f 66 66 73 65 74 2c  ile*)id, offset,
8b6a0 20 70 42 75 66 2c 20 61 6d 74 29 29 3e 30 20 29   pBuf, amt))>0 )
8b6b0 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f  {.    amt -= wro
8b6c0 74 65 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b  te;.    offset +
8b6d0 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75  = wrote;.    pBu
8b6e0 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75  f = &((char*)pBu
8b6f0 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20  f)[wrote];.  }. 
8b700 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
8b710 28 28 20 77 72 6f 74 65 3d 28 2d 31 29 2c 20 61  (( wrote=(-1), a
8b720 6d 74 3d 31 20 29 29 3b 0a 20 20 53 69 6d 75 6c  mt=1 ));.  Simul
8b730 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
8b740 28 28 20 77 72 6f 74 65 3d 30 2c 20 61 6d 74 3d  (( wrote=0, amt=
8b750 31 20 29 29 3b 0a 20 20 69 66 28 20 61 6d 74 3e  1 ));.  if( amt>
8b760 30 20 29 7b 0a 20 20 20 20 69 66 28 20 77 72 6f  0 ){.    if( wro
8b770 74 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 65  te<0 ){.      re
8b780 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
8b790 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c  R_WRITE;.    }el
8b7a0 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  se{.      return
8b7b0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20   SQLITE_FULL;.  
8b7c0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
8b7d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
8b7e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
8b7f0 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68  T./*.** Count th
8b800 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c  e number of full
8b810 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c  syncs and normal
8b820 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73   syncs.  This is
8b830 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a   used to test.**
8b840 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20   that syncs and 
8b850 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63  fullsyncs are oc
8b860 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69  curing at the ri
8b870 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51  ght times..*/.SQ
8b880 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
8b890 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20  ite3_sync_count 
8b8a0 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  = 0;.SQLITE_API 
8b8b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c  int sqlite3_full
8b8c0 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  sync_count = 0;.
8b8d0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 55 73  #endif../*.** Us
8b8e0 65 20 74 68 65 20 66 64 61 74 61 73 79 6e 63 28  e the fdatasync(
8b8f0 29 20 41 50 49 20 6f 6e 6c 79 20 69 66 20 74 68  ) API only if th
8b900 65 20 48 41 56 45 5f 46 44 41 54 41 53 59 4e 43  e HAVE_FDATASYNC
8b910 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65   macro is define
8b920 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20  d..** Otherwise 
8b930 75 73 65 20 66 73 79 6e 63 28 29 20 69 6e 20 69  use fsync() in i
8b940 74 73 20 70 6c 61 63 65 2e 0a 2a 2f 0a 23 69 66  ts place..*/.#if
8b950 6e 64 65 66 20 48 41 56 45 5f 46 44 41 54 41 53  ndef HAVE_FDATAS
8b960 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 66 64 61  YNC.# define fda
8b970 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e  tasync fsync.#en
8b980 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e  dif../*.** Defin
8b990 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  e HAVE_FULLFSYNC
8b9a0 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e   to 0 or 1 depen
8b9b0 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20  ding on whether 
8b9c0 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f  or not.** the F_
8b9d0 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20  FULLFSYNC macro 
8b9e0 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46  is defined.  F_F
8b9f0 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72  ULLFSYNC is curr
8ba00 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76  ently.** only av
8ba10 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f  ailable on Mac O
8ba20 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63  S X.  But that c
8ba30 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  ould change..*/.
8ba40 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59  #ifdef F_FULLFSY
8ba50 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45  NC.# define HAVE
8ba60 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c  _FULLFSYNC 1.#el
8ba70 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45  se.# define HAVE
8ba80 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e  _FULLFSYNC 0.#en
8ba90 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dif.../*.** The 
8baa0 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63  fsync() system c
8bab0 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  all does not wor
8bac0 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20  k as advertised 
8bad0 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20  on many.** unix 
8bae0 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f  systems.  The fo
8baf0 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72  llowing procedur
8bb00 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20  e is an attempt 
8bb10 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f  to make.** it wo
8bb20 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a  rk better..**.**
8bb30 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53   The SQLITE_NO_S
8bb40 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c  YNC macro disabl
8bb50 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e  es all fsync()s.
8bb60 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c    This is useful
8bb70 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20  .** for testing 
8bb80 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20  when we want to 
8bb90 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  run through the 
8bba0 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b  test suite quick
8bbb0 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73  ly..** You are s
8bbc0 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20  trongly advised 
8bbd0 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20  *not* to deploy 
8bbe0 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  with SQLITE_NO_S
8bbf0 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20  YNC.** enabled, 
8bc00 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77  however, since w
8bc10 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  ith SQLITE_NO_SY
8bc20 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f  NC enabled, an O
8bc30 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f  S crash.** or po
8bc40 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c  wer failure will
8bc50 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20   likely corrupt 
8bc60 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
8bc70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
8bc80 20 66 75 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20   full_fsync(int 
8bc90 66 64 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63  fd, int fullSync
8bca0 2c 20 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b  , int dataOnly){
8bcb0 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a  .  int rc;..  /*
8bcc0 20 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62   Record the numb
8bcd0 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74  er of times that
8bce0 20 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20   we do a normal 
8bcf0 66 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a  fsync() and .  *
8bd00 2a 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69  * FULLSYNC.  Thi
8bd10 73 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67  s is used during
8bd20 20 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69   testing to veri
8bd30 66 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f  fy that this pro
8bd40 63 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73  cedure.  ** gets
8bd50 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65   called with the
8bd60 20 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e   correct argumen
8bd70 74 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  ts..  */.#ifdef 
8bd80 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66  SQLITE_TEST.  if
8bd90 28 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c  ( fullSync ) sql
8bda0 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
8bdb0 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  unt++;.  sqlite3
8bdc0 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  _sync_count++;.#
8bdd0 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77  endif..  /* If w
8bde0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  e compiled with 
8bdf0 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  the SQLITE_NO_SY
8be00 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79  NC flag, then sy
8be10 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20  ncing is a.  ** 
8be20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65  no-op.  */.#ifde
8be30 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  f SQLITE_NO_SYNC
8be40 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  .  rc = SQLITE_O
8be50 4b 3b 0a 23 65 6c 73 65 0a 0a 23 69 66 20 48 41  K;.#else..#if HA
8be60 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 20 69  VE_FULLFSYNC.  i
8be70 66 28 20 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20  f( fullSync ){. 
8be80 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 66 64     rc = fcntl(fd
8be90 2c 20 46 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 30  , F_FULLFSYNC, 0
8bea0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
8beb0 72 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 2f 2a  rc = 1;.  }.  /*
8bec0 20 49 66 20 74 68 65 20 46 55 4c 4c 46 53 59 4e   If the FULLFSYN
8bed0 43 20 66 61 69 6c 65 64 2c 20 66 61 6c 6c 20 62  C failed, fall b
8bee0 61 63 6b 20 74 6f 20 61 74 74 65 6d 70 74 69 6e  ack to attemptin
8bef0 67 20 61 6e 20 66 73 79 6e 63 28 29 2e 0a 20 20  g an fsync()..  
8bf00 20 2a 20 49 74 20 73 68 6f 75 6c 64 6e 27 74 20   * It shouldn't 
8bf10 62 65 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20  be possible for 
8bf20 66 75 6c 6c 66 73 79 6e 63 20 74 6f 20 66 61 69  fullfsync to fai
8bf30 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 0a  l on the local .
8bf40 20 20 20 2a 20 66 69 6c 65 20 73 79 73 74 65 6d     * file system
8bf50 20 28 6f 6e 20 4f 53 58 29 2c 20 73 6f 20 66 61   (on OSX), so fa
8bf60 69 6c 75 72 65 20 69 6e 64 69 63 61 74 65 73 20  ilure indicates 
8bf70 74 68 61 74 20 46 55 4c 4c 46 53 59 4e 43 0a 20  that FULLFSYNC. 
8bf80 20 20 2a 20 69 73 6e 27 74 20 73 75 70 70 6f 72    * isn't suppor
8bf90 74 65 64 20 66 6f 72 20 74 68 69 73 20 66 69 6c  ted for this fil
8bfa0 65 20 73 79 73 74 65 6d 2e 20 53 6f 2c 20 61 74  e system. So, at
8bfb0 74 65 6d 70 74 20 61 6e 20 66 73 79 6e 63 20 0a  tempt an fsync .
8bfc0 20 20 20 2a 20 61 6e 64 20 28 66 6f 72 20 6e 6f     * and (for no
8bfd0 77 29 20 69 67 6e 6f 72 65 20 74 68 65 20 6f 76  w) ignore the ov
8bfe0 65 72 68 65 61 64 20 6f 66 20 61 20 73 75 70 65  erhead of a supe
8bff0 72 66 6c 75 6f 75 73 20 66 63 6e 74 6c 20 63 61  rfluous fcntl ca
8c000 6c 6c 2e 20 20 0a 20 20 20 2a 20 49 74 27 64 20  ll.  .   * It'd 
8c010 62 65 20 62 65 74 74 65 72 20 74 6f 20 64 65 74  be better to det
8c020 65 63 74 20 66 75 6c 6c 66 73 79 6e 63 20 73 75  ect fullfsync su
8c030 70 70 6f 72 74 20 6f 6e 63 65 20 61 6e 64 20 61  pport once and a
8c040 76 6f 69 64 20 0a 20 20 20 2a 20 74 68 65 20 66  void .   * the f
8c050 63 6e 74 6c 20 63 61 6c 6c 20 65 76 65 72 79 20  cntl call every 
8c060 74 69 6d 65 20 73 79 6e 63 20 69 73 20 63 61 6c  time sync is cal
8c070 6c 65 64 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28  led..   */.  if(
8c080 20 72 63 20 29 20 72 63 20 3d 20 66 73 79 6e 63   rc ) rc = fsync
8c090 28 66 64 29 3b 0a 0a 23 65 6c 73 65 20 0a 20 20  (fd);..#else .  
8c0a0 69 66 28 20 64 61 74 61 4f 6e 6c 79 20 29 7b 0a  if( dataOnly ){.
8c0b0 20 20 20 20 72 63 20 3d 20 66 64 61 74 61 73 79      rc = fdatasy
8c0c0 6e 63 28 66 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  nc(fd);.  }else{
8c0d0 0a 20 20 20 20 72 63 20 3d 20 66 73 79 6e 63 28  .    rc = fsync(
8c0e0 66 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  fd);.  }.#endif 
8c0f0 2f 2a 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e  /* HAVE_FULLFSYN
8c100 43 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 64  C */.#endif /* d
8c110 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4e 4f  efined(SQLITE_NO
8c120 5f 53 59 4e 43 29 20 2a 2f 0a 0a 20 20 72 65 74  _SYNC) */..  ret
8c130 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
8c140 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77   Make sure all w
8c150 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69  rites to a parti
8c160 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63  cular file are c
8c170 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b  ommitted to disk
8c180 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 61 74 61 4f  ..**.** If dataO
8c190 6e 6c 79 3d 3d 30 20 74 68 65 6e 20 62 6f 74 68  nly==0 then both
8c1a0 20 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c 66   the file itself
8c1b0 20 61 6e 64 20 69 74 73 20 6d 65 74 61 64 61 74   and its metadat
8c1c0 61 20 28 66 69 6c 65 0a 2a 2a 20 73 69 7a 65 2c  a (file.** size,
8c1d0 20 61 63 63 65 73 73 20 74 69 6d 65 2c 20 65 74   access time, et
8c1e0 63 29 20 61 72 65 20 73 79 6e 63 65 64 2e 20 20  c) are synced.  
8c1f0 49 66 20 64 61 74 61 4f 6e 6c 79 21 3d 30 20 74  If dataOnly!=0 t
8c200 68 65 6e 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20  hen only the.** 
8c210 66 69 6c 65 20 64 61 74 61 20 69 73 20 73 79 6e  file data is syn
8c220 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 64 65 72  ced..**.** Under
8c230 20 55 6e 69 78 2c 20 61 6c 73 6f 20 6d 61 6b 65   Unix, also make
8c240 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 64   sure that the d
8c250 69 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 66  irectory entry f
8c260 6f 72 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 68  or the file.** h
8c270 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 20  as been created 
8c280 62 79 20 66 73 79 6e 63 2d 69 6e 67 20 74 68 65  by fsync-ing the
8c290 20 64 69 72 65 63 74 6f 72 79 20 74 68 61 74 20   directory that 
8c2a0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 69 6c  contains the fil
8c2b0 65 2e 0a 2a 2a 20 49 66 20 77 65 20 64 6f 20 6e  e..** If we do n
8c2c0 6f 74 20 64 6f 20 74 68 69 73 20 61 6e 64 20 77  ot do this and w
8c2d0 65 20 65 6e 63 6f 75 6e 74 65 72 20 61 20 70 6f  e encounter a po
8c2e0 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 65  wer failure, the
8c2f0 20 64 69 72 65 63 74 6f 72 79 0a 2a 2a 20 65 6e   directory.** en
8c300 74 72 79 20 66 6f 72 20 74 68 65 20 6a 6f 75 72  try for the jour
8c310 6e 61 6c 20 6d 69 67 68 74 20 6e 6f 74 20 65 78  nal might not ex
8c320 69 73 74 20 61 66 74 65 72 20 77 65 20 72 65 62  ist after we reb
8c330 6f 6f 74 2e 20 20 54 68 65 20 6e 65 78 74 0a 2a  oot.  The next.*
8c340 2a 20 53 51 4c 69 74 65 20 74 6f 20 61 63 63 65  * SQLite to acce
8c350 73 73 20 74 68 65 20 66 69 6c 65 20 77 69 6c 6c  ss the file will
8c360 20 6e 6f 74 20 6b 6e 6f 77 20 74 68 61 74 20 74   not know that t
8c370 68 65 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74  he journal exist
8c380 73 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68  s (because.** th
8c390 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74 72  e directory entr
8c3a0 79 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61  y for the journa
8c3b0 6c 20 77 61 73 20 6e 65 76 65 72 20 63 72 65 61  l was never crea
8c3c0 74 65 64 29 20 61 6e 64 20 74 68 65 20 74 72 61  ted) and the tra
8c3d0 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c  nsaction.** will
8c3e0 20 6e 6f 74 20 72 6f 6c 6c 20 62 61 63 6b 20 2d   not roll back -
8c3f0 20 70 6f 73 73 69 62 6c 79 20 6c 65 61 64 69 6e   possibly leadin
8c400 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 6f  g to database co
8c410 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  rruption..*/.sta
8c420 74 69 63 20 69 6e 74 20 75 6e 69 78 53 79 6e 63  tic int unixSync
8c430 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
8c440 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  d, int flags){. 
8c450 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 69 78 46   int rc;.  unixF
8c460 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
8c470 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 69  ixFile*)id;..  i
8c480 6e 74 20 69 73 44 61 74 61 4f 6e 6c 79 20 3d 20  nt isDataOnly = 
8c490 28 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 53 59  (flags&SQLITE_SY
8c4a0 4e 43 5f 44 41 54 41 4f 4e 4c 59 29 3b 0a 20 20  NC_DATAONLY);.  
8c4b0 69 6e 74 20 69 73 46 75 6c 6c 73 79 6e 63 20 3d  int isFullsync =
8c4c0 20 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53   (flags&0x0F)==S
8c4d0 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3b  QLITE_SYNC_FULL;
8c4e0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
8c4f0 74 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  t one of SQLITE_
8c500 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 46  SYNC_NORMAL or F
8c510 55 4c 4c 20 77 61 73 20 70 61 73 73 65 64 20 2a  ULL was passed *
8c520 2f 0a 20 20 61 73 73 65 72 74 28 28 66 6c 61 67  /.  assert((flag
8c530 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f  s&0x0F)==SQLITE_
8c540 53 59 4e 43 5f 4e 4f 52 4d 41 4c 0a 20 20 20 20  SYNC_NORMAL.    
8c550 20 20 7c 7c 20 28 66 6c 61 67 73 26 30 78 30 46    || (flags&0x0F
8c560 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46  )==SQLITE_SYNC_F
8c570 55 4c 4c 0a 20 20 29 3b 0a 0a 20 20 61 73 73 65  ULL.  );..  asse
8c580 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
8c590 53 54 52 41 43 45 32 28 22 53 59 4e 43 20 20 20  STRACE2("SYNC   
8c5a0 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d   %-3d\n", pFile-
8c5b0 3e 68 29 3b 0a 20 20 72 63 20 3d 20 66 75 6c 6c  >h);.  rc = full
8c5c0 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 68 2c  _fsync(pFile->h,
8c5d0 20 69 73 46 75 6c 6c 73 79 6e 63 2c 20 69 73 44   isFullsync, isD
8c5e0 61 74 61 4f 6e 6c 79 29 3b 0a 20 20 53 69 6d 75  ataOnly);.  Simu
8c5f0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d  lateIOError( rc=
8c600 31 20 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  1 );.  if( rc ){
8c610 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
8c620 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 3b 0a  TE_IOERR_FSYNC;.
8c630 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d    }.  if( pFile-
8c640 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20  >dirfd>=0 ){.   
8c650 20 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59   OSTRACE4("DIRSY
8c660 4e 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75  NC %-3d (have_fu
8c670 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73  llfsync=%d fulls
8c680 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c  ync=%d)\n", pFil
8c690 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20  e->dirfd,.      
8c6a0 20 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46        HAVE_FULLF
8c6b0 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63  SYNC, isFullsync
8c6c0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
8c6d0 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e  E_DISABLE_DIRSYN
8c6e0 43 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72  C.    /* The dir
8c6f0 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f  ectory sync is o
8c700 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66  nly attempted if
8c710 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20   full_fsync is. 
8c720 20 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66     ** turned off
8c730 20 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e   or unavailable.
8c740 20 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e    If a full_fsyn
8c750 63 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65  c occurred above
8c760 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68  ,.    ** then th
8c770 65 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63  e directory sync
8c780 20 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e   is superfluous.
8c790 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
8c7a0 28 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  (!HAVE_FULLFSYNC
8c7b0 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29   || !isFullsync)
8c7c0 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70   && full_fsync(p
8c7d0 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29  File->dirfd,0,0)
8c7e0 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20   ){.       /*.  
8c7f0 20 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20       ** We have 
8c800 72 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c  received multipl
8c810 65 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79  e reports of fsy
8c820 6e 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20  nc() returning. 
8c830 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20        ** errors 
8c840 77 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20  when applied to 
8c850 64 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63  directories on c
8c860 65 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74  ertain file syst
8c870 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41  ems..       ** A
8c880 20 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72   failed director
8c890 79 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20  y sync is not a 
8c8a0 62 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74  big deal.  So it
8c8b0 20 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a   seems.       **
8c8c0 20 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72   better to ignor
8c8d0 65 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69  e the error.  Ti
8c8e0 63 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20  cket #1657.     
8c8f0 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 72    */.       /* r
8c900 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
8c910 52 52 3b 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e  RR; */.    }.#en
8c920 64 69 66 0a 20 20 20 20 63 6c 6f 73 65 28 70 46  dif.    close(pF
8c930 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 20 2f 2a  ile->dirfd);  /*
8c940 20 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79   Only need to sy
8c950 6e 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73  nc once, so clos
8c960 65 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  e the directory 
8c970 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 69  */.    pFile->di
8c980 72 66 64 20 3d 20 2d 31 3b 20 20 20 20 2f 2a 20  rfd = -1;    /* 
8c990 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65  when we are done
8c9a0 2e 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72  . */.  }.  retur
8c9b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
8c9c0 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61  /*.** Truncate a
8c9d0 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61  n open file to a
8c9e0 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a   specified size.
8c9f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
8ca00 69 78 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  ixTruncate(sqlit
8ca10 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34  e3_file *id, i64
8ca20 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72   nByte){.  int r
8ca30 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 20  c;.  assert( id 
8ca40 29 3b 0a 20 20 72 63 20 3d 20 66 74 72 75 6e 63  );.  rc = ftrunc
8ca50 61 74 65 28 28 28 75 6e 69 78 46 69 6c 65 2a 29  ate(((unixFile*)
8ca60 69 64 29 2d 3e 68 2c 20 28 6f 66 66 5f 74 29 6e  id)->h, (off_t)n
8ca70 42 79 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74  Byte);.  Simulat
8ca80 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 29  eIOError( rc=1 )
8ca90 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
8caa0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
8cab0 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a  IOERR_TRUNCATE;.
8cac0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
8cad0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
8cae0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65   }.}../*.** Dete
8caf0 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e  rmine the curren
8cb00 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65  t size of a file
8cb10 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61   in bytes.*/.sta
8cb20 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65  tic int unixFile
8cb30 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
8cb40 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a  e *id, i64 *pSiz
8cb50 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e){.  int rc;.  
8cb60 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b  struct stat buf;
8cb70 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b  .  assert( id );
8cb80 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 28 28  .  rc = fstat(((
8cb90 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68  unixFile*)id)->h
8cba0 2c 20 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 6c  , &buf);.  Simul
8cbb0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31  ateIOError( rc=1
8cbc0 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20   );.  if( rc!=0 
8cbd0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
8cbe0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
8cbf0 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d  ;.  }.  *pSize =
8cc00 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 20 20   buf.st_size;.  
8cc10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
8cc20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
8cc30 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
8cc40 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
8cc50 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
8cc60 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
8cc70 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
8cc80 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
8cc90 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
8cca0 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72   lock is held, r
8ccb0 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  eturn.** non-zer
8ccc0 6f 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 20  o.  If the file 
8ccd0 69 73 20 75 6e 6c 6f 63 6b 65 64 20 6f 72 20 68  is unlocked or h
8cce0 6f 6c 64 73 20 6f 6e 6c 79 20 53 48 41 52 45 44  olds only SHARED
8ccf0 20 6c 6f 63 6b 73 2c 20 74 68 65 6e 0a 2a 2a 20   locks, then.** 
8cd00 72 65 74 75 72 6e 20 7a 65 72 6f 2e 0a 2a 2f 0a  return zero..*/.
8cd10 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43  static int unixC
8cd20 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
8cd30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
8cd40 64 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b  d){.  int r = 0;
8cd50 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
8cd60 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
8cd70 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  id;..  assert( p
8cd80 46 69 6c 65 20 29 3b 0a 20 20 65 6e 74 65 72 4d  File );.  enterM
8cd90 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75  utex(); /* Becau
8cda0 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20  se pFile->pLock 
8cdb0 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73  is shared across
8cdc0 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f   threads */..  /
8cdd0 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72  * Check if a thr
8cde0 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63  ead in this proc
8cdf0 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61  ess holds such a
8ce00 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70   lock */.  if( p
8ce10 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63  File->pLock->loc
8ce20 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43  ktype>SHARED_LOC
8ce30 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a  K ){.    r = 1;.
8ce40 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 68 65 72 77    }..  /* Otherw
8ce50 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20  ise see if some 
8ce60 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f  other process ho
8ce70 6c 64 73 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69  lds it..  */.  i
8ce80 66 28 20 21 72 20 29 7b 0a 20 20 20 20 73 74 72  f( !r ){.    str
8ce90 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a  uct flock lock;.
8cea0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63      lock.l_whenc
8ceb0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
8cec0 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
8ced0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
8cee0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
8cef0 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74   1;.    lock.l_t
8cf00 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20  ype = F_WRLCK;. 
8cf10 20 20 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e     fcntl(pFile->
8cf20 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63  h, F_GETLK, &loc
8cf30 6b 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b  k);.    if( lock
8cf40 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b  .l_type!=F_UNLCK
8cf50 20 29 7b 0a 20 20 20 20 20 20 72 20 3d 20 31 3b   ){.      r = 1;
8cf60 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20  .    }.  }.  .  
8cf70 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  leaveMutex();.  
8cf80 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57  OSTRACE3("TEST W
8cf90 52 2d 4c 4f 43 4b 20 25 64 20 25 64 5c 6e 22 2c  R-LOCK %d %d\n",
8cfa0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 29 3b 0a 0a   pFile->h, r);..
8cfb0 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f    return r;.}../
8cfc0 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  *.** Lock the fi
8cfd0 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
8cfe0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
8cff0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65  rameter locktype
8d000 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
8d010 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
8d020 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
8d030 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
8d040 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
8d050 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
8d060 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
8d070 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
8d080 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
8d090 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
8d0a0 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
8d0b0 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
8d0c0 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
8d0d0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
8d0e0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
8d0f0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
8d100 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
8d110 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
8d120 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
8d130 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
8d140 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
8d150 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
8d160 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
8d170 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
8d180 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
8d190 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
8d1a0 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
8d1b0 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
8d1c0 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
8d1d0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
8d1e0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
8d1f0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
8d200 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
8d210 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
8d220 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
8d230 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
8d240 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
8d250 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
8d260 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
8d270 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
8d280 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
8d290 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
8d2a0 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f  Use the sqlite3O
8d2b0 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75  sUnlock().** rou
8d2c0 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20  tine to lower a 
8d2d0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a  locking level..*
8d2e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
8d2f0 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  xLock(sqlite3_fi
8d300 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
8d310 74 79 70 65 29 7b 0a 20 20 2f 2a 20 54 68 65 20  type){.  /* The 
8d320 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69  following descri
8d330 62 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  bes the implemen
8d340 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61  tation of the va
8d350 72 69 6f 75 73 20 6c 6f 63 6b 73 20 61 6e 64 0a  rious locks and.
8d360 20 20 2a 2a 20 6c 6f 63 6b 20 74 72 61 6e 73 69    ** lock transi
8d370 74 69 6f 6e 73 20 69 6e 20 74 65 72 6d 73 20 6f  tions in terms o
8d380 66 20 74 68 65 20 50 4f 53 49 58 20 61 64 76 69  f the POSIX advi
8d390 73 6f 72 79 20 73 68 61 72 65 64 20 61 6e 64 20  sory shared and 
8d3a0 65 78 63 6c 75 73 69 76 65 0a 20 20 2a 2a 20 6c  exclusive.  ** l
8d3b0 6f 63 6b 20 70 72 69 6d 69 74 69 76 65 73 20 28  ock primitives (
8d3c0 63 61 6c 6c 65 64 20 72 65 61 64 2d 6c 6f 63 6b  called read-lock
8d3d0 73 20 61 6e 64 20 77 72 69 74 65 2d 6c 6f 63 6b  s and write-lock
8d3e0 73 20 62 65 6c 6f 77 2c 20 74 6f 20 61 76 6f 69  s below, to avoi
8d3f0 64 0a 20 20 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e  d.  ** confusion
8d400 20 77 69 74 68 20 53 51 4c 69 74 65 20 6c 6f 63   with SQLite loc
8d410 6b 20 6e 61 6d 65 73 29 2e 20 54 68 65 20 61 6c  k names). The al
8d420 67 6f 72 69 74 68 6d 73 20 61 72 65 20 63 6f 6d  gorithms are com
8d430 70 6c 69 63 61 74 65 64 0a 20 20 2a 2a 20 73 6c  plicated.  ** sl
8d440 69 67 68 74 6c 79 20 69 6e 20 6f 72 64 65 72 20  ightly in order 
8d450 74 6f 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65  to be compatible
8d460 20 77 69 74 68 20 77 69 6e 64 6f 77 73 20 73 79   with windows sy
8d470 73 74 65 6d 73 20 73 69 6d 75 6c 74 61 6e 65 6f  stems simultaneo
8d480 75 73 6c 79 0a 20 20 2a 2a 20 61 63 63 65 73 73  usly.  ** access
8d490 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74  ing the same dat
8d4a0 61 62 61 73 65 20 66 69 6c 65 2c 20 69 6e 20 63  abase file, in c
8d4b0 61 73 65 20 74 68 61 74 20 69 73 20 65 76 65 72  ase that is ever
8d4c0 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a   required..  **.
8d4d0 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 20 64 65 66    ** Symbols def
8d4e0 69 6e 65 64 20 69 6e 20 6f 73 2e 68 20 69 6e 64  ined in os.h ind
8d4f0 65 6e 74 69 66 79 20 74 68 65 20 27 70 65 6e 64  entify the 'pend
8d500 69 6e 67 20 62 79 74 65 27 20 61 6e 64 20 74 68  ing byte' and th
8d510 65 20 27 72 65 73 65 72 76 65 64 0a 20 20 2a 2a  e 'reserved.  **
8d520 20 62 79 74 65 27 2c 20 65 61 63 68 20 73 69 6e   byte', each sin
8d530 67 6c 65 20 62 79 74 65 73 20 61 74 20 77 65 6c  gle bytes at wel
8d540 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 73 2c  l known offsets,
8d550 20 61 6e 64 20 74 68 65 20 27 73 68 61 72 65 64   and the 'shared
8d560 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65   byte.  ** range
8d570 27 2c 20 61 20 72 61 6e 67 65 20 6f 66 20 35 31  ', a range of 51
8d580 30 20 62 79 74 65 73 20 61 74 20 61 20 77 65 6c  0 bytes at a wel
8d590 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e 0a  l known offset..
8d5a0 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 6f 62 74    **.  ** To obt
8d5b0 61 69 6e 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ain a SHARED loc
8d5c0 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69  k, a read-lock i
8d5d0 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68  s obtained on th
8d5e0 65 20 27 70 65 6e 64 69 6e 67 0a 20 20 2a 2a 20  e 'pending.  ** 
8d5f0 62 79 74 65 27 2e 20 20 49 66 20 74 68 69 73 20  byte'.  If this 
8d600 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61  is successful, a
8d610 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f   random byte fro
8d620 6d 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79  m the 'shared by
8d630 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69  te.  ** range' i
8d640 73 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20 61 6e  s read-locked an
8d650 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  d the lock on th
8d660 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27  e 'pending byte'
8d670 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2a 0a   released..  **.
8d680 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d    ** A process m
8d690 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61  ay only obtain a
8d6a0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61   RESERVED lock a
8d6b0 66 74 65 72 20 69 74 20 68 61 73 20 61 20 53 48  fter it has a SH
8d6c0 41 52 45 44 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20  ARED lock..  ** 
8d6d0 41 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  A RESERVED lock 
8d6e0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  is implemented b
8d6f0 79 20 67 72 61 62 62 69 6e 67 20 61 20 77 72 69  y grabbing a wri
8d700 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20  te-lock on the. 
8d710 20 2a 2a 20 27 72 65 73 65 72 76 65 64 20 62 79   ** 'reserved by
8d720 74 65 27 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  te'. .  **.  ** 
8d730 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e  A process may on
8d740 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44  ly obtain a PEND
8d750 49 4e 47 20 6c 6f 63 6b 20 61 66 74 65 72 20 69  ING lock after i
8d760 74 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 61  t has obtained a
8d770 0a 20 20 2a 2a 20 53 48 41 52 45 44 20 6c 6f 63  .  ** SHARED loc
8d780 6b 2e 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63  k. A PENDING loc
8d790 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  k is implemented
8d7a0 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 20   by obtaining a 
8d7b0 77 72 69 74 65 2d 6c 6f 63 6b 0a 20 20 2a 2a 20  write-lock.  ** 
8d7c0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  on the 'pending 
8d7d0 62 79 74 65 27 2e 20 54 68 69 73 20 65 6e 73 75  byte'. This ensu
8d7e0 72 65 73 20 74 68 61 74 20 6e 6f 20 6e 65 77 20  res that no new 
8d7f0 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 63 61 6e  SHARED locks can
8d800 20 62 65 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 65   be.  ** obtaine
8d810 64 2c 20 62 75 74 20 65 78 69 73 74 69 6e 67 20  d, but existing 
8d820 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 61 72 65  SHARED locks are
8d830 20 61 6c 6c 6f 77 65 64 20 74 6f 20 70 65 72 73   allowed to pers
8d840 69 73 74 2e 20 41 20 70 72 6f 63 65 73 73 0a 20  ist. A process. 
8d850 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76   ** does not hav
8d860 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 52 45  e to obtain a RE
8d870 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SERVED lock on t
8d880 68 65 20 77 61 79 20 74 6f 20 61 20 50 45 4e 44  he way to a PEND
8d890 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 54  ING lock..  ** T
8d8a0 68 69 73 20 70 72 6f 70 65 72 74 79 20 69 73 20  his property is 
8d8b0 75 73 65 64 20 62 79 20 74 68 65 20 61 6c 67 6f  used by the algo
8d8c0 72 69 74 68 6d 20 66 6f 72 20 72 6f 6c 6c 69 6e  rithm for rollin
8d8d0 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c  g back a journal
8d8e0 20 66 69 6c 65 0a 20 20 2a 2a 20 61 66 74 65 72   file.  ** after
8d8f0 20 61 20 63 72 61 73 68 2e 0a 20 20 2a 2a 0a 20   a crash..  **. 
8d900 20 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45   ** An EXCLUSIVE
8d910 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 6e 65 64 20   lock, obtained 
8d920 61 66 74 65 72 20 61 20 50 45 4e 44 49 4e 47 20  after a PENDING 
8d930 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 69 73  lock is held, is
8d940 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65  .  ** implemente
8d950 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61  d by obtaining a
8d960 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74   write-lock on t
8d970 68 65 20 65 6e 74 69 72 65 20 27 73 68 61 72 65  he entire 'share
8d980 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67  d byte.  ** rang
8d990 65 27 2e 20 53 69 6e 63 65 20 61 6c 6c 20 6f 74  e'. Since all ot
8d9a0 68 65 72 20 6c 6f 63 6b 73 20 72 65 71 75 69 72  her locks requir
8d9b0 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  e a read-lock on
8d9c0 20 6f 6e 65 20 6f 66 20 74 68 65 20 62 79 74 65   one of the byte
8d9d0 73 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 74 68  s.  ** within th
8d9e0 69 73 20 72 61 6e 67 65 2c 20 74 68 69 73 20 65  is range, this e
8d9f0 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6f  nsures that no o
8da00 74 68 65 72 20 6c 6f 63 6b 73 20 61 72 65 20 68  ther locks are h
8da10 65 6c 64 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  eld on the.  ** 
8da20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 2a 2a 0a  database. .  **.
8da30 20 20 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20    ** The reason 
8da40 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61  a single byte ca
8da50 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 69 6e 73  nnot be used ins
8da60 74 65 61 64 20 6f 66 20 74 68 65 20 27 73 68 61  tead of the 'sha
8da70 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61  red byte.  ** ra
8da80 6e 67 65 27 20 69 73 20 74 68 61 74 20 73 6f 6d  nge' is that som
8da90 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69  e versions of wi
8daa0 6e 64 6f 77 73 20 64 6f 20 6e 6f 74 20 73 75 70  ndows do not sup
8dab0 70 6f 72 74 20 72 65 61 64 2d 6c 6f 63 6b 73 2e  port read-locks.
8dac0 20 42 79 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67   By.  ** locking
8dad0 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66   a random byte f
8dae0 72 6f 6d 20 61 20 72 61 6e 67 65 2c 20 63 6f 6e  rom a range, con
8daf0 63 75 72 72 65 6e 74 20 53 48 41 52 45 44 20 6c  current SHARED l
8db00 6f 63 6b 73 20 6d 61 79 20 65 78 69 73 74 0a 20  ocks may exist. 
8db10 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 65 20   ** even if the 
8db20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76  locking primitiv
8db30 65 20 75 73 65 64 20 69 73 20 61 6c 77 61 79 73  e used is always
8db40 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2e 0a 20   a write-lock.. 
8db50 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   */.  int rc = S
8db60 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78  QLITE_OK;.  unix
8db70 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
8db80 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73  nixFile*)id;.  s
8db90 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a  truct lockInfo *
8dba0 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70  pLock = pFile->p
8dbb0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66  Lock;.  struct f
8dbc0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74  lock lock;.  int
8dbd0 20 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   s;..  assert( p
8dbe0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
8dbf0 45 37 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25  E7("LOCK    %d %
8dc00 73 20 77 61 73 20 25 73 28 25 73 2c 25 64 29 20  s was %s(%s,%d) 
8dc10 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  pid=%d\n", pFile
8dc20 2d 3e 68 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74  ->h,.      lockt
8dc30 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65  ypeName(locktype
8dc40 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28  ), locktypeName(
8dc50 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29  pFile->locktype)
8dc60 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65  ,.      locktype
8dc70 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  Name(pLock->lock
8dc80 74 79 70 65 29 2c 20 70 4c 6f 63 6b 2d 3e 63 6e  type), pLock->cn
8dc90 74 20 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a  t , getpid());..
8dca0 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
8dcb0 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20   already a lock 
8dcc0 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20  of this type or 
8dcd0 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65  more restrictive
8dce0 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69   on the.  ** uni
8dcf0 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e  xFile, do nothin
8dd00 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65  g. Don't use the
8dd10 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20   end_lock: exit 
8dd20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 65 6e  path, as.  ** en
8dd30 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27  terMutex() hasn'
8dd40 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65  t been called ye
8dd50 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46  t..  */.  if( pF
8dd60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c  ile->locktype>=l
8dd70 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f  ocktype ){.    O
8dd80 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20  STRACE3("LOCK   
8dd90 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61   %d %s ok (alrea
8dda0 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69  dy held)\n", pFi
8ddb0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
8ddc0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
8ddd0 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20  locktype));.    
8dde0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
8ddf0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
8de00 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e   sure the lockin
8de10 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f  g sequence is co
8de20 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73  rrect.  */.  ass
8de30 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ert( pFile->lock
8de40 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c  type!=NO_LOCK ||
8de50 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
8de60 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  D_LOCK );.  asse
8de70 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45  rt( locktype!=PE
8de80 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20  NDING_LOCK );.  
8de90 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
8dea0 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  !=RESERVED_LOCK 
8deb0 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  || pFile->lockty
8dec0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
8ded0 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75  );..  /* This mu
8dee0 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 62 65  tex is needed be
8def0 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f  cause pFile->pLo
8df00 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72  ck is shared acr
8df10 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f  oss threads.  */
8df20 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b  .  enterMutex();
8df30 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
8df40 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
8df50 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 69  ead owns the pFi
8df60 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  le..  */.  rc = 
8df70 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69  transferOwnershi
8df80 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  p(pFile);.  if( 
8df90 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
8dfa0 0a 20 20 20 20 6c 65 61 76 65 4d 75 74 65 78 28  .    leaveMutex(
8dfb0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
8dfc0 3b 0a 20 20 7d 0a 20 20 70 4c 6f 63 6b 20 3d 20  ;.  }.  pLock = 
8dfd0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20  pFile->pLock;.. 
8dfe0 20 2f 2a 20 49 66 20 73 6f 6d 65 20 74 68 72 65   /* If some thre
8dff0 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49  ad using this PI
8e000 44 20 68 61 73 20 61 20 6c 6f 63 6b 20 76 69 61  D has a lock via
8e010 20 61 20 64 69 66 66 65 72 65 6e 74 20 75 6e 69   a different uni
8e020 78 46 69 6c 65 2a 0a 20 20 2a 2a 20 68 61 6e 64  xFile*.  ** hand
8e030 6c 65 20 74 68 61 74 20 70 72 65 63 6c 75 64 65  le that preclude
8e040 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  s the requested 
8e050 6c 6f 63 6b 2c 20 72 65 74 75 72 6e 20 42 55 53  lock, return BUS
8e060 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  Y..  */.  if( (p
8e070 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d  File->locktype!=
8e080 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20  pLock->locktype 
8e090 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 28 70  && .          (p
8e0a0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  Lock->locktype>=
8e0b0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 7c 7c 20  PENDING_LOCK || 
8e0c0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
8e0d0 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20  LOCK)).  ){.    
8e0e0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
8e0f0 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c  ;.    goto end_l
8e100 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ock;.  }..  /* I
8e110 66 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20  f a SHARED lock 
8e120 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 61 6e  is requested, an
8e130 64 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73  d some thread us
8e140 69 6e 67 20 74 68 69 73 20 50 49 44 20 61 6c 72  ing this PID alr
8e150 65 61 64 79 0a 20 20 2a 2a 20 68 61 73 20 61 20  eady.  ** has a 
8e160 53 48 41 52 45 44 20 6f 72 20 52 45 53 45 52 56  SHARED or RESERV
8e170 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 69 6e  ED lock, then in
8e180 63 72 65 6d 65 6e 74 20 72 65 66 65 72 65 6e 63  crement referenc
8e190 65 20 63 6f 75 6e 74 73 20 61 6e 64 0a 20 20 2a  e counts and.  *
8e1a0 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * return SQLITE_
8e1b0 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  OK..  */.  if( l
8e1c0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
8e1d0 4c 4f 43 4b 20 26 26 20 0a 20 20 20 20 20 20 28  LOCK && .      (
8e1e0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pLock->locktype=
8e1f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20  =SHARED_LOCK || 
8e200 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pLock->locktype=
8e210 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20  =RESERVED_LOCK) 
8e220 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c  ){.    assert( l
8e230 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
8e240 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65  LOCK );.    asse
8e250 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
8e260 79 70 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  ype==0 );.    as
8e270 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74  sert( pLock->cnt
8e280 3e 30 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  >0 );.    pFile-
8e290 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  >locktype = SHAR
8e2a0 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f  ED_LOCK;.    pLo
8e2b0 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 70  ck->cnt++;.    p
8e2c0 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f  File->pOpen->nLo
8e2d0 63 6b 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ck++;.    goto e
8e2e0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20  nd_lock;.  }..  
8e2f0 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b  lock.l_len = 1L;
8e300 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63  ..  lock.l_whenc
8e310 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 0a 20  e = SEEK_SET;.. 
8e320 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f   /* A PENDING lo
8e330 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66  ck is needed bef
8e340 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20  ore acquiring a 
8e350 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20  SHARED lock and 
8e360 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75  before.  ** acqu
8e370 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49  iring an EXCLUSI
8e380 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68  VE lock.  For th
8e390 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74  e SHARED lock, t
8e3a0 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a  he PENDING will.
8e3b0 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64    ** be released
8e3c0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  ..  */.  if( loc
8e3d0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
8e3e0 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f  CK .      || (lo
8e3f0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
8e400 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d  E_LOCK && pFile-
8e410 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e  >locktype<PENDIN
8e420 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20  G_LOCK).  ){.   
8e430 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 28   lock.l_type = (
8e440 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
8e450 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43 4b 3a 46 5f  _LOCK?F_RDLCK:F_
8e460 57 52 4c 43 4b 29 3b 0a 20 20 20 20 6c 6f 63 6b  WRLCK);.    lock
8e470 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49  .l_start = PENDI
8e480 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 73 20 3d  NG_BYTE;.    s =
8e490 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
8e4a0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
8e4b0 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31  ;.    if( s==(-1
8e4c0 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
8e4d0 28 65 72 72 6e 6f 3d 3d 45 49 4e 56 41 4c 29 20  (errno==EINVAL) 
8e4e0 3f 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 3a  ? SQLITE_NOLFS :
8e4f0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
8e500 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63      goto end_loc
8e510 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20  k;.    }.  }... 
8e520 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67   /* If control g
8e530 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e  ets to this poin
8e540 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79  t, then actually
8e550 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61   go ahead and ma
8e560 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e  ke.  ** operatin
8e570 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66  g system calls f
8e580 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64  or the specified
8e590 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66   lock..  */.  if
8e5a0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
8e5b0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61  ED_LOCK ){.    a
8e5c0 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e  ssert( pLock->cn
8e5d0 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t==0 );.    asse
8e5e0 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74  rt( pLock->lockt
8e5f0 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 2f  ype==0 );..    /
8e600 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72 65  * Now get the re
8e610 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c  ad-lock */.    l
8e620 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48  ock.l_start = SH
8e630 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
8e640 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41  lock.l_len = SHA
8e650 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 73 20  RED_SIZE;.    s 
8e660 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68  = fcntl(pFile->h
8e670 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
8e680 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20  );..    /* Drop 
8e690 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45  the temporary PE
8e6a0 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20  NDING lock */.  
8e6b0 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
8e6c0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20   PENDING_BYTE;. 
8e6d0 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20     lock.l_len = 
8e6e0 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74  1L;.    lock.l_t
8e6f0 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20  ype = F_UNLCK;. 
8e700 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46 69     if( fcntl(pFi
8e710 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  le->h, F_SETLK, 
8e720 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20  &lock)!=0 ){.   
8e730 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
8e740 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 20 2f 2a  OERR_UNLOCK;  /*
8e750 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76   This should nev
8e760 65 72 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 20  er happen */.   
8e770 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b     goto end_lock
8e780 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
8e790 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  s==(-1) ){.     
8e7a0 20 72 63 20 3d 20 28 65 72 72 6e 6f 3d 3d 45 49   rc = (errno==EI
8e7b0 4e 56 41 4c 29 20 3f 20 53 51 4c 49 54 45 5f 4e  NVAL) ? SQLITE_N
8e7c0 4f 4c 46 53 20 3a 20 53 51 4c 49 54 45 5f 42 55  OLFS : SQLITE_BU
8e7d0 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  SY;.    }else{. 
8e7e0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b       pFile->lock
8e7f0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f  type = SHARED_LO
8e800 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  CK;.      pFile-
8e810 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b  >pOpen->nLock++;
8e820 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e  .      pLock->cn
8e830 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  t = 1;.    }.  }
8e840 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70  else if( locktyp
8e850 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
8e860 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e  K && pLock->cnt>
8e870 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61  1 ){.    /* We a
8e880 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 6e  re trying for an
8e890 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
8e8a0 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65  but another thre
8e8b0 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20 2a  ad in this.    *
8e8c0 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 69  * same process i
8e8d0 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20  s still holding 
8e8e0 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 2a  a shared lock. *
8e8f0 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  /.    rc = SQLIT
8e900 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b  E_BUSY;.  }else{
8e910 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75  .    /* The requ
8e920 65 73 74 20 77 61 73 20 66 6f 72 20 61 20 52 45  est was for a RE
8e930 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53  SERVED or EXCLUS
8e940 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73  IVE lock.  It is
8e950 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20  .    ** assumed 
8e960 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 20  that there is a 
8e970 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 65  SHARED or greate
8e980 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69  r lock on the fi
8e990 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  le.    ** alread
8e9a0 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  y..    */.    as
8e9b0 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e  sert( 0!=pFile->
8e9c0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20  locktype );.    
8e9d0 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  lock.l_type = F_
8e9e0 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69 74 63  WRLCK;.    switc
8e9f0 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  h( locktype ){. 
8ea00 20 20 20 20 20 63 61 73 65 20 52 45 53 45 52 56       case RESERV
8ea10 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 20  ED_LOCK:.       
8ea20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
8ea30 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20  RESERVED_BYTE;. 
8ea40 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
8ea50 20 20 20 20 63 61 73 65 20 45 58 43 4c 55 53 49      case EXCLUSI
8ea60 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 20  VE_LOCK:.       
8ea70 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
8ea80 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20  SHARED_FIRST;.  
8ea90 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e        lock.l_len
8eaa0 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a   = SHARED_SIZE;.
8eab0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
8eac0 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20       default:.  
8ead0 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29 3b        assert(0);
8eae0 0a 20 20 20 20 7d 0a 20 20 20 20 73 20 3d 20 66  .    }.    s = f
8eaf0 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46  cntl(pFile->h, F
8eb00 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a  _SETLK, &lock);.
8eb10 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20      if( s==(-1) 
8eb20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 28 65  ){.      rc = (e
8eb30 72 72 6e 6f 3d 3d 45 49 4e 56 41 4c 29 20 3f 20  rrno==EINVAL) ? 
8eb40 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 3a 20 53  SQLITE_NOLFS : S
8eb50 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
8eb60 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 72  }.  }.  .  if( r
8eb70 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
8eb80 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
8eb90 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
8eba0 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74      pLock->lockt
8ebb0 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
8ebc0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b    }else if( lock
8ebd0 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
8ebe0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c  LOCK ){.    pFil
8ebf0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45  e->locktype = PE
8ec00 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20  NDING_LOCK;.    
8ec10 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20  pLock->locktype 
8ec20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a  = PENDING_LOCK;.
8ec30 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20    }..end_lock:. 
8ec40 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20   leaveMutex();. 
8ec50 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20   OSTRACE4("LOCK 
8ec60 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20     %d %s %s\n", 
8ec70 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
8ec80 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29  peName(locktype)
8ec90 2c 20 0a 20 20 20 20 20 20 72 63 3d 3d 53 51 4c  , .      rc==SQL
8eca0 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20  ITE_OK ? "ok" : 
8ecb0 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74  "failed");.  ret
8ecc0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
8ecd0 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
8ece0 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
8ecf0 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
8ed00 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20  e to locktype.  
8ed10 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74  locktype.** must
8ed20 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
8ed30 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
8ed40 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
8ed50 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
8ed60 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
8ed70 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
8ed80 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
8ed90 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
8eda0 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
8edb0 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
8edc0 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
8edd0 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71  nt unixUnlock(sq
8ede0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
8edf0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20  int locktype){. 
8ee00 20 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f   struct lockInfo
8ee10 20 2a 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63   *pLock;.  struc
8ee20 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20  t flock lock;.  
8ee30 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
8ee40 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  OK;.  unixFile *
8ee50 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
8ee60 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74  e*)id;..  assert
8ee70 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54  ( pFile );.  OST
8ee80 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 25  RACE7("UNLOCK  %
8ee90 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c 25  d %d was %d(%d,%
8eea0 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  d) pid=%d\n", pF
8eeb0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
8eec0 2c 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ,.      pFile->l
8eed0 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
8eee0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pLock->locktype,
8eef0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63   pFile->pLock->c
8ef00 6e 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a  nt, getpid());..
8ef10 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
8ef20 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
8ef30 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
8ef40 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79  locktype<=lockty
8ef50 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  pe ){.    return
8ef60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
8ef70 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45    if( CHECK_THRE
8ef80 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20  ADID(pFile) ){. 
8ef90 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
8efa0 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 65  _MISUSE;.  }.  e
8efb0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70  nterMutex();.  p
8efc0 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c  Lock = pFile->pL
8efd0 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ock;.  assert( p
8efe0 4c 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 29 3b 0a  Lock->cnt!=0 );.
8eff0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
8f000 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43  ktype>SHARED_LOC
8f010 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  K ){.    assert(
8f020 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
8f030 3d 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ==pFile->locktyp
8f040 65 20 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63  e );.    if( loc
8f050 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
8f060 43 4b 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b  CK ){.      lock
8f070 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43  .l_type = F_RDLC
8f080 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  K;.      lock.l_
8f090 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45  whence = SEEK_SE
8f0a0 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  T;.      lock.l_
8f0b0 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46  start = SHARED_F
8f0c0 49 52 53 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b  IRST;.      lock
8f0d0 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f  .l_len = SHARED_
8f0e0 53 49 5a 45 3b 0a 20 20 20 20 20 20 69 66 28 20  SIZE;.      if( 
8f0f0 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
8f100 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3d  F_SETLK, &lock)=
8f110 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  =(-1) ){.       
8f120 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20   /* This should 
8f130 6e 65 76 65 72 20 68 61 70 70 65 6e 20 2a 2f 0a  never happen */.
8f140 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
8f150 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b  ITE_IOERR_RDLOCK
8f160 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
8f170 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
8f180 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c  = F_UNLCK;.    l
8f190 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53  ock.l_whence = S
8f1a0 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63  EEK_SET;.    loc
8f1b0 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44  k.l_start = PEND
8f1c0 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f  ING_BYTE;.    lo
8f1d0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20  ck.l_len = 2L;  
8f1e0 61 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47 5f  assert( PENDING_
8f1f0 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 44  BYTE+1==RESERVED
8f200 5f 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66 28  _BYTE );.    if(
8f210 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
8f220 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
8f230 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  !=(-1) ){.      
8f240 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20  pLock->locktype 
8f250 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
8f260 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
8f270 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
8f280 52 5f 55 4e 4c 4f 43 4b 3b 20 20 2f 2a 20 54 68  R_UNLOCK;  /* Th
8f290 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  is should never 
8f2a0 68 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20 7d 0a  happen */.    }.
8f2b0 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79    }.  if( lockty
8f2c0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20  pe==NO_LOCK ){. 
8f2d0 20 20 20 73 74 72 75 63 74 20 6f 70 65 6e 43 6e     struct openCn
8f2e0 74 20 2a 70 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f  t *pOpen;..    /
8f2f0 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
8f300 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e  shared lock coun
8f310 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74 68  ter.  Release th
8f320 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a  e lock using an.
8f330 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f      ** OS call o
8f340 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68 72  nly when all thr
8f350 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61 6d  eads in this sam
8f360 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20 72  e process have r
8f370 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20 74  eleased.    ** t
8f380 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a  he lock..    */.
8f390 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d      pLock->cnt--
8f3a0 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d  ;.    if( pLock-
8f3b0 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  >cnt==0 ){.     
8f3c0 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46   lock.l_type = F
8f3d0 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f  _UNLCK;.      lo
8f3e0 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45  ck.l_whence = SE
8f3f0 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f  EK_SET;.      lo
8f400 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c 6f 63  ck.l_start = loc
8f410 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20  k.l_len = 0L;.  
8f420 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46      if( fcntl(pF
8f430 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c  ile->h, F_SETLK,
8f440 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b   &lock)!=(-1) ){
8f450 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  .        pLock->
8f460 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f  locktype = NO_LO
8f470 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  CK;.      }else{
8f480 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
8f490 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
8f4a0 4b 3b 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75  K;  /* This shou
8f4b0 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20  ld never happen 
8f4c0 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  */.      }.    }
8f4d0 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65  ..    /* Decreme
8f4e0 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  nt the count of 
8f4f0 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68  locks against th
8f500 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57  is same file.  W
8f510 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63  hen the.    ** c
8f520 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72  ount reaches zer
8f530 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68  o, close any oth
8f540 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  er file descript
8f550 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a  ors whose close.
8f560 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72      ** was defer
8f570 72 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f  red because of o
8f580 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73  utstanding locks
8f590 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70  ..    */.    pOp
8f5a0 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  en = pFile->pOpe
8f5b0 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c  n;.    pOpen->nL
8f5c0 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72  ock--;.    asser
8f5d0 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e  t( pOpen->nLock>
8f5e0 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  =0 );.    if( pO
8f5f0 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26  pen->nLock==0 &&
8f600 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67   pOpen->nPending
8f610 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  >0 ){.      int 
8f620 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  i;.      for(i=0
8f630 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64  ; i<pOpen->nPend
8f640 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ing; i++){.     
8f650 20 20 20 63 6c 6f 73 65 28 70 4f 70 65 6e 2d 3e     close(pOpen->
8f660 61 50 65 6e 64 69 6e 67 5b 69 5d 29 3b 0a 20 20  aPending[i]);.  
8f670 20 20 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65      }.      free
8f680 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67  (pOpen->aPending
8f690 29 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  );.      pOpen->
8f6a0 6e 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20  nPending = 0;.  
8f6b0 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64      pOpen->aPend
8f6c0 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ing = 0;.    }. 
8f6d0 20 7d 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28   }.  leaveMutex(
8f6e0 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  );.  pFile->lock
8f6f0 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
8f700 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
8f710 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66  ./*.** Close a f
8f720 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
8f730 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c  nt unixClose(sql
8f740 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a  ite3_file *id){.
8f750 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
8f760 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29  e = (unixFile *)
8f770 69 64 3b 0a 20 20 69 66 28 20 21 70 46 69 6c 65  id;.  if( !pFile
8f780 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
8f790 5f 4f 4b 3b 0a 20 20 75 6e 69 78 55 6e 6c 6f 63  _OK;.  unixUnloc
8f7a0 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  k(id, NO_LOCK);.
8f7b0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 64 69 72    if( pFile->dir
8f7c0 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 70 46  fd>=0 ) close(pF
8f7d0 69 6c 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20 70  ile->dirfd);.  p
8f7e0 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31  File->dirfd = -1
8f7f0 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29  ;.  enterMutex()
8f800 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  ;..  if( pFile->
8f810 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a  pOpen->nLock ){.
8f820 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
8f830 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  are outstanding 
8f840 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63  locks, do not ac
8f850 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65  tually close the
8f860 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 2a   file just.    *
8f870 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74 68  * yet because th
8f880 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74  at would clear t
8f890 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73  hose locks.  Ins
8f8a0 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 69  tead, add the fi
8f8b0 6c 65 0a 20 20 20 20 2a 2a 20 64 65 73 63 72 69  le.    ** descri
8f8c0 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61  ptor to pOpen->a
8f8d0 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c  Pending.  It wil
8f8e0 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
8f8f0 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20  ly closed when. 
8f900 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c     ** the last l
8f910 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a  ock is cleared..
8f920 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a      */.    int *
8f930 61 4e 65 77 3b 0a 20 20 20 20 73 74 72 75 63 74  aNew;.    struct
8f940 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20   openCnt *pOpen 
8f950 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a  = pFile->pOpen;.
8f960 20 20 20 20 61 4e 65 77 20 3d 20 72 65 61 6c 6c      aNew = reall
8f970 6f 63 28 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64  oc( pOpen->aPend
8f980 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e 6e 50 65  ing, (pOpen->nPe
8f990 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65 6f 66 28  nding+1)*sizeof(
8f9a0 69 6e 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20  int) );.    if( 
8f9b0 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  aNew==0 ){.     
8f9c0 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20   /* If a malloc 
8f9d0 66 61 69 6c 73 2c 20 6a 75 73 74 20 6c 65 61 6b  fails, just leak
8f9e0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
8f9f0 70 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  ptor */.    }els
8fa00 65 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  e{.      pOpen->
8fa10 61 50 65 6e 64 69 6e 67 20 3d 20 61 4e 65 77 3b  aPending = aNew;
8fa20 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50  .      pOpen->aP
8fa30 65 6e 64 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e 50  ending[pOpen->nP
8fa40 65 6e 64 69 6e 67 5d 20 3d 20 70 46 69 6c 65 2d  ending] = pFile-
8fa50 3e 68 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  >h;.      pOpen-
8fa60 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20  >nPending++;.   
8fa70 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
8fa80 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20  /* There are no 
8fa90 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b  outstanding lock
8faa0 73 20 73 6f 20 77 65 20 63 61 6e 20 63 6c 6f 73  s so we can clos
8fab0 65 20 74 68 65 20 66 69 6c 65 20 69 6d 6d 65 64  e the file immed
8fac0 69 61 74 65 6c 79 20 2a 2f 0a 20 20 20 20 63 6c  iately */.    cl
8fad0 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20  ose(pFile->h);. 
8fae0 20 7d 0a 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b   }.  releaseLock
8faf0 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63  Info(pFile->pLoc
8fb00 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 4f 70 65  k);.  releaseOpe
8fb10 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65  nCnt(pFile->pOpe
8fb20 6e 29 3b 0a 0a 20 20 6c 65 61 76 65 4d 75 74 65  n);..  leaveMute
8fb30 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28  x();.  OSTRACE2(
8fb40 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22  "CLOSE   %-3d\n"
8fb50 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 4f  , pFile->h);.  O
8fb60 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a  penCounter(-1);.
8fb70 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20    memset(pFile, 
8fb80 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69  0, sizeof(unixFi
8fb90 6c 65 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  le));.  return S
8fba0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69  QLITE_OK;.}...#i
8fbb0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
8fbc0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
8fbd0 0a 23 70 72 61 67 6d 61 20 6d 61 72 6b 20 41 46  .#pragma mark AF
8fbe0 50 20 53 75 70 70 6f 72 74 0a 0a 2f 2a 0a 20 2a  P Support../*. *
8fbf0 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67  * The afpLocking
8fc00 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72  Context structur
8fc10 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61  e contains all a
8fc20 66 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63  fp lock specific
8fc30 20 73 74 61 74 65 0a 20 2a 2f 0a 74 79 70 65 64   state. */.typed
8fc40 65 66 20 73 74 72 75 63 74 20 61 66 70 4c 6f 63  ef struct afpLoc
8fc50 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 4c  kingContext afpL
8fc60 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73  ockingContext;.s
8fc70 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67  truct afpLocking
8fc80 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73 69  Context {.  unsi
8fc90 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73  gned long long s
8fca0 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b 0a 20  haredLockByte;. 
8fcb0 20 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 3b   char *filePath;
8fcc0 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 42 79 74 65  .};..struct Byte
8fcd0 52 61 6e 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20  RangeLockPB2.{. 
8fce0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
8fcf0 6f 6e 67 20 6f 66 66 73 65 74 3b 20 20 20 20 20  ong offset;     
8fd00 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20     /* offset to 
8fd10 66 69 72 73 74 20 62 79 74 65 20 74 6f 20 6c 6f  first byte to lo
8fd20 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ck */.  unsigned
8fd30 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74   long long lengt
8fd40 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 62 72  h;        /* nbr
8fd50 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63   of bytes to loc
8fd60 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  k */.  unsigned 
8fd70 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e  long long retRan
8fd80 67 65 53 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20  geStart; /* nbr 
8fd90 6f 66 20 31 73 74 20 62 79 74 65 20 6c 6f 63 6b  of 1st byte lock
8fda0 65 64 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  ed if successful
8fdb0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
8fdc0 68 61 72 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20  har unLockFlag; 
8fdd0 20 20 20 20 20 20 20 20 2f 2a 20 31 20 3d 20 75          /* 1 = u
8fde0 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20  nlock, 0 = lock 
8fdf0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
8fe00 61 72 20 73 74 61 72 74 45 6e 64 46 6c 61 67 3b  ar startEndFlag;
8fe10 20 20 20 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20         /* 1=rel 
8fe20 74 6f 20 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20  to end of fork, 
8fe30 30 3d 72 65 6c 20 74 6f 20 73 74 61 72 74 20 2a  0=rel to start *
8fe40 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20  /.  int fd;     
8fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8fe60 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65        /* file de
8fe70 73 63 20 74 6f 20 61 73 73 6f 63 20 74 68 69 73  sc to assoc this
8fe80 20 6c 6f 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b   lock with */.};
8fe90 0a 0a 23 64 65 66 69 6e 65 20 61 66 70 66 73 42  ..#define afpfsB
8fea0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43  yteRangeLock2FSC
8feb0 54 4c 20 20 20 20 20 20 20 20 5f 49 4f 57 52 28  TL        _IOWR(
8fec0 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20  'z', 23, struct 
8fed0 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
8fee0 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e  )../* .** Return
8fef0 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 31   0 on success, 1
8ff00 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 54 6f   on failure.  To
8ff10 20 6d 61 74 63 68 20 74 68 65 20 62 65 68 61 76   match the behav
8ff20 69 6f 72 20 6f 66 20 74 68 65 20 0a 2a 2a 20 6e  ior of the .** n
8ff30 6f 72 6d 61 6c 20 70 6f 73 69 78 20 66 69 6c 65  ormal posix file
8ff40 20 6c 6f 63 6b 69 6e 67 20 28 75 73 65 64 20 69   locking (used i
8ff50 6e 20 75 6e 69 78 4c 6f 63 6b 20 66 6f 72 20 65  n unixLock for e
8ff60 78 61 6d 70 6c 65 29 2c 20 77 65 20 73 68 6f 75  xample), we shou
8ff70 6c 64 20 0a 2a 2a 20 70 72 6f 76 69 64 65 20 27  ld .** provide '
8ff80 72 69 63 68 65 72 27 20 72 65 74 75 72 6e 20 63  richer' return c
8ff90 6f 64 65 73 20 2d 20 73 70 65 63 69 66 69 63 61  odes - specifica
8ffa0 6c 6c 79 20 74 6f 20 64 69 66 66 65 72 65 6e 74  lly to different
8ffb0 69 61 74 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20  iate between.** 
8ffc0 27 66 69 6c 65 20 62 75 73 79 27 20 61 6e 64 20  'file busy' and 
8ffd0 27 66 69 6c 65 20 73 79 73 74 65 6d 20 65 72 72  'file system err
8ffe0 6f 72 27 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a  or' results..*/.
8fff0 73 74 61 74 69 63 20 69 6e 74 20 5f 41 46 50 46  static int _AFPF
90000 53 53 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73  SSetLock(.  cons
90010 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 0a 20  t char *path, . 
90020 20 69 6e 74 20 66 64 2c 20 0a 20 20 75 6e 73 69   int fd, .  unsi
90030 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f  gned long long o
90040 66 66 73 65 74 2c 20 0a 20 20 75 6e 73 69 67 6e  ffset, .  unsign
90050 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e  ed long long len
90060 67 74 68 2c 20 0a 20 20 69 6e 74 20 73 65 74 4c  gth, .  int setL
90070 6f 63 6b 46 6c 61 67 0a 29 7b 0a 20 20 73 74 72  ockFlag.){.  str
90080 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63  uct ByteRangeLoc
90090 6b 50 42 32 20 20 20 20 20 20 20 70 62 3b 0a 20  kPB2       pb;. 
900a0 20 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20   int            
900b0 20 20 20 20 20 20 20 20 20 65 72 72 3b 0a 20 20           err;.  
900c0 0a 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67  .  pb.unLockFlag
900d0 20 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f   = setLockFlag ?
900e0 20 30 20 3a 20 31 3b 0a 20 20 70 62 2e 73 74 61   0 : 1;.  pb.sta
900f0 72 74 45 6e 64 46 6c 61 67 20 3d 20 30 3b 0a 20  rtEndFlag = 0;. 
90100 20 70 62 2e 6f 66 66 73 65 74 20 3d 20 6f 66 66   pb.offset = off
90110 73 65 74 3b 0a 20 20 70 62 2e 6c 65 6e 67 74 68  set;.  pb.length
90120 20 3d 20 6c 65 6e 67 74 68 3b 20 0a 20 20 70 62   = length; .  pb
90130 2e 66 64 20 3d 20 66 64 3b 0a 20 20 4f 53 54 52  .fd = fd;.  OSTR
90140 41 43 45 35 28 22 41 46 50 4c 4f 43 4b 20 73 65  ACE5("AFPLOCK se
90150 74 74 69 6e 67 20 6c 6f 63 6b 20 25 73 20 66 6f  tting lock %s fo
90160 72 20 25 64 20 69 6e 20 72 61 6e 67 65 20 25 6c  r %d in range %l
90170 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20  lx:%llx\n", .   
90180 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f   (setLockFlag?"O
90190 4e 22 3a 22 4f 46 46 22 29 2c 20 66 64 2c 20 6f  N":"OFF"), fd, o
901a0 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 3b 0a  ffset, length);.
901b0 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28 70 61    err = fsctl(pa
901c0 74 68 2c 20 61 66 70 66 73 42 79 74 65 52 61 6e  th, afpfsByteRan
901d0 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 26 70  geLock2FSCTL, &p
901e0 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 65 72  b, 0);.  if ( er
901f0 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 4f 53  r==-1 ) {.    OS
90200 54 52 41 43 45 34 28 22 41 46 50 4c 4f 43 4b 20  TRACE4("AFPLOCK 
90210 66 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c 28  failed to fsctl(
90220 29 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 2c  ) '%s' %d %s\n",
90230 20 70 61 74 68 2c 20 65 72 72 6e 6f 2c 20 0a 20   path, errno, . 
90240 20 20 20 20 20 73 74 72 65 72 72 6f 72 28 65 72       strerror(er
90250 72 6e 6f 29 29 3b 0a 20 20 20 20 72 65 74 75 72  rno));.    retur
90260 6e 20 31 3b 20 2f 2a 20 65 72 72 6f 72 20 2a 2f  n 1; /* error */
90270 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
90280 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a  return 0;.  }.}.
90290 0a 2f 2a 0a 20 2a 2a 20 54 68 69 73 20 72 6f 75  ./*. ** This rou
902a0 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
902b0 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
902c0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
902d0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 20 2a  the specified. *
902e0 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
902f0 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
90300 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
90310 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74  ock is held, ret
90320 75 72 6e 0a 20 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f  urn. ** non-zero
90330 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 69  .  If the file i
90340 73 20 75 6e 6c 6f 63 6b 65 64 20 6f 72 20 68 6f  s unlocked or ho
90350 6c 64 73 20 6f 6e 6c 79 20 53 48 41 52 45 44 20  lds only SHARED 
90360 6c 6f 63 6b 73 2c 20 74 68 65 6e 0a 20 2a 2a 20  locks, then. ** 
90370 72 65 74 75 72 6e 20 7a 65 72 6f 2e 0a 20 2a 2f  return zero.. */
90380 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 55  .static int afpU
90390 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  nixCheckReserved
903a0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
903b0 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 20  e *id){.  int r 
903c0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  = 0;.  unixFile 
903d0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
903e0 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 61 73 73  le*)id;.  .  ass
903f0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 20 0a 20  ert( pFile ); . 
90400 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
90410 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61  xt *context = (a
90420 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
90430 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69   *) pFile->locki
90440 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20  ngContext;.  .  
90450 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68  /* Check if a th
90460 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f  read in this pro
90470 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20  cess holds such 
90480 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20  a lock */.  if( 
90490 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
904a0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
904b0 20 20 20 72 20 3d 20 31 3b 0a 20 20 7d 0a 20 20     r = 1;.  }.  
904c0 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  .  /* Otherwise 
904d0 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  see if some othe
904e0 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  r process holds 
904f0 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 20 28  it..   */.  if (
90500 20 21 72 20 29 20 7b 0a 20 20 20 20 2f 2a 20 6c   !r ) {.    /* l
90510 6f 63 6b 20 74 68 65 20 62 79 74 65 20 2a 2f 0a  ock the byte */.
90520 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 3d      int failed =
90530 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63   _AFPFSSetLock(c
90540 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68  ontext->filePath
90550 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45  , pFile->h, RESE
90560 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b  RVED_BYTE, 1,1);
90570 20 20 0a 20 20 20 20 69 66 20 28 66 61 69 6c 65    .    if (faile
90580 64 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66  d) {.      /* if
90590 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 67 65   we failed to ge
905a0 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 6e 20  t the lock then 
905b0 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 75 73  someone else mus
905c0 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 20 20  t have it */.   
905d0 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20 7d 20     r = 1;.    } 
905e0 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20  else {.      /* 
905f0 69 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20  if we succeeded 
90600 69 6e 20 74 61 6b 69 6e 67 20 74 68 65 20 72 65  in taking the re
90610 73 65 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c  served lock, unl
90620 6f 63 6b 20 69 74 20 74 6f 20 72 65 73 74 6f 72  ock it to restor
90630 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6f  e.      ** the o
90640 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f  riginal state */
90650 0a 20 20 20 20 20 20 5f 41 46 50 46 53 53 65 74  .      _AFPFSSet
90660 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69  Lock(context->fi
90670 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68  lePath, pFile->h
90680 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c  , RESERVED_BYTE,
90690 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20   1, 0);.    }.  
906a0 7d 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 45  }.  OSTRACE3("TE
906b0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
906c0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
906d0 29 3b 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 72  );.  .  return r
906e0 3b 0a 7d 0a 0a 2f 2a 20 41 46 50 2d 73 74 79 6c  ;.}../* AFP-styl
906f0 65 20 6c 6f 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77  e locking follow
90700 69 6e 67 20 74 68 65 20 62 65 68 61 76 69 6f 72  ing the behavior
90710 20 6f 66 20 75 6e 69 78 4c 6f 63 6b 2c 20 73 65   of unixLock, se
90720 65 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 20 0a  e the unixLock .
90730 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 6d  ** function comm
90740 65 6e 74 73 20 66 6f 72 20 64 65 74 61 69 6c 73  ents for details
90750 20 6f 66 20 6c 6f 63 6b 20 6d 61 6e 61 67 65 6d   of lock managem
90760 65 6e 74 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  ent. */.static i
90770 6e 74 20 61 66 70 55 6e 69 78 4c 6f 63 6b 28 73  nt afpUnixLock(s
90780 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
90790 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 0a 7b   int locktype).{
907a0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
907b0 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c  TE_OK;.  unixFil
907c0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
907d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c  File*)id;.  afpL
907e0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63  ockingContext *c
907f0 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63  ontext = (afpLoc
90800 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70  kingContext *) p
90810 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
90820 74 65 78 74 3b 0a 20 20 69 6e 74 20 67 6f 74 50  text;.  int gotP
90830 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a  endingLock = 0;.
90840 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
90850 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35  le );.  OSTRACE5
90860 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
90870 77 61 73 20 25 73 20 70 69 64 3d 25 64 5c 6e 22  was %s pid=%d\n"
90880 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
90890 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d       locktypeNam
908a0 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63  e(locktype), loc
908b0 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d  ktypeName(pFile-
908c0 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 67 65 74 70  >locktype), getp
908d0 69 64 28 29 29 3b 20 20 0a 20 20 2f 2a 20 49 66  id());  .  /* If
908e0 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64   there is alread
908f0 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73  y a lock of this
90900 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65   type or more re
90910 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65  strictive on the
90920 0a 20 20 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65  .    ** unixFile
90930 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f  , do nothing. Do
90940 6e 27 74 20 75 73 65 20 74 68 65 20 61 66 70 5f  n't use the afp_
90950 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70  end_lock: exit p
90960 61 74 68 2c 20 61 73 0a 20 20 20 20 2a 2a 20 65  ath, as.    ** e
90970 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e  nterMutex() hasn
90980 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79  't been called y
90990 65 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 69 66 28  et..    */.  if(
909a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
909b0 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  >=locktype ){.  
909c0 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b    OSTRACE3("LOCK
909d0 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c      %d %s ok (al
909e0 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20  ready held)\n", 
909f0 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
90a00 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d       locktypeNam
90a10 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20  e(locktype));.  
90a20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
90a30 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  OK;.  }..  /* Ma
90a40 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b  ke sure the lock
90a50 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20  ing sequence is 
90a60 63 6f 72 72 65 63 74 0a 20 20 20 20 2a 2f 0a 20  correct.    */. 
90a70 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
90a80 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
90a90 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  K || locktype==S
90aa0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
90ab0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
90ac0 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  !=PENDING_LOCK )
90ad0 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
90ae0 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c  type!=RESERVED_L
90af0 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f  OCK || pFile->lo
90b00 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
90b10 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54  OCK );.  .  /* T
90b20 68 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65  his mutex is nee
90b30 64 65 64 20 62 65 63 61 75 73 65 20 70 46 69 6c  ded because pFil
90b40 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72  e->pLock is shar
90b50 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64  ed across thread
90b60 73 0a 20 20 20 20 2a 2f 0a 20 20 65 6e 74 65 72  s.    */.  enter
90b70 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d  Mutex();..  /* M
90b80 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72  ake sure the cur
90b90 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73  rent thread owns
90ba0 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 20 20   the pFile..    
90bb0 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66  */.  rc = transf
90bc0 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c  erOwnership(pFil
90bd0 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  e);.  if( rc!=SQ
90be0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 6c  LITE_OK ){.    l
90bf0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20  eaveMutex();.   
90c00 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
90c10 20 20 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44      .  /* A PEND
90c20 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64  ING lock is need
90c30 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72  ed before acquir
90c40 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ing a SHARED loc
90c50 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 20  k and before.   
90c60 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e   ** acquiring an
90c70 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
90c80 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 44    For the SHARED
90c90 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49   lock, the PENDI
90ca0 4e 47 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62  NG will.    ** b
90cb0 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 20 20  e released..    
90cc0 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
90cd0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a  e==SHARED_LOCK .
90ce0 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79        || (lockty
90cf0 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
90d00 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK && pFile->loc
90d10 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f  ktype<PENDING_LO
90d20 43 4b 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  CK).      ){.   
90d30 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 5f 41   int failed = _A
90d40 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74  FPFSSetLock(cont
90d50 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70  ext->filePath, p
90d60 46 69 6c 65 2d 3e 68 2c 20 0a 20 20 20 20 20 20  File->h, .      
90d70 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c  PENDING_BYTE, 1,
90d80 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69   1);.    if (fai
90d90 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20  led) {.      rc 
90da0 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
90db0 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e       goto afp_en
90dc0 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20  d_lock;.    }.  
90dd0 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f 6e  }.  .  /* If con
90de0 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69  trol gets to thi
90df0 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63  s point, then ac
90e00 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20  tually go ahead 
90e10 61 6e 64 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20  and make.    ** 
90e20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
90e30 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73   calls for the s
90e40 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20  pecified lock.. 
90e50 20 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b     */.  if( lock
90e60 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
90e70 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c  K ){.    int lk,
90e80 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 69 6e 74   failed;.    int
90e90 20 74 72 69 65 73 20 3d 20 30 3b 0a 20 20 20 20   tries = 0;.    
90ea0 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20  .    /* Now get 
90eb0 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f  the read-lock */
90ec0 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61  .    /* note tha
90ed0 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66  t the quality of
90ee0 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20   the randomness 
90ef0 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74  doesn't matter t
90f00 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20  hat much */.    
90f10 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a  lk = random(); .
90f20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61      context->sha
90f30 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 6c  redLockByte = (l
90f40 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25  k & 0x7fffffff)%
90f50 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31  (SHARED_SIZE - 1
90f60 29 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20  );.    failed = 
90f70 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f  _AFPFSSetLock(co
90f80 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c  ntext->filePath,
90f90 20 70 46 69 6c 65 2d 3e 68 2c 20 0a 20 20 20 20   pFile->h, .    
90fa0 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63    SHARED_FIRST+c
90fb0 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f  ontext->sharedLo
90fc0 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20  ckByte, 1, 1);. 
90fd0 20 20 20 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20     .    /* Drop 
90fe0 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45  the temporary PE
90ff0 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20  NDING lock */.  
91000 20 20 69 66 20 28 5f 41 46 50 46 53 53 65 74 4c    if (_AFPFSSetL
91010 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c  ock(context->fil
91020 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c  ePath, pFile->h,
91030 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31   PENDING_BYTE, 1
91040 2c 20 30 29 29 20 7b 0a 20 20 20 20 20 20 72 63  , 0)) {.      rc
91050 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
91060 55 4e 4c 4f 43 4b 3b 20 20 2f 2a 20 54 68 69 73  UNLOCK;  /* This
91070 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61   should never ha
91080 70 70 65 6e 20 2a 2f 0a 20 20 20 20 20 20 67 6f  ppen */.      go
91090 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b  to afp_end_lock;
910a0 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20  .    }.    .    
910b0 69 66 28 20 66 61 69 6c 65 64 20 29 7b 0a 20 20  if( failed ){.  
910c0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
910d0 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 65  BUSY;.    } else
910e0 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e   {.      pFile->
910f0 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  locktype = SHARE
91100 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  D_LOCK;.    }.  
91110 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
91120 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f  e request was fo
91130 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  r a RESERVED or 
91140 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
91150 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73   It is.    ** as
91160 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65  sumed that there
91170 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20   is a SHARED or 
91180 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20  greater lock on 
91190 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
911a0 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a  already..    */.
911b0 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 3d      int failed =
911c0 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   0;.    assert( 
911d0 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  0!=pFile->lockty
911e0 70 65 20 29 3b 0a 20 20 20 20 69 66 20 28 6c 6f  pe );.    if (lo
911f0 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 52 56  cktype >= RESERV
91200 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65  ED_LOCK && pFile
91210 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 45 53  ->locktype < RES
91220 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20  ERVED_LOCK) {.  
91230 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65        /* Acquire
91240 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
91250 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c   */.        fail
91260 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  ed = _AFPFSSetLo
91270 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
91280 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  Path, pFile->h, 
91290 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31  RESERVED_BYTE, 1
912a0 2c 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ,1);.    }.    i
912b0 66 20 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f  f (!failed && lo
912c0 63 6b 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53  cktype == EXCLUS
912d0 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  IVE_LOCK) {.    
912e0 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20    /* Acquire an 
912f0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a  EXCLUSIVE lock *
91300 2f 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20  /.        .     
91310 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73   /* Remove the s
91320 68 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72  hared lock befor
91330 65 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e  e trying the ran
91340 67 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20  ge.  we'll need 
91350 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65  to .      ** ree
91360 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61  stablish the sha
91370 72 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63  red lock if we c
91380 61 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66  an't get the  af
91390 70 55 6e 69 78 55 6e 6c 6f 63 6b 0a 20 20 20 20  pUnixUnlock.    
913a0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 20 28 21    */.      if (!
913b0 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f  _AFPFSSetLock(co
913c0 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c  ntext->filePath,
913d0 20 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45   pFile->h, SHARE
913e0 44 5f 46 49 52 53 54 20 2b 0a 20 20 20 20 20 20  D_FIRST +.      
913f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
91400 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72     context->shar
91410 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 30  edLockByte, 1, 0
91420 29 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  )) {.        /* 
91430 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20 74 6f 20  now attemmpt to 
91440 67 65 74 20 74 68 65 20 65 78 63 6c 75 73 69 76  get the exclusiv
91450 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20 2a 2f 0a  e lock range */.
91460 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d          failed =
91470 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63   _AFPFSSetLock(c
91480 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68  ontext->filePath
91490 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  , pFile->h, SHAR
914a0 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20  ED_FIRST, .     
914b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
914c0 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44            SHARED
914d0 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20  _SIZE, 1);.     
914e0 20 20 20 69 66 20 28 66 61 69 6c 65 64 20 26 26     if (failed &&
914f0 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63   _AFPFSSetLock(c
91500 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68  ontext->filePath
91510 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  , pFile->h, SHAR
91520 45 44 5f 46 49 52 53 54 20 2b 0a 20 20 20 20 20  ED_FIRST +.     
91530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
91540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
91550 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f  ontext->sharedLo
91560 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 29 20 7b  ckByte, 1, 1)) {
91570 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
91580 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c  SQLITE_IOERR_RDL
91590 4f 43 4b 3b 20 2f 2a 20 74 68 69 73 20 73 68 6f  OCK; /* this sho
915a0 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
915b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
915c0 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
915d0 20 20 20 20 20 2f 2a 20 2a 2f 0a 20 20 20 20 20       /* */.     
915e0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
915f0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 2f 2a 20  OERR_UNLOCK; /* 
91600 74 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65  this should neve
91610 72 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20  r happen */.    
91620 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
91630 28 20 66 61 69 6c 65 64 20 26 26 20 72 63 20 3d  ( failed && rc =
91640 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 7b 0a 20 20  = SQLITE_OK){.  
91650 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
91660 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  BUSY;.    }.  }.
91670 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c    .  if( rc==SQL
91680 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46  ITE_OK ){.    pF
91690 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
916a0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73  locktype;.  }els
916b0 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d  e if( locktype==
916c0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
916d0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
916e0 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f  ktype = PENDING_
916f0 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70  LOCK;.  }.  .afp
91700 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 20 20 6c  _end_lock:.    l
91710 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
91720 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20  STRACE4("LOCK   
91730 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46   %d %s %s\n", pF
91740 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
91750 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20  Name(locktype), 
91760 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51  .         rc==SQ
91770 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a  LITE_OK ? "ok" :
91780 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65   "failed");.  re
91790 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 20  turn rc;.}../*. 
917a0 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63  ** Lower the loc
917b0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69  king level on fi
917c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46  le descriptor pF
917d0 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e  ile to locktype.
917e0 20 20 6c 6f 63 6b 74 79 70 65 0a 20 2a 2a 20 6d    locktype. ** m
917f0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
91800 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
91810 4c 4f 43 4b 2e 0a 20 2a 2a 0a 20 2a 2a 20 49 66  LOCK.. **. ** If
91820 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
91830 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
91840 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
91850 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
91860 0a 20 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  . ** the request
91870 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
91880 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
91890 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 2a 2f 0a 73  s a no-op.. */.s
918a0 74 61 74 69 63 20 69 6e 74 20 61 66 70 55 6e 69  tatic int afpUni
918b0 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  xUnlock(sqlite3_
918c0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
918d0 63 6b 74 79 70 65 29 20 7b 0a 20 20 73 74 72 75  cktype) {.  stru
918e0 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20  ct flock lock;. 
918f0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
91900 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
91910 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
91920 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63  le*)id;.  afpLoc
91930 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e  kingContext *con
91940 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69  text = (afpLocki
91950 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
91960 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
91970 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  xt;..  assert( p
91980 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
91990 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25  E5("UNLOCK  %d %
919a0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c  d was %d pid=%d\
919b0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
919c0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20  cktype,.        
919d0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
919e0 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 0a  , getpid());.  .
919f0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
91a00 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
91a10 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
91a20 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79  locktype<=lockty
91a30 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  pe ){.    return
91a40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
91a50 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45    if( CHECK_THRE
91a60 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20  ADID(pFile) ){. 
91a70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
91a80 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 65  _MISUSE;.  }.  e
91a90 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69  nterMutex();.  i
91aa0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
91ab0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  pe>SHARED_LOCK )
91ac0 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79  {.    if( lockty
91ad0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
91ae0 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 61 69  ){.      int fai
91af0 6c 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20  led = 0;..      
91b00 2f 2a 20 75 6e 6c 6f 63 6b 20 74 68 65 20 65 78  /* unlock the ex
91b10 63 6c 75 73 69 76 65 20 72 61 6e 67 65 20 2d 20  clusive range - 
91b20 74 68 65 6e 20 72 65 2d 65 73 74 61 62 6c 69 73  then re-establis
91b30 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  h the shared loc
91b40 6b 20 2a 2f 0a 20 20 20 20 20 20 69 66 20 28 70  k */.      if (p
91b50 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
91b60 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 20  EXCLUSIVE_LOCK) 
91b70 7b 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65 64  {.        failed
91b80 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b   = _AFPFSSetLock
91b90 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61  (context->filePa
91ba0 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 53 48  th, pFile->h, SH
91bb0 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20  ARED_FIRST, .   
91bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
91bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 48                SH
91be0 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20  ARED_SIZE, 0);. 
91bf0 20 20 20 20 20 20 20 69 66 20 28 21 66 61 69 6c         if (!fail
91c00 65 64 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ed) {.          
91c10 2f 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  /* successfully 
91c20 72 65 6d 6f 76 65 64 20 74 68 65 20 65 78 63 6c  removed the excl
91c30 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  usive lock */.  
91c40 20 20 20 20 20 20 20 20 69 66 20 28 5f 41 46 50          if (_AFP
91c50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  FSSetLock(contex
91c60 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69  t->filePath, pFi
91c70 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
91c80 52 53 54 2b 0a 20 20 20 20 20 20 20 20 20 20 20  RST+.           
91c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
91ca0 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64   context->shared
91cb0 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 29  LockByte, 1, 1))
91cc0 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f   {.            /
91cd0 2a 20 66 61 69 6c 65 64 20 74 6f 20 72 65 2d 65  * failed to re-e
91ce0 73 74 61 62 6c 69 73 68 20 6f 75 72 20 73 68 61  stablish our sha
91cf0 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  red lock */.    
91d00 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
91d10 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b  ITE_IOERR_RDLOCK
91d20 3b 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64  ; /* This should
91d30 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 2a 2f   never happen */
91d40 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
91d50 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20       } else {.  
91d60 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
91d70 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70  should never hap
91d80 70 65 6e 20 2d 20 66 61 69 6c 65 64 20 74 6f 20  pen - failed to 
91d90 75 6e 6c 6f 63 6b 20 74 68 65 20 65 78 63 6c 75  unlock the exclu
91da0 73 69 76 65 20 72 61 6e 67 65 20 2a 2f 0a 20 20  sive range */.  
91db0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
91dc0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
91dd0 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20  ;.        } .   
91de0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
91df0 66 20 28 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f  f (rc == SQLITE_
91e00 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  OK && pFile->loc
91e10 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c  ktype>=PENDING_L
91e20 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 69 66 20  OCK) {.      if 
91e30 28 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63  (_AFPFSSetLock(c
91e40 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68  ontext->filePath
91e50 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44  , pFile->h, PEND
91e60 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 29  ING_BYTE, 1, 0))
91e70 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 61 69  {.        /* fai
91e80 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 74  led to release t
91e90 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20  he pending lock 
91ea0 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  */.        rc = 
91eb0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
91ec0 4f 43 4b 3b 20 2f 2a 20 54 68 69 73 20 73 68 6f  OCK; /* This sho
91ed0 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
91ee0 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   */.      }.    
91ef0 7d 20 0a 20 20 20 20 69 66 20 28 72 63 20 3d 3d  } .    if (rc ==
91f00 20 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46   SQLITE_OK && pF
91f10 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52  ile->locktype>=R
91f20 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a  ESERVED_LOCK) {.
91f30 20 20 20 20 20 20 69 66 20 28 5f 41 46 50 46 53        if (_AFPFS
91f40 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d  SetLock(context-
91f50 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65  >filePath, pFile
91f60 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59  ->h, RESERVED_BY
91f70 54 45 2c 20 31 2c 20 30 29 29 20 7b 0a 20 20 20  TE, 1, 0)) {.   
91f80 20 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 74       /* failed t
91f90 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 72 65  o release the re
91fa0 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20  served lock */. 
91fb0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
91fc0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b  TE_IOERR_UNLOCK;
91fd0 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64    /* This should
91fe0 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 2a 2f   never happen */
91ff0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 0a  .      }.    } .
92000 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79    }.  if( lockty
92010 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20  pe==NO_LOCK ){. 
92020 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20     int failed = 
92030 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f  _AFPFSSetLock(co
92040 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c  ntext->filePath,
92050 20 70 46 69 6c 65 2d 3e 68 2c 20 0a 20 20 20 20   pFile->h, .    
92060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
92070 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45             SHARE
92080 44 5f 46 49 52 53 54 20 2b 20 63 6f 6e 74 65 78  D_FIRST + contex
92090 74 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  t->sharedLockByt
920a0 65 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66  e, 1, 0);.    if
920b0 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 20   (failed) {.    
920c0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
920d0 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 20 2f 2a 20  ERR_UNLOCK;  /* 
920e0 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65  This should neve
920f0 72 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20  r happen */.    
92100 7d 0a 20 20 7d 0a 20 20 69 66 20 28 72 63 20 3d  }.  }.  if (rc =
92110 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20  = SQLITE_OK).   
92120 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
92130 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 6c   = locktype;.  l
92140 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
92150 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
92160 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65   ** Close a file
92170 20 26 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73   & cleanup AFP s
92180 70 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20  pecific locking 
92190 63 6f 6e 74 65 78 74 20 0a 20 2a 2f 0a 73 74 61  context . */.sta
921a0 74 69 63 20 69 6e 74 20 61 66 70 55 6e 69 78 43  tic int afpUnixC
921b0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
921c0 65 20 2a 69 64 29 20 7b 0a 20 20 75 6e 69 78 46  e *id) {.  unixF
921d0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
921e0 69 78 46 69 6c 65 2a 29 70 49 64 3b 0a 0a 20 20  ixFile*)pId;..  
921f0 69 66 28 20 21 70 46 69 6c 65 20 29 20 72 65 74  if( !pFile ) ret
92200 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
92210 20 61 66 70 55 6e 69 78 55 6e 6c 6f 63 6b 28 2a   afpUnixUnlock(*
92220 70 49 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  pId, NO_LOCK);. 
92230 20 2f 2a 20 66 72 65 65 20 74 68 65 20 41 46 50   /* free the AFP
92240 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75   locking structu
92250 72 65 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c  re */.  if (pFil
92260 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
92270 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20 20 20  t != NULL) {.   
92280 20 69 66 20 28 28 28 61 66 70 4c 6f 63 6b 69 6e   if (((afpLockin
92290 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65  gContext *)pFile
922a0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
922b0 29 2d 3e 66 69 6c 65 50 61 74 68 20 21 3d 20 4e  )->filePath != N
922c0 55 4c 4c 29 0a 20 20 20 20 20 20 73 71 6c 69 74  ULL).      sqlit
922d0 65 33 5f 66 72 65 65 28 28 28 61 66 70 4c 6f 63  e3_free(((afpLoc
922e0 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69  kingContext*)pFi
922f0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
92300 78 74 29 2d 3e 66 69 6c 65 50 61 74 68 29 3b 0a  xt)->filePath);.
92310 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
92320 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  (pFile->lockingC
92330 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 0a 20 20  ontext);.  }..  
92340 69 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64  if( pFile->dirfd
92350 3e 3d 30 20 29 20 63 6c 6f 73 65 28 70 46 69 6c  >=0 ) close(pFil
92360 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20 70 46 69  e->dirfd);.  pFi
92370 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a  le->dirfd = -1;.
92380 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68    close(pFile->h
92390 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43  );.  OSTRACE2("C
923a0 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20  LOSE   %-3d\n", 
923b0 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 4f 70 65  pFile->h);.  Ope
923c0 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20  nCounter(-1);.  
923d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
923e0 3b 0a 7d 0a 0a 0a 23 70 72 61 67 6d 61 20 6d 61  ;.}...#pragma ma
923f0 72 6b 20 66 6c 6f 63 6b 28 29 20 73 74 79 6c 65  rk flock() style
92400 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a 20 2a 2a   locking../*. **
92410 20 54 68 65 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e   The flockLockin
92420 67 43 6f 6e 74 65 78 74 20 69 73 20 6e 6f 74 20  gContext is not 
92430 75 73 65 64 0a 20 2a 2f 0a 74 79 70 65 64 65 66  used. */.typedef
92440 20 76 6f 69 64 20 66 6c 6f 63 6b 4c 6f 63 6b 69   void flockLocki
92450 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 73 74 61 74  ngContext;..stat
92460 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 69 78  ic int flockUnix
92470 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
92480 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
92490 69 64 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  id) {.  unixFile
924a0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
924b0 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 69 66  ile*)id;.  .  if
924c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
924d0 65 20 3d 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f  e == RESERVED_LO
924e0 43 4b 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e  CK) {.    return
924f0 20 31 3b 20 2f 2a 20 61 6c 72 65 61 64 79 20 68   1; /* already h
92500 61 76 65 20 61 20 72 65 73 65 72 76 65 64 20 6c  ave a reserved l
92510 6f 63 6b 20 2a 2f 0a 20 20 7d 20 65 6c 73 65 20  ock */.  } else 
92520 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 74  {.    /* attempt
92530 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b   to get the lock
92540 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d   */.    int rc =
92550 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c   flock(pFile->h,
92560 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f   LOCK_EX | LOCK_
92570 4e 42 29 3b 0a 20 20 20 20 69 66 20 28 21 72 63  NB);.    if (!rc
92580 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74  ) {.      /* got
92590 20 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63   the lock, unloc
925a0 6b 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 66 6c  k it */.      fl
925b0 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f  ock(pFile->h, LO
925c0 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 72 65  CK_UN);.      re
925d0 74 75 72 6e 20 30 3b 20 20 2f 2a 20 6e 6f 20 6f  turn 0;  /* no o
925e0 6e 65 20 68 61 73 20 69 74 20 72 65 73 65 72 76  ne has it reserv
925f0 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ed */.    }.    
92600 72 65 74 75 72 6e 20 31 3b 20 2f 2a 20 73 6f 6d  return 1; /* som
92610 65 6f 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 20  eone else might 
92620 68 61 76 65 20 69 74 20 72 65 73 65 72 76 65 64  have it reserved
92630 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69   */.  }.}..stati
92640 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 69 78 4c  c int flockUnixL
92650 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
92660 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
92670 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  pe) {.  unixFile
92680 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
92690 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 2f 2a  ile*)id;.  .  /*
926a0 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68   if we already h
926b0 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69  ave a lock, it i
926c0 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20  s exclusive.  . 
926d0 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20   ** Just adjust 
926e0 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f  level and punt o
926f0 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f  n outta here. */
92700 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f  .  if (pFile->lo
92710 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b  cktype > NO_LOCK
92720 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ) {.    pFile->l
92730 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
92740 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  pe;.    return S
92750 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
92760 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78  .  /* grab an ex
92770 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a  clusive lock */.
92780 20 20 69 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b    int rc = flock
92790 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f  (pFile->h, LOCK_
927a0 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20  EX | LOCK_NB);. 
927b0 20 69 66 20 28 72 63 29 20 7b 0a 20 20 20 20 2f   if (rc) {.    /
927c0 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20 6d 75  * didn't get, mu
927d0 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a 20 20  st be busy */.  
927e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
927f0 42 55 53 59 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  BUSY;.  } else {
92800 0a 20 20 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20  .    /* got it, 
92810 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64  set the type and
92820 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20   return ok */.  
92830 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
92840 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
92850 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
92860 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69  OK;.  }.}..stati
92870 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 69 78 55  c int flockUnixU
92880 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
92890 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
928a0 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69  type) {.  unixFi
928b0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
928c0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20  xFile*)id;.  .  
928d0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
928e0 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
928f0 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69  .  .  /* no-op i
92900 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20  f possible */.  
92910 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
92920 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype==locktype ){
92930 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
92940 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
92950 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75  /* shared can ju
92960 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73  st be set becaus
92970 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65  e we always have
92980 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f   an exclusive */
92990 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d  .  if (locktype=
929a0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a  =SHARED_LOCK) {.
929b0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
929c0 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
929d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
929e0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
929f0 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e  * no, really, un
92a00 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72  lock. */.  int r
92a10 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d  c = flock(pFile-
92a20 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20  >h, LOCK_UN);.  
92a30 69 66 20 28 72 63 29 0a 20 20 20 20 72 65 74 75  if (rc).    retu
92a40 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
92a50 55 4e 4c 4f 43 4b 3b 0a 20 20 65 6c 73 65 20 7b  UNLOCK;.  else {
92a60 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
92a70 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
92a80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
92a90 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  E_OK;.  }.}../*.
92aa0 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65   ** Close a file
92ab0 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  .. */.static int
92ac0 20 66 6c 6f 63 6b 55 6e 69 78 43 6c 6f 73 65 28   flockUnixClose(
92ad0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49  sqlite3_file *pI
92ae0 64 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  d) {.  unixFile 
92af0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
92b00 6c 65 2a 29 2a 70 49 64 3b 0a 20 20 0a 20 20 69  le*)*pId;.  .  i
92b10 66 28 20 21 70 46 69 6c 65 20 29 20 72 65 74 75  f( !pFile ) retu
92b20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
92b30 66 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f 63 6b 28  flockUnixUnlock(
92b40 2a 70 49 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  *pId, NO_LOCK);.
92b50 20 20 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e    .  if( pFile->
92b60 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65  dirfd>=0 ) close
92b70 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 0a  (pFile->dirfd);.
92b80 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d    pFile->dirfd =
92b90 20 2d 31 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65   -1;.  enterMute
92ba0 78 28 29 3b 0a 20 20 0a 20 20 63 6c 6f 73 65 28  x();.  .  close(
92bb0 70 46 69 6c 65 2d 3e 68 29 3b 20 20 0a 20 20 6c  pFile->h);  .  l
92bc0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
92bd0 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20 20  STRACE2("CLOSE  
92be0 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d   %-3d\n", pFile-
92bf0 3e 68 29 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74  >h);.  OpenCount
92c00 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e  er(-1);.  return
92c10 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
92c20 70 72 61 67 6d 61 20 6d 61 72 6b 20 4f 6c 64 2d  pragma mark Old-
92c30 53 63 68 6f 6f 6c 20 2e 6c 6f 63 6b 20 66 69 6c  School .lock fil
92c40 65 20 62 61 73 65 64 20 6c 6f 63 6b 69 6e 67 0a  e based locking.
92c50 0a 2f 2a 0a 20 2a 2a 20 54 68 65 20 64 6f 74 6c  ./*. ** The dotl
92c60 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
92c70 74 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  t structure cont
92c80 61 69 6e 73 20 61 6c 6c 20 64 6f 74 6c 6f 63 6b  ains all dotlock
92c90 20 28 2e 6c 6f 63 6b 29 20 6c 6f 63 6b 0a 20 2a   (.lock) lock. *
92ca0 2a 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65  * specific state
92cb0 0a 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
92cc0 75 63 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69  uct dotlockLocki
92cd0 6e 67 43 6f 6e 74 65 78 74 20 64 6f 74 6c 6f 63  ngContext dotloc
92ce0 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  kLockingContext;
92cf0 0a 73 74 72 75 63 74 20 64 6f 74 6c 6f 63 6b 4c  .struct dotlockL
92d00 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a  ockingContext {.
92d10 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 61 74 68    char *lockPath
92d20 3b 0a 7d 3b 0a 0a 0a 73 74 61 74 69 63 20 69 6e  ;.};...static in
92d30 74 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 43 68 65  t dotlockUnixChe
92d40 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
92d50 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
92d60 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
92d70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
92d80 2a 29 69 64 3b 0a 20 20 64 6f 74 6c 6f 63 6b 4c  *)id;.  dotlockL
92d90 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63  ockingContext *c
92da0 6f 6e 74 65 78 74 20 3d 20 0a 20 20 20 20 28 64  ontext = .    (d
92db0 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e  otlockLockingCon
92dc0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c  text *) pFile->l
92dd0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
92de0 20 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c   .  if (pFile->l
92df0 6f 63 6b 74 79 70 65 20 3d 3d 20 52 45 53 45 52  ocktype == RESER
92e00 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  VED_LOCK) {.    
92e10 72 65 74 75 72 6e 20 31 3b 20 2f 2a 20 61 6c 72  return 1; /* alr
92e20 65 61 64 79 20 68 61 76 65 20 61 20 72 65 73 65  eady have a rese
92e30 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d  rved lock */.  }
92e40 20 65 6c 73 65 20 7b 0a 20 20 20 20 73 74 72 75   else {.    stru
92e50 63 74 20 73 74 61 74 20 73 74 61 74 42 75 66 3b  ct stat statBuf;
92e60 0a 20 20 20 20 69 66 20 28 6c 73 74 61 74 28 63  .    if (lstat(c
92e70 6f 6e 74 65 78 74 2d 3e 6c 6f 63 6b 50 61 74 68  ontext->lockPath
92e80 2c 26 73 74 61 74 42 75 66 29 20 3d 3d 20 30 29  ,&statBuf) == 0)
92e90 0a 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 65  .      /* file e
92ea0 78 69 73 74 73 2c 20 73 6f 6d 65 6f 6e 65 20 65  xists, someone e
92eb0 6c 73 65 20 68 61 73 20 74 68 65 20 6c 6f 63 6b  lse has the lock
92ec0 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
92ed0 20 31 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20   1;.    else.   
92ee0 20 20 20 2f 2a 20 66 69 6c 65 20 64 6f 65 73 20     /* file does 
92ef0 6e 6f 74 20 65 78 69 73 74 2c 20 77 65 20 63 6f  not exist, we co
92f00 75 6c 64 20 68 61 76 65 20 69 74 20 69 66 20 77  uld have it if w
92f10 65 20 77 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20  e want it */.   
92f20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
92f30 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .}..static int d
92f40 6f 74 6c 6f 63 6b 55 6e 69 78 4c 6f 63 6b 28 73  otlockUnixLock(s
92f50 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
92f60 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
92f70 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
92f80 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
92f90 69 64 3b 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63  id;.  dotlockLoc
92fa0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e  kingContext *con
92fb0 74 65 78 74 20 3d 20 0a 20 20 20 20 28 64 6f 74  text = .    (dot
92fc0 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  lockLockingConte
92fd0 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63  xt *) pFile->loc
92fe0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a  kingContext;.  .
92ff0 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61    /* if we alrea
93000 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20  dy have a lock, 
93010 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e  it is exclusive.
93020 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a    .  ** Just adj
93030 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75  ust level and pu
93040 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65  nt on outta here
93050 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65  . */.  if (pFile
93060 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f  ->locktype > NO_
93070 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c  LOCK) {.    pFil
93080 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
93090 63 6b 74 79 70 65 3b 0a 20 20 20 20 0a 20 20 20  cktype;.    .   
930a0 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64 61 74   /* Always updat
930b0 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d 70 20  e the timestamp 
930c0 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c 65 20  on the old file 
930d0 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 28 63 6f  */.    utimes(co
930e0 6e 74 65 78 74 2d 3e 6c 6f 63 6b 50 61 74 68 2c  ntext->lockPath,
930f0 4e 55 4c 4c 29 3b 0a 20 20 20 20 72 65 74 75 72  NULL);.    retur
93100 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
93110 0a 20 20 0a 20 20 2f 2a 20 63 68 65 63 6b 20 74  .  .  /* check t
93120 6f 20 73 65 65 20 69 66 20 6c 6f 63 6b 20 66 69  o see if lock fi
93130 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
93140 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 74  s */.  struct st
93150 61 74 20 73 74 61 74 42 75 66 3b 0a 20 20 69 66  at statBuf;.  if
93160 20 28 6c 73 74 61 74 28 63 6f 6e 74 65 78 74 2d   (lstat(context-
93170 3e 6c 6f 63 6b 50 61 74 68 2c 26 73 74 61 74 42  >lockPath,&statB
93180 75 66 29 20 3d 3d 20 30 29 7b 0a 20 20 20 20 72  uf) == 0){.    r
93190 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
931a0 59 3b 20 2f 2a 20 69 74 20 64 6f 65 73 2c 20 62  Y; /* it does, b
931b0 75 73 79 20 2a 2f 0a 20 20 7d 0a 20 20 0a 20 20  usy */.  }.  .  
931c0 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75  /* grab an exclu
931d0 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69  sive lock */.  i
931e0 6e 74 20 66 64 20 3d 20 6f 70 65 6e 28 63 6f 6e  nt fd = open(con
931f0 74 65 78 74 2d 3e 6c 6f 63 6b 50 61 74 68 2c 4f  text->lockPath,O
93200 5f 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c  _RDONLY|O_CREAT|
93210 4f 5f 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20  O_EXCL,0600);.  
93220 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 20 20  if (fd < 0) {.  
93230 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f    /* failed to o
93240 70 65 6e 2f 63 72 65 61 74 65 20 74 68 65 20 66  pen/create the f
93250 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73  ile, someone els
93260 65 20 6d 61 79 20 68 61 76 65 20 73 74 6f 6c 65  e may have stole
93270 6e 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  n the lock */.  
93280 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
93290 42 55 53 59 3b 20 0a 20 20 7d 0a 20 20 63 6c 6f  BUSY; .  }.  clo
932a0 73 65 28 66 64 29 3b 0a 20 20 0a 20 20 2f 2a 20  se(fd);.  .  /* 
932b0 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20  got it, set the 
932c0 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20  type and return 
932d0 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c  ok */.  pFile->l
932e0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
932f0 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  pe;.  return SQL
93300 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69  ITE_OK;.}..stati
93310 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 69  c int dotlockUni
93320 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  xUnlock(sqlite3_
93330 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
93340 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78  cktype) {.  unix
93350 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
93360 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 64  nixFile*)id;.  d
93370 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e  otlockLockingCon
93380 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20  text *context = 
93390 0a 20 20 20 20 28 64 6f 74 6c 6f 63 6b 4c 6f 63  .    (dotlockLoc
933a0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70  kingContext *) p
933b0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
933c0 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 65 72  text;.  .  asser
933d0 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
933e0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20  RED_LOCK );.  . 
933f0 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73   /* no-op if pos
93400 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70  sible */.  if( p
93410 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
93420 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
93430 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
93440 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68  ;.  }.  .  /* sh
93450 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65  ared can just be
93460 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20   set because we 
93470 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65  always have an e
93480 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66  xclusive */.  if
93490 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52   (locktype==SHAR
934a0 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70  ED_LOCK) {.    p
934b0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
934c0 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72   locktype;.    r
934d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
934e0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c  .  }.  .  /* no,
934f0 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e   really, unlock.
93500 20 2a 2f 0a 20 20 75 6e 6c 69 6e 6b 28 63 6f 6e   */.  unlink(con
93510 74 65 78 74 2d 3e 6c 6f 63 6b 50 61 74 68 29 3b  text->lockPath);
93520 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  .  pFile->lockty
93530 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20  pe = NO_LOCK;.  
93540 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
93550 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73  ;.}../*. ** Clos
93560 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74  e a file.. */.st
93570 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b  atic int dotlock
93580 55 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74 65  UnixClose(sqlite
93590 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20  3_file *id) {.  
935a0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
935b0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
935c0 0a 20 20 0a 20 20 69 66 28 20 21 70 46 69 6c 65  .  .  if( !pFile
935d0 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
935e0 5f 4f 4b 3b 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e  _OK;.  dotlockUn
935f0 69 78 55 6e 6c 6f 63 6b 28 2a 70 49 64 2c 20 4e  ixUnlock(*pId, N
93600 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 2f 2a 20 66 72  O_LOCK);.  /* fr
93610 65 65 20 74 68 65 20 64 6f 74 6c 6f 63 6b 20 6c  ee the dotlock l
93620 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65  ocking structure
93630 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d   */.  if (pFile-
93640 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  >lockingContext 
93650 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 69  != NULL) {.    i
93660 66 20 28 28 28 64 6f 74 6c 6f 63 6b 4c 6f 63 6b  f (((dotlockLock
93670 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69  ingContext *)pFi
93680 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
93690 78 74 29 2d 3e 6c 6f 63 6b 50 61 74 68 20 21 3d  xt)->lockPath !=
936a0 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 73 71 6c   NULL).      sql
936b0 69 74 65 33 5f 66 72 65 65 28 20 28 20 28 64 6f  ite3_free( ( (do
936c0 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74  tlockLockingCont
936d0 65 78 74 20 2a 29 0a 20 20 20 20 20 20 20 20 70  ext *).        p
936e0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
936f0 74 65 78 74 29 2d 3e 6c 6f 63 6b 50 61 74 68 29  text)->lockPath)
93700 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
93710 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  ee(pFile->lockin
93720 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20  gContext);.  }. 
93730 20 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 64   .  if( pFile->d
93740 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28  irfd>=0 ) close(
93750 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 0a 20  pFile->dirfd);. 
93760 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20   pFile->dirfd = 
93770 2d 31 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78  -1;.  enterMutex
93780 28 29 3b 0a 20 20 0a 20 20 63 6c 6f 73 65 28 70  ();.  .  close(p
93790 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 0a 20 20 6c  File->h);.  .  l
937a0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
937b0 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20 20  STRACE2("CLOSE  
937c0 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d   %-3d\n", pFile-
937d0 3e 68 29 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74  >h);.  OpenCount
937e0 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e  er(-1);.  return
937f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
93800 23 70 72 61 67 6d 61 20 6d 61 72 6b 20 4e 6f 20  #pragma mark No 
93810 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a 20 2a 2a 20  locking../*. ** 
93820 54 68 65 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 69 6e  The nolockLockin
93830 67 43 6f 6e 74 65 78 74 20 69 73 20 76 6f 69 64  gContext is void
93840 0a 20 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69  . */.typedef voi
93850 64 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43  d nolockLockingC
93860 6f 6e 74 65 78 74 3b 0a 0a 73 74 61 74 69 63 20  ontext;..static 
93870 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 69 78 43 68  int nolockUnixCh
93880 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
93890 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
938a0 29 20 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  ) {.  return 0;.
938b0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  }..static int no
938c0 6c 6f 63 6b 55 6e 69 78 4c 6f 63 6b 28 73 71 6c  lockUnixLock(sql
938d0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
938e0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
938f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
93900 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  K;.}..static int
93910 20 6e 6f 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f 63   nolockUnixUnloc
93920 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
93930 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
93940 29 20 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ) {.  return SQL
93950 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a  ITE_OK;.}../*. *
93960 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a  * Close a file..
93970 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e   */.static int n
93980 6f 6c 6f 63 6b 55 6e 69 78 43 6c 6f 73 65 28 73  olockUnixClose(s
93990 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
939a0 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
939b0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
939c0 2a 29 69 64 3b 0a 20 20 0a 20 20 69 66 28 20 21  *)id;.  .  if( !
939d0 70 46 69 6c 65 20 29 20 72 65 74 75 72 6e 20 53  pFile ) return S
939e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
939f0 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20  pFile->dirfd>=0 
93a00 29 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64  ) close(pFile->d
93a10 69 72 66 64 29 3b 0a 20 20 70 46 69 6c 65 2d 3e  irfd);.  pFile->
93a20 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 65 6e  dirfd = -1;.  en
93a30 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 0a 20  terMutex();.  . 
93a40 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29   close(pFile->h)
93a50 3b 0a 20 20 0a 20 20 6c 65 61 76 65 4d 75 74 65  ;.  .  leaveMute
93a60 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28  x();.  OSTRACE2(
93a70 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22  "CLOSE   %-3d\n"
93a80 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 4f  , pFile->h);.  O
93a90 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a  penCounter(-1);.
93aa0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
93ab0 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  OK;.}..#endif /*
93ac0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
93ad0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
93ae0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74  ../*.** Informat
93af0 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20  ion and control 
93b00 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  of an open file 
93b10 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  handle..*/.stati
93b20 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 43 6f  c int unixFileCo
93b30 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69  ntrol(sqlite3_fi
93b40 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20  le *id, int op, 
93b50 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73  void *pArg){.  s
93b60 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
93b70 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e   case SQLITE_FCN
93b80 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a  TL_LOCKSTATE: {.
93b90 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72        *(int*)pAr
93ba0 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29  g = ((unixFile*)
93bb0 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20  id)->locktype;. 
93bc0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
93bd0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
93be0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
93bf0 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _ERROR;.}../*.**
93c00 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74   Return the sect
93c10 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  or size in bytes
93c20 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69   of the underlyi
93c30 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20  ng block device 
93c40 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69  for.** the speci
93c50 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20  fied file. This 
93c60 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73  is almost always
93c70 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20   512 bytes, but 
93c80 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72  may be.** larger
93c90 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65   for some device
93ca0 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  s..**.** SQLite 
93cb0 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69  code assumes thi
93cc0 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f  s function canno
93cd0 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20  t fail. It also 
93ce0 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20  assumes that.** 
93cf0 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65  if two files are
93d00 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20   created in the 
93d10 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d  same file-system
93d20 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e   directory (i.e.
93d30 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61  .** a database a
93d40 6e 64 20 69 74 27 73 20 6a 6f 75 72 6e 61 6c 20  nd it's journal 
93d50 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 73  file) that the s
93d60 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20  ector size will 
93d70 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66  be the.** same f
93d80 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74  or both..*/.stat
93d90 69 63 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f  ic int unixSecto
93da0 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
93db0 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72  le *id){.  retur
93dc0 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  n SQLITE_DEFAULT
93dd0 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a  _SECTOR_SIZE;.}.
93de0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
93df0 65 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74  e device charact
93e00 65 72 69 73 74 69 63 73 20 66 6f 72 20 74 68 65  eristics for the
93e10 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
93e20 6c 77 61 79 73 20 30 2e 0a 2a 2f 0a 73 74 61 74  lways 0..*/.stat
93e30 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76 69 63  ic int unixDevic
93e40 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
93e50 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
93e60 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  d){.  return 0;.
93e70 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  }../*.** This ve
93e80 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c  ctor defines all
93e90 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61   the methods tha
93ea0 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e  t can operate on
93eb0 20 61 6e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   an sqlite3_file
93ec0 0a 2a 2a 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f  .** for unix..*/
93ed0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
93ee0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
93ef0 20 73 71 6c 69 74 65 33 55 6e 69 78 49 6f 4d 65   sqlite3UnixIoMe
93f00 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20  thod = {.  1,   
93f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93f20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
93f30 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c   */.  unixClose,
93f40 0a 20 20 75 6e 69 78 52 65 61 64 2c 0a 20 20 75  .  unixRead,.  u
93f50 6e 69 78 57 72 69 74 65 2c 0a 20 20 75 6e 69 78  nixWrite,.  unix
93f60 54 72 75 6e 63 61 74 65 2c 0a 20 20 75 6e 69 78  Truncate,.  unix
93f70 53 79 6e 63 2c 0a 20 20 75 6e 69 78 46 69 6c 65  Sync,.  unixFile
93f80 53 69 7a 65 2c 0a 20 20 75 6e 69 78 4c 6f 63 6b  Size,.  unixLock
93f90 2c 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 0a  ,.  unixUnlock,.
93fa0 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72    unixCheckReser
93fb0 76 65 64 4c 6f 63 6b 2c 0a 20 20 75 6e 69 78 46  vedLock,.  unixF
93fc0 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 75 6e  ileControl,.  un
93fd0 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20  ixSectorSize,.  
93fe0 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63  unixDeviceCharac
93ff0 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 23 69  teristics.};..#i
94000 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
94010 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
94020 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74  ./*.** This vect
94030 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74  or defines all t
94040 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  he methods that 
94050 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61  can operate on a
94060 6e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a  n sqlite3_file.*
94070 2a 20 66 6f 72 20 75 6e 69 78 20 77 69 74 68 20  * for unix with 
94080 41 46 50 20 73 74 79 6c 65 20 66 69 6c 65 20 6c  AFP style file l
94090 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  ocking..*/.stati
940a0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
940b0 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74  io_methods sqlit
940c0 65 33 41 46 50 4c 6f 63 6b 69 6e 67 55 6e 69 78  e3AFPLockingUnix
940d0 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31  IoMethod = {.  1
940e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
940f0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
94100 73 69 6f 6e 20 2a 2f 0a 20 20 75 6e 69 78 43 6c  sion */.  unixCl
94110 6f 73 65 2c 0a 20 20 75 6e 69 78 52 65 61 64 2c  ose,.  unixRead,
94120 0a 20 20 75 6e 69 78 57 72 69 74 65 2c 0a 20 20  .  unixWrite,.  
94130 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 0a 20 20  unixTruncate,.  
94140 75 6e 69 78 53 79 6e 63 2c 0a 20 20 75 6e 69 78  unixSync,.  unix
94150 46 69 6c 65 53 69 7a 65 2c 0a 20 20 61 66 70 55  FileSize,.  afpU
94160 6e 69 78 4c 6f 63 6b 2c 0a 20 20 61 66 70 55 6e  nixLock,.  afpUn
94170 69 78 55 6e 6c 6f 63 6b 2c 0a 20 20 61 66 70 55  ixUnlock,.  afpU
94180 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  nixCheckReserved
94190 4c 6f 63 6b 2c 0a 20 20 75 6e 69 78 46 69 6c 65  Lock,.  unixFile
941a0 43 6f 6e 74 72 6f 6c 2c 0a 20 20 75 6e 69 78 53  Control,.  unixS
941b0 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 75 6e 69  ectorSize,.  uni
941c0 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
941d0 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  istics.};../*.**
941e0 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66   This vector def
941f0 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74  ines all the met
94200 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70  hods that can op
94210 65 72 61 74 65 20 6f 6e 20 61 6e 20 73 71 6c 69  erate on an sqli
94220 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 66 6f 72 20  te3_file.** for 
94230 75 6e 69 78 20 77 69 74 68 20 66 6c 6f 63 6b 28  unix with flock(
94240 29 20 73 74 79 6c 65 20 66 69 6c 65 20 6c 6f 63  ) style file loc
94250 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  king..*/.static 
94260 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
94270 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33  _methods sqlite3
94280 46 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69 78  FlockLockingUnix
94290 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31  IoMethod = {.  1
942a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
942b0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
942c0 73 69 6f 6e 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55  sion */.  flockU
942d0 6e 69 78 43 6c 6f 73 65 2c 0a 20 20 75 6e 69 78  nixClose,.  unix
942e0 52 65 61 64 2c 0a 20 20 75 6e 69 78 57 72 69 74  Read,.  unixWrit
942f0 65 2c 0a 20 20 75 6e 69 78 54 72 75 6e 63 61 74  e,.  unixTruncat
94300 65 2c 0a 20 20 75 6e 69 78 53 79 6e 63 2c 0a 20  e,.  unixSync,. 
94310 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 0a 20   unixFileSize,. 
94320 20 66 6c 6f 63 6b 55 6e 69 78 4c 6f 63 6b 2c 0a   flockUnixLock,.
94330 20 20 66 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f 63    flockUnixUnloc
94340 6b 2c 0a 20 20 66 6c 6f 63 6b 55 6e 69 78 43 68  k,.  flockUnixCh
94350 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c  eckReservedLock,
94360 0a 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72  .  unixFileContr
94370 6f 6c 2c 0a 20 20 75 6e 69 78 53 65 63 74 6f 72  ol,.  unixSector
94380 53 69 7a 65 2c 0a 20 20 75 6e 69 78 44 65 76 69  Size,.  unixDevi
94390 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
943a0 73 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  s.};../*.** This
943b0 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20   vector defines 
943c0 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20  all the methods 
943d0 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65  that can operate
943e0 20 6f 6e 20 61 6e 20 73 71 6c 69 74 65 33 5f 66   on an sqlite3_f
943f0 69 6c 65 0a 2a 2a 20 66 6f 72 20 75 6e 69 78 20  ile.** for unix 
94400 77 69 74 68 20 64 6f 74 6c 6f 63 6b 20 73 74 79  with dotlock sty
94410 6c 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e  le file locking.
94420 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
94430 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
94440 6f 64 73 20 73 71 6c 69 74 65 33 44 6f 74 6c 6f  ods sqlite3Dotlo
94450 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69 78 49 6f 4d  ckLockingUnixIoM
94460 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20  ethod = {.  1,  
94470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94480 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
94490 6e 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e  n */.  dotlockUn
944a0 69 78 43 6c 6f 73 65 2c 0a 20 20 75 6e 69 78 52  ixClose,.  unixR
944b0 65 61 64 2c 0a 20 20 75 6e 69 78 57 72 69 74 65  ead,.  unixWrite
944c0 2c 0a 20 20 75 6e 69 78 54 72 75 6e 63 61 74 65  ,.  unixTruncate
944d0 2c 0a 20 20 75 6e 69 78 53 79 6e 63 2c 0a 20 20  ,.  unixSync,.  
944e0 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 0a 20 20  unixFileSize,.  
944f0 64 6f 74 6c 6f 63 6b 55 6e 69 78 4c 6f 63 6b 2c  dotlockUnixLock,
94500 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 55 6e  .  dotlockUnixUn
94510 6c 6f 63 6b 2c 0a 20 20 64 6f 74 6c 6f 63 6b 55  lock,.  dotlockU
94520 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  nixCheckReserved
94530 4c 6f 63 6b 2c 0a 20 20 75 6e 69 78 46 69 6c 65  Lock,.  unixFile
94540 43 6f 6e 74 72 6f 6c 2c 0a 20 20 75 6e 69 78 53  Control,.  unixS
94550 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 75 6e 69  ectorSize,.  uni
94560 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
94570 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  istics.};../*.**
94580 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66   This vector def
94590 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74  ines all the met
945a0 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70  hods that can op
945b0 65 72 61 74 65 20 6f 6e 20 61 6e 20 73 71 6c 69  erate on an sqli
945c0 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 66 6f 72 20  te3_file.** for 
945d0 75 6e 69 78 20 77 69 74 68 20 64 6f 74 6c 6f 63  unix with dotloc
945e0 6b 20 73 74 79 6c 65 20 66 69 6c 65 20 6c 6f 63  k style file loc
945f0 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  king..*/.static 
94600 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
94610 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33  _methods sqlite3
94620 4e 6f 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69  NolockLockingUni
94630 78 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20  xIoMethod = {.  
94640 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
94650 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
94660 72 73 69 6f 6e 20 2a 2f 0a 20 20 6e 6f 6c 6f 63  rsion */.  noloc
94670 6b 55 6e 69 78 43 6c 6f 73 65 2c 0a 20 20 75 6e  kUnixClose,.  un
94680 69 78 52 65 61 64 2c 0a 20 20 75 6e 69 78 57 72  ixRead,.  unixWr
94690 69 74 65 2c 0a 20 20 75 6e 69 78 54 72 75 6e 63  ite,.  unixTrunc
946a0 61 74 65 2c 0a 20 20 75 6e 69 78 53 79 6e 63 2c  ate,.  unixSync,
946b0 0a 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c  .  unixFileSize,
946c0 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 69 78 4c 6f 63  .  nolockUnixLoc
946d0 6b 2c 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 69 78 55  k,.  nolockUnixU
946e0 6e 6c 6f 63 6b 2c 0a 20 20 6e 6f 6c 6f 63 6b 55  nlock,.  nolockU
946f0 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  nixCheckReserved
94700 4c 6f 63 6b 2c 0a 20 20 75 6e 69 78 46 69 6c 65  Lock,.  unixFile
94710 43 6f 6e 74 72 6f 6c 2c 0a 20 20 75 6e 69 78 53  Control,.  unixS
94720 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 75 6e 69  ectorSize,.  uni
94730 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
94740 69 73 74 69 63 73 0a 7d 3b 0a 0a 23 65 6e 64 69  istics.};..#endi
94750 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  f /* SQLITE_ENAB
94760 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
94770 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63   */../*.** Alloc
94780 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 61  ate memory for a
94790 20 6e 65 77 20 75 6e 69 78 46 69 6c 65 20 61 6e   new unixFile an
947a0 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 61  d initialize tha
947b0 74 20 75 6e 69 78 46 69 6c 65 2e 0a 2a 2a 20 57  t unixFile..** W
947c0 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74  rite a pointer t
947d0 6f 20 74 68 65 20 6e 65 77 20 75 6e 69 78 46 69  o the new unixFi
947e0 6c 65 20 69 6e 74 6f 20 2a 70 49 64 2e 0a 2a 2a  le into *pId..**
947f0 20 49 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f   If we run out o
94800 66 20 6d 65 6d 6f 72 79 2c 20 63 6c 6f 73 65 20  f memory, close 
94810 74 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65 74  the file and ret
94820 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  urn an error..*/
94830 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
94840 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
94850 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 57 68 65 6e 20  YLE./* .** When 
94860 6c 6f 63 6b 69 6e 67 20 65 78 74 65 6e 73 69 6f  locking extensio
94870 6e 73 20 61 72 65 20 65 6e 61 62 6c 65 64 2c 20  ns are enabled, 
94880 74 68 65 20 66 69 6c 65 70 61 74 68 20 61 6e 64  the filepath and
94890 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 0a   locking style .
948a0 2a 2a 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f  ** are needed to
948b0 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 75   determine the u
948c0 6e 69 78 46 69 6c 65 20 70 4d 65 74 68 6f 64 20  nixFile pMethod 
948d0 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f 63 6b 69  to use for locki
948e0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a  ng operations..*
948f0 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 2d 73 74  * The locking-st
94900 79 6c 65 20 73 70 65 63 69 66 69 63 20 6c 6f 63  yle specific loc
94910 6b 69 6e 67 43 6f 6e 74 65 78 74 20 64 61 74 61  kingContext data
94920 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72   structure is cr
94930 65 61 74 65 64 20 0a 2a 2a 20 61 6e 64 20 61 73  eated .** and as
94940 73 69 67 6e 65 64 20 68 65 72 65 20 61 6c 73 6f  signed here also
94950 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
94960 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a  fillInUnixFile(.
94970 20 20 69 6e 74 20 68 2c 20 20 20 20 20 20 20 20    int h,        
94980 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
94990 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
949a0 72 20 6f 66 20 66 69 6c 65 20 62 65 69 6e 67 20  r of file being 
949b0 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20  opened */.  int 
949c0 64 69 72 66 64 2c 20 20 20 20 20 20 20 20 20 20  dirfd,          
949d0 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f 72 79      /* Directory
949e0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
949f0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
94a00 6c 65 20 2a 70 49 64 2c 20 20 20 20 20 20 2f 2a  le *pId,      /*
94a10 20 57 72 69 74 65 20 63 6f 6d 70 6c 65 74 65 64   Write completed
94a20 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
94a30 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  here */.  const 
94a40 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
94a50 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
94a60 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e   file being open
94a70 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  ed */.){.  sqlit
94a80 65 33 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 6c  e3LockingStyle l
94a90 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 75  ockingStyle;.  u
94aa0 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20  nixFile *pNew = 
94ab0 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49 64 3b  (unixFile *)pId;
94ac0 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 6d 65  .  int rc;..  me
94ad0 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69  mset(pNew, 0, si
94ae0 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b  zeof(unixFile));
94af0 0a 20 20 6c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  .  lockingStyle 
94b00 3d 20 73 71 6c 69 74 65 33 44 65 74 65 63 74 4c  = sqlite3DetectL
94b10 6f 63 6b 69 6e 67 53 74 79 6c 65 28 7a 46 69 6c  ockingStyle(zFil
94b20 65 6e 61 6d 65 2c 20 68 29 3b 0a 20 20 69 66 20  ename, h);.  if 
94b30 28 20 6c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d  ( lockingStyle =
94b40 3d 20 70 6f 73 69 78 4c 6f 63 6b 69 6e 67 53 74  = posixLockingSt
94b50 79 6c 65 20 29 20 7b 0a 20 20 20 20 65 6e 74 65  yle ) {.    ente
94b60 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 63  rMutex();.    rc
94b70 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28   = findLockInfo(
94b80 68 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c  h, &pNew->pLock,
94b90 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a   &pNew->pOpen);.
94ba0 20 20 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29      leaveMutex()
94bb0 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
94bc0 20 20 20 20 20 20 63 6c 6f 73 65 28 68 29 3b 0a        close(h);.
94bd0 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69        unlink(zFi
94be0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72  lename);.      r
94bf0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
94c00 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c  EM;.    }.  } el
94c10 73 65 20 7b 0a 20 20 20 20 2f 2a 20 20 70 4c 6f  se {.    /*  pLo
94c20 63 6b 20 61 6e 64 20 70 4f 70 65 6e 20 61 72 65  ck and pOpen are
94c30 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 70   only used for p
94c40 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f  osix advisory lo
94c50 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 70 4e 65  cking */.    pNe
94c60 77 2d 3e 70 4c 6f 63 6b 20 3d 20 4e 55 4c 4c 3b  w->pLock = NULL;
94c70 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e  .    pNew->pOpen
94c80 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 70   = NULL;.  }.  p
94c90 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b  New->dirfd = -1;
94ca0 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a  .  pNew->h = h;.
94cb0 20 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 70    SET_THREADID(p
94cc0 4e 65 77 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73  New);.  pNew = s
94cd0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73  qlite3_malloc( s
94ce0 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 20  izeof(unixFile) 
94cf0 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
94d00 20 29 7b 0a 20 20 20 20 63 6c 6f 73 65 28 68 29   ){.    close(h)
94d10 3b 0a 20 20 20 20 65 6e 74 65 72 4d 75 74 65 78  ;.    enterMutex
94d20 28 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c  ();.    releaseL
94d30 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2d 3e 70 4c  ockInfo(pNew->pL
94d40 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73  ock);.    releas
94d50 65 4f 70 65 6e 43 6e 74 28 70 4e 65 77 2d 3e 70  eOpenCnt(pNew->p
94d60 4f 70 65 6e 29 3b 0a 20 20 20 20 6c 65 61 76 65  Open);.    leave
94d70 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74  Mutex();.    ret
94d80 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
94d90 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
94da0 77 69 74 63 68 28 6c 6f 63 6b 69 6e 67 53 74 79  witch(lockingSty
94db0 6c 65 29 20 7b 0a 20 20 20 20 20 20 63 61 73 65  le) {.      case
94dc0 20 61 66 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65   afpLockingStyle
94dd0 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 61  : {.        /* a
94de0 66 70 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20  fp locking uses 
94df0 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f  the file path so
94e00 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20   it needs to be 
94e10 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20  included in.    
94e20 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70 4c 6f      ** the afpLo
94e30 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 2f 0a  ckingContext */.
94e40 20 20 20 20 20 20 20 20 69 6e 74 20 6e 46 69 6c          int nFil
94e50 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 70  ename;.        p
94e60 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26  New->pMethod = &
94e70 73 71 6c 69 74 65 33 41 46 50 4c 6f 63 6b 69 6e  sqlite3AFPLockin
94e80 67 55 6e 69 78 49 6f 4d 65 74 68 6f 64 3b 0a 20  gUnixIoMethod;. 
94e90 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63         pNew->loc
94ea0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 0a 20  kingContext = . 
94eb0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
94ec0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 61  _malloc(sizeof(a
94ed0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
94ee0 29 29 3b 0a 20 20 20 20 20 20 20 20 6e 46 69 6c  ));.        nFil
94ef0 65 6e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a  ename = strlen(z
94f00 46 69 6c 65 6e 61 6d 65 29 2b 31 3b 0a 20 20 20  Filename)+1;.   
94f10 20 20 20 20 20 28 28 61 66 70 4c 6f 63 6b 69 6e       ((afpLockin
94f20 67 43 6f 6e 74 65 78 74 20 2a 29 70 4e 65 77 2d  gContext *)pNew-
94f30 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29  >lockingContext)
94f40 2d 3e 66 69 6c 65 50 61 74 68 20 3d 20 0a 20 20  ->filePath = .  
94f50 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
94f60 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65  malloc(nFilename
94f70 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  );.        memcp
94f80 79 28 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  y(((afpLockingCo
94f90 6e 74 65 78 74 20 2a 29 70 4e 65 77 2d 3e 6c 6f  ntext *)pNew->lo
94fa0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 66  ckingContext)->f
94fb0 69 6c 65 50 61 74 68 2c 20 0a 20 20 20 20 20 20  ilePath, .      
94fc0 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61           zFilena
94fd0 6d 65 2c 20 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a  me, nFilename);.
94fe0 20 20 20 20 20 20 20 20 73 72 61 6e 64 6f 6d 64          srandomd
94ff0 65 76 28 29 3b 0a 20 20 20 20 20 20 20 20 62 72  ev();.        br
95000 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
95010 20 20 20 63 61 73 65 20 66 6c 6f 63 6b 4c 6f 63     case flockLoc
95020 6b 69 6e 67 53 74 79 6c 65 3a 0a 20 20 20 20 20  kingStyle:.     
95030 20 20 20 2f 2a 20 66 6c 6f 63 6b 20 6c 6f 63 6b     /* flock lock
95040 69 6e 67 20 64 6f 65 73 6e 27 74 20 6e 65 65 64  ing doesn't need
95050 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
95060 69 6e 67 43 6f 6e 74 65 78 74 20 69 6e 66 6f 72  ingContext infor
95070 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  mation */.      
95080 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20    pNew->pMethod 
95090 3d 20 26 73 71 6c 69 74 65 33 46 6c 6f 63 6b 4c  = &sqlite3FlockL
950a0 6f 63 6b 69 6e 67 55 6e 69 78 49 6f 4d 65 74 68  ockingUnixIoMeth
950b0 6f 64 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  od;.        brea
950c0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 64 6f  k;.      case do
950d0 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79 6c  tlockLockingStyl
950e0 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  e: {.        /* 
950f0 64 6f 74 6c 6f 63 6b 20 6c 6f 63 6b 69 6e 67 20  dotlock locking 
95100 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61  uses the file pa
95110 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74  th so it needs t
95120 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e  o be included in
95130 0a 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  .         ** the
95140 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43   dotlockLockingC
95150 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 20 20  ontext */.      
95160 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b    int nFilename;
95170 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  .        pNew->p
95180 4d 65 74 68 6f 64 20 3d 20 26 73 71 6c 69 74 65  Method = &sqlite
95190 33 44 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 55  3DotlockLockingU
951a0 6e 69 78 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 20  nixIoMethod;.   
951b0 20 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69       pNew->locki
951c0 6e 67 43 6f 6e 74 65 78 74 20 3d 20 73 71 6c 69  ngContext = sqli
951d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 0a 20 20 20 20  te3_malloc(.    
951e0 20 20 20 20 20 20 73 69 7a 65 6f 66 28 64 6f 74        sizeof(dot
951f0 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  lockLockingConte
95200 78 74 29 29 3b 0a 20 20 20 20 20 20 20 20 6e 46  xt));.        nF
95210 69 6c 65 6e 61 6d 65 20 3d 20 73 74 72 6c 65 6e  ilename = strlen
95220 28 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b  (zFilename) + 6;
95230 0a 20 20 20 20 20 20 20 20 28 28 64 6f 74 6c 6f  .        ((dotlo
95240 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ckLockingContext
95250 20 2a 29 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67   *)pNew->locking
95260 43 6f 6e 74 65 78 74 29 2d 3e 6c 6f 63 6b 50 61  Context)->lockPa
95270 74 68 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20  th = .          
95280 20 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63    sqlite3_malloc
95290 28 20 6e 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20  ( nFilename );. 
952a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
952b0 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d  nprintf(nFilenam
952c0 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
952d0 20 20 20 20 28 28 64 6f 74 6c 6f 63 6b 4c 6f 63      ((dotlockLoc
952e0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 4e  kingContext *)pN
952f0 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
95300 78 74 29 2d 3e 6c 6f 63 6b 50 61 74 68 2c 20 0a  xt)->lockPath, .
95310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
95320 22 25 73 2e 6c 6f 63 6b 22 2c 20 7a 46 69 6c 65  "%s.lock", zFile
95330 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 62  name);.        b
95340 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
95350 20 20 20 20 63 61 73 65 20 70 6f 73 69 78 4c 6f      case posixLo
95360 63 6b 69 6e 67 53 74 79 6c 65 3a 0a 20 20 20 20  ckingStyle:.    
95370 20 20 20 20 2f 2a 20 70 6f 73 69 78 20 6c 6f 63      /* posix loc
95380 6b 69 6e 67 20 64 6f 65 73 6e 27 74 20 6e 65 65  king doesn't nee
95390 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63  d additional loc
953a0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 69 6e 66 6f  kingContext info
953b0 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  rmation */.     
953c0 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64     pNew->pMethod
953d0 20 3d 20 26 73 71 6c 69 74 65 33 55 6e 69 78 49   = &sqlite3UnixI
953e0 6f 4d 65 74 68 6f 64 3b 0a 20 20 20 20 20 20 20  oMethod;.       
953f0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
95400 73 65 20 6e 6f 4c 6f 63 6b 69 6e 67 53 74 79 6c  se noLockingStyl
95410 65 3a 0a 20 20 20 20 20 20 63 61 73 65 20 75 6e  e:.      case un
95420 73 75 70 70 6f 72 74 65 64 4c 6f 63 6b 69 6e 67  supportedLocking
95430 53 74 79 6c 65 3a 0a 20 20 20 20 20 20 64 65 66  Style:.      def
95440 61 75 6c 74 3a 20 0a 20 20 20 20 20 20 20 20 70  ault: .        p
95450 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26  New->pMethod = &
95460 73 71 6c 69 74 65 33 4e 6f 6c 6f 63 6b 4c 6f 63  sqlite3NolockLoc
95470 6b 69 6e 67 55 6e 69 78 49 6f 4d 65 74 68 6f 64  kingUnixIoMethod
95480 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 70 65 6e  ;.    }.    Open
95490 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 20  Counter(+1);.   
954a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
954b0 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 2f  K;.  }.}.#else /
954c0 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
954d0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
954e0 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c  .static int fill
954f0 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 69 6e  InUnixFile(.  in
95500 74 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20  t h,            
95510 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c       /* Open fil
95520 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20  e descriptor on 
95530 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65  file being opene
95540 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64  d */.  int dirfd
95550 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ,.  sqlite3_file
95560 20 2a 70 49 64 2c 20 20 20 20 20 2f 2a 20 57 72   *pId,     /* Wr
95570 69 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46  ite to the unixF
95580 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 65  ile structure he
95590 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  re */.  const ch
955a0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 20 20 2f  ar *zFilename  /
955b0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69  * Name of the fi
955c0 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20  le being opened 
955d0 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65  */.){.  unixFile
955e0 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69   *pNew = (unixFi
955f0 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20  le *)pId;.  int 
95600 72 63 3b 0a 0a 23 69 66 64 65 66 20 46 44 5f 43  rc;..#ifdef FD_C
95610 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 68  LOEXEC.  fcntl(h
95620 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c  , F_SETFD, fcntl
95630 28 68 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20  (h, F_GETFD, 0) 
95640 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23  | FD_CLOEXEC);.#
95650 65 6e 64 69 66 0a 0a 20 20 65 6e 74 65 72 4d 75  endif..  enterMu
95660 74 65 78 28 29 3b 0a 20 20 72 63 20 3d 20 66 69  tex();.  rc = fi
95670 6e 64 4c 6f 63 6b 49 6e 66 6f 28 68 2c 20 26 70  ndLockInfo(h, &p
95680 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65  New->pLock, &pNe
95690 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 6c 65 61  w->pOpen);.  lea
956a0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  veMutex();.  if(
956b0 20 72 63 20 29 7b 0a 20 20 20 20 63 6c 6f 73 65   rc ){.    close
956c0 28 68 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  (h);.    return 
956d0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
956e0 7d 0a 0a 20 20 4f 53 54 52 41 43 45 33 28 22 4f  }..  OSTRACE3("O
956f0 50 45 4e 20 20 20 20 25 2d 33 64 20 25 73 5c 6e  PEN    %-3d %s\n
95700 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29  ", h, zFilename)
95710 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20  ;.  pNew->dirfd 
95720 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 68 20  = -1;.  pNew->h 
95730 3d 20 68 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72  = h;.  pNew->dir
95740 66 64 20 3d 20 64 69 72 66 64 3b 0a 20 20 53 45  fd = dirfd;.  SE
95750 54 5f 54 48 52 45 41 44 49 44 28 70 4e 65 77 29  T_THREADID(pNew)
95760 3b 0a 0a 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68  ;..  pNew->pMeth
95770 6f 64 20 3d 20 26 73 71 6c 69 74 65 33 55 6e 69  od = &sqlite3Uni
95780 78 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 4f 70 65  xIoMethod;.  Ope
95790 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20  nCounter(+1);.  
957a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
957b0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
957c0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
957d0 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a  ING_STYLE */../*
957e0 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 20  .** Open a file 
957f0 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74 68  descriptor to th
95800 65 20 64 69 72 65 63 74 6f 72 79 20 63 6f 6e 74  e directory cont
95810 61 69 6e 69 6e 67 20 66 69 6c 65 20 7a 46 69 6c  aining file zFil
95820 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66 20 73 75 63  ename..** If suc
95830 63 65 73 73 66 75 6c 2c 20 2a 70 46 64 20 69 73  cessful, *pFd is
95840 20 73 65 74 20 74 6f 20 74 68 65 20 6f 70 65 6e   set to the open
95850 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
95860 6f 72 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45  or and.** SQLITE
95870 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
95880 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
95890 75 72 73 2c 20 65 69 74 68 65 72 20 53 51 4c 49  urs, either SQLI
958a0 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6f 72 20 53  TE_NOMEM.** or S
958b0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 69  QLITE_CANTOPEN i
958c0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
958d0 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 61 6e  pFd is set to an
958e0 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 76 61   undefined.** va
958f0 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51  lue..**.** If SQ
95900 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
95910 6e 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20  ned, the caller 
95920 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
95930 6f 72 20 63 6c 6f 73 69 6e 67 0a 2a 2a 20 74 68  or closing.** th
95940 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
95950 72 20 2a 70 46 64 20 75 73 69 6e 67 20 63 6c 6f  r *pFd using clo
95960 73 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  se()..*/.static 
95970 69 6e 74 20 6f 70 65 6e 44 69 72 65 63 74 6f 72  int openDirector
95980 79 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  y(const char *zF
95990 69 6c 65 6e 61 6d 65 2c 20 69 6e 74 20 2a 70 46  ilename, int *pF
959a0 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  d){.  int ii;.  
959b0 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 7a  int fd;.  char z
959c0 44 69 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48  Dirname[MAX_PATH
959d0 4e 41 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69  NAME+1];..  sqli
959e0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58  te3_snprintf(MAX
959f0 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 6e  _PATHNAME, zDirn
95a00 61 6d 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c 65  ame, "%s", zFile
95a10 6e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d  name);.  for(ii=
95a20 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29  strlen(zDirname)
95a30 3b 20 69 69 3e 3d 30 20 26 26 20 7a 44 69 72 6e  ; ii>=0 && zDirn
95a40 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69  ame[ii]!='/'; ii
95a50 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20  --);.  if( ii>0 
95a60 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b  ){.    zDirname[
95a70 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  ii] = '\0';.    
95a80 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61  fd = open(zDirna
95a90 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42  me, O_RDONLY|O_B
95aa0 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69  INARY, 0);.    i
95ab0 66 28 20 66 64 3e 30 20 29 7b 0a 23 69 66 64 65  f( fd>0 ){.#ifde
95ac0 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20  f FD_CLOEXEC.   
95ad0 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53     fcntl(fd, F_S
95ae0 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20  ETFD, fcntl(fd, 
95af0 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44  F_GETFD, 0) | FD
95b00 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69  _CLOEXEC);.#endi
95b10 66 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33  f.      OSTRACE3
95b20 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25  ("OPENDIR %-3d %
95b30 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61  s\n", fd, zDirna
95b40 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  me);.    }.  }. 
95b50 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65   *pFd = fd;.  re
95b60 74 75 72 6e 20 28 66 64 3e 30 3f 53 51 4c 49 54  turn (fd>0?SQLIT
95b70 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54  E_OK:SQLITE_CANT
95b80 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  OPEN);.}../*.** 
95b90 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 7a 50  Open the file zP
95ba0 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 76  ath..** .** Prev
95bb0 69 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 4c 69  iously, the SQLi
95bc0 74 65 20 4f 53 20 6c 61 79 65 72 20 75 73 65 64  te OS layer used
95bd0 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73   three functions
95be0 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 69   in place of thi
95bf0 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20  s.** one:.**.** 
95c00 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65      sqlite3OsOpe
95c10 6e 52 65 61 64 57 72 69 74 65 28 29 3b 0a 2a 2a  nReadWrite();.**
95c20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70       sqlite3OsOp
95c30 65 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a 2a  enReadOnly();.**
95c40 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70       sqlite3OsOp
95c50 65 6e 45 78 63 6c 75 73 69 76 65 28 29 3b 0a 2a  enExclusive();.*
95c60 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 73  *.** These calls
95c70 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74   correspond to t
95c80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d  he following com
95c90 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66 6c 61  binations of fla
95ca0 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 65  gs:.**.**     Re
95cb0 61 64 57 72 69 74 65 28 29 20 2d 3e 20 20 20 20  adWrite() ->    
95cc0 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52   (READWRITE | CR
95cd0 45 41 54 45 29 0a 2a 2a 20 20 20 20 20 52 65 61  EATE).**     Rea
95ce0 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20 20 20  dOnly()  ->     
95cf0 28 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 20  (READONLY) .**  
95d00 20 20 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65     OpenExclusive
95d10 28 29 20 2d 3e 20 28 52 45 41 44 57 52 49 54 45  () -> (READWRITE
95d20 20 7c 20 43 52 45 41 54 45 20 7c 20 45 58 43 4c   | CREATE | EXCL
95d30 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 65  USIVE).**.** The
95d40 20 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75 73 69   old OpenExclusi
95d50 76 65 28 29 20 61 63 63 65 70 74 65 64 20 61 20  ve() accepted a 
95d60 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74  boolean argument
95d70 20 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20 49 66   - "delFlag". If
95d80 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 66 69  .** true, the fi
95d90 6c 65 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65  le was configure
95da0 64 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 74 69  d to be automati
95db0 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 77 68  cally deleted wh
95dc0 65 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 68  en the.** file h
95dd0 61 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f  andle closed. To
95de0 20 61 63 68 69 65 76 65 20 74 68 65 20 73 61 6d   achieve the sam
95df0 65 20 65 66 66 65 63 74 20 75 73 69 6e 67 20 74  e effect using t
95e00 68 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 65  his new .** inte
95e10 72 66 61 63 65 2c 20 61 64 64 20 74 68 65 20 44  rface, add the D
95e20 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 6c 61  ELETEONCLOSE fla
95e30 67 20 74 6f 20 74 68 6f 73 65 20 73 70 65 63 69  g to those speci
95e40 66 69 65 64 20 61 62 6f 76 65 20 66 6f 72 20 0a  fied above for .
95e50 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65  ** OpenExclusive
95e60 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ()..*/.static in
95e70 74 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20 73 71  t unixOpen(.  sq
95e80 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
95e90 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
95ea0 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c 69 74 65  zPath, .  sqlite
95eb0 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 0a 20  3_file *pFile,. 
95ec0 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e   int flags,.  in
95ed0 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a  t *pOutFlags.){.
95ee0 20 20 69 6e 74 20 66 64 20 3d 20 30 3b 20 20 20    int fd = 0;   
95ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
95f00 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70   /* File descrip
95f10 74 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20  tor returned by 
95f20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20  open() */.  int 
95f30 64 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 20 20  dirfd = -1;     
95f40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69             /* Di
95f50 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73  rectory file des
95f60 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  criptor */.  int
95f70 20 6f 66 6c 61 67 73 20 3d 20 30 3b 20 20 20 20   oflags = 0;    
95f80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
95f90 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f 20  lags to pass to 
95fa0 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20  open() */.  int 
95fb0 65 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30 78  eType = flags&0x
95fc0 46 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 79  FFFFFF00;  /* Ty
95fd0 70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70  pe of file to op
95fe0 65 6e 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45  en */..  int isE
95ff0 78 63 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61  xclusive  = (fla
96000 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
96010 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69  _EXCLUSIVE);.  i
96020 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 20 20  nt isDelete     
96030 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
96040 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
96050 4c 4f 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43  LOSE);.  int isC
96060 72 65 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61  reate     = (fla
96070 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
96080 5f 43 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20  _CREATE);.  int 
96090 69 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28  isReadonly   = (
960a0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
960b0 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20  PEN_READONLY);. 
960c0 20 69 6e 74 20 69 73 52 65 61 64 57 72 69 74 65   int isReadWrite
960d0 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c    = (flags & SQL
960e0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
960f0 54 45 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72  TE);..  /* If cr
96100 65 61 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20  eating a master 
96110 6f 72 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75  or main-file jou
96120 72 6e 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74  rnal, this funct
96130 69 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20  ion will open.  
96140 2a 2a 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69  ** a file-descri
96150 70 74 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65  ptor on the dire
96160 63 74 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66  ctory too. The f
96170 69 72 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79  irst time unixSy
96180 6e 63 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c  nc().  ** is cal
96190 6c 65 64 20 74 68 65 20 64 69 72 65 63 74 6f 72  led the director
961a0 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  y file descripto
961b0 72 20 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28  r will be fsync(
961c0 29 65 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64  )ed and close()d
961d0 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f  ..  */.  int isO
961e0 70 65 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28  penDirectory = (
961f0 69 73 43 72 65 61 74 65 20 26 26 20 0a 20 20 20  isCreate && .   
96200 20 20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54     (eType==SQLIT
96210 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
96220 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d  URNAL || eType==
96230 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
96240 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a  _JOURNAL).  );..
96250 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66    /* Check the f
96260 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65  ollowing stateme
96270 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20  nts are true: . 
96280 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45   **.  **   (a) E
96290 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68  xactly one of th
962a0 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20  e READWRITE and 
962b0 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d  READONLY flags m
962c0 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20  ust be set, and 
962d0 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43  .  **   (b) if C
962e0 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68  REATE is set, th
962f0 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73  en READWRITE mus
96300 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61  t also be set, a
96310 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66  nd.  **   (c) if
96320 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65   EXCLUSIVE is se
96330 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d  t, then CREATE m
96340 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e  ust also be set.
96350 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44  .  **   (d) if D
96360 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20  ELETEONCLOSE is 
96370 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45  set, then CREATE
96380 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65   must also be se
96390 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  t..  */.  assert
963a0 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20  ((isReadonly==0 
963b0 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d  || isReadWrite==
963c0 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69  0) && (isReadWri
963d0 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79  te || isReadonly
963e0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43  ));.  assert(isC
963f0 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65  reate==0 || isRe
96400 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65  adWrite);.  asse
96410 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d  rt(isExclusive==
96420 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a  0 || isCreate);.
96430 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74    assert(isDelet
96440 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65  e==0 || isCreate
96450 29 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61  );...  /* The ma
96460 69 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72  in DB, main jour
96470 6e 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20  nal, and master 
96480 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65  journal are neve
96490 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  r automatically.
964a0 20 20 2a 2a 20 64 65 6c 65 74 65 64 0a 20 20 2a    ** deleted.  *
964b0 2f 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70  /.  assert( eTyp
964c0 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e!=SQLITE_OPEN_M
964d0 41 49 4e 5f 44 42 20 7c 7c 20 21 69 73 44 65 6c  AIN_DB || !isDel
964e0 65 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ete );.  assert(
964f0 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f   eType!=SQLITE_O
96500 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
96510 20 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b   || !isDelete );
96520 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65  .  assert( eType
96530 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  !=SQLITE_OPEN_MA
96540 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20  STER_JOURNAL || 
96550 21 69 73 44 65 6c 65 74 65 20 29 3b 0a 0a 20 20  !isDelete );..  
96560 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74  /* Assert that t
96570 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 68  he upper layer h
96580 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 74 68  as set one of th
96590 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20 66 6c  e "file-type" fl
965a0 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ags. */.  assert
965b0 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  ( eType==SQLITE_
965c0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20  OPEN_MAIN_DB    
965d0 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
965e0 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20  TE_OPEN_TEMP_DB 
965f0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
96600 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
96610 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54  IN_JOURNAL || eT
96620 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
96630 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20  _TEMP_JOURNAL . 
96640 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d        || eType==
96650 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a  SQLITE_OPEN_SUBJ
96660 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70  OURNAL   || eTyp
96670 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e==SQLITE_OPEN_M
96680 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20  ASTER_JOURNAL . 
96690 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d        || eType==
966a0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e  SQLITE_OPEN_TRAN
966b0 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a 0a 20  SIENT_DB.  );.. 
966c0 20 69 66 28 20 69 73 52 65 61 64 6f 6e 6c 79 20   if( isReadonly 
966d0 29 20 20 6f 66 6c 61 67 73 20 7c 3d 20 4f 5f 52  )  oflags |= O_R
966e0 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52  DONLY;.  if( isR
966f0 65 61 64 57 72 69 74 65 20 29 20 6f 66 6c 61 67  eadWrite ) oflag
96700 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20 69  s |= O_RDWR;.  i
96710 66 28 20 69 73 43 72 65 61 74 65 20 29 20 20 20  f( isCreate )   
96720 20 6f 66 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 45   oflags |= O_CRE
96730 41 54 3b 0a 20 20 69 66 28 20 69 73 45 78 63 6c  AT;.  if( isExcl
96740 75 73 69 76 65 20 29 20 6f 66 6c 61 67 73 20 7c  usive ) oflags |
96750 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f  = (O_EXCL|O_NOFO
96760 4c 4c 4f 57 29 3b 0a 20 20 6f 66 6c 61 67 73 20  LLOW);.  oflags 
96770 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c 45 7c  |= (O_LARGEFILE|
96780 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20 6d 65  O_BINARY);..  me
96790 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73  mset(pFile, 0, s
967a0 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29  izeof(unixFile))
967b0 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 50  ;.  fd = open(zP
967c0 61 74 68 2c 20 6f 66 6c 61 67 73 2c 20 69 73 44  ath, oflags, isD
967d0 65 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54  elete?0600:SQLIT
967e0 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50  E_DEFAULT_FILE_P
967f0 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 69  ERMISSIONS);.  i
96800 66 28 20 66 64 3c 30 20 26 26 20 65 72 72 6e 6f  f( fd<0 && errno
96810 21 3d 45 49 53 44 49 52 20 26 26 20 69 73 52 65  !=EISDIR && isRe
96820 61 64 57 72 69 74 65 20 26 26 20 21 69 73 45 78  adWrite && !isEx
96830 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 2f  clusive ){.    /
96840 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e  * Failed to open
96850 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 65   the file for re
96860 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e  ad/write access.
96870 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20   Try read-only. 
96880 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 26 3d 20  */.    flags &= 
96890 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  ~(SQLITE_OPEN_RE
968a0 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f  ADWRITE|SQLITE_O
968b0 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 20  PEN_CREATE);.   
968c0 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45   flags |= SQLITE
968d0 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a  _OPEN_READONLY;.
968e0 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78 4f      return unixO
968f0 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c  pen(pVfs, zPath,
96900 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70   pFile, flags, p
96910 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 20  OutFlags);.  }. 
96920 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20   if( fd<0 ){.   
96930 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
96940 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 69  ANTOPEN;.  }.  i
96950 66 28 20 69 73 44 65 6c 65 74 65 20 29 7b 0a 20  f( isDelete ){. 
96960 20 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29     unlink(zPath)
96970 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74  ;.  }.  if( pOut
96980 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f  Flags ){.    *pO
96990 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b  utFlags = flags;
969a0 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 66  .  }..  assert(f
969b0 64 21 3d 30 29 3b 0a 20 20 69 66 28 20 69 73 4f  d!=0);.  if( isO
969c0 70 65 6e 44 69 72 65 63 74 6f 72 79 20 29 7b 0a  penDirectory ){.
969d0 20 20 20 20 69 6e 74 20 72 63 20 3d 20 6f 70 65      int rc = ope
969e0 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68  nDirectory(zPath
969f0 2c 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20 69  , &dirfd);.    i
96a00 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
96a10 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28   ){.      close(
96a20 66 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  fd);.      retur
96a30 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
96a40 20 20 72 65 74 75 72 6e 20 66 69 6c 6c 49 6e 55    return fillInU
96a50 6e 69 78 46 69 6c 65 28 66 64 2c 20 64 69 72 66  nixFile(fd, dirf
96a60 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 29  d, pFile, zPath)
96a70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
96a80 65 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50  e the file at zP
96a90 61 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53  ath. If the dirS
96aa0 79 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ync argument is 
96ab0 74 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a  true, fsync().**
96ac0 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61   the directory a
96ad0 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68  fter deleting th
96ae0 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
96af0 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65  c int unixDelete
96b00 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
96b10 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  fs, const char *
96b20 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79  zPath, int dirSy
96b30 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  nc){.  int rc = 
96b40 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 53 69 6d  SQLITE_OK;.  Sim
96b50 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74  ulateIOError(ret
96b60 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
96b70 5f 44 45 4c 45 54 45 29 3b 0a 20 20 75 6e 6c 69  _DELETE);.  unli
96b80 6e 6b 28 7a 50 61 74 68 29 3b 0a 20 20 69 66 28  nk(zPath);.  if(
96b90 20 64 69 72 53 79 6e 63 20 29 7b 0a 20 20 20 20   dirSync ){.    
96ba0 69 6e 74 20 66 64 3b 0a 20 20 20 20 72 63 20 3d  int fd;.    rc =
96bb0 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a   openDirectory(z
96bc0 50 61 74 68 2c 20 26 66 64 29 3b 0a 20 20 20 20  Path, &fd);.    
96bd0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
96be0 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 66  K ){.      if( f
96bf0 73 79 6e 63 28 66 64 29 20 29 7b 0a 20 20 20 20  sync(fd) ){.    
96c00 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
96c10 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 3b  IOERR_DIR_FSYNC;
96c20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
96c30 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 7d 0a  lose(fd);.    }.
96c40 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
96c50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74  .}../*.** Test t
96c60 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20  he existance of 
96c70 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69 73  or access permis
96c80 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a 50  sions of file zP
96c90 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73 74  ath. The.** test
96ca0 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65 6e   performed depen
96cb0 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ds on the value 
96cc0 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20  of flags:.**.** 
96cd0 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53      SQLITE_ACCES
96ce0 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72 6e  S_EXISTS: Return
96cf0 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 65   1 if the file e
96d00 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51 4c  xists.**     SQL
96d10 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
96d20 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20 69  RITE: Return 1 i
96d30 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65  f the file is re
96d40 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2e  ad and writable.
96d50 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41  .**     SQLITE_A
96d60 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a 20  CCESS_READONLY: 
96d70 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
96d80 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65  file is readable
96d90 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
96da0 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73  e return 0..*/.s
96db0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41 63  tatic int unixAc
96dc0 63 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73  cess(sqlite3_vfs
96dd0 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68   *pVfs, const ch
96de0 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20 66  ar *zPath, int f
96df0 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 61 6d 6f  lags){.  int amo
96e00 64 65 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c  de;.  switch( fl
96e10 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ags ){.    case 
96e20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
96e30 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d 6f 64  ISTS:.      amod
96e40 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20 20 20  e = F_OK;.      
96e50 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
96e60 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
96e70 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 61  ADWRITE:.      a
96e80 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b  mode = W_OK|R_OK
96e90 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
96ea0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
96eb0 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20  CCESS_READ:.    
96ec0 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b 3b 0a    amode = R_OK;.
96ed0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20        break;..  
96ee0 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
96ef0 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 6c 69   assert(!"Invali
96f00 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  d flags argument
96f10 22 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ");.  }.  return
96f20 20 28 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20   (access(zPath, 
96f30 61 6d 6f 64 65 29 3d 3d 30 29 3b 0a 7d 0a 0a 2f  amode)==0);.}../
96f40 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65  *.** Create a te
96f50 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d  mporary file nam
96f60 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66  e in zBuf.  zBuf
96f70 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74   must be allocat
96f80 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c  ed.** by the cal
96f90 6c 69 6e 67 20 70 72 6f 63 65 73 73 20 61 6e 64  ling process and
96fa0 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f   must be big eno
96fb0 75 67 68 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c  ugh to hold at l
96fc0 65 61 73 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78  east.** pVfs->mx
96fd0 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a  Pathname bytes..
96fe0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
96ff0 69 78 47 65 74 54 65 6d 70 4e 61 6d 65 28 73 71  ixGetTempName(sq
97000 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
97010 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20   char *zBuf){.  
97020 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
97030 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a  r *azDirs[] = {.
97040 20 20 20 20 20 30 2c 0a 20 20 20 20 20 22 2f 76       0,.     "/v
97050 61 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f  ar/tmp",.     "/
97060 75 73 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22  usr/tmp",.     "
97070 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2e 22 2c  /tmp",.     ".",
97080 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  .  };.  static c
97090 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
970a0 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20  ar zChars[] =.  
970b0 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d    "abcdefghijklm
970c0 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20  nopqrstuvwxyz". 
970d0 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c     "ABCDEFGHIJKL
970e0 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a  MNOPQRSTUVWXYZ".
970f0 20 20 20 20 22 30 31 32 33 34 35 36 37 38 39 22      "0123456789"
97100 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
97110 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b  struct stat buf;
97120 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
97130 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a  Dir = ".";..  /*
97140 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d   It's odd to sim
97150 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f  ulate an io-erro
97160 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c  r here, but real
97170 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a  ly this is just.
97180 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69    ** using the i
97190 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72  o-error infrastr
971a0 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74  ucture to test t
971b0 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c  hat SQLite handl
971c0 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e  es this.  ** fun
971d0 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a  ction failing. .
971e0 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49    */.  SimulateI
971f0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
97200 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a  QLITE_ERROR );..
97210 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71    azDirs[0] = sq
97220 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
97230 74 6f 72 79 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  tory;.  for(i=0;
97240 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 69 72 73   i<sizeof(azDirs
97250 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 72 73 5b  )/sizeof(azDirs[
97260 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  0]); i++){.    i
97270 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d 3d 30 20  f( azDirs[i]==0 
97280 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
97290 69 66 28 20 73 74 61 74 28 61 7a 44 69 72 73 5b  if( stat(azDirs[
972a0 69 5d 2c 20 26 62 75 66 29 20 29 20 63 6f 6e 74  i], &buf) ) cont
972b0 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 21 53  inue;.    if( !S
972c0 5f 49 53 44 49 52 28 62 75 66 2e 73 74 5f 6d 6f  _ISDIR(buf.st_mo
972d0 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  de) ) continue;.
972e0 20 20 20 20 69 66 28 20 61 63 63 65 73 73 28 61      if( access(a
972f0 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 20 29 20  zDirs[i], 07) ) 
97300 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 44  continue;.    zD
97310 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 5d 3b 0a  ir = azDirs[i];.
97320 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
97330 20 64 6f 7b 0a 20 20 20 20 61 73 73 65 72 74 28   do{.    assert(
97340 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
97350 65 3d 3d 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20  e==MAX_PATHNAME 
97360 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
97370 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48  nprintf(MAX_PATH
97380 4e 41 4d 45 2d 31 37 2c 20 7a 42 75 66 2c 20 22  NAME-17, zBuf, "
97390 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f  %s/"SQLITE_TEMP_
973a0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44 69  FILE_PREFIX, zDi
973b0 72 29 3b 0a 20 20 20 20 6a 20 3d 20 73 74 72 6c  r);.    j = strl
973c0 65 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 71  en(zBuf);.    sq
973d0 6c 69 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28  lite3Randomness(
973e0 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20  15, &zBuf[j]);. 
973f0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35     for(i=0; i<15
97400 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20  ; i++, j++){.   
97410 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68     zBuf[j] = (ch
97420 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73  ar)zChars[ ((uns
97430 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b  igned char)zBuf[
97440 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61  j])%(sizeof(zCha
97450 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a  rs)-1) ];.    }.
97460 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b      zBuf[j] = 0;
97470 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73  .  }while( acces
97480 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a  s(zBuf,0)==0 );.
97490 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
974a0 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75  OK;.}.../*.** Tu
974b0 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  rn a relative pa
974c0 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75  thname into a fu
974d0 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54 68 65  ll pathname. The
974e0 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 0a 2a   relative path.*
974f0 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61  * is stored as a
97500 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
97510 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 62 75  string in the bu
97520 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20  ffer pointed to 
97530 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a 2a 2a  by.** zPath. .**
97540 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74 73 20  .** zOut points 
97550 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 61  to a buffer of a
97560 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65 33 5f  t least sqlite3_
97570 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 20 62  vfs.mxPathname b
97580 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74 68 69  ytes .** (in thi
97590 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41 54 48  s case, MAX_PATH
975a0 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54 68 65  NAME bytes). The
975b0 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20 77 72   full-path is wr
975c0 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68 69 73  itten to.** this
975d0 20 62 75 66 66 65 72 20 62 65 66 6f 72 65 20 72   buffer before r
975e0 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61  eturning..*/.sta
975f0 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75 6c 6c  tic int unixFull
97600 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65 33  Pathname(sqlite3
97610 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73  _vfs *pVfs, cons
97620 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 63  t char *zPath, c
97630 68 61 72 20 2a 7a 4f 75 74 29 7b 0a 0a 20 20 2f  har *zOut){..  /
97640 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69  * It's odd to si
97650 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72  mulate an io-err
97660 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61  or here, but rea
97670 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74  lly this is just
97680 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  .  ** using the 
97690 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74  io-error infrast
976a0 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20  ructure to test 
976b0 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64  that SQLite hand
976c0 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75  les this.  ** fu
976d0 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20  nction failing. 
976e0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
976f0 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72  uld fail if, for
97700 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20   example, the.  
97710 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69  ** current worki
97720 6e 67 20 64 69 72 65 63 74 6c 79 20 68 61 73 20  ng directly has 
97730 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20  been unlinked.. 
97740 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   */.  SimulateIO
97750 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
97760 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20  LITE_ERROR );.. 
97770 20 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e 6d   assert( pVfs->m
97780 78 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50  xPathname==MAX_P
97790 41 54 48 4e 41 4d 45 20 29 3b 0a 20 20 7a 4f 75  ATHNAME );.  zOu
977a0 74 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2d 31  t[MAX_PATHNAME-1
977b0 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20  ] = '\0';.  if( 
977c0 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b  zPath[0]=='/' ){
977d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
977e0 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41  rintf(MAX_PATHNA
977f0 4d 45 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20  ME, zOut, "%s", 
97800 7a 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b  zPath);.  }else{
97810 0a 20 20 20 20 69 6e 74 20 6e 43 77 64 3b 0a 20  .    int nCwd;. 
97820 20 20 20 69 66 28 20 67 65 74 63 77 64 28 7a 4f     if( getcwd(zO
97830 75 74 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ut, MAX_PATHNAME
97840 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  -1)==0 ){.      
97850 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
97860 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20  NTOPEN;.    }.  
97870 20 20 6e 43 77 64 20 3d 20 73 74 72 6c 65 6e 28    nCwd = strlen(
97880 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  zOut);.    sqlit
97890 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f  e3_snprintf(MAX_
978a0 50 41 54 48 4e 41 4d 45 2d 6e 43 77 64 2c 20 26  PATHNAME-nCwd, &
978b0 7a 4f 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 73  zOut[nCwd], "/%s
978c0 22 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d 0a 20  ", zPath);.  }. 
978d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
978e0 4b 3b 0a 0a 23 69 66 20 30 0a 20 20 2f 2a 0a 20  K;..#if 0.  /*. 
978f0 20 2a 2a 20 52 65 6d 6f 76 65 20 22 2f 2e 2f 22   ** Remove "/./"
97900 20 70 61 74 68 20 65 6c 65 6d 65 6e 74 73 20 61   path elements a
97910 6e 64 20 63 6f 6e 76 65 72 74 20 22 2f 41 2f 2e  nd convert "/A/.
97920 2f 22 20 70 61 74 68 20 65 6c 65 6d 65 6e 74 73  /" path elements
97930 0a 20 20 2a 2a 20 74 6f 20 6a 75 73 74 20 22 2f  .  ** to just "/
97940 22 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46  "..  */.  if( zF
97950 75 6c 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ull ){.    int i
97960 2c 20 6a 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6a  , j;.    for(i=j
97970 3d 30 3b 20 7a 46 75 6c 6c 5b 69 5d 3b 20 69 2b  =0; zFull[i]; i+
97980 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 46  +){.      if( zF
97990 75 6c 6c 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20  ull[i]=='/' ){. 
979a0 20 20 20 20 20 20 20 69 66 28 20 7a 46 75 6c 6c         if( zFull
979b0 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e  [i+1]=='/' ) con
979c0 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69  tinue;.        i
979d0 66 28 20 7a 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27  f( zFull[i+1]=='
979e0 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d  .' && zFull[i+2]
979f0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20  =='/' ){.       
97a00 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20     i += 1;.     
97a10 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
97a20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
97a30 20 69 66 28 20 7a 46 75 6c 6c 5b 69 2b 31 5d 3d   if( zFull[i+1]=
97a40 3d 27 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69 2b  ='.' && zFull[i+
97a50 32 5d 3d 3d 27 2e 27 20 26 26 20 7a 46 75 6c 6c  2]=='.' && zFull
97a60 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20  [i+3]=='/' ){.  
97a70 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a          while( j
97a80 3e 30 20 26 26 20 7a 46 75 6c 6c 5b 6a 2d 31 5d  >0 && zFull[j-1]
97a90 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a  !='/' ){ j--; }.
97aa0 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 33            i += 3
97ab0 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74  ;.          cont
97ac0 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  inue;.        }.
97ad0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 46        }.      zF
97ae0 75 6c 6c 5b 6a 2b 2b 5d 20 3d 20 7a 46 75 6c 6c  ull[j++] = zFull
97af0 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  [i];.    }.    z
97b00 46 75 6c 6c 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d  Full[j] = 0;.  }
97b10 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 23 69 66 6e  .#endif.}...#ifn
97b20 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
97b30 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f  LOAD_EXTENSION./
97b40 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20  *.** Interfaces 
97b50 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68  for opening a sh
97b60 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69  ared library, fi
97b70 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e  nding entry poin
97b80 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  ts.** within the
97b90 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
97ba0 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65   and closing the
97bb0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e   shared library.
97bc0 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c  .*/.#include <dl
97bd0 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f  fcn.h>.static vo
97be0 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73  id *unixDlOpen(s
97bf0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
97c00 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
97c10 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 72 65 74 75  ilename){.  retu
97c20 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e  rn dlopen(zFilen
97c30 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20  ame, RTLD_NOW | 
97c40 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a  RTLD_GLOBAL);.}.
97c50 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61  ./*.** SQLite ca
97c60 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  lls this functio
97c70 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66  n immediately af
97c80 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e  ter a call to un
97c90 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20  ixDlSym() or.** 
97ca0 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69  unixDlOpen() fai
97cb0 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75  ls (returns a nu
97cc0 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20  ll pointer). If 
97cd0 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20  a more detailed 
97ce0 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65  error.** message
97cf0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69   is available, i
97d00 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  t is written to 
97d10 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65  zBufOut. If no e
97d20 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20  rror message.** 
97d30 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42  is available, zB
97d40 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e  ufOut is left un
97d50 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c  modified and SQL
97d60 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61 75  ite uses a defau
97d70 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73  lt.** error mess
97d80 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
97d90 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28  oid unixDlError(
97da0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
97db0 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
97dc0 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 63  r *zBufOut){.  c
97dd0 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 65 6e 74  har *zErr;.  ent
97de0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 7a 45 72  erMutex();.  zEr
97df0 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 3b 0a 20  r = dlerror();. 
97e00 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20   if( zErr ){.   
97e10 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
97e20 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 2c  f(nBuf, zBufOut,
97e30 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20   "%s", zErr);.  
97e40 7d 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29  }.  leaveMutex()
97e50 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
97e60 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74  *unixDlSym(sqlit
97e70 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f  e3_vfs *pVfs, vo
97e80 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e  id *pHandle, con
97e90 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c  st char *zSymbol
97ea0 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 6c 73 79  ){.  return dlsy
97eb0 6d 28 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62  m(pHandle, zSymb
97ec0 6f 6c 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ol);.}.static vo
97ed0 69 64 20 75 6e 69 78 44 6c 43 6c 6f 73 65 28 73  id unixDlClose(s
97ee0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
97ef0 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29  , void *pHandle)
97f00 7b 0a 20 20 64 6c 63 6c 6f 73 65 28 70 48 61 6e  {.  dlclose(pHan
97f10 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a  dle);.}.#else /*
97f20 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
97f30 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69  LOAD_EXTENSION i
97f40 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20  s defined: */.  
97f50 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 4f 70  #define unixDlOp
97f60 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20  en  0.  #define 
97f70 75 6e 69 78 44 6c 45 72 72 6f 72 20 30 0a 20 20  unixDlError 0.  
97f80 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 53 79  #define unixDlSy
97f90 6d 20 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20  m   0.  #define 
97fa0 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30 0a 23 65  unixDlClose 0.#e
97fb0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  ndif../*.** Writ
97fc0 65 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20  e nBuf bytes of 
97fd0 72 61 6e 64 6f 6d 20 64 61 74 61 20 74 6f 20 74  random data to t
97fe0 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66 66  he supplied buff
97ff0 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74  er zBuf..*/.stat
98000 69 63 20 69 6e 74 20 75 6e 69 78 52 61 6e 64 6f  ic int unixRando
98010 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66  mness(sqlite3_vf
98020 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
98030 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
98040 0a 20 20 61 73 73 65 72 74 28 6e 42 75 66 3e 3d  .  assert(nBuf>=
98050 28 73 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b  (sizeof(time_t)+
98060 73 69 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0a 0a  sizeof(int)));..
98070 20 20 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20    /* We have to 
98080 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20  initialize zBuf 
98090 74 6f 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72  to prevent valgr
980a0 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69  ind from reporti
980b0 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20  ng.  ** errors. 
980c0 20 54 68 65 20 72 65 70 6f 72 74 73 20 69 73 73   The reports iss
980d0 75 65 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20  ued by valgrind 
980e0 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20  are incorrect - 
980f0 77 65 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72  we would.  ** pr
98100 65 66 65 72 20 74 68 61 74 20 74 68 65 20 72 61  efer that the ra
98110 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72  ndomness be incr
98120 65 61 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20  eased by making 
98130 75 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  use of the.  ** 
98140 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70  uninitialized sp
98150 61 63 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75  ace in zBuf - bu
98160 74 20 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72  t valgrind error
98170 73 20 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a  s tend to worry.
98180 20 20 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e    ** some users.
98190 20 20 52 61 74 68 65 72 20 74 68 61 6e 20 61 72    Rather than ar
981a0 67 75 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61  gue, it seems ea
981b0 73 69 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69  sier just to ini
981c0 74 69 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65  tialize.  ** the
981d0 20 77 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64   whole array and
981e0 20 73 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e   silence valgrin
981f0 64 2c 20 65 76 65 6e 20 69 66 20 74 68 61 74 20  d, even if that 
98200 6d 65 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f  means less rando
98210 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68  mness.  ** in th
98220 65 20 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20  e random seed.. 
98230 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65   **.  ** When te
98240 73 74 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a  sting, initializ
98250 69 6e 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65  ing zBuf[] to ze
98260 72 6f 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e  ro is all we do.
98270 20 20 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a    That means.  *
98280 2a 20 74 68 61 74 20 77 65 20 61 6c 77 61 79 73  * that we always
98290 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 72 61   use the same ra
982a0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75  ndom number sequ
982b0 65 6e 63 65 2e 20 20 54 68 69 73 20 6d 61 6b 65  ence.  This make
982c0 73 20 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73  s the.  ** tests
982d0 20 72 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a   repeatable..  *
982e0 2f 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c  /.  memset(zBuf,
982f0 20 30 2c 20 6e 42 75 66 29 3b 0a 23 69 66 20 21   0, nBuf);.#if !
98300 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
98310 45 53 54 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74  EST).  {.    int
98320 20 70 69 64 2c 20 66 64 3b 0a 20 20 20 20 66 64   pid, fd;.    fd
98330 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72   = open("/dev/ur
98340 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59  andom", O_RDONLY
98350 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20  );.    if( fd<0 
98360 29 7b 0a 20 20 20 20 20 20 74 69 6d 65 5f 74 20  ){.      time_t 
98370 74 3b 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74  t;.      time(&t
98380 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
98390 7a 42 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66  zBuf, &t, sizeof
983a0 28 74 29 29 3b 0a 20 20 20 20 20 20 70 69 64 20  (t));.      pid 
983b0 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20 20 20  = getpid();.    
983c0 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 73    memcpy(&zBuf[s
983d0 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c  izeof(t)], &pid,
983e0 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20   sizeof(pid));. 
983f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
98400 72 65 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e  read(fd, zBuf, n
98410 42 75 66 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73  Buf);.      clos
98420 65 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  e(fd);.    }.  }
98430 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
98440 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
98450 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20  /*.** Sleep for 
98460 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20  a little while. 
98470 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
98480 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74  nt of time slept
98490 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  ..** The argumen
984a0 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
984b0 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  of microseconds 
984c0 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70  we want to sleep
984d0 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  ..** The return 
984e0 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d  value is the num
984f0 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
98500 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74  nds of sleep act
98510 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74  ually.** request
98520 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65  ed from the unde
98530 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
98540 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65   system, a numbe
98550 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74  r which.** might
98560 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
98570 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
98580 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e   argument, but n
98590 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ot less.** than 
985a0 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  the argument..*/
985b0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
985c0 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66  Sleep(sqlite3_vf
985d0 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63  s *pVfs, int mic
985e0 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20  roseconds){.#if 
985f0 64 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c  defined(HAVE_USL
98600 45 45 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c  EEP) && HAVE_USL
98610 45 45 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63  EEP.  usleep(mic
98620 72 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 20 72 65  roseconds);.  re
98630 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64  turn microsecond
98640 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73  s;.#else.  int s
98650 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72 6f 73  econds = (micros
98660 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29 2f 31  econds+999999)/1
98670 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65 70 28  000000;.  sleep(
98680 73 65 63 6f 6e 64 73 29 3b 0a 20 20 72 65 74 75  seconds);.  retu
98690 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30  rn seconds*10000
986a0 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  00;.#endif.}../*
986b0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
986c0 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73  g variable, if s
986d0 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  et to a non-zero
986e0 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20   value, becomes 
986f0 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65  the result.** re
98700 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  turned from sqli
98710 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
98720 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  ().  This is use
98730 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a  d for testing..*
98740 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
98750 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
98760 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72  int sqlite3_curr
98770 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65  ent_time = 0;.#e
98780 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ndif../*.** Find
98790 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d   the current tim
987a0 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20  e (in Universal 
987b0 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65  Coordinated Time
987c0 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a  ).  Write the.**
987d0 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e   current time an
987e0 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69  d date as a Juli
987f0 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e  an Day number in
98800 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a  to *prNow and.**
98810 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75   return 0.  Retu
98820 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65  rn 1 if the time
98830 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74   and date cannot
98840 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74   be found..*/.st
98850 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72  atic int unixCur
98860 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33  rentTime(sqlite3
98870 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62  _vfs *pVfs, doub
98880 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 64  le *prNow){.#ifd
98890 65 66 20 4e 4f 5f 47 45 54 54 4f 44 0a 20 20 74  ef NO_GETTOD.  t
988a0 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 28  ime_t t;.  time(
988b0 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20  &t);.  *prNow = 
988c0 74 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30  t/86400.0 + 2440
988d0 35 38 37 2e 35 3b 0a 23 65 6c 73 65 0a 20 20 73  587.5;.#else.  s
988e0 74 72 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e  truct timeval sN
988f0 6f 77 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64  ow;.  gettimeofd
98900 61 79 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20  ay(&sNow, 0);.  
98910 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37  *prNow = 2440587
98920 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63  .5 + sNow.tv_sec
98930 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e  /86400.0 + sNow.
98940 74 76 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30  tv_usec/86400000
98950 30 30 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 23 69  000.0;.#endif.#i
98960 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
98970 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63  .  if( sqlite3_c
98980 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20  urrent_time ){. 
98990 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69     *prNow = sqli
989a0 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
989b0 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35  /86400.0 + 24405
989c0 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  87.5;.  }.#endif
989d0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
989e0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
989f0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 71  ointer to the sq
98a00 6c 69 74 65 33 44 65 66 61 75 6c 74 56 66 73 20  lite3DefaultVfs 
98a10 73 74 72 75 63 74 75 72 65 2e 20 20 20 57 65 20  structure.   We 
98a20 75 73 65 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f  use.** a functio
98a30 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 69  n rather than gi
98a40 76 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65  ve the structure
98a50 20 67 6c 6f 62 61 6c 20 73 63 6f 70 65 20 62 65   global scope be
98a60 63 61 75 73 65 0a 2a 2a 20 73 6f 6d 65 20 63 6f  cause.** some co
98a70 6d 70 69 6c 65 72 73 20 28 4d 53 56 43 29 20 64  mpilers (MSVC) d
98a80 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 66 6f 72 77  o not allow forw
98a90 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
98aa0 20 6f 66 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a   of.** initializ
98ab0 65 64 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a  ed structures..*
98ac0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
98ad0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71   sqlite3_vfs *sq
98ae0 6c 69 74 65 33 4f 73 44 65 66 61 75 6c 74 56 66  lite3OsDefaultVf
98af0 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  s(void){.  stati
98b00 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 75 6e  c sqlite3_vfs un
98b10 69 78 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c  ixVfs = {.    1,
98b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98b30 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f    /* iVersion */
98b40 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 78  .    sizeof(unix
98b50 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73  File),   /* szOs
98b60 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58 5f  File */.    MAX_
98b70 50 41 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 20  PATHNAME,       
98b80 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f  /* mxPathname */
98b90 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
98ba0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78           /* pNex
98bb0 74 20 2a 2f 0a 20 20 20 20 22 75 6e 69 78 22 2c  t */.    "unix",
98bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
98bd0 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20  zName */.    0, 
98be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98bf0 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a   /* pAppData */.
98c00 20 20 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e 2c    .    unixOpen,
98c10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
98c20 70 65 6e 20 2a 2f 0a 20 20 20 20 75 6e 69 78 44  pen */.    unixD
98c30 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f  elete,         /
98c40 2a 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20  * xDelete */.   
98c50 20 75 6e 69 78 41 63 63 65 73 73 2c 20 20 20 20   unixAccess,    
98c60 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20       /* xAccess 
98c70 2a 2f 0a 20 20 20 20 75 6e 69 78 47 65 74 54 65  */.    unixGetTe
98c80 6d 70 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 78 47  mpName,    /* xG
98c90 65 74 54 65 6d 70 4e 61 6d 65 20 2a 2f 0a 20 20  etTempName */.  
98ca0 20 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61    unixFullPathna
98cb0 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61  me,   /* xFullPa
98cc0 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 6e  thname */.    un
98cd0 69 78 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20  ixDlOpen,       
98ce0 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a    /* xDlOpen */.
98cf0 20 20 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c      unixDlError,
98d00 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72          /* xDlEr
98d10 72 6f 72 20 2a 2f 0a 20 20 20 20 75 6e 69 78 44  ror */.    unixD
98d20 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f  lSym,          /
98d30 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20  * xDlSym */.    
98d40 75 6e 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20  unixDlClose,    
98d50 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20      /* xDlClose 
98d60 2a 2f 0a 20 20 20 20 75 6e 69 78 52 61 6e 64 6f  */.    unixRando
98d70 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52  mness,     /* xR
98d80 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20  andomness */.   
98d90 20 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20   unixSleep,     
98da0 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a       /* xSleep *
98db0 2f 0a 20 20 20 20 75 6e 69 78 43 75 72 72 65 6e  /.    unixCurren
98dc0 74 54 69 6d 65 20 20 20 20 20 2f 2a 20 78 43 75  tTime     /* xCu
98dd0 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20 7d  rrentTime */.  }
98de0 3b 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 26 75  ;.  .  return &u
98df0 6e 69 78 56 66 73 3b 0a 7d 0a 20 0a 23 65 6e 64  nixVfs;.}. .#end
98e00 69 66 20 2f 2a 20 4f 53 5f 55 4e 49 58 20 2a 2f  if /* OS_UNIX */
98e10 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
98e20 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78  * End of os_unix
98e30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
98e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98e60 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
98e70 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
98e80 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  win.c **********
98e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98eb0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
98ec0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
98ed0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
98ee0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
98ef0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
98f00 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
98f10 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
98f20 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
98f30 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
98f40 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
98f50 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
98f60 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
98f70 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
98f80 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
98f90 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
98fa0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
98fb0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
98fc0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
98fd0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
98fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99020 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
99030 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
99040 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70   code that is sp
99050 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77  ecific to window
99060 73 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 57 49 4e  s..*/.#if OS_WIN
99070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
99080 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 75  * This file is u
99090 73 65 64 20 66 6f 72 20 77 69 6e 64 6f 77 73 20  sed for windows 
990a0 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  only */.../*.** 
990b0 41 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d  A Note About Mem
990c0 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a  ory Allocation:.
990d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65  **.** This drive
990e0 72 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f  r uses malloc()/
990f0 66 72 65 65 28 29 20 64 69 72 65 63 74 6c 79 20  free() directly 
99100 72 61 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e  rather than goin
99110 67 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65  g through.** the
99120 20 53 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73   SQLite-wrappers
99130 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
99140 29 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  )/sqlite3_free()
99150 2e 20 20 54 68 6f 73 65 20 77 72 61 70 70 65 72  .  Those wrapper
99160 73 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65  s.** are designe
99170 64 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62  d for use on emb
99180 65 64 64 65 64 20 73 79 73 74 65 6d 73 20 77 68  edded systems wh
99190 65 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63  ere memory is sc
991a0 61 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c  arce and.** mall
991b0 6f 63 20 66 61 69 6c 75 72 65 73 20 68 61 70 70  oc failures happ
991c0 65 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20  en frequently.  
991d0 57 69 6e 33 32 20 64 6f 65 73 20 6e 6f 74 20 74  Win32 does not t
991e0 79 70 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a  ypically run on.
991f0 2a 2a 20 65 6d 62 65 64 64 65 64 20 73 79 73 74  ** embedded syst
99200 65 6d 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74  ems, and when it
99210 20 64 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f   does the develo
99220 70 65 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61  pers normally ha
99230 76 65 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f  ve bigger.** pro
99240 62 6c 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61  blems to worry a
99250 62 6f 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e  bout than runnin
99260 67 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e  g out of memory.
99270 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f    So there is no
99280 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e  t.** a compellin
99290 67 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68  g need to use th
992a0 65 20 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a  e wrappers..**.*
992b0 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61  * But there is a
992c0 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20   good reason to 
992d0 6e 6f 74 20 75 73 65 20 74 68 65 20 77 72 61 70  not use the wrap
992e0 70 65 72 73 2e 20 20 49 66 20 77 65 20 75 73 65  pers.  If we use
992f0 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73   the.** wrappers
99300 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65   then we will ge
99310 74 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c  t simulated mall
99320 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 77 69  oc() failures wi
99330 74 68 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69  thin this.** dri
99340 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63  ver.  And that c
99350 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20  auses all kinds 
99360 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  of problems for 
99370 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a  our tests.  We.*
99380 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20  * could enhance 
99390 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77  SQLite to deal w
993a0 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61  ith simulated ma
993b0 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69  lloc failures wi
993c0 74 68 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64  thin.** the OS d
993d0 72 69 76 65 72 2c 20 62 75 74 20 74 68 65 20 63  river, but the c
993e0 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68  ode to deal with
993f0 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77   those failure w
99400 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65  ould not.** be e
99410 78 65 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75  xercised on Linu
99420 78 20 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f  x (which does no
99430 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63  t need to malloc
99440 28 29 20 69 6e 20 74 68 65 20 64 72 69 76 65 72  () in the driver
99450 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77  ).** and so we w
99460 6f 75 6c 64 20 68 61 76 65 20 64 69 66 66 69 63  ould have diffic
99470 75 6c 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76  ulty writing cov
99480 65 72 61 67 65 20 74 65 73 74 73 20 66 6f 72 20  erage tests for 
99490 74 68 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42  that.** code.  B
994a0 65 74 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74  etter to leave t
994b0 68 65 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20  he code out, we 
994c0 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  think..**.** The
994d0 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64   point of this d
994e0 69 73 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20  iscussion is as 
994f0 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63  follows:  When c
99500 72 65 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a  reating a new.**
99510 20 4f 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e   OS layer for an
99520 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
99530 2c 20 69 66 20 79 6f 75 20 75 73 65 20 74 68 69  , if you use thi
99540 73 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61  s file as an exa
99550 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74  mple,.** avoid t
99560 68 65 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63  he use of malloc
99570 28 29 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73  ()/free().  Thos
99580 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20  e routines work 
99590 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77 73 0a 2a 2a  ok on windows.**
995a0 20 64 65 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f   desktops but no
995b0 74 20 73 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62  t so well in emb
995c0 65 64 64 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a  edded systems..*
995d0 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 77 69 6e  /..#include <win
995e0 62 61 73 65 2e 68 3e 0a 0a 23 69 66 64 65 66 20  base.h>..#ifdef 
995f0 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23 20 69 6e 63  __CYGWIN__.# inc
99600 6c 75 64 65 20 3c 73 79 73 2f 63 79 67 77 69 6e  lude <sys/cygwin
99610 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  .h>.#endif../*.*
99620 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20 74 6f  * Macros used to
99630 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   determine wheth
99640 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65  er or not to use
99650 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66   threads..*/.#if
99660 20 64 65 66 69 6e 65 64 28 54 48 52 45 41 44 53   defined(THREADS
99670 41 46 45 29 20 26 26 20 54 48 52 45 41 44 53 41  AFE) && THREADSA
99680 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  FE.# define SQLI
99690 54 45 5f 57 33 32 5f 54 48 52 45 41 44 53 20 31  TE_W32_THREADS 1
996a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
996b0 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74  nclude code that
996c0 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c   is common to al
996d0 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a  l os_*.c files.*
996e0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
996f0 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d  * Include os_com
99700 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  mon.h in the mid
99710 64 6c 65 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20  dle of os_win.c 
99720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99730 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
99740 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
99750 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
99760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99780 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
99790 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
997a0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
997b0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
997c0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
997d0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
997e0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
997f0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
99800 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
99810 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
99820 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
99830 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
99840 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
99850 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
99860 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
99870 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
99880 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
99890 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
998a0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
998b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
998c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
998d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
998e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
998f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
99900 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
99910 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69   macros and a li
99920 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65  ttle bit of code
99930 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
99940 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65  to.** all of the
99950 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66   platform-specif
99960 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63  ic files (os_*.c
99970 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64  ) and is #includ
99980 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a  ed into those.**
99990 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   files..**.** Th
999a0 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  is file should b
999b0 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74  e #included by t
999c0 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20  he os_*.c files 
999d0 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74  only.  It is not
999e0 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75   a.** general pu
999f0 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c  rpose header fil
99a00 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20  e..*/../*.** At 
99a10 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68  least two bugs h
99a20 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62  ave slipped in b
99a30 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65  ecause we change
99a40 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42  d the MEMORY_DEB
99a50 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53  UG.** macro to S
99a60 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20  QLITE_DEBUG and 
99a70 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66  some older makef
99a80 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65  iles have not ye
99a90 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77  t made the.** sw
99aa0 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  itch.  The follo
99ab0 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64  wing code should
99ac0 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62   catch this prob
99ad0 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lem at compile-t
99ae0 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d  ime..*/.#ifdef M
99af0 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72  EMORY_DEBUG.# er
99b00 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f  ror "The MEMORY_
99b10 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f  DEBUG macro is o
99b20 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51  bsolete.  Use SQ
99b30 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65  LITE_DEBUG inste
99b40 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  ad.".#endif.../*
99b50 0a 20 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67  . * When testing
99b60 2c 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61  , this global va
99b70 72 69 61 62 6c 65 20 73 74 6f 72 65 73 20 74 68  riable stores th
99b80 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68  e location of th
99b90 65 0a 20 2a 20 70 65 6e 64 69 6e 67 2d 62 79 74  e. * pending-byt
99ba0 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
99bb0 65 20 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64  e file.. */.#ifd
99bc0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
99bd0 51 4c 49 54 45 5f 41 50 49 20 75 6e 73 69 67 6e  QLITE_API unsign
99be0 65 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  ed int sqlite3_p
99bf0 65 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78  ending_byte = 0x
99c00 34 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66  40000000;.#endif
99c10 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
99c20 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 41 50 49  DEBUG.SQLITE_API
99c30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f   int sqlite3_os_
99c40 74 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69  trace = 0;.#defi
99c50 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20  ne OSTRACE1(X)  
99c60 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
99c70 65 33 5f 6f 73 5f 74 72 61 63 65 20 29 20 73 71  e3_os_trace ) sq
99c80 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
99c90 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  (X).#define OSTR
99ca0 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20  ACE2(X,Y)       
99cb0 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  if( sqlite3_os_t
99cc0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
99cd0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23  bugPrintf(X,Y).#
99ce0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28  define OSTRACE3(
99cf0 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73  X,Y,Z)     if( s
99d00 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65 20  qlite3_os_trace 
99d10 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
99d20 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66  intf(X,Y,Z).#def
99d30 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59  ine OSTRACE4(X,Y
99d40 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69  ,Z,A)   if( sqli
99d50 74 65 33 5f 6f 73 5f 74 72 61 63 65 20 29 20 73  te3_os_trace ) s
99d60 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
99d70 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69  f(X,Y,Z,A).#defi
99d80 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c  ne OSTRACE5(X,Y,
99d90 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74  Z,A,B) if( sqlit
99da0 65 33 5f 6f 73 5f 74 72 61 63 65 20 29 20 73 71  e3_os_trace ) sq
99db0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
99dc0 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66  (X,Y,Z,A,B).#def
99dd0 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59  ine OSTRACE6(X,Y
99de0 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20  ,Z,A,B,C) \.    
99df0 69 66 28 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72  if(sqlite3_os_tr
99e00 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75  ace) sqlite3Debu
99e10 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c  gPrintf(X,Y,Z,A,
99e20 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  B,C).#define OST
99e30 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE7(X,Y,Z,A,B,
99e40 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71  C,D) \.    if(sq
99e50 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65 29 20  lite3_os_trace) 
99e60 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
99e70 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  tf(X,Y,Z,A,B,C,D
99e80 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
99e90 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66  OSTRACE1(X).#def
99ea0 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59  ine OSTRACE2(X,Y
99eb0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
99ec0 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e  E3(X,Y,Z).#defin
99ed0 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a  e OSTRACE4(X,Y,Z
99ee0 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,A).#define OSTR
99ef0 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a  ACE5(X,Y,Z,A,B).
99f00 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36  #define OSTRACE6
99f10 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64  (X,Y,Z,A,B,C).#d
99f20 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58  efine OSTRACE7(X
99f30 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65  ,Y,Z,A,B,C,D).#e
99f40 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ndif../*.** Macr
99f50 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e  os for performan
99f60 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72  ce tracing.  Nor
99f70 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66  mally turned off
99f80 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a  .  Only works.**
99f90 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72   on i486 hardwar
99fa0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
99fb0 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f  ITE_PERFORMANCE_
99fc0 54 52 41 43 45 0a 5f 5f 69 6e 6c 69 6e 65 5f 5f  TRACE.__inline__
99fd0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
99fe0 6f 6e 67 20 69 6e 74 20 68 77 74 69 6d 65 28 76  ong int hwtime(v
99ff0 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  oid){.  unsigned
9a000 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 78   long long int x
9a010 3b 0a 20 20 5f 5f 61 73 6d 5f 5f 28 22 72 64 74  ;.  __asm__("rdt
9a020 73 63 5c 6e 5c 74 22 0a 20 20 20 20 20 20 20 20  sc\n\t".        
9a030 20 20 22 6d 6f 76 20 25 25 65 64 78 2c 20 25 25    "mov %%edx, %%
9a040 65 63 78 5c 6e 5c 74 22 0a 20 20 20 20 20 20 20  ecx\n\t".       
9a050 20 20 20 3a 22 3d 41 22 20 28 78 29 29 3b 0a 20     :"=A" (x));. 
9a060 20 72 65 74 75 72 6e 20 78 3b 0a 7d 0a 73 74 61   return x;.}.sta
9a070 74 69 63 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  tic unsigned lon
9a080 67 20 6c 6f 6e 67 20 69 6e 74 20 67 5f 73 74 61  g long int g_sta
9a090 72 74 3b 0a 73 74 61 74 69 63 20 75 6e 73 69 67  rt;.static unsig
9a0a0 6e 65 64 20 69 6e 74 20 65 6c 61 70 73 65 3b 0a  ned int elapse;.
9a0b0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54  #define TIMER_ST
9a0c0 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72  ART       g_star
9a0d0 74 3d 68 77 74 69 6d 65 28 29 0a 23 64 65 66 69  t=hwtime().#defi
9a0e0 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20  ne TIMER_END    
9a0f0 20 20 20 20 20 65 6c 61 70 73 65 3d 68 77 74 69       elapse=hwti
9a100 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65  me()-g_start.#de
9a110 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53  fine TIMER_ELAPS
9a120 45 44 20 20 20 20 20 65 6c 61 70 73 65 0a 23 65  ED     elapse.#e
9a130 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  lse.#define TIME
9a140 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20  R_START.#define 
9a150 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e  TIMER_END.#defin
9a160 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20  e TIMER_ELAPSED 
9a170 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a      0.#endif../*
9a180 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c  .** If we compil
9a190 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  e with the SQLIT
9a1a0 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74  E_TEST macro set
9a1b0 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  , then the follo
9a1c0 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66  wing block.** of
9a1d0 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20   code will give 
9a1e0 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74  us the ability t
9a1f0 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73  o simulate a dis
9a200 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68  k I/O error.  Th
9a210 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f  is.** is used fo
9a220 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f  r testing the I/
9a230 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63  O recovery logic
9a240 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
9a250 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
9a260 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
9a270 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b  o_error_hit = 0;
9a280 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
9a290 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
9a2a0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51  _pending = 0;.SQ
9a2b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
9a2c0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
9a2d0 72 73 69 73 74 20 3d 20 30 3b 0a 53 51 4c 49 54  rsist = 0;.SQLIT
9a2e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
9a2f0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
9a300 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  ng = 0;.SQLITE_A
9a310 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
9a320 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65  iskfull = 0;.#de
9a330 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
9a340 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20  rror(CODE)  \.  
9a350 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  if( sqlite3_io_e
9a360 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 7c 7c 20  rror_pending || 
9a370 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
9a380 5f 68 69 74 20 29 20 5c 0a 20 20 20 20 20 69 66  _hit ) \.     if
9a390 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ( sqlite3_io_err
9a3a0 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20  or_pending-- == 
9a3b0 31 20 5c 0a 20 20 20 20 20 20 20 20 20 7c 7c 20  1 \.         || 
9a3c0 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f  (sqlite3_io_erro
9a3d0 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c  r_persist && sql
9a3e0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
9a3f0 74 29 20 29 20 5c 0a 20 20 20 20 20 20 20 20 20  t) ) \.         
9a400 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69         { local_i
9a410 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a  oerr(); CODE; }.
9a420 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61  static void loca
9a430 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54  l_ioerr(){.  IOT
9a440 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29  RACE(("IOERR\n")
9a450 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  );.  sqlite3_io_
9a460 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 0a 7d  error_hit = 1;.}
9a470 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
9a480 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43  eDiskfullError(C
9a490 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71  ODE) \.   if( sq
9a4a0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
9a4b0 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20  ending ){ \.    
9a4c0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73   if( sqlite3_dis
9a4d0 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d  kfull_pending ==
9a4e0 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c   1 ){ \.       l
9a4f0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a  ocal_ioerr(); \.
9a500 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
9a510 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20  iskfull = 1; \. 
9a520 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f        sqlite3_io
9a530 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20  _error_hit = 1; 
9a540 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c  \.       CODE; \
9a550 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20  .     }else{ \. 
9a560 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69        sqlite3_di
9a570 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d  skfull_pending--
9a580 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20  ; \.     } \.   
9a590 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  }.#else.#define 
9a5a0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
9a5b0 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  A).#define Simul
9a5c0 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
9a5d0 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (A).#endif../*.*
9a5e0 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20  * When testing, 
9a5f0 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20  keep a count of 
9a600 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70  the number of op
9a610 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66  en files..*/.#if
9a620 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
9a630 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
9a640 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65  qlite3_open_file
9a650 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66  _count = 0;.#def
9a660 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  ine OpenCounter(
9a670 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  X)  sqlite3_open
9a680 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29  _file_count+=(X)
9a690 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
9a6a0 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65  penCounter(X).#e
9a6b0 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
9a6c0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
9a6d0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
9a6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a700 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
9a710 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
9a720 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
9a730 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e 63 20 2a  ff in os_win.c *
9a740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a750 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 74  ****/../*.** Det
9a760 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65  ermine if we are
9a770 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69   dealing with Wi
9a780 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20  ndowsCE - which 
9a790 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65  has a much.** re
9a7a0 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69  duced API..*/.#i
9a7b0 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32  f defined(_WIN32
9a7c0 5f 57 43 45 29 0a 23 20 64 65 66 69 6e 65 20 4f  _WCE).# define O
9a7d0 53 5f 57 49 4e 43 45 20 31 0a 23 20 64 65 66 69  S_WINCE 1.# defi
9a7e0 6e 65 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e  ne AreFileApisAN
9a7f0 53 49 28 29 20 31 0a 23 65 6c 73 65 0a 23 20 64  SI() 1.#else.# d
9a800 65 66 69 6e 65 20 4f 53 5f 57 49 4e 43 45 20 30  efine OS_WINCE 0
9a810 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
9a820 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76  inCE lacks nativ
9a830 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69  e support for fi
9a840 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65  le locking so we
9a850 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74   have to fake it
9a860 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f  .** with some co
9a870 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a  de of our own..*
9a880 2f 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a 74  /.#if OS_WINCE.t
9a890 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77 69  ypedef struct wi
9a8a0 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74 20  nceLock {.  int 
9a8b0 6e 52 65 61 64 65 72 73 3b 20 20 20 20 20 20 20  nReaders;       
9a8c0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 61  /* Number of rea
9a8d0 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 61 69 6e  der locks obtain
9a8e0 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 50 65  ed */.  BOOL bPe
9a8f0 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f 2a 20 49  nding;      /* I
9a900 6e 64 69 63 61 74 65 73 20 61 20 70 65 6e 64 69  ndicates a pendi
9a910 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e  ng lock has been
9a920 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42   obtained */.  B
9a930 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 3b 20 20  OOL bReserved;  
9a940 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20     /* Indicates 
9a950 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20  a reserved lock 
9a960 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65  has been obtaine
9a970 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 45 78 63  d */.  BOOL bExc
9a980 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a 20 49 6e  lusive;    /* In
9a990 64 69 63 61 74 65 73 20 61 6e 20 65 78 63 6c 75  dicates an exclu
9a9a0 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 20 62 65  sive lock has be
9a9b0 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 7d  en obtained */.}
9a9c0 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 65 6e 64   winceLock;.#end
9a9d0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 77 69  if../*.** The wi
9a9e0 6e 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20  nFile structure 
9a9f0 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66  is a subclass of
9aa00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 20 73   sqlite3_file* s
9aa10 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 77  pecific to the w
9aa20 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 62 69 6c  in32.** portabil
9aa30 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79  ity layer..*/.ty
9aa40 70 65 64 65 66 20 73 74 72 75 63 74 20 77 69 6e  pedef struct win
9aa50 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b 0a 73 74  File winFile;.st
9aa60 72 75 63 74 20 77 69 6e 46 69 6c 65 20 7b 0a 20  ruct winFile {. 
9aa70 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
9aa80 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68  o_methods *pMeth
9aa90 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65 20 66 69  od;/* Must be fi
9aaa0 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20  rst */.  HANDLE 
9aab0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
9aac0 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61   /* Handle for a
9aad0 63 63 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c  ccessing the fil
9aae0 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
9aaf0 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 2f  char locktype; /
9ab00 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63  * Type of lock c
9ab10 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e  urrently held on
9ab20 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
9ab30 73 68 6f 72 74 20 73 68 61 72 65 64 4c 6f 63 6b  short sharedLock
9ab40 42 79 74 65 3b 20 20 20 2f 2a 20 52 61 6e 64 6f  Byte;   /* Rando
9ab50 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79 74 65 20  mly chosen byte 
9ab60 75 73 65 64 20 61 73 20 61 20 73 68 61 72 65 64  used as a shared
9ab70 20 6c 6f 63 6b 20 2a 2f 0a 23 69 66 20 4f 53 5f   lock */.#if OS_
9ab80 57 49 4e 43 45 0a 20 20 57 43 48 41 52 20 2a 7a  WINCE.  WCHAR *z
9ab90 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20  DeleteOnClose;  
9aba0 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20  /* Name of file 
9abb0 74 6f 20 64 65 6c 65 74 65 20 77 68 65 6e 20 63  to delete when c
9abc0 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44  losing */.  HAND
9abd0 4c 45 20 68 4d 75 74 65 78 3b 20 20 20 20 20 20  LE hMutex;      
9abe0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65      /* Mutex use
9abf0 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63  d to control acc
9ac00 65 73 73 20 74 6f 20 73 68 61 72 65 64 20 6c 6f  ess to shared lo
9ac10 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45  ck */  .  HANDLE
9ac20 20 68 53 68 61 72 65 64 3b 20 20 20 20 20 20 20   hShared;       
9ac30 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f    /* Shared memo
9ac40 72 79 20 73 65 67 6d 65 6e 74 20 75 73 65 64 20  ry segment used 
9ac50 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20  for locking */. 
9ac60 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c   winceLock local
9ac70 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b  ;        /* Lock
9ac80 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68  s obtained by th
9ac90 69 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 77  is instance of w
9aca0 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63  inFile */.  winc
9acb0 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b 20 20  eLock *shared;  
9acc0 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68      /* Global sh
9acd0 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79  ared lock memory
9ace0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 20 2a   for the file  *
9acf0 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 0a 2f 2a  /.#endif.};.../*
9ad00 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
9ad10 67 20 76 61 72 69 61 62 6c 65 20 69 73 20 28 6e  g variable is (n
9ad20 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63  ormally) set onc
9ad30 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68 61 6e  e and never chan
9ad40 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66 74 65  ges.** thereafte
9ad50 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73 20 77  r.  It records w
9ad60 68 65 74 68 65 72 20 74 68 65 20 6f 70 65 72 61  hether the opera
9ad70 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 57  ting system is W
9ad80 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54  in95.** or WinNT
9ad90 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65  ..**.** 0:   Ope
9ada0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 75 6e  rating system un
9adb0 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f  known..** 1:   O
9adc0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
9add0 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20  is Win95..** 2: 
9ade0 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74    Operating syst
9adf0 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a  em is WinNT..**.
9ae00 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 66  ** In order to f
9ae10 61 63 69 6c 69 74 61 74 65 20 74 65 73 74 69 6e  acilitate testin
9ae20 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73 79 73  g on a WinNT sys
9ae30 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20 66 69  tem, the test fi
9ae40 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e  xture.** can man
9ae50 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73 20 76  ually set this v
9ae60 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65 6d 75  alue to 1 to emu
9ae70 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68 61 76  late Win98 behav
9ae80 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ior..*/.#ifdef S
9ae90 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
9aea0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
9aeb0 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23  3_os_type = 0;.#
9aec0 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e 74 20  else.static int 
9aed0 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20  sqlite3_os_type 
9aee0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
9aef0 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 28  ** Return true (
9af00 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20  non-zero) if we 
9af10 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65  are running unde
9af20 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20  r WinNT, Win2K, 
9af30 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e  WinXP,.** or Win
9af40 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73  CE.  Return fals
9af50 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e  e (zero) for Win
9af60 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69  95, Win98, or Wi
9af70 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20  nME..**.** Here 
9af80 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e  is an interestin
9af90 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20  g observation:  
9afa0 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e  Win95, Win98, an
9afb0 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20  d WinME lack.** 
9afc0 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  the LockFileEx()
9afd0 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63 61   API.  But we ca
9afe0 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c  n still statical
9aff0 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  ly link against 
9b000 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c  that.** API as l
9b010 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20  ong as we don't 
9b020 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e  call it win runn
9b030 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e  ing Win95/98/ME.
9b040 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74    A call to.** t
9b050 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
9b060 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
9b070 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20   if the host is 
9b080 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a  Win95/98/ME or.*
9b090 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f  * WinNT/2K/XP so
9b0a0 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e   that we will kn
9b0b0 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ow whether or no
9b0c0 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20  t we can safely 
9b0d0 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b  call.** the Lock
9b0e0 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f  FileEx() API..*/
9b0f0 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a 23 20  .#if OS_WINCE.# 
9b100 64 65 66 69 6e 65 20 69 73 4e 54 28 29 20 20 28  define isNT()  (
9b110 31 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69  1).#else.  stati
9b120 63 20 69 6e 74 20 69 73 4e 54 28 76 6f 69 64 29  c int isNT(void)
9b130 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
9b140 33 5f 6f 73 5f 74 79 70 65 3d 3d 30 20 29 7b 0a  3_os_type==0 ){.
9b150 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49        OSVERSIONI
9b160 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20  NFO sInfo;.     
9b170 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69   sInfo.dwOSVersi
9b180 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a  onInfoSize = siz
9b190 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20  eof(sInfo);.    
9b1a0 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26    GetVersionEx(&
9b1b0 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71  sInfo);.      sq
9b1c0 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20  lite3_os_type = 
9b1d0 73 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d  sInfo.dwPlatform
9b1e0 49 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d  Id==VER_PLATFORM
9b1f0 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20  _WIN32_NT ? 2 : 
9b200 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  1;.    }.    ret
9b210 75 72 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  urn sqlite3_os_t
9b220 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64  ype==2;.  }.#end
9b230 69 66 20 2f 2a 20 4f 53 5f 57 49 4e 43 45 20 2a  if /* OS_WINCE *
9b240 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  /../*.** Convert
9b250 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20   a UTF-8 string 
9b260 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69  to microsoft uni
9b270 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e 20  code (UTF-16?). 
9b280 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20  .**.** Space to 
9b290 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
9b2a0 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61  d string is obta
9b2b0 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
9b2c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41  ..*/.static WCHA
9b2d0 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64 65  R *utf8ToUnicode
9b2e0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
9b2f0 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e  lename){.  int n
9b300 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a 7a  Char;.  WCHAR *z
9b310 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20  WideFilename;.. 
9b320 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 79   nChar = MultiBy
9b330 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 5f  teToWideChar(CP_
9b340 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61  UTF8, 0, zFilena
9b350 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29  me, -1, NULL, 0)
9b360 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  ;.  zWideFilenam
9b370 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 61  e = malloc( nCha
9b380 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46 69  r*sizeof(zWideFi
9b390 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20  lename[0]) );.  
9b3a0 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  if( zWideFilenam
9b3b0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
9b3c0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61  rn 0;.  }.  nCha
9b3d0 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57  r = MultiByteToW
9b3e0 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 2c  ideChar(CP_UTF8,
9b3f0 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d   0, zFilename, -
9b400 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65  1, zWideFilename
9b410 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 20  , nChar);.  if( 
9b420 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20  nChar==0 ){.    
9b430 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e 61  free(zWideFilena
9b440 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46 69  me);.    zWideFi
9b450 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a  lename = 0;.  }.
9b460 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46 69    return zWideFi
9b470 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  lename;.}../*.**
9b480 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f   Convert microso
9b490 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55 54  ft unicode to UT
9b4a0 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68  F-8.  Space to h
9b4b0 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64  old the returned
9b4c0 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62   string is.** ob
9b4d0 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
9b4e0 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  oc()..*/.static 
9b4f0 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55  char *unicodeToU
9b500 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52 20  tf8(const WCHAR 
9b510 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b  *zWideFilename){
9b520 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20  .  int nByte;.  
9b530 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b  char *zFilename;
9b540 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65  ..  nByte = Wide
9b550 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28  CharToMultiByte(
9b560 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64  CP_UTF8, 0, zWid
9b570 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30  eFilename, -1, 0
9b580 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46  , 0, 0, 0);.  zF
9b590 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63  ilename = malloc
9b5a0 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28  ( nByte );.  if(
9b5b0 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b   zFilename==0 ){
9b5c0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
9b5d0 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64   }.  nByte = Wid
9b5e0 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
9b5f0 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69  (CP_UTF8, 0, zWi
9b600 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20  deFilename, -1, 
9b610 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65  zFilename, nByte
9b620 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
9b630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b640 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79  0, 0);.  if( nBy
9b650 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66  te == 0 ){.    f
9b660 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ree(zFilename);.
9b670 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20      zFilename = 
9b680 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
9b690 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a  zFilename;.}../*
9b6a0 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 61  .** Convert an a
9b6b0 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d 69  nsi string to mi
9b6c0 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 2c  crosoft unicode,
9b6d0 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
9b6e0 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67   current codepag
9b6f0 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 66  e settings for f
9b700 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a  ile apis..** .**
9b710 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
9b720 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
9b730 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a  ng is obtained.*
9b740 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a  * from malloc..*
9b750 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a  /.static WCHAR *
9b760 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63 6f  mbcsToUnicode(co
9b770 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
9b780 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ame){.  int nByt
9b790 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62 63  e;.  WCHAR *zMbc
9b7a0 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74  sFilename;.  int
9b7b0 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 46   codepage = AreF
9b7c0 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20  ileApisANSI() ? 
9b7d0 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43  CP_ACP : CP_OEMC
9b7e0 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75  P;..  nByte = Mu
9b7f0 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61  ltiByteToWideCha
9b800 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a  r(codepage, 0, z
9b810 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55  Filename, -1, NU
9b820 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43 48  LL,0)*sizeof(WCH
9b830 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c 65  AR);.  zMbcsFile
9b840 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  name = malloc( n
9b850 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 63  Byte*sizeof(zMbc
9b860 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b  sFilename[0]) );
9b870 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c 65  .  if( zMbcsFile
9b880 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  name==0 ){.    r
9b890 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
9b8a0 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74 65  Byte = MultiByte
9b8b0 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65 70  ToWideChar(codep
9b8c0 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d  age, 0, zFilenam
9b8d0 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c 65  e, -1, zMbcsFile
9b8e0 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20 20  name, nByte);.  
9b8f0 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a  if( nByte==0 ){.
9b900 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46 69      free(zMbcsFi
9b910 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62  lename);.    zMb
9b920 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a  csFilename = 0;.
9b930 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62    }.  return zMb
9b940 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f  csFilename;.}../
9b950 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63  *.** Convert mic
9b960 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74  rosoft unicode t
9b970 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72  o multibyte char
9b980 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 61  acter string, ba
9b990 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73  sed on the.** us
9b9a0 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70 61  er's Ansi codepa
9b9b0 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20  ge..**.** Space 
9b9c0 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
9b9d0 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f  rned string is o
9b9e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
9b9f0 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61  malloc()..*/.sta
9ba00 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64  tic char *unicod
9ba10 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57 43  eToMbcs(const WC
9ba20 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61  HAR *zWideFilena
9ba30 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65  me){.  int nByte
9ba40 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  ;.  char *zFilen
9ba50 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 70  ame;.  int codep
9ba60 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70 69  age = AreFileApi
9ba70 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 50  sANSI() ? CP_ACP
9ba80 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20   : CP_OEMCP;..  
9ba90 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72  nByte = WideChar
9baa0 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65  ToMultiByte(code
9bab0 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69  page, 0, zWideFi
9bac0 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30  lename, -1, 0, 0
9bad0 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65  , 0, 0);.  zFile
9bae0 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  name = malloc( n
9baf0 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46  Byte );.  if( zF
9bb00 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ilename==0 ){.  
9bb10 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
9bb20 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68    nByte = WideCh
9bb30 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f  arToMultiByte(co
9bb40 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65  depage, 0, zWide
9bb50 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46  Filename, -1, zF
9bb60 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a  ilename, nByte,.
9bb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9bb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
9bb90 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65   0);.  if( nByte
9bba0 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65   == 0 ){.    fre
9bbb0 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
9bbc0 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b    zFilename = 0;
9bbd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46  .  }.  return zF
9bbe0 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ilename;.}../*.*
9bbf0 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69 62  * Convert multib
9bc00 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 74  yte character st
9bc10 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20 20  ring to UTF-8.  
9bc20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
9bc30 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74  e.** returned st
9bc40 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64  ring is obtained
9bc50 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a   from malloc()..
9bc60 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
9bc70 6d 62 63 73 54 6f 55 74 66 38 28 63 6f 6e 73 74  mbcsToUtf8(const
9bc80 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
9bc90 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  ){.  char *zFile
9bca0 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 41  nameUtf8;.  WCHA
9bcb0 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20  R *zTmpWide;..  
9bcc0 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54  zTmpWide = mbcsT
9bcd0 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61  oUnicode(zFilena
9bce0 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57  me);.  if( zTmpW
9bcf0 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ide==0 ){.    re
9bd00 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46  turn 0;.  }.  zF
9bd10 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e  ilenameUtf8 = un
9bd20 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70  icodeToUtf8(zTmp
9bd30 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54  Wide);.  free(zT
9bd40 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72  mpWide);.  retur
9bd50 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b  n zFilenameUtf8;
9bd60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
9bd70 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 69  t UTF-8 to multi
9bd80 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73  byte character s
9bd90 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f  tring.  Space to
9bda0 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 65   hold the .** re
9bdb0 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
9bdc0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
9bdd0 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
9bde0 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f 4d  ic char *utf8ToM
9bdf0 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  bcs(const char *
9be00 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68  zFilename){.  ch
9be10 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 63  ar *zFilenameMbc
9be20 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70  s;.  WCHAR *zTmp
9be30 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64  Wide;..  zTmpWid
9be40 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64  e = utf8ToUnicod
9be50 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
9be60 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20  if( zTmpWide==0 
9be70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
9be80 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65  .  }.  zFilename
9be90 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 6f  Mbcs = unicodeTo
9bea0 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b 0a  Mbcs(zTmpWide);.
9beb0 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29    free(zTmpWide)
9bec0 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65  ;.  return zFile
9bed0 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 66  nameMbcs;.}..#if
9bee0 20 4f 53 5f 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a   OS_WINCE./*****
9bef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9bf00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9bf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9bf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9bf30 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63  ****.** This sec
9bf40 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 63 6f  tion contains co
9bf50 64 65 20 66 6f 72 20 57 69 6e 43 45 20 6f 6e 6c  de for WinCE onl
9bf60 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64  y..*/./*.** Wind
9bf70 6f 77 73 43 45 20 64 6f 65 73 20 6e 6f 74 20 68  owsCE does not h
9bf80 61 76 65 20 61 20 6c 6f 63 61 6c 74 69 6d 65 28  ave a localtime(
9bf90 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f 20  ) function.  So 
9bfa0 63 72 65 61 74 65 20 61 0a 2a 2a 20 73 75 62 73  create a.** subs
9bfb0 74 69 74 75 74 65 2e 0a 2a 2f 0a 73 74 72 75 63  titute..*/.struc
9bfc0 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f  t tm *__cdecl lo
9bfd0 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74 20 74 69  caltime(const ti
9bfe0 6d 65 5f 74 20 2a 74 29 0a 7b 0a 20 20 73 74 61  me_t *t).{.  sta
9bff0 74 69 63 20 73 74 72 75 63 74 20 74 6d 20 79 3b  tic struct tm y;
9c000 0a 20 20 46 49 4c 45 54 49 4d 45 20 75 54 6d 2c  .  FILETIME uTm,
9c010 20 6c 54 6d 3b 0a 20 20 53 59 53 54 45 4d 54 49   lTm;.  SYSTEMTI
9c020 4d 45 20 70 54 6d 3b 0a 20 20 69 36 34 20 74 36  ME pTm;.  i64 t6
9c030 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b 0a 20  4;.  t64 = *t;. 
9c040 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 31 31   t64 = (t64 + 11
9c050 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 30 30  644473600)*10000
9c060 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f 77  000;.  uTm.dwLow
9c070 44 61 74 65 54 69 6d 65 20 3d 20 74 36 34 20 26  DateTime = t64 &
9c080 20 30 78 46 46 46 46 46 46 46 46 3b 0a 20 20 75   0xFFFFFFFF;.  u
9c090 54 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d  Tm.dwHighDateTim
9c0a0 65 3d 20 74 36 34 20 3e 3e 20 33 32 3b 0a 20 20  e= t64 >> 32;.  
9c0b0 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 61 6c 46  FileTimeToLocalF
9c0c0 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c 26 6c 54  ileTime(&uTm,&lT
9c0d0 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f  m);.  FileTimeTo
9c0e0 53 79 73 74 65 6d 54 69 6d 65 28 26 6c 54 6d 2c  SystemTime(&lTm,
9c0f0 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d 5f 79 65  &pTm);.  y.tm_ye
9c100 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 72 20 2d  ar = pTm.wYear -
9c110 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d 5f 6d 6f   1900;.  y.tm_mo
9c120 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d  n = pTm.wMonth -
9c130 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 61 79 20   1;.  y.tm_wday 
9c140 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 65 65 6b  = pTm.wDayOfWeek
9c150 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 20 3d 20  ;.  y.tm_mday = 
9c160 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 2e 74 6d  pTm.wDay;.  y.tm
9c170 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 48 6f 75  _hour = pTm.wHou
9c180 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e 20 3d 20  r;.  y.tm_min = 
9c190 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a 20 20 79  pTm.wMinute;.  y
9c1a0 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d 2e 77 53  .tm_sec = pTm.wS
9c1b0 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 72 6e 20  econd;.  return 
9c1c0 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 77  &y;.}../* This w
9c1d0 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 63 61 6c  ill never be cal
9c1e0 6c 65 64 2c 20 62 75 74 20 64 65 66 69 6e 65 64  led, but defined
9c1f0 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f 64   to make the cod
9c200 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a 23 64 65  e compile */.#de
9c210 66 69 6e 65 20 47 65 74 54 65 6d 70 50 61 74 68  fine GetTempPath
9c220 41 28 61 2c 62 29 0a 0a 23 64 65 66 69 6e 65 20  A(a,b)..#define 
9c230 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64  LockFile(a,b,c,d
9c240 2c 65 29 20 20 20 20 20 20 20 77 69 6e 63 65 4c  ,e)       winceL
9c250 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20 63  ockFile(&a, b, c
9c260 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 6e 65 20  , d, e).#define 
9c270 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63  UnlockFile(a,b,c
9c280 2c 64 2c 65 29 20 20 20 20 20 77 69 6e 63 65 55  ,d,e)     winceU
9c290 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c  nlockFile(&a, b,
9c2a0 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 6e   c, d, e).#defin
9c2b0 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 61 2c 62  e LockFileEx(a,b
9c2c0 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 69 6e 63  ,c,d,e,f)   winc
9c2d0 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 61 2c 20  eLockFileEx(&a, 
9c2e0 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 29 0a 0a  b, c, d, e, f)..
9c2f0 23 64 65 66 69 6e 65 20 48 41 4e 44 4c 45 5f 54  #define HANDLE_T
9c300 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20 28 77 69  O_WINFILE(a) (wi
9c310 6e 46 69 6c 65 2a 29 26 28 28 63 68 61 72 2a 29  nFile*)&((char*)
9c320 61 29 5b 2d 6f 66 66 73 65 74 6f 66 28 77 69 6e  a)[-offsetof(win
9c330 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20  File,h)]../*.** 
9c340 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f  Acquire a lock o
9c350 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a  n the handle h.*
9c360 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69  /.static void wi
9c370 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
9c380 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44 57  HANDLE h){.   DW
9c390 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20 64 6f  ORD dwErr;.   do
9c3a0 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20 3d 20   {.     dwErr = 
9c3b0 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a  WaitForSingleObj
9c3c0 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 45 29  ect(h, INFINITE)
9c3d0 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 64 77  ;.   } while (dw
9c3e0 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45  Err != WAIT_OBJE
9c3f0 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 21 3d  CT_0 && dwErr !=
9c400 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29   WAIT_ABANDONED)
9c410 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  ;.}./*.** Releas
9c420 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 72 65  e a lock acquire
9c430 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 78 41  d by winceMutexA
9c440 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66  cquire().*/.#def
9c450 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 52 65  ine winceMutexRe
9c460 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 73 65  lease(h) Release
9c470 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20  Mutex(h)../*.** 
9c480 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 65 78  Create the mutex
9c490 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 6d 6f   and shared memo
9c4a0 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  ry used for lock
9c4b0 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c 65 0a  ing in the file.
9c4c0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 70 46  ** descriptor pF
9c4d0 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  ile.*/.static BO
9c4e0 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 4c 6f  OL winceCreateLo
9c4f0 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ck(const char *z
9c500 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c  Filename, winFil
9c510 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57 43 48  e *pFile){.  WCH
9c520 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41  AR *zTok;.  WCHA
9c530 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54  R *zName = utf8T
9c540 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61  oUnicode(zFilena
9c550 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69  me);.  BOOL bIni
9c560 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20  t = TRUE;..  /* 
9c570 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c  Initialize the l
9c580 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f  ocal lockdata */
9c590 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70  .  ZeroMemory(&p
9c5a0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a  File->local, siz
9c5b0 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  eof(pFile->local
9c5c0 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63  ));..  /* Replac
9c5d0 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73 68 65  e the backslashe
9c5e0 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e  s from the filen
9c5f0 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63 61 73  ame and lowercas
9c600 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72  e it.  ** to der
9c610 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61 6d 65  ive a mutex name
9c620 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68  . */.  zTok = Ch
9c630 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b  arLowerW(zName);
9c640 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a  .  for (;*zTok;z
9c650 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28  Tok++){.    if (
9c660 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a  *zTok == '\\') *
9c670 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a  zTok = '_';.  }.
9c680 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65  .  /* Create/ope
9c690 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75 74 65  n the named mute
9c6a0 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d  x */.  pFile->hM
9c6b0 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d 75 74  utex = CreateMut
9c6c0 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c  exW(NULL, FALSE,
9c6d0 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28 21   zName);.  if (!
9c6e0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a  pFile->hMutex){.
9c6f0 20 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b      free(zName);
9c700 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53  .    return FALS
9c710 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71  E;.  }..  /* Acq
9c720 75 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62  uire the mutex b
9c730 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67  efore continuing
9c740 20 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78   */.  winceMutex
9c750 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68  Acquire(pFile->h
9c760 4d 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20  Mutex);.  .  /* 
9c770 53 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20  Since the names 
9c780 6f 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73  of named mutexes
9c790 2c 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69  , semaphores, fi
9c7a0 6c 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20  le mappings etc 
9c7b0 61 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73  are .  ** case-s
9c7c0 65 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61  ensitive, take a
9c7d0 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74  dvantage of that
9c7e0 20 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20   by uppercasing 
9c7f0 74 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20  the mutex name. 
9c800 20 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68   ** and using th
9c810 61 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64  at as the shared
9c820 20 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d   filemapping nam
9c830 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70  e..  */.  CharUp
9c840 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70  perW(zName);.  p
9c850 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20  File->hShared = 
9c860 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e  CreateFileMappin
9c870 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c  gW(INVALID_HANDL
9c880 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20  E_VALUE, NULL,. 
9c890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c8b0 20 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57        PAGE_READW
9c8c0 52 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28  RITE, 0, sizeof(
9c8d0 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20  winceLock),.    
9c8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c900 20 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20     zName);  ..  
9c910 2f 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68  /* Set a flag th
9c920 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27  at indicates we'
9c930 72 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20  re the first to 
9c940 63 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72  create the memor
9c950 79 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75  y so it .  ** mu
9c960 73 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69  st be zero-initi
9c970 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28  alized */.  if (
9c980 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d  GetLastError() =
9c990 3d 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f  = ERROR_ALREADY_
9c9a0 45 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e  EXISTS){.    bIn
9c9b0 69 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a  it = FALSE;.  }.
9c9c0 0a 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a  .  free(zName);.
9c9d0 0a 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63  .  /* If we succ
9c9e0 65 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20  eeded in making 
9c9f0 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72  the shared memor
9ca00 79 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74  y handle, map it
9ca10 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65  . */.  if (pFile
9ca20 2d 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20  ->hShared){.    
9ca30 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20  pFile->shared = 
9ca40 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56  (winceLock*)MapV
9ca50 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d  iewOfFile(pFile-
9ca60 3e 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20  >hShared, .     
9ca70 20 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50          FILE_MAP
9ca80 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57  _READ|FILE_MAP_W
9ca90 52 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65  RITE, 0, 0, size
9caa0 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a  of(winceLock));.
9cab0 20 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e      /* If mappin
9cac0 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20  g failed, close 
9cad0 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72  the shared memor
9cae0 79 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61  y handle and era
9caf0 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20  se it */.    if 
9cb00 28 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29  (!pFile->shared)
9cb10 7b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48 61 6e  {.      CloseHan
9cb20 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72  dle(pFile->hShar
9cb30 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65  ed);.      pFile
9cb40 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c  ->hShared = NULL
9cb50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
9cb60 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65 6d 6f  * If shared memo
9cb70 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ry could not be 
9cb80 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 6c  created, then cl
9cb90 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e  ose the mutex an
9cba0 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28  d fail */.  if (
9cbb0 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d  pFile->hShared =
9cbc0 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e  = NULL){.    win
9cbd0 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70  ceMutexRelease(p
9cbe0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20  File->hMutex);. 
9cbf0 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70     CloseHandle(p
9cc00 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20  File->hMutex);. 
9cc10 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78     pFile->hMutex
9cc20 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74   = NULL;.    ret
9cc30 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20  urn FALSE;.  }. 
9cc40 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a   .  /* Initializ
9cc50 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d  e the shared mem
9cc60 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 75 70  ory if we're sup
9cc70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66  posed to */.  if
9cc80 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a   (bInit) {.    Z
9cc90 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d  eroMemory(pFile-
9cca0 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f 66 28  >shared, sizeof(
9ccb0 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d  winceLock));.  }
9ccc0 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65  ..  winceMutexRe
9ccd0 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75  lease(pFile->hMu
9cce0 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 54  tex);.  return T
9ccf0 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  RUE;.}../*.** De
9cd00 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 20 6f  stroy the part o
9cd10 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 20 64  f winFile that d
9cd20 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 65 20  eals with wince 
9cd30 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20  locks.*/.static 
9cd40 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 72 6f  void winceDestro
9cd50 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70  yLock(winFile *p
9cd60 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 46 69  File){.  if (pFi
9cd70 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20  le->hMutex){.   
9cd80 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65 20   /* Acquire the 
9cd90 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e  mutex */.    win
9cda0 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70  ceMutexAcquire(p
9cdb0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a  File->hMutex);..
9cdc0 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
9cdd0 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75  wing blocks shou
9cde0 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 73 65  ld probably asse
9cdf0 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 65  rt in debug mode
9ce00 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 20 20  , but they.     
9ce10 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e 75 70    are to cleanup
9ce20 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f 63   in case any loc
9ce30 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e  ks remained open
9ce40 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c   */.    if (pFil
9ce50 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
9ce60 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  s){.      pFile-
9ce70 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72  >shared->nReader
9ce80 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  s --;.    }.    
9ce90 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
9cea0 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 20 20  .bReserved){.   
9ceb0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
9cec0 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41  ->bReserved = FA
9ced0 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  LSE;.    }.    i
9cee0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
9cef0 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20  bPending){.     
9cf00 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
9cf10 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45  bPending = FALSE
9cf20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28  ;.    }.    if (
9cf30 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78  pFile->local.bEx
9cf40 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20  clusive){.      
9cf50 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
9cf60 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53  Exclusive = FALS
9cf70 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  E;.    }..    /*
9cf80 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 61 6e   De-reference an
9cf90 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79  d close our copy
9cfa0 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6d   of the shared m
9cfb0 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a  emory handle */.
9cfc0 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f 66 46      UnmapViewOfF
9cfd0 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61 72 65  ile(pFile->share
9cfe0 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e  d);.    CloseHan
9cff0 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72  dle(pFile->hShar
9d000 65 64 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 46  ed);..    if( pF
9d010 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c  ile->zDeleteOnCl
9d020 6f 73 65 20 29 7b 0a 20 20 20 20 20 20 44 65 6c  ose ){.      Del
9d030 65 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e  eteFileW(pFile->
9d040 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b  zDeleteOnClose);
9d050 0a 20 20 20 20 20 20 66 72 65 65 28 70 46 69 6c  .      free(pFil
9d060 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73  e->zDeleteOnClos
9d070 65 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  e);.      pFile-
9d080 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20  >zDeleteOnClose 
9d090 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 0;.    }..    
9d0a0 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65  /* Done with the
9d0b0 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69   mutex */.    wi
9d0c0 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28  nceMutexRelease(
9d0d0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20  pFile->hMutex); 
9d0e0 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e     .    CloseHan
9d0f0 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  dle(pFile->hMute
9d100 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68  x);.    pFile->h
9d110 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20  Mutex = NULL;.  
9d120 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69  }.}../* .** An i
9d130 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
9d140 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20   the LockFile() 
9d150 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66  API of windows f
9d160 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74  or wince.*/.stat
9d170 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63  ic BOOL winceLoc
9d180 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20  kFile(.  HANDLE 
9d190 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44  *phFile,.  DWORD
9d1a0 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77   dwFileOffsetLow
9d1b0 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65  ,.  DWORD dwFile
9d1c0 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57  OffsetHigh,.  DW
9d1d0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
9d1e0 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44  esToLockLow,.  D
9d1f0 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79  WORD nNumberOfBy
9d200 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b  tesToLockHigh.){
9d210 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
9d220 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49  e = HANDLE_TO_WI
9d230 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20  NFILE(phFile);. 
9d240 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20   BOOL bReturn = 
9d250 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70  FALSE;..  if (!p
9d260 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65  File->hMutex) re
9d270 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e  turn TRUE;.  win
9d280 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70  ceMutexAcquire(p
9d290 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a  File->hMutex);..
9d2a0 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20    /* Wanting an 
9d2b0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20  exclusive lock? 
9d2c0 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  */.  if (dwFileO
9d2d0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52  ffsetLow == SHAR
9d2e0 45 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20  ED_FIRST.       
9d2f0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
9d300 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48  sToLockLow == SH
9d310 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20  ARED_SIZE){.    
9d320 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65  if (pFile->share
9d330 64 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30  d->nReaders == 0
9d340 20 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65   && pFile->share
9d350 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d  d->bExclusive ==
9d360 20 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c   0){.       pFil
9d370 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c  e->shared->bExcl
9d380 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20  usive = TRUE;.  
9d390 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
9d3a0 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54  l.bExclusive = T
9d3b0 52 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74  RUE;.       bRet
9d3c0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
9d3d0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
9d3e0 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63   a read-only loc
9d3f0 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20  k? */.  else if 
9d400 28 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f  ((dwFileOffsetLo
9d410 77 20 3e 3d 20 53 48 41 52 45 44 5f 46 49 52 53  w >= SHARED_FIRS
9d420 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  T &&.           
9d430 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77   dwFileOffsetLow
9d440 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20   < SHARED_FIRST 
9d450 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29 20 26  + SHARED_SIZE) &
9d460 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e  &.            nN
9d470 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
9d480 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20  ckLow == 1){.   
9d490 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72   if (pFile->shar
9d4a0 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d  ed->bExclusive =
9d4b0 3d 20 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  = 0){.      pFil
9d4c0 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
9d4d0 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28  s ++;.      if (
9d4e0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65  pFile->local.nRe
9d4f0 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20  aders == 1){.   
9d500 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
9d510 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b  ed->nReaders ++;
9d520 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
9d530 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20  Return = TRUE;. 
9d540 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57     }.  }..  /* W
9d550 61 6e 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f  ant a pending lo
9d560 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ck? */.  else if
9d570 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f   (dwFileOffsetLo
9d580 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  w == PENDING_BYT
9d590 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
9d5a0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20  tesToLockLow == 
9d5b0 31 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f  1){.    /* If no
9d5c0 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61   pending lock ha
9d5d0 73 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c  s been acquired,
9d5e0 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74   then acquire it
9d5f0 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c   */.    if (pFil
9d600 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
9d610 69 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20  ing == 0) {.    
9d620 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
9d630 3e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45  >bPending = TRUE
9d640 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
9d650 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20  ocal.bPending = 
9d660 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74  TRUE;.      bRet
9d670 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
9d680 7d 0a 20 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20  }.  }.  /* Want 
9d690 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f  a reserved lock?
9d6a0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
9d6b0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
9d6c0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20  = RESERVED_BYTE 
9d6d0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
9d6e0 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29  sToLockLow == 1)
9d6f0 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  {.    if (pFile-
9d700 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76  >shared->bReserv
9d710 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20  ed == 0) {.     
9d720 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
9d730 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45  bReserved = TRUE
9d740 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
9d750 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d  ocal.bReserved =
9d760 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65   TRUE;.      bRe
9d770 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
9d780 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d   }.  }..  winceM
9d790 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
9d7a0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65  e->hMutex);.  re
9d7b0 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a  turn bReturn;.}.
9d7c0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d  ./*.** An implem
9d7d0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
9d7e0 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f  UnlockFile API o
9d7f0 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69  f windows for wi
9d800 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  nce.*/.static BO
9d810 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69  OL winceUnlockFi
9d820 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68  le(.  HANDLE *ph
9d830 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77  File,.  DWORD dw
9d840 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20  FileOffsetLow,. 
9d850 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66   DWORD dwFileOff
9d860 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44  setHigh,.  DWORD
9d870 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
9d880 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57  oUnlockLow,.  DW
9d890 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
9d8a0 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29  esToUnlockHigh.)
9d8b0 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  {.  winFile *pFi
9d8c0 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57  le = HANDLE_TO_W
9d8d0 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a  INFILE(phFile);.
9d8e0 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d    BOOL bReturn =
9d8f0 20 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21   FALSE;..  if (!
9d900 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72  pFile->hMutex) r
9d910 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69  eturn TRUE;.  wi
9d920 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
9d930 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
9d940 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20  .  /* Releasing 
9d950 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72  a reader lock or
9d960 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
9d970 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69  ck */.  if (dwFi
9d980 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53  leOffsetLow >= S
9d990 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20  HARED_FIRST &&. 
9d9a0 20 20 20 20 20 20 64 77 46 69 6c 65 4f 66 66 73        dwFileOffs
9d9b0 65 74 4c 6f 77 20 3c 20 53 48 41 52 45 44 5f 46  etLow < SHARED_F
9d9c0 49 52 53 54 20 2b 20 53 48 41 52 45 44 5f 53 49  IRST + SHARED_SI
9d9d0 5a 45 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20  ZE){.    /* Did 
9d9e0 77 65 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75  we have an exclu
9d9f0 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  sive lock? */.  
9da00 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
9da10 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a  al.bExclusive){.
9da20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
9da30 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20  al.bExclusive = 
9da40 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69  FALSE;.      pFi
9da50 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63  le->shared->bExc
9da60 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a  lusive = FALSE;.
9da70 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
9da80 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  TRUE;.    }..   
9da90 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 74 20   /* Did we just 
9daa0 68 61 76 65 20 61 20 72 65 61 64 65 72 20 6c 6f  have a reader lo
9dab0 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20  ck? */.    else 
9dac0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
9dad0 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20  .nReaders){.    
9dae0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e    pFile->local.n
9daf0 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20  Readers --;.    
9db00 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
9db10 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30  al.nReaders == 0
9db20 29 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  ).      {.      
9db30 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
9db40 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20  >nReaders --;.  
9db50 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74      }.      bRet
9db60 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
9db70 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65  }.  }..  /* Rele
9db80 61 73 69 6e 67 20 61 20 70 65 6e 64 69 6e 67 20  asing a pending 
9db90 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69  lock */.  else i
9dba0 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  f (dwFileOffsetL
9dbb0 6f 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59  ow == PENDING_BY
9dbc0 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42  TE && nNumberOfB
9dbd0 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20  ytesToUnlockLow 
9dbe0 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70  == 1){.    if (p
9dbf0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e  File->local.bPen
9dc00 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69  ding){.      pFi
9dc10 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69  le->local.bPendi
9dc20 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ng = FALSE;.    
9dc30 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
9dc40 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53  >bPending = FALS
9dc50 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
9dc60 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
9dc70 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e   }.  /* Releasin
9dc80 67 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  g a reserved loc
9dc90 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  k */.  else if (
9dca0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
9dcb0 3d 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  == RESERVED_BYTE
9dcc0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74   && nNumberOfByt
9dcd0 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d  esToUnlockLow ==
9dce0 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69   1){.    if (pFi
9dcf0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72  le->local.bReser
9dd00 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 69  ved) {.      pFi
9dd10 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72  le->local.bReser
9dd20 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ved = FALSE;.   
9dd30 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
9dd40 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41  ->bReserved = FA
9dd50 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75  LSE;.      bRetu
9dd60 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
9dd70 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74  .  }..  winceMut
9dd80 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d  exRelease(pFile-
9dd90 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  >hMutex);.  retu
9dda0 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  rn bReturn;.}../
9ddb0 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e  *.** An implemen
9ddc0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f  tation of the Lo
9ddd0 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 6f  ckFileEx() API o
9dde0 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69  f windows for wi
9ddf0 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  nce.*/.static BO
9de00 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65  OL winceLockFile
9de10 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68  Ex(.  HANDLE *ph
9de20 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77  File,.  DWORD dw
9de30 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 64  Flags,.  DWORD d
9de40 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 4f  wReserved,.  DWO
9de50 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  RD nNumberOfByte
9de60 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57  sToLockLow,.  DW
9de70 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
9de80 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20  esToLockHigh,.  
9de90 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 4f  LPOVERLAPPED lpO
9dea0 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f  verlapped.){.  /
9deb0 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
9dec0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72  wants a shared r
9ded0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72  ead lock, forwar
9dee0 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a  d this call.  **
9def0 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c   to winceLockFil
9df00 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65  e */.  if (lpOve
9df10 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20  rlapped->Offset 
9df20 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20  == SHARED_FIRST 
9df30 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73  &&.      dwFlags
9df40 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e   == 1 &&.      n
9df50 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
9df60 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44  ockLow == SHARED
9df70 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75  _SIZE){.    retu
9df80 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65  rn winceLockFile
9df90 28 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f  (phFile, SHARED_
9dfa0 46 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b  FIRST, 0, 1, 0);
9dfb0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41  .  }.  return FA
9dfc0 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64  LSE;.}./*.** End
9dfd0 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20   of the special 
9dfe0 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a  code for wince.*
9dff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
9e040 6e 64 69 66 20 2f 2a 20 4f 53 5f 57 49 4e 43 45  ndif /* OS_WINCE
9e050 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
9e060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e0a0 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  ***.** The next 
9e0b0 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65  group of routine
9e0c0 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  s implement the 
9e0d0 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63  I/O methods spec
9e0e0 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  ified.** by the 
9e0f0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
9e100 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a  ds object..*****
9e110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
9e160 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a  * Close a file..
9e170 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 72 65 70 6f  **.** It is repo
9e180 72 74 65 64 20 74 68 61 74 20 61 6e 20 61 74 74  rted that an att
9e190 65 6d 70 74 20 74 6f 20 63 6c 6f 73 65 20 61 20  empt to close a 
9e1a0 68 61 6e 64 6c 65 20 6d 69 67 68 74 20 73 6f 6d  handle might som
9e1b0 65 74 69 6d 65 73 0a 2a 2a 20 66 61 69 6c 2e 20  etimes.** fail. 
9e1c0 20 54 68 69 73 20 69 73 20 61 20 76 65 72 79 20   This is a very 
9e1d0 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 72 65 73  unreasonable res
9e1e0 75 6c 74 2c 20 62 75 74 20 77 69 6e 64 6f 77 73  ult, but windows
9e1f0 20 69 73 20 6e 6f 74 6f 72 69 6f 75 73 0a 2a 2a   is notorious.**
9e200 20 66 6f 72 20 62 65 69 6e 67 20 75 6e 72 65 61   for being unrea
9e210 73 6f 6e 61 62 6c 65 20 73 6f 20 49 20 64 6f 20  sonable so I do 
9e220 6e 6f 74 20 64 6f 75 62 74 20 74 68 61 74 20 69  not doubt that i
9e230 74 20 6d 69 67 68 74 20 68 61 70 70 65 6e 2e 20  t might happen. 
9e240 20 49 66 0a 2a 2a 20 74 68 65 20 63 6c 6f 73 65   If.** the close
9e250 20 66 61 69 6c 73 2c 20 77 65 20 70 61 75 73 65   fails, we pause
9e260 20 66 6f 72 20 31 30 30 20 6d 69 6c 6c 69 73 65   for 100 millise
9e270 63 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 61 67  conds and try ag
9e280 61 69 6e 2e 20 20 41 73 0a 2a 2a 20 6d 61 6e 79  ain.  As.** many
9e290 20 61 73 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54   as MX_CLOSE_ATT
9e2a0 45 4d 50 54 20 61 74 74 65 6d 70 74 73 20 74 6f  EMPT attempts to
9e2b0 20 63 6c 6f 73 65 20 74 68 65 20 68 61 6e 64 6c   close the handl
9e2c0 65 20 61 72 65 20 6d 61 64 65 20 62 65 66 6f 72  e are made befor
9e2d0 65 0a 2a 2a 20 67 69 76 69 6e 67 20 75 70 20 61  e.** giving up a
9e2e0 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20  nd returning an 
9e2f0 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  error..*/.#defin
9e300 65 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d  e MX_CLOSE_ATTEM
9e310 50 54 20 33 0a 73 74 61 74 69 63 20 69 6e 74 20  PT 3.static int 
9e320 77 69 6e 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  winClose(sqlite3
9e330 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e  _file *id){.  in
9e340 74 20 72 63 2c 20 63 6e 74 20 3d 20 30 3b 0a 20  t rc, cnt = 0;. 
9e350 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
9e360 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
9e370 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53    OSTRACE2("CLOS
9e380 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  E %d\n", pFile->
9e390 68 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72 63  h);.  do{.    rc
9e3a0 20 3d 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70   = CloseHandle(p
9e3b0 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68 69  File->h);.  }whi
9e3c0 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 63 6e 74  le( rc==0 && cnt
9e3d0 2b 2b 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 54  ++ < MX_CLOSE_AT
9e3e0 54 45 4d 50 54 20 26 26 20 28 53 6c 65 65 70 28  TEMPT && (Sleep(
9e3f0 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20  100), 1) );.#if 
9e400 4f 53 5f 57 49 4e 43 45 0a 20 20 77 69 6e 63 65  OS_WINCE.  wince
9e410 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46 69 6c  DestroyLock(pFil
9e420 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 4f 70 65  e);.#endif.  Ope
9e430 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20  nCounter(-1);.  
9e440 72 65 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49  return rc ? SQLI
9e450 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49  TE_OK : SQLITE_I
9e460 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  OERR;.}../*.** S
9e470 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f  ome microsoft co
9e480 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69  mpilers lack thi
9e490 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f  s definition..*/
9e4a0 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44  .#ifndef INVALID
9e4b0 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
9e4c0 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c  R.# define INVAL
9e4d0 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e  ID_SET_FILE_POIN
9e4e0 54 45 52 20 28 28 44 57 4f 52 44 29 2d 31 29 0a  TER ((DWORD)-1).
9e4f0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
9e500 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66  ad data from a f
9e510 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65  ile into a buffe
9e520 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  r.  Return SQLIT
9e530 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62  E_OK if all.** b
9e540 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73  ytes were read s
9e550 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20  uccessfully and 
9e560 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20  SQLITE_IOERR if 
9e570 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a  anything goes.**
9e580 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69   wrong..*/.stati
9e590 63 20 69 6e 74 20 77 69 6e 52 65 61 64 28 0a 20  c int winRead(. 
9e5a0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
9e5b0 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  d,          /* F
9e5c0 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d  ile to read from
9e5d0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66   */.  void *pBuf
9e5e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
9e5f0 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e   /* Write conten
9e600 74 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66  t into this buff
9e610 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c  er */.  int amt,
9e620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9e630 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
9e640 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f  bytes to read */
9e650 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
9e660 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 2f 2a   offset       /*
9e670 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
9e680 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
9e690 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72  .){.  LONG upper
9e6a0 42 69 74 73 20 3d 20 28 6f 66 66 73 65 74 3e 3e  Bits = (offset>>
9e6b0 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66  32) & 0x7fffffff
9e6c0 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69  ;.  LONG lowerBi
9e6d0 74 73 20 3d 20 6f 66 66 73 65 74 20 26 20 30 78  ts = offset & 0x
9e6e0 66 66 66 66 66 66 66 66 3b 0a 20 20 44 57 4f 52  ffffffff;.  DWOR
9e6f0 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 67 6f  D rc;.  DWORD go
9e700 74 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  t;.  winFile *pF
9e710 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
9e720 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  id;.  assert( id
9e730 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  !=0 );.  Simulat
9e740 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20  eIOError(return 
9e750 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
9e760 44 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22  D);.  OSTRACE3("
9e770 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c  READ %d lock=%d\
9e780 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46  n", pFile->h, pF
9e790 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a  ile->locktype);.
9e7a0 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f    rc = SetFilePo
9e7b0 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20  inter(pFile->h, 
9e7c0 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65  lowerBits, &uppe
9e7d0 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49  rBits, FILE_BEGI
9e7e0 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e  N);.  if( rc==IN
9e7f0 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50  VALID_SET_FILE_P
9e800 4f 49 4e 54 45 52 20 26 26 20 47 65 74 4c 61 73  OINTER && GetLas
9e810 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f 45 52 52  tError()!=NO_ERR
9e820 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
9e830 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20   SQLITE_FULL;.  
9e840 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c  }.  if( !ReadFil
9e850 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66  e(pFile->h, pBuf
9e860 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20  , amt, &got, 0) 
9e870 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
9e880 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b  LITE_IOERR_READ;
9e890 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 3d 3d  .  }.  if( got==
9e8a0 28 44 57 4f 52 44 29 61 6d 74 20 29 7b 0a 20 20  (DWORD)amt ){.  
9e8b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
9e8c0 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
9e8d0 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a   memset(&((char*
9e8e0 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20  )pBuf)[got], 0, 
9e8f0 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65  amt-got);.    re
9e900 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
9e910 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20  R_SHORT_READ;.  
9e920 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  }.}../*.** Write
9e930 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66   data from a buf
9e940 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e  fer into a file.
9e950 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
9e960 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a  OK on success.**
9e970 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65   or some other e
9e980 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69  rror code on fai
9e990 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lure..*/.static 
9e9a0 69 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20  int winWrite(.  
9e9b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
9e9c0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c  ,         /* Fil
9e9d0 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20  e to write into 
9e9e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
9e9f0 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f  *pBuf,         /
9ea00 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62  * The bytes to b
9ea10 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69  e written */.  i
9ea20 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
9ea30 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
9ea40 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77  er of bytes to w
9ea50 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rite */.  sqlite
9ea60 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20  3_int64 offset  
9ea70 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
9ea80 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  to the file to b
9ea90 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20  egin writing at 
9eaa0 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70  */.){.  LONG upp
9eab0 65 72 42 69 74 73 20 3d 20 28 6f 66 66 73 65 74  erBits = (offset
9eac0 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66  >>32) & 0x7fffff
9ead0 66 66 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72  ff;.  LONG lower
9eae0 42 69 74 73 20 3d 20 6f 66 66 73 65 74 20 26 20  Bits = offset & 
9eaf0 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 44 57  0xffffffff;.  DW
9eb00 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20  ORD rc;.  DWORD 
9eb10 77 72 6f 74 65 3b 0a 20 20 77 69 6e 46 69 6c 65  wrote;.  winFile
9eb20 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
9eb30 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74  le*)id;.  assert
9eb40 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d  ( id!=0 );.  Sim
9eb50 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74  ulateIOError(ret
9eb60 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
9eb70 5f 57 52 49 54 45 29 3b 0a 20 20 53 69 6d 75 6c  _WRITE);.  Simul
9eb80 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
9eb90 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46  (return SQLITE_F
9eba0 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  ULL);.  OSTRACE3
9ebb0 28 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d  ("WRITE %d lock=
9ebc0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
9ebd0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
9ebe0 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c  );.  rc = SetFil
9ebf0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e  ePointer(pFile->
9ec00 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75  h, lowerBits, &u
9ec10 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42  pperBits, FILE_B
9ec20 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d  EGIN);.  if( rc=
9ec30 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c  =INVALID_SET_FIL
9ec40 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 47 65 74  E_POINTER && Get
9ec50 4c 61 73 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f  LastError()!=NO_
9ec60 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74  ERROR ){.    ret
9ec70 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  urn SQLITE_FULL;
9ec80 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61  .  }.  assert( a
9ec90 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  mt>0 );.  while(
9eca0 0a 20 20 20 20 20 61 6d 74 3e 30 0a 20 20 20 20  .     amt>0.    
9ecb0 20 26 26 20 28 72 63 20 3d 20 57 72 69 74 65 46   && (rc = WriteF
9ecc0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42  ile(pFile->h, pB
9ecd0 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c  uf, amt, &wrote,
9ece0 20 30 29 29 21 3d 30 0a 20 20 20 20 20 26 26 20   0))!=0.     && 
9ecf0 77 72 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20  wrote>0.  ){.   
9ed00 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20   amt -= wrote;. 
9ed10 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61     pBuf = &((cha
9ed20 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b  r*)pBuf)[wrote];
9ed30 0a 20 20 7d 0a 20 20 69 66 28 20 21 72 63 20 7c  .  }.  if( !rc |
9ed40 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65  | amt>(int)wrote
9ed50 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
9ed60 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a  QLITE_FULL;.  }.
9ed70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
9ed80 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
9ed90 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69  ncate an open fi
9eda0 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65  le to a specifie
9edb0 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63  d size.*/.static
9edc0 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65   int winTruncate
9edd0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
9ede0 64 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20  d, i64 nByte){. 
9edf0 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20   LONG upperBits 
9ee00 3d 20 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 20  = (nByte>>32) & 
9ee10 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 4c 4f  0x7fffffff;.  LO
9ee20 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 6e  NG lowerBits = n
9ee30 42 79 74 65 20 26 20 30 78 66 66 66 66 66 66 66  Byte & 0xfffffff
9ee40 66 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  f;.  winFile *pF
9ee50 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
9ee60 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22  id;.  OSTRACE3("
9ee70 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64  TRUNCATE %d %lld
9ee80 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e  \n", pFile->h, n
9ee90 42 79 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74  Byte);.  Simulat
9eea0 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20  eIOError(return 
9eeb0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55  SQLITE_IOERR_TRU
9eec0 4e 43 41 54 45 29 3b 0a 20 20 53 65 74 46 69 6c  NCATE);.  SetFil
9eed0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e  ePointer(pFile->
9eee0 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75  h, lowerBits, &u
9eef0 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42  pperBits, FILE_B
9ef00 45 47 49 4e 29 3b 0a 20 20 53 65 74 45 6e 64 4f  EGIN);.  SetEndO
9ef10 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b  fFile(pFile->h);
9ef20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
9ef30 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  _OK;.}..#ifdef S
9ef40 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
9ef50 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65   Count the numbe
9ef60 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61  r of fullsyncs a
9ef70 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e  nd normal syncs.
9ef80 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74    This is used t
9ef90 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73  o test.** that s
9efa0 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e  yncs and fullsyn
9efb0 63 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20  cs are occuring 
9efc0 61 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d  at the right tim
9efd0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
9efe0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79  I int sqlite3_sy
9eff0 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51  nc_count = 0;.SQ
9f000 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
9f010 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
9f020 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
9f030 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
9f040 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61   all writes to a
9f050 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65   particular file
9f060 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74   are committed t
9f070 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69  o disk..*/.stati
9f080 63 20 69 6e 74 20 77 69 6e 53 79 6e 63 28 73 71  c int winSync(sq
9f090 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
9f0a0 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 77 69  int flags){.  wi
9f0b0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
9f0c0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f  winFile*)id;.  O
9f0d0 53 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 64  STRACE3("SYNC %d
9f0e0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69   lock=%d\n", pFi
9f0f0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f  le->h, pFile->lo
9f100 63 6b 74 79 70 65 29 3b 0a 23 69 66 64 65 66 20  cktype);.#ifdef 
9f110 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66  SQLITE_TEST.  if
9f120 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
9f130 5f 53 59 4e 43 5f 46 55 4c 4c 20 29 7b 0a 20 20  _SYNC_FULL ){.  
9f140 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79    sqlite3_fullsy
9f150 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a  nc_count++;.  }.
9f160 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63    sqlite3_sync_c
9f170 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
9f180 20 69 66 28 20 46 6c 75 73 68 46 69 6c 65 42 75   if( FlushFileBu
9f190 66 66 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 20  ffers(pFile->h) 
9f1a0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
9f1b0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
9f1c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
9f1d0 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 7d  ITE_IOERR;.  }.}
9f1e0 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e  ../*.** Determin
9f1f0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  e the current si
9f200 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20  ze of a file in 
9f210 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20  bytes.*/.static 
9f220 69 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28  int winFileSize(
9f230 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
9f240 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
9f250 2a 70 53 69 7a 65 29 7b 0a 20 20 77 69 6e 46 69  *pSize){.  winFi
9f260 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
9f270 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
9f280 44 20 75 70 70 65 72 42 69 74 73 2c 20 6c 6f 77  D upperBits, low
9f290 65 72 42 69 74 73 3b 0a 20 20 53 69 6d 75 6c 61  erBits;.  Simula
9f2a0 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e  teIOError(return
9f2b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53   SQLITE_IOERR_FS
9f2c0 54 41 54 29 3b 0a 20 20 6c 6f 77 65 72 42 69 74  TAT);.  lowerBit
9f2d0 73 20 3d 20 47 65 74 46 69 6c 65 53 69 7a 65 28  s = GetFileSize(
9f2e0 70 46 69 6c 65 2d 3e 68 2c 20 26 75 70 70 65 72  pFile->h, &upper
9f2f0 42 69 74 73 29 3b 0a 20 20 2a 70 53 69 7a 65 20  Bits);.  *pSize 
9f300 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74  = (((sqlite3_int
9f310 36 34 29 75 70 70 65 72 42 69 74 73 29 3c 3c 33  64)upperBits)<<3
9f320 32 29 20 2b 20 6c 6f 77 65 72 42 69 74 73 3b 0a  2) + lowerBits;.
9f330 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
9f340 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43  OK;.}../*.** LOC
9f350 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44  KFILE_FAIL_IMMED
9f360 49 41 54 45 4c 59 20 69 73 20 75 6e 64 65 66 69  IATELY is undefi
9f370 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 57 69 6e 64  ned on some Wind
9f380 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a  ows systems..*/.
9f390 23 69 66 6e 64 65 66 20 4c 4f 43 4b 46 49 4c 45  #ifndef LOCKFILE
9f3a0 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c  _FAIL_IMMEDIATEL
9f3b0 59 0a 23 20 64 65 66 69 6e 65 20 4c 4f 43 4b 46  Y.# define LOCKF
9f3c0 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41  ILE_FAIL_IMMEDIA
9f3d0 54 45 4c 59 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  TELY 1.#endif../
9f3e0 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 72  *.** Acquire a r
9f3f0 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44  eader lock..** D
9f400 69 66 66 65 72 65 6e 74 20 41 50 49 20 72 6f 75  ifferent API rou
9f410 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64  tines are called
9f420 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68   depending on wh
9f430 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 69  ether or not thi
9f440 73 0a 2a 2a 20 69 73 20 57 69 6e 39 35 20 6f 72  s.** is Win95 or
9f450 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69   WinNT..*/.stati
9f460 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c 6f 63  c int getReadLoc
9f470 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65  k(winFile *pFile
9f480 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20  ){.  int res;.  
9f490 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
9f4a0 20 20 4f 56 45 52 4c 41 50 50 45 44 20 6f 76 6c    OVERLAPPED ovl
9f4b0 70 3b 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73  p;.    ovlp.Offs
9f4c0 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  et = SHARED_FIRS
9f4d0 54 3b 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73  T;.    ovlp.Offs
9f4e0 65 74 48 69 67 68 20 3d 20 30 3b 0a 20 20 20 20  etHigh = 0;.    
9f4f0 6f 76 6c 70 2e 68 45 76 65 6e 74 20 3d 20 30 3b  ovlp.hEvent = 0;
9f500 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46  .    res = LockF
9f510 69 6c 65 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20  ileEx(pFile->h, 
9f520 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d  LOCKFILE_FAIL_IM
9f530 4d 45 44 49 41 54 45 4c 59 2c 0a 20 20 20 20 20  MEDIATELY,.     
9f540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f550 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20  0, SHARED_SIZE, 
9f560 30 2c 20 26 6f 76 6c 70 29 3b 0a 20 20 7d 65 6c  0, &ovlp);.  }el
9f570 73 65 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a  se{.    int lk;.
9f580 20 20 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f      sqlite3Rando
9f590 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29  mness(sizeof(lk)
9f5a0 2c 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c  , &lk);.    pFil
9f5b0 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e->sharedLockByt
9f5c0 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66  e = (lk & 0x7fff
9f5d0 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49  ffff)%(SHARED_SI
9f5e0 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 72 65 73  ZE - 1);.    res
9f5f0 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   = LockFile(pFil
9f600 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52  e->h, SHARED_FIR
9f610 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  ST+pFile->shared
9f620 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20  LockByte, 0, 1, 
9f630 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
9f640 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55   res;.}../*.** U
9f650 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a  ndo a readlock.*
9f660 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c  /.static int unl
9f670 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46  ockReadLock(winF
9f680 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69  ile *pFile){.  i
9f690 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73  nt res;.  if( is
9f6a0 4e 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73 20  NT() ){.    res 
9f6b0 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69  = UnlockFile(pFi
9f6c0 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
9f6d0 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53  RST, 0, SHARED_S
9f6e0 49 5a 45 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  IZE, 0);.  }else
9f6f0 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f  {.    res = Unlo
9f700 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
9f710 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20   SHARED_FIRST + 
9f720 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63  pFile->sharedLoc
9f730 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b  kByte, 0, 1, 0);
9f740 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65  .  }.  return re
9f750 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  s;.}../*.** Lock
9f760 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
9f770 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
9f780 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c  d by parameter l
9f790 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a  ocktype - one.**
9f7a0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
9f7b0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  g:.**.**     (1)
9f7c0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20   SHARED_LOCK.** 
9f7d0 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
9f7e0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29  _LOCK.**     (3)
9f7f0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a   PENDING_LOCK.**
9f800 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
9f810 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f  VE_LOCK.**.** So
9f820 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
9f830 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
9f840 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
9f850 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a  al lock states.*
9f860 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
9f870 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
9f880 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
9f890 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
9f8a0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69   later.** transi
9f8b0 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68  tions leaving th
9f8c0 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66  e lock state dif
9f8d0 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74  ferent from what
9f8e0 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a   it started but.
9f8f0 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
9f900 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
9f910 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
9f920 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
9f930 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  ed.** transition
9f940 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74  s and the insert
9f950 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ed intermediate 
9f960 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
9f970 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41   UNLOCKED -> SHA
9f980 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  RED.**    SHARED
9f990 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20   -> RESERVED.** 
9f9a0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
9f9b0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
9f9c0 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  IVE.**    RESERV
9f9d0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
9f9e0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
9f9f0 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58     PENDING -> EX
9fa00 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68  CLUSIVE.**.** Th
9fa10 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
9fa20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
9fa30 6c 6f 63 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e  lock.  The winUn
9fa40 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a  lock() routine.*
9fa50 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63  * erases all loc
9fa60 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72  ks at once and r
9fa70 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69  eturns us immedi
9fa80 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67  ately to locking
9fa90 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20   level 0..** It 
9faa0 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
9fab0 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63  to lower the loc
9fac0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73  king level one s
9fad0 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20  tep at a time.  
9fae0 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73  You.** must go s
9faf0 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69  traight to locki
9fb00 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73  ng level 0..*/.s
9fb10 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63  tatic int winLoc
9fb20 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
9fb30 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
9fb40 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
9fb50 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52  LITE_OK;    /* R
9fb60 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
9fb70 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20  subroutines */. 
9fb80 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20   int res = 1;   
9fb90 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
9fba0 74 20 6f 66 20 61 20 77 69 6e 64 6f 77 73 20 6c  t of a windows l
9fbb0 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e  ock call */.  in
9fbc0 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20  t newLocktype;  
9fbd0 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 69 6c       /* Set pFil
9fbe0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74  e->locktype to t
9fbf0 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65  his value before
9fc00 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e   exiting */.  in
9fc10 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b  t gotPendingLock
9fc20 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 20   = 0;/* True if 
9fc30 77 65 20 61 63 71 75 69 72 65 64 20 61 20 50 45  we acquired a PE
9fc40 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20  NDING lock this 
9fc50 74 69 6d 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c  time */.  winFil
9fc60 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46  e *pFile = (winF
9fc70 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65  ile*)id;..  asse
9fc80 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a  rt( pFile!=0 );.
9fc90 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b    OSTRACE5("LOCK
9fca0 20 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64   %d %d was %d(%d
9fcb0 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  )\n",.          
9fcc0 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
9fcd0 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  pe, pFile->lockt
9fce0 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72  ype, pFile->shar
9fcf0 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20  edLockByte);..  
9fd00 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
9fd10 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66  lready a lock of
9fd20 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f   this type or mo
9fd30 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f  re restrictive o
9fd40 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c  n the.  ** OsFil
9fd50 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44  e, do nothing. D
9fd60 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64  on't use the end
9fd70 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68  _lock: exit path
9fd80 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65  , as.  ** sqlite
9fd90 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 20  3OsEnterMutex() 
9fda0 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c  hasn't been call
9fdb0 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69  ed yet..  */.  i
9fdc0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
9fdd0 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe>=locktype ){.
9fde0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
9fdf0 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
9fe00 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f  Make sure the lo
9fe10 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  cking sequence i
9fe20 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20  s correct.  */. 
9fe30 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
9fe40 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
9fe50 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  K || locktype==S
9fe60 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
9fe70 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
9fe80 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  !=PENDING_LOCK )
9fe90 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
9fea0 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c  type!=RESERVED_L
9feb0 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f  OCK || pFile->lo
9fec0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
9fed0 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  OCK );..  /* Loc
9fee0 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f  k the PENDING_LO
9fef0 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65  CK byte if we ne
9ff00 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20  ed to acquire a 
9ff10 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a  PENDING lock or.
9ff20 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f    ** a SHARED lo
9ff30 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61  ck.  If we are a
9ff40 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45  cquiring a SHARE
9ff50 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75  D lock, the acqu
9ff60 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20  isition of.  ** 
9ff70 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  the PENDING_LOCK
9ff80 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61   byte is tempora
9ff90 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f  ry..  */.  newLo
9ffa0 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e  cktype = pFile->
9ffb0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20  locktype;.  if( 
9ffc0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
9ffd0 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 7c 7c 20 28  =NO_LOCK.   || (
9ffe0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
9fff0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c  IVE_LOCK && pFil
a0000 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  e->locktype==RES
a0010 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b  ERVED_LOCK).  ){
a0020 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 33  .    int cnt = 3
a0030 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6e 74  ;.    while( cnt
a0040 2d 2d 3e 30 20 26 26 20 28 72 65 73 20 3d 20 4c  -->0 && (res = L
a0050 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
a0060 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
a0070 30 2c 20 31 2c 20 30 29 29 3d 3d 30 20 29 7b 0a  0, 1, 0))==0 ){.
a0080 20 20 20 20 20 20 2f 2a 20 54 72 79 20 33 20 74        /* Try 3 t
a0090 69 6d 65 73 20 74 6f 20 67 65 74 20 74 68 65 20  imes to get the 
a00a0 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 54  pending lock.  T
a00b0 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20  he pending lock 
a00c0 6d 69 67 68 74 20 62 65 0a 20 20 20 20 20 20 2a  might be.      *
a00d0 2a 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65  * held by anothe
a00e0 72 20 72 65 61 64 65 72 20 70 72 6f 63 65 73 73  r reader process
a00f0 20 77 68 6f 20 77 69 6c 6c 20 72 65 6c 65 61 73   who will releas
a0100 65 20 69 74 20 6d 6f 6d 65 6e 74 61 72 69 6c 79  e it momentarily
a0110 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
a0120 20 4f 53 54 52 41 43 45 32 28 22 63 6f 75 6c 64   OSTRACE2("could
a0130 20 6e 6f 74 20 67 65 74 20 61 20 50 45 4e 44 49   not get a PENDI
a0140 4e 47 20 6c 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c  NG lock. cnt=%d\
a0150 6e 22 2c 20 63 6e 74 29 3b 0a 20 20 20 20 20 20  n", cnt);.      
a0160 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a  Sleep(1);.    }.
a0170 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f      gotPendingLo
a0180 63 6b 20 3d 20 72 65 73 3b 0a 20 20 7d 0a 0a 20  ck = res;.  }.. 
a0190 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 73 68   /* Acquire a sh
a01a0 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  ared lock.  */. 
a01b0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
a01c0 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65  HARED_LOCK && re
a01d0 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  s ){.    assert(
a01e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
a01f0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20  ==NO_LOCK );.   
a0200 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f   res = getReadLo
a0210 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69  ck(pFile);.    i
a0220 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20  f( res ){.      
a0230 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48  newLocktype = SH
a0240 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  ARED_LOCK;.    }
a0250 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
a0260 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  re a RESERVED lo
a0270 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
a0280 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44  cktype==RESERVED
a0290 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
a02a0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
a02b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  e->locktype==SHA
a02c0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
a02d0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70  res = LockFile(p
a02e0 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45  File->h, RESERVE
a02f0 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  D_BYTE, 0, 1, 0)
a0300 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
a0310 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
a0320 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f  pe = RESERVED_LO
a0330 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  CK;.    }.  }.. 
a0340 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45   /* Acquire a PE
a0350 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a  NDING lock.  */.
a0360 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
a0370 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26  EXCLUSIVE_LOCK &
a0380 26 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65 77  & res ){.    new
a0390 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49  Locktype = PENDI
a03a0 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74  NG_LOCK;.    got
a03b0 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b  PendingLock = 0;
a03c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
a03d0 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  re an EXCLUSIVE 
a03e0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
a03f0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
a0400 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  IVE_LOCK && res 
a0410 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
a0420 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
a0430 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
a0440 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52     res = unlockR
a0450 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
a0460 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 75 6e      OSTRACE2("un
a0470 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22  readlock = %d\n"
a0480 2c 20 72 65 73 29 3b 0a 20 20 20 20 72 65 73 20  , res);.    res 
a0490 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
a04a0 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
a04b0 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a  T, 0, SHARED_SIZ
a04c0 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  E, 0);.    if( r
a04d0 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c  es ){.      newL
a04e0 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53  ocktype = EXCLUS
a04f0 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65  IVE_LOCK;.    }e
a0500 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41  lse{.      OSTRA
a0510 43 45 32 28 22 65 72 72 6f 72 2d 63 6f 64 65 20  CE2("error-code 
a0520 3d 20 25 64 5c 6e 22 2c 20 47 65 74 4c 61 73 74  = %d\n", GetLast
a0530 45 72 72 6f 72 28 29 29 3b 0a 20 20 20 20 20 20  Error());.      
a0540 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
a0550 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  e);.    }.  }.. 
a0560 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f   /* If we are ho
a0570 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20  lding a PENDING 
a0580 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20  lock that ought 
a0590 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20  to be released, 
a05a0 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73  then.  ** releas
a05b0 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20  e it now..  */. 
a05c0 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c   if( gotPendingL
a05d0 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d  ock && locktype=
a05e0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
a05f0 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70      UnlockFile(p
a0600 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
a0610 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
a0620 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
a0630 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
a0640 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64  he lock has held
a0650 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73   in the file des
a0660 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a  criptor then.  *
a0670 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70  * return the app
a0680 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20  ropriate result 
a0690 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  code..  */.  if(
a06a0 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 20 3d   res ){.    rc =
a06b0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
a06c0 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45  lse{.    OSTRACE
a06d0 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25  4("LOCK FAILED %
a06e0 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20  d trying for %d 
a06f0 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70  but got %d\n", p
a0700 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
a0710 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65      locktype, ne
a0720 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20  wLocktype);.    
a0730 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
a0740 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c  ;.  }.  pFile->l
a0750 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63  ocktype = newLoc
a0760 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20  ktype;.  return 
a0770 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
a0780 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
a0790 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
a07a0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
a07b0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
a07c0 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  ed.** file by th
a07d0 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
a07e0 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
a07f0 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
a0800 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a   return.** non-z
a0810 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a  ero, otherwise z
a0820 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ero..*/.static i
a0830 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 73 65 72  nt winCheckReser
a0840 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
a0850 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74  file *id){.  int
a0860 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a   rc;.  winFile *
a0870 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
a0880 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20  *)id;.  assert( 
a0890 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66  pFile!=0 );.  if
a08a0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
a08b0 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
a08c0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a   ){.    rc = 1;.
a08d0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45      OSTRACE3("TE
a08e0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
a08f0 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69   (local)\n", pFi
a0900 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65  le->h, rc);.  }e
a0910 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f  lse{.    rc = Lo
a0920 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
a0930 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
a0940 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 1, 0);.    if
a0950 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e  ( rc ){.      Un
a0960 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
a0970 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  h, RESERVED_BYTE
a0980 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  , 0, 1, 0);.    
a0990 7d 0a 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a  }.    rc = !rc;.
a09a0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45      OSTRACE3("TE
a09b0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
a09c0 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46   (remote)\n", pF
a09d0 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d  ile->h, rc);.  }
a09e0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
a09f0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
a0a00 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
a0a10 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
a0a20 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65  r id to locktype
a0a30 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
a0a40 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
a0a50 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
a0a60 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
a0a70 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
a0a80 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
a0a90 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
a0aa0 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
a0ab0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
a0ac0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
a0ad0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
a0ae0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74   no-op..**.** It
a0af0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
a0b00 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e   for this routin
a0b10 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65  e to fail if the
a0b20 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
a0b30 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20  .** is NO_LOCK. 
a0b40 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   If the second a
a0b50 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45  rgument is SHARE
a0b60 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73  D_LOCK then this
a0b70 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68   routine.** migh
a0b80 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  t return SQLITE_
a0b90 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63  IOERR;.*/.static
a0ba0 20 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73   int winUnlock(s
a0bb0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
a0bc0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
a0bd0 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77 69    int type;.  wi
a0be0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
a0bf0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69  winFile*)id;.  i
a0c00 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
a0c10 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  K;.  assert( pFi
a0c20 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  le!=0 );.  asser
a0c30 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
a0c40 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53  RED_LOCK );.  OS
a0c50 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 25  TRACE5("UNLOCK %
a0c60 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 25  d to %d was %d(%
a0c70 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  d)\n", pFile->h,
a0c80 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20   locktype,.     
a0c90 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b       pFile->lock
a0ca0 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61  type, pFile->sha
a0cb0 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 20  redLockByte);.  
a0cc0 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  type = pFile->lo
a0cd0 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79  cktype;.  if( ty
a0ce0 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe>=EXCLUSIVE_LO
a0cf0 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b  CK ){.    Unlock
a0d00 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53  File(pFile->h, S
a0d10 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20  HARED_FIRST, 0, 
a0d20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b  SHARED_SIZE, 0);
a0d30 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  .    if( locktyp
a0d40 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
a0d50 26 20 21 67 65 74 52 65 61 64 4c 6f 63 6b 28 70  & !getReadLock(p
a0d60 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 20 20 2f  File) ){.      /
a0d70 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65  * This should ne
a0d80 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20  ver happen.  We 
a0d90 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
a0da0 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a   able to.      *
a0db0 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20  * reacquire the 
a0dc0 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  read lock */.   
a0dd0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
a0de0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20  OERR_UNLOCK;.   
a0df0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70   }.  }.  if( typ
a0e00 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
a0e10 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69   ){.    UnlockFi
a0e20 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53  le(pFile->h, RES
a0e30 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31  ERVED_BYTE, 0, 1
a0e40 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 0);.  }.  if( 
a0e50 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
a0e60 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45  K && type>=SHARE
a0e70 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e  D_LOCK ){.    un
a0e80 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69  lockReadLock(pFi
a0e90 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74  le);.  }.  if( t
a0ea0 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype>=PENDING_LOC
a0eb0 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46  K ){.    UnlockF
a0ec0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45  ile(pFile->h, PE
a0ed0 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31  NDING_BYTE, 0, 1
a0ee0 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c  , 0);.  }.  pFil
a0ef0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
a0f00 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e  cktype;.  return
a0f10 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   rc;.}../*.** Co
a0f20 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20  ntrol and query 
a0f30 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65  of the open file
a0f40 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74   handle..*/.stat
a0f50 69 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f  ic int winFileCo
a0f60 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69  ntrol(sqlite3_fi
a0f70 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20  le *id, int op, 
a0f80 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73  void *pArg){.  s
a0f90 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
a0fa0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e   case SQLITE_FCN
a0fb0 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a  TL_LOCKSTATE: {.
a0fc0 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72        *(int*)pAr
a0fd0 67 20 3d 20 28 28 77 69 6e 46 69 6c 65 2a 29 69  g = ((winFile*)i
a0fe0 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  d)->locktype;.  
a0ff0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
a1000 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
a1010 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a1020 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ERROR;.}../*.** 
a1030 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f  Return the secto
a1040 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  r size in bytes 
a1050 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
a1060 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66  g block device f
a1070 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66  or.** the specif
a1080 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69  ied file. This i
a1090 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  s almost always 
a10a0 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d  512 bytes, but m
a10b0 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20  ay be.** larger 
a10c0 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73  for some devices
a10d0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  ..**.** SQLite c
a10e0 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73  ode assumes this
a10f0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74   function cannot
a1100 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61   fail. It also a
a1110 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69  ssumes that.** i
a1120 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20  f two files are 
a1130 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73  created in the s
a1140 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  ame file-system 
a1150 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a  directory (i.e..
a1160 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e  ** a database an
a1170 64 20 69 74 27 73 20 6a 6f 75 72 6e 61 6c 20 66  d it's journal f
a1180 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65  ile) that the se
a1190 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62  ctor size will b
a11a0 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f  e the.** same fo
a11b0 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69  r both..*/.stati
a11c0 63 20 69 6e 74 20 77 69 6e 53 65 63 74 6f 72 53  c int winSectorS
a11d0 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
a11e0 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20   *id){.  return 
a11f0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53  SQLITE_DEFAULT_S
a1200 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f  ECTOR_SIZE;.}../
a1210 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 76 65  *.** Return a ve
a1220 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65 20 63  ctor of device c
a1230 68 61 72 61 63 74 65 72 69 73 74 69 63 73 2e 0a  haracteristics..
a1240 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
a1250 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  nDeviceCharacter
a1260 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66  istics(sqlite3_f
a1270 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75  ile *id){.  retu
a1280 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 0;.}../*.** T
a1290 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e  his vector defin
a12a0 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f  es all the metho
a12b0 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72  ds that can oper
a12c0 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c  ate on an.** sql
a12d0 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 77 69  ite3_file for wi
a12e0 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  n32..*/.static c
a12f0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
a1300 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74  methods winIoMet
a1310 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20  hod = {.  1,    
a1320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1330 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
a1340 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20  */.  winClose,. 
a1350 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57   winRead,.  winW
a1360 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63  rite,.  winTrunc
a1370 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a  ate,.  winSync,.
a1380 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20    winFileSize,. 
a1390 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55   winLock,.  winU
a13a0 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63  nlock,.  winChec
a13b0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20  kReservedLock,. 
a13c0 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c   winFileControl,
a13d0 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65  .  winSectorSize
a13e0 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43 68 61  ,.  winDeviceCha
a13f0 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a  racteristics.};.
a1400 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
a1450 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 49   Here ends the I
a1460 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  /O methods that 
a1470 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 33  form the sqlite3
a1480 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
a1490 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  ct..**.** The ne
a14a0 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65  xt block of code
a14b0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
a14c0 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a  VFS methods..***
a14d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a14e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a14f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
a1520 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d  * Convert a UTF-
a1530 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20  8 filename into 
a1540 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20 74 68  whatever form th
a1550 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20  e underlying.** 
a1560 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
a1570 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73   wants filenames
a1580 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 68   in.  Space to h
a1590 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  old the result.*
a15a0 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  * is obtained fr
a15b0 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75  om malloc and mu
a15c0 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  st be freed by t
a15d0 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75  he calling.** fu
a15e0 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
a15f0 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55  c void *convertU
a1600 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73  tf8Filename(cons
a1610 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
a1620 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e  e){.  void *zCon
a1630 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20 69 66  verted = 0;.  if
a1640 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
a1650 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66  zConverted = utf
a1660 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65  8ToUnicode(zFile
a1670 6e 61 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  name);.  }else{.
a1680 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d      zConverted =
a1690 20 75 74 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c   utf8ToMbcs(zFil
a16a0 65 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 2f 2a  ename);.  }.  /*
a16b0 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 68 61 6e   caller will han
a16c0 64 6c 65 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  dle out of memor
a16d0 79 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 7a 43  y */.  return zC
a16e0 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a  onverted;.}../*.
a16f0 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a  ** Open a file..
a1700 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
a1710 6e 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  nOpen(.  sqlite3
a1720 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
a1730 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a     /* Not used *
a1740 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
a1750 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a  zName,        /*
a1760 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
a1770 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73  e (UTF-8) */.  s
a1780 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
a1790 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
a17a0 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c  e the SQLite fil
a17b0 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f  e handle here */
a17c0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
a17d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a17e0 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20  Open mode flags 
a17f0 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  */.  int *pOutFl
a1800 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
a1810 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20  * Status return 
a1820 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41  flags */.){.  HA
a1830 4e 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20  NDLE h;.  DWORD 
a1840 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b  dwDesiredAccess;
a1850 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65  .  DWORD dwShare
a1860 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77  Mode;.  DWORD dw
a1870 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
a1880 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46  ion;.  DWORD dwF
a1890 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
a18a0 73 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65  s = 0;.  winFile
a18b0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
a18c0 6c 65 2a 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a  le*)id;.  void *
a18d0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
a18e0 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
a18f0 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a  (zName);.  if( z
a1900 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a  Converted==0 ){.
a1910 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
a1920 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
a1930 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
a1940 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
a1950 45 20 29 7b 0a 20 20 20 20 64 77 44 65 73 69 72  E ){.    dwDesir
a1960 65 64 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52  edAccess = GENER
a1970 49 43 5f 52 45 41 44 20 7c 20 47 45 4e 45 52 49  IC_READ | GENERI
a1980 43 5f 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65  C_WRITE;.  }else
a1990 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41  {.    dwDesiredA
a19a0 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f  ccess = GENERIC_
a19b0 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20  READ;.  }.  if( 
a19c0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
a19d0 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20  PEN_CREATE ){.  
a19e0 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70    dwCreationDisp
a19f0 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41  osition = OPEN_A
a1a00 4c 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a  LWAYS;.  }else{.
a1a10 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69      dwCreationDi
a1a20 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e  sposition = OPEN
a1a30 5f 45 58 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20  _EXISTING;.  }. 
a1a40 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
a1a50 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
a1a60 20 29 7b 0a 20 20 20 20 64 77 53 68 61 72 65 4d   ){.    dwShareM
a1a70 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48 41 52 45  ode = FILE_SHARE
a1a80 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f 53 48 41  _READ | FILE_SHA
a1a90 52 45 5f 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73  RE_WRITE;.  }els
a1aa0 65 7b 0a 20 20 20 20 64 77 53 68 61 72 65 4d 6f  e{.    dwShareMo
a1ab0 64 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  de = 0;.  }.  if
a1ac0 28 20 66 6c 61 67 73 20 26 20 28 53 51 4c 49 54  ( flags & (SQLIT
a1ad0 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 7c  E_OPEN_TEMP_DB |
a1ae0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d   SQLITE_OPEN_TEM
a1af0 50 5f 4a 4f 55 52 4e 41 4c 0a 20 20 20 20 20 20  P_JOURNAL.      
a1b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
a1b10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a  SQLITE_OPEN_SUBJ
a1b20 4f 55 52 4e 41 4c 29 20 29 7b 0a 20 20 20 20 64  OURNAL) ){.    d
a1b30 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75  wFlagsAndAttribu
a1b40 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49  tes = FILE_ATTRI
a1b50 42 55 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20  BUTE_TEMPORARY. 
a1b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
a1b80 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48  FILE_ATTRIBUTE_H
a1b90 49 44 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20  IDDEN.          
a1ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1bb0 20 20 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47       | FILE_FLAG
a1bc0 5f 44 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45  _DELETE_ON_CLOSE
a1bd0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
a1be0 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75  wFlagsAndAttribu
a1bf0 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49  tes = FILE_ATTRI
a1c00 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d  BUTE_NORMAL;.  }
a1c10 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 28  .  if( flags & (
a1c20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
a1c30 5f 44 42 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  _DB | SQLITE_OPE
a1c40 4e 5f 54 45 4d 50 5f 44 42 29 20 29 7b 0a 20 20  N_TEMP_DB) ){.  
a1c50 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72    dwFlagsAndAttr
a1c60 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f 46  ibutes |= FILE_F
a1c70 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53  LAG_RANDOM_ACCES
a1c80 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  S;.  }else{.    
a1c90 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
a1ca0 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f 46 4c 41  utes |= FILE_FLA
a1cb0 47 5f 53 45 51 55 45 4e 54 49 41 4c 5f 53 43 41  G_SEQUENTIAL_SCA
a1cc0 4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e  N;.  }.  if( isN
a1cd0 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 43  T() ){.    h = C
a1ce0 72 65 61 74 65 46 69 6c 65 57 28 28 57 43 48 41  reateFileW((WCHA
a1cf0 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20  R*)zConverted,. 
a1d00 20 20 20 20 20 20 64 77 44 65 73 69 72 65 64 41        dwDesiredA
a1d10 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77  ccess,.       dw
a1d20 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20  ShareMode,.     
a1d30 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64    NULL,.       d
a1d40 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69  wCreationDisposi
a1d50 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46  tion,.       dwF
a1d60 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
a1d70 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20  s,.       NULL. 
a1d80 20 20 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23     );.  }else{.#
a1d90 69 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20  if OS_WINCE.    
a1da0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
a1db0 4d 45 4d 3b 0a 23 65 6c 73 65 0a 20 20 20 20 68  MEM;.#else.    h
a1dc0 20 3d 20 43 72 65 61 74 65 46 69 6c 65 41 28 28   = CreateFileA((
a1dd0 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64  char*)zConverted
a1de0 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72  ,.       dwDesir
a1df0 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20  edAccess,.      
a1e00 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20   dwShareMode,.  
a1e10 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20       NULL,.     
a1e20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70    dwCreationDisp
a1e30 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20  osition,.       
a1e40 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
a1e50 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c  utes,.       NUL
a1e60 4c 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a  L.    );.#endif.
a1e70 20 20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56    }.  if( h==INV
a1e80 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55  ALID_HANDLE_VALU
a1e90 45 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 43  E ){.    free(zC
a1ea0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 69  onverted);.    i
a1eb0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
a1ec0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
a1ed0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
a1ee0 20 77 69 6e 4f 70 65 6e 28 30 2c 20 7a 4e 61 6d   winOpen(0, zNam
a1ef0 65 2c 20 69 64 2c 20 0a 20 20 20 20 20 20 20 20  e, id, .        
a1f00 20 20 20 20 20 28 28 66 6c 61 67 73 7c 53 51 4c       ((flags|SQL
a1f10 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
a1f20 59 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  Y)&~SQLITE_OPEN_
a1f30 52 45 41 44 57 52 49 54 45 29 2c 20 70 4f 75 74  READWRITE), pOut
a1f40 46 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65 6c 73  Flags);.    }els
a1f50 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
a1f60 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
a1f70 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
a1f80 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20   pOutFlags ){.  
a1f90 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51    if( flags & SQ
a1fa0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
a1fb0 49 54 45 20 29 7b 0a 20 20 20 20 20 20 2a 70 4f  ITE ){.      *pO
a1fc0 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45  utFlags = SQLITE
a1fd0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 3b  _OPEN_READWRITE;
a1fe0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
a1ff0 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53    *pOutFlags = S
a2000 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
a2010 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  NLY;.    }.  }. 
a2020 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30   memset(pFile, 0
a2030 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29  , sizeof(*pFile)
a2040 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74  );.  pFile->pMet
a2050 68 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 74 68  hod = &winIoMeth
a2060 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d  od;.  pFile->h =
a2070 20 68 3b 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45   h;.#if OS_WINCE
a2080 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 20  .  if( (flags & 
a2090 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  (SQLITE_OPEN_REA
a20a0 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
a20b0 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a  EN_MAIN_DB)) ==.
a20c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
a20d0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
a20e0 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45  WRITE|SQLITE_OPE
a20f0 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20 20  N_MAIN_DB).     
a2100 20 20 26 26 20 21 77 69 6e 63 65 43 72 65 61 74    && !winceCreat
a2110 65 4c 6f 63 6b 28 7a 46 69 6c 65 6e 61 6d 65 2c  eLock(zFilename,
a2120 20 26 66 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c   &f).  ){.    Cl
a2130 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20  oseHandle(h);.  
a2140 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
a2150 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  d);.    return S
a2160 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
a2170 20 20 7d 0a 20 20 69 66 28 20 64 77 46 6c 61 67    }.  if( dwFlag
a2180 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 26  sAndAttributes &
a2190 20 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 54   FILE_FLAG_DELET
a21a0 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20  EONCLOSE ){.    
a21b0 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e  pFile->zDeleteOn
a21c0 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e 76 65 72 74  Close = zConvert
a21d0 65 64 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  ed;.  }else.#end
a21e0 69 66 0a 20 20 7b 0a 20 20 20 20 66 72 65 65 28  if.  {.    free(
a21f0 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 7d  zConverted);.  }
a2200 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b  .  OpenCounter(+
a2210 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  1);.  return SQL
a2220 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
a2230 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65   Delete the name
a2240 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  d file..**.** No
a2250 74 65 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20  te that windows 
a2260 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61  does not allow a
a2270 20 66 69 6c 65 20 74 6f 20 62 65 20 64 65 6c 65   file to be dele
a2280 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  ted if some othe
a2290 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 68 61 73  r.** process has
a22a0 20 69 74 20 6f 70 65 6e 2e 20 20 53 6f 6d 65 74   it open.  Somet
a22b0 69 6d 65 73 20 61 20 76 69 72 75 73 20 73 63 61  imes a virus sca
a22c0 6e 6e 65 72 20 6f 72 20 69 6e 64 65 78 69 6e 67  nner or indexing
a22d0 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 77 69 6c 6c   program.** will
a22e0 20 6f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20   open a journal 
a22f0 66 69 6c 65 20 73 68 6f 72 74 6c 79 20 61 66 74  file shortly aft
a2300 65 72 20 69 74 20 69 73 20 63 72 65 61 74 65 64  er it is created
a2310 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 0a   in order to do.
a2320 2a 2a 20 77 68 61 74 65 76 65 72 20 69 74 20 69  ** whatever it i
a2330 73 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c  s it does.  Whil
a2340 65 20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f  e this other pro
a2350 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20  cess is holding 
a2360 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e  the.** file open
a2370 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61  , we will be una
a2380 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74  ble to delete it
a2390 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  .  To work aroun
a23a0 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65  d this.** proble
a23b0 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30 20  m, we delay 100 
a23c0 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64  milliseconds and
a23d0 20 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61   try to delete a
a23e0 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20  gain.  Up.** to 
a23f0 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45  MX_DELETION_ATTE
a2400 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74  MPTs deletion at
a2410 74 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62  tempts are run b
a2420 65 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20  efore giving.** 
a2430 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67  up and returning
a2440 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64   an error..*/.#d
a2450 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f  efine MX_DELETIO
a2460 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a 73 74 61  N_ATTEMPTS 3.sta
a2470 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 74  tic int winDelet
a2480 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  e(.  sqlite3_vfs
a2490 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20   *pVfs,         
a24a0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20   /* Not used on 
a24b0 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74  win32 */.  const
a24c0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
a24d0 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ,      /* Name o
a24e0 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65  f file to delete
a24f0 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69   */.  int syncDi
a2500 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
a2510 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e    /* Not used on
a2520 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69   win32 */.){.  i
a2530 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 6e  nt cnt = 0;.  in
a2540 74 20 72 63 3b 0a 20 20 76 6f 69 64 20 2a 7a 43  t rc;.  void *zC
a2550 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65  onverted = conve
a2560 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a  rtUtf8Filename(z
a2570 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28  Filename);.  if(
a2580 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29   zConverted==0 )
a2590 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
a25a0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
a25b0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
a25c0 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49  (return SQLITE_I
a25d0 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20  OERR_DELETE);.  
a25e0 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
a25f0 20 20 64 6f 7b 0a 20 20 20 20 20 20 72 63 20 3d    do{.      rc =
a2600 20 44 65 6c 65 74 65 46 69 6c 65 57 28 7a 43 6f   DeleteFileW(zCo
a2610 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77  nverted);.    }w
a2620 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 47  hile( rc==0 && G
a2630 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
a2640 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 21 3d 30  W(zConverted)!=0
a2650 78 66 66 66 66 66 66 66 66 20 0a 20 20 20 20 20  xffffffff .     
a2660 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20         && cnt++ 
a2670 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54  < MX_DELETION_AT
a2680 54 45 4d 50 54 53 20 26 26 20 28 53 6c 65 65 70  TEMPTS && (Sleep
a2690 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 20 20 7d  (100), 1) );.  }
a26a0 65 6c 73 65 7b 0a 23 69 66 20 4f 53 5f 57 49 4e  else{.#if OS_WIN
a26b0 43 45 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  CE.    return SQ
a26c0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 23 65 6c 73  LITE_NOMEM;.#els
a26d0 65 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20  e.    do{.      
a26e0 72 63 20 3d 20 44 65 6c 65 74 65 46 69 6c 65 41  rc = DeleteFileA
a26f0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
a2700 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 30 20    }while( rc==0 
a2710 26 26 20 47 65 74 46 69 6c 65 41 74 74 72 69 62  && GetFileAttrib
a2720 75 74 65 73 41 28 7a 43 6f 6e 76 65 72 74 65 64  utesA(zConverted
a2730 29 21 3d 30 78 66 66 66 66 66 66 66 66 0a 20 20  )!=0xffffffff.  
a2740 20 20 20 20 20 20 20 20 20 20 26 26 20 63 6e 74            && cnt
a2750 2b 2b 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e  ++ < MX_DELETION
a2760 5f 41 54 54 45 4d 50 54 53 20 26 26 20 28 53 6c  _ATTEMPTS && (Sl
a2770 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a  eep(100), 1) );.
a2780 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65  #endif.  }.  fre
a2790 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
a27a0 20 4f 53 54 52 41 43 45 32 28 22 44 45 4c 45 54   OSTRACE2("DELET
a27b0 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69  E \"%s\"\n", zFi
a27c0 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72  lename);.  retur
a27d0 6e 20 72 63 21 3d 30 20 3f 20 53 51 4c 49 54 45  n rc!=0 ? SQLITE
a27e0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
a27f0 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  RR;.}../*.** Che
a2800 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  ck the existance
a2810 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61   and status of a
a2820 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
a2830 20 69 6e 74 20 77 69 6e 41 63 63 65 73 73 28 0a   int winAccess(.
a2840 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
a2850 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Vfs,         /* 
a2860 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33  Not used on win3
a2870 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  2 */.  const cha
a2880 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
a2890 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
a28a0 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20  e to check */.  
a28b0 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20 20  int flags       
a28c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79             /* Ty
a28d0 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61  pe of test to ma
a28e0 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  ke on this file 
a28f0 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 61 74  */.){.  DWORD at
a2900 74 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  tr;.  int rc;.  
a2910 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
a2920 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69   = convertUtf8Fi
a2930 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65  lename(zFilename
a2940 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72  );.  if( zConver
a2950 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ted==0 ){.    re
a2960 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
a2970 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e  M;.  }.  if( isN
a2980 54 28 29 20 29 7b 0a 20 20 20 20 61 74 74 72 20  T() ){.    attr 
a2990 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  = GetFileAttribu
a29a0 74 65 73 57 28 28 57 43 48 41 52 2a 29 7a 43 6f  tesW((WCHAR*)zCo
a29b0 6e 76 65 72 74 65 64 29 3b 0a 20 20 7d 65 6c 73  nverted);.  }els
a29c0 65 7b 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a  e{.#if OS_WINCE.
a29d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
a29e0 45 5f 4e 4f 4d 45 4d 3b 0a 23 65 6c 73 65 0a 20  E_NOMEM;.#else. 
a29f0 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c     attr = GetFil
a2a00 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68  eAttributesA((ch
a2a10 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ar*)zConverted);
a2a20 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72  .#endif.  }.  fr
a2a30 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
a2a40 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20    switch( flags 
a2a50 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
a2a60 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a  TE_ACCESS_READ:.
a2a70 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
a2a80 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20  ACCESS_EXISTS:. 
a2a90 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d       rc = attr!=
a2aa0 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 20 20  0xffffffff;.    
a2ab0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
a2ac0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
a2ad0 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20  READWRITE:.     
a2ae0 20 72 63 20 3d 20 28 61 74 74 72 20 26 20 46 49   rc = (attr & FI
a2af0 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 52 45 41  LE_ATTRIBUTE_REA
a2b00 44 4f 4e 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20  DONLY)==0;.     
a2b10 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
a2b20 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72  ult:.      asser
a2b30 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67  t(!"Invalid flag
a2b40 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20  s argument");.  
a2b50 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
a2b60 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  .../*.** Create 
a2b70 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
a2b80 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20   name in zBuf.  
a2b90 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67  zBuf must be big
a2ba0 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f   enough to.** ho
a2bb0 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61  ld at pVfs->mxPa
a2bc0 74 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72  thname character
a2bd0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
a2be0 20 77 69 6e 47 65 74 54 65 6d 70 4e 61 6d 65 28   winGetTempName(
a2bf0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
a2c00 73 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  s, char *zBuf){.
a2c10 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a 43    static char zC
a2c20 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62  hars[] =.    "ab
a2c30 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72  cdefghijklmnopqr
a2c40 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41  stuvwxyz".    "A
a2c50 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51  BCDEFGHIJKLMNOPQ
a2c60 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22  RSTUVWXYZ".    "
a2c70 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69  0123456789";.  i
a2c80 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20  nt i, j;.  char 
a2c90 7a 54 65 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41  zTempPath[MAX_PA
a2ca0 54 48 2b 31 5d 3b 0a 20 20 69 66 28 20 73 71 6c  TH+1];.  if( sql
a2cb0 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
a2cc0 6f 72 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ory ){.    sqlit
a2cd0 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f  e3_snprintf(MAX_
a2ce0 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61  PATH-30, zTempPa
a2cf0 74 68 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  th, "%s", sqlite
a2d00 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
a2d10 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69  );.  }else if( i
a2d20 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 63 68 61  sNT() ){.    cha
a2d30 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20 20 20 20 57  r *zMulti;.    W
a2d40 43 48 41 52 20 7a 57 69 64 65 50 61 74 68 5b 4d  CHAR zWidePath[M
a2d50 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65  AX_PATH];.    Ge
a2d60 74 54 65 6d 70 50 61 74 68 57 28 4d 41 58 5f 50  tTempPathW(MAX_P
a2d70 41 54 48 2d 33 30 2c 20 7a 57 69 64 65 50 61 74  ATH-30, zWidePat
a2d80 68 29 3b 0a 20 20 20 20 7a 4d 75 6c 74 69 20 3d  h);.    zMulti =
a2d90 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a   unicodeToUtf8(z
a2da0 57 69 64 65 50 61 74 68 29 3b 0a 20 20 20 20 69  WidePath);.    i
a2db0 66 28 20 7a 4d 75 6c 74 69 20 29 7b 0a 20 20 20  f( zMulti ){.   
a2dc0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
a2dd0 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c  ntf(MAX_PATH-30,
a2de0 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22   zTempPath, "%s"
a2df0 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 20  , zMulti);.     
a2e00 20 66 72 65 65 28 7a 4d 75 6c 74 69 29 3b 0a 20   free(zMulti);. 
a2e10 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a2e20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
a2e30 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  MEM;.    }.  }el
a2e40 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 55  se{.    char *zU
a2e50 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d  tf8;.    char zM
a2e60 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41 54 48  bcsPath[MAX_PATH
a2e70 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61  ];.    GetTempPa
a2e80 74 68 41 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c  thA(MAX_PATH-30,
a2e90 20 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20   zMbcsPath);.   
a2ea0 20 7a 55 74 66 38 20 3d 20 6d 62 63 73 54 6f 55   zUtf8 = mbcsToU
a2eb0 74 66 38 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a  tf8(zMbcsPath);.
a2ec0 20 20 20 20 69 66 28 20 7a 55 74 66 38 20 29 7b      if( zUtf8 ){
a2ed0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
a2ee0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48  nprintf(MAX_PATH
a2ef0 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20  -30, zTempPath, 
a2f00 22 25 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20  "%s", zUtf8);.  
a2f10 20 20 20 20 66 72 65 65 28 7a 55 74 66 38 29 3b      free(zUtf8);
a2f20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
a2f30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a2f40 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
a2f50 0a 20 20 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28  .  for(i=strlen(
a2f60 7a 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30 20  zTempPath); i>0 
a2f70 26 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31  && zTempPath[i-1
a2f80 5d 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a  ]=='\\'; i--){}.
a2f90 20 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d    zTempPath[i] =
a2fa0 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e   0;.  sqlite3_sn
a2fb0 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50  printf(pVfs->mxP
a2fc0 61 74 68 6e 61 6d 65 2d 33 30 2c 20 7a 42 75 66  athname-30, zBuf
a2fd0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
a2fe0 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54       "%s\\"SQLIT
a2ff0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
a3000 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a  IX, zTempPath);.
a3010 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28 7a 42 75    j = strlen(zBu
a3020 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 61 6e  f);.  sqlite3Ran
a3030 64 6f 6d 6e 65 73 73 28 32 30 2c 20 26 7a 42 75  domness(20, &zBu
a3040 66 5b 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30  f[j]);.  for(i=0
a3050 3b 20 69 3c 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b  ; i<20; i++, j++
a3060 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d  ){.    zBuf[j] =
a3070 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28   (char)zChars[ (
a3080 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a  (unsigned char)z
a3090 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28  Buf[j])%(sizeof(
a30a0 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20  zChars)-1) ];.  
a30b0 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b  }.  zBuf[j] = 0;
a30c0 0a 20 20 4f 53 54 52 41 43 45 32 28 22 54 45 4d  .  OSTRACE2("TEM
a30d0 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e  P FILENAME: %s\n
a30e0 22 2c 20 7a 42 75 66 29 3b 0a 20 20 72 65 74 75  ", zBuf);.  retu
a30f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d  rn SQLITE_OK; .}
a3100 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72  ../*.** Turn a r
a3110 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65  elative pathname
a3120 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74   into a full pat
a3130 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68  hname.  Write th
a3140 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61  e full.** pathna
a3150 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20  me into zOut[]. 
a3160 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65 20   zOut[] will be 
a3170 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d  at least pVfs->m
a3180 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74  xPathname.** byt
a3190 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73  es in size..*/.s
a31a0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 75 6c  tatic int winFul
a31b0 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c  lPathname(.  sql
a31c0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
a31d0 20 20 20 20 20 20 20 20 20 0a 20 20 63 6f 6e 73           .  cons
a31e0 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
a31f0 65 2c 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c  e,.  char *zFull
a3200 0a 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  .){..#if defined
a3210 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 63  (__CYGWIN__).  c
a3220 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75  ygwin_conv_to_fu
a3230 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52  ll_win32_path(zR
a3240 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b  elative, zFull);
a3250 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
a3260 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  _OK;.#endif..#if
a3270 20 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 57   OS_WINCE.  /* W
a3280 69 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e 63  inCE has no conc
a3290 65 70 74 20 6f 66 20 61 20 72 65 6c 61 74 69 76  ept of a relativ
a32a0 65 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20 73  e pathname, or s
a32b0 6f 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f 0a  o I am told. */.
a32c0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
a32d0 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  tf(pVfs->mxPathn
a32e0 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22  ame, zFull, "%s"
a32f0 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 23 65  , zRelative);.#e
a3300 6e 64 69 66 0a 0a 23 69 66 20 21 4f 53 5f 57 49  ndif..#if !OS_WI
a3310 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 64 28  NCE && !defined(
a3320 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e  __CYGWIN__).  in
a3330 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 20  t nByte;.  void 
a3340 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63  *zConverted;.  c
a3350 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 7a 43 6f  har *zOut;.  zCo
a3360 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72  nverted = conver
a3370 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52  tUtf8Filename(zR
a3380 65 6c 61 74 69 76 65 29 3b 0a 20 20 69 66 28 20  elative);.  if( 
a3390 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 43  isNT() ){.    WC
a33a0 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20  HAR *zTemp;.    
a33b0 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c 50  nByte = GetFullP
a33c0 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a  athNameW((WCHAR*
a33d0 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20  )zConverted, 0, 
a33e0 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a  0, 0) + 3;.    z
a33f0 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  Temp = malloc( n
a3400 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d  Byte*sizeof(zTem
a3410 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28  p[0]) );.    if(
a3420 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20   zTemp==0 ){.   
a3430 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
a3440 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ed);.      retur
a3450 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
a3460 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c      }.    GetFul
a3470 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41  lPathNameW((WCHA
a3480 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e  R*)zConverted, n
a3490 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b  Byte, zTemp, 0);
a34a0 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65  .    free(zConve
a34b0 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20  rted);.    zOut 
a34c0 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28  = unicodeToUtf8(
a34d0 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65  zTemp);.    free
a34e0 28 7a 54 65 6d 70 29 3b 0a 20 20 7d 65 6c 73 65  (zTemp);.  }else
a34f0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d  {.    char *zTem
a3500 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47  p;.    nByte = G
a3510 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28  etFullPathNameA(
a3520 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
a3530 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b  d, 0, 0, 0) + 3;
a3540 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c  .    zTemp = mal
a3550 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f  loc( nByte*sizeo
a3560 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20  f(zTemp[0]) );. 
a3570 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20     if( zTemp==0 
a3580 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43  ){.      free(zC
a3590 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20  onverted);.     
a35a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
a35b0 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
a35c0 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41  GetFullPathNameA
a35d0 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74  ((char*)zConvert
a35e0 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70  ed, nByte, zTemp
a35f0 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a  , 0);.    free(z
a3600 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
a3610 7a 4f 75 74 20 3d 20 6d 62 63 73 54 6f 55 74 66  zOut = mbcsToUtf
a3620 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72  8(zTemp);.    fr
a3630 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 7d 0a 20  ee(zTemp);.  }. 
a3640 20 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20 20 20   if( zOut ){.   
a3650 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
a3660 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  f(pVfs->mxPathna
a3670 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c  me, zFull, "%s",
a3680 20 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72 65 65   zOut);.    free
a3690 28 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65 74 75  (zOut);.    retu
a36a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
a36b0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
a36c0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
a36d0 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 23 69    }.#endif.}..#i
a36e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
a36f0 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
a3700 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65  ./*.** Interface
a3710 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20  s for opening a 
a3720 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
a3730 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f  finding entry po
a3740 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74  ints.** within t
a3750 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
a3760 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74  y, and closing t
a3770 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
a3780 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  y..*/./*.** Inte
a3790 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
a37a0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
a37b0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
a37c0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
a37d0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
a37e0 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
a37f0 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
a3800 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  ibrary..*/.stati
a3810 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65  c void *winDlOpe
a3820 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  n(sqlite3_vfs *p
a3830 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Vfs, const char 
a3840 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48  *zFilename){.  H
a3850 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20  ANDLE h;.  void 
a3860 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f  *zConverted = co
a3870 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d  nvertUtf8Filenam
a3880 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
a3890 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d  if( zConverted==
a38a0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
a38b0 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e  0;.  }.  if( isN
a38c0 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 4c  T() ){.    h = L
a38d0 6f 61 64 4c 69 62 72 61 72 79 57 28 28 57 43 48  oadLibraryW((WCH
a38e0 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b  AR*)zConverted);
a38f0 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 20 4f 53  .  }else{.#if OS
a3900 5f 57 49 4e 43 45 0a 20 20 20 20 72 65 74 75 72  _WINCE.    retur
a3910 6e 20 30 3b 0a 23 65 6c 73 65 0a 20 20 20 20 68  n 0;.#else.    h
a3920 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 41 28   = LoadLibraryA(
a3930 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
a3940 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20  d);.#endif.  }. 
a3950 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64   free(zConverted
a3960 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69  );.  return (voi
a3970 64 2a 29 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76  d*)h;.}.static v
a3980 6f 69 64 20 77 69 6e 44 6c 45 72 72 6f 72 28 73  oid winDlError(s
a3990 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
a39a0 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
a39b0 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 46 6f   *zBufOut){.  Fo
a39c0 72 6d 61 74 4d 65 73 73 61 67 65 28 0a 20 20 20  rmatMessage(.   
a39d0 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f   FORMAT_MESSAGE_
a39e0 46 52 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 20  FROM_SYSTEM,.   
a39f0 20 4e 55 4c 4c 2c 0a 20 20 20 20 47 65 74 4c 61   NULL,.    GetLa
a3a00 73 74 45 72 72 6f 72 28 29 2c 0a 20 20 20 20 30  stError(),.    0
a3a10 2c 0a 20 20 20 20 7a 42 75 66 4f 75 74 2c 0a 20  ,.    zBufOut,. 
a3a20 20 20 20 6e 42 75 66 2d 31 2c 0a 20 20 20 20 30     nBuf-1,.    0
a3a30 0a 20 20 29 3b 0a 7d 0a 76 6f 69 64 20 2a 77 69  .  );.}.void *wi
a3a40 6e 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76  nDlSym(sqlite3_v
a3a50 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a  fs *pVfs, void *
a3a60 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63  pHandle, const c
a3a70 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 23  har *zSymbol){.#
a3a80 69 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a  if OS_WINCE.  /*
a3a90 20 54 68 65 20 47 65 74 50 72 6f 63 41 64 64 72   The GetProcAddr
a3aa0 65 73 73 41 28 29 20 72 6f 75 74 69 6e 65 20 69  essA() routine i
a3ab0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
a3ac0 20 6f 6e 20 77 69 6e 63 65 2e 20 2a 2f 0a 20 20   on wince. */.  
a3ad0 72 65 74 75 72 6e 20 47 65 74 50 72 6f 63 41 64  return GetProcAd
a3ae0 64 72 65 73 73 41 28 28 48 41 4e 44 4c 45 29 70  dressA((HANDLE)p
a3af0 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29  Handle, zSymbol)
a3b00 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 41 6c 6c  ;.#else.  /* All
a3b10 20 6f 74 68 65 72 20 77 69 6e 64 6f 77 73 20 70   other windows p
a3b20 6c 61 74 66 6f 72 6d 73 20 65 78 70 65 63 74 20  latforms expect 
a3b30 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 28 29  GetProcAddress()
a3b40 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a 20 61 6e   to take.  ** an
a3b50 20 41 6e 73 69 20 73 74 72 69 6e 67 20 72 65 67   Ansi string reg
a3b60 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 5f  ardless of the _
a3b70 55 4e 49 43 4f 44 45 20 73 65 74 74 69 6e 67 20  UNICODE setting 
a3b80 2a 2f 0a 20 20 72 65 74 75 72 6e 20 47 65 74 50  */.  return GetP
a3b90 72 6f 63 41 64 64 72 65 73 73 28 28 48 41 4e 44  rocAddress((HAND
a3ba0 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d  LE)pHandle, zSym
a3bb0 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 76  bol);.#endif.}.v
a3bc0 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65 28 73  oid winDlClose(s
a3bd0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
a3be0 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29  , void *pHandle)
a3bf0 7b 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79 28  {.  FreeLibrary(
a3c00 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 29  (HANDLE)pHandle)
a3c10 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20  ;.}.#else /* if 
a3c20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
a3c30 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65  _EXTENSION is de
a3c40 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66  fined: */.  #def
a3c50 69 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 30  ine winDlOpen  0
a3c60 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c  .  #define winDl
a3c70 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e  Error 0.  #defin
a3c80 65 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a 20  e winDlSym   0. 
a3c90 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 6c   #define winDlCl
a3ca0 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f  ose 0.#endif.../
a3cb0 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f  *.** Write up to
a3cc0 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72   nBuf bytes of r
a3cd0 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a  andomness into z
a3ce0 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  Buf..*/.static i
a3cf0 6e 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73  nt winRandomness
a3d00 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
a3d10 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
a3d20 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e 74  ar *zBuf){.  int
a3d30 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 69   n = 0;.  if( si
a3d40 7a 65 6f 66 28 53 59 53 54 45 4d 54 49 4d 45 29  zeof(SYSTEMTIME)
a3d50 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20  <=nBuf-n ){.    
a3d60 53 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20  SYSTEMTIME x;.  
a3d70 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28    GetSystemTime(
a3d80 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  &x);.    memcpy(
a3d90 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69  &zBuf[n], &x, si
a3da0 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20  zeof(x));.    n 
a3db0 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20  += sizeof(x);.  
a3dc0 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44  }.  if( sizeof(D
a3dd0 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b  WORD)<=nBuf-n ){
a3de0 0a 20 20 20 20 44 57 4f 52 44 20 70 69 64 20 3d  .    DWORD pid =
a3df0 20 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65   GetCurrentProce
a3e00 73 73 49 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63  ssId();.    memc
a3e10 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69  py(&zBuf[n], &pi
a3e20 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b  d, sizeof(pid));
a3e30 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66  .    n += sizeof
a3e40 28 70 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28  (pid);.  }.  if(
a3e50 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d   sizeof(DWORD)<=
a3e60 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57  nBuf-n ){.    DW
a3e70 4f 52 44 20 63 6e 74 20 3d 20 47 65 74 54 69 63  ORD cnt = GetTic
a3e80 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65  kCount();.    me
a3e90 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
a3ea0 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29  cnt, sizeof(cnt)
a3eb0 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
a3ec0 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69  of(cnt);.  }.  i
a3ed0 66 28 20 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f  f( sizeof(LARGE_
a3ee0 49 4e 54 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e  INTEGER)<=nBuf-n
a3ef0 20 29 7b 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e   ){.    LARGE_IN
a3f00 54 45 47 45 52 20 69 3b 0a 20 20 20 20 51 75 65  TEGER i;.    Que
a3f10 72 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75  ryPerformanceCou
a3f20 6e 74 65 72 28 26 69 29 3b 0a 20 20 20 20 6d 65  nter(&i);.    me
a3f30 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
a3f40 69 2c 20 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20  i, sizeof(i));. 
a3f50 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69     n += sizeof(i
a3f60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
a3f70 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65  n;.}.../*.** Sle
a3f80 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20  ep for a little 
a3f90 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74  while.  Return t
a3fa0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  he amount of tim
a3fb0 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 74  e slept..*/.stat
a3fc0 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 28  ic int winSleep(
a3fd0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
a3fe0 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 29  s, int microsec)
a3ff0 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72 6f  {.  Sleep((micro
a4000 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a  sec+999)/1000);.
a4010 20 20 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f    return ((micro
a4020 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31  sec+999)/1000)*1
a4030 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  000;.}../*.** Th
a4040 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
a4050 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20  able, if set to 
a4060 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
a4070 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65  , becomes the re
a4080 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64  sult.** returned
a4090 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43   from sqlite3OsC
a40a0 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54  urrentTime().  T
a40b0 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
a40c0 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64  testing..*/.#ifd
a40d0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
a40e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
a40f0 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
a4100 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  me = 0;.#endif..
a4110 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63  /*.** Find the c
a4120 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20  urrent time (in 
a4130 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69  Universal Coordi
a4140 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72  nated Time).  Wr
a4150 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65  ite the.** curre
a4160 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65  nt time and date
a4170 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79   as a Julian Day
a4180 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72   number into *pr
a4190 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72  Now and.** retur
a41a0 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69  n 0.  Return 1 i
a41b0 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64  f the time and d
a41c0 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  ate cannot be fo
a41d0 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43  und..*/.int winC
a41e0 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
a41f0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f  e3_vfs *pVfs, do
a4200 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20  uble *prNow){.  
a4210 46 49 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f  FILETIME ft;.  /
a4220 2a 20 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63  * FILETIME struc
a4230 74 75 72 65 20 69 73 20 61 20 36 34 2d 62 69 74  ture is a 64-bit
a4240 20 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74   value represent
a4250 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ing the number o
a4260 66 20 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f  f .     100-nano
a4270 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73  second intervals
a4280 20 73 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31   since January 1
a4290 2c 20 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30  , 1601 (= JD 230
a42a0 35 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20  5813.5). .  */. 
a42b0 20 64 6f 75 62 6c 65 20 6e 6f 77 3b 0a 23 69 66   double now;.#if
a42c0 20 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 53 54   OS_WINCE.  SYST
a42d0 45 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20 47  EMTIME time;.  G
a42e0 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74 69  etSystemTime(&ti
a42f0 6d 65 29 3b 0a 20 20 53 79 73 74 65 6d 54 69 6d  me);.  SystemTim
a4300 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d  eToFileTime(&tim
a4310 65 2c 26 66 74 29 3b 0a 23 65 6c 73 65 0a 20 20  e,&ft);.#else.  
a4320 47 65 74 53 79 73 74 65 6d 54 69 6d 65 41 73 46  GetSystemTimeAsF
a4330 69 6c 65 54 69 6d 65 28 20 26 66 74 20 29 3b 0a  ileTime( &ft );.
a4340 23 65 6e 64 69 66 0a 20 20 6e 6f 77 20 3d 20 28  #endif.  now = (
a4350 28 64 6f 75 62 6c 65 29 66 74 2e 64 77 48 69 67  (double)ft.dwHig
a4360 68 44 61 74 65 54 69 6d 65 29 20 2a 20 34 32 39  hDateTime) * 429
a4370 34 39 36 37 32 39 36 2e 30 3b 20 0a 20 20 2a 70  4967296.0; .  *p
a4380 72 4e 6f 77 20 3d 20 28 6e 6f 77 20 2b 20 66 74  rNow = (now + ft
a4390 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 29 2f  .dwLowDateTime)/
a43a0 38 36 34 30 30 30 30 30 30 30 30 30 2e 30 20 2b  864000000000.0 +
a43b0 20 32 33 30 35 38 31 33 2e 35 3b 0a 23 69 66 64   2305813.5;.#ifd
a43c0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
a43d0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72   if( sqlite3_cur
a43e0 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20  rent_time ){.   
a43f0 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65   *prNow = sqlite
a4400 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38  3_current_time/8
a4410 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37  6400.0 + 2440587
a4420 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  .5;.  }.#endif. 
a4430 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f   return 0;.}.../
a4440 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
a4450 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  inter to the sql
a4460 69 74 65 33 44 65 66 61 75 6c 74 56 66 73 20 73  ite3DefaultVfs s
a4470 74 72 75 63 74 75 72 65 2e 20 20 20 57 65 20 75  tructure.   We u
a4480 73 65 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f 6e  se.** a function
a4490 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 69 76   rather than giv
a44a0 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  e the structure 
a44b0 67 6c 6f 62 61 6c 20 73 63 6f 70 65 20 62 65 63  global scope bec
a44c0 61 75 73 65 0a 2a 2a 20 73 6f 6d 65 20 63 6f 6d  ause.** some com
a44d0 70 69 6c 65 72 73 20 28 4d 53 56 43 29 20 64 6f  pilers (MSVC) do
a44e0 20 6e 6f 74 20 61 6c 6c 6f 77 20 66 6f 72 77 61   not allow forwa
a44f0 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20  rd declarations 
a4500 6f 66 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65  of.** initialize
a4510 64 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  d structures..*/
a4520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a4530 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c  sqlite3_vfs *sql
a4540 69 74 65 33 4f 73 44 65 66 61 75 6c 74 56 66 73  ite3OsDefaultVfs
a4550 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
a4560 20 73 71 6c 69 74 65 33 5f 76 66 73 20 77 69 6e   sqlite3_vfs win
a4570 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20  Vfs = {.    1,  
a4580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a4590 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20  * iVersion */.  
a45a0 20 20 73 69 7a 65 6f 66 28 77 69 6e 46 69 6c 65    sizeof(winFile
a45b0 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65  ),   /* szOsFile
a45c0 20 2a 2f 0a 20 20 20 20 4d 41 58 5f 50 41 54 48   */.    MAX_PATH
a45d0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78  ,          /* mx
a45e0 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  Pathname */.    
a45f0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
a4600 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20     /* pNext */. 
a4610 20 20 20 22 77 69 6e 33 32 22 2c 20 20 20 20 20     "win32",     
a4620 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a        /* zName *
a4630 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
a4640 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70           /* pApp
a4650 44 61 74 61 20 2a 2f 0a 20 20 0a 20 20 20 20 77  Data */.  .    w
a4660 69 6e 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20  inOpen,         
a4670 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20    /* xOpen */.  
a4680 20 20 77 69 6e 44 65 6c 65 74 65 2c 20 20 20 20    winDelete,    
a4690 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20       /* xDelete 
a46a0 2a 2f 0a 20 20 20 20 77 69 6e 41 63 63 65 73 73  */.    winAccess
a46b0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63  ,         /* xAc
a46c0 63 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 47  cess */.    winG
a46d0 65 74 54 65 6d 70 4e 61 6d 65 2c 20 20 20 20 2f  etTempName,    /
a46e0 2a 20 78 47 65 74 54 65 6d 70 4e 61 6d 65 20 2a  * xGetTempName *
a46f0 2f 0a 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74  /.    winFullPat
a4700 68 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c  hname,   /* xFul
a4710 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20  lPathname */.   
a4720 20 77 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20   winDlOpen,     
a4730 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a      /* xDlOpen *
a4740 2f 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72  /.    winDlError
a4750 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45  ,        /* xDlE
a4760 72 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44  rror */.    winD
a4770 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f  lSym,          /
a4780 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20  * xDlSym */.    
a4790 77 69 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20  winDlClose,     
a47a0 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a     /* xDlClose *
a47b0 2f 0a 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e  /.    winRandomn
a47c0 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e  ess,     /* xRan
a47d0 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77  domness */.    w
a47e0 69 6e 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20  inSleep,        
a47f0 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20    /* xSleep */. 
a4800 20 20 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d     winCurrentTim
a4810 65 20 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e  e     /* xCurren
a4820 74 54 69 6d 65 20 2a 2f 0a 20 20 7d 3b 0a 20 20  tTime */.  };.  
a4830 0a 20 20 72 65 74 75 72 6e 20 26 77 69 6e 56 66  .  return &winVf
a4840 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  s;.}..#endif /* 
a4850 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  OS_WIN */../****
a4860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
a4870 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a  f os_win.c *****
a4880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a48a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
a48b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
a48c0 20 66 69 6c 65 20 70 61 67 65 72 2e 63 20 2a 2a   file pager.c **
a48d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a48e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a48f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
a4900 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
a4910 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
a4920 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
a4930 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
a4940 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
a4950 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
a4960 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
a4970 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
a4980 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
a4990 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
a49a0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
a49b0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
a49c0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
a49d0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
a49e0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
a49f0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
a4a00 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
a4a10 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
a4a20 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
a4a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4a70 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
a4a80 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
a4a90 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68  of the page cach
a4aa0 65 20 73 75 62 73 79 73 74 65 6d 20 6f 72 20 22  e subsystem or "
a4ab0 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54  pager"..** .** T
a4ac0 68 65 20 70 61 67 65 72 20 69 73 20 75 73 65 64  he pager is used
a4ad0 20 74 6f 20 61 63 63 65 73 73 20 61 20 64 61 74   to access a dat
a4ae0 61 62 61 73 65 20 64 69 73 6b 20 66 69 6c 65 2e  abase disk file.
a4af0 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a    It implements.
a4b00 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74  ** atomic commit
a4b10 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68   and rollback th
a4b20 72 6f 75 67 68 20 74 68 65 20 75 73 65 20 6f 66  rough the use of
a4b30 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20   a journal file 
a4b40 74 68 61 74 0a 2a 2a 20 69 73 20 73 65 70 61 72  that.** is separ
a4b50 61 74 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74  ate from the dat
a4b60 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65  abase file.  The
a4b70 20 70 61 67 65 72 20 61 6c 73 6f 20 69 6d 70 6c   pager also impl
a4b80 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a 20 6c  ements file.** l
a4b90 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 76 65 6e  ocking to preven
a4ba0 74 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20  t two processes 
a4bb0 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 74 68 65  from writing the
a4bc0 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a   same database.*
a4bd0 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e 65  * file simultane
a4be0 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 70 72  ously, or one pr
a4bf0 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 61 64 69  ocess from readi
a4c00 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
a4c10 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 65 72  while.** another
a4c20 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a   is writing..**.
a4c30 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67  ** @(#) $Id: pag
a4c40 65 72 2e 63 2c 76 20 31 2e 33 38 38 20 32 30 30  er.c,v 1.388 200
a4c50 37 2f 30 39 2f 31 30 20 30 36 3a 31 32 3a 30 34  7/09/10 06:12:04
a4c60 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
a4c70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51   $.*/.#ifndef SQ
a4c80 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
a4c90 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
a4ca0 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69  or troubleshooti
a4cb0 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75  ng.  Normally tu
a4cc0 72 6e 65 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20  rned off.*/.#if 
a4cd0 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  0.#define sqlite
a4ce0 33 44 65 62 75 67 50 72 69 6e 74 66 20 70 72 69  3DebugPrintf pri
a4cf0 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41 47 45  ntf.#define PAGE
a4d00 52 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20  RTRACE1(X)      
a4d10 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
a4d20 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 50  ntf(X).#define P
a4d30 41 47 45 52 54 52 41 43 45 32 28 58 2c 59 29 20  AGERTRACE2(X,Y) 
a4d40 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
a4d50 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66  Printf(X,Y).#def
a4d60 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 33 28  ine PAGERTRACE3(
a4d70 58 2c 59 2c 5a 29 20 20 20 73 71 6c 69 74 65 33  X,Y,Z)   sqlite3
a4d80 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
a4d90 5a 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  Z).#define PAGER
a4da0 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 57 29 20  TRACE4(X,Y,Z,W) 
a4db0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
a4dc0 74 66 28 58 2c 59 2c 5a 2c 57 29 0a 23 64 65 66  tf(X,Y,Z,W).#def
a4dd0 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 35 28  ine PAGERTRACE5(
a4de0 58 2c 59 2c 5a 2c 57 2c 56 29 20 73 71 6c 69 74  X,Y,Z,W,V) sqlit
a4df0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
a4e00 59 2c 5a 2c 57 2c 56 29 0a 23 65 6c 73 65 0a 23  Y,Z,W,V).#else.#
a4e10 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43  define PAGERTRAC
a4e20 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 50 41  E1(X).#define PA
a4e30 47 45 52 54 52 41 43 45 32 28 58 2c 59 29 0a 23  GERTRACE2(X,Y).#
a4e40 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43  define PAGERTRAC
a4e50 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e  E3(X,Y,Z).#defin
a4e60 65 20 50 41 47 45 52 54 52 41 43 45 34 28 58 2c  e PAGERTRACE4(X,
a4e70 59 2c 5a 2c 57 29 0a 23 64 65 66 69 6e 65 20 50  Y,Z,W).#define P
a4e80 41 47 45 52 54 52 41 43 45 35 28 58 2c 59 2c 5a  AGERTRACE5(X,Y,Z
a4e90 2c 57 2c 56 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ,W,V).#endif../*
a4ea0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
a4eb0 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65  g two macros are
a4ec0 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 65   used within the
a4ed0 20 50 41 47 45 52 54 52 41 43 45 58 28 29 20 6d   PAGERTRACEX() m
a4ee0 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74  acros above.** t
a4ef0 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c 65  o print out file
a4f00 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a  -descriptors. .*
a4f10 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20 74  *.** PAGERID() t
a4f20 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  akes a pointer t
a4f30 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 63 74  o a Pager struct
a4f40 20 61 73 20 69 74 27 73 20 61 72 67 75 6d 65 6e   as it's argumen
a4f50 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69  t. The.** associ
a4f60 61 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69  ated file-descri
a4f70 70 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  ptor is returned
a4f80 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29  . FILEHANDLEID()
a4f90 20 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65   takes an sqlite
a4fa0 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74  3_file.** struct
a4fb0 20 61 73 20 69 74 27 73 20 61 72 67 75 6d 65 6e   as it's argumen
a4fc0 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  t..*/.#define PA
a4fd0 47 45 52 49 44 28 70 29 20 28 28 69 6e 74 29 28  GERID(p) ((int)(
a4fe0 70 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20  p->fd)).#define 
a4ff0 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29  FILEHANDLEID(fd)
a5000 20 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a   ((int)fd)../*.*
a5010 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65  * The page cache
a5020 20 61 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61   as a whole is a
a5030 6c 77 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20  lways in one of 
a5040 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
a5050 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
a5060 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20   PAGER_UNLOCK   
a5070 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61       The page ca
a5080 63 68 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65  che is not curre
a5090 6e 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20  ntly reading or 
a50a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
a50b0 20 20 20 20 20 20 20 20 20 20 77 72 69 74 69 6e            writin
a50c0 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  g the database f
a50d0 69 6c 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e  ile.  There is n
a50e0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  o.**            
a50f0 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 20             data 
a5100 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20  held in memory. 
a5110 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6e 69   This is the ini
a5120 74 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  tial.**         
a5130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
a5140 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47  ate..**.**   PAG
a5150 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 20  ER_SHARED       
a5160 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
a5170 69 73 20 72 65 61 64 69 6e 67 20 74 68 65 20 64  is reading the d
a5180 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20  atabase..**     
a5190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a51a0 20 20 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74    Writing is not
a51b0 20 70 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65   permitted.  The
a51c0 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20  re can be.**    
a51d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a51e0 20 20 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64     multiple read
a51f0 65 72 73 20 61 63 63 65 73 73 69 6e 67 20 74 68  ers accessing th
a5200 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a  e same database.
a5210 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
a5220 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 61 74           file at
a5230 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a   the same time..
a5240 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45  **.**   PAGER_RE
a5250 53 45 52 56 45 44 20 20 20 20 20 20 54 68 69 73  SERVED      This
a5260 20 70 72 6f 63 65 73 73 20 68 61 73 20 72 65 73   process has res
a5270 65 72 76 65 64 20 74 68 65 20 64 61 74 61 62 61  erved the databa
a5280 73 65 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a  se for writing.*
a5290 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
a52a0 20 20 20 20 20 20 20 20 62 75 74 20 68 61 73 20          but has 
a52b0 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79  not yet made any
a52c0 20 63 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20   changes.  Only 
a52d0 6f 6e 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20  one process.**  
a52e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a52f0 20 20 20 20 20 61 74 20 61 20 74 69 6d 65 20 63       at a time c
a5300 61 6e 20 72 65 73 65 72 76 65 20 74 68 65 20 64  an reserve the d
a5310 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72  atabase.  The or
a5320 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  iginal.**       
a5330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5340 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
a5350 73 20 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66  s not been modif
a5360 69 65 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20  ied so other.** 
a5370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5380 20 20 20 20 20 20 70 72 6f 63 65 73 73 65 73 20        processes 
a5390 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61  may still be rea
a53a0 64 69 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b  ding the on-disk
a53b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
a53c0 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61            databa
a53d0 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20  se file..**.**  
a53e0 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
a53f0 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61       The page ca
a5400 63 68 65 20 69 73 20 77 72 69 74 69 6e 67 20 74  che is writing t
a5410 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20  he database..** 
a5420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5430 20 20 20 20 20 20 41 63 63 65 73 73 20 69 73 20        Access is 
a5440 65 78 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f  exclusive.  No o
a5450 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f  ther processes o
a5460 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
a5470 20 20 20 20 20 20 20 20 20 20 20 74 68 72 65 61             threa
a5480 64 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e  ds can be readin
a5490 67 20 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69  g or writing whi
a54a0 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20  le one.**       
a54b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a54c0 70 72 6f 63 65 73 73 20 69 73 20 77 72 69 74 69  process is writi
a54d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  ng..**.**   PAGE
a54e0 52 5f 53 59 4e 43 45 44 20 20 20 20 20 20 20 20  R_SYNCED        
a54f0 54 68 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20  The pager moves 
a5500 74 6f 20 74 68 69 73 20 73 74 61 74 65 20 66 72  to this state fr
a5510 6f 6d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49  om PAGER_EXCLUSI
a5520 56 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  VE.**           
a5530 20 20 20 20 20 20 20 20 20 20 20 20 61 66 74 65              afte
a5540 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  r all dirty page
a5550 73 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74  s have been writ
a5560 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20  ten to the.**   
a5570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5580 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c      database fil
a5590 65 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68  e and the file h
a55a0 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74  as been synced t
a55b0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  o.**            
a55c0 20 20 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e             disk.
a55d0 20 41 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e   All that remain
a55e0 73 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65  s to do is to re
a55f0 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20  move or.**      
a5600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5610 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f   truncate the jo
a5620 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74  urnal file and t
a5630 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a  he transaction .
a5640 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
a5650 20 20 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65           will be
a5660 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a   committed..**.*
a5670 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65  * The page cache
a5680 20 63 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47   comes up in PAG
a5690 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20  ER_UNLOCK.  The 
a56a0 66 69 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20  first time a.** 
a56b0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
a56c0 29 20 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74  ) occurs, the st
a56d0 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  ate transitions 
a56e0 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e  to PAGER_SHARED.
a56f0 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61  .** After all pa
a5700 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65  ges have been re
a5710 6c 65 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c  leased using sql
a5720 69 74 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29  ite_page_unref()
a5730 2c 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74  ,.** the state t
a5740 72 61 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20  ransitions back 
a5750 74 6f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e  to PAGER_UNLOCK.
a5760 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65    The first time
a5770 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33  .** that sqlite3
a5780 50 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20  PagerWrite() is 
a5790 63 61 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74  called, the stat
a57a0 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f  e transitions to
a57b0 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56  .** PAGER_RESERV
a57c0 45 44 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20  ED.  (Note that 
a57d0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
a57e0 65 28 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a  e() can only be.
a57f0 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20  ** called on an 
a5800 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
a5810 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61   which means tha
a5820 74 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74  t the pager must
a5830 0a 2a 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f  .** be in PAGER_
a5840 53 48 41 52 45 44 20 62 65 66 6f 72 65 20 69 74  SHARED before it
a5850 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20   transitions to 
a5860 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29  PAGER_RESERVED.)
a5870 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56  .** PAGER_RESERV
a5880 45 44 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ED means that th
a5890 65 72 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72  ere is an open r
a58a0 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
a58b0 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69  .** The transiti
a58c0 6f 6e 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c  on to PAGER_EXCL
a58d0 55 53 49 56 45 20 6f 63 63 75 72 73 20 62 65 66  USIVE occurs bef
a58e0 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a  ore any changes.
a58f0 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74  ** are made to t
a5900 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
a5910 2c 20 74 68 6f 75 67 68 20 77 72 69 74 65 73 20  , though writes 
a5920 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a  to the rollback.
a5930 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72  ** journal occur
a5940 73 20 77 69 74 68 20 6a 75 73 74 20 50 41 47 45  s with just PAGE
a5950 52 5f 52 45 53 45 52 56 45 44 2e 20 20 41 66 74  R_RESERVED.  Aft
a5960 65 72 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67  er an sqlite3Pag
a5970 65 72 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20  erRollback().** 
a5980 6f 72 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  or sqlite3PagerC
a5990 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c  ommitPhaseTwo(),
a59a0 20 74 68 65 20 73 74 61 74 65 20 63 61 6e 20 67   the state can g
a59b0 6f 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f  o back to PAGER_
a59c0 53 48 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74  SHARED,.** or it
a59d0 20 63 61 6e 20 73 74 61 79 20 61 74 20 50 41 47   can stay at PAG
a59e0 45 52 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20  ER_EXCLUSIVE if 
a59f0 77 65 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73  we are in exclus
a5a00 69 76 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e  ive access mode.
a5a10 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45  .*/.#define PAGE
a5a20 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a  R_UNLOCK      0.
a5a30 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48  #define PAGER_SH
a5a40 41 52 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a  ARED      1   /*
a5a50 20 73 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f   same as SHARED_
a5a60 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20  LOCK */.#define 
a5a70 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20  PAGER_RESERVED  
a5a80 20 20 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73    2   /* same as
a5a90 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a   RESERVED_LOCK *
a5aa0 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
a5ab0 45 58 43 4c 55 53 49 56 45 20 20 20 34 20 20 20  EXCLUSIVE   4   
a5ac0 2f 2a 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55  /* same as EXCLU
a5ad0 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65  SIVE_LOCK */.#de
a5ae0 66 69 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45  fine PAGER_SYNCE
a5af0 44 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20  D      5../*.** 
a5b00 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55  If the SQLITE_BU
a5b10 53 59 5f 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  SY_RESERVED_LOCK
a5b20 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20 74 6f   macro is set to
a5b30 20 74 72 75 65 20 61 74 20 63 6f 6d 70 69 6c 65   true at compile
a5b40 2d 74 69 6d 65 2c 0a 2a 2a 20 74 68 65 6e 20 66  -time,.** then f
a5b50 61 69 6c 65 64 20 61 74 74 65 6d 70 74 73 20 74  ailed attempts t
a5b60 6f 20 67 65 74 20 61 20 72 65 73 65 72 76 65 64  o get a reserved
a5b70 20 6c 6f 63 6b 20 77 69 6c 6c 20 69 6e 76 6f 6b   lock will invok
a5b80 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62  e the busy callb
a5b90 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  ack..** This is 
a5ba0 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e 20  off by default. 
a5bb0 20 54 6f 20 73 65 65 20 77 68 79 2c 20 63 6f 6e   To see why, con
a5bc0 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77  sider the follow
a5bd0 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a 2a 2a  ing scenario:.**
a5be0 20 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 72   .** Suppose thr
a5bf0 65 61 64 20 41 20 61 6c 72 65 61 64 79 20 68 61  ead A already ha
a5c00 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  s a shared lock 
a5c10 61 6e 64 20 77 61 6e 74 73 20 61 20 72 65 73 65  and wants a rese
a5c20 72 76 65 64 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68  rved lock..** Th
a5c30 72 65 61 64 20 42 20 61 6c 72 65 61 64 79 20 68  read B already h
a5c40 61 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f  as a reserved lo
a5c50 63 6b 20 61 6e 64 20 77 61 6e 74 73 20 61 6e 20  ck and wants an 
a5c60 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20  exclusive lock. 
a5c70 20 49 66 0a 2a 2a 20 62 6f 74 68 20 74 68 72 65   If.** both thre
a5c80 61 64 73 20 61 72 65 20 75 73 69 6e 67 20 74 68  ads are using th
a5c90 65 69 72 20 62 75 73 79 20 63 61 6c 6c 62 61 63  eir busy callbac
a5ca0 6b 73 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20  ks, it might be 
a5cb0 61 20 6c 6f 6e 67 20 74 69 6d 65 0a 2a 2a 20 62  a long time.** b
a5cc0 65 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  e for one of the
a5cd0 20 74 68 72 65 61 64 73 20 67 69 76 65 20 75 70   threads give up
a5ce0 20 61 6e 64 20 61 6c 6c 6f 77 73 20 74 68 65 20   and allows the 
a5cf0 6f 74 68 65 72 20 74 6f 20 70 72 6f 63 65 65 64  other to proceed
a5d00 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20  ..** But if the 
a5d10 74 68 72 65 61 64 20 74 72 79 69 6e 67 20 74 6f  thread trying to
a5d20 20 67 65 74 20 74 68 65 20 72 65 73 65 72 76 65   get the reserve
a5d30 64 20 6c 6f 63 6b 20 67 69 76 65 73 20 75 70 20  d lock gives up 
a5d40 71 75 69 63 6b 6c 79 0a 2a 2a 20 28 69 66 20 69  quickly.** (if i
a5d50 74 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 73 20  t never invokes 
a5d60 69 74 73 20 62 75 73 79 20 63 61 6c 6c 62 61 63  its busy callbac
a5d70 6b 29 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 74  k) then the cont
a5d80 65 6e 74 69 6f 6e 20 77 69 6c 6c 20 62 65 0a 2a  ention will be.*
a5d90 2a 20 72 65 73 6f 6c 76 65 64 20 71 75 69 63 6b  * resolved quick
a5da0 6c 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ly..*/.#ifndef S
a5db0 51 4c 49 54 45 5f 42 55 53 59 5f 52 45 53 45 52  QLITE_BUSY_RESER
a5dc0 56 45 44 5f 4c 4f 43 4b 0a 23 20 64 65 66 69 6e  VED_LOCK.# defin
a5dd0 65 20 53 51 4c 49 54 45 5f 42 55 53 59 5f 52 45  e SQLITE_BUSY_RE
a5de0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 30 0a 23 65  SERVED_LOCK 0.#e
a5df0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
a5e00 20 6d 61 63 72 6f 20 72 6f 75 6e 64 73 20 76 61   macro rounds va
a5e10 6c 75 65 73 20 75 70 20 73 6f 20 74 68 61 74 20  lues up so that 
a5e20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  if the value is 
a5e30 61 6e 20 61 64 64 72 65 73 73 20 69 74 0a 2a 2a  an address it.**
a5e40 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
a5e50 6f 20 62 65 20 61 6e 20 61 64 64 72 65 73 73 20  o be an address 
a5e60 74 68 61 74 20 69 73 20 61 6c 69 67 6e 65 64 20  that is aligned 
a5e70 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62 6f 75  to an 8-byte bou
a5e80 6e 64 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ndary..*/.#defin
a5e90 65 20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d 45 4e  e FORCE_ALIGNMEN
a5ea0 54 28 58 29 20 20 20 28 28 28 58 29 2b 37 29 26  T(X)   (((X)+7)&
a5eb0 7e 37 29 0a 0a 74 79 70 65 64 65 66 20 73 74 72  ~7)..typedef str
a5ec0 75 63 74 20 50 67 48 64 72 20 50 67 48 64 72 3b  uct PgHdr PgHdr;
a5ed0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 70 61 67  ../*.** Each pag
a5ee0 65 72 20 73 74 6f 72 65 73 20 61 6c 6c 20 63 75  er stores all cu
a5ef0 72 72 65 6e 74 6c 79 20 75 6e 72 65 66 65 72 65  rrently unrefere
a5f00 6e 63 65 64 20 70 61 67 65 73 20 69 6e 20 61 20  nced pages in a 
a5f10 6c 69 73 74 20 73 6f 72 74 65 64 0a 2a 2a 20 69  list sorted.** i
a5f20 6e 20 6c 65 61 73 74 2d 72 65 63 65 6e 74 6c 79  n least-recently
a5f30 2d 75 73 65 64 20 28 4c 52 55 29 20 6f 72 64 65  -used (LRU) orde
a5f40 72 20 28 69 2e 65 2e 20 74 68 65 20 66 69 72 73  r (i.e. the firs
a5f50 74 20 69 74 65 6d 20 6f 6e 20 74 68 65 20 6c 69  t item on the li
a5f60 73 74 20 68 61 73 20 0a 2a 2a 20 6e 6f 74 20 62  st has .** not b
a5f70 65 65 6e 20 72 65 66 65 72 65 6e 63 65 64 20 69  een referenced i
a5f80 6e 20 61 20 6c 6f 6e 67 20 74 69 6d 65 2c 20 74  n a long time, t
a5f90 68 65 20 6c 61 73 74 20 69 74 65 6d 20 68 61 73  he last item has
a5fa0 20 62 65 65 6e 20 72 65 63 65 6e 74 6c 79 0a 2a   been recently.*
a5fb0 2a 20 75 73 65 64 29 2e 20 41 6e 20 69 6e 73 74  * used). An inst
a5fc0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
a5fd0 75 63 74 75 72 65 20 69 73 20 69 6e 63 6c 75 64  ucture is includ
a5fe0 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 65 61  ed as part of ea
a5ff0 63 68 0a 2a 2a 20 70 61 67 65 72 20 73 74 72 75  ch.** pager stru
a6000 63 74 75 72 65 20 66 6f 72 20 74 68 69 73 20 70  cture for this p
a6010 75 72 70 6f 73 65 20 28 76 61 72 69 61 62 6c 65  urpose (variable
a6020 20 50 61 67 65 72 2e 6c 72 75 29 2e 0a 2a 2a 0a   Pager.lru)..**.
a6030 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  ** Additionally,
a6040 20 69 66 20 6d 65 6d 6f 72 79 2d 6d 61 6e 61 67   if memory-manag
a6050 65 6d 65 6e 74 20 69 73 20 65 6e 61 62 6c 65 64  ement is enabled
a6060 2c 20 61 6c 6c 20 75 6e 72 65 66 65 72 65 6e 63  , all unreferenc
a6070 65 64 20 70 61 67 65 73 20 0a 2a 2a 20 61 72 65  ed pages .** are
a6080 20 73 74 6f 72 65 64 20 69 6e 20 61 20 67 6c 6f   stored in a glo
a6090 62 61 6c 20 4c 52 55 20 6c 69 73 74 20 28 67 6c  bal LRU list (gl
a60a0 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 73 71  obal variable sq
a60b0 6c 69 74 65 33 4c 72 75 50 61 67 65 4c 69 73 74  lite3LruPageList
a60c0 29 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 62 6f 74 68  )..**.** In both
a60d0 20 63 61 73 65 73 2c 20 74 68 65 20 50 61 67 65   cases, the Page
a60e0 72 4c 72 75 4c 69 73 74 2e 70 46 69 72 73 74 53  rLruList.pFirstS
a60f0 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 70  ynced variable p
a6100 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20  oints to.** the 
a6110 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 74 68  first page in th
a6120 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
a6130 6c 69 73 74 20 74 68 61 74 20 64 6f 65 73 20 6e  list that does n
a6140 6f 74 20 72 65 71 75 69 72 65 20 61 6e 0a 2a 2a  ot require an.**
a6150 20 66 73 79 6e 63 28 29 20 6f 70 65 72 61 74 69   fsync() operati
a6160 6f 6e 20 62 65 66 6f 72 65 20 69 74 27 73 20 6d  on before it's m
a6170 65 6d 6f 72 79 20 63 61 6e 20 62 65 20 72 65 63  emory can be rec
a6180 6c 61 69 6d 65 64 2e 20 49 66 20 6e 6f 20 73 75  laimed. If no su
a6190 63 68 0a 2a 2a 20 70 61 67 65 20 65 78 69 73 74  ch.** page exist
a61a0 73 2c 20 50 61 67 65 72 4c 72 75 4c 69 73 74 2e  s, PagerLruList.
a61b0 70 46 69 72 73 74 53 79 6e 63 65 64 20 69 73 20  pFirstSynced is 
a61c0 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  set to NULL..*/.
a61d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
a61e0 61 67 65 72 4c 72 75 4c 69 73 74 20 50 61 67 65  agerLruList Page
a61f0 72 4c 72 75 4c 69 73 74 3b 0a 73 74 72 75 63 74  rLruList;.struct
a6200 20 50 61 67 65 72 4c 72 75 4c 69 73 74 20 7b 0a   PagerLruList {.
a6210 20 20 50 67 48 64 72 20 2a 70 46 69 72 73 74 3b    PgHdr *pFirst;
a6220 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
a6230 74 20 70 61 67 65 20 69 6e 20 4c 52 55 20 6c 69  t page in LRU li
a6240 73 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70  st */.  PgHdr *p
a6250 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f  Last;          /
a6260 2a 20 4c 61 73 74 20 70 61 67 65 20 69 6e 20 4c  * Last page in L
a6270 52 55 20 6c 69 73 74 20 28 74 68 65 20 6d 6f 73  RU list (the mos
a6280 74 20 72 65 63 65 6e 74 6c 79 20 75 73 65 64 29  t recently used)
a6290 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 46 69   */.  PgHdr *pFi
a62a0 72 73 74 53 79 6e 63 65 64 3b 20 20 20 2f 2a 20  rstSynced;   /* 
a62b0 46 69 72 73 74 20 70 61 67 65 20 69 6e 20 6c 69  First page in li
a62c0 73 74 20 77 69 74 68 20 50 67 48 64 72 2e 6e 65  st with PgHdr.ne
a62d0 65 64 53 79 6e 63 3d 3d 30 20 2a 2f 0a 7d 3b 0a  edSync==0 */.};.
a62e0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
a62f0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63  wing structure c
a6300 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 65 78 74  ontains the next
a6310 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 70 6f   and previous po
a6320 69 6e 74 65 72 73 20 75 73 65 64 0a 2a 2a 20 74  inters used.** t
a6330 6f 20 6c 69 6e 6b 20 61 20 50 67 48 64 72 20 73  o link a PgHdr s
a6340 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20  tructure into a 
a6350 50 61 67 65 72 4c 72 75 4c 69 73 74 20 6c 69 6e  PagerLruList lin
a6360 6b 65 64 20 6c 69 73 74 2e 20 0a 2a 2f 0a 74 79  ked list. .*/.ty
a6370 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 67  pedef struct Pag
a6380 65 72 4c 72 75 4c 69 6e 6b 20 50 61 67 65 72 4c  erLruLink PagerL
a6390 72 75 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 50  ruLink;.struct P
a63a0 61 67 65 72 4c 72 75 4c 69 6e 6b 20 7b 0a 20 20  agerLruLink {.  
a63b0 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20  PgHdr *pNext;.  
a63c0 50 67 48 64 72 20 2a 70 50 72 65 76 3b 0a 7d 3b  PgHdr *pPrev;.};
a63d0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 2d  ../*.** Each in-
a63e0 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20  memory image of 
a63f0 61 20 70 61 67 65 20 62 65 67 69 6e 73 20 77 69  a page begins wi
a6400 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  th the following
a6410 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68 69 73   header..** This
a6420 20 68 65 61 64 65 72 20 69 73 20 6f 6e 6c 79 20   header is only 
a6430 76 69 73 69 62 6c 65 20 74 6f 20 74 68 69 73 20  visible to this 
a6440 70 61 67 65 72 20 6d 6f 64 75 6c 65 2e 20 20 54  pager module.  T
a6450 68 65 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64  he client.** cod
a6460 65 20 74 68 61 74 20 63 61 6c 6c 73 20 70 61 67  e that calls pag
a6470 65 72 20 73 65 65 73 20 6f 6e 6c 79 20 74 68 65  er sees only the
a6480 20 64 61 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f   data that follo
a6490 77 73 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a  ws the header..*
a64a0 2a 0a 2a 2a 20 43 6c 69 65 6e 74 20 63 6f 64 65  *.** Client code
a64b0 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c   should call sql
a64c0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
a64d0 20 6f 6e 20 61 20 70 61 67 65 20 70 72 69 6f 72   on a page prior
a64e0 20 74 6f 20 6d 61 6b 69 6e 67 0a 2a 2a 20 61 6e   to making.** an
a64f0 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20  y modifications 
a6500 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20 20 54  to that page.  T
a6510 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 73 71  he first time sq
a6520 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
a6530 29 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2c 20  ).** is called, 
a6540 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  the original pag
a6550 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 20 77  e contents are w
a6560 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
a6570 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72  rollback.** jour
a6580 6e 61 6c 20 61 6e 64 20 50 67 48 64 72 2e 69 6e  nal and PgHdr.in
a6590 4a 6f 75 72 6e 61 6c 20 61 6e 64 20 50 67 48 64  Journal and PgHd
a65a0 72 2e 6e 65 65 64 53 79 6e 63 20 61 72 65 20 73  r.needSync are s
a65b0 65 74 2e 20 20 4c 61 74 65 72 2c 20 6f 6e 63 65  et.  Later, once
a65c0 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  .** the journal 
a65d0 70 61 67 65 20 68 61 73 20 6d 61 64 65 20 69 74  page has made it
a65e0 20 6f 6e 74 6f 20 74 68 65 20 64 69 73 6b 20 73   onto the disk s
a65f0 75 72 66 61 63 65 2c 20 50 67 48 64 72 2e 6e 65  urface, PgHdr.ne
a6600 65 64 53 79 6e 63 0a 2a 2a 20 69 73 20 63 6c 65  edSync.** is cle
a6610 61 72 65 64 2e 20 20 54 68 65 20 6d 6f 64 69 66  ared.  The modif
a6620 69 65 64 20 70 61 67 65 20 63 61 6e 6e 6f 74 20  ied page cannot 
a6630 62 65 20 77 72 69 74 74 65 6e 20 62 61 63 6b 20  be written back 
a6640 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61  into the origina
a6650 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69  l.** database fi
a6660 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 6a 6f 75  le until the jou
a6670 72 6e 61 6c 20 70 61 67 65 73 20 68 61 73 20 62  rnal pages has b
a6680 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 69  een synced to di
a6690 73 6b 20 61 6e 64 20 74 68 65 0a 2a 2a 20 50 67  sk and the.** Pg
a66a0 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 68 61 73  Hdr.needSync has
a66b0 20 62 65 65 6e 20 63 6c 65 61 72 65 64 2e 0a 2a   been cleared..*
a66c0 2a 0a 2a 2a 20 54 68 65 20 50 67 48 64 72 2e 64  *.** The PgHdr.d
a66d0 69 72 74 79 20 66 6c 61 67 20 69 73 20 73 65 74  irty flag is set
a66e0 20 77 68 65 6e 20 73 71 6c 69 74 65 33 50 61 67   when sqlite3Pag
a66f0 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c  erWrite() is cal
a6700 6c 65 64 20 61 6e 64 0a 2a 2a 20 69 73 20 63 6c  led and.** is cl
a6710 65 61 72 65 64 20 61 67 61 69 6e 20 77 68 65 6e  eared again when
a6720 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e   the page conten
a6730 74 20 69 73 20 77 72 69 74 74 65 6e 20 62 61 63  t is written bac
a6740 6b 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61  k to the origina
a6750 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69  l.** database fi
a6760 6c 65 2e 0a 2a 2a 0a 2a 2a 20 44 65 74 61 69 6c  le..**.** Detail
a6770 73 20 6f 66 20 69 6d 70 6f 72 74 61 6e 74 20 73  s of important s
a6780 74 72 75 63 74 75 72 65 20 65 6c 65 6d 65 6e 74  tructure element
a6790 73 3a 0a 2a 2a 0a 2a 2a 20 6e 65 65 64 53 79 6e  s:.**.** needSyn
a67a0 63 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 66 20 74  c.**.**     If t
a67b0 68 69 73 20 69 73 20 74 72 75 65 2c 20 74 68 69  his is true, thi
a67c0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 69 74 20  s means that it 
a67d0 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 77  is not safe to w
a67e0 72 69 74 65 20 74 68 65 20 70 61 67 65 0a 2a 2a  rite the page.**
a67f0 20 20 20 20 20 63 6f 6e 74 65 6e 74 20 74 6f 20       content to 
a6800 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65 63  the database bec
a6810 61 75 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61  ause the origina
a6820 6c 20 63 6f 6e 74 65 6e 74 20 6e 65 65 64 65 64  l content needed
a6830 0a 2a 2a 20 20 20 20 20 66 6f 72 20 72 6f 6c 6c  .**     for roll
a6840 62 61 63 6b 20 68 61 73 20 6e 6f 74 20 62 79 20  back has not by 
a6850 73 79 6e 63 65 64 20 74 6f 20 74 68 65 20 6d 61  synced to the ma
a6860 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  in rollback jour
a6870 6e 61 6c 2e 0a 2a 2a 20 20 20 20 20 54 68 65 20  nal..**     The 
a6880 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74  original content
a6890 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 77   may have been w
a68a0 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 72 6f  ritten to the ro
a68b0 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a  llback journal.*
a68c0 2a 20 20 20 20 20 62 75 74 20 69 74 20 68 61 73  *     but it has
a68d0 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 73 79   not yet been sy
a68e0 6e 63 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e  nced.  So we can
a68f0 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65  not write to the
a6900 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20   database.**    
a6910 20 66 69 6c 65 20 62 65 63 61 75 73 65 20 70 6f   file because po
a6920 77 65 72 20 66 61 69 6c 75 72 65 20 6d 69 67 68  wer failure migh
a6930 74 20 63 61 75 73 65 20 74 68 65 20 70 61 67 65  t cause the page
a6940 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
a6950 66 69 6c 65 0a 2a 2a 20 20 20 20 20 74 6f 20 6e  file.**     to n
a6960 65 76 65 72 20 72 65 61 63 68 20 74 68 65 20 64  ever reach the d
a6970 69 73 6b 2e 20 20 49 74 20 69 73 20 61 73 20 69  isk.  It is as i
a6980 66 20 74 68 65 20 77 72 69 74 65 20 74 6f 20 74  f the write to t
a6990 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a  he journal file.
a69a0 2a 2a 20 20 20 20 20 64 6f 65 73 20 6e 6f 74 20  **     does not 
a69b0 6f 63 63 75 72 20 75 6e 74 69 6c 20 74 68 65 20  occur until the 
a69c0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
a69d0 73 79 6e 63 65 64 2e 0a 2a 2a 20 20 20 20 20 0a  synced..**     .
a69e0 2a 2a 20 20 20 20 20 54 68 69 73 20 66 6c 61 67  **     This flag
a69f0 20 69 73 20 66 61 6c 73 65 20 69 66 20 74 68 65   is false if the
a6a00 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 65 78   page content ex
a6a10 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 77 68  actly matches wh
a6a20 61 74 0a 2a 2a 20 20 20 20 20 63 75 72 72 65 6e  at.**     curren
a6a30 74 6c 79 20 65 78 69 73 74 73 20 69 6e 20 74 68  tly exists in th
a6a40 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
a6a50 20 20 54 68 65 20 6e 65 65 64 53 79 6e 63 20 66    The needSync f
a6a60 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 20  lag is also.**  
a6a70 20 20 20 66 61 6c 73 65 20 69 66 20 74 68 65 20     false if the 
a6a80 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74  original content
a6a90 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65   has been writte
a6aa0 6e 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f  n to the main ro
a6ab0 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 6a 6f  llback.**     jo
a6ac0 75 72 6e 61 6c 20 61 6e 64 20 73 79 6e 63 65 64  urnal and synced
a6ad0 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 72  .  If the page r
a6ae0 65 70 72 65 73 65 6e 74 73 20 61 20 6e 65 77 20  epresents a new 
a6af0 70 61 67 65 20 74 68 61 74 20 68 61 73 0a 2a 2a  page that has.**
a6b00 20 20 20 20 20 62 65 65 6e 20 61 64 64 65 64 20       been added 
a6b10 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  onto the end of 
a6b20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 75 72  the database dur
a6b30 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 0a  ing the current.
a6b40 2a 2a 20 20 20 20 20 74 72 61 6e 73 61 63 74 69  **     transacti
a6b50 6f 6e 2c 20 74 68 65 20 6e 65 65 64 53 79 6e 63  on, the needSync
a6b60 20 66 6c 61 67 20 69 73 20 74 72 75 65 20 75 6e   flag is true un
a6b70 74 69 6c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  til the original
a6b80 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20   database.**    
a6b90 20 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f 75   size in the jou
a6ba0 72 6e 61 6c 20 68 65 61 64 65 72 20 68 61 73 20  rnal header has 
a6bb0 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64  been synced to d
a6bc0 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 69 6e 4a 6f 75  isk..**.** inJou
a6bd0 72 6e 61 6c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 54  rnal.**.**     T
a6be0 68 69 73 20 69 73 20 74 72 75 65 20 69 66 20 74  his is true if t
a6bf0 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
a6c00 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65   has been writte
a6c10 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a  n into the main.
a6c20 2a 2a 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b 20  **     rollback 
a6c30 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 69 73 20 69  journal.  This i
a6c40 73 20 61 6c 77 61 79 73 20 66 61 6c 73 65 20 66  s always false f
a6c50 6f 72 20 6e 65 77 20 70 61 67 65 73 20 61 64 64  or new pages add
a6c60 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65  ed to.**     the
a6c70 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61 74 61   end of the data
a6c80 62 61 73 65 20 66 69 6c 65 20 64 75 72 69 6e 67  base file during
a6c90 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
a6ca0 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 20 20 20  nsaction..**    
a6cb0 20 41 6e 64 20 74 68 69 73 20 66 6c 61 67 20 73   And this flag s
a6cc0 61 79 73 20 6e 6f 74 68 69 6e 67 20 61 62 6f 75  ays nothing abou
a6cd0 74 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  t whether or not
a6ce0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20   the journal.** 
a6cf0 20 20 20 20 68 61 73 20 62 65 65 6e 20 73 79 6e      has been syn
a6d00 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 46 6f  ced to disk.  Fo
a6d10 72 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65  r pages that are
a6d20 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
a6d30 0a 2a 2a 20 20 20 20 20 64 61 74 61 62 61 73 65  .**     database
a6d40 20 66 69 6c 65 2c 20 74 68 65 20 66 6f 6c 6c 6f   file, the follo
a6d50 77 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 20  wing expression 
a6d60 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
a6d70 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   true:.**.**    
a6d80 20 20 20 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 28     inJournal = (
a6d90 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e  pPager->aInJourn
a6da0 61 6c 5b 28 70 67 6e 6f 2d 31 29 2f 38 5d 20 26  al[(pgno-1)/8] &
a6db0 20 28 31 3c 3c 28 28 70 67 6e 6f 2d 31 29 25 38   (1<<((pgno-1)%8
a6dc0 29 29 21 3d 30 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ))!=0.**.**     
a6dd0 54 68 65 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a  The pPager->aInJ
a6de0 6f 75 72 6e 61 6c 5b 5d 20 61 72 72 61 79 20 69  ournal[] array i
a6df0 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72  s only valid for
a6e00 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a   the original.**
a6e10 20 20 20 20 20 70 61 67 65 73 20 6f 66 20 74 68       pages of th
a6e20 65 20 64 61 74 61 62 61 73 65 2c 20 6e 6f 74 20  e database, not 
a6e30 6e 65 77 20 70 61 67 65 73 20 74 68 61 74 20 61  new pages that a
a6e40 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  re added to the 
a6e50 65 6e 64 0a 2a 2a 20 20 20 20 20 6f 66 20 74 68  end.**     of th
a6e60 65 20 64 61 74 61 62 61 73 65 2c 20 73 6f 20 6f  e database, so o
a6e70 62 76 69 6f 75 73 6c 79 20 74 68 65 20 61 62 6f  bviously the abo
a6e80 76 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61  ve expression ca
a6e90 6e 6e 6f 74 20 62 65 0a 2a 2a 20 20 20 20 20 76  nnot be.**     v
a6ea0 61 6c 69 64 20 66 6f 72 20 6e 65 77 20 70 61 67  alid for new pag
a6eb0 65 73 2e 20 20 46 6f 72 20 6e 65 77 20 70 61 67  es.  For new pag
a6ec0 65 73 20 69 6e 4a 6f 75 72 6e 61 6c 20 69 73 20  es inJournal is 
a6ed0 61 6c 77 61 79 73 20 30 2e 0a 2a 2a 0a 2a 2a 20  always 0..**.** 
a6ee0 64 69 72 74 79 0a 2a 2a 0a 2a 2a 20 20 20 20 20  dirty.**.**     
a6ef0 57 68 65 6e 20 74 72 75 65 2c 20 74 68 69 73 20  When true, this 
a6f00 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63  means that the c
a6f10 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
a6f20 67 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 20  ge has been.**  
a6f30 20 20 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 20     modified and 
a6f40 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 69 74  needs to be writ
a6f50 74 65 6e 20 62 61 63 6b 20 74 6f 20 74 68 65 20  ten back to the 
a6f60 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
a6f70 2a 20 20 20 20 20 49 66 20 66 61 6c 73 65 2c 20  *     If false, 
a6f80 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 69  it means that ei
a6f90 74 68 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74  ther the content
a6fa0 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 73 0a   of the page is.
a6fb0 2a 2a 20 20 20 20 20 75 6e 63 68 61 6e 67 65 64  **     unchanged
a6fc0 20 6f 72 20 65 6c 73 65 20 74 68 65 20 63 6f 6e   or else the con
a6fd0 74 65 6e 74 20 69 73 20 75 6e 69 6d 70 6f 72 74  tent is unimport
a6fe0 61 6e 74 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f  ant and we do no
a6ff0 74 0a 2a 2a 20 20 20 20 20 63 61 72 65 20 77 68  t.**     care wh
a7000 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20  ether or not it 
a7010 69 73 20 70 72 65 73 65 72 76 65 64 2e 0a 2a 2a  is preserved..**
a7020 0a 2a 2a 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61  .** alwaysRollba
a7030 63 6b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 54 68 69  ck.**.**     Thi
a7040 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  s means that the
a7050 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
a7060 74 52 6f 6c 6c 62 61 63 6b 28 29 20 41 50 49 20  tRollback() API 
a7070 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 20 20 20  should be.**    
a7080 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 74 68 69   ignored for thi
a7090 73 20 70 61 67 65 2e 20 20 54 68 65 20 44 6f 6e  s page.  The Don
a70a0 74 52 6f 6c 6c 62 61 63 6b 28 29 20 41 50 49 20  tRollback() API 
a70b0 61 74 74 65 6d 70 74 73 20 74 6f 20 73 61 79 0a  attempts to say.
a70c0 2a 2a 20 20 20 20 20 74 68 61 74 20 74 68 65 20  **     that the 
a70d0 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70  content of the p
a70e0 61 67 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 75  age on disk is u
a70f0 6e 69 6d 70 6f 72 74 61 6e 74 20 28 69 74 20 69  nimportant (it i
a7100 73 20 61 6e 0a 2a 2a 20 20 20 20 20 75 6e 75 73  s an.**     unus
a7110 65 64 20 70 61 67 65 20 6f 6e 20 74 68 65 20 66  ed page on the f
a7120 72 65 65 6c 69 73 74 29 20 73 6f 20 74 68 61 74  reelist) so that
a7130 20 69 74 20 69 73 20 75 6e 6e 65 63 65 73 73 61   it is unnecessa
a7140 72 79 20 74 6f 20 0a 2a 2a 20 20 20 20 20 72 6f  ry to .**     ro
a7150 6c 6c 62 61 63 6b 20 63 68 61 6e 67 65 73 20 74  llback changes t
a7160 6f 20 74 68 69 73 20 70 61 67 65 20 62 65 63 61  o this page beca
a7170 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  use the content 
a7180 6f 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 20  of the page.**  
a7190 20 20 20 63 61 6e 20 63 68 61 6e 67 65 20 77 69     can change wi
a71a0 74 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 74  thout changing t
a71b0 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68  he meaning of th
a71c0 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
a71d0 73 0a 2a 2a 20 20 20 20 20 66 6c 61 67 20 6f 76  s.**     flag ov
a71e0 65 72 72 69 64 65 73 20 61 6e 79 20 44 6f 6e 74  errides any Dont
a71f0 52 6f 6c 6c 62 61 63 6b 28 29 20 61 74 74 65 6d  Rollback() attem
a7200 70 74 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69  pt.  This flag i
a7210 73 20 73 65 74 0a 2a 2a 20 20 20 20 20 77 68 65  s set.**     whe
a7220 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 6f 72  n a page that or
a7230 69 67 69 6e 61 6c 6c 79 20 63 6f 6e 74 61 69 6e  iginally contain
a7240 65 64 20 76 61 6c 69 64 20 64 61 74 61 20 69 73  ed valid data is
a7250 20 61 64 64 65 64 20 74 6f 0a 2a 2a 20 20 20 20   added to.**    
a7260 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20   the freelist.  
a7270 4c 61 74 65 72 20 69 6e 20 74 68 65 20 73 61 6d  Later in the sam
a7280 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  e transaction, t
a7290 68 69 73 20 70 61 67 65 20 6d 69 67 68 74 0a 2a  his page might.*
a72a0 2a 20 20 20 20 20 62 65 20 70 75 6c 6c 65 64 20  *     be pulled 
a72b0 66 72 6f 6d 20 74 68 65 20 66 72 65 65 6c 69 73  from the freelis
a72c0 74 20 61 6e 64 20 72 65 75 73 65 64 20 66 6f 72  t and reused for
a72d0 20 73 6f 6d 65 74 68 69 6e 67 20 64 69 66 66 65   something diffe
a72e0 72 65 6e 74 0a 2a 2a 20 20 20 20 20 61 6e 64 20  rent.**     and 
a72f0 61 74 20 74 68 61 74 20 70 6f 69 6e 74 20 74 68  at that point th
a7300 65 20 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29  e DontRollback()
a7310 20 41 50 49 20 77 69 6c 6c 20 62 65 20 63 61 6c   API will be cal
a7320 6c 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 20  led because.**  
a7330 20 20 20 70 61 67 65 73 20 74 61 6b 65 6e 20 66     pages taken f
a7340 72 6f 6d 20 74 68 65 20 66 72 65 65 6c 69 73 74  rom the freelist
a7350 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
a7360 62 65 20 70 72 6f 74 65 63 74 65 64 20 62 79 0a  be protected by.
a7370 2a 2a 20 20 20 20 20 74 68 65 20 72 6f 6c 6c 62  **     the rollb
a7380 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 42 75  ack journal.  Bu
a7390 74 20 74 68 69 73 20 66 6c 61 67 20 73 61 79 73  t this flag says
a73a0 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 77   that the page w
a73b0 61 73 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6f 72  as.**     not or
a73c0 69 67 69 6e 61 6c 6c 79 20 70 61 72 74 20 6f 66  iginally part of
a73d0 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f   the freelist so
a73e0 20 74 68 61 74 20 69 74 20 73 74 69 6c 6c 20 6e   that it still n
a73f0 65 65 64 73 20 74 6f 0a 2a 2a 20 20 20 20 20 62  eeds to.**     b
a7400 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 6e  e rolled back in
a7410 20 73 70 69 74 65 20 6f 66 20 61 6e 79 20 73 75   spite of any su
a7420 62 73 65 71 75 65 6e 74 20 44 6f 6e 74 52 6f 6c  bsequent DontRol
a7430 6c 62 61 63 6b 28 29 20 63 61 6c 6c 73 2e 0a 2a  lback() calls..*
a7440 2a 0a 2a 2a 20 6e 65 65 64 52 65 61 64 20 0a 2a  *.** needRead .*
a7450 2a 0a 2a 2a 20 20 20 20 20 54 68 69 73 20 66 6c  *.**     This fl
a7460 61 67 20 6d 65 61 6e 73 20 28 77 68 65 6e 20 74  ag means (when t
a7470 72 75 65 29 20 74 68 61 74 20 74 68 65 20 63 6f  rue) that the co
a7480 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67  ntent of the pag
a7490 65 20 68 61 73 0a 2a 2a 20 20 20 20 20 6e 6f 74  e has.**     not
a74a0 20 79 65 74 20 62 65 65 6e 20 6c 6f 61 64 65 64   yet been loaded
a74b0 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 54 68 65   from disk.  The
a74c0 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 6f 6e 74 65   in-memory conte
a74d0 6e 74 20 69 73 20 6a 75 73 74 0a 2a 2a 20 20 20  nt is just.**   
a74e0 20 20 67 61 72 62 61 67 65 2e 20 20 28 41 63 74    garbage.  (Act
a74f0 75 61 6c 6c 79 2c 20 77 65 20 7a 65 72 6f 20 74  ually, we zero t
a7500 68 65 20 63 6f 6e 74 65 6e 74 2c 20 62 75 74 20  he content, but 
a7510 79 6f 75 20 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a  you should not.*
a7520 2a 20 20 20 20 20 6d 61 6b 65 20 61 6e 79 20 61  *     make any a
a7530 73 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74  ssumptions about
a7540 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6e 65 76   the content nev
a7550 65 72 74 68 65 6c 65 73 73 2e 29 20 20 49 66 20  ertheless.)  If 
a7560 74 68 65 0a 2a 2a 20 20 20 20 20 63 6f 6e 74 65  the.**     conte
a7570 6e 74 20 69 73 20 6e 65 65 64 65 64 20 69 6e 20  nt is needed in 
a7580 74 68 65 20 66 75 74 75 72 65 2c 20 69 74 20 73  the future, it s
a7590 68 6f 75 6c 64 20 62 65 20 72 65 61 64 20 66 72  hould be read fr
a75a0 6f 6d 20 74 68 65 0a 2a 2a 20 20 20 20 20 6f 72  om the.**     or
a75b0 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20  iginal database 
a75c0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  file..*/.struct 
a75d0 50 67 48 64 72 20 7b 0a 20 20 50 61 67 65 72 20  PgHdr {.  Pager 
a75e0 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20  *pPager;        
a75f0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
a7600 70 61 67 65 72 20 74 6f 20 77 68 69 63 68 20 74  pager to which t
a7610 68 69 73 20 70 61 67 65 20 62 65 6c 6f 6e 67 73  his page belongs
a7620 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b   */.  Pgno pgno;
a7630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7640 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
a7650 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73   number for this
a7660 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72   page */.  PgHdr
a7670 20 2a 70 4e 65 78 74 48 61 73 68 2c 20 2a 70 50   *pNextHash, *pP
a7680 72 65 76 48 61 73 68 3b 20 20 2f 2a 20 48 61 73  revHash;  /* Has
a7690 68 20 63 6f 6c 6c 69 73 69 6f 6e 20 63 68 61 69  h collision chai
a76a0 6e 20 66 6f 72 20 50 67 48 64 72 2e 70 67 6e 6f  n for PgHdr.pgno
a76b0 20 2a 2f 0a 20 20 50 61 67 65 72 4c 72 75 4c 69   */.  PagerLruLi
a76c0 6e 6b 20 66 72 65 65 3b 20 20 20 20 20 20 20 20  nk free;        
a76d0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64       /* Next and
a76e0 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 70   previous free p
a76f0 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 20  ages */.  PgHdr 
a7700 2a 70 4e 65 78 74 41 6c 6c 3b 20 20 20 20 20 20  *pNextAll;      
a7710 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 6c 69           /* A li
a7720 73 74 20 6f 66 20 61 6c 6c 20 70 61 67 65 73 20  st of all pages 
a7730 2a 2f 0a 20 20 75 38 20 69 6e 4a 6f 75 72 6e 61  */.  u8 inJourna
a7740 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
a7750 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 68      /* TRUE if h
a7760 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  as been written 
a7770 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  to journal */.  
a7780 75 38 20 64 69 72 74 79 3b 20 20 20 20 20 20 20  u8 dirty;       
a7790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a77a0 2a 20 54 52 55 45 20 69 66 20 77 65 20 6e 65 65  * TRUE if we nee
a77b0 64 20 74 6f 20 77 72 69 74 65 20 62 61 63 6b 20  d to write back 
a77c0 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 75 38 20  changes */.  u8 
a77d0 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20  needSync;       
a77e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
a77f0 79 6e 63 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f  ync journal befo
a7800 72 65 20 77 72 69 74 69 6e 67 20 74 68 69 73 20  re writing this 
a7810 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 61 6c 77  page */.  u8 alw
a7820 61 79 73 52 6f 6c 6c 62 61 63 6b 3b 20 20 20 20  aysRollback;    
a7830 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73 61           /* Disa
a7840 62 6c 65 20 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b  ble DontRollback
a7850 28 29 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  () for this page
a7860 20 2a 2f 0a 20 20 75 38 20 6e 65 65 64 52 65 61   */.  u8 needRea
a7870 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
a7880 20 20 20 20 20 2f 2a 20 52 65 61 64 20 63 6f 6e       /* Read con
a7890 74 65 6e 74 20 69 66 20 50 61 67 65 72 57 72 69  tent if PagerWri
a78a0 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 2a  te() is called *
a78b0 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20 6e 52  /.  short int nR
a78c0 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
a78d0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
a78e0 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 70 61  users of this pa
a78f0 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70  ge */.  PgHdr *p
a7900 44 69 72 74 79 2c 20 2a 70 50 72 65 76 44 69 72  Dirty, *pPrevDir
a7910 74 79 3b 20 20 20 20 2f 2a 20 44 69 72 74 79 20  ty;    /* Dirty 
a7920 70 61 67 65 73 20 2a 2f 0a 23 69 66 64 65 66 20  pages */.#ifdef 
a7930 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
a7940 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
a7950 20 20 50 61 67 65 72 4c 72 75 4c 69 6e 6b 20 67    PagerLruLink g
a7960 66 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  free;           
a7970 20 2f 2a 20 47 6c 6f 62 61 6c 20 6c 69 73 74 20   /* Global list 
a7980 6f 66 20 6e 52 65 66 3d 3d 30 20 70 61 67 65 73  of nRef==0 pages
a7990 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65   */.#endif.#ifde
a79a0 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
a79b0 41 47 45 53 0a 20 20 75 33 32 20 70 61 67 65 48  AGES.  u32 pageH
a79c0 61 73 68 3b 0a 23 65 6e 64 69 66 0a 20 20 76 6f  ash;.#endif.  vo
a79d0 69 64 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20  id *pData;      
a79e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a79f0 50 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 2f  Page data */.  /
a7a00 2a 20 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62  * Pager.nExtra b
a7a10 79 74 65 73 20 6f 66 20 6c 6f 63 61 6c 20 64 61  ytes of local da
a7a20 74 61 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  ta appended to t
a7a30 68 69 73 20 68 65 61 64 65 72 20 2a 2f 0a 7d 3b  his header */.};
a7a40 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 69  ../*.** For an i
a7a50 6e 2d 6d 65 6d 6f 72 79 20 6f 6e 6c 79 20 64 61  n-memory only da
a7a60 74 61 62 61 73 65 2c 20 73 6f 6d 65 20 65 78 74  tabase, some ext
a7a70 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ra information i
a7a80 73 20 72 65 63 6f 72 64 65 64 20 61 62 6f 75 74  s recorded about
a7a90 0a 2a 2a 20 65 61 63 68 20 70 61 67 65 20 73 6f  .** each page so
a7aa0 20 74 68 61 74 20 63 68 61 6e 67 65 73 20 63 61   that changes ca
a7ab0 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n be rolled back
a7ac0 2e 20 20 28 4a 6f 75 72 6e 61 6c 20 66 69 6c 65  .  (Journal file
a7ad0 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 75 73 65  s are not.** use
a7ae0 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20  d for in-memory 
a7af0 64 61 74 61 62 61 73 65 73 2e 29 20 20 54 68 65  databases.)  The
a7b00 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72   following infor
a7b10 6d 61 74 69 6f 6e 20 69 73 20 61 64 64 65 64 20  mation is added 
a7b20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  to.** the end of
a7b30 20 65 76 65 72 79 20 45 58 54 52 41 20 62 6c 6f   every EXTRA blo
a7b40 63 6b 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79  ck for in-memory
a7b50 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a   databases..**.*
a7b60 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  * This informati
a7b70 6f 6e 20 63 6f 75 6c 64 20 68 61 76 65 20 62 65  on could have be
a7b80 65 6e 20 61 64 64 65 64 20 64 69 72 65 63 74 6c  en added directl
a7b90 79 20 74 6f 20 74 68 65 20 50 67 48 64 72 20 73  y to the PgHdr s
a7ba0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75 74  tructure..** But
a7bb0 20 74 68 65 6e 20 69 74 20 77 6f 75 6c 64 20 74   then it would t
a7bc0 61 6b 65 20 75 70 20 61 6e 20 65 78 74 72 61 20  ake up an extra 
a7bd0 38 20 62 79 74 65 73 20 6f 66 20 73 74 6f 72 61  8 bytes of stora
a7be0 67 65 20 6f 6e 20 65 76 65 72 79 20 50 67 48 64  ge on every PgHd
a7bf0 72 0a 2a 2a 20 65 76 65 6e 20 66 6f 72 20 64 69  r.** even for di
a7c00 73 6b 2d 62 61 73 65 64 20 64 61 74 61 62 61 73  sk-based databas
a7c10 65 73 2e 20 20 53 70 6c 69 74 74 69 6e 67 20 69  es.  Splitting i
a7c20 74 20 6f 75 74 20 73 61 76 65 73 20 38 20 62 79  t out saves 8 by
a7c30 74 65 73 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73  tes.  This.** is
a7c40 20 6f 6e 6c 79 20 61 20 73 61 76 69 6e 67 73 20   only a savings 
a7c50 6f 66 20 30 2e 38 25 20 62 75 74 20 74 68 6f 73  of 0.8% but thos
a7c60 65 20 70 65 72 63 65 6e 74 61 67 65 73 20 61 64  e percentages ad
a7c70 64 20 75 70 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  d up..*/.typedef
a7c80 20 73 74 72 75 63 74 20 50 67 48 69 73 74 6f 72   struct PgHistor
a7c90 79 20 50 67 48 69 73 74 6f 72 79 3b 0a 73 74 72  y PgHistory;.str
a7ca0 75 63 74 20 50 67 48 69 73 74 6f 72 79 20 7b 0a  uct PgHistory {.
a7cb0 20 20 75 38 20 2a 70 4f 72 69 67 3b 20 20 20 20    u8 *pOrig;    
a7cc0 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 70 61 67   /* Original pag
a7cd0 65 20 74 65 78 74 2e 20 20 52 65 73 74 6f 72 65  e text.  Restore
a7ce0 20 74 6f 20 74 68 69 73 20 6f 6e 20 61 20 66 75   to this on a fu
a7cf0 6c 6c 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ll rollback */. 
a7d00 20 75 38 20 2a 70 53 74 6d 74 3b 20 20 20 20 20   u8 *pStmt;     
a7d10 2f 2a 20 54 65 78 74 20 61 73 20 69 74 20 77 61  /* Text as it wa
a7d20 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  s at the beginni
a7d30 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ng of the curren
a7d40 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  t statement */. 
a7d50 20 50 67 48 64 72 20 2a 70 4e 65 78 74 53 74 6d   PgHdr *pNextStm
a7d60 74 2c 20 2a 70 50 72 65 76 53 74 6d 74 3b 20 20  t, *pPrevStmt;  
a7d70 2f 2a 20 4c 69 73 74 20 6f 66 20 70 61 67 65 73  /* List of pages
a7d80 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   in the statemen
a7d90 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75  t journal */.  u
a7da0 38 20 69 6e 53 74 6d 74 3b 20 20 20 20 20 20 20  8 inStmt;       
a7db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a7dc0 20 54 52 55 45 20 69 66 20 69 6e 20 74 68 65 20   TRUE if in the 
a7dd0 73 74 61 74 65 6d 65 6e 74 20 73 75 62 6a 6f 75  statement subjou
a7de0 72 6e 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  rnal */.};../*.*
a7df0 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 20 66  * A macro used f
a7e00 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  or invoking the 
a7e10 63 6f 64 65 63 20 69 66 20 74 68 65 72 65 20 69  codec if there i
a7e20 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20  s one.*/.#ifdef 
a7e30 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
a7e40 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 31  .# define CODEC1
a7e50 28 50 2c 44 2c 4e 2c 58 29 20 69 66 28 20 50 2d  (P,D,N,X) if( P-
a7e60 3e 78 43 6f 64 65 63 21 3d 30 20 29 7b 20 50 2d  >xCodec!=0 ){ P-
a7e70 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65  >xCodec(P->pCode
a7e80 63 41 72 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23  cArg,D,N,X); }.#
a7e90 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50   define CODEC2(P
a7ea0 2c 44 2c 4e 2c 58 29 20 28 28 63 68 61 72 2a 29  ,D,N,X) ((char*)
a7eb0 28 50 2d 3e 78 43 6f 64 65 63 21 3d 30 3f 50 2d  (P->xCodec!=0?P-
a7ec0 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65  >xCodec(P->pCode
a7ed0 63 41 72 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a  cArg,D,N,X):D)).
a7ee0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 43  #else.# define C
a7ef0 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20 2f  ODEC1(P,D,N,X) /
a7f00 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66  * NO-OP */.# def
a7f10 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e  ine CODEC2(P,D,N
a7f20 2c 58 29 20 28 28 63 68 61 72 2a 29 44 29 0a 23  ,X) ((char*)D).#
a7f30 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  endif../*.** Con
a7f40 76 65 72 74 20 61 20 70 6f 69 6e 74 65 72 20 74  vert a pointer t
a7f50 6f 20 61 20 50 67 48 64 72 20 69 6e 74 6f 20 61  o a PgHdr into a
a7f60 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20   pointer to its 
a7f70 64 61 74 61 0a 2a 2a 20 61 6e 64 20 62 61 63 6b  data.** and back
a7f80 20 61 67 61 69 6e 2e 0a 2a 2f 0a 23 64 65 66 69   again..*/.#defi
a7f90 6e 65 20 50 47 48 44 52 5f 54 4f 5f 44 41 54 41  ne PGHDR_TO_DATA
a7fa0 28 50 29 20 20 20 20 28 28 50 29 2d 3e 70 44 61  (P)    ((P)->pDa
a7fb0 74 61 29 0a 23 64 65 66 69 6e 65 20 50 47 48 44  ta).#define PGHD
a7fc0 52 5f 54 4f 5f 45 58 54 52 41 28 47 2c 50 29 20  R_TO_EXTRA(G,P) 
a7fd0 28 28 76 6f 69 64 2a 29 26 28 28 47 29 5b 31 5d  ((void*)&((G)[1]
a7fe0 29 29 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52  )).#define PGHDR
a7ff0 5f 54 4f 5f 48 49 53 54 28 50 2c 50 47 52 29 20  _TO_HIST(P,PGR) 
a8000 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 28   \.            (
a8010 28 50 67 48 69 73 74 6f 72 79 2a 29 26 28 28 63  (PgHistory*)&((c
a8020 68 61 72 2a 29 28 26 28 50 29 5b 31 5d 29 29 5b  har*)(&(P)[1]))[
a8030 28 50 47 52 29 2d 3e 6e 45 78 74 72 61 5d 29 0a  (PGR)->nExtra]).
a8040 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e 20 70 61  ./*.** A open pa
a8050 67 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69  ge cache is an i
a8060 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
a8070 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
a8080 72 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 72 2e  re..**.** Pager.
a8090 65 72 72 43 6f 64 65 20 6d 61 79 20 62 65 20 73  errCode may be s
a80a0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 49 4f 45  et to SQLITE_IOE
a80b0 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  RR, SQLITE_CORRU
a80c0 50 54 2c 20 6f 72 0a 2a 2a 20 6f 72 20 53 51 4c  PT, or.** or SQL
a80d0 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e 63 65 20 6f  ITE_FULL. Once o
a80e0 6e 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ne of the first 
a80f0 74 68 72 65 65 20 65 72 72 6f 72 73 20 6f 63 63  three errors occ
a8100 75 72 73 2c 20 69 74 20 70 65 72 73 69 73 74 73  urs, it persists
a8110 0a 2a 2a 20 61 6e 64 20 69 73 20 72 65 74 75 72  .** and is retur
a8120 6e 65 64 20 61 73 20 74 68 65 20 72 65 73 75 6c  ned as the resul
a8130 74 20 6f 66 20 65 76 65 72 79 20 6d 61 6a 6f 72  t of every major
a8140 20 70 61 67 65 72 20 41 50 49 20 63 61 6c 6c 2e   pager API call.
a8150 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f    The.** SQLITE_
a8160 46 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64 65  FULL return code
a8170 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66   is slightly dif
a8180 66 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73 69  ferent. It persi
a8190 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74  sts only until t
a81a0 68 65 0a 2a 2a 20 6e 65 78 74 20 73 75 63 63 65  he.** next succe
a81b0 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 69  ssful rollback i
a81c0 73 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74  s performed on t
a81d0 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20  he pager cache. 
a81e0 41 6c 73 6f 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  Also,.** SQLITE_
a81f0 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66  FULL does not af
a8200 66 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 33  fect the sqlite3
a8210 50 61 67 65 72 47 65 74 28 29 20 61 6e 64 20 73  PagerGet() and s
a8220 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
a8230 70 28 29 0a 2a 2a 20 41 50 49 73 2c 20 74 68 65  p().** APIs, the
a8240 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75  y may still be u
a8250 73 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  sed successfully
a8260 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 67 65  ..*/.struct Page
a8270 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  r {.  sqlite3_vf
a8280 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
a8290 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e    /* OS function
a82a0 73 20 74 6f 20 75 73 65 20 66 6f 72 20 49 4f 20  s to use for IO 
a82b0 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4f  */.  u8 journalO
a82c0 70 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pen;            
a82d0 20 2f 2a 20 54 72 75 65 20 69 66 20 6a 6f 75 72   /* True if jour
a82e0 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
a82f0 74 6f 72 73 20 69 73 20 76 61 6c 69 64 20 2a 2f  tors is valid */
a8300 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53 74 61  .  u8 journalSta
a8310 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f  rted;          /
a8320 2a 20 54 72 75 65 20 69 66 20 68 65 61 64 65 72  * True if header
a8330 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73   of journal is s
a8340 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 75 73  ynced */.  u8 us
a8350 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20  eJournal;       
a8360 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20         /* Use a 
a8370 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
a8380 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   on this file */
a8390 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b  .  u8 noReadlock
a83a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a83b0 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20  * Do not bother 
a83c0 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f  to obtain readlo
a83d0 63 6b 73 20 2a 2f 0a 20 20 75 38 20 73 74 6d 74  cks */.  u8 stmt
a83e0 4f 70 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  Open;           
a83f0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a8400 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75  the statement su
a8410 62 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e  bjournal is open
a8420 20 2a 2f 0a 20 20 75 38 20 73 74 6d 74 49 6e 55   */.  u8 stmtInU
a8430 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
a8440 20 20 2f 2a 20 54 72 75 65 20 77 65 20 61 72 65    /* True we are
a8450 20 69 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20   in a statement 
a8460 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  subtransaction *
a8470 2f 0a 20 20 75 38 20 73 74 6d 74 41 75 74 6f 6f  /.  u8 stmtAutoo
a8480 70 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pen;            
a8490 2f 2a 20 4f 70 65 6e 20 73 74 6d 74 20 6a 6f 75  /* Open stmt jou
a84a0 72 6e 61 6c 20 77 68 65 6e 20 6d 61 69 6e 20 6a  rnal when main j
a84b0 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 65 64  ournal is opened
a84c0 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b 20  */.  u8 noSync; 
a84d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a84e0 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 20   /* Do not sync 
a84f0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74  the journal if t
a8500 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c 6c  rue */.  u8 full
a8510 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Sync;           
a8520 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 61       /* Do extra
a8530 20 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a 6f   syncs of the jo
a8540 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 74  urnal for robust
a8550 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e  ness */.  u8 syn
a8560 63 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  c_flags;        
a8570 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
a8580 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53  SYNC_NORMAL or S
a8590 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 38  YNC_FULL */.  u8
a85a0 20 73 74 61 74 65 3b 20 20 20 20 20 20 20 20 20   state;         
a85b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47            /* PAG
a85c0 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52  ER_UNLOCK, _SHAR
a85d0 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c 20 65  ED, _RESERVED, e
a85e0 74 63 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70  tc. */.  u8 temp
a85f0 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  File;           
a8600 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d       /* zFilenam
a8610 65 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79  e is a temporary
a8620 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 72 65   file */.  u8 re
a8630 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20  adOnly;         
a8640 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
a8650 6f 72 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64  or a read-only d
a8660 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20  atabase */.  u8 
a8670 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20  needSync;       
a8680 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a8690 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69   if an fsync() i
a86a0 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20  s needed on the 
a86b0 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20  journal */.  u8 
a86c0 64 69 72 74 79 43 61 63 68 65 3b 20 20 20 20 20  dirtyCache;     
a86d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a86e0 20 69 66 20 63 61 63 68 65 64 20 70 61 67 65 73   if cached pages
a86f0 20 68 61 76 65 20 63 68 61 6e 67 65 64 20 2a 2f   have changed */
a8700 0a 20 20 75 38 20 61 6c 77 61 79 73 52 6f 6c 6c  .  u8 alwaysRoll
a8710 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f  back;          /
a8720 2a 20 44 69 73 61 62 6c 65 20 44 6f 6e 74 52 6f  * Disable DontRo
a8730 6c 6c 62 61 63 6b 28 29 20 66 6f 72 20 61 6c 6c  llback() for all
a8740 20 70 61 67 65 73 20 2a 2f 0a 20 20 75 38 20 6d   pages */.  u8 m
a8750 65 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20  emDb;           
a8760 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a8770 74 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66  to inhibit all f
a8780 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 75 38 20  ile I/O */.  u8 
a8790 73 65 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20  setMaster;      
a87a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a87b0 20 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68   if a m-j name h
a87c0 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  as been written 
a87d0 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20  to jrnl */.  u8 
a87e0 64 6f 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20  doNotSync;      
a87f0 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c           /* Bool
a8800 65 61 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c  ean. While true,
a8810 20 64 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68   do not spill th
a8820 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20  e cache */.  u8 
a8830 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20  exclusiveMode;  
a8840 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c           /* Bool
a8850 65 61 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63  ean. True if loc
a8860 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55  king_mode==EXCLU
a8870 53 49 56 45 20 2a 2f 0a 20 20 75 38 20 63 68 61  SIVE */.  u8 cha
a8880 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20  ngeCountDone;   
a8890 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74        /* Set aft
a88a0 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  er incrementing 
a88b0 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
a88c0 65 72 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46  er */.  u32 vfsF
a88d0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
a88e0 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72      /* Flags for
a88f0 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70   sqlite3_vfs.xOp
a8900 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72  en() */.  int er
a8910 72 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  rCode;          
a8920 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
a8930 73 65 76 65 72 61 6c 20 6b 69 6e 64 73 20 6f 66  several kinds of
a8940 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74   errors */.  int
a8950 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20   dbSize;        
a8960 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
a8970 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
a8980 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  he file */.  int
a8990 20 6f 72 69 67 44 62 53 69 7a 65 3b 20 20 20 20   origDbSize;    
a89a0 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69           /* dbSi
a89b0 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 75  ze before the cu
a89c0 72 72 65 6e 74 20 63 68 61 6e 67 65 20 2a 2f 0a  rrent change */.
a89d0 20 20 69 6e 74 20 73 74 6d 74 53 69 7a 65 3b 20    int stmtSize; 
a89e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a89f0 20 53 69 7a 65 20 6f 66 20 64 61 74 61 62 61 73   Size of databas
a8a00 65 20 28 69 6e 20 70 61 67 65 73 29 20 61 74 20  e (in pages) at 
a8a10 73 74 6d 74 5f 62 65 67 69 6e 28 29 20 2a 2f 0a  stmt_begin() */.
a8a20 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20    int nRec;     
a8a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a8a40 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
a8a50 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
a8a60 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32  journal */.  u32
a8a70 20 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20   cksumInit;     
a8a80 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73           /* Quas
a8a90 69 2d 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61  i-random value a
a8aa0 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 63 68  dded to every ch
a8ab0 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 69 6e 74 20  ecksum */.  int 
a8ac0 73 74 6d 74 4e 52 65 63 3b 20 20 20 20 20 20 20  stmtNRec;       
a8ad0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
a8ae0 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e 20  r of records in 
a8af0 73 74 6d 74 20 73 75 62 6a 6f 75 72 6e 61 6c 20  stmt subjournal 
a8b00 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 3b  */.  int nExtra;
a8b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8b20 20 2f 2a 20 41 64 64 20 74 68 69 73 20 6d 61 6e   /* Add this man
a8b30 79 20 62 79 74 65 73 20 74 6f 20 65 61 63 68 20  y bytes to each 
a8b40 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a  in-memory page *
a8b50 2f 0a 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65  /.  int pageSize
a8b60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a8b70 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
a8b80 65 73 20 69 6e 20 61 20 70 61 67 65 20 2a 2f 0a  es in a page */.
a8b90 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20    int nPage;    
a8ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a8bb0 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
a8bc0 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
a8bd0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
a8be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8bf0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
a8c00 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 77  n-memory pages w
a8c10 69 74 68 20 50 67 48 64 72 2e 6e 52 65 66 3e 30  ith PgHdr.nRef>0
a8c20 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 67 65   */.  int mxPage
a8c30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a8c40 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d    /* Maximum num
a8c50 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20  ber of pages to 
a8c60 68 6f 6c 64 20 69 6e 20 63 61 63 68 65 20 2a 2f  hold in cache */
a8c70 0a 20 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b 20  .  Pgno mxPgno; 
a8c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a8c90 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65  * Maximum allowe
a8ca0 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  d size of the da
a8cb0 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 2a  tabase */.  u8 *
a8cc0 61 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20  aInJournal;     
a8cd0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62          /* One b
a8ce0 69 74 20 66 6f 72 20 65 61 63 68 20 70 61 67 65  it for each page
a8cf0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
a8d00 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 2a 61   file */.  u8 *a
a8d10 49 6e 53 74 6d 74 3b 20 20 20 20 20 20 20 20 20  InStmt;         
a8d20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69         /* One bi
a8d30 74 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  t for each page 
a8d40 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
a8d50 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  */.  char *zFile
a8d60 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  name;           
a8d70 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
a8d80 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
a8d90 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61  .  char *zJourna
a8da0 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
a8db0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  * Name of the jo
a8dc0 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20  urnal file */.  
a8dd0 63 68 61 72 20 2a 7a 44 69 72 65 63 74 6f 72 79  char *zDirectory
a8de0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  ;           /* D
a8df0 69 72 65 63 74 6f 72 79 20 68 6f 6c 64 20 64 61  irectory hold da
a8e00 74 61 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e  tabase and journ
a8e10 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 63 68  al files */.  ch
a8e20 61 72 20 2a 7a 53 74 6d 74 4a 72 6e 6c 3b 20 20  ar *zStmtJrnl;  
a8e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
a8e40 65 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  e of the stateme
a8e50 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  nt journal file 
a8e60 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
a8e70 65 20 2a 66 64 2c 20 2a 6a 66 64 3b 20 20 20 20  e *fd, *jfd;    
a8e80 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70   /* File descrip
a8e90 74 6f 72 73 20 66 6f 72 20 64 61 74 61 62 61 73  tors for databas
a8ea0 65 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 2a 2f  e and journal */
a8eb0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
a8ec0 2a 73 74 66 64 3b 20 20 20 20 20 20 20 20 20 2f  *stfd;         /
a8ed0 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * File descripto
a8ee0 72 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d  r for the statem
a8ef0 65 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 2a 2f  ent subjournal*/
a8f00 0a 20 20 42 75 73 79 48 61 6e 64 6c 65 72 20 2a  .  BusyHandler *
a8f10 70 42 75 73 79 48 61 6e 64 6c 65 72 3b 20 20 2f  pBusyHandler;  /
a8f20 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 71 6c  * Pointer to sql
a8f30 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 20  ite.busyHandler 
a8f40 2a 2f 0a 20 20 50 61 67 65 72 4c 72 75 4c 69 73  */.  PagerLruLis
a8f50 74 20 6c 72 75 3b 20 20 20 20 20 20 20 20 20 20  t lru;          
a8f60 20 2f 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20   /* LRU list of 
a8f70 66 72 65 65 20 70 61 67 65 73 20 2a 2f 0a 20 20  free pages */.  
a8f80 50 67 48 64 72 20 2a 70 41 6c 6c 3b 20 20 20 20  PgHdr *pAll;    
a8f90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
a8fa0 69 73 74 20 6f 66 20 61 6c 6c 20 70 61 67 65 73  ist of all pages
a8fb0 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 53 74   */.  PgHdr *pSt
a8fc0 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mt;             
a8fd0 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 70 61 67    /* List of pag
a8fe0 65 73 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d  es in the statem
a8ff0 65 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 20 2a  ent subjournal *
a9000 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74  /.  PgHdr *pDirt
a9010 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
a9020 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 64  /* List of all d
a9030 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20  irty pages */.  
a9040 69 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20  i64 journalOff; 
a9050 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
a9060 75 72 72 65 6e 74 20 62 79 74 65 20 6f 66 66 73  urrent byte offs
a9070 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  et in the journa
a9080 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20  l file */.  i64 
a9090 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 20  journalHdr;     
a90a0 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20          /* Byte 
a90b0 6f 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 6f  offset to previo
a90c0 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  us journal heade
a90d0 72 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74 48  r */.  i64 stmtH
a90e0 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20  drOff;          
a90f0 20 20 20 2f 2a 20 46 69 72 73 74 20 6a 6f 75 72     /* First jour
a9100 6e 61 6c 20 68 65 61 64 65 72 20 77 72 69 74 74  nal header writt
a9110 65 6e 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e  en this statemen
a9120 74 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74 43  t */.  i64 stmtC
a9130 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  ksum;           
a9140 20 20 20 2f 2a 20 63 6b 73 75 6d 49 6e 69 74 20     /* cksumInit 
a9150 77 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 77  when statement w
a9160 61 73 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 20  as started */.  
a9170 69 36 34 20 73 74 6d 74 4a 53 69 7a 65 3b 20 20  i64 stmtJSize;  
a9180 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
a9190 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 61  ize of journal a
a91a0 74 20 73 74 6d 74 5f 62 65 67 69 6e 28 29 20 2a  t stmt_begin() *
a91b0 2f 0a 20 20 69 6e 74 20 73 65 63 74 6f 72 53 69  /.  int sectorSi
a91c0 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
a91d0 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f  /* Assumed secto
a91e0 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f  r size during ro
a91f0 6c 6c 62 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66  llback */.#ifdef
a9200 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
a9210 6e 74 20 6e 48 69 74 2c 20 6e 4d 69 73 73 3b 20  nt nHit, nMiss; 
a9220 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61             /* Ca
a9230 63 68 65 20 68 69 74 73 20 61 6e 64 20 6d 69 73  che hits and mis
a9240 73 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  sing */.  int nR
a9250 65 61 64 2c 20 6e 57 72 69 74 65 3b 20 20 20 20  ead, nWrite;    
a9260 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
a9270 65 20 70 61 67 65 73 20 72 65 61 64 2f 77 72 69  e pages read/wri
a9280 74 74 65 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  tten */.#endif. 
a9290 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 75 63   void (*xDestruc
a92a0 74 6f 72 29 28 44 62 50 61 67 65 2a 2c 69 6e 74  tor)(DbPage*,int
a92b0 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20  ); /* Call this 
a92c0 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 66 72 65  routine when fre
a92d0 65 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 20 20  eing pages */.  
a92e0 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 65 72  void (*xReiniter
a92f0 29 28 44 62 50 61 67 65 2a 2c 69 6e 74 29 3b 20  )(DbPage*,int); 
a9300 20 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 72    /* Call this r
a9310 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f  outine when relo
a9320 61 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23  ading pages */.#
a9330 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
a9340 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a 28  _CODEC.  void *(
a9350 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76  *xCodec)(void*,v
a9360 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20  oid*,Pgno,int); 
a9370 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65  /* Routine for e
a9380 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20  n/decoding data 
a9390 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65  */.  void *pCode
a93a0 63 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  cArg;           
a93b0 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
a93c0 6e 74 20 74 6f 20 78 43 6f 64 65 63 28 29 20 2a  nt to xCodec() *
a93d0 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e  /.#endif.  int n
a93e0 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20  Hash;           
a93f0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
a9400 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 68  f the pager hash
a9410 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 48 64   table */.  PgHd
a9420 72 20 2a 2a 61 48 61 73 68 3b 20 20 20 20 20 20  r **aHash;      
a9430 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
a9440 74 61 62 6c 65 20 74 6f 20 6d 61 70 20 70 61 67  table to map pag
a9450 65 20 6e 75 6d 62 65 72 20 74 6f 20 50 67 48 64  e number to PgHd
a9460 72 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  r */.#ifdef SQLI
a9470 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  TE_ENABLE_MEMORY
a9480 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 50 61  _MANAGEMENT.  Pa
a9490 67 65 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  ger *pNext;     
a94a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 75            /* Dou
a94b0 62 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  bly linked list 
a94c0 6f 66 20 70 61 67 65 72 73 20 6f 6e 20 77 68 69  of pagers on whi
a94d0 63 68 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70  ch */.  Pager *p
a94e0 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20  Prev;           
a94f0 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72      /* sqlite3_r
a9500 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
a9510 77 69 6c 6c 20 77 6f 72 6b 20 2a 2f 0a 20 20 69  will work */.  i
a9520 6e 74 20 69 49 6e 55 73 65 4d 4d 3b 20 20 20 20  nt iInUseMM;    
a9530 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f             /* No
a9540 6e 2d 7a 65 72 6f 20 69 66 20 75 6e 61 76 61 69  n-zero if unavai
a9550 6c 61 62 6c 65 20 74 6f 20 4d 4d 20 2a 2f 0a 20  lable to MM */. 
a9560 20 69 6e 74 20 69 49 6e 55 73 65 44 42 3b 20 20   int iInUseDB;  
a9570 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a9580 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 69 6e 20 73  Non-zero if in s
a9590 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
a95a0 65 6d 6f 72 79 28 29 20 2a 2f 0a 23 65 6e 64 69  emory() */.#endi
a95b0 66 0a 20 20 63 68 61 72 20 2a 70 54 6d 70 53 70  f.  char *pTmpSp
a95c0 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ace;            
a95d0 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a  /* Pager.pageSiz
a95e0 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  e bytes of space
a95f0 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a   for tmp use */.
a9600 20 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 72    char dbFileVer
a9610 73 5b 31 36 5d 3b 20 20 20 20 20 20 20 20 2f 2a  s[16];        /*
a9620 20 43 68 61 6e 67 65 73 20 77 68 65 6e 65 76 65   Changes wheneve
a9630 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  r database file 
a9640 63 68 61 6e 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f  changes */.};../
a9650 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
a9660 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  ng global variab
a9670 6c 65 73 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72  les hold counter
a9680 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65  s used for.** te
a9690 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f  sting purposes o
a96a0 6e 6c 79 2e 20 20 54 68 65 73 65 20 76 61 72 69  nly.  These vari
a96b0 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 65 78 69  ables do not exi
a96c0 73 74 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74  st in.** a non-t
a96d0 65 73 74 69 6e 67 20 62 75 69 6c 64 2e 20 20 54  esting build.  T
a96e0 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 61  hese variables a
a96f0 72 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61  re not thread-sa
a9700 66 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  fe..*/.#ifdef SQ
a9710 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
a9720 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
a9730 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f  _pager_readdb_co
a9740 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e  unt = 0;    /* N
a9750 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61  umber of full pa
a9760 67 65 73 20 72 65 61 64 20 66 72 6f 6d 20 44 42  ges read from DB
a9770 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
a9780 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72  nt sqlite3_pager
a9790 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d  _writedb_count =
a97a0 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20   0;   /* Number 
a97b0 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 77 72  of full pages wr
a97c0 69 74 74 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53  itten to DB */.S
a97d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
a97e0 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74  lite3_pager_writ
a97f0 65 6a 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20  ej_count = 0;   
a9800 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
a9810 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6a  ges written to j
a9820 6f 75 72 6e 61 6c 20 2a 2f 0a 53 51 4c 49 54 45  ournal */.SQLITE
a9830 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
a9840 5f 70 61 67 65 72 5f 70 67 66 72 65 65 5f 63 6f  _pager_pgfree_co
a9850 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e  unt = 0;    /* N
a9860 75 6d 62 65 72 20 6f 66 20 63 61 63 68 65 20 70  umber of cache p
a9870 61 67 65 73 20 66 72 65 65 64 20 2a 2f 0a 23 20  ages freed */.# 
a9880 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43  define PAGER_INC
a9890 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73 65 0a  R(v)  v++.#else.
a98a0 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49  # define PAGER_I
a98b0 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a 0a 2f  NCR(v).#endif../
a98c0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
a98d0 6e 67 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e  ng variable poin
a98e0 74 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f  ts to the head o
a98f0 66 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65  f a double-linke
a9900 64 20 6c 69 73 74 0a 2a 2a 20 6f 66 20 61 6c 6c  d list.** of all
a9910 20 70 61 67 65 72 73 20 74 68 61 74 20 61 72 65   pagers that are
a9920 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61   eligible for pa
a9930 67 65 20 73 74 65 61 6c 69 6e 67 20 62 79 20 74  ge stealing by t
a9940 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65  he.** sqlite3_re
a9950 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69  lease_memory() i
a9960 6e 74 65 72 66 61 63 65 2e 20 20 41 63 63 65 73  nterface.  Acces
a9970 73 20 74 6f 20 74 68 69 73 20 6c 69 73 74 20 69  s to this list i
a9980 73 0a 2a 2a 20 70 72 6f 74 65 63 74 65 64 20 62  s.** protected b
a9990 79 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54  y the SQLITE_MUT
a99a0 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 20 6d  EX_STATIC_MEM2 m
a99b0 75 74 65 78 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  utex..*/.#ifdef 
a99c0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
a99d0 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
a99e0 73 74 61 74 69 63 20 50 61 67 65 72 20 2a 73 71  static Pager *sq
a99f0 6c 69 74 65 33 50 61 67 65 72 4c 69 73 74 20 3d  lite3PagerList =
a9a00 20 30 3b 0a 73 74 61 74 69 63 20 50 61 67 65 72   0;.static Pager
a9a10 4c 72 75 4c 69 73 74 20 73 71 6c 69 74 65 33 4c  LruList sqlite3L
a9a20 72 75 50 61 67 65 4c 69 73 74 20 3d 20 7b 30 2c  ruPageList = {0,
a9a30 20 30 2c 20 30 7d 3b 0a 23 65 6e 64 69 66 0a 0a   0, 0};.#endif..
a9a40 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66  ./*.** Journal f
a9a50 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74 68 20  iles begin with 
a9a60 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
a9a70 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54 68 65  gic string.  The
a9a80 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f 62 74   data.** was obt
a9a90 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65 76 2f  ained from /dev/
a9aa0 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73 20 75  random.  It is u
a9ab0 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20 73 61  sed only as a sa
a9ac0 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a  nity check..**.*
a9ad0 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20  * Since version 
a9ae0 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75 72 6e  2.8.0, the journ
a9af0 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74 61 69  al format contai
a9b00 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 61  ns additional sa
a9b10 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69 6e 67  nity.** checking
a9b20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49   information.  I
a9b30 66 20 74 68 65 20 70 6f 77 65 72 20 66 61 69 6c  f the power fail
a9b40 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72  s while the jour
a9b50 6e 61 6c 20 69 73 20 62 65 67 69 6e 0a 2a 2a 20  nal is begin.** 
a9b60 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d 72 61  written, semi-ra
a9b70 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64 61 74  ndom garbage dat
a9b80 61 20 6d 69 67 68 74 20 61 70 70 65 61 72 20 69  a might appear i
a9b90 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  n the journal.**
a9ba0 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f 77 65   file after powe
a9bb0 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e 20 20  r is restored.  
a9bc0 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  If an attempt is
a9bd0 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f   then made.** to
a9be0 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 6e 61   roll the journa
a9bf0 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61  l back, the data
a9c00 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20 63 6f  base could be co
a9c10 72 72 75 70 74 65 64 2e 20 20 54 68 65 20 61 64  rrupted.  The ad
a9c20 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69  ditional.** sani
a9c30 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61 74 61  ty checking data
a9c40 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74   is an attempt t
a9c50 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 67  o discover the g
a9c60 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a  arbage in the.**
a9c70 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 67 6e   journal and ign
a9c80 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ore it..**.** Th
a9c90 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  e sanity checkin
a9ca0 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  g information fo
a9cb0 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72 6e 61  r the new journa
a9cc0 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69 73 74  l format consist
a9cd0 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69 74  s.** of a 32-bit
a9ce0 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61 63   checksum on eac
a9cf0 68 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 20  h page of data. 
a9d00 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20 63 6f   The checksum co
a9d10 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74 68 65  vers both.** the
a9d20 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64   page number and
a9d30 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 61 67   the pPager->pag
a9d40 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64  eSize bytes of d
a9d50 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 65  ata for the page
a9d60 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d 20  ..** This cksum 
a9d70 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
a9d80 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e 64 6f  o a 32-bit rando
a9d90 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 70 70  m value that app
a9da0 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6a  ears in the.** j
a9db0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 67 68  ournal file righ
a9dc0 74 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64  t after the head
a9dd0 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d 20  er.  The random 
a9de0 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 20 69  initializer is i
a9df0 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 65 63  mportant,.** bec
a9e00 61 75 73 65 20 67 61 72 62 61 67 65 20 64 61 74  ause garbage dat
a9e10 61 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  a that appears a
a9e20 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a  t the end of a j
a9e30 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65 6c 79  ournal is likely
a9e40 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20 77 61  .** data that wa
a9e50 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65 72 20  s once in other 
a9e60 66 69 6c 65 73 20 74 68 61 74 20 68 61 76 65 20  files that have 
a9e70 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74 65 64  now been deleted
a9e80 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67 61 72  .  If the.** gar
a9e90 62 61 67 65 20 64 61 74 61 20 63 61 6d 65 20 66  bage data came f
a9ea0 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 65 20  rom an obsolete 
a9eb0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68  journal file, th
a9ec0 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69 67 68  e checksums migh
a9ed0 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63 74 2e  t.** be correct.
a9ee0 20 20 42 75 74 20 62 79 20 69 6e 69 74 69 61 6c    But by initial
a9ef0 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63 6b 73  izing the checks
a9f00 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76 61 6c  um to random val
a9f10 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 64  ue which.** is d
a9f20 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 76 65  ifferent for eve
a9f30 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 20 6d  ry journal, we m
a9f40 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72 69 73  inimize that ris
a9f50 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  k..*/.static con
a9f60 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
a9f70 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d   aJournalMagic[]
a9f80 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30 78 64   = {.  0xd9, 0xd
a9f90 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c 20 30  5, 0x05, 0xf9, 0
a9fa0 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36 33 2c  x20, 0xa1, 0x63,
a9fb0 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a   0xd7,.};../*.**
a9fc0 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   The size of the
a9fd0 20 68 65 61 64 65 72 20 61 6e 64 20 6f 66 20 65   header and of e
a9fe0 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 20  ach page in the 
a9ff0 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65 74 65 72  journal is deter
aa000 6d 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  mined.** by the 
aa010 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73  following macros
aa020 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55  ..*/.#define JOU
aa030 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
aa040 72 29 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61  r)  ((pPager->pa
aa050 67 65 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a  geSize) + 8)../*
aa060 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
aa070 68 65 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20  header size for 
aa080 74 68 69 73 20 70 61 67 65 72 2e 20 49 6e 20 74  this pager. In t
aa090 68 65 20 66 75 74 75 72 65 2c 20 74 68 69 73 20  he future, this 
aa0a0 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 73 65 74 20  could be.** set 
aa0b0 74 6f 20 73 6f 6d 65 20 76 61 6c 75 65 20 72 65  to some value re
aa0c0 61 64 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b  ad from the disk
aa0d0 20 63 6f 6e 74 72 6f 6c 6c 65 72 2e 20 54 68 65   controller. The
aa0e0 20 69 6d 70 6f 72 74 61 6e 74 0a 2a 2a 20 63 68   important.** ch
aa0f0 61 72 61 63 74 65 72 69 73 74 69 63 20 69 73 20  aracteristic is 
aa100 74 68 61 74 20 69 74 20 69 73 20 74 68 65 20 73  that it is the s
aa110 61 6d 65 20 73 69 7a 65 20 61 73 20 61 20 64 69  ame size as a di
aa120 73 6b 20 73 65 63 74 6f 72 2e 0a 2a 2f 0a 23 64  sk sector..*/.#d
aa130 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 44  efine JOURNAL_HD
aa140 52 5f 53 5a 28 70 50 61 67 65 72 29 20 28 70 50  R_SZ(pPager) (pP
aa150 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
aa160 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63  )../*.** The mac
aa170 72 6f 20 4d 45 4d 44 42 20 69 73 20 74 72 75 65  ro MEMDB is true
aa180 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69   if we are deali
aa190 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 2d 6d 65  ng with an in-me
aa1a0 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a  mory database..*
aa1b0 2a 20 57 65 20 64 6f 20 74 68 69 73 20 61 73 20  * We do this as 
aa1c0 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20  a macro so that 
aa1d0 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d  if the SQLITE_OM
aa1e0 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d 61 63 72  IT_MEMORYDB macr
aa1f0 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65  o is set,.** the
aa200 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d 44 42 20   value of MEMDB 
aa210 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e 73 74 61  will be a consta
aa220 6e 74 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69  nt and the compi
aa230 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69 6d 69 7a  ler will optimiz
aa240 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65 20 74 68  e.** out code th
aa250 61 74 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 65  at would never e
aa260 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 65  xecute..*/.#ifde
aa270 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45  f SQLITE_OMIT_ME
aa280 4d 4f 52 59 44 42 0a 23 20 64 65 66 69 6e 65 20  MORYDB.# define 
aa290 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65 0a 23 20  MEMDB 0.#else.# 
aa2a0 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 70 50 61  define MEMDB pPa
aa2b0 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 69  ger->memDb.#endi
aa2c0 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 6e 75  f../*.** Page nu
aa2d0 6d 62 65 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47  mber PAGER_MJ_PG
aa2e0 4e 4f 20 69 73 20 6e 65 76 65 72 20 75 73 65 64  NO is never used
aa2f0 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 61   in an SQLite da
aa300 74 61 62 61 73 65 20 28 69 74 20 69 73 0a 2a 2a  tabase (it is.**
aa310 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 77 6f   reserved for wo
aa320 72 6b 69 6e 67 20 61 72 6f 75 6e 64 20 61 20 77  rking around a w
aa330 69 6e 64 6f 77 73 2f 70 6f 73 69 78 20 69 6e 63  indows/posix inc
aa340 6f 6d 70 61 74 69 62 69 6c 69 74 79 29 2e 20 49  ompatibility). I
aa350 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20  t is.** used in 
aa360 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73  the journal to s
aa370 69 67 6e 69 66 79 20 74 68 61 74 20 74 68 65 20  ignify that the 
aa380 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65  remainder of the
aa390 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a   journal file .*
aa3a0 2a 20 69 73 20 64 65 76 6f 74 65 64 20 74 6f 20  * is devoted to 
aa3b0 73 74 6f 72 69 6e 67 20 61 20 6d 61 73 74 65 72  storing a master
aa3c0 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2d 20   journal name - 
aa3d0 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72  there are no mor
aa3e0 65 20 70 61 67 65 73 20 74 6f 0a 2a 2a 20 72 6f  e pages to.** ro
aa3f0 6c 6c 20 62 61 63 6b 2e 20 53 65 65 20 63 6f 6d  ll back. See com
aa400 6d 65 6e 74 73 20 66 6f 72 20 66 75 6e 63 74 69  ments for functi
aa410 6f 6e 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f  on writeMasterJo
aa420 75 72 6e 61 6c 28 29 20 66 6f 72 20 64 65 74 61  urnal() for deta
aa430 69 6c 73 2e 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69  ils..*/./* #defi
aa440 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  ne PAGER_MJ_PGNO
aa450 28 78 29 20 28 50 45 4e 44 49 4e 47 5f 42 59 54  (x) (PENDING_BYT
aa460 45 2f 28 28 78 29 2d 3e 70 61 67 65 53 69 7a 65  E/((x)->pageSize
aa470 29 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  )) */.#define PA
aa480 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 78 29 20 28  GER_MJ_PGNO(x) (
aa490 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28  (PENDING_BYTE/((
aa4a0 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31  x)->pageSize))+1
aa4b0 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  )../*.** The max
aa4c0 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 20  imum legal page 
aa4d0 6e 75 6d 62 65 72 20 69 73 20 28 32 5e 33 31 20  number is (2^31 
aa4e0 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  - 1)..*/.#define
aa4f0 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20   PAGER_MAX_PGNO 
aa500 32 31 34 37 34 38 33 36 34 37 0a 0a 2f 2a 0a 2a  2147483647../*.*
aa510 2a 20 54 68 65 20 70 61 67 65 72 45 6e 74 65 72  * The pagerEnter
aa520 28 29 20 61 6e 64 20 70 61 67 65 72 4c 65 61 76  () and pagerLeav
aa530 65 28 29 20 72 6f 75 74 69 6e 65 73 20 61 63 71  e() routines acq
aa540 75 69 72 65 20 61 6e 64 20 72 65 6c 65 61 73 65  uire and release
aa550 0a 2a 2a 20 61 20 6d 75 74 65 78 20 6f 6e 20 65  .** a mutex on e
aa560 61 63 68 20 70 61 67 65 72 2e 20 20 54 68 65 20  ach pager.  The 
aa570 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69  mutex is recursi
aa580 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ve..**.** This i
aa590 73 20 61 20 73 70 65 63 69 61 6c 2d 70 75 72 70  s a special-purp
aa5a0 6f 73 65 20 6d 75 74 65 78 2e 20 20 49 74 20 6f  ose mutex.  It o
aa5b0 6e 6c 79 20 70 72 6f 76 69 64 65 73 20 6d 75 74  nly provides mut
aa5c0 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 0a 2a 2a  ual exclusion.**
aa5d0 20 62 65 74 77 65 65 6e 20 74 68 65 20 42 74 72   between the Btr
aa5e0 65 65 20 61 6e 64 20 74 68 65 20 4d 65 6d 6f 72  ee and the Memor
aa5f0 79 20 4d 61 6e 61 67 65 6d 65 6e 74 20 73 71 6c  y Management sql
aa600 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
aa610 6f 72 79 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f  ory().** functio
aa620 6e 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  n.  It does not 
aa630 70 72 65 76 65 6e 74 2c 20 66 6f 72 20 65 78 61  prevent, for exa
aa640 6d 70 6c 65 2c 20 74 77 6f 20 42 74 72 65 65 73  mple, two Btrees
aa650 20 66 72 6f 6d 20 61 63 63 65 73 73 69 6e 67 0a   from accessing.
aa660 2a 2a 20 74 68 65 20 73 61 6d 65 20 70 61 67 65  ** the same page
aa670 72 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  r at the same ti
aa680 6d 65 2e 20 20 4f 74 68 65 72 20 67 65 6e 65 72  me.  Other gener
aa690 61 6c 2d 70 75 72 70 6f 73 65 20 6d 75 74 65 78  al-purpose mutex
aa6a0 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 62 74 72  es in.** the btr
aa6b0 65 65 20 6c 61 79 65 72 20 68 61 6e 64 6c 65 20  ee layer handle 
aa6c0 74 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2f 0a 23  that chore..*/.#
aa6d0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
aa6e0 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
aa6f0 45 4d 45 4e 54 0a 20 20 73 74 61 74 69 63 20 76  EMENT.  static v
aa700 6f 69 64 20 70 61 67 65 72 45 6e 74 65 72 28 50  oid pagerEnter(P
aa710 61 67 65 72 20 2a 70 29 7b 0a 20 20 20 20 70 2d  ager *p){.    p-
aa720 3e 69 49 6e 55 73 65 44 42 2b 2b 3b 0a 20 20 20  >iInUseDB++;.   
aa730 20 69 66 28 20 70 2d 3e 69 49 6e 55 73 65 4d 4d   if( p->iInUseMM
aa740 20 26 26 20 70 2d 3e 69 49 6e 55 73 65 44 42 3d   && p->iInUseDB=
aa750 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =1 ){.      sqli
aa760 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
aa770 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 20 3d 20  ;.      mutex = 
aa780 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
aa790 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
aa7a0 5f 53 54 41 54 49 43 5f 4d 45 4d 32 29 3b 0a 20  _STATIC_MEM2);. 
aa7b0 20 20 20 20 20 70 2d 3e 69 49 6e 55 73 65 44 42       p->iInUseDB
aa7c0 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 0;.      sqli
aa7d0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
aa7e0 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 2d  mutex);.      p-
aa7f0 3e 69 49 6e 55 73 65 44 42 20 3d 20 31 3b 0a 20  >iInUseDB = 1;. 
aa800 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
aa810 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b  ex_leave(mutex);
aa820 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
aa830 74 28 20 70 2d 3e 69 49 6e 55 73 65 4d 4d 3d 3d  t( p->iInUseMM==
aa840 30 20 29 3b 0a 20 20 7d 0a 20 20 73 74 61 74 69  0 );.  }.  stati
aa850 63 20 76 6f 69 64 20 70 61 67 65 72 4c 65 61 76  c void pagerLeav
aa860 65 28 50 61 67 65 72 20 2a 70 29 7b 0a 20 20 20  e(Pager *p){.   
aa870 20 70 2d 3e 69 49 6e 55 73 65 44 42 2d 2d 3b 0a   p->iInUseDB--;.
aa880 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69      assert( p->i
aa890 49 6e 55 73 65 44 42 3e 3d 30 20 29 3b 0a 20 20  InUseDB>=0 );.  
aa8a0 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
aa8b0 20 70 61 67 65 72 45 6e 74 65 72 28 58 29 0a 23   pagerEnter(X).#
aa8c0 20 64 65 66 69 6e 65 20 70 61 67 65 72 4c 65 61   define pagerLea
aa8d0 76 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ve(X).#endif../*
aa8e0 0a 2a 2a 20 45 6e 61 62 6c 65 20 72 65 66 65 72  .** Enable refer
aa8f0 65 6e 63 65 20 63 6f 75 6e 74 20 74 72 61 63 6b  ence count track
aa900 69 6e 67 20 28 66 6f 72 20 64 65 62 75 67 67 69  ing (for debuggi
aa910 6e 67 29 20 68 65 72 65 3a 0a 2a 2f 0a 23 69 66  ng) here:.*/.#if
aa920 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
aa930 0a 20 20 69 6e 74 20 70 61 67 65 72 33 5f 72 65  .  int pager3_re
aa940 66 69 6e 66 6f 5f 65 6e 61 62 6c 65 20 3d 20 30  finfo_enable = 0
aa950 3b 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20  ;.  static void 
aa960 70 61 67 65 72 5f 72 65 66 69 6e 66 6f 28 50 67  pager_refinfo(Pg
aa970 48 64 72 20 2a 70 29 7b 0a 20 20 20 20 73 74 61  Hdr *p){.    sta
aa980 74 69 63 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b  tic int cnt = 0;
aa990 0a 20 20 20 20 69 66 28 20 21 70 61 67 65 72 33  .    if( !pager3
aa9a0 5f 72 65 66 69 6e 66 6f 5f 65 6e 61 62 6c 65 20  _refinfo_enable 
aa9b0 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 73 71  ) return;.    sq
aa9c0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
aa9d0 28 0a 20 20 20 20 20 20 20 22 52 45 46 43 4e 54  (.       "REFCNT
aa9e0 3a 20 25 34 64 20 61 64 64 72 3d 25 70 20 6e 52  : %4d addr=%p nR
aa9f0 65 66 3d 25 2d 33 64 20 74 6f 74 61 6c 3d 25 64  ef=%-3d total=%d
aaa00 5c 6e 22 2c 0a 20 20 20 20 20 20 20 70 2d 3e 70  \n",.       p->p
aaa10 67 6e 6f 2c 20 50 47 48 44 52 5f 54 4f 5f 44 41  gno, PGHDR_TO_DA
aaa20 54 41 28 70 29 2c 20 70 2d 3e 6e 52 65 66 2c 20  TA(p), p->nRef, 
aaa30 70 2d 3e 70 50 61 67 65 72 2d 3e 6e 52 65 66 0a  p->pPager->nRef.
aaa40 20 20 20 20 29 3b 0a 20 20 20 20 63 6e 74 2b 2b      );.    cnt++
aaa50 3b 20 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e 67  ;   /* Something
aaa60 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70   to set a breakp
aaa70 6f 69 6e 74 20 6f 6e 20 2a 2f 0a 20 20 7d 0a 23  oint on */.  }.#
aaa80 20 64 65 66 69 6e 65 20 52 45 46 49 4e 46 4f 28   define REFINFO(
aaa90 58 29 20 20 70 61 67 65 72 5f 72 65 66 69 6e 66  X)  pager_refinf
aaaa0 6f 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  o(X).#else.# def
aaab0 69 6e 65 20 52 45 46 49 4e 46 4f 28 58 29 0a 23  ine REFINFO(X).#
aaac0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  endif../*.** Add
aaad0 20 70 61 67 65 20 70 50 67 20 74 6f 20 74 68 65   page pPg to the
aaae0 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 69 6e 6b   end of the link
aaaf0 65 64 20 6c 69 73 74 20 6d 61 6e 61 67 65 64 20  ed list managed 
aab00 62 79 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  by structure.** 
aab10 70 4c 69 73 74 20 28 70 50 67 20 62 65 63 6f 6d  pList (pPg becom
aab20 65 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  es the last entr
aab30 79 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2d 20  y in the list - 
aab40 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
aab50 79 20 0a 2a 2a 20 75 73 65 64 29 2e 20 41 72 67  y .** used). Arg
aab60 75 6d 65 6e 74 20 70 4c 69 6e 6b 20 73 68 6f 75  ument pLink shou
aab70 6c 64 20 70 6f 69 6e 74 20 74 6f 20 65 69 74 68  ld point to eith
aab80 65 72 20 70 50 67 2d 3e 66 72 65 65 20 6f 72 20  er pPg->free or 
aab90 70 50 67 2d 3e 67 66 72 65 65 2c 0a 2a 2a 20 64  pPg->gfree,.** d
aaba0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74  epending on whet
aabb0 68 65 72 20 70 50 67 20 69 73 20 62 65 69 6e 67  her pPg is being
aabc0 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 61   added to the pa
aabd0 67 65 72 2d 73 70 65 63 69 66 69 63 20 6f 72 0a  ger-specific or.
aabe0 2a 2a 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69  ** global LRU li
aabf0 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  st..*/.static vo
aac00 69 64 20 6c 69 73 74 41 64 64 28 50 61 67 65 72  id listAdd(Pager
aac10 4c 72 75 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  LruList *pList, 
aac20 50 61 67 65 72 4c 72 75 4c 69 6e 6b 20 2a 70 4c  PagerLruLink *pL
aac30 69 6e 6b 2c 20 50 67 48 64 72 20 2a 70 50 67 29  ink, PgHdr *pPg)
aac40 7b 0a 20 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74  {.  pLink->pNext
aac50 20 3d 20 30 3b 0a 20 20 70 4c 69 6e 6b 2d 3e 70   = 0;.  pLink->p
aac60 50 72 65 76 20 3d 20 70 4c 69 73 74 2d 3e 70 4c  Prev = pList->pL
aac70 61 73 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  ast;..#ifdef SQL
aac80 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
aac90 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 61  Y_MANAGEMENT.  a
aaca0 73 73 65 72 74 28 70 4c 69 6e 6b 3d 3d 26 70 50  ssert(pLink==&pP
aacb0 67 2d 3e 66 72 65 65 20 7c 7c 20 70 4c 69 6e 6b  g->free || pLink
aacc0 3d 3d 26 70 50 67 2d 3e 67 66 72 65 65 29 3b 0a  ==&pPg->gfree);.
aacd0 20 20 61 73 73 65 72 74 28 70 4c 69 6e 6b 3d 3d    assert(pLink==
aace0 26 70 50 67 2d 3e 67 66 72 65 65 20 7c 7c 20 70  &pPg->gfree || p
aacf0 4c 69 73 74 21 3d 26 73 71 6c 69 74 65 33 4c 72  List!=&sqlite3Lr
aad00 75 50 61 67 65 4c 69 73 74 29 3b 0a 23 65 6e 64  uPageList);.#end
aad10 69 66 0a 0a 20 20 69 66 28 20 70 4c 69 73 74 2d  if..  if( pList-
aad20 3e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 69 6e  >pLast ){.    in
aad30 74 20 69 4f 66 66 20 3d 20 28 63 68 61 72 20 2a  t iOff = (char *
aad40 29 70 4c 69 6e 6b 20 2d 20 28 63 68 61 72 20 2a  )pLink - (char *
aad50 29 70 50 67 3b 0a 20 20 20 20 50 61 67 65 72 4c  )pPg;.    PagerL
aad60 72 75 4c 69 6e 6b 20 2a 70 4c 61 73 74 4c 69 6e  ruLink *pLastLin
aad70 6b 20 3d 20 28 50 61 67 65 72 4c 72 75 4c 69 6e  k = (PagerLruLin
aad80 6b 20 2a 29 28 26 28 28 75 38 20 2a 29 70 4c 69  k *)(&((u8 *)pLi
aad90 73 74 2d 3e 70 4c 61 73 74 29 5b 69 4f 66 66 5d  st->pLast)[iOff]
aada0 29 3b 0a 20 20 20 20 70 4c 61 73 74 4c 69 6e 6b  );.    pLastLink
aadb0 2d 3e 70 4e 65 78 74 20 3d 20 70 50 67 3b 0a 20  ->pNext = pPg;. 
aadc0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
aadd0 72 74 28 21 70 4c 69 73 74 2d 3e 70 46 69 72 73  rt(!pList->pFirs
aade0 74 29 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70  t);.    pList->p
aadf0 46 69 72 73 74 20 3d 20 70 50 67 3b 0a 20 20 7d  First = pPg;.  }
aae00 0a 0a 20 20 70 4c 69 73 74 2d 3e 70 4c 61 73 74  ..  pList->pLast
aae10 20 3d 20 70 50 67 3b 0a 20 20 69 66 28 20 21 70   = pPg;.  if( !p
aae20 4c 69 73 74 2d 3e 70 46 69 72 73 74 53 79 6e 63  List->pFirstSync
aae30 65 64 20 26 26 20 70 50 67 2d 3e 6e 65 65 64 53  ed && pPg->needS
aae40 79 6e 63 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c  ync==0 ){.    pL
aae50 69 73 74 2d 3e 70 46 69 72 73 74 53 79 6e 63 65  ist->pFirstSynce
aae60 64 20 3d 20 70 50 67 3b 0a 20 20 7d 0a 7d 0a 0a  d = pPg;.  }.}..
aae70 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 50 67  /*.** Remove pPg
aae80 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6d   from the list m
aae90 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 73 74  anaged by the st
aaea0 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20  ructure pointed 
aaeb0 74 6f 20 62 79 20 70 4c 69 73 74 2e 0a 2a 2a 0a  to by pList..**.
aaec0 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 4c 69 6e  ** Argument pLin
aaed0 6b 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74  k should point t
aaee0 6f 20 65 69 74 68 65 72 20 70 50 67 2d 3e 66 72  o either pPg->fr
aaef0 65 65 20 6f 72 20 70 50 67 2d 3e 67 66 72 65 65  ee or pPg->gfree
aaf00 2c 20 64 65 70 65 6e 64 69 6e 67 20 0a 2a 2a 20  , depending .** 
aaf10 6f 6e 20 77 68 65 74 68 65 72 20 70 50 67 20 69  on whether pPg i
aaf20 73 20 62 65 69 6e 67 20 61 64 64 65 64 20 74 6f  s being added to
aaf30 20 74 68 65 20 70 61 67 65 72 2d 73 70 65 63 69   the pager-speci
aaf40 66 69 63 20 6f 72 20 67 6c 6f 62 61 6c 20 4c 52  fic or global LR
aaf50 55 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  U list..*/.stati
aaf60 63 20 76 6f 69 64 20 6c 69 73 74 52 65 6d 6f 76  c void listRemov
aaf70 65 28 50 61 67 65 72 4c 72 75 4c 69 73 74 20 2a  e(PagerLruList *
aaf80 70 4c 69 73 74 2c 20 50 61 67 65 72 4c 72 75 4c  pList, PagerLruL
aaf90 69 6e 6b 20 2a 70 4c 69 6e 6b 2c 20 50 67 48 64  ink *pLink, PgHd
aafa0 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 20 69  r *pPg){.  int i
aafb0 4f 66 66 20 3d 20 28 63 68 61 72 20 2a 29 70 4c  Off = (char *)pL
aafc0 69 6e 6b 20 2d 20 28 63 68 61 72 20 2a 29 70 50  ink - (char *)pP
aafd0 67 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  g;..#ifdef SQLIT
aafe0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
aaff0 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 61 73 73  MANAGEMENT.  ass
ab000 65 72 74 28 70 4c 69 6e 6b 3d 3d 26 70 50 67 2d  ert(pLink==&pPg-
ab010 3e 66 72 65 65 20 7c 7c 20 70 4c 69 6e 6b 3d 3d  >free || pLink==
ab020 26 70 50 67 2d 3e 67 66 72 65 65 29 3b 0a 20 20  &pPg->gfree);.  
ab030 61 73 73 65 72 74 28 70 4c 69 6e 6b 3d 3d 26 70  assert(pLink==&p
ab040 50 67 2d 3e 67 66 72 65 65 20 7c 7c 20 70 4c 69  Pg->gfree || pLi
ab050 73 74 21 3d 26 73 71 6c 69 74 65 33 4c 72 75 50  st!=&sqlite3LruP
ab060 61 67 65 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66  ageList);.#endif
ab070 0a 0a 20 20 69 66 28 20 70 50 67 3d 3d 70 4c 69  ..  if( pPg==pLi
ab080 73 74 2d 3e 70 46 69 72 73 74 20 29 7b 0a 20 20  st->pFirst ){.  
ab090 20 20 70 4c 69 73 74 2d 3e 70 46 69 72 73 74 20    pList->pFirst 
ab0a0 3d 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 3b 0a  = pLink->pNext;.
ab0b0 20 20 7d 0a 20 20 69 66 28 20 70 50 67 3d 3d 70    }.  if( pPg==p
ab0c0 4c 69 73 74 2d 3e 70 4c 61 73 74 20 29 7b 0a 20  List->pLast ){. 
ab0d0 20 20 20 70 4c 69 73 74 2d 3e 70 4c 61 73 74 20     pList->pLast 
ab0e0 3d 20 70 4c 69 6e 6b 2d 3e 70 50 72 65 76 3b 0a  = pLink->pPrev;.
ab0f0 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 6e 6b 2d    }.  if( pLink-
ab100 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 50 61  >pPrev ){.    Pa
ab110 67 65 72 4c 72 75 4c 69 6e 6b 20 2a 70 50 72 65  gerLruLink *pPre
ab120 76 4c 69 6e 6b 20 3d 20 28 50 61 67 65 72 4c 72  vLink = (PagerLr
ab130 75 4c 69 6e 6b 20 2a 29 28 26 28 28 75 38 20 2a  uLink *)(&((u8 *
ab140 29 70 4c 69 6e 6b 2d 3e 70 50 72 65 76 29 5b 69  )pLink->pPrev)[i
ab150 4f 66 66 5d 29 3b 0a 20 20 20 20 70 50 72 65 76  Off]);.    pPrev
ab160 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 4c  Link->pNext = pL
ab170 69 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  ink->pNext;.  }.
ab180 20 20 69 66 28 20 70 4c 69 6e 6b 2d 3e 70 4e 65    if( pLink->pNe
ab190 78 74 20 29 7b 0a 20 20 20 20 50 61 67 65 72 4c  xt ){.    PagerL
ab1a0 72 75 4c 69 6e 6b 20 2a 70 4e 65 78 74 4c 69 6e  ruLink *pNextLin
ab1b0 6b 20 3d 20 28 50 61 67 65 72 4c 72 75 4c 69 6e  k = (PagerLruLin
ab1c0 6b 20 2a 29 28 26 28 28 75 38 20 2a 29 70 4c 69  k *)(&((u8 *)pLi
ab1d0 6e 6b 2d 3e 70 4e 65 78 74 29 5b 69 4f 66 66 5d  nk->pNext)[iOff]
ab1e0 29 3b 0a 20 20 20 20 70 4e 65 78 74 4c 69 6e 6b  );.    pNextLink
ab1f0 2d 3e 70 50 72 65 76 20 3d 20 70 4c 69 6e 6b 2d  ->pPrev = pLink-
ab200 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66  >pPrev;.  }.  if
ab210 28 20 70 50 67 3d 3d 70 4c 69 73 74 2d 3e 70 46  ( pPg==pList->pF
ab220 69 72 73 74 53 79 6e 63 65 64 20 29 7b 0a 20 20  irstSynced ){.  
ab230 20 20 50 67 48 64 72 20 2a 70 20 3d 20 70 4c 69    PgHdr *p = pLi
ab240 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 77  nk->pNext;.    w
ab250 68 69 6c 65 28 20 70 20 26 26 20 70 2d 3e 6e 65  hile( p && p->ne
ab260 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20  edSync ){.      
ab270 50 61 67 65 72 4c 72 75 4c 69 6e 6b 20 2a 70 4c  PagerLruLink *pL
ab280 20 3d 20 28 50 61 67 65 72 4c 72 75 4c 69 6e 6b   = (PagerLruLink
ab290 20 2a 29 28 26 28 28 75 38 20 2a 29 70 29 5b 69   *)(&((u8 *)p)[i
ab2a0 4f 66 66 5d 29 3b 0a 20 20 20 20 20 20 70 20 3d  Off]);.      p =
ab2b0 20 70 4c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20   pL->pNext;.    
ab2c0 7d 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 46 69  }.    pList->pFi
ab2d0 72 73 74 53 79 6e 63 65 64 20 3d 20 70 3b 0a 20  rstSynced = p;. 
ab2e0 20 7d 0a 0a 20 20 70 4c 69 6e 6b 2d 3e 70 4e 65   }..  pLink->pNe
ab2f0 78 74 20 3d 20 70 4c 69 6e 6b 2d 3e 70 50 72 65  xt = pLink->pPre
ab300 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  v = 0;.}../* .**
ab310 20 41 64 64 20 70 61 67 65 20 70 50 67 20 74 6f   Add page pPg to
ab320 20 74 68 65 20 6c 69 73 74 20 6f 66 20 66 72 65   the list of fre
ab330 65 20 70 61 67 65 73 20 66 6f 72 20 74 68 65 20  e pages for the 
ab340 70 61 67 65 72 2e 20 49 66 20 0a 2a 2a 20 6d 65  pager. If .** me
ab350 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74 20  mory-management 
ab360 69 73 20 65 6e 61 62 6c 65 64 2c 20 61 6c 73 6f  is enabled, also
ab370 20 61 64 64 20 74 68 65 20 70 61 67 65 20 74 6f   add the page to
ab380 20 74 68 65 20 67 6c 6f 62 61 6c 20 0a 2a 2a 20   the global .** 
ab390 6c 69 73 74 20 6f 66 20 66 72 65 65 20 70 61 67  list of free pag
ab3a0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
ab3b0 69 64 20 6c 72 75 4c 69 73 74 41 64 64 28 50 67  id lruListAdd(Pg
ab3c0 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 6c 69 73  Hdr *pPg){.  lis
ab3d0 74 41 64 64 28 26 70 50 67 2d 3e 70 50 61 67 65  tAdd(&pPg->pPage
ab3e0 72 2d 3e 6c 72 75 2c 20 26 70 50 67 2d 3e 66 72  r->lru, &pPg->fr
ab3f0 65 65 2c 20 70 50 67 29 3b 0a 23 69 66 64 65 66  ee, pPg);.#ifdef
ab400 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
ab410 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
ab420 0a 20 20 69 66 28 20 21 70 50 67 2d 3e 70 50 61  .  if( !pPg->pPa
ab430 67 65 72 2d 3e 6d 65 6d 44 62 20 29 7b 0a 20 20  ger->memDb ){.  
ab440 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
ab450 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  enter(sqlite3_mu
ab460 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45  tex_alloc(SQLITE
ab470 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
ab480 55 29 29 3b 0a 20 20 20 20 6c 69 73 74 41 64 64  U));.    listAdd
ab490 28 26 73 71 6c 69 74 65 33 4c 72 75 50 61 67 65  (&sqlite3LruPage
ab4a0 4c 69 73 74 2c 20 26 70 50 67 2d 3e 67 66 72 65  List, &pPg->gfre
ab4b0 65 2c 20 70 50 67 29 3b 0a 20 20 20 20 73 71 6c  e, pPg);.    sql
ab4c0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
ab4d0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  (sqlite3_mutex_a
ab4e0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
ab4f0 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 29 3b 0a  X_STATIC_LRU));.
ab500 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a    }.#endif.}../*
ab510 20 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61 67 65   .** Remove page
ab520 20 70 50 67 20 66 72 6f 6d 20 74 68 65 20 6c 69   pPg from the li
ab530 73 74 20 6f 66 20 66 72 65 65 20 70 61 67 65 73  st of free pages
ab540 20 66 6f 72 20 74 68 65 20 61 73 73 6f 63 69 61   for the associa
ab550 74 65 64 20 70 61 67 65 72 2e 0a 2a 2a 20 49 66  ted pager..** If
ab560 20 6d 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65   memory-manageme
ab570 6e 74 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 61  nt is enabled, a
ab580 6c 73 6f 20 72 65 6d 6f 76 65 20 70 50 67 20 66  lso remove pPg f
ab590 72 6f 6d 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c  rom the global l
ab5a0 69 73 74 0a 2a 2a 20 6f 66 20 66 72 65 65 20 70  ist.** of free p
ab5b0 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ages..*/.static 
ab5c0 76 6f 69 64 20 6c 72 75 4c 69 73 74 52 65 6d 6f  void lruListRemo
ab5d0 76 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  ve(PgHdr *pPg){.
ab5e0 20 20 6c 69 73 74 52 65 6d 6f 76 65 28 26 70 50    listRemove(&pP
ab5f0 67 2d 3e 70 50 61 67 65 72 2d 3e 6c 72 75 2c 20  g->pPager->lru, 
ab600 26 70 50 67 2d 3e 66 72 65 65 2c 20 70 50 67 29  &pPg->free, pPg)
ab610 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
ab620 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
ab630 4e 41 47 45 4d 45 4e 54 0a 20 20 69 66 28 20 21  NAGEMENT.  if( !
ab640 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6d 65 6d  pPg->pPager->mem
ab650 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Db ){.    sqlite
ab660 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
ab670 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
ab680 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
ab690 54 41 54 49 43 5f 4c 52 55 29 29 3b 0a 20 20 20  TATIC_LRU));.   
ab6a0 20 6c 69 73 74 52 65 6d 6f 76 65 28 26 73 71 6c   listRemove(&sql
ab6b0 69 74 65 33 4c 72 75 50 61 67 65 4c 69 73 74 2c  ite3LruPageList,
ab6c0 20 26 70 50 67 2d 3e 67 66 72 65 65 2c 20 70 50   &pPg->gfree, pP
ab6d0 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  g);.    sqlite3_
ab6e0 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69  mutex_leave(sqli
ab6f0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
ab700 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
ab710 54 49 43 5f 4c 52 55 29 29 3b 0a 20 20 7d 0a 23  TIC_LRU));.  }.#
ab720 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  endif.}../* .** 
ab730 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
ab740 20 63 61 6c 6c 65 64 20 6a 75 73 74 20 61 66 74   called just aft
ab750 65 72 20 74 68 65 20 6e 65 65 64 53 79 6e 63 20  er the needSync 
ab760 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 63 6c  flag has been cl
ab770 65 61 72 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 6c  eared.** from al
ab780 6c 20 70 61 67 65 73 20 6d 61 6e 61 67 65 64 20  l pages managed 
ab790 62 79 20 70 50 61 67 65 72 20 28 75 73 75 61 6c  by pPager (usual
ab7a0 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 20 6a  ly because the j
ab7b0 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68  ournal file.** h
ab7c0 61 73 20 6a 75 73 74 20 62 65 65 6e 20 73 79 6e  as just been syn
ab7d0 63 65 64 29 2e 20 49 74 20 75 70 64 61 74 65 73  ced). It updates
ab7e0 20 74 68 65 20 70 50 61 67 65 72 2d 3e 6c 72 75   the pPager->lru
ab7f0 2e 70 46 69 72 73 74 53 79 6e 63 65 64 20 76 61  .pFirstSynced va
ab800 72 69 61 62 6c 65 0a 2a 2a 20 61 6e 64 2c 20 69  riable.** and, i
ab810 66 20 6d 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d  f memory-managem
ab820 65 6e 74 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ent is enabled, 
ab830 74 68 65 20 73 71 6c 69 74 65 33 4c 72 75 50 61  the sqlite3LruPa
ab840 67 65 4c 69 73 74 2e 70 46 69 72 73 74 53 79 6e  geList.pFirstSyn
ab850 63 65 64 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20  ced.** variable 
ab860 61 6c 73 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  also..*/.static 
ab870 76 6f 69 64 20 6c 72 75 4c 69 73 74 53 65 74 46  void lruListSetF
ab880 69 72 73 74 53 79 6e 63 65 64 28 50 61 67 65 72  irstSynced(Pager
ab890 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 70 50 61   *pPager){.  pPa
ab8a0 67 65 72 2d 3e 6c 72 75 2e 70 46 69 72 73 74 53  ger->lru.pFirstS
ab8b0 79 6e 63 65 64 20 3d 20 70 50 61 67 65 72 2d 3e  ynced = pPager->
ab8c0 6c 72 75 2e 70 46 69 72 73 74 3b 0a 23 69 66 64  lru.pFirst;.#ifd
ab8d0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
ab8e0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
ab8f0 4e 54 0a 20 20 69 66 28 20 21 70 50 61 67 65 72  NT.  if( !pPager
ab900 2d 3e 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 50  ->memDb ){.    P
ab910 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 73 71 6c  gHdr *p;.    sql
ab920 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
ab930 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  (sqlite3_mutex_a
ab940 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
ab950 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 29 3b 0a  X_STATIC_LRU));.
ab960 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65      for(p=sqlite
ab970 33 4c 72 75 50 61 67 65 4c 69 73 74 2e 70 46 69  3LruPageList.pFi
ab980 72 73 74 3b 20 70 20 26 26 20 70 2d 3e 6e 65 65  rst; p && p->nee
ab990 64 53 79 6e 63 3b 20 70 3d 70 2d 3e 67 66 72 65  dSync; p=p->gfre
ab9a0 65 2e 70 4e 65 78 74 29 3b 0a 20 20 20 20 61 73  e.pNext);.    as
ab9b0 73 65 72 74 28 70 3d 3d 70 50 61 67 65 72 2d 3e  sert(p==pPager->
ab9c0 6c 72 75 2e 70 46 69 72 73 74 53 79 6e 63 65 64  lru.pFirstSynced
ab9d0 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72   || p==sqlite3Lr
ab9e0 75 50 61 67 65 4c 69 73 74 2e 70 46 69 72 73 74  uPageList.pFirst
ab9f0 53 79 6e 63 65 64 29 3b 0a 20 20 20 20 73 71 6c  Synced);.    sql
aba00 69 74 65 33 4c 72 75 50 61 67 65 4c 69 73 74 2e  ite3LruPageList.
aba10 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d 20 70  pFirstSynced = p
aba20 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
aba30 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
aba40 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51  3_mutex_alloc(SQ
aba50 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
aba60 43 5f 4c 52 55 29 29 3b 0a 20 20 7d 0a 23 65 6e  C_LRU));.  }.#en
aba70 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  dif.}../*.** Ret
aba80 75 72 6e 20 74 72 75 65 20 69 66 20 70 61 67 65  urn true if page
aba90 20 2a 70 50 67 20 68 61 73 20 61 6c 72 65 61 64   *pPg has alread
abaa0 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  y been written t
abab0 6f 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a  o the statement.
abac0 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 6f 72 20 73  ** journal (or s
abad0 74 61 74 65 6d 65 6e 74 20 73 6e 61 70 73 68 6f  tatement snapsho
abae0 74 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74  t has been creat
abaf0 65 64 2c 20 69 66 20 2a 70 50 67 20 69 73 20 70  ed, if *pPg is p
abb00 61 72 74 0a 2a 2a 20 6f 66 20 61 6e 20 69 6e 2d  art.** of an in-
abb10 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 29  memory database)
abb20 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
abb30 70 61 67 65 49 6e 53 74 61 74 65 6d 65 6e 74 28  pageInStatement(
abb40 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50  PgHdr *pPg){.  P
abb50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
abb60 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 66  Pg->pPager;.  if
abb70 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 72  ( MEMDB ){.    r
abb80 65 74 75 72 6e 20 50 47 48 44 52 5f 54 4f 5f 48  eturn PGHDR_TO_H
abb90 49 53 54 28 70 50 67 2c 20 70 50 61 67 65 72 29  IST(pPg, pPager)
abba0 2d 3e 69 6e 53 74 6d 74 3b 0a 20 20 7d 65 6c 73  ->inStmt;.  }els
abbb0 65 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f  e{.    Pgno pgno
abbc0 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20   = pPg->pgno;.  
abbd0 20 20 75 38 20 2a 61 20 3d 20 70 50 61 67 65 72    u8 *a = pPager
abbe0 2d 3e 61 49 6e 53 74 6d 74 3b 0a 20 20 20 20 72  ->aInStmt;.    r
abbf0 65 74 75 72 6e 20 28 61 20 26 26 20 28 69 6e 74  eturn (a && (int
abc00 29 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 73  )pgno<=pPager->s
abc10 74 6d 74 53 69 7a 65 20 26 26 20 28 61 5b 70 67  tmtSize && (a[pg
abc20 6e 6f 2f 38 5d 20 26 20 28 31 3c 3c 28 70 67 6e  no/8] & (1<<(pgn
abc30 6f 26 37 29 29 29 29 3b 0a 20 20 7d 0a 7d 0a 0a  o&7))));.  }.}..
abc40 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
abc50 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 61 67   size of the pag
abc60 65 72 20 68 61 73 68 20 74 61 62 6c 65 20 74 6f  er hash table to
abc70 20 4e 2e 20 20 4e 20 6d 75 73 74 20 62 65 20 61   N.  N must be a
abc80 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20 74 77 6f   power.** of two
abc90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
abca0 20 70 61 67 65 72 5f 72 65 73 69 7a 65 5f 68 61   pager_resize_ha
abcb0 73 68 5f 74 61 62 6c 65 28 50 61 67 65 72 20 2a  sh_table(Pager *
abcc0 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 29 7b 0a  pPager, int N){.
abcd0 20 20 50 67 48 64 72 20 2a 2a 61 48 61 73 68 2c    PgHdr **aHash,
abce0 20 2a 70 50 67 3b 0a 20 20 61 73 73 65 72 74 28   *pPg;.  assert(
abcf0 20 4e 3e 30 20 26 26 20 28 4e 26 28 4e 2d 31 29   N>0 && (N&(N-1)
abd00 29 3d 3d 30 20 29 3b 0a 20 20 70 61 67 65 72 4c  )==0 );.  pagerL
abd10 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20  eave(pPager);.  
abd20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 42 65 6e  sqlite3MallocBen
abd30 69 67 6e 46 61 69 6c 75 72 65 28 28 69 6e 74 29  ignFailure((int)
abd40 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 3b 0a  pPager->aHash);.
abd50 20 20 61 48 61 73 68 20 3d 20 73 71 6c 69 74 65    aHash = sqlite
abd60 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a  3MallocZero( siz
abd70 65 6f 66 28 61 48 61 73 68 5b 30 5d 29 2a 4e 20  eof(aHash[0])*N 
abd80 29 3b 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28  );.  pagerEnter(
abd90 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 61  pPager);.  if( a
abda0 48 61 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  Hash==0 ){.    /
abdb0 2a 20 46 61 69 6c 75 72 65 20 74 6f 20 72 65 68  * Failure to reh
abdc0 61 73 68 20 69 73 20 6e 6f 74 20 61 6e 20 65 72  ash is not an er
abdd0 72 6f 72 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ror.  It is only
abde0 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68   a performance h
abdf0 69 74 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  it. */.    retur
abe00 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  n;.  }.  sqlite3
abe10 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 48  _free(pPager->aH
abe20 61 73 68 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ash);.  pPager->
abe30 6e 48 61 73 68 20 3d 20 4e 3b 0a 20 20 70 50 61  nHash = N;.  pPa
abe40 67 65 72 2d 3e 61 48 61 73 68 20 3d 20 61 48 61  ger->aHash = aHa
abe50 73 68 3b 0a 20 20 66 6f 72 28 70 50 67 3d 70 50  sh;.  for(pPg=pP
abe60 61 67 65 72 2d 3e 70 41 6c 6c 3b 20 70 50 67 3b  ager->pAll; pPg;
abe70 20 70 50 67 3d 70 50 67 2d 3e 70 4e 65 78 74 41   pPg=pPg->pNextA
abe80 6c 6c 29 7b 0a 20 20 20 20 69 6e 74 20 68 3b 0a  ll){.    int h;.
abe90 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e      if( pPg->pgn
abea0 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  o==0 ){.      as
abeb0 73 65 72 74 28 20 70 50 67 2d 3e 70 4e 65 78 74  sert( pPg->pNext
abec0 48 61 73 68 3d 3d 30 20 26 26 20 70 50 67 2d 3e  Hash==0 && pPg->
abed0 70 50 72 65 76 48 61 73 68 3d 3d 30 20 29 3b 0a  pPrevHash==0 );.
abee0 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
abef0 20 20 20 20 7d 0a 20 20 20 20 68 20 3d 20 70 50      }.    h = pP
abf00 67 2d 3e 70 67 6e 6f 20 26 20 28 4e 2d 31 29 3b  g->pgno & (N-1);
abf10 0a 20 20 20 20 70 50 67 2d 3e 70 4e 65 78 74 48  .    pPg->pNextH
abf20 61 73 68 20 3d 20 61 48 61 73 68 5b 68 5d 3b 0a  ash = aHash[h];.
abf30 20 20 20 20 69 66 28 20 61 48 61 73 68 5b 68 5d      if( aHash[h]
abf40 20 29 7b 0a 20 20 20 20 20 20 61 48 61 73 68 5b   ){.      aHash[
abf50 68 5d 2d 3e 70 50 72 65 76 48 61 73 68 20 3d 20  h]->pPrevHash = 
abf60 70 50 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  pPg;.    }.    a
abf70 48 61 73 68 5b 68 5d 20 3d 20 70 50 67 3b 0a 20  Hash[h] = pPg;. 
abf80 20 20 20 70 50 67 2d 3e 70 50 72 65 76 48 61 73     pPg->pPrevHas
abf90 68 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  h = 0;.  }.}../*
abfa0 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69  .** Read a 32-bi
abfb0 74 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74  t integer from t
abfc0 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65  he given file de
abfd0 73 63 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65  scriptor.  Store
abfe0 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   the integer.** 
abff0 74 68 61 74 20 69 73 20 72 65 61 64 20 69 6e 20  that is read in 
ac000 2a 70 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53  *pRes.  Return S
ac010 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72  QLITE_OK if ever
ac020 79 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f  ything worked, o
ac030 72 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f  r an.** error co
ac040 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20  de is something 
ac050 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a  goes wrong..**.*
ac060 2a 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65  * All values are
ac070 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20   stored on disk 
ac080 61 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a  as big-endian..*
ac090 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61  /.static int rea
ac0a0 64 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f  d32bits(sqlite3_
ac0b0 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66  file *fd, i64 of
ac0c0 66 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29  fset, u32 *pRes)
ac0d0 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
ac0e0 72 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72  r ac[4];.  int r
ac0f0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
ac100 64 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66  d(fd, ac, sizeof
ac110 28 61 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20  (ac), offset);. 
ac120 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
ac130 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20  OK ){.    *pRes 
ac140 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74  = sqlite3Get4byt
ac150 65 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74  e(ac);.  }.  ret
ac160 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
ac170 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20   Write a 32-bit 
ac180 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73  integer into a s
ac190 74 72 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20  tring buffer in 
ac1a0 62 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20  big-endian byte 
ac1b0 6f 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  order..*/.#defin
ac1c0 65 20 70 75 74 33 32 62 69 74 73 28 41 2c 42 29  e put32bits(A,B)
ac1d0 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74    sqlite3Put4byt
ac1e0 65 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a  e((u8*)A,B)../*.
ac1f0 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69  ** Write a 32-bi
ac200 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74  t integer into t
ac210 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65  he given file de
ac220 73 63 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72  scriptor.  Retur
ac230 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f  n SQLITE_OK.** o
ac240 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20  n success or an 
ac250 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f  error code is so
ac260 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
ac270 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
ac280 74 20 77 72 69 74 65 33 32 62 69 74 73 28 73 71  t write32bits(sq
ac290 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20  lite3_file *fd, 
ac2a0 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20  i64 offset, u32 
ac2b0 76 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b  val){.  char ac[
ac2c0 34 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28  4];.  put32bits(
ac2d0 61 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75  ac, val);.  retu
ac2e0 72 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  rn sqlite3OsWrit
ac2f0 65 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66  e(fd, ac, 4, off
ac300 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  set);.}../*.** I
ac310 66 20 66 69 6c 65 20 70 46 64 20 69 73 20 6f 70  f file pFd is op
ac320 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  en, call sqlite3
ac330 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20 69 74  OsUnlock() on it
ac340 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
ac350 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  osUnlock(sqlite3
ac360 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e 74 20  _file *pFd, int 
ac370 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 21 70  eLock){.  if( !p
ac380 46 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a  Fd->pMethods ){.
ac390 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ac3a0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  E_OK;.  }.  retu
ac3b0 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  rn sqlite3OsUnlo
ac3c0 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b 0a  ck(pFd, eLock);.
ac3d0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
ac3e0 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65  nction determine
ac3f0 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
ac400 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74   the atomic-writ
ac410 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a  e optimization.*
ac420 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69  * can be used wi
ac430 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 54  th this pager. T
ac440 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
ac450 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a 0a  can be used if:.
ac460 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 76  **.**  (a) the v
ac470 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
ac480 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74   OsDeviceCharact
ac490 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 63  eristics() indic
ac4a0 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 20  ates that.**    
ac4b0 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67    a database pag
ac4c0 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e  e may be written
ac4d0 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e 64   atomically, and
ac4e0 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 6c  .**  (b) the val
ac4f0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f  ue returned by O
ac500 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 73  sSectorSize() is
ac510 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
ac520 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74  ual.**      to t
ac530 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a  he page size..**
ac540 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d  .** If the optim
ac550 69 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62  ization cannot b
ac560 65 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 74  e used, 0 is ret
ac570 75 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e  urned. If it can
ac580 20 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65   be used,.** the
ac590 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
ac5a0 72 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a 65  rned is the size
ac5b0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
ac5c0 66 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20  file when it.** 
ac5d0 63 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63  contains rollbac
ac5e0 6b 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 74  k data for exact
ac5f0 6c 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a  ly one page..*/.
ac600 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
ac610 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
ac620 45 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  E.static int jrn
ac630 6c 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65  lBufferSize(Page
ac640 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
ac650 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20  t dc;           
ac660 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63  /* Device charac
ac670 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 69  teristics */.  i
ac680 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20  nt nSector;     
ac690 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20   /* Sector size 
ac6a0 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20  */.  int nPage; 
ac6b0 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 73         /* Page s
ac6c0 69 7a 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ize */.  sqlite3
ac6d0 5f 66 69 6c 65 20 2a 66 64 20 3d 20 70 50 61 67  _file *fd = pPag
ac6e0 65 72 2d 3e 66 64 3b 0a 0a 20 20 69 66 28 20 66  er->fd;..  if( f
ac6f0 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20  d->pMethods ){. 
ac700 20 20 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f     dc = sqlite3O
ac710 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
ac720 69 73 74 69 63 73 28 66 64 29 3b 0a 20 20 20 20  istics(fd);.    
ac730 6e 53 65 63 74 6f 72 20 3d 20 73 71 6c 69 74 65  nSector = sqlite
ac740 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 66 64  3OsSectorSize(fd
ac750 29 3b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 70  );.    nPage = p
ac760 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b  Pager->pageSize;
ac770 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 53  .  }..  assert(S
ac780 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
ac790 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29  IC512==(512>>8))
ac7a0 3b 0a 20 20 61 73 73 65 72 74 28 53 51 4c 49 54  ;.  assert(SQLIT
ac7b0 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34  E_IOCAP_ATOMIC64
ac7c0 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a  K==(65536>>8));.
ac7d0 0a 20 20 69 66 28 20 21 66 64 2d 3e 70 4d 65 74  .  if( !fd->pMet
ac7e0 68 6f 64 73 20 7c 7c 20 28 64 63 26 28 53 51 4c  hods || (dc&(SQL
ac7f0 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
ac800 7c 28 6e 50 61 67 65 3e 3e 38 29 29 26 26 6e 53  |(nPage>>8))&&nS
ac810 65 63 74 6f 72 3c 3d 6e 50 61 67 65 29 20 29 7b  ector<=nPage) ){
ac820 0a 20 20 20 20 72 65 74 75 72 6e 20 4a 4f 55 52  .    return JOUR
ac830 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
ac840 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f  r) + JOURNAL_PG_
ac850 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  SZ(pPager);.  }.
ac860 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65    return 0;.}.#e
ac870 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
ac880 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
ac890 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   be called when 
ac8a0 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
ac8b0 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 72  within the pager
ac8c0 0a 2a 2a 20 63 6f 64 65 2e 20 54 68 65 20 66 69  .** code. The fi
ac8d0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
ac8e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
ac8f0 20 70 61 67 65 72 20 73 74 72 75 63 74 75 72 65   pager structure
ac900 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20  , the.** second 
ac910 74 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61  the error-code a
ac920 62 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75 72  bout to be retur
ac930 6e 65 64 20 62 79 20 61 20 70 61 67 65 72 20 41  ned by a pager A
ac940 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a  PI function. .**
ac950 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
ac960 6e 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ned is a copy of
ac970 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
ac980 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
ac990 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  ction. .**.** If
ac9a0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
ac9b0 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 49  ment is SQLITE_I
ac9c0 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52  OERR, SQLITE_COR
ac9d0 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 45 5f  RUPT, or SQLITE_
ac9e0 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72 6f  FULL.** the erro
ac9f0 72 20 62 65 63 6f 6d 65 73 20 70 65 72 73 69 73  r becomes persis
aca00 74 65 6e 74 2e 20 41 6c 6c 20 73 75 62 73 65 71  tent. All subseq
aca10 75 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 6f  uent API calls o
aca20 6e 20 74 68 69 73 20 50 61 67 65 72 0a 2a 2a 20  n this Pager.** 
aca30 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79  will immediately
aca40 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65   return the same
aca50 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
aca60 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
aca70 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50  _error(Pager *pP
aca80 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20  ager, int rc){. 
aca90 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20   int rc2 = rc & 
acaa0 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 0a  0xff;.  assert(.
acab0 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65         pPager->e
acac0 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46  rrCode==SQLITE_F
acad0 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 50  ULL ||.       pP
acae0 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53  ager->errCode==S
acaf0 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20  QLITE_OK ||.    
acb00 20 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43     (pPager->errC
acb10 6f 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c  ode & 0xff)==SQL
acb20 49 54 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20  ITE_IOERR.  );. 
acb30 20 69 66 28 0a 20 20 20 20 72 63 32 3d 3d 53 51   if(.    rc2==SQ
acb40 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20  LITE_FULL ||.   
acb50 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45   rc2==SQLITE_IOE
acb60 52 52 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53  RR ||.    rc2==S
acb70 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 20 20  QLITE_CORRUPT.  
acb80 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65  ){.    pPager->e
acb90 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d  rrCode = rc;.  }
acba0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
acbb0 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  ./*.** If SQLITE
acbc0 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20  _CHECK_PAGES is 
acbd0 64 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20  defined then we 
acbe0 64 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63  do some sanity c
acbf0 68 65 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68  hecking.** on th
acc00 65 20 63 61 63 68 65 20 75 73 69 6e 67 20 61 20  e cache using a 
acc10 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20  hash function.  
acc20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72  This is used for
acc30 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20   testing.** and 
acc40 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a  debugging only..
acc50 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
acc60 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a  _CHECK_PAGES./*.
acc70 2a 2a 20 52 65 74 75 72 6e 20 61 20 33 32 2d 62  ** Return a 32-b
acc80 69 74 20 68 61 73 68 20 6f 66 20 74 68 65 20 70  it hash of the p
acc90 61 67 65 20 64 61 74 61 20 66 6f 72 20 70 50 61  age data for pPa
acca0 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33  ge..*/.static u3
accb0 32 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68  2 pager_datahash
accc0 28 69 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 69  (int nByte, unsi
accd0 67 6e 65 64 20 63 68 61 72 20 2a 70 44 61 74 61  gned char *pData
acce0 29 7b 0a 20 20 75 33 32 20 68 61 73 68 20 3d 20  ){.  u32 hash = 
accf0 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  0;.  int i;.  fo
acd00 72 28 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20  r(i=0; i<nByte; 
acd10 69 2b 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 3d  i++){.    hash =
acd20 20 28 68 61 73 68 2a 31 30 33 39 29 20 2b 20 70   (hash*1039) + p
acd30 44 61 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72  Data[i];.  }.  r
acd40 65 74 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74  eturn hash;.}.st
acd50 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 70  atic u32 pager_p
acd60 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70  agehash(PgHdr *p
acd70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  Page){.  return 
acd80 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70  pager_datahash(p
acd90 50 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61  Page->pPager->pa
acda0 67 65 53 69 7a 65 2c 20 0a 20 20 20 20 20 20 20  geSize, .       
acdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acdc0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
acdd0 2a 29 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28  *)PGHDR_TO_DATA(
acde0 70 50 61 67 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pPage));.}../*.*
acdf0 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45  * The CHECK_PAGE
ace00 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50   macro takes a P
ace10 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75  gHdr* as an argu
ace20 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f  ment. If SQLITE_
ace30 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69  CHECK_PAGES.** i
ace40 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e  s defined, and N
ace50 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66  DEBUG is not def
ace60 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28  ined, an assert(
ace70 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63  ) statement chec
ace80 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70  ks.** that the p
ace90 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64 69  age is either di
acea0 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74  rty or still mat
aceb0 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61  ches the calcula
acec0 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a  ted page-hash..*
aced0 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  /.#define CHECK_
acee0 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 67  PAGE(x) checkPag
acef0 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64  e(x).static void
acf00 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 72   checkPage(PgHdr
acf10 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20   *pPg){.  Pager 
acf20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
acf30 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28  Pager;.  assert(
acf40 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20   !pPg->pageHash 
acf50 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  || pPager->errCo
acf60 64 65 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 70  de || MEMDB || p
acf70 50 67 2d 3e 64 69 72 74 79 20 7c 7c 20 0a 20 20  Pg->dirty || .  
acf80 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73      pPg->pageHas
acf90 68 3d 3d 70 61 67 65 72 5f 70 61 67 65 68 61 73  h==pager_pagehas
acfa0 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c  h(pPg) );.}..#el
acfb0 73 65 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72  se.#define pager
acfc0 5f 64 61 74 61 68 61 73 68 28 58 2c 59 29 20 20  _datahash(X,Y)  
acfd0 30 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f  0.#define pager_
acfe0 70 61 67 65 68 61 73 68 28 58 29 20 20 30 0a 23  pagehash(X)  0.#
acff0 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41 47  define CHECK_PAG
ad000 45 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  E(x).#endif../*.
ad010 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20  ** When this is 
ad020 63 61 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e  called the journ
ad030 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65  al file for page
ad040 72 20 70 50 61 67 65 72 20 6d 75 73 74 20 62 65  r pPager must be
ad050 20 6f 70 65 6e 2e 0a 2a 2a 20 54 68 65 20 6d 61   open..** The ma
ad060 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
ad070 65 20 6e 61 6d 65 20 69 73 20 72 65 61 64 20 66  e name is read f
ad080 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74  rom the end of t
ad090 68 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20  he file and .** 
ad0a0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 6d 65 6d  written into mem
ad0b0 6f 72 79 20 73 75 70 70 6c 69 65 64 20 62 79 20  ory supplied by 
ad0c0 74 68 65 20 63 61 6c 6c 65 72 2e 20 0a 2a 2a 0a  the caller. .**.
ad0d0 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20  ** zMaster must 
ad0e0 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65  point to a buffe
ad0f0 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d  r of at least nM
ad100 61 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f  aster bytes allo
ad110 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  cated by.** the 
ad120 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 6f  caller. This sho
ad130 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f 76  uld be sqlite3_v
ad140 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20  fs.mxPathname+1 
ad150 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72 65  (to ensure there
ad160 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 70   is.** enough sp
ad170 61 63 65 20 74 6f 20 77 72 69 74 65 20 74 68 65  ace to write the
ad180 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
ad190 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d 61  name). If the ma
ad1a0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ster journal.** 
ad1b0 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72  name in the jour
ad1c0 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 68  nal is longer th
ad1d0 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73  an nMaster bytes
ad1e0 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a 2a   (including a.**
ad1f0 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29   nul-terminator)
ad200 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 68  , then this is h
ad210 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f 20  andled as if no 
ad220 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
ad230 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 73  ame.** were pres
ad240 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ent in the journ
ad250 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20  al..**.** If no 
ad260 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
ad270 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73  ile name is pres
ad280 65 6e 74 20 7a 4d 61 73 74 65 72 5b 30 5d 20 69  ent zMaster[0] i
ad290 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 0a 2a  s set to 0 and.*
ad2a0 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  * SQLITE_OK retu
ad2b0 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
ad2c0 69 6e 74 20 72 65 61 64 4d 61 73 74 65 72 4a 6f  int readMasterJo
ad2d0 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69  urnal(sqlite3_fi
ad2e0 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63 68 61 72 20  le *pJrnl, char 
ad2f0 2a 7a 4d 61 73 74 65 72 2c 20 69 6e 74 20 6e 4d  *zMaster, int nM
ad300 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  aster){.  int rc
ad310 3b 0a 20 20 75 33 32 20 6c 65 6e 3b 0a 20 20 69  ;.  u32 len;.  i
ad320 36 34 20 73 7a 4a 3b 0a 20 20 75 33 32 20 63 6b  64 szJ;.  u32 ck
ad330 73 75 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  sum;.  int i;.  
ad340 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d  unsigned char aM
ad350 61 67 69 63 5b 38 5d 3b 20 2f 2a 20 41 20 62 75  agic[8]; /* A bu
ad360 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ffer to hold the
ad370 20 6d 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f   magic header */
ad380 0a 0a 20 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d  ..  zMaster[0] =
ad390 20 27 5c 30 27 3b 0a 0a 20 20 72 63 20 3d 20 73   '\0';..  rc = s
ad3a0 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
ad3b0 28 70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 3b 0a 20  (pJrnl, &szJ);. 
ad3c0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
ad3d0 4f 4b 20 7c 7c 20 73 7a 4a 3c 31 36 20 29 20 72  OK || szJ<16 ) r
ad3e0 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
ad3f0 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72  = read32bits(pJr
ad400 6e 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e  nl, szJ-16, &len
ad410 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
ad420 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
ad430 72 63 3b 0a 0a 20 20 69 66 28 20 6c 65 6e 3e 3d  rc;..  if( len>=
ad440 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72  nMaster ){.    r
ad450 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ad460 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61  .  }..  rc = rea
ad470 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73  d32bits(pJrnl, s
ad480 7a 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 3b 0a  zJ-12, &cksum);.
ad490 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
ad4a0 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
ad4b0 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
ad4c0 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d  OsRead(pJrnl, aM
ad4d0 61 67 69 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 3b  agic, 8, szJ-8);
ad4e0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
ad4f0 45 5f 4f 4b 20 7c 7c 20 6d 65 6d 63 6d 70 28 61  E_OK || memcmp(a
ad500 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d  Magic, aJournalM
ad510 61 67 69 63 2c 20 38 29 20 29 20 72 65 74 75 72  agic, 8) ) retur
ad520 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
ad530 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e  lite3OsRead(pJrn
ad540 6c 2c 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c  l, zMaster, len,
ad550 20 73 7a 4a 2d 31 36 2d 6c 65 6e 29 3b 0a 20 20   szJ-16-len);.  
ad560 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
ad570 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
ad580 72 63 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65  rc;.  }.  zMaste
ad590 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 0a  r[len] = '\0';..
ad5a0 20 20 2f 2a 20 53 65 65 20 69 66 20 74 68 65 20    /* See if the 
ad5b0 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 68 65 73  checksum matches
ad5c0 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
ad5d0 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f  nal name */.  fo
ad5e0 72 28 69 3d 30 3b 20 69 3c 6c 65 6e 3b 20 69 2b  r(i=0; i<len; i+
ad5f0 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d  +){.    cksum -=
ad600 20 7a 4d 61 73 74 65 72 5b 69 5d 3b 0a 20 20 20   zMaster[i];.   
ad610 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20 29 7b  }.  if( cksum ){
ad620 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63  .    /* If the c
ad630 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27 74 20  hecksum doesn't 
ad640 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f 6e 65  add up, then one
ad650 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20   or more of the 
ad660 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20 20 20  disk sectors.   
ad670 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74   ** containing t
ad680 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
ad690 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 63 6f  l filename is co
ad6a0 72 72 75 70 74 65 64 2e 20 54 68 69 73 20 6d 65  rrupted. This me
ad6b0 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66 69 6e  ans.    ** defin
ad6c0 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63 6b 2c  itely roll back,
ad6d0 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72 6e 20   so just return 
ad6e0 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65  SQLITE_OK and re
ad6f0 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20 20 20  port a (nul).   
ad700 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e   ** master-journ
ad710 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20 20 20  al filename..   
ad720 20 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 5b   */.    zMaster[
ad730 30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 0a 20  0] = '\0';.  }. 
ad740 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49    .  return SQLI
ad750 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
ad760 53 65 65 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Seek the journal
ad770 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
ad780 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63   to the next sec
ad790 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 77 68 65  tor boundary whe
ad7a0 72 65 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  re a.** journal 
ad7b0 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 72 65  header may be re
ad7c0 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 50  ad or written. P
ad7d0 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ager.journalOff 
ad7e0 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68 0a  is updated with.
ad7f0 2a 2a 20 74 68 65 20 6e 65 77 20 73 65 65 6b 20  ** the new seek 
ad800 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 69 2e  offset..**.** i.
ad810 65 20 66 6f 72 20 61 20 73 65 63 74 6f 72 20 73  e for a sector s
ad820 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a  ize of 512:.**.*
ad830 2a 20 49 6e 70 75 74 20 4f 66 66 73 65 74 20 20  * Input Offset  
ad840 20 20 20 20 20 20 20 20 20 20 20 20 4f 75 74 70              Outp
ad850 75 74 20 4f 66 66 73 65 74 0a 2a 2a 20 2d 2d 2d  ut Offset.** ---
ad860 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ad870 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ad880 2d 2d 2d 2d 0a 2a 2a 20 30 20 20 20 20 20 20 20  ----.** 0       
ad890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8a0 20 20 30 0a 2a 2a 20 35 31 32 20 20 20 20 20 20    0.** 512      
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8c0 20 35 31 32 0a 2a 2a 20 31 30 30 20 20 20 20 20   512.** 100     
ad8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8e0 20 20 35 31 32 0a 2a 2a 20 32 30 30 30 20 20 20    512.** 2000   
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad900 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73     2048.** .*/.s
ad910 74 61 74 69 63 20 76 6f 69 64 20 73 65 65 6b 4a  tatic void seekJ
ad920 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20  ournalHdr(Pager 
ad930 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20  *pPager){.  i64 
ad940 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36  offset = 0;.  i6
ad950 34 20 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  4 c = pPager->jo
ad960 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20  urnalOff;.  if( 
ad970 63 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20  c ){.    offset 
ad980 3d 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c  = ((c-1)/JOURNAL
ad990 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
ad9a0 2b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48  + 1) * JOURNAL_H
ad9b0 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  DR_SZ(pPager);. 
ad9c0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66   }.  assert( off
ad9d0 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  set%JOURNAL_HDR_
ad9e0 53 5a 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b  SZ(pPager)==0 );
ad9f0 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65  .  assert( offse
ada00 74 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74  t>=c );.  assert
ada10 28 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55  ( (offset-c)<JOU
ada20 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
ada30 65 72 29 20 29 3b 0a 20 20 70 50 61 67 65 72 2d  er) );.  pPager-
ada40 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6f 66  >journalOff = of
ada50 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  fset;.}../*.** T
ada60 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
ada70 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65  must be open whe
ada80 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
ada90 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72  s called. A jour
adaa0 6e 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a  nal.** header (J
adab0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79  OURNAL_HDR_SZ by
adac0 74 65 73 29 20 69 73 20 77 72 69 74 74 65 6e 20  tes) is written 
adad0 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
adae0 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20   file at the.** 
adaf0 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
adb00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d  ..**.** The form
adb10 61 74 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e  at for the journ
adb20 61 6c 20 68 65 61 64 65 72 20 69 73 20 61 73 20  al header is as 
adb30 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20  follows:.** - 8 
adb40 62 79 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65  bytes: Magic ide
adb50 6e 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c  ntifying journal
adb60 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20   format..** - 4 
adb70 62 79 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66  bytes: Number of
adb80 20 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72   records in jour
adb90 6e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79  nal, or -1 no-sy
adba0 6e 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a  nc mode is on..*
adbb0 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e  * - 4 bytes: Ran
adbc0 64 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20  dom number used 
adbd0 66 6f 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a  for page hash..*
adbe0 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69  * - 4 bytes: Ini
adbf0 74 69 61 6c 20 64 61 74 61 62 61 73 65 20 70 61  tial database pa
adc00 67 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34  ge count..** - 4
adc10 20 62 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73   bytes: Sector s
adc20 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20  ize used by the 
adc30 70 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f  process that wro
adc40 74 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e  te this journal.
adc50 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64  .** .** Followed
adc60 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52   by (JOURNAL_HDR
adc70 5f 53 5a 20 2d 20 32 34 29 20 62 79 74 65 73 20  _SZ - 24) bytes 
adc80 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 2e  of unused space.
adc90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
adca0 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50  riteJournalHdr(P
adcb0 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
adcc0 20 63 68 61 72 20 7a 48 65 61 64 65 72 5b 73 69   char zHeader[si
adcd0 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
adce0 69 63 29 2b 31 36 5d 3b 0a 20 20 69 6e 74 20 72  ic)+16];.  int r
adcf0 63 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72  c;..  if( pPager
add00 2d 3e 73 74 6d 74 48 64 72 4f 66 66 3d 3d 30 20  ->stmtHdrOff==0 
add10 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  ){.    pPager->s
add20 74 6d 74 48 64 72 4f 66 66 20 3d 20 70 50 61 67  tmtHdrOff = pPag
add30 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a  er->journalOff;.
add40 20 20 7d 0a 0a 20 20 73 65 65 6b 4a 6f 75 72 6e    }..  seekJourn
add50 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20  alHdr(pPager);. 
add60 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
add70 48 64 72 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  Hdr = pPager->jo
add80 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 6d 65 6d  urnalOff;..  mem
add90 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f  cpy(zHeader, aJo
adda0 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65  urnalMagic, size
addb0 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
addc0 29 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20  ));..  /* .  ** 
addd0 57 72 69 74 65 20 74 68 65 20 6e 52 65 63 20 46  Write the nRec F
adde0 69 65 6c 64 20 2d 20 74 68 65 20 6e 75 6d 62 65  ield - the numbe
addf0 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f 72 64  r of page record
ade00 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68  s that follow th
ade10 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20  is.  ** journal 
ade20 68 65 61 64 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79  header. Normally
ade30 2c 20 7a 65 72 6f 20 69 73 20 77 72 69 74 74 65  , zero is writte
ade40 6e 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20  n to this value 
ade50 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 20 20  at this time..  
ade60 2a 2a 20 41 66 74 65 72 20 74 68 65 20 72 65 63  ** After the rec
ade70 6f 72 64 73 20 61 72 65 20 61 64 64 65 64 20 74  ords are added t
ade80 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 28 61  o the journal (a
ade90 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73  nd the journal s
adea0 79 6e 63 65 64 2c 20 0a 20 20 2a 2a 20 69 66 20  ynced, .  ** if 
adeb0 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64  in full-sync mod
adec0 65 29 2c 20 74 68 65 20 7a 65 72 6f 20 69 73 20  e), the zero is 
aded0 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68  overwritten with
adee0 20 74 68 65 20 74 72 75 65 20 6e 75 6d 62 65 72   the true number
adef0 0a 20 20 2a 2a 20 6f 66 20 72 65 63 6f 72 64 73  .  ** of records
adf00 20 28 73 65 65 20 73 79 6e 63 4a 6f 75 72 6e 61   (see syncJourna
adf10 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  l())..  **.  ** 
adf20 41 20 66 61 73 74 65 72 20 61 6c 74 65 72 6e 61  A faster alterna
adf30 74 69 76 65 20 69 73 20 74 6f 20 77 72 69 74 65  tive is to write
adf40 20 30 78 46 46 46 46 46 46 46 46 20 74 6f 20 74   0xFFFFFFFF to t
adf50 68 65 20 6e 52 65 63 20 66 69 65 6c 64 2e 20 57  he nRec field. W
adf60 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67  hen.  ** reading
adf70 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 69   the journal thi
adf80 73 20 76 61 6c 75 65 20 74 65 6c 6c 73 20 53 51  s value tells SQ
adf90 4c 69 74 65 20 74 6f 20 61 73 73 75 6d 65 20 74  Lite to assume t
adfa0 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 72 65 73  hat the.  ** res
adfb0 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
adfc0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 76   file contains v
adfd0 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64  alid page record
adfe0 73 2e 20 54 68 69 73 20 61 73 73 75 6d 70 74 69  s. This assumpti
adff0 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 61 6e 67 65  on.  ** is dange
ae000 72 6f 75 73 2c 20 61 73 20 69 66 20 61 20 66 61  rous, as if a fa
ae010 69 6c 75 72 65 20 6f 63 63 75 72 65 64 20 77 68  ilure occured wh
ae020 69 6c 73 74 20 77 72 69 74 69 6e 67 20 74 6f 20  ilst writing to 
ae030 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a  the journal.  **
ae040 20 66 69 6c 65 20 69 74 20 6d 61 79 20 63 6f 6e   file it may con
ae050 74 61 69 6e 20 73 6f 6d 65 20 67 61 72 62 61 67  tain some garbag
ae060 65 20 64 61 74 61 2e 20 54 68 65 72 65 20 61 72  e data. There ar
ae070 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 0a  e two scenarios.
ae080 20 20 2a 2a 20 77 68 65 72 65 20 74 68 69 73 20    ** where this 
ae090 72 69 73 6b 20 63 61 6e 20 62 65 20 69 67 6e 6f  risk can be igno
ae0a0 72 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  red:.  **.  **  
ae0b0 20 2a 20 57 68 65 6e 20 74 68 65 20 70 61 67 65   * When the page
ae0c0 72 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20  r is in no-sync 
ae0d0 6d 6f 64 65 2e 20 43 6f 72 72 75 70 74 69 6f 6e  mode. Corruption
ae0e0 20 63 61 6e 20 66 6f 6c 6c 6f 77 20 61 0a 20 20   can follow a.  
ae0f0 2a 2a 20 20 20 20 20 70 6f 77 65 72 20 66 61 69  **     power fai
ae100 6c 75 72 65 20 69 6e 20 74 68 69 73 20 63 61 73  lure in this cas
ae110 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2a 0a 20  e anyway..  **. 
ae120 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65   **   * When the
ae130 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41   SQLITE_IOCAP_SA
ae140 46 45 5f 41 50 50 45 4e 44 20 66 6c 61 67 20 69  FE_APPEND flag i
ae150 73 20 73 65 74 2e 20 54 68 69 73 20 67 75 61 72  s set. This guar
ae160 61 6e 74 65 65 73 0a 20 20 2a 2a 20 20 20 20 20  antees.  **     
ae170 74 68 61 74 20 67 61 72 62 61 67 65 20 64 61 74  that garbage dat
ae180 61 20 69 73 20 6e 65 76 65 72 20 61 70 70 65 6e  a is never appen
ae190 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ded to the journ
ae1a0 61 6c 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20  al file..  */.  
ae1b0 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66  assert(pPager->f
ae1c0 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61  d->pMethods||pPa
ae1d0 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 3b 0a 20 20  ger->noSync);.  
ae1e0 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53  if( (pPager->noS
ae1f0 79 6e 63 29 20 0a 20 20 20 7c 7c 20 28 73 71 6c  ync) .   || (sql
ae200 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72  ite3OsDeviceChar
ae210 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67  acteristics(pPag
ae220 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49  er->fd)&SQLITE_I
ae230 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
ae240 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 75 74 33  ) .  ){.    put3
ae250 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73  2bits(&zHeader[s
ae260 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
ae270 67 69 63 29 5d 2c 20 30 78 66 66 66 66 66 66 66  gic)], 0xfffffff
ae280 66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  f);.  }else{.   
ae290 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61   put32bits(&zHea
ae2a0 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72  der[sizeof(aJour
ae2b0 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 30 29 3b 0a  nalMagic)], 0);.
ae2c0 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 72 61    }..  /* The ra
ae2d0 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 73 68 20  ndom check-hash 
ae2e0 69 6e 69 74 69 61 6c 69 73 65 72 20 2a 2f 20 0a  initialiser */ .
ae2f0 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f 6d 6e    sqlite3Randomn
ae300 65 73 73 28 73 69 7a 65 6f 66 28 70 50 61 67 65  ess(sizeof(pPage
ae310 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 2c 20 26  r->cksumInit), &
ae320 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
ae330 74 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28  t);.  put32bits(
ae340 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
ae350 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34  aJournalMagic)+4
ae360 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d  ], pPager->cksum
ae370 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 54 68 65 20  Init);.  /* The 
ae380 69 6e 69 74 69 61 6c 20 64 61 74 61 62 61 73 65  initial database
ae390 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32   size */.  put32
ae3a0 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69  bits(&zHeader[si
ae3b0 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
ae3c0 69 63 29 2b 38 5d 2c 20 70 50 61 67 65 72 2d 3e  ic)+8], pPager->
ae3d0 64 62 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68  dbSize);.  /* Th
ae3e0 65 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72  e assumed sector
ae3f0 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70   size for this p
ae400 72 6f 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33  rocess */.  put3
ae410 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73  2bits(&zHeader[s
ae420 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
ae430 67 69 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72  gic)+12], pPager
ae440 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 20  ->sectorSize);. 
ae450 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20   IOTRACE(("JHDR 
ae460 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70  %p %lld %d\n", p
ae470 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a  Pager, pPager->j
ae480 6f 75 72 6e 61 6c 48 64 72 2c 20 73 69 7a 65 6f  ournalHdr, sizeo
ae490 66 28 7a 48 65 61 64 65 72 29 29 29 0a 20 20 72  f(zHeader))).  r
ae4a0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
ae4b0 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
ae4c0 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28  zHeader, sizeof(
ae4d0 7a 48 65 61 64 65 72 29 2c 70 50 61 67 65 72 2d  zHeader),pPager-
ae4e0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20  >journalOff);.  
ae4f0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
ae500 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44  ff += JOURNAL_HD
ae510 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 0a 20  R_SZ(pPager);.. 
ae520 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20   /* The journal 
ae530 68 65 61 64 65 72 20 68 61 73 20 62 65 65 6e 20  header has been 
ae540 77 72 69 74 74 65 6e 20 73 75 63 63 65 73 73 66  written successf
ae550 75 6c 6c 79 2e 20 53 65 65 6b 20 74 68 65 20 6a  ully. Seek the j
ae560 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65  ournal.  ** file
ae570 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74   descriptor to t
ae580 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f  he end of the jo
ae590 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63  urnal header sec
ae5a0 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  tor..  */.  if( 
ae5b0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
ae5c0 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a  .    IOTRACE(("J
ae5d0 54 41 49 4c 20 25 70 20 25 6c 6c 64 5c 6e 22 2c  TAIL %p %lld\n",
ae5e0 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d   pPager, pPager-
ae5f0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2d 31 29 29 0a  >journalOff-1)).
ae600 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
ae610 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
ae620 6a 66 64 2c 20 22 5c 30 30 30 22 2c 20 31 2c 20  jfd, "\000", 1, 
ae630 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
ae640 66 66 2d 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ff-1);.  }.  ret
ae650 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
ae660 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
ae670 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77  e must be open w
ae680 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c  hen this is call
ae690 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 68 65  ed. A journal he
ae6a0 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20 28 4a 4f  ader file.** (JO
ae6b0 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74  URNAL_HDR_SZ byt
ae6c0 65 73 29 20 69 73 20 72 65 61 64 20 66 72 6f 6d  es) is read from
ae6d0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63   the current loc
ae6e0 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75  ation in the jou
ae6f0 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 53 65  rnal.** file. Se
ae700 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65  e comments above
ae710 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a   function writeJ
ae720 6f 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 20  ournalHdr() for 
ae730 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  a description of
ae740 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  .** the journal 
ae750 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a  header format..*
ae760 2a 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61 64  *.** If the head
ae770 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63 65  er is read succe
ae780 73 73 66 75 6c 6c 79 2c 20 2a 6e 52 65 63 20 69  ssfully, *nRec i
ae790 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d  s set to the num
ae7a0 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72  ber of.** page r
ae7b0 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67  ecords following
ae7c0 20 74 68 69 73 20 68 65 61 64 65 72 20 61 6e 64   this header and
ae7d0 20 2a 64 62 53 69 7a 65 20 69 73 20 73 65 74 20   *dbSize is set 
ae7e0 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  to the size of t
ae7f0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 62  he.** database b
ae800 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61  efore the transa
ae810 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e 20  ction began, in 
ae820 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50 61  pages. Also, pPa
ae830 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a  ger->cksumInit.*
ae840 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  * is set to the 
ae850 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20  value read from 
ae860 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
ae870 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  er. SQLITE_OK is
ae880 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e 20   returned.** in 
ae890 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  this case..**.**
ae8a0 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
ae8b0 68 65 61 64 65 72 20 66 69 6c 65 20 61 70 70 65  header file appe
ae8c0 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70  ars to be corrup
ae8d0 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45  ted, SQLITE_DONE
ae8e0 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20   is.** returned 
ae8f0 61 6e 64 20 2a 6e 52 65 63 20 61 6e 64 20 2a 64  and *nRec and *d
ae900 62 53 69 7a 65 20 61 72 65 20 6e 6f 74 20 73 65  bSize are not se
ae910 74 2e 20 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48  t.  If JOURNAL_H
ae920 44 52 5f 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63  DR_SZ bytes.** c
ae930 61 6e 6e 6f 74 20 62 65 20 72 65 61 64 20 66 72  annot be read fr
ae940 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  om the journal f
ae950 69 6c 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  ile an error cod
ae960 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
ae970 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61  /.static int rea
ae980 64 4a 6f 75 72 6e 61 6c 48 64 72 28 0a 20 20 50  dJournalHdr(.  P
ae990 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 0a 20  ager *pPager, . 
ae9a0 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65   i64 journalSize
ae9b0 2c 0a 20 20 75 33 32 20 2a 70 4e 52 65 63 2c 20  ,.  u32 *pNRec, 
ae9c0 0a 20 20 75 33 32 20 2a 70 44 62 53 69 7a 65 0a  .  u32 *pDbSize.
ae9d0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75  ){.  int rc;.  u
ae9e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61  nsigned char aMa
ae9f0 67 69 63 5b 38 5d 3b 20 2f 2a 20 41 20 62 75 66  gic[8]; /* A buf
aea00 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  fer to hold the 
aea10 6d 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a  magic header */.
aea20 20 20 69 36 34 20 6a 72 6e 6c 4f 66 66 3b 0a 0a    i64 jrnlOff;..
aea30 20 20 73 65 65 6b 4a 6f 75 72 6e 61 6c 48 64 72    seekJournalHdr
aea40 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20  (pPager);.  if( 
aea50 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
aea60 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  ff+JOURNAL_HDR_S
aea70 5a 28 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72  Z(pPager) > jour
aea80 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72  nalSize ){.    r
aea90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
aeaa0 45 3b 0a 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66  E;.  }.  jrnlOff
aeab0 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
aeac0 61 6c 4f 66 66 3b 0a 0a 20 20 72 63 20 3d 20 73  alOff;..  rc = s
aead0 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61  qlite3OsRead(pPa
aeae0 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63  ger->jfd, aMagic
aeaf0 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29  , sizeof(aMagic)
aeb00 2c 20 6a 72 6e 6c 4f 66 66 29 3b 0a 20 20 69 66  , jrnlOff);.  if
aeb10 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
aeb20 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20 73  ;.  jrnlOff += s
aeb30 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 3b 0a 0a  izeof(aMagic);..
aeb40 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61    if( memcmp(aMa
aeb50 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67  gic, aJournalMag
aeb60 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69  ic, sizeof(aMagi
aeb70 63 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65  c))!=0 ){.    re
aeb80 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
aeb90 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72 65  ;.  }..  rc = re
aeba0 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  ad32bits(pPager-
aebb0 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70  >jfd, jrnlOff, p
aebc0 4e 52 65 63 29 3b 0a 20 20 69 66 28 20 72 63 20  NRec);.  if( rc 
aebd0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
aebe0 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
aebf0 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e  pPager->jfd, jrn
aec00 6c 4f 66 66 2b 34 2c 20 26 70 50 61 67 65 72 2d  lOff+4, &pPager-
aec10 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 69  >cksumInit);.  i
aec20 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
aec30 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33  c;..  rc = read3
aec40 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
aec50 64 2c 20 6a 72 6e 6c 4f 66 66 2b 38 2c 20 70 44  d, jrnlOff+8, pD
aec60 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63  bSize);.  if( rc
aec70 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
aec80 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61   /* Update the a
aec90 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69  ssumed sector-si
aeca0 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  ze to match the 
aecb0 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20  value used by . 
aecc0 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20   ** the process 
aecd0 74 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69  that created thi
aece0 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68  s journal. If th
aecf0 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20  is journal was. 
aed00 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 61   ** created by a
aed10 20 70 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74   process other t
aed20 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68  han this one, th
aed30 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
aed40 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61    ** is being ca
aed50 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
aed60 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
aed70 29 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c  ). The local val
aed80 75 65 0a 20 20 2a 2a 20 6f 66 20 50 61 67 65 72  ue.  ** of Pager
aed90 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72  .sectorSize is r
aeda0 65 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 65  estored at the e
aedb0 6e 64 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69  nd of that routi
aedc0 6e 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  ne..  */.  rc = 
aedd0 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65  read32bits(pPage
aede0 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b  r->jfd, jrnlOff+
aedf0 31 32 2c 20 28 75 33 32 20 2a 29 26 70 50 61 67  12, (u32 *)&pPag
aee00 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b  er->sectorSize);
aee10 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
aee20 72 6e 20 72 63 3b 0a 0a 20 20 70 50 61 67 65 72  rn rc;..  pPager
aee30 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20  ->journalOff += 
aee40 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
aee50 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  Pager);.  return
aee60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
aee70 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  /*.** Write the 
aee80 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72 20  supplied master 
aee90 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74  journal name int
aeea0 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
aeeb0 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20  le for pager.** 
aeec0 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63 75  pPager at the cu
aeed0 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20  rrent location. 
aeee0 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  The master journ
aeef0 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20  al name must be 
aef00 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e  the last.** thin
aef10 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 6a  g written to a j
aef20 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
aef30 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
aef40 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20  full-sync mode, 
aef50 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
aef60 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
aef70 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74 68  s advanced to th
aef80 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62 6f  e next sector bo
aef90 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a  undary before.**
aefa0 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72 69   anything is wri
aefb0 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74  tten. The format
aefc0 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 2b 20 34 20 62   is:.**.** + 4 b
aefd0 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50  ytes: PAGER_MJ_P
aefe0 47 4e 4f 2e 0a 2a 2a 20 2b 20 4e 20 62 79 74 65  GNO..** + N byte
aeff0 73 3a 20 6c 65 6e 67 74 68 20 6f 66 20 6d 61 73  s: length of mas
af000 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
af010 2e 0a 2a 2a 20 2b 20 34 20 62 79 74 65 73 3a 20  ..** + 4 bytes: 
af020 4e 0a 2a 2a 20 2b 20 34 20 62 79 74 65 73 3a 20  N.** + 4 bytes: 
af030 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  Master journal n
af040 61 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a  ame checksum..**
af050 20 2b 20 38 20 62 79 74 65 73 3a 20 61 4a 6f 75   + 8 bytes: aJou
af060 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 0a  rnalMagic[]..**.
af070 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f  ** The master jo
af080 75 72 6e 61 6c 20 70 61 67 65 20 63 68 65 63 6b  urnal page check
af090 73 75 6d 20 69 73 20 74 68 65 20 73 75 6d 20 6f  sum is the sum o
af0a0 66 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74  f the bytes in t
af0b0 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75  he master.** jou
af0c0 72 6e 61 6c 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a  rnal name..**.**
af0d0 20 49 66 20 7a 4d 61 73 74 65 72 20 69 73 20 61   If zMaster is a
af0e0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 6f   NULL pointer (o
af0f0 63 63 75 72 73 20 66 6f 72 20 61 20 73 69 6e 67  ccurs for a sing
af100 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e  le database tran
af110 73 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68  saction), .** th
af120 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
af130 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
af140 74 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75  t writeMasterJou
af150 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67  rnal(Pager *pPag
af160 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  er, const char *
af170 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20  zMaster){.  int 
af180 72 63 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 0a  rc;.  int len; .
af190 20 20 69 6e 74 20 69 3b 20 0a 20 20 69 36 34 20    int i; .  i64 
af1a0 6a 72 6e 6c 4f 66 66 3b 0a 20 20 75 33 32 20 63  jrnlOff;.  u32 c
af1b0 6b 73 75 6d 20 3d 20 30 3b 0a 20 20 63 68 61 72  ksum = 0;.  char
af1c0 20 7a 42 75 66 5b 73 69 7a 65 6f 66 28 61 4a 6f   zBuf[sizeof(aJo
af1d0 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 2a 34 5d  urnalMagic)+2*4]
af1e0 3b 0a 0a 20 20 69 66 28 20 21 7a 4d 61 73 74 65  ;..  if( !zMaste
af1f0 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 65 74  r || pPager->set
af200 4d 61 73 74 65 72 29 20 72 65 74 75 72 6e 20 53  Master) return S
af210 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67  QLITE_OK;.  pPag
af220 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20  er->setMaster = 
af230 31 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c  1;..  len = strl
af240 65 6e 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20 66  en(zMaster);.  f
af250 6f 72 28 69 3d 30 3b 20 69 3c 6c 65 6e 3b 20 69  or(i=0; i<len; i
af260 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b  ++){.    cksum +
af270 3d 20 7a 4d 61 73 74 65 72 5b 69 5d 3b 0a 20 20  = zMaster[i];.  
af280 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 75  }..  /* If in fu
af290 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64  ll-sync mode, ad
af2a0 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78  vance to the nex
af2b0 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62 65  t disk sector be
af2c0 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20 2a  fore writing.  *
af2d0 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  * the master jou
af2e0 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20  rnal name. This 
af2f0 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 20 70  is in case the p
af300 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 72 69  revious page wri
af310 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65  tten to.  ** the
af320 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c 72   journal has alr
af330 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64  eady been synced
af340 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
af350 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b  ger->fullSync ){
af360 0a 20 20 20 20 73 65 65 6b 4a 6f 75 72 6e 61 6c  .    seekJournal
af370 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  Hdr(pPager);.  }
af380 0a 20 20 6a 72 6e 6c 4f 66 66 20 3d 20 70 50 61  .  jrnlOff = pPa
af390 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
af3a0 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  .  pPager->journ
af3b0 61 6c 4f 66 66 20 2b 3d 20 28 6c 65 6e 2b 32 30  alOff += (len+20
af3c0 29 3b 0a 0a 20 20 72 63 20 3d 20 77 72 69 74 65  );..  rc = write
af3d0 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
af3e0 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 50 41 47  fd, jrnlOff, PAG
af3f0 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
af400 72 29 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  r));.  if( rc!=S
af410 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
af420 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20  n rc;.  jrnlOff 
af430 2b 3d 20 34 3b 0a 0a 20 20 72 63 20 3d 20 73 71  += 4;..  rc = sq
af440 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
af450 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65  ger->jfd, zMaste
af460 72 2c 20 6c 65 6e 2c 20 6a 72 6e 6c 4f 66 66 29  r, len, jrnlOff)
af470 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
af480 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
af490 63 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20  c;.  jrnlOff += 
af4a0 6c 65 6e 3b 0a 0a 20 20 70 75 74 33 32 62 69 74  len;..  put32bit
af4b0 73 28 7a 42 75 66 2c 20 6c 65 6e 29 3b 0a 20 20  s(zBuf, len);.  
af4c0 70 75 74 33 32 62 69 74 73 28 26 7a 42 75 66 5b  put32bits(&zBuf[
af4d0 34 5d 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 6d 65  4], cksum);.  me
af4e0 6d 63 70 79 28 26 7a 42 75 66 5b 38 5d 2c 20 61  mcpy(&zBuf[8], a
af4f0 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69  JournalMagic, si
af500 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
af510 69 63 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ic));.  rc = sql
af520 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
af530 65 72 2d 3e 6a 66 64 2c 20 7a 42 75 66 2c 20 38  er->jfd, zBuf, 8
af540 2b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  +sizeof(aJournal
af550 4d 61 67 69 63 29 2c 20 6a 72 6e 6c 4f 66 66 29  Magic), jrnlOff)
af560 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64  ;.  pPager->need
af570 53 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e  Sync = !pPager->
af580 6e 6f 53 79 6e 63 3b 0a 20 20 72 65 74 75 72 6e  noSync;.  return
af590 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64   rc;.}../*.** Ad
af5a0 64 20 6f 72 20 72 65 6d 6f 76 65 20 61 20 70 61  d or remove a pa
af5b0 67 65 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74  ge from the list
af5c0 20 6f 66 20 61 6c 6c 20 70 61 67 65 73 20 74 68   of all pages th
af5d0 61 74 20 61 72 65 20 69 6e 20 74 68 65 0a 2a 2a  at are in the.**
af5e0 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
af5f0 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 61  al..**.** The Pa
af600 67 65 72 20 6b 65 65 70 73 20 61 20 73 65 70 61  ger keeps a sepa
af610 72 61 74 65 20 6c 69 73 74 20 6f 66 20 70 61 67  rate list of pag
af620 65 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72  es that are curr
af630 65 6e 74 6c 79 20 69 6e 0a 2a 2a 20 74 68 65 20  ently in.** the 
af640 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
af650 6c 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74  l.  This helps t
af660 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  he sqlite3PagerS
af670 74 6d 74 43 6f 6d 6d 69 74 28 29 0a 2a 2a 20 72  tmtCommit().** r
af680 6f 75 74 69 6e 65 20 72 75 6e 20 4d 55 43 48 20  outine run MUCH 
af690 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 20 63  faster for the c
af6a0 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65  ommon case where
af6b0 20 74 68 65 72 65 20 61 72 65 20 6d 61 6e 79 0a   there are many.
af6c0 2a 2a 20 70 61 67 65 73 20 69 6e 20 6d 65 6d 6f  ** pages in memo
af6d0 72 79 20 62 75 74 20 6f 6e 6c 79 20 61 20 66 65  ry but only a fe
af6e0 77 20 61 72 65 20 69 6e 20 74 68 65 20 73 74 61  w are in the sta
af6f0 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a  tement journal..
af700 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
af710 61 67 65 5f 61 64 64 5f 74 6f 5f 73 74 6d 74 5f  age_add_to_stmt_
af720 6c 69 73 74 28 50 67 48 64 72 20 2a 70 50 67 29  list(PgHdr *pPg)
af730 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  {.  Pager *pPage
af740 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b  r = pPg->pPager;
af750 0a 20 20 50 67 48 69 73 74 6f 72 79 20 2a 70 48  .  PgHistory *pH
af760 69 73 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48  ist = PGHDR_TO_H
af770 49 53 54 28 70 50 67 2c 20 70 50 61 67 65 72 29  IST(pPg, pPager)
af780 3b 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d 44  ;.  assert( MEMD
af790 42 20 29 3b 0a 20 20 69 66 28 20 21 70 48 69 73  B );.  if( !pHis
af7a0 74 2d 3e 69 6e 53 74 6d 74 20 29 7b 0a 20 20 20  t->inStmt ){.   
af7b0 20 61 73 73 65 72 74 28 20 70 48 69 73 74 2d 3e   assert( pHist->
af7c0 70 50 72 65 76 53 74 6d 74 3d 3d 30 20 26 26 20  pPrevStmt==0 && 
af7d0 70 48 69 73 74 2d 3e 70 4e 65 78 74 53 74 6d 74  pHist->pNextStmt
af7e0 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ==0 );.    if( p
af7f0 50 61 67 65 72 2d 3e 70 53 74 6d 74 20 29 7b 0a  Pager->pStmt ){.
af800 20 20 20 20 20 20 50 47 48 44 52 5f 54 4f 5f 48        PGHDR_TO_H
af810 49 53 54 28 70 50 61 67 65 72 2d 3e 70 53 74 6d  IST(pPager->pStm
af820 74 2c 20 70 50 61 67 65 72 29 2d 3e 70 50 72 65  t, pPager)->pPre
af830 76 53 74 6d 74 20 3d 20 70 50 67 3b 0a 20 20 20  vStmt = pPg;.   
af840 20 7d 0a 20 20 20 20 70 48 69 73 74 2d 3e 70 4e   }.    pHist->pN
af850 65 78 74 53 74 6d 74 20 3d 20 70 50 61 67 65 72  extStmt = pPager
af860 2d 3e 70 53 74 6d 74 3b 0a 20 20 20 20 70 50 61  ->pStmt;.    pPa
af870 67 65 72 2d 3e 70 53 74 6d 74 20 3d 20 70 50 67  ger->pStmt = pPg
af880 3b 0a 20 20 20 20 70 48 69 73 74 2d 3e 69 6e 53  ;.    pHist->inS
af890 74 6d 74 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a  tmt = 1;.  }.}..
af8a0 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67  /*.** Find a pag
af8b0 65 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  e in the hash ta
af8c0 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20 70 61  ble given its pa
af8d0 67 65 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75  ge number.  Retu
af8e0 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
af8f0 74 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20 4e  to the page or N
af900 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  ULL if not found
af910 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64  ..*/.static PgHd
af920 72 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28  r *pager_lookup(
af930 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
af940 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48  gno pgno){.  PgH
af950 64 72 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 61  dr *p;.  if( pPa
af960 67 65 72 2d 3e 61 48 61 73 68 3d 3d 30 20 29 20  ger->aHash==0 ) 
af970 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20  return 0;.  p = 
af980 70 50 61 67 65 72 2d 3e 61 48 61 73 68 5b 70 67  pPager->aHash[pg
af990 6e 6f 20 26 20 28 70 50 61 67 65 72 2d 3e 6e 48  no & (pPager->nH
af9a0 61 73 68 2d 31 29 5d 3b 0a 20 20 77 68 69 6c 65  ash-1)];.  while
af9b0 28 20 70 20 26 26 20 70 2d 3e 70 67 6e 6f 21 3d  ( p && p->pgno!=
af9c0 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 20 3d 20  pgno ){.    p = 
af9d0 70 2d 3e 70 4e 65 78 74 48 61 73 68 3b 0a 20 20  p->pNextHash;.  
af9e0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
af9f0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68  ./*.** Unlock th
afa00 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
afa10 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
afa20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67  pager_unlock(Pag
afa30 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
afa40 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  f( !pPager->excl
afa50 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20  usiveMode ){.   
afa60 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20   if( !MEMDB ){. 
afa70 20 20 20 20 20 6f 73 55 6e 6c 6f 63 6b 28 70 50       osUnlock(pP
afa80 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43  ager->fd, NO_LOC
afa90 4b 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  K);.      pPager
afaa0 2d 3e 64 62 53 69 7a 65 20 3d 20 2d 31 3b 0a 20  ->dbSize = -1;. 
afab0 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 55       IOTRACE(("U
afac0 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61  NLOCK %p\n", pPa
afad0 67 65 72 29 29 0a 20 20 20 20 7d 0a 20 20 20 20  ger)).    }.    
afae0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
afaf0 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20  PAGER_UNLOCK;.  
afb00 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65    pPager->change
afb10 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20  CountDone = 0;. 
afb20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63   }.}../*.** Exec
afb30 75 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69  ute a rollback i
afb40 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
afb50 69 73 20 61 63 74 69 76 65 20 61 6e 64 20 75 6e  is active and un
afb60 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74  lock the .** dat
afb70 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73  abase file. This
afb80 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74   is a no-op if t
afb90 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72  he pager has alr
afba0 65 61 64 79 20 65 6e 74 65 72 65 64 0a 2a 2a 20  eady entered.** 
afbb0 74 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 2e  the error-state.
afbc0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
afbd0 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f  pagerUnlockAndRo
afbe0 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 29  llback(Pager *p)
afbf0 7b 0a 20 20 69 66 28 20 70 2d 3e 65 72 72 43 6f  {.  if( p->errCo
afc00 64 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  de ) return;.  a
afc10 73 73 65 72 74 28 20 70 2d 3e 73 74 61 74 65 3e  ssert( p->state>
afc20 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  =PAGER_RESERVED 
afc30 7c 7c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  || p->journalOpe
afc40 6e 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d  n==0 );.  if( p-
afc50 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  >state>=PAGER_RE
afc60 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 73 71  SERVED ){.    sq
afc70 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
afc80 63 6b 28 70 29 3b 0a 20 20 7d 0a 20 20 70 61 67  ck(p);.  }.  pag
afc90 65 72 5f 75 6e 6c 6f 63 6b 28 70 29 3b 0a 20 20  er_unlock(p);.  
afca0 61 73 73 65 72 74 28 20 70 2d 3e 65 72 72 43 6f  assert( p->errCo
afcb0 64 65 20 7c 7c 20 21 70 2d 3e 6a 6f 75 72 6e 61  de || !p->journa
afcc0 6c 4f 70 65 6e 20 7c 7c 20 28 70 2d 3e 65 78 63  lOpen || (p->exc
afcd0 6c 75 73 69 76 65 4d 6f 64 65 26 26 21 70 2d 3e  lusiveMode&&!p->
afce0 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 29 3b 0a 20  journalOff) );. 
afcf0 20 61 73 73 65 72 74 28 20 70 2d 3e 65 72 72 43   assert( p->errC
afd00 6f 64 65 20 7c 7c 20 21 70 2d 3e 73 74 6d 74 4f  ode || !p->stmtO
afd10 70 65 6e 20 7c 7c 20 70 2d 3e 65 78 63 6c 75 73  pen || p->exclus
afd20 69 76 65 4d 6f 64 65 20 29 3b 0a 7d 0a 0a 0a 2f  iveMode );.}.../
afd30 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69  *.** Clear the i
afd40 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20  n-memory cache. 
afd50 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
afd60 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20   sets the state 
afd70 6f 66 20 74 68 65 20 70 61 67 65 72 20 62 61 63  of the pager bac
afd80 6b 20 74 6f 20 77 68 61 74 20 69 74 20 77 61 73  k to what it was
afd90 20 77 68 65 6e 20 69 74 20 77 61 73 20 66 69 72   when it was fir
afda0 73 74 0a 2a 2a 20 6f 70 65 6e 65 64 2e 20 20 41  st.** opened.  A
afdb0 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  ny outstanding p
afdc0 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64  ages are invalid
afdd0 61 74 65 64 20 61 6e 64 20 73 75 62 73 65 71 75  ated and subsequ
afde0 65 6e 74 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20  ent attempts.** 
afdf0 74 6f 20 61 63 63 65 73 73 20 74 68 6f 73 65 20  to access those 
afe00 70 61 67 65 73 20 77 69 6c 6c 20 6c 69 6b 65 6c  pages will likel
afe10 79 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f  y result in a co
afe20 72 65 64 75 6d 70 2e 0a 2a 2f 0a 73 74 61 74 69  redump..*/.stati
afe30 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65 73  c void pager_res
afe40 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  et(Pager *pPager
afe50 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 2c  ){.  PgHdr *pPg,
afe60 20 2a 70 4e 65 78 74 3b 0a 20 20 69 66 28 20 70   *pNext;.  if( p
afe70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29  Pager->errCode )
afe80 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70   return;.  for(p
afe90 50 67 3d 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b  Pg=pPager->pAll;
afea0 20 70 50 67 3b 20 70 50 67 3d 70 4e 65 78 74 29   pPg; pPg=pNext)
afeb0 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  {.    IOTRACE(("
afec0 50 47 46 52 45 45 20 25 70 20 25 64 5c 6e 22 2c  PGFREE %p %d\n",
afed0 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67   pPager, pPg->pg
afee0 6e 6f 29 29 3b 0a 20 20 20 20 50 41 47 45 52 5f  no));.    PAGER_
afef0 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67  INCR(sqlite3_pag
aff00 65 72 5f 70 67 66 72 65 65 5f 63 6f 75 6e 74 29  er_pgfree_count)
aff10 3b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 50  ;.    pNext = pP
aff20 67 2d 3e 70 4e 65 78 74 41 6c 6c 3b 0a 20 20 20  g->pNextAll;.   
aff30 20 6c 72 75 4c 69 73 74 52 65 6d 6f 76 65 28 70   lruListRemove(p
aff40 50 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Pg);.    sqlite3
aff50 5f 66 72 65 65 28 70 50 67 2d 3e 70 44 61 74 61  _free(pPg->pData
aff60 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
aff70 72 65 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20  ree(pPg);.  }.  
aff80 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6c  assert(pPager->l
aff90 72 75 2e 70 46 69 72 73 74 3d 3d 30 29 3b 0a 20  ru.pFirst==0);. 
affa0 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e   assert(pPager->
affb0 6c 72 75 2e 70 46 69 72 73 74 53 79 6e 63 65 64  lru.pFirstSynced
affc0 3d 3d 30 29 3b 0a 20 20 61 73 73 65 72 74 28 70  ==0);.  assert(p
affd0 50 61 67 65 72 2d 3e 6c 72 75 2e 70 4c 61 73 74  Pager->lru.pLast
affe0 3d 3d 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ==0);.  pPager->
afff0 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 70 50 61  pStmt = 0;.  pPa
b0000 67 65 72 2d 3e 70 41 6c 6c 20 3d 20 30 3b 0a 20  ger->pAll = 0;. 
b0010 20 70 50 61 67 65 72 2d 3e 6e 48 61 73 68 20 3d   pPager->nHash =
b0020 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   0;.  sqlite3_fr
b0030 65 65 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68  ee(pPager->aHash
b0040 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 50 61  );.  pPager->nPa
b0050 67 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  ge = 0;.  pPager
b0060 2d 3e 61 48 61 73 68 20 3d 20 30 3b 0a 20 20 70  ->aHash = 0;.  p
b0070 50 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b  Pager->nRef = 0;
b0080 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
b0090 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 72  outine ends a tr
b00a0 61 6e 73 61 63 74 69 6f 6e 2e 20 20 41 20 74 72  ansaction.  A tr
b00b0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 6e 64  ansaction is end
b00c0 65 64 20 62 79 20 65 69 74 68 65 72 0a 2a 2a 20  ed by either.** 
b00d0 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f  a COMMIT or a RO
b00e0 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 20 57 68  LLBACK..**.** Wh
b00f0 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
b0100 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 70  is called, the p
b0110 61 67 65 72 20 68 61 73 20 74 68 65 20 6a 6f 75  ager has the jou
b0120 72 6e 61 6c 20 66 69 6c 65 20 6f 70 65 6e 20 61  rnal file open a
b0130 6e 64 0a 2a 2a 20 61 20 52 45 53 45 52 56 45 44  nd.** a RESERVED
b0140 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   or EXCLUSIVE lo
b0150 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
b0160 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  se.  This routin
b0170 65 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 0a 2a  e will release.*
b0180 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  * the database l
b0190 6f 63 6b 20 61 6e 64 20 61 63 71 75 69 72 65 73  ock and acquires
b01a0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 69   a SHARED lock i
b01b0 6e 20 69 74 73 20 70 6c 61 63 65 20 69 66 20 74  n its place if t
b01c0 68 61 74 20 69 73 0a 2a 2a 20 74 68 65 20 61 70  hat is.** the ap
b01d0 70 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20  propriate thing 
b01e0 74 6f 20 64 6f 2e 20 20 52 65 6c 65 61 73 65 20  to do.  Release 
b01f0 6c 6f 63 6b 73 20 75 73 75 61 6c 6c 79 20 69 73  locks usually is
b0200 20 61 70 70 72 6f 70 72 69 61 74 65 2c 0a 2a 2a   appropriate,.**
b0210 20 75 6e 6c 65 73 73 20 77 65 20 61 72 65 20 69   unless we are i
b0220 6e 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65  n exclusive acce
b0230 73 73 20 6d 6f 64 65 20 6f 72 20 75 6e 6c 65 73  ss mode or unles
b0240 73 20 74 68 69 73 20 69 73 20 61 20 0a 2a 2a 20  s this is a .** 
b0250 43 4f 4d 4d 49 54 20 41 4e 44 20 42 45 47 49 4e  COMMIT AND BEGIN
b0260 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 41 4e 44   or ROLLBACK AND
b0270 20 42 45 47 49 4e 20 6f 70 65 72 61 74 69 6f 6e   BEGIN operation
b0280 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72  ..**.** The jour
b0290 6e 61 6c 20 66 69 6c 65 20 69 73 20 65 69 74 68  nal file is eith
b02a0 65 72 20 64 65 6c 65 74 65 64 20 6f 72 20 74 72  er deleted or tr
b02b0 75 6e 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  uncated..**.** T
b02c0 4f 44 4f 3a 20 43 6f 6e 73 69 64 65 72 20 6b 65  ODO: Consider ke
b02d0 65 70 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61  eping the journa
b02e0 6c 20 66 69 6c 65 20 6f 70 65 6e 20 66 6f 72 20  l file open for 
b02f0 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
b0300 73 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6d 69 67  ses..** This mig
b0310 68 74 20 67 69 76 65 20 61 20 70 65 72 66 6f 72  ht give a perfor
b0320 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e  mance improvemen
b0330 74 20 6f 6e 20 77 69 6e 64 6f 77 73 20 77 68 65  t on windows whe
b0340 72 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 61 20  re opening.** a 
b0350 66 69 6c 65 20 69 73 20 61 6e 20 65 78 70 65 6e  file is an expen
b0360 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  sive operation..
b0370 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
b0380 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
b0390 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65  ion(Pager *pPage
b03a0 72 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  r){.  PgHdr *pPg
b03b0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
b03c0 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 63  ITE_OK;.  int rc
b03d0 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  2 = SQLITE_OK;. 
b03e0 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
b03f0 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
b0400 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53  >state<PAGER_RES
b0410 45 52 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74  ERVED ){.    ret
b0420 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
b0430 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65   }.  sqlite3Page
b0440 72 53 74 6d 74 43 6f 6d 6d 69 74 28 70 50 61 67  rStmtCommit(pPag
b0450 65 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  er);.  if( pPage
b0460 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 26 26 20 21  r->stmtOpen && !
b0470 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
b0480 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c  eMode ){.    sql
b0490 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
b04a0 65 72 2d 3e 73 74 66 64 29 3b 0a 20 20 20 20 70  er->stfd);.    p
b04b0 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20  Pager->stmtOpen 
b04c0 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  = 0;.  }.  if( p
b04d0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
b04e0 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  en ){.    if( pP
b04f0 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
b0500 6f 64 65 20 0a 20 20 20 20 20 20 20 20 20 20 26  ode .          &
b0510 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  & (rc = sqlite3O
b0520 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  sTruncate(pPager
b0530 2d 3e 6a 66 64 2c 20 30 29 29 3d 3d 53 51 4c 49  ->jfd, 0))==SQLI
b0540 54 45 5f 4f 4b 20 29 7b 3b 0a 20 20 20 20 20 20  TE_OK ){;.      
b0550 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
b0560 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ff = 0;.      pP
b0570 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
b0580 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65  rted = 0;.    }e
b0590 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
b05a0 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72  e3OsClose(pPager
b05b0 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50  ->jfd);.      pP
b05c0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
b05d0 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  n = 0;.      if(
b05e0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
b05f0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
b0600 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
b0610 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61  Pager->pVfs, pPa
b0620 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30  ger->zJournal, 0
b0630 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
b0640 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
b0650 65 28 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f  e( pPager->aInJo
b0660 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20 70 50 61  urnal );.    pPa
b0670 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 20  ger->aInJournal 
b0680 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 50 67  = 0;.    for(pPg
b0690 3d 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20 70  =pPager->pAll; p
b06a0 50 67 3b 20 70 50 67 3d 70 50 67 2d 3e 70 4e 65  Pg; pPg=pPg->pNe
b06b0 78 74 41 6c 6c 29 7b 0a 20 20 20 20 20 20 70 50  xtAll){.      pP
b06c0 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30  g->inJournal = 0
b06d0 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e 64 69 72  ;.      pPg->dir
b06e0 74 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ty = 0;.      pP
b06f0 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  g->needSync = 0;
b0700 0a 20 20 20 20 20 20 70 50 67 2d 3e 61 6c 77 61  .      pPg->alwa
b0710 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a  ysRollback = 0;.
b0720 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
b0730 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 20 20  ECK_PAGES.      
b0740 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20  pPg->pageHash = 
b0750 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
b0760 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  Pg);.#endif.    
b0770 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 44  }.    pPager->pD
b0780 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 70 50  irty = 0;.    pP
b0790 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65  ager->dirtyCache
b07a0 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
b07b0 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 7d 65  ->nRec = 0;.  }e
b07c0 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
b07d0 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72   pPager->aInJour
b07e0 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  nal==0 );.    as
b07f0 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 69  sert( pPager->di
b0800 72 74 79 43 61 63 68 65 3d 3d 30 20 7c 7c 20 70  rtyCache==0 || p
b0810 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61  Pager->useJourna
b0820 6c 3d 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  l==0 );.  }..  i
b0830 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  f( !pPager->excl
b0840 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20  usiveMode ){.   
b0850 20 72 63 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28   rc2 = osUnlock(
b0860 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52  pPager->fd, SHAR
b0870 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50  ED_LOCK);.    pP
b0880 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41  ager->state = PA
b0890 47 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 7d 65  GER_SHARED;.  }e
b08a0 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e  lse if( pPager->
b08b0 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e  state==PAGER_SYN
b08c0 43 45 44 20 29 7b 0a 20 20 20 20 70 50 61 67 65  CED ){.    pPage
b08d0 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
b08e0 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a  _EXCLUSIVE;.  }.
b08f0 20 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62    pPager->origDb
b0900 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 50 61 67  Size = 0;.  pPag
b0910 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20  er->setMaster = 
b0920 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65  0;.  pPager->nee
b0930 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 6c 72 75  dSync = 0;.  lru
b0940 4c 69 73 74 53 65 74 46 69 72 73 74 53 79 6e 63  ListSetFirstSync
b0950 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 70 50  ed(pPager);.  pP
b0960 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 2d  ager->dbSize = -
b0970 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63  1;..  return (rc
b0980 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a  ==SQLITE_OK?rc2:
b0990 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  rc);.}../*.** Co
b09a0 6d 70 75 74 65 20 61 6e 64 20 72 65 74 75 72 6e  mpute and return
b09b0 20 61 20 63 68 65 63 6b 73 75 6d 20 66 6f 72 20   a checksum for 
b09c0 74 68 65 20 70 61 67 65 20 6f 66 20 64 61 74 61  the page of data
b09d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
b09e0 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b  not a real check
b09f0 73 75 6d 2e 20 20 49 74 20 69 73 20 72 65 61 6c  sum.  It is real
b0a00 6c 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20  ly just the sum 
b0a10 6f 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f  of the .** rando
b0a20 6d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20  m initial value 
b0a30 61 6e 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d  and the page num
b0a40 62 65 72 2e 20 20 57 65 20 65 78 70 65 72 69 6d  ber.  We experim
b0a50 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20  ented with.** a 
b0a60 63 68 65 63 6b 73 75 6d 20 6f 66 20 74 68 65 20  checksum of the 
b0a70 65 6e 74 69 72 65 20 64 61 74 61 2c 20 62 75 74  entire data, but
b0a80 20 74 68 61 74 20 77 61 73 20 66 6f 75 6e 64 20   that was found 
b0a90 74 6f 20 62 65 20 74 6f 6f 20 73 6c 6f 77 2e 0a  to be too slow..
b0aa0 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
b0ab0 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
b0ac0 69 73 20 73 74 6f 72 65 64 20 61 74 20 74 68 65  is stored at the
b0ad0 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61   beginning of da
b0ae0 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 68  ta and.** the ch
b0af0 65 63 6b 73 75 6d 20 69 73 20 73 74 6f 72 65 64  ecksum is stored
b0b00 20 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68   at the end.  Th
b0b10 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e  is is important.
b0b20 20 20 49 66 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20    If journal.** 
b0b30 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72  corruption occur
b0b40 73 20 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72  s due to a power
b0b50 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f   failure, the mo
b0b60 73 74 20 6c 69 6b 65 6c 79 20 73 63 65 6e 61 72  st likely scenar
b0b70 69 6f 0a 2a 2a 20 69 73 20 74 68 61 74 20 6f 6e  io.** is that on
b0b80 65 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68  e end or the oth
b0b90 65 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  er of the record
b0ba0 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64   will be changed
b0bb0 2e 20 20 49 74 20 69 73 0a 2a 2a 20 6d 75 63 68  .  It is.** much
b0bc0 20 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61   less likely tha
b0bd0 74 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f  t the two ends o
b0be0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65  f the journal re
b0bf0 63 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20  cord will be.** 
b0c00 63 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20  correct and the 
b0c10 6d 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70  middle be corrup
b0c20 74 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22  t.  Thus, this "
b0c30 63 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65  checksum" scheme
b0c40 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74  ,.** though fast
b0c50 20 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74   and simple, cat
b0c60 63 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20  ches the mostly 
b0c70 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63  likely kind of c
b0c80 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  orruption..**.**
b0c90 20 46 49 58 20 4d 45 3a 20 20 43 6f 6e 73 69 64   FIX ME:  Consid
b0ca0 65 72 20 61 64 64 69 6e 67 20 65 76 65 72 79 20  er adding every 
b0cb0 32 30 30 74 68 20 28 6f 72 20 73 6f 29 20 62 79  200th (or so) by
b0cc0 74 65 20 6f 66 20 74 68 65 20 64 61 74 61 20 74  te of the data t
b0cd0 6f 20 74 68 65 0a 2a 2a 20 63 68 65 63 6b 73 75  o the.** checksu
b0ce0 6d 2e 20 20 54 68 61 74 20 77 61 79 20 69 66 20  m.  That way if 
b0cf0 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 73 70  a single page sp
b0d00 61 6e 73 20 33 20 6f 72 20 6d 6f 72 65 20 64 69  ans 3 or more di
b0d10 73 6b 20 73 65 63 74 6f 72 73 20 61 6e 64 0a 2a  sk sectors and.*
b0d20 2a 20 6f 6e 6c 79 20 74 68 65 20 6d 69 64 64 6c  * only the middl
b0d30 65 20 73 65 63 74 6f 72 20 69 73 20 63 6f 72 72  e sector is corr
b0d40 75 70 74 2c 20 77 65 20 77 69 6c 6c 20 73 74 69  upt, we will sti
b0d50 6c 6c 20 68 61 76 65 20 61 20 72 65 61 73 6f 6e  ll have a reason
b0d60 61 62 6c 65 0a 2a 2a 20 63 68 61 6e 63 65 20 6f  able.** chance o
b0d70 66 20 66 61 69 6c 69 6e 67 20 74 68 65 20 63 68  f failing the ch
b0d80 65 63 6b 73 75 6d 20 61 6e 64 20 74 68 75 73 20  ecksum and thus 
b0d90 64 65 74 65 63 74 69 6e 67 20 74 68 65 20 70 72  detecting the pr
b0da0 6f 62 6c 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63  oblem..*/.static
b0db0 20 75 33 32 20 70 61 67 65 72 5f 63 6b 73 75 6d   u32 pager_cksum
b0dc0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
b0dd0 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61 29  const u8 *aData)
b0de0 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20  {.  u32 cksum = 
b0df0 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
b0e00 74 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61  t;.  int i = pPa
b0e10 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30  ger->pageSize-20
b0e20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20  0;.  while( i>0 
b0e30 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20  ){.    cksum += 
b0e40 61 44 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20  aData[i];.    i 
b0e50 2d 3d 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65  -= 200;.  }.  re
b0e60 74 75 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f  turn cksum;.}../
b0e70 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
b0e80 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20  ation */.static 
b0e90 76 6f 69 64 20 6d 61 6b 65 43 6c 65 61 6e 28 50  void makeClean(P
b0ea0 67 48 64 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52  gHdr*);../*.** R
b0eb0 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70 61 67  ead a single pag
b0ec0 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  e from the journ
b0ed0 61 6c 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 6f  al file opened o
b0ee0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
b0ef0 72 0a 2a 2a 20 6a 66 64 2e 20 20 50 6c 61 79 62  r.** jfd.  Playb
b0f00 61 63 6b 20 74 68 69 73 20 6f 6e 65 20 70 61 67  ack this one pag
b0f10 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 75 73 65 43  e..**.** If useC
b0f20 6b 73 75 6d 3d 3d 30 20 69 74 20 6d 65 61 6e 73  ksum==0 it means
b0f30 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 64 6f   this journal do
b0f40 65 73 20 6e 6f 74 20 75 73 65 20 63 68 65 63 6b  es not use check
b0f50 73 75 6d 73 2e 20 20 43 68 65 63 6b 73 75 6d 73  sums.  Checksums
b0f60 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 75 73 65 64  .** are not used
b0f70 20 69 6e 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f   in statement jo
b0f80 75 72 6e 61 6c 73 20 62 65 63 61 75 73 65 20 73  urnals because s
b0f90 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
b0fa0 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 65 65 64  s do not.** need
b0fb0 20 74 6f 20 73 75 72 76 69 76 65 20 70 6f 77 65   to survive powe
b0fc0 72 20 66 61 69 6c 75 72 65 73 2e 0a 2a 2f 0a 73  r failures..*/.s
b0fd0 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
b0fe0 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67  playback_one_pag
b0ff0 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  e(.  Pager *pPag
b1000 65 72 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66  er, .  sqlite3_f
b1010 69 6c 65 20 2a 6a 66 64 2c 0a 20 20 69 36 34 20  ile *jfd,.  i64 
b1020 6f 66 66 73 65 74 2c 0a 20 20 69 6e 74 20 75 73  offset,.  int us
b1030 65 43 6b 73 75 6d 0a 29 7b 0a 20 20 69 6e 74 20  eCksum.){.  int 
b1040 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  rc;.  PgHdr *pPg
b1050 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b1060 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 74 69      /* An existi
b1070 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  ng page in the c
b1080 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  ache */.  Pgno p
b1090 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  gno;            
b10a0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
b10b0 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20  age number of a 
b10c0 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c 20  page in journal 
b10d0 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20  */.  u32 cksum; 
b10e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b10f0 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 75     /* Checksum u
b1100 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 20 63  sed for sanity c
b1110 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 38 20  hecking */.  u8 
b1120 2a 61 44 61 74 61 20 3d 20 28 75 38 20 2a 29 70  *aData = (u8 *)p
b1130 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
b1140 3b 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72  ;   /* Temp stor
b1150 61 67 65 20 66 6f 72 20 61 20 70 61 67 65 20 2a  age for a page *
b1160 2f 0a 0a 20 20 2f 2a 20 75 73 65 43 6b 73 75 6d  /..  /* useCksum
b1170 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 65 20   should be true 
b1180 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75  for the main jou
b1190 72 6e 61 6c 20 61 6e 64 20 66 61 6c 73 65 20 66  rnal and false f
b11a0 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  or.  ** statemen
b11b0 74 20 6a 6f 75 72 6e 61 6c 73 2e 20 20 56 65 72  t journals.  Ver
b11c0 69 66 79 20 74 68 61 74 20 74 68 69 73 20 69 73  ify that this is
b11d0 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65   always the case
b11e0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
b11f0 6a 66 64 20 3d 3d 20 28 75 73 65 43 6b 73 75 6d  jfd == (useCksum
b1200 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a   ? pPager->jfd :
b1210 20 70 50 61 67 65 72 2d 3e 73 74 66 64 29 20 29   pPager->stfd) )
b1220 3b 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 74  ;.  assert( aDat
b1230 61 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61  a );..  rc = rea
b1240 64 33 32 62 69 74 73 28 6a 66 64 2c 20 6f 66 66  d32bits(jfd, off
b1250 73 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69  set, &pgno);.  i
b1260 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
b1270 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
b1280 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
b1290 61 64 28 6a 66 64 2c 20 61 44 61 74 61 2c 20 70  ad(jfd, aData, p
b12a0 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
b12b0 20 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 69 66   offset+4);.  if
b12c0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
b12d0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70  ) return rc;.  p
b12e0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
b12f0 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67  f += pPager->pag
b1300 65 53 69 7a 65 20 2b 20 34 3b 0a 0a 20 20 2f 2a  eSize + 4;..  /*
b1310 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   Sanity checking
b1320 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20 20 54   on the page.  T
b1330 68 69 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 6f  his is more impo
b1340 72 74 61 6e 74 20 74 68 61 74 20 49 20 6f 72 69  rtant that I ori
b1350 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f  ginally.  ** tho
b1360 75 67 68 74 2e 20 20 49 66 20 61 20 70 6f 77 65  ught.  If a powe
b1370 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73  r failure occurs
b1380 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e   while the journ
b1390 61 6c 20 69 73 20 62 65 69 6e 67 20 77 72 69 74  al is being writ
b13a0 74 65 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f 75  ten,.  ** it cou
b13b0 6c 64 20 63 61 75 73 65 20 69 6e 76 61 6c 69 64  ld cause invalid
b13c0 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 74   data to be writ
b13d0 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ten into the jou
b13e0 72 6e 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 74  rnal.  We need t
b13f0 6f 0a 20 20 2a 2a 20 64 65 74 65 63 74 20 74 68  o.  ** detect th
b1400 69 73 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20  is invalid data 
b1410 28 77 69 74 68 20 68 69 67 68 20 70 72 6f 62 61  (with high proba
b1420 62 69 6c 69 74 79 29 20 61 6e 64 20 69 67 6e 6f  bility) and igno
b1430 72 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  re it..  */.  if
b1440 28 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e  ( pgno==0 || pgn
b1450 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  o==PAGER_MJ_PGNO
b1460 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  (pPager) ){.    
b1470 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
b1480 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67  NE;.  }.  if( pg
b1490 6e 6f 3e 28 75 6e 73 69 67 6e 65 64 29 70 50 61  no>(unsigned)pPa
b14a0 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 7b 0a 20  ger->dbSize ){. 
b14b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
b14c0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75  _OK;.  }.  if( u
b14d0 73 65 43 6b 73 75 6d 20 29 7b 0a 20 20 20 20 72  seCksum ){.    r
b14e0 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a  c = read32bits(j
b14f0 66 64 2c 20 6f 66 66 73 65 74 2b 70 50 61 67 65  fd, offset+pPage
b1500 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 2c 20 26  r->pageSize+4, &
b1510 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69 66 28 20  cksum);.    if( 
b1520 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
b1530 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
b1540 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20 20 20  nalOff += 4;.   
b1550 20 69 66 28 20 70 61 67 65 72 5f 63 6b 73 75 6d   if( pager_cksum
b1560 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 29 21  (pPager, aData)!
b1570 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20  =cksum ){.      
b1580 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
b1590 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  NE;.    }.  }.. 
b15a0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
b15b0 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45  >state==PAGER_RE
b15c0 53 45 52 56 45 44 20 7c 7c 20 70 50 61 67 65 72  SERVED || pPager
b15d0 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45  ->state>=PAGER_E
b15e0 58 43 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f  XCLUSIVE );..  /
b15f0 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
b1600 73 20 69 6e 20 52 45 53 45 52 56 45 44 20 73 74  s in RESERVED st
b1610 61 74 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20  ate, then there 
b1620 6d 75 73 74 20 62 65 20 61 20 63 6f 70 79 20 6f  must be a copy o
b1630 66 20 74 68 69 73 0a 20 20 2a 2a 20 70 61 67 65  f this.  ** page
b1640 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61   in the pager ca
b1650 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  che. In this cas
b1660 65 20 6a 75 73 74 20 75 70 64 61 74 65 20 74 68  e just update th
b1670 65 20 70 61 67 65 72 20 63 61 63 68 65 2c 0a 20  e pager cache,. 
b1680 20 2a 2a 20 6e 6f 74 20 74 68 65 20 64 61 74 61   ** not the data
b1690 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 70  base file. The p
b16a0 61 67 65 20 69 73 20 6c 65 66 74 20 6d 61 72 6b  age is left mark
b16b0 65 64 20 64 69 72 74 79 20 69 6e 20 74 68 69 73  ed dirty in this
b16c0 20 63 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   case..  **.  **
b16d0 20 41 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f   An exception to
b16e0 20 74 68 65 20 61 62 6f 76 65 20 72 75 6c 65 3a   the above rule:
b16f0 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
b1700 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d   is in no-sync m
b1710 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70  ode.  ** and a p
b1720 61 67 65 20 69 73 20 6d 6f 76 65 64 20 64 75 72  age is moved dur
b1730 69 6e 67 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74  ing an increment
b1740 61 6c 20 76 61 63 75 75 6d 20 74 68 65 6e 20 74  al vacuum then t
b1750 68 65 20 70 61 67 65 20 6d 61 79 0a 20 20 2a 2a  he page may.  **
b1760 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20 70   not be in the p
b1770 61 67 65 72 20 63 61 63 68 65 2e 20 4c 61 74 65  ager cache. Late
b1780 72 3a 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  r: if a malloc()
b1790 20 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   or IO error occ
b17a0 75 72 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20  urs.  ** during 
b17b0 61 20 4d 6f 76 65 70 61 67 65 28 29 20 63 61 6c  a Movepage() cal
b17c0 6c 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 65  l, then the page
b17d0 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 20 74   may not be in t
b17e0 68 65 20 63 61 63 68 65 0a 20 20 2a 2a 20 65 69  he cache.  ** ei
b17f0 74 68 65 72 2e 20 53 6f 20 74 68 65 20 63 6f 6e  ther. So the con
b1800 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64  dition described
b1810 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 70 61   in the above pa
b1820 72 61 67 72 61 70 68 20 69 73 20 6e 6f 74 0a 20  ragraph is not. 
b1830 20 2a 2a 20 61 73 73 65 72 74 28 29 61 62 6c 65   ** assert()able
b1840 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69  ..  **.  ** If i
b1850 6e 20 45 58 43 4c 55 53 49 56 45 20 73 74 61 74  n EXCLUSIVE stat
b1860 65 2c 20 74 68 65 6e 20 77 65 20 75 70 64 61 74  e, then we updat
b1870 65 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  e the pager cach
b1880 65 20 69 66 20 69 74 20 65 78 69 73 74 73 0a 20  e if it exists. 
b1890 20 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 69 6e   ** and the main
b18a0 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20   file. The page 
b18b0 69 73 20 74 68 65 6e 20 6d 61 72 6b 65 64 20 6e  is then marked n
b18c0 6f 74 20 64 69 72 74 79 2e 0a 20 20 2a 2a 0a 20  ot dirty..  **. 
b18d0 20 2a 2a 20 54 69 63 6b 65 74 20 23 31 31 37 31   ** Ticket #1171
b18e0 3a 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74  :  The statement
b18f0 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 63   journal might c
b1900 6f 6e 74 61 69 6e 20 70 61 67 65 20 63 6f 6e 74  ontain page cont
b1910 65 6e 74 20 74 68 61 74 20 69 73 0a 20 20 2a 2a  ent that is.  **
b1920 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
b1930 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74  the page content
b1940 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
b1950 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
b1960 2e 0a 20 20 2a 2a 20 54 68 69 73 20 6f 63 63 75  ..  ** This occu
b1970 72 73 20 77 68 65 6e 20 61 20 70 61 67 65 20 69  rs when a page i
b1980 73 20 63 68 61 6e 67 65 64 20 70 72 69 6f 72 20  s changed prior 
b1990 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
b19a0 61 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a  a statement.  **
b19b0 20 74 68 65 6e 20 63 68 61 6e 67 65 64 20 61 67   then changed ag
b19c0 61 69 6e 20 77 69 74 68 69 6e 20 74 68 65 20 73  ain within the s
b19d0 74 61 74 65 6d 65 6e 74 2e 20 20 57 68 65 6e 20  tatement.  When 
b19e0 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 73 75 63  rolling back suc
b19f0 68 20 61 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  h a.  ** stateme
b1a00 6e 74 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 77  nt we must not w
b1a10 72 69 74 65 20 74 6f 20 74 68 65 20 6f 72 69 67  rite to the orig
b1a20 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 75 6e  inal database un
b1a30 6c 65 73 73 20 77 65 20 6b 6e 6f 77 0a 20 20 2a  less we know.  *
b1a40 2a 20 66 6f 72 20 63 65 72 74 61 69 6e 20 74 68  * for certain th
b1a50 61 74 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  at original page
b1a60 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 20 73 79   contents are sy
b1a70 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6d 61  nced into the ma
b1a80 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a  in rollback.  **
b1a90 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 74 68 65 72   journal.  Other
b1aa0 77 69 73 65 2c 20 61 20 70 6f 77 65 72 20 6c 6f  wise, a power lo
b1ab0 73 73 20 6d 69 67 68 74 20 6c 65 61 76 65 20 6d  ss might leave m
b1ac0 6f 64 69 66 69 65 64 20 64 61 74 61 20 69 6e 20  odified data in 
b1ad0 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
b1ae0 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 61  e file without a
b1af0 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72  n entry in the r
b1b00 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
b1b10 74 68 61 74 20 63 61 6e 0a 20 20 2a 2a 20 72 65  that can.  ** re
b1b20 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61  store the databa
b1b30 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e  se to its origin
b1b40 61 6c 20 66 6f 72 6d 2e 20 20 54 77 6f 20 63 6f  al form.  Two co
b1b50 6e 64 69 74 69 6f 6e 73 20 6d 75 73 74 20 62 65  nditions must be
b1b60 0a 20 20 2a 2a 20 6d 65 74 20 62 65 66 6f 72 65  .  ** met before
b1b70 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
b1b80 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20  database files. 
b1b90 28 31 29 20 74 68 65 20 64 61 74 61 62 61 73 65  (1) the database
b1ba0 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6c 6f   must be.  ** lo
b1bb0 63 6b 65 64 2e 20 20 28 32 29 20 77 65 20 6b 6e  cked.  (2) we kn
b1bc0 6f 77 20 74 68 61 74 20 74 68 65 20 6f 72 69 67  ow that the orig
b1bd0 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e  inal page conten
b1be0 74 20 69 73 20 66 75 6c 6c 79 20 73 79 6e 63 65  t is fully synce
b1bf0 64 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6d 61  d.  ** in the ma
b1c00 69 6e 20 6a 6f 75 72 6e 61 6c 20 65 69 74 68 65  in journal eithe
b1c10 72 20 62 65 63 61 75 73 65 20 74 68 65 20 70 61  r because the pa
b1c20 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63  ge is not in cac
b1c30 68 65 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20  he or else.  ** 
b1c40 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b  the page is mark
b1c50 65 64 20 61 73 20 6e 65 65 64 53 79 6e 63 3d 3d  ed as needSync==
b1c60 30 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 20 3d 20  0..  */.  pPg = 
b1c70 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61  pager_lookup(pPa
b1c80 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 50 41  ger, pgno);.  PA
b1c90 47 45 52 54 52 41 43 45 34 28 22 50 4c 41 59 42  GERTRACE4("PLAYB
b1ca0 41 43 4b 20 25 64 20 70 61 67 65 20 25 64 20 68  ACK %d page %d h
b1cb0 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20  ash(%08x)\n",.  
b1cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47               PAG
b1cd0 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67  ERID(pPager), pg
b1ce0 6e 6f 2c 20 70 61 67 65 72 5f 64 61 74 61 68 61  no, pager_dataha
b1cf0 73 68 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53  sh(pPager->pageS
b1d00 69 7a 65 2c 20 61 44 61 74 61 29 29 3b 0a 20 20  ize, aData));.  
b1d10 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
b1d20 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49  e>=PAGER_EXCLUSI
b1d30 56 45 20 26 26 20 28 70 50 67 3d 3d 30 20 7c 7c  VE && (pPg==0 ||
b1d40 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 3d 3d   pPg->needSync==
b1d50 30 29 20 29 7b 0a 20 20 20 20 69 36 34 20 6f 66  0) ){.    i64 of
b1d60 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a  fset = (pgno-1)*
b1d70 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67  (i64)pPager->pag
b1d80 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20  eSize;.    rc = 
b1d90 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
b1da0 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74 61  Pager->fd, aData
b1db0 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
b1dc0 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 20  ze, offset);.   
b1dd0 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20   if( pPg ){.    
b1de0 20 20 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29    makeClean(pPg)
b1df0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
b1e00 28 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20  ( pPg ){.    /* 
b1e10 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65  No page should e
b1e20 76 65 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c  ver be explicitl
b1e30 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68  y rolled back th
b1e40 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78  at is in use, ex
b1e50 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20  cept.    ** for 
b1e60 70 61 67 65 20 31 20 77 68 69 63 68 20 69 73 20  page 1 which is 
b1e70 68 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f  held in use in o
b1e80 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65  rder to keep the
b1e90 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20   lock on the.   
b1ea0 20 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 74   ** database act
b1eb0 69 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63  ive. However suc
b1ec0 68 20 61 20 70 61 67 65 20 6d 61 79 20 62 65 20  h a page may be 
b1ed0 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61  rolled back as a
b1ee0 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f   result.    ** o
b1ef0 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72  f an internal er
b1f00 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e  ror resulting in
b1f10 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61   an automatic ca
b1f20 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c  ll to.    ** sql
b1f30 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
b1f40 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  k()..    */.    
b1f50 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20  void *pData;.   
b1f60 20 2f 2a 20 61 73 73 65 72 74 28 20 70 50 67 2d   /* assert( pPg-
b1f70 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 50 67 2d  >nRef==0 || pPg-
b1f80 3e 70 67 6e 6f 3d 3d 31 20 29 3b 20 2a 2f 0a 20  >pgno==1 ); */. 
b1f90 20 20 20 70 44 61 74 61 20 3d 20 50 47 48 44 52     pData = PGHDR
b1fa0 5f 54 4f 5f 44 41 54 41 28 70 50 67 29 3b 0a 20  _TO_DATA(pPg);. 
b1fb0 20 20 20 6d 65 6d 63 70 79 28 70 44 61 74 61 2c     memcpy(pData,
b1fc0 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e   aData, pPager->
b1fd0 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69  pageSize);.    i
b1fe0 66 28 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e  f( pPager->xRein
b1ff0 69 74 65 72 20 29 7b 0a 20 20 20 20 20 20 70 50  iter ){.      pP
b2000 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28  ager->xReiniter(
b2010 70 50 67 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  pPg, pPager->pag
b2020 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 69  eSize);.    }.#i
b2030 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
b2040 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d  K_PAGES.    pPg-
b2050 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65  >pageHash = page
b2060 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b  r_pagehash(pPg);
b2070 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 49  .#endif.    /* I
b2080 66 20 74 68 69 73 20 77 61 73 20 70 61 67 65 20  f this was page 
b2090 31 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20  1, then restore 
b20a0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67  the value of Pag
b20b0 65 72 2e 64 62 46 69 6c 65 56 65 72 73 2e 0a 20  er.dbFileVers.. 
b20c0 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 62 65     ** Do this be
b20d0 66 6f 72 65 20 61 6e 79 20 64 65 63 6f 64 69 6e  fore any decodin
b20e0 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67  g. */.    if( pg
b20f0 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d  no==1 ){.      m
b2100 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64  emcpy(&pPager->d
b2110 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75 38  bFileVers, &((u8
b2120 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a  *)pData)[24],siz
b2130 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69  eof(pPager->dbFi
b2140 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a  leVers));.    }.
b2150 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 65 20 74  .    /* Decode t
b2160 68 65 20 70 61 67 65 20 6a 75 73 74 20 72 65 61  he page just rea
b2170 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20  d from disk */. 
b2180 20 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72     CODEC1(pPager
b2190 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67  , pData, pPg->pg
b21a0 6e 6f 2c 20 33 29 3b 0a 20 20 7d 0a 20 20 72 65  no, 3);.  }.  re
b21b0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
b21c0 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4d 61 73  * Parameter zMas
b21d0 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ter is the name 
b21e0 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  of a master jour
b21f0 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 69 6e 67  nal file. A sing
b2200 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69  le journal.** fi
b2210 6c 65 20 74 68 61 74 20 72 65 66 65 72 72 65 64  le that referred
b2220 20 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20 6a   to the master j
b2230 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20  ournal file has 
b2240 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64  just been rolled
b2250 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72   back..** This r
b2260 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
b2270 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
b2280 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61  to delete the ma
b2290 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
b22a0 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73  e,.** and does s
b22b0 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a  o if it is..**.*
b22c0 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d 61 73 74  * Argument zMast
b22d0 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20  er may point to 
b22e0 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 2e  Pager.pTmpSpace.
b22f0 20 53 6f 20 74 68 61 74 20 62 75 66 66 65 72 20   So that buffer 
b2300 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 6c  is not .** avail
b2310 61 62 6c 65 20 66 6f 72 20 75 73 65 20 77 69 74  able for use wit
b2320 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hin this functio
b2330 6e 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  n..**.**.** The 
b2340 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
b2350 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
b2360 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63 68   names of all ch
b2370 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a  ild journals..**
b2380 20 54 6f 20 74 65 6c 6c 20 69 66 20 61 20 6d 61   To tell if a ma
b2390 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 63 61 6e  ster journal can
b23a0 20 62 65 20 64 65 6c 65 74 65 64 2c 20 63 68 65   be deleted, che
b23b0 63 6b 20 74 6f 20 65 61 63 68 20 6f 66 20 74 68  ck to each of th
b23c0 65 0a 2a 2a 20 63 68 69 6c 64 72 65 6e 2e 20 20  e.** children.  
b23d0 49 66 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20  If all children 
b23e0 61 72 65 20 65 69 74 68 65 72 20 6d 69 73 73 69  are either missi
b23f0 6e 67 20 6f 72 20 64 6f 20 6e 6f 74 20 72 65 66  ng or do not ref
b2400 65 72 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66 65  er to.** a diffe
b2410 72 65 6e 74 20 6d 61 73 74 65 72 20 6a 6f 75 72  rent master jour
b2420 6e 61 6c 2c 20 74 68 65 6e 20 74 68 69 73 20 6d  nal, then this m
b2430 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 63 61  aster journal ca
b2440 6e 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a 2f  n be deleted..*/
b2450 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
b2460 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67 65  r_delmaster(Page
b2470 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74  r *pPager, const
b2480 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b   char *zMaster){
b2490 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
b24a0 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70  pVfs = pPager->p
b24b0 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  Vfs;.  int rc;. 
b24c0 20 69 6e 74 20 6d 61 73 74 65 72 5f 6f 70 65 6e   int master_open
b24d0 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
b24e0 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 3b 0a 20  file *pMaster;. 
b24f0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
b2500 4a 6f 75 72 6e 61 6c 3b 0a 20 20 63 68 61 72 20  Journal;.  char 
b2510 2a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20  *zMasterJournal 
b2520 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73  = 0; /* Contents
b2530 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   of master journ
b2540 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34  al file */.  i64
b2550 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b   nMasterJournal;
b2560 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
b2570 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  f master journal
b2580 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 4f   file */..  /* O
b2590 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a  pen the master j
b25a0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 63 6c  ournal file excl
b25b0 75 73 69 76 65 6c 79 20 69 6e 20 63 61 73 65 20  usively in case 
b25c0 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65  some other proce
b25d0 73 73 0a 20 20 2a 2a 20 69 73 20 72 75 6e 6e 69  ss.  ** is runni
b25e0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ng this routine 
b25f0 61 6c 73 6f 2e 20 4e 6f 74 20 74 68 61 74 20 69  also. Not that i
b2600 74 20 6d 61 6b 65 73 20 74 6f 6f 20 6d 75 63 68  t makes too much
b2610 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20 2a   difference..  *
b2620 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28 73  /.  pMaster = (s
b2630 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71  qlite3_file *)sq
b2640 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 56 66  lite3_malloc(pVf
b2650 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29  s->szOsFile * 2)
b2660 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28  ;.  pJournal = (
b2670 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28  sqlite3_file *)(
b2680 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20  ((u8 *)pMaster) 
b2690 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  + pVfs->szOsFile
b26a0 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65  );.  if( !pMaste
b26b0 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  r ){.    rc = SQ
b26c0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
b26d0 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 66 6c 61  lse{.    int fla
b26e0 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45  gs = (SQLITE_OPE
b26f0 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54  N_READONLY|SQLIT
b2700 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
b2710 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 20 3d  URNAL);.    rc =
b2720 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
b2730 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4d  Vfs, zMaster, pM
b2740 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 30 29  aster, flags, 0)
b2750 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d  ;.  }.  if( rc!=
b2760 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
b2770 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
b2780 20 20 6d 61 73 74 65 72 5f 6f 70 65 6e 20 3d 20    master_open = 
b2790 31 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  1;..  rc = sqlit
b27a0 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4d 61  e3OsFileSize(pMa
b27b0 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f  ster, &nMasterJo
b27c0 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 20 72 63  urnal);.  if( rc
b27d0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
b27e0 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  to delmaster_out
b27f0 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73 74 65 72  ;..  if( nMaster
b2800 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a 20 20 20  Journal>0 ){.   
b2810 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b   char *zJournal;
b2820 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  .    char *zMast
b2830 65 72 50 74 72 20 3d 20 30 3b 0a 20 20 20 20 69  erPtr = 0;.    i
b2840 6e 74 20 6e 4d 61 73 74 65 72 50 74 72 20 3d 20  nt nMasterPtr = 
b2850 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78  pPager->pVfs->mx
b2860 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20  Pathname+1;..   
b2870 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74   /* Load the ent
b2880 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  ire master journ
b2890 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61  al file into spa
b28a0 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ce obtained from
b28b0 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  .    ** sqlite3_
b28c0 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69  malloc() and poi
b28d0 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74  nted to by zMast
b28e0 65 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20  erJournal. .    
b28f0 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f  */.    zMasterJo
b2900 75 72 6e 61 6c 20 3d 20 28 63 68 61 72 20 2a 29  urnal = (char *)
b2910 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
b2920 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 2b 20  MasterJournal + 
b2930 6e 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20  nMasterPtr);.   
b2940 20 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75   if( !zMasterJou
b2950 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63  rnal ){.      rc
b2960 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
b2970 0a 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d  .      goto delm
b2980 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d  aster_out;.    }
b2990 0a 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20  .    zMasterPtr 
b29a0 3d 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61  = &zMasterJourna
b29b0 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  l[nMasterJournal
b29c0 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ];.    rc = sqli
b29d0 74 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 65  te3OsRead(pMaste
b29e0 72 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61  r, zMasterJourna
b29f0 6c 2c 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61  l, nMasterJourna
b2a00 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  l, 0);.    if( r
b2a10 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
b2a20 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75  oto delmaster_ou
b2a30 74 3b 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c  t;..    zJournal
b2a40 20 3d 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61   = zMasterJourna
b2a50 6c 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a  l;.    while( (z
b2a60 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a  Journal-zMasterJ
b2a70 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a  ournal)<nMasterJ
b2a80 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20  ournal ){.      
b2a90 69 66 28 20 73 71 6c 69 74 65 33 4f 73 41 63 63  if( sqlite3OsAcc
b2aa0 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e  ess(pVfs, zJourn
b2ab0 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53  al, SQLITE_ACCES
b2ac0 53 5f 45 58 49 53 54 53 29 20 29 7b 0a 20 20 20  S_EXISTS) ){.   
b2ad0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
b2ae0 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f 69 6e  he journals poin
b2af0 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 6d 61  ted to by the ma
b2b00 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 78 69  ster journal exi
b2b10 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20  sts..        ** 
b2b20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68 65 63  Open it and chec
b2b30 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73 20 61  k if it points a
b2b40 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  t the master jou
b2b50 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 20 20  rnal. If.       
b2b60 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e 20 77   ** so, return w
b2b70 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e 67 20  ithout deleting 
b2b80 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
b2b90 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20  al file..       
b2ba0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20   */.        int 
b2bb0 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 66  c;.        int f
b2bc0 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f  lags = (SQLITE_O
b2bd0 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c  PEN_READONLY|SQL
b2be0 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
b2bf0 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 20 20  URNAL);.        
b2c00 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
b2c10 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61  en(pVfs, zJourna
b2c20 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c 61  l, pJournal, fla
b2c30 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  gs, 0);.        
b2c40 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
b2c50 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  K ){.          g
b2c60 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75  oto delmaster_ou
b2c70 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  t;.        }..  
b2c80 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d        rc = readM
b2c90 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a 6f  asterJournal(pJo
b2ca0 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50 74  urnal, zMasterPt
b2cb0 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b 0a  r, nMasterPtr);.
b2cc0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
b2cd0 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c 29  sClose(pJournal)
b2ce0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
b2cf0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
b2d00 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65           goto de
b2d10 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20  lmaster_out;.   
b2d20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
b2d30 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 5b 30  c = zMasterPtr[0
b2d40 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70 28 7a  ]!=0 && strcmp(z
b2d50 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 73 74  MasterPtr, zMast
b2d60 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20  er)==0;.        
b2d70 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20 20 20  if( c ){.       
b2d80 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 20     /* We have a 
b2d90 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 64 65  match. Do not de
b2da0 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  lete the master 
b2db0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f  journal file. */
b2dc0 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
b2dd0 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20  delmaster_out;. 
b2de0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
b2df0 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20  .      zJournal 
b2e00 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 4a 6f 75 72  += (strlen(zJour
b2e10 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20  nal)+1);.    }. 
b2e20 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c   }.  .  rc = sql
b2e30 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
b2e40 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a  s, zMaster, 0);.
b2e50 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a  .delmaster_out:.
b2e60 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75    if( zMasterJou
b2e70 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  rnal ){.    sqli
b2e80 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72  te3_free(zMaster
b2e90 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a  Journal);.  }  .
b2ea0 20 20 69 66 28 20 6d 61 73 74 65 72 5f 6f 70 65    if( master_ope
b2eb0 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  n ){.    sqlite3
b2ec0 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 29  OsClose(pMaster)
b2ed0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
b2ee0 66 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20  free(pMaster);. 
b2ef0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
b2f00 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
b2f10 72 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65  r_truncate_cache
b2f20 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b  (Pager *pPager);
b2f30 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65  ../*.** Truncate
b2f40 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 20 6f   the main file o
b2f50 66 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65  f the given page
b2f60 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  r to the number 
b2f70 6f 66 20 70 61 67 65 73 0a 2a 2a 20 69 6e 64 69  of pages.** indi
b2f80 63 61 74 65 64 2e 20 41 6c 73 6f 20 74 72 75 6e  cated. Also trun
b2f90 63 61 74 65 20 74 68 65 20 63 61 63 68 65 64 20  cate the cached 
b2fa0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
b2fb0 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  f the file..*/.s
b2fc0 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
b2fd0 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 20 2a  truncate(Pager *
b2fe0 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 50 61 67  pPager, int nPag
b2ff0 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
b3000 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
b3010 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
b3020 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 26  AGER_EXCLUSIVE &
b3030 26 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d  & pPager->fd->pM
b3040 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 63  ethods ){.    rc
b3050 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e   = sqlite3OsTrun
b3060 63 61 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c  cate(pPager->fd,
b3070 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
b3080 65 2a 28 69 36 34 29 6e 50 61 67 65 29 3b 0a 20  e*(i64)nPage);. 
b3090 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
b30a0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50  ITE_OK ){.    pP
b30b0 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e  ager->dbSize = n
b30c0 50 61 67 65 3b 0a 20 20 20 20 70 61 67 65 72 5f  Page;.    pager_
b30d0 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65 28 70  truncate_cache(p
b30e0 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65  Pager);.  }.  re
b30f0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
b3100 2a 20 53 65 74 20 74 68 65 20 73 65 63 74 6f 72  * Set the sector
b3110 53 69 7a 65 20 66 6f 72 20 74 68 65 20 67 69 76  Size for the giv
b3120 65 6e 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20  en pager..**.** 
b3130 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  The sector size 
b3140 69 73 20 74 68 65 20 6c 61 72 67 65 72 20 6f 66  is the larger of
b3150 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
b3160 20 72 65 70 6f 72 74 65 64 0a 2a 2a 20 62 79 20   reported.** by 
b3170 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53  sqlite3OsSectorS
b3180 69 7a 65 28 29 20 61 6e 64 20 74 68 65 20 70 61  ize() and the pa
b3190 67 65 53 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  geSize..*/.stati
b31a0 63 20 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72  c void setSector
b31b0 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67  Size(Pager *pPag
b31c0 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 70 50  er){.  assert(pP
b31d0 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f  ager->fd->pMetho
b31e0 64 73 7c 7c 70 50 61 67 65 72 2d 3e 74 65 6d 70  ds||pPager->temp
b31f0 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 50  File);.  if( !pP
b3200 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
b3210 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20  {.    /* Sector 
b3220 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 74  size doesn't mat
b3230 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 72  ter for temporar
b3240 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 74  y files. Also, t
b3250 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 6d  he file.    ** m
b3260 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e  ay not have been
b3270 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e 20   opened yet, in 
b3280 77 68 63 69 68 20 63 61 73 65 20 74 68 65 20 4f  whcih case the O
b3290 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20  sSectorSize().  
b32a0 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 73    ** call will s
b32b0 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a  egfault..    */.
b32c0 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74      pPager->sect
b32d0 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33  orSize = sqlite3
b32e0 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61  OsSectorSize(pPa
b32f0 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 20  ger->fd);.  }.  
b3300 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74  if( pPager->sect
b3310 6f 72 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 70  orSize<pPager->p
b3320 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 70  ageSize ){.    p
b3330 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
b3340 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65  e = pPager->page
b3350 53 69 7a 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  Size;.  }.}../*.
b3360 2a 2a 20 50 6c 61 79 62 61 63 6b 20 74 68 65 20  ** Playback the 
b3370 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 75 73  journal and thus
b3380 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74   restore the dat
b3390 61 62 61 73 65 20 66 69 6c 65 20 74 6f 0a 2a 2a  abase file to.**
b33a0 20 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61   the state it wa
b33b0 73 20 69 6e 20 62 65 66 6f 72 65 20 77 65 20 73  s in before we s
b33c0 74 61 72 74 65 64 20 6d 61 6b 69 6e 67 20 63 68  tarted making ch
b33d0 61 6e 67 65 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  anges.  .**.** T
b33e0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
b33f0 66 6f 72 6d 61 74 20 69 73 20 61 73 20 66 6f 6c  format is as fol
b3400 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31  lows: .**.**  (1
b3410 29 20 20 38 20 62 79 74 65 20 70 72 65 66 69 78  )  8 byte prefix
b3420 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 61 4a 6f  .  A copy of aJo
b3430 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a  urnalMagic[]..**
b3440 20 20 28 32 29 20 20 34 20 62 79 74 65 20 62 69    (2)  4 byte bi
b3450 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
b3460 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75   which is the nu
b3470 6d 62 65 72 20 6f 66 20 76 61 6c 69 64 20 70 61  mber of valid pa
b3480 67 65 20 72 65 63 6f 72 64 73 0a 2a 2a 20 20 20  ge records.**   
b3490 20 20 20 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e      in the journ
b34a0 61 6c 2e 20 20 49 66 20 74 68 69 73 20 76 61 6c  al.  If this val
b34b0 75 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66  ue is 0xffffffff
b34c0 2c 20 74 68 65 6e 20 63 6f 6d 70 75 74 65 20 74  , then compute t
b34d0 68 65 0a 2a 2a 20 20 20 20 20 20 20 6e 75 6d 62  he.**       numb
b34e0 65 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f 72  er of page recor
b34f0 64 73 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ds from the jour
b3500 6e 61 6c 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 33  nal size..**  (3
b3510 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e  )  4 byte big-en
b3520 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  dian integer whi
b3530 63 68 20 69 73 20 74 68 65 20 69 6e 69 74 69 61  ch is the initia
b3540 6c 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  l value for the 
b3550 0a 2a 2a 20 20 20 20 20 20 20 73 61 6e 69 74 79  .**       sanity
b3560 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28   checksum..**  (
b3570 34 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67  4)  4 byte integ
b3580 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20  er which is the 
b3590 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
b35a0 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a  to truncate the.
b35b0 2a 2a 20 20 20 20 20 20 20 64 61 74 61 62 61 73  **       databas
b35c0 65 20 74 6f 20 64 75 72 69 6e 67 20 61 20 72 6f  e to during a ro
b35d0 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 20 28 35 29 20  llback..**  (5) 
b35e0 20 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20   4 byte integer 
b35f0 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d  which is the num
b3600 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
b3610 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
b3620 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65  al.**       name
b3630 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79  .  The value may
b3640 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61   be zero (indica
b3650 74 65 20 74 68 61 74 20 74 68 65 72 65 20 69 73  te that there is
b3660 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20   no master.**   
b3670 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a      journal.).**
b3680 20 20 28 36 29 20 20 4e 20 62 79 74 65 73 20 6f    (6)  N bytes o
b3690 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  f the master jou
b36a0 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20  rnal name.  The 
b36b0 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c  name will be nul
b36c0 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20  -terminated.**  
b36d0 20 20 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62       and might b
b36e0 65 20 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74  e shorter than t
b36f0 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
b3700 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 68 65 20  om (5).  If the 
b3710 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20  first byte.**   
b3720 20 20 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20      of the name 
b3730 69 73 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65  is \000 then the
b3740 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20  re is no master 
b3750 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61  journal.  The ma
b3760 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f  ster.**       jo
b3770 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74  urnal name is st
b3780 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a  ored in UTF-8..*
b3790 2a 20 20 28 37 29 20 20 5a 65 72 6f 20 6f 72 20  *  (7)  Zero or 
b37a0 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 61  more pages insta
b37b0 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f  nces, each as fo
b37c0 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20  llows:.**       
b37d0 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 20   +  4 byte page 
b37e0 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20  number..**      
b37f0 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67    +  pPager->pag
b3800 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64  eSize bytes of d
b3810 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b  ata..**        +
b3820 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 75    4 byte checksu
b3830 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20  m.**.** When we 
b3840 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75  speak of the jou
b3850 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20  rnal header, we 
b3860 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 36  mean the first 6
b3870 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a   items above..**
b3880 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74   Each entry in t
b3890 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e  he journal is an
b38a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
b38b0 20 37 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a   7th item..**.**
b38c0 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20   Call the value 
b38d0 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20  from the second 
b38e0 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20  bullet "nRec".  
b38f0 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62  nRec is the numb
b3900 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70  er of.** valid p
b3910 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74  age entries in t
b3920 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20  he journal.  In 
b3930 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20  most cases, you 
b3940 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a  can compute the.
b3950 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63  ** value of nRec
b3960 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f   from the size o
b3970 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
b3980 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f  le.  But if a po
b3990 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f  wer.** failure o
b39a0 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 68  ccurred while th
b39b0 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65  e journal was be
b39c0 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20  ing written, it 
b39d0 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20  could be the.** 
b39e0 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 69  case that the si
b39f0 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ze of the journa
b3a00 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61  l file had alrea
b3a10 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65  dy been increase
b3a20 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74  d but.** the ext
b3a30 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e  ra entries had n
b3a40 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 73  ot yet made it s
b3a50 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20  afely to disk.  
b3a60 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a  In such a case,.
b3a70 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
b3a80 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72  nRec computed fr
b3a90 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65  om the file size
b3aa0 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61   would be too la
b3ab0 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61  rge.  For.** tha
b3ac0 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77  t reason, we alw
b3ad0 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 63  ays use the nRec
b3ae0 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65   value in the he
b3af0 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ader..**.** If t
b3b00 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73  he nRec value is
b3b10 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 6d   0xffffffff it m
b3b20 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73  eans that nRec s
b3b30 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65  hould be compute
b3b40 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69  d.** from the fi
b3b50 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76  le size.  This v
b3b60 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 65  alue is used whe
b3b70 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 63  n the user selec
b3b80 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e  ts the.** no-syn
b3b90 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65  c option for the
b3ba0 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77   journal.  A pow
b3bb0 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64  er failure could
b3bc0 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74   lead to corrupt
b3bd0 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63  ion.** in this c
b3be0 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68  ase.  But for th
b3bf0 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72  ings like tempor
b3c00 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 68  ary table (which
b3c10 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65   will be.** dele
b3c20 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77  ted when the pow
b3c30 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20  er is restored) 
b3c40 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20  we don't care.  
b3c50 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
b3c60 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65  le opened as the
b3c70 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
b3c80 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d   not a well-form
b3c90 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  ed.** journal fi
b3ca0 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65  le then all page
b3cb0 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73  s up to the firs
b3cc0 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 65  t corrupted page
b3cd0 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62   are rolled.** b
b3ce0 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73  ack (or no pages
b3cf0 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   if the journal 
b3d00 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 70  header is corrup
b3d10 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61  ted). The journa
b3d20 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65  l file.** is the
b3d30 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51  n deleted and SQ
b3d40 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
b3d50 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20  , just as if no 
b3d60 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a  corruption had.*
b3d70 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72  * been encounter
b3d80 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
b3d90 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20  I/O or malloc() 
b3da0 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
b3db0 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69  e journal-file i
b3dc0 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a  s not deleted.**
b3dd0 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f   and an error co
b3de0 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
b3df0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
b3e00 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67  ger_playback(Pag
b3e10 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
b3e20 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65  isHot){.  sqlite
b3e30 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50  3_vfs *pVfs = pP
b3e40 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36  ager->pVfs;.  i6
b3e50 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20  4 szJ;          
b3e60 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
b3e70 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
b3e80 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  le in bytes */. 
b3e90 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20   u32 nRec;      
b3ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
b3eb0 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69  ber of Records i
b3ec0 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f  n the journal */
b3ed0 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
b3ee0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
b3ef0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
b3f00 20 50 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20   Pgno mxPg = 0; 
b3f10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
b3f20 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  e of the origina
b3f30 6c 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20  l file in pages 
b3f40 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
b3f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
b3f60 20 52 65 73 75 6c 74 20 63 6f 64 65 20 6f 66 20   Result code of 
b3f70 61 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  a subroutine */.
b3f80 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20    char *zMaster 
b3f90 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61  = 0;       /* Na
b3fa0 6d 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75  me of master jou
b3fb0 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 61 6e 79  rnal file if any
b3fc0 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65   */..  /* Figure
b3fd0 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65   out how many re
b3fe0 63 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68 65  cords are in the
b3ff0 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74   journal.  Abort
b4000 20 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74   early if.  ** t
b4010 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d  he journal is em
b4020 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  pty..  */.  asse
b4030 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  rt( pPager->jour
b4040 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 72 63 20  nalOpen );.  rc 
b4050 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
b4060 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ize(pPager->jfd,
b4070 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63   &szJ);.  if( rc
b4080 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73  !=SQLITE_OK || s
b4090 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74  zJ==0 ){.    got
b40a0 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
b40b0 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74    }..  /* Read t
b40c0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
b40d0 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20  l name from the 
b40e0 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69  journal, if it i
b40f0 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20  s present..  ** 
b4100 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  If a master jour
b4110 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73  nal file name is
b4120 20 73 70 65 63 69 66 69 65 64 2c 20 62 75 74 20   specified, but 
b4130 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a  the file is not.
b4140 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20    ** present on 
b4150 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a  disk, then the j
b4160 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f  ournal is not ho
b4170 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e  t and does not n
b4180 65 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70  eed to be.  ** p
b4190 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2f  layed back..  */
b41a0 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61  .  zMaster = pPa
b41b0 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a  ger->pTmpSpace;.
b41c0 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65    rc = readMaste
b41d0 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d  rJournal(pPager-
b41e0 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70  >jfd, zMaster, p
b41f0 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50  Pager->pVfs->mxP
b4200 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 61 73  athname+1);.  as
b4210 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
b4220 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 72  _DONE );.  if( r
b4230 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 0a 20 20  c!=SQLITE_OK .  
b4240 20 7c 7c 20 28 7a 4d 61 73 74 65 72 5b 30 5d 20   || (zMaster[0] 
b4250 26 26 20 21 73 71 6c 69 74 65 33 4f 73 41 63 63  && !sqlite3OsAcc
b4260 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65  ess(pVfs, zMaste
b4270 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  r, SQLITE_ACCESS
b4280 5f 45 58 49 53 54 53 29 29 20 0a 20 20 29 7b 0a  _EXISTS)) .  ){.
b4290 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b      zMaster = 0;
b42a0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
b42b0 49 54 45 5f 44 4f 4e 45 20 29 20 72 63 20 3d 20  ITE_DONE ) rc = 
b42c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67  SQLITE_OK;.    g
b42d0 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b  oto end_playback
b42e0 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e  ;.  }.  pPager->
b42f0 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
b4300 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 0a    zMaster = 0;..
b4310 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74    /* This loop t
b4320 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72  erminates either
b4330 20 77 68 65 6e 20 74 68 65 20 72 65 61 64 4a 6f   when the readJo
b4340 75 72 6e 61 6c 48 64 72 28 29 20 63 61 6c 6c 20  urnalHdr() call 
b4350 72 65 74 75 72 6e 73 0a 20 20 2a 2a 20 53 51 4c  returns.  ** SQL
b4360 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49  ITE_DONE or an I
b4370 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20  O error occurs. 
b4380 2a 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b  */.  while( 1 ){
b4390 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68  ..    /* Read th
b43a0 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68  e next journal h
b43b0 65 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a  eader from the j
b43c0 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66  ournal file.  If
b43d0 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a   there are.    *
b43e0 2a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74  * not enough byt
b43f0 65 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a  es left in the j
b4400 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20  ournal file for 
b4410 61 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65  a complete heade
b4420 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20  r, or.    ** it 
b4430 69 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68  is corrupted, th
b4440 65 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73  en a process mus
b4450 74 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c  t of failed whil
b4460 65 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20  e writing it..  
b4470 20 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61    ** This indica
b4480 74 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65  tes nothing more
b4490 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c   needs to be rol
b44a0 6c 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f  led back..    */
b44b0 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f  .    rc = readJo
b44c0 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c  urnalHdr(pPager,
b44d0 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d 78   szJ, &nRec, &mx
b44e0 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  Pg);.    if( rc!
b44f0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a 20  =SQLITE_OK ){ . 
b4500 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
b4510 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
b4520 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
b4530 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OK;.      }.    
b4540 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62    goto end_playb
b4550 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ack;.    }..    
b4560 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 78  /* If nRec is 0x
b4570 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 74  ffffffff, then t
b4580 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  his journal was 
b4590 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f  created by a pro
b45a0 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 6b  cess.    ** work
b45b0 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d  ing in no-sync m
b45c0 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  ode. This means 
b45d0 74 68 61 74 20 74 68 65 20 72 65 73 74 20 6f 66  that the rest of
b45e0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20   the journal.   
b45f0 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 74   ** file consist
b4600 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 72  s of pages, ther
b4610 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f  e are no more jo
b4620 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 43  urnal headers. C
b4630 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 68  ompute.    ** th
b4640 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20  e value of nRec 
b4650 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 73  based on this as
b4660 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  sumption..    */
b4670 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30  .    if( nRec==0
b4680 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20  xffffffff ){.   
b4690 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
b46a0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a  r->journalOff==J
b46b0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
b46c0 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 6e  ager) );.      n
b46d0 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 4a 4f 55  Rec = (szJ - JOU
b46e0 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
b46f0 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f  er))/JOURNAL_PG_
b4700 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  SZ(pPager);.    
b4710 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65  }..    /* If nRe
b4720 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73 20  c is 0 and this 
b4730 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20 61  rollback is of a
b4740 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72 65   transaction cre
b4750 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20 20  ated by this.   
b4760 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e 64 20   ** process and 
b4770 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
b4780 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 74  inal header in t
b4790 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e  he journal, then
b47a0 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a   it means.    **
b47b0 20 74 68 61 74 20 74 68 69 73 20 70 61 72 74 20   that this part 
b47c0 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  of the journal w
b47d0 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 64 20  as being filled 
b47e0 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20  but has not yet 
b47f0 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 6e 63  been.    ** sync
b4800 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 6f 6d  ed to disk.  Com
b4810 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  pute the number 
b4820 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 20 6f  of pages based o
b4830 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 0a  n the remaining.
b4840 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 74      ** size of t
b4850 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a  he file..    **.
b4860 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 72 64      ** The third
b4870 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 65 73   term of the tes
b4880 74 20 77 61 73 20 61 64 64 65 64 20 74 6f 20 66  t was added to f
b4890 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e  ix ticket #2565.
b48a0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
b48b0 6e 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f  nRec==0 && !isHo
b48c0 74 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61  t &&.        pPa
b48d0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b  ger->journalHdr+
b48e0 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
b48f0 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e  Pager)==pPager->
b4900 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20  journalOff ){.  
b4910 20 20 20 20 6e 52 65 63 20 3d 20 28 73 7a 4a 20      nRec = (szJ 
b4920 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  - pPager->journa
b4930 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f  lOff) / JOURNAL_
b4940 50 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  PG_SZ(pPager);. 
b4950 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
b4960 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
b4970 74 20 68 65 61 64 65 72 20 72 65 61 64 20 66 72  t header read fr
b4980 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20  om the journal, 
b4990 74 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20  truncate the.   
b49a0 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
b49b0 65 20 62 61 63 6b 20 74 6f 20 69 74 27 73 20 6f  e back to it's o
b49c0 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20  riginal size..  
b49d0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61    */.    if( pPa
b49e0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
b49f0 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  =JOURNAL_HDR_SZ(
b4a00 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20  pPager) ){.     
b4a10 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e   rc = pager_trun
b4a20 63 61 74 65 28 70 50 61 67 65 72 2c 20 6d 78 50  cate(pPager, mxP
b4a30 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  g);.      if( rc
b4a40 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
b4a50 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
b4a60 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20  playback;.      
b4a70 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
b4a80 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61  Copy original pa
b4a90 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a  ges out of the j
b4aa0 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20  ournal and back 
b4ab0 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
b4ac0 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20  e file..    */. 
b4ad0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52     for(i=0; i<nR
b4ae0 65 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ec; i++){.      
b4af0 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62  rc = pager_playb
b4b00 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61  ack_one_page(pPa
b4b10 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  ger, pPager->jfd
b4b20 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  , pPager->journa
b4b30 6c 4f 66 66 2c 20 31 29 3b 0a 20 20 20 20 20 20  lOff, 1);.      
b4b40 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
b4b50 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  K ){.        if(
b4b60 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
b4b70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
b4b80 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
b4b90 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
b4ba0 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a  journalOff = szJ
b4bb0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
b4bc0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  k;.        }else
b4bd0 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
b4be0 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20   end_playback;. 
b4bf0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
b4c00 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e  .    }.  }.  /*N
b4c10 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 73  OTREACHED*/.  as
b4c20 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f  sert( 0 );..end_
b4c30 70 6c 61 79 62 61 63 6b 3a 0a 20 20 69 66 28 20  playback:.  if( 
b4c40 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
b4c50 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70  .    zMaster = p
b4c60 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
b4c70 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d  ;.    rc = readM
b4c80 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61  asterJournal(pPa
b4c90 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65  ger->jfd, zMaste
b4ca0 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d  r, pPager->pVfs-
b4cb0 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a  >mxPathname+1);.
b4cc0 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
b4cd0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
b4ce0 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  c = pager_end_tr
b4cf0 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
b4d00 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
b4d10 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d  =SQLITE_OK && zM
b4d20 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20  aster[0] ){.    
b4d30 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20  /* If there was 
b4d40 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
b4d50 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
b4d60 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75  e will return su
b4d70 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65  ccess,.    ** se
b4d80 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69  e if it is possi
b4d90 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68  ble to delete th
b4da0 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
b4db0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
b4dc0 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65  = pager_delmaste
b4dd0 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65  r(pPager, zMaste
b4de0 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  r);.  }..  /* Th
b4df0 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69  e Pager.sectorSi
b4e00 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20  ze variable may 
b4e10 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65  have been update
b4e20 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a  d while rolling.
b4e30 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72    ** back a jour
b4e40 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 20 61  nal created by a
b4e50 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 61 20   process with a 
b4e60 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f 72  different sector
b4e70 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65   size.  ** value
b4e80 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 74 68  . Reset it to th
b4e90 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 20  e correct value 
b4ea0 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73  for this process
b4eb0 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 74  ..  */.  setSect
b4ec0 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a  orSize(pPager);.
b4ed0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
b4ee0 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 74  /*.** Playback t
b4ef0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  he statement jou
b4f00 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rnal..**.** This
b4f10 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 70   is similar to p
b4f20 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20  laying back the 
b4f30 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72  transaction jour
b4f40 6e 61 6c 20 62 75 74 20 77 69 74 68 0a 2a 2a 20  nal but with.** 
b4f50 61 20 66 65 77 20 65 78 74 72 61 20 74 77 69 73  a few extra twis
b4f60 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29  ts..**.**    (1)
b4f70 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20    The number of 
b4f80 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
b4f90 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68  abase file at th
b4fa0 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 20 20  e start of.**   
b4fb0 20 20 20 20 20 20 74 68 65 20 73 74 61 74 65 6d        the statem
b4fc0 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ent is stored in
b4fd0 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a   pPager->stmtSiz
b4fe0 65 2c 20 6e 6f 74 20 69 6e 20 74 68 65 0a 2a 2a  e, not in the.**
b4ff0 20 20 20 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c           journal
b5000 20 66 69 6c 65 20 69 74 73 65 6c 66 2e 0a 2a 2a   file itself..**
b5010 0a 2a 2a 20 20 20 20 28 32 29 20 20 49 6e 20 61  .**    (2)  In a
b5020 64 64 69 74 69 6f 6e 20 74 6f 20 70 6c 61 79 69  ddition to playi
b5030 6e 67 20 62 61 63 6b 20 74 68 65 20 73 74 61 74  ng back the stat
b5040 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2c 20 61  ement journal, a
b5050 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 70  lso.**         p
b5060 6c 61 79 62 61 63 6b 20 61 6c 6c 20 70 61 67 65  layback all page
b5070 73 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  s of the transac
b5080 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 62 65 67  tion journal beg
b5090 69 6e 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  inning.**       
b50a0 20 20 61 74 20 6f 66 66 73 65 74 20 70 50 61 67    at offset pPag
b50b0 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 2e 0a 2a  er->stmtJSize..*
b50c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
b50d0 65 72 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b  er_stmt_playback
b50e0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
b50f0 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20  .  i64 szJ;     
b5100 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
b5110 69 7a 65 20 6f 66 20 74 68 65 20 66 75 6c 6c 20  ize of the full 
b5120 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34  journal */.  i64
b5130 20 68 64 72 4f 66 66 3b 0a 20 20 69 6e 74 20 6e   hdrOff;.  int n
b5140 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
b5150 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
b5160 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69 6e   Records */.  in
b5170 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
b5180 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
b5190 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
b51a0 72 63 3b 0a 0a 20 20 73 7a 4a 20 3d 20 70 50 61  rc;..  szJ = pPa
b51b0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
b51c0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20  .#ifndef NDEBUG 
b51d0 0a 20 20 7b 0a 20 20 20 20 69 36 34 20 6f 73 5f  .  {.    i64 os_
b51e0 73 7a 4a 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  szJ;.    rc = sq
b51f0 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
b5200 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6f 73  pPager->jfd, &os
b5210 5f 73 7a 4a 29 3b 0a 20 20 20 20 69 66 28 20 72  _szJ);.    if( r
b5220 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
b5230 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 61 73  eturn rc;.    as
b5240 73 65 72 74 28 20 73 7a 4a 3d 3d 6f 73 5f 73 7a  sert( szJ==os_sz
b5250 4a 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  J );.  }.#endif.
b5260 0a 20 20 2f 2a 20 53 65 74 20 68 64 72 4f 66 66  .  /* Set hdrOff
b5270 20 74 6f 20 62 65 20 74 68 65 20 6f 66 66 73 65   to be the offse
b5280 74 20 6a 75 73 74 20 61 66 74 65 72 20 74 68 65  t just after the
b5290 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74   end of the last
b52a0 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 70 61   journal.  ** pa
b52b0 67 65 20 77 72 69 74 74 65 6e 20 62 65 66 6f 72  ge written befor
b52c0 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72  e the first jour
b52d0 6e 61 6c 2d 68 65 61 64 65 72 20 66 6f 72 20 74  nal-header for t
b52e0 68 69 73 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  his statement.  
b52f0 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  ** transaction w
b5300 61 73 20 77 72 69 74 74 65 6e 2c 20 6f 72 20 74  as written, or t
b5310 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69  he end of the fi
b5320 6c 65 20 69 66 20 6e 6f 20 6a 6f 75 72 6e 61 6c  le if no journal
b5330 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77 61 73  .  ** header was
b5340 20 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20   written..  */. 
b5350 20 68 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72   hdrOff = pPager
b5360 2d 3e 73 74 6d 74 48 64 72 4f 66 66 3b 0a 20 20  ->stmtHdrOff;.  
b5370 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b5380 66 75 6c 6c 53 79 6e 63 20 7c 7c 20 21 68 64 72  fullSync || !hdr
b5390 4f 66 66 20 29 3b 0a 20 20 69 66 28 20 21 68 64  Off );.  if( !hd
b53a0 72 4f 66 66 20 29 7b 0a 20 20 20 20 68 64 72 4f  rOff ){.    hdrO
b53b0 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a 20 20  ff = szJ;.  }.  
b53c0 0a 20 20 2f 2a 20 54 72 75 6e 63 61 74 65 20 74  .  /* Truncate t
b53d0 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 6b  he database back
b53e0 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c   to its original
b53f0 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72 63   size..  */.  rc
b5400 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74   = pager_truncat
b5410 65 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  e(pPager, pPager
b5420 2d 3e 73 74 6d 74 53 69 7a 65 29 3b 0a 20 20 61  ->stmtSize);.  a
b5430 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
b5440 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
b5450 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75  ED );..  /* Figu
b5460 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
b5470 72 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20 74  records are in t
b5480 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  he statement jou
b5490 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  rnal..  */.  ass
b54a0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 6d  ert( pPager->stm
b54b0 74 49 6e 55 73 65 20 26 26 20 70 50 61 67 65 72  tInUse && pPager
b54c0 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b  ->journalOpen );
b54d0 0a 20 20 6e 52 65 63 20 3d 20 70 50 61 67 65 72  .  nRec = pPager
b54e0 2d 3e 73 74 6d 74 4e 52 65 63 3b 0a 20 20 0a 20  ->stmtNRec;.  . 
b54f0 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e 61   /* Copy origina
b5500 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74  l pages out of t
b5510 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  he statement jou
b5520 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 69 6e  rnal and back in
b5530 74 6f 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  to the.  ** data
b5540 62 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f 74 65  base file.  Note
b5550 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d   that the statem
b5560 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6f 6d 69 74  ent journal omit
b5570 73 20 63 68 65 63 6b 73 75 6d 73 20 66 72 6f 6d  s checksums from
b5580 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63 6f 72  .  ** each recor
b5590 64 20 73 69 6e 63 65 20 70 6f 77 65 72 2d 66 61  d since power-fa
b55a0 69 6c 75 72 65 20 72 65 63 6f 76 65 72 79 20 69  ilure recovery i
b55b0 73 20 6e 6f 74 20 69 6d 70 6f 72 74 61 6e 74 20  s not important 
b55c0 74 6f 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a  to statement.  *
b55d0 2a 20 6a 6f 75 72 6e 61 6c 73 2e 0a 20 20 2a 2f  * journals..  */
b55e0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52  .  for(i=0; i<nR
b55f0 65 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 36  ec; i++){.    i6
b5600 34 20 6f 66 66 73 65 74 20 3d 20 69 2a 28 34 2b  4 offset = i*(4+
b5610 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
b5620 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  );.    rc = page
b5630 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
b5640 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 61 67  age(pPager, pPag
b5650 65 72 2d 3e 73 74 66 64 2c 20 6f 66 66 73 65 74  er->stfd, offset
b5660 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , 0);.    assert
b5670 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
b5680 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  E );.    if( rc!
b5690 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
b56a0 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62  o end_stmt_playb
b56b0 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e  ack;.  }..  /* N
b56c0 6f 77 20 72 6f 6c 6c 20 73 6f 6d 65 20 70 61 67  ow roll some pag
b56d0 65 73 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65  es back from the
b56e0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75   transaction jou
b56f0 72 6e 61 6c 2e 20 50 61 67 65 72 2e 73 74 6d 74  rnal. Pager.stmt
b5700 4a 53 69 7a 65 0a 20 20 2a 2a 20 77 61 73 20 74  JSize.  ** was t
b5710 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a  he size of the j
b5720 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e  ournal file when
b5730 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   this statement 
b5740 77 61 73 20 73 74 61 72 74 65 64 2c 20 73 6f 0a  was started, so.
b5750 20 20 2a 2a 20 65 76 65 72 79 74 68 69 6e 67 20    ** everything 
b5760 61 66 74 65 72 20 74 68 61 74 20 6e 65 65 64 73  after that needs
b5770 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61   to be rolled ba
b5780 63 6b 2c 20 65 69 74 68 65 72 20 69 6e 74 6f 20  ck, either into 
b5790 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
b57a0 65 2c 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 61  e, the memory ca
b57b0 63 68 65 2c 20 6f 72 20 62 6f 74 68 2e 0a 20 20  che, or both..  
b57c0 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 74 20 69 73  **.  ** If it is
b57d0 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20   not zero, then 
b57e0 50 61 67 65 72 2e 73 74 6d 74 48 64 72 4f 66 66  Pager.stmtHdrOff
b57f0 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 74   is the offset t
b5800 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 2a 2a  o the start.  **
b5810 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 6a 6f   of the first jo
b5820 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 72 69  urnal header wri
b5830 74 74 65 6e 20 64 75 72 69 6e 67 20 74 68 69 73  tten during this
b5840 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
b5850 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 70  action..  */.  p
b5860 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
b5870 66 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 6d 74  f = pPager->stmt
b5880 4a 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 72 2d  JSize;.  pPager-
b5890 3e 63 6b 73 75 6d 49 6e 69 74 20 3d 20 70 50 61  >cksumInit = pPa
b58a0 67 65 72 2d 3e 73 74 6d 74 43 6b 73 75 6d 3b 0a  ger->stmtCksum;.
b58b0 20 20 77 68 69 6c 65 28 20 70 50 61 67 65 72 2d    while( pPager-
b58c0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3c 20 68 64  >journalOff < hd
b58d0 72 4f 66 66 20 29 7b 0a 20 20 20 20 72 63 20 3d  rOff ){.    rc =
b58e0 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
b58f0 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c  one_page(pPager,
b5900 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50   pPager->jfd, pP
b5910 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
b5920 2c 20 31 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , 1);.    assert
b5930 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
b5940 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  E );.    if( rc!
b5950 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
b5960 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62  o end_stmt_playb
b5970 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c  ack;.  }..  whil
b5980 65 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  e( pPager->journ
b5990 61 6c 4f 66 66 20 3c 20 73 7a 4a 20 29 7b 0a 20  alOff < szJ ){. 
b59a0 20 20 20 75 33 32 20 6e 4a 52 65 63 3b 20 20 20     u32 nJRec;   
b59b0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
b59c0 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f 72  of Journal Recor
b59d0 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64 75  ds */.    u32 du
b59e0 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72 65  mmy;.    rc = re
b59f0 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  adJournalHdr(pPa
b5a00 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63  ger, szJ, &nJRec
b5a10 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 69  , &dummy);.    i
b5a20 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
b5a30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
b5a40 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
b5a50 45 20 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  E );.      goto 
b5a60 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63  end_stmt_playbac
b5a70 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
b5a80 20 6e 4a 52 65 63 3d 3d 30 20 29 7b 0a 20 20 20   nJRec==0 ){.   
b5a90 20 20 20 6e 4a 52 65 63 20 3d 20 28 73 7a 4a 20     nJRec = (szJ 
b5aa0 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  - pPager->journa
b5ab0 6c 4f 66 66 29 20 2f 20 28 70 50 61 67 65 72 2d  lOff) / (pPager-
b5ac0 3e 70 61 67 65 53 69 7a 65 2b 38 29 3b 0a 20 20  >pageSize+8);.  
b5ad0 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 6e 4a    }.    for(i=nJ
b5ae0 52 65 63 2d 31 3b 20 69 3e 3d 30 20 26 26 20 70  Rec-1; i>=0 && p
b5af0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
b5b00 66 20 3c 20 73 7a 4a 3b 20 69 2d 2d 29 7b 0a 20  f < szJ; i--){. 
b5b10 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
b5b20 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67  playback_one_pag
b5b30 65 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  e(pPager, pPager
b5b40 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a  ->jfd, pPager->j
b5b50 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 29 3b 0a 20  ournalOff, 1);. 
b5b60 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21       assert( rc!
b5b70 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a  =SQLITE_DONE );.
b5b80 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
b5b90 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
b5ba0 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b  nd_stmt_playback
b5bb0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  ;.    }.  }..  p
b5bc0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
b5bd0 66 20 3d 20 73 7a 4a 3b 0a 20 20 0a 65 6e 64 5f  f = szJ;.  .end_
b5be0 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3a 0a 20  stmt_playback:. 
b5bf0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
b5c00 4f 4b 29 20 7b 0a 20 20 20 20 70 50 61 67 65 72  OK) {.    pPager
b5c10 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73  ->journalOff = s
b5c20 7a 4a 3b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72  zJ;.    /* pager
b5c30 5f 72 65 6c 6f 61 64 5f 63 61 63 68 65 28 70 50  _reload_cache(pP
b5c40 61 67 65 72 29 3b 20 2a 2f 0a 20 20 7d 0a 20 20  ager); */.  }.  
b5c50 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
b5c60 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6d  .** Change the m
b5c70 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
b5c80 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
b5c90 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f 77 65   that are allowe
b5ca0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
b5cb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
b5cc0 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 69  3PagerSetCachesi
b5cd0 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
b5ce0 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
b5cf0 20 69 66 28 20 6d 78 50 61 67 65 3e 31 30 20 29   if( mxPage>10 )
b5d00 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78  {.    pPager->mx
b5d10 50 61 67 65 20 3d 20 6d 78 50 61 67 65 3b 0a 20  Page = mxPage;. 
b5d20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67   }else{.    pPag
b5d30 65 72 2d 3e 6d 78 50 61 67 65 20 3d 20 31 30 3b  er->mxPage = 10;
b5d40 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
b5d50 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73 74 6e  just the robustn
b5d60 65 73 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  ess of the datab
b5d70 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20 64 75  ase to damage du
b5d80 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 0a  e to OS crashes.
b5d90 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c  ** or power fail
b5da0 75 72 65 73 20 62 79 20 63 68 61 6e 67 69 6e 67  ures by changing
b5db0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
b5dc0 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77 72 69  yncs()s when wri
b5dd0 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c  ting.** the roll
b5de0 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 54  back journal.  T
b5df0 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 6c  here are three l
b5e00 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  evels:.**.**    
b5e10 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69 74 65  OFF       sqlite
b5e20 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e 65 76  3OsSync() is nev
b5e30 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73  er called.  This
b5e40 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 0a   is the default.
b5e50 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
b5e60 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 61 6e  for temporary an
b5e70 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65  d transient file
b5e80 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f 52 4d  s..**.**    NORM
b5e90 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61  AL    The journa
b5ea0 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e 63 65  l is synced once
b5eb0 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62   before writes b
b5ec0 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20  egin on the.**  
b5ed0 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
b5ee0 62 61 73 65 2e 20 20 54 68 69 73 20 69 73 20 6e  base.  This is n
b5ef0 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61 74 65  ormally adequate
b5f00 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62 75 74   protection, but
b5f10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
b5f20 20 69 74 20 69 73 20 74 68 65 6f 72 65 74 69 63   it is theoretic
b5f30 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c 20 74  ally possible, t
b5f40 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c 69 6b  hough very unlik
b5f50 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ely,.**         
b5f60 20 20 20 20 20 74 68 61 74 20 61 6e 20 69 6e 6f       that an ino
b5f70 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20 66 61  pertune power fa
b5f80 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 61 76  ilure could leav
b5f90 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  e the journal.**
b5fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
b5fb0 20 61 20 73 74 61 74 65 20 77 68 69 63 68 20 77   a state which w
b5fc0 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d 61 67  ould cause damag
b5fd0 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
b5fe0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
b5ff0 20 20 77 68 65 6e 20 69 74 20 69 73 20 72 6f 6c    when it is rol
b6000 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  led back..**.** 
b6010 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54 68 65     FULL      The
b6020 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63   journal is sync
b6030 65 64 20 74 77 69 63 65 20 62 65 66 6f 72 65 20  ed twice before 
b6040 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20  writes begin on 
b6050 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
b6060 20 20 20 20 64 61 74 61 62 61 73 65 20 28 77 69      database (wi
b6070 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e  th some addition
b6080 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d  al information -
b6090 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a   the nRec field.
b60a0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
b60b0 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  of the journal h
b60c0 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20 77 72  eader - being wr
b60d0 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65 65 6e  itten in between
b60e0 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20 20 20   the two.**     
b60f0 20 20 20 20 20 20 20 20 20 73 79 6e 63 73 29 2e           syncs).
b6100 20 20 49 66 20 77 65 20 61 73 73 75 6d 65 20 74    If we assume t
b6110 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a 2a 2a  hat writing a.**
b6120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
b6130 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72  ngle disk sector
b6140 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68 65 6e   is atomic, then
b6150 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f 76 69   this mode provi
b6160 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  des.**          
b6170 20 20 20 20 61 73 73 75 72 61 6e 63 65 20 74 68      assurance th
b6180 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  at the journal w
b6190 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72 72 75  ill not be corru
b61a0 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 20  pted to the.**  
b61b0 20 20 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e              poin
b61c0 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64 61 6d  t of causing dam
b61d0 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  age to the datab
b61e0 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62  ase during rollb
b61f0 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 72  ack..**.** Numer
b6200 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f 63 69  ic values associ
b6210 61 74 65 64 20 77 69 74 68 20 74 68 65 73 65 20  ated with these 
b6220 73 74 61 74 65 73 20 61 72 65 20 4f 46 46 3d 3d  states are OFF==
b6230 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a 20  1, NORMAL=2,.** 
b6240 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a 23  and FULL=3..*/.#
b6250 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
b6260 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
b6270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b6280 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
b6290 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28  rSetSafetyLevel(
b62a0 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
b62b0 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 66 75  nt level, int fu
b62c0 6c 6c 5f 66 73 79 6e 63 29 7b 0a 20 20 70 50 61  ll_fsync){.  pPa
b62d0 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 20 6c  ger->noSync =  l
b62e0 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 50 61 67 65  evel==1 || pPage
b62f0 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70  r->tempFile;.  p
b6300 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20  Pager->fullSync 
b6310 3d 20 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70  = level==3 && !p
b6320 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b  Pager->tempFile;
b6330 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  .  pPager->sync_
b6340 66 6c 61 67 73 20 3d 20 28 66 75 6c 6c 5f 66 73  flags = (full_fs
b6350 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f  ync?SQLITE_SYNC_
b6360 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e 43  FULL:SQLITE_SYNC
b6370 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 20  _NORMAL);.  if( 
b6380 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29  pPager->noSync )
b6390 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
b63a0 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  c = 0;.}.#endif.
b63b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
b63c0 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69  wing global vari
b63d0 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e  able is incremen
b63e0 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65  ted whenever the
b63f0 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 65   library.** atte
b6400 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 74  mpts to open a t
b6410 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20  emporary file.  
b6420 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
b6430 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20   is used for.** 
b6440 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c  testing and anal
b6450 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a  ysis only.  .*/.
b6460 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
b6470 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
b6480 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65  t sqlite3_opente
b6490 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  mp_count = 0;.#e
b64a0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  ndif../*.** Open
b64b0 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
b64c0 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  e. .**.** Write 
b64d0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
b64e0 74 6f 72 20 69 6e 74 6f 20 2a 66 64 2e 20 20 52  tor into *fd.  R
b64f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
b6500 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 73 6f  on success or so
b6510 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 65 72 72 6f  me.** other erro
b6520 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 61 69  r code if we fai
b6530 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c 20 61  l. The OS will a
b6540 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c  utomatically del
b6550 65 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72  ete the temporar
b6560 79 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 69  y.** file when i
b6570 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a  t is closed..*/.
b6580 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
b6590 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d 70 28  e3PagerOpentemp(
b65a0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
b65b0 70 56 66 73 2c 20 20 20 20 2f 2a 20 54 68 65 20  pVfs,    /* The 
b65c0 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73  virtual file sys
b65d0 74 65 6d 20 6c 61 79 65 72 20 2a 2f 0a 20 20 73  tem layer */.  s
b65e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
b65f0 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 20 74 68  le,  /* Write th
b6600 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
b6610 72 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72  r here */.  char
b6620 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20   *zFilename,    
b6630 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
b6640 20 66 69 6c 65 2e 20 20 4d 69 67 68 74 20 62 65   file.  Might be
b6650 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 76   NULL */.  int v
b6660 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20  fsFlags         
b6670 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64   /* Flags passed
b6680 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
b6690 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  VFS */.){.  int 
b66a0 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 46  rc;.  assert( zF
b66b0 69 6c 65 6e 61 6d 65 21 3d 30 20 29 3b 0a 0a 23  ilename!=0 );..#
b66c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
b66d0 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  T.  sqlite3_open
b66e0 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f  temp_count++;  /
b66f0 2a 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69  * Used for testi
b6700 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20  ng and analysis 
b6710 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  only */.#endif..
b6720 20 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53    vfsFlags |=  S
b6730 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
b6740 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50  RITE | SQLITE_OP
b6750 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20  EN_CREATE |.    
b6760 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
b6770 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20  PEN_EXCLUSIVE | 
b6780 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
b6790 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20  TEONCLOSE;.  rc 
b67a0 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  = sqlite3OsOpen(
b67b0 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  pVfs, zFilename,
b67c0 20 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73   pFile, vfsFlags
b67d0 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20  , 0);.  assert( 
b67e0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
b67f0 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73   pFile->pMethods
b6800 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
b6810 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
b6820 20 61 20 6e 65 77 20 70 61 67 65 20 63 61 63 68   a new page cach
b6830 65 20 61 6e 64 20 70 75 74 20 61 20 70 6f 69 6e  e and put a poin
b6840 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20  ter to the page 
b6850 63 61 63 68 65 20 69 6e 20 2a 70 70 50 61 67 65  cache in *ppPage
b6860 72 2e 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 74  r..** The file t
b6870 6f 20 62 65 20 63 61 63 68 65 64 20 6e 65 65 64  o be cached need
b6880 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 54 68 65   not exist.  The
b6890 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63   file is not loc
b68a0 6b 65 64 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 65  ked until.** the
b68b0 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73   first call to s
b68c0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
b68d0 20 61 6e 64 20 69 73 20 6f 6e 6c 79 20 68 65 6c   and is only hel
b68e0 64 20 6f 70 65 6e 20 75 6e 74 69 6c 20 74 68 65  d open until the
b68f0 0a 2a 2a 20 6c 61 73 74 20 70 61 67 65 20 69 73  .** last page is
b6900 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20   released using 
b6910 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
b6920 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46  f()..**.** If zF
b6930 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20  ilename is NULL 
b6940 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d  then a randomly-
b6950 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 20  named temporary 
b6960 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 0a  file is created.
b6970 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73 20 74  ** and used as t
b6980 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 63 61  he file to be ca
b6990 63 68 65 64 2e 20 20 54 68 65 20 66 69 6c 65 20  ched.  The file 
b69a0 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 0a  will be deleted.
b69b0 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ** automatically
b69c0 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73   when it is clos
b69d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69  ed..**.** If zFi
b69e0 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f  lename is ":memo
b69f0 72 79 3a 22 20 74 68 65 6e 20 61 6c 6c 20 69 6e  ry:" then all in
b6a00 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 6c  formation is hel
b6a10 64 20 69 6e 20 63 61 63 68 65 2e 0a 2a 2a 20 49  d in cache..** I
b6a20 74 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74  t is never writt
b6a30 65 6e 20 74 6f 20 64 69 73 6b 2e 20 20 54 68 69  en to disk.  Thi
b6a40 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
b6a50 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 0a 2a 2a   implement an.**
b6a60 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
b6a70 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ase..*/.SQLITE_P
b6a80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b6a90 65 33 50 61 67 65 72 4f 70 65 6e 28 0a 20 20 73  e3PagerOpen(.  s
b6aa0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
b6ab0 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  ,       /* The v
b6ac0 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74  irtual file syst
b6ad0 65 6d 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 50  em to use */.  P
b6ae0 61 67 65 72 20 2a 2a 70 70 50 61 67 65 72 2c 20  ager **ppPager, 
b6af0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
b6b00 6e 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75  n the Pager stru
b6b10 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20  cture here */.  
b6b20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
b6b30 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65  ename,   /* Name
b6b40 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
b6b50 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f   file to open */
b6b60 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20  .  int nExtra,  
b6b70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
b6b80 78 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e  xtra bytes appen
b6b90 64 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d  d to each in-mem
b6ba0 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ory page */.  in
b6bb0 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
b6bc0 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20         /* flags 
b6bd0 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73  controlling this
b6be0 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76   file */.  int v
b6bf0 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20  fsFlags         
b6c00 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73      /* flags pas
b6c10 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73  sed through to s
b6c20 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e  qlite3_vfs.xOpen
b6c30 28 29 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70  () */.){.  u8 *p
b6c40 50 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  Ptr;.  Pager *pP
b6c50 61 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ager = 0;.  int 
b6c60 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
b6c70 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 74    int i;.  int t
b6c80 65 6d 70 46 69 6c 65 20 3d 20 30 3b 0a 20 20 69  empFile = 0;.  i
b6c90 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 0a 20 20  nt memDb = 0;.  
b6ca0 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 30  int readOnly = 0
b6cb0 3b 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e  ;.  int useJourn
b6cc0 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41  al = (flags & PA
b6cd0 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c  GER_OMIT_JOURNAL
b6ce0 29 3d 3d 30 3b 0a 20 20 69 6e 74 20 6e 6f 52 65  )==0;.  int noRe
b6cf0 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20  adlock = (flags 
b6d00 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c  & PAGER_NO_READL
b6d10 4f 43 4b 29 21 3d 30 3b 0a 20 20 69 6e 74 20 6a  OCK)!=0;.  int j
b6d20 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d  ournalFileSize =
b6d30 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
b6d40 69 7a 65 28 70 56 66 73 29 3b 0a 20 20 69 6e 74  ize(pVfs);.  int
b6d50 20 6e 44 65 66 61 75 6c 74 50 61 67 65 20 3d 20   nDefaultPage = 
b6d60 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
b6d70 41 47 45 5f 53 49 5a 45 3b 0a 20 20 63 68 61 72  AGE_SIZE;.  char
b6d80 20 2a 7a 50 61 74 68 6e 61 6d 65 3b 0a 20 20 69   *zPathname;.  i
b6d90 6e 74 20 6e 50 61 74 68 6e 61 6d 65 3b 0a 0a 20  nt nPathname;.. 
b6da0 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20   /* The default 
b6db0 72 65 74 75 72 6e 20 69 73 20 61 20 4e 55 4c 4c  return is a NULL
b6dc0 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 2a 70   pointer */.  *p
b6dd0 70 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f  pPager = 0;..  /
b6de0 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 66 75  * Compute the fu
b6df0 6c 6c 20 70 61 74 68 6e 61 6d 65 20 2a 2f 0a 20  ll pathname */. 
b6e00 20 7a 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c   zPathname = sql
b6e10 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 56 66 73  ite3_malloc(pVfs
b6e20 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b  ->mxPathname+1);
b6e30 0a 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65  .  if( zPathname
b6e40 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
b6e50 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
b6e60 20 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e    }.  if( zFilen
b6e70 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65  ame && zFilename
b6e80 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  [0] ){.#ifndef S
b6e90 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
b6ea0 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72 63  YDB.    if( strc
b6eb0 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d  mp(zFilename,":m
b6ec0 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a 20  emory:")==0 ){. 
b6ed0 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a       memDb = 1;.
b6ee0 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b        zPathname[
b6ef0 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  0] = 0;.    }els
b6f00 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20  e.#endif.    {. 
b6f10 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
b6f20 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  3OsFullPathname(
b6f30 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  pVfs, zFilename,
b6f40 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20   zPathname);.   
b6f50 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
b6f60 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 47 65  rc = sqlite3OsGe
b6f70 74 54 65 6d 70 4e 61 6d 65 28 70 56 66 73 2c 20  tTempName(pVfs, 
b6f80 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a  zPathname);.  }.
b6f90 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
b6fa0 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
b6fb0 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d  e3_free(zPathnam
b6fc0 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  e);.    return r
b6fd0 63 3b 0a 20 20 7d 0a 20 20 6e 50 61 74 68 6e 61  c;.  }.  nPathna
b6fe0 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 50 61 74  me = strlen(zPat
b6ff0 68 6e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 41 6c  hname);..  /* Al
b7000 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f  locate memory fo
b7010 72 20 74 68 65 20 70 61 67 65 72 20 73 74 72 75  r the pager stru
b7020 63 74 75 72 65 20 2a 2f 0a 20 20 70 50 61 67 65  cture */.  pPage
b7030 72 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  r = sqlite3Mallo
b7040 63 5a 65 72 6f 28 0a 20 20 20 20 73 69 7a 65 6f  cZero(.    sizeo
b7050 66 28 2a 70 50 61 67 65 72 29 20 2b 20 20 20 20  f(*pPager) +    
b7060 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20         /* Pager 
b7070 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20  structure */.   
b7080 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65   journalFileSize
b7090 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   +           /* 
b70a0 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  The journal file
b70b0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 20 0a 20   structure */ . 
b70c0 20 20 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c     pVfs->szOsFil
b70d0 65 20 2a 20 32 20 2b 20 20 20 20 20 20 20 20 2f  e * 2 +        /
b70e0 2a 20 54 68 65 20 64 62 20 61 6e 64 20 73 74 6d  * The db and stm
b70f0 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20  t journal files 
b7100 2a 2f 20 0a 20 20 20 20 34 2a 6e 50 61 74 68 6e  */ .    4*nPathn
b7110 61 6d 65 20 2b 20 34 30 20 20 20 20 20 20 20 20  ame + 40        
b7120 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65      /* zFilename
b7130 2c 20 7a 44 69 72 65 63 74 6f 72 79 2c 20 7a 4a  , zDirectory, zJ
b7140 6f 75 72 6e 61 6c 2c 20 7a 53 74 6d 74 4a 72 6e  ournal, zStmtJrn
b7150 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20  l */.  );.  if( 
b7160 21 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 73  !pPager ){.    s
b7170 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74  qlite3_free(zPat
b7180 68 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  hname);.    retu
b7190 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
b71a0 0a 20 20 7d 0a 20 20 70 50 74 72 20 3d 20 28 75  .  }.  pPtr = (u
b71b0 38 20 2a 29 26 70 50 61 67 65 72 5b 31 5d 3b 0a  8 *)&pPager[1];.
b71c0 20 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61    pPager->vfsFla
b71d0 67 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 20  gs = vfsFlags;. 
b71e0 20 70 50 61 67 65 72 2d 3e 66 64 20 3d 20 28 73   pPager->fd = (s
b71f0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50  qlite3_file*)&pP
b7200 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  tr[pVfs->szOsFil
b7210 65 2a 30 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e  e*0];.  pPager->
b7220 73 74 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f  stfd = (sqlite3_
b7230 66 69 6c 65 2a 29 26 70 50 74 72 5b 70 56 66 73  file*)&pPtr[pVfs
b7240 2d 3e 73 7a 4f 73 46 69 6c 65 2a 31 5d 3b 0a 20  ->szOsFile*1];. 
b7250 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3d 20 28   pPager->jfd = (
b7260 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 26 70  sqlite3_file*)&p
b7270 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69  Ptr[pVfs->szOsFi
b7280 6c 65 2a 32 5d 3b 0a 20 20 70 50 61 67 65 72 2d  le*2];.  pPager-
b7290 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 28 63 68  >zFilename = (ch
b72a0 61 72 2a 29 26 70 50 74 72 5b 70 56 66 73 2d 3e  ar*)&pPtr[pVfs->
b72b0 73 7a 4f 73 46 69 6c 65 2a 32 2b 6a 6f 75 72 6e  szOsFile*2+journ
b72c0 61 6c 46 69 6c 65 53 69 7a 65 5d 3b 0a 20 20 70  alFileSize];.  p
b72d0 50 61 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72  Pager->zDirector
b72e0 79 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a 46 69  y = &pPager->zFi
b72f0 6c 65 6e 61 6d 65 5b 6e 50 61 74 68 6e 61 6d 65  lename[nPathname
b7300 2b 31 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a  +1];.  pPager->z
b7310 4a 6f 75 72 6e 61 6c 20 3d 20 26 70 50 61 67 65  Journal = &pPage
b7320 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 6e 50  r->zDirectory[nP
b7330 61 74 68 6e 61 6d 65 2b 31 5d 3b 0a 20 20 70 50  athname+1];.  pP
b7340 61 67 65 72 2d 3e 7a 53 74 6d 74 4a 72 6e 6c 20  ager->zStmtJrnl 
b7350 3d 20 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  = &pPager->zJour
b7360 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 2b 31 30  nal[nPathname+10
b7370 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 66  ];.  pPager->pVf
b7380 73 20 3d 20 70 56 66 73 3b 0a 20 20 6d 65 6d 63  s = pVfs;.  memc
b7390 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  py(pPager->zFile
b73a0 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c  name, zPathname,
b73b0 20 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20   nPathname+1);. 
b73c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50   sqlite3_free(zP
b73d0 61 74 68 6e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20  athname);..  /* 
b73e0 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66  Open the pager f
b73f0 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ile..  */.  if( 
b7400 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69  zFilename && zFi
b7410 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65  lename[0] && !me
b7420 6d 44 62 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  mDb ){.    if( n
b7430 50 61 74 68 6e 61 6d 65 3e 28 70 56 66 73 2d 3e  Pathname>(pVfs->
b7440 6d 78 50 61 74 68 6e 61 6d 65 20 2d 20 73 69 7a  mxPathname - siz
b7450 65 6f 66 28 22 2d 6a 6f 75 72 6e 61 6c 22 29 29  eof("-journal"))
b7460 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
b7470 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
b7480 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b7490 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b 0a 20   int fout = 0;. 
b74a0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
b74b0 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50  3OsOpen(pVfs, pP
b74c0 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
b74d0 20 70 50 61 67 65 72 2d 3e 66 64 2c 0a 20 20 20   pPager->fd,.   
b74e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b74f0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 76 66        pPager->vf
b7500 73 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a  sFlags, &fout);.
b7510 20 20 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d        readOnly =
b7520 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50   (fout&SQLITE_OP
b7530 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20  EN_READONLY);.. 
b7540 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66       /* If the f
b7550 69 6c 65 20 77 61 73 20 73 75 63 63 65 73 73 66  ile was successf
b7560 75 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20  ully opened for 
b7570 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73  read/write acces
b7580 73 2c 0a 20 20 20 20 20 20 2a 2a 20 63 68 6f 6f  s,.      ** choo
b7590 73 65 20 61 20 64 65 66 61 75 6c 74 20 70 61 67  se a default pag
b75a0 65 20 73 69 7a 65 20 69 6e 20 63 61 73 65 20 77  e size in case w
b75b0 65 20 68 61 76 65 20 74 6f 20 63 72 65 61 74 65  e have to create
b75c0 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61   the.      ** da
b75d0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65  tabase file. The
b75e0 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69   default page si
b75f0 7a 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75  ze is the maximu
b7600 6d 20 6f 66 3a 0a 20 20 20 20 20 20 2a 2a 0a 20  m of:.      **. 
b7610 20 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 51 4c       **    + SQL
b7620 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  ITE_DEFAULT_PAGE
b7630 5f 53 49 5a 45 2c 0a 20 20 20 20 20 20 2a 2a 20  _SIZE,.      ** 
b7640 20 20 20 2b 20 54 68 65 20 76 61 6c 75 65 20 72     + The value r
b7650 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
b7660 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29  e3OsSectorSize()
b7670 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 2b 20 54  .      **    + T
b7680 68 65 20 6c 61 72 67 65 73 74 20 70 61 67 65 20  he largest page 
b7690 73 69 7a 65 20 74 68 61 74 20 63 61 6e 20 62 65  size that can be
b76a0 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61   written atomica
b76b0 6c 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  lly..      */.  
b76c0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
b76d0 54 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e  TE_OK && !readOn
b76e0 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ly ){.        in
b76f0 74 20 69 53 65 63 74 6f 72 53 69 7a 65 20 3d 20  t iSectorSize = 
b7700 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53  sqlite3OsSectorS
b7710 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ize(pPager->fd);
b7720 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 44 65  .        if( nDe
b7730 66 61 75 6c 74 50 61 67 65 3c 69 53 65 63 74 6f  faultPage<iSecto
b7740 72 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  rSize ){.       
b7750 20 20 20 6e 44 65 66 61 75 6c 74 50 61 67 65 20     nDefaultPage 
b7760 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20  = iSectorSize;. 
b7770 20 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20         }.#ifdef 
b7780 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
b7790 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20  OMIC_WRITE.     
b77a0 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 69     {.          i
b77b0 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33  nt iDc = sqlite3
b77c0 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
b77d0 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e  ristics(pPager->
b77e0 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  fd);.          i
b77f0 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20  nt ii;.         
b7800 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49   assert(SQLITE_I
b7810 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d  OCAP_ATOMIC512==
b7820 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 20  (512>>8));.     
b7830 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49       assert(SQLI
b7840 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36  TE_IOCAP_ATOMIC6
b7850 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b  4K==(65536>>8));
b7860 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
b7870 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  t(SQLITE_MAX_DEF
b7880 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d  AULT_PAGE_SIZE<=
b7890 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20 20  65536);.        
b78a0 20 20 66 6f 72 28 69 69 3d 6e 44 65 66 61 75 6c    for(ii=nDefaul
b78b0 74 50 61 67 65 3b 20 69 69 3c 3d 53 51 4c 49 54  tPage; ii<=SQLIT
b78c0 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
b78d0 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32  GE_SIZE; ii=ii*2
b78e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
b78f0 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49  f( iDc&(SQLITE_I
b7900 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e  OCAP_ATOMIC|(ii>
b7910 3e 38 29 29 20 29 20 6e 44 65 66 61 75 6c 74 50  >8)) ) nDefaultP
b7920 61 67 65 20 3d 20 69 69 3b 0a 20 20 20 20 20 20  age = ii;.      
b7930 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
b7940 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69  #endif.        i
b7950 66 28 20 6e 44 65 66 61 75 6c 74 50 61 67 65 3e  f( nDefaultPage>
b7960 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55  SQLITE_MAX_DEFAU
b7970 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b 0a  LT_PAGE_SIZE ){.
b7980 20 20 20 20 20 20 20 20 20 20 6e 44 65 66 61 75            nDefau
b7990 6c 74 50 61 67 65 20 3d 20 53 51 4c 49 54 45 5f  ltPage = SQLITE_
b79a0 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  MAX_DEFAULT_PAGE
b79b0 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d  _SIZE;.        }
b79c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
b79d0 20 7d 65 6c 73 65 20 69 66 28 20 21 6d 65 6d 44   }else if( !memD
b79e0 62 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61  b ){.    /* If a
b79f0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
b7a00 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74  is requested, it
b7a10 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69   is not opened i
b7a20 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20  mmediately..    
b7a30 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ** In this case 
b7a40 77 65 20 61 63 63 65 70 74 20 74 68 65 20 64 65  we accept the de
b7a50 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20  fault page size 
b7a60 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61 6c  and delay actual
b7a70 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e  ly.    ** openin
b7a80 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74 69 6c  g the file until
b7a90 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
b7aa0 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 20  to OsWrite()..  
b7ab0 20 20 2a 2f 20 0a 20 20 20 20 74 65 6d 70 46 69    */ .    tempFi
b7ac0 6c 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67  le = 1;.    pPag
b7ad0 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
b7ae0 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d  R_EXCLUSIVE;.  }
b7af0 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 20 26  ..  if( pPager &
b7b00 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
b7b10 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70  ){.    pPager->p
b7b20 54 6d 70 53 70 61 63 65 20 3d 20 28 63 68 61 72  TmpSpace = (char
b7b30 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
b7b40 63 28 6e 44 65 66 61 75 6c 74 50 61 67 65 29 3b  c(nDefaultPage);
b7b50 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e  .  }..  /* If an
b7b60 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69   error occured i
b7b70 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20  n either of the 
b7b80 62 6c 6f 63 6b 73 20 61 62 6f 76 65 2e 0a 20 20  blocks above..  
b7b90 2a 2a 20 46 72 65 65 20 74 68 65 20 50 61 67 65  ** Free the Page
b7ba0 72 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  r structure and 
b7bb0 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a  close the file..
b7bc0 20 20 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 70    ** Since the p
b7bd0 61 67 65 72 20 69 73 20 6e 6f 74 20 61 6c 6c 6f  ager is not allo
b7be0 63 61 74 65 64 20 74 68 65 72 65 20 69 73 20 6e  cated there is n
b7bf0 6f 20 6e 65 65 64 20 74 6f 20 73 65 74 20 0a 20  o need to set . 
b7c00 20 2a 2a 20 61 6e 79 20 50 61 67 65 72 2e 65 72   ** any Pager.er
b7c10 72 4d 61 73 6b 20 76 61 72 69 61 62 6c 65 73 2e  rMask variables.
b7c20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61  .  */.  if( !pPa
b7c30 67 65 72 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e  ger || !pPager->
b7c40 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20  pTmpSpace ){.   
b7c50 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
b7c60 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20  pPager->fd);.   
b7c70 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
b7c80 61 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72  ager);.    retur
b7c90 6e 20 28 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  n ((rc==SQLITE_O
b7ca0 4b 29 3f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3a  K)?SQLITE_NOMEM:
b7cb0 72 63 29 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45  rc);.  }..  PAGE
b7cc0 52 54 52 41 43 45 33 28 22 4f 50 45 4e 20 25 64  RTRACE3("OPEN %d
b7cd0 20 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44   %s\n", FILEHAND
b7ce0 4c 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29  LEID(pPager->fd)
b7cf0 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e  , pPager->zFilen
b7d00 61 6d 65 29 3b 0a 20 20 49 4f 54 52 41 43 45 28  ame);.  IOTRACE(
b7d10 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c  ("OPEN %p %s\n",
b7d20 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d   pPager, pPager-
b7d30 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20  >zFilename))..  
b7d40 2f 2a 20 46 69 6c 6c 20 69 6e 20 50 61 67 65 72  /* Fill in Pager
b7d50 2e 7a 44 69 72 65 63 74 6f 72 79 5b 5d 20 2a 2f  .zDirectory[] */
b7d60 0a 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72  .  memcpy(pPager
b7d70 2d 3e 7a 44 69 72 65 63 74 6f 72 79 2c 20 70 50  ->zDirectory, pP
b7d80 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
b7d90 20 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20   nPathname+1);. 
b7da0 20 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28 70 50   for(i=strlen(pP
b7db0 61 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79  ager->zDirectory
b7dc0 29 3b 20 69 3e 30 20 26 26 20 70 50 61 67 65 72  ); i>0 && pPager
b7dd0 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31  ->zDirectory[i-1
b7de0 5d 21 3d 27 2f 27 3b 20 69 2d 2d 29 7b 7d 0a 20  ]!='/'; i--){}. 
b7df0 20 69 66 28 20 69 3e 30 20 29 20 70 50 61 67 65   if( i>0 ) pPage
b7e00 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d  r->zDirectory[i-
b7e10 31 5d 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 46 69  1] = 0;..  /* Fi
b7e20 6c 6c 20 69 6e 20 50 61 67 65 72 2e 7a 4a 6f 75  ll in Pager.zJou
b7e30 72 6e 61 6c 5b 5d 20 61 6e 64 20 50 61 67 65 72  rnal[] and Pager
b7e40 2e 7a 53 74 6d 74 4a 72 6e 6c 5b 5d 20 2a 2f 0a  .zStmtJrnl[] */.
b7e50 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d    memcpy(pPager-
b7e60 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65  >zJournal, pPage
b7e70 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50  r->zFilename, nP
b7e80 61 74 68 6e 61 6d 65 29 3b 0a 20 20 6d 65 6d 63  athname);.  memc
b7e90 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  py(&pPager->zJou
b7ea0 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c  rnal[nPathname],
b7eb0 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 39 29 3b   "-journal", 9);
b7ec0 0a 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72  .  memcpy(pPager
b7ed0 2d 3e 7a 53 74 6d 74 4a 72 6e 6c 2c 20 70 50 61  ->zStmtJrnl, pPa
b7ee0 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20  ger->zFilename, 
b7ef0 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 6d 65  nPathname);.  me
b7f00 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 53  mcpy(&pPager->zS
b7f10 74 6d 74 4a 72 6e 6c 5b 6e 50 61 74 68 6e 61 6d  tmtJrnl[nPathnam
b7f20 65 5d 2c 20 22 2d 73 74 6d 74 6a 72 6e 6c 22 2c  e], "-stmtjrnl",
b7f30 20 31 30 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 67   10);..  /* pPag
b7f40 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
b7f50 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72  = 0; */.  pPager
b7f60 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 75  ->useJournal = u
b7f70 73 65 4a 6f 75 72 6e 61 6c 20 26 26 20 21 6d 65  seJournal && !me
b7f80 6d 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  mDb;.  pPager->n
b7f90 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 6e 6f 52 65  oReadlock = noRe
b7fa0 61 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e  adlock && readOn
b7fb0 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  ly;.  /* pPager-
b7fc0 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a  >stmtOpen = 0; *
b7fd0 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73  /.  /* pPager->s
b7fe0 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f  tmtInUse = 0; */
b7ff0 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52  .  /* pPager->nR
b8000 65 66 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  ef = 0; */.  pPa
b8010 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 65  ger->dbSize = me
b8020 6d 44 62 2d 31 3b 0a 20 20 70 50 61 67 65 72 2d  mDb-1;.  pPager-
b8030 3e 70 61 67 65 53 69 7a 65 20 3d 20 6e 44 65 66  >pageSize = nDef
b8040 61 75 6c 74 50 61 67 65 3b 0a 20 20 2f 2a 20 70  aultPage;.  /* p
b8050 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20  Pager->stmtSize 
b8060 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61  = 0; */.  /* pPa
b8070 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d  ger->stmtJSize =
b8080 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
b8090 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 20 2a  er->nPage = 0; *
b80a0 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 61  /.  pPager->mxPa
b80b0 67 65 20 3d 20 31 30 30 3b 0a 20 20 70 50 61 67  ge = 100;.  pPag
b80c0 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51 4c  er->mxPgno = SQL
b80d0 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55  ITE_MAX_PAGE_COU
b80e0 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  NT;.  /* pPager-
b80f0 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 55  >state = PAGER_U
b8100 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73 65  NLOCK; */.  asse
b8110 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
b8120 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 3f  e == (tempFile ?
b8130 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
b8140 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 29   : PAGER_UNLOCK)
b8150 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d   );.  /* pPager-
b8160 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f  >errMask = 0; */
b8170 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  .  pPager->tempF
b8180 69 6c 65 20 3d 20 74 65 6d 70 46 69 6c 65 3b 0a  ile = tempFile;.
b8190 20 20 61 73 73 65 72 74 28 20 74 65 6d 70 46 69    assert( tempFi
b81a0 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  le==PAGER_LOCKIN
b81b0 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20  GMODE_NORMAL .  
b81c0 20 20 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46          || tempF
b81d0 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ile==PAGER_LOCKI
b81e0 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
b81f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41   );.  assert( PA
b8200 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
b8210 45 58 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a  EXCLUSIVE==1 );.
b8220 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73    pPager->exclus
b8230 69 76 65 4d 6f 64 65 20 3d 20 74 65 6d 70 46 69  iveMode = tempFi
b8240 6c 65 3b 20 0a 20 20 70 50 61 67 65 72 2d 3e 6d  le; .  pPager->m
b8250 65 6d 44 62 20 3d 20 6d 65 6d 44 62 3b 0a 20 20  emDb = memDb;.  
b8260 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79  pPager->readOnly
b8270 20 3d 20 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f   = readOnly;.  /
b8280 2a 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79  * pPager->needSy
b8290 6e 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  nc = 0; */.  pPa
b82a0 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50  ger->noSync = pP
b82b0 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 7c  ager->tempFile |
b82c0 7c 20 21 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20  | !useJournal;. 
b82d0 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
b82e0 63 20 3d 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53  c = (pPager->noS
b82f0 79 6e 63 3f 30 3a 31 29 3b 0a 20 20 70 50 61 67  ync?0:1);.  pPag
b8300 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d  er->sync_flags =
b8310 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52   SQLITE_SYNC_NOR
b8320 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72  MAL;.  /* pPager
b8330 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f  ->pFirst = 0; */
b8340 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46  .  /* pPager->pF
b8350 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20  irstSynced = 0; 
b8360 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  */.  /* pPager->
b8370 70 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20  pLast = 0; */.  
b8380 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d  pPager->nExtra =
b8390 20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d 45 4e 54   FORCE_ALIGNMENT
b83a0 28 6e 45 78 74 72 61 29 3b 0a 20 20 61 73 73 65  (nExtra);.  asse
b83b0 72 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70  rt(pPager->fd->p
b83c0 4d 65 74 68 6f 64 73 7c 7c 6d 65 6d 44 62 7c 7c  Methods||memDb||
b83d0 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 69 66 28  tempFile);.  if(
b83e0 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 73   !memDb ){.    s
b83f0 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61  etSectorSize(pPa
b8400 67 65 72 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70  ger);.  }.  /* p
b8410 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64  Pager->pBusyHand
b8420 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a  ler = 0; */.  /*
b8430 20 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e   memset(pPager->
b8440 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66  aHash, 0, sizeof
b8450 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29  (pPager->aHash))
b8460 3b 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20  ; */.  *ppPager 
b8470 3d 20 70 50 61 67 65 72 3b 0a 23 69 66 64 65 66  = pPager;.#ifdef
b8480 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
b8490 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
b84a0 0a 20 20 70 50 61 67 65 72 2d 3e 69 49 6e 55 73  .  pPager->iInUs
b84b0 65 4d 4d 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  eMM = 0;.  pPage
b84c0 72 2d 3e 69 49 6e 55 73 65 44 42 20 3d 20 30 3b  r->iInUseDB = 0;
b84d0 0a 20 20 69 66 28 20 21 6d 65 6d 44 62 20 29 7b  .  if( !memDb ){
b84e0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
b84f0 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
b8500 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
b8510 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
b8520 54 49 43 5f 4d 45 4d 32 29 3b 0a 20 20 20 20 73  TIC_MEM2);.    s
b8530 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
b8540 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 70  er(mutex);.    p
b8550 50 61 67 65 72 2d 3e 70 4e 65 78 74 20 3d 20 73  Pager->pNext = s
b8560 71 6c 69 74 65 33 50 61 67 65 72 4c 69 73 74 3b  qlite3PagerList;
b8570 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
b8580 50 61 67 65 72 4c 69 73 74 20 29 7b 0a 20 20 20  PagerList ){.   
b8590 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
b85a0 65 33 50 61 67 65 72 4c 69 73 74 2d 3e 70 50 72  e3PagerList->pPr
b85b0 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ev==0 );.      s
b85c0 71 6c 69 74 65 33 50 61 67 65 72 4c 69 73 74 2d  qlite3PagerList-
b85d0 3e 70 50 72 65 76 20 3d 20 70 50 61 67 65 72 3b  >pPrev = pPager;
b85e0 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
b85f0 72 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20  r->pPrev = 0;.  
b8600 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 69    sqlite3PagerLi
b8610 73 74 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 20  st = pPager;.   
b8620 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
b8630 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d  eave(mutex);.  }
b8640 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
b8650 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
b8660 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 73  *.** Set the bus
b8670 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69  y handler functi
b8680 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
b8690 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
b86a0 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61  e3PagerSetBusyha
b86b0 6e 64 6c 65 72 28 50 61 67 65 72 20 2a 70 50 61  ndler(Pager *pPa
b86c0 67 65 72 2c 20 42 75 73 79 48 61 6e 64 6c 65 72  ger, BusyHandler
b86d0 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 29 7b   *pBusyHandler){
b86e0 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79  .  pPager->pBusy
b86f0 48 61 6e 64 6c 65 72 20 3d 20 70 42 75 73 79 48  Handler = pBusyH
b8700 61 6e 64 6c 65 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  andler;.}../*.**
b8710 20 53 65 74 20 74 68 65 20 64 65 73 74 72 75 63   Set the destruc
b8720 74 6f 72 20 66 6f 72 20 74 68 69 73 20 70 61 67  tor for this pag
b8730 65 72 2e 20 20 49 66 20 6e 6f 74 20 4e 55 4c 4c  er.  If not NULL
b8740 2c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  , the destructor
b8750 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 77 68   is called.** wh
b8760 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  en the reference
b8770 20 63 6f 75 6e 74 20 6f 6e 20 65 61 63 68 20 70   count on each p
b8780 61 67 65 20 72 65 61 63 68 65 73 20 7a 65 72 6f  age reaches zero
b8790 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f  .  The destructo
b87a0 72 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64  r can.** be used
b87b0 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 69 6e 66   to clean up inf
b87c0 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  ormation in the 
b87d0 65 78 74 72 61 20 73 65 67 6d 65 6e 74 20 61 70  extra segment ap
b87e0 70 65 6e 64 65 64 20 74 6f 20 65 61 63 68 20 70  pended to each p
b87f0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  age..**.** The d
b8800 65 73 74 72 75 63 74 6f 72 20 69 73 20 6e 6f 74  estructor is not
b8810 20 63 61 6c 6c 65 64 20 61 73 20 61 20 72 65 73   called as a res
b8820 75 6c 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  ult sqlite3Pager
b8830 43 6c 6f 73 65 28 29 2e 20 20 0a 2a 2a 20 44 65  Close().  .** De
b8840 73 74 72 75 63 74 6f 72 73 20 61 72 65 20 6f 6e  structors are on
b8850 6c 79 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c  ly called by sql
b8860 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29  ite3PagerUnref()
b8870 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b8880 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
b8890 50 61 67 65 72 53 65 74 44 65 73 74 72 75 63 74  PagerSetDestruct
b88a0 6f 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  or(Pager *pPager
b88b0 2c 20 76 6f 69 64 20 28 2a 78 44 65 73 63 29 28  , void (*xDesc)(
b88c0 44 62 50 61 67 65 2a 2c 69 6e 74 29 29 7b 0a 20  DbPage*,int)){. 
b88d0 20 70 50 61 67 65 72 2d 3e 78 44 65 73 74 72 75   pPager->xDestru
b88e0 63 74 6f 72 20 3d 20 78 44 65 73 63 3b 0a 7d 0a  ctor = xDesc;.}.
b88f0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 72  ./*.** Set the r
b8900 65 69 6e 69 74 69 61 6c 69 7a 65 72 20 66 6f 72  einitializer for
b8910 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 49 66   this pager.  If
b8920 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 72   not NULL, the r
b8930 65 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20  einitializer.** 
b8940 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  is called when t
b8950 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
b8960 70 61 67 65 20 69 6e 20 63 61 63 68 65 20 69 73  page in cache is
b8970 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73   restored to its
b8980 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 76 61 6c   original.** val
b8990 75 65 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f  ue as a result o
b89a0 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54  f a rollback.  T
b89b0 68 65 20 63 61 6c 6c 62 61 63 6b 20 67 69 76 65  he callback give
b89c0 73 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 63  s higher-level c
b89d0 6f 64 65 0a 2a 2a 20 61 6e 20 6f 70 70 6f 72 74  ode.** an opport
b89e0 75 6e 69 74 79 20 74 6f 20 72 65 73 74 6f 72 65  unity to restore
b89f0 20 74 68 65 20 45 58 54 52 41 20 73 65 63 74 69   the EXTRA secti
b8a00 6f 6e 20 74 6f 20 61 67 72 65 65 20 77 69 74 68  on to agree with
b8a10 20 74 68 65 20 72 65 73 74 6f 72 65 64 0a 2a 2a   the restored.**
b8a20 20 70 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a 53   page data..*/.S
b8a30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
b8a40 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
b8a50 65 74 52 65 69 6e 69 74 65 72 28 50 61 67 65 72  etReiniter(Pager
b8a60 20 2a 70 50 61 67 65 72 2c 20 76 6f 69 64 20 28   *pPager, void (
b8a70 2a 78 52 65 69 6e 69 74 29 28 44 62 50 61 67 65  *xReinit)(DbPage
b8a80 2a 2c 69 6e 74 29 29 7b 0a 20 20 70 50 61 67 65  *,int)){.  pPage
b8a90 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78  r->xReiniter = x
b8aa0 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Reinit;.}../*.**
b8ab0 20 53 65 74 20 74 68 65 20 70 61 67 65 20 73 69   Set the page si
b8ac0 7a 65 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65  ze to *pPageSize
b8ad0 2e 20 49 66 20 74 68 65 20 73 75 67 67 65 73 74  . If the suggest
b8ae0 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 69   new page size i
b8af0 73 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69 61  s.** inappropria
b8b00 74 65 2c 20 74 68 65 6e 20 61 6e 20 61 6c 74 65  te, then an alte
b8b10 72 6e 61 74 69 76 65 20 70 61 67 65 20 73 69 7a  rnative page siz
b8b20 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 61 74  e is set to that
b8b30 0a 2a 2a 20 76 61 6c 75 65 20 62 65 66 6f 72 65  .** value before
b8b40 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53   returning..*/.S
b8b50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b8b60 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  t sqlite3PagerSe
b8b70 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 20  tPagesize(Pager 
b8b80 2a 70 50 61 67 65 72 2c 20 75 31 36 20 2a 70 50  *pPager, u16 *pP
b8b90 61 67 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20  ageSize){.  int 
b8ba0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
b8bb0 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 3d    u16 pageSize =
b8bc0 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 61   *pPageSize;.  a
b8bd0 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65 3d  ssert( pageSize=
b8be0 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e  =0 || (pageSize>
b8bf0 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a 65  =512 && pageSize
b8c00 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  <=SQLITE_MAX_PAG
b8c10 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 69 66 28  E_SIZE) );.  if(
b8c20 20 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67   pageSize && pag
b8c30 65 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70  eSize!=pPager->p
b8c40 61 67 65 53 69 7a 65 20 0a 20 20 20 26 26 20 21  ageSize .   && !
b8c50 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 26 26  pPager->memDb &&
b8c60 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 3d 3d 30   pPager->nRef==0
b8c70 20 0a 20 20 29 7b 0a 20 20 20 20 63 68 61 72 20   .  ){.    char 
b8c80 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29  *pNew = (char *)
b8c90 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70  sqlite3_malloc(p
b8ca0 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66  ageSize);.    if
b8cb0 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  ( !pNew ){.     
b8cc0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
b8cd0 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  EM;.    }else{. 
b8ce0 20 20 20 20 20 70 61 67 65 72 45 6e 74 65 72 28       pagerEnter(
b8cf0 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 70  pPager);.      p
b8d00 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65  ager_reset(pPage
b8d10 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  r);.      pPager
b8d20 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67  ->pageSize = pag
b8d30 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 73 65 74  eSize;.      set
b8d40 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65  SectorSize(pPage
b8d50 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
b8d60 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e 70  3_free(pPager->p
b8d70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20  TmpSpace);.     
b8d80 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61   pPager->pTmpSpa
b8d90 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  ce = pNew;.     
b8da0 20 70 61 67 65 72 4c 65 61 76 65 28 70 50 61 67   pagerLeave(pPag
b8db0 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  er);.    }.  }. 
b8dc0 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20 70 50   *pPageSize = pP
b8dd0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a  ager->pageSize;.
b8de0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
b8df0 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
b8e00 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d   set the maximum
b8e10 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63   database page c
b8e20 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69  ount if mxPage i
b8e30 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20  s positive. .** 
b8e40 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20  Make no changes 
b8e50 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72  if mxPage is zer
b8e60 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20  o or negative.  
b8e70 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75 63 65  And never reduce
b8e80 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20   the.** maximum 
b8e90 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77  page count below
b8ea0 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a   the current siz
b8eb0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
b8ec0 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c  e..**.** Regardl
b8ed0 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72  ess of mxPage, r
b8ee0 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e  eturn the curren
b8ef0 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  t maximum page c
b8f00 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ount..*/.SQLITE_
b8f10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b8f20 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43  te3PagerMaxPageC
b8f30 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  ount(Pager *pPag
b8f40 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  er, int mxPage){
b8f50 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20  .  if( mxPage>0 
b8f60 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d  ){.    pPager->m
b8f70 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a  xPgno = mxPage;.
b8f80 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67    }.  sqlite3Pag
b8f90 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
b8fa0 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50  er);.  return pP
b8fb0 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a  ager->mxPgno;.}.
b8fc0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
b8fd0 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74  wing set of rout
b8fe0 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  ines are used to
b8ff0 20 64 69 73 61 62 6c 65 20 74 68 65 20 73 69 6d   disable the sim
b9000 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65 72  ulated.** I/O er
b9010 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20  ror mechanism.  
b9020 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
b9030 72 65 20 75 73 65 64 20 74 6f 20 61 76 6f 69 64  re used to avoid
b9040 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65 72   simulated.** er
b9050 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 73 20 77  rors in places w
b9060 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 63  here we do not c
b9070 61 72 65 20 61 62 6f 75 74 20 65 72 72 6f 72 73  are about errors
b9080 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d  ..**.** Unless -
b9090 44 53 51 4c 49 54 45 5f 54 45 53 54 3d 31 20 69  DSQLITE_TEST=1 i
b90a0 73 20 75 73 65 64 2c 20 74 68 65 73 65 20 72 6f  s used, these ro
b90b0 75 74 69 6e 65 73 20 61 72 65 20 61 6c 6c 20 6e  utines are all n
b90c0 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65 6e  o-ops.** and gen
b90d0 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a  erate no code..*
b90e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
b90f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
b9100 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
b9110 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
b9120 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ing;.SQLITE_API 
b9130 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
b9140 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 3b  e3_io_error_hit;
b9150 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65  .static int save
b9160 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 61  d_cnt;.void disa
b9170 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
b9180 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20  _errors(void){. 
b9190 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c   saved_cnt = sql
b91a0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
b91b0 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 33  nding;.  sqlite3
b91c0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
b91d0 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65  g = -1;.}.void e
b91e0 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
b91f0 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b  io_errors(void){
b9200 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
b9210 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 61  ror_pending = sa
b9220 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65  ved_cnt;.}.#else
b9230 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c  .# define disabl
b9240 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
b9250 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65  rrors().# define
b9260 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65   enable_simulate
b9270 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65  d_io_errors().#e
b9280 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  ndif../*.** Read
b9290 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 74   the first N byt
b92a0 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  es from the begi
b92b0 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c  nning of the fil
b92c0 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a  e into memory.**
b92d0 20 74 68 61 74 20 70 44 65 73 74 20 70 6f 69 6e   that pDest poin
b92e0 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f  ts to. .**.** No
b92f0 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20   error checking 
b9300 69 73 20 64 6f 6e 65 2e 20 54 68 65 20 72 61 74  is done. The rat
b9310 69 6f 6e 61 6c 20 66 6f 72 20 74 68 69 73 20 69  ional for this i
b9320 73 20 74 68 61 74 20 74 68 69 73 20 66 75 6e 63  s that this func
b9330 74 69 6f 6e 20 0a 2a 2a 20 6d 61 79 20 62 65 20  tion .** may be 
b9340 63 61 6c 6c 65 64 20 65 76 65 6e 20 69 66 20 74  called even if t
b9350 68 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74  he file does not
b9360 20 65 78 69 73 74 20 6f 72 20 63 6f 6e 74 61 69   exist or contai
b9370 6e 20 61 20 68 65 61 64 65 72 2e 20 49 6e 20 0a  n a header. In .
b9380 2a 2a 20 74 68 65 73 65 20 63 61 73 65 73 20 73  ** these cases s
b9390 71 6c 69 74 65 33 4f 73 52 65 61 64 28 29 20 77  qlite3OsRead() w
b93a0 69 6c 6c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  ill return an er
b93b0 72 6f 72 2c 20 74 6f 20 77 68 69 63 68 20 74 68  ror, to which th
b93c0 65 20 63 6f 72 72 65 63 74 20 0a 2a 2a 20 72 65  e correct .** re
b93d0 73 70 6f 6e 73 65 20 69 73 20 74 6f 20 7a 65 72  sponse is to zer
b93e0 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 74 20  o the memory at 
b93f0 70 44 65 73 74 20 61 6e 64 20 63 6f 6e 74 69 6e  pDest and contin
b9400 75 65 2e 20 20 41 20 72 65 61 6c 20 49 4f 20 65  ue.  A real IO e
b9410 72 72 6f 72 20 0a 2a 2a 20 77 69 6c 6c 20 70 72  rror .** will pr
b9420 65 73 75 6d 61 62 6c 79 20 72 65 63 75 72 20 61  esumably recur a
b9430 6e 64 20 62 65 20 70 69 63 6b 65 64 20 75 70 20  nd be picked up 
b9440 6c 61 74 65 72 20 28 54 6f 64 6f 3a 20 54 68 69  later (Todo: Thi
b9450 6e 6b 20 61 62 6f 75 74 20 74 68 69 73 29 2e 0a  nk about this)..
b9460 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b9470 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
b9480 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72  erReadFileheader
b9490 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
b94a0 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20  int N, unsigned 
b94b0 63 68 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20  char *pDest){.  
b94c0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
b94d0 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65  OK;.  memset(pDe
b94e0 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73  st, 0, N);.  ass
b94f0 65 72 74 28 4d 45 4d 44 42 7c 7c 70 50 61 67 65  ert(MEMDB||pPage
b9500 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c  r->fd->pMethods|
b9510 7c 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  |pPager->tempFil
b9520 65 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  e);.  if( pPager
b9530 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29  ->fd->pMethods )
b9540 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  {.    IOTRACE(("
b9550 44 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e 22  DBHDR %p 0 %d\n"
b9560 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 20  , pPager, N)).  
b9570 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
b9580 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c  Read(pPager->fd,
b9590 20 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20   pDest, N, 0);. 
b95a0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
b95b0 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45  E_IOERR_SHORT_RE
b95c0 41 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  AD ){.      rc =
b95d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
b95e0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
b95f0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
b9600 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  rn the total num
b9610 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
b9620 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61 73  the disk file as
b9630 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a  sociated with.**
b9640 20 70 50 61 67 65 72 2e 20 0a 2a 2a 0a 2a 2a 20   pPager. .**.** 
b9650 49 66 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 42  If the PENDING_B
b9660 59 54 45 20 6c 69 65 73 20 6f 6e 20 74 68 65 20  YTE lies on the 
b9670 70 61 67 65 20 64 69 72 65 63 74 6c 79 20 61 66  page directly af
b9680 74 65 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ter the end of t
b9690 68 65 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65 6e  he.** file, then
b96a0 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 20 70   consider this p
b96b0 61 67 65 20 70 61 72 74 20 6f 66 20 74 68 65 20  age part of the 
b96c0 66 69 6c 65 20 74 6f 6f 2e 20 46 6f 72 20 65 78  file too. For ex
b96d0 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 50 45 4e  ample, if.** PEN
b96e0 44 49 4e 47 5f 42 59 54 45 20 69 73 20 62 79 74  DING_BYTE is byt
b96f0 65 20 34 30 39 36 20 28 74 68 65 20 66 69 72 73  e 4096 (the firs
b9700 74 20 62 79 74 65 20 6f 66 20 70 61 67 65 20 35  t byte of page 5
b9710 29 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f  ) and the size o
b9720 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 73  f the.** file is
b9730 20 34 30 39 36 20 62 79 74 65 73 2c 20 35 20 69   4096 bytes, 5 i
b9740 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65  s returned inste
b9750 61 64 20 6f 66 20 34 2e 0a 2a 2f 0a 53 51 4c 49  ad of 4..*/.SQLI
b9760 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b9770 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
b9780 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  ount(Pager *pPag
b9790 65 72 29 7b 0a 20 20 69 36 34 20 6e 20 3d 20 30  er){.  i64 n = 0
b97a0 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  ;.  int rc;.  as
b97b0 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20  sert( pPager!=0 
b97c0 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
b97d0 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
b97e0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
b97f0 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  if( pPager->dbSi
b9800 7a 65 3e 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d  ze>=0 ){.    n =
b9810 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b   pPager->dbSize;
b9820 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
b9830 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66  assert(pPager->f
b9840 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61  d->pMethods||pPa
b9850 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a  ger->tempFile);.
b9860 20 20 20 20 69 66 28 20 28 70 50 61 67 65 72 2d      if( (pPager-
b9870 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 20  >fd->pMethods). 
b9880 20 20 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c      && (rc = sql
b9890 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
b98a0 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29 21  Pager->fd, &n))!
b98b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
b98c0 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28      pager_error(
b98d0 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20  pPager, rc);.   
b98e0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
b98f0 20 7d 0a 20 20 20 20 69 66 28 20 6e 3e 30 20 26   }.    if( n>0 &
b9900 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70 61 67 65  & n<pPager->page
b9910 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6e 20  Size ){.      n 
b9920 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 1;.    }else{.
b9930 20 20 20 20 20 20 6e 20 2f 3d 20 70 50 61 67 65        n /= pPage
b9940 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  r->pageSize;.   
b9950 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65   }.    if( pPage
b9960 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f  r->state!=PAGER_
b9970 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20  UNLOCK ){.      
b9980 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
b9990 20 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   n;.    }.  }.  
b99a0 69 66 28 20 6e 3d 3d 28 50 45 4e 44 49 4e 47 5f  if( n==(PENDING_
b99b0 42 59 54 45 2f 70 50 61 67 65 72 2d 3e 70 61 67  BYTE/pPager->pag
b99c0 65 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 6e 2b  eSize) ){.    n+
b99d0 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 70  +;.  }.  if( n>p
b99e0 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b  Pager->mxPgno ){
b99f0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50  .    pPager->mxP
b9a00 67 6e 6f 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 72  gno = n;.  }.  r
b9a10 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 23 69 66  eturn n;.}...#if
b9a20 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
b9a30 5f 4d 45 4d 4f 52 59 44 42 0a 2f 2a 0a 2a 2a 20  _MEMORYDB./*.** 
b9a40 43 6c 65 61 72 20 61 20 50 67 48 69 73 74 6f 72  Clear a PgHistor
b9a50 79 20 62 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69  y block.*/.stati
b9a60 63 20 76 6f 69 64 20 63 6c 65 61 72 48 69 73 74  c void clearHist
b9a70 6f 72 79 28 50 67 48 69 73 74 6f 72 79 20 2a 70  ory(PgHistory *p
b9a80 48 69 73 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  Hist){.  sqlite3
b9a90 5f 66 72 65 65 28 70 48 69 73 74 2d 3e 70 4f 72  _free(pHist->pOr
b9aa0 69 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ig);.  sqlite3_f
b9ab0 72 65 65 28 70 48 69 73 74 2d 3e 70 53 74 6d 74  ree(pHist->pStmt
b9ac0 29 3b 0a 20 20 70 48 69 73 74 2d 3e 70 4f 72 69  );.  pHist->pOri
b9ad0 67 20 3d 20 30 3b 0a 20 20 70 48 69 73 74 2d 3e  g = 0;.  pHist->
b9ae0 70 53 74 6d 74 20 3d 20 30 3b 0a 7d 0a 23 65 6c  pStmt = 0;.}.#el
b9af0 73 65 0a 23 64 65 66 69 6e 65 20 63 6c 65 61 72  se.#define clear
b9b00 48 69 73 74 6f 72 79 28 78 29 0a 23 65 6e 64 69  History(x).#endi
b9b10 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64  f../*.** Forward
b9b20 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2f 0a   declaration.*/.
b9b30 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 4a  static int syncJ
b9b40 6f 75 72 6e 61 6c 28 50 61 67 65 72 2a 29 3b 0a  ournal(Pager*);.
b9b50 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 70 50  ./*.** Unlink pP
b9b60 67 20 66 72 6f 6d 20 69 74 27 73 20 68 61 73 68  g from it's hash
b9b70 20 63 68 61 69 6e 2e 20 41 6c 73 6f 20 73 65 74   chain. Also set
b9b80 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
b9b90 20 74 6f 20 30 20 74 6f 20 69 6e 64 69 63 61 74   to 0 to indicat
b9ba0 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 61  e.** that the pa
b9bb0 67 65 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  ge is not part o
b9bc0 66 20 61 6e 79 20 68 61 73 68 20 63 68 61 69 6e  f any hash chain
b9bd0 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72  . This is requir
b9be0 65 64 20 62 65 63 61 75 73 65 20 74 68 65 0a 2a  ed because the.*
b9bf0 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f  * sqlite3PagerMo
b9c00 76 65 70 61 67 65 28 29 20 72 6f 75 74 69 6e 65  vepage() routine
b9c10 20 63 61 6e 20 6c 65 61 76 65 20 61 20 70 61 67   can leave a pag
b9c20 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 70 4e 65  e in the .** pNe
b9c30 78 74 46 72 65 65 2f 70 50 72 65 76 46 72 65 65  xtFree/pPrevFree
b9c40 20 6c 69 73 74 20 74 68 61 74 20 69 73 20 6e 6f   list that is no
b9c50 74 20 61 20 70 61 72 74 20 6f 66 20 61 6e 79 20  t a part of any 
b9c60 68 61 73 68 2d 63 68 61 69 6e 2e 0a 2a 2f 0a 73  hash-chain..*/.s
b9c70 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 69 6e  tatic void unlin
b9c80 6b 48 61 73 68 43 68 61 69 6e 28 50 61 67 65 72  kHashChain(Pager
b9c90 20 2a 70 50 61 67 65 72 2c 20 50 67 48 64 72 20   *pPager, PgHdr 
b9ca0 2a 70 50 67 29 7b 0a 20 20 69 66 28 20 70 50 67  *pPg){.  if( pPg
b9cb0 2d 3e 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20  ->pgno==0 ){.   
b9cc0 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 4e   assert( pPg->pN
b9cd0 65 78 74 48 61 73 68 3d 3d 30 20 26 26 20 70 50  extHash==0 && pP
b9ce0 67 2d 3e 70 50 72 65 76 48 61 73 68 3d 3d 30 20  g->pPrevHash==0 
b9cf0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
b9d00 20 7d 0a 20 20 69 66 28 20 70 50 67 2d 3e 70 4e   }.  if( pPg->pN
b9d10 65 78 74 48 61 73 68 20 29 7b 0a 20 20 20 20 70  extHash ){.    p
b9d20 50 67 2d 3e 70 4e 65 78 74 48 61 73 68 2d 3e 70  Pg->pNextHash->p
b9d30 50 72 65 76 48 61 73 68 20 3d 20 70 50 67 2d 3e  PrevHash = pPg->
b9d40 70 50 72 65 76 48 61 73 68 3b 0a 20 20 7d 0a 20  pPrevHash;.  }. 
b9d50 20 69 66 28 20 70 50 67 2d 3e 70 50 72 65 76 48   if( pPg->pPrevH
b9d60 61 73 68 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ash ){.    asser
b9d70 74 28 20 70 50 61 67 65 72 2d 3e 61 48 61 73 68  t( pPager->aHash
b9d80 5b 70 50 67 2d 3e 70 67 6e 6f 20 26 20 28 70 50  [pPg->pgno & (pP
b9d90 61 67 65 72 2d 3e 6e 48 61 73 68 2d 31 29 5d 21  ager->nHash-1)]!
b9da0 3d 70 50 67 20 29 3b 0a 20 20 20 20 70 50 67 2d  =pPg );.    pPg-
b9db0 3e 70 50 72 65 76 48 61 73 68 2d 3e 70 4e 65 78  >pPrevHash->pNex
b9dc0 74 48 61 73 68 20 3d 20 70 50 67 2d 3e 70 4e 65  tHash = pPg->pNe
b9dd0 78 74 48 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b  xtHash;.  }else{
b9de0 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 70 50 67  .    int h = pPg
b9df0 2d 3e 70 67 6e 6f 20 26 20 28 70 50 61 67 65 72  ->pgno & (pPager
b9e00 2d 3e 6e 48 61 73 68 2d 31 29 3b 0a 20 20 20 20  ->nHash-1);.    
b9e10 70 50 61 67 65 72 2d 3e 61 48 61 73 68 5b 68 5d  pPager->aHash[h]
b9e20 20 3d 20 70 50 67 2d 3e 70 4e 65 78 74 48 61 73   = pPg->pNextHas
b9e30 68 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d 45 4d  h;.  }.  if( MEM
b9e40 44 42 20 29 7b 0a 20 20 20 20 63 6c 65 61 72 48  DB ){.    clearH
b9e50 69 73 74 6f 72 79 28 50 47 48 44 52 5f 54 4f 5f  istory(PGHDR_TO_
b9e60 48 49 53 54 28 70 50 67 2c 20 70 50 61 67 65 72  HIST(pPg, pPager
b9e70 29 29 3b 0a 20 20 7d 0a 20 20 70 50 67 2d 3e 70  ));.  }.  pPg->p
b9e80 67 6e 6f 20 3d 20 30 3b 0a 20 20 70 50 67 2d 3e  gno = 0;.  pPg->
b9e90 70 4e 65 78 74 48 61 73 68 20 3d 20 70 50 67 2d  pNextHash = pPg-
b9ea0 3e 70 50 72 65 76 48 61 73 68 20 3d 20 30 3b 0a  >pPrevHash = 0;.
b9eb0 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  }../*.** Unlink 
b9ec0 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
b9ed0 66 72 65 65 20 6c 69 73 74 20 28 74 68 65 20 6c  free list (the l
b9ee0 69 73 74 20 6f 66 20 61 6c 6c 20 70 61 67 65 73  ist of all pages
b9ef0 20 77 68 65 72 65 20 6e 52 65 66 3d 3d 30 29 0a   where nRef==0).
b9f00 2a 2a 20 61 6e 64 20 66 72 6f 6d 20 69 74 73 20  ** and from its 
b9f10 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 20 63  hash collision c
b9f20 68 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  hain..*/.static 
b9f30 76 6f 69 64 20 75 6e 6c 69 6e 6b 50 61 67 65 28  void unlinkPage(
b9f40 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50  PgHdr *pPg){.  P
b9f50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
b9f60 50 67 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f  Pg->pPager;..  /
b9f70 2a 20 55 6e 6c 69 6e 6b 20 66 72 6f 6d 20 66 72  * Unlink from fr
b9f80 65 65 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a  ee page list */.
b9f90 20 20 6c 72 75 4c 69 73 74 52 65 6d 6f 76 65 28    lruListRemove(
b9fa0 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 69  pPg);..  /* Unli
b9fb0 6e 6b 20 66 72 6f 6d 20 74 68 65 20 70 67 6e 6f  nk from the pgno
b9fc0 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
b9fd0 20 75 6e 6c 69 6e 6b 48 61 73 68 43 68 61 69 6e   unlinkHashChain
b9fe0 28 70 50 61 67 65 72 2c 20 70 50 67 29 3b 0a 7d  (pPager, pPg);.}
b9ff0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
ba000 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20  tine is used to 
ba010 74 72 75 6e 63 61 74 65 20 74 68 65 20 63 61 63  truncate the cac
ba020 68 65 20 77 68 65 6e 20 61 20 64 61 74 61 62 61  he when a databa
ba030 73 65 0a 2a 2a 20 69 73 20 74 72 75 6e 63 61 74  se.** is truncat
ba040 65 64 2e 20 20 44 72 6f 70 20 66 72 6f 6d 20 74  ed.  Drop from t
ba050 68 65 20 63 61 63 68 65 20 61 6c 6c 20 70 61 67  he cache all pag
ba060 65 73 20 77 68 6f 73 65 20 70 67 6e 6f 20 69 73  es whose pgno is
ba070 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20  .** larger than 
ba080 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 61  pPager->dbSize a
ba090 6e 64 20 69 73 20 75 6e 72 65 66 65 72 65 6e 63  nd is unreferenc
ba0a0 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65  ed..**.** Refere
ba0b0 6e 63 65 64 20 70 61 67 65 73 20 6c 61 72 67 65  nced pages large
ba0c0 72 20 74 68 61 6e 20 70 50 61 67 65 72 2d 3e 64  r than pPager->d
ba0d0 62 53 69 7a 65 20 61 72 65 20 7a 65 72 6f 65 64  bSize are zeroed
ba0e0 2e 0a 2a 2a 0a 2a 2a 20 41 63 74 75 61 6c 6c 79  ..**.** Actually
ba0f0 2c 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20 74  , at the point t
ba100 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
ba110 61 6c 6c 65 64 2c 20 69 74 20 77 6f 75 6c 64 20  alled, it would 
ba120 62 65 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74  be.** an error t
ba130 6f 20 68 61 76 65 20 61 20 72 65 66 65 72 65 6e  o have a referen
ba140 63 65 64 20 70 61 67 65 2e 20 20 42 75 74 20 72  ced page.  But r
ba150 61 74 68 65 72 20 74 68 61 6e 20 64 65 6c 65 74  ather than delet
ba160 65 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20 61  e.** that page a
ba170 6e 64 20 67 75 61 72 61 6e 74 65 65 20 61 20 73  nd guarantee a s
ba180 75 62 73 65 71 75 65 6e 74 20 73 65 67 66 61 75  ubsequent segfau
ba190 6c 74 2c 20 69 74 20 73 65 65 6d 73 20 62 65 74  lt, it seems bet
ba1a0 74 65 72 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 69  ter.** to zero i
ba1b0 74 20 61 6e 64 20 68 6f 70 65 20 74 68 61 74 20  t and hope that 
ba1c0 77 65 20 65 72 72 6f 72 20 6f 75 74 20 73 61 6e  we error out san
ba1d0 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ely..*/.static v
ba1e0 6f 69 64 20 70 61 67 65 72 5f 74 72 75 6e 63 61  oid pager_trunca
ba1f0 74 65 5f 63 61 63 68 65 28 50 61 67 65 72 20 2a  te_cache(Pager *
ba200 70 50 61 67 65 72 29 7b 0a 20 20 50 67 48 64 72  pPager){.  PgHdr
ba210 20 2a 70 50 67 3b 0a 20 20 50 67 48 64 72 20 2a   *pPg;.  PgHdr *
ba220 2a 70 70 50 67 3b 0a 20 20 69 6e 74 20 64 62 53  *ppPg;.  int dbS
ba230 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62  ize = pPager->db
ba240 53 69 7a 65 3b 0a 0a 20 20 70 70 50 67 20 3d 20  Size;..  ppPg = 
ba250 26 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 0a 20  &pPager->pAll;. 
ba260 20 77 68 69 6c 65 28 20 28 70 50 67 20 3d 20 2a   while( (pPg = *
ba270 70 70 50 67 29 21 3d 30 20 29 7b 0a 20 20 20 20  ppPg)!=0 ){.    
ba280 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 64  if( pPg->pgno<=d
ba290 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70  bSize ){.      p
ba2a0 70 50 67 20 3d 20 26 70 50 67 2d 3e 70 4e 65 78  pPg = &pPg->pNex
ba2b0 74 41 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 20  tAll;.    }else 
ba2c0 69 66 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20  if( pPg->nRef>0 
ba2d0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  ){.      memset(
ba2e0 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50  PGHDR_TO_DATA(pP
ba2f0 67 29 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70  g), 0, pPager->p
ba300 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  ageSize);.      
ba310 70 70 50 67 20 3d 20 26 70 50 67 2d 3e 70 4e 65  ppPg = &pPg->pNe
ba320 78 74 41 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65  xtAll;.    }else
ba330 7b 0a 20 20 20 20 20 20 2a 70 70 50 67 20 3d 20  {.      *ppPg = 
ba340 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 3b 0a 20  pPg->pNextAll;. 
ba350 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 50       IOTRACE(("P
ba360 47 46 52 45 45 20 25 70 20 25 64 5c 6e 22 2c 20  GFREE %p %d\n", 
ba370 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e  pPager, pPg->pgn
ba380 6f 29 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52  o));.      PAGER
ba390 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61  _INCR(sqlite3_pa
ba3a0 67 65 72 5f 70 67 66 72 65 65 5f 63 6f 75 6e 74  ger_pgfree_count
ba3b0 29 3b 0a 20 20 20 20 20 20 75 6e 6c 69 6e 6b 50  );.      unlinkP
ba3c0 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20  age(pPg);.      
ba3d0 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a  makeClean(pPg);.
ba3e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
ba3f0 65 65 28 70 50 67 2d 3e 70 44 61 74 61 29 3b 0a  ee(pPg->pData);.
ba400 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
ba410 65 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 70  ee(pPg);.      p
ba420 50 61 67 65 72 2d 3e 6e 50 61 67 65 2d 2d 3b 0a  Pager->nPage--;.
ba430 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
ba440 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e  ** Try to obtain
ba450 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c   a lock on a fil
ba460 65 2e 20 20 49 6e 76 6f 6b 65 20 74 68 65 20 62  e.  Invoke the b
ba470 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 20  usy callback if 
ba480 74 68 65 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 63  the lock.** is c
ba490 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61  urrently not ava
ba4a0 69 6c 61 62 6c 65 2e 20 20 52 65 70 65 61 74 20  ilable.  Repeat 
ba4b0 75 6e 74 69 6c 20 74 68 65 20 62 75 73 79 20 63  until the busy c
ba4c0 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a  allback returns.
ba4d0 2a 2a 20 66 61 6c 73 65 20 6f 72 20 75 6e 74 69  ** false or unti
ba4e0 6c 20 74 68 65 20 6c 6f 63 6b 20 73 75 63 63 65  l the lock succe
ba4f0 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  eds..**.** Retur
ba500 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
ba510 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65 72  uccess and an er
ba520 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 63  ror code if we c
ba530 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20  annot obtain.** 
ba540 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61  the lock..*/.sta
ba550 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 61  tic int pager_wa
ba560 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72  it_on_lock(Pager
ba570 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f   *pPager, int lo
ba580 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72  cktype){.  int r
ba590 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20  c;..  /* The OS 
ba5a0 6c 6f 63 6b 20 76 61 6c 75 65 73 20 6d 75 73 74  lock values must
ba5b0 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20   be the same as 
ba5c0 74 68 65 20 50 61 67 65 72 20 6c 6f 63 6b 20 76  the Pager lock v
ba5d0 61 6c 75 65 73 20 2a 2f 0a 20 20 61 73 73 65 72  alues */.  asser
ba5e0 74 28 20 50 41 47 45 52 5f 53 48 41 52 45 44 3d  t( PAGER_SHARED=
ba5f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
ba600 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
ba610 52 45 53 45 52 56 45 44 3d 3d 52 45 53 45 52 56  RESERVED==RESERV
ba620 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
ba630 65 72 74 28 20 50 41 47 45 52 5f 45 58 43 4c 55  ert( PAGER_EXCLU
ba640 53 49 56 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f  SIVE==EXCLUSIVE_
ba650 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  LOCK );..  /* If
ba660 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 75 72   the file is cur
ba670 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20  rently unlocked 
ba680 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6d 75  then the size mu
ba690 73 74 20 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f  st be unknown */
ba6a0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
ba6b0 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
ba6c0 53 48 41 52 45 44 20 7c 7c 20 70 50 61 67 65 72  SHARED || pPager
ba6d0 2d 3e 64 62 53 69 7a 65 3c 30 20 7c 7c 20 4d 45  ->dbSize<0 || ME
ba6e0 4d 44 42 20 29 3b 0a 0a 20 20 69 66 28 20 70 50  MDB );..  if( pP
ba6f0 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63  ager->state>=loc
ba700 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 63 20  ktype ){.    rc 
ba710 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
ba720 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a 20  else{.    do {. 
ba730 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
ba740 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e  3OsLock(pPager->
ba750 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20  fd, locktype);. 
ba760 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53     }while( rc==S
ba770 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 73 71  QLITE_BUSY && sq
ba780 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48  lite3InvokeBusyH
ba790 61 6e 64 6c 65 72 28 70 50 61 67 65 72 2d 3e 70  andler(pPager->p
ba7a0 42 75 73 79 48 61 6e 64 6c 65 72 29 20 29 3b 0a  BusyHandler) );.
ba7b0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
ba7c0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
ba7d0 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 6c  Pager->state = l
ba7e0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 49  ocktype;.      I
ba7f0 4f 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 25 70  OTRACE(("LOCK %p
ba800 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
ba810 6c 6f 63 6b 74 79 70 65 29 29 0a 20 20 20 20 7d  locktype)).    }
ba820 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
ba830 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63  ;.}../*.** Trunc
ba840 61 74 65 20 74 68 65 20 66 69 6c 65 20 74 6f 20  ate the file to 
ba850 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
ba860 67 65 73 20 73 70 65 63 69 66 69 65 64 2e 0a 2a  ges specified..*
ba870 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
ba880 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
ba890 72 54 72 75 6e 63 61 74 65 28 50 61 67 65 72 20  rTruncate(Pager 
ba8a0 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50  *pPager, Pgno nP
ba8b0 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  age){.  int rc;.
ba8c0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
ba8d0 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53  ->state>=PAGER_S
ba8e0 48 41 52 45 44 20 7c 7c 20 4d 45 4d 44 42 20 29  HARED || MEMDB )
ba8f0 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ;.  sqlite3Pager
ba900 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
ba910 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
ba920 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
ba930 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72  rc = pPager->err
ba940 43 6f 64 65 3b 0a 20 20 20 20 72 65 74 75 72 6e  Code;.    return
ba950 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e   rc;.  }.  if( n
ba960 50 61 67 65 3e 3d 28 75 6e 73 69 67 6e 65 64 29  Page>=(unsigned)
ba970 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29  pPager->dbSize )
ba980 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
ba990 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
ba9a0 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70  ( MEMDB ){.    p
ba9b0 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
ba9c0 6e 50 61 67 65 3b 0a 20 20 20 20 70 61 67 65 72  nPage;.    pager
ba9d0 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65 28  _truncate_cache(
ba9e0 70 50 61 67 65 72 29 3b 0a 20 20 20 20 72 65 74  pPager);.    ret
ba9f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
baa00 20 7d 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28   }.  pagerEnter(
baa10 70 50 61 67 65 72 29 3b 0a 20 20 72 63 20 3d 20  pPager);.  rc = 
baa20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67  syncJournal(pPag
baa30 65 72 29 3b 0a 20 20 70 61 67 65 72 4c 65 61 76  er);.  pagerLeav
baa40 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28  e(pPager);.  if(
baa50 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
baa60 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
baa70 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 61  .  }..  /* Get a
baa80 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
baa90 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
baaa0 20 62 65 66 6f 72 65 20 74 72 75 6e 63 61 74 69   before truncati
baab0 6e 67 2e 20 2a 2f 0a 20 20 70 61 67 65 72 45 6e  ng. */.  pagerEn
baac0 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20 20 72  ter(pPager);.  r
baad0 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f  c = pager_wait_o
baae0 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45  n_lock(pPager, E
baaf0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a  XCLUSIVE_LOCK);.
bab00 20 20 70 61 67 65 72 4c 65 61 76 65 28 70 50 61    pagerLeave(pPa
bab10 67 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ger);.  if( rc!=
bab20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
bab30 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
bab40 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72  .  rc = pager_tr
bab50 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 6e  uncate(pPager, n
bab60 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Page);.  return 
bab70 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75  rc;.}../*.** Shu
bab80 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63  tdown the page c
bab90 61 63 68 65 2e 20 20 46 72 65 65 20 61 6c 6c 20  ache.  Free all 
baba0 6d 65 6d 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65  memory and close
babb0 20 61 6c 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a   all files..**.*
babc0 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  * If a transacti
babd0 6f 6e 20 77 61 73 20 69 6e 20 70 72 6f 67 72 65  on was in progre
babe0 73 73 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  ss when this rou
babf0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
bac00 74 68 61 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74  that.** transact
bac10 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
bac20 63 6b 2e 20 20 41 6c 6c 20 6f 75 74 73 74 61 6e  ck.  All outstan
bac30 64 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 69  ding pages are i
bac40 6e 76 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61 6e  nvalidated.** an
bac50 64 20 74 68 65 69 72 20 6d 65 6d 6f 72 79 20 69  d their memory i
bac60 73 20 66 72 65 65 64 2e 20 20 41 6e 79 20 61 74  s freed.  Any at
bac70 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 70  tempt to use a p
bac80 61 67 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  age associated.*
bac90 2a 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65  * with this page
baca0 20 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69   cache after thi
bacb0 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
bacc0 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a  ns will likely.*
bacd0 2a 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f  * result in a co
bace0 72 65 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  redump..**.** Th
bacf0 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61  is function alwa
bad00 79 73 20 73 75 63 63 65 65 64 73 2e 20 49 66 20  ys succeeds. If 
bad10 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
bad20 20 61 63 74 69 76 65 20 61 6e 20 61 74 74 65 6d   active an attem
bad30 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f  pt.** is made to
bad40 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 49   roll it back. I
bad50 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
bad60 73 20 64 75 72 69 6e 67 20 74 68 65 20 72 6f 6c  s during the rol
bad70 6c 62 61 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 20  lback .** a hot 
bad80 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 62 65 20 6c  journal may be l
bad90 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 73  eft in the files
bada0 79 73 74 65 6d 20 62 75 74 20 6e 6f 20 65 72 72  ystem but no err
badb0 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a  or is returned.*
badc0 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e  * to the caller.
badd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
bade0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
badf0 67 65 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a  gerClose(Pager *
bae00 70 50 61 67 65 72 29 7b 0a 23 69 66 64 65 66 20  pPager){.#ifdef 
bae10 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
bae20 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
bae30 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a    if( !MEMDB ){.
bae40 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
bae50 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  x *mutex = sqlit
bae60 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
bae70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
bae80 49 43 5f 4d 45 4d 32 29 3b 0a 20 20 20 20 73 71  IC_MEM2);.    sq
bae90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
baea0 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66  r(mutex);.    if
baeb0 28 20 70 50 61 67 65 72 2d 3e 70 50 72 65 76 20  ( pPager->pPrev 
baec0 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
baed0 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
baee0 70 50 61 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20  pPager->pNext;. 
baef0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
baf00 73 71 6c 69 74 65 33 50 61 67 65 72 4c 69 73 74  sqlite3PagerList
baf10 20 3d 20 70 50 61 67 65 72 2d 3e 70 4e 65 78 74   = pPager->pNext
baf20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
baf30 70 50 61 67 65 72 2d 3e 70 4e 65 78 74 20 29 7b  pPager->pNext ){
baf40 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70  .      pPager->p
baf50 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 50  Next->pPrev = pP
baf60 61 67 65 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20  ager->pPrev;.   
baf70 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d   }.    sqlite3_m
baf80 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
baf90 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
bafa0 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74   disable_simulat
bafb0 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a  ed_io_errors();.
bafc0 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64    pPager->errCod
bafd0 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  e = 0;.  pPager-
bafe0 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d  >exclusiveMode =
baff0 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65   0;.  pager_rese
bb000 74 28 70 50 61 67 65 72 29 3b 0a 20 20 70 61 67  t(pPager);.  pag
bb010 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  erUnlockAndRollb
bb020 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 65  ack(pPager);.  e
bb030 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
bb040 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50  io_errors();.  P
bb050 41 47 45 52 54 52 41 43 45 32 28 22 43 4c 4f 53  AGERTRACE2("CLOS
bb060 45 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  E %d\n", PAGERID
bb070 28 70 50 61 67 65 72 29 29 3b 0a 20 20 49 4f 54  (pPager));.  IOT
bb080 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c  RACE(("CLOSE %p\
bb090 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 61  n", pPager)).  a
bb0a0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65  ssert( pPager->e
bb0b0 72 72 43 6f 64 65 20 7c 7c 20 28 70 50 61 67 65  rrCode || (pPage
bb0c0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d  r->journalOpen==
bb0d0 30 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 6d  0 && pPager->stm
bb0e0 74 4f 70 65 6e 3d 3d 30 29 20 29 3b 0a 20 20 69  tOpen==0) );.  i
bb0f0 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
bb100 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 73 71  alOpen ){.    sq
bb110 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
bb120 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 7d 0a 20  ger->jfd);.  }. 
bb130 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
bb140 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c  ager->aInJournal
bb150 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
bb160 3e 73 74 6d 74 4f 70 65 6e 20 29 7b 0a 20 20 20  >stmtOpen ){.   
bb170 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
bb180 70 50 61 67 65 72 2d 3e 73 74 66 64 29 3b 0a 20  pPager->stfd);. 
bb190 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c   }.  sqlite3OsCl
bb1a0 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ose(pPager->fd);
bb1b0 0a 20 20 2f 2a 20 54 65 6d 70 20 66 69 6c 65 73  .  /* Temp files
bb1c0 20 61 72 65 20 61 75 74 6f 6d 61 74 69 63 61 6c   are automatical
bb1d0 6c 79 20 64 65 6c 65 74 65 64 20 62 79 20 74 68  ly deleted by th
bb1e0 65 20 4f 53 0a 20 20 2a 2a 20 69 66 28 20 70 50  e OS.  ** if( pP
bb1f0 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
bb200 7b 0a 20 20 2a 2a 20 20 20 73 71 6c 69 74 65 33  {.  **   sqlite3
bb210 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d  OsDelete(pPager-
bb220 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 2a  >zFilename);.  *
bb230 2a 20 7d 0a 20 20 2a 2f 0a 0a 20 20 73 71 6c 69  * }.  */..  sqli
bb240 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d  te3_free(pPager-
bb250 3e 61 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74  >aHash);.  sqlit
bb260 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e  e3_free(pPager->
bb270 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71  pTmpSpace);.  sq
bb280 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65  lite3_free(pPage
bb290 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  r);.  return SQL
bb2a0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21  ITE_OK;.}..#if !
bb2b0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20  defined(NDEBUG) 
bb2c0 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
bb2d0 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65  E_TEST)./*.** Re
bb2e0 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e 75  turn the page nu
bb2f0 6d 62 65 72 20 66 6f 72 20 74 68 65 20 67 69 76  mber for the giv
bb300 65 6e 20 70 61 67 65 20 64 61 74 61 2e 0a 2a 2f  en page data..*/
bb310 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
bb320 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65  Pgno sqlite3Page
bb330 72 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61  rPagenumber(DbPa
bb340 67 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ge *p){.  return
bb350 20 70 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64   p->pgno;.}.#end
bb360 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  if../*.** The pa
bb370 67 65 5f 72 65 66 28 29 20 66 75 6e 63 74 69 6f  ge_ref() functio
bb380 6e 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65  n increments the
bb390 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
bb3a0 20 66 6f 72 20 61 20 70 61 67 65 2e 0a 2a 2a 20   for a page..** 
bb3b0 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 63  If the page is c
bb3c0 75 72 72 65 6e 74 6c 79 20 6f 6e 20 74 68 65 20  urrently on the 
bb3d0 66 72 65 65 6c 69 73 74 20 28 74 68 65 20 72 65  freelist (the re
bb3e0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 73  ference count is
bb3f0 20 7a 65 72 6f 29 20 74 68 65 6e 0a 2a 2a 20 72   zero) then.** r
bb400 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d 20 74 68  emove it from th
bb410 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a  e freelist..**.*
bb420 2a 20 46 6f 72 20 6e 6f 6e 2d 74 65 73 74 20 73  * For non-test s
bb430 79 73 74 65 6d 73 2c 20 70 61 67 65 5f 72 65 66  ystems, page_ref
bb440 28 29 20 69 73 20 61 20 6d 61 63 72 6f 20 74 68  () is a macro th
bb450 61 74 20 63 61 6c 6c 73 20 5f 70 61 67 65 5f 72  at calls _page_r
bb460 65 66 28 29 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 6f  ef().** online o
bb470 66 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  f the reference 
bb480 63 6f 75 6e 74 20 69 73 20 7a 65 72 6f 2e 20 20  count is zero.  
bb490 46 6f 72 20 74 65 73 74 20 73 79 73 74 65 6d 73  For test systems
bb4a0 2c 20 70 61 67 65 5f 72 65 66 28 29 0a 2a 2a 20  , page_ref().** 
bb4b0 69 73 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69  is a real functi
bb4c0 6f 6e 20 73 6f 20 74 68 61 74 20 77 65 20 63 61  on so that we ca
bb4d0 6e 20 73 65 74 20 62 72 65 61 6b 70 6f 69 6e 74  n set breakpoint
bb4e0 73 20 61 6e 64 20 74 72 61 63 65 20 69 74 2e 0a  s and trace it..
bb4f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 5f  */.static void _
bb500 70 61 67 65 5f 72 65 66 28 50 67 48 64 72 20 2a  page_ref(PgHdr *
bb510 70 50 67 29 7b 0a 20 20 69 66 28 20 70 50 67 2d  pPg){.  if( pPg-
bb520 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
bb530 2f 2a 20 54 68 65 20 70 61 67 65 20 69 73 20 63  /* The page is c
bb540 75 72 72 65 6e 74 6c 79 20 6f 6e 20 74 68 65 20  urrently on the 
bb550 66 72 65 65 6c 69 73 74 2e 20 20 52 65 6d 6f 76  freelist.  Remov
bb560 65 20 69 74 2e 20 2a 2f 0a 20 20 20 20 6c 72 75  e it. */.    lru
bb570 4c 69 73 74 52 65 6d 6f 76 65 28 70 50 67 29 3b  ListRemove(pPg);
bb580 0a 20 20 20 20 70 50 67 2d 3e 70 50 61 67 65 72  .    pPg->pPager
bb590 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20  ->nRef++;.  }.  
bb5a0 70 50 67 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 52  pPg->nRef++;.  R
bb5b0 45 46 49 4e 46 4f 28 70 50 67 29 3b 0a 7d 0a 23  EFINFO(pPg);.}.#
bb5c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
bb5d0 55 47 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64  UG.  static void
bb5e0 20 70 61 67 65 5f 72 65 66 28 50 67 48 64 72 20   page_ref(PgHdr 
bb5f0 2a 70 50 67 29 7b 0a 20 20 20 20 69 66 28 20 70  *pPg){.    if( p
bb600 50 67 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20  Pg->nRef==0 ){. 
bb610 20 20 20 20 20 5f 70 61 67 65 5f 72 65 66 28 70       _page_ref(p
bb620 50 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Pg);.    }else{.
bb630 20 20 20 20 20 20 70 50 67 2d 3e 6e 52 65 66 2b        pPg->nRef+
bb640 2b 3b 0a 20 20 20 20 20 20 52 45 46 49 4e 46 4f  +;.      REFINFO
bb650 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  (pPg);.    }.  }
bb660 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
bb670 70 61 67 65 5f 72 65 66 28 50 29 20 20 20 28 28  page_ref(P)   ((
bb680 50 29 2d 3e 6e 52 65 66 3d 3d 30 3f 5f 70 61 67  P)->nRef==0?_pag
bb690 65 5f 72 65 66 28 50 29 3a 28 76 6f 69 64 29 28  e_ref(P):(void)(
bb6a0 50 29 2d 3e 6e 52 65 66 2b 2b 29 0a 23 65 6e 64  P)->nRef++).#end
bb6b0 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d  if../*.** Increm
bb6c0 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63  ent the referenc
bb6d0 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 70 61  e count for a pa
bb6e0 67 65 2e 20 20 54 68 65 20 69 6e 70 75 74 20 70  ge.  The input p
bb6f0 6f 69 6e 74 65 72 20 69 73 0a 2a 2a 20 61 20 72  ointer is.** a r
bb700 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
bb710 70 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a 53 51  page data..*/.SQ
bb720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
bb730 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
bb740 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20  (DbPage *pPg){. 
bb750 20 70 61 67 65 72 45 6e 74 65 72 28 70 50 67 2d   pagerEnter(pPg-
bb760 3e 70 50 61 67 65 72 29 3b 0a 20 20 70 61 67 65  >pPager);.  page
bb770 5f 72 65 66 28 70 50 67 29 3b 0a 20 20 70 61 67  _ref(pPg);.  pag
bb780 65 72 4c 65 61 76 65 28 70 50 67 2d 3e 70 50 61  erLeave(pPg->pPa
bb790 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ger);.  return S
bb7a0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
bb7b0 2a 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72  ** Sync the jour
bb7c0 6e 61 6c 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  nal.  In other w
bb7d0 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75 72 65 20  ords, make sure 
bb7e0 61 6c 6c 20 74 68 65 20 70 61 67 65 73 20 74 68  all the pages th
bb7f0 61 74 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20  at have.** been 
bb800 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
bb810 6f 75 72 6e 61 6c 20 68 61 76 65 20 61 63 74 75  ournal have actu
bb820 61 6c 6c 79 20 72 65 61 63 68 65 64 20 74 68 65  ally reached the
bb830 20 73 75 72 66 61 63 65 20 6f 66 20 74 68 65 0a   surface of the.
bb840 2a 2a 20 64 69 73 6b 2e 20 20 49 74 20 69 73 20  ** disk.  It is 
bb850 6e 6f 74 20 73 61 66 65 20 74 6f 20 6d 6f 64 69  not safe to modi
bb860 66 79 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  fy the original 
bb870 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 6e  database file un
bb880 74 69 6c 20 61 66 74 65 72 0a 2a 2a 20 74 68 65  til after.** the
bb890 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 62 65 65   journal has bee
bb8a0 6e 20 73 79 6e 63 65 64 2e 20 20 49 66 20 74 68  n synced.  If th
bb8b0 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62  e original datab
bb8c0 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20  ase is modified 
bb8d0 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6a 6f  before.** the jo
bb8e0 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20  urnal is synced 
bb8f0 61 6e 64 20 61 20 70 6f 77 65 72 20 66 61 69 6c  and a power fail
bb900 75 72 65 20 6f 63 63 75 72 73 2c 20 74 68 65 20  ure occurs, the 
bb910 75 6e 73 79 6e 63 65 64 20 6a 6f 75 72 6e 61 6c  unsynced journal
bb920 0a 2a 2a 20 64 61 74 61 20 77 6f 75 6c 64 20 62  .** data would b
bb930 65 20 6c 6f 73 74 20 61 6e 64 20 77 65 20 77 6f  e lost and we wo
bb940 75 6c 64 20 62 65 20 75 6e 61 62 6c 65 20 74 6f  uld be unable to
bb950 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 6f 6c 6c   completely roll
bb960 62 61 63 6b 20 74 68 65 0a 2a 2a 20 64 61 74 61  back the.** data
bb970 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 44  base changes.  D
bb980 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
bb990 6f 6e 20 77 6f 75 6c 64 20 6f 63 63 75 72 2e 0a  on would occur..
bb9a0 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ** .** This rout
bb9b0 69 6e 65 20 61 6c 73 6f 20 75 70 64 61 74 65 73  ine also updates
bb9c0 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20   the nRec field 
bb9d0 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 6f 66  in the header of
bb9e0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a   the journal..**
bb9f0 20 28 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f   (See comments o
bba00 6e 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 79  n the pager_play
bba10 62 61 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 66  back() routine f
bba20 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
bba30 66 6f 72 6d 61 74 69 6f 6e 2e 29 0a 2a 2a 20 49  formation.).** I
bba40 66 20 74 68 65 20 73 79 6e 63 20 6d 6f 64 65 20  f the sync mode 
bba50 69 73 20 46 55 4c 4c 2c 20 74 77 6f 20 73 79 6e  is FULL, two syn
bba60 63 73 20 77 69 6c 6c 20 6f 63 63 75 72 2e 20 20  cs will occur.  
bba70 46 69 72 73 74 20 74 68 65 20 77 68 6f 6c 65 20  First the whole 
bba80 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 73 79  journal.** is sy
bba90 6e 63 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6e  nced, then the n
bbaa0 52 65 63 20 66 69 65 6c 64 20 69 73 20 75 70 64  Rec field is upd
bbab0 61 74 65 64 2c 20 74 68 65 6e 20 61 20 73 65 63  ated, then a sec
bbac0 6f 6e 64 20 73 79 6e 63 20 6f 63 63 75 72 73 2e  ond sync occurs.
bbad0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 65 6d 70 6f  .**.** For tempo
bbae0 72 61 72 79 20 64 61 74 61 62 61 73 65 73 2c 20  rary databases, 
bbaf0 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 69  we do not care i
bbb00 66 20 77 65 20 61 72 65 20 61 62 6c 65 20 74 6f  f we are able to
bbb10 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 61 66 74   rollback.** aft
bbb20 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75  er a power failu
bbb30 72 65 2c 20 73 6f 20 6e 6f 20 73 79 6e 63 20 6f  re, so no sync o
bbb40 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ccurs..**.** If 
bbb50 74 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e  the IOCAP_SEQUEN
bbb60 54 49 41 4c 20 66 6c 61 67 20 69 73 20 73 65 74  TIAL flag is set
bbb70 20 66 6f 72 20 74 68 65 20 70 65 72 73 69 73 74   for the persist
bbb80 65 6e 74 20 6d 65 64 69 61 20 6f 6e 20 77 68 69  ent media on whi
bbb90 63 68 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  ch.** the databa
bbba0 73 65 20 69 73 20 73 74 6f 72 65 64 2c 20 74 68  se is stored, th
bbbb0 65 6e 20 4f 73 53 79 6e 63 28 29 20 69 73 20 6e  en OsSync() is n
bbbc0 65 76 65 72 20 63 61 6c 6c 65 64 20 6f 6e 20 74  ever called on t
bbbd0 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69  he journal.** fi
bbbe0 6c 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  le. In this case
bbbf0 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71   all that is req
bbc00 75 69 72 65 64 20 69 73 20 74 6f 20 75 70 64 61  uired is to upda
bbc10 74 65 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  te the nRec fiel
bbc20 64 20 69 6e 0a 2a 2a 20 74 68 65 20 6a 6f 75 72  d in.** the jour
bbc30 6e 61 6c 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a  nal header..**.*
bbc40 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
bbc50 6c 65 61 72 73 20 74 68 65 20 6e 65 65 64 53 79  lears the needSy
bbc60 6e 63 20 66 69 65 6c 64 20 6f 66 20 65 76 65 72  nc field of ever
bbc70 79 20 70 61 67 65 20 63 75 72 72 65 6e 74 20 68  y page current h
bbc80 65 6c 64 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79  eld in.** memory
bbc90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
bbca0 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65  syncJournal(Page
bbcb0 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 50 67  r *pPager){.  Pg
bbcc0 48 64 72 20 2a 70 50 67 3b 0a 20 20 69 6e 74 20  Hdr *pPg;.  int 
bbcd0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
bbce0 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20  ..  /* Sync the 
bbcf0 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 6d  journal before m
bbd00 6f 64 69 66 79 69 6e 67 20 74 68 65 20 6d 61 69  odifying the mai
bbd10 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  n database.  ** 
bbd20 28 61 73 73 75 6d 69 6e 67 20 74 68 65 72 65 20  (assuming there 
bbd30 69 73 20 61 20 6a 6f 75 72 6e 61 6c 20 61 6e 64  is a journal and
bbd40 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20   it needs to be 
bbd50 73 79 6e 63 65 64 2e 29 0a 20 20 2a 2f 0a 20 20  synced.).  */.  
bbd60 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64  if( pPager->need
bbd70 53 79 6e 63 20 29 7b 0a 20 20 20 20 69 66 28 20  Sync ){.    if( 
bbd80 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
bbd90 65 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  e ){.      int i
bbda0 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65  Dc = sqlite3OsDe
bbdb0 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
bbdc0 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ics(pPager->fd);
bbdd0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
bbde0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
bbdf0 65 6e 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  en );..      /* 
bbe00 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d  assert( !pPager-
bbe10 3e 6e 6f 53 79 6e 63 20 29 3b 20 2f 2f 20 6e 6f  >noSync ); // no
bbe20 53 79 6e 63 20 6d 69 67 68 74 20 62 65 20 73 65  Sync might be se
bbe30 74 20 69 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73  t if synchronous
bbe40 0a 20 20 20 20 20 20 2a 2a 20 77 61 73 20 74 75  .      ** was tu
bbe50 72 6e 65 64 20 6f 66 66 20 61 66 74 65 72 20 74  rned off after t
bbe60 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  he transaction w
bbe70 61 73 20 73 74 61 72 74 65 64 2e 20 20 54 69 63  as started.  Tic
bbe80 6b 65 74 20 23 36 31 35 20 2a 2f 0a 23 69 66 6e  ket #615 */.#ifn
bbe90 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20  def NDEBUG.     
bbea0 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61   {.        /* Ma
bbeb0 6b 65 20 73 75 72 65 20 74 68 65 20 70 50 61 67  ke sure the pPag
bbec0 65 72 2d 3e 6e 52 65 63 20 63 6f 75 6e 74 65 72  er->nRec counter
bbed0 20 77 65 20 61 72 65 20 6b 65 65 70 69 6e 67 20   we are keeping 
bbee0 61 67 72 65 65 73 0a 20 20 20 20 20 20 20 20 2a  agrees.        *
bbef0 2a 20 77 69 74 68 20 74 68 65 20 6e 52 65 63 20  * with the nRec 
bbf00 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d 20 74 68  computed from th
bbf10 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f  e size of the jo
bbf20 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20  urnal file..    
bbf30 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
bbf40 36 34 20 6a 53 7a 3b 0a 20 20 20 20 20 20 20 20  64 jSz;.        
bbf50 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
bbf60 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a  leSize(pPager->j
bbf70 66 64 2c 20 26 6a 53 7a 29 3b 0a 20 20 20 20 20  fd, &jSz);.     
bbf80 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 72     if( rc!=0 ) r
bbf90 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
bbfa0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
bbfb0 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a 53  ->journalOff==jS
bbfc0 7a 20 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  z );.      }.#en
bbfd0 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 30 3d  dif.      if( 0=
bbfe0 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43  =(iDc&SQLITE_IOC
bbff0 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20  AP_SAFE_APPEND) 
bc000 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ){.        /* Wr
bc010 69 74 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c  ite the nRec val
bc020 75 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  ue into the jour
bc030 6e 61 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e  nal file header.
bc040 20 49 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a   If in.        *
bc050 2a 20 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f  * full-synchrono
bc060 75 73 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68  us mode, sync th
bc070 65 20 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e  e journal first.
bc080 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   This ensures th
bc090 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c  at.        ** al
bc0a0 6c 20 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c  l data has reall
bc0b0 79 20 68 69 74 20 74 68 65 20 64 69 73 6b 20 62  y hit the disk b
bc0c0 65 66 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70  efore nRec is up
bc0d0 64 61 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20  dated to mark.  
bc0e0 20 20 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61        ** it as a
bc0f0 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72   candidate for r
bc100 6f 6c 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20  ollback..       
bc110 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54   **.        ** T
bc120 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69  his is not requi
bc130 72 65 64 20 69 66 20 74 68 65 20 70 65 72 73 69  red if the persi
bc140 73 74 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70  stent media supp
bc150 6f 72 74 73 20 74 68 65 0a 20 20 20 20 20 20 20  orts the.       
bc160 20 2a 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20   ** SAFE_APPEND 
bc170 70 72 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73  property. Becaus
bc180 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69  e in this case i
bc190 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
bc1a0 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f  e .        ** fo
bc1b0 72 20 67 61 72 62 61 67 65 20 64 61 74 61 20 74  r garbage data t
bc1c0 6f 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f  o be appended to
bc1d0 20 74 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e   the file, the n
bc1e0 52 65 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20  Rec field.      
bc1f0 20 20 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65    ** is populate
bc200 64 20 77 69 74 68 20 30 78 46 46 46 46 46 46 46  d with 0xFFFFFFF
bc210 46 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  F when the journ
bc220 61 6c 20 68 65 61 64 65 72 20 69 73 20 77 72 69  al header is wri
bc230 74 74 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  tten.        ** 
bc240 61 6e 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20  and never needs 
bc250 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20  to be updated.. 
bc260 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
bc270 20 20 69 36 34 20 6a 72 6e 6c 4f 66 66 3b 0a 20    i64 jrnlOff;. 
bc280 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
bc290 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 30  r->fullSync && 0
bc2a0 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f  ==(iDc&SQLITE_IO
bc2b0 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20  CAP_SEQUENTIAL) 
bc2c0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 41 47  ){.          PAG
bc2d0 45 52 54 52 41 43 45 32 28 22 53 59 4e 43 20 6a  ERTRACE2("SYNC j
bc2e0 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c  ournal of %d\n",
bc2f0 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
bc300 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f 54  );.          IOT
bc310 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c  RACE(("JSYNC %p\
bc320 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20  n", pPager)).   
bc330 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
bc340 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72  te3OsSync(pPager
bc350 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73  ->jfd, pPager->s
bc360 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20  ync_flags);.    
bc370 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 30 20        if( rc!=0 
bc380 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
bc390 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
bc3a0 6a 72 6e 6c 4f 66 66 20 3d 20 70 50 61 67 65 72  jrnlOff = pPager
bc3b0 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 2b 20 73  ->journalHdr + s
bc3c0 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
bc3d0 67 69 63 29 3b 0a 20 20 20 20 20 20 20 20 49 4f  gic);.        IO
bc3e0 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20  TRACE(("JHDR %p 
bc3f0 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67  %lld %d\n", pPag
bc400 65 72 2c 20 6a 72 6e 6c 4f 66 66 2c 20 34 29 29  er, jrnlOff, 4))
bc410 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77  ;.        rc = w
bc420 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65  rite32bits(pPage
bc430 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c  r->jfd, jrnlOff,
bc440 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a   pPager->nRec);.
bc450 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29          if( rc )
bc460 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
bc470 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 30 3d    }.      if( 0=
bc480 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43  =(iDc&SQLITE_IOC
bc490 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29  AP_SEQUENTIAL) )
bc4a0 7b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54  {.        PAGERT
bc4b0 52 41 43 45 32 28 22 53 59 4e 43 20 6a 6f 75 72  RACE2("SYNC jour
bc4c0 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41  nal of %d\n", PA
bc4d0 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a  GERID(pPager));.
bc4e0 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28          IOTRACE(
bc4f0 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70  ("JSYNC %p\n", p
bc500 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20  Pager)).        
bc510 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79  rc = sqlite3OsSy
bc520 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  nc(pPager->jfd, 
bc530 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61  pPager->sync_fla
bc540 67 73 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28  gs| .          (
bc550 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61  pPager->sync_fla
bc560 67 73 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f  gs==SQLITE_SYNC_
bc570 46 55 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43  FULL?SQLITE_SYNC
bc580 5f 44 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20  _DATAONLY:0).   
bc590 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
bc5a0 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74 75  if( rc!=0 ) retu
bc5b0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
bc5c0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
bc5d0 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 31 3b  rnalStarted = 1;
bc5e0 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
bc5f0 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  r->needSync = 0;
bc600 0a 0a 20 20 20 20 2f 2a 20 45 72 61 73 65 20 74  ..    /* Erase t
bc610 68 65 20 6e 65 65 64 53 79 6e 63 20 66 6c 61 67  he needSync flag
bc620 20 66 72 6f 6d 20 65 76 65 72 79 20 70 61 67 65   from every page
bc630 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
bc640 28 70 50 67 3d 70 50 61 67 65 72 2d 3e 70 41 6c  (pPg=pPager->pAl
bc650 6c 3b 20 70 50 67 3b 20 70 50 67 3d 70 50 67 2d  l; pPg; pPg=pPg-
bc660 3e 70 4e 65 78 74 41 6c 6c 29 7b 0a 20 20 20 20  >pNextAll){.    
bc670 20 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20    pPg->needSync 
bc680 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c  = 0;.    }.    l
bc690 72 75 4c 69 73 74 53 65 74 46 69 72 73 74 53 79  ruListSetFirstSy
bc6a0 6e 63 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20  nced(pPager);.  
bc6b0 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
bc6c0 47 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 50 61  G.  /* If the Pa
bc6d0 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61  ger.needSync fla
bc6e0 67 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20  g is clear then 
bc6f0 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79  the PgHdr.needSy
bc700 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20 6d 75 73  nc.  ** flag mus
bc710 74 20 61 6c 73 6f 20 62 65 20 63 6c 65 61 72 20  t also be clear 
bc720 66 6f 72 20 61 6c 6c 20 70 61 67 65 73 2e 20 20  for all pages.  
bc730 56 65 72 69 66 79 20 74 68 61 74 20 74 68 69 73  Verify that this
bc740 0a 20 20 2a 2a 20 69 6e 76 61 72 69 61 6e 74 20  .  ** invariant 
bc750 69 73 20 74 72 75 65 2e 0a 20 20 2a 2f 0a 20 20  is true..  */.  
bc760 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 70 50  else{.    for(pP
bc770 67 3d 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20  g=pPager->pAll; 
bc780 70 50 67 3b 20 70 50 67 3d 70 50 67 2d 3e 70 4e  pPg; pPg=pPg->pN
bc790 65 78 74 41 6c 6c 29 7b 0a 20 20 20 20 20 20 61  extAll){.      a
bc7a0 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 65 65 64  ssert( pPg->need
bc7b0 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  Sync==0 );.    }
bc7c0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
bc7d0 67 65 72 2d 3e 6c 72 75 2e 70 46 69 72 73 74 53  ger->lru.pFirstS
bc7e0 79 6e 63 65 64 3d 3d 70 50 61 67 65 72 2d 3e 6c  ynced==pPager->l
bc7f0 72 75 2e 70 46 69 72 73 74 20 29 3b 0a 20 20 7d  ru.pFirst );.  }
bc800 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72  .#endif..  retur
bc810 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
bc820 65 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f  erge two lists o
bc830 66 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65  f pages connecte
bc840 64 20 62 79 20 70 44 69 72 74 79 20 61 6e 64 20  d by pDirty and 
bc850 69 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a  in pgno order..*
bc860 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69  * Do not both fi
bc870 78 69 6e 67 20 74 68 65 20 70 50 72 65 76 44 69  xing the pPrevDi
bc880 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f  rty pointers..*/
bc890 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 6d  .static PgHdr *m
bc8a0 65 72 67 65 5f 70 61 67 65 6c 69 73 74 28 50 67  erge_pagelist(Pg
bc8b0 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20 2a  Hdr *pA, PgHdr *
bc8c0 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 65 73  pB){.  PgHdr res
bc8d0 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 70  ult, *pTail;.  p
bc8e0 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 3b 0a  Tail = &result;.
bc8f0 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70    while( pA && p
bc900 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41 2d  B ){.    if( pA-
bc910 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f 20 29  >pgno<pB->pgno )
bc920 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70  {.      pTail->p
bc930 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 20 20  Dirty = pA;.    
bc940 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a 20 20    pTail = pA;.  
bc950 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 44 69      pA = pA->pDi
bc960 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rty;.    }else{.
bc970 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69        pTail->pDi
bc980 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20 20  rty = pB;.      
bc990 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20 20  pTail = pB;.    
bc9a0 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72 74    pB = pB->pDirt
bc9b0 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  y;.    }.  }.  i
bc9c0 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 54 61  f( pA ){.    pTa
bc9d0 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b  il->pDirty = pA;
bc9e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 20  .  }else if( pB 
bc9f0 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 44  ){.    pTail->pD
bca00 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d 65 6c  irty = pB;.  }el
bca10 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70  se{.    pTail->p
bca20 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d 0a 20  Dirty = 0;.  }. 
bca30 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 2e 70   return result.p
bca40 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Dirty;.}../*.** 
bca50 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 6f 66  Sort the list of
bca60 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64   pages in accend
bca70 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 67 6e  ing order by pgn
bca80 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a 2a 2a  o.  Pages are.**
bca90 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 44   connected by pD
bcaa0 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 20 20  irty pointers.  
bcab0 54 68 65 20 70 50 72 65 76 44 69 72 74 79 20 70  The pPrevDirty p
bcac0 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a 20 63  ointers are.** c
bcad0 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 69 73  orrupted by this
bcae0 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e   sort..*/.#defin
bcaf0 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f  e N_SORT_BUCKET_
bcb00 41 4c 4c 4f 43 20 32 35 0a 23 64 65 66 69 6e 65  ALLOC 25.#define
bcb10 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 20 20   N_SORT_BUCKET  
bcb20 20 20 20 20 20 32 35 0a 23 69 66 64 65 66 20 53       25.#ifdef S
bcb30 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e 74  QLITE_TEST.  int
bcb40 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e   sqlite3_pager_n
bcb50 5f 73 6f 72 74 5f 62 75 63 6b 65 74 20 3d 20 30  _sort_bucket = 0
bcb60 3b 0a 20 20 23 75 6e 64 65 66 20 4e 5f 53 4f 52  ;.  #undef N_SOR
bcb70 54 5f 42 55 43 4b 45 54 0a 20 20 23 64 65 66 69  T_BUCKET.  #defi
bcb80 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  ne N_SORT_BUCKET
bcb90 20 5c 0a 20 20 20 28 73 71 6c 69 74 65 33 5f 70   \.   (sqlite3_p
bcba0 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b  ager_n_sort_buck
bcbb0 65 74 3f 73 71 6c 69 74 65 33 5f 70 61 67 65 72  et?sqlite3_pager
bcbc0 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 3a 4e  _n_sort_bucket:N
bcbd0 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41 4c 4c  _SORT_BUCKET_ALL
bcbe0 4f 43 29 0a 23 65 6e 64 69 66 0a 73 74 61 74 69  OC).#endif.stati
bcbf0 63 20 50 67 48 64 72 20 2a 73 6f 72 74 5f 70 61  c PgHdr *sort_pa
bcc00 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70 49  gelist(PgHdr *pI
bcc10 6e 29 7b 0a 20 20 50 67 48 64 72 20 2a 61 5b 4e  n){.  PgHdr *a[N
bcc20 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41 4c 4c  _SORT_BUCKET_ALL
bcc30 4f 43 5d 2c 20 2a 70 3b 0a 20 20 69 6e 74 20 69  OC], *p;.  int i
bcc40 3b 0a 20 20 6d 65 6d 73 65 74 28 61 2c 20 30 2c  ;.  memset(a, 0,
bcc50 20 73 69 7a 65 6f 66 28 61 29 29 3b 0a 20 20 77   sizeof(a));.  w
bcc60 68 69 6c 65 28 20 70 49 6e 20 29 7b 0a 20 20 20  hile( pIn ){.   
bcc70 20 70 20 3d 20 70 49 6e 3b 0a 20 20 20 20 70 49   p = pIn;.    pI
bcc80 6e 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20  n = p->pDirty;. 
bcc90 20 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 30     p->pDirty = 0
bcca0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
bccb0 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d 31  <N_SORT_BUCKET-1
bccc0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
bccd0 28 20 61 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20  ( a[i]==0 ){.   
bcce0 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 3b 0a 20       a[i] = p;. 
bccf0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
bcd00 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
bcd10 20 20 20 70 20 3d 20 6d 65 72 67 65 5f 70 61 67     p = merge_pag
bcd20 65 6c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a  elist(a[i], p);.
bcd30 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30          a[i] = 0
bcd40 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
bcd50 20 20 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52      if( i==N_SOR
bcd60 54 5f 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20  T_BUCKET-1 ){.  
bcd70 20 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a      /* Coverage:
bcd80 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68   To get here, th
bcd90 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32  ere need to be 2
bcda0 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29  ^(N_SORT_BUCKET)
bcdb0 20 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65   .      ** eleme
bcdc0 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74  nts in the input
bcdd0 20 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 70   list. This is p
bcde0 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70  ossible, but imp
bcdf0 72 61 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20  ractical..      
bce00 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 69 73 20  ** Testing this 
bce10 6c 69 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e  line is the poin
bce20 74 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69  t of global vari
bce30 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71  able.      ** sq
bce40 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f  lite3_pager_n_so
bce50 72 74 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20  rt_bucket..     
bce60 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d   */.      a[i] =
bce70 20 6d 65 72 67 65 5f 70 61 67 65 6c 69 73 74 28   merge_pagelist(
bce80 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20 7d 0a  a[i], p);.    }.
bce90 20 20 7d 0a 20 20 70 20 3d 20 61 5b 30 5d 3b 0a    }.  p = a[0];.
bcea0 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e 5f 53    for(i=1; i<N_S
bceb0 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69 2b 2b 29  ORT_BUCKET; i++)
bcec0 7b 0a 20 20 20 20 70 20 3d 20 6d 65 72 67 65 5f  {.    p = merge_
bced0 70 61 67 65 6c 69 73 74 28 70 2c 20 61 5b 69 5d  pagelist(p, a[i]
bcee0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
bcef0 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  p;.}../*.** Give
bcf00 6e 20 61 20 6c 69 73 74 20 6f 66 20 70 61 67 65  n a list of page
bcf10 73 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  s (connected by 
bcf20 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79  the PgHdr.pDirty
bcf30 20 70 6f 69 6e 74 65 72 29 20 77 72 69 74 65 0a   pointer) write.
bcf40 2a 2a 20 65 76 65 72 79 20 6f 6e 65 20 6f 66 20  ** every one of 
bcf50 74 68 6f 73 65 20 70 61 67 65 73 20 6f 75 74 20  those pages out 
bcf60 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
bcf70 66 69 6c 65 20 61 6e 64 20 6d 61 72 6b 20 74 68  file and mark th
bcf80 65 6d 20 61 6c 6c 0a 2a 2a 20 61 73 20 63 6c 65  em all.** as cle
bcf90 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  an..*/.static in
bcfa0 74 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61  t pager_write_pa
bcfb0 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70 4c  gelist(PgHdr *pL
bcfc0 69 73 74 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  ist){.  Pager *p
bcfd0 50 61 67 65 72 3b 0a 20 20 50 67 48 64 72 20 2a  Pager;.  PgHdr *
bcfe0 70 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  p;.  int rc;..  
bcff0 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
bd000 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
bd010 0a 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73  .  pPager = pLis
bd020 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a  t->pPager;..  /*
bd030 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
bd040 68 65 72 65 20 6d 61 79 20 62 65 20 65 69 74 68  here may be eith
bd050 65 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  er a RESERVED or
bd060 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
bd070 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  on the.  ** data
bd080 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68  base file. If th
bd090 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
bd0a0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
bd0b0 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  , the following.
bd0c0 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71    ** calls to sq
bd0d0 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 61 72  lite3OsLock() ar
bd0e0 65 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2a 0a 20  e no-ops..  **. 
bd0f0 20 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c   ** Moving the l
bd100 6f 63 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45  ock from RESERVE
bd110 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61  D to EXCLUSIVE a
bd120 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73  ctually involves
bd130 20 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f   going.  ** thro
bd140 75 67 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69  ugh an intermedi
bd150 61 74 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e  ate state PENDIN
bd160 47 2e 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c  G.   A PENDING l
bd170 6f 63 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77  ock prevents new
bd180 0a 20 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72  .  ** readers fr
bd190 6f 6d 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20  om attaching to 
bd1a0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74  the database but
bd1b0 20 69 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74   is unsufficient
bd1c0 20 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20   for us to.  ** 
bd1d0 77 72 69 74 65 2e 20 20 54 68 65 20 69 64 65 61  write.  The idea
bd1e0 20 6f 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f   of a PENDING lo
bd1f0 63 6b 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74  ck is to prevent
bd200 20 6e 65 77 20 72 65 61 64 65 72 73 20 66 72 6f   new readers fro
bd210 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e  m.  ** coming in
bd220 20 77 68 69 6c 65 20 77 65 20 77 61 69 74 20 66   while we wait f
bd230 6f 72 20 65 78 69 73 74 69 6e 67 20 72 65 61 64  or existing read
bd240 65 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20  ers to clear..  
bd250 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68  **.  ** While th
bd260 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68  e pager is in th
bd270 65 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65  e RESERVED state
bd280 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  , the original d
bd290 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a  atabase file.  *
bd2a0 2a 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61  * is unchanged a
bd2b0 6e 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61  nd we can rollba
bd2c0 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e  ck without havin
bd2d0 67 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68  g to playback th
bd2e0 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69  e.  ** journal i
bd2f0 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  nto the original
bd300 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
bd310 20 4f 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74   Once we transit
bd320 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c  ion to.  ** EXCL
bd330 55 53 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20  USIVE, it means 
bd340 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
bd350 65 20 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67  e has been chang
bd360 65 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62  ed and any rollb
bd370 61 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65  ack.  ** will re
bd380 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20  quire a journal 
bd390 70 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20  playback..  */. 
bd3a0 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74   rc = pager_wait
bd3b0 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c  _on_lock(pPager,
bd3c0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29   EXCLUSIVE_LOCK)
bd3d0 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
bd3e0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
bd3f0 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 70  urn rc;.  }..  p
bd400 4c 69 73 74 20 3d 20 73 6f 72 74 5f 70 61 67 65  List = sort_page
bd410 6c 69 73 74 28 70 4c 69 73 74 29 3b 0a 20 20 66  list(pList);.  f
bd420 6f 72 28 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70  or(p=pList; p; p
bd430 3d 70 2d 3e 70 44 69 72 74 79 29 7b 0a 20 20 20  =p->pDirty){.   
bd440 20 61 73 73 65 72 74 28 20 70 2d 3e 64 69 72 74   assert( p->dirt
bd450 79 20 29 3b 0a 20 20 20 20 70 2d 3e 64 69 72 74  y );.    p->dirt
bd460 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 77 68 69  y = 0;.  }.  whi
bd470 6c 65 28 20 70 4c 69 73 74 20 29 7b 0a 0a 20 20  le( pList ){..  
bd480 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65    /* If the file
bd490 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
bd4a0 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69  n opened, open i
bd4b0 74 20 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20 69 66  t now. */.    if
bd4c0 28 20 21 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70  ( !pPager->fd->p
bd4d0 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20  Methods ){.     
bd4e0 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e   assert(pPager->
bd4f0 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 20 20 20  tempFile);.     
bd500 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
bd510 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65  erOpentemp(pPage
bd520 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d  r->pVfs, pPager-
bd530 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69  >fd, pPager->zFi
bd540 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  lename,.        
bd550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd560 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
bd570 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 20 20 20  vfsFlags);.     
bd580 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
bd590 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   rc;.    }..    
bd5a0 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
bd5b0 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74  dirty pages in t
bd5c0 68 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69  he page cache wi
bd5d0 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20  th page numbers 
bd5e0 67 72 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74  greater.    ** t
bd5f0 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65  han Pager.dbSize
bd600 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c  , this means sql
bd610 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
bd620 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 20 74  e() was called t
bd630 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74 68  o.    ** make th
bd640 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20 28  e file smaller (
bd650 70 72 65 73 75 6d 61 62 6c 79 20 62 79 20 61 75  presumably by au
bd660 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64 65 29 2e  to-vacuum code).
bd670 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20 20   Do not write.  
bd680 20 20 2a 2a 20 61 6e 79 20 73 75 63 68 20 70 61    ** any such pa
bd690 67 65 73 20 74 6f 20 74 68 65 20 66 69 6c 65 2e  ges to the file.
bd6a0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
bd6b0 70 4c 69 73 74 2d 3e 70 67 6e 6f 3c 3d 70 50 61  pList->pgno<=pPa
bd6c0 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 7b 0a 20  ger->dbSize ){. 
bd6d0 20 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20       i64 offset 
bd6e0 3d 20 28 70 4c 69 73 74 2d 3e 70 67 6e 6f 2d 31  = (pList->pgno-1
bd6f0 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70  )*(i64)pPager->p
bd700 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 63  ageSize;.      c
bd710 68 61 72 20 2a 70 44 61 74 61 20 3d 20 43 4f 44  har *pData = COD
bd720 45 43 32 28 70 50 61 67 65 72 2c 20 50 47 48 44  EC2(pPager, PGHD
bd730 52 5f 54 4f 5f 44 41 54 41 28 70 4c 69 73 74 29  R_TO_DATA(pList)
bd740 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 2c 20 36  , pList->pgno, 6
bd750 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52  );.      PAGERTR
bd760 41 43 45 34 28 22 53 54 4f 52 45 20 25 64 20 70  ACE4("STORE %d p
bd770 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78  age %d hash(%08x
bd780 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  )\n",.          
bd790 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44           PAGERID
bd7a0 28 70 50 61 67 65 72 29 2c 20 70 4c 69 73 74 2d  (pPager), pList-
bd7b0 3e 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67  >pgno, pager_pag
bd7c0 65 68 61 73 68 28 70 4c 69 73 74 29 29 3b 0a 20  ehash(pList));. 
bd7d0 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 50       IOTRACE(("P
bd7e0 47 4f 55 54 20 25 70 20 25 64 5c 6e 22 2c 20 70  GOUT %p %d\n", p
bd7f0 50 61 67 65 72 2c 20 70 4c 69 73 74 2d 3e 70 67  Pager, pList->pg
bd800 6e 6f 29 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  no));.      rc =
bd810 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
bd820 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61 74  pPager->fd, pDat
bd830 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
bd840 69 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20  ize, offset);.  
bd850 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73      PAGER_INCR(s
bd860 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69  qlite3_pager_wri
bd870 74 65 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 20  tedb_count);.   
bd880 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50     PAGER_INCR(pP
bd890 61 67 65 72 2d 3e 6e 57 72 69 74 65 29 3b 0a 20  ager->nWrite);. 
bd8a0 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e       if( pList->
bd8b0 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20  pgno==1 ){.     
bd8c0 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65     memcpy(&pPage
bd8d0 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26  r->dbFileVers, &
bd8e0 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a 65 6f  pData[24], sizeo
bd8f0 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
bd900 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a  Vers));.      }.
bd910 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44      }.#ifndef ND
bd920 45 42 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20  EBUG.    else{. 
bd930 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 33       PAGERTRACE3
bd940 28 22 4e 4f 53 54 4f 52 45 20 25 64 20 70 61 67  ("NOSTORE %d pag
bd950 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  e %d\n", PAGERID
bd960 28 70 50 61 67 65 72 29 2c 20 70 4c 69 73 74 2d  (pPager), pList-
bd970 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 7d 0a 23 65  >pgno);.    }.#e
bd980 6e 64 69 66 0a 20 20 20 20 69 66 28 20 72 63 20  ndif.    if( rc 
bd990 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 69 66  ) return rc;.#if
bd9a0 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
bd9b0 5f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 74  _PAGES.    pList
bd9c0 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67  ->pageHash = pag
bd9d0 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73  er_pagehash(pLis
bd9e0 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70  t);.#endif.    p
bd9f0 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44  List = pList->pD
bda00 69 72 74 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75  irty;.  }.  retu
bda10 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
bda20 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 65 63 74 20 65  ./*.** Collect e
bda30 76 65 72 79 20 64 69 72 74 79 20 70 61 67 65 20  very dirty page 
bda40 69 6e 74 6f 20 61 20 64 69 72 74 79 20 6c 69 73  into a dirty lis
bda50 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  t and.** return 
bda60 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
bda70 20 68 65 61 64 20 6f 66 20 74 68 61 74 20 6c 69   head of that li
bda80 73 74 2e 20 20 41 6c 6c 20 70 61 67 65 73 20 61  st.  All pages a
bda90 72 65 0a 2a 2a 20 63 6f 6c 6c 65 63 74 65 64 20  re.** collected 
bdaa0 65 76 65 6e 20 69 66 20 74 68 65 79 20 61 72 65  even if they are
bdab0 20 73 74 69 6c 6c 20 69 6e 20 75 73 65 2e 0a 2a   still in use..*
bdac0 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a  /.static PgHdr *
bdad0 70 61 67 65 72 5f 67 65 74 5f 61 6c 6c 5f 64 69  pager_get_all_di
bdae0 72 74 79 5f 70 61 67 65 73 28 50 61 67 65 72 20  rty_pages(Pager 
bdaf0 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
bdb00 72 6e 20 70 50 61 67 65 72 2d 3e 70 44 69 72 74  rn pPager->pDirt
bdb10 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  y;.}../*.** Retu
bdb20 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 72 65  rn TRUE if there
bdb30 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61   is a hot journa
bdb40 6c 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70  l on the given p
bdb50 61 67 65 72 2e 0a 2a 2a 20 41 20 68 6f 74 20 6a  ager..** A hot j
bdb60 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68  ournal is one th
bdb70 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70  at needs to be p
bdb80 6c 61 79 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a  layed back..**.*
bdb90 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74  * If the current
bdba0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
bdbb0 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 20  abase file is 0 
bdbc0 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  but a journal fi
bdbd0 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68  le.** exists, th
bdbe0 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 61  at is probably a
bdbf0 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65  n old journal le
bdc00 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 70  ft over from a p
bdc10 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 65  rior.** database
bdc20 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e   with the same n
bdc30 61 6d 65 2e 20 20 4a 75 73 74 20 64 65 6c 65 74  ame.  Just delet
bdc40 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  e the journal..*
bdc50 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73  /.static int has
bdc60 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72  HotJournal(Pager
bdc70 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 71 6c   *pPager){.  sql
bdc80 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
bdc90 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20   pPager->pVfs;. 
bdca0 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 75 73   if( !pPager->us
bdcb0 65 4a 6f 75 72 6e 61 6c 20 29 20 72 65 74 75 72  eJournal ) retur
bdcc0 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  n 0;.  if( !sqli
bdcd0 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73  te3OsAccess(pVfs
bdce0 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  , pPager->zJourn
bdcf0 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53  al, SQLITE_ACCES
bdd00 53 5f 45 58 49 53 54 53 29 20 29 7b 0a 20 20 20  S_EXISTS) ){.   
bdd10 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
bdd20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73 43 68   if( sqlite3OsCh
bdd30 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
bdd40 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20  pPager->fd) ){. 
bdd50 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
bdd60 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61  .  if( sqlite3Pa
bdd70 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61  gerPagecount(pPa
bdd80 67 65 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  ger)==0 ){.    s
bdd90 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
bdda0 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  Vfs, pPager->zJo
bddb0 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 72  urnal, 0);.    r
bddc0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
bddd0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
bdde0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79    }.}../*.** Try
bddf0 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20   to find a page 
bde00 69 6e 20 74 68 65 20 63 61 63 68 65 20 74 68 61  in the cache tha
bde10 74 20 63 61 6e 20 62 65 20 72 65 63 79 63 6c 65  t can be recycle
bde20 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  d. .**.** This r
bde30 6f 75 74 69 6e 65 20 6d 61 79 20 72 65 74 75 72  outine may retur
bde40 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20  n SQLITE_IOERR, 
bde50 53 51 4c 49 54 45 5f 46 55 4c 4c 20 6f 72 20 53  SQLITE_FULL or S
bde60 51 4c 49 54 45 5f 4f 4b 2e 20 49 74 20 0a 2a 2a  QLITE_OK. It .**
bde70 20 64 6f 65 73 20 6e 6f 74 20 73 65 74 20 74 68   does not set th
bde80 65 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  e pPager->errCod
bde90 65 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 73  e variable..*/.s
bdea0 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
bdeb0 72 65 63 79 63 6c 65 28 50 61 67 65 72 20 2a 70  recycle(Pager *p
bdec0 50 61 67 65 72 2c 20 50 67 48 64 72 20 2a 2a 70  Pager, PgHdr **p
bded0 70 50 67 29 7b 0a 20 20 50 67 48 64 72 20 2a 70  pPg){.  PgHdr *p
bdee0 50 67 3b 0a 20 20 2a 70 70 50 67 20 3d 20 30 3b  Pg;.  *ppPg = 0;
bdef0 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c  ..  /* It is ill
bdf00 65 67 61 6c 20 74 6f 20 63 61 6c 6c 20 74 68 69  egal to call thi
bdf10 73 20 66 75 6e 63 74 69 6f 6e 20 75 6e 6c 65 73  s function unles
bdf20 73 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65  s the pager obje
bdf30 63 74 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 64 20  ct.  ** pointed 
bdf40 74 6f 20 62 79 20 70 50 61 67 65 72 20 68 61 73  to by pPager has
bdf50 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 72   at least one fr
bdf60 65 65 20 70 61 67 65 20 28 70 61 67 65 20 77 69  ee page (page wi
bdf70 74 68 20 6e 52 65 66 3d 3d 30 29 2e 0a 20 20 2a  th nRef==0)..  *
bdf80 2f 20 0a 20 20 61 73 73 65 72 74 28 21 4d 45 4d  / .  assert(!MEM
bdf90 44 42 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50  DB);.  assert(pP
bdfa0 61 67 65 72 2d 3e 6c 72 75 2e 70 46 69 72 73 74  ager->lru.pFirst
bdfb0 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 61 20  );..  /* Find a 
bdfc0 70 61 67 65 20 74 6f 20 72 65 63 79 63 6c 65 2e  page to recycle.
bdfd0 20 20 54 72 79 20 74 6f 20 6c 6f 63 61 74 65 20    Try to locate 
bdfe0 61 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73  a page that does
bdff0 20 6e 6f 74 0a 20 20 2a 2a 20 72 65 71 75 69 72   not.  ** requir
be000 65 20 75 73 20 74 6f 20 64 6f 20 61 6e 20 66 73  e us to do an fs
be010 79 6e 63 28 29 20 6f 6e 20 74 68 65 20 6a 6f 75  ync() on the jou
be020 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 70 50 67  rnal..  */.  pPg
be030 20 3d 20 70 50 61 67 65 72 2d 3e 6c 72 75 2e 70   = pPager->lru.p
be040 46 69 72 73 74 53 79 6e 63 65 64 3b 0a 0a 20 20  FirstSynced;..  
be050 2f 2a 20 49 66 20 77 65 20 63 6f 75 6c 64 20 6e  /* If we could n
be060 6f 74 20 66 69 6e 64 20 61 20 70 61 67 65 20 74  ot find a page t
be070 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 71  hat does not req
be080 75 69 72 65 20 61 6e 20 66 73 79 6e 63 28 29 0a  uire an fsync().
be090 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6a 6f 75 72    ** on the jour
be0a0 6e 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 66 73  nal file then fs
be0b0 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ync the journal 
be0c0 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 61  file.  This is a
be0d0 0a 20 20 2a 2a 20 76 65 72 79 20 73 6c 6f 77 20  .  ** very slow 
be0e0 6f 70 65 72 61 74 69 6f 6e 2c 20 73 6f 20 77 65  operation, so we
be0f0 20 77 6f 72 6b 20 68 61 72 64 20 74 6f 20 61 76   work hard to av
be100 6f 69 64 20 69 74 2e 20 20 42 75 74 20 73 6f 6d  oid it.  But som
be110 65 74 69 6d 65 73 0a 20 20 2a 2a 20 69 74 20 63  etimes.  ** it c
be120 61 6e 27 74 20 62 65 20 68 65 6c 70 65 64 2e 0a  an't be helped..
be130 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 3d 3d    */.  if( pPg==
be140 30 20 26 26 20 70 50 61 67 65 72 2d 3e 6c 72 75  0 && pPager->lru
be150 2e 70 46 69 72 73 74 29 7b 0a 20 20 20 20 69 6e  .pFirst){.    in
be160 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f  t iDc = sqlite3O
be170 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
be180 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
be190 64 29 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  d);.    int rc =
be1a0 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61   syncJournal(pPa
be1b0 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ger);.    if( rc
be1c0 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  !=0 ){.      ret
be1d0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
be1e0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75    if( pPager->fu
be1f0 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44  llSync && 0==(iD
be200 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
be210 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20  AFE_APPEND) ){. 
be220 20 20 20 20 20 2f 2a 20 49 66 20 69 6e 20 66 75       /* If in fu
be230 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 77 72  ll-sync mode, wr
be240 69 74 65 20 61 20 6e 65 77 20 6a 6f 75 72 6e 61  ite a new journa
be250 6c 20 68 65 61 64 65 72 20 69 6e 74 6f 20 74 68  l header into th
be260 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e  e.      ** journ
be270 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73  al file. This is
be280 20 64 6f 6e 65 20 74 6f 20 61 76 6f 69 64 20 65   done to avoid e
be290 76 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 20  ver modifying a 
be2a0 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a  journal.      **
be2b0 20 68 65 61 64 65 72 20 74 68 61 74 20 69 73 20   header that is 
be2c0 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20  involved in the 
be2d0 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 70 61 67 65  rollback of page
be2e0 73 20 74 68 61 74 20 68 61 76 65 0a 20 20 20 20  s that have.    
be2f0 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65    ** already bee
be300 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  n written to the
be310 20 64 61 74 61 62 61 73 65 20 28 69 6e 20 63 61   database (in ca
be320 73 65 20 74 68 65 20 68 65 61 64 65 72 20 69 73  se the header is
be330 0a 20 20 20 20 20 20 2a 2a 20 74 72 61 73 68 65  .      ** trashe
be340 64 20 77 68 65 6e 20 74 68 65 20 6e 52 65 63 20  d when the nRec 
be350 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64  field is updated
be360 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
be370 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
be380 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   0;.      assert
be390 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
be3a0 6c 4f 66 66 20 3e 20 30 20 29 3b 0a 20 20 20 20  lOff > 0 );.    
be3b0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
be3c0 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29  ->doNotSync==0 )
be3d0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69  ;.      rc = wri
be3e0 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  teJournalHdr(pPa
be3f0 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ger);.      if( 
be400 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rc!=0 ){.       
be410 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
be420 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 50    }.    }.    pP
be430 67 20 3d 20 70 50 61 67 65 72 2d 3e 6c 72 75 2e  g = pPager->lru.
be440 70 46 69 72 73 74 3b 0a 20 20 7d 0a 0a 20 20 61  pFirst;.  }..  a
be450 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66  ssert( pPg->nRef
be460 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 72 69  ==0 );..  /* Wri
be470 74 65 20 74 68 65 20 70 61 67 65 20 74 6f 20 74  te the page to t
be480 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
be490 20 69 66 20 69 74 20 69 73 20 64 69 72 74 79 2e   if it is dirty.
be4a0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d  .  */.  if( pPg-
be4b0 3e 64 69 72 74 79 20 29 7b 0a 20 20 20 20 69 6e  >dirty ){.    in
be4c0 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74  t rc;.    assert
be4d0 28 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 3d  ( pPg->needSync=
be4e0 3d 30 20 29 3b 0a 20 20 20 20 6d 61 6b 65 43 6c  =0 );.    makeCl
be4f0 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 70 50  ean(pPg);.    pP
be500 67 2d 3e 64 69 72 74 79 20 3d 20 31 3b 0a 20 20  g->dirty = 1;.  
be510 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d 20    pPg->pDirty = 
be520 30 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  0;.    rc = page
be530 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74  r_write_pagelist
be540 28 20 70 50 67 20 29 3b 0a 20 20 20 20 70 50 67  ( pPg );.    pPg
be550 2d 3e 64 69 72 74 79 20 3d 20 30 3b 0a 20 20 20  ->dirty = 0;.   
be560 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
be570 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
be580 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
be590 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  .  assert( pPg->
be5a0 64 69 72 74 79 3d 3d 30 20 29 3b 0a 0a 20 20 2f  dirty==0 );..  /
be5b0 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 77 65  * If the page we
be5c0 20 61 72 65 20 72 65 63 79 63 6c 69 6e 67 20 69   are recycling i
be5d0 73 20 6d 61 72 6b 65 64 20 61 73 20 61 6c 77 61  s marked as alwa
be5e0 79 73 52 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e  ysRollback, then
be5f0 0a 20 20 2a 2a 20 73 65 74 20 74 68 65 20 67 6c  .  ** set the gl
be600 6f 62 61 6c 20 61 6c 77 61 79 73 52 6f 6c 6c 62  obal alwaysRollb
be610 61 63 6b 20 66 6c 61 67 2c 20 74 68 75 73 20 64  ack flag, thus d
be620 69 73 61 62 6c 69 6e 67 20 74 68 65 0a 20 20 2a  isabling the.  *
be630 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  * sqlite3PagerDo
be640 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 6f 70 74  ntRollback() opt
be650 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 74 68  imization for th
be660 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 20 74  e rest of this t
be670 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a  ransaction..  **
be680 20 49 74 20 69 73 20 6e 65 63 65 73 73 61 72 79   It is necessary
be690 20 74 6f 20 64 6f 20 74 68 69 73 20 62 65 63 61   to do this beca
be6a0 75 73 65 20 74 68 65 20 70 61 67 65 20 6d 61 72  use the page mar
be6b0 6b 65 64 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61  ked alwaysRollba
be6c0 63 6b 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62 65  ck.  ** might be
be6d0 20 72 65 6c 6f 61 64 65 64 20 61 74 20 61 20 6c   reloaded at a l
be6e0 61 74 65 72 20 74 69 6d 65 20 62 75 74 20 61 74  ater time but at
be6f0 20 74 68 61 74 20 70 6f 69 6e 74 20 77 65 20 77   that point we w
be700 6f 6e 27 74 20 72 65 6d 65 6d 62 65 72 0a 20 20  on't remember.  
be710 2a 2a 20 74 68 61 74 20 69 73 20 77 61 73 20 6d  ** that is was m
be720 61 72 6b 65 64 20 61 6c 77 61 79 73 52 6f 6c 6c  arked alwaysRoll
be730 62 61 63 6b 2e 20 20 54 68 69 73 20 6d 65 61 6e  back.  This mean
be740 73 20 74 68 61 74 20 61 6c 6c 20 70 61 67 65 73  s that all pages
be750 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 6d 61   must.  ** be ma
be760 72 6b 65 64 20 61 73 20 61 6c 77 61 79 73 52 6f  rked as alwaysRo
be770 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 68 65 72 65  llback from here
be780 20 6f 6e 20 6f 75 74 2e 0a 20 20 2a 2f 0a 20 20   on out..  */.  
be790 69 66 28 20 70 50 67 2d 3e 61 6c 77 61 79 73 52  if( pPg->alwaysR
be7a0 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 49  ollback ){.    I
be7b0 4f 54 52 41 43 45 28 28 22 41 4c 57 41 59 53 5f  OTRACE(("ALWAYS_
be7c0 52 4f 4c 4c 42 41 43 4b 20 25 70 5c 6e 22 2c 20  ROLLBACK %p\n", 
be7d0 70 50 61 67 65 72 29 29 0a 20 20 20 20 70 50 61  pPager)).    pPa
be7e0 67 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c 62  ger->alwaysRollb
be7f0 61 63 6b 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  ack = 1;.  }..  
be800 2f 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 6f 6c  /* Unlink the ol
be810 64 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  d page from the 
be820 66 72 65 65 20 6c 69 73 74 20 61 6e 64 20 74 68  free list and th
be830 65 20 68 61 73 68 20 74 61 62 6c 65 0a 20 20 2a  e hash table.  *
be840 2f 0a 20 20 75 6e 6c 69 6e 6b 50 61 67 65 28 70  /.  unlinkPage(p
be850 50 67 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Pg);.  assert( p
be860 50 67 2d 3e 70 67 6e 6f 3d 3d 30 20 29 3b 0a 0a  Pg->pgno==0 );..
be870 20 20 2a 70 70 50 67 20 3d 20 70 50 67 3b 0a 20    *ppPg = pPg;. 
be880 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
be890 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  K;.}..#ifdef SQL
be8a0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
be8b0 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a  Y_MANAGEMENT./*.
be8c0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
be8d0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66 72   is called to fr
be8e0 65 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20 64  ee superfluous d
be8f0 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
be900 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68  ated memory.** h
be910 65 6c 64 20 62 79 20 74 68 65 20 70 61 67 65 72  eld by the pager
be920 20 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20   system. Memory 
be930 69 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53 51  in use by any SQ
be940 4c 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f 63  Lite pager alloc
be950 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63  ated.** by the c
be960 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d 61  urrent thread ma
be970 79 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72 65  y be sqlite3_fre
be980 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65  e()ed..**.** nRe
be990 71 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  q is the number 
be9a0 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  of bytes of memo
be9b0 72 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e 63  ry required. Onc
be9c0 65 20 74 68 69 73 20 6d 75 63 68 20 68 61 73 0a  e this much has.
be9d0 2a 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64  ** been released
be9e0 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72  , the function r
be9f0 65 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 75  eturns. The retu
bea00 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
bea10 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a  total number .**
bea20 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d   of bytes of mem
bea30 6f 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f  ory released..*/
bea40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
bea50 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
bea60 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e  ReleaseMemory(in
bea70 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e  t nReq){.  int n
bea80 52 65 6c 65 61 73 65 64 20 3d 20 30 3b 20 20 20  Released = 0;   
bea90 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
beaa0 6f 66 20 6d 65 6d 6f 72 79 20 72 65 6c 65 61 73  of memory releas
beab0 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 73  ed so far */.  s
beac0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
bead0 74 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  tex;       /* Th
beae0 65 20 4d 45 4d 32 20 6d 75 74 65 78 20 2a 2f 0a  e MEM2 mutex */.
beaf0 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
beb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
beb10 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
beb20 72 20 70 61 67 65 72 73 20 2a 2f 0a 20 20 69 6e  r pagers */.  in
beb30 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
beb40 3b 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  ;..  /* Acquire 
beb50 74 68 65 20 6d 65 6d 6f 72 79 2d 6d 61 6e 61 67  the memory-manag
beb60 65 6d 65 6e 74 20 6d 75 74 65 78 0a 20 20 2a 2f  ement mutex.  */
beb70 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  .  mutex = sqlit
beb80 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
beb90 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
beba0 49 43 5f 4d 45 4d 32 29 3b 0a 20 20 73 71 6c 69  IC_MEM2);.  sqli
bebb0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
bebc0 6d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 53 69  mutex);..  /* Si
bebd0 67 6e 61 6c 20 61 6c 6c 20 64 61 74 61 62 61 73  gnal all databas
bebe0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68  e connections th
bebf0 61 74 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65  at memory manage
bec00 6d 65 6e 74 20 77 61 6e 74 73 0a 20 20 2a 2a 20  ment wants.  ** 
bec10 74 6f 20 68 61 76 65 20 61 63 63 65 73 73 20 74  to have access t
bec20 6f 20 74 68 65 20 70 61 67 65 72 73 2e 0a 20 20  o the pagers..  
bec30 2a 2f 0a 20 20 66 6f 72 28 70 50 61 67 65 72 3d  */.  for(pPager=
bec40 73 71 6c 69 74 65 33 50 61 67 65 72 4c 69 73 74  sqlite3PagerList
bec50 3b 20 70 50 61 67 65 72 3b 20 70 50 61 67 65 72  ; pPager; pPager
bec60 3d 70 50 61 67 65 72 2d 3e 70 4e 65 78 74 29 7b  =pPager->pNext){
bec70 0a 20 20 20 20 20 70 50 61 67 65 72 2d 3e 69 49  .     pPager->iI
bec80 6e 55 73 65 4d 4d 20 3d 20 31 3b 0a 20 20 7d 0a  nUseMM = 1;.  }.
bec90 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
beca0 4c 49 54 45 5f 4f 4b 20 26 26 20 28 6e 52 65 71  LITE_OK && (nReq
becb0 3c 30 20 7c 7c 20 6e 52 65 6c 65 61 73 65 64 3c  <0 || nReleased<
becc0 6e 52 65 71 29 20 29 7b 0a 20 20 20 20 50 67 48  nReq) ){.    PgH
becd0 64 72 20 2a 70 50 67 3b 0a 20 20 20 20 50 67 48  dr *pPg;.    PgH
bece0 64 72 20 2a 70 52 65 63 79 63 6c 65 64 3b 0a 20  dr *pRecycled;. 
becf0 0a 20 20 20 20 2f 2a 20 54 72 79 20 74 6f 20 66  .    /* Try to f
bed00 69 6e 64 20 61 20 70 61 67 65 20 74 6f 20 72 65  ind a page to re
bed10 63 79 63 6c 65 20 74 68 61 74 20 64 6f 65 73 20  cycle that does 
bed20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 73 79  not require a sy
bed30 6e 63 28 29 2e 20 49 66 0a 20 20 20 20 2a 2a 20  nc(). If.    ** 
bed40 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73  this is not poss
bed50 69 62 6c 65 2c 20 66 69 6e 64 20 6f 6e 65 20 74  ible, find one t
bed60 68 61 74 20 64 6f 65 73 20 72 65 71 75 69 72 65  hat does require
bed70 20 61 20 73 79 6e 63 28 29 2e 0a 20 20 20 20 2a   a sync()..    *
bed80 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  /.    sqlite3_mu
bed90 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
beda0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51  3_mutex_alloc(SQ
bedb0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
bedc0 43 5f 4c 52 55 29 29 3b 0a 20 20 20 20 70 50 67  C_LRU));.    pPg
bedd0 20 3d 20 73 71 6c 69 74 65 33 4c 72 75 50 61 67   = sqlite3LruPag
bede0 65 4c 69 73 74 2e 70 46 69 72 73 74 53 79 6e 63  eList.pFirstSync
bedf0 65 64 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  ed;.    while( p
bee00 50 67 20 26 26 20 28 70 50 67 2d 3e 6e 65 65 64  Pg && (pPg->need
bee10 53 79 6e 63 20 7c 7c 20 70 50 67 2d 3e 70 50 61  Sync || pPg->pPa
bee20 67 65 72 2d 3e 69 49 6e 55 73 65 44 42 29 20 29  ger->iInUseDB) )
bee30 7b 0a 20 20 20 20 20 20 70 50 67 20 3d 20 70 50  {.      pPg = pP
bee40 67 2d 3e 67 66 72 65 65 2e 70 4e 65 78 74 3b 0a  g->gfree.pNext;.
bee50 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70      }.    if( !p
bee60 50 67 20 29 7b 0a 20 20 20 20 20 20 70 50 67 20  Pg ){.      pPg 
bee70 3d 20 73 71 6c 69 74 65 33 4c 72 75 50 61 67 65  = sqlite3LruPage
bee80 4c 69 73 74 2e 70 46 69 72 73 74 3b 0a 20 20 20  List.pFirst;.   
bee90 20 20 20 77 68 69 6c 65 28 20 70 50 67 20 26 26     while( pPg &&
beea0 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 69 49   pPg->pPager->iI
beeb0 6e 55 73 65 44 42 20 29 7b 0a 20 20 20 20 20 20  nUseDB ){.      
beec0 20 20 70 50 67 20 3d 20 70 50 67 2d 3e 67 66 72    pPg = pPg->gfr
beed0 65 65 2e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  ee.pNext;.      
beee0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
beef0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
bef00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
bef10 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
bef20 5f 53 54 41 54 49 43 5f 4c 52 55 29 29 3b 0a 0a  _STATIC_LRU));..
bef30 20 20 20 20 2f 2a 20 49 66 20 70 50 67 3d 3d 30      /* If pPg==0
bef40 2c 20 74 68 65 6e 20 74 68 65 20 62 6c 6f 63 6b  , then the block
bef50 20 61 62 6f 76 65 20 68 61 73 20 66 61 69 6c 65   above has faile
bef60 64 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65  d to find a page
bef70 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 63 79 63   to.    ** recyc
bef80 6c 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  le. In this case
bef90 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20 2d 20   return early - 
befa0 6e 6f 20 66 75 72 74 68 65 72 20 6d 65 6d 6f 72  no further memor
befb0 79 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65  y will.    ** be
befc0 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 20 20 2a   released..    *
befd0 2f 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 29  /.    if( !pPg )
befe0 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 70 50 61   break;..    pPa
beff0 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65  ger = pPg->pPage
bf000 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 70  r;.    assert(!p
bf010 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20 7c 7c 20  Pg->needSync || 
bf020 70 50 67 3d 3d 70 50 61 67 65 72 2d 3e 6c 72 75  pPg==pPager->lru
bf030 2e 70 46 69 72 73 74 29 3b 0a 20 20 20 20 61 73  .pFirst);.    as
bf040 73 65 72 74 28 70 50 67 2d 3e 6e 65 65 64 53 79  sert(pPg->needSy
bf050 6e 63 20 7c 7c 20 70 50 67 3d 3d 70 50 61 67 65  nc || pPg==pPage
bf060 72 2d 3e 6c 72 75 2e 70 46 69 72 73 74 53 79 6e  r->lru.pFirstSyn
bf070 63 65 64 29 3b 0a 20 20 0a 20 20 20 20 72 63 20  ced);.  .    rc 
bf080 3d 20 70 61 67 65 72 5f 72 65 63 79 63 6c 65 28  = pager_recycle(
bf090 70 50 61 67 65 72 2c 20 26 70 52 65 63 79 63 6c  pPager, &pRecycl
bf0a0 65 64 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ed);.    assert(
bf0b0 70 52 65 63 79 63 6c 65 64 3d 3d 70 50 67 20 7c  pRecycled==pPg |
bf0c0 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29  | rc!=SQLITE_OK)
bf0d0 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
bf0e0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
bf0f0 20 2f 2a 20 57 65 27 76 65 20 66 6f 75 6e 64 20   /* We've found 
bf100 61 20 70 61 67 65 20 74 6f 20 66 72 65 65 2e 20  a page to free. 
bf110 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68  At this point th
bf120 65 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20  e page has been 
bf130 0a 20 20 20 20 20 20 2a 2a 20 72 65 6d 6f 76 65  .      ** remove
bf140 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20  d from the page 
bf150 68 61 73 68 2d 74 61 62 6c 65 2c 20 66 72 65 65  hash-table, free
bf160 2d 6c 69 73 74 20 61 6e 64 20 73 79 6e 63 65 64  -list and synced
bf170 2d 6c 69 73 74 20 0a 20 20 20 20 20 20 2a 2a 20  -list .      ** 
bf180 28 70 46 69 72 73 74 53 79 6e 63 65 64 29 2e 20  (pFirstSynced). 
bf190 49 74 20 69 73 20 73 74 69 6c 6c 20 69 6e 20 74  It is still in t
bf1a0 68 65 20 61 6c 6c 20 70 61 67 65 73 20 28 70 41  he all pages (pA
bf1b0 6c 6c 29 20 6c 69 73 74 2e 20 0a 20 20 20 20 20  ll) list. .     
bf1c0 20 2a 2a 20 52 65 6d 6f 76 65 20 69 74 20 66 72   ** Remove it fr
bf1d0 6f 6d 20 74 68 69 73 20 6c 69 73 74 20 62 65 66  om this list bef
bf1e0 6f 72 65 20 66 72 65 65 69 6e 67 2e 0a 20 20 20  ore freeing..   
bf1f0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54     **.      ** T
bf200 6f 64 6f 3a 20 43 68 65 63 6b 20 74 68 65 20 50  odo: Check the P
bf210 61 67 65 72 2e 70 53 74 6d 74 20 6c 69 73 74 20  ager.pStmt list 
bf220 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 69  to make sure thi
bf230 73 20 69 73 20 4f 6b 2e 20 49 74 20 0a 20 20 20  s is Ok. It .   
bf240 20 20 20 2a 2a 20 70 72 6f 62 61 62 6c 79 20 69     ** probably i
bf250 73 20 74 68 6f 75 67 68 2e 0a 20 20 20 20 20 20  s though..      
bf260 2a 2f 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a  */.      PgHdr *
bf270 70 54 6d 70 3b 0a 20 20 20 20 20 20 61 73 73 65  pTmp;.      asse
bf280 72 74 28 20 70 50 67 20 29 3b 0a 20 20 20 20 20  rt( pPg );.     
bf290 20 69 66 28 20 70 50 67 3d 3d 70 50 61 67 65 72   if( pPg==pPager
bf2a0 2d 3e 70 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20  ->pAll ){.      
bf2b0 20 20 20 70 50 61 67 65 72 2d 3e 70 41 6c 6c 20     pPager->pAll 
bf2c0 3d 20 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 3b  = pPg->pNextAll;
bf2d0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
bf2e0 20 20 20 20 20 20 66 6f 72 28 20 70 54 6d 70 3d        for( pTmp=
bf2f0 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20 70 54  pPager->pAll; pT
bf300 6d 70 2d 3e 70 4e 65 78 74 41 6c 6c 21 3d 70 50  mp->pNextAll!=pP
bf310 67 3b 20 70 54 6d 70 3d 70 54 6d 70 2d 3e 70 4e  g; pTmp=pTmp->pN
bf320 65 78 74 41 6c 6c 20 29 7b 7d 0a 20 20 20 20 20  extAll ){}.     
bf330 20 20 20 70 54 6d 70 2d 3e 70 4e 65 78 74 41 6c     pTmp->pNextAl
bf340 6c 20 3d 20 70 50 67 2d 3e 70 4e 65 78 74 41 6c  l = pPg->pNextAl
bf350 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  l;.      }.     
bf360 20 6e 52 65 6c 65 61 73 65 64 20 2b 3d 20 28 0a   nReleased += (.
bf370 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
bf380 28 2a 70 50 67 29 20 2b 20 70 50 61 67 65 72 2d  (*pPg) + pPager-
bf390 3e 70 61 67 65 53 69 7a 65 0a 20 20 20 20 20 20  >pageSize.      
bf3a0 20 20 20 20 2b 20 73 69 7a 65 6f 66 28 75 33 32      + sizeof(u32
bf3b0 29 20 2b 20 70 50 61 67 65 72 2d 3e 6e 45 78 74  ) + pPager->nExt
bf3c0 72 61 0a 20 20 20 20 20 20 20 20 20 20 2b 20 4d  ra.          + M
bf3d0 45 4d 44 42 2a 73 69 7a 65 6f 66 28 50 67 48 69  EMDB*sizeof(PgHi
bf3e0 73 74 6f 72 79 29 20 0a 20 20 20 20 20 20 29 3b  story) .      );
bf3f0 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28  .      IOTRACE((
bf400 22 50 47 46 52 45 45 20 25 70 20 25 64 20 2a 5c  "PGFREE %p %d *\
bf410 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d  n", pPager, pPg-
bf420 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50  >pgno));.      P
bf430 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
bf440 33 5f 70 61 67 65 72 5f 70 67 66 72 65 65 5f 63  3_pager_pgfree_c
bf450 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  ount);.      sql
bf460 69 74 65 33 5f 66 72 65 65 28 70 50 67 2d 3e 70  ite3_free(pPg->p
bf470 44 61 74 61 29 3b 0a 20 20 20 20 20 20 73 71 6c  Data);.      sql
bf480 69 74 65 33 5f 66 72 65 65 28 70 50 67 29 3b 0a  ite3_free(pPg);.
bf490 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 50        pPager->nP
bf4a0 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 65 6c 73 65  age--;.    }else
bf4b0 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72  {.      /* An er
bf4c0 72 6f 72 20 6f 63 63 75 72 65 64 20 77 68 69 6c  ror occured whil
bf4d0 73 74 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68  st writing to th
bf4e0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
bf4f0 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75  or .      ** jou
bf500 72 6e 61 6c 20 69 6e 20 70 61 67 65 72 5f 72 65  rnal in pager_re
bf510 63 79 63 6c 65 28 29 2e 20 54 68 65 20 65 72 72  cycle(). The err
bf520 6f 72 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e  or is not return
bf530 65 64 20 74 6f 20 74 68 65 20 0a 20 20 20 20 20  ed to the .     
bf540 20 2a 2a 20 63 61 6c 6c 65 72 20 6f 66 20 74 68   ** caller of th
bf550 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49 6e 73  is function. Ins
bf560 74 65 61 64 2c 20 73 65 74 20 74 68 65 20 50 61  tead, set the Pa
bf570 67 65 72 2e 65 72 72 43 6f 64 65 20 76 61 72 69  ger.errCode vari
bf580 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2a 20 54  able..      ** T
bf590 68 65 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65  he error will be
bf5a0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
bf5b0 20 75 73 65 72 20 28 6f 72 20 75 73 65 72 73 2c   user (or users,
bf5c0 20 69 6e 20 74 68 65 20 63 61 73 65 20 0a 20 20   in the case .  
bf5d0 20 20 20 20 2a 2a 20 6f 66 20 61 20 73 68 61 72      ** of a shar
bf5e0 65 64 20 70 61 67 65 72 20 63 61 63 68 65 29 20  ed pager cache) 
bf5f0 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 6f 72  of the pager for
bf600 20 77 68 69 63 68 20 74 68 65 20 65 72 72 6f 72   which the error
bf610 20 6f 63 63 75 72 65 64 2e 0a 20 20 20 20 20 20   occured..      
bf620 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
bf630 0a 20 20 20 20 20 20 20 20 20 20 28 72 63 26 30  .          (rc&0
bf640 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  xff)==SQLITE_IOE
bf650 52 52 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20  RR ||.          
bf660 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20  rc==SQLITE_FULL 
bf670 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 72 63 3d  ||.          rc=
bf680 3d 53 51 4c 49 54 45 5f 42 55 53 59 0a 20 20 20  =SQLITE_BUSY.   
bf690 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65     );.      asse
bf6a0 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
bf6b0 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45  e>=PAGER_RESERVE
bf6c0 44 20 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72  D );.      pager
bf6d0 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
bf6e0 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  c);.    }.  }.. 
bf6f0 20 2f 2a 20 43 6c 65 61 72 20 74 68 65 20 6d 65   /* Clear the me
bf700 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20  mory management 
bf710 66 6c 61 67 73 20 61 6e 64 20 72 65 6c 65 61 73  flags and releas
bf720 65 20 74 68 65 20 6d 75 74 65 78 0a 20 20 2a 2f  e the mutex.  */
bf730 0a 20 20 66 6f 72 28 70 50 61 67 65 72 3d 73 71  .  for(pPager=sq
bf740 6c 69 74 65 33 50 61 67 65 72 4c 69 73 74 3b 20  lite3PagerList; 
bf750 70 50 61 67 65 72 3b 20 70 50 61 67 65 72 3d 70  pPager; pPager=p
bf760 50 61 67 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  Pager->pNext){. 
bf770 20 20 20 20 70 50 61 67 65 72 2d 3e 69 49 6e 55      pPager->iInU
bf780 73 65 4d 4d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  seMM = 0;.  }.  
bf790 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
bf7a0 61 76 65 28 6d 75 74 65 78 29 3b 0a 0a 20 20 2f  ave(mutex);..  /
bf7b0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
bf7c0 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 6c  ber of bytes rel
bf7d0 65 61 73 65 64 0a 20 20 2a 2f 0a 20 20 72 65 74  eased.  */.  ret
bf7e0 75 72 6e 20 6e 52 65 6c 65 61 73 65 64 3b 0a 7d  urn nReleased;.}
bf7f0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
bf800 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
bf810 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 2f  MANAGEMENT */../
bf820 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f  *.** Read the co
bf830 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 70 50  ntent of page pP
bf840 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61 74  g out of the dat
bf850 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  abase file..*/.s
bf860 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 44 62  tatic int readDb
bf870 50 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67  Page(Pager *pPag
bf880 65 72 2c 20 50 67 48 64 72 20 2a 70 50 67 2c 20  er, PgHdr *pPg, 
bf890 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e  Pgno pgno){.  in
bf8a0 74 20 72 63 3b 0a 20 20 69 36 34 20 6f 66 66 73  t rc;.  i64 offs
bf8b0 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20 4d 45  et;.  assert( ME
bf8c0 4d 44 42 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  MDB==0 );.  asse
bf8d0 72 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70  rt(pPager->fd->p
bf8e0 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d  Methods||pPager-
bf8f0 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 69 66  >tempFile);.  if
bf900 28 20 21 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70  ( !pPager->fd->p
bf910 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72  Methods ){.    r
bf920 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
bf930 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20  RR_SHORT_READ;. 
bf940 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 28 70   }.  offset = (p
bf950 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67  gno-1)*(i64)pPag
bf960 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20  er->pageSize;.  
bf970 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
bf980 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 50  ad(pPager->fd, P
bf990 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50 67  GHDR_TO_DATA(pPg
bf9a0 29 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  ), pPager->pageS
bf9b0 69 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20  ize, offset);.  
bf9c0 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74  PAGER_INCR(sqlit
bf9d0 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f  e3_pager_readdb_
bf9e0 63 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 52 5f  count);.  PAGER_
bf9f0 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 52 65  INCR(pPager->nRe
bfa00 61 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28  ad);.  IOTRACE((
bfa10 22 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 2c 20  "PGIN %p %d\n", 
bfa20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a  pPager, pgno));.
bfa30 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b    if( pgno==1 ){
bfa40 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61  .    memcpy(&pPa
bfa50 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c  ger->dbFileVers,
bfa60 20 26 28 28 75 38 2a 29 50 47 48 44 52 5f 54 4f   &((u8*)PGHDR_TO
bfa70 5f 44 41 54 41 28 70 50 67 29 29 5b 32 34 5d 2c  _DATA(pPg))[24],
bfa80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
bfa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
bfab0 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62  izeof(pPager->db
bfac0 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 7d 0a  FileVers));.  }.
bfad0 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c    CODEC1(pPager,
bfae0 20 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70   PGHDR_TO_DATA(p
bfaf0 50 67 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  Pg), pPg->pgno, 
bfb00 33 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45  3);.  PAGERTRACE
bfb10 34 28 22 46 45 54 43 48 20 25 64 20 70 61 67 65  4("FETCH %d page
bfb20 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e   %d hash(%08x)\n
bfb30 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
bfb40 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72    PAGERID(pPager
bfb50 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 61  ), pPg->pgno, pa
bfb60 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
bfb70 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ));.  return rc;
bfb80 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
bfb90 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
bfba0 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  ed to obtain the
bfbb0 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 72 65 71   shared lock req
bfbc0 75 69 72 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20  uired before.** 
bfbd0 64 61 74 61 20 6d 61 79 20 62 65 20 72 65 61 64  data may be read
bfbe0 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20   from the pager 
bfbf0 63 61 63 68 65 2e 20 49 66 20 74 68 65 20 73 68  cache. If the sh
bfc00 61 72 65 64 20 6c 6f 63 6b 20 68 61 73 20 61 6c  ared lock has al
bfc10 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 6f 62  ready.** been ob
bfc20 74 61 69 6e 65 64 2c 20 74 68 69 73 20 66 75 6e  tained, this fun
bfc30 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
bfc40 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d 65 64 69 61 74  ..**.** Immediat
bfc50 65 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69 6e  ely after obtain
bfc60 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
bfc70 6f 63 6b 20 28 69 66 20 72 65 71 75 69 72 65 64  ock (if required
bfc80 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ), this function
bfc90 0a 2a 2a 20 63 68 65 63 6b 73 20 66 6f 72 20 61  .** checks for a
bfca0 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   hot-journal fil
bfcb0 65 2e 20 49 66 20 6f 6e 65 20 69 73 20 66 6f 75  e. If one is fou
bfcc0 6e 64 2c 20 61 6e 20 65 6d 65 72 67 65 6e 63 79  nd, an emergency
bfcd0 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20   rollback.** is 
bfce0 70 65 72 66 6f 72 6d 65 64 20 69 6d 6d 65 64 69  performed immedi
bfcf0 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ately..*/.static
bfd00 20 69 6e 74 20 70 61 67 65 72 53 68 61 72 65 64   int pagerShared
bfd10 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67  Lock(Pager *pPag
bfd20 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
bfd30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
bfd40 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  ( pPager->state=
bfd50 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b  =PAGER_UNLOCK ){
bfd60 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
bfd70 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d   *pVfs = pPager-
bfd80 3e 70 56 66 73 3b 0a 20 20 20 20 69 66 28 20 21  >pVfs;.    if( !
bfd90 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 61  MEMDB ){.      a
bfda0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e  ssert( pPager->n
bfdb0 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  Ref==0 );.      
bfdc0 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 52  if( !pPager->noR
bfdd0 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  eadlock ){.     
bfde0 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61     rc = pager_wa
bfdf0 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65  it_on_lock(pPage
bfe00 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b  r, SHARED_LOCK);
bfe10 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
bfe20 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
bfe30 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70          return p
bfe40 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
bfe50 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20  r, rc);.        
bfe60 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
bfe70 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
bfe80 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
bfe90 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20        }.  .     
bfea0 20 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c   /* If a journal
bfeb0 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e   file exists, an
bfec0 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45  d there is no RE
bfed0 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SERVED lock on t
bfee0 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61  he.      ** data
bfef0 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20  base file, then 
bff00 69 74 20 65 69 74 68 65 72 20 6e 65 65 64 73 20  it either needs 
bff10 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63  to be played bac
bff20 6b 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 20 20  k or deleted..  
bff30 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
bff40 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70   hasHotJournal(p
bff50 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20  Pager) ){.      
bff60 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c    /* Get an EXCL
bff70 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
bff80 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
bff90 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69   At this point i
bffa0 74 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  t is.        ** 
bffb0 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61  important that a
bffc0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69   RESERVED lock i
bffd0 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f  s not obtained o
bffe0 6e 20 74 68 65 20 77 61 79 20 74 6f 20 74 68 65  n the way to the
bfff0 0a 20 20 20 20 20 20 20 20 2a 2a 20 45 58 43 4c  .        ** EXCL
c0000 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69  USIVE lock. If i
c0010 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20  t were, another 
c0020 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70  process might op
c0030 65 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  en the.        *
c0040 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  * database file,
c0050 20 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45   detect the RESE
c0060 52 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63  RVED lock, and c
c0070 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65  onclude that the
c0080 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61  .        ** data
c0090 62 61 73 65 20 69 73 20 73 61 66 65 20 74 6f 20  base is safe to 
c00a0 72 65 61 64 20 77 68 69 6c 65 20 74 68 69 73 20  read while this 
c00b0 70 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c  process is still
c00c0 20 72 6f 6c 6c 69 6e 67 20 69 74 20 0a 20 20 20   rolling it .   
c00d0 20 20 20 20 20 2a 2a 20 62 61 63 6b 2e 0a 20 20       ** back..  
c00e0 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20        ** .      
c00f0 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65    ** Because the
c0100 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 52 45   intermediate RE
c0110 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e  SERVED lock is n
c0120 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20 74 68  ot requested, th
c0130 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 63  e.        ** sec
c0140 6f 6e 64 20 70 72 6f 63 65 73 73 20 77 69 6c 6c  ond process will
c0150 20 67 65 74 20 74 6f 20 74 68 69 73 20 70 6f 69   get to this poi
c0160 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61  nt in the code a
c0170 6e 64 20 66 61 69 6c 20 74 6f 0a 20 20 20 20 20  nd fail to.     
c0180 20 20 20 2a 2a 20 6f 62 74 61 69 6e 20 69 74 27     ** obtain it'
c0190 73 20 6f 77 6e 20 45 58 43 4c 55 53 49 56 45 20  s own EXCLUSIVE 
c01a0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
c01b0 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20  base file..     
c01c0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63     */.        rc
c01d0 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b   = sqlite3OsLock
c01e0 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 45 58 43  (pPager->fd, EXC
c01f0 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20  LUSIVE_LOCK);.  
c0200 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
c0210 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
c0220 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63       pager_unloc
c0230 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  k(pPager);.     
c0240 20 20 20 20 20 72 65 74 75 72 6e 20 70 61 67 65       return page
c0250 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20  r_error(pPager, 
c0260 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rc);.        }. 
c0270 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73         pPager->s
c0280 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43  tate = PAGER_EXC
c0290 4c 55 53 49 56 45 3b 0a 20 0a 20 20 20 20 20 20  LUSIVE;. .      
c02a0 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f    /* Open the jo
c02b0 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 69 6e  urnal for readin
c02c0 67 20 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20  g only.  Return 
c02d0 53 51 4c 49 54 45 5f 42 55 53 59 20 69 66 0a 20  SQLITE_BUSY if. 
c02e0 20 20 20 20 20 20 20 2a 2a 20 77 65 20 61 72 65         ** we are
c02f0 20 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20   unable to open 
c0300 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
c0310 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  . .        **.  
c0320 20 20 20 20 20 20 2a 2a 20 54 68 65 20 6a 6f 75        ** The jou
c0330 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e  rnal file does n
c0340 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f  ot need to be lo
c0350 63 6b 65 64 20 69 74 73 65 6c 66 2e 20 20 54 68  cked itself.  Th
c0360 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6a 6f 75  e.        ** jou
c0370 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 65 76  rnal file is nev
c0380 65 72 20 6f 70 65 6e 20 75 6e 6c 65 73 73 20 74  er open unless t
c0390 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
c03a0 20 66 69 6c 65 20 68 6f 6c 64 73 0a 20 20 20 20   file holds.    
c03b0 20 20 20 20 2a 2a 20 61 20 77 72 69 74 65 20 6c      ** a write l
c03c0 6f 63 6b 2c 20 73 6f 20 74 68 65 72 65 20 69 73  ock, so there is
c03d0 20 6e 65 76 65 72 20 61 6e 79 20 63 68 61 6e 63   never any chanc
c03e0 65 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65  e of two or more
c03f0 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63  .        ** proc
c0400 65 73 73 65 73 20 6f 70 65 6e 69 6e 67 20 74 68  esses opening th
c0410 65 20 6a 6f 75 72 6e 61 6c 20 61 74 20 74 68 65  e journal at the
c0420 20 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 20 20   same time..    
c0430 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
c0440 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e  * Open the journ
c0450 61 6c 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74  al for read/writ
c0460 65 20 61 63 63 65 73 73 2e 20 54 68 69 73 20 69  e access. This i
c0470 73 20 62 65 63 61 75 73 65 20 69 6e 20 0a 20 20  s because in .  
c0480 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69        ** exclusi
c0490 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74  ve-access mode t
c04a0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
c04b0 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74 20  or will be kept 
c04c0 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20 20  open and.       
c04d0 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73 65   ** possibly use
c04e0 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74  d for a transact
c04f0 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e  ion later on. On
c0500 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 74   some systems, t
c0510 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 73  he.        ** Os
c0520 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20  Truncate() call 
c0530 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76  used in exclusiv
c0540 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c  e-access mode al
c0550 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 20 20  so requires.    
c0560 20 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72      ** a read/wr
c0570 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e  ite file handle.
c0580 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
c0590 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
c05a0 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 69 66  BUSY;.        if
c05b0 28 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  ( sqlite3OsAcces
c05c0 73 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  s(pVfs, pPager->
c05d0 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45  zJournal, SQLITE
c05e0 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 29 20  _ACCESS_EXISTS) 
c05f0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
c0600 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20 20 20   fout = 0;.     
c0610 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d       int flags =
c0620 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
c0630 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
c0640 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b  EN_MAIN_JOURNAL;
c0650 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
c0660 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  t( !pPager->temp
c0670 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20  File );.        
c0680 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
c0690 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65  Open(pVfs, pPage
c06a0 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61  r->zJournal, pPa
c06b0 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c  ger->jfd, flags,
c06c0 20 26 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20   &fout);.       
c06d0 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
c06e0 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
c06f0 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f 64  er->jfd->pMethod
c0700 73 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  s );.          i
c0710 66 28 20 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f  f( fout&SQLITE_O
c0720 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a  PEN_READONLY ){.
c0730 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
c0740 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
c0750 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
c0760 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
c0770 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20  >jfd);.         
c0780 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
c0790 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
c07a0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
c07b0 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b      pager_unlock
c07c0 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
c07d0 20 20 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d      return (rc==
c07e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3f 72 63 3a  SQLITE_NOMEM?rc:
c07f0 53 51 4c 49 54 45 5f 42 55 53 59 29 3b 0a 20 20  SQLITE_BUSY);.  
c0800 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
c0810 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
c0820 70 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  pen = 1;.       
c0830 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
c0840 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20  Started = 0;.   
c0850 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
c0860 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20  rnalOff = 0;.   
c0870 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74       pPager->set
c0880 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
c0890 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
c08a0 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a 20 20  nalHdr = 0;. .  
c08b0 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63        /* Playbac
c08c0 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65  k and delete the
c08d0 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20   journal.  Drop 
c08e0 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72 69  the database wri
c08f0 74 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 6f  te.        ** lo
c0900 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 65  ck and reacquire
c0910 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a   the read lock..
c0920 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
c0930 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
c0940 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31  ayback(pPager, 1
c0950 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
c0960 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
c0970 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
c0980 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
c0990 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20  ger, rc);.      
c09a0 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65    }.        asse
c09b0 72 74 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  rt(pPager->state
c09c0 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c  ==PAGER_SHARED |
c09d0 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 28  | .            (
c09e0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
c09f0 65 4d 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d  eMode && pPager-
c0a00 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 53 48 41  >state>PAGER_SHA
c0a10 52 45 44 29 0a 20 20 20 20 20 20 20 20 29 3b 0a  RED).        );.
c0a20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
c0a30 66 28 20 70 50 61 67 65 72 2d 3e 70 41 6c 6c 20  f( pPager->pAll 
c0a40 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
c0a50 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61  e shared-lock ha
c0a60 73 20 6a 75 73 74 20 62 65 65 6e 20 61 63 71 75  s just been acqu
c0a70 69 72 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61  ired on the data
c0a80 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20  base file.      
c0a90 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61    ** and there a
c0aa0 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65 73  re already pages
c0ab0 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28 66   in the cache (f
c0ac0 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a 20  rom a previous. 
c0ad0 20 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f         ** read o
c0ae0 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  r write transact
c0af0 69 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20  ion).  Check to 
c0b00 73 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62  see if the datab
c0b10 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 68  ase.        ** h
c0b20 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  as been modified
c0b30 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61  .  If the databa
c0b40 73 65 20 68 61 73 20 63 68 61 6e 67 65 64 2c 20  se has changed, 
c0b50 66 6c 75 73 68 20 74 68 65 0a 20 20 20 20 20 20  flush the.      
c0b60 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 20 20 20    ** cache..    
c0b70 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
c0b80 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67  * Database chang
c0b90 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62  es is detected b
c0ba0 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20  y looking at 15 
c0bb0 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a  bytes beginning.
c0bc0 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66          ** at of
c0bd0 66 73 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65  fset 24 into the
c0be0 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73   file.  The firs
c0bf0 74 20 34 20 6f 66 20 74 68 65 73 65 20 31 36 20  t 4 of these 16 
c0c00 62 79 74 65 73 20 61 72 65 0a 20 20 20 20 20 20  bytes are.      
c0c10 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 20 63 6f    ** a 32-bit co
c0c20 75 6e 74 65 72 20 74 68 61 74 20 69 73 20 69 6e  unter that is in
c0c30 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65  cremented with e
c0c40 61 63 68 20 63 68 61 6e 67 65 2e 20 20 54 68 65  ach change.  The
c0c50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 74 68 65  .        ** othe
c0c60 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 20 72  r bytes change r
c0c70 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 63  andomly with eac
c0c80 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77 68  h file change wh
c0c90 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20  en.        ** a 
c0ca0 63 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e  codec is in use.
c0cb0 0a 20 20 20 20 20 20 20 20 2a 2a 20 0a 20 20 20  .        ** .   
c0cc0 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73       ** There is
c0cd0 20 61 20 76 61 6e 69 73 68 69 6e 67 6c 79 20 73   a vanishingly s
c0ce0 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 68 61 74  mall chance that
c0cf0 20 61 20 63 68 61 6e 67 65 20 77 69 6c 6c 20 6e   a change will n
c0d00 6f 74 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a  ot be .        *
c0d10 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 68 65  * detected.  The
c0d20 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 75 6e   chance of an un
c0d30 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 65 20  detected change 
c0d40 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74  is so small that
c0d50 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 63  .        ** it c
c0d60 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e  an be neglected.
c0d70 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
c0d80 20 20 20 20 63 68 61 72 20 64 62 46 69 6c 65 56      char dbFileV
c0d90 65 72 73 5b 73 69 7a 65 6f 66 28 70 50 61 67 65  ers[sizeof(pPage
c0da0 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 5d 3b  r->dbFileVers)];
c0db0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
c0dc0 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
c0dd0 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 20 20 20  Pager);..       
c0de0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
c0df0 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Code ){.        
c0e00 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
c0e10 3e 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20  >errCode;.      
c0e20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 66 28    }..        if(
c0e30 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e   pPager->dbSize>
c0e40 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 49  0 ){.          I
c0e50 4f 54 52 41 43 45 28 28 22 43 4b 56 45 52 53 20  OTRACE(("CKVERS 
c0e60 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72  %p %d\n", pPager
c0e70 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56  , sizeof(dbFileV
c0e80 65 72 73 29 29 29 3b 0a 20 20 20 20 20 20 20 20  ers)));.        
c0e90 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
c0ea0 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c  Read(pPager->fd,
c0eb0 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69   &dbFileVers, si
c0ec0 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29  zeof(dbFileVers)
c0ed0 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 20  , 24);.         
c0ee0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
c0ef0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
c0f00 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
c0f10 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
c0f20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
c0f30 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 56    memset(dbFileV
c0f40 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64  ers, 0, sizeof(d
c0f50 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20  bFileVers));.   
c0f60 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
c0f70 69 66 28 20 6d 65 6d 63 6d 70 28 70 50 61 67 65  if( memcmp(pPage
c0f80 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64  r->dbFileVers, d
c0f90 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f  bFileVers, sizeo
c0fa0 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 21 3d  f(dbFileVers))!=
c0fb0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  0 ){.          p
c0fc0 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65  ager_reset(pPage
c0fd0 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
c0fe0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
c0ff0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
c1000 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c  exclusiveMode ||
c1010 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 3d   pPager->state<=
c1020 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a  PAGER_SHARED );.
c1030 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
c1040 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c  state==PAGER_UNL
c1050 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61  OCK ){.      pPa
c1060 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47  ger->state = PAG
c1070 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 7d  ER_SHARED;.    }
c1080 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
c1090 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  c;.}../*.** Allo
c10a0 63 61 74 65 20 61 20 50 67 48 64 72 20 6f 62 6a  cate a PgHdr obj
c10b0 65 63 74 2e 20 20 20 45 69 74 68 65 72 20 63 72  ect.   Either cr
c10c0 65 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 20 6f  eate a new one o
c10d0 72 20 72 65 75 73 65 0a 2a 2a 20 61 6e 20 65 78  r reuse.** an ex
c10e0 69 73 74 69 6e 67 20 6f 6e 65 20 74 68 61 74 20  isting one that 
c10f0 69 73 20 6e 6f 74 20 6f 74 68 65 72 77 69 73 65  is not otherwise
c1100 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 41   in use..**.** A
c1110 20 6e 65 77 20 50 67 48 64 72 20 73 74 72 75 63   new PgHdr struc
c1120 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20  ture is created 
c1130 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f  if any of the fo
c1140 6c 6c 6f 77 69 6e 67 20 61 72 65 0a 2a 2a 20 74  llowing are.** t
c1150 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  rue:.**.**     (
c1160 31 29 20 20 57 65 20 68 61 76 65 20 6e 6f 74 20  1)  We have not 
c1170 65 78 63 65 65 64 65 64 20 6f 75 72 20 6d 61 78  exceeded our max
c1180 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 65 64 20 63  imum allocated c
c1190 61 63 68 65 20 73 69 7a 65 0a 2a 2a 20 20 20 20  ache size.**    
c11a0 20 20 20 20 20 20 61 73 20 73 65 74 20 62 79 20        as set by 
c11b0 74 68 65 20 22 50 52 41 47 4d 41 20 63 61 63 68  the "PRAGMA cach
c11c0 65 5f 73 69 7a 65 22 20 63 6f 6d 6d 61 6e 64 2e  e_size" command.
c11d0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 32 29 20 20  .**.**     (2)  
c11e0 54 68 65 72 65 20 61 72 65 20 6e 6f 20 75 6e 75  There are no unu
c11f0 73 65 64 20 50 67 48 64 72 20 6f 62 6a 65 63 74  sed PgHdr object
c1200 73 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 74  s available at t
c1210 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  his time..**.** 
c1220 20 20 20 20 28 33 29 20 20 54 68 69 73 20 69 73      (3)  This is
c1230 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
c1240 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  tabase..**.**   
c1250 20 20 28 34 29 20 20 54 68 65 72 65 20 61 72 65    (4)  There are
c1260 20 6e 6f 20 50 67 48 64 72 20 6f 62 6a 65 63 74   no PgHdr object
c1270 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 72 65  s that do not re
c1280 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 0a  quire a journal.
c1290 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c 65  **          file
c12a0 20 73 79 6e 63 20 61 6e 64 20 61 20 73 79 6e 63   sync and a sync
c12b0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
c12c0 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c  file is currentl
c12d0 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 72  y.**          pr
c12e0 6f 68 69 62 69 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ohibited..**.** 
c12f0 4f 74 68 65 72 77 69 73 65 2c 20 72 65 75 73 65  Otherwise, reuse
c1300 20 61 6e 20 65 78 69 73 74 69 6e 67 20 50 67 48   an existing PgH
c1310 64 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  dr.  In other wo
c1320 72 64 73 2c 20 72 65 75 73 65 20 61 6e 0a 2a 2a  rds, reuse an.**
c1330 20 65 78 69 73 74 69 6e 67 20 50 67 48 64 72 20   existing PgHdr 
c1340 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f  if all of the fo
c1350 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65  llowing are true
c1360 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20  :.**.**     (1) 
c1370 20 57 65 20 68 61 76 65 20 72 65 61 63 68 65 64   We have reached
c1380 20 6f 72 20 65 78 63 65 65 64 65 64 20 74 68 65   or exceeded the
c1390 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 20 73   maximum cache s
c13a0 69 7a 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ize.**          
c13b0 61 6c 6c 6f 77 65 64 20 62 79 20 22 50 52 41 47  allowed by "PRAG
c13c0 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 22 2e 0a  MA cache_size"..
c13d0 2a 2a 0a 2a 2a 20 20 20 20 20 28 32 29 20 20 54  **.**     (2)  T
c13e0 68 65 72 65 20 69 73 20 61 20 50 67 48 64 72 20  here is a PgHdr 
c13f0 61 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20 50  available with P
c1400 67 48 64 72 2d 3e 6e 52 65 66 3d 3d 30 0a 2a 2a  gHdr->nRef==0.**
c1410 0a 2a 2a 20 20 20 20 20 28 33 29 20 20 57 65 20  .**     (3)  We 
c1420 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 20 69 6e  are not in an in
c1430 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
c1440 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 34 29 20 20  .**.**     (4)  
c1450 45 69 74 68 65 72 20 74 68 65 72 65 20 69 73 20  Either there is 
c1460 61 6e 20 61 76 61 69 6c 61 62 6c 65 20 50 67 48  an available PgH
c1470 64 72 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  dr that does not
c1480 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 20 20 20   need.**        
c1490 20 20 74 6f 20 62 65 20 73 79 6e 63 65 64 20 74    to be synced t
c14a0 6f 20 64 69 73 6b 20 6f 72 20 65 6c 73 65 20 64  o disk or else d
c14b0 69 73 6b 20 73 79 6e 63 69 6e 67 20 69 73 20 63  isk syncing is c
c14c0 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20  urrently.**     
c14d0 20 20 20 20 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f       allowed..*/
c14e0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
c14f0 72 41 6c 6c 6f 63 61 74 65 50 61 67 65 28 50 61  rAllocatePage(Pa
c1500 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 48  ger *pPager, PgH
c1510 64 72 20 2a 2a 70 70 50 67 29 7b 0a 20 20 69 6e  dr **ppPg){.  in
c1520 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
c1530 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a  ;.  PgHdr *pPg;.
c1540 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 0a    void *pData;..
c1550 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65    /* Create a ne
c1560 77 20 50 67 48 64 72 20 69 66 20 61 6e 79 20 6f  w PgHdr if any o
c1570 66 20 74 68 65 20 66 6f 75 72 20 63 6f 6e 64 69  f the four condi
c1580 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 0a 20  tions defined . 
c1590 20 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 6d 65   ** above are me
c15a0 74 3a 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  t: */.  if( pPag
c15b0 65 72 2d 3e 6e 50 61 67 65 3c 70 50 61 67 65 72  er->nPage<pPager
c15c0 2d 3e 6d 78 50 61 67 65 0a 20 20 20 7c 7c 20 70  ->mxPage.   || p
c15d0 50 61 67 65 72 2d 3e 6c 72 75 2e 70 46 69 72 73  Pager->lru.pFirs
c15e0 74 3d 3d 30 20 0a 20 20 20 7c 7c 20 4d 45 4d 44  t==0 .   || MEMD
c15f0 42 0a 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d  B.   || (pPager-
c1600 3e 6c 72 75 2e 70 46 69 72 73 74 53 79 6e 63 65  >lru.pFirstSynce
c1610 64 3d 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e  d==0 && pPager->
c1620 64 6f 4e 6f 74 53 79 6e 63 29 0a 20 20 29 7b 0a  doNotSync).  ){.
c1630 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
c1640 6e 50 61 67 65 3e 3d 70 50 61 67 65 72 2d 3e 6e  nPage>=pPager->n
c1650 48 61 73 68 20 29 7b 0a 20 20 20 20 20 20 70 61  Hash ){.      pa
c1660 67 65 72 5f 72 65 73 69 7a 65 5f 68 61 73 68 5f  ger_resize_hash_
c1670 74 61 62 6c 65 28 70 50 61 67 65 72 2c 0a 20 20  table(pPager,.  
c1680 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e         pPager->n
c1690 48 61 73 68 3c 32 35 36 20 3f 20 32 35 36 20 3a  Hash<256 ? 256 :
c16a0 20 70 50 61 67 65 72 2d 3e 6e 48 61 73 68 2a 32   pPager->nHash*2
c16b0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  );.      if( pPa
c16c0 67 65 72 2d 3e 6e 48 61 73 68 3d 3d 30 20 29 7b  ger->nHash==0 ){
c16d0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
c16e0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
c16f0 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61      goto pager_a
c1700 6c 6c 6f 63 61 74 65 5f 6f 75 74 3b 0a 20 20 20  llocate_out;.   
c1710 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
c1720 61 67 65 72 4c 65 61 76 65 28 70 50 61 67 65 72  agerLeave(pPager
c1730 29 3b 0a 20 20 20 20 70 50 67 20 3d 20 73 71 6c  );.    pPg = sql
c1740 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
c1750 65 6f 66 28 2a 70 50 67 29 20 2b 20 73 69 7a 65  eof(*pPg) + size
c1760 6f 66 28 75 33 32 29 20 2b 20 70 50 61 67 65 72  of(u32) + pPager
c1770 2d 3e 6e 45 78 74 72 61 0a 20 20 20 20 20 20 20  ->nExtra.       
c1780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1790 20 20 20 20 20 2b 20 4d 45 4d 44 42 2a 73 69 7a       + MEMDB*siz
c17a0 65 6f 66 28 50 67 48 69 73 74 6f 72 79 29 20 29  eof(PgHistory) )
c17b0 3b 0a 20 20 20 20 69 66 28 20 70 50 67 20 29 7b  ;.    if( pPg ){
c17c0 0a 20 20 20 20 20 20 70 44 61 74 61 20 3d 20 73  .      pData = s
c17d0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 70  qlite3_malloc( p
c17e0 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20  Pager->pageSize 
c17f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 61  );.      if( pDa
c1800 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ta==0 ){.       
c1810 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
c1820 67 29 3b 0a 20 20 20 20 20 20 20 20 70 50 67 20  g);.        pPg 
c1830 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
c1840 20 7d 0a 20 20 20 20 70 61 67 65 72 45 6e 74 65   }.    pagerEnte
c1850 72 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  r(pPager);.    i
c1860 66 28 20 70 50 67 3d 3d 30 20 29 7b 0a 20 20 20  f( pPg==0 ){.   
c1870 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
c1880 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f  OMEM;.      goto
c1890 20 70 61 67 65 72 5f 61 6c 6c 6f 63 61 74 65 5f   pager_allocate_
c18a0 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  out;.    }.    m
c18b0 65 6d 73 65 74 28 70 50 67 2c 20 30 2c 20 73 69  emset(pPg, 0, si
c18c0 7a 65 6f 66 28 2a 70 50 67 29 29 3b 0a 20 20 20  zeof(*pPg));.   
c18d0 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20   if( MEMDB ){.  
c18e0 20 20 20 20 6d 65 6d 73 65 74 28 50 47 48 44 52      memset(PGHDR
c18f0 5f 54 4f 5f 48 49 53 54 28 70 50 67 2c 20 70 50  _TO_HIST(pPg, pP
c1900 61 67 65 72 29 2c 20 30 2c 20 73 69 7a 65 6f 66  ager), 0, sizeof
c1910 28 50 67 48 69 73 74 6f 72 79 29 29 3b 0a 20 20  (PgHistory));.  
c1920 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 70 44 61    }.    pPg->pDa
c1930 74 61 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20  ta = pData;.    
c1940 70 50 67 2d 3e 70 50 61 67 65 72 20 3d 20 70 50  pPg->pPager = pP
c1950 61 67 65 72 3b 0a 20 20 20 20 70 50 67 2d 3e 70  ager;.    pPg->p
c1960 4e 65 78 74 41 6c 6c 20 3d 20 70 50 61 67 65 72  NextAll = pPager
c1970 2d 3e 70 41 6c 6c 3b 0a 20 20 20 20 70 50 61 67  ->pAll;.    pPag
c1980 65 72 2d 3e 70 41 6c 6c 20 3d 20 70 50 67 3b 0a  er->pAll = pPg;.
c1990 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 50 61 67      pPager->nPag
c19a0 65 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  e++;.  }else{.  
c19b0 20 20 2f 2a 20 52 65 63 79 63 6c 65 20 61 6e 20    /* Recycle an 
c19c0 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 77 69  existing page wi
c19d0 74 68 20 61 20 7a 65 72 6f 20 72 65 66 2d 63 6f  th a zero ref-co
c19e0 75 6e 74 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  unt. */.    rc =
c19f0 20 70 61 67 65 72 5f 72 65 63 79 63 6c 65 28 70   pager_recycle(p
c1a00 50 61 67 65 72 2c 20 26 70 50 67 29 3b 0a 20 20  Pager, &pPg);.  
c1a10 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
c1a20 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 72  _BUSY ){.      r
c1a30 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
c1a40 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 7d 0a  _BLOCKED;.    }.
c1a50 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
c1a60 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67  TE_OK ){.      g
c1a70 6f 74 6f 20 70 61 67 65 72 5f 61 6c 6c 6f 63 61  oto pager_alloca
c1a80 74 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  te_out;.    }.  
c1a90 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
c1aa0 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45 44 5f  ->state>=SHARED_
c1ab0 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65  LOCK );.    asse
c1ac0 72 74 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 2a  rt(pPg);.  }.  *
c1ad0 70 70 50 67 20 3d 20 70 50 67 3b 0a 0a 70 61 67  ppPg = pPg;..pag
c1ae0 65 72 5f 61 6c 6c 6f 63 61 74 65 5f 6f 75 74 3a  er_allocate_out:
c1af0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
c1b00 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
c1b10 20 77 65 20 68 61 76 65 20 74 68 65 20 63 6f 6e   we have the con
c1b20 74 65 6e 74 20 66 6f 72 20 61 20 70 61 67 65 2e  tent for a page.
c1b30 20 20 49 66 20 74 68 65 20 70 61 67 65 20 77 61    If the page wa
c1b40 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
c1b50 61 63 71 75 69 72 65 64 20 77 69 74 68 20 6e 6f  acquired with no
c1b60 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74 68 65 6e  Content==1, then
c1b70 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 61 73   the content was
c1b80 0a 2a 2a 20 6a 75 73 74 20 69 6e 69 74 69 61 6c  .** just initial
c1b90 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 69 6e  ized to zeros in
c1ba0 73 74 65 61 64 20 6f 66 20 62 65 69 6e 67 20 72  stead of being r
c1bb0 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a  ead from disk..*
c1bc0 2a 20 42 75 74 20 6e 6f 77 20 77 65 20 6e 65 65  * But now we nee
c1bd0 64 20 74 68 65 20 72 65 61 6c 20 64 61 74 61 20  d the real data 
c1be0 6f 66 66 20 6f 66 20 64 69 73 6b 2e 20 20 53 6f  off of disk.  So
c1bf0 20 6d 61 6b 65 20 73 75 72 65 20 77 65 0a 2a 2a   make sure we.**
c1c00 20 68 61 76 65 20 69 74 2e 20 20 52 65 61 64 20   have it.  Read 
c1c10 69 74 20 69 6e 20 69 66 20 77 65 20 64 6f 20 6e  it in if we do n
c1c20 6f 74 20 68 61 76 65 20 69 74 20 61 6c 72 65 61  ot have it alrea
c1c30 64 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  dy..*/.static in
c1c40 74 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74  t pager_get_cont
c1c50 65 6e 74 28 50 67 48 64 72 20 2a 70 50 67 29 7b  ent(PgHdr *pPg){
c1c60 0a 20 20 69 66 28 20 70 50 67 2d 3e 6e 65 65 64  .  if( pPg->need
c1c70 52 65 61 64 20 29 7b 0a 20 20 20 20 69 6e 74 20  Read ){.    int 
c1c80 72 63 20 3d 20 72 65 61 64 44 62 50 61 67 65 28  rc = readDbPage(
c1c90 70 50 67 2d 3e 70 50 61 67 65 72 2c 20 70 50 67  pPg->pPager, pPg
c1ca0 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
c1cb0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
c1cc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 67  _OK ){.      pPg
c1cd0 2d 3e 6e 65 65 64 52 65 61 64 20 3d 20 30 3b 0a  ->needRead = 0;.
c1ce0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
c1cf0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
c1d00 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  }.  }.  return S
c1d10 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
c1d20 2a 2a 20 41 63 71 75 69 72 65 20 61 20 70 61 67  ** Acquire a pag
c1d30 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 61 64 20  e..**.** A read 
c1d40 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 69 73 6b  lock on the disk
c1d50 20 66 69 6c 65 20 69 73 20 6f 62 74 61 69 6e 65   file is obtaine
c1d60 64 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 74  d when the first
c1d70 20 70 61 67 65 20 69 73 20 61 63 71 75 69 72 65   page is acquire
c1d80 64 2e 20 0a 2a 2a 20 54 68 69 73 20 72 65 61 64  d. .** This read
c1d90 20 6c 6f 63 6b 20 69 73 20 64 72 6f 70 70 65 64   lock is dropped
c1da0 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 70   when the last p
c1db0 61 67 65 20 69 73 20 72 65 6c 65 61 73 65 64 2e  age is released.
c1dc0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
c1dd0 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20 61 6e  ine works for an
c1de0 79 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72  y page number gr
c1df0 65 61 74 65 72 20 74 68 61 6e 20 30 2e 20 20 49  eater than 0.  I
c1e00 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  f the database.*
c1e10 2a 20 66 69 6c 65 20 69 73 20 73 6d 61 6c 6c 65  * file is smalle
c1e20 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 65  r than the reque
c1e30 73 74 65 64 20 70 61 67 65 2c 20 74 68 65 6e 20  sted page, then 
c1e40 6e 6f 20 61 63 74 75 61 6c 20 64 69 73 6b 0a 2a  no actual disk.*
c1e50 2a 20 72 65 61 64 20 6f 63 63 75 72 73 20 61 6e  * read occurs an
c1e60 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61  d the memory ima
c1e70 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 69  ge of the page i
c1e80 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  s initialized to
c1e90 0a 2a 2a 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 20  .** all zeros.  
c1ea0 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20 61  The extra data a
c1eb0 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 67  ppended to a pag
c1ec0 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 74  e is always init
c1ed0 69 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 7a 65  ialized.** to ze
c1ee0 72 6f 73 20 74 68 65 20 66 69 72 73 74 20 74 69  ros the first ti
c1ef0 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61  me a page is loa
c1f00 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e  ded into memory.
c1f10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 69  .**.** The acqui
c1f20 73 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 69  sition might fai
c1f30 6c 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 65  l for several re
c1f40 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 63  asons.  In all c
c1f50 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 72  ases,.** an appr
c1f60 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f  opriate error co
c1f70 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  de is returned a
c1f80 6e 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  nd *ppPage is se
c1f90 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  t to NULL..**.**
c1fa0 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65   See also sqlite
c1fb0 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20  3PagerLookup(). 
c1fc0 20 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69   Both this routi
c1fd0 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20  ne and Lookup() 
c1fe0 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 69  attempt.** to fi
c1ff0 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65  nd a page in the
c2000 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65   in-memory cache
c2010 20 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 20   first.  If the 
c2020 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  page is not alre
c2030 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79  ady.** in memory
c2040 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67  , this routine g
c2050 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72  oes to disk to r
c2060 65 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 61  ead it in wherea
c2070 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75  s Lookup().** ju
c2080 73 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 54  st returns 0.  T
c2090 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75  his routine acqu
c20a0 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  ires a read-lock
c20b0 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
c20c0 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f 20  it.** has to go 
c20d0 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f 75  to disk, and cou
c20e0 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 6b  ld also playback
c20f0 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20   an old journal 
c2100 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a  if necessary..**
c2110 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 20   Since Lookup() 
c2120 6e 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 69  never goes to di
c2130 73 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 73  sk, it never has
c2140 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c 6f   to deal with lo
c2150 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61  cks.** or journa
c2160 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  l files..**.** I
c2170 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 66  f noContent is f
c2180 61 6c 73 65 2c 20 74 68 65 20 70 61 67 65 20 63  alse, the page c
c2190 6f 6e 74 65 6e 74 73 20 61 72 65 20 61 63 74 75  ontents are actu
c21a0 61 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 64  ally read from d
c21b0 69 73 6b 2e 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e  isk..** If noCon
c21c0 74 65 6e 74 20 69 73 20 74 72 75 65 2c 20 69 74  tent is true, it
c21d0 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 64   means that we d
c21e0 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74  o not care about
c21f0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a   the contents.**
c2200 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 20   of the page at 
c2210 74 68 69 73 20 74 69 6d 65 2c 20 73 6f 20 64 6f  this time, so do
c2220 20 6e 6f 74 20 64 6f 20 61 20 64 69 73 6b 20 72   not do a disk r
c2230 65 61 64 2e 20 20 4a 75 73 74 20 66 69 6c 6c 20  ead.  Just fill 
c2240 69 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63  in the.** page c
c2250 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f  ontent with zero
c2260 73 2e 20 20 42 75 74 20 6d 61 72 6b 20 74 68 65  s.  But mark the
c2270 20 66 61 63 74 20 74 68 61 74 20 77 65 20 68 61   fact that we ha
c2280 76 65 20 6e 6f 74 20 72 65 61 64 20 74 68 65 0a  ve not read the.
c2290 2a 2a 20 63 6f 6e 74 65 6e 74 20 62 79 20 73 65  ** content by se
c22a0 74 74 69 6e 67 20 74 68 65 20 50 67 48 64 72 2e  tting the PgHdr.
c22b0 6e 65 65 64 52 65 61 64 20 66 6c 61 67 2e 20 20  needRead flag.  
c22c0 4c 61 74 65 72 20 6f 6e 2c 20 69 66 20 0a 2a 2a  Later on, if .**
c22d0 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
c22e0 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f  te() is called o
c22f0 6e 20 74 68 69 73 20 70 61 67 65 20 6f 72 20 69  n this page or i
c2300 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
c2310 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 67 61 69  s.** called agai
c2320 6e 20 77 69 74 68 20 6e 6f 43 6f 6e 74 65 6e 74  n with noContent
c2330 3d 3d 30 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  ==0, that means 
c2340 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
c2350 20 69 73 20 6e 65 65 64 65 64 0a 2a 2a 20 61 6e   is needed.** an
c2360 64 20 74 68 65 20 64 69 73 6b 20 72 65 61 64 20  d the disk read 
c2370 73 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20  should occur at 
c2380 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73  that point..*/.s
c2390 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 41  tatic int pagerA
c23a0 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20  cquire(.  Pager 
c23b0 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a  *pPager,      /*
c23c0 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20   The pager open 
c23d0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
c23e0 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  file */.  Pgno p
c23f0 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  gno,          /*
c2400 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20   Page number to 
c2410 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67  fetch */.  DbPag
c2420 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f  e **ppPage,    /
c2430 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65  * Write a pointe
c2440 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65  r to the page he
c2450 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f  re */.  int noCo
c2460 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44  ntent       /* D
c2470 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61  o not bother rea
c2480 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f  ding content fro
c2490 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a  m disk if true *
c24a0 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50  /.){.  PgHdr *pP
c24b0 67 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  g;.  int rc;..  
c24c0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
c24d0 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c  state==PAGER_UNL
c24e0 4f 43 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e  OCK || pPager->n
c24f0 52 65 66 3e 30 20 7c 7c 20 70 67 6e 6f 3d 3d 31  Ref>0 || pgno==1
c2500 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61   );..  /* The ma
c2510 78 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65  ximum page numbe
c2520 72 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72  r is 2^31. Retur
c2530 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
c2540 20 69 66 20 61 20 70 61 67 65 0a 20 20 2a 2a 20   if a page.  ** 
c2550 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74  number greater t
c2560 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 7a 65 72  han this, or zer
c2570 6f 2c 20 69 73 20 72 65 71 75 65 73 74 65 64 2e  o, is requested.
c2580 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f  .  */.  if( pgno
c2590 3e 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20  >PAGER_MAX_PGNO 
c25a0 7c 7c 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67  || pgno==0 || pg
c25b0 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  no==PAGER_MJ_PGN
c25c0 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  O(pPager) ){.   
c25d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
c25e0 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
c25f0 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
c2600 20 77 65 20 68 61 76 65 20 6e 6f 74 20 68 69 74   we have not hit
c2610 20 61 6e 79 20 63 72 69 74 69 63 61 6c 20 65 72   any critical er
c2620 72 6f 72 73 2e 0a 20 20 2a 2f 20 0a 20 20 61 73  rors..  */ .  as
c2630 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20  sert( pPager!=0 
c2640 29 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30  );.  *ppPage = 0
c2650 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
c2660 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65  errCode && pPage
c2670 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49  r->errCode!=SQLI
c2680 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72  TE_FULL ){.    r
c2690 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
c26a0 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rCode;.  }..  /*
c26b0 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
c26c0 66 69 72 73 74 20 70 61 67 65 20 61 63 63 65 73  first page acces
c26d0 73 65 64 2c 20 74 68 65 6e 20 67 65 74 20 61 20  sed, then get a 
c26e0 53 48 41 52 45 44 20 6c 6f 63 6b 0a 20 20 2a 2a  SHARED lock.  **
c26f0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
c2700 20 66 69 6c 65 2e 20 70 61 67 65 72 53 68 61 72   file. pagerShar
c2710 65 64 4c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f  edLock() is a no
c2720 2d 6f 70 20 69 66 20 0a 20 20 2a 2a 20 61 20 64  -op if .  ** a d
c2730 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 69 73 20  atabase lock is 
c2740 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a 20 20  already held..  
c2750 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 53  */.  rc = pagerS
c2760 68 61 72 65 64 4c 6f 63 6b 28 70 50 61 67 65 72  haredLock(pPager
c2770 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
c2780 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
c2790 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
c27a0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
c27b0 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate!=PAGER_UNLO
c27c0 43 4b 20 29 3b 0a 0a 20 20 70 50 67 20 3d 20 70  CK );..  pPg = p
c27d0 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67  ager_lookup(pPag
c27e0 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28  er, pgno);.  if(
c27f0 20 70 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 2f   pPg==0 ){.    /
c2800 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 64 20  * The requested 
c2810 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 74  page is not in t
c2820 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 2a  he page cache. *
c2830 2f 0a 20 20 20 20 69 6e 74 20 6e 4d 61 78 3b 0a  /.    int nMax;.
c2840 20 20 20 20 69 6e 74 20 68 3b 0a 20 20 20 20 50      int h;.    P
c2850 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72  AGER_INCR(pPager
c2860 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 20 72 63  ->nMiss);.    rc
c2870 20 3d 20 70 61 67 65 72 41 6c 6c 6f 63 61 74 65   = pagerAllocate
c2880 50 61 67 65 28 70 50 61 67 65 72 2c 20 26 70 50  Page(pPager, &pP
c2890 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  g);.    if( rc!=
c28a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
c28b0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
c28c0 20 20 7d 0a 0a 20 20 20 20 70 50 67 2d 3e 70 67    }..    pPg->pg
c28d0 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 61  no = pgno;.    a
c28e0 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 7c 7c  ssert( !MEMDB ||
c28f0 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 73 74   pgno>pPager->st
c2900 6d 74 53 69 7a 65 20 29 3b 0a 20 20 20 20 69 66  mtSize );.    if
c2910 28 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75  ( pPager->aInJou
c2920 72 6e 61 6c 20 26 26 20 28 69 6e 74 29 70 67 6e  rnal && (int)pgn
c2930 6f 3c 3d 70 50 61 67 65 72 2d 3e 6f 72 69 67 44  o<=pPager->origD
c2940 62 53 69 7a 65 20 29 7b 0a 23 69 66 20 30 0a 20  bSize ){.#if 0. 
c2950 20 20 20 20 20 73 71 6c 69 74 65 33 43 68 65 63       sqlite3Chec
c2960 6b 4d 65 6d 6f 72 79 28 70 50 61 67 65 72 2d 3e  kMemory(pPager->
c2970 61 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e 6f  aInJournal, pgno
c2980 2f 38 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  /8);.#endif.    
c2990 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
c29a0 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b  ->journalOpen );
c29b0 0a 20 20 20 20 20 20 70 50 67 2d 3e 69 6e 4a 6f  .      pPg->inJo
c29c0 75 72 6e 61 6c 20 3d 20 28 70 50 61 67 65 72 2d  urnal = (pPager-
c29d0 3e 61 49 6e 4a 6f 75 72 6e 61 6c 5b 70 67 6e 6f  >aInJournal[pgno
c29e0 2f 38 5d 20 26 20 28 31 3c 3c 28 70 67 6e 6f 26  /8] & (1<<(pgno&
c29f0 37 29 29 29 21 3d 30 3b 0a 20 20 20 20 20 20 70  7)))!=0;.      p
c2a00 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  Pg->needSync = 0
c2a10 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
c2a20 20 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61     pPg->inJourna
c2a30 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 67  l = 0;.      pPg
c2a40 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  ->needSync = 0;.
c2a50 20 20 20 20 7d 0a 0a 20 20 20 20 6d 61 6b 65 43      }..    makeC
c2a60 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 70  lean(pPg);.    p
c2a70 50 67 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  Pg->nRef = 1;.  
c2a80 20 20 52 45 46 49 4e 46 4f 28 70 50 67 29 3b 0a    REFINFO(pPg);.
c2a90 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65  .    pPager->nRe
c2aa0 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 50 61  f++;.    if( pPa
c2ab0 67 65 72 2d 3e 6e 45 78 74 72 61 3e 30 20 29 7b  ger->nExtra>0 ){
c2ac0 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 50 47  .      memset(PG
c2ad0 48 44 52 5f 54 4f 5f 45 58 54 52 41 28 70 50 67  HDR_TO_EXTRA(pPg
c2ae0 2c 20 70 50 61 67 65 72 29 2c 20 30 2c 20 70 50  , pPager), 0, pP
c2af0 61 67 65 72 2d 3e 6e 45 78 74 72 61 29 3b 0a 20  ager->nExtra);. 
c2b00 20 20 20 7d 0a 20 20 20 20 6e 4d 61 78 20 3d 20     }.    nMax = 
c2b10 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
c2b20 63 6f 75 6e 74 28 70 50 61 67 65 72 29 3b 0a 20  count(pPager);. 
c2b30 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65     if( pPager->e
c2b40 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  rrCode ){.      
c2b50 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
c2b60 66 28 70 50 67 29 3b 0a 20 20 20 20 20 20 72 63  f(pPg);.      rc
c2b70 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f   = pPager->errCo
c2b80 64 65 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  de;.      return
c2b90 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   rc;.    }..    
c2ba0 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  /* Populate the 
c2bb0 70 61 67 65 20 77 69 74 68 20 64 61 74 61 2c 20  page with data, 
c2bc0 65 69 74 68 65 72 20 62 79 20 72 65 61 64 69 6e  either by readin
c2bd0 67 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  g from the datab
c2be0 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 2c  ase.    ** file,
c2bf0 20 6f 72 20 62 79 20 73 65 74 74 69 6e 67 20 74   or by setting t
c2c00 68 65 20 65 6e 74 69 72 65 20 70 61 67 65 20 74  he entire page t
c2c10 6f 20 7a 65 72 6f 2e 0a 20 20 20 20 2a 2f 0a 20  o zero..    */. 
c2c20 20 20 20 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74     if( nMax<(int
c2c30 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c  )pgno || MEMDB |
c2c40 7c 20 28 6e 6f 43 6f 6e 74 65 6e 74 20 26 26 20  | (noContent && 
c2c50 21 70 50 61 67 65 72 2d 3e 61 6c 77 61 79 73 52  !pPager->alwaysR
c2c60 6f 6c 6c 62 61 63 6b 29 20 29 7b 0a 20 20 20 20  ollback) ){.    
c2c70 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65    if( pgno>pPage
c2c80 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20  r->mxPgno ){.   
c2c90 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
c2ca0 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20  rUnref(pPg);.   
c2cb0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
c2cc0 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 7d  TE_FULL;.      }
c2cd0 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 50 47  .      memset(PG
c2ce0 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50 67 29  HDR_TO_DATA(pPg)
c2cf0 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  , 0, pPager->pag
c2d00 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70 50  eSize);.      pP
c2d10 67 2d 3e 6e 65 65 64 52 65 61 64 20 3d 20 6e 6f  g->needRead = no
c2d20 43 6f 6e 74 65 6e 74 20 26 26 20 21 70 50 61 67  Content && !pPag
c2d30 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c 62 61  er->alwaysRollba
c2d40 63 6b 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43  ck;.      IOTRAC
c2d50 45 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c 6e  E(("ZERO %p %d\n
c2d60 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  ", pPager, pgno)
c2d70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
c2d80 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62 50      rc = readDbP
c2d90 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 67 2c  age(pPager, pPg,
c2da0 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66   pgno);.      if
c2db0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
c2dc0 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f  && rc!=SQLITE_IO
c2dd0 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
c2de0 7b 0a 20 20 20 20 20 20 20 20 70 50 67 2d 3e 70  {.        pPg->p
c2df0 67 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  gno = 0;.       
c2e00 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
c2e10 65 66 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20  ef(pPg);.       
c2e20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
c2e30 20 20 7d 0a 20 20 20 20 20 20 70 50 67 2d 3e 6e    }.      pPg->n
c2e40 65 65 64 52 65 61 64 20 3d 20 30 3b 0a 20 20 20  eedRead = 0;.   
c2e50 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 69 6e 6b 20   }..    /* Link 
c2e60 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68  the page into th
c2e70 65 20 70 61 67 65 20 68 61 73 68 20 74 61 62 6c  e page hash tabl
c2e80 65 20 2a 2f 0a 20 20 20 20 68 20 3d 20 70 67 6e  e */.    h = pgn
c2e90 6f 20 26 20 28 70 50 61 67 65 72 2d 3e 6e 48 61  o & (pPager->nHa
c2ea0 73 68 2d 31 29 3b 0a 20 20 20 20 61 73 73 65 72  sh-1);.    asser
c2eb0 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 20  t( pgno!=0 );.  
c2ec0 20 20 70 50 67 2d 3e 70 4e 65 78 74 48 61 73 68    pPg->pNextHash
c2ed0 20 3d 20 70 50 61 67 65 72 2d 3e 61 48 61 73 68   = pPager->aHash
c2ee0 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  [h];.    pPager-
c2ef0 3e 61 48 61 73 68 5b 68 5d 20 3d 20 70 50 67 3b  >aHash[h] = pPg;
c2f00 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 4e  .    if( pPg->pN
c2f10 65 78 74 48 61 73 68 20 29 7b 0a 20 20 20 20 20  extHash ){.     
c2f20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 4e   assert( pPg->pN
c2f30 65 78 74 48 61 73 68 2d 3e 70 50 72 65 76 48 61  extHash->pPrevHa
c2f40 73 68 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  sh==0 );.      p
c2f50 50 67 2d 3e 70 4e 65 78 74 48 61 73 68 2d 3e 70  Pg->pNextHash->p
c2f60 50 72 65 76 48 61 73 68 20 3d 20 70 50 67 3b 0a  PrevHash = pPg;.
c2f70 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51      }..#ifdef SQ
c2f80 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
c2f90 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61  .    pPg->pageHa
c2fa0 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68  sh = pager_pageh
c2fb0 61 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66  ash(pPg);.#endif
c2fc0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
c2fd0 20 54 68 65 20 72 65 71 75 65 73 74 65 64 20 70   The requested p
c2fe0 61 67 65 20 69 73 20 69 6e 20 74 68 65 20 70 61  age is in the pa
c2ff0 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20  ge cache. */.   
c3000 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e   assert(pPager->
c3010 6e 52 65 66 3e 30 20 7c 7c 20 70 67 6e 6f 3d 3d  nRef>0 || pgno==
c3020 31 29 3b 0a 20 20 20 20 50 41 47 45 52 5f 49 4e  1);.    PAGER_IN
c3030 43 52 28 70 50 61 67 65 72 2d 3e 6e 48 69 74 29  CR(pPager->nHit)
c3040 3b 0a 20 20 20 20 69 66 28 20 21 6e 6f 43 6f 6e  ;.    if( !noCon
c3050 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63  tent ){.      rc
c3060 20 3d 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e   = pager_get_con
c3070 74 65 6e 74 28 70 50 67 29 3b 0a 20 20 20 20 20  tent(pPg);.     
c3080 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
c3090 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
c30a0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
c30b0 70 61 67 65 5f 72 65 66 28 70 50 67 29 3b 0a 20  page_ref(pPg);. 
c30c0 20 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70   }.  *ppPage = p
c30d0 50 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  Pg;.  return SQL
c30e0 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45  ITE_OK;.}.SQLITE
c30f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
c3100 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
c3110 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  (.  Pager *pPage
c3120 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  r,      /* The p
c3130 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65  ager open on the
c3140 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
c3150 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20  /.  Pgno pgno,  
c3160 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
c3170 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 68 20  number to fetch 
c3180 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a 70 70  */.  DbPage **pp
c3190 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69 74  Page,    /* Writ
c31a0 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
c31b0 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a  he page here */.
c31c0 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20    int noContent 
c31d0 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
c31e0 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 20 63  bother reading c
c31f0 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73 6b  ontent from disk
c3200 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20   if true */.){. 
c3210 20 69 6e 74 20 72 63 3b 0a 20 20 70 61 67 65 72   int rc;.  pager
c3220 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20  Enter(pPager);. 
c3230 20 72 63 20 3d 20 70 61 67 65 72 41 63 71 75 69   rc = pagerAcqui
c3240 72 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c  re(pPager, pgno,
c3250 20 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65   ppPage, noConte
c3260 6e 74 29 3b 0a 20 20 70 61 67 65 72 4c 65 61 76  nt);.  pagerLeav
c3270 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74  e(pPager);.  ret
c3280 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
c3290 2a 20 41 63 71 75 69 72 65 20 61 20 70 61 67 65  * Acquire a page
c32a0 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64   if it is alread
c32b0 79 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  y in the in-memo
c32c0 72 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a  ry cache.  Do.**
c32d0 20 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70 61   not read the pa
c32e0 67 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52  ge from disk.  R
c32f0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
c3300 74 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20  to the page,.** 
c3310 6f 72 20 30 20 69 66 20 74 68 65 20 70 61 67 65  or 0 if the page
c3320 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65   is not in cache
c3330 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
c3340 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
c3350 28 29 2e 20 20 54 68 65 20 64 69 66 66 65 72 65  ().  The differe
c3360 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73  nce between this
c3370 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20   routine.** and 
c3380 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
c3390 29 20 69 73 20 74 68 61 74 20 5f 67 65 74 28 29  ) is that _get()
c33a0 20 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20   will go to the 
c33b0 64 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a  disk and read.**
c33c0 20 69 6e 20 74 68 65 20 70 61 67 65 20 69 66 20   in the page if 
c33d0 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
c33e0 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65  already in cache
c33f0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a  .  This routine.
c3400 2a 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20  ** returns NULL 
c3410 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  if the page is n
c3420 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69  ot in cache or i
c3430 66 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72  f a disk I/O err
c3440 6f 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20  or .** has ever 
c3450 68 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  happened..*/.SQL
c3460 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61  ITE_PRIVATE DbPa
c3470 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  ge *sqlite3Pager
c3480 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50  Lookup(Pager *pP
c3490 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  ager, Pgno pgno)
c34a0 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d  {.  PgHdr *pPg =
c34b0 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   0;..  assert( p
c34c0 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73  Pager!=0 );.  as
c34d0 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b  sert( pgno!=0 );
c34e0 0a 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28 70  ..  pagerEnter(p
c34f0 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50  Pager);.  if( pP
c3500 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
c3510 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20  ER_UNLOCK ){.   
c3520 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
c3530 2d 3e 70 41 6c 6c 20 7c 7c 20 70 50 61 67 65 72  ->pAll || pPager
c3540 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
c3550 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
c3560 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26  Pager->errCode &
c3570 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  & pPager->errCod
c3580 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29  e!=SQLITE_FULL )
c3590 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68  {.    /* Do noth
c35a0 69 6e 67 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69  ing */.  }else i
c35b0 66 28 20 28 70 50 67 20 3d 20 70 61 67 65 72 5f  f( (pPg = pager_
c35c0 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70  lookup(pPager, p
c35d0 67 6e 6f 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  gno))!=0 ){.    
c35e0 70 61 67 65 5f 72 65 66 28 70 50 67 29 3b 0a 20  page_ref(pPg);. 
c35f0 20 7d 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28   }.  pagerLeave(
c3600 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72  pPager);.  retur
c3610 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n pPg;.}../*.** 
c3620 52 65 6c 65 61 73 65 20 61 20 70 61 67 65 2e 0a  Release a page..
c3630 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d  **.** If the num
c3640 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
c3650 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72  s to the page dr
c3660 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e  op to zero, then
c3670 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20   the.** page is 
c3680 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55  added to the LRU
c3690 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c   list.  When all
c36a0 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61   references to a
c36b0 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20  ll pages.** are 
c36c0 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c  released, a roll
c36d0 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20  back occurs and 
c36e0 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  the lock on the 
c36f0 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72  database is.** r
c3700 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  emoved..*/.SQLIT
c3710 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
c3720 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
c3730 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 0a 20  DbPage *pPg){.. 
c3740 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68   /* Decrement th
c3750 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
c3760 74 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 0a  t for this page.
c3770 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
c3780 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20  Pg->nRef>0 );.  
c3790 70 61 67 65 72 45 6e 74 65 72 28 70 50 67 2d 3e  pagerEnter(pPg->
c37a0 70 50 61 67 65 72 29 3b 0a 20 20 70 50 67 2d 3e  pPager);.  pPg->
c37b0 6e 52 65 66 2d 2d 3b 0a 20 20 52 45 46 49 4e 46  nRef--;.  REFINF
c37c0 4f 28 70 50 67 29 3b 0a 0a 20 20 43 48 45 43 4b  O(pPg);..  CHECK
c37d0 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f  _PAGE(pPg);..  /
c37e0 2a 20 57 68 65 6e 20 74 68 65 20 6e 75 6d 62 65  * When the numbe
c37f0 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
c3800 74 6f 20 61 20 70 61 67 65 20 72 65 61 63 68 20  to a page reach 
c3810 30 2c 20 63 61 6c 6c 20 74 68 65 0a 20 20 2a 2a  0, call the.  **
c3820 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20   destructor and 
c3830 61 64 64 20 74 68 65 20 70 61 67 65 20 74 6f 20  add the page to 
c3840 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 20 20  the freelist..  
c3850 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d 3e 6e 52  */.  if( pPg->nR
c3860 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61 67  ef==0 ){.    Pag
c3870 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
c3880 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 20 20 6c  ->pPager;..    l
c3890 72 75 4c 69 73 74 41 64 64 28 70 50 67 29 3b 0a  ruListAdd(pPg);.
c38a0 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
c38b0 78 44 65 73 74 72 75 63 74 6f 72 20 29 7b 0a 20  xDestructor ){. 
c38c0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 78 44 65       pPager->xDe
c38d0 73 74 72 75 63 74 6f 72 28 70 50 67 2c 20 70 50  structor(pPg, pP
c38e0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
c38f0 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
c3900 20 57 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20   When all pages 
c3910 72 65 61 63 68 20 74 68 65 20 66 72 65 65 6c 69  reach the freeli
c3920 73 74 2c 20 64 72 6f 70 20 74 68 65 20 72 65 61  st, drop the rea
c3930 64 20 6c 6f 63 6b 20 66 72 6f 6d 0a 20 20 20 20  d lock from.    
c3940 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
c3950 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  file..    */.   
c3960 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 2d 2d 3b   pPager->nRef--;
c3970 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
c3980 67 65 72 2d 3e 6e 52 65 66 3e 3d 30 20 29 3b 0a  ger->nRef>=0 );.
c3990 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
c39a0 6e 52 65 66 3d 3d 30 20 26 26 20 28 21 70 50 61  nRef==0 && (!pPa
c39b0 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
c39c0 64 65 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f  de || pPager->jo
c39d0 75 72 6e 61 6c 4f 66 66 3e 30 29 20 29 7b 0a 20  urnalOff>0) ){. 
c39e0 20 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b       pagerUnlock
c39f0 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67  AndRollback(pPag
c3a00 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  er);.    }.  }. 
c3a10 20 70 61 67 65 72 4c 65 61 76 65 28 70 50 67 2d   pagerLeave(pPg-
c3a20 3e 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75  >pPager);.  retu
c3a30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
c3a40 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
c3a50 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
c3a60 20 70 50 61 67 65 72 2e 20 20 54 68 65 72 65 20   pPager.  There 
c3a70 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62  should already b
c3a80 65 20 61 20 52 45 53 45 52 56 45 44 0a 2a 2a 20  e a RESERVED.** 
c3a90 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  or EXCLUSIVE loc
c3aa0 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
c3ab0 65 20 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73  e file when this
c3ac0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
c3ad0 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ed..**.** Return
c3ae0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76   SQLITE_OK if ev
c3af0 65 72 79 74 68 69 6e 67 2e 20 20 52 65 74 75 72  erything.  Retur
c3b00 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  n an error code 
c3b10 61 6e 64 20 72 65 6c 65 61 73 65 20 74 68 65 0a  and release the.
c3b20 2a 2a 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66  ** write lock if
c3b30 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77   anything goes w
c3b40 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rong..*/.static 
c3b50 69 6e 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a  int pager_open_j
c3b60 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50  ournal(Pager *pP
c3b70 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  ager){.  sqlite3
c3b80 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61  _vfs *pVfs = pPa
c3b90 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74  ger->pVfs;.  int
c3ba0 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45   flags = (SQLITE
c3bb0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c  _OPEN_READWRITE|
c3bc0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
c3bd0 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45  USIVE|SQLITE_OPE
c3be0 4e 5f 43 52 45 41 54 45 29 3b 0a 0a 20 20 69 6e  N_CREATE);..  in
c3bf0 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
c3c00 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65  !MEMDB );.  asse
c3c10 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
c3c20 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45  e>=PAGER_RESERVE
c3c30 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  D );.  assert( p
c3c40 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
c3c50 65 6e 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  en==0 );.  asser
c3c60 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  t( pPager->useJo
c3c70 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72  urnal );.  asser
c3c80 74 28 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f  t( pPager->aInJo
c3c90 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 73 71  urnal==0 );.  sq
c3ca0 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
c3cb0 75 6e 74 28 70 50 61 67 65 72 29 3b 0a 20 20 70  unt(pPager);.  p
c3cc0 61 67 65 72 4c 65 61 76 65 28 70 50 61 67 65 72  agerLeave(pPager
c3cd0 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 61 49 6e  );.  pPager->aIn
c3ce0 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  Journal = sqlite
c3cf0 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 70 50 61  3MallocZero( pPa
c3d00 67 65 72 2d 3e 64 62 53 69 7a 65 2f 38 20 2b 20  ger->dbSize/8 + 
c3d10 31 20 29 3b 0a 20 20 70 61 67 65 72 45 6e 74 65  1 );.  pagerEnte
c3d20 72 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28  r(pPager);.  if(
c3d30 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72   pPager->aInJour
c3d40 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  nal==0 ){.    rc
c3d50 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
c3d60 0a 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64  .    goto failed
c3d70 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  _to_open_journal
c3d80 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61  ;.  }..  if( pPa
c3d90 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
c3da0 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 53  .    flags |= (S
c3db0 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
c3dc0 45 4f 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f  EONCLOSE|SQLITE_
c3dd0 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41  OPEN_TEMP_JOURNA
c3de0 4c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  L);.  }else{.   
c3df0 20 66 6c 61 67 73 20 7c 3d 20 28 53 51 4c 49 54   flags |= (SQLIT
c3e00 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
c3e10 4e 41 4c 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66  NAL);.  }.#ifdef
c3e20 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
c3e30 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 72 63  TOMIC_WRITE.  rc
c3e40 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61   = sqlite3Journa
c3e50 6c 4f 70 65 6e 28 0a 20 20 20 20 20 20 70 56 66  lOpen(.      pVf
c3e60 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
c3e70 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
c3e80 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66  , flags, jrnlBuf
c3e90 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a  ferSize(pPager).
c3ea0 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 72 63 20    );.#else.  rc 
c3eb0 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  = sqlite3OsOpen(
c3ec0 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a  pVfs, pPager->zJ
c3ed0 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e  ournal, pPager->
c3ee0 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a  jfd, flags, 0);.
c3ef0 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28  #endif.  assert(
c3f00 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
c3f10 7c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d 3e 70  | pPager->jfd->p
c3f20 4d 65 74 68 6f 64 73 20 29 3b 0a 20 20 70 50 61  Methods );.  pPa
c3f30 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
c3f40 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73  = 0;.  pPager->s
c3f50 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  etMaster = 0;.  
c3f60 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
c3f70 64 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63  dr = 0;.  if( rc
c3f80 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
c3f90 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
c3fa0 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  E_NOMEM ){.     
c3fb0 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
c3fc0 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
c3fd0 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20  Journal, 0);.   
c3fe0 20 7d 0a 20 20 20 20 67 6f 74 6f 20 66 61 69 6c   }.    goto fail
c3ff0 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  ed_to_open_journ
c4000 61 6c 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72  al;.  }.  pPager
c4010 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d 20  ->journalOpen = 
c4020 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  1;.  pPager->jou
c4030 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b  rnalStarted = 0;
c4040 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  .  pPager->needS
c4050 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  ync = 0;.  pPage
c4060 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63  r->alwaysRollbac
c4070 6b 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  k = 0;.  pPager-
c4080 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 69 66 28  >nRec = 0;.  if(
c4090 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
c40a0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 50 61   ){.    rc = pPa
c40b0 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20  ger->errCode;.  
c40c0 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f 74 6f    goto failed_to
c40d0 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a 20  _open_journal;. 
c40e0 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6f 72 69   }.  pPager->ori
c40f0 67 44 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72  gDbSize = pPager
c4100 2d 3e 64 62 53 69 7a 65 3b 0a 0a 20 20 72 63 20  ->dbSize;..  rc 
c4110 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64  = writeJournalHd
c4120 72 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 69 66  r(pPager);..  if
c4130 28 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75  ( pPager->stmtAu
c4140 74 6f 6f 70 65 6e 20 26 26 20 72 63 3d 3d 53 51  toopen && rc==SQ
c4150 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
c4160 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
c4170 53 74 6d 74 42 65 67 69 6e 28 70 50 61 67 65 72  StmtBegin(pPager
c4180 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21  );.  }.  if( rc!
c4190 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63  =SQLITE_OK && rc
c41a0 21 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  !=SQLITE_NOMEM )
c41b0 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72  {.    rc = pager
c41c0 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
c41d0 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66  (pPager);.    if
c41e0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
c41f0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
c4200 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 7d  LITE_FULL;.    }
c4210 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
c4220 3b 0a 0a 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65  ;..failed_to_ope
c4230 6e 5f 6a 6f 75 72 6e 61 6c 3a 0a 20 20 73 71 6c  n_journal:.  sql
c4240 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72  ite3_free(pPager
c4250 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20  ->aInJournal);. 
c4260 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72   pPager->aInJour
c4270 6e 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  nal = 0;.  retur
c4280 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
c4290 63 71 75 69 72 65 20 61 20 77 72 69 74 65 2d 6c  cquire a write-l
c42a0 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
c42b0 61 73 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20 69  ase.  The lock i
c42c0 73 20 72 65 6d 6f 76 65 64 20 77 68 65 6e 0a 2a  s removed when.*
c42d0 2a 20 74 68 65 20 61 6e 79 20 6f 66 20 74 68 65  * the any of the
c42e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 68 61 70 70 65   following happe
c42f0 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 73 71  n:.**.**   *  sq
c4300 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
c4310 50 68 61 73 65 54 77 6f 28 29 20 69 73 20 63 61  PhaseTwo() is ca
c4320 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71  lled..**   *  sq
c4330 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
c4340 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a  ck() is called..
c4350 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50  **   *  sqlite3P
c4360 61 67 65 72 43 6c 6f 73 65 28 29 20 69 73 20 63  agerClose() is c
c4370 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73  alled..**   *  s
c4380 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
c4390 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  () is called to 
c43a0 6f 6e 20 65 76 65 72 79 20 6f 75 74 73 74 61 6e  on every outstan
c43b0 64 69 6e 67 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  ding page..**.**
c43c0 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
c43d0 65 74 65 72 20 74 6f 20 74 68 69 73 20 72 6f 75  eter to this rou
c43e0 74 69 6e 65 20 69 73 20 61 20 70 6f 69 6e 74 65  tine is a pointe
c43f0 72 20 74 6f 20 61 6e 79 20 6f 70 65 6e 20 70 61  r to any open pa
c4400 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74  ge of the.** dat
c4410 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f 74  abase file.  Not
c4420 68 69 6e 67 20 63 68 61 6e 67 65 73 20 61 62 6f  hing changes abo
c4430 75 74 20 74 68 65 20 70 61 67 65 20 2d 20 69 74  ut the page - it
c4440 20 69 73 20 75 73 65 64 20 6d 65 72 65 6c 79 20   is used merely 
c4450 74 6f 0a 2a 2a 20 61 63 71 75 69 72 65 20 61 20  to.** acquire a 
c4460 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50  pointer to the P
c4470 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 61  ager structure a
c4480 6e 64 20 61 73 20 70 72 6f 6f 66 20 74 68 61 74  nd as proof that
c4490 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6c 72   there is.** alr
c44a0 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b  eady a read-lock
c44b0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
c44c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ..**.** The seco
c44d0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64  nd parameter ind
c44e0 69 63 61 74 65 73 20 68 6f 77 20 6d 75 63 68 20  icates how much 
c44f0 73 70 61 63 65 20 69 6e 20 62 79 74 65 73 20 74  space in bytes t
c4500 6f 20 72 65 73 65 72 76 65 20 66 6f 72 20 61 0a  o reserve for a.
c4510 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  ** master journa
c4520 6c 20 66 69 6c 65 2d 6e 61 6d 65 20 61 74 20 74  l file-name at t
c4530 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
c4540 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 20 69 74 20  journal when it 
c4550 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a  is created..**.*
c4560 2a 20 41 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  * A journal file
c4570 20 69 73 20 6f 70 65 6e 65 64 20 69 66 20 74 68   is opened if th
c4580 69 73 20 69 73 20 6e 6f 74 20 61 20 74 65 6d 70  is is not a temp
c4590 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 46 6f 72  orary file.  For
c45a0 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 66 69   temporary.** fi
c45b0 6c 65 73 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67  les, the opening
c45c0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
c45d0 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 65 64  file is deferred
c45e0 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 73 20   until there is 
c45f0 61 6e 0a 2a 2a 20 61 63 74 75 61 6c 20 6e 65 65  an.** actual nee
c4600 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68  d to write to th
c4610 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  e journal..**.**
c4620 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
c4630 20 69 73 20 61 6c 72 65 61 64 79 20 72 65 73 65   is already rese
c4640 72 76 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67  rved for writing
c4650 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
c4660 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
c4670 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 72   If exFlag is tr
c4680 75 65 2c 20 67 6f 20 61 68 65 61 64 20 61 6e 64  ue, go ahead and
c4690 20 67 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56   get an EXCLUSIV
c46a0 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69  E lock on the fi
c46b0 6c 65 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  le.** immediatel
c46c0 79 20 69 6e 73 74 65 61 64 20 6f 66 20 77 61 69  y instead of wai
c46d0 74 69 6e 67 20 75 6e 74 69 6c 20 77 65 20 74 72  ting until we tr
c46e0 79 20 74 6f 20 66 6c 75 73 68 20 74 68 65 20 63  y to flush the c
c46f0 61 63 68 65 2e 20 20 54 68 65 0a 2a 2a 20 65 78  ache.  The.** ex
c4700 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 20  Flag is ignored 
c4710 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  if a transaction
c4720 20 69 73 20 61 6c 72 65 61 64 79 20 61 63 74 69   is already acti
c4730 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
c4740 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
c4750 33 50 61 67 65 72 42 65 67 69 6e 28 44 62 50 61  3PagerBegin(DbPa
c4760 67 65 20 2a 70 50 67 2c 20 69 6e 74 20 65 78 46  ge *pPg, int exF
c4770 6c 61 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  lag){.  Pager *p
c4780 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
c4790 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ger;.  int rc = 
c47a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 61 67  SQLITE_OK;.  pag
c47b0 65 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b  erEnter(pPager);
c47c0 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  .  assert( pPg->
c47d0 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 73 73 65  nRef>0 );.  asse
c47e0 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
c47f0 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  e!=PAGER_UNLOCK 
c4800 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
c4810 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48  >state==PAGER_SH
c4820 41 52 45 44 20 29 7b 0a 20 20 20 20 61 73 73 65  ARED ){.    asse
c4830 72 74 28 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a  rt( pPager->aInJ
c4840 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20  ournal==0 );.   
c4850 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20   if( MEMDB ){.  
c4860 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
c4870 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53  e = PAGER_EXCLUS
c4880 49 56 45 3b 0a 20 20 20 20 20 20 70 50 61 67 65  IVE;.      pPage
c4890 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 3d 20  r->origDbSize = 
c48a0 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a  pPager->dbSize;.
c48b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
c48c0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c   rc = sqlite3OsL
c48d0 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ock(pPager->fd, 
c48e0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a  RESERVED_LOCK);.
c48f0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
c4900 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
c4910 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65     pPager->state
c4920 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56 45   = PAGER_RESERVE
c4930 44 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65  D;.        if( e
c4940 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20  xFlag ){.       
c4950 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61     rc = pager_wa
c4960 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65  it_on_lock(pPage
c4970 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  r, EXCLUSIVE_LOC
c4980 4b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  K);.        }.  
c4990 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
c49a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
c49b0 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 4c 65  .        pagerLe
c49c0 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20  ave(pPager);.   
c49d0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
c49e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50        }.      pP
c49f0 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65  ager->dirtyCache
c4a00 20 3d 20 30 3b 0a 20 20 20 20 20 20 50 41 47 45   = 0;.      PAGE
c4a10 52 54 52 41 43 45 32 28 22 54 52 41 4e 53 41 43  RTRACE2("TRANSAC
c4a20 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50 41 47 45  TION %d\n", PAGE
c4a30 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20  RID(pPager));.  
c4a40 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
c4a50 75 73 65 4a 6f 75 72 6e 61 6c 20 26 26 20 21 70  useJournal && !p
c4a60 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
c4a70 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
c4a80 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  pager_open_journ
c4a90 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
c4aa0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
c4ab0 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f  e if( pPager->jo
c4ac0 75 72 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50 61  urnalOpen && pPa
c4ad0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
c4ae0 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  =0 ){.    /* Thi
c4af0 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74  s happens when t
c4b00 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e 20  he pager was in 
c4b10 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73  exclusive-access
c4b20 20 6d 6f 64 65 20 6c 61 73 74 0a 20 20 20 20 2a   mode last.    *
c4b30 2a 20 74 69 6d 65 20 61 20 28 72 65 61 64 20 6f  * time a (read o
c4b40 72 20 77 72 69 74 65 29 20 74 72 61 6e 73 61 63  r write) transac
c4b50 74 69 6f 6e 20 77 61 73 20 73 75 63 63 65 73 73  tion was success
c4b60 66 75 6c 6c 79 20 63 6f 6e 63 6c 75 64 65 64 0a  fully concluded.
c4b70 20 20 20 20 2a 2a 20 62 79 20 74 68 69 73 20 63      ** by this c
c4b80 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 6e 73 74 65  onnection. Inste
c4b90 61 64 20 6f 66 20 64 65 6c 65 74 69 6e 67 20 74  ad of deleting t
c4ba0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
c4bb0 69 74 20 77 61 73 20 0a 20 20 20 20 2a 2a 20 6b  it was .    ** k
c4bc0 65 70 74 20 6f 70 65 6e 20 61 6e 64 20 74 72 75  ept open and tru
c4bd0 6e 63 61 74 65 64 20 74 6f 20 30 20 62 79 74 65  ncated to 0 byte
c4be0 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  s..    */.    as
c4bf0 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52  sert( pPager->nR
c4c00 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  ec==0 );.    ass
c4c10 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6f 72 69  ert( pPager->ori
c4c20 67 44 62 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20  gDbSize==0 );.  
c4c30 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
c4c40 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->aInJournal==0 
c4c50 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
c4c60 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61  gerPagecount(pPa
c4c70 67 65 72 29 3b 0a 20 20 20 20 70 61 67 65 72 4c  ger);.    pagerL
c4c80 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20  eave(pPager);.  
c4c90 20 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75    pPager->aInJou
c4ca0 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61  rnal = sqlite3Ma
c4cb0 6c 6c 6f 63 5a 65 72 6f 28 20 70 50 61 67 65 72  llocZero( pPager
c4cc0 2d 3e 64 62 53 69 7a 65 2f 38 20 2b 20 31 20 29  ->dbSize/8 + 1 )
c4cd0 3b 0a 20 20 20 20 70 61 67 65 72 45 6e 74 65 72  ;.    pagerEnter
c4ce0 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66  (pPager);.    if
c4cf0 28 20 21 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f  ( !pPager->aInJo
c4d00 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72  urnal ){.      r
c4d10 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
c4d20 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
c4d30 20 20 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44     pPager->origD
c4d40 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e  bSize = pPager->
c4d50 64 62 53 69 7a 65 3b 0a 20 20 20 20 20 20 72 63  dbSize;.      rc
c4d60 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48   = writeJournalH
c4d70 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  dr(pPager);.    
c4d80 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
c4d90 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  !pPager->journal
c4da0 4f 70 65 6e 20 7c 7c 20 70 50 61 67 65 72 2d 3e  Open || pPager->
c4db0 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20  journalOff>0 || 
c4dc0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
c4dd0 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28 70 50  .  pagerLeave(pP
c4de0 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  ager);.  return 
c4df0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  rc;.}../*.** Mak
c4e00 65 20 61 20 70 61 67 65 20 64 69 72 74 79 2e 20  e a page dirty. 
c4e10 20 53 65 74 20 69 74 73 20 64 69 72 74 79 20 66   Set its dirty f
c4e20 6c 61 67 20 61 6e 64 20 61 64 64 20 69 74 20 74  lag and add it t
c4e30 6f 20 74 68 65 20 64 69 72 74 79 0a 2a 2a 20 70  o the dirty.** p
c4e40 61 67 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61  age list..*/.sta
c4e50 74 69 63 20 76 6f 69 64 20 6d 61 6b 65 44 69 72  tic void makeDir
c4e60 74 79 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  ty(PgHdr *pPg){.
c4e70 20 20 69 66 28 20 70 50 67 2d 3e 64 69 72 74 79    if( pPg->dirty
c4e80 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61 67 65 72  ==0 ){.    Pager
c4e90 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
c4ea0 70 50 61 67 65 72 3b 0a 20 20 20 20 70 50 67 2d  pPager;.    pPg-
c4eb0 3e 64 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20  >dirty = 1;.    
c4ec0 70 50 67 2d 3e 70 44 69 72 74 79 20 3d 20 70 50  pPg->pDirty = pP
c4ed0 61 67 65 72 2d 3e 70 44 69 72 74 79 3b 0a 20 20  ager->pDirty;.  
c4ee0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 44    if( pPager->pD
c4ef0 69 72 74 79 20 29 7b 0a 20 20 20 20 20 20 70 50  irty ){.      pP
c4f00 61 67 65 72 2d 3e 70 44 69 72 74 79 2d 3e 70 50  ager->pDirty->pP
c4f10 72 65 76 44 69 72 74 79 20 3d 20 70 50 67 3b 0a  revDirty = pPg;.
c4f20 20 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 70      }.    pPg->p
c4f30 50 72 65 76 44 69 72 74 79 20 3d 20 30 3b 0a 20  PrevDirty = 0;. 
c4f40 20 20 20 70 50 61 67 65 72 2d 3e 70 44 69 72 74     pPager->pDirt
c4f50 79 20 3d 20 70 50 67 3b 0a 20 20 7d 0a 7d 0a 0a  y = pPg;.  }.}..
c4f60 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 70 61 67  /*.** Make a pag
c4f70 65 20 63 6c 65 61 6e 2e 20 20 43 6c 65 61 72 20  e clean.  Clear 
c4f80 69 74 73 20 64 69 72 74 79 20 62 69 74 20 61 6e  its dirty bit an
c4f90 64 20 72 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d  d remove it from
c4fa0 20 74 68 65 0a 2a 2a 20 64 69 72 74 79 20 70 61   the.** dirty pa
c4fb0 67 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74  ge list..*/.stat
c4fc0 69 63 20 76 6f 69 64 20 6d 61 6b 65 43 6c 65 61  ic void makeClea
c4fd0 6e 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  n(PgHdr *pPg){. 
c4fe0 20 69 66 28 20 70 50 67 2d 3e 64 69 72 74 79 20   if( pPg->dirty 
c4ff0 29 7b 0a 20 20 20 20 70 50 67 2d 3e 64 69 72 74  ){.    pPg->dirt
c5000 79 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70  y = 0;.    if( p
c5010 50 67 2d 3e 70 44 69 72 74 79 20 29 7b 0a 20 20  Pg->pDirty ){.  
c5020 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d      assert( pPg-
c5030 3e 70 44 69 72 74 79 2d 3e 70 50 72 65 76 44 69  >pDirty->pPrevDi
c5040 72 74 79 3d 3d 70 50 67 20 29 3b 0a 20 20 20 20  rty==pPg );.    
c5050 20 20 70 50 67 2d 3e 70 44 69 72 74 79 2d 3e 70    pPg->pDirty->p
c5060 50 72 65 76 44 69 72 74 79 20 3d 20 70 50 67 2d  PrevDirty = pPg-
c5070 3e 70 50 72 65 76 44 69 72 74 79 3b 0a 20 20 20  >pPrevDirty;.   
c5080 20 7d 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e   }.    if( pPg->
c5090 70 50 72 65 76 44 69 72 74 79 20 29 7b 0a 20 20  pPrevDirty ){.  
c50a0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d      assert( pPg-
c50b0 3e 70 50 72 65 76 44 69 72 74 79 2d 3e 70 44 69  >pPrevDirty->pDi
c50c0 72 74 79 3d 3d 70 50 67 20 29 3b 0a 20 20 20 20  rty==pPg );.    
c50d0 20 20 70 50 67 2d 3e 70 50 72 65 76 44 69 72 74    pPg->pPrevDirt
c50e0 79 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 67 2d  y->pDirty = pPg-
c50f0 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c  >pDirty;.    }el
c5100 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
c5110 28 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 70  ( pPg->pPager->p
c5120 44 69 72 74 79 3d 3d 70 50 67 20 29 3b 0a 20 20  Dirty==pPg );.  
c5130 20 20 20 20 70 50 67 2d 3e 70 50 61 67 65 72 2d      pPg->pPager-
c5140 3e 70 44 69 72 74 79 20 3d 20 70 50 67 2d 3e 70  >pDirty = pPg->p
c5150 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d  Dirty;.    }.  }
c5160 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20  .}.../*.** Mark 
c5170 61 20 64 61 74 61 20 70 61 67 65 20 61 73 20 77  a data page as w
c5180 72 69 74 65 61 62 6c 65 2e 20 20 54 68 65 20 70  riteable.  The p
c5190 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  age is written i
c51a0 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
c51b0 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 6e 6f 74  .** if it is not
c51c0 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e 20   there already. 
c51d0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75   This routine mu
c51e0 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66  st be called bef
c51f0 6f 72 65 20 6d 61 6b 69 6e 67 0a 2a 2a 20 63 68  ore making.** ch
c5200 61 6e 67 65 73 20 74 6f 20 61 20 70 61 67 65 2e  anges to a page.
c5210 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
c5220 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69   time this routi
c5230 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  ne is called, th
c5240 65 20 70 61 67 65 72 20 63 72 65 61 74 65 73 20  e pager creates 
c5250 61 20 6e 65 77 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  a new.** journal
c5260 20 61 6e 64 20 61 63 71 75 69 72 65 73 20 61 20   and acquires a 
c5270 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e  RESERVED lock on
c5280 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
c5290 49 66 20 74 68 65 20 52 45 53 45 52 56 45 44 0a  If the RESERVED.
c52a0 2a 2a 20 6c 6f 63 6b 20 63 6f 75 6c 64 20 6e 6f  ** lock could no
c52b0 74 20 62 65 20 61 63 71 75 69 72 65 64 2c 20 74  t be acquired, t
c52c0 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
c52d0 72 6e 73 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  rns SQLITE_BUSY.
c52e0 20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67    The.** calling
c52f0 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 63 68   routine must ch
c5300 65 63 6b 20 66 6f 72 20 74 68 61 74 20 72 65 74  eck for that ret
c5310 75 72 6e 20 76 61 6c 75 65 20 61 6e 64 20 62 65  urn value and be
c5320 20 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f 0a   careful not to.
c5330 2a 2a 20 63 68 61 6e 67 65 20 61 6e 79 20 70 61  ** change any pa
c5340 67 65 20 64 61 74 61 20 75 6e 74 69 6c 20 74 68  ge data until th
c5350 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
c5360 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  ns SQLITE_OK..**
c5370 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e  .** If the journ
c5380 61 6c 20 66 69 6c 65 20 63 6f 75 6c 64 20 6e 6f  al file could no
c5390 74 20 62 65 20 77 72 69 74 74 65 6e 20 62 65 63  t be written bec
c53a0 61 75 73 65 20 74 68 65 20 64 69 73 6b 20 69 73  ause the disk is
c53b0 20 66 75 6c 6c 2c 0a 2a 2a 20 74 68 65 6e 20 74   full,.** then t
c53c0 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
c53d0 72 6e 73 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  rns SQLITE_FULL 
c53e0 61 6e 64 20 64 6f 65 73 20 61 6e 20 69 6d 6d 65  and does an imme
c53f0 64 69 61 74 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a  diate rollback..
c5400 2a 2a 20 41 6c 6c 20 73 75 62 73 65 71 75 65 6e  ** All subsequen
c5410 74 20 77 72 69 74 65 20 61 74 74 65 6d 70 74 73  t write attempts
c5420 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 53 51 4c   also return SQL
c5430 49 54 45 5f 46 55 4c 4c 20 75 6e 74 69 6c 20 74  ITE_FULL until t
c5440 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 63 61 6c  here.** is a cal
c5450 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  l to sqlite3Page
c5460 72 43 6f 6d 6d 69 74 28 29 20 6f 72 20 73 71 6c  rCommit() or sql
c5470 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
c5480 6b 28 29 20 74 6f 0a 2a 2a 20 72 65 73 65 74 2e  k() to.** reset.
c5490 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
c54a0 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 64 72  ager_write(PgHdr
c54b0 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 20 2a   *pPg){.  void *
c54c0 70 44 61 74 61 20 3d 20 50 47 48 44 52 5f 54 4f  pData = PGHDR_TO
c54d0 5f 44 41 54 41 28 70 50 67 29 3b 0a 20 20 50 61  _DATA(pPg);.  Pa
c54e0 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
c54f0 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74  g->pPager;.  int
c5500 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
c5510 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
c5520 20 65 72 72 6f 72 73 0a 20 20 2a 2f 0a 20 20 69   errors.  */.  i
c5530 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
c5540 64 65 20 29 7b 20 0a 20 20 20 20 72 65 74 75 72  de ){ .    retur
c5550 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  n pPager->errCod
c5560 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  e;.  }.  if( pPa
c5570 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b  ger->readOnly ){
c5580 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
c5590 54 45 5f 50 45 52 4d 3b 0a 20 20 7d 0a 0a 20 20  TE_PERM;.  }..  
c55a0 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d  assert( !pPager-
c55b0 3e 73 65 74 4d 61 73 74 65 72 20 29 3b 0a 0a 20  >setMaster );.. 
c55c0 20 43 48 45 43 4b 5f 50 41 47 45 28 70 50 67 29   CHECK_PAGE(pPg)
c55d0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
c55e0 70 61 67 65 20 77 61 73 20 70 72 65 76 69 6f 75  page was previou
c55f0 73 6c 79 20 61 63 71 75 69 72 65 64 20 77 69 74  sly acquired wit
c5600 68 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20  h noContent==1, 
c5610 74 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20  that means.  ** 
c5620 77 65 20 64 69 64 6e 27 74 20 72 65 61 6c 6c 79  we didn't really
c5630 20 72 65 61 64 20 69 6e 20 74 68 65 20 63 6f 6e   read in the con
c5640 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65  tent of the page
c5650 2e 20 20 54 68 69 73 20 63 61 6e 20 68 61 70 70  .  This can happ
c5660 65 6e 0a 20 20 2a 2a 20 28 66 6f 72 20 65 78 61  en.  ** (for exa
c5670 6d 70 6c 65 29 20 77 68 65 6e 20 74 68 65 20 70  mple) when the p
c5680 61 67 65 20 69 73 20 62 65 69 6e 67 20 6d 6f 76  age is being mov
c5690 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ed to the freeli
c56a0 73 74 2e 20 20 42 75 74 0a 20 20 2a 2a 20 6e 6f  st.  But.  ** no
c56b0 77 20 77 65 20 61 72 65 20 28 70 65 72 68 61 70  w we are (perhap
c56c0 73 29 20 6d 6f 76 69 6e 67 20 74 68 65 20 70 61  s) moving the pa
c56d0 67 65 20 6f 66 66 20 6f 66 20 74 68 65 20 66 72  ge off of the fr
c56e0 65 65 6c 69 73 74 20 66 6f 72 0a 20 20 2a 2a 20  eelist for.  ** 
c56f0 72 65 75 73 65 20 61 6e 64 20 77 65 20 6e 65 65  reuse and we nee
c5700 64 20 74 6f 20 6b 6e 6f 77 20 69 74 73 20 6f 72  d to know its or
c5710 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 73  iginal content s
c5720 6f 20 74 68 61 74 20 63 6f 6e 74 65 6e 74 0a 20  o that content. 
c5730 20 2a 2a 20 63 61 6e 20 62 65 20 73 74 6f 72 65   ** can be store
c5740 64 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  d in the rollbac
c5750 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 53 6f 20 64  k journal.  So d
c5760 6f 20 74 68 65 20 72 65 61 64 20 61 74 20 74 68  o the read at th
c5770 69 73 0a 20 20 2a 2a 20 74 69 6d 65 2e 0a 20 20  is.  ** time..  
c5780 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f  */.  rc = pager_
c5790 67 65 74 5f 63 6f 6e 74 65 6e 74 28 70 50 67 29  get_content(pPg)
c57a0 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
c57b0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
c57c0 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20  ..  /* Mark the 
c57d0 70 61 67 65 20 61 73 20 64 69 72 74 79 2e 20 20  page as dirty.  
c57e0 49 66 20 74 68 65 20 70 61 67 65 20 68 61 73 20  If the page has 
c57f0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 69  already been wri
c5800 74 74 65 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65  tten.  ** to the
c5810 20 6a 6f 75 72 6e 61 6c 20 74 68 65 6e 20 77 65   journal then we
c5820 20 63 61 6e 20 72 65 74 75 72 6e 20 72 69 67 68   can return righ
c5830 74 20 61 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 6d  t away..  */.  m
c5840 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20  akeDirty(pPg);. 
c5850 20 69 66 28 20 70 50 67 2d 3e 69 6e 4a 6f 75 72   if( pPg->inJour
c5860 6e 61 6c 20 26 26 20 28 70 61 67 65 49 6e 53 74  nal && (pageInSt
c5870 61 74 65 6d 65 6e 74 28 70 50 67 29 20 7c 7c 20  atement(pPg) || 
c5880 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73  pPager->stmtInUs
c5890 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 70 50 61  e==0) ){.    pPa
c58a0 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20  ger->dirtyCache 
c58b0 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20  = 1;.  }else{.. 
c58c0 20 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 20     /* If we get 
c58d0 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 61  this far, it mea
c58e0 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ns that the page
c58f0 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20   needs to be.   
c5900 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74   ** written to t
c5910 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a  he transaction j
c5920 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b  ournal or the ck
c5930 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c  eckpoint journal
c5940 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e  .    ** or both.
c5950 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
c5960 69 72 73 74 20 63 68 65 63 6b 20 74 6f 20 73 65  irst check to se
c5970 65 20 74 68 61 74 20 74 68 65 20 74 72 61 6e 73  e that the trans
c5980 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 65  action journal e
c5990 78 69 73 74 73 20 61 6e 64 0a 20 20 20 20 2a 2a  xists and.    **
c59a0 20 63 72 65 61 74 65 20 69 74 20 69 66 20 69 74   create it if it
c59b0 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20 20 20 2a   does not..    *
c59c0 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  /.    assert( pP
c59d0 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
c59e0 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20  ER_UNLOCK );.   
c59f0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
c5a00 65 72 42 65 67 69 6e 28 70 50 67 2c 20 30 29 3b  erBegin(pPg, 0);
c5a10 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
c5a20 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
c5a30 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
c5a40 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
c5a50 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
c5a60 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20  R_RESERVED );.  
c5a70 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a    if( !pPager->j
c5a80 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50  ournalOpen && pP
c5a90 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c  ager->useJournal
c5aa0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
c5ab0 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61  ager_open_journa
c5ac0 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  l(pPager);.     
c5ad0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
c5ae0 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
c5af0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
c5b00 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
c5b10 6c 4f 70 65 6e 20 7c 7c 20 21 70 50 61 67 65 72  lOpen || !pPager
c5b20 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a  ->useJournal );.
c5b30 20 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74      pPager->dirt
c5b40 79 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 0a 20  yCache = 1;.  . 
c5b50 20 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 61     /* The transa
c5b60 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f  ction journal no
c5b70 77 20 65 78 69 73 74 73 20 61 6e 64 20 77 65 20  w exists and we 
c5b80 68 61 76 65 20 61 20 52 45 53 45 52 56 45 44 20  have a RESERVED 
c5b90 6f 72 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43  or an.    ** EXC
c5ba0 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74  LUSIVE lock on t
c5bb0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
c5bc0 20 66 69 6c 65 2e 20 20 57 72 69 74 65 20 74 68   file.  Write th
c5bd0 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74  e current page t
c5be0 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 72 61  o.    ** the tra
c5bf0 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c  nsaction journal
c5c00 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68   if it is not th
c5c10 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20  ere already..   
c5c20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 67   */.    if( !pPg
c5c30 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 26 26 20 28  ->inJournal && (
c5c40 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e  pPager->useJourn
c5c50 61 6c 20 7c 7c 20 4d 45 4d 44 42 29 20 29 7b 0a  al || MEMDB) ){.
c5c60 20 20 20 20 20 20 69 66 28 20 28 69 6e 74 29 70        if( (int)p
c5c70 50 67 2d 3e 70 67 6e 6f 20 3c 3d 20 70 50 61 67  Pg->pgno <= pPag
c5c80 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29  er->origDbSize )
c5c90 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 4d 45  {.        if( ME
c5ca0 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 20 20 20  MDB ){.         
c5cb0 20 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69 73   PgHistory *pHis
c5cc0 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53  t = PGHDR_TO_HIS
c5cd0 54 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a  T(pPg, pPager);.
c5ce0 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 54            PAGERT
c5cf0 52 41 43 45 33 28 22 4a 4f 55 52 4e 41 4c 20 25  RACE3("JOURNAL %
c5d00 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41  d page %d\n", PA
c5d10 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
c5d20 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  Pg->pgno);.     
c5d30 20 20 20 20 20 61 73 73 65 72 74 28 20 70 48 69       assert( pHi
c5d40 73 74 2d 3e 70 4f 72 69 67 3d 3d 30 20 29 3b 0a  st->pOrig==0 );.
c5d50 20 20 20 20 20 20 20 20 20 20 70 48 69 73 74 2d            pHist-
c5d60 3e 70 4f 72 69 67 20 3d 20 73 71 6c 69 74 65 33  >pOrig = sqlite3
c5d70 5f 6d 61 6c 6c 6f 63 28 20 70 50 61 67 65 72 2d  _malloc( pPager-
c5d80 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20  >pageSize );.   
c5d90 20 20 20 20 20 20 20 69 66 28 20 70 48 69 73 74         if( pHist
c5da0 2d 3e 70 4f 72 69 67 20 29 7b 0a 20 20 20 20 20  ->pOrig ){.     
c5db0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 48         memcpy(pH
c5dc0 69 73 74 2d 3e 70 4f 72 69 67 2c 20 50 47 48 44  ist->pOrig, PGHD
c5dd0 52 5f 54 4f 5f 44 41 54 41 28 70 50 67 29 2c 20  R_TO_DATA(pPg), 
c5de0 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
c5df0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
c5e00 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
c5e10 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75          u32 cksu
c5e20 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  m;.          cha
c5e30 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20  r *pData2;..    
c5e40 20 20 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75        /* We shou
c5e50 6c 64 20 6e 65 76 65 72 20 77 72 69 74 65 20 74  ld never write t
c5e60 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
c5e70 6c 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74  le the page that
c5e80 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
c5e90 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 62  ntains the datab
c5ea0 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20  ase locks.  The 
c5eb0 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
c5ec0 20 76 65 72 69 66 69 65 73 0a 20 20 20 20 20 20   verifies.      
c5ed0 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64      ** that we d
c5ee0 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20  o not. */.      
c5ef0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d      assert( pPg-
c5f00 3e 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f  >pgno!=PAGER_MJ_
c5f10 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a  PGNO(pPager) );.
c5f20 20 20 20 20 20 20 20 20 20 20 70 44 61 74 61 32            pData2
c5f30 20 3d 20 43 4f 44 45 43 32 28 70 50 61 67 65 72   = CODEC2(pPager
c5f40 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67  , pData, pPg->pg
c5f50 6e 6f 2c 20 37 29 3b 0a 20 20 20 20 20 20 20 20  no, 7);.        
c5f60 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 65 72 5f    cksum = pager_
c5f70 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 28 75  cksum(pPager, (u
c5f80 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 20 20 20  8*)pData2);.    
c5f90 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
c5fa0 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
c5fb0 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  fd, pPager->jour
c5fc0 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 6e  nalOff, pPg->pgn
c5fd0 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  o);.          if
c5fe0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
c5ff0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
c6000 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
c6010 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
c6020 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e  pData2, pPager->
c6030 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20  pageSize,.      
c6040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6050 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
c6060 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34  ->journalOff + 4
c6070 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
c6080 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
c6090 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67  f += pPager->pag
c60a0 65 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20 20 20  eSize+4;.       
c60b0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
c60c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
c60d0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
c60e0 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
c60f0 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
c6100 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
c6110 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20 20 20  , cksum);.      
c6120 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
c6130 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20  urnalOff += 4;. 
c6140 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
c6150 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a       IOTRACE(("J
c6160 4f 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25  OUT %p %d %lld %
c6170 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50  d\n", pPager, pP
c6180 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20  g->pgno, .      
c6190 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
c61a0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c  ger->journalOff,
c61b0 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
c61c0 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 50  e));.          P
c61d0 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
c61e0 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63  3_pager_writej_c
c61f0 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20  ount);.         
c6200 20 50 41 47 45 52 54 52 41 43 45 35 28 22 4a 4f   PAGERTRACE5("JO
c6210 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64  URNAL %d page %d
c6220 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73   needSync=%d has
c6230 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20  h(%08x)\n",.    
c6240 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52             PAGER
c6250 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
c6260 3e 70 67 6e 6f 2c 20 70 50 67 2d 3e 6e 65 65 64  >pgno, pPg->need
c6270 53 79 6e 63 2c 20 70 61 67 65 72 5f 70 61 67 65  Sync, pager_page
c6280 68 61 73 68 28 70 50 67 29 29 3b 0a 0a 20 20 20  hash(pPg));..   
c6290 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72         /* An err
c62a0 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 20 77  or has occured w
c62b0 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f  riting to the jo
c62c0 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20  urnal file. The 
c62d0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 72  .          ** tr
c62e0 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62  ansaction will b
c62f0 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79  e rolled back by
c6300 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65   the layer above
c6310 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
c6320 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
c6330 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
c6340 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
c6350 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   rc;.          }
c6360 0a 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  ..          pPag
c6370 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20  er->nRec++;.    
c6380 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
c6390 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c  ager->aInJournal
c63a0 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  !=0 );.         
c63b0 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72   pPager->aInJour
c63c0 6e 61 6c 5b 70 50 67 2d 3e 70 67 6e 6f 2f 38 5d  nal[pPg->pgno/8]
c63d0 20 7c 3d 20 31 3c 3c 28 70 50 67 2d 3e 70 67 6e   |= 1<<(pPg->pgn
c63e0 6f 26 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20  o&7);.          
c63f0 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20  pPg->needSync = 
c6400 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b  !pPager->noSync;
c6410 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
c6420 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65  Pager->stmtInUse
c6430 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
c6440 70 50 61 67 65 72 2d 3e 61 49 6e 53 74 6d 74 5b  pPager->aInStmt[
c6450 70 50 67 2d 3e 70 67 6e 6f 2f 38 5d 20 7c 3d 20  pPg->pgno/8] |= 
c6460 31 3c 3c 28 70 50 67 2d 3e 70 67 6e 6f 26 37 29  1<<(pPg->pgno&7)
c6470 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
c6480 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
c6490 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 50 67  lse{.        pPg
c64a0 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50  ->needSync = !pP
c64b0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
c64c0 72 74 65 64 20 26 26 20 21 70 50 61 67 65 72 2d  rted && !pPager-
c64d0 3e 6e 6f 53 79 6e 63 3b 0a 20 20 20 20 20 20 20  >noSync;.       
c64e0 20 50 41 47 45 52 54 52 41 43 45 34 28 22 41 50   PAGERTRACE4("AP
c64f0 50 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20  PEND %d page %d 
c6500 6e 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a  needSync=%d\n",.
c6510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6520 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
c6530 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 50 67 2d   pPg->pgno, pPg-
c6540 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20 20 20  >needSync);.    
c6550 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 50    }.      if( pP
c6560 67 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20  g->needSync ){. 
c6570 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e         pPager->n
c6580 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20  eedSync = 1;.   
c6590 20 20 20 7d 0a 20 20 20 20 20 20 70 50 67 2d 3e     }.      pPg->
c65a0 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20  inJournal = 1;. 
c65b0 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
c65c0 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
c65d0 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20  journal is open 
c65e0 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20  and the page is 
c65f0 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a  not in it,.    *
c6600 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65  * then write the
c6610 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f   current page to
c6620 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   the statement j
c6630 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68  ournal.  Note th
c6640 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74  at.    ** the st
c6650 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
c6660 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73 20 66  format differs f
c6670 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64  rom the standard
c6680 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a   journal format.
c6690 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74 20 69      ** in that i
c66a0 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68 65 63  t omits the chec
c66b0 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20 68 65  ksums and the he
c66c0 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ader..    */.   
c66d0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 6d   if( pPager->stm
c66e0 74 49 6e 55 73 65 20 0a 20 20 20 20 20 26 26 20  tInUse .     && 
c66f0 21 70 61 67 65 49 6e 53 74 61 74 65 6d 65 6e 74  !pageInStatement
c6700 28 70 50 67 29 20 0a 20 20 20 20 20 26 26 20 28  (pPg) .     && (
c6710 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70  int)pPg->pgno<=p
c6720 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20  Pager->stmtSize 
c6730 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 61 73  .    ){.      as
c6740 73 65 72 74 28 20 70 50 67 2d 3e 69 6e 4a 6f 75  sert( pPg->inJou
c6750 72 6e 61 6c 20 7c 7c 20 28 69 6e 74 29 70 50 67  rnal || (int)pPg
c6760 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f  ->pgno>pPager->o
c6770 72 69 67 44 62 53 69 7a 65 20 29 3b 0a 20 20 20  rigDbSize );.   
c6780 20 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a     if( MEMDB ){.
c6790 20 20 20 20 20 20 20 20 50 67 48 69 73 74 6f 72          PgHistor
c67a0 79 20 2a 70 48 69 73 74 20 3d 20 50 47 48 44 52  y *pHist = PGHDR
c67b0 5f 54 4f 5f 48 49 53 54 28 70 50 67 2c 20 70 50  _TO_HIST(pPg, pP
c67c0 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 61  ager);.        a
c67d0 73 73 65 72 74 28 20 70 48 69 73 74 2d 3e 70 53  ssert( pHist->pS
c67e0 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  tmt==0 );.      
c67f0 20 20 70 48 69 73 74 2d 3e 70 53 74 6d 74 20 3d    pHist->pStmt =
c6800 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
c6810 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
c6820 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e );.        if(
c6830 20 70 48 69 73 74 2d 3e 70 53 74 6d 74 20 29 7b   pHist->pStmt ){
c6840 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
c6850 79 28 70 48 69 73 74 2d 3e 70 53 74 6d 74 2c 20  y(pHist->pStmt, 
c6860 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50  PGHDR_TO_DATA(pP
c6870 67 29 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  g), pPager->page
c6880 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  Size);.        }
c6890 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52  .        PAGERTR
c68a0 41 43 45 33 28 22 53 54 4d 54 2d 4a 4f 55 52 4e  ACE3("STMT-JOURN
c68b0 41 4c 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22  AL %d page %d\n"
c68c0 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
c68d0 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20  ), pPg->pgno);. 
c68e0 20 20 20 20 20 20 20 70 61 67 65 5f 61 64 64 5f         page_add_
c68f0 74 6f 5f 73 74 6d 74 5f 6c 69 73 74 28 70 50 67  to_stmt_list(pPg
c6900 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
c6910 20 20 20 20 20 20 20 20 69 36 34 20 6f 66 66 73          i64 offs
c6920 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 6d  et = pPager->stm
c6930 74 4e 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d  tNRec*(4+pPager-
c6940 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20  >pageSize);.    
c6950 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32      char *pData2
c6960 20 3d 20 43 4f 44 45 43 32 28 70 50 61 67 65 72   = CODEC2(pPager
c6970 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67  , pData, pPg->pg
c6980 6e 6f 2c 20 37 29 3b 0a 20 20 20 20 20 20 20 20  no, 7);.        
c6990 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
c69a0 28 70 50 61 67 65 72 2d 3e 73 74 66 64 2c 20 6f  (pPager->stfd, o
c69b0 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f  ffset, pPg->pgno
c69c0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
c69d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
c69e0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
c69f0 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
c6a00 61 67 65 72 2d 3e 73 74 66 64 2c 20 70 44 61 74  ager->stfd, pDat
c6a10 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  a2, pPager->page
c6a20 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b  Size, offset+4);
c6a30 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
c6a40 20 20 20 50 41 47 45 52 54 52 41 43 45 33 28 22     PAGERTRACE3("
c6a50 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20  STMT-JOURNAL %d 
c6a60 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45  page %d\n", PAGE
c6a70 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67  RID(pPager), pPg
c6a80 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
c6a90 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
c6aa0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
c6ab0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
c6ac0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61     }.        pPa
c6ad0 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 2b 2b 3b  ger->stmtNRec++;
c6ae0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
c6af0 20 70 50 61 67 65 72 2d 3e 61 49 6e 53 74 6d 74   pPager->aInStmt
c6b00 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70  !=0 );.        p
c6b10 50 61 67 65 72 2d 3e 61 49 6e 53 74 6d 74 5b 70  Pager->aInStmt[p
c6b20 50 67 2d 3e 70 67 6e 6f 2f 38 5d 20 7c 3d 20 31  Pg->pgno/8] |= 1
c6b30 3c 3c 28 70 50 67 2d 3e 70 67 6e 6f 26 37 29 3b  <<(pPg->pgno&7);
c6b40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
c6b50 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20   }..  /* Update 
c6b60 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a  the database siz
c6b70 65 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20  e and return..  
c6b80 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
c6b90 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
c6ba0 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 69 66  R_SHARED );.  if
c6bb0 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
c6bc0 3c 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20  <(int)pPg->pgno 
c6bd0 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64  ){.    pPager->d
c6be0 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e  bSize = pPg->pgn
c6bf0 6f 3b 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44  o;.    if( !MEMD
c6c00 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53  B && pPager->dbS
c6c10 69 7a 65 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  ize==PENDING_BYT
c6c20 45 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  E/pPager->pageSi
c6c30 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  ze ){.      pPag
c6c40 65 72 2d 3e 64 62 53 69 7a 65 2b 2b 3b 0a 20 20  er->dbSize++;.  
c6c50 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
c6c60 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
c6c70 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
c6c80 73 65 64 20 74 6f 20 6d 61 72 6b 20 61 20 64 61  sed to mark a da
c6c90 74 61 2d 70 61 67 65 20 61 73 20 77 72 69 74 61  ta-page as writa
c6ca0 62 6c 65 2e 20 49 74 20 75 73 65 73 20 0a 2a 2a  ble. It uses .**
c6cb0 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 20 74   pager_write() t
c6cc0 6f 20 6f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c  o open a journal
c6cd0 20 66 69 6c 65 20 28 69 66 20 69 74 20 69 73 20   file (if it is 
c6ce0 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e  not already open
c6cf0 29 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74  ).** and write t
c6d00 68 65 20 70 61 67 65 20 2a 70 44 61 74 61 20 74  he page *pData t
c6d10 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  o the journal..*
c6d20 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65  *.** The differe
c6d30 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73  nce between this
c6d40 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61   function and pa
c6d50 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 20 74  ger_write() is t
c6d60 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63  hat this.** func
c6d70 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20  tion also deals 
c6d80 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c  with the special
c6d90 20 63 61 73 65 20 77 68 65 72 65 20 32 20 6f 72   case where 2 or
c6da0 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a 20 66   more pages.** f
c6db0 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64  it on a single d
c6dc0 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e 20 74  isk sector. In t
c6dd0 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d  his case all co-
c6de0 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 0a 2a  resident pages.*
c6df0 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e  * must have been
c6e00 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
c6e10 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66  journal file bef
c6e20 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
c6e30 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
c6e40 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
c6e50 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70  rWrite(DbPage *p
c6e60 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72  DbPage){.  int r
c6e70 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
c6e80 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70    PgHdr *pPg = p
c6e90 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20  DbPage;.  Pager 
c6ea0 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
c6eb0 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50  Pager;.  Pgno nP
c6ec0 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d 20 28  agePerSector = (
c6ed0 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
c6ee0 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 53  ze/pPager->pageS
c6ef0 69 7a 65 29 3b 0a 0a 20 20 70 61 67 65 72 45 6e  ize);..  pagerEn
c6f00 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20 20 69  ter(pPager);.  i
c6f10 66 28 20 21 4d 45 4d 44 42 20 26 26 20 6e 50 61  f( !MEMDB && nPa
c6f20 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20 29 7b  gePerSector>1 ){
c6f30 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 43  .    Pgno nPageC
c6f40 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f  ount;          /
c6f50 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
c6f60 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62  f pages in datab
c6f70 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  ase file */.    
c6f80 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 20 20  Pgno pg1;       
c6f90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
c6fa0 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73  st page of the s
c6fb0 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63  ector pPg is loc
c6fc0 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20  ated on. */.    
c6fd0 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20  int nPage;      
c6fe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
c6ff0 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 61  ber of pages sta
c7000 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 6f 20  rting at pg1 to 
c7010 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69  journal */.    i
c7020 6e 74 20 69 69 3b 0a 20 20 20 20 69 6e 74 20 6e  nt ii;.    int n
c7030 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 0a 20 20  eedSync = 0;..  
c7040 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f 4e    /* Set the doN
c7050 6f 74 53 79 6e 63 20 66 6c 61 67 20 74 6f 20 31  otSync flag to 1
c7060 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73  . This is becaus
c7070 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f  e we cannot allo
c7080 77 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  w a journal.    
c7090 2a 2a 20 68 65 61 64 65 72 20 74 6f 20 62 65 20  ** header to be 
c70a0 77 72 69 74 74 65 6e 20 62 65 74 77 65 65 6e 20  written between 
c70b0 74 68 65 20 70 61 67 65 73 20 6a 6f 75 72 6e 61  the pages journa
c70c0 6c 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  led by this func
c70d0 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
c70e0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
c70f0 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b  >doNotSync==0 );
c7100 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e  .    pPager->doN
c7110 6f 74 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20  otSync = 1;..   
c7120 20 2f 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61   /* This trick a
c7130 73 73 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68  ssumes that both
c7140 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61   the page-size a
c7150 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61  nd sector-size a
c7160 72 65 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74  re.    ** an int
c7170 65 67 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e  eger power of 2.
c7180 20 49 74 20 73 65 74 73 20 76 61 72 69 61 62 6c   It sets variabl
c7190 65 20 70 67 31 20 74 6f 20 74 68 65 20 69 64 65  e pg1 to the ide
c71a0 6e 74 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f  ntifier.    ** o
c71b0 66 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  f the first page
c71c0 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70   of the sector p
c71d0 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e  Pg is located on
c71e0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31  ..    */.    pg1
c71f0 20 3d 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31   = ((pPg->pgno-1
c7200 29 20 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65  ) & ~(nPagePerSe
c7210 63 74 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20  ctor-1)) + 1;.. 
c7220 20 20 20 6e 50 61 67 65 43 6f 75 6e 74 20 3d 20     nPageCount = 
c7230 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
c7240 63 6f 75 6e 74 28 70 50 61 67 65 72 29 3b 0a 20  count(pPager);. 
c7250 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f     if( pPg->pgno
c7260 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20  >nPageCount ){. 
c7270 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 70 50       nPage = (pP
c7280 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b 31  g->pgno - pg1)+1
c7290 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
c72a0 28 70 67 31 2b 6e 50 61 67 65 50 65 72 53 65 63  (pg1+nPagePerSec
c72b0 74 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 6e  tor-1)>nPageCoun
c72c0 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65  t ){.      nPage
c72d0 20 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 2d   = nPageCount+1-
c72e0 70 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  pg1;.    }else{.
c72f0 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50        nPage = nP
c7300 61 67 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 20  agePerSector;.  
c7310 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 6e    }.    assert(n
c7320 50 61 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 73  Page>0);.    ass
c7330 65 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67  ert(pg1<=pPg->pg
c7340 6e 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  no);.    assert(
c7350 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d  (pg1+nPage)>pPg-
c7360 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f 72  >pgno);..    for
c7370 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20  (ii=0; ii<nPage 
c7380 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
c7390 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50  ; ii++){.      P
c73a0 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 3b  gno pg = pg1+ii;
c73b0 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50  .      PgHdr *pP
c73c0 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21  age;.      if( !
c73d0 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e  pPager->aInJourn
c73e0 61 6c 20 7c 7c 20 70 67 3d 3d 70 50 67 2d 3e 70  al || pg==pPg->p
c73f0 67 6e 6f 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  gno || .        
c7400 20 20 70 67 3e 70 50 61 67 65 72 2d 3e 6f 72 69    pg>pPager->ori
c7410 67 44 62 53 69 7a 65 20 7c 7c 20 21 28 70 50 61  gDbSize || !(pPa
c7420 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 5b  ger->aInJournal[
c7430 70 67 2f 38 5d 26 28 31 3c 3c 28 70 67 26 37 29  pg/8]&(1<<(pg&7)
c7440 29 29 0a 20 20 20 20 20 20 29 20 7b 0a 20 20 20  )).      ) {.   
c7450 20 20 20 20 20 69 66 28 20 70 67 21 3d 50 41 47       if( pg!=PAG
c7460 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
c7470 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  r) ){.          
c7480 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
c7490 72 47 65 74 28 70 50 61 67 65 72 2c 20 70 67 2c  rGet(pPager, pg,
c74a0 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20   &pPage);.      
c74b0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
c74c0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
c74d0 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
c74e0 77 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20 20  write(pPage);.  
c74f0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 50            if( pP
c7500 61 67 65 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b  age->needSync ){
c7510 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  .              n
c7520 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20  eedSync = 1;.   
c7530 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
c7540 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
c7550 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b  gerUnref(pPage);
c7560 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
c7570 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
c7580 73 65 20 69 66 28 20 28 70 50 61 67 65 20 3d 20  se if( (pPage = 
c7590 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61  pager_lookup(pPa
c75a0 67 65 72 2c 20 70 67 29 29 20 29 7b 0a 20 20 20  ger, pg)) ){.   
c75b0 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
c75c0 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20  needSync ){.    
c75d0 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d        needSync =
c75e0 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
c75f0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
c7600 20 2f 2a 20 49 66 20 74 68 65 20 50 67 48 64 72   /* If the PgHdr
c7610 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69  .needSync flag i
c7620 73 20 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66  s set for any of
c7630 20 74 68 65 20 6e 50 61 67 65 20 70 61 67 65 73   the nPage pages
c7640 20 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e   .    ** startin
c7650 67 20 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69  g at pg1, then i
c7660 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65  t needs to be se
c7670 74 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  t for all of the
c7680 6d 2e 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a  m. Because.    *
c7690 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79  * writing to any
c76a0 20 6f 66 20 74 68 65 73 65 20 6e 50 61 67 65 20   of these nPage 
c76b0 70 61 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65  pages may damage
c76c0 20 74 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65   the others, the
c76d0 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20  .    ** journal 
c76e0 66 69 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69  file must contai
c76f0 6e 20 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65  n sync()ed copie
c7700 73 20 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d  s of all of them
c7710 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61  .    ** before a
c7720 6e 79 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62  ny of them can b
c7730 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f  e written out to
c7740 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
c7750 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  le..    */.    i
c7760 66 28 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20  f( needSync ){. 
c7770 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
c7780 69 3c 6e 50 61 67 65 20 26 26 20 6e 65 65 64 53  i<nPage && needS
c7790 79 6e 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ync; ii++){.    
c77a0 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65      PgHdr *pPage
c77b0 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28   = pager_lookup(
c77c0 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b  pPager, pg1+ii);
c77d0 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
c77e0 67 65 20 29 20 70 50 61 67 65 2d 3e 6e 65 65 64  ge ) pPage->need
c77f0 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20  Sync = 1;.      
c7800 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70  }.      assert(p
c7810 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29  Pager->needSync)
c7820 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73  ;.    }..    ass
c7830 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e  ert( pPager->doN
c7840 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20  otSync==1 );.   
c7850 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79   pPager->doNotSy
c7860 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  nc = 0;.  }else{
c7870 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
c7880 77 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a  write(pDbPage);.
c7890 20 20 7d 0a 20 20 70 61 67 65 72 4c 65 61 76 65    }.  pagerLeave
c78a0 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75  (pPager);.  retu
c78b0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
c78c0 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
c78d0 68 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 6e  he page given in
c78e0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 61   the argument wa
c78f0 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 73  s previously pas
c7900 73 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  sed.** to sqlite
c7910 33 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 20  3PagerWrite().  
c7920 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
c7930 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69  return TRUE if i
c7940 74 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68  t is ok.** to ch
c7950 61 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  ange the content
c7960 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f   of the page..*/
c7970 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
c7980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
c7990 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  nt sqlite3PagerI
c79a0 73 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67  swriteable(DbPag
c79b0 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72  e *pPg){.  retur
c79c0 6e 20 70 50 67 2d 3e 64 69 72 74 79 3b 0a 7d 0a  n pPg->dirty;.}.
c79d0 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
c79e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55  SQLITE_OMIT_VACU
c79f0 55 4d 0a 2f 2a 0a 2a 2a 20 52 65 70 6c 61 63 65  UM./*.** Replace
c7a00 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
c7a10 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 77 69  a single page wi
c7a20 74 68 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  th the informati
c7a30 6f 6e 20 69 6e 20 74 68 65 20 74 68 69 72 64 0a  on in the third.
c7a40 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  ** argument..*/.
c7a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
c7a60 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  nt sqlite3PagerO
c7a70 76 65 72 77 72 69 74 65 28 50 61 67 65 72 20 2a  verwrite(Pager *
c7a80 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e  pPager, Pgno pgn
c7a90 6f 2c 20 76 6f 69 64 20 2a 70 44 61 74 61 29 7b  o, void *pData){
c7aa0 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20  .  PgHdr *pPg;. 
c7ab0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 70 61 67 65   int rc;..  page
c7ac0 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a  rEnter(pPager);.
c7ad0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
c7ae0 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 70  gerGet(pPager, p
c7af0 67 6e 6f 2c 20 26 70 50 67 29 3b 0a 20 20 69 66  gno, &pPg);.  if
c7b00 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
c7b10 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
c7b20 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
c7b30 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  g);.    if( rc==
c7b40 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
c7b50 20 20 20 6d 65 6d 63 70 79 28 73 71 6c 69 74 65     memcpy(sqlite
c7b60 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50  3PagerGetData(pP
c7b70 67 29 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65  g), pData, pPage
c7b80 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  r->pageSize);.  
c7b90 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50    }.    sqlite3P
c7ba0 61 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a  agerUnref(pPg);.
c7bb0 20 20 7d 0a 20 20 70 61 67 65 72 4c 65 61 76 65    }.  pagerLeave
c7bc0 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75  (pPager);.  retu
c7bd0 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a  rn rc;.}.#endif.
c7be0 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f  ./*.** A call to
c7bf0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65   this routine te
c7c00 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68  lls the pager th
c7c10 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63  at it is not nec
c7c20 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69  essary to.** wri
c7c30 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  te the informati
c7c40 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62  on on page pPg b
c7c50 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c  ack to the disk,
c7c60 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20   even though.** 
c7c70 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20  that page might 
c7c80 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72  be marked as dir
c7c90 74 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76  ty..**.** The ov
c7ca0 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65  erlying software
c7cb0 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69   layer calls thi
c7cc0 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61  s routine when a
c7cd0 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a  ll of the data.*
c7ce0 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70  * on the given p
c7cf0 61 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 20  age is unused.  
c7d00 54 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20  The pager marks 
c7d10 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61  the page as clea
c7d20 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20  n so.** that it 
c7d30 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69  does not get wri
c7d40 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a  tten to disk..**
c7d50 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74  .** Tests show t
c7d60 68 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a  hat this optimiz
c7d70 61 74 69 6f 6e 2c 20 74 6f 67 65 74 68 65 72 20  ation, together 
c7d80 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 71 6c 69  with the.** sqli
c7d90 74 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c  te3PagerDontRoll
c7da0 62 61 63 6b 28 29 20 62 65 6c 6f 77 2c 20 6d 6f  back() below, mo
c7db0 72 65 20 74 68 61 6e 20 64 6f 75 62 6c 65 20 74  re than double t
c7dc0 68 65 20 73 70 65 65 64 0a 2a 2a 20 6f 66 20 6c  he speed.** of l
c7dd0 61 72 67 65 20 49 4e 53 45 52 54 20 6f 70 65 72  arge INSERT oper
c7de0 61 74 69 6f 6e 73 20 61 6e 64 20 71 75 61 64 72  ations and quadr
c7df0 75 70 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f  uple the speed o
c7e00 66 20 6c 61 72 67 65 20 44 45 4c 45 54 45 73 2e  f large DELETEs.
c7e10 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  .**.** When this
c7e20 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
c7e30 65 64 2c 20 73 65 74 20 74 68 65 20 61 6c 77 61  ed, set the alwa
c7e40 79 73 52 6f 6c 6c 62 61 63 6b 20 66 6c 61 67 20  ysRollback flag 
c7e50 74 6f 20 74 72 75 65 2e 0a 2a 2a 20 53 75 62 73  to true..** Subs
c7e60 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20  equent calls to 
c7e70 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
c7e80 52 6f 6c 6c 62 61 63 6b 28 29 20 66 6f 72 20 74  Rollback() for t
c7e90 68 65 20 73 61 6d 65 20 70 61 67 65 0a 2a 2a 20  he same page.** 
c7ea0 77 69 6c 6c 20 74 68 65 72 65 61 66 74 65 72 20  will thereafter 
c7eb0 62 65 20 69 67 6e 6f 72 65 64 2e 20 20 54 68 69  be ignored.  Thi
c7ec0 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74  s is necessary t
c7ed0 6f 20 61 76 6f 69 64 20 61 20 70 72 6f 62 6c 65  o avoid a proble
c7ee0 6d 0a 2a 2a 20 77 68 65 72 65 20 61 20 70 61 67  m.** where a pag
c7ef0 65 20 77 69 74 68 20 64 61 74 61 20 69 73 20 61  e with data is a
c7f00 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65  dded to the free
c7f10 6c 69 73 74 20 64 75 72 69 6e 67 20 6f 6e 65 20  list during one 
c7f20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 74 72 61  part of.** a tra
c7f30 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 72 65  nsaction then re
c7f40 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 66  moved from the f
c7f50 72 65 65 6c 69 73 74 20 64 75 72 69 6e 67 20 61  reelist during a
c7f60 20 6c 61 74 65 72 20 70 61 72 74 0a 2a 2a 20 6f   later part.** o
c7f70 66 20 74 68 65 20 73 61 6d 65 20 74 72 61 6e 73  f the same trans
c7f80 61 63 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65  action and reuse
c7f90 64 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  d for some other
c7fa0 20 70 75 72 70 6f 73 65 2e 20 20 57 68 65 6e 20   purpose.  When 
c7fb0 69 74 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 61  it.** is first a
c7fc0 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65  dded to the free
c7fd0 6c 69 73 74 2c 20 74 68 69 73 20 72 6f 75 74 69  list, this routi
c7fe0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 57  ne is called.  W
c7ff0 68 65 6e 20 72 65 75 73 65 64 2c 0a 2a 2a 20 74  hen reused,.** t
c8000 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 44  he sqlite3PagerD
c8010 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 72 6f  ontRollback() ro
c8020 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
c8030 20 20 42 75 74 20 62 65 63 61 75 73 65 20 74 68    But because th
c8040 65 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 69  e.** page contai
c8050 6e 73 20 63 72 69 74 69 63 61 6c 20 64 61 74 61  ns critical data
c8060 2c 20 77 65 20 73 74 69 6c 6c 20 6e 65 65 64 20  , we still need 
c8070 74 6f 20 62 65 20 73 75 72 65 20 69 74 20 67 65  to be sure it ge
c8080 74 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63  ts.** rolled bac
c8090 6b 20 69 6e 20 73 70 69 74 65 20 6f 66 20 74 68  k in spite of th
c80a0 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  e sqlite3PagerDo
c80b0 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 63 61 6c  ntRollback() cal
c80c0 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  l..*/.SQLITE_PRI
c80d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
c80e0 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28  3PagerDontWrite(
c80f0 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 29  DbPage *pDbPage)
c8100 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d  {.  PgHdr *pPg =
c8110 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65   pDbPage;.  Page
c8120 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
c8130 3e 70 50 61 67 65 72 3b 0a 0a 20 20 69 66 28 20  >pPager;..  if( 
c8140 4d 45 4d 44 42 20 29 20 72 65 74 75 72 6e 3b 0a  MEMDB ) return;.
c8150 20 20 70 61 67 65 72 45 6e 74 65 72 28 70 50 61    pagerEnter(pPa
c8160 67 65 72 29 3b 0a 20 20 70 50 67 2d 3e 61 6c 77  ger);.  pPg->alw
c8170 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b  aysRollback = 1;
c8180 0a 20 20 69 66 28 20 70 50 67 2d 3e 64 69 72 74  .  if( pPg->dirt
c8190 79 20 26 26 20 21 70 50 61 67 65 72 2d 3e 73 74  y && !pPager->st
c81a0 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 61  mtInUse ){.    a
c81b0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
c81c0 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
c81d0 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  ED );.    if( pP
c81e0 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 28 69  ager->dbSize==(i
c81f0 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20 26 26 20  nt)pPg->pgno && 
c8200 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69  pPager->origDbSi
c8210 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  ze<pPager->dbSiz
c8220 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  e ){.      /* If
c8230 20 74 68 69 73 20 70 61 67 65 73 20 69 73 20 74   this pages is t
c8240 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20  he last page in 
c8250 74 68 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65  the file and the
c8260 20 66 69 6c 65 20 68 61 73 20 67 72 6f 77 6e 0a   file has grown.
c8270 20 20 20 20 20 20 2a 2a 20 64 75 72 69 6e 67 20        ** during 
c8280 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  the current tran
c8290 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 64 6f  saction, then do
c82a0 20 4e 4f 54 20 6d 61 72 6b 20 74 68 65 20 70 61   NOT mark the pa
c82b0 67 65 20 61 73 20 63 6c 65 61 6e 2e 0a 20 20 20  ge as clean..   
c82c0 20 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 20 64     ** When the d
c82d0 61 74 61 62 61 73 65 20 66 69 6c 65 20 67 72 6f  atabase file gro
c82e0 77 73 2c 20 77 65 20 6d 75 73 74 20 6d 61 6b 65  ws, we must make
c82f0 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6c   sure that the l
c8300 61 73 74 20 70 61 67 65 0a 20 20 20 20 20 20 2a  ast page.      *
c8310 2a 20 67 65 74 73 20 77 72 69 74 74 65 6e 20 61  * gets written a
c8320 74 20 6c 65 61 73 74 20 6f 6e 63 65 20 73 6f 20  t least once so 
c8330 74 68 61 74 20 74 68 65 20 64 69 73 6b 20 66 69  that the disk fi
c8340 6c 65 20 77 69 6c 6c 20 62 65 20 74 68 65 20 63  le will be the c
c8350 6f 72 72 65 63 74 0a 20 20 20 20 20 20 2a 2a 20  orrect.      ** 
c8360 73 69 7a 65 2e 20 49 66 20 79 6f 75 20 64 6f 20  size. If you do 
c8370 6e 6f 74 20 77 72 69 74 65 20 74 68 69 73 20 70  not write this p
c8380 61 67 65 20 61 6e 64 20 74 68 65 20 73 69 7a 65  age and the size
c8390 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 20 20   of the file.   
c83a0 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 69 73     ** on the dis
c83b0 6b 20 65 6e 64 73 20 75 70 20 62 65 69 6e 67 20  k ends up being 
c83c0 74 6f 6f 20 73 6d 61 6c 6c 2c 20 74 68 61 74 20  too small, that 
c83d0 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61  can lead to data
c83e0 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f  base.      ** co
c83f0 72 72 75 70 74 69 6f 6e 20 64 75 72 69 6e 67 20  rruption during 
c8400 74 68 65 20 6e 65 78 74 20 74 72 61 6e 73 61 63  the next transac
c8410 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  tion..      */. 
c8420 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
c8430 50 41 47 45 52 54 52 41 43 45 33 28 22 44 4f 4e  PAGERTRACE3("DON
c8440 54 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 20  T_WRITE page %d 
c8450 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e 70  of %d\n", pPg->p
c8460 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 61  gno, PAGERID(pPa
c8470 67 65 72 29 29 3b 0a 20 20 20 20 20 20 49 4f 54  ger));.      IOT
c8480 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20  RACE(("CLEAN %p 
c8490 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
c84a0 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 20  Pg->pgno)).     
c84b0 20 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b   makeClean(pPg);
c84c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
c84d0 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 20  HECK_PAGES.     
c84e0 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d   pPg->pageHash =
c84f0 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28   pager_pagehash(
c8500 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  pPg);.#endif.   
c8510 20 7d 0a 20 20 7d 0a 20 20 70 61 67 65 72 4c 65   }.  }.  pagerLe
c8520 61 76 65 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a  ave(pPager);.}..
c8530 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  /*.** A call to 
c8540 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c  this routine tel
c8550 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61  ls the pager tha
c8560 74 20 69 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20  t if a rollback 
c8570 6f 63 63 75 72 73 2c 0a 2a 2a 20 69 74 20 69 73  occurs,.** it is
c8580 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74   not necessary t
c8590 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61  o restore the da
c85a0 74 61 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  ta on the given 
c85b0 70 61 67 65 2e 20 20 54 68 69 73 0a 2a 2a 20 6d  page.  This.** m
c85c0 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61  eans that the pa
c85d0 67 65 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76  ger does not hav
c85e0 65 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20  e to record the 
c85f0 67 69 76 65 6e 20 70 61 67 65 20 69 6e 20 74 68  given page in th
c8600 65 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  e.** rollback jo
c8610 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urnal..**.** If 
c8620 77 65 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20  we have not yet 
c8630 61 63 74 75 61 6c 6c 79 20 72 65 61 64 20 74 68  actually read th
c8640 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69  e content of thi
c8650 73 20 70 61 67 65 20 28 69 66 0a 2a 2a 20 74 68  s page (if.** th
c8660 65 20 50 67 48 64 72 2e 6e 65 65 64 52 65 61 64  e PgHdr.needRead
c8670 20 66 6c 61 67 20 69 73 20 73 65 74 29 20 74 68   flag is set) th
c8680 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
c8690 61 63 74 73 20 61 73 20 61 20 70 72 6f 6d 69 73  acts as a promis
c86a0 65 0a 2a 2a 20 74 68 61 74 20 77 65 20 77 69 6c  e.** that we wil
c86b0 6c 20 6e 65 76 65 72 20 6e 65 65 64 20 74 6f 20  l never need to 
c86c0 72 65 61 64 20 74 68 65 20 70 61 67 65 20 63 6f  read the page co
c86d0 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 66 75 74  ntent in the fut
c86e0 75 72 65 2e 0a 2a 2a 20 73 6f 20 74 68 65 20 6e  ure..** so the n
c86f0 65 65 64 52 65 61 64 20 66 6c 61 67 20 63 61 6e  eedRead flag can
c8700 20 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 74   be cleared at t
c8710 68 69 73 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51  his point..*/.SQ
c8720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
c8730 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  d sqlite3PagerDo
c8740 6e 74 52 6f 6c 6c 62 61 63 6b 28 44 62 50 61 67  ntRollback(DbPag
c8750 65 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72  e *pPg){.  Pager
c8760 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
c8770 70 50 61 67 65 72 3b 0a 0a 20 20 70 61 67 65 72  pPager;..  pager
c8780 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20  Enter(pPager);. 
c8790 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
c87a0 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  >state>=PAGER_RE
c87b0 53 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20  SERVED );.  if( 
c87c0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
c87d0 70 65 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  pen==0 ) return;
c87e0 0a 20 20 69 66 28 20 70 50 67 2d 3e 61 6c 77 61  .  if( pPg->alwa
c87f0 79 73 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 70 50  ysRollback || pP
c8800 61 67 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c  ager->alwaysRoll
c8810 62 61 63 6b 20 7c 7c 20 4d 45 4d 44 42 20 29 20  back || MEMDB ) 
c8820 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70  return;.  if( !p
c8830 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 26 26  Pg->inJournal &&
c8840 20 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20   (int)pPg->pgno 
c8850 3c 3d 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44  <= pPager->origD
c8860 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 61 73 73  bSize ){.    ass
c8870 65 72 74 28 20 70 50 61 67 65 72 2d 3e 61 49 6e  ert( pPager->aIn
c8880 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20  Journal!=0 );.  
c8890 20 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75    pPager->aInJou
c88a0 72 6e 61 6c 5b 70 50 67 2d 3e 70 67 6e 6f 2f 38  rnal[pPg->pgno/8
c88b0 5d 20 7c 3d 20 31 3c 3c 28 70 50 67 2d 3e 70 67  ] |= 1<<(pPg->pg
c88c0 6e 6f 26 37 29 3b 0a 20 20 20 20 70 50 67 2d 3e  no&7);.    pPg->
c88d0 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20  inJournal = 1;. 
c88e0 20 20 20 70 50 67 2d 3e 6e 65 65 64 52 65 61 64     pPg->needRead
c88f0 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50   = 0;.    if( pP
c8900 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20  ager->stmtInUse 
c8910 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
c8920 3e 61 49 6e 53 74 6d 74 5b 70 50 67 2d 3e 70 67  >aInStmt[pPg->pg
c8930 6e 6f 2f 38 5d 20 7c 3d 20 31 3c 3c 28 70 50 67  no/8] |= 1<<(pPg
c8940 2d 3e 70 67 6e 6f 26 37 29 3b 0a 20 20 20 20 7d  ->pgno&7);.    }
c8950 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 33  .    PAGERTRACE3
c8960 28 22 44 4f 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  ("DONT_ROLLBACK 
c8970 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22  page %d of %d\n"
c8980 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47  , pPg->pgno, PAG
c8990 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20  ERID(pPager));. 
c89a0 20 20 20 49 4f 54 52 41 43 45 28 28 22 47 41 52     IOTRACE(("GAR
c89b0 42 41 47 45 20 25 70 20 25 64 5c 6e 22 2c 20 70  BAGE %p %d\n", p
c89c0 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
c89d0 29 29 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  )).  }.  if( pPa
c89e0 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 0a  ger->stmtInUse .
c89f0 20 20 20 26 26 20 21 70 61 67 65 49 6e 53 74 61     && !pageInSta
c8a00 74 65 6d 65 6e 74 28 70 50 67 29 20 0a 20 20 20  tement(pPg) .   
c8a10 26 26 20 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e  && (int)pPg->pgn
c8a20 6f 3c 3d 70 50 61 67 65 72 2d 3e 73 74 6d 74 53  o<=pPager->stmtS
c8a30 69 7a 65 20 0a 20 20 29 7b 0a 20 20 20 20 61 73  ize .  ){.    as
c8a40 73 65 72 74 28 20 70 50 67 2d 3e 69 6e 4a 6f 75  sert( pPg->inJou
c8a50 72 6e 61 6c 20 7c 7c 20 28 69 6e 74 29 70 50 67  rnal || (int)pPg
c8a60 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f  ->pgno>pPager->o
c8a70 72 69 67 44 62 53 69 7a 65 20 29 3b 0a 20 20 20  rigDbSize );.   
c8a80 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
c8a90 3e 61 49 6e 53 74 6d 74 21 3d 30 20 29 3b 0a 20  >aInStmt!=0 );. 
c8aa0 20 20 20 70 50 61 67 65 72 2d 3e 61 49 6e 53 74     pPager->aInSt
c8ab0 6d 74 5b 70 50 67 2d 3e 70 67 6e 6f 2f 38 5d 20  mt[pPg->pgno/8] 
c8ac0 7c 3d 20 31 3c 3c 28 70 50 67 2d 3e 70 67 6e 6f  |= 1<<(pPg->pgno
c8ad0 26 37 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72  &7);.  }.  pager
c8ae0 4c 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 7d  Leave(pPager);.}
c8af0 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
c8b00 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
c8b10 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  to increment the
c8b20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63   database file c
c8b30 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 0a 2a  hange-counter,.*
c8b40 2a 20 73 74 6f 72 65 64 20 61 74 20 62 79 74 65  * stored at byte
c8b50 20 32 34 20 6f 66 20 74 68 65 20 70 61 67 65 72   24 of the pager
c8b60 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
c8b70 20 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f   int pager_incr_
c8b80 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61  changecounter(Pa
c8b90 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
c8ba0 20 69 73 44 69 72 65 63 74 29 7b 0a 20 20 50 67   isDirect){.  Pg
c8bb0 48 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 75  Hdr *pPgHdr;.  u
c8bc0 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65  32 change_counte
c8bd0 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  r;.  int rc = SQ
c8be0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20  LITE_OK;..  if( 
c8bf0 21 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43  !pPager->changeC
c8c00 6f 75 6e 74 44 6f 6e 65 20 29 7b 0a 20 20 20 20  ountDone ){.    
c8c10 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f  /* Open page 1 o
c8c20 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77  f the file for w
c8c30 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72  riting. */.    r
c8c40 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
c8c50 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26  Get(pPager, 1, &
c8c60 70 50 67 48 64 72 29 3b 0a 20 20 20 20 69 66 28  pPgHdr);.    if(
c8c70 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
c8c80 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
c8c90 20 69 66 28 20 21 69 73 44 69 72 65 63 74 20 29   if( !isDirect )
c8ca0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
c8cb0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
c8cc0 50 67 48 64 72 29 3b 0a 20 20 20 20 20 20 69 66  PgHdr);.      if
c8cd0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
c8ce0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
c8cf0 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65   }..    /* Incre
c8d00 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a  ment the value j
c8d10 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69  ust read and wri
c8d20 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79  te it back to by
c8d30 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20 63 68  te 24. */.    ch
c8d40 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73  ange_counter = s
c8d50 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 28  qlite3Get4byte((
c8d60 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69  u8*)pPager->dbFi
c8d70 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 63 68 61  leVers);.    cha
c8d80 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20  nge_counter++;. 
c8d90 20 20 20 70 75 74 33 32 62 69 74 73 28 28 28 63     put32bits(((c
c8da0 68 61 72 2a 29 50 47 48 44 52 5f 54 4f 5f 44 41  har*)PGHDR_TO_DA
c8db0 54 41 28 70 50 67 48 64 72 29 29 2b 32 34 2c 20  TA(pPgHdr))+24, 
c8dc0 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b  change_counter);
c8dd0 0a 0a 20 20 20 20 69 66 28 20 69 73 44 69 72 65  ..    if( isDire
c8de0 63 74 20 26 26 20 70 50 61 67 65 72 2d 3e 66 64  ct && pPager->fd
c8df0 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
c8e00 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a      const void *
c8e10 7a 42 75 66 20 3d 20 50 47 48 44 52 5f 54 4f 5f  zBuf = PGHDR_TO_
c8e20 44 41 54 41 28 70 50 67 48 64 72 29 3b 0a 20 20  DATA(pPgHdr);.  
c8e30 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
c8e40 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
c8e50 66 64 2c 20 7a 42 75 66 2c 20 70 50 61 67 65 72  fd, zBuf, pPager
c8e60 2d 3e 70 61 67 65 53 69 7a 65 2c 20 30 29 3b 0a  ->pageSize, 0);.
c8e70 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
c8e80 6c 65 61 73 65 20 74 68 65 20 70 61 67 65 20 72  lease the page r
c8e90 65 66 65 72 65 6e 63 65 2e 20 2a 2f 0a 20 20 20  eference. */.   
c8ea0 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
c8eb0 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20  ef(pPgHdr);.    
c8ec0 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
c8ed0 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 7d  untDone = 1;.  }
c8ee0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
c8ef0 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20  ./*.** Sync the 
c8f00 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f  database file fo
c8f10 72 20 74 68 65 20 70 61 67 65 72 20 70 50 61 67  r the pager pPag
c8f20 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e  er. zMaster poin
c8f30 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65 0a 2a  ts to the name.*
c8f40 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f  * of a master jo
c8f50 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 20  urnal file that 
c8f60 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65  should be writte
c8f70 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 69 76  n into the indiv
c8f80 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  idual.** journal
c8f90 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 20 6d   file. zMaster m
c8fa0 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63  ay be NULL, whic
c8fb0 68 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64  h is interpreted
c8fc0 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a   as no master.**
c8fd0 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 6e 67   journal (a sing
c8fe0 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e  le database tran
c8ff0 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20  saction)..**.** 
c9000 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73  This routine ens
c9010 75 72 65 73 20 74 68 61 74 20 74 68 65 20 6a 6f  ures that the jo
c9020 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 2c  urnal is synced,
c9030 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
c9040 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 74   written.** to t
c9050 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
c9060 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   and the databas
c9070 65 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 54  e file synced. T
c9080 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68  he only thing th
c9090 61 74 0a 2a 2a 20 72 65 6d 61 69 6e 73 20 74 6f  at.** remains to
c90a0 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e   commit the tran
c90b0 73 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 64 65  saction is to de
c90c0 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lete the journal
c90d0 20 66 69 6c 65 20 28 6f 72 0a 2a 2a 20 6d 61 73   file (or.** mas
c90e0 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
c90f0 20 69 66 20 73 70 65 63 69 66 69 65 64 29 2e 0a   if specified)..
c9100 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
c9110 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c  if zMaster==NULL
c9120 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20  , this does not 
c9130 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 65 76  overwrite a prev
c9140 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70 61  ious value.** pa
c9150 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c 69 74  ssed to an sqlit
c9160 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
c9170 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a  seOne() call..**
c9180 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72  .** If parameter
c9190 20 6e 54 72 75 6e 63 20 69 73 20 6e 6f 6e 2d 7a   nTrunc is non-z
c91a0 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61  ero, then the pa
c91b0 67 65 72 20 66 69 6c 65 20 69 73 20 74 72 75 6e  ger file is trun
c91c0 63 61 74 65 64 20 74 6f 0a 2a 2a 20 6e 54 72 75  cated to.** nTru
c91d0 6e 63 20 70 61 67 65 73 20 28 74 68 69 73 20 69  nc pages (this i
c91e0 73 20 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76  s used by auto-v
c91f0 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 73 29  acuum databases)
c9200 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
c9210 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
c9220 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f  agerCommitPhaseO
c9230 6e 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ne(Pager *pPager
c9240 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d  , const char *zM
c9250 61 73 74 65 72 2c 20 50 67 6e 6f 20 6e 54 72 75  aster, Pgno nTru
c9260 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  nc){.  int rc = 
c9270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 41  SQLITE_OK;..  PA
c9280 47 45 52 54 52 41 43 45 34 28 22 44 41 54 41 42  GERTRACE4("DATAB
c9290 41 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25  ASE SYNC: File=%
c92a0 73 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 54 72  s zMaster=%s nTr
c92b0 75 6e 63 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20  unc=%d\n", .    
c92c0 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e    pPager->zFilen
c92d0 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 54  ame, zMaster, nT
c92e0 72 75 6e 63 29 3b 0a 20 20 70 61 67 65 72 45 6e  runc);.  pagerEn
c92f0 74 65 72 28 70 50 61 67 65 72 29 3b 0a 0a 20 20  ter(pPager);..  
c9300 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
c9310 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f   in-memory db, o
c9320 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 65 20  r no pages have 
c9330 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c  been written to,
c9340 20 6f 72 20 74 68 69 73 0a 20 20 2a 2a 20 66 75   or this.  ** fu
c9350 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61  nction has alrea
c9360 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20  dy been called, 
c9370 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20  it is a no-op.. 
c9380 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72   */.  if( pPager
c9390 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 53  ->state!=PAGER_S
c93a0 59 4e 43 45 44 20 26 26 20 21 4d 45 4d 44 42 20  YNCED && !MEMDB 
c93b0 26 26 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79  && pPager->dirty
c93c0 43 61 63 68 65 20 29 7b 0a 20 20 20 20 50 67 48  Cache ){.    PgH
c93d0 64 72 20 2a 70 50 67 3b 0a 0a 23 69 66 64 65 66  dr *pPg;..#ifdef
c93e0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
c93f0 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20  TOMIC_WRITE.    
c9400 2f 2a 20 54 68 65 20 61 74 6f 6d 69 63 2d 77 72  /* The atomic-wr
c9410 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ite optimization
c9420 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 20   can be used if 
c9430 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  all of the.    *
c9440 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  * following are 
c9450 74 72 75 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  true:.    **.   
c9460 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 66 69 6c   **    + The fil
c9470 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  e-system support
c9480 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69  s the atomic-wri
c9490 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72 0a  te property for.
c94a0 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f 63      **      bloc
c94b0 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65 2d  ks of size page-
c94c0 73 69 7a 65 2c 20 61 6e 64 0a 20 20 20 20 2a 2a  size, and.    **
c94d0 20 20 20 20 2b 20 54 68 69 73 20 63 6f 6d 6d 69      + This commi
c94e0 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
c94f0 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 74 72   a multi-file tr
c9500 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a 20  ansaction, and. 
c9510 20 20 20 2a 2a 20 20 20 20 2b 20 45 78 61 63 74     **    + Exact
c9520 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 61 73 20  ly one page has 
c9530 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 61 6e  been modified an
c9540 64 20 73 74 6f 72 65 20 69 6e 20 74 68 65 20 6a  d store in the j
c9550 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20  ournal file..   
c9560 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68   **.    ** If th
c9570 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63  e optimization c
c9580 61 6e 20 62 65 20 75 73 65 64 2c 20 74 68 65 6e  an be used, then
c9590 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
c95a0 65 20 77 69 6c 6c 20 6e 65 76 65 72 0a 20 20 20  e will never.   
c95b0 20 2a 2a 20 62 65 20 63 72 65 61 74 65 64 20 66   ** be created f
c95c0 6f 72 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  or this transact
c95d0 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
c95e0 69 6e 74 20 75 73 65 41 74 6f 6d 69 63 57 72 69  int useAtomicWri
c95f0 74 65 20 3d 20 28 0a 20 20 20 20 20 20 20 20 21  te = (.        !
c9600 7a 4d 61 73 74 65 72 20 26 26 20 0a 20 20 20 20  zMaster && .    
c9610 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
c9620 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66  nalOff==jrnlBuff
c9630 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 20 26  erSize(pPager) &
c9640 26 20 0a 20 20 20 20 20 20 20 20 6e 54 72 75 6e  & .        nTrun
c9650 63 3d 3d 30 20 26 26 20 0a 20 20 20 20 20 20 20  c==0 && .       
c9660 20 28 30 3d 3d 70 50 61 67 65 72 2d 3e 70 44 69   (0==pPager->pDi
c9670 72 74 79 20 7c 7c 20 30 3d 3d 70 50 61 67 65 72  rty || 0==pPager
c9680 2d 3e 70 44 69 72 74 79 2d 3e 70 44 69 72 74 79  ->pDirty->pDirty
c9690 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  ).    );.    if(
c96a0 20 75 73 65 41 74 6f 6d 69 63 57 72 69 74 65 20   useAtomicWrite 
c96b0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61  ){.      /* Upda
c96c0 74 65 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  te the nRec fiel
c96d0 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d in the journal
c96e0 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20   file. */.      
c96f0 69 6e 74 20 6f 66 66 73 65 74 20 3d 20 70 50 61  int offset = pPa
c9700 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20  ger->journalHdr 
c9710 2b 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  + sizeof(aJourna
c9720 6c 4d 61 67 69 63 29 3b 0a 20 20 20 20 20 20 61  lMagic);.      a
c9730 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 52  ssert(pPager->nR
c9740 65 63 3d 3d 31 29 3b 0a 20 20 20 20 20 20 72 63  ec==1);.      rc
c9750 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
c9760 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6f 66 66 73  Pager->jfd, offs
c9770 65 74 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 63  et, pPager->nRec
c9780 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64  );..      /* Upd
c9790 61 74 65 20 74 68 65 20 64 62 20 66 69 6c 65 20  ate the db file 
c97a0 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 2e 20  change counter. 
c97b0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61  The following ca
c97c0 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 20  ll will modify. 
c97d0 20 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 2d 6d       ** the in-m
c97e0 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61  emory representa
c97f0 74 69 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 74  tion of page 1 t
c9800 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75 70  o include the up
c9810 64 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 63  dated.      ** c
c9820 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e  hange counter an
c9830 64 20 74 68 65 6e 20 77 72 69 74 65 20 70 61 67  d then write pag
c9840 65 20 31 20 64 69 72 65 63 74 6c 79 20 74 6f 20  e 1 directly to 
c9850 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20  the database.   
c9860 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65 63 61     ** file. Beca
c9870 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d 69  use of the atomi
c9880 63 2d 77 72 69 74 65 20 70 72 6f 70 65 72 74 79  c-write property
c9890 20 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 6c   of the host fil
c98a0 65 2d 73 79 73 74 65 6d 2c 20 0a 20 20 20 20 20  e-system, .     
c98b0 20 2a 2a 20 74 68 69 73 20 69 73 20 73 61 66 65   ** this is safe
c98c0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
c98d0 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72   rc = pager_incr
c98e0 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70  _changecounter(p
c98f0 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 7d  Pager, 1);.    }
c9900 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
c9910 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43   sqlite3JournalC
c9920 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  reate(pPager->jf
c9930 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  d);.      if( rc
c9940 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
c9950 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20  to sync_exit;.  
c9960 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 75 73    }..    if( !us
c9970 65 41 74 6f 6d 69 63 57 72 69 74 65 20 29 0a 23  eAtomicWrite ).#
c9980 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66  endif..    /* If
c9990 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
c99a0 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 61 73 20  l file name has 
c99b0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 69  already been wri
c99c0 74 74 65 6e 20 74 6f 20 74 68 65 0a 20 20 20 20  tten to the.    
c99d0 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  ** journal file,
c99e0 20 74 68 65 6e 20 6e 6f 20 73 79 6e 63 20 69 73   then no sync is
c99f0 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20   required. This 
c9a00 68 61 70 70 65 6e 73 20 77 68 65 6e 20 69 74 20  happens when it 
c9a10 69 73 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65  is.    ** writte
c9a20 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63  n, then the proc
c9a30 65 73 73 20 66 61 69 6c 73 20 74 6f 20 75 70 67  ess fails to upg
c9a40 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45 53 45  rade from a RESE
c9a50 52 56 45 44 20 74 6f 20 61 6e 0a 20 20 20 20 2a  RVED to an.    *
c9a60 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  * EXCLUSIVE lock
c9a70 2e 20 54 68 65 20 6e 65 78 74 20 74 69 6d 65 20  . The next time 
c9a80 74 68 65 20 70 72 6f 63 65 73 73 20 74 72 69 65  the process trie
c9a90 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 0a  s to commit the.
c9aa0 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69      ** transacti
c9ab0 6f 6e 20 74 68 65 20 6d 2d 6a 20 6e 61 6d 65 20  on the m-j name 
c9ac0 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 64  will have alread
c9ad0 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 2e 0a  y been written..
c9ae0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
c9af0 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65  pPager->setMaste
c9b00 72 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  r ){.      asser
c9b10 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t( pPager->journ
c9b20 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20  alOpen );.      
c9b30 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f  rc = pager_incr_
c9b40 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50  changecounter(pP
c9b50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  ager, 0);.      
c9b60 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
c9b70 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78  K ) goto sync_ex
c9b80 69 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  it;.#ifndef SQLI
c9b90 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
c9ba0 55 4d 0a 20 20 20 20 20 20 69 66 28 20 6e 54 72  UM.      if( nTr
c9bb0 75 6e 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  unc!=0 ){.      
c9bc0 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61    /* If this tra
c9bd0 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64  nsaction has mad
c9be0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  e the database s
c9bf0 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c  maller, then all
c9c00 20 70 61 67 65 73 0a 20 20 20 20 20 20 20 20 2a   pages.        *
c9c10 2a 20 62 65 69 6e 67 20 64 69 73 63 61 72 64 65  * being discarde
c9c20 64 20 62 79 20 74 68 65 20 74 72 75 6e 63 61 74  d by the truncat
c9c30 69 6f 6e 20 6d 75 73 74 20 62 65 20 77 72 69 74  ion must be writ
c9c40 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
c9c50 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69  al.        ** fi
c9c60 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  le..        */. 
c9c70 20 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 0a 20         Pgno i;. 
c9c80 20 20 20 20 20 20 20 69 6e 74 20 69 53 6b 69 70         int iSkip
c9c90 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f   = PAGER_MJ_PGNO
c9ca0 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
c9cb0 20 20 66 6f 72 28 20 69 3d 6e 54 72 75 6e 63 2b    for( i=nTrunc+
c9cc0 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d 3e 6f 72  1; i<=pPager->or
c9cd0 69 67 44 62 53 69 7a 65 3b 20 69 2b 2b 20 29 7b  igDbSize; i++ ){
c9ce0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
c9cf0 28 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72  (pPager->aInJour
c9d00 6e 61 6c 5b 69 2f 38 5d 20 26 20 28 31 3c 3c 28  nal[i/8] & (1<<(
c9d10 69 26 37 29 29 29 20 26 26 20 69 21 3d 69 53 6b  i&7))) && i!=iSk
c9d20 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ip ){.          
c9d30 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
c9d40 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 69  gerGet(pPager, i
c9d50 2c 20 26 70 50 67 29 3b 0a 20 20 20 20 20 20 20  , &pPg);.       
c9d60 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
c9d70 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79  ITE_OK ) goto sy
c9d80 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20  nc_exit;.       
c9d90 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
c9da0 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 29  3PagerWrite(pPg)
c9db0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
c9dc0 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
c9dd0 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pPg);.          
c9de0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
c9df0 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f  _OK ) goto sync_
c9e00 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 20 20  exit;.          
c9e10 7d 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20  }.        } .   
c9e20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
c9e30 20 20 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74    rc = writeMast
c9e40 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  erJournal(pPager
c9e50 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
c9e60 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
c9e70 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f  _OK ) goto sync_
c9e80 65 78 69 74 3b 0a 20 20 20 20 20 20 72 63 20 3d  exit;.      rc =
c9e90 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61   syncJournal(pPa
c9ea0 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ger);.    }.    
c9eb0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
c9ec0 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78  K ) goto sync_ex
c9ed0 69 74 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  it;..#ifndef SQL
c9ee0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
c9ef0 55 55 4d 0a 20 20 20 20 69 66 28 20 6e 54 72 75  UUM.    if( nTru
c9f00 6e 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  nc!=0 ){.      r
c9f10 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
c9f20 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c  Truncate(pPager,
c9f30 20 6e 54 72 75 6e 63 29 3b 0a 20 20 20 20 20 20   nTrunc);.      
c9f40 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
c9f50 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78  K ) goto sync_ex
c9f60 69 74 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  it;.    }.#endif
c9f70 0a 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61  ..    /* Write a
c9f80 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 74  ll dirty pages t
c9f90 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
c9fa0 69 6c 65 20 2a 2f 0a 20 20 20 20 70 50 67 20 3d  ile */.    pPg =
c9fb0 20 70 61 67 65 72 5f 67 65 74 5f 61 6c 6c 5f 64   pager_get_all_d
c9fc0 69 72 74 79 5f 70 61 67 65 73 28 70 50 61 67 65  irty_pages(pPage
c9fd0 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67  r);.    rc = pag
c9fe0 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73  er_write_pagelis
c9ff0 74 28 70 50 67 29 3b 0a 20 20 20 20 69 66 28 20  t(pPg);.    if( 
ca000 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
ca010 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 50  .      while( pP
ca020 67 20 26 26 20 21 70 50 67 2d 3e 64 69 72 74 79  g && !pPg->dirty
ca030 20 29 7b 20 70 50 67 20 3d 20 70 50 67 2d 3e 70   ){ pPg = pPg->p
ca040 44 69 72 74 79 3b 20 7d 0a 20 20 20 20 20 20 70  Dirty; }.      p
ca050 50 61 67 65 72 2d 3e 70 44 69 72 74 79 20 3d 20  Pager->pDirty = 
ca060 70 50 67 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  pPg;.      goto 
ca070 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d  sync_exit;.    }
ca080 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 44 69  .    pPager->pDi
ca090 72 74 79 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  rty = 0;..    /*
ca0a0 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   Sync the databa
ca0b0 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20  se file. */.    
ca0c0 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53  if( !pPager->noS
ca0d0 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ync ){.      rc 
ca0e0 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
ca0f0 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67  pPager->fd, pPag
ca100 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b  er->sync_flags);
ca110 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41  .    }.    IOTRA
ca120 43 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e  CE(("DBSYNC %p\n
ca130 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20  ", pPager))..   
ca140 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d   pPager->state =
ca150 20 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20   PAGER_SYNCED;. 
ca160 20 7d 65 6c 73 65 20 69 66 28 20 4d 45 4d 44 42   }else if( MEMDB
ca170 20 26 26 20 6e 54 72 75 6e 63 21 3d 30 20 29 7b   && nTrunc!=0 ){
ca180 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
ca190 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 70  3PagerTruncate(p
ca1a0 50 61 67 65 72 2c 20 6e 54 72 75 6e 63 29 3b 0a  Pager, nTrunc);.
ca1b0 20 20 7d 0a 0a 73 79 6e 63 5f 65 78 69 74 3a 0a    }..sync_exit:.
ca1c0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
ca1d0 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29  _IOERR_BLOCKED )
ca1e0 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f 69  {.    /* pager_i
ca1f0 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ncr_changecounte
ca200 72 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74 20  r() may attempt 
ca210 74 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78 63  to obtain an exc
ca220 6c 75 73 69 76 65 0a 20 20 20 20 20 2a 20 6c 6f  lusive.     * lo
ca230 63 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65 20  ck to spill the 
ca240 63 61 63 68 65 20 61 6e 64 20 72 65 74 75 72 6e  cache and return
ca250 20 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e 20   IOERR_BLOCKED. 
ca260 42 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20 20  But since .     
ca270 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68  * there is no ch
ca280 61 6e 63 65 20 74 68 65 20 63 61 63 68 65 20 69  ance the cache i
ca290 73 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c 20  s inconsistent, 
ca2a0 69 74 27 73 0a 20 20 20 20 20 2a 20 62 65 74 74  it's.     * bett
ca2b0 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c  er to return SQL
ca2c0 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 20 2a  ITE_BUSY..     *
ca2d0 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  /.    rc = SQLIT
ca2e0 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 61  E_BUSY;.  }.  pa
ca2f0 67 65 72 4c 65 61 76 65 28 70 50 61 67 65 72 29  gerLeave(pPager)
ca300 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
ca310 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20  .../*.** Commit 
ca320 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 6f 20 74  all changes to t
ca330 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  he database and 
ca340 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 74  release the writ
ca350 65 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  e lock..**.** If
ca360 20 74 68 65 20 63 6f 6d 6d 69 74 20 66 61 69 6c   the commit fail
ca370 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e  s for any reason
ca380 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 74 74  , a rollback att
ca390 65 6d 70 74 20 69 73 20 6d 61 64 65 0a 2a 2a 20  empt is made.** 
ca3a0 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
ca3b0 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  e is returned.  
ca3c0 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 77 6f  If the commit wo
ca3d0 72 6b 65 64 2c 20 53 51 4c 49 54 45 5f 4f 4b 0a  rked, SQLITE_OK.
ca3e0 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  ** is returned..
ca3f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ca400 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
ca410 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
ca420 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
ca430 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48  .  int rc;.  PgH
ca440 64 72 20 2a 70 50 67 3b 0a 0a 20 20 69 66 28 20  dr *pPg;..  if( 
ca450 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
ca460 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50  ){.    return pP
ca470 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
ca480 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d   }.  if( pPager-
ca490 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53  >state<PAGER_RES
ca4a0 45 52 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74  ERVED ){.    ret
ca4b0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
ca4c0 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 45 6e 74  ;.  }.  pagerEnt
ca4d0 65 72 28 70 50 61 67 65 72 29 3b 0a 20 20 50 41  er(pPager);.  PA
ca4e0 47 45 52 54 52 41 43 45 32 28 22 43 4f 4d 4d 49  GERTRACE2("COMMI
ca4f0 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  T %d\n", PAGERID
ca500 28 70 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28  (pPager));.  if(
ca510 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50   MEMDB ){.    pP
ca520 67 20 3d 20 70 61 67 65 72 5f 67 65 74 5f 61 6c  g = pager_get_al
ca530 6c 5f 64 69 72 74 79 5f 70 61 67 65 73 28 70 50  l_dirty_pages(pP
ca540 61 67 65 72 29 3b 0a 20 20 20 20 77 68 69 6c 65  ager);.    while
ca550 28 20 70 50 67 20 29 7b 0a 20 20 20 20 20 20 50  ( pPg ){.      P
ca560 67 48 69 73 74 6f 72 79 20 2a 70 48 69 73 74 20  gHistory *pHist 
ca570 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54 28  = PGHDR_TO_HIST(
ca580 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a 20 20  pPg, pPager);.  
ca590 20 20 20 20 63 6c 65 61 72 48 69 73 74 6f 72 79      clearHistory
ca5a0 28 70 48 69 73 74 29 3b 0a 20 20 20 20 20 20 70  (pHist);.      p
ca5b0 50 67 2d 3e 64 69 72 74 79 20 3d 20 30 3b 0a 20  Pg->dirty = 0;. 
ca5c0 20 20 20 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72       pPg->inJour
ca5d0 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  nal = 0;.      p
ca5e0 48 69 73 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30  Hist->inStmt = 0
ca5f0 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e 6e 65 65  ;.      pPg->nee
ca600 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 20  dSync = 0;.     
ca610 20 70 48 69 73 74 2d 3e 70 50 72 65 76 53 74 6d   pHist->pPrevStm
ca620 74 20 3d 20 70 48 69 73 74 2d 3e 70 4e 65 78 74  t = pHist->pNext
ca630 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Stmt = 0;.      
ca640 70 50 67 20 3d 20 70 50 67 2d 3e 70 44 69 72 74  pPg = pPg->pDirt
ca650 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  y;.    }.    pPa
ca660 67 65 72 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b  ger->pDirty = 0;
ca670 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
ca680 20 20 20 20 66 6f 72 28 70 50 67 3d 70 50 61 67      for(pPg=pPag
ca690 65 72 2d 3e 70 41 6c 6c 3b 20 70 50 67 3b 20 70  er->pAll; pPg; p
ca6a0 50 67 3d 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c  Pg=pPg->pNextAll
ca6b0 29 7b 0a 20 20 20 20 20 20 50 67 48 69 73 74 6f  ){.      PgHisto
ca6c0 72 79 20 2a 70 48 69 73 74 20 3d 20 50 47 48 44  ry *pHist = PGHD
ca6d0 52 5f 54 4f 5f 48 49 53 54 28 70 50 67 2c 20 70  R_TO_HIST(pPg, p
ca6e0 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 61 73  Pager);.      as
ca6f0 73 65 72 74 28 20 21 70 50 67 2d 3e 61 6c 77 61  sert( !pPg->alwa
ca700 79 73 52 6f 6c 6c 62 61 63 6b 20 29 3b 0a 20 20  ysRollback );.  
ca710 20 20 20 20 61 73 73 65 72 74 28 20 21 70 48 69      assert( !pHi
ca720 73 74 2d 3e 70 4f 72 69 67 20 29 3b 0a 20 20 20  st->pOrig );.   
ca730 20 20 20 61 73 73 65 72 74 28 20 21 70 48 69 73     assert( !pHis
ca740 74 2d 3e 70 53 74 6d 74 20 29 3b 0a 20 20 20 20  t->pStmt );.    
ca750 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61  }.#endif.    pPa
ca760 67 65 72 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a  ger->pStmt = 0;.
ca770 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
ca780 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44  e = PAGER_SHARED
ca790 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
ca7a0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73  ITE_OK;.  }.  as
ca7b0 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f  sert( pPager->jo
ca7c0 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c 20 21 70 50  urnalOpen || !pP
ca7d0 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65  ager->dirtyCache
ca7e0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
ca7f0 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
ca800 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 21 70 50  ER_SYNCED || !pP
ca810 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65  ager->dirtyCache
ca820 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72   );.  rc = pager
ca830 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
ca840 28 70 50 61 67 65 72 29 3b 0a 20 20 72 63 20 3d  (pPager);.  rc =
ca850 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
ca860 67 65 72 2c 20 72 63 29 3b 0a 20 20 70 61 67 65  ger, rc);.  page
ca870 72 4c 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a  rLeave(pPager);.
ca880 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
ca890 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61  /*.** Rollback a
ca8a0 6c 6c 20 63 68 61 6e 67 65 73 2e 20 20 54 68 65  ll changes.  The
ca8b0 20 64 61 74 61 62 61 73 65 20 66 61 6c 6c 73 20   database falls 
ca8c0 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48  back to PAGER_SH
ca8d0 41 52 45 44 20 6d 6f 64 65 2e 0a 2a 2a 20 41 6c  ARED mode..** Al
ca8e0 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68  l in-memory cach
ca8f0 65 20 70 61 67 65 73 20 72 65 76 65 72 74 20 74  e pages revert t
ca900 6f 20 74 68 65 69 72 20 6f 72 69 67 69 6e 61 6c  o their original
ca910 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 73 2e 0a   data contents..
ca920 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69  ** The journal i
ca930 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a  s deleted..**.**
ca940 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
ca950 6e 6e 6f 74 20 66 61 69 6c 20 75 6e 6c 65 73 73  nnot fail unless
ca960 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
ca970 65 73 73 20 69 73 20 6e 6f 74 20 66 6f 6c 6c 6f  ess is not follo
ca980 77 69 6e 67 0a 2a 2a 20 74 68 65 20 63 6f 72 72  wing.** the corr
ca990 65 63 74 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 74  ect locking prot
ca9a0 6f 63 6f 6c 20 6f 72 20 75 6e 6c 65 73 73 20 73  ocol or unless s
ca9b0 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f  ome other.** pro
ca9c0 63 65 73 73 20 69 73 20 77 72 69 74 69 6e 67 20  cess is writing 
ca9d0 74 72 61 73 68 20 69 6e 74 6f 20 74 68 65 20 6a  trash into the j
ca9e0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 53 51 4c  ournal file (SQL
ca9f0 49 54 45 5f 43 4f 52 52 55 50 54 29 20 6f 72 0a  ITE_CORRUPT) or.
caa00 2a 2a 20 75 6e 6c 65 73 73 20 61 20 70 72 69 6f  ** unless a prio
caa10 72 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65  r malloc() faile
caa20 64 20 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29  d (SQLITE_NOMEM)
caa30 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 20 65  .  Appropriate e
caa40 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 73 20 61 72  rror.** codes ar
caa50 65 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20 61  e returned for a
caa60 6c 6c 20 74 68 65 73 65 20 6f 63 63 61 73 69 6f  ll these occasio
caa70 6e 73 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 0a  ns.  Otherwise,.
caa80 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
caa90 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
caaa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
caab0 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
caac0 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67  back(Pager *pPag
caad0 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  er){.  int rc;. 
caae0 20 50 41 47 45 52 54 52 41 43 45 32 28 22 52 4f   PAGERTRACE2("RO
caaf0 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41  LLBACK %d\n", PA
cab00 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a  GERID(pPager));.
cab10 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20    if( MEMDB ){. 
cab20 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20     PgHdr *p;.   
cab30 20 66 6f 72 28 70 3d 70 50 61 67 65 72 2d 3e 70   for(p=pPager->p
cab40 41 6c 6c 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  All; p; p=p->pNe
cab50 78 74 41 6c 6c 29 7b 0a 20 20 20 20 20 20 50 67  xtAll){.      Pg
cab60 48 69 73 74 6f 72 79 20 2a 70 48 69 73 74 3b 0a  History *pHist;.
cab70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
cab80 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b  ->alwaysRollback
cab90 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70   );.      if( !p
caba0 2d 3e 64 69 72 74 79 20 29 7b 0a 20 20 20 20 20  ->dirty ){.     
cabb0 20 20 20 61 73 73 65 72 74 28 20 21 28 28 50 67     assert( !((Pg
cabc0 48 69 73 74 6f 72 79 20 2a 29 50 47 48 44 52 5f  History *)PGHDR_
cabd0 54 4f 5f 48 49 53 54 28 70 2c 20 70 50 61 67 65  TO_HIST(p, pPage
cabe0 72 29 29 2d 3e 70 4f 72 69 67 20 29 3b 0a 20 20  r))->pOrig );.  
cabf0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 28        assert( !(
cac00 28 50 67 48 69 73 74 6f 72 79 20 2a 29 50 47 48  (PgHistory *)PGH
cac10 44 52 5f 54 4f 5f 48 49 53 54 28 70 2c 20 70 50  DR_TO_HIST(p, pP
cac20 61 67 65 72 29 29 2d 3e 70 53 74 6d 74 20 29 3b  ager))->pStmt );
cac30 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
cac40 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  e;.      }..    
cac50 20 20 70 48 69 73 74 20 3d 20 50 47 48 44 52 5f    pHist = PGHDR_
cac60 54 4f 5f 48 49 53 54 28 70 2c 20 70 50 61 67 65  TO_HIST(p, pPage
cac70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 48  r);.      if( pH
cac80 69 73 74 2d 3e 70 4f 72 69 67 20 29 7b 0a 20 20  ist->pOrig ){.  
cac90 20 20 20 20 20 20 6d 65 6d 63 70 79 28 50 47 48        memcpy(PGH
caca0 44 52 5f 54 4f 5f 44 41 54 41 28 70 29 2c 20 70  DR_TO_DATA(p), p
cacb0 48 69 73 74 2d 3e 70 4f 72 69 67 2c 20 70 50 61  Hist->pOrig, pPa
cacc0 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  ger->pageSize);.
cacd0 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41          PAGERTRA
cace0 43 45 33 28 22 52 4f 4c 4c 42 41 43 4b 2d 50 41  CE3("ROLLBACK-PA
cacf0 47 45 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20  GE %d of %d\n", 
cad00 70 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44  p->pgno, PAGERID
cad10 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20  (pPager));.     
cad20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
cad30 50 41 47 45 52 54 52 41 43 45 33 28 22 50 41 47  PAGERTRACE3("PAG
cad40 45 20 25 64 20 69 73 20 63 6c 65 61 6e 20 6f 6e  E %d is clean on
cad50 20 25 64 5c 6e 22 2c 20 70 2d 3e 70 67 6e 6f 2c   %d\n", p->pgno,
cad60 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
cad70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
cad80 20 63 6c 65 61 72 48 69 73 74 6f 72 79 28 70 48   clearHistory(pH
cad90 69 73 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 64  ist);.      p->d
cada0 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  irty = 0;.      
cadb0 70 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30  p->inJournal = 0
cadc0 3b 0a 20 20 20 20 20 20 70 48 69 73 74 2d 3e 69  ;.      pHist->i
cadd0 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20  nStmt = 0;.     
cade0 20 70 48 69 73 74 2d 3e 70 50 72 65 76 53 74 6d   pHist->pPrevStm
cadf0 74 20 3d 20 70 48 69 73 74 2d 3e 70 4e 65 78 74  t = pHist->pNext
cae00 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Stmt = 0;.      
cae10 69 66 28 20 70 50 61 67 65 72 2d 3e 78 52 65 69  if( pPager->xRei
cae20 6e 69 74 65 72 20 29 7b 0a 20 20 20 20 20 20 20  niter ){.       
cae30 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74   pPager->xReinit
cae40 65 72 28 70 2c 20 70 50 61 67 65 72 2d 3e 70 61  er(p, pPager->pa
cae50 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d  geSize);.      }
cae60 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
cae70 72 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20  r->pDirty = 0;. 
cae80 20 20 20 70 50 61 67 65 72 2d 3e 70 53 74 6d 74     pPager->pStmt
cae90 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
caea0 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65  ->dbSize = pPage
caeb0 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 3b 0a 20  r->origDbSize;. 
caec0 20 20 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74     pager_truncat
caed0 65 5f 63 61 63 68 65 28 70 50 61 67 65 72 29 3b  e_cache(pPager);
caee0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d  .    pPager->stm
caef0 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20 20 20 20  tInUse = 0;.    
caf00 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
caf10 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a 20 20  PAGER_SHARED;.  
caf20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
caf30 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 61 67 65 72  OK;.  }..  pager
caf40 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20  Enter(pPager);. 
caf50 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 64 69   if( !pPager->di
caf60 72 74 79 43 61 63 68 65 20 7c 7c 20 21 70 50 61  rtyCache || !pPa
caf70 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e  ger->journalOpen
caf80 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   ){.    rc = pag
caf90 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  er_end_transacti
cafa0 6f 6e 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  on(pPager);.    
cafb0 70 61 67 65 72 4c 65 61 76 65 28 70 50 61 67 65  pagerLeave(pPage
cafc0 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  r);.    return r
cafd0 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  c;.  }..  if( pP
cafe0 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26  ager->errCode &&
caff0 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
cb000 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b  !=SQLITE_FULL ){
cb010 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
cb020 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58  >state>=PAGER_EX
cb030 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20  CLUSIVE ){.     
cb040 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
cb050 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20  pPager, 0);.    
cb060 7d 0a 20 20 20 20 70 61 67 65 72 4c 65 61 76 65  }.    pagerLeave
cb070 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 72 65  (pPager);.    re
cb080 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
cb090 43 6f 64 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Code;.  }.  if( 
cb0a0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
cb0b0 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b  AGER_RESERVED ){
cb0c0 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20  .    int rc2;.  
cb0d0 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
cb0e0 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29  yback(pPager, 0)
cb0f0 3b 0a 20 20 20 20 72 63 32 20 3d 20 70 61 67 65  ;.    rc2 = page
cb100 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
cb110 6e 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  n(pPager);.    i
cb120 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
cb130 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   ){.      rc = r
cb140 63 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  c2;.    }.  }els
cb150 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  e{.    rc = page
cb160 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65  r_playback(pPage
cb170 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20  r, 0);.  }.  /* 
cb180 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67  pager_reset(pPag
cb190 65 72 29 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72  er); */.  pPager
cb1a0 2d 3e 64 62 53 69 7a 65 20 3d 20 2d 31 3b 0a 0a  ->dbSize = -1;..
cb1b0 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72    /* If an error
cb1c0 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61   occurs during a
cb1d0 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 63 61   ROLLBACK, we ca
cb1e0 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 73  n no longer trus
cb1f0 74 20 74 68 65 20 70 61 67 65 72 0a 20 20 2a 2a  t the pager.  **
cb200 20 63 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20   cache. So call 
cb210 70 61 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e  pager_error() on
cb220 20 74 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20   the way out to 
cb230 6d 61 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a  make any error .
cb240 20 20 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 2e    ** persistent.
cb250 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67  .  */.  rc = pag
cb260 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
cb270 20 72 63 29 3b 0a 20 20 70 61 67 65 72 4c 65 61   rc);.  pagerLea
cb280 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65  ve(pPager);.  re
cb290 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
cb2a0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
cb2b0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
cb2c0 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61  le is opened rea
cb2d0 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20  d-only.  Return 
cb2e0 46 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20  FALSE.** if the 
cb2f0 64 61 74 61 62 61 73 65 20 69 73 20 28 69 6e 20  database is (in 
cb300 74 68 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65  theory) writable
cb310 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
cb320 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
cb330 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50  agerIsreadonly(P
cb340 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
cb350 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
cb360 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a  readOnly;.}../*.
cb370 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
cb380 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63  mber of referenc
cb390 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e  es to the pager.
cb3a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cb3b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
cb3c0 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65  gerRefcount(Page
cb3d0 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
cb3e0 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 52 65  turn pPager->nRe
cb3f0 66 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  f;.}..#ifdef SQL
cb400 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54  ITE_TEST./*.** T
cb410 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
cb420 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
cb430 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c  and analysis onl
cb440 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
cb450 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 74 65  VATE int *sqlite
cb460 33 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65  3PagerStats(Page
cb470 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 74  r *pPager){.  st
cb480 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a  atic int a[11];.
cb490 20 20 61 5b 30 5d 20 3d 20 70 50 61 67 65 72 2d    a[0] = pPager-
cb4a0 3e 6e 52 65 66 3b 0a 20 20 61 5b 31 5d 20 3d 20  >nRef;.  a[1] = 
cb4b0 70 50 61 67 65 72 2d 3e 6e 50 61 67 65 3b 0a 20  pPager->nPage;. 
cb4c0 20 61 5b 32 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[2] = pPager->
cb4d0 6d 78 50 61 67 65 3b 0a 20 20 61 5b 33 5d 20 3d  mxPage;.  a[3] =
cb4e0 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b   pPager->dbSize;
cb4f0 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72  .  a[4] = pPager
cb500 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20  ->state;.  a[5] 
cb510 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  = pPager->errCod
cb520 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67  e;.  a[6] = pPag
cb530 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 5d  er->nHit;.  a[7]
cb540 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73   = pPager->nMiss
cb550 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 2f  ;.  a[8] = 0;  /
cb560 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 50 61  * Used to be pPa
cb570 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20  ger->nOvfl */.  
cb580 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e  a[9] = pPager->n
cb590 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d 20  Read;.  a[10] = 
cb5a0 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b 0a  pPager->nWrite;.
cb5b0 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 65    return a;.}.#e
cb5c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  ndif../*.** Set 
cb5d0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 6f  the statement ro
cb5e0 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 2e 0a 2a 2a  llback point..**
cb5f0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
cb600 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65   should be calle
cb610 64 20 77 69 74 68 20 74 68 65 20 74 72 61 6e 73  d with the trans
cb620 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 61  action journal a
cb630 6c 72 65 61 64 79 0a 2a 2a 20 6f 70 65 6e 2e 20  lready.** open. 
cb640 20 41 20 6e 65 77 20 73 74 61 74 65 6d 65 6e 74   A new statement
cb650 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 72 65 61   journal is crea
cb660 74 65 64 20 74 68 61 74 20 63 61 6e 20 62 65 20  ted that can be 
cb670 75 73 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  used to rollback
cb680 0a 2a 2a 20 63 68 61 6e 67 65 73 20 6f 66 20 61  .** changes of a
cb690 20 73 69 6e 67 6c 65 20 53 51 4c 20 63 6f 6d 6d   single SQL comm
cb6a0 61 6e 64 20 77 69 74 68 69 6e 20 61 20 6c 61 72  and within a lar
cb6b0 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ger transaction.
cb6c0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
cb6d0 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28 50 61  agerStmtBegin(Pa
cb6e0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
cb6f0 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
cb700 28 20 21 70 50 61 67 65 72 2d 3e 73 74 6d 74 49  ( !pPager->stmtI
cb710 6e 55 73 65 20 29 3b 0a 20 20 61 73 73 65 72 74  nUse );.  assert
cb720 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
cb730 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b  =PAGER_SHARED );
cb740 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
cb750 72 2d 3e 64 62 53 69 7a 65 3e 3d 30 20 29 3b 0a  r->dbSize>=0 );.
cb760 20 20 50 41 47 45 52 54 52 41 43 45 32 28 22 53    PAGERTRACE2("S
cb770 54 4d 54 2d 42 45 47 49 4e 20 25 64 5c 6e 22 2c  TMT-BEGIN %d\n",
cb780 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
cb790 29 3b 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29  );.  if( MEMDB )
cb7a0 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  {.    pPager->st
cb7b0 6d 74 49 6e 55 73 65 20 3d 20 31 3b 0a 20 20 20  mtInUse = 1;.   
cb7c0 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a   pPager->stmtSiz
cb7d0 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69  e = pPager->dbSi
cb7e0 7a 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ze;.    return S
cb7f0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
cb800 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75  if( !pPager->jou
cb810 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20  rnalOpen ){.    
cb820 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f  pPager->stmtAuto
cb830 6f 70 65 6e 20 3d 20 31 3b 0a 20 20 20 20 72 65  open = 1;.    re
cb840 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
cb850 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50    }.  assert( pP
cb860 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
cb870 6e 20 29 3b 0a 20 20 70 61 67 65 72 4c 65 61 76  n );.  pagerLeav
cb880 65 28 70 50 61 67 65 72 29 3b 0a 20 20 70 50 61  e(pPager);.  pPa
cb890 67 65 72 2d 3e 61 49 6e 53 74 6d 74 20 3d 20 73  ger->aInStmt = s
cb8a0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
cb8b0 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
cb8c0 2f 38 20 2b 20 31 20 29 3b 0a 20 20 70 61 67 65  /8 + 1 );.  page
cb8d0 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a  rEnter(pPager);.
cb8e0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61 49    if( pPager->aI
cb8f0 6e 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20  nStmt==0 ){.    
cb900 2f 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b  /* sqlite3OsLock
cb910 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 41  (pPager->fd, SHA
cb920 52 45 44 5f 4c 4f 43 4b 29 3b 20 2a 2f 0a 20 20  RED_LOCK); */.  
cb930 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cb940 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 69 66 6e 64  NOMEM;.  }.#ifnd
cb950 65 66 20 4e 44 45 42 55 47 0a 20 20 72 63 20 3d  ef NDEBUG.  rc =
cb960 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
cb970 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ze(pPager->jfd, 
cb980 26 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69  &pPager->stmtJSi
cb990 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  ze);.  if( rc ) 
cb9a0 67 6f 74 6f 20 73 74 6d 74 5f 62 65 67 69 6e 5f  goto stmt_begin_
cb9b0 66 61 69 6c 65 64 3b 0a 20 20 61 73 73 65 72 74  failed;.  assert
cb9c0 28 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53  ( pPager->stmtJS
cb9d0 69 7a 65 20 3d 3d 20 70 50 61 67 65 72 2d 3e 6a  ize == pPager->j
cb9e0 6f 75 72 6e 61 6c 4f 66 66 20 29 3b 0a 23 65 6e  ournalOff );.#en
cb9f0 64 69 66 0a 20 20 70 50 61 67 65 72 2d 3e 73 74  dif.  pPager->st
cba00 6d 74 4a 53 69 7a 65 20 3d 20 70 50 61 67 65 72  mtJSize = pPager
cba10 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20  ->journalOff;.  
cba20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65  pPager->stmtSize
cba30 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
cba40 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d  e;.  pPager->stm
cba50 74 48 64 72 4f 66 66 20 3d 20 30 3b 0a 20 20 70  tHdrOff = 0;.  p
cba60 50 61 67 65 72 2d 3e 73 74 6d 74 43 6b 73 75 6d  Pager->stmtCksum
cba70 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d   = pPager->cksum
cba80 49 6e 69 74 3b 0a 20 20 69 66 28 20 21 70 50 61  Init;.  if( !pPa
cba90 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 29 7b  ger->stmtOpen ){
cbaa0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
cbab0 33 50 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70  3PagerOpentemp(p
cbac0 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61  Pager->pVfs, pPa
cbad0 67 65 72 2d 3e 73 74 66 64 2c 20 70 50 61 67 65  ger->stfd, pPage
cbae0 72 2d 3e 7a 53 74 6d 74 4a 72 6e 6c 2c 0a 20 20  r->zStmtJrnl,.  
cbaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cbb00 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
cbb10 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e  TE_OPEN_SUBJOURN
cbb20 41 4c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  AL);.    if( rc 
cbb30 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 74  ){.      goto st
cbb40 6d 74 5f 62 65 67 69 6e 5f 66 61 69 6c 65 64 3b  mt_begin_failed;
cbb50 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
cbb60 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 31 3b  r->stmtOpen = 1;
cbb70 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d  .    pPager->stm
cbb80 74 4e 52 65 63 20 3d 20 30 3b 0a 20 20 7d 0a 20  tNRec = 0;.  }. 
cbb90 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55   pPager->stmtInU
cbba0 73 65 20 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e  se = 1;.  return
cbbb0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 0a 73 74   SQLITE_OK;. .st
cbbc0 6d 74 5f 62 65 67 69 6e 5f 66 61 69 6c 65 64 3a  mt_begin_failed:
cbbd0 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61  .  if( pPager->a
cbbe0 49 6e 53 74 6d 74 20 29 7b 0a 20 20 20 20 73 71  InStmt ){.    sq
cbbf0 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65  lite3_free(pPage
cbc00 72 2d 3e 61 49 6e 53 74 6d 74 29 3b 0a 20 20 20  r->aInStmt);.   
cbc10 20 70 50 61 67 65 72 2d 3e 61 49 6e 53 74 6d 74   pPager->aInStmt
cbc20 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
cbc30 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
cbc40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
cbc50 74 65 33 50 61 67 65 72 53 74 6d 74 42 65 67 69  te3PagerStmtBegi
cbc60 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  n(Pager *pPager)
cbc70 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 61  {.  int rc;.  pa
cbc80 67 65 72 45 6e 74 65 72 28 70 50 61 67 65 72 29  gerEnter(pPager)
cbc90 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 53 74  ;.  rc = pagerSt
cbca0 6d 74 42 65 67 69 6e 28 70 50 61 67 65 72 29 3b  mtBegin(pPager);
cbcb0 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28 70 50  .  pagerLeave(pP
cbcc0 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  ager);.  return 
cbcd0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  rc;.}../*.** Com
cbce0 6d 69 74 20 61 20 73 74 61 74 65 6d 65 6e 74 2e  mit a statement.
cbcf0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cbd00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
cbd10 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28 50 61  gerStmtCommit(Pa
cbd20 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
cbd30 70 61 67 65 72 45 6e 74 65 72 28 70 50 61 67 65  pagerEnter(pPage
cbd40 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  r);.  if( pPager
cbd50 2d 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20  ->stmtInUse ){. 
cbd60 20 20 20 50 67 48 64 72 20 2a 70 50 67 2c 20 2a     PgHdr *pPg, *
cbd70 70 4e 65 78 74 3b 0a 20 20 20 20 50 41 47 45 52  pNext;.    PAGER
cbd80 54 52 41 43 45 32 28 22 53 54 4d 54 2d 43 4f 4d  TRACE2("STMT-COM
cbd90 4d 49 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  MIT %d\n", PAGER
cbda0 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20  ID(pPager));.   
cbdb0 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20   if( !MEMDB ){. 
cbdc0 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 4f       /* sqlite3O
cbdd0 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  sTruncate(pPager
cbde0 2d 3e 73 74 66 64 2c 20 30 29 3b 20 2a 2f 0a 20  ->stfd, 0); */. 
cbdf0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
cbe00 65 28 20 70 50 61 67 65 72 2d 3e 61 49 6e 53 74  e( pPager->aInSt
cbe10 6d 74 20 29 3b 0a 20 20 20 20 20 20 70 50 61 67  mt );.      pPag
cbe20 65 72 2d 3e 61 49 6e 53 74 6d 74 20 3d 20 30 3b  er->aInStmt = 0;
cbe30 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
cbe40 20 20 66 6f 72 28 70 50 67 3d 70 50 61 67 65 72    for(pPg=pPager
cbe50 2d 3e 70 53 74 6d 74 3b 20 70 50 67 3b 20 70 50  ->pStmt; pPg; pP
cbe60 67 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  g=pNext){.      
cbe70 20 20 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69    PgHistory *pHi
cbe80 73 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49  st = PGHDR_TO_HI
cbe90 53 54 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b  ST(pPg, pPager);
cbea0 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d  .        pNext =
cbeb0 20 70 48 69 73 74 2d 3e 70 4e 65 78 74 53 74 6d   pHist->pNextStm
cbec0 74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  t;.        asser
cbed0 74 28 20 70 48 69 73 74 2d 3e 69 6e 53 74 6d 74  t( pHist->inStmt
cbee0 20 29 3b 0a 20 20 20 20 20 20 20 20 70 48 69 73   );.        pHis
cbef0 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20  t->inStmt = 0;. 
cbf00 20 20 20 20 20 20 20 70 48 69 73 74 2d 3e 70 50         pHist->pP
cbf10 72 65 76 53 74 6d 74 20 3d 20 70 48 69 73 74 2d  revStmt = pHist-
cbf20 3e 70 4e 65 78 74 53 74 6d 74 20 3d 20 30 3b 0a  >pNextStmt = 0;.
cbf30 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
cbf40 66 72 65 65 28 70 48 69 73 74 2d 3e 70 53 74 6d  free(pHist->pStm
cbf50 74 29 3b 0a 20 20 20 20 20 20 20 20 70 48 69 73  t);.        pHis
cbf60 74 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  t->pStmt = 0;.  
cbf70 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
cbf80 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63  pPager->stmtNRec
cbf90 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
cbfa0 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b  ->stmtInUse = 0;
cbfb0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 53 74  .    pPager->pSt
cbfc0 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50  mt = 0;.  }.  pP
cbfd0 61 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70  ager->stmtAutoop
cbfe0 65 6e 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 4c  en = 0;.  pagerL
cbff0 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20  eave(pPager);.  
cc000 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
cc010 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62  ;.}../*.** Rollb
cc020 61 63 6b 20 61 20 73 74 61 74 65 6d 65 6e 74 2e  ack a statement.
cc030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cc040 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
cc050 67 65 72 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28  gerStmtRollback(
cc060 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
cc070 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 61 67 65    int rc;.  page
cc080 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a  rEnter(pPager);.
cc090 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
cc0a0 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 50  mtInUse ){.    P
cc0b0 41 47 45 52 54 52 41 43 45 32 28 22 53 54 4d 54  AGERTRACE2("STMT
cc0c0 2d 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c  -ROLLBACK %d\n",
cc0d0 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
cc0e0 29 3b 0a 20 20 20 20 69 66 28 20 4d 45 4d 44 42  );.    if( MEMDB
cc0f0 20 29 7b 0a 20 20 20 20 20 20 50 67 48 64 72 20   ){.      PgHdr 
cc100 2a 70 50 67 3b 0a 20 20 20 20 20 20 50 67 48 69  *pPg;.      PgHi
cc110 73 74 6f 72 79 20 2a 70 48 69 73 74 3b 0a 20 20  story *pHist;.  
cc120 20 20 20 20 66 6f 72 28 70 50 67 3d 70 50 61 67      for(pPg=pPag
cc130 65 72 2d 3e 70 53 74 6d 74 3b 20 70 50 67 3b 20  er->pStmt; pPg; 
cc140 70 50 67 3d 70 48 69 73 74 2d 3e 70 4e 65 78 74  pPg=pHist->pNext
cc150 53 74 6d 74 29 7b 0a 20 20 20 20 20 20 20 20 70  Stmt){.        p
cc160 48 69 73 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f  Hist = PGHDR_TO_
cc170 48 49 53 54 28 70 50 67 2c 20 70 50 61 67 65 72  HIST(pPg, pPager
cc180 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
cc190 48 69 73 74 2d 3e 70 53 74 6d 74 20 29 7b 0a 20  Hist->pStmt ){. 
cc1a0 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
cc1b0 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50  PGHDR_TO_DATA(pP
cc1c0 67 29 2c 20 70 48 69 73 74 2d 3e 70 53 74 6d 74  g), pHist->pStmt
cc1d0 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
cc1e0 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ze);.          s
cc1f0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 69 73  qlite3_free(pHis
cc200 74 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 20  t->pStmt);.     
cc210 20 20 20 20 20 70 48 69 73 74 2d 3e 70 53 74 6d       pHist->pStm
cc220 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  t = 0;.        }
cc230 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
cc240 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
cc250 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65  pPager->stmtSize
cc260 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 74 72  ;.      pager_tr
cc270 75 6e 63 61 74 65 5f 63 61 63 68 65 28 70 50 61  uncate_cache(pPa
cc280 67 65 72 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ger);.      rc =
cc290 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
cc2a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
cc2b0 3d 20 70 61 67 65 72 5f 73 74 6d 74 5f 70 6c 61  = pager_stmt_pla
cc2c0 79 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20  yback(pPager);. 
cc2d0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
cc2e0 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28  PagerStmtCommit(
cc2f0 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65  pPager);.  }else
cc300 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
cc310 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 67  E_OK;.  }.  pPag
cc320 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 65 6e  er->stmtAutoopen
cc330 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 4c 65 61   = 0;.  pagerLea
cc340 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65  ve(pPager);.  re
cc350 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
cc360 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c  * Return the ful
cc370 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68  l pathname of th
cc380 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
cc390 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cc3a0 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
cc3b0 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e  qlite3PagerFilen
cc3c0 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ame(Pager *pPage
cc3d0 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61  r){.  return pPa
cc3e0 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a  ger->zFilename;.
cc3f0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
cc400 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72  the VFS structur
cc410 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 2e  e for the pager.
cc420 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cc430 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  TE const sqlite3
cc440 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67  _vfs *sqlite3Pag
cc450 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50 61  erVfs(Pager *pPa
cc460 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ger){.  return p
cc470 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a  Pager->pVfs;.}..
cc480 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
cc490 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72   file handle for
cc4a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
cc4b0 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  le associated.**
cc4c0 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72 2e   with the pager.
cc4d0 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65 74    This might ret
cc4e0 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20  urn NULL if the 
cc4f0 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 20  file has.** not 
cc500 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e  yet been opened.
cc510 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cc520 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  TE sqlite3_file 
cc530 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c  *sqlite3PagerFil
cc540 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
cc550 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
cc560 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r->fd;.}../*.** 
cc570 52 65 74 75 72 6e 20 74 68 65 20 64 69 72 65 63  Return the direc
cc580 74 6f 72 79 20 6f 66 20 74 68 65 20 64 61 74 61  tory of the data
cc590 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51  base file..*/.SQ
cc5a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
cc5b0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
cc5c0 50 61 67 65 72 44 69 72 6e 61 6d 65 28 50 61 67  PagerDirname(Pag
cc5d0 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
cc5e0 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a 44  eturn pPager->zD
cc5f0 69 72 65 63 74 6f 72 79 3b 0a 7d 0a 0a 2f 2a 0a  irectory;.}../*.
cc600 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75  ** Return the fu
cc610 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74  ll pathname of t
cc620 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
cc630 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cc640 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
cc650 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
cc660 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50  alname(Pager *pP
cc670 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
cc680 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
cc690 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
cc6a0 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63 28  n true if fsync(
cc6b0 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61  ) calls are disa
cc6c0 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70 61  bled for this pa
cc6d0 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c  ger.  Return FAL
cc6e0 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29  SE.** if fsync()
cc6f0 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20 6e  s are executed n
cc700 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  ormally..*/.SQLI
cc710 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
cc720 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e  qlite3PagerNosyn
cc730 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  c(Pager *pPager)
cc740 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
cc750 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69  r->noSync;.}..#i
cc760 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
cc770 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20  CODEC./*.** Set 
cc780 74 68 65 20 63 6f 64 65 63 20 66 6f 72 20 74 68  the codec for th
cc790 69 73 20 70 61 67 65 72 0a 2a 2f 0a 53 51 4c 49  is pager.*/.SQLI
cc7a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
cc7b0 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
cc7c0 6f 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a 70  odec(.  Pager *p
cc7d0 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28  Pager,.  void *(
cc7e0 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76  *xCodec)(void*,v
cc7f0 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a  oid*,Pgno,int),.
cc800 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 41 72    void *pCodecAr
cc810 67 0a 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78  g.){.  pPager->x
cc820 43 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a  Codec = xCodec;.
cc830 20 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63    pPager->pCodec
cc840 41 72 67 20 3d 20 70 43 6f 64 65 63 41 72 67 3b  Arg = pCodecArg;
cc850 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
cc860 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
cc870 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
cc880 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50  Move the page pP
cc890 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67  g to location pg
cc8a0 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20  no in the file. 
cc8b0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
cc8c0 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63  t be no referenc
cc8d0 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70  es to the page p
cc8e0 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65  reviously locate
cc8f0 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68  d at.** pgno (wh
cc900 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f  ich we call pPgO
cc910 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20  ld) though that 
cc920 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20  page is allowed 
cc930 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68  to be.** in cach
cc940 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20  e.  If the page 
cc950 70 72 65 76 69 6f 75 73 20 6c 6f 63 61 74 65 64  previous located
cc960 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20   at pgno is not 
cc970 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68  already.** in th
cc980 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
cc990 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75  al, it is not pu
cc9a0 74 20 74 68 65 72 65 20 62 79 20 62 79 20 74 68  t there by by th
cc9b0 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a  is routine..**.*
cc9c0 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20  * References to 
cc9d0 74 68 65 20 70 61 67 65 20 70 50 67 20 72 65 6d  the page pPg rem
cc9e0 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74  ain valid. Updat
cc9f0 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d  ing any.** meta-
cca00 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  data associated 
cca10 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64  with pPg (i.e. d
cca20 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68  ata stored in th
cca30 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a  e nExtra bytes.*
cca40 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e  * allocated alon
cca50 67 20 77 69 74 68 20 74 68 65 20 70 61 67 65 29  g with the page)
cca60 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69   is the responsi
cca70 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61  bility of the ca
cca80 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72  ller..**.** A tr
cca90 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
ccaa0 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 74 68  e active when th
ccab0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
ccac0 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f  lled. It used to
ccad0 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20   be.** required 
ccae0 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74  that a statement
ccaf0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
ccb00 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74   not active, but
ccb10 20 74 68 69 73 20 72 65 73 74 72 69 63 74 69 6f   this restrictio
ccb20 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65  n.** has been re
ccb30 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 49 4e  moved (CREATE IN
ccb40 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76  DEX needs to mov
ccb50 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 61 20  e a page when a 
ccb60 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61  statement.** tra
ccb70 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
ccb80 76 65 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ve)..*/.SQLITE_P
ccb90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ccba0 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28  e3PagerMovepage(
ccbb0 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 44  Pager *pPager, D
ccbc0 62 50 61 67 65 20 2a 70 50 67 2c 20 50 67 6e 6f  bPage *pPg, Pgno
ccbd0 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20   pgno){.  PgHdr 
ccbe0 2a 70 50 67 4f 6c 64 3b 20 20 2f 2a 20 54 68 65  *pPgOld;  /* The
ccbf0 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72   page being over
ccc00 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 69 6e  written. */.  in
ccc10 74 20 68 3b 0a 20 20 50 67 6e 6f 20 6e 65 65 64  t h;.  Pgno need
ccc20 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b 0a 0a 20  SyncPgno = 0;.. 
ccc30 20 70 61 67 65 72 45 6e 74 65 72 28 70 50 61 67   pagerEnter(pPag
ccc40 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  er);.  assert( p
ccc50 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 0a 20  Pg->nRef>0 );.. 
ccc60 20 50 41 47 45 52 54 52 41 43 45 35 28 22 4d 4f   PAGERTRACE5("MO
ccc70 56 45 20 25 64 20 70 61 67 65 20 25 64 20 28 6e  VE %d page %d (n
ccc80 65 65 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65  eedSync=%d) move
ccc90 73 20 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20  s to %d\n", .   
ccca0 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65     PAGERID(pPage
cccb0 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70  r), pPg->pgno, p
cccc0 50 67 2d 3e 6e 65 65 64 53 79 6e 63 2c 20 70 67  Pg->needSync, pg
cccd0 6e 6f 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28  no);.  IOTRACE((
ccce0 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64 5c 6e  "MOVE %p %d %d\n
cccf0 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e  ", pPager, pPg->
ccd00 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 20  pgno, pgno))..  
ccd10 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74 65 6e  pager_get_conten
ccd20 74 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 50  t(pPg);.  if( pP
ccd30 67 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20  g->needSync ){. 
ccd40 20 20 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20     needSyncPgno 
ccd50 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20  = pPg->pgno;.   
ccd60 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 69 6e   assert( pPg->in
ccd70 4a 6f 75 72 6e 61 6c 20 7c 7c 20 28 69 6e 74 29  Journal || (int)
ccd80 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f 72 69  pgno>pPager->ori
ccd90 67 44 62 53 69 7a 65 20 29 3b 0a 20 20 20 20 61  gDbSize );.    a
ccda0 73 73 65 72 74 28 20 70 50 67 2d 3e 64 69 72 74  ssert( pPg->dirt
ccdb0 79 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  y );.    assert(
ccdc0 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
ccdd0 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55  c );.  }..  /* U
ccde0 6e 6c 69 6e 6b 20 70 50 67 20 66 72 6f 6d 20 69  nlink pPg from i
ccdf0 74 27 73 20 68 61 73 68 2d 63 68 61 69 6e 20 2a  t's hash-chain *
cce00 2f 0a 20 20 75 6e 6c 69 6e 6b 48 61 73 68 43 68  /.  unlinkHashCh
cce10 61 69 6e 28 70 50 61 67 65 72 2c 20 70 50 67 29  ain(pPager, pPg)
cce20 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  ;..  /* If the c
cce30 61 63 68 65 20 63 6f 6e 74 61 69 6e 73 20 61 20  ache contains a 
cce40 70 61 67 65 20 77 69 74 68 20 70 61 67 65 2d 6e  page with page-n
cce50 75 6d 62 65 72 20 70 67 6e 6f 2c 20 72 65 6d 6f  umber pgno, remo
cce60 76 65 20 69 74 0a 20 20 2a 2a 20 66 72 6f 6d 20  ve it.  ** from 
cce70 69 74 27 73 20 68 61 73 68 20 63 68 61 69 6e 2e  it's hash chain.
cce80 20 41 6c 73 6f 2c 20 69 66 20 74 68 65 20 50 67   Also, if the Pg
cce90 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 77 61 73  Hdr.needSync was
ccea0 20 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20 70   set for .  ** p
cceb0 61 67 65 20 70 67 6e 6f 20 62 65 66 6f 72 65 20  age pgno before 
ccec0 74 68 65 20 27 6d 6f 76 65 27 20 6f 70 65 72 61  the 'move' opera
cced0 74 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20 74  tion, it needs t
ccee0 6f 20 62 65 20 72 65 74 61 69 6e 65 64 20 0a 20  o be retained . 
ccef0 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 61 67 65   ** for the page
ccf00 20 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a 20 20   moved there..  
ccf10 2a 2f 0a 20 20 70 50 67 2d 3e 6e 65 65 64 53 79  */.  pPg->needSy
ccf20 6e 63 20 3d 20 30 3b 0a 20 20 70 50 67 4f 6c 64  nc = 0;.  pPgOld
ccf30 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28   = pager_lookup(
ccf40 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20  pPager, pgno);. 
ccf50 20 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20   if( pPgOld ){. 
ccf60 20 20 20 61 73 73 65 72 74 28 20 70 50 67 4f 6c     assert( pPgOl
ccf70 64 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20  d->nRef==0 );.  
ccf80 20 20 75 6e 6c 69 6e 6b 48 61 73 68 43 68 61 69    unlinkHashChai
ccf90 6e 28 70 50 61 67 65 72 2c 20 70 50 67 4f 6c 64  n(pPager, pPgOld
ccfa0 29 3b 0a 20 20 20 20 6d 61 6b 65 43 6c 65 61 6e  );.    makeClean
ccfb0 28 70 50 67 4f 6c 64 29 3b 0a 20 20 20 20 70 50  (pPgOld);.    pP
ccfc0 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 70 50  g->needSync = pP
ccfd0 67 4f 6c 64 2d 3e 6e 65 65 64 53 79 6e 63 3b 0a  gOld->needSync;.
ccfe0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 67    }else{.    pPg
ccff0 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  ->needSync = 0;.
cd000 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72    }.  if( pPager
cd010 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 20 26 26 20  ->aInJournal && 
cd020 28 69 6e 74 29 70 67 6e 6f 3c 3d 70 50 61 67 65  (int)pgno<=pPage
cd030 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 7b  r->origDbSize ){
cd040 0a 20 20 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72  .    pPg->inJour
cd050 6e 61 6c 20 3d 20 20 28 70 50 61 67 65 72 2d 3e  nal =  (pPager->
cd060 61 49 6e 4a 6f 75 72 6e 61 6c 5b 70 67 6e 6f 2f  aInJournal[pgno/
cd070 38 5d 20 26 20 28 31 3c 3c 28 70 67 6e 6f 26 37  8] & (1<<(pgno&7
cd080 29 29 29 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b  )))!=0;.  }else{
cd090 0a 20 20 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72  .    pPg->inJour
cd0a0 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 61 73 73  nal = 0;.    ass
cd0b0 65 72 74 28 20 70 50 67 2d 3e 6e 65 65 64 53 79  ert( pPg->needSy
cd0c0 6e 63 3d 3d 30 20 7c 7c 20 28 69 6e 74 29 70 67  nc==0 || (int)pg
cd0d0 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f 72 69 67 44  no>pPager->origD
cd0e0 62 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 0a 20 20  bSize );.  }..  
cd0f0 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61  /* Change the pa
cd100 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 50  ge number for pP
cd110 67 20 61 6e 64 20 69 6e 73 65 72 74 20 69 74 20  g and insert it 
cd120 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 68 61 73  into the new has
cd130 68 2d 63 68 61 69 6e 2e 20 2a 2f 0a 20 20 61 73  h-chain. */.  as
cd140 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b  sert( pgno!=0 );
cd150 0a 20 20 70 50 67 2d 3e 70 67 6e 6f 20 3d 20 70  .  pPg->pgno = p
cd160 67 6e 6f 3b 0a 20 20 68 20 3d 20 70 67 6e 6f 20  gno;.  h = pgno 
cd170 26 20 28 70 50 61 67 65 72 2d 3e 6e 48 61 73 68  & (pPager->nHash
cd180 2d 31 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  -1);.  if( pPage
cd190 72 2d 3e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20  r->aHash[h] ){. 
cd1a0 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
cd1b0 72 2d 3e 61 48 61 73 68 5b 68 5d 2d 3e 70 50 72  r->aHash[h]->pPr
cd1c0 65 76 48 61 73 68 3d 3d 30 20 29 3b 0a 20 20 20  evHash==0 );.   
cd1d0 20 70 50 61 67 65 72 2d 3e 61 48 61 73 68 5b 68   pPager->aHash[h
cd1e0 5d 2d 3e 70 50 72 65 76 48 61 73 68 20 3d 20 70  ]->pPrevHash = p
cd1f0 50 67 3b 0a 20 20 7d 0a 20 20 70 50 67 2d 3e 70  Pg;.  }.  pPg->p
cd200 4e 65 78 74 48 61 73 68 20 3d 20 70 50 61 67 65  NextHash = pPage
cd210 72 2d 3e 61 48 61 73 68 5b 68 5d 3b 0a 20 20 70  r->aHash[h];.  p
cd220 50 61 67 65 72 2d 3e 61 48 61 73 68 5b 68 5d 20  Pager->aHash[h] 
cd230 3d 20 70 50 67 3b 0a 20 20 70 50 67 2d 3e 70 50  = pPg;.  pPg->pP
cd240 72 65 76 48 61 73 68 20 3d 20 30 3b 0a 0a 20 20  revHash = 0;..  
cd250 6d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a  makeDirty(pPg);.
cd260 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43    pPager->dirtyC
cd270 61 63 68 65 20 3d 20 31 3b 0a 0a 20 20 69 66 28  ache = 1;..  if(
cd280 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b   needSyncPgno ){
cd290 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 53  .    /* If needS
cd2a0 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a  yncPgno is non-z
cd2b0 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f  ero, then the jo
cd2c0 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 73  urnal file needs
cd2d0 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 73   to be .    ** s
cd2e0 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 61  ync()ed before a
cd2f0 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 74 74  ny data is writt
cd300 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 20 66  en to database f
cd310 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 79 6e  ile page needSyn
cd320 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75  cPgno..    ** Cu
cd330 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68  rrently, no such
cd340 20 70 61 67 65 20 65 78 69 73 74 73 20 69 6e 20   page exists in 
cd350 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 61  the page-cache a
cd360 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 50  nd the .    ** P
cd370 61 67 65 72 2e 61 49 6e 4a 6f 75 72 6e 61 6c 20  ager.aInJournal 
cd380 62 69 74 20 68 61 73 20 62 65 65 6e 20 73 65 74  bit has been set
cd390 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74 6f 20  . This needs to 
cd3a0 62 65 20 72 65 6d 65 64 69 65 64 20 62 79 20 6c  be remedied by l
cd3b0 6f 61 64 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68  oading.    ** th
cd3c0 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  e page into the 
cd3d0 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20  pager-cache and 
cd3e0 73 65 74 74 69 6e 67 20 74 68 65 20 50 67 48 64  setting the PgHd
cd3f0 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 2e  r.needSync flag.
cd400 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
cd410 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  he sqlite3PagerG
cd420 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61  et() call may ca
cd430 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  use the journal 
cd440 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65  to sync. So make
cd450 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65  .    ** sure the
cd460 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20   Pager.needSync 
cd470 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e  flag is set too.
cd480 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
cd490 72 63 3b 0a 20 20 20 20 50 67 48 64 72 20 2a 70  rc;.    PgHdr *p
cd4a0 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72  PgHdr;.    asser
cd4b0 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  t( pPager->needS
cd4c0 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ync );.    rc = 
cd4d0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
cd4e0 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63  pPager, needSync
cd4f0 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a  Pgno, &pPgHdr);.
cd500 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
cd510 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
cd520 63 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e  c;.    pPager->n
cd530 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20  eedSync = 1;.   
cd540 20 70 50 67 48 64 72 2d 3e 6e 65 65 64 53 79 6e   pPgHdr->needSyn
cd550 63 20 3d 20 31 3b 0a 20 20 20 20 70 50 67 48 64  c = 1;.    pPgHd
cd560 72 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 31  r->inJournal = 1
cd570 3b 0a 20 20 20 20 6d 61 6b 65 44 69 72 74 79 28  ;.    makeDirty(
cd580 70 50 67 48 64 72 29 3b 0a 20 20 20 20 73 71 6c  pPgHdr);.    sql
cd590 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
cd5a0 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 70  PgHdr);.  }..  p
cd5b0 61 67 65 72 4c 65 61 76 65 28 70 50 61 67 65 72  agerLeave(pPager
cd5c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
cd5d0 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
cd5e0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
cd5f0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 64  pointer to the d
cd600 61 74 61 20 66 6f 72 20 74 68 65 20 73 70 65 63  ata for the spec
cd610 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53  ified page..*/.S
cd620 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
cd630 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
cd640 47 65 74 44 61 74 61 28 44 62 50 61 67 65 20 2a  GetData(DbPage *
cd650 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 50  pPg){.  return P
cd660 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50 67  GHDR_TO_DATA(pPg
cd670 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
cd680 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
cd690 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72 61  the Pager.nExtra
cd6a0 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72 61   bytes of "extra
cd6b0 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f  " space .** allo
cd6c0 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68  cated along with
cd6d0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70   the specified p
cd6e0 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  age..*/.SQLITE_P
cd6f0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
cd700 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
cd710 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  a(DbPage *pPg){.
cd720 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
cd730 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20  = pPg->pPager;. 
cd740 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 72 3f   return (pPager?
cd750 50 47 48 44 52 5f 54 4f 5f 45 58 54 52 41 28 70  PGHDR_TO_EXTRA(p
cd760 50 67 2c 20 70 50 61 67 65 72 29 3a 30 29 3b 0a  Pg, pPager):0);.
cd770 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74  }../*.** Get/set
cd780 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64   the locking-mod
cd790 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72  e for this pager
cd7a0 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64  . Parameter eMod
cd7b0 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a 2a  e must be one.**
cd7c0 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e   of PAGER_LOCKIN
cd7d0 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41 47  GMODE_QUERY, PAG
cd7e0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
cd7f0 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41 47  ORMAL or .** PAG
cd800 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
cd810 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68 65  XCLUSIVE. If the
cd820 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
cd830 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a 2a  t _QUERY, then.*
cd840 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f  * the locking-mo
cd850 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  de is set to the
cd860 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 64   value specified
cd870 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
cd880 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65 69  rned value is ei
cd890 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b 49  ther PAGER_LOCKI
cd8a0 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72  NGMODE_NORMAL or
cd8b0 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  .** PAGER_LOCKIN
cd8c0 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2c  GMODE_EXCLUSIVE,
cd8d0 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20   indicating the 
cd8e0 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c  current (possibl
cd8f0 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c 6f  y updated).** lo
cd900 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53  cking-mode..*/.S
cd910 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
cd920 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f  t sqlite3PagerLo
cd930 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 20  ckingMode(Pager 
cd940 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f  *pPager, int eMo
cd950 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 65  de){.  assert( e
cd960 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
cd970 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20 20  INGMODE_QUERY.  
cd980 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
cd990 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  de==PAGER_LOCKIN
cd9a0 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20 20  GMODE_NORMAL.   
cd9b0 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64           || eMod
cd9c0 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
cd9d0 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29  MODE_EXCLUSIVE )
cd9e0 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
cd9f0 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55  R_LOCKINGMODE_QU
cda00 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65 72  ERY<0 );.  asser
cda10 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  t( PAGER_LOCKING
cda20 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20 26  MODE_NORMAL>=0 &
cda30 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  & PAGER_LOCKINGM
cda40 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d 30  ODE_EXCLUSIVE>=0
cda50 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65 3e   );.  if( eMode>
cda60 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74  =0 && !pPager->t
cda70 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 70  empFile ){.    p
cda80 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
cda90 4d 6f 64 65 20 3d 20 65 4d 6f 64 65 3b 0a 20 20  Mode = eMode;.  
cdaa0 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  }.  return (int)
cdab0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
cdac0 65 4d 6f 64 65 3b 0a 7d 0a 0a 23 69 66 64 65 66  eMode;.}..#ifdef
cdad0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
cdae0 0a 2a 2a 20 50 72 69 6e 74 20 61 20 6c 69 73 74  .** Print a list
cdaf0 69 6e 67 20 6f 66 20 61 6c 6c 20 72 65 66 65 72  ing of all refer
cdb00 65 6e 63 65 64 20 70 61 67 65 73 20 61 6e 64 20  enced pages and 
cdb10 74 68 65 69 72 20 72 65 66 20 63 6f 75 6e 74 2e  their ref count.
cdb20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cdb30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
cdb40 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67 65  agerRefdump(Page
cdb50 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 50 67  r *pPager){.  Pg
cdb60 48 64 72 20 2a 70 50 67 3b 0a 20 20 66 6f 72 28  Hdr *pPg;.  for(
cdb70 70 50 67 3d 70 50 61 67 65 72 2d 3e 70 41 6c 6c  pPg=pPager->pAll
cdb80 3b 20 70 50 67 3b 20 70 50 67 3d 70 50 67 2d 3e  ; pPg; pPg=pPg->
cdb90 70 4e 65 78 74 41 6c 6c 29 7b 0a 20 20 20 20 69  pNextAll){.    i
cdba0 66 28 20 70 50 67 2d 3e 6e 52 65 66 3c 3d 30 20  f( pPg->nRef<=0 
cdbb0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
cdbc0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
cdbd0 74 66 28 22 50 41 47 45 20 25 33 64 20 61 64 64  tf("PAGE %3d add
cdbe0 72 3d 25 70 20 6e 52 65 66 3d 25 64 5c 6e 22 2c  r=%p nRef=%d\n",
cdbf0 20 0a 20 20 20 20 20 20 20 70 50 67 2d 3e 70 67   .       pPg->pg
cdc00 6e 6f 2c 20 50 47 48 44 52 5f 54 4f 5f 44 41 54  no, PGHDR_TO_DAT
cdc10 41 28 70 50 67 29 2c 20 70 50 67 2d 3e 6e 52 65  A(pPg), pPg->nRe
cdc20 66 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  f);.  }.}.#endif
cdc30 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
cdc40 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 20 2a  TE_OMIT_DISKIO *
cdc50 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
cdc60 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 72 2e  ** End of pager.
cdc70 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
cdc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdca0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
cdcb0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74  ** Begin file bt
cdcc0 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  mutex.c ********
cdcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdcf0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75  */./*.** 2007 Au
cdd00 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68  gust 27.**.** Th
cdd10 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
cdd20 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
cdd30 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
cdd40 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
cdd50 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
cdd60 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
cdd70 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
cdd80 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
cdd90 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
cdda0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
cddb0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
cddc0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
cddd0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
cdde0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
cddf0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
cde00 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
cde10 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
cde20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cde30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cde40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cde50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cde60 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64  ******.**.** $Id
cde70 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76 20 31 2e  : btmutex.c,v 1.
cde80 37 20 32 30 30 37 2f 30 38 2f 33 30 20 30 31 3a  7 2007/08/30 01:
cde90 31 39 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a  19:59 drh Exp $.
cdea0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
cdeb0 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
cdec0 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
cded0 6d 75 74 65 78 65 73 20 6f 6e 20 42 74 72 65 65  mutexes on Btree
cdee0 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 69   objects..** Thi
cdef0 73 20 63 6f 64 65 20 72 65 61 6c 6c 79 20 62 65  s code really be
cdf00 6c 6f 6e 67 73 20 69 6e 20 62 74 72 65 65 2e 63  longs in btree.c
cdf10 2e 20 20 42 75 74 20 62 74 72 65 65 2e 63 20 69  .  But btree.c i
cdf20 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 0a 2a 2a  s getting too.**
cdf30 20 62 69 67 20 61 6e 64 20 77 65 20 77 61 6e 74   big and we want
cdf40 20 74 6f 20 62 72 65 61 6b 20 69 74 20 64 6f 77   to break it dow
cdf50 6e 20 73 6f 6d 65 2e 20 20 54 68 69 73 20 70 61  n some.  This pa
cdf60 63 6b 61 67 65 64 20 73 65 65 6d 65 64 20 6c 69  ckaged seemed li
cdf70 6b 65 0a 2a 2a 20 61 20 67 6f 6f 64 20 62 72 65  ke.** a good bre
cdf80 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  akout..*/./*****
cdf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
cdfa0 65 20 62 74 72 65 65 49 6e 74 2e 68 20 69 6e 20  e btreeInt.h in 
cdfb0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 62 74  the middle of bt
cdfc0 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  mutex.c ********
cdfd0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
cdfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
cdff0 66 69 6c 65 20 62 74 72 65 65 49 6e 74 2e 68 20  file btreeInt.h 
ce000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce020 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
ce030 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a  2004 April 6.**.
ce040 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
ce050 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
ce060 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
ce070 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
ce080 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
ce090 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
ce0a0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
ce0b0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
ce0c0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
ce0d0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
ce0e0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
ce0f0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
ce100 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
ce110 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
ce120 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
ce130 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
ce140 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
ce150 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
ce160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ce190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24  ***********.** $
ce1a0 49 64 3a 20 62 74 72 65 65 49 6e 74 2e 68 2c 76  Id: btreeInt.h,v
ce1b0 20 31 2e 31 33 20 32 30 30 37 2f 30 38 2f 33 30   1.13 2007/08/30
ce1c0 20 30 31 3a 31 39 3a 35 39 20 64 72 68 20 45 78   01:19:59 drh Ex
ce1d0 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p $.**.** This f
ce1e0 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
ce1f0 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d   external (disk-
ce200 62 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20  based) database 
ce210 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a  using BTrees..**
ce220 20 46 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20   For a detailed 
ce230 64 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54  discussion of BT
ce240 72 65 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a  rees, refer to.*
ce250 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20  *.**     Donald 
ce260 45 2e 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52  E. Knuth, THE AR
ce270 54 20 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52  T OF COMPUTER PR
ce280 4f 47 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d  OGRAMMING, Volum
ce290 65 20 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72  e 3:.**     "Sor
ce2a0 74 69 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69  ting And Searchi
ce2b0 6e 67 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34  ng", pages 473-4
ce2c0 38 30 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c  80. Addison-Wesl
ce2d0 65 79 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73  ey.**     Publis
ce2e0 68 69 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65  hing Company, Re
ce2f0 61 64 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73  ading, Massachus
ce300 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  etts..**.** The 
ce310 62 61 73 69 63 20 69 64 65 61 20 69 73 20 74 68  basic idea is th
ce320 61 74 20 65 61 63 68 20 70 61 67 65 20 6f 66 20  at each page of 
ce330 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  the file contain
ce340 73 20 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  s N database.** 
ce350 65 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20  entries and N+1 
ce360 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70  pointers to subp
ce370 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d  ages..**.**   --
ce380 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ce390 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ce3a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ce3b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
ce3c0 2a 20 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20  *   |  Ptr(0) | 
ce3d0 4b 65 79 28 30 29 20 7c 20 50 74 72 28 31 29 20  Key(0) | Ptr(1) 
ce3e0 7c 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c  | Key(1) | ... |
ce3f0 20 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28   Key(N-1) | Ptr(
ce400 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  N) |.**   ------
ce410 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ce420 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ce430 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ce440 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
ce450 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73   All of the keys
ce460 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 68 61   on the page tha
ce470 74 20 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20  t Ptr(0) points 
ce480 74 6f 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c  to have values l
ce490 65 73 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28  ess.** than Key(
ce4a0 30 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20  0).  All of the 
ce4b0 6b 65 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72  keys on page Ptr
ce4c0 28 31 29 20 61 6e 64 20 69 74 73 20 73 75 62 70  (1) and its subp
ce4d0 61 67 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c  ages have.** val
ce4e0 75 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e  ues greater than
ce4f0 20 4b 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73   Key(0) and less
ce500 20 74 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41   than Key(1).  A
ce510 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a  ll of the keys.*
ce520 2a 20 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20  * on Ptr(N) and 
ce530 69 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76  its subpages hav
ce540 65 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72  e values greater
ce550 20 74 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20   than Key(N-1). 
ce560 20 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68   And.** so forth
ce570 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20  ..**.** Finding 
ce580 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79  a particular key
ce590 20 72 65 71 75 69 72 65 73 20 72 65 61 64 69 6e   requires readin
ce5a0 67 20 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65  g O(log(M)) page
ce5b0 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64  s from the .** d
ce5c0 69 73 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74  isk where M is t
ce5d0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
ce5e0 72 69 65 73 20 69 6e 20 74 68 65 20 74 72 65 65  ries in the tree
ce5f0 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20  ..**.** In this 
ce600 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
ce610 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61  a single file ca
ce620 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f  n hold one or mo
ce630 72 65 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20  re separate .** 
ce640 42 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54  BTrees.  Each BT
ce650 72 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65  ree is identifie
ce660 64 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f  d by the index o
ce670 66 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e  f its root page.
ce680 20 20 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64    The.** key and
ce690 20 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e   data for any en
ce6a0 74 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64  try are combined
ce6b0 20 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61   to form the "pa
ce6c0 79 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69  yload".  A.** fi
ce6d0 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61  xed amount of pa
ce6e0 79 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72  yload can be car
ce6f0 72 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e  ried directly on
ce700 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
ce710 20 70 61 67 65 2e 20 20 49 66 20 74 68 65 20 70   page.  If the p
ce720 61 79 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72  ayload is larger
ce730 20 74 68 61 6e 20 74 68 65 20 70 72 65 73 65 74   than the preset
ce740 20 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72   amount then sur
ce750 70 6c 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72  plus.** bytes ar
ce760 65 20 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72  e stored on over
ce770 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65  flow pages.  The
ce780 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20   payload for an 
ce790 65 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65  entry.** and the
ce7a0 20 70 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74   preceding point
ce7b0 65 72 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20  er are combined 
ce7c0 74 6f 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22  to form a "Cell"
ce7d0 2e 20 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65  .  Each .** page
ce7e0 20 68 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61   has a small hea
ce7f0 64 65 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69  der which contai
ce800 6e 73 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f  ns the Ptr(N) po
ce810 69 6e 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a  inter and other.
ce820 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73  ** information s
ce830 75 63 68 20 61 73 20 74 68 65 20 73 69 7a 65 20  uch as the size 
ce840 6f 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e  of key and data.
ce850 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45  .**.** FORMAT DE
ce860 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20  TAILS.**.** The 
ce870 66 69 6c 65 20 69 73 20 64 69 76 69 64 65 64 20  file is divided 
ce880 69 6e 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65  into pages.  The
ce890 20 66 69 72 73 74 20 70 61 67 65 20 69 73 20 63   first page is c
ce8a0 61 6c 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a  alled page 1,.**
ce8b0 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70   the second is p
ce8c0 61 67 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f  age 2, and so fo
ce8d0 72 74 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d  rth.  A page num
ce8e0 62 65 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69  ber of zero indi
ce8f0 63 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63  cates.** "no suc
ce900 68 20 70 61 67 65 22 2e 20 20 54 68 65 20 70 61  h page".  The pa
ce910 67 65 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61  ge size can be a
ce920 6e 79 74 68 69 6e 67 20 62 65 74 77 65 65 6e 20  nything between 
ce930 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e 0a 2a  512 and 65536..*
ce940 2a 20 45 61 63 68 20 70 61 67 65 20 63 61 6e 20  * Each page can 
ce950 62 65 20 65 69 74 68 65 72 20 61 20 62 74 72 65  be either a btre
ce960 65 20 70 61 67 65 2c 20 61 20 66 72 65 65 6c 69  e page, a freeli
ce970 73 74 20 70 61 67 65 20 6f 72 20 61 6e 20 6f 76  st page or an ov
ce980 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2e 0a  erflow.** page..
ce990 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
ce9a0 70 61 67 65 20 69 73 20 61 6c 77 61 79 73 20 61  page is always a
ce9b0 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 68   btree page.  Th
ce9c0 65 20 66 69 72 73 74 20 31 30 30 20 62 79 74 65  e first 100 byte
ce9d0 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a  s of the first.*
ce9e0 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 61  * page contain a
ce9f0 20 73 70 65 63 69 61 6c 20 68 65 61 64 65 72 20   special header 
cea00 28 74 68 65 20 22 66 69 6c 65 20 68 65 61 64 65  (the "file heade
cea10 72 22 29 20 74 68 61 74 20 64 65 73 63 72 69 62  r") that describ
cea20 65 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20  es the file..** 
cea30 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68  The format of th
cea40 65 20 66 69 6c 65 20 68 65 61 64 65 72 20 69 73  e file header is
cea50 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
cea60 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49  **   OFFSET   SI
cea70 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f  ZE    DESCRIPTIO
cea80 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20  N.**      0     
cea90 20 31 36 20 20 20 20 20 48 65 61 64 65 72 20 73   16     Header s
ceaa0 74 72 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66  tring: "SQLite f
ceab0 6f 72 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a 20  ormat 3\000".** 
ceac0 20 20 20 20 31 36 20 20 20 20 20 20 20 32 20 20      16       2  
cead0 20 20 20 50 61 67 65 20 73 69 7a 65 20 69 6e 20     Page size in 
ceae0 62 79 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 20  bytes.  .**     
ceaf0 31 38 20 20 20 20 20 20 20 31 20 20 20 20 20 46  18       1     F
ceb00 69 6c 65 20 66 6f 72 6d 61 74 20 77 72 69 74 65  ile format write
ceb10 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20   version.**     
ceb20 31 39 20 20 20 20 20 20 20 31 20 20 20 20 20 46  19       1     F
ceb30 69 6c 65 20 66 6f 72 6d 61 74 20 72 65 61 64 20  ile format read 
ceb40 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  version.**     2
ceb50 30 20 20 20 20 20 20 20 31 20 20 20 20 20 42 79  0       1     By
ceb60 74 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70  tes of unused sp
ceb70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ace at the end o
ceb80 66 20 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 20  f each page.**  
ceb90 20 20 20 32 31 20 20 20 20 20 20 20 31 20 20 20     21       1   
ceba0 20 20 4d 61 78 20 65 6d 62 65 64 64 65 64 20 70    Max embedded p
cebb0 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a  ayload fraction.
cebc0 2a 2a 20 20 20 20 20 32 32 20 20 20 20 20 20 20  **     22       
cebd0 31 20 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 64  1     Min embedd
cebe0 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
cebf0 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 20  ion.**     23   
cec00 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65      1     Min le
cec10 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  af payload fract
cec20 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 20  ion.**     24   
cec30 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 63      4     File c
cec40 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a  hange counter.**
cec50 20 20 20 20 20 32 38 20 20 20 20 20 20 20 34 20       28       4 
cec60 20 20 20 20 52 65 73 65 72 76 65 64 20 66 6f 72      Reserved for
cec70 20 66 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 20   future use.**  
cec80 20 20 20 33 32 20 20 20 20 20 20 20 34 20 20 20     32       4   
cec90 20 20 46 69 72 73 74 20 66 72 65 65 6c 69 73 74    First freelist
ceca0 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 20   page.**     36 
cecb0 20 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62        4     Numb
cecc0 65 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 70  er of freelist p
cecd0 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65  ages in the file
cece0 0a 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 20  .**     40      
cecf0 36 30 20 20 20 20 20 31 35 20 34 2d 62 79 74 65  60     15 4-byte
ced00 20 6d 65 74 61 20 76 61 6c 75 65 73 20 70 61 73   meta values pas
ced10 73 65 64 20 74 6f 20 68 69 67 68 65 72 20 6c 61  sed to higher la
ced20 79 65 72 73 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f  yers.**.** All o
ced30 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61  f the integer va
ced40 6c 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64  lues are big-end
ced50 69 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66  ian (most signif
ced60 69 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74  icant byte first
ced70 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c  )..**.** The fil
ced80 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  e change counter
ced90 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
ceda0 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  when the databas
cedb0 65 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20  e is changed.** 
cedc0 54 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c  This counter all
cedd0 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ows other proces
cede0 73 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e  ses to know when
cedf0 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68   the file has ch
cee00 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75  anged.** and thu
cee10 73 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64  s when they need
cee20 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20   to flush their 
cee30 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cache..**.** The
cee40 20 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61   max embedded pa
cee50 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69  yload fraction i
cee60 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  s the amount of 
cee70 74 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65  the total usable
cee80 0a 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70  .** space in a p
cee90 61 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 20  age that can be 
ceea0 63 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69  consumed by a si
ceeb0 6e 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74  ngle cell for st
ceec0 61 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65  andard.** B-tree
ceed0 20 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20   (non-LEAFDATA) 
ceee0 74 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65  tables.  A value
ceef0 20 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30   of 255 means 10
cef00 30 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  0%.  The default
cef10 0a 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20  .** is to limit 
cef20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c  the maximum cell
cef30 20 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74   size so that at
cef40 20 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77   least 4 cells w
cef50 69 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e  ill fit.** on on
cef60 65 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 68  e page.  Thus th
cef70 65 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d  e default max em
cef80 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66  bedded payload f
cef90 72 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a  raction is 64..*
cefa0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c  *.** If the payl
cefb0 6f 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69  oad for a cell i
cefc0 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
cefd0 65 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74  e max payload, t
cefe0 68 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79  hen extra.** pay
ceff0 6c 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20  load is spilled 
cf000 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
cf010 73 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72  s.  Once an over
cf020 66 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c  flow page is all
cf030 6f 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61  ocated,.** as ma
cf040 6e 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 73  ny bytes as poss
cf050 69 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69  ible are moved i
cf060 6e 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  nto the overflow
cf070 20 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c   pages without l
cf080 65 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65  etting.** the ce
cf090 6c 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c  ll size drop bel
cf0a0 6f 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64  ow the min embed
cf0b0 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
cf0c0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
cf0d0 6d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64  min leaf payload
cf0e0 20 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b   fraction is lik
cf0f0 65 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64  e the min embedd
cf100 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
cf110 69 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68  ion.** except th
cf120 61 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f  at it applies to
cf130 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61   leaf nodes in a
cf140 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e 20   LEAFDATA tree. 
cf150 20 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20   The maximum.** 
cf160 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e  payload fraction
cf170 20 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20   for a LEAFDATA 
cf180 74 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 31  tree is always 1
cf190 30 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64  00% (or 255) and
cf1a0 20 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69   it.** not speci
cf1b0 66 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 64  fied in the head
cf1c0 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62  er..**.** Each b
cf1d0 74 72 65 65 20 70 61 67 65 73 20 69 73 20 64 69  tree pages is di
cf1e0 76 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65  vided into three
cf1f0 20 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20   sections:  The 
cf200 68 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63  header, the.** c
cf210 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
cf220 79 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20  y, and the cell 
cf230 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50  content area.  P
cf240 61 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61  age 1 also has a
cf250 20 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c   100-byte.** fil
cf260 65 20 68 65 61 64 65 72 20 74 68 61 74 20 6f 63  e header that oc
cf270 63 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 20  curs before the 
cf280 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a  page header..**.
cf290 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
cf2a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20  ---------|.**   
cf2b0 20 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72     | file header
cf2c0 20 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 65      |   100 byte
cf2d0 73 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e  s.  Page 1 only.
cf2e0 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d  .**      |------
cf2f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20  ----------|.**  
cf300 20 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 65      | page heade
cf310 72 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 73  r    |   8 bytes
cf320 20 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32   for leaves.  12
cf330 20 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72   bytes for inter
cf340 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20  ior nodes.**    
cf350 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
cf360 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63  ---|.**      | c
cf370 65 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20  ell pointer   | 
cf380 20 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72    |  2 bytes per
cf390 20 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f   cell.  Sorted o
cf3a0 72 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20  rder..**      | 
cf3b0 61 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c  array          |
cf3c0 20 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e     |  Grows down
cf3d0 77 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20  ward.**      |  
cf3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
cf3f0 20 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d    v.**      |---
cf400 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a  -------------|.*
cf410 2a 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63  *      | unalloc
cf420 61 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20  ated    |.**    
cf430 20 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 20    | space       
cf440 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d     |.**      |--
cf450 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20  --------------| 
cf460 20 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72    ^  Grows upwar
cf470 64 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c  ds.**      | cel
cf480 6c 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20  l content   |   
cf490 7c 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 64  |  Arbitrary ord
cf4a0 65 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 20  er interspersed 
cf4b0 77 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e  with freeblocks.
cf4c0 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20  .**      | area 
cf4d0 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20            |   | 
cf4e0 20 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 20   and free space 
cf4f0 66 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20  fragments..**   
cf500 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     |------------
cf510 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ----|.**.** The 
cf520 70 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f  page headers loo
cf530 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ks like this:.**
cf540 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53  .**   OFFSET   S
cf550 49 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 54  IZE     DESCRIPT
cf560 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20  ION.**      0   
cf570 20 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 73      1      Flags
cf580 2e 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20  . 1: intkey, 2: 
cf590 7a 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61  zerodata, 4: lea
cf5a0 66 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a  fdata, 8: leaf.*
cf5b0 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 32  *      1       2
cf5c0 20 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 65        byte offse
cf5d0 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66  t to the first f
cf5e0 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20  reeblock.**     
cf5f0 20 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20   3       2      
cf600 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
cf610 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20  on this page.** 
cf620 20 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20       5       2  
cf630 20 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f      first byte o
cf640 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  f the cell conte
cf650 6e 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20  nt area.**      
cf660 37 20 20 20 20 20 20 20 31 20 20 20 20 20 20 6e  7       1      n
cf670 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e  umber of fragmen
cf680 74 65 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a  ted free bytes.*
cf690 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 20 34  *      8       4
cf6a0 20 20 20 20 20 20 52 69 67 68 74 20 63 68 69 6c        Right chil
cf6b0 64 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 61  d (the Ptr(N) va
cf6c0 6c 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f  lue).  Omitted o
cf6d0 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20  n leaves..**.** 
cf6e0 54 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65  The flags define
cf6f0 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74   the format of t
cf700 68 69 73 20 62 74 72 65 65 20 70 61 67 65 2e 20  his btree page. 
cf710 20 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d   The leaf flag m
cf720 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69  eans that.** thi
cf730 73 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68  s page has no ch
cf740 69 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72  ildren.  The zer
cf750 6f 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73  odata flag means
cf760 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20   that this page 
cf770 63 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20  carries.** only 
cf780 6b 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61  keys and no data
cf790 2e 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c  .  The intkey fl
cf7a0 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ag means that th
cf7b0 65 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67  e key is a integ
cf7c0 65 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73  er.** which is s
cf7d0 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79  tored in the key
cf7e0 20 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74   size entry of t
cf7f0 68 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72  he cell header r
cf800 61 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a  ather than in.**
cf810 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65   the payload are
cf820 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c  a..**.** The cel
cf830 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20  l pointer array 
cf840 62 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69  begins on the fi
cf850 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74  rst byte after t
cf860 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a  he page header..
cf870 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e  ** The cell poin
cf880 74 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69  ter array contai
cf890 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  ns zero or more 
cf8a0 32 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77  2-byte numbers w
cf8b0 68 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73  hich are.** offs
cf8c0 65 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67  ets from the beg
cf8d0 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61  inning of the pa
cf8e0 67 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63  ge to the cell c
cf8f0 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65  ontent in the ce
cf900 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72  ll.** content ar
cf910 65 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f  ea.  The cell po
cf920 69 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20  inters occur in 
cf930 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54  sorted order.  T
cf940 68 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65  he system strive
cf950 73 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65  s.** to keep fre
cf960 65 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68  e space after th
cf970 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e  e last cell poin
cf980 74 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20  ter so that new 
cf990 63 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20  cells can.** be 
cf9a0 65 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74  easily added wit
cf9b0 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64  hout having to d
cf9c0 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61  efragment the pa
cf9d0 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63  ge..**.** Cell c
cf9e0 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64  ontent is stored
cf9f0 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64   at the very end
cfa00 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64   of the page and
cfa10 20 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68   grows toward th
cfa20 65 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f  e.** beginning o
cfa30 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a  f the page..**.*
cfa40 2a 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77  * Unused space w
cfa50 69 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63  ithin the cell c
cfa60 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63  ontent area is c
cfa70 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20  ollected into a 
cfa80 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a  linked list of.*
cfa90 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45  * freeblocks.  E
cfaa0 61 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73  ach freeblock is
cfab0 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65   at least 4 byte
cfac0 73 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20  s in size.  The 
cfad0 62 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74  byte offset.** t
cfae0 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65  o the first free
cfaf0 62 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69  block is given i
cfb00 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46  n the header.  F
cfb10 72 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20  reeblocks occur 
cfb20 69 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67  in.** increasing
cfb30 20 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65   order.  Because
cfb40 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73   a freeblock mus
cfb50 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20  t be at least 4 
cfb60 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a  bytes in size,.*
cfb70 2a 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33  * any group of 3
cfb80 20 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64   or fewer unused
cfb90 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65   bytes in the ce
cfba0 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20  ll content area 
cfbb0 63 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20  cannot.** exist 
cfbc0 6f 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b  on the freeblock
cfbd0 20 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70   chain.  A group
cfbe0 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66   of 3 or fewer f
cfbf0 72 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c  ree bytes is cal
cfc00 6c 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e  led.** a fragmen
cfc10 74 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75  t.  The total nu
cfc20 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
cfc30 20 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69   all fragments i
cfc40 73 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69  s recorded..** i
cfc50 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  n the page heade
cfc60 72 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a  r at offset 7..*
cfc70 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20  *.**    SIZE    
cfc80 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20  DESCRIPTION.**  
cfc90 20 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f      2     Byte o
cfca0 66 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78  ffset of the nex
cfcb0 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20  t freeblock.**  
cfcc0 20 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20      2     Bytes 
cfcd0 69 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63  in this freebloc
cfce0 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72  k.**.** Cells ar
cfcf0 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65  e of variable le
cfd00 6e 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65  ngth.  Cells are
cfd10 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63   stored in the c
cfd20 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
cfd30 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f   at.** the end o
cfd40 66 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69  f the page.  Poi
cfd50 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c  nters to the cel
cfd60 6c 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65  ls are in the ce
cfd70 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
cfd80 0a 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61  .** that immedia
cfd90 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65  tely follows the
cfda0 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43   page header.  C
cfdb0 65 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65  ells is not nece
cfdc0 73 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69  ssarily.** conti
cfdd0 67 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65  guous or in orde
cfde0 72 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e  r, but cell poin
cfdf0 74 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75  ters are contigu
cfe00 6f 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72  ous and in order
cfe10 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e  ..**.** Cell con
cfe20 74 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f  tent makes use o
cfe30 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74  f variable lengt
cfe40 68 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76  h integers.  A v
cfe50 61 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74  ariable.** lengt
cfe60 68 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74  h integer is 1 t
cfe70 6f 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20  o 9 bytes where 
cfe80 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73  the lower 7 bits
cfe90 20 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74   of each .** byt
cfea0 65 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65  e are used.  The
cfeb0 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74   integer consist
cfec0 73 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74  s of all bytes t
cfed0 68 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73  hat have bit 8 s
cfee0 65 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69  et and.** the fi
cfef0 72 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69  rst byte with bi
cff00 74 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20  t 8 clear.  The 
cff10 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74  most significant
cff20 20 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74   byte of the int
cff30 65 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20  eger.** appears 
cff40 66 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62  first.  A variab
cff50 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
cff60 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72  r may not be mor
cff70 65 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c  e than 9 bytes l
cff80 6f 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65  ong..** As a spe
cff90 63 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38  cial case, all 8
cffa0 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74   bytes of the 9t
cffb0 68 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20  h byte are used 
cffc0 61 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a  as data.  This.*
cffd0 2a 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69  * allows a 64-bi
cffe0 74 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20  t integer to be 
cfff0 65 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74  encoded in 9 byt
d0000 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30  es..**.**    0x0
d0010 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
d0020 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20         becomes  
d0030 30 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20  0x00000000.**   
d0040 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20   0x7f           
d0050 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d             becom
d0060 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a  es  0x0000007f.*
d0070 2a 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20  *    0x81 0x00  
d0080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
d0090 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30  ecomes  0x000000
d00a0 38 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78  80.**    0x82 0x
d00b0 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
d00c0 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30     becomes  0x00
d00d0 30 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38  000100.**    0x8
d00e0 30 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20  0 0x7f          
d00f0 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20         becomes  
d0100 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20  0x0000007f.**   
d0110 20 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20   0x8a 0x91 0xd1 
d0120 30 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d  0xac 0x78  becom
d0130 65 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a  es  0x12345678.*
d0140 2a 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30  *    0x81 0x81 0
d0150 78 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62  x81 0x81 0x01  b
d0160 65 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30  ecomes  0x102040
d0170 38 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c  81.**.** Variabl
d0180 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e length integer
d0190 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72  s are used for r
d01a0 6f 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c  owids and to hol
d01b0 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  d the number of.
d01c0 2a 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20  ** bytes of key 
d01d0 61 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74  and data in a bt
d01e0 72 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ree cell..**.** 
d01f0 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61  The content of a
d0200 20 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65   cell looks like
d0210 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
d0220 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54  SIZE    DESCRIPT
d0230 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  ION.**      4   
d0240 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66    Page number of
d0250 20 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e   the left child.
d0260 20 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66   Omitted if leaf
d0270 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a   flag is set..**
d0280 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62       var    Numb
d0290 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64  er of bytes of d
d02a0 61 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20  ata. Omitted if 
d02b0 74 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61  the zerodata fla
d02c0 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20  g is set..**    
d02d0 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f   var    Number o
d02e0 66 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20  f bytes of key. 
d02f0 4f 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c  Or the key itsel
d0300 66 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67  f if intkey flag
d0310 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20   is set..**     
d0320 20 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a   *     Payload.*
d0330 2a 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72  *      4     Fir
d0340 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f  st page of the o
d0350 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20  verflow chain.  
d0360 4f 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76  Omitted if no ov
d0370 65 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65  erflow.**.** Ove
d0380 72 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d  rflow pages form
d0390 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20   a linked list. 
d03a0 20 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70   Each page excep
d03b0 74 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f  t the last is co
d03c0 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c  mpletely.** fill
d03d0 65 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61  ed with data (pa
d03e0 67 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73  gesize - 4 bytes
d03f0 29 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67  ).  The last pag
d0400 65 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69  e can have as li
d0410 74 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74  ttle.** as 1 byt
d0420 65 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  e of data..**.**
d0430 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43      SIZE    DESC
d0440 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20  RIPTION.**      
d0450 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65  4     Page numbe
d0460 72 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c  r of next overfl
d0470 6f 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20  ow page.**      
d0480 2a 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a  *     Data.**.**
d0490 20 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20   Freelist pages 
d04a0 63 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74  come in two subt
d04b0 79 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65  ypes: trunk page
d04c0 73 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73  s and leaf pages
d04d0 2e 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68  .  The.** file h
d04e0 65 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20  eader points to 
d04f0 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c  the first in a l
d0500 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72  inked list of tr
d0510 75 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20  unk page.  Each 
d0520 74 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f  trunk.** page po
d0530 69 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65  ints to multiple
d0540 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68   leaf pages.  Th
d0550 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c  e content of a l
d0560 65 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75  eaf page is.** u
d0570 6e 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74  nspecified.  A t
d0580 72 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20  runk page looks 
d0590 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
d05a0 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43      SIZE    DESC
d05b0 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20  RIPTION.**      
d05c0 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65  4     Page numbe
d05d0 72 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20  r of next trunk 
d05e0 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20  page.**      4  
d05f0 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61     Number of lea
d0600 66 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68  f pointers on th
d0610 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20  is page.**      
d0620 2a 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f  *     zero or mo
d0630 72 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73  re pages numbers
d0640 20 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 2f   of leaves.*/../
d0650 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d  * Round up a num
d0660 62 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ber to the next 
d0670 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20  larger multiple 
d0680 6f 66 20 38 2e 20 20 54 68 69 73 20 69 73 20 75  of 8.  This is u
d0690 73 65 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20  sed.** to force 
d06a0 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74  8-byte alignment
d06b0 20 6f 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69   on 64-bit archi
d06c0 74 65 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65  tectures..*/.#de
d06d0 66 69 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20  fine ROUND8(x)  
d06e0 20 28 28 78 2b 37 29 26 7e 37 29 0a 0a 0a 2f 2a   ((x+7)&~7).../*
d06f0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
d0700 61 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 69  alue is the maxi
d0710 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 73  mum cell size as
d0720 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d  suming a maximum
d0730 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69   page.** size gi
d0740 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65  ve above..*/.#de
d0750 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a  fine MX_CELL_SIZ
d0760 45 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 61  E(pBt)  (pBt->pa
d0770 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68  geSize-8)../* Th
d0780 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
d0790 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73   of cells on a s
d07a0 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68  ingle page of th
d07b0 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
d07c0 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d  s.** assumes a m
d07d0 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  inimum cell size
d07e0 20 6f 66 20 33 20 62 79 74 65 73 2e 20 20 53 75   of 3 bytes.  Su
d07f0 63 68 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77  ch small cells w
d0800 69 6c 6c 20 62 65 0a 2a 2a 20 65 78 63 65 65 64  ill be.** exceed
d0810 69 6e 67 6c 79 20 72 61 72 65 2c 20 62 75 74 20  ingly rare, but 
d0820 74 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c  they are possibl
d0830 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58  e..*/.#define MX
d0840 5f 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74  _CELL(pBt) ((pBt
d0850 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 33 29  ->pageSize-8)/3)
d0860 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
d0870 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70  larations */.typ
d0880 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50  edef struct MemP
d0890 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70  age MemPage;.typ
d08a0 65 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f  edef struct BtLo
d08b0 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a  ck BtLock;../*.*
d08c0 2a 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69  * This is a magi
d08d0 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70  c string that ap
d08e0 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67  pears at the beg
d08f0 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a  inning of every.
d0900 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  ** SQLite databa
d0910 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69  se in order to i
d0920 64 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c 65  dentify the file
d0930 20 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 62   as a real datab
d0940 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63  ase..**.** You c
d0950 61 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 76  an change this v
d0960 61 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d  alue at compile-
d0970 74 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 69  time by specifyi
d0980 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45  ng a.** -DSQLITE
d0990 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e  _FILE_HEADER="..
d09a0 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c  ." on the compil
d09b0 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e  er command-line.
d09c0 20 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 20    The.** header 
d09d0 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20  must be exactly 
d09e0 31 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 69  16 bytes includi
d09f0 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d  ng the zero-term
d0a00 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65  inator so.** the
d0a10 20 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 73   string itself s
d0a20 68 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 72  hould be 15 char
d0a30 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66  acters long.  If
d0a40 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74   you change.** t
d0a50 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e 20  he header, then 
d0a60 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72  your custom libr
d0a70 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  ary will not be 
d0a80 61 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a  able to read .**
d0a90 20 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 72   databases gener
d0aa0 61 74 65 64 20 62 79 20 74 68 65 20 73 74 61 6e  ated by the stan
d0ab0 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74  dard tools and t
d0ac0 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c  he standard tool
d0ad0 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65  s.** will not be
d0ae0 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 61   able to read da
d0af0 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20  tabases created 
d0b00 62 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c  by your custom l
d0b10 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64  ibrary..*/.#ifnd
d0b20 65 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48  ef SQLITE_FILE_H
d0b30 45 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 37  EADER /* 1234567
d0b40 38 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 20  89 123456 */.#  
d0b50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 49  define SQLITE_FI
d0b60 4c 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 74  LE_HEADER "SQLit
d0b70 65 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64  e format 3".#end
d0b80 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74  if../*.** Page t
d0b90 79 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f  ype flags.  An O
d0ba0 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  Red combination 
d0bb0 6f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61  of these flags a
d0bc0 70 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a 20  ppear as the.** 
d0bd0 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f 6e  first byte of on
d0be0 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 65  -disk image of e
d0bf0 76 65 72 79 20 42 54 72 65 65 20 70 61 67 65 2e  very BTree page.
d0c00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  .*/.#define PTF_
d0c10 49 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a 23  INTKEY    0x01.#
d0c20 64 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44  define PTF_ZEROD
d0c30 41 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 6e  ATA  0x02.#defin
d0c40 65 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 20  e PTF_LEAFDATA  
d0c50 30 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 46  0x04.#define PTF
d0c60 5f 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 0a  _LEAF      0x08.
d0c70 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 70  ./*.** As each p
d0c80 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  age of the file 
d0c90 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d  is loaded into m
d0ca0 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e  emory, an instan
d0cb0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
d0cc0 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ing.** structure
d0cd0 20 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e 64   is appended and
d0ce0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
d0cf0 7a 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 75  zero.  This stru
d0d00 63 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20  cture stores.** 
d0d10 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
d0d20 74 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  t the page that 
d0d30 69 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d 20  is decoded from 
d0d40 74 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 67  the raw file pag
d0d50 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61  e..**.** The pPa
d0d60 72 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e 74  rent field point
d0d70 73 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 61  s back to the pa
d0d80 72 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 73  rent page.  This
d0d90 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a   allows us to.**
d0da0 20 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72   walk up the BTr
d0db0 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66  ee from any leaf
d0dc0 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43   to the root.  C
d0dd0 61 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65  are must be take
d0de0 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20  n to.** unref() 
d0df0 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
d0e00 70 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69  pointer when thi
d0e10 73 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e  s page is no lon
d0e20 67 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0a  ger referenced..
d0e30 2a 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 72  ** The pageDestr
d0e40 75 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20  uctor() routine 
d0e50 68 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f  handles that cho
d0e60 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73  re..**.** Access
d0e70 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f   to all fields o
d0e80 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
d0e90 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62   is controlled b
d0ea0 79 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73  y the mutex.** s
d0eb0 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65  tored in MemPage
d0ec0 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a  .pBt->mutex..*/.
d0ed0 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 7b  struct MemPage {
d0ee0 0a 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 20  .  u8 isInit;   
d0ef0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
d0f00 69 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e  if previously in
d0f10 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20  itialized. MUST 
d0f20 42 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 75  BE FIRST! */.  u
d0f30 38 20 69 64 78 53 68 69 66 74 3b 20 20 20 20 20  8 idxShift;     
d0f40 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 43      /* True if C
d0f50 65 6c 6c 20 69 6e 64 69 63 65 73 20 68 61 76 65  ell indices have
d0f60 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 38   changed */.  u8
d0f70 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20   nOverflow;     
d0f80 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
d0f90 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f  overflow cell bo
d0fa0 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20  dies in aCell[] 
d0fb0 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20  */.  u8 intKey; 
d0fc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
d0fd0 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67  e if intkey flag
d0fe0 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20   is set */.  u8 
d0ff0 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20  leaf;           
d1000 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61    /* True if lea
d1010 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f  f flag is set */
d1020 0a 20 20 75 38 20 7a 65 72 6f 44 61 74 61 3b 20  .  u8 zeroData; 
d1030 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
d1040 69 66 20 74 61 62 6c 65 20 73 74 6f 72 65 73 20  if table stores 
d1050 6b 65 79 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 75  keys only */.  u
d1060 38 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20 20  8 leafData;     
d1070 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
d1080 61 62 6c 65 73 20 73 74 6f 72 65 73 20 64 61 74  ables stores dat
d1090 61 20 6f 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79  a on leaves only
d10a0 20 2a 2f 0a 20 20 75 38 20 68 61 73 44 61 74 61   */.  u8 hasData
d10b0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ;          /* Tr
d10c0 75 65 20 69 66 20 74 68 69 73 20 70 61 67 65 20  ue if this page 
d10d0 73 74 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a 20  stores data */. 
d10e0 20 75 38 20 68 64 72 4f 66 66 73 65 74 3b 20 20   u8 hdrOffset;  
d10f0 20 20 20 20 20 20 2f 2a 20 31 30 30 20 66 6f 72        /* 100 for
d1100 20 70 61 67 65 20 31 2e 20 20 30 20 6f 74 68 65   page 1.  0 othe
d1110 72 77 69 73 65 20 2a 2f 0a 20 20 75 38 20 63 68  rwise */.  u8 ch
d1120 69 6c 64 50 74 72 53 69 7a 65 3b 20 20 20 20 20  ildPtrSize;     
d1130 2f 2a 20 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e  /* 0 if leaf==1.
d1140 20 20 34 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a    4 if leaf==0 *
d1150 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c  /.  u16 maxLocal
d1160 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79  ;        /* Copy
d1170 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 61 78   of BtShared.max
d1180 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65  Local or BtShare
d1190 64 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75  d.maxLeaf */.  u
d11a0 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20  16 minLocal;    
d11b0 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42      /* Copy of B
d11c0 74 53 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c  tShared.minLocal
d11d0 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 69 6e   or BtShared.min
d11e0 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 65  Leaf */.  u16 ce
d11f0 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f  llOffset;      /
d1200 2a 20 49 6e 64 65 78 20 69 6e 20 61 44 61 74 61  * Index in aData
d1210 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70   of first cell p
d1220 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 20  ointer */.  u16 
d1230 69 64 78 50 61 72 65 6e 74 3b 20 20 20 20 20 20  idxParent;      
d1240 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 61 72   /* Index in par
d1250 65 6e 74 20 6f 66 20 74 68 69 73 20 6e 6f 64 65  ent of this node
d1260 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 65 3b   */.  u16 nFree;
d1270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
d1280 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 74  mber of free byt
d1290 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a  es on the page *
d12a0 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20 20  /.  u16 nCell;  
d12b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
d12c0 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74  er of cells on t
d12d0 68 69 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c 20  his page, local 
d12e0 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 73 74  and ovfl */.  st
d12f0 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b  ruct _OvflCell {
d1300 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74     /* Cells that
d1310 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e   will not fit on
d1320 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20   aData[] */.    
d1330 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20  u8 *pCell;      
d1340 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20      /* Pointers 
d1350 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74  to the body of t
d1360 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  he overflow cell
d1370 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 64 78 3b   */.    u16 idx;
d1380 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
d1390 6e 73 65 72 74 20 74 68 69 73 20 63 65 6c 6c 20  nsert this cell 
d13a0 62 65 66 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f  before idx-th no
d13b0 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20  n-overflow cell 
d13c0 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b  */.  } aOvfl[5];
d13d0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
d13e0 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  ;       /* Point
d13f0 65 72 20 74 6f 20 42 74 53 68 61 72 65 64 20 74  er to BtShared t
d1400 68 61 74 20 74 68 69 73 20 70 61 67 65 20 69 73  hat this page is
d1410 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38   part of */.  u8
d1420 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20   *aData;        
d1430 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
d1440 20 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 74   disk image of t
d1450 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a  he page data */.
d1460 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
d1470 65 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20  e;     /* Pager 
d1480 70 61 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  page handle */. 
d1490 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20   Pgno pgno;     
d14a0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
d14b0 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61  mber for this pa
d14c0 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ge */.  MemPage 
d14d0 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 2f 2a 20  *pParent;    /* 
d14e0 54 68 65 20 70 61 72 65 6e 74 20 6f 66 20 74 68  The parent of th
d14f0 69 73 20 70 61 67 65 2e 20 20 4e 55 4c 4c 20 66  is page.  NULL f
d1500 6f 72 20 72 6f 6f 74 20 2a 2f 0a 7d 3b 0a 0a 2f  or root */.};../
d1510 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f  *.** The in-memo
d1520 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69  ry image of a di
d1530 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 65 20  sk page has the 
d1540 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d  auxiliary inform
d1550 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a  ation appended.*
d1560 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45  * to the end.  E
d1570 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65  XTRA_SIZE is the
d1580 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
d1590 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64   of space needed
d15a0 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74   to hold.** that
d15b0 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69   extra informati
d15c0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  on..*/.#define E
d15d0 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66  XTRA_SIZE sizeof
d15e0 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 20 41 20  (MemPage)../* A 
d15f0 42 74 72 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a  Btree handle.**.
d1600 2a 2a 20 41 20 64 61 74 61 62 61 73 65 20 63 6f  ** A database co
d1610 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e  nnection contain
d1620 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
d1630 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a  n instance of.**
d1640 20 74 68 69 73 20 6f 62 6a 65 63 74 20 66 6f 72   this object for
d1650 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20   every database 
d1660 66 69 6c 65 20 74 68 61 74 20 69 74 20 68 61 73  file that it has
d1670 20 6f 70 65 6e 2e 20 20 54 68 69 73 20 73 74 72   open.  This str
d1680 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61  ucture.** is opa
d1690 71 75 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  que to the datab
d16a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ase connection. 
d16b0 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
d16c0 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a  nnection cannot.
d16d0 2a 2a 20 73 65 65 20 74 68 65 20 69 6e 74 65 72  ** see the inter
d16e0 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72  nals of this str
d16f0 75 63 74 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20  ucture and only 
d1700 64 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74  deals with point
d1710 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73  ers to.** this s
d1720 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
d1730 46 6f 72 20 73 6f 6d 65 20 64 61 74 61 62 61 73  For some databas
d1740 65 20 66 69 6c 65 73 2c 20 74 68 65 20 73 61 6d  e files, the sam
d1750 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74  e underlying dat
d1760 61 62 61 73 65 20 63 61 63 68 65 20 6d 69 67 68  abase cache migh
d1770 74 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20  t be .** shared 
d1780 62 65 74 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65  between multiple
d1790 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49   connections.  I
d17a0 6e 20 74 68 61 74 20 63 61 73 65 2c 20 65 61 63  n that case, eac
d17b0 68 20 63 6f 6e 74 65 63 74 69 6f 6e 0a 2a 2a 20  h contection.** 
d17c0 68 61 73 20 69 74 20 6f 77 6e 20 70 6f 69 6e 74  has it own point
d17d0 65 72 20 74 6f 20 74 68 69 73 20 6f 62 6a 65 63  er to this objec
d17e0 74 2e 20 20 42 75 74 20 65 61 63 68 20 69 6e 73  t.  But each ins
d17f0 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
d1800 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  ject.** points t
d1810 6f 20 74 68 65 20 73 61 6d 65 20 42 74 53 68 61  o the same BtSha
d1820 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 65  red object.  The
d1830 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20   database cache 
d1840 61 6e 64 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d  and the.** schem
d1850 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
d1860 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  h the database f
d1870 69 6c 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74  ile are all cont
d1880 61 69 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20  ained within.** 
d1890 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
d18a0 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66  ect..**.** All f
d18b0 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74  ields in this st
d18c0 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65  ructure are acce
d18d0 73 73 65 64 20 75 6e 64 65 72 20 73 71 6c 69 74  ssed under sqlit
d18e0 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65  e3.mutex..** The
d18f0 20 70 42 74 20 70 6f 69 6e 74 65 72 20 69 74 73   pBt pointer its
d1900 65 6c 66 20 6d 61 79 20 6e 6f 74 20 62 65 20 63  elf may not be c
d1910 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74 68 65  hanged while the
d1920 72 65 20 65 78 69 73 74 73 20 63 75 72 73 6f 72  re exists cursor
d1930 73 20 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 66  s .** in the ref
d1940 65 72 65 6e 63 65 64 20 42 74 53 68 61 72 65 64  erenced BtShared
d1950 20 74 68 61 74 20 70 6f 69 6e 74 20 62 61 63 6b   that point back
d1960 20 74 6f 20 74 68 69 73 20 42 74 72 65 65 20 73   to this Btree s
d1970 69 6e 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75  ince those.** cu
d1980 72 73 6f 72 73 20 68 61 76 65 20 74 6f 20 64 6f  rsors have to do
d1990 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69 73   go through this
d19a0 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 74   Btree to find t
d19b0 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61 6e  heir BtShared an
d19c0 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e 20  d.** they often 
d19d0 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68 6f  do so without ho
d19e0 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75  lding sqlite3.mu
d19f0 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42  tex..*/.struct B
d1a00 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  tree {.  sqlite3
d1a10 20 2a 70 53 71 6c 69 74 65 3b 20 20 2f 2a 20 54   *pSqlite;  /* T
d1a20 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
d1a30 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74  ection holding t
d1a40 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42  his btree */.  B
d1a50 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20  tShared *pBt;   
d1a60 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 6f    /* Sharable co
d1a70 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62 74  ntent of this bt
d1a80 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72  ree */.  u8 inTr
d1a90 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ans;        /* T
d1aa0 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53  RANS_NONE, TRANS
d1ab0 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f 57  _READ or TRANS_W
d1ac0 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 61  RITE */.  u8 sha
d1ad0 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20  rable;       /* 
d1ae0 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20 73  True if we can s
d1af0 68 61 72 65 20 70 42 74 20 77 69 74 68 20 6f 74  hare pBt with ot
d1b00 68 65 72 20 70 53 71 6c 69 74 65 20 2a 2f 0a 20  her pSqlite */. 
d1b10 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20   u8 locked;     
d1b20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
d1b30 53 71 6c 69 74 65 20 63 75 72 72 65 6e 74 6c 79  Sqlite currently
d1b40 20 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20   has pBt locked 
d1b50 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c  */.  int wantToL
d1b60 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ock;    /* Numbe
d1b70 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c  r of nested call
d1b80 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  s to sqlite3Btre
d1b90 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 42 74  eEnter() */.  Bt
d1ba0 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  ree *pNext;     
d1bb0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65   /* List of othe
d1bc0 72 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65  r sharable Btree
d1bd0 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20  s from the same 
d1be0 70 53 71 6c 69 74 65 20 2a 2f 0a 20 20 42 74 72  pSqlite */.  Btr
d1bf0 65 65 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20  ee *pPrev;      
d1c00 2f 2a 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20  /* Back pointer 
d1c10 6f 66 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74  of the same list
d1c20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74   */.};../*.** Bt
d1c30 72 65 65 2e 69 6e 54 72 61 6e 73 20 6d 61 79 20  ree.inTrans may 
d1c40 74 61 6b 65 20 6f 6e 65 20 6f 66 20 74 68 65 20  take one of the 
d1c50 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73  following values
d1c60 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73  ..**.** If the s
d1c70 68 61 72 65 64 2d 64 61 74 61 20 65 78 74 65 6e  hared-data exten
d1c80 73 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c  sion is enabled,
d1c90 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 6d 75   there may be mu
d1ca0 6c 74 69 70 6c 65 20 75 73 65 72 73 0a 2a 2a 20  ltiple users.** 
d1cb0 6f 66 20 74 68 65 20 42 74 72 65 65 20 73 74 72  of the Btree str
d1cc0 75 63 74 75 72 65 2e 20 41 74 20 6d 6f 73 74 20  ucture. At most 
d1cd0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 6d 61 79  one of these may
d1ce0 20 6f 70 65 6e 20 61 20 77 72 69 74 65 20 74 72   open a write tr
d1cf0 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75  ansaction,.** bu
d1d00 74 20 61 6e 79 20 6e 75 6d 62 65 72 20 6d 61 79  t any number may
d1d10 20 68 61 76 65 20 61 63 74 69 76 65 20 72 65 61   have active rea
d1d20 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a  d transactions..
d1d30 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53  */.#define TRANS
d1d40 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 66 69 6e 65  _NONE  0.#define
d1d50 20 54 52 41 4e 53 5f 52 45 41 44 20 20 31 0a 23   TRANS_READ  1.#
d1d60 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 57 52 49  define TRANS_WRI
d1d70 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  TE 2../*.** An i
d1d80 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
d1d90 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74  object represent
d1da0 73 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62  s a single datab
d1db0 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a  ase file..** .**
d1dc0 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   A single databa
d1dd0 73 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 69  se file can be i
d1de0 6e 20 75 73 65 20 61 73 20 74 68 65 20 73 61 6d  n use as the sam
d1df0 65 20 74 69 6d 65 20 62 79 20 74 77 6f 0a 2a 2a  e time by two.**
d1e00 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73   or more databas
d1e10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20  e connections.  
d1e20 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65  When two or more
d1e30 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65   connections are
d1e40 0a 2a 2a 20 73 68 61 72 69 6e 67 20 74 68 65 20  .** sharing the 
d1e50 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69  same database fi
d1e60 6c 65 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74  le, each connect
d1e70 69 6f 6e 20 68 61 73 20 69 74 20 6f 77 6e 0a 2a  ion has it own.*
d1e80 2a 20 70 72 69 76 61 74 65 20 42 74 72 65 65 20  * private Btree 
d1e90 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 66  object for the f
d1ea0 69 6c 65 20 61 6e 64 20 65 61 63 68 20 6f 66 20  ile and each of 
d1eb0 74 68 6f 73 65 20 42 74 72 65 65 73 20 70 6f 69  those Btrees poi
d1ec0 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 69 73 20 6f  nts.** to this o
d1ed0 6e 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  ne BtShared obje
d1ee0 63 74 2e 20 20 42 74 53 68 61 72 65 64 2e 6e 52  ct.  BtShared.nR
d1ef0 65 66 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ef is the number
d1f00 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f   of.** connectio
d1f10 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 73 68 61  ns currently sha
d1f20 72 69 6e 67 20 74 68 69 73 20 64 61 74 61 62 61  ring this databa
d1f30 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46  se file..**.** F
d1f40 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74  ields in this st
d1f50 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65  ructure are acce
d1f60 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42  ssed under the B
d1f70 74 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a  tShared.mutex.**
d1f80 20 6d 75 74 65 78 2c 20 65 78 63 65 70 74 20 66   mutex, except f
d1f90 6f 72 20 6e 52 65 66 20 61 6e 64 20 70 4e 65 78  or nRef and pNex
d1fa0 74 20 77 68 69 63 68 20 61 72 65 20 61 63 63 65  t which are acce
d1fb0 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 0a 2a  ssed under the.*
d1fc0 2a 20 67 6c 6f 62 61 6c 20 53 51 4c 49 54 45 5f  * global SQLITE_
d1fd0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
d1fe0 54 45 52 20 6d 75 74 65 78 2e 20 20 54 68 65 20  TER mutex.  The 
d1ff0 70 50 61 67 65 72 20 66 69 65 6c 64 0a 2a 2a 20  pPager field.** 
d2000 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66  may not be modif
d2010 69 65 64 20 6f 6e 63 65 20 69 74 20 69 73 20 69  ied once it is i
d2020 6e 69 74 69 61 6c 6c 79 20 73 65 74 20 61 73 20  nitially set as 
d2030 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a  long as nRef>0..
d2040 2a 2a 20 54 68 65 20 70 53 63 68 65 6d 61 20 66  ** The pSchema f
d2050 69 65 6c 64 20 6d 61 79 20 62 65 20 73 65 74 20  ield may be set 
d2060 6f 6e 63 65 20 75 6e 64 65 72 20 42 74 53 68 61  once under BtSha
d2070 72 65 64 2e 6d 75 74 65 78 20 61 6e 64 0a 2a 2a  red.mutex and.**
d2080 20 74 68 65 72 65 61 66 74 65 72 20 69 73 20 75   thereafter is u
d2090 6e 63 68 61 6e 67 65 64 20 61 73 20 6c 6f 6e 67  nchanged as long
d20a0 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2f 0a 73   as nRef>0..*/.s
d20b0 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20 7b  truct BtShared {
d20c0 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
d20d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
d20e0 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a 20 20  page cache */.  
d20f0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  BtCursor *pCurso
d2100 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20  r;    /* A list 
d2110 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73  of all open curs
d2120 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  ors */.  MemPage
d2130 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f   *pPage1;      /
d2140 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20  * First page of 
d2150 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
d2160 20 20 75 38 20 69 6e 53 74 6d 74 3b 20 20 20 20    u8 inStmt;    
d2170 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
d2180 69 66 20 77 65 20 61 72 65 20 69 6e 20 61 20 73  if we are in a s
d2190 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e  tatement subtran
d21a0 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20  saction */.  u8 
d21b0 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20  readOnly;       
d21c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
d21d0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c  e underlying fil
d21e0 65 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f  e is readonly */
d21f0 0a 20 20 75 38 20 6d 61 78 45 6d 62 65 64 46 72  .  u8 maxEmbedFr
d2200 61 63 3b 20 20 20 20 20 20 2f 2a 20 4d 61 78 69  ac;      /* Maxi
d2210 6d 75 6d 20 70 61 79 6c 6f 61 64 20 61 73 20 25  mum payload as %
d2220 20 6f 66 20 74 6f 74 61 6c 20 70 61 67 65 20 73   of total page s
d2230 69 7a 65 20 2a 2f 0a 20 20 75 38 20 6d 69 6e 45  ize */.  u8 minE
d2240 6d 62 65 64 46 72 61 63 3b 20 20 20 20 20 20 2f  mbedFrac;      /
d2250 2a 20 4d 69 6e 69 6d 75 6d 20 70 61 79 6c 6f 61  * Minimum payloa
d2260 64 20 61 73 20 25 20 6f 66 20 74 6f 74 61 6c 20  d as % of total 
d2270 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 75  page size */.  u
d2280 38 20 6d 69 6e 4c 65 61 66 46 72 61 63 3b 20 20  8 minLeafFrac;  
d2290 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20       /* Minimum 
d22a0 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 61 73 20  leaf payload as 
d22b0 25 20 6f 66 20 74 6f 74 61 6c 20 70 61 67 65 20  % of total page 
d22c0 73 69 7a 65 20 2a 2f 0a 20 20 75 38 20 70 61 67  size */.  u8 pag
d22d0 65 53 69 7a 65 46 69 78 65 64 3b 20 20 20 20 20  eSizeFixed;     
d22e0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 70  /* True if the p
d22f0 61 67 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20  age size can no 
d2300 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65  longer be change
d2310 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  d */.#ifndef SQL
d2320 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
d2330 55 55 4d 0a 20 20 75 38 20 61 75 74 6f 56 61 63  UUM.  u8 autoVac
d2340 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  uum;        /* T
d2350 72 75 65 20 69 66 20 61 75 74 6f 2d 76 61 63 75  rue if auto-vacu
d2360 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f  um is enabled */
d2370 0a 20 20 75 38 20 69 6e 63 72 56 61 63 75 75 6d  .  u8 incrVacuum
d2380 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
d2390 20 69 66 20 69 6e 63 72 2d 76 61 63 75 75 6d 20   if incr-vacuum 
d23a0 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20  is enabled */.  
d23b0 50 67 6e 6f 20 6e 54 72 75 6e 63 3b 20 20 20 20  Pgno nTrunc;    
d23c0 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72        /* Non-zer
d23d0 6f 20 69 66 20 74 68 65 20 64 62 20 77 69 6c 6c  o if the db will
d23e0 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 28 69   be truncated (i
d23f0 6e 63 72 20 76 61 63 75 75 6d 29 20 2a 2f 0a 23  ncr vacuum) */.#
d2400 65 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67 65  endif.  u16 page
d2410 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Size;         /*
d2420 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
d2430 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65   bytes on a page
d2440 20 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c 65   */.  u16 usable
d2450 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e  Size;       /* N
d2460 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20  umber of usable 
d2470 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61  bytes on each pa
d2480 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 78 4c  ge */.  int maxL
d2490 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  ocal;         /*
d24a0 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70   Maximum local p
d24b0 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45  ayload in non-LE
d24c0 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f  AFDATA tables */
d24d0 0a 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b  .  int minLocal;
d24e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69           /* Mini
d24f0 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61  mum local payloa
d2500 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54  d in non-LEAFDAT
d2510 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e  A tables */.  in
d2520 74 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20 20  t maxLeaf;      
d2530 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
d2540 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20  ocal payload in 
d2550 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65  a LEAFDATA table
d2560 20 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 4c 65 61   */.  int minLea
d2570 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  f;          /* M
d2580 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79  inimum local pay
d2590 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41  load in a LEAFDA
d25a0 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 42 75  TA table */.  Bu
d25b0 73 79 48 61 6e 64 6c 65 72 20 2a 70 42 75 73 79  syHandler *pBusy
d25c0 48 61 6e 64 6c 65 72 3b 20 20 20 2f 2a 20 43 61  Handler;   /* Ca
d25d0 6c 6c 62 61 63 6b 20 66 6f 72 20 77 68 65 6e 20  llback for when 
d25e0 74 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f  there is lock co
d25f0 6e 74 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  ntention */.  u8
d2600 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20   inTransaction; 
d2610 20 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 69      /* Transacti
d2620 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e  on state */.  in
d2630 74 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20  t nTransaction; 
d2640 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
d2650 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f   open transactio
d2660 6e 73 20 28 72 65 61 64 20 2b 20 77 72 69 74 65  ns (read + write
d2670 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63  ) */.  void *pSc
d2680 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20  hema;        /* 
d2690 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 65  Pointer to space
d26a0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71   allocated by sq
d26b0 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
d26c0 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  () */.  void (*x
d26d0 46 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 64  FreeSchema)(void
d26e0 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74  *);  /* Destruct
d26f0 6f 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 2e  or for BtShared.
d2700 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c  pSchema */.  sql
d2710 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
d2720 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73  x; /* Non-recurs
d2730 69 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 72  ive mutex requir
d2740 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69  ed to access thi
d2750 73 20 73 74 72 75 63 74 20 2a 2f 0a 23 69 66 6e  s struct */.#ifn
d2760 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
d2770 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69  SHARED_CACHE.  i
d2780 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
d2790 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
d27a0 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
d27b0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
d27c0 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e  /.  BtShared *pN
d27d0 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
d27e0 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73  t on a list of s
d27f0 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64  harable BtShared
d2800 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74   structs */.  Bt
d2810 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20  Lock *pLock;    
d2820 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c      /* List of l
d2830 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69  ocks held on thi
d2840 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73  s shared-btree s
d2850 74 72 75 63 74 20 2a 2f 0a 23 65 6e 64 69 66 0a  truct */.#endif.
d2860 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
d2870 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
d2880 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
d2890 20 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64   is used to hold
d28a0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
d28b0 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54  about a cell.  T
d28c0 68 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28  he parseCellPtr(
d28d0 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73  ) function fills
d28e0 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75   in this structu
d28f0 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69  re.** based on i
d2900 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61  nformation extra
d2910 63 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20  ct from the raw 
d2920 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79  disk page..*/.ty
d2930 70 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c  pedef struct Cel
d2940 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a  lInfo CellInfo;.
d2950 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20  struct CellInfo 
d2960 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20  {.  u8 *pCell;  
d2970 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
d2980 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65   the start of ce
d2990 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20  ll content */.  
d29a0 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f  i64 nKey;      /
d29b0 2a 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e  * The key for IN
d29c0 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20  TKEY tables, or 
d29d0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
d29e0 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20  in key */.  u32 
d29f0 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75  nData;     /* Nu
d2a00 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
d2a10 20 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e   data */.  u32 n
d2a20 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74  Payload;  /* Tot
d2a30 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79  al amount of pay
d2a40 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48  load */.  u16 nH
d2a50 65 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65  eader;   /* Size
d2a60 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   of the cell con
d2a70 74 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62  tent header in b
d2a80 79 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c  ytes */.  u16 nL
d2a90 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75  ocal;    /* Amou
d2aa0 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65  nt of payload he
d2ab0 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20  ld locally */.  
d2ac0 75 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f  u16 iOverflow; /
d2ad0 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72  * Offset to over
d2ae0 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72  flow page number
d2af0 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76  .  Zero if no ov
d2b00 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20  erflow */.  u16 
d2b10 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69  nSize;     /* Si
d2b20 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  ze of the cell c
d2b30 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61  ontent on the ma
d2b40 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a  in b-tree page *
d2b50 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75  /.};../*.** A cu
d2b60 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 65  rsor is a pointe
d2b70 72 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61  r to a particula
d2b80 72 20 65 6e 74 72 79 20 77 69 74 68 69 6e 20 61  r entry within a
d2b90 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 62   particular.** b
d2ba0 2d 74 72 65 65 20 77 69 74 68 69 6e 20 61 20 64  -tree within a d
d2bb0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
d2bc0 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 20 69 73  .** The entry is
d2bd0 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 69   identified by i
d2be0 74 73 20 4d 65 6d 50 61 67 65 20 61 6e 64 20 74  ts MemPage and t
d2bf0 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d  he index in.** M
d2c00 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f  emPage.aCell[] o
d2c10 66 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a  f the entry..**.
d2c20 2a 2a 20 57 68 65 6e 20 61 20 73 69 6e 67 6c 65  ** When a single
d2c30 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63   database file c
d2c40 61 6e 20 73 68 61 72 65 64 20 62 79 20 74 77 6f  an shared by two
d2c50 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 63   more database c
d2c60 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62  onnections,.** b
d2c70 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e 6e 6f  ut cursors canno
d2c80 74 20 62 65 20 73 68 61 72 65 64 2e 20 20 45 61  t be shared.  Ea
d2c90 63 68 20 63 75 72 73 6f 72 20 69 73 20 61 73 73  ch cursor is ass
d2ca0 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a 2a  ociated with a.*
d2cb0 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74  * particular dat
d2cc0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
d2cd0 20 69 64 65 6e 74 69 66 69 65 64 20 42 74 43 75   identified BtCu
d2ce0 72 73 6f 72 2e 70 42 74 72 65 65 2e 70 53 71 6c  rsor.pBtree.pSql
d2cf0 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64  ite..**.** Field
d2d00 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74  s in this struct
d2d10 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64  ure are accessed
d2d20 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 68 61   under the BtSha
d2d30 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 66 6f 75  red.mutex.** fou
d2d40 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 42 74 2d  nd at self->pBt-
d2d50 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 74 72 75  >mutex. .*/.stru
d2d60 63 74 20 42 74 43 75 72 73 6f 72 20 7b 0a 20 20  ct BtCursor {.  
d2d70 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 20  Btree *pBtree;  
d2d80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
d2d90 20 42 74 72 65 65 20 74 6f 20 77 68 69 63 68 20   Btree to which 
d2da0 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c 6f  this cursor belo
d2db0 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 61 72 65  ngs */.  BtShare
d2dc0 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20  d *pBt;         
d2dd0 20 20 20 2f 2a 20 54 68 65 20 42 74 53 68 61 72     /* The BtShar
d2de0 65 64 20 74 68 69 73 20 63 75 72 73 6f 72 20 70  ed this cursor p
d2df0 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 20 42 74  oints to */.  Bt
d2e00 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 2c 20 2a  Cursor *pNext, *
d2e10 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f 72 6d 73  pPrev;  /* Forms
d2e20 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
d2e30 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 2a 2f  f all cursors */
d2e40 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 70 61 72  .  int (*xCompar
d2e50 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  e)(void*,int,con
d2e60 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
d2e70 73 74 20 76 6f 69 64 2a 29 3b 20 2f 2a 20 4b 65  st void*); /* Ke
d2e80 79 20 63 6f 6d 70 20 66 75 6e 63 20 2a 2f 0a 20  y comp func */. 
d2e90 20 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20   void *pArg;    
d2ea0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
d2eb0 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70  rst arg to xComp
d2ec0 61 72 65 28 29 20 2a 2f 0a 20 20 50 67 6e 6f 20  are() */.  Pgno 
d2ed0 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20 20 20 20  pgnoRoot;       
d2ee0 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74       /* The root
d2ef0 20 70 61 67 65 20 6f 66 20 74 68 69 73 20 74 72   page of this tr
d2f00 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ee */.  MemPage 
d2f10 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  *pPage;         
d2f20 20 20 2f 2a 20 50 61 67 65 20 74 68 61 74 20 63    /* Page that c
d2f30 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72  ontains the entr
d2f40 79 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 20  y */.  int idx; 
d2f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d2f60 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
d2f70 20 65 6e 74 72 79 20 69 6e 20 70 50 61 67 65 2d   entry in pPage-
d2f80 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 43 65  >aCell[] */.  Ce
d2f90 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 20 20  llInfo info;    
d2fa0 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72          /* A par
d2fb0 73 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 77  se of the cell w
d2fc0 65 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61  e are pointing a
d2fd0 74 20 2a 2f 0a 20 20 75 38 20 77 72 46 6c 61 67  t */.  u8 wrFlag
d2fe0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
d2ff0 20 2f 2a 20 54 72 75 65 20 69 66 20 77 72 69 74   /* True if writ
d3000 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 53 74  able */.  u8 eSt
d3010 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ate;            
d3020 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
d3030 65 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e  e CURSOR_XXX con
d3040 73 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c 6f  stants (see belo
d3050 77 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b  w) */.  void *pK
d3060 65 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65  ey;      /* Save
d3070 64 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 63  d key that was c
d3080 75 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f  ursor's last kno
d3090 77 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20  wn position */. 
d30a0 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20   i64 nKey;      
d30b0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65    /* Size of pKe
d30c0 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 65 67  y, or last integ
d30d0 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  er key */.  int 
d30e0 73 6b 69 70 3b 20 20 20 20 20 20 20 20 2f 2a 20  skip;        /* 
d30f0 28 73 6b 69 70 3c 30 29 20 2d 3e 20 50 72 65 76  (skip<0) -> Prev
d3100 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 28  () is a no-op. (
d3110 73 6b 69 70 3e 30 29 20 2d 3e 20 4e 65 78 74 28  skip>0) -> Next(
d3120 29 20 69 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20  ) is */.#ifndef 
d3130 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
d3140 42 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e 63 72  BLOB.  u8 isIncr
d3150 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 20 20  blobHandle;     
d3160 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
d3170 20 63 75 72 73 6f 72 20 69 73 20 61 6e 20 69 6e   cursor is an in
d3180 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 2a 2f  cr. io handle */
d3190 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66 6c  .  Pgno *aOverfl
d31a0 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ow;          /* 
d31b0 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c 6f  Cache of overflo
d31c0 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 73  w page locations
d31d0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
d31e0 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76  *.** Potential v
d31f0 61 6c 75 65 73 20 66 6f 72 20 42 74 43 75 72 73  alues for BtCurs
d3200 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  or.eState..**.**
d3210 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a   CURSOR_VALID:.*
d3220 2a 20 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74  *   Cursor point
d3230 73 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74  s to a valid ent
d3240 72 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29  ry. getPayload()
d3250 20 65 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c   etc. may be cal
d3260 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f  led..**.** CURSO
d3270 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20  R_INVALID:.**   
d3280 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20  Cursor does not 
d3290 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64  point to a valid
d32a0 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e   entry. This can
d32b0 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61   happen (for exa
d32c0 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61  mple) .**   beca
d32d0 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73  use the table is
d32e0 20 65 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73   empty or becaus
d32f0 65 20 42 74 72 65 65 43 75 72 73 6f 72 46 69 72  e BtreeCursorFir
d3300 73 74 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65  st() has not bee
d3310 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a  n.**   called..*
d3320 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55  *.** CURSOR_REQU
d3330 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68  IRESEEK:.**   Th
d3340 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 69  e table that thi
d3350 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65  s cursor was ope
d3360 6e 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69  ned on still exi
d3370 73 74 73 2c 20 62 75 74 20 68 61 73 20 62 65 65  sts, but has bee
d3380 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64  n .**   modified
d3390 20 73 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f   since the curso
d33a0 72 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e  r was last used.
d33b0 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69   The cursor posi
d33c0 74 69 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a  tion is saved.**
d33d0 20 20 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20     in variables 
d33e0 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e  BtCursor.pKey an
d33f0 64 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e  d BtCursor.nKey.
d3400 20 57 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69   When a cursor i
d3410 73 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20  s in .**   this 
d3420 73 74 61 74 65 2c 20 72 65 73 74 6f 72 65 4f 72  state, restoreOr
d3430 43 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69 74  ClearCursorPosit
d3440 69 6f 6e 28 29 20 63 61 6e 20 62 65 20 63 61 6c  ion() can be cal
d3450 6c 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74  led to attempt t
d3460 6f 0a 2a 2a 20 20 20 73 65 65 6b 20 74 68 65 20  o.**   seek the 
d3470 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 73 61  cursor to the sa
d3480 76 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a  ved position..**
d3490 0a 2a 2a 20 43 55 52 53 4f 52 5f 46 41 55 4c 54  .** CURSOR_FAULT
d34a0 3a 0a 2a 2a 20 20 20 41 20 75 6e 72 65 63 6f 76  :.**   A unrecov
d34b0 65 72 61 62 6c 65 20 65 72 72 6f 72 20 28 61 6e  erable error (an
d34c0 20 49 2f 4f 20 65 72 72 6f 72 20 6f 72 20 61 20   I/O error or a 
d34d0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 29 20  malloc failure) 
d34e0 68 61 73 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20  has occurred.** 
d34f0 20 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74    on a different
d3500 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
d3510 20 73 68 61 72 65 73 20 74 68 65 20 42 74 53 68   shares the BtSh
d3520 61 72 65 64 20 63 61 63 68 65 20 77 69 74 68 20  ared cache with 
d3530 74 68 69 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72  this.**   cursor
d3540 2e 20 20 54 68 65 20 65 72 72 6f 72 20 68 61 73  .  The error has
d3550 20 6c 65 66 74 20 74 68 65 20 63 61 63 68 65 20   left the cache 
d3560 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
d3570 6e 74 20 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44  nt state..**   D
d3580 6f 20 6e 6f 74 68 69 6e 67 20 65 6c 73 65 20 77  o nothing else w
d3590 69 74 68 20 74 68 69 73 20 63 75 72 73 6f 72 2e  ith this cursor.
d35a0 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f    Any attempt to
d35b0 20 75 73 65 20 74 68 65 20 63 75 72 73 6f 72 0a   use the cursor.
d35c0 2a 2a 20 20 20 73 68 6f 75 6c 64 20 72 65 74 75  **   should retu
d35d0 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  rn the error cod
d35e0 65 20 73 74 6f 72 65 64 20 69 6e 20 42 74 43 75  e stored in BtCu
d35f0 72 73 6f 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65  rsor.skip.*/.#de
d3600 66 69 6e 65 20 43 55 52 53 4f 52 5f 49 4e 56 41  fine CURSOR_INVA
d3610 4c 49 44 20 20 20 20 20 20 20 20 20 20 20 30 0a  LID           0.
d3620 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 56  #define CURSOR_V
d3630 41 4c 49 44 20 20 20 20 20 20 20 20 20 20 20 20  ALID            
d3640 20 31 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f   1.#define CURSO
d3650 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 20 20  R_REQUIRESEEK   
d3660 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 43 55      2.#define CU
d3670 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 20 20 20  RSOR_FAULT      
d3680 20 20 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20         3../*.** 
d3690 54 68 65 20 54 52 41 43 45 20 6d 61 63 72 6f 20  The TRACE macro 
d36a0 77 69 6c 6c 20 70 72 69 6e 74 20 68 69 67 68 2d  will print high-
d36b0 6c 65 76 65 6c 20 73 74 61 74 75 73 20 69 6e 66  level status inf
d36c0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
d36d0 68 65 0a 2a 2a 20 62 74 72 65 65 20 6f 70 65 72  he.** btree oper
d36e0 61 74 69 6f 6e 20 77 68 65 6e 20 74 68 65 20 67  ation when the g
d36f0 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 73  lobal variable s
d3700 71 6c 69 74 65 33 5f 62 74 72 65 65 5f 74 72 61  qlite3_btree_tra
d3710 63 65 20 69 73 0a 2a 2a 20 65 6e 61 62 6c 65 64  ce is.** enabled
d3720 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
d3730 54 45 53 54 0a 23 20 64 65 66 69 6e 65 20 54 52  TEST.# define TR
d3740 41 43 45 28 58 29 20 20 20 69 66 28 20 73 71 6c  ACE(X)   if( sql
d3750 69 74 65 33 5f 62 74 72 65 65 5f 74 72 61 63 65  ite3_btree_trace
d3760 20 29 7b 20 70 72 69 6e 74 66 20 58 3b 20 66 66   ){ printf X; ff
d3770 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 20 7d 0a  lush(stdout); }.
d3780 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 54  #else.# define T
d3790 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a  RACE(X).#endif..
d37a0 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74  /*.** Routines t
d37b0 6f 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 65  o read and write
d37c0 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
d37d0 20 69 6e 74 65 67 65 72 73 2e 20 20 54 68 65 73   integers.  Thes
d37e0 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20  e used to.** be 
d37f0 64 65 66 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c  defined locally,
d3800 20 62 75 74 20 6e 6f 77 20 77 65 20 75 73 65 20   but now we use 
d3810 74 68 65 20 76 61 72 69 6e 74 20 72 6f 75 74 69  the varint routi
d3820 6e 65 73 20 69 6e 20 74 68 65 20 75 74 69 6c 2e  nes in the util.
d3830 63 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64  c.** file..*/.#d
d3840 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20  efine getVarint 
d3850 20 20 20 73 71 6c 69 74 65 33 47 65 74 56 61 72     sqlite3GetVar
d3860 69 6e 74 0a 23 64 65 66 69 6e 65 20 67 65 74 56  int.#define getV
d3870 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 28  arint32(A,B)  ((
d3880 2a 42 3d 2a 28 41 29 29 3c 3d 30 78 37 66 3f 31  *B=*(A))<=0x7f?1
d3890 3a 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e  :sqlite3GetVarin
d38a0 74 33 32 28 41 2c 42 29 29 0a 23 64 65 66 69 6e  t32(A,B)).#defin
d38b0 65 20 70 75 74 56 61 72 69 6e 74 20 20 20 20 73  e putVarint    s
d38c0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 0a  qlite3PutVarint.
d38d0 0a 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65  ./* The database
d38e0 20 70 61 67 65 20 74 68 65 20 50 45 4e 44 49 4e   page the PENDIN
d38f0 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65 73 2e  G_BYTE occupies.
d3900 20 54 68 69 73 20 70 61 67 65 20 69 73 20 6e 65   This page is ne
d3910 76 65 72 20 75 73 65 64 2e 0a 2a 2a 20 54 4f 44  ver used..** TOD
d3920 4f 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73  O: This macro is
d3930 20 76 65 72 79 20 73 69 6d 69 6c 61 72 79 20 74   very similary t
d3940 6f 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  o PAGER_MJ_PGNO(
d3950 29 20 69 6e 20 70 61 67 65 72 2e 63 2e 20 54 68  ) in pager.c. Th
d3960 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 70 6f 73  ey.** should pos
d3970 73 69 62 6c 79 20 62 65 20 63 6f 6e 73 6f 6c 69  sibly be consoli
d3980 64 61 74 65 64 20 28 70 72 65 73 75 6d 61 62 6c  dated (presumabl
d3990 79 20 69 6e 20 70 61 67 65 72 2e 68 29 2e 0a 2a  y in pager.h)..*
d39a0 2a 0a 2a 2a 20 49 66 20 64 69 73 6b 20 49 2f 4f  *.** If disk I/O
d39b0 20 69 73 20 6f 6d 69 74 74 65 64 20 28 6d 65 61   is omitted (mea
d39c0 6e 69 6e 67 20 74 68 61 74 20 74 68 65 20 64 61  ning that the da
d39d0 74 61 62 61 73 65 20 69 73 20 73 74 6f 72 65 64  tabase is stored
d39e0 20 70 75 72 65 6c 79 0a 2a 2a 20 69 6e 20 6d 65   purely.** in me
d39f0 6d 6f 72 79 29 20 74 68 65 6e 20 74 68 65 72 65  mory) then there
d3a00 20 69 73 20 6e 6f 20 70 65 6e 64 69 6e 67 20 62   is no pending b
d3a10 79 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  yte..*/.#ifdef S
d3a20 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
d3a30 4f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49  O.# define PENDI
d3a40 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
d3a50 29 20 20 30 78 37 66 66 66 66 66 66 66 0a 23 65  )  0x7fffffff.#e
d3a60 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 45 4e  lse.# define PEN
d3a70 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
d3a80 42 74 29 20 28 28 50 45 4e 44 49 4e 47 5f 42 59  Bt) ((PENDING_BY
d3a90 54 45 2f 28 70 42 74 29 2d 3e 70 61 67 65 53 69  TE/(pBt)->pageSi
d3aa0 7a 65 29 2b 31 29 0a 23 65 6e 64 69 66 0a 0a 2f  ze)+1).#endif../
d3ab0 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69  *.** A linked li
d3ac0 73 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  st of the follow
d3ad0 69 6e 67 20 73 74 72 75 63 74 75 72 65 73 20 69  ing structures i
d3ae0 73 20 73 74 6f 72 65 64 20 61 74 20 42 74 53 68  s stored at BtSh
d3af0 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c  ared.pLock..** L
d3b00 6f 63 6b 73 20 61 72 65 20 61 64 64 65 64 20 28  ocks are added (
d3b10 6f 72 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d  or upgraded from
d3b20 20 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52   READ_LOCK to WR
d3b30 49 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61  ITE_LOCK) when a
d3b40 20 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f   cursor .** is o
d3b50 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62  pened on the tab
d3b60 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67  le with root pag
d3b70 65 20 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c  e BtShared.iTabl
d3b80 65 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d  e. Locks are rem
d3b90 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69  oved.** from thi
d3ba0 73 20 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72  s list when a tr
d3bb0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
d3bc0 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64  mitted or rolled
d3bd0 20 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a   back, or when.*
d3be0 2a 20 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65  * a btree handle
d3bf0 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73   is closed..*/.s
d3c00 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20  truct BtLock {. 
d3c10 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20   Btree *pBtree; 
d3c20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
d3c30 68 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74  handle holding t
d3c40 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67  his lock */.  Pg
d3c50 6e 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20  no iTable;      
d3c60 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65      /* Root page
d3c70 20 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75   of table */.  u
d3c80 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20  8 eLock;        
d3c90 20 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43       /* READ_LOC
d3ca0 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20  K or WRITE_LOCK 
d3cb0 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65  */.  BtLock *pNe
d3cc0 78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  xt;        /* Ne
d3cd0 78 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70  xt in BtShared.p
d3ce0 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a  Lock list */.};.
d3cf0 0a 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61  ./* Candidate va
d3d00 6c 75 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e  lues for BtLock.
d3d10 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65  eLock */.#define
d3d20 20 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31   READ_LOCK     1
d3d30 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c  .#define WRITE_L
d3d40 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20  OCK    2../*.** 
d3d50 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 66  These macros def
d3d60 69 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e  ine the location
d3d70 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   of the pointer-
d3d80 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61 20  map entry for a 
d3d90 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67  .** database pag
d3da0 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67  e. The first arg
d3db0 75 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69 73  ument to each is
d3dc0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75   the number of u
d3dd0 73 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20 6f  sable.** bytes o
d3de0 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74  n each page of t
d3df0 68 65 20 64 61 74 61 62 61 73 65 20 28 6f 66 74  he database (oft
d3e00 65 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73 65  en 1024). The se
d3e10 63 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20 70  cond is the.** p
d3e20 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f  age number to lo
d3e30 6f 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f 69  ok up in the poi
d3e40 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20  nter map..**.** 
d3e50 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72 65  PTRMAP_PAGENO re
d3e60 74 75 72 6e 73 20 74 68 65 20 64 61 74 61 62 61  turns the databa
d3e70 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  se page number o
d3e80 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  f the pointer-ma
d3e90 70 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20 73  p.** page that s
d3ea0 74 6f 72 65 73 20 74 68 65 20 72 65 71 75 69 72  tores the requir
d3eb0 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52 4d  ed pointer. PTRM
d3ec0 41 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65 74  AP_PTROFFSET ret
d3ed0 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73  urns.** the offs
d3ee0 65 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73  et of the reques
d3ef0 74 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a 2a  ted map entry..*
d3f00 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e 6f  *.** If the pgno
d3f10 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
d3f20 20 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45 4e   to PTRMAP_PAGEN
d3f30 4f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d  O is a pointer-m
d3f40 61 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e  ap page,.** then
d3f50 20 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e 65   pgno is returne
d3f60 64 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 52  d. So (pgno==PTR
d3f70 4d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a 2c  MAP_PAGENO(pgsz,
d3f80 20 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a 2a   pgno)) can be.*
d3f90 2a 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 69  * used to test i
d3fa0 66 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69 6e  f pgno is a poin
d3fb0 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50 54  ter-map page. PT
d3fc0 52 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70 6c  RMAP_ISPAGE impl
d3fd0 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20 74  ements.** this t
d3fe0 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  est..*/.#define 
d3ff0 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42  PTRMAP_PAGENO(pB
d4000 74 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70 50  t, pgno) ptrmapP
d4010 61 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f 29  ageno(pBt, pgno)
d4020 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
d4030 50 54 52 4f 46 46 53 45 54 28 70 42 74 2c 20 70  PTROFFSET(pBt, p
d4040 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 74  gno) (5*(pgno-pt
d4050 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20  rmapPageno(pBt, 
d4060 70 67 6e 6f 29 2d 31 29 29 0a 23 64 65 66 69 6e  pgno)-1)).#defin
d4070 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28  e PTRMAP_ISPAGE(
d4080 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 4d  pBt, pgno) (PTRM
d4090 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 2c  AP_PAGENO((pBt),
d40a0 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 29  (pgno))==(pgno))
d40b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ../*.** The poin
d40c0 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f 6f  ter map is a loo
d40d0 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 69  kup table that i
d40e0 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61  dentifies the pa
d40f0 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a 2a  rent page for.**
d4100 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 65   each child page
d4110 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
d4120 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 65   file.  The pare
d4130 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20 70  nt page is the p
d4140 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 74  age that.** cont
d4150 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
d4160 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 76  o the child.  Ev
d4170 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
d4180 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
d4190 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 65  s.** 0 or 1 pare
d41a0 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 74  nt pages.  (In t
d41b0 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 74  his context 'dat
d41c0 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66 65  abase page' refe
d41d0 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 67  rs.** to any pag
d41e0 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61  e that is not pa
d41f0 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65  rt of the pointe
d4200 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 20  r map itself.)  
d4210 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 70  Each pointer map
d4220 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 73  .** entry consis
d4230 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 62  ts of a single b
d4240 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 61  yte 'type' and a
d4250 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 70   4 byte parent p
d4260 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 54  age number..** T
d4270 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 64  he PTRMAP_XXX id
d4280 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 20  entifiers below 
d4290 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 79  are the valid ty
d42a0 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  pes..**.** The p
d42b0 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 6f  urpose of the po
d42c0 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f 20  inter map is to 
d42d0 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20  facility moving 
d42e0 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a 2a  pages from one.*
d42f0 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68  * position in th
d4300 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65  e file to anothe
d4310 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 74  r as part of aut
d4320 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 61  ovacuum.  When a
d4330 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 65   page.** is move
d4340 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69  d, the pointer i
d4350 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 73  n its parent mus
d4360 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f 20  t be updated to 
d4370 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20  point to the.** 
d4380 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54  new location.  T
d4390 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
d43a0 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  s used to locate
d43b0 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
d43c0 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20   quickly..**.** 
d43d0 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 3a  PTRMAP_ROOTPAGE:
d43e0 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
d43f0 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67  ge is a root-pag
d4400 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62  e. The page-numb
d4410 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20  er is not.**    
d4420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73                us
d4430 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ed in this case.
d4440 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 52  .**.** PTRMAP_FR
d4450 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 61  EEPAGE: The data
d4460 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e 20  base page is an 
d4470 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 61  unused (free) pa
d4480 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d  ge. The page-num
d4490 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ber .**         
d44a0 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 20           is not 
d44b0 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73  used in this cas
d44c0 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  e..**.** PTRMAP_
d44d0 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 64  OVERFLOW1: The d
d44e0 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20  atabase page is 
d44f0 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  the first page i
d4500 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a 20  n a list of .** 
d4510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d4520 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73    overflow pages
d4530 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  . The page numbe
d4540 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  r identifies the
d4550 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 20   page that.**   
d4560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d4570 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c  contains the cel
d4580 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72  l with a pointer
d4590 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   to this overflo
d45a0 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54  w page..**.** PT
d45b0 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20  RMAP_OVERFLOW2: 
d45c0 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  The database pag
d45d0 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  e is the second 
d45e0 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 6e  or later page in
d45f0 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20   a list of.**   
d4600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d4610 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
d4620 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  The page-number 
d4630 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70  identifies the p
d4640 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 20  revious.**      
d4650 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
d4660 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f  e in the overflo
d4670 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 0a  w page list..**.
d4680 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a  ** PTRMAP_BTREE:
d4690 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
d46a0 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74  ge is a non-root
d46b0 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 65   btree page. The
d46c0 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20   page number.** 
d46d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 64                id
d46e0 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72  entifies the par
d46f0 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ent page in the 
d4700 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  btree..*/.#defin
d4710 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  e PTRMAP_ROOTPAG
d4720 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  E 1.#define PTRM
d4730 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 64  AP_FREEPAGE 2.#d
d4740 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45  efine PTRMAP_OVE
d4750 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e 65  RFLOW1 3.#define
d4760 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
d4770 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  2 4.#define PTRM
d4780 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 41  AP_BTREE 5../* A
d4790 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 74   bunch of assert
d47a0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  () statements to
d47b0 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e 73   check the trans
d47c0 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 72  action state var
d47d0 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 6e  iables.** of han
d47e0 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 65  dle p (type Btre
d47f0 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 6c  e*) are internal
d4800 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a  ly consistent..*
d4810 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 49  /.#define btreeI
d4820 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 20  ntegrity(p) \.  
d4830 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e  assert( p->pBt->
d4840 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54  inTransaction!=T
d4850 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e  RANS_NONE || p->
d4860 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f  pBt->nTransactio
d4870 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 65  n==0 ); \.  asse
d4880 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72  rt( p->pBt->inTr
d4890 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e  ansaction>=p->in
d48a0 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a  Trans ); .../*.*
d48b0 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 55  * The ISAUTOVACU
d48c0 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  UM macro is used
d48d0 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f   within balance_
d48e0 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 74  nonroot() to det
d48f0 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65  ermine.** if the
d4900 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
d4910 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f  ts auto-vacuum o
d4920 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 69  r not. Because i
d4930 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 74  t is used.** wit
d4940 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f  hin an expressio
d4950 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 67  n that is an arg
d4960 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72  ument to another
d4970 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69   macro .** (sqli
d4980 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74  teMallocRaw), it
d4990 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
d49a0 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 6f   to use conditio
d49b0 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e  nal compilation.
d49c0 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 63  .** So, this mac
d49d0 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e  ro is defined in
d49e0 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  stead..*/.#ifnde
d49f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
d4a00 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65  TOVACUUM.#define
d4a10 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 70   ISAUTOVACUUM (p
d4a20 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a  Bt->autoVacuum).
d4a30 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 53  #else.#define IS
d4a40 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e  AUTOVACUUM 0.#en
d4a50 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  dif.../*.** This
d4a60 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61   structure is pa
d4a70 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 6f  ssed around thro
d4a80 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e 69  ugh all the sani
d4a90 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 74  ty checking rout
d4aa0 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  ines.** in order
d4ab0 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f   to keep track o
d4ac0 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74  f some global st
d4ad0 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  ate information.
d4ae0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
d4af0 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49  ct IntegrityCk I
d4b00 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75  ntegrityCk;.stru
d4b10 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b  ct IntegrityCk {
d4b20 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
d4b30 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65  ;    /* The tree
d4b40 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f   being checked o
d4b50 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70  ut */.  Pager *p
d4b60 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65  Pager;    /* The
d4b70 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65   associated page
d4b80 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69  r.  Also accessi
d4b90 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67  ble by pBt->pPag
d4ba0 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67  er */.  int nPag
d4bb0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  e;        /* Num
d4bc0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
d4bd0 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
d4be0 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 20    int *anRef;   
d4bf0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
d4c00 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 65   times each page
d4c10 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a   is referenced *
d4c20 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 20  /.  int mxErr;  
d4c30 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 63        /* Stop ac
d4c40 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f 72  cumulating error
d4c50 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 63  s when this reac
d4c60 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 63 68  hes zero */.  ch
d4c70 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20  ar *zErrMsg;    
d4c80 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73  /* An error mess
d4c90 61 67 65 2e 20 20 4e 55 4c 4c 20 69 66 20 6e 6f  age.  NULL if no
d4ca0 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 20 2a 2f   errors seen. */
d4cb0 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20  .  int nErr;    
d4cc0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
d4cd0 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74  f messages writt
d4ce0 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f  en to zErrMsg so
d4cf0 20 66 61 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a   far */.};../*.*
d4d00 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20  * Read or write 
d4d10 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d  a two- and four-
d4d20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
d4d30 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a  integer values..
d4d40 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62  */.#define get2b
d4d50 79 74 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d  yte(x)   ((x)[0]
d4d60 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64  <<8 | (x)[1]).#d
d4d70 65 66 69 6e 65 20 70 75 74 32 62 79 74 65 28 70  efine put2byte(p
d4d80 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 76  ,v) ((p)[0] = (v
d4d90 29 3e 3e 38 2c 20 28 70 29 5b 31 5d 20 3d 20 28  )>>8, (p)[1] = (
d4da0 76 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 34  v)).#define get4
d4db0 62 79 74 65 20 73 71 6c 69 74 65 33 47 65 74 34  byte sqlite3Get4
d4dc0 62 79 74 65 0a 23 64 65 66 69 6e 65 20 70 75 74  byte.#define put
d4dd0 34 62 79 74 65 20 73 71 6c 69 74 65 33 50 75 74  4byte sqlite3Put
d4de0 34 62 79 74 65 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74  4byte../*.** Int
d4df0 65 72 6e 61 6c 20 72 6f 75 74 69 6e 65 73 20 74  ernal routines t
d4e00 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 61 63  hat should be ac
d4e10 63 65 73 73 65 64 20 62 79 20 74 68 65 20 62 74  cessed by the bt
d4e20 72 65 65 20 6c 61 79 65 72 20 6f 6e 6c 79 2e 0a  ree layer only..
d4e30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
d4e40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
d4e50 65 65 47 65 74 50 61 67 65 28 42 74 53 68 61 72  eeGetPage(BtShar
d4e60 65 64 2a 2c 20 50 67 6e 6f 2c 20 4d 65 6d 50 61  ed*, Pgno, MemPa
d4e70 67 65 2a 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ge**, int);.SQLI
d4e80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
d4e90 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
d4ea0 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
d4eb0 67 65 2c 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ge, MemPage *pPa
d4ec0 72 65 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rent);.SQLITE_PR
d4ed0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
d4ee0 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
d4ef0 50 74 72 28 4d 65 6d 50 61 67 65 2a 2c 20 75 38  Ptr(MemPage*, u8
d4f00 2a 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53  *, CellInfo*);.S
d4f10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
d4f20 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  id sqlite3BtreeP
d4f30 61 72 73 65 43 65 6c 6c 28 4d 65 6d 50 61 67 65  arseCell(MemPage
d4f40 2a 2c 20 69 6e 74 2c 20 43 65 6c 6c 49 6e 66 6f  *, int, CellInfo
d4f50 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  *);.#ifdef SQLIT
d4f60 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
d4f70 49 56 41 54 45 20 75 38 20 2a 73 71 6c 69 74 65  IVATE u8 *sqlite
d4f80 33 42 74 72 65 65 46 69 6e 64 43 65 6c 6c 28 4d  3BtreeFindCell(M
d4f90 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
d4fa0 6e 74 20 69 43 65 6c 6c 29 3b 0a 23 65 6e 64 69  nt iCell);.#endi
d4fb0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
d4fc0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
d4fd0 65 52 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43  eRestoreOrClearC
d4fe0 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74  ursorPosition(Bt
d4ff0 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a 53  Cursor *pCur);.S
d5000 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
d5010 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  id sqlite3BtreeG
d5020 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43  etTempCursor(BtC
d5030 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 43  ursor *pCur, BtC
d5040 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 29  ursor *pTempCur)
d5050 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
d5060 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
d5070 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72  eeReleaseTempCur
d5080 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  sor(BtCursor *pC
d5090 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ur);.SQLITE_PRIV
d50a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
d50b0 74 72 65 65 49 73 52 6f 6f 74 50 61 67 65 28 4d  treeIsRootPage(M
d50c0 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 0a  emPage *pPage);.
d50d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
d50e0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
d50f0 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43  MoveToParent(BtC
d5100 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a 0a 2f  ursor *pCur);../
d5110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
d5120 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68  nd of btreeInt.h
d5130 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
d5140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
d5160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
d5170 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
d5180 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62  we left off in b
d5190 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
d51a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  *************/.#
d51b0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
d51c0 53 41 46 45 20 26 26 20 21 64 65 66 69 6e 65 64  SAFE && !defined
d51d0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  (SQLITE_OMIT_SHA
d51e0 52 45 44 5f 43 41 43 48 45 29 0a 0a 0a 2f 2a 0a  RED_CACHE).../*.
d51f0 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78  ** Enter a mutex
d5200 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54   on the given BT
d5210 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  ree object..**.*
d5220 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20  * If the object 
d5230 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c  is not sharable,
d5240 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69   then no mutex i
d5250 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 0a  s ever required.
d5260 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  ** and this rout
d5270 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  ine is a no-op. 
d5280 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   The underlying 
d5290 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63  mutex is non-rec
d52a0 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77  ursive..** But w
d52b0 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e  e keep a referen
d52c0 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65  ce count in Btre
d52d0 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20  e.wantToLock so 
d52e0 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  the behavior.** 
d52f0 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  of this interfac
d5300 65 20 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a  e is recursive..
d5310 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64  **.** To avoid d
d5320 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70  eadlocks, multip
d5330 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f  le Btrees are lo
d5340 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  cked in the same
d5350 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c   order.** by all
d5360 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
d5370 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70  tions.  The p->p
d5380 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f  Next is a list o
d5390 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65  f other.** Btree
d53a0 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74  s belonging to t
d53b0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
d53c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74   connection as t
d53d0 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68  he p Btree.** wh
d53e0 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c  ich need to be l
d53f0 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20  ocked after p.  
d5400 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74  If we cannot get
d5410 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c   a lock on.** p,
d5420 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f   then first unlo
d5430 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74  ck all of the ot
d5440 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74  hers on p->pNext
d5450 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66  , then wait.** f
d5460 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62  or the lock to b
d5470 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20  ecome available 
d5480 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63  on p, then reloc
d5490 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20  k all of the.** 
d54a0 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 65  subsequent Btree
d54b0 73 20 74 68 61 74 20 64 65 73 69 72 65 20 61 20  s that desire a 
d54c0 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lock..*/.SQLITE_
d54d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
d54e0 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42  ite3BtreeEnter(B
d54f0 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65  tree *p){.  Btre
d5500 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a  e *pLater;..  /*
d5510 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69   Some basic sani
d5520 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74  ty checking on t
d5530 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c  he Btree.  The l
d5540 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20  ist of Btrees.  
d5550 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ** connected by 
d5560 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 20  pNext and pPrev 
d5570 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72  should be in sor
d5580 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a  ted order by.  *
d5590 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75  * Btree.pBt valu
d55a0 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20  e. All elements 
d55b0 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75  of the list shou
d55c0 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a  ld belong to.  *
d55d0 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65  * the same conne
d55e0 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72  ction. Only shar
d55f0 65 64 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e  ed Btrees are on
d5600 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20   the list. */.  
d5610 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74  assert( p->pNext
d5620 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d  ==0 || p->pNext-
d5630 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20  >pBt>p->pBt );. 
d5640 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65   assert( p->pPre
d5650 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76  v==0 || p->pPrev
d5660 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a  ->pBt<p->pBt );.
d5670 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65    assert( p->pNe
d5680 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78  xt==0 || p->pNex
d5690 74 2d 3e 70 53 71 6c 69 74 65 3d 3d 70 2d 3e 70  t->pSqlite==p->p
d56a0 53 71 6c 69 74 65 20 29 3b 0a 20 20 61 73 73 65  Sqlite );.  asse
d56b0 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20  rt( p->pPrev==0 
d56c0 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70 53 71  || p->pPrev->pSq
d56d0 6c 69 74 65 3d 3d 70 2d 3e 70 53 71 6c 69 74 65  lite==p->pSqlite
d56e0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
d56f0 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 28 70 2d  >sharable || (p-
d5700 3e 70 4e 65 78 74 3d 3d 30 20 26 26 20 70 2d 3e  >pNext==0 && p->
d5710 70 50 72 65 76 3d 3d 30 29 20 29 3b 0a 0a 20 20  pPrev==0) );..  
d5720 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6c 6f 63  /* Check for loc
d5730 6b 69 6e 67 20 63 6f 6e 73 69 73 74 65 6e 63 79  king consistency
d5740 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70   */.  assert( !p
d5750 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e 77  ->locked || p->w
d5760 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20  antToLock>0 );. 
d5770 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
d5780 61 62 6c 65 20 7c 7c 20 70 2d 3e 77 61 6e 74 54  able || p->wantT
d5790 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f  oLock==0 );..  /
d57a0 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65  * We should alre
d57b0 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20  ady hold a lock 
d57c0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
d57d0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
d57e0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
d57f0 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53  mutex_held(p->pS
d5800 71 6c 69 74 65 2d 3e 6d 75 74 65 78 29 20 29 3b  qlite->mutex) );
d5810 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72  ..  if( !p->shar
d5820 61 62 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a 20  able ) return;. 
d5830 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b   p->wantToLock++
d5840 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65  ;.  if( p->locke
d5850 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f  d ) return;..  /
d5860 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c  * In most cases,
d5870 20 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61 62   we should be ab
d5880 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 68  le to acquire th
d5890 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77  e lock we.  ** w
d58a0 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76 69  ant without havi
d58b0 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68  ng to go through
d58c0 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 20  t the ascending 
d58d0 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64  lock.  ** proced
d58e0 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ure that follows
d58f0 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72 65 20  .  Just be sure 
d5900 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20  not to block..  
d5910 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
d5920 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 70 42  _mutex_try(p->pB
d5930 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54  t->mutex)==SQLIT
d5940 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 6c  E_OK ){.    p->l
d5950 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 72  ocked = 1;.    r
d5960 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eturn;.  }..  /*
d5970 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f   To avoid deadlo
d5980 63 6b 2c 20 66 69 72 73 74 20 72 65 6c 65 61 73  ck, first releas
d5990 65 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 74 68  e all locks with
d59a0 20 61 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 42   a larger.  ** B
d59b0 74 53 68 61 72 65 64 20 61 64 64 72 65 73 73 2e  tShared address.
d59c0 20 20 54 68 65 6e 20 61 63 71 75 69 72 65 20 6f    Then acquire o
d59d0 75 72 20 6c 6f 63 6b 2e 20 20 54 68 65 6e 20 72  ur lock.  Then r
d59e0 65 61 63 71 75 69 72 65 0a 20 20 2a 2a 20 74 68  eacquire.  ** th
d59f0 65 20 6f 74 68 65 72 20 42 74 53 68 61 72 65 64  e other BtShared
d5a00 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 20 75   locks that we u
d5a10 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e 20 61  sed to hold in a
d5a20 73 63 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72  scending.  ** or
d5a30 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  der..  */.  for(
d5a40 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b  pLater=p->pNext;
d5a50 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d   pLater; pLater=
d5a60 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a  pLater->pNext){.
d5a70 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 61 74      assert( pLat
d5a80 65 72 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a  er->sharable );.
d5a90 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 61 74      assert( pLat
d5aa0 65 72 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20  er->pNext==0 || 
d5ab0 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 2d 3e 70  pLater->pNext->p
d5ac0 42 74 3e 70 4c 61 74 65 72 2d 3e 70 42 74 20 29  Bt>pLater->pBt )
d5ad0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  ;.    assert( !p
d5ae0 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c  Later->locked ||
d5af0 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c   pLater->wantToL
d5b00 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 69 66 28  ock>0 );.    if(
d5b10 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20   pLater->locked 
d5b20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
d5b30 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4c 61  _mutex_leave(pLa
d5b40 74 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  ter->pBt->mutex)
d5b50 3b 0a 20 20 20 20 20 20 70 4c 61 74 65 72 2d 3e  ;.      pLater->
d5b60 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20  locked = 0;.    
d5b70 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
d5b80 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70  mutex_enter(p->p
d5b90 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d  Bt->mutex);.  p-
d5ba0 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 66  >locked = 1;.  f
d5bb0 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65  or(pLater=p->pNe
d5bc0 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74  xt; pLater; pLat
d5bd0 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74  er=pLater->pNext
d5be0 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65  ){.    if( pLate
d5bf0 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b  r->wantToLock ){
d5c00 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
d5c10 75 74 65 78 5f 65 6e 74 65 72 28 70 4c 61 74 65  utex_enter(pLate
d5c20 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  r->pBt->mutex);.
d5c30 20 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f        pLater->lo
d5c40 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  cked = 1;.    }.
d5c50 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69    }.}../*.** Exi
d5c60 74 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20  t the recursive 
d5c70 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65  mutex on a Btree
d5c80 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
d5c90 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
d5ca0 42 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65  BtreeLeave(Btree
d5cb0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73   *p){.  if( p->s
d5cc0 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 61  harable ){.    a
d5cd0 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f  ssert( p->wantTo
d5ce0 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d  Lock>0 );.    p-
d5cf0 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20  >wantToLock--;. 
d5d00 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f     if( p->wantTo
d5d10 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Lock==0 ){.     
d5d20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b   assert( p->lock
d5d30 65 64 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ed );.      sqli
d5d40 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
d5d50 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  p->pBt->mutex);.
d5d60 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20        p->locked 
d5d70 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 0;.    }.  }.}
d5d80 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
d5d90 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
d5da0 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72  ue if the BtShar
d5db0 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  ed mutex is held
d5dc0 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2e 20 20   on the btree.  
d5dd0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
d5de0 69 6e 65 20 6d 61 6b 65 73 20 6e 6f 20 64 65 74  ine makes no det
d5df0 65 72 6d 69 6e 61 74 69 6f 6e 20 6f 6e 65 20 77  ermination one w
d5e00 68 79 20 6f 72 20 61 6e 6f 74 68 65 72 20 69 66  hy or another if
d5e10 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
d5e20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65   connection mute
d5e30 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a  x is held..**.**
d5e40 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
d5e50 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20   used only from 
d5e60 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 20  within assert() 
d5e70 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53  statements..*/.S
d5e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
d5e90 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  t sqlite3BtreeHo
d5ea0 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 20 2a  ldsMutex(Btree *
d5eb0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  p){.  return (p-
d5ec0 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 0a  >sharable==0 ||.
d5ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 70 2d               (p-
d5ee0 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 77 61  >locked && p->wa
d5ef0 6e 74 54 6f 4c 6f 63 6b 20 26 26 20 73 71 6c 69  ntToLock && sqli
d5f00 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
d5f10 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 29 3b  ->pBt->mutex)));
d5f20 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e  .}.#endif...#ifn
d5f30 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
d5f40 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45  INCRBLOB./*.** E
d5f50 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61  nter and leave a
d5f60 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65   mutex on a Btre
d5f70 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72  e given a cursor
d5f80 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a   owned by that.*
d5f90 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 20  * Btree.  These 
d5fa0 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65  entry points are
d5fb0 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65   used by increme
d5fc0 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e  ntal I/O and can
d5fd0 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69   be.** omitted i
d5fe0 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73  f that module is
d5ff0 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51   not used..*/.SQ
d6000 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
d6010 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
d6020 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73  terCursor(BtCurs
d6030 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c  or *pCur){.  sql
d6040 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
d6050 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a  Cur->pBtree);.}.
d6060 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
d6070 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
d6080 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75  LeaveCursor(BtCu
d6090 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73  rsor *pCur){.  s
d60a0 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
d60b0 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a  (pCur->pBtree);.
d60c0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
d60d0 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
d60e0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65   */.../*.** Ente
d60f0 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65  r the mutex on e
d6100 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63  very Btree assoc
d6110 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74  iated with a dat
d6120 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
d6130 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65  ion.  This is ne
d6140 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c  eded (for exampl
d6150 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73  e) prior to pars
d6160 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65  ing.** a stateme
d6170 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c  nt since we will
d6180 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61   be comparing ta
d6190 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e  ble and column n
d61a0 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20  ames.** against 
d61b0 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20  all schemas and 
d61c0 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  we do not want t
d61d0 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 69  hose schemas bei
d61e0 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20  ng.** reset out 
d61f0 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a  from under us..*
d6200 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20  *.** There is a 
d6210 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65  corresponding le
d6220 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72  ave-all procedur
d6230 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20  es..**.** Enter 
d6240 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61  the mutexes in a
d6250 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  ccending order b
d6260 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74  y BtShared point
d6270 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f  er address.** to
d6280 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69   avoid the possi
d6290 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f  bility of deadlo
d62a0 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65  ck when two thre
d62b0 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20  ads with.** two 
d62c0 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69  or more btrees i
d62d0 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72  n common both tr
d62e0 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68  y to lock all th
d62f0 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74  eir btrees.** at
d6300 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e   the same instan
d6310 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
d6320 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
d6330 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73  3BtreeEnterAll(s
d6340 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
d6350 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70  nt i;.  Btree *p
d6360 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73  , *pLater;.  ass
d6370 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
d6380 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
d6390 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  x) );.  for(i=0;
d63a0 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
d63b0 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44  {.    p = db->aD
d63c0 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
d63d0 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62  ( p && p->sharab
d63e0 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77  le ){.      p->w
d63f0 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  antToLock++;.   
d6400 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65     if( !p->locke
d6410 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  d ){.        ass
d6420 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  ert( p->wantToLo
d6430 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20  ck==1 );.       
d6440 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 72 65 76   while( p->pPrev
d6450 20 29 20 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b   ) p = p->pPrev;
d6460 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
d6470 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e  p->locked && p->
d6480 70 4e 65 78 74 20 29 20 70 20 3d 20 70 2d 3e 70  pNext ) p = p->p
d6490 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66 6f  Next;.        fo
d64a0 72 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e  r(pLater = p->pN
d64b0 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61  ext; pLater; pLa
d64c0 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78  ter=pLater->pNex
d64d0 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  t){.          if
d64e0 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64  ( pLater->locked
d64f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
d6500 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
d6510 61 76 65 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d  ave(pLater->pBt-
d6520 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20  >mutex);.       
d6530 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f 63       pLater->loc
d6540 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ked = 0;.       
d6550 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
d6560 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20         while( p 
d6570 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
d6580 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
d6590 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
d65a0 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f  .          p->lo
d65b0 63 6b 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20  cked++;.        
d65c0 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a    p = p->pNext;.
d65d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
d65e0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51  }.    }.  }.}.SQ
d65f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
d6600 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  d sqlite3BtreeLe
d6610 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a  aveAll(sqlite3 *
d6620 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  db){.  int i;.  
d6630 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 73 73 65  Btree *p;.  asse
d6640 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
d6650 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
d6660 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ) );.  for(i=0; 
d6670 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
d6680 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62  .    p = db->aDb
d6690 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
d66a0 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c   p && p->sharabl
d66b0 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  e ){.      asser
d66c0 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  t( p->wantToLock
d66d0 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 77  >0 );.      p->w
d66e0 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20  antToLock--;.   
d66f0 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f     if( p->wantTo
d6700 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Lock==0 ){.     
d6710 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f     assert( p->lo
d6720 63 6b 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  cked );.        
d6730 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
d6740 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  ave(p->pBt->mute
d6750 78 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c  x);.        p->l
d6760 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 20  ocked = 0;.     
d6770 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
d6780 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f  #ifndef NDEBUG./
d6790 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
d67a0 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   if the current 
d67b0 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65  thread holds the
d67c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
d67d0 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 6e  tion.** mutex an
d67e0 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 42  d all required B
d67f0 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 2e  tShared mutexes.
d6800 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
d6810 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69  ine is used insi
d6820 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
d6830 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ements only..*/.
d6840 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
d6850 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  nt sqlite3BtreeH
d6860 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 73  oldsAllMutexes(s
d6870 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
d6880 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c  nt i;.  if( !sql
d6890 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
d68a0 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20  db->mutex) ){.  
d68b0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
d68c0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
d68d0 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
d68e0 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70 20  Btree *p;.    p 
d68f0 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
d6900 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70  ;.    if( p && p
d6910 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20 20  ->sharable &&.  
d6920 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74 54         (p->wantT
d6930 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 6c  oLock==0 || !sql
d6940 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
d6950 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 20  p->pBt->mutex)) 
d6960 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
d6970 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
d6980 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
d6990 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a  f /* NDEBUG */..
d69a0 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 6c  /*.** Potentiall
d69b0 79 20 64 64 20 61 20 6e 65 77 20 42 74 72 65 65  y dd a new Btree
d69c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 42 74   pointer to a Bt
d69d0 72 65 65 4d 75 74 65 78 41 72 72 61 79 2e 0a 2a  reeMutexArray..*
d69e0 2a 20 52 65 61 6c 6c 79 20 6f 6e 6c 79 20 61 64  * Really only ad
d69f0 64 20 74 68 65 20 42 74 72 65 65 20 69 66 20 69  d the Btree if i
d6a00 74 20 63 61 6e 20 70 6f 73 73 69 62 6c 79 20 62  t can possibly b
d6a10 65 20 73 68 61 72 65 64 20 77 69 74 68 0a 2a 2a  e shared with.**
d6a20 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73   another databas
d6a30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a  e connection..**
d6a40 0a 2a 2a 20 54 68 65 20 42 74 72 65 65 73 20 61  .** The Btrees a
d6a50 72 65 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 65  re kept in sorte
d6a60 64 20 6f 72 64 65 72 20 62 79 20 70 42 74 72 65  d order by pBtre
d6a70 65 2d 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a 2a  e->pBt.  That.**
d6a80 20 77 61 79 20 77 68 65 6e 20 77 65 20 67 6f 20   way when we go 
d6a90 74 6f 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 65  to enter all the
d6aa0 20 6d 75 74 65 78 65 73 2c 20 77 65 20 63 61 6e   mutexes, we can
d6ab0 20 65 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 69   enter them.** i
d6ac0 6e 20 6f 72 64 65 72 20 77 69 74 68 6f 75 74 20  n order without 
d6ad0 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20  every having to 
d6ae0 62 61 63 6b 75 70 20 61 6e 64 20 72 65 74 72 79  backup and retry
d6af0 20 61 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20   and without.** 
d6b00 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 20 64  worrying about d
d6b10 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  eadlock..**.** T
d6b20 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 61  he number of sha
d6b30 72 65 64 20 62 74 72 65 65 73 20 77 69 6c 6c 20  red btrees will 
d6b40 61 6c 77 61 79 73 20 62 65 20 73 6d 61 6c 6c 20  always be small 
d6b50 28 75 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 29  (usually 0 or 1)
d6b60 0a 2a 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 74  .** so an insert
d6b70 69 6f 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 61  ion sort is an a
d6b80 64 65 71 75 61 74 65 20 61 6c 67 6f 72 69 74 68  dequate algorith
d6b90 6d 20 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  m here..*/.SQLIT
d6ba0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
d6bb0 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
d6bc0 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 72 65  ArrayInsert(Btre
d6bd0 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 72  eMutexArray *pAr
d6be0 72 61 79 2c 20 42 74 72 65 65 20 2a 70 42 74 72  ray, Btree *pBtr
d6bf0 65 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  ee){.  int i, j;
d6c00 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
d6c10 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 3d 3d  ;.  if( pBtree==
d6c20 30 20 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 61  0 || pBtree->sha
d6c30 72 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72  rable==0 ) retur
d6c40 6e 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  n;.#ifndef NDEBU
d6c50 47 0a 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 3d  G.  {.    for(i=
d6c60 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75  0; i<pArray->nMu
d6c70 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  tex; i++){.     
d6c80 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
d6c90 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72  >aBtree[i]!=pBtr
d6ca0 65 65 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ee );.    }.  }.
d6cb0 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28  #endif.  assert(
d6cc0 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3e   pArray->nMutex>
d6cd0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
d6ce0 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c 73  pArray->nMutex<s
d6cf0 69 7a 65 6f 66 28 70 41 72 72 61 79 2d 3e 61 42  izeof(pArray->aB
d6d00 74 72 65 65 29 2f 73 69 7a 65 6f 66 28 70 41 72  tree)/sizeof(pAr
d6d10 72 61 79 2d 3e 61 42 74 72 65 65 5b 30 5d 29 2d  ray->aBtree[0])-
d6d20 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 74  1 );.  pBt = pBt
d6d30 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 28  ree->pBt;.  for(
d6d40 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e  i=0; i<pArray->n
d6d50 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20  Mutex; i++){.   
d6d60 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
d6d70 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72  >aBtree[i]!=pBtr
d6d80 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 41  ee );.    if( pA
d6d90 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 2d  rray->aBtree[i]-
d6da0 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 20  >pBt>pBt ){.    
d6db0 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d 3e    for(j=pArray->
d6dc0 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d 2d  nMutex; j>i; j--
d6dd0 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 61  ){.        pArra
d6de0 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 70  y->aBtree[j] = p
d6df0 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 2d  Array->aBtree[j-
d6e00 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1];.      }.    
d6e10 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65    pArray->aBtree
d6e20 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 20  [i] = pBtree;.  
d6e30 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74      pArray->nMut
d6e40 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75  ex++;.      retu
d6e50 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
d6e60 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 70  pArray->aBtree[p
d6e70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 5d  Array->nMutex++]
d6e80 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f 2a   = pBtree;.}../*
d6e90 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75  .** Enter the mu
d6ea0 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72  tex of every btr
d6eb0 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e  ee in the array.
d6ec0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
d6ed0 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 74  s.** called at t
d6ee0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
d6ef0 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
d6f00 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20  ).  The mutexes 
d6f10 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 74  are.** exited at
d6f20 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
d6f30 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  same function..*
d6f40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
d6f50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
d6f60 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65  eeMutexArrayEnte
d6f70 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  r(BtreeMutexArra
d6f80 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e  y *pArray){.  in
d6f90 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
d6fa0 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78  i<pArray->nMutex
d6fb0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65  ; i++){.    Btre
d6fc0 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61  e *p = pArray->a
d6fd0 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a  Btree[i];.    /*
d6fe0 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69   Some basic sani
d6ff0 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20  ty checking */. 
d7000 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20     assert( i==0 
d7010 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  || pArray->aBtre
d7020 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70  e[i-1]->pBt<p->p
d7030 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Bt );.    assert
d7040 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20  ( !p->locked || 
d7050 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
d7060 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68  );..    /* We sh
d7070 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c  ould already hol
d7080 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  d a lock on the 
d7090 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
d70a0 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ion */.    asser
d70b0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
d70c0 5f 68 65 6c 64 28 70 2d 3e 70 53 71 6c 69 74 65  _held(p->pSqlite
d70d0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20  ->mutex) );..   
d70e0 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b   p->wantToLock++
d70f0 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f  ;.    if( !p->lo
d7100 63 6b 65 64 20 26 26 20 70 2d 3e 73 68 61 72 61  cked && p->shara
d7110 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ble ){.      sql
d7120 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
d7130 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
d7140 0a 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64  .      p->locked
d7150 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
d7160 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 61 76 65 20 74  }../*.** Leave t
d7170 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72  he mutex of ever
d7180 79 20 62 74 72 65 65 20 69 6e 20 74 68 65 20 67  y btree in the g
d7190 72 6f 75 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  roup..*/.SQLITE_
d71a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
d71b0 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
d71c0 72 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75  rayLeave(BtreeMu
d71d0 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79  texArray *pArray
d71e0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
d71f0 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d  r(i=0; i<pArray-
d7200 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20  >nMutex; i++){. 
d7210 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41     Btree *p = pA
d7220 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b  rray->aBtree[i];
d7230 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73  .    /* Some bas
d7240 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  ic sanity checki
d7250 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  ng */.    assert
d7260 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79  ( i==0 || pArray
d7270 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70  ->aBtree[i-1]->p
d7280 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20  Bt<p->pBt );.   
d7290 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b   assert( p->lock
d72a0 65 64 20 7c 7c 20 21 70 2d 3e 73 68 61 72 61 62  ed || !p->sharab
d72b0 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  le );.    assert
d72c0 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e  ( p->wantToLock>
d72d0 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20  0 );..    /* We 
d72e0 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68  should already h
d72f0 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68  old a lock on th
d7300 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
d7310 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73  ction */.    ass
d7320 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
d7330 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 71 6c 69  ex_held(p->pSqli
d7340 74 65 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  te->mutex) );.. 
d7350 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b     p->wantToLock
d7360 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77  --;.    if( p->w
d7370 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20  antToLock==0 && 
d7380 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20  p->locked ){.   
d7390 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
d73a0 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d  _leave(p->pBt->m
d73b0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 2d 3e  utex);.      p->
d73c0 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20  locked = 0;.    
d73d0 7d 0a 20 20 7d 0a 7d 0a 0a 0a 23 65 6e 64 69 66  }.  }.}...#endif
d73e0 20 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45    /* SQLITE_THRE
d73f0 41 44 53 41 46 45 20 26 26 20 21 53 51 4c 49 54  ADSAFE && !SQLIT
d7400 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
d7410 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  CHE */../*******
d7420 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62  ******* End of b
d7430 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
d7440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7460 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
d7470 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
d7480 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a  le btree.c *****
d7490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d74a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d74b0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
d74c0 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a  04 April 6.**.**
d74d0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
d74e0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
d74f0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
d7500 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
d7510 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
d7520 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
d7530 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
d7540 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
d7550 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
d7560 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
d7570 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
d7580 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
d7590 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
d75a0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
d75b0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
d75c0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
d75d0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
d75e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d75f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7620 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64  *********.** $Id
d7630 3a 20 62 74 72 65 65 2e 63 2c 76 20 31 2e 34 32  : btree.c,v 1.42
d7640 36 20 32 30 30 37 2f 30 39 2f 31 32 20 31 37 3a  6 2007/09/12 17:
d7650 30 31 3a 34 35 20 64 61 6e 69 65 6c 6b 31 39 37  01:45 danielk197
d7660 37 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68  7 Exp $.**.** Th
d7670 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
d7680 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64  ts a external (d
d7690 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62  isk-based) datab
d76a0 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73  ase using BTrees
d76b0 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61  ..** See the hea
d76c0 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22  der comment on "
d76d0 62 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20  btreeInt.h" for 
d76e0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
d76f0 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75  mation..** Inclu
d7700 64 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 69  ding a descripti
d7710 6f 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61  on of file forma
d7720 74 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65  t and an overvie
d7730 77 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  w of operation..
d7740 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65  */../*.** The he
d7750 61 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 74  ader string that
d7760 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20   appears at the 
d7770 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65  beginning of eve
d7780 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74  ry.** SQLite dat
d7790 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  abase..*/.static
d77a0 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67   const char zMag
d77b0 69 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c  icHeader[] = SQL
d77c0 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b  ITE_FILE_HEADER;
d77d0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73  ../*.** Set this
d77e0 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
d77f0 20 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20   to 1 to enable 
d7800 74 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68  tracing using th
d7810 65 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f  e TRACE.** macro
d7820 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
d7830 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65 33  TEST.int sqlite3
d7840 5f 62 74 72 65 65 5f 74 72 61 63 65 3d 30 3b 20  _btree_trace=0; 
d7850 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62   /* True to enab
d7860 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 65  le tracing */.#e
d7870 6e 64 69 66 0a 0a 0a 0a 23 69 66 6e 64 65 66 20  ndif....#ifndef 
d7880 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
d7890 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41  ED_CACHE./*.** A
d78a0 20 66 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74   flag to indicat
d78b0 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
d78c0 20 73 68 61 72 65 64 20 63 61 63 68 65 20 69 73   shared cache is
d78d0 20 65 6e 61 62 6c 65 64 2e 20 20 41 6c 73 6f 2c   enabled.  Also,
d78e0 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 42 74  .** a list of Bt
d78f0 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73 20 74  Shared objects t
d7900 68 61 74 20 61 72 65 20 65 6c 69 67 69 62 6c 65  hat are eligible
d7910 20 66 6f 72 20 70 61 72 74 69 63 69 70 61 74 69   for participati
d7920 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65 64 20  on.** in shared 
d7930 63 61 63 68 65 2e 20 20 54 68 65 20 76 61 72 69  cache.  The vari
d7940 61 62 6c 65 73 20 68 61 76 65 20 66 69 6c 65 20  ables have file 
d7950 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f 72  scope during nor
d7960 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20 62  mal builds,.** b
d7970 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72 6e  ut the test harn
d7980 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63 63  ess needs to acc
d7990 65 73 73 20 74 68 65 73 65 20 76 61 72 69 61 62  ess these variab
d79a0 6c 65 73 20 73 6f 20 77 65 20 6d 61 6b 65 20 74  les so we make t
d79b0 68 65 6d 0a 2a 2a 20 67 6c 6f 62 61 6c 20 66 6f  hem.** global fo
d79c0 72 20 74 65 73 74 20 62 75 69 6c 64 73 2e 0a 2a  r test builds..*
d79d0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
d79e0 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
d79f0 41 54 45 20 42 74 53 68 61 72 65 64 20 2a 73 71  ATE BtShared *sq
d7a00 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
d7a10 4c 69 73 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45  List = 0;.SQLITE
d7a20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
d7a30 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 45  ite3SharedCacheE
d7a40 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23 65 6c 73  nabled = 0;.#els
d7a50 65 0a 73 74 61 74 69 63 20 42 74 53 68 61 72 65  e.static BtShare
d7a60 64 20 2a 73 71 6c 69 74 65 33 53 68 61 72 65 64  d *sqlite3Shared
d7a70 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 73  CacheList = 0;.s
d7a80 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
d7a90 33 53 68 61 72 65 64 43 61 63 68 65 45 6e 61 62  3SharedCacheEnab
d7aa0 6c 65 64 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  led = 0;.#endif.
d7ab0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
d7ac0 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
d7ad0 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  HE */..#ifndef S
d7ae0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
d7af0 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e  D_CACHE./*.** En
d7b00 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20  able or disable 
d7b10 74 68 65 20 73 68 61 72 65 64 20 70 61 67 65 72  the shared pager
d7b20 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65 61 74   and schema feat
d7b30 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ures..**.** This
d7b40 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e 6f 20   routine has no 
d7b50 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73 74 69  effect on existi
d7b60 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ng database conn
d7b70 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20  ections..** The 
d7b80 73 68 61 72 65 64 20 63 61 63 68 65 20 73 65 74  shared cache set
d7b90 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f 6e 6c  ting effects onl
d7ba0 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73 20 74  y future calls t
d7bb0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  o.** sqlite3_ope
d7bc0 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f 70 65  n(), sqlite3_ope
d7bd0 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69 74 65  n16(), or sqlite
d7be0 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a  3_open_v2()..*/.
d7bf0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
d7c00 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68  qlite3_enable_sh
d7c10 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 20 65  ared_cache(int e
d7c20 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69 74 65  nable){.  sqlite
d7c30 33 53 68 61 72 65 64 43 61 63 68 65 45 6e 61 62  3SharedCacheEnab
d7c40 6c 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20  led = enable;.  
d7c50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d7c60 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  ;.}.#endif.../*.
d7c70 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  ** Forward decla
d7c80 72 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63  ration.*/.static
d7c90 20 69 6e 74 20 63 68 65 63 6b 52 65 61 64 4c 6f   int checkReadLo
d7ca0 63 6b 73 28 42 74 72 65 65 2a 2c 50 67 6e 6f 2c  cks(Btree*,Pgno,
d7cb0 42 74 43 75 72 73 6f 72 2a 29 3b 0a 0a 0a 23 69  BtCursor*);...#i
d7cc0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
d7cd0 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
d7ce0 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 75 6e 63  /*.  ** The func
d7cf0 74 69 6f 6e 73 20 71 75 65 72 79 54 61 62 6c 65  tions queryTable
d7d00 4c 6f 63 6b 28 29 2c 20 6c 6f 63 6b 54 61 62 6c  Lock(), lockTabl
d7d10 65 28 29 20 61 6e 64 20 75 6e 6c 6f 63 6b 41 6c  e() and unlockAl
d7d20 6c 54 61 62 6c 65 73 28 29 0a 20 20 2a 2a 20 6d  lTables().  ** m
d7d30 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69 65  anipulate entrie
d7d40 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72 65  s in the BtShare
d7d50 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c  d.pLock linked l
d7d60 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ist used to stor
d7d70 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61  e.  ** shared-ca
d7d80 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c 20  che table level 
d7d90 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c 69  locks. If the li
d7da0 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65  brary is compile
d7db0 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20  d with the.  ** 
d7dc0 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 61  shared-cache fea
d7dd0 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74  ture disabled, t
d7de0 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 6c  hen there is onl
d7df0 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 0a  y ever one user.
d7e00 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 53    ** of each BtS
d7e10 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20  hared structure 
d7e20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 6b  and so this lock
d7e30 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  ing is not neces
d7e40 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 64  sary. .  ** So d
d7e50 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 72  efine the lock r
d7e60 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 73  elated functions
d7e70 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f   as no-ops..  */
d7e80 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72 79  .  #define query
d7e90 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29  TableLock(a,b,c)
d7ea0 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65   SQLITE_OK.  #de
d7eb0 66 69 6e 65 20 6c 6f 63 6b 54 61 62 6c 65 28 61  fine lockTable(a
d7ec0 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a  ,b,c) SQLITE_OK.
d7ed0 20 20 23 64 65 66 69 6e 65 20 75 6e 6c 6f 63 6b    #define unlock
d7ee0 41 6c 6c 54 61 62 6c 65 73 28 61 29 0a 23 65 6e  AllTables(a).#en
d7ef0 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
d7f00 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
d7f10 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 51 75 65 72  CACHE./*.** Quer
d7f20 79 20 74 6f 20 73 65 65 20 69 66 20 62 74 72 65  y to see if btre
d7f30 65 20 68 61 6e 64 6c 65 20 70 20 6d 61 79 20 6f  e handle p may o
d7f40 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20  btain a lock of 
d7f50 74 79 70 65 20 65 4c 6f 63 6b 20 0a 2a 2a 20 28  type eLock .** (
d7f60 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49  READ_LOCK or WRI
d7f70 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 74 68 65 20  TE_LOCK) on the 
d7f80 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d  table with root-
d7f90 70 61 67 65 20 69 54 61 62 2e 20 52 65 74 75 72  page iTab. Retur
d7fa0 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  n.** SQLITE_OK i
d7fb0 66 20 74 68 65 20 6c 6f 63 6b 20 6d 61 79 20 62  f the lock may b
d7fc0 65 20 6f 62 74 61 69 6e 65 64 20 28 62 79 20 63  e obtained (by c
d7fd0 61 6c 6c 69 6e 67 20 6c 6f 63 6b 54 61 62 6c 65  alling lockTable
d7fe0 28 29 29 2c 20 6f 72 0a 2a 2a 20 53 51 4c 49 54  ()), or.** SQLIT
d7ff0 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e 6f 74 2e  E_LOCKED if not.
d8000 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 71  .*/.static int q
d8010 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 42 74  ueryTableLock(Bt
d8020 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61  ree *p, Pgno iTa
d8030 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20  b, u8 eLock){.  
d8040 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
d8050 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b  p->pBt;.  BtLock
d8060 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65   *pIter;..  asse
d8070 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
d8080 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
d8090 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 69 73  .  .  /* This is
d80a0 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20   a no-op if the 
d80b0 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20  shared-cache is 
d80c0 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20  not enabled */. 
d80d0 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
d80e0 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
d80f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
d8100 20 20 2f 2a 20 54 68 69 73 20 28 61 6c 6f 6e 67    /* This (along
d8110 20 77 69 74 68 20 6c 6f 63 6b 54 61 62 6c 65 28   with lockTable(
d8120 29 29 20 69 73 20 77 68 65 72 65 20 74 68 65 20  )) is where the 
d8130 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
d8140 66 6c 61 67 20 69 73 0a 20 20 2a 2a 20 64 65 61  flag is.  ** dea
d8150 6c 74 20 77 69 74 68 2e 20 49 66 20 74 68 65 20  lt with. If the 
d8160 63 61 6c 6c 65 72 20 69 73 20 71 75 65 72 79 69  caller is queryi
d8170 6e 67 20 66 6f 72 20 61 20 72 65 61 64 2d 6c 6f  ng for a read-lo
d8180 63 6b 20 61 6e 64 20 74 68 65 20 66 6c 61 67 20  ck and the flag 
d8190 69 73 0a 20 20 2a 2a 20 73 65 74 2c 20 69 74 20  is.  ** set, it 
d81a0 69 73 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c  is unconditional
d81b0 6c 79 20 67 72 61 6e 74 65 64 20 2d 20 65 76 65  ly granted - eve
d81c0 6e 20 69 66 20 74 68 65 72 65 20 61 72 65 20 77  n if there are w
d81d0 72 69 74 65 2d 6c 6f 63 6b 73 0a 20 20 2a 2a 20  rite-locks.  ** 
d81e0 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49 66  on the table. If
d81f0 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73   a write-lock is
d8200 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 20   requested, the 
d8210 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
d8220 66 6c 61 67 0a 20 20 2a 2a 20 69 73 20 6e 6f 74  flag.  ** is not
d8230 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 20 20 2a   considered..  *
d8240 2a 0a 20 20 2a 2a 20 49 6e 20 66 75 6e 63 74 69  *.  ** In functi
d8250 6f 6e 20 6c 6f 63 6b 54 61 62 6c 65 28 29 2c 20  on lockTable(), 
d8260 69 66 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69  if a read-lock i
d8270 73 20 64 65 6d 61 6e 64 65 64 20 61 6e 64 20 74  s demanded and t
d8280 68 65 20 0a 20 20 2a 2a 20 52 65 61 64 55 6e 63  he .  ** ReadUnc
d8290 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73  ommitted flag is
d82a0 20 73 65 74 2c 20 6e 6f 20 65 6e 74 72 79 20 69   set, no entry i
d82b0 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c  s added to the l
d82c0 6f 63 6b 73 20 6c 69 73 74 20 0a 20 20 2a 2a 20  ocks list .  ** 
d82d0 28 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 29  (BtShared.pLock)
d82e0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 73  ..  **.  ** To s
d82f0 75 6d 6d 61 72 69 7a 65 3a 20 49 66 20 74 68 65  ummarize: If the
d8300 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64   ReadUncommitted
d8310 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
d8320 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 73 20  en read cursors 
d8330 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 63 72 65 61  do.  ** not crea
d8340 74 65 20 6f 72 20 72 65 73 70 65 63 74 20 74 61  te or respect ta
d8350 62 6c 65 20 6c 6f 63 6b 73 2e 20 54 68 65 20 6c  ble locks. The l
d8360 6f 63 6b 69 6e 67 20 70 72 6f 63 65 64 75 72 65  ocking procedure
d8370 20 66 6f 72 20 61 20 0a 20 20 2a 2a 20 77 72 69   for a .  ** wri
d8380 74 65 2d 63 75 72 73 6f 72 20 64 6f 65 73 20 6e  te-cursor does n
d8390 6f 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a  ot change..  */.
d83a0 20 20 69 66 28 20 0a 20 20 20 20 21 70 2d 3e 70    if( .    !p->p
d83b0 53 71 6c 69 74 65 20 7c 7c 20 0a 20 20 20 20 30  Sqlite || .    0
d83c0 3d 3d 28 70 2d 3e 70 53 71 6c 69 74 65 2d 3e 66  ==(p->pSqlite->f
d83d0 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64  lags&SQLITE_Read
d83e0 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20  Uncommitted) || 
d83f0 0a 20 20 20 20 65 4c 6f 63 6b 3d 3d 57 52 49 54  .    eLock==WRIT
d8400 45 5f 4c 4f 43 4b 20 7c 7c 0a 20 20 20 20 69 54  E_LOCK ||.    iT
d8410 61 62 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a  ab==MASTER_ROOT.
d8420 20 20 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 74    ){.    for(pIt
d8430 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70  er=pBt->pLock; p
d8440 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65  Iter; pIter=pIte
d8450 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  r->pNext){.     
d8460 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72   if( pIter->pBtr
d8470 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e  ee!=p && pIter->
d8480 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20  iTable==iTab && 
d8490 0a 20 20 20 20 20 20 20 20 20 20 28 70 49 74 65  .          (pIte
d84a0 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20  r->eLock!=eLock 
d84b0 7c 7c 20 65 4c 6f 63 6b 21 3d 52 45 41 44 5f 4c  || eLock!=READ_L
d84c0 4f 43 4b 29 20 29 7b 0a 20 20 20 20 20 20 20 20  OCK) ){.        
d84d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
d84e0 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20  CKED;.      }.  
d84f0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
d8500 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
d8510 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
d8520 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
d8530 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
d8540 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
d8550 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64  _CACHE./*.** Add
d8560 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74   a lock on the t
d8570 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70  able with root-p
d8580 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 68  age iTable to th
d8590 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 75  e shared-btree u
d85a0 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 20  sed.** by Btree 
d85b0 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d 65  handle p. Parame
d85c0 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 62  ter eLock must b
d85d0 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c 4f  e either READ_LO
d85e0 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 5f  CK or .** WRITE_
d85f0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  LOCK..**.** SQLI
d8600 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
d8610 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73  d if the lock is
d8620 20 61 64 64 65 64 20 73 75 63 63 65 73 73 66 75   added successfu
d8630 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 42 55 53 59  lly. SQLITE_BUSY
d8640 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e   and.** SQLITE_N
d8650 4f 4d 45 4d 20 6d 61 79 20 61 6c 73 6f 20 62 65  OMEM may also be
d8660 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
d8670 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 61 62  atic int lockTab
d8680 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e  le(Btree *p, Pgn
d8690 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 4c 6f  o iTable, u8 eLo
d86a0 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ck){.  BtShared 
d86b0 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
d86c0 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d   BtLock *pLock =
d86d0 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49   0;.  BtLock *pI
d86e0 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ter;..  assert( 
d86f0 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
d8700 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a 20 20  sMutex(p) );..  
d8710 2f 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d  /* This is a no-
d8720 6f 70 20 69 66 20 74 68 65 20 73 68 61 72 65 64  op if the shared
d8730 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20 65 6e  -cache is not en
d8740 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 20 21  abled */.  if( !
d8750 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20  p->sharable ){. 
d8760 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d8770 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  _OK;.  }..  asse
d8780 72 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71  rt( SQLITE_OK==q
d8790 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ueryTableLock(p,
d87a0 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63 6b 29 20   iTable, eLock) 
d87b0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
d87c0 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64  read-uncommitted
d87d0 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64   flag is set and
d87e0 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20   a read-lock is 
d87f0 72 65 71 75 65 73 74 65 64 2c 0a 20 20 2a 2a 20  requested,.  ** 
d8800 72 65 74 75 72 6e 20 65 61 72 6c 79 20 77 69 74  return early wit
d8810 68 6f 75 74 20 61 64 64 69 6e 67 20 61 6e 20 65  hout adding an e
d8820 6e 74 72 79 20 74 6f 20 74 68 65 20 42 74 53 68  ntry to the BtSh
d8830 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 2e  ared.pLock list.
d8840 20 53 65 65 0a 20 20 2a 2a 20 63 6f 6d 6d 65 6e   See.  ** commen
d8850 74 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 71 75  t in function qu
d8860 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66  eryTableLock() f
d8870 6f 72 20 6d 6f 72 65 20 69 6e 66 6f 20 6f 6e 20  or more info on 
d8880 68 61 6e 64 6c 69 6e 67 20 0a 20 20 2a 2a 20 74  handling .  ** t
d8890 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  he ReadUncommitt
d88a0 65 64 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20  ed flag..  */.  
d88b0 69 66 28 20 0a 20 20 20 20 28 70 2d 3e 70 53 71  if( .    (p->pSq
d88c0 6c 69 74 65 29 20 26 26 20 0a 20 20 20 20 28 70  lite) && .    (p
d88d0 2d 3e 70 53 71 6c 69 74 65 2d 3e 66 6c 61 67 73  ->pSqlite->flags
d88e0 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f  &SQLITE_ReadUnco
d88f0 6d 6d 69 74 74 65 64 29 20 26 26 20 0a 20 20 20  mmitted) && .   
d8900 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f   (eLock==READ_LO
d8910 43 4b 29 20 26 26 0a 20 20 20 20 69 54 61 62 6c  CK) &&.    iTabl
d8920 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a 20  e!=MASTER_ROOT. 
d8930 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
d8940 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
d8950 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68   /* First search
d8960 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 6e   the list for an
d8970 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 6f   existing lock o
d8980 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f  n this table. */
d8990 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74  .  for(pIter=pBt
d89a0 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20  ->pLock; pIter; 
d89b0 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65  pIter=pIter->pNe
d89c0 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74  xt){.    if( pIt
d89d0 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62  er->iTable==iTab
d89e0 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 74  le && pIter->pBt
d89f0 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20  ree==p ){.      
d8a00 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a 20  pLock = pIter;. 
d8a10 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
d8a20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
d8a30 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 20  he above search 
d8a40 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 42  did not find a B
d8a50 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 73  tLock struct ass
d8a60 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 70  ociating Btree p
d8a70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c 65  .  ** with table
d8a80 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74   iTable, allocat
d8a90 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69  e one and link i
d8aa0 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e  t into the list.
d8ab0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c 6f  .  */.  if( !pLo
d8ac0 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20  ck ){.    pLock 
d8ad0 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69  = (BtLock *)sqli
d8ae0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
d8af0 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a 20  zeof(BtLock));. 
d8b00 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b     if( !pLock ){
d8b10 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
d8b20 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
d8b30 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 61  }.    pLock->iTa
d8b40 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20  ble = iTable;.  
d8b50 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20    pLock->pBtree 
d8b60 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  = p;.    pLock->
d8b70 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f  pNext = pBt->pLo
d8b80 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c 6f  ck;.    pBt->pLo
d8b90 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a  ck = pLock;.  }.
d8ba0 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 74  .  /* Set the Bt
d8bb0 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 61  Lock.eLock varia
d8bc0 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 6d  ble to the maxim
d8bd0 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  um of the curren
d8be0 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20  t lock.  ** and 
d8bf0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f  the requested lo
d8c00 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69  ck. This means i
d8c10 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77  f a write-lock w
d8c20 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 0a  as already held.
d8c30 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 2d    ** and a read-
d8c40 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c 20  lock requested, 
d8c50 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 65  we don't incorre
d8c60 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 74  ctly downgrade t
d8c70 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  he lock..  */.  
d8c80 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f  assert( WRITE_LO
d8c90 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a  CK>READ_LOCK );.
d8ca0 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63    if( eLock>pLoc
d8cb0 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  k->eLock ){.    
d8cc0 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65  pLock->eLock = e
d8cd0 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Lock;.  }..  ret
d8ce0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
d8cf0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49  .#endif /* !SQLI
d8d00 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
d8d10 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ACHE */..#ifndef
d8d20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
d8d30 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20  RED_CACHE./*.** 
d8d40 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20  Release all the 
d8d50 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f 63  table locks (loc
d8d60 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 20  ks obtained via 
d8d70 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 6c 6f 63  calls to the loc
d8d80 6b 54 61 62 6c 65 28 29 0a 2a 2a 20 70 72 6f 63  kTable().** proc
d8d90 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 42  edure) held by B
d8da0 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a 2a  tree handle p..*
d8db0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e  /.static void un
d8dc0 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 42 74  lockAllTables(Bt
d8dd0 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 4c 6f 63  ree *p){.  BtLoc
d8de0 6b 20 2a 2a 70 70 49 74 65 72 20 3d 20 26 70 2d  k **ppIter = &p-
d8df0 3e 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20  >pBt->pLock;..  
d8e00 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
d8e10 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
d8e20 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
d8e30 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 3d  ->sharable || 0=
d8e40 3d 2a 70 70 49 74 65 72 20 29 3b 0a 0a 20 20 77  =*ppIter );..  w
d8e50 68 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29 7b  hile( *ppIter ){
d8e60 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f  .    BtLock *pLo
d8e70 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20  ck = *ppIter;.  
d8e80 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74    if( pLock->pBt
d8e90 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20  ree==p ){.      
d8ea0 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d  *ppIter = pLock-
d8eb0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71  >pNext;.      sq
d8ec0 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b  lite3_free(pLock
d8ed0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
d8ee0 20 20 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c      ppIter = &pL
d8ef0 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ock->pNext;.    
d8f00 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  }.  }.}.#endif /
d8f10 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  * SQLITE_OMIT_SH
d8f20 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 73  ARED_CACHE */..s
d8f30 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61  tatic void relea
d8f40 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  sePage(MemPage *
d8f50 70 50 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72 77  pPage);  /* Forw
d8f60 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f  ard reference */
d8f70 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 69 66 79 20 74  ../*.** Verify t
d8f80 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68  hat the cursor h
d8f90 6f 6c 64 73 20 61 20 6d 75 74 65 78 20 6f 6e 20  olds a mutex on 
d8fa0 74 68 65 20 42 74 53 68 61 72 65 64 0a 2a 2f 0a  the BtShared.*/.
d8fb0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73  #ifndef NDEBUG.s
d8fc0 74 61 74 69 63 20 69 6e 74 20 63 75 72 73 6f 72  tatic int cursor
d8fd0 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 75 72  HoldsMutex(BtCur
d8fe0 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  sor *p){.  retur
d8ff0 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  n sqlite3_mutex_
d9000 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74  held(p->pBt->mut
d9010 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  ex);.}.#endif...
d9020 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
d9030 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a  MIT_INCRBLOB./*.
d9040 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68  ** Invalidate th
d9050 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
d9060 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 63  list cache for c
d9070 75 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20 61  ursor pCur, if a
d9080 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ny..*/.static vo
d9090 69 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65  id invalidateOve
d90a0 72 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75 72  rflowCache(BtCur
d90b0 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73  sor *pCur){.  as
d90c0 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
d90d0 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
d90e0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
d90f0 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b  Cur->aOverflow);
d9100 0a 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  .  pCur->aOverfl
d9110 6f 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ow = 0;.}../*.**
d9120 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20   Invalidate the 
d9130 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69  overflow page-li
d9140 73 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c 6c  st cache for all
d9150 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a   cursors opened.
d9160 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64  ** on the shared
d9170 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65   btree structure
d9180 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   pBt..*/.static 
d9190 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 41  void invalidateA
d91a0 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  llOverflowCache(
d91b0 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
d91c0 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20    BtCursor *p;. 
d91d0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
d91e0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
d91f0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
d9200 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b  (p=pBt->pCursor;
d9210 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
d9220 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f  .    invalidateO
d9230 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29 3b  verflowCache(p);
d9240 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 23  .  }.}.#else.  #
d9250 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74  define invalidat
d9260 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78  eOverflowCache(x
d9270 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61  ).  #define inva
d9280 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f  lidateAllOverflo
d9290 77 43 61 63 68 65 28 78 29 0a 23 65 6e 64 69 66  wCache(x).#endif
d92a0 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65  ../*.** Save the
d92b0 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20   current cursor 
d92c0 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20  position in the 
d92d0 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 73  variables BtCurs
d92e0 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20  or.nKey .** and 
d92f0 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54  BtCursor.pKey. T
d9300 68 65 20 63 75 72 73 6f 72 27 73 20 73 74 61 74  he cursor's stat
d9310 65 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53  e is set to CURS
d9320 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 2e 0a  OR_REQUIRESEEK..
d9330 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61  */.static int sa
d9340 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  veCursorPosition
d9350 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
d9360 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  {.  int rc;..  a
d9370 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41  ssert( CURSOR_VA
d9380 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74  LID==pCur->eStat
d9390 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 30  e );.  assert( 0
d93a0 3d 3d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a  ==pCur->pKey );.
d93b0 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
d93c0 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
d93d0 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69   );..  rc = sqli
d93e0 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
d93f0 70 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e 4b 65  pCur, &pCur->nKe
d9400 79 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69  y);..  /* If thi
d9410 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74  s is an intKey t
d9420 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61  able, then the a
d9430 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72  bove call to Btr
d9440 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a  eeKeySize().  **
d9450 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65   stores the inte
d9460 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d  ger key in pCur-
d9470 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63  >nKey. In this c
d9480 61 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69  ase this value i
d9490 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20  s.  ** all that 
d94a0 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68  is required. Oth
d94b0 65 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20  erwise, if pCur 
d94c0 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61  is not open on a
d94d0 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61  n intKey.  ** ta
d94e0 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63  ble, then malloc
d94f0 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73   space for and s
d9500 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e  tore the pCur->n
d9510 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79  Key bytes of key
d9520 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a   .  ** data..  *
d9530 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
d9540 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 70 43 75 72  TE_OK && 0==pCur
d9550 2d 3e 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 29  ->pPage->intKey)
d9560 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4b 65 79  {.    void *pKey
d9570 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
d9580 63 28 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20  c(pCur->nKey);. 
d9590 20 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20     if( pKey ){. 
d95a0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d95b0 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20  3BtreeKey(pCur, 
d95c0 30 2c 20 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70  0, pCur->nKey, p
d95d0 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Key);.      if( 
d95e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
d95f0 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70  .        pCur->p
d9600 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20  Key = pKey;.    
d9610 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
d9620 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b   sqlite3_free(pK
d9630 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ey);.      }.   
d9640 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
d9650 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
d9660 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73  .    }.  }.  ass
d9670 65 72 74 28 20 21 70 43 75 72 2d 3e 70 50 61 67  ert( !pCur->pPag
d9680 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 70 43  e->intKey || !pC
d9690 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 20 69  ur->pKey );..  i
d96a0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
d96b0 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50   ){.    releaseP
d96c0 61 67 65 28 70 43 75 72 2d 3e 70 50 61 67 65 29  age(pCur->pPage)
d96d0 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70 50 61 67  ;.    pCur->pPag
d96e0 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d  e = 0;.    pCur-
d96f0 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
d9700 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20 20  _REQUIRESEEK;.  
d9710 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 4f  }..  invalidateO
d9720 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75  verflowCache(pCu
d9730 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
d9740 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74  .}../*.** Save t
d9750 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20  he positions of 
d9760 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 63 65  all cursors exce
d9770 70 74 20 70 45 78 63 65 70 74 20 6f 70 65 6e 20  pt pExcept open 
d9780 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a  on the table .**
d9790 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20   with root-page 
d97a0 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79 2c 20  iRoot. Usually, 
d97b0 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6a  this is called j
d97c0 75 73 74 20 62 65 66 6f 72 65 20 63 75 72 73 6f  ust before curso
d97d0 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 69 73 20  r.** pExcept is 
d97e0 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 20 74  used to modify t
d97f0 68 65 20 74 61 62 6c 65 20 28 42 74 72 65 65 44  he table (BtreeD
d9800 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65 65  elete() or Btree
d9810 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a 73 74  Insert())..*/.st
d9820 61 74 69 63 20 69 6e 74 20 73 61 76 65 41 6c 6c  atic int saveAll
d9830 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64  Cursors(BtShared
d9840 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 6f 6f   *pBt, Pgno iRoo
d9850 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 45 78  t, BtCursor *pEx
d9860 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72 73 6f  cept){.  BtCurso
d9870 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  r *p;.  assert( 
d9880 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
d9890 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
d98a0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 63  ;.  assert( pExc
d98b0 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 65 70  ept==0 || pExcep
d98c0 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 20  t->pBt==pBt );. 
d98d0 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72   for(p=pBt->pCur
d98e0 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  sor; p; p=p->pNe
d98f0 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 21 3d  xt){.    if( p!=
d9900 70 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d 69  pExcept && (0==i
d9910 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f 52  Root || p->pgnoR
d9920 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26 20 0a  oot==iRoot) && .
d9930 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74          p->eStat
d9940 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
d9950 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20  ){.      int rc 
d9960 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69  = saveCursorPosi
d9970 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20 69  tion(p);.      i
d9980 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63  f( SQLITE_OK!=rc
d9990 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
d99a0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
d99b0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
d99c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d99d0 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20  /*.** Clear the 
d99e0 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70  current cursor p
d99f0 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  osition..*/.stat
d9a00 69 63 20 76 6f 69 64 20 63 6c 65 61 72 43 75 72  ic void clearCur
d9a10 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75  sorPosition(BtCu
d9a20 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61  rsor *pCur){.  a
d9a30 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
d9a40 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
d9a50 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
d9a60 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70  pCur->pKey);.  p
d9a70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20  Cur->pKey = 0;. 
d9a80 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
d9a90 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
d9aa0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65  }../*.** Restore
d9ab0 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
d9ac0 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77  he position it w
d9ad0 61 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f  as in (or as clo
d9ae0 73 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c  se to as possibl
d9af0 65 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43  e).** when saveC
d9b00 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20  ursorPosition() 
d9b10 77 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65  was called. Note
d9b20 20 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20   that this call 
d9b30 64 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20  deletes the .** 
d9b40 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69  saved position i
d9b50 6e 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61  nfo stored by sa
d9b60 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  veCursorPosition
d9b70 28 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e  (), so there can
d9b80 20 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f   be.** at most o
d9b90 6e 65 20 65 66 66 65 63 74 69 76 65 20 72 65 73  ne effective res
d9ba0 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73 6f  toreOrClearCurso
d9bb0 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6c 6c  rPosition() call
d9bc0 20 61 66 74 65 72 20 65 61 63 68 20 0a 2a 2a 20   after each .** 
d9bd0 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69  saveCursorPositi
d9be0 6f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  on()..**.** If t
d9bf0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
d9c00 6e 74 20 61 72 67 75 6d 65 6e 74 20 2d 20 64 6f  nt argument - do
d9c10 53 65 65 6b 20 2d 20 69 73 20 66 61 6c 73 65 2c  Seek - is false,
d9c20 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f 66   then instead of
d9c30 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20 74   .** returning t
d9c40 68 65 20 63 75 72 73 6f 72 20 74 6f 20 69 74 27  he cursor to it'
d9c50 73 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e  s saved position
d9c60 2c 20 61 6e 79 20 73 61 76 65 64 20 70 6f 73 69  , any saved posi
d9c70 74 69 6f 6e 20 69 73 20 64 65 6c 65 74 65 64 0a  tion is deleted.
d9c80 2a 2a 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f  ** and the curso
d9c90 72 20 73 74 61 74 65 20 73 65 74 20 74 6f 20 43  r state set to C
d9ca0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 2e 0a 2a  URSOR_INVALID..*
d9cb0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
d9cc0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
d9cd0 65 52 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43  eRestoreOrClearC
d9ce0 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74  ursorPosition(Bt
d9cf0 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
d9d00 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
d9d10 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
d9d20 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
d9d30 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
d9d40 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55  ate>=CURSOR_REQU
d9d50 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28  IRESEEK );.  if(
d9d60 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
d9d70 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20  URSOR_FAULT ){. 
d9d80 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e     return pCur->
d9d90 73 6b 69 70 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  skip;.  }.#ifnde
d9da0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
d9db0 43 52 42 4c 4f 42 0a 20 20 69 66 28 20 70 43 75  CRBLOB.  if( pCu
d9dc0 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  r->isIncrblobHan
d9dd0 64 6c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  dle ){.    retur
d9de0 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a  n SQLITE_ABORT;.
d9df0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 43 75    }.#endif.  pCu
d9e00 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
d9e10 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 72 63  OR_INVALID;.  rc
d9e20 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
d9e30 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 43 75 72  oveto(pCur, pCur
d9e40 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e 6e 4b  ->pKey, pCur->nK
d9e50 65 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e 73 6b  ey, 0, &pCur->sk
d9e60 69 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ip);.  if( rc==S
d9e70 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
d9e80 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75  sqlite3_free(pCu
d9e90 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 70 43  r->pKey);.    pC
d9ea0 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20  ur->pKey = 0;.  
d9eb0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
d9ec0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
d9ed0 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53  ALID || pCur->eS
d9ee0 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56  tate==CURSOR_INV
d9ef0 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20 20 72 65  ALID );.  }.  re
d9f00 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 64 65 66  turn rc;.}..#def
d9f10 69 6e 65 20 72 65 73 74 6f 72 65 4f 72 43 6c 65  ine restoreOrCle
d9f20 61 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  arCursorPosition
d9f30 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61  (p) \.  (p->eSta
d9f40 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49  te>=CURSOR_REQUI
d9f50 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20  RESEEK ? \.     
d9f60 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
d9f70 52 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75  RestoreOrClearCu
d9f80 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20  rsorPosition(p) 
d9f90 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 51 4c  : \.         SQL
d9fa0 49 54 45 5f 4f 4b 29 0a 0a 23 69 66 6e 64 65 66  ITE_OK)..#ifndef
d9fb0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
d9fc0 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69  OVACUUM./*.** Gi
d9fd0 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65  ven a page numbe
d9fe0 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 64  r of a regular d
d9ff0 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 65  atabase page, re
da000 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a  turn the page.**
da010 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
da020 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
da030 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
da040 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  he entry for the
da050 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 6e  .** input page n
da060 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  umber..*/.static
da070 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 65   Pgno ptrmapPage
da080 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  no(BtShared *pBt
da090 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20  , Pgno pgno){.  
da0a0 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 70  int nPagesPerMap
da0b0 50 61 67 65 2c 20 69 50 74 72 4d 61 70 2c 20 72  Page, iPtrMap, r
da0c0 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  et;.  assert( sq
da0d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
da0e0 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
da0f0 20 20 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61    nPagesPerMapPa
da100 67 65 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c  ge = (pBt->usabl
da110 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20 20 69 50  eSize/5)+1;.  iP
da120 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f 2d 32 29  trMap = (pgno-2)
da130 2f 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67  /nPagesPerMapPag
da140 65 3b 0a 20 20 72 65 74 20 3d 20 28 69 50 74 72  e;.  ret = (iPtr
da150 4d 61 70 2a 6e 50 61 67 65 73 50 65 72 4d 61 70  Map*nPagesPerMap
da160 50 61 67 65 29 20 2b 20 32 3b 20 0a 20 20 69 66  Page) + 2; .  if
da170 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e 47 5f 42  ( ret==PENDING_B
da180 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
da190 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a  .    ret++;.  }.
da1a0 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a    return ret;.}.
da1b0 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20  ./*.** Write an 
da1c0 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70  entry into the p
da1d0 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a  ointer map..**.*
da1e0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75  * This routine u
da1f0 70 64 61 74 65 73 20 74 68 65 20 70 6f 69 6e 74  pdates the point
da200 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72  er map entry for
da210 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 6b 65   page number 'ke
da220 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74  y'.** so that it
da230 20 6d 61 70 73 20 74 6f 20 74 79 70 65 20 27 65   maps to type 'e
da240 54 79 70 65 27 20 61 6e 64 20 70 61 72 65 6e 74  Type' and parent
da250 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 70 67   page number 'pg
da260 6e 6f 27 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72  no'..** An error
da270 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
da280 64 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67  d if something g
da290 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72  oes wrong, other
da2a0 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  wise SQLITE_OK..
da2b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74  */.static int pt
da2c0 72 6d 61 70 50 75 74 28 42 74 53 68 61 72 65 64  rmapPut(BtShared
da2d0 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c   *pBt, Pgno key,
da2e0 20 75 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20   u8 eType, Pgno 
da2f0 70 61 72 65 6e 74 29 7b 0a 20 20 44 62 50 61 67  parent){.  DbPag
da300 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20  e *pDbPage;  /* 
da310 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
da320 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50  page */.  u8 *pP
da330 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54  trmap;      /* T
da340 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64  he pointer map d
da350 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50  ata */.  Pgno iP
da360 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68  trmap;     /* Th
da370 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  e pointer map pa
da380 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  ge number */.  i
da390 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  nt offset;      
da3a0 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f   /* Offset in po
da3b0 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a  inter map page *
da3c0 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  /.  int rc;..  a
da3d0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
da3e0 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
da3f0 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68  utex) );.  /* Th
da400 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  e master-journal
da410 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6d 75 73   page number mus
da420 74 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20  t never be used 
da430 61 73 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70  as a pointer map
da440 20 70 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72   page */.  asser
da450 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50  t( 0==PTRMAP_ISP
da460 41 47 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47  AGE(pBt, PENDING
da470 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 29  _BYTE_PAGE(pBt))
da480 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
da490 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
da4a0 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29  ;.  if( key==0 )
da4b0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
da4c0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
da4d0 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20  ;.  }.  iPtrmap 
da4e0 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  = PTRMAP_PAGENO(
da4f0 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20  pBt, key);.  rc 
da500 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
da510 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
da520 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65  Ptrmap, &pDbPage
da530 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
da540 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
da550 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6f  turn rc;.  }.  o
da560 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50  ffset = PTRMAP_P
da570 54 52 4f 46 46 53 45 54 28 70 42 74 2c 20 6b 65  TROFFSET(pBt, ke
da580 79 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 3d 20  y);.  pPtrmap = 
da590 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67  (u8 *)sqlite3Pag
da5a0 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67  erGetData(pDbPag
da5b0 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65  e);..  if( eType
da5c0 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  !=pPtrmap[offset
da5d0 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70  ] || get4byte(&p
da5e0 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d  Ptrmap[offset+1]
da5f0 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20  )!=parent ){.   
da600 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f   TRACE(("PTRMAP_
da610 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c  UPDATE: %d->(%d,
da620 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79  %d)\n", key, eTy
da630 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20  pe, parent));.  
da640 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
da650 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65  gerWrite(pDbPage
da660 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
da670 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
da680 20 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74    pPtrmap[offset
da690 5d 20 3d 20 65 54 79 70 65 3b 0a 20 20 20 20 20  ] = eType;.     
da6a0 20 70 75 74 34 62 79 74 65 28 26 70 50 74 72 6d   put4byte(&pPtrm
da6b0 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 70 61  ap[offset+1], pa
da6c0 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  rent);.    }.  }
da6d0 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ..  sqlite3Pager
da6e0 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a  Unref(pDbPage);.
da6f0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
da700 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20 65 6e  /*.** Read an en
da710 74 72 79 20 66 72 6f 6d 20 74 68 65 20 70 6f 69  try from the poi
da720 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20  nter map..**.** 
da730 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
da740 72 69 65 76 65 73 20 74 68 65 20 70 6f 69 6e 74  rieves the point
da750 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72  er map entry for
da760 20 70 61 67 65 20 27 6b 65 79 27 2c 20 77 72 69   page 'key', wri
da770 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79 70 65  ting.** the type
da780 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65   and parent page
da790 20 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45 54 79   number to *pETy
da7a0 70 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65  pe and *pPgno re
da7b0 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 41  spectively..** A
da7c0 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  n error code is 
da7d0 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65  returned if some
da7e0 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
da7f0 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49  , otherwise SQLI
da800 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63  TE_OK..*/.static
da810 20 69 6e 74 20 70 74 72 6d 61 70 47 65 74 28 42   int ptrmapGet(B
da820 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67  tShared *pBt, Pg
da830 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 54 79  no key, u8 *pETy
da840 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 29  pe, Pgno *pPgno)
da850 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  {.  DbPage *pDbP
da860 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 6f  age;   /* The po
da870 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a  inter map page *
da880 2f 0a 20 20 69 6e 74 20 69 50 74 72 6d 61 70 3b  /.  int iPtrmap;
da890 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
da8a0 72 20 6d 61 70 20 70 61 67 65 20 69 6e 64 65 78  r map page index
da8b0 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61   */.  u8 *pPtrma
da8c0 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  p;       /* Poin
da8d0 74 65 72 20 6d 61 70 20 70 61 67 65 20 64 61 74  ter map page dat
da8e0 61 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65  a */.  int offse
da8f0 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66  t;        /* Off
da900 73 65 74 20 6f 66 20 65 6e 74 72 79 20 69 6e 20  set of entry in 
da910 70 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f 0a 20  pointer map */. 
da920 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
da930 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
da940 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
da950 78 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d 61 70  x) );..  iPtrmap
da960 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f   = PTRMAP_PAGENO
da970 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63  (pBt, key);.  rc
da980 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
da990 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  et(pBt->pPager, 
da9a0 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67  iPtrmap, &pDbPag
da9b0 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20  e);.  if( rc!=0 
da9c0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
da9d0 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20  ;.  }.  pPtrmap 
da9e0 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50  = (u8 *)sqlite3P
da9f0 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
daa00 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65 74 20  age);..  offset 
daa10 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53  = PTRMAP_PTROFFS
daa20 45 54 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20  ET(pBt, key);.  
daa30 61 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d  assert( pEType!=
daa40 30 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d  0 );.  *pEType =
daa50 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d   pPtrmap[offset]
daa60 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20  ;.  if( pPgno ) 
daa70 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74  *pPgno = get4byt
daa80 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65  e(&pPtrmap[offse
daa90 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65  t+1]);..  sqlite
daaa0 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50  3PagerUnref(pDbP
daab0 61 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54  age);.  if( *pET
daac0 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65  ype<1 || *pEType
daad0 3e 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  >5 ) return SQLI
daae0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
daaf0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
dab00 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  _OK;.}..#endif /
dab10 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  * SQLITE_OMIT_AU
dab20 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 0a  TOVACUUM */../*.
dab30 2a 2a 20 47 69 76 65 6e 20 61 20 62 74 72 65 65  ** Given a btree
dab40 20 70 61 67 65 20 61 6e 64 20 61 20 63 65 6c 6c   page and a cell
dab50 20 69 6e 64 65 78 20 28 30 20 6d 65 61 6e 73 20   index (0 means 
dab60 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 6f  the first cell o
dab70 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 2c 20 31  n.** the page, 1
dab80 20 6d 65 61 6e 73 20 74 68 65 20 73 65 63 6f 6e   means the secon
dab90 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73 6f 20 66  d cell, and so f
daba0 6f 72 74 68 29 20 72 65 74 75 72 6e 20 61 20 70  orth) return a p
dabb0 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ointer.** to the
dabc0 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a   cell content..*
dabd0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
dabe0 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20 66 6f 72  e works only for
dabf0 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 6e   pages that do n
dac00 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66  ot contain overf
dac10 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64  low cells..*/.#d
dac20 65 66 69 6e 65 20 66 69 6e 64 43 65 6c 6c 28 70  efine findCell(p
dac30 50 61 67 65 2c 20 69 43 65 6c 6c 29 20 5c 0a 20  Page, iCell) \. 
dac40 20 28 28 70 50 61 67 65 29 2d 3e 61 44 61 74 61   ((pPage)->aData
dac50 20 2b 20 67 65 74 32 62 79 74 65 28 26 28 70 50   + get2byte(&(pP
dac60 61 67 65 29 2d 3e 61 44 61 74 61 5b 28 70 50 61  age)->aData[(pPa
dac70 67 65 29 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2b  ge)->cellOffset+
dac80 32 2a 28 69 43 65 6c 6c 29 5d 29 29 0a 23 69 66  2*(iCell)])).#if
dac90 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
daca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
dacb0 38 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 46  8 *sqlite3BtreeF
dacc0 69 6e 64 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20  indCell(MemPage 
dacd0 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c  *pPage, int iCel
dace0 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 43  l){.  assert( iC
dacf0 65 6c 6c 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65  ell>=0 );.  asse
dad00 72 74 28 20 69 43 65 6c 6c 3c 67 65 74 32 62 79  rt( iCell<get2by
dad10 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
dad20 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
dad30 74 2b 33 5d 29 20 29 3b 0a 20 20 72 65 74 75 72  t+3]) );.  retur
dad40 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  n findCell(pPage
dad50 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 23 65 6e 64  , iCell);.}.#end
dad60 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61  if../*.** This a
dad70 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76 65   more complex ve
dad80 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  rsion of sqlite3
dad90 42 74 72 65 65 46 69 6e 64 43 65 6c 6c 28 29 20  BtreeFindCell() 
dada0 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a  that works for.*
dadb0 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20  * pages that do 
dadc0 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77  contain overflow
dadd0 20 63 65 6c 6c 73 2e 20 20 53 65 65 20 69 6e 73   cells.  See ins
dade0 65 72 74 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38  ert.*/.static u8
dadf0 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65   *findOverflowCe
dae00 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ll(MemPage *pPag
dae10 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20  e, int iCell){. 
dae20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
dae30 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
dae40 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
dae50 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
dae60 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  (i=pPage->nOverf
dae70 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  low-1; i>=0; i--
dae80 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20  ){.    int k;.  
dae90 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65    struct _OvflCe
daea0 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70  ll *pOvfl;.    p
daeb0 4f 76 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61  Ovfl = &pPage->a
daec0 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d  Ovfl[i];.    k =
daed0 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20   pOvfl->idx;.   
daee0 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b   if( k<=iCell ){
daef0 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43  .      if( k==iC
daf00 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ell ){.        r
daf10 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65  eturn pOvfl->pCe
daf20 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
daf30 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d    iCell--;.    }
daf40 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69  .  }.  return fi
daf50 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  ndCell(pPage, iC
daf60 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ell);.}../*.** P
daf70 61 72 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74  arse a cell cont
daf80 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69  ent block and fi
daf90 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e  ll in the CellIn
dafa0 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  fo structure.  T
dafb0 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20  here.** are two 
dafc0 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73  versions of this
dafd0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 73 71 6c 69   function.  sqli
dafe0 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
daff0 6c 28 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20  l() takes a .** 
db000 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 20 74 68  cell index as th
db010 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
db020 74 20 61 6e 64 20 73 71 6c 69 74 65 33 42 74 72  t and sqlite3Btr
db030 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29  eeParseCellPtr()
db040 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 70 6f 69   .** takes a poi
db050 6e 74 65 72 20 74 6f 20 74 68 65 20 62 6f 64 79  nter to the body
db060 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 61 73 20   of the cell as 
db070 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  its second argum
db080 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69  ent..**.** Withi
db090 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65  n this file, the
db0a0 20 70 61 72 73 65 43 65 6c 6c 28 29 20 6d 61 63   parseCell() mac
db0b0 72 6f 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  ro can be called
db0c0 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 73   instead of.** s
db0d0 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
db0e0 43 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67  CellPtr(). Using
db0f0 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c   some compilers,
db100 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61   this will be fa
db110 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ster..*/.SQLITE_
db120 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
db130 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
db140 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65  llPtr(.  MemPage
db150 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20   *pPage,        
db160 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e   /* Page contain
db170 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a  ing the cell */.
db180 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20    u8 *pCell,    
db190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
db1a0 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c  nter to the cell
db1b0 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c   text. */.  Cell
db1c0 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20  Info *pInfo     
db1d0 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74      /* Fill in t
db1e0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
db1f0 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20  .){.  int n;    
db200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
db210 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e   Number bytes in
db220 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
db230 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50  ader */.  u32 nP
db240 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20  ayload;         
db250 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
db260 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79  ytes of cell pay
db270 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  load */..  asser
db280 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
db290 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
db2a0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
db2b0 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43  Info->pCell = pC
db2c0 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ell;.  assert( p
db2d0 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c  Page->leaf==0 ||
db2e0 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20   pPage->leaf==1 
db2f0 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e  );.  n = pPage->
db300 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20  childPtrSize;.  
db310 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70  assert( n==4-4*p
db320 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
db330 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
db340 74 61 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 67  ta ){.    n += g
db350 65 74 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c  etVarint32(&pCel
db360 6c 5b 6e 5d 2c 20 26 6e 50 61 79 6c 6f 61 64 29  l[n], &nPayload)
db370 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
db380 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 7d  Payload = 0;.  }
db390 0a 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20  .  pInfo->nData 
db3a0 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 69 66  = nPayload;.  if
db3b0 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
db3c0 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56  ){.    n += getV
db3d0 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c  arint(&pCell[n],
db3e0 20 28 75 36 34 20 2a 29 26 70 49 6e 66 6f 2d 3e   (u64 *)&pInfo->
db3f0 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nKey);.  }else{.
db400 20 20 20 20 75 33 32 20 78 3b 0a 20 20 20 20 6e      u32 x;.    n
db410 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28   += getVarint32(
db420 26 70 43 65 6c 6c 5b 6e 5d 2c 20 26 78 29 3b 0a  &pCell[n], &x);.
db430 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20      pInfo->nKey 
db440 3d 20 78 3b 0a 20 20 20 20 6e 50 61 79 6c 6f 61  = x;.    nPayloa
db450 64 20 2b 3d 20 78 3b 0a 20 20 7d 0a 20 20 70 49  d += x;.  }.  pI
db460 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20  nfo->nPayload = 
db470 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66  nPayload;.  pInf
db480 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a  o->nHeader = n;.
db490 20 20 69 66 28 20 6e 50 61 79 6c 6f 61 64 3c 3d    if( nPayload<=
db4a0 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20  pPage->maxLocal 
db4b0 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69  ){.    /* This i
db4c0 73 20 74 68 65 20 28 65 61 73 79 29 20 63 6f 6d  s the (easy) com
db4d0 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 74  mon case where t
db4e0 68 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61  he entire payloa
db4f0 64 20 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f 6e  d fits.    ** on
db500 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2e   the local page.
db510 20 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69 73    No overflow is
db520 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a   required..    *
db530 2f 0a 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 3b  /.    int nSize;
db540 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
db550 61 6c 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 20  al size of cell 
db560 63 6f 6e 74 65 6e 74 20 69 6e 20 62 79 74 65 73  content in bytes
db570 20 2a 2f 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e   */.    pInfo->n
db580 4c 6f 63 61 6c 20 3d 20 6e 50 61 79 6c 6f 61 64  Local = nPayload
db590 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76  ;.    pInfo->iOv
db5a0 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20  erflow = 0;.    
db5b0 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64  nSize = nPayload
db5c0 20 2b 20 6e 3b 0a 20 20 20 20 69 66 28 20 6e 53   + n;.    if( nS
db5d0 69 7a 65 3c 34 20 29 7b 0a 20 20 20 20 20 20 6e  ize<4 ){.      n
db5e0 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20 20 20  Size = 4;       
db5f0 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c   /* Minimum cell
db600 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20   size is 4 */.  
db610 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e    }.    pInfo->n
db620 53 69 7a 65 20 3d 20 6e 53 69 7a 65 3b 0a 20 20  Size = nSize;.  
db630 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66  }else{.    /* If
db640 20 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c   the payload wil
db650 6c 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65  l not fit comple
db660 74 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61  tely on the loca
db670 6c 20 70 61 67 65 2c 20 77 65 20 68 61 76 65 0a  l page, we have.
db680 20 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 65      ** to decide
db690 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 6f   how much to sto
db6a0 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68  re locally and h
db6b0 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c  ow much to spill
db6c0 20 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65   onto.    ** ove
db6d0 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68  rflow pages.  Th
db6e0 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74 6f  e strategy is to
db6f0 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d   minimize the am
db700 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a 20  ount of unused. 
db710 20 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f     ** space on o
db720 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 68  verflow pages wh
db730 69 6c 65 20 6b 65 65 70 69 6e 67 20 74 68 65 20  ile keeping the 
db740 61 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20  amount of local 
db750 73 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69  storage.    ** i
db760 6e 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63  n between minLoc
db770 61 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e  al and maxLocal.
db780 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57  .    **.    ** W
db790 61 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e  arning:  changin
db7a0 67 20 74 68 65 20 77 61 79 20 6f 76 65 72 66 6c  g the way overfl
db7b0 6f 77 20 70 61 79 6c 6f 61 64 20 69 73 20 64 69  ow payload is di
db7c0 73 74 72 69 62 75 74 65 64 20 69 6e 20 61 6e 79  stributed in any
db7d0 0a 20 20 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c  .    ** way will
db7e0 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e   result in an in
db7f0 63 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 20  compatible file 
db800 66 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20  format..    */. 
db810 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b     int minLocal;
db820 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f    /* Minimum amo
db830 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68  unt of payload h
db840 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20  eld locally */. 
db850 20 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b     int maxLocal;
db860 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f    /* Maximum amo
db870 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68  unt of payload h
db880 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20  eld locally */. 
db890 20 20 20 69 6e 74 20 73 75 72 70 6c 75 73 3b 20     int surplus; 
db8a0 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61    /* Overflow pa
db8b0 79 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 20  yload available 
db8c0 66 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67  for local storag
db8d0 65 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63  e */..    minLoc
db8e0 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c  al = pPage->minL
db8f0 6f 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63  ocal;.    maxLoc
db900 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c  al = pPage->maxL
db910 6f 63 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75  ocal;.    surplu
db920 73 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28  s = minLocal + (
db930 6e 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f  nPayload - minLo
db940 63 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74  cal)%(pPage->pBt
db950 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34  ->usableSize - 4
db960 29 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70 6c  );.    if( surpl
db970 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29  us <= maxLocal )
db980 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  {.      pInfo->n
db990 4c 6f 63 61 6c 20 3d 20 73 75 72 70 6c 75 73 3b  Local = surplus;
db9a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
db9b0 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20    pInfo->nLocal 
db9c0 3d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20  = minLocal;.    
db9d0 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76  }.    pInfo->iOv
db9e0 65 72 66 6c 6f 77 20 3d 20 70 49 6e 66 6f 2d 3e  erflow = pInfo->
db9f0 6e 4c 6f 63 61 6c 20 2b 20 6e 3b 0a 20 20 20 20  nLocal + n;.    
dba00 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70  pInfo->nSize = p
dba10 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20  Info->iOverflow 
dba20 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69  + 4;.  }.}.#defi
dba30 6e 65 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61  ne parseCell(pPa
dba40 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f  ge, iCell, pInfo
dba50 29 20 5c 0a 20 20 73 71 6c 69 74 65 33 42 74 72  ) \.  sqlite3Btr
dba60 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28  eeParseCellPtr((
dba70 70 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c  pPage), findCell
dba80 28 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c  ((pPage), (iCell
dba90 29 29 2c 20 28 70 49 6e 66 6f 29 29 0a 53 51 4c  )), (pInfo)).SQL
dbaa0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
dbab0 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
dbac0 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67  seCell(.  MemPag
dbad0 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20  e *pPage,       
dbae0 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69    /* Page contai
dbaf0 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f  ning the cell */
dbb00 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20  .  int iCell,   
dbb10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
dbb20 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46  e cell index.  F
dbb30 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a  irst cell is 0 *
dbb40 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49  /.  CellInfo *pI
dbb50 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46  nfo         /* F
dbb60 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75  ill in this stru
dbb70 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61  cture */.){.  pa
dbb80 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  rseCell(pPage, i
dbb90 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a  Cell, pInfo);.}.
dbba0 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
dbbb0 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
dbbc0 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20  of bytes that a 
dbbd0 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68  Cell needs in th
dbbe0 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61  e cell.** data a
dbbf0 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65  rea of the btree
dbc00 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75  -page.  The retu
dbc10 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64  rn number includ
dbc20 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64  es the cell.** d
dbc30 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74  ata header and t
dbc40 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  he local payload
dbc50 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76  , but not any ov
dbc60 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a  erflow page or.*
dbc70 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64  * the space used
dbc80 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69   by the cell poi
dbc90 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  nter..*/.#ifndef
dbca0 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 69   NDEBUG.static i
dbcb0 6e 74 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50  nt cellSize(MemP
dbcc0 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20  age *pPage, int 
dbcd0 69 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 6e  iCell){.  CellIn
dbce0 66 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c 69 74  fo info;.  sqlit
dbcf0 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
dbd00 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 26  (pPage, iCell, &
dbd10 69 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20  info);.  return 
dbd20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 7d 0a 23 65  info.nSize;.}.#e
dbd30 6e 64 69 66 0a 73 74 61 74 69 63 20 69 6e 74 20  ndif.static int 
dbd40 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d 50  cellSizePtr(MemP
dbd50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a  age *pPage, u8 *
dbd60 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 6e  pCell){.  CellIn
dbd70 66 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c 69 74  fo info;.  sqlit
dbd80 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
dbd90 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
dbda0 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 72 65 74 75  , &info);.  retu
dbdb0 72 6e 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 7d  rn info.nSize;.}
dbdc0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
dbdd0 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
dbde0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65  ./*.** If the ce
dbdf0 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20 6f  ll pCell, part o
dbe00 66 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e  f page pPage con
dbe10 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a  tains a pointer.
dbe20 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f  ** to an overflo
dbe30 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61  w page, insert a
dbe40 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65  n entry into the
dbe50 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20   pointer-map.** 
dbe60 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  for the overflow
dbe70 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   page..*/.static
dbe80 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f 76   int ptrmapPutOv
dbe90 66 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70  flPtr(MemPage *p
dbea0 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29  Page, u8 *pCell)
dbeb0 7b 0a 20 20 69 66 28 20 70 43 65 6c 6c 20 29 7b  {.  if( pCell ){
dbec0 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e  .    CellInfo in
dbed0 66 6f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  fo;.    sqlite3B
dbee0 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
dbef0 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26  (pPage, pCell, &
dbf00 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73 65 72  info);.    asser
dbf10 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28  t( (info.nData+(
dbf20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a  pPage->intKey?0:
dbf30 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66  info.nKey))==inf
dbf40 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20  o.nPayload );.  
dbf50 20 20 69 66 28 20 28 69 6e 66 6f 2e 6e 44 61 74    if( (info.nDat
dbf60 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  a+(pPage->intKey
dbf70 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3e 69  ?0:info.nKey))>i
dbf80 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20  nfo.nLocal ){.  
dbf90 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20      Pgno ovfl = 
dbfa0 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b  get4byte(&pCell[
dbfb0 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29  info.iOverflow])
dbfc0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  ;.      return p
dbfd0 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d 3e  trmapPut(pPage->
dbfe0 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d 41  pBt, ovfl, PTRMA
dbff0 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 61  P_OVERFLOW1, pPa
dc000 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 7d  ge->pgno);.    }
dc010 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
dc020 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a  LITE_OK;.}./*.**
dc030 20 49 66 20 74 68 65 20 63 65 6c 6c 20 77 69 74   If the cell wit
dc040 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20 6f 6e  h index iCell on
dc050 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74   page pPage cont
dc060 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a  ains a pointer.*
dc070 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77  * to an overflow
dc080 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e   page, insert an
dc090 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20   entry into the 
dc0a0 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66  pointer-map.** f
dc0b0 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
dc0c0 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  page..*/.static 
dc0d0 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f 76 66  int ptrmapPutOvf
dc0e0 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  l(MemPage *pPage
dc0f0 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20  , int iCell){.  
dc100 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 61 73 73  u8 *pCell;.  ass
dc110 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
dc120 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
dc130 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
dc140 70 43 65 6c 6c 20 3d 20 66 69 6e 64 4f 76 65 72  pCell = findOver
dc150 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20  flowCell(pPage, 
dc160 69 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  iCell);.  return
dc170 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74   ptrmapPutOvflPt
dc180 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b  r(pPage, pCell);
dc190 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
dc1a0 2a 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65  * Defragment the
dc1b0 20 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c   page given.  Al
dc1c0 6c 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65  l Cells are move
dc1d0 64 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20  d to the.** end 
dc1e0 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20  of the page and 
dc1f0 61 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69  all free space i
dc200 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f  s collected into
dc210 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65   one.** big Free
dc220 42 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20  Blk that occurs 
dc230 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68  in between the h
dc240 65 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a  eader and cell.*
dc250 2a 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20  * pointer array 
dc260 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e  and the cell con
dc270 74 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74  tent area..*/.st
dc280 61 74 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d  atic int defragm
dc290 65 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20  entPage(MemPage 
dc2a0 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69  *pPage){.  int i
dc2b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
dc2c0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
dc2d0 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70  unter */.  int p
dc2e0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
dc2f0 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
dc300 20 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20   of a i-th cell 
dc310 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20  */.  int addr;  
dc320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dc330 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 66 69 72  /* Offset of fir
dc340 73 74 20 62 79 74 65 20 61 66 74 65 72 20 63 65  st byte after ce
dc350 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
dc360 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20   */.  int hdr;  
dc370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dc380 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68   /* Offset to th
dc390 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f  e page header */
dc3a0 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20  .  int size;    
dc3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
dc3c0 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20   Size of a cell 
dc3d0 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53  */.  int usableS
dc3e0 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
dc3f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61  /* Number of usa
dc400 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70  ble bytes on a p
dc410 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c  age */.  int cel
dc420 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  lOffset;        
dc430 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f      /* Offset to
dc440 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65   the cell pointe
dc450 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74  r array */.  int
dc460 20 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20   brk;           
dc470 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
dc480 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f  t to the cell co
dc490 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20  ntent area */.  
dc4a0 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20  int nCell;      
dc4b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
dc4c0 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e  mber of cells on
dc4d0 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75   the page */.  u
dc4e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61  nsigned char *da
dc4f0 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ta;       /* The
dc500 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20   page data */.  
dc510 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74  unsigned char *t
dc520 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65  emp;       /* Te
dc530 6d 70 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c  mp area for cell
dc540 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61   content */..  a
dc550 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
dc560 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
dc570 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
dc580 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
dc590 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61  e->pBt!=0 );.  a
dc5a0 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42  ssert( pPage->pB
dc5b0 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d  t->usableSize <=
dc5c0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
dc5d0 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72  _SIZE );.  asser
dc5e0 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
dc5f0 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  low==0 );.  asse
dc600 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
dc610 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
dc620 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74  t->mutex) );.  t
dc630 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  emp = sqlite3_ma
dc640 6c 6c 6f 63 28 20 70 50 61 67 65 2d 3e 70 42 74  lloc( pPage->pBt
dc650 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20  ->pageSize );.  
dc660 69 66 28 20 74 65 6d 70 3d 3d 30 20 29 20 72 65  if( temp==0 ) re
dc670 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
dc680 4d 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67  M;.  data = pPag
dc690 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20  e->aData;.  hdr 
dc6a0 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
dc6b0 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74  et;.  cellOffset
dc6c0 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66   = pPage->cellOf
dc6d0 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20  fset;.  nCell = 
dc6e0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20  pPage->nCell;.  
dc6f0 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d 67  assert( nCell==g
dc700 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
dc710 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61 62 6c  r+3]) );.  usabl
dc720 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e 70  eSize = pPage->p
dc730 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
dc740 20 20 62 72 6b 20 3d 20 67 65 74 32 62 79 74 65    brk = get2byte
dc750 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a  (&data[hdr+5]);.
dc760 20 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b 62    memcpy(&temp[b
dc770 72 6b 5d 2c 20 26 64 61 74 61 5b 62 72 6b 5d 2c  rk], &data[brk],
dc780 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 62 72   usableSize - br
dc790 6b 29 3b 0a 20 20 62 72 6b 20 3d 20 75 73 61 62  k);.  brk = usab
dc7a0 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 3d  leSize;.  for(i=
dc7b0 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
dc7c0 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b  {.    u8 *pAddr;
dc7d0 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68       /* The i-th
dc7e0 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f   cell pointer */
dc7f0 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61  .    pAddr = &da
dc800 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20  ta[cellOffset + 
dc810 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67  i*2];.    pc = g
dc820 65 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a  et2byte(pAddr);.
dc830 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3c 70      assert( pc<p
dc840 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
dc850 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 73 69 7a  eSize );.    siz
dc860 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  e = cellSizePtr(
dc870 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d  pPage, &temp[pc]
dc880 29 3b 0a 20 20 20 20 62 72 6b 20 2d 3d 20 73 69  );.    brk -= si
dc890 7a 65 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  ze;.    memcpy(&
dc8a0 64 61 74 61 5b 62 72 6b 5d 2c 20 26 74 65 6d 70  data[brk], &temp
dc8b0 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20  [pc], size);.   
dc8c0 20 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c   put2byte(pAddr,
dc8d0 20 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73   brk);.  }.  ass
dc8e0 65 72 74 28 20 62 72 6b 3e 3d 63 65 6c 6c 4f 66  ert( brk>=cellOf
dc8f0 66 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 29 3b 0a  fset+2*nCell );.
dc900 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
dc910 5b 68 64 72 2b 35 5d 2c 20 62 72 6b 29 3b 0a 20  [hdr+5], brk);. 
dc920 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30   data[hdr+1] = 0
dc930 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20  ;.  data[hdr+2] 
dc940 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b  = 0;.  data[hdr+
dc950 37 5d 20 3d 20 30 3b 0a 20 20 61 64 64 72 20 3d  7] = 0;.  addr =
dc960 20 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43   cellOffset+2*nC
dc970 65 6c 6c 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64  ell;.  memset(&d
dc980 61 74 61 5b 61 64 64 72 5d 2c 20 30 2c 20 62 72  ata[addr], 0, br
dc990 6b 2d 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74  k-addr);.  sqlit
dc9a0 65 33 5f 66 72 65 65 28 74 65 6d 70 29 3b 0a 20  e3_free(temp);. 
dc9b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
dc9c0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  K;.}../*.** Allo
dc9d0 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73  cate nByte bytes
dc9e0 20 6f 66 20 73 70 61 63 65 20 6f 6e 20 61 20 70   of space on a p
dc9f0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  age..**.** Retur
dca00 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f  n the index into
dca10 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20   pPage->aData[] 
dca20 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 79 74  of the first byt
dca30 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20  e of.** the new 
dca40 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 4f 72 20 72  allocation. Or r
dca50 65 74 75 72 6e 20 30 20 69 66 20 74 68 65 72 65  eturn 0 if there
dca60 20 69 73 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66   is not enough f
dca70 72 65 65 0a 2a 2a 20 73 70 61 63 65 20 6f 6e 20  ree.** space on 
dca80 74 68 65 20 70 61 67 65 20 74 6f 20 73 61 74 69  the page to sati
dca90 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  sfy the allocati
dcaa0 6f 6e 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a  on request..**.*
dcab0 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 63 6f  * If the page co
dcac0 6e 74 61 69 6e 73 20 6e 42 79 74 65 73 20 6f 66  ntains nBytes of
dcad0 20 66 72 65 65 20 73 70 61 63 65 20 62 75 74 20   free space but 
dcae0 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e  does not contain
dcaf0 0a 2a 2a 20 6e 42 79 74 65 73 20 6f 66 20 63 6f  .** nBytes of co
dcb00 6e 74 69 67 75 6f 75 73 20 66 72 65 65 20 73 70  ntiguous free sp
dcb10 61 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ace, then this r
dcb20 6f 75 74 69 6e 65 20 61 75 74 6f 6d 61 74 69 63  outine automatic
dcb30 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 73 20 64 65  ally.** calls de
dcb40 66 72 61 67 65 6d 65 6e 74 50 61 67 65 28 29 20  fragementPage() 
dcb50 74 6f 20 63 6f 6e 73 6f 6c 69 64 61 74 65 20 61  to consolidate a
dcb60 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 62 65  ll free space be
dcb70 66 6f 72 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74  fore .** allocat
dcb80 69 6e 67 20 74 68 65 20 6e 65 77 20 63 68 75 6e  ing the new chun
dcb90 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  k..*/.static int
dcba0 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 4d   allocateSpace(M
dcbb0 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
dcbc0 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74  nt nByte){.  int
dcbd0 20 61 64 64 72 2c 20 70 63 2c 20 68 64 72 3b 0a   addr, pc, hdr;.
dcbe0 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 69 6e    int size;.  in
dcbf0 74 20 6e 46 72 61 67 3b 0a 20 20 69 6e 74 20 74  t nFrag;.  int t
dcc00 6f 70 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b  op;.  int nCell;
dcc10 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65  .  int cellOffse
dcc20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  t;.  unsigned ch
dcc30 61 72 20 2a 64 61 74 61 3b 0a 20 20 0a 20 20 64  ar *data;.  .  d
dcc40 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
dcc50 74 61 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  ta;.  assert( sq
dcc60 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
dcc70 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
dcc80 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
dcc90 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b  t( pPage->pBt );
dcca0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
dccb0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
dccc0 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
dccd0 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c   );.  if( nByte<
dcce0 34 20 29 20 6e 42 79 74 65 20 3d 20 34 3b 0a 20  4 ) nByte = 4;. 
dccf0 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 46 72 65   if( pPage->nFre
dcd00 65 3c 6e 42 79 74 65 20 7c 7c 20 70 50 61 67 65  e<nByte || pPage
dcd10 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 20  ->nOverflow>0 ) 
dcd20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 50 61 67  return 0;.  pPag
dcd30 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 6e 42 79 74  e->nFree -= nByt
dcd40 65 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65  e;.  hdr = pPage
dcd50 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 0a 20 20  ->hdrOffset;..  
dcd60 6e 46 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72  nFrag = data[hdr
dcd70 2b 37 5d 3b 0a 20 20 69 66 28 20 6e 46 72 61 67  +7];.  if( nFrag
dcd80 3c 36 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65  <60 ){.    /* Se
dcd90 61 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 73  arch the freelis
dcda0 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20  t looking for a 
dcdb0 73 6c 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 20  slot big enough 
dcdc0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 20  to satisfy the. 
dcdd0 20 20 20 2a 2a 20 73 70 61 63 65 20 72 65 71 75     ** space requ
dcde0 65 73 74 2e 20 2a 2f 0a 20 20 20 20 61 64 64 72  est. */.    addr
dcdf0 20 3d 20 68 64 72 2b 31 3b 0a 20 20 20 20 77 68   = hdr+1;.    wh
dce00 69 6c 65 28 20 28 70 63 20 3d 20 67 65 74 32 62  ile( (pc = get2b
dce10 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29  yte(&data[addr])
dce20 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a  )>0 ){.      siz
dce30 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  e = get2byte(&da
dce40 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20  ta[pc+2]);.     
dce50 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65   if( size>=nByte
dce60 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
dce70 73 69 7a 65 3c 6e 42 79 74 65 2b 34 20 29 7b 0a  size<nByte+4 ){.
dce80 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
dce90 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64  (&data[addr], &d
dcea0 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20  ata[pc], 2);.   
dceb0 20 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b         data[hdr+
dcec0 37 5d 20 3d 20 6e 46 72 61 67 20 2b 20 73 69 7a  7] = nFrag + siz
dced0 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20 20  e - nByte;.     
dcee0 20 20 20 20 20 72 65 74 75 72 6e 20 70 63 3b 0a       return pc;.
dcef0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
dcf00 20 20 20 20 20 20 20 20 20 70 75 74 32 62 79 74           put2byt
dcf10 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c 20 73  e(&data[pc+2], s
dcf20 69 7a 65 2d 6e 42 79 74 65 29 3b 0a 20 20 20 20  ize-nByte);.    
dcf30 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 63 20        return pc 
dcf40 2b 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a  + size - nByte;.
dcf50 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
dcf60 7d 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 70  }.      addr = p
dcf70 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
dcf80 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  /* Allocate memo
dcf90 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20  ry from the gap 
dcfa0 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63  in between the c
dcfb0 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
dcfc0 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63  y.  ** and the c
dcfd0 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
dcfe0 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 3d 20 67  ..  */.  top = g
dcff0 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
dd000 72 2b 35 5d 29 3b 0a 20 20 6e 43 65 6c 6c 20 3d  r+5]);.  nCell =
dd010 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
dd020 68 64 72 2b 33 5d 29 3b 0a 20 20 63 65 6c 6c 4f  hdr+3]);.  cellO
dd030 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63  ffset = pPage->c
dd040 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 69 66 28  ellOffset;.  if(
dd050 20 6e 46 72 61 67 3e 3d 36 30 20 7c 7c 20 63 65   nFrag>=60 || ce
dd060 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 65  llOffset + 2*nCe
dd070 6c 6c 20 3e 20 74 6f 70 20 2d 20 6e 42 79 74 65  ll > top - nByte
dd080 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 66 72   ){.    if( defr
dd090 61 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65  agmentPage(pPage
dd0a0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
dd0b0 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65    top = get2byte
dd0c0 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a  (&data[hdr+5]);.
dd0d0 20 20 7d 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79    }.  top -= nBy
dd0e0 74 65 3b 0a 20 20 61 73 73 65 72 74 28 20 63 65  te;.  assert( ce
dd0f0 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 65  llOffset + 2*nCe
dd100 6c 6c 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 20 70  ll <= top );.  p
dd110 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
dd120 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 72 65  r+5], top);.  re
dd130 74 75 72 6e 20 74 6f 70 3b 0a 7d 0a 0a 2f 2a 0a  turn top;.}../*.
dd140 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 65 63 74  ** Return a sect
dd150 69 6f 6e 20 6f 66 20 74 68 65 20 70 50 61 67 65  ion of the pPage
dd160 2d 3e 61 44 61 74 61 20 74 6f 20 74 68 65 20 66  ->aData to the f
dd170 72 65 65 6c 69 73 74 2e 0a 2a 2a 20 54 68 65 20  reelist..** The 
dd180 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
dd190 65 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b  e new free block
dd1a0 20 69 73 20 70 50 61 67 65 2d 3e 61 44 69 73 6b   is pPage->aDisk
dd1b0 5b 73 74 61 72 74 5d 0a 2a 2a 20 61 6e 64 20 74  [start].** and t
dd1c0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62  he size of the b
dd1d0 6c 6f 63 6b 20 69 73 20 22 73 69 7a 65 22 20 62  lock is "size" b
dd1e0 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74  ytes..**.** Most
dd1f0 20 6f 66 20 74 68 65 20 65 66 66 6f 72 74 20 68   of the effort h
dd200 65 72 65 20 69 73 20 69 6e 76 6f 6c 76 65 64 20  ere is involved 
dd210 69 6e 20 63 6f 61 6c 65 73 69 6e 67 20 61 64 6a  in coalesing adj
dd220 61 63 65 6e 74 0a 2a 2a 20 66 72 65 65 20 62 6c  acent.** free bl
dd230 6f 63 6b 73 20 69 6e 74 6f 20 61 20 73 69 6e 67  ocks into a sing
dd240 6c 65 20 62 69 67 20 66 72 65 65 20 62 6c 6f 63  le big free bloc
dd250 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
dd260 64 20 66 72 65 65 53 70 61 63 65 28 4d 65 6d 50  d freeSpace(MemP
dd270 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20  age *pPage, int 
dd280 73 74 61 72 74 2c 20 69 6e 74 20 73 69 7a 65 29  start, int size)
dd290 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c 20 70 62  {.  int addr, pb
dd2a0 65 67 69 6e 2c 20 68 64 72 3b 0a 20 20 75 6e 73  egin, hdr;.  uns
dd2b0 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61  igned char *data
dd2c0 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
dd2d0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
dd2e0 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61  e->pBt!=0 );.  a
dd2f0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
dd300 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
dd310 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
dd320 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 72  ;.  assert( star
dd330 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  t>=pPage->hdrOff
dd340 73 65 74 2b 36 2b 28 70 50 61 67 65 2d 3e 6c 65  set+6+(pPage->le
dd350 61 66 3f 30 3a 34 29 20 29 3b 0a 20 20 61 73 73  af?0:4) );.  ass
dd360 65 72 74 28 20 28 73 74 61 72 74 20 2b 20 73 69  ert( (start + si
dd370 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d  ze)<=pPage->pBt-
dd380 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20  >usableSize );. 
dd390 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
dd3a0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
dd3b0 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
dd3c0 3b 0a 20 20 69 66 28 20 73 69 7a 65 3c 34 20 29  ;.  if( size<4 )
dd3d0 20 73 69 7a 65 20 3d 20 34 3b 0a 0a 23 69 66 64   size = 4;..#ifd
dd3e0 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45  ef SQLITE_SECURE
dd3f0 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65  _DELETE.  /* Ove
dd400 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69  rwrite deleted i
dd410 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20  nformation with 
dd420 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53  zeros when the S
dd430 45 43 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20  ECURE_DELETE .  
dd440 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61  ** option is ena
dd450 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d  bled at compile-
dd460 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  time */.  memset
dd470 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30  (&data[start], 0
dd480 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a  , size);.#endif.
dd490 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70  .  /* Add the sp
dd4a0 61 63 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  ace back into th
dd4b0 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  e linked list of
dd4c0 20 66 72 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20   freeblocks */. 
dd4d0 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64   hdr = pPage->hd
dd4e0 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20  rOffset;.  addr 
dd4f0 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69  = hdr + 1;.  whi
dd500 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65  le( (pbegin = ge
dd510 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64  t2byte(&data[add
dd520 72 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 62  r]))<start && pb
dd530 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73  egin>0 ){.    as
dd540 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50  sert( pbegin<=pP
dd550 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
dd560 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 61 73  Size-4 );.    as
dd570 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64  sert( pbegin>add
dd580 72 20 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20  r );.    addr = 
dd590 70 62 65 67 69 6e 3b 0a 20 20 7d 0a 20 20 61 73  pbegin;.  }.  as
dd5a0 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50  sert( pbegin<=pP
dd5b0 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
dd5c0 53 69 7a 65 2d 34 20 29 3b 0a 20 20 61 73 73 65  Size-4 );.  asse
dd5d0 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20  rt( pbegin>addr 
dd5e0 7c 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a  || pbegin==0 );.
dd5f0 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
dd600 5b 61 64 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a  [addr], start);.
dd610 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
dd620 5b 73 74 61 72 74 5d 2c 20 70 62 65 67 69 6e 29  [start], pbegin)
dd630 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
dd640 74 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a  ta[start+2], siz
dd650 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72  e);.  pPage->nFr
dd660 65 65 20 2b 3d 20 73 69 7a 65 3b 0a 0a 20 20 2f  ee += size;..  /
dd670 2a 20 43 6f 61 6c 65 73 63 65 20 61 64 6a 61 63  * Coalesce adjac
dd680 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 20  ent free blocks 
dd690 2a 2f 0a 20 20 61 64 64 72 20 3d 20 70 50 61 67  */.  addr = pPag
dd6a0 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 2b 20 31  e->hdrOffset + 1
dd6b0 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67  ;.  while( (pbeg
dd6c0 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  in = get2byte(&d
dd6d0 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b  ata[addr]))>0 ){
dd6e0 0a 20 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20  .    int pnext, 
dd6f0 70 73 69 7a 65 3b 0a 20 20 20 20 61 73 73 65 72  psize;.    asser
dd700 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 29  t( pbegin>addr )
dd710 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 62  ;.    assert( pb
dd720 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74  egin<=pPage->pBt
dd730 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29  ->usableSize-4 )
dd740 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 67 65  ;.    pnext = ge
dd750 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65  t2byte(&data[pbe
dd760 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65  gin]);.    psize
dd770 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
dd780 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20  a[pbegin+2]);.  
dd790 20 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20 70    if( pbegin + p
dd7a0 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78  size + 3 >= pnex
dd7b0 74 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a  t && pnext>0 ){.
dd7c0 20 20 20 20 20 20 69 6e 74 20 66 72 61 67 20 3d        int frag =
dd7d0 20 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69 6e   pnext - (pbegin
dd7e0 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 61  +psize);.      a
dd7f0 73 73 65 72 74 28 20 66 72 61 67 3c 3d 64 61 74  ssert( frag<=dat
dd800 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
dd810 65 74 2b 37 5d 20 29 3b 0a 20 20 20 20 20 20 64  et+7] );.      d
dd820 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
dd830 66 73 65 74 2b 37 5d 20 2d 3d 20 66 72 61 67 3b  fset+7] -= frag;
dd840 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28  .      put2byte(
dd850 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 67  &data[pbegin], g
dd860 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 6e  et2byte(&data[pn
dd870 65 78 74 5d 29 29 3b 0a 20 20 20 20 20 20 70 75  ext]));.      pu
dd880 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65  t2byte(&data[pbe
dd890 67 69 6e 2b 32 5d 2c 20 70 6e 65 78 74 2b 67 65  gin+2], pnext+ge
dd8a0 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65  t2byte(&data[pne
dd8b0 78 74 2b 32 5d 29 2d 70 62 65 67 69 6e 29 3b 0a  xt+2])-pbegin);.
dd8c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
dd8d0 20 61 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a   addr = pbegin;.
dd8e0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
dd8f0 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  If the cell cont
dd900 65 6e 74 20 61 72 65 61 20 62 65 67 69 6e 73 20  ent area begins 
dd910 77 69 74 68 20 61 20 66 72 65 65 62 6c 6f 63 6b  with a freeblock
dd920 2c 20 72 65 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a  , remove it. */.
dd930 20 20 69 66 28 20 64 61 74 61 5b 68 64 72 2b 31    if( data[hdr+1
dd940 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 35 5d 20 26  ]==data[hdr+5] &
dd950 26 20 64 61 74 61 5b 68 64 72 2b 32 5d 3d 3d 64  & data[hdr+2]==d
dd960 61 74 61 5b 68 64 72 2b 36 5d 20 29 7b 0a 20 20  ata[hdr+6] ){.  
dd970 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 20 20 70    int top;.    p
dd980 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65  begin = get2byte
dd990 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a  (&data[hdr+1]);.
dd9a0 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61      memcpy(&data
dd9b0 5b 68 64 72 2b 31 5d 2c 20 26 64 61 74 61 5b 70  [hdr+1], &data[p
dd9c0 62 65 67 69 6e 5d 2c 20 32 29 3b 0a 20 20 20 20  begin], 2);.    
dd9d0 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26  top = get2byte(&
dd9e0 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20  data[hdr+5]);.  
dd9f0 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
dda00 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 20 2b 20 67  [hdr+5], top + g
dda10 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62  et2byte(&data[pb
dda20 65 67 69 6e 2b 32 5d 29 29 3b 0a 20 20 7d 0a 7d  egin+2]));.  }.}
dda30 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 20 74  ../*.** Decode t
dda40 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 28 74  he flags byte (t
dda50 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66  he first byte of
dda60 20 74 68 65 20 68 65 61 64 65 72 29 20 66 6f 72   the header) for
dda70 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e 64 20 69   a page.** and i
dda80 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 6c 64 73  nitialize fields
dda90 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20   of the MemPage 
ddaa0 73 74 72 75 63 74 75 72 65 20 61 63 63 6f 72 64  structure accord
ddab0 69 6e 67 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ingly..*/.static
ddac0 20 76 6f 69 64 20 64 65 63 6f 64 65 46 6c 61 67   void decodeFlag
ddad0 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  s(MemPage *pPage
ddae0 2c 20 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b  , int flagByte){
ddaf0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
ddb00 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20  ;     /* A copy 
ddb10 6f 66 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f  of pPage->pBt */
ddb20 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
ddb30 65 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70  e->hdrOffset==(p
ddb40 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20  Page->pgno==1 ? 
ddb50 31 30 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73  100 : 0) );.  as
ddb60 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
ddb70 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
ddb80 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
ddb90 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d   pPage->intKey =
ddba0 20 28 66 6c 61 67 42 79 74 65 20 26 20 28 50 54   (flagByte & (PT
ddbb0 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41  F_INTKEY|PTF_LEA
ddbc0 46 44 41 54 41 29 29 21 3d 30 3b 0a 20 20 70 50  FDATA))!=0;.  pP
ddbd0 61 67 65 2d 3e 7a 65 72 6f 44 61 74 61 20 3d 20  age->zeroData = 
ddbe0 28 66 6c 61 67 42 79 74 65 20 26 20 50 54 46 5f  (flagByte & PTF_
ddbf0 5a 45 52 4f 44 41 54 41 29 21 3d 30 3b 0a 20 20  ZERODATA)!=0;.  
ddc00 70 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 66  pPage->leaf = (f
ddc10 6c 61 67 42 79 74 65 20 26 20 50 54 46 5f 4c 45  lagByte & PTF_LE
ddc20 41 46 29 21 3d 30 3b 0a 20 20 70 50 61 67 65 2d  AF)!=0;.  pPage-
ddc30 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20  >childPtrSize = 
ddc40 34 2a 28 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d  4*(pPage->leaf==
ddc50 30 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67  0);.  pBt = pPag
ddc60 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66 6c  e->pBt;.  if( fl
ddc70 61 67 42 79 74 65 20 26 20 50 54 46 5f 4c 45 41  agByte & PTF_LEA
ddc80 46 44 41 54 41 20 29 7b 0a 20 20 20 20 70 50 61  FDATA ){.    pPa
ddc90 67 65 2d 3e 6c 65 61 66 44 61 74 61 20 3d 20 31  ge->leafData = 1
ddca0 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78  ;.    pPage->max
ddcb0 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78  Local = pBt->max
ddcc0 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d  Leaf;.    pPage-
ddcd0 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d  >minLocal = pBt-
ddce0 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73  >minLeaf;.  }els
ddcf0 65 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6c 65  e{.    pPage->le
ddd00 61 66 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20  afData = 0;.    
ddd10 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20  pPage->maxLocal 
ddd20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b  = pBt->maxLocal;
ddd30 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c  .    pPage->minL
ddd40 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c  ocal = pBt->minL
ddd50 6f 63 61 6c 3b 0a 20 20 7d 0a 20 20 70 50 61 67  ocal;.  }.  pPag
ddd60 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 21 28 70  e->hasData = !(p
ddd70 50 61 67 65 2d 3e 7a 65 72 6f 44 61 74 61 20 7c  Page->zeroData |
ddd80 7c 20 28 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  | (!pPage->leaf 
ddd90 26 26 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61  && pPage->leafDa
ddda0 74 61 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ta));.}../*.** I
dddb0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75  nitialize the au
dddc0 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74  xiliary informat
dddd0 69 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62  ion for a disk b
ddde0 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lock..**.** The 
dddf0 70 50 61 72 65 6e 74 20 70 61 72 61 6d 65 74 65  pParent paramete
dde00 72 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e  r must be a poin
dde10 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 50 61  ter to the MemPa
dde20 67 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74  ge which.** is t
dde30 68 65 20 70 61 72 65 6e 74 20 6f 66 20 74 68 65  he parent of the
dde40 20 70 61 67 65 20 62 65 69 6e 67 20 69 6e 69 74   page being init
dde50 69 61 6c 69 7a 65 64 2e 20 20 54 68 65 20 72 6f  ialized.  The ro
dde60 6f 74 20 6f 66 20 61 0a 2a 2a 20 42 54 72 65 65  ot of a.** BTree
dde70 20 68 61 73 20 6e 6f 20 70 61 72 65 6e 74 20 61   has no parent a
dde80 6e 64 20 73 6f 20 66 6f 72 20 74 68 61 74 20 70  nd so for that p
dde90 61 67 65 2c 20 70 50 61 72 65 6e 74 3d 3d 4e 55  age, pParent==NU
ddea0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  LL..**.** Return
ddeb0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
ddec0 63 63 65 73 73 2e 20 20 49 66 20 77 65 20 73 65  ccess.  If we se
dded0 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
ddee0 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74  does.** not cont
ddef0 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65  ain a well-forme
ddf00 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c  d database page,
ddf10 20 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a 2a   then return .**
ddf20 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e   SQLITE_CORRUPT.
ddf30 20 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72 65    Note that a re
ddf40 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f 4f  turn of SQLITE_O
ddf50 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67 75  K does not.** gu
ddf60 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65  arantee that the
ddf70 20 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66 6f   page is well-fo
ddf80 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20 73  rmed.  It only s
ddf90 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65 20  hows that.** we 
ddfa0 66 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63 74  failed to detect
ddfb0 20 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 2e   any corruption.
ddfc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
ddfd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
ddfe0 72 65 65 49 6e 69 74 50 61 67 65 28 0a 20 20 4d  reeInitPage(.  M
ddff0 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20  emPage *pPage,  
de000 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
de010 65 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69  e to be initiali
de020 7a 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  zed */.  MemPage
de030 20 2a 70 50 61 72 65 6e 74 20 20 20 20 20 20 20   *pParent       
de040 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 2e 20 20  /* The parent.  
de050 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  Might be NULL */
de060 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20  .){.  int pc;   
de070 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
de080 65 73 73 20 6f 66 20 61 20 66 72 65 65 62 6c 6f  ess of a freeblo
de090 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67 65 2d  ck within pPage-
de0a0 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e  >aData[] */.  in
de0b0 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20  t hdr;          
de0c0 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 62 65   /* Offset to be
de0d0 67 69 6e 6e 69 6e 67 20 6f 66 20 70 61 67 65 20  ginning of page 
de0e0 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 38 20 2a  header */.  u8 *
de0f0 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f  data;          /
de100 2a 20 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65  * Equal to pPage
de110 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 42 74 53  ->aData */.  BtS
de120 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20  hared *pBt;     
de130 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62     /* The main b
de140 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
de150 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69  /.  int usableSi
de160 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74  ze;    /* Amount
de170 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65   of usable space
de180 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f   on each page */
de190 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65  .  int cellOffse
de1a0 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20  t;    /* Offset 
de1b0 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 61  from start of pa
de1c0 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c 6c  ge to first cell
de1d0 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   pointer */.  in
de1e0 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20  t nFree;        
de1f0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e   /* Number of un
de200 75 73 65 64 20 62 79 74 65 73 20 6f 6e 20 74 68  used bytes on th
de210 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  e page */.  int 
de220 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f  top;           /
de230 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20  * First byte of 
de240 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
de250 20 61 72 65 61 20 2a 2f 0a 0a 20 20 70 42 74 20   area */..  pBt 
de260 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
de270 61 73 73 65 72 74 28 20 70 42 74 21 3d 30 20 29  assert( pBt!=0 )
de280 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
de290 65 6e 74 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e  ent==0 || pParen
de2a0 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 20  t->pBt==pBt );. 
de2b0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
de2c0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
de2d0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
de2e0 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ert( pPage->pgno
de2f0 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  ==sqlite3PagerPa
de300 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e  genumber(pPage->
de310 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73  pDbPage) );.  as
de320 73 65 72 74 28 20 70 50 61 67 65 20 3d 3d 20 73  sert( pPage == s
de330 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78  qlite3PagerGetEx
de340 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61  tra(pPage->pDbPa
de350 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
de360 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 3d   pPage->aData ==
de370 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
de380 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50  Data(pPage->pDbP
de390 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 70 50  age) );.  if( pP
de3a0 61 67 65 2d 3e 70 50 61 72 65 6e 74 21 3d 70 50  age->pParent!=pP
de3b0 61 72 65 6e 74 20 26 26 20 28 70 50 61 67 65 2d  arent && (pPage-
de3c0 3e 70 50 61 72 65 6e 74 21 3d 30 20 7c 7c 20 70  >pParent!=0 || p
de3d0 50 61 67 65 2d 3e 69 73 49 6e 69 74 29 20 29 7b  Page->isInit) ){
de3e0 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65  .    /* The pare
de3f0 6e 74 20 70 61 67 65 20 73 68 6f 75 6c 64 20 6e  nt page should n
de400 65 76 65 72 20 63 68 61 6e 67 65 20 75 6e 6c 65  ever change unle
de410 73 73 20 74 68 65 20 66 69 6c 65 20 69 73 20 63  ss the file is c
de420 6f 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 72 65  orrupt */.    re
de430 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
de440 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20  UPT_BKPT;.  }.  
de450 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  if( pPage->isIni
de460 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  t ) return SQLIT
de470 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67  E_OK;.  if( pPag
de480 65 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 26 26  e->pParent==0 &&
de490 20 70 50 61 72 65 6e 74 21 3d 30 20 29 7b 0a 20   pParent!=0 ){. 
de4a0 20 20 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e     pPage->pParen
de4b0 74 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 20  t = pParent;.   
de4c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
de4d0 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67  (pParent->pDbPag
de4e0 65 29 3b 0a 20 20 7d 0a 20 20 68 64 72 20 3d 20  e);.  }.  hdr = 
de4f0 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
de500 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65  ;.  data = pPage
de510 2d 3e 61 44 61 74 61 3b 0a 20 20 64 65 63 6f 64  ->aData;.  decod
de520 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 64 61  eFlags(pPage, da
de530 74 61 5b 68 64 72 5d 29 3b 0a 20 20 70 50 61 67  ta[hdr]);.  pPag
de540 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30  e->nOverflow = 0
de550 3b 0a 20 20 70 50 61 67 65 2d 3e 69 64 78 53 68  ;.  pPage->idxSh
de560 69 66 74 20 3d 20 30 3b 0a 20 20 75 73 61 62 6c  ift = 0;.  usabl
de570 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61  eSize = pBt->usa
de580 62 6c 65 53 69 7a 65 3b 0a 20 20 70 50 61 67 65  bleSize;.  pPage
de590 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 63  ->cellOffset = c
de5a0 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 64 72 20  ellOffset = hdr 
de5b0 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e  + 12 - 4*pPage->
de5c0 6c 65 61 66 3b 0a 20 20 74 6f 70 20 3d 20 67 65  leaf;.  top = ge
de5d0 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
de5e0 2b 35 5d 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e  +5]);.  pPage->n
de5f0 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28  Cell = get2byte(
de600 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20  &data[hdr+3]);. 
de610 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c   if( pPage->nCel
de620 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 29  l>MX_CELL(pBt) )
de630 7b 0a 20 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79  {.    /* To many
de640 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e   cells for a sin
de650 67 6c 65 20 70 61 67 65 2e 20 20 54 68 65 20 70  gle page.  The p
de660 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72  age must be corr
de670 75 70 74 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  upt */.    retur
de680 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
de690 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28  _BKPT;.  }.  if(
de6a0 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30   pPage->nCell==0
de6b0 20 26 26 20 70 50 61 72 65 6e 74 21 3d 30 20 26   && pParent!=0 &
de6c0 26 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21  & pParent->pgno!
de6d0 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c  =1 ){.    /* All
de6e0 20 70 61 67 65 73 20 6d 75 73 74 20 68 61 76 65   pages must have
de6f0 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 63 65   at least one ce
de700 6c 6c 2c 20 65 78 63 65 70 74 20 66 6f 72 20 72  ll, except for r
de710 6f 6f 74 20 70 61 67 65 73 20 2a 2f 0a 20 20 20  oot pages */.   
de720 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
de730 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
de740 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74  ..  /* Compute t
de750 68 65 20 74 6f 74 61 6c 20 66 72 65 65 20 73 70  he total free sp
de760 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65 20  ace on the page 
de770 2a 2f 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79  */.  pc = get2by
de780 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29  te(&data[hdr+1])
de790 3b 0a 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61  ;.  nFree = data
de7a0 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 20 2d 20  [hdr+7] + top - 
de7b0 28 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a  (cellOffset + 2*
de7c0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20  pPage->nCell);. 
de7d0 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a   while( pc>0 ){.
de7e0 20 20 20 20 69 6e 74 20 6e 65 78 74 2c 20 73 69      int next, si
de7f0 7a 65 3b 0a 20 20 20 20 69 66 28 20 70 63 3e 75  ze;.    if( pc>u
de800 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20  sableSize-4 ){. 
de810 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f       /* Free blo
de820 63 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70 61  ck is off the pa
de830 67 65 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  ge */.      retu
de840 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
de850 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20  T_BKPT; .    }. 
de860 20 20 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79     next = get2by
de870 74 65 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20  te(&data[pc]);. 
de880 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79     size = get2by
de890 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b  te(&data[pc+2]);
de8a0 0a 20 20 20 20 69 66 28 20 6e 65 78 74 3e 30 20  .    if( next>0 
de8b0 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73 69 7a 65  && next<=pc+size
de8c0 2b 33 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46  +3 ){.      /* F
de8d0 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73 74 20  ree blocks must 
de8e0 62 65 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20  be in accending 
de8f0 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 72  order */.      r
de900 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
de910 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20  RUPT_BKPT; .    
de920 7d 0a 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73  }.    nFree += s
de930 69 7a 65 3b 0a 20 20 20 20 70 63 20 3d 20 6e 65  ize;.    pc = ne
de940 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d  xt;.  }.  pPage-
de950 3e 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 3b 0a  >nFree = nFree;.
de960 20 20 69 66 28 20 6e 46 72 65 65 3e 3d 75 73 61    if( nFree>=usa
de970 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 2f  bleSize ){.    /
de980 2a 20 46 72 65 65 20 73 70 61 63 65 20 63 61 6e  * Free space can
de990 6e 6f 74 20 65 78 63 65 65 64 20 74 6f 74 61 6c  not exceed total
de9a0 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20   page size */.  
de9b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
de9c0 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20  CORRUPT_BKPT; . 
de9d0 20 7d 0a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49   }..  pPage->isI
de9e0 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65 74 75 72  nit = 1;.  retur
de9f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
dea00 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72  /*.** Set up a r
dea10 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 74 20  aw page so that 
dea20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20  it looks like a 
dea30 64 61 74 61 62 61 73 65 20 70 61 67 65 20 68 6f  database page ho
dea40 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72  lding.** no entr
dea50 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ies..*/.static v
dea60 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d  oid zeroPage(Mem
dea70 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74  Page *pPage, int
dea80 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67   flags){.  unsig
dea90 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d  ned char *data =
deaa0 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20   pPage->aData;. 
deab0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
deac0 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69   pPage->pBt;.  i
dead0 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  nt hdr = pPage->
deae0 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74  hdrOffset;.  int
deaf0 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72   first;..  asser
deb00 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  t( sqlite3PagerP
deb10 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d  agenumber(pPage-
deb20 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65  >pDbPage)==pPage
deb30 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65  ->pgno );.  asse
deb40 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
deb50 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e  GetExtra(pPage->
deb60 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69  pDbPage) == (voi
deb70 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73  d*)pPage );.  as
deb80 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
deb90 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d  erGetData(pPage-
deba0 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74  >pDbPage) == dat
debb0 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  a );.  assert( s
debc0 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
debd0 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
debe0 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
debf0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
dec00 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
dec10 78 29 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26  x) );.  memset(&
dec20 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42  data[hdr], 0, pB
dec30 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
dec40 68 64 72 29 3b 0a 20 20 64 61 74 61 5b 68 64 72  hdr);.  data[hdr
dec50 5d 20 3d 20 66 6c 61 67 73 3b 0a 20 20 66 69 72  ] = flags;.  fir
dec60 73 74 20 3d 20 68 64 72 20 2b 20 38 20 2b 20 34  st = hdr + 8 + 4
dec70 2a 28 28 66 6c 61 67 73 26 50 54 46 5f 4c 45 41  *((flags&PTF_LEA
dec80 46 29 3d 3d 30 29 3b 0a 20 20 6d 65 6d 73 65 74  F)==0);.  memset
dec90 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30  (&data[hdr+1], 0
deca0 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72  , 4);.  data[hdr
decb0 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62  +7] = 0;.  put2b
decc0 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
decd0 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  , pBt->usableSiz
dece0 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72  e);.  pPage->nFr
decf0 65 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ee = pBt->usable
ded00 53 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20  Size - first;.  
ded10 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67  decodeFlags(pPag
ded20 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61  e, flags);.  pPa
ded30 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20  ge->hdrOffset = 
ded40 68 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65  hdr;.  pPage->ce
ded50 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74  llOffset = first
ded60 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  ;.  pPage->nOver
ded70 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 70 50 61 67  flow = 0;.  pPag
ded80 65 2d 3e 69 64 78 53 68 69 66 74 20 3d 20 30 3b  e->idxShift = 0;
ded90 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  .  pPage->nCell 
deda0 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 69 73  = 0;.  pPage->is
dedb0 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  Init = 1;.}../*.
dedc0 2a 2a 20 47 65 74 20 61 20 70 61 67 65 20 66 72  ** Get a page fr
dedd0 6f 6d 20 74 68 65 20 70 61 67 65 72 2e 20 20 49  om the pager.  I
dede0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65  nitialize the Me
dedf0 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a  mPage.pBt and.**
dee00 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65   MemPage.aData e
dee10 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65  lements if neede
dee20 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
dee30 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61 67 20 69  noContent flag i
dee40 73 20 73 65 74 2c 20 69 74 20 6d 65 61 6e 73 20  s set, it means 
dee50 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63  that we do not c
dee60 61 72 65 20 61 62 6f 75 74 0a 2a 2a 20 74 68 65  are about.** the
dee70 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
dee80 70 61 67 65 20 61 74 20 74 68 69 73 20 74 69 6d  page at this tim
dee90 65 2e 20 20 53 6f 20 64 6f 20 6e 6f 74 20 67 6f  e.  So do not go
deea0 20 74 6f 20 74 68 65 20 64 69 73 6b 0a 2a 2a 20   to the disk.** 
deeb0 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e  to fetch the con
deec0 74 65 6e 74 2e 20 20 4a 75 73 74 20 66 69 6c 6c  tent.  Just fill
deed0 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   in the content 
deee0 77 69 74 68 20 7a 65 72 6f 73 20 66 6f 72 20 6e  with zeros for n
deef0 6f 77 2e 0a 2a 2a 20 49 66 20 69 6e 20 74 68 65  ow..** If in the
def00 20 66 75 74 75 72 65 20 77 65 20 63 61 6c 6c 20   future we call 
def10 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
def20 65 28 29 20 6f 6e 20 74 68 69 73 20 70 61 67 65  e() on this page
def30 2c 20 74 68 61 74 0a 2a 2a 20 6d 65 61 6e 73 20  , that.** means 
def40 77 65 20 68 61 76 65 20 73 74 61 72 74 65 64 20  we have started 
def50 74 6f 20 62 65 20 63 6f 6e 63 65 72 6e 65 64 20  to be concerned 
def60 61 62 6f 75 74 20 63 6f 6e 74 65 6e 74 20 61 6e  about content an
def70 64 20 74 68 65 20 64 69 73 6b 0a 2a 2a 20 72 65  d the disk.** re
def80 61 64 20 73 68 6f 75 6c 64 20 6f 63 63 75 72 20  ad should occur 
def90 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a  at that point..*
defa0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
defb0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
defc0 65 47 65 74 50 61 67 65 28 0a 20 20 42 74 53 68  eGetPage(.  BtSh
defd0 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20  ared *pBt,      
defe0 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f   /* The btree */
deff0 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20  .  Pgno pgno,   
df000 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
df010 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f  r of the page to
df020 20 66 65 74 63 68 20 2a 2f 0a 20 20 4d 65 6d 50   fetch */.  MemP
df030 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20  age **ppPage,   
df040 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70   /* Return the p
df050 61 67 65 20 69 6e 20 74 68 69 73 20 70 61 72 61  age in this para
df060 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  meter */.  int n
df070 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 20  oContent        
df080 2f 2a 20 44 6f 20 6e 6f 74 20 6c 6f 61 64 20 70  /* Do not load p
df090 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 66 20 74  age content if t
df0a0 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  rue */.){.  int 
df0b0 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  rc;.  MemPage *p
df0c0 50 61 67 65 3b 0a 20 20 44 62 50 61 67 65 20 2a  Page;.  DbPage *
df0d0 70 44 62 50 61 67 65 3b 0a 0a 20 20 61 73 73 65  pDbPage;..  asse
df0e0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
df0f0 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
df100 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  x) );.  rc = sql
df110 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
df120 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67  (pBt->pPager, pg
df130 6e 6f 2c 20 28 44 62 50 61 67 65 2a 2a 29 26 70  no, (DbPage**)&p
df140 44 62 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e  DbPage, noConten
df150 74 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  t);.  if( rc ) r
df160 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 50 61 67  eturn rc;.  pPag
df170 65 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73  e = (MemPage *)s
df180 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78  qlite3PagerGetEx
df190 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20  tra(pDbPage);.  
df1a0 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73  pPage->aData = s
df1b0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
df1c0 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70  ta(pDbPage);.  p
df1d0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20  Page->pDbPage = 
df1e0 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65  pDbPage;.  pPage
df1f0 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70  ->pBt = pBt;.  p
df200 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e  Page->pgno = pgn
df210 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f  o;.  pPage->hdrO
df220 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70  ffset = pPage->p
df230 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30  gno==1 ? 100 : 0
df240 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50  ;.  *ppPage = pP
df250 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  age;.  return SQ
df260 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
df270 2a 20 47 65 74 20 61 20 70 61 67 65 20 66 72 6f  * Get a page fro
df280 6d 20 74 68 65 20 70 61 67 65 72 20 61 6e 64 20  m the pager and 
df290 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2e 20 20  initialize it.  
df2a0 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
df2b0 69 73 20 6a 75 73 74 20 61 20 63 6f 6e 76 65 6e  is just a conven
df2c0 69 65 6e 63 65 20 77 72 61 70 70 65 72 20 61 72  ience wrapper ar
df2d0 6f 75 6e 64 20 73 65 70 61 72 61 74 65 20 63 61  ound separate ca
df2e0 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  lls to.** sqlite
df2f0 33 42 74 72 65 65 47 65 74 50 61 67 65 28 29 20  3BtreeGetPage() 
df300 61 6e 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  and sqlite3Btree
df310 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a 2f 0a 73  InitPage()..*/.s
df320 74 61 74 69 63 20 69 6e 74 20 67 65 74 41 6e 64  tatic int getAnd
df330 49 6e 69 74 50 61 67 65 28 0a 20 20 42 74 53 68  InitPage(.  BtSh
df340 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20  ared *pBt,      
df350 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
df360 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67  ase file */.  Pg
df370 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20  no pgno,        
df380 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
df390 74 68 65 20 70 61 67 65 20 74 6f 20 67 65 74 20  the page to get 
df3a0 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70  */.  MemPage **p
df3b0 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69  pPage,    /* Wri
df3c0 74 65 20 74 68 65 20 70 61 67 65 20 70 6f 69 6e  te the page poin
df3d0 74 65 72 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65  ter here */.  Me
df3e0 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 20 20  mPage *pParent  
df3f0 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6f 66 20     /* Parent of 
df400 74 68 65 20 70 61 67 65 20 2a 2f 0a 29 7b 0a 20  the page */.){. 
df410 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
df420 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
df430 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
df440 29 20 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3d  ) );.  if( pgno=
df450 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
df460 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
df470 42 4b 50 54 3b 20 0a 20 20 7d 0a 20 20 72 63 20  BKPT; .  }.  rc 
df480 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
df490 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c  tPage(pBt, pgno,
df4a0 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69   ppPage, 0);.  i
df4b0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
df4c0 20 26 26 20 28 2a 70 70 50 61 67 65 29 2d 3e 69   && (*ppPage)->i
df4d0 73 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20  sInit==0 ){.    
df4e0 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
df4f0 65 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61 67  eInitPage(*ppPag
df500 65 2c 20 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d  e, pParent);.  }
df510 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
df520 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
df530 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20   MemPage.  This 
df540 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64  should be called
df550 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70   once for each p
df560 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  rior.** call to 
df570 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
df580 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
df590 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28  oid releasePage(
df5a0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b  MemPage *pPage){
df5b0 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a  .  if( pPage ){.
df5c0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
df5d0 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20  e->aData );.    
df5e0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
df5f0 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Bt );.    assert
df600 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  ( sqlite3PagerGe
df610 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44  tExtra(pPage->pD
df620 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a  bPage) == (void*
df630 29 70 50 61 67 65 20 29 3b 0a 20 20 20 20 61 73  )pPage );.    as
df640 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
df650 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d  erGetData(pPage-
df660 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65  >pDbPage)==pPage
df670 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61  ->aData );.    a
df680 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
df690 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
df6a0 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
df6b0 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
df6c0 55 6e 72 65 66 28 70 50 61 67 65 2d 3e 70 44 62  Unref(pPage->pDb
df6d0 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  Page);.  }.}../*
df6e0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
df6f0 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
df700 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
df710 75 6e 74 20 66 6f 72 20 61 20 70 61 67 65 0a 2a  unt for a page.*
df720 2a 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e 20  * reaches zero. 
df730 20 57 65 20 6e 65 65 64 20 74 6f 20 75 6e 72 65   We need to unre
df740 66 20 74 68 65 20 70 50 61 72 65 6e 74 20 70 6f  f the pParent po
df750 69 6e 74 65 72 20 77 68 65 6e 20 74 68 61 74 0a  inter when that.
df760 2a 2a 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a 73  ** happens..*/.s
df770 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 44  tatic void pageD
df780 65 73 74 72 75 63 74 6f 72 28 44 62 50 61 67 65  estructor(DbPage
df790 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 70 61 67   *pData, int pag
df7a0 65 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 50 61 67  eSize){.  MemPag
df7b0 65 20 2a 70 50 61 67 65 3b 0a 20 20 61 73 73 65  e *pPage;.  asse
df7c0 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20  rt( (pageSize & 
df7d0 37 29 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67 65  7)==0 );.  pPage
df7e0 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71   = (MemPage *)sq
df7f0 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
df800 72 61 28 70 44 61 74 61 29 3b 0a 20 20 61 73 73  ra(pData);.  ass
df810 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e  ert( pPage->isIn
df820 69 74 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  it==0 || sqlite3
df830 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
df840 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
df850 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70  ;.  if( pPage->p
df860 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 4d 65  Parent ){.    Me
df870 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 20 3d  mPage *pParent =
df880 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 3b   pPage->pParent;
df890 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
df8a0 72 65 6e 74 2d 3e 70 42 74 3d 3d 70 50 61 67 65  rent->pBt==pPage
df8b0 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 70 50 61  ->pBt );.    pPa
df8c0 67 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 30 3b  ge->pParent = 0;
df8d0 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
df8e0 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 20  (pParent);.  }. 
df8f0 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
df900 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72   0;.}../*.** Dur
df910 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20  ing a rollback, 
df920 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 72  when the pager r
df930 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 69  eloads informati
df940 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68  on into the cach
df950 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65  e.** so that the
df960 20 63 61 63 68 65 20 69 73 20 72 65 73 74 6f 72   cache is restor
df970 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e  ed to its origin
df980 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 65 20  al state at the 
df990 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20  start of.** the 
df9a0 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72  transaction, for
df9b0 20 65 61 63 68 20 70 61 67 65 20 72 65 73 74 6f   each page resto
df9c0 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65  red this routine
df9d0 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a   is called..**.*
df9e0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e  * This routine n
df9f0 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 74 68  eeds to reset th
dfa00 65 20 65 78 74 72 61 20 64 61 74 61 20 73 65 63  e extra data sec
dfa10 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64 20  tion at the end 
dfa20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74  of the.** page t
dfa30 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65  o agree with the
dfa40 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e 0a   restored data..
dfa50 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
dfa60 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 67 65  ageReinit(DbPage
dfa70 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 70 61 67   *pData, int pag
dfa80 65 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 50 61 67  eSize){.  MemPag
dfa90 65 20 2a 70 50 61 67 65 3b 0a 20 20 61 73 73 65  e *pPage;.  asse
dfaa0 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20  rt( (pageSize & 
dfab0 37 29 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67 65  7)==0 );.  pPage
dfac0 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71   = (MemPage *)sq
dfad0 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
dfae0 72 61 28 70 44 61 74 61 29 3b 0a 20 20 69 66 28  ra(pData);.  if(
dfaf0 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
dfb00 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  {.    assert( sq
dfb10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
dfb20 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
dfb30 65 78 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65  ex) );.    pPage
dfb40 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20  ->isInit = 0;.  
dfb50 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e    sqlite3BtreeIn
dfb60 69 74 50 61 67 65 28 70 50 61 67 65 2c 20 70 50  itPage(pPage, pP
dfb70 61 67 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20  age->pParent);. 
dfb80 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e   }.}../*.** Open
dfb90 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
dfba0 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61  ..** .** zFilena
dfbb0 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  me is the name o
dfbc0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
dfbd0 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61  ile.  If zFilena
dfbe0 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20  me is NULL.** a 
dfbf0 6e 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74  new database wit
dfc00 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20  h a random name 
dfc10 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69  is created.  Thi
dfc20 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64  s randomly named
dfc30 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
dfc40 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65  e will be delete
dfc50 64 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74  d when sqlite3Bt
dfc60 72 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61  reeClose() is ca
dfc70 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c  lled..** If zFil
dfc80 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72  ename is ":memor
dfc90 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d  y:" then an in-m
dfca0 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69  emory database i
dfcb0 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61  s created.** tha
dfcc0 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
dfcd0 6c 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65  ly destroyed whe
dfce0 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a  n it is closed..
dfcf0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
dfd00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
dfd10 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20  eeOpen(.  const 
dfd20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
dfd30 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
dfd40 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67   file containing
dfd50 20 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62   the BTree datab
dfd60 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ase */.  sqlite3
dfd70 20 2a 70 53 71 6c 69 74 65 2c 20 20 20 20 20 20   *pSqlite,      
dfd80 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64   /* Associated d
dfd90 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
dfda0 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74  /.  Btree **ppBt
dfdb0 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  ree,        /* P
dfdc0 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74  ointer to new Bt
dfdd0 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74  ree object writt
dfde0 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  en here */.  int
dfdf0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
dfe00 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20       /* Options 
dfe10 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67  */.  int vfsFlag
dfe20 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s            /* 
dfe30 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  Flags passed thr
dfe40 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f  ough to sqlite3_
dfe50 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29  vfs.xOpen() */.)
dfe60 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
dfe70 2a 70 56 66 73 3b 20 20 20 20 20 20 2f 2a 20 54  *pVfs;      /* T
dfe80 68 65 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f  he VFS to use fo
dfe90 72 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a  r this btree */.
dfea0 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
dfeb0 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 53 68 61  = 0;      /* Sha
dfec0 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65  red part of btre
dfed0 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
dfee0 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20   Btree *p;      
dfef0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
dff00 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  le to return */.
dff10 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
dff20 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 52 65 73  E_OK;.  int nRes
dff30 65 72 76 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64  erve;.  unsigned
dff40 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b   char zDbHeader[
dff50 31 30 30 5d 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  100];..  /* Set 
dff60 74 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d  the variable isM
dff70 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72  emdb to true for
dff80 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
dff90 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a  tabase, or .  **
dffa0 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c   false for a fil
dffb0 65 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65  e-based database
dffc0 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73  . This symbol is
dffd0 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69   only required i
dffe0 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66  f.  ** either of
dfff0 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61   the shared-data
e0000 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66   or autovacuum f
e0010 65 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70  eatures are comp
e0020 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20  iled .  ** into 
e0030 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a  the library..  *
e0040 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
e0050 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
e0060 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66  D_CACHE) || !def
e0070 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
e0080 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23  _AUTOVACUUM).  #
e0090 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
e00a0 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63  T_MEMORYDB.    c
e00b0 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62  onst int isMemdb
e00c0 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20   = 0;.  #else.  
e00d0 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65    const int isMe
e00e0 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20  mdb = zFilename 
e00f0 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65  && !strcmp(zFile
e0100 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22  name, ":memory:"
e0110 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64  );.  #endif.#end
e0120 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 53  if..  assert( pS
e0130 71 6c 69 74 65 21 3d 30 20 29 3b 0a 20 20 61 73  qlite!=0 );.  as
e0140 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
e0150 74 65 78 5f 68 65 6c 64 28 70 53 71 6c 69 74 65  tex_held(pSqlite
e0160 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
e0170 56 66 73 20 3d 20 70 53 71 6c 69 74 65 2d 3e 70  Vfs = pSqlite->p
e0180 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74  Vfs;.  p = sqlit
e0190 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a  e3MallocZero(siz
e01a0 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69  eof(Btree));.  i
e01b0 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74  f( !p ){.    ret
e01c0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
e01d0 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61  ;.  }.  p->inTra
e01e0 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b  ns = TRANS_NONE;
e01f0 0a 20 20 70 2d 3e 70 53 71 6c 69 74 65 20 3d 20  .  p->pSqlite = 
e0200 70 53 71 6c 69 74 65 3b 0a 0a 23 69 66 20 21 64  pSqlite;..#if !d
e0210 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
e0220 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29  IT_SHARED_CACHE)
e0230 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
e0240 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29  ITE_OMIT_DISKIO)
e0250 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  /*.  ** If th
e0260 69 73 20 42 74 72 65 65 20 69 73 20 61 20 63 61  is Btree is a ca
e0270 6e 64 69 64 61 74 65 20 66 6f 72 20 73 68 61 72  ndidate for shar
e0280 65 64 20 63 61 63 68 65 2c 20 74 72 79 20 74 6f  ed cache, try to
e0290 20 66 69 6e 64 20 61 6e 0a 20 20 2a 2a 20 65 78   find an.  ** ex
e02a0 69 73 74 69 6e 67 20 42 74 53 68 61 72 65 64 20  isting BtShared 
e02b0 6f 62 6a 65 63 74 20 74 68 61 74 20 77 65 20 63  object that we c
e02c0 61 6e 20 73 68 61 72 65 20 77 69 74 68 0a 20 20  an share with.  
e02d0 2a 2f 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20  */.  if( (flags 
e02e0 26 20 42 54 52 45 45 5f 50 52 49 56 41 54 45 29  & BTREE_PRIVATE)
e02f0 3d 3d 30 0a 20 20 20 26 26 20 69 73 4d 65 6d 64  ==0.   && isMemd
e0300 62 3d 3d 30 0a 20 20 20 26 26 20 28 70 53 71 6c  b==0.   && (pSql
e0310 69 74 65 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  ite->flags & SQL
e0320 49 54 45 5f 56 74 61 62 29 3d 3d 30 0a 20 20 20  ITE_Vtab)==0.   
e0330 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20  && zFilename && 
e0340 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 0a 20 20 29  zFilename[0].  )
e0350 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
e0360 33 53 68 61 72 65 64 43 61 63 68 65 45 6e 61 62  3SharedCacheEnab
e0370 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 63 68 61  led ){.      cha
e0380 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65  r *zFullPathname
e0390 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
e03a0 65 33 5f 6d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e  e3_malloc(pVfs->
e03b0 6d 78 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20  mxPathname);.   
e03c0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
e03d0 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20   *mutexShared;. 
e03e0 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65       p->sharable
e03f0 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20   = 1;.      if( 
e0400 70 53 71 6c 69 74 65 20 29 7b 0a 20 20 20 20 20  pSqlite ){.     
e0410 20 20 20 70 53 71 6c 69 74 65 2d 3e 66 6c 61 67     pSqlite->flag
e0420 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68 61 72  s |= SQLITE_Shar
e0430 65 64 43 61 63 68 65 3b 0a 20 20 20 20 20 20 7d  edCache;.      }
e0440 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c  .      if( !zFul
e0450 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20  lPathname ){.   
e0460 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
e0470 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65  e(p);.        re
e0480 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
e0490 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
e04a0 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61   sqlite3OsFullPa
e04b0 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69  thname(pVfs, zFi
e04c0 6c 65 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74  lename, zFullPat
e04d0 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75  hname);.      mu
e04e0 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69  texShared = sqli
e04f0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
e0500 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
e0510 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20  TIC_MASTER);.   
e0520 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
e0530 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72  _enter(mutexShar
e0540 65 64 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  ed);.      for(p
e0550 42 74 3d 73 71 6c 69 74 65 33 53 68 61 72 65 64  Bt=sqlite3Shared
e0560 43 61 63 68 65 4c 69 73 74 3b 20 70 42 74 3b 20  CacheList; pBt; 
e0570 70 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b  pBt=pBt->pNext){
e0580 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
e0590 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a   pBt->nRef>0 );.
e05a0 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73          if( 0==s
e05b0 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e  trcmp(zFullPathn
e05c0 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65  ame, sqlite3Page
e05d0 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70  rFilename(pBt->p
e05e0 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20  Pager)).        
e05f0 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69           && sqli
e0600 74 65 33 50 61 67 65 72 56 66 73 28 70 42 74 2d  te3PagerVfs(pBt-
e0610 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29  >pPager)==pVfs )
e0620 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  {.          p->p
e0630 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20  Bt = pBt;.      
e0640 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b      pBt->nRef++;
e0650 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
e0660 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
e0670 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
e0680 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
e0690 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20  texShared);.    
e06a0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
e06b0 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
e06c0 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
e06d0 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73  TE_DEBUG.    els
e06e0 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64  e{.      /* In d
e06f0 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61  ebug mode, we ma
e0700 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e  rk all persisten
e0710 74 20 64 61 74 61 62 61 73 65 73 20 61 73 20 73  t databases as s
e0720 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  harable.      **
e0730 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20   even when they 
e0740 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65  are not.  This e
e0750 78 65 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63  xercises the loc
e0760 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20  king code and.  
e0770 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72      ** gives mor
e0780 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f  e opportunity fo
e0790 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 74 65  r asserts(sqlite
e07a0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a  3_mutex_held()).
e07b0 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65        ** stateme
e07c0 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b  nts to find lock
e07d0 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20  ing problems..  
e07e0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e      */.      p->
e07f0 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20  sharable = 1;.  
e0800 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23    }.#endif.  }.#
e0810 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d  endif.  if( pBt=
e0820 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20  =0 ){.    /*.   
e0830 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   ** The followin
e0840 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73  g asserts make s
e0850 75 72 65 20 74 68 61 74 20 73 74 72 75 63 74 75  ure that structu
e0860 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  res used by the 
e0870 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a  btree are.    **
e0880 20 74 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e   the right size.
e0890 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 75 61    This is to gua
e08a0 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20  rd against size 
e08b0 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 65 73  changes that res
e08c0 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20  ult.    ** when 
e08d0 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64  compiling on a d
e08e0 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 74 65  ifferent archite
e08f0 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  cture..    */.  
e0900 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
e0910 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65  (i64)==8 || size
e0920 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20  of(i64)==4 );.  
e0930 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
e0940 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65  (u64)==8 || size
e0950 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20  of(u64)==4 );.  
e0960 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
e0970 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20  (u32)==4 );.    
e0980 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75  assert( sizeof(u
e0990 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73  16)==2 );.    as
e09a0 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e  sert( sizeof(Pgn
e09b0 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20  o)==4 );.  .    
e09c0 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  pBt = sqlite3Mal
e09d0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
e09e0 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28  *pBt) );.    if(
e09f0 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   pBt==0 ){.     
e0a00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
e0a10 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  EM;.      goto b
e0a20 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  tree_open_out;. 
e0a30 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
e0a40 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70  lite3PagerOpen(p
e0a50 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65  Vfs, &pBt->pPage
e0a60 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20  r, zFilename,.  
e0a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0a80 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53 49          EXTRA_SI
e0a90 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c  ZE, flags, vfsFl
e0aa0 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ags);.    if( rc
e0ab0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
e0ac0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
e0ad0 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65  3PagerReadFilehe
e0ae0 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72  ader(pBt->pPager
e0af0 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 65  ,sizeof(zDbHeade
e0b00 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a 20  r),zDbHeader);. 
e0b10 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
e0b20 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e0b30 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f      goto btree_o
e0b40 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
e0b50 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b     p->pBt = pBt;
e0b60 0a 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 50  .  .    sqlite3P
e0b70 61 67 65 72 53 65 74 44 65 73 74 72 75 63 74 6f  agerSetDestructo
e0b80 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70  r(pBt->pPager, p
e0b90 61 67 65 44 65 73 74 72 75 63 74 6f 72 29 3b 0a  ageDestructor);.
e0ba0 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
e0bb0 53 65 74 52 65 69 6e 69 74 65 72 28 70 42 74 2d  SetReiniter(pBt-
e0bc0 3e 70 50 61 67 65 72 2c 20 70 61 67 65 52 65 69  >pPager, pageRei
e0bd0 6e 69 74 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70  nit);.    pBt->p
e0be0 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20  Cursor = 0;.    
e0bf0 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b  pBt->pPage1 = 0;
e0c00 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e  .    pBt->readOn
e0c10 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ly = sqlite3Page
e0c20 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d  rIsreadonly(pBt-
e0c30 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42  >pPager);.    pB
e0c40 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65  t->pageSize = ge
e0c50 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65  t2byte(&zDbHeade
e0c60 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20  r[16]);.    if( 
e0c70 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31  pBt->pageSize<51
e0c80 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69  2 || pBt->pageSi
e0c90 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ze>SQLITE_MAX_PA
e0ca0 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20  GE_SIZE.        
e0cb0 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53   || ((pBt->pageS
e0cc0 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65  ize-1)&pBt->page
e0cd0 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Size)!=0 ){.    
e0ce0 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
e0cf0 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
e0d00 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
e0d10 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ze(pBt->pPager, 
e0d20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b  &pBt->pageSize);
e0d30 0a 20 20 20 20 20 20 70 42 74 2d 3e 6d 61 78 45  .      pBt->maxE
e0d40 6d 62 65 64 46 72 61 63 20 3d 20 36 34 3b 20 20  mbedFrac = 64;  
e0d50 20 2f 2a 20 32 35 25 20 2a 2f 0a 20 20 20 20 20   /* 25% */.     
e0d60 20 70 42 74 2d 3e 6d 69 6e 45 6d 62 65 64 46 72   pBt->minEmbedFr
e0d70 61 63 20 3d 20 33 32 3b 20 20 20 2f 2a 20 31 32  ac = 32;   /* 12
e0d80 2e 35 25 20 2a 2f 0a 20 20 20 20 20 20 70 42 74  .5% */.      pBt
e0d90 2d 3e 6d 69 6e 4c 65 61 66 46 72 61 63 20 3d 20  ->minLeafFrac = 
e0da0 33 32 3b 20 20 20 20 2f 2a 20 31 32 2e 35 25 20  32;    /* 12.5% 
e0db0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
e0dc0 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
e0dd0 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  M.      /* If th
e0de0 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d  e magic name ":m
e0df0 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63 72 65  emory:" will cre
e0e00 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ate an in-memory
e0e10 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a   database, then.
e0e20 20 20 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 74        ** leave t
e0e30 68 65 20 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f  he autoVacuum mo
e0e40 64 65 20 61 74 20 30 20 28 64 6f 20 6e 6f 74 20  de at 0 (do not 
e0e50 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76  auto-vacuum), ev
e0e60 65 6e 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53  en if.      ** S
e0e70 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
e0e80 54 4f 56 41 43 55 55 4d 20 69 73 20 74 72 75 65  TOVACUUM is true
e0e90 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  . On the other h
e0ea0 61 6e 64 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a  and, if.      **
e0eb0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d   SQLITE_OMIT_MEM
e0ec0 4f 52 59 44 42 20 68 61 73 20 62 65 65 6e 20 64  ORYDB has been d
e0ed0 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 22 3a 6d  efined, then ":m
e0ee0 65 6d 6f 72 79 3a 22 20 69 73 20 6a 75 73 74 20  emory:" is just 
e0ef0 61 0a 20 20 20 20 20 20 2a 2a 20 72 65 67 75 6c  a.      ** regul
e0f00 61 72 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e  ar file-name. In
e0f10 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 61   this case the a
e0f20 75 74 6f 2d 76 61 63 75 75 6d 20 61 70 70 6c 69  uto-vacuum appli
e0f30 65 73 20 61 73 20 70 65 72 20 6e 6f 72 6d 61 6c  es as per normal
e0f40 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
e0f50 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26   if( zFilename &
e0f60 26 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20  & !isMemdb ){.  
e0f70 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56        pBt->autoV
e0f80 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f  acuum = (SQLITE_
e0f90 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
e0fa0 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20  UM ? 1 : 0);.   
e0fb0 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61       pBt->incrVa
e0fc0 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44  cuum = (SQLITE_D
e0fd0 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
e0fe0 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20  M==2 ? 1 : 0);. 
e0ff0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
e1000 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 30      nReserve = 0
e1010 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
e1020 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 7a 44     nReserve = zD
e1030 62 48 65 61 64 65 72 5b 32 30 5d 3b 0a 20 20 20  bHeader[20];.   
e1040 20 20 20 70 42 74 2d 3e 6d 61 78 45 6d 62 65 64     pBt->maxEmbed
e1050 46 72 61 63 20 3d 20 7a 44 62 48 65 61 64 65 72  Frac = zDbHeader
e1060 5b 32 31 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d  [21];.      pBt-
e1070 3e 6d 69 6e 45 6d 62 65 64 46 72 61 63 20 3d 20  >minEmbedFrac = 
e1080 7a 44 62 48 65 61 64 65 72 5b 32 32 5d 3b 0a 20  zDbHeader[22];. 
e1090 20 20 20 20 20 70 42 74 2d 3e 6d 69 6e 4c 65 61       pBt->minLea
e10a0 66 46 72 61 63 20 3d 20 7a 44 62 48 65 61 64 65  fFrac = zDbHeade
e10b0 72 5b 32 33 5d 3b 0a 20 20 20 20 20 20 70 42 74  r[23];.      pBt
e10c0 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20  ->pageSizeFixed 
e10d0 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 1;.#ifndef SQL
e10e0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
e10f0 55 55 4d 0a 20 20 20 20 20 20 70 42 74 2d 3e 61  UUM.      pBt->a
e1100 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74  utoVacuum = (get
e1110 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72  4byte(&zDbHeader
e1120 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29  [36 + 4*4])?1:0)
e1130 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63  ;.      pBt->inc
e1140 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62  rVacuum = (get4b
e1150 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33  yte(&zDbHeader[3
e1160 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a  6 + 7*4])?1:0);.
e1170 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20  #endif.    }.   
e1180 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
e1190 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65   = pBt->pageSize
e11a0 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 20   - nReserve;.   
e11b0 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e 70   assert( (pBt->p
e11c0 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20  ageSize & 7)==0 
e11d0 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c  );  /* 8-byte al
e11e0 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67 65 53  ignment of pageS
e11f0 69 7a 65 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ize */.    sqlit
e1200 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
e1210 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ze(pBt->pPager, 
e1220 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b  &pBt->pageSize);
e1230 0a 20 20 20 0a 23 69 66 20 21 64 65 66 69 6e 65  .   .#if !define
e1240 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  d(SQLITE_OMIT_SH
e1250 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 21  ARED_CACHE) && !
e1260 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
e1270 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 20 20  MIT_DISKIO).    
e1280 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20 42  /* Add the new B
e1290 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74  tShared object t
e12a0 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  o the linked lis
e12b0 74 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61  t sharable BtSha
e12c0 72 65 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  reds..    */.   
e12d0 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65   if( p->sharable
e12e0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
e12f0 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68  3_mutex *mutexSh
e1300 61 72 65 64 3b 0a 20 20 20 20 20 20 70 42 74 2d  ared;.      pBt-
e1310 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20  >nRef = 1;.     
e1320 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73   mutexShared = s
e1330 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
e1340 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
e1350 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
e1360 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45        if( SQLITE
e1370 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b 0a 20  _THREADSAFE ){. 
e1380 20 20 20 20 20 20 20 70 42 74 2d 3e 6d 75 74 65         pBt->mute
e1390 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  x = sqlite3_mute
e13a0 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  x_alloc(SQLITE_M
e13b0 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20  UTEX_FAST);.    
e13c0 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 74      if( pBt->mut
e13d0 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ex==0 ){.       
e13e0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
e13f0 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20  OMEM;.          
e1400 70 53 71 6c 69 74 65 2d 3e 6d 61 6c 6c 6f 63 46  pSqlite->mallocF
e1410 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20  ailed = 0;.     
e1420 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f       goto btree_
e1430 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  open_out;.      
e1440 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
e1450 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
e1460 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 65  enter(mutexShare
e1470 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70  d);.      pBt->p
e1480 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 53 68  Next = sqlite3Sh
e1490 61 72 65 64 43 61 63 68 65 4c 69 73 74 3b 0a 20  aredCacheList;. 
e14a0 20 20 20 20 20 73 71 6c 69 74 65 33 53 68 61 72       sqlite3Shar
e14b0 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 70 42  edCacheList = pB
e14c0 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
e14d0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
e14e0 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 7d  exShared);.    }
e14f0 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 66  .#endif.  }..#if
e1500 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
e1510 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
e1520 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  HE) && !defined(
e1530 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
e1540 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  IO).  /* If the 
e1550 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20 61  new Btree uses a
e1560 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 61   sharable pBtSha
e1570 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 74  red, then link t
e1580 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 65  he new.  ** Btre
e1590 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 20  e into the list 
e15a0 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 20  of all sharable 
e15b0 42 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 73  Btrees for the s
e15c0 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ame connection..
e15d0 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 73    ** The list is
e15e0 20 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 69   kept in ascendi
e15f0 6e 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 20  ng order by pBt 
e1600 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 20  address..  */.  
e1610 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  if( p->sharable 
e1620 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
e1630 20 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a 20    Btree *pSib;. 
e1640 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53     for(i=0; i<pS
e1650 71 6c 69 74 65 2d 3e 6e 44 62 3b 20 69 2b 2b 29  qlite->nDb; i++)
e1660 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 69  {.      if( (pSi
e1670 62 20 3d 20 70 53 71 6c 69 74 65 2d 3e 61 44 62  b = pSqlite->aDb
e1680 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70  [i].pBt)!=0 && p
e1690 53 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b  Sib->sharable ){
e16a0 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
e16b0 70 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70  pSib->pPrev ){ p
e16c0 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65  Sib = pSib->pPre
e16d0 76 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28  v; }.        if(
e16e0 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42   p->pBt<pSib->pB
e16f0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  t ){.          p
e1700 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a  ->pNext = pSib;.
e1710 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72            p->pPr
e1720 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ev = 0;.        
e1730 20 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20    pSib->pPrev = 
e1740 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  p;.        }else
e1750 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  {.          whil
e1760 65 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26  e( pSib->pNext &
e1770 26 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70  & pSib->pNext->p
e1780 42 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20  Bt<p->pBt ){.   
e1790 20 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20           pSib = 
e17a0 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pSib->pNext;.   
e17b0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
e17c0 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53     p->pNext = pS
e17d0 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ib->pNext;.     
e17e0 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20       p->pPrev = 
e17f0 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pSib;.          
e1800 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a  if( p->pNext ){.
e1810 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70              p->p
e1820 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b  Next->pPrev = p;
e1830 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
e1840 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65         pSib->pNe
e1850 78 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  xt = p;.        
e1860 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
e1870 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
e1880 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42   }.#endif.  *ppB
e1890 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65  tree = p;..btree
e18a0 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28  _open_out:.  if(
e18b0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
e18c0 7b 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26  {.    if( pBt &&
e18d0 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a   pBt->pPager ){.
e18e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
e18f0 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61  erClose(pBt->pPa
e1900 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ger);.    }.    
e1910 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74  sqlite3_free(pBt
e1920 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
e1930 72 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42  ree(p);.    *ppB
e1940 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  tree = 0;.  }.  
e1950 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
e1960 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68  .** Decrement th
e1970 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20  e BtShared.nRef 
e1980 63 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 69  counter.  When i
e1990 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a  t reaches zero,.
e19a0 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 74  ** remove the Bt
e19b0 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
e19c0 20 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 6e   from the sharin
e19d0 67 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 0a  g list.  Return.
e19e0 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 42  ** true if the B
e19f0 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75  tShared.nRef cou
e1a00 6e 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 72  nter reaches zer
e1a10 6f 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20  o and return.** 
e1a20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73  false if it is s
e1a30 74 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a  till positive..*
e1a40 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d  /.static int rem
e1a50 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69  oveFromSharingLi
e1a60 73 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  st(BtShared *pBt
e1a70 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
e1a80 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
e1a90 43 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  CHE.  sqlite3_mu
e1aa0 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20  tex *pMaster;.  
e1ab0 42 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 3b  BtShared *pList;
e1ac0 0a 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d  .  int removed =
e1ad0 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73   0;..  assert( s
e1ae0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
e1af0 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
e1b00 20 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20   );.  pMaster = 
e1b10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
e1b20 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
e1b30 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b  _STATIC_MASTER);
e1b40 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
e1b50 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b  _enter(pMaster);
e1b60 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a  .  pBt->nRef--;.
e1b70 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c    if( pBt->nRef<
e1b80 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  =0 ){.    if( sq
e1b90 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
e1ba0 4c 69 73 74 3d 3d 70 42 74 20 29 7b 0a 20 20 20  List==pBt ){.   
e1bb0 20 20 20 73 71 6c 69 74 65 33 53 68 61 72 65 64     sqlite3Shared
e1bc0 43 61 63 68 65 4c 69 73 74 20 3d 20 70 42 74 2d  CacheList = pBt-
e1bd0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73  >pNext;.    }els
e1be0 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20 3d  e{.      pList =
e1bf0 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61   sqlite3SharedCa
e1c00 63 68 65 4c 69 73 74 3b 0a 20 20 20 20 20 20 77  cheList;.      w
e1c10 68 69 6c 65 28 20 70 4c 69 73 74 20 26 26 20 70  hile( pList && p
e1c20 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74  List->pNext!=pBt
e1c30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73   ){.        pLis
e1c40 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a  t=pList->pNext;.
e1c50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
e1c60 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  ( pList ){.     
e1c70 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 20     pList->pNext 
e1c80 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pBt->pNext;.  
e1c90 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
e1ca0 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 41  if( SQLITE_THREA
e1cb0 44 53 41 46 45 20 29 7b 0a 20 20 20 20 20 20 73  DSAFE ){.      s
e1cc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65  qlite3_mutex_fre
e1cd0 65 28 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20  e(pBt->mutex);. 
e1ce0 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f 76 65 64     }.    removed
e1cf0 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 1;.  }.  sqli
e1d00 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
e1d10 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75  pMaster);.  retu
e1d20 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 65 6c 73  rn removed;.#els
e1d30 65 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65  e.  return 1;.#e
e1d40 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  ndif.}../*.** Cl
e1d50 6f 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61  ose an open data
e1d60 62 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64  base and invalid
e1d70 61 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e  ate all cursors.
e1d80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
e1d90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
e1da0 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a  reeClose(Btree *
e1db0 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  p){.  BtShared *
e1dc0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
e1dd0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  BtCursor *pCur;.
e1de0 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20  .  /* Close all 
e1df0 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76  cursors opened v
e1e00 69 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20  ia this handle. 
e1e10 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
e1e20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
e1e30 28 70 2d 3e 70 53 71 6c 69 74 65 2d 3e 6d 75 74  (p->pSqlite->mut
e1e40 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
e1e50 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
e1e60 20 70 43 75 72 20 3d 20 70 42 74 2d 3e 70 43 75   pCur = pBt->pCu
e1e70 72 73 6f 72 3b 0a 20 20 77 68 69 6c 65 28 20 70  rsor;.  while( p
e1e80 43 75 72 20 29 7b 0a 20 20 20 20 42 74 43 75 72  Cur ){.    BtCur
e1e90 73 6f 72 20 2a 70 54 6d 70 20 3d 20 70 43 75 72  sor *pTmp = pCur
e1ea0 3b 0a 20 20 20 20 70 43 75 72 20 3d 20 70 43 75  ;.    pCur = pCu
e1eb0 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66  r->pNext;.    if
e1ec0 28 20 70 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d  ( pTmp->pBtree==
e1ed0 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
e1ee0 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73  e3BtreeCloseCurs
e1ef0 6f 72 28 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a  or(pTmp);.    }.
e1f00 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61    }..  /* Rollba
e1f10 63 6b 20 61 6e 79 20 61 63 74 69 76 65 20 74 72  ck any active tr
e1f20 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 66 72  ansaction and fr
e1f30 65 65 20 74 68 65 20 68 61 6e 64 6c 65 20 73 74  ee the handle st
e1f40 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68  ructure..  ** Th
e1f50 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
e1f60 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29  3BtreeRollback()
e1f70 20 64 72 6f 70 73 20 61 6e 79 20 74 61 62 6c 65   drops any table
e1f80 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 0a 20  -locks held by. 
e1f90 20 2a 2a 20 74 68 69 73 20 68 61 6e 64 6c 65 2e   ** this handle.
e1fa0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42  .  */.  sqlite3B
e1fb0 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 29 3b  treeRollback(p);
e1fc0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
e1fd0 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 49  eave(p);..  /* I
e1fe0 66 20 74 68 65 72 65 20 61 72 65 20 73 74 69 6c  f there are stil
e1ff0 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 61 6e 64  l other outstand
e2000 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 74  ing references t
e2010 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  o the shared-btr
e2020 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72  ee.  ** structur
e2030 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54  e, return now. T
e2040 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20  he remainder of 
e2050 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 63  this procedure c
e2060 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 70 20 74  leans .  ** up t
e2070 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e  he shared-btree.
e2080 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
e2090 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30  p->wantToLock==0
e20a0 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30   && p->locked==0
e20b0 20 29 3b 0a 20 20 69 66 28 20 21 70 2d 3e 73 68   );.  if( !p->sh
e20c0 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 65  arable || remove
e20d0 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28  FromSharingList(
e20e0 70 42 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54  pBt) ){.    /* T
e20f0 68 65 20 70 42 74 20 69 73 20 6e 6f 20 6c 6f 6e  he pBt is no lon
e2100 67 65 72 20 6f 6e 20 74 68 65 20 73 68 61 72 69  ger on the shari
e2110 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 65 20 63  ng list, so we c
e2120 61 6e 20 61 63 63 65 73 73 0a 20 20 20 20 2a 2a  an access.    **
e2130 20 69 74 20 77 69 74 68 6f 75 74 20 68 61 76 69   it without havi
e2140 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d  ng to hold the m
e2150 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  utex..    **.   
e2160 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 20 61 6e   ** Clean out an
e2170 64 20 64 65 6c 65 74 65 20 74 68 65 20 42 74 53  d delete the BtS
e2180 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 20 20  hared object..  
e2190 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
e21a0 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29   !pBt->pCursor )
e21b0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
e21c0 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61  erClose(pBt->pPa
e21d0 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 70 42  ger);.    if( pB
e21e0 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 26  t->xFreeSchema &
e21f0 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 29  & pBt->pSchema )
e2200 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78 46 72  {.      pBt->xFr
e2210 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e 70 53  eeSchema(pBt->pS
e2220 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20  chema);.    }.  
e2230 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
e2240 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  Bt->pSchema);.  
e2250 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
e2260 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  Bt);.  }..#ifnde
e2270 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
e2280 41 52 45 44 5f 43 41 43 48 45 0a 20 20 61 73 73  ARED_CACHE.  ass
e2290 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  ert( p->wantToLo
e22a0 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ck==0 );.  asser
e22b0 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  t( p->locked==0 
e22c0 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65  );.  if( p->pPre
e22d0 76 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e  v ) p->pPrev->pN
e22e0 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a  ext = p->pNext;.
e22f0 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29    if( p->pNext )
e2300 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76   p->pNext->pPrev
e2310 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e   = p->pPrev;.#en
e2320 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66  dif..  sqlite3_f
e2330 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ree(p);.  return
e2340 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
e2350 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
e2360 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63 61 6c  busy handler cal
e2370 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a  lback function..
e2380 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
e2390 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
e23a0 65 65 53 65 74 42 75 73 79 48 61 6e 64 6c 65 72  eeSetBusyHandler
e23b0 28 42 74 72 65 65 20 2a 70 2c 20 42 75 73 79 48  (Btree *p, BusyH
e23c0 61 6e 64 6c 65 72 20 2a 70 48 61 6e 64 6c 65 72  andler *pHandler
e23d0 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
e23e0 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61  Bt = p->pBt;.  a
e23f0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
e2400 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 71  utex_held(p->pSq
e2410 6c 69 74 65 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  lite->mutex) );.
e2420 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
e2430 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 70  ter(p);.  pBt->p
e2440 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 70 48  BusyHandler = pH
e2450 61 6e 64 6c 65 72 3b 0a 20 20 73 71 6c 69 74 65  andler;.  sqlite
e2460 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
e2470 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72  dler(pBt->pPager
e2480 2c 20 70 48 61 6e 64 6c 65 72 29 3b 0a 20 20 73  , pHandler);.  s
e2490 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
e24a0 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
e24b0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
e24c0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d  * Change the lim
e24d0 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72  it on the number
e24e0 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65   of pages allowe
e24f0 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a  d in the cache..
e2500 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  **.** The maximu
e2510 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68  m number of cach
e2520 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74  e pages is set t
e2530 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a  o the absolute.*
e2540 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67  * value of mxPag
e2550 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73  e.  If mxPage is
e2560 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70   negative, the p
e2570 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65  ager will.** ope
e2580 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75  rate asynchronou
e2590 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f  sly - it will no
e25a0 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79  t stop to do fsy
e25b0 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75  nc()s.** to insu
e25c0 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74  re data is writt
e25d0 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73  en to the disk s
e25e0 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a  urface before.**
e25f0 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72   continuing.  Tr
e2600 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c  ansactions still
e2610 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f   work if synchro
e2620 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20  nous is off,.** 
e2630 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
e2640 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75   cannot be corru
e2650 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f  pted if this pro
e2660 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e  gram.** crashes.
e2670 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65    But if the ope
e2680 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72  rating system cr
e2690 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69  ashes or there i
e26a0 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70  s.** an abrupt p
e26b0 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65  ower failure whe
e26c0 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  n synchronous is
e26d0 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61   off, the databa
e26e0 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c  se.** could be l
e26f0 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73  eft in an incons
e2700 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63  istent and unrec
e2710 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a  overable state..
e2720 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69  ** Synchronous i
e2730 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20  s on by default 
e2740 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  so database corr
e2750 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a  uption is not.**
e2760 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72   normally a worr
e2770 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
e2780 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
e2790 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
e27a0 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  e(Btree *p, int 
e27b0 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61  mxPage){.  BtSha
e27c0 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
e27d0 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  t;.  assert( sql
e27e0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
e27f0 70 2d 3e 70 53 71 6c 69 74 65 2d 3e 6d 75 74 65  p->pSqlite->mute
e2800 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  x) );.  sqlite3B
e2810 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
e2820 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
e2830 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70 50  achesize(pBt->pP
e2840 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20  ager, mxPage);. 
e2850 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
e2860 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
e2870 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
e2880 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 77  .** Change the w
e2890 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63 65  ay data is synce
e28a0 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64  d to disk in ord
e28b0 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20 6f  er to increase o
e28c0 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f  r decrease.** ho
e28d0 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61 62  w well the datab
e28e0 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d 61  ase resists dama
e28f0 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61  ge due to OS cra
e2900 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a  shes and power.*
e2910 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65 76  * failures.  Lev
e2920 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d 65  el 1 is the same
e2930 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73   as asynchronous
e2940 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63 63   (no syncs() occ
e2950 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20  ur and.** there 
e2960 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61 62  is a high probab
e2970 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 29  ility of damage)
e2980 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68 65    Level 2 is the
e2990 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72 65   default.  There
e29a0 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c 6f  .** is a very lo
e29b0 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70  w but non-zero p
e29c0 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61  robability of da
e29d0 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20 72  mage.  Level 3 r
e29e0 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70 72  educes the.** pr
e29f0 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d  obability of dam
e2a00 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f  age to near zero
e2a10 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69 74   but with a writ
e2a20 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 65  e performance re
e2a30 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e  duction..*/.#ifn
e2a40 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
e2a50 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 51  PAGER_PRAGMAS.SQ
e2a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
e2a70 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
e2a80 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65  SafetyLevel(Btre
e2a90 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c 2c  e *p, int level,
e2aa0 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b 0a   int fullSync){.
e2ab0 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
e2ac0 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65  = p->pBt;.  asse
e2ad0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
e2ae0 78 5f 68 65 6c 64 28 70 2d 3e 70 53 71 6c 69 74  x_held(p->pSqlit
e2af0 65 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  e->mutex) );.  s
e2b00 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
e2b10 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61  (p);.  sqlite3Pa
e2b20 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65  gerSetSafetyLeve
e2b30 6c 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6c  l(pBt->pPager, l
e2b40 65 76 65 6c 2c 20 66 75 6c 6c 53 79 6e 63 29 3b  evel, fullSync);
e2b50 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
e2b60 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
e2b70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
e2b80 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
e2b90 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
e2ba0 67 69 76 65 6e 20 62 74 72 65 65 20 69 73 20 73  given btree is s
e2bb0 65 74 20 74 6f 20 73 61 66 65 74 79 20 6c 65 76  et to safety lev
e2bc0 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 0a  el 1.  In other.
e2bd0 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e  ** words, return
e2be0 20 54 52 55 45 20 69 66 20 6e 6f 20 73 79 6e 63   TRUE if no sync
e2bf0 28 29 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65  () occurs on the
e2c00 20 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a   disk files..*/.
e2c10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
e2c20 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
e2c30 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65  yncDisabled(Btre
e2c40 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65  e *p){.  BtShare
e2c50 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
e2c60 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73  .  int rc;.  ass
e2c70 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
e2c80 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 71 6c 69  ex_held(p->pSqli
e2c90 74 65 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a  te->mutex) );  .
e2ca0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
e2cb0 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74  ter(p);.  assert
e2cc0 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50  ( pBt && pBt->pP
e2cd0 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 73  ager );.  rc = s
e2ce0 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e  qlite3PagerNosyn
e2cf0 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  c(pBt->pPager);.
e2d00 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
e2d10 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
e2d20 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66   rc;.}..#if !def
e2d30 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
e2d40 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20  _PAGER_PRAGMAS) 
e2d50 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
e2d60 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 0a  TE_OMIT_VACUUM).
e2d70 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
e2d80 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 73   default pages s
e2d90 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62  ize and the numb
e2da0 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 62  er of reserved b
e2db0 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a 2a  ytes per page..*
e2dc0 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 73 69  *.** The page si
e2dd0 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77  ze must be a pow
e2de0 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e 20  er of 2 between 
e2df0 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e 20 20  512 and 65536.  
e2e00 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 73  If the page.** s
e2e10 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64 6f 65  ize supplied doe
e2e20 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 73 20  s not meet this 
e2e30 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 6e 20  constraint then 
e2e40 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73  the page size is
e2e50 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e   not.** changed.
e2e60 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65  .**.** Page size
e2e70 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e 65  s are constraine
e2e80 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 20  d to be a power 
e2e90 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20 74  of two so that t
e2ea0 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20  he region.** of 
e2eb0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
e2ec0 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69  e used for locki
e2ed0 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61 74  ng (beginning at
e2ee0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a   PENDING_BYTE,.*
e2ef0 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  * the first byte
e2f00 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62 6f   past the 1GB bo
e2f10 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30 30 30  undary, 0x400000
e2f20 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 63 63  00) needs to occ
e2f30 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 65 67  ur.** at the beg
e2f40 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 67 65  inning of a page
e2f50 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d  ..**.** If param
e2f60 65 74 65 72 20 6e 52 65 73 65 72 76 65 20 69 73  eter nReserve is
e2f70 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
e2f80 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72   then the number
e2f90 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a 2a 20   of reserved.** 
e2fa0 62 79 74 65 73 20 70 65 72 20 70 61 67 65 20 69  bytes per page i
e2fb0 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64  s left unchanged
e2fc0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
e2fd0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
e2fe0 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28  treeSetPageSize(
e2ff0 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 61  Btree *p, int pa
e3000 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73  geSize, int nRes
e3010 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  erve){.  int rc 
e3020 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42  = SQLITE_OK;.  B
e3030 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
e3040 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
e3050 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
e3060 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69   if( pBt->pageSi
e3070 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20 73  zeFixed ){.    s
e3080 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
e3090 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  (p);.    return 
e30a0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
e30b0 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 65  .  }.  if( nRese
e30c0 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65  rve<0 ){.    nRe
e30d0 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 67  serve = pBt->pag
e30e0 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61  eSize - pBt->usa
e30f0 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 69  bleSize;.  }.  i
e3100 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32  f( pageSize>=512
e3110 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51   && pageSize<=SQ
e3120 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
e3130 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 28 28  ZE &&.        ((
e3140 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65  pageSize-1)&page
e3150 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Size)==0 ){.    
e3160 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a  assert( (pageSiz
e3170 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20  e & 7)==0 );.   
e3180 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70   assert( !pBt->p
e3190 50 61 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70  Page1 && !pBt->p
e31a0 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42  Cursor );.    pB
e31b0 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61  t->pageSize = pa
e31c0 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d  geSize;.    rc =
e31d0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
e31e0 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50  Pagesize(pBt->pP
e31f0 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65  ager, &pBt->page
e3200 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 70 42 74  Size);.  }.  pBt
e3210 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70  ->usableSize = p
e3220 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e  Bt->pageSize - n
e3230 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 74  Reserve;.  sqlit
e3240 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
e3250 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
e3260 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
e3270 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69  e currently defi
e3280 6e 65 64 20 70 61 67 65 20 73 69 7a 65 0a 2a 2f  ned page size.*/
e3290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
e32a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
e32b0 47 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65  GetPageSize(Btre
e32c0 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  e *p){.  return 
e32d0 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  p->pBt->pageSize
e32e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
e32f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
e3300 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74  reeGetReserve(Bt
e3310 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  ree *p){.  int n
e3320 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
e3330 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20  Enter(p);.  n = 
e3340 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  p->pBt->pageSize
e3350 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c   - p->pBt->usabl
e3360 65 53 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33  eSize;.  sqlite3
e3370 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
e3380 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a   return n;.}../*
e3390 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69  .** Set the maxi
e33a0 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66  mum page count f
e33b0 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 69 66  or a database if
e33c0 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 74   mxPage is posit
e33d0 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67  ive..** No chang
e33e0 65 73 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d  es are made if m
e33f0 78 50 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65  xPage is 0 or ne
e3400 67 61 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72  gative..** Regar
e3410 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c  dless of the val
e3420 75 65 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65  ue of mxPage, re
e3430 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  turn the maximum
e3440 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a   page count..*/.
e3450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
e3460 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
e3470 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65  axPageCount(Btre
e3480 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65  e *p, int mxPage
e3490 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71  ){.  int n;.  sq
e34a0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
e34b0 70 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65  p);.  n = sqlite
e34c0 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75  3PagerMaxPageCou
e34d0 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  nt(p->pBt->pPage
e34e0 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71  r, mxPage);.  sq
e34f0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
e3500 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a  p);.  return n;.
e3510 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
e3520 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
e3530 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20  _PAGER_PRAGMAS) 
e3540 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
e3550 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20  TE_OMIT_VACUUM) 
e3560 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  */../*.** Change
e3570 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75   the 'auto-vacuu
e3580 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74  m' property of t
e3590 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 20  he database. If 
e35a0 74 68 65 20 27 61 75 74 6f 56 61 63 75 75 6d 27  the 'autoVacuum'
e35b0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73  .** parameter is
e35c0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
e35d0 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
e35e0 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 20   is enabled. If 
e35f0 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64  zero, it.** is d
e3600 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 65 66  isabled. The def
e3610 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
e3620 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 70  he auto-vacuum p
e3630 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 64  roperty is .** d
e3640 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65  etermined by the
e3650 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
e3660 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f  AUTOVACUUM macro
e3670 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
e3680 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
e3690 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75  treeSetAutoVacuu
e36a0 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  m(Btree *p, int 
e36b0 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66  autoVacuum){.#if
e36c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
e36d0 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74  AUTOVACUUM.  ret
e36e0 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
e36f0 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53  NLY;.#else.  BtS
e3700 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
e3710 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  pBt;.  int rc = 
e3720 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
e3730 20 61 76 20 3d 20 28 61 75 74 6f 56 61 63 75 75   av = (autoVacuu
e3740 6d 3f 31 3a 30 29 3b 0a 0a 20 20 73 71 6c 69 74  m?1:0);..  sqlit
e3750 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
e3760 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65  .  if( pBt->page
e3770 53 69 7a 65 46 69 78 65 64 20 26 26 20 61 76 21  SizeFixed && av!
e3780 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  =pBt->autoVacuum
e3790 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
e37a0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
e37b0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e  }else{.    pBt->
e37c0 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 3b  autoVacuum = av;
e37d0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
e37e0 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
e37f0 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66  eturn rc;.#endif
e3800 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
e3810 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
e3820 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20  e 'auto-vacuum' 
e3830 70 72 6f 70 65 72 74 79 2e 20 49 66 20 61 75 74  property. If aut
e3840 6f 2d 76 61 63 75 75 6d 20 69 73 20 0a 2a 2a 20  o-vacuum is .** 
e3850 65 6e 61 62 6c 65 64 20 31 20 69 73 20 72 65 74  enabled 1 is ret
e3860 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
e3870 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   0..*/.SQLITE_PR
e3880 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
e3890 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63  3BtreeGetAutoVac
e38a0 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b 0a 23  uum(Btree *p){.#
e38b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
e38c0 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72  T_AUTOVACUUM.  r
e38d0 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f  eturn BTREE_AUTO
e38e0 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c  VACUUM_NONE;.#el
e38f0 73 65 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  se.  int rc;.  s
e3900 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
e3910 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 0a 20 20  (p);.  rc = (.  
e3920 20 20 28 21 70 2d 3e 70 42 74 2d 3e 61 75 74 6f    (!p->pBt->auto
e3930 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f 41 55  Vacuum)?BTREE_AU
e3940 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3a 0a 20  TOVACUUM_NONE:. 
e3950 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 69 6e 63     (!p->pBt->inc
e3960 72 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f 41  rVacuum)?BTREE_A
e3970 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3a 0a  UTOVACUUM_FULL:.
e3980 20 20 20 20 42 54 52 45 45 5f 41 55 54 4f 56 41      BTREE_AUTOVA
e3990 43 55 55 4d 5f 49 4e 43 52 0a 20 20 29 3b 0a 20  CUUM_INCR.  );. 
e39a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
e39b0 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
e39c0 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f  rc;.#endif.}.../
e39d0 2a 0a 2a 2a 20 47 65 74 20 61 20 72 65 66 65 72  *.** Get a refer
e39e0 65 6e 63 65 20 74 6f 20 70 50 61 67 65 31 20 6f  ence to pPage1 o
e39f0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
e3a00 69 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 0a  ile.  This will.
e3a10 2a 2a 20 61 6c 73 6f 20 61 63 71 75 69 72 65 20  ** also acquire 
e3a20 61 20 72 65 61 64 6c 6f 63 6b 20 6f 6e 20 74 68  a readlock on th
e3a30 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53  at file..**.** S
e3a40 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
e3a50 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e  rned on success.
e3a60 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73    If the file is
e3a70 20 6e 6f 74 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66   not a.** well-f
e3a80 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 66  ormed database f
e3a90 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45  ile, then SQLITE
e3aa0 5f 43 4f 52 52 55 50 54 20 69 73 20 72 65 74 75  _CORRUPT is retu
e3ab0 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f  rned..** SQLITE_
e3ac0 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64  BUSY is returned
e3ad0 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
e3ae0 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 53 51 4c   is locked.  SQL
e3af0 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 73 20  ITE_NOMEM.** is 
e3b00 72 65 74 75 72 6e 65 64 20 69 66 20 77 65 20 72  returned if we r
e3b10 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  un out of memory
e3b20 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  . .*/.static int
e3b30 20 6c 6f 63 6b 42 74 72 65 65 28 42 74 53 68 61   lockBtree(BtSha
e3b40 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74  red *pBt){.  int
e3b50 20 72 63 2c 20 70 61 67 65 53 69 7a 65 3b 0a 20   rc, pageSize;. 
e3b60 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31   MemPage *pPage1
e3b70 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
e3b80 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
e3b90 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
e3ba0 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31   if( pBt->pPage1
e3bb0 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
e3bc0 5f 4f 4b 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  _OK;.  rc = sqli
e3bd0 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
e3be0 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c  pBt, 1, &pPage1,
e3bf0 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   0);.  if( rc!=S
e3c00 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
e3c10 6e 20 72 63 3b 0a 20 20 0a 0a 20 20 2f 2a 20 44  n rc;.  ..  /* D
e3c20 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20  o some checking 
e3c30 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74  to help insure t
e3c40 68 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65  he file we opene
e3c50 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a  d really is.  **
e3c60 20 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73   a valid databas
e3c70 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20  e file. .  */.  
e3c80 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41  rc = SQLITE_NOTA
e3c90 44 42 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  DB;.  if( sqlite
e3ca0 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
e3cb0 70 42 74 2d 3e 70 50 61 67 65 72 29 3e 30 20 29  pBt->pPager)>0 )
e3cc0 7b 0a 20 20 20 20 75 38 20 2a 70 61 67 65 31 20  {.    u8 *page1 
e3cd0 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b  = pPage1->aData;
e3ce0 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28  .    if( memcmp(
e3cf0 70 61 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 61  page1, zMagicHea
e3d00 64 65 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20  der, 16)!=0 ){. 
e3d10 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f       goto page1_
e3d20 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20  init_failed;.   
e3d30 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31   }.    if( page1
e3d40 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  [18]>1 ){.      
e3d50 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20  pBt->readOnly = 
e3d60 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
e3d70 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a   page1[19]>1 ){.
e3d80 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31        goto page1
e3d90 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20  _init_failed;.  
e3da0 20 20 7d 0a 20 20 20 20 70 61 67 65 53 69 7a 65    }.    pageSize
e3db0 20 3d 20 67 65 74 32 62 79 74 65 28 26 70 61 67   = get2byte(&pag
e3dc0 65 31 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28  e1[16]);.    if(
e3dd0 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70   ((pageSize-1)&p
e3de0 61 67 65 53 69 7a 65 29 21 3d 30 20 7c 7c 20 70  ageSize)!=0 || p
e3df0 61 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c 0a 20  ageSize<512 ||. 
e3e00 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4d         (SQLITE_M
e3e10 41 58 5f 50 41 47 45 5f 53 49 5a 45 3c 33 32 37  AX_PAGE_SIZE<327
e3e20 36 38 20 26 26 20 70 61 67 65 53 69 7a 65 3e 53  68 && pageSize>S
e3e30 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
e3e40 49 5a 45 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  IZE).    ){.    
e3e50 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69    goto page1_ini
e3e60 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a  t_failed;.    }.
e3e70 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61 67      assert( (pag
e3e80 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b  eSize & 7)==0 );
e3e90 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69  .    pBt->pageSi
e3ea0 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20  ze = pageSize;. 
e3eb0 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69     pBt->usableSi
e3ec0 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20 2d 20  ze = pageSize - 
e3ed0 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69  page1[20];.    i
e3ee0 66 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  f( pBt->usableSi
e3ef0 7a 65 3c 35 30 30 20 29 7b 0a 20 20 20 20 20 20  ze<500 ){.      
e3f00 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
e3f10 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
e3f20 20 20 70 42 74 2d 3e 6d 61 78 45 6d 62 65 64 46    pBt->maxEmbedF
e3f30 72 61 63 20 3d 20 70 61 67 65 31 5b 32 31 5d 3b  rac = page1[21];
e3f40 0a 20 20 20 20 70 42 74 2d 3e 6d 69 6e 45 6d 62  .    pBt->minEmb
e3f50 65 64 46 72 61 63 20 3d 20 70 61 67 65 31 5b 32  edFrac = page1[2
e3f60 32 5d 3b 0a 20 20 20 20 70 42 74 2d 3e 6d 69 6e  2];.    pBt->min
e3f70 4c 65 61 66 46 72 61 63 20 3d 20 70 61 67 65 31  LeafFrac = page1
e3f80 5b 32 33 5d 3b 0a 23 69 66 6e 64 65 66 20 53 51  [23];.#ifndef SQ
e3f90 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
e3fa0 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75  CUUM.    pBt->au
e3fb0 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34  toVacuum = (get4
e3fc0 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b  byte(&page1[36 +
e3fd0 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20   4*4])?1:0);.   
e3fe0 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d   pBt->incrVacuum
e3ff0 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61   = (get4byte(&pa
e4000 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31  ge1[36 + 7*4])?1
e4010 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  :0);.#endif.  }.
e4020 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69  .  /* maxLocal i
e4030 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d  s the maximum am
e4040 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
e4050 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79  to store locally
e4060 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c   for.  ** a cell
e4070 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20  .  Make sure it 
e4080 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20  is small enough 
e4090 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74  so that at least
e40a0 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20   minFanout.  ** 
e40b0 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66  cells can will f
e40c0 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20  it on one page. 
e40d0 20 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d   We assume a 10-
e40e0 62 79 74 65 20 70 61 67 65 20 68 65 61 64 65 72  byte page header
e40f0 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74  ..  ** Besides t
e4100 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20  he payload, the 
e4110 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a  cell must store:
e4120 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65  .  **     2-byte
e4130 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
e4140 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d  cell.  **     4-
e4150 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74  byte child point
e4160 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79  er.  **     9-by
e4170 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20  te nKey value.  
e4180 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44  **     4-byte nD
e4190 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20  ata value.  **  
e41a0 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c     4-byte overfl
e41b0 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a  ow page pointer.
e41c0 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63    ** So a cell c
e41d0 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62  onsists of a 2-b
e41e0 79 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65  yte poiner, a he
e41f0 61 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73  ader which is as
e4200 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37   much as.  ** 17
e4210 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74   bytes long, 0 t
e4220 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79  o N bytes of pay
e4230 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74  load, and an opt
e4240 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65  ional 4 byte ove
e4250 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20  rflow.  ** page 
e4260 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20  pointer..  */.  
e4270 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20  pBt->maxLocal = 
e4280 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
e4290 2d 31 32 29 2a 70 42 74 2d 3e 6d 61 78 45 6d 62  -12)*pBt->maxEmb
e42a0 65 64 46 72 61 63 2f 32 35 35 20 2d 20 32 33 3b  edFrac/255 - 23;
e42b0 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c  .  pBt->minLocal
e42c0 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53   = (pBt->usableS
e42d0 69 7a 65 2d 31 32 29 2a 70 42 74 2d 3e 6d 69 6e  ize-12)*pBt->min
e42e0 45 6d 62 65 64 46 72 61 63 2f 32 35 35 20 2d 20  EmbedFrac/255 - 
e42f0 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65  23;.  pBt->maxLe
e4300 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  af = pBt->usable
e4310 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74  Size - 35;.  pBt
e4320 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74  ->minLeaf = (pBt
e4330 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29  ->usableSize-12)
e4340 2a 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 46 72 61  *pBt->minLeafFra
e4350 63 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 69 66  c/255 - 23;.  if
e4360 28 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3e  ( pBt->minLocal>
e4370 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 7c 7c  pBt->maxLocal ||
e4380 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3c 30   pBt->maxLocal<0
e4390 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 61 67   ){.    goto pag
e43a0 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a  e1_init_failed;.
e43b0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 42    }.  assert( pB
e43c0 74 2d 3e 6d 61 78 4c 65 61 66 20 2b 20 32 33 20  t->maxLeaf + 23 
e43d0 3c 3d 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28  <= MX_CELL_SIZE(
e43e0 70 42 74 29 20 29 3b 0a 20 20 70 42 74 2d 3e 70  pBt) );.  pBt->p
e43f0 50 61 67 65 31 20 3d 20 70 50 61 67 65 31 3b 0a  Page1 = pPage1;.
e4400 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e4410 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69 6e 69 74 5f  OK;..page1_init_
e4420 66 61 69 6c 65 64 3a 0a 20 20 72 65 6c 65 61 73  failed:.  releas
e4430 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20  ePage(pPage1);. 
e4440 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30   pBt->pPage1 = 0
e4450 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
e4460 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
e4470 74 69 6e 65 20 77 6f 72 6b 73 20 6c 69 6b 65 20  tine works like 
e4480 6c 6f 63 6b 42 74 72 65 65 28 29 20 65 78 63 65  lockBtree() exce
e4490 70 74 20 74 68 61 74 20 69 74 20 61 6c 73 6f 20  pt that it also 
e44a0 69 6e 76 6f 6b 65 73 20 74 68 65 0a 2a 2a 20 62  invokes the.** b
e44b0 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 20  usy callback if 
e44c0 74 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f  there is lock co
e44d0 6e 74 65 6e 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  ntention..*/.sta
e44e0 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65  tic int lockBtre
e44f0 65 57 69 74 68 52 65 74 72 79 28 42 74 72 65 65  eWithRetry(Btree
e4500 20 2a 70 52 65 66 29 7b 0a 20 20 69 6e 74 20 72   *pRef){.  int r
e4510 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
e4520 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
e4530 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
e4540 28 70 52 65 66 29 20 29 3b 0a 20 20 69 66 28 20  (pRef) );.  if( 
e4550 70 52 65 66 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  pRef->inTrans==T
e4560 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20  RANS_NONE ){.   
e4570 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f   u8 inTransactio
e4580 6e 20 3d 20 70 52 65 66 2d 3e 70 42 74 2d 3e 69  n = pRef->pBt->i
e4590 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 0a 20 20  nTransaction;.  
e45a0 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79    btreeIntegrity
e45b0 28 70 52 65 66 29 3b 0a 20 20 20 20 72 63 20 3d  (pRef);.    rc =
e45c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
e45d0 69 6e 54 72 61 6e 73 28 70 52 65 66 2c 20 30 29  inTrans(pRef, 0)
e45e0 3b 0a 20 20 20 20 70 52 65 66 2d 3e 70 42 74 2d  ;.    pRef->pBt-
e45f0 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d  >inTransaction =
e4600 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 0a   inTransaction;.
e4610 20 20 20 20 70 52 65 66 2d 3e 69 6e 54 72 61 6e      pRef->inTran
e4620 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  s = TRANS_NONE;.
e4630 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
e4640 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
e4650 52 65 66 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73  Ref->pBt->nTrans
e4660 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 7d 0a  action--;.    }.
e4670 20 20 20 20 62 74 72 65 65 49 6e 74 65 67 72 69      btreeIntegri
e4680 74 79 28 70 52 65 66 29 3b 0a 20 20 7d 0a 20 20  ty(pRef);.  }.  
e4690 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 20 20  return rc;.}.   
e46a0 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74      ../*.** If t
e46b0 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73  here are no outs
e46c0 74 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 20  tanding cursors 
e46d0 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 69  and we are not i
e46e0 6e 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a 20  n the middle.** 
e46f0 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  of a transaction
e4700 20 62 75 74 20 74 68 65 72 65 20 69 73 20 61 20   but there is a 
e4710 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  read lock on the
e4720 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a   database, then.
e4730 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
e4740 75 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 74  unrefs the first
e4750 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74   page of the dat
e4760 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 68  abase file which
e4770 20 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 66   .** has the eff
e4780 65 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e 67  ect of releasing
e4790 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a   the read lock..
e47a0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61  **.** If there a
e47b0 72 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69  re any outstandi
e47c0 6e 67 20 63 75 72 73 6f 72 73 2c 20 74 68 69 73  ng cursors, this
e47d0 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
e47e0 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  -op..**.** If th
e47f0 65 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63  ere is a transac
e4800 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73  tion in progress
e4810 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
e4820 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
e4830 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b  atic void unlock
e4840 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74  BtreeIfUnused(Bt
e4850 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
e4860 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
e4870 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
e4880 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
e4890 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
e48a0 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26  on==TRANS_NONE &
e48b0 26 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3d 3d  & pBt->pCursor==
e48c0 30 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31  0 && pBt->pPage1
e48d0 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73  !=0 ){.    if( s
e48e0 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
e48f0 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
e4900 3e 3d 31 20 29 7b 0a 20 20 20 20 20 20 69 66 28  >=1 ){.      if(
e4910 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44   pBt->pPage1->aD
e4920 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ata==0 ){.      
e4930 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
e4940 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a   = pBt->pPage1;.
e4950 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61          pPage->a
e4960 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61  Data = sqlite3Pa
e4970 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65  gerGetData(pPage
e4980 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
e4990 20 20 20 20 70 50 61 67 65 2d 3e 70 42 74 20 3d      pPage->pBt =
e49a0 20 70 42 74 3b 0a 20 20 20 20 20 20 20 20 70 50   pBt;.        pP
e49b0 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 31 3b 0a 20  age->pgno = 1;. 
e49c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c       }.      rel
e49d0 65 61 73 65 50 61 67 65 28 70 42 74 2d 3e 70 50  easePage(pBt->pP
e49e0 61 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20  age1);.    }.   
e49f0 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30   pBt->pPage1 = 0
e4a00 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d  ;.    pBt->inStm
e4a10 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  t = 0;.  }.}../*
e4a20 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
e4a30 20 64 61 74 61 62 61 73 65 20 62 79 20 69 6e 69   database by ini
e4a40 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 66 69  tializing the fi
e4a50 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 0a  rst page of the.
e4a60 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  ** file..*/.stat
e4a70 69 63 20 69 6e 74 20 6e 65 77 44 61 74 61 62 61  ic int newDataba
e4a80 73 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  se(BtShared *pBt
e4a90 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  ){.  MemPage *pP
e4aa0 31 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  1;.  unsigned ch
e4ab0 61 72 20 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20  ar *data;.  int 
e4ac0 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  rc;..  assert( s
e4ad0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
e4ae0 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
e4af0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61  .  if( sqlite3Pa
e4b00 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
e4b10 2d 3e 70 50 61 67 65 72 29 3e 30 20 29 20 72 65  ->pPager)>0 ) re
e4b20 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e4b30 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61    pP1 = pBt->pPa
e4b40 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ge1;.  assert( p
e4b50 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20  P1!=0 );.  data 
e4b60 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20  = pP1->aData;.  
e4b70 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
e4b80 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50  rWrite(pP1->pDbP
e4b90 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
e4ba0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65   return rc;.  me
e4bb0 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69  mcpy(data, zMagi
e4bc0 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28  cHeader, sizeof(
e4bd0 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a  zMagicHeader));.
e4be0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
e4bf0 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d  (zMagicHeader)==
e4c00 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65  16 );.  put2byte
e4c10 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d  (&data[16], pBt-
e4c20 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61  >pageSize);.  da
e4c30 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61  ta[18] = 1;.  da
e4c40 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 64 61  ta[19] = 1;.  da
e4c50 74 61 5b 32 30 5d 20 3d 20 70 42 74 2d 3e 70 61  ta[20] = pBt->pa
e4c60 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73  geSize - pBt->us
e4c70 61 62 6c 65 53 69 7a 65 3b 0a 20 20 64 61 74 61  ableSize;.  data
e4c80 5b 32 31 5d 20 3d 20 70 42 74 2d 3e 6d 61 78 45  [21] = pBt->maxE
e4c90 6d 62 65 64 46 72 61 63 3b 0a 20 20 64 61 74 61  mbedFrac;.  data
e4ca0 5b 32 32 5d 20 3d 20 70 42 74 2d 3e 6d 69 6e 45  [22] = pBt->minE
e4cb0 6d 62 65 64 46 72 61 63 3b 0a 20 20 64 61 74 61  mbedFrac;.  data
e4cc0 5b 32 33 5d 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c  [23] = pBt->minL
e4cd0 65 61 66 46 72 61 63 3b 0a 20 20 6d 65 6d 73 65  eafFrac;.  memse
e4ce0 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20  t(&data[24], 0, 
e4cf0 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50  100-24);.  zeroP
e4d00 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49 4e 54  age(pP1, PTF_INT
e4d10 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46  KEY|PTF_LEAF|PTF
e4d20 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 20 70  _LEAFDATA );.  p
e4d30 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
e4d40 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53  d = 1;.#ifndef S
e4d50 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
e4d60 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74 28 20  ACUUM.  assert( 
e4d70 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d  pBt->autoVacuum=
e4d80 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56  =1 || pBt->autoV
e4d90 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73  acuum==0 );.  as
e4da0 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 72 56  sert( pBt->incrV
e4db0 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d  acuum==1 || pBt-
e4dc0 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20 29  >incrVacuum==0 )
e4dd0 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61  ;.  put4byte(&da
e4de0 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42  ta[36 + 4*4], pB
e4df0 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a  t->autoVacuum);.
e4e00 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61    put4byte(&data
e4e10 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d  [36 + 7*4], pBt-
e4e20 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65  >incrVacuum);.#e
e4e30 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51  ndif.  return SQ
e4e40 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
e4e50 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61  * Attempt to sta
e4e60 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63  rt a new transac
e4e70 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72  tion. A write-tr
e4e80 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20  ansaction.** is 
e4e90 73 74 61 72 74 65 64 20 69 66 20 74 68 65 20 73  started if the s
e4ea0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
e4eb0 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72  s nonzero, other
e4ec0 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20  wise a read-.** 
e4ed0 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66  transaction.  If
e4ee0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
e4ef0 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72  ment is 2 or mor
e4f00 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a  e and exclusive.
e4f10 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
e4f20 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69  s started, meani
e4f30 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  ng that no other
e4f40 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f   process is allo
e4f50 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73  wed.** to access
e4f60 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
e4f70 41 20 70 72 65 65 78 69 73 74 69 6e 67 20 74 72  A preexisting tr
e4f80 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f  ansaction may no
e4f90 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64  t be.** upgraded
e4fa0 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79   to exclusive by
e4fb0 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f   calling this ro
e4fc0 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74  utine a second t
e4fd0 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63  ime - the.** exc
e4fe0 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e  lusivity flag on
e4ff0 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e  ly works for a n
e5000 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  ew transaction..
e5010 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72  **.** A write-tr
e5020 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
e5030 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65  e started before
e5040 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20   attempting any 
e5050 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74  .** changes to t
e5060 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f  he database.  No
e5070 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
e5080 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a  ing routines .**
e5090 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73   will work unles
e50a0 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  s a transaction 
e50b0 69 73 20 73 74 61 72 74 65 64 20 66 69 72 73 74  is started first
e50c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c  :.**.**      sql
e50d0 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
e50e0 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
e50f0 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
e5100 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20  eIndex().**     
e5110 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
e5120 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20  arTable().**    
e5130 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72    sqlite3BtreeDr
e5140 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20  opTable().**    
e5150 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e    sqlite3BtreeIn
e5160 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73  sert().**      s
e5170 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
e5180 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
e5190 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
e51a0 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ta().**.** If an
e51b0 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74   initial attempt
e51c0 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20   to acquire the 
e51d0 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75  lock fails becau
e51e0 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65  se of lock conte
e51f0 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65  ntion.** and the
e5200 20 64 61 74 61 62 61 73 65 20 77 61 73 20 70 72   database was pr
e5210 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65  eviously unlocke
e5220 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74  d, then invoke t
e5230 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a  he busy handler.
e5240 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ** if there is o
e5250 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68 65 72  ne.  But if ther
e5260 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  e was previously
e5270 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f   a read-lock, do
e5280 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74   not.** invoke t
e5290 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  he busy handler 
e52a0 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51  - just return SQ
e52b0 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49  LITE_BUSY.  SQLI
e52c0 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72  TE_BUSY is .** r
e52d0 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65  eturned when the
e52e0 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
e52f0 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64  read-lock in ord
e5300 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65  er to avoid a de
e5310 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75  adlock..**.** Su
e5320 70 70 6f 73 65 20 74 68 65 72 65 20 61 72 65 20  ppose there are 
e5330 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20  two processes A 
e5340 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20 61 20  and B.  A has a 
e5350 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20  read lock and B 
e5360 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65  has.** a reserve
e5370 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73  d lock.  B tries
e5380 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65   to promote to e
e5390 78 63 6c 75 73 69 76 65 20 62 75 74 20 69 73 20  xclusive but is 
e53a0 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a  blocked because.
e53b0 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c  ** of A's read l
e53c0 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f  ock.  A tries to
e53d0 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65   promote to rese
e53e0 72 76 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63  rved but is bloc
e53f0 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65  ked by B..** One
e5400 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66   or the other of
e5410 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73   the two process
e5420 65 73 20 6d 75 73 74 20 67 69 76 65 20 77 61 79  es must give way
e5430 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20 62 65   or there can be
e5440 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e  .** no progress.
e5450 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53    By returning S
e5460 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e  QLITE_BUSY and n
e5470 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  ot invoking the 
e5480 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  busy callback.**
e5490 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20   when A already 
e54a0 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c  has a read lock,
e54b0 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20   we encourage A 
e54c0 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c  to give up and l
e54d0 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e  et B.** proceed.
e54e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
e54f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
e5500 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74  reeBeginTrans(Bt
e5510 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c  ree *p, int wrfl
e5520 61 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ag){.  BtShared 
e5530 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
e5540 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
e5550 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  _OK;..  sqlite3B
e5560 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
e5570 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
e5580 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
e5590 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79  btree is already
e55a0 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e   in a write-tran
e55b0 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20  saction, or it. 
e55c0 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69   ** is already i
e55d0 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63  n a read-transac
e55e0 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d  tion and a read-
e55f0 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a  transaction.  **
e5600 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74   is requested, t
e5610 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  his is a no-op..
e5620 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e    */.  if( p->in
e5630 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
e5640 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e  TE || (p->inTran
e5650 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26  s==TRANS_READ &&
e5660 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20   !wrflag) ){.   
e5670 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75   goto trans_begu
e5680 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69  n;.  }..  /* Wri
e5690 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  te transactions 
e56a0 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65  are not possible
e56b0 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20   on a read-only 
e56c0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66  database */.  if
e56d0 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  ( pBt->readOnly 
e56e0 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  && wrflag ){.   
e56f0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
e5700 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20  DONLY;.    goto 
e5710 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d  trans_begun;.  }
e5720 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65  ..  /* If anothe
e5730 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  r database handl
e5740 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f 70  e has already op
e5750 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 61  ened a write tra
e5760 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f  nsaction .  ** o
e5770 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74  n this shared-bt
e5780 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e  ree structure an
e5790 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 65  d a second write
e57a0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a   transaction is.
e57b0 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c 20    ** requested, 
e57c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
e57d0 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  SY..  */.  if( p
e57e0 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
e57f0 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26  n==TRANS_WRITE &
e5800 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20  & wrflag ){.    
e5810 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
e5820 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73  ;.    goto trans
e5830 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 64  _begun;.  }..  d
e5840 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 42 74 2d  o {.    if( pBt-
e5850 3e 70 50 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20  >pPage1==0 ){.  
e5860 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 42 74 72      rc = lockBtr
e5870 65 65 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 0a  ee(pBt);.    }..
e5880 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
e5890 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20  TE_OK && wrflag 
e58a0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  ){.      if( pBt
e58b0 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20  ->readOnly ){.  
e58c0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e58d0 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20  E_READONLY;.    
e58e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
e58f0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
e5900 65 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61  erBegin(pBt->pPa
e5910 67 65 31 2d 3e 70 44 62 50 61 67 65 2c 20 77 72  ge1->pDbPage, wr
e5920 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 20 20 20  flag>1);.       
e5930 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
e5940 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
e5950 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65  rc = newDatabase
e5960 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  (pBt);.        }
e5970 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
e5980 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51   .    if( rc==SQ
e5990 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
e59a0 20 69 66 28 20 77 72 66 6c 61 67 20 29 20 70 42   if( wrflag ) pB
e59b0 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20  t->inStmt = 0;. 
e59c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
e59d0 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
e59e0 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a  sed(pBt);.    }.
e59f0 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51    }while( rc==SQ
e5a00 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74  LITE_BUSY && pBt
e5a10 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
e5a20 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20  =TRANS_NONE &&. 
e5a30 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
e5a40 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65  InvokeBusyHandle
e5a50 72 28 70 42 74 2d 3e 70 42 75 73 79 48 61 6e 64  r(pBt->pBusyHand
e5a60 6c 65 72 29 20 29 3b 0a 0a 20 20 69 66 28 20 72  ler) );..  if( r
e5a70 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
e5a80 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61      if( p->inTra
e5a90 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ns==TRANS_NONE )
e5aa0 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72  {.      pBt->nTr
e5ab0 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 20 20 20  ansaction++;.   
e5ac0 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e   }.    p->inTran
e5ad0 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 4e  s = (wrflag?TRAN
e5ae0 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 45  S_WRITE:TRANS_RE
e5af0 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  AD);.    if( p->
e5b00 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54  inTrans>pBt->inT
e5b10 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20  ransaction ){.  
e5b20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73      pBt->inTrans
e5b30 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72  action = p->inTr
e5b40 61 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ans;.    }.  }..
e5b50 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20  .trans_begun:.  
e5b60 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
e5b70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
e5b80 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
e5b90 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64  urn rc;.}..#ifnd
e5ba0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
e5bb0 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a 2a 2a  UTOVACUUM../*.**
e5bc0 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72   Set the pointer
e5bd0 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72  -map entries for
e5be0 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66   all children of
e5bf0 20 70 61 67 65 20 70 50 61 67 65 2e 20 41 6c 73   page pPage. Als
e5c00 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65 20 63  o, if.** pPage c
e5c10 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20 74 68  ontains cells th
e5c20 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72  at point to over
e5c30 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65 74 20  flow pages, set 
e5c40 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d  the pointer.** m
e5c50 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74  ap entries for t
e5c60 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
e5c70 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 73 74  s as well..*/.st
e5c80 61 74 69 63 20 69 6e 74 20 73 65 74 43 68 69 6c  atic int setChil
e5c90 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61 67 65  dPtrmaps(MemPage
e5ca0 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20   *pPage){.  int 
e5cb0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
e5cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e5cd0 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69 61 62  * Counter variab
e5ce0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c  le */.  int nCel
e5cf0 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
e5d00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
e5d10 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e  mber of cells in
e5d20 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f 0a 20   page pPage */. 
e5d30 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
e5d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e5d50 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
e5d60 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64  de */.  BtShared
e5d70 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70   *pBt = pPage->p
e5d80 42 74 3b 0a 20 20 69 6e 74 20 69 73 49 6e 69 74  Bt;.  int isInit
e5d90 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73  Orig = pPage->is
e5da0 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e  Init;.  Pgno pgn
e5db0 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b  o = pPage->pgno;
e5dc0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
e5dd0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
e5de0 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
e5df0 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ) );.  rc = sqli
e5e00 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65  te3BtreeInitPage
e5e10 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 70  (pPage, pPage->p
e5e20 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 72  Parent);.  if( r
e5e30 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
e5e40 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69      goto set_chi
e5e50 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a  ld_ptrmaps_out;.
e5e60 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50    }.  nCell = pP
e5e70 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66  age->nCell;..  f
e5e80 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
e5e90 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70   i++){.    u8 *p
e5ea0 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
e5eb0 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20  pPage, i);..    
e5ec0 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76  rc = ptrmapPutOv
e5ed0 66 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  flPtr(pPage, pCe
e5ee0 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ll);.    if( rc!
e5ef0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e5f00 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69      goto set_chi
e5f10 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a  ld_ptrmaps_out;.
e5f20 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21      }..    if( !
e5f30 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
e5f40 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50       Pgno childP
e5f50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70  gno = get4byte(p
e5f60 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 72 63 20  Cell);.      rc 
e5f70 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c  = ptrmapPut(pBt,
e5f80 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d   childPgno, PTRM
e5f90 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 29 3b  AP_BTREE, pgno);
e5fa0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
e5fb0 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
e5fc0 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70  set_child_ptrmap
e5fd0 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  s_out;.    }.  }
e5fe0 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e  ..  if( !pPage->
e5ff0 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f  leaf ){.    Pgno
e6000 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74   childPgno = get
e6010 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
e6020 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
e6030 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 72 63  fset+8]);.    rc
e6040 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74   = ptrmapPut(pBt
e6050 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52  , childPgno, PTR
e6060 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 29  MAP_BTREE, pgno)
e6070 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 69 6c 64  ;.  }..set_child
e6080 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a 0a 20 20  _ptrmaps_out:.  
e6090 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
e60a0 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 72 65  isInitOrig;.  re
e60b0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
e60c0 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e 20 70  * Somewhere on p
e60d0 50 61 67 65 2c 20 77 68 69 63 68 20 69 73 20 67  Page, which is g
e60e0 75 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20  uarenteed to be 
e60f0 61 20 62 74 72 65 65 20 70 61 67 65 2c 20 6e 6f  a btree page, no
e6100 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a  t an overflow.**
e6110 20 70 61 67 65 2c 20 69 73 20 61 20 70 6f 69 6e   page, is a poin
e6120 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 72 6f  ter to page iFro
e6130 6d 2e 20 4d 6f 64 69 66 79 20 74 68 69 73 20 70  m. Modify this p
e6140 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 20 69  ointer so that i
e6150 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 69  t points to.** i
e6160 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65 54  To. Parameter eT
e6170 79 70 65 20 64 65 73 63 72 69 62 65 73 20 74 68  ype describes th
e6180 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74 65  e type of pointe
e6190 72 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 64  r to be modified
e61a0 2c 20 61 73 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 73  , as .** follows
e61b0 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42  :.**.** PTRMAP_B
e61c0 54 52 45 45 3a 20 20 20 20 20 70 50 61 67 65 20  TREE:     pPage 
e61d0 69 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 2e  is a btree-page.
e61e0 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69   The pointer poi
e61f0 6e 74 73 20 61 74 20 61 20 63 68 69 6c 64 20 0a  nts at a child .
e6200 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
e6210 20 20 20 20 20 70 61 67 65 20 6f 66 20 70 50 61       page of pPa
e6220 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  ge..**.** PTRMAP
e6230 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50 61 67  _OVERFLOW1: pPag
e6240 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67  e is a btree-pag
e6250 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70  e. The pointer p
e6260 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f 76 65 72  oints at an over
e6270 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20  flow.**         
e6280 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 70            page p
e6290 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6f 6e 65  ointed to by one
e62a0 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f 6e   of the cells on
e62b0 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54   pPage..**.** PT
e62c0 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20  RMAP_OVERFLOW2: 
e62d0 70 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72  pPage is an over
e62e0 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65 20 70  flow-page. The p
e62f0 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74  ointer points at
e6300 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20   the next.**    
e6310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
e6320 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20  verflow page in 
e6330 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61  the list..*/.sta
e6340 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 79 50 61  tic int modifyPa
e6350 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50 61 67  gePointer(MemPag
e6360 65 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f 20 69  e *pPage, Pgno i
e6370 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c 20  From, Pgno iTo, 
e6380 75 38 20 65 54 79 70 65 29 7b 0a 20 20 61 73 73  u8 eType){.  ass
e6390 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
e63a0 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
e63b0 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
e63c0 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
e63d0 50 5f 4f 56 45 52 46 4c 4f 57 32 20 29 7b 0a 20  P_OVERFLOW2 ){. 
e63e0 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65     /* The pointe
e63f0 72 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20  r is always the 
e6400 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6f 66  first 4 bytes of
e6410 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69   the page in thi
e6420 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20  s case.  */.    
e6430 69 66 28 20 67 65 74 34 62 79 74 65 28 70 50 61  if( get4byte(pPa
e6440 67 65 2d 3e 61 44 61 74 61 29 21 3d 69 46 72 6f  ge->aData)!=iFro
e6450 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  m ){.      retur
e6460 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
e6470 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _BKPT;.    }.   
e6480 20 70 75 74 34 62 79 74 65 28 70 50 61 67 65 2d   put4byte(pPage-
e6490 3e 61 44 61 74 61 2c 20 69 54 6f 29 3b 0a 20 20  >aData, iTo);.  
e64a0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69  }else{.    int i
e64b0 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67  sInitOrig = pPag
e64c0 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 20 20 69  e->isInit;.    i
e64d0 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 43  nt i;.    int nC
e64e0 65 6c 6c 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  ell;..    sqlite
e64f0 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70  3BtreeInitPage(p
e6500 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 6e 43  Page, 0);.    nC
e6510 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65  ell = pPage->nCe
e6520 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30  ll;..    for(i=0
e6530 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ; i<nCell; i++){
e6540 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c  .      u8 *pCell
e6550 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67   = findCell(pPag
e6560 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28  e, i);.      if(
e6570 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f   eType==PTRMAP_O
e6580 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 20 20  VERFLOW1 ){.    
e6590 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66      CellInfo inf
e65a0 6f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  o;.        sqlit
e65b0 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
e65c0 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
e65d0 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20  , &info);.      
e65e0 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72    if( info.iOver
e65f0 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20  flow ){.        
e6600 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 67 65 74    if( iFrom==get
e6610 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66  4byte(&pCell[inf
e6620 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 20 29 7b  o.iOverflow]) ){
e6630 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74  .            put
e6640 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66  4byte(&pCell[inf
e6650 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 2c 20 69 54  o.iOverflow], iT
e6660 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  o);.            
e6670 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
e6680 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
e6690 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
e66a0 20 20 69 66 28 20 67 65 74 34 62 79 74 65 28 70    if( get4byte(p
e66b0 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29 7b 0a  Cell)==iFrom ){.
e66c0 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79            put4by
e66d0 74 65 28 70 43 65 6c 6c 2c 20 69 54 6f 29 3b 0a  te(pCell, iTo);.
e66e0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
e66f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
e6700 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
e6710 69 66 28 20 69 3d 3d 6e 43 65 6c 6c 20 29 7b 0a  if( i==nCell ){.
e6720 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 21        if( eType!
e6730 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c  =PTRMAP_BTREE ||
e6740 20 0a 20 20 20 20 20 20 20 20 20 20 67 65 74 34   .          get4
e6750 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
e6760 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
e6770 73 65 74 2b 38 5d 29 21 3d 69 46 72 6f 6d 20 29  set+8])!=iFrom )
e6780 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
e6790 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
e67a0 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20  BKPT;.      }.  
e67b0 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50      put4byte(&pP
e67c0 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
e67d0 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20  ->hdrOffset+8], 
e67e0 69 54 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  iTo);.    }..   
e67f0 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
e6800 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 7d   isInitOrig;.  }
e6810 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
e6820 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d  _OK;.}.../*.** M
e6830 6f 76 65 20 74 68 65 20 6f 70 65 6e 20 64 61 74  ove the open dat
e6840 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61  abase page pDbPa
e6850 67 65 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69  ge to location i
e6860 46 72 65 65 50 61 67 65 20 69 6e 20 74 68 65 20  FreePage in the 
e6870 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68  .** database. Th
e6880 65 20 70 44 62 50 61 67 65 20 72 65 66 65 72 65  e pDbPage refere
e6890 6e 63 65 20 72 65 6d 61 69 6e 73 20 76 61 6c 69  nce remains vali
e68a0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
e68b0 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a 20   relocatePage(. 
e68c0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
e68d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72            /* Btr
e68e0 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ee */.  MemPage 
e68f0 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 20  *pDbPage,       
e6900 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 6f   /* Open page to
e6910 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 54   move */.  u8 eT
e6920 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ype,            
e6930 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d      /* Pointer m
e6940 61 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 20  ap 'type' entry 
e6950 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20  for pDbPage */. 
e6960 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c 20   Pgno iPtrPage, 
e6970 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
e6980 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d 6e  nter map 'page-n
e6990 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62  o' entry for pDb
e69a0 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  Page */.  Pgno i
e69b0 46 72 65 65 50 61 67 65 20 20 20 20 20 20 20 20  FreePage        
e69c0 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69     /* The locati
e69d0 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 61  on to move pDbPa
e69e0 67 65 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 4d 65  ge to */.){.  Me
e69f0 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67 65 3b  mPage *pPtrPage;
e6a00 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74     /* The page t
e6a10 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  hat contains a p
e6a20 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50 61 67  ointer to pDbPag
e6a30 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50  e */.  Pgno iDbP
e6a40 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d 3e 70  age = pDbPage->p
e6a50 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  gno;.  Pager *pP
e6a60 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67  ager = pBt->pPag
e6a70 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  er;.  int rc;.. 
e6a80 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d   assert( eType==
e6a90 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
e6aa0 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41   || eType==PTRMA
e6ab0 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a  P_OVERFLOW1 || .
e6ac0 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 54 52        eType==PTR
e6ad0 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79  MAP_BTREE || eTy
e6ae0 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe==PTRMAP_ROOTP
e6af0 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  AGE );.  assert(
e6b00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
e6b10 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
e6b20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 62  );.  assert( pDb
e6b30 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29  Page->pBt==pBt )
e6b40 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67  ;..  /* Move pag
e6b50 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d 20 69  e iDbPage from i
e6b60 74 27 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 61  t's current loca
e6b70 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d  tion to page num
e6b80 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a 2f  ber iFreePage */
e6b90 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f 56  .  TRACE(("AUTOV
e6ba0 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64  ACUUM: Moving %d
e6bb0 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25 64   to free page %d
e6bc0 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74 79   (ptr page %d ty
e6bd0 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20  pe %d)\n", .    
e6be0 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65    iDbPage, iFree
e6bf0 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c 20  Page, iPtrPage, 
e6c00 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d 20  eType));.  rc = 
e6c10 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65  sqlite3PagerMove
e6c20 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44 62  page(pPager, pDb
e6c30 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 69  Page->pDbPage, i
e6c40 46 72 65 65 50 61 67 65 29 3b 0a 20 20 69 66 28  FreePage);.  if(
e6c50 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
e6c60 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
e6c70 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 2d 3e  .  }.  pDbPage->
e6c80 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 65  pgno = iFreePage
e6c90 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 61  ;..  /* If pDbPa
e6ca0 67 65 20 77 61 73 20 61 20 62 74 72 65 65 2d 70  ge was a btree-p
e6cb0 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 79  age, then it may
e6cc0 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 67 65   have child page
e6cd0 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a 20  s and/or cells. 
e6ce0 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 74   ** that point t
e6cf0 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
e6d00 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  . The pointer ma
e6d10 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c  p entries for al
e6d20 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 67  l these.  ** pag
e6d30 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 63 68  es need to be ch
e6d40 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  anged..  **.  **
e6d50 20 49 66 20 70 44 62 50 61 67 65 20 69 73 20 61   If pDbPage is a
e6d60 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c  n overflow page,
e6d70 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   then the first 
e6d80 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 6f 72  4 bytes may stor
e6d90 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72  e a.  ** pointer
e6da0 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 6e 74   to a subsequent
e6db0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20   overflow page. 
e6dc0 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
e6dd0 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74  ase, then.  ** t
e6de0 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 6e  he pointer map n
e6df0 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74  eeds to be updat
e6e00 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 73 65  ed for the subse
e6e10 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70  quent overflow p
e6e20 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  age..  */.  if( 
e6e30 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54  eType==PTRMAP_BT
e6e40 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54  REE || eType==PT
e6e50 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b  RMAP_ROOTPAGE ){
e6e60 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 68 69  .    rc = setChi
e6e70 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 61 67  ldPtrmaps(pDbPag
e6e80 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
e6e90 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
e6ea0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
e6eb0 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
e6ec0 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 3d   Pgno nextOvfl =
e6ed0 20 67 65 74 34 62 79 74 65 28 70 44 62 50 61 67   get4byte(pDbPag
e6ee0 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 69  e->aData);.    i
e6ef0 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 29  f( nextOvfl!=0 )
e6f00 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  {.      rc = ptr
e6f10 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78 74  mapPut(pBt, next
e6f20 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45  Ovfl, PTRMAP_OVE
e6f30 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 67  RFLOW2, iFreePag
e6f40 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  e);.      if( rc
e6f50 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
e6f60 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
e6f70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
e6f80 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 68    }..  /* Fix th
e6f90 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74  e database point
e6fa0 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 72 50  er on page iPtrP
e6fb0 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 65 64  age that pointed
e6fc0 20 61 74 20 69 44 62 50 61 67 65 20 73 6f 0a 20   at iDbPage so. 
e6fd0 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e   ** that it poin
e6fe0 74 73 20 61 74 20 69 46 72 65 65 50 61 67 65 2e  ts at iFreePage.
e6ff0 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 70 6f   Also fix the po
e7000 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20  inter map entry 
e7010 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 67  for.  ** iPtrPag
e7020 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54  e..  */.  if( eT
e7030 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54  ype!=PTRMAP_ROOT
e7040 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d  PAGE ){.    rc =
e7050 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
e7060 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 61  Page(pBt, iPtrPa
e7070 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30  ge, &pPtrPage, 0
e7080 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
e7090 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e70a0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
e70b0 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
e70c0 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
e70d0 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  trPage->pDbPage)
e70e0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
e70f0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
e7100 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74   releasePage(pPt
e7110 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65  rPage);.      re
e7120 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
e7130 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61     rc = modifyPa
e7140 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61  gePointer(pPtrPa
e7150 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72  ge, iDbPage, iFr
e7160 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a  eePage, eType);.
e7170 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
e7180 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69  pPtrPage);.    i
e7190 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
e71a0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
e71b0 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69 46  trmapPut(pBt, iF
e71c0 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 2c 20  reePage, eType, 
e71d0 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 7d  iPtrPage);.    }
e71e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
e71f0 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  ;.}../* Forward 
e7200 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75  declaration requ
e7210 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 75  ired by incrVacu
e7220 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61  umStep(). */.sta
e7230 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65  tic int allocate
e7240 42 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72  BtreePage(BtShar
e7250 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a  ed *, MemPage **
e7260 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20  , Pgno *, Pgno, 
e7270 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66  u8);../*.** Perf
e7280 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65  orm a single ste
e7290 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e  p of an incremen
e72a0 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73  tal-vacuum. If s
e72b0 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65  uccessful,.** re
e72c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  turn SQLITE_OK. 
e72d0 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77  If there is no w
e72e0 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74  ork to do (and t
e72f0 68 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70  herefore no.** p
e7300 6f 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20  oint in calling 
e7310 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67  this function ag
e7320 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c  ain), return SQL
e7330 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20  ITE_DONE..**.** 
e7340 4d 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c  More specificly,
e7350 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
e7360 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72  ttempts to re-or
e7370 67 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64  ganize the .** d
e7380 61 74 61 62 61 73 65 20 73 6f 20 74 68 61 74 20  atabase so that 
e7390 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66  the last page of
e73a0 20 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e   the file curren
e73b0 74 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73  tly in use.** is
e73c0 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73   no longer in us
e73d0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
e73e0 6e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69  nFin parameter i
e73f0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20  s non-zero, the 
e7400 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61  implementation a
e7410 73 73 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74  ssumes.** that t
e7420 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b  he caller will k
e7430 65 65 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72  eep calling incr
e7440 56 61 63 75 75 6d 53 74 65 70 28 29 20 75 6e 74  VacuumStep() unt
e7450 69 6c 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e 73  il.** it returns
e7460 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20   SQLITE_DONE or 
e7470 61 6e 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68  an error, and th
e7480 61 74 20 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a  at nFin is the.*
e7490 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * number of page
e74a0 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
e74b0 69 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e  ile will contain
e74c0 20 61 66 74 65 72 20 74 68 69 73 20 0a 2a 2a 20   after this .** 
e74d0 70 72 6f 63 65 73 73 20 69 73 20 63 6f 6d 70 6c  process is compl
e74e0 65 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ete..*/.static i
e74f0 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65  nt incrVacuumSte
e7500 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c  p(BtShared *pBt,
e7510 20 50 67 6e 6f 20 6e 46 69 6e 29 7b 0a 20 20 50   Pgno nFin){.  P
e7520 67 6e 6f 20 69 4c 61 73 74 50 67 3b 20 20 20 20  gno iLastPg;    
e7530 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
e7540 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
e7550 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  abase */.  Pgno 
e7560 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 20 20 20  nFreeList;      
e7570 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
e7580 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e  f pages still on
e7590 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2a   the free-list *
e75a0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  /..  assert( sql
e75b0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
e75c0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
e75d0 20 69 4c 61 73 74 50 67 20 3d 20 70 42 74 2d 3e   iLastPg = pBt->
e75e0 6e 54 72 75 6e 63 3b 0a 20 20 69 66 28 20 69 4c  nTrunc;.  if( iL
e75f0 61 73 74 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20  astPg==0 ){.    
e7600 69 4c 61 73 74 50 67 20 3d 20 73 71 6c 69 74 65  iLastPg = sqlite
e7610 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
e7620 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
e7630 7d 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50  }..  if( !PTRMAP
e7640 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61  _ISPAGE(pBt, iLa
e7650 73 74 50 67 29 20 26 26 20 69 4c 61 73 74 50 67  stPg) && iLastPg
e7660 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
e7670 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
e7680 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65  int rc;.    u8 e
e7690 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69  Type;.    Pgno i
e76a0 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46  PtrPage;..    nF
e76b0 72 65 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79  reeList = get4by
e76c0 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
e76d0 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20  >aData[36]);.   
e76e0 20 69 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d   if( nFreeList==
e76f0 30 20 7c 7c 20 6e 46 69 6e 3d 3d 69 4c 61 73 74  0 || nFin==iLast
e7700 50 67 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  Pg ){.      retu
e7710 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
e7720 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
e7730 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69  ptrmapGet(pBt, i
e7740 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c 20  LastPg, &eType, 
e7750 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20  &iPtrPage);.    
e7760 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
e7770 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
e7780 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
e7790 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
e77a0 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20  P_ROOTPAGE ){.  
e77b0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e77c0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
e77d0 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 65      }..    if( e
e77e0 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45  Type==PTRMAP_FRE
e77f0 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 69  EPAGE ){.      i
e7800 66 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20  f( nFin==0 ){.  
e7810 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20        /* Remove 
e7820 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68  the page from th
e7830 65 20 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73  e files free-lis
e7840 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72  t. This is not r
e7850 65 71 75 69 72 65 64 0a 20 20 20 20 20 20 20 20  equired.        
e7860 2a 2a 20 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f  ** if nFin is no
e7870 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74 20  n-zero. In that 
e7880 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 2d 6c  case, the free-l
e7890 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  ist will be.    
e78a0 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 64      ** truncated
e78b0 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 20 74   to zero after t
e78c0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
e78d0 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65 73  urns, so it does
e78e0 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  n't .        ** 
e78f0 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 74 69  matter if it sti
e7900 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65  ll contains some
e7910 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 65 73   garbage entries
e7920 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
e7930 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50       Pgno iFreeP
e7940 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61  g;.        MemPa
e7950 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20  ge *pFreePg;.   
e7960 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
e7970 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
e7980 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65   &pFreePg, &iFre
e7990 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 31 29  ePg, iLastPg, 1)
e79a0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
e79b0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
e79c0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
e79d0 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
e79e0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46        assert( iF
e79f0 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 67 20 29  reePg==iLastPg )
e7a00 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
e7a10 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a  ePage(pFreePg);.
e7a20 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c        }.    } el
e7a30 73 65 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20  se {.      Pgno 
e7a40 69 46 72 65 65 50 67 3b 20 20 20 20 20 20 20 20  iFreePg;        
e7a50 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
e7a60 20 66 72 65 65 20 70 61 67 65 20 74 6f 20 6d 6f   free page to mo
e7a70 76 65 20 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f  ve pLastPg to */
e7a80 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  .      MemPage *
e7a90 70 4c 61 73 74 50 67 3b 0a 0a 20 20 20 20 20 20  pLastPg;..      
e7aa0 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
e7ab0 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c  eGetPage(pBt, iL
e7ac0 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 67 2c  astPg, &pLastPg,
e7ad0 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
e7ae0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
e7af0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
e7b00 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  c;.      }..    
e7b10 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 73 20    /* If nFin is 
e7b20 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f 70 20  zero, this loop 
e7b30 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f 6e 63  runs exactly onc
e7b40 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 73 74  e and page pLast
e7b50 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73  Pg.      ** is s
e7b60 77 61 70 70 65 64 20 77 69 74 68 20 74 68 65 20  wapped with the 
e7b70 66 69 72 73 74 20 66 72 65 65 20 70 61 67 65 20  first free page 
e7b80 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 20 66  pulled off the f
e7b90 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 20 20  ree list..      
e7ba0 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e 20 74  **.      ** On t
e7bb0 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69  he other hand, i
e7bc0 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 74 65  f nFin is greate
e7bd0 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  r than zero, the
e7be0 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a 20  n keep.      ** 
e7bf0 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 20  looping until a 
e7c00 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 74 65  free-page locate
e7c10 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 69 72  d within the fir
e7c20 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a 20 20  st nFin pages.  
e7c30 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69      ** of the fi
e7c40 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 20  le is found..   
e7c50 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b     */.      do {
e7c60 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65  .        MemPage
e7c70 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20   *pFreePg;.     
e7c80 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65     rc = allocate
e7c90 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26  BtreePage(pBt, &
e7ca0 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50  pFreePg, &iFreeP
e7cb0 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  g, 0, 0);.      
e7cc0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
e7cd0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
e7ce0 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61   releasePage(pLa
e7cf0 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20  stPg);.         
e7d00 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
e7d10 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
e7d20 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50  leasePage(pFreeP
e7d30 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65  g);.      }while
e7d40 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 46 72  ( nFin!=0 && iFr
e7d50 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 20 20  eePg>nFin );.   
e7d60 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65     assert( iFree
e7d70 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a 20 20  Pg<iLastPg );.  
e7d80 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20      .      rc = 
e7d90 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
e7da0 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 50 61  e(pLastPg->pDbPa
e7db0 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
e7dc0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
e7dd0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
e7de0 63 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20  c;.      } .    
e7df0 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50    rc = relocateP
e7e00 61 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50 67  age(pBt, pLastPg
e7e10 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67  , eType, iPtrPag
e7e20 65 2c 20 69 46 72 65 65 50 67 29 3b 0a 20 20 20  e, iFreePg);.   
e7e30 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
e7e40 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69  LastPg);.      i
e7e50 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e7e60 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
e7e70 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 20 0a  rn rc;.      } .
e7e80 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 42 74      }.  }..  pBt
e7e90 2d 3e 6e 54 72 75 6e 63 20 3d 20 69 4c 61 73 74  ->nTrunc = iLast
e7ea0 50 67 20 2d 20 31 3b 0a 20 20 77 68 69 6c 65 28  Pg - 1;.  while(
e7eb0 20 70 42 74 2d 3e 6e 54 72 75 6e 63 3d 3d 50 45   pBt->nTrunc==PE
e7ec0 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
e7ed0 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50  pBt)||PTRMAP_ISP
e7ee0 41 47 45 28 70 42 74 2c 20 70 42 74 2d 3e 6e 54  AGE(pBt, pBt->nT
e7ef0 72 75 6e 63 29 20 29 7b 0a 20 20 20 20 70 42 74  runc) ){.    pBt
e7f00 2d 3e 6e 54 72 75 6e 63 2d 2d 3b 0a 20 20 7d 0a  ->nTrunc--;.  }.
e7f10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e7f20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77  OK;.}../*.** A w
e7f30 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
e7f40 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20   must be opened 
e7f50 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74  before calling t
e7f60 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  his function..**
e7f70 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73   It performs a s
e7f80 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f  ingle unit of wo
e7f90 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e  rk towards an in
e7fa0 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
e7fb0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ..**.** If the i
e7fc0 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
e7fd0 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 66  m is finished af
e7fe0 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ter this functio
e7ff0 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51  n has run,.** SQ
e8000 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74  LITE_DONE is ret
e8010 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73 20  urned. If it is 
e8020 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62 75  not finished, bu
e8030 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  t no error occur
e8040 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  ed,.** SQLITE_OK
e8050 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
e8060 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c 69 74  herwise an SQLit
e8070 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a  e error code. .*
e8080 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
e8090 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
e80a0 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65  eIncrVacuum(Btre
e80b0 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
e80c0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
e80d0 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71   = p->pBt;..  sq
e80e0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
e80f0 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42  p);.  assert( pB
e8100 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
e8110 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26  ==TRANS_WRITE &&
e8120 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
e8130 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66  NS_WRITE );.  if
e8140 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  ( !pBt->autoVacu
e8150 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  um ){.    rc = S
e8160 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65  QLITE_DONE;.  }e
e8170 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64  lse{.    invalid
e8180 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61  ateAllOverflowCa
e8190 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63  che(pBt);.    rc
e81a0 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65   = incrVacuumSte
e81b0 70 28 70 42 74 2c 20 30 29 3b 0a 20 20 7d 0a 20  p(pBt, 0);.  }. 
e81c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
e81d0 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
e81e0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
e81f0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
e8200 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c  led prior to sql
e8210 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20  ite3PagerCommit 
e8220 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69  when a transacti
e8230 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 65  on.** is commite
e8240 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61  d for an auto-va
e8250 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a 2a  cuum database..*
e8260 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f  *.** If SQLITE_O
e8270 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74  K is returned, t
e8280 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 20  hen *pnTrunc is 
e8290 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
e82a0 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 68  r of pages.** th
e82b0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
e82c0 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61  should be trunca
e82d0 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 68  ted to during th
e82e0 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73  e commit process
e82f0 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 64  . .** i.e. the d
e8300 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e  atabase has been
e8310 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f 20   reorganized so 
e8320 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 69  that only the fi
e8330 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20  rst *pnTrunc.** 
e8340 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 65  pages are in use
e8350 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e8360 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74  autoVacuumCommit
e8370 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
e8380 50 67 6e 6f 20 2a 70 6e 54 72 75 6e 63 29 7b 0a  Pgno *pnTrunc){.
e8390 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
e83a0 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70  E_OK;.  Pager *p
e83b0 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61  Pager = pBt->pPa
e83c0 67 65 72 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  ger;.#ifndef NDE
e83d0 42 55 47 0a 20 20 69 6e 74 20 6e 52 65 66 20 3d  BUG.  int nRef =
e83e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
e83f0 63 6f 75 6e 74 28 70 50 61 67 65 72 29 3b 0a 23  count(pPager);.#
e8400 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28  endif..  assert(
e8410 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
e8420 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
e8430 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 41  );.  invalidateA
e8440 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  llOverflowCache(
e8450 70 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28 70  pBt);.  assert(p
e8460 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b  Bt->autoVacuum);
e8470 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e 63  .  if( !pBt->inc
e8480 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50  rVacuum ){.    P
e8490 67 6e 6f 20 6e 46 69 6e 20 3d 20 30 3b 0a 0a 20  gno nFin = 0;.. 
e84a0 20 20 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 75     if( pBt->nTru
e84b0 6e 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 50  nc==0 ){.      P
e84c0 67 6e 6f 20 6e 46 72 65 65 3b 0a 20 20 20 20 20  gno nFree;.     
e84d0 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 0a 20   Pgno nPtrmap;. 
e84e0 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 70       const int p
e84f0 67 73 7a 20 3d 20 70 42 74 2d 3e 70 61 67 65 53  gsz = pBt->pageS
e8500 69 7a 65 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20  ize;.      Pgno 
e8510 6e 4f 72 69 67 20 3d 20 73 71 6c 69 74 65 33 50  nOrig = sqlite3P
e8520 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
e8530 74 2d 3e 70 50 61 67 65 72 29 3b 0a 0a 20 20 20  t->pPager);..   
e8540 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 53     if( PTRMAP_IS
e8550 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 29  PAGE(pBt, nOrig)
e8560 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
e8570 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
e8580 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a  T_BKPT;.      }.
e8590 20 20 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3d        if( nOrig=
e85a0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
e85b0 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  GE(pBt) ){.     
e85c0 20 20 20 6e 4f 72 69 67 2d 2d 3b 0a 20 20 20 20     nOrig--;.    
e85d0 20 20 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20    }.      nFree 
e85e0 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d  = get4byte(&pBt-
e85f0 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
e8600 36 5d 29 3b 0a 20 20 20 20 20 20 6e 50 74 72 6d  6]);.      nPtrm
e8610 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69  ap = (nFree-nOri
e8620 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  g+PTRMAP_PAGENO(
e8630 70 42 74 2c 20 6e 4f 72 69 67 29 2b 70 67 73 7a  pBt, nOrig)+pgsz
e8640 2f 35 29 2f 28 70 67 73 7a 2f 35 29 3b 0a 20 20  /5)/(pgsz/5);.  
e8650 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67      nFin = nOrig
e8660 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d   - nFree - nPtrm
e8670 61 70 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f  ap;.      if( nO
e8680 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45  rig>PENDING_BYTE
e8690 5f 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46  _PAGE(pBt) && nF
e86a0 69 6e 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  in<=PENDING_BYTE
e86b0 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20  _PAGE(pBt) ){.  
e86c0 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20        nFin--;.  
e86d0 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c      }.      whil
e86e0 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  e( PTRMAP_ISPAGE
e86f0 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c 20 6e  (pBt, nFin) || n
e8700 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  Fin==PENDING_BYT
e8710 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
e8720 20 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20         nFin--;. 
e8730 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
e8740 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
e8750 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
e8760 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53  rc = incrVacuumS
e8770 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 29 3b 0a  tep(pBt, nFin);.
e8780 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
e8790 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
e87a0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 46  .      assert(nF
e87b0 69 6e 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 6e 54  in==0 || pBt->nT
e87c0 72 75 6e 63 3d 3d 30 20 7c 7c 20 6e 46 69 6e 3c  runc==0 || nFin<
e87d0 3d 70 42 74 2d 3e 6e 54 72 75 6e 63 29 3b 0a 20  =pBt->nTrunc);. 
e87e0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
e87f0 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 66 28 20 70  _OK;.      if( p
e8800 42 74 2d 3e 6e 54 72 75 6e 63 20 29 7b 0a 20 20  Bt->nTrunc ){.  
e8810 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
e8820 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74  e3PagerWrite(pBt
e8830 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  ->pPage1->pDbPag
e8840 65 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34  e);.        put4
e8850 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65  byte(&pBt->pPage
e8860 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 30 29  1->aData[32], 0)
e8870 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79  ;.        put4by
e8880 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
e8890 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 3b 0a  >aData[36], 0);.
e88a0 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72          pBt->nTr
e88b0 75 6e 63 20 3d 20 6e 46 69 6e 3b 0a 20 20 20 20  unc = nFin;.    
e88c0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
e88d0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
e88e0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
e88f0 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50  PagerRollback(pP
e8900 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ager);.    }.  }
e8910 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
e8920 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 6e  TE_OK ){.    *pn
e8930 54 72 75 6e 63 20 3d 20 70 42 74 2d 3e 6e 54 72  Trunc = pBt->nTr
e8940 75 6e 63 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 54  unc;.    pBt->nT
e8950 72 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  runc = 0;.  }.  
e8960 61 73 73 65 72 74 28 20 6e 52 65 66 3d 3d 73 71  assert( nRef==sq
e8970 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
e8980 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  nt(pPager) );.  
e8990 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65  return rc;.}..#e
e89a0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
e89b0 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68   routine does th
e89c0 65 20 66 69 72 73 74 20 70 68 61 73 65 20 6f 66  e first phase of
e89d0 20 61 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d   a two-phase com
e89e0 6d 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69  mit.  This routi
e89f0 6e 65 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72  ne.** causes a r
e8a00 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
e8a10 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 28 69  to be created (i
e8a20 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c  f it does not al
e8a30 72 65 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20  ready exist).** 
e8a40 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69  and populated wi
e8a50 74 68 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d  th enough inform
e8a60 61 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66  ation so that if
e8a70 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63   a power loss oc
e8a80 63 75 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61  curs.** the data
e8a90 62 61 73 65 20 63 61 6e 20 62 65 20 72 65 73 74  base can be rest
e8aa0 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67  ored to its orig
e8ab0 69 6e 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c  inal state by pl
e8ac0 61 79 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68  aying back.** th
e8ad0 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e  e journal.  Then
e8ae0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
e8af0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65   the journal are
e8b00 20 66 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a   flushed out to.
e8b10 2a 2a 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66  ** the disk.  Af
e8b20 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ter the journal 
e8b30 69 73 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69  is safely on oxi
e8b40 64 65 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20  de, the changes 
e8b50 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  to the.** databa
e8b60 73 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 69  se are written i
e8b70 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
e8b80 20 66 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65   file and flushe
e8b90 64 20 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41  d to oxide..** A
e8ba0 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69  t the end of thi
e8bb0 73 20 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c  s call, the roll
e8bc0 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69  back journal sti
e8bd0 6c 6c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65  ll exists on the
e8be0 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20  .** disk and we 
e8bf0 61 72 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e  are still holdin
e8c00 67 20 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20  g all locks, so 
e8c10 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
e8c20 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69  has not.** commi
e8c30 74 74 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74  tted.  See sqlit
e8c40 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 20  e3BtreeCommit() 
e8c50 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70  for the second p
e8c60 68 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63  hase of the.** c
e8c70 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a  ommit process..*
e8c80 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69  *.** This call i
e8c90 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20  s a no-op if no 
e8ca0 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
e8cb0 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  n is currently a
e8cc0 63 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a  ctive on pBt..**
e8cd0 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73  .** Otherwise, s
e8ce0 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65  ync the database
e8cf0 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74   file for the bt
e8d00 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72  ree pBt. zMaster
e8d10 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68   points to.** th
e8d20 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74  e name of a mast
e8d30 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
e8d40 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77  that should be w
e8d50 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a  ritten into the.
e8d60 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f  ** individual jo
e8d70 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69  urnal file, or i
e8d80 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69  s NULL, indicati
e8d90 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  ng no master jou
e8da0 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73  rnal file .** (s
e8db0 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74  ingle database t
e8dc0 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a  ransaction)..**.
e8dd0 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20  ** When this is 
e8de0 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74  called, the mast
e8df0 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c  er journal shoul
e8e00 64 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62  d already have b
e8e10 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20  een.** created, 
e8e20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74  populated with t
e8e30 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  his journal poin
e8e40 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74  ter and synced t
e8e50 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  o disk..**.** On
e8e60 63 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69  ce this is routi
e8e70 6e 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c  ne has returned,
e8e80 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   the only thing 
e8e90 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d  required to comm
e8ea0 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d  it.** the write-
e8eb0 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20  transaction for 
e8ec0 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69  this database fi
e8ed0 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20  le is to delete 
e8ee0 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a  the journal..*/.
e8ef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
e8f00 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
e8f10 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74  ommitPhaseOne(Bt
e8f20 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  ree *p, const ch
e8f30 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
e8f40 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
e8f50 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54  OK;.  if( p->inT
e8f60 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
e8f70 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65  E ){.    BtShare
e8f80 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
e8f90 0a 20 20 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63  .    Pgno nTrunc
e8fa0 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
e8fb0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
e8fc0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
e8fd0 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
e8fe0 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
e8ff0 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20  Vacuum ){.      
e9000 72 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43  rc = autoVacuumC
e9010 6f 6d 6d 69 74 28 70 42 74 2c 20 26 6e 54 72 75  ommit(pBt, &nTru
e9020 6e 63 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20  nc); .      if( 
e9030 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
e9040 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
e9050 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
e9060 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
e9070 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
e9080 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20  #endif.    rc = 
e9090 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
e90a0 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e  itPhaseOne(pBt->
e90b0 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c  pPager, zMaster,
e90c0 20 6e 54 72 75 6e 63 29 3b 0a 20 20 20 20 73 71   nTrunc);.    sq
e90d0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
e90e0 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p);.  }.  return
e90f0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   rc;.}../*.** Co
e9100 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63  mmit the transac
e9110 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69  tion currently i
e9120 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a  n progress..**.*
e9130 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
e9140 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65  mplements the se
e9150 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20  cond phase of a 
e9160 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20  2-phase commit. 
e9170 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42   The.** sqlite3B
e9180 74 72 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69  treeSync() routi
e9190 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73  ne does the firs
e91a0 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75  t phase and shou
e91b0 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a  ld be invoked.**
e91c0 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
e91d0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  g this routine. 
e91e0 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 65   The sqlite3Btre
e91f0 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 20  eSync() routine 
e9200 64 69 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 77  did.** all the w
e9210 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69  ork of writing i
e9220 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74  nformation out t
e9230 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68  o disk and flush
e9240 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65  ing the.** conte
e9250 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79  nts so that they
e9260 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74   are written ont
e9270 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74  o the disk platt
e9280 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a  er.  All this.**
e9290 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20   routine has to 
e92a0 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20  do is delete or 
e92b0 74 72 75 6e 63 61 74 65 20 74 68 65 20 72 6f 6c  truncate the rol
e92c0 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  lback journal.**
e92d0 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 74   (which causes t
e92e0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  he transaction t
e92f0 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 72  o commit) and dr
e9300 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  op locks..**.** 
e9310 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73  This will releas
e9320 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b  e the write lock
e9330 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
e9340 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65   file.  If there
e9350 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76  .** are no activ
e9360 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c  e cursors, it al
e9370 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20  so releases the 
e9380 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51  read lock..*/.SQ
e9390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
e93a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
e93b0 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65  mitPhaseTwo(Btre
e93c0 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65  e *p){.  BtShare
e93d0 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
e93e0 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
e93f0 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 72 65  Enter(p);.  btre
e9400 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a  eIntegrity(p);..
e9410 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64    /* If the hand
e9420 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d 74  le has a write-t
e9430 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c  ransaction open,
e9440 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61 72   commit the shar
e9450 65 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a 20  ed-btrees .  ** 
e9460 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20  transaction and 
e9470 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20 73  set the shared s
e9480 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52 45  tate to TRANS_RE
e9490 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  AD..  */.  if( p
e94a0 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
e94b0 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 6e  _WRITE ){.    in
e94c0 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74  t rc;.    assert
e94d0 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ( pBt->inTransac
e94e0 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54  tion==TRANS_WRIT
e94f0 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  E );.    assert(
e9500 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69   pBt->nTransacti
e9510 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d  on>0 );.    rc =
e9520 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
e9530 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 2d  mitPhaseTwo(pBt-
e9540 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66  >pPager);.    if
e9550 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
e9560 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
e9570 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
e9580 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
e9590 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 69      }.    pBt->i
e95a0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54  nTransaction = T
e95b0 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 20 20 70  RANS_READ;.    p
e95c0 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a  Bt->inStmt = 0;.
e95d0 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b 41 6c 6c 54    }.  unlockAllT
e95e0 61 62 6c 65 73 28 70 29 3b 0a 0a 20 20 2f 2a 20  ables(p);..  /* 
e95f0 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61  If the handle ha
e9600 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72  s any kind of tr
e9610 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20  ansaction open, 
e9620 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 74 72  decrement the tr
e9630 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63  ansaction.  ** c
e9640 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 61 72  ount of the shar
e9650 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 68 65  ed btree. If the
e9660 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75   transaction cou
e9670 6e 74 20 72 65 61 63 68 65 73 20 30 2c 20 73 65  nt reaches 0, se
e9680 74 0a 20 20 2a 2a 20 74 68 65 20 73 68 61 72 65  t.  ** the share
e9690 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53  d state to TRANS
e96a0 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e 6c 6f 63  _NONE. The unloc
e96b0 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 29  kBtreeIfUnused()
e96c0 20 63 61 6c 6c 20 62 65 6c 6f 77 0a 20 20 2a 2a   call below.  **
e96d0 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 68 65   will unlock the
e96e0 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 69   pager..  */.  i
e96f0 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54  f( p->inTrans!=T
e9700 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20  RANS_NONE ){.   
e9710 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69   pBt->nTransacti
e9720 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 30 3d  on--;.    if( 0=
e9730 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69  =pBt->nTransacti
e9740 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d  on ){.      pBt-
e9750 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d  >inTransaction =
e9760 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20   TRANS_NONE;.   
e9770 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74   }.  }..  /* Set
e9780 20 74 68 65 20 68 61 6e 64 6c 65 73 20 63 75 72   the handles cur
e9790 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
e97a0 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f   state to TRANS_
e97b0 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 6b 0a  NONE and unlock.
e97c0 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72 20 69    ** the pager i
e97d0 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f 73  f this call clos
e97e0 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 64  ed the only read
e97f0 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61   or write transa
e9800 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 70 2d  ction..  */.  p-
e9810 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53  >inTrans = TRANS
e9820 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42  _NONE;.  unlockB
e9830 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74  treeIfUnused(pBt
e9840 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67  );..  btreeInteg
e9850 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74  rity(p);.  sqlit
e9860 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
e9870 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
e9880 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f  _OK;.}../*.** Do
e9890 20 62 6f 74 68 20 70 68 61 73 65 73 20 6f 66 20   both phases of 
e98a0 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c  a commit..*/.SQL
e98b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
e98c0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
e98d0 69 74 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  it(Btree *p){.  
e98e0 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
e98f0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
e9900 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
e9910 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  reeCommitPhaseOn
e9920 65 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  e(p, 0);.  if( r
e9930 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
e9940 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
e9950 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
e9960 54 77 6f 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71  Two(p);.  }.  sq
e9970 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
e9980 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
e9990 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  .}..#ifndef NDEB
e99a0 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  UG./*.** Return 
e99b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 72  the number of wr
e99c0 69 74 65 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e  ite-cursors open
e99d0 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c 65 2e   on this handle.
e99e0 20 54 68 69 73 20 69 73 20 66 6f 72 20 75 73 65   This is for use
e99f0 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 29 20  .** in assert() 
e9a00 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20  expressions, so 
e9a10 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69  it is only compi
e9a20 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20 69 73  led if NDEBUG is
e9a30 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e   not.** defined.
e9a40 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70  .**.** For the p
e9a50 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20  urposes of this 
e9a60 72 6f 75 74 69 6e 65 2c 20 61 20 77 72 69 74 65  routine, a write
e9a70 2d 63 75 72 73 6f 72 20 69 73 20 61 6e 79 20 63  -cursor is any c
e9a80 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73  ursor that.** is
e9a90 20 63 61 70 61 62 6c 65 20 6f 66 20 77 72 69 74   capable of writ
e9aa0 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62  ing to the datab
e9ab0 73 65 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 20  se.  That means 
e9ac0 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 0a 2a  the cursor was.*
e9ad0 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65  * originally ope
e9ae0 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20  ned for writing 
e9af0 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 68  and the cursor h
e9b00 61 73 20 6e 6f 74 20 62 65 20 64 69 73 61 62 6c  as not be disabl
e9b10 65 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e 67 20  ed.** by having 
e9b20 69 74 73 20 73 74 61 74 65 20 63 68 61 6e 67 65  its state change
e9b30 64 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c  d to CURSOR_FAUL
e9b40 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  T..*/.static int
e9b50 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f   countWriteCurso
e9b60 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  rs(BtShared *pBt
e9b70 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ){.  BtCursor *p
e9b80 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30  Cur;.  int r = 0
e9b90 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42 74  ;.  for(pCur=pBt
e9ba0 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72 3b  ->pCursor; pCur;
e9bb0 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78   pCur=pCur->pNex
e9bc0 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72  t){.    if( pCur
e9bd0 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 43 75 72  ->wrFlag && pCur
e9be0 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52  ->eState!=CURSOR
e9bf0 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20  _FAULT ) r++; . 
e9c00 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d   }.  return r;.}
e9c10 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
e9c20 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
e9c30 20 74 68 65 20 73 74 61 74 65 20 74 6f 20 43 55   the state to CU
e9c40 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 20 74  RSOR_FAULT and t
e9c50 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65  he error.** code
e9c60 20 74 6f 20 65 72 72 43 6f 64 65 20 66 6f 72 20   to errCode for 
e9c70 65 76 65 72 79 20 63 75 72 73 6f 72 20 6f 6e 20  every cursor on 
e9c80 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70 42  BtShared that pB
e9c90 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63  tree.** referenc
e9ca0 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  es..**.** Every 
e9cb0 63 75 72 73 6f 72 20 69 73 20 74 72 69 70 70 65  cursor is trippe
e9cc0 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 72  d, including cur
e9cd0 73 6f 72 73 20 74 68 61 74 20 62 65 6c 6f 6e 67  sors that belong
e9ce0 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64 61 74  .** to other dat
e9cf0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
e9d00 73 20 74 68 61 74 20 68 61 70 70 65 6e 20 74 6f  s that happen to
e9d10 20 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74   be sharing.** t
e9d20 68 65 20 63 61 63 68 65 20 77 69 74 68 20 70 42  he cache with pB
e9d30 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tree..**.** This
e9d40 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63 61   routine gets ca
e9d50 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c  lled when a roll
e9d60 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20  back occurs..** 
e9d70 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 73 69 6e  All cursors usin
e9d80 67 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 65  g the same cache
e9d90 20 6d 75 73 74 20 62 65 20 74 72 69 70 70 65 64   must be tripped
e9da0 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 74  .** to prevent t
e9db0 68 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e 67 20  hem from trying 
e9dc0 74 6f 20 75 73 65 20 74 68 65 20 62 74 72 65 65  to use the btree
e9dd0 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f   after.** the ro
e9de0 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c  llback.  The rol
e9df0 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64  lback may have d
e9e00 65 6c 65 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a  eleted tables.**
e9e10 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70   or moved root p
e9e20 61 67 65 73 2c 20 73 6f 20 69 74 20 69 73 20 6e  ages, so it is n
e9e30 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f  ot sufficient to
e9e40 0a 2a 2a 20 73 61 76 65 20 74 68 65 20 73 74 61  .** save the sta
e9e50 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72  te of the cursor
e9e60 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75  .  The cursor mu
e9e70 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64  st be.** invalid
e9e80 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
e9e90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
e9ea0 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c  ite3BtreeTripAll
e9eb0 43 75 72 73 6f 72 73 28 42 74 72 65 65 20 2a 70  Cursors(Btree *p
e9ec0 42 74 72 65 65 2c 20 69 6e 74 20 65 72 72 43 6f  Btree, int errCo
e9ed0 64 65 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20  de){.  BtCursor 
e9ee0 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  *p;.  sqlite3Btr
e9ef0 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b  eeEnter(pBtree);
e9f00 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65 65 2d  .  for(p=pBtree-
e9f10 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70  >pBt->pCursor; p
e9f20 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  ; p=p->pNext){. 
e9f30 20 20 20 63 6c 65 61 72 43 75 72 73 6f 72 50 6f     clearCursorPo
e9f40 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 70  sition(p);.    p
e9f50 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
e9f60 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20 70 2d 3e  R_FAULT;.    p->
e9f70 73 6b 69 70 20 3d 20 65 72 72 43 6f 64 65 3b 0a  skip = errCode;.
e9f80 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
e9f90 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b  eeLeave(pBtree);
e9fa0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61  .}../*.** Rollba
e9fb0 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ck the transacti
e9fc0 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 20  on in progress. 
e9fd0 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 69 6c   All cursors wil
e9fe0 6c 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 65  l be.** invalide
e9ff0 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74  d by this operat
ea000 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70  ion.  Any attemp
ea010 74 20 74 6f 20 75 73 65 20 61 20 63 75 72 73 6f  t to use a curso
ea020 72 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 6f 70  r.** that was op
ea030 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  en at the beginn
ea040 69 6e 67 20 6f 66 20 74 68 69 73 20 6f 70 65 72  ing of this oper
ea050 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c  ation will resul
ea060 74 0a 2a 2a 20 69 6e 20 61 6e 20 65 72 72 6f 72  t.** in an error
ea070 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c  ..**.** This wil
ea080 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72  l release the wr
ea090 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ite lock on the 
ea0a0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
ea0b0 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20  If there.** are 
ea0c0 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72  no active cursor
ea0d0 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61  s, it also relea
ea0e0 73 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63  ses the read loc
ea0f0 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  k..*/.SQLITE_PRI
ea100 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ea110 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74  BtreeRollback(Bt
ea120 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ree *p){.  int r
ea130 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  c;.  BtShared *p
ea140 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d  Bt = p->pBt;.  M
ea150 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a  emPage *pPage1;.
ea160 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
ea170 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
ea180 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70  saveAllCursors(p
ea190 42 74 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64  Bt, 0, 0);.#ifnd
ea1a0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
ea1b0 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66  HARED_CACHE.  if
ea1c0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
ea1d0 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69  ){.    /* This i
ea1e0 73 20 61 20 68 6f 72 72 69 62 6c 65 20 73 69 74  s a horrible sit
ea1f0 75 61 74 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72  uation. An IO or
ea200 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20   malloc() error 
ea210 6f 63 63 75 72 65 64 20 77 68 69 6c 73 74 0a 20  occured whilst. 
ea220 20 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20     ** trying to 
ea230 73 61 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69  save cursor posi
ea240 74 69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69  tions. If this i
ea250 73 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72  s an automatic r
ea260 6f 6c 6c 62 61 63 6b 20 28 61 73 0a 20 20 20 20  ollback (as.    
ea270 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  ** the result of
ea280 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d   a constraint, m
ea290 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20  alloc() failure 
ea2a0 6f 72 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65  or IO error) the
ea2b0 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61  n .    ** the ca
ea2c0 63 68 65 20 6d 61 79 20 62 65 20 69 6e 74 65 72  che may be inter
ea2d0 6e 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65  nally inconsiste
ea2e0 6e 74 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  nt (not contain 
ea2f0 76 61 6c 69 64 20 74 72 65 65 73 29 20 73 6f 0a  valid trees) so.
ea300 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 6e 6f 74      ** we cannot
ea310 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74   simply return t
ea320 68 65 20 65 72 72 6f 72 20 74 6f 20 74 68 65 20  he error to the 
ea330 63 61 6c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c  caller. Instead,
ea340 20 61 62 6f 72 74 20 0a 20 20 20 20 2a 2a 20 61   abort .    ** a
ea350 6c 6c 20 71 75 65 72 69 65 73 20 74 68 61 74 20  ll queries that 
ea360 6d 61 79 20 62 65 20 75 73 69 6e 67 20 61 6e 79  may be using any
ea370 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 73 20   of the cursors 
ea380 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 20 73  that failed to s
ea390 61 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ave..    */.    
ea3a0 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70  sqlite3BtreeTrip
ea3b0 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 72 63  AllCursors(p, rc
ea3c0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
ea3d0 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
ea3e0 29 3b 0a 20 20 75 6e 6c 6f 63 6b 41 6c 6c 54 61  );.  unlockAllTa
ea3f0 62 6c 65 73 28 70 29 3b 0a 0a 20 20 69 66 28 20  bles(p);..  if( 
ea400 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
ea410 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69  S_WRITE ){.    i
ea420 6e 74 20 72 63 32 3b 0a 0a 23 69 66 6e 64 65 66  nt rc2;..#ifndef
ea430 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
ea440 4f 56 41 43 55 55 4d 0a 20 20 20 20 70 42 74 2d  OVACUUM.    pBt-
ea450 3e 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 23 65 6e  >nTrunc = 0;.#en
ea460 64 69 66 0a 0a 20 20 20 20 61 73 73 65 72 74 28  dif..    assert(
ea470 20 54 52 41 4e 53 5f 57 52 49 54 45 3d 3d 70 42   TRANS_WRITE==pB
ea480 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
ea490 20 29 3b 0a 20 20 20 20 72 63 32 20 3d 20 73 71   );.    rc2 = sq
ea4a0 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
ea4b0 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  ck(pBt->pPager);
ea4c0 0a 20 20 20 20 69 66 28 20 72 63 32 21 3d 53 51  .    if( rc2!=SQ
ea4d0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
ea4e0 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d   rc = rc2;.    }
ea4f0 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6c  ..    /* The rol
ea500 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64  lback may have d
ea510 65 73 74 72 6f 79 65 64 20 74 68 65 20 70 50 61  estroyed the pPa
ea520 67 65 31 2d 3e 61 44 61 74 61 20 76 61 6c 75 65  ge1->aData value
ea530 2e 20 20 53 6f 0a 20 20 20 20 2a 2a 20 63 61 6c  .  So.    ** cal
ea540 6c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  l sqlite3BtreeGe
ea550 74 50 61 67 65 28 29 20 6f 6e 20 70 61 67 65 20  tPage() on page 
ea560 31 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a  1 again to make.
ea570 20 20 20 20 2a 2a 20 73 75 72 65 20 70 50 61 67      ** sure pPag
ea580 65 31 2d 3e 61 44 61 74 61 20 69 73 20 73 65 74  e1->aData is set
ea590 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20   correctly. */. 
ea5a0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
ea5b0 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
ea5c0 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3d 3d  1, &pPage1, 0)==
ea5d0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
ea5e0 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
ea5f0 50 61 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20  Page1);.    }.  
ea600 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74 57    assert( countW
ea610 72 69 74 65 43 75 72 73 6f 72 73 28 70 42 74 29  riteCursors(pBt)
ea620 3d 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e  ==0 );.    pBt->
ea630 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
ea640 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a  TRANS_READ;.  }.
ea650 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
ea660 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b  s!=TRANS_NONE ){
ea670 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74  .    assert( pBt
ea680 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30  ->nTransaction>0
ea690 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 54 72   );.    pBt->nTr
ea6a0 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20  ansaction--;.   
ea6b0 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72   if( 0==pBt->nTr
ea6c0 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20  ansaction ){.   
ea6d0 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
ea6e0 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f  ction = TRANS_NO
ea6f0 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  NE;.    }.  }.. 
ea700 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52   p->inTrans = TR
ea710 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 42 74 2d  ANS_NONE;.  pBt-
ea720 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 75  >inStmt = 0;.  u
ea730 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
ea740 65 64 28 70 42 74 29 3b 0a 0a 20 20 62 74 72 65  ed(pBt);..  btre
ea750 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20  eIntegrity(p);. 
ea760 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
ea770 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
ea780 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61  rc;.}../*.** Sta
ea790 72 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 73  rt a statement s
ea7a0 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  ubtransaction.  
ea7b0 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69  The subtransacti
ea7c0 6f 6e 20 63 61 6e 0a 2a 2a 20 63 61 6e 20 62 65  on can.** can be
ea7d0 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 6e 64   rolled back ind
ea7e0 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66 20 74 68  ependently of th
ea7f0 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69  e main transacti
ea800 6f 6e 2e 0a 2a 2a 20 59 6f 75 20 6d 75 73 74 20  on..** You must 
ea810 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74  start a transact
ea820 69 6f 6e 20 62 65 66 6f 72 65 20 73 74 61 72 74  ion before start
ea830 69 6e 67 20 61 20 73 75 62 74 72 61 6e 73 61 63  ing a subtransac
ea840 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 73 75 62  tion..** The sub
ea850 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65  transaction is e
ea860 6e 64 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  nded automatical
ea870 6c 79 20 69 66 20 74 68 65 20 6d 61 69 6e 20 74  ly if the main t
ea880 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 63 6f  ransaction.** co
ea890 6d 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73 20 62  mmits or rolls b
ea8a0 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  ack..**.** Only 
ea8b0 6f 6e 65 20 73 75 62 74 72 61 6e 73 61 63 74 69  one subtransacti
ea8c0 6f 6e 20 6d 61 79 20 62 65 20 61 63 74 69 76 65  on may be active
ea8d0 20 61 74 20 61 20 74 69 6d 65 2e 20 20 49 74 20   at a time.  It 
ea8e0 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 74  is an error to t
ea8f0 72 79 0a 2a 2a 20 74 6f 20 73 74 61 72 74 20 61  ry.** to start a
ea900 20 6e 65 77 20 73 75 62 74 72 61 6e 73 61 63 74   new subtransact
ea910 69 6f 6e 20 69 66 20 61 6e 6f 74 68 65 72 20 73  ion if another s
ea920 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  ubtransaction is
ea930 20 61 6c 72 65 61 64 79 20 61 63 74 69 76 65 2e   already active.
ea940 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74  .**.** Statement
ea950 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 73   subtransactions
ea960 20 61 72 65 20 75 73 65 64 20 61 72 6f 75 6e 64   are used around
ea970 20 69 6e 64 69 76 69 64 75 61 6c 20 53 51 4c 20   individual SQL 
ea980 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68  statements.** th
ea990 61 74 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64  at are contained
ea9a0 20 77 69 74 68 69 6e 20 61 20 42 45 47 49 4e 2e   within a BEGIN.
ea9b0 2e 2e 43 4f 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20  ..COMMIT block. 
ea9c0 20 49 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74   If a constraint
ea9d0 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73  .** error occurs
ea9e0 20 77 69 74 68 69 6e 20 74 68 65 20 73 74 61 74   within the stat
ea9f0 65 6d 65 6e 74 2c 20 74 68 65 20 65 66 66 65 63  ement, the effec
eaa00 74 20 6f 66 20 74 68 61 74 20 6f 6e 65 20 73 74  t of that one st
eaa10 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62  atement.** can b
eaa20 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 77 69  e rolled back wi
eaa30 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
eaa40 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 65 6e 74  rollback the ent
eaa50 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ire transaction.
eaa60 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
eaa70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
eaa80 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72  reeBeginStmt(Btr
eaa90 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  ee *p){.  int rc
eaaa0 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
eaab0 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71  t = p->pBt;.  sq
eaac0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
eaad0 70 29 3b 0a 20 20 69 66 28 20 28 70 2d 3e 69 6e  p);.  if( (p->in
eaae0 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49  Trans!=TRANS_WRI
eaaf0 54 45 29 20 7c 7c 20 70 42 74 2d 3e 69 6e 53 74  TE) || pBt->inSt
eab00 6d 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  mt ){.    rc = p
eab10 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 53  Bt->readOnly ? S
eab20 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 3a  QLITE_READONLY :
eab30 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
eab40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
eab50 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73  rt( pBt->inTrans
eab60 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
eab70 49 54 45 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ITE );.    rc = 
eab80 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20  pBt->readOnly ? 
eab90 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 73 71 6c 69  SQLITE_OK : sqli
eaba0 74 65 33 50 61 67 65 72 53 74 6d 74 42 65 67 69  te3PagerStmtBegi
eabb0 6e 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  n(pBt->pPager);.
eabc0 20 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20      pBt->inStmt 
eabd0 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 1;.  }.  sqlit
eabe0 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
eabf0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
eac00 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74  ../*.** Commit t
eac10 68 65 20 73 74 61 74 6d 65 6e 74 20 73 75 62 74  he statment subt
eac20 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 65  ransaction curre
eac30 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73 73  ntly in progress
eac40 2e 20 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 62 74  .  If no.** subt
eac50 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
eac60 74 69 76 65 2c 20 74 68 69 73 20 69 73 20 61 20  tive, this is a 
eac70 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45  no-op..*/.SQLITE
eac80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
eac90 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53  ite3BtreeCommitS
eaca0 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  tmt(Btree *p){. 
eacb0 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61   int rc;.  BtSha
eacc0 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
eacd0 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  t;.  sqlite3Btre
eace0 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28  eEnter(p);.  if(
eacf0 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 26 26 20   pBt->inStmt && 
ead00 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29  !pBt->readOnly )
ead10 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
ead20 65 33 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69  e3PagerStmtCommi
ead30 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  t(pBt->pPager);.
ead40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
ead50 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
ead60 0a 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d  .  pBt->inStmt =
ead70 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   0;.  sqlite3Btr
ead80 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
ead90 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
eada0 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 61  * Rollback the a
eadb0 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 20  ctive statement 
eadc0 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  subtransaction. 
eadd0 20 49 66 20 6e 6f 20 73 75 62 74 72 61 6e 73 61   If no subtransa
eade0 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 63 74 69  ction.** is acti
eadf0 76 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ve this routine 
eae00 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
eae10 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 69  * All cursors wi
eae20 6c 6c 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  ll be invalidate
eae30 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74  d by this operat
eae40 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70  ion.  Any attemp
eae50 74 0a 2a 2a 20 74 6f 20 75 73 65 20 61 20 63 75  t.** to use a cu
eae60 72 73 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70  rsor that was op
eae70 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  en at the beginn
eae80 69 6e 67 20 6f 66 20 74 68 69 73 20 6f 70 65 72  ing of this oper
eae90 61 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 72 65  ation.** will re
eaea0 73 75 6c 74 20 69 6e 20 61 6e 20 65 72 72 6f 72  sult in an error
eaeb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
eaec0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
eaed0 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74  treeRollbackStmt
eaee0 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e  (Btree *p){.  in
eaef0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
eaf00 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
eaf10 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71  t = p->pBt;.  sq
eaf20 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
eaf30 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 4d 61 6c  p);.  sqlite3Mal
eaf40 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 3b 0a 20  locDisallow();. 
eaf50 20 69 66 28 20 70 42 74 2d 3e 69 6e 53 74 6d 74   if( pBt->inStmt
eaf60 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e   && !pBt->readOn
eaf70 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  ly ){.    rc = s
eaf80 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 52  qlite3PagerStmtR
eaf90 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 61  ollback(pBt->pPa
eafa0 67 65 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ger);.    assert
eafb0 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73  ( countWriteCurs
eafc0 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20  ors(pBt)==0 );. 
eafd0 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d     pBt->inStmt =
eafe0 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
eaff0 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 3b 0a  3MallocAllow();.
eb000 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
eb010 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
eb020 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65   rc;.}../*.** De
eb030 66 61 75 6c 74 20 6b 65 79 20 63 6f 6d 70 61 72  fault key compar
eb040 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ison function to
eb050 20 62 65 20 75 73 65 64 20 69 66 20 6e 6f 20 63   be used if no c
eb060 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
eb070 6f 6e 0a 2a 2a 20 69 73 20 73 70 65 63 69 66 69  on.** is specifi
eb080 65 64 20 6f 6e 20 74 68 65 20 73 71 6c 69 74 65  ed on the sqlite
eb090 33 42 74 72 65 65 43 75 72 73 6f 72 28 29 20 63  3BtreeCursor() c
eb0a0 61 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  all..*/.static i
eb0b0 6e 74 20 64 66 6c 74 43 6f 6d 70 61 72 65 28 0a  nt dfltCompare(.
eb0c0 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c    void *NotUsed,
eb0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
eb0e0 55 73 65 72 20 64 61 74 61 20 69 73 20 6e 6f 74  User data is not
eb0f0 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e   used */.  int n
eb100 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  1, const void *p
eb110 31 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6b  1,    /* First k
eb120 65 79 20 74 6f 20 63 6f 6d 70 61 72 65 20 2a 2f  ey to compare */
eb130 0a 20 20 69 6e 74 20 6e 32 2c 20 63 6f 6e 73 74  .  int n2, const
eb140 20 76 6f 69 64 20 2a 70 32 20 20 20 20 20 2f 2a   void *p2     /*
eb150 20 53 65 63 6f 6e 64 20 6b 65 79 20 74 6f 20 63   Second key to c
eb160 6f 6d 70 61 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  ompare */.){.  i
eb170 6e 74 20 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63  nt c;.  c = memc
eb180 6d 70 28 70 31 2c 20 70 32 2c 20 6e 31 3c 6e 32  mp(p1, p2, n1<n2
eb190 20 3f 20 6e 31 20 3a 20 6e 32 29 3b 0a 20 20 69   ? n1 : n2);.  i
eb1a0 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63  f( c==0 ){.    c
eb1b0 20 3d 20 6e 31 20 2d 20 6e 32 3b 0a 20 20 7d 0a   = n1 - n2;.  }.
eb1c0 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f    return c;.}../
eb1d0 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
eb1e0 77 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65  w cursor for the
eb1f0 20 42 54 72 65 65 20 77 68 6f 73 65 20 72 6f 6f   BTree whose roo
eb200 74 20 69 73 20 6f 6e 20 74 68 65 20 70 61 67 65  t is on the page
eb210 0a 2a 2a 20 69 54 61 62 6c 65 2e 20 20 54 68 65  .** iTable.  The
eb220 20 61 63 74 20 6f 66 20 61 63 71 75 69 72 69 6e   act of acquirin
eb230 67 20 61 20 63 75 72 73 6f 72 20 67 65 74 73 20  g a cursor gets 
eb240 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a  a read lock on .
eb250 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
eb260 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77  file..**.** If w
eb270 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74  rFlag==0, then t
eb280 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e  he cursor can on
eb290 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72 20 72  ly be used for r
eb2a0 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72  eading..** If wr
eb2b0 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68  Flag==1, then th
eb2c0 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20  e cursor can be 
eb2d0 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67  used for reading
eb2e0 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69   or for.** writi
eb2f0 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64  ng if other cond
eb300 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69  itions for writi
eb310 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e  ng are also met.
eb320 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74    These.** are t
eb330 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68  he conditions th
eb340 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69  at must be met i
eb350 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74  n order for writ
eb360 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c  ing to.** be all
eb370 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20  owed:.**.** 1:  
eb380 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20  The cursor must 
eb390 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64  have been opened
eb3a0 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a   with wrFlag==1.
eb3b0 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20  **.** 2:  Other 
eb3c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
eb3d0 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72 65 20  ions that share 
eb3e0 74 68 65 20 73 61 6d 65 20 70 61 67 65 72 20 63  the same pager c
eb3f0 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20  ache.**     but 
eb400 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e  which are not in
eb410 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d   the READ_UNCOMM
eb420 49 54 54 45 44 20 73 74 61 74 65 20 6d 61 79 20  ITTED state may 
eb430 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20  not have.**     
eb440 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74  cursors open wit
eb450 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74  h wrFlag==0 on t
eb460 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20  he same table.  
eb470 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20  Otherwise.**    
eb480 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64   the changes mad
eb490 65 20 62 79 20 74 68 69 73 20 77 72 69 74 65 20  e by this write 
eb4a0 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20  cursor would be 
eb4b0 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20  visible to.**   
eb4c0 20 20 74 68 65 20 72 65 61 64 20 63 75 72 73 6f    the read curso
eb4d0 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20  rs in the other 
eb4e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
eb4f0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54  ion..**.** 3:  T
eb500 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74  he database must
eb510 20 62 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f   be writable (no
eb520 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d  t on read-only m
eb530 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20  edia).**.** 4:  
eb540 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e  There must be an
eb550 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74   active transact
eb560 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68  ion..**.** No ch
eb570 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74  ecking is done t
eb580 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
eb590 20 70 61 67 65 20 69 54 61 62 6c 65 20 72 65 61   page iTable rea
eb5a0 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f  lly is the.** ro
eb5b0 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74  ot page of a b-t
eb5c0 72 65 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e  ree.  If it is n
eb5d0 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  ot, then the cur
eb5e0 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20  sor acquired.** 
eb5f0 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f  will not work co
eb600 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  rrectly..**.** T
eb610 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75  he comparison fu
eb620 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6c  nction must be l
eb630 6f 67 69 63 61 6c 6c 79 20 74 68 65 20 73 61 6d  ogically the sam
eb640 65 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 73  e for every curs
eb650 6f 72 0a 2a 2a 20 6f 6e 20 61 20 70 61 72 74 69  or.** on a parti
eb660 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 20 43 68  cular table.  Ch
eb670 61 6e 67 69 6e 67 20 74 68 65 20 63 6f 6d 70 61  anging the compa
eb680 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 77  rison function w
eb690 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e  ill result.** in
eb6a0 20 69 6e 63 6f 72 72 65 63 74 20 6f 70 65 72 61   incorrect opera
eb6b0 74 69 6f 6e 73 2e 20 20 49 66 20 74 68 65 20 63  tions.  If the c
eb6c0 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
eb6d0 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 61 0a 2a 2a  on is NULL, a.**
eb6e0 20 64 65 66 61 75 6c 74 20 63 6f 6d 70 61 72 69   default compari
eb6f0 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  son function is 
eb700 75 73 65 64 2e 20 20 54 68 65 20 63 6f 6d 70 61  used.  The compa
eb710 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 69  rison function i
eb720 73 0a 2a 2a 20 61 6c 77 61 79 73 20 69 67 6e 6f  s.** always igno
eb730 72 65 64 20 66 6f 72 20 49 4e 54 4b 45 59 20 74  red for INTKEY t
eb740 61 62 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ables..*/.static
eb750 20 69 6e 74 20 62 74 72 65 65 43 75 72 73 6f 72   int btreeCursor
eb760 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20  (.  Btree *p,   
eb770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb790 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a  /* The btree */.
eb7a0 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
eb7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
eb7d0 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61   Root page of ta
eb7e0 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20  ble to open */. 
eb7f0 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20   int wrFlag,    
eb800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb810 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
eb820 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65  1 to write. 0 re
eb830 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 69 6e 74  ad-only */.  int
eb840 20 28 2a 78 43 6d 70 29 28 76 6f 69 64 2a 2c 69   (*xCmp)(void*,i
eb850 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
eb860 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c  nt,const void*),
eb870 20 2f 2a 20 4b 65 79 20 43 6f 6d 70 61 72 69 73   /* Key Comparis
eb880 6f 6e 20 66 75 6e 63 20 2a 2f 0a 20 20 76 6f 69  on func */.  voi
eb890 64 20 2a 70 41 72 67 2c 20 20 20 20 20 20 20 20  d *pArg,        
eb8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb8b0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
eb8c0 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 72  t arg to xCompar
eb8d0 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f  e() */.  BtCurso
eb8e0 72 20 2a 2a 70 70 43 75 72 20 20 20 20 20 20 20  r **ppCur       
eb8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb900 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65       /* Write ne
eb910 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 2a 2f  w cursor here */
eb920 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
eb930 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  BtCursor *pCur;.
eb940 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
eb950 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73  = p->pBt;..  ass
eb960 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
eb970 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29  eHoldsMutex(p) )
eb980 3b 0a 20 20 2a 70 70 43 75 72 20 3d 20 30 3b 0a  ;.  *ppCur = 0;.
eb990 20 20 69 66 28 20 77 72 46 6c 61 67 20 29 7b 0a    if( wrFlag ){.
eb9a0 20 20 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61      if( pBt->rea
eb9b0 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 72  dOnly ){.      r
eb9c0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41  eturn SQLITE_REA
eb9d0 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20  DONLY;.    }.   
eb9e0 20 69 66 28 20 63 68 65 63 6b 52 65 61 64 4c 6f   if( checkReadLo
eb9f0 63 6b 73 28 70 2c 20 69 54 61 62 6c 65 2c 20 30  cks(p, iTable, 0
eba00 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
eba10 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b  n SQLITE_LOCKED;
eba20 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
eba30 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30  ( pBt->pPage1==0
eba40 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6c 6f 63   ){.    rc = loc
eba50 6b 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28  kBtreeWithRetry(
eba60 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  p);.    if( rc!=
eba70 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
eba80 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
eba90 20 20 7d 0a 20 20 20 20 69 66 28 20 70 42 74 2d    }.    if( pBt-
ebaa0 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 77 72 46  >readOnly && wrF
ebab0 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 72 65 74  lag ){.      ret
ebac0 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
ebad0 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  NLY;.    }.  }. 
ebae0 20 70 43 75 72 20 3d 20 73 71 6c 69 74 65 33 4d   pCur = sqlite3M
ebaf0 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
ebb00 66 28 2a 70 43 75 72 29 20 29 3b 0a 20 20 69 66  f(*pCur) );.  if
ebb10 28 20 70 43 75 72 3d 3d 30 20 29 7b 0a 20 20 20  ( pCur==0 ){.   
ebb20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
ebb30 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65  EM;.    goto cre
ebb40 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70  ate_cursor_excep
ebb50 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 70 43 75 72  tion;.  }.  pCur
ebb60 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 67  ->pgnoRoot = (Pg
ebb70 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20 69 66 28  no)iTable;.  if(
ebb80 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 73 71   iTable==1 && sq
ebb90 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
ebba0 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
ebbb0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
ebbc0 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20  SQLITE_EMPTY;.  
ebbd0 20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 63 75    goto create_cu
ebbe0 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3b 0a  rsor_exception;.
ebbf0 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 41 6e    }.  rc = getAn
ebc00 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70  dInitPage(pBt, p
ebc10 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26  Cur->pgnoRoot, &
ebc20 70 43 75 72 2d 3e 70 50 61 67 65 2c 20 30 29 3b  pCur->pPage, 0);
ebc30 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
ebc40 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
ebc50 20 63 72 65 61 74 65 5f 63 75 72 73 6f 72 5f 65   create_cursor_e
ebc60 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20  xception;.  }.. 
ebc70 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20   /* Now that no 
ebc80 6f 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e  other errors can
ebc90 20 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66   occur, finish f
ebca0 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74  illing in the Bt
ebcb0 43 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69  Cursor.  ** vari
ebcc0 61 62 6c 65 73 2c 20 6c 69 6e 6b 20 74 68 65 20  ables, link the 
ebcd0 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20  cursor into the 
ebce0 42 74 53 68 61 72 65 64 20 6c 69 73 74 20 61 6e  BtShared list an
ebcf0 64 20 73 65 74 20 2a 70 70 43 75 72 20 28 74 68  d set *ppCur (th
ebd00 65 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 61 72  e.  ** output ar
ebd10 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
ebd20 75 6e 63 74 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20  unction)..  */. 
ebd30 20 70 43 75 72 2d 3e 78 43 6f 6d 70 61 72 65 20   pCur->xCompare 
ebd40 3d 20 78 43 6d 70 20 3f 20 78 43 6d 70 20 3a 20  = xCmp ? xCmp : 
ebd50 64 66 6c 74 43 6f 6d 70 61 72 65 3b 0a 20 20 70  dfltCompare;.  p
ebd60 43 75 72 2d 3e 70 41 72 67 20 3d 20 70 41 72 67  Cur->pArg = pArg
ebd70 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 65 65  ;.  pCur->pBtree
ebd80 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70 42   = p;.  pCur->pB
ebd90 74 20 3d 20 70 42 74 3b 0a 20 20 70 43 75 72 2d  t = pBt;.  pCur-
ebda0 3e 77 72 46 6c 61 67 20 3d 20 77 72 46 6c 61 67  >wrFlag = wrFlag
ebdb0 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 20  ;.  pCur->pNext 
ebdc0 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a  = pBt->pCursor;.
ebdd0 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78    if( pCur->pNex
ebde0 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70  t ){.    pCur->p
ebdf0 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43  Next->pPrev = pC
ebe00 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70  ur;.  }.  pBt->p
ebe10 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20  Cursor = pCur;. 
ebe20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
ebe30 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
ebe40 20 20 2a 70 70 43 75 72 20 3d 20 70 43 75 72 3b    *ppCur = pCur;
ebe50 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
ebe60 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74 65 5f 63 75  E_OK;..create_cu
ebe70 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3a 0a  rsor_exception:.
ebe80 20 20 69 66 28 20 70 43 75 72 20 29 7b 0a 20 20    if( pCur ){.  
ebe90 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43    releasePage(pC
ebea0 75 72 2d 3e 70 50 61 67 65 29 3b 0a 20 20 20 20  ur->pPage);.    
ebeb0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75  sqlite3_free(pCu
ebec0 72 29 3b 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b  r);.  }.  unlock
ebed0 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42  BtreeIfUnused(pB
ebee0 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  t);.  return rc;
ebef0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
ebf00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
ebf10 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65  eeCursor(.  Btre
ebf20 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
ebf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ebf40 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
ebf50 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54  tree */.  int iT
ebf60 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20  able,           
ebf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ebf80 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61        /* Root pa
ebf90 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f  ge of table to o
ebfa0 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46  pen */.  int wrF
ebfb0 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20  lag,            
ebfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ebfd0 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69       /* 1 to wri
ebfe0 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20  te. 0 read-only 
ebff0 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29  */.  int (*xCmp)
ec000 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
ec010 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
ec020 20 76 6f 69 64 2a 29 2c 20 2f 2a 20 4b 65 79 20   void*), /* Key 
ec030 43 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 20  Comparison func 
ec040 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c  */.  void *pArg,
ec050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec070 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f   /* First arg to
ec080 20 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20   xCompare() */. 
ec090 20 42 74 43 75 72 73 6f 72 20 2a 2a 70 70 43 75   BtCursor **ppCu
ec0a0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
ec0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ec0c0 57 72 69 74 65 20 6e 65 77 20 63 75 72 73 6f 72  Write new cursor
ec0d0 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
ec0e0 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42  t rc;.  sqlite3B
ec0f0 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
ec100 72 63 20 3d 20 62 74 72 65 65 43 75 72 73 6f 72  rc = btreeCursor
ec110 28 70 2c 20 69 54 61 62 6c 65 2c 20 77 72 46 6c  (p, iTable, wrFl
ec120 61 67 2c 20 78 43 6d 70 2c 20 70 41 72 67 2c 20  ag, xCmp, pArg, 
ec130 70 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65  ppCur);.  sqlite
ec140 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
ec150 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
ec160 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63  ./*.** Close a c
ec170 75 72 73 6f 72 2e 20 20 54 68 65 20 72 65 61 64  ursor.  The read
ec180 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
ec190 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 72 65  abase file is re
ec1a0 6c 65 61 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74  leased.** when t
ec1b0 68 65 20 6c 61 73 74 20 63 75 72 73 6f 72 20 69  he last cursor i
ec1c0 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c  s closed..*/.SQL
ec1d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ec1e0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
ec1f0 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  eCursor(BtCursor
ec200 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 53 68 61   *pCur){.  BtSha
ec210 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d  red *pBt = pCur-
ec220 3e 70 42 74 3b 0a 20 20 42 74 72 65 65 20 2a 70  >pBt;.  Btree *p
ec230 42 74 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42  Btree = pCur->pB
ec240 74 72 65 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33  tree;..  sqlite3
ec250 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65  BtreeEnter(pBtre
ec260 65 29 3b 0a 20 20 63 6c 65 61 72 43 75 72 73 6f  e);.  clearCurso
ec270 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b  rPosition(pCur);
ec280 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50 72  .  if( pCur->pPr
ec290 65 76 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e  ev ){.    pCur->
ec2a0 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70  pPrev->pNext = p
ec2b0 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65  Cur->pNext;.  }e
ec2c0 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 70 43  lse{.    pBt->pC
ec2d0 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e  ursor = pCur->pN
ec2e0 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ext;.  }.  if( p
ec2f0 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  Cur->pNext ){.  
ec300 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70    pCur->pNext->p
ec310 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70 50 72  Prev = pCur->pPr
ec320 65 76 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  ev;.  }.  releas
ec330 65 50 61 67 65 28 70 43 75 72 2d 3e 70 50 61 67  ePage(pCur->pPag
ec340 65 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65  e);.  unlockBtre
ec350 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
ec360 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72    invalidateOver
ec370 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b  flowCache(pCur);
ec380 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
ec390 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  pCur);.  sqlite3
ec3a0 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65  BtreeLeave(pBtre
ec3b0 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  e);.  return SQL
ec3c0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
ec3d0 20 4d 61 6b 65 20 61 20 74 65 6d 70 6f 72 61 72   Make a temporar
ec3e0 79 20 63 75 72 73 6f 72 20 62 79 20 66 69 6c 6c  y cursor by fill
ec3f0 69 6e 67 20 69 6e 20 74 68 65 20 66 69 65 6c 64  ing in the field
ec400 73 20 6f 66 20 70 54 65 6d 70 43 75 72 2e 0a 2a  s of pTempCur..*
ec410 2a 20 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20  * The temporary 
ec420 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 6f 6e  cursor is not on
ec430 20 74 68 65 20 63 75 72 73 6f 72 20 6c 69 73 74   the cursor list
ec440 20 66 6f 72 20 74 68 65 20 42 74 72 65 65 2e 0a   for the Btree..
ec450 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ec460 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
ec470 72 65 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72  reeGetTempCursor
ec480 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
ec490 20 42 74 43 75 72 73 6f 72 20 2a 70 54 65 6d 70   BtCursor *pTemp
ec4a0 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Cur){.  assert( 
ec4b0 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
ec4c0 28 70 43 75 72 29 20 29 3b 0a 20 20 6d 65 6d 63  (pCur) );.  memc
ec4d0 70 79 28 70 54 65 6d 70 43 75 72 2c 20 70 43 75  py(pTempCur, pCu
ec4e0 72 2c 20 73 69 7a 65 6f 66 28 2a 70 43 75 72 29  r, sizeof(*pCur)
ec4f0 29 3b 0a 20 20 70 54 65 6d 70 43 75 72 2d 3e 70  );.  pTempCur->p
ec500 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 54 65 6d  Next = 0;.  pTem
ec510 70 43 75 72 2d 3e 70 50 72 65 76 20 3d 20 30 3b  pCur->pPrev = 0;
ec520 0a 20 20 69 66 28 20 70 54 65 6d 70 43 75 72 2d  .  if( pTempCur-
ec530 3e 70 50 61 67 65 20 29 7b 0a 20 20 20 20 73 71  >pPage ){.    sq
ec540 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 54  lite3PagerRef(pT
ec550 65 6d 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 70  empCur->pPage->p
ec560 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a  DbPage);.  }.}..
ec570 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 20 74  /*.** Delete a t
ec580 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20  emporary cursor 
ec590 73 75 63 68 20 61 73 20 77 61 73 20 6d 61 64 65  such as was made
ec5a0 20 62 79 20 74 68 65 20 43 72 65 61 74 65 54 65   by the CreateTe
ec5b0 6d 70 6f 72 61 72 79 43 75 72 73 6f 72 28 29 0a  mporaryCursor().
ec5c0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 62 6f 76  ** function abov
ec5d0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
ec5e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
ec5f0 33 42 74 72 65 65 52 65 6c 65 61 73 65 54 65 6d  3BtreeReleaseTem
ec600 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  pCursor(BtCursor
ec610 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72   *pCur){.  asser
ec620 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
ec630 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69  tex(pCur) );.  i
ec640 66 28 20 70 43 75 72 2d 3e 70 50 61 67 65 20 29  f( pCur->pPage )
ec650 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  {.    sqlite3Pag
ec660 65 72 55 6e 72 65 66 28 70 43 75 72 2d 3e 70 50  erUnref(pCur->pP
ec670 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
ec680 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
ec690 20 73 75 72 65 20 74 68 65 20 42 74 43 75 72 73   sure the BtCurs
ec6a0 6f 72 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65  or* given in the
ec6b0 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20   argument has a 
ec6c0 76 61 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f  valid.** BtCurso
ec6d0 72 2e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  r.info structure
ec6e0 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  .  If it is not 
ec6f0 61 6c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63  already valid, c
ec700 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74  all.** sqlite3Bt
ec710 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74  reeParseCell() t
ec720 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a  o fill it in..**
ec730 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66  .** BtCursor.inf
ec740 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20  o is a cache of 
ec750 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
ec760 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  in the current c
ec770 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68  ell..** Using th
ec780 69 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73  is cache reduces
ec790 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
ec7a0 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42  alls to sqlite3B
ec7b0 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 2e  treeParseCell().
ec7c0 0a 2a 2a 0a 2a 2a 20 32 30 30 37 2d 30 36 2d 32  .**.** 2007-06-2
ec7d0 35 3a 20 20 54 68 65 72 65 20 69 73 20 61 20 62  5:  There is a b
ec7e0 75 67 20 69 6e 20 73 6f 6d 65 20 76 65 72 73 69  ug in some versi
ec7f0 6f 6e 73 20 6f 66 20 4d 53 56 43 20 74 68 61 74  ons of MSVC that
ec800 20 63 61 75 73 65 20 74 68 65 0a 2a 2a 20 63 6f   cause the.** co
ec810 6d 70 69 6c 65 72 20 74 6f 20 63 72 61 73 68 20  mpiler to crash 
ec820 77 68 65 6e 20 67 65 74 43 65 6c 6c 49 6e 66 6f  when getCellInfo
ec830 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  () is implemente
ec840 64 20 61 73 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a  d as a macro..**
ec850 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20   But there is a 
ec860 6d 65 61 73 75 72 65 61 62 6c 65 20 73 70 65 65  measureable spee
ec870 64 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 75  d advantage to u
ec880 73 69 6e 67 20 74 68 65 20 6d 61 63 72 6f 20 6f  sing the macro o
ec890 6e 20 67 63 63 0a 2a 2a 20 28 77 68 65 6e 20 6c  n gcc.** (when l
ec8a0 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74  ess compiler opt
ec8b0 69 6d 69 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 20  imizations like 
ec8c0 2d 4f 73 20 6f 72 20 2d 4f 30 20 61 72 65 20 75  -Os or -O0 are u
ec8d0 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 63  sed and the.** c
ec8e0 6f 6d 70 69 6c 65 72 20 69 73 20 6e 6f 74 20 64  ompiler is not d
ec8f0 6f 69 6e 67 20 61 67 72 65 73 73 69 76 65 20 69  oing agressive i
ec900 6e 6c 69 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65  nlining.)  So we
ec910 20 75 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63   use a real func
ec920 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 43  tion.** for MSVC
ec930 20 61 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f 72   and a macro for
ec940 20 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65   everything else
ec950 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 37 2e  .  Ticket #2457.
ec960 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  .*/.#ifndef NDEB
ec970 55 47 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64  UG.  static void
ec980 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
ec990 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
ec9a0 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e  .    CellInfo in
ec9b0 66 6f 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  fo;.    memset(&
ec9c0 69 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  info, 0, sizeof(
ec9d0 69 6e 66 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69  info));.    sqli
ec9e0 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
ec9f0 6c 28 70 43 75 72 2d 3e 70 50 61 67 65 2c 20 70  l(pCur->pPage, p
eca00 43 75 72 2d 3e 69 64 78 2c 20 26 69 6e 66 6f 29  Cur->idx, &info)
eca10 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  ;.    assert( me
eca20 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75  mcmp(&info, &pCu
eca30 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28  r->info, sizeof(
eca40 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d  info))==0 );.  }
eca50 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
eca60 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
eca70 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  x).#endif.#ifdef
eca80 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55   _MSC_VER.  /* U
eca90 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69  se a real functi
ecaa0 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f  on in MSVC to wo
ecab0 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69  rk around bugs i
ecac0 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e  n that compiler.
ecad0 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69   */.  static voi
ecae0 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74  d getCellInfo(Bt
ecaf0 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
ecb00 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66     if( pCur->inf
ecb10 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20  o.nSize==0 ){.  
ecb20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
ecb30 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
ecb40 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78  pPage, pCur->idx
ecb50 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a  , &pCur->info);.
ecb60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
ecb70 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
ecb80 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pCur);.    }.  }
ecb90 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74  .#else /* if not
ecba0 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f   _MSC_VER */.  /
ecbb0 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e  * Use a macro in
ecbc0 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69   all other compi
ecbd0 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65  lers so that the
ecbe0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c   function is inl
ecbf0 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ined */.#define 
ecc00 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  getCellInfo(pCur
ecc10 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
ecc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecc40 5c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e  \.  if( pCur->in
ecc50 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 20 20  fo.nSize==0 ){  
ecc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecc80 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ecc90 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
ecca0 65 43 65 6c 6c 28 70 43 75 72 2d 3e 70 50 61 67  eCell(pCur->pPag
eccb0 65 2c 20 70 43 75 72 2d 3e 69 64 78 2c 20 26 70  e, pCur->idx, &p
eccc0 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 20 20 20 20  Cur->info);     
eccd0 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20      \.  }else{  
ecce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eccf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ecd20 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e      assertCellIn
ecd30 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20  fo(pCur);       
ecd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecd60 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65          \.  }.#e
ecd70 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52  ndif /* _MSC_VER
ecd80 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a   */../*.** Set *
ecd90 70 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a  pSize to the siz
ecda0 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  e of the buffer 
ecdb0 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74  needed to hold t
ecdc0 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74  he value of.** t
ecdd0 68 65 20 6b 65 79 20 66 6f 72 20 74 68 65 20 63  he key for the c
ecde0 75 72 72 65 6e 74 20 65 6e 74 72 79 2e 20 20 49  urrent entry.  I
ecdf0 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
ece00 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20  not pointing.** 
ece10 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79  to a valid entry
ece20 2c 20 2a 70 53 69 7a 65 20 69 73 20 73 65 74 20  , *pSize is set 
ece30 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72  to 0. .**.** For
ece40 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 74 68   a table with th
ece50 65 20 49 4e 54 4b 45 59 20 66 6c 61 67 20 73 65  e INTKEY flag se
ece60 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  t, this routine 
ece70 72 65 74 75 72 6e 73 20 74 68 65 20 6b 65 79 0a  returns the key.
ece80 2a 2a 20 69 74 73 65 6c 66 2c 20 6e 6f 74 20 74  ** itself, not t
ece90 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
ecea0 65 73 20 69 6e 20 74 68 65 20 6b 65 79 2e 0a 2a  es in the key..*
eceb0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
ecec0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
eced0 65 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f  eKeySize(BtCurso
ecee0 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53  r *pCur, i64 *pS
ecef0 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ize){.  int rc;.
ecf00 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
ecf10 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
ecf20 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74  ) );.  rc = rest
ecf30 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73 6f 72  oreOrClearCursor
ecf40 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
ecf50 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
ecf60 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
ecf70 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
ecf80 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
ecf90 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  || pCur->eState=
ecfa0 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
ecfb0 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65  .    if( pCur->e
ecfc0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
ecfd0 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2a  VALID ){.      *
ecfe0 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d  pSize = 0;.    }
ecff0 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 74 43  else{.      getC
ed000 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20  ellInfo(pCur);. 
ed010 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 43       *pSize = pC
ed020 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20  ur->info.nKey;. 
ed030 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
ed040 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
ed050 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65  et *pSize to the
ed060 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
ed070 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 65 20   of data in the 
ed080 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72  entry the.** cur
ed090 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  sor currently po
ed0a0 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77 61 79 73  ints to.  Always
ed0b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
ed0c0 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73  K..** Failure is
ed0d0 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20   not possible.  
ed0e0 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  If the cursor is
ed0f0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a 2a   not currently.*
ed100 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e  * pointing to an
ed110 20 65 6e 74 72 79 20 28 77 68 69 63 68 20 63 61   entry (which ca
ed120 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 78  n happen, for ex
ed130 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68 65  ample, if.** the
ed140 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d 70   database is emp
ed150 74 79 29 20 74 68 65 6e 20 2a 70 53 69 7a 65 20  ty) then *pSize 
ed160 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a  is set to 0..*/.
ed170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ed180 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
ed190 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72  ataSize(BtCursor
ed1a0 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69   *pCur, u32 *pSi
ed1b0 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ze){.  int rc;..
ed1c0 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
ed1d0 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
ed1e0 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f   );.  rc = resto
ed1f0 72 65 4f 72 43 6c 65 61 72 43 75 72 73 6f 72 50  reOrClearCursorP
ed200 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20  osition(pCur);. 
ed210 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
ed220 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OK ){.    assert
ed230 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
ed240 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c  CURSOR_INVALID |
ed250 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  | pCur->eState==
ed260 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
ed270 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53      if( pCur->eS
ed280 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56  tate==CURSOR_INV
ed290 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2f 2a  ALID ){.      /*
ed2a0 20 4e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74   Not pointing at
ed2b0 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 20 2d   a valid entry -
ed2c0 20 73 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 30   set *pSize to 0
ed2d0 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70 53 69 7a  . */.      *pSiz
ed2e0 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 0;.    }else
ed2f0 7b 0a 20 20 20 20 20 20 67 65 74 43 65 6c 6c 49  {.      getCellI
ed300 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 20  nfo(pCur);.     
ed310 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e   *pSize = pCur->
ed320 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 20  info.nData;.    
ed330 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
ed340 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  c;.}../*.** Give
ed350 6e 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  n the page numbe
ed360 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f 77  r of an overflow
ed370 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
ed380 61 62 61 73 65 20 28 70 61 72 61 6d 65 74 65 72  abase (parameter
ed390 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74 68 69 73 20  .** ovfl), this 
ed3a0 66 75 6e 63 74 69 6f 6e 20 66 69 6e 64 73 20 74  function finds t
ed3b0 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  he page number o
ed3c0 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  f the next page 
ed3d0 69 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65  in the .** linke
ed3e0 64 20 6c 69 73 74 20 6f 66 20 6f 76 65 72 66 6c  d list of overfl
ed3f0 6f 77 20 70 61 67 65 73 2e 20 49 66 20 70 6f 73  ow pages. If pos
ed400 73 69 62 6c 65 2c 20 69 74 20 75 73 65 73 20 74  sible, it uses t
ed410 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a  he auto-vacuum.*
ed420 2a 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 64 61  * pointer-map da
ed430 74 61 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65  ta instead of re
ed440 61 64 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  ading the conten
ed450 74 20 6f 66 20 70 61 67 65 20 6f 76 66 6c 20 74  t of page ovfl t
ed460 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20  o do so. .**.** 
ed470 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
ed480 72 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  rs an SQLite err
ed490 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
ed4a0 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 3a 0a  ned. Otherwise:.
ed4b0 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 70 50 67  **.** Unless pPg
ed4c0 6e 6f 4e 65 78 74 20 69 73 20 4e 55 4c 4c 2c 20  noNext is NULL, 
ed4d0 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
ed4e0 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76 65 72  of the next over
ed4f0 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 20 69 6e  flow .** page in
ed500 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
ed510 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 2a   is written to *
ed520 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61  pPgnoNext. If pa
ed530 67 65 20 6f 76 66 6c 0a 2a 2a 20 69 73 20 74 68  ge ovfl.** is th
ed540 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 69  e last page in i
ed550 74 27 73 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c  t's linked list,
ed560 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 73   *pPgnoNext is s
ed570 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a  et to zero. .**.
ed580 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69 73 20  ** If ppPage is 
ed590 6e 6f 74 20 4e 55 4c 4c 2c 20 2a 70 70 50 61 67  not NULL, *ppPag
ed5a0 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
ed5b0 4d 65 6d 50 61 67 65 2a 20 68 61 6e 64 6c 65 0a  MemPage* handle.
ed5c0 2a 2a 20 66 6f 72 20 70 61 67 65 20 6f 76 66 6c  ** for page ovfl
ed5d0 2e 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  . The underlying
ed5e0 20 70 61 67 65 72 20 70 61 67 65 20 6d 61 79 20   pager page may 
ed5f0 68 61 76 65 20 62 65 65 6e 20 72 65 71 75 65 73  have been reques
ed600 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ted.** with the 
ed610 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61 67 20 73  noContent flag s
ed620 65 74 2c 20 73 6f 20 74 68 65 20 70 61 67 65 20  et, so the page 
ed630 64 61 74 61 20 61 63 63 65 73 73 61 62 6c 65 20  data accessable 
ed640 76 69 61 0a 2a 2a 20 74 68 69 73 20 68 61 6e 64  via.** this hand
ed650 6c 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 72  le may not be tr
ed660 75 73 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  usted..*/.static
ed670 20 69 6e 74 20 67 65 74 4f 76 65 72 66 6c 6f 77   int getOverflow
ed680 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
ed690 20 2a 70 42 74 2c 20 0a 20 20 50 67 6e 6f 20 6f   *pBt, .  Pgno o
ed6a0 76 66 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  vfl,            
ed6b0 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 66 6c         /* Overfl
ed6c0 6f 77 20 70 61 67 65 20 2a 2f 0a 20 20 4d 65 6d  ow page */.  Mem
ed6d0 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20  Page **ppPage,  
ed6e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
ed6f0 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 6c 65  : MemPage handle
ed700 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e   */.  Pgno *pPgn
ed710 6f 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20  oNext           
ed720 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20     /* OUT: Next 
ed730 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75  overflow page nu
ed740 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e  mber */.){.  Pgn
ed750 6f 20 6e 65 78 74 20 3d 20 30 3b 0a 20 20 69 6e  o next = 0;.  in
ed760 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
ed770 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
ed780 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
ed790 29 3b 0a 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74  );.  /* One of t
ed7a0 68 65 73 65 20 6d 75 73 74 20 6e 6f 74 20 62 65  hese must not be
ed7b0 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65   NULL. Otherwise
ed7c0 2c 20 77 68 79 20 63 61 6c 6c 20 74 68 69 73 20  , why call this 
ed7d0 66 75 6e 63 74 69 6f 6e 3f 20 2a 2f 0a 20 20 61  function? */.  a
ed7e0 73 73 65 72 74 28 70 70 50 61 67 65 20 7c 7c 20  ssert(ppPage || 
ed7f0 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a 20 20 2f  pPgnoNext);..  /
ed800 2a 20 49 66 20 70 50 67 6e 6f 4e 65 78 74 20 69  * If pPgnoNext i
ed810 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69  s NULL, then thi
ed820 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  s function is be
ed830 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62  ing called to ob
ed840 74 61 69 6e 0a 20 20 2a 2a 20 61 20 4d 65 6d 50  tain.  ** a MemP
ed850 61 67 65 2a 20 72 65 66 65 72 65 6e 63 65 20 6f  age* reference o
ed860 6e 6c 79 2e 20 4e 6f 20 70 61 67 65 2d 64 61 74  nly. No page-dat
ed870 61 20 69 73 20 72 65 71 75 69 72 65 64 20 69 6e  a is required in
ed880 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f   this case..  */
ed890 0a 20 20 69 66 28 20 21 70 50 67 6e 6f 4e 65 78  .  if( !pPgnoNex
ed8a0 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
ed8b0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
ed8c0 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 70  age(pBt, ovfl, p
ed8d0 70 50 61 67 65 2c 20 31 29 3b 0a 20 20 7d 0a 0a  pPage, 1);.  }..
ed8e0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
ed8f0 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
ed900 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20   /* Try to find 
ed910 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e  the next page in
ed920 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69   the overflow li
ed930 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a  st using the.  *
ed940 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69  * autovacuum poi
ed950 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20  nter-map pages. 
ed960 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e  Guess that the n
ed970 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a  ext page in .  *
ed980 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c  * the overflow l
ed990 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62  ist is page numb
ed9a0 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20  er (ovfl+1). If 
ed9b0 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73  that guess turns
ed9c0 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65   .  ** out to be
ed9d0 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63   wrong, fall bac
ed9e0 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65  k to loading the
ed9f0 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20   data of page . 
eda00 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20   ** number ovfl 
eda10 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
eda20 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65   next page numbe
eda30 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42  r..  */.  if( pB
eda40 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
eda50 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a  .    Pgno pgno;.
eda60 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20      Pgno iGuess 
eda70 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38  = ovfl+1;.    u8
eda80 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69   eType;..    whi
eda90 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47  le( PTRMAP_ISPAG
edaa0 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c  E(pBt, iGuess) |
edab0 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e  | iGuess==PENDIN
edac0 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
edad0 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73   ){.      iGuess
edae0 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
edaf0 66 28 20 69 47 75 65 73 73 3c 3d 73 71 6c 69 74  f( iGuess<=sqlit
edb00 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
edb10 28 70 42 74 2d 3e 70 50 61 67 65 72 29 20 29 7b  (pBt->pPager) ){
edb20 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d  .      rc = ptrm
edb30 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 65 73  apGet(pBt, iGues
edb40 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 6e 6f  s, &eType, &pgno
edb50 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
edb60 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
edb70 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
edb80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
edb90 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  f( eType==PTRMAP
edba0 5f 4f 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67  _OVERFLOW2 && pg
edbb0 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20  no==ovfl ){.    
edbc0 20 20 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73      next = iGues
edbd0 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  s;.      }.    }
edbe0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
edbf0 66 28 20 6e 65 78 74 3d 3d 30 20 7c 7c 20 70 70  f( next==0 || pp
edc00 50 61 67 65 20 29 7b 0a 20 20 20 20 4d 65 6d 50  Page ){.    MemP
edc10 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  age *pPage = 0;.
edc20 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
edc30 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
edc40 74 2c 20 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c  t, ovfl, &pPage,
edc50 20 6e 65 78 74 21 3d 30 29 3b 0a 20 20 20 20 61   next!=0);.    a
edc60 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45  ssert(rc==SQLITE
edc70 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d 30 29  _OK || pPage==0)
edc80 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74 3d 3d  ;.    if( next==
edc90 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  0 && rc==SQLITE_
edca0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 65 78 74  OK ){.      next
edcb0 20 3d 20 67 65 74 34 62 79 74 65 28 70 50 61 67   = get4byte(pPag
edcc0 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 7d  e->aData);.    }
edcd0 0a 0a 20 20 20 20 69 66 28 20 70 70 50 61 67 65  ..    if( ppPage
edce0 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 50 61 67   ){.      *ppPag
edcf0 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d  e = pPage;.    }
edd00 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 6c 65  else{.      rele
edd10 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
edd20 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 50 67      }.  }.  *pPg
edd30 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 0a  noNext = next;..
edd40 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
edd50 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 61 20  /*.** Copy data 
edd60 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 74 6f  from a buffer to
edd70 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72 6f 6d   a page, or from
edd80 20 61 20 70 61 67 65 20 74 6f 20 61 20 62 75 66   a page to a buf
edd90 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c  fer..**.** pPayl
edda0 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74 65 72  oad is a pointer
eddb0 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65 64 20   to data stored 
eddc0 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65  on database page
eddd0 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 66 20   pDbPage..** If 
edde0 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 73 20  argument eOp is 
eddf0 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 79 74  false, then nByt
ede00 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  e bytes of data 
ede10 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72  are copied.** fr
ede20 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f 20 74  om pPayload to t
ede30 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  he buffer pointe
ede40 64 20 61 74 20 62 79 20 70 42 75 66 2e 20 49 66  d at by pBuf. If
ede50 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a 2a 2a   eOp is true,.**
ede60 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50 61 67   then sqlite3Pag
ede70 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c  erWrite() is cal
ede80 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65 20 61  led on pDbPage a
ede90 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73 0a 2a  nd nByte bytes.*
edea0 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f  * of data are co
edeb0 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75  pied from the bu
edec0 66 66 65 72 20 70 42 75 66 20 74 6f 20 70 50 61  ffer pBuf to pPa
eded0 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  yload..**.** SQL
edee0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
edef0 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f  ed on success, o
edf00 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f  therwise an erro
edf10 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  r code..*/.stati
edf20 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c 6f 61  c int copyPayloa
edf30 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 79 6c  d(.  void *pPayl
edf40 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f  oad,           /
edf50 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67  * Pointer to pag
edf60 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64  e data */.  void
edf70 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20   *pBuf,         
edf80 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
edf90 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a 20 20   to buffer */.  
edfa0 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20  int nByte,      
edfb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
edfc0 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
edfd0 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 65 4f  copy */.  int eO
edfe0 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
edff0 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f 70 79      /* 0 -> copy
ee000 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 2d 3e   from page, 1 ->
ee010 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20 2a 2f   copy to page */
ee020 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61  .  DbPage *pDbPa
ee030 67 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ge           /* 
ee040 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  Page containing 
ee050 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20  pPayload */.){. 
ee060 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 20 20   if( eOp ){.    
ee070 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f  /* Copy data fro
ee080 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61 67 65  m buffer to page
ee090 20 28 61 20 77 72 69 74 65 20 6f 70 65 72 61 74   (a write operat
ee0a0 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ion) */.    int 
ee0b0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
ee0c0 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b  rWrite(pDbPage);
ee0d0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
ee0e0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
ee0f0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
ee100 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 79  .    memcpy(pPay
ee110 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 79 74  load, pBuf, nByt
ee120 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
ee130 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72   /* Copy data fr
ee140 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66 66 65  om page to buffe
ee150 72 20 28 61 20 72 65 61 64 20 6f 70 65 72 61 74  r (a read operat
ee160 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 6d 63  ion) */.    memc
ee170 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c 6f 61  py(pBuf, pPayloa
ee180 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20  d, nByte);.  }. 
ee190 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
ee1a0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  K;.}../*.** This
ee1b0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
ee1c0 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f 76 65  d to read or ove
ee1d0 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64 20 69  rwrite payload i
ee1e0 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f  nformation.** fo
ee1f0 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  r the entry that
ee200 20 74 68 65 20 70 43 75 72 20 63 75 72 73 6f 72   the pCur cursor
ee210 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e   is pointing to.
ee220 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a 20 70   If the eOp.** p
ee230 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74  arameter is 0, t
ee240 68 69 73 20 69 73 20 61 20 72 65 61 64 20 6f 70  his is a read op
ee250 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20 63 6f  eration (data co
ee260 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 75 66  pied into.** buf
ee270 66 65 72 20 70 42 75 66 29 2e 20 49 66 20 69 74  fer pBuf). If it
ee280 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 20   is non-zero, a 
ee290 77 72 69 74 65 20 28 64 61 74 61 20 63 6f 70 69  write (data copi
ee2a0 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 66 65  ed from.** buffe
ee2b0 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 41  r pBuf)..**.** A
ee2c0 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22 20   total of "amt" 
ee2d0 62 79 74 65 73 20 61 72 65 20 72 65 61 64 20 6f  bytes are read o
ee2e0 72 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e 6e  r written beginn
ee2f0 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22 2e  ing at "offset".
ee300 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61 64  .** Data is read
ee310 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65 20   to or from the 
ee320 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a 0a  buffer pBuf..**.
ee330 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ee340 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 61 20  does not make a 
ee350 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77  distinction betw
ee360 65 65 6e 20 6b 65 79 20 61 6e 64 20 64 61 74 61  een key and data
ee370 2e 0a 2a 2a 20 49 74 20 6a 75 73 74 20 72 65 61  ..** It just rea
ee380 64 73 20 6f 72 20 77 72 69 74 65 73 20 62 79 74  ds or writes byt
ee390 65 73 20 66 72 6f 6d 20 74 68 65 20 70 61 79 6c  es from the payl
ee3a0 6f 61 64 20 61 72 65 61 2e 20 20 44 61 74 61 20  oad area.  Data 
ee3b0 6d 69 67 68 74 20 0a 2a 2a 20 61 70 70 65 61 72  might .** appear
ee3c0 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70 61 67   on the main pag
ee3d0 65 20 6f 72 20 62 65 20 73 63 61 74 74 65 72 65  e or be scattere
ee3e0 64 20 6f 75 74 20 6f 6e 20 6d 75 6c 74 69 70 6c  d out on multipl
ee3f0 65 20 6f 76 65 72 66 6c 6f 77 20 0a 2a 2a 20 70  e overflow .** p
ee400 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ages..**.** If t
ee410 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73 49 6e  he BtCursor.isIn
ee420 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61  crblobHandle fla
ee430 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20 74 68  g is set, and th
ee440 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 75 72  e current.** cur
ee450 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73 20 6f  sor entry uses o
ee460 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66  ne or more overf
ee470 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69 73 20  low pages, this 
ee480 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f  function.** allo
ee490 63 61 74 65 73 20 73 70 61 63 65 20 66 6f 72 20  cates space for 
ee4a0 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 6c 75  and lazily poplu
ee4b0 61 74 65 73 20 74 68 65 20 6f 76 65 72 66 6c 6f  ates the overflo
ee4c0 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a 2a 20  w page-list .** 
ee4d0 63 61 63 68 65 20 61 72 72 61 79 20 28 42 74 43  cache array (BtC
ee4e0 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29  ursor.aOverflow)
ee4f0 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c  . Subsequent cal
ee500 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a 20 63  ls use this.** c
ee510 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 65  ache to make see
ee520 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70  king to the supp
ee530 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f 72 65  lied offset more
ee540 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a   efficient..**.*
ee550 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c  * Once an overfl
ee560 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63  ow page-list cac
ee570 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f  he has been allo
ee580 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20 62 65  cated, it may be
ee590 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 20  .** invalidated 
ee5a0 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 75  if some other cu
ee5b0 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f 20 74  rsor writes to t
ee5c0 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 6f  he same table, o
ee5d0 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 72 73  r if.** the curs
ee5e0 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 61  or is moved to a
ee5f0 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77 2e 20   different row. 
ee600 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e  Additionally, in
ee610 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20   auto-vacuum.** 
ee620 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  mode, the follow
ee630 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 20 69  ing events may i
ee640 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f 76 65  nvalidate an ove
ee650 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20  rflow page-list 
ee660 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a  cache..**.**   *
ee670 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20   An incremental 
ee680 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41  vacuum,.**   * A
ee690 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 6f 5f   commit in auto_
ee6a0 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f  vacuum="full" mo
ee6b0 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 61 74  de,.**   * Creat
ee6c0 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d 61 79  ing a table (may
ee6d0 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20   require moving 
ee6e0 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
ee6f0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
ee700 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 0a   accessPayload(.
ee710 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
ee720 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  ,      /* Cursor
ee730 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74   pointing to ent
ee740 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  ry to read from 
ee750 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 2c  */.  int offset,
ee760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67            /* Beg
ee770 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69 73 20  in reading this 
ee780 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f 61 64  far into payload
ee790 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20   */.  int amt,  
ee7a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
ee7b0 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74  ad this many byt
ee7c0 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  es */.  unsigned
ee7d0 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a 20   char *pBuf, /* 
ee7e0 57 72 69 74 65 20 74 68 65 20 62 79 74 65 73 20  Write the bytes 
ee7f0 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72  into this buffer
ee800 20 2a 2f 20 0a 20 20 69 6e 74 20 73 6b 69 70 4b   */ .  int skipK
ee810 65 79 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6f  ey,         /* o
ee820 66 66 73 65 74 20 62 65 67 69 6e 73 20 61 74 20  ffset begins at 
ee830 64 61 74 61 20 69 66 20 74 68 69 73 20 69 73 20  data if this is 
ee840 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4f  true */.  int eO
ee850 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p              /
ee860 2a 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20  * zero to read. 
ee870 6e 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74  non-zero to writ
ee880 65 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67  e. */.){.  unsig
ee890 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f  ned char *aPaylo
ee8a0 61 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ad;.  int rc = S
ee8b0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20  QLITE_OK;.  u32 
ee8c0 6e 4b 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78  nKey;.  int iIdx
ee8d0 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20   = 0;.  MemPage 
ee8e0 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70  *pPage = pCur->p
ee8f0 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 42 74 72  Page;     /* Btr
ee900 65 65 20 70 61 67 65 20 6f 66 20 63 75 72 72 65  ee page of curre
ee910 6e 74 20 63 75 72 73 6f 72 20 65 6e 74 72 79 20  nt cursor entry 
ee920 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
ee930 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20  Bt = pCur->pBt; 
ee940 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
ee950 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c 6f  this cursor belo
ee960 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 20 61 73 73  ngs to */..  ass
ee970 65 72 74 28 20 70 50 61 67 65 20 29 3b 0a 20 20  ert( pPage );.  
ee980 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
ee990 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
ee9a0 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ID );.  assert( 
ee9b0 70 43 75 72 2d 3e 69 64 78 3e 3d 30 20 26 26 20  pCur->idx>=0 && 
ee9c0 70 43 75 72 2d 3e 69 64 78 3c 70 50 61 67 65 2d  pCur->idx<pPage-
ee9d0 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65  >nCell );.  asse
ee9e0 72 74 28 20 6f 66 66 73 65 74 3e 3d 30 20 29 3b  rt( offset>=0 );
ee9f0 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
eea00 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
eea10 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49  ) );..  getCellI
eea20 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61  nfo(pCur);.  aPa
eea30 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e  yload = pCur->in
eea40 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d  fo.pCell + pCur-
eea50 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20  >info.nHeader;. 
eea60 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d 3e   nKey = (pPage->
eea70 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 70 43 75  intKey ? 0 : pCu
eea80 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 0a  r->info.nKey);..
eea90 20 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b    if( skipKey ){
eeaa0 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 6e  .    offset += n
eeab0 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f  Key;.  }.  if( o
eeac0 66 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79  ffset+amt > nKey
eead0 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74  +pCur->info.nDat
eeae0 61 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69  a ){.    /* Tryi
eeaf0 6e 67 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72  ng to read or wr
eeb00 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64  ite past the end
eeb10 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 73 20   of the data is 
eeb20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  an error */.    
eeb30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
eeb40 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  ROR;.  }..  /* C
eeb50 68 65 63 6b 20 69 66 20 64 61 74 61 20 6d 75 73  heck if data mus
eeb60 74 20 62 65 20 72 65 61 64 2f 77 72 69 74 74 65  t be read/writte
eeb70 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74  n to/from the bt
eeb80 72 65 65 20 70 61 67 65 20 69 74 73 65 6c 66 2e  ree page itself.
eeb90 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 74   */.  if( offset
eeba0 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63  <pCur->info.nLoc
eebb0 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 20  al ){.    int a 
eebc0 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 20 61  = amt;.    if( a
eebd0 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e  +offset>pCur->in
eebe0 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20  fo.nLocal ){.   
eebf0 20 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66     a = pCur->inf
eec00 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65  o.nLocal - offse
eec10 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  t;.    }.    rc 
eec20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61  = copyPayload(&a
eec30 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c  Payload[offset],
eec40 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70   pBuf, a, eOp, p
eec50 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
eec60 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a      offset = 0;.
eec70 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20      pBuf += a;. 
eec80 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d     amt -= a;.  }
eec90 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 65 74  else{.    offset
eeca0 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e   -= pCur->info.n
eecb0 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66  Local;.  }..  if
eecc0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
eecd0 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20  && amt>0 ){.    
eece0 63 6f 6e 73 74 20 69 6e 74 20 6f 76 66 6c 53 69  const int ovflSi
eecf0 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ze = pBt->usable
eed00 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79  Size - 4;  /* By
eed10 74 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20  tes content per 
eed20 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20  ovfl page */.   
eed30 20 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a   Pgno nextPage;.
eed40 0a 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20  .    nextPage = 
eed50 67 65 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f  get4byte(&aPaylo
eed60 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c  ad[pCur->info.nL
eed70 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66  ocal]);..#ifndef
eed80 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
eed90 52 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20  RBLOB.    /* If 
eeda0 74 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61  the isIncrblobHa
eedb0 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74  ndle flag is set
eedc0 20 61 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f   and the BtCurso
eedd0 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20  r.aOverflow[].  
eede0 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65    ** has not bee
eedf0 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c  n allocated, all
eee00 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68  ocate it now. Th
eee10 65 20 61 72 72 61 79 20 69 73 20 73 69 7a 65 64  e array is sized
eee20 20 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65   at.    ** one e
eee30 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76  ntry for each ov
eee40 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74  erflow page in t
eee50 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69  he overflow chai
eee60 6e 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 61  n. The.    ** pa
eee70 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
eee80 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20   first overflow 
eee90 70 61 67 65 20 69 73 20 73 74 6f 72 65 64 20 69  page is stored i
eeea0 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a  n aOverflow[0],.
eeeb0 20 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 61      ** etc. A va
eeec0 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20  lue of 0 in the 
eeed0 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61  aOverflow[] arra
eeee0 79 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 74  y means "not yet
eeef0 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28   known".    ** (
eef00 74 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a  the cache is laz
eef10 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a  ily populated)..
eef20 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
eef30 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48  Cur->isIncrblobH
eef40 61 6e 64 6c 65 20 26 26 20 21 70 43 75 72 2d 3e  andle && !pCur->
eef50 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20  aOverflow ){.   
eef60 20 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28     int nOvfl = (
eef70 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c  pCur->info.nPayl
eef80 6f 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  oad-pCur->info.n
eef90 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31  Local+ovflSize-1
eefa0 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20  )/ovflSize;.    
eefb0 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f    pCur->aOverflo
eefc0 77 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69  w = (Pgno *)sqli
eefd0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
eefe0 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c  zeof(Pgno)*nOvfl
eeff0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 76  );.      if( nOv
ef000 66 6c 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76  fl && !pCur->aOv
ef010 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
ef020 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
ef030 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
ef040 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
ef050 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
ef060 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20 62  list cache has b
ef070 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e  een allocated an
ef080 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74  d the.    ** ent
ef090 72 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  ry for the first
ef0a0 20 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 6c   required overfl
ef0b0 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 64  ow page is valid
ef0c0 2c 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69  , skip.    ** di
ef0d0 72 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20  rectly to it..  
ef0e0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75    */.    if( pCu
ef0f0 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20  r->aOverflow && 
ef100 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b  pCur->aOverflow[
ef110 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d  offset/ovflSize]
ef120 20 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20 3d   ){.      iIdx =
ef130 20 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a   (offset/ovflSiz
ef140 65 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50 61  e);.      nextPa
ef150 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72  ge = pCur->aOver
ef160 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20  flow[iIdx];.    
ef170 20 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66 73    offset = (offs
ef180 65 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20  et%ovflSize);.  
ef190 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
ef1a0 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54  for( ; rc==SQLIT
ef1b0 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26  E_OK && amt>0 &&
ef1c0 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 2b   nextPage; iIdx+
ef1d0 2b 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  +){..#ifndef SQL
ef1e0 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
ef1f0 42 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65  B.      /* If re
ef200 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74 65  quired, populate
ef210 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
ef220 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20 2a  ge-list cache. *
ef230 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72  /.      if( pCur
ef240 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20  ->aOverflow ){. 
ef250 20 20 20 20 20 20 20 61 73 73 65 72 74 28 21 70         assert(!p
ef260 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
ef270 49 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f  Idx] || pCur->aO
ef280 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e  verflow[iIdx]==n
ef290 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20  extPage);.      
ef2a0 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f    pCur->aOverflo
ef2b0 77 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 61  w[iIdx] = nextPa
ef2c0 67 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  ge;.      }.#end
ef2d0 69 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f 66  if..      if( of
ef2e0 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29  fset>=ovflSize )
ef2f0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
ef300 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20   only reason to 
ef310 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 69  read this page i
ef320 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20  s to obtain the 
ef330 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  page.        ** 
ef340 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e  number for the n
ef350 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ext page in the 
ef360 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20  overflow chain. 
ef370 54 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20  The page.       
ef380 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 20   ** data is not 
ef390 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 72  required. So fir
ef3a0 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70  st try to lookup
ef3b0 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20   the overflow.  
ef3c0 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69        ** page-li
ef3d0 73 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e 79  st cache, if any
ef3e0 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b  , then fall back
ef3f0 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 66   to the getOverf
ef400 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 20  lowPage().      
ef410 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20    ** function.. 
ef420 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65         */.#ifnde
ef430 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
ef440 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20 69  CRBLOB.        i
ef450 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  f( pCur->aOverfl
ef460 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65  ow && pCur->aOve
ef470 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b  rflow[iIdx+1] ){
ef480 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50  .          nextP
ef490 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65  age = pCur->aOve
ef4a0 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20  rflow[iIdx+1];. 
ef4b0 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a 23         } else .#
ef4c0 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20  endif.          
ef4d0 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77  rc = getOverflow
ef4e0 50 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50 61  Page(pBt, nextPa
ef4f0 67 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67 65  ge, 0, &nextPage
ef500 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65  );.        offse
ef510 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20  t -= ovflSize;. 
ef520 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
ef530 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72      /* Need to r
ef540 65 61 64 20 74 68 69 73 20 70 61 67 65 20 70 72  ead this page pr
ef550 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61  operly. It conta
ef560 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a  ins some of the.
ef570 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 65          ** range
ef580 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 73   of data that is
ef590 20 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f 70   being read (eOp
ef5a0 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e 20  ==0) or written 
ef5b0 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20 20  (eOp!=0)..      
ef5c0 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62 50    */.        DbP
ef5d0 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20  age *pDbPage;.  
ef5e0 20 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d        int a = am
ef5f0 74 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  t;.        rc = 
ef600 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
ef610 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78  pBt->pPager, nex
ef620 74 50 61 67 65 2c 20 26 70 44 62 50 61 67 65 29  tPage, &pDbPage)
ef630 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
ef640 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
ef650 20 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f 61           aPayloa
ef660 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  d = sqlite3Pager
ef670 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29  GetData(pDbPage)
ef680 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74  ;.          next
ef690 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
ef6a0 61 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20  aPayload);.     
ef6b0 20 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66 66       if( a + off
ef6c0 73 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29  set > ovflSize )
ef6d0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 20  {.            a 
ef6e0 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66  = ovflSize - off
ef6f0 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  set;.          }
ef700 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
ef710 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61  copyPayload(&aPa
ef720 79 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c  yload[offset+4],
ef730 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70   pBuf, a, eOp, p
ef740 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
ef750 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
ef760 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20  nref(pDbPage);. 
ef770 20 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20           offset 
ef780 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61  = 0;.          a
ef790 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20  mt -= a;.       
ef7a0 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20     pBuf += a;.  
ef7b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
ef7c0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
ef7d0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
ef7e0 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72  & amt>0 ){.    r
ef7f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
ef800 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
ef810 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
ef820 2a 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f  *.** Read part o
ef830 66 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63 69  f the key associ
ef840 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72  ated with cursor
ef850 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a   pCur.  Exactly.
ef860 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 77  ** "amt" bytes w
ef870 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 65  ill be transfere
ef880 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20  d into pBuf[].  
ef890 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20  The transfer.** 
ef8a0 62 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65  begins at "offse
ef8b0 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  t"..**.** Return
ef8c0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
ef8d0 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f  ccess or an erro
ef8e0 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69  r code if anythi
ef8f0 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67  ng goes.** wrong
ef900 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72  .  An error is r
ef910 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73  eturned if "offs
ef920 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65  et+amt" is large
ef930 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76  r than.** the av
ef940 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e  ailable payload.
ef950 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
ef960 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
ef970 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 20  reeKey(BtCursor 
ef980 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65  *pCur, u32 offse
ef990 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64  t, u32 amt, void
ef9a0 20 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72   *pBuf){.  int r
ef9b0 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  c;..  assert( cu
ef9c0 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
ef9d0 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72  Cur) );.  rc = r
ef9e0 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72  estoreOrClearCur
ef9f0 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72  sorPosition(pCur
efa00 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
efa10 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73  ITE_OK ){.    as
efa20 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
efa30 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
efa40 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
efa50 70 43 75 72 2d 3e 70 50 61 67 65 21 3d 30 20 29  pCur->pPage!=0 )
efa60 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  ;.    if( pCur->
efa70 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
efa80 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
efa90 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
efaa0 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  T;.    }.    ass
efab0 65 72 74 28 20 70 43 75 72 2d 3e 70 50 61 67 65  ert( pCur->pPage
efac0 2d 3e 69 6e 74 4b 65 79 3d 3d 30 20 29 3b 0a 20  ->intKey==0 );. 
efad0 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
efae0 3e 69 64 78 3e 3d 30 20 26 26 20 70 43 75 72 2d  >idx>=0 && pCur-
efaf0 3e 69 64 78 3c 70 43 75 72 2d 3e 70 50 61 67 65  >idx<pCur->pPage
efb00 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72  ->nCell );.    r
efb10 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61  c = accessPayloa
efb20 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20  d(pCur, offset, 
efb30 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  amt, (unsigned c
efb40 68 61 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29  har*)pBuf, 0, 0)
efb50 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
efb60 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  c;.}../*.** Read
efb70 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
efb80 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
efb90 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20  h cursor pCur.  
efba0 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22  Exactly.** "amt"
efbb0 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74   bytes will be t
efbc0 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70  ransfered into p
efbd0 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e  Buf[].  The tran
efbe0 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61  sfer.** begins a
efbf0 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a  t "offset"..**.*
efc00 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
efc10 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72  OK on success or
efc20 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
efc30 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
efc40 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72  ** wrong.  An er
efc50 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
efc60 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20  if "offset+amt" 
efc70 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a  is larger than.*
efc80 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  * the available 
efc90 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49  payload..*/.SQLI
efca0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
efcb0 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
efcc0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
efcd0 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
efce0 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29  amt, void *pBuf)
efcf0 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  {.  int rc;..  a
efd00 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
efd10 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
efd20 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 4f  .  rc = restoreO
efd30 72 43 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69  rClearCursorPosi
efd40 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66  tion(pCur);.  if
efd50 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
efd60 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
efd70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
efd80 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20  SOR_VALID );.   
efd90 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70   assert( pCur->p
efda0 50 61 67 65 21 3d 30 20 29 3b 0a 20 20 20 20 61  Page!=0 );.    a
efdb0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 64 78  ssert( pCur->idx
efdc0 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 69 64 78  >=0 && pCur->idx
efdd0 3c 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43  <pCur->pPage->nC
efde0 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ell );.    rc = 
efdf0 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43  accessPayload(pC
efe00 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c  ur, offset, amt,
efe10 20 70 42 75 66 2c 20 31 2c 20 30 29 3b 0a 20 20   pBuf, 1, 0);.  
efe20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
efe30 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
efe40 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 6c   pointer to payl
efe50 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  oad information 
efe60 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74  from the entry t
efe70 68 61 74 20 74 68 65 20 0a 2a 2a 20 70 43 75 72  hat the .** pCur
efe80 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74   cursor is point
efe90 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 70 6f 69  ing to.  The poi
efea0 6e 74 65 72 20 69 73 20 74 6f 20 74 68 65 20 62  nter is to the b
efeb0 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74  eginning of.** t
efec0 68 65 20 6b 65 79 20 69 66 20 73 6b 69 70 4b 65  he key if skipKe
efed0 79 3d 3d 30 20 61 6e 64 20 69 74 20 70 6f 69 6e  y==0 and it poin
efee0 74 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e  ts to the beginn
efef0 69 6e 67 20 6f 66 20 64 61 74 61 20 69 66 0a 2a  ing of data if.*
eff00 2a 20 73 6b 69 70 4b 65 79 3d 3d 31 2e 20 20 54  * skipKey==1.  T
eff10 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
eff20 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20  es of available 
eff30 6b 65 79 2f 64 61 74 61 20 69 73 20 77 72 69 74  key/data is writ
eff40 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d  ten.** into *pAm
eff50 74 2e 20 20 49 66 20 2a 70 41 6d 74 3d 3d 30 2c  t.  If *pAmt==0,
eff60 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
eff70 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 6f  returned will no
eff80 74 20 62 65 0a 2a 2a 20 61 20 76 61 6c 69 64 20  t be.** a valid 
eff90 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  pointer..**.** T
effa0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
effb0 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  n optimization. 
effc0 20 49 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f   It is common fo
effd0 72 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79  r the entire key
effe0 0a 2a 2a 20 61 6e 64 20 64 61 74 61 20 74 6f 20  .** and data to 
efff0 66 69 74 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c  fit on the local
f0000 20 70 61 67 65 20 61 6e 64 20 66 6f 72 20 74 68   page and for th
f0010 65 72 65 20 74 6f 20 62 65 20 6e 6f 20 6f 76 65  ere to be no ove
f0020 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 73 2e 20  rflow.** pages. 
f0030 20 57 68 65 6e 20 74 68 61 74 20 69 73 20 73 6f   When that is so
f0040 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63  , this routine c
f0050 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 61 63  an be used to ac
f0060 63 65 73 73 20 74 68 65 0a 2a 2a 20 6b 65 79 20  cess the.** key 
f0070 61 6e 64 20 64 61 74 61 20 77 69 74 68 6f 75 74  and data without
f0080 20 6d 61 6b 69 6e 67 20 61 20 63 6f 70 79 2e 20   making a copy. 
f0090 20 49 66 20 74 68 65 20 6b 65 79 20 61 6e 64 2f   If the key and/
f00a0 6f 72 20 64 61 74 61 20 73 70 69 6c 6c 73 0a 2a  or data spills.*
f00b0 2a 20 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 20  * onto overflow 
f00c0 70 61 67 65 73 2c 20 74 68 65 6e 20 61 63 63 65  pages, then acce
f00d0 73 73 50 61 79 6c 6f 61 64 28 29 20 6d 75 73 74  ssPayload() must
f00e0 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 73   be used to reas
f00f0 73 65 6d 62 6c 79 0a 2a 2a 20 74 68 65 20 6b 65  sembly.** the ke
f0100 79 2f 64 61 74 61 20 61 6e 64 20 63 6f 70 79 20  y/data and copy 
f0110 69 74 20 69 6e 74 6f 20 61 20 70 72 65 61 6c 6c  it into a preall
f0120 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a  ocated buffer..*
f0130 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72  *.** The pointer
f0140 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
f0150 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20  s routine looks 
f0160 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 74 68  directly into th
f0170 65 20 63 61 63 68 65 64 0a 2a 2a 20 70 61 67 65  e cached.** page
f0180 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
f0190 2e 20 20 54 68 65 20 64 61 74 61 20 6d 69 67 68  .  The data migh
f01a0 74 20 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76 65  t change or move
f01b0 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 0a 2a   the next time.*
f01c0 2a 20 61 6e 79 20 62 74 72 65 65 20 72 6f 75 74  * any btree rout
f01d0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ine is called..*
f01e0 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75  /.static const u
f01f0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 66 65  nsigned char *fe
f0200 74 63 68 50 61 79 6c 6f 61 64 28 0a 20 20 42 74  tchPayload(.  Bt
f0210 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20  Cursor *pCur,   
f0220 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69     /* Cursor poi
f0230 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74  nting to entry t
f0240 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20  o read from */. 
f0250 20 69 6e 74 20 2a 70 41 6d 74 2c 20 20 20 20 20   int *pAmt,     
f0260 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
f0270 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61  he number of ava
f0280 69 6c 61 62 6c 65 20 62 79 74 65 73 20 68 65 72  ilable bytes her
f0290 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4b  e */.  int skipK
f02a0 65 79 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72  ey          /* r
f02b0 65 61 64 20 62 65 67 69 6e 6e 69 6e 67 20 61 74  ead beginning at
f02c0 20 64 61 74 61 20 69 66 20 74 68 69 73 20 69 73   data if this is
f02d0 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e   true */.){.  un
f02e0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61  signed char *aPa
f02f0 79 6c 6f 61 64 3b 0a 20 20 4d 65 6d 50 61 67 65  yload;.  MemPage
f0300 20 2a 70 50 61 67 65 3b 0a 20 20 75 33 32 20 6e   *pPage;.  u32 n
f0310 4b 65 79 3b 0a 20 20 69 6e 74 20 6e 4c 6f 63 61  Key;.  int nLoca
f0320 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43  l;..  assert( pC
f0330 75 72 21 3d 30 20 26 26 20 70 43 75 72 2d 3e 70  ur!=0 && pCur->p
f0340 50 61 67 65 21 3d 30 20 29 3b 0a 20 20 61 73 73  Page!=0 );.  ass
f0350 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
f0360 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
f0370 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  );.  assert( cur
f0380 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
f0390 75 72 29 20 29 3b 0a 20 20 70 50 61 67 65 20 3d  ur) );.  pPage =
f03a0 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20   pCur->pPage;.  
f03b0 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 64  assert( pCur->id
f03c0 78 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 69 64  x>=0 && pCur->id
f03d0 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  x<pPage->nCell )
f03e0 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28  ;.  getCellInfo(
f03f0 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f 61  pCur);.  aPayloa
f0400 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70  d = pCur->info.p
f0410 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64  Cell;.  aPayload
f0420 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e   += pCur->info.n
f0430 48 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50  Header;.  if( pP
f0440 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
f0450 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d     nKey = 0;.  }
f0460 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d  else{.    nKey =
f0470 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79   pCur->info.nKey
f0480 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 6b 69 70  ;.  }.  if( skip
f0490 4b 65 79 20 29 7b 0a 20 20 20 20 61 50 61 79 6c  Key ){.    aPayl
f04a0 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20  oad += nKey;.   
f04b0 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e   nLocal = pCur->
f04c0 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6e 4b  info.nLocal - nK
f04d0 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ey;.  }else{.   
f04e0 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e   nLocal = pCur->
f04f0 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 20  info.nLocal;.   
f0500 20 69 66 28 20 6e 4c 6f 63 61 6c 3e 6e 4b 65 79   if( nLocal>nKey
f0510 20 29 7b 0a 20 20 20 20 20 20 6e 4c 6f 63 61 6c   ){.      nLocal
f0520 20 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 7d 0a 20   = nKey;.    }. 
f0530 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f   }.  *pAmt = nLo
f0540 63 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50  cal;.  return aP
f0550 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ayload;.}.../*.*
f0560 2a 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20  * For the entry 
f0570 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72  that cursor pCur
f0580 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65   is point to, re
f0590 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20  turn as.** many 
f05a0 62 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79  bytes of the key
f05b0 20 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20   or data as are 
f05c0 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65  available on the
f05d0 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65   local.** b-tree
f05e0 20 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68   page.  Write th
f05f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69  e number of avai
f0600 6c 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f  lable bytes into
f0610 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68   *pAmt..**.** Th
f0620 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e  e pointer return
f0630 65 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e  ed is ephemeral.
f0640 20 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d    The key/data m
f0650 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65  ay move.** or be
f0660 20 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68   destroyed on th
f0670 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61  e next call to a
f0680 6e 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65  ny Btree routine
f0690 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63  ,.** including c
f06a0 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20  alls from other 
f06b0 74 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20  threads against 
f06c0 74 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a  the same cache..
f06d0 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65  ** Hence, a mute
f06e0 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65  x on the BtShare
f06f0 64 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64  d should be held
f0700 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
f0710 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  g.** this routin
f0720 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  e..**.** These r
f0730 6f 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20  outines is used 
f0740 74 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63  to get quick acc
f0750 65 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64  ess to key and d
f0760 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f  ata.** in the co
f0770 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20  mmon case where 
f0780 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  no overflow page
f0790 73 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53  s are used..*/.S
f07a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
f07b0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
f07c0 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42  3BtreeKeyFetch(B
f07d0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69  tCursor *pCur, i
f07e0 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 61 73 73  nt *pAmt){.  ass
f07f0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
f0800 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
f0810 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
f0820 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
f0830 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 63  ){.    return (c
f0840 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68  onst void*)fetch
f0850 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41  Payload(pCur, pA
f0860 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65  mt, 0);.  }.  re
f0870 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45  turn 0;.}.SQLITE
f0880 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
f0890 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65  oid *sqlite3Btre
f08a0 65 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72  eDataFetch(BtCur
f08b0 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
f08c0 70 41 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28  pAmt){.  assert(
f08d0 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
f08e0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28  x(pCur) );.  if(
f08f0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
f0900 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20  URSOR_VALID ){. 
f0910 20 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74     return (const
f0920 20 76 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c   void*)fetchPayl
f0930 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20  oad(pCur, pAmt, 
f0940 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  1);.  }.  return
f0950 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f   0;.}.../*.** Mo
f0960 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f  ve the cursor do
f0970 77 6e 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c  wn to a new chil
f0980 64 20 70 61 67 65 2e 20 20 54 68 65 20 6e 65 77  d page.  The new
f0990 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 69 73  Pgno argument is
f09a0 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d   the.** page num
f09b0 62 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64  ber of the child
f09c0 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f   page to move to
f09d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f09e0 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75  moveToChild(BtCu
f09f0 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20  rsor *pCur, u32 
f0a00 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20  newPgno){.  int 
f0a10 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  rc;.  MemPage *p
f0a20 4e 65 77 50 61 67 65 3b 0a 20 20 4d 65 6d 50 61  NewPage;.  MemPa
f0a30 67 65 20 2a 70 4f 6c 64 50 61 67 65 3b 0a 20 20  ge *pOldPage;.  
f0a40 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
f0a50 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61 73  pCur->pBt;..  as
f0a60 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
f0a70 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
f0a80 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
f0a90 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
f0aa0 41 4c 49 44 20 29 3b 0a 20 20 72 63 20 3d 20 67  ALID );.  rc = g
f0ab0 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42  etAndInitPage(pB
f0ac0 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65  t, newPgno, &pNe
f0ad0 77 50 61 67 65 2c 20 70 43 75 72 2d 3e 70 50 61  wPage, pCur->pPa
f0ae0 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  ge);.  if( rc ) 
f0af0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 4e 65  return rc;.  pNe
f0b00 77 50 61 67 65 2d 3e 69 64 78 50 61 72 65 6e 74  wPage->idxParent
f0b10 20 3d 20 70 43 75 72 2d 3e 69 64 78 3b 0a 20 20   = pCur->idx;.  
f0b20 70 4f 6c 64 50 61 67 65 20 3d 20 70 43 75 72 2d  pOldPage = pCur-
f0b30 3e 70 50 61 67 65 3b 0a 20 20 70 4f 6c 64 50 61  >pPage;.  pOldPa
f0b40 67 65 2d 3e 69 64 78 53 68 69 66 74 20 3d 20 30  ge->idxShift = 0
f0b50 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  ;.  releasePage(
f0b60 70 4f 6c 64 50 61 67 65 29 3b 0a 20 20 70 43 75  pOldPage);.  pCu
f0b70 72 2d 3e 70 50 61 67 65 20 3d 20 70 4e 65 77 50  r->pPage = pNewP
f0b80 61 67 65 3b 0a 20 20 70 43 75 72 2d 3e 69 64 78  age;.  pCur->idx
f0b90 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e   = 0;.  pCur->in
f0ba0 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
f0bb0 69 66 28 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43  if( pNewPage->nC
f0bc0 65 6c 6c 3c 31 20 29 7b 0a 20 20 20 20 72 65 74  ell<1 ){.    ret
f0bd0 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
f0be0 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72  PT_BKPT;.  }.  r
f0bf0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f0c00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
f0c10 20 74 72 75 65 20 69 66 20 74 68 65 20 70 61 67   true if the pag
f0c20 65 20 69 73 20 74 68 65 20 76 69 72 74 75 61 6c  e is the virtual
f0c30 20 72 6f 6f 74 20 6f 66 20 69 74 73 20 74 61 62   root of its tab
f0c40 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 69  le..**.** The vi
f0c50 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20  rtual root page 
f0c60 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  is the root page
f0c70 20 66 6f 72 20 6d 6f 73 74 20 74 61 62 6c 65 73   for most tables
f0c80 2e 20 20 42 75 74 0a 2a 2a 20 66 6f 72 20 74 68  .  But.** for th
f0c90 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 6f  e table rooted o
f0ca0 6e 20 70 61 67 65 20 31 2c 20 73 6f 6d 65 74 69  n page 1, someti
f0cb0 6d 65 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74  me the real root
f0cc0 20 70 61 67 65 0a 2a 2a 20 69 73 20 65 6d 70 74   page.** is empt
f0cd0 79 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65  y except for the
f0ce0 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 2e 20   right-pointer. 
f0cf0 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74   In such cases t
f0d00 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 72 6f  he.** virtual ro
f0d10 6f 74 20 70 61 67 65 20 69 73 20 74 68 65 20 70  ot page is the p
f0d20 61 67 65 20 74 68 61 74 20 74 68 65 20 72 69 67  age that the rig
f0d30 68 74 2d 70 6f 69 6e 74 65 72 20 6f 66 20 70 61  ht-pointer of pa
f0d40 67 65 0a 2a 2a 20 31 20 69 73 20 70 6f 69 6e 74  ge.** 1 is point
f0d50 69 6e 67 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54  ing to..*/.SQLIT
f0d60 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f0d70 6c 69 74 65 33 42 74 72 65 65 49 73 52 6f 6f 74  lite3BtreeIsRoot
f0d80 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
f0d90 61 67 65 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  age){.  MemPage 
f0da0 2a 70 50 61 72 65 6e 74 3b 0a 0a 20 20 61 73 73  *pParent;..  ass
f0db0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
f0dc0 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
f0dd0 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
f0de0 70 50 61 72 65 6e 74 20 3d 20 70 50 61 67 65 2d  pParent = pPage-
f0df0 3e 70 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20  >pParent;.  if( 
f0e00 70 50 61 72 65 6e 74 3d 3d 30 20 29 20 72 65 74  pParent==0 ) ret
f0e10 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 70 50 61  urn 1;.  if( pPa
f0e20 72 65 6e 74 2d 3e 70 67 6e 6f 3e 31 20 29 20 72  rent->pgno>1 ) r
f0e30 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 67  eturn 0;.  if( g
f0e40 65 74 32 62 79 74 65 28 26 70 50 61 72 65 6e 74  et2byte(&pParent
f0e50 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d  ->aData[pParent-
f0e60 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d 29 3d 3d  >hdrOffset+3])==
f0e70 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
f0e80 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
f0e90 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  ** Move the curs
f0ea0 6f 72 20 75 70 20 74 6f 20 74 68 65 20 70 61 72  or up to the par
f0eb0 65 6e 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  ent page..**.** 
f0ec0 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74  pCur->idx is set
f0ed0 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 64   to the cell ind
f0ee0 65 78 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ex that contains
f0ef0 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20   the pointer.** 
f0f00 74 6f 20 74 68 65 20 70 61 67 65 20 77 65 20 61  to the page we a
f0f10 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20  re coming from. 
f0f20 20 49 66 20 77 65 20 61 72 65 20 63 6f 6d 69 6e   If we are comin
f0f30 67 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69  g from the.** ri
f0f40 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70  ght-most child p
f0f50 61 67 65 20 74 68 65 6e 20 70 43 75 72 2d 3e 69  age then pCur->i
f0f60 64 78 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65  dx is set to one
f0f70 20 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68   more than.** th
f0f80 65 20 6c 61 72 67 65 73 74 20 63 65 6c 6c 20 69  e largest cell i
f0f90 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ndex..*/.SQLITE_
f0fa0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
f0fb0 69 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50  ite3BtreeMoveToP
f0fc0 61 72 65 6e 74 28 42 74 43 75 72 73 6f 72 20 2a  arent(BtCursor *
f0fd0 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65  pCur){.  MemPage
f0fe0 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 4d 65 6d   *pParent;.  Mem
f0ff0 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69  Page *pPage;.  i
f1000 6e 74 20 69 64 78 50 61 72 65 6e 74 3b 0a 0a 20  nt idxParent;.. 
f1010 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
f1020 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
f1030 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
f1040 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
f1050 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 70 50 61  R_VALID );.  pPa
f1060 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65  ge = pCur->pPage
f1070 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
f1080 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  e!=0 );.  assert
f1090 28 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 49  ( !sqlite3BtreeI
f10a0 73 52 6f 6f 74 50 61 67 65 28 70 50 61 67 65 29  sRootPage(pPage)
f10b0 20 29 3b 0a 20 20 70 50 61 72 65 6e 74 20 3d 20   );.  pParent = 
f10c0 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 3b 0a  pPage->pParent;.
f10d0 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
f10e0 74 21 3d 30 20 29 3b 0a 20 20 69 64 78 50 61 72  t!=0 );.  idxPar
f10f0 65 6e 74 20 3d 20 70 50 61 67 65 2d 3e 69 64 78  ent = pPage->idx
f1100 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74 65  Parent;.  sqlite
f1110 33 50 61 67 65 72 52 65 66 28 70 50 61 72 65 6e  3PagerRef(pParen
f1120 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 72  t->pDbPage);.  r
f1130 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
f1140 29 3b 0a 20 20 70 43 75 72 2d 3e 70 50 61 67 65  );.  pCur->pPage
f1150 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 70 43   = pParent;.  pC
f1160 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
f1170 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   0;.  assert( pP
f1180 61 72 65 6e 74 2d 3e 69 64 78 53 68 69 66 74 3d  arent->idxShift=
f1190 3d 30 20 29 3b 0a 20 20 70 43 75 72 2d 3e 69 64  =0 );.  pCur->id
f11a0 78 20 3d 20 69 64 78 50 61 72 65 6e 74 3b 0a 7d  x = idxParent;.}
f11b0 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  ../*.** Move the
f11c0 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 72   cursor to the r
f11d0 6f 6f 74 20 70 61 67 65 0a 2a 2f 0a 73 74 61 74  oot page.*/.stat
f11e0 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f  ic int moveToRoo
f11f0 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t(BtCursor *pCur
f1200 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52  ){.  MemPage *pR
f1210 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  oot;.  int rc = 
f1220 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72  SQLITE_OK;.  Btr
f1230 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42  ee *p = pCur->pB
f1240 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64  tree;.  BtShared
f1250 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
f1260 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
f1270 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
f1280 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43  ) );.  assert( C
f1290 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20  URSOR_INVALID < 
f12a0 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
f12b0 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  EK );.  assert( 
f12c0 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c  CURSOR_VALID   <
f12d0 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
f12e0 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  EEK );.  assert(
f12f0 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20   CURSOR_FAULT   
f1300 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  > CURSOR_REQUIRE
f1310 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43  SEEK );.  if( pC
f1320 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53  ur->eState>=CURS
f1330 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
f1340 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  {.    if( pCur->
f1350 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46  eState==CURSOR_F
f1360 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 72 65  AULT ){.      re
f1370 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b  turn pCur->skip;
f1380 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c 65 61 72  .    }.    clear
f1390 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
f13a0 43 75 72 29 3b 0a 20 20 7d 0a 20 20 70 52 6f 6f  Cur);.  }.  pRoo
f13b0 74 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b  t = pCur->pPage;
f13c0 0a 20 20 69 66 28 20 70 52 6f 6f 74 20 26 26 20  .  if( pRoot && 
f13d0 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75  pRoot->pgno==pCu
f13e0 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20  r->pgnoRoot ){. 
f13f0 20 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74     assert( pRoot
f1400 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 7d 65  ->isInit );.  }e
f1410 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 0a 20 20  lse{.    if( .  
f1420 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28      SQLITE_OK!=(
f1430 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50  rc = getAndInitP
f1440 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e 70  age(pBt, pCur->p
f1450 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c  gnoRoot, &pRoot,
f1460 20 30 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20   0)).    ){.    
f1470 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
f1480 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b   CURSOR_INVALID;
f1490 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
f14a0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65  ;.    }.    rele
f14b0 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 70 50  asePage(pCur->pP
f14c0 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e  age);.    pCur->
f14d0 70 50 61 67 65 20 3d 20 70 52 6f 6f 74 3b 0a 20  pPage = pRoot;. 
f14e0 20 7d 0a 20 20 70 43 75 72 2d 3e 69 64 78 20 3d   }.  pCur->idx =
f14f0 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f   0;.  pCur->info
f1500 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66  .nSize = 0;.  if
f1510 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d  ( pRoot->nCell==
f1520 30 20 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 61  0 && !pRoot->lea
f1530 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75  f ){.    Pgno su
f1540 62 70 61 67 65 3b 0a 20 20 20 20 61 73 73 65 72  bpage;.    asser
f1550 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d  t( pRoot->pgno==
f1560 31 20 29 3b 0a 20 20 20 20 73 75 62 70 61 67 65  1 );.    subpage
f1570 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f   = get4byte(&pRo
f1580 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d  ot->aData[pRoot-
f1590 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a  >hdrOffset+8]);.
f15a0 20 20 20 20 61 73 73 65 72 74 28 20 73 75 62 70      assert( subp
f15b0 61 67 65 3e 30 20 29 3b 0a 20 20 20 20 70 43 75  age>0 );.    pCu
f15c0 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
f15d0 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63  OR_VALID;.    rc
f15e0 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70   = moveToChild(p
f15f0 43 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20  Cur, subpage);. 
f1600 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74   }.  pCur->eStat
f1610 65 20 3d 20 28 28 70 43 75 72 2d 3e 70 50 61 67  e = ((pCur->pPag
f1620 65 2d 3e 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53  e->nCell>0)?CURS
f1630 4f 52 5f 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f  OR_VALID:CURSOR_
f1640 49 4e 56 41 4c 49 44 29 3b 0a 20 20 72 65 74 75  INVALID);.  retu
f1650 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
f1660 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
f1670 64 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 74  down to the left
f1680 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79  -most leaf entry
f1690 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20   beneath the.** 
f16a0 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 69  entry to which i
f16b0 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  t is currently p
f16c0 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  ointing..**.** T
f16d0 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61  he left-most lea
f16e0 66 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74  f is the one wit
f16f0 68 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b  h the smallest k
f1700 65 79 20 2d 20 74 68 65 20 66 69 72 73 74 0a 2a  ey - the first.*
f1710 2a 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f  * in ascending o
f1720 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rder..*/.static 
f1730 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  int moveToLeftmo
f1740 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  st(BtCursor *pCu
f1750 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b  r){.  Pgno pgno;
f1760 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
f1770 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65  TE_OK;.  MemPage
f1780 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65   *pPage;..  asse
f1790 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
f17a0 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
f17b0 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
f17c0 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
f17d0 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72  ID );.  while( r
f17e0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
f17f0 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e  !(pPage = pCur->
f1800 70 50 61 67 65 29 2d 3e 6c 65 61 66 20 29 7b 0a  pPage)->leaf ){.
f1810 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
f1820 2d 3e 69 64 78 3e 3d 30 20 26 26 20 70 43 75 72  ->idx>=0 && pCur
f1830 2d 3e 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65  ->idx<pPage->nCe
f1840 6c 6c 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d  ll );.    pgno =
f1850 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65   get4byte(findCe
f1860 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e  ll(pPage, pCur->
f1870 69 64 78 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  idx));.    rc = 
f1880 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
f1890 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72  , pgno);.  }.  r
f18a0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
f18b0 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  ** Move the curs
f18c0 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 72  or down to the r
f18d0 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65  ight-most leaf e
f18e0 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
f18f0 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77 68 69 63  .** page to whic
f1900 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  h it is currentl
f1910 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74  y pointing.  Not
f1920 69 63 65 20 74 68 65 20 64 69 66 66 65 72 65 6e  ice the differen
f1930 63 65 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f  ce.** between mo
f1940 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61  veToLeftmost() a
f1950 6e 64 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f  nd moveToRightmo
f1960 73 74 28 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66  st().  moveToLef
f1970 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73  tmost().** finds
f1980 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65   the left-most e
f1990 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
f19a0 20 2a 65 6e 74 72 79 2a 20 77 68 65 72 65 61 73   *entry* whereas
f19b0 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
f19c0 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20  ().** finds the 
f19d0 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79  right-most entry
f19e0 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 70 61   beneath the *pa
f19f0 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ge*..**.** The r
f1a00 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20  ight-most entry 
f1a10 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20  is the one with 
f1a20 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20  the largest key 
f1a30 2d 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65  - the last.** ke
f1a40 79 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f  y in ascending o
f1a50 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rder..*/.static 
f1a60 69 6e 74 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d  int moveToRightm
f1a70 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ost(BtCursor *pC
f1a80 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  ur){.  Pgno pgno
f1a90 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
f1aa0 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67  ITE_OK;.  MemPag
f1ab0 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73  e *pPage;..  ass
f1ac0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
f1ad0 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
f1ae0 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
f1af0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
f1b00 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  LID );.  while( 
f1b10 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
f1b20 20 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d   !(pPage = pCur-
f1b30 3e 70 50 61 67 65 29 2d 3e 6c 65 61 66 20 29 7b  >pPage)->leaf ){
f1b40 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34  .    pgno = get4
f1b50 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
f1b60 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
f1b70 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75  set+8]);.    pCu
f1b80 72 2d 3e 69 64 78 20 3d 20 70 50 61 67 65 2d 3e  r->idx = pPage->
f1b90 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d 20  nCell;.    rc = 
f1ba0 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
f1bb0 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 69  , pgno);.  }.  i
f1bc0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
f1bd0 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 64   ){.    pCur->id
f1be0 78 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  x = pPage->nCell
f1bf0 20 2d 20 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e   - 1;.    pCur->
f1c00 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a  info.nSize = 0;.
f1c10 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
f1c20 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 6f  ITE_OK;.}../* Mo
f1c30 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
f1c40 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
f1c50 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20   in the table.  
f1c60 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
f1c70 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  .** on success. 
f1c80 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20   Set *pRes to 0 
f1c90 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63  if the cursor ac
f1ca0 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f  tually points to
f1cb0 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72   something.** or
f1cc0 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20   set *pRes to 1 
f1cd0 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
f1ce0 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  empty..*/.SQLITE
f1cf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
f1d00 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42  ite3BtreeFirst(B
f1d10 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69  tCursor *pCur, i
f1d20 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74  nt *pRes){.  int
f1d30 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
f1d40 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
f1d50 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
f1d60 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
f1d70 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74  x_held(pCur->pBt
f1d80 72 65 65 2d 3e 70 53 71 6c 69 74 65 2d 3e 6d 75  ree->pSqlite->mu
f1d90 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d  tex) );.  rc = m
f1da0 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b  oveToRoot(pCur);
f1db0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
f1dc0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
f1dd0 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
f1de0 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a  RSOR_INVALID ){.
f1df0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43        assert( pC
f1e00 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ur->pPage->nCell
f1e10 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52  ==0 );.      *pR
f1e20 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63  es = 1;.      rc
f1e30 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f1e40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
f1e50 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 50 61  ssert( pCur->pPa
f1e60 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20  ge->nCell>0 );. 
f1e70 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a       *pRes = 0;.
f1e80 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54        rc = moveT
f1e90 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b  oLeftmost(pCur);
f1ea0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
f1eb0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f  urn rc;.}../* Mo
f1ec0 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
f1ed0 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
f1ee0 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52  in the table.  R
f1ef0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a  eturn SQLITE_OK.
f1f00 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  ** on success.  
f1f10 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69  Set *pRes to 0 i
f1f20 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74  f the cursor act
f1f30 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20  ually points to 
f1f40 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20  something.** or 
f1f50 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69  set *pRes to 1 i
f1f60 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  f the table is e
f1f70 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mpty..*/.SQLITE_
f1f80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
f1f90 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43  te3BtreeLast(BtC
f1fa0 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74  ursor *pCur, int
f1fb0 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72   *pRes){.  int r
f1fc0 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63  c;. .  assert( c
f1fd0 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
f1fe0 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
f1ff0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
f2000 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72  _held(pCur->pBtr
f2010 65 65 2d 3e 70 53 71 6c 69 74 65 2d 3e 6d 75 74  ee->pSqlite->mut
f2020 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f  ex) );.  rc = mo
f2030 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a  veToRoot(pCur);.
f2040 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
f2050 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 43  _OK ){.    if( C
f2060 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70  URSOR_INVALID==p
f2070 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20  Cur->eState ){. 
f2080 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75       assert( pCu
f2090 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d  r->pPage->nCell=
f20a0 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65  =0 );.      *pRe
f20b0 73 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  s = 1;.    }else
f20c0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
f20d0 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
f20e0 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
f20f0 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
f2100 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f       rc = moveTo
f2110 52 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b  Rightmost(pCur);
f2120 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
f2130 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f  urn rc;.}../* Mo
f2140 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 73 6f  ve the cursor so
f2150 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
f2160 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 72  to an entry near
f2170 20 70 4b 65 79 2f 6e 4b 65 79 2e 0a 2a 2a 20 52   pKey/nKey..** R
f2180 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20  eturn a success 
f2190 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  code..**.** For 
f21a0 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f  INTKEY tables, o
f21b0 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 70 61 72  nly the nKey par
f21c0 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e 20  ameter is used. 
f21d0 20 70 4b 65 79 20 69 73 0a 2a 2a 20 69 67 6e 6f   pKey is.** igno
f21e0 72 65 64 2e 20 20 46 6f 72 20 6f 74 68 65 72 20  red.  For other 
f21f0 74 61 62 6c 65 73 2c 20 6e 4b 65 79 20 69 73 20  tables, nKey is 
f2200 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
f2210 74 65 73 20 6f 66 20 64 61 74 61 0a 2a 2a 20 69  tes of data.** i
f2220 6e 20 70 4b 65 79 2e 20 20 54 68 65 20 63 6f 6d  n pKey.  The com
f2230 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
f2240 20 73 70 65 63 69 66 69 65 64 20 77 68 65 6e 20   specified when 
f2250 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 0a 2a  the cursor was.*
f2260 2a 20 63 72 65 61 74 65 64 20 69 73 20 75 73 65  * created is use
f2270 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 6b 65 79  d to compare key
f2280 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  s..**.** If an e
f2290 78 61 63 74 20 6d 61 74 63 68 20 69 73 20 6e 6f  xact match is no
f22a0 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68  t found, then th
f22b0 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61  e cursor is alwa
f22c0 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f 69 6e 74  ys.** left point
f22d0 69 6e 67 20 61 74 20 61 20 6c 65 61 66 20 70 61  ing at a leaf pa
f22e0 67 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 68  ge which would h
f22f0 6f 6c 64 20 74 68 65 20 65 6e 74 72 79 20 69 66  old the entry if
f2300 20 69 74 0a 2a 2a 20 77 65 72 65 20 70 72 65 73   it.** were pres
f2310 65 6e 74 2e 20 20 54 68 65 20 63 75 72 73 6f 72  ent.  The cursor
f2320 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20   might point to 
f2330 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 63 6f  an entry that co
f2340 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65 20 6f 72  mes.** before or
f2350 20 61 66 74 65 72 20 74 68 65 20 6b 65 79 2e 0a   after the key..
f2360 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  **.** The result
f2370 20 6f 66 20 63 6f 6d 70 61 72 69 6e 67 20 74 68   of comparing th
f2380 65 20 6b 65 79 20 77 69 74 68 20 74 68 65 20 65  e key with the e
f2390 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68  ntry to which th
f23a0 65 0a 2a 2a 20 63 75 72 73 6f 72 20 69 73 20 77  e.** cursor is w
f23b0 72 69 74 74 65 6e 20 74 6f 20 2a 70 52 65 73 20  ritten to *pRes 
f23c0 69 66 20 70 52 65 73 21 3d 4e 55 4c 4c 2e 20 20  if pRes!=NULL.  
f23d0 54 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 0a 2a  The meaning of.*
f23e0 2a 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20  * this value is 
f23f0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
f2400 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20  *     *pRes<0   
f2410 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73     The cursor is
f2420 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
f2430 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a  t an entry that.
f2440 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
f2450 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74      is smaller t
f2460 68 61 6e 20 70 4b 65 79 20 6f 72 20 69 66 20 74  han pKey or if t
f2470 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
f2480 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  y.**            
f2490 20 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 75        and the cu
f24a0 72 73 6f 72 20 69 73 20 74 68 65 72 65 66 6f 72  rsor is therefor
f24b0 65 20 6c 65 66 74 20 70 6f 69 6e 74 20 74 6f 20  e left point to 
f24c0 6e 6f 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20  nothing..**.**  
f24d0 20 20 20 2a 70 52 65 73 3d 3d 30 20 20 20 20 20     *pRes==0     
f24e0 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
f24f0 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
f2500 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20  n entry that.** 
f2510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2520 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73   exactly matches
f2530 20 70 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20   pKey..**.**    
f2540 20 2a 70 52 65 73 3e 30 20 20 20 20 20 20 54 68   *pRes>0      Th
f2550 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
f2560 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20   pointing at an 
f2570 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20  entry that.**   
f2580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
f2590 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 70 4b  s larger than pK
f25a0 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  ey..**.*/.SQLITE
f25b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
f25c0 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28  ite3BtreeMoveto(
f25d0 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
f25e0 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r,        /* The
f25f0 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f   cursor to be mo
f2600 76 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ved */.  const v
f2610 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 20 20 20  oid *pKey,      
f2620 2f 2a 20 54 68 65 20 6b 65 79 20 63 6f 6e 74 65  /* The key conte
f2630 6e 74 20 66 6f 72 20 69 6e 64 69 63 65 73 2e 20  nt for indices. 
f2640 20 4e 6f 74 20 75 73 65 64 20 62 79 20 74 61 62   Not used by tab
f2650 6c 65 73 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65  les */.  i64 nKe
f2660 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
f2670 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 2e  /* Size of pKey.
f2680 20 20 4f 72 20 74 68 65 20 6b 65 79 20 66 6f 72    Or the key for
f2690 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e 74   tables */.  int
f26a0 20 62 69 61 73 52 69 67 68 74 2c 20 20 20 20 20   biasRight,     
f26b0 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
f26c0 62 69 61 73 20 74 68 65 20 73 65 61 72 63 68 20  bias the search 
f26d0 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20  to the high end 
f26e0 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20  */.  int *pRes  
f26f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
f2700 65 61 72 63 68 20 72 65 73 75 6c 74 20 66 6c 61  earch result fla
f2710 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  g */.){.  int rc
f2720 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
f2730 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
f2740 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
f2750 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
f2760 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65  eld(pCur->pBtree
f2770 2d 3e 70 53 71 6c 69 74 65 2d 3e 6d 75 74 65 78  ->pSqlite->mutex
f2780 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65  ) );.  rc = move
f2790 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20  ToRoot(pCur);.  
f27a0 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
f27b0 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
f27c0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 50 61  ssert( pCur->pPa
f27d0 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ge );.  assert( 
f27e0 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 69 73 49  pCur->pPage->isI
f27f0 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 70 43 75  nit );.  if( pCu
f2800 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
f2810 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20  R_INVALID ){.   
f2820 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20   *pRes = -1;.   
f2830 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70   assert( pCur->p
f2840 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  Page->nCell==0 )
f2850 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
f2860 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 66 6f  ITE_OK;.  }.  fo
f2870 72 28 3b 3b 29 7b 0a 20 20 20 20 69 6e 74 20 6c  r(;;){.    int l
f2880 77 72 2c 20 75 70 72 3b 0a 20 20 20 20 50 67 6e  wr, upr;.    Pgn
f2890 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65  o chldPg;.    Me
f28a0 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70  mPage *pPage = p
f28b0 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 20 20  Cur->pPage;.    
f28c0 69 6e 74 20 63 20 3d 20 2d 31 3b 20 20 2f 2a 20  int c = -1;  /* 
f28d0 70 52 65 73 20 72 65 74 75 72 6e 20 69 66 20 74  pRes return if t
f28e0 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20 6d 75  able is empty mu
f28f0 73 74 20 62 65 20 2d 31 20 2a 2f 0a 20 20 20 20  st be -1 */.    
f2900 6c 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72  lwr = 0;.    upr
f2910 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d   = pPage->nCell-
f2920 31 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  1;.    if( !pPag
f2930 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 70 4b 65  e->intKey && pKe
f2940 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  y==0 ){.      re
f2950 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
f2960 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
f2970 20 20 20 20 69 66 28 20 62 69 61 73 52 69 67 68      if( biasRigh
f2980 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d  t ){.      pCur-
f2990 3e 69 64 78 20 3d 20 75 70 72 3b 0a 20 20 20 20  >idx = upr;.    
f29a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75  }else{.      pCu
f29b0 72 2d 3e 69 64 78 20 3d 20 28 75 70 72 2b 6c 77  r->idx = (upr+lw
f29c0 72 29 2f 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20  r)/2;.    }.    
f29d0 69 66 28 20 6c 77 72 3c 3d 75 70 72 20 29 20 66  if( lwr<=upr ) f
f29e0 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 76 6f  or(;;){.      vo
f29f0 69 64 20 2a 70 43 65 6c 6c 4b 65 79 3b 0a 20 20  id *pCellKey;.  
f2a00 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79      i64 nCellKey
f2a10 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e  ;.      pCur->in
f2a20 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
f2a30 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69      if( pPage->i
f2a40 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  ntKey ){.       
f2a50 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20   u8 *pCell;.    
f2a60 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64      pCell = find
f2a70 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72  Cell(pPage, pCur
f2a80 2d 3e 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e  ->idx) + pPage->
f2a90 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20  childPtrSize;.  
f2aa0 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d        if( pPage-
f2ab0 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20  >hasData ){.    
f2ac0 20 20 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b        u32 dummy;
f2ad0 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c  .          pCell
f2ae0 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28   += getVarint32(
f2af0 70 43 65 6c 6c 2c 20 26 64 75 6d 6d 79 29 3b 0a  pCell, &dummy);.
f2b00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
f2b10 20 20 67 65 74 56 61 72 69 6e 74 28 70 43 65 6c    getVarint(pCel
f2b20 6c 2c 20 28 75 36 34 20 2a 29 26 6e 43 65 6c 6c  l, (u64 *)&nCell
f2b30 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Key);.        if
f2b40 28 20 6e 43 65 6c 6c 4b 65 79 3c 6e 4b 65 79 20  ( nCellKey<nKey 
f2b50 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d  ){.          c =
f2b60 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c   -1;.        }el
f2b70 73 65 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3e  se if( nCellKey>
f2b80 6e 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  nKey ){.        
f2b90 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20    c = +1;.      
f2ba0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
f2bb0 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 20 20     c = 0;.      
f2bc0 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
f2bd0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 76 61  .        int ava
f2be0 69 6c 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20  ilable;.        
f2bf0 70 43 65 6c 6c 4b 65 79 20 3d 20 28 76 6f 69 64  pCellKey = (void
f2c00 20 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28   *)fetchPayload(
f2c10 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65  pCur, &available
f2c20 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 6e 43  , 0);.        nC
f2c30 65 6c 6c 4b 65 79 20 3d 20 70 43 75 72 2d 3e 69  ellKey = pCur->i
f2c40 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20  nfo.nKey;.      
f2c50 20 20 69 66 28 20 61 76 61 69 6c 61 62 6c 65 3e    if( available>
f2c60 3d 6e 43 65 6c 6c 4b 65 79 20 29 7b 0a 20 20 20  =nCellKey ){.   
f2c70 20 20 20 20 20 20 20 63 20 3d 20 70 43 75 72 2d         c = pCur-
f2c80 3e 78 43 6f 6d 70 61 72 65 28 70 43 75 72 2d 3e  >xCompare(pCur->
f2c90 70 41 72 67 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20  pArg, nCellKey, 
f2ca0 70 43 65 6c 6c 4b 65 79 2c 20 6e 4b 65 79 2c 20  pCellKey, nKey, 
f2cb0 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d  pKey);.        }
f2cc0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
f2cd0 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74  pCellKey = sqlit
f2ce0 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c  e3_malloc( nCell
f2cf0 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20  Key );.         
f2d00 20 69 66 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30   if( pCellKey==0
f2d10 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
f2d20 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
f2d30 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
f2d40 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c 20  reeKey(pCur, 0, 
f2d50 6e 43 65 6c 6c 4b 65 79 2c 20 28 76 6f 69 64 20  nCellKey, (void 
f2d60 2a 29 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20  *)pCellKey);.   
f2d70 20 20 20 20 20 20 20 63 20 3d 20 70 43 75 72 2d         c = pCur-
f2d80 3e 78 43 6f 6d 70 61 72 65 28 70 43 75 72 2d 3e  >xCompare(pCur->
f2d90 70 41 72 67 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20  pArg, nCellKey, 
f2da0 70 43 65 6c 6c 4b 65 79 2c 20 6e 4b 65 79 2c 20  pCellKey, nKey, 
f2db0 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20  pKey);.         
f2dc0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
f2dd0 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  ellKey);.       
f2de0 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
f2df0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
f2e00 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  rc;.          }.
f2e10 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
f2e20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30  }.      if( c==0
f2e30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
f2e40 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61 20  pPage->leafData 
f2e50 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  && !pPage->leaf 
f2e60 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72  ){.          lwr
f2e70 20 3d 20 70 43 75 72 2d 3e 69 64 78 3b 0a 20 20   = pCur->idx;.  
f2e80 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 6c 77          upr = lw
f2e90 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  r - 1;.         
f2ea0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
f2eb0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
f2ec0 20 69 66 28 20 70 52 65 73 20 29 20 2a 70 52 65   if( pRes ) *pRe
f2ed0 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  s = 0;.         
f2ee0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f2ef0 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  K;.        }.   
f2f00 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63     }.      if( c
f2f10 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 77  <0 ){.        lw
f2f20 72 20 3d 20 70 43 75 72 2d 3e 69 64 78 2b 31 3b  r = pCur->idx+1;
f2f30 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
f2f40 20 20 20 20 20 20 75 70 72 20 3d 20 70 43 75 72        upr = pCur
f2f50 2d 3e 69 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d  ->idx-1;.      }
f2f60 0a 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75  .      if( lwr>u
f2f70 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72  pr ){.        br
f2f80 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
f2f90 20 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20 28     pCur->idx = (
f2fa0 6c 77 72 2b 75 70 72 29 2f 32 3b 0a 20 20 20 20  lwr+upr)/2;.    
f2fb0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 77  }.    assert( lw
f2fc0 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20 20  r==upr+1 );.    
f2fd0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
f2fe0 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 28  sInit );.    if(
f2ff0 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a   pPage->leaf ){.
f3000 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 30        chldPg = 0
f3010 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
f3020 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  lwr>=pPage->nCel
f3030 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50  l ){.      chldP
f3040 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  g = get4byte(&pP
f3050 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
f3060 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
f3070 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
f3080 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62    chldPg = get4b
f3090 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61  yte(findCell(pPa
f30a0 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 7d  ge, lwr));.    }
f30b0 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67 3d  .    if( chldPg=
f30c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
f30d0 72 74 28 20 70 43 75 72 2d 3e 69 64 78 3e 3d 30  rt( pCur->idx>=0
f30e0 20 26 26 20 70 43 75 72 2d 3e 69 64 78 3c 70 43   && pCur->idx<pC
f30f0 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ur->pPage->nCell
f3100 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52   );.      if( pR
f3110 65 73 20 29 20 2a 70 52 65 73 20 3d 20 63 3b 0a  es ) *pRes = c;.
f3120 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
f3130 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
f3140 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20 6c 77    pCur->idx = lw
f3150 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66  r;.    pCur->inf
f3160 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20  o.nSize = 0;.   
f3170 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
f3180 64 28 70 43 75 72 2c 20 63 68 6c 64 50 67 29 3b  d(pCur, chldPg);
f3190 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
f31a0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
f31b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 4e      }.  }.  /* N
f31c0 4f 54 20 52 45 41 43 48 45 44 20 2a 2f 0a 7d 0a  OT REACHED */.}.
f31d0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
f31e0 52 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f  RUE if the curso
f31f0 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e  r is not pointin
f3200 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66  g at an entry of
f3210 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
f3220 2a 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72  * TRUE will be r
f3230 65 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20  eturned after a 
f3240 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42  call to sqlite3B
f3250 74 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73  treeNext() moves
f3260 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73  .** past the las
f3270 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74  t entry in the t
f3280 61 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42  able or sqlite3B
f3290 74 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73  treePrev() moves
f32a0 20 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72   past.** the fir
f32b0 73 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20  st entry.  TRUE 
f32c0 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64  is also returned
f32d0 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   if the table is
f32e0 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54   empty..*/.SQLIT
f32f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f3300 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74  lite3BtreeEof(Bt
f3310 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
f3320 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69   /* TODO: What i
f3330 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
f3340 69 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52  in CURSOR_REQUIR
f3350 45 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61  ESEEK but all ta
f3360 62 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a  ble entries.  **
f3370 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74   have been delet
f3380 65 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c  ed? This API wil
f3390 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65  l need to change
f33a0 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72   to return an er
f33b0 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73  ror code.  ** as
f33c0 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f   well as the boo
f33d0 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75  lean result valu
f33e0 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e  e..  */.  return
f33f0 20 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d   (CURSOR_VALID!=
f3400 70 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d  pCur->eState);.}
f3410 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
f3420 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
f3430 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f  ection handle fo
f3440 72 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53  r a cursor..*/.S
f3450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
f3460 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42 74  lite3 *sqlite3Bt
f3470 72 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e 73  reeCursorDb(cons
f3480 74 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t BtCursor *pCur
f3490 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
f34a0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
f34b0 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 70 53  pCur->pBtree->pS
f34c0 71 6c 69 74 65 2d 3e 6d 75 74 65 78 29 20 29 3b  qlite->mutex) );
f34d0 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e  .  return pCur->
f34e0 70 42 74 72 65 65 2d 3e 70 53 71 6c 69 74 65 3b  pBtree->pSqlite;
f34f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63  .}../*.** Advanc
f3500 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
f3510 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 69  the next entry i
f3520 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
f3530 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75   If.** successfu
f3540 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73  l then set *pRes
f3550 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 73  =0.  If the curs
f3560 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64  or.** was alread
f3570 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  y pointing to th
f3580 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20  e last entry in 
f3590 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65 66  the database bef
f35a0 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  ore.** this rout
f35b0 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20  ine was called, 
f35c0 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31  then set *pRes=1
f35d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f35e0 62 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73  btreeNext(BtCurs
f35f0 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
f3600 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Res){.  int rc;.
f3610 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
f3620 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
f3630 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
f3640 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65  ur) );.  rc = re
f3650 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73  storeOrClearCurs
f3660 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
f3670 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
f3680 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
f3690 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73  urn rc;.  }.  as
f36a0 73 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b  sert( pRes!=0 );
f36b0 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
f36c0 3e 70 50 61 67 65 3b 0a 20 20 69 66 28 20 43 55  >pPage;.  if( CU
f36d0 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43  RSOR_INVALID==pC
f36e0 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20  ur->eState ){.  
f36f0 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20    *pRes = 1;.   
f3700 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f3710 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75  K;.  }.  if( pCu
f3720 72 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a 20 20 20  r->skip>0 ){.   
f3730 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b   pCur->skip = 0;
f3740 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a  .    *pRes = 0;.
f3750 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
f3760 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72  E_OK;.  }.  pCur
f3770 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a 20 20 61  ->skip = 0;..  a
f3780 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73  ssert( pPage->is
f3790 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74  Init );.  assert
f37a0 28 20 70 43 75 72 2d 3e 69 64 78 3c 70 50 61 67  ( pCur->idx<pPag
f37b0 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 70  e->nCell );..  p
f37c0 43 75 72 2d 3e 69 64 78 2b 2b 3b 0a 20 20 70 43  Cur->idx++;.  pC
f37d0 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
f37e0 20 30 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e   0;.  if( pCur->
f37f0 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  idx>=pPage->nCel
f3800 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 50  l ){.    if( !pP
f3810 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
f3820 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
f3830 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79  ild(pCur, get4by
f3840 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
f3850 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
f3860 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 69 66  t+8]));.      if
f3870 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
f3880 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76  ;.      rc = mov
f3890 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72  eToLeftmost(pCur
f38a0 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  );.      *pRes =
f38b0 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   0;.      return
f38c0 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64   rc;.    }.    d
f38d0 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  o{.      if( sql
f38e0 69 74 65 33 42 74 72 65 65 49 73 52 6f 6f 74 50  ite3BtreeIsRootP
f38f0 61 67 65 28 70 50 61 67 65 29 20 29 7b 0a 20 20  age(pPage) ){.  
f3900 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b        *pRes = 1;
f3910 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65  .        pCur->e
f3920 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
f3930 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20  NVALID;.        
f3940 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f3950 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
f3960 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
f3970 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a  ToParent(pCur);.
f3980 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43        pPage = pC
f3990 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 20 20 7d  ur->pPage;.    }
f39a0 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 69 64 78  while( pCur->idx
f39b0 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  >=pPage->nCell )
f39c0 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b  ;.    *pRes = 0;
f39d0 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
f39e0 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20  leafData ){.    
f39f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
f3a00 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20 70 52  reeNext(pCur, pR
f3a10 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  es);.    }else{.
f3a20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
f3a30 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
f3a40 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
f3a50 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 66   *pRes = 0;.  if
f3a60 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b  ( pPage->leaf ){
f3a70 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f3a80 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20  TE_OK;.  }.  rc 
f3a90 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  = moveToLeftmost
f3aa0 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e  (pCur);.  return
f3ab0 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   rc;.}.SQLITE_PR
f3ac0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
f3ad0 33 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72  3BtreeNext(BtCur
f3ae0 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
f3af0 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pRes){.  int rc;
f3b00 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
f3b10 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
f3b20 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65  ) );.  rc = btre
f3b30 65 4e 65 78 74 28 70 43 75 72 2c 20 70 52 65 73  eNext(pCur, pRes
f3b40 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
f3b50 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74  }.../*.** Step t
f3b60 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
f3b70 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65   back to the pre
f3b80 76 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74  vious entry in t
f3b90 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66  he database.  If
f3ba0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74  .** successful t
f3bb0 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e  hen set *pRes=0.
f3bc0 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a    If the cursor.
f3bd0 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70  ** was already p
f3be0 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66  ointing to the f
f3bf0 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  irst entry in th
f3c00 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72  e database befor
f3c10 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  e.** this routin
f3c20 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68  e was called, th
f3c30 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a  en set *pRes=1..
f3c40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74  */.static int bt
f3c50 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75  reePrevious(BtCu
f3c60 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
f3c70 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63  *pRes){.  int rc
f3c80 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20  ;.  Pgno pgno;. 
f3c90 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
f3ca0 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
f3cb0 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
f3cc0 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73  r) );.  rc = res
f3cd0 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73 6f  toreOrClearCurso
f3ce0 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b  rPosition(pCur);
f3cf0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
f3d00 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
f3d10 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28  rn rc;.  }.  if(
f3d20 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d   CURSOR_INVALID=
f3d30 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b  =pCur->eState ){
f3d40 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a  .    *pRes = 1;.
f3d50 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
f3d60 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  E_OK;.  }.  if( 
f3d70 70 43 75 72 2d 3e 73 6b 69 70 3c 30 20 29 7b 0a  pCur->skip<0 ){.
f3d80 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d      pCur->skip =
f3d90 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   0;.    *pRes = 
f3da0 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
f3db0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70  LITE_OK;.  }.  p
f3dc0 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a  Cur->skip = 0;..
f3dd0 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
f3de0 70 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28  pPage;.  assert(
f3df0 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
f3e00 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
f3e10 2d 3e 69 64 78 3e 3d 30 20 29 3b 0a 20 20 69 66  ->idx>=0 );.  if
f3e20 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
f3e30 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74  {.    pgno = get
f3e40 34 62 79 74 65 28 20 66 69 6e 64 43 65 6c 6c 28  4byte( findCell(
f3e50 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78  pPage, pCur->idx
f3e60 29 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f  ) );.    rc = mo
f3e70 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
f3e80 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72  pgno);.    if( r
f3e90 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  c ){.      retur
f3ea0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
f3eb0 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74  rc = moveToRight
f3ec0 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 7d 65  most(pCur);.  }e
f3ed0 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20  lse{.    while( 
f3ee0 70 43 75 72 2d 3e 69 64 78 3d 3d 30 20 29 7b 0a  pCur->idx==0 ){.
f3ef0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
f3f00 33 42 74 72 65 65 49 73 52 6f 6f 74 50 61 67 65  3BtreeIsRootPage
f3f10 28 70 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20  (pPage) ){.     
f3f20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
f3f30 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
f3f40 3b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 20  ;.        *pRes 
f3f50 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74  = 1;.        ret
f3f60 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
f3f70 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
f3f80 69 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50  ite3BtreeMoveToP
f3f90 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20  arent(pCur);.   
f3fa0 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d     pPage = pCur-
f3fb0 3e 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20  >pPage;.    }.  
f3fc0 20 20 70 43 75 72 2d 3e 69 64 78 2d 2d 3b 0a 20    pCur->idx--;. 
f3fd0 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53     pCur->info.nS
f3fe0 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  ize = 0;.    if(
f3ff0 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61   pPage->leafData
f4000 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66   && !pPage->leaf
f4010 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
f4020 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69  qlite3BtreePrevi
f4030 6f 75 73 28 70 43 75 72 2c 20 70 52 65 73 29 3b  ous(pCur, pRes);
f4040 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
f4050 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
f4060 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70  ;.    }.  }.  *p
f4070 52 65 73 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  Res = 0;.  retur
f4080 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  n rc;.}.SQLITE_P
f4090 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f40a0 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
f40b0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
f40c0 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e  int *pRes){.  in
f40d0 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
f40e0 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
f40f0 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d  (pCur) );.  rc =
f4100 20 62 74 72 65 65 50 72 65 76 69 6f 75 73 28 70   btreePrevious(p
f4110 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 72 65  Cur, pRes);.  re
f4120 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
f4130 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
f4140 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64   page from the d
f4150 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
f4160 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65  .** The new page
f4170 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69   is marked as di
f4180 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20  rty.  (In other 
f4190 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61  words, sqlite3Pa
f41a0 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61  gerWrite().** ha
f41b0 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  s already been c
f41c0 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77  alled on the new
f41d0 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77   page.)  The new
f41e0 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a   page has also.*
f41f0 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65  * been reference
f4200 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e  d and the callin
f4210 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73  g routine is res
f4220 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c  ponsible for cal
f4230 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50  ling.** sqlite3P
f4240 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74  agerUnref() on t
f4250 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e  he new page when
f4260 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a   it is done..**.
f4270 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
f4280 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63  returned on succ
f4290 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20  ess.  Any other 
f42a0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64  return value ind
f42b0 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72  icates.** an err
f42c0 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e 64  or.  *ppPage and
f42d0 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 65   *pPgno are unde
f42e0 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76 65  fined in the eve
f42f0 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a  nt of an error..
f4300 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65  ** Do not invoke
f4310 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
f4320 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 20  ef() on *ppPage 
f4330 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72  if an error is r
f4340 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
f4350 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 70  f the "nearby" p
f4360 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20  arameter is not 
f4370 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62 6c  0, then a (feebl
f4380 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61 64  e) effort is mad
f4390 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 20  e to .** locate 
f43a0 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f 20  a page close to 
f43b0 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
f43c0 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73 20  "nearby".  This 
f43d0 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61  can be used in a
f43e0 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20  n.** attempt to 
f43f0 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70 61 67  keep related pag
f4400 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 68  es close to each
f4410 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64 61   other in the da
f4420 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20  tabase file,.** 
f4430 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63 61  which in turn ca
f4440 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20  n make database 
f4450 61 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a 2a  access faster..*
f4460 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78 61  *.** If the "exa
f4470 63 74 22 20 70 61 72 61 6d 65 74 65 72 20 69 73  ct" parameter is
f4480 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 20   not 0, and the 
f4490 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 72  page-number near
f44a0 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 6e  by exists .** an
f44b0 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72  ywhere on the fr
f44c0 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 74  ee-list, then it
f44d0 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 74   is guarenteed t
f44e0 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54  o be returned. T
f44f0 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75  his.** is only u
f4500 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63 75  sed by auto-vacu
f4510 75 6d 20 64 61 74 61 62 61 73 65 73 20 77 68 65  um databases whe
f4520 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e  n allocating a n
f4530 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ew table..*/.sta
f4540 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65  tic int allocate
f4550 42 74 72 65 65 50 61 67 65 28 0a 20 20 42 74 53  BtreePage(.  BtS
f4560 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 4d  hared *pBt, .  M
f4570 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c  emPage **ppPage,
f4580 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c   .  Pgno *pPgno,
f4590 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 2c   .  Pgno nearby,
f45a0 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a 20  .  u8 exact.){. 
f45b0 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31   MemPage *pPage1
f45c0 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  ;.  int rc;.  in
f45d0 74 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  t n;     /* Numb
f45e0 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74  er of pages on t
f45f0 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20  he freelist */. 
f4600 20 69 6e 74 20 6b 3b 20 20 20 20 20 2f 2a 20 4e   int k;     /* N
f4610 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 20  umber of leaves 
f4620 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 20  on the trunk of 
f4630 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a  the freelist */.
f4640 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e    MemPage *pTrun
f4650 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65  k = 0;.  MemPage
f4660 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30   *pPrevTrunk = 0
f4670 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
f4680 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
f4690 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
f46a0 20 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70   pPage1 = pBt->p
f46b0 50 61 67 65 31 3b 0a 20 20 6e 20 3d 20 67 65 74  Page1;.  n = get
f46c0 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
f46d0 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 69 66 28  Data[36]);.  if(
f46e0 20 6e 3e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54   n>0 ){.    /* T
f46f0 68 65 72 65 20 61 72 65 20 70 61 67 65 73 20 6f  here are pages o
f4700 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20  n the freelist. 
f4710 20 52 65 75 73 65 20 6f 6e 65 20 6f 66 20 74 68   Reuse one of th
f4720 6f 73 65 20 70 61 67 65 73 2e 20 2a 2f 0a 20 20  ose pages. */.  
f4730 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20    Pgno iTrunk;. 
f4740 20 20 20 75 38 20 73 65 61 72 63 68 4c 69 73 74     u8 searchList
f4750 20 3d 20 30 3b 20 2f 2a 20 49 66 20 74 68 65 20   = 0; /* If the 
f4760 66 72 65 65 2d 6c 69 73 74 20 6d 75 73 74 20 62  free-list must b
f4770 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 27  e searched for '
f4780 6e 65 61 72 62 79 27 20 2a 2f 0a 20 20 20 20 0a  nearby' */.    .
f4790 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 27 65      /* If the 'e
f47a0 78 61 63 74 27 20 70 61 72 61 6d 65 74 65 72 20  xact' parameter 
f47b0 77 61 73 20 74 72 75 65 20 61 6e 64 20 61 20 71  was true and a q
f47c0 75 65 72 79 20 6f 66 20 74 68 65 20 70 6f 69 6e  uery of the poin
f47d0 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 73  ter-map.    ** s
f47e0 68 6f 77 73 20 74 68 61 74 20 74 68 65 20 70 61  hows that the pa
f47f0 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 20 73  ge 'nearby' is s
f4800 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74 68 65 20  omewhere on the 
f4810 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 0a  free-list, then.
f4820 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72      ** the entir
f4830 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 20 73  e-list will be s
f4840 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 61 74  earched for that
f4850 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 23 69   page..    */.#i
f4860 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
f4870 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
f4880 20 69 66 28 20 65 78 61 63 74 20 26 26 20 6e 65   if( exact && ne
f4890 61 72 62 79 3c 3d 73 71 6c 69 74 65 33 50 61 67  arby<=sqlite3Pag
f48a0 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
f48b0 3e 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  >pPager) ){.    
f48c0 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20    u8 eType;.    
f48d0 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79    assert( nearby
f48e0 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  >0 );.      asse
f48f0 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  rt( pBt->autoVac
f4900 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20  uum );.      rc 
f4910 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c  = ptrmapGet(pBt,
f4920 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c   nearby, &eType,
f4930 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
f4940 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
f4950 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d       if( eType==
f4960 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
f4970 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63  ){.        searc
f4980 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20  hList = 1;.     
f4990 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20   }.      *pPgno 
f49a0 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a  = nearby;.    }.
f49b0 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44  #endif..    /* D
f49c0 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65  ecrement the fre
f49d0 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20  e-list count by 
f49e0 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f  1. Set iTrunk to
f49f0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
f4a00 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66  e.    ** first f
f4a10 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70  ree-list trunk p
f4a20 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20  age. iPrevTrunk 
f4a30 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a  is initially 1..
f4a40 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
f4a50 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
f4a60 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  e(pPage1->pDbPag
f4a70 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
f4a80 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
f4a90 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31  put4byte(&pPage1
f4aa0 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31  ->aData[36], n-1
f4ab0 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
f4ac0 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 20  ode within this 
f4ad0 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79  loop is run only
f4ae0 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 65   once if the 'se
f4af0 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 62  archList' variab
f4b00 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74  le.    ** is not
f4b10 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65   true. Otherwise
f4b20 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66  , it runs once f
f4b30 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61  or each trunk-pa
f4b40 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a  ge on the.    **
f4b50 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c   free-list until
f4b60 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62   the page 'nearb
f4b70 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20  y' is located.. 
f4b80 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20     */.    do {. 
f4b90 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20       pPrevTrunk 
f4ba0 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  = pTrunk;.      
f4bb0 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29  if( pPrevTrunk )
f4bc0 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b  {.        iTrunk
f4bd0 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 72   = get4byte(&pPr
f4be0 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  evTrunk->aData[0
f4bf0 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ]);.      }else{
f4c00 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20  .        iTrunk 
f4c10 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
f4c20 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a  e1->aData[32]);.
f4c30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
f4c40 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
f4c50 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75  etPage(pBt, iTru
f4c60 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b  nk, &pTrunk, 0);
f4c70 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
f4c80 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20  .        pTrunk 
f4c90 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  = 0;.        got
f4ca0 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
f4cb0 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  age;.      }..  
f4cc0 20 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65      k = get4byte
f4cd0 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
f4ce0 34 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b  4]);.      if( k
f4cf0 3d 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c 69  ==0 && !searchLi
f4d00 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  st ){.        /*
f4d10 20 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e   The trunk has n
f4d20 6f 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65  o leaves and the
f4d30 20 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69   list is not bei
f4d40 6e 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20  ng searched. .  
f4d50 20 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72        ** So extr
f4d60 61 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61  act the trunk pa
f4d70 67 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73  ge itself and us
f4d80 65 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c  e it as the newl
f4d90 79 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c  y .        ** al
f4da0 6c 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a  located page */.
f4db0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
f4dc0 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b  pPrevTrunk==0 );
f4dd0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
f4de0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
f4df0 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29  pTrunk->pDbPage)
f4e00 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
f4e10 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
f4e20 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
f4e30 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  page;.        }.
f4e40 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d          *pPgno =
f4e50 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20   iTrunk;.       
f4e60 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d   memcpy(&pPage1-
f4e70 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72  >aData[32], &pTr
f4e80 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34  unk->aData[0], 4
f4e90 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61  );.        *ppPa
f4ea0 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20  ge = pTrunk;.   
f4eb0 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b       pTrunk = 0;
f4ec0 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28  .        TRACE((
f4ed0 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72  "ALLOCATE: %d tr
f4ee0 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61  unk - %d free pa
f4ef0 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50  ges left\n", *pP
f4f00 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20  gno, n-1));.    
f4f10 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 70 42    }else if( k>pB
f4f20 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20  t->usableSize/4 
f4f30 2d 20 38 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  - 8 ){.        /
f4f40 2a 20 56 61 6c 75 65 20 6f 66 20 6b 20 69 73 20  * Value of k is 
f4f50 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 44  out of range.  D
f4f60 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
f4f70 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63  on */.        rc
f4f80 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
f4f90 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20  T_BKPT;.        
f4fa0 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
f4fb0 65 5f 70 61 67 65 3b 0a 23 69 66 6e 64 65 66 20  e_page;.#ifndef 
f4fc0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
f4fd0 56 41 43 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c  VACUUM.      }el
f4fe0 73 65 20 69 66 28 20 73 65 61 72 63 68 4c 69 73  se if( searchLis
f4ff0 74 20 26 26 20 6e 65 61 72 62 79 3d 3d 69 54 72  t && nearby==iTr
f5000 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  unk ){.        /
f5010 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 62 65  * The list is be
f5020 69 6e 67 20 73 65 61 72 63 68 65 64 20 61 6e 64  ing searched and
f5030 20 74 68 69 73 20 74 72 75 6e 6b 20 70 61 67 65   this trunk page
f5040 20 69 73 20 74 68 65 20 70 61 67 65 0a 20 20 20   is the page.   
f5050 20 20 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63       ** to alloc
f5060 61 74 65 2c 20 72 65 67 61 72 64 6c 65 73 73 20  ate, regardless 
f5070 6f 66 20 77 68 65 74 68 65 72 20 69 74 20 68 61  of whether it ha
f5080 73 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20  s leaves..      
f5090 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73    */.        ass
f50a0 65 72 74 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 72  ert( *pPgno==iTr
f50b0 75 6e 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 2a  unk );.        *
f50c0 70 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b  ppPage = pTrunk;
f50d0 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c  .        searchL
f50e0 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ist = 0;.       
f50f0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
f5100 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e  erWrite(pTrunk->
f5110 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
f5120 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
f5130 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
f5140 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
f5150 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
f5160 69 66 28 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20  if( k==0 ){.    
f5170 20 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76        if( !pPrev
f5180 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20  Trunk ){.       
f5190 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61       memcpy(&pPa
f51a0 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20  ge1->aData[32], 
f51b0 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  &pTrunk->aData[0
f51c0 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20  ], 4);.         
f51d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
f51e0 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 72 65      memcpy(&pPre
f51f0 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  vTrunk->aData[0]
f5200 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  , &pTrunk->aData
f5210 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20  [0], 4);.       
f5220 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
f5230 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
f5240 20 54 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20   The trunk page 
f5250 69 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74  is required by t
f5260 68 65 20 63 61 6c 6c 65 72 20 62 75 74 20 69 74  he caller but it
f5270 20 63 6f 6e 74 61 69 6e 73 20 0a 20 20 20 20 20   contains .     
f5280 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 73       ** pointers
f5290 20 74 6f 20 66 72 65 65 2d 6c 69 73 74 20 6c 65   to free-list le
f52a0 61 76 65 73 2e 20 54 68 65 20 66 69 72 73 74 20  aves. The first 
f52b0 6c 65 61 66 20 62 65 63 6f 6d 65 73 20 61 20 74  leaf becomes a t
f52c0 72 75 6e 6b 0a 20 20 20 20 20 20 20 20 20 20 2a  runk.          *
f52d0 2a 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63  * page in this c
f52e0 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ase..          *
f52f0 2f 0a 20 20 20 20 20 20 20 20 20 20 4d 65 6d 50  /.          MemP
f5300 61 67 65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a  age *pNewTrunk;.
f5310 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69            Pgno i
f5320 4e 65 77 54 72 75 6e 6b 20 3d 20 67 65 74 34 62  NewTrunk = get4b
f5330 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61  yte(&pTrunk->aDa
f5340 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 20  ta[8]);.        
f5350 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
f5360 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
f5370 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77  iNewTrunk, &pNew
f5380 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20  Trunk, 0);.     
f5390 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
f53a0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
f53b0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
f53c0 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
f53d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
f53e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
f53f0 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 54  PagerWrite(pNewT
f5400 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
f5410 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
f5420 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
f5430 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61             relea
f5440 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b  sePage(pNewTrunk
f5450 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  );.            g
f5460 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
f5470 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  _page;.         
f5480 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d   }.          mem
f5490 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e  cpy(&pNewTrunk->
f54a0 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e  aData[0], &pTrun
f54b0 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b  k->aData[0], 4);
f54c0 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62  .          put4b
f54d0 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e  yte(&pNewTrunk->
f54e0 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a  aData[4], k-1);.
f54f0 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
f5500 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61  (&pNewTrunk->aDa
f5510 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  ta[8], &pTrunk->
f5520 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29  aData[12], (k-1)
f5530 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  *4);.          r
f5540 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 54  eleasePage(pNewT
f5550 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20  runk);.         
f5560 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b   if( !pPrevTrunk
f5570 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
f5580 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31  put4byte(&pPage1
f5590 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65  ->aData[32], iNe
f55a0 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20  wTrunk);.       
f55b0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
f55c0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
f55d0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 72  e3PagerWrite(pPr
f55e0 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65  evTrunk->pDbPage
f55f0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
f5600 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
f5610 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
f5620 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
f5630 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
f5640 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74           put4byt
f5650 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61  e(&pPrevTrunk->a
f5660 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75  Data[0], iNewTru
f5670 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  nk);.          }
f5680 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
f5690 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20     pTrunk = 0;. 
f56a0 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41         TRACE(("A
f56b0 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e  LLOCATE: %d trun
f56c0 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65  k - %d free page
f56d0 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e  s left\n", *pPgn
f56e0 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66  o, n-1));.#endif
f56f0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
f5700 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74        /* Extract
f5710 20 61 20 6c 65 61 66 20 66 72 6f 6d 20 74 68 65   a leaf from the
f5720 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20   trunk */.      
f5730 20 20 69 6e 74 20 63 6c 6f 73 65 73 74 3b 0a 20    int closest;. 
f5740 20 20 20 20 20 20 20 50 67 6e 6f 20 69 50 61 67         Pgno iPag
f5750 65 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67  e;.        unsig
f5760 6e 65 64 20 63 68 61 72 20 2a 61 44 61 74 61 20  ned char *aData 
f5770 3d 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 3b  = pTrunk->aData;
f5780 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
f5790 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
f57a0 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29  pTrunk->pDbPage)
f57b0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
f57c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
f57d0 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
f57e0 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  page;.        }.
f57f0 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 61 72          if( near
f5800 62 79 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  by>0 ){.        
f5810 20 20 69 6e 74 20 69 2c 20 64 69 73 74 3b 0a 20    int i, dist;. 
f5820 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74           closest
f5830 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
f5840 64 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 28  dist = get4byte(
f5850 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 6e 65 61  &aData[8]) - nea
f5860 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20 69  rby;.          i
f5870 66 28 20 64 69 73 74 3c 30 20 29 20 64 69 73 74  f( dist<0 ) dist
f5880 20 3d 20 2d 64 69 73 74 3b 0a 20 20 20 20 20 20   = -dist;.      
f5890 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b      for(i=1; i<k
f58a0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
f58b0 20 20 20 20 69 6e 74 20 64 32 20 3d 20 67 65 74      int d2 = get
f58c0 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 2b 69  4byte(&aData[8+i
f58d0 2a 34 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20  *4]) - nearby;. 
f58e0 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64             if( d
f58f0 32 3c 30 20 29 20 64 32 20 3d 20 2d 64 32 3b 0a  2<0 ) d2 = -d2;.
f5900 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
f5910 64 32 3c 64 69 73 74 20 29 7b 0a 20 20 20 20 20  d2<dist ){.     
f5920 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74           closest
f5930 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20   = i;.          
f5940 20 20 20 20 64 69 73 74 20 3d 20 64 32 3b 0a 20      dist = d2;. 
f5950 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
f5960 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
f5970 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
f5980 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20    closest = 0;. 
f5990 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
f59a0 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62 79    iPage = get4by
f59b0 74 65 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73  te(&aData[8+clos
f59c0 65 73 74 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20  est*4]);.       
f59d0 20 69 66 28 20 21 73 65 61 72 63 68 4c 69 73 74   if( !searchList
f59e0 20 7c 7c 20 69 50 61 67 65 3d 3d 6e 65 61 72 62   || iPage==nearb
f59f0 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  y ){.          *
f5a00 70 50 67 6e 6f 20 3d 20 69 50 61 67 65 3b 0a 20  pPgno = iPage;. 
f5a10 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 50           if( *pP
f5a20 67 6e 6f 3e 73 71 6c 69 74 65 33 50 61 67 65 72  gno>sqlite3Pager
f5a30 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70  Pagecount(pBt->p
f5a40 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20  Pager) ){.      
f5a50 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70 61        /* Free pa
f5a60 67 65 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f  ge off the end o
f5a70 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  f the file */.  
f5a80 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
f5a90 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
f5aa0 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20  BKPT;.          
f5ab0 7d 0a 20 20 20 20 20 20 20 20 20 20 54 52 41 43  }.          TRAC
f5ac0 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64  E(("ALLOCATE: %d
f5ad0 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66 20   was leaf %d of 
f5ae0 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a  %d on trunk %d".
f5af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f5b00 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 65   ": %d more free
f5b10 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20   pages\n",.     
f5b20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 50 67              *pPg
f5b30 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b  no, closest+1, k
f5b40 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20  , pTrunk->pgno, 
f5b50 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20  n-1));.         
f5b60 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31   if( closest<k-1
f5b70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
f5b80 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38 2b  memcpy(&aData[8+
f5b90 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 61  closest*4], &aDa
f5ba0 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20  ta[4+k*4], 4);. 
f5bb0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
f5bc0 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 61       put4byte(&a
f5bd0 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20  Data[4], k-1);. 
f5be0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
f5bf0 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
f5c00 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70  e(pBt, *pPgno, p
f5c10 70 50 61 67 65 2c 20 31 29 3b 0a 20 20 20 20 20  pPage, 1);.     
f5c20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
f5c30 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
f5c40 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
f5c50 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 28  erDontRollback((
f5c60 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67  *ppPage)->pDbPag
f5c70 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
f5c80 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
f5c90 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 29  rWrite((*ppPage)
f5ca0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
f5cb0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
f5cc0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
f5cd0 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61             relea
f5ce0 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b  sePage(*ppPage);
f5cf0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
f5d00 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
f5d10 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20       searchList 
f5d20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
f5d30 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c       }.      rel
f5d40 65 61 73 65 50 61 67 65 28 70 50 72 65 76 54 72  easePage(pPrevTr
f5d50 75 6e 6b 29 3b 0a 20 20 20 20 20 20 70 50 72 65  unk);.      pPre
f5d60 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20  vTrunk = 0;.    
f5d70 7d 77 68 69 6c 65 28 20 73 65 61 72 63 68 4c 69  }while( searchLi
f5d80 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  st );.  }else{. 
f5d90 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20     /* There are 
f5da0 6e 6f 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20  no pages on the 
f5db0 66 72 65 65 6c 69 73 74 2c 20 73 6f 20 63 72 65  freelist, so cre
f5dc0 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 61  ate a new page a
f5dd0 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64  t the.    ** end
f5de0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a   of the file */.
f5df0 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 73 71 6c      *pPgno = sql
f5e00 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
f5e10 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 20  nt(pBt->pPager) 
f5e20 2b 20 31 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  + 1;..#ifndef SQ
f5e30 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
f5e40 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74  CUUM.    if( pBt
f5e50 2d 3e 6e 54 72 75 6e 63 20 29 7b 0a 20 20 20 20  ->nTrunc ){.    
f5e60 20 20 2f 2a 20 41 6e 20 69 6e 63 72 2d 76 61 63    /* An incr-vac
f5e70 75 75 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20  uum has already 
f5e80 72 75 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20  run within this 
f5e90 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 53 6f 20  transaction. So 
f5ea0 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 67  the.      ** pag
f5eb0 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 69 73  e to allocate is
f5ec0 20 6e 6f 74 20 66 72 6f 6d 20 74 68 65 20 70 68   not from the ph
f5ed0 79 73 69 63 61 6c 20 65 6e 64 20 6f 66 20 74 68  ysical end of th
f5ee0 65 20 66 69 6c 65 2c 20 62 75 74 0a 20 20 20 20  e file, but.    
f5ef0 20 20 2a 2a 20 61 74 20 70 42 74 2d 3e 6e 54 72    ** at pBt->nTr
f5f00 75 6e 63 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20  unc. .      */. 
f5f10 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 70 42       *pPgno = pB
f5f20 74 2d 3e 6e 54 72 75 6e 63 2b 31 3b 0a 20 20 20  t->nTrunc+1;.   
f5f30 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50     if( *pPgno==P
f5f40 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
f5f50 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  (pBt) ){.       
f5f60 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20   (*pPgno)++;.   
f5f70 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
f5f80 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
f5f90 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50  um && PTRMAP_ISP
f5fa0 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29  AGE(pBt, *pPgno)
f5fb0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
f5fc0 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f  *pPgno refers to
f5fd0 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70   a pointer-map p
f5fe0 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77  age, allocate tw
f5ff0 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20  o new pages.    
f6000 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20    ** at the end 
f6010 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74  of the file inst
f6020 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20  ead of one. The 
f6030 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20  first allocated 
f6040 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65  page.      ** be
f6050 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e  comes a new poin
f6060 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68  ter-map page, th
f6070 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64  e second is used
f6080 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   by the caller..
f6090 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54        */.      T
f60a0 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a  RACE(("ALLOCATE:
f60b0 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20   %d from end of 
f60c0 66 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d 6d 61  file (pointer-ma
f60d0 70 20 70 61 67 65 29 5c 6e 22 2c 20 2a 70 50 67  p page)\n", *pPg
f60e0 6e 6f 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65  no));.      asse
f60f0 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44  rt( *pPgno!=PEND
f6100 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
f6110 74 29 20 29 3b 0a 20 20 20 20 20 20 28 2a 70 50  t) );.      (*pP
f6120 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  gno)++;.    }.  
f6130 20 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 75 6e    if( pBt->nTrun
f6140 63 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  c ){.      pBt->
f6150 6e 54 72 75 6e 63 20 3d 20 2a 70 50 67 6e 6f 3b  nTrunc = *pPgno;
f6160 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
f6170 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e     assert( *pPgn
f6180 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  o!=PENDING_BYTE_
f6190 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20  PAGE(pBt) );.   
f61a0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
f61b0 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a  eeGetPage(pBt, *
f61c0 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 30  pPgno, ppPage, 0
f61d0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
f61e0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 72  return rc;.    r
f61f0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
f6200 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d  Write((*ppPage)-
f6210 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  >pDbPage);.    i
f6220 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
f6230 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73   ){.      releas
f6240 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a  ePage(*ppPage);.
f6250 20 20 20 20 7d 0a 20 20 20 20 54 52 41 43 45 28      }.    TRACE(
f6260 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66  ("ALLOCATE: %d f
f6270 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 5c  rom end of file\
f6280 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20  n", *pPgno));.  
f6290 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 2a 70 50  }..  assert( *pP
f62a0 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54  gno!=PENDING_BYT
f62b0 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 0a  E_PAGE(pBt) );..
f62c0 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
f62d0 65 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  e:.  releasePage
f62e0 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 6c 65  (pTrunk);.  rele
f62f0 61 73 65 50 61 67 65 28 70 50 72 65 76 54 72 75  asePage(pPrevTru
f6300 6e 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  nk);.  return rc
f6310 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  ;.}../*.** Add a
f6320 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74   page of the dat
f6330 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 74 68  abase file to th
f6340 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a  e freelist..**.*
f6350 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e  * sqlite3PagerUn
f6360 72 65 66 28 29 20 69 73 20 4e 4f 54 20 63 61 6c  ref() is NOT cal
f6370 6c 65 64 20 66 6f 72 20 70 50 61 67 65 2e 0a 2a  led for pPage..*
f6380 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65  /.static int fre
f6390 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  ePage(MemPage *p
f63a0 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61 72 65  Page){.  BtShare
f63b0 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
f63c0 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  pBt;.  MemPage *
f63d0 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50  pPage1 = pBt->pP
f63e0 61 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 2c 20  age1;.  int rc, 
f63f0 6e 2c 20 6b 3b 0a 0a 20 20 2f 2a 20 50 72 65 70  n, k;..  /* Prep
f6400 61 72 65 20 74 68 65 20 70 61 67 65 20 66 6f 72  are the page for
f6410 20 66 72 65 65 69 6e 67 20 2a 2f 0a 20 20 61 73   freeing */.  as
f6420 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
f6430 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
f6440 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
f6450 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
f6460 70 67 6e 6f 3e 31 20 29 3b 0a 20 20 70 50 61 67  pgno>1 );.  pPag
f6470 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20  e->isInit = 0;. 
f6480 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
f6490 67 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  ge->pParent);.  
f64a0 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 20 3d  pPage->pParent =
f64b0 20 30 3b 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d   0;..  /* Increm
f64c0 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 67  ent the free pag
f64d0 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65  e count on pPage
f64e0 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69  1 */.  rc = sqli
f64f0 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
f6500 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a  age1->pDbPage);.
f6510 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
f6520 6e 20 72 63 3b 0a 20 20 6e 20 3d 20 67 65 74 34  n rc;.  n = get4
f6530 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44  byte(&pPage1->aD
f6540 61 74 61 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34  ata[36]);.  put4
f6550 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44  byte(&pPage1->aD
f6560 61 74 61 5b 33 36 5d 2c 20 6e 2b 31 29 3b 0a 0a  ata[36], n+1);..
f6570 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45  #ifdef SQLITE_SE
f6580 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a  CURE_DELETE.  /*
f6590 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53   If the SQLITE_S
f65a0 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f 6d  ECURE_DELETE com
f65b0 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
f65c0 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65   is enabled, the
f65d0 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75  n.  ** always fu
f65e0 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 65  lly overwrite de
f65f0 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f  leted informatio
f6600 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20  n with zeros..  
f6610 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
f6620 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
f6630 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69  e->pDbPage);.  i
f6640 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
f6650 63 3b 0a 20 20 6d 65 6d 73 65 74 28 70 50 61 67  c;.  memset(pPag
f6660 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61  e->aData, 0, pPa
f6670 67 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a  ge->pBt->pageSiz
f6680 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  e);.#endif..#ifn
f6690 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
f66a0 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20  AUTOVACUUM.  /* 
f66b0 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
f66c0 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
f66d0 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e 20 65  cuum, write an e
f66e0 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e  ntry in the poin
f66f0 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20  ter-map.  ** to 
f6700 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68  indicate that th
f6710 65 20 70 61 67 65 20 69 73 20 66 72 65 65 2e 0a  e page is free..
f6720 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e    */.  if( pBt->
f6730 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
f6740 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
f6750 28 70 42 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e  (pBt, pPage->pgn
f6760 6f 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41  o, PTRMAP_FREEPA
f6770 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  GE, 0);.    if( 
f6780 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
f6790 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
f67a0 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  ( n==0 ){.    /*
f67b0 20 54 68 69 73 20 69 73 20 74 68 65 20 66 69 72   This is the fir
f67c0 73 74 20 66 72 65 65 20 70 61 67 65 20 2a 2f 0a  st free page */.
f67d0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
f67e0 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
f67f0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
f6800 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
f6810 72 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  rc;.    memset(p
f6820 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20  Page->aData, 0, 
f6830 38 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65  8);.    put4byte
f6840 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
f6850 33 32 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  32], pPage->pgno
f6860 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22 46  );.    TRACE(("F
f6870 52 45 45 2d 50 41 47 45 3a 20 25 64 20 66 69 72  REE-PAGE: %d fir
f6880 73 74 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67  st\n", pPage->pg
f6890 6e 6f 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  no));.  }else{. 
f68a0 20 20 20 2f 2a 20 4f 74 68 65 72 20 66 72 65 65     /* Other free
f68b0 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 65   pages already e
f68c0 78 69 73 74 2e 20 20 52 65 74 72 69 76 65 20 74  xist.  Retrive t
f68d0 68 65 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70  he first trunk p
f68e0 61 67 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  age.    ** of th
f68f0 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 66  e freelist and f
f6900 69 6e 64 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ind out how many
f6910 20 6c 65 61 76 65 73 20 69 74 20 68 61 73 2e 20   leaves it has. 
f6920 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  */.    MemPage *
f6930 70 54 72 75 6e 6b 3b 0a 20 20 20 20 72 63 20 3d  pTrunk;.    rc =
f6940 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
f6950 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79  Page(pBt, get4by
f6960 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  te(&pPage1->aDat
f6970 61 5b 33 32 5d 29 2c 20 26 70 54 72 75 6e 6b 2c  a[32]), &pTrunk,
f6980 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20   0);.    if( rc 
f6990 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
f69a0 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70   k = get4byte(&p
f69b0 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29  Trunk->aData[4])
f69c0 3b 0a 20 20 20 20 69 66 28 20 6b 3e 3d 70 42 74  ;.    if( k>=pBt
f69d0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d  ->usableSize/4 -
f69e0 20 38 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54   8 ){.      /* T
f69f0 68 65 20 74 72 75 6e 6b 20 69 73 20 66 75 6c 6c  he trunk is full
f6a00 2e 20 20 54 75 72 6e 20 74 68 65 20 70 61 67 65  .  Turn the page
f6a10 20 62 65 69 6e 67 20 66 72 65 65 64 20 69 6e 74   being freed int
f6a20 6f 20 61 20 6e 65 77 0a 20 20 20 20 20 20 2a 2a  o a new.      **
f6a30 20 74 72 75 6e 6b 20 70 61 67 65 20 77 69 74 68   trunk page with
f6a40 20 6e 6f 20 6c 65 61 76 65 73 2e 20 2a 2f 0a 20   no leaves. */. 
f6a50 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
f6a60 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
f6a70 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
f6a80 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
f6a90 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
f6aa0 70 75 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e  put4byte(pPage->
f6ab0 61 44 61 74 61 2c 20 70 54 72 75 6e 6b 2d 3e 70  aData, pTrunk->p
f6ac0 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 75  gno);.        pu
f6ad0 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
f6ae0 44 61 74 61 5b 34 5d 2c 20 30 29 3b 0a 20 20 20  Data[4], 0);.   
f6af0 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
f6b00 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
f6b10 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a  , pPage->pgno);.
f6b20 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22          TRACE(("
f6b30 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6e 65  FREE-PAGE: %d ne
f6b40 77 20 74 72 75 6e 6b 20 70 61 67 65 20 72 65 70  w trunk page rep
f6b50 6c 61 63 69 6e 67 20 25 64 5c 6e 22 2c 0a 20 20  lacing %d\n",.  
f6b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
f6b70 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 54 72 75 6e  age->pgno, pTrun
f6b80 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20  k->pgno));.     
f6b90 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
f6ba0 20 6b 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63   k<0 ){.      rc
f6bb0 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
f6bc0 54 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  T;.    }else{.  
f6bd0 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e      /* Add the n
f6be0 65 77 6c 79 20 66 72 65 65 64 20 70 61 67 65 20  ewly freed page 
f6bf0 61 73 20 61 20 6c 65 61 66 20 6f 6e 20 74 68 65  as a leaf on the
f6c00 20 63 75 72 72 65 6e 74 20 74 72 75 6e 6b 20 2a   current trunk *
f6c10 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  /.      rc = sql
f6c20 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
f6c30 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
f6c40 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
f6c50 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f6c60 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 54      put4byte(&pT
f6c70 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20  runk->aData[4], 
f6c80 6b 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75  k+1);.        pu
f6c90 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e  t4byte(&pTrunk->
f6ca0 61 44 61 74 61 5b 38 2b 6b 2a 34 5d 2c 20 70 50  aData[8+k*4], pP
f6cb0 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e  age->pgno);.#ifn
f6cc0 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52  def SQLITE_SECUR
f6cd0 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20  E_DELETE.       
f6ce0 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
f6cf0 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44  tWrite(pPage->pD
f6d00 62 50 61 67 65 29 3b 0a 23 65 6e 64 69 66 0a 20  bPage);.#endif. 
f6d10 20 20 20 20 20 7d 0a 20 20 20 20 20 20 54 52 41       }.      TRA
f6d20 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20  CE(("FREE-PAGE: 
f6d30 25 64 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b  %d leaf on trunk
f6d40 20 70 61 67 65 20 25 64 5c 6e 22 2c 70 50 61 67   page %d\n",pPag
f6d50 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e  e->pgno,pTrunk->
f6d60 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20  pgno));.    }.  
f6d70 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
f6d80 72 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20 72 65 74  runk);.  }.  ret
f6d90 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
f6da0 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c   Free any overfl
f6db0 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 61  ow pages associa
f6dc0 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76  ted with the giv
f6dd0 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74  en Cell..*/.stat
f6de0 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c  ic int clearCell
f6df0 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
f6e00 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
f6e10 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72  pCell){.  BtShar
f6e20 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d  ed *pBt = pPage-
f6e30 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f  >pBt;.  CellInfo
f6e40 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76   info;.  Pgno ov
f6e50 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63  flPgno;.  int rc
f6e60 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20  ;.  int nOvfl;. 
f6e70 20 69 6e 74 20 6f 76 66 6c 50 61 67 65 53 69 7a   int ovflPageSiz
f6e80 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
f6e90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
f6ea0 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
f6eb0 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
f6ec0 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  BtreeParseCellPt
f6ed0 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
f6ee0 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e  &info);.  if( in
f6ef0 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  fo.iOverflow==0 
f6f00 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
f6f10 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20  LITE_OK;  /* No 
f6f20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
f6f30 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64  Return without d
f6f40 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f  oing anything */
f6f50 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f 20  .  }.  ovflPgno 
f6f60 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c  = get4byte(&pCel
f6f70 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  l[info.iOverflow
f6f80 5d 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 53 69  ]);.  ovflPageSi
f6f90 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ze = pBt->usable
f6fa0 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 66  Size - 4;.  nOvf
f6fb0 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79 6c 6f  l = (info.nPaylo
f6fc0 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  ad - info.nLocal
f6fd0 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 20   + ovflPageSize 
f6fe0 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53 69 7a  - 1)/ovflPageSiz
f6ff0 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 76 66  e;.  assert( ovf
f7000 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 66  lPgno==0 || nOvf
f7010 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  l>0 );.  while( 
f7020 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 4d  nOvfl-- ){.    M
f7030 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 3b 0a 20  emPage *pOvfl;. 
f7040 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3d     if( ovflPgno=
f7050 3d 30 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 73  =0 || ovflPgno>s
f7060 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
f7070 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
f7080 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
f7090 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
f70a0 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20  _BKPT;.    }..  
f70b0 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c    rc = getOverfl
f70c0 6f 77 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c  owPage(pBt, ovfl
f70d0 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 28 6e  Pgno, &pOvfl, (n
f70e0 4f 76 66 6c 3d 3d 30 29 3f 30 3a 26 6f 76 66 6c  Ovfl==0)?0:&ovfl
f70f0 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72  Pgno);.    if( r
f7100 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
f7110 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65     rc = freePage
f7120 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20 73 71 6c  (pOvfl);.    sql
f7130 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
f7140 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Ovfl->pDbPage);.
f7150 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
f7160 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65  urn rc;.  }.  re
f7170 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
f7180 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
f7190 74 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63  the byte sequenc
f71a0 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  e used to repres
f71b0 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61  ent a cell on pa
f71c0 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20  ge pPage.** and 
f71d0 77 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20  write that byte 
f71e0 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43  sequence into pC
f71f0 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77  ell[].  Overflow
f7200 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c   pages are.** al
f7210 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c  located and fill
f7220 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61  ed in as necessa
f7230 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  ry.  The calling
f7240 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73   procedure.** is
f7250 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
f7260 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66   making sure suf
f7270 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61  ficient space ha
f7280 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  s been allocated
f7290 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e  .** for pCell[].
f72a0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
f72b0 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20   pCell does not 
f72c0 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74  necessary need t
f72d0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70  o point to the p
f72e0 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61  Page->aData.** a
f72f0 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68  rea.  pCell migh
f7300 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20  t point to some 
f7310 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67  temporary storag
f7320 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c  e.  The cell wil
f7330 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63  l.** be construc
f7340 74 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70  ted in this temp
f7350 6f 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20  orary area then 
f7360 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67  copied into pPag
f7370 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65  e->aData.** late
f7380 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
f7390 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d   fillInCell(.  M
f73a0 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20  emPage *pPage,  
f73b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f73c0 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63   The page that c
f73d0 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c  ontains the cell
f73e0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
f73f0 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20  har *pCell,     
f7400 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65       /* Complete
f7410 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c   text of the cel
f7420 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  l */.  const voi
f7430 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65  d *pKey, i64 nKe
f7440 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  y,    /* The key
f7450 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
f7460 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74   *pData,int nDat
f7470 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  a,   /* The data
f7480 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c   */.  int nZero,
f7490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f74a0 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65       /* Extra ze
f74b0 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65  ro bytes to appe
f74c0 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20  nd to pData */. 
f74d0 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20   int *pnSize    
f74e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f74f0 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69  /* Write cell si
f7500 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ze here */.){.  
f7510 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20  int nPayload;.  
f7520 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a  const u8 *pSrc;.
f7530 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72    int nSrc, n, r
f7540 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65  c;.  int spaceLe
f7550 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  ft;.  MemPage *p
f7560 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50  Ovfl = 0;.  MemP
f7570 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20  age *pToRelease 
f7580 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  = 0;.  unsigned 
f7590 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20  char *pPrior;.  
f75a0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
f75b0 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61  Payload;.  BtSha
f75c0 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
f75d0 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67  ->pBt;.  Pgno pg
f75e0 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e  noOvfl = 0;.  in
f75f0 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c  t nHeader;.  Cel
f7600 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61  lInfo info;..  a
f7610 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
f7620 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
f7630 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
f7640 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68  .  /* Fill in th
f7650 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 20 6e  e header. */.  n
f7660 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 69 66  Header = 0;.  if
f7670 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
f7680 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d  {.    nHeader +=
f7690 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50   4;.  }.  if( pP
f76a0 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a  age->hasData ){.
f76b0 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70      nHeader += p
f76c0 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b  utVarint(&pCell[
f76d0 6e 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 61 2b  nHeader], nData+
f76e0 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b  nZero);.  }else{
f76f0 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e 5a 65  .    nData = nZe
f7700 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 48  ro = 0;.  }.  nH
f7710 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69  eader += putVari
f7720 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65  nt(&pCell[nHeade
f7730 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b 65 79  r], *(u64*)&nKey
f7740 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
f7750 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
f7760 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
f7770 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e  o);.  assert( in
f7780 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61  fo.nHeader==nHea
f7790 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  der );.  assert(
f77a0 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79   info.nKey==nKey
f77b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e   );.  assert( in
f77c0 66 6f 2e 6e 44 61 74 61 3d 3d 6e 44 61 74 61 2b  fo.nData==nData+
f77d0 6e 5a 65 72 6f 20 29 3b 0a 20 20 0a 20 20 2f 2a  nZero );.  .  /*
f77e0 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 61 79   Fill in the pay
f77f0 6c 6f 61 64 20 2a 2f 0a 20 20 6e 50 61 79 6c 6f  load */.  nPaylo
f7800 61 64 20 3d 20 6e 44 61 74 61 20 2b 20 6e 5a 65  ad = nData + nZe
f7810 72 6f 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d  ro;.  if( pPage-
f7820 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 70  >intKey ){.    p
f7830 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20  Src = pData;.   
f7840 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a 20   nSrc = nData;. 
f7850 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20     nData = 0;.  
f7860 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 50 61 79 6c  }else{.    nPayl
f7870 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20  oad += nKey;.   
f7880 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20   pSrc = pKey;.  
f7890 20 20 6e 53 72 63 20 3d 20 6e 4b 65 79 3b 0a 20    nSrc = nKey;. 
f78a0 20 7d 0a 20 20 2a 70 6e 53 69 7a 65 20 3d 20 69   }.  *pnSize = i
f78b0 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 20 20 73 70 61  nfo.nSize;.  spa
f78c0 63 65 4c 65 66 74 20 3d 20 69 6e 66 6f 2e 6e 4c  ceLeft = info.nL
f78d0 6f 63 61 6c 3b 0a 20 20 70 50 61 79 6c 6f 61 64  ocal;.  pPayload
f78e0 20 3d 20 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65   = &pCell[nHeade
f78f0 72 5d 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20 26  r];.  pPrior = &
f7900 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72  pCell[info.iOver
f7910 66 6c 6f 77 5d 3b 0a 0a 20 20 77 68 69 6c 65 28  flow];..  while(
f7920 20 6e 50 61 79 6c 6f 61 64 3e 30 20 29 7b 0a 20   nPayload>0 ){. 
f7930 20 20 20 69 66 28 20 73 70 61 63 65 4c 65 66 74     if( spaceLeft
f7940 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ==0 ){.      int
f7950 20 69 73 45 78 61 63 74 20 3d 20 30 3b 0a 23 69   isExact = 0;.#i
f7960 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
f7970 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
f7980 20 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d     Pgno pgnoPtrm
f7990 61 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f  ap = pgnoOvfl; /
f79a0 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  * Overflow page 
f79b0 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
f79c0 79 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20  y page */.      
f79d0 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
f79e0 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64  uum ){.        d
f79f0 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e  o{.          pgn
f7a00 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20  oOvfl++;.       
f7a10 20 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20   } while( .     
f7a20 20 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41       PTRMAP_ISPA
f7a30 47 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c  GE(pBt, pgnoOvfl
f7a40 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50  ) || pgnoOvfl==P
f7a50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
f7a60 28 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29  (pBt) .        )
f7a70 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 67  ;.        if( pg
f7a80 6e 6f 4f 76 66 6c 3e 31 20 29 7b 0a 20 20 20 20  noOvfl>1 ){.    
f7a90 20 20 20 20 20 20 2f 2a 20 69 73 45 78 61 63 74        /* isExact
f7aa0 20 3d 20 31 3b 20 2a 2f 0a 20 20 20 20 20 20 20   = 1; */.       
f7ab0 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69   }.      }.#endi
f7ac0 66 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c  f.      rc = all
f7ad0 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
f7ae0 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e  Bt, &pOvfl, &pgn
f7af0 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c  oOvfl, pgnoOvfl,
f7b00 20 69 73 45 78 61 63 74 29 3b 0a 23 69 66 6e 64   isExact);.#ifnd
f7b10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
f7b20 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
f7b30 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
f7b40 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f  se supports auto
f7b50 2d 76 61 63 75 75 6d 2c 20 61 6e 64 20 74 68 65  -vacuum, and the
f7b60 20 73 65 63 6f 6e 64 20 6f 72 20 73 75 62 73 65   second or subse
f7b70 71 75 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f  quent.      ** o
f7b80 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20  verflow page is 
f7b90 62 65 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c  being allocated,
f7ba0 20 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 6f   add an entry to
f7bb0 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
f7bc0 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68  .      ** for th
f7bd0 61 74 20 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20  at page now. .  
f7be0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
f7bf0 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  If this is the f
f7c00 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  irst overflow pa
f7c10 67 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  ge, then write a
f7c20 20 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20 0a   partial entry .
f7c30 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20        ** to the 
f7c40 70 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20  pointer-map. If 
f7c50 77 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e 67  we write nothing
f7c60 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72   to this pointer
f7c70 2d 6d 61 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20  -map slot,.     
f7c80 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f 70 74   ** then the opt
f7c90 69 6d 69 73 74 69 63 20 6f 76 65 72 66 6c 6f 77  imistic overflow
f7ca0 20 63 68 61 69 6e 20 70 72 6f 63 65 73 73 69 6e   chain processin
f7cb0 67 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29  g in clearCell()
f7cc0 0a 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69  .      ** may mi
f7cd0 73 69 6e 74 65 72 70 72 65 74 20 74 68 65 20 75  sinterpret the u
f7ce0 6e 69 6e 69 74 69 61 6c 69 73 65 64 20 76 61 6c  ninitialised val
f7cf0 75 65 73 20 61 6e 64 20 64 65 6c 65 74 65 20 74  ues and delete t
f7d00 68 65 0a 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e  he.      ** wron
f7d10 67 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65  g pages from the
f7d20 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20   database..     
f7d30 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42   */.      if( pB
f7d40 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26  t->autoVacuum &&
f7d50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
f7d60 7b 0a 20 20 20 20 20 20 20 20 75 38 20 65 54 79  {.        u8 eTy
f7d70 70 65 20 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70  pe = (pgnoPtrmap
f7d80 3f 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  ?PTRMAP_OVERFLOW
f7d90 32 3a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  2:PTRMAP_OVERFLO
f7da0 57 31 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  W1);.        rc 
f7db0 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c  = ptrmapPut(pBt,
f7dc0 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79 70 65   pgnoOvfl, eType
f7dd0 2c 20 70 67 6e 6f 50 74 72 6d 61 70 29 3b 0a 20  , pgnoPtrmap);. 
f7de0 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
f7df0 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61  .          relea
f7e00 73 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20  sePage(pOvfl);. 
f7e10 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
f7e20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66  .#endif.      if
f7e30 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
f7e40 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52  releasePage(pToR
f7e50 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 20  elease);.       
f7e60 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
f7e70 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79    }.      put4by
f7e80 74 65 28 70 50 72 69 6f 72 2c 20 70 67 6e 6f 4f  te(pPrior, pgnoO
f7e90 76 66 6c 29 3b 0a 20 20 20 20 20 20 72 65 6c 65  vfl);.      rele
f7ea0 61 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61  asePage(pToRelea
f7eb0 73 65 29 3b 0a 20 20 20 20 20 20 70 54 6f 52 65  se);.      pToRe
f7ec0 6c 65 61 73 65 20 3d 20 70 4f 76 66 6c 3b 0a 20  lease = pOvfl;. 
f7ed0 20 20 20 20 20 70 50 72 69 6f 72 20 3d 20 70 4f       pPrior = pO
f7ee0 76 66 6c 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  vfl->aData;.    
f7ef0 20 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f    put4byte(pPrio
f7f00 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61  r, 0);.      pPa
f7f10 79 6c 6f 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e  yload = &pOvfl->
f7f20 61 44 61 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20  aData[4];.      
f7f30 73 70 61 63 65 4c 65 66 74 20 3d 20 70 42 74 2d  spaceLeft = pBt-
f7f40 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b  >usableSize - 4;
f7f50 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e  .    }.    n = n
f7f60 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 69 66 28  Payload;.    if(
f7f70 20 6e 3e 73 70 61 63 65 4c 65 66 74 20 29 20 6e   n>spaceLeft ) n
f7f80 20 3d 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 20   = spaceLeft;.  
f7f90 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a    if( nSrc>0 ){.
f7fa0 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63        if( n>nSrc
f7fb0 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20   ) n = nSrc;.   
f7fc0 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20     assert( pSrc 
f7fd0 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
f7fe0 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20  pPayload, pSrc, 
f7ff0 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  n);.    }else{. 
f8000 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79       memset(pPay
f8010 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20  load, 0, n);.   
f8020 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20   }.    nPayload 
f8030 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f  -= n;.    pPaylo
f8040 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72  ad += n;.    pSr
f8050 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63  c += n;.    nSrc
f8060 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65   -= n;.    space
f8070 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69  Left -= n;.    i
f8080 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20  f( nSrc==0 ){.  
f8090 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61      nSrc = nData
f80a0 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70  ;.      pSrc = p
f80b0 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Data;.    }.  }.
f80c0 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
f80d0 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74  oRelease);.  ret
f80e0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
f80f0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
f8100 68 65 20 4d 65 6d 50 61 67 65 2e 70 50 61 72 65  he MemPage.pPare
f8110 6e 74 20 70 6f 69 6e 74 65 72 20 6f 6e 20 74 68  nt pointer on th
f8120 65 20 70 61 67 65 20 77 68 6f 73 65 20 6e 75 6d  e page whose num
f8130 62 65 72 20 69 73 0a 2a 2a 20 67 69 76 65 6e 20  ber is.** given 
f8140 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  in the second ar
f8150 67 75 6d 65 6e 74 20 73 6f 20 74 68 61 74 20 4d  gument so that M
f8160 65 6d 50 61 67 65 2e 70 50 61 72 65 6e 74 20 68  emPage.pParent h
f8170 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 70 6f 69 6e  olds the.** poin
f8180 74 65 72 20 69 6e 20 74 68 65 20 74 68 69 72 64  ter in the third
f8190 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
f81a0 61 74 69 63 20 69 6e 74 20 72 65 70 61 72 65 6e  atic int reparen
f81b0 74 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a  tPage(BtShared *
f81c0 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  pBt, Pgno pgno, 
f81d0 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 72  MemPage *pNewPar
f81e0 65 6e 74 2c 20 69 6e 74 20 69 64 78 29 7b 0a 20  ent, int idx){. 
f81f0 20 4d 65 6d 50 61 67 65 20 2a 70 54 68 69 73 3b   MemPage *pThis;
f8200 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61  .  DbPage *pDbPa
f8210 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ge;..  assert( s
f8220 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
f8230 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
f8240 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 50  .  assert( pNewP
f8250 61 72 65 6e 74 21 3d 30 20 29 3b 0a 20 20 69 66  arent!=0 );.  if
f8260 28 20 70 67 6e 6f 3d 3d 30 20 29 20 72 65 74 75  ( pgno==0 ) retu
f8270 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
f8280 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61  assert( pBt->pPa
f8290 67 65 72 21 3d 30 20 29 3b 0a 20 20 70 44 62 50  ger!=0 );.  pDbP
f82a0 61 67 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  age = sqlite3Pag
f82b0 65 72 4c 6f 6f 6b 75 70 28 70 42 74 2d 3e 70 50  erLookup(pBt->pP
f82c0 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69  ager, pgno);.  i
f82d0 66 28 20 70 44 62 50 61 67 65 20 29 7b 0a 20 20  f( pDbPage ){.  
f82e0 20 20 70 54 68 69 73 20 3d 20 28 4d 65 6d 50 61    pThis = (MemPa
f82f0 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ge *)sqlite3Page
f8300 72 47 65 74 45 78 74 72 61 28 70 44 62 50 61 67  rGetExtra(pDbPag
f8310 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 68 69  e);.    if( pThi
f8320 73 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  s->isInit ){.   
f8330 20 20 20 61 73 73 65 72 74 28 20 70 54 68 69 73     assert( pThis
f8340 2d 3e 61 44 61 74 61 3d 3d 73 71 6c 69 74 65 33  ->aData==sqlite3
f8350 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62  PagerGetData(pDb
f8360 50 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 69  Page) );.      i
f8370 66 28 20 70 54 68 69 73 2d 3e 70 50 61 72 65 6e  f( pThis->pParen
f8380 74 21 3d 70 4e 65 77 50 61 72 65 6e 74 20 29 7b  t!=pNewParent ){
f8390 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 68  .        if( pTh
f83a0 69 73 2d 3e 70 50 61 72 65 6e 74 20 29 20 73 71  is->pParent ) sq
f83b0 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
f83c0 70 54 68 69 73 2d 3e 70 50 61 72 65 6e 74 2d 3e  pThis->pParent->
f83d0 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
f83e0 20 20 70 54 68 69 73 2d 3e 70 50 61 72 65 6e 74    pThis->pParent
f83f0 20 3d 20 70 4e 65 77 50 61 72 65 6e 74 3b 0a 20   = pNewParent;. 
f8400 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
f8410 67 65 72 52 65 66 28 70 4e 65 77 50 61 72 65 6e  gerRef(pNewParen
f8420 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  t->pDbPage);.   
f8430 20 20 20 7d 0a 20 20 20 20 20 20 70 54 68 69 73     }.      pThis
f8440 2d 3e 69 64 78 50 61 72 65 6e 74 20 3d 20 69 64  ->idxParent = id
f8450 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  x;.    }.    sql
f8460 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
f8470 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 23 69  DbPage);.  }..#i
f8480 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
f8490 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69  T_AUTOVACUUM.  i
f84a0 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
f84b0 75 6d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  um ){.    return
f84c0 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
f84d0 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52  pgno, PTRMAP_BTR
f84e0 45 45 2c 20 70 4e 65 77 50 61 72 65 6e 74 2d 3e  EE, pNewParent->
f84f0 70 67 6e 6f 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  pgno);.  }.#endi
f8500 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  f.  return SQLIT
f8510 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a  E_OK;.}..../*.**
f8520 20 43 68 61 6e 67 65 20 74 68 65 20 70 50 61 72   Change the pPar
f8530 65 6e 74 20 70 6f 69 6e 74 65 72 20 6f 66 20 61  ent pointer of a
f8540 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70  ll children of p
f8550 50 61 67 65 20 74 6f 20 70 6f 69 6e 74 20 62 61  Page to point ba
f8560 63 6b 0a 2a 2a 20 74 6f 20 70 50 61 67 65 2e 0a  ck.** to pPage..
f8570 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
f8580 6f 72 64 73 2c 20 66 6f 72 20 65 76 65 72 79 20  ords, for every 
f8590 63 68 69 6c 64 20 6f 66 20 70 50 61 67 65 2c 20  child of pPage, 
f85a0 69 6e 76 6f 6b 65 20 72 65 70 61 72 65 6e 74 50  invoke reparentP
f85b0 61 67 65 28 29 0a 2a 2a 20 74 6f 20 6d 61 6b 65  age().** to make
f85c0 20 73 75 72 65 20 74 68 61 74 20 65 61 63 68 20   sure that each 
f85d0 63 68 69 6c 64 20 6b 6e 6f 77 73 20 74 68 61 74  child knows that
f85e0 20 70 50 61 67 65 20 69 73 20 69 74 73 20 70 61   pPage is its pa
f85f0 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rent..**.** This
f8600 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63 61   routine gets ca
f8610 6c 6c 65 64 20 61 66 74 65 72 20 79 6f 75 20 6d  lled after you m
f8620 65 6d 63 70 79 28 29 20 6f 6e 65 20 70 61 67 65  emcpy() one page
f8630 20 69 6e 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72   into.** another
f8640 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f8650 72 65 70 61 72 65 6e 74 43 68 69 6c 64 50 61 67  reparentChildPag
f8660 65 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  es(MemPage *pPag
f8670 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42  e){.  int i;.  B
f8680 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
f8690 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74  Page->pBt;.  int
f86a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
f86b0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
f86c0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
f86d0 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
f86e0 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ) );.  if( pPage
f86f0 2d 3e 6c 65 61 66 20 29 20 72 65 74 75 72 6e 20  ->leaf ) return 
f8700 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 66 6f  SQLITE_OK;..  fo
f8710 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e  r(i=0; i<pPage->
f8720 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
f8730 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e   u8 *pCell = fin
f8740 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b  dCell(pPage, i);
f8750 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
f8760 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72  >leaf ){.      r
f8770 63 20 3d 20 72 65 70 61 72 65 6e 74 50 61 67 65  c = reparentPage
f8780 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70  (pBt, get4byte(p
f8790 43 65 6c 6c 29 2c 20 70 50 61 67 65 2c 20 69 29  Cell), pPage, i)
f87a0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
f87b0 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
f87c0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
f87d0 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
f87e0 65 61 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  eaf ){.    rc = 
f87f0 72 65 70 61 72 65 6e 74 50 61 67 65 28 70 42 74  reparentPage(pBt
f8800 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  , get4byte(&pPag
f8810 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
f8820 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 2c 20 0a  hdrOffset+8]), .
f8830 20 20 20 20 20 20 20 70 50 61 67 65 2c 20 69 29         pPage, i)
f8840 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 64 78  ;.    pPage->idx
f8850 53 68 69 66 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  Shift = 0;.  }. 
f8860 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
f8870 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20  *.** Remove the 
f8880 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d 20 70  i-th cell from p
f8890 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75 74  Page.  This rout
f88a0 69 6e 65 20 65 66 66 65 63 74 73 20 70 50 61 67  ine effects pPag
f88b0 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65 20 63  e only..** The c
f88c0 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e  ell content is n
f88d0 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65 61 6c  ot freed or deal
f88e0 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69 73 20  located.  It is 
f88f0 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20  assumed that.** 
f8900 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
f8910 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69 65 64   has been copied
f8920 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73 65 2e   someplace else.
f8930 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a    This routine j
f8940 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73 20 74  ust.** removes t
f8950 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  he reference to 
f8960 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50  the cell from pP
f8970 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a 22 20  age..**.** "sz" 
f8980 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75 6d 62  must be the numb
f8990 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
f89a0 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74  he cell..*/.stat
f89b0 69 63 20 76 6f 69 64 20 64 72 6f 70 43 65 6c 6c  ic void dropCell
f89c0 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
f89d0 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 73 7a   int idx, int sz
f89e0 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
f89f0 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
f8a00 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63  nter */.  int pc
f8a10 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66  ;         /* Off
f8a20 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74  set to cell cont
f8a30 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e  ent of cell bein
f8a40 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75  g deleted */.  u
f8a50 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f  8 *data;       /
f8a60 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a  * pPage->aData *
f8a70 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20  /.  u8 *ptr;    
f8a80 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d      /* Used to m
f8a90 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64  ove bytes around
f8aa0 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a   within data[] *
f8ab0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 78  /..  assert( idx
f8ac0 3e 3d 30 20 26 26 20 69 64 78 3c 70 50 61 67 65  >=0 && idx<pPage
f8ad0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73  ->nCell );.  ass
f8ae0 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a  ert( sz==cellSiz
f8af0 65 28 70 50 61 67 65 2c 20 69 64 78 29 20 29 3b  e(pPage, idx) );
f8b00 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
f8b10 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
f8b20 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
f8b30 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
f8b40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
f8b50 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
f8b60 75 74 65 78 29 20 29 3b 0a 20 20 64 61 74 61 20  utex) );.  data 
f8b70 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
f8b80 20 20 70 74 72 20 3d 20 26 64 61 74 61 5b 70 50    ptr = &data[pP
f8b90 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
f8ba0 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d  + 2*idx];.  pc =
f8bb0 20 67 65 74 32 62 79 74 65 28 70 74 72 29 3b 0a   get2byte(ptr);.
f8bc0 20 20 61 73 73 65 72 74 28 20 70 63 3e 31 30 20    assert( pc>10 
f8bd0 26 26 20 70 63 2b 73 7a 3c 3d 70 50 61 67 65 2d  && pc+sz<=pPage-
f8be0 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
f8bf0 20 29 3b 0a 20 20 66 72 65 65 53 70 61 63 65 28   );.  freeSpace(
f8c00 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a  pPage, pc, sz);.
f8c10 20 20 66 6f 72 28 69 3d 69 64 78 2b 31 3b 20 69    for(i=idx+1; i
f8c20 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69  <pPage->nCell; i
f8c30 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a 20 20 20  ++, ptr+=2){.   
f8c40 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 32 5d   ptr[0] = ptr[2]
f8c50 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70  ;.    ptr[1] = p
f8c60 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20 70 50 61  tr[3];.  }.  pPa
f8c70 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70  ge->nCell--;.  p
f8c80 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 50  ut2byte(&data[pP
f8c90 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33  age->hdrOffset+3
f8ca0 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29  ], pPage->nCell)
f8cb0 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  ;.  pPage->nFree
f8cc0 20 2b 3d 20 32 3b 0a 20 20 70 50 61 67 65 2d 3e   += 2;.  pPage->
f8cd0 69 64 78 53 68 69 66 74 20 3d 20 31 3b 0a 7d 0a  idxShift = 1;.}.
f8ce0 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
f8cf0 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67  new cell on pPag
f8d00 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20  e at cell index 
f8d10 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e  "i".  pCell poin
f8d20 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e  ts to the.** con
f8d30 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  tent of the cell
f8d40 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  ..**.** If the c
f8d50 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c  ell content will
f8d60 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65   fit on the page
f8d70 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74 68  , then put it th
f8d80 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77  ere.  If it.** w
f8d90 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65  ill not fit, the
f8da0 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  n make a copy of
f8db0 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
f8dc0 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a  t into pTemp if.
f8dd0 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20  ** pTemp is not 
f8de0 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73  null.  Regardles
f8df0 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f  s of pTemp, allo
f8e00 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79  cate a new entry
f8e10 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 4f  .** in pPage->aO
f8e20 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69  vfl[] and make i
f8e30 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63  t point to the c
f8e40 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74  ell content (eit
f8e50 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20  her.** in pTemp 
f8e60 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  or the original 
f8e70 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20  pCell) and also 
f8e80 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78  record its index
f8e90 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67  . .** Allocating
f8ea0 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20   a new entry in 
f8eb0 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69  pPage->aCell[] i
f8ec0 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20  mplies that .** 
f8ed0 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
f8ee0 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e   is incremented.
f8ef0 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 20  .**.** If nSkip 
f8f00 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
f8f10 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68  n do not copy th
f8f20 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79  e first nSkip by
f8f30 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 65  tes of the.** ce
f8f40 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77  ll. The caller w
f8f50 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68  ill overwrite th
f8f60 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 66 75  em after this fu
f8f70 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20  nction returns. 
f8f80 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 6e  If.** nSkip is n
f8f90 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 43  on-zero, then pC
f8fa0 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 6e  ell may not poin
f8fb0 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20  t to an invalid 
f8fc0 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  memory location 
f8fd0 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e  .** (but pCell+n
f8fe0 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 20 76  Skip is always v
f8ff0 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  alid)..*/.static
f9000 20 69 6e 74 20 69 6e 73 65 72 74 43 65 6c 6c 28   int insertCell(
f9010 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
f9020 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 6e 74  e,   /* Page int
f9030 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 63  o which we are c
f9040 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  opying */.  int 
f9050 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  i,            /*
f9060 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f 6d 65   New cell become
f9070 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20  s the i-th cell 
f9080 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20  of the page */. 
f9090 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20   u8 *pCell,     
f90a0 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
f90b0 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 2a 2f   the new cell */
f90c0 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20 20 20  .  int sz,      
f90d0 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66       /* Bytes of
f90e0 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43 65 6c   content in pCel
f90f0 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65 6d 70  l */.  u8 *pTemp
f9100 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70  ,        /* Temp
f9110 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 66   storage space f
f9120 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e 65 65  or pCell, if nee
f9130 64 65 64 20 2a 2f 0a 20 20 75 38 20 6e 53 6b 69  ded */.  u8 nSki
f9140 70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  p          /* Do
f9150 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20 66   not write the f
f9160 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74 65 73  irst nSkip bytes
f9170 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a   of the cell */.
f9180 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20  ){.  int idx;   
f9190 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
f91a0 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63 65 6c  to write new cel
f91b0 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74  l content in dat
f91c0 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20  a[] */.  int j; 
f91d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
f91e0 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
f91f0 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 20  int top;        
f9200 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
f9210 6f 66 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61  of content for a
f9220 6e 79 20 63 65 6c 6c 20 69 6e 20 64 61 74 61 5b  ny cell in data[
f9230 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20  ] */.  int end; 
f9240 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
f9250 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20  t byte past the 
f9260 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  last cell pointe
f9270 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20  r in data[] */. 
f9280 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20   int ins;       
f9290 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64     /* Index in d
f92a0 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20  ata[] where new 
f92b0 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20  cell pointer is 
f92c0 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e  inserted */.  in
f92d0 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20  t hdr;          
f92e0 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 64  /* Offset into d
f92f0 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 70 61 67  ata[] of the pag
f9300 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e  e header */.  in
f9310 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20  t cellOffset;   
f9320 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 66 69  /* Address of fi
f9330 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  rst cell pointer
f9340 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20   in data[] */.  
f9350 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20  u8 *data;       
f9360 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74    /* The content
f9370 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61   of the whole pa
f9380 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b  ge */.  u8 *ptr;
f9390 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
f93a0 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66  d for moving inf
f93b0 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20  ormation around 
f93c0 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20  in data[] */..  
f93d0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
f93e0 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b  i<=pPage->nCell+
f93f0 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
f9400 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a   );.  assert( sz
f9410 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  ==cellSizePtr(pP
f9420 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20  age, pCell) );. 
f9430 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
f9440 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
f9450 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
f9460 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e  ;.  if( pPage->n
f9470 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32  Overflow || sz+2
f9480 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b  >pPage->nFree ){
f9490 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 29  .    if( pTemp )
f94a0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  {.      memcpy(p
f94b0 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c  Temp+nSkip, pCel
f94c0 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69  l+nSkip, sz-nSki
f94d0 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20  p);.      pCell 
f94e0 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20  = pTemp;.    }. 
f94f0 20 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e 6e 4f     j = pPage->nO
f9500 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 61  verflow++;.    a
f9510 73 73 65 72 74 28 20 6a 3c 73 69 7a 65 6f 66 28  ssert( j<sizeof(
f9520 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69  pPage->aOvfl)/si
f9530 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66  zeof(pPage->aOvf
f9540 6c 5b 30 5d 29 20 29 3b 0a 20 20 20 20 70 50 61  l[0]) );.    pPa
f9550 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65  ge->aOvfl[j].pCe
f9560 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20  ll = pCell;.    
f9570 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e  pPage->aOvfl[j].
f9580 69 64 78 20 3d 20 69 3b 0a 20 20 20 20 70 50 61  idx = i;.    pPa
f9590 67 65 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20  ge->nFree = 0;. 
f95a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
f95b0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
f95c0 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44  rWrite(pPage->pD
f95d0 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
f95e0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
f95f0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
f9600 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
f9610 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
f9620 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
f9630 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
f9640 20 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d     data = pPage-
f9650 3e 61 44 61 74 61 3b 0a 20 20 20 20 68 64 72 20  >aData;.    hdr 
f9660 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
f9670 65 74 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65  et;.    top = ge
f9680 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
f9690 2b 35 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 4f 66  +5]);.    cellOf
f96a0 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65  fset = pPage->ce
f96b0 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 65 6e  llOffset;.    en
f96c0 64 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b  d = cellOffset +
f96d0 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20   2*pPage->nCell 
f96e0 2b 20 32 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63  + 2;.    ins = c
f96f0 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b  ellOffset + 2*i;
f9700 0a 20 20 20 20 69 66 28 20 65 6e 64 20 3e 20 74  .    if( end > t
f9710 6f 70 20 2d 20 73 7a 20 29 7b 0a 20 20 20 20 20  op - sz ){.     
f9720 20 72 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74   rc = defragment
f9730 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
f9740 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
f9750 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
f9760 3b 0a 20 20 20 20 20 20 74 6f 70 20 3d 20 67 65  ;.      top = ge
f9770 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
f9780 2b 35 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65  +5]);.      asse
f9790 72 74 28 20 65 6e 64 20 2b 20 73 7a 20 3c 3d 20  rt( end + sz <= 
f97a0 74 6f 70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  top );.    }.   
f97b0 20 69 64 78 20 3d 20 61 6c 6c 6f 63 61 74 65 53   idx = allocateS
f97c0 70 61 63 65 28 70 50 61 67 65 2c 20 73 7a 29 3b  pace(pPage, sz);
f97d0 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78  .    assert( idx
f97e0 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
f97f0 28 20 65 6e 64 20 3c 3d 20 67 65 74 32 62 79 74  ( end <= get2byt
f9800 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20  e(&data[hdr+5]) 
f9810 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43  );.    pPage->nC
f9820 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65  ell++;.    pPage
f9830 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 3b 0a 20 20  ->nFree -= 2;.  
f9840 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 69    memcpy(&data[i
f9850 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65 6c 6c  dx+nSkip], pCell
f9860 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70  +nSkip, sz-nSkip
f9870 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e 64  );.    for(j=end
f9880 2d 32 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d  -2, ptr=&data[j]
f9890 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70  ; j>ins; j-=2, p
f98a0 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 74  tr-=2){.      pt
f98b0 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a  r[0] = ptr[-2];.
f98c0 20 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70        ptr[1] = p
f98d0 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20  tr[-1];.    }.  
f98e0 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
f98f0 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20  [ins], idx);.   
f9900 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
f9910 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e  hdr+3], pPage->n
f9920 43 65 6c 6c 29 3b 0a 20 20 20 20 70 50 61 67 65  Cell);.    pPage
f9930 2d 3e 69 64 78 53 68 69 66 74 20 3d 20 31 3b 0a  ->idxShift = 1;.
f9940 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
f9950 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
f9960 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42     if( pPage->pB
f9970 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
f9980 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65  .      /* The ce
f9990 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61  ll may contain a
f99a0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f   pointer to an o
f99b0 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66  verflow page. If
f99c0 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20   so, write.     
f99d0 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f   ** the entry fo
f99e0 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  r the overflow p
f99f0 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69  age into the poi
f9a00 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20  nter map..      
f9a10 2a 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66  */.      CellInf
f9a20 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 73 71  o info;.      sq
f9a30 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
f9a40 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
f9a50 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20  ell, &info);.   
f9a60 20 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f     assert( (info
f9a70 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69  .nData+(pPage->i
f9a80 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65  ntKey?0:info.nKe
f9a90 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f  y))==info.nPaylo
f9aa0 61 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ad );.      if( 
f9ab0 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61  (info.nData+(pPa
f9ac0 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66  ge->intKey?0:inf
f9ad0 6f 2e 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c  o.nKey))>info.nL
f9ae0 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  ocal ){.        
f9af0 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20  Pgno pgnoOvfl = 
f9b00 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b  get4byte(&pCell[
f9b10 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29  info.iOverflow])
f9b20 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  ;.        rc = p
f9b30 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d 3e  trmapPut(pPage->
f9b40 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50  pBt, pgnoOvfl, P
f9b50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c  TRMAP_OVERFLOW1,
f9b60 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20   pPage->pgno);. 
f9b70 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
f9b80 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
f9b90 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
f9ba0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a    }.#endif.  }..
f9bb0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f9bc0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  OK;.}../*.** Add
f9bd0 20 61 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73   a list of cells
f9be0 20 74 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65   to a page.  The
f9bf0 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20   page should be 
f9c00 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e  initially empty.
f9c10 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72  .** The cells ar
f9c20 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  e guaranteed to 
f9c30 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e  fit on the page.
f9c40 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
f9c50 61 73 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20  assemblePage(.  
f9c60 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
f9c70 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f    /* The page to
f9c80 20 62 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a   be assemblied *
f9c90 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20  /.  int nCell,  
f9ca0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d        /* The num
f9cb0 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20  ber of cells to 
f9cc0 61 64 64 20 74 6f 20 74 68 69 73 20 70 61 67 65  add to this page
f9cd0 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c   */.  u8 **apCel
f9ce0 6c 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  l,      /* Point
f9cf0 65 72 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69  ers to cell bodi
f9d00 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 53 69  es */.  int *aSi
f9d10 7a 65 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a  ze        /* Siz
f9d20 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20  es of the cells 
f9d30 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  */.){.  int i;  
f9d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
f9d50 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
f9d60 6e 74 20 74 6f 74 61 6c 53 69 7a 65 3b 20 20 20  nt totalSize;   
f9d70 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f   /* Total size o
f9d80 66 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 20  f all cells */. 
f9d90 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20   int hdr;       
f9da0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 70     /* Index of p
f9db0 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  age header */.  
f9dc0 69 6e 74 20 63 65 6c 6c 70 74 72 3b 20 20 20 20  int cellptr;    
f9dd0 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
f9de0 6e 65 78 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  next cell pointe
f9df0 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62  r */.  int cellb
f9e00 6f 64 79 3b 20 20 20 20 20 2f 2a 20 41 64 64 72  ody;     /* Addr
f9e10 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c  ess of next cell
f9e20 20 62 6f 64 79 20 2a 2f 0a 20 20 75 38 20 2a 64   body */.  u8 *d
f9e30 61 74 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ata;         /* 
f9e40 44 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67  Data for the pag
f9e50 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
f9e60 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
f9e70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
f9e80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
f9e90 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
f9ea0 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 6f 74 61  mutex) );.  tota
f9eb0 6c 53 69 7a 65 20 3d 20 30 3b 0a 20 20 66 6f 72  lSize = 0;.  for
f9ec0 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69  (i=0; i<nCell; i
f9ed0 2b 2b 29 7b 0a 20 20 20 20 74 6f 74 61 6c 53 69  ++){.    totalSi
f9ee0 7a 65 20 2b 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a  ze += aSize[i];.
f9ef0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 74 6f    }.  assert( to
f9f00 74 61 6c 53 69 7a 65 2b 32 2a 6e 43 65 6c 6c 3c  talSize+2*nCell<
f9f10 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 3b  =pPage->nFree );
f9f20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
f9f30 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20  ->nCell==0 );.  
f9f40 63 65 6c 6c 70 74 72 20 3d 20 70 50 61 67 65 2d  cellptr = pPage-
f9f50 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 64  >cellOffset;.  d
f9f60 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
f9f70 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  ta;.  hdr = pPag
f9f80 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
f9f90 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  put2byte(&data[h
f9fa0 64 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20  dr+3], nCell);. 
f9fb0 20 69 66 28 20 6e 43 65 6c 6c 20 29 7b 0a 20 20   if( nCell ){.  
f9fc0 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 61 6c 6c    cellbody = all
f9fd0 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65  ocateSpace(pPage
f9fe0 2c 20 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20  , totalSize);.  
f9ff0 20 20 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f    assert( cellbo
fa000 64 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  dy>0 );.    asse
fa010 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  rt( pPage->nFree
fa020 20 3e 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20   >= 2*nCell );. 
fa030 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20     pPage->nFree 
fa040 2d 3d 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 20 20  -= 2*nCell;.    
fa050 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c  for(i=0; i<nCell
fa060 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 75  ; i++){.      pu
fa070 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c  t2byte(&data[cel
fa080 6c 70 74 72 5d 2c 20 63 65 6c 6c 62 6f 64 79 29  lptr], cellbody)
fa090 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ;.      memcpy(&
fa0a0 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20  data[cellbody], 
fa0b0 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65  apCell[i], aSize
fa0c0 5b 69 5d 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c  [i]);.      cell
fa0d0 70 74 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20  ptr += 2;.      
fa0e0 63 65 6c 6c 62 6f 64 79 20 2b 3d 20 61 53 69 7a  cellbody += aSiz
fa0f0 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  e[i];.    }.    
fa100 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79  assert( cellbody
fa110 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  ==pPage->pBt->us
fa120 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a  ableSize );.  }.
fa130 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d    pPage->nCell =
fa140 20 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   nCell;.}../*.**
fa150 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70   The following p
fa160 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 72 6d  arameters determ
fa170 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 64 6a  ine how many adj
fa180 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 74 20  acent pages get 
fa190 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 61  involved.** in a
fa1a0 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61   balancing opera
fa1b0 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 68 65  tion.  NN is the
fa1c0 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68   number of neigh
fa1d0 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73  bors on either s
fa1e0 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61  ide.** of the pa
fa1f0 67 65 20 74 68 61 74 20 70 61 72 74 69 63 69 70  ge that particip
fa200 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e  ate in the balan
fa210 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20  cing operation. 
fa220 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 74 6f   NB is the.** to
fa230 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
fa240 67 65 73 20 74 68 61 74 20 70 61 72 74 69 63 69  ges that partici
fa250 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20  pate, including 
fa260 74 68 65 20 74 61 72 67 65 74 20 70 61 67 65 20  the target page 
fa270 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 62  and.** NN neighb
fa280 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69  ors on either si
fa290 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69  de..**.** The mi
fa2a0 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e  nimum value of N
fa2b0 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 72 73  N is 1 (of cours
fa2c0 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e 67 20  e).  Increasing 
fa2d0 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 28 74  NN above 1.** (t
fa2e0 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 73 20  o 2 or 3) gives 
fa2f0 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f 76 65  a modest improve
fa300 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 20 61  ment in SELECT a
fa310 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 6f 72  nd DELETE perfor
fa320 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 63 68  mance.** in exch
fa330 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 67 65  ange for a large
fa340 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 69 6e  r degradation in
fa350 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44 41   INSERT and UPDA
fa360 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a  TE performance..
fa370 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
fa380 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 67 69  NN appears to gi
fa390 76 65 20 74 68 65 20 62 65 73 74 20 72 65 73 75  ve the best resu
fa3a0 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a  lts overall..*/.
fa3b0 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 20 20  #define NN 1    
fa3c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
fa3d0 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 20  er of neighbors 
fa3e0 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f  on either side o
fa3f0 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 66 69  f pPage */.#defi
fa400 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 20  ne NB (NN*2+1)  
fa410 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 61 67      /* Total pag
fa420 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74  es involved in t
fa430 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a 2f  he balance */../
fa440 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65  * Forward refere
fa450 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nce */.static in
fa460 74 20 62 61 6c 61 6e 63 65 28 4d 65 6d 50 61 67  t balance(MemPag
fa470 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 6e 64  e*, int);..#ifnd
fa480 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51  ef SQLITE_OMIT_Q
fa490 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a 2a  UICKBALANCE./*.*
fa4a0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
fa4b0 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61 6e 64  f balance() hand
fa4c0 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73  les the common s
fa4d0 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72  pecial case wher
fa4e0 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72 79  e.** a new entry
fa4f0 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74   is being insert
fa500 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72 65 6d  ed on the extrem
fa510 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66 20 74  e right-end of t
fa520 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 6f  he.** tree, in o
fa530 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68 65 6e  ther words, when
fa540 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77   the new entry w
fa550 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6c  ill become the l
fa560 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79 20  argest.** entry 
fa570 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a  in the tree..**.
fa580 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 74 72  ** Instead of tr
fa590 79 69 6e 67 20 62 61 6c 61 6e 63 65 20 74 68 65  ying balance the
fa5a0 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65   3 right-most le
fa5b0 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74 20 61  af pages, just a
fa5c0 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67 65  dd.** a new page
fa5d0 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 68 61   to the right-ha
fa5e0 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75 74 20  nd side and put 
fa5f0 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72  the one new entr
fa600 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67  y in.** that pag
fa610 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 20  e.  This leaves 
fa620 74 68 65 20 72 69 67 68 74 20 73 69 64 65 20 6f  the right side o
fa630 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d 65 77  f the tree somew
fa640 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65  hat.** unbalance
fa650 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61 72 65  d.  But odds are
fa660 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65   that we will be
fa670 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 65   inserting new e
fa680 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74 68 65  ntries.** at the
fa690 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72 77   end soon afterw
fa6a0 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65 61 72  ards so the near
fa6b0 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20 77 69  ly empty page wi
fa6c0 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69  ll quickly.** fi
fa6d0 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65 72 61  ll up.  On avera
fa6e0 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20  ge..**.** pPage 
fa6f0 69 73 20 74 68 65 20 6c 65 61 66 20 70 61 67 65  is the leaf page
fa700 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 69   which is the ri
fa710 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e  ght-most page in
fa720 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50   the tree..** pP
fa730 61 72 65 6e 74 20 69 73 20 69 74 73 20 70 61 72  arent is its par
fa740 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75 73 74  ent.  pPage must
fa750 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f   have a single o
fa760 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a  verflow entry.**
fa770 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f 20 74   which is also t
fa780 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e  he right-most en
fa790 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67 65 2e  try on the page.
fa7a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
fa7b0 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 4d 65 6d  alance_quick(Mem
fa7c0 50 61 67 65 20 2a 70 50 61 67 65 2c 20 4d 65 6d  Page *pPage, Mem
fa7d0 50 61 67 65 20 2a 70 50 61 72 65 6e 74 29 7b 0a  Page *pParent){.
fa7e0 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50    int rc;.  MemP
fa7f0 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 50 67 6e  age *pNew;.  Pgn
fa800 6f 20 70 67 6e 6f 4e 65 77 3b 0a 20 20 75 38 20  o pgnoNew;.  u8 
fa810 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 73 7a  *pCell;.  int sz
fa820 43 65 6c 6c 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f  Cell;.  CellInfo
fa830 20 69 6e 66 6f 3b 0a 20 20 42 74 53 68 61 72 65   info;.  BtShare
fa840 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
fa850 70 42 74 3b 0a 20 20 69 6e 74 20 70 61 72 65 6e  pBt;.  int paren
fa860 74 49 64 78 20 3d 20 70 50 61 72 65 6e 74 2d 3e  tIdx = pParent->
fa870 6e 43 65 6c 6c 3b 20 20 20 2f 2a 20 70 50 61 72  nCell;   /* pPar
fa880 65 6e 74 20 6e 65 77 20 64 69 76 69 64 65 72 20  ent new divider 
fa890 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20  cell index */.  
fa8a0 69 6e 74 20 70 61 72 65 6e 74 53 69 7a 65 3b 20  int parentSize; 
fa8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fa8c0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6e 65 77    /* Size of new
fa8d0 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f   divider cell */
fa8e0 0a 20 20 75 38 20 70 61 72 65 6e 74 43 65 6c 6c  .  u8 parentCell
fa8f0 5b 36 34 5d 3b 20 20 20 20 20 20 20 20 20 20 20  [64];           
fa900 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f       /* Space fo
fa910 72 20 74 68 65 20 6e 65 77 20 64 69 76 69 64 65  r the new divide
fa920 72 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 61 73 73  r cell */..  ass
fa930 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
fa940 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
fa950 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  Bt->mutex) );.. 
fa960 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e   /* Allocate a n
fa970 65 77 20 70 61 67 65 2e 20 49 6e 73 65 72 74 20  ew page. Insert 
fa980 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  the overflow cel
fa990 6c 20 66 72 6f 6d 20 70 50 61 67 65 0a 20 20 2a  l from pPage.  *
fa9a0 2a 20 69 6e 74 6f 20 69 74 2e 20 54 68 65 6e 20  * into it. Then 
fa9b0 72 65 6d 6f 76 65 20 74 68 65 20 6f 76 65 72 66  remove the overf
fa9c0 6c 6f 77 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50  low cell from pP
fa9d0 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  age..  */.  rc =
fa9e0 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
fa9f0 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26  ge(pBt, &pNew, &
faa00 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29 3b 0a  pgnoNew, 0, 0);.
faa10 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
faa20 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
faa30 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 65 6c  n rc;.  }.  pCel
faa40 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c  l = pPage->aOvfl
faa50 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 73 7a 43  [0].pCell;.  szC
faa60 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  ell = cellSizePt
faa70 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b  r(pPage, pCell);
faa80 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77  .  zeroPage(pNew
faa90 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30  , pPage->aData[0
faaa0 5d 29 3b 0a 20 20 61 73 73 65 6d 62 6c 65 50 61  ]);.  assemblePa
faab0 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65  ge(pNew, 1, &pCe
faac0 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 20  ll, &szCell);.  
faad0 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
faae0 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 53 65 74 20   = 0;..  /* Set 
faaf0 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 74 68  the parent of th
fab00 65 20 6e 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65  e newly allocate
fab10 64 20 70 61 67 65 20 74 6f 20 70 50 61 72 65 6e  d page to pParen
fab20 74 2e 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 70 50  t. */.  pNew->pP
fab30 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74 3b  arent = pParent;
fab40 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  .  sqlite3PagerR
fab50 65 66 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50  ef(pParent->pDbP
fab60 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 67  age);..  /* pPag
fab70 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 74  e is currently t
fab80 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f  he right-child o
fab90 66 20 70 50 61 72 65 6e 74 2e 20 43 68 61 6e 67  f pParent. Chang
faba0 65 20 74 68 69 73 0a 20 20 2a 2a 20 73 6f 20 74  e this.  ** so t
fabb0 68 61 74 20 74 68 65 20 72 69 67 68 74 2d 63 68  hat the right-ch
fabc0 69 6c 64 20 69 73 20 74 68 65 20 6e 65 77 20 70  ild is the new p
fabd0 61 67 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 62  age allocated ab
fabe0 6f 76 65 20 61 6e 64 0a 20 20 2a 2a 20 70 50 61  ove and.  ** pPa
fabf0 67 65 20 69 73 20 74 68 65 20 6e 65 78 74 2d 74  ge is the next-t
fac00 6f 2d 72 69 67 68 74 20 63 68 69 6c 64 2e 20 0a  o-right child. .
fac10 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
fac20 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b  Page->nCell>0 );
fac30 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43  .  pCell = findC
fac40 65 6c 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65  ell(pPage, pPage
fac50 2d 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 73 71  ->nCell-1);.  sq
fac60 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
fac70 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
fac80 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 72  ell, &info);.  r
fac90 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70  c = fillInCell(p
faca0 50 61 72 65 6e 74 2c 20 70 61 72 65 6e 74 43 65  Parent, parentCe
facb0 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79  ll, 0, info.nKey
facc0 2c 20 30 2c 20 30 2c 20 30 2c 20 26 70 61 72 65  , 0, 0, 0, &pare
facd0 6e 74 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72  ntSize);.  if( r
face0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
facf0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
fad00 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 61 72   }.  assert( par
fad10 65 6e 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20 20  entSize<64 );.  
fad20 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28  rc = insertCell(
fad30 70 50 61 72 65 6e 74 2c 20 70 61 72 65 6e 74 49  pParent, parentI
fad40 64 78 2c 20 70 61 72 65 6e 74 43 65 6c 6c 2c 20  dx, parentCell, 
fad50 70 61 72 65 6e 74 53 69 7a 65 2c 20 30 2c 20 34  parentSize, 0, 4
fad60 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
fad70 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
fad80 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70  turn rc;.  }.  p
fad90 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72  ut4byte(findOver
fada0 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72 65 6e 74  flowCell(pParent
fadb0 2c 70 61 72 65 6e 74 49 64 78 29 2c 20 70 50 61  ,parentIdx), pPa
fadc0 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 70 75 74  ge->pgno);.  put
fadd0 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e  4byte(&pParent->
fade0 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68  aData[pParent->h
fadf0 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e  drOffset+8], pgn
fae00 6f 4e 65 77 29 3b 0a 0a 23 69 66 6e 64 65 66 20  oNew);..#ifndef 
fae10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
fae20 56 41 43 55 55 4d 0a 20 20 2f 2a 20 49 66 20 74  VACUUM.  /* If t
fae30 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76  his is an auto-v
fae40 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20  acuum database, 
fae50 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74  update the point
fae60 65 72 20 6d 61 70 0a 20 20 2a 2a 20 77 69 74 68  er map.  ** with
fae70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65   entries for the
fae80 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64 20 61   new page, and a
fae90 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f 6d 20  ny pointer from 
faea0 74 68 65 20 0a 20 20 2a 2a 20 63 65 6c 6c 20 6f  the .  ** cell o
faeb0 6e 20 74 68 65 20 70 61 67 65 20 74 6f 20 61 6e  n the page to an
faec0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a   overflow page..
faed0 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e    */.  if( pBt->
faee0 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
faef0 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
faf00 28 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50  (pBt, pgnoNew, P
faf10 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61  TRMAP_BTREE, pPa
faf20 72 65 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20  rent->pgno);.   
faf30 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
faf40 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
faf50 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70   ptrmapPutOvfl(p
faf60 4e 65 77 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  New, 0);.    }. 
faf70 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
faf80 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
faf90 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 29 3b  leasePage(pNew);
fafa0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
fafb0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
fafc0 69 66 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 65  if..  /* Release
fafd0 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74   the reference t
fafe0 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 61  o the new page a
faff0 6e 64 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70  nd balance the p
fb000 61 72 65 6e 74 20 70 61 67 65 2c 0a 20 20 2a 2a  arent page,.  **
fb010 20 69 6e 20 63 61 73 65 20 74 68 65 20 64 69 76   in case the div
fb020 69 64 65 72 20 63 65 6c 6c 20 69 6e 73 65 72 74  ider cell insert
fb030 65 64 20 63 61 75 73 65 64 20 69 74 20 74 6f 20  ed caused it to 
fb040 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 2e  become overfull.
fb050 0a 20 20 2a 2f 0a 20 20 72 65 6c 65 61 73 65 50  .  */.  releaseP
fb060 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 72 65 74  age(pNew);.  ret
fb070 75 72 6e 20 62 61 6c 61 6e 63 65 28 70 50 61 72  urn balance(pPar
fb080 65 6e 74 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69  ent, 0);.}.#endi
fb090 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
fb0a0 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f  _QUICKBALANCE */
fb0b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
fb0c0 74 69 6e 65 20 72 65 64 69 73 74 72 69 62 75 74  tine redistribut
fb0d0 65 73 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67  es Cells on pPag
fb0e0 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 4e 2a 32  e and up to NN*2
fb0f0 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20   siblings.** of 
fb100 70 50 61 67 65 20 73 6f 20 74 68 61 74 20 61 6c  pPage so that al
fb110 6c 20 70 61 67 65 73 20 68 61 76 65 20 61 62 6f  l pages have abo
fb120 75 74 20 74 68 65 20 73 61 6d 65 20 61 6d 6f 75  ut the same amou
fb130 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65  nt of free space
fb140 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 4e 4e 20  ..** Usually NN 
fb150 73 69 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68  siblings on eith
fb160 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65  er side of pPage
fb170 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 20   is used in the 
fb180 62 61 6c 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68  balancing,.** th
fb190 6f 75 67 68 20 6d 6f 72 65 20 73 69 62 6c 69 6e  ough more siblin
fb1a0 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72  gs might come fr
fb1b0 6f 6d 20 6f 6e 65 20 73 69 64 65 20 69 66 20 70  om one side if p
fb1c0 50 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73  Page is the firs
fb1d0 74 0a 2a 2a 20 6f 72 20 6c 61 73 74 20 63 68 69  t.** or last chi
fb1e0 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74  ld of its parent
fb1f0 2e 20 20 49 66 20 70 50 61 67 65 20 68 61 73 20  .  If pPage has 
fb200 66 65 77 65 72 20 74 68 61 6e 20 32 2a 4e 4e 20  fewer than 2*NN 
fb210 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d  siblings.** (som
fb220 65 74 68 69 6e 67 20 77 68 69 63 68 20 63 61 6e  ething which can
fb230 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20   only happen if 
fb240 70 50 61 67 65 20 69 73 20 74 68 65 20 72 6f 6f  pPage is the roo
fb250 74 20 70 61 67 65 20 6f 72 20 61 20 0a 2a 2a 20  t page or a .** 
fb260 63 68 69 6c 64 20 6f 66 20 72 6f 6f 74 29 20 74  child of root) t
fb270 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c  hen all availabl
fb280 65 20 73 69 62 6c 69 6e 67 73 20 70 61 72 74 69  e siblings parti
fb290 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61  cipate in the ba
fb2a0 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  lancing..**.** T
fb2b0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62  he number of sib
fb2c0 6c 69 6e 67 73 20 6f 66 20 70 50 61 67 65 20 6d  lings of pPage m
fb2d0 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65  ight be increase
fb2e0 64 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 62  d or decreased b
fb2f0 79 20 6f 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f 20  y one or.** two 
fb300 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20  in an effort to 
fb310 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 72 6c  keep pages nearl
fb320 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f  y full but not o
fb330 76 65 72 20 66 75 6c 6c 2e 20 54 68 65 20 72 6f  ver full. The ro
fb340 6f 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 73 70  ot page.** is sp
fb350 65 63 69 61 6c 20 61 6e 64 20 69 73 20 61 6c 6c  ecial and is all
fb360 6f 77 65 64 20 74 6f 20 62 65 20 6e 65 61 72 6c  owed to be nearl
fb370 79 20 65 6d 70 74 79 2e 20 49 66 20 70 50 61 67  y empty. If pPag
fb380 65 20 69 73 20 0a 2a 2a 20 74 68 65 20 72 6f 6f  e is .** the roo
fb390 74 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65  t page, then the
fb3a0 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 74 72   depth of the tr
fb3b0 65 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72  ee might be incr
fb3c0 65 61 73 65 64 0a 2a 2a 20 6f 72 20 64 65 63 72  eased.** or decr
fb3d0 65 61 73 65 64 20 62 79 20 6f 6e 65 2c 20 61 73  eased by one, as
fb3e0 20 6e 65 63 65 73 73 61 72 79 2c 20 74 6f 20 6b   necessary, to k
fb3f0 65 65 70 20 74 68 65 20 72 6f 6f 74 20 70 61 67  eep the root pag
fb400 65 20 66 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20  e from being.** 
fb410 6f 76 65 72 66 75 6c 6c 20 6f 72 20 63 6f 6d 70  overfull or comp
fb420 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a  letely empty..**
fb430 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68  .** Note that wh
fb440 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
fb450 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20  is called, some 
fb460 6f 66 20 74 68 65 20 43 65 6c 6c 73 20 6f 6e 20  of the Cells on 
fb470 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e  pPage.** might n
fb480 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73  ot actually be s
fb490 74 6f 72 65 64 20 69 6e 20 70 50 61 67 65 2d 3e  tored in pPage->
fb4a0 61 44 61 74 61 5b 5d 2e 20 20 54 68 69 73 20 63  aData[].  This c
fb4b0 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20  an happen.** if 
fb4c0 74 68 65 20 70 61 67 65 20 69 73 20 6f 76 65 72  the page is over
fb4d0 66 75 6c 6c 2e 20 20 50 61 72 74 20 6f 66 20 74  full.  Part of t
fb4e0 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72  he job of this r
fb4f0 6f 75 74 69 6e 65 20 69 73 20 74 6f 0a 2a 2a 20  outine is to.** 
fb500 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 43 65  make sure all Ce
fb510 6c 6c 73 20 66 6f 72 20 70 50 61 67 65 20 6f 6e  lls for pPage on
fb520 63 65 20 61 67 61 69 6e 20 66 69 74 20 69 6e 20  ce again fit in 
fb530 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a  pPage->aData[]..
fb540 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75  **.** In the cou
fb550 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67  rse of balancing
fb560 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 6f 66   the siblings of
fb570 20 70 50 61 67 65 2c 20 74 68 65 20 70 61 72 65   pPage, the pare
fb580 6e 74 20 6f 66 20 70 50 61 67 65 0a 2a 2a 20 6d  nt of pPage.** m
fb590 69 67 68 74 20 62 65 63 6f 6d 65 20 6f 76 65 72  ight become over
fb5a0 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c  full or underful
fb5b0 6c 2e 20 20 49 66 20 74 68 61 74 20 68 61 70 70  l.  If that happ
fb5c0 65 6e 73 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ens, then this r
fb5d0 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c  outine.** is cal
fb5e0 6c 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 20  led recursively 
fb5f0 6f 6e 20 74 68 65 20 70 61 72 65 6e 74 2e 0a 2a  on the parent..*
fb600 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75  *.** If this rou
fb610 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61  tine fails for a
fb620 6e 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69  ny reason, it mi
fb630 67 68 74 20 6c 65 61 76 65 20 74 68 65 20 64 61  ght leave the da
fb640 74 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63  tabase.** in a c
fb650 6f 72 72 75 70 74 65 64 20 73 74 61 74 65 2e 20  orrupted state. 
fb660 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74   So if this rout
fb670 69 6e 65 20 66 61 69 6c 73 2c 20 74 68 65 20 64  ine fails, the d
fb680 61 74 61 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a  atabase should.*
fb690 2a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  * be rolled back
fb6a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
fb6b0 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28  balance_nonroot(
fb6c0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b  MemPage *pPage){
fb6d0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72  .  MemPage *pPar
fb6e0 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ent;            
fb6f0 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f 66  /* The parent of
fb700 20 70 50 61 67 65 20 2a 2f 0a 20 20 42 74 53 68   pPage */.  BtSh
fb710 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
fb720 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
fb730 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a  whole database *
fb740 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20  /.  int nCell = 
fb750 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
fb760 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65   /* Number of ce
fb770 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20  lls in apCell[] 
fb780 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c  */.  int nMaxCel
fb790 6c 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ls = 0;         
fb7a0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73    /* Allocated s
fb7b0 69 7a 65 20 6f 66 20 61 70 43 65 6c 6c 2c 20 73  ize of apCell, s
fb7c0 7a 43 65 6c 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f  zCell, aFrom. */
fb7d0 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 20 20 20 20  .  int nOld;    
fb7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb7f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
fb800 65 73 20 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f  es in apOld[] */
fb810 0a 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20  .  int nNew;    
fb820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb830 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
fb840 65 73 20 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f  es in apNew[] */
fb850 0a 20 20 69 6e 74 20 6e 44 69 76 3b 20 20 20 20  .  int nDiv;    
fb860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb870 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c  /* Number of cel
fb880 6c 73 20 69 6e 20 61 70 44 69 76 5b 5d 20 2a 2f  ls in apDiv[] */
fb890 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20  .  int i, j, k; 
fb8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb8b0 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
fb8c0 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 20 20   */.  int idx;  
fb8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb8e0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 70     /* Index of p
fb8f0 50 61 67 65 20 69 6e 20 70 50 61 72 65 6e 74 2d  Page in pParent-
fb900 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e  >aCell[] */.  in
fb910 74 20 6e 78 44 69 76 3b 20 20 20 20 20 20 20 20  t nxDiv;        
fb920 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
fb930 78 74 20 64 69 76 69 64 65 72 20 73 6c 6f 74 20  xt divider slot 
fb940 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c  in pParent->aCel
fb950 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  l[] */.  int rc;
fb960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb970 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74        /* The ret
fb980 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn code */.  in
fb990 74 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  t leafCorrection
fb9a0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34 20  ;          /* 4 
fb9b0 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65  if pPage is a le
fb9c0 61 66 2e 20 20 30 20 69 66 20 6e 6f 74 20 2a 2f  af.  0 if not */
fb9d0 0a 20 20 69 6e 74 20 6c 65 61 66 44 61 74 61 3b  .  int leafData;
fb9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb9f0 2f 2a 20 54 72 75 65 20 69 66 20 70 50 61 67 65  /* True if pPage
fba00 20 69 73 20 61 20 6c 65 61 66 20 6f 66 20 61 20   is a leaf of a 
fba10 4c 45 41 46 44 41 54 41 20 74 72 65 65 20 2a 2f  LEAFDATA tree */
fba20 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 70 61  .  int usableSpa
fba30 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ce;             
fba40 2f 2a 20 42 79 74 65 73 20 69 6e 20 70 50 61 67  /* Bytes in pPag
fba50 65 20 62 65 79 6f 6e 64 20 74 68 65 20 68 65 61  e beyond the hea
fba60 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67  der */.  int pag
fba70 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  eFlags;         
fba80 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
fba90 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30  f pPage->aData[0
fbaa0 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 74 6f  ] */.  int subto
fbab0 74 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  tal;            
fbac0 20 20 20 20 2f 2a 20 53 75 62 74 6f 74 61 6c 20      /* Subtotal 
fbad0 6f 66 20 62 79 74 65 73 20 69 6e 20 63 65 6c 6c  of bytes in cell
fbae0 73 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f  s on one page */
fbaf0 0a 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20  .  int iSpace = 
fbb00 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
fbb10 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20  /* First unused 
fbb20 62 79 74 65 20 6f 66 20 61 53 70 61 63 65 5b 5d  byte of aSpace[]
fbb30 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61   */.  MemPage *a
fbb40 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20  pOld[NB];       
fbb50 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64 20     /* pPage and 
fbb60 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c 69 6e  up to two siblin
fbb70 67 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  gs */.  Pgno pgn
fbb80 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20  oOld[NB];       
fbb90 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
fbba0 62 65 72 73 20 66 6f 72 20 65 61 63 68 20 70 61  bers for each pa
fbbb0 67 65 20 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f  ge in apOld[] */
fbbc0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 43 6f  .  MemPage *apCo
fbbd0 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20  py[NB];         
fbbe0 2f 2a 20 50 72 69 76 61 74 65 20 63 6f 70 69 65  /* Private copie
fbbf0 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70 61 67  s of apOld[] pag
fbc00 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  es */.  MemPage 
fbc10 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20  *apNew[NB+2];   
fbc20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e       /* pPage an
fbc30 64 20 75 70 20 74 6f 20 4e 42 20 73 69 62 6c 69  d up to NB sibli
fbc40 6e 67 73 20 61 66 74 65 72 20 62 61 6c 61 6e 63  ngs after balanc
fbc50 69 6e 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ing */.  Pgno pg
fbc60 6e 6f 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20  noNew[NB+2];    
fbc70 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
fbc80 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68 20 70  mbers for each p
fbc90 61 67 65 20 69 6e 20 61 70 4e 65 77 5b 5d 20 2a  age in apNew[] *
fbca0 2f 0a 20 20 75 38 20 2a 61 70 44 69 76 5b 4e 42  /.  u8 *apDiv[NB
fbcb0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
fbcc0 20 2f 2a 20 44 69 76 69 64 65 72 20 63 65 6c 6c   /* Divider cell
fbcd0 73 20 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a  s in pParent */.
fbce0 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b    int cntNew[NB+
fbcf0 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  2];            /
fbd00 2a 20 49 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c  * Index in aCell
fbd10 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66 74 65 72  [] of cell after
fbd20 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20   i-th page */.  
fbd30 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b  int szNew[NB+2];
fbd40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fbd50 43 6f 6d 62 69 6e 65 64 20 73 69 7a 65 20 6f 66  Combined size of
fbd60 20 63 65 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20   cells place on 
fbd70 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 75  i-th page */.  u
fbd80 38 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20  8 **apCell = 0; 
fbd90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
fbda0 6c 6c 20 63 65 6c 6c 73 20 62 65 67 69 6e 20 62  ll cells begin b
fbdb0 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74  alanced */.  int
fbdc0 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20   *szCell;       
fbdd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63            /* Loc
fbde0 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63  al size of all c
fbdf0 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ells in apCell[]
fbe00 20 2a 2f 0a 20 20 75 38 20 2a 61 43 6f 70 79 5b   */.  u8 *aCopy[
fbe10 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  NB];            
fbe20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20     /* Space for 
fbe30 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 6f 66 20  holding data of 
fbe40 61 70 43 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75 38  apCopy[] */.  u8
fbe50 20 2a 61 53 70 61 63 65 3b 20 20 20 20 20 20 20   *aSpace;       
fbe60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
fbe70 61 63 65 20 74 6f 20 68 6f 6c 64 20 63 6f 70 69  ace to hold copi
fbe80 65 73 20 6f 66 20 64 69 76 69 64 65 72 73 20 63  es of dividers c
fbe90 65 6c 6c 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20  ells */.#ifndef 
fbea0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
fbeb0 56 41 43 55 55 4d 0a 20 20 75 38 20 2a 61 46 72  VACUUM.  u8 *aFr
fbec0 6f 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  om = 0;.#endif..
fbed0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
fbee0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
fbef0 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
fbf00 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 46  );..  /* .  ** F
fbf10 69 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 70  ind the parent p
fbf20 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  age..  */.  asse
fbf30 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  rt( pPage->isIni
fbf40 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  t );.  assert( s
fbf50 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
fbf60 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
fbf70 62 50 61 67 65 29 20 7c 7c 20 70 50 61 67 65 2d  bPage) || pPage-
fbf80 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b  >nOverflow==1 );
fbf90 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  .  pBt = pPage->
fbfa0 70 42 74 3b 0a 20 20 70 50 61 72 65 6e 74 20 3d  pBt;.  pParent =
fbfb0 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 3b   pPage->pParent;
fbfc0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65  .  assert( pPare
fbfd0 6e 74 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  nt );.  if( SQLI
fbfe0 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
fbff0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
fc000 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29  Parent->pDbPage)
fc010 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
fc020 72 63 3b 0a 20 20 7d 0a 20 20 54 52 41 43 45 28  rc;.  }.  TRACE(
fc030 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e  ("BALANCE: begin
fc040 20 70 61 67 65 20 25 64 20 63 68 69 6c 64 20 6f   page %d child o
fc050 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e  f %d\n", pPage->
fc060 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70  pgno, pParent->p
fc070 67 6e 6f 29 29 3b 0a 0a 23 69 66 6e 64 65 66 20  gno));..#ifndef 
fc080 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43  SQLITE_OMIT_QUIC
fc090 4b 42 41 4c 41 4e 43 45 0a 20 20 2f 2a 0a 20 20  KBALANCE.  /*.  
fc0a0 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 63 61 73  ** A special cas
fc0b0 65 3a 20 20 49 66 20 61 20 6e 65 77 20 65 6e 74  e:  If a new ent
fc0c0 72 79 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ry has just been
fc0d0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61   inserted into a
fc0e0 0a 20 20 2a 2a 20 74 61 62 6c 65 20 28 74 68 61  .  ** table (tha
fc0f0 74 20 69 73 2c 20 61 20 62 74 72 65 65 20 77 69  t is, a btree wi
fc100 74 68 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20  th integer keys 
fc110 61 6e 64 20 61 6c 6c 20 64 61 74 61 20 61 74 20  and all data at 
fc120 74 68 65 20 6c 65 61 76 65 73 29 0a 20 20 2a 2a  the leaves).  **
fc130 20 61 6e 64 20 74 68 65 20 6e 65 77 20 65 6e 74   and the new ent
fc140 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74 2d  ry is the right-
fc150 6d 6f 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  most entry in th
fc160 65 20 74 72 65 65 20 28 69 74 20 68 61 73 20 74  e tree (it has t
fc170 68 65 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74 20  he.  ** largest 
fc180 6b 65 79 29 20 74 68 65 6e 20 75 73 65 20 74 68  key) then use th
fc190 65 20 73 70 65 63 69 61 6c 20 62 61 6c 61 6e 63  e special balanc
fc1a0 65 5f 71 75 69 63 6b 28 29 20 72 6f 75 74 69 6e  e_quick() routin
fc1b0 65 20 66 6f 72 0a 20 20 2a 2a 20 62 61 6c 61 6e  e for.  ** balan
fc1c0 63 69 6e 67 2e 20 20 62 61 6c 61 6e 63 65 5f 71  cing.  balance_q
fc1d0 75 69 63 6b 28 29 20 69 73 20 6d 75 63 68 20 66  uick() is much f
fc1e0 61 73 74 65 72 20 61 6e 64 20 72 65 73 75 6c 74  aster and result
fc1f0 73 20 69 6e 20 61 20 74 69 67 68 74 65 72 0a 20  s in a tighter. 
fc200 20 2a 2a 20 70 61 63 6b 69 6e 67 20 6f 66 20 64   ** packing of d
fc210 61 74 61 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f  ata in the commo
fc220 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  n case..  */.  i
fc230 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 26  f( pPage->leaf &
fc240 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69  &.      pPage->i
fc250 6e 74 4b 65 79 20 26 26 0a 20 20 20 20 20 20 70  ntKey &&.      p
fc260 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61 20 26  Page->leafData &
fc270 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e  &.      pPage->n
fc280 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 26 26 0a 20  Overflow==1 &&. 
fc290 20 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 66       pPage->aOvf
fc2a0 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 2d  l[0].idx==pPage-
fc2b0 3e 6e 43 65 6c 6c 20 26 26 0a 20 20 20 20 20 20  >nCell &&.      
fc2c0 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 2d 3e  pPage->pParent->
fc2d0 70 67 6e 6f 21 3d 31 20 26 26 0a 20 20 20 20 20  pgno!=1 &&.     
fc2e0 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65   get4byte(&pPare
fc2f0 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e  nt->aData[pParen
fc300 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  t->hdrOffset+8])
fc310 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 0a 20 20  ==pPage->pgno.  
fc320 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  ){.    /*.    **
fc330 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 74 68 65   TODO: Check the
fc340 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 74 68 65   siblings to the
fc350 20 6c 65 66 74 20 6f 66 20 70 50 61 67 65 2e 20   left of pPage. 
fc360 49 74 20 6d 61 79 20 62 65 20 74 68 61 74 0a 20  It may be that. 
fc370 20 20 20 2a 2a 20 74 68 65 79 20 61 72 65 20 6e     ** they are n
fc380 6f 74 20 66 75 6c 6c 20 61 6e 64 20 6e 6f 20 6e  ot full and no n
fc390 65 77 20 70 61 67 65 20 69 73 20 72 65 71 75 69  ew page is requi
fc3a0 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  red..    */.    
fc3b0 72 65 74 75 72 6e 20 62 61 6c 61 6e 63 65 5f 71  return balance_q
fc3c0 75 69 63 6b 28 70 50 61 67 65 2c 20 70 50 61 72  uick(pPage, pPar
fc3d0 65 6e 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ent);.  }.#endif
fc3e0 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
fc3f0 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
fc400 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
fc410 2d 3e 70 44 62 50 61 67 65 29 29 20 29 7b 0a 20  ->pDbPage)) ){. 
fc420 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
fc430 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e  }..  /*.  ** Fin
fc440 64 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68  d the cell in th
fc450 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 68  e parent page wh
fc460 6f 73 65 20 6c 65 66 74 20 63 68 69 6c 64 20 70  ose left child p
fc470 6f 69 6e 74 73 20 62 61 63 6b 0a 20 20 2a 2a 20  oints back.  ** 
fc480 74 6f 20 70 50 61 67 65 2e 20 20 54 68 65 20 22  to pPage.  The "
fc490 69 64 78 22 20 76 61 72 69 61 62 6c 65 20 69 73  idx" variable is
fc4a0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
fc4b0 61 74 20 63 65 6c 6c 2e 20 20 49 66 20 70 50 61  at cell.  If pPa
fc4c0 67 65 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 72  ge.  ** is the r
fc4d0 69 67 68 74 6d 6f 73 74 20 63 68 69 6c 64 20 6f  ightmost child o
fc4e0 66 20 70 50 61 72 65 6e 74 20 74 68 65 6e 20 73  f pParent then s
fc4f0 65 74 20 69 64 78 20 74 6f 20 70 50 61 72 65 6e  et idx to pParen
fc500 74 2d 3e 6e 43 65 6c 6c 20 0a 20 20 2a 2f 0a 20  t->nCell .  */. 
fc510 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 69 64   if( pParent->id
fc520 78 53 68 69 66 74 20 29 7b 0a 20 20 20 20 50 67  xShift ){.    Pg
fc530 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 70 67 6e  no pgno;.    pgn
fc540 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b  o = pPage->pgno;
fc550 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e  .    assert( pgn
fc560 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 50  o==sqlite3PagerP
fc570 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d  agenumber(pPage-
fc580 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20  >pDbPage) );.   
fc590 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 64 78 3c   for(idx=0; idx<
fc5a0 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 20  pParent->nCell; 
fc5b0 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  idx++){.      if
fc5c0 28 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43  ( get4byte(findC
fc5d0 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 64 78  ell(pParent, idx
fc5e0 29 29 3d 3d 70 67 6e 6f 20 29 7b 0a 20 20 20 20  ))==pgno ){.    
fc5f0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
fc600 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73   }.    }.    ass
fc610 65 72 74 28 20 69 64 78 3c 70 50 61 72 65 6e 74  ert( idx<pParent
fc620 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 20  ->nCell.        
fc630 20 20 20 20 20 7c 7c 20 67 65 74 34 62 79 74 65       || get4byte
fc640 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61  (&pParent->aData
fc650 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66  [pParent->hdrOff
fc660 73 65 74 2b 38 5d 29 3d 3d 70 67 6e 6f 20 29 3b  set+8])==pgno );
fc670 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 64  .  }else{.    id
fc680 78 20 3d 20 70 50 61 67 65 2d 3e 69 64 78 50 61  x = pPage->idxPa
fc690 72 65 6e 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  rent;.  }..  /*.
fc6a0 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    ** Initialize 
fc6b0 76 61 72 69 61 62 6c 65 73 20 73 6f 20 74 68 61  variables so tha
fc6c0 74 20 69 74 20 77 69 6c 6c 20 62 65 20 73 61 66  t it will be saf
fc6d0 65 20 74 6f 20 6a 75 6d 70 0a 20 20 2a 2a 20 64  e to jump.  ** d
fc6e0 69 72 65 63 74 6c 79 20 74 6f 20 62 61 6c 61 6e  irectly to balan
fc6f0 63 65 5f 63 6c 65 61 6e 75 70 20 61 74 20 61 6e  ce_cleanup at an
fc700 79 20 6d 6f 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20  y moment..  */. 
fc710 20 6e 4f 6c 64 20 3d 20 6e 4e 65 77 20 3d 20 30   nOld = nNew = 0
fc720 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ;.  sqlite3Pager
fc730 52 65 66 28 70 50 61 72 65 6e 74 2d 3e 70 44 62  Ref(pParent->pDb
fc740 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  Page);..  /*.  *
fc750 2a 20 46 69 6e 64 20 73 69 62 6c 69 6e 67 20 70  * Find sibling p
fc760 61 67 65 73 20 74 6f 20 70 50 61 67 65 20 61 6e  ages to pPage an
fc770 64 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 70  d the cells in p
fc780 50 61 72 65 6e 74 20 74 68 61 74 20 64 69 76 69  Parent that divi
fc790 64 65 0a 20 20 2a 2a 20 74 68 65 20 73 69 62 6c  de.  ** the sibl
fc7a0 69 6e 67 73 2e 20 20 41 6e 20 61 74 74 65 6d 70  ings.  An attemp
fc7b0 74 20 69 73 20 6d 61 64 65 20 74 6f 20 66 69 6e  t is made to fin
fc7c0 64 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e  d NN siblings on
fc7d0 20 65 69 74 68 65 72 0a 20 20 2a 2a 20 73 69 64   either.  ** sid
fc7e0 65 20 6f 66 20 70 50 61 67 65 2e 20 20 4d 6f 72  e of pPage.  Mor
fc7f0 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 74  e siblings are t
fc800 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e 65 20 73 69  aken from one si
fc810 64 65 2c 20 68 6f 77 65 76 65 72 2c 20 69 66 0a  de, however, if.
fc820 20 20 2a 2a 20 70 50 61 67 65 20 74 68 65 72 65    ** pPage there
fc830 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20   are fewer than 
fc840 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74  NN siblings on t
fc850 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e 20 20  he other side.  
fc860 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20  If pParent.  ** 
fc870 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20  has NB or fewer 
fc880 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c  children then al
fc890 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50  l children of pP
fc8a0 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e  arent are taken.
fc8b0 0a 20 20 2a 2f 0a 20 20 6e 78 44 69 76 20 3d 20  .  */.  nxDiv = 
fc8c0 69 64 78 20 2d 20 4e 4e 3b 0a 20 20 69 66 28 20  idx - NN;.  if( 
fc8d0 6e 78 44 69 76 20 2b 20 4e 42 20 3e 20 70 50 61  nxDiv + NB > pPa
fc8e0 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  rent->nCell ){. 
fc8f0 20 20 20 6e 78 44 69 76 20 3d 20 70 50 61 72 65     nxDiv = pPare
fc900 6e 74 2d 3e 6e 43 65 6c 6c 20 2d 20 4e 42 20 2b  nt->nCell - NB +
fc910 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 78   1;.  }.  if( nx
fc920 44 69 76 3c 30 20 29 7b 0a 20 20 20 20 6e 78 44  Div<0 ){.    nxD
fc930 69 76 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 44  iv = 0;.  }.  nD
fc940 69 76 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  iv = 0;.  for(i=
fc950 30 2c 20 6b 3d 6e 78 44 69 76 3b 20 69 3c 4e 42  0, k=nxDiv; i<NB
fc960 3b 20 69 2b 2b 2c 20 6b 2b 2b 29 7b 0a 20 20 20  ; i++, k++){.   
fc970 20 69 66 28 20 6b 3c 70 50 61 72 65 6e 74 2d 3e   if( k<pParent->
fc980 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 61  nCell ){.      a
fc990 70 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43 65  pDiv[i] = findCe
fc9a0 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6b 29 3b 0a  ll(pParent, k);.
fc9b0 20 20 20 20 20 20 6e 44 69 76 2b 2b 3b 0a 20 20        nDiv++;.  
fc9c0 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
fc9d0 72 65 6e 74 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  rent->leaf );.  
fc9e0 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d      pgnoOld[i] =
fc9f0 20 67 65 74 34 62 79 74 65 28 61 70 44 69 76 5b   get4byte(apDiv[
fca00 69 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  i]);.    }else i
fca10 66 28 20 6b 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e  f( k==pParent->n
fca20 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 67  Cell ){.      pg
fca30 6e 6f 4f 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62  noOld[i] = get4b
fca40 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44  yte(&pParent->aD
fca50 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72  ata[pParent->hdr
fca60 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
fca70 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 62 72 65  }else{.      bre
fca80 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  ak;.    }.    rc
fca90 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
fcaa0 65 28 70 42 74 2c 20 70 67 6e 6f 4f 6c 64 5b 69  e(pBt, pgnoOld[i
fcab0 5d 2c 20 26 61 70 4f 6c 64 5b 69 5d 2c 20 70 50  ], &apOld[i], pP
fcac0 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 20  arent);.    if( 
fcad0 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63  rc ) goto balanc
fcae0 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 61  e_cleanup;.    a
fcaf0 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72 65  pOld[i]->idxPare
fcb00 6e 74 20 3d 20 6b 3b 0a 20 20 20 20 61 70 43 6f  nt = k;.    apCo
fcb10 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 61  py[i] = 0;.    a
fcb20 73 73 65 72 74 28 20 69 3d 3d 6e 4f 6c 64 20 29  ssert( i==nOld )
fcb30 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b 3b 0a 20 20  ;.    nOld++;.  
fcb40 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d 20 31    nMaxCells += 1
fcb50 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65 6c 6c  +apOld[i]->nCell
fcb60 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76 65 72  +apOld[i]->nOver
fcb70 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  flow;.  }..  /* 
fcb80 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20 61  Make nMaxCells a
fcb90 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 20 69   multiple of 2 i
fcba0 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65  n order to prese
fcbb0 72 76 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20  rve 8-byte.  ** 
fcbc0 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20 6e  alignment */.  n
fcbd0 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61 78  MaxCells = (nMax
fcbe0 43 65 6c 6c 73 20 2b 20 31 29 26 7e 31 3b 0a 0a  Cells + 1)&~1;..
fcbf0 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61    /*.  ** Alloca
fcc00 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d  te space for mem
fcc10 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 0a 20  ory structures. 
fcc20 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73   */.  apCell = s
fcc30 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 0a  qlite3_malloc( .
fcc40 20 20 20 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73         nMaxCells
fcc50 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20 20 20 20  *sizeof(u8*)    
fcc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fcc70 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c 6c         /* apCell
fcc80 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 43   */.     + nMaxC
fcc90 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 69 6e 74 29  ells*sizeof(int)
fcca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fccb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a             /* sz
fccc0 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 52  Cell */.     + R
fccd0 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d  OUND8(sizeof(Mem
fcce0 50 61 67 65 29 29 2a 4e 42 20 20 20 20 20 20 20  Page))*NB       
fccf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
fcd00 2a 20 61 43 6f 70 79 20 2a 2f 0a 20 20 20 20 20  * aCopy */.     
fcd10 2b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2a  + pBt->pageSize*
fcd20 28 35 2b 4e 42 29 20 20 20 20 20 20 20 20 20 20  (5+NB)          
fcd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fcd40 20 20 2f 2a 20 61 53 70 61 63 65 20 2a 2f 0a 20    /* aSpace */. 
fcd50 20 20 20 20 2b 20 28 49 53 41 55 54 4f 56 41 43      + (ISAUTOVAC
fcd60 55 55 4d 20 3f 20 6e 4d 61 78 43 65 6c 6c 73 20  UUM ? nMaxCells 
fcd70 3a 20 30 29 20 20 20 20 20 20 20 20 20 20 20 20  : 0)            
fcd80 20 20 20 20 20 20 2f 2a 20 61 46 72 6f 6d 20 2a        /* aFrom *
fcd90 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20 61 70 43  /.  );.  if( apC
fcda0 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  ell==0 ){.    rc
fcdb0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
fcdc0 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63  .    goto balanc
fcdd0 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  e_cleanup;.  }. 
fcde0 20 73 7a 43 65 6c 6c 20 3d 20 28 69 6e 74 2a 29   szCell = (int*)
fcdf0 26 61 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c  &apCell[nMaxCell
fce00 73 5d 3b 0a 20 20 61 43 6f 70 79 5b 30 5d 20 3d  s];.  aCopy[0] =
fce10 20 28 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d   (u8*)&szCell[nM
fce20 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65  axCells];.  asse
fce30 72 74 28 20 28 28 61 43 6f 70 79 5b 30 5d 20 2d  rt( ((aCopy[0] -
fce40 20 28 75 38 2a 29 61 70 43 65 6c 6c 29 20 26 20   (u8*)apCell) & 
fce50 37 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79  7)==0 ); /* 8-by
fce60 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71  te alignment req
fce70 75 69 72 65 64 20 2a 2f 0a 20 20 66 6f 72 28 69  uired */.  for(i
fce80 3d 31 3b 20 69 3c 4e 42 3b 20 69 2b 2b 29 7b 0a  =1; i<NB; i++){.
fce90 20 20 20 20 61 43 6f 70 79 5b 69 5d 20 3d 20 26      aCopy[i] = &
fcea0 61 43 6f 70 79 5b 69 2d 31 5d 5b 70 42 74 2d 3e  aCopy[i-1][pBt->
fceb0 70 61 67 65 53 69 7a 65 2b 52 4f 55 4e 44 38 28  pageSize+ROUND8(
fcec0 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29  sizeof(MemPage))
fced0 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ];.    assert( (
fcee0 28 61 43 6f 70 79 5b 69 5d 20 2d 20 28 75 38 2a  (aCopy[i] - (u8*
fcef0 29 61 70 43 65 6c 6c 29 20 26 20 37 29 3d 3d 30  )apCell) & 7)==0
fcf00 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c   ); /* 8-byte al
fcf10 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 64  ignment required
fcf20 20 2a 2f 0a 20 20 7d 0a 20 20 61 53 70 61 63 65   */.  }.  aSpace
fcf30 20 3d 20 26 61 43 6f 70 79 5b 4e 42 2d 31 5d 5b   = &aCopy[NB-1][
fcf40 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2b 52 4f  pBt->pageSize+RO
fcf50 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50  UND8(sizeof(MemP
fcf60 61 67 65 29 29 5d 3b 0a 20 20 61 73 73 65 72 74  age))];.  assert
fcf70 28 20 28 28 61 53 70 61 63 65 20 2d 20 28 75 38  ( ((aSpace - (u8
fcf80 2a 29 61 70 43 65 6c 6c 29 20 26 20 37 29 3d 3d  *)apCell) & 7)==
fcf90 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61  0 ); /* 8-byte a
fcfa0 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65  lignment require
fcfb0 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  d */.#ifndef SQL
fcfc0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
fcfd0 55 55 4d 0a 20 20 69 66 28 20 70 42 74 2d 3e 61  UUM.  if( pBt->a
fcfe0 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
fcff0 20 61 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 65   aFrom = &aSpace
fd000 5b 35 2a 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  [5*pBt->pageSize
fd010 5d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  ];.  }.#endif.  
fd020 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20  .  /*.  ** Make 
fd030 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 63 6f  copies of the co
fd040 6e 74 65 6e 74 20 6f 66 20 70 50 61 67 65 20 61  ntent of pPage a
fd050 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20  nd its siblings 
fd060 69 6e 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a  into aOld[]..  *
fd070 2a 20 54 68 65 20 72 65 73 74 20 6f 66 20 74 68  * The rest of th
fd080 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c  is function will
fd090 20 75 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74   use data from t
fd0a0 68 65 20 63 6f 70 69 65 73 20 72 61 74 68 65 72  he copies rather
fd0b0 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f  .  ** that the o
fd0c0 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69  riginal pages si
fd0d0 6e 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  nce the original
fd0e0 20 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69   pages will be i
fd0f0 6e 20 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65  n the.  ** proce
fd100 73 73 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72  ss of being over
fd110 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20  written..  */.  
fd120 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b  for(i=0; i<nOld;
fd130 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61   i++){.    MemPa
fd140 67 65 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69  ge *p = apCopy[i
fd150 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 61 43  ] = (MemPage*)aC
fd160 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63  opy[i];.    memc
fd170 70 79 28 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20  py(p, apOld[i], 
fd180 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29  sizeof(MemPage))
fd190 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74 61 20 3d  ;.    p->aData =
fd1a0 20 28 76 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20   (void*)&p[1];. 
fd1b0 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61     memcpy(p->aDa
fd1c0 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44  ta, apOld[i]->aD
fd1d0 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69  ata, pBt->pageSi
fd1e0 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  ze);.  }..  /*. 
fd1f0 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72   ** Load pointer
fd200 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f  s to all cells o
fd210 6e 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20  n sibling pages 
fd220 61 6e 64 20 74 68 65 20 64 69 76 69 64 65 72 20  and the divider 
fd230 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20  cells.  ** into 
fd240 74 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c  the local apCell
fd250 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20  [] array.  Make 
fd260 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 64 69  copies of the di
fd270 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a  vider cells.  **
fd280 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61   into space obta
fd290 69 6e 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65  ined form aSpace
fd2a0 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68  [] and remove th
fd2b0 65 20 74 68 65 20 64 69 76 69 64 65 72 20 43 65  e the divider Ce
fd2c0 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50  lls.  ** from pP
fd2d0 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  arent..  **.  **
fd2e0 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73   If the siblings
fd2f0 20 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67   are on leaf pag
fd300 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69  es, then the chi
fd310 6c 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74  ld pointers of t
fd320 68 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20  he.  ** divider 
fd330 63 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70  cells are stripp
fd340 65 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c  ed from the cell
fd350 73 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72  s before they ar
fd360 65 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e  e copied.  ** in
fd370 74 6f 20 61 53 70 61 63 65 5b 5d 2e 20 20 49 6e  to aSpace[].  In
fd380 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63   this way, all c
fd390 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ells in apCell[]
fd3a0 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a   are without.  *
fd3b0 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73  * child pointers
fd3c0 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61  .  If siblings a
fd3d0 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74  re not leaves, t
fd3e0 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a  hen all cell in.
fd3f0 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e    ** apCell[] in
fd400 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e  clude child poin
fd410 74 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61  ters.  Either wa
fd420 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20  y, all cells in 
fd430 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72  apCell[].  ** ar
fd440 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20  e alike..  **.  
fd450 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  ** leafCorrectio
fd460 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69  n:  4 if pPage i
fd470 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20  s a leaf.  0 if 
fd480 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c  pPage is not a l
fd490 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20  eaf..  **       
fd4a0 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20  leafData:  1 if 
fd4b0 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b  pPage holds key+
fd4c0 64 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74  data and pParent
fd4d0 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73   holds only keys
fd4e0 2e 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d  ..  */.  nCell =
fd4f0 20 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63   0;.  leafCorrec
fd500 74 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65  tion = pPage->le
fd510 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61  af*4;.  leafData
fd520 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61   = pPage->leafDa
fd530 74 61 20 26 26 20 70 50 61 67 65 2d 3e 6c 65 61  ta && pPage->lea
fd540 66 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  f;.  for(i=0; i<
fd550 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nOld; i++){.    
fd560 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20  MemPage *pOld = 
fd570 61 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69  apCopy[i];.    i
fd580 6e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d  nt limit = pOld-
fd590 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76  >nCell+pOld->nOv
fd5a0 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28  erflow;.    for(
fd5b0 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b  j=0; j<limit; j+
fd5c0 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  +){.      assert
fd5d0 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c  ( nCell<nMaxCell
fd5e0 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c  s );.      apCel
fd5f0 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f  l[nCell] = findO
fd600 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64  verflowCell(pOld
fd610 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65  , j);.      szCe
fd620 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c  ll[nCell] = cell
fd630 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70  SizePtr(pOld, ap
fd640 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 23 69  Cell[nCell]);.#i
fd650 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
fd660 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
fd670 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
fd680 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20  Vacuum ){.      
fd690 20 20 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20    int a;.       
fd6a0 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20   aFrom[nCell] = 
fd6b0 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 61  i;.        for(a
fd6c0 3d 30 3b 20 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65  =0; a<pOld->nOve
fd6d0 72 66 6c 6f 77 3b 20 61 2b 2b 29 7b 0a 20 20 20  rflow; a++){.   
fd6e0 20 20 20 20 20 20 20 69 66 28 20 70 4f 6c 64 2d         if( pOld-
fd6f0 3e 61 4f 76 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d  >aOvfl[a].pCell=
fd700 3d 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29  =apCell[nCell] )
fd710 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 46  {.            aF
fd720 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46  rom[nCell] = 0xF
fd730 46 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  F;.            b
fd740 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
fd750 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
fd760 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
fd770 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a   nCell++;.    }.
fd780 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31      if( i<nOld-1
fd790 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a   ){.      int sz
fd7a0 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
fd7b0 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d  Parent, apDiv[i]
fd7c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61  );.      if( lea
fd7d0 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20  fData ){.       
fd7e0 20 2f 2a 20 57 69 74 68 20 74 68 65 20 4c 45 41   /* With the LEA
fd7f0 46 44 41 54 41 20 66 6c 61 67 2c 20 70 50 61 72  FDATA flag, pPar
fd800 65 6e 74 20 63 65 6c 6c 73 20 68 6f 6c 64 20 6f  ent cells hold o
fd810 6e 6c 79 20 49 4e 54 4b 45 59 73 20 74 68 61 74  nly INTKEYs that
fd820 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 72 65 20  .        ** are 
fd830 64 75 70 6c 69 63 61 74 65 73 20 6f 66 20 6b 65  duplicates of ke
fd840 79 73 20 6f 6e 20 74 68 65 20 63 68 69 6c 64 20  ys on the child 
fd850 70 61 67 65 73 2e 20 20 57 65 20 6e 65 65 64 20  pages.  We need 
fd860 74 6f 20 72 65 6d 6f 76 65 0a 20 20 20 20 20 20  to remove.      
fd870 20 20 2a 2a 20 74 68 65 20 64 69 76 69 64 65 72    ** the divider
fd880 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 50 61 72   cells from pPar
fd890 65 6e 74 2c 20 62 75 74 20 74 68 65 20 64 69 76  ent, but the div
fd8a0 69 64 65 72 73 20 63 65 6c 6c 73 20 61 72 65 20  iders cells are 
fd8b0 6e 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  not.        ** a
fd8c0 64 64 65 64 20 74 6f 20 61 70 43 65 6c 6c 5b 5d  dded to apCell[]
fd8d0 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72   because they ar
fd8e0 65 20 64 75 70 6c 69 63 61 74 65 73 20 6f 66 20  e duplicates of 
fd8f0 63 68 69 6c 64 20 63 65 6c 6c 73 2e 0a 20 20 20  child cells..   
fd900 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
fd910 64 72 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e 74  dropCell(pParent
fd920 2c 20 6e 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20  , nxDiv, sz);.  
fd930 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
fd940 20 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20     u8 *pTemp;.  
fd950 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43        assert( nC
fd960 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ell<nMaxCells );
fd970 0a 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b  .        szCell[
fd980 6e 43 65 6c 6c 5d 20 3d 20 73 7a 3b 0a 20 20 20  nCell] = sz;.   
fd990 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 61 53       pTemp = &aS
fd9a0 70 61 63 65 5b 69 53 70 61 63 65 5d 3b 0a 20 20  pace[iSpace];.  
fd9b0 20 20 20 20 20 20 69 53 70 61 63 65 20 2b 3d 20        iSpace += 
fd9c0 73 7a 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  sz;.        asse
fd9d0 72 74 28 20 69 53 70 61 63 65 3c 3d 70 42 74 2d  rt( iSpace<=pBt-
fd9e0 3e 70 61 67 65 53 69 7a 65 2a 35 20 29 3b 0a 20  >pageSize*5 );. 
fd9f0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54         memcpy(pT
fda00 65 6d 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73  emp, apDiv[i], s
fda10 7a 29 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65  z);.        apCe
fda20 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d  ll[nCell] = pTem
fda30 70 2b 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  p+leafCorrection
fda40 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
fda50 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
fda60 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 42 74  .        if( pBt
fda70 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
fda80 20 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b            aFrom[
fda90 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20  nCell] = 0xFF;. 
fdaa0 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a         }.#endif.
fdab0 20 20 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c          dropCell
fdac0 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c  (pParent, nxDiv,
fdad0 20 73 7a 29 3b 0a 20 20 20 20 20 20 20 20 73 7a   sz);.        sz
fdae0 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 6c  Cell[nCell] -= l
fdaf0 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20  eafCorrection;. 
fdb00 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 67         assert( g
fdb10 65 74 34 62 79 74 65 28 70 54 65 6d 70 29 3d 3d  et4byte(pTemp)==
fdb20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 29 3b 0a 20 20  pgnoOld[i] );.  
fdb30 20 20 20 20 20 20 69 66 28 20 21 70 4f 6c 64 2d        if( !pOld-
fdb40 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20  >leaf ){.       
fdb50 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43     assert( leafC
fdb60 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a  orrection==0 );.
fdb70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
fdb80 20 72 69 67 68 74 20 70 6f 69 6e 74 65 72 20 6f   right pointer o
fdb90 66 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65  f the child page
fdba0 20 70 4f 6c 64 20 62 65 63 6f 6d 65 73 20 74 68   pOld becomes th
fdbb0 65 20 6c 65 66 74 0a 20 20 20 20 20 20 20 20 20  e left.         
fdbc0 20 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 74   ** pointer of t
fdbd0 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  he divider cell 
fdbe0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  */.          mem
fdbf0 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c  cpy(apCell[nCell
fdc00 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 74 61 5b  ], &pOld->aData[
fdc10 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b  pOld->hdrOffset+
fdc20 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20  8], 4);.        
fdc30 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
fdc40 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72   assert( leafCor
fdc50 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20  rection==4 );.  
fdc60 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65          if( szCe
fdc70 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20  ll[nCell]<4 ){. 
fdc80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
fdc90 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63   not allow any c
fdca0 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  ells smaller tha
fdcb0 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20  n 4 bytes. */.  
fdcc0 20 20 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c            szCell
fdcd0 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20  [nCell] = 4;.   
fdce0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
fdcf0 20 7d 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c   }.        nCell
fdd00 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
fdd10 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }.  }..  /*.  **
fdd20 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20   Figure out the 
fdd30 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
fdd40 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 61  needed to hold a
fdd50 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a  ll nCell cells..
fdd60 20 20 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 20    ** Store this 
fdd70 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 20  number in "k".  
fdd80 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e  Also compute szN
fdd90 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68  ew[] which is th
fdda0 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a  e total.  ** siz
fddb0 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f  e of all cells o
fddc0 6e 20 74 68 65 20 69 2d 74 68 20 70 61 67 65 20  n the i-th page 
fddd0 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69  and cntNew[] whi
fdde0 63 68 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a  ch is the index.
fddf0 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d    ** in apCell[]
fde00 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61   of the cell tha
fde10 74 20 64 69 76 69 64 65 73 20 70 61 67 65 20 69  t divides page i
fde20 20 66 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20   from page i+1. 
fde30 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d   .  ** cntNew[k]
fde40 20 73 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43   should equal nC
fde50 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56  ell..  **.  ** V
fde60 61 6c 75 65 73 20 63 6f 6d 70 75 74 65 64 20 62  alues computed b
fde70 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20  y this block:.  
fde80 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
fde90 20 20 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e    k: The total n
fdea0 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67  umber of sibling
fdeb0 20 70 61 67 65 73 0a 20 20 2a 2a 20 20 20 20 73   pages.  **    s
fdec0 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63 65 64 20  zNew[i]: Spaced 
fded0 75 73 65 64 20 6f 6e 20 74 68 65 20 69 2d 74 68  used on the i-th
fdee0 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20   sibling page.. 
fdef0 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a   **   cntNew[i]:
fdf00 20 49 6e 64 65 78 20 69 6e 20 61 70 43 65 6c 6c   Index in apCell
fdf10 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20  [] and szCell[] 
fdf20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 63 65  for the first ce
fdf30 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20  ll to.  **      
fdf40 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68          the righ
fdf50 74 20 6f 66 20 74 68 65 20 69 2d 74 68 20 73 69  t of the i-th si
fdf60 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a  bling page..  **
fdf70 20 75 73 61 62 6c 65 53 70 61 63 65 3a 20 4e 75   usableSpace: Nu
fdf80 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
fdf90 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65   space available
fdfa0 20 6f 6e 20 65 61 63 68 20 73 69 62 6c 69 6e 67   on each sibling
fdfb0 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75  ..  ** .  */.  u
fdfc0 73 61 62 6c 65 53 70 61 63 65 20 3d 20 70 42 74  sableSpace = pBt
fdfd0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 31  ->usableSize - 1
fdfe0 32 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69  2 + leafCorrecti
fdff0 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f 74  on;.  for(subtot
fe000 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c  al=k=i=0; i<nCel
fe010 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73  l; i++){.    ass
fe020 65 72 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73  ert( i<nMaxCells
fe030 20 29 3b 0a 20 20 20 20 73 75 62 74 6f 74 61 6c   );.    subtotal
fe040 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20   += szCell[i] + 
fe050 32 3b 0a 20 20 20 20 69 66 28 20 73 75 62 74 6f  2;.    if( subto
fe060 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 70 61 63  tal > usableSpac
fe070 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77  e ){.      szNew
fe080 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d  [k] = subtotal -
fe090 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20   szCell[i];.    
fe0a0 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b    cntNew[k] = i;
fe0b0 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61 66 44  .      if( leafD
fe0c0 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20  ata ){ i--; }.  
fe0d0 20 20 20 20 73 75 62 74 6f 74 61 6c 20 3d 20 30      subtotal = 0
fe0e0 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20  ;.      k++;.   
fe0f0 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b   }.  }.  szNew[k
fe100 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20  ] = subtotal;.  
fe110 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c  cntNew[k] = nCel
fe120 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a  l;.  k++;..  /*.
fe130 20 20 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67    ** The packing
fe140 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65   computed by the
fe150 20 70 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20   previous block 
fe160 69 73 20 62 69 61 73 65 64 20 74 6f 77 61 72 64  is biased toward
fe170 20 74 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20   the siblings.  
fe180 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73  ** on the left s
fe190 69 64 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73  ide.  The left s
fe1a0 69 62 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61  iblings are alwa
fe1b0 79 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20  ys nearly full, 
fe1c0 77 68 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72  while the.  ** r
fe1d0 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e  ight-most siblin
fe1e0 67 20 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c  g might be nearl
fe1f0 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62  y empty.  This b
fe200 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74  lock of code att
fe210 65 6d 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64  empts.  ** to ad
fe220 6a 75 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67  just the packing
fe230 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20   of siblings to 
fe240 67 65 74 20 61 20 62 65 74 74 65 72 20 62 61 6c  get a better bal
fe250 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ance..  **.  ** 
fe260 54 68 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20  This adjustment 
fe270 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20  is more than an 
fe280 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54  optimization.  T
fe290 68 65 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65  he packing above
fe2a0 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73   might.  ** be s
fe2b0 6f 20 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65  o out of balance
fe2c0 20 61 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61   as to be illega
fe2d0 6c 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  l.  For example,
fe2e0 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a   the right-most.
fe2f0 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67    ** sibling mig
fe300 68 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79  ht be completely
fe310 20 65 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64   empty.  This ad
fe320 6a 75 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20  justment is not 
fe330 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20  optional..  */. 
fe340 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b   for(i=k-1; i>0;
fe350 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73   i--){.    int s
fe360 7a 52 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69  zRight = szNew[i
fe370 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73  ];  /* Size of s
fe380 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69  ibling on the ri
fe390 67 68 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73  ght */.    int s
fe3a0 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d  zLeft = szNew[i-
fe3b0 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73  1]; /* Size of s
fe3c0 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65  ibling on the le
fe3d0 66 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b  ft */.    int r;
fe3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
fe3f0 20 49 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d   Index of right-
fe400 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66  most cell in lef
fe410 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20  t sibling */.   
fe420 20 69 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20   int d;         
fe430 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
fe440 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74   first cell to t
fe450 68 65 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74  he left of right
fe460 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20   sibling */..   
fe470 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d   r = cntNew[i-1]
fe480 20 2d 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20   - 1;.    d = r 
fe490 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a  + 1 - leafData;.
fe4a0 20 20 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d      assert( d<nM
fe4b0 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61  axCells );.    a
fe4c0 73 73 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c  ssert( r<nMaxCel
fe4d0 6c 73 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  ls );.    while(
fe4e0 20 73 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73   szRight==0 || s
fe4f0 7a 52 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d  zRight+szCell[d]
fe500 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65  +2<=szLeft-(szCe
fe510 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20  ll[r]+2) ){.    
fe520 20 20 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43    szRight += szC
fe530 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20  ell[d] + 2;.    
fe540 20 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65    szLeft -= szCe
fe550 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20  ll[r] + 2;.     
fe560 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a   cntNew[i-1]--;.
fe570 20 20 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77        r = cntNew
fe580 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20  [i-1] - 1;.     
fe590 20 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61   d = r + 1 - lea
fe5a0 66 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20  fData;.    }.   
fe5b0 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69   szNew[i] = szRi
fe5c0 67 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69  ght;.    szNew[i
fe5d0 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20  -1] = szLeft;.  
fe5e0 7d 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77  }..  /* Either w
fe5f0 65 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d  e found one or m
fe600 6f 72 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65  ore cells (cntne
fe610 77 5b 30 5d 29 3e 30 29 20 6f 72 20 77 65 20 61  w[0])>0) or we a
fe620 72 65 20 74 68 65 0a 20 20 2a 2a 20 61 20 76 69  re the.  ** a vi
fe630 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e  rtual root page.
fe640 20 20 41 20 76 69 72 74 75 61 6c 20 72 6f 6f 74    A virtual root
fe650 20 70 61 67 65 20 69 73 20 77 68 65 6e 20 74 68   page is when th
fe660 65 20 72 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a  e real root.  **
fe670 20 70 61 67 65 20 69 73 20 70 61 67 65 20 31 20   page is page 1 
fe680 61 6e 64 20 77 65 20 61 72 65 20 74 68 65 20 6f  and we are the o
fe690 6e 6c 79 20 63 68 69 6c 64 20 6f 66 20 74 68 61  nly child of tha
fe6a0 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61  t page..  */.  a
fe6b0 73 73 65 72 74 28 20 63 6e 74 4e 65 77 5b 30 5d  ssert( cntNew[0]
fe6c0 3e 30 20 7c 7c 20 28 70 50 61 72 65 6e 74 2d 3e  >0 || (pParent->
fe6d0 70 67 6e 6f 3d 3d 31 20 26 26 20 70 50 61 72 65  pgno==1 && pPare
fe6e0 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b  nt->nCell==0) );
fe6f0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f  ..  /*.  ** Allo
fe700 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73  cate k new pages
fe710 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67  .  Reuse old pag
fe720 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c  es where possibl
fe730 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
fe740 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3e 31 20  ( pPage->pgno>1 
fe750 29 3b 0a 20 20 70 61 67 65 46 6c 61 67 73 20 3d  );.  pageFlags =
fe760 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d   pPage->aData[0]
fe770 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b  ;.  for(i=0; i<k
fe780 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50  ; i++){.    MemP
fe790 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69  age *pNew;.    i
fe7a0 66 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20  f( i<nOld ){.   
fe7b0 20 20 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b     pNew = apNew[
fe7c0 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b 0a 20  i] = apOld[i];. 
fe7d0 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d 20       pgnoNew[i] 
fe7e0 3d 20 70 67 6e 6f 4f 6c 64 5b 69 5d 3b 0a 20 20  = pgnoOld[i];.  
fe7f0 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30      apOld[i] = 0
fe800 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
fe810 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
fe820 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  New->pDbPage);. 
fe830 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20       nNew++;.   
fe840 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
fe850 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
fe860 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
fe870 20 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29     assert( i>0 )
fe880 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c  ;.      rc = all
fe890 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
fe8a0 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f  Bt, &pNew, &pgno
fe8b0 4e 65 77 5b 69 5d 2c 20 70 67 6e 6f 4e 65 77 5b  New[i], pgnoNew[
fe8c0 69 2d 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20  i-1], 0);.      
fe8d0 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61  if( rc ) goto ba
fe8e0 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
fe8f0 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20       apNew[i] = 
fe900 70 4e 65 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77  pNew;.      nNew
fe910 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 65  ++;.    }.    ze
fe920 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 61 67  roPage(pNew, pag
fe930 65 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20  eFlags);.  }..  
fe940 2f 2a 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20  /* Free any old 
fe950 70 61 67 65 73 20 74 68 61 74 20 77 65 72 65 20  pages that were 
fe960 6e 6f 74 20 72 65 75 73 65 64 20 61 73 20 6e 65  not reused as ne
fe970 77 20 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20  w pages..  */.  
fe980 77 68 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b  while( i<nOld ){
fe990 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61  .    rc = freePa
fe9a0 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20  ge(apOld[i]);.  
fe9b0 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
fe9c0 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
fe9d0 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
fe9e0 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20  (apOld[i]);.    
fe9f0 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20  apOld[i] = 0;.  
fea00 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a    i++;.  }..  /*
fea10 0a 20 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65  .  ** Put the ne
fea20 77 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e  w pages in accen
fea30 64 69 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69  ding order.  Thi
fea40 73 20 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20  s helps to.  ** 
fea50 6b 65 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20  keep entries in 
fea60 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e  the disk file in
fea70 20 6f 72 64 65 72 20 73 6f 20 74 68 61 74 20 61   order so that a
fea80 20 73 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68   scan.  ** of th
fea90 65 20 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e  e table is a lin
feaa0 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68  ear scan through
feab0 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74   the file.  That
feac0 0a 20 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65  .  ** in turn he
fead0 6c 70 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e  lps the operatin
feae0 67 20 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69  g system to deli
feaf0 76 65 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66  ver pages.  ** f
feb00 72 6f 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72  rom the disk mor
feb10 65 20 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a  e rapidly..  **.
feb20 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69    ** An O(n^2) i
feb30 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c  nsertion sort al
feb40 67 6f 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c  gorithm is used,
feb50 20 62 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20   but since.  ** 
feb60 6e 20 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20  n is never more 
feb70 74 68 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c  than NB (a small
feb80 20 63 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74   constant), that
feb90 20 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74   should.  ** not
feba0 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20   be a problem.. 
febb0 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42   **.  ** When NB
febc0 3d 3d 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70  ==3, this one op
febd0 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73  timization makes
febe0 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20   the database.  
febf0 2a 2a 20 61 62 6f 75 74 20 32 35 25 20 66 61 73  ** about 25% fas
fec00 74 65 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e  ter for large in
fec10 73 65 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c  sertions and del
fec20 65 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66  etions..  */.  f
fec30 6f 72 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69  or(i=0; i<k-1; i
fec40 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e  ++){.    int min
fec50 56 20 3d 20 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a  V = pgnoNew[i];.
fec60 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69      int minI = i
fec70 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b  ;.    for(j=i+1;
fec80 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<k; j++){.    
fec90 20 20 69 66 28 20 70 67 6e 6f 4e 65 77 5b 6a 5d    if( pgnoNew[j]
feca0 3c 28 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20  <(unsigned)minV 
fecb0 29 7b 0a 20 20 20 20 20 20 20 20 6d 69 6e 49 20  ){.        minI 
fecc0 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e  = j;.        min
fecd0 56 20 3d 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3b 0a  V = pgnoNew[j];.
fece0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
fecf0 20 20 69 66 28 20 6d 69 6e 49 3e 69 20 29 7b 0a    if( minI>i ){.
fed00 20 20 20 20 20 20 69 6e 74 20 74 3b 0a 20 20 20        int t;.   
fed10 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 3b 0a     MemPage *pT;.
fed20 20 20 20 20 20 20 74 20 3d 20 70 67 6e 6f 4e 65        t = pgnoNe
fed30 77 5b 69 5d 3b 0a 20 20 20 20 20 20 70 54 20 3d  w[i];.      pT =
fed40 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20   apNew[i];.     
fed50 20 70 67 6e 6f 4e 65 77 5b 69 5d 20 3d 20 70 67   pgnoNew[i] = pg
fed60 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20  noNew[minI];.   
fed70 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70     apNew[i] = ap
fed80 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20  New[minI];.     
fed90 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 20 3d   pgnoNew[minI] =
feda0 20 74 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b   t;.      apNew[
fedb0 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20  minI] = pT;.    
fedc0 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 28 28 22  }.  }.  TRACE(("
fedd0 42 41 4c 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64  BALANCE: old: %d
fede0 20 25 64 20 25 64 20 20 6e 65 77 3a 20 25 64 28   %d %d  new: %d(
fedf0 25 64 29 20 25 64 28 25 64 29 20 25 64 28 25 64  %d) %d(%d) %d(%d
fee00 29 20 25 64 28 25 64 29 20 25 64 28 25 64 29 5c  ) %d(%d) %d(%d)\
fee10 6e 22 2c 0a 20 20 20 20 70 67 6e 6f 4f 6c 64 5b  n",.    pgnoOld[
fee20 30 5d 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32  0], .    nOld>=2
fee30 20 3f 20 70 67 6e 6f 4f 6c 64 5b 31 5d 20 3a 20   ? pgnoOld[1] : 
fee40 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f  0,.    nOld>=3 ?
fee50 20 70 67 6e 6f 4f 6c 64 5b 32 5d 20 3a 20 30 2c   pgnoOld[2] : 0,
fee60 0a 20 20 20 20 70 67 6e 6f 4e 65 77 5b 30 5d 2c  .    pgnoNew[0],
fee70 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e   szNew[0],.    n
fee80 4e 65 77 3e 3d 32 20 3f 20 70 67 6e 6f 4e 65 77  New>=2 ? pgnoNew
fee90 5b 31 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 32  [1] : 0, nNew>=2
feea0 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 3a 20 30 2c   ? szNew[1] : 0,
feeb0 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 20 3f 20 70  .    nNew>=3 ? p
feec0 67 6e 6f 4e 65 77 5b 32 5d 20 3a 20 30 2c 20 6e  gnoNew[2] : 0, n
feed0 4e 65 77 3e 3d 33 20 3f 20 73 7a 4e 65 77 5b 32  New>=3 ? szNew[2
feee0 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e  ] : 0,.    nNew>
feef0 3d 34 20 3f 20 70 67 6e 6f 4e 65 77 5b 33 5d 20  =4 ? pgnoNew[3] 
fef00 3a 20 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73  : 0, nNew>=4 ? s
fef10 7a 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20  zNew[3] : 0,.   
fef20 20 6e 4e 65 77 3e 3d 35 20 3f 20 70 67 6e 6f 4e   nNew>=5 ? pgnoN
fef30 65 77 5b 34 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e  ew[4] : 0, nNew>
fef40 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20  =5 ? szNew[4] : 
fef50 30 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  0));..  /*.  ** 
fef60 45 76 65 6e 6c 79 20 64 69 73 74 72 69 62 75 74  Evenly distribut
fef70 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 61 70  e the data in ap
fef80 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68  Cell[] across th
fef90 65 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a  e new pages..  *
fefa0 2a 20 49 6e 73 65 72 74 20 64 69 76 69 64 65 72  * Insert divider
fefb0 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72   cells into pPar
fefc0 65 6e 74 20 61 73 20 6e 65 63 65 73 73 61 72 79  ent as necessary
fefd0 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a  ..  */.  j = 0;.
fefe0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65    for(i=0; i<nNe
feff0 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20  w; i++){.    /* 
ff000 41 73 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 77  Assemble the new
ff010 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a   sibling page. *
ff020 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  /.    MemPage *p
ff030 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a  New = apNew[i];.
ff040 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d      assert( j<nM
ff050 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61  axCells );.    a
ff060 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 67 6e  ssert( pNew->pgn
ff070 6f 3d 3d 70 67 6e 6f 4e 65 77 5b 69 5d 20 29 3b  o==pgnoNew[i] );
ff080 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67  .    assemblePag
ff090 65 28 70 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69  e(pNew, cntNew[i
ff0a0 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c  ]-j, &apCell[j],
ff0b0 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20   &szCell[j]);.  
ff0c0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e    assert( pNew->
ff0d0 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77  nCell>0 || (nNew
ff0e0 3d 3d 31 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d  ==1 && cntNew[0]
ff0f0 3d 3d 30 29 20 29 3b 0a 20 20 20 20 61 73 73 65  ==0) );.    asse
ff100 72 74 28 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66  rt( pNew->nOverf
ff110 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 23 69 66 6e 64  low==0 );..#ifnd
ff120 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
ff130 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 2f 2a  UTOVACUUM.    /*
ff140 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61   If this is an a
ff150 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
ff160 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20  ase, update the 
ff170 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72  pointer map entr
ff180 69 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  ies.    ** that 
ff190 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 69 62  point to the sib
ff1a0 6c 69 6e 67 73 20 74 68 61 74 20 77 65 72 65 20  lings that were 
ff1b0 72 65 61 72 72 61 6e 67 65 64 2e 20 54 68 65 73  rearranged. Thes
ff1c0 65 20 63 61 6e 20 62 65 3a 20 6c 65 66 74 0a 20  e can be: left. 
ff1d0 20 20 20 2a 2a 20 63 68 69 6c 64 72 65 6e 20 6f     ** children o
ff1e0 66 20 63 65 6c 6c 73 2c 20 74 68 65 20 72 69 67  f cells, the rig
ff1f0 68 74 2d 63 68 69 6c 64 20 6f 66 20 74 68 65 20  ht-child of the 
ff200 70 61 67 65 2c 20 6f 72 20 6f 76 65 72 66 6c 6f  page, or overflo
ff210 77 20 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 70  w pages.    ** p
ff220 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 63 65 6c  ointed to by cel
ff230 6c 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ls..    */.    i
ff240 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
ff250 75 6d 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  um ){.      for(
ff260 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b 69 5d  k=j; k<cntNew[i]
ff270 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; k++){.        
ff280 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 78 43 65  assert( k<nMaxCe
ff290 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 69  lls );.        i
ff2a0 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30 78 46  f( aFrom[k]==0xF
ff2b0 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 46 72 6f  F || apCopy[aFro
ff2c0 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65  m[k]]->pgno!=pNe
ff2d0 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20  w->pgno ){.     
ff2e0 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
ff2f0 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c 20 6b 2d  PutOvfl(pNew, k-
ff300 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  j);.          if
ff310 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
ff320 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  ){.            g
ff330 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
ff340 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  nup;.          }
ff350 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
ff360 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
ff370 0a 20 20 20 20 6a 20 3d 20 63 6e 74 4e 65 77 5b  .    j = cntNew[
ff380 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  i];..    /* If t
ff390 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20  he sibling page 
ff3a0 61 73 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20  assembled above 
ff3b0 77 61 73 20 6e 6f 74 20 74 68 65 20 72 69 67 68  was not the righ
ff3c0 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 2c 0a  t-most sibling,.
ff3d0 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 20 61 20      ** insert a 
ff3e0 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74  divider cell int
ff3f0 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  o the parent pag
ff400 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
ff410 28 20 69 3c 6e 4e 65 77 2d 31 20 26 26 20 6a 3c  ( i<nNew-1 && j<
ff420 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75  nCell ){.      u
ff430 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20  8 *pCell;.      
ff440 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20  u8 *pTemp;.     
ff450 20 69 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20   int sz;..      
ff460 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65  assert( j<nMaxCe
ff470 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65  lls );.      pCe
ff480 6c 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a  ll = apCell[j];.
ff490 20 20 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c        sz = szCel
ff4a0 6c 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65  l[j] + leafCorre
ff4b0 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28  ction;.      if(
ff4c0 20 21 70 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a   !pNew->leaf ){.
ff4d0 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
ff4e0 70 4e 65 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20  pNew->aData[8], 
ff4f0 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20  pCell, 4);.     
ff500 20 20 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20     pTemp = 0;.  
ff510 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65      }else if( le
ff520 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20  afData ){.      
ff530 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65    /* If the tree
ff540 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20   is a leaf-data 
ff550 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69  tree, and the si
ff560 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65  blings are leave
ff570 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  s, .        ** t
ff580 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hen there is no 
ff590 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20  divider cell in 
ff5a0 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61  apCell[]. Instea
ff5b0 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0a  d, the divider .
ff5c0 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20          ** cell 
ff5d0 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
ff5e0 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20  integer key for 
ff5f0 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63  the right-most c
ff600 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20  ell of .        
ff610 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70  ** the sibling-p
ff620 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62  age assembled ab
ff630 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20  ove only..      
ff640 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c    */.        Cel
ff650 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20  lInfo info;.    
ff660 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20      j--;.       
ff670 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
ff680 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c 20  seCellPtr(pNew, 
ff690 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f  apCell[j], &info
ff6a0 29 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c  );.        pCell
ff6b0 20 3d 20 26 61 53 70 61 63 65 5b 69 53 70 61 63   = &aSpace[iSpac
ff6c0 65 5d 3b 0a 20 20 20 20 20 20 20 20 66 69 6c 6c  e];.        fill
ff6d0 49 6e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  InCell(pParent, 
ff6e0 70 43 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e  pCell, 0, info.n
ff6f0 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26 73  Key, 0, 0, 0, &s
ff700 7a 29 3b 0a 20 20 20 20 20 20 20 20 69 53 70 61  z);.        iSpa
ff710 63 65 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20  ce += sz;.      
ff720 20 20 61 73 73 65 72 74 28 20 69 53 70 61 63 65    assert( iSpace
ff730 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2a  <=pBt->pageSize*
ff740 35 20 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65  5 );.        pTe
ff750 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  mp = 0;.      }e
ff760 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65  lse{.        pCe
ff770 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20  ll -= 4;.       
ff780 20 70 54 65 6d 70 20 3d 20 26 61 53 70 61 63 65   pTemp = &aSpace
ff790 5b 69 53 70 61 63 65 5d 3b 0a 20 20 20 20 20 20  [iSpace];.      
ff7a0 20 20 69 53 70 61 63 65 20 2b 3d 20 73 7a 3b 0a    iSpace += sz;.
ff7b0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
ff7c0 69 53 70 61 63 65 3c 3d 70 42 74 2d 3e 70 61 67  iSpace<=pBt->pag
ff7d0 65 53 69 7a 65 2a 35 20 29 3b 0a 20 20 20 20 20  eSize*5 );.     
ff7e0 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61     /* Obscure ca
ff7f0 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d  se for non-leaf-
ff800 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 74  data trees: If t
ff810 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c  he cell at pCell
ff820 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20   was.        ** 
ff830 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65  previously store
ff840 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65  d on a leaf node
ff850 2c 20 61 6e 64 20 69 74 27 73 20 72 65 70 6f 72  , and it's repor
ff860 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20  ted size was 4. 
ff870 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c         ** bytes,
ff880 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74   then it may act
ff890 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72  ually be smaller
ff8a0 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20   than this .    
ff8b0 20 20 20 20 2a 2a 20 28 73 65 65 20 73 71 6c 69      ** (see sqli
ff8c0 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
ff8d0 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65 73 20  lPtr(), 4 bytes 
ff8e0 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 73  is the minimum s
ff8f0 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20 2a  ize of.        *
ff900 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75 74  * any cell). But
ff910 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20   it's important 
ff920 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f 72 72  to pass the corr
ff930 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20 20 20  ect size to .   
ff940 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 43 65       ** insertCe
ff950 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72 73 65  ll(), so reparse
ff960 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a 20   the cell now.. 
ff970 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
ff980 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74    ** Note that t
ff990 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20 68 61  his can never ha
ff9a0 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c 69 74  ppen in an SQLit
ff9b0 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61 73 20  e data file, as 
ff9c0 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  all.        ** c
ff9d0 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65 61 73  ells are at leas
ff9e0 74 20 34 20 62 79 74 65 73 2e 20 49 74 20 6f 6e  t 4 bytes. It on
ff9f0 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20 62 2d  ly happens in b-
ffa00 74 72 65 65 73 20 75 73 65 64 0a 20 20 20 20 20  trees used.     
ffa10 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75 61 74     ** to evaluat
ffa20 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e  e "IN (SELECT ..
ffa30 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61 72 20  .)" and similar 
ffa40 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20 20 20  clauses..       
ffa50 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
ffa60 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0a  szCell[j]==4 ){.
ffa70 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
ffa80 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d  (leafCorrection=
ffa90 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  =4);.          s
ffaa0 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  z = cellSizePtr(
ffab0 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b  pParent, pCell);
ffac0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
ffad0 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e   }.      rc = in
ffae0 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74  sertCell(pParent
ffaf0 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20  , nxDiv, pCell, 
ffb00 73 7a 2c 20 70 54 65 6d 70 2c 20 34 29 3b 0a 20  sz, pTemp, 4);. 
ffb10 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
ffb20 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61  ITE_OK ) goto ba
ffb30 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
ffb40 20 20 20 20 20 70 75 74 34 62 79 74 65 28 66 69       put4byte(fi
ffb50 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70  ndOverflowCell(p
ffb60 50 61 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70  Parent,nxDiv), p
ffb70 4e 65 77 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e  New->pgno);.#ifn
ffb80 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
ffb90 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
ffba0 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
ffbb0 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61  n auto-vacuum da
ffbc0 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74 20  tabase, and not 
ffbd0 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65  a leaf-data tree
ffbe0 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20  ,.      ** then 
ffbf0 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74  update the point
ffc00 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20 65  er map with an e
ffc10 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65  ntry for the ove
ffc20 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20  rflow page.     
ffc30 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65 6c   ** that the cel
ffc40 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20  l just inserted 
ffc50 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61 6e  points to (if an
ffc60 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  y)..      */.   
ffc70 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
ffc80 56 61 63 75 75 6d 20 26 26 20 21 6c 65 61 66 44  Vacuum && !leafD
ffc90 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ata ){.        r
ffca0 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66  c = ptrmapPutOvf
ffcb0 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76  l(pParent, nxDiv
ffcc0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
ffcd0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
ffce0 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62            goto b
ffcf0 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
ffd00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
ffd10 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6a  }.#endif.      j
ffd20 2b 2b 3b 0a 20 20 20 20 20 20 6e 78 44 69 76 2b  ++;.      nxDiv+
ffd30 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  +;.    }.  }.  a
ffd40 73 73 65 72 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20  ssert( j==nCell 
ffd50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 6c  );.  assert( nOl
ffd60 64 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  d>0 );.  assert(
ffd70 20 6e 4e 65 77 3e 30 20 29 3b 0a 20 20 69 66 28   nNew>0 );.  if(
ffd80 20 28 70 61 67 65 46 6c 61 67 73 20 26 20 50 54   (pageFlags & PT
ffd90 46 5f 4c 45 41 46 29 3d 3d 30 20 29 7b 0a 20 20  F_LEAF)==0 ){.  
ffda0 20 20 6d 65 6d 63 70 79 28 26 61 70 4e 65 77 5b    memcpy(&apNew[
ffdb0 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b 38  nNew-1]->aData[8
ffdc0 5d 2c 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d  ], &apCopy[nOld-
ffdd0 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 34 29  1]->aData[8], 4)
ffde0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 78 44 69  ;.  }.  if( nxDi
ffdf0 76 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c  v==pParent->nCel
ffe00 6c 2b 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72  l+pParent->nOver
ffe10 66 6c 6f 77 20 29 7b 0a 20 20 20 20 2f 2a 20 52  flow ){.    /* R
ffe20 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e  ight-most siblin
ffe30 67 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d  g is the right-m
ffe40 6f 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50 61  ost child of pPa
ffe50 72 65 6e 74 20 2a 2f 0a 20 20 20 20 70 75 74 34  rent */.    put4
ffe60 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61  byte(&pParent->a
ffe70 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64  Data[pParent->hd
ffe80 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f  rOffset+8], pgno
ffe90 4e 65 77 5b 6e 4e 65 77 2d 31 5d 29 3b 0a 20 20  New[nNew-1]);.  
ffea0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 52 69  }else{.    /* Ri
ffeb0 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67  ght-most sibling
ffec0 20 69 73 20 74 68 65 20 6c 65 66 74 20 63 68 69   is the left chi
ffed0 6c 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ld of the first 
ffee0 65 6e 74 72 79 20 69 6e 20 70 50 61 72 65 6e 74  entry in pParent
ffef0 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65  .    ** past the
fff00 20 72 69 67 68 74 2d 6d 6f 73 74 20 64 69 76 69   right-most divi
fff10 64 65 72 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20  der entry */.   
fff20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76   put4byte(findOv
fff30 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72 65  erflowCell(pPare
fff40 6e 74 2c 20 6e 78 44 69 76 29 2c 20 70 67 6e 6f  nt, nxDiv), pgno
fff50 4e 65 77 5b 6e 4e 65 77 2d 31 5d 29 3b 0a 20 20  New[nNew-1]);.  
fff60 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 70  }..  /*.  ** Rep
fff70 61 72 65 6e 74 20 63 68 69 6c 64 72 65 6e 20 6f  arent children o
fff80 66 20 61 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a  f all cells..  *
fff90 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  /.  for(i=0; i<n
fffa0 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72  New; i++){.    r
fffb0 63 20 3d 20 72 65 70 61 72 65 6e 74 43 68 69 6c  c = reparentChil
fffc0 64 50 61 67 65 73 28 61 70 4e 65 77 5b 69 5d 29  dPages(apNew[i])
fffd0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
fffe0 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62  LITE_OK ) goto b
ffff0 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
10000 20 20 7d 0a 20 20 72 63 20 3d 20 72 65 70 61 72    }.  rc = repar
10001 65 6e 74 43 68 69 6c 64 50 61 67 65 73 28 70 50  entChildPages(pP
10002 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63  arent);.  if( rc
10003 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
10004 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
10005 75 70 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 42  up;..  /*.  ** B
10006 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e  alance the paren
10007 74 20 70 61 67 65 2e 20 20 4e 6f 74 65 20 74 68  t page.  Note th
10008 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  at the current p
10009 61 67 65 20 28 70 50 61 67 65 29 20 6d 69 67 68  age (pPage) migh
1000a 74 0a 20 20 2a 2a 20 68 61 76 65 20 62 65 65 6e  t.  ** have been
1000b 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72   added to the fr
1000c 65 65 6c 69 73 74 20 73 6f 20 69 74 20 6d 69 67  eelist so it mig
1000d 68 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20  ht no longer be 
1000e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a  initialized..  *
1000f 2a 20 42 75 74 20 74 68 65 20 70 61 72 65 6e 74  * But the parent
10010 20 70 61 67 65 20 77 69 6c 6c 20 61 6c 77 61 79   page will alway
10011 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  s be initialized
10012 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
10013 20 70 50 61 72 65 6e 74 2d 3e 69 73 49 6e 69 74   pParent->isInit
10014 20 29 3b 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e   );.  rc = balan
10015 63 65 28 70 50 61 72 65 6e 74 2c 20 30 29 3b 0a  ce(pParent, 0);.
10016 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65    .  /*.  ** Cle
10017 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74 75  anup before retu
10018 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61  rning..  */.bala
10019 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73  nce_cleanup:.  s
1001a 71 6c 69 74 65 33 5f 66 72 65 65 28 61 70 43 65  qlite3_free(apCe
1001b 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ll);.  for(i=0; 
1001c 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20  i<nOld; i++){.  
1001d 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70    releasePage(ap
1001e 4f 6c 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 66  Old[i]);.  }.  f
1001f 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20  or(i=0; i<nNew; 
10020 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73  i++){.    releas
10021 65 50 61 67 65 28 61 70 4e 65 77 5b 69 5d 29 3b  ePage(apNew[i]);
10022 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61  .  }.  releasePa
10023 67 65 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 54  ge(pParent);.  T
10024 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20  RACE(("BALANCE: 
10025 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 25 64  finished with %d
10026 3a 20 6f 6c 64 3d 25 64 20 6e 65 77 3d 25 64 20  : old=%d new=%d 
10027 63 65 6c 6c 73 3d 25 64 5c 6e 22 2c 0a 20 20 20  cells=%d\n",.   
10028 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 67         pPage->pg
10029 6e 6f 2c 20 6e 4f 6c 64 2c 20 6e 4e 65 77 2c 20  no, nOld, nNew, 
1002a 6e 43 65 6c 6c 29 29 3b 0a 20 20 72 65 74 75 72  nCell));.  retur
1002b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
1002c 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
1002d 61 6c 6c 65 64 20 66 6f 72 20 74 68 65 20 72 6f  alled for the ro
1002e 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 74 72  ot page of a btr
1002f 65 65 20 77 68 65 6e 20 74 68 65 20 72 6f 6f 74  ee when the root
10030 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 6e  .** page contain
10031 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 20 54 68 69  s no cells.  Thi
10032 73 20 69 73 20 61 6e 20 6f 70 70 6f 72 74 75 6e  s is an opportun
10033 69 74 79 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  ity to make the 
10034 74 72 65 65 0a 2a 2a 20 73 68 61 6c 6c 6f 77 65  tree.** shallowe
10035 72 20 62 79 20 6f 6e 65 20 6c 65 76 65 6c 2e 0a  r by one level..
10036 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61  */.static int ba
10037 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f 77 65 72 28  lance_shallower(
10038 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b  MemPage *pPage){
10039 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69  .  MemPage *pChi
1003a 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ld;             
1003b 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 69 6c  /* The only chil
1003c 64 20 70 61 67 65 20 6f 66 20 70 50 61 67 65 20  d page of pPage 
1003d 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68  */.  Pgno pgnoCh
1003e 69 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ild;            
1003f 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
10040 20 66 6f 72 20 70 43 68 69 6c 64 20 2a 2f 0a 20   for pChild */. 
10041 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
10042 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  _OK;          /*
10043 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   Return code fro
10044 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20  m subprocedures 
10045 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
10046 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Bt;             
10047 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e       /* The main
10048 20 42 54 72 65 65 20 73 74 72 75 63 74 75 72 65   BTree structure
10049 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 65 6c 6c   */.  int mxCell
1004a 50 65 72 50 61 67 65 3b 20 20 20 20 20 20 20 20  PerPage;        
1004b 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75     /* Maximum nu
1004c 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 70 65  mber of cells pe
1004d 72 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  r page */.  u8 *
1004e 2a 61 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  *apCell;        
1004f 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20           /* All 
10050 63 65 6c 6c 73 20 66 72 6f 6d 20 70 61 67 65 73  cells from pages
10051 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20   being balanced 
10052 2a 2f 0a 20 20 69 6e 74 20 2a 73 7a 43 65 6c 6c  */.  int *szCell
10053 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10054 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20    /* Local size 
10055 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a  of all cells */.
10056 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
10057 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29 3b 0a  ->pParent==0 );.
10058 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
10059 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 61  >nCell==0 );.  a
1005a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1005b 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
1005c 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
1005d 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70    pBt = pPage->p
1005e 42 74 3b 0a 20 20 6d 78 43 65 6c 6c 50 65 72 50  Bt;.  mxCellPerP
1005f 61 67 65 20 3d 20 4d 58 5f 43 45 4c 4c 28 70 42  age = MX_CELL(pB
10060 74 29 3b 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73  t);.  apCell = s
10061 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6d  qlite3_malloc( m
10062 78 43 65 6c 6c 50 65 72 50 61 67 65 2a 28 73 69  xCellPerPage*(si
10063 7a 65 6f 66 28 75 38 2a 29 2b 73 69 7a 65 6f 66  zeof(u8*)+sizeof
10064 28 69 6e 74 29 29 20 29 3b 0a 20 20 69 66 28 20  (int)) );.  if( 
10065 61 70 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75  apCell==0 ) retu
10066 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
10067 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28 69 6e 74  .  szCell = (int
10068 2a 29 26 61 70 43 65 6c 6c 5b 6d 78 43 65 6c 6c  *)&apCell[mxCell
10069 50 65 72 50 61 67 65 5d 3b 0a 20 20 69 66 28 20  PerPage];.  if( 
1006a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
1006b 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
1006c 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d  is completely em
1006d 70 74 79 20 2a 2f 0a 20 20 20 20 54 52 41 43 45  pty */.    TRACE
1006e 28 28 22 42 41 4c 41 4e 43 45 3a 20 65 6d 70 74  (("BALANCE: empt
1006f 79 20 74 61 62 6c 65 20 25 64 5c 6e 22 2c 20 70  y table %d\n", p
10070 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20  Page->pgno));.  
10071 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
10072 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65  e root page is e
10073 6d 70 74 79 20 62 75 74 20 68 61 73 20 6f 6e 65  mpty but has one
10074 20 63 68 69 6c 64 2e 20 20 54 72 61 6e 73 66 65   child.  Transfe
10075 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 66  r the.    ** inf
10076 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
10077 61 74 20 6f 6e 65 20 63 68 69 6c 64 20 69 6e 74  at one child int
10078 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  o the root page 
10079 69 66 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 69  if it .    ** wi
1007a 6c 6c 20 66 69 74 2e 20 20 54 68 69 73 20 72 65  ll fit.  This re
1007b 64 75 63 65 73 20 74 68 65 20 64 65 70 74 68 20  duces the depth 
1007c 6f 66 20 74 68 65 20 74 72 65 65 20 62 79 20 6f  of the tree by o
1007d 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ne..    **.    *
1007e 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 20 70 61  * If the root pa
1007f 67 65 20 69 73 20 70 61 67 65 20 31 2c 20 69 74  ge is page 1, it
10080 20 68 61 73 20 6c 65 73 73 20 73 70 61 63 65 20   has less space 
10081 61 76 61 69 6c 61 62 6c 65 20 74 68 61 6e 0a 20  available than. 
10082 20 20 20 2a 2a 20 69 74 73 20 63 68 69 6c 64 20     ** its child 
10083 28 64 75 65 20 74 6f 20 74 68 65 20 31 30 30 20  (due to the 100 
10084 62 79 74 65 20 68 65 61 64 65 72 20 74 68 61 74  byte header that
10085 20 6f 63 63 75 72 73 20 61 74 20 74 68 65 20 62   occurs at the b
10086 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20  eginning.    ** 
10087 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
10088 66 6c 65 29 2c 20 73 6f 20 69 74 20 6d 69 67 68  fle), so it migh
10089 74 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f  t not be able to
1008a 20 68 6f 6c 64 20 61 6c 6c 20 6f 66 20 74 68 65   hold all of the
1008b 20 0a 20 20 20 20 2a 2a 20 69 6e 66 6f 72 6d 61   .    ** informa
1008c 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 63  tion currently c
1008d 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
1008e 63 68 69 6c 64 2e 20 20 49 66 20 74 68 69 73 20  child.  If this 
1008f 69 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  is the .    ** c
10090 61 73 65 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  ase, then do not
10091 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66 65 72   do the transfer
10092 2e 20 20 4c 65 61 76 65 20 70 61 67 65 20 31 20  .  Leave page 1 
10093 65 6d 70 74 79 20 65 78 63 65 70 74 0a 20 20 20  empty except.   
10094 20 2a 2a 20 66 6f 72 20 74 68 65 20 72 69 67 68   ** for the righ
10095 74 2d 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  t-pointer to the
10096 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68   child page.  Th
10097 65 20 63 68 69 6c 64 20 70 61 67 65 20 62 65 63  e child page bec
10098 6f 6d 65 73 0a 20 20 20 20 2a 2a 20 74 68 65 20  omes.    ** the 
10099 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 6f 66 20  virtual root of 
1009a 74 68 65 20 74 72 65 65 2e 0a 20 20 20 20 2a 2f  the tree..    */
1009b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d  .    pgnoChild =
1009c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
1009d 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
1009e 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
1009f 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68    assert( pgnoCh
100a0 69 6c 64 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ild>0 );.    ass
100a1 65 72 74 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d  ert( pgnoChild<=
100a2 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
100a3 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 42 74  count(pPage->pBt
100a4 2d 3e 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20  ->pPager) );.   
100a5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
100a6 65 65 47 65 74 50 61 67 65 28 70 50 61 67 65 2d  eeGetPage(pPage-
100a7 3e 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c  >pBt, pgnoChild,
100a8 20 26 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20   &pChild, 0);.  
100a9 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
100aa 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61  end_shallow_bala
100ab 6e 63 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61  nce;.    if( pPa
100ac 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20  ge->pgno==1 ){. 
100ad 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
100ae 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70  3BtreeInitPage(p
100af 43 68 69 6c 64 2c 20 70 50 61 67 65 29 3b 0a 20  Child, pPage);. 
100b0 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
100b1 74 6f 20 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62  to end_shallow_b
100b2 61 6c 61 6e 63 65 3b 0a 20 20 20 20 20 20 61 73  alance;.      as
100b3 73 65 72 74 28 20 70 43 68 69 6c 64 2d 3e 6e 4f  sert( pChild->nO
100b4 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20  verflow==0 );.  
100b5 20 20 20 20 69 66 28 20 70 43 68 69 6c 64 2d 3e      if( pChild->
100b6 6e 46 72 65 65 3e 3d 31 30 30 20 29 7b 0a 20 20  nFree>=100 ){.  
100b7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 68 69        /* The chi
100b8 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77  ld information w
100b9 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 72  ill fit on the r
100ba 6f 6f 74 20 70 61 67 65 2c 20 73 6f 20 64 6f 20  oot page, so do 
100bb 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  the.        ** c
100bc 6f 70 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  opy */.        i
100bd 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 7a 65  nt i;.        ze
100be 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 70 43  roPage(pPage, pC
100bf 68 69 6c 64 2d 3e 61 44 61 74 61 5b 30 5d 29 3b  hild->aData[0]);
100c0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
100c1 3b 20 69 3c 70 43 68 69 6c 64 2d 3e 6e 43 65 6c  ; i<pChild->nCel
100c2 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; i++){.       
100c3 20 20 20 61 70 43 65 6c 6c 5b 69 5d 20 3d 20 66     apCell[i] = f
100c4 69 6e 64 43 65 6c 6c 28 70 43 68 69 6c 64 2c 69  indCell(pChild,i
100c5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 43  );.          szC
100c6 65 6c 6c 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a  ell[i] = cellSiz
100c7 65 50 74 72 28 70 43 68 69 6c 64 2c 20 61 70 43  ePtr(pChild, apC
100c8 65 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20  ell[i]);.       
100c9 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 6d   }.        assem
100ca 62 6c 65 50 61 67 65 28 70 50 61 67 65 2c 20 70  blePage(pPage, p
100cb 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 2c 20 61 70  Child->nCell, ap
100cc 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 29 3b 0a 20  Cell, szCell);. 
100cd 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74         /* Copy t
100ce 68 65 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72  he right-pointer
100cf 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 74 6f   of the child to
100d0 20 74 68 65 20 70 61 72 65 6e 74 2e 20 2a 2f 0a   the parent. */.
100d1 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
100d2 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
100d3 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
100d4 38 5d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  8], .           
100d5 20 67 65 74 34 62 79 74 65 28 26 70 43 68 69 6c   get4byte(&pChil
100d6 64 2d 3e 61 44 61 74 61 5b 70 43 68 69 6c 64 2d  d->aData[pChild-
100d7 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b  >hdrOffset+8]));
100d8 0a 20 20 20 20 20 20 20 20 66 72 65 65 50 61 67  .        freePag
100d9 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20  e(pChild);.     
100da 20 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e     TRACE(("BALAN
100db 43 45 3a 20 63 68 69 6c 64 20 25 64 20 74 72 61  CE: child %d tra
100dc 6e 73 66 65 72 20 74 6f 20 70 61 67 65 20 31 5c  nsfer to page 1\
100dd 6e 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f  n", pChild->pgno
100de 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ));.      }else{
100df 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
100e0 63 68 69 6c 64 20 68 61 73 20 6d 6f 72 65 20 69  child has more i
100e1 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20  nformation that 
100e2 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 20  will fit on the 
100e3 72 6f 6f 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a  root..        **
100e4 20 54 68 65 20 74 72 65 65 20 69 73 20 61 6c 72   The tree is alr
100e5 65 61 64 79 20 62 61 6c 61 6e 63 65 64 2e 20 20  eady balanced.  
100e6 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 2a 2f 0a 20  Do nothing. */. 
100e7 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 42         TRACE(("B
100e8 41 4c 41 4e 43 45 3a 20 63 68 69 6c 64 20 25 64  ALANCE: child %d
100e9 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e   will not fit on
100ea 20 70 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 69   page 1\n", pChi
100eb 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  ld->pgno));.    
100ec 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
100ed 20 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67       memcpy(pPag
100ee 65 2d 3e 61 44 61 74 61 2c 20 70 43 68 69 6c 64  e->aData, pChild
100ef 2d 3e 61 44 61 74 61 2c 20 70 50 61 67 65 2d 3e  ->aData, pPage->
100f0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29  pBt->usableSize)
100f1 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69  ;.      pPage->i
100f2 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  sInit = 0;.     
100f3 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 20   pPage->pParent 
100f4 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
100f5 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
100f6 50 61 67 65 28 70 50 61 67 65 2c 20 30 29 3b 0a  Page(pPage, 0);.
100f7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
100f8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
100f9 20 20 20 20 20 66 72 65 65 50 61 67 65 28 70 43       freePage(pC
100fa 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 54 52 41  hild);.      TRA
100fb 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 74 72  CE(("BALANCE: tr
100fc 61 6e 73 66 65 72 20 63 68 69 6c 64 20 25 64 20  ansfer child %d 
100fd 69 6e 74 6f 20 72 6f 6f 74 20 25 64 5c 6e 22 2c  into root %d\n",
100fe 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
100ff 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 70 50 61  Child->pgno, pPa
10100 67 65 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  ge->pgno));.    
10101 7d 0a 20 20 20 20 72 63 20 3d 20 72 65 70 61 72  }.    rc = repar
10102 65 6e 74 43 68 69 6c 64 50 61 67 65 73 28 70 50  entChildPages(pP
10103 61 67 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74  age);.    assert
10104 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
10105 6f 77 3d 3d 30 20 29 3b 0a 23 69 66 6e 64 65 66  ow==0 );.#ifndef
10106 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
10107 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
10108 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
10109 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
1010a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
1010b 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69  <pPage->nCell; i
1010c 2b 2b 29 7b 20 0a 20 20 20 20 20 20 20 20 72 63  ++){ .        rc
1010d 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c   = ptrmapPutOvfl
1010e 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20  (pPage, i);.    
1010f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
10110 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
10111 20 20 20 67 6f 74 6f 20 65 6e 64 5f 73 68 61 6c     goto end_shal
10112 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a 20 20 20  low_balance;.   
10113 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
10114 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
10115 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 68 69  releasePage(pChi
10116 6c 64 29 3b 0a 20 20 7d 0a 65 6e 64 5f 73 68 61  ld);.  }.end_sha
10117 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3a 0a 20 20  llow_balance:.  
10118 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 70 43  sqlite3_free(apC
10119 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ell);.  return r
1011a 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  c;.}.../*.** The
1011b 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 6f 76   root page is ov
1011c 65 72 66 75 6c 6c 0a 2a 2a 0a 2a 2a 20 57 68 65  erfull.**.** Whe
1011d 6e 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20  n this happens, 
1011e 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 68 69  Create a new chi
1011f 6c 64 20 70 61 67 65 20 61 6e 64 20 63 6f 70 79  ld page and copy
10120 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73   the.** contents
10121 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 69 6e 74   of the root int
10122 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 54 68  o the child.  Th
10123 65 6e 20 6d 61 6b 65 20 74 68 65 20 72 6f 6f 74  en make the root
10124 0a 2a 2a 20 70 61 67 65 20 61 6e 20 65 6d 70 74  .** page an empt
10125 79 20 70 61 67 65 20 77 69 74 68 20 72 69 67 68  y page with righ
10126 74 43 68 69 6c 64 20 70 6f 69 6e 74 69 6e 67 20  tChild pointing 
10127 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 68  to the new.** ch
10128 69 6c 64 2e 20 20 20 46 69 6e 61 6c 6c 79 2c 20  ild.   Finally, 
10129 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 69 6e 74  call balance_int
1012a 65 72 6e 61 6c 28 29 20 6f 6e 20 74 68 65 20 6e  ernal() on the n
1012b 65 77 20 63 68 69 6c 64 0a 2a 2a 20 74 6f 20 63  ew child.** to c
1012c 61 75 73 65 20 69 74 20 74 6f 20 73 70 6c 69 74  ause it to split
1012d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1012e 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 4d  balance_deeper(M
1012f 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a  emPage *pPage){.
10130 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
10131 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
10132 76 61 6c 75 65 20 66 72 6f 6d 20 73 75 62 70 72  value from subpr
10133 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20 20 4d 65  ocedures */.  Me
10134 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 20  mPage *pChild;  
10135 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
10136 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65  a new child page
10137 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43   */.  Pgno pgnoC
10138 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20 50 61 67  hild;     /* Pag
10139 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
1013a 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a  new child page *
1013b 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
1013c 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  t;         /* Th
1013d 65 20 42 54 72 65 65 20 2a 2f 0a 20 20 69 6e 74  e BTree */.  int
1013e 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20   usableSize;    
1013f 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61 62 6c 65   /* Total usable
10140 20 73 69 7a 65 20 6f 66 20 61 20 70 61 67 65 20   size of a page 
10141 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20  */.  u8 *data;  
10142 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
10143 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 65 6e  ent of the paren
10144 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  t page */.  u8 *
10145 63 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  cdata;          
10146 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68  /* Content of th
10147 65 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a  e child page */.
10148 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20    int hdr;      
10149 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1014a 74 6f 20 70 61 67 65 20 68 65 61 64 65 72 20 69  to page header i
1014b 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e  n parent */.  in
1014c 74 20 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20  t brk;          
1014d 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63    /* Offset to c
1014e 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20  ontent of first 
1014f 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e 74 20 2a  cell in parent *
10150 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  /..  assert( pPa
10151 67 65 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29  ge->pParent==0 )
10152 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
10153 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29  e->nOverflow>0 )
10154 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d  ;.  pBt = pPage-
10155 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20  >pBt;.  assert( 
10156 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
10157 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
10158 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74  ;.  rc = allocat
10159 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
1015a 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43 68  &pChild, &pgnoCh
1015b 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ild, pPage->pgno
1015c 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29  , 0);.  if( rc )
1015d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73   return rc;.  as
1015e 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1015f 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43  erIswriteable(pC
10160 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29  hild->pDbPage) )
10161 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d  ;.  usableSize =
10162 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
10163 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65  ;.  data = pPage
10164 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d  ->aData;.  hdr =
10165 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
10166 74 3b 0a 20 20 62 72 6b 20 3d 20 67 65 74 32 62  t;.  brk = get2b
10167 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
10168 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70 43 68  );.  cdata = pCh
10169 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20 6d 65  ild->aData;.  me
1016a 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64 61 74  mcpy(cdata, &dat
1016b 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d 3e 63  a[hdr], pPage->c
1016c 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50 61 67  ellOffset+2*pPag
1016d 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b 0a 20  e->nCell-hdr);. 
1016e 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61 5b 62   memcpy(&cdata[b
1016f 72 6b 5d 2c 20 26 64 61 74 61 5b 62 72 6b 5d 2c  rk], &data[brk],
10170 20 75 73 61 62 6c 65 53 69 7a 65 2d 62 72 6b 29   usableSize-brk)
10171 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 68 69  ;.  assert( pChi
10172 6c 64 2d 3e 69 73 49 6e 69 74 3d 3d 30 20 29 3b  ld->isInit==0 );
10173 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
10174 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 43 68  treeInitPage(pCh
10175 69 6c 64 2c 20 70 50 61 67 65 29 3b 0a 20 20 69  ild, pPage);.  i
10176 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c  f( rc ) goto bal
10177 61 6e 63 65 64 65 65 70 65 72 5f 6f 75 74 3b 0a  ancedeeper_out;.
10178 20 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d    memcpy(pChild-
10179 3e 61 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61  >aOvfl, pPage->a
1017a 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 6e 4f 76  Ovfl, pPage->nOv
1017b 65 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 50  erflow*sizeof(pP
1017c 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b  age->aOvfl[0]));
1017d 0a 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72  .  pChild->nOver
1017e 66 6c 6f 77 20 3d 20 70 50 61 67 65 2d 3e 6e 4f  flow = pPage->nO
1017f 76 65 72 66 6c 6f 77 3b 0a 20 20 69 66 28 20 70  verflow;.  if( p
10180 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77  Child->nOverflow
10181 20 29 7b 0a 20 20 20 20 70 43 68 69 6c 64 2d 3e   ){.    pChild->
10182 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  nFree = 0;.  }. 
10183 20 61 73 73 65 72 74 28 20 70 43 68 69 6c 64 2d   assert( pChild-
10184 3e 6e 43 65 6c 6c 3d 3d 70 50 61 67 65 2d 3e 6e  >nCell==pPage->n
10185 43 65 6c 6c 20 29 3b 0a 20 20 7a 65 72 6f 50 61  Cell );.  zeroPa
10186 67 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64  ge(pPage, pChild
10187 2d 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54  ->aData[0] & ~PT
10188 46 5f 4c 45 41 46 29 3b 0a 20 20 70 75 74 34 62  F_LEAF);.  put4b
10189 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
1018a 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
1018b 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64  et+8], pgnoChild
1018c 29 3b 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c  );.  TRACE(("BAL
1018d 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20  ANCE: copy root 
1018e 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70  %d into %d\n", p
1018f 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 43 68 69  Page->pgno, pChi
10190 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 23 69 66 6e  ld->pgno));.#ifn
10191 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
10192 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28  AUTOVACUUM.  if(
10193 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
10194 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
10195 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
10196 74 28 70 42 74 2c 20 70 43 68 69 6c 64 2d 3e 70  t(pBt, pChild->p
10197 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
10198 45 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b  E, pPage->pgno);
10199 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
1019a 74 6f 20 62 61 6c 61 6e 63 65 64 65 65 70 65 72  to balancedeeper
1019b 5f 6f 75 74 3b 0a 20 20 20 20 66 6f 72 28 69 3d  _out;.    for(i=
1019c 30 3b 20 69 3c 70 43 68 69 6c 64 2d 3e 6e 43 65  0; i<pChild->nCe
1019d 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ll; i++){.      
1019e 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76  rc = ptrmapPutOv
1019f 66 6c 28 70 43 68 69 6c 64 2c 20 69 29 3b 0a 20  fl(pChild, i);. 
101a0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
101a1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
101a2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
101a3 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
101a4 65 6e 64 69 66 0a 20 20 72 63 20 3d 20 62 61 6c  endif.  rc = bal
101a5 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 68  ance_nonroot(pCh
101a6 69 6c 64 29 3b 0a 0a 62 61 6c 61 6e 63 65 64 65  ild);..balancede
101a7 65 70 65 72 5f 6f 75 74 3a 0a 20 20 72 65 6c 65  eper_out:.  rele
101a8 61 73 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b  asePage(pChild);
101a9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
101aa 0a 2f 2a 0a 2a 2a 20 44 65 63 69 64 65 20 69 66  ./*.** Decide if
101ab 20 74 68 65 20 70 61 67 65 20 70 50 61 67 65 20   the page pPage 
101ac 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61 6c 61  needs to be bala
101ad 6e 63 65 64 2e 20 20 49 66 20 62 61 6c 61 6e 63  nced.  If balanc
101ae 69 6e 67 20 69 73 0a 2a 2a 20 72 65 71 75 69 72  ing is.** requir
101af 65 64 2c 20 63 61 6c 6c 20 74 68 65 20 61 70 70  ed, call the app
101b0 72 6f 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69  ropriate balanci
101b1 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73  ng routine..*/.s
101b2 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63  tatic int balanc
101b3 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
101b4 2c 20 69 6e 74 20 69 6e 73 65 72 74 29 7b 0a 20  , int insert){. 
101b5 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
101b6 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 73  _OK;.  assert( s
101b7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
101b8 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
101b9 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 50  tex) );.  if( pP
101ba 61 67 65 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20  age->pParent==0 
101bb 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
101bc 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
101bd 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
101be 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
101bf 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e  E_OK && pPage->n
101c0 4f 76 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20  Overflow>0 ){.  
101c1 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
101c2 5f 64 65 65 70 65 72 28 70 50 61 67 65 29 3b 0a  _deeper(pPage);.
101c3 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
101c4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
101c5 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  Page->nCell==0 )
101c6 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c  {.      rc = bal
101c7 61 6e 63 65 5f 73 68 61 6c 6c 6f 77 65 72 28 70  ance_shallower(p
101c8 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Page);.    }.  }
101c9 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50  else{.    if( pP
101ca 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30  age->nOverflow>0
101cb 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 28 21 69   || .        (!i
101cc 6e 73 65 72 74 20 26 26 20 70 50 61 67 65 2d 3e  nsert && pPage->
101cd 6e 46 72 65 65 3e 70 50 61 67 65 2d 3e 70 42 74  nFree>pPage->pBt
101ce 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33  ->usableSize*2/3
101cf 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
101d0 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28  balance_nonroot(
101d1 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pPage);.    }.  
101d2 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
101d3 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
101d4 74 69 6e 65 20 63 68 65 63 6b 73 20 61 6c 6c 20  tine checks all 
101d5 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69  cursors that poi
101d6 6e 74 20 74 6f 20 74 61 62 6c 65 20 70 67 6e 6f  nt to table pgno
101d7 52 6f 6f 74 2e 0a 2a 2a 20 49 66 20 61 6e 79 20  Root..** If any 
101d8 6f 66 20 74 68 6f 73 65 20 63 75 72 73 6f 72 73  of those cursors
101d9 20 77 65 72 65 20 6f 70 65 6e 65 64 20 77 69 74   were opened wit
101da 68 20 77 72 46 6c 61 67 3d 3d 30 20 69 6e 20 61  h wrFlag==0 in a
101db 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61   different.** da
101dc 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
101dd 6e 20 28 61 20 64 61 74 61 62 61 73 65 20 63 6f  n (a database co
101de 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68  nnection that sh
101df 61 72 65 73 20 74 68 65 20 70 61 67 65 72 0a 2a  ares the pager.*
101e0 2a 20 63 61 63 68 65 20 77 69 74 68 20 74 68 65  * cache with the
101e1 20 63 75 72 72 65 6e 74 20 63 6f 6e 6e 65 63 74   current connect
101e2 69 6f 6e 29 20 61 6e 64 20 74 68 61 74 20 6f 74  ion) and that ot
101e3 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a  her connection .
101e4 2a 2a 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65  ** is not in the
101e5 20 52 65 61 64 55 6e 63 6f 6d 6d 6d 69 74 74 65   ReadUncommmitte
101e6 64 20 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68  d state, then th
101e7 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
101e8 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f  ns .** SQLITE_LO
101e9 43 4b 45 44 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61  CKED..**.** In a
101ea 64 64 69 74 69 6f 6e 20 74 6f 20 63 68 65 63 6b  ddition to check
101eb 69 6e 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63  ing for read-loc
101ec 6b 73 20 28 77 68 65 72 65 20 61 20 72 65 61 64  ks (where a read
101ed 2d 6c 6f 63 6b 20 0a 2a 2a 20 6d 65 61 6e 73 20  -lock .** means 
101ee 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20  a cursor opened 
101ef 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 29 20  with wrFlag==0) 
101f0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73  this routine als
101f1 6f 20 6d 6f 76 65 73 0a 2a 2a 20 61 6c 6c 20 77  o moves.** all w
101f2 72 69 74 65 20 63 75 72 73 6f 72 73 20 73 6f 20  rite cursors so 
101f3 74 68 61 74 20 74 68 65 79 20 61 72 65 20 70 6f  that they are po
101f4 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 0a 2a  inting to the .*
101f5 2a 20 66 69 72 73 74 20 43 65 6c 6c 20 6f 6e 20  * first Cell on 
101f6 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 20  the root page.  
101f7 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72  This is necessar
101f8 79 20 62 65 63 61 75 73 65 20 61 6e 20 69 6e 73  y because an ins
101f9 65 72 74 20 0a 2a 2a 20 6f 72 20 64 65 6c 65 74  ert .** or delet
101fa 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74  e might change t
101fb 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c  he number of cel
101fc 6c 73 20 6f 6e 20 61 20 70 61 67 65 20 6f 72 20  ls on a page or 
101fd 64 65 6c 65 74 65 0a 2a 2a 20 61 20 70 61 67 65  delete.** a page
101fe 20 65 6e 74 69 72 65 6c 79 20 61 6e 64 20 77 65   entirely and we
101ff 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20   do not want to 
10200 6c 65 61 76 65 20 61 6e 79 20 63 75 72 73 6f 72  leave any cursor
10201 73 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74  s .** pointing t
10202 6f 20 6e 6f 6e 2d 65 78 69 73 74 61 6e 74 20 70  o non-existant p
10203 61 67 65 73 20 6f 72 20 63 65 6c 6c 73 2e 0a 2a  ages or cells..*
10204 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
10205 63 6b 52 65 61 64 4c 6f 63 6b 73 28 42 74 72 65  ckReadLocks(Btre
10206 65 20 2a 70 42 74 72 65 65 2c 20 50 67 6e 6f 20  e *pBtree, Pgno 
10207 70 67 6e 6f 52 6f 6f 74 2c 20 42 74 43 75 72 73  pgnoRoot, BtCurs
10208 6f 72 20 2a 70 45 78 63 6c 75 64 65 29 7b 0a 20  or *pExclude){. 
10209 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20   BtCursor *p;.  
1020a 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1020b 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 73  pBtree->pBt;.  s
1020c 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 42 74  qlite3 *db = pBt
1020d 72 65 65 2d 3e 70 53 71 6c 69 74 65 3b 0a 20 20  ree->pSqlite;.  
1020e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1020f 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
10210 42 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28  Btree) );.  for(
10211 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20  p=pBt->pCursor; 
10212 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
10213 20 20 20 20 69 66 28 20 70 3d 3d 70 45 78 63 6c      if( p==pExcl
10214 75 64 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ude ) continue;.
10215 20 20 20 20 69 66 28 20 70 2d 3e 65 53 74 61 74      if( p->eStat
10216 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e!=CURSOR_VALID 
10217 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
10218 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 21  if( p->pgnoRoot!
10219 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63 6f 6e 74  =pgnoRoot ) cont
1021a 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 2d  inue;.    if( p-
1021b 3e 77 72 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20  >wrFlag==0 ){.  
1021c 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f      sqlite3 *dbO
1021d 74 68 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65  ther = p->pBtree
1021e 2d 3e 70 53 71 6c 69 74 65 3b 0a 20 20 20 20 20  ->pSqlite;.     
1021f 20 69 66 28 20 64 62 4f 74 68 65 72 3d 3d 30 20   if( dbOther==0 
10220 7c 7c 0a 20 20 20 20 20 20 20 20 20 28 64 62 4f  ||.         (dbO
10221 74 68 65 72 21 3d 64 62 20 26 26 20 28 64 62 4f  ther!=db && (dbO
10222 74 68 65 72 2d 3e 66 6c 61 67 73 20 26 20 53 51  ther->flags & SQ
10223 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
10224 74 74 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20 20  tted)==0) ){.   
10225 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
10226 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20  TE_LOCKED;.     
10227 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
10228 20 70 2d 3e 70 50 61 67 65 2d 3e 70 67 6e 6f 21   p->pPage->pgno!
10229 3d 70 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a  =p->pgnoRoot ){.
1022a 20 20 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74        moveToRoot
1022b 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  (p);.    }.  }. 
1022c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1022d 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65  K;.}../*.** Inse
1022e 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20  rt a new record 
1022f 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20  into the BTree. 
10230 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 65   The key is give
10231 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29  n by (pKey,nKey)
10232 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61  .** and the data
10233 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 44   is given by (pD
10234 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65  ata,nData).  The
10235 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20   cursor is used 
10236 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e  only to.** defin
10237 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 65  e what table the
10238 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62   record should b
10239 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e  e inserted into.
1023a 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20    The cursor.** 
1023b 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
1023c 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63   at a random loc
1023d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ation..**.** For
1023e 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65   an INTKEY table
1023f 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20  , only the nKey 
10240 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79  value of the key
10241 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20   is used.  pKey 
10242 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20  is.** ignored.  
10243 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74  For a ZERODATA t
10244 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20  able, the pData 
10245 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f  and nData are bo
10246 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53  th ignored..*/.S
10247 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10248 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  t sqlite3BtreeIn
10249 73 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72  sert(.  BtCursor
1024a 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20   *pCur,         
1024b 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74         /* Insert
1024c 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74   data into the t
1024d 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72  able of this cur
1024e 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  sor */.  const v
1024f 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e  oid *pKey, i64 n
10250 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b  Key,    /* The k
10251 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65  ey of the new re
10252 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  cord */.  const 
10253 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74  void *pData, int
10254 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20   nData,  /* The 
10255 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20  data of the new 
10256 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20  record */.  int 
10257 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20  nZero,          
10258 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
10259 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 20  mber of extra 0 
1025a 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20  bytes to append 
1025b 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74  to data */.  int
1025c 20 61 70 70 65 6e 64 42 69 61 73 20 20 20 20 20   appendBias     
1025d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1025e 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c  rue if this is l
1025f 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20  ikely an append 
10260 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
10261 20 20 69 6e 74 20 6c 6f 63 3b 0a 20 20 69 6e 74    int loc;.  int
10262 20 73 7a 4e 65 77 3b 0a 20 20 4d 65 6d 50 61 67   szNew;.  MemPag
10263 65 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65  e *pPage;.  Btre
10264 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74  e *p = pCur->pBt
10265 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20  ree;.  BtShared 
10266 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
10267 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
10268 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67  oldCell;.  unsig
10269 6e 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c  ned char *newCel
1026a 6c 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  l = 0;..  assert
1026b 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
1026c 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66  ex(pCur) );.  if
1026d 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ( pBt->inTransac
1026e 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 57 52 49 54  tion!=TRANS_WRIT
1026f 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 75 73 74  E ){.    /* Must
10270 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63   start a transac
10271 74 69 6f 6e 20 62 65 66 6f 72 65 20 64 6f 69 6e  tion before doin
10272 67 20 61 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20  g an insert */. 
10273 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61     rc = pBt->rea
10274 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52  dOnly ? SQLITE_R
10275 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45  EADONLY : SQLITE
10276 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75  _ERROR;.    retu
10277 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
10278 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f  ert( !pBt->readO
10279 6e 6c 79 20 29 3b 0a 20 20 69 66 28 20 21 70 43  nly );.  if( !pC
1027a 75 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20  ur->wrFlag ){.  
1027b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1027c 50 45 52 4d 3b 20 20 20 2f 2a 20 43 75 72 73 6f  PERM;   /* Curso
1027d 72 20 6e 6f 74 20 6f 70 65 6e 20 66 6f 72 20 77  r not open for w
1027e 72 69 74 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20  riting */.  }.  
1027f 69 66 28 20 63 68 65 63 6b 52 65 61 64 4c 6f 63  if( checkReadLoc
10280 6b 73 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2c  ks(pCur->pBtree,
10281 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
10282 20 70 43 75 72 29 20 29 7b 0a 20 20 20 20 72 65   pCur) ){.    re
10283 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
10284 45 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c 65  ED; /* The table
10285 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20   pCur points to 
10286 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  has a read lock 
10287 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75  */.  }.  if( pCu
10288 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
10289 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72  R_FAULT ){.    r
1028a 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70  eturn pCur->skip
1028b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65  ;.  }..  /* Save
1028c 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f   the positions o
1028d 66 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73  f any other curs
1028e 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73  ors open on this
1028f 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6c 65 61   table */.  clea
10290 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  rCursorPosition(
10291 70 43 75 72 29 3b 0a 20 20 69 66 28 20 0a 20 20  pCur);.  if( .  
10292 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63    SQLITE_OK!=(rc
10293 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72   = saveAllCursor
10294 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e  s(pBt, pCur->pgn
10295 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29 20 7c 7c  oRoot, pCur)) ||
10296 0a 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d  .    SQLITE_OK!=
10297 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72  (rc = sqlite3Btr
10298 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70  eeMoveto(pCur, p
10299 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70 70 65 6e  Key, nKey, appen
1029a 64 42 69 61 73 2c 20 26 6c 6f 63 29 29 0a 20 20  dBias, &loc)).  
1029b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
1029c 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 20 3d  ;.  }..  pPage =
1029d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20   pCur->pPage;.  
1029e 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
1029f 6e 74 4b 65 79 20 7c 7c 20 6e 4b 65 79 3e 3d 30  ntKey || nKey>=0
102a0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
102a1 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 50  age->leaf || !pP
102a2 61 67 65 2d 3e 6c 65 61 66 44 61 74 61 20 29 3b  age->leafData );
102a3 0a 20 20 54 52 41 43 45 28 28 22 49 4e 53 45 52  .  TRACE(("INSER
102a4 54 3a 20 74 61 62 6c 65 3d 25 64 20 6e 6b 65 79  T: table=%d nkey
102a5 3d 25 6c 6c 64 20 6e 64 61 74 61 3d 25 64 20 70  =%lld ndata=%d p
102a6 61 67 65 3d 25 64 20 25 73 5c 6e 22 2c 0a 20 20  age=%d %s\n",.  
102a7 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67          pCur->pg
102a8 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 79 2c 20 6e 44  noRoot, nKey, nD
102a9 61 74 61 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ata, pPage->pgno
102aa 2c 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 3d  ,.          loc=
102ab 3d 30 20 3f 20 22 6f 76 65 72 77 72 69 74 65 22  =0 ? "overwrite"
102ac 20 3a 20 22 6e 65 77 20 65 6e 74 72 79 22 29 29   : "new entry"))
102ad 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
102ae 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 6e  e->isInit );.  n
102af 65 77 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33  ewCell = sqlite3
102b0 5f 6d 61 6c 6c 6f 63 28 20 4d 58 5f 43 45 4c 4c  _malloc( MX_CELL
102b1 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20  _SIZE(pBt) );.  
102b2 69 66 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29  if( newCell==0 )
102b3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
102b4 4f 4d 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c  OMEM;.  rc = fil
102b5 6c 49 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e  lInCell(pPage, n
102b6 65 77 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b  ewCell, pKey, nK
102b7 65 79 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  ey, pData, nData
102b8 2c 20 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29  , nZero, &szNew)
102b9 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74  ;.  if( rc ) got
102ba 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20  o end_insert;.  
102bb 61 73 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63  assert( szNew==c
102bc 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
102bd 2c 20 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20  , newCell) );.  
102be 61 73 73 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d  assert( szNew<=M
102bf 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
102c0 20 29 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30   );.  if( loc==0
102c1 20 26 26 20 43 55 52 53 4f 52 5f 56 41 4c 49 44   && CURSOR_VALID
102c2 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29  ==pCur->eState )
102c3 7b 0a 20 20 20 20 69 6e 74 20 73 7a 4f 6c 64 3b  {.    int szOld;
102c4 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
102c5 72 2d 3e 69 64 78 3e 3d 30 20 26 26 20 70 43 75  r->idx>=0 && pCu
102c6 72 2d 3e 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43  r->idx<pPage->nC
102c7 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ell );.    rc = 
102c8 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
102c9 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
102ca 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
102cb 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
102cc 69 6e 73 65 72 74 3b 0a 20 20 20 20 7d 0a 20 20  insert;.    }.  
102cd 20 20 6f 6c 64 43 65 6c 6c 20 3d 20 66 69 6e 64    oldCell = find
102ce 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72  Cell(pPage, pCur
102cf 2d 3e 69 64 78 29 3b 0a 20 20 20 20 69 66 28 20  ->idx);.    if( 
102d0 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
102d1 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e 65 77        memcpy(new
102d2 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c 20 34  Cell, oldCell, 4
102d3 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4f  );.    }.    szO
102d4 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  ld = cellSizePtr
102d5 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29  (pPage, oldCell)
102d6 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72  ;.    rc = clear
102d7 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c 64 43  Cell(pPage, oldC
102d8 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ell);.    if( rc
102d9 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65   ) goto end_inse
102da 72 74 3b 0a 20 20 20 20 64 72 6f 70 43 65 6c 6c  rt;.    dropCell
102db 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64  (pPage, pCur->id
102dc 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20 20 7d 65 6c  x, szOld);.  }el
102dd 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26 20  se if( loc<0 && 
102de 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29  pPage->nCell>0 )
102df 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
102e0 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20  age->leaf );.   
102e1 20 70 43 75 72 2d 3e 69 64 78 2b 2b 3b 0a 20 20   pCur->idx++;.  
102e2 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
102e3 7a 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ze = 0;.  }else{
102e4 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
102e5 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a  ge->leaf );.  }.
102e6 20 20 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c    rc = insertCel
102e7 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69  l(pPage, pCur->i
102e8 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e  dx, newCell, szN
102e9 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28  ew, 0, 0);.  if(
102ea 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
102eb 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74   goto end_insert
102ec 3b 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65  ;.  rc = balance
102ed 28 70 50 61 67 65 2c 20 31 29 3b 0a 20 20 2f 2a  (pPage, 1);.  /*
102ee 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
102ef 65 44 75 6d 70 28 70 43 75 72 2d 3e 70 42 74 2c  eDump(pCur->pBt,
102f0 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
102f1 20 31 29 3b 20 2a 2f 0a 20 20 2f 2a 20 66 66 6c   1); */.  /* ffl
102f2 75 73 68 28 73 74 64 6f 75 74 29 3b 20 2a 2f 0a  ush(stdout); */.
102f3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
102f4 5f 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54  _OK ){.    moveT
102f5 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d  oRoot(pCur);.  }
102f6 0a 65 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 73  .end_insert:.  s
102f7 71 6c 69 74 65 33 5f 66 72 65 65 28 6e 65 77 43  qlite3_free(newC
102f8 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ell);.  return r
102f9 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  c;.}../*.** Dele
102fa 74 65 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  te the entry tha
102fb 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  t the cursor is 
102fc 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68  pointing to.  Th
102fd 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c  e cursor.** is l
102fe 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
102ff 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f  a random locatio
10300 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
10301 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
10302 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 75  BtreeDelete(BtCu
10303 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d  rsor *pCur){.  M
10304 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
10305 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 75  pCur->pPage;.  u
10306 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43  nsigned char *pC
10307 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ell;.  int rc;. 
10308 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 20   Pgno pgnoChild 
10309 3d 20 30 3b 0a 20 20 42 74 72 65 65 20 2a 70 20  = 0;.  Btree *p 
1030a 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a  = pCur->pBtree;.
1030b 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1030c 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73  = p->pBt;..  ass
1030d 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
1030e 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
1030f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
10310 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20  isInit );.  if( 
10311 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
10312 6f 6e 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  on!=TRANS_WRITE 
10313 29 7b 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 73  ){.    /* Must s
10314 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69  tart a transacti
10315 6f 6e 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 20  on before doing 
10316 61 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20  a delete */.    
10317 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e  rc = pBt->readOn
10318 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41 44  ly ? SQLITE_READ
10319 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45 52  ONLY : SQLITE_ER
1031a 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ROR;.    return 
1031b 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rc;.  }.  assert
1031c 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  ( !pBt->readOnly
1031d 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e   );.  if( pCur->
1031e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46  eState==CURSOR_F
1031f 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75  AULT ){.    retu
10320 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20  rn pCur->skip;. 
10321 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69   }.  if( pCur->i
10322 64 78 20 3e 3d 20 70 50 61 67 65 2d 3e 6e 43 65  dx >= pPage->nCe
10323 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
10324 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20   SQLITE_ERROR;  
10325 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 69 73  /* The cursor is
10326 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f   not pointing to
10327 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d   anything */.  }
10328 0a 20 20 69 66 28 20 21 70 43 75 72 2d 3e 77 72  .  if( !pCur->wr
10329 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75  Flag ){.    retu
1032a 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 20  rn SQLITE_PERM; 
1032b 20 20 2f 2a 20 44 69 64 20 6e 6f 74 20 6f 70 65    /* Did not ope
1032c 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 66 6f  n this cursor fo
1032d 72 20 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 7d  r writing */.  }
1032e 0a 20 20 69 66 28 20 63 68 65 63 6b 52 65 61 64  .  if( checkRead
1032f 4c 6f 63 6b 73 28 70 43 75 72 2d 3e 70 42 74 72  Locks(pCur->pBtr
10330 65 65 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f  ee, pCur->pgnoRo
10331 6f 74 2c 20 70 43 75 72 29 20 29 7b 0a 20 20 20  ot, pCur) ){.   
10332 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
10333 4f 43 4b 45 44 3b 20 2f 2a 20 54 68 65 20 74 61  OCKED; /* The ta
10334 62 6c 65 20 70 43 75 72 20 70 6f 69 6e 74 73 20  ble pCur points 
10335 74 6f 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f  to has a read lo
10336 63 6b 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ck */.  }..  /* 
10337 52 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 72  Restore the curr
10338 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74  ent cursor posit
10339 69 6f 6e 20 28 61 20 6e 6f 2d 6f 70 20 69 66 20  ion (a no-op if 
1033a 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f  the cursor is no
1033b 74 20 69 6e 20 0a 20 20 2a 2a 20 43 55 52 53 4f  t in .  ** CURSO
1033c 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 73 74  R_REQUIRESEEK st
1033d 61 74 65 29 20 61 6e 64 20 73 61 76 65 20 74 68  ate) and save th
1033e 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61  e positions of a
1033f 6e 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73  ny other cursors
10340 20 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f 6e 20 74   .  ** open on t
10341 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 54  he same table. T
10342 68 65 6e 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  hen call sqlite3
10343 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20  PagerWrite() on 
10344 74 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 74 68  the page.  ** th
10345 61 74 20 74 68 65 20 65 6e 74 72 79 20 77 69 6c  at the entry wil
10346 6c 20 62 65 20 64 65 6c 65 74 65 64 20 66 72 6f  l be deleted fro
10347 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20  m..  */.  if( . 
10348 20 20 20 28 72 63 20 3d 20 72 65 73 74 6f 72 65     (rc = restore
10349 4f 72 43 6c 65 61 72 43 75 72 73 6f 72 50 6f 73  OrClearCursorPos
1034a 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d 30 20  ition(pCur))!=0 
1034b 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 61 76  ||.    (rc = sav
1034c 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c  eAllCursors(pBt,
1034d 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
1034e 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a 20 20   pCur))!=0 ||.  
1034f 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50    (rc = sqlite3P
10350 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
10351 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a 20 20  >pDbPage))!=0.  
10352 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
10353 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61  ;.  }..  /* Loca
10354 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68  te the cell with
10355 69 6e 20 69 74 27 73 20 70 61 67 65 20 61 6e 64  in it's page and
10356 20 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f 69   leave pCell poi
10357 6e 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 2a  nting to the.  *
10358 2a 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 61  * data. The clea
10359 72 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 65  rCell() call fre
1035a 65 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20  es any overflow 
1035b 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64  pages associated
1035c 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 63   with the.  ** c
1035d 65 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 74  ell. The cell it
1035e 73 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 6e  self is still in
1035f 74 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 65  tact..  */.  pCe
10360 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
10361 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78 29 3b  age, pCur->idx);
10362 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
10363 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 43  eaf ){.    pgnoC
10364 68 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28  hild = get4byte(
10365 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a 20 20 72 63  pCell);.  }.  rc
10366 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61   = clearCell(pPa
10367 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 69 66  ge, pCell);.  if
10368 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
10369 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66  rn rc;.  }..  if
1036a 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
1036b 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  {.    /*.    ** 
1036c 54 68 65 20 65 6e 74 72 79 20 77 65 20 61 72 65  The entry we are
1036d 20 61 62 6f 75 74 20 74 6f 20 64 65 6c 65 74 65   about to delete
1036e 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 73   is not a leaf s
1036f 6f 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 0a 20  o if we do not. 
10370 20 20 20 2a 2a 20 64 6f 20 73 6f 6d 65 74 68 69     ** do somethi
10371 6e 67 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65  ng we will leave
10372 20 61 20 68 6f 6c 65 20 6f 6e 20 61 6e 20 69 6e   a hole on an in
10373 74 65 72 6e 61 6c 20 70 61 67 65 2e 0a 20 20 20  ternal page..   
10374 20 2a 2a 20 57 65 20 68 61 76 65 20 74 6f 20 66   ** We have to f
10375 69 6c 6c 20 74 68 65 20 68 6f 6c 65 20 62 79 20  ill the hole by 
10376 6d 6f 76 69 6e 67 20 69 6e 20 61 20 63 65 6c 6c  moving in a cell
10377 20 66 72 6f 6d 20 61 20 6c 65 61 66 2e 20 20 54   from a leaf.  T
10378 68 65 0a 20 20 20 20 2a 2a 20 6e 65 78 74 20 43  he.    ** next C
10379 65 6c 6c 20 61 66 74 65 72 20 74 68 65 20 6f 6e  ell after the on
1037a 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  e to be deleted 
1037b 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
1037c 20 65 78 69 73 74 20 61 6e 64 0a 20 20 20 20 2a   exist and.    *
1037d 2a 20 74 6f 20 62 65 20 61 20 6c 65 61 66 20 73  * to be a leaf s
1037e 6f 20 77 65 20 63 61 6e 20 75 73 65 20 69 74 2e  o we can use it.
1037f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 42 74 43 75  .    */.    BtCu
10380 72 73 6f 72 20 6c 65 61 66 43 75 72 3b 0a 20 20  rsor leafCur;.  
10381 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
10382 2a 70 4e 65 78 74 3b 0a 20 20 20 20 69 6e 74 20  *pNext;.    int 
10383 73 7a 4e 65 78 74 3b 20 20 2f 2a 20 54 68 65 20  szNext;  /* The 
10384 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67  compiler warning
10385 20 69 73 20 77 72 6f 6e 67 3a 20 73 7a 4e 65 78   is wrong: szNex
10386 74 20 69 73 20 61 6c 77 61 79 73 20 0a 20 20 20  t is always .   
10387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
10388 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 65 66   initialized bef
10389 6f 72 65 20 75 73 65 2e 20 20 41 64 64 69 6e 67  ore use.  Adding
1038a 20 61 6e 20 65 78 74 72 61 20 69 6e 69 74 69 61   an extra initia
1038b 6c 69 7a 61 74 69 6f 6e 0a 20 20 20 20 20 20 20  lization.       
1038c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20            ** to 
1038d 73 69 6c 65 6e 63 65 20 74 68 65 20 63 6f 6d 70  silence the comp
1038e 69 6c 65 72 20 73 6c 6f 77 73 20 64 6f 77 6e 20  iler slows down 
1038f 74 68 65 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 20  the code. */.   
10390 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a 20 20   int notUsed;.  
10391 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
10392 2a 74 65 6d 70 43 65 6c 6c 20 3d 20 30 3b 0a 20  *tempCell = 0;. 
10393 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67     assert( !pPag
10394 65 2d 3e 6c 65 61 66 44 61 74 61 20 29 3b 0a 20  e->leafData );. 
10395 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47     sqlite3BtreeG
10396 65 74 54 65 6d 70 43 75 72 73 6f 72 28 70 43 75  etTempCursor(pCu
10397 72 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a 20 20  r, &leafCur);.  
10398 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
10399 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75 72  reeNext(&leafCur
1039a 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 20  , &notUsed);.   
1039b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1039c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
1039d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1039e 74 65 28 6c 65 61 66 43 75 72 2e 70 50 61 67 65  te(leafCur.pPage
1039f 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
103a0 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
103a1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
103a2 20 54 52 41 43 45 28 28 22 44 45 4c 45 54 45 3a   TRACE(("DELETE:
103a3 20 74 61 62 6c 65 3d 25 64 20 64 65 6c 65 74 65   table=%d delete
103a4 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f 6d 20 25   internal from %
103a5 64 20 72 65 70 6c 61 63 65 20 66 72 6f 6d 20 6c  d replace from l
103a6 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20  eaf %d\n",.     
103a7 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f      pCur->pgnoRo
103a8 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  ot, pPage->pgno,
103a9 20 6c 65 61 66 43 75 72 2e 70 50 61 67 65 2d 3e   leafCur.pPage->
103aa 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72  pgno));.      dr
103ab 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43  opCell(pPage, pC
103ac 75 72 2d 3e 69 64 78 2c 20 63 65 6c 6c 53 69 7a  ur->idx, cellSiz
103ad 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  ePtr(pPage, pCel
103ae 6c 29 29 3b 0a 20 20 20 20 20 20 70 4e 65 78 74  l));.      pNext
103af 20 3d 20 66 69 6e 64 43 65 6c 6c 28 6c 65 61 66   = findCell(leaf
103b0 43 75 72 2e 70 50 61 67 65 2c 20 6c 65 61 66 43  Cur.pPage, leafC
103b1 75 72 2e 69 64 78 29 3b 0a 20 20 20 20 20 20 73  ur.idx);.      s
103b2 7a 4e 65 78 74 20 3d 20 63 65 6c 6c 53 69 7a 65  zNext = cellSize
103b3 50 74 72 28 6c 65 61 66 43 75 72 2e 70 50 61 67  Ptr(leafCur.pPag
103b4 65 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20  e, pNext);.     
103b5 20 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c   assert( MX_CELL
103b6 5f 53 49 5a 45 28 70 42 74 29 3e 3d 73 7a 4e 65  _SIZE(pBt)>=szNe
103b7 78 74 2b 34 20 29 3b 0a 20 20 20 20 20 20 74 65  xt+4 );.      te
103b8 6d 70 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33  mpCell = sqlite3
103b9 5f 6d 61 6c 6c 6f 63 28 20 4d 58 5f 43 45 4c 4c  _malloc( MX_CELL
103ba 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20  _SIZE(pBt) );.  
103bb 20 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c 6c      if( tempCell
103bc 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
103bd 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
103be 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
103bf 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
103c0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
103c1 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70  c = insertCell(p
103c2 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78 2c  Page, pCur->idx,
103c3 20 70 4e 65 78 74 2d 34 2c 20 73 7a 4e 65 78 74   pNext-4, szNext
103c4 2b 34 2c 20 74 65 6d 70 43 65 6c 6c 2c 20 30 29  +4, tempCell, 0)
103c5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
103c6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
103c7 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
103c8 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c  findOverflowCell
103c9 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64  (pPage, pCur->id
103ca 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a  x), pgnoChild);.
103cb 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e        rc = balan
103cc 63 65 28 70 50 61 67 65 2c 20 30 29 3b 0a 20 20  ce(pPage, 0);.  
103cd 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
103ce 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
103cf 20 20 20 64 72 6f 70 43 65 6c 6c 28 6c 65 61 66     dropCell(leaf
103d0 43 75 72 2e 70 50 61 67 65 2c 20 6c 65 61 66 43  Cur.pPage, leafC
103d1 75 72 2e 69 64 78 2c 20 73 7a 4e 65 78 74 29 3b  ur.idx, szNext);
103d2 0a 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61  .      rc = bala
103d3 6e 63 65 28 6c 65 61 66 43 75 72 2e 70 50 61 67  nce(leafCur.pPag
103d4 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  e, 0);.    }.   
103d5 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 74 65   sqlite3_free(te
103d6 6d 70 43 65 6c 6c 29 3b 0a 20 20 20 20 73 71 6c  mpCell);.    sql
103d7 69 74 65 33 42 74 72 65 65 52 65 6c 65 61 73 65  ite3BtreeRelease
103d8 54 65 6d 70 43 75 72 73 6f 72 28 26 6c 65 61 66  TempCursor(&leaf
103d9 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Cur);.  }else{. 
103da 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45 54     TRACE(("DELET
103db 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c 65  E: table=%d dele
103dc 74 65 20 66 72 6f 6d 20 6c 65 61 66 20 25 64 5c  te from leaf %d\
103dd 6e 22 2c 0a 20 20 20 20 20 20 20 70 43 75 72 2d  n",.       pCur-
103de 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 50 61 67 65  >pgnoRoot, pPage
103df 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 64 72  ->pgno));.    dr
103e0 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43  opCell(pPage, pC
103e1 75 72 2d 3e 69 64 78 2c 20 63 65 6c 6c 53 69 7a  ur->idx, cellSiz
103e2 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  ePtr(pPage, pCel
103e3 6c 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 61  l));.    rc = ba
103e4 6c 61 6e 63 65 28 70 50 61 67 65 2c 20 30 29 3b  lance(pPage, 0);
103e5 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
103e6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
103e7 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
103e8 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
103e9 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  c;.}../*.** Crea
103ea 74 65 20 61 20 6e 65 77 20 42 54 72 65 65 20 74  te a new BTree t
103eb 61 62 6c 65 2e 20 20 57 72 69 74 65 20 69 6e 74  able.  Write int
103ec 6f 20 2a 70 69 54 61 62 6c 65 20 74 68 65 20 70  o *piTable the p
103ed 61 67 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f  age.** number fo
103ee 72 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  r the root page 
103ef 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
103f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65  ..**.** The type
103f1 20 6f 66 20 74 79 70 65 20 69 73 20 64 65 74 65   of type is dete
103f2 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 66 6c  rmined by the fl
103f3 61 67 73 20 70 61 72 61 6d 65 74 65 72 2e 20 20  ags parameter.  
103f4 4f 6e 6c 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c  Only the.** foll
103f5 6f 77 69 6e 67 20 76 61 6c 75 65 73 20 6f 66 20  owing values of 
103f6 66 6c 61 67 73 20 61 72 65 20 63 75 72 72 65 6e  flags are curren
103f7 74 6c 79 20 69 6e 20 75 73 65 2e 20 20 4f 74 68  tly in use.  Oth
103f8 65 72 20 76 61 6c 75 65 73 20 66 6f 72 0a 2a 2a  er values for.**
103f9 20 66 6c 61 67 73 20 6d 69 67 68 74 20 6e 6f 74   flags might not
103fa 20 77 6f 72 6b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   work:.**.**    
103fb 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 7c 42 54   BTREE_INTKEY|BT
103fc 52 45 45 5f 4c 45 41 46 44 41 54 41 20 20 20 20  REE_LEAFDATA    
103fd 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20 74 61   Used for SQL ta
103fe 62 6c 65 73 20 77 69 74 68 20 72 6f 77 69 64 20  bles with rowid 
103ff 6b 65 79 73 0a 2a 2a 20 20 20 20 20 42 54 52 45  keys.**     BTRE
10400 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 20 20 20  E_ZERODATA      
10401 20 20 20 20 20 20 20 20 20 20 20 20 55 73 65 64              Used
10402 20 66 6f 72 20 53 51 4c 20 69 6e 64 69 63 65 73   for SQL indices
10403 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
10404 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28  treeCreateTable(
10405 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70  Btree *p, int *p
10406 69 54 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67  iTable, int flag
10407 73 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  s){.  BtShared *
10408 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
10409 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a  MemPage *pRoot;.
1040a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b    Pgno pgnoRoot;
1040b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
1040c 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
1040d 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20  eeHoldsMutex(p) 
1040e 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e  );.  if( pBt->in
1040f 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41  Transaction!=TRA
10410 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
10411 2f 2a 20 4d 75 73 74 20 73 74 61 72 74 20 61 20  /* Must start a 
10412 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 69 72 73  transaction firs
10413 74 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 42  t */.    rc = pB
10414 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51  t->readOnly ? SQ
10415 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 3a 20  LITE_READONLY : 
10416 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
10417 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
10418 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d  .  assert( !pBt-
10419 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 0a 23 69  >readOnly );..#i
1041a 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1041b 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 63  _AUTOVACUUM.  rc
1041c 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
1041d 50 61 67 65 28 70 42 74 2c 20 26 70 52 6f 6f 74  Page(pBt, &pRoot
1041e 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20  , &pgnoRoot, 1, 
1041f 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  0);.  if( rc ){.
10420 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
10421 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 70   }.#else.  if( p
10422 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
10423 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 4d  {.    Pgno pgnoM
10424 6f 76 65 3b 20 20 20 20 20 20 2f 2a 20 4d 6f 76  ove;      /* Mov
10425 65 20 61 20 70 61 67 65 20 68 65 72 65 20 74 6f  e a page here to
10426 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74   make room for t
10427 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 2a 2f 0a  he root-page */.
10428 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61      MemPage *pPa
10429 67 65 4d 6f 76 65 3b 20 2f 2a 20 54 68 65 20 70  geMove; /* The p
1042a 61 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e 20  age to move to. 
1042b 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74  */..    /* Creat
1042c 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 20  ing a new table 
1042d 6d 61 79 20 70 72 6f 62 61 62 6c 79 20 72 65 71  may probably req
1042e 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 65  uire moving an e
1042f 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65  xisting database
10430 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20  .    ** to make 
10431 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 6e 65 77  room for the new
10432 20 74 61 62 6c 65 73 20 72 6f 6f 74 20 70 61 67   tables root pag
10433 65 2e 20 49 6e 20 63 61 73 65 20 74 68 69 73 20  e. In case this 
10434 70 61 67 65 20 74 75 72 6e 73 0a 20 20 20 20 2a  page turns.    *
10435 2a 20 6f 75 74 20 74 6f 20 62 65 20 61 6e 20 6f  * out to be an o
10436 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 64 65  verflow page, de
10437 6c 65 74 65 20 61 6c 6c 20 6f 76 65 72 66 6c 6f  lete all overflo
10438 77 20 70 61 67 65 2d 6d 61 70 20 63 61 63 68 65  w page-map cache
10439 73 0a 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79  s.    ** held by
1043a 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a 20   open cursors.. 
1043b 20 20 20 2a 2f 0a 20 20 20 20 69 6e 76 61 6c 69     */.    invali
1043c 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43  dateAllOverflowC
1043d 61 63 68 65 28 70 42 74 29 3b 0a 0a 20 20 20 20  ache(pBt);..    
1043e 2f 2a 20 52 65 61 64 20 74 68 65 20 76 61 6c 75  /* Read the valu
1043f 65 20 6f 66 20 6d 65 74 61 5b 33 5d 20 66 72 6f  e of meta[3] fro
10440 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  m the database t
10441 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 72  o determine wher
10442 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 6f 6f  e the.    ** roo
10443 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65  t page of the ne
10444 77 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 67  w table should g
10445 6f 2e 20 6d 65 74 61 5b 33 5d 20 69 73 20 74 68  o. meta[3] is th
10446 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70  e largest root-p
10447 61 67 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74  age.    ** creat
10448 65 64 20 73 6f 20 66 61 72 2c 20 73 6f 20 74 68  ed so far, so th
10449 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 67 65 20  e new root-page 
1044a 69 73 20 28 6d 65 74 61 5b 33 5d 2b 31 29 2e 0a  is (meta[3]+1)..
1044b 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
1044c 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
1044d 65 74 61 28 70 2c 20 34 2c 20 26 70 67 6e 6f 52  eta(p, 4, &pgnoR
1044e 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  oot);.    if( rc
1044f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
10450 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
10451 20 20 20 20 7d 0a 20 20 20 20 70 67 6e 6f 52 6f      }.    pgnoRo
10452 6f 74 2b 2b 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  ot++;..    /* Th
10453 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 67 65 20  e new root-page 
10454 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63  may not be alloc
10455 61 74 65 64 20 6f 6e 20 61 20 70 6f 69 6e 74 65  ated on a pointe
10456 72 2d 6d 61 70 20 70 61 67 65 2c 20 6f 72 20 74  r-map page, or t
10457 68 65 0a 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e  he.    ** PENDIN
10458 47 5f 42 59 54 45 20 70 61 67 65 2e 0a 20 20 20  G_BYTE page..   
10459 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f   */.    if( pgno
1045a 52 6f 6f 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47  Root==PTRMAP_PAG
1045b 45 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f  ENO(pBt, pgnoRoo
1045c 74 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 70 67  t) ||.        pg
1045d 6e 6f 52 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f  noRoot==PENDING_
1045e 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
1045f 7b 0a 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74  {.      pgnoRoot
10460 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  ++;.    }.    as
10461 73 65 72 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d  sert( pgnoRoot>=
10462 33 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c  3 );..    /* All
10463 6f 63 61 74 65 20 61 20 70 61 67 65 2e 20 54 68  ocate a page. Th
10464 65 20 70 61 67 65 20 74 68 61 74 20 63 75 72 72  e page that curr
10465 65 6e 74 6c 79 20 72 65 73 69 64 65 73 20 61 74  ently resides at
10466 20 70 67 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20   pgnoRoot will. 
10467 20 20 20 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74     ** be moved t
10468 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  o the allocated 
10469 70 61 67 65 20 28 75 6e 6c 65 73 73 20 74 68 65  page (unless the
1046a 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20   allocated page 
1046b 68 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74  happens.    ** t
1046c 6f 20 72 65 73 69 64 65 20 61 74 20 70 67 6e 6f  o reside at pgno
1046d 52 6f 6f 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  Root)..    */.  
1046e 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
1046f 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
10470 50 61 67 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d  PageMove, &pgnoM
10471 6f 76 65 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31  ove, pgnoRoot, 1
10472 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
10473 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
10474 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
10475 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 67 6e 6f   }..    if( pgno
10476 4d 6f 76 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29  Move!=pgnoRoot )
10477 7b 0a 20 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52  {.      /* pgnoR
10478 6f 6f 74 20 69 73 20 74 68 65 20 70 61 67 65 20  oot is the page 
10479 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65  that will be use
1047a 64 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70  d for the root-p
1047b 61 67 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20  age of.      ** 
1047c 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 61  the new table (a
1047d 73 73 75 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72  ssuming an error
1047e 20 64 69 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e   did not occur).
1047f 20 42 75 74 20 77 65 20 77 65 72 65 0a 20 20 20   But we were.   
10480 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20     ** allocated 
10481 70 67 6e 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71  pgnoMove. If req
10482 75 69 72 65 64 20 28 69 2e 65 2e 20 69 66 20 69  uired (i.e. if i
10483 74 20 77 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61  t was not alloca
10484 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20  ted.      ** by 
10485 65 78 74 65 6e 64 69 6e 67 20 74 68 65 20 66 69  extending the fi
10486 6c 65 29 2c 20 74 68 65 20 63 75 72 72 65 6e 74  le), the current
10487 20 70 61 67 65 20 61 74 20 70 6f 73 69 74 69 6f   page at positio
10488 6e 20 70 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20  n pgnoMove.     
10489 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 6a   ** is already j
1048a 6f 75 72 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20  ournaled..      
1048b 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 54 79 70  */.      u8 eTyp
1048c 65 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50  e;.      Pgno iP
1048d 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 72  trPage;..      r
1048e 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
1048f 4d 6f 76 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  Move);..      /*
10490 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 63   Move the page c
10491 75 72 72 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f  urrently at pgno
10492 52 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65  Root to pgnoMove
10493 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  . */.      rc = 
10494 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
10495 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f  age(pBt, pgnoRoo
10496 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20  t, &pRoot, 0);. 
10497 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
10498 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
10499 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1049a 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
1049b 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 70  ptrmapGet(pBt, p
1049c 67 6e 6f 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c  gnoRoot, &eType,
1049d 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20   &iPtrPage);.   
1049e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1049f 45 5f 4f 4b 20 7c 7c 20 65 54 79 70 65 3d 3d 50  E_OK || eType==P
104a0 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c  TRMAP_ROOTPAGE |
104a1 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  | eType==PTRMAP_
104a2 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20  FREEPAGE ){.    
104a3 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
104a4 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20  pRoot);.        
104a5 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
104a6 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
104a7 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52   eType!=PTRMAP_R
104a8 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20 20  OOTPAGE );.     
104a9 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d   assert( eType!=
104aa 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
104ab 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
104ac 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
104ad 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b  pRoot->pDbPage);
104ae 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
104af 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
104b0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
104b1 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20  pRoot);.        
104b2 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
104b3 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65   }.      rc = re
104b4 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20  locatePage(pBt, 
104b5 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 50  pRoot, eType, iP
104b6 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65  trPage, pgnoMove
104b7 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  );.      release
104b8 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20  Page(pRoot);..  
104b9 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68      /* Obtain th
104ba 65 20 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f  e page at pgnoRo
104bb 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ot */.      if( 
104bc 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
104bd 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
104be 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
104bf 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
104c0 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
104c1 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74  pgnoRoot, &pRoot
104c2 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
104c3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
104c4 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
104c5 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
104c6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
104c7 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e  gerWrite(pRoot->
104c8 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
104c9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
104ca 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  K ){.        rel
104cb 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
104cc 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
104cd 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
104ce 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f  }else{.      pRo
104cf 6f 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a  ot = pPageMove;.
104d0 20 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55      } ..    /* U
104d1 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65  pdate the pointe
104d2 72 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64  r-map and meta-d
104d3 61 74 61 20 77 69 74 68 20 74 68 65 20 6e 65 77  ata with the new
104d4 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
104d5 72 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70  r. */.    rc = p
104d6 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67  trmapPut(pBt, pg
104d7 6e 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52  noRoot, PTRMAP_R
104d8 4f 4f 54 50 41 47 45 2c 20 30 29 3b 0a 20 20 20  OOTPAGE, 0);.   
104d9 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
104da 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
104db 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ot);.      retur
104dc 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
104dd 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
104de 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34  eUpdateMeta(p, 4
104df 2c 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20  , pgnoRoot);.   
104e0 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
104e1 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
104e2 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ot);.      retur
104e3 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d  n rc;.    }..  }
104e4 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61  else{.    rc = a
104e5 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65  llocateBtreePage
104e6 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70  (pBt, &pRoot, &p
104e7 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a  gnoRoot, 1, 0);.
104e8 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
104e9 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64  urn rc;.  }.#end
104ea 69 66 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  if.  assert( sql
104eb 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
104ec 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50  able(pRoot->pDbP
104ed 61 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61  age) );.  zeroPa
104ee 67 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20  ge(pRoot, flags 
104ef 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73  | PTF_LEAF);.  s
104f0 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
104f1 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
104f2 3b 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28  ;.  *piTable = (
104f3 69 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20  int)pgnoRoot;.  
104f4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
104f5 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
104f6 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
104f7 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42  reeCreateTable(B
104f8 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69  tree *p, int *pi
104f9 54 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73  Table, int flags
104fa 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
104fb 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
104fc 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65  (p);.  rc = btre
104fd 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c 20  eCreateTable(p, 
104fe 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b  piTable, flags);
104ff 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
10500 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
10501 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  n rc;.}../*.** E
10502 72 61 73 65 20 74 68 65 20 67 69 76 65 6e 20 64  rase the given d
10503 61 74 61 62 61 73 65 20 70 61 67 65 20 61 6e 64  atabase page and
10504 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 65   all its childre
10505 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68  n.  Return.** th
10506 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 66 72  e page to the fr
10507 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  eelist..*/.stati
10508 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61 62  c int clearDatab
10509 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 68 61  asePage(.  BtSha
1050a 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20  red *pBt,       
1050b 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65      /* The BTree
1050c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
1050d 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67  he table */.  Pg
1050e 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20  no pgno,        
1050f 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
10510 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20  er to clear */. 
10511 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e   MemPage *pParen
10512 74 2c 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74  t,     /* Parent
10513 20 70 61 67 65 2e 20 20 4e 55 4c 4c 20 66 6f 72   page.  NULL for
10514 20 74 68 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 69   the root */.  i
10515 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61 67 20  nt freePageFlag 
10516 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61       /* Dealloca
10517 74 65 20 70 61 67 65 20 69 66 20 74 72 75 65 20  te page if true 
10518 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  */.){.  MemPage 
10519 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e  *pPage = 0;.  in
1051a 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64  t rc;.  unsigned
1051b 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20   char *pCell;.  
1051c 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74  int i;..  assert
1051d 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1051e 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
1051f 20 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e 73   );.  if( pgno>s
10520 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
10521 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
10522 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
10523 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
10524 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d  KPT;.  }..  rc =
10525 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
10526 70 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67  pBt, pgno, &pPag
10527 65 2c 20 70 50 61 72 65 6e 74 29 3b 0a 20 20 69  e, pParent);.  i
10528 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65  f( rc ) goto cle
10529 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f  ardatabasepage_o
1052a 75 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ut;.  for(i=0; i
1052b 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69  <pPage->nCell; i
1052c 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c 20 3d  ++){.    pCell =
1052d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
1052e 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50   i);.    if( !pP
1052f 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
10530 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74     rc = clearDat
10531 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 67  abasePage(pBt, g
10532 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c 20  et4byte(pCell), 
10533 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 2c 20  pPage->pParent, 
10534 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  1);.      if( rc
10535 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74   ) goto cleardat
10536 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20  abasepage_out;. 
10537 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c     }.    rc = cl
10538 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70  earCell(pPage, p
10539 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  Cell);.    if( r
1053a 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61  c ) goto clearda
1053b 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a  tabasepage_out;.
1053c 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65    }.  if( !pPage
1053d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 63  ->leaf ){.    rc
1053e 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65   = clearDatabase
1053f 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79  Page(pBt, get4by
10540 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
10541 5b 38 5d 29 2c 20 70 50 61 67 65 2d 3e 70 50 61  [8]), pPage->pPa
10542 72 65 6e 74 2c 20 31 29 3b 0a 20 20 20 20 69 66  rent, 1);.    if
10543 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61  ( rc ) goto clea
10544 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
10545 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 72 65  t;.  }.  if( fre
10546 65 50 61 67 65 46 6c 61 67 20 29 7b 0a 20 20 20  ePageFlag ){.   
10547 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70   rc = freePage(p
10548 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69  Page);.  }else i
10549 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  f( (rc = sqlite3
1054a 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
1054b 2d 3e 70 44 62 50 61 67 65 29 29 3d 3d 30 20 29  ->pDbPage))==0 )
1054c 7b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70  {.    zeroPage(p
1054d 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 61 44 61  Page, pPage->aDa
1054e 74 61 5b 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46  ta[0] | PTF_LEAF
1054f 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61 72 64 61 74  );.  }..cleardat
10550 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3a 0a 20  abasepage_out:. 
10551 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
10552 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ge);.  return rc
10553 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
10554 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  e all informatio
10555 6e 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20  n from a single 
10556 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74  table in the dat
10557 61 62 61 73 65 2e 20 20 69 54 61 62 6c 65 20 69  abase.  iTable i
10558 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75  s.** the page nu
10559 6d 62 65 72 20 6f 66 20 74 68 65 20 72 6f 6f 74  mber of the root
1055a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
1055b 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69  After this routi
1055c 6e 65 20 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 74  ne returns,.** t
1055d 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20  he root page is 
1055e 65 6d 70 74 79 2c 20 62 75 74 20 73 74 69 6c 6c  empty, but still
1055f 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54   exists..**.** T
10560 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
10561 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54   fail with SQLIT
10562 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72  E_LOCKED if ther
10563 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a  e are any open.*
10564 2a 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 6f  * read cursors o
10565 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 4f 70  n the table.  Op
10566 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72 73  en write cursors
10567 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68   are moved to th
10568 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66 20 74 68 65  e.** root of the
10569 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
1056a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1056b 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
1056c 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69  able(Btree *p, i
1056d 6e 74 20 69 54 61 62 6c 65 29 7b 0a 20 20 69 6e  nt iTable){.  in
1056e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64  t rc;.  BtShared
1056f 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
10570 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
10571 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 2d  ter(p);.  if( p-
10572 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f  >inTrans!=TRANS_
10573 57 52 49 54 45 20 29 7b 0a 20 20 20 20 72 63 20  WRITE ){.    rc 
10574 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  = pBt->readOnly 
10575 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  ? SQLITE_READONL
10576 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  Y : SQLITE_ERROR
10577 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72  ;.  }else if( (r
10578 63 20 3d 20 63 68 65 63 6b 52 65 61 64 4c 6f 63  c = checkReadLoc
10579 6b 73 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 29  ks(p, iTable, 0)
1057a 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  )!=SQLITE_OK ){.
1057b 20 20 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74      /* nothing t
1057c 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65 6c 73 65 20  o do */.  }else 
1057d 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
1057e 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73  rc = saveAllCurs
1057f 6f 72 73 28 70 42 74 2c 20 69 54 61 62 6c 65 2c  ors(pBt, iTable,
10580 20 30 29 29 20 29 7b 0a 20 20 20 20 2f 2a 20 6e   0)) ){.    /* n
10581 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a  othing to do */.
10582 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
10583 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50  = clearDatabaseP
10584 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69  age(pBt, (Pgno)i
10585 54 61 62 6c 65 2c 20 30 2c 20 30 29 3b 0a 20 20  Table, 0, 0);.  
10586 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
10587 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
10588 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
10589 45 72 61 73 65 20 61 6c 6c 20 69 6e 66 6f 72 6d  Erase all inform
1058a 61 74 69 6f 6e 20 69 6e 20 61 20 74 61 62 6c 65  ation in a table
1058b 20 61 6e 64 20 61 64 64 20 74 68 65 20 72 6f 6f   and add the roo
1058c 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  t of the table t
1058d 6f 0a 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73  o.** the freelis
1058e 74 2e 20 20 45 78 63 65 70 74 2c 20 74 68 65 20  t.  Except, the 
1058f 72 6f 6f 74 20 6f 66 20 74 68 65 20 70 72 69 6e  root of the prin
10590 63 69 70 6c 65 20 74 61 62 6c 65 20 28 74 68 65  ciple table (the
10591 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 67 65 20   one on.** page 
10592 31 29 20 69 73 20 6e 65 76 65 72 20 61 64 64 65  1) is never adde
10593 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73  d to the freelis
10594 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  t..**.** This ro
10595 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20  utine will fail 
10596 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  with SQLITE_LOCK
10597 45 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ED if there are 
10598 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 75 72 73  any open.** curs
10599 6f 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ors on the table
1059a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56  ..**.** If AUTOV
1059b 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64  ACUUM is enabled
1059c 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 61 74   and the page at
1059d 20 69 54 61 62 6c 65 20 69 73 20 6e 6f 74 20 74   iTable is not t
1059e 68 65 20 6c 61 73 74 0a 2a 2a 20 72 6f 6f 74 20  he last.** root 
1059f 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  page in the data
105a0 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20  base file, then 
105a1 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61  the last root pa
105a2 67 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61  ge .** in the da
105a3 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d  tabase file is m
105a4 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 73 6c  oved into the sl
105a5 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 75  ot formerly occu
105a6 70 69 65 64 20 62 79 0a 2a 2a 20 69 54 61 62 6c  pied by.** iTabl
105a7 65 20 61 6e 64 20 74 68 61 74 20 6c 61 73 74 20  e and that last 
105a8 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63  slot formerly oc
105a9 63 75 70 69 65 64 20 62 79 20 74 68 65 20 6c 61  cupied by the la
105aa 73 74 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20  st root page.** 
105ab 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  is added to the 
105ac 66 72 65 65 6c 69 73 74 20 69 6e 73 74 65 61 64  freelist instead
105ad 20 6f 66 20 69 54 61 62 6c 65 2e 20 20 49 6e 20   of iTable.  In 
105ae 74 68 69 73 20 73 61 79 2c 20 61 6c 6c 0a 2a 2a  this say, all.**
105af 20 72 6f 6f 74 20 70 61 67 65 73 20 61 72 65 20   root pages are 
105b0 6b 65 70 74 20 61 74 20 74 68 65 20 62 65 67 69  kept at the begi
105b1 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74  nning of the dat
105b2 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63  abase file, whic
105b3 68 0a 2a 2a 20 69 73 20 6e 65 63 65 73 73 61 72  h.** is necessar
105b4 79 20 66 6f 72 20 41 55 54 4f 56 41 43 55 55 4d  y for AUTOVACUUM
105b5 20 74 6f 20 77 6f 72 6b 20 72 69 67 68 74 2e 20   to work right. 
105b6 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 74   *piMoved is set
105b7 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 61 67 65   to the .** page
105b8 20 6e 75 6d 62 65 72 20 74 68 61 74 20 75 73 65   number that use
105b9 64 20 74 6f 20 62 65 20 74 68 65 20 6c 61 73 74  d to be the last
105ba 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68   root page in th
105bb 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 2a 2a  e file before.**
105bc 20 74 68 65 20 6d 6f 76 65 2e 20 20 49 66 20 6e   the move.  If n
105bd 6f 20 70 61 67 65 20 67 65 74 73 20 6d 6f 76 65  o page gets move
105be 64 2c 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 73  d, *piMoved is s
105bf 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 65 20  et to 0..** The 
105c0 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69  last root page i
105c1 73 20 72 65 63 6f 72 64 65 64 20 69 6e 20 6d 65  s recorded in me
105c2 74 61 5b 33 5d 20 61 6e 64 20 74 68 65 20 76 61  ta[3] and the va
105c3 6c 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 61 5b 33  lue of.** meta[3
105c4 5d 20 69 73 20 75 70 64 61 74 65 64 20 62 79 20  ] is updated by 
105c5 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 0a  this procedure..
105c6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74  */.static int bt
105c7 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72  reeDropTable(Btr
105c8 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c  ee *p, int iTabl
105c9 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29  e, int *piMoved)
105ca 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
105cb 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30  mPage *pPage = 0
105cc 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
105cd 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61  t = p->pBt;..  a
105ce 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
105cf 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29  reeHoldsMutex(p)
105d0 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54   );.  if( p->inT
105d1 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54  rans!=TRANS_WRIT
105d2 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  E ){.    return 
105d3 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20  pBt->readOnly ? 
105d4 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20  SQLITE_READONLY 
105d5 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  : SQLITE_ERROR;.
105d6 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20    }..  /* It is 
105d7 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20  illegal to drop 
105d8 61 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63  a table if any c
105d9 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20  ursors are open 
105da 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  on the.  ** data
105db 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 62 65  base. This is be
105dc 63 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61  cause in auto-va
105dd 63 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61  cuum mode the ba
105de 63 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e  ckend may.  ** n
105df 65 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74  eed to move anot
105e0 68 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f  her root-page to
105e1 20 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74   fill a gap left
105e2 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a   by the deleted.
105e3 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20    ** root page. 
105e4 49 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f  If an open curso
105e5 72 20 77 61 73 20 75 73 69 6e 67 20 74 68 69 73  r was using this
105e6 20 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20   page a problem 
105e7 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75  would .  ** occu
105e8 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42  r..  */.  if( pB
105e9 74 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20  t->pCursor ){.  
105ea 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
105eb 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 72  LOCKED;.  }..  r
105ec 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
105ed 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 67  GetPage(pBt, (Pg
105ee 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50 61 67  no)iTable, &pPag
105ef 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  e, 0);.  if( rc 
105f0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
105f1 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
105f2 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20 69 54  ClearTable(p, iT
105f3 61 62 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 20  able);.  if( rc 
105f4 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
105f5 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72  ge(pPage);.    r
105f6 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
105f7 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a   *piMoved = 0;..
105f8 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29    if( iTable>1 )
105f9 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
105fa 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
105fb 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67      rc = freePag
105fc 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65  e(pPage);.    re
105fd 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
105fe 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20  ;.#else.    if( 
105ff 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
10600 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61  ){.      Pgno ma
10601 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20  xRootPgno;.     
10602 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
10603 65 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20  eeGetMeta(p, 4, 
10604 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20  &maxRootPgno);. 
10605 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
10606 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
10607 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
10608 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65  age);.        re
10609 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
1060a 0a 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62  ..      if( iTab
1060b 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20  le==maxRootPgno 
1060c 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
1060d 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
1060e 20 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20   dropped is the 
1060f 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c  table with the l
10610 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65  argest root-page
10611 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62  .        ** numb
10612 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  er in the databa
10613 73 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74  se, put the root
10614 20 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65   page on the fre
10615 65 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20  e list. .       
10616 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d   */.        rc =
10617 20 66 72 65 65 50 61 67 65 28 70 50 61 67 65 29   freePage(pPage)
10618 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
10619 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
1061a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1061b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1061c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1061d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1061e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
1061f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e  * The table bein
10620 67 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e  g dropped does n
10621 6f 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67  ot have the larg
10622 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20  est root-page.  
10623 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
10624 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
10625 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67   So move the pag
10626 65 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f  e that does into
10627 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a   the .        **
10628 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65   gap left by the
10629 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61   deleted root-pa
1062a 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ge..        */. 
1062b 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a         MemPage *
1062c 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72  pMove;.        r
1062d 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
1062e 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
1062f 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
10630 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74  age(pBt, maxRoot
10631 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29  Pgno, &pMove, 0)
10632 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
10633 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
10634 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
10635 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
10636 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63        rc = reloc
10637 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 4d 6f  atePage(pBt, pMo
10638 76 65 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50  ve, PTRMAP_ROOTP
10639 41 47 45 2c 20 30 2c 20 69 54 61 62 6c 65 29 3b  AGE, 0, iTable);
1063a 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
1063b 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20  Page(pMove);.   
1063c 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1063d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1063e 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1063f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
10640 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
10641 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6d  eeGetPage(pBt, m
10642 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f  axRootPgno, &pMo
10643 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ve, 0);.        
10644 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
10645 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
10646 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
10647 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d    }.        rc =
10648 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76 65 29   freePage(pMove)
10649 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
1064a 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20  ePage(pMove);.  
1064b 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1064c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1064d 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1064e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1064f 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78    *piMoved = max
10650 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20  RootPgno;.      
10651 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20  }..      /* Set 
10652 74 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f  the new 'max-roo
10653 74 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e  t-page' value in
10654 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65   the database he
10655 61 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20  ader. This.     
10656 20 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76   ** is the old v
10657 61 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c  alue less one, l
10658 65 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20  ess one more if 
10659 74 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a  that happens to.
1065a 20 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f        ** be a ro
1065b 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20  ot-page number, 
1065c 6c 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69  less one again i
1065d 66 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20  f that is the.  
1065e 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42      ** PENDING_B
1065f 59 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20  YTE_PAGE..      
10660 2a 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74  */.      maxRoot
10661 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 69 66  Pgno--;.      if
10662 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50  ( maxRootPgno==P
10663 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
10664 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  (pBt) ){.       
10665 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a   maxRootPgno--;.
10666 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
10667 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50  ( maxRootPgno==P
10668 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
10669 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20 29  , maxRootPgno) )
1066a 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f 6f  {.        maxRoo
1066b 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d  tPgno--;.      }
1066c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d  .      assert( m
1066d 61 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e 44  axRootPgno!=PEND
1066e 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
1066f 74 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63 20  t) );..      rc 
10670 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70  = sqlite3BtreeUp
10671 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 6d  dateMeta(p, 4, m
10672 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20  axRootPgno);.   
10673 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
10674 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67   = freePage(pPag
10675 65 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73  e);.      releas
10676 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
10677 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c    }.#endif.  }el
10678 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71  se{.    /* If sq
10679 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61  lite3BtreeDropTa
1067a 62 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20 6f  ble was called o
1067b 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20 20 20  n page 1. */.   
1067c 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c   zeroPage(pPage,
1067d 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f   PTF_INTKEY|PTF_
1067e 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65 6c 65  LEAF );.    rele
1067f 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
10680 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
10681 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56    .}.SQLITE_PRIV
10682 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
10683 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74  treeDropTable(Bt
10684 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62  ree *p, int iTab
10685 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64  le, int *piMoved
10686 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
10687 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
10688 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65  (p);.  rc = btre
10689 65 44 72 6f 70 54 61 62 6c 65 28 70 2c 20 69 54  eDropTable(p, iT
1068a 61 62 6c 65 2c 20 70 69 4d 6f 76 65 64 29 3b 0a  able, piMoved);.
1068b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1068c 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1068d 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52   rc;.}.../*.** R
1068e 65 61 64 20 74 68 65 20 6d 65 74 61 2d 69 6e 66  ead the meta-inf
1068f 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 6f 66 20  ormation out of 
10690 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
10691 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20    Meta[0].** is 
10692 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72  the number of fr
10693 65 65 20 70 61 67 65 73 20 63 75 72 72 65 6e 74  ee pages current
10694 6c 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ly in the databa
10695 73 65 2e 20 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20  se.  Meta[1].** 
10696 74 68 72 6f 75 67 68 20 6d 65 74 61 5b 31 35 5d  through meta[15]
10697 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 66   are available f
10698 6f 72 20 75 73 65 20 62 79 20 68 69 67 68 65 72  or use by higher
10699 20 6c 61 79 65 72 73 2e 20 20 4d 65 74 61 5b 30   layers.  Meta[0
1069a 5d 0a 2a 2a 20 69 73 20 72 65 61 64 2d 6f 6e 6c  ].** is read-onl
1069b 79 2c 20 74 68 65 20 6f 74 68 65 72 73 20 61 72  y, the others ar
1069c 65 20 72 65 61 64 2f 77 72 69 74 65 2e 0a 2a 2a  e read/write..**
1069d 20 0a 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 20   .** The schema 
1069e 6c 61 79 65 72 20 6e 75 6d 62 65 72 73 20 6d 65  layer numbers me
1069f 74 61 20 76 61 6c 75 65 73 20 64 69 66 66 65 72  ta values differ
106a0 65 6e 74 6c 79 2e 20 20 41 74 20 74 68 65 20 73  ently.  At the s
106a1 63 68 65 6d 61 0a 2a 2a 20 6c 61 79 65 72 20 28  chema.** layer (
106a2 61 6e 64 20 74 68 65 20 53 65 74 43 6f 6f 6b 69  and the SetCooki
106a3 65 20 61 6e 64 20 52 65 61 64 43 6f 6f 6b 69 65  e and ReadCookie
106a4 20 6f 70 63 6f 64 65 73 29 20 74 68 65 20 6e 75   opcodes) the nu
106a5 6d 62 65 72 20 6f 66 0a 2a 2a 20 66 72 65 65 20  mber of.** free 
106a6 70 61 67 65 73 20 69 73 20 6e 6f 74 20 76 69 73  pages is not vis
106a7 69 62 6c 65 2e 20 20 53 6f 20 43 6f 6f 6b 69 65  ible.  So Cookie
106a8 5b 30 5d 20 69 73 20 74 68 65 20 73 61 6d 65 20  [0] is the same 
106a9 61 73 20 4d 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53  as Meta[1]..*/.S
106aa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
106ab 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
106ac 74 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20  tMeta(Btree *p, 
106ad 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a 70 4d  int idx, u32 *pM
106ae 65 74 61 29 7b 0a 20 20 44 62 50 61 67 65 20 2a  eta){.  DbPage *
106af 70 44 62 50 61 67 65 3b 0a 20 20 69 6e 74 20 72  pDbPage;.  int r
106b0 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  c;.  unsigned ch
106b1 61 72 20 2a 70 50 31 3b 0a 20 20 42 74 53 68 61  ar *pP1;.  BtSha
106b2 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
106b3 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72  t;..  sqlite3Btr
106b4 65 65 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 2f  eeEnter(p);..  /
106b5 2a 20 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61  * Reading a meta
106b6 2d 64 61 74 61 20 76 61 6c 75 65 20 72 65 71 75  -data value requ
106b7 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  ires a read-lock
106b8 20 6f 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20   on page 1 (and 
106b9 68 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73  hence.  ** the s
106ba 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
106bb 6c 65 2e 20 57 65 20 67 72 61 62 20 74 68 69 73  le. We grab this
106bc 20 6c 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73   lock regardless
106bd 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20   of whether or. 
106be 20 2a 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49   ** not the SQLI
106bf 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
106c0 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 28  ed flag is set (
106c1 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64  the table rooted
106c2 20 61 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20   at page.  ** 1 
106c3 69 73 20 74 72 65 61 74 65 64 20 61 73 20 61 20  is treated as a 
106c4 73 70 65 63 69 61 6c 20 63 61 73 65 20 62 79 20  special case by 
106c5 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 29  queryTableLock()
106c6 20 61 6e 64 20 6c 6f 63 6b 54 61 62 6c 65 28 29   and lockTable()
106c7 29 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 71  )..  */.  rc = q
106c8 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ueryTableLock(p,
106c9 20 31 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a   1, READ_LOCK);.
106ca 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
106cb 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
106cc 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
106cd 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
106ce 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 69    }..  assert( i
106cf 64 78 3e 3d 30 20 26 26 20 69 64 78 3c 3d 31 35  dx>=0 && idx<=15
106d0 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
106d1 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e  e3PagerGet(pBt->
106d2 70 50 61 67 65 72 2c 20 31 2c 20 26 70 44 62 50  pPager, 1, &pDbP
106d3 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
106d4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
106d5 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20  eeLeave(p);.    
106d6 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
106d7 20 70 50 31 20 3d 20 28 75 6e 73 69 67 6e 65 64   pP1 = (unsigned
106d8 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50   char *)sqlite3P
106d9 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
106da 61 67 65 29 3b 0a 20 20 2a 70 4d 65 74 61 20 3d  age);.  *pMeta =
106db 20 67 65 74 34 62 79 74 65 28 26 70 50 31 5b 33   get4byte(&pP1[3
106dc 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 20 20 73  6 + idx*4]);.  s
106dd 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
106de 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 2f 2a  (pDbPage);..  /*
106df 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d 65 64   If autovacuumed
106e0 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20   is disabled in 
106e1 74 68 69 73 20 62 75 69 6c 64 20 62 75 74 20 77  this build but w
106e2 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20  e are trying to 
106e3 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61 6e 20  .  ** access an 
106e4 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64 61 74  autovacuumed dat
106e5 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61 6b 65  abase, then make
106e6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65   the database re
106e7 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a 23 69  adonly. .  */.#i
106e8 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
106e9 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66  _AUTOVACUUM.  if
106ea 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70 4d 65  ( idx==4 && *pMe
106eb 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 61 64  ta>0 ) pBt->read
106ec 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 69 66  Only = 1;.#endif
106ed 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20  ..  /* Grab the 
106ee 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70 61 67  read-lock on pag
106ef 65 20 31 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 6c  e 1. */.  rc = l
106f0 6f 63 6b 54 61 62 6c 65 28 70 2c 20 31 2c 20 52  ockTable(p, 1, R
106f1 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 73 71 6c  EAD_LOCK);.  sql
106f2 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
106f3 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
106f4 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d  }../*.** Write m
106f5 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
106f6 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61  back into the da
106f7 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d  tabase.  Meta[0]
106f8 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79   is.** read-only
106f9 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20   and may not be 
106fa 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49  written..*/.SQLI
106fb 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
106fc 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
106fd 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20  eMeta(Btree *p, 
106fe 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65  int idx, u32 iMe
106ff 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ta){.  BtShared 
10700 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
10701 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
10702 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  pP1;.  int rc;. 
10703 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20   assert( idx>=1 
10704 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20  && idx<=15 );.  
10705 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
10706 72 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69  r(p);.  if( p->i
10707 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52  nTrans!=TRANS_WR
10708 49 54 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ITE ){.    rc = 
10709 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20  pBt->readOnly ? 
1070a 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20  SQLITE_READONLY 
1070b 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  : SQLITE_ERROR;.
1070c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1070d 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ert( pBt->pPage1
1070e 21 3d 30 20 29 3b 0a 20 20 20 20 70 50 31 20 3d  !=0 );.    pP1 =
1070f 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44   pBt->pPage1->aD
10710 61 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  ata;.    rc = sq
10711 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
10712 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62  pBt->pPage1->pDb
10713 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
10714 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
10715 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
10716 70 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d 2c  pP1[36 + idx*4],
10717 20 69 4d 65 74 61 29 3b 0a 23 69 66 6e 64 65 66   iMeta);.#ifndef
10718 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
10719 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66  OVACUUM.      if
1071a 28 20 69 64 78 3d 3d 37 20 29 7b 0a 20 20 20 20  ( idx==7 ){.    
1071b 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d      assert( pBt-
1071c 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c 7c 20 69  >autoVacuum || i
1071d 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Meta==0 );.     
1071e 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74 61     assert( iMeta
1071f 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 20  ==0 || iMeta==1 
10720 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  );.        pBt->
10721 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 69 4d 65  incrVacuum = iMe
10722 74 61 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  ta;.      }.#end
10723 69 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  if.    }.  }.  s
10724 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
10725 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
10726 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
10727 6e 20 74 68 65 20 66 6c 61 67 20 62 79 74 65 20  n the flag byte 
10728 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
10729 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 68 61   of the page tha
1072a 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  t the cursor.** 
1072b 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
1072c 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2f 0a 53 51 4c  nting to..*/.SQL
1072d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1072e 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67  sqlite3BtreeFlag
1072f 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  s(BtCursor *pCur
10730 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68  ){.  /* TODO: Wh
10731 61 74 20 61 62 6f 75 74 20 43 55 52 53 4f 52 5f  at about CURSOR_
10732 52 45 51 55 49 52 45 53 45 45 4b 20 73 74 61 74  REQUIRESEEK stat
10733 65 3f 20 50 72 6f 62 61 62 6c 79 20 6e 65 65 64  e? Probably need
10734 20 74 6f 20 63 61 6c 6c 0a 20 20 2a 2a 20 72 65   to call.  ** re
10735 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73  storeOrClearCurs
10736 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 68 65 72  orPosition() her
10737 65 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  e..  */.  MemPag
10738 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d  e *pPage = pCur-
10739 3e 70 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74  >pPage;.  assert
1073a 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
1073b 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
1073c 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74  sert( pPage->pBt
1073d 3d 3d 70 43 75 72 2d 3e 70 42 74 20 29 3b 0a 20  ==pCur->pBt );. 
1073e 20 72 65 74 75 72 6e 20 70 50 61 67 65 20 3f 20   return pPage ? 
1073f 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
10740 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 5d 20 3a  ge->hdrOffset] :
10741 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65   0;.}.../*.** Re
10742 74 75 72 6e 20 74 68 65 20 70 61 67 65 72 20 61  turn the pager a
10743 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
10744 20 42 54 72 65 65 2e 20 20 54 68 69 73 20 72 6f   BTree.  This ro
10745 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
10746 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64  r.** testing and
10747 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
10748 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10749 54 45 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65  TE Pager *sqlite
1074a 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 65  3BtreePager(Btre
1074b 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  e *p){.  return 
1074c 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a  p->pBt->pPager;.
1074d 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1074e 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
1074f 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 70 70  _CHECK./*.** App
10750 65 6e 64 20 61 20 6d 65 73 73 61 67 65 20 74 6f  end a message to
10751 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
10752 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74  ge string..*/.st
10753 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41  atic void checkA
10754 70 70 65 6e 64 4d 73 67 28 0a 20 20 49 6e 74 65  ppendMsg(.  Inte
10755 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c  grityCk *pCheck,
10756 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 31 2c 0a  .  char *zMsg1,.
10757 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
10758 6f 72 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a  ormat,.  ....){.
10759 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
1075a 63 68 61 72 20 2a 7a 4d 73 67 32 3b 0a 20 20 69  char *zMsg2;.  i
1075b 66 28 20 21 70 43 68 65 63 6b 2d 3e 6d 78 45 72  f( !pCheck->mxEr
1075c 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43  r ) return;.  pC
1075d 68 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b 0a 20  heck->mxErr--;. 
1075e 20 70 43 68 65 63 6b 2d 3e 6e 45 72 72 2b 2b 3b   pCheck->nErr++;
1075f 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
10760 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 4d 73 67  zFormat);.  zMsg
10761 32 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  2 = sqlite3VMPri
10762 6e 74 66 28 30 2c 20 7a 46 6f 72 6d 61 74 2c 20  ntf(0, zFormat, 
10763 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
10764 29 3b 0a 20 20 69 66 28 20 7a 4d 73 67 31 3d 3d  );.  if( zMsg1==
10765 30 20 29 20 7a 4d 73 67 31 20 3d 20 22 22 3b 0a  0 ) zMsg1 = "";.
10766 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 7a 45    if( pCheck->zE
10767 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 63 68 61  rrMsg ){.    cha
10768 72 20 2a 7a 4f 6c 64 20 3d 20 70 43 68 65 63 6b  r *zOld = pCheck
10769 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70  ->zErrMsg;.    p
1076a 43 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67 20 3d  Check->zErrMsg =
1076b 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   0;.    sqlite3S
1076c 65 74 53 74 72 69 6e 67 28 26 70 43 68 65 63 6b  etString(&pCheck
1076d 2d 3e 7a 45 72 72 4d 73 67 2c 20 7a 4f 6c 64 2c  ->zErrMsg, zOld,
1076e 20 22 5c 6e 22 2c 20 7a 4d 73 67 31 2c 20 7a 4d   "\n", zMsg1, zM
1076f 73 67 32 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a  sg2, (char*)0);.
10770 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
10771 28 7a 4f 6c 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  (zOld);.  }else{
10772 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
10773 74 72 69 6e 67 28 26 70 43 68 65 63 6b 2d 3e 7a  tring(&pCheck->z
10774 45 72 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 7a  ErrMsg, zMsg1, z
10775 4d 73 67 32 2c 20 28 63 68 61 72 2a 29 30 29 3b  Msg2, (char*)0);
10776 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
10777 72 65 65 28 7a 4d 73 67 32 29 3b 0a 7d 0a 23 65  ree(zMsg2);.}.#e
10778 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
10779 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
1077a 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ECK */..#ifndef 
1077b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
1077c 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a  GRITY_CHECK./*.*
1077d 2a 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72  * Add 1 to the r
1077e 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66  eference count f
1077f 6f 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20  or page iPage.  
10780 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73  If this is the s
10781 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e  econd.** referen
10782 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20  ce to the page, 
10783 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  add an error mes
10784 73 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e  sage to pCheck->
10785 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75  zErrMsg..** Retu
10786 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72  rn 1 if there ar
10787 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66  e 2 ore more ref
10788 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70  erences to the p
10789 61 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20  age and 0 if.** 
1078a 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
1078b 69 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74  irst reference t
1078c 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a  o the page..**.*
1078d 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61  * Also check tha
1078e 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  t the page numbe
1078f 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a  r is in bounds..
10790 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68  */.static int ch
10791 65 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79  eckRef(Integrity
10792 43 6b 20 2a 70 43 68 65 63 6b 2c 20 69 6e 74 20  Ck *pCheck, int 
10793 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f  iPage, char *zCo
10794 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50  ntext){.  if( iP
10795 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  age==0 ) return 
10796 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70  1;.  if( iPage>p
10797 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 7c 7c 20  Check->nPage || 
10798 69 50 61 67 65 3c 30 20 29 7b 0a 20 20 20 20 63  iPage<0 ){.    c
10799 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
1079a 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
1079b 22 69 6e 76 61 6c 69 64 20 70 61 67 65 20 6e 75  "invalid page nu
1079c 6d 62 65 72 20 25 64 22 2c 20 69 50 61 67 65 29  mber %d", iPage)
1079d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
1079e 20 20 7d 0a 20 20 69 66 28 20 70 43 68 65 63 6b    }.  if( pCheck
1079f 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 3d 3d  ->anRef[iPage]==
107a0 31 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70  1 ){.    checkAp
107a1 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
107a2 7a 43 6f 6e 74 65 78 74 2c 20 22 32 6e 64 20 72  zContext, "2nd r
107a3 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65  eference to page
107a4 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20   %d", iPage);.  
107a5 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
107a6 20 20 72 65 74 75 72 6e 20 20 28 70 43 68 65 63    return  (pChec
107a7 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 2b  k->anRef[iPage]+
107a8 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  +)>1;.}..#ifndef
107a9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
107aa 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 68  OVACUUM./*.** Ch
107ab 65 63 6b 20 74 68 61 74 20 74 68 65 20 65 6e 74  eck that the ent
107ac 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65  ry in the pointe
107ad 72 2d 6d 61 70 20 66 6f 72 20 70 61 67 65 20 69  r-map for page i
107ae 43 68 69 6c 64 20 6d 61 70 73 20 74 6f 20 0a 2a  Child maps to .*
107af 2a 20 70 61 67 65 20 69 50 61 72 65 6e 74 2c 20  * page iParent, 
107b0 70 6f 69 6e 74 65 72 20 74 79 70 65 20 70 74 72  pointer type ptr
107b1 54 79 70 65 2e 20 49 66 20 6e 6f 74 2c 20 61 70  Type. If not, ap
107b2 70 65 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65  pend an error me
107b3 73 73 61 67 65 0a 2a 2a 20 74 6f 20 70 43 68 65  ssage.** to pChe
107b4 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ck..*/.static vo
107b5 69 64 20 63 68 65 63 6b 50 74 72 6d 61 70 28 0a  id checkPtrmap(.
107b6 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70    IntegrityCk *p
107b7 43 68 65 63 6b 2c 20 20 20 2f 2a 20 49 6e 74 65  Check,   /* Inte
107b8 67 72 69 74 79 20 63 68 65 63 6b 20 63 6f 6e 74  grity check cont
107b9 65 78 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 43  ext */.  Pgno iC
107ba 68 69 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20  hild,           
107bb 2f 2a 20 43 68 69 6c 64 20 70 61 67 65 20 6e 75  /* Child page nu
107bc 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 54 79  mber */.  u8 eTy
107bd 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pe,             
107be 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69   /* Expected poi
107bf 6e 74 65 72 20 6d 61 70 20 74 79 70 65 20 2a 2f  nter map type */
107c0 0a 20 20 50 67 6e 6f 20 69 50 61 72 65 6e 74 2c  .  Pgno iParent,
107c1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
107c2 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61  ected pointer ma
107c3 70 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75  p parent page nu
107c4 6d 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a  mber */.  char *
107c5 7a 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20  zContext        
107c6 20 2f 2a 20 43 6f 6e 74 65 78 74 20 64 65 73 63   /* Context desc
107c7 72 69 70 74 69 6f 6e 20 28 75 73 65 64 20 66 6f  ription (used fo
107c8 72 20 65 72 72 6f 72 20 6d 73 67 29 20 2a 2f 0a  r error msg) */.
107c9 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75  ){.  int rc;.  u
107ca 38 20 65 50 74 72 6d 61 70 54 79 70 65 3b 0a 20  8 ePtrmapType;. 
107cb 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 50 61 72   Pgno iPtrmapPar
107cc 65 6e 74 3b 0a 0a 20 20 72 63 20 3d 20 70 74 72  ent;..  rc = ptr
107cd 6d 61 70 47 65 74 28 70 43 68 65 63 6b 2d 3e 70  mapGet(pCheck->p
107ce 42 74 2c 20 69 43 68 69 6c 64 2c 20 26 65 50 74  Bt, iChild, &ePt
107cf 72 6d 61 70 54 79 70 65 2c 20 26 69 50 74 72 6d  rmapType, &iPtrm
107d0 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28  apParent);.  if(
107d1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
107d2 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  {.    checkAppen
107d3 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
107d4 6e 74 65 78 74 2c 20 22 46 61 69 6c 65 64 20 74  ntext, "Failed t
107d5 6f 20 72 65 61 64 20 70 74 72 6d 61 70 20 6b 65  o read ptrmap ke
107d6 79 3d 25 64 22 2c 20 69 43 68 69 6c 64 29 3b 0a  y=%d", iChild);.
107d7 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
107d8 0a 20 20 69 66 28 20 65 50 74 72 6d 61 70 54 79  .  if( ePtrmapTy
107d9 70 65 21 3d 65 54 79 70 65 20 7c 7c 20 69 50 74  pe!=eType || iPt
107da 72 6d 61 70 50 61 72 65 6e 74 21 3d 69 50 61 72  rmapParent!=iPar
107db 65 6e 74 20 29 7b 0a 20 20 20 20 63 68 65 63 6b  ent ){.    check
107dc 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
107dd 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20  , zContext, .   
107de 20 20 20 22 42 61 64 20 70 74 72 20 6d 61 70 20     "Bad ptr map 
107df 65 6e 74 72 79 20 6b 65 79 3d 25 64 20 65 78 70  entry key=%d exp
107e0 65 63 74 65 64 3d 28 25 64 2c 25 64 29 20 67 6f  ected=(%d,%d) go
107e1 74 3d 28 25 64 2c 25 64 29 22 2c 20 0a 20 20 20  t=(%d,%d)", .   
107e2 20 20 20 69 43 68 69 6c 64 2c 20 65 54 79 70 65     iChild, eType
107e3 2c 20 69 50 61 72 65 6e 74 2c 20 65 50 74 72 6d  , iParent, ePtrm
107e4 61 70 54 79 70 65 2c 20 69 50 74 72 6d 61 70 50  apType, iPtrmapP
107e5 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65  arent);.  }.}.#e
107e6 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ndif../*.** Chec
107e7 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20  k the integrity 
107e8 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  of the freelist 
107e9 6f 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f  or of an overflo
107ea 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 20  w page list..** 
107eb 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
107ec 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
107ed 6f 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 4e  on the list is N
107ee 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
107ef 20 63 68 65 63 6b 4c 69 73 74 28 0a 20 20 49 6e   checkList(.  In
107f0 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63  tegrityCk *pChec
107f1 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79  k,  /* Integrity
107f2 20 63 68 65 63 6b 69 6e 67 20 63 6f 6e 74 65 78   checking contex
107f3 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 46 72 65  t */.  int isFre
107f4 65 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20  eList,       /* 
107f5 54 72 75 65 20 66 6f 72 20 61 20 66 72 65 65 6c  True for a freel
107f6 69 73 74 2e 20 20 46 61 6c 73 65 20 66 6f 72 20  ist.  False for 
107f7 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69  overflow page li
107f8 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67  st */.  int iPag
107f9 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
107fa 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72   Page number for
107fb 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 74   first page in t
107fc 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  he list */.  int
107fd 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   N,             
107fe 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 6e     /* Expected n
107ff 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
10800 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20  n the list */.  
10801 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20  char *zContext  
10802 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
10803 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   for error messa
10804 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ges */.){.  int 
10805 69 3b 0a 20 20 69 6e 74 20 65 78 70 65 63 74 65  i;.  int expecte
10806 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 20 69 46 69  d = N;.  int iFi
10807 72 73 74 20 3d 20 69 50 61 67 65 3b 0a 20 20 77  rst = iPage;.  w
10808 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26  hile( N-- > 0 &&
10809 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29   pCheck->mxErr )
1080a 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 4f  {.    DbPage *pO
1080b 76 66 6c 50 61 67 65 3b 0a 20 20 20 20 75 6e 73  vflPage;.    uns
1080c 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4f 76 66  igned char *pOvf
1080d 6c 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 69  lData;.    if( i
1080e 50 61 67 65 3c 31 20 29 7b 0a 20 20 20 20 20 20  Page<1 ){.      
1080f 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
10810 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
10811 0a 20 20 20 20 20 20 20 20 20 22 25 64 20 6f 66  .         "%d of
10812 20 25 64 20 70 61 67 65 73 20 6d 69 73 73 69 6e   %d pages missin
10813 67 20 66 72 6f 6d 20 6f 76 65 72 66 6c 6f 77 20  g from overflow 
10814 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74  list starting at
10815 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %d",.          
10816 4e 2b 31 2c 20 65 78 70 65 63 74 65 64 2c 20 69  N+1, expected, i
10817 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 62 72  First);.      br
10818 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
10819 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 65  f( checkRef(pChe
1081a 63 6b 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  ck, iPage, zCont
1081b 65 78 74 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  ext) ) break;.  
1081c 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67    if( sqlite3Pag
1081d 65 72 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 50  erGet(pCheck->pP
1081e 61 67 65 72 2c 20 28 50 67 6e 6f 29 69 50 61 67  ager, (Pgno)iPag
1081f 65 2c 20 26 70 4f 76 66 6c 50 61 67 65 29 20 29  e, &pOvflPage) )
10820 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70  {.      checkApp
10821 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
10822 43 6f 6e 74 65 78 74 2c 20 22 66 61 69 6c 65 64  Context, "failed
10823 20 74 6f 20 67 65 74 20 70 61 67 65 20 25 64 22   to get page %d"
10824 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
10825 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
10826 20 70 4f 76 66 6c 44 61 74 61 20 3d 20 28 75 6e   pOvflData = (un
10827 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71  signed char *)sq
10828 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
10829 61 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20  a(pOvflPage);.  
1082a 20 20 69 66 28 20 69 73 46 72 65 65 4c 69 73 74    if( isFreeList
1082b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20   ){.      int n 
1082c 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76 66  = get4byte(&pOvf
1082d 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 69 66 6e 64  lData[4]);.#ifnd
1082e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1082f 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
10830 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d  if( pCheck->pBt-
10831 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
10832 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
10833 61 70 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65  ap(pCheck, iPage
10834 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  , PTRMAP_FREEPAG
10835 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  E, 0, zContext);
10836 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
10837 20 20 20 20 20 20 69 66 28 20 6e 3e 70 43 68 65        if( n>pChe
10838 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ck->pBt->usableS
10839 69 7a 65 2f 34 2d 38 20 29 7b 0a 20 20 20 20 20  ize/4-8 ){.     
1083a 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
1083b 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65  g(pCheck, zConte
1083c 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 22  xt,.           "
1083d 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20 63 6f  freelist leaf co
1083e 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f 6e 20 70  unt too big on p
1083f 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b  age %d", iPage);
10840 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b 0a 20 20  .        N--;.  
10841 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
10842 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b     for(i=0; i<n;
10843 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
10844 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65 20   Pgno iFreePage 
10845 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76 66  = get4byte(&pOvf
10846 6c 44 61 74 61 5b 38 2b 69 2a 34 5d 29 3b 0a 23  lData[8+i*4]);.#
10847 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
10848 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
10849 20 20 20 20 20 20 20 20 69 66 28 20 70 43 68 65          if( pChe
1084a 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63  ck->pBt->autoVac
1084b 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  uum ){.         
1084c 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70     checkPtrmap(p
1084d 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 65  Check, iFreePage
1084e 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  , PTRMAP_FREEPAG
1084f 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  E, 0, zContext);
10850 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  .          }.#en
10851 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 63 68  dif.          ch
10852 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69  eckRef(pCheck, i
10853 46 72 65 65 50 61 67 65 2c 20 7a 43 6f 6e 74 65  FreePage, zConte
10854 78 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  xt);.        }. 
10855 20 20 20 20 20 20 20 4e 20 2d 3d 20 6e 3b 0a 20         N -= n;. 
10856 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66       }.    }.#if
10857 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
10858 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
10859 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49  else{.      /* I
1085a 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  f this database 
1085b 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
1085c 63 75 75 6d 20 61 6e 64 20 69 50 61 67 65 20 69  cuum and iPage i
1085d 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 20  s not the last. 
1085e 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20       ** page in 
1085f 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 6c 69  this overflow li
10860 73 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74  st, check that t
10861 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
10862 6e 74 72 79 20 66 6f 72 0a 20 20 20 20 20 20 2a  ntry for.      *
10863 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * the following 
10864 70 61 67 65 20 6d 61 74 63 68 65 73 20 69 50 61  page matches iPa
10865 67 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ge..      */.   
10866 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70     if( pCheck->p
10867 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26  Bt->autoVacuum &
10868 26 20 4e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  & N>0 ){.       
10869 20 69 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f   i = get4byte(pO
1086a 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 20 20  vflData);.      
1086b 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43    checkPtrmap(pC
1086c 68 65 63 6b 2c 20 69 2c 20 50 54 52 4d 41 50 5f  heck, i, PTRMAP_
1086d 4f 56 45 52 46 4c 4f 57 32 2c 20 69 50 61 67 65  OVERFLOW2, iPage
1086e 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20  , zContext);.   
1086f 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
10870 66 0a 20 20 20 20 69 50 61 67 65 20 3d 20 67 65  f.    iPage = ge
10871 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61  t4byte(pOvflData
10872 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
10873 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c 50 61  gerUnref(pOvflPa
10874 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  ge);.  }.}.#endi
10875 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
10876 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
10877 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
10878 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
10879 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 44  TY_CHECK./*.** D
1087a 6f 20 76 61 72 69 6f 75 73 20 73 61 6e 69 74 79  o various sanity
1087b 20 63 68 65 63 6b 73 20 6f 6e 20 61 20 73 69 6e   checks on a sin
1087c 67 6c 65 20 70 61 67 65 20 6f 66 20 61 20 74 72  gle page of a tr
1087d 65 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74  ee.  Return.** t
1087e 68 65 20 74 72 65 65 20 64 65 70 74 68 2e 20 20  he tree depth.  
1087f 52 6f 6f 74 20 70 61 67 65 73 20 72 65 74 75 72  Root pages retur
10880 6e 20 30 2e 20 20 50 61 72 65 6e 74 73 20 6f 66  n 0.  Parents of
10881 20 72 6f 6f 74 20 70 61 67 65 73 0a 2a 2a 20 72   root pages.** r
10882 65 74 75 72 6e 20 31 2c 20 61 6e 64 20 73 6f 20  eturn 1, and so 
10883 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  forth..** .** Th
10884 65 73 65 20 63 68 65 63 6b 73 20 61 72 65 20 64  ese checks are d
10885 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  one:.**.**      
10886 31 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  1.  Make sure th
10887 61 74 20 63 65 6c 6c 73 20 61 6e 64 20 66 72 65  at cells and fre
10888 65 62 6c 6f 63 6b 73 20 64 6f 20 6e 6f 74 20 6f  eblocks do not o
10889 76 65 72 6c 61 70 0a 2a 2a 20 20 20 20 20 20 20  verlap.**       
1088a 20 20 20 62 75 74 20 63 6f 6d 62 69 6e 65 20 74     but combine t
1088b 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 76  o completely cov
1088c 65 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20  er the page..** 
1088d 20 4e 4f 20 20 32 2e 20 20 4d 61 6b 65 20 73 75   NO  2.  Make su
1088e 72 65 20 63 65 6c 6c 20 6b 65 79 73 20 61 72 65  re cell keys are
1088f 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 4e   in order..**  N
10890 4f 20 20 33 2e 20 20 4d 61 6b 65 20 73 75 72 65  O  3.  Make sure
10891 20 6e 6f 20 6b 65 79 20 69 73 20 6c 65 73 73 20   no key is less 
10892 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
10893 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 2e 0a 2a 2a   zLowerBound..**
10894 20 20 4e 4f 20 20 34 2e 20 20 4d 61 6b 65 20 73    NO  4.  Make s
10895 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 67 72  ure no key is gr
10896 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
10897 75 61 6c 20 74 6f 20 7a 55 70 70 65 72 42 6f 75  ual to zUpperBou
10898 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 35 2e 20 20  nd..**      5.  
10899 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
1089a 69 74 79 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20  ity of overflow 
1089b 70 61 67 65 73 2e 0a 2a 2a 20 20 20 20 20 20 36  pages..**      6
1089c 2e 20 20 52 65 63 75 72 73 69 76 65 6c 79 20 63  .  Recursively c
1089d 61 6c 6c 20 63 68 65 63 6b 54 72 65 65 50 61 67  all checkTreePag
1089e 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65  e on all childre
1089f 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 2e 20 20 56  n..**      7.  V
108a0 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64  erify that the d
108a1 65 70 74 68 20 6f 66 20 61 6c 6c 20 63 68 69 6c  epth of all chil
108a2 64 72 65 6e 20 69 73 20 74 68 65 20 73 61 6d 65  dren is the same
108a3 2e 0a 2a 2a 20 20 20 20 20 20 38 2e 20 20 4d 61  ..**      8.  Ma
108a4 6b 65 20 73 75 72 65 20 74 68 69 73 20 70 61 67  ke sure this pag
108a5 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 33 33  e is at least 33
108a6 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73 65 20 69  % full or else i
108a7 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  t is.**         
108a8 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
108a9 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   tree..*/.static
108aa 20 69 6e 74 20 63 68 65 63 6b 54 72 65 65 50 61   int checkTreePa
108ab 67 65 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43  ge(.  IntegrityC
108ac 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 43  k *pCheck,  /* C
108ad 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20 73  ontext for the s
108ae 61 6e 69 74 79 20 63 68 65 63 6b 20 2a 2f 0a 20  anity check */. 
108af 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20   int iPage,     
108b0 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
108b1 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
108b2 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20  e to check */.  
108b3 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74  MemPage *pParent
108b4 2c 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20  ,     /* Parent 
108b5 70 61 67 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  page */.  char *
108b6 7a 50 61 72 65 6e 74 43 6f 6e 74 65 78 74 20 20  zParentContext  
108b7 2f 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78  /* Parent contex
108b8 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67  t */.){.  MemPag
108b9 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20  e *pPage;.  int 
108ba 69 2c 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32  i, rc, depth, d2
108bb 2c 20 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69  , pgno, cnt;.  i
108bc 6e 74 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72  nt hdr, cellStar
108bd 74 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a  t;.  int nCell;.
108be 20 20 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74    u8 *data;.  Bt
108bf 53 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69  Shared *pBt;.  i
108c0 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20  nt usableSize;. 
108c1 20 63 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31   char zContext[1
108c2 30 30 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74  00];.  char *hit
108c3 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  ;..  sqlite3_snp
108c4 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f  rintf(sizeof(zCo
108c5 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74  ntext), zContext
108c6 2c 20 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69  , "Page %d: ", i
108c7 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65  Page);..  /* Che
108c8 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ck that the page
108c9 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70   exists.  */.  p
108ca 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74  Bt = pCheck->pBt
108cb 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d  ;.  usableSize =
108cc 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
108cd 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30  ;.  if( iPage==0
108ce 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
108cf 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 65  f( checkRef(pChe
108d0 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65  ck, iPage, zPare
108d1 6e 74 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74  ntContext) ) ret
108d2 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63  urn 0;.  if( (rc
108d3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
108d4 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e  etPage(pBt, (Pgn
108d5 6f 29 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c  o)iPage, &pPage,
108d6 20 30 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 63   0))!=0 ){.    c
108d7 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
108d8 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a  heck, zContext,.
108d9 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74         "unable t
108da 6f 20 67 65 74 20 74 68 65 20 70 61 67 65 2e 20  o get the page. 
108db 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 22 2c 20  error code=%d", 
108dc 72 63 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  rc);.    return 
108dd 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 72 63  0;.  }.  if( (rc
108de 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49   = sqlite3BtreeI
108df 6e 69 74 50 61 67 65 28 70 50 61 67 65 2c 20 70  nitPage(pPage, p
108e0 50 61 72 65 6e 74 29 29 21 3d 30 20 29 7b 0a 20  Parent))!=0 ){. 
108e1 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
108e2 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65  g(pCheck, zConte
108e3 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  xt, .           
108e4 20 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 33          "sqlite3
108e5 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  BtreeInitPage() 
108e6 72 65 74 75 72 6e 73 20 65 72 72 6f 72 20 63 6f  returns error co
108e7 64 65 20 25 64 22 2c 20 72 63 29 3b 0a 20 20 20  de %d", rc);.   
108e8 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
108e9 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ge);.    return 
108ea 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  0;.  }..  /* Che
108eb 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68 65 20 63  ck out all the c
108ec 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 64 65 70  ells..  */.  dep
108ed 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  th = 0;.  for(i=
108ee 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  0; i<pPage->nCel
108ef 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45  l && pCheck->mxE
108f0 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38  rr; i++){.    u8
108f1 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 69 6e 74   *pCell;.    int
108f2 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c 49 6e 66   sz;.    CellInf
108f3 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 2f 2a 20  o info;..    /* 
108f4 43 68 65 63 6b 20 70 61 79 6c 6f 61 64 20 6f 76  Check payload ov
108f5 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20  erflow pages.   
108f6 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   */.    sqlite3_
108f7 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
108f8 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74  zContext), zCont
108f9 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
108fa 20 20 22 4f 6e 20 74 72 65 65 20 70 61 67 65 20    "On tree page 
108fb 25 64 20 63 65 6c 6c 20 25 64 3a 20 22 2c 20 69  %d cell %d: ", i
108fc 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 70 43  Page, i);.    pC
108fd 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
108fe 50 61 67 65 2c 69 29 3b 0a 20 20 20 20 73 71 6c  Page,i);.    sql
108ff 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
10900 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
10901 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  ll, &info);.    
10902 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74 61 3b  sz = info.nData;
10903 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
10904 3e 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b 3d 20  >intKey ) sz += 
10905 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 61  info.nKey;.    a
10906 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f 2e  ssert( sz==info.
10907 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20 20  nPayload );.    
10908 69 66 28 20 73 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63  if( sz>info.nLoc
10909 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  al ){.      int 
1090a 6e 50 61 67 65 20 3d 20 28 73 7a 20 2d 20 69 6e  nPage = (sz - in
1090b 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 62  fo.nLocal + usab
1090c 6c 65 53 69 7a 65 20 2d 20 35 29 2f 28 75 73 61  leSize - 5)/(usa
1090d 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20  bleSize - 4);.  
1090e 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66      Pgno pgnoOvf
1090f 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43  l = get4byte(&pC
10910 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c  ell[info.iOverfl
10911 6f 77 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ow]);.#ifndef SQ
10912 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
10913 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70  CUUM.      if( p
10914 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
10915 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50  {.        checkP
10916 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67  trmap(pCheck, pg
10917 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f  noOvfl, PTRMAP_O
10918 56 45 52 46 4c 4f 57 31 2c 20 69 50 61 67 65 2c  VERFLOW1, iPage,
10919 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   zContext);.    
1091a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
1091b 20 63 68 65 63 6b 4c 69 73 74 28 70 43 68 65 63   checkList(pChec
1091c 6b 2c 20 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20  k, 0, pgnoOvfl, 
1091d 6e 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29  nPage, zContext)
1091e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1091f 43 68 65 63 6b 20 73 61 6e 69 74 79 20 6f 66 20  Check sanity of 
10920 6c 65 66 74 20 63 68 69 6c 64 20 70 61 67 65 2e  left child page.
10921 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
10922 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
10923 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74        pgno = get
10924 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 23 69  4byte(pCell);.#i
10925 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
10926 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
10927 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
10928 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20  Vacuum ){.      
10929 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43    checkPtrmap(pC
1092a 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d  heck, pgno, PTRM
1092b 41 50 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c  AP_BTREE, iPage,
1092c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   zContext);.    
1092d 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
1092e 20 64 32 20 3d 20 63 68 65 63 6b 54 72 65 65 50   d2 = checkTreeP
1092f 61 67 65 28 70 43 68 65 63 6b 2c 70 67 6e 6f 2c  age(pCheck,pgno,
10930 70 50 61 67 65 2c 7a 43 6f 6e 74 65 78 74 29 3b  pPage,zContext);
10931 0a 20 20 20 20 20 20 69 66 28 20 69 3e 30 20 26  .      if( i>0 &
10932 26 20 64 32 21 3d 64 65 70 74 68 20 29 7b 0a 20  & d2!=depth ){. 
10933 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65         checkAppe
10934 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
10935 6f 6e 74 65 78 74 2c 20 22 43 68 69 6c 64 20 70  ontext, "Child p
10936 61 67 65 20 64 65 70 74 68 20 64 69 66 66 65 72  age depth differ
10937 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  s");.      }.   
10938 20 20 20 64 65 70 74 68 20 3d 20 64 32 3b 0a 20     depth = d2;. 
10939 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 21     }.  }.  if( !
1093a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
1093b 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79     pgno = get4by
1093c 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
1093d 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
1093e 74 2b 38 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74  t+8]);.    sqlit
1093f 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
10940 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43  of(zContext), zC
10941 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20  ontext, .       
10942 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 4f                "O
10943 6e 20 70 61 67 65 20 25 64 20 61 74 20 72 69 67  n page %d at rig
10944 68 74 20 63 68 69 6c 64 3a 20 22 2c 20 69 50 61  ht child: ", iPa
10945 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ge);.#ifndef SQL
10946 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
10947 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d  UUM.    if( pBt-
10948 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
10949 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70       checkPtrmap
1094a 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 50  (pCheck, pgno, P
1094b 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69 50 61  TRMAP_BTREE, iPa
1094c 67 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65  ge, 0);.    }.#e
1094d 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54 72  ndif.    checkTr
1094e 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c 20 70  eePage(pCheck, p
1094f 67 6e 6f 2c 20 70 50 61 67 65 2c 20 7a 43 6f 6e  gno, pPage, zCon
10950 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f  text);.  }. .  /
10951 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70  * Check for comp
10952 6c 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66  lete coverage of
10953 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20   the page.  */. 
10954 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
10955 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50  Data;.  hdr = pP
10956 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
10957 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 4d    hit = sqlite3M
10958 61 6c 6c 6f 63 5a 65 72 6f 28 20 75 73 61 62 6c  allocZero( usabl
10959 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 68  eSize );.  if( h
1095a 69 74 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74  it ){.    memset
1095b 28 68 69 74 2c 20 31 2c 20 67 65 74 32 62 79 74  (hit, 1, get2byt
1095c 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 29  e(&data[hdr+5]))
1095d 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65  ;.    nCell = ge
1095e 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
1095f 2b 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74  +3]);.    cellSt
10960 61 72 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d  art = hdr + 12 -
10961 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a   4*pPage->leaf;.
10962 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
10963 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Cell; i++){.    
10964 20 20 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62    int pc = get2b
10965 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74  yte(&data[cellSt
10966 61 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20  art+i*2]);.     
10967 20 69 6e 74 20 73 69 7a 65 20 3d 20 63 65 6c 6c   int size = cell
10968 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26  SizePtr(pPage, &
10969 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20  data[pc]);.     
1096a 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 66   int j;.      if
1096b 28 20 28 70 63 2b 73 69 7a 65 2d 31 29 3e 3d 75  ( (pc+size-1)>=u
1096c 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 70 63 3c  sableSize || pc<
1096d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65  0 ){.        che
1096e 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
1096f 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20  ck, 0, .        
10970 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20      "Corruption 
10971 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c 6c  detected in cell
10972 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c   %d on page %d",
10973 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 20  i,iPage,0);.    
10974 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
10975 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31   for(j=pc+size-1
10976 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69  ; j>=pc; j--) hi
10977 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  t[j]++;.      }.
10978 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 63 6e      }.    for(cn
10979 74 3d 30 2c 20 69 3d 67 65 74 32 62 79 74 65 28  t=0, i=get2byte(
1097a 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 20 69  &data[hdr+1]); i
1097b 3e 30 20 26 26 20 69 3c 75 73 61 62 6c 65 53 69  >0 && i<usableSi
1097c 7a 65 20 26 26 20 63 6e 74 3c 31 30 30 30 30 3b  ze && cnt<10000;
1097d 20 0a 20 20 20 20 20 20 20 20 20 20 20 63 6e 74   .           cnt
1097e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  ++){.      int s
1097f 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26  ize = get2byte(&
10980 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20 20 20 20  data[i+2]);.    
10981 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69    int j;.      i
10982 66 28 20 28 69 2b 73 69 7a 65 2d 31 29 3e 3d 75  f( (i+size-1)>=u
10983 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 69 3c 30  sableSize || i<0
10984 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
10985 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
10986 6b 2c 20 30 2c 20 20 0a 20 20 20 20 20 20 20 20  k, 0,  .        
10987 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20      "Corruption 
10988 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c 6c  detected in cell
10989 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c   %d on page %d",
1098a 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 20  i,iPage,0);.    
1098b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1098c 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b   for(j=i+size-1;
1098d 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b   j>=i; j--) hit[
1098e 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  j]++;.      }.  
1098f 20 20 20 20 69 20 3d 20 67 65 74 32 62 79 74 65      i = get2byte
10990 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20 20 20 20  (&data[i]);.    
10991 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d  }.    for(i=cnt=
10992 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b  0; i<usableSize;
10993 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
10994 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20   hit[i]==0 ){.  
10995 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
10996 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 69 74     }else if( hit
10997 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 20  [i]>1 ){.       
10998 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
10999 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20  pCheck, 0,.     
1099a 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65 20 75       "Multiple u
1099b 73 65 73 20 66 6f 72 20 62 79 74 65 20 25 64 20  ses for byte %d 
1099c 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69 2c 20  of page %d", i, 
1099d 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  iPage);.        
1099e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1099f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e 74     }.    if( cnt
109a0 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b  !=data[hdr+7] ){
109a1 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
109a2 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c  ndMsg(pCheck, 0,
109a3 20 0a 20 20 20 20 20 20 20 20 20 20 22 46 72 61   .          "Fra
109a4 67 6d 65 6e 74 65 64 20 73 70 61 63 65 20 69 73  gmented space is
109a5 20 25 64 20 62 79 74 65 20 72 65 70 6f 72 74 65   %d byte reporte
109a6 64 20 61 73 20 25 64 20 6f 6e 20 70 61 67 65 20  d as %d on page 
109a7 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 63  %d",.          c
109a8 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c  nt, data[hdr+7],
109a9 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20   iPage);.    }. 
109aa 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
109ab 65 28 68 69 74 29 3b 0a 0a 20 20 72 65 6c 65 61  e(hit);..  relea
109ac 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
109ad 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b   return depth+1;
109ae 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
109af 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
109b0 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66  TY_CHECK */..#if
109b1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
109b2 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
109b3 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
109b4 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c  ine does a compl
109b5 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65  ete check of the
109b6 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c   given BTree fil
109b7 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a  e.  aRoot[] is.*
109b8 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61  * an array of pa
109b9 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65  ges numbers were
109ba 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65   each page numbe
109bb 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61  r is the root pa
109bc 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65  ge of.** a table
109bd 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20  .  nRoot is the 
109be 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
109bf 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a  s in aRoot..**.*
109c0 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20  * If everything 
109c1 63 68 65 63 6b 73 20 6f 75 74 2c 20 74 68 69 73  checks out, this
109c2 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
109c3 20 4e 55 4c 4c 2e 20 20 49 66 20 73 6f 6d 65 74   NULL.  If somet
109c4 68 69 6e 67 20 69 73 0a 2a 2a 20 61 6d 69 73 73  hing is.** amiss
109c5 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  , an error messa
109c6 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ge is written in
109c7 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
109c8 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
109c9 0a 2a 2a 20 61 6e 64 20 61 20 70 6f 69 6e 74 65  .** and a pointe
109ca 72 20 74 6f 20 74 68 61 74 20 65 72 72 6f 72 20  r to that error 
109cb 6d 65 73 73 61 67 65 20 69 73 20 72 65 74 75 72  message is retur
109cc 6e 65 64 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  ned.  The callin
109cd 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73  g function.** is
109ce 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
109cf 20 66 72 65 65 69 6e 67 20 74 68 65 20 65 72 72   freeing the err
109d0 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65 6e 20  or message when 
109d1 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2f 0a 53  it is done..*/.S
109d2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
109d3 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
109d4 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 0a  IntegrityCheck(.
109d5 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20    Btree *p,     
109d6 2f 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20  /* The btree to 
109d7 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20  be checked */.  
109d8 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20 2f 2a  int *aRoot,   /*
109d9 20 41 6e 20 61 72 72 61 79 20 6f 66 20 72 6f 6f   An array of roo
109da 74 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20  t pages numbers 
109db 66 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74  for individual t
109dc 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  rees */.  int nR
109dd 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65  oot,    /* Numbe
109de 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
109df 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74  aRoot[] */.  int
109e0 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20 53 74   mxErr,    /* St
109e1 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 65 72 72  op reporting err
109e2 6f 72 73 20 61 66 74 65 72 20 74 68 69 73 20 6d  ors after this m
109e3 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  any */.  int *pn
109e4 45 72 72 20 20 20 20 2f 2a 20 57 72 69 74 65 20  Err    /* Write 
109e5 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
109e6 20 73 65 65 6e 20 74 6f 20 74 68 69 73 20 76 61   seen to this va
109e7 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69  riable */.){.  i
109e8 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65 66  nt i;.  int nRef
109e9 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20  ;.  IntegrityCk 
109ea 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68 61 72  sCheck;.  BtShar
109eb 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
109ec 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
109ed 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 52 65  eEnter(p);.  nRe
109ee 66 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  f = sqlite3Pager
109ef 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  Refcount(pBt->pP
109f0 61 67 65 72 29 3b 0a 20 20 69 66 28 20 6c 6f 63  ager);.  if( loc
109f1 6b 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28  kBtreeWithRetry(
109f2 70 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  p)!=SQLITE_OK ){
109f3 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
109f4 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72  eLeave(p);.    r
109f5 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72  eturn sqlite3Str
109f6 44 75 70 28 22 55 6e 61 62 6c 65 20 74 6f 20 61  Dup("Unable to a
109f7 63 71 75 69 72 65 20 61 20 72 65 61 64 20 6c 6f  cquire a read lo
109f8 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
109f9 73 65 22 29 3b 0a 20 20 7d 0a 20 20 73 43 68 65  se");.  }.  sChe
109fa 63 6b 2e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20  ck.pBt = pBt;.  
109fb 73 43 68 65 63 6b 2e 70 50 61 67 65 72 20 3d 20  sCheck.pPager = 
109fc 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73  pBt->pPager;.  s
109fd 43 68 65 63 6b 2e 6e 50 61 67 65 20 3d 20 73 71  Check.nPage = sq
109fe 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
109ff 75 6e 74 28 73 43 68 65 63 6b 2e 70 50 61 67 65  unt(sCheck.pPage
10a00 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45  r);.  sCheck.mxE
10a01 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43  rr = mxErr;.  sC
10a02 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20  heck.nErr = 0;. 
10a03 20 2a 70 6e 45 72 72 20 3d 20 30 3b 0a 23 69 66   *pnErr = 0;.#if
10a04 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
10a05 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66  _AUTOVACUUM.  if
10a06 28 20 70 42 74 2d 3e 6e 54 72 75 6e 63 21 3d 30  ( pBt->nTrunc!=0
10a07 20 29 7b 0a 20 20 20 20 73 43 68 65 63 6b 2e 6e   ){.    sCheck.n
10a08 50 61 67 65 20 3d 20 70 42 74 2d 3e 6e 54 72 75  Page = pBt->nTru
10a09 6e 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  nc;.  }.#endif. 
10a0a 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 50 61 67   if( sCheck.nPag
10a0b 65 3d 3d 30 20 29 7b 0a 20 20 20 20 75 6e 6c 6f  e==0 ){.    unlo
10a0c 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28  ckBtreeIfUnused(
10a0d 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pBt);.    sqlite
10a0e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
10a0f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
10a10 7d 0a 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66  }.  sCheck.anRef
10a11 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
10a12 63 28 20 28 73 43 68 65 63 6b 2e 6e 50 61 67 65  c( (sCheck.nPage
10a13 2b 31 29 2a 73 69 7a 65 6f 66 28 73 43 68 65 63  +1)*sizeof(sChec
10a14 6b 2e 61 6e 52 65 66 5b 30 5d 29 20 29 3b 0a 20  k.anRef[0]) );. 
10a15 20 69 66 28 20 21 73 43 68 65 63 6b 2e 61 6e 52   if( !sCheck.anR
10a16 65 66 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b  ef ){.    unlock
10a17 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42  BtreeIfUnused(pB
10a18 74 29 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d  t);.    *pnErr =
10a19 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   1;.    sqlite3B
10a1a 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
10a1b 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
10a1c 4d 50 72 69 6e 74 66 28 70 2d 3e 70 53 71 6c 69  MPrintf(p->pSqli
10a1d 74 65 2c 20 22 55 6e 61 62 6c 65 20 74 6f 20 6d  te, "Unable to m
10a1e 61 6c 6c 6f 63 20 25 64 20 62 79 74 65 73 22 2c  alloc %d bytes",
10a1f 20 0a 20 20 20 20 20 20 20 20 28 73 43 68 65 63   .        (sChec
10a20 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f  k.nPage+1)*sizeo
10a21 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30  f(sCheck.anRef[0
10a22 5d 29 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  ]));.  }.  for(i
10a23 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50  =0; i<=sCheck.nP
10a24 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63  age; i++){ sChec
10a25 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20  k.anRef[i] = 0; 
10a26 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f  }.  i = PENDING_
10a27 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a  BYTE_PAGE(pBt);.
10a28 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e    if( i<=sCheck.
10a29 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68  nPage ){.    sCh
10a2a 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31  eck.anRef[i] = 1
10a2b 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e 7a  ;.  }.  sCheck.z
10a2c 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 2f  ErrMsg = 0;..  /
10a2d 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65  * Check the inte
10a2e 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65  grity of the fre
10a2f 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65  elist.  */.  che
10a30 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20  ckList(&sCheck, 
10a31 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74  1, get4byte(&pBt
10a32 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
10a33 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  32]),.          
10a34 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d    get4byte(&pBt-
10a35 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
10a36 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c  6]), "Main freel
10a37 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43  ist: ");..  /* C
10a38 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62  heck all the tab
10a39 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  les..  */.  for(
10a3a 69 3d 30 3b 20 69 3c 6e 52 6f 6f 74 20 26 26 20  i=0; i<nRoot && 
10a3b 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b  sCheck.mxErr; i+
10a3c 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f  +){.    if( aRoo
10a3d 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  t[i]==0 ) contin
10a3e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ue;.#ifndef SQLI
10a3f 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
10a40 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e  UM.    if( pBt->
10a41 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52  autoVacuum && aR
10a42 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20  oot[i]>1 ){.    
10a43 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73    checkPtrmap(&s
10a44 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c  Check, aRoot[i],
10a45 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
10a46 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23  , 0, 0);.    }.#
10a47 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54  endif.    checkT
10a48 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c  reePage(&sCheck,
10a49 20 61 52 6f 6f 74 5b 69 5d 2c 20 30 2c 20 22 4c   aRoot[i], 0, "L
10a4a 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74  ist of tree root
10a4b 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  s: ");.  }..  /*
10a4c 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79   Make sure every
10a4d 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c   page in the fil
10a4e 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a  e is referenced.
10a4f 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20    */.  for(i=1; 
10a50 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20  i<=sCheck.nPage 
10a51 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b  && sCheck.mxErr;
10a52 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51   i++){.#ifdef SQ
10a53 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
10a54 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68  CUUM.    if( sCh
10a55 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20  eck.anRef[i]==0 
10a56 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
10a57 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c  pendMsg(&sCheck,
10a58 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 20   0, "Page %d is 
10a59 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b  never used", i);
10a5a 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20  .    }.#else.   
10a5b 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62   /* If the datab
10a5c 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
10a5d 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73  o-vacuum, make s
10a5e 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f  ure no tables co
10a5f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66  ntain.    ** ref
10a60 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74  erences to point
10a61 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20  er-map pages..  
10a62 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68    */.    if( sCh
10a63 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20  eck.anRef[i]==0 
10a64 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d  && .       (PTRM
10a65 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69  AP_PAGENO(pBt, i
10a66 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75  )!=i || !pBt->au
10a67 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20  toVacuum) ){.   
10a68 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
10a69 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50  g(&sCheck, 0, "P
10a6a 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20  age %d is never 
10a6b 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d  used", i);.    }
10a6c 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e  .    if( sCheck.
10a6d 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a  anRef[i]!=0 && .
10a6e 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50         (PTRMAP_P
10a6f 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69  AGENO(pBt, i)==i
10a70 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63   && pBt->autoVac
10a71 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68  uum) ){.      ch
10a72 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43  eckAppendMsg(&sC
10a73 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65  heck, 0, "Pointe
10a74 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 73  r map page %d is
10a75 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29   referenced", i)
10a76 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
10a77 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
10a78 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73  re this analysis
10a79 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61   did not leave a
10a7a 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 73  ny unref() pages
10a7b 0a 20 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74  .  */.  unlockBt
10a7c 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
10a7d 3b 0a 20 20 69 66 28 20 6e 52 65 66 20 21 3d 20  ;.  if( nRef != 
10a7e 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
10a7f 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
10a80 29 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70  ) ){.    checkAp
10a81 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c  pendMsg(&sCheck,
10a82 20 30 2c 20 0a 20 20 20 20 20 20 22 4f 75 74 73   0, .      "Outs
10a83 74 61 6e 64 69 6e 67 20 70 61 67 65 20 63 6f 75  tanding page cou
10a84 6e 74 20 67 6f 65 73 20 66 72 6f 6d 20 25 64 20  nt goes from %d 
10a85 74 6f 20 25 64 20 64 75 72 69 6e 67 20 74 68 69  to %d during thi
10a86 73 20 61 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20  s analysis",.   
10a87 20 20 20 6e 52 65 66 2c 20 73 71 6c 69 74 65 33     nRef, sqlite3
10a88 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42  PagerRefcount(pB
10a89 74 2d 3e 70 50 61 67 65 72 29 0a 20 20 20 20 29  t->pPager).    )
10a8a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61  ;.  }..  /* Clea
10a8b 6e 20 20 75 70 20 61 6e 64 20 72 65 70 6f 72 74  n  up and report
10a8c 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20   errors..  */.  
10a8d 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
10a8e 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  e(p);.  sqlite3_
10a8f 66 72 65 65 28 73 43 68 65 63 6b 2e 61 6e 52 65  free(sCheck.anRe
10a90 66 29 3b 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73  f);.  *pnErr = s
10a91 43 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 72 65  Check.nErr;.  re
10a92 74 75 72 6e 20 73 43 68 65 63 6b 2e 7a 45 72 72  turn sCheck.zErr
10a93 4d 73 67 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Msg;.}.#endif /*
10a94 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
10a95 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a  EGRITY_CHECK */.
10a96 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
10a97 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20  e full pathname 
10a98 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
10a99 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  g database file.
10a9a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
10a9b 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76   filename is inv
10a9c 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61  ariant as long a
10a9d 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a  s the pager is.*
10a9e 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20  * open so it is 
10a9f 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77  safe to access w
10aa0 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61  ithout the BtSha
10aa1 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  red mutex..*/.SQ
10aa2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
10aa3 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
10aa4 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
10aa5 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73  (Btree *p){.  as
10aa6 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50  sert( p->pBt->pP
10aa7 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74  ager!=0 );.  ret
10aa8 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72  urn sqlite3Pager
10aa9 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d  Filename(p->pBt-
10aaa 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a  >pPager);.}../*.
10aab 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61  ** Return the pa
10aac 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 69  thname of the di
10aad 72 65 63 74 6f 72 79 20 74 68 61 74 20 63 6f 6e  rectory that con
10aae 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 62 61  tains the databa
10aaf 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  se file..**.** T
10ab0 68 65 20 70 61 67 65 72 20 64 69 72 65 63 74 6f  he pager directo
10ab1 72 79 20 6e 61 6d 65 20 69 73 20 69 6e 76 61 72  ry name is invar
10ab2 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20  iant as long as 
10ab3 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20  the pager is.** 
10ab4 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61  open so it is sa
10ab5 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74  fe to access wit
10ab6 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65  hout the BtShare
10ab7 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49  d mutex..*/.SQLI
10ab8 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
10ab9 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
10aba 72 65 65 47 65 74 44 69 72 6e 61 6d 65 28 42 74  reeGetDirname(Bt
10abb 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
10abc 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  t( p->pBt->pPage
10abd 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  r!=0 );.  return
10abe 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 69 72   sqlite3PagerDir
10abf 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61  name(p->pBt->pPa
10ac0 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ger);.}../*.** R
10ac1 65 74 75 72 6e 20 74 68 65 20 70 61 74 68 6e 61  eturn the pathna
10ac2 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  me of the journa
10ac3 6c 20 66 69 6c 65 20 66 6f 72 20 74 68 69 73 20  l file for this 
10ac4 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 72 65  database. The re
10ac5 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66  turn.** value of
10ac6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
10ac7 20 74 68 65 20 73 61 6d 65 20 72 65 67 61 72 64   the same regard
10ac8 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20  less of whether 
10ac9 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
10aca 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65  .** has been cre
10acb 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a 2a 2a 0a  ated or not..**.
10acc 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6a 6f 75  ** The pager jou
10acd 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73  rnal filename is
10ace 20 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f   invariant as lo
10acf 6e 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20  ng as the pager 
10ad0 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74  is.** open so it
10ad1 20 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65   is safe to acce
10ad2 73 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42  ss without the B
10ad3 74 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a  tShared mutex..*
10ad4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10ad5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
10ad6 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72  ite3BtreeGetJour
10ad7 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 70  nalname(Btree *p
10ad8 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
10ad9 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29  pBt->pPager!=0 )
10ada 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
10adb 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61  e3PagerJournalna
10adc 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  me(p->pBt->pPage
10add 72 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  r);.}..#ifndef S
10ade 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
10adf 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65  M./*.** Copy the
10ae0 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e   complete conten
10ae1 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e 74  t of pBtFrom int
10ae2 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 72 61 6e  o pBtTo.  A tran
10ae3 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20  saction.** must 
10ae4 62 65 20 61 63 74 69 76 65 20 66 6f 72 20 62 6f  be active for bo
10ae5 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  th files..**.** 
10ae6 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c 65  The size of file
10ae7 20 70 42 74 46 72 6f 6d 20 6d 61 79 20 62 65 20   pBtFrom may be 
10ae8 72 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20  reduced by this 
10ae9 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 20 49 66  operation..** If
10aea 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77   anything goes w
10aeb 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 61  rong, the transa
10aec 63 74 69 6f 6e 20 6f 6e 20 70 42 74 46 72 6f 6d  ction on pBtFrom
10aed 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   is rolled back.
10aee 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
10aef 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72  treeCopyFile(Btr
10af0 65 65 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a  ee *pTo, Btree *
10af1 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63  pFrom){.  int rc
10af2 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
10af3 50 67 6e 6f 20 69 2c 20 6e 50 61 67 65 2c 20 6e  Pgno i, nPage, n
10af4 54 6f 50 61 67 65 2c 20 69 53 6b 69 70 3b 0a 0a  ToPage, iSkip;..
10af5 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 54    BtShared *pBtT
10af6 6f 20 3d 20 70 54 6f 2d 3e 70 42 74 3b 0a 20 20  o = pTo->pBt;.  
10af7 42 74 53 68 61 72 65 64 20 2a 70 42 74 46 72 6f  BtShared *pBtFro
10af8 6d 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 74 3b 0a  m = pFrom->pBt;.
10af9 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 6e 54 72  .  if( pTo->inTr
10afa 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans!=TRANS_WRITE
10afb 20 7c 7c 20 70 46 72 6f 6d 2d 3e 69 6e 54 72 61   || pFrom->inTra
10afc 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns!=TRANS_WRITE 
10afd 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
10afe 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
10aff 20 20 69 66 28 20 70 42 74 54 6f 2d 3e 70 43 75    if( pBtTo->pCu
10b00 72 73 6f 72 20 29 20 72 65 74 75 72 6e 20 53 51  rsor ) return SQ
10b01 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 6e 54 6f  LITE_BUSY;.  nTo
10b02 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 50 61  Page = sqlite3Pa
10b03 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
10b04 54 6f 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 6e  To->pPager);.  n
10b05 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 50 61  Page = sqlite3Pa
10b06 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
10b07 46 72 6f 6d 2d 3e 70 50 61 67 65 72 29 3b 0a 20  From->pPager);. 
10b08 20 69 53 6b 69 70 20 3d 20 50 45 4e 44 49 4e 47   iSkip = PENDING
10b09 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 54 6f  _BYTE_PAGE(pBtTo
10b0a 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 72 63  );.  for(i=1; rc
10b0b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
10b0c 3c 3d 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20  <=nPage; i++){. 
10b0d 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61     DbPage *pDbPa
10b0e 67 65 3b 0a 20 20 20 20 69 66 28 20 69 3d 3d 69  ge;.    if( i==i
10b0f 53 6b 69 70 20 29 20 63 6f 6e 74 69 6e 75 65 3b  Skip ) continue;
10b10 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
10b11 33 50 61 67 65 72 47 65 74 28 70 42 74 46 72 6f  3PagerGet(pBtFro
10b12 6d 2d 3e 70 50 61 67 65 72 2c 20 69 2c 20 26 70  m->pPager, i, &p
10b13 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  DbPage);.    if(
10b14 20 72 63 20 29 20 62 72 65 61 6b 3b 0a 20 20 20   rc ) break;.   
10b15 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
10b16 65 72 4f 76 65 72 77 72 69 74 65 28 70 42 74 54  erOverwrite(pBtT
10b17 6f 2d 3e 70 50 61 67 65 72 2c 20 69 2c 20 73 71  o->pPager, i, sq
10b18 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
10b19 61 28 70 44 62 50 61 67 65 29 29 3b 0a 20 20 20  a(pDbPage));.   
10b1a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
10b1b 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 7d  ef(pDbPage);.  }
10b1c 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69  ..  /* If the fi
10b1d 6c 65 20 69 73 20 73 68 72 69 6e 6b 69 6e 67 2c  le is shrinking,
10b1e 20 6a 6f 75 72 6e 61 6c 20 74 68 65 20 70 61 67   journal the pag
10b1f 65 73 20 74 68 61 74 20 61 72 65 20 62 65 69 6e  es that are bein
10b20 67 20 74 72 75 6e 63 61 74 65 64 0a 20 20 2a 2a  g truncated.  **
10b21 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61   so that they ca
10b22 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n be rolled back
10b23 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 66   if the commit f
10b24 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ails..  */.  for
10b25 28 69 3d 6e 50 61 67 65 2b 31 3b 20 72 63 3d 3d  (i=nPage+1; rc==
10b26 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 3d  SQLITE_OK && i<=
10b27 6e 54 6f 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20  nToPage; i++){. 
10b28 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61     DbPage *pDbPa
10b29 67 65 3b 0a 20 20 20 20 69 66 28 20 69 3d 3d 69  ge;.    if( i==i
10b2a 53 6b 69 70 20 29 20 63 6f 6e 74 69 6e 75 65 3b  Skip ) continue;
10b2b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
10b2c 33 50 61 67 65 72 47 65 74 28 70 42 74 54 6f 2d  3PagerGet(pBtTo-
10b2d 3e 70 50 61 67 65 72 2c 20 69 2c 20 26 70 44 62  >pPager, i, &pDb
10b2e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
10b2f 63 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 72  c ) break;.    r
10b30 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
10b31 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a  Write(pDbPage);.
10b32 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
10b33 44 6f 6e 74 57 72 69 74 65 28 70 44 62 50 61 67  DontWrite(pDbPag
10b34 65 29 3b 0a 20 20 20 20 2f 2a 20 59 65 61 68 2e  e);.    /* Yeah.
10b35 20 20 49 74 20 73 65 65 6d 73 20 77 69 65 72 64    It seems wierd
10b36 20 74 6f 20 63 61 6c 6c 20 44 6f 6e 74 57 72 69   to call DontWri
10b37 74 65 28 29 20 72 69 67 68 74 20 61 66 74 65 72  te() right after
10b38 20 57 72 69 74 65 28 29 2e 20 20 42 75 74 0a 20   Write().  But. 
10b39 20 20 20 2a 2a 20 74 68 61 74 20 69 73 20 62 65     ** that is be
10b3a 63 61 75 73 65 20 74 68 65 20 6e 61 6d 65 73 20  cause the names 
10b3b 6f 66 20 74 68 6f 73 65 20 70 72 6f 63 65 64 75  of those procedu
10b3c 72 65 73 20 64 6f 20 6e 6f 74 20 65 78 61 63 74  res do not exact
10b3d 6c 79 20 0a 20 20 20 20 2a 2a 20 72 65 70 72 65  ly .    ** repre
10b3e 73 65 6e 74 20 77 68 61 74 20 74 68 65 79 20 64  sent what they d
10b3f 6f 2e 20 20 57 72 69 74 65 28 29 20 72 65 61 6c  o.  Write() real
10b40 6c 79 20 6d 65 61 6e 73 20 22 70 75 74 20 74 68  ly means "put th
10b41 69 73 20 70 61 67 65 20 69 6e 20 74 68 65 0a 20  is page in the. 
10b42 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a     ** rollback j
10b43 6f 75 72 6e 61 6c 20 61 6e 64 20 6d 61 72 6b 20  ournal and mark 
10b44 69 74 20 61 73 20 64 69 72 74 79 20 73 6f 20 74  it as dirty so t
10b45 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 77  hat it will be w
10b46 72 69 74 74 65 6e 0a 20 20 20 20 2a 2a 20 74 6f  ritten.    ** to
10b47 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
10b48 6c 65 20 6c 61 74 65 72 2e 22 20 20 44 6f 6e 74  le later."  Dont
10b49 57 72 69 74 65 28 29 20 75 6e 64 6f 65 73 20 74  Write() undoes t
10b4a 68 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 6f  he second part o
10b4b 66 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 61 6e  f.    ** that an
10b4c 64 20 70 72 65 76 65 6e 74 73 20 74 68 65 20 70  d prevents the p
10b4d 61 67 65 20 66 72 6f 6d 20 62 65 69 6e 67 20 77  age from being w
10b4e 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
10b4f 74 61 62 61 73 65 2e 20 20 54 68 65 0a 20 20 20  tabase.  The.   
10b50 20 2a 2a 20 70 61 67 65 20 69 73 20 73 74 69 6c   ** page is stil
10b51 6c 20 6f 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  l on the rollbac
10b52 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 6f 75 67  k journal, thoug
10b53 68 2e 20 20 41 6e 64 20 74 68 61 74 20 69 73 20  h.  And that is 
10b54 74 68 65 20 77 68 6f 6c 65 0a 20 20 20 20 2a 2a  the whole.    **
10b55 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 6c   point of this l
10b56 6f 6f 70 3a 20 74 6f 20 70 75 74 20 70 61 67 65  oop: to put page
10b57 73 20 6f 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  s on the rollbac
10b58 6b 20 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20  k journal. */.  
10b59 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
10b5a 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ref(pDbPage);.  
10b5b 7d 0a 20 20 69 66 28 20 21 72 63 20 26 26 20 6e  }.  if( !rc && n
10b5c 50 61 67 65 3c 6e 54 6f 50 61 67 65 20 29 7b 0a  Page<nToPage ){.
10b5d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
10b5e 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 70 42  PagerTruncate(pB
10b5f 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20 6e 50 61  tTo->pPager, nPa
10b60 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ge);.  }..  if( 
10b61 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rc ){.    sqlite
10b62 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70  3BtreeRollback(p
10b63 54 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  To);.  }.  retur
10b64 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 45  n rc;  .}.SQLITE
10b65 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10b66 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c  ite3BtreeCopyFil
10b67 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 74  e(Btree *pTo, Bt
10b68 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69  ree *pFrom){.  i
10b69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
10b6a 42 74 72 65 65 45 6e 74 65 72 28 70 54 6f 29 3b  BtreeEnter(pTo);
10b6b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
10b6c 6e 74 65 72 28 70 46 72 6f 6d 29 3b 0a 20 20 72  nter(pFrom);.  r
10b6d 63 20 3d 20 62 74 72 65 65 43 6f 70 79 46 69 6c  c = btreeCopyFil
10b6e 65 28 70 54 6f 2c 20 70 46 72 6f 6d 29 3b 0a 20  e(pTo, pFrom);. 
10b6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
10b70 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c  ve(pFrom);.  sql
10b71 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
10b72 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  To);.  return rc
10b73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
10b74 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
10b75 4d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  M */../*.** Retu
10b76 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61  rn non-zero if a
10b77 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
10b78 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54  active..*/.SQLIT
10b79 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
10b7a 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
10b7b 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ans(Btree *p){. 
10b7c 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c   assert( p==0 ||
10b7d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
10b7e 65 6c 64 28 70 2d 3e 70 53 71 6c 69 74 65 2d 3e  eld(p->pSqlite->
10b7f 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75  mutex) );.  retu
10b80 72 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54  rn (p && (p->inT
10b81 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
10b82 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  E));.}../*.** Re
10b83 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66  turn non-zero if
10b84 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
10b85 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
10b86 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
10b87 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
10b88 33 42 74 72 65 65 49 73 49 6e 53 74 6d 74 28 42  3BtreeIsInStmt(B
10b89 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
10b8a 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
10b8b 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
10b8c 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 70 42  .  return (p->pB
10b8d 74 20 26 26 20 70 2d 3e 70 42 74 2d 3e 69 6e 53  t && p->pBt->inS
10b8e 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  tmt);.}../*.** R
10b8f 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
10b90 66 20 61 20 72 65 61 64 20 28 6f 72 20 77 72 69  f a read (or wri
10b91 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  te) transaction 
10b92 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51  is active..*/.SQ
10b93 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10b94 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
10b95 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65  nReadTrans(Btree
10b96 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
10b97 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
10b98 6c 64 28 70 2d 3e 70 53 71 6c 69 74 65 2d 3e 6d  ld(p->pSqlite->m
10b99 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72  utex) );.  retur
10b9a 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72  n (p && (p->inTr
10b9b 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 29  ans!=TRANS_NONE)
10b9c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
10b9d 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
10b9e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
10b9f 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20   blob of memory 
10ba0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
10ba1 2a 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61 72  ** a single shar
10ba2 65 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d 65  ed-btree. The me
10ba3 6d 6f 72 79 20 69 73 20 75 73 65 64 20 62 79 20  mory is used by 
10ba4 63 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72 20  client code for 
10ba5 69 74 27 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70  it's own.** purp
10ba6 6f 73 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c  oses (for exampl
10ba7 65 2c 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69  e, to store a hi
10ba8 67 68 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20  gh-level schema 
10ba9 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
10baa 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62  .** the shared-b
10bab 74 72 65 65 29 2e 20 54 68 65 20 62 74 72 65 65  tree). The btree
10bac 20 6c 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72   layer manages r
10bad 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e  eference countin
10bae 67 20 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  g issues..**.** 
10baf 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
10bb0 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  his is called on
10bb1 20 61 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c   a shared-btree,
10bb2 20 6e 42 79 74 65 73 20 62 79 74 65 73 20 6f 66   nBytes bytes of
10bb3 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61   memory.** are a
10bb4 6c 6c 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64  llocated, zeroed
10bb5 2c 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74  , and returned t
10bb6 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f  o the caller. Fo
10bb7 72 20 65 61 63 68 20 73 75 62 73 65 71 75 65 6e  r each subsequen
10bb8 74 20 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e  t .** call the n
10bb9 42 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20  Bytes parameter 
10bba 69 73 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61  is ignored and a
10bbb 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
10bbc 73 61 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20  same blob.** of 
10bbd 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e  memory returned.
10bbe 20 0a 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62 65 66   .**.** Just bef
10bbf 6f 72 65 20 74 68 65 20 73 68 61 72 65 64 2d 62  ore the shared-b
10bc0 74 72 65 65 20 69 73 20 63 6c 6f 73 65 64 2c 20  tree is closed, 
10bc1 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73  the function pas
10bc2 73 65 64 20 61 73 20 74 68 65 20 0a 2a 2a 20 78  sed as the .** x
10bc3 46 72 65 65 20 61 72 67 75 6d 65 6e 74 20 77 68  Free argument wh
10bc4 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  en the memory al
10bc5 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6d 61 64  location was mad
10bc6 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20  e is invoked on 
10bc7 74 68 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f 66 20  the .** blob of 
10bc8 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79  allocated memory
10bc9 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
10bca 73 68 6f 75 6c 64 20 6e 6f 74 20 63 61 6c 6c 20  should not call 
10bcb 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 0a 2a  sqlite3_free().*
10bcc 2a 20 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 79 2c  * on the memory,
10bcd 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   the btree layer
10bce 20 64 6f 65 73 20 74 68 61 74 2e 0a 2a 2f 0a 53   does that..*/.S
10bcf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
10bd0 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
10bd1 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 70 2c  Schema(Btree *p,
10bd2 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 76 6f 69   int nBytes, voi
10bd3 64 28 2a 78 46 72 65 65 29 28 76 6f 69 64 20 2a  d(*xFree)(void *
10bd4 29 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  )){.  BtShared *
10bd5 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
10bd6 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
10bd7 72 28 70 29 3b 0a 20 20 69 66 28 20 21 70 42 74  r(p);.  if( !pBt
10bd8 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ->pSchema ){.   
10bd9 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20   pBt->pSchema = 
10bda 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
10bdb 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70  o(nBytes);.    p
10bdc 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20  Bt->xFreeSchema 
10bdd 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73  = xFree;.  }.  s
10bde 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
10bdf 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42  (p);.  return pB
10be0 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f  t->pSchema;.}../
10be1 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
10be2 20 69 66 20 61 6e 6f 74 68 65 72 20 75 73 65 72   if another user
10be3 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 68 61   of the same sha
10be4 72 65 64 20 62 74 72 65 65 20 61 73 20 74 68 65  red btree as the
10be5 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 68 61 6e   argument.** han
10be6 64 6c 65 20 68 6f 6c 64 73 20 61 6e 20 65 78 63  dle holds an exc
10be7 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74  lusive lock on t
10be8 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
10be9 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
10bea 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
10beb 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
10bec 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29  Locked(Btree *p)
10bed 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
10bee 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
10bef 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 71 6c  tex_held(p->pSql
10bf0 69 74 65 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  ite->mutex) );. 
10bf1 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
10bf2 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 71  er(p);.  rc = (q
10bf3 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ueryTableLock(p,
10bf4 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45   MASTER_ROOT, RE
10bf5 41 44 5f 4c 4f 43 4b 29 21 3d 53 51 4c 49 54 45  AD_LOCK)!=SQLITE
10bf6 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  _OK);.  sqlite3B
10bf7 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
10bf8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23  return rc;.}...#
10bf9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
10bfa 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
10bfb 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c  /*.** Obtain a l
10bfc 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ock on the table
10bfd 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65   whose root page
10bfe 20 69 73 20 69 54 61 62 2e 20 20 54 68 65 0a 2a   is iTab.  The.*
10bff 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77 72 69 74  * lock is a writ
10c00 65 20 6c 6f 63 6b 20 69 66 20 69 73 57 72 69 74  e lock if isWrit
10c01 65 6c 6f 63 6b 20 69 73 20 74 72 75 65 20 6f 72  elock is true or
10c02 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20   a read lock.** 
10c03 69 66 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a  if it is false..
10c04 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
10c05 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
10c06 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65  eeLockTable(Btre
10c07 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 2c 20  e *p, int iTab, 
10c08 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 7b  u8 isWriteLock){
10c09 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
10c0a 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 6c 6f 63 6b  TE_OK;.  u8 lock
10c0b 54 79 70 65 20 3d 20 28 69 73 57 72 69 74 65 4c  Type = (isWriteL
10c0c 6f 63 6b 3f 57 52 49 54 45 5f 4c 4f 43 4b 3a 52  ock?WRITE_LOCK:R
10c0d 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 73 71 6c  EAD_LOCK);.  sql
10c0e 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
10c0f 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54  );.  rc = queryT
10c10 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62  ableLock(p, iTab
10c11 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 69  , lockType);.  i
10c12 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
10c13 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6c 6f 63   ){.    rc = loc
10c14 6b 54 61 62 6c 65 28 70 2c 20 69 54 61 62 2c 20  kTable(p, iTab, 
10c15 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 7d 0a 20  lockType);.  }. 
10c16 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
10c17 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
10c18 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  rc;.}.#endif..#i
10c19 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
10c1a 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a  T_INCRBLOB./*.**
10c1b 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20 6d   Argument pCsr m
10c1c 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72 20  ust be a cursor 
10c1d 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69  opened for writi
10c1e 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54  ng on an .** INT
10c1f 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72 65 6e  KEY table curren
10c20 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  tly pointing at 
10c21 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20 65 6e  a valid table en
10c22 74 72 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75  try. .** This fu
10c23 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20  nction modifies 
10c24 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 20  the data stored 
10c25 61 73 20 70 61 72 74 20 6f 66 20 74 68 61 74 20  as part of that 
10c26 65 6e 74 72 79 2e 0a 2a 2a 20 4f 6e 6c 79 20 74  entry..** Only t
10c27 68 65 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 20  he data content 
10c28 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64 69  may only be modi
10c29 66 69 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20  fied, it is not 
10c2a 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 63  possible.** to c
10c2b 68 61 6e 67 65 20 74 68 65 20 6c 65 6e 67 74 68  hange the length
10c2c 20 6f 66 20 74 68 65 20 64 61 74 61 20 73 74 6f   of the data sto
10c2d 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
10c2e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
10c2f 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42  e3BtreePutData(B
10c30 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75  tCursor *pCsr, u
10c31 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61  32 offset, u32 a
10c32 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20  mt, void *z){.  
10c33 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
10c34 6c 64 73 4d 75 74 65 78 28 70 43 73 72 29 20 29  ldsMutex(pCsr) )
10c35 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
10c36 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
10c37 43 73 72 2d 3e 70 42 74 72 65 65 2d 3e 70 53 71  Csr->pBtree->pSq
10c38 6c 69 74 65 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  lite->mutex) );.
10c39 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e 69    assert(pCsr->i
10c3a 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29  sIncrblobHandle)
10c3b 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53  ;.  if( pCsr->eS
10c3c 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51  tate>=CURSOR_REQ
10c3d 55 49 52 45 53 45 45 4b 20 29 7b 0a 20 20 20 20  UIRESEEK ){.    
10c3e 69 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65  if( pCsr->eState
10c3f 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29  ==CURSOR_FAULT )
10c40 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  {.      return p
10c41 43 73 72 2d 3e 73 6b 69 70 3b 0a 20 20 20 20 7d  Csr->skip;.    }
10c42 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75  else{.      retu
10c43 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b  rn SQLITE_ABORT;
10c44 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
10c45 20 43 68 65 63 6b 20 73 6f 6d 65 20 70 72 65 63   Check some prec
10c46 6f 6e 64 69 74 69 6f 6e 73 3a 20 0a 20 20 2a 2a  onditions: .  **
10c47 20 20 20 28 61 29 20 74 68 65 20 63 75 72 73 6f     (a) the curso
10c48 72 20 69 73 20 6f 70 65 6e 20 66 6f 72 20 77 72  r is open for wr
10c49 69 74 69 6e 67 2c 0a 20 20 2a 2a 20 20 20 28 62  iting,.  **   (b
10c4a 29 20 74 68 65 72 65 20 69 73 20 6e 6f 20 72 65  ) there is no re
10c4b 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74  ad-lock on the t
10c4c 61 62 6c 65 20 62 65 69 6e 67 20 6d 6f 64 69 66  able being modif
10c4d 69 65 64 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28  ied and.  **   (
10c4e 63 29 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f  c) the cursor po
10c4f 69 6e 74 73 20 61 74 20 61 20 76 61 6c 69 64 20  ints at a valid 
10c50 72 6f 77 20 6f 66 20 61 6e 20 69 6e 74 4b 65 79  row of an intKey
10c51 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
10c52 66 28 20 21 70 43 73 72 2d 3e 77 72 46 6c 61 67  f( !pCsr->wrFlag
10c53 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
10c54 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a  QLITE_READONLY;.
10c55 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70    }.  assert( !p
10c56 43 73 72 2d 3e 70 42 74 2d 3e 72 65 61 64 4f 6e  Csr->pBt->readOn
10c57 6c 79 20 0a 20 20 20 20 20 20 20 20 20 20 26 26  ly .          &&
10c58 20 70 43 73 72 2d 3e 70 42 74 2d 3e 69 6e 54 72   pCsr->pBt->inTr
10c59 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
10c5a 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20  _WRITE );.  if( 
10c5b 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70  checkReadLocks(p
10c5c 43 73 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 73  Csr->pBtree, pCs
10c5d 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 73  r->pgnoRoot, pCs
10c5e 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  r) ){.    return
10c5f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20   SQLITE_LOCKED; 
10c60 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70 43 75  /* The table pCu
10c61 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20  r points to has 
10c62 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20  a read lock */. 
10c63 20 7d 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65   }.  if( pCsr->e
10c64 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
10c65 56 41 4c 49 44 20 7c 7c 20 21 70 43 73 72 2d 3e  VALID || !pCsr->
10c66 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
10c67 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
10c68 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
10c69 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61   return accessPa
10c6a 79 6c 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73  yload(pCsr, offs
10c6b 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e  et, amt, (unsign
10c6c 65 64 20 63 68 61 72 20 2a 29 7a 2c 20 30 2c 20  ed char *)z, 0, 
10c6d 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65  1);.}../* .** Se
10c6e 74 20 61 20 66 6c 61 67 20 6f 6e 20 74 68 69 73  t a flag on this
10c6f 20 63 75 72 73 6f 72 20 74 6f 20 63 61 63 68 65   cursor to cache
10c70 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 73 20 6f   the locations o
10c71 66 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65  f pages from the
10c72 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6c 69   .** overflow li
10c73 73 74 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  st for the curre
10c74 6e 74 20 72 6f 77 2e 20 54 68 69 73 20 69 73 20  nt row. This is 
10c75 75 73 65 64 20 62 79 20 63 75 72 73 6f 72 73 20  used by cursors 
10c76 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 69 6e  opened.** for in
10c77 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49  cremental blob I
10c78 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  O only..**.** Th
10c79 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73  is function sets
10c7a 20 61 20 66 6c 61 67 20 6f 6e 6c 79 2e 20 54 68   a flag only. Th
10c7b 65 20 61 63 74 75 61 6c 20 70 61 67 65 20 6c 6f  e actual page lo
10c7c 63 61 74 69 6f 6e 20 63 61 63 68 65 0a 2a 2a 20  cation cache.** 
10c7d 28 73 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72  (stored in BtCur
10c7e 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 29  sor.aOverflow[])
10c7f 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
10c80 64 20 75 73 65 64 20 62 79 20 66 75 6e 63 74 69  d used by functi
10c81 6f 6e 0a 2a 2a 20 61 63 63 65 73 73 50 61 79 6c  on.** accessPayl
10c82 6f 61 64 28 29 20 28 74 68 65 20 77 6f 72 6b 65  oad() (the worke
10c83 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 73  r function for s
10c84 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
10c85 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ) and.** sqlite3
10c86 42 74 72 65 65 50 75 74 44 61 74 61 28 29 29 2e  BtreePutData()).
10c87 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10c88 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
10c89 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f  treeCacheOverflo
10c8a 77 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  w(BtCursor *pCur
10c8b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ){.  assert( cur
10c8c 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
10c8d 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
10c8e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
10c8f 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65  eld(pCur->pBtree
10c90 2d 3e 70 53 71 6c 69 74 65 2d 3e 6d 75 74 65 78  ->pSqlite->mutex
10c91 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 21 70  ) );.  assert(!p
10c92 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48  Cur->isIncrblobH
10c93 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73 65 72 74  andle);.  assert
10c94 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  (!pCur->aOverflo
10c95 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69 73 49 6e  w);.  pCur->isIn
10c96 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 3d 20 31  crblobHandle = 1
10c97 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
10c98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
10c99 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a  of btree.c *****
10c9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
10c9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
10c9e 6e 20 66 69 6c 65 20 76 64 62 65 66 69 66 6f 2e  n file vdbefifo.
10c9f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
10ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ca1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
10ca2 2a 20 32 30 30 35 20 4a 75 6e 65 20 31 36 0a 2a  * 2005 June 16.*
10ca3 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
10ca4 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
10ca5 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
10ca6 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
10ca7 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
10ca8 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
10ca9 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
10caa 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
10cab 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
10cac 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
10cad 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
10cae 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
10caf 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
10cb0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
10cb1 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
10cb2 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
10cb3 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
10cb4 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
10cb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
10cb9 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
10cba 6d 65 6e 74 73 20 61 20 46 49 46 4f 20 71 75 65  ments a FIFO que
10cbb 75 65 20 6f 66 20 72 6f 77 69 64 73 20 75 73 65  ue of rowids use
10cbc 64 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  d for processing
10cbd 0a 2a 2a 20 55 50 44 41 54 45 20 61 6e 64 20 44  .** UPDATE and D
10cbe 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73  ELETE statements
10cbf 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ..*/../*.** Allo
10cc0 63 61 74 65 20 61 20 6e 65 77 20 46 69 66 6f 50  cate a new FifoP
10cc1 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  age and return a
10cc2 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
10cc3 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 0a   Return NULL if.
10cc4 2a 2a 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66  ** we run out of
10cc5 20 6d 65 6d 6f 72 79 2e 20 20 4c 65 61 76 65 20   memory.  Leave 
10cc6 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67  space on the pag
10cc7 65 20 66 6f 72 20 6e 45 6e 74 72 79 20 65 6e 74  e for nEntry ent
10cc8 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ries..*/.static 
10cc9 46 69 66 6f 50 61 67 65 20 2a 61 6c 6c 6f 63 61  FifoPage *alloca
10cca 74 65 46 69 66 6f 50 61 67 65 28 69 6e 74 20 6e  teFifoPage(int n
10ccb 45 6e 74 72 79 29 7b 0a 20 20 46 69 66 6f 50 61  Entry){.  FifoPa
10ccc 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 66 28  ge *pPage;.  if(
10ccd 20 6e 45 6e 74 72 79 3e 33 32 37 36 37 20 29 7b   nEntry>32767 ){
10cce 0a 20 20 20 20 6e 45 6e 74 72 79 20 3d 20 33 32  .    nEntry = 32
10ccf 37 36 37 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  767;.  }.  pPage
10cd0 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
10cd1 63 28 20 73 69 7a 65 6f 66 28 46 69 66 6f 50 61  c( sizeof(FifoPa
10cd2 67 65 29 20 2b 20 73 69 7a 65 6f 66 28 69 36 34  ge) + sizeof(i64
10cd3 29 2a 28 6e 45 6e 74 72 79 2d 31 29 20 29 3b 0a  )*(nEntry-1) );.
10cd4 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20    if( pPage ){. 
10cd5 20 20 20 70 50 61 67 65 2d 3e 6e 53 6c 6f 74 20     pPage->nSlot 
10cd6 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 70 50  = nEntry;.    pP
10cd7 61 67 65 2d 3e 69 57 72 69 74 65 20 3d 20 30 3b  age->iWrite = 0;
10cd8 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 52 65 61  .    pPage->iRea
10cd9 64 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  d = 0;.    pPage
10cda 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d  ->pNext = 0;.  }
10cdb 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b  .  return pPage;
10cdc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
10cdd 6c 69 7a 65 20 61 20 46 69 66 6f 20 73 74 72 75  lize a Fifo stru
10cde 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
10cdf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10ce0 6c 69 74 65 33 56 64 62 65 46 69 66 6f 49 6e 69  lite3VdbeFifoIni
10ce1 74 28 46 69 66 6f 20 2a 70 46 69 66 6f 29 7b 0a  t(Fifo *pFifo){.
10ce2 20 20 6d 65 6d 73 65 74 28 70 46 69 66 6f 2c 20    memset(pFifo, 
10ce3 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 66 6f  0, sizeof(*pFifo
10ce4 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73  ));.}../*.** Pus
10ce5 68 20 61 20 73 69 6e 67 6c 65 20 36 34 2d 62 69  h a single 64-bi
10ce6 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  t integer value 
10ce7 69 6e 74 6f 20 74 68 65 20 46 69 66 6f 2e 20 20  into the Fifo.  
10ce8 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
10ce9 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 20  .** normally.   
10cea 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20  SQLITE_NOMEM is 
10ceb 72 65 74 75 72 6e 65 64 20 69 66 20 77 65 20 61  returned if we a
10cec 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c  re unable to all
10ced 6f 63 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2e  ocate.** memory.
10cee 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10cef 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
10cf0 62 65 46 69 66 6f 50 75 73 68 28 46 69 66 6f 20  beFifoPush(Fifo 
10cf1 2a 70 46 69 66 6f 2c 20 69 36 34 20 76 61 6c 29  *pFifo, i64 val)
10cf2 7b 0a 20 20 46 69 66 6f 50 61 67 65 20 2a 70 50  {.  FifoPage *pP
10cf3 61 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20 70  age;.  pPage = p
10cf4 46 69 66 6f 2d 3e 70 4c 61 73 74 3b 0a 20 20 69  Fifo->pLast;.  i
10cf5 66 28 20 70 50 61 67 65 3d 3d 30 20 29 7b 0a 20  f( pPage==0 ){. 
10cf6 20 20 20 70 50 61 67 65 20 3d 20 70 46 69 66 6f     pPage = pFifo
10cf7 2d 3e 70 4c 61 73 74 20 3d 20 70 46 69 66 6f 2d  ->pLast = pFifo-
10cf8 3e 70 46 69 72 73 74 20 3d 20 61 6c 6c 6f 63 61  >pFirst = alloca
10cf9 74 65 46 69 66 6f 50 61 67 65 28 32 30 29 3b 0a  teFifoPage(20);.
10cfa 20 20 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30      if( pPage==0
10cfb 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
10cfc 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
10cfd 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
10cfe 20 70 50 61 67 65 2d 3e 69 57 72 69 74 65 3e 3d   pPage->iWrite>=
10cff 70 50 61 67 65 2d 3e 6e 53 6c 6f 74 20 29 7b 0a  pPage->nSlot ){.
10d00 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74      pPage->pNext
10d01 20 3d 20 61 6c 6c 6f 63 61 74 65 46 69 66 6f 50   = allocateFifoP
10d02 61 67 65 28 70 46 69 66 6f 2d 3e 6e 45 6e 74 72  age(pFifo->nEntr
10d03 79 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67  y);.    if( pPag
10d04 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29 7b 0a 20  e->pNext==0 ){. 
10d05 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
10d06 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
10d07 20 20 20 20 70 50 61 67 65 20 3d 20 70 46 69 66      pPage = pFif
10d08 6f 2d 3e 70 4c 61 73 74 20 3d 20 70 50 61 67 65  o->pLast = pPage
10d09 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70  ->pNext;.  }.  p
10d0a 50 61 67 65 2d 3e 61 53 6c 6f 74 5b 70 50 61 67  Page->aSlot[pPag
10d0b 65 2d 3e 69 57 72 69 74 65 2b 2b 5d 20 3d 20 76  e->iWrite++] = v
10d0c 61 6c 3b 0a 20 20 70 46 69 66 6f 2d 3e 6e 45 6e  al;.  pFifo->nEn
10d0d 74 72 79 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20  try++;.  return 
10d0e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
10d0f 0a 2a 2a 20 45 78 74 72 61 63 74 20 61 20 73 69  .** Extract a si
10d10 6e 67 6c 65 20 36 34 2d 62 69 74 20 69 6e 74 65  ngle 64-bit inte
10d11 67 65 72 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  ger value from t
10d12 68 65 20 46 69 66 6f 2e 20 20 54 68 65 20 69 6e  he Fifo.  The in
10d13 74 65 67 65 72 0a 2a 2a 20 65 78 74 72 61 63 74  teger.** extract
10d14 65 64 20 69 73 20 74 68 65 20 6f 6e 65 20 6c 65  ed is the one le
10d15 61 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73  ast recently ins
10d16 65 72 74 65 64 2e 20 20 49 66 20 74 68 65 20 46  erted.  If the F
10d17 69 66 6f 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20  ifo is empty.** 
10d18 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
10d19 4e 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  NE..*/.SQLITE_PR
10d1a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
10d1b 33 56 64 62 65 46 69 66 6f 50 6f 70 28 46 69 66  3VdbeFifoPop(Fif
10d1c 6f 20 2a 70 46 69 66 6f 2c 20 69 36 34 20 2a 70  o *pFifo, i64 *p
10d1d 56 61 6c 29 7b 0a 20 20 46 69 66 6f 50 61 67 65  Val){.  FifoPage
10d1e 20 2a 70 50 61 67 65 3b 0a 20 20 69 66 28 20 70   *pPage;.  if( p
10d1f 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d 3d 30 20  Fifo->nEntry==0 
10d20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
10d21 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20  LITE_DONE;.  }. 
10d22 20 61 73 73 65 72 74 28 20 70 46 69 66 6f 2d 3e   assert( pFifo->
10d23 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20 20 70 50  nEntry>0 );.  pP
10d24 61 67 65 20 3d 20 70 46 69 66 6f 2d 3e 70 46 69  age = pFifo->pFi
10d25 72 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70  rst;.  assert( p
10d26 50 61 67 65 21 3d 30 20 29 3b 0a 20 20 61 73 73  Page!=0 );.  ass
10d27 65 72 74 28 20 70 50 61 67 65 2d 3e 69 57 72 69  ert( pPage->iWri
10d28 74 65 3e 70 50 61 67 65 2d 3e 69 52 65 61 64 20  te>pPage->iRead 
10d29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
10d2a 67 65 2d 3e 69 57 72 69 74 65 3c 3d 70 50 61 67  ge->iWrite<=pPag
10d2b 65 2d 3e 6e 53 6c 6f 74 20 29 3b 0a 20 20 61 73  e->nSlot );.  as
10d2c 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 52 65  sert( pPage->iRe
10d2d 61 64 3c 70 50 61 67 65 2d 3e 6e 53 6c 6f 74 20  ad<pPage->nSlot 
10d2e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
10d2f 67 65 2d 3e 69 52 65 61 64 3e 3d 30 20 29 3b 0a  ge->iRead>=0 );.
10d30 20 20 2a 70 56 61 6c 20 3d 20 70 50 61 67 65 2d    *pVal = pPage-
10d31 3e 61 53 6c 6f 74 5b 70 50 61 67 65 2d 3e 69 52  >aSlot[pPage->iR
10d32 65 61 64 2b 2b 5d 3b 0a 20 20 70 46 69 66 6f 2d  ead++];.  pFifo-
10d33 3e 6e 45 6e 74 72 79 2d 2d 3b 0a 20 20 69 66 28  >nEntry--;.  if(
10d34 20 70 50 61 67 65 2d 3e 69 52 65 61 64 3e 3d 70   pPage->iRead>=p
10d35 50 61 67 65 2d 3e 69 57 72 69 74 65 20 29 7b 0a  Page->iWrite ){.
10d36 20 20 20 20 70 46 69 66 6f 2d 3e 70 46 69 72 73      pFifo->pFirs
10d37 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74  t = pPage->pNext
10d38 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
10d39 65 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69  ee(pPage);.    i
10d3a 66 28 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79  f( pFifo->nEntry
10d3b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
10d3c 65 72 74 28 20 70 46 69 66 6f 2d 3e 70 4c 61 73  ert( pFifo->pLas
10d3d 74 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20  t==pPage );.    
10d3e 20 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d    pFifo->pLast =
10d3f 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
10d40 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69       assert( pFi
10d41 66 6f 2d 3e 70 46 69 72 73 74 21 3d 30 20 29 3b  fo->pFirst!=0 );
10d42 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
10d43 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 66      assert( pFif
10d44 6f 2d 3e 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20  o->nEntry>0 );. 
10d45 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
10d46 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
10d47 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72  Delete all infor
10d48 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 46 69  mation from a Fi
10d49 66 6f 20 6f 62 6a 65 63 74 2e 20 20 20 46 72 65  fo object.   Fre
10d4a 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 68 65 6c  e all memory hel
10d4b 64 0a 2a 2a 20 62 79 20 74 68 65 20 46 69 66 6f  d.** by the Fifo
10d4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10d4d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
10d4e 56 64 62 65 46 69 66 6f 43 6c 65 61 72 28 46 69  VdbeFifoClear(Fi
10d4f 66 6f 20 2a 70 46 69 66 6f 29 7b 0a 20 20 46 69  fo *pFifo){.  Fi
10d50 66 6f 50 61 67 65 20 2a 70 50 61 67 65 2c 20 2a  foPage *pPage, *
10d51 70 4e 65 78 74 50 61 67 65 3b 0a 20 20 66 6f 72  pNextPage;.  for
10d52 28 70 50 61 67 65 3d 70 46 69 66 6f 2d 3e 70 46  (pPage=pFifo->pF
10d53 69 72 73 74 3b 20 70 50 61 67 65 3b 20 70 50 61  irst; pPage; pPa
10d54 67 65 3d 70 4e 65 78 74 50 61 67 65 29 7b 0a 20  ge=pNextPage){. 
10d55 20 20 20 70 4e 65 78 74 50 61 67 65 20 3d 20 70     pNextPage = p
10d56 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Page->pNext;.   
10d57 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
10d58 61 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  age);.  }.  sqli
10d59 74 65 33 56 64 62 65 46 69 66 6f 49 6e 69 74 28  te3VdbeFifoInit(
10d5a 70 46 69 66 6f 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  pFifo);.}../****
10d5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
10d5c 66 20 76 64 62 65 66 69 66 6f 2e 63 20 2a 2a 2a  f vdbefifo.c ***
10d5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
10d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
10d61 20 66 69 6c 65 20 76 64 62 65 6d 65 6d 2e 63 20   file vdbemem.c 
10d62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
10d65 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a   2004 May 26.**.
10d66 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
10d67 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
10d68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
10d69 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
10d6a 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
10d6b 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
10d6c 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
10d6d 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
10d6e 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
10d6f 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
10d70 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
10d71 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
10d72 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
10d73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
10d74 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
10d75 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
10d76 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
10d77 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
10d78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
10d7c 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
10d7d 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f  ains code use to
10d7e 20 6d 61 6e 69 70 75 6c 61 74 65 20 22 4d 65 6d   manipulate "Mem
10d7f 22 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 20  " structure.  A 
10d80 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72 65 73 20  "Mem".** stores 
10d81 61 20 73 69 6e 67 6c 65 20 76 61 6c 75 65 20 69  a single value i
10d82 6e 20 74 68 65 20 56 44 42 45 2e 20 20 4d 65 6d  n the VDBE.  Mem
10d83 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74   is an opaque st
10d84 72 75 63 74 75 72 65 20 76 69 73 69 62 6c 65 0a  ructure visible.
10d85 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 74  ** only within t
10d86 68 65 20 56 44 42 45 2e 20 20 49 6e 74 65 72 66  he VDBE.  Interf
10d87 61 63 65 20 72 6f 75 74 69 6e 65 73 20 72 65 66  ace routines ref
10d88 65 72 20 74 6f 20 61 20 4d 65 6d 20 75 73 69 6e  er to a Mem usin
10d89 67 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 73 71  g the.** name sq
10d8a 6c 69 74 65 5f 76 61 6c 75 65 0a 2a 2f 0a 0a 2f  lite_value.*/../
10d8b 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
10d8c 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
10d8d 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70  ob() on the supp
10d8e 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65  lied value (type
10d8f 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72   Mem*).** P if r
10d90 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66  equired..*/.#def
10d91 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50  ine expandBlob(P
10d92 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d  ) (((P)->flags&M
10d93 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33  EM_Zero)?sqlite3
10d94 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
10d95 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49  b(P):0)../*.** I
10d96 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a  f pMem is an obj
10d97 65 63 74 20 77 69 74 68 20 61 20 76 61 6c 69 64  ect with a valid
10d98 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
10d99 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75  tation, this rou
10d9a 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20  tine.** ensures 
10d9b 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63  the internal enc
10d9c 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74  oding for the st
10d9d 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74  ring representat
10d9e 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72  ion is.** 'desir
10d9f 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53  edEnc', one of S
10da0 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
10da1 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51  TE_UTF16LE or SQ
10da2 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a  LITE_UTF16BE..**
10da3 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e  .** If pMem is n
10da4 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65  ot a string obje
10da5 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64  ct, or the encod
10da6 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e  ing of the strin
10da7 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74  g.** representat
10da8 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73  ion is already s
10da9 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65 20  tored using the 
10daa 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69  requested encodi
10dab 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a  ng, then this.**
10dac 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
10dad 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  -op..**.** SQLIT
10dae 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
10daf 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69   if the conversi
10db0 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  on is successful
10db1 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65   (or not require
10db2 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f  d)..** SQLITE_NO
10db3 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72  MEM may be retur
10db4 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28  ned if a malloc(
10db5 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63  ) fails during c
10db6 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74  onversion.** bet
10db7 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f  ween formats..*/
10db8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10db9 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
10dba 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65  hangeEncoding(Me
10dbb 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73  m *pMem, int des
10dbc 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20  iredEnc){.  int 
10dbd 72 63 3b 0a 20 20 69 66 28 20 21 28 70 4d 65 6d  rc;.  if( !(pMem
10dbe 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29  ->flags&MEM_Str)
10dbf 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64   || pMem->enc==d
10dc0 65 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20 20  esiredEnc ){.   
10dc1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
10dc2 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  K;.  }.  assert(
10dc3 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
10dc4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
10dc5 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
10dc6 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51  ex) );.#ifdef SQ
10dc7 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
10dc8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10dc9 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20 20  ERROR;.#else..  
10dca 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28  /* MemTranslate(
10dcb 29 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  ) may return SQL
10dcc 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 45  ITE_OK or SQLITE
10dcd 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45 4d  _NOMEM. If NOMEM
10dce 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20 20   is returned,.  
10dcf 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63 6f  ** then the enco
10dd0 64 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c 75  ding of the valu
10dd1 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 63  e may not have c
10dd2 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 72  hanged..  */.  r
10dd3 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
10dd4 65 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65 6d  emTranslate(pMem
10dd5 2c 20 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20  , desiredEnc);. 
10dd6 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49   assert(rc==SQLI
10dd7 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d  TE_OK    || rc==
10dd8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20  SQLITE_NOMEM);. 
10dd9 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49   assert(rc==SQLI
10dda 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d  TE_OK    || pMem
10ddb 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e  ->enc!=desiredEn
10ddc 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d  c);.  assert(rc=
10ddd 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
10dde 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69   pMem->enc==desi
10ddf 72 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72  redEnc);.  retur
10de0 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  n rc;.#endif.}..
10de1 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20 67  /*.** Make the g
10de2 69 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74 20  iven Mem object 
10de3 4d 45 4d 5f 44 79 6e 2e 0a 2a 2a 0a 2a 2a 20 52  MEM_Dyn..**.** R
10de4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
10de5 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 53 51  on success or SQ
10de6 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 6d 61  LITE_NOMEM if ma
10de7 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53  lloc fails..*/.S
10de8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10de9 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
10dea 44 79 6e 61 6d 69 63 69 66 79 28 4d 65 6d 20 2a  Dynamicify(Mem *
10deb 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a  pMem){.  int n;.
10dec 20 20 75 38 20 2a 7a 3b 0a 20 20 61 73 73 65 72    u8 *z;.  asser
10ded 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
10dee 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
10def 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
10df0 75 74 65 78 29 20 29 3b 0a 20 20 65 78 70 61 6e  utex) );.  expan
10df1 64 42 6c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 69  dBlob(pMem);.  i
10df2 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  f( (pMem->flags 
10df3 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d  & (MEM_Ephem|MEM
10df4 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 68 6f 72  _Static|MEM_Shor
10df5 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  t))==0 ){.    re
10df6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
10df7 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 70    }.  assert( (p
10df8 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
10df9 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 6e 20  _Dyn)==0 );.  n 
10dfa 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 61 73 73  = pMem->n;.  ass
10dfb 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
10dfc 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f   & (MEM_Str|MEM_
10dfd 42 6c 6f 62 29 20 29 3b 0a 20 20 7a 20 3d 20 73  Blob) );.  z = s
10dfe 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
10dff 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 2b 32 20  w(pMem->db, n+2 
10e00 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  );.  if( z==0 ){
10e01 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
10e02 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
10e03 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
10e04 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b  EM_Dyn|MEM_Term;
10e05 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20  .  pMem->xDel = 
10e06 30 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70  0;.  memcpy(z, p
10e07 4d 65 6d 2d 3e 7a 2c 20 6e 20 29 3b 0a 20 20 7a  Mem->z, n );.  z
10e08 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7a 5b 6e 2b 31  [n] = 0;.  z[n+1
10e09 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 7a  ] = 0;.  pMem->z
10e0a 20 3d 20 28 63 68 61 72 2a 29 7a 3b 0a 20 20 70   = (char*)z;.  p
10e0b 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
10e0c 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
10e0d 61 74 69 63 7c 4d 45 4d 5f 53 68 6f 72 74 29 3b  atic|MEM_Short);
10e0e 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
10e0f 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  _OK;.}../*.** If
10e10 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20   the given Mem* 
10e11 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65  has a zero-fille
10e12 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20  d tail, turn it 
10e13 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79  into an ordinary
10e14 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20  .** blob stored 
10e15 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  in dynamically a
10e16 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a  llocated space..
10e17 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
10e18 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
10e19 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
10e1a 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
10e1b 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20  mExpandBlob(Mem 
10e1c 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d  *pMem){.  if( pM
10e1d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
10e1e 5a 65 72 6f 20 29 7b 0a 20 20 20 20 63 68 61 72  Zero ){.    char
10e1f 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20   *pNew;.    int 
10e20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72  nByte;.    asser
10e21 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
10e22 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 29  & MEM_Blob)!=0 )
10e23 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 4d  ;.    nByte = pM
10e24 65 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75 2e  em->n + pMem->u.
10e25 69 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65  i;.    if( nByte
10e26 3c 3d 30 20 29 20 6e 42 79 74 65 20 3d 20 31 3b  <=0 ) nByte = 1;
10e27 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
10e28 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
10e29 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
10e2a 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
10e2b 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  );.    pNew = sq
10e2c 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
10e2d 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 42 79 74 65  (pMem->db, nByte
10e2e 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  );.    if( pNew=
10e2f 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
10e30 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
10e31 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70  .    }.    memcp
10e32 79 28 70 4e 65 77 2c 20 70 4d 65 6d 2d 3e 7a 2c  y(pNew, pMem->z,
10e33 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6d   pMem->n);.    m
10e34 65 6d 73 65 74 28 26 70 4e 65 77 5b 70 4d 65 6d  emset(&pNew[pMem
10e35 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75  ->n], 0, pMem->u
10e36 2e 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .i);.    sqlite3
10e37 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
10e38 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  Mem);.    pMem->
10e39 7a 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 4d  z = pNew;.    pM
10e3a 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75  em->n += pMem->u
10e3b 2e 69 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  .i;.    pMem->u.
10e3c 69 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d  i = 0;.    pMem-
10e3d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
10e3e 5a 65 72 6f 7c 4d 45 4d 5f 53 74 61 74 69 63 7c  Zero|MEM_Static|
10e3f 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 68  MEM_Ephem|MEM_Sh
10e40 6f 72 74 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20  ort|MEM_Term);. 
10e41 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
10e42 3d 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 7d 0a 20  = MEM_Dyn;.  }. 
10e43 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
10e44 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  K;.}.#endif.../*
10e45 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 76  .** Make the giv
10e46 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 65 69  en Mem object ei
10e47 74 68 65 72 20 4d 45 4d 5f 53 68 6f 72 74 20 6f  ther MEM_Short o
10e48 72 20 4d 45 4d 5f 44 79 6e 20 73 6f 20 74 68 61  r MEM_Dyn so tha
10e49 74 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 74 68  t bytes.** of th
10e4a 65 20 4d 65 6d 2e 7a 5b 5d 20 61 72 72 61 79 20  e Mem.z[] array 
10e4b 63 61 6e 20 62 65 20 6d 6f 64 69 66 69 65 64 2e  can be modified.
10e4c 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
10e4d 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
10e4e 73 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ss or SQLITE_NOM
10e4f 45 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69  EM if malloc fai
10e50 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ls..*/.SQLITE_PR
10e51 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
10e52 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
10e53 65 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29  eable(Mem *pMem)
10e54 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 38 20  {.  int n;.  u8 
10e55 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  *z;.  assert( pM
10e56 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
10e57 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10e58 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
10e59 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62   );.  expandBlob
10e5a 28 70 4d 65 6d 29 3b 0a 20 20 69 66 28 20 28 70  (pMem);.  if( (p
10e5b 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
10e5c 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74  M_Ephem|MEM_Stat
10e5d 69 63 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ic))==0 ){.    r
10e5e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
10e5f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28  .  }.  assert( (
10e60 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
10e61 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 61  M_Dyn)==0 );.  a
10e62 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61  ssert( pMem->fla
10e63 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45  gs & (MEM_Str|ME
10e64 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 69 66 28  M_Blob) );.  if(
10e65 20 28 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 29 2b 32   (n = pMem->n)+2
10e66 3c 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e 7a 53  <sizeof(pMem->zS
10e67 68 6f 72 74 29 20 29 7b 0a 20 20 20 20 7a 20 3d  hort) ){.    z =
10e68 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a 53 68 6f   (u8*)pMem->zSho
10e69 72 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  rt;.    pMem->fl
10e6a 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 68 6f 72 74  ags |= MEM_Short
10e6b 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 65 6c  |MEM_Term;.  }el
10e6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  se{.    z = sqli
10e6d 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
10e6e 4d 65 6d 2d 3e 64 62 2c 20 6e 2b 32 20 29 3b 0a  Mem->db, n+2 );.
10e6f 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a      if( z==0 ){.
10e70 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
10e71 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
10e72 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
10e73 20 7c 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f   |= MEM_Dyn|MEM_
10e74 54 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  Term;.    pMem->
10e75 78 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  xDel = 0;.  }.  
10e76 6d 65 6d 63 70 79 28 7a 2c 20 70 4d 65 6d 2d 3e  memcpy(z, pMem->
10e77 7a 2c 20 6e 20 29 3b 0a 20 20 7a 5b 6e 5d 20 3d  z, n );.  z[n] =
10e78 20 30 3b 0a 20 20 7a 5b 6e 2b 31 5d 20 3d 20 30   0;.  z[n+1] = 0
10e79 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63  ;.  pMem->z = (c
10e7a 68 61 72 2a 29 7a 3b 0a 20 20 70 4d 65 6d 2d 3e  har*)z;.  pMem->
10e7b 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45  flags &= ~(MEM_E
10e7c 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29  phem|MEM_Static)
10e7d 3b 0a 20 20 61 73 73 65 72 74 28 30 3d 3d 28 31  ;.  assert(0==(1
10e7e 26 28 69 6e 74 29 70 4d 65 6d 2d 3e 7a 29 29 3b  &(int)pMem->z));
10e7f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
10e80 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  _OK;.}../*.** Ma
10e81 6b 65 20 73 75 72 65 20 74 68 65 20 67 69 76 65  ke sure the give
10e82 6e 20 4d 65 6d 20 69 73 20 5c 75 30 30 30 30 20  n Mem is \u0000 
10e83 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53  terminated..*/.S
10e84 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10e85 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
10e86 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d  NulTerminate(Mem
10e87 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72   *pMem){.  asser
10e88 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
10e89 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
10e8a 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
10e8b 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 28  utex) );.  if( (
10e8c 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
10e8d 4d 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 28 70  M_Term)!=0 || (p
10e8e 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
10e8f 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20  _Str)==0 ){.    
10e90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
10e91 3b 20 20 20 2f 2a 20 4e 6f 74 68 69 6e 67 20 74  ;   /* Nothing t
10e92 6f 20 64 6f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  o do */.  }.  if
10e93 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
10e94 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
10e95 45 70 68 65 6d 29 20 29 7b 0a 20 20 20 20 72 65  Ephem) ){.    re
10e96 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65  turn sqlite3Vdbe
10e97 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
10e98 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pMem);.  }else{
10e99 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 0a 20  .    char *z; . 
10e9a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
10e9b 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d  mExpandBlob(pMem
10e9c 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  );.    z = sqlit
10e9d 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d  e3DbMallocRaw(pM
10e9e 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 6e 2b  em->db, pMem->n+
10e9f 32 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 29  2);.    if( !z )
10ea0 7b 0a 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  {.       return 
10ea1 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
10ea2 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a    }.    memcpy(z
10ea3 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d 65 6d 2d  , pMem->z, pMem-
10ea4 3e 6e 29 3b 0a 20 20 20 20 7a 5b 70 4d 65 6d 2d  >n);.    z[pMem-
10ea5 3e 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 5b 70  >n] = 0;.    z[p
10ea6 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20  Mem->n+1] = 0;. 
10ea7 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 78 44 65     if( pMem->xDe
10ea8 6c 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  l ){.      pMem-
10ea9 3e 78 44 65 6c 28 70 4d 65 6d 2d 3e 7a 29 3b 0a  >xDel(pMem->z);.
10eaa 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
10eab 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d   sqlite3_free(pM
10eac 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  em->z);.    }.  
10ead 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30    pMem->xDel = 0
10eae 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20  ;.    pMem->z = 
10eaf 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  z;.    pMem->fla
10eb0 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a  gs |= MEM_Term;.
10eb1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
10eb2 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
10eb3 20 41 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20   Add MEM_Str to 
10eb4 74 68 65 20 73 65 74 20 6f 66 20 72 65 70 72 65  the set of repre
10eb5 73 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74  sentations for t
10eb6 68 65 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e  he given Mem.  N
10eb7 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f  umbers.** are co
10eb8 6e 76 65 72 74 65 64 20 75 73 69 6e 67 20 73 71  nverted using sq
10eb9 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
10eba 2e 20 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 20  .  Converting a 
10ebb 42 4c 4f 42 20 74 6f 20 61 20 73 74 72 69 6e 67  BLOB to a string
10ebc 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  .** is a no-op..
10ebd 2a 2a 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 72  **.** Existing r
10ebe 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d  epresentations M
10ebf 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52  EM_Int and MEM_R
10ec0 65 61 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e  eal are *not* in
10ec1 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  validated..**.**
10ec2 20 41 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75   A MEM_Null valu
10ec3 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  e will never be 
10ec4 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
10ec5 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 75  unction. This fu
10ec6 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65  nction is.** use
10ec7 64 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67  d for converting
10ec8 20 76 61 6c 75 65 73 20 74 6f 20 74 65 78 74 20   values to text 
10ec9 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 74 6f  for returning to
10eca 20 74 68 65 20 75 73 65 72 20 28 69 2e 65 2e 20   the user (i.e. 
10ecb 76 69 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  via.** sqlite3_v
10ecc 61 6c 75 65 5f 74 65 78 74 28 29 29 2c 20 6f 72  alue_text()), or
10ecd 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68   for ensuring th
10ece 61 74 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20  at values to be 
10ecf 75 73 65 64 20 61 73 20 62 74 72 65 65 0a 2a 2a  used as btree.**
10ed0 20 6b 65 79 73 20 61 72 65 20 73 74 72 69 6e 67   keys are string
10ed1 73 2e 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 72  s. In the former
10ed2 20 63 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69   case a NULL poi
10ed3 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
10ed4 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64   the.** user and
10ed5 20 74 68 65 20 6c 61 74 65 72 20 69 73 20 61 6e   the later is an
10ed6 20 69 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 61   internal progra
10ed7 6d 6d 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a  mming error..*/.
10ed8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
10ed9 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
10eda 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a  mStringify(Mem *
10edb 70 4d 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a  pMem, int enc){.
10edc 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
10edd 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d  E_OK;.  int fg =
10ede 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20   pMem->flags;.  
10edf 63 68 61 72 20 2a 7a 20 3d 20 70 4d 65 6d 2d 3e  char *z = pMem->
10ee0 7a 53 68 6f 72 74 3b 0a 0a 20 20 61 73 73 65 72  zShort;..  asser
10ee1 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
10ee2 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
10ee3 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
10ee4 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
10ee5 74 28 20 21 28 66 67 26 4d 45 4d 5f 5a 65 72 6f  t( !(fg&MEM_Zero
10ee6 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ) );.  assert( !
10ee7 28 66 67 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  (fg&(MEM_Str|MEM
10ee8 5f 42 6c 6f 62 29 29 20 29 3b 0a 20 20 61 73 73  _Blob)) );.  ass
10ee9 65 72 74 28 20 66 67 26 28 4d 45 4d 5f 49 6e 74  ert( fg&(MEM_Int
10eea 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 3b 0a 0a 20  |MEM_Real) );.. 
10eeb 20 2f 2a 20 46 6f 72 20 61 20 52 65 61 6c 20 6f   /* For a Real o
10eec 72 20 49 6e 74 65 67 65 72 2c 20 75 73 65 20 73  r Integer, use s
10eed 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
10eee 29 20 74 6f 20 70 72 6f 64 75 63 65 20 74 68 65  ) to produce the
10eef 20 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74 72 69   UTF-8.  ** stri
10ef0 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
10ef1 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20  n of the value. 
10ef2 54 68 65 6e 2c 20 69 66 20 74 68 65 20 72 65 71  Then, if the req
10ef3 75 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 0a 20  uired encoding. 
10ef4 20 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c 65 20   ** is UTF-16le 
10ef5 6f 72 20 55 54 46 2d 31 36 62 65 20 64 6f 20 61  or UTF-16be do a
10ef6 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a 20 20   translation..  
10ef7 2a 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d 45 3a  ** .  ** FIX ME:
10ef8 20 49 74 20 77 6f 75 6c 64 20 62 65 20 62 65 74   It would be bet
10ef9 74 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f 73  ter if sqlite3_s
10efa 6e 70 72 69 6e 74 66 28 29 20 63 6f 75 6c 64 20  nprintf() could 
10efb 64 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a 2f 0a  do UTF-16..  */.
10efc 20 20 69 66 28 20 66 67 20 26 20 4d 45 4d 5f 49    if( fg & MEM_I
10efd 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  nt ){.    sqlite
10efe 33 5f 73 6e 70 72 69 6e 74 66 28 4e 42 46 53 2c  3_snprintf(NBFS,
10eff 20 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d   z, "%lld", pMem
10f00 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b  ->u.i);.  }else{
10f01 0a 20 20 20 20 61 73 73 65 72 74 28 20 66 67 20  .    assert( fg 
10f02 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20  & MEM_Real );.  
10f03 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
10f04 74 66 28 4e 42 46 53 2c 20 7a 2c 20 22 25 21 2e  tf(NBFS, z, "%!.
10f05 31 35 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a  15g", pMem->r);.
10f06 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20    }.  pMem->n = 
10f07 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 70 4d 65  strlen(z);.  pMe
10f08 6d 2d 3e 7a 20 3d 20 7a 3b 0a 20 20 70 4d 65 6d  m->z = z;.  pMem
10f09 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
10f0a 54 46 38 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  TF8;.  pMem->fla
10f0b 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 20 7c 20  gs |= MEM_Str | 
10f0c 4d 45 4d 5f 53 68 6f 72 74 20 7c 20 4d 45 4d 5f  MEM_Short | MEM_
10f0d 54 65 72 6d 3b 0a 20 20 73 71 6c 69 74 65 33 56  Term;.  sqlite3V
10f0e 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
10f0f 67 28 70 4d 65 6d 2c 20 65 6e 63 29 3b 0a 20 20  g(pMem, enc);.  
10f10 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
10f11 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20  .** Memory cell 
10f12 70 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 74 68  pMem contains th
10f13 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61 6e 20  e context of an 
10f14 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
10f15 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  on..** This rout
10f16 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20 66 69  ine calls the fi
10f17 6e 61 6c 69 7a 65 20 6d 65 74 68 6f 64 20 66 6f  nalize method fo
10f18 72 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 2e  r that function.
10f19 20 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20    The.** result 
10f1a 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  of the aggregate
10f1b 20 69 73 20 73 74 6f 72 65 64 20 62 61 63 6b 20   is stored back 
10f1c 69 6e 74 6f 20 70 4d 65 6d 2e 0a 2a 2a 0a 2a 2a  into pMem..**.**
10f1d 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   Return SQLITE_E
10f1e 52 52 4f 52 20 69 66 20 74 68 65 20 66 69 6e 61  RROR if the fina
10f1f 6c 69 7a 65 72 20 72 65 70 6f 72 74 73 20 61 6e  lizer reports an
10f20 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54 45 5f   error.  SQLITE_
10f21 4f 4b 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 2e  OK.** otherwise.
10f22 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10f23 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
10f24 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65  beMemFinalize(Me
10f25 6d 20 2a 70 4d 65 6d 2c 20 46 75 6e 63 44 65 66  m *pMem, FuncDef
10f26 20 2a 70 46 75 6e 63 29 7b 0a 20 20 69 6e 74 20   *pFunc){.  int 
10f27 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
10f28 20 20 69 66 28 20 70 46 75 6e 63 20 26 26 20 70    if( pFunc && p
10f29 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 20  Func->xFinalize 
10f2a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  ){.    sqlite3_c
10f2b 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20  ontext ctx;.    
10f2c 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
10f2d 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
10f2e 21 3d 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d  !=0 || pFunc==pM
10f2f 65 6d 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20  em->u.pDef );.  
10f30 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
10f31 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
10f32 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
10f33 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
10f34 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20      ctx.s.flags 
10f35 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
10f36 63 74 78 2e 73 2e 7a 20 3d 20 70 4d 65 6d 2d 3e  ctx.s.z = pMem->
10f37 7a 53 68 6f 72 74 3b 0a 20 20 20 20 63 74 78 2e  zShort;.    ctx.
10f38 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b  s.db = pMem->db;
10f39 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20  .    ctx.pMem = 
10f3a 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46  pMem;.    ctx.pF
10f3b 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20  unc = pFunc;.   
10f3c 20 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30   ctx.isError = 0
10f3d 3b 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69  ;.    pFunc->xFi
10f3e 6e 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20  nalize(&ctx);.  
10f3f 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 26    if( pMem->z &&
10f40 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e   pMem->z!=pMem->
10f41 7a 53 68 6f 72 74 20 29 7b 0a 20 20 20 20 20 20  zShort ){.      
10f42 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 4d  sqlite3_free( pM
10f43 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 7d 0a 20  em->z );.    }. 
10f44 20 20 20 2a 70 4d 65 6d 20 3d 20 63 74 78 2e 73     *pMem = ctx.s
10f45 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e  ;.    if( pMem->
10f46 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 68 6f 72  flags & MEM_Shor
10f47 74 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  t ){.      pMem-
10f48 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 53 68 6f 72  >z = pMem->zShor
10f49 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  t;.    }.    rc 
10f4a 3d 20 28 63 74 78 2e 69 73 45 72 72 6f 72 3f 53  = (ctx.isError?S
10f4b 51 4c 49 54 45 5f 45 52 52 4f 52 3a 53 51 4c 49  QLITE_ERROR:SQLI
10f4c 54 45 5f 4f 4b 29 3b 0a 20 20 7d 0a 20 20 72 65  TE_OK);.  }.  re
10f4d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
10f4e 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d 65  * Release any me
10f4f 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65  mory held by the
10f50 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c   Mem. This may l
10f51 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e 20  eave the Mem in 
10f52 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65  an.** inconsiste
10f53 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 78  nt state, for ex
10f54 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e  ample with (Mem.
10f55 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65  z==0) and.** (Me
10f56 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54  m.type==SQLITE_T
10f57 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  EXT)..*/.SQLITE_
10f58 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10f59 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
10f5a 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73  se(Mem *p){.  as
10f5b 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c  sert( p->db==0 |
10f5c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
10f5d 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
10f5e 78 29 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66  x) );.  if( p->f
10f5f 6c 61 67 73 20 26 20 28 4d 45 4d 5f 44 79 6e 7c  lags & (MEM_Dyn|
10f60 4d 45 4d 5f 41 67 67 29 20 29 7b 0a 20 20 20 20  MEM_Agg) ){.    
10f61 69 66 28 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20  if( p->xDel ){. 
10f62 20 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67       if( p->flag
10f63 73 20 26 20 4d 45 4d 5f 41 67 67 20 29 7b 0a 20  s & MEM_Agg ){. 
10f64 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
10f65 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c  beMemFinalize(p,
10f66 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20   p->u.pDef);.   
10f67 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d       assert( (p-
10f68 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67  >flags & MEM_Agg
10f69 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  )==0 );.        
10f6a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
10f6b 6c 65 61 73 65 28 70 29 3b 0a 20 20 20 20 20 20  lease(p);.      
10f6c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
10f6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 70  ->xDel((void *)p
10f6e 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ->z);.      }.  
10f6f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
10f70 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a  qlite3_free(p->z
10f71 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  );.    }.    p->
10f72 7a 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 78 44  z = 0;.    p->xD
10f73 65 6c 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  el = 0;.  }.}../
10f74 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d 65  *.** Return some
10f75 20 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 72   kind of integer
10f76 20 76 61 6c 75 65 20 77 68 69 63 68 20 69 73 20   value which is 
10f77 74 68 65 20 62 65 73 74 20 77 65 20 63 61 6e 20  the best we can 
10f78 64 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 65  do.** at represe
10f79 6e 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20  nting the value 
10f7a 74 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 72  that *pMem descr
10f7b 69 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65 67  ibes as an integ
10f7c 65 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69  er..** If pMem i
10f7d 73 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68  s an integer, th
10f7e 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  en the value is 
10f7f 65 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d 20  exact.  If pMem 
10f80 69 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e 67  is.** a floating
10f81 2d 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 20  -point then the 
10f82 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
10f83 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 70 61  s the integer pa
10f84 72 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69  rt..** If pMem i
10f85 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c  s a string or bl
10f86 6f 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b 65  ob, then we make
10f87 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63   an attempt to c
10f88 6f 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e 74  onvert.** it int
10f89 6f 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64 20  o a integer and 
10f8a 72 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49 66  return that.  If
10f8b 20 70 4d 65 6d 20 69 73 20 4e 55 4c 4c 2c 20 72   pMem is NULL, r
10f8c 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  eturn 0..**.** I
10f8d 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69  f pMem is a stri
10f8e 6e 67 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67  ng, its encoding
10f8f 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65   might be change
10f90 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
10f91 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33  VATE i64 sqlite3
10f92 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d  VdbeIntValue(Mem
10f93 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66   *pMem){.  int f
10f94 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20  lags;.  assert( 
10f95 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
10f96 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
10f97 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
10f98 78 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20  x) );.  flags = 
10f99 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69  pMem->flags;.  i
10f9a 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  f( flags & MEM_I
10f9b 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  nt ){.    return
10f9c 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65   pMem->u.i;.  }e
10f9d 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
10f9e 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
10f9f 72 65 74 75 72 6e 20 28 69 36 34 29 70 4d 65 6d  return (i64)pMem
10fa0 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ->r;.  }else if(
10fa1 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74   flags & (MEM_St
10fa2 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20  r|MEM_Blob) ){. 
10fa3 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20     i64 value;.  
10fa4 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
10fa5 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66   MEM_Str;.    if
10fa6 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  ( sqlite3VdbeCha
10fa7 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
10fa8 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20  , SQLITE_UTF8). 
10fa9 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
10faa 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
10fab 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20  ate(pMem) ){.   
10fac 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
10fad 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
10fae 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71  Mem->z );.    sq
10faf 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 6d  lite3Atoi64(pMem
10fb0 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20  ->z, &value);.  
10fb1 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a    return value;.
10fb2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
10fb3 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 0;.  }.}../*
10fb4 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62  .** Return the b
10fb5 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  est representati
10fb6 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20  on of pMem that 
10fb7 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20  we can get into 
10fb8 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66  a.** double.  If
10fb9 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79   pMem is already
10fba 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20   a double or an 
10fbb 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20  integer, return 
10fbc 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49  its.** value.  I
10fbd 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e 67  f it is a string
10fbe 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
10fbf 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
10fc0 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69   double..** If i
10fc1 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74  t is a NULL, ret
10fc2 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49  urn 0.0..*/.SQLI
10fc3 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c  TE_PRIVATE doubl
10fc4 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  e sqlite3VdbeRea
10fc5 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d  lValue(Mem *pMem
10fc6 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ){.  assert( pMe
10fc7 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
10fc8 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
10fc9 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
10fca 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66  );.  if( pMem->f
10fcb 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
10fcc 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4d  ){.    return pM
10fcd 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65 20 69  em->r;.  }else i
10fce 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
10fcf 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
10fd0 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 70  return (double)p
10fd1 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73  Mem->u.i;.  }els
10fd2 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  e if( pMem->flag
10fd3 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  s & (MEM_Str|MEM
10fd4 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 64 6f  _Blob) ){.    do
10fd5 75 62 6c 65 20 76 61 6c 20 3d 20 30 2e 30 3b 0a  uble val = 0.0;.
10fd6 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
10fd7 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20  |= MEM_Str;.    
10fd8 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43  if( sqlite3VdbeC
10fd9 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d  hangeEncoding(pM
10fda 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  em, SQLITE_UTF8)
10fdb 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74  .       || sqlit
10fdc 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d  e3VdbeMemNulTerm
10fdd 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20  inate(pMem) ){. 
10fde 20 20 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b       return 0.0;
10fdf 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
10fe0 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20  t( pMem->z );.  
10fe1 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d    sqlite3AtoF(pM
10fe2 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20  em->z, &val);.  
10fe3 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20    return val;.  
10fe4 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
10fe5 6e 20 30 2e 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  n 0.0;.  }.}../*
10fe6 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74 72 75  .** The MEM stru
10fe7 63 74 75 72 65 20 69 73 20 61 6c 72 65 61 64 79  cture is already
10fe8 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72   a MEM_Real.  Tr
10fe9 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69  y to also make i
10fea 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69  t a.** MEM_Int i
10feb 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c  f we can..*/.SQL
10fec 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
10fed 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65   sqlite3VdbeInte
10fee 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65 6d 20  gerAffinity(Mem 
10fef 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74  *pMem){.  assert
10ff0 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
10ff1 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 61 73  MEM_Real );.  as
10ff2 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
10ff3 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
10ff4 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
10ff5 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d  ->mutex) );.  pM
10ff6 65 6d 2d 3e 75 2e 69 20 3d 20 70 4d 65 6d 2d 3e  em->u.i = pMem->
10ff7 72 3b 0a 20 20 69 66 28 20 28 28 64 6f 75 62 6c  r;.  if( ((doubl
10ff8 65 29 70 4d 65 6d 2d 3e 75 2e 69 29 3d 3d 70 4d  e)pMem->u.i)==pM
10ff9 65 6d 2d 3e 72 20 29 7b 0a 20 20 20 20 70 4d 65  em->r ){.    pMe
10ffa 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  m->flags |= MEM_
10ffb 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  Int;.  }.}../*.*
10ffc 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74  * Convert pMem t
10ffd 6f 20 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20  o type integer. 
10ffe 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20   Invalidate any 
10fff 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61  prior representa
11000 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tions..*/.SQLITE
11001 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
11002 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
11003 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29  erify(Mem *pMem)
11004 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  {.  assert( pMem
11005 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
11006 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
11007 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
11008 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20  ;.  pMem->u.i = 
11009 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1100a 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 73 71 6c  lue(pMem);.  sql
1100b 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1100c 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d  se(pMem);.  pMem
1100d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1100e 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  t;.  return SQLI
1100f 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
11010 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f 20  Convert pMem so 
11011 74 68 61 74 20 69 74 20 69 73 20 6f 66 20 74 79  that it is of ty
11012 70 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20  pe MEM_Real..** 
11013 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70  Invalidate any p
11014 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74  rior representat
11015 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
11016 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
11017 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66  te3VdbeMemRealif
11018 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20  y(Mem *pMem){.  
11019 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
1101a 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
1101b 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
1101c 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1101d 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65  pMem->r = sqlite
1101e 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
1101f 4d 65 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  Mem);.  sqlite3V
11020 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d  dbeMemRelease(pM
11021 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  em);.  pMem->fla
11022 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20  gs = MEM_Real;. 
11023 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
11024 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  K;.}../*.** Conv
11025 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74  ert pMem so that
11026 20 69 74 20 68 61 73 20 74 79 70 65 73 20 4d 45   it has types ME
11027 4d 5f 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e  M_Real or MEM_In
11028 74 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e  t or both..** In
11029 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69  validate any pri
1102a 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  or representatio
1102b 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ns..*/.SQLITE_PR
1102c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1102d 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79  3VdbeMemNumerify
1102e 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64  (Mem *pMem){.  d
1102f 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20  ouble r1, r2;.  
11030 69 36 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28  i64 i;.  assert(
11031 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
11032 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
11033 6c 7c 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20  l|MEM_Null))==0 
11034 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  );.  assert( (pM
11035 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  em->flags & (MEM
11036 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21  _Blob|MEM_Str))!
11037 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
11038 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
11039 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1103a 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
1103b 78 29 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c  x) );.  r1 = sql
1103c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
1103d 65 28 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 28  e(pMem);.  i = (
1103e 69 36 34 29 72 31 3b 0a 20 20 72 32 20 3d 20 28  i64)r1;.  r2 = (
1103f 64 6f 75 62 6c 65 29 69 3b 0a 20 20 69 66 28 20  double)i;.  if( 
11040 72 31 3d 3d 72 32 20 29 7b 0a 20 20 20 20 73 71  r1==r2 ){.    sq
11041 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
11042 67 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20  gerify(pMem);.  
11043 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d  }else{.    pMem-
11044 3e 72 20 3d 20 72 31 3b 0a 20 20 20 20 70 4d 65  >r = r1;.    pMe
11045 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52  m->flags = MEM_R
11046 65 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  eal;.    sqlite3
11047 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
11048 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Mem);.  }.  retu
11049 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1104a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
1104b 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
1104c 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
1104d 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d  ue stored in *pM
1104e 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53  em to NULL..*/.S
1104f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
11050 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
11051 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d  mSetNull(Mem *pM
11052 65 6d 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64  em){.  sqlite3Vd
11053 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
11054 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  m);.  pMem->flag
11055 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
11056 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
11057 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 70 4d 65 6d  ITE_NULL;.  pMem
11058 2d 3e 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->n = 0;.}../*.*
11059 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65  * Delete any pre
1105a 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20  vious value and 
1105b 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f  set the value to
1105c 20 62 65 20 61 20 42 4c 4f 42 20 6f 66 20 6c 65   be a BLOB of le
1105d 6e 67 74 68 0a 2a 2a 20 6e 20 63 6f 6e 74 61 69  ngth.** n contai
1105e 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 73 2e 0a  ning all zeros..
1105f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11060 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
11061 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62  beMemSetZeroBlob
11062 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20  (Mem *pMem, int 
11063 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  n){.  sqlite3Vdb
11064 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
11065 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  );.  pMem->flags
11066 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f   = MEM_Blob|MEM_
11067 5a 65 72 6f 7c 4d 45 4d 5f 53 68 6f 72 74 3b 0a  Zero|MEM_Short;.
11068 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
11069 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d  QLITE_BLOB;.  pM
1106a 65 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28  em->n = 0;.  if(
1106b 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20   n<0 ) n = 0;.  
1106c 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 6e 3b 0a 20  pMem->u.i = n;. 
1106d 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d   pMem->z = pMem-
1106e 3e 7a 53 68 6f 72 74 3b 0a 20 20 70 4d 65 6d 2d  >zShort;.  pMem-
1106f 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
11070 46 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  F8;.}../*.** Del
11071 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ete any previous
11072 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74   value and set t
11073 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  he value stored 
11074 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c  in *pMem to val,
11075 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70  .** manifest typ
11076 65 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51  e INTEGER..*/.SQ
11077 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
11078 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
11079 53 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d  SetInt64(Mem *pM
1107a 65 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20  em, i64 val){.  
1107b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1107c 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70  lease(pMem);.  p
1107d 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a  Mem->u.i = val;.
1107e 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1107f 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d  MEM_Int;.  pMem-
11080 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
11081 4e 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NTEGER;.}../*.**
11082 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76   Delete any prev
11083 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73  ious value and s
11084 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  et the value sto
11085 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20  red in *pMem to 
11086 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74  val,.** manifest
11087 20 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53   type REAL..*/.S
11088 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
11089 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
1108a 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a  mSetDouble(Mem *
1108b 70 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c  pMem, double val
1108c 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
1108d 5f 69 73 6e 61 6e 28 76 61 6c 29 20 29 7b 0a 20  _isnan(val) ){. 
1108e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1108f 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a  mSetNull(pMem);.
11090 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
11091 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
11092 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d  se(pMem);.    pM
11093 65 6d 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20  em->r = val;.   
11094 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
11095 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65  EM_Real;.    pMe
11096 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
11097 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f  _FLOAT;.  }.}../
11098 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
11099 20 69 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a 65   if the Mem obje
1109a 63 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 54 45  ct contains a TE
1109b 58 54 20 6f 72 20 42 4c 4f 42 20 74 68 61 74 20  XT or BLOB that 
1109c 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65 20  is.** too large 
1109d 2d 20 77 68 6f 73 65 20 73 69 7a 65 20 65 78 63  - whose size exc
1109e 65 65 64 73 20 53 51 4c 49 54 45 5f 4d 41 58 5f  eeds SQLITE_MAX_
1109f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54  LENGTH..*/.SQLIT
110a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
110a1 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42  lite3VdbeMemTooB
110a2 69 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66  ig(Mem *p){.  if
110a3 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  ( p->flags & (ME
110a4 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
110a5 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70  ){.    int n = p
110a6 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ->n;.    if( p->
110a7 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
110a8 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70   ){.      n += p
110a9 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20  ->u.i;.    }.   
110aa 20 72 65 74 75 72 6e 20 6e 3e 53 51 4c 49 54 45   return n>SQLITE
110ab 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d  _MAX_LENGTH;.  }
110ac 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a  .  return 0; .}.
110ad 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e 20 73  ./*.** Make an s
110ae 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 70  hallow copy of p
110af 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20  From into pTo.  
110b0 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f  Prior contents o
110b1 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20 6f 76 65  f.** pTo are ove
110b2 72 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 70  rwritten.  The p
110b3 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73  From->z field is
110b4 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e   not duplicated.
110b5 20 20 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a    If.** pFrom->z
110b6 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 70   is used, then p
110b7 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20  To->z points to 
110b8 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61  the same thing a
110b9 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e  s pFrom->z.** an
110ba 64 20 66 6c 61 67 73 20 67 65 74 73 20 73 72 63  d flags gets src
110bb 54 79 70 65 20 28 65 69 74 68 65 72 20 4d 45 4d  Type (either MEM
110bc 5f 45 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74  _Ephem or MEM_St
110bd 61 74 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  atic)..*/.SQLITE
110be 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
110bf 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
110c0 6c 6f 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f  lowCopy(Mem *pTo
110c1 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72  , const Mem *pFr
110c2 6f 6d 2c 20 69 6e 74 20 73 72 63 54 79 70 65 29  om, int srcType)
110c3 7b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20  {.  memcpy(pTo, 
110c4 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 2a 70  pFrom, sizeof(*p
110c5 46 72 6f 6d 29 2d 73 69 7a 65 6f 66 28 70 46 72  From)-sizeof(pFr
110c6 6f 6d 2d 3e 7a 53 68 6f 72 74 29 29 3b 0a 20 20  om->zShort));.  
110c7 70 54 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20  pTo->xDel = 0;. 
110c8 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 20   if( pTo->flags 
110c9 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
110ca 6c 6f 62 29 20 29 7b 0a 20 20 20 20 70 54 6f 2d  lob) ){.    pTo-
110cb 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
110cc 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d  Dyn|MEM_Static|M
110cd 45 4d 5f 53 68 6f 72 74 7c 4d 45 4d 5f 45 70 68  EM_Short|MEM_Eph
110ce 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  em);.    assert(
110cf 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 45 70   srcType==MEM_Ep
110d0 68 65 6d 20 7c 7c 20 73 72 63 54 79 70 65 3d 3d  hem || srcType==
110d1 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b 0a 20 20  MEM_Static );.  
110d2 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20    pTo->flags |= 
110d3 73 72 63 54 79 70 65 3b 0a 20 20 7d 0a 7d 0a 0a  srcType;.  }.}..
110d4 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 66 75 6c  /*.** Make a ful
110d5 6c 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20  l copy of pFrom 
110d6 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72  into pTo.  Prior
110d7 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 54 6f   contents of pTo
110d8 20 61 72 65 0a 2a 2a 20 66 72 65 65 64 20 62 65   are.** freed be
110d9 66 6f 72 65 20 74 68 65 20 63 6f 70 79 20 69 73  fore the copy is
110da 20 6d 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   made..*/.SQLITE
110db 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
110dc 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28  ite3VdbeMemCopy(
110dd 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20  Mem *pTo, const 
110de 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69  Mem *pFrom){.  i
110df 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 54 6f  nt rc;.  if( pTo
110e0 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79  ->flags & MEM_Dy
110e1 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  n ){.    sqlite3
110e2 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
110e3 54 6f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  To);.  }.  sqlit
110e4 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
110e5 43 6f 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c  Copy(pTo, pFrom,
110e6 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 69   MEM_Ephem);.  i
110e7 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 20  f( pTo->flags & 
110e8 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20  MEM_Ephem ){.   
110e9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
110ea 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c  eMemMakeWriteabl
110eb 65 28 70 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b  e(pTo);.  }else{
110ec 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
110ed 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  _OK;.  }.  retur
110ee 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
110ef 72 61 6e 73 66 65 72 20 74 68 65 20 63 6f 6e 74  ransfer the cont
110f0 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 74 6f  ents of pFrom to
110f1 20 70 54 6f 2e 20 41 6e 79 20 65 78 69 73 74 69   pTo. Any existi
110f2 6e 67 20 76 61 6c 75 65 20 69 6e 20 70 54 6f 20  ng value in pTo 
110f3 69 73 0a 2a 2a 20 66 72 65 65 64 2e 20 49 66 20  is.** freed. If 
110f4 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 65  pFrom contains e
110f5 70 68 65 6d 65 72 61 6c 20 64 61 74 61 2c 20 61  phemeral data, a
110f6 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a   copy is made..*
110f7 2a 0a 2a 2a 20 70 46 72 6f 6d 20 63 6f 6e 74 61  *.** pFrom conta
110f8 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20  ins an SQL NULL 
110f9 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
110fa 65 20 72 65 74 75 72 6e 73 2e 20 20 53 51 4c 49  e returns.  SQLI
110fb 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6d 69 67 68  TE_NOMEM.** migh
110fc 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66  t be returned if
110fd 20 70 46 72 6f 6d 20 68 65 6c 64 20 65 70 68 65   pFrom held ephe
110fe 6d 65 72 61 6c 20 64 61 74 61 20 61 6e 64 20 77  meral data and w
110ff 65 20 77 65 72 65 20 75 6e 61 62 6c 65 0a 2a 2a  e were unable.**
11100 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f   to allocate eno
11101 75 67 68 20 73 70 61 63 65 20 74 6f 20 6d 61 6b  ugh space to mak
11102 65 20 61 20 63 6f 70 79 2e 0a 2a 2f 0a 53 51 4c  e a copy..*/.SQL
11103 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
11104 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
11105 76 65 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d  ve(Mem *pTo, Mem
11106 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20   *pFrom){.  int 
11107 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46  rc;.  assert( pF
11108 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  rom->db==0 || sq
11109 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1110a 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65  (pFrom->db->mute
1110b 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
1110c 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  pTo->db==0 || sq
1110d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1110e 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  (pTo->db->mutex)
1110f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46   );.  assert( pF
11110 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54  rom->db==0 || pT
11111 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f  o->db==0 || pFro
11112 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29  m->db==pTo->db )
11113 3b 0a 20 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61  ;.  if( pTo->fla
11114 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a  gs & MEM_Dyn ){.
11115 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
11116 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a  emRelease(pTo);.
11117 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f    }.  memcpy(pTo
11118 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28  , pFrom, sizeof(
11119 4d 65 6d 29 29 3b 0a 20 20 69 66 28 20 70 46 72  Mem));.  if( pFr
1111a 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  om->flags & MEM_
1111b 53 68 6f 72 74 20 29 7b 0a 20 20 20 20 70 54 6f  Short ){.    pTo
1111c 2d 3e 7a 20 3d 20 70 54 6f 2d 3e 7a 53 68 6f 72  ->z = pTo->zShor
1111d 74 3b 0a 20 20 7d 0a 20 20 70 46 72 6f 6d 2d 3e  t;.  }.  pFrom->
1111e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1111f 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20  ;.  pFrom->xDel 
11120 3d 20 30 3b 0a 20 20 69 66 28 20 70 54 6f 2d 3e  = 0;.  if( pTo->
11121 66 6c 61 67 73 20 26 20 4d 45 4d 5f 45 70 68 65  flags & MEM_Ephe
11122 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  m ){.    rc = sq
11123 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
11124 57 72 69 74 65 61 62 6c 65 28 70 54 6f 29 3b 0a  Writeable(pTo);.
11125 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
11126 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
11127 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
11128 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
11129 65 20 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d  e value of a Mem
1112a 20 74 6f 20 62 65 20 61 20 73 74 72 69 6e 67 20   to be a string 
1112b 6f 72 20 61 20 42 4c 4f 42 2e 0a 2a 2f 0a 53 51  or a BLOB..*/.SQ
1112c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1112d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1112e 65 74 53 74 72 28 0a 20 20 4d 65 6d 20 2a 70 4d  etStr(.  Mem *pM
1112f 65 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  em,          /* 
11130 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73  Memory cell to s
11131 65 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c  et to string val
11132 75 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ue */.  const ch
11133 61 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20 53  ar *z,      /* S
11134 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f  tring pointer */
11135 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20  .  int n,       
11136 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
11137 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65  in string, or ne
11138 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65  gative */.  u8 e
11139 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nc,             
1113a 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a  /* Encoding of z
1113b 2e 20 20 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a  .  0 for BLOBs *
1113c 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29  /.  void (*xDel)
1113d 28 76 6f 69 64 2a 29 20 2f 2a 20 44 65 73 74 72  (void*) /* Destr
1113e 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a  uctor function *
1113f 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  /.){.  assert( p
11140 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
11141 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
11142 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
11143 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
11144 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
11145 6d 29 3b 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a  m);.  if( !z ){.
11146 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
11147 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
11148 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
11149 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 72 65  ITE_NULL;.    re
1114a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1114b 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20    }.  pMem->z = 
1114c 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 69 66 28  (char *)z;.  if(
1114d 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54   xDel==SQLITE_ST
1114e 41 54 49 43 20 29 7b 0a 20 20 20 20 70 4d 65 6d  ATIC ){.    pMem
1114f 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74  ->flags = MEM_St
11150 61 74 69 63 3b 0a 20 20 7d 65 6c 73 65 20 69 66  atic;.  }else if
11151 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54  ( xDel==SQLITE_T
11152 52 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20  RANSIENT ){.    
11153 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
11154 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65  M_Ephem;.  }else
11155 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  {.    pMem->flag
11156 73 20 3d 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20  s = MEM_Dyn;.   
11157 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44   pMem->xDel = xD
11158 65 6c 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 6d 2d  el;.  }..  pMem-
11159 3e 65 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 70 4d  >enc = enc;.  pM
1115a 65 6d 2d 3e 74 79 70 65 20 3d 20 65 6e 63 3d 3d  em->type = enc==
1115b 30 20 3f 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20  0 ? SQLITE_BLOB 
1115c 3a 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20  : SQLITE_TEXT;. 
1115d 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 0a 20   pMem->n = n;.. 
1115e 20 61 73 73 65 72 74 28 20 65 6e 63 3d 3d 30 20   assert( enc==0 
1115f 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  || enc==SQLITE_U
11160 54 46 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49  TF8 || enc==SQLI
11161 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20 20 20  TE_UTF16LE .    
11162 20 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45    || enc==SQLITE
11163 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 73 77  _UTF16BE );.  sw
11164 69 74 63 68 28 20 65 6e 63 20 29 7b 0a 20 20 20  itch( enc ){.   
11165 20 63 61 73 65 20 30 3a 0a 20 20 20 20 20 20 70   case 0:.      p
11166 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
11167 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 70 4d  M_Blob;.      pM
11168 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  em->enc = SQLITE
11169 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 62 72 65  _UTF8;.      bre
1116a 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 53 51  ak;..    case SQ
1116b 4c 49 54 45 5f 55 54 46 38 3a 0a 20 20 20 20 20  LITE_UTF8:.     
1116c 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20   pMem->flags |= 
1116d 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 20 20 69  MEM_Str;.      i
1116e 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20  f( n<0 ){.      
1116f 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c    pMem->n = strl
11170 65 6e 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 70  en(z);.        p
11171 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
11172 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a  M_Term;.      }.
11173 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 23 69        break;..#i
11174 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
11175 54 5f 55 54 46 31 36 0a 20 20 20 20 63 61 73 65  T_UTF16.    case
11176 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3a   SQLITE_UTF16LE:
11177 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
11178 5f 55 54 46 31 36 42 45 3a 0a 20 20 20 20 20 20  _UTF16BE:.      
11179 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
1117a 45 4d 5f 53 74 72 3b 0a 20 20 20 20 20 20 69 66  EM_Str;.      if
1117b 28 20 70 4d 65 6d 2d 3e 6e 3c 30 20 29 7b 0a 20  ( pMem->n<0 ){. 
1117c 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d         pMem->n =
1117d 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74   sqlite3Utf16Byt
1117e 65 4c 65 6e 28 70 4d 65 6d 2d 3e 7a 2c 2d 31 29  eLen(pMem->z,-1)
1117f 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
11180 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72  flags |= MEM_Ter
11181 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  m;.      }.     
11182 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
11183 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65  MemHandleBom(pMe
11184 6d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  m) ){.        re
11185 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
11186 4d 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  M;.      }.#endi
11187 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
11188 5f 55 54 46 31 36 20 2a 2f 0a 20 20 7d 0a 20 20  _UTF16 */.  }.  
11189 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  if( pMem->flags&
1118a 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20  MEM_Ephem ){.   
1118b 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1118c 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
1118d 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 7d 0a 20  ble(pMem);.  }. 
1118e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1118f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  K;.}../*.** Comp
11190 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 63  are the values c
11191 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 20  ontained by the 
11192 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  two memory cells
11193 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e  , returning.** n
11194 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72  egative, zero or
11195 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d 65   positive if pMe
11196 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  m1 is less than,
11197 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72   equal to, or gr
11198 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d  eater.** than pM
11199 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64  em2. Sorting ord
1119a 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 72  er is NULL's fir
1119b 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  st, followed by 
1119c 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 72  numbers (integer
1119d 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 20  s.** and reals) 
1119e 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 6c  sorted numerical
1119f 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  ly, followed by 
111a0 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 20  text ordered by 
111a1 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a  the collating.**
111a2 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20   sequence pColl 
111a3 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62  and finally blob
111a4 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d 65  's ordered by me
111a5 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77  mcmp()..**.** Tw
111a6 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72  o NULL values ar
111a7 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75  e considered equ
111a8 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  al by this funct
111a9 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
111aa 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
111ab 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e  e3MemCompare(con
111ac 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63  st Mem *pMem1, c
111ad 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c  onst Mem *pMem2,
111ae 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a   const CollSeq *
111af 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63  pColl){.  int rc
111b0 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a  ;.  int f1, f2;.
111b1 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66    int combined_f
111b2 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65  lags;..  /* Inte
111b3 72 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 61 6e  rchange pMem1 an
111b4 64 20 70 4d 65 6d 32 20 69 66 20 74 68 65 20 63  d pMem2 if the c
111b5 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
111b6 65 20 73 70 65 63 69 66 69 65 73 0a 20 20 2a 2a  e specifies.  **
111b7 20 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 20 2a   DESC order..  *
111b8 2f 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 2d 3e  /.  f1 = pMem1->
111b9 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 70 4d  flags;.  f2 = pM
111ba 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f  em2->flags;.  co
111bb 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d 20 66  mbined_flags = f
111bc 31 7c 66 32 3b 0a 20 0a 20 20 2f 2a 20 49 66 20  1|f2;. .  /* If 
111bd 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c  one value is NUL
111be 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68  L, it is less th
111bf 61 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66  an the other. If
111c0 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a   both values.  *
111c1 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75  * are NULL, retu
111c2 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rn 0..  */.  if(
111c3 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26   combined_flags&
111c4 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
111c5 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e  return (f2&MEM_N
111c6 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e  ull) - (f1&MEM_N
111c7 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ull);.  }..  /* 
111c8 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20  If one value is 
111c9 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65  a number and the
111ca 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74   other is not, t
111cb 68 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73  he number is les
111cc 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20  s..  ** If both 
111cd 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d  are numbers, com
111ce 70 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66  pare as reals if
111cf 20 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20   one is a real, 
111d0 6f 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20  or as integers. 
111d1 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75   ** if both valu
111d2 65 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e  es are integers.
111d3 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62  .  */.  if( comb
111d4 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f  ined_flags&(MEM_
111d5 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b  Int|MEM_Real) ){
111d6 0a 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d  .    if( !(f1&(M
111d7 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29  EM_Int|MEM_Real)
111d8 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
111d9 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
111da 66 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74  f( !(f2&(MEM_Int
111db 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20  |MEM_Real)) ){. 
111dc 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a       return -1;.
111dd 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66      }.    if( (f
111de 31 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74  1 & f2 & MEM_Int
111df 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f  )==0 ){.      do
111e0 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20  uble r1, r2;.   
111e1 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52     if( (f1&MEM_R
111e2 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  eal)==0 ){.     
111e3 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 75     r1 = pMem1->u
111e4 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  .i;.      }else{
111e5 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 70 4d  .        r1 = pM
111e6 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a  em1->r;.      }.
111e7 20 20 20 20 20 20 69 66 28 20 28 66 32 26 4d 45        if( (f2&ME
111e8 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Real)==0 ){.  
111e9 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32        r2 = pMem2
111ea 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c  ->u.i;.      }el
111eb 73 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d  se{.        r2 =
111ec 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20   pMem2->r;.     
111ed 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c   }.      if( r1<
111ee 72 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  r2 ) return -1;.
111ef 20 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20        if( r1>r2 
111f0 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
111f1 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
111f2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
111f3 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20  ert( f1&MEM_Int 
111f4 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
111f5 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20   f2&MEM_Int );. 
111f6 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e       if( pMem1->
111f7 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69  u.i < pMem2->u.i
111f8 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
111f9 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75      if( pMem1->u
111fa 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20  .i > pMem2->u.i 
111fb 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
111fc 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
111fd 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f  }.  }..  /* If o
111fe 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74  ne value is a st
111ff 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68  ring and the oth
11200 65 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68  er is a blob, th
11201 65 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73  e string is less
11202 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61  ..  ** If both a
11203 72 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70  re strings, comp
11204 61 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f  are using the co
11205 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
11206 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f  s..  */.  if( co
11207 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d  mbined_flags&MEM
11208 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20  _Str ){.    if( 
11209 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d  (f1 & MEM_Str)==
1120a 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
1120b 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
1120c 66 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72  f( (f2 & MEM_Str
1120d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )==0 ){.      re
1120e 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a  turn -1;.    }..
1120f 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
11210 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65  1->enc==pMem2->e
11211 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nc );.    assert
11212 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51  ( pMem1->enc==SQ
11213 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20  LITE_UTF8 || .  
11214 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d            pMem1-
11215 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  >enc==SQLITE_UTF
11216 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65  16LE || pMem1->e
11217 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
11218 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  BE );..    /* Th
11219 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
1121a 65 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66  ence must be def
1121b 69 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69  ined at this poi
1121c 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20  nt, even if.    
1121d 2a 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65  ** the user dele
1121e 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  tes the collatio
1121f 6e 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72  n sequence after
11220 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61   the vdbe progra
11221 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70  m is.    ** comp
11222 69 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e  iled (this was n
11223 6f 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61  ot always the ca
11224 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  se)..    */.    
11225 61 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c  assert( !pColl |
11226 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b  | pColl->xCmp );
11227 0a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ..    if( pColl 
11228 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65  ){.      if( pMe
11229 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e  m1->enc==pColl->
1122a 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  enc ){.        /
1122b 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72  * The strings ar
1122c 65 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  e already in the
1122d 20 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e   correct encodin
1122e 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20  g.  Call the.   
1122f 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73       ** comparis
11230 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65  on function dire
11231 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20  ctly */.        
11232 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43  return pColl->xC
11233 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c  mp(pColl->pUser,
11234 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e  pMem1->n,pMem1->
11235 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32  z,pMem2->n,pMem2
11236 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ->z);.      }els
11237 65 7b 0a 20 20 20 20 20 20 20 20 75 38 20 6f 72  e{.        u8 or
11238 69 67 45 6e 63 20 3d 20 70 4d 65 6d 31 2d 3e 65  igEnc = pMem1->e
11239 6e 63 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  nc;.        cons
1123a 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b  t void *v1, *v2;
1123b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c  .        int n1,
1123c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20   n2;.        /* 
1123d 43 6f 6e 76 65 72 74 20 74 68 65 20 73 74 72 69  Convert the stri
1123e 6e 67 73 20 69 6e 74 6f 20 74 68 65 20 65 6e 63  ngs into the enc
1123f 6f 64 69 6e 67 20 74 68 61 74 20 74 68 65 20 63  oding that the c
11240 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 20 20  omparison.      
11241 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 65 78    ** function ex
11242 70 65 63 74 73 20 2a 2f 0a 20 20 20 20 20 20 20  pects */.       
11243 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61 6c   v1 = sqlite3Val
11244 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f  ueText((sqlite3_
11245 76 61 6c 75 65 2a 29 70 4d 65 6d 31 2c 20 70 43  value*)pMem1, pC
11246 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20  oll->enc);.     
11247 20 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20     n1 = v1==0 ? 
11248 30 20 3a 20 70 4d 65 6d 31 2d 3e 6e 3b 0a 20 20  0 : pMem1->n;.  
11249 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 31        assert( n1
1124a 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79  ==sqlite3ValueBy
1124b 74 65 73 28 28 73 71 6c 69 74 65 33 5f 76 61 6c  tes((sqlite3_val
1124c 75 65 2a 29 70 4d 65 6d 31 2c 20 70 43 6f 6c 6c  ue*)pMem1, pColl
1124d 2d 3e 65 6e 63 29 20 29 3b 0a 20 20 20 20 20 20  ->enc) );.      
1124e 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61    v2 = sqlite3Va
1124f 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33  lueText((sqlite3
11250 5f 76 61 6c 75 65 2a 29 70 4d 65 6d 32 2c 20 70  _value*)pMem2, p
11251 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20  Coll->enc);.    
11252 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f      n2 = v2==0 ?
11253 20 30 20 3a 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20   0 : pMem2->n;. 
11254 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e         assert( n
11255 32 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 42  2==sqlite3ValueB
11256 79 74 65 73 28 28 73 71 6c 69 74 65 33 5f 76 61  ytes((sqlite3_va
11257 6c 75 65 2a 29 70 4d 65 6d 32 2c 20 70 43 6f 6c  lue*)pMem2, pCol
11258 6c 2d 3e 65 6e 63 29 20 29 3b 0a 20 20 20 20 20  l->enc) );.     
11259 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d     /* Do the com
1125a 70 61 72 69 73 6f 6e 20 2a 2f 0a 20 20 20 20 20  parison */.     
1125b 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78     rc = pColl->x
1125c 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72  Cmp(pColl->pUser
1125d 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32  , n1, v1, n2, v2
1125e 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  );.        /* Co
1125f 6e 76 65 72 74 20 74 68 65 20 73 74 72 69 6e 67  nvert the string
11260 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  s back into the 
11261 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e  database encodin
11262 67 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c  g */.        sql
11263 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73  ite3ValueText((s
11264 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 4d  qlite3_value*)pM
11265 65 6d 31 2c 20 6f 72 69 67 45 6e 63 29 3b 0a 20  em1, origEnc);. 
11266 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61         sqlite3Va
11267 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33  lueText((sqlite3
11268 5f 76 61 6c 75 65 2a 29 70 4d 65 6d 32 2c 20 6f  _value*)pMem2, o
11269 72 69 67 45 6e 63 29 3b 0a 20 20 20 20 20 20 20  rigEnc);.       
1126a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1126b 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a    }.    }.    /*
1126c 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   If a NULL point
1126d 65 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73  er was passed as
1126e 20 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e   the collate fun
1126f 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f  ction, fall thro
11270 75 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68  ugh.    ** to th
11271 65 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20  e blob case and 
11272 75 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a  use memcmp().  *
11273 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74  /.  }. .  /* Bot
11274 68 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65  h values must be
11275 20 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65   blobs.  Compare
11276 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e   using memcmp().
11277 20 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63    */.  rc = memc
11278 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65  mp(pMem1->z, pMe
11279 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e  m2->z, (pMem1->n
1127a 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32  >pMem2->n)?pMem2
1127b 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20  ->n:pMem1->n);. 
1127c 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20   if( rc==0 ){.  
1127d 20 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20    rc = pMem1->n 
1127e 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a  - pMem2->n;.  }.
1127f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11280 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20  /*.** Move data 
11281 6f 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b  out of a btree k
11282 65 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64  ey or data field
11283 20 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20   and into a Mem 
11284 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
11285 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73  e data or key is
11286 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20   taken from the 
11287 65 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20  entry that pCur 
11288 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
11289 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66  nting.** to.  of
1128a 66 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74  fset and amt det
1128b 65 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74  ermine what port
1128c 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20  ion of the data 
1128d 6f 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65  or key to retrie
1128e 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72  ve..** key is tr
1128f 75 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65  ue to get the ke
11290 79 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65  y or false to ge
11291 74 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73  t data.  The res
11292 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a  ult is written.*
11293 2a 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20  * into the pMem 
11294 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  element..**.** T
11295 68 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72  he pMem structur
11296 65 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  e is assumed to 
11297 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  be uninitialized
11298 2e 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e  .  Any prior con
11299 74 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77  tent.** is overw
1129a 72 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62  ritten without b
1129b 65 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a  eing freed..**.*
1129c 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * If this routin
1129d 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20  e fails for any 
1129e 72 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72  reason (malloc r
1129f 65 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75  eturns NULL or u
112a0 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64  nable.** to read
112a1 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20   from the disk) 
112a2 74 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73  then the pMem is
112a3 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f   left in an inco
112a4 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a  nsistent state..
112a5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
112a6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
112a7 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20  eMemFromBtree(. 
112a8 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c   BtCursor *pCur,
112a9 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69     /* Cursor poi
112aa 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20  nting at record 
112ab 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a  to retrieve. */.
112ac 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20    int offset,   
112ad 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72      /* Offset fr
112ae 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  om the start of 
112af 64 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62  data to return b
112b0 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20  ytes from. */.  
112b1 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
112b2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
112b3 79 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20  ytes to return. 
112b4 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20  */.  int key,   
112b5 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
112b6 65 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d  e, retrieve from
112b7 20 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20   the btree key, 
112b8 6e 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d  not data. */.  M
112b9 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20  em *pMem        
112ba 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20   /* OUT: Return 
112bb 64 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d  data in this Mem
112bc 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29   structure. */.)
112bd 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b  {.  char *zData;
112be 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66         /* Data f
112bf 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61  rom the btree la
112c0 79 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61  yer */.  int ava
112c1 69 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e  ilable = 0; /* N
112c2 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61  umber of bytes a
112c3 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
112c4 6c 6f 63 61 6c 20 62 74 72 65 65 20 70 61 67 65  local btree page
112c5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
112c6 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  b;       /* Data
112c7 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
112c8 2a 2f 0a 0a 20 20 64 62 20 3d 20 73 71 6c 69 74  */..  db = sqlit
112c9 65 33 42 74 72 65 65 43 75 72 73 6f 72 44 62 28  e3BtreeCursorDb(
112ca 70 43 75 72 29 3b 0a 20 20 61 73 73 65 72 74 28  pCur);.  assert(
112cb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
112cc 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
112cd 3b 0a 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20  ;.  if( key ){. 
112ce 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72     zData = (char
112cf 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b   *)sqlite3BtreeK
112d0 65 79 46 65 74 63 68 28 70 43 75 72 2c 20 26 61  eyFetch(pCur, &a
112d1 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 65 6c  vailable);.  }el
112d2 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20  se{.    zData = 
112d3 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42  (char *)sqlite3B
112d4 74 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43  treeDataFetch(pC
112d5 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b  ur, &available);
112d6 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a  .  }.  assert( z
112d7 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20 20 70 4d  Data!=0 );..  pM
112d8 65 6d 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70  em->db = db;.  p
112d9 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 20 20  Mem->n = amt;.  
112da 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d  if( offset+amt<=
112db 61 76 61 69 6c 61 62 6c 65 20 29 7b 0a 20 20 20  available ){.   
112dc 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74   pMem->z = &zDat
112dd 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70  a[offset];.    p
112de 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
112df 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b  _Blob|MEM_Ephem;
112e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
112e1 74 20 72 63 3b 0a 20 20 20 20 69 66 28 20 61 6d  t rc;.    if( am
112e2 74 3e 4e 42 46 53 2d 32 20 29 7b 0a 20 20 20 20  t>NBFS-2 ){.    
112e3 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20    zData = (char 
112e4 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
112e5 63 52 61 77 28 64 62 2c 20 61 6d 74 2b 32 29 3b  cRaw(db, amt+2);
112e6 0a 20 20 20 20 20 20 69 66 28 20 21 7a 44 61 74  .      if( !zDat
112e7 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  a ){.        ret
112e8 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
112e9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
112ea 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
112eb 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d  M_Blob|MEM_Dyn|M
112ec 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  EM_Term;.      p
112ed 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20  Mem->xDel = 0;. 
112ee 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
112ef 7a 44 61 74 61 20 3d 20 26 28 70 4d 65 6d 2d 3e  zData = &(pMem->
112f0 7a 53 68 6f 72 74 5b 30 5d 29 3b 0a 20 20 20 20  zShort[0]);.    
112f1 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
112f2 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 68 6f  MEM_Blob|MEM_Sho
112f3 72 74 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  rt|MEM_Term;.   
112f4 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d   }.    pMem->z =
112f5 20 7a 44 61 74 61 3b 0a 20 20 20 20 70 4d 65 6d   zData;.    pMem
112f6 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70  ->enc = 0;.    p
112f7 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
112f8 54 45 5f 42 4c 4f 42 3b 0a 0a 20 20 20 20 69 66  TE_BLOB;..    if
112f9 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 20 20 72  ( key ){.      r
112fa 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
112fb 4b 65 79 28 70 43 75 72 2c 20 6f 66 66 73 65 74  Key(pCur, offset
112fc 2c 20 61 6d 74 2c 20 7a 44 61 74 61 29 3b 0a 20  , amt, zData);. 
112fd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
112fe 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
112ff 65 44 61 74 61 28 70 43 75 72 2c 20 6f 66 66 73  eData(pCur, offs
11300 65 74 2c 20 61 6d 74 2c 20 7a 44 61 74 61 29 3b  et, amt, zData);
11301 0a 20 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 61  .    }.    zData
11302 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20 7a  [amt] = 0;.    z
11303 44 61 74 61 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b  Data[amt+1] = 0;
11304 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
11305 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
11306 69 66 28 20 61 6d 74 3e 4e 42 46 53 2d 32 20 29  if( amt>NBFS-2 )
11307 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
11308 28 20 7a 44 61 74 61 21 3d 70 4d 65 6d 2d 3e 7a  ( zData!=pMem->z
11309 53 68 6f 72 74 20 29 3b 0a 20 20 20 20 20 20 20  Short );.       
1130a 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66   assert( pMem->f
1130b 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29  lags & MEM_Dyn )
1130c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1130d 33 5f 66 72 65 65 28 7a 44 61 74 61 29 3b 0a 20  3_free(zData);. 
1130e 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20       } else {.  
1130f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 44        assert( zD
11310 61 74 61 3d 3d 70 4d 65 6d 2d 3e 7a 53 68 6f 72  ata==pMem->zShor
11311 74 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  t );.        ass
11312 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
11313 20 26 20 4d 45 4d 5f 53 68 6f 72 74 20 29 3b 0a   & MEM_Short );.
11314 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
11315 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
11316 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
11317 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
11318 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ef NDEBUG./*.** 
11319 50 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20  Perform various 
1131a 63 68 65 63 6b 73 20 6f 6e 20 74 68 65 20 6d 65  checks on the me
1131b 6d 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 2e 20  mory cell pMem. 
1131c 41 6e 20 61 73 73 65 72 74 28 29 20 77 69 6c 6c  An assert() will
1131d 0a 2a 2a 20 66 61 69 6c 20 69 66 20 70 4d 65 6d  .** fail if pMem
1131e 20 69 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 69   is internally i
1131f 6e 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a  nconsistent..*/.
11320 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
11321 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
11322 65 6d 53 61 6e 69 74 79 28 4d 65 6d 20 2a 70 4d  emSanity(Mem *pM
11323 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73  em){.  int flags
11324 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a   = pMem->flags;.
11325 20 20 61 73 73 65 72 74 28 20 66 6c 61 67 73 21    assert( flags!
11326 3d 30 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20 64  =0 );  /* Must d
11327 65 66 69 6e 65 20 73 6f 6d 65 20 74 79 70 65 20  efine some type 
11328 2a 2f 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  */.  if( flags &
11329 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c   (MEM_Str|MEM_Bl
1132a 6f 62 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 78  ob) ){.    int x
1132b 20 3d 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f   = flags & (MEM_
1132c 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d  Static|MEM_Dyn|M
1132d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 68 6f  EM_Ephem|MEM_Sho
1132e 72 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  rt);.    assert(
1132f 20 78 21 3d 30 20 29 3b 20 20 20 20 20 20 20 20   x!=0 );        
11330 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 73 20 6d      /* Strings m
11331 75 73 74 20 64 65 66 69 6e 65 20 61 20 73 74 72  ust define a str
11332 69 6e 67 20 73 75 62 74 79 70 65 20 2a 2f 0a 20  ing subtype */. 
11333 20 20 20 61 73 73 65 72 74 28 20 28 78 20 26 20     assert( (x & 
11334 28 78 2d 31 29 29 3d 3d 30 20 29 3b 20 20 2f 2a  (x-1))==0 );  /*
11335 20 4f 6e 6c 79 20 6f 6e 65 20 73 74 72 69 6e 67   Only one string
11336 20 73 75 62 74 79 70 65 20 63 61 6e 20 62 65 20   subtype can be 
11337 64 65 66 69 6e 65 64 20 2a 2f 0a 20 20 20 20 61  defined */.    a
11338 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d  ssert( pMem->z!=
11339 30 20 29 3b 20 20 20 20 20 20 2f 2a 20 53 74 72  0 );      /* Str
1133a 69 6e 67 73 20 6d 75 73 74 20 68 61 76 65 20 61  ings must have a
1133b 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 2f 2a   value */.    /*
1133c 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f   Mem.z points to
1133d 20 4d 65 6d 2e 7a 53 68 6f 72 74 20 69 66 66 20   Mem.zShort iff 
1133e 74 68 65 20 73 75 62 74 79 70 65 20 69 73 20 4d  the subtype is M
1133f 45 4d 5f 53 68 6f 72 74 20 2a 2f 0a 20 20 20 20  EM_Short */.    
11340 61 73 73 65 72 74 28 20 28 78 20 26 20 4d 45 4d  assert( (x & MEM
11341 5f 53 68 6f 72 74 29 3d 3d 30 20 7c 7c 20 70 4d  _Short)==0 || pM
11342 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 53 68  em->z==pMem->zSh
11343 6f 72 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ort );.    asser
11344 74 28 20 28 78 20 26 20 4d 45 4d 5f 53 68 6f 72  t( (x & MEM_Shor
11345 74 29 21 3d 30 20 7c 7c 20 70 4d 65 6d 2d 3e 7a  t)!=0 || pMem->z
11346 21 3d 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74 20 29  !=pMem->zShort )
11347 3b 0a 20 20 20 20 2f 2a 20 4e 6f 20 64 65 73 74  ;.    /* No dest
11348 72 75 63 74 6f 72 20 75 6e 6c 65 73 73 20 74 68  ructor unless th
11349 65 72 65 20 69 73 20 4d 45 4d 5f 44 79 6e 20 2a  ere is MEM_Dyn *
1134a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  /.    assert( pM
1134b 65 6d 2d 3e 78 44 65 6c 3d 3d 30 20 7c 7c 20 28  em->xDel==0 || (
1134c 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
1134d 4d 5f 44 79 6e 29 21 3d 30 20 29 3b 0a 0a 20 20  M_Dyn)!=0 );..  
1134e 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 4d    if( (flags & M
1134f 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 20  EM_Str) ){.     
11350 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65   assert( pMem->e
11351 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
11352 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  || .            
11353 20 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c    pMem->enc==SQL
11354 49 54 45 5f 55 54 46 31 36 42 45 20 7c 7c 0a 20  ITE_UTF16BE ||. 
11355 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65               pMe
11356 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  m->enc==SQLITE_U
11357 54 46 31 36 4c 45 20 0a 20 20 20 20 20 20 29 3b  TF16LE .      );
11358 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
11359 20 73 74 72 69 6e 67 20 69 73 20 55 54 46 2d 38   string is UTF-8
1135a 20 65 6e 63 6f 64 65 64 20 61 6e 64 20 6e 75 6c   encoded and nul
1135b 20 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65   terminated, the
1135c 6e 20 70 4d 65 6d 2d 3e 6e 0a 20 20 20 20 20 20  n pMem->n.      
1135d 2a 2a 20 6d 75 73 74 20 62 65 20 74 68 65 20 6c  ** must be the l
1135e 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72  ength of the str
1135f 69 6e 67 2e 20 20 28 4c 61 74 65 72 3a 29 20 20  ing.  (Later:)  
11360 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
11361 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 68 61  file.      ** ha
11362 73 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64  s been corrupted
11363 2c 20 27 5c 30 30 30 27 20 63 68 61 72 61 63 74  , '\000' charact
11364 65 72 73 20 6d 69 67 68 74 20 68 61 76 65 20 62  ers might have b
11365 65 65 6e 20 69 6e 73 65 72 74 65 64 0a 20 20 20  een inserted.   
11366 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6d     ** into the m
11367 69 64 64 6c 65 20 6f 66 20 74 68 65 20 73 74 72  iddle of the str
11368 69 6e 67 2e 20 20 49 6e 20 74 68 61 74 20 63 61  ing.  In that ca
11369 73 65 2c 20 74 68 65 20 73 74 72 6c 65 6e 28 29  se, the strlen()
1136a 20 6d 69 67 68 74 0a 20 20 20 20 20 20 2a 2a 20   might.      ** 
1136b 62 65 20 6c 65 73 73 2e 0a 20 20 20 20 20 20 2a  be less..      *
1136c 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d  /.      if( pMem
1136d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
1136e 46 38 20 26 26 20 28 66 6c 61 67 73 20 26 20 4d  F8 && (flags & M
1136f 45 4d 5f 54 65 72 6d 29 20 29 7b 20 0a 20 20 20  EM_Term) ){ .   
11370 20 20 20 20 20 61 73 73 65 72 74 28 20 73 74 72       assert( str
11371 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3c 3d 70 4d  len(pMem->z)<=pM
11372 65 6d 2d 3e 6e 20 29 3b 0a 20 20 20 20 20 20 20  em->n );.       
11373 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a   assert( pMem->z
11374 5b 70 4d 65 6d 2d 3e 6e 5d 3d 3d 30 20 29 3b 0a  [pMem->n]==0 );.
11375 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
11376 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 61  }else{.    /* Ca
11377 6e 6e 6f 74 20 64 65 66 69 6e 65 20 61 20 73 74  nnot define a st
11378 72 69 6e 67 20 73 75 62 74 79 70 65 20 66 6f 72  ring subtype for
11379 20 6e 6f 6e 2d 73 74 72 69 6e 67 20 6f 62 6a 65   non-string obje
1137a 63 74 73 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  cts */.    asser
1137b 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
1137c 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45  & (MEM_Static|ME
1137d 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c  M_Dyn|MEM_Ephem|
1137e 4d 45 4d 5f 53 68 6f 72 74 29 29 3d 3d 30 20 29  MEM_Short))==0 )
1137f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  ;.    assert( pM
11380 65 6d 2d 3e 78 44 65 6c 3d 3d 30 20 29 3b 0a 20  em->xDel==0 );. 
11381 20 7d 0a 20 20 2f 2a 20 4d 45 4d 5f 4e 75 6c 6c   }.  /* MEM_Null
11382 20 65 78 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74   excludes all ot
11383 68 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 61  her types */.  a
11384 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
11385 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  ags&(MEM_Str|MEM
11386 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45  _Int|MEM_Real|ME
11387 4d 5f 42 6c 6f 62 29 29 3d 3d 30 0a 20 20 20 20  M_Blob))==0.    
11388 20 20 20 20 20 20 7c 7c 20 28 70 4d 65 6d 2d 3e        || (pMem->
11389 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 3d  flags&MEM_Null)=
1138a 3d 30 20 29 3b 0a 20 20 2f 2a 20 49 66 20 74 68  =0 );.  /* If th
1138b 65 20 4d 45 4d 20 69 73 20 62 6f 74 68 20 72 65  e MEM is both re
1138c 61 6c 20 61 6e 64 20 69 6e 74 65 67 65 72 2c 20  al and integer, 
1138d 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 65  the values are e
1138e 71 75 61 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74  qual */.  assert
1138f 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
11390 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65   (MEM_Int|MEM_Re
11391 61 6c 29 29 21 3d 28 4d 45 4d 5f 49 6e 74 7c 4d  al))!=(MEM_Int|M
11392 45 4d 5f 52 65 61 6c 29 20 0a 20 20 20 20 20 20  EM_Real) .      
11393 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 72 3d 3d      || pMem->r==
11394 70 4d 65 6d 2d 3e 75 2e 69 20 29 3b 0a 7d 0a 23  pMem->u.i );.}.#
11395 65 6e 64 69 66 0a 0a 2f 2a 20 54 68 69 73 20 66  endif../* This f
11396 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
11397 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 72 6e  available intern
11398 61 6c 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20  ally, it is not 
11399 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65  part of the.** e
1139a 78 74 65 72 6e 61 6c 20 41 50 49 2e 20 49 74 20  xternal API. It 
1139b 77 6f 72 6b 73 20 69 6e 20 61 20 73 69 6d 69 6c  works in a simil
1139c 61 72 20 77 61 79 20 74 6f 20 73 71 6c 69 74 65  ar way to sqlite
1139d 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 2c 0a  3_value_text(),.
1139e 2a 2a 20 65 78 63 65 70 74 20 74 68 65 20 64 61  ** except the da
1139f 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 69  ta returned is i
113a0 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 73  n the encoding s
113a1 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
113a2 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65  second.** parame
113a3 74 65 72 2c 20 77 68 69 63 68 20 6d 75 73 74 20  ter, which must 
113a4 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45  be one of SQLITE
113a5 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45  _UTF16BE, SQLITE
113a6 5f 55 54 46 31 36 4c 45 20 6f 72 0a 2a 2a 20 53  _UTF16LE or.** S
113a7 51 4c 49 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a 2a  QLITE_UTF8..**.*
113a8 2a 20 28 32 30 30 36 2d 30 32 2d 31 36 3a 29 20  * (2006-02-16:) 
113a9 20 54 68 65 20 65 6e 63 20 76 61 6c 75 65 20 63   The enc value c
113aa 61 6e 20 62 65 20 6f 72 2d 65 64 20 77 69 74 68  an be or-ed with
113ab 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c   SQLITE_UTF16_AL
113ac 49 47 4e 45 44 2e 0a 2a 2a 20 49 66 20 74 68 61  IGNED..** If tha
113ad 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  t is the case, t
113ae 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6d  hen the result m
113af 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20 6f  ust be aligned o
113b0 6e 20 61 6e 20 65 76 65 6e 20 62 79 74 65 0a 2a  n an even byte.*
113b1 2a 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 53  * boundary..*/.S
113b2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
113b3 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
113b4 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74  3ValueText(sqlit
113b5 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 2c 20  e3_value* pVal, 
113b6 75 38 20 65 6e 63 29 7b 0a 20 20 69 66 28 20 21  u8 enc){.  if( !
113b7 70 56 61 6c 20 29 20 72 65 74 75 72 6e 20 30 3b  pVal ) return 0;
113b8 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56 61 6c  ..  assert( pVal
113b9 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
113ba 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 56  e3_mutex_held(pV
113bb 61 6c 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  al->db->mutex) )
113bc 3b 0a 20 20 61 73 73 65 72 74 28 20 28 65 6e 63  ;.  assert( (enc
113bd 26 33 29 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 54  &3)==(enc&~SQLIT
113be 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
113bf 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c 2d   );..  if( pVal-
113c0 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20  >flags&MEM_Null 
113c1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
113c2 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28  .  }.  assert( (
113c3 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d 20  MEM_Blob>>3) == 
113c4 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56 61  MEM_Str );.  pVa
113c5 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56 61  l->flags |= (pVa
113c6 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42  l->flags & MEM_B
113c7 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61 6e  lob)>>3;.  expan
113c8 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 69  dBlob(pVal);.  i
113c9 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d  f( pVal->flags&M
113ca 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73 71  EM_Str ){.    sq
113cb 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
113cc 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e  ncoding(pVal, en
113cd 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31  c & ~SQLITE_UTF1
113ce 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20 20  6_ALIGNED);.    
113cf 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49 54  if( (enc & SQLIT
113d0 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
113d1 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 28 69 6e  !=0 && 1==(1&(in
113d2 74 29 70 56 61 6c 2d 3e 7a 29 20 29 7b 0a 20 20  t)pVal->z) ){.  
113d3 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61      assert( (pVa
113d4 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  l->flags & (MEM_
113d5 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63  Ephem|MEM_Static
113d6 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  ))!=0 );.      i
113d7 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
113d8 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70  mMakeWriteable(p
113d9 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  Val)!=SQLITE_OK 
113da 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
113db 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
113dc 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
113dd 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
113de 65 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65  e(pVal);.  }else
113df 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  {.    assert( (p
113e0 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42  Val->flags&MEM_B
113e1 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73  lob)==0 );.    s
113e2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
113e3 69 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63  ingify(pVal, enc
113e4 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30  );.    assert( 0
113e5 3d 3d 28 31 26 28 69 6e 74 29 70 56 61 6c 2d 3e  ==(1&(int)pVal->
113e6 7a 29 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  z) );.  }.  asse
113e7 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65  rt(pVal->enc==(e
113e8 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46  nc & ~SQLITE_UTF
113e9 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70  16_ALIGNED) || p
113ea 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20  Val->db==0.     
113eb 20 20 20 20 20 20 20 20 20 7c 7c 20 70 56 61 6c           || pVal
113ec 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
113ed 65 64 20 29 3b 0a 20 20 69 66 28 20 70 56 61 6c  ed );.  if( pVal
113ee 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53  ->enc==(enc & ~S
113ef 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
113f0 4e 45 44 29 20 29 7b 0a 20 20 20 20 72 65 74 75  NED) ){.    retu
113f1 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65  rn pVal->z;.  }e
113f2 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
113f3 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
113f4 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c  Create a new sql
113f5 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
113f6 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
113f7 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c  VATE sqlite3_val
113f8 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65  ue *sqlite3Value
113f9 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 64 62 29  New(sqlite3 *db)
113fa 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c  {.  Mem *p = sql
113fb 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
113fc 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29  (db, sizeof(*p))
113fd 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
113fe 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f   p->flags = MEM_
113ff 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70  Null;.    p->typ
11400 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b  e = SQLITE_NULL;
11401 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  .    p->db = db;
11402 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
11403 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
11404 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76   a new sqlite3_v
11405 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e  alue object, con
11406 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75  taining the valu
11407 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a  e of pExpr..**.*
11408 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b  * This only work
11409 73 20 66 6f 72 20 76 65 72 79 20 73 69 6d 70 6c  s for very simpl
1140a 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  e expressions th
1140b 61 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e  at consist of on
1140c 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f  e constant.** to
1140d 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 2c 20 22  ken (i.e. "5", "
1140e 35 2e 31 22 2c 20 22 4e 55 4c 4c 22 2c 20 22 27  5.1", "NULL", "'
1140f 61 20 73 74 72 69 6e 67 27 22 29 2e 20 49 66 20  a string'"). If 
11410 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63  the expression c
11411 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74  an.** be convert
11412 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f  ed directly into
11413 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74   a value, then t
11414 68 65 20 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f  he value is allo
11415 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70  cated and.** a p
11416 6f 69 6e 74 65 72 20 77 72 69 74 74 65 6e 20 74  ointer written t
11417 6f 20 2a 70 70 56 61 6c 2e 20 54 68 65 20 63 61  o *ppVal. The ca
11418 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69  ller is responsi
11419 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61  ble for dealloca
1141a 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ting.** the valu
1141b 65 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20  e by passing it 
1141c 74 6f 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  to sqlite3ValueF
1141d 72 65 65 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20  ree() later on. 
1141e 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
1141f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63  n.** cannot be c
11420 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 76 61  onverted to a va
11421 6c 75 65 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c  lue, then *ppVal
11422 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
11423 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
11424 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61  TE int sqlite3Va
11425 6c 75 65 46 72 6f 6d 45 78 70 72 28 0a 20 20 73  lueFromExpr(.  s
11426 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
11427 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
11428 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
11429 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ion */.  Expr *p
1142a 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
1142b 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
1142c 73 69 6f 6e 20 74 6f 20 65 76 61 6c 75 61 74 65  sion to evaluate
1142d 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
1142e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1142f 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75  /* Encoding to u
11430 73 65 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e  se */.  u8 affin
11431 69 74 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  ity,            
11432 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f    /* Affinity to
11433 20 75 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65   use */.  sqlite
11434 33 5f 76 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20  3_value **ppVal 
11435 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
11436 20 6e 65 77 20 76 61 6c 75 65 20 68 65 72 65 20   new value here 
11437 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a  */.){.  int op;.
11438 20 20 63 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30    char *zVal = 0
11439 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
1143a 65 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 0a 20 20  e *pVal = 0;..  
1143b 69 66 28 20 21 70 45 78 70 72 20 29 7b 0a 20 20  if( !pExpr ){.  
1143c 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20    *ppVal = 0;.  
1143d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1143e 4f 4b 3b 0a 20 20 7d 0a 20 20 6f 70 20 3d 20 70  OK;.  }.  op = p
1143f 45 78 70 72 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28  Expr->op;..  if(
11440 20 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c   op==TK_STRING |
11441 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c  | op==TK_FLOAT |
11442 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52  | op==TK_INTEGER
11443 20 29 7b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 73   ){.    zVal = s
11444 71 6c 69 74 65 33 53 74 72 4e 44 75 70 28 28 63  qlite3StrNDup((c
11445 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
11446 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65  n.z, pExpr->toke
11447 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d  n.n);.    pVal =
11448 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
11449 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  (db);.    if( !z
1144a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 29 20 67  Val || !pVal ) g
1144b 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20  oto no_mem;.    
1144c 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a  sqlite3Dequote(z
1144d 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Val);.    sqlite
1144e 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61  3ValueSetStr(pVa
1144f 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c  l, -1, zVal, SQL
11450 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 65  ITE_UTF8, sqlite
11451 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 69 66 28  3_free);.    if(
11452 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52   (op==TK_INTEGER
11453 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54   || op==TK_FLOAT
11454 20 29 20 26 26 20 61 66 66 69 6e 69 74 79 3d 3d   ) && affinity==
11455 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
11456 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
11457 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69  ValueApplyAffini
11458 74 79 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  ty(pVal, SQLITE_
11459 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63  AFF_NUMERIC, enc
1145a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1145b 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
1145c 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56  ApplyAffinity(pV
1145d 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e  al, affinity, en
1145e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  c);.    }.  }els
1145f 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49  e if( op==TK_UMI
11460 4e 55 53 20 29 20 7b 0a 20 20 20 20 69 66 28 20  NUS ) {.    if( 
11461 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74  SQLITE_OK==sqlit
11462 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
11463 64 62 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  db,pExpr->pLeft,
11464 65 6e 63 2c 61 66 66 69 6e 69 74 79 2c 26 70 56  enc,affinity,&pV
11465 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 70 56 61  al) ){.      pVa
11466 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20 2a 20 70 56  l->u.i = -1 * pV
11467 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 70  al->u.i;.      p
11468 56 61 6c 2d 3e 72 20 3d 20 2d 31 2e 30 20 2a 20  Val->r = -1.0 * 
11469 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20  pVal->r;.    }. 
1146a 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1146b 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
1146c 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f  RAL.  else if( o
1146d 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20  p==TK_BLOB ){.  
1146e 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20    int nVal;.    
1146f 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61  pVal = sqlite3Va
11470 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20  lueNew(db);.    
11471 7a 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 53 74  zVal = sqlite3St
11472 72 4e 44 75 70 28 28 63 68 61 72 2a 29 70 45 78  rNDup((char*)pEx
11473 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2b 31 2c 20 70  pr->token.z+1, p
11474 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 29  Expr->token.n-1)
11475 3b 0a 20 20 20 20 69 66 28 20 21 7a 56 61 6c 20  ;.    if( !zVal 
11476 7c 7c 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20  || !pVal ) goto 
11477 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  no_mem;.    sqli
11478 74 65 33 44 65 71 75 6f 74 65 28 7a 56 61 6c 29  te3Dequote(zVal)
11479 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 74 72  ;.    nVal = str
1147a 6c 65 6e 28 7a 56 61 6c 29 2f 32 3b 0a 20 20 20  len(zVal)/2;.   
1147b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1147c 65 74 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69  etStr(pVal, sqli
1147d 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c  te3HexToBlob(db,
1147e 20 7a 56 61 6c 29 2c 20 6e 56 61 6c 2c 30 2c 73   zVal), nVal,0,s
1147f 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
11480 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
11481 56 61 6c 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Val);.  }.#endif
11482 0a 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 61  ..  *ppVal = pVa
11483 6c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  l;.  return SQLI
11484 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a  TE_OK;..no_mem:.
11485 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
11486 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  ed = 1;.  sqlite
11487 33 5f 66 72 65 65 28 7a 56 61 6c 29 3b 0a 20 20  3_free(zVal);.  
11488 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
11489 28 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c  (pVal);.  *ppVal
1148a 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53   = 0;.  return S
1148b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a  QLITE_NOMEM;.}..
1148c 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
1148d 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f 66   string value of
1148e 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   an sqlite3_valu
1148f 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49  e object.*/.SQLI
11490 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
11491 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
11492 74 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  tr(.  sqlite3_va
11493 6c 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56  lue *v,     /* V
11494 61 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20 2a  alue to be set *
11495 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20  /.  int n,      
11496 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
11497 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a 20  gth of string z 
11498 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
11499 2a 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65  *z,        /* Te
1149a 78 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73 74  xt of the new st
1149b 72 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63  ring */.  u8 enc
1149c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1149d 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75  /* Encoding to u
1149e 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  se */.  void (*x
1149f 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a  Del)(void*)   /*
114a0 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   Destructor for 
114a1 74 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b  the string */.){
114a2 0a 20 20 69 66 28 20 76 20 29 20 73 71 6c 69 74  .  if( v ) sqlit
114a3 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
114a4 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20  (Mem *)v, z, n, 
114a5 65 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f  enc, xDel);.}../
114a6 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 6c  *.** Free an sql
114a7 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
114a8 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  t.*/.SQLITE_PRIV
114a9 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
114aa 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65  ValueFree(sqlite
114ab 33 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69  3_value *v){.  i
114ac 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a  f( !v ) return;.
114ad 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
114ae 74 53 74 72 28 76 2c 20 30 2c 20 30 2c 20 53 51  tStr(v, 0, 0, SQ
114af 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
114b0 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c  E_STATIC);.  sql
114b1 69 74 65 33 5f 66 72 65 65 28 76 29 3b 0a 7d 0a  ite3_free(v);.}.
114b2 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
114b3 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
114b4 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  s in the sqlite3
114b5 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 61 73  _value object as
114b6 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69  suming.** that i
114b7 74 20 75 73 65 73 20 74 68 65 20 65 6e 63 6f 64  t uses the encod
114b8 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c  ing "enc".*/.SQL
114b9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
114ba 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65  sqlite3ValueByte
114bb 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  s(sqlite3_value 
114bc 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a  *pVal, u8 enc){.
114bd 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a    Mem *p = (Mem*
114be 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 28 70 2d  )pVal;.  if( (p-
114bf 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
114c0 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  b)!=0 || sqlite3
114c1 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
114c2 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 66 28 20  enc) ){.    if( 
114c3 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a  p->flags & MEM_Z
114c4 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ero ){.      ret
114c5 75 72 6e 20 70 2d 3e 6e 2b 70 2d 3e 75 2e 69 3b  urn p->n+p->u.i;
114c6 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
114c7 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20    return p->n;. 
114c8 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
114c9 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  n 0;.}../*******
114ca 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
114cb 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a  dbemem.c *******
114cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114ce 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
114cf 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
114d0 6c 65 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a  le vdbeaux.c ***
114d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114d3 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
114d4 30 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a  03 September 6.*
114d5 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
114d6 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
114d7 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
114d8 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
114d9 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
114da 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
114db 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
114dc 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
114dd 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
114de 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
114df 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
114e0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
114e1 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
114e2 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
114e3 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
114e4 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
114e5 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
114e6 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
114e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
114eb 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
114ec 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 66 6f  ins code used fo
114ed 72 20 63 72 65 61 74 69 6e 67 2c 20 64 65 73 74  r creating, dest
114ee 72 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75  roying, and popu
114ef 6c 61 74 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45  lating.** a VDBE
114f0 20 28 6f 72 20 61 6e 20 22 73 71 6c 69 74 65 33   (or an "sqlite3
114f1 5f 73 74 6d 74 22 20 61 73 20 69 74 20 69 73 20  _stmt" as it is 
114f2 6b 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75 74  known to the out
114f3 73 69 64 65 20 77 6f 72 6c 64 2e 29 20 20 50 72  side world.)  Pr
114f4 69 6f 72 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f  ior.** to versio
114f5 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69  n 2.8.7, all thi
114f6 73 20 63 6f 64 65 20 77 61 73 20 63 6f 6d 62 69  s code was combi
114f7 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 76 64 62  ned into the vdb
114f8 65 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e  e.c source file.
114f9 0a 2a 2a 20 42 75 74 20 74 68 61 74 20 66 69 6c  .** But that fil
114fa 65 20 77 61 73 20 67 65 74 74 69 6e 67 20 74 6f  e was getting to
114fb 6f 20 62 69 67 20 73 6f 20 74 68 69 73 20 73 75  o big so this su
114fc 62 72 6f 75 74 69 6e 65 73 20 77 65 72 65 20 73  broutines were s
114fd 70 6c 69 74 20 6f 75 74 2e 0a 2a 2f 0a 0a 0a 0a  plit out..*/....
114fe 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 75 67  /*.** When debug
114ff 67 69 6e 67 20 74 68 65 20 63 6f 64 65 20 67 65  ging the code ge
11500 6e 65 72 61 74 6f 72 20 69 6e 20 61 20 73 79 6d  nerator in a sym
11501 62 6f 6c 69 63 20 64 65 62 75 67 67 65 72 2c 20  bolic debugger, 
11502 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 20 74  one can.** set t
11503 68 65 20 73 71 6c 69 74 65 33 5f 76 64 62 65 5f  he sqlite3_vdbe_
11504 61 64 64 6f 70 5f 74 72 61 63 65 20 74 6f 20 31  addop_trace to 1
11505 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73   and all opcodes
11506 20 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64   will be printed
11507 0a 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20  .** as they are 
11508 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73  added to the ins
11509 74 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e  truction stream.
1150a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1150b 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 41  E_DEBUG.SQLITE_A
1150c 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76  PI int sqlite3_v
1150d 64 62 65 5f 61 64 64 6f 70 5f 74 72 61 63 65 20  dbe_addop_trace 
1150e 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  = 0;.#endif.../*
1150f 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
11510 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73   virtual databas
11511 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c  e engine..*/.SQL
11512 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
11513 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65   *sqlite3VdbeCre
11514 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ate(sqlite3 *db)
11515 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 70  {.  Vdbe *p;.  p
11516 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
11517 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
11518 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 28  f(Vdbe) );.  if(
11519 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
1151a 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a  ;.  p->db = db;.
1151b 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20    if( db->pVdbe 
1151c 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65  ){.    db->pVdbe
1151d 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d  ->pPrev = p;.  }
1151e 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62  .  p->pNext = db
1151f 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50  ->pVdbe;.  p->pP
11520 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70  rev = 0;.  db->p
11521 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d  Vdbe = p;.  p->m
11522 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
11523 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e  C_INIT;.  return
11524 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   p;.}../*.** Rem
11525 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 74  ember the SQL st
11526 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 61  ring for a prepa
11527 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  red statement..*
11528 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
11529 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1152a 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 2c  eSetSql(Vdbe *p,
1152b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
1152c 69 6e 74 20 6e 29 7b 0a 20 20 69 66 28 20 70 3d  int n){.  if( p=
1152d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  =0 ) return;.  a
1152e 73 73 65 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d  ssert( p->zSql==
1152f 30 20 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d  0 );.  p->zSql =
11530 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75   sqlite3DbStrNDu
11531 70 28 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a  p(p->db, z, n);.
11532 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
11533 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74  the SQL associat
11534 65 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72  ed with a prepar
11535 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a  ed statement.*/.
11536 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
11537 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
11538 65 33 56 64 62 65 47 65 74 53 71 6c 28 56 64 62  e3VdbeGetSql(Vdb
11539 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  e *p){.  return 
1153a 70 2d 3e 7a 53 71 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a  p->zSql;.}../*.*
1153b 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f 6e 74 65  * Swap all conte
1153c 6e 74 20 62 65 74 77 65 65 6e 20 74 77 6f 20 56  nt between two V
1153d 44 42 45 20 73 74 72 75 63 74 75 72 65 73 2e 0a  DBE structures..
1153e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1153f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
11540 62 65 53 77 61 70 28 56 64 62 65 20 2a 70 41 2c  beSwap(Vdbe *pA,
11541 20 56 64 62 65 20 2a 70 42 29 7b 0a 20 20 56 64   Vdbe *pB){.  Vd
11542 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a 20  be tmp, *pTmp;. 
11543 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 69   char *zTmp;.  i
11544 6e 74 20 6e 54 6d 70 3b 0a 20 20 74 6d 70 20 3d  nt nTmp;.  tmp =
11545 20 2a 70 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70   *pA;.  *pA = *p
11546 42 3b 0a 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a  B;.  *pB = tmp;.
11547 20 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65    pTmp = pA->pNe
11548 78 74 3b 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20  xt;.  pA->pNext 
11549 3d 20 70 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70  = pB->pNext;.  p
1154a 42 2d 3e 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b  B->pNext = pTmp;
1154b 0a 20 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50  .  pTmp = pA->pP
1154c 72 65 76 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76  rev;.  pA->pPrev
1154d 20 3d 20 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20   = pB->pPrev;.  
1154e 70 42 2d 3e 70 50 72 65 76 20 3d 20 70 54 6d 70  pB->pPrev = pTmp
1154f 3b 0a 20 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a  ;.  zTmp = pA->z
11550 53 71 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20  Sql;.  pA->zSql 
11551 3d 20 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42  = pB->zSql;.  pB
11552 2d 3e 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20  ->zSql = zTmp;. 
11553 20 6e 54 6d 70 20 3d 20 70 41 2d 3e 6e 53 71 6c   nTmp = pA->nSql
11554 3b 0a 20 20 70 41 2d 3e 6e 53 71 6c 20 3d 20 70  ;.  pA->nSql = p
11555 42 2d 3e 6e 53 71 6c 3b 0a 20 20 70 42 2d 3e 6e  B->nSql;.  pB->n
11556 53 71 6c 20 3d 20 6e 54 6d 70 3b 0a 7d 0a 0a 23  Sql = nTmp;.}..#
11557 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
11558 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72  UG./*.** Turn tr
11559 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a  acing on or off.
1155a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1155b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1155c 62 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c  beTrace(Vdbe *p,
1155d 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20   FILE *trace){. 
1155e 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63   p->trace = trac
1155f 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e;.}.#endif../*.
11560 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64  ** Resize the Vd
11561 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20  be.aOp array so 
11562 74 68 61 74 20 69 74 20 63 6f 6e 74 61 69 6e 73  that it contains
11563 20 61 74 20 6c 65 61 73 74 20 4e 0a 2a 2a 20 65   at least N.** e
11564 6c 65 6d 65 6e 74 73 2e 20 49 66 20 74 68 65 20  lements. If the 
11565 56 64 62 65 20 69 73 20 69 6e 20 56 44 42 45 5f  Vdbe is in VDBE_
11566 4d 41 47 49 43 5f 52 55 4e 20 73 74 61 74 65 2c  MAGIC_RUN state,
11567 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 56 64 62   then.** the Vdb
11568 65 2e 61 4f 70 20 61 72 72 61 79 20 77 69 6c 6c  e.aOp array will
11569 20 62 65 20 73 69 7a 65 64 20 74 6f 20 63 6f 6e   be sized to con
1156a 74 61 69 6e 20 65 78 61 63 74 6c 79 20 4e 0a 2a  tain exactly N.*
1156b 2a 20 65 6c 65 6d 65 6e 74 73 2e 20 56 64 62 65  * elements. Vdbe
1156c 2e 6e 4f 70 41 6c 6c 6f 63 20 69 73 20 73 65 74  .nOpAlloc is set
1156d 20 74 6f 20 72 65 66 6c 65 63 74 20 74 68 65 20   to reflect the 
1156e 6e 65 77 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74  new size of.** t
1156f 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20  he array..**.** 
11570 49 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d  If an out-of-mem
11571 6f 72 79 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ory error occurs
11572 20 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20   while resizing 
11573 74 68 65 20 61 72 72 61 79 2c 0a 2a 2a 20 56 64  the array,.** Vd
11574 62 65 2e 61 4f 70 20 61 6e 64 20 56 64 62 65 2e  be.aOp and Vdbe.
11575 6e 4f 70 41 6c 6c 6f 63 20 72 65 6d 61 69 6e 20  nOpAlloc remain 
11576 75 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 20  unchanged (this 
11577 69 73 20 73 6f 20 74 68 61 74 0a 2a 2a 20 61 6e  is so that.** an
11578 79 20 6f 70 63 6f 64 65 73 20 61 6c 72 65 61 64  y opcodes alread
11579 79 20 61 6c 6c 6f 63 61 74 65 64 20 63 61 6e 20  y allocated can 
1157a 62 65 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61  be correctly dea
1157b 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e  llocated.** alon
1157c 67 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20  g with the rest 
1157d 6f 66 20 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f  of the Vdbe)..*/
1157e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73  .static void res
1157f 69 7a 65 4f 70 41 72 72 61 79 28 56 64 62 65 20  izeOpArray(Vdbe 
11580 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e  *p, int N){.  in
11581 74 20 72 75 6e 4d 6f 64 65 20 3d 20 70 2d 3e 6d  t runMode = p->m
11582 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
11583 5f 52 55 4e 3b 0a 20 20 69 66 28 20 72 75 6e 4d  _RUN;.  if( runM
11584 6f 64 65 20 7c 7c 20 70 2d 3e 6e 4f 70 41 6c 6c  ode || p->nOpAll
11585 6f 63 3c 4e 20 29 7b 0a 20 20 20 20 56 64 62 65  oc<N ){.    Vdbe
11586 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e  Op *pNew;.    in
11587 74 20 6e 4e 65 77 20 3d 20 4e 20 2b 20 31 30 30  t nNew = N + 100
11588 2a 28 21 72 75 6e 4d 6f 64 65 29 3b 0a 20 20 20  *(!runMode);.   
11589 20 69 6e 74 20 6f 6c 64 53 69 7a 65 20 3d 20 70   int oldSize = p
1158a 2d 3e 6e 4f 70 41 6c 6c 6f 63 3b 0a 20 20 20 20  ->nOpAlloc;.    
1158b 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
1158c 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 2c 20 70  Realloc(p->db, p
1158d 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73 69 7a 65  ->aOp, nNew*size
1158e 6f 66 28 4f 70 29 29 3b 0a 20 20 20 20 69 66 28  of(Op));.    if(
1158f 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 70   pNew ){.      p
11590 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 6e 4e 65  ->nOpAlloc = nNe
11591 77 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 20  w;.      p->aOp 
11592 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 69 66  = pNew;.      if
11593 28 20 6e 4e 65 77 3e 6f 6c 64 53 69 7a 65 20 29  ( nNew>oldSize )
11594 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  {.        memset
11595 28 26 70 2d 3e 61 4f 70 5b 6f 6c 64 53 69 7a 65  (&p->aOp[oldSize
11596 5d 2c 20 30 2c 20 28 6e 4e 65 77 2d 6f 6c 64 53  ], 0, (nNew-oldS
11597 69 7a 65 29 2a 73 69 7a 65 6f 66 28 4f 70 29 29  ize)*sizeof(Op))
11598 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
11599 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
1159a 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69   a new instructi
1159b 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f  on to the list o
1159c 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 63  f instructions c
1159d 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a 2a  urrent in the.**
1159e 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20 74   VDBE.  Return t
1159f 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68  he address of th
115a0 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f  e new instructio
115a1 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  n..**.** Paramet
115a2 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 20  ers:.**.**    p 
115a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 6f                Po
115a4 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 42  inter to the VDB
115a5 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 20  E.**.**    op   
115a6 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 6f             The o
115a7 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 69  pcode for this i
115a8 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a  nstruction.**.**
115a9 20 20 20 20 70 31 2c 20 70 32 20 20 20 20 20 20      p1, p2      
115aa 20 20 20 20 46 69 72 73 74 20 74 77 6f 20 6f 66      First two of
115ab 20 74 68 65 20 74 68 72 65 65 20 70 6f 73 73 69   the three possi
115ac 62 6c 65 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a  ble operands..**
115ad 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69  .** Use the sqli
115ae 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
115af 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  bel() function t
115b0 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73  o fix an address
115b1 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69   and.** the sqli
115b2 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
115b3 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68  ) function to ch
115b4 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
115b5 66 20 74 68 65 20 50 33 0a 2a 2a 20 6f 70 65 72  f the P3.** oper
115b6 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  and..*/.SQLITE_P
115b7 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
115b8 65 33 56 64 62 65 41 64 64 4f 70 28 56 64 62 65  e3VdbeAddOp(Vdbe
115b9 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74   *p, int op, int
115ba 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20   p1, int p2){.  
115bb 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f 70 20  int i;.  VdbeOp 
115bc 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70 2d 3e  *pOp;..  i = p->
115bd 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70  nOp;.  assert( p
115be 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
115bf 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66  GIC_INIT );.  if
115c0 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69  ( p->nOpAlloc<=i
115c1 20 29 7b 0a 20 20 20 20 72 65 73 69 7a 65 4f 70   ){.    resizeOp
115c2 41 72 72 61 79 28 70 2c 20 69 2b 31 29 3b 0a 20  Array(p, i+1);. 
115c3 20 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61     if( p->db->ma
115c4 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
115c5 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
115c6 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70    }.  }.  p->nOp
115c7 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e  ++;.  pOp = &p->
115c8 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f  aOp[i];.  pOp->o
115c9 70 63 6f 64 65 20 3d 20 6f 70 3b 0a 20 20 70 4f  pcode = op;.  pO
115ca 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f  p->p1 = p1;.  pO
115cb 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f  p->p2 = p2;.  pO
115cc 70 2d 3e 70 33 20 3d 20 30 3b 0a 20 20 70 4f 70  p->p3 = 0;.  pOp
115cd 2d 3e 70 33 74 79 70 65 20 3d 20 50 33 5f 4e 4f  ->p3type = P3_NO
115ce 54 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78 70 69  TUSED;.  p->expi
115cf 72 65 64 20 3d 20 30 3b 0a 23 69 66 64 65 66 20  red = 0;.#ifdef 
115d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69  SQLITE_DEBUG.  i
115d1 66 28 20 73 71 6c 69 74 65 33 5f 76 64 62 65 5f  f( sqlite3_vdbe_
115d2 61 64 64 6f 70 5f 74 72 61 63 65 20 29 20 73 71  addop_trace ) sq
115d3 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70  lite3VdbePrintOp
115d4 28 30 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69  (0, i, &p->aOp[i
115d5 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74  ]);.#endif.  ret
115d6 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn i;.}../*.** 
115d7 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68  Add an opcode th
115d8 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20  at includes the 
115d9 70 33 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c  p3 value..*/.SQL
115da 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
115db 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 56  sqlite3VdbeOp3(V
115dc 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20  dbe *p, int op, 
115dd 69 6e 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20  int p1, int p2, 
115de 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 33 2c  const char *zP3,
115df 69 6e 74 20 70 33 74 79 70 65 29 7b 0a 20 20 69  int p3type){.  i
115e0 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  nt addr = sqlite
115e1 33 56 64 62 65 41 64 64 4f 70 28 70 2c 20 6f 70  3VdbeAddOp(p, op
115e2 2c 20 70 31 2c 20 70 32 29 3b 0a 20 20 73 71 6c  , p1, p2);.  sql
115e3 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
115e4 28 70 2c 20 61 64 64 72 2c 20 7a 50 33 2c 20 70  (p, addr, zP3, p
115e5 33 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72 6e  3type);.  return
115e6 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   addr;.}../*.** 
115e7 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 79 6d  Create a new sym
115e8 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72 20  bolic label for 
115e9 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  an instruction t
115ea 68 61 74 20 68 61 73 20 79 65 74 20 74 6f 20 62  hat has yet to b
115eb 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68 65  e.** coded.  The
115ec 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20   symbolic label 
115ed 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61  is really just a
115ee 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72   negative number
115ef 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c 20  .  The.** label 
115f0 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74  can be used as t
115f1 68 65 20 50 32 20 76 61 6c 75 65 20 6f 66 20 61  he P2 value of a
115f2 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c 61  n operation.  La
115f3 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65  ter, when.** the
115f4 20 6c 61 62 65 6c 20 69 73 20 72 65 73 6f 6c 76   label is resolv
115f5 65 64 20 74 6f 20 61 20 73 70 65 63 69 66 69 63  ed to a specific
115f6 20 61 64 64 72 65 73 73 2c 20 74 68 65 20 56 44   address, the VD
115f7 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a 20  BE will scan.** 
115f8 74 68 72 6f 75 67 68 20 69 74 73 20 6f 70 65 72  through its oper
115f9 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20 63  ation list and c
115fa 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65 73  hange all values
115fb 20 6f 66 20 50 32 20 77 68 69 63 68 20 6d 61 74   of P2 which mat
115fc 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20  ch.** the label 
115fd 69 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c 76 65  into the resolve
115fe 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a  d address..**.**
115ff 20 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 73 20   The VDBE knows 
11600 74 68 61 74 20 61 20 50 32 20 76 61 6c 75 65 20  that a P2 value 
11601 69 73 20 61 20 6c 61 62 65 6c 20 62 65 63 61 75  is a label becau
11602 73 65 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a 2a  se labels are.**
11603 20 61 6c 77 61 79 73 20 6e 65 67 61 74 69 76 65   always negative
11604 20 61 6e 64 20 50 32 20 76 61 6c 75 65 73 20 61   and P2 values a
11605 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65  re suppose to be
11606 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a   non-negative..*
11607 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 61 74  * Hence, a negat
11608 69 76 65 20 50 32 20 76 61 6c 75 65 20 69 73 20  ive P2 value is 
11609 61 20 6c 61 62 65 6c 20 74 68 61 74 20 68 61 73  a label that has
1160a 20 79 65 74 20 74 6f 20 62 65 20 72 65 73 6f 6c   yet to be resol
1160b 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f 20  ved..**.** Zero 
1160c 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61  is returned if a
1160d 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e   malloc() fails.
1160e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1160f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
11610 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65  beMakeLabel(Vdbe
11611 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
11612 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b 2b   i = p->nLabel++
11613 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ;.  assert( p->m
11614 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
11615 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 69  _INIT );.  if( i
11616 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63  >=p->nLabelAlloc
11617 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65   ){.    p->nLabe
11618 6c 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 4c 61 62  lAlloc = p->nLab
11619 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 31 30 3b 0a  elAlloc*2 + 10;.
1161a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20      p->aLabel = 
1161b 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
1161c 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  OrFree(p->db, p-
1161d 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20  >aLabel,.       
1161e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1161f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e               p->
11620 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a 73 69 7a 65  nLabelAlloc*size
11621 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29  of(p->aLabel[0])
11622 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
11623 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d  aLabel ){.    p-
11624 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b  >aLabel[i] = -1;
11625 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31  .  }.  return -1
11626 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73  -i;.}../*.** Res
11627 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 74  olve label "x" t
11628 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73 73  o be the address
11629 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73   of the next ins
1162a 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62  truction to.** b
1162b 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 65  e inserted.  The
1162c 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 6d   parameter "x" m
1162d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62  ust have been ob
1162e 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 61  tained from.** a
1162f 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73   prior call to s
11630 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
11631 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  bel()..*/.SQLITE
11632 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
11633 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
11634 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 69  Label(Vdbe *p, i
11635 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 3d  nt x){.  int j =
11636 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 28   -1-x;.  assert(
11637 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
11638 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
11639 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20  assert( j>=0 && 
1163a 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20  j<p->nLabel );. 
1163b 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29   if( p->aLabel )
1163c 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b  {.    p->aLabel[
1163d 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d  j] = p->nOp;.  }
1163e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1163f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 6f 70 63   non-zero if opc
11640 6f 64 65 20 27 6f 70 27 20 69 73 20 67 75 61 72  ode 'op' is guar
11641 65 6e 74 65 65 64 20 6e 6f 74 20 74 6f 20 70 75  enteed not to pu
11642 73 68 20 6d 6f 72 65 20 76 61 6c 75 65 73 0a 2a  sh more values.*
11643 2a 20 6f 6e 74 6f 20 74 68 65 20 56 44 42 45 20  * onto the VDBE 
11644 73 74 61 63 6b 20 74 68 61 6e 20 69 74 20 70 6f  stack than it po
11645 70 73 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69  ps off..*/.stati
11646 63 20 69 6e 74 20 6f 70 63 6f 64 65 4e 6f 50 75  c int opcodeNoPu
11647 73 68 28 75 38 20 6f 70 29 7b 0a 20 20 2f 2a 20  sh(u8 op){.  /* 
11648 54 68 65 20 31 30 20 4e 4f 50 55 53 48 5f 4d 41  The 10 NOPUSH_MA
11649 53 4b 5f 6e 20 63 6f 6e 73 74 61 6e 74 73 20 61  SK_n constants a
1164a 72 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68  re defined in th
1164b 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  e automatically.
1164c 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 68    ** generated h
1164d 65 61 64 65 72 20 66 69 6c 65 20 6f 70 63 6f 64  eader file opcod
1164e 65 73 2e 68 2e 20 45 61 63 68 20 69 73 20 61 20  es.h. Each is a 
1164f 31 36 2d 62 69 74 20 62 69 74 6d 61 73 6b 2c 20  16-bit bitmask, 
11650 6f 6e 65 0a 20 20 2a 2a 20 62 69 74 20 63 6f 72  one.  ** bit cor
11651 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 65 61  responding to ea
11652 63 68 20 6f 70 63 6f 64 65 20 69 6d 70 6c 65 6d  ch opcode implem
11653 65 6e 74 65 64 20 62 79 20 74 68 65 20 76 69 72  ented by the vir
11654 74 75 61 6c 0a 20 20 2a 2a 20 6d 61 63 68 69 6e  tual.  ** machin
11655 65 20 69 6e 20 76 64 62 65 2e 63 2e 20 54 68 65  e in vdbe.c. The
11656 20 62 69 74 20 69 73 20 74 72 75 65 20 69 66 20   bit is true if 
11657 74 68 65 20 77 6f 72 64 20 22 6e 6f 2d 70 75 73  the word "no-pus
11658 68 22 20 61 70 70 65 61 72 73 0a 20 20 2a 2a 20  h" appears.  ** 
11659 69 6e 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20  in a comment on 
1165a 74 68 65 20 73 61 6d 65 20 6c 69 6e 65 20 61 73  the same line as
1165b 20 74 68 65 20 22 63 61 73 65 20 4f 50 5f 58 58   the "case OP_XX
1165c 58 3a 22 20 69 6e 20 0a 20 20 2a 2a 20 73 71 6c  X:" in .  ** sql
1165d 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20 69  ite3VdbeExec() i
1165e 6e 20 76 64 62 65 2e 63 2e 0a 20 20 2a 2a 0a 20  n vdbe.c..  **. 
1165f 20 2a 2a 20 49 66 20 74 68 65 20 62 69 74 20 69   ** If the bit i
11660 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65  s true, then the
11661 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f   corresponding o
11662 70 63 6f 64 65 20 69 73 20 67 75 61 72 65 6e 74  pcode is guarent
11663 65 65 64 20 6e 6f 74 0a 20 20 2a 2a 20 74 6f 20  eed not.  ** to 
11664 67 72 6f 77 20 74 68 65 20 73 74 61 63 6b 20 77  grow the stack w
11665 68 65 6e 20 69 74 20 69 73 20 65 78 65 63 75 74  hen it is execut
11666 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ed. Otherwise, i
11667 74 20 6d 61 79 20 67 72 6f 77 20 74 68 65 0a 20  t may grow the. 
11668 20 2a 2a 20 73 74 61 63 6b 20 62 79 20 61 74 20   ** stack by at 
11669 6d 6f 73 74 20 6f 6e 65 20 65 6e 74 72 79 2e 0a  most one entry..
1166a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 4f 50 55 53 48    **.  ** NOPUSH
1166b 5f 4d 41 53 4b 5f 30 20 63 6f 72 72 65 73 70 6f  _MASK_0 correspo
1166c 6e 64 73 20 74 6f 20 6f 70 63 6f 64 65 73 20 30  nds to opcodes 0
1166d 20 74 6f 20 31 35 2e 20 4e 4f 50 55 53 48 5f 4d   to 15. NOPUSH_M
1166e 41 53 4b 5f 31 20 63 6f 6e 74 61 69 6e 73 0a 20  ASK_1 contains. 
1166f 20 2a 2a 20 6f 6e 65 20 62 69 74 20 66 6f 72 20   ** one bit for 
11670 6f 70 63 6f 64 65 73 20 31 36 20 74 6f 20 33 31  opcodes 16 to 31
11671 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 20 20 2a  , and so on..  *
11672 2a 0a 20 20 2a 2a 20 31 36 2d 62 69 74 20 62 69  *.  ** 16-bit bi
11673 74 6d 61 73 6b 73 20 28 72 61 74 68 65 72 20 74  tmasks (rather t
11674 68 61 6e 20 33 32 2d 62 69 74 29 20 61 72 65 20  han 32-bit) are 
11675 73 70 65 63 69 66 69 65 64 20 69 6e 20 6f 70 63  specified in opc
11676 6f 64 65 73 2e 68 20 0a 20 20 2a 2a 20 62 65 63  odes.h .  ** bec
11677 61 75 73 65 20 74 68 65 20 66 69 6c 65 20 69 73  ause the file is
11678 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 6e   generated by an
11679 20 61 77 6b 20 70 72 6f 67 72 61 6d 2e 20 41 77   awk program. Aw
1167a 6b 20 6d 61 6e 69 70 75 6c 61 74 65 73 0a 20 20  k manipulates.  
1167b 2a 2a 20 61 6c 6c 20 6e 75 6d 62 65 72 73 20 61  ** all numbers a
1167c 73 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74  s floating-point
1167d 20 61 6e 64 20 77 65 20 64 6f 6e 27 74 20 77 61   and we don't wa
1167e 6e 74 20 74 6f 20 72 69 73 6b 20 61 20 72 6f 75  nt to risk a rou
1167f 6e 64 69 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72  nding.  ** error
11680 20 69 66 20 73 6f 6d 65 6f 6e 65 20 62 75 69 6c   if someone buil
11681 64 73 20 77 69 74 68 20 61 6e 20 61 77 6b 20 74  ds with an awk t
11682 68 61 74 20 75 73 65 73 20 28 66 6f 72 20 65 78  hat uses (for ex
11683 61 6d 70 6c 65 29 20 33 32 2d 62 69 74 20 0a 20  ample) 32-bit . 
11684 20 2a 2a 20 49 45 45 45 20 66 6c 6f 61 74 73 2e   ** IEEE floats.
11685 0a 20 20 2a 2f 20 0a 20 20 73 74 61 74 69 63 20  .  */ .  static 
11686 63 6f 6e 73 74 20 75 33 32 20 6d 61 73 6b 73 5b  const u32 masks[
11687 35 5d 20 3d 20 7b 0a 20 20 20 20 4e 4f 50 55 53  5] = {.    NOPUS
11688 48 5f 4d 41 53 4b 5f 30 20 2b 20 28 28 28 75 6e  H_MASK_0 + (((un
11689 73 69 67 6e 65 64 29 4e 4f 50 55 53 48 5f 4d 41  signed)NOPUSH_MA
1168a 53 4b 5f 31 29 3c 3c 31 36 29 2c 0a 20 20 20 20  SK_1)<<16),.    
1168b 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 32 20 2b 20  NOPUSH_MASK_2 + 
1168c 28 28 28 75 6e 73 69 67 6e 65 64 29 4e 4f 50 55  (((unsigned)NOPU
1168d 53 48 5f 4d 41 53 4b 5f 33 29 3c 3c 31 36 29 2c  SH_MASK_3)<<16),
1168e 0a 20 20 20 20 4e 4f 50 55 53 48 5f 4d 41 53 4b  .    NOPUSH_MASK
1168f 5f 34 20 2b 20 28 28 28 75 6e 73 69 67 6e 65 64  _4 + (((unsigned
11690 29 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 35 29 3c  )NOPUSH_MASK_5)<
11691 3c 31 36 29 2c 0a 20 20 20 20 4e 4f 50 55 53 48  <16),.    NOPUSH
11692 5f 4d 41 53 4b 5f 36 20 2b 20 28 28 28 75 6e 73  _MASK_6 + (((uns
11693 69 67 6e 65 64 29 4e 4f 50 55 53 48 5f 4d 41 53  igned)NOPUSH_MAS
11694 4b 5f 37 29 3c 3c 31 36 29 2c 0a 20 20 20 20 4e  K_7)<<16),.    N
11695 4f 50 55 53 48 5f 4d 41 53 4b 5f 38 20 2b 20 28  OPUSH_MASK_8 + (
11696 28 28 75 6e 73 69 67 6e 65 64 29 4e 4f 50 55 53  ((unsigned)NOPUS
11697 48 5f 4d 41 53 4b 5f 39 29 3c 3c 31 36 29 0a 20  H_MASK_9)<<16). 
11698 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70   };.  assert( op
11699 3c 33 32 2a 35 20 29 3b 0a 20 20 72 65 74 75 72  <32*5 );.  retur
1169a 6e 20 28 6d 61 73 6b 73 5b 6f 70 3e 3e 35 5d 20  n (masks[op>>5] 
1169b 26 20 28 31 3c 3c 28 6f 70 26 30 78 31 46 29 29  & (1<<(op&0x1F))
1169c 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44  );.}..#ifndef ND
1169d 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
1169e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1169f 64 62 65 4f 70 63 6f 64 65 4e 6f 50 75 73 68 28  dbeOpcodeNoPush(
116a0 75 38 20 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e  u8 op){.  return
116a1 20 6f 70 63 6f 64 65 4e 6f 50 75 73 68 28 6f 70   opcodeNoPush(op
116a2 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
116a3 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  ** Loop through 
116a4 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b  the program look
116a5 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65  ing for P2 value
116a6 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74  s that are negat
116a7 69 76 65 2e 0a 2a 2a 20 45 61 63 68 20 73 75 63  ive..** Each suc
116a8 68 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62  h value is a lab
116a9 65 6c 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65  el.  Resolve the
116aa 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e   label by settin
116ab 67 20 74 68 65 20 50 32 0a 2a 2a 20 76 61 6c 75  g the P2.** valu
116ac 65 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74  e to its correct
116ad 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e   non-zero value.
116ae 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
116af 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  ine is called on
116b0 63 65 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63  ce after all opc
116b1 6f 64 65 73 20 68 61 76 65 20 62 65 65 6e 20 69  odes have been i
116b2 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56  nserted..**.** V
116b3 61 72 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e  ariable *pMaxFun
116b4 63 41 72 67 73 20 69 73 20 73 65 74 20 74 6f 20  cArgs is set to 
116b5 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75  the maximum valu
116b6 65 20 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75  e of any P2 argu
116b7 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f  ment .** to an O
116b8 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41  P_Function, OP_A
116b9 67 67 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69  ggStep or OP_VFi
116ba 6c 74 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69  lter opcode. Thi
116bb 73 20 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a  s is used by .**
116bc 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
116bd 52 65 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20  Ready() to size 
116be 74 68 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d  the Vdbe.apArg[]
116bf 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68   array..**.** Th
116c0 65 20 69 6e 74 65 67 65 72 20 2a 70 4d 61 78 53  e integer *pMaxS
116c1 74 61 63 6b 20 69 73 20 73 65 74 20 74 6f 20 74  tack is set to t
116c2 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
116c3 72 20 6f 66 20 76 64 62 65 20 73 74 61 63 6b 0a  r of vdbe stack.
116c4 2a 2a 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  ** entries that 
116c5 73 74 61 74 69 63 20 61 6e 61 6c 79 73 69 73 20  static analysis 
116c6 72 65 76 65 61 6c 73 20 74 68 69 73 20 70 72 6f  reveals this pro
116c7 67 72 61 6d 20 6d 69 67 68 74 20 6e 65 65 64 2e  gram might need.
116c8 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
116c9 69 6e 65 20 61 6c 73 6f 20 64 6f 65 73 20 74 68  ine also does th
116ca 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 74 69  e following opti
116cb 6d 69 7a 61 74 69 6f 6e 3a 20 20 49 74 20 73 63  mization:  It sc
116cc 61 6e 73 20 66 6f 72 0a 2a 2a 20 48 61 6c 74 20  ans for.** Halt 
116cd 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 77 68 65  instructions whe
116ce 72 65 20 50 31 3d 3d 53 51 4c 49 54 45 5f 43 4f  re P1==SQLITE_CO
116cf 4e 53 54 52 41 49 4e 54 20 6f 72 20 50 32 3d 3d  NSTRAINT or P2==
116d0 4f 45 5f 41 62 6f 72 74 20 6f 72 20 66 6f 72 0a  OE_Abort or for.
116d1 2a 2a 20 49 64 78 49 6e 73 65 72 74 20 69 6e 73  ** IdxInsert ins
116d2 74 72 75 63 74 69 6f 6e 73 20 77 68 65 72 65 20  tructions where 
116d3 50 32 21 3d 30 2e 20 20 49 66 20 6e 6f 20 73 75  P2!=0.  If no su
116d4 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  ch instruction i
116d5 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65 6e  s.** found, then
116d6 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e 74   every Statement
116d7 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
116d8 63 68 61 6e 67 65 64 20 74 6f 20 61 20 4e 6f 6f  changed to a Noo
116d9 70 2e 20 20 49 6e 0a 2a 2a 20 74 68 69 73 20 77  p.  In.** this w
116da 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63 72 65  ay, we avoid cre
116db 61 74 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d  ating the statem
116dc 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ent journal file
116dd 20 75 6e 6e 65 63 65 73 73 61 72 69 6c 79 2e 0a   unnecessarily..
116de 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
116df 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56  esolveP2Values(V
116e0 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61  dbe *p, int *pMa
116e1 78 46 75 6e 63 41 72 67 73 2c 20 69 6e 74 20 2a  xFuncArgs, int *
116e2 70 4d 61 78 53 74 61 63 6b 29 7b 0a 20 20 69 6e  pMaxStack){.  in
116e3 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41  t i;.  int nMaxA
116e4 72 67 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  rgs = 0;.  int n
116e5 4d 61 78 53 74 61 63 6b 20 3d 20 70 2d 3e 6e 4f  MaxStack = p->nO
116e6 70 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  p;.  Op *pOp;.  
116e7 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20 70 2d  int *aLabel = p-
116e8 3e 61 4c 61 62 65 6c 3b 0a 20 20 69 6e 74 20 64  >aLabel;.  int d
116e9 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c  oesStatementRoll
116ea 62 61 63 6b 20 3d 20 30 3b 0a 20 20 69 6e 74 20  back = 0;.  int 
116eb 68 61 73 53 74 61 74 65 6d 65 6e 74 42 65 67 69  hasStatementBegi
116ec 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 4f 70  n = 0;.  for(pOp
116ed 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f  =p->aOp, i=p->nO
116ee 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20  p-1; i>=0; i--, 
116ef 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20 6f  pOp++){.    u8 o
116f0 70 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63  pcode = pOp->opc
116f1 6f 64 65 3b 0a 0a 20 20 20 20 69 66 28 20 6f 70  ode;..    if( op
116f2 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f  code==OP_Functio
116f3 6e 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f  n || opcode==OP_
116f4 41 67 67 53 74 65 70 20 0a 23 69 66 6e 64 65 66  AggStep .#ifndef
116f5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
116f6 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20  TUALTABLE.      
116f7 20 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f    || opcode==OP_
116f8 56 55 70 64 61 74 65 0a 23 65 6e 64 69 66 0a 20  VUpdate.#endif. 
116f9 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20     ){.      if( 
116fa 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 41 72 67 73  pOp->p2>nMaxArgs
116fb 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f   ) nMaxArgs = pO
116fc 70 2d 3e 70 32 3b 0a 20 20 20 20 7d 0a 20 20 20  p->p2;.    }.   
116fd 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f   if( opcode==OP_
116fe 48 61 6c 74 20 29 7b 0a 20 20 20 20 20 20 69 66  Halt ){.      if
116ff 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49 54  ( pOp->p1==SQLIT
11700 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26 26 20  E_CONSTRAINT && 
11701 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 62 6f 72  pOp->p2==OE_Abor
11702 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 65  t ){.        doe
11703 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61  sStatementRollba
11704 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ck = 1;.      }.
11705 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70      }else if( op
11706 63 6f 64 65 3d 3d 4f 50 5f 53 74 61 74 65 6d 65  code==OP_Stateme
11707 6e 74 20 29 7b 0a 20 20 20 20 20 20 68 61 73 53  nt ){.      hasS
11708 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20  tatementBegin = 
11709 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  1;.#ifndef SQLIT
1170a 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1170b 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  BLE.    }else if
1170c 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70  ( opcode==OP_VUp
1170d 64 61 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d  date || opcode==
1170e 4f 50 5f 56 52 65 6e 61 6d 65 20 29 7b 0a 20 20  OP_VRename ){.  
1170f 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d 65 6e      doesStatemen
11710 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a 20  tRollback = 1;. 
11711 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63     }else if( opc
11712 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20  ode==OP_VFilter 
11713 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a  ){.      int n;.
11714 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
11715 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b  >nOp - i >= 3 );
11716 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
11717 4f 70 5b 2d 32 5d 2e 6f 70 63 6f 64 65 3d 3d 4f  Op[-2].opcode==O
11718 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20 20  P_Integer );.   
11719 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 32 5d 2e 70     n = pOp[-2].p
1171a 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e  1;.      if( n>n
1171b 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72  MaxArgs ) nMaxAr
1171c 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20  gs = n;.#endif. 
1171d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 63     }.    if( opc
1171e 6f 64 65 4e 6f 50 75 73 68 28 6f 70 63 6f 64 65  odeNoPush(opcode
1171f 29 20 29 7b 0a 20 20 20 20 20 20 6e 4d 61 78 53  ) ){.      nMaxS
11720 74 61 63 6b 2d 2d 3b 0a 20 20 20 20 7d 0a 0a 20  tack--;.    }.. 
11721 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 3d     if( pOp->p2>=
11722 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
11723 20 20 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70    assert( -1-pOp
11724 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29  ->p2<p->nLabel )
11725 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20  ;.    pOp->p2 = 
11726 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70  aLabel[-1-pOp->p
11727 32 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  2];.  }.  sqlite
11728 33 5f 66 72 65 65 28 70 2d 3e 61 4c 61 62 65 6c  3_free(p->aLabel
11729 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d  );.  p->aLabel =
1172a 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63   0;..  *pMaxFunc
1172b 41 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b  Args = nMaxArgs;
1172c 0a 20 20 2a 70 4d 61 78 53 74 61 63 6b 20 3d 20  .  *pMaxStack = 
1172d 6e 4d 61 78 53 74 61 63 6b 3b 0a 0a 20 20 2f 2a  nMaxStack;..  /*
1172e 20 49 66 20 77 65 20 6e 65 76 65 72 20 72 6f 6c   If we never rol
1172f 6c 62 61 63 6b 20 61 20 73 74 61 74 65 6d 65 6e  lback a statemen
11730 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  t transaction, t
11731 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  hen statement.  
11732 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  ** transactions 
11733 61 72 65 20 6e 6f 74 20 6e 65 65 64 65 64 2e 20  are not needed. 
11734 20 53 6f 20 63 68 61 6e 67 65 20 65 76 65 72 79   So change every
11735 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74 0a 20 20   OP_Statement.  
11736 2a 2a 20 6f 70 63 6f 64 65 20 69 6e 74 6f 20 61  ** opcode into a
11737 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20 54 68 69 73  n OP_Noop.  This
11738 20 61 76 6f 69 64 20 61 20 63 61 6c 6c 20 74 6f   avoid a call to
11739 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78   sqlite3OsOpenEx
1173a 63 6c 75 73 69 76 65 28 29 0a 20 20 2a 2a 20 77  clusive().  ** w
1173b 68 69 63 68 20 63 61 6e 20 62 65 20 65 78 70 65  hich can be expe
1173c 6e 73 69 76 65 20 6f 6e 20 73 6f 6d 65 20 70 6c  nsive on some pl
1173d 61 74 66 6f 72 6d 73 2e 0a 20 20 2a 2f 0a 20 20  atforms..  */.  
1173e 69 66 28 20 68 61 73 53 74 61 74 65 6d 65 6e 74  if( hasStatement
1173f 42 65 67 69 6e 20 26 26 20 21 64 6f 65 73 53 74  Begin && !doesSt
11740 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20  atementRollback 
11741 29 7b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70  ){.    for(pOp=p
11742 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d  ->aOp, i=p->nOp-
11743 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f  1; i>=0; i--, pO
11744 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  p++){.      if( 
11745 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
11746 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20  Statement ){.   
11747 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65       pOp->opcode
11748 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20   = OP_Noop;.    
11749 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
1174a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1174b 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
1174c 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1174d 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  n to be inserted
1174e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1174f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
11750 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56  dbeCurrentAddr(V
11751 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  dbe *p){.  asser
11752 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
11753 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
11754 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b    return p->nOp;
11755 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
11756 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70  whole list of op
11757 65 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20  erations to the 
11758 6f 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e  operation stack.
11759 20 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20    Return the.** 
1175a 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66  address of the f
1175b 69 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61  irst operation a
1175c 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dded..*/.SQLITE_
1175d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1175e 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
1175f 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f  (Vdbe *p, int nO
11760 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f  p, VdbeOpList co
11761 6e 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74  nst *aOp){.  int
11762 20 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28   addr;.  assert(
11763 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
11764 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
11765 72 65 73 69 7a 65 4f 70 41 72 72 61 79 28 70 2c  resizeOpArray(p,
11766 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 29 3b 0a   p->nOp + nOp);.
11767 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c    if( p->db->mal
11768 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
11769 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1176a 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a   addr = p->nOp;.
1176b 20 20 69 66 28 20 6e 4f 70 3e 30 20 29 7b 0a 20    if( nOp>0 ){. 
1176c 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64     int i;.    Vd
1176d 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a  beOpList const *
1176e 70 49 6e 20 3d 20 61 4f 70 3b 0a 20 20 20 20 66  pIn = aOp;.    f
1176f 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69  or(i=0; i<nOp; i
11770 2b 2b 2c 20 70 49 6e 2b 2b 29 7b 0a 20 20 20 20  ++, pIn++){.    
11771 20 20 69 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e    int p2 = pIn->
11772 70 32 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70  p2;.      VdbeOp
11773 20 2a 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4f 70   *pOut = &p->aOp
11774 5b 69 2b 61 64 64 72 5d 3b 0a 20 20 20 20 20 20  [i+addr];.      
11775 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 20 3d 20 70  pOut->opcode = p
11776 49 6e 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20  In->opcode;.    
11777 20 20 70 4f 75 74 2d 3e 70 31 20 3d 20 70 49 6e    pOut->p1 = pIn
11778 2d 3e 70 31 3b 0a 20 20 20 20 20 20 70 4f 75 74  ->p1;.      pOut
11779 2d 3e 70 32 20 3d 20 70 32 3c 30 20 3f 20 61 64  ->p2 = p2<0 ? ad
1177a 64 72 20 2b 20 41 44 44 52 28 70 32 29 20 3a 20  dr + ADDR(p2) : 
1177b 70 32 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  p2;.      pOut->
1177c 70 33 20 3d 20 70 49 6e 2d 3e 70 33 3b 0a 20 20  p3 = pIn->p3;.  
1177d 20 20 20 20 70 4f 75 74 2d 3e 70 33 74 79 70 65      pOut->p3type
1177e 20 3d 20 70 49 6e 2d 3e 70 33 20 3f 20 50 33 5f   = pIn->p3 ? P3_
1177f 53 54 41 54 49 43 20 3a 20 50 33 5f 4e 4f 54 55  STATIC : P3_NOTU
11780 53 45 44 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  SED;.#ifdef SQLI
11781 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69  TE_DEBUG.      i
11782 66 28 20 73 71 6c 69 74 65 33 5f 76 64 62 65 5f  f( sqlite3_vdbe_
11783 61 64 64 6f 70 5f 74 72 61 63 65 20 29 7b 0a 20  addop_trace ){. 
11784 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
11785 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61  bePrintOp(0, i+a
11786 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61  ddr, &p->aOp[i+a
11787 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23  ddr]);.      }.#
11788 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20  endif.    }.    
11789 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20  p->nOp += nOp;. 
1178a 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72   }.  return addr
1178b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
1178c 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
1178d 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f  he P1 operand fo
1178e 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73  r a specific ins
1178f 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  truction..** Thi
11790 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
11791 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65  ful when a large
11792 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64   program is load
11793 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61  ed from a.** sta
11794 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20  tic array using 
11795 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
11796 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74  List but we want
11797 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65   to make a.** fe
11798 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20  w minor changes 
11799 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a  to the program..
1179a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1179b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1179c 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20  beChangeP1(Vdbe 
1179d 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  *p, int addr, in
1179e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74  t val){.  assert
1179f 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67  ( p==0 || p->mag
117a0 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
117a1 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26  NIT );.  if( p &
117a2 26 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e  & addr>=0 && p->
117a3 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61  nOp>addr && p->a
117a4 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70  Op ){.    p->aOp
117a5 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b  [addr].p1 = val;
117a6 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  .  }.}../*.** Ch
117a7 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
117a8 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64  f the P2 operand
117a9 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20   for a specific 
117aa 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
117ab 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
117ac 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69  useful for setti
117ad 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e  ng a jump destin
117ae 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
117af 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
117b0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
117b1 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  2(Vdbe *p, int a
117b2 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20  ddr, int val){. 
117b3 20 61 73 73 65 72 74 28 20 76 61 6c 3e 3d 30 20   assert( val>=0 
117b4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d  );.  assert( p==
117b5 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  0 || p->magic==V
117b6 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
117b7 3b 0a 20 20 69 66 28 20 70 20 26 26 20 61 64 64  ;.  if( p && add
117b8 72 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 3e 61  r>=0 && p->nOp>a
117b9 64 64 72 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b  ddr && p->aOp ){
117ba 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72  .    p->aOp[addr
117bb 5d 2e 70 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a  ].p2 = val;.  }.
117bc 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
117bd 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 6f  the P2 operand o
117be 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 64  f instruction ad
117bf 64 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f  dr so that it po
117c0 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 61  ints to.** the a
117c1 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65  ddress of the ne
117c2 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  xt instruction t
117c3 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a 2f 0a 53  o be coded..*/.S
117c4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
117c5 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  id sqlite3VdbeJu
117c6 6d 70 48 65 72 65 28 56 64 62 65 20 2a 70 2c 20  mpHere(Vdbe *p, 
117c7 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 71 6c  int addr){.  sql
117c8 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
117c9 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e 6e 4f 70  (p, addr, p->nOp
117ca 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  );.}.../*.** If 
117cb 74 68 65 20 69 6e 70 75 74 20 46 75 6e 63 44 65  the input FuncDe
117cc 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20 65  f structure is e
117cd 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 6e 20 66  phemeral, then f
117ce 72 65 65 20 69 74 2e 20 20 49 66 0a 2a 2a 20 74  ree it.  If.** t
117cf 68 65 20 46 75 6e 63 44 65 66 20 69 73 20 6e 6f  he FuncDef is no
117d0 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 68 65 6e  t ephermal, then
117d1 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2f 0a   do nothing..*/.
117d2 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
117d3 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f  EphemeralFunctio
117d4 6e 28 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29  n(FuncDef *pDef)
117d5 7b 0a 20 20 69 66 28 20 70 44 65 66 20 26 26 20  {.  if( pDef && 
117d6 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53  (pDef->flags & S
117d7 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d  QLITE_FUNC_EPHEM
117d8 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )!=0 ){.    sqli
117d9 74 65 33 5f 66 72 65 65 28 70 44 65 66 29 3b 0a  te3_free(pDef);.
117da 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c    }.}../*.** Del
117db 65 74 65 20 61 20 50 33 20 76 61 6c 75 65 20 69  ete a P3 value i
117dc 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a  f necessary..*/.
117dd 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
117de 50 33 28 69 6e 74 20 70 33 74 79 70 65 2c 20 76  P3(int p3type, v
117df 6f 69 64 20 2a 70 33 29 7b 0a 20 20 69 66 28 20  oid *p3){.  if( 
117e0 70 33 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68  p3 ){.    switch
117e1 28 20 70 33 74 79 70 65 20 29 7b 0a 20 20 20 20  ( p3type ){.    
117e2 20 20 63 61 73 65 20 50 33 5f 44 59 4e 41 4d 49    case P3_DYNAMI
117e3 43 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 33  C:.      case P3
117e4 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20  _KEYINFO:.      
117e5 63 61 73 65 20 50 33 5f 4b 45 59 49 4e 46 4f 5f  case P3_KEYINFO_
117e6 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 20 20  HANDOFF: {.     
117e7 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
117e8 70 33 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  p3);.        bre
117e9 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
117ea 20 20 63 61 73 65 20 50 33 5f 4d 50 52 49 4e 54    case P3_MPRINT
117eb 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  F: {.        sql
117ec 69 74 65 33 5f 66 72 65 65 28 70 33 29 3b 0a 20  ite3_free(p3);. 
117ed 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
117ee 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
117ef 20 50 33 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a   P3_VDBEFUNC: {.
117f0 20 20 20 20 20 20 20 20 56 64 62 65 46 75 6e 63          VdbeFunc
117f1 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56   *pVdbeFunc = (V
117f2 64 62 65 46 75 6e 63 20 2a 29 70 33 3b 0a 20 20  dbeFunc *)p3;.  
117f3 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65        freeEpheme
117f4 72 61 6c 46 75 6e 63 74 69 6f 6e 28 70 56 64 62  ralFunction(pVdb
117f5 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a 20  eFunc->pFunc);. 
117f6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
117f7 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28  beDeleteAuxData(
117f8 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a 20  pVdbeFunc, 0);. 
117f9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
117fa 72 65 65 28 70 56 64 62 65 46 75 6e 63 29 3b 0a  ree(pVdbeFunc);.
117fb 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
117fc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
117fd 65 20 50 33 5f 46 55 4e 43 44 45 46 3a 20 7b 0a  e P3_FUNCDEF: {.
117fe 20 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65          freeEphe
117ff 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 28 46  meralFunction((F
11800 75 6e 63 44 65 66 2a 29 70 33 29 3b 0a 20 20 20  uncDef*)p3);.   
11801 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
11802 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50    }.      case P
11803 33 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20  3_MEM: {.       
11804 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
11805 65 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e((sqlite3_value
11806 2a 29 70 33 29 3b 0a 20 20 20 20 20 20 20 20 62  *)p3);.        b
11807 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
11808 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a    }.  }.}.../*.*
11809 2a 20 43 68 61 6e 67 65 20 4e 20 6f 70 63 6f 64  * Change N opcod
1180a 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 61  es starting at a
1180b 64 64 72 20 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a  ddr to No-ops..*
1180c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1180d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1180e 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64  eChangeToNoop(Vd
1180f 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c  be *p, int addr,
11810 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70   int N){.  if( p
11811 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20   && p->aOp ){.  
11812 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20    VdbeOp *pOp = 
11813 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20  &p->aOp[addr];. 
11814 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b     while( N-- ){
11815 0a 20 20 20 20 20 20 66 72 65 65 50 33 28 70 4f  .      freeP3(pO
11816 70 2d 3e 70 33 74 79 70 65 2c 20 70 4f 70 2d 3e  p->p3type, pOp->
11817 70 33 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65  p3);.      memse
11818 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f 66  t(pOp, 0, sizeof
11819 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 20  (pOp[0]));.     
1181a 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f   pOp->opcode = O
1181b 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 4f  P_Noop;.      pO
1181c 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  p++;.    }.  }.}
1181d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
1181e 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1181f 50 33 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61  P3 operand for a
11820 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75   specific instru
11821 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72  ction..** This r
11822 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c  outine is useful
11823 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 72   when a large pr
11824 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 20  ogram is loaded 
11825 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 63  from a.** static
11826 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 6c   array using sql
11827 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
11828 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 6f  t but we want to
11829 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 6d   make a.** few m
1182a 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20  inor changes to 
1182b 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a  the program..**.
1182c 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e 20  ** If n>=0 then 
1182d 74 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 69  the P3 operand i
1182e 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e 69  s dynamic, meani
1182f 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 6f  ng that a copy o
11830 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  f.** the string 
11831 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 6d  is made into mem
11832 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
11833 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
11834 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f  ()..** A value o
11835 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f 70  f n==0 means cop
11836 79 20 62 79 74 65 73 20 6f 66 20 7a 50 33 20 75  y bytes of zP3 u
11837 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 69  p to and includi
11838 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  ng the.** first 
11839 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 6e  null byte.  If n
1183a 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b 31  >0 then copy n+1
1183b 20 62 79 74 65 73 20 6f 66 20 7a 50 33 2e 0a 2a   bytes of zP3..*
1183c 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 33 5f 4b 45  *.** If n==P3_KE
1183d 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20 74  YINFO it means t
1183e 68 61 74 20 7a 50 33 20 69 73 20 61 20 70 6f 69  hat zP3 is a poi
1183f 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66  nter to a KeyInf
11840 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  o structure..** 
11841 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f  A copy is made o
11842 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  f the KeyInfo st
11843 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65 6d  ructure into mem
11844 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
11845 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c  m.** sqlite3_mal
11846 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65 64  loc, to be freed
11847 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69   when the Vdbe i
11848 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20  s finalized..** 
11849 6e 3d 3d 50 33 5f 4b 45 59 49 4e 46 4f 5f 48 41  n==P3_KEYINFO_HA
1184a 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73 20  NDOFF indicates 
1184b 74 68 61 74 20 7a 50 33 20 70 6f 69 6e 74 73 20  that zP3 points 
1184c 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72  to a KeyInfo str
1184d 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65 64  ucture.** stored
1184e 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74 20   in memory that 
1184f 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f  the caller has o
11850 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
11851 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68 65  ite3_malloc. The
11852 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75   .** caller shou
11853 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65 20  ld not free the 
11854 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 77  allocation, it w
11855 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68 65  ill be freed whe
11856 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a 2a  n the Vdbe is.**
11857 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 0a   finalized..** .
11858 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20  ** Other values 
11859 6f 66 20 6e 20 28 50 33 5f 53 54 41 54 49 43 2c  of n (P3_STATIC,
1185a 20 50 33 5f 43 4f 4c 4c 53 45 51 20 65 74 63 2e   P3_COLLSEQ etc.
1185b 29 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  ) indicate that 
1185c 7a 50 33 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f  zP3 points.** to
1185d 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74 72   a string or str
1185e 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20 67  ucture that is g
1185f 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69  uaranteed to exi
11860 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74  st for the lifet
11861 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56 64  ime of.** the Vd
11862 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73  be. In these cas
11863 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20 63  es we can just c
11864 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72 2e  opy the pointer.
11865 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c 30  .**.** If addr<0
11866 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50 33 20   then change P3 
11867 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  on the most rece
11868 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e  ntly inserted in
11869 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  struction..*/.SQ
1186a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1186b 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
1186c 6e 67 65 50 33 28 56 64 62 65 20 2a 70 2c 20 69  ngeP3(Vdbe *p, i
1186d 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63  nt addr, const c
1186e 68 61 72 20 2a 7a 50 33 2c 20 69 6e 74 20 6e 29  har *zP3, int n)
1186f 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 61  {.  Op *pOp;.  a
11870 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70  ssert( p==0 || p
11871 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
11872 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66  GIC_INIT );.  if
11873 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70  ( p==0 || p->aOp
11874 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61  ==0 || p->db->ma
11875 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
11876 20 20 69 66 20 28 6e 20 21 3d 20 50 33 5f 4b 45    if (n != P3_KE
11877 59 49 4e 46 4f 29 20 7b 0a 20 20 20 20 20 20 66  YINFO) {.      f
11878 72 65 65 50 33 28 6e 2c 20 28 76 6f 69 64 2a 29  reeP3(n, (void*)
11879 2a 28 63 68 61 72 2a 2a 29 26 7a 50 33 29 3b 0a  *(char**)&zP3);.
1187a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
1187b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 64 64 72  ;.  }.  if( addr
1187c 3c 30 20 7c 7c 20 61 64 64 72 3e 3d 70 2d 3e 6e  <0 || addr>=p->n
1187d 4f 70 20 29 7b 0a 20 20 20 20 61 64 64 72 20 3d  Op ){.    addr =
1187e 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 20   p->nOp - 1;.   
1187f 20 69 66 28 20 61 64 64 72 3c 30 20 29 20 72 65   if( addr<0 ) re
11880 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 4f 70 20  turn;.  }.  pOp 
11881 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b  = &p->aOp[addr];
11882 0a 20 20 66 72 65 65 50 33 28 70 4f 70 2d 3e 70  .  freeP3(pOp->p
11883 33 74 79 70 65 2c 20 70 4f 70 2d 3e 70 33 29 3b  3type, pOp->p3);
11884 0a 20 20 70 4f 70 2d 3e 70 33 20 3d 20 30 3b 0a  .  pOp->p3 = 0;.
11885 20 20 69 66 28 20 7a 50 33 3d 3d 30 20 29 7b 0a    if( zP3==0 ){.
11886 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20 30 3b      pOp->p3 = 0;
11887 0a 20 20 20 20 70 4f 70 2d 3e 70 33 74 79 70 65  .    pOp->p3type
11888 20 3d 20 50 33 5f 4e 4f 54 55 53 45 44 3b 0a 20   = P3_NOTUSED;. 
11889 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 33   }else if( n==P3
1188a 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20  _KEYINFO ){.    
1188b 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
1188c 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 65 6c  o;.    int nFiel
1188d 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 20 6e  d, nByte;..    n
1188e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 6e 66  Field = ((KeyInf
1188f 6f 2a 29 7a 50 33 29 2d 3e 6e 46 69 65 6c 64 3b  o*)zP3)->nField;
11890 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69 7a  .    nByte = siz
11891 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 20 2b  eof(*pKeyInfo) +
11892 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 7a 65   (nField-1)*size
11893 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  of(pKeyInfo->aCo
11894 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c 64 3b  ll[0]) + nField;
11895 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20  .    pKeyInfo = 
11896 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
11897 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f 70  nByte );.    pOp
11898 2d 3e 70 33 20 3d 20 28 63 68 61 72 2a 29 70 4b  ->p3 = (char*)pK
11899 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66 28 20  eyInfo;.    if( 
1189a 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20  pKeyInfo ){.    
1189b 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1189c 2a 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  *aSortOrder;.   
1189d 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e     memcpy(pKeyIn
1189e 66 6f 2c 20 7a 50 33 2c 20 6e 42 79 74 65 29 3b  fo, zP3, nByte);
1189f 0a 20 20 20 20 20 20 61 53 6f 72 74 4f 72 64 65  .      aSortOrde
118a0 72 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53  r = pKeyInfo->aS
118a1 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20  ortOrder;.      
118a2 69 66 28 20 61 53 6f 72 74 4f 72 64 65 72 20 29  if( aSortOrder )
118a3 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e  {.        pKeyIn
118a4 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d  fo->aSortOrder =
118a5 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a   (unsigned char*
118a6 29 26 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  )&pKeyInfo->aCol
118a7 6c 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20  l[nField];.     
118a8 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e     memcpy(pKeyIn
118a9 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20  fo->aSortOrder, 
118aa 61 53 6f 72 74 4f 72 64 65 72 2c 20 6e 46 69 65  aSortOrder, nFie
118ab 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ld);.      }.   
118ac 20 20 20 70 4f 70 2d 3e 70 33 74 79 70 65 20 3d     pOp->p3type =
118ad 20 50 33 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20   P3_KEYINFO;.   
118ae 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d   }else{.      p-
118af 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
118b0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70  d = 1;.      pOp
118b1 2d 3e 70 33 74 79 70 65 20 3d 20 50 33 5f 4e 4f  ->p3type = P3_NO
118b2 54 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d  TUSED;.    }.  }
118b3 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 33 5f 4b  else if( n==P3_K
118b4 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29  EYINFO_HANDOFF )
118b5 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20  {.    pOp->p3 = 
118b6 28 63 68 61 72 2a 29 7a 50 33 3b 0a 20 20 20 20  (char*)zP3;.    
118b7 70 4f 70 2d 3e 70 33 74 79 70 65 20 3d 20 50 33  pOp->p3type = P3
118b8 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73  _KEYINFO;.  }els
118b9 65 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20  e if( n<0 ){.   
118ba 20 70 4f 70 2d 3e 70 33 20 3d 20 28 63 68 61 72   pOp->p3 = (char
118bb 2a 29 7a 50 33 3b 0a 20 20 20 20 70 4f 70 2d 3e  *)zP3;.    pOp->
118bc 70 33 74 79 70 65 20 3d 20 6e 3b 0a 20 20 7d 65  p3type = n;.  }e
118bd 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d  lse{.    if( n==
118be 30 20 29 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a  0 ) n = strlen(z
118bf 50 33 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 33  P3);.    pOp->p3
118c0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
118c1 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50 33 2c 20  Dup(p->db, zP3, 
118c2 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 33 74  n);.    pOp->p3t
118c3 79 70 65 20 3d 20 50 33 5f 44 59 4e 41 4d 49 43  ype = P3_DYNAMIC
118c4 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66  ;.  }.}..#ifndef
118c5 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65   NDEBUG./*.** Re
118c6 70 6c 61 63 65 20 74 68 65 20 50 33 20 66 69 65  place the P3 fie
118c7 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  ld of the most r
118c8 65 63 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e  ecently coded in
118c9 73 74 72 75 63 74 69 6f 6e 20 77 69 74 68 0a 2a  struction with.*
118ca 2a 20 63 6f 6d 6d 65 6e 74 20 74 65 78 74 2e 0a  * comment text..
118cb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
118cc 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
118cd 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a  beComment(Vdbe *
118ce 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
118cf 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
118d0 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 61 73  va_list ap;.  as
118d1 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c  sert( p->nOp>0 |
118d2 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20  | p->aOp==0 );. 
118d3 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d   assert( p->aOp=
118d4 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e  =0 || p->aOp[p->
118d5 6e 4f 70 2d 31 5d 2e 70 33 3d 3d 30 20 7c 7c 20  nOp-1].p3==0 || 
118d6 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
118d7 6c 65 64 20 29 3b 0a 20 20 76 61 5f 73 74 61 72  led );.  va_star
118d8 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
118d9 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
118da 6e 67 65 50 33 28 70 2c 20 2d 31 2c 20 73 71 6c  ngeP3(p, -1, sql
118db 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e  ite3VMPrintf(p->
118dc 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
118dd 2c 20 50 33 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P3_DYNAMIC);. 
118de 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 7d 0a 23   va_end(ap);.}.#
118df 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
118e0 75 72 6e 20 74 68 65 20 6f 70 63 6f 64 65 20 66  urn the opcode f
118e1 6f 72 20 61 20 67 69 76 65 6e 20 61 64 64 72 65  or a given addre
118e2 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ss..*/.SQLITE_PR
118e3 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71  IVATE VdbeOp *sq
118e4 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 56  lite3VdbeGetOp(V
118e5 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
118e6 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
118e7 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
118e8 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65  C_INIT );.  asse
118e9 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26 26 20  rt( (addr>=0 && 
118ea 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20  addr<p->nOp) || 
118eb 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
118ec 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  led );.  return 
118ed 28 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64  ((addr>=0 && add
118ee 72 3c 70 2d 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61  r<p->nOp)?(&p->a
118ef 4f 70 5b 61 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a  Op[addr]):0);.}.
118f0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
118f1 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
118f2 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e  N) || !defined(N
118f3 44 45 42 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c  DEBUG) \.     ||
118f4 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52   defined(VDBE_PR
118f5 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65  OFILE) || define
118f6 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
118f7 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20  /*.** Compute a 
118f8 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63  string that desc
118f9 72 69 62 65 73 20 74 68 65 20 50 33 20 70 61 72  ribes the P3 par
118fa 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70  ameter for an op
118fb 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65  code..** Use zTe
118fc 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75 69  mp for any requi
118fd 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62 75  red temporary bu
118fe 66 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73  ffer space..*/.s
118ff 74 61 74 69 63 20 63 68 61 72 20 2a 64 69 73 70  tatic char *disp
11900 6c 61 79 50 33 28 4f 70 20 2a 70 4f 70 2c 20 63  layP3(Op *pOp, c
11901 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20  har *zTemp, int 
11902 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a  nTemp){.  char *
11903 7a 50 33 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  zP3;.  assert( n
11904 54 65 6d 70 3e 3d 32 30 20 29 3b 0a 20 20 73 77  Temp>=20 );.  sw
11905 69 74 63 68 28 20 70 4f 70 2d 3e 70 33 74 79 70  itch( pOp->p3typ
11906 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 50 33  e ){.    case P3
11907 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 20  _KEYINFO: {.    
11908 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20    int i, j;.    
11909 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
1190a 6e 66 6f 20 3d 20 28 4b 65 79 49 6e 66 6f 2a 29  nfo = (KeyInfo*)
1190b 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 20 20 73  pOp->p3;.      s
1190c 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1190d 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b  nTemp, zTemp, "k
1190e 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79  eyinfo(%d", pKey
1190f 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20  Info->nField);. 
11910 20 20 20 20 20 69 20 3d 20 73 74 72 6c 65 6e 28       i = strlen(
11911 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f  zTemp);.      fo
11912 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66  r(j=0; j<pKeyInf
11913 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b  o->nField; j++){
11914 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
11915 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e   *pColl = pKeyIn
11916 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20  fo->aColl[j];.  
11917 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20        if( pColl 
11918 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
11919 20 6e 20 3d 20 73 74 72 6c 65 6e 28 70 43 6f 6c   n = strlen(pCol
1191a 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  l->zName);.     
1191b 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e 54 65       if( i+n>nTe
1191c 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20 20  mp-6 ){.        
1191d 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d      memcpy(&zTem
1191e 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29 3b 0a  p[i],",...",4);.
1191f 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
11920 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
11921 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 69           zTemp[i
11922 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20 20 20  ++] = ',';.     
11923 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66       if( pKeyInf
11924 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26 26  o->aSortOrder &&
11925 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
11926 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 20 20  Order[j] ){.    
11927 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 2b          zTemp[i+
11928 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20  +] = '-';.      
11929 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1192a 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d  memcpy(&zTemp[i]
1192b 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 6e  , pColl->zName,n
1192c 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  +1);.          i
1192d 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 7d   += n;.        }
1192e 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e 54 65  else if( i+4<nTe
1192f 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20 20  mp-6 ){.        
11930 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b    memcpy(&zTemp[
11931 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a 20 20  i],",nil",4);.  
11932 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 3b 0a          i += 4;.
11933 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
11934 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 2b  }.      zTemp[i+
11935 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 20 20  +] = ')';.      
11936 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a 20 20  zTemp[i] = 0;.  
11937 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 54      assert( i<nT
11938 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 7a 50 33  emp );.      zP3
11939 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20   = zTemp;.      
1193a 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1193b 20 63 61 73 65 20 50 33 5f 43 4f 4c 4c 53 45 51   case P3_COLLSEQ
1193c 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65  : {.      CollSe
1193d 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c  q *pColl = (Coll
1193e 53 65 71 2a 29 70 4f 70 2d 3e 70 33 3b 0a 20 20  Seq*)pOp->p3;.  
1193f 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
11940 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
11941 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30  p, "collseq(%.20
11942 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  s)", pColl->zNam
11943 65 29 3b 0a 20 20 20 20 20 20 7a 50 33 20 3d 20  e);.      zP3 = 
11944 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 62 72 65  zTemp;.      bre
11945 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
11946 73 65 20 50 33 5f 46 55 4e 43 44 45 46 3a 20 7b  se P3_FUNCDEF: {
11947 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a  .      FuncDef *
11948 70 44 65 66 20 3d 20 28 46 75 6e 63 44 65 66 2a  pDef = (FuncDef*
11949 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 20 20  )pOp->p3;.      
1194a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1194b 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
1194c 25 73 28 25 64 29 22 2c 20 70 44 65 66 2d 3e 7a  %s(%d)", pDef->z
1194d 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 6e 41 72 67  Name, pDef->nArg
1194e 29 3b 0a 20 20 20 20 20 20 7a 50 33 20 3d 20 7a  );.      zP3 = z
1194f 54 65 6d 70 3b 0a 20 20 20 20 20 20 62 72 65 61  Temp;.      brea
11950 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  k;.    }.#ifndef
11951 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
11952 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 61  TUALTABLE.    ca
11953 73 65 20 50 33 5f 56 54 41 42 3a 20 7b 0a 20 20  se P3_VTAB: {.  
11954 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
11955 20 2a 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74   *pVtab = (sqlit
11956 65 33 5f 76 74 61 62 2a 29 70 4f 70 2d 3e 70 33  e3_vtab*)pOp->p3
11957 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
11958 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
11959 7a 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a  zTemp, "vtab:%p:
1195a 25 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61  %p", pVtab, pVta
1195b 62 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20  b->pModule);.   
1195c 20 20 20 7a 50 33 20 3d 20 7a 54 65 6d 70 3b 0a     zP3 = zTemp;.
1195d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1195e 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 65   }.#endif.    de
1195f 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a  fault: {.      z
11960 50 33 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  P3 = pOp->p3;.  
11961 20 20 20 20 69 66 28 20 7a 50 33 3d 3d 30 20 7c      if( zP3==0 |
11962 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  | pOp->opcode==O
11963 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20  P_Noop ){.      
11964 20 20 7a 50 33 20 3d 20 22 22 3b 0a 20 20 20 20    zP3 = "";.    
11965 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
11966 61 73 73 65 72 74 28 20 7a 50 33 21 3d 30 20 29  assert( zP3!=0 )
11967 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 33 3b 0a  ;.  return zP3;.
11968 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
11969 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65 20 56  Declare to the V
1196a 64 62 65 20 74 68 61 74 20 74 68 65 20 42 54 72  dbe that the BTr
1196b 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64 62 2d  ee object at db-
1196c 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65 64 2e  >aDb[i] is used.
1196d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  .**.*/.SQLITE_PR
1196e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1196f 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
11970 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b  Vdbe *p, int i){
11971 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61  .  int mask;.  a
11972 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
11973 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  <p->db->nDb );. 
11974 20 61 73 73 65 72 74 28 20 69 3c 73 69 7a 65 6f   assert( i<sizeo
11975 66 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 29 2a  f(p->btreeMask)*
11976 38 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 31 3c  8 );.  mask = 1<
11977 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74  <i;.  if( (p->bt
11978 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d  reeMask & mask)=
11979 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72  =0 ){.    p->btr
1197a 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a  eeMask |= mask;.
1197b 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1197c 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74  MutexArrayInsert
1197d 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e  (&p->aMutex, p->
1197e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b  db->aDb[i].pBt);
1197f 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66  .  }.}...#if def
11980 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c  ined(VDBE_PROFIL
11981 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
11982 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
11983 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65  * Print a single
11984 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72   opcode.  This r
11985 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66  outine is used f
11986 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c  or debugging onl
11987 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
11988 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
11989 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c  3VdbePrintOp(FIL
1198a 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c  E *pOut, int pc,
1198b 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61   Op *pOp){.  cha
1198c 72 20 2a 7a 50 33 3b 0a 20 20 63 68 61 72 20 7a  r *zP3;.  char z
1198d 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69  Ptr[50];.  stati
1198e 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  c const char *zF
1198f 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d  ormat1 = "%4d %-
11990 31 33 73 20 25 34 64 20 25 34 64 20 25 73 5c 6e  13s %4d %4d %s\n
11991 22 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d 30  ";.  if( pOut==0
11992 20 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 74   ) pOut = stdout
11993 3b 0a 20 20 7a 50 33 20 3d 20 64 69 73 70 6c 61  ;.  zP3 = displa
11994 79 50 33 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73  yP3(pOp, zPtr, s
11995 69 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20  izeof(zPtr));.  
11996 66 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46  fprintf(pOut, zF
11997 6f 72 6d 61 74 31 2c 0a 20 20 20 20 20 20 70 63  ormat1,.      pc
11998 2c 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e  , sqlite3OpcodeN
11999 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29  ame(pOp->opcode)
1199a 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
1199b 70 32 2c 20 7a 50 33 29 3b 0a 20 20 66 66 6c 75  p2, zP3);.  fflu
1199c 73 68 28 70 4f 75 74 29 3b 0a 7d 0a 23 65 6e 64  sh(pOut);.}.#end
1199d 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  if../*.** Releas
1199e 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 4e 20  e an array of N 
1199f 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 0a 2a 2f 0a  Mem elements.*/.
119a0 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65  static void rele
119a1 61 73 65 4d 65 6d 41 72 72 61 79 28 4d 65 6d 20  aseMemArray(Mem 
119a2 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66  *p, int N){.  if
119a3 28 20 70 20 29 7b 0a 20 20 20 20 77 68 69 6c 65  ( p ){.    while
119a4 28 20 4e 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 20  ( N-->0 ){.     
119a5 20 61 73 73 65 72 74 28 20 4e 3c 32 20 7c 7c 20   assert( N<2 || 
119a6 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64 62  p[0].db==p[1].db
119a7 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
119a8 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
119a9 70 2b 2b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  p++);.    }.  }.
119aa 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
119ab 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f  E_OMIT_EXPLAIN./
119ac 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69 73 74  *.** Give a list
119ad 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f 67 72  ing of the progr
119ae 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74 75 61  am in the virtua
119af 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a  l machine..**.**
119b0 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 69   The interface i
119b1 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 73 71  s the same as sq
119b2 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e  lite3VdbeExec().
119b3 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66    But instead of
119b4 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 20  .** running the 
119b5 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73  code, it invokes
119b6 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
119b7 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e 73 74  ce for each inst
119b8 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73  ruction..** This
119b9 20 66 65 61 74 75 72 65 20 69 73 20 75 73 65 64   feature is used
119ba 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45   to implement "E
119bb 58 50 4c 41 49 4e 22 2e 0a 2a 2f 0a 53 51 4c 49  XPLAIN"..*/.SQLI
119bc 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
119bd 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 0a  qlite3VdbeList(.
119be 20 20 56 64 62 65 20 2a 70 20 20 20 20 20 20 20    Vdbe *p       
119bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
119c0 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20  he VDBE */.){.  
119c1 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
119c2 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  >db;.  int i;.  
119c3 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
119c4 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  OK;..  assert( p
119c5 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 69  ->explain );.  i
119c6 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42  f( p->magic!=VDB
119c7 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 20 72 65  E_MAGIC_RUN ) re
119c8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
119c9 53 45 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62  SE;.  assert( db
119ca 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
119cb 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20  MAGIC_BUSY );.  
119cc 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53  assert( p->rc==S
119cd 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72  QLITE_OK || p->r
119ce 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29  c==SQLITE_BUSY )
119cf 3b 0a 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f  ;..  /* Even tho
119d0 75 67 68 20 74 68 69 73 20 6f 70 63 6f 64 65 20  ugh this opcode 
119d1 64 6f 65 73 20 6e 6f 74 20 70 75 74 20 64 79 6e  does not put dyn
119d2 61 6d 69 63 20 73 74 72 69 6e 67 73 20 6f 6e 74  amic strings ont
119d3 6f 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 20 73  o the.  ** the s
119d4 74 61 63 6b 2c 20 74 68 65 79 20 6d 61 79 20 62  tack, they may b
119d5 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66  ecome dynamic if
119d6 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a   the user calls.
119d7 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c    ** sqlite3_col
119d8 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61  umn_text16(), ca
119d9 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74  using a translat
119da 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e  ion to UTF-16 en
119db 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69  coding..  */.  i
119dc 66 28 20 70 2d 3e 70 54 6f 73 3d 3d 26 70 2d 3e  f( p->pTos==&p->
119dd 61 53 74 61 63 6b 5b 34 5d 20 29 7b 0a 20 20 20  aStack[4] ){.   
119de 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79   releaseMemArray
119df 28 70 2d 3e 61 53 74 61 63 6b 2c 20 35 29 3b 0a  (p->aStack, 5);.
119e0 20 20 7d 0a 20 20 70 2d 3e 72 65 73 4f 6e 53 74    }.  p->resOnSt
119e1 61 63 6b 20 3d 20 30 3b 0a 0a 20 20 64 6f 7b 0a  ack = 0;..  do{.
119e2 20 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b      i = p->pc++;
119e3 0a 20 20 7d 77 68 69 6c 65 28 20 69 3c 70 2d 3e  .  }while( i<p->
119e4 6e 4f 70 20 26 26 20 70 2d 3e 65 78 70 6c 61 69  nOp && p->explai
119e5 6e 3d 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69  n==2 && p->aOp[i
119e6 5d 2e 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70  ].opcode!=OP_Exp
119e7 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e  lain );.  if( i>
119e8 3d 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 70  =p->nOp ){.    p
119e9 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
119ea 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
119eb 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20  E_DONE;.  }else 
119ec 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  if( db->u1.isInt
119ed 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20  errupted ){.    
119ee 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49  p->rc = SQLITE_I
119ef 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 72 63  NTERRUPT;.    rc
119f0 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
119f1 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
119f2 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
119f3 67 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  g, sqlite3ErrStr
119f4 28 70 2d 3e 72 63 29 2c 20 28 63 68 61 72 2a 29  (p->rc), (char*)
119f5 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  0);.  }else{.   
119f6 20 4f 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61   Op *pOp = &p->a
119f7 4f 70 5b 69 5d 3b 0a 20 20 20 20 4d 65 6d 20 2a  Op[i];.    Mem *
119f8 70 4d 65 6d 20 3d 20 70 2d 3e 61 53 74 61 63 6b  pMem = p->aStack
119f9 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
119fa 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
119fb 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
119fc 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20  LITE_INTEGER;.  
119fd 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 3b    pMem->u.i = i;
119fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a00 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74  /* Program count
11a01 65 72 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2b 2b  er */.    pMem++
11a02 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ;..    pMem->fla
11a03 67 73 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c  gs = MEM_Static|
11a04 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d  MEM_Str|MEM_Term
11a05 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20  ;.    pMem->z = 
11a06 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 4f 70  (char*)sqlite3Op
11a07 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70  codeName(pOp->op
11a08 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64  code);  /* Opcod
11a09 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  e */.    assert(
11a0a 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20   pMem->z!=0 );. 
11a0b 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72     pMem->n = str
11a0c 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  len(pMem->z);.  
11a0d 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
11a0e 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20  QLITE_TEXT;.    
11a0f 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
11a10 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 70 4d 65  TE_UTF8;.    pMe
11a11 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e  m++;..    pMem->
11a12 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
11a13 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  .    pMem->u.i =
11a14 20 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 20 20   pOp->p1;       
11a15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a16 20 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20     /* P1 */.    
11a17 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
11a18 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  ITE_INTEGER;.   
11a19 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d   pMem++;..    pM
11a1a 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
11a1b 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75  Int;.    pMem->u
11a1c 2e 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20  .i = pOp->p2;   
11a1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a1e 20 20 20 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a         /* P2 */.
11a1f 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
11a20 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b   SQLITE_INTEGER;
11a21 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20  .    pMem++;..  
11a22 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
11a23 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
11a24 72 7c 4d 45 4d 5f 54 65 72 6d 3b 20 20 20 2f 2a  r|MEM_Term;   /*
11a25 20 50 33 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d   P3 */.    pMem-
11a26 3e 7a 20 3d 20 64 69 73 70 6c 61 79 50 33 28 70  >z = displayP3(p
11a27 4f 70 2c 20 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74  Op, pMem->zShort
11a28 2c 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e 7a  , sizeof(pMem->z
11a29 53 68 6f 72 74 29 29 3b 0a 20 20 20 20 61 73 73  Short));.    ass
11a2a 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20  ert( pMem->z!=0 
11a2b 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  );.    pMem->n =
11a2c 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29   strlen(pMem->z)
11a2d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65  ;.    pMem->type
11a2e 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a   = SQLITE_TEXT;.
11a2f 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
11a30 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 20 20  SQLITE_UTF8;..  
11a31 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20    p->nResColumn 
11a32 3d 20 35 20 2d 20 32 2a 28 70 2d 3e 65 78 70 6c  = 5 - 2*(p->expl
11a33 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 70  ain-1);.    p->p
11a34 54 6f 73 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20  Tos = pMem;.    
11a35 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  p->rc = SQLITE_O
11a36 4b 3b 0a 20 20 20 20 70 2d 3e 72 65 73 4f 6e 53  K;.    p->resOnS
11a37 74 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 72 63  tack = 1;.    rc
11a38 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20   = SQLITE_ROW;. 
11a39 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
11a3a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
11a3b 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20  TE_OMIT_EXPLAIN 
11a3c 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
11a3d 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72  E_DEBUG./*.** Pr
11a3e 69 6e 74 20 74 68 65 20 53 51 4c 20 74 68 61 74  int the SQL that
11a3f 20 77 61 73 20 75 73 65 64 20 74 6f 20 67 65 6e   was used to gen
11a40 65 72 61 74 65 20 61 20 56 44 42 45 20 70 72 6f  erate a VDBE pro
11a41 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gram..*/.SQLITE_
11a42 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
11a43 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c  ite3VdbePrintSql
11a44 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74  (Vdbe *p){.  int
11a45 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20   nOp = p->nOp;. 
11a46 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20   VdbeOp *pOp;.  
11a47 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 74 75  if( nOp<1 ) retu
11a48 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e  rn;.  pOp = &p->
11a49 61 4f 70 5b 6e 4f 70 2d 31 5d 3b 0a 20 20 69 66  aOp[nOp-1];.  if
11a4a 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
11a4b 50 5f 4e 6f 6f 70 20 26 26 20 70 4f 70 2d 3e 70  P_Noop && pOp->p
11a4c 33 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73  3!=0 ){.    cons
11a4d 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d  t char *z = pOp-
11a4e 3e 70 33 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  >p3;.    while( 
11a4f 69 73 73 70 61 63 65 28 2a 28 75 38 2a 29 7a 29  isspace(*(u8*)z)
11a50 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 70 72 69 6e   ) z++;.    prin
11a51 74 66 28 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e 22  tf("SQL: [%s]\n"
11a52 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  , z);.  }.}.#end
11a53 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  if..#if !defined
11a54 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41  (SQLITE_OMIT_TRA
11a55 43 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53  CE) && defined(S
11a56 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54  QLITE_ENABLE_IOT
11a57 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e  RACE)./*.** Prin
11a58 74 20 61 6e 20 49 4f 54 52 41 43 45 20 6d 65 73  t an IOTRACE mes
11a59 73 61 67 65 20 73 68 6f 77 69 6e 67 20 53 51 4c  sage showing SQL
11a5a 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c   content..*/.SQL
11a5b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
11a5c 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72   sqlite3VdbeIOTr
11a5d 61 63 65 53 71 6c 28 56 64 62 65 20 2a 70 29 7b  aceSql(Vdbe *p){
11a5e 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e  .  int nOp = p->
11a5f 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70  nOp;.  VdbeOp *p
11a60 4f 70 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  Op;.  if( sqlite
11a61 33 5f 69 6f 5f 74 72 61 63 65 3d 3d 30 20 29 20  3_io_trace==0 ) 
11a62 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e 4f  return;.  if( nO
11a63 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  p<1 ) return;.  
11a64 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 6e 4f  pOp = &p->aOp[nO
11a65 70 2d 31 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d  p-1];.  if( pOp-
11a66 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 6f 6f 70  >opcode==OP_Noop
11a67 20 26 26 20 70 4f 70 2d 3e 70 33 21 3d 30 20 29   && pOp->p3!=0 )
11a68 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  {.    int i, j;.
11a69 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30 30 5d      char z[1000]
11a6a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ;.    sqlite3_sn
11a6b 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 29  printf(sizeof(z)
11a6c 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e  , z, "%s", pOp->
11a6d 70 33 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  p3);.    for(i=0
11a6e 3b 20 69 73 73 70 61 63 65 28 28 75 6e 73 69 67  ; isspace((unsig
11a6f 6e 65 64 20 63 68 61 72 29 7a 5b 69 5d 29 3b 20  ned char)z[i]); 
11a70 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 28 6a  i++){}.    for(j
11a71 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  =0; z[i]; i++){.
11a72 20 20 20 20 20 20 69 66 28 20 69 73 73 70 61 63        if( isspac
11a73 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  e((unsigned char
11a74 29 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  )z[i]) ){.      
11a75 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21 3d 27 20    if( z[i-1]!=' 
11a76 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ' ){.          z
11a77 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20  [j++] = ' ';.   
11a78 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
11a79 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b  se{.        z[j+
11a7a 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 20  +] = z[i];.     
11a7b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a   }.    }.    z[j
11a7c 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  ] = 0;.    sqlit
11a7d 65 33 5f 69 6f 5f 74 72 61 63 65 28 22 53 51 4c  e3_io_trace("SQL
11a7e 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a   %s\n", z);.  }.
11a7f 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
11a80 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 26  ITE_OMIT_TRACE &
11a81 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
11a82 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 0a 2f 2a 0a  IOTRACE */.../*.
11a83 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72  ** Prepare a vir
11a84 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72  tual machine for
11a85 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69   execution.  Thi
11a86 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67  s involves thing
11a87 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c  s such.** as all
11a88 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70  ocating stack sp
11a89 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ace and initiali
11a8a 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d  zing the program
11a8b 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74   counter..** Aft
11a8c 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20  er the VDBE has 
11a8d 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63  be prepped, it c
11a8e 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62  an be executed b
11a8f 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  y one or more.**
11a90 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
11a91 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a  3VdbeExec().  .*
11a92 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
11a93 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76   only way to mov
11a94 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44  e a VDBE from VD
11a95 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f  BE_MAGIC_INIT to
11a96 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52  .** VDBE_MAGIC_R
11a97 55 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  UN..*/.SQLITE_PR
11a98 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
11a99 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28  e3VdbeMakeReady(
11a9a 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20  .  Vdbe *p,     
11a9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a9c 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f    /* The VDBE */
11a9d 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20  .  int nVar,    
11a9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a9f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27    /* Number of '
11aa0 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 53 51  ?' see in the SQ
11aa1 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  L statement */. 
11aa2 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20   int nMem,      
11aa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11aa4 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  /* Number of mem
11aa5 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c  ory cells to all
11aa6 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  ocate */.  int n
11aa7 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20  Cursor,         
11aa8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
11aa9 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74  ber of cursors t
11aaa 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20  o allocate */.  
11aab 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 20 20 20  int isExplain   
11aac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11aad 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58  * True if the EX
11aae 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69  PLAIN keywords i
11aaf 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29 7b 0a  s present */.){.
11ab0 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
11ab1 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  e3 *db = p->db;.
11ab2 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
11ab3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
11ab4 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
11ab5 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f 2a 20  C_INIT );..  /* 
11ab6 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20  There should be 
11ab7 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 63  at least one opc
11ab8 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ode..  */.  asse
11ab9 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a  rt( p->nOp>0 );.
11aba 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d 61  .  /* Set the ma
11abb 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 47 49  gic to VDBE_MAGI
11abc 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 61 74  C_RUN sooner rat
11abd 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 2e 20  her than later. 
11abe 54 68 69 73 0a 20 20 20 2a 20 69 73 20 62 65 63  This.   * is bec
11abf 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f  ause the call to
11ac0 20 72 65 73 69 7a 65 4f 70 41 72 72 61 79 28 29   resizeOpArray()
11ac1 20 62 65 6c 6f 77 20 6d 61 79 20 73 68 72 69 6e   below may shrin
11ac2 6b 20 74 68 65 0a 20 20 20 2a 20 70 2d 3e 61 4f  k the.   * p->aO
11ac3 70 5b 5d 20 61 72 72 61 79 20 74 6f 20 73 61 76  p[] array to sav
11ac4 65 20 6d 65 6d 6f 72 79 20 69 66 20 63 61 6c 6c  e memory if call
11ac5 65 64 20 77 68 65 6e 20 69 6e 20 56 44 42 45 5f  ed when in VDBE_
11ac6 4d 41 47 49 43 5f 52 55 4e 20 0a 20 20 20 2a 20  MAGIC_RUN .   * 
11ac7 73 74 61 74 65 2e 0a 20 20 20 2a 2f 0a 20 20 70  state..   */.  p
11ac8 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d  ->magic = VDBE_M
11ac9 41 47 49 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20  AGIC_RUN;..  /* 
11aca 4e 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65  No instruction e
11acb 76 65 72 20 70 75 73 68 65 73 20 6d 6f 72 65 20  ver pushes more 
11acc 74 68 61 6e 20 61 20 73 69 6e 67 6c 65 20 65 6c  than a single el
11acd 65 6d 65 6e 74 20 6f 6e 74 6f 20 74 68 65 0a 20  ement onto the. 
11ace 20 2a 2a 20 73 74 61 63 6b 2e 20 20 41 6e 64 20   ** stack.  And 
11acf 74 68 65 20 73 74 61 63 6b 20 6e 65 76 65 72 20  the stack never 
11ad0 67 72 6f 77 73 20 6f 6e 20 73 75 63 63 65 73 73  grows on success
11ad1 69 76 65 20 65 78 65 63 75 74 69 6f 6e 73 20 6f  ive executions o
11ad2 66 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20  f the.  ** same 
11ad3 6c 6f 6f 70 2e 20 20 53 6f 20 74 68 65 20 74 6f  loop.  So the to
11ad4 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e  tal number of in
11ad5 73 74 72 75 63 74 69 6f 6e 73 20 69 73 20 61 6e  structions is an
11ad6 20 75 70 70 65 72 20 62 6f 75 6e 64 0a 20 20 2a   upper bound.  *
11ad7 2a 20 6f 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  * on the maximum
11ad8 20 73 74 61 63 6b 20 64 65 70 74 68 20 72 65 71   stack depth req
11ad9 75 69 72 65 64 2e 20 20 28 41 64 64 65 64 20 6c  uired.  (Added l
11ada 61 74 65 72 3a 29 20 20 54 68 65 0a 20 20 2a 2a  ater:)  The.  **
11adb 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73   resolveP2Values
11adc 28 29 20 63 61 6c 6c 20 63 6f 6d 70 75 74 65 73  () call computes
11add 20 61 20 74 69 67 68 74 65 72 20 75 70 70 65 72   a tighter upper
11ade 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 0a 20 20   bound on the.  
11adf 2a 2a 20 73 74 61 63 6b 20 73 69 7a 65 2e 0a 20  ** stack size.. 
11ae0 20 2a 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74   **.  ** Allocat
11ae1 69 6f 6e 20 61 6c 6c 20 74 68 65 20 73 74 61 63  ion all the stac
11ae2 6b 20 73 70 61 63 65 20 77 65 20 77 69 6c 6c 20  k space we will 
11ae3 65 76 65 72 20 6e 65 65 64 2e 0a 20 20 2a 2f 0a  ever need..  */.
11ae4 20 20 69 66 28 20 70 2d 3e 61 53 74 61 63 6b 3d    if( p->aStack=
11ae5 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41  =0 ){.    int nA
11ae6 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78  rg;       /* Max
11ae7 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61  imum number of a
11ae8 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61 20  rgs passed to a 
11ae9 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 2a  user function. *
11aea 2f 0a 20 20 20 20 69 6e 74 20 6e 53 74 61 63 6b  /.    int nStack
11aeb 3b 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d  ;     /* Maximum
11aec 20 6e 75 6d 62 65 72 20 6f 66 20 73 74 61 63 6b   number of stack
11aed 20 65 6e 74 72 69 65 73 20 72 65 71 75 69 72 65   entries require
11aee 64 20 2a 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65  d */.    resolve
11aef 50 32 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72  P2Values(p, &nAr
11af0 67 2c 20 26 6e 53 74 61 63 6b 29 3b 0a 20 20 20  g, &nStack);.   
11af1 20 72 65 73 69 7a 65 4f 70 41 72 72 61 79 28 70   resizeOpArray(p
11af2 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20 20 61  , p->nOp);.    a
11af3 73 73 65 72 74 28 20 6e 56 61 72 3e 3d 30 20 29  ssert( nVar>=0 )
11af4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 53  ;.    assert( nS
11af5 74 61 63 6b 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20  tack<p->nOp );. 
11af6 20 20 20 69 66 28 20 69 73 45 78 70 6c 61 69 6e     if( isExplain
11af7 20 29 7b 0a 20 20 20 20 20 20 6e 53 74 61 63 6b   ){.      nStack
11af8 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a 20 20 20   = 10;.    }.   
11af9 20 70 2d 3e 61 53 74 61 63 6b 20 3d 20 73 71 6c   p->aStack = sql
11afa 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
11afb 28 64 62 2c 0a 20 20 20 20 20 20 20 20 6e 53 74  (db,.        nSt
11afc 61 63 6b 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 53  ack*sizeof(p->aS
11afd 74 61 63 6b 5b 30 5d 29 20 20 20 20 2f 2a 20 61  tack[0])    /* a
11afe 53 74 61 63 6b 20 2a 2f 0a 20 20 20 20 20 20 2b  Stack */.      +
11aff 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d   nArg*sizeof(Mem
11b00 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *)              
11b01 2f 2a 20 61 70 41 72 67 20 2a 2f 0a 20 20 20 20  /* apArg */.    
11b02 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28    + nVar*sizeof(
11b03 4d 65 6d 29 20 20 20 20 20 20 20 20 20 20 20 20  Mem)            
11b04 20 20 20 2f 2a 20 61 56 61 72 20 2a 2f 0a 20 20     /* aVar */.  
11b05 20 20 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f      + nVar*sizeo
11b06 66 28 63 68 61 72 2a 29 20 20 20 20 20 20 20 20  f(char*)        
11b07 20 20 20 20 20 2f 2a 20 61 7a 56 61 72 20 2a 2f       /* azVar */
11b08 0a 20 20 20 20 20 20 2b 20 6e 4d 65 6d 2a 73 69  .      + nMem*si
11b09 7a 65 6f 66 28 4d 65 6d 29 20 20 20 20 20 20 20  zeof(Mem)       
11b0a 20 20 20 20 20 20 20 20 2f 2a 20 61 4d 65 6d 20          /* aMem 
11b0b 2a 2f 0a 20 20 20 20 20 20 2b 20 6e 43 75 72 73  */.      + nCurs
11b0c 6f 72 2a 73 69 7a 65 6f 66 28 43 75 72 73 6f 72  or*sizeof(Cursor
11b0d 2a 29 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43  *)        /* apC
11b0e 73 72 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20  sr */.    );.   
11b0f 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
11b10 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
11b11 70 2d 3e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 53  p->aMem = &p->aS
11b12 74 61 63 6b 5b 6e 53 74 61 63 6b 5d 3b 0a 20 20  tack[nStack];.  
11b13 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d      p->nMem = nM
11b14 65 6d 3b 0a 20 20 20 20 20 20 70 2d 3e 61 56 61  em;.      p->aVa
11b15 72 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 6e 4d 65  r = &p->aMem[nMe
11b16 6d 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61  m];.      p->nVa
11b17 72 20 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 20  r = nVar;.      
11b18 70 2d 3e 6f 6b 56 61 72 20 3d 20 30 3b 0a 20 20  p->okVar = 0;.  
11b19 20 20 20 20 70 2d 3e 61 70 41 72 67 20 3d 20 28      p->apArg = (
11b1a 4d 65 6d 2a 2a 29 26 70 2d 3e 61 56 61 72 5b 6e  Mem**)&p->aVar[n
11b1b 56 61 72 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 61  Var];.      p->a
11b1c 7a 56 61 72 20 3d 20 28 63 68 61 72 2a 2a 29 26  zVar = (char**)&
11b1d 70 2d 3e 61 70 41 72 67 5b 6e 41 72 67 5d 3b 0a  p->apArg[nArg];.
11b1e 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72 20 3d        p->apCsr =
11b1f 20 28 43 75 72 73 6f 72 2a 2a 29 26 70 2d 3e 61   (Cursor**)&p->a
11b20 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20 20 20 20  zVar[nVar];.    
11b21 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 6e    p->nCursor = n
11b22 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 66 6f  Cursor;.      fo
11b23 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20 6e  r(n=0; n<nVar; n
11b24 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ++){.        p->
11b25 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20  aVar[n].flags = 
11b26 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
11b27 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 20    p->aVar[n].db 
11b28 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = db;.      }.  
11b29 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e      for(n=0; n<n
11b2a 53 74 61 63 6b 3b 20 6e 2b 2b 29 7b 0a 20 20 20  Stack; n++){.   
11b2b 20 20 20 20 20 70 2d 3e 61 53 74 61 63 6b 5b 6e       p->aStack[n
11b2c 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20  ].db = db;.     
11b2d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66   }.    }.  }.  f
11b2e 6f 72 28 6e 3d 30 3b 20 6e 3c 70 2d 3e 6e 4d 65  or(n=0; n<p->nMe
11b2f 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 70 2d 3e  m; n++){.    p->
11b30 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20  aMem[n].flags = 
11b31 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d  MEM_Null;.    p-
11b32 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62  >aMem[n].db = db
11b33 3b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 70 54 6f 73  ;.  }..  p->pTos
11b34 20 3d 20 26 70 2d 3e 61 53 74 61 63 6b 5b 2d 31   = &p->aStack[-1
11b35 5d 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b  ];.  p->pc = -1;
11b36 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54  .  p->rc = SQLIT
11b37 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 75 6e 69 71 75  E_OK;.  p->uniqu
11b38 65 43 6e 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 72  eCnt = 0;.  p->r
11b39 65 74 75 72 6e 44 65 70 74 68 20 3d 20 30 3b 0a  eturnDepth = 0;.
11b3a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e    p->errorAction
11b3b 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 70   = OE_Abort;.  p
11b3c 2d 3e 70 6f 70 53 74 61 63 6b 20 3d 20 20 30 3b  ->popStack =  0;
11b3d 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d  .  p->explain |=
11b3e 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d   isExplain;.  p-
11b3f 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
11b40 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43  GIC_RUN;.  p->nC
11b41 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  hange = 0;.  p->
11b42 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20  cacheCtr = 1;.  
11b43 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  p->minWriteFileF
11b44 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70  ormat = 255;.  p
11b45 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e  ->openedStatemen
11b46 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44  t = 0;.#ifdef VD
11b47 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20  BE_PROFILE.  {. 
11b48 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
11b49 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
11b4a 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e   i++){.      p->
11b4b 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a  aOp[i].cnt = 0;.
11b4c 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e        p->aOp[i].
11b4d 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20  cycles = 0;.    
11b4e 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  }.  }.#endif.}..
11b4f 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44  /*.** Close a VD
11b50 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65  BE cursor and re
11b51 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65  lease all the re
11b52 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72  sources that cur
11b53 73 6f 72 20 68 61 70 70 65 6e 73 0a 2a 2a 20 74  sor happens.** t
11b54 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54  o hold..*/.SQLIT
11b55 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
11b56 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
11b57 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 43 75  rsor(Vdbe *p, Cu
11b58 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69 66  rsor *pCx){.  if
11b59 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20  ( pCx==0 ){.    
11b5a 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
11b5b 28 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29  ( pCx->pCursor )
11b5c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
11b5d 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43  eeCloseCursor(pC
11b5e 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  x->pCursor);.  }
11b5f 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20  .  if( pCx->pBt 
11b60 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
11b61 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42  reeClose(pCx->pB
11b62 74 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20  t);.  }.#ifndef 
11b63 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
11b64 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70  UALTABLE.  if( p
11b65 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  Cx->pVtabCursor 
11b66 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
11b67 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61  tab_cursor *pVta
11b68 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d 3e 70  bCursor = pCx->p
11b69 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20  VtabCursor;.    
11b6a 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
11b6b 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20  dule *pModule = 
11b6c 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  pCx->pModule;.  
11b6d 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
11b6e 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74  d = 1;.    sqlit
11b6f 65 33 53 61 66 65 74 79 4f 66 66 28 70 2d 3e 64  e3SafetyOff(p->d
11b70 62 29 3b 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d  b);.    pModule-
11b71 3e 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72  >xClose(pVtabCur
11b72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  sor);.    sqlite
11b73 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29  3SafetyOn(p->db)
11b74 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d  ;.    p->inVtabM
11b75 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23  ethod = 0;.  }.#
11b76 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
11b77 66 72 65 65 28 70 43 78 2d 3e 70 44 61 74 61 29  free(pCx->pData)
11b78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
11b79 28 70 43 78 2d 3e 61 54 79 70 65 29 3b 0a 20 20  (pCx->aType);.  
11b7a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 78  sqlite3_free(pCx
11b7b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  );.}../*.** Clos
11b7c 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78  e all cursors ex
11b7d 63 65 70 74 20 66 6f 72 20 56 54 61 62 20 63 75  cept for VTab cu
11b7e 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 63  rsors that are c
11b7f 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 69 6e 20 75  urrently.** in u
11b80 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  se..*/.static vo
11b81 69 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f  id closeAllCurso
11b82 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74  rsExceptActiveVt
11b83 61 62 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  abs(Vdbe *p){.  
11b84 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 2d 3e  int i;.  if( p->
11b85 61 70 43 73 72 3d 3d 30 20 29 20 72 65 74 75 72  apCsr==0 ) retur
11b86 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
11b87 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29  p->nCursor; i++)
11b88 7b 0a 20 20 20 20 43 75 72 73 6f 72 20 2a 70 43  {.    Cursor *pC
11b89 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a   = p->apCsr[i];.
11b8a 20 20 20 20 69 66 28 20 70 43 20 26 26 20 28 21      if( pC && (!
11b8b 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20  p->inVtabMethod 
11b8c 7c 7c 20 21 70 43 2d 3e 70 56 74 61 62 43 75 72  || !pC->pVtabCur
11b8d 73 6f 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71  sor) ){.      sq
11b8e 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72  lite3VdbeFreeCur
11b8f 73 6f 72 28 70 2c 20 70 43 29 3b 0a 20 20 20 20  sor(p, pC);.    
11b90 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20    p->apCsr[i] = 
11b91 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
11b92 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74  /*.** Clean up t
11b93 68 65 20 56 4d 20 61 66 74 65 72 20 65 78 65 63  he VM after exec
11b94 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ution..**.** Thi
11b95 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61  s routine will a
11b96 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f  utomatically clo
11b97 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20  se any cursors, 
11b98 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a  lists, and/or.**
11b99 20 73 6f 72 74 65 72 73 20 74 68 61 74 20 77 65   sorters that we
11b9a 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49  re left open.  I
11b9b 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74  t also deletes t
11b9c 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20  he values of.** 
11b9d 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65  variables in the
11b9e 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a   aVar[] array..*
11b9f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c  /.static void Cl
11ba0 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a  eanup(Vdbe *p){.
11ba1 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
11ba2 2d 3e 61 53 74 61 63 6b 20 29 7b 0a 20 20 20 20  ->aStack ){.    
11ba3 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28  releaseMemArray(
11ba4 70 2d 3e 61 53 74 61 63 6b 2c 20 31 20 2b 20 28  p->aStack, 1 + (
11ba5 70 2d 3e 70 54 6f 73 20 2d 20 70 2d 3e 61 53 74  p->pTos - p->aSt
11ba6 61 63 6b 29 29 3b 0a 20 20 20 20 70 2d 3e 70 54  ack));.    p->pT
11ba7 6f 73 20 3d 20 26 70 2d 3e 61 53 74 61 63 6b 5b  os = &p->aStack[
11ba8 2d 31 5d 3b 0a 20 20 7d 0a 20 20 63 6c 6f 73 65  -1];.  }.  close
11ba9 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65 70 74  AllCursorsExcept
11baa 41 63 74 69 76 65 56 74 61 62 73 28 70 29 3b 0a  ActiveVtabs(p);.
11bab 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61    releaseMemArra
11bac 79 28 70 2d 3e 61 4d 65 6d 2c 20 70 2d 3e 6e 4d  y(p->aMem, p->nM
11bad 65 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  em);.  sqlite3Vd
11bae 62 65 46 69 66 6f 43 6c 65 61 72 28 26 70 2d 3e  beFifoClear(&p->
11baf 73 46 69 66 6f 29 3b 0a 20 20 69 66 28 20 70 2d  sFifo);.  if( p-
11bb0 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 20 29 7b  >contextStack ){
11bb1 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
11bb2 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 54  p->contextStackT
11bb3 6f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  op; i++){.      
11bb4 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f 43  sqlite3VdbeFifoC
11bb5 6c 65 61 72 28 26 70 2d 3e 63 6f 6e 74 65 78 74  lear(&p->context
11bb6 53 74 61 63 6b 5b 69 5d 2e 73 46 69 66 6f 29 3b  Stack[i].sFifo);
11bb7 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
11bb8 65 33 5f 66 72 65 65 28 70 2d 3e 63 6f 6e 74 65  e3_free(p->conte
11bb9 78 74 53 74 61 63 6b 29 3b 0a 20 20 7d 0a 20 20  xtStack);.  }.  
11bba 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 20  p->contextStack 
11bbb 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74 65 78  = 0;.  p->contex
11bbc 74 53 74 61 63 6b 44 65 70 74 68 20 3d 20 30 3b  tStackDepth = 0;
11bbd 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61  .  p->contextSta
11bbe 63 6b 54 6f 70 20 3d 20 30 3b 0a 20 20 73 71 6c  ckTop = 0;.  sql
11bbf 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 45 72  ite3_free(p->zEr
11bc0 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72  rMsg);.  p->zErr
11bc1 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 72 65  Msg = 0;.  p->re
11bc2 73 4f 6e 53 74 61 63 6b 20 3d 20 30 3b 0a 7d 0a  sOnStack = 0;.}.
11bc3 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e  ./*.** Set the n
11bc4 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
11bc5 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c  columns that wil
11bc6 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  l be returned by
11bc7 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61   this SQL.** sta
11bc8 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20  tement. This is 
11bc9 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69  now set at compi
11bca 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20  le time, rather 
11bcb 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65  than during.** e
11bcc 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20  xecution of the 
11bcd 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20  vdbe program so 
11bce 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c  that sqlite3_col
11bcf 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a  umn_count() can.
11bd0 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20  ** be called on 
11bd1 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
11bd2 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f   before sqlite3_
11bd3 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  step()..*/.SQLIT
11bd4 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
11bd5 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
11bd6 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e  Cols(Vdbe *p, in
11bd7 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20  t nResColumn){. 
11bd8 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a   Mem *pColName;.
11bd9 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 72 65 6c 65    int n;..  rele
11bda 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61  aseMemArray(p->a
11bdb 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73  ColName, p->nRes
11bdc 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e  Column*COLNAME_N
11bdd 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
11bde 65 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a  e(p->aColName);.
11bdf 20 20 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e    n = nResColumn
11be0 2a 43 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d  *COLNAME_N;.  p-
11be1 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 6e 52  >nResColumn = nR
11be2 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61  esColumn;.  p->a
11be3 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61  ColName = pColNa
11be4 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74  me = (Mem*)sqlit
11be5 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70  e3DbMallocZero(p
11be6 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d  ->db, sizeof(Mem
11be7 29 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  )*n );.  if( p->
11be8 61 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65  aColName==0 ) re
11be9 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e  turn;.  while( n
11bea 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43  -- > 0 ){.    pC
11beb 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20  olName->flags = 
11bec 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43  MEM_Null;.    pC
11bed 6f 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e  olName->db = p->
11bee 64 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65  db;.    pColName
11bef 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ++;.  }.}../*.**
11bf0 20 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66   Set the name of
11bf1 20 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75   the idx'th colu
11bf2 6d 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  mn to be returne
11bf3 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61  d by the SQL sta
11bf4 74 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65  tement..** zName
11bf5 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74   must be a point
11bf6 65 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d  er to a nul term
11bf7 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a  inated string..*
11bf8 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d  *.** This call m
11bf9 75 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65  ust be made afte
11bfa 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  r a call to sqli
11bfb 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
11bfc 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 4e 3d  s()..**.** If N=
11bfd 3d 50 33 5f 53 54 41 54 49 43 20 20 69 74 20 6d  =P3_STATIC  it m
11bfe 65 61 6e 73 20 74 68 61 74 20 7a 4e 61 6d 65 20  eans that zName 
11bff 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
11c00 61 20 63 6f 6e 73 74 61 6e 74 20 73 74 61 74 69  a constant stati
11c01 63 0a 2a 2a 20 73 74 72 69 6e 67 20 61 6e 64 20  c.** string and 
11c02 77 65 20 63 61 6e 20 6a 75 73 74 20 63 6f 70 79  we can just copy
11c03 20 74 68 65 20 70 6f 69 6e 74 65 72 2e 20 49 66   the pointer. If
11c04 20 69 74 20 69 73 20 50 33 5f 44 59 4e 41 4d 49   it is P3_DYNAMI
11c05 43 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 68 65 20  C, then .** the 
11c06 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 64 20  string is freed 
11c07 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72  using sqlite3_fr
11c08 65 65 28 29 20 77 68 65 6e 20 74 68 65 20 76 64  ee() when the vd
11c09 62 65 20 69 73 20 66 69 6e 69 73 68 65 64 20 77  be is finished w
11c0a 69 74 68 0a 2a 2a 20 69 74 2e 20 4f 74 68 65 72  ith.** it. Other
11c0b 77 69 73 65 2c 20 4e 20 62 79 74 65 73 20 6f 66  wise, N bytes of
11c0c 20 7a 4e 61 6d 65 20 61 72 65 20 63 6f 70 69 65   zName are copie
11c0d 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
11c0e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
11c0f 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56  VdbeSetColName(V
11c10 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c  dbe *p, int idx,
11c11 20 69 6e 74 20 76 61 72 2c 20 63 6f 6e 73 74 20   int var, const 
11c12 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74  char *zName, int
11c13 20 4e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20   N){.  int rc;. 
11c14 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a   Mem *pColName;.
11c15 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 2d    assert( idx<p-
11c16 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20  >nResColumn );. 
11c17 20 61 73 73 65 72 74 28 20 76 61 72 3c 43 4f 4c   assert( var<COL
11c18 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20 69 66 28 20  NAME_N );.  if( 
11c19 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
11c1a 6c 65 64 20 29 20 72 65 74 75 72 6e 20 53 51 4c  led ) return SQL
11c1b 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 61 73 73  ITE_NOMEM;.  ass
11c1c 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65  ert( p->aColName
11c1d 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 6d  !=0 );.  pColNam
11c1e 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d  e = &(p->aColNam
11c1f 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65  e[idx+var*p->nRe
11c20 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 69 66 28  sColumn]);.  if(
11c21 20 4e 3d 3d 50 33 5f 44 59 4e 41 4d 49 43 20 7c   N==P3_DYNAMIC |
11c22 7c 20 4e 3d 3d 50 33 5f 53 54 41 54 49 43 20 29  | N==P3_STATIC )
11c23 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
11c24 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
11c25 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c  pColName, zName,
11c26 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
11c27 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
11c28 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
11c29 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
11c2a 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d  emSetStr(pColNam
11c2b 65 2c 20 7a 4e 61 6d 65 2c 20 4e 2c 20 53 51 4c  e, zName, N, SQL
11c2c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f  ITE_UTF8,SQLITE_
11c2d 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a  TRANSIENT);.  }.
11c2e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
11c2f 5f 4f 4b 20 26 26 20 4e 3d 3d 50 33 5f 44 59 4e  _OK && N==P3_DYN
11c30 41 4d 49 43 20 29 7b 0a 20 20 20 20 70 43 6f 6c  AMIC ){.    pCol
11c31 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 28 70  Name->flags = (p
11c32 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 28  ColName->flags&(
11c33 7e 4d 45 4d 5f 53 74 61 74 69 63 29 29 7c 4d 45  ~MEM_Static))|ME
11c34 4d 5f 44 79 6e 3b 0a 20 20 20 20 70 43 6f 6c 4e  M_Dyn;.    pColN
11c35 61 6d 65 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20  ame->xDel = 0;. 
11c36 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
11c37 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20  }../*.** A read 
11c38 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  or write transac
11c39 74 69 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20  tion may or may 
11c3a 6e 6f 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e  not be active on
11c3b 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
11c3c 0a 2a 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61  .** db. If a tra
11c3d 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
11c3e 76 65 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49  ve, commit it. I
11c3f 66 20 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20  f there is a.** 
11c40 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
11c41 6e 20 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20  n spanning more 
11c42 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  than one databas
11c43 65 20 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75  e file, this rou
11c44 74 69 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61  tine.** takes ca
11c45 72 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  re of the master
11c46 20 6a 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72   journal tricker
11c47 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
11c48 20 76 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69   vdbeCommit(sqli
11c49 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
11c4a 69 3b 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 20  i;.  int nTrans 
11c4b 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20  = 0;  /* Number 
11c4c 6f 66 20 64 61 74 61 62 61 73 65 73 20 77 69 74  of databases wit
11c4d 68 20 61 6e 20 61 63 74 69 76 65 20 77 72 69 74  h an active writ
11c4e 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  e-transaction */
11c4f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
11c50 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65 65  TE_OK;.  int nee
11c51 64 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a 20  dXcommit = 0;.. 
11c52 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67   /* Before doing
11c53 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20   anything else, 
11c54 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29  call the xSync()
11c55 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e   callback for an
11c56 79 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d  y.  ** virtual m
11c57 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69  odule tables wri
11c58 74 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61  tten in this tra
11c59 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68  nsaction. This h
11c5a 61 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f  as to.  ** be do
11c5b 6e 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d  ne before determ
11c5c 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 20  ining whether a 
11c5d 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
11c5e 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71  ile is .  ** req
11c5f 75 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79  uired, as an xSy
11c60 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61  nc() callback ma
11c61 79 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 65  y add an attache
11c62 64 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  d database.  ** 
11c63 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  to the transacti
11c64 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  on..  */.  rc = 
11c65 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28  sqlite3VtabSync(
11c66 64 62 2c 20 72 63 29 3b 0a 20 20 69 66 28 20 72  db, rc);.  if( r
11c67 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
11c68 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
11c69 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f   }..  /* This lo
11c6a 6f 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61  op determines (a
11c6b 29 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20  ) if the commit 
11c6c 68 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69  hook should be i
11c6d 6e 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20  nvoked and.  ** 
11c6e 28 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74  (b) how many dat
11c6f 61 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65  abase files have
11c70 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e   open write tran
11c71 73 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20  sactions, not . 
11c72 20 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68   ** including th
11c73 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e  e temp database.
11c74 20 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e   (b) is importan
11c75 74 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72  t because if mor
11c76 65 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65  e than .  ** one
11c77 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
11c78 61 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65  as an open write
11c79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20   transaction, a 
11c7a 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20  master journal. 
11c7b 20 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75   ** file is requ
11c7c 69 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d  ired for an atom
11c7d 69 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20  ic commit..  */ 
11c7e 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
11c7f 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
11c80 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
11c81 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
11c82 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
11c83 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74  reeIsInTrans(pBt
11c84 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58  ) ){.      needX
11c85 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  commit = 1;.    
11c86 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72    if( i!=1 ) nTr
11c87 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ans++;.    }.  }
11c88 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
11c89 61 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74 72  are any write-tr
11c8a 61 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61 6c  ansactions at al
11c8b 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f  l, invoke the co
11c8c 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69  mmit hook */.  i
11c8d 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 26  f( needXcommit &
11c8e 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c  & db->xCommitCal
11c8f 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c  lback ){.    sql
11c90 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
11c91 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e  );.    rc = db->
11c92 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28  xCommitCallback(
11c93 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b  db->pCommitArg);
11c94 0a 20 20 20 20 73 71 6c 69 74 65 33 53 61 66 65  .    sqlite3Safe
11c95 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69 66  tyOn(db);.    if
11c96 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
11c97 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53  turn SQLITE_CONS
11c98 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20  TRAINT;.    }.  
11c99 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d 70  }..  /* The simp
11c9a 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f 72  le case - no mor
11c9b 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62  e than one datab
11c9c 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63 6f  ase file (not co
11c9d 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20  unting the.  ** 
11c9e 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 20 68  TEMP database) h
11c9f 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  as a transaction
11ca0 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 72 65   active.   There
11ca1 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20   is no need for 
11ca2 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72 2d  the.  ** master-
11ca3 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20  journal..  **.  
11ca4 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 72 6e  ** If the return
11ca5 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65   value of sqlite
11ca6 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d  3BtreeGetFilenam
11ca7 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c 65  e() is a zero le
11ca8 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e 67  ngth.  ** string
11ca9 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 6d  , it means the m
11caa 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 20  ain database is 
11cab 3a 6d 65 6d 6f 72 79 3a 2e 20 20 49 6e 20 74 68  :memory:.  In th
11cac 61 74 20 63 61 73 65 20 77 65 20 64 6f 0a 20 20  at case we do.  
11cad 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61  ** not support a
11cae 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65  tomic multi-file
11caf 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65   commits, so use
11cb0 20 74 68 65 20 73 69 6d 70 6c 65 20 63 61 73 65   the simple case
11cb1 20 74 68 65 6e 0a 20 20 2a 2a 20 74 6f 6f 2e 0a   then.  ** too..
11cb2 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 74    */.  if( 0==st
11cb3 72 6c 65 6e 28 73 71 6c 69 74 65 33 42 74 72 65  rlen(sqlite3Btre
11cb4 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d  eGetFilename(db-
11cb5 3e 61 44 62 5b 30 5d 2e 70 42 74 29 29 20 7c 7c  >aDb[0].pBt)) ||
11cb6 20 6e 54 72 61 6e 73 3c 3d 31 20 29 7b 0a 20 20   nTrans<=1 ){.  
11cb7 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
11cb8 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
11cb9 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
11cba 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
11cbb 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
11cbc 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29  .      if( pBt )
11cbd 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
11cbe 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
11cbf 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30  tPhaseOne(pBt, 0
11cc0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
11cc1 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20  ..    /* Do the 
11cc2 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61  commit only if a
11cc3 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63  ll databases suc
11cc4 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65  cessfully comple
11cc5 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20  te phase 1. .   
11cc6 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68   ** If one of th
11cc7 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e BtreeCommitPha
11cc8 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61  seOne() calls fa
11cc9 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61  ils, this indica
11cca 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f  tes an.    ** IO
11ccb 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c   error while del
11ccc 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74  eting or truncat
11ccd 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  ing a journal fi
11cce 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65  le. It is unlike
11ccf 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63  ly,.    ** but c
11cd0 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20  ould happen. In 
11cd1 74 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f  this case abando
11cd2 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64  n processing and
11cd3 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
11cd4 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  r..    */.    fo
11cd5 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
11cd6 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44  E_OK && i<db->nD
11cd7 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42  b; i++){.      B
11cd8 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
11cd9 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
11cda 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
11cdb 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
11cdc 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
11cdd 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20  eTwo(pBt);.     
11cde 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
11cdf 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
11ce0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
11ce1 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20  tabCommit(db);. 
11ce2 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
11ce3 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65 20  he complex case 
11ce4 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d 75 6c  - There is a mul
11ce5 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74 72  ti-file write-tr
11ce6 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 65  ansaction active
11ce7 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71 75  ..  ** This requ
11ce8 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a 6f  ires a master jo
11ce9 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e  urnal file to en
11cea 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 61 63  sure the transac
11ceb 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d  tion is.  ** com
11cec 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e  mitted atomicly.
11ced 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
11cee 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
11cef 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c  .  else{.    sql
11cf0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
11cf1 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69   db->pVfs;.    i
11cf2 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  nt needSync = 0;
11cf3 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  .    char *zMast
11cf4 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c  er = 0;   /* Fil
11cf5 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 6d  e-name for the m
11cf6 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f  aster journal */
11cf7 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20  .    char const 
11cf8 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c  *zMainFile = sql
11cf9 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65  ite3BtreeGetFile
11cfa 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e  name(db->aDb[0].
11cfb 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pBt);.    sqlite
11cfc 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 20  3_file *pMaster 
11cfd 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66 66  = 0;.    i64 off
11cfe 73 65 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  set = 0;..    /*
11cff 20 53 65 6c 65 63 74 20 61 20 6d 61 73 74 65 72   Select a master
11d00 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
11d01 6d 65 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20  me */.    do {. 
11d02 20 20 20 20 20 75 33 32 20 72 61 6e 64 6f 6d 3b       u32 random;
11d03 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
11d04 72 65 65 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20  ree(zMaster);.  
11d05 20 20 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f      sqlite3Rando
11d06 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 61 6e  mness(sizeof(ran
11d07 64 6f 6d 29 2c 20 26 72 61 6e 64 6f 6d 29 3b 0a  dom), &random);.
11d08 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20        zMaster = 
11d09 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
11d0a 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20  b, "%s-mj%08X", 
11d0b 7a 4d 61 69 6e 46 69 6c 65 2c 20 72 61 6e 64 6f  zMainFile, rando
11d0c 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20  m&0x7fffffff);. 
11d0d 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65       if( !zMaste
11d0e 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  r ){.        ret
11d0f 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
11d10 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77  ;.      }.    }w
11d11 68 69 6c 65 28 20 73 71 6c 69 74 65 33 4f 73 41  hile( sqlite3OsA
11d12 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73  ccess(pVfs, zMas
11d13 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  ter, SQLITE_ACCE
11d14 53 53 5f 45 58 49 53 54 53 29 20 29 3b 0a 0a 20  SS_EXISTS) );.. 
11d15 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6d     /* Open the m
11d16 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 2a  aster journal. *
11d17 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
11d18 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 70  e3OsOpenMalloc(p
11d19 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 26 70  Vfs, zMaster, &p
11d1a 4d 61 73 74 65 72 2c 20 0a 20 20 20 20 20 20 20  Master, .       
11d1b 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
11d1c 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
11d1d 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20  EN_CREATE|.     
11d1e 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
11d1f 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f  XCLUSIVE|SQLITE_
11d20 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
11d21 4e 41 4c 2c 20 30 0a 20 20 20 20 29 3b 0a 20 20  NAL, 0.    );.  
11d22 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
11d23 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
11d24 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65  ite3_free(zMaste
11d25 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  r);.      return
11d26 20 72 63 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20   rc;.    }. .   
11d27 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 61   /* Write the na
11d28 6d 65 20 6f 66 20 65 61 63 68 20 64 61 74 61 62  me of each datab
11d29 61 73 65 20 66 69 6c 65 20 69 6e 20 74 68 65 20  ase file in the 
11d2a 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 6f  transaction into
11d2b 20 74 68 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20   the new.    ** 
11d2c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
11d2d 69 6c 65 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ile. If an error
11d2e 20 6f 63 63 75 72 73 20 61 74 20 74 68 69 73 20   occurs at this 
11d2f 70 6f 69 6e 74 20 63 6c 6f 73 65 0a 20 20 20 20  point close.    
11d30 2a 2a 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68  ** and delete th
11d31 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11d32 20 66 69 6c 65 2e 20 41 6c 6c 20 74 68 65 20 69   file. All the i
11d33 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61  ndividual journa
11d34 6c 20 66 69 6c 65 73 0a 20 20 20 20 2a 2a 20 73  l files.    ** s
11d35 74 69 6c 6c 20 68 61 76 65 20 27 6e 75 6c 6c 27  till have 'null'
11d36 20 61 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a   as the master j
11d37 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 2c 20  ournal pointer, 
11d38 73 6f 20 74 68 65 79 20 77 69 6c 6c 20 72 6f 6c  so they will rol
11d39 6c 0a 20 20 20 20 2a 2a 20 62 61 63 6b 20 69 6e  l.    ** back in
11d3a 64 65 70 65 6e 64 65 6e 74 6c 79 20 69 66 20 61  dependently if a
11d3b 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e   failure occurs.
11d3c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
11d3d 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
11d3e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65  i++){.      Btre
11d3f 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
11d40 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69  [i].pBt;.      i
11d41 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e  f( i==1 ) contin
11d42 75 65 3b 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20  ue;   /* Ignore 
11d43 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73  the TEMP databas
11d44 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73  e */.      if( s
11d45 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
11d46 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20  rans(pBt) ){.   
11d47 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20       char const 
11d48 2a 7a 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33  *zFile = sqlite3
11d49 42 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e  BtreeGetJournaln
11d4a 61 6d 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20  ame(pBt);.      
11d4b 20 20 69 66 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d    if( zFile[0]==
11d4c 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f  0 ) continue;  /
11d4d 2a 20 49 67 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79  * Ignore :memory
11d4e 3a 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20  : databases */. 
11d4f 20 20 20 20 20 20 20 69 66 28 20 21 6e 65 65 64         if( !need
11d50 53 79 6e 63 20 26 26 20 21 73 71 6c 69 74 65 33  Sync && !sqlite3
11d51 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65  BtreeSyncDisable
11d52 64 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  d(pBt) ){.      
11d53 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31      needSync = 1
11d54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
11d55 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
11d56 4f 73 57 72 69 74 65 28 70 4d 61 73 74 65 72 2c  OsWrite(pMaster,
11d57 20 7a 46 69 6c 65 2c 20 73 74 72 6c 65 6e 28 7a   zFile, strlen(z
11d58 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29  File)+1, offset)
11d59 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74  ;.        offset
11d5a 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 46 69 6c 65   += strlen(zFile
11d5b 29 2b 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28  )+1;.        if(
11d5c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11d5d 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
11d5e 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70  te3OsCloseFree(p
11d5f 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20  Master);.       
11d60 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65     sqlite3OsDele
11d61 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  te(pVfs, zMaster
11d62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
11d63 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61  sqlite3_free(zMa
11d64 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  ster);.         
11d65 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
11d66 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
11d67 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63    }..    /* Sync
11d68 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11d69 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65  nal file. If the
11d6a 20 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41   IOCAP_SEQUENTIA
11d6b 4c 20 64 65 76 69 63 65 0a 20 20 20 20 2a 2a 20  L device.    ** 
11d6c 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 69 73  flag is set this
11d6d 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
11d6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61  ..    */.    zMa
11d6f 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33  inFile = sqlite3
11d70 42 74 72 65 65 47 65 74 44 69 72 6e 61 6d 65 28  BtreeGetDirname(
11d71 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b  db->aDb[0].pBt);
11d72 0a 20 20 20 20 69 66 28 20 28 6e 65 65 64 53 79  .    if( (needSy
11d73 6e 63 20 0a 20 20 20 20 20 26 26 20 28 30 3d 3d  nc .     && (0==
11d74 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65  (sqlite3OsDevice
11d75 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
11d76 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54 45 5f  pMaster)&SQLITE_
11d77 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c  IOCAP_SEQUENTIAL
11d78 29 29 0a 20 20 20 20 20 26 26 20 28 72 63 3d 73  )).     && (rc=s
11d79 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61  qlite3OsSync(pMa
11d7a 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e  ster, SQLITE_SYN
11d7b 43 5f 4e 4f 52 4d 41 4c 29 29 21 3d 53 51 4c 49  C_NORMAL))!=SQLI
11d7c 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20  TE_OK) ){.      
11d7d 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72  sqlite3OsCloseFr
11d7e 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20  ee(pMaster);.   
11d7f 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65     sqlite3OsDele
11d80 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  te(pVfs, zMaster
11d81 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
11d82 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72  te3_free(zMaster
11d83 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
11d84 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rc;.    }..    /
11d85 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 65 20 64  * Sync all the d
11d86 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64  b files involved
11d87 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74   in the transact
11d88 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 20 63 61  ion. The same ca
11d89 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 74  ll.    ** sets t
11d8a 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
11d8b 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 65 61 63  l pointer in eac
11d8c 68 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75  h individual jou
11d8d 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20  rnal. If.    ** 
11d8e 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
11d8f 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c  here, do not del
11d90 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ete the master j
11d91 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20  ournal file..   
11d92 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68   **.    ** If th
11d93 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  e error occurs d
11d94 75 72 69 6e 67 20 74 68 65 20 66 69 72 73 74 20  uring the first 
11d95 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73  call to.    ** s
11d96 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
11d97 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 74 68 65  tPhaseOne(), the
11d98 6e 20 74 68 65 72 65 20 69 73 20 61 20 63 68 61  n there is a cha
11d99 6e 63 65 20 74 68 61 74 20 74 68 65 0a 20 20 20  nce that the.   
11d9a 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   ** master journ
11d9b 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20  al file will be 
11d9c 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 20 77 65  orphaned. But we
11d9d 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69   cannot delete i
11d9e 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61 73  t,.    ** in cas
11d9f 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
11da0 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77  rnal file name w
11da1 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  as written into 
11da2 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  the journal.    
11da3 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74  ** file before t
11da4 68 65 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  he failure occur
11da5 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  ed..    */.    f
11da6 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
11da7 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e  TE_OK && i<db->n
11da8 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20  Db; i++){ .     
11da9 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
11daa 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
11dab 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
11dac 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
11dad 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
11dae 61 73 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73  aseOne(pBt, zMas
11daf 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ter);.      }.  
11db0 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f    }.    sqlite3O
11db1 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74  sCloseFree(pMast
11db2 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  er);.    if( rc!
11db3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11db4 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
11db5 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  (zMaster);.     
11db6 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
11db7 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  }..    /* Delete
11db8 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11db9 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 63  nal file. This c
11dba 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61 6e 73  ommits the trans
11dbb 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a 20 20  action. After.  
11dbc 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73 20    ** doing this 
11dbd 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 69 73  the directory is
11dbe 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20 62 65   synced again be
11dbf 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 69 64  fore any individ
11dc0 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  ual.    ** trans
11dc1 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 72 65  action files are
11dc2 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f   deleted..    */
11dc3 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11dc4 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20  3OsDelete(pVfs, 
11dc5 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20 20 20  zMaster, 1);.   
11dc6 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d   sqlite3_free(zM
11dc7 61 73 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73  aster);.    zMas
11dc8 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  ter = 0;.    if(
11dc9 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74   rc ){.      ret
11dca 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20  urn rc;.    }.. 
11dcb 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20     /* All files 
11dcc 61 6e 64 20 64 69 72 65 63 74 6f 72 69 65 73 20  and directories 
11dcd 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
11dce 6e 20 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 65  n synced, so the
11dcf 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a   following.    *
11dd0 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
11dd1 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
11dd2 73 65 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79  seTwo() are only
11dd3 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 61   closing files a
11dd4 6e 64 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69  nd.    ** deleti
11dd5 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67  ng or truncating
11dd6 20 6a 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f   journals. If so
11dd7 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
11dd8 6e 67 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20  ng while.    ** 
11dd9 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e  this is happenin
11dda 67 20 77 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c  g we don't reall
11ddb 79 20 63 61 72 65 2e 20 54 68 65 20 69 6e 74 65  y care. The inte
11ddc 67 72 69 74 79 20 6f 66 20 74 68 65 0a 20 20 20  grity of the.   
11ddd 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
11dde 69 73 20 61 6c 72 65 61 64 79 20 67 75 61 72 61  is already guara
11ddf 6e 74 65 65 64 2c 20 62 75 74 20 73 6f 6d 65 20  nteed, but some 
11de0 73 74 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75  stray 'cold' jou
11de1 72 6e 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79  rnals.    ** may
11de2 20 62 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64   be lying around
11de3 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 65  . Returning an e
11de4 72 72 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74 20  rror code won't 
11de5 68 65 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20 20  help matters..  
11de6 20 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65    */.    disable
11de7 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
11de8 72 6f 72 73 28 29 3b 0a 20 20 20 20 66 6f 72 28  rors();.    for(
11de9 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
11dea 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72  i++){ .      Btr
11deb 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
11dec 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20  b[i].pBt;.      
11ded 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20  if( pBt ){.     
11dee 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
11def 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42  ommitPhaseTwo(pB
11df0 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
11df1 7d 0a 20 20 20 20 65 6e 61 62 6c 65 5f 73 69 6d  }.    enable_sim
11df2 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
11df3 28 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33  ();..    sqlite3
11df4 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a  VtabCommit(db);.
11df5 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65    }.#endif..  re
11df6 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a  turn rc;.}../* .
11df7 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
11df8 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 20  checks that the 
11df9 73 71 6c 69 74 65 33 2e 61 63 74 69 76 65 56 64  sqlite3.activeVd
11dfa 62 65 43 6e 74 20 63 6f 75 6e 74 20 76 61 72 69  beCnt count vari
11dfb 61 62 6c 65 0a 2a 2a 20 6d 61 74 63 68 65 73 20  able.** matches 
11dfc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 64  the number of vd
11dfd 62 65 27 73 20 69 6e 20 74 68 65 20 6c 69 73 74  be's in the list
11dfe 20 73 71 6c 69 74 65 33 2e 70 56 64 62 65 20 74   sqlite3.pVdbe t
11dff 68 61 74 20 61 72 65 0a 2a 2a 20 63 75 72 72 65  hat are.** curre
11e00 6e 74 6c 79 20 61 63 74 69 76 65 2e 20 41 6e 20  ntly active. An 
11e01 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20  assertion fails 
11e02 69 66 20 74 68 65 20 74 77 6f 20 63 6f 75 6e 74  if the two count
11e03 73 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 2e 0a  s do not match..
11e04 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e  ** This is an in
11e05 74 65 72 6e 61 6c 20 73 65 6c 66 2d 63 68 65 63  ternal self-chec
11e06 6b 20 6f 6e 6c 79 20 2d 20 69 74 20 69 73 20 6e  k only - it is n
11e07 6f 74 20 61 6e 20 65 73 73 65 6e 74 69 61 6c 20  ot an essential 
11e08 70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 73 74  processing.** st
11e09 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ep..**.** This i
11e0a 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 4e 44 45  s a no-op if NDE
11e0b 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a  BUG is defined..
11e0c 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
11e0d 47 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68  G.static void ch
11e0e 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74  eckActiveVdbeCnt
11e0f 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
11e10 20 56 64 62 65 20 2a 70 3b 0a 20 20 69 6e 74 20   Vdbe *p;.  int 
11e11 63 6e 74 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64  cnt = 0;.  p = d
11e12 62 2d 3e 70 56 64 62 65 3b 0a 20 20 77 68 69 6c  b->pVdbe;.  whil
11e13 65 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20  e( p ){.    if( 
11e14 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
11e15 41 47 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70  AGIC_RUN && p->p
11e16 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e  c>=0 ){.      cn
11e17 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  t++;.    }.    p
11e18 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d   = p->pNext;.  }
11e19 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 3d 3d  .  assert( cnt==
11e1a 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
11e1b 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65  t );.}.#else.#de
11e1c 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 76 65  fine checkActive
11e1d 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e 64 69  VdbeCnt(x).#endi
11e1e 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 76 65  f../*.** For eve
11e1f 72 79 20 42 74 72 65 65 20 74 68 61 74 20 69 6e  ry Btree that in
11e20 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
11e21 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20 0a 2a  tion db which .*
11e22 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66  * has been modif
11e23 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72 20 69  ied, "trip" or i
11e24 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68 20 63  nvalidate each c
11e25 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68 61 74  ursor in.** that
11e26 20 42 74 72 65 65 20 6d 69 67 68 74 20 68 61 76   Btree might hav
11e27 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  e been modified 
11e28 73 6f 20 74 68 61 74 20 74 68 65 20 63 75 72 73  so that the curs
11e29 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65 72 20  or.** can never 
11e2a 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e 20 20  be used again.  
11e2b 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65  This happens whe
11e2c 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 2a  n a rollback.***
11e2d 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68 61 76   occurs.  We hav
11e2e 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20 74 68  e to trip all th
11e2f 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 2c  e other cursors,
11e30 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 20   even.** cursor 
11e31 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73 20 69  from other VMs i
11e32 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61  n different data
11e33 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
11e34 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f 6e  ,.** so that non
11e35 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20 74 6f  e of them try to
11e36 20 75 73 65 20 74 68 65 20 64 61 74 61 20 61 74   use the data at
11e37 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20 77   which they.** w
11e38 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 6e 64  ere pointing and
11e39 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79 20 68   which now may h
11e3a 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67 65 64  ave been changed
11e3b 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65 20 72   due.** to the r
11e3c 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52  ollback..**.** R
11e3d 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61 20 72  emember that a r
11e3e 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65 6c 65  ollback can dele
11e3f 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70 6c 65  te tables comple
11e40 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72 64 65  te and.** reorde
11e41 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20 53 6f  r rootpages.  So
11e42 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 69   it is not suffi
11e43 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20 73 61  cient just to sa
11e44 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20  ve.** the state 
11e45 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20  of the cursor.  
11e46 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76 61 6c  We have to inval
11e47 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72  idate the cursor
11e48 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20 69  .** so that it i
11e49 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 67 61  s never used aga
11e4a 69 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 69 6e 76 61  in..*/.void inva
11e4b 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d  lidateCursorsOnM
11e4c 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 73 71  odifiedBtrees(sq
11e4d 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
11e4e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
11e4f 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
11e50 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20  .    Btree *p = 
11e51 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
11e52 20 20 20 20 69 66 28 20 70 20 26 26 20 73 71 6c      if( p && sql
11e53 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
11e54 6e 73 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73  ns(p) ){.      s
11e55 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41  qlite3BtreeTripA
11e56 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c  llCursors(p, SQL
11e57 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20  ITE_ABORT);.    
11e58 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  }.  }.}../*.** T
11e59 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
11e5a 61 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61  alled the when a
11e5b 20 56 44 42 45 20 74 72 69 65 73 20 74 6f 20 68   VDBE tries to h
11e5c 61 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44 42  alt.  If the VDB
11e5d 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68  E.** has made ch
11e5e 61 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20  anges and is in 
11e5f 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c  autocommit mode,
11e60 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f   then commit tho
11e61 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20  se.** changes.  
11e62 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73  If a rollback is
11e63 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f   needed, then do
11e64 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a   the rollback..*
11e65 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
11e66 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61  e is the only wa
11e67 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74  y to move the st
11e68 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d  ate of a VM from
11e69 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  .** SQLITE_MAGIC
11e6a 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d  _RUN to SQLITE_M
11e6b 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69  AGIC_HALT.  It i
11e6c 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a  s harmless to.**
11e6d 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20   call this on a 
11e6e 56 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74 68  VM that is in th
11e6f 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48  e SQLITE_MAGIC_H
11e70 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  ALT state..**.**
11e71 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
11e72 20 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63   code.  If the c
11e73 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20  ommit could not 
11e74 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65  complete because
11e75 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74   of.** lock cont
11e76 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53  ention, return S
11e77 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20  QLITE_BUSY.  If 
11e78 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
11e79 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d  eturned, it.** m
11e7a 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64  eans the close d
11e7b 69 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e  id not happen an
11e7c 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  d needs to be re
11e7d 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  peated..*/.SQLIT
11e7e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
11e7f 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64  lite3VdbeHalt(Vd
11e80 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  be *p){.  sqlite
11e81 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  3 *db = p->db;. 
11e82 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 28 2a   int i;.  int (*
11e83 78 46 75 6e 63 29 28 42 74 72 65 65 20 2a 70 42  xFunc)(Btree *pB
11e84 74 29 20 3d 20 30 3b 20 20 2f 2a 20 46 75 6e 63  t) = 0;  /* Func
11e85 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 6f 6e 20  tion to call on 
11e86 65 61 63 68 20 62 74 72 65 65 20 62 61 63 6b 65  each btree backe
11e87 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 70  nd */.  int isSp
11e88 65 63 69 61 6c 45 72 72 6f 72 3b 20 20 20 20 20  ecialError;     
11e89 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f         /* Set to
11e8a 20 74 72 75 65 20 69 66 20 53 51 4c 49 54 45 5f   true if SQLITE_
11e8b 4e 4f 4d 45 4d 20 6f 72 20 49 4f 45 52 52 20 2a  NOMEM or IOERR *
11e8c 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e  /..  /* This fun
11e8d 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74  ction contains t
11e8e 68 65 20 6c 6f 67 69 63 20 74 68 61 74 20 64 65  he logic that de
11e8f 74 65 72 6d 69 6e 65 73 20 69 66 20 61 20 73 74  termines if a st
11e90 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20 2a 2a 20  atement or.  ** 
11e91 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c  transaction will
11e92 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72   be committed or
11e93 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20   rolled back as 
11e94 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a  a result of the.
11e95 20 20 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f    ** execution o
11e96 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 6d  f this virtual m
11e97 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20  achine. .  **.  
11e98 2a 2a 20 53 70 65 63 69 61 6c 20 65 72 72 6f 72  ** Special error
11e99 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  s:.  **.  **    
11e9a 20 49 66 20 61 6e 20 53 51 4c 49 54 45 5f 4e 4f   If an SQLITE_NO
11e9b 4d 45 4d 20 65 72 72 6f 72 20 68 61 73 20 6f 63  MEM error has oc
11e9c 63 75 72 65 64 20 69 6e 20 61 20 73 74 61 74 65  cured in a state
11e9d 6d 65 6e 74 20 74 68 61 74 20 77 72 69 74 65 73  ment that writes
11e9e 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 74 68 65   to.  **     the
11e9f 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20   database, then 
11ea0 65 69 74 68 65 72 20 61 20 73 74 61 74 65 6d 65  either a stateme
11ea1 6e 74 20 6f 72 20 74 72 61 6e 73 61 63 74 69 6f  nt or transactio
11ea2 6e 20 6d 75 73 74 20 62 65 20 72 6f 6c 6c 65 64  n must be rolled
11ea3 0a 20 20 2a 2a 20 20 20 20 20 62 61 63 6b 20 74  .  **     back t
11ea4 6f 20 65 6e 73 75 72 65 20 74 68 65 20 74 72 65  o ensure the tre
11ea5 65 2d 73 74 72 75 63 74 75 72 65 73 20 61 72 65  e-structures are
11ea6 20 69 6e 20 61 20 63 6f 6e 73 69 73 74 65 6e 74   in a consistent
11ea7 20 73 74 61 74 65 2e 20 41 0a 20 20 2a 2a 20 20   state. A.  **  
11ea8 20 20 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61     statement tra
11ea9 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
11eaa 65 64 20 62 61 63 6b 20 69 66 20 6f 6e 65 20 69  ed back if one i
11eab 73 20 6f 70 65 6e 2c 20 6f 74 68 65 72 77 69 73  s open, otherwis
11eac 65 20 74 68 65 0a 20 20 2a 2a 20 20 20 20 20 65  e the.  **     e
11ead 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f  ntire transactio
11eae 6e 20 6d 75 73 74 20 62 65 20 72 6f 6c 6c 65 64  n must be rolled
11eaf 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   back..  **.  **
11eb0 20 20 20 20 20 49 66 20 61 6e 20 53 51 4c 49 54       If an SQLIT
11eb1 45 5f 49 4f 45 52 52 20 65 72 72 6f 72 20 68 61  E_IOERR error ha
11eb2 73 20 6f 63 63 75 72 65 64 20 69 6e 20 61 20 73  s occured in a s
11eb3 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77 72  tatement that wr
11eb4 69 74 65 73 20 74 6f 0a 20 20 2a 2a 20 20 20 20  ites to.  **    
11eb5 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
11eb6 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 74  hen the entire t
11eb7 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
11eb8 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  be rolled back. 
11eb9 54 68 65 0a 20 20 2a 2a 20 20 20 20 20 49 2f 4f  The.  **     I/O
11eba 20 65 72 72 6f 72 20 6d 61 79 20 68 61 76 65 20   error may have 
11ebb 63 61 75 73 65 64 20 67 61 72 62 61 67 65 20 74  caused garbage t
11ebc 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20  o be written to 
11ebd 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a  the journal .  *
11ebe 2a 20 20 20 20 20 66 69 6c 65 2e 20 57 65 72 65  *     file. Were
11ebf 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
11ec0 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 61 6e 64   to continue and
11ec1 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 72   eventually be r
11ec2 6f 6c 6c 65 64 20 0a 20 20 2a 2a 20 20 20 20 20  olled .  **     
11ec3 62 61 63 6b 20 74 68 61 74 20 67 61 72 62 61 67  back that garbag
11ec4 65 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20 69  e might end up i
11ec5 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
11ec6 69 6c 65 2e 0a 20 20 2a 2a 20 20 20 20 20 0a 20  ile..  **     . 
11ec7 20 2a 2a 20 20 20 20 20 49 6e 20 62 6f 74 68 20   **     In both 
11ec8 6f 66 20 74 68 65 20 61 62 6f 76 65 20 63 61 73  of the above cas
11ec9 65 73 2c 20 74 68 65 20 56 64 62 65 2e 65 72 72  es, the Vdbe.err
11eca 6f 72 41 63 74 69 6f 6e 20 76 61 72 69 61 62 6c  orAction variabl
11ecb 65 20 69 73 20 0a 20 20 2a 2a 20 20 20 20 20 69  e is .  **     i
11ecc 67 6e 6f 72 65 64 2e 20 49 66 20 74 68 65 20 73  gnored. If the s
11ecd 71 6c 69 74 65 33 2e 61 75 74 6f 43 6f 6d 6d 69  qlite3.autoCommi
11ece 74 20 66 6c 61 67 20 69 73 20 66 61 6c 73 65 20  t flag is false 
11ecf 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f  and a transactio
11ed0 6e 0a 20 20 2a 2a 20 20 20 20 20 69 73 20 72 6f  n.  **     is ro
11ed1 6c 6c 65 64 20 62 61 63 6b 2c 20 69 74 20 77 69  lled back, it wi
11ed2 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 74 72 75  ll be set to tru
11ed3 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68  e..  **.  ** Oth
11ed4 65 72 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a  er errors:.  **.
11ed5 20 20 2a 2a 20 4e 6f 20 65 72 72 6f 72 3a 0a 20    ** No error:. 
11ed6 20 2a 2a 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20   **.  */..  if( 
11ed7 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
11ed8 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63  led ){.    p->rc
11ed9 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
11eda 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43  .  }.  closeAllC
11edb 75 72 73 6f 72 73 45 78 63 65 70 74 41 63 74 69  ursorsExceptActi
11edc 76 65 56 74 61 62 73 28 70 29 3b 0a 20 20 69 66  veVtabs(p);.  if
11edd 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45  ( p->magic!=VDBE
11ede 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20  _MAGIC_RUN ){.  
11edf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11ee0 4f 4b 3b 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41  OK;.  }.  checkA
11ee1 63 74 69 76 65 56 64 62 65 43 6e 74 28 64 62 29  ctiveVdbeCnt(db)
11ee2 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69  ;..  /* No commi
11ee3 74 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65  t or rollback ne
11ee4 65 64 65 64 20 69 66 20 74 68 65 20 70 72 6f 67  eded if the prog
11ee5 72 61 6d 20 6e 65 76 65 72 20 73 74 61 72 74 65  ram never starte
11ee6 64 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63  d */.  if( p->pc
11ee7 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d  >=0 ){.    int m
11ee8 72 63 3b 20 20 20 2f 2a 20 50 72 69 6d 61 72 79  rc;   /* Primary
11ee9 20 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d   error code from
11eea 20 70 2d 3e 72 63 20 2a 2f 0a 0a 20 20 20 20 2f   p->rc */..    /
11eeb 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65  * Lock all btree
11eec 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 74  s used by the st
11eed 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73  atement */.    s
11eee 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
11eef 41 72 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61  ArrayEnter(&p->a
11ef0 4d 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20  Mutex);..    /* 
11ef1 43 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66  Check for one of
11ef2 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 72   the special err
11ef3 6f 72 73 20 2d 20 53 51 4c 49 54 45 5f 4e 4f 4d  ors - SQLITE_NOM
11ef4 45 4d 20 6f 72 20 53 51 4c 49 54 45 5f 49 4f 45  EM or SQLITE_IOE
11ef5 52 52 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20  RR */.    mrc = 
11ef6 70 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20  p->rc & 0xff;.  
11ef7 20 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72    isSpecialError
11ef8 20 3d 20 28 0a 20 20 20 20 20 20 20 20 28 6d 72   = (.        (mr
11ef9 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
11efa 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49  || mrc==SQLITE_I
11efb 4f 45 52 52 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c  OERR || mrc==SQL
11efc 49 54 45 5f 49 4e 54 45 52 52 55 50 54 29 3f 31  ITE_INTERRUPT)?1
11efd 3a 30 29 3b 0a 20 20 20 20 69 66 28 20 69 73 53  :0);.    if( isS
11efe 70 65 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20  pecialError ){. 
11eff 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f       /* This loo
11f00 70 20 64 6f 65 73 20 73 74 61 74 69 63 20 61 6e  p does static an
11f01 61 6c 79 73 69 73 20 6f 66 20 74 68 65 20 71 75  alysis of the qu
11f02 65 72 79 20 74 6f 20 73 65 65 20 77 68 69 63 68  ery to see which
11f03 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a   of the.      **
11f04 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65   following three
11f05 20 63 61 74 65 67 6f 72 69 65 73 20 69 74 20 66   categories it f
11f06 61 6c 6c 73 20 69 6e 74 6f 3a 0a 20 20 20 20 20  alls into:.     
11f07 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   **.      **    
11f08 20 52 65 61 64 2d 6f 6e 6c 79 0a 20 20 20 20 20   Read-only.     
11f09 20 2a 2a 20 20 20 20 20 51 75 65 72 79 20 77 69   **     Query wi
11f0a 74 68 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  th statement jou
11f0b 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 20 20  rnal.      **   
11f0c 20 20 51 75 65 72 79 20 77 69 74 68 6f 75 74 20    Query without 
11f0d 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
11f0e 6c 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  l.      **.     
11f0f 20 2a 2a 20 57 65 20 63 6f 75 6c 64 20 64 6f 20   ** We could do 
11f10 73 6f 6d 65 74 68 69 6e 67 20 6d 6f 72 65 20 65  something more e
11f11 6c 65 67 61 6e 74 20 74 68 61 6e 20 74 68 69 73  legant than this
11f12 20 73 74 61 74 69 63 20 61 6e 61 6c 79 73 69 73   static analysis
11f13 20 28 69 2e 65 2e 0a 20 20 20 20 20 20 2a 2a 20   (i.e..      ** 
11f14 73 74 6f 72 65 20 74 68 65 20 74 79 70 65 20 6f  store the type o
11f15 66 20 71 75 65 72 79 20 61 73 20 70 61 72 74 20  f query as part 
11f16 6f 66 20 74 68 65 20 63 6f 6d 70 6c 69 61 74 69  of the compliati
11f17 6f 6e 20 70 68 61 73 65 29 2c 20 62 75 74 20 0a  on phase), but .
11f18 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 69 6e        ** handlin
11f19 67 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f  g malloc() or IO
11f1a 20 66 61 69 6c 75 72 65 20 69 73 20 61 20 66 61   failure is a fa
11f1b 69 72 6c 79 20 6f 62 73 63 75 72 65 20 65 64 67  irly obscure edg
11f1c 65 20 63 61 73 65 20 73 6f 20 0a 20 20 20 20 20  e case so .     
11f1d 20 2a 2a 20 74 68 69 73 20 69 73 20 70 72 6f 62   ** this is prob
11f1e 61 62 6c 79 20 65 61 73 69 65 72 2e 20 54 6f 64  ably easier. Tod
11f1f 6f 3a 20 4d 69 67 68 74 20 62 65 20 61 6e 20 6f  o: Might be an o
11f20 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 72 65  pportunity to re
11f21 64 75 63 65 20 0a 20 20 20 20 20 20 2a 2a 20 63  duce .      ** c
11f22 6f 64 65 20 73 69 7a 65 20 61 20 76 65 72 79 20  ode size a very 
11f23 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 74 68 6f  small amount tho
11f24 75 67 68 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a  ugh....      */.
11f25 20 20 20 20 20 20 69 6e 74 20 69 73 52 65 61 64        int isRead
11f26 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 20 20  Only = 1;.      
11f27 69 6e 74 20 69 73 53 74 61 74 65 6d 65 6e 74 20  int isStatement 
11f28 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72  = 0;.      asser
11f29 74 28 70 2d 3e 61 4f 70 20 7c 7c 20 70 2d 3e 6e  t(p->aOp || p->n
11f2a 4f 70 3d 3d 30 29 3b 0a 20 20 20 20 20 20 66 6f  Op==0);.      fo
11f2b 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
11f2c 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 20 20   i++){ .        
11f2d 73 77 69 74 63 68 28 20 70 2d 3e 61 4f 70 5b 69  switch( p->aOp[i
11f2e 5d 2e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20  ].opcode ){.    
11f2f 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 54 72        case OP_Tr
11f30 61 6e 73 61 63 74 69 6f 6e 3a 0a 20 20 20 20 20  ansaction:.     
11f31 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69         /* This i
11f32 73 20 61 20 62 69 74 20 73 74 72 61 6e 67 65 2e  s a bit strange.
11f33 20 49 66 20 77 65 20 68 69 74 20 61 20 6d 61 6c   If we hit a mal
11f34 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72 72 6f  loc() or IO erro
11f35 72 20 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20  r and.          
11f36 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65    ** the stateme
11f37 6e 74 20 64 69 64 20 6e 6f 74 20 6f 70 65 6e 20  nt did not open 
11f38 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  a statement tran
11f39 73 61 63 74 69 6f 6e 2c 20 77 65 20 77 69 6c 6c  saction, we will
11f3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
11f3b 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74  rollback any act
11f3c 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ive transaction 
11f3d 61 6e 64 20 61 62 6f 72 74 20 61 6c 6c 20 6f 74  and abort all ot
11f3e 68 65 72 20 61 63 74 69 76 65 0a 20 20 20 20 20  her active.     
11f3f 20 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d         ** statem
11f40 65 6e 74 73 2e 20 4f 72 2c 20 69 66 20 74 68 69  ents. Or, if thi
11f41 73 20 69 73 20 61 6e 20 53 51 4c 49 54 45 5f 49  s is an SQLITE_I
11f42 4e 54 45 52 52 55 50 54 20 65 72 72 6f 72 2c 20  NTERRUPT error, 
11f43 77 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  we.            *
11f44 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 6f 6c 6c  * will only roll
11f45 62 61 63 6b 20 69 66 20 74 68 65 20 69 6e 74 65  back if the inte
11f46 72 72 75 70 74 65 64 20 73 74 61 74 65 6d 65 6e  rrupted statemen
11f47 74 20 77 61 73 20 61 20 77 72 69 74 65 2e 0a 20  t was a write.. 
11f48 20 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20             **.  
11f49 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20            ** It 
11f4a 63 6f 75 6c 64 20 62 65 20 61 72 67 75 65 64 20  could be argued 
11f4b 74 68 61 74 20 72 65 61 64 2d 6f 6e 6c 79 20 73  that read-only s
11f4c 74 61 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64  tatements should
11f4d 20 6e 65 76 65 72 0a 20 20 20 20 20 20 20 20 20   never.         
11f4e 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 61     ** rollback a
11f4f 6e 79 74 68 69 6e 67 2e 20 42 75 74 20 63 61 72  nything. But car
11f50 65 66 75 6c 20 61 6e 61 6c 79 73 69 73 20 69 73  eful analysis is
11f51 20 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65   required before
11f52 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
11f53 6d 61 6b 69 6e 67 20 74 68 69 73 20 63 68 61 6e  making this chan
11f54 67 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  ge.            *
11f55 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  /.            if
11f56 28 20 70 2d 3e 61 4f 70 5b 69 5d 2e 70 32 20 7c  ( p->aOp[i].p2 |
11f57 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e  | mrc!=SQLITE_IN
11f58 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20 20 20  TERRUPT ){.     
11f59 20 20 20 20 20 20 20 20 20 69 73 52 65 61 64 4f           isReadO
11f5a 6e 6c 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  nly = 0;.       
11f5b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
11f5c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
11f5d 20 20 20 20 63 61 73 65 20 4f 50 5f 53 74 61 74      case OP_Stat
11f5e 65 6d 65 6e 74 3a 0a 20 20 20 20 20 20 20 20 20  ement:.         
11f5f 20 20 20 69 73 53 74 61 74 65 6d 65 6e 74 20 3d     isStatement =
11f60 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
11f61 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
11f62 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 0a 20 20  .      }..   .  
11f63 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75      /* If the qu
11f64 65 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c  ery was read-onl
11f65 79 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f  y, we need do no
11f66 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c   rollback at all
11f67 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20  . Otherwise,.   
11f68 20 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69     ** proceed wi
11f69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 68  th the special h
11f6a 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a  andling..      *
11f6b 2f 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 52  /.      if( !isR
11f6c 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20  eadOnly ){.     
11f6d 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51     if( p->rc==SQ
11f6e 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
11f6f 45 44 20 26 26 20 69 73 53 74 61 74 65 6d 65 6e  ED && isStatemen
11f70 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78  t ){.          x
11f71 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 42 74  Func = sqlite3Bt
11f72 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 3b  reeRollbackStmt;
11f73 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63  .          p->rc
11f74 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
11f75 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69          } else i
11f76 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  f( p->rc==SQLITE
11f77 5f 4e 4f 4d 45 4d 20 26 26 20 69 73 53 74 61 74  _NOMEM && isStat
11f78 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20  ement ){.       
11f79 20 20 20 78 46 75 6e 63 20 3d 20 73 71 6c 69 74     xFunc = sqlit
11f7a 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53  e3BtreeRollbackS
11f7b 74 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  tmt;.        }el
11f7c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
11f7d 20 57 65 20 61 72 65 20 66 6f 72 63 65 64 20 74   We are forced t
11f7e 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 20  o roll back the 
11f7f 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69  active transacti
11f80 6f 6e 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67  on. Before doing
11f81 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f  .          ** so
11f82 2c 20 61 62 6f 72 74 20 61 6e 79 20 6f 74 68 65  , abort any othe
11f83 72 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 69  r statements thi
11f84 73 20 68 61 6e 64 6c 65 20 63 75 72 72 65 6e 74  s handle current
11f85 6c 79 20 68 61 73 20 61 63 74 69 76 65 2e 0a 20  ly has active.. 
11f86 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
11f87 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65        invalidate
11f88 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65  CursorsOnModifie
11f89 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20  dBtrees(db);.   
11f8a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f         sqlite3Ro
11f8b 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20  llbackAll(db);. 
11f8c 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74           db->aut
11f8d 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  oCommit = 1;.   
11f8e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
11f8f 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
11f90 66 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69  f the auto-commi
11f91 74 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e  t flag is set an
11f92 64 20 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e  d this is the on
11f93 6c 79 20 61 63 74 69 76 65 20 76 64 62 65 2c 20  ly active vdbe, 
11f94 74 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20 64  then.    ** we d
11f95 6f 20 65 69 74 68 65 72 20 61 20 63 6f 6d 6d 69  o either a commi
11f96 74 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66  t or rollback of
11f97 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
11f98 6e 73 61 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a  nsaction. .    *
11f99 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54  *.    ** Note: T
11f9a 68 69 73 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72  his block also r
11f9b 75 6e 73 20 69 66 20 6f 6e 65 20 6f 66 20 74 68  uns if one of th
11f9c 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73  e special errors
11f9d 20 68 61 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a   handled .    **
11f9e 20 61 62 6f 76 65 20 68 61 73 20 6f 63 63 75 72   above has occur
11f9f 65 64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ed. .    */.    
11fa0 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  if( db->autoComm
11fa1 69 74 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65  it && db->active
11fa2 56 64 62 65 43 6e 74 3d 3d 31 20 29 7b 0a 20 20  VdbeCnt==1 ){.  
11fa3 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53      if( p->rc==S
11fa4 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 2d 3e  QLITE_OK || (p->
11fa5 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f  errorAction==OE_
11fa6 46 61 69 6c 20 26 26 20 21 69 73 53 70 65 63 69  Fail && !isSpeci
11fa7 61 6c 45 72 72 6f 72 29 20 29 7b 0a 20 20 20 20  alError) ){.    
11fa8 20 20 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d      /* The auto-
11fa9 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 74  commit flag is t
11faa 72 75 65 2c 20 61 6e 64 20 74 68 65 20 76 64 62  rue, and the vdb
11fab 65 20 70 72 6f 67 72 61 6d 20 77 61 73 20 0a 20  e program was . 
11fac 20 20 20 20 20 20 20 2a 2a 20 73 75 63 63 65 73         ** succes
11fad 73 66 75 6c 20 6f 72 20 68 69 74 20 61 6e 20 27  sful or hit an '
11fae 4f 52 20 46 41 49 4c 27 20 63 6f 6e 73 74 72 61  OR FAIL' constra
11faf 69 6e 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  int. This means 
11fb0 61 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20  a commit .      
11fb1 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64    ** is required
11fb2 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
11fb3 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 76 64       int rc = vd
11fb4 62 65 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20  beCommit(db);.  
11fb5 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
11fb6 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20  LITE_BUSY ){.   
11fb7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
11fb8 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
11fb9 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a  ve(&p->aMutex);.
11fba 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
11fbb 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
11fbc 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
11fbd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
11fbe 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63  .          p->rc
11fbf 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20   = rc;.         
11fc0 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
11fc1 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20  All(db);.       
11fc2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
11fc3 20 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49    sqlite3CommitI
11fc4 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 64  nternalChanges(d
11fc5 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  b);.        }.  
11fc6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
11fc7 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61     sqlite3Rollba
11fc8 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20  ckAll(db);.     
11fc9 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
11fca 20 21 78 46 75 6e 63 20 29 7b 0a 20 20 20 20 20   !xFunc ){.     
11fcb 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
11fcc 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f  TE_OK || p->erro
11fcd 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c  rAction==OE_Fail
11fce 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
11fcf 70 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65  p->openedStateme
11fd0 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nt ){.          
11fd1 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 42  xFunc = sqlite3B
11fd2 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 3b 0a  treeCommitStmt;.
11fd3 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20          } .     
11fd4 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72   }else if( p->er
11fd5 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62  rorAction==OE_Ab
11fd6 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 78  ort ){.        x
11fd7 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 42 74  Func = sqlite3Bt
11fd8 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 3b  reeRollbackStmt;
11fd9 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
11fda 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65        invalidate
11fdb 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65  CursorsOnModifie
11fdc 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20  dBtrees(db);.   
11fdd 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c       sqlite3Roll
11fde 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20  backAll(db);.   
11fdf 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d       db->autoCom
11fe0 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  mit = 1;.      }
11fe1 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
11fe2 20 49 66 20 78 46 75 6e 63 20 69 73 20 6e 6f 74   If xFunc is not
11fe3 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69   NULL, then it i
11fe4 73 20 6f 6e 65 20 6f 66 20 73 71 6c 69 74 65 33  s one of sqlite3
11fe5 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d  BtreeRollbackStm
11fe6 74 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69  t or.    ** sqli
11fe7 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 74  te3BtreeCommitSt
11fe8 6d 74 2e 20 43 61 6c 6c 20 69 74 20 6f 6e 63 65  mt. Call it once
11fe9 20 6f 6e 20 65 61 63 68 20 62 61 63 6b 65 6e 64   on each backend
11fea 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
11feb 63 75 72 73 0a 20 20 20 20 2a 2a 20 61 6e 64 20  curs.    ** and 
11fec 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
11fed 69 73 20 73 74 69 6c 6c 20 53 51 4c 49 54 45 5f  is still SQLITE_
11fee 4f 4b 2c 20 73 65 74 20 74 68 65 20 72 65 74 75  OK, set the retu
11fef 72 6e 20 63 6f 64 65 20 74 6f 20 74 68 65 20 6e  rn code to the n
11ff0 65 77 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20  ew.    ** error 
11ff1 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  value..    */.  
11ff2 20 20 61 73 73 65 72 74 28 21 78 46 75 6e 63 20    assert(!xFunc 
11ff3 7c 7c 0a 20 20 20 20 20 20 78 46 75 6e 63 3d 3d  ||.      xFunc==
11ff4 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
11ff5 69 74 53 74 6d 74 20 7c 7c 0a 20 20 20 20 20 20  itStmt ||.      
11ff6 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65 33 42 74  xFunc==sqlite3Bt
11ff7 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 0a  reeRollbackStmt.
11ff8 20 20 20 20 29 3b 0a 20 20 20 20 66 6f 72 28 69      );.    for(i
11ff9 3d 30 3b 20 78 46 75 6e 63 20 26 26 20 69 3c 64  =0; xFunc && i<d
11ffa 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20  b->nDb; i++){ . 
11ffb 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
11ffc 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
11ffd 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
11ffe 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b        if( pBt ){
11fff 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 78 46  .        rc = xF
12000 75 6e 63 28 70 42 74 29 3b 0a 20 20 20 20 20 20  unc(pBt);.      
12001 20 20 69 66 28 20 72 63 20 26 26 20 28 70 2d 3e    if( rc && (p->
12002 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
12003 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43   p->rc==SQLITE_C
12004 4f 4e 53 54 52 41 49 4e 54 29 20 29 7b 0a 20 20  ONSTRAINT) ){.  
12005 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20          p->rc = 
12006 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  rc;.          sq
12007 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
12008 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 30 29 3b 0a  p->zErrMsg, 0);.
12009 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1200a 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
1200b 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 61 6e  * If this was an
1200c 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20   INSERT, UPDATE 
1200d 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 74 68  or DELETE and th
1200e 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20  e statement was 
1200f 63 6f 6d 6d 69 74 74 65 64 2c 20 0a 20 20 20 20  committed, .    
12010 2a 2a 20 73 65 74 20 74 68 65 20 63 68 61 6e 67  ** set the chang
12011 65 20 63 6f 75 6e 74 65 72 2e 20 0a 20 20 20 20  e counter. .    
12012 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 68  */.    if( p->ch
12013 61 6e 67 65 43 6e 74 4f 6e 20 26 26 20 70 2d 3e  angeCntOn && p->
12014 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  pc>=0 ){.      i
12015 66 28 20 21 78 46 75 6e 63 20 7c 7c 20 78 46 75  f( !xFunc || xFu
12016 6e 63 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65  nc==sqlite3Btree
12017 43 6f 6d 6d 69 74 53 74 6d 74 20 29 7b 0a 20 20  CommitStmt ){.  
12018 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
12019 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20  eSetChanges(db, 
1201a 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20  p->nChange);.   
1201b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1201c 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1201d 43 68 61 6e 67 65 73 28 64 62 2c 20 30 29 3b 0a  Changes(db, 0);.
1201e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
1201f 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20  >nChange = 0;.  
12020 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 6f    }.  .    /* Ro
12021 6c 6c 62 61 63 6b 20 6f 72 20 63 6f 6d 6d 69 74  llback or commit
12022 20 61 6e 79 20 73 63 68 65 6d 61 20 63 68 61 6e   any schema chan
12023 67 65 73 20 74 68 61 74 20 6f 63 63 75 72 72 65  ges that occurre
12024 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d  d. */.    if( p-
12025 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26  >rc!=SQLITE_OK &
12026 26 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49  & db->flags&SQLI
12027 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
12028 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
12029 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1202a 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20  hema(db, 0);.   
1202b 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 28     db->flags = (
1202c 64 62 2d 3e 66 6c 61 67 73 20 7c 20 53 51 4c 49  db->flags | SQLI
1202d 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
1202e 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
1202f 20 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63   Release the loc
12030 6b 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  ks */.    sqlite
12031 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  3BtreeMutexArray
12032 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78  Leave(&p->aMutex
12033 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20  );.  }..  /* We 
12034 68 61 76 65 20 73 75 63 63 65 73 73 66 75 6c 6c  have successfull
12035 79 20 68 61 6c 74 65 64 20 61 6e 64 20 63 6c 6f  y halted and clo
12036 73 65 64 20 74 68 65 20 56 4d 2e 20 20 52 65 63  sed the VM.  Rec
12037 6f 72 64 20 74 68 69 73 20 66 61 63 74 2e 20 2a  ord this fact. *
12038 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30  /.  if( p->pc>=0
12039 20 29 7b 0a 20 20 20 20 64 62 2d 3e 61 63 74 69   ){.    db->acti
1203a 76 65 56 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 7d  veVdbeCnt--;.  }
1203b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
1203c 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20  BE_MAGIC_HALT;. 
1203d 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65   checkActiveVdbe
1203e 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70  Cnt(db);.  if( p
1203f 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
12040 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  ed ){.    p->rc 
12041 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
12042 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76    }.  checkActiv
12043 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20  eVdbeCnt(db);.. 
12044 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
12045 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  K;.}.../*.** Eac
12046 68 20 56 44 42 45 20 68 6f 6c 64 73 20 74 68 65  h VDBE holds the
12047 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6d   result of the m
12048 6f 73 74 20 72 65 63 65 6e 74 20 73 71 6c 69 74  ost recent sqlit
12049 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 0a 2a  e3_step() call.*
1204a 2a 20 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68 69  * in p->rc.  Thi
1204b 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
1204c 68 61 74 20 72 65 73 75 6c 74 20 62 61 63 6b 20  hat result back 
1204d 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  to SQLITE_OK..*/
1204e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1204f 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
12050 52 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28  ResetStepResult(
12051 56 64 62 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 72  Vdbe *p){.  p->r
12052 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  c = SQLITE_OK;.}
12053 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70  ../*.** Clean up
12054 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78   a VDBE after ex
12055 65 63 75 74 69 6f 6e 20 62 75 74 20 64 6f 20 6e  ecution but do n
12056 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 56 44  ot delete the VD
12057 42 45 20 6a 75 73 74 20 79 65 74 2e 0a 2a 2a 20  BE just yet..** 
12058 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20  Write any error 
12059 6d 65 73 73 61 67 65 73 20 69 6e 74 6f 20 2a 70  messages into *p
1205a 7a 45 72 72 4d 73 67 2e 20 20 52 65 74 75 72 6e  zErrMsg.  Return
1205b 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65   the result code
1205c 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68  ..**.** After th
1205d 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75  is routine is ru
1205e 6e 2c 20 74 68 65 20 56 44 42 45 20 73 68 6f 75  n, the VDBE shou
1205f 6c 64 20 62 65 20 72 65 61 64 79 20 74 6f 20 62  ld be ready to b
12060 65 20 65 78 65 63 75 74 65 64 0a 2a 2a 20 61 67  e executed.** ag
12061 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f  ain..**.** To lo
12062 6f 6b 20 61 74 20 69 74 20 61 6e 6f 74 68 65 72  ok at it another
12063 20 77 61 79 2c 20 74 68 69 73 20 72 6f 75 74 69   way, this routi
12064 6e 65 20 72 65 73 65 74 73 20 74 68 65 20 73 74  ne resets the st
12065 61 74 65 20 6f 66 20 74 68 65 0a 2a 2a 20 76 69  ate of the.** vi
12066 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 72  rtual machine fr
12067 6f 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  om VDBE_MAGIC_RU
12068 4e 20 6f 72 20 56 44 42 45 5f 4d 41 47 49 43 5f  N or VDBE_MAGIC_
12069 48 41 4c 54 20 62 61 63 6b 20 74 6f 0a 2a 2a 20  HALT back to.** 
1206a 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 2e  VDBE_MAGIC_INIT.
1206b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1206c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1206d 62 65 52 65 73 65 74 28 56 64 62 65 20 2a 70 29  beReset(Vdbe *p)
1206e 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
1206f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a  .  db = p->db;..
12070 20 20 2f 2a 20 49 66 20 74 68 65 20 56 4d 20 64    /* If the VM d
12071 69 64 20 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f  id not run to co
12072 6d 70 6c 65 74 69 6f 6e 20 6f 72 20 69 66 20 69  mpletion or if i
12073 74 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 6e  t encountered an
12074 0a 20 20 2a 2a 20 65 72 72 6f 72 2c 20 74 68 65  .  ** error, the
12075 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68  n it might not h
12076 61 76 65 20 62 65 65 6e 20 68 61 6c 74 65 64 20  ave been halted 
12077 70 72 6f 70 65 72 6c 79 2e 20 20 53 6f 20 68 61  properly.  So ha
12078 6c 74 0a 20 20 2a 2a 20 69 74 20 6e 6f 77 2e 0a  lt.  ** it now..
12079 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 61    */.  sqlite3Sa
1207a 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71  fetyOn(db);.  sq
1207b 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29  lite3VdbeHalt(p)
1207c 3b 0a 20 20 73 71 6c 69 74 65 33 53 61 66 65 74  ;.  sqlite3Safet
1207d 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20  yOff(db);..  /* 
1207e 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20  If the VDBE has 
1207f 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72 74  be run even part
12080 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 6e  ially, then tran
12081 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20 63  sfer the error c
12082 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 72  ode.  ** and err
12083 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20  or message from 
12084 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74 68  the VDBE into th
12085 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
12086 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74 0a  structure.  But.
12087 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42 45    ** if the VDBE
12088 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 73   has just been s
12089 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68 61  et to run but ha
1208a 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 65  s not actually e
1208b 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a 2a  xecuted any.  **
1208c 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 65   instructions ye
1208d 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61 69  t, leave the mai
1208e 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72  n database error
1208f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e 63   information unc
12090 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  hanged..  */.  i
12091 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  f( p->pc>=0 ){. 
12092 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d 73     if( p->zErrMs
12093 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  g ){.      sqlit
12094 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62  e3ValueSetStr(db
12095 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e 7a 45 72  ->pErr,-1,p->zEr
12096 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55 54 46 38  rMsg,SQLITE_UTF8
12097 2c 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a  ,sqlite3_free);.
12098 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64        db->errCod
12099 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20 20  e = p->rc;.     
1209a 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b   p->zErrMsg = 0;
1209b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
1209c 2d 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71  ->rc ){.      sq
1209d 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70  lite3Error(db, p
1209e 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65  ->rc, 0);.    }e
1209f 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
120a0 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
120a1 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d  TE_OK, 0);.    }
120a2 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
120a3 72 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64  rc && p->expired
120a4 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65   ){.    /* The e
120a5 78 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20  xpired flag was 
120a6 73 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20  set on the VDBE 
120a7 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74  before the first
120a8 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20   call.    ** to 
120a9 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20  sqlite3_step(). 
120aa 46 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20  For consistency 
120ab 28 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73  (since sqlite3_s
120ac 74 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a  tep() was.    **
120ad 20 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68   called), set th
120ae 65 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72  e database error
120af 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73   in this case as
120b0 20 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20   well..    */.  
120b1 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
120b2 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20  b, p->rc, 0);.  
120b3 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c 61 69 6d 20  }..  /* Reclaim 
120b4 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  all memory used 
120b5 62 79 20 74 68 65 20 56 44 42 45 0a 20 20 2a 2f  by the VDBE.  */
120b6 0a 20 20 43 6c 65 61 6e 75 70 28 70 29 3b 0a 0a  .  Cleanup(p);..
120b7 20 20 2f 2a 20 53 61 76 65 20 70 72 6f 66 69 6c    /* Save profil
120b8 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
120b9 66 72 6f 6d 20 74 68 69 73 20 56 44 42 45 20 72  from this VDBE r
120ba 75 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  un..  */.  asser
120bb 74 28 20 70 2d 3e 70 54 6f 73 3c 26 70 2d 3e 61  t( p->pTos<&p->a
120bc 53 74 61 63 6b 5b 70 2d 3e 70 63 3c 30 3f 30 3a  Stack[p->pc<0?0:
120bd 70 2d 3e 70 63 5d 20 7c 7c 20 21 70 2d 3e 61 53  p->pc] || !p->aS
120be 74 61 63 6b 20 29 3b 0a 23 69 66 64 65 66 20 56  tack );.#ifdef V
120bf 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a  DBE_PROFILE.  {.
120c0 20 20 20 20 46 49 4c 45 20 2a 6f 75 74 20 3d 20      FILE *out = 
120c1 66 6f 70 65 6e 28 22 76 64 62 65 5f 70 72 6f 66  fopen("vdbe_prof
120c2 69 6c 65 2e 6f 75 74 22 2c 20 22 61 22 29 3b 0a  ile.out", "a");.
120c3 20 20 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20      if( out ){. 
120c4 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
120c5 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
120c6 2d 2d 2d 2d 20 22 29 3b 0a 20 20 20 20 20 20 66  ---- ");.      f
120c7 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70  or(i=0; i<p->nOp
120c8 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
120c9 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 30  fprintf(out, "%0
120ca 32 78 22 2c 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f  2x", p->aOp[i].o
120cb 70 63 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a  pcode);.      }.
120cc 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
120cd 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 20 20  t, "\n");.      
120ce 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f  for(i=0; i<p->nO
120cf 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  p; i++){.       
120d0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
120d1 36 64 20 25 31 30 6c 6c 64 20 25 38 6c 6c 64 20  6d %10lld %8lld 
120d2 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d  ",.           p-
120d3 3e 61 4f 70 5b 69 5d 2e 63 6e 74 2c 0a 20 20 20  >aOp[i].cnt,.   
120d4 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69          p->aOp[i
120d5 5d 2e 63 79 63 6c 65 73 2c 0a 20 20 20 20 20 20  ].cycles,.      
120d6 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63       p->aOp[i].c
120d7 6e 74 3e 30 20 3f 20 70 2d 3e 61 4f 70 5b 69 5d  nt>0 ? p->aOp[i]
120d8 2e 63 79 63 6c 65 73 2f 70 2d 3e 61 4f 70 5b 69  .cycles/p->aOp[i
120d9 5d 2e 63 6e 74 20 3a 20 30 0a 20 20 20 20 20 20  ].cnt : 0.      
120da 20 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c    );.        sql
120db 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
120dc 6f 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b  out, i, &p->aOp[
120dd 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i]);.      }.   
120de 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a     fclose(out);.
120df 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
120e0 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
120e1 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20  BE_MAGIC_INIT;. 
120e2 20 70 2d 3e 61 62 6f 72 74 65 64 20 3d 20 30 3b   p->aborted = 0;
120e3 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 72 63 20  .  return p->rc 
120e4 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d  & db->errMask;.}
120e5 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75  . ./*.** Clean u
120e6 70 20 61 6e 64 20 64 65 6c 65 74 65 20 61 20 56  p and delete a V
120e7 44 42 45 20 61 66 74 65 72 20 65 78 65 63 75 74  DBE after execut
120e8 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 61 6e 20  ion.  Return an 
120e9 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
120ea 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 63  .** the result c
120eb 6f 64 65 2e 20 20 57 72 69 74 65 20 61 6e 79 20  ode.  Write any 
120ec 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65  error message te
120ed 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73  xt into *pzErrMs
120ee 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
120ef 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
120f0 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62  VdbeFinalize(Vdb
120f1 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20  e *p){.  int rc 
120f2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
120f3 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  f( p->magic==VDB
120f4 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70  E_MAGIC_RUN || p
120f5 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
120f6 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20 20 20 20  GIC_HALT ){.    
120f7 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
120f8 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 61 73  Reset(p);.    as
120f9 73 65 72 74 28 20 28 72 63 20 26 20 70 2d 3e 64  sert( (rc & p->d
120fa 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20  b->errMask)==rc 
120fb 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
120fc 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41  ->magic!=VDBE_MA
120fd 47 49 43 5f 49 4e 49 54 20 29 7b 0a 20 20 20 20  GIC_INIT ){.    
120fe 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
120ff 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  SUSE;.  }.  sqli
12100 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70 29  te3VdbeDelete(p)
12101 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
12102 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65  ../*.** Call the
12103 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   destructor for 
12104 65 61 63 68 20 61 75 78 64 61 74 61 20 65 6e 74  each auxdata ent
12105 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e 63 20  ry in pVdbeFunc 
12106 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 68 65  for which.** the
12107 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62   corresponding b
12108 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 63 6c  it in mask is cl
12109 65 61 72 2e 20 20 41 75 78 64 61 74 61 20 65 6e  ear.  Auxdata en
1210a 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33 31 0a  tries beyond 31.
1210b 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20 64 65  ** are always de
1210c 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 65 73  stroyed.  To des
1210d 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 74 61  troy all auxdata
1210e 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c 20 74   entries, call t
1210f 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77  his.** routine w
12110 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f 0a  ith mask==0..*/.
12111 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
12112 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
12113 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62  eleteAuxData(Vdb
12114 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63  eFunc *pVdbeFunc
12115 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 69  , int mask){.  i
12116 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
12117 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41   i<pVdbeFunc->nA
12118 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74  ux; i++){.    st
12119 72 75 63 74 20 41 75 78 44 61 74 61 20 2a 70 41  ruct AuxData *pA
1211a 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d  ux = &pVdbeFunc-
1211b 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 20 69  >apAux[i];.    i
1211c 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 6d 61  f( (i>31 || !(ma
1211d 73 6b 26 28 31 3c 3c 69 29 29 29 20 26 26 20 70  sk&(1<<i))) && p
1211e 41 75 78 2d 3e 70 41 75 78 20 29 7b 0a 20 20 20  Aux->pAux ){.   
1211f 20 20 20 69 66 28 20 70 41 75 78 2d 3e 78 44 65     if( pAux->xDe
12120 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  lete ){.        
12121 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 28 70 41  pAux->xDelete(pA
12122 75 78 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 20  ux->pAux);.     
12123 20 7d 0a 20 20 20 20 20 20 70 41 75 78 2d 3e 70   }.      pAux->p
12124 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Aux = 0;.    }. 
12125 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65   }.}../*.** Dele
12126 74 65 20 61 6e 20 65 6e 74 69 72 65 20 56 44 42  te an entire VDB
12127 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  E..*/.SQLITE_PRI
12128 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
12129 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65  3VdbeDelete(Vdbe
1212a 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
1212b 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
1212c 72 6e 3b 0a 20 20 43 6c 65 61 6e 75 70 28 70 29  rn;.  Cleanup(p)
1212d 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76  ;.  if( p->pPrev
1212e 20 29 7b 0a 20 20 20 20 70 2d 3e 70 50 72 65 76   ){.    p->pPrev
1212f 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65  ->pNext = p->pNe
12130 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  xt;.  }else{.   
12131 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e   assert( p->db->
12132 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 20 20 20  pVdbe==p );.    
12133 70 2d 3e 64 62 2d 3e 70 56 64 62 65 20 3d 20 70  p->db->pVdbe = p
12134 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
12135 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  f( p->pNext ){. 
12136 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72     p->pNext->pPr
12137 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20  ev = p->pPrev;. 
12138 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20   }.  if( p->aOp 
12139 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
1213a 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a  i<p->nOp; i++){.
1213b 20 20 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d 20        Op *pOp = 
1213c 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20  &p->aOp[i];.    
1213d 20 20 66 72 65 65 50 33 28 70 4f 70 2d 3e 70 33    freeP3(pOp->p3
1213e 74 79 70 65 2c 20 70 4f 70 2d 3e 70 33 29 3b 0a  type, pOp->p3);.
1213f 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
12140 33 5f 66 72 65 65 28 70 2d 3e 61 4f 70 29 3b 0a  3_free(p->aOp);.
12141 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d    }.  releaseMem
12142 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 70  Array(p->aVar, p
12143 2d 3e 6e 56 61 72 29 3b 0a 20 20 73 71 6c 69 74  ->nVar);.  sqlit
12144 65 33 5f 66 72 65 65 28 70 2d 3e 61 4c 61 62 65  e3_free(p->aLabe
12145 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  l);.  sqlite3_fr
12146 65 65 28 70 2d 3e 61 53 74 61 63 6b 29 3b 0a 20  ee(p->aStack);. 
12147 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79   releaseMemArray
12148 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d  (p->aColName, p-
12149 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e  >nResColumn*COLN
1214a 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65  AME_N);.  sqlite
1214b 33 5f 66 72 65 65 28 70 2d 3e 61 43 6f 6c 4e 61  3_free(p->aColNa
1214c 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  me);.  sqlite3_f
1214d 72 65 65 28 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20  ree(p->zSql);.  
1214e 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f  p->magic = VDBE_
1214f 4d 41 47 49 43 5f 44 45 41 44 3b 0a 20 20 73 71  MAGIC_DEAD;.  sq
12150 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d  lite3_free(p);.}
12151 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76  ../*.** If a Mov
12152 65 54 6f 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  eTo operation is
12153 20 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20   pending on the 
12154 67 69 76 65 6e 20 63 75 72 73 6f 72 2c 20 74 68  given cursor, th
12155 65 6e 20 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f  en do that.** Mo
12156 76 65 54 6f 20 6e 6f 77 2e 20 20 52 65 74 75 72  veTo now.  Retur
12157 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e  n an error code.
12158 20 20 49 66 20 6e 6f 20 4d 6f 76 65 54 6f 20 69    If no MoveTo i
12159 73 20 70 65 6e 64 69 6e 67 2c 20 74 68 69 73 0a  s pending, this.
1215a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  ** routine does 
1215b 6e 6f 74 68 69 6e 67 20 61 6e 64 20 72 65 74 75  nothing and retu
1215c 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  rns SQLITE_OK..*
1215d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1215e 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1215f 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 43 75 72  CursorMoveto(Cur
12160 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  sor *p){.  if( p
12161 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
12162 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c   ){.    int res,
12163 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c 49   rc;.#ifdef SQLI
12164 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74 65  TE_TEST.    exte
12165 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  rn int sqlite3_s
12166 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e  earch_count;.#en
12167 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20  dif.    assert( 
12168 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  p->isTable );.  
12169 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1216a 72 65 65 4d 6f 76 65 74 6f 28 70 2d 3e 70 43 75  reeMoveto(p->pCu
1216b 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d 6f 76 65  rsor, 0, p->move
1216c 74 6f 54 61 72 67 65 74 2c 20 30 2c 20 26 72 65  toTarget, 0, &re
1216d 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  s);.    if( rc )
1216e 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1216f 2a 70 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 30  *p->pIncrKey = 0
12170 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77  ;.    p->lastRow
12171 69 64 20 3d 20 6b 65 79 54 6f 49 6e 74 28 70 2d  id = keyToInt(p-
12172 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 29 3b 0a  >movetoTarget);.
12173 20 20 20 20 70 2d 3e 72 6f 77 69 64 49 73 56 61      p->rowidIsVa
12174 6c 69 64 20 3d 20 72 65 73 3d 3d 30 3b 0a 20 20  lid = res==0;.  
12175 20 20 69 66 28 20 72 65 73 3c 30 20 29 7b 0a 20    if( res<0 ){. 
12176 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12177 33 42 74 72 65 65 4e 65 78 74 28 70 2d 3e 70 43  3BtreeNext(p->pC
12178 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20  ursor, &res);.  
12179 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1217a 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 23 69  urn rc;.    }.#i
1217b 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1217c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61  .    sqlite3_sea
1217d 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e  rch_count++;.#en
1217e 64 69 66 0a 20 20 20 20 70 2d 3e 64 65 66 65 72  dif.    p->defer
1217f 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
12180 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75     p->cacheStatu
12181 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
12182 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
12183 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
12184 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
12185 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a  functions:.**.**
12186 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
12187 61 6c 54 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69  alType().** sqli
12188 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
12189 65 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65  eLen().** sqlite
1218a 33 56 64 62 65 53 65 72 69 61 6c 52 65 61 64 28  3VdbeSerialRead(
1218b 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  ).** sqlite3Vdbe
1218c 53 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73  SerialLen().** s
1218d 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1218e 57 72 69 74 65 28 29 0a 2a 2a 0a 2a 2a 20 65 6e  Write().**.** en
1218f 63 61 70 73 75 6c 61 74 65 20 74 68 65 20 63 6f  capsulate the co
12190 64 65 20 74 68 61 74 20 73 65 72 69 61 6c 69 7a  de that serializ
12191 65 73 20 76 61 6c 75 65 73 20 66 6f 72 20 73 74  es values for st
12192 6f 72 61 67 65 20 69 6e 20 53 51 4c 69 74 65 0a  orage in SQLite.
12193 2a 2a 20 64 61 74 61 20 61 6e 64 20 69 6e 64 65  ** data and inde
12194 78 20 72 65 63 6f 72 64 73 2e 20 45 61 63 68 20  x records. Each 
12195 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65  serialized value
12196 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a   consists of a.*
12197 2a 20 27 73 65 72 69 61 6c 2d 74 79 70 65 27 20  * 'serial-type' 
12198 61 6e 64 20 61 20 62 6c 6f 62 20 6f 66 20 64 61  and a blob of da
12199 74 61 2e 20 54 68 65 20 73 65 72 69 61 6c 20 74  ta. The serial t
1219a 79 70 65 20 69 73 20 61 6e 20 38 2d 62 79 74 65  ype is an 8-byte
1219b 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74   unsigned.** int
1219c 65 67 65 72 2c 20 73 74 6f 72 65 64 20 61 73 20  eger, stored as 
1219d 61 20 76 61 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20  a varint..**.** 
1219e 49 6e 20 61 6e 20 53 51 4c 69 74 65 20 69 6e 64  In an SQLite ind
1219f 65 78 20 72 65 63 6f 72 64 2c 20 74 68 65 20 73  ex record, the s
121a0 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 73 74  erial type is st
121a1 6f 72 65 64 20 64 69 72 65 63 74 6c 79 20 62 65  ored directly be
121a2 66 6f 72 65 0a 2a 2a 20 74 68 65 20 62 6c 6f 62  fore.** the blob
121a3 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 74   of data that it
121a4 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 2e   corresponds to.
121a5 20 49 6e 20 61 20 74 61 62 6c 65 20 72 65 63 6f   In a table reco
121a6 72 64 2c 20 61 6c 6c 20 73 65 72 69 61 6c 0a 2a  rd, all serial.*
121a7 2a 20 74 79 70 65 73 20 61 72 65 20 73 74 6f 72  * types are stor
121a8 65 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20  ed at the start 
121a9 6f 66 20 74 68 65 20 72 65 63 6f 72 64 2c 20 61  of the record, a
121aa 6e 64 20 74 68 65 20 62 6c 6f 62 73 20 6f 66 20  nd the blobs of 
121ab 64 61 74 61 20 61 74 0a 2a 2a 20 74 68 65 20 65  data at.** the e
121ac 6e 64 2e 20 48 65 6e 63 65 20 74 68 65 73 65 20  nd. Hence these 
121ad 66 75 6e 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 20  functions allow 
121ae 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 68 61  the caller to ha
121af 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 73 65 72 69  ndle the.** seri
121b0 61 6c 2d 74 79 70 65 20 61 6e 64 20 64 61 74 61  al-type and data
121b1 20 62 6c 6f 62 20 73 65 70 65 72 61 74 65 6c 79   blob seperately
121b2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ..**.** The foll
121b3 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 73 63  owing table desc
121b4 72 69 62 65 73 20 74 68 65 20 76 61 72 69 6f 75  ribes the variou
121b5 73 20 73 74 6f 72 61 67 65 20 63 6c 61 73 73 65  s storage classe
121b6 73 20 66 6f 72 20 64 61 74 61 3a 0a 2a 2a 0a 2a  s for data:.**.*
121b7 2a 20 20 20 73 65 72 69 61 6c 20 74 79 70 65 20  *   serial type 
121b8 20 20 20 20 20 20 20 62 79 74 65 73 20 6f 66 20         bytes of 
121b9 64 61 74 61 20 20 20 20 20 20 74 79 70 65 0a 2a  data      type.*
121ba 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *   ------------
121bb 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  --     ---------
121bc 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d  ------    ------
121bd 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20  ---------.**    
121be 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
121bf 20 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20          0       
121c0 20 20 20 20 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20       NULL.**    
121c1 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20    1             
121c2 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20          1       
121c3 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
121c4 67 65 72 0a 2a 2a 20 20 20 20 20 20 32 20 20 20  ger.**      2   
121c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121c6 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 73    2            s
121c7 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
121c8 20 20 20 20 20 20 33 20 20 20 20 20 20 20 20 20        3         
121c9 20 20 20 20 20 20 20 20 20 20 20 20 33 20 20 20              3   
121ca 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20           signed 
121cb 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
121cc 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  4               
121cd 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20        4         
121ce 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65     signed intege
121cf 72 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20  r.**      5     
121d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121d1 36 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67  6            sig
121d2 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  ned integer.**  
121d3 20 20 20 20 36 20 20 20 20 20 20 20 20 20 20 20      6           
121d4 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20            8     
121d5 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e         signed in
121d6 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 37 20  teger.**      7 
121d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121d8 20 20 20 20 38 20 20 20 20 20 20 20 20 20 20 20      8           
121d9 20 49 45 45 45 20 66 6c 6f 61 74 0a 2a 2a 20 20   IEEE float.**  
121da 20 20 20 20 38 20 20 20 20 20 20 20 20 20 20 20      8           
121db 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 20            0     
121dc 20 20 20 20 20 20 20 49 6e 74 65 67 65 72 20 63         Integer c
121dd 6f 6e 73 74 61 6e 74 20 30 0a 2a 2a 20 20 20 20  onstant 0.**    
121de 20 20 39 20 20 20 20 20 20 20 20 20 20 20 20 20    9             
121df 20 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20          0       
121e0 20 20 20 20 20 49 6e 74 65 67 65 72 20 63 6f 6e       Integer con
121e1 73 74 61 6e 74 20 31 0a 2a 2a 20 20 20 20 20 31  stant 1.**     1
121e2 30 2c 31 31 20 20 20 20 20 20 20 20 20 20 20 20  0,11            
121e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121e4 20 20 20 72 65 73 65 72 76 65 64 20 66 6f 72 20     reserved for 
121e5 65 78 70 61 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  expansion.**    
121e6 4e 3e 3d 31 32 20 61 6e 64 20 65 76 65 6e 20 20  N>=12 and even  
121e7 20 20 20 20 20 28 4e 2d 31 32 29 2f 32 20 20 20       (N-12)/2   
121e8 20 20 20 20 20 42 4c 4f 42 0a 2a 2a 20 20 20 20       BLOB.**    
121e9 4e 3e 3d 31 33 20 61 6e 64 20 6f 64 64 20 20 20  N>=13 and odd   
121ea 20 20 20 20 20 28 4e 2d 31 33 29 2f 32 20 20 20       (N-13)/2   
121eb 20 20 20 20 20 74 65 78 74 0a 2a 2a 0a 2a 2a 20       text.**.** 
121ec 54 68 65 20 38 20 61 6e 64 20 39 20 74 79 70 65  The 8 and 9 type
121ed 73 20 77 65 72 65 20 61 64 64 65 64 20 69 6e 20  s were added in 
121ee 33 2e 33 2e 30 2c 20 66 69 6c 65 20 66 6f 72 6d  3.3.0, file form
121ef 61 74 20 34 2e 20 20 50 72 69 6f 72 20 76 65 72  at 4.  Prior ver
121f0 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69  sions.** of SQLi
121f1 74 65 20 77 69 6c 6c 20 6e 6f 74 20 75 6e 64 65  te will not unde
121f2 72 73 74 61 6e 64 20 74 68 6f 73 65 20 73 65 72  rstand those ser
121f3 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2f 0a 0a 2f  ial types..*/../
121f4 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
121f5 73 65 72 69 61 6c 2d 74 79 70 65 20 66 6f 72 20  serial-type for 
121f6 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64  the value stored
121f7 20 69 6e 20 70 4d 65 6d 2e 0a 2a 2f 0a 53 51 4c   in pMem..*/.SQL
121f8 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
121f9 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
121fa 6c 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c  lType(Mem *pMem,
121fb 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74   int file_format
121fc 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d  ){.  int flags =
121fd 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20   pMem->flags;.  
121fe 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28 20 66 6c  int n;..  if( fl
121ff 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  ags&MEM_Null ){.
12200 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
12201 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45  }.  if( flags&ME
12202 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20  M_Int ){.    /* 
12203 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 74 68  Figure out wheth
12204 65 72 20 74 6f 20 75 73 65 20 31 2c 20 32 2c 20  er to use 1, 2, 
12205 34 2c 20 36 20 6f 72 20 38 20 62 79 74 65 73 2e  4, 6 or 8 bytes.
12206 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65 20 4d   */.#   define M
12207 41 58 5f 36 42 59 54 45 20 28 28 28 28 69 36 34  AX_6BYTE ((((i64
12208 29 30 78 30 30 30 30 31 30 30 30 29 3c 3c 33 32  )0x00001000)<<32
12209 29 2d 31 29 0a 20 20 20 20 69 36 34 20 69 20 3d  )-1).    i64 i =
1220a 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20   pMem->u.i;.    
1220b 75 36 34 20 75 3b 0a 20 20 20 20 69 66 28 20 66  u64 u;.    if( f
1220c 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 26 26  ile_format>=4 &&
1220d 20 28 69 26 31 29 3d 3d 69 20 29 7b 0a 20 20 20   (i&1)==i ){.   
1220e 20 20 20 72 65 74 75 72 6e 20 38 2b 69 3b 0a 20     return 8+i;. 
1220f 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69 3c 30     }.    u = i<0
12210 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20 20 69   ? -i : i;.    i
12211 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65 74 75  f( u<=127 ) retu
12212 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 75 3c  rn 1;.    if( u<
12213 3d 33 32 37 36 37 20 29 20 72 65 74 75 72 6e 20  =32767 ) return 
12214 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 38 33  2;.    if( u<=83
12215 38 38 36 30 37 20 29 20 72 65 74 75 72 6e 20 33  88607 ) return 3
12216 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32 31 34  ;.    if( u<=214
12217 37 34 38 33 36 34 37 20 29 20 72 65 74 75 72 6e  7483647 ) return
12218 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 4d   4;.    if( u<=M
12219 41 58 5f 36 42 59 54 45 20 29 20 72 65 74 75 72  AX_6BYTE ) retur
1221a 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  n 5;.    return 
1221b 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61  6;.  }.  if( fla
1221c 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20  gs&MEM_Real ){. 
1221d 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d     return 7;.  }
1221e 0a 20 20 61 73 73 65 72 74 28 20 66 6c 61 67 73  .  assert( flags
1221f 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c  &(MEM_Str|MEM_Bl
12220 6f 62 29 20 29 3b 0a 20 20 6e 20 3d 20 70 4d 65  ob) );.  n = pMe
12221 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20 66 6c 61 67  m->n;.  if( flag
12222 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  s & MEM_Zero ){.
12223 20 20 20 20 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75      n += pMem->u
12224 2e 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  .i;.  }.  assert
12225 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75  ( n>=0 );.  retu
12226 72 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b  rn ((n*2) + 12 +
12227 20 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72   ((flags&MEM_Str
12228 29 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  )!=0));.}../*.**
12229 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67   Return the leng
1222a 74 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 63  th of the data c
1222b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
1222c 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 65 72  the supplied ser
1222d 69 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c  ial-type..*/.SQL
1222e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1222f 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
12230 6c 54 79 70 65 4c 65 6e 28 75 33 32 20 73 65 72  lTypeLen(u32 ser
12231 69 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69 66 28  ial_type){.  if(
12232 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32   serial_type>=12
12233 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
12234 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f  serial_type-12)/
12235 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  2;.  }else{.    
12236 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
12237 61 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31  aSize[] = { 0, 1
12238 2c 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c  , 2, 3, 4, 6, 8,
12239 20 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d   8, 0, 0, 0, 0 }
1223a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 53 69  ;.    return aSi
1223b 7a 65 5b 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b  ze[serial_type];
1223c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
1223d 20 77 65 20 61 72 65 20 6f 6e 20 61 6e 20 61 72   we are on an ar
1223e 63 68 69 74 65 63 74 75 72 65 20 77 69 74 68 20  chitecture with 
1223f 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f  mixed-endian flo
12240 61 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73  ating .** points
12241 20 28 65 78 3a 20 41 52 4d 37 29 20 74 68 65 6e   (ex: ARM7) then
12242 20 73 77 61 70 20 74 68 65 20 6c 6f 77 65 72 20   swap the lower 
12243 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68 65  4 bytes with the
12244 20 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 79 74   .** upper 4 byt
12245 65 73 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  es.  Return the 
12246 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f  result..**.** Fo
12247 72 20 6d 6f 73 74 20 61 72 63 68 69 74 65 63 74  r most architect
12248 75 72 65 73 2c 20 74 68 69 73 20 69 73 20 61 20  ures, this is a 
12249 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61  no-op..**.** (la
1224a 74 65 72 29 3a 20 20 49 74 20 69 73 20 72 65 70  ter):  It is rep
1224b 6f 72 74 65 64 20 74 6f 20 6d 65 20 74 68 61 74  orted to me that
1224c 20 74 68 65 20 6d 69 78 65 64 2d 65 6e 64 69 61   the mixed-endia
1224d 6e 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20  n problem.** on 
1224e 41 52 4d 37 20 69 73 20 61 6e 20 69 73 73 75 65  ARM7 is an issue
1224f 20 77 69 74 68 20 47 43 43 2c 20 6e 6f 74 20 77   with GCC, not w
12250 69 74 68 20 74 68 65 20 41 52 4d 37 20 63 68 69  ith the ARM7 chi
12251 70 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20  p.  It seems.** 
12252 74 68 61 74 20 65 61 72 6c 79 20 76 65 72 73 69  that early versi
12253 6f 6e 73 20 6f 66 20 47 43 43 20 73 74 6f 72 65  ons of GCC store
12254 64 20 74 68 65 20 74 77 6f 20 77 6f 72 64 73 20  d the two words 
12255 6f 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66  of a 64-bit.** f
12256 6c 6f 61 74 20 69 6e 20 74 68 65 20 77 72 6f 6e  loat in the wron
12257 67 20 6f 72 64 65 72 2e 20 20 41 6e 64 20 74 68  g order.  And th
12258 61 74 20 65 72 72 6f 72 20 68 61 73 20 62 65 65  at error has bee
12259 6e 20 70 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20  n propagated.** 
1225a 65 76 65 72 20 73 69 6e 63 65 2e 20 20 54 68 65  ever since.  The
1225b 20 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65   blame is not ne
1225c 63 65 73 73 61 72 69 6c 79 20 77 69 74 68 20 47  cessarily with G
1225d 43 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47  CC, though..** G
1225e 43 43 20 6d 69 67 68 74 20 68 61 76 65 20 6a 75  CC might have ju
1225f 73 74 20 63 6f 70 79 69 6e 67 20 74 68 65 20 70  st copying the p
12260 72 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72  roblem from a pr
12261 69 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a  ior compiler..**
12262 20 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20   I am also told 
12263 74 68 61 74 20 6e 65 77 65 72 20 76 65 72 73 69  that newer versi
12264 6f 6e 73 20 6f 66 20 47 43 43 20 74 68 61 74 20  ons of GCC that 
12265 66 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65 72 65  follow a differe
12266 6e 74 0a 2a 2a 20 41 42 49 20 67 65 74 20 74 68  nt.** ABI get th
12267 65 20 62 79 74 65 20 6f 72 64 65 72 20 72 69 67  e byte order rig
12268 68 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f  ht..**.** Develo
12269 70 65 72 73 20 75 73 69 6e 67 20 53 51 4c 69 74  pers using SQLit
1226a 65 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f  e on an ARM7 sho
1226b 75 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20  uld compile and 
1226c 72 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61 70 70  run their.** app
1226d 6c 69 63 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d  lication using -
1226e 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 20  DSQLITE_DEBUG=1 
1226f 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20  at least once.  
12270 57 69 74 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e  With DEBUG.** en
12271 61 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73 73 65  abled, some asse
12272 72 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65  rts below will e
12273 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 62  nsure that the b
12274 79 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20  yte order of.** 
12275 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
12276 61 6c 75 65 73 20 69 73 20 63 6f 72 72 65 63 74  alues is correct
12277 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38  ..**.** (2007-08
12278 2d 33 30 29 20 20 46 72 61 6e 6b 20 76 61 6e 20  -30)  Frank van 
12279 56 75 67 74 20 68 61 73 20 73 74 75 64 69 65 64  Vugt has studied
1227a 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c   this problem cl
1227b 6f 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73  osely.** and has
1227c 20 73 65 6e 64 20 68 69 73 20 66 69 6e 64 69 6e   send his findin
1227d 67 73 20 74 6f 20 74 68 65 20 53 51 4c 69 74 65  gs to the SQLite
1227e 20 64 65 76 65 6c 6f 70 65 72 73 2e 20 20 46 72   developers.  Fr
1227f 61 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20 74 68  ank.** writes th
12280 61 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65  at some Linux ke
12281 72 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61  rnels offer floa
12282 74 69 6e 67 20 70 6f 69 6e 74 20 68 61 72 64 77  ting point hardw
12283 61 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e  are.** emulation
12284 20 74 68 61 74 20 75 73 65 73 20 6f 6e 6c 79 20   that uses only 
12285 33 32 2d 62 69 74 20 6d 61 6e 74 69 73 73 61 73  32-bit mantissas
12286 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 75   instead of a fu
12287 6c 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61  ll .** 48-bits a
12288 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68  s required by th
12289 65 20 49 45 45 45 20 73 74 61 6e 64 61 72 64 2e  e IEEE standard.
1228a 20 20 28 54 68 69 73 20 69 73 20 74 68 65 0a 2a    (This is the.*
1228b 2a 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53  * CONFIG_FPE_FAS
1228c 54 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f  TFPE option.)  O
1228d 6e 20 73 75 63 68 20 73 79 73 74 65 6d 73 2c 20  n such systems, 
1228e 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a  floating point.*
1228f 2a 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20  * byte swapping 
12290 62 65 63 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d  becomes very com
12291 70 6c 69 63 61 74 65 64 2e 20 20 54 6f 20 61 76  plicated.  To av
12292 6f 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a  oid problems,.**
12293 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 62   the necessary b
12294 79 74 65 20 73 77 61 70 70 69 6e 67 20 69 73 20  yte swapping is 
12295 63 61 72 72 69 65 64 20 6f 75 74 20 75 73 69 6e  carried out usin
12296 67 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67  g a 64-bit integ
12297 65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74 68 61  er.** rather tha
12298 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74  n a 64-bit float
12299 2e 20 20 46 72 61 6e 6b 20 61 73 73 75 72 65 73  .  Frank assures
1229a 20 75 73 20 74 68 61 74 20 74 68 65 20 63 6f 64   us that the cod
1229b 65 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20  e here.** works 
1229c 66 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20 74 68  for him.  We, th
1229d 65 20 64 65 76 65 6c 6f 70 65 72 73 2c 20 68 61  e developers, ha
1229e 76 65 20 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64  ve no way to ind
1229f 65 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65  ependently.** ve
122a0 72 69 66 79 20 74 68 69 73 2c 20 62 75 74 20 46  rify this, but F
122a1 72 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e  rank seems to kn
122a2 6f 77 20 77 68 61 74 20 68 65 20 69 73 20 74 61  ow what he is ta
122a3 6c 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73  lking about.** s
122a4 6f 20 77 65 20 74 72 75 73 74 20 68 69 6d 2e 0a  o we trust him..
122a5 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
122a6 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34  _MIXED_ENDIAN_64
122a7 42 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63  BIT_FLOAT.static
122a8 20 75 36 34 20 66 6c 6f 61 74 53 77 61 70 28 75   u64 floatSwap(u
122a9 36 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20  64 in){.  union 
122aa 7b 0a 20 20 20 20 75 36 34 20 72 3b 0a 20 20 20  {.    u64 r;.   
122ab 20 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75   u32 i[2];.  } u
122ac 3b 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e  ;.  u32 t;..  u.
122ad 72 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e  r = in;.  t = u.
122ae 69 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d  i[0];.  u.i[0] =
122af 20 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31   u.i[1];.  u.i[1
122b0 5d 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 6e 20  ] = t;.  return 
122b1 75 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20  u.r;.}.# define 
122b2 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46  swapMixedEndianF
122b3 6c 6f 61 74 28 58 29 20 20 58 20 3d 20 66 6c 6f  loat(X)  X = flo
122b4 61 74 53 77 61 70 28 58 29 0a 23 65 6c 73 65 0a  atSwap(X).#else.
122b5 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78  # define swapMix
122b6 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29  edEndianFloat(X)
122b7 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
122b8 72 69 74 65 20 74 68 65 20 73 65 72 69 61 6c 69  rite the seriali
122b9 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20 66 6f  zed data blob fo
122ba 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  r the value stor
122bb 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20  ed in pMem into 
122bc 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 20 61  .** buf. It is a
122bd 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20  ssumed that the 
122be 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63  caller has alloc
122bf 61 74 65 64 20 73 75 66 66 69 63 69 65 6e 74 20  ated sufficient 
122c0 73 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e  space..** Return
122c1 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
122c2 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a  ytes written..**
122c3 0a 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 65 20  .** nBuf is the 
122c4 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20  amount of space 
122c5 6c 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20  left in buf[].  
122c6 6e 42 75 66 20 6d 75 73 74 20 61 6c 77 61 79 73  nBuf must always
122c7 20 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f   be.** large eno
122c8 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ugh to hold the 
122c9 65 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 20 45  entire field.  E
122ca 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 66 69  xcept, if the fi
122cb 65 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62  eld is.** a blob
122cc 20 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c   with a zero-fil
122cd 6c 65 64 20 74 61 69 6c 2c 20 74 68 65 6e 20 62  led tail, then b
122ce 75 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 6a 75  uf[] might be ju
122cf 73 74 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20  st the right.** 
122d0 73 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 76 65  size to hold eve
122d1 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 66  rything except f
122d2 6f 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c  or the zero-fill
122d3 65 64 20 74 61 69 6c 2e 20 20 49 66 20 62 75 66  ed tail.  If buf
122d4 5b 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69  [].** is only bi
122d5 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  g enough to hold
122d6 20 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72   the non-zero pr
122d7 65 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  efix, then only 
122d8 77 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 70 72  write that.** pr
122d9 65 66 69 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e  efix into buf[].
122da 20 20 42 75 74 20 69 66 20 62 75 66 5b 5d 20 69    But if buf[] i
122db 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74  s large enough t
122dc 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a  o hold both the.
122dd 2a 2a 20 70 72 65 66 69 78 20 61 6e 64 20 74 68  ** prefix and th
122de 65 20 74 61 69 6c 20 74 68 65 6e 20 77 72 69 74  e tail then writ
122df 65 20 74 68 65 20 70 72 65 66 69 78 20 61 6e 64  e the prefix and
122e0 20 73 65 74 20 74 68 65 20 74 61 69 6c 20 74 6f   set the tail to
122e1 20 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a   all.** zeros..*
122e2 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
122e3 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
122e4 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e  actually written
122e5 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68   into buf[].  Th
122e6 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62  e number.** of b
122e7 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72 6f  ytes in the zero
122e8 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73 20  -filled tail is 
122e9 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20  included in the 
122ea 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c  return value onl
122eb 79 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62 79  y.** if those by
122ec 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64 20  tes were zeroed 
122ed 69 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51  in buf[]..*/ .SQ
122ee 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
122ef 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
122f0 61 6c 50 75 74 28 75 38 20 2a 62 75 66 2c 20 69  alPut(u8 *buf, i
122f1 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d  nt nBuf, Mem *pM
122f2 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72  em, int file_for
122f3 6d 61 74 29 7b 0a 20 20 75 33 32 20 73 65 72 69  mat){.  u32 seri
122f4 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65  al_type = sqlite
122f5 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
122f6 70 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61  pMem, file_forma
122f7 74 29 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a  t);.  int len;..
122f8 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 6e 64    /* Integer and
122f9 20 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73   Real */.  if( s
122fa 65 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26  erial_type<=7 &&
122fb 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 20 29   serial_type>0 )
122fc 7b 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 20 20  {.    u64 v;.   
122fd 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28 20   int i;.    if( 
122fe 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29  serial_type==7 )
122ff 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
12300 73 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f  sizeof(v)==sizeo
12301 66 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20  f(pMem->r) );.  
12302 20 20 20 20 6d 65 6d 63 70 79 28 26 76 2c 20 26      memcpy(&v, &
12303 70 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28  pMem->r, sizeof(
12304 76 29 29 3b 0a 20 20 20 20 20 20 73 77 61 70 4d  v));.      swapM
12305 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
12306 76 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  v);.    }else{. 
12307 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75       v = pMem->u
12308 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65  .i;.    }.    le
12309 6e 20 3d 20 69 20 3d 20 73 71 6c 69 74 65 33 56  n = i = sqlite3V
1230a 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
1230b 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20  (serial_type);. 
1230c 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d     assert( len<=
1230d 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c  nBuf );.    whil
1230e 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20  e( i-- ){.      
1230f 62 75 66 5b 69 5d 20 3d 20 28 76 26 30 78 46 46  buf[i] = (v&0xFF
12310 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 38  );.      v >>= 8
12311 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
12312 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  rn len;.  }..  /
12313 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  * String or blob
12314 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c   */.  if( serial
12315 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20  _type>=12 ){.   
12316 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e   assert( pMem->n
12317 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73   + ((pMem->flags
12318 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65   & MEM_Zero)?pMe
12319 6d 2d 3e 75 2e 69 3a 30 29 0a 20 20 20 20 20 20  m->u.i:0).      
1231a 20 20 20 20 20 20 20 3d 3d 20 73 71 6c 69 74 65         == sqlite
1231b 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
1231c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 20  en(serial_type) 
1231d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1231e 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a  Mem->n<=nBuf );.
1231f 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e      len = pMem->
12320 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 62 75  n;.    memcpy(bu
12321 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29  f, pMem->z, len)
12322 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e  ;.    if( pMem->
12323 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
12324 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20 2b 3d   ){.      len +=
12325 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20   pMem->u.i;.    
12326 20 20 69 66 28 20 6c 65 6e 3e 6e 42 75 66 20 29    if( len>nBuf )
12327 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20  {.        len = 
12328 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d 0a 20 20  nBuf;.      }.  
12329 20 20 20 20 6d 65 6d 73 65 74 28 26 62 75 66 5b      memset(&buf[
1232a 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 6c 65 6e  pMem->n], 0, len
1232b 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7d  -pMem->n);.    }
1232c 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b  .    return len;
1232d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20  .  }..  /* NULL 
1232e 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20 30 20 6f  or constants 0 o
1232f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  r 1 */.  return 
12330 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65  0;.}../*.** Dese
12331 72 69 61 6c 69 7a 65 20 74 68 65 20 64 61 74 61  rialize the data
12332 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64 20 74 6f   blob pointed to
12333 20 62 79 20 62 75 66 20 61 73 20 73 65 72 69 61   by buf as seria
12334 6c 20 74 79 70 65 20 73 65 72 69 61 6c 5f 74 79  l type serial_ty
12335 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20  pe.** and store 
12336 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 70 4d  the result in pM
12337 65 6d 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  em.  Return the 
12338 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
12339 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45  read..*/ .SQLITE
1233a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1233b 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
1233c 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  t(.  const unsig
1233d 6e 65 64 20 63 68 61 72 20 2a 62 75 66 2c 20 20  ned char *buf,  
1233e 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20     /* Buffer to 
1233f 64 65 73 65 72 69 61 6c 69 7a 65 20 66 72 6f 6d  deserialize from
12340 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c   */.  u32 serial
12341 5f 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20  _type,          
12342 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79      /* Serial ty
12343 70 65 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a  pe to deserializ
12344 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d  e */.  Mem *pMem
12345 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12346 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
12347 65 6c 6c 20 74 6f 20 77 72 69 74 65 20 76 61 6c  ell to write val
12348 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b 0a 20 20  ue into */.){.  
12349 73 77 69 74 63 68 28 20 73 65 72 69 61 6c 5f 74  switch( serial_t
1234a 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ype ){.    case 
1234b 31 30 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65  10:   /* Reserve
1234c 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65  d for future use
1234d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 31 3a   */.    case 11:
1234e 20 20 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66     /* Reserved f
1234f 6f 72 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f  or future use */
12350 0a 20 20 20 20 63 61 73 65 20 30 3a 20 7b 20 20  .    case 0: {  
12351 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20  /* NULL */.     
12352 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
12353 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 62  EM_Null;.      b
12354 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
12355 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20 31 2d 62  case 1: { /* 1-b
12356 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
12357 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  er */.      pMem
12358 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e 65 64 20  ->u.i = (signed 
12359 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a 20 20 20  char)buf[0];.   
1235a 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1235b 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1235c 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1235d 20 20 20 20 63 61 73 65 20 32 3a 20 7b 20 2f 2a      case 2: { /*
1235e 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   2-byte signed i
1235f 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
12360 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73  pMem->u.i = (((s
12361 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30  igned char)buf[0
12362 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d 3b  ])<<8) | buf[1];
12363 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
12364 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
12365 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
12366 20 20 7d 0a 20 20 20 20 63 61 73 65 20 33 3a 20    }.    case 3: 
12367 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73 69 67 6e  { /* 3-byte sign
12368 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20  ed integer */.  
12369 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
1236a 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 62  (((signed char)b
1236b 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c 20 28 62  uf[0])<<16) | (b
1236c 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62 75 66 5b  uf[1]<<8) | buf[
1236d 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  2];.      pMem->
1236e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1236f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 33 3b  .      return 3;
12370 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
12371 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74 65 20 73  4: { /* 4-byte s
12372 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
12373 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
12374 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20   = (buf[0]<<24) 
12375 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c  | (buf[1]<<16) |
12376 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62   (buf[2]<<8) | b
12377 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 70 4d 65  uf[3];.      pMe
12378 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
12379 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  nt;.      return
1237a 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   4;.    }.    ca
1237b 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d 62 79 74  se 5: { /* 6-byt
1237c 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
1237d 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78 20   */.      u64 x 
1237e 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72  = (((signed char
1237f 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62  )buf[0])<<8) | b
12380 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 75 33 32  uf[1];.      u32
12381 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c 3c 32 34   y = (buf[2]<<24
12382 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c 31 36 29  ) | (buf[3]<<16)
12383 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38 29 20 7c   | (buf[4]<<8) |
12384 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20 20 20 78   buf[5];.      x
12385 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 79 3b 0a   = (x<<32) | y;.
12386 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
12387 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 20 20  = *(i64*)&x;.   
12388 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
12389 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1238a 72 65 74 75 72 6e 20 36 3b 0a 20 20 20 20 7d 0a  return 6;.    }.
1238b 20 20 20 20 63 61 73 65 20 36 3a 20 20 20 2f 2a      case 6:   /*
1238c 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   8-byte signed i
1238d 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 63 61  nteger */.    ca
1238e 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45 45 45 20  se 7: { /* IEEE 
1238f 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 2a  floating point *
12390 2f 0a 20 20 20 20 20 20 75 36 34 20 78 3b 0a 20  /.      u64 x;. 
12391 20 20 20 20 20 75 33 32 20 79 3b 0a 23 69 66 20       u32 y;.#if 
12392 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
12393 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
12394 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
12395 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20 20 20 2f  G_POINT).      /
12396 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 69 6e  * Verify that in
12397 74 65 67 65 72 73 20 61 6e 64 20 66 6c 6f 61 74  tegers and float
12398 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73  ing point values
12399 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 20 20   use the same.  
1239a 20 20 20 20 2a 2a 20 62 79 74 65 20 6f 72 64 65      ** byte orde
1239b 72 2e 20 20 4f 72 2c 20 74 68 61 74 20 69 66 20  r.  Or, that if 
1239c 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44  SQLITE_MIXED_END
1239d 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 54 20  IAN_64BIT_FLOAT 
1239e 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65 66 69  is.      ** defi
1239f 6e 65 64 20 74 68 61 74 20 36 34 2d 62 69 74 20  ned that 64-bit 
123a0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
123a1 61 6c 75 65 73 20 72 65 61 6c 6c 79 20 61 72 65  alues really are
123a2 20 6d 69 78 65 64 0a 20 20 20 20 20 20 2a 2a 20   mixed.      ** 
123a3 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20 20 2a 2f  endian..      */
123a4 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f  .      static co
123a5 6e 73 74 20 75 36 34 20 74 31 20 3d 20 28 28 75  nst u64 t1 = ((u
123a6 36 34 29 30 78 33 66 66 30 30 30 30 30 29 3c 3c  64)0x3ff00000)<<
123a7 33 32 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63  32;.      static
123a8 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65 20 72 31   const double r1
123a9 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 75 36   = 1.0;.      u6
123aa 34 20 74 32 20 3d 20 74 31 3b 0a 20 20 20 20 20  4 t2 = t1;.     
123ab 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e   swapMixedEndian
123ac 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20 20 20 20  Float(t2);.     
123ad 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
123ae 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74 32 29 20  r1)==sizeof(t2) 
123af 26 26 20 6d 65 6d 63 6d 70 28 26 72 31 2c 20 26  && memcmp(&r1, &
123b0 74 32 2c 20 73 69 7a 65 6f 66 28 72 31 29 29 3d  t2, sizeof(r1))=
123b1 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  =0 );.#endif..  
123b2 20 20 20 20 78 20 3d 20 28 62 75 66 5b 30 5d 3c      x = (buf[0]<
123b3 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c  <24) | (buf[1]<<
123b4 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38  16) | (buf[2]<<8
123b5 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20  ) | buf[3];.    
123b6 20 20 79 20 3d 20 28 62 75 66 5b 34 5d 3c 3c 32    y = (buf[4]<<2
123b7 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c 3c 31 36  4) | (buf[5]<<16
123b8 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c 38 29 20  ) | (buf[6]<<8) 
123b9 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20 20 20 20  | buf[7];.      
123ba 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 79 3b  x = (x<<32) | y;
123bb 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69 61  .      if( seria
123bc 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a 20 20 20  l_type==6 ){.   
123bd 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d       pMem->u.i =
123be 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 20 20 20   *(i64*)&x;.    
123bf 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
123c0 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
123c1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
123c2 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 78  assert( sizeof(x
123c3 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f 66 28 70  )==8 && sizeof(p
123c4 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b 0a 20 20  Mem->r)==8 );.  
123c5 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45        swapMixedE
123c6 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29 3b 0a 20  ndianFloat(x);. 
123c7 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70         memcpy(&p
123c8 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73 69 7a 65  Mem->r, &x, size
123c9 6f 66 28 78 29 29 3b 0a 20 20 20 20 20 20 20 20  of(x));.        
123ca 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
123cb 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a  M_Real;.      }.
123cc 20 20 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a        return 8;.
123cd 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38      }.    case 8
123ce 3a 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20  :    /* Integer 
123cf 30 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a  0 */.    case 9:
123d0 20 7b 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31   {  /* Integer 1
123d1 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
123d2 75 2e 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70  u.i = serial_typ
123d3 65 2d 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  e-8;.      pMem-
123d4 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
123d5 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
123d6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
123d7 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ult: {.      int
123d8 20 6c 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74   len = (serial_t
123d9 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20  ype-12)/2;.     
123da 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72   pMem->z = (char
123db 20 2a 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d   *)buf;.      pM
123dc 65 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20  em->n = len;.   
123dd 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20     pMem->xDel = 
123de 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72  0;.      if( ser
123df 69 61 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b  ial_type&0x01 ){
123e0 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  .        pMem->f
123e1 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c  lags = MEM_Str |
123e2 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20   MEM_Ephem;.    
123e3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
123e4 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
123e5 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70  EM_Blob | MEM_Ep
123e6 68 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  hem;.      }.   
123e7 20 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20     return len;. 
123e8 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
123e9 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  n 0;.}../*.** Th
123ea 65 20 68 65 61 64 65 72 20 6f 66 20 61 20 72 65  e header of a re
123eb 63 6f 72 64 20 63 6f 6e 73 69 73 74 73 20 6f 66  cord consists of
123ec 20 61 20 73 65 71 75 65 6e 63 65 20 76 61 72 69   a sequence vari
123ed 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
123ee 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69  gers..** These i
123ef 6e 74 65 67 65 72 73 20 61 72 65 20 61 6c 6d 6f  ntegers are almo
123f0 73 74 20 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20  st always small 
123f1 61 6e 64 20 61 72 65 20 65 6e 63 6f 64 65 64 20  and are encoded 
123f2 61 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65  as a single byte
123f3 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
123f4 6e 67 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61  ng macro takes a
123f5 64 76 61 6e 74 61 67 65 20 74 68 69 73 20 66 61  dvantage this fa
123f6 63 74 20 74 6f 20 70 72 6f 76 69 64 65 20 61 20  ct to provide a 
123f7 66 61 73 74 20 64 65 63 6f 64 65 0a 2a 2a 20 6f  fast decode.** o
123f8 66 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 69  f the integers i
123f9 6e 20 61 20 72 65 63 6f 72 64 20 68 65 61 64 65  n a record heade
123fa 72 2e 20 20 49 74 20 69 73 20 66 61 73 74 65 72  r.  It is faster
123fb 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 20   for the common 
123fc 63 61 73 65 0a 2a 2a 20 77 68 65 72 65 20 74 68  case.** where th
123fd 65 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 73  e integer is a s
123fe 69 6e 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20  ingle byte.  It 
123ff 69 73 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77  is a little slow
12400 65 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69  er when the.** i
12401 6e 74 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72  nteger is two or
12402 20 6d 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75   more bytes.  Bu
12403 74 20 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20  t overall it is 
12404 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  faster..**.** Th
12405 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72  e following expr
12406 65 73 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69  essions are equi
12407 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  valent:.**.**   
12408 20 20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74    x = sqlite3Get
12409 56 61 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20  Varint32( A, &B 
1240a 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d  );.**.**     x =
1240b 20 47 65 74 56 61 72 69 6e 74 28 20 41 2c 20 42   GetVarint( A, B
1240c 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e   );.**.*/.#defin
1240d 65 20 47 65 74 56 61 72 69 6e 74 28 41 2c 42 29  e GetVarint(A,B)
1240e 20 20 28 28 42 20 3d 20 2a 28 41 29 29 3c 3d 30    ((B = *(A))<=0
1240f 78 37 66 20 3f 20 31 20 3a 20 73 71 6c 69 74 65  x7f ? 1 : sqlite
12410 33 47 65 74 56 61 72 69 6e 74 33 32 28 41 2c 20  3GetVarint32(A, 
12411 26 42 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  &B))../*.** This
12412 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72   function compar
12413 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65  es the two table
12414 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72   rows or index r
12415 65 63 6f 72 64 73 20 73 70 65 63 69 66 69 65 64  ecords specified
12416 20 62 79 20 0a 2a 2a 20 7b 6e 4b 65 79 31 2c 20   by .** {nKey1, 
12417 70 4b 65 79 31 7d 20 61 6e 64 20 7b 6e 4b 65 79  pKey1} and {nKey
12418 32 2c 20 70 4b 65 79 32 7d 2c 20 72 65 74 75 72  2, pKey2}, retur
12419 6e 69 6e 67 20 61 20 6e 65 67 61 74 69 76 65 2c  ning a negative,
1241a 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73 69   zero.** or posi
1241b 74 69 76 65 20 69 6e 74 65 67 65 72 20 69 66 20  tive integer if 
1241c 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 69  {nKey1, pKey1} i
1241d 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75  s less than, equ
1241e 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65  al to or .** gre
1241f 61 74 65 72 20 74 68 61 6e 20 7b 6e 4b 65 79 32  ater than {nKey2
12420 2c 20 70 4b 65 79 32 7d 2e 20 20 42 6f 74 68 20  , pKey2}.  Both 
12421 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20 6d 75  Key1 and Key2 mu
12422 73 74 20 62 65 20 62 79 74 65 20 73 74 72 69 6e  st be byte strin
12423 67 73 0a 2a 2a 20 63 6f 6d 70 6f 73 65 64 20 62  gs.** composed b
12424 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63  y the OP_MakeRec
12425 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 68  ord opcode of th
12426 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54  e VDBE..*/.SQLIT
12427 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12428 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
12429 6f 6d 70 61 72 65 28 0a 20 20 76 6f 69 64 20 2a  ompare(.  void *
1242a 75 73 65 72 44 61 74 61 2c 0a 20 20 69 6e 74 20  userData,.  int 
1242b 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69  nKey1, const voi
1242c 64 20 2a 70 4b 65 79 31 2c 20 0a 20 20 69 6e 74  d *pKey1, .  int
1242d 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f   nKey2, const vo
1242e 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 4b  id *pKey2.){.  K
1242f 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
12430 20 3d 20 28 4b 65 79 49 6e 66 6f 2a 29 75 73 65   = (KeyInfo*)use
12431 72 44 61 74 61 3b 0a 20 20 75 33 32 20 64 31 2c  rData;.  u32 d1,
12432 20 64 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a   d2;          /*
12433 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65   Offset into aKe
12434 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 61  y[] of next data
12435 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33   element */.  u3
12436 32 20 69 64 78 31 2c 20 69 64 78 32 3b 20 20 20  2 idx1, idx2;   
12437 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
12438 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74  o aKey[] of next
12439 20 68 65 61 64 65 72 20 65 6c 65 6d 65 6e 74 20   header element 
1243a 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 31 2c  */.  u32 szHdr1,
1243b 20 73 7a 48 64 72 32 3b 20 20 2f 2a 20 4e 75 6d   szHdr2;  /* Num
1243c 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1243d 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20  header */.  int 
1243e 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69  i = 0;.  int nFi
1243f 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  eld;.  int rc = 
12440 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  0;.  const unsig
12441 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 31 20  ned char *aKey1 
12442 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  = (const unsigne
12443 64 20 63 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a  d char *)pKey1;.
12444 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
12445 20 63 68 61 72 20 2a 61 4b 65 79 32 20 3d 20 28   char *aKey2 = (
12446 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
12447 68 61 72 20 2a 29 70 4b 65 79 32 3b 0a 0a 20 20  har *)pKey2;..  
12448 4d 65 6d 20 6d 65 6d 31 3b 0a 20 20 4d 65 6d 20  Mem mem1;.  Mem 
12449 6d 65 6d 32 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63  mem2;.  mem1.enc
1244a 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63   = pKeyInfo->enc
1244b 3b 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b  ;.  mem1.db = pK
1244c 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65  eyInfo->db;.  me
1244d 6d 32 2e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66  m2.enc = pKeyInf
1244e 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 32 2e 64  o->enc;.  mem2.d
1244f 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62  b = pKeyInfo->db
12450 3b 0a 20 20 0a 20 20 69 64 78 31 20 3d 20 47 65  ;.  .  idx1 = Ge
12451 74 56 61 72 69 6e 74 28 61 4b 65 79 31 2c 20 73  tVarint(aKey1, s
12452 7a 48 64 72 31 29 3b 0a 20 20 64 31 20 3d 20 73  zHdr1);.  d1 = s
12453 7a 48 64 72 31 3b 0a 20 20 69 64 78 32 20 3d 20  zHdr1;.  idx2 = 
12454 47 65 74 56 61 72 69 6e 74 28 61 4b 65 79 32 2c  GetVarint(aKey2,
12455 20 73 7a 48 64 72 32 29 3b 0a 20 20 64 32 20 3d   szHdr2);.  d2 =
12456 20 73 7a 48 64 72 32 3b 0a 20 20 6e 46 69 65 6c   szHdr2;.  nFiel
12457 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  d = pKeyInfo->nF
12458 69 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28 20 69  ield;.  while( i
12459 64 78 31 3c 73 7a 48 64 72 31 20 26 26 20 69 64  dx1<szHdr1 && id
1245a 78 32 3c 73 7a 48 64 72 32 20 29 7b 0a 20 20 20  x2<szHdr2 ){.   
1245b 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
1245c 31 3b 0a 20 20 20 20 75 33 32 20 73 65 72 69 61  1;.    u32 seria
1245d 6c 5f 74 79 70 65 32 3b 0a 0a 20 20 20 20 2f 2a  l_type2;..    /*
1245e 20 52 65 61 64 20 74 68 65 20 73 65 72 69 61 6c   Read the serial
1245f 20 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 6e   types for the n
12460 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 65  ext element in e
12461 61 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20  ach key. */.    
12462 69 64 78 31 20 2b 3d 20 47 65 74 56 61 72 69 6e  idx1 += GetVarin
12463 74 28 20 61 4b 65 79 31 2b 69 64 78 31 2c 20 73  t( aKey1+idx1, s
12464 65 72 69 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20  erial_type1 );. 
12465 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31     if( d1>=nKey1
12466 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 53   && sqlite3VdbeS
12467 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72  erialTypeLen(ser
12468 69 61 6c 5f 74 79 70 65 31 29 3e 30 20 29 20 62  ial_type1)>0 ) b
12469 72 65 61 6b 3b 0a 20 20 20 20 69 64 78 32 20 2b  reak;.    idx2 +
1246a 3d 20 47 65 74 56 61 72 69 6e 74 28 20 61 4b 65  = GetVarint( aKe
1246b 79 32 2b 69 64 78 32 2c 20 73 65 72 69 61 6c 5f  y2+idx2, serial_
1246c 74 79 70 65 32 20 29 3b 0a 20 20 20 20 69 66 28  type2 );.    if(
1246d 20 64 32 3e 3d 6e 4b 65 79 32 20 26 26 20 73 71   d2>=nKey2 && sq
1246e 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1246f 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79  ypeLen(serial_ty
12470 70 65 32 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a  pe2)>0 ) break;.
12471 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20  .    /* Extract 
12472 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 62 65  the values to be
12473 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20 2a   compared..    *
12474 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71 6c 69  /.    d1 += sqli
12475 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74  te3VdbeSerialGet
12476 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65 72  (&aKey1[d1], ser
12477 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d 31  ial_type1, &mem1
12478 29 3b 0a 20 20 20 20 64 32 20 2b 3d 20 73 71 6c  );.    d2 += sql
12479 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
1247a 74 28 26 61 4b 65 79 32 5b 64 32 5d 2c 20 73 65  t(&aKey2[d2], se
1247b 72 69 61 6c 5f 74 79 70 65 32 2c 20 26 6d 65 6d  rial_type2, &mem
1247c 32 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74  2);..    /* Do t
1247d 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20  he comparison.  
1247e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
1247f 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
12480 26 6d 65 6d 31 2c 20 26 6d 65 6d 32 2c 20 69 3c  &mem1, &mem2, i<
12481 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79 49 6e 66  nField ? pKeyInf
12482 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29  o->aColl[i] : 0)
12483 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 31 2e 66  ;.    if( mem1.f
12484 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29  lags & MEM_Dyn )
12485 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
12486 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 20  elease(&mem1);. 
12487 20 20 20 69 66 28 20 6d 65 6d 32 2e 66 6c 61 67     if( mem2.flag
12488 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 20 73 71  s & MEM_Dyn ) sq
12489 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1248a 61 73 65 28 26 6d 65 6d 32 29 3b 0a 20 20 20 20  ase(&mem2);.    
1248b 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20  if( rc!=0 ){.   
1248c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1248d 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20      i++;.  }..  
1248e 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6b 65  /* One of the ke
1248f 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66 69  ys ran out of fi
12490 65 6c 64 73 2c 20 62 75 74 20 61 6c 6c 20 74 68  elds, but all th
12491 65 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 74  e fields up to t
12492 68 61 74 20 70 6f 69 6e 74 0a 20 20 2a 2a 20 77  hat point.  ** w
12493 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20 74 68  ere equal. If th
12494 65 20 69 6e 63 72 4b 65 79 20 66 6c 61 67 20 69  e incrKey flag i
12495 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65  s true, then the
12496 20 73 65 63 6f 6e 64 20 6b 65 79 20 69 73 0a 20   second key is. 
12497 20 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20 6c   ** treated as l
12498 61 72 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  arger..  */.  if
12499 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ( rc==0 ){.    i
1249a 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 69 6e 63  f( pKeyInfo->inc
1249b 72 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63  rKey ){.      rc
1249c 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65   = -1;.    }else
1249d 20 69 66 28 20 64 31 3c 6e 4b 65 79 31 20 29 7b   if( d1<nKey1 ){
1249e 0a 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  .      rc = 1;. 
1249f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 32 3c     }else if( d2<
124a0 6e 4b 65 79 32 20 29 7b 0a 20 20 20 20 20 20 72  nKey2 ){.      r
124a1 63 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20  c = -1;.    }.  
124a2 7d 65 6c 73 65 20 69 66 28 20 70 4b 65 79 49 6e  }else if( pKeyIn
124a3 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26  fo->aSortOrder &
124a4 26 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  & i<pKeyInfo->nF
124a5 69 65 6c 64 0a 20 20 20 20 20 20 20 20 20 20 20  ield.           
124a6 20 20 20 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d      && pKeyInfo-
124a7 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29  >aSortOrder[i] )
124a8 7b 0a 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a  {.    rc = -rc;.
124a9 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
124aa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  ;.}../*.** The a
124ab 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e  rgument is an in
124ac 64 65 78 20 65 6e 74 72 79 20 63 6f 6d 70 6f 73  dex entry compos
124ad 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f  ed using the OP_
124ae 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64  MakeRecord opcod
124af 65 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 65  e..** The last e
124b0 6e 74 72 79 20 69 6e 20 74 68 69 73 20 72 65 63  ntry in this rec
124b1 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 20 61 6e  ord should be an
124b2 20 69 6e 74 65 67 65 72 20 28 73 70 65 63 69 66   integer (specif
124b3 69 63 61 6c 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74  ically.** an int
124b4 65 67 65 72 20 72 6f 77 69 64 29 2e 20 20 54 68  eger rowid).  Th
124b5 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
124b6 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
124b7 20 62 79 74 65 73 20 69 6e 0a 2a 2a 20 74 68 61   bytes in.** tha
124b8 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51  t integer..*/.SQ
124b9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
124ba 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52   sqlite3VdbeIdxR
124bb 6f 77 69 64 4c 65 6e 28 63 6f 6e 73 74 20 75 38  owidLen(const u8
124bc 20 2a 61 4b 65 79 29 7b 0a 20 20 75 33 32 20 73   *aKey){.  u32 s
124bd 7a 48 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  zHdr;        /* 
124be 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64  Size of the head
124bf 65 72 20 2a 2f 0a 20 20 75 33 32 20 74 79 70 65  er */.  u32 type
124c0 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 53 65 72  Rowid;    /* Ser
124c1 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20  ial type of the 
124c2 72 6f 77 69 64 20 2a 2f 0a 0a 20 20 73 71 6c 69  rowid */..  sqli
124c3 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 61  te3GetVarint32(a
124c4 4b 65 79 2c 20 26 73 7a 48 64 72 29 3b 0a 20 20  Key, &szHdr);.  
124c5 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
124c6 33 32 28 26 61 4b 65 79 5b 73 7a 48 64 72 2d 31  32(&aKey[szHdr-1
124c7 5d 2c 20 26 74 79 70 65 52 6f 77 69 64 29 3b 0a  ], &typeRowid);.
124c8 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
124c9 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
124ca 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 7d 0a  n(typeRowid);.}.
124cb 20 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20 70    ../*.** pCur p
124cc 6f 69 6e 74 73 20 61 74 20 61 6e 20 69 6e 64 65  oints at an inde
124cd 78 20 65 6e 74 72 79 20 63 72 65 61 74 65 64 20  x entry created 
124ce 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 6b  using the OP_Mak
124cf 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 0a  eRecord opcode..
124d0 2a 2a 20 52 65 61 64 20 74 68 65 20 72 6f 77 69  ** Read the rowi
124d1 64 20 28 74 68 65 20 6c 61 73 74 20 66 69 65 6c  d (the last fiel
124d2 64 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 29  d in the record)
124d3 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e   and store it in
124d4 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74 75   *rowid..** Retu
124d5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
124d6 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73  everything works
124d7 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  , or an error co
124d8 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f  de otherwise..*/
124d9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
124da 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
124db 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72  dxRowid(BtCursor
124dc 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77   *pCur, i64 *row
124dd 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c  id){.  i64 nCell
124de 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  Key = 0;.  int r
124df 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20  c;.  u32 szHdr; 
124e0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
124e1 66 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  f the header */.
124e2 20 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b    u32 typeRowid;
124e3 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79      /* Serial ty
124e4 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20  pe of the rowid 
124e5 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69  */.  u32 lenRowi
124e6 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f  d;     /* Size o
124e7 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20  f the rowid */. 
124e8 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 73 71   Mem m, v;..  sq
124e9 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a  lite3BtreeKeySiz
124ea 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65  e(pCur, &nCellKe
124eb 79 29 3b 0a 20 20 69 66 28 20 6e 43 65 6c 6c 4b  y);.  if( nCellK
124ec 65 79 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ey<=0 ){.    ret
124ed 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
124ee 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72  PT_BKPT;.  }.  r
124ef 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
124f0 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 75 72  emFromBtree(pCur
124f1 2c 20 30 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 31  , 0, nCellKey, 1
124f2 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20  , &m);.  if( rc 
124f3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
124f4 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 47  ;.  }.  sqlite3G
124f5 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29  etVarint32((u8*)
124f6 6d 2e 7a 2c 20 26 73 7a 48 64 72 29 3b 0a 20 20  m.z, &szHdr);.  
124f7 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
124f8 33 32 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48  32((u8*)&m.z[szH
124f9 64 72 2d 31 5d 2c 20 26 74 79 70 65 52 6f 77 69  dr-1], &typeRowi
124fa 64 29 3b 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d  d);.  lenRowid =
124fb 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
124fc 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f  alTypeLen(typeRo
124fd 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  wid);.  sqlite3V
124fe 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38  dbeSerialGet((u8
124ff 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f  *)&m.z[m.n-lenRo
12500 77 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c  wid], typeRowid,
12501 20 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d   &v);.  *rowid =
12502 20 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65   v.u.i;.  sqlite
12503 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
12504 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  &m);.  return SQ
12505 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
12506 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 6b 65  * Compare the ke
12507 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65  y of the index e
12508 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f 72  ntry that cursor
12509 20 70 43 20 69 73 20 70 6f 69 6e 74 20 74 6f 20   pC is point to 
1250a 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 6b  against.** the k
1250b 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70 4b 65  ey string in pKe
1250c 79 20 28 6f 66 20 6c 65 6e 67 74 68 20 6e 4b 65  y (of length nKe
1250d 79 29 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20  y).  Write into 
1250e 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72 0a 2a  *pRes a number.*
1250f 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 74 69  * that is negati
12510 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73  ve, zero, or pos
12511 69 74 69 76 65 20 69 66 20 70 43 20 69 73 20 6c  itive if pC is l
12512 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20  ess than, equal 
12513 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65  to,.** or greate
12514 72 20 74 68 61 6e 20 70 4b 65 79 2e 20 20 52 65  r than pKey.  Re
12515 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
12516 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  n success..**.**
12517 20 70 4b 65 79 20 69 73 20 65 69 74 68 65 72 20   pKey is either 
12518 63 72 65 61 74 65 64 20 77 69 74 68 6f 75 74 20  created without 
12519 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20 74 72  a rowid or is tr
1251a 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61 74 20  uncated so that 
1251b 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 65 20  it.** omits the 
1251c 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e 64  rowid at the end
1251d 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61 74 20  .  The rowid at 
1251e 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
1251f 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20 69 73  ndex entry.** is
12520 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65 6c 6c   ignored as well
12521 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
12522 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
12523 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65  dbeIdxKeyCompare
12524 28 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 2c 20  (.  Cursor *pC, 
12525 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12526 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f  /* The cursor to
12527 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74   compare against
12528 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20   */.  int nKey, 
12529 63 6f 6e 73 74 20 75 38 20 2a 70 4b 65 79 2c 20  const u8 *pKey, 
1252a 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 74 6f 20    /* The key to 
1252b 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20 69 6e 74  compare */.  int
1252c 20 2a 72 65 73 20 20 20 20 20 20 20 20 20 20 20   *res           
1252d 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
1252e 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  e the comparison
1252f 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a   result here */.
12530 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65  ){.  i64 nCellKe
12531 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  y = 0;.  int rc;
12532 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
12533 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b  r = pC->pCursor;
12534 0a 20 20 69 6e 74 20 6c 65 6e 52 6f 77 69 64 3b  .  int lenRowid;
12535 0a 20 20 4d 65 6d 20 6d 3b 0a 0a 20 20 73 71 6c  .  Mem m;..  sql
12536 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
12537 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79  (pCur, &nCellKey
12538 29 3b 0a 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65  );.  if( nCellKe
12539 79 3c 3d 30 20 29 7b 0a 20 20 20 20 2a 72 65 73  y<=0 ){.    *res
1253a 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
1253b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1253c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1253d 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70  beMemFromBtree(p
1253e 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 6e  C->pCursor, 0, n
1253f 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b  CellKey, 1, &m);
12540 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
12541 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
12542 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c    lenRowid = sql
12543 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64  ite3VdbeIdxRowid
12544 4c 65 6e 28 28 75 38 2a 29 6d 2e 7a 29 3b 0a 20  Len((u8*)m.z);. 
12545 20 2a 72 65 73 20 3d 20 73 71 6c 69 74 65 33 56   *res = sqlite3V
12546 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
12547 28 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 6d  (pC->pKeyInfo, m
12548 2e 6e 2d 6c 65 6e 52 6f 77 69 64 2c 20 6d 2e 7a  .n-lenRowid, m.z
12549 2c 20 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20  , nKey, pKey);. 
1254a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1254b 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65  elease(&m);.  re
1254c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1254d 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
1254e 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 76  utine sets the v
1254f 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74 75 72  alue to be retur
12550 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65 6e  ned by subsequen
12551 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71  t calls to.** sq
12552 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20  lite3_changes() 
12553 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
12554 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f  handle 'db'. .*/
12555 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12556 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
12557 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74  SetChanges(sqlit
12558 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61  e3 *db, int nCha
12559 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  nge){.  assert( 
1255a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1255b 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
1255c 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d  .  db->nChange =
1255d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e   nChange;.  db->
1255e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20  nTotalChange += 
1255f 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  nChange;.}../*.*
12560 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 6e 20  * Set a flag in 
12561 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 64 61  the vdbe to upda
12562 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f  te the change co
12563 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 73  unter when it is
12564 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72   finalised.** or
12565 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54   reset..*/.SQLIT
12566 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
12567 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
12568 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76 29 7b  hanges(Vdbe *v){
12569 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f  .  v->changeCntO
1256a 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n = 1;.}../*.** 
1256b 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65 70 61  Mark every prepa
1256c 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 73  red statement as
1256d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
1256e 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1256f 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72 65  ion.** as expire
12570 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69  d..**.** An expi
12571 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 65  red statement me
12572 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d 70 69  ans that recompi
12573 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74  lation of the st
12574 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65  atement is.** re
12575 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d  commend.  Statem
12576 65 6e 74 73 20 65 78 70 69 72 65 20 77 68 65 6e  ents expire when
12577 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e 20 74   things happen t
12578 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a  hat make their.*
12579 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c  * programs obsol
1257a 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75  ete.  Removing u
1257b 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ser-defined func
1257c 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69  tions or collati
1257d 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c  ng.** sequences,
1257e 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 6e 20   or changing an 
1257f 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75  authorization fu
12580 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74  nction are the t
12581 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67  ypes of.** thing
12582 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 65 70  s that make prep
12583 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
12584 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c  obsolete..*/.SQL
12585 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
12586 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
12587 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
12588 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
12589 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28   Vdbe *p;.  for(
1258a 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20 70  p = db->pVdbe; p
1258b 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  ; p=p->pNext){. 
1258c 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20     p->expired = 
1258d 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
1258e 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 62  Return the datab
1258f 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ase associated w
12590 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f  ith the Vdbe..*/
12591 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12592 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33  sqlite3 *sqlite3
12593 56 64 62 65 44 62 28 56 64 62 65 20 2a 76 29 7b  VdbeDb(Vdbe *v){
12594 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b  .  return v->db;
12595 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
12596 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61  *** End of vdbea
12597 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ux.c ***********
12598 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12599 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1259a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1259b 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76  *** Begin file v
1259c 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a  dbeapi.c *******
1259d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1259e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1259f 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
125a0 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 26.**.** The 
125a1 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
125a2 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
125a3 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
125a4 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
125a5 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
125a6 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
125a7 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
125a8 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
125a9 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
125aa 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
125ab 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
125ac 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
125ad 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
125ae 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
125af 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
125b0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
125b1 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
125b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
125b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
125b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
125b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
125b6 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
125b7 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
125b8 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d  de use to implem
125b9 65 6e 74 20 41 50 49 73 20 74 68 61 74 20 61 72  ent APIs that ar
125ba 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a  e part of the.**
125bb 20 56 44 42 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   VDBE..*/../*.**
125bc 20 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e 6f   Return TRUE (no
125bd 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20 73  n-zero) of the s
125be 74 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69 65  tatement supplie
125bf 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
125c0 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20   needs.** to be 
125c1 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41 20 73  recompiled.  A s
125c2 74 61 74 65 6d 65 6e 74 20 6e 65 65 64 73 20 74  tatement needs t
125c3 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20  o be recompiled 
125c4 77 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20  whenever the.** 
125c5 65 78 65 63 75 74 69 6f 6e 20 65 6e 76 69 72 6f  execution enviro
125c6 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 69 6e  nment changes in
125c7 20 61 20 77 61 79 20 74 68 61 74 20 77 6f 75 6c   a way that woul
125c8 64 20 61 6c 74 65 72 20 74 68 65 20 70 72 6f 67  d alter the prog
125c9 72 61 6d 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69  ram.** that sqli
125ca 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 67 65  te3_prepare() ge
125cb 6e 65 72 61 74 65 73 2e 20 20 46 6f 72 20 65 78  nerates.  For ex
125cc 61 6d 70 6c 65 2c 20 69 66 20 6e 65 77 20 66 75  ample, if new fu
125cd 6e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f  nctions or.** co
125ce 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
125cf 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64  s are registered
125d0 20 6f 72 20 69 66 20 61 6e 20 61 75 74 68 6f 72   or if an author
125d1 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 69 73  izer function is
125d2 0a 2a 2a 20 61 64 64 65 64 20 6f 72 20 63 68 61  .** added or cha
125d3 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nged..*/.SQLITE_
125d4 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
125d5 65 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f  expired(sqlite3_
125d6 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
125d7 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
125d8 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e  )pStmt;.  return
125d9 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 78 70 69   p==0 || p->expi
125da 72 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  red;.}../*.** Th
125db 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
125dc 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 76  ine destroys a v
125dd 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 74  irtual machine t
125de 68 61 74 20 69 73 20 63 72 65 61 74 65 64 20 62  hat is created b
125df 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33  y.** the sqlite3
125e0 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75 74 69  _compile() routi
125e1 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65 72 20  ne. The integer 
125e2 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e 20 53  returned is an S
125e3 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63 65 73  QLITE_.** succes
125e4 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65 20 74  s/failure code t
125e5 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68  hat describes th
125e6 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 65 63  e result of exec
125e7 75 74 69 6e 67 20 74 68 65 20 76 69 72 74 75 61  uting the virtua
125e8 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a  l.** machine..**
125e9 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
125ea 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20   sets the error 
125eb 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20  code and string 
125ec 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
125ed 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
125ee 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
125ef 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
125f0 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51  rrmsg16()..*/.SQ
125f1 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
125f2 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71  ite3_finalize(sq
125f3 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
125f4 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
125f5 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a  if( pStmt==0 ){.
125f6 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
125f7 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
125f8 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65   Vdbe *v = (Vdbe
125f9 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c  *)pStmt;.    sql
125fa 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
125fb 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78  x = v->db->mutex
125fc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
125fd 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
125fe 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
125ff 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76  e3VdbeFinalize(v
12600 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
12601 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
12602 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
12603 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72  rc;.}../*.** Ter
12604 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65  minate the curre
12605 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  nt execution of 
12606 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
12607 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a   and reset it.**
12608 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61   back to its sta
12609 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74  rting state so t
1260a 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
1260b 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20  used. A success 
1260c 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  code from.** the
1260d 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e   prior execution
1260e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
1260f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
12610 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20   sets the error 
12611 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20  code and string 
12612 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
12613 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
12614 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
12615 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
12616 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51  rrmsg16()..*/.SQ
12617 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
12618 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74  ite3_reset(sqlit
12619 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1261a 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
1261b 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   pStmt==0 ){.   
1261c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1261d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64  .  }else{.    Vd
1261e 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70  be *v = (Vdbe*)p
1261f 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Stmt;.    sqlite
12620 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d  3_mutex_enter(v-
12621 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
12622 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
12623 65 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 73  eReset(v);.    s
12624 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65  qlite3VdbeMakeRe
12625 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c 20 30 2c  ady(v, -1, 0, 0,
12626 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   0);.    assert(
12627 20 28 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65   (rc & (v->db->e
12628 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a  rrMask))==rc );.
12629 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1262a 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d  x_leave(v->db->m
1262b 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
1262c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1262d 20 53 65 74 20 61 6c 6c 20 74 68 65 20 70 61 72   Set all the par
1262e 61 6d 65 74 65 72 73 20 69 6e 20 74 68 65 20 63  ameters in the c
1262f 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74  ompiled SQL stat
12630 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  ement to NULL..*
12631 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
12632 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62   sqlite3_clear_b
12633 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f  indings(sqlite3_
12634 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
12635 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
12636 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56  = SQLITE_OK;.  V
12637 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29  dbe *v = (Vdbe*)
12638 70 53 74 6d 74 3b 0a 20 20 73 71 6c 69 74 65 33  pStmt;.  sqlite3
12639 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e  _mutex_enter(v->
1263a 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66 6f  db->mutex);.  fo
1263b 72 28 69 3d 31 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=1; rc==SQLIT
1263c 45 5f 4f 4b 20 26 26 20 69 3c 3d 73 71 6c 69 74  E_OK && i<=sqlit
1263d 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
1263e 72 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 20  r_count(pStmt); 
1263f 69 2b 2b 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  i++){.    rc = s
12640 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
12641 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 7d 0a  (pStmt, i);.  }.
12642 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
12643 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74  leave(v->db->mut
12644 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
12645 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}.../*********
12646 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12647 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  *** sqlite3_valu
12648 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e_  ************
12649 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1264a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ***.** The follo
1264b 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 65 78  wing routines ex
1264c 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f  tract informatio
1264d 6e 20 66 72 6f 6d 20 61 20 4d 65 6d 20 6f 72 20  n from a Mem or 
1264e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 0a 2a 2a  sqlite3_value.**
1264f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53   structure..*/.S
12650 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
12651 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61  void *sqlite3_va
12652 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33  lue_blob(sqlite3
12653 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
12654 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29   Mem *p = (Mem*)
12655 70 56 61 6c 3b 0a 20 20 69 66 28 20 70 2d 3e 66  pVal;.  if( p->f
12656 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62  lags & (MEM_Blob
12657 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20  |MEM_Str) ){.   
12658 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45   sqlite3VdbeMemE
12659 78 70 61 6e 64 42 6c 6f 62 28 70 29 3b 0a 20 20  xpandBlob(p);.  
1265a 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d    p->flags &= ~M
1265b 45 4d 5f 53 74 72 3b 0a 20 20 20 20 70 2d 3e 66  EM_Str;.    p->f
1265c 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62  lags |= MEM_Blob
1265d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e  ;.    return p->
1265e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  z;.  }else{.    
1265f 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76  return sqlite3_v
12660 61 6c 75 65 5f 74 65 78 74 28 70 56 61 6c 29 3b  alue_text(pVal);
12661 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  .  }.}.SQLITE_AP
12662 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
12663 6c 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65  lue_bytes(sqlite
12664 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
12665 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
12666 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c  ValueBytes(pVal,
12667 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d   SQLITE_UTF8);.}
12668 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
12669 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
1266a 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61  tes16(sqlite3_va
1266b 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65  lue *pVal){.  re
1266c 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75  turn sqlite3Valu
1266d 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c  eBytes(pVal, SQL
1266e 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29  ITE_UTF16NATIVE)
1266f 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 64  ;.}.SQLITE_API d
12670 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 61  ouble sqlite3_va
12671 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74  lue_double(sqlit
12672 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
12673 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
12674 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 28  3VdbeRealValue((
12675 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51  Mem*)pVal);.}.SQ
12676 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
12677 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73  ite3_value_int(s
12678 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
12679 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
1267a 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1267b 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d  e((Mem*)pVal);.}
1267c 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1267d 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  te_int64 sqlite3
1267e 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c  _value_int64(sql
1267f 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
12680 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
12681 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
12682 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53  (Mem*)pVal);.}.S
12683 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
12684 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73  unsigned char *s
12685 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
12686 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t(sqlite3_value 
12687 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
12688 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
12689 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 56   char *)sqlite3V
1268a 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53  alueText(pVal, S
1268b 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23  QLITE_UTF8);.}.#
1268c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1268d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1268e 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1268f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
12690 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c  xt16(sqlite3_val
12691 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue* pVal){.  ret
12692 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
12693 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
12694 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a  E_UTF16NATIVE);.
12695 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  }.SQLITE_API con
12696 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
12697 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28  _value_text16be(
12698 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
12699 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
1269a 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
1269b 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
1269c 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  16BE);.}.SQLITE_
1269d 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1269e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1269f 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76  xt16le(sqlite3_v
126a0 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
126a1 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c  eturn sqlite3Val
126a2 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
126a3 49 54 45 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a  ITE_UTF16LE);.}.
126a4 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
126a5 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53  _OMIT_UTF16 */.S
126a6 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
126a7 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
126a8 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20  (sqlite3_value* 
126a9 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
126aa 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f  pVal->type;.}../
126ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
126ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c  ************ sql
126ad 69 74 65 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a  ite3_result_  **
126ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
126af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
126b0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
126b1 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
126b2 20 62 79 20 75 73 65 72 2d 64 65 66 69 6e 65 64   by user-defined
126b3 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70   functions to sp
126b4 65 63 69 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e  ecify.** the fun
126b5 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2f  ction result..*/
126b6 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
126b7 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
126b8 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f  blob(.  sqlite3_
126b9 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a  context *pCtx, .
126ba 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c    const void *z,
126bb 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f   .  int n, .  vo
126bc 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20  id (*xDel)(void 
126bd 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  *).){.  assert( 
126be 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  n>=0 );.  assert
126bf 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
126c0 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
126c1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
126c2 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
126c3 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e  r(&pCtx->s, z, n
126c4 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51  , 0, xDel);.}.SQ
126c5 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
126c6 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
126c7 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ble(sqlite3_cont
126c8 65 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c  ext *pCtx, doubl
126c9 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65 72  e rVal){.  asser
126ca 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
126cb 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
126cc 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
126cd 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
126ce 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20  ouble(&pCtx->s, 
126cf 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rVal);.}.SQLITE_
126d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
126d1 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71  _result_error(sq
126d2 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
126d3 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Ctx, const char 
126d4 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73  *z, int n){.  as
126d5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
126d6 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
126d7 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
126d8 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
126d9 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   1;.  sqlite3Vdb
126da 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
126db 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  ->s, z, n, SQLIT
126dc 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54  E_UTF8, SQLITE_T
126dd 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66  RANSIENT);.}.#if
126de 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
126df 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
126e0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
126e1 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71  esult_error16(sq
126e2 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
126e3 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  Ctx, const void 
126e4 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73  *z, int n){.  as
126e5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
126e6 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
126e7 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
126e8 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
126e9 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   1;.  sqlite3Vdb
126ea 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
126eb 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  ->s, z, n, SQLIT
126ec 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53  E_UTF16NATIVE, S
126ed 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
126ee 3b 0a 7d 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  ;.}.#endif.SQLIT
126ef 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
126f0 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 71  e3_result_int(sq
126f1 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
126f2 43 74 78 2c 20 69 6e 74 20 69 56 61 6c 29 7b 0a  Ctx, int iVal){.
126f3 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
126f4 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
126f5 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
126f6 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
126f7 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43 74  MemSetInt64(&pCt
126f8 78 2d 3e 73 2c 20 28 69 36 34 29 69 56 61 6c 29  x->s, (i64)iVal)
126f9 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
126fa 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
126fb 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33  lt_int64(sqlite3
126fc 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
126fd 69 36 34 20 69 56 61 6c 29 7b 0a 20 20 61 73 73  i64 iVal){.  ass
126fe 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
126ff 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
12700 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
12701 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
12702 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c  tInt64(&pCtx->s,
12703 20 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45   iVal);.}.SQLITE
12704 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
12705 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71  3_result_null(sq
12706 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
12707 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Ctx){.  assert( 
12708 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
12709 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1270a 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1270b 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
1270c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 7d 0a 53 51  (&pCtx->s);.}.SQ
1270d 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1270e 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1270f 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  t(.  sqlite3_con
12710 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63  text *pCtx, .  c
12711 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 0a 20  onst char *z, . 
12712 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69 64 20 28   int n,.  void (
12713 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29  *xDel)(void *).)
12714 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
12715 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
12716 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
12717 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
12718 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74  beMemSetStr(&pCt
12719 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49  x->s, z, n, SQLI
1271a 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a  TE_UTF8, xDel);.
1271b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1271c 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1271d 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1271e 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
1271f 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  6(.  sqlite3_con
12720 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63  text *pCtx, .  c
12721 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20  onst void *z, . 
12722 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20   int n, .  void 
12723 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a  (*xDel)(void *).
12724 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
12725 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
12726 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
12727 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
12728 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43  dbeMemSetStr(&pC
12729 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c  tx->s, z, n, SQL
1272a 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c  ITE_UTF16NATIVE,
1272b 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45   xDel);.}.SQLITE
1272c 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1272d 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  3_result_text16b
1272e 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  e(.  sqlite3_con
1272f 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63  text *pCtx, .  c
12730 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20  onst void *z, . 
12731 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20   int n, .  void 
12732 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a  (*xDel)(void *).
12733 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
12734 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
12735 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
12736 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
12737 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43  dbeMemSetStr(&pC
12738 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c  tx->s, z, n, SQL
12739 49 54 45 5f 55 54 46 31 36 42 45 2c 20 78 44 65  ITE_UTF16BE, xDe
1273a 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
1273b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1273c 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 0a 20  sult_text16le(. 
1273d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1273e 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74   *pCtx, .  const
1273f 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74   void *z, .  int
12740 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44   n, .  void (*xD
12741 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20  el)(void *).){. 
12742 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
12743 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
12744 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
12745 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
12746 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
12747 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  s, z, n, SQLITE_
12748 55 54 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a  UTF16LE, xDel);.
12749 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1274a 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1274b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1274c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1274d 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f  value(sqlite3_co
1274e 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 73 71 6c  ntext *pCtx, sql
1274f 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
12750 75 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ue){.  assert( s
12751 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
12752 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
12753 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
12754 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 43  3VdbeMemCopy(&pC
12755 74 78 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a  tx->s, pValue);.
12756 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
12757 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
12758 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65  _zeroblob(sqlite
12759 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
1275a 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72   int n){.  asser
1275b 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1275c 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1275d 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1275e 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
1275f 65 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73  eroBlob(&pCtx->s
12760 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 63  , n);.}../* Forc
12761 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42  e an SQLITE_TOOB
12762 49 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c  IG error. */.SQL
12763 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
12764 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
12765 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33  r_toobig(sqlite3
12766 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b  _context *pCtx){
12767 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
12768 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
12769 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1276a 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1276b 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28  eMemSetZeroBlob(
1276c 26 70 43 74 78 2d 3e 73 2c 20 53 51 4c 49 54 45  &pCtx->s, SQLITE
1276d 5f 4d 41 58 5f 4c 45 4e 47 54 48 2b 31 29 3b 0a  _MAX_LENGTH+1);.
1276e 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f  }../* An SQLITE_
1276f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a  NOMEM error. */.
12770 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
12771 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
12772 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74  rror_nomem(sqlit
12773 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
12774 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
12775 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
12776 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
12777 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
12778 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70  dbeMemSetNull(&p
12779 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d  Ctx->s);.  pCtx-
1277a 3e 69 73 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20  >isError = 1;.  
1277b 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c  pCtx->s.db->mall
1277c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a  ocFailed = 1;.}.
1277d 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74  ./*.** Execute t
1277e 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74  he statement pSt
1277f 6d 74 2c 20 65 69 74 68 65 72 20 75 6e 74 69 6c  mt, either until
12780 20 61 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69   a row of data i
12781 73 20 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20  s ready, the.** 
12782 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d  statement is com
12783 70 6c 65 74 65 6c 79 20 65 78 65 63 75 74 65 64  pletely executed
12784 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   or an error occ
12785 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  urs..**.** This 
12786 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e  routine implemen
12787 74 73 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 74  ts the bulk of t
12788 68 65 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 20  he logic behind 
12789 74 68 65 20 73 71 6c 69 74 65 5f 73 74 65 70 28  the sqlite_step(
1278a 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f  ).** API.  The o
1278b 6e 6c 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65  nly thing omitte
1278c 64 20 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74  d is the automat
1278d 69 63 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20  ic recompile if 
1278e 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61  a .** schema cha
1278f 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64  nge has occurred
12790 2e 20 20 54 68 61 74 20 64 65 74 61 69 6c 20 69  .  That detail i
12791 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65  s handled by the
12792 0a 2a 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65  .** outer sqlite
12793 33 5f 73 74 65 70 28 29 20 77 72 61 70 70 65 72  3_step() wrapper
12794 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73   procedure..*/.s
12795 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
12796 33 53 74 65 70 28 56 64 62 65 20 2a 70 29 7b 0a  3Step(Vdbe *p){.
12797 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
12798 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 41   int rc;..  /* A
12799 73 73 65 72 74 20 74 68 61 74 20 6d 61 6c 6c 6f  ssert that mallo
1279a 63 28 29 20 68 61 73 20 6e 6f 74 20 66 61 69 6c  c() has not fail
1279b 65 64 20 2a 2f 0a 20 20 64 62 20 3d 20 70 2d 3e  ed */.  db = p->
1279c 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64  db;.  assert( !d
1279d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1279e 29 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c  );..  if( p==0 |
1279f 7c 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45  | p->magic!=VDBE
127a0 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20  _MAGIC_RUN ){.  
127a1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
127a2 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66  MISUSE;.  }.  if
127a3 28 20 70 2d 3e 61 62 6f 72 74 65 64 20 29 7b 0a  ( p->aborted ){.
127a4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
127a5 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 20 20 69  E_ABORT;.  }.  i
127a6 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26 26 20 70  f( p->pc<=0 && p
127a7 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20  ->expired ){.   
127a8 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
127a9 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
127aa 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43  ->rc = SQLITE_SC
127ab 48 45 4d 41 3b 0a 20 20 20 20 7d 0a 20 20 20 20  HEMA;.    }.    
127ac 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
127ad 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  R;.    goto end_
127ae 6f 66 5f 73 74 65 70 3b 0a 20 20 7d 0a 20 20 69  of_step;.  }.  i
127af 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
127b0 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 70 2d  On(db) ){.    p-
127b1 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53  >rc = SQLITE_MIS
127b2 55 53 45 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  USE;.    return 
127b3 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
127b4 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 30   }.  if( p->pc<0
127b5 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
127b6 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72  ere are no other
127b7 20 73 74 61 74 65 6d 65 6e 74 73 20 63 75 72 72   statements curr
127b8 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20 74  ently running, t
127b9 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65 74  hen.    ** reset
127ba 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 20 66   the interrupt f
127bb 6c 61 67 2e 20 20 54 68 69 73 20 70 72 65 76 65  lag.  This preve
127bc 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  nts a call to sq
127bd 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 0a  lite3_interrupt.
127be 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74 65      ** from inte
127bf 72 72 75 70 74 69 6e 67 20 61 20 73 74 61 74 65  rrupting a state
127c0 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 6e 6f  ment that has no
127c1 74 20 79 65 74 20 73 74 61 72 74 65 64 2e 0a 20  t yet started.. 
127c2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62     */.    if( db
127c3 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d  ->activeVdbeCnt=
127c4 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  =0 ){.      db->
127c5 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
127c6 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 23 69 66   = 0;.    }..#if
127c7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
127c8 5f 54 52 41 43 45 0a 20 20 20 20 2f 2a 20 49 6e  _TRACE.    /* In
127c9 76 6f 6b 65 20 74 68 65 20 74 72 61 63 65 20 63  voke the trace c
127ca 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 65  allback if there
127cb 20 69 73 20 6f 6e 65 0a 20 20 20 20 2a 2f 0a 20   is one.    */. 
127cc 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61 63     if( db->xTrac
127cd 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e 62  e && !db->init.b
127ce 75 73 79 20 29 7b 0a 20 20 20 20 20 20 61 73 73  usy ){.      ass
127cf 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b  ert( p->nOp>0 );
127d0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
127d1 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e  ->aOp[p->nOp-1].
127d2 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 6f 6f 70 20  opcode==OP_Noop 
127d3 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
127d4 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31   p->aOp[p->nOp-1
127d5 5d 2e 70 33 21 3d 30 20 29 3b 0a 20 20 20 20 20  ].p3!=0 );.     
127d6 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 5b   assert( p->aOp[
127d7 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 33 74 79 70 65  p->nOp-1].p3type
127d8 3d 3d 50 33 5f 44 59 4e 41 4d 49 43 20 29 3b 0a  ==P3_DYNAMIC );.
127d9 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 61 66        sqlite3Saf
127da 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
127db 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62 2d    db->xTrace(db-
127dc 3e 70 54 72 61 63 65 41 72 67 2c 20 70 2d 3e 61  >pTraceArg, p->a
127dd 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 33 29  Op[p->nOp-1].p3)
127de 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
127df 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
127e0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63  ){.        p->rc
127e1 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45   = SQLITE_MISUSE
127e2 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
127e3 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
127e4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
127e5 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 66 69    if( db->xProfi
127e6 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e  le && !db->init.
127e7 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 64 6f  busy ){.      do
127e8 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 20  uble rNow;.     
127e9 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
127ea 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20  tTime(db->pVfs, 
127eb 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20 20 70 2d  &rNow);.      p-
127ec 3e 73 74 61 72 74 54 69 6d 65 20 3d 20 28 72 4e  >startTime = (rN
127ed 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a  ow - (int)rNow)*
127ee 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30  3600.0*24.0*1000
127ef 30 30 30 30 30 30 2e 30 3b 0a 20 20 20 20 7d 0a  000000.0;.    }.
127f0 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 50  #endif..    /* P
127f1 72 69 6e 74 20 61 20 63 6f 70 79 20 6f 66 20 53  rint a copy of S
127f2 51 4c 20 61 73 20 69 74 20 69 73 20 65 78 65 63  QL as it is exec
127f3 75 74 65 64 20 69 66 20 74 68 65 20 53 51 4c 5f  uted if the SQL_
127f4 54 52 41 43 45 20 70 72 61 67 6d 61 20 69 73 20  TRACE pragma is 
127f5 74 75 72 6e 65 64 0a 20 20 20 20 2a 2a 20 6f 6e  turned.    ** on
127f6 20 69 6e 20 64 65 62 75 67 67 69 6e 67 20 6d 6f   in debugging mo
127f7 64 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65  de..    */.#ifde
127f8 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
127f9 20 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67     if( (db->flag
127fa 73 20 26 20 53 51 4c 49 54 45 5f 53 71 6c 54 72  s & SQLITE_SqlTr
127fb 61 63 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ace)!=0 ){.     
127fc 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
127fd 6e 74 66 28 22 53 51 4c 2d 74 72 61 63 65 3a 20  ntf("SQL-trace: 
127fe 25 73 5c 6e 22 2c 20 70 2d 3e 61 4f 70 5b 70 2d  %s\n", p->aOp[p-
127ff 3e 6e 4f 70 2d 31 5d 2e 70 33 29 3b 0a 20 20 20  >nOp-1].p3);.   
12800 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
12801 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 20 20  ITE_DEBUG */..  
12802 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65    db->activeVdbe
12803 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70 63  Cnt++;.    p->pc
12804 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65   = 0;.  }.#ifnde
12805 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  f SQLITE_OMIT_EX
12806 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65  PLAIN.  if( p->e
12807 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 63  xplain ){.    rc
12808 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69   = sqlite3VdbeLi
12809 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  st(p);.  }else.#
1280a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1280b 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a  OMIT_EXPLAIN */.
1280c 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    {.    rc = sql
1280d 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29 3b  ite3VdbeExec(p);
1280e 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69  .  }..  if( sqli
1280f 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
12810 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
12811 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
12812 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
12813 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20  OMIT_TRACE.  /* 
12814 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 69  Invoke the profi
12815 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74  le callback if t
12816 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f  here is one.  */
12817 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
12818 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 72  E_ROW && db->xPr
12819 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e  ofile && !db->in
1281a 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 64  it.busy ){.    d
1281b 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20  ouble rNow;.    
1281c 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65 3b 0a  u64 elapseTime;.
1281d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75  .    sqlite3OsCu
1281e 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56  rrentTime(db->pV
1281f 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20  fs, &rNow);.    
12820 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28 72 4e  elapseTime = (rN
12821 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a  ow - (int)rNow)*
12822 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30  3600.0*24.0*1000
12823 30 30 30 30 30 30 2e 30 20 2d 20 70 2d 3e 73 74  000000.0 - p->st
12824 61 72 74 54 69 6d 65 3b 0a 20 20 20 20 61 73 73  artTime;.    ass
12825 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b  ert( p->nOp>0 );
12826 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
12827 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 6f 70  aOp[p->nOp-1].op
12828 63 6f 64 65 3d 3d 4f 50 5f 4e 6f 6f 70 20 29 3b  code==OP_Noop );
12829 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1282a 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 33  aOp[p->nOp-1].p3
1282b 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  !=0 );.    asser
1282c 74 28 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70  t( p->aOp[p->nOp
1282d 2d 31 5d 2e 70 33 74 79 70 65 3d 3d 50 33 5f 44  -1].p3type==P3_D
1282e 59 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20 64 62  YNAMIC );.    db
1282f 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e 70  ->xProfile(db->p
12830 50 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e 61  ProfileArg, p->a
12831 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 33 2c  Op[p->nOp-1].p3,
12832 20 65 6c 61 70 73 65 54 69 6d 65 29 3b 0a 20 20   elapseTime);.  
12833 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69  }.#endif..  sqli
12834 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20  te3Error(p->db, 
12835 72 63 2c 20 30 29 3b 0a 20 20 70 2d 3e 72 63 20  rc, 0);.  p->rc 
12836 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
12837 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a  (p->db, p->rc);.
12838 65 6e 64 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 61  end_of_step:.  a
12839 73 73 65 72 74 28 20 28 72 63 26 30 78 66 66 29  ssert( (rc&0xff)
1283a 3d 3d 72 63 20 29 3b 0a 20 20 69 66 28 20 70 2d  ==rc );.  if( p-
1283b 3e 7a 53 71 6c 20 26 26 20 28 72 63 26 30 78 66  >zSql && (rc&0xf
1283c 66 29 3c 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  f)<SQLITE_ROW ){
1283d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 65 68  .    /* This beh
1283e 61 76 69 6f 72 20 6f 63 63 75 72 73 20 69 66 20  avior occurs if 
1283f 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
12840 76 32 28 29 20 77 61 73 20 75 73 65 64 20 74 6f  v2() was used to
12841 20 62 75 69 6c 64 0a 20 20 20 20 2a 2a 20 74 68   build.    ** th
12842 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
12843 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 65 72  ment.  Return er
12844 72 6f 72 20 63 6f 64 65 73 20 64 69 72 65 63 74  ror codes direct
12845 6c 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  ly */.    sqlite
12846 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 70 2d  3Error(p->db, p-
12847 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 72 65 74  >rc, 0);.    ret
12848 75 72 6e 20 70 2d 3e 72 63 3b 0a 20 20 7d 65 6c  urn p->rc;.  }el
12849 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  se{.    /* This 
1284a 69 73 20 66 6f 72 20 6c 65 67 61 63 79 20 73 71  is for legacy sq
1284b 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20  lite3_prepare() 
1284c 62 75 69 6c 64 73 20 61 6e 64 20 77 68 65 6e 20  builds and when 
1284d 74 68 65 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20  the code.    ** 
1284e 69 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 6f 72  is SQLITE_ROW or
1284f 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 2a 2f 0a   SQLITE_DONE */.
12850 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
12851 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
12852 20 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65   is the top-leve
12853 6c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  l implementation
12854 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70   of sqlite3_step
12855 28 29 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c  ().  Call.** sql
12856 69 74 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f  ite3Step() to do
12857 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72   most of the wor
12858 6b 2e 20 20 49 66 20 61 20 73 63 68 65 6d 61 20  k.  If a schema 
12859 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a  error occurs,.**
1285a 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70   call sqlite3Rep
1285b 72 65 70 61 72 65 28 29 20 61 6e 64 20 74 72 79  repare() and try
1285c 20 61 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65   again..*/.#ifde
1285d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  f SQLITE_OMIT_PA
1285e 52 53 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20  RSER.SQLITE_API 
1285f 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70  int sqlite3_step
12860 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
12861 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Stmt){.  int rc;
12862 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 76 20  .  Vdbe *v;.  v 
12863 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a  = (Vdbe*)pStmt;.
12864 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
12865 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75 74  enter(v->db->mut
12866 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ex);.  rc = sqli
12867 74 65 33 53 74 65 70 28 76 29 3b 0a 20 20 73 71  te3Step(v);.  sq
12868 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
12869 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(v->db->mutex);
1286a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1286b 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 41 50 49  #else.SQLITE_API
1286c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65   int sqlite3_ste
1286d 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  p(sqlite3_stmt *
1286e 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 63 6e  pStmt){.  int cn
1286f 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  t = 0;.  int rc;
12870 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64  .  Vdbe *v = (Vd
12871 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 73 71 6c  be*)pStmt;.  sql
12872 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
12873 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (v->db->mutex);.
12874 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73    while( (rc = s
12875 71 6c 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d  qlite3Step(v))==
12876 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20  SQLITE_SCHEMA.  
12877 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20         && cnt++ 
12878 3c 20 35 0a 20 20 20 20 20 20 20 20 20 26 26 20  < 5.         && 
12879 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65  sqlite3Reprepare
1287a 28 76 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  (v) ){.    sqlit
1287b 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b  e3_reset(pStmt);
1287c 0a 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20  .    v->expired 
1287d 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
1287e 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 76  e3_mutex_leave(v
1287f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
12880 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
12881 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61  dif../*.** Extra
12882 63 74 20 74 68 65 20 75 73 65 72 20 64 61 74 61  ct the user data
12883 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f   from a sqlite3_
12884 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72  context structur
12885 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a  e and return a.*
12886 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  * pointer to it.
12887 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
12888 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 65  oid *sqlite3_use
12889 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63  r_data(sqlite3_c
1288a 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 73  ontext *p){.  as
1288b 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46  sert( p && p->pF
1288c 75 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  unc );.  return 
1288d 70 2d 3e 70 46 75 6e 63 2d 3e 70 55 73 65 72 44  p->pFunc->pUserD
1288e 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ata;.}../*.** Th
1288f 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74  e following is t
12890 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
12891 6e 20 6f 66 20 61 6e 20 53 51 4c 20 66 75 6e 63  n of an SQL func
12892 74 69 6f 6e 20 74 68 61 74 20 61 6c 77 61 79 73  tion that always
12893 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61  .** fails with a
12894 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
12895 73 74 61 74 69 6e 67 20 74 68 61 74 20 74 68 65  stating that the
12896 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
12897 64 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 6f 6e  d in the.** wron
12898 67 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20  g context.  The 
12899 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
1289a 5f 66 75 6e 63 74 69 6f 6e 28 29 20 41 50 49 20  _function() API 
1289b 6d 69 67 68 74 20 63 6f 6e 73 74 72 75 63 74 0a  might construct.
1289c 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ** SQL function 
1289d 74 68 61 74 20 75 73 65 20 74 68 69 73 20 72 6f  that use this ro
1289e 75 74 69 6e 65 20 73 6f 20 74 68 61 74 20 74 68  utine so that th
1289f 65 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 6c 6c  e functions will
128a0 20 65 78 69 73 74 0a 2a 2a 20 66 6f 72 20 6e 61   exist.** for na
128a1 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 62 75  me resolution bu
128a2 74 20 61 72 65 20 61 63 74 75 61 6c 6c 79 20 6f  t are actually o
128a3 76 65 72 6c 6f 61 64 65 64 20 62 79 20 74 68 65  verloaded by the
128a4 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 0a 2a   xFindFunction.*
128a5 2a 20 6d 65 74 68 6f 64 20 6f 66 20 76 69 72 74  * method of virt
128a6 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53  ual tables..*/.S
128a7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
128a8 69 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69  id sqlite3Invali
128a9 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c  dFunction(.  sql
128aa 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
128ab 6e 74 65 78 74 2c 20 20 2f 2a 20 54 68 65 20 66  ntext,  /* The f
128ac 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 20  unction calling 
128ad 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
128ae 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20   argc,          
128af 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
128b0 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
128b1 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
128b2 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  /.  sqlite3_valu
128b3 65 20 2a 2a 61 72 67 76 20 20 20 20 20 20 20 2f  e **argv       /
128b4 2a 20 56 61 6c 75 65 20 6f 66 20 65 61 63 68 20  * Value of each 
128b5 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20  argument */.){. 
128b6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
128b7 6d 65 20 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46  me = context->pF
128b8 75 6e 63 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 68  unc->zName;.  ch
128b9 61 72 20 2a 7a 45 72 72 3b 0a 20 20 7a 45 72 72  ar *zErr;.  zErr
128ba 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
128bb 66 28 30 2c 0a 20 20 20 20 20 20 22 75 6e 61 62  f(0,.      "unab
128bc 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69  le to use functi
128bd 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71  on %s in the req
128be 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c  uested context",
128bf 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74   zName);.  sqlit
128c0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
128c1 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
128c2 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  1);.  sqlite3_fr
128c3 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a  ee(zErr);.}../*.
128c4 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72  ** Allocate or r
128c5 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67  eturn the aggreg
128c6 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ate context for 
128c7 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e  a user function.
128c8 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65    A new.** conte
128c9 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  xt is allocated 
128ca 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  on the first cal
128cb 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63  l.  Subsequent c
128cc 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a  alls return the.
128cd 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20  ** same context 
128ce 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65  that was returne
128cf 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73  d on prior calls
128d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
128d1 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67  void *sqlite3_ag
128d2 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
128d3 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
128d4 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a  *p, int nByte){.
128d5 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61    Mem *pMem;.  a
128d6 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70  ssert( p && p->p
128d7 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63  Func && p->pFunc
128d8 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73  ->xStep );.  ass
128d9 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
128da 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d  ex_held(p->s.db-
128db 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65  >mutex) );.  pMe
128dc 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69  m = p->pMem;.  i
128dd 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  f( (pMem->flags 
128de 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b  & MEM_Agg)==0 ){
128df 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d  .    if( nByte==
128e0 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
128e1 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3d 3d  t( pMem->flags==
128e2 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  MEM_Null );.    
128e3 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20    pMem->z = 0;. 
128e4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
128e5 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
128e6 4d 5f 41 67 67 3b 0a 20 20 20 20 20 20 70 4d 65  M_Agg;.      pMe
128e7 6d 2d 3e 78 44 65 6c 20 3d 20 73 71 6c 69 74 65  m->xDel = sqlite
128e8 33 5f 66 72 65 65 3b 0a 20 20 20 20 20 20 70 4d  3_free;.      pM
128e9 65 6d 2d 3e 75 2e 70 44 65 66 20 3d 20 70 2d 3e  em->u.pDef = p->
128ea 70 46 75 6e 63 3b 0a 20 20 20 20 20 20 69 66 28  pFunc;.      if(
128eb 20 6e 42 79 74 65 3c 3d 4e 42 46 53 20 29 7b 0a   nByte<=NBFS ){.
128ec 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20          pMem->z 
128ed 3d 20 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74 3b 0a  = pMem->zShort;.
128ee 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
128ef 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 65  Mem->z, 0, nByte
128f0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
128f1 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20          pMem->z 
128f2 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
128f3 63 5a 65 72 6f 28 70 2d 3e 73 2e 64 62 2c 20 6e  cZero(p->s.db, n
128f4 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Byte);.      }. 
128f5 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
128f6 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a  n (void*)pMem->z
128f7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
128f8 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79 20 64  n the auxilary d
128f9 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69 66 20  ata pointer, if 
128fa 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69 41 72  any, for the iAr
128fb 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  g'th argument to
128fc 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66 75 6e  .** the user-fun
128fd 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79  ction defined by
128fe 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45   pCtx..*/.SQLITE
128ff 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
12900 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 73  e3_get_auxdata(s
12901 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
12902 70 43 74 78 2c 20 69 6e 74 20 69 41 72 67 29 7b  pCtx, int iArg){
12903 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64  .  VdbeFunc *pVd
12904 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 73 65 72  beFunc;..  asser
12905 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
12906 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
12907 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 56  ->mutex) );.  pV
12908 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e  dbeFunc = pCtx->
12909 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 28  pVdbeFunc;.  if(
1290a 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 69   !pVdbeFunc || i
1290b 41 72 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d 3e  Arg>=pVdbeFunc->
1290c 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c 30 20 29  nAux || iArg<0 )
1290d 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
1290e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 56 64    }.  return pVd
1290f 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41  beFunc->apAux[iA
12910 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a  rg].pAux;.}../*.
12911 2a 2a 20 53 65 74 20 74 68 65 20 61 75 78 69 6c  ** Set the auxil
12912 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72  ary data pointer
12913 20 61 6e 64 20 64 65 6c 65 74 65 20 66 75 6e 63   and delete func
12914 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 20 69 41  tion, for the iA
12915 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e  rg'th.** argumen
12916 74 20 74 6f 20 74 68 65 20 75 73 65 72 2d 66 75  t to the user-fu
12917 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62  nction defined b
12918 79 20 70 43 74 78 2e 20 41 6e 79 20 70 72 65 76  y pCtx. Any prev
12919 69 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a  ious value is.**
1291a 20 64 65 6c 65 74 65 64 20 62 79 20 63 61 6c 6c   deleted by call
1291b 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 20 66  ing the delete f
1291c 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65  unction specifie
1291d 64 20 77 68 65 6e 20 69 74 20 77 61 73 20 73 65  d when it was se
1291e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
1291f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65   void sqlite3_se
12920 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73 71 6c  t_auxdata(.  sql
12921 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
12922 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 72 67 2c  tx, .  int iArg,
12923 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20   .  void *pAux, 
12924 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74  .  void (*xDelet
12925 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73  e)(void*).){.  s
12926 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a 70  truct AuxData *p
12927 41 75 78 44 61 74 61 3b 0a 20 20 56 64 62 65 46  AuxData;.  VdbeF
12928 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a  unc *pVdbeFunc;.
12929 20 20 69 66 28 20 69 41 72 67 3c 30 20 29 20 67    if( iArg<0 ) g
1292a 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 20 61  oto failed;..  a
1292b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1292c 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1292d 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1292e 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43    pVdbeFunc = pC
1292f 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20  tx->pVdbeFunc;. 
12930 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20   if( !pVdbeFunc 
12931 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41  || pVdbeFunc->nA
12932 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 20 20 20  ux<=iArg ){.    
12933 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 56 64 62  int nAux = (pVdb
12934 65 46 75 6e 63 20 3f 20 70 56 64 62 65 46 75 6e  eFunc ? pVdbeFun
12935 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b 0a 20 20  c->nAux : 0);.  
12936 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20    int nMalloc = 
12937 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e 63 29  sizeof(VdbeFunc)
12938 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74   + sizeof(struct
12939 20 41 75 78 44 61 74 61 29 2a 69 41 72 67 3b 0a   AuxData)*iArg;.
1293a 20 20 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20      pVdbeFunc = 
1293b 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
1293c 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 70 56 64  (pCtx->s.db, pVd
1293d 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29  beFunc, nMalloc)
1293e 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64 62 65  ;.    if( !pVdbe
1293f 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 67 6f  Func ){.      go
12940 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d  to failed;.    }
12941 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64 62 65  .    pCtx->pVdbe
12942 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75 6e 63  Func = pVdbeFunc
12943 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 56  ;.    memset(&pV
12944 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 6e  dbeFunc->apAux[n
12945 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Aux], 0, sizeof(
12946 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29 2a  struct AuxData)*
12947 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b 0a  (iArg+1-nAux));.
12948 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e      pVdbeFunc->n
12949 41 75 78 20 3d 20 69 41 72 67 2b 31 3b 0a 20 20  Aux = iArg+1;.  
1294a 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75    pVdbeFunc->pFu
1294b 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e 63  nc = pCtx->pFunc
1294c 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 44 61 74  ;.  }..  pAuxDat
1294d 61 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d 3e  a = &pVdbeFunc->
1294e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a 20 20 69  apAux[iArg];.  i
1294f 66 28 20 70 41 75 78 44 61 74 61 2d 3e 70 41 75  f( pAuxData->pAu
12950 78 20 26 26 20 70 41 75 78 44 61 74 61 2d 3e 78  x && pAuxData->x
12951 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 70 41  Delete ){.    pA
12952 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 28  uxData->xDelete(
12953 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 29 3b  pAuxData->pAux);
12954 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 74 61 2d  .  }.  pAuxData-
12955 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 20  >pAux = pAux;.  
12956 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74  pAuxData->xDelet
12957 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a 20 20 72  e = xDelete;.  r
12958 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a  eturn;..failed:.
12959 20 20 69 66 28 20 78 44 65 6c 65 74 65 20 29 7b    if( xDelete ){
1295a 0a 20 20 20 20 78 44 65 6c 65 74 65 28 70 41 75  .    xDelete(pAu
1295b 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  x);.  }.}../*.**
1295c 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1295d 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20  er of times the 
1295e 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66  Step function of
1295f 20 61 20 61 67 67 72 65 67 61 74 65 20 68 61 73   a aggregate has
12960 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64   been .** called
12961 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
12962 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61  ction is depreca
12963 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65  ted.  Do not use
12964 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f 64 65   it for new code
12965 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76  .  It is.** prov
12966 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69  ide only to avoi
12967 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 61 63  d breaking legac
12968 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 67 67  y code.  New agg
12969 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a  regate function.
1296a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
1296b 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 20 74  ns should keep t
1296c 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20  heir own counts 
1296d 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 67 67  within their agg
1296e 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78  regate.** contex
1296f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
12970 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67   int sqlite3_agg
12971 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c  regate_count(sql
12972 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29  ite3_context *p)
12973 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26  {.  assert( p &&
12974 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e   p->pFunc && p->
12975 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a  pFunc->xStep );.
12976 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d    return p->pMem
12977 2d 3e 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ->n;.}../*.** Re
12978 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
12979 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
1297a 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72  e result set for
1297b 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70   the statement p
1297c 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Stmt..*/.SQLITE_
1297d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1297e 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c  column_count(sql
1297f 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
12980 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d  ){.  Vdbe *pVm =
12981 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a   (Vdbe *)pStmt;.
12982 20 20 72 65 74 75 72 6e 20 70 56 6d 20 3f 20 70    return pVm ? p
12983 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3a  Vm->nResColumn :
12984 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   0;.}../*.** Ret
12985 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
12986 66 20 76 61 6c 75 65 73 20 61 76 61 69 6c 61 62  f values availab
12987 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72  le from the curr
12988 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 0a 2a  ent row of the.*
12989 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63  * currently exec
1298a 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20  uting statement 
1298b 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pStmt..*/.SQLITE
1298c 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1298d 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69  _data_count(sqli
1298e 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
1298f 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20  {.  Vdbe *pVm = 
12990 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
12991 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c 20 21   if( pVm==0 || !
12992 70 56 6d 2d 3e 72 65 73 4f 6e 53 74 61 63 6b 20  pVm->resOnStack 
12993 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65  ) return 0;.  re
12994 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 65 73 43 6f  turn pVm->nResCo
12995 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  lumn;.}.../*.** 
12996 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
12997 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66 20 74  column iCol of t
12998 68 65 20 67 69 76 65 6e 20 73 74 61 74 65 6d 65  he given stateme
12999 6e 74 20 69 73 20 76 61 6c 69 64 2e 20 20 49 66  nt is valid.  If
1299a 0a 2a 2a 20 69 74 20 69 73 2c 20 72 65 74 75 72  .** it is, retur
1299b 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
1299c 68 65 20 4d 65 6d 20 66 6f 72 20 74 68 65 20 76  he Mem for the v
1299d 61 6c 75 65 20 6f 66 20 74 68 61 74 20 63 6f 6c  alue of that col
1299e 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69 43 6f 6c 20  umn..** If iCol 
1299f 69 73 20 6e 6f 74 20 76 61 6c 69 64 2c 20 72 65  is not valid, re
129a0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
129a1 6f 20 61 20 4d 65 6d 20 77 68 69 63 68 20 68 61  o a Mem which ha
129a2 73 20 61 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  s a value.** of 
129a3 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  NULL..*/.static 
129a4 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d 65 6d 28 73  Mem *columnMem(s
129a5 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
129a6 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64  mt, int i){.  Vd
129a7 62 65 20 2a 70 56 6d 3b 0a 20 20 69 6e 74 20 76  be *pVm;.  int v
129a8 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74  als;.  Mem *pOut
129a9 3b 0a 0a 20 20 70 56 6d 20 3d 20 28 56 64 62 65  ;..  pVm = (Vdbe
129aa 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20   *)pStmt;.  if( 
129ab 70 56 6d 20 26 26 20 70 56 6d 2d 3e 72 65 73 4f  pVm && pVm->resO
129ac 6e 53 74 61 63 6b 20 26 26 20 69 3c 70 56 6d 2d  nStack && i<pVm-
129ad 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 26 26 20 69  >nResColumn && i
129ae 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
129af 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
129b0 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  Vm->db->mutex);.
129b1 20 20 20 20 76 61 6c 73 20 3d 20 73 71 6c 69 74      vals = sqlit
129b2 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 70 53  e3_data_count(pS
129b3 74 6d 74 29 3b 0a 20 20 20 20 70 4f 75 74 20 3d  tmt);.    pOut =
129b4 20 26 70 56 6d 2d 3e 70 54 6f 73 5b 28 31 2d 76   &pVm->pTos[(1-v
129b5 61 6c 73 29 2b 69 5d 3b 0a 20 20 7d 65 6c 73 65  als)+i];.  }else
129b6 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
129b7 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 3d  st Mem nullMem =
129b8 20 7b 7b 30 7d 2c 20 30 2e 30 2c 20 30 2c 20 22   {{0}, 0.0, 0, "
129b9 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20  ", 0, MEM_Null, 
129ba 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 7d 3b 0a 20  SQLITE_NULL };. 
129bb 20 20 20 69 66 28 20 70 56 6d 2d 3e 64 62 20 29     if( pVm->db )
129bc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
129bd 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d  mutex_enter(pVm-
129be 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
129bf 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
129c0 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f  pVm->db, SQLITE_
129c1 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d  RANGE, 0);.    }
129c2 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d  .    pOut = (Mem
129c3 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a  *)&nullMem;.  }.
129c4 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d    return pOut;.}
129c5 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
129c6 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
129c7 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61  after invoking a
129c8 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  n sqlite3_value_
129c9 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20  XXX function on 
129ca 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c  a .** column val
129cb 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c 75 65  ue (i.e. a value
129cc 20 72 65 74 75 72 6e 65 64 20 62 79 20 65 76 61   returned by eva
129cd 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c 20 65  luating an SQL e
129ce 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
129cf 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73 74 20  .** select list 
129d0 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
129d1 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61 79 20  ement) that may 
129d2 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29  cause a malloc()
129d3 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a   failure. If .**
129d4 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61   malloc() has fa
129d5 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65 61 64  iled, the thread
129d6 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  s mallocFailed f
129d7 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 61  lag is cleared a
129d8 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  nd the result.**
129d9 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65 6d 65   code of stateme
129da 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74 6f 20  nt pStmt set to 
129db 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a  SQLITE_NOMEM..**
129dc 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c 6c 79  .** Specifically
129dd 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  , this is called
129de 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a   from within:.**
129df 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .**     sqlite3_
129e0 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20  column_int().** 
129e1 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
129e2 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20  mn_int64().**   
129e3 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
129e4 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20 20 73  _text().**     s
129e5 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
129e6 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20 73 71  xt16().**     sq
129e7 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61  lite3_column_rea
129e8 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  l().**     sqlit
129e9 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
129ea 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
129eb 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
129ec 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20  ).**.** But not 
129ed 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  for sqlite3_colu
129ee 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69 63 68  mn_blob(), which
129ef 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c   never calls mal
129f0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
129f1 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   void columnMall
129f2 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69 74 65  ocFailure(sqlite
129f3 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b  3_stmt *pStmt).{
129f4 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28  .  /* If malloc(
129f5 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20  ) failed during 
129f6 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76  an encoding conv
129f7 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e  ersion within an
129f8 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  .  ** sqlite3_co
129f9 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20 74 68  lumn_XXX API, th
129fa 65 6e 20 73 65 74 20 74 68 65 20 72 65 74 75 72  en set the retur
129fb 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20 73 74  n code of the st
129fc 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20  atement to.  ** 
129fd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68  SQLITE_NOMEM. Th
129fe 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f  e next call to _
129ff 73 74 65 70 28 29 20 28 69 66 20 61 6e 79 29 20  step() (if any) 
12a00 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49  will return SQLI
12a01 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e  TE_ERROR.  ** an
12a02 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20 77 69  d _finalize() wi
12a03 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e  ll return NOMEM.
12a04 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20  .  */.  Vdbe *p 
12a05 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
12a06 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
12a07 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65 33 41  p->rc = sqlite3A
12a08 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d  piExit(p->db, p-
12a09 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  >rc);.    sqlite
12a0a 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
12a0b 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
12a0c 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
12a0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a0e 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
12a0f 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  _  *************
12a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a11 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
12a12 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ing routines are
12a13 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
12a14 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
12a15 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69  current row.** i
12a16 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
12a17 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
12a18 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
12a19 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
12a1a 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
12a1b 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63  tmt, int i){.  c
12a1c 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a  onst void *val;.
12a1d 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f    val = sqlite3_
12a1e 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75  value_blob( colu
12a1f 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
12a20 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75  ;.  /* Even thou
12a21 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65  gh there is no e
12a22 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69  ncoding conversi
12a23 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29  on, value_blob()
12a24 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64   might.  ** need
12a25 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28   to call malloc(
12a26 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20  ) to expand the 
12a27 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f  result of a zero
12a28 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65 78 70  blob() .  ** exp
12a29 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20  ression. .  */. 
12a2a 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
12a2b 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
12a2c 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
12a2d 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
12a2e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
12a2f 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
12a30 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
12a31 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  int val = sqlite
12a32 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 20 63  3_value_bytes( c
12a33 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
12a34 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
12a35 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
12a36 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
12a37 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
12a38 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
12a39 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33  _bytes16(sqlite3
12a3a 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
12a3b 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20  t i){.  int val 
12a3c 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
12a3d 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d  bytes16( columnM
12a3e 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
12a3f 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
12a40 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
12a41 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
12a42 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73  ITE_API double s
12a43 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f  qlite3_column_do
12a44 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  uble(sqlite3_stm
12a45 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
12a46 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d  {.  double val =
12a47 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
12a48 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d  ouble( columnMem
12a49 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
12a4a 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
12a4b 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
12a4c 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
12a4d 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
12a4e 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c  3_column_int(sql
12a4f 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
12a50 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
12a51 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
12a52 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d  lue_int( columnM
12a53 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
12a54 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
12a55 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
12a56 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
12a57 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69  ITE_API sqlite_i
12a58 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c  nt64 sqlite3_col
12a59 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74 65  umn_int64(sqlite
12a5a 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
12a5b 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f  nt i){.  sqlite_
12a5c 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71 6c 69  int64 val = sqli
12a5d 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
12a5e 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
12a5f 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
12a60 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
12a61 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
12a62 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
12a63 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
12a64 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
12a65 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33  umn_text(sqlite3
12a66 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
12a67 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  t i){.  const un
12a68 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c  signed char *val
12a69 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
12a6a 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d  _text( columnMem
12a6b 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
12a6c 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
12a6d 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
12a6e 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
12a6f 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61  E_API sqlite3_va
12a70 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  lue *sqlite3_col
12a71 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65  umn_value(sqlite
12a72 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
12a73 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74 65 33  nt i){.  sqlite3
12a74 5f 76 61 6c 75 65 20 2a 70 4f 75 74 20 3d 20 63  _value *pOut = c
12a75 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 20  olumnMem(pStmt, 
12a76 69 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c  i);.  columnMall
12a77 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
12a78 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b  ;.  return pOut;
12a79 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
12a7a 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
12a7b 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
12a7c 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
12a7d 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65  mn_text16(sqlite
12a7e 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
12a7f 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76  nt i){.  const v
12a80 6f 69 64 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74  oid *val = sqlit
12a81 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
12a82 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
12a83 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
12a84 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
12a85 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
12a86 6c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  l;.}.#endif /* S
12a87 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
12a88 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
12a89 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
12a8a 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73  n_type(sqlite3_s
12a8b 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
12a8c 69 29 7b 0a 20 20 69 6e 74 20 69 54 79 70 65 20  i){.  int iType 
12a8d 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
12a8e 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  type( columnMem(
12a8f 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f  pStmt,i) );.  co
12a90 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
12a91 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
12a92 72 6e 20 69 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 20  rn iType;.}../* 
12a93 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75  The following fu
12a94 6e 63 74 69 6f 6e 20 69 73 20 65 78 70 65 72 69  nction is experi
12a95 6d 65 6e 74 61 6c 20 61 6e 64 20 73 75 62 6a 65  mental and subje
12a96 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a  ct to change or.
12a97 2a 2a 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a  ** removal */./*
12a98 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
12a99 6d 6e 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  mn_numeric_type(
12a9a 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
12a9b 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 2a 2a 20  tmt, int i){.** 
12a9c 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
12a9d 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
12a9e 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  pe( columnMem(pS
12a9f 74 6d 74 2c 69 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f  tmt,i) );.**}.*/
12aa0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
12aa1 74 68 65 20 4e 2d 74 68 20 65 6c 65 6d 65 6e 74  the N-th element
12aa2 20 6f 66 20 70 53 74 6d 74 2d 3e 70 43 6f 6c 4e   of pStmt->pColN
12aa3 61 6d 65 5b 5d 20 69 6e 74 6f 20 61 20 73 74 72  ame[] into a str
12aa4 69 6e 67 20 75 73 69 6e 67 0a 2a 2a 20 78 46 75  ing using.** xFu
12aa5 6e 63 28 29 20 74 68 65 6e 20 72 65 74 75 72 6e  nc() then return
12aa6 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20 49   that string.  I
12aa7 66 20 4e 20 69 73 20 6f 75 74 20 6f 66 20 72 61  f N is out of ra
12aa8 6e 67 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a  nge, return 0..*
12aa9 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 75  *.** There are u
12aaa 70 20 74 6f 20 35 20 6e 61 6d 65 73 20 66 6f 72  p to 5 names for
12aab 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 75   each column.  u
12aac 73 65 54 79 70 65 20 64 65 74 65 72 6d 69 6e 65  seType determine
12aad 73 20 77 68 69 63 68 0a 2a 2a 20 6e 61 6d 65 20  s which.** name 
12aae 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 48 65  is returned.  He
12aaf 72 65 20 61 72 65 20 74 68 65 20 6e 61 6d 65 73  re are the names
12ab0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20  :.**.**    0    
12ab1 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d    The column nam
12ab2 65 20 61 73 20 69 74 20 73 68 6f 75 6c 64 20 62  e as it should b
12ab3 65 20 64 69 73 70 6c 61 79 65 64 20 66 6f 72 20  e displayed for 
12ab4 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 31 20 20  output.**    1  
12ab5 20 20 20 20 54 68 65 20 64 61 74 61 74 79 70 65      The datatype
12ab6 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f   name for the co
12ab7 6c 75 6d 6e 0a 2a 2a 20 20 20 20 32 20 20 20 20  lumn.**    2    
12ab8 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68    The name of th
12ab9 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  e database that 
12aba 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76  the column deriv
12abb 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 33 20  es from.**    3 
12abc 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66       The name of
12abd 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
12abe 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76  the column deriv
12abf 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 34 20  es from.**    4 
12ac0 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66       The name of
12ac1 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d   the table colum
12ac2 6e 20 74 68 61 74 20 74 68 65 20 72 65 73 75 6c  n that the resul
12ac3 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73  t column derives
12ac4 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 74   from.**.** If t
12ac5 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 74  he result is not
12ac6 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e   a simple column
12ac7 20 72 65 66 65 72 65 6e 63 65 20 28 69 66 20 69   reference (if i
12ac8 74 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69  t is an expressi
12ac9 6f 6e 0a 2a 2a 20 6f 72 20 61 20 63 6f 6e 73 74  on.** or a const
12aca 61 6e 74 29 20 74 68 65 6e 20 75 73 65 54 79 70  ant) then useTyp
12acb 65 73 20 32 2c 20 33 2c 20 61 6e 64 20 34 20 72  es 2, 3, and 4 r
12acc 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73  eturn NULL..*/.s
12acd 74 61 74 69 63 20 63 6f 6e 73 74 20 76 6f 69 64  tatic const void
12ace 20 2a 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20   *columnName(.  
12acf 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
12ad0 74 6d 74 2c 0a 20 20 69 6e 74 20 4e 2c 0a 20 20  tmt,.  int N,.  
12ad1 63 6f 6e 73 74 20 76 6f 69 64 20 2a 28 2a 78 46  const void *(*xF
12ad2 75 6e 63 29 28 4d 65 6d 2a 29 2c 0a 20 20 69 6e  unc)(Mem*),.  in
12ad3 74 20 75 73 65 54 79 70 65 0a 29 7b 0a 20 20 63  t useType.){.  c
12ad4 6f 6e 73 74 20 76 6f 69 64 20 2a 72 65 74 20 3d  onst void *ret =
12ad5 20 30 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20   0;.  Vdbe *p = 
12ad6 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
12ad7 20 69 6e 74 20 6e 3b 0a 20 20 0a 0a 20 20 69 66   int n;.  ..  if
12ad8 28 20 70 21 3d 30 20 29 7b 0a 20 20 20 20 6e 20  ( p!=0 ){.    n 
12ad9 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
12ada 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20  _count(pStmt);. 
12adb 20 20 20 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e     if( N<n && N>
12adc 3d 30 20 29 7b 0a 20 20 20 20 20 20 4e 20 2b 3d  =0 ){.      N +=
12add 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20   useType*n;.    
12ade 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
12adf 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74  enter(p->db->mut
12ae0 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74 20 3d  ex);.      ret =
12ae1 20 78 46 75 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e   xFunc(&p->aColN
12ae2 61 6d 65 5b 4e 5d 29 3b 0a 0a 20 20 20 20 20 20  ame[N]);..      
12ae3 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 61 79 20  /* A malloc may 
12ae4 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69  have failed insi
12ae5 64 65 20 6f 66 20 74 68 65 20 78 46 75 6e 63 28  de of the xFunc(
12ae6 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 69 73 0a  ) call. If this.
12ae7 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20        ** is the 
12ae8 63 61 73 65 2c 20 63 6c 65 61 72 20 74 68 65 20  case, clear the 
12ae9 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
12aea 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  g and return NUL
12aeb 4c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  L..      */.    
12aec 20 20 69 66 28 20 70 2d 3e 64 62 20 26 26 20 70    if( p->db && p
12aed 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
12aee 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  ed ){.        p-
12aef 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
12af0 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72  d = 0;.        r
12af1 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  et = 0;.      }.
12af2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
12af3 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
12af4 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20  >mutex);.    }. 
12af5 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b   }.  return ret;
12af6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
12af7 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
12af8 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   Nth column of t
12af9 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 72 65  he result set re
12afa 74 75 72 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a  turned by SQL.**
12afb 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74   statement pStmt
12afc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
12afd 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
12afe 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28  te3_column_name(
12aff 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
12b00 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
12b01 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
12b02 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
12b03 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
12b04 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
12b05 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e  value_text, COLN
12b06 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66  AME_NAME);.}.#if
12b07 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
12b08 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
12b09 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
12b0a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
12b0b 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
12b0c 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
12b0d 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
12b0e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
12b0f 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
12b10 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
12b11 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
12b12 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b  , COLNAME_NAME);
12b13 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
12b14 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 75   Return the colu
12b15 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  mn declaration t
12b16 79 70 65 20 28 69 66 20 61 70 70 6c 69 63 61 62  ype (if applicab
12b17 6c 65 29 20 6f 66 20 74 68 65 20 27 69 27 74 68  le) of the 'i'th
12b18 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68   column.** of th
12b19 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
12b1a 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 53  SQL statement pS
12b1b 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  tmt..*/.SQLITE_A
12b1c 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
12b1d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
12b1e 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73  cltype(sqlite3_s
12b1f 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
12b20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
12b21 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
12b22 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
12b23 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
12b24 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
12b25 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54  t, COLNAME_DECLT
12b26 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  YPE);.}.#ifndef 
12b27 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
12b28 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
12b29 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
12b2a 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
12b2b 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  16(sqlite3_stmt 
12b2c 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
12b2d 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
12b2e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
12b2f 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
12b30 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
12b31 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c  e3_value_text16,
12b32 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50   COLNAME_DECLTYP
12b33 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  E);.}.#endif /* 
12b34 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
12b35 36 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  6 */..#ifdef SQL
12b36 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
12b37 4e 5f 4d 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a  N_METADATA./*.**
12b38 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   Return the name
12b39 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
12b3a 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65   from which a re
12b3b 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69  sult column deri
12b3c 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20  ves..** NULL is 
12b3d 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20  returned if the 
12b3e 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73  result column is
12b3f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   an expression o
12b40 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a  r constant or.**
12b41 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77   anything else w
12b42 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75  hich is not an u
12b43 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65  nabiguous refere
12b44 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73  nce to a databas
12b45 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c  e column..*/.SQL
12b46 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
12b47 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
12b48 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
12b49 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
12b4a 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
12b4b 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
12b4c 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
12b4d 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
12b4e 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
12b4f 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c  _value_text, COL
12b50 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a  NAME_DATABASE);.
12b51 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
12b52 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
12b53 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
12b54 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
12b55 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
12b56 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
12b57 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
12b58 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
12b59 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
12b5a 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
12b5b 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
12b5c 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20  3_value_text16, 
12b5d 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45  COLNAME_DATABASE
12b5e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
12b5f 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
12b60 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   */../*.** Retur
12b61 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
12b62 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69  e table from whi
12b63 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  ch a result colu
12b64 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e  mn derives..** N
12b65 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
12b66 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  if the result co
12b67 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65  lumn is an expre
12b68 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e  ssion or constan
12b69 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  t or.** anything
12b6a 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e   else which is n
12b6b 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73  ot an unabiguous
12b6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
12b6d 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e  database column.
12b6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
12b6f 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
12b70 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
12b71 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
12b72 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
12b73 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
12b74 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
12b75 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
12b76 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
12b77 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c  ite3_value_text,
12b78 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b   COLNAME_TABLE);
12b79 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
12b7a 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
12b7b 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
12b7c 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
12b7d 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28  mn_table_name16(
12b7e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
12b7f 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
12b80 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
12b81 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
12b82 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
12b83 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
12b84 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f  value_text16, CO
12b85 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a  LNAME_TABLE);.}.
12b86 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
12b87 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
12b88 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
12b89 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
12b8a 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77  le column from w
12b8b 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f  hich a result co
12b8c 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a  lumn derives..**
12b8d 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
12b8e 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20  d if the result 
12b8f 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70  column is an exp
12b90 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74  ression or const
12b91 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69  ant or.** anythi
12b92 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 73  ng else which is
12b93 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f   not an unabiguo
12b94 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  us reference to 
12b95 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d  a database colum
12b96 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
12b97 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
12b98 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
12b99 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  in_name(sqlite3_
12b9a 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
12b9b 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
12b9c 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
12b9d 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
12b9e 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
12b9f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
12ba0 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55  xt, COLNAME_COLU
12ba1 4d 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  MN);.}.#ifndef S
12ba2 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
12ba3 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
12ba4 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
12ba5 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
12ba6 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
12ba7 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
12ba8 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
12ba9 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
12baa 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
12bab 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
12bac 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
12bad 36 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  6, COLNAME_COLUM
12bae 4e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  N);.}.#endif /* 
12baf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
12bb0 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53  6 */.#endif /* S
12bb1 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
12bb2 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a  UMN_METADATA */.
12bb3 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
12bb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12bb5 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ** sqlite3_bind_
12bb6 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
12bb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
12bb8 20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73   .** Routines us
12bb9 65 64 20 74 6f 20 61 74 74 61 63 68 20 76 61 6c  ed to attach val
12bba 75 65 73 20 74 6f 20 77 69 6c 64 63 61 72 64 73  ues to wildcards
12bbb 20 69 6e 20 61 20 63 6f 6d 70 69 6c 65 64 20 53   in a compiled S
12bbc 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  QL statement..*/
12bbd 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68  ./*.** Unbind th
12bbe 65 20 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f  e value bound to
12bbf 20 76 61 72 69 61 62 6c 65 20 69 20 69 6e 20 76   variable i in v
12bc0 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70  irtual machine p
12bc1 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 0a 2a  . This is the .*
12bc2 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 62 69  * the same as bi
12bc3 6e 64 69 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c  nding a NULL val
12bc4 75 65 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e  ue to the column
12bc5 2e 20 49 66 20 74 68 65 20 22 69 22 20 70 61 72  . If the "i" par
12bc6 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74  ameter is.** out
12bc7 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20   of range, then 
12bc8 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 69 73 20  SQLITE_RANGE is 
12bc9 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 77 69  returned. Othewi
12bca 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  se SQLITE_OK..**
12bcb 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f  .** The error co
12bcc 64 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74  de stored in dat
12bcd 61 62 61 73 65 20 70 2d 3e 64 62 20 69 73 20 6f  abase p->db is o
12bce 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20  verwritten with 
12bcf 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  the return.** va
12bd0 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 73 65 2e  lue in any case.
12bd1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
12bd2 64 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a  dbeUnbind(Vdbe *
12bd3 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d  p, int i){.  Mem
12bd4 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 70 3d   *pVar;.  if( p=
12bd5 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 21 3d  =0 || p->magic!=
12bd6 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c  VDBE_MAGIC_RUN |
12bd7 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  | p->pc>=0 ){.  
12bd8 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74 65    if( p ) sqlite
12bd9 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51  3Error(p->db, SQ
12bda 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b  LITE_MISUSE, 0);
12bdb 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
12bdc 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
12bdd 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 2d   if( i<1 || i>p-
12bde 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c  >nVar ){.    sql
12bdf 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c  ite3Error(p->db,
12be0 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30   SQLITE_RANGE, 0
12be1 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
12be2 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a  LITE_RANGE;.  }.
12be3 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20    i--;.  pVar = 
12be4 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73  &p->aVar[i];.  s
12be5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
12be6 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56  ease(pVar);.  pV
12be7 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ar->flags = MEM_
12be8 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45  Null;.  sqlite3E
12be9 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49  rror(p->db, SQLI
12bea 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74  TE_OK, 0);.  ret
12beb 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
12bec 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74  ../*.** Bind a t
12bed 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75  ext or BLOB valu
12bee 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
12bef 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 6c   bindText(.  sql
12bf0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
12bf1 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65  ,   /* The state
12bf2 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 61  ment to bind aga
12bf3 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c  inst */.  int i,
12bf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12bf5 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
12bf6 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69   parameter to bi
12bf7 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nd */.  const vo
12bf8 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f  id *zData,     /
12bf9 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
12bfa 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e   data to be boun
12bfb 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61  d */.  int nData
12bfc 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
12bfd 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
12bfe 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62   of data to be b
12bff 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28  ound */.  void (
12c00 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20  *xDel)(void*),  
12c01 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
12c02 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  or the data */. 
12c03 20 69 6e 74 20 65 6e 63 6f 64 69 6e 67 20 20 20   int encoding   
12c04 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64          /* Encod
12c05 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61  ing for the data
12c06 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70   */.){.  Vdbe *p
12c07 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
12c08 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20  ;.  Mem *pVar;. 
12c09 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20   int rc;..  if( 
12c0a 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  p==0 ){.    retu
12c0b 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
12c0c 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
12c0d 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64  mutex_enter(p->d
12c0e 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20  b->mutex);.  rc 
12c0f 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
12c10 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
12c11 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 44 61 74 61  LITE_OK && zData
12c12 21 3d 30 20 29 7b 0a 20 20 20 20 70 56 61 72 20  !=0 ){.    pVar 
12c13 3d 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b  = &p->aVar[i-1];
12c14 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
12c15 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
12c16 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74  Var, zData, nDat
12c17 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65  a, encoding, xDe
12c18 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  l);.    if( rc==
12c19 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63  SQLITE_OK && enc
12c1a 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20  oding!=0 ){.    
12c1b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
12c1c 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
12c1d 28 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62  (pVar, ENC(p->db
12c1e 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ));.    }.    sq
12c1f 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62  lite3Error(p->db
12c20 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 72 63  , rc, 0);.    rc
12c21 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
12c22 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20  t(p->db, rc);.  
12c23 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
12c24 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
12c25 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
12c26 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69  rc;.}.../*.** Bi
12c27 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20  nd a blob value 
12c28 74 6f 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  to an SQL statem
12c29 65 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f  ent variable..*/
12c2a 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
12c2b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
12c2c 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  b(.  sqlite3_stm
12c2d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74  t *pStmt, .  int
12c2e 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69   i, .  const voi
12c2f 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74  d *zData, .  int
12c30 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20   nData, .  void 
12c31 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
12c32 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54  {.  return bindT
12c33 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44  ext(pStmt, i, zD
12c34 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c  ata, nData, xDel
12c35 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  , 0);.}.SQLITE_A
12c36 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
12c37 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74  ind_double(sqlit
12c38 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
12c39 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56  int i, double rV
12c3a 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  alue){.  int rc;
12c3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
12c3c 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 73 71  be *)pStmt;.  sq
12c3d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
12c3e 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  r(p->db->mutex);
12c3f 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69  .  rc = vdbeUnbi
12c40 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20  nd(p, i);.  if( 
12c41 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
12c42 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
12c43 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d  MemSetDouble(&p-
12c44 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c  >aVar[i-1], rVal
12c45 75 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ue);.  }.  sqlit
12c46 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
12c47 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
12c48 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
12c49 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
12c4a 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c  te3_bind_int(sql
12c4b 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e  ite3_stmt *p, in
12c4c 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29  t i, int iValue)
12c4d 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
12c4e 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c  e3_bind_int64(p,
12c4f 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29   i, (i64)iValue)
12c50 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
12c51 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
12c52 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74  int64(sqlite3_st
12c53 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
12c54 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
12c55 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63  Value){.  int rc
12c56 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
12c57 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 73  dbe *)pStmt;.  s
12c58 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
12c59 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  er(p->db->mutex)
12c5a 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62  ;.  rc = vdbeUnb
12c5b 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28  ind(p, i);.  if(
12c5c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
12c5d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
12c5e 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 2d  eMemSetInt64(&p-
12c5f 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 69 56 61 6c  >aVar[i-1], iVal
12c60 75 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ue);.  }.  sqlit
12c61 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
12c62 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
12c63 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
12c64 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
12c65 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71  te3_bind_null(sq
12c66 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
12c67 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74  t, int i){.  int
12c68 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d   rc;.  Vdbe *p =
12c69 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
12c6a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
12c6b 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  nter(p->db->mute
12c6c 78 29 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55  x);.  rc = vdbeU
12c6d 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 73  nbind(p, i);.  s
12c6e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
12c6f 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
12c70 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
12c71 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
12c72 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
12c73 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  t( .  sqlite3_st
12c74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e  mt *pStmt, .  in
12c75 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  t i, .  const ch
12c76 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e  ar *zData, .  in
12c77 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64  t nData, .  void
12c78 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a   (*xDel)(void*).
12c79 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64  ){.  return bind
12c7a 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a  Text(pStmt, i, z
12c7b 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65  Data, nData, xDe
12c7c 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
12c7d 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
12c7e 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
12c7f 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
12c80 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28  te3_bind_text16(
12c81 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
12c82 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69  *pStmt, .  int i
12c83 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
12c84 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e  *zData, .  int n
12c85 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a  Data, .  void (*
12c86 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  xDel)(void*).){.
12c87 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78    return bindTex
12c88 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74  t(pStmt, i, zDat
12c89 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20  a, nData, xDel, 
12c8a 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
12c8b 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  VE);.}.#endif /*
12c8c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
12c8d 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  16 */.SQLITE_API
12c8e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
12c8f 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  d_value(sqlite3_
12c90 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
12c91 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65   i, const sqlite
12c92 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29  3_value *pValue)
12c93 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64  {.  int rc;.  Vd
12c94 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
12c95 70 53 74 6d 74 3b 0a 20 20 73 71 6c 69 74 65 33  pStmt;.  sqlite3
12c96 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e  _mutex_enter(p->
12c97 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63  db->mutex);.  rc
12c98 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
12c99 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
12c9a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12c9b 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
12c9c 4d 65 6d 43 6f 70 79 28 26 70 2d 3e 61 56 61 72  MemCopy(&p->aVar
12c9d 5b 69 2d 31 5d 2c 20 70 56 61 6c 75 65 29 3b 0a  [i-1], pValue);.
12c9e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
12c9f 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
12ca0 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
12ca1 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  n rc;.}.SQLITE_A
12ca2 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
12ca3 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c  ind_zeroblob(sql
12ca4 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
12ca5 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b  , int i, int n){
12ca6 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62  .  int rc;.  Vdb
12ca7 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
12ca8 53 74 6d 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Stmt;.  sqlite3_
12ca9 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64  mutex_enter(p->d
12caa 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20  b->mutex);.  rc 
12cab 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
12cac 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
12cad 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
12cae 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
12caf 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61  ZeroBlob(&p->aVa
12cb0 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 7d 0a  r[i-1], n);.  }.
12cb1 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
12cb2 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
12cb3 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
12cb4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
12cb5 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
12cb6 77 69 6c 64 63 61 72 64 73 20 74 68 61 74 20 63  wildcards that c
12cb7 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c 6c  an be potentiall
12cb8 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54  y bound to..** T
12cb9 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
12cba 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20  dded to support 
12cbb 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a 2a  DBD::SQLite.  .*
12cbc 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
12cbd 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
12cbe 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71  rameter_count(sq
12cbf 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
12cc0 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  t){.  Vdbe *p = 
12cc1 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
12cc2 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56  return p ? p->nV
12cc3 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ar : 0;.}../*.**
12cc4 20 43 72 65 61 74 65 20 61 20 6d 61 70 70 69 6e   Create a mappin
12cc5 67 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 20  g from variable 
12cc6 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69 61  numbers to varia
12cc7 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20  ble names.** in 
12cc8 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d  the Vdbe.azVar[]
12cc9 20 61 72 72 61 79 2c 20 69 66 20 73 75 63 68 20   array, if such 
12cca 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 6e  a mapping does n
12ccb 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 78  ot already.** ex
12ccc 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
12ccd 6f 69 64 20 63 72 65 61 74 65 56 61 72 4d 61 70  oid createVarMap
12cce 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28  (Vdbe *p){.  if(
12ccf 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20   !p->okVar ){.  
12cd0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
12cd1 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74  enter(p->db->mut
12cd2 65 78 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d  ex);.    if( !p-
12cd3 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 20 20  >okVar ){.      
12cd4 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 4f 70 20  int j;.      Op 
12cd5 2a 70 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 28  *pOp;.      for(
12cd6 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b  j=0, pOp=p->aOp;
12cd7 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20   j<p->nOp; j++, 
12cd8 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  pOp++){.        
12cd9 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
12cda 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b 0a  =OP_Variable ){.
12cdb 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
12cdc 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70  ( pOp->p1>0 && p
12cdd 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20  Op->p1<=p->nVar 
12cde 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  );.          p->
12cdf 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d  azVar[pOp->p1-1]
12ce0 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20   = pOp->p3;.    
12ce1 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
12ce2 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31      p->okVar = 1
12ce3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
12ce4 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
12ce5 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
12ce6 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
12ce7 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  rn the name of a
12ce8 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65   wildcard parame
12ce9 74 65 72 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  ter.  Return NUL
12cea 4c 20 69 66 20 74 68 65 20 69 6e 64 65 78 0a 2a  L if the index.*
12ceb 2a 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  * is out of rang
12cec 65 20 6f 72 20 69 66 20 74 68 65 20 77 69 6c 64  e or if the wild
12ced 63 61 72 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e  card is unnamed.
12cee 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  .**.** The resul
12cef 74 20 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d  t is always UTF-
12cf0 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  8..*/.SQLITE_API
12cf1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
12cf2 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
12cf3 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  ter_name(sqlite3
12cf4 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
12cf5 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 20  t i){.  Vdbe *p 
12cf6 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a  = (Vdbe*)pStmt;.
12cf7 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c    if( p==0 || i<
12cf8 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29  1 || i>p->nVar )
12cf9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
12cfa 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61 72 4d    }.  createVarM
12cfb 61 70 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ap(p);.  return 
12cfc 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d  p->azVar[i-1];.}
12cfd 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20  ../*.** Given a 
12cfe 77 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74  wildcard paramet
12cff 65 72 20 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20  er name, return 
12d00 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
12d01 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74   variable.** wit
12d02 68 20 74 68 61 74 20 6e 61 6d 65 2e 20 20 49 66  h that name.  If
12d03 20 74 68 65 72 65 20 69 73 20 6e 6f 20 76 61 72   there is no var
12d04 69 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 67  iable with the g
12d05 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65  iven name,.** re
12d06 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  turn 0..*/.SQLIT
12d07 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
12d08 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
12d09 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73  _index(sqlite3_s
12d0a 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73  tmt *pStmt, cons
12d0b 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a  t char *zName){.
12d0c 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
12d0d 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20  e*)pStmt;.  int 
12d0e 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  i;.  if( p==0 ){
12d0f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
12d10 20 7d 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61   }.  createVarMa
12d11 70 28 70 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61  p(p); .  if( zNa
12d12 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  me ){.    for(i=
12d13 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b  0; i<p->nVar; i+
12d14 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  +){.      const 
12d15 63 68 61 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56  char *z = p->azV
12d16 61 72 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  ar[i];.      if(
12d17 20 7a 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a   z && strcmp(z,z
12d18 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Name)==0 ){.    
12d19 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a      return i+1;.
12d1a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
12d1b 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
12d1c 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20  ./*.** Transfer 
12d1d 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20 66 72 6f  all bindings fro
12d1e 6d 20 74 68 65 20 66 69 72 73 74 20 73 74 61 74  m the first stat
12d1f 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f 20 74 68  ement over to th
12d20 65 20 73 65 63 6f 6e 64 2e 0a 2a 2a 20 49 66 20  e second..** If 
12d21 74 68 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e  the two statemen
12d22 74 73 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66  ts contain a dif
12d23 66 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  ferent number of
12d24 20 62 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a   bindings, then.
12d25 2a 2a 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52  ** an SQLITE_ERR
12d26 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OR is returned..
12d27 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
12d28 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66  t sqlite3_transf
12d29 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69  er_bindings(sqli
12d2a 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53  te3_stmt *pFromS
12d2b 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  tmt, sqlite3_stm
12d2c 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56  t *pToStmt){.  V
12d2d 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64  dbe *pFrom = (Vd
12d2e 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20  be*)pFromStmt;. 
12d2f 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64   Vdbe *pTo = (Vd
12d30 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69  be*)pToStmt;.  i
12d31 6e 74 20 69 2c 20 72 63 20 3d 20 53 51 4c 49 54  nt i, rc = SQLIT
12d32 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 28 70 46 72  E_OK;.  if( (pFr
12d33 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f  om->magic!=VDBE_
12d34 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70 46 72  MAGIC_RUN && pFr
12d35 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f  om->magic!=VDBE_
12d36 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20 20 20 20  MAGIC_HALT).    
12d37 7c 7c 20 28 70 54 6f 2d 3e 6d 61 67 69 63 21 3d  || (pTo->magic!=
12d38 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26  VDBE_MAGIC_RUN &
12d39 26 20 70 54 6f 2d 3e 6d 61 67 69 63 21 3d 56 44  & pTo->magic!=VD
12d3a 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20  BE_MAGIC_HALT). 
12d3b 20 20 20 7c 7c 20 70 54 6f 2d 3e 64 62 21 3d 70     || pTo->db!=p
12d3c 46 72 6f 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20  From->db ){.    
12d3d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
12d3e 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SUSE;.  }.  if( 
12d3f 70 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f  pFrom->nVar!=pTo
12d40 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65  ->nVar ){.    re
12d41 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
12d42 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  R;.  }.  sqlite3
12d43 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54 6f  _mutex_enter(pTo
12d44 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
12d45 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
12d46 49 54 45 5f 4f 4b 20 26 26 20 69 3c 70 46 72 6f  ITE_OK && i<pFro
12d47 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20  m->nVar; i++){. 
12d48 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63     sqlite3Malloc
12d49 44 69 73 61 6c 6c 6f 77 28 29 3b 0a 20 20 20 20  Disallow();.    
12d4a 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
12d4b 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56  MemMove(&pTo->aV
12d4c 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61  ar[i], &pFrom->a
12d4d 56 61 72 5b 69 5d 29 3b 0a 20 20 20 20 73 71 6c  Var[i]);.    sql
12d4e 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28  ite3MallocAllow(
12d4f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
12d50 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 54 6f  _mutex_leave(pTo
12d51 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
12d52 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
12d53 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c  TE_OK || rc==SQL
12d54 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 72  ITE_NOMEM );.  r
12d55 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
12d56 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 71  ** Return the sq
12d57 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20  lite3* database 
12d58 68 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20  handle to which 
12d59 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
12d5a 74 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20  tement given.** 
12d5b 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  in the argument 
12d5c 62 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69  belongs.  This i
12d5d 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  s the same datab
12d5e 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20  ase handle that 
12d5f 77 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  was.** the first
12d60 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
12d61 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
12d62 28 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64  () that was used
12d63 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68   to create.** th
12d64 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74  e statement in t
12d65 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a  he first place..
12d66 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
12d67 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64  lite3 *sqlite3_d
12d68 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33  b_handle(sqlite3
12d69 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
12d6a 20 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20   return pStmt ? 
12d6b 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e  ((Vdbe*)pStmt)->
12d6c 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  db : 0;.}../****
12d6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
12d6e 66 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a  f vdbeapi.c ****
12d6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12d71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
12d72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
12d73 20 66 69 6c 65 20 76 64 62 65 2e 63 20 2a 2a 2a   file vdbe.c ***
12d74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12d75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12d76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
12d77 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
12d78 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
12d79 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
12d7a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
12d7b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
12d7c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
12d7d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
12d7e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
12d7f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
12d80 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
12d81 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
12d82 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
12d83 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
12d84 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
12d85 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
12d86 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
12d87 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
12d88 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
12d89 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
12d8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12d8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12d8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12d8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12d8e 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
12d8f 20 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   this file imple
12d90 6d 65 6e 74 73 20 65 78 65 63 75 74 69 6f 6e 20  ments execution 
12d91 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 0a 2a  method of the .*
12d92 2a 20 56 69 72 74 75 61 6c 20 44 61 74 61 62 61  * Virtual Databa
12d93 73 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29  se Engine (VDBE)
12d94 2e 20 20 41 20 73 65 70 61 72 61 74 65 20 66 69  .  A separate fi
12d95 6c 65 20 28 22 76 64 62 65 61 75 78 2e 63 22 29  le ("vdbeaux.c")
12d96 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 68 6f 75 73  .** handles hous
12d97 65 6b 65 65 70 69 6e 67 20 64 65 74 61 69 6c 73  ekeeping details
12d98 20 73 75 63 68 20 61 73 20 63 72 65 61 74 69 6e   such as creatin
12d99 67 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 0a 2a  g and deleting.*
12d9a 2a 20 56 44 42 45 20 69 6e 73 74 61 6e 63 65 73  * VDBE instances
12d9b 2e 20 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  .  This file is 
12d9c 73 6f 6c 65 6c 79 20 69 6e 74 65 72 65 73 74 65  solely intereste
12d9d 64 20 69 6e 20 65 78 65 63 75 74 69 6e 67 0a 2a  d in executing.*
12d9e 2a 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  * the VDBE progr
12d9f 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  am..**.** In the
12da0 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66   external interf
12da1 61 63 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33  ace, an "sqlite3
12da2 5f 73 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70  _stmt*" is an op
12da3 61 71 75 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  aque pointer.** 
12da4 74 6f 20 61 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a  to a VDBE..**.**
12da5 20 54 68 65 20 53 51 4c 20 70 61 72 73 65 72 20   The SQL parser 
12da6 67 65 6e 65 72 61 74 65 73 20 61 20 70 72 6f 67  generates a prog
12da7 72 61 6d 20 77 68 69 63 68 20 69 73 20 74 68 65  ram which is the
12da8 6e 20 65 78 65 63 75 74 65 64 20 62 79 0a 2a 2a  n executed by.**
12da9 20 74 68 65 20 56 44 42 45 20 74 6f 20 64 6f 20   the VDBE to do 
12daa 74 68 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 20  the work of the 
12dab 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  SQL statement.  
12dac 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 61 72  VDBE programs ar
12dad 65 20 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e  e .** similar in
12dae 20 66 6f 72 6d 20 74 6f 20 61 73 73 65 6d 62 6c   form to assembl
12daf 79 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65  y language.  The
12db0 20 70 72 6f 67 72 61 6d 20 63 6f 6e 73 69 73 74   program consist
12db1 73 20 6f 66 0a 2a 2a 20 61 20 6c 69 6e 65 61 72  s of.** a linear
12db2 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 70 65   sequence of ope
12db3 72 61 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 6f  rations.  Each o
12db4 70 65 72 61 74 69 6f 6e 20 68 61 73 20 61 6e 20  peration has an 
12db5 6f 70 63 6f 64 65 20 0a 2a 2a 20 61 6e 64 20 33  opcode .** and 3
12db6 20 6f 70 65 72 61 6e 64 73 2e 20 20 4f 70 65 72   operands.  Oper
12db7 61 6e 64 73 20 50 31 20 61 6e 64 20 50 32 20 61  ands P1 and P2 a
12db8 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70  re integers.  Op
12db9 65 72 61 6e 64 20 50 33 20 0a 2a 2a 20 69 73 20  erand P3 .** is 
12dba 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
12dbb 64 20 73 74 72 69 6e 67 2e 20 20 20 54 68 65 20  d string.   The 
12dbc 50 32 20 6f 70 65 72 61 6e 64 20 6d 75 73 74 20  P2 operand must 
12dbd 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  be non-negative.
12dbe 0a 2a 2a 20 4f 70 63 6f 64 65 73 20 77 69 6c 6c  .** Opcodes will
12dbf 20 74 79 70 69 63 61 6c 6c 79 20 69 67 6e 6f 72   typically ignor
12dc0 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 70  e one or more op
12dc1 65 72 61 6e 64 73 2e 20 20 4d 61 6e 79 20 6f 70  erands.  Many op
12dc2 63 6f 64 65 73 0a 2a 2a 20 69 67 6e 6f 72 65 20  codes.** ignore 
12dc3 61 6c 6c 20 74 68 72 65 65 20 6f 70 65 72 61 6e  all three operan
12dc4 64 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74  ds..**.** Comput
12dc5 61 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72  ation results ar
12dc6 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 74  e stored on a st
12dc7 61 63 6b 2e 20 20 45 61 63 68 20 65 6e 74 72 79  ack.  Each entry
12dc8 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b   on the.** stack
12dc9 20 69 73 20 65 69 74 68 65 72 20 61 6e 20 69 6e   is either an in
12dca 74 65 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65  teger, a null-te
12dcb 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c  rminated string,
12dcc 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
12dcd 74 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20  t.** number, or 
12dce 74 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76  the SQL "NULL" v
12dcf 61 6c 75 65 2e 20 20 41 6e 20 69 6e 70 6c 69 63  alue.  An inplic
12dd0 69 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72  it conversion fr
12dd1 6f 6d 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74  om one.** type t
12dd2 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75  o the other occu
12dd3 72 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  rs as necessary.
12dd4 0a 2a 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20  .** .** Most of 
12dd5 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  the code in this
12dd6 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 75   file is taken u
12dd7 70 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  p by the sqlite3
12dd8 56 64 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 75  VdbeExec().** fu
12dd9 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65  nction which doe
12dda 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e  s the work of in
12ddb 74 65 72 70 72 65 74 69 6e 67 20 61 20 56 44 42  terpreting a VDB
12ddc 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75  E program..** Bu
12ddd 74 20 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 73  t other routines
12dde 20 61 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 64   are also provid
12ddf 65 64 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 75  ed to help in bu
12de0 69 6c 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70  ilding up.** a p
12de1 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69  rogram instructi
12de2 6f 6e 20 62 79 20 69 6e 73 74 72 75 63 74 69 6f  on by instructio
12de3 6e 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73  n..**.** Various
12de4 20 73 63 72 69 70 74 73 20 73 63 61 6e 20 74 68   scripts scan th
12de5 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69  is source file i
12de6 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72  n order to gener
12de7 61 74 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75  ate HTML.** docu
12de8 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 65  mentation, heade
12de9 72 73 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68  rs files, or oth
12dea 65 72 20 64 65 72 69 76 65 64 20 66 69 6c 65 73  er derived files
12deb 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e  .  The formattin
12dec 67 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 65  g.** of the code
12ded 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
12dee 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 69 6d 70  , therefore, imp
12def 6f 72 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 68  ortant.  See oth
12df0 65 72 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69  er comments.** i
12df1 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20  n this file for 
12df2 64 65 74 61 69 6c 73 2e 20 20 49 66 20 69 6e 20  details.  If in 
12df3 64 6f 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65  doubt, do not de
12df4 76 69 61 74 65 20 66 72 6f 6d 20 65 78 69 73 74  viate from exist
12df5 69 6e 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e  ing.** commentin
12df6 67 20 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f  g and indentatio
12df7 6e 20 70 72 61 63 74 69 63 65 73 20 77 68 65 6e  n practices when
12df8 20 63 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 64   changing or add
12df9 69 6e 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ing code..**.** 
12dfa 24 49 64 3a 20 76 64 62 65 2e 63 2c 76 20 31 2e  $Id: vdbe.c,v 1.
12dfb 36 35 30 20 32 30 30 37 2f 30 39 2f 30 33 20 31  650 2007/09/03 1
12dfc 35 3a 31 39 3a 33 36 20 64 72 68 20 45 78 70 20  5:19:36 drh Exp 
12dfd 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
12dfe 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c  following global
12dff 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63   variable is inc
12e00 72 65 6d 65 6e 74 65 64 20 65 76 65 72 79 20 74  remented every t
12e01 69 6d 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20  ime a cursor.** 
12e02 6d 6f 76 65 73 2c 20 65 69 74 68 65 72 20 62 79  moves, either by
12e03 20 74 68 65 20 4f 50 5f 4d 6f 76 65 58 58 2c 20   the OP_MoveXX, 
12e04 4f 50 5f 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50  OP_Next, or OP_P
12e05 72 65 76 20 6f 70 63 6f 64 65 73 2e 20 20 54 68  rev opcodes.  Th
12e06 65 20 74 65 73 74 0a 2a 2a 20 70 72 6f 63 65 64  e test.** proced
12e07 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e  ures use this in
12e08 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
12e09 65 20 73 75 72 65 20 74 68 61 74 20 69 6e 64 69  e sure that indi
12e0a 63 65 73 20 61 72 65 0a 2a 2a 20 77 6f 72 6b 69  ces are.** worki
12e0b 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  ng correctly.  T
12e0c 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73  his variable has
12e0d 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68   no function oth
12e0e 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65  er than to.** he
12e0f 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f  lp verify the co
12e10 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
12e11 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a  of the library..
12e12 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
12e13 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
12e14 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61   int sqlite3_sea
12e15 72 63 68 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  rch_count = 0;.#
12e16 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  endif../*.** Whe
12e17 6e 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61  n this global va
12e18 72 69 61 62 6c 65 20 69 73 20 70 6f 73 69 74 69  riable is positi
12e19 76 65 2c 20 69 74 20 67 65 74 73 20 64 65 63 72  ve, it gets decr
12e1a 65 6d 65 6e 74 65 64 20 6f 6e 63 65 20 62 65 66  emented once bef
12e1b 6f 72 65 0a 2a 2a 20 65 61 63 68 20 69 6e 73 74  ore.** each inst
12e1c 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 56  ruction in the V
12e1d 44 42 45 2e 20 20 57 68 65 6e 20 72 65 61 63 68  DBE.  When reach
12e1e 65 73 20 7a 65 72 6f 2c 20 74 68 65 20 75 31 2e  es zero, the u1.
12e1f 69 73 49 6e 74 65 72 72 75 70 74 65 64 0a 2a 2a  isInterrupted.**
12e20 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71   field of the sq
12e21 6c 69 74 65 33 20 73 74 72 75 63 74 75 72 65 20  lite3 structure 
12e22 69 73 20 73 65 74 20 69 6e 20 6f 72 64 65 72 20  is set in order 
12e23 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 64 20  to simulate and 
12e24 69 6e 74 65 72 72 75 70 74 2e 0a 2a 2a 0a 2a 2a  interrupt..**.**
12e25 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 20 69   This facility i
12e26 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
12e27 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79  ng purposes only
12e28 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 66  .  It does not f
12e29 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 6e  unction.** in an
12e2a 20 6f 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e   ordinary build.
12e2b 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
12e2c 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
12e2d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e  I int sqlite3_in
12e2e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 20 3d 20  terrupt_count = 
12e2f 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
12e30 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c   The next global
12e31 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63   variable is inc
12e32 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 79  remented each ty
12e33 70 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f  pe the OP_Sort o
12e34 70 63 6f 64 65 0a 2a 2a 20 69 73 20 65 78 65 63  pcode.** is exec
12e35 75 74 65 64 2e 20 20 54 68 65 20 74 65 73 74 20  uted.  The test 
12e36 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74  procedures use t
12e37 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
12e38 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
12e39 74 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 69 73 20  t.** sorting is 
12e3a 6f 63 63 75 72 72 69 6e 67 20 6f 72 20 6e 6f 74  occurring or not
12e3b 20 6f 63 63 75 72 69 6e 67 20 61 74 20 61 70 70   occuring at app
12e3c 72 6f 70 72 69 61 74 65 20 74 69 6d 65 73 2e 20  ropriate times. 
12e3d 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 0a    This variable.
12e3e 2a 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69  ** has no functi
12e3f 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f  on other than to
12e40 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65   help verify the
12e41 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69   correct operati
12e42 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 62  on of the.** lib
12e43 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rary..*/.#ifdef 
12e44 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
12e45 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
12e46 65 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d 20  e3_sort_count = 
12e47 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
12e48 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c   The next global
12e49 20 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72 64   variable record
12e4a 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
12e4b 65 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42 6c  e largest MEM_Bl
12e4c 6f 62 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74 72  ob.** or MEM_Str
12e4d 20 74 68 61 74 20 68 61 73 20 61 70 70 65 61 72   that has appear
12e4e 65 64 20 6f 6e 20 74 68 65 20 56 44 42 45 20 73  ed on the VDBE s
12e4f 74 61 63 6b 2e 20 20 54 68 65 20 74 65 73 74 20  tack.  The test 
12e50 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 75 73  procedures.** us
12e51 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  e this informati
12e52 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  on to make sure 
12e53 74 68 61 74 20 74 68 65 20 7a 65 72 6f 2d 62 6c  that the zero-bl
12e54 6f 62 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  ob functionality
12e55 0a 2a 2a 20 69 73 20 77 6f 72 6b 69 6e 67 20 63  .** is working c
12e56 6f 72 72 65 63 74 6c 79 2e 20 20 20 54 68 69 73  orrectly.   This
12e57 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f   variable has no
12e58 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20   function other 
12e59 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20  than to.** help 
12e5a 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65  verify the corre
12e5b 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  ct operation of 
12e5c 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  the library..*/.
12e5d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
12e5e 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
12e5f 74 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c  t sqlite3_max_bl
12e60 6f 62 73 69 7a 65 20 3d 20 30 3b 0a 23 65 6e 64  obsize = 0;.#end
12e61 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  if../*.** Releas
12e62 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 73 73  e the memory ass
12e63 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
12e64 20 67 69 76 65 6e 20 73 74 61 63 6b 20 6c 65 76   given stack lev
12e65 65 6c 2e 20 20 54 68 69 73 0a 2a 2a 20 6c 65 61  el.  This.** lea
12e66 76 65 73 20 74 68 65 20 4d 65 6d 2e 66 6c 61 67  ves the Mem.flag
12e67 73 20 66 69 65 6c 64 20 69 6e 20 61 6e 20 69 6e  s field in an in
12e68 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65  consistent state
12e69 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 65 6c  ..*/.#define Rel
12e6a 65 61 73 65 28 50 29 20 69 66 28 28 50 29 2d 3e  ease(P) if((P)->
12e6b 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 7b 20  flags&MEM_Dyn){ 
12e6c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
12e6d 6c 65 61 73 65 28 50 29 3b 20 7d 0a 0a 2f 2a 0a  lease(P); }../*.
12e6e 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 67  ** Convert the g
12e6f 69 76 65 6e 20 73 74 61 63 6b 20 65 6e 74 69 74  iven stack entit
12e70 79 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20  y into a string 
12e71 69 66 20 69 74 20 69 73 6e 27 74 20 6f 6e 65 0a  if it isn't one.
12e72 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 65 74 75  ** already. Retu
12e73 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61  rn non-zero if a
12e74 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e   malloc() fails.
12e75 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 74 72 69  .*/.#define Stri
12e76 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 20 5c 0a  ngify(P, enc) \.
12e77 20 20 20 69 66 28 28 28 50 29 2d 3e 66 6c 61 67     if(((P)->flag
12e78 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  s&(MEM_Str|MEM_B
12e79 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 71 6c 69  lob))==0 && sqli
12e7a 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67  te3VdbeMemString
12e7b 69 66 79 28 50 2c 65 6e 63 29 29 20 5c 0a 20 20  ify(P,enc)) \.  
12e7c 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d     { goto no_mem
12e7d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  ; }../*.** The h
12e7e 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72  eader of a recor
12e7f 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  d consists of a 
12e80 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c  sequence variabl
12e81 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
12e82 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65  s..** These inte
12e83 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20  gers are almost 
12e84 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64  always small and
12e85 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20   are encoded as 
12e86 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a  a single byte..*
12e87 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
12e88 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 64 76 61  macro takes adva
12e89 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20  ntage this fact 
12e8a 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73  to provide a fas
12e8b 74 20 64 65 63 6f 64 65 0a 2a 2a 20 6f 66 20 74  t decode.** of t
12e8c 68 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20 61  he integers in a
12e8d 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 2e 20   record header. 
12e8e 20 49 74 20 69 73 20 66 61 73 74 65 72 20 66 6f   It is faster fo
12e8f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73  r the common cas
12e90 65 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 69  e.** where the i
12e91 6e 74 65 67 65 72 20 69 73 20 61 20 73 69 6e 67  nteger is a sing
12e92 6c 65 20 62 79 74 65 2e 20 20 49 74 20 69 73 20  le byte.  It is 
12e93 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 20  a little slower 
12e94 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e 74 65  when the.** inte
12e95 67 65 72 20 69 73 20 74 77 6f 20 6f 72 20 6d 6f  ger is two or mo
12e96 72 65 20 62 79 74 65 73 2e 20 20 42 75 74 20 6f  re bytes.  But o
12e97 76 65 72 61 6c 6c 20 69 74 20 69 73 20 66 61 73  verall it is fas
12e98 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ter..**.** The f
12e99 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 73  ollowing express
12e9a 69 6f 6e 73 20 61 72 65 20 65 71 75 69 76 61 6c  ions are equival
12e9b 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78  ent:.**.**     x
12e9c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72   = sqlite3GetVar
12e9d 69 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b 0a  int32( A, &B );.
12e9e 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 47 65  **.**     x = Ge
12e9f 74 56 61 72 69 6e 74 28 20 41 2c 20 42 20 29 3b  tVarint( A, B );
12ea0 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 47  .**.*/.#define G
12ea1 65 74 56 61 72 69 6e 74 28 41 2c 42 29 20 20 28  etVarint(A,B)  (
12ea2 28 42 20 3d 20 2a 28 41 29 29 3c 3d 30 78 37 66  (B = *(A))<=0x7f
12ea3 20 3f 20 31 20 3a 20 73 71 6c 69 74 65 33 47 65   ? 1 : sqlite3Ge
12ea4 74 56 61 72 69 6e 74 33 32 28 41 2c 20 26 42 29  tVarint32(A, &B)
12ea5 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 65 70 68 65  )../*.** An ephe
12ea6 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 76 61 6c  meral string val
12ea7 75 65 20 28 73 69 67 6e 69 66 69 65 64 20 62 79  ue (signified by
12ea8 20 74 68 65 20 4d 45 4d 5f 45 70 68 65 6d 20 66   the MEM_Ephem f
12ea9 6c 61 67 29 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  lag) contains.**
12eaa 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
12eab 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
12eac 63 61 74 65 64 20 73 74 72 69 6e 67 20 77 68 65  cated string whe
12ead 72 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 6e  re some other en
12eae 74 69 74 79 0a 2a 2a 20 69 73 20 72 65 73 70 6f  tity.** is respo
12eaf 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c  nsible for deall
12eb0 6f 63 61 74 69 6e 67 20 74 68 61 74 20 73 74 72  ocating that str
12eb1 69 6e 67 2e 20 20 42 65 63 61 75 73 65 20 74 68  ing.  Because th
12eb2 65 20 73 74 61 63 6b 20 65 6e 74 72 79 0a 2a 2a  e stack entry.**
12eb3 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 6f   does not contro
12eb4 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 69 74  l the string, it
12eb5 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 74 65   might be delete
12eb6 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 73 74  d without the st
12eb7 61 63 6b 0a 2a 2a 20 65 6e 74 72 79 20 6b 6e 6f  ack.** entry kno
12eb8 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54  wing it..**.** T
12eb9 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76  his routine conv
12eba 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61  erts an ephemera
12ebb 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20  l string into a 
12ebc 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
12ebd 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20  cated.** string 
12ebe 74 68 61 74 20 74 68 65 20 73 74 61 63 6b 20 65  that the stack e
12ebf 6e 74 72 79 20 69 74 73 65 6c 66 20 63 6f 6e 74  ntry itself cont
12ec0 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20  rols.  In other 
12ec1 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e  words, it.** con
12ec2 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68  verts an MEM_Eph
12ec3 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61  em string into a
12ec4 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67  n MEM_Dyn string
12ec5 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65  ..*/.#define Dee
12ec6 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c  phemeralize(P) \
12ec7 0a 20 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c  .   if( ((P)->fl
12ec8 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d  ags&MEM_Ephem)!=
12ec9 30 20 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71  0 \.       && sq
12eca 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
12ecb 57 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20  Writeable(P) ){ 
12ecc 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f  goto no_mem;}../
12ecd 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
12ece 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
12ecf 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70  ob() on the supp
12ed0 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65  lied value (type
12ed1 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72   Mem*).** P if r
12ed2 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66  equired..*/.#def
12ed3 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50  ine ExpandBlob(P
12ed4 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d  ) (((P)->flags&M
12ed5 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33  EM_Zero)?sqlite3
12ed6 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
12ed7 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41  b(P):0)../*.** A
12ed8 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69  rgument pMem poi
12ed9 6e 74 73 20 61 74 20 61 20 6d 65 6d 6f 72 79 20  nts at a memory 
12eda 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62  cell that will b
12edb 65 20 70 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a  e passed to a.**
12edc 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75   user-defined fu
12edd 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e  nction or return
12ede 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 61  ed to the user a
12edf 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  s the result of 
12ee0 61 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20  a query..** The 
12ee1 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
12ee2 20 27 64 62 5f 65 6e 63 27 20 69 73 20 74 68 65   'db_enc' is the
12ee3 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75   text encoding u
12ee4 73 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20  sed by the vdbe 
12ee5 66 6f 72 0a 2a 2a 20 73 74 61 63 6b 20 76 61 72  for.** stack var
12ee6 69 61 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f  iables.  This ro
12ee7 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70  utine sets the p
12ee8 4d 65 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65  Mem->enc and pMe
12ee9 6d 2d 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61  m->type.** varia
12eea 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 65  bles used by the
12eeb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a   sqlite3_value_*
12eec 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  () routines..*/.
12eed 23 64 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70  #define storeTyp
12eee 65 49 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72  eInfo(A,B) _stor
12eef 65 54 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61  eTypeInfo(A).sta
12ef0 74 69 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54  tic void _storeT
12ef1 79 70 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65  ypeInfo(Mem *pMe
12ef2 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20  m){.  int flags 
12ef3 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20  = pMem->flags;. 
12ef4 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
12ef5 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65  _Null ){.    pMe
12ef6 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
12ef7 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73  _NULL;.  }.  els
12ef8 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45  e if( flags & ME
12ef9 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65  M_Int ){.    pMe
12efa 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
12efb 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20  _INTEGER;.  }.  
12efc 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
12efd 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20   MEM_Real ){.   
12efe 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
12eff 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a  LITE_FLOAT;.  }.
12f00 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73    else if( flags
12f01 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20   & MEM_Str ){.  
12f02 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
12f03 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65  QLITE_TEXT;.  }e
12f04 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  lse{.    pMem->t
12f05 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ype = SQLITE_BLO
12f06 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  B;.  }.}../*.** 
12f07 50 6f 70 20 74 68 65 20 73 74 61 63 6b 20 4e 20  Pop the stack N 
12f08 74 69 6d 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  times..*/.static
12f09 20 76 6f 69 64 20 70 6f 70 53 74 61 63 6b 28 4d   void popStack(M
12f0a 65 6d 20 2a 2a 70 70 54 6f 73 2c 20 69 6e 74 20  em **ppTos, int 
12f0b 4e 29 7b 0a 20 20 4d 65 6d 20 2a 70 54 6f 73 20  N){.  Mem *pTos 
12f0c 3d 20 2a 70 70 54 6f 73 3b 0a 20 20 77 68 69 6c  = *ppTos;.  whil
12f0d 65 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 4e 2d  e( N>0 ){.    N-
12f0e 2d 3b 0a 20 20 20 20 52 65 6c 65 61 73 65 28 70  -;.    Release(p
12f0f 54 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d 2d  Tos);.    pTos--
12f10 3b 0a 20 20 7d 0a 20 20 2a 70 70 54 6f 73 20 3d  ;.  }.  *ppTos =
12f11 20 70 54 6f 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pTos;.}../*.** 
12f12 41 6c 6c 6f 63 61 74 65 20 63 75 72 73 6f 72 20  Allocate cursor 
12f13 6e 75 6d 62 65 72 20 69 43 75 72 2e 20 20 52 65  number iCur.  Re
12f14 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
12f15 6f 20 69 74 2e 20 20 52 65 74 75 72 6e 20 4e 55  o it.  Return NU
12f16 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 72 75 6e 20  LL.** if we run 
12f17 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a  out of memory..*
12f18 2f 0a 73 74 61 74 69 63 20 43 75 72 73 6f 72 20  /.static Cursor 
12f19 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28  *allocateCursor(
12f1a 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 43 75  Vdbe *p, int iCu
12f1b 72 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 43  r, int iDb){.  C
12f1c 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 61 73  ursor *pCx;.  as
12f1d 73 65 72 74 28 20 69 43 75 72 3c 70 2d 3e 6e 43  sert( iCur<p->nC
12f1e 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70  ursor );.  if( p
12f1f 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 29 7b  ->apCsr[iCur] ){
12f20 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
12f21 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 2d  FreeCursor(p, p-
12f22 3e 61 70 43 73 72 5b 69 43 75 72 5d 29 3b 0a 20  >apCsr[iCur]);. 
12f23 20 7d 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 43   }.  p->apCsr[iC
12f24 75 72 5d 20 3d 20 70 43 78 20 3d 20 73 71 6c 69  ur] = pCx = sqli
12f25 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
12f26 69 7a 65 6f 66 28 43 75 72 73 6f 72 29 20 29 3b  izeof(Cursor) );
12f27 0a 20 20 69 66 28 20 70 43 78 20 29 7b 0a 20 20  .  if( pCx ){.  
12f28 20 20 70 43 78 2d 3e 69 44 62 20 3d 20 69 44 62    pCx->iDb = iDb
12f29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
12f2a 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79  Cx;.}../*.** Try
12f2b 20 74 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61   to convert a va
12f2c 6c 75 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72  lue into a numer
12f2d 69 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ic representatio
12f2e 6e 20 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64  n if we can.** d
12f2f 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73  o so without los
12f30 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  s of information
12f31 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
12f32 73 2c 20 69 66 20 74 68 65 20 73 74 72 69 6e 67  s, if the string
12f33 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61  .** looks like a
12f34 20 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74   number, convert
12f35 20 69 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65   it into a numbe
12f36 72 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e  r.  If it does n
12f37 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20  ot.** look like 
12f38 61 20 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20  a number, leave 
12f39 69 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61  it alone..*/.sta
12f3a 74 69 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75  tic void applyNu
12f3b 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65  mericAffinity(Me
12f3c 6d 20 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20  m *pRec){.  if( 
12f3d 28 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28  (pRec->flags & (
12f3e 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74  MEM_Real|MEM_Int
12f3f 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ))==0 ){.    int
12f40 20 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71   realnum;.    sq
12f41 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54  lite3VdbeMemNulT
12f42 65 72 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a  erminate(pRec);.
12f43 20 20 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66      if( (pRec->f
12f44 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20  lags&MEM_Str).  
12f45 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
12f46 33 49 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e  3IsNumber(pRec->
12f47 7a 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65  z, &realnum, pRe
12f48 63 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20  c->enc) ){.     
12f49 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20   i64 value;.    
12f4a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
12f4b 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63  ngeEncoding(pRec
12f4c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
12f4d 20 20 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e        if( !realn
12f4e 75 6d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f  um && sqlite3Ato
12f4f 69 36 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61  i64(pRec->z, &va
12f50 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  lue) ){.        
12f51 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
12f52 6c 65 61 73 65 28 70 52 65 63 29 3b 0a 20 20 20  lease(pRec);.   
12f53 20 20 20 20 20 70 52 65 63 2d 3e 75 2e 69 20 3d       pRec->u.i =
12f54 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20   value;.        
12f55 70 52 65 63 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pRec->flags = ME
12f56 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c  M_Int;.      }el
12f57 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
12f58 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66  te3VdbeMemRealif
12f59 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20 20 7d  y(pRec);.      }
12f5a 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
12f5b 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 69  .** Processing i
12f5c 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79 20 74  s determine by t
12f5d 68 65 20 61 66 66 69 6e 69 74 79 20 70 61 72 61  he affinity para
12f5e 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c  meter:.**.** SQL
12f5f 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a  ITE_AFF_INTEGER:
12f60 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52  .** SQLITE_AFF_R
12f61 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41  EAL:.** SQLITE_A
12f62 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20  FF_NUMERIC:.**  
12f63 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74    Try to convert
12f64 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e 74 65   pRec to an inte
12f65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ger representati
12f66 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66  on or a .**    f
12f67 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65  loating-point re
12f68 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20  presentation if 
12f69 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65  an integer repre
12f6a 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20  sentation.**    
12f6b 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e  is not possible.
12f6c 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20    Note that the 
12f6d 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e  integer represen
12f6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20  tation is.**    
12f6f 61 6c 77 61 79 73 20 70 72 65 66 65 72 72 65 64  always preferred
12f70 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 61 66  , even if the af
12f71 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c 2c 20  finity is REAL, 
12f72 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e  because.**    an
12f73 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65   integer represe
12f74 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20  ntation is more 
12f75 73 70 61 63 65 20 65 66 66 69 63 69 65 6e 74 20  space efficient 
12f76 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53  on disk..**.** S
12f77 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a  QLITE_AFF_TEXT:.
12f78 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 70 52  **    Convert pR
12f79 65 63 20 74 6f 20 61 20 74 65 78 74 20 72 65 70  ec to a text rep
12f7a 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a  resentation..**.
12f7b 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  ** SQLITE_AFF_NO
12f7c 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e  NE:.**    No-op.
12f7d 20 20 70 52 65 63 20 69 73 20 75 6e 63 68 61 6e    pRec is unchan
12f7e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ged..*/.static v
12f7f 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e 69 74  oid applyAffinit
12f80 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20  y(.  Mem *pRec, 
12f81 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
12f82 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 20 61  value to apply a
12f83 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20  ffinity to */.  
12f84 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c 20 20  char affinity,  
12f85 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e      /* The affin
12f86 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65  ity to be applie
12f87 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 20 20  d */.  u8 enc   
12f88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
12f89 65 20 74 68 69 73 20 74 65 78 74 20 65 6e 63 6f  e this text enco
12f8a 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  ding */.){.  if(
12f8b 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
12f8c 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a 20 20  E_AFF_TEXT ){.  
12f8d 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70    /* Only attemp
12f8e 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  t the conversion
12f8f 20 74 6f 20 54 45 58 54 20 69 66 20 74 68 65 72   to TEXT if ther
12f90 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  e is an integer 
12f91 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72  or real.    ** r
12f92 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 28 62  epresentation (b
12f93 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20  lob and NULL do 
12f94 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 74 65  not get converte
12f95 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69 6e 67  d) but no string
12f96 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e  .    ** represen
12f97 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  tation..    */. 
12f98 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65 63 2d     if( 0==(pRec-
12f99 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20  >flags&MEM_Str) 
12f9a 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26  && (pRec->flags&
12f9b 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e  (MEM_Real|MEM_In
12f9c 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  t)) ){.      sql
12f9d 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e  ite3VdbeMemStrin
12f9e 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63 29 3b  gify(pRec, enc);
12f9f 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 63 2d  .    }.    pRec-
12fa0 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
12fa1 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Real|MEM_Int);. 
12fa2 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66 69 6e   }else if( affin
12fa3 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity!=SQLITE_AFF_
12fa4 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73 65  NONE ){.    asse
12fa5 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51  rt( affinity==SQ
12fa6 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
12fa7 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51   || affinity==SQ
12fa8 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20  LITE_AFF_REAL.  
12fa9 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 61 66             || af
12faa 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
12fab 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20  FF_NUMERIC );.  
12fac 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66    applyNumericAf
12fad 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20  finity(pRec);.  
12fae 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67    if( pRec->flag
12faf 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a  s & MEM_Real ){.
12fb0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
12fb1 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79  eIntegerAffinity
12fb2 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20  (pRec);.    }.  
12fb3 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  }.}../*.** Try t
12fb4 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 74 79  o convert the ty
12fb5 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e  pe of a function
12fb6 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 20 72   argument or a r
12fb7 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  esult column.** 
12fb8 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72  into a numeric r
12fb9 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20  epresentation.  
12fba 55 73 65 20 65 69 74 68 65 72 20 49 4e 54 45 47  Use either INTEG
12fbb 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69 63 68  ER or REAL which
12fbc 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f  ever.** is appro
12fbd 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c  priate.  But onl
12fbe 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 72 73  y do the convers
12fbf 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70 6f 73  ion if it is pos
12fc0 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a  sible without.**
12fc1 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61   loss of informa
12fc2 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 20  tion and return 
12fc3 74 68 65 20 72 65 76 69 73 65 64 20 74 79 70 65  the revised type
12fc4 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   of the argument
12fc5 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
12fc6 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  an EXPERIMENTAL 
12fc7 61 70 69 20 61 6e 64 20 69 73 20 73 75 62 6a 65  api and is subje
12fc8 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 20  ct to change or 
12fc9 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49  removal..*/.SQLI
12fca 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
12fcb 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63  e3_value_numeric
12fcc 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61  _type(sqlite3_va
12fcd 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65  lue *pVal){.  Me
12fce 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29  m *pMem = (Mem*)
12fcf 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d  pVal;.  applyNum
12fd0 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 4d 65  ericAffinity(pMe
12fd1 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79 70 65 49  m);.  storeTypeI
12fd2 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a 20 20  nfo(pMem, 0);.  
12fd3 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70  return pMem->typ
12fd4 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f  e;.}../*.** Expo
12fd5 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  rted version of 
12fd6 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29 2e  applyAffinity().
12fd7 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20   This one works 
12fd8 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  on sqlite3_value
12fd9 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69  *, .** not the i
12fda 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70  nternal Mem* typ
12fdb 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
12fdc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
12fdd 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e  3ValueApplyAffin
12fde 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ity(.  sqlite3_v
12fdf 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75  alue *pVal, .  u
12fe0 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 20 75  8 affinity, .  u
12fe1 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79  8 enc.){.  apply
12fe2 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29  Affinity((Mem *)
12fe3 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20  pVal, affinity, 
12fe4 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  enc);.}..#ifdef 
12fe5 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
12fe6 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63 65 20  ** Write a nice 
12fe7 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
12fe8 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e  ation of the con
12fe9 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d  tents of cell pM
12fea 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65  em.** into buffe
12feb 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e  r zBuf, length n
12fec 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Buf..*/.SQLITE_P
12fed 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
12fee 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79  te3VdbeMemPretty
12fef 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c  Print(Mem *pMem,
12ff0 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20   char *zBuf){.  
12ff1 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75  char *zCsr = zBu
12ff2 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d 65  f;.  int f = pMe
12ff3 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61  m->flags;..  sta
12ff4 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
12ff5 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d  const encnames[]
12ff6 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 29 22   = {"(X)", "(8)"
12ff7 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28 31 36  , "(16LE)", "(16
12ff8 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26  BE)"};..  if( f&
12ff9 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20  MEM_Blob ){.    
12ffa 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20  int i;.    char 
12ffb 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d  c;.    if( f & M
12ffc 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20  EM_Dyn ){.      
12ffd 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61  c = 'z';.      a
12ffe 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
12fff 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65  _Static|MEM_Ephe
13000 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65  m))==0 );.    }e
13001 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f  lse if( f & MEM_
13002 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20  Static ){.      
13003 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61  c = 't';.      a
13004 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
13005 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29  _Dyn|MEM_Ephem))
13006 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ==0 );.    }else
13007 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68   if( f & MEM_Eph
13008 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20  em ){.      c = 
13009 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72  'e';.      asser
1300a 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61  t( (f & (MEM_Sta
1300b 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30  tic|MEM_Dyn))==0
1300c 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   );.    }else{. 
1300d 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a 20 20       c = 's';.  
1300e 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
1300f 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a  _snprintf(100, z
13010 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20  Csr, "%c", c);. 
13011 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 6c 65     zCsr += strle
13012 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 73 71 6c  n(zCsr);.    sql
13013 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
13014 30 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20  0, zCsr, "%d[", 
13015 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43  pMem->n);.    zC
13016 73 72 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73  sr += strlen(zCs
13017 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  r);.    for(i=0;
13018 20 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d   i<16 && i<pMem-
13019 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  >n; i++){.      
1301a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1301b 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30 32  (100, zCsr, "%02
1301c 58 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e  X", ((int)pMem->
1301d 7a 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a 20  z[i] & 0xFF));. 
1301e 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72       zCsr += str
1301f 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d  len(zCsr);.    }
13020 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
13021 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b  16 && i<pMem->n;
13022 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61   i++){.      cha
13023 72 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d  r z = pMem->z[i]
13024 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32  ;.      if( z<32
13025 20 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73   || z>126 ) *zCs
13026 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20  r++ = '.';.     
13027 20 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20   else *zCsr++ = 
13028 7a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  z;.    }..    sq
13029 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
1302a 30 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c  00, zCsr, "]%s",
1302b 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e   encnames[pMem->
1302c 65 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20  enc]);.    zCsr 
1302d 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29 3b  += strlen(zCsr);
1302e 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d  .    if( f & MEM
1302f 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 73  _Zero ){.      s
13030 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
13031 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 6c 6c 64  100, zCsr,"+%lld
13032 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20  z",pMem->u.i);. 
13033 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72       zCsr += str
13034 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d  len(zCsr);.    }
13035 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c 30  .    *zCsr = '\0
13036 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  ';.  }else if( f
13037 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20   & MEM_Str ){.  
13038 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 20    int j, k;.    
13039 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20  zBuf[0] = ' ';. 
1303a 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44     if( f & MEM_D
1303b 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66  yn ){.      zBuf
1303c 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20  [1] = 'z';.     
1303d 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
1303e 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70  EM_Static|MEM_Ep
1303f 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  hem))==0 );.    
13040 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45  }else if( f & ME
13041 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  M_Static ){.    
13042 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 3b    zBuf[1] = 't';
13043 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
13044 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  f & (MEM_Dyn|MEM
13045 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20  _Ephem))==0 );. 
13046 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26     }else if( f &
13047 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20   MEM_Ephem ){.  
13048 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 65      zBuf[1] = 'e
13049 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ';.      assert(
1304a 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69   (f & (MEM_Stati
1304b 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29  c|MEM_Dyn))==0 )
1304c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1304d 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 27     zBuf[1] = 's'
1304e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d 20  ;.    }.    k = 
1304f 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  2;.    sqlite3_s
13050 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a 42  nprintf(100, &zB
13051 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d 65  uf[k], "%d", pMe
13052 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20  m->n);.    k += 
13053 73 74 72 6c 65 6e 28 26 7a 42 75 66 5b 6b 5d 29  strlen(&zBuf[k])
13054 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20  ;.    zBuf[k++] 
13055 3d 20 27 5b 27 3b 0a 20 20 20 20 66 6f 72 28 6a  = '[';.    for(j
13056 3d 30 3b 20 6a 3c 31 35 20 26 26 20 6a 3c 70 4d  =0; j<15 && j<pM
13057 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  em->n; j++){.   
13058 20 20 20 75 38 20 63 20 3d 20 70 4d 65 6d 2d 3e     u8 c = pMem->
13059 7a 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  z[j];.      if( 
1305a 63 3e 3d 30 78 32 30 20 26 26 20 63 3c 30 78 37  c>=0x20 && c<0x7
1305b 66 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  f ){.        zBu
1305c 66 5b 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20 20  f[k++] = c;.    
1305d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1305e 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27   zBuf[k++] = '.'
1305f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
13060 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
13061 27 5d 27 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ']';.    sqlite3
13062 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 26 7a  _snprintf(100,&z
13063 42 75 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73  Buf[k], encnames
13064 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20  [pMem->enc]);.  
13065 20 20 6b 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a    k += strlen(&z
13066 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75  Buf[k]);.    zBu
13067 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a  f[k++] = 0;.  }.
13068 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65  }.#endif...#ifde
13069 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 2f  f VDBE_PROFILE./
1306a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1306b 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  ng routine only 
1306c 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d  works on pentium
1306d 2d 63 6c 61 73 73 20 70 72 6f 63 65 73 73 6f 72  -class processor
1306e 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68  s..** It uses th
1306f 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74  e RDTSC opcode t
13070 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65  o read the cycle
13071 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74   count value out
13072 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65   of the.** proce
13073 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73  ssor and returns
13074 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68   that value.  Th
13075 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  is can be used f
13076 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70  or high-res.** p
13077 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 5f 5f 69  rofiling..*/.__i
13078 6e 6c 69 6e 65 5f 5f 20 75 6e 73 69 67 6e 65 64  nline__ unsigned
13079 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 68   long long int h
1307a 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 75  wtime(void){.  u
1307b 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
1307c 67 20 69 6e 74 20 78 3b 0a 20 20 5f 5f 61 73 6d  g int x;.  __asm
1307d 5f 5f 28 22 72 64 74 73 63 5c 6e 5c 74 22 0a 20  __("rdtsc\n\t". 
1307e 20 20 20 20 20 20 20 20 20 22 6d 6f 76 20 25 25           "mov %%
1307f 65 64 78 2c 20 25 25 65 63 78 5c 6e 5c 74 22 0a  edx, %%ecx\n\t".
13080 20 20 20 20 20 20 20 20 20 20 3a 22 3d 41 22 20            :"=A" 
13081 28 78 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 78  (x));.  return x
13082 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
13083 2a 20 54 68 65 20 43 48 45 43 4b 5f 46 4f 52 5f  * The CHECK_FOR_
13084 49 4e 54 45 52 52 55 50 54 20 6d 61 63 72 6f 20  INTERRUPT macro 
13085 64 65 66 69 6e 65 64 20 68 65 72 65 20 6c 6f 6f  defined here loo
13086 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ks to see if the
13087 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  .** sqlite3_inte
13088 72 72 75 70 74 28 29 20 72 6f 75 74 69 6e 65 20  rrupt() routine 
13089 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e  has been called.
1308a 20 20 49 66 20 69 74 20 68 61 73 20 62 65 65 6e    If it has been
1308b 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 6f 63 65 73  , then.** proces
1308c 73 69 6e 67 20 6f 66 20 74 68 65 20 56 44 42 45  sing of the VDBE
1308d 20 70 72 6f 67 72 61 6d 20 69 73 20 69 6e 74 65   program is inte
1308e 72 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  rrupted..**.** T
1308f 68 69 73 20 6d 61 63 72 6f 20 61 64 64 65 64 20  his macro added 
13090 74 6f 20 65 76 65 72 79 20 69 6e 73 74 72 75 63  to every instruc
13091 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 61  tion that does a
13092 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65 72 20 74   jump in order t
13093 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 61  o.** implement a
13094 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20 74 65 73   loop.  This tes
13095 74 20 75 73 65 64 20 74 6f 20 62 65 20 6f 6e 20  t used to be on 
13096 65 76 65 72 79 20 73 69 6e 67 6c 65 20 69 6e 73  every single ins
13097 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74  truction,.** but
13098 20 74 68 61 74 20 6d 65 61 6e 74 20 77 65 20 6d   that meant we m
13099 6f 72 65 20 74 65 73 74 69 6e 67 20 74 68 61 74  ore testing that
1309a 20 77 65 20 6e 65 65 64 65 64 2e 20 20 42 79 20   we needed.  By 
1309b 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20 74 68 65  only testing the
1309c 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a 75 6d 70  .** flag on jump
1309d 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2c 20 77   instructions, w
1309e 65 20 67 65 74 20 61 20 28 73 6d 61 6c 6c 29 20  e get a (small) 
1309f 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e  speed improvemen
130a0 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48  t..*/.#define CH
130a1 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50  ECK_FOR_INTERRUP
130a2 54 20 5c 0a 20 20 20 69 66 28 20 64 62 2d 3e 75  T \.   if( db->u
130a3 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
130a4 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
130a5 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3b 0a 0a  _to_interrupt;..
130a6 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61  ./*.** Execute a
130a7 73 20 6d 75 63 68 20 6f 66 20 61 20 56 44 42 45  s much of a VDBE
130a8 20 70 72 6f 67 72 61 6d 20 61 73 20 77 65 20 63   program as we c
130a9 61 6e 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a  an then return..
130aa 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  **.** sqlite3Vdb
130ab 65 4d 61 6b 65 52 65 61 64 79 28 29 20 6d 75 73  eMakeReady() mus
130ac 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f  t be called befo
130ad 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  re this routine 
130ae 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63  in order to.** c
130af 6c 6f 73 65 20 74 68 65 20 70 72 6f 67 72 61 6d  lose the program
130b0 20 77 69 74 68 20 61 20 66 69 6e 61 6c 20 4f 50   with a final OP
130b1 5f 48 61 6c 74 20 61 6e 64 20 74 6f 20 73 65 74  _Halt and to set
130b2 20 75 70 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   up the callback
130b3 73 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 72 72  s.** and the err
130b4 6f 72 20 6d 65 73 73 61 67 65 20 70 6f 69 6e 74  or message point
130b5 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65 76  er..**.** Whenev
130b6 65 72 20 61 20 72 6f 77 20 6f 72 20 72 65 73 75  er a row or resu
130b7 6c 74 20 64 61 74 61 20 69 73 20 61 76 61 69 6c  lt data is avail
130b8 61 62 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69  able, this routi
130b9 6e 65 20 77 69 6c 6c 20 65 69 74 68 65 72 0a 2a  ne will either.*
130ba 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 65 73  * invoke the res
130bb 75 6c 74 20 63 61 6c 6c 62 61 63 6b 20 28 69 66  ult callback (if
130bc 20 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f   there is one) o
130bd 72 20 72 65 74 75 72 6e 20 77 69 74 68 0a 2a 2a  r return with.**
130be 20 53 51 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a 0a   SQLITE_ROW..**.
130bf 2a 2a 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74  ** If an attempt
130c0 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e   is made to open
130c1 20 61 20 6c 6f 63 6b 65 64 20 64 61 74 61 62 61   a locked databa
130c2 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  se, then this ro
130c3 75 74 69 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65 69  utine.** will ei
130c4 74 68 65 72 20 69 6e 76 6f 6b 65 20 74 68 65 20  ther invoke the 
130c5 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 28 69  busy callback (i
130c6 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20  f there is one) 
130c7 6f 72 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 72 65  or it will.** re
130c8 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
130c9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72  ..**.** If an er
130ca 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 65  ror occurs, an e
130cb 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20  rror message is 
130cc 77 72 69 74 74 65 6e 20 74 6f 20 6d 65 6d 6f 72  written to memor
130cd 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  y obtained.** fr
130ce 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
130cf 63 28 29 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d  c() and p->zErrM
130d0 73 67 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f  sg is made to po
130d1 69 6e 74 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f  int to that memo
130d2 72 79 2e 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72  ry..** The error
130d3 20 63 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20   code is stored 
130d4 69 6e 20 70 2d 3e 72 63 20 61 6e 64 20 74 68 69  in p->rc and thi
130d5 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
130d6 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a  s SQLITE_ERROR..
130d7 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c  **.** If the cal
130d8 6c 62 61 63 6b 20 65 76 65 72 20 72 65 74 75 72  lback ever retur
130d9 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  ns non-zero, the
130da 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20 65 78  n the program ex
130db 69 74 73 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65  its.** immediate
130dc 6c 79 2e 20 20 54 68 65 72 65 20 77 69 6c 6c 20  ly.  There will 
130dd 62 65 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73  be no error mess
130de 61 67 65 20 62 75 74 20 74 68 65 20 70 2d 3e 72  age but the p->r
130df 63 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65  c field is.** se
130e0 74 20 74 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52  t to SQLITE_ABOR
130e1 54 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69  T and this routi
130e2 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53  ne will return S
130e3 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a  QLITE_ERROR..**.
130e4 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ** A memory allo
130e5 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 63 61 75  cation error cau
130e6 73 65 73 20 70 2d 3e 72 63 20 74 6f 20 62 65 20  ses p->rc to be 
130e7 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f  set to SQLITE_NO
130e8 4d 45 4d 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20  MEM and this.** 
130e9 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 74 75 72  routine to retur
130ea 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a  n SQLITE_ERROR..
130eb 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61  **.** Other fata
130ec 6c 20 65 72 72 6f 72 73 20 72 65 74 75 72 6e 20  l errors return 
130ed 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a  SQLITE_ERROR..**
130ee 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72  .** After this r
130ef 6f 75 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73  outine has finis
130f0 68 65 64 2c 20 73 71 6c 69 74 65 33 56 64 62 65  hed, sqlite3Vdbe
130f1 46 69 6e 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c  Finalize() shoul
130f2 64 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20  d be.** used to 
130f3 63 6c 65 61 6e 20 75 70 20 74 68 65 20 6d 65 73  clean up the mes
130f4 73 20 74 68 61 74 20 77 61 73 20 6c 65 66 74 20  s that was left 
130f5 62 65 68 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54  behind..*/.SQLIT
130f6 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
130f7 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 0a 20  lite3VdbeExec(. 
130f8 20 56 64 62 65 20 2a 70 20 20 20 20 20 20 20 20   Vdbe *p        
130f9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
130fa 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20  he VDBE */.){.  
130fb 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20  int pc;         
130fc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
130fd 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  e program counte
130fe 72 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20  r */.  Op *pOp; 
130ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13100 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65    /* Current ope
13101 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ration */.  int 
13102 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
13103 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
13104 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73  to return */.  s
13105 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
13106 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  db;       /* The
13107 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75   database */.  u
13108 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43  8 encoding = ENC
13109 28 64 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65  (db);     /* The
1310a 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69   database encodi
1310b 6e 67 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 54 6f  ng */.  Mem *pTo
1310c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
1310d 20 20 20 2f 2a 20 54 6f 70 20 65 6e 74 72 79 20     /* Top entry 
1310e 69 6e 20 74 68 65 20 6f 70 65 72 61 6e 64 20 73  in the operand s
1310f 74 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66 20 56  tack */.#ifdef V
13110 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 75 6e  DBE_PROFILE.  un
13111 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
13112 20 73 74 61 72 74 3b 20 20 2f 2a 20 43 50 55 20   start;  /* CPU 
13113 63 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 74 20 73  clock count at s
13114 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a  tart of opcode *
13115 2f 0a 20 20 69 6e 74 20 6f 72 69 67 50 63 3b 20  /.  int origPc; 
13116 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13117 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  * Program counte
13118 72 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70  r at start of op
13119 63 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  code */.#endif.#
1311a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1311b 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c  IT_PROGRESS_CALL
1311c 42 41 43 4b 0a 20 20 69 6e 74 20 6e 50 72 6f 67  BACK.  int nProg
1311d 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20 20  ressOps = 0;    
1311e 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78 65    /* Opcodes exe
1311f 63 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f 67  cuted since prog
13120 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20 2a  ress callback. *
13121 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  /.#endif.#ifndef
13122 20 4e 44 45 42 55 47 0a 20 20 4d 65 6d 20 2a 70   NDEBUG.  Mem *p
13123 53 74 61 63 6b 4c 69 6d 69 74 3b 0a 23 65 6e 64  StackLimit;.#end
13124 69 66 0a 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67  if..  if( p->mag
13125 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic!=VDBE_MAGIC_R
13126 55 4e 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  UN ) return SQLI
13127 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 61 73 73  TE_MISUSE;.  ass
13128 65 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d  ert( db->magic==
13129 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
1312a 59 20 29 3b 0a 20 20 70 54 6f 73 20 3d 20 70 2d  Y );.  pTos = p-
1312b 3e 70 54 6f 73 3b 0a 20 20 73 71 6c 69 74 65 33  >pTos;.  sqlite3
1312c 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45  BtreeMutexArrayE
1312d 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 78 29  nter(&p->aMutex)
1312e 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53  ;.  if( p->rc==S
1312f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
13130 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65     /* This happe
13131 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  ns if a malloc()
13132 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c 20 74   inside a call t
13133 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  o sqlite3_column
13134 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20 20 2a  _text() or.    *
13135 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
13136 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c 65 64  _text16() failed
13137 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e  .  */.    goto n
13138 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73  o_mem;.  }.  ass
13139 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49  ert( p->rc==SQLI
1313a 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d  TE_OK || p->rc==
1313b 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 20  SQLITE_BUSY );. 
1313c 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
1313d 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  OK;.  assert( p-
1313e 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20  >explain==0 );. 
1313f 20 69 66 28 20 70 2d 3e 70 6f 70 53 74 61 63 6b   if( p->popStack
13140 20 29 7b 0a 20 20 20 20 70 6f 70 53 74 61 63 6b   ){.    popStack
13141 28 26 70 54 6f 73 2c 20 70 2d 3e 70 6f 70 53 74  (&pTos, p->popSt
13142 61 63 6b 29 3b 0a 20 20 20 20 70 2d 3e 70 6f 70  ack);.    p->pop
13143 53 74 61 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 20  Stack = 0;.  }. 
13144 20 70 2d 3e 72 65 73 4f 6e 53 74 61 63 6b 20 3d   p->resOnStack =
13145 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61   0;.  db->busyHa
13146 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b  ndler.nBusy = 0;
13147 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54  .  CHECK_FOR_INT
13148 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 74 65  ERRUPT;.  sqlite
13149 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28  3VdbeIOTraceSql(
1314a 70 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  p);.#ifdef SQLIT
1314b 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 28 70  E_DEBUG.  if( (p
1314c 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
1314d 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67  LITE_VdbeListing
1314e 29 21 3d 30 0a 20 20 20 20 7c 7c 20 73 71 6c 69  )!=0.    || sqli
1314f 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e  te3OsAccess(db->
13150 70 56 66 73 2c 20 22 76 64 62 65 5f 65 78 70 6c  pVfs, "vdbe_expl
13151 61 69 6e 22 2c 20 53 51 4c 49 54 45 5f 41 43 43  ain", SQLITE_ACC
13152 45 53 53 5f 45 58 49 53 54 53 29 0a 20 20 29 7b  ESS_EXISTS).  ){
13153 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
13154 70 72 69 6e 74 66 28 22 56 44 42 45 20 50 72 6f  printf("VDBE Pro
13155 67 72 61 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22  gram Listing:\n"
13156 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
13157 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20  bePrintSql(p);. 
13158 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
13159 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nOp; i++){.    
1315a 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1315b 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 69 2c 20  ntOp(stdout, i, 
1315c 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20  &p->aOp[i]);.   
1315d 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c   }.  }.  if( sql
1315e 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d  ite3OsAccess(db-
1315f 3e 70 56 66 73 2c 20 22 76 64 62 65 5f 74 72 61  >pVfs, "vdbe_tra
13160 63 65 22 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  ce", SQLITE_ACCE
13161 53 53 5f 45 58 49 53 54 53 29 20 29 7b 0a 20 20  SS_EXISTS) ){.  
13162 20 20 70 2d 3e 74 72 61 63 65 20 3d 20 73 74 64    p->trace = std
13163 6f 75 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  out;.  }.#endif.
13164 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20    for(pc=p->pc; 
13165 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70  rc==SQLITE_OK; p
13166 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  c++){.    assert
13167 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d  ( pc>=0 && pc<p-
13168 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65  >nOp );.    asse
13169 72 74 28 20 70 54 6f 73 3c 3d 26 70 2d 3e 61 53  rt( pTos<=&p->aS
1316a 74 61 63 6b 5b 70 63 5d 20 29 3b 0a 20 20 20 20  tack[pc] );.    
1316b 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1316c 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  iled ) goto no_m
1316d 65 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f  em;.#ifdef VDBE_
1316e 50 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 67  PROFILE.    orig
1316f 50 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 61  Pc = pc;.    sta
13170 72 74 20 3d 20 68 77 74 69 6d 65 28 29 3b 0a 23  rt = hwtime();.#
13171 65 6e 64 69 66 0a 20 20 20 20 70 4f 70 20 3d 20  endif.    pOp = 
13172 26 70 2d 3e 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20  &p->aOp[pc];..  
13173 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20    /* Only allow 
13174 74 72 61 63 69 6e 67 20 69 66 20 53 51 4c 49 54  tracing if SQLIT
13175 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69 6e  E_DEBUG is defin
13176 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65  ed..    */.#ifde
13177 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
13178 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20     if( p->trace 
13179 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3d  ){.      if( pc=
1317a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72  =0 ){.        pr
1317b 69 6e 74 66 28 22 56 44 42 45 20 45 78 65 63 75  intf("VDBE Execu
1317c 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e 22 29 3b  tion Trace:\n");
1317d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1317e 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b  VdbePrintSql(p);
1317f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
13180 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
13181 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 63 2c 20  p(p->trace, pc, 
13182 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  pOp);.    }.    
13183 69 66 28 20 70 2d 3e 74 72 61 63 65 3d 3d 30 20  if( p->trace==0 
13184 26 26 20 70 63 3d 3d 30 20 0a 20 20 20 20 20 26  && pc==0 .     &
13185 26 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  & sqlite3OsAcces
13186 73 28 64 62 2d 3e 70 56 66 73 2c 20 22 76 64 62  s(db->pVfs, "vdb
13187 65 5f 73 71 6c 74 72 61 63 65 22 2c 20 53 51 4c  e_sqltrace", SQL
13188 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
13189 53 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  S) ){.      sqli
1318a 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
1318b 70 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  p);.    }.#endif
1318c 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20  .      ..    /* 
1318d 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
1318e 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c  we need to simul
1318f 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75 70 74  ate an interrupt
13190 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70  .  This only hap
13191 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 20 77  pens.    ** if w
13192 65 20 68 61 76 65 20 61 20 73 70 65 63 69 61 6c  e have a special
13193 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 20 20   test build..   
13194 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
13195 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28 20 73  E_TEST.    if( s
13196 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
13197 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 20 20  _count>0 ){.    
13198 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72    sqlite3_interr
13199 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20  upt_count--;.   
1319a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69     if( sqlite3_i
1319b 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3d 3d  nterrupt_count==
1319c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
1319d 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 64  ite3_interrupt(d
1319e 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  b);.      }.    
1319f 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
131a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52  f SQLITE_OMIT_PR
131a1 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a  OGRESS_CALLBACK.
131a2 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20      /* Call the 
131a3 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
131a4 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e 66 69  k if it is confi
131a5 67 75 72 65 64 20 61 6e 64 20 74 68 65 20 72 65  gured and the re
131a6 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a 20 20  quired number.  
131a7 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f 70 73    ** of VDBE ops
131a8 20 68 61 76 65 20 62 65 65 6e 20 65 78 65 63 75   have been execu
131a9 74 65 64 20 28 65 69 74 68 65 72 20 73 69 6e 63  ted (either sinc
131aa 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f  e this invocatio
131ab 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 6c 69  n of.    ** sqli
131ac 74 65 33 56 64 62 65 45 78 65 63 28 29 20 6f 72  te3VdbeExec() or
131ad 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69 6d 65   since last time
131ae 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61   the progress ca
131af 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c 6c 65  llback was calle
131b0 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68  d)..    ** If th
131b1 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
131b2 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ack returns non-
131b3 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65 20 76  zero, exit the v
131b4 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 77  irtual machine w
131b5 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 65 74  ith.    ** a ret
131b6 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 45 5f  urn code SQLITE_
131b7 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a 20 20  ABORT..    */.  
131b8 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 67 72    if( db->xProgr
131b9 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ess ){.      if(
131ba 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70   db->nProgressOp
131bb 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 73 20  s==nProgressOps 
131bc 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 70  ){.        int p
131bd 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  rc;.        if( 
131be 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
131bf 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
131c0 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
131c1 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d 64 62  .        prc =db
131c2 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62 2d 3e  ->xProgress(db->
131c3 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b 0a 20  pProgressArg);. 
131c4 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
131c5 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
131c6 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
131c7 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 20  to_misuse;.     
131c8 20 20 20 69 66 28 20 70 72 63 21 3d 30 20 29 7b     if( prc!=0 ){
131c9 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
131ca 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
131cb 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
131cc 20 76 64 62 65 5f 68 61 6c 74 3b 0a 20 20 20 20   vdbe_halt;.    
131cd 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 50      }.        nP
131ce 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a  rogressOps = 0;.
131cf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 50        }.      nP
131d0 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20  rogressOps++;.  
131d1 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e    }.#endif..#ifn
131d2 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f  def NDEBUG.    /
131d3 2a 20 54 68 69 73 20 69 73 20 74 6f 20 63 68 65  * This is to che
131d4 63 6b 20 74 68 61 74 20 74 68 65 20 72 65 74 75  ck that the retu
131d5 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 74 61 74  rn value of stat
131d6 69 63 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20  ic function.    
131d7 2a 2a 20 6f 70 63 6f 64 65 4e 6f 50 75 73 68 28  ** opcodeNoPush(
131d8 29 20 28 73 65 65 20 76 64 62 65 61 75 78 2e 63  ) (see vdbeaux.c
131d9 29 20 72 65 74 75 72 6e 73 20 76 61 6c 75 65 73  ) returns values
131da 20 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 0a   that match the.
131db 20 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74      ** implement
131dc 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 69 72  ation of the vir
131dd 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 69 6e 20  tual machine in 
131de 74 68 69 73 20 66 69 6c 65 2e 20 49 66 0a 20 20  this file. If.  
131df 20 20 2a 2a 20 6f 70 63 6f 64 65 4e 6f 50 75 73    ** opcodeNoPus
131e0 68 28 29 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  h() returns non-
131e1 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 73  zero, then the s
131e2 74 61 63 6b 20 69 73 20 67 75 61 72 65 6e 74 65  tack is guarente
131e3 65 64 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 74 6f  ed.    ** not to
131e4 20 67 72 6f 77 20 77 68 65 6e 20 74 68 65 20 6f   grow when the o
131e5 70 63 6f 64 65 20 69 73 20 65 78 65 63 75 74 65  pcode is execute
131e6 64 2e 20 49 66 20 69 74 20 72 65 74 75 72 6e 73  d. If it returns
131e7 20 7a 65 72 6f 2c 20 74 68 65 6e 0a 20 20 20 20   zero, then.    
131e8 2a 2a 20 74 68 65 20 73 74 61 63 6b 20 6d 61 79  ** the stack may
131e9 20 67 72 6f 77 20 62 79 20 61 74 20 6d 6f 73 74   grow by at most
131ea 20 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a   1..    **.    *
131eb 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 77 72 61  * The global wra
131ec 70 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 73 71  pper function sq
131ed 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 55  lite3VdbeOpcodeU
131ee 73 65 73 53 74 61 63 6b 28 29 20 69 73 20 6e 6f  sesStack() is no
131ef 74 20 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c 61  t .    ** availa
131f0 62 6c 65 20 69 66 20 4e 44 45 42 55 47 20 69 73  ble if NDEBUG is
131f1 20 64 65 66 69 6e 65 64 20 61 74 20 62 75 69 6c   defined at buil
131f2 64 20 74 69 6d 65 2e 0a 20 20 20 20 2a 2f 20 0a  d time..    */ .
131f3 20 20 20 20 70 53 74 61 63 6b 4c 69 6d 69 74 20      pStackLimit 
131f4 3d 20 70 54 6f 73 3b 0a 20 20 20 20 69 66 28 20  = pTos;.    if( 
131f5 21 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f  !sqlite3VdbeOpco
131f6 64 65 4e 6f 50 75 73 68 28 70 4f 70 2d 3e 6f 70  deNoPush(pOp->op
131f7 63 6f 64 65 29 20 29 7b 0a 20 20 20 20 20 20 70  code) ){.      p
131f8 53 74 61 63 6b 4c 69 6d 69 74 2b 2b 3b 0a 20 20  StackLimit++;.  
131f9 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
131fa 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63  switch( pOp->opc
131fb 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ode ){../*******
131fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
131fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
131fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
131ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13200 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 74 20 66  ******.** What f
13201 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d 61 73 73  ollows is a mass
13202 69 76 65 20 73 77 69 74 63 68 20 73 74 61 74 65  ive switch state
13203 6d 65 6e 74 20 77 68 65 72 65 20 65 61 63 68 20  ment where each 
13204 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  case implements 
13205 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 69 6e  a.** separate in
13206 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65  struction in the
13207 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
13208 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c 6f 77 20  .  If we follow 
13209 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 69 6e 64  the usual.** ind
1320a 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 65 6e 74  entation convent
1320b 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 73 65 20  ions, each case 
1320c 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 65 6e 74  should be indent
1320d 65 64 20 62 79 20 36 20 73 70 61 63 65 73 2e 20  ed by 6 spaces. 
1320e 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 69 73 20   But.** that is 
1320f 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 65 64 20  a lot of wasted 
13210 73 70 61 63 65 20 6f 6e 20 74 68 65 20 6c 65 66  space on the lef
13211 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f 20 74 68  t margin.  So th
13212 65 20 63 6f 64 65 20 77 69 74 68 69 6e 0a 2a 2a  e code within.**
13213 20 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74   the switch stat
13214 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65 61 6b  ement will break
13215 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 69 6f 6e   with convention
13216 20 61 6e 64 20 62 65 20 66 6c 75 73 68 2d 6c 65   and be flush-le
13217 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 62  ft. Another.** b
13218 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69 6d 69  ig comment (simi
13219 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29  lar to this one)
1321a 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65 20 70   will mark the p
1321b 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65  oint in the code
1321c 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 74 72 61   where.** we tra
1321d 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 74 6f 20  nsition back to 
1321e 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69  normal indentati
1321f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  on..**.** The fo
13220 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 61 63 68  rmatting of each
13221 20 63 61 73 65 20 69 73 20 69 6d 70 6f 72 74 61   case is importa
13222 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 66 69 6c  nt.  The makefil
13223 65 20 66 6f 72 20 53 51 4c 69 74 65 0a 2a 2a 20  e for SQLite.** 
13224 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 43 20  generates two C 
13225 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 73 2e 68  files "opcodes.h
13226 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 73 2e 63  " and "opcodes.c
13227 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 74 68  " by scanning th
13228 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f 6f 6b 69  is.** file looki
13229 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 74 68 61  ng for lines tha
1322a 74 20 62 65 67 69 6e 20 77 69 74 68 20 22 63 61  t begin with "ca
1322b 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 20 6f 70  se OP_".  The op
1322c 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 0a 2a 2a  codes.h files.**
1322d 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c 65 64 20   will be filled 
1322e 77 69 74 68 20 23 64 65 66 69 6e 65 73 20 74 68  with #defines th
1322f 61 74 20 67 69 76 65 20 75 6e 69 71 75 65 20 69  at give unique i
13230 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f  nteger values to
13231 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f 64 65 20   each.** opcode 
13232 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 73 2e  and the opcodes.
13233 63 20 66 69 6c 65 20 69 73 20 66 69 6c 6c 65 64  c file is filled
13234 20 77 69 74 68 20 61 6e 20 61 72 72 61 79 20 6f   with an array o
13235 66 20 73 74 72 69 6e 67 73 20 77 68 65 72 65 0a  f strings where.
13236 2a 2a 20 65 61 63 68 20 73 74 72 69 6e 67 20 69  ** each string i
13237 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e  s the symbolic n
13238 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 72 72  ame for the corr
13239 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 6f 64 65  esponding opcode
1323a 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 61 73  .  If the.** cas
1323b 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 66  e statement is f
1323c 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 63 6f 6d  ollowed by a com
1323d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d  ment of the form
1323e 20 22 2f 23 20 73 61 6d 65 20 61 73 20 2e 2e 2e   "/# same as ...
1323f 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 63 6f 6d   #/".** that com
13240 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20  ment is used to 
13241 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 70 61  determine the pa
13242 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f  rticular value o
13243 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 0a 2a 2a  f the opcode..**
13244 0a 2a 2a 20 49 66 20 61 20 63 6f 6d 6d 65 6e 74  .** If a comment
13245 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 6c 69 6e   on the same lin
13246 65 20 61 73 20 74 68 65 20 22 63 61 73 65 20 4f  e as the "case O
13247 50 5f 22 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  P_" construction
13248 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65   contains.** the
13249 20 77 6f 72 64 20 22 6e 6f 2d 70 75 73 68 22 2c   word "no-push",
1324a 20 74 68 65 6e 20 74 68 65 20 6f 70 63 6f 64 65   then the opcode
1324b 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 6e   is guarenteed n
1324c 6f 74 20 74 6f 20 67 72 6f 77 20 74 68 65 20 0a  ot to grow the .
1324d 2a 2a 20 76 64 62 65 20 73 74 61 63 6b 20 77 68  ** vdbe stack wh
1324e 65 6e 20 69 74 20 69 73 20 65 78 65 63 75 74 65  en it is execute
1324f 64 2e 20 53 65 65 20 66 75 6e 63 74 69 6f 6e 20  d. See function 
13250 6f 70 63 6f 64 65 28 29 20 69 6e 0a 2a 2a 20 76  opcode() in.** v
13251 64 62 65 61 75 78 2e 63 20 66 6f 72 20 64 65 74  dbeaux.c for det
13252 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75  ails..**.** Docu
13253 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 20  mentation about 
13254 56 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 20  VDBE opcodes is 
13255 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 61  generated by sca
13256 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 0a  nning this file.
13257 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 20  ** for lines of 
13258 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f 70  that contain "Op
13259 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c 69  code:".  That li
1325a 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 65  ne and all subse
1325b 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  quent.** comment
1325c 20 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 20   lines are used 
1325d 69 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 6f  in the generatio
1325e 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e  n of the opcode.
1325f 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 69  html documentati
13260 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  on.** file..**.*
13261 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a  * SUMMARY:.**.**
13262 20 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 20       Formatting 
13263 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  is important to 
13264 73 63 72 69 70 74 73 20 74 68 61 74 20 73 63 61  scripts that sca
13265 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20  n this file..** 
13266 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 61      Do not devia
13267 74 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d  te from the form
13268 61 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 72  atting style cur
13269 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a  rently in use..*
1326a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1326b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1326c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1326d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1326e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1326f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f  ../* Opcode:  Go
13270 74 6f 20 2a 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  to * P2 *.**.** 
13271 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c  An unconditional
13272 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73   jump to address
13273 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74   P2..** The next
13274 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65   instruction exe
13275 63 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a  cuted will be .*
13276 2a 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64  * the one at ind
13277 65 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62  ex P2 from the b
13278 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74  eginning of.** t
13279 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63  he program..*/.c
1327a 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20  ase OP_Goto: {  
1327b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f             /* no
1327c 2d 70 75 73 68 20 2a 2f 0a 20 20 43 48 45 43 4b  -push */.  CHECK
1327d 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
1327e 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1327f 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   1;.  break;.}..
13280 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75  /* Opcode:  Gosu
13281 62 20 2a 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 50  b * P2 *.**.** P
13282 75 73 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  ush the current 
13283 61 64 64 72 65 73 73 20 70 6c 75 73 20 31 20 6f  address plus 1 o
13284 6e 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 61  nto the return a
13285 64 64 72 65 73 73 20 73 74 61 63 6b 0a 2a 2a 20  ddress stack.** 
13286 61 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  and then jump to
13287 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 0a   address P2..**.
13288 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 61 64  ** The return ad
13289 64 72 65 73 73 20 73 74 61 63 6b 20 69 73 20 6f  dress stack is o
1328a 66 20 6c 69 6d 69 74 65 64 20 64 65 70 74 68 2e  f limited depth.
1328b 20 20 49 66 20 74 6f 6f 20 6d 61 6e 79 0a 2a 2a    If too many.**
1328c 20 4f 50 5f 47 6f 73 75 62 20 6f 70 65 72 61 74   OP_Gosub operat
1328d 69 6f 6e 73 20 6f 63 63 75 72 20 77 69 74 68 6f  ions occur witho
1328e 75 74 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 4f  ut intervening O
1328f 50 5f 52 65 74 75 72 6e 73 2c 20 74 68 65 6e 0a  P_Returns, then.
13290 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 61 64  ** the return ad
13291 64 72 65 73 73 20 73 74 61 63 6b 20 77 69 6c 6c  dress stack will
13292 20 66 69 6c 6c 20 75 70 20 61 6e 64 20 70 72 6f   fill up and pro
13293 63 65 73 73 69 6e 67 20 77 69 6c 6c 20 61 62 6f  cessing will abo
13294 72 74 0a 2a 2a 20 77 69 74 68 20 61 20 66 61 74  rt.** with a fat
13295 61 6c 20 65 72 72 6f 72 2e 0a 2a 2f 0a 63 61 73  al error..*/.cas
13296 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20 20  e OP_Gosub: {   
13297 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70           /* no-p
13298 75 73 68 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ush */.  assert(
13299 20 70 2d 3e 72 65 74 75 72 6e 44 65 70 74 68 3c   p->returnDepth<
1329a 73 69 7a 65 6f 66 28 70 2d 3e 72 65 74 75 72 6e  sizeof(p->return
1329b 53 74 61 63 6b 29 2f 73 69 7a 65 6f 66 28 70 2d  Stack)/sizeof(p-
1329c 3e 72 65 74 75 72 6e 53 74 61 63 6b 5b 30 5d 29  >returnStack[0])
1329d 20 29 3b 0a 20 20 70 2d 3e 72 65 74 75 72 6e 53   );.  p->returnS
1329e 74 61 63 6b 5b 70 2d 3e 72 65 74 75 72 6e 44 65  tack[p->returnDe
1329f 70 74 68 2b 2b 5d 20 3d 20 70 63 2b 31 3b 0a 20  pth++] = pc+1;. 
132a0 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
132a1 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  1;.  break;.}../
132a2 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 74 75 72  * Opcode:  Retur
132a3 6e 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75  n * * *.**.** Ju
132a4 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
132a5 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
132a6 75 63 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65  uction after the
132a7 20 6c 61 73 74 20 75 6e 72 65 74 75 72 6e 65 64   last unreturned
132a8 0a 2a 2a 20 4f 50 5f 47 6f 73 75 62 2e 20 20 49  .** OP_Gosub.  I
132a9 66 20 61 6e 20 4f 50 5f 52 65 74 75 72 6e 20 68  f an OP_Return h
132aa 61 73 20 6f 63 63 75 72 72 65 64 20 66 6f 72 20  as occurred for 
132ab 61 6c 6c 20 4f 50 5f 47 6f 73 75 62 73 2c 20 74  all OP_Gosubs, t
132ac 68 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e  hen.** processin
132ad 67 20 61 62 6f 72 74 73 20 77 69 74 68 20 61 20  g aborts with a 
132ae 66 61 74 61 6c 20 65 72 72 6f 72 2e 0a 2a 2f 0a  fatal error..*/.
132af 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20  case OP_Return: 
132b0 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e  {           /* n
132b1 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73 73 65  o-push */.  asse
132b2 72 74 28 20 70 2d 3e 72 65 74 75 72 6e 44 65 70  rt( p->returnDep
132b3 74 68 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 74  th>0 );.  p->ret
132b4 75 72 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 70 63  urnDepth--;.  pc
132b5 20 3d 20 70 2d 3e 72 65 74 75 72 6e 53 74 61 63   = p->returnStac
132b6 6b 5b 70 2d 3e 72 65 74 75 72 6e 44 65 70 74 68  k[p->returnDepth
132b7 5d 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a  ] - 1;.  break;.
132b8 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48  }../* Opcode:  H
132b9 61 6c 74 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  alt P1 P2 P3.**.
132ba 2a 2a 20 45 78 69 74 20 69 6d 6d 65 64 69 61 74  ** Exit immediat
132bb 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 65 6e 20 63  ely.  All open c
132bc 75 72 73 6f 72 73 2c 20 46 69 66 6f 73 2c 20 65  ursors, Fifos, e
132bd 74 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a  tc are closed.**
132be 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a   automatically..
132bf 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
132c0 72 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 75  result code retu
132c1 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
132c2 65 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 5f  exec(), sqlite3_
132c3 72 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73  reset(),.** or s
132c4 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
132c5 29 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c  ).  For a normal
132c6 20 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f 75   halt, this shou
132c7 6c 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20  ld be SQLITE_OK 
132c8 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f  (0)..** For erro
132c9 72 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 6f  rs, it can be so
132ca 6d 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e 20  me other value. 
132cb 20 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 50   If P1!=0 then P
132cc 32 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65  2 will determine
132cd 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e  .** whether or n
132ce 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  ot to rollback t
132cf 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
132d0 61 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20  action.  Do not 
132d1 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50  rollback.** if P
132d2 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74  2==OE_Fail. Do t
132d3 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50  he rollback if P
132d4 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20  2==OE_Rollback. 
132d5 20 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74   If P2==OE_Abort
132d6 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f  ,.** then back o
132d7 75 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 74  ut all changes t
132d8 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65  hat have occurre
132d9 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 78  d during this ex
132da 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a  ecution of the.*
132db 2a 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 6e  * VDBE, but do n
132dc 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  ot rollback the 
132dd 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a  transaction. .**
132de 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 6e 6f 74  .** If P3 is not
132df 20 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73   null then it is
132e0 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
132e1 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  e string..**.** 
132e2 54 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c  There is an impl
132e3 69 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22  ied "Halt 0 0 0"
132e4 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73   instruction ins
132e5 65 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72  erted at the ver
132e6 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72  y end of.** ever
132e7 79 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61  y program.  So a
132e8 20 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 6c   jump past the l
132e9 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ast instruction 
132ea 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a  of the program.*
132eb 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  * is the same as
132ec 20 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 2e   executing Halt.
132ed 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74  .*/.case OP_Halt
132ee 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
132ef 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 70  * no-push */.  p
132f0 2d 3e 70 54 6f 73 20 3d 20 70 54 6f 73 3b 0a 20  ->pTos = pTos;. 
132f1 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31   p->rc = pOp->p1
132f2 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a  ;.  p->pc = pc;.
132f3 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e    p->errorAction
132f4 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 66   = pOp->p2;.  if
132f5 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20  ( pOp->p3 ){.   
132f6 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
132f7 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 70  g(&p->zErrMsg, p
132f8 4f 70 2d 3e 70 33 2c 20 28 63 68 61 72 2a 29 30  Op->p3, (char*)0
132f9 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  );.  }.  rc = sq
132fa 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29  lite3VdbeHalt(p)
132fb 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
132fc 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72  SQLITE_BUSY || r
132fd 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
132fe 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
132ff 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e  _BUSY ){.    p->
13300 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45  rc = rc = SQLITE
13301 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _BUSY;.  }else{.
13302 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f      rc = p->rc ?
13303 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20   SQLITE_ERROR : 
13304 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
13305 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74  .  goto vdbe_ret
13306 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  urn;.}../* Opcod
13307 65 3a 20 49 6e 74 65 67 65 72 20 50 31 20 2a 20  e: Integer P1 * 
13308 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32 2d 62  *.**.** The 32-b
13309 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  it integer value
1330a 20 50 31 20 69 73 20 70 75 73 68 65 64 20 6f 6e   P1 is pushed on
1330b 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f  to the stack..*/
1330c 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 65 72  .case OP_Integer
1330d 3a 20 7b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20  : {.  pTos++;.  
1330e 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pTos->flags = ME
1330f 4d 5f 49 6e 74 3b 0a 20 20 70 54 6f 73 2d 3e 75  M_Int;.  pTos->u
13310 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  .i = pOp->p1;.  
13311 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
13312 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 2a 20 50  ode: Int64 * * P
13313 33 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20  3.**.** P3 is a 
13314 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
13315 61 74 69 6f 6e 20 6f 66 20 61 6e 20 69 6e 74 65  ation of an inte
13316 67 65 72 2e 20 20 43 6f 6e 76 65 72 74 20 74 68  ger.  Convert th
13317 61 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 6f  at integer.** to
13318 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 65 20   a 64-bit value 
13319 61 6e 64 20 70 75 73 68 20 69 74 20 6f 6e 74 6f  and push it onto
1331a 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f 0a 63   the stack..*/.c
1331b 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a 20 7b 0a  ase OP_Int64: {.
1331c 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 61 73 73 65    pTos++;.  asse
1331d 72 74 28 20 70 4f 70 2d 3e 70 33 21 3d 30 20 29  rt( pOp->p3!=0 )
1331e 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20  ;.  pTos->flags 
1331f 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74  = MEM_Str|MEM_St
13320 61 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20  atic|MEM_Term;. 
13321 20 70 54 6f 73 2d 3e 7a 20 3d 20 70 4f 70 2d 3e   pTos->z = pOp->
13322 70 33 3b 0a 20 20 70 54 6f 73 2d 3e 6e 20 3d 20  p3;.  pTos->n = 
13323 73 74 72 6c 65 6e 28 70 54 6f 73 2d 3e 7a 29 3b  strlen(pTos->z);
13324 0a 20 20 70 54 6f 73 2d 3e 65 6e 63 20 3d 20 53  .  pTos->enc = S
13325 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 54  QLITE_UTF8;.  pT
13326 6f 73 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65  os->u.i = sqlite
13327 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 54  3VdbeIntValue(pT
13328 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61  os);.  pTos->fla
13329 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  gs |= MEM_Int;. 
1332a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1332b 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 2a 20 50  code: Real * * P
1332c 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69  3.**.** The stri
1332d 6e 67 20 76 61 6c 75 65 20 50 33 20 69 73 20 63  ng value P3 is c
1332e 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 72 65  onverted to a re
1332f 61 6c 20 61 6e 64 20 70 75 73 68 65 64 20 6f 6e  al and pushed on
13330 20 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a   to the stack..*
13331 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 3a 20  /.case OP_Real: 
13332 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
13333 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54  same as TK_FLOAT
13334 2c 20 2a 2f 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20  , */.  pTos++;. 
13335 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d   pTos->flags = M
13336 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69  EM_Str|MEM_Stati
13337 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 54  c|MEM_Term;.  pT
13338 6f 73 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 33 3b  os->z = pOp->p3;
13339 0a 20 20 70 54 6f 73 2d 3e 6e 20 3d 20 73 74 72  .  pTos->n = str
1333a 6c 65 6e 28 70 54 6f 73 2d 3e 7a 29 3b 0a 20 20  len(pTos->z);.  
1333b 70 54 6f 73 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pTos->enc = SQLI
1333c 54 45 5f 55 54 46 38 3b 0a 20 20 70 54 6f 73 2d  TE_UTF8;.  pTos-
1333d 3e 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  >r = sqlite3Vdbe
1333e 52 65 61 6c 56 61 6c 75 65 28 70 54 6f 73 29 3b  RealValue(pTos);
1333f 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 7c  .  pTos->flags |
13340 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 73 71  = MEM_Real;.  sq
13341 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
13342 6e 63 6f 64 69 6e 67 28 70 54 6f 73 2c 20 65 6e  ncoding(pTos, en
13343 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65 61 6b  coding);.  break
13344 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
13345 53 74 72 69 6e 67 38 20 2a 20 2a 20 50 33 0a 2a  String8 * * P3.*
13346 2a 0a 2a 2a 20 50 33 20 70 6f 69 6e 74 73 20 74  *.** P3 points t
13347 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74  o a nul terminat
13348 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e  ed UTF-8 string.
13349 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20   This opcode is 
1334a 74 72 61 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20  transformed .** 
1334b 69 6e 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e  into an OP_Strin
1334c 67 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 65  g before it is e
1334d 78 65 63 75 74 65 64 20 66 6f 72 20 74 68 65 20  xecuted for the 
1334e 66 69 72 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63  first time..*/.c
1334f 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20  ase OP_String8: 
13350 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d  {         /* sam
13351 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 20 2a  e as TK_STRING *
13352 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
13353 3e 70 33 21 3d 30 20 29 3b 0a 20 20 70 4f 70 2d  >p3!=0 );.  pOp-
13354 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 53 74 72  >opcode = OP_Str
13355 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d  ing;.  pOp->p1 =
13356 20 73 74 72 6c 65 6e 28 70 4f 70 2d 3e 70 33 29   strlen(pOp->p3)
13357 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c 49  ;.  assert( SQLI
13358 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
13359 48 20 3c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  H < SQLITE_MAX_L
1335a 45 4e 47 54 48 20 29 3b 0a 20 20 61 73 73 65 72  ENGTH );.  asser
1335b 74 28 20 70 4f 70 2d 3e 70 31 20 3c 20 53 51 4c  t( pOp->p1 < SQL
1335c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29  ITE_MAX_LENGTH )
1335d 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
1335e 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 69  E_OMIT_UTF16.  i
1335f 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d 53 51 4c  f( encoding!=SQL
13360 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20  ITE_UTF8 ){.    
13361 70 54 6f 73 2b 2b 3b 0a 20 20 20 20 73 71 6c 69  pTos++;.    sqli
13362 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
13363 28 70 54 6f 73 2c 20 70 4f 70 2d 3e 70 33 2c 20  (pTos, pOp->p3, 
13364 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  -1, SQLITE_UTF8,
13365 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
13366 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
13367 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 65 43  OK!=sqlite3VdbeC
13368 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 54  hangeEncoding(pT
13369 6f 73 2c 20 65 6e 63 6f 64 69 6e 67 29 20 29 20  os, encoding) ) 
1336a 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
1336b 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
1336c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 44 79  sqlite3VdbeMemDy
1336d 6e 61 6d 69 63 69 66 79 28 70 54 6f 73 29 20 29  namicify(pTos) )
1336e 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1336f 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 3d    pTos->flags &=
13370 20 7e 28 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 20   ~(MEM_Dyn);.   
13371 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 7c 3d 20   pTos->flags |= 
13372 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20  MEM_Static;.    
13373 69 66 28 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d  if( pOp->p3type=
13374 3d 50 33 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20  =P3_DYNAMIC ){. 
13375 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
13376 65 28 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 20 20  e(pOp->p3);.    
13377 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 33 74 79 70  }.    pOp->p3typ
13378 65 20 3d 20 50 33 5f 44 59 4e 41 4d 49 43 3b 0a  e = P3_DYNAMIC;.
13379 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20 70 54      pOp->p3 = pT
1337a 6f 73 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e  os->z;.    pOp->
1337b 70 31 20 3d 20 70 54 6f 73 2d 3e 6e 3b 0a 20 20  p1 = pTos->n;.  
1337c 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1337d 31 20 3c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  1 < SQLITE_MAX_L
1337e 45 4e 47 54 48 20 29 3b 20 2f 2a 20 44 75 65 20  ENGTH ); /* Due 
1337f 74 6f 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  to SQLITE_MAX_SQ
13380 4c 5f 4c 45 4e 47 54 48 20 2a 2f 0a 20 20 20 20  L_LENGTH */.    
13381 62 72 65 61 6b 3b 0a 20 20 7d 0a 23 65 6e 64 69  break;.  }.#endi
13382 66 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65  f.  /* Otherwise
13383 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   fall through to
13384 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 2c 20   the next case, 
13385 4f 50 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20  OP_String */.}. 
13386 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72   ./* Opcode: Str
13387 69 6e 67 20 50 31 20 2a 20 50 33 0a 2a 2a 0a 2a  ing P1 * P3.**.*
13388 2a 20 54 68 65 20 73 74 72 69 6e 67 20 76 61 6c  * The string val
13389 75 65 20 50 33 20 6f 66 20 6c 65 6e 67 74 68 20  ue P3 of length 
1338a 50 31 20 28 62 79 74 65 73 29 20 69 73 20 70 75  P1 (bytes) is pu
1338b 73 68 65 64 20 6f 6e 74 6f 20 74 68 65 20 73 74  shed onto the st
1338c 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ack..*/.case OP_
1338d 53 74 72 69 6e 67 3a 20 7b 0a 20 20 61 73 73 65  String: {.  asse
1338e 72 74 28 20 70 4f 70 2d 3e 70 31 20 3c 20 53 51  rt( pOp->p1 < SQ
1338f 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20  LITE_MAX_LENGTH 
13390 29 3b 20 2f 2a 20 44 75 65 20 74 6f 20 53 51 4c  ); /* Due to SQL
13391 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
13392 54 48 20 2a 2f 0a 20 20 70 54 6f 73 2b 2b 3b 0a  TH */.  pTos++;.
13393 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
13394 33 21 3d 30 20 29 3b 0a 20 20 70 54 6f 73 2d 3e  3!=0 );.  pTos->
13395 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c  flags = MEM_Str|
13396 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54  MEM_Static|MEM_T
13397 65 72 6d 3b 0a 20 20 70 54 6f 73 2d 3e 7a 20 3d  erm;.  pTos->z =
13398 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 70 54 6f 73   pOp->p3;.  pTos
13399 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  ->n = pOp->p1;. 
1339a 20 70 54 6f 73 2d 3e 65 6e 63 20 3d 20 65 6e 63   pTos->enc = enc
1339b 6f 64 69 6e 67 3b 0a 20 20 62 72 65 61 6b 3b 0a  oding;.  break;.
1339c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75  }../* Opcode: Nu
1339d 6c 6c 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50  ll * * *.**.** P
1339e 75 73 68 20 61 20 4e 55 4c 4c 20 6f 6e 74 6f 20  ush a NULL onto 
1339f 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f 0a 63 61  the stack..*/.ca
133a0 73 65 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 0a 20 20  se OP_Null: {.  
133a1 70 54 6f 73 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e  pTos++;.  pTos->
133a2 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
133a3 3b 0a 20 20 70 54 6f 73 2d 3e 6e 20 3d 20 30 3b  ;.  pTos->n = 0;
133a4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69  .  break;.}...#i
133a5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
133a6 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 2f  T_BLOB_LITERAL./
133a7 2a 20 4f 70 63 6f 64 65 3a 20 48 65 78 42 6c 6f  * Opcode: HexBlo
133a8 62 20 2a 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 50  b * * P3.**.** P
133a9 33 20 69 73 20 61 6e 20 55 54 46 2d 38 20 53 51  3 is an UTF-8 SQ
133aa 4c 20 68 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f  L hex encoding o
133ab 66 20 61 20 62 6c 6f 62 2e 20 54 68 65 20 62 6c  f a blob. The bl
133ac 6f 62 20 69 73 20 70 75 73 68 65 64 20 6f 6e 74  ob is pushed ont
133ad 6f 20 74 68 65 0a 2a 2a 20 76 64 62 65 20 73 74  o the.** vdbe st
133ae 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ack..**.** The f
133af 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69  irst time this i
133b0 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63 75  nstruction execu
133b1 74 65 73 2c 20 69 6e 20 74 72 61 6e 73 66 6f 72  tes, in transfor
133b2 6d 73 20 69 74 73 65 6c 66 20 69 6e 74 6f 20 61  ms itself into a
133b3 0a 2a 2a 20 27 42 6c 6f 62 27 20 6f 70 63 6f 64  .** 'Blob' opcod
133b4 65 20 77 69 74 68 20 61 20 62 69 6e 61 72 79 20  e with a binary 
133b5 62 6c 6f 62 20 61 73 20 50 33 2e 0a 2a 2f 0a 63  blob as P3..*/.c
133b6 61 73 65 20 4f 50 5f 48 65 78 42 6c 6f 62 3a 20  ase OP_HexBlob: 
133b7 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
133b8 73 61 6d 65 20 61 73 20 54 4b 5f 42 4c 4f 42 20  same as TK_BLOB 
133b9 2a 2f 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  */.  pOp->opcode
133ba 20 3d 20 4f 50 5f 42 6c 6f 62 3b 0a 20 20 70 4f   = OP_Blob;.  pO
133bb 70 2d 3e 70 31 20 3d 20 73 74 72 6c 65 6e 28 70  p->p1 = strlen(p
133bc 4f 70 2d 3e 70 33 29 2f 32 3b 0a 20 20 61 73 73  Op->p3)/2;.  ass
133bd 65 72 74 28 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ert( SQLITE_MAX_
133be 53 51 4c 5f 4c 45 4e 47 54 48 20 3c 20 53 51 4c  SQL_LENGTH < SQL
133bf 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29  ITE_MAX_LENGTH )
133c0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
133c1 3e 70 31 20 3c 20 53 51 4c 49 54 45 5f 4d 41 58  >p1 < SQLITE_MAX
133c2 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 69 66 28  _LENGTH );.  if(
133c3 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20   pOp->p1 ){.    
133c4 63 68 61 72 20 2a 7a 42 6c 6f 62 20 3d 20 73 71  char *zBlob = sq
133c5 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 64  lite3HexToBlob(d
133c6 62 2c 20 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 20  b, pOp->p3);.   
133c7 20 69 66 28 20 21 7a 42 6c 6f 62 20 29 20 67 6f   if( !zBlob ) go
133c8 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69  to no_mem;.    i
133c9 66 28 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d  f( pOp->p3type==
133ca 50 33 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20  P3_DYNAMIC ){.  
133cb 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
133cc 28 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 20 20 7d  (pOp->p3);.    }
133cd 0a 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20 7a  .    pOp->p3 = z
133ce 42 6c 6f 62 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  Blob;.    pOp->p
133cf 33 74 79 70 65 20 3d 20 50 33 5f 44 59 4e 41 4d  3type = P3_DYNAM
133d0 49 43 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  IC;.  }else{.   
133d1 20 69 66 28 20 70 4f 70 2d 3e 70 33 74 79 70 65   if( pOp->p3type
133d2 3d 3d 50 33 5f 44 59 4e 41 4d 49 43 20 29 7b 0a  ==P3_DYNAMIC ){.
133d3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
133d4 65 65 28 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 20  ee(pOp->p3);.   
133d5 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 33 74 79   }.    pOp->p3ty
133d6 70 65 20 3d 20 50 33 5f 53 54 41 54 49 43 3b 0a  pe = P3_STATIC;.
133d7 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20 22 22      pOp->p3 = ""
133d8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 6c 6c  ;.  }..  /* Fall
133d9 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
133da 6e 65 78 74 20 63 61 73 65 2c 20 4f 50 5f 42 6c  next case, OP_Bl
133db 6f 62 2e 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63  ob. */.}../* Opc
133dc 6f 64 65 3a 20 42 6c 6f 62 20 50 31 20 2a 20 50  ode: Blob P1 * P
133dd 33 0a 2a 2a 0a 2a 2a 20 50 33 20 70 6f 69 6e 74  3.**.** P3 point
133de 73 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 64  s to a blob of d
133df 61 74 61 20 50 31 20 62 79 74 65 73 20 6c 6f 6e  ata P1 bytes lon
133e0 67 2e 20 50 75 73 68 20 74 68 69 73 0a 2a 2a 20  g. Push this.** 
133e1 76 61 6c 75 65 20 6f 6e 74 6f 20 74 68 65 20 73  value onto the s
133e2 74 61 63 6b 2e 20 54 68 69 73 20 69 6e 73 74 72  tack. This instr
133e3 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f  uction is not co
133e4 64 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20  ded directly.** 
133e5 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e  by the compiler.
133e6 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 63 6f   Instead, the co
133e7 6d 70 69 6c 65 72 20 6c 61 79 65 72 20 73 70 65  mpiler layer spe
133e8 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f  cifies.** an OP_
133e9 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20  HexBlob opcode, 
133ea 77 69 74 68 20 74 68 65 20 68 65 78 20 73 74 72  with the hex str
133eb 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
133ec 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f  on of.** the blo
133ed 62 20 61 73 20 50 33 2e 20 54 68 69 73 20 6f 70  b as P3. This op
133ee 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72  code is transfor
133ef 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f  med to an OP_Blo
133f0 62 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74  b.** the first t
133f1 69 6d 65 20 69 74 20 69 73 20 65 78 65 63 75 74  ime it is execut
133f2 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42  ed..*/.case OP_B
133f3 6c 6f 62 3a 20 7b 0a 20 20 70 54 6f 73 2b 2b 3b  lob: {.  pTos++;
133f4 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
133f5 70 31 20 3c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  p1 < SQLITE_MAX_
133f6 4c 45 4e 47 54 48 20 29 3b 20 2f 2a 20 44 75 65  LENGTH ); /* Due
133f7 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53   to SQLITE_MAX_S
133f8 51 4c 5f 4c 45 4e 47 54 48 20 2a 2f 0a 20 20 73  QL_LENGTH */.  s
133f9 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
133fa 53 74 72 28 70 54 6f 73 2c 20 70 4f 70 2d 3e 70  Str(pTos, pOp->p
133fb 33 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30  3, pOp->p1, 0, 0
133fc 29 3b 0a 20 20 70 54 6f 73 2d 3e 65 6e 63 20 3d  );.  pTos->enc =
133fd 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 62 72 65   encoding;.  bre
133fe 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
133ff 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
13400 5f 4c 49 54 45 52 41 4c 20 2a 2f 0a 0a 2f 2a 20  _LITERAL */../* 
13401 4f 70 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65  Opcode: Variable
13402 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 75   P1 * *.**.** Pu
13403 73 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  sh the value of 
13404 76 61 72 69 61 62 6c 65 20 50 31 20 6f 6e 74 6f  variable P1 onto
13405 20 74 68 65 20 73 74 61 63 6b 2e 20 20 41 20 76   the stack.  A v
13406 61 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 61 6e  ariable is.** an
13407 20 75 6e 6b 6e 6f 77 6e 20 69 6e 20 74 68 65 20   unknown in the 
13408 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 72  original SQL str
13409 69 6e 67 20 61 73 20 68 61 6e 64 65 64 20 74 6f  ing as handed to
1340a 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65   sqlite3_compile
1340b 28 29 2e 0a 2a 2a 20 41 6e 79 20 6f 63 63 75 72  ()..** Any occur
1340c 61 6e 63 65 20 6f 66 20 74 68 65 20 27 3f 27 20  ance of the '?' 
1340d 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65  character in the
1340e 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 69 73   original SQL is
1340f 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 61   considered.** a
13410 20 76 61 72 69 61 62 6c 65 2e 20 20 56 61 72 69   variable.  Vari
13411 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53 51 4c  ables in the SQL
13412 20 73 74 72 69 6e 67 20 61 72 65 20 6e 75 6d 62   string are numb
13413 65 72 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 0a  er from left to.
13414 2a 2a 20 72 69 67 68 74 20 62 65 67 69 6e 6e 69  ** right beginni
13415 6e 67 20 77 69 74 68 20 31 2e 20 20 54 68 65 20  ng with 1.  The 
13416 76 61 6c 75 65 73 20 6f 66 20 76 61 72 69 61 62  values of variab
13417 6c 65 73 20 61 72 65 20 73 65 74 20 75 73 69 6e  les are set usin
13418 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  g the.** sqlite3
13419 5f 62 69 6e 64 28 29 20 41 50 49 2e 0a 2a 2f 0a  _bind() API..*/.
1341a 63 61 73 65 20 4f 50 5f 56 61 72 69 61 62 6c 65  case OP_Variable
1341b 3a 20 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 70 4f  : {.  int j = pO
1341c 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 4d 65 6d  p->p1 - 1;.  Mem
1341d 20 2a 70 56 61 72 3b 0a 20 20 61 73 73 65 72 74   *pVar;.  assert
1341e 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e  ( j>=0 && j<p->n
1341f 56 61 72 20 29 3b 0a 0a 20 20 70 56 61 72 20 3d  Var );..  pVar =
13420 20 26 70 2d 3e 61 56 61 72 5b 6a 5d 3b 0a 20 20   &p->aVar[j];.  
13421 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
13422 65 6d 54 6f 6f 42 69 67 28 70 56 61 72 29 20 29  emTooBig(pVar) )
13423 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62  {.    goto too_b
13424 69 67 3b 0a 20 20 7d 0a 20 20 70 54 6f 73 2b 2b  ig;.  }.  pTos++
13425 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
13426 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 54  emShallowCopy(pT
13427 6f 73 2c 20 26 70 2d 3e 61 56 61 72 5b 6a 5d 2c  os, &p->aVar[j],
13428 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20   MEM_Static);.  
13429 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1342a 6f 64 65 3a 20 50 6f 70 20 50 31 20 2a 20 2a 0a  ode: Pop P1 * *.
1342b 2a 2a 0a 2a 2a 20 50 31 20 65 6c 65 6d 65 6e 74  **.** P1 element
1342c 73 20 61 72 65 20 70 6f 70 70 65 64 20 6f 66 66  s are popped off
1342d 20 6f 66 20 74 68 65 20 74 6f 70 20 6f 66 20 73   of the top of s
1342e 74 61 63 6b 20 61 6e 64 20 64 69 73 63 61 72 64  tack and discard
1342f 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50  ed..*/.case OP_P
13430 6f 70 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  op: {           
13431 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20   /* no-push */. 
13432 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
13433 3e 3d 30 20 29 3b 0a 20 20 70 6f 70 53 74 61 63  >=0 );.  popStac
13434 6b 28 26 70 54 6f 73 2c 20 70 4f 70 2d 3e 70 31  k(&pTos, pOp->p1
13435 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  );.  assert( pTo
13436 73 3e 3d 26 70 2d 3e 61 53 74 61 63 6b 5b 2d 31  s>=&p->aStack[-1
13437 5d 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ] );.  break;.}.
13438 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 75 70 20  ./* Opcode: Dup 
13439 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 41 20  P1 P2 *.**.** A 
1343a 63 6f 70 79 20 6f 66 20 74 68 65 20 50 31 2d 74  copy of the P1-t
1343b 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  h element of the
1343c 20 73 74 61 63 6b 20 0a 2a 2a 20 69 73 20 6d 61   stack .** is ma
1343d 64 65 20 61 6e 64 20 70 75 73 68 65 64 20 6f 6e  de and pushed on
1343e 74 6f 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  to the top of th
1343f 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 54 68 65 20  e stack..** The 
13440 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
13441 20 69 73 20 65 6c 65 6d 65 6e 74 20 30 2e 20 20   is element 0.  
13442 53 6f 20 74 68 65 0a 2a 2a 20 69 6e 73 74 72 75  So the.** instru
13443 63 74 69 6f 6e 20 22 44 75 70 20 30 20 30 20 30  ction "Dup 0 0 0
13444 22 20 77 69 6c 6c 20 6d 61 6b 65 20 61 20 63 6f  " will make a co
13445 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 74 6f 70  py of the.** top
13446 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a   of the stack..*
13447 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 74  *.** If the cont
13448 65 6e 74 20 6f 66 20 74 68 65 20 50 31 2d 74 68  ent of the P1-th
13449 20 65 6c 65 6d 65 6e 74 20 69 73 20 61 20 64 79   element is a dy
1344a 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c  namically.** all
1344b 6f 63 61 74 65 64 20 73 74 72 69 6e 67 2c 20 74  ocated string, t
1344c 68 65 6e 20 61 20 6e 65 77 20 63 6f 70 79 20 6f  hen a new copy o
1344d 66 20 74 68 61 74 20 73 74 72 69 6e 67 0a 2a 2a  f that string.**
1344e 20 69 73 20 6d 61 64 65 20 69 66 20 50 32 3d 3d   is made if P2==
1344f 30 2e 20 20 49 66 20 50 32 21 3d 30 2c 20 74 68  0.  If P2!=0, th
13450 65 6e 20 6a 75 73 74 20 61 20 70 6f 69 6e 74 65  en just a pointe
13451 72 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 72 69  r.** to the stri
13452 6e 67 20 69 73 20 63 6f 70 69 65 64 2e 0a 2a 2a  ng is copied..**
13453 0a 2a 2a 20 41 6c 73 6f 20 73 65 65 20 74 68 65  .** Also see the
13454 20 50 75 6c 6c 20 69 6e 73 74 72 75 63 74 69 6f   Pull instructio
13455 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 75  n..*/.case OP_Du
13456 70 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 46 72 6f  p: {.  Mem *pFro
13457 6d 20 3d 20 26 70 54 6f 73 5b 2d 70 4f 70 2d 3e  m = &pTos[-pOp->
13458 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p1];.  assert( p
13459 46 72 6f 6d 3c 3d 70 54 6f 73 20 26 26 20 70 46  From<=pTos && pF
1345a 72 6f 6d 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29  rom>=p->aStack )
1345b 3b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 73 71  ;.  pTos++;.  sq
1345c 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
1345d 6c 6f 77 43 6f 70 79 28 70 54 6f 73 2c 20 70 46  lowCopy(pTos, pF
1345e 72 6f 6d 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b  rom, MEM_Ephem);
1345f 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 29  .  if( pOp->p2 )
13460 7b 0a 20 20 20 20 44 65 65 70 68 65 6d 65 72 61  {.    Deephemera
13461 6c 69 7a 65 28 70 54 6f 73 29 3b 0a 20 20 7d 0a  lize(pTos);.  }.
13462 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
13463 70 63 6f 64 65 3a 20 50 75 6c 6c 20 50 31 20 2a  pcode: Pull P1 *
13464 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 2d   *.**.** The P1-
13465 74 68 20 65 6c 65 6d 65 6e 74 20 69 73 20 72 65  th element is re
13466 6d 6f 76 65 64 20 66 72 6f 6d 20 69 74 73 20 63  moved from its c
13467 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20  urrent location 
13468 6f 6e 20 0a 2a 2a 20 74 68 65 20 73 74 61 63 6b  on .** the stack
13469 20 61 6e 64 20 70 75 73 68 65 64 20 62 61 63 6b   and pushed back
1346a 20 6f 6e 20 74 6f 70 20 6f 66 20 74 68 65 20 73   on top of the s
1346b 74 61 63 6b 2e 20 20 54 68 65 0a 2a 2a 20 74 6f  tack.  The.** to
1346c 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69  p of the stack i
1346d 73 20 65 6c 65 6d 65 6e 74 20 30 2c 20 73 6f 20  s element 0, so 
1346e 22 50 75 6c 6c 20 30 20 30 20 30 22 20 69 73 0a  "Pull 0 0 0" is.
1346f 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 20 22 50 75  ** a no-op.  "Pu
13470 6c 6c 20 31 20 30 20 30 22 20 73 77 61 70 73 20  ll 1 0 0" swaps 
13471 74 68 65 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d  the top two elem
13472 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73  ents of.** the s
13473 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  tack..**.** See 
13474 61 6c 73 6f 20 74 68 65 20 44 75 70 20 69 6e 73  also the Dup ins
13475 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  truction..*/.cas
13476 65 20 4f 50 5f 50 75 6c 6c 3a 20 7b 20 20 20 20  e OP_Pull: {    
13477 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
13478 73 68 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 46 72  sh */.  Mem *pFr
13479 6f 6d 20 3d 20 26 70 54 6f 73 5b 2d 70 4f 70 2d  om = &pTos[-pOp-
1347a 3e 70 31 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  >p1];.  int i;. 
1347b 20 4d 65 6d 20 74 73 3b 0a 0a 20 20 74 73 20 3d   Mem ts;..  ts =
1347c 20 2a 70 46 72 6f 6d 3b 0a 20 20 44 65 65 70 68   *pFrom;.  Deeph
1347d 65 6d 65 72 61 6c 69 7a 65 28 70 54 6f 73 29 3b  emeralize(pTos);
1347e 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f  .  for(i=0; i<pO
1347f 70 2d 3e 70 31 3b 20 69 2b 2b 2c 20 70 46 72 6f  p->p1; i++, pFro
13480 6d 2b 2b 29 7b 0a 20 20 20 20 44 65 65 70 68 65  m++){.    Deephe
13481 6d 65 72 61 6c 69 7a 65 28 26 70 46 72 6f 6d 5b  meralize(&pFrom[
13482 31 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1]);.    assert(
13483 20 28 70 46 72 6f 6d 5b 31 5d 2e 66 6c 61 67 73   (pFrom[1].flags
13484 20 26 20 4d 45 4d 5f 45 70 68 65 6d 29 3d 3d 30   & MEM_Ephem)==0
13485 20 29 3b 0a 20 20 20 20 2a 70 46 72 6f 6d 20 3d   );.    *pFrom =
13486 20 70 46 72 6f 6d 5b 31 5d 3b 0a 20 20 20 20 69   pFrom[1];.    i
13487 66 28 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20  f( pFrom->flags 
13488 26 20 4d 45 4d 5f 53 68 6f 72 74 20 29 7b 0a 20  & MEM_Short ){. 
13489 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72       assert( pFr
1348a 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  om->flags & (MEM
1348b 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29  _Str|MEM_Blob) )
1348c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1348d 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 5b  pFrom->z==pFrom[
1348e 31 5d 2e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20  1].zShort );.   
1348f 20 20 20 70 46 72 6f 6d 2d 3e 7a 20 3d 20 70 46     pFrom->z = pF
13490 72 6f 6d 2d 3e 7a 53 68 6f 72 74 3b 0a 20 20 20  rom->zShort;.   
13491 20 7d 0a 20 20 7d 0a 20 20 2a 70 54 6f 73 20 3d   }.  }.  *pTos =
13492 20 74 73 3b 0a 20 20 69 66 28 20 70 54 6f 73 2d   ts;.  if( pTos-
13493 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 68 6f  >flags & MEM_Sho
13494 72 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  rt ){.    assert
13495 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20  ( pTos->flags & 
13496 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
13497 62 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  b) );.    assert
13498 28 20 70 54 6f 73 2d 3e 7a 3d 3d 70 54 6f 73 5b  ( pTos->z==pTos[
13499 2d 70 4f 70 2d 3e 70 31 5d 2e 7a 53 68 6f 72 74  -pOp->p1].zShort
1349a 20 29 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 7a 20   );.    pTos->z 
1349b 3d 20 70 54 6f 73 2d 3e 7a 53 68 6f 72 74 3b 0a  = pTos->zShort;.
1349c 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1349d 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 75 73 68 20  /* Opcode: Push 
1349e 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 76 65  P1 * *.**.** Ove
1349f 72 77 72 69 74 65 20 74 68 65 20 76 61 6c 75 65  rwrite the value
134a0 20 6f 66 20 74 68 65 20 50 31 2d 74 68 20 65 6c   of the P1-th el
134a1 65 6d 65 6e 74 20 64 6f 77 6e 20 6f 6e 20 74 68  ement down on th
134a2 65 0a 2a 2a 20 73 74 61 63 6b 20 28 50 31 3d 3d  e.** stack (P1==
134a3 30 20 69 73 20 74 68 65 20 74 6f 70 20 6f 66 20  0 is the top of 
134a4 74 68 65 20 73 74 61 63 6b 29 20 77 69 74 68 20  the stack) with 
134a5 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
134a6 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
134a7 74 61 63 6b 2e 20 20 54 68 65 6e 20 70 6f 70 20  tack.  Then pop 
134a8 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
134a9 74 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tack..*/.case OP
134aa 5f 50 75 73 68 3a 20 7b 20 20 20 20 20 20 20 20  _Push: {        
134ab 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a      /* no-push *
134ac 2f 0a 20 20 4d 65 6d 20 2a 70 54 6f 20 3d 20 26  /.  Mem *pTo = &
134ad 70 54 6f 73 5b 2d 70 4f 70 2d 3e 70 31 5d 3b 0a  pTos[-pOp->p1];.
134ae 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 3e 3d  .  assert( pTo>=
134af 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 73  p->aStack );.  s
134b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
134b1 65 28 70 54 6f 2c 20 70 54 6f 73 29 3b 0a 20 20  e(pTo, pTos);.  
134b2 70 54 6f 73 2d 2d 3b 0a 20 20 62 72 65 61 6b 3b  pTos--;.  break;
134b3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
134b4 61 6c 6c 62 61 63 6b 20 50 31 20 2a 20 2a 0a 2a  allback P1 * *.*
134b5 2a 0a 2a 2a 20 54 68 65 20 74 6f 70 20 50 31 20  *.** The top P1 
134b6 76 61 6c 75 65 73 20 6f 6e 20 74 68 65 20 73 74  values on the st
134b7 61 63 6b 20 72 65 70 72 65 73 65 6e 74 20 61 20  ack represent a 
134b8 73 69 6e 67 6c 65 20 72 65 73 75 6c 74 20 72 6f  single result ro
134b9 77 20 66 72 6f 6d 0a 2a 2a 20 61 20 71 75 65 72  w from.** a quer
134ba 79 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20  y.  This opcode 
134bb 63 61 75 73 65 73 20 74 68 65 20 73 71 6c 69 74  causes the sqlit
134bc 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74  e3_step() call t
134bd 6f 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77  o terminate.** w
134be 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f  ith an SQLITE_RO
134bf 57 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e  W return code an
134c0 64 20 69 74 20 73 65 74 73 20 75 70 20 74 68 65  d it sets up the
134c1 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a   sqlite3_stmt.**
134c2 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 72   structure to pr
134c3 6f 76 69 64 65 20 61 63 63 65 73 73 20 74 6f 20  ovide access to 
134c4 74 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65  the top P1 value
134c5 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a  s as the result.
134c6 2a 2a 20 72 6f 77 2e 20 20 57 68 65 6e 20 74 68  ** row.  When th
134c7 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  e sqlite3_step()
134c8 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e   function is run
134c9 20 61 67 61 69 6e 2c 20 74 68 65 20 74 6f 70 20   again, the top 
134ca 50 31 0a 2a 2a 20 76 61 6c 75 65 73 20 77 69 6c  P1.** values wil
134cb 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
134cc 6c 79 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74  ly popped from t
134cd 68 65 20 73 74 61 63 6b 20 62 65 66 6f 72 65 20  he stack before 
134ce 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 73 74  the next.** inst
134cf 72 75 63 74 69 6f 6e 20 65 78 65 63 75 74 65 73  ruction executes
134d0 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 61 6c  ..*/.case OP_Cal
134d1 6c 62 61 63 6b 3a 20 7b 20 20 20 20 20 20 20 20  lback: {        
134d2 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a      /* no-push *
134d3 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20  /.  Mem *pMem;. 
134d4 20 4d 65 6d 20 2a 70 46 69 72 73 74 43 6f 6c 75   Mem *pFirstColu
134d5 6d 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  mn;.  assert( p-
134d6 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70  >nResColumn==pOp
134d7 2d 3e 70 31 20 29 3b 0a 0a 20 20 2f 2a 20 44 61  ->p1 );..  /* Da
134d8 74 61 20 69 6e 20 74 68 65 20 70 61 67 65 72 20  ta in the pager 
134d9 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 20 6f  might be moved o
134da 72 20 63 68 61 6e 67 65 64 20 6f 75 74 20 66 72  r changed out fr
134db 6f 6d 20 75 6e 64 65 72 20 75 73 0a 20 20 2a 2a  om under us.  **
134dc 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20   in between the 
134dd 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 69 73  return from this
134de 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
134df 63 61 6c 6c 20 61 6e 64 20 74 68 65 0a 20 20 2a  call and the.  *
134e0 2a 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73  * next call to s
134e1 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20  qlite3_step().  
134e2 53 6f 20 64 65 65 70 68 65 72 6d 65 72 61 6c 69  So deephermerali
134e3 7a 65 20 65 76 65 72 79 74 68 69 6e 67 20 6f 6e  ze everything on
134e4 20 0a 20 20 2a 2a 20 74 68 65 20 73 74 61 63 6b   .  ** the stack
134e5 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 65 70 68  .  Note that eph
134e6 65 6d 65 72 61 6c 20 64 61 74 61 20 69 73 20 6e  emeral data is n
134e7 65 76 65 72 20 73 74 6f 72 65 64 20 69 6e 20 6d  ever stored in m
134e8 65 6d 6f 72 79 20 0a 20 20 2a 2a 20 63 65 6c 6c  emory .  ** cell
134e9 73 20 73 6f 20 77 65 20 64 6f 20 6e 6f 74 20 68  s so we do not h
134ea 61 76 65 20 74 6f 20 77 6f 72 72 79 20 61 62 6f  ave to worry abo
134eb 75 74 20 74 68 65 6d 2e 0a 20 20 2a 2f 0a 20 20  ut them..  */.  
134ec 70 46 69 72 73 74 43 6f 6c 75 6d 6e 20 3d 20 26  pFirstColumn = &
134ed 70 54 6f 73 5b 30 2d 70 4f 70 2d 3e 70 31 5d 3b  pTos[0-pOp->p1];
134ee 0a 20 20 66 6f 72 28 70 4d 65 6d 20 3d 20 70 2d  .  for(pMem = p-
134ef 3e 61 53 74 61 63 6b 3b 20 70 4d 65 6d 3c 70 46  >aStack; pMem<pF
134f0 69 72 73 74 43 6f 6c 75 6d 6e 3b 20 70 4d 65 6d  irstColumn; pMem
134f1 2b 2b 29 7b 0a 20 20 20 20 44 65 65 70 68 65 6d  ++){.    Deephem
134f2 65 72 61 6c 69 7a 65 28 70 4d 65 6d 29 3b 0a 20  eralize(pMem);. 
134f3 20 7d 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 64   }..  /* Invalid
134f4 61 74 65 20 61 6c 6c 20 65 70 68 65 6d 65 72 61  ate all ephemera
134f5 6c 20 63 75 72 73 6f 72 20 72 6f 77 20 63 61 63  l cursor row cac
134f6 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63 61 63 68  hes */.  p->cach
134f7 65 43 74 72 20 3d 20 28 70 2d 3e 63 61 63 68 65  eCtr = (p->cache
134f8 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f  Ctr + 2)|1;..  /
134f9 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
134fa 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 63  results of the c
134fb 75 72 72 65 6e 74 20 72 6f 77 20 61 72 65 20 5c  urrent row are \
134fc 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 20  000 terminated. 
134fd 20 2a 2a 20 61 6e 64 20 68 61 76 65 20 61 6e 20   ** and have an 
134fe 61 73 73 69 67 6e 65 64 20 74 79 70 65 2e 20 20  assigned type.  
134ff 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  The results are 
13500 64 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 64 20  deephemeralized 
13501 61 73 0a 20 20 2a 2a 20 61 73 20 73 69 64 65 20  as.  ** as side 
13502 65 66 66 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 66  effect..  */.  f
13503 6f 72 28 3b 20 70 4d 65 6d 3c 3d 70 54 6f 73 3b  or(; pMem<=pTos;
13504 20 70 4d 65 6d 2b 2b 20 29 7b 0a 20 20 20 20 73   pMem++ ){.    s
13505 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c  qlite3VdbeMemNul
13506 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 3b  Terminate(pMem);
13507 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e  .    storeTypeIn
13508 66 6f 28 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e  fo(pMem, encodin
13509 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  g);.  }..  /* Se
1350a 74 20 75 70 20 74 68 65 20 73 74 61 74 65 6d 65  t up the stateme
1350b 6e 74 20 73 74 72 75 63 74 75 72 65 20 73 6f 20  nt structure so 
1350c 74 68 61 74 20 69 74 20 77 69 6c 6c 20 70 6f 70  that it will pop
1350d 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a   the current.  *
1350e 2a 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 74  * results from t
1350f 68 65 20 73 74 61 63 6b 20 77 68 65 6e 20 74 68  he stack when th
13510 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75  e statement retu
13511 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 72  rns..  */.  p->r
13512 65 73 4f 6e 53 74 61 63 6b 20 3d 20 31 3b 0a 20  esOnStack = 1;. 
13513 20 70 2d 3e 6e 43 61 6c 6c 62 61 63 6b 2b 2b 3b   p->nCallback++;
13514 0a 20 20 70 2d 3e 70 6f 70 53 74 61 63 6b 20 3d  .  p->popStack =
13515 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 70   pOp->p1;.  p->p
13516 63 20 3d 20 70 63 20 2b 20 31 3b 0a 20 20 70 2d  c = pc + 1;.  p-
13517 3e 70 54 6f 73 20 3d 20 70 54 6f 73 3b 0a 20 20  >pTos = pTos;.  
13518 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b  rc = SQLITE_ROW;
13519 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74  .  goto vdbe_ret
1351a 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  urn;.}../* Opcod
1351b 65 3a 20 43 6f 6e 63 61 74 20 50 31 20 50 32 20  e: Concat P1 P2 
1351c 2a 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 20 61 74 20  *.**.** Look at 
1351d 74 68 65 20 66 69 72 73 74 20 50 31 2b 32 20 65  the first P1+2 e
1351e 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 73  lements of the s
1351f 74 61 63 6b 2e 20 20 41 70 70 65 6e 64 20 74 68  tack.  Append th
13520 65 6d 20 61 6c 6c 20 0a 2a 2a 20 74 6f 67 65 74  em all .** toget
13521 68 65 72 20 77 69 74 68 20 74 68 65 20 6c 6f 77  her with the low
13522 65 73 74 20 65 6c 65 6d 65 6e 74 20 66 69 72 73  est element firs
13523 74 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c  t.  The original
13524 20 50 31 2b 32 20 65 6c 65 6d 65 6e 74 73 0a 2a   P1+2 elements.*
13525 2a 20 61 72 65 20 70 6f 70 70 65 64 20 66 72 6f  * are popped fro
13526 6d 20 74 68 65 20 73 74 61 63 6b 20 69 66 20 50  m the stack if P
13527 32 3d 3d 30 20 61 6e 64 20 72 65 74 61 69 6e 65  2==0 and retaine
13528 64 20 69 66 20 50 32 3d 3d 31 2e 20 20 49 66 0a  d if P2==1.  If.
13529 2a 2a 20 61 6e 79 20 65 6c 65 6d 65 6e 74 20 6f  ** any element o
1352a 66 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 4e  f the stack is N
1352b 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ULL, then the re
1352c 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a  sult is NULL..**
1352d 0a 2a 2a 20 57 68 65 6e 20 50 31 3d 3d 31 2c 20  .** When P1==1, 
1352e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b  this routine mak
1352f 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  es a copy of the
13530 20 74 6f 70 20 73 74 61 63 6b 20 65 6c 65 6d 65   top stack eleme
13531 6e 74 0a 2a 2a 20 69 6e 74 6f 20 6d 65 6d 6f 72  nt.** into memor
13532 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
13533 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
13534 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e  ..*/.case OP_Con
13535 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  cat: {          
13536 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43   /* same as TK_C
13537 4f 4e 43 41 54 20 2a 2f 0a 20 20 63 68 61 72 20  ONCAT */.  char 
13538 2a 7a 4e 65 77 3b 0a 20 20 69 36 34 20 6e 42 79  *zNew;.  i64 nBy
13539 74 65 3b 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64  te;.  int nField
1353a 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
1353b 4d 65 6d 20 2a 70 54 65 72 6d 3b 0a 0a 20 20 2f  Mem *pTerm;..  /
1353c 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
1353d 68 65 20 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74  he stack element
1353e 73 20 74 6f 20 73 65 65 20 68 6f 77 20 6c 6f 6e  s to see how lon
1353f 67 20 74 68 65 20 72 65 73 75 6c 74 20 77 69 6c  g the result wil
13540 6c 20 62 65 2e 20 2a 2f 0a 20 20 6e 46 69 65 6c  l be. */.  nFiel
13541 64 20 3d 20 70 4f 70 2d 3e 70 31 20 2b 20 32 3b  d = pOp->p1 + 2;
13542 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 54 6f 73  .  pTerm = &pTos
13543 5b 31 2d 6e 46 69 65 6c 64 5d 3b 0a 20 20 6e 42  [1-nField];.  nB
13544 79 74 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  yte = 0;.  for(i
13545 3d 30 3b 20 69 3c 6e 46 69 65 6c 64 3b 20 69 2b  =0; i<nField; i+
13546 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  +, pTerm++){.   
13547 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
13548 3d 3d 30 20 7c 7c 20 28 70 54 65 72 6d 2d 3e 66  ==0 || (pTerm->f
13549 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 29 3b  lags&MEM_Str) );
1354a 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  .    if( pTerm->
1354b 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29  flags&MEM_Null )
1354c 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20  {.      nByte = 
1354d 2d 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  -1;.      break;
1354e 0a 20 20 20 20 7d 0a 20 20 20 20 45 78 70 61 6e  .    }.    Expan
1354f 64 42 6c 6f 62 28 70 54 65 72 6d 29 3b 0a 20 20  dBlob(pTerm);.  
13550 20 20 53 74 72 69 6e 67 69 66 79 28 70 54 65 72    Stringify(pTer
13551 6d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  m, encoding);.  
13552 20 20 6e 42 79 74 65 20 2b 3d 20 70 54 65 72 6d    nByte += pTerm
13553 2d 3e 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ->n;.  }..  if( 
13554 6e 42 79 74 65 3c 30 20 29 7b 0a 20 20 20 20 2f  nByte<0 ){.    /
13555 2a 20 49 66 20 6e 42 79 74 65 20 69 73 20 6c 65  * If nByte is le
13556 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  ss than zero, th
13557 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 4e 55  en there is a NU
13558 4c 4c 20 76 61 6c 75 65 20 6f 6e 20 74 68 65 20  LL value on the 
13559 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2a 20 49 6e  stack..    ** In
1355a 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 20   this case just 
1355b 70 6f 70 20 74 68 65 20 76 61 6c 75 65 73 20 6f  pop the values o
1355c 66 66 20 74 68 65 20 73 74 61 63 6b 20 28 69 66  ff the stack (if
1355d 20 72 65 71 75 69 72 65 64 29 20 61 6e 64 0a 20   required) and. 
1355e 20 20 20 2a 2a 20 70 75 73 68 20 6f 6e 20 61 20     ** push on a 
1355f 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  NULL..    */.   
13560 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20   if( pOp->p2==0 
13561 29 7b 0a 20 20 20 20 20 20 70 6f 70 53 74 61 63  ){.      popStac
13562 6b 28 26 70 54 6f 73 2c 20 6e 46 69 65 6c 64 29  k(&pTos, nField)
13563 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 73  ;.    }.    pTos
13564 2b 2b 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c  ++;.    pTos->fl
13565 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
13566 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
13567 4f 74 68 65 72 77 69 73 65 20 6d 61 6c 6c 6f 63  Otherwise malloc
13568 28 29 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  () space for the
13569 20 72 65 73 75 6c 74 20 61 6e 64 20 63 6f 6e 63   result and conc
1356a 61 74 65 6e 61 74 65 20 61 6c 6c 20 74 68 65 0a  atenate all the.
1356b 20 20 20 20 2a 2a 20 73 74 61 63 6b 20 76 61 6c      ** stack val
1356c 75 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ues..    */.    
1356d 69 66 28 20 6e 42 79 74 65 2b 32 3e 53 51 4c 49  if( nByte+2>SQLI
1356e 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b  TE_MAX_LENGTH ){
1356f 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  .      goto too_
13570 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  big;.    }.    z
13571 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
13572 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79  allocRaw(db, nBy
13573 74 65 2b 32 20 29 3b 0a 20 20 20 20 69 66 28 20  te+2 );.    if( 
13574 7a 4e 65 77 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  zNew==0 ) goto n
13575 6f 5f 6d 65 6d 3b 0a 20 20 20 20 6a 20 3d 20 30  o_mem;.    j = 0
13576 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70  ;.    pTerm = &p
13577 54 6f 73 5b 31 2d 6e 46 69 65 6c 64 5d 3b 0a 20  Tos[1-nField];. 
13578 20 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c     for(i=j=0; i<
13579 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 54 65  nField; i++, pTe
1357a 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74  rm++){.      int
1357b 20 6e 20 3d 20 70 54 65 72 6d 2d 3e 6e 3b 0a 20   n = pTerm->n;. 
1357c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65       assert( pTe
1357d 72 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  rm->flags & (MEM
1357e 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29  _Str|MEM_Blob) )
1357f 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ;.      memcpy(&
13580 7a 4e 65 77 5b 6a 5d 2c 20 70 54 65 72 6d 2d 3e  zNew[j], pTerm->
13581 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 6a 20 2b  z, n);.      j +
13582 3d 20 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  = n;.    }.    z
13583 4e 65 77 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20  New[j] = 0;.    
13584 7a 4e 65 77 5b 6a 2b 31 5d 20 3d 20 30 3b 0a 20  zNew[j+1] = 0;. 
13585 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e 42     assert( j==nB
13586 79 74 65 20 29 3b 0a 0a 20 20 20 20 69 66 28 20  yte );..    if( 
13587 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20  pOp->p2==0 ){.  
13588 20 20 20 20 70 6f 70 53 74 61 63 6b 28 26 70 54      popStack(&pT
13589 6f 73 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20  os, nField);.   
1358a 20 7d 0a 20 20 20 20 70 54 6f 73 2b 2b 3b 0a 20   }.    pTos++;. 
1358b 20 20 20 70 54 6f 73 2d 3e 6e 20 3d 20 6a 3b 0a     pTos->n = j;.
1358c 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20      pTos->flags 
1358d 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 44 79  = MEM_Str|MEM_Dy
1358e 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  n|MEM_Term;.    
1358f 70 54 6f 73 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a  pTos->xDel = 0;.
13590 20 20 20 20 70 54 6f 73 2d 3e 65 6e 63 20 3d 20      pTos->enc = 
13591 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 20 20 70 54  encoding;.    pT
13592 6f 73 2d 3e 7a 20 3d 20 7a 4e 65 77 3b 0a 20 20  os->z = zNew;.  
13593 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
13594 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 2a 20 2a   Opcode: Add * *
13595 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68 65   *.**.** Pop the
13596 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e 74   top two element
13597 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b  s from the stack
13598 2c 20 61 64 64 20 74 68 65 6d 20 74 6f 67 65 74  , add them toget
13599 68 65 72 2c 0a 2a 2a 20 61 6e 64 20 70 75 73 68  her,.** and push
1359a 20 74 68 65 20 72 65 73 75 6c 74 20 62 61 63 6b   the result back
1359b 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e   onto the stack.
1359c 20 20 49 66 20 65 69 74 68 65 72 20 65 6c 65 6d    If either elem
1359d 65 6e 74 0a 2a 2a 20 69 73 20 61 20 73 74 72 69  ent.** is a stri
1359e 6e 67 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f  ng then it is co
1359f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 64 6f 75  nverted to a dou
135a0 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 61 74  ble using the at
135a1 6f 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  of().** function
135a2 20 62 65 66 6f 72 65 20 74 68 65 20 61 64 64 69   before the addi
135a3 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 65 69 74 68  tion..** If eith
135a4 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55  er operand is NU
135a5 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69  LL, the result i
135a6 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70  s NULL..*/./* Op
135a7 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20 2a  code: Multiply *
135a8 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74   * *.**.** Pop t
135a9 68 65 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65  he top two eleme
135aa 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61  nts from the sta
135ab 63 6b 2c 20 6d 75 6c 74 69 70 6c 79 20 74 68 65  ck, multiply the
135ac 6d 20 74 6f 67 65 74 68 65 72 2c 0a 2a 2a 20 61  m together,.** a
135ad 6e 64 20 70 75 73 68 20 74 68 65 20 72 65 73 75  nd push the resu
135ae 6c 74 20 62 61 63 6b 20 6f 6e 74 6f 20 74 68 65  lt back onto the
135af 20 73 74 61 63 6b 2e 20 20 49 66 20 65 69 74 68   stack.  If eith
135b0 65 72 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20 69 73  er element.** is
135b1 20 61 20 73 74 72 69 6e 67 20 74 68 65 6e 20 69   a string then i
135b2 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 74  t is converted t
135b3 6f 20 61 20 64 6f 75 62 6c 65 20 75 73 69 6e 67  o a double using
135b4 20 74 68 65 20 61 74 6f 66 28 29 0a 2a 2a 20 66   the atof().** f
135b5 75 6e 63 74 69 6f 6e 20 62 65 66 6f 72 65 20 74  unction before t
135b6 68 65 20 6d 75 6c 74 69 70 6c 69 63 61 74 69 6f  he multiplicatio
135b7 6e 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20  n..** If either 
135b8 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c  operand is NULL,
135b9 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
135ba 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ULL..*/./* Opcod
135bb 65 3a 20 53 75 62 74 72 61 63 74 20 2a 20 2a 20  e: Subtract * * 
135bc 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20  *.**.** Pop the 
135bd 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73  top two elements
135be 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 2c   from the stack,
135bf 20 73 75 62 74 72 61 63 74 20 74 68 65 0a 2a 2a   subtract the.**
135c0 20 66 69 72 73 74 20 28 77 68 61 74 20 77 61 73   first (what was
135c1 20 6f 6e 20 74 6f 70 20 6f 66 20 74 68 65 20 73   on top of the s
135c2 74 61 63 6b 29 20 66 72 6f 6d 20 74 68 65 20 73  tack) from the s
135c3 65 63 6f 6e 64 20 28 74 68 65 0a 2a 2a 20 6e 65  econd (the.** ne
135c4 78 74 20 6f 6e 20 73 74 61 63 6b 29 0a 2a 2a 20  xt on stack).** 
135c5 61 6e 64 20 70 75 73 68 20 74 68 65 20 72 65 73  and push the res
135c6 75 6c 74 20 62 61 63 6b 20 6f 6e 74 6f 20 74 68  ult back onto th
135c7 65 20 73 74 61 63 6b 2e 20 20 49 66 20 65 69 74  e stack.  If eit
135c8 68 65 72 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20 69  her element.** i
135c9 73 20 61 20 73 74 72 69 6e 67 20 74 68 65 6e 20  s a string then 
135ca 69 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  it is converted 
135cb 74 6f 20 61 20 64 6f 75 62 6c 65 20 75 73 69 6e  to a double usin
135cc 67 20 74 68 65 20 61 74 6f 66 28 29 0a 2a 2a 20  g the atof().** 
135cd 66 75 6e 63 74 69 6f 6e 20 62 65 66 6f 72 65 20  function before 
135ce 74 68 65 20 73 75 62 74 72 61 63 74 69 6f 6e 2e  the subtraction.
135cf 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70  .** If either op
135d0 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74  erand is NULL, t
135d1 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c  he result is NUL
135d2 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  L..*/./* Opcode:
135d3 20 44 69 76 69 64 65 20 2a 20 2a 20 2a 0a 2a 2a   Divide * * *.**
135d4 0a 2a 2a 20 50 6f 70 20 74 68 65 20 74 6f 70 20  .** Pop the top 
135d5 74 77 6f 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f  two elements fro
135d6 6d 20 74 68 65 20 73 74 61 63 6b 2c 20 64 69 76  m the stack, div
135d7 69 64 65 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  ide the.** first
135d8 20 28 77 68 61 74 20 77 61 73 20 6f 6e 20 74 6f   (what was on to
135d9 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 29 20  p of the stack) 
135da 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20  from the second 
135db 28 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 6e 20  (the.** next on 
135dc 73 74 61 63 6b 29 0a 2a 2a 20 61 6e 64 20 70 75  stack).** and pu
135dd 73 68 20 74 68 65 20 72 65 73 75 6c 74 20 62 61  sh the result ba
135de 63 6b 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63  ck onto the stac
135df 6b 2e 20 20 49 66 20 65 69 74 68 65 72 20 65 6c  k.  If either el
135e0 65 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 20 73 74  ement.** is a st
135e1 72 69 6e 67 20 74 68 65 6e 20 69 74 20 69 73 20  ring then it is 
135e2 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 64  converted to a d
135e3 6f 75 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20  ouble using the 
135e4 61 74 6f 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69  atof().** functi
135e5 6f 6e 20 62 65 66 6f 72 65 20 74 68 65 20 64 69  on before the di
135e6 76 69 73 69 6f 6e 2e 20 20 44 69 76 69 73 69 6f  vision.  Divisio
135e7 6e 20 62 79 20 7a 65 72 6f 20 72 65 74 75 72 6e  n by zero return
135e8 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69  s NULL..** If ei
135e9 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
135ea 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
135eb 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
135ec 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69 6e 64 65  Opcode: Remainde
135ed 72 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f  r * * *.**.** Po
135ee 70 20 74 68 65 20 74 6f 70 20 74 77 6f 20 65 6c  p the top two el
135ef 65 6d 65 6e 74 73 20 66 72 6f 6d 20 74 68 65 20  ements from the 
135f0 73 74 61 63 6b 2c 20 64 69 76 69 64 65 20 74 68  stack, divide th
135f1 65 0a 2a 2a 20 66 69 72 73 74 20 28 77 68 61 74  e.** first (what
135f2 20 77 61 73 20 6f 6e 20 74 6f 70 20 6f 66 20 74   was on top of t
135f3 68 65 20 73 74 61 63 6b 29 20 66 72 6f 6d 20 74  he stack) from t
135f4 68 65 20 73 65 63 6f 6e 64 20 28 74 68 65 0a 2a  he second (the.*
135f5 2a 20 6e 65 78 74 20 6f 6e 20 73 74 61 63 6b 29  * next on stack)
135f6 0a 2a 2a 20 61 6e 64 20 70 75 73 68 20 74 68 65  .** and push the
135f7 20 72 65 6d 61 69 6e 64 65 72 20 61 66 74 65 72   remainder after
135f8 20 64 69 76 69 73 69 6f 6e 20 6f 6e 74 6f 20 74   division onto t
135f9 68 65 20 73 74 61 63 6b 2e 20 20 49 66 20 65 69  he stack.  If ei
135fa 74 68 65 72 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20  ther element.** 
135fb 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 65 6e  is a string then
135fc 20 69 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64   it is converted
135fd 20 74 6f 20 61 20 64 6f 75 62 6c 65 20 75 73 69   to a double usi
135fe 6e 67 20 74 68 65 20 61 74 6f 66 28 29 0a 2a 2a  ng the atof().**
135ff 20 66 75 6e 63 74 69 6f 6e 20 62 65 66 6f 72 65   function before
13600 20 74 68 65 20 64 69 76 69 73 69 6f 6e 2e 20 20   the division.  
13601 44 69 76 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f  Division by zero
13602 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a   returns NULL..*
13603 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70 65 72  * If either oper
13604 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  and is NULL, the
13605 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
13606 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a  .*/.case OP_Add:
13607 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13608 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
13609 5f 50 4c 55 53 2c 20 6e 6f 2d 70 75 73 68 20 2a  _PLUS, no-push *
1360a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61  /.case OP_Subtra
1360b 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  ct:             
1360c 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d   /* same as TK_M
1360d 49 4e 55 53 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f  INUS, no-push */
1360e 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c  .case OP_Multipl
1360f 79 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y:              
13610 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54  /* same as TK_ST
13611 41 52 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63  AR, no-push */.c
13612 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 20  ase OP_Divide:  
13613 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13614 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53   same as TK_SLAS
13615 48 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61  H, no-push */.ca
13616 73 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 3a  se OP_Remainder:
13617 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
13618 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c 20  same as TK_REM, 
13619 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 4d 65 6d  no-push */.  Mem
1361a 20 2a 70 4e 6f 73 20 3d 20 26 70 54 6f 73 5b 2d   *pNos = &pTos[-
1361b 31 5d 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b  1];.  int flags;
1361c 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 73 3e  .  assert( pNos>
1361d 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20  =p->aStack );.  
1361e 66 6c 61 67 73 20 3d 20 70 54 6f 73 2d 3e 66 6c  flags = pTos->fl
1361f 61 67 73 20 7c 20 70 4e 6f 73 2d 3e 66 6c 61 67  ags | pNos->flag
13620 73 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20  s;.  if( (flags 
13621 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29  & MEM_Null)!=0 )
13622 7b 0a 20 20 20 20 52 65 6c 65 61 73 65 28 70 54  {.    Release(pT
13623 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d 2d 3b  os);.    pTos--;
13624 0a 20 20 20 20 52 65 6c 65 61 73 65 28 70 54 6f  .    Release(pTo
13625 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c  s);.    pTos->fl
13626 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
13627 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 54 6f    }else if( (pTo
13628 73 2d 3e 66 6c 61 67 73 20 26 20 70 4e 6f 73 2d  s->flags & pNos-
13629 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1362a 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20  )==MEM_Int ){.  
1362b 20 20 69 36 34 20 61 2c 20 62 3b 0a 20 20 20 20    i64 a, b;.    
1362c 61 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b 0a 20  a = pTos->u.i;. 
1362d 20 20 20 62 20 3d 20 70 4e 6f 73 2d 3e 75 2e 69     b = pNos->u.i
1362e 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f  ;.    switch( pO
1362f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20  p->opcode ){.   
13630 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20     case OP_Add: 
13631 20 20 20 20 20 20 20 20 62 20 2b 3d 20 61 3b 20          b += a; 
13632 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
13633 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72     case OP_Subtr
13634 61 63 74 3a 20 20 20 20 62 20 2d 3d 20 61 3b 20  act:    b -= a; 
13635 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
13636 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69     case OP_Multi
13637 70 6c 79 3a 20 20 20 20 62 20 2a 3d 20 61 3b 20  ply:    b *= a; 
13638 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
13639 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 69 64     case OP_Divid
1363a 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28  e: {.        if(
1363b 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 64 69 76   a==0 ) goto div
1363c 69 64 65 5f 62 79 5f 7a 65 72 6f 3b 0a 20 20 20  ide_by_zero;.   
1363d 20 20 20 20 20 2f 2a 20 44 69 76 69 64 69 6e 67       /* Dividing
1363e 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73   the largest pos
1363f 73 69 62 6c 65 20 6e 65 67 61 74 69 76 65 20 36  sible negative 6
13640 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 28 31  4-bit integer (1
13641 3c 3c 36 33 29 20 62 79 20 0a 20 20 20 20 20 20  <<63) by .      
13642 20 20 2a 2a 20 2d 31 20 72 65 74 75 72 6e 73 20    ** -1 returns 
13643 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 6c 61  an integer to la
13644 72 67 65 20 74 6f 20 73 74 6f 72 65 20 69 6e 20  rge to store in 
13645 61 20 36 34 2d 62 69 74 20 64 61 74 61 2d 74 79  a 64-bit data-ty
13646 70 65 2e 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a  pe. On.        *
13647 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74  * some architect
13648 75 72 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20  ures, the value 
13649 6f 76 65 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c  overflows to (1<
1364a 3c 36 33 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c  <63). On others,
1364b 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 53 49  .        ** a SI
1364c 47 46 50 45 20 69 73 20 69 73 73 75 65 64 2e 20  GFPE is issued. 
1364d 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  The following st
1364e 61 74 65 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a  atement normaliz
1364f 65 73 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  es this.        
13650 2a 2a 20 62 65 68 61 76 69 6f 75 72 20 73 6f 20  ** behaviour so 
13651 74 68 61 74 20 61 6c 6c 20 61 72 63 68 69 74 65  that all archite
13652 63 74 75 72 65 73 20 62 65 68 61 76 65 20 61 73  ctures behave as
13653 20 69 66 20 69 6e 74 65 67 65 72 20 0a 20 20 20   if integer .   
13654 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77       ** overflow
13655 20 6f 63 63 75 72 65 64 2e 0a 20 20 20 20 20 20   occured..      
13656 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28    */.        if(
13657 20 61 3d 3d 2d 31 20 26 26 20 62 3d 3d 28 28 28   a==-1 && b==(((
13658 69 36 34 29 31 29 3c 3c 36 33 29 20 29 20 61 20  i64)1)<<63) ) a 
13659 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 20 2f  = 1;.        b /
1365a 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = a;.        bre
1365b 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1365c 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1365d 20 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20       if( a==0 ) 
1365e 67 6f 74 6f 20 64 69 76 69 64 65 5f 62 79 5f 7a  goto divide_by_z
1365f 65 72 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ero;.        if(
13660 20 61 3d 3d 2d 31 20 29 20 61 20 3d 20 31 3b 0a   a==-1 ) a = 1;.
13661 20 20 20 20 20 20 20 20 62 20 25 3d 20 61 3b 0a          b %= a;.
13662 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
13663 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
13664 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a   Release(pTos);.
13665 20 20 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 20 20      pTos--;.    
13666 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20  Release(pTos);. 
13667 20 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 62     pTos->u.i = b
13668 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ;.    pTos->flag
13669 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 7d  s = MEM_Int;.  }
1366a 65 6c 73 65 7b 0a 20 20 20 20 64 6f 75 62 6c 65  else{.    double
1366b 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20 73   a, b;.    a = s
1366c 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
1366d 6c 75 65 28 70 54 6f 73 29 3b 0a 20 20 20 20 62  lue(pTos);.    b
1366e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1366f 61 6c 56 61 6c 75 65 28 70 4e 6f 73 29 3b 0a 20  alValue(pNos);. 
13670 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e     switch( pOp->
13671 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  opcode ){.      
13672 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20  case OP_Add:    
13673 20 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20       b += a;    
13674 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
13675 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74  case OP_Subtract
13676 3a 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20  :    b -= a;    
13677 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
13678 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79  case OP_Multiply
13679 3a 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20  :    b *= a;    
1367a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1367b 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20  case OP_Divide: 
1367c 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 3d  {.        if( a=
1367d 3d 30 2e 30 20 29 20 67 6f 74 6f 20 64 69 76 69  =0.0 ) goto divi
1367e 64 65 5f 62 79 5f 7a 65 72 6f 3b 0a 20 20 20 20  de_by_zero;.    
1367f 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 20 20 20      b /= a;.    
13680 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
13681 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74   }.      default
13682 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 36 34 20  : {.        i64 
13683 69 61 20 3d 20 28 69 36 34 29 61 3b 0a 20 20 20  ia = (i64)a;.   
13684 20 20 20 20 20 69 36 34 20 69 62 20 3d 20 28 69       i64 ib = (i
13685 36 34 29 62 3b 0a 20 20 20 20 20 20 20 20 69 66  64)b;.        if
13686 28 20 69 61 3d 3d 30 20 29 20 67 6f 74 6f 20 64  ( ia==0 ) goto d
13687 69 76 69 64 65 5f 62 79 5f 7a 65 72 6f 3b 0a 20  ivide_by_zero;. 
13688 20 20 20 20 20 20 20 69 66 28 20 69 61 3d 3d 2d         if( ia==-
13689 31 20 29 20 69 61 20 3d 20 31 3b 0a 20 20 20 20  1 ) ia = 1;.    
1368a 20 20 20 20 62 20 3d 20 69 62 20 25 20 69 61 3b      b = ib % ia;
1368b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1368c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1368d 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 73    if( sqlite3_is
1368e 6e 61 6e 28 62 29 20 29 7b 0a 20 20 20 20 20 20  nan(b) ){.      
1368f 67 6f 74 6f 20 64 69 76 69 64 65 5f 62 79 5f 7a  goto divide_by_z
13690 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 52  ero;.    }.    R
13691 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20 20  elease(pTos);.  
13692 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 20 20 52 65    pTos--;.    Re
13693 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20 20 20  lease(pTos);.   
13694 20 70 54 6f 73 2d 3e 72 20 3d 20 62 3b 0a 20 20   pTos->r = b;.  
13695 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
13696 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 69 66  MEM_Real;.    if
13697 28 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  ( (flags & MEM_R
13698 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  eal)==0 ){.     
13699 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65   sqlite3VdbeInte
1369a 67 65 72 41 66 66 69 6e 69 74 79 28 70 54 6f 73  gerAffinity(pTos
1369b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  );.    }.  }.  b
1369c 72 65 61 6b 3b 0a 0a 64 69 76 69 64 65 5f 62 79  reak;..divide_by
1369d 5f 7a 65 72 6f 3a 0a 20 20 52 65 6c 65 61 73 65  _zero:.  Release
1369e 28 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 2d  (pTos);.  pTos--
1369f 3b 0a 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73  ;.  Release(pTos
136a0 29 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73  );.  pTos->flags
136a1 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 62   = MEM_Null;.  b
136a2 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
136a3 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a 20  de: CollSeq * * 
136a4 50 33 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61  P3.**.** P3 is a
136a5 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f   pointer to a Co
136a6 6c 6c 53 65 71 20 73 74 72 75 63 74 2e 20 49 66  llSeq struct. If
136a7 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74   the next call t
136a8 6f 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f  o a user functio
136a9 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 61 74  n.** or aggregat
136aa 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 47  e calls sqlite3G
136ab 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 2c  etFuncCollSeq(),
136ac 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20   this collation 
136ad 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a 2a  sequence will.**
136ae 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68   be returned. Th
136af 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68  is is used by th
136b0 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28 29  e built-in min()
136b1 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c 6c  , max() and null
136b2 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  if().** function
136b3 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74  s..**.** The int
136b4 65 72 66 61 63 65 20 75 73 65 64 20 62 79 20 74  erface used by t
136b5 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
136b6 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 65 6d 65  n of the aforeme
136b7 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 6f 6e  ntioned function
136b8 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 76 65  s.** to retrieve
136b9 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
136ba 65 71 75 65 6e 63 65 20 73 65 74 20 62 79 20 74  equence set by t
136bb 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6e 6f  his opcode is no
136bc 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 70  t available.** p
136bd 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 74 6f  ublicly, only to
136be 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 20   user functions 
136bf 64 65 66 69 6e 65 64 20 69 6e 20 66 75 6e 63 2e  defined in func.
136c0 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f  c..*/.case OP_Co
136c1 6c 6c 53 65 71 3a 20 7b 20 20 20 20 20 20 20 20  llSeq: {        
136c2 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
136c3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
136c4 2d 3e 70 33 74 79 70 65 3d 3d 50 33 5f 43 4f 4c  ->p3type==P3_COL
136c5 4c 53 45 51 20 29 3b 0a 20 20 62 72 65 61 6b 3b  LSEQ );.  break;
136c6 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46  .}../* Opcode: F
136c7 75 6e 63 74 69 6f 6e 20 50 31 20 50 32 20 50 33  unction P1 P2 P3
136c8 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20  .**.** Invoke a 
136c9 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50  user function (P
136ca 33 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  3 is a pointer t
136cb 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72  o a Function str
136cc 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64  ucture that.** d
136cd 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74  efines the funct
136ce 69 6f 6e 29 20 77 69 74 68 20 50 32 20 61 72 67  ion) with P2 arg
136cf 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f  uments taken fro
136d0 6d 20 74 68 65 20 73 74 61 63 6b 2e 20 20 50 6f  m the stack.  Po
136d1 70 20 61 6c 6c 0a 2a 2a 20 61 72 67 75 6d 65 6e  p all.** argumen
136d2 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ts from the stac
136d3 6b 20 61 6e 64 20 70 75 73 68 20 62 61 63 6b 20  k and push back 
136d4 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a  the result..**.*
136d5 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 69 74  * P1 is a 32-bit
136d6 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74   bitmask indicat
136d7 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ing whether or n
136d8 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74  ot each argument
136d9 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63   to the .** func
136da 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d 69  tion was determi
136db 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74 61  ned to be consta
136dc 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  nt at compile ti
136dd 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 73 74  me. If the first
136de 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61 73  .** argument was
136df 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 62   constant then b
136e0 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73 65  it 0 of P1 is se
136e1 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  t. This is used 
136e2 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20  to determine.** 
136e3 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 61 74  whether meta dat
136e4 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
136e5 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f  h a user functio
136e6 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e 67  n argument using
136e7 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
136e8 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50  set_auxdata() AP
136e9 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20  I may be safely 
136ea 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 74  retained until t
136eb 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63  he next.** invoc
136ec 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70  ation of this op
136ed 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  code..**.** See 
136ee 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61 6e  also: AggStep an
136ef 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61  d AggFinal.*/.ca
136f0 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20  se OP_Function: 
136f1 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d  {.  int i;.  Mem
136f2 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65   *pArg;.  sqlite
136f3 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20  3_context ctx;. 
136f4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
136f5 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 20  *apVal;.  int n 
136f6 3d 20 70 4f 70 2d 3e 70 32 3b 0a 0a 20 20 61 70  = pOp->p2;..  ap
136f7 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a  Val = p->apArg;.
136f8 20 20 61 73 73 65 72 74 28 20 61 70 56 61 6c 20    assert( apVal 
136f9 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 20 70 41  || n==0 );..  pA
136fa 72 67 20 3d 20 26 70 54 6f 73 5b 31 2d 6e 5d 3b  rg = &pTos[1-n];
136fb 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b  .  for(i=0; i<n;
136fc 20 69 2b 2b 2c 20 70 41 72 67 2b 2b 29 7b 0a 20   i++, pArg++){. 
136fd 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 41     apVal[i] = pA
136fe 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70  rg;.    storeTyp
136ff 65 49 6e 66 6f 28 70 41 72 67 2c 20 65 6e 63 6f  eInfo(pArg, enco
13700 64 69 6e 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73  ding);.  }..  as
13701 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 74 79 70  sert( pOp->p3typ
13702 65 3d 3d 50 33 5f 46 55 4e 43 44 45 46 20 7c 7c  e==P3_FUNCDEF ||
13703 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50 33   pOp->p3type==P3
13704 5f 56 44 42 45 46 55 4e 43 20 29 3b 0a 20 20 69  _VDBEFUNC );.  i
13705 66 28 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d  f( pOp->p3type==
13706 50 33 5f 46 55 4e 43 44 45 46 20 29 7b 0a 20 20  P3_FUNCDEF ){.  
13707 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 28 46    ctx.pFunc = (F
13708 75 6e 63 44 65 66 2a 29 70 4f 70 2d 3e 70 33 3b  uncDef*)pOp->p3;
13709 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75  .    ctx.pVdbeFu
1370a 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  nc = 0;.  }else{
1370b 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75  .    ctx.pVdbeFu
1370c 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a 29  nc = (VdbeFunc*)
1370d 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 63 74 78  pOp->p3;.    ctx
1370e 2e 70 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 64  .pFunc = ctx.pVd
1370f 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20  beFunc->pFunc;. 
13710 20 7d 0a 0a 20 20 63 74 78 2e 73 2e 66 6c 61 67   }..  ctx.s.flag
13711 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
13712 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 63  ctx.s.z = 0;.  c
13713 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20  tx.s.xDel = 0;. 
13714 20 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a   ctx.s.db = db;.
13715 20 20 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20    ctx.isError = 
13716 30 3b 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75  0;.  if( ctx.pFu
13717 6e 63 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20  nc->needCollSeq 
13718 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
13719 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20  Op>p->aOp );.   
1371a 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d   assert( pOp[-1]
1371b 2e 70 33 74 79 70 65 3d 3d 50 33 5f 43 4f 4c 4c  .p3type==P3_COLL
1371c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72  SEQ );.    asser
1371d 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64  t( pOp[-1].opcod
1371e 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b  e==OP_CollSeq );
1371f 0a 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d  .    ctx.pColl =
13720 20 28 43 6f 6c 6c 53 65 71 20 2a 29 70 4f 70 5b   (CollSeq *)pOp[
13721 2d 31 5d 2e 70 33 3b 0a 20 20 7d 0a 20 20 69 66  -1].p3;.  }.  if
13722 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
13723 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
13724 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
13725 65 3b 0a 20 20 28 2a 63 74 78 2e 70 46 75 6e 63  e;.  (*ctx.pFunc
13726 2d 3e 78 46 75 6e 63 29 28 26 63 74 78 2c 20 6e  ->xFunc)(&ctx, n
13727 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20  , apVal);.  if( 
13728 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
13729 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1372a 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1372b 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
1372c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a  Failed ){.    /*
1372d 20 45 76 65 6e 20 74 68 6f 75 67 68 20 61 20 6d   Even though a m
1372e 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c  alloc() has fail
1372f 65 64 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ed, the implemen
13730 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20  tation of the.  
13731 20 20 2a 2a 20 75 73 65 72 20 66 75 6e 63 74 69    ** user functi
13732 6f 6e 20 6d 61 79 20 68 61 76 65 20 63 61 6c 6c  on may have call
13733 65 64 20 61 6e 20 73 71 6c 69 74 65 33 5f 72 65  ed an sqlite3_re
13734 73 75 6c 74 5f 58 58 58 28 29 20 66 75 6e 63 74  sult_XXX() funct
13735 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65  ion.    ** to re
13736 74 75 72 6e 20 61 20 76 61 6c 75 65 2e 20 54 68  turn a value. Th
13737 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c  e following call
13738 20 72 65 6c 65 61 73 65 73 20 61 6e 79 20 72 65   releases any re
13739 73 6f 75 72 63 65 73 0a 20 20 20 20 2a 2a 20 61  sources.    ** a
1373a 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73  ssociated with s
1373b 75 63 68 20 61 20 76 61 6c 75 65 2e 0a 20 20 20  uch a value..   
1373c 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a   **.    ** Note:
1373d 20 4d 61 79 62 65 20 4d 65 6d 52 65 6c 65 61 73   Maybe MemReleas
1373e 65 28 29 20 73 68 6f 75 6c 64 20 62 65 20 63 61  e() should be ca
1373f 6c 6c 65 64 20 69 66 20 73 71 6c 69 74 65 33 53  lled if sqlite3S
13740 61 66 65 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a  afetyOn().    **
13741 20 66 61 69 6c 73 20 61 6c 73 6f 20 28 74 68 65   fails also (the
13742 20 69 66 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65   if(...) stateme
13743 6e 74 20 61 62 6f 76 65 29 2e 20 42 75 74 20 69  nt above). But i
13744 66 20 70 65 6f 70 6c 65 20 61 72 65 0a 20 20 20  f people are.   
13745 20 2a 2a 20 6d 69 73 75 73 69 6e 67 20 73 71 6c   ** misusing sql
13746 69 74 65 2c 20 74 68 65 79 20 68 61 76 65 20 62  ite, they have b
13747 69 67 67 65 72 20 70 72 6f 62 6c 65 6d 73 20 74  igger problems t
13748 68 61 6e 20 61 20 6c 65 61 6b 65 64 20 76 61 6c  han a leaked val
13749 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ue..    */.    s
1374a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1374b 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20  ease(&ctx.s);.  
1374c 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1374d 20 7d 0a 20 20 70 6f 70 53 74 61 63 6b 28 26 70   }.  popStack(&p
1374e 54 6f 73 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 49  Tos, n);..  /* I
1374f 66 20 61 6e 79 20 61 75 78 69 6c 61 72 79 20 64  f any auxilary d
13750 61 74 61 20 66 75 6e 63 74 69 6f 6e 73 20 68 61  ata functions ha
13751 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 62  ve been called b
13752 79 20 74 68 69 73 20 75 73 65 72 20 66 75 6e 63  y this user func
13753 74 69 6f 6e 2c 0a 20 20 2a 2a 20 69 6d 6d 65 64  tion,.  ** immed
13754 69 61 74 65 6c 79 20 63 61 6c 6c 20 74 68 65 20  iately call the 
13755 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 61  destructor for a
13756 6e 79 20 6e 6f 6e 2d 73 74 61 74 69 63 20 76 61  ny non-static va
13757 6c 75 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  lues..  */.  if(
13758 20 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 29   ctx.pVdbeFunc )
13759 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1375a 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 63  eDeleteAuxData(c
1375b 74 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f  tx.pVdbeFunc, pO
1375c 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d  p->p1);.    pOp-
1375d 3e 70 33 20 3d 20 28 63 68 61 72 20 2a 29 63 74  >p3 = (char *)ct
1375e 78 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20  x.pVdbeFunc;.   
1375f 20 70 4f 70 2d 3e 70 33 74 79 70 65 20 3d 20 50   pOp->p3type = P
13760 33 5f 56 44 42 45 46 55 4e 43 3b 0a 20 20 7d 0a  3_VDBEFUNC;.  }.
13761 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 75 6e  .  /* If the fun
13762 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61  ction returned a
13763 6e 20 65 72 72 6f 72 2c 20 74 68 72 6f 77 20 61  n error, throw a
13764 6e 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20  n exception */. 
13765 20 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f 72   if( ctx.isError
13766 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
13767 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
13768 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  rMsg, sqlite3_va
13769 6c 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73 29  lue_text(&ctx.s)
1376a 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20  , (char*)0);.   
1376b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1376c 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f  OR;.  }..  /* Co
1376d 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  py the result of
1376e 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f   the function to
1376f 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
13770 73 74 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74  stack */.  sqlit
13771 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
13772 64 69 6e 67 28 26 63 74 78 2e 73 2c 20 65 6e 63  ding(&ctx.s, enc
13773 6f 64 69 6e 67 29 3b 0a 20 20 70 54 6f 73 2b 2b  oding);.  pTos++
13774 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20  ;.  pTos->flags 
13775 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  = 0;.  sqlite3Vd
13776 62 65 4d 65 6d 4d 6f 76 65 28 70 54 6f 73 2c 20  beMemMove(pTos, 
13777 26 63 74 78 2e 73 29 3b 0a 20 20 69 66 28 20 73  &ctx.s);.  if( s
13778 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f  qlite3VdbeMemToo
13779 42 69 67 28 70 54 6f 73 29 20 29 7b 0a 20 20 20  Big(pTos) ){.   
1377a 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
1377b 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1377c 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 41 6e 64  * Opcode: BitAnd
1377d 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70   * * *.**.** Pop
1377e 20 74 68 65 20 74 6f 70 20 74 77 6f 20 65 6c 65   the top two ele
1377f 6d 65 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 73  ments from the s
13780 74 61 63 6b 2e 20 20 43 6f 6e 76 65 72 74 20 62  tack.  Convert b
13781 6f 74 68 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20  oth elements.** 
13782 74 6f 20 69 6e 74 65 67 65 72 73 2e 20 20 50 75  to integers.  Pu
13783 73 68 20 62 61 63 6b 20 6f 6e 74 6f 20 74 68 65  sh back onto the
13784 20 73 74 61 63 6b 20 74 68 65 20 62 69 74 2d 77   stack the bit-w
13785 69 73 65 20 41 4e 44 20 6f 66 20 74 68 65 0a 2a  ise AND of the.*
13786 2a 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73 2e 0a  * two elements..
13787 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70 65  ** If either ope
13788 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68  rand is NULL, th
13789 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1378a 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1378b 42 69 74 4f 72 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  BitOr * * *.**.*
1378c 2a 20 50 6f 70 20 74 68 65 20 74 6f 70 20 74 77  * Pop the top tw
1378d 6f 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20  o elements from 
1378e 74 68 65 20 73 74 61 63 6b 2e 20 20 43 6f 6e 76  the stack.  Conv
1378f 65 72 74 20 62 6f 74 68 20 65 6c 65 6d 65 6e 74  ert both element
13790 73 0a 2a 2a 20 74 6f 20 69 6e 74 65 67 65 72 73  s.** to integers
13791 2e 20 20 50 75 73 68 20 62 61 63 6b 20 6f 6e 74  .  Push back ont
13792 6f 20 74 68 65 20 73 74 61 63 6b 20 74 68 65 20  o the stack the 
13793 62 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74  bit-wise OR of t
13794 68 65 0a 2a 2a 20 74 77 6f 20 65 6c 65 6d 65 6e  he.** two elemen
13795 74 73 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  ts..** If either
13796 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
13797 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
13798 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
13799 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 2a 20  de: ShiftLeft * 
1379a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68  * *.**.** Pop th
1379b 65 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e  e top two elemen
1379c 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ts from the stac
1379d 6b 2e 20 20 43 6f 6e 76 65 72 74 20 62 6f 74 68  k.  Convert both
1379e 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 6f 20   elements.** to 
1379f 69 6e 74 65 67 65 72 73 2e 20 20 50 75 73 68 20  integers.  Push 
137a0 62 61 63 6b 20 6f 6e 74 6f 20 74 68 65 20 73 74  back onto the st
137a1 61 63 6b 20 74 68 65 20 73 65 63 6f 6e 64 20 65  ack the second e
137a2 6c 65 6d 65 6e 74 20 73 68 69 66 74 65 64 0a 2a  lement shifted.*
137a3 2a 20 6c 65 66 74 20 62 79 20 4e 20 62 69 74 73  * left by N bits
137a4 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
137a5 74 6f 70 20 65 6c 65 6d 65 6e 74 20 6f 6e 20 74  top element on t
137a6 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 49 66 20  he stack..** If 
137a7 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69  either operand i
137a8 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
137a9 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
137aa 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52  * Opcode: ShiftR
137ab 69 67 68 74 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  ight * * *.**.**
137ac 20 50 6f 70 20 74 68 65 20 74 6f 70 20 74 77 6f   Pop the top two
137ad 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 74   elements from t
137ae 68 65 20 73 74 61 63 6b 2e 20 20 43 6f 6e 76 65  he stack.  Conve
137af 72 74 20 62 6f 74 68 20 65 6c 65 6d 65 6e 74 73  rt both elements
137b0 0a 2a 2a 20 74 6f 20 69 6e 74 65 67 65 72 73 2e  .** to integers.
137b1 20 20 50 75 73 68 20 62 61 63 6b 20 6f 6e 74 6f    Push back onto
137b2 20 74 68 65 20 73 74 61 63 6b 20 74 68 65 20 73   the stack the s
137b3 65 63 6f 6e 64 20 65 6c 65 6d 65 6e 74 20 73 68  econd element sh
137b4 69 66 74 65 64 0a 2a 2a 20 72 69 67 68 74 20 62  ifted.** right b
137b5 79 20 4e 20 62 69 74 73 20 77 68 65 72 65 20 4e  y N bits where N
137b6 20 69 73 20 74 68 65 20 74 6f 70 20 65 6c 65 6d   is the top elem
137b7 65 6e 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  ent on the stack
137b8 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f  ..** If either o
137b9 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20  perand is NULL, 
137ba 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
137bb 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42  LL..*/.case OP_B
137bc 69 74 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20  itAnd:          
137bd 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
137be 73 20 54 4b 5f 42 49 54 41 4e 44 2c 20 6e 6f 2d  s TK_BITAND, no-
137bf 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  push */.case OP_
137c0 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20  BitOr:          
137c1 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
137c2 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 6e 6f 2d  as TK_BITOR, no-
137c3 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  push */.case OP_
137c4 53 68 69 66 74 4c 65 66 74 3a 20 20 20 20 20 20  ShiftLeft:      
137c5 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
137c6 61 73 20 54 4b 5f 4c 53 48 49 46 54 2c 20 6e 6f  as TK_LSHIFT, no
137c7 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50  -push */.case OP
137c8 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20  _ShiftRight: {  
137c9 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
137ca 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 6e   as TK_RSHIFT, n
137cb 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 4d 65 6d 20  o-push */.  Mem 
137cc 2a 70 4e 6f 73 20 3d 20 26 70 54 6f 73 5b 2d 31  *pNos = &pTos[-1
137cd 5d 3b 0a 20 20 69 36 34 20 61 2c 20 62 3b 0a 0a  ];.  i64 a, b;..
137ce 20 20 61 73 73 65 72 74 28 20 70 4e 6f 73 3e 3d    assert( pNos>=
137cf 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 69  p->aStack );.  i
137d0 66 28 20 28 70 54 6f 73 2d 3e 66 6c 61 67 73 20  f( (pTos->flags 
137d1 7c 20 70 4e 6f 73 2d 3e 66 6c 61 67 73 29 20 26  | pNos->flags) &
137d2 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
137d3 20 70 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c   popStack(&pTos,
137d4 20 32 29 3b 0a 20 20 20 20 70 54 6f 73 2b 2b 3b   2);.    pTos++;
137d5 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73  .    pTos->flags
137d6 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
137d7 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 61 20   break;.  }.  a 
137d8 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
137d9 56 61 6c 75 65 28 70 4e 6f 73 29 3b 0a 20 20 62  Value(pNos);.  b
137da 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
137db 74 56 61 6c 75 65 28 70 54 6f 73 29 3b 0a 20 20  tValue(pTos);.  
137dc 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63  switch( pOp->opc
137dd 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ode ){.    case 
137de 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20  OP_BitAnd:      
137df 61 20 26 3d 20 62 3b 20 20 20 20 20 62 72 65 61  a &= b;     brea
137e0 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42  k;.    case OP_B
137e1 69 74 4f 72 3a 20 20 20 20 20 20 20 61 20 7c 3d  itOr:       a |=
137e2 20 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20   b;     break;. 
137e3 20 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74     case OP_Shift
137e4 4c 65 66 74 3a 20 20 20 61 20 3c 3c 3d 20 62 3b  Left:   a <<= b;
137e5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
137e6 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68  ase OP_ShiftRigh
137e7 74 3a 20 20 61 20 3e 3e 3d 20 62 3b 20 20 20 20  t:  a >>= b;    
137e8 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75  break;.    defau
137e9 6c 74 3a 20 20 20 2f 2a 20 43 41 4e 54 20 48 41  lt:   /* CANT HA
137ea 50 50 45 4e 20 2a 2f 20 20 20 20 20 62 72 65 61  PPEN */     brea
137eb 6b 3b 0a 20 20 7d 0a 20 20 52 65 6c 65 61 73 65  k;.  }.  Release
137ec 28 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 2d  (pTos);.  pTos--
137ed 3b 0a 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73  ;.  Release(pTos
137ee 29 3b 0a 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d  );.  pTos->u.i =
137ef 20 61 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67   a;.  pTos->flag
137f0 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 62  s = MEM_Int;.  b
137f1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
137f2 64 65 3a 20 41 64 64 49 6d 6d 20 20 50 31 20 2a  de: AddImm  P1 *
137f3 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68   *.** .** Add th
137f4 65 20 76 61 6c 75 65 20 50 31 20 74 6f 20 77 68  e value P1 to wh
137f5 61 74 65 76 65 72 20 69 73 20 6f 6e 20 74 6f 70  atever is on top
137f6 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20 20   of the stack.  
137f7 54 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73  The result.** is
137f8 20 61 6c 77 61 79 73 20 61 6e 20 69 6e 74 65 67   always an integ
137f9 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 66 6f 72  er..**.** To for
137fa 63 65 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  ce the top of th
137fb 65 20 73 74 61 63 6b 20 74 6f 20 62 65 20 61 6e  e stack to be an
137fc 20 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 61   integer, just a
137fd 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  dd 0..*/.case OP
137fe 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20  _AddImm: {      
137ff 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
13800 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
13801 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b  os>=p->aStack );
13802 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
13803 6d 49 6e 74 65 67 65 72 69 66 79 28 70 54 6f 73  mIntegerify(pTos
13804 29 3b 0a 20 20 70 54 6f 73 2d 3e 75 2e 69 20 2b  );.  pTos->u.i +
13805 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 62 72 65  = pOp->p1;.  bre
13806 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
13807 3a 20 46 6f 72 63 65 49 6e 74 20 50 31 20 50 32  : ForceInt P1 P2
13808 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74   *.**.** Convert
13809 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
1380a 73 74 61 63 6b 20 69 6e 74 6f 20 61 6e 20 69 6e  stack into an in
1380b 74 65 67 65 72 2e 20 20 49 66 20 74 68 65 20 63  teger.  If the c
1380c 75 72 72 65 6e 74 20 74 6f 70 20 6f 66 0a 2a 2a  urrent top of.**
1380d 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 6e 6f   the stack is no
1380e 74 20 6e 75 6d 65 72 69 63 20 28 6d 65 61 6e 69  t numeric (meani
1380f 6e 67 20 74 68 61 74 20 69 73 20 69 73 20 61 20  ng that is is a 
13810 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67  NULL or a string
13811 20 74 68 61 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f   that.** does no
13812 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 6e 20 69  t look like an i
13813 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 69  nteger or floati
13814 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 29  ng point number)
13815 20 74 68 65 6e 20 70 6f 70 20 74 68 65 0a 2a 2a   then pop the.**
13816 20 73 74 61 63 6b 20 61 6e 64 20 6a 75 6d 70 20   stack and jump 
13817 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65 20 74  to P2.  If the t
13818 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
13819 69 73 20 6e 75 6d 65 72 69 63 20 74 68 65 6e 0a  is numeric then.
1381a 2a 2a 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e  ** convert it in
1381b 74 6f 20 74 68 65 20 6c 65 61 73 74 20 69 6e 74  to the least int
1381c 65 67 65 72 20 74 68 61 74 20 69 73 20 67 72 65  eger that is gre
1381d 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
1381e 61 6c 20 74 6f 20 69 74 73 0a 2a 2a 20 63 75 72  al to its.** cur
1381f 72 65 6e 74 20 76 61 6c 75 65 20 69 66 20 50 31  rent value if P1
13820 3d 3d 30 2c 20 6f 72 20 74 6f 20 74 68 65 20 6c  ==0, or to the l
13821 65 61 73 74 20 69 6e 74 65 67 65 72 20 74 68 61  east integer tha
13822 74 20 69 73 20 73 74 72 69 63 74 6c 79 0a 2a 2a  t is strictly.**
13823 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 74   greater than it
13824 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  s current value 
13825 69 66 20 50 31 3d 3d 31 2e 0a 2a 2f 0a 63 61 73  if P1==1..*/.cas
13826 65 20 4f 50 5f 46 6f 72 63 65 49 6e 74 3a 20 7b  e OP_ForceInt: {
13827 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
13828 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 36 34 20  o-push */.  i64 
13829 76 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  v;.  assert( pTo
1382a 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a  s>=p->aStack );.
1382b 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
1382c 70 54 6f 73 2c 20 53 51 4c 49 54 45 5f 41 46 46  pTos, SQLITE_AFF
1382d 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69  _NUMERIC, encodi
1382e 6e 67 29 3b 0a 20 20 69 66 28 20 28 70 54 6f 73  ng);.  if( (pTos
1382f 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49  ->flags & (MEM_I
13830 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d 30  nt|MEM_Real))==0
13831 20 29 7b 0a 20 20 20 20 52 65 6c 65 61 73 65 28   ){.    Release(
13832 70 54 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d  pTos);.    pTos-
13833 2d 3b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  -;.    pc = pOp-
13834 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 62 72 65  >p2 - 1;.    bre
13835 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 54  ak;.  }.  if( pT
13836 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  os->flags & MEM_
13837 49 6e 74 20 29 7b 0a 20 20 20 20 76 20 3d 20 70  Int ){.    v = p
13838 54 6f 73 2d 3e 75 2e 69 20 2b 20 28 70 4f 70 2d  Tos->u.i + (pOp-
13839 3e 70 31 21 3d 30 29 3b 0a 20 20 7d 65 6c 73 65  >p1!=0);.  }else
1383a 7b 0a 20 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a  {.    /* FIX ME:
1383b 20 20 73 68 6f 75 6c 64 20 74 68 69 73 20 6e 6f    should this no
1383c 74 20 62 65 20 61 73 73 65 72 74 28 20 70 54 6f  t be assert( pTo
1383d 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  s->flags & MEM_R
1383e 65 61 6c 20 29 20 3f 3f 3f 20 2a 2f 0a 20 20 20  eal ) ??? */.   
1383f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
13840 65 61 6c 69 66 79 28 70 54 6f 73 29 3b 0a 20 20  ealify(pTos);.  
13841 20 20 76 20 3d 20 28 69 6e 74 29 70 54 6f 73 2d    v = (int)pTos-
13842 3e 72 3b 0a 20 20 20 20 69 66 28 20 70 54 6f 73  >r;.    if( pTos
13843 2d 3e 72 3e 28 64 6f 75 62 6c 65 29 76 20 29 20  ->r>(double)v ) 
13844 76 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 4f 70  v++;.    if( pOp
13845 2d 3e 70 31 20 26 26 20 70 54 6f 73 2d 3e 72 3d  ->p1 && pTos->r=
13846 3d 28 64 6f 75 62 6c 65 29 76 20 29 20 76 2b 2b  =(double)v ) v++
13847 3b 0a 20 20 7d 0a 20 20 52 65 6c 65 61 73 65 28  ;.  }.  Release(
13848 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 3e 75  pTos);.  pTos->u
13849 2e 69 20 3d 20 76 3b 0a 20 20 70 54 6f 73 2d 3e  .i = v;.  pTos->
1384a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1384b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1384c 4f 70 63 6f 64 65 3a 20 4d 75 73 74 42 65 49 6e  Opcode: MustBeIn
1384d 74 20 50 31 20 50 32 20 2a 0a 2a 2a 20 0a 2a 2a  t P1 P2 *.** .**
1384e 20 46 6f 72 63 65 20 74 68 65 20 74 6f 70 20 6f   Force the top o
1384f 66 20 74 68 65 20 73 74 61 63 6b 20 74 6f 20 62  f the stack to b
13850 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49  e an integer.  I
13851 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  f the top of the
13852 0a 2a 2a 20 73 74 61 63 6b 20 69 73 20 6e 6f 74  .** stack is not
13853 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20   an integer and 
13854 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72  cannot be conver
13855 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65  ted into an inte
13856 67 65 72 0a 2a 2a 20 77 69 74 68 20 6f 75 74 20  ger.** with out 
13857 64 61 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e 20  data loss, then 
13858 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
13859 20 74 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 32   to P2, or if P2
1385a 3d 3d 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20  ==0.** raise an 
1385b 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20  SQLITE_MISMATCH 
1385c 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  exception..**.**
1385d 20 49 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74   If the top of t
1385e 68 65 20 73 74 61 63 6b 20 69 73 20 6e 6f 74 20  he stack is not 
1385f 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20 50  an integer and P
13860 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 61 6e  2 is not zero an
13861 64 0a 2a 2a 20 50 31 20 69 73 20 31 2c 20 74 68  d.** P1 is 1, th
13862 65 6e 20 74 68 65 20 73 74 61 63 6b 20 69 73 20  en the stack is 
13863 70 6f 70 70 65 64 2e 20 20 49 6e 20 61 6c 6c 20  popped.  In all 
13864 6f 74 68 65 72 20 63 61 73 65 73 2c 20 74 68 65  other cases, the
13865 20 64 65 70 74 68 0a 2a 2a 20 6f 66 20 74 68 65   depth.** of the
13866 20 73 74 61 63 6b 20 69 73 20 75 6e 63 68 61 6e   stack is unchan
13867 67 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ged..*/.case OP_
13868 4d 75 73 74 42 65 49 6e 74 3a 20 7b 20 20 20 20  MustBeInt: {    
13869 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
1386a 73 68 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  sh */.  assert( 
1386b 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20  pTos>=p->aStack 
1386c 29 3b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69  );.  applyAffini
1386d 74 79 28 70 54 6f 73 2c 20 53 51 4c 49 54 45 5f  ty(pTos, SQLITE_
1386e 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63  AFF_NUMERIC, enc
1386f 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 20 28 70  oding);.  if( (p
13870 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Tos->flags & MEM
13871 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20  _Int)==0 ){.    
13872 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29  if( pOp->p2==0 )
13873 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
13874 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b 0a 20 20  ITE_MISMATCH;.  
13875 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
13876 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
13877 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
13878 28 20 70 4f 70 2d 3e 70 31 20 29 20 70 6f 70 53  ( pOp->p1 ) popS
13879 74 61 63 6b 28 26 70 54 6f 73 2c 20 31 29 3b 0a  tack(&pTos, 1);.
1387a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1387b 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  p2 - 1;.    }.  
1387c 7d 65 6c 73 65 7b 0a 20 20 20 20 52 65 6c 65 61  }else{.    Relea
1387d 73 65 28 70 54 6f 73 29 3b 0a 20 20 20 20 70 54  se(pTos);.    pT
1387e 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  os->flags = MEM_
1387f 49 6e 74 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  Int;.  }.  break
13880 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
13881 52 65 61 6c 41 66 66 69 6e 69 74 79 20 2a 20 2a  RealAffinity * *
13882 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20   *.**.** If the 
13883 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
13884 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2c 20   is an integer, 
13885 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20  convert it to a 
13886 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  real value..**.*
13887 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73  * This opcode is
13888 20 75 73 65 64 20 77 68 65 6e 20 65 78 74 72 61   used when extra
13889 63 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  cting informatio
1388a 6e 20 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20  n from a column 
1388b 74 68 61 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c  that.** has REAL
1388c 20 61 66 66 69 6e 69 74 79 2e 20 20 53 75 63 68   affinity.  Such
1388d 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d   column values m
1388e 61 79 20 73 74 69 6c 6c 20 62 65 20 73 74 6f 72  ay still be stor
1388f 65 64 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72  ed as.** integer
13890 73 2c 20 66 6f 72 20 73 70 61 63 65 20 65 66 66  s, for space eff
13891 69 63 69 65 6e 63 79 2c 20 62 75 74 20 61 66 74  iciency, but aft
13892 65 72 20 65 78 74 72 61 63 74 69 6f 6e 20 77 65  er extraction we
13893 20 77 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f   want them.** to
13894 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 72 65 61   have only a rea
13895 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65  l value..*/.case
13896 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79   OP_RealAffinity
13897 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
13898 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
13899 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  */.  assert( pTo
1389a 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a  s>=p->aStack );.
1389b 20 20 69 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67    if( pTos->flag
1389c 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
1389d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1389e 6d 52 65 61 6c 69 66 79 28 70 54 6f 73 29 3b 0a  mRealify(pTos);.
1389f 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
138a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
138a1 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f  MIT_CAST./* Opco
138a2 64 65 3a 20 54 6f 54 65 78 74 20 2a 20 2a 20 2a  de: ToText * * *
138a3 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65  .**.** Force the
138a4 20 76 61 6c 75 65 20 6f 6e 20 74 68 65 20 74 6f   value on the to
138a5 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 74  p of the stack t
138a6 6f 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 49 66  o be text..** If
138a7 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75   the value is nu
138a8 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69  meric, convert i
138a9 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73  t to a string us
138aa 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76  ing the.** equiv
138ab 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 66 28  alent of printf(
138ac 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 73 20  ).  Blob values 
138ad 61 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e  are unchanged an
138ae 64 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 77 61  d.** are afterwa
138af 72 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 65 72  rds simply inter
138b0 70 72 65 74 65 64 20 61 73 20 74 65 78 74 2e 0a  preted as text..
138b1 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c  **.** A NULL val
138b2 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  ue is not change
138b3 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  d by this routin
138b4 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e  e.  It remains N
138b5 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
138b6 54 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 20 20  ToText: {       
138b7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
138b8 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54  me as TK_TO_TEXT
138b9 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61  , no-push */.  a
138ba 73 73 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e  ssert( pTos>=p->
138bb 61 53 74 61 63 6b 20 29 3b 0a 20 20 69 66 28 20  aStack );.  if( 
138bc 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pTos->flags & ME
138bd 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a  M_Null ) break;.
138be 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74    assert( MEM_St
138bf 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29  r==(MEM_Blob>>3)
138c0 20 29 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67   );.  pTos->flag
138c1 73 20 7c 3d 20 28 70 54 6f 73 2d 3e 66 6c 61 67  s |= (pTos->flag
138c2 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a  s&MEM_Blob)>>3;.
138c3 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
138c4 70 54 6f 73 2c 20 53 51 4c 49 54 45 5f 41 46 46  pTos, SQLITE_AFF
138c5 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29  _TEXT, encoding)
138c6 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42  ;.  rc = ExpandB
138c7 6c 6f 62 28 70 54 6f 73 29 3b 0a 20 20 61 73 73  lob(pTos);.  ass
138c8 65 72 74 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73  ert( pTos->flags
138c9 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20   & MEM_Str );.  
138ca 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pTos->flags &= ~
138cb 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
138cc 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 62  l|MEM_Blob);.  b
138cd 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
138ce 64 65 3a 20 54 6f 42 6c 6f 62 20 2a 20 2a 20 2a  de: ToBlob * * *
138cf 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65  .**.** Force the
138d0 20 76 61 6c 75 65 20 6f 6e 20 74 68 65 20 74 6f   value on the to
138d1 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 74  p of the stack t
138d2 6f 20 62 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20  o be a BLOB..** 
138d3 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
138d4 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74  numeric, convert
138d5 20 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20   it to a string 
138d6 66 69 72 73 74 2e 0a 2a 2a 20 53 74 72 69 6e 67  first..** String
138d7 73 20 61 72 65 20 73 69 6d 70 6c 79 20 72 65 69  s are simply rei
138d8 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 62 6c  nterpreted as bl
138d9 6f 62 73 20 77 69 74 68 20 6e 6f 20 63 68 61 6e  obs with no chan
138da 67 65 0a 2a 2a 20 74 6f 20 74 68 65 20 75 6e 64  ge.** to the und
138db 65 72 6c 79 69 6e 67 20 64 61 74 61 2e 0a 2a 2a  erlying data..**
138dc 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65  .** A NULL value
138dd 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20   is not changed 
138de 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  by this routine.
138df 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c    It remains NUL
138e0 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f  L..*/.case OP_To
138e1 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 20  Blob: {         
138e2 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
138e3 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20   as TK_TO_BLOB, 
138e4 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73 73  no-push */.  ass
138e5 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53  ert( pTos>=p->aS
138e6 74 61 63 6b 20 29 3b 0a 20 20 69 66 28 20 70 54  tack );.  if( pT
138e7 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  os->flags & MEM_
138e8 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20  Null ) break;.  
138e9 69 66 28 20 28 70 54 6f 73 2d 3e 66 6c 61 67 73  if( (pTos->flags
138ea 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20   & MEM_Blob)==0 
138eb 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69  ){.    applyAffi
138ec 6e 69 74 79 28 70 54 6f 73 2c 20 53 51 4c 49 54  nity(pTos, SQLIT
138ed 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f  E_AFF_TEXT, enco
138ee 64 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72  ding);.    asser
138ef 74 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  t( pTos->flags &
138f0 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 20 20   MEM_Str );.    
138f1 70 54 6f 73 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pTos->flags |= M
138f2 45 4d 5f 42 6c 6f 62 3b 0a 20 20 7d 0a 20 20 70  EM_Blob;.  }.  p
138f3 54 6f 73 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Tos->flags &= ~(
138f4 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
138f5 7c 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 62 72 65  |MEM_Str);.  bre
138f6 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
138f7 3a 20 54 6f 4e 75 6d 65 72 69 63 20 2a 20 2a 20  : ToNumeric * * 
138f8 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
138f9 65 20 76 61 6c 75 65 20 6f 6e 20 74 68 65 20 74  e value on the t
138fa 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
138fb 74 6f 20 62 65 20 6e 75 6d 65 72 69 63 20 28 65  to be numeric (e
138fc 69 74 68 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65  ither an.** inte
138fd 67 65 72 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e  ger or a floatin
138fe 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29  g-point number.)
138ff 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
13900 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62   is text or blob
13901 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  , try to convert
13902 20 69 74 20 74 6f 20 61 6e 20 75 73 69 6e 67 20   it to an using 
13903 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e  the.** equivalen
13904 74 20 6f 66 20 61 74 6f 69 28 29 20 6f 72 20 61  t of atoi() or a
13905 74 6f 66 28 29 20 61 6e 64 20 73 74 6f 72 65 20  tof() and store 
13906 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e  0 if no such con
13907 76 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70  version .** is p
13908 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  ossible..**.** A
13909 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
1390a 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
1390b 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
1390c 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
1390d 0a 63 61 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72  .case OP_ToNumer
1390e 69 63 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ic: {           
1390f 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
13910 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c  s TK_TO_NUMERIC,
13911 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73   no-push */.  as
13912 73 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61  sert( pTos>=p->a
13913 53 74 61 63 6b 20 29 3b 0a 20 20 69 66 28 20 28  Stack );.  if( (
13914 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pTos->flags & (M
13915 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c  EM_Null|MEM_Int|
13916 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 29 7b  MEM_Real))==0 ){
13917 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
13918 4d 65 6d 4e 75 6d 65 72 69 66 79 28 70 54 6f 73  MemNumerify(pTos
13919 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1391a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1391b 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a  TE_OMIT_CAST */.
1391c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 49 6e  ./* Opcode: ToIn
1391d 74 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f  t * * *.**.** Fo
1391e 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 6f 6e  rce the value on
1391f 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
13920 73 74 61 63 6b 20 74 6f 20 62 65 20 61 6e 20 69  stack to be an i
13921 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54  nteger.  If.** T
13922 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 72  he value is curr
13923 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d  ently a real num
13924 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72  ber, drop its fr
13925 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a  actional part..*
13926 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69  * If the value i
13927 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20  s text or blob, 
13928 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69  try to convert i
13929 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  t to an integer 
1392a 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75  using the.** equ
1392b 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28  ivalent of atoi(
1392c 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66  ) and store 0 if
1392d 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73   no such convers
1392e 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e  ion is possible.
1392f 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
13930 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
13931 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
13932 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
13933 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
13934 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20  _ToInt: {       
13935 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
13936 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c  me as TK_TO_INT,
13937 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73   no-push */.  as
13938 73 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61  sert( pTos>=p->a
13939 53 74 61 63 6b 20 29 3b 0a 20 20 69 66 28 20 28  Stack );.  if( (
1393a 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pTos->flags & ME
1393b 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Null)==0 ){.  
1393c 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1393d 49 6e 74 65 67 65 72 69 66 79 28 70 54 6f 73 29  Integerify(pTos)
1393e 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1393f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
13940 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70  _OMIT_CAST./* Op
13941 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20 2a 20 2a  code: ToReal * *
13942 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74   *.**.** Force t
13943 68 65 20 76 61 6c 75 65 20 6f 6e 20 74 68 65 20  he value on the 
13944 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
13945 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e   to be a floatin
13946 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a  g point number..
13947 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75 65 20  ** If The value 
13948 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20  is currently an 
13949 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74  integer, convert
1394a 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76   it..** If the v
1394b 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20  alue is text or 
1394c 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e  blob, try to con
1394d 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e  vert it to an in
1394e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a  teger using the.
1394f 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66  ** equivalent of
13950 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72   atoi() and stor
13951 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63  e 0 if no such c
13952 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73  onversion is pos
13953 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e  sible..**.** A N
13954 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74  ULL value is not
13955 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73   changed by this
13956 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65   routine.  It re
13957 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63  mains NULL..*/.c
13958 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b  ase OP_ToReal: {
13959 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1395a 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1395b 54 4f 5f 52 45 41 4c 2c 20 6e 6f 2d 70 75 73 68  TO_REAL, no-push
1395c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
1395d 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b  os>=p->aStack );
1395e 0a 20 20 69 66 28 20 28 70 54 6f 73 2d 3e 66 6c  .  if( (pTos->fl
1395f 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d  ags & MEM_Null)=
13960 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
13961 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28  3VdbeMemRealify(
13962 70 54 6f 73 29 3b 0a 20 20 7d 0a 20 20 62 72 65  pTos);.  }.  bre
13963 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
13964 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
13965 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20   */../* Opcode: 
13966 45 71 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a  Eq P1 P2 P3.**.*
13967 2a 20 50 6f 70 20 74 68 65 20 74 6f 70 20 74 77  * Pop the top tw
13968 6f 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20  o elements from 
13969 74 68 65 20 73 74 61 63 6b 2e 20 20 49 66 20 74  the stack.  If t
1396a 68 65 79 20 61 72 65 20 65 71 75 61 6c 2c 20 74  hey are equal, t
1396b 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69  hen.** jump to i
1396c 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 20  nstruction P2.  
1396d 4f 74 68 65 72 77 69 73 65 2c 20 63 6f 6e 74 69  Otherwise, conti
1396e 6e 75 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  nue to the next 
1396f 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a  instruction..**.
13970 2a 2a 20 49 66 20 74 68 65 20 30 78 31 30 30 20  ** If the 0x100 
13971 62 69 74 20 6f 66 20 50 31 20 69 73 20 74 72 75  bit of P1 is tru
13972 65 20 61 6e 64 20 65 69 74 68 65 72 20 6f 70 65  e and either ope
13973 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65  rand is NULL the
13974 6e 20 74 61 6b 65 20 74 68 65 0a 2a 2a 20 6a 75  n take the.** ju
13975 6d 70 2e 20 20 49 66 20 74 68 65 20 30 78 31 30  mp.  If the 0x10
13976 30 20 62 69 74 20 6f 66 20 50 31 20 69 73 20 63  0 bit of P1 is c
13977 6c 65 61 72 20 74 68 65 6e 20 66 61 6c 6c 20 74  lear then fall t
13978 68 72 75 20 69 66 20 65 69 74 68 65 72 20 6f 70  hru if either op
13979 65 72 61 6e 64 0a 2a 2a 20 69 73 20 4e 55 4c 4c  erand.** is NULL
1397a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 30  ..**.** If the 0
1397b 78 32 30 30 20 62 69 74 20 6f 66 20 50 31 20 69  x200 bit of P1 i
1397c 73 20 73 65 74 20 61 6e 64 20 65 69 74 68 65 72  s set and either
1397d 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1397e 20 74 68 65 6e 0a 2a 2a 20 62 6f 74 68 20 6f 70   then.** both op
1397f 65 72 61 6e 64 73 20 61 72 65 20 63 6f 6e 76 65  erands are conve
13980 72 74 65 64 20 74 6f 20 69 6e 74 65 67 65 72 73  rted to integers
13981 20 70 72 69 6f 72 20 74 6f 20 63 6f 6d 70 61 72   prior to compar
13982 69 73 6f 6e 2e 0a 2a 2a 20 4e 55 4c 4c 20 6f 70  ison..** NULL op
13983 65 72 61 6e 64 73 20 61 72 65 20 63 6f 6e 76 65  erands are conve
13984 72 74 65 64 20 74 6f 20 7a 65 72 6f 20 61 6e 64  rted to zero and
13985 20 6e 6f 6e 2d 4e 55 4c 4c 20 6f 70 65 72 61 6e   non-NULL operan
13986 64 73 20 61 72 65 0a 2a 2a 20 63 6f 6e 76 65 72  ds are.** conver
13987 74 65 64 20 74 6f 20 31 2e 20 20 54 68 75 73 2c  ted to 1.  Thus,
13988 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 69   for example, wi
13989 74 68 20 30 78 32 30 30 20 73 65 74 2c 20 20 4e  th 0x200 set,  N
1398a 55 4c 4c 3d 3d 4e 55 4c 4c 20 69 73 20 74 72 75  ULL==NULL is tru
1398b 65 0a 2a 2a 20 77 68 65 72 65 61 73 20 69 74 20  e.** whereas it 
1398c 77 6f 75 6c 64 20 6e 6f 72 6d 61 6c 6c 79 20 62  would normally b
1398d 65 20 4e 55 4c 4c 2e 20 20 53 69 6d 69 6c 61 72  e NULL.  Similar
1398e 6c 79 2c 20 20 4e 55 4c 4c 3d 3d 31 32 33 20 69  ly,  NULL==123 i
1398f 73 20 66 61 6c 73 65 20 77 68 65 6e 0a 2a 2a 20  s false when.** 
13990 30 78 32 30 30 20 69 73 20 73 65 74 20 62 75 74  0x200 is set but
13991 20 69 73 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68   is NULL when th
13992 65 20 30 78 32 30 30 20 62 69 74 20 6f 66 20 50  e 0x200 bit of P
13993 31 20 69 73 20 63 6c 65 61 72 2e 0a 2a 2a 0a 2a  1 is clear..**.*
13994 2a 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e  * The least sign
13995 69 66 69 63 61 6e 74 20 62 79 74 65 20 6f 66 20  ificant byte of 
13996 50 31 20 28 6d 61 73 6b 20 30 78 66 66 29 20 6d  P1 (mask 0xff) m
13997 75 73 74 20 62 65 20 61 6e 20 61 66 66 69 6e 69  ust be an affini
13998 74 79 20 63 68 61 72 61 63 74 65 72 20 2d 0a 2a  ty character -.*
13999 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  * SQLITE_AFF_TEX
1399a 54 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  T, SQLITE_AFF_IN
1399b 54 45 47 45 52 2c 20 61 6e 64 20 73 6f 20 66 6f  TEGER, and so fo
1399c 72 74 68 2e 20 41 6e 20 61 74 74 65 6d 70 74 20  rth. An attempt 
1399d 69 73 20 6d 61 64 65 20 0a 2a 2a 20 74 6f 20 63  is made .** to c
1399e 6f 65 72 63 65 20 62 6f 74 68 20 76 61 6c 75 65  oerce both value
1399f 73 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74  s.** according t
139a0 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 62  o the affinity b
139a1 65 66 6f 72 65 20 74 68 65 20 63 6f 6d 70 61 72  efore the compar
139a2 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e 20 49 66  ison is made. If
139a3 20 74 68 65 20 62 79 74 65 20 69 73 0a 2a 2a 20   the byte is.** 
139a4 30 78 30 30 2c 20 74 68 65 6e 20 6e 75 6d 65 72  0x00, then numer
139a5 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20 75  ic affinity is u
139a6 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20  sed..**.** Once 
139a7 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  any conversions 
139a8 68 61 76 65 20 74 61 6b 65 6e 20 70 6c 61 63 65  have taken place
139a9 2c 20 61 6e 64 20 6e 65 69 74 68 65 72 20 76 61  , and neither va
139aa 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a 2a  lue is NULL, .**
139ab 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 20   the values are 
139ac 63 6f 6d 70 61 72 65 64 2e 20 49 66 20 62 6f 74  compared. If bot
139ad 68 20 76 61 6c 75 65 73 20 61 72 65 20 62 6c 6f  h values are blo
139ae 62 73 2c 20 6f 72 20 62 6f 74 68 20 61 72 65 20  bs, or both are 
139af 74 65 78 74 2c 0a 2a 2a 20 74 68 65 6e 20 6d 65  text,.** then me
139b0 6d 63 6d 70 28 29 20 69 73 20 75 73 65 64 20 74  mcmp() is used t
139b1 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
139b2 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 63  results of the c
139b3 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66 0a 2a 2a  omparison. If.**
139b4 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65   both values are
139b5 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 6e 20 61   numeric, then a
139b6 20 6e 75 6d 65 72 69 63 20 63 6f 6d 70 61 72 69   numeric compari
139b7 73 6f 6e 20 69 73 20 75 73 65 64 2e 20 49 66 20  son is used. If 
139b8 74 68 65 0a 2a 2a 20 74 77 6f 20 76 61 6c 75 65  the.** two value
139b9 73 20 61 72 65 20 6f 66 20 64 69 66 66 65 72 65  s are of differe
139ba 6e 74 20 74 79 70 65 73 2c 20 74 68 65 6e 20 74  nt types, then t
139bb 68 65 79 20 61 72 65 20 69 6e 65 71 75 61 6c 2e  hey are inequal.
139bc 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20  .**.** If P2 is 
139bd 7a 65 72 6f 2c 20 64 6f 20 6e 6f 74 20 6a 75 6d  zero, do not jum
139be 70 2e 20 20 49 6e 73 74 65 61 64 2c 20 70 75 73  p.  Instead, pus
139bf 68 20 61 6e 20 69 6e 74 65 67 65 72 20 31 20 6f  h an integer 1 o
139c0 6e 74 6f 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b  nto the.** stack
139c1 20 69 66 20 74 68 65 20 6a 75 6d 70 20 77 6f 75   if the jump wou
139c2 6c 64 20 68 61 76 65 20 62 65 65 6e 20 74 61 6b  ld have been tak
139c3 65 6e 2c 20 6f 72 20 61 20 30 20 69 66 20 6e 6f  en, or a 0 if no
139c4 74 2e 20 20 50 75 73 68 20 61 0a 2a 2a 20 4e 55  t.  Push a.** NU
139c5 4c 4c 20 69 66 20 65 69 74 68 65 72 20 6f 70 65  LL if either ope
139c6 72 61 6e 64 20 77 61 73 20 4e 55 4c 4c 2e 0a 2a  rand was NULL..*
139c7 2a 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 6e 6f  *.** If P3 is no
139c8 74 20 4e 55 4c 4c 20 69 74 20 69 73 20 61 20 70  t NULL it is a p
139c9 6f 69 6e 74 65 72 20 74 6f 20 61 20 63 6f 6c 6c  ointer to a coll
139ca 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 28  ating sequence (
139cb 61 20 43 6f 6c 6c 53 65 71 0a 2a 2a 20 73 74 72  a CollSeq.** str
139cc 75 63 74 75 72 65 29 20 74 68 61 74 20 64 65 66  ucture) that def
139cd 69 6e 65 73 20 68 6f 77 20 74 6f 20 63 6f 6d 70  ines how to comp
139ce 61 72 65 20 74 65 78 74 2e 0a 2a 2f 0a 2f 2a 20  are text..*/./* 
139cf 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 50 32  Opcode: Ne P1 P2
139d0 20 50 33 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77   P3.**.** This w
139d1 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74  orks just like t
139d2 68 65 20 45 71 20 6f 70 63 6f 64 65 20 65 78 63  he Eq opcode exc
139d3 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d  ept that the jum
139d4 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a  p is taken if.**
139d5 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 66 72   the operands fr
139d6 6f 6d 20 74 68 65 20 73 74 61 63 6b 20 61 72 65  om the stack are
139d7 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65   not equal.  See
139d8 20 74 68 65 20 45 71 20 6f 70 63 6f 64 65 20 66   the Eq opcode f
139d9 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c  or.** additional
139da 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
139db 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50  ./* Opcode: Lt P
139dc 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 54 68  1 P2 P3.**.** Th
139dd 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69  is works just li
139de 6b 65 20 74 68 65 20 45 71 20 6f 70 63 6f 64 65  ke the Eq opcode
139df 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
139e0 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69   jump is taken i
139e1 66 0a 2a 2a 20 74 68 65 20 32 6e 64 20 65 6c 65  f.** the 2nd ele
139e2 6d 65 6e 74 20 64 6f 77 6e 20 6f 6e 20 74 68 65  ment down on the
139e3 20 73 74 61 63 6b 20 69 73 20 6c 65 73 73 20 74   stack is less t
139e4 68 61 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74  han the top of t
139e5 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 53 65 65  he stack..** See
139e6 20 74 68 65 20 45 71 20 6f 70 63 6f 64 65 20 66   the Eq opcode f
139e7 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
139e8 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a  formation..*/./*
139e9 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20 50   Opcode: Le P1 P
139ea 32 20 50 33 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  2 P3.**.** This 
139eb 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20  works just like 
139ec 74 68 65 20 45 71 20 6f 70 63 6f 64 65 20 65 78  the Eq opcode ex
139ed 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75  cept that the ju
139ee 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a  mp is taken if.*
139ef 2a 20 74 68 65 20 32 6e 64 20 65 6c 65 6d 65 6e  * the 2nd elemen
139f0 74 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20 73 74  t down on the st
139f1 61 63 6b 20 69 73 20 6c 65 73 73 20 74 68 61 6e  ack is less than
139f2 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
139f3 0a 2a 2a 20 74 6f 70 20 6f 66 20 74 68 65 20 73  .** top of the s
139f4 74 61 63 6b 2e 20 20 53 65 65 20 74 68 65 20 45  tack.  See the E
139f5 71 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64  q opcode for add
139f6 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
139f7 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ion..*/./* Opcod
139f8 65 3a 20 47 74 20 50 31 20 50 32 20 50 33 0a 2a  e: Gt P1 P2 P3.*
139f9 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
139fa 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 45 71  just like the Eq
139fb 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
139fc 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
139fd 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20  taken if.** the 
139fe 32 6e 64 20 65 6c 65 6d 65 6e 74 20 64 6f 77 6e  2nd element down
139ff 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 69 73   on the stack is
13a00 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
13a01 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
13a02 63 6b 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 45  ck..** See the E
13a03 71 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64  q opcode for add
13a04 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
13a05 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ion..*/./* Opcod
13a06 65 3a 20 47 65 20 50 31 20 50 32 20 50 33 0a 2a  e: Ge P1 P2 P3.*
13a07 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
13a08 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 45 71  just like the Eq
13a09 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
13a0a 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
13a0b 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20  taken if.** the 
13a0c 32 6e 64 20 65 6c 65 6d 65 6e 74 20 64 6f 77 6e  2nd element down
13a0d 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 69 73   on the stack is
13a0e 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
13a0f 20 65 71 75 61 6c 20 74 6f 20 74 68 65 0a 2a 2a   equal to the.**
13a10 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63   top of the stac
13a11 6b 2e 20 20 53 65 65 20 74 68 65 20 45 71 20 6f  k.  See the Eq o
13a12 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
13a13 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
13a14 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71 3a  ..*/.case OP_Eq:
13a15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13a16 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 2c  * same as TK_EQ,
13a17 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65   no-push */.case
13a18 20 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20   OP_Ne:         
13a19 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
13a1a 20 54 4b 5f 4e 45 2c 20 6e 6f 2d 70 75 73 68 20   TK_NE, no-push 
13a1b 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20  */.case OP_Lt:  
13a1c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13a1d 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6e  same as TK_LT, n
13a1e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f  o-push */.case O
13a1f 50 5f 4c 65 3a 20 20 20 20 20 20 20 20 20 20 20  P_Le:           
13a20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
13a21 4b 5f 4c 45 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f  K_LE, no-push */
13a22 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20  .case OP_Gt:    
13a23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
13a24 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20 6e 6f 2d  me as TK_GT, no-
13a25 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  push */.case OP_
13a26 47 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  Ge: {           
13a27 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
13a28 47 45 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20  GE, no-push */. 
13a29 20 4d 65 6d 20 2a 70 4e 6f 73 3b 0a 20 20 69 6e   Mem *pNos;.  in
13a2a 74 20 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72  t flags;.  int r
13a2b 65 73 3b 0a 20 20 63 68 61 72 20 61 66 66 69 6e  es;.  char affin
13a2c 69 74 79 3b 0a 0a 20 20 70 4e 6f 73 20 3d 20 26  ity;..  pNos = &
13a2d 70 54 6f 73 5b 2d 31 5d 3b 0a 20 20 66 6c 61 67  pTos[-1];.  flag
13a2e 73 20 3d 20 70 54 6f 73 2d 3e 66 6c 61 67 73 7c  s = pTos->flags|
13a2f 70 4e 6f 73 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20  pNos->flags;..  
13a30 2f 2a 20 49 66 20 65 69 74 68 65 72 20 76 61 6c  /* If either val
13a31 75 65 20 69 73 20 61 20 4e 55 4c 4c 20 50 32 20  ue is a NULL P2 
13a32 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 61 6b  is not zero, tak
13a33 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 74 68  e the jump if th
13a34 65 20 6c 65 61 73 74 0a 20 20 2a 2a 20 73 69 67  e least.  ** sig
13a35 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 6f 66  nificant byte of
13a36 20 50 31 20 69 73 20 74 72 75 65 2e 20 49 66 20   P1 is true. If 
13a37 50 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e  P2 is zero, then
13a38 20 70 75 73 68 20 61 20 4e 55 4c 4c 20 6f 6e 74   push a NULL ont
13a39 6f 0a 20 20 2a 2a 20 74 68 65 20 73 74 61 63 6b  o.  ** the stack
13a3a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 6c 61  ..  */.  if( fla
13a3b 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20  gs&MEM_Null ){. 
13a3c 20 20 20 69 66 28 20 28 70 4f 70 2d 3e 70 31 20     if( (pOp->p1 
13a3d 26 20 30 78 32 30 30 29 21 3d 30 20 29 7b 0a 20  & 0x200)!=0 ){. 
13a3e 20 20 20 20 20 2f 2a 20 54 68 65 20 30 78 32 30       /* The 0x20
13a3f 30 20 62 69 74 20 6f 66 20 50 31 20 6d 65 61 6e  0 bit of P1 mean
13a40 73 2c 20 72 6f 75 67 68 6c 79 20 22 64 6f 20 6e  s, roughly "do n
13a41 6f 74 20 74 72 65 61 74 20 4e 55 4c 4c 20 61 73  ot treat NULL as
13a42 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6d 61   the.      ** ma
13a43 67 69 63 20 53 51 4c 20 76 61 6c 75 65 20 69 74  gic SQL value it
13a44 20 6e 6f 72 6d 61 6c 6c 79 20 69 73 20 2d 20 74   normally is - t
13a45 72 65 61 74 20 69 74 20 61 73 20 69 66 20 69 74  reat it as if it
13a46 20 77 65 72 65 20 61 6e 6f 74 68 65 72 0a 20 20   were another.  
13a47 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 22 2e      ** integer".
13a48 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
13a49 2a 2a 20 57 69 74 68 20 30 78 32 30 30 20 73 65  ** With 0x200 se
13a4a 74 2c 20 69 66 20 65 69 74 68 65 72 20 6f 70 65  t, if either ope
13a4b 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65  rand is NULL the
13a4c 6e 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 0a  n both operands.
13a4d 20 20 20 20 20 20 2a 2a 20 61 72 65 20 63 6f 6e        ** are con
13a4e 76 65 72 74 65 64 20 74 6f 20 69 6e 74 65 67 65  verted to intege
13a4f 72 73 20 70 72 69 6f 72 20 74 6f 20 62 65 69 6e  rs prior to bein
13a50 67 20 70 61 73 73 65 64 20 64 6f 77 6e 20 69 6e  g passed down in
13a51 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  to the.      ** 
13a52 6e 6f 72 6d 61 6c 20 63 6f 6d 70 61 72 69 73 6f  normal compariso
13a53 6e 20 6c 6f 67 69 63 20 62 65 6c 6f 77 2e 20 20  n logic below.  
13a54 4e 55 4c 4c 20 6f 70 65 72 61 6e 64 73 20 61 72  NULL operands ar
13a55 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 0a 20  e converted to. 
13a56 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64       ** zero and
13a57 20 6e 6f 6e 2d 4e 55 4c 4c 20 6f 70 65 72 61 6e   non-NULL operan
13a58 64 73 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64  ds are converted
13a59 20 74 6f 20 31 2e 20 20 54 68 75 73 2c 20 66 6f   to 1.  Thus, fo
13a5a 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20 20 20 20  r example,.     
13a5b 20 2a 2a 20 77 69 74 68 20 30 78 32 30 30 20 73   ** with 0x200 s
13a5c 65 74 2c 20 20 4e 55 4c 4c 3d 3d 4e 55 4c 4c 20  et,  NULL==NULL 
13a5d 69 73 20 74 72 75 65 20 77 68 65 72 65 61 73 20  is true whereas 
13a5e 69 74 20 77 6f 75 6c 64 20 6e 6f 72 6d 61 6c 6c  it would normall
13a5f 79 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 4e 55  y.      ** be NU
13a60 4c 4c 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20  LL.  Similarly, 
13a61 20 4e 55 4c 4c 21 3d 31 32 33 20 69 73 20 74 72   NULL!=123 is tr
13a62 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ue..      */.   
13a63 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
13a64 6d 53 65 74 49 6e 74 36 34 28 70 54 6f 73 2c 20  mSetInt64(pTos, 
13a65 28 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d  (pTos->flags & M
13a66 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 29 3b 0a 20 20  EM_Null)==0);.  
13a67 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
13a68 65 6d 53 65 74 49 6e 74 36 34 28 70 4e 6f 73 2c  emSetInt64(pNos,
13a69 20 28 70 4e 6f 73 2d 3e 66 6c 61 67 73 20 26 20   (pNos->flags & 
13a6a 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 29 3b 0a 20  MEM_Null)==0);. 
13a6b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
13a6c 2f 2a 20 49 66 20 74 68 65 20 30 78 32 30 30 20  /* If the 0x200 
13a6d 62 69 74 20 6f 66 20 50 31 20 69 73 20 63 6c 65  bit of P1 is cle
13a6e 61 72 20 61 6e 64 20 65 69 74 68 65 72 20 6f 70  ar and either op
13a6f 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68  erand is NULL th
13a70 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  en.      ** the 
13a71 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73  result is always
13a72 20 4e 55 4c 4c 2e 20 20 54 68 65 20 6a 75 6d 70   NULL.  The jump
13a73 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65   is taken if the
13a74 20 30 78 31 30 30 20 62 69 74 0a 20 20 20 20 20   0x100 bit.     
13a75 20 2a 2a 20 6f 66 20 50 31 20 69 73 20 73 65 74   ** of P1 is set
13a76 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
13a77 20 70 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c   popStack(&pTos,
13a78 20 32 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   2);.      if( p
13a79 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 20 20  Op->p2 ){.      
13a7a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 26 20    if( pOp->p1 & 
13a7b 30 78 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20  0x100 ){.       
13a7c 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d     pc = pOp->p2-
13a7d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
13a7e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
13a7f 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 20 20 20 20    pTos++;.      
13a80 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
13a81 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
13a82 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
13a83 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 66 66 69     }.  }..  affi
13a84 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 31 20 26  nity = pOp->p1 &
13a85 20 30 78 46 46 3b 0a 20 20 69 66 28 20 61 66 66   0xFF;.  if( aff
13a86 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 61 70 70  inity ){.    app
13a87 6c 79 41 66 66 69 6e 69 74 79 28 70 4e 6f 73 2c  lyAffinity(pNos,
13a88 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64   affinity, encod
13a89 69 6e 67 29 3b 0a 20 20 20 20 61 70 70 6c 79 41  ing);.    applyA
13a8a 66 66 69 6e 69 74 79 28 70 54 6f 73 2c 20 61 66  ffinity(pTos, af
13a8b 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67  finity, encoding
13a8c 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
13a8d 28 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50  ( pOp->p3type==P
13a8e 33 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70  3_COLLSEQ || pOp
13a8f 2d 3e 70 33 3d 3d 30 20 29 3b 0a 20 20 45 78 70  ->p3==0 );.  Exp
13a90 61 6e 64 42 6c 6f 62 28 70 4e 6f 73 29 3b 0a 20  andBlob(pNos);. 
13a91 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 54 6f 73   ExpandBlob(pTos
13a92 29 3b 0a 20 20 72 65 73 20 3d 20 73 71 6c 69 74  );.  res = sqlit
13a93 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 4e 6f  e3MemCompare(pNo
13a94 73 2c 20 70 54 6f 73 2c 20 28 43 6f 6c 6c 53 65  s, pTos, (CollSe
13a95 71 2a 29 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 73  q*)pOp->p3);.  s
13a96 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f  witch( pOp->opco
13a97 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f  de ){.    case O
13a98 50 5f 45 71 3a 20 20 20 20 72 65 73 20 3d 20 72  P_Eq:    res = r
13a99 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 6b  es==0;     break
13a9a 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e 65  ;.    case OP_Ne
13a9b 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 21 3d  :    res = res!=
13a9c 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  0;     break;.  
13a9d 20 20 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20    case OP_Lt:   
13a9e 20 72 65 73 20 3d 20 72 65 73 3c 30 3b 20 20 20   res = res<0;   
13a9f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
13aa0 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 72 65 73  se OP_Le:    res
13aa1 20 3d 20 72 65 73 3c 3d 30 3b 20 20 20 20 20 62   = res<=0;     b
13aa2 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f  reak;.    case O
13aa3 50 5f 47 74 3a 20 20 20 20 72 65 73 20 3d 20 72  P_Gt:    res = r
13aa4 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65 61 6b  es>0;      break
13aa5 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ;.    default:  
13aa6 20 20 20 20 20 72 65 73 20 3d 20 72 65 73 3e 3d       res = res>=
13aa7 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  0;     break;.  
13aa8 7d 0a 0a 20 20 70 6f 70 53 74 61 63 6b 28 26 70  }..  popStack(&p
13aa9 54 6f 73 2c 20 32 29 3b 0a 20 20 69 66 28 20 70  Tos, 2);.  if( p
13aaa 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 69 66  Op->p2 ){.    if
13aab 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70  ( res ){.      p
13aac 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20  c = pOp->p2-1;. 
13aad 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
13aae 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 20 20 70 54    pTos++;.    pT
13aaf 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  os->flags = MEM_
13ab0 49 6e 74 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 75  Int;.    pTos->u
13ab1 2e 69 20 3d 20 72 65 73 3b 0a 20 20 7d 0a 20 20  .i = res;.  }.  
13ab2 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
13ab3 6f 64 65 3a 20 41 6e 64 20 2a 20 2a 20 2a 0a 2a  ode: And * * *.*
13ab4 2a 0a 2a 2a 20 50 6f 70 20 74 77 6f 20 76 61 6c  *.** Pop two val
13ab5 75 65 73 20 6f 66 66 20 74 68 65 20 73 74 61 63  ues off the stac
13ab6 6b 2e 20 20 54 61 6b 65 20 74 68 65 20 6c 6f 67  k.  Take the log
13ab7 69 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 0a  ical AND of the.
13ab8 2a 2a 20 74 77 6f 20 76 61 6c 75 65 73 20 61 6e  ** two values an
13ab9 64 20 70 75 73 68 20 74 68 65 20 72 65 73 75 6c  d push the resul
13aba 74 69 6e 67 20 62 6f 6f 6c 65 61 6e 20 76 61 6c  ting boolean val
13abb 75 65 20 62 61 63 6b 20 6f 6e 74 6f 20 74 68 65  ue back onto the
13abc 0a 2a 2a 20 73 74 61 63 6b 2e 20 0a 2a 2f 0a 2f  .** stack. .*/./
13abd 2a 20 4f 70 63 6f 64 65 3a 20 4f 72 20 2a 20 2a  * Opcode: Or * *
13abe 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 77 6f   *.**.** Pop two
13abf 20 76 61 6c 75 65 73 20 6f 66 66 20 74 68 65 20   values off the 
13ac0 73 74 61 63 6b 2e 20 20 54 61 6b 65 20 74 68 65  stack.  Take the
13ac1 20 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74   logical OR of t
13ac2 68 65 0a 2a 2a 20 74 77 6f 20 76 61 6c 75 65 73  he.** two values
13ac3 20 61 6e 64 20 70 75 73 68 20 74 68 65 20 72 65   and push the re
13ac4 73 75 6c 74 69 6e 67 20 62 6f 6f 6c 65 61 6e 20  sulting boolean 
13ac5 76 61 6c 75 65 20 62 61 63 6b 20 6f 6e 74 6f 20  value back onto 
13ac6 74 68 65 0a 2a 2a 20 73 74 61 63 6b 2e 20 0a 2a  the.** stack. .*
13ac7 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a 20 20  /.case OP_And:  
13ac8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
13ac9 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c 20 6e  ame as TK_AND, n
13aca 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f  o-push */.case O
13acb 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Or: {         
13acc 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
13acd 4b 5f 4f 52 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f  K_OR, no-push */
13ace 0a 20 20 4d 65 6d 20 2a 70 4e 6f 73 20 3d 20 26  .  Mem *pNos = &
13acf 70 54 6f 73 5b 2d 31 5d 3b 0a 20 20 69 6e 74 20  pTos[-1];.  int 
13ad0 76 31 2c 20 76 32 3b 20 20 20 20 2f 2a 20 30 3d  v1, v2;    /* 0=
13ad1 3d 54 52 55 45 2c 20 31 3d 3d 46 41 4c 53 45 2c  =TRUE, 1==FALSE,
13ad2 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e   2==UNKNOWN or N
13ad3 55 4c 4c 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ULL */..  assert
13ad4 28 20 70 4e 6f 73 3e 3d 70 2d 3e 61 53 74 61 63  ( pNos>=p->aStac
13ad5 6b 20 29 3b 0a 20 20 69 66 28 20 70 54 6f 73 2d  k );.  if( pTos-
13ad6 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
13ad7 6c 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 32 3b  l ){.    v1 = 2;
13ad8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
13ad9 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
13ada 67 65 72 69 66 79 28 70 54 6f 73 29 3b 0a 20 20  gerify(pTos);.  
13adb 20 20 76 31 20 3d 20 70 54 6f 73 2d 3e 75 2e 69    v1 = pTos->u.i
13adc 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ==0;.  }.  if( p
13add 4e 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Nos->flags & MEM
13ade 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 76 32 20  _Null ){.    v2 
13adf 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 2;.  }else{.  
13ae0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
13ae1 49 6e 74 65 67 65 72 69 66 79 28 70 4e 6f 73 29  Integerify(pNos)
13ae2 3b 0a 20 20 20 20 76 32 20 3d 20 70 4e 6f 73 2d  ;.    v2 = pNos-
13ae3 3e 75 2e 69 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69  >u.i==0;.  }.  i
13ae4 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
13ae5 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20 73 74  OP_And ){.    st
13ae6 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
13ae7 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c 6f 67  ned char and_log
13ae8 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32  ic[] = { 0, 1, 2
13ae9 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 31 2c  , 1, 1, 1, 2, 1,
13aea 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 3d 20 61   2 };.    v1 = a
13aeb 6e 64 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b 76 32  nd_logic[v1*3+v2
13aec 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
13aed 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
13aee 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f 6c 6f  igned char or_lo
13aef 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c 20  gic[] = { 0, 0, 
13af0 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 32  0, 0, 1, 2, 0, 2
13af1 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 3d 20  , 2 };.    v1 = 
13af2 6f 72 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b 76 32  or_logic[v1*3+v2
13af3 5d 3b 0a 20 20 7d 0a 20 20 70 6f 70 53 74 61 63  ];.  }.  popStac
13af4 6b 28 26 70 54 6f 73 2c 20 32 29 3b 0a 20 20 70  k(&pTos, 2);.  p
13af5 54 6f 73 2b 2b 3b 0a 20 20 69 66 28 20 76 31 3d  Tos++;.  if( v1=
13af6 3d 32 20 29 7b 0a 20 20 20 20 70 54 6f 73 2d 3e  =2 ){.    pTos->
13af7 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
13af8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
13af9 54 6f 73 2d 3e 75 2e 69 20 3d 20 76 31 3d 3d 30  Tos->u.i = v1==0
13afa 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ;.    pTos->flag
13afb 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 7d  s = MEM_Int;.  }
13afc 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
13afd 4f 70 63 6f 64 65 3a 20 4e 65 67 61 74 69 76 65  Opcode: Negative
13afe 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 65   * * *.**.** Tre
13aff 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  at the top of th
13b00 65 20 73 74 61 63 6b 20 61 73 20 61 20 6e 75 6d  e stack as a num
13b01 65 72 69 63 20 71 75 61 6e 74 69 74 79 2e 20 20  eric quantity.  
13b02 52 65 70 6c 61 63 65 20 69 74 0a 2a 2a 20 77 69  Replace it.** wi
13b03 74 68 20 69 74 73 20 61 64 64 69 74 69 76 65 20  th its additive 
13b04 69 6e 76 65 72 73 65 2e 20 20 49 66 20 74 68 65  inverse.  If the
13b05 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63   top of the stac
13b06 6b 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 74 73  k is NULL.** its
13b07 20 76 61 6c 75 65 20 69 73 20 75 6e 63 68 61 6e   value is unchan
13b08 67 65 64 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ged..*/./* Opcod
13b09 65 3a 20 41 62 73 56 61 6c 75 65 20 2a 20 2a 20  e: AbsValue * * 
13b0a 2a 0a 2a 2a 0a 2a 2a 20 54 72 65 61 74 20 74 68  *.**.** Treat th
13b0b 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
13b0c 63 6b 20 61 73 20 61 20 6e 75 6d 65 72 69 63 20  ck as a numeric 
13b0d 71 75 61 6e 74 69 74 79 2e 20 20 52 65 70 6c 61  quantity.  Repla
13b0e 63 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 69 74  ce it.** with it
13b0f 73 20 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65  s absolute value
13b10 2e 20 49 66 20 74 68 65 20 74 6f 70 20 6f 66 20  . If the top of 
13b11 74 68 65 20 73 74 61 63 6b 20 69 73 20 4e 55 4c  the stack is NUL
13b12 4c 0a 2a 2a 20 69 74 73 20 76 61 6c 75 65 20 69  L.** its value i
13b13 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  s unchanged..*/.
13b14 63 61 73 65 20 4f 50 5f 4e 65 67 61 74 69 76 65  case OP_Negative
13b15 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  :              /
13b16 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 55 4d 49  * same as TK_UMI
13b17 4e 55 53 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a  NUS, no-push */.
13b18 63 61 73 65 20 4f 50 5f 41 62 73 56 61 6c 75 65  case OP_AbsValue
13b19 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 54  : {.  assert( pT
13b1a 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b  os>=p->aStack );
13b1b 0a 20 20 69 66 28 20 28 70 54 6f 73 2d 3e 66 6c  .  if( (pTos->fl
13b1c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c 7c  ags & (MEM_Real|
13b1d 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 4e 75 6c 6c  MEM_Int|MEM_Null
13b1e 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ))==0 ){.    sql
13b1f 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72  ite3VdbeMemNumer
13b20 69 66 79 28 70 54 6f 73 29 3b 0a 20 20 7d 0a 20  ify(pTos);.  }. 
13b21 20 69 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73   if( pTos->flags
13b22 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
13b23 20 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29     Release(pTos)
13b24 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f  ;.    if( pOp->o
13b25 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 67 61 74 69  pcode==OP_Negati
13b26 76 65 20 7c 7c 20 70 54 6f 73 2d 3e 72 3c 30 2e  ve || pTos->r<0.
13b27 30 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 73 2d  0 ){.      pTos-
13b28 3e 72 20 3d 20 2d 70 54 6f 73 2d 3e 72 3b 0a 20  >r = -pTos->r;. 
13b29 20 20 20 7d 0a 20 20 20 20 70 54 6f 73 2d 3e 66     }.    pTos->f
13b2a 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b  lags = MEM_Real;
13b2b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6f  .  }else if( pTo
13b2c 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  s->flags & MEM_I
13b2d 6e 74 20 29 7b 0a 20 20 20 20 52 65 6c 65 61 73  nt ){.    Releas
13b2e 65 28 70 54 6f 73 29 3b 0a 20 20 20 20 69 66 28  e(pTos);.    if(
13b2f 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
13b30 5f 4e 65 67 61 74 69 76 65 20 7c 7c 20 70 54 6f  _Negative || pTo
13b31 73 2d 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20  s->u.i<0 ){.    
13b32 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 2d 70    pTos->u.i = -p
13b33 54 6f 73 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d 0a  Tos->u.i;.    }.
13b34 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20      pTos->flags 
13b35 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 7d 0a 20  = MEM_Int;.  }. 
13b36 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
13b37 63 6f 64 65 3a 20 4e 6f 74 20 2a 20 2a 20 2a 0a  code: Not * * *.
13b38 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  **.** Interpret 
13b39 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
13b3a 74 61 63 6b 20 61 73 20 61 20 62 6f 6f 6c 65 61  tack as a boolea
13b3b 6e 20 76 61 6c 75 65 2e 20 20 52 65 70 6c 61 63  n value.  Replac
13b3c 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 69 74 73  e it.** with its
13b3d 20 63 6f 6d 70 6c 65 6d 65 6e 74 2e 20 20 49 66   complement.  If
13b3e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
13b3f 73 74 61 63 6b 20 69 73 20 4e 55 4c 4c 20 69 74  stack is NULL it
13b40 73 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 75 6e  s value.** is un
13b41 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 63 61 73 65  changed..*/.case
13b42 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20 20 20 20   OP_Not: {      
13b43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
13b44 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20 6e 6f 2d  e as TK_NOT, no-
13b45 70 75 73 68 20 2a 2f 0a 20 20 61 73 73 65 72 74  push */.  assert
13b46 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63  ( pTos>=p->aStac
13b47 6b 20 29 3b 0a 20 20 69 66 28 20 70 54 6f 73 2d  k );.  if( pTos-
13b48 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
13b49 6c 20 29 20 62 72 65 61 6b 3b 20 20 2f 2a 20 44  l ) break;  /* D
13b4a 6f 20 6e 6f 74 68 69 6e 67 20 74 6f 20 4e 55 4c  o nothing to NUL
13b4b 4c 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  Ls */.  sqlite3V
13b4c 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
13b4d 28 70 54 6f 73 29 3b 0a 20 20 61 73 73 65 72 74  (pTos);.  assert
13b4e 28 20 28 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  ( (pTos->flags &
13b4f 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a   MEM_Dyn)==0 );.
13b50 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 21 70    pTos->u.i = !p
13b51 54 6f 73 2d 3e 75 2e 69 3b 0a 20 20 70 54 6f 73  Tos->u.i;.  pTos
13b52 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
13b53 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  t;.  break;.}../
13b54 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4e 6f 74  * Opcode: BitNot
13b55 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74   * * *.**.** Int
13b56 65 72 70 72 65 74 20 74 68 65 20 74 6f 70 20 6f  erpret the top o
13b57 66 20 74 68 65 20 73 74 61 63 6b 20 61 73 20 61  f the stack as a
13b58 6e 20 76 61 6c 75 65 2e 20 20 52 65 70 6c 61 63  n value.  Replac
13b59 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 69 74 73  e it.** with its
13b5a 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74   ones-complement
13b5b 2e 20 20 49 66 20 74 68 65 20 74 6f 70 20 6f 66  .  If the top of
13b5c 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 4e 55   the stack is NU
13b5d 4c 4c 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 20  LL its.** value 
13b5e 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f  is unchanged..*/
13b5f 0a 63 61 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a  .case OP_BitNot:
13b60 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
13b61 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
13b62 4e 4f 54 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a  NOT, no-push */.
13b63 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 3e 3d    assert( pTos>=
13b64 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 69  p->aStack );.  i
13b65 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  f( pTos->flags &
13b66 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61   MEM_Null ) brea
13b67 6b 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e  k;  /* Do nothin
13b68 67 20 74 6f 20 4e 55 4c 4c 73 20 2a 2f 0a 20 20  g to NULLs */.  
13b69 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
13b6a 74 65 67 65 72 69 66 79 28 70 54 6f 73 29 3b 0a  tegerify(pTos);.
13b6b 20 20 61 73 73 65 72 74 28 20 28 70 54 6f 73 2d    assert( (pTos-
13b6c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  >flags & MEM_Dyn
13b6d 29 3d 3d 30 20 29 3b 0a 20 20 70 54 6f 73 2d 3e  )==0 );.  pTos->
13b6e 75 2e 69 20 3d 20 7e 70 54 6f 73 2d 3e 75 2e 69  u.i = ~pTos->u.i
13b6f 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20  ;.  pTos->flags 
13b70 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 62 72 65  = MEM_Int;.  bre
13b71 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
13b72 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a 0a 2a 2a 0a  : Noop * * *.**.
13b73 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20  ** Do nothing.  
13b74 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
13b75 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c   is often useful
13b76 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65   as a jump.** de
13b77 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a  stination..*/./*
13b78 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78  .** The magic Ex
13b79 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65  plain opcode are
13b7a 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77   only inserted w
13b7b 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28  hen explain==2 (
13b7c 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73  which.** is to s
13b7d 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c  ay when the EXPL
13b7e 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73  AIN QUERY PLAN s
13b7f 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a  yntax is used.).
13b80 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72  ** This opcode r
13b81 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69  ecords informati
13b82 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69  on from the opti
13b83 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68  mizer.  It is th
13b84 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73  e.** the same as
13b85 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20   a no-op.  This 
13b86 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70  opcodesnever app
13b87 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56  ears in a real V
13b88 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61  M program..*/.ca
13b89 73 65 20 4f 50 5f 45 78 70 6c 61 69 6e 3a 0a 63  se OP_Explain:.c
13b8a 61 73 65 20 4f 50 5f 4e 6f 6f 70 3a 20 7b 20 20  ase OP_Noop: {  
13b8b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d            /* no-
13b8c 70 75 73 68 20 2a 2f 0a 20 20 62 72 65 61 6b 3b  push */.  break;
13b8d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
13b8e 66 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  f P1 P2 *.**.** 
13b8f 50 6f 70 20 61 20 73 69 6e 67 6c 65 20 62 6f 6f  Pop a single boo
13b90 6c 65 61 6e 20 66 72 6f 6d 20 74 68 65 20 73 74  lean from the st
13b91 61 63 6b 2e 20 20 49 66 20 74 68 65 20 62 6f 6f  ack.  If the boo
13b92 6c 65 61 6e 20 70 6f 70 70 65 64 20 69 73 0a 2a  lean popped is.*
13b93 2a 20 74 72 75 65 2c 20 74 68 65 6e 20 6a 75 6d  * true, then jum
13b94 70 20 74 6f 20 70 32 2e 20 20 4f 74 68 65 72 77  p to p2.  Otherw
13b95 69 73 65 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20  ise continue to 
13b96 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
13b97 74 69 6f 6e 2e 0a 2a 2a 20 41 6e 20 69 6e 74 65  tion..** An inte
13b98 67 65 72 20 69 73 20 66 61 6c 73 65 20 69 66 20  ger is false if 
13b99 7a 65 72 6f 20 61 6e 64 20 74 72 75 65 20 6f 74  zero and true ot
13b9a 68 65 72 77 69 73 65 2e 20 20 41 20 73 74 72 69  herwise.  A stri
13b9b 6e 67 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20 69  ng is.** false i
13b9c 66 20 69 74 20 68 61 73 20 7a 65 72 6f 20 6c 65  f it has zero le
13b9d 6e 67 74 68 20 61 6e 64 20 74 72 75 65 20 6f 74  ngth and true ot
13b9e 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  herwise..**.** I
13b9f 66 20 74 68 65 20 76 61 6c 75 65 20 70 6f 70 70  f the value popp
13ba0 65 64 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  ed of the stack 
13ba1 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 61  is NULL, then ta
13ba2 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50  ke the jump if P
13ba3 31 0a 2a 2a 20 69 73 20 74 72 75 65 20 61 6e 64  1.** is true and
13ba4 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 66   fall through if
13ba5 20 50 31 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2f   P1 is false..*/
13ba6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e 6f  ./* Opcode: IfNo
13ba7 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  t P1 P2 *.**.** 
13ba8 50 6f 70 20 61 20 73 69 6e 67 6c 65 20 62 6f 6f  Pop a single boo
13ba9 6c 65 61 6e 20 66 72 6f 6d 20 74 68 65 20 73 74  lean from the st
13baa 61 63 6b 2e 20 20 49 66 20 74 68 65 20 62 6f 6f  ack.  If the boo
13bab 6c 65 61 6e 20 70 6f 70 70 65 64 20 69 73 0a 2a  lean popped is.*
13bac 2a 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6a 75  * false, then ju
13bad 6d 70 20 74 6f 20 70 32 2e 20 20 4f 74 68 65 72  mp to p2.  Other
13bae 77 69 73 65 20 63 6f 6e 74 69 6e 75 65 20 74 6f  wise continue to
13baf 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
13bb0 63 74 69 6f 6e 2e 0a 2a 2a 20 41 6e 20 69 6e 74  ction..** An int
13bb1 65 67 65 72 20 69 73 20 66 61 6c 73 65 20 69 66  eger is false if
13bb2 20 7a 65 72 6f 20 61 6e 64 20 74 72 75 65 20 6f   zero and true o
13bb3 74 68 65 72 77 69 73 65 2e 20 20 41 20 73 74 72  therwise.  A str
13bb4 69 6e 67 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20  ing is.** false 
13bb5 69 66 20 69 74 20 68 61 73 20 7a 65 72 6f 20 6c  if it has zero l
13bb6 65 6e 67 74 68 20 61 6e 64 20 74 72 75 65 20 6f  ength and true o
13bb7 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20  therwise..**.** 
13bb8 49 66 20 74 68 65 20 76 61 6c 75 65 20 70 6f 70  If the value pop
13bb9 70 65 64 20 6f 66 20 74 68 65 20 73 74 61 63 6b  ped of the stack
13bba 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   is NULL, then t
13bbb 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20  ake the jump if 
13bbc 50 31 0a 2a 2a 20 69 73 20 74 72 75 65 20 61 6e  P1.** is true an
13bbd 64 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 69  d fall through i
13bbe 66 20 50 31 20 69 73 20 66 61 6c 73 65 2e 0a 2a  f P1 is false..*
13bbf 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a 20 20 20  /.case OP_If:   
13bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13bc1 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65   no-push */.case
13bc2 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20   OP_IfNot: {    
13bc3 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
13bc4 73 68 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 20  sh */.  int c;. 
13bc5 20 61 73 73 65 72 74 28 20 70 54 6f 73 3e 3d 70   assert( pTos>=p
13bc6 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 69 66  ->aStack );.  if
13bc7 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20  ( pTos->flags & 
13bc8 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
13bc9 63 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 7d  c = pOp->p1;.  }
13bca 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51 4c  else{.#ifdef SQL
13bcb 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
13bcc 47 5f 50 4f 49 4e 54 0a 20 20 20 20 63 20 3d 20  G_POINT.    c = 
13bcd 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
13bce 6c 75 65 28 70 54 6f 73 29 3b 0a 23 65 6c 73 65  lue(pTos);.#else
13bcf 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
13bd0 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 54  VdbeRealValue(pT
13bd1 6f 73 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 69 66  os)!=0.0;.#endif
13bd2 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
13bd3 63 6f 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29  code==OP_IfNot )
13bd4 20 63 20 3d 20 21 63 3b 0a 20 20 7d 0a 20 20 52   c = !c;.  }.  R
13bd5 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20 20  elease(pTos);.  
13bd6 70 54 6f 73 2d 2d 3b 0a 20 20 69 66 28 20 63 20  pTos--;.  if( c 
13bd7 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31  ) pc = pOp->p2-1
13bd8 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
13bd9 20 4f 70 63 6f 64 65 3a 20 49 73 4e 75 6c 6c 20   Opcode: IsNull 
13bda 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68  P1 P2 *.**.** Ch
13bdb 65 63 6b 20 74 68 65 20 74 6f 70 20 6f 66 20 74  eck the top of t
13bdc 68 65 20 73 74 61 63 6b 20 61 6e 64 20 6a 75 6d  he stack and jum
13bdd 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 74  p to P2 if the t
13bde 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 0a  op of the stack.
13bdf 2a 2a 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20  ** is NULL.  If 
13be0 50 31 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20  P1 is positive, 
13be1 74 68 65 6e 20 70 6f 70 20 50 31 20 65 6c 65 6d  then pop P1 elem
13be2 65 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74  ents from the st
13be3 61 63 6b 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73  ack.** regardles
13be4 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20  s of whether or 
13be5 6e 6f 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  not the jump is 
13be6 74 61 6b 65 6e 2e 20 20 49 66 20 50 31 20 69 73  taken.  If P1 is
13be7 20 6e 65 67 61 74 69 76 65 2c 0a 2a 2a 20 70 6f   negative,.** po
13be8 70 20 2d 50 31 20 65 6c 65 6d 65 6e 74 73 20 66  p -P1 elements f
13be9 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 20 6f 6e  rom the stack on
13bea 6c 79 20 69 66 20 74 68 65 20 6a 75 6d 70 20 69  ly if the jump i
13beb 73 20 74 61 6b 65 6e 20 61 6e 64 20 6c 65 61 76  s taken and leav
13bec 65 0a 2a 2a 20 74 68 65 20 73 74 61 63 6b 20 75  e.** the stack u
13bed 6e 63 68 61 6e 67 65 64 20 69 66 20 74 68 65 20  nchanged if the 
13bee 6a 75 6d 70 20 69 73 20 6e 6f 74 20 74 61 6b 65  jump is not take
13bef 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73  n..*/.case OP_Is
13bf0 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20  Null: {         
13bf1 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
13bf2 5f 49 53 4e 55 4c 4c 2c 20 6e 6f 2d 70 75 73 68  _ISNULL, no-push
13bf3 20 2a 2f 0a 20 20 69 66 28 20 70 54 6f 73 2d 3e   */.  if( pTos->
13bf4 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
13bf5 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
13bf6 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 69 66 28 20  ->p2-1;.    if( 
13bf7 70 4f 70 2d 3e 70 31 3c 30 20 29 7b 0a 20 20 20  pOp->p1<0 ){.   
13bf8 20 20 20 70 6f 70 53 74 61 63 6b 28 26 70 54 6f     popStack(&pTo
13bf9 73 2c 20 2d 70 4f 70 2d 3e 70 31 29 3b 0a 20 20  s, -pOp->p1);.  
13bfa 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f    }.  }.  if( pO
13bfb 70 2d 3e 70 31 3e 30 20 29 7b 0a 20 20 20 20 70  p->p1>0 ){.    p
13bfc 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c 20 70  opStack(&pTos, p
13bfd 4f 70 2d 3e 70 31 29 3b 0a 20 20 7d 0a 20 20 62  Op->p1);.  }.  b
13bfe 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
13bff 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 20 50  de: NotNull P1 P
13c00 32 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74  2 *.**.** Jump t
13c01 6f 20 50 32 20 69 66 20 74 68 65 20 74 6f 70 20  o P2 if the top 
13c02 61 62 73 28 50 31 29 20 76 61 6c 75 65 73 20 6f  abs(P1) values o
13c03 6e 20 74 68 65 20 73 74 61 63 6b 20 61 72 65 20  n the stack are 
13c04 61 6c 6c 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a  all not NULL.  .
13c05 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66  ** Regardless of
13c06 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
13c07 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65  the jump is take
13c08 6e 2c 20 70 6f 70 20 74 68 65 20 73 74 61 63 6b  n, pop the stack
13c09 0a 2a 2a 20 50 31 20 74 69 6d 65 73 20 69 66 20  .** P1 times if 
13c0a 50 31 20 69 73 20 67 72 65 61 74 65 72 20 74 68  P1 is greater th
13c0b 61 6e 20 7a 65 72 6f 2e 20 20 42 75 74 20 69 66  an zero.  But if
13c0c 20 50 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c   P1 is negative,
13c0d 0a 2a 2a 20 6c 65 61 76 65 20 74 68 65 20 73 74  .** leave the st
13c0e 61 63 6b 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a  ack unchanged..*
13c0f 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c  /.case OP_NotNul
13c10 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  l: {            
13c11 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f  /* same as TK_NO
13c12 54 4e 55 4c 4c 2c 20 6e 6f 2d 70 75 73 68 20 2a  TNULL, no-push *
13c13 2f 0a 20 20 69 6e 74 20 69 2c 20 63 6e 74 3b 0a  /.  int i, cnt;.
13c14 20 20 63 6e 74 20 3d 20 70 4f 70 2d 3e 70 31 3b    cnt = pOp->p1;
13c15 0a 20 20 69 66 28 20 63 6e 74 3c 30 20 29 20 63  .  if( cnt<0 ) c
13c16 6e 74 20 3d 20 2d 63 6e 74 3b 0a 20 20 61 73 73  nt = -cnt;.  ass
13c17 65 72 74 28 20 26 70 54 6f 73 5b 31 2d 63 6e 74  ert( &pTos[1-cnt
13c18 5d 20 3e 3d 20 70 2d 3e 61 53 74 61 63 6b 20 29  ] >= p->aStack )
13c19 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 63  ;.  for(i=0; i<c
13c1a 6e 74 20 26 26 20 28 70 54 6f 73 5b 31 2b 69 2d  nt && (pTos[1+i-
13c1b 63 6e 74 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d  cnt].flags & MEM
13c1c 5f 4e 75 6c 6c 29 3d 3d 30 3b 20 69 2b 2b 29 7b  _Null)==0; i++){
13c1d 7d 0a 20 20 69 66 28 20 69 3e 3d 63 6e 74 20 29  }.  if( i>=cnt )
13c1e 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b   pc = pOp->p2-1;
13c1f 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3e 30  .  if( pOp->p1>0
13c20 20 29 20 70 6f 70 53 74 61 63 6b 28 26 70 54 6f   ) popStack(&pTo
13c21 73 2c 20 63 6e 74 29 3b 0a 20 20 62 72 65 61 6b  s, cnt);.  break
13c22 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
13c23 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 50 31  SetNumColumns P1
13c24 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 66 6f   P2 *.**.** Befo
13c25 72 65 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e  re the OP_Column
13c26 20 6f 70 63 6f 64 65 20 63 61 6e 20 62 65 20 65   opcode can be e
13c27 78 65 63 75 74 65 64 20 6f 6e 20 61 20 63 75 72  xecuted on a cur
13c28 73 6f 72 2c 20 74 68 69 73 0a 2a 2a 20 6f 70 63  sor, this.** opc
13c29 6f 64 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ode must be call
13c2a 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 6e 75  ed to set the nu
13c2b 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69  mber of fields i
13c2c 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  n the table..**.
13c2d 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 73  ** This opcode s
13c2e 65 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ets the number o
13c2f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 63 75  f columns for cu
13c30 72 73 6f 72 20 50 31 20 74 6f 20 50 32 2e 0a 2a  rsor P1 to P2..*
13c31 2a 0a 2a 2a 20 49 66 20 4f 50 5f 4b 65 79 41 73  *.** If OP_KeyAs
13c32 44 61 74 61 20 69 73 20 74 6f 20 62 65 20 61 70  Data is to be ap
13c33 70 6c 69 65 64 20 74 6f 20 63 75 72 73 6f 72 20  plied to cursor 
13c34 50 31 2c 20 69 74 20 6d 75 73 74 20 62 65 20 65  P1, it must be e
13c35 78 65 63 75 74 65 64 0a 2a 2a 20 62 65 66 6f 72  xecuted.** befor
13c36 65 20 74 68 69 73 20 6f 70 2d 63 6f 64 65 2e 0a  e this op-code..
13c37 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 4e 75  */.case OP_SetNu
13c38 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 20 20 20 20 20  mColumns: {     
13c39 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
13c3a 20 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20    Cursor *pC;.  
13c3b 61 73 73 65 72 74 28 20 28 70 4f 70 2d 3e 70 31  assert( (pOp->p1
13c3c 29 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  )<p->nCursor );.
13c3d 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43    assert( p->apC
13c3e 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 29  sr[pOp->p1]!=0 )
13c3f 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73  ;.  pC = p->apCs
13c40 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 43  r[pOp->p1];.  pC
13c41 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e  ->nField = pOp->
13c42 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  p2;.  break;.}..
13c43 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d  /* Opcode: Colum
13c44 6e 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a  n P1 P2 P3.**.**
13c45 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 64   Interpret the d
13c46 61 74 61 20 74 68 61 74 20 63 75 72 73 6f 72 20  ata that cursor 
13c47 50 31 20 70 6f 69 6e 74 73 20 74 6f 20 61 73 20  P1 points to as 
13c48 61 20 73 74 72 75 63 74 75 72 65 20 62 75 69 6c  a structure buil
13c49 74 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 4d  t using.** the M
13c4a 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75  akeRecord instru
13c4b 63 74 69 6f 6e 2e 20 20 28 53 65 65 20 74 68 65  ction.  (See the
13c4c 20 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f   MakeRecord opco
13c4d 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  de for additiona
13c4e 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  l.** information
13c4f 20 61 62 6f 75 74 20 74 68 65 20 66 6f 72 6d 61   about the forma
13c50 74 20 6f 66 20 74 68 65 20 64 61 74 61 2e 29 20  t of the data.) 
13c51 50 75 73 68 20 6f 6e 74 6f 20 74 68 65 20 73 74  Push onto the st
13c52 61 63 6b 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a  ack the value.**
13c53 20 6f 66 20 74 68 65 20 50 32 2d 74 68 20 63 6f   of the P2-th co
13c54 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 65 64 20 69  lumn contained i
13c55 6e 20 74 68 65 20 64 61 74 61 2e 20 49 66 20 74  n the data. If t
13c56 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 74 68  here are less th
13c57 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61  at (P2+1) .** va
13c58 6c 75 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  lues in the reco
13c59 72 64 2c 20 70 75 73 68 20 61 20 4e 55 4c 4c 20  rd, push a NULL 
13c5a 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a  onto the stack..
13c5b 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4b 65 79  **.** If the Key
13c5c 41 73 44 61 74 61 20 6f 70 63 6f 64 65 20 68 61  AsData opcode ha
13c5d 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 65  s previously exe
13c5e 63 75 74 65 64 20 6f 6e 20 74 68 69 73 20 63 75  cuted on this cu
13c5f 72 73 6f 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a  rsor, then the.*
13c60 2a 20 66 69 65 6c 64 20 6d 69 67 68 74 20 62 65  * field might be
13c61 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
13c62 74 68 65 20 6b 65 79 20 72 61 74 68 65 72 20 74  the key rather t
13c63 68 61 6e 20 74 68 65 20 64 61 74 61 2e 0a 2a 2a  han the data..**
13c64 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d  .** If the colum
13c65 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65 77 65 72  n contains fewer
13c66 20 74 68 61 6e 20 50 32 20 66 69 65 6c 64 73 2c   than P2 fields,
13c67 20 74 68 65 6e 20 70 75 73 68 20 61 20 4e 55 4c   then push a NUL
13c68 4c 2e 20 20 4f 72 0a 2a 2a 20 69 66 20 50 33 20  L.  Or.** if P3 
13c69 69 73 20 6f 66 20 74 79 70 65 20 50 33 5f 4d 45  is of type P3_ME
13c6a 4d 2c 20 74 68 65 6e 20 70 75 73 68 20 74 68 65  M, then push the
13c6b 20 50 33 20 76 61 6c 75 65 2e 20 20 54 68 65 20   P3 value.  The 
13c6c 50 33 20 76 61 6c 75 65 20 77 69 6c 6c 0a 2a 2a  P3 value will.**
13c6d 20 62 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75   be default valu
13c6e 65 20 66 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 74  e for a column t
13c6f 68 61 74 20 68 61 73 20 62 65 65 6e 20 61 64 64  hat has been add
13c70 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 4c 54  ed using the ALT
13c71 45 52 20 54 41 42 4c 45 0a 2a 2a 20 41 44 44 20  ER TABLE.** ADD 
13c72 43 4f 4c 55 4d 4e 20 63 6f 6d 6d 61 6e 64 2e 20  COLUMN command. 
13c73 20 49 66 20 50 33 20 69 73 20 61 6e 20 6f 72 64   If P3 is an ord
13c74 69 6e 61 72 79 20 73 74 72 69 6e 67 2c 20 6a 75  inary string, ju
13c75 73 74 20 70 75 73 68 20 61 20 4e 55 4c 4c 2e 0a  st push a NULL..
13c76 2a 2a 20 57 68 65 6e 20 50 33 20 69 73 20 61 20  ** When P3 is a 
13c77 73 74 72 69 6e 67 20 69 74 20 69 73 20 72 65 61  string it is rea
13c78 6c 6c 79 20 6a 75 73 74 20 61 20 63 6f 6d 6d 65  lly just a comme
13c79 6e 74 20 64 65 73 63 72 69 62 69 6e 67 20 74 68  nt describing th
13c7a 65 20 76 61 6c 75 65 0a 2a 2a 20 74 6f 20 62 65  e value.** to be
13c7b 20 70 75 73 68 65 64 2c 20 6e 6f 74 20 61 20 64   pushed, not a d
13c7c 65 66 61 75 6c 74 20 76 61 6c 75 65 2e 0a 2a 2f  efault value..*/
13c7d 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a  .case OP_Column:
13c7e 20 7b 0a 20 20 75 33 32 20 70 61 79 6c 6f 61 64   {.  u32 payload
13c7f 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65  Size;   /* Numbe
13c80 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
13c81 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  e record */.  in
13c82 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20  t p1 = pOp->p1; 
13c83 20 2f 2a 20 50 31 20 76 61 6c 75 65 20 6f 66 20   /* P1 value of 
13c84 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  the opcode */.  
13c85 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32  int p2 = pOp->p2
13c86 3b 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d  ;  /* column num
13c87 62 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 20  ber to retrieve 
13c88 2a 2f 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 20  */.  Cursor *pC 
13c89 3d 20 30 3b 20 20 20 20 2f 2a 20 54 68 65 20 56  = 0;    /* The V
13c8a 44 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  DBE cursor */.  
13c8b 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20  char *zRec;     
13c8c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
13c8d 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64   complete record
13c8e 2d 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72  -data */.  BtCur
13c8f 73 6f 72 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a  sor *pCrsr;   /*
13c90 20 54 68 65 20 42 54 72 65 65 20 63 75 72 73 6f   The BTree curso
13c91 72 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70  r */.  u32 *aTyp
13c92 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79  e;        /* aTy
13c93 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20  pe[i] holds the 
13c94 6e 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20  numeric type of 
13c95 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20  the i-th column 
13c96 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65  */.  u32 *aOffse
13c97 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73  t;      /* aOffs
13c98 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20  et[i] is offset 
13c99 74 6f 20 73 74 61 72 74 20 6f 66 20 64 61 74 61  to start of data
13c9a 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e   for i-th column
13c9b 20 2a 2f 0a 20 20 75 33 32 20 6e 46 69 65 6c 64   */.  u32 nField
13c9c 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62  ;        /* numb
13c9d 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
13c9e 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
13c9f 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20  int len;        
13ca0 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68     /* The length
13ca1 20 6f 66 20 74 68 65 20 73 65 72 69 61 6c 69 7a   of the serializ
13ca2 65 64 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  ed data for the 
13ca3 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20  column */.  int 
13ca4 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  i;             /
13ca5 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
13ca6 2f 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b  /.  char *zData;
13ca7 20 20 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f         /* Part o
13ca8 66 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69  f the record bei
13ca9 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20  ng decoded */.  
13caa 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20 20  Mem sMem;       
13cab 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e     /* For storin
13cac 67 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69  g the record bei
13cad 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 0a 20  ng decoded */.. 
13cae 20 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b   sMem.flags = 0;
13caf 0a 20 20 61 73 73 65 72 74 28 20 70 31 3c 70 2d  .  assert( p1<p-
13cb0 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 54  >nCursor );.  pT
13cb1 6f 73 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c  os++;.  pTos->fl
13cb2 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
13cb3 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b  .  /* This block
13cb4 20 73 65 74 73 20 74 68 65 20 76 61 72 69 61 62   sets the variab
13cb5 6c 65 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 74  le payloadSize t
13cb6 6f 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 6e  o be the total n
13cb7 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 79  umber of.  ** by
13cb8 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
13cb9 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 7a 52 65  d..  **.  ** zRe
13cba 63 20 69 73 20 73 65 74 20 74 6f 20 62 65 20 74  c is set to be t
13cbb 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74  he complete text
13cbc 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 69   of the record i
13cbd 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c  f it is availabl
13cbe 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70  e..  ** The comp
13cbf 6c 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74  lete record text
13cc0 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 69 6c   is always avail
13cc1 61 62 6c 65 20 66 6f 72 20 70 73 65 75 64 6f 2d  able for pseudo-
13cc2 74 61 62 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74  tables.  ** If t
13cc3 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 74 6f  he record is sto
13cc4 72 65 64 20 69 6e 20 61 20 63 75 72 73 6f 72 2c  red in a cursor,
13cc5 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 65   the complete re
13cc6 63 6f 72 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d  cord text.  ** m
13cc7 69 67 68 74 20 62 65 20 61 76 61 69 6c 61 62 6c  ight be availabl
13cc8 65 20 69 6e 20 74 68 65 20 20 70 43 2d 3e 61 52  e in the  pC->aR
13cc9 6f 77 20 63 61 63 68 65 2e 20 20 4f 72 20 69 74  ow cache.  Or it
13cca 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 2e 0a 20   might not be.. 
13ccb 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 20   ** If the data 
13ccc 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 2c 20  is unavailable, 
13ccd 20 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20   zRec is set to 
13cce 4e 55 4c 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  NULL..  **.  ** 
13ccf 57 65 20 61 6c 73 6f 20 63 6f 6d 70 75 74 65 20  We also compute 
13cd0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
13cd1 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 63  lumns in the rec
13cd2 6f 72 64 2e 20 20 46 6f 72 20 63 75 72 73 6f 72  ord.  For cursor
13cd3 73 2c 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62  s,.  ** the numb
13cd4 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 73  er of columns is
13cd5 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 43   stored in the C
13cd6 75 72 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65  ursor.nField ele
13cd7 6d 65 6e 74 2e 20 20 46 6f 72 0a 20 20 2a 2a 20  ment.  For.  ** 
13cd8 72 65 63 6f 72 64 73 20 6f 6e 20 74 68 65 20 73  records on the s
13cd9 74 61 63 6b 2c 20 74 68 65 20 6e 65 78 74 20 65  tack, the next e
13cda 6e 74 72 79 20 64 6f 77 6e 20 6f 6e 20 74 68 65  ntry down on the
13cdb 20 73 74 61 63 6b 20 69 73 20 61 6e 20 69 6e 74   stack is an int
13cdc 65 67 65 72 0a 20 20 2a 2a 20 77 68 69 63 68 20  eger.  ** which 
13cdd 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
13cde 20 72 65 63 6f 72 64 73 2e 0a 20 20 2a 2f 0a 20   records..  */. 
13cdf 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70   pC = p->apCsr[p
13ce0 31 5d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  1];.#ifndef SQLI
13ce1 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
13ce2 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28 20 70  ABLE.  assert( p
13ce3 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d  C->pVtabCursor==
13ce4 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73  0 );.#endif.  as
13ce5 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20  sert( pC!=0 );. 
13ce6 20 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72   if( pC->pCursor
13ce7 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  !=0 ){.    /* Th
13ce8 65 20 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72  e record is stor
13ce9 65 64 20 69 6e 20 61 20 42 2d 54 72 65 65 20 2a  ed in a B-Tree *
13cea 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
13ceb 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
13cec 74 6f 28 70 43 29 3b 0a 20 20 20 20 69 66 28 20  to(pC);.    if( 
13ced 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  rc ) goto abort_
13cee 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
13cef 20 20 7a 52 65 63 20 3d 20 30 3b 0a 20 20 20 20    zRec = 0;.    
13cf0 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72  pCrsr = pC->pCur
13cf1 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 43 2d  sor;.    if( pC-
13cf2 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20  >nullRow ){.    
13cf3 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20    payloadSize = 
13cf4 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
13cf5 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73   pC->cacheStatus
13cf6 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20 29 7b  ==p->cacheCtr ){
13cf7 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69  .      payloadSi
13cf8 7a 65 20 3d 20 70 43 2d 3e 70 61 79 6c 6f 61 64  ze = pC->payload
13cf9 53 69 7a 65 3b 0a 20 20 20 20 20 20 7a 52 65 63  Size;.      zRec
13cfa 20 3d 20 28 63 68 61 72 2a 29 70 43 2d 3e 61 52   = (char*)pC->aR
13cfb 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ow;.    }else if
13cfc 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b  ( pC->isIndex ){
13cfd 0a 20 20 20 20 20 20 69 36 34 20 70 61 79 6c 6f  .      i64 paylo
13cfe 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20 20 20  adSize64;.      
13cff 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53  sqlite3BtreeKeyS
13d00 69 7a 65 28 70 43 72 73 72 2c 20 26 70 61 79 6c  ize(pCrsr, &payl
13d01 6f 61 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 20  oadSize64);.    
13d02 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20    payloadSize = 
13d03 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20  payloadSize64;. 
13d04 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
13d05 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
13d06 53 69 7a 65 28 70 43 72 73 72 2c 20 26 70 61 79  Size(pCrsr, &pay
13d07 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 20 20 7d  loadSize);.    }
13d08 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43  .    nField = pC
13d09 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 7d 65 6c 73  ->nField;.  }els
13d0a 65 20 69 66 28 20 70 43 2d 3e 70 73 65 75 64 6f  e if( pC->pseudo
13d0b 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20  Table ){.    /* 
13d0c 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 74 68  The record is th
13d0d 65 20 73 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20  e sole entry of 
13d0e 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a  a pseudo-table *
13d0f 2f 0a 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a  /.    payloadSiz
13d10 65 20 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20  e = pC->nData;. 
13d11 20 20 20 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44     zRec = pC->pD
13d12 61 74 61 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63  ata;.    pC->cac
13d13 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
13d14 5f 53 54 41 4c 45 3b 0a 20 20 20 20 61 73 73 65  _STALE;.    asse
13d15 72 74 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d  rt( payloadSize=
13d16 3d 30 20 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b  =0 || zRec!=0 );
13d17 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43  .    nField = pC
13d18 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 43  ->nField;.    pC
13d19 72 73 72 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  rsr = 0;.  }else
13d1a 7b 0a 20 20 20 20 7a 52 65 63 20 3d 20 30 3b 0a  {.    zRec = 0;.
13d1b 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20      payloadSize 
13d1c 3d 20 30 3b 0a 20 20 20 20 70 43 72 73 72 20 3d  = 0;.    pCrsr =
13d1d 20 30 3b 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d   0;.    nField =
13d1e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66   0;.  }..  /* If
13d1f 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 69 73 20   payloadSize is 
13d20 30 2c 20 74 68 65 6e 20 6a 75 73 74 20 70 75 73  0, then just pus
13d21 68 20 61 20 4e 55 4c 4c 20 6f 6e 74 6f 20 74 68  h a NULL onto th
13d22 65 20 73 74 61 63 6b 2e 20 2a 2f 0a 20 20 69 66  e stack. */.  if
13d23 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30  ( payloadSize==0
13d24 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
13d25 70 54 6f 73 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d  pTos->flags==MEM
13d26 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 62 72 65  _Null );.    bre
13d27 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 61  ak;.  }.  if( pa
13d28 79 6c 6f 61 64 53 69 7a 65 3e 53 51 4c 49 54 45  yloadSize>SQLITE
13d29 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b 0a 20  _MAX_LENGTH ){. 
13d2a 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
13d2b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
13d2c 70 32 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20  p2<nField );..  
13d2d 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 73  /* Read and pars
13d2e 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 61 64  e the table head
13d2f 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72  er.  Store the r
13d30 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 70 61  esults of the pa
13d31 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  rse.  ** into th
13d32 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20  e record header 
13d33 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f 66 20  cache fields of 
13d34 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f  the cursor..  */
13d35 0a 20 20 69 66 28 20 70 43 20 26 26 20 70 43 2d  .  if( pC && pC-
13d36 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d  >cacheStatus==p-
13d37 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20  >cacheCtr ){.   
13d38 20 61 54 79 70 65 20 3d 20 70 43 2d 3e 61 54 79   aType = pC->aTy
13d39 70 65 3b 0a 20 20 20 20 61 4f 66 66 73 65 74 20  pe;.    aOffset 
13d3a 3d 20 70 43 2d 3e 61 4f 66 66 73 65 74 3b 0a 20  = pC->aOffset;. 
13d3b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38 20 2a   }else{.    u8 *
13d3c 7a 49 64 78 3b 20 20 20 20 20 20 20 20 2f 2a 20  zIdx;        /* 
13d3d 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65  Index into heade
13d3e 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 7a 45 6e  r */.    u8 *zEn
13d3f 64 48 64 72 3b 20 20 20 20 20 2f 2a 20 50 6f 69  dHdr;     /* Poi
13d40 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 62 79  nter to first by
13d41 74 65 20 61 66 74 65 72 20 74 68 65 20 68 65 61  te after the hea
13d42 64 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 6f  der */.    u32 o
13d43 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 4f  ffset;      /* O
13d44 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 64  ffset into the d
13d45 61 74 61 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73  ata */.    int s
13d46 7a 48 64 72 53 7a 3b 20 20 20 20 20 2f 2a 20 53  zHdrSz;     /* S
13d47 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 65  ize of the heade
13d48 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74 20  r size field at 
13d49 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64 20  start of record 
13d4a 2a 2f 0a 20 20 20 20 69 6e 74 20 61 76 61 69 6c  */.    int avail
13d4b 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
13d4c 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76  r of bytes of av
13d4d 61 69 6c 61 62 6c 65 20 64 61 74 61 20 2a 2f 0a  ailable data */.
13d4e 0a 20 20 20 20 61 54 79 70 65 20 3d 20 70 43 2d  .    aType = pC-
13d4f 3e 61 54 79 70 65 3b 0a 20 20 20 20 69 66 28 20  >aType;.    if( 
13d50 61 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20  aType==0 ){.    
13d51 20 20 70 43 2d 3e 61 54 79 70 65 20 3d 20 61 54    pC->aType = aT
13d52 79 70 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ype = sqlite3DbM
13d53 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32 2a 6e  allocRaw(db, 2*n
13d54 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 61 54 79  Field*sizeof(aTy
13d55 70 65 29 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pe) );.    }.   
13d56 20 69 66 28 20 61 54 79 70 65 3d 3d 30 20 29 7b   if( aType==0 ){
13d57 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d  .      goto no_m
13d58 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43  em;.    }.    pC
13d59 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 4f 66 66  ->aOffset = aOff
13d5a 73 65 74 20 3d 20 26 61 54 79 70 65 5b 6e 46 69  set = &aType[nFi
13d5b 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d 3e 70 61  eld];.    pC->pa
13d5c 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61 79 6c  yloadSize = payl
13d5d 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 70 43 2d  oadSize;.    pC-
13d5e 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 70  >cacheStatus = p
13d5f 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 20 20  ->cacheCtr;..   
13d60 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
13d61 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 61 72  ow many bytes ar
13d62 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20  e in the header 
13d63 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 65 63 20  */.    if( zRec 
13d64 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d  ){.      zData =
13d65 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65   zRec;.    }else
13d66 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 2d 3e  {.      if( pC->
13d67 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20  isIndex ){.     
13d68 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72     zData = (char
13d69 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  *)sqlite3BtreeKe
13d6a 79 46 65 74 63 68 28 70 43 72 73 72 2c 20 26 61  yFetch(pCrsr, &a
13d6b 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d 65 6c  vail);.      }el
13d6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 44 61 74  se{.        zDat
13d6d 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
13d6e 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
13d6f 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c 29 3b  (pCrsr, &avail);
13d70 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
13d71 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28 29 2f  * If KeyFetch()/
13d72 44 61 74 61 46 65 74 63 68 28 29 20 6d 61 6e 61  DataFetch() mana
13d73 67 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 65  ged to get the e
13d74 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c 0a 20  ntire payload,. 
13d75 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74 68 65       ** save the
13d76 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68 65 20   payload in the 
13d77 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20  pC->aRow cache. 
13d78 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20   That will save 
13d79 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a  us from.      **
13d7a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20   having to make 
13d7b 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73  additional calls
13d7c 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f   to fetch the co
13d7d 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66  ntent portion of
13d7e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65  .      ** the re
13d7f 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  cord..      */. 
13d80 20 20 20 20 20 69 66 28 20 61 76 61 69 6c 3e 3d       if( avail>=
13d81 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b 0a 20  payloadSize ){. 
13d82 20 20 20 20 20 20 20 7a 52 65 63 20 3d 20 7a 44         zRec = zD
13d83 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70 43 2d  ata;.        pC-
13d84 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 7a 44 61  >aRow = (u8*)zDa
13d85 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ta;.      }else{
13d86 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 52 6f  .        pC->aRo
13d87 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  w = 0;.      }. 
13d88 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20     }.    /* The 
13d89 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
13d8a 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c 6c 20   is true in all 
13d8b 63 61 73 65 73 20 61 63 63 65 70 74 20 77 68 65  cases accept whe
13d8c 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74  n.    ** the dat
13d8d 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62  abase file has b
13d8e 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 65 78  een corrupted ex
13d8f 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a  ternally..    **
13d90 20 20 20 20 61 73 73 65 72 74 28 20 7a 52 65 63      assert( zRec
13d91 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e 3d 70 61  !=0 || avail>=pa
13d92 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 61 76 61  yloadSize || ava
13d93 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20 20 20  il>=9 ); */.    
13d94 73 7a 48 64 72 53 7a 20 3d 20 47 65 74 56 61 72  szHdrSz = GetVar
13d95 69 6e 74 28 28 75 38 2a 29 7a 44 61 74 61 2c 20  int((u8*)zData, 
13d96 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 2f 2a  offset);..    /*
13d97 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20   The KeyFetch() 
13d98 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61  or DataFetch() a
13d99 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e  bove are fast an
13d9a 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65  d will get the e
13d9b 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63  ntire.    ** rec
13d9c 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f  ord header in mo
13d9d 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74  st cases.  But t
13d9e 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f  hey will fail to
13d9f 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74   get the complet
13da0 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20  e.    ** record 
13da1 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65  header if the re
13da2 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73  cord header does
13da3 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69   not fit on a si
13da4 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a  ngle page.    **
13da5 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20   in the B-Tree. 
13da6 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65   When that happe
13da7 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56  ns, use sqlite3V
13da8 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
13da9 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75  ) to.    ** acqu
13daa 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ire the complete
13dab 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20   header text..  
13dac 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 7a 52    */.    if( !zR
13dad 65 63 20 26 26 20 61 76 61 69 6c 3c 6f 66 66 73  ec && avail<offs
13dae 65 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  et ){.      rc =
13daf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
13db0 72 6f 6d 42 74 72 65 65 28 70 43 72 73 72 2c 20  romBtree(pCrsr, 
13db1 30 2c 20 6f 66 66 73 65 74 2c 20 70 43 2d 3e 69  0, offset, pC->i
13db2 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a  sIndex, &sMem);.
13db3 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
13db4 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13db5 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
13db6 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  n_out;.      }. 
13db7 20 20 20 20 20 7a 44 61 74 61 20 3d 20 73 4d 65       zData = sMe
13db8 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  m.z;.    }.    z
13db9 45 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26  EndHdr = (u8 *)&
13dba 7a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20  zData[offset];. 
13dbb 20 20 20 7a 49 64 78 20 3d 20 28 75 38 20 2a 29     zIdx = (u8 *)
13dbc 26 7a 44 61 74 61 5b 73 7a 48 64 72 53 7a 5d 3b  &zData[szHdrSz];
13dbd 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 68  ..    /* Scan th
13dbe 65 20 68 65 61 64 65 72 20 61 6e 64 20 75 73 65  e header and use
13dbf 20 69 74 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74   it to fill in t
13dc0 68 65 20 61 54 79 70 65 5b 5d 20 61 6e 64 20 61  he aType[] and a
13dc1 4f 66 66 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20  Offset[].    ** 
13dc2 61 72 72 61 79 73 2e 20 20 61 54 79 70 65 5b 69  arrays.  aType[i
13dc3 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74  ] will contain t
13dc4 68 65 20 74 79 70 65 20 69 6e 74 65 67 65 72 20  he type integer 
13dc5 66 6f 72 20 74 68 65 20 69 2d 74 68 0a 20 20 20  for the i-th.   
13dc6 20 2a 2a 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 61   ** column and a
13dc7 4f 66 66 73 65 74 5b 69 5d 20 77 69 6c 6c 20 63  Offset[i] will c
13dc8 6f 6e 74 61 69 6e 20 74 68 65 20 6f 66 66 73 65  ontain the offse
13dc9 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e  t from the begin
13dca 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74  ning.    ** of t
13dcb 68 65 20 72 65 63 6f 72 64 20 74 6f 20 74 68 65  he record to the
13dcc 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 64 61   start of the da
13dcd 74 61 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20  ta for the i-th 
13dce 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20  column.    */.  
13dcf 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 69    for(i=0; i<nFi
13dd0 65 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  eld; i++){.     
13dd1 20 69 66 28 20 7a 49 64 78 3c 7a 45 6e 64 48 64   if( zIdx<zEndHd
13dd2 72 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4f 66  r ){.        aOf
13dd3 66 73 65 74 5b 69 5d 20 3d 20 6f 66 66 73 65 74  fset[i] = offset
13dd4 3b 0a 20 20 20 20 20 20 20 20 7a 49 64 78 20 2b  ;.        zIdx +
13dd5 3d 20 47 65 74 56 61 72 69 6e 74 28 7a 49 64 78  = GetVarint(zIdx
13dd6 2c 20 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20  , aType[i]);.   
13dd7 20 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 73       offset += s
13dd8 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
13dd9 54 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d  TypeLen(aType[i]
13dda 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
13ddb 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 20          /* If i 
13ddc 69 73 20 6c 65 73 73 20 74 68 61 74 20 6e 46 69  is less that nFi
13ddd 65 6c 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  eld, then there 
13dde 61 72 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20  are less fields 
13ddf 69 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  in this.        
13de0 2a 2a 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53  ** record than S
13de1 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64  etNumColumns ind
13de2 69 63 61 74 65 64 20 74 68 65 72 65 20 61 72 65  icated there are
13de3 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a   columns in the.
13de4 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65          ** table
13de5 2e 20 53 65 74 20 74 68 65 20 6f 66 66 73 65 74  . Set the offset
13de6 20 66 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63   for any extra c
13de7 6f 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65  olumns not prese
13de8 6e 74 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a  nt in.        **
13de9 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30   the record to 0
13dea 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64  . This tells cod
13deb 65 20 62 65 6c 6f 77 20 74 6f 20 70 75 73 68 20  e below to push 
13dec 61 20 4e 55 4c 4c 20 6f 6e 74 6f 20 74 68 65 0a  a NULL onto the.
13ded 20 20 20 20 20 20 20 20 2a 2a 20 73 74 61 63 6b          ** stack
13dee 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73 65   instead of dese
13def 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75  rializing a valu
13df0 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72  e from the recor
13df1 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  d..        */.  
13df2 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69 5d        aOffset[i]
13df3 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
13df4 20 20 7d 0a 20 20 20 20 52 65 6c 65 61 73 65 28    }.    Release(
13df5 26 73 4d 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d  &sMem);.    sMem
13df6 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  .flags = MEM_Nul
13df7 6c 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  l;..    /* If we
13df8 20 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20   have read more 
13df9 68 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e  header data than
13dfa 20 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69   was contained i
13dfb 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20  n the header,.  
13dfc 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65    ** or if the e
13dfd 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66  nd of the last f
13dfe 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20  ield appears to 
13dff 62 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  be past the end 
13e00 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65  of the.    ** re
13e01 63 6f 72 64 2c 20 74 68 65 6e 20 77 65 20 6d 75  cord, then we mu
13e02 73 74 20 62 65 20 64 65 61 6c 69 6e 67 20 77 69  st be dealing wi
13e03 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  th a corrupt dat
13e04 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  abase..    */.  
13e05 20 20 69 66 28 20 7a 49 64 78 3e 7a 45 6e 64 48    if( zIdx>zEndH
13e06 64 72 20 7c 7c 20 6f 66 66 73 65 74 3e 70 61 79  dr || offset>pay
13e07 6c 6f 61 64 53 69 7a 65 20 29 7b 0a 20 20 20 20  loadSize ){.    
13e08 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
13e09 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
13e0a 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e    goto op_column
13e0b 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  _out;.    }.  }.
13e0c 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f  .  /* Get the co
13e0d 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  lumn information
13e0e 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d  . If aOffset[p2]
13e0f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
13e10 65 6e 20 0a 20 20 2a 2a 20 64 65 73 65 72 69 61  en .  ** deseria
13e11 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 65 20 66  lize the value f
13e12 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 20  rom the record. 
13e13 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d 20 69  If aOffset[p2] i
13e14 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65  s zero,.  ** the
13e15 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 74 20  n there are not 
13e16 65 6e 6f 75 67 68 20 66 69 65 6c 64 73 20 69 6e  enough fields in
13e17 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 73   the record to s
13e18 61 74 69 73 66 79 20 74 68 65 0a 20 20 2a 2a 20  atisfy the.  ** 
13e19 72 65 71 75 65 73 74 2e 20 20 49 6e 20 74 68 69  request.  In thi
13e1a 73 20 63 61 73 65 2c 20 73 65 74 20 74 68 65 20  s case, set the 
13e1b 76 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f  value NULL or to
13e1c 20 50 33 20 69 66 20 50 33 20 69 73 0a 20 20 2a   P3 if P3 is.  *
13e1d 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
13e1e 20 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a   Mem object..  *
13e1f 2f 0a 20 20 69 66 28 20 61 4f 66 66 73 65 74 5b  /.  if( aOffset[
13e20 70 32 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72  p2] ){.    asser
13e21 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
13e22 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 65 63   );.    if( zRec
13e23 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61 20   ){.      zData 
13e24 3d 20 26 7a 52 65 63 5b 61 4f 66 66 73 65 74 5b  = &zRec[aOffset[
13e25 70 32 5d 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  p2]];.    }else{
13e26 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c  .      len = sql
13e27 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
13e28 70 65 4c 65 6e 28 61 54 79 70 65 5b 70 32 5d 29  peLen(aType[p2])
13e29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
13e2a 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42  ite3VdbeMemFromB
13e2b 74 72 65 65 28 70 43 72 73 72 2c 20 61 4f 66 66  tree(pCrsr, aOff
13e2c 73 65 74 5b 70 32 5d 2c 20 6c 65 6e 2c 20 70 43  set[p2], len, pC
13e2d 2d 3e 69 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d  ->isIndex, &sMem
13e2e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
13e2f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13e30 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
13e31 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  lumn_out;.      
13e32 7d 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20  }.      zData = 
13e33 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20 20  sMem.z;.    }.  
13e34 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72    sqlite3VdbeSer
13e35 69 61 6c 47 65 74 28 28 75 38 2a 29 7a 44 61 74  ialGet((u8*)zDat
13e36 61 2c 20 61 54 79 70 65 5b 70 32 5d 2c 20 70 54  a, aType[p2], pT
13e37 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 65  os);.    pTos->e
13e38 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20  nc = encoding;. 
13e39 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
13e3a 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50 33 5f  pOp->p3type==P3_
13e3b 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  MEM ){.      sql
13e3c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c  ite3VdbeMemShall
13e3d 6f 77 43 6f 70 79 28 70 54 6f 73 2c 20 28 4d 65  owCopy(pTos, (Me
13e3e 6d 20 2a 29 28 70 4f 70 2d 3e 70 33 29 2c 20 4d  m *)(pOp->p3), M
13e3f 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20  EM_Static);.    
13e40 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 6f  }else{.      pTo
13e41 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  s->flags = MEM_N
13e42 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ull;.    }.  }..
13e43 20 20 2f 2a 20 49 66 20 77 65 20 64 79 6e 61 6d    /* If we dynam
13e44 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
13e45 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   space to hold t
13e46 68 65 20 64 61 74 61 20 28 69 6e 20 74 68 65 0a  he data (in the.
13e47 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65    ** sqlite3Vdbe
13e48 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 63  MemFromBtree() c
13e49 61 6c 6c 20 61 62 6f 76 65 29 20 74 68 65 6e 20  all above) then 
13e4a 74 72 61 6e 73 66 65 72 20 63 6f 6e 74 72 6f 6c  transfer control
13e4b 20 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20 64 79   of that.  ** dy
13e4c 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
13e4d 74 65 64 20 73 70 61 63 65 20 6f 76 65 72 20 74  ted space over t
13e4e 6f 20 74 68 65 20 70 54 6f 73 20 73 74 72 75 63  o the pTos struc
13e4f 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20  ture..  ** This 
13e50 70 72 65 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72  prevents a memor
13e51 79 20 63 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69  y copy..  */.  i
13e52 66 28 20 28 73 4d 65 6d 2e 66 6c 61 67 73 20 26  f( (sMem.flags &
13e53 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 29 7b 0a   MEM_Dyn)!=0 ){.
13e54 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 73      assert( pTos
13e55 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 45 70  ->flags & MEM_Ep
13e56 68 65 6d 20 29 3b 0a 20 20 20 20 61 73 73 65 72  hem );.    asser
13e57 74 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  t( pTos->flags &
13e58 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c   (MEM_Str|MEM_Bl
13e59 6f 62 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ob) );.    asser
13e5a 74 28 20 70 54 6f 73 2d 3e 7a 3d 3d 73 4d 65 6d  t( pTos->z==sMem
13e5b 2e 7a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  .z );.    assert
13e5c 28 20 73 4d 65 6d 2e 66 6c 61 67 73 20 26 20 4d  ( sMem.flags & M
13e5d 45 4d 5f 54 65 72 6d 20 29 3b 0a 20 20 20 20 70  EM_Term );.    p
13e5e 54 6f 73 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d  Tos->flags &= ~M
13e5f 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 70 54  EM_Ephem;.    pT
13e60 6f 73 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  os->flags |= MEM
13e61 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20  _Dyn|MEM_Term;. 
13e62 20 7d 0a 0a 20 20 2f 2a 20 70 54 6f 73 2d 3e 7a   }..  /* pTos->z
13e63 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74 69   might be pointi
13e64 6e 67 20 74 6f 20 73 4d 65 6d 2e 7a 53 68 6f 72  ng to sMem.zShor
13e65 74 5b 5d 2e 20 20 46 69 78 20 74 68 61 74 20 73  t[].  Fix that s
13e66 6f 20 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 63  o that we.  ** c
13e67 61 6e 20 61 62 61 6e 64 6f 6e 20 73 4d 65 6d 20  an abandon sMem 
13e68 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
13e69 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
13e6a 65 61 62 6c 65 28 70 54 6f 73 29 3b 0a 0a 6f 70  eable(pTos);..op
13e6b 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a 20 20 62  _column_out:.  b
13e6c 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
13e6d 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 20 50  de: MakeRecord P
13e6e 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 43 6f  1 P2 P3.**.** Co
13e6f 6e 76 65 72 74 20 74 68 65 20 74 6f 70 20 61 62  nvert the top ab
13e70 73 28 50 31 29 20 65 6e 74 72 69 65 73 20 6f 66  s(P1) entries of
13e71 20 74 68 65 20 73 74 61 63 6b 20 69 6e 74 6f 20   the stack into 
13e72 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a  a single entry.*
13e73 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75  * suitable for u
13e74 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65 63  se as a data rec
13e75 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61 73  ord in a databas
13e76 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61 20  e table or as a 
13e77 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e 64  key.** in an ind
13e78 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 6c 73  ex.  The details
13e79 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 61   of the format a
13e7a 72 65 20 69 72 72 65 6c 61 76 61 6e 74 20 61 73  re irrelavant as
13e7b 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65 20   long as.** the 
13e7c 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
13e7d 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65 20   can decode the 
13e7e 72 65 63 6f 72 64 20 6c 61 74 65 72 20 61 6e 64  record later and
13e7f 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 0a   as long as the.
13e80 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  ** sqlite3VdbeRe
13e81 63 6f 72 64 43 6f 6d 70 61 72 65 20 66 75 6e 63  cordCompare func
13e82 74 69 6f 6e 20 77 69 6c 6c 20 63 6f 72 72 65 63  tion will correc
13e83 74 6c 79 20 63 6f 6d 70 61 72 65 20 74 77 6f 20  tly compare two 
13e84 65 6e 63 6f 64 65 64 0a 2a 2a 20 72 65 63 6f 72  encoded.** recor
13e85 64 73 2e 20 20 52 65 66 65 72 20 74 6f 20 73 6f  ds.  Refer to so
13e86 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e  urce code commen
13e87 74 73 20 66 6f 72 20 74 68 65 20 64 65 74 61 69  ts for the detai
13e88 6c 73 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  ls of the record
13e89 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a  .** format..**.*
13e8a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 73  * The original s
13e8b 74 61 63 6b 20 65 6e 74 72 69 65 73 20 61 72 65  tack entries are
13e8c 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65   popped from the
13e8d 20 73 74 61 63 6b 20 69 66 20 50 31 3e 30 20 62   stack if P1>0 b
13e8e 75 74 0a 2a 2a 20 72 65 6d 61 69 6e 20 6f 6e 20  ut.** remain on 
13e8f 74 68 65 20 73 74 61 63 6b 20 69 66 20 50 31 3c  the stack if P1<
13e90 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69  0..**.** If P2 i
13e91 73 20 6e 6f 74 20 7a 65 72 6f 20 61 6e 64 20 6f  s not zero and o
13e92 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
13e93 65 20 65 6e 74 72 69 65 73 20 61 72 65 20 4e 55  e entries are NU
13e94 4c 4c 2c 20 74 68 65 6e 20 6a 75 6d 70 0a 2a 2a  LL, then jump.**
13e95 20 74 6f 20 74 68 65 20 61 64 64 72 65 73 73 20   to the address 
13e96 67 69 76 65 6e 20 62 79 20 50 32 2e 20 20 54 68  given by P2.  Th
13e97 69 73 20 66 65 61 74 75 72 65 20 63 61 6e 20 62  is feature can b
13e98 65 20 75 73 65 64 20 74 6f 20 73 6b 69 70 20 61  e used to skip a
13e99 0a 2a 2a 20 75 6e 69 71 75 65 6e 65 73 73 20 74  .** uniqueness t
13e9a 65 73 74 20 6f 6e 20 69 6e 64 69 63 65 73 2e 0a  est on indices..
13e9b 2a 2a 0a 2a 2a 20 50 33 20 6d 61 79 20 62 65 20  **.** P3 may be 
13e9c 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73  a string that is
13e9d 20 50 31 20 63 68 61 72 61 63 74 65 72 73 20 6c   P1 characters l
13e9e 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20 63 68  ong.  The nth ch
13e9f 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a  aracter of the.*
13ea0 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74  * string indicat
13ea1 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66  es the column af
13ea2 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75  finity that shou
13ea3 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ld be used for t
13ea4 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 6c 64 20  he nth.** field 
13ea5 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79  of the index key
13ea6 20 28 69 2e 65 2e 20 74 68 65 20 66 69 72 73 74   (i.e. the first
13ea7 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 50 33   character of P3
13ea8 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
13ea9 74 68 65 0a 2a 2a 20 6c 6f 77 65 73 74 20 65 6c  the.** lowest el
13eaa 65 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 74 61  ement on the sta
13eab 63 6b 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  ck)..**.** The m
13eac 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72  apping from char
13ead 61 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74  acter to affinit
13eae 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68  y is given by th
13eaf 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a  e SQLITE_AFF_.**
13eb0 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20   macros defined 
13eb1 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a  in sqliteInt.h..
13eb2 2a 2a 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 4e  **.** If P3 is N
13eb3 55 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64  ULL then all ind
13eb4 65 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74  ex fields have t
13eb5 68 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45  he affinity NONE
13eb6 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
13eb7 20 4f 50 5f 4d 61 6b 65 49 64 78 52 65 63 0a 2a   OP_MakeIdxRec.*
13eb8 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 61 6b  /./* Opcode: Mak
13eb9 65 49 64 78 52 65 63 20 50 31 20 50 32 20 50 33  eIdxRec P1 P2 P3
13eba 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
13ebb 64 65 20 77 6f 72 6b 73 20 6a 75 73 74 20 4f 50  de works just OP
13ebc 5f 4d 61 6b 65 52 65 63 6f 72 64 20 65 78 63 65  _MakeRecord exce
13ebd 70 74 20 74 68 61 74 20 69 74 20 72 65 61 64 73  pt that it reads
13ebe 20 61 6e 20 65 78 74 72 61 0a 2a 2a 20 69 6e 74   an extra.** int
13ebf 65 67 65 72 20 66 72 6f 6d 20 74 68 65 20 73 74  eger from the st
13ec0 61 63 6b 20 28 74 68 75 73 20 72 65 61 64 69 6e  ack (thus readin
13ec1 67 20 61 20 74 6f 74 61 6c 20 6f 66 20 61 62 73  g a total of abs
13ec2 28 50 31 2b 31 29 20 65 6e 74 72 69 65 73 29 0a  (P1+1) entries).
13ec3 2a 2a 20 61 6e 64 20 61 70 70 65 6e 64 73 20 74  ** and appends t
13ec4 68 61 74 20 65 78 74 72 61 20 69 6e 74 65 67 65  hat extra intege
13ec5 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  r to the end of 
13ec6 74 68 65 20 72 65 63 6f 72 64 20 61 73 20 61 20  the record as a 
13ec7 76 61 72 69 6e 74 2e 0a 2a 2a 20 54 68 69 73 20  varint..** This 
13ec8 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 69 6e  results in an in
13ec9 64 65 78 20 6b 65 79 2e 0a 2a 2f 0a 63 61 73 65  dex key..*/.case
13eca 20 4f 50 5f 4d 61 6b 65 49 64 78 52 65 63 3a 0a   OP_MakeIdxRec:.
13ecb 63 61 73 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  case OP_MakeReco
13ecc 72 64 3a 20 7b 0a 20 20 2f 2a 20 41 73 73 75 6d  rd: {.  /* Assum
13ecd 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 63  ing the record c
13ece 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 73  ontains N fields
13ecf 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72  , the record for
13ed0 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20 6c  mat looks.  ** l
13ed1 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20  ike this:.  **. 
13ed2 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ** ------------
13ed3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13ed4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13ed5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13ed6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a  ------------.  *
13ed7 2a 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20 74  * | hdr-size | t
13ed8 79 70 65 20 30 20 7c 20 74 79 70 65 20 31 20 7c  ype 0 | type 1 |
13ed9 20 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31 20   ... | type N-1 
13eda 7c 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c 20  | data0 | ... | 
13edb 64 61 74 61 20 4e 2d 31 20 7c 20 0a 20 20 2a 2a  data N-1 | .  **
13edc 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
13edd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13ede 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13edf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13ee0 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20  ---------.  **. 
13ee1 20 2a 2a 20 44 61 74 61 28 30 29 20 69 73 20 74   ** Data(0) is t
13ee2 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 6c 6f  aken from the lo
13ee3 77 65 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  west element of 
13ee4 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20 64 61  the stack and da
13ee5 74 61 28 4e 2d 31 29 20 69 73 0a 20 20 2a 2a 20  ta(N-1) is.  ** 
13ee6 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
13ee7 74 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  tack..  **.  ** 
13ee8 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64 20  Each type field 
13ee9 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70 72  is a varint repr
13eea 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72  esenting the ser
13eeb 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20  ial type of the 
13eec 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  .  ** correspond
13eed 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74  ing data element
13eee 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 64 62   (see sqlite3Vdb
13eef 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20  eSerialType()). 
13ef0 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a  The.  ** hdr-siz
13ef1 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20  e field is also 
13ef2 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20 69  a varint which i
13ef3 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f  s the offset fro
13ef4 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
13ef5 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f    ** of the reco
13ef6 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 2a  rd to data0..  *
13ef7 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f  /.  u8 *zNewReco
13ef8 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20  rd;        /* A 
13ef9 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
13efa 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  he data for the 
13efb 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
13efc 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20  Mem *pRec;      
13efd 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
13efe 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65  w record */.  Me
13eff 6d 20 2a 70 52 6f 77 69 64 20 3d 20 30 3b 20 20  m *pRowid = 0;  
13f00 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 61 70       /* Rowid ap
13f01 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6e 65  pended to the ne
13f02 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36  w record */.  u6
13f03 34 20 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 20  4 nData = 0;    
13f04 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
13f05 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  f bytes of data 
13f06 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  space */.  int n
13f07 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Hdr = 0;        
13f08 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
13f09 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73  ytes of header s
13f0a 70 61 63 65 20 2a 2f 0a 20 20 75 36 34 20 6e 42  pace */.  u64 nB
13f0b 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  yte = 0;        
13f0c 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72   /* Data space r
13f0d 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73  equired for this
13f0e 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
13f0f 20 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20   nZero = 0;     
13f10 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
13f11 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74   zero bytes at t
13f12 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65  he end of the re
13f13 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56  cord */.  int nV
13f14 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  arint;          
13f15 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
13f16 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20  tes in a varint 
13f17 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f  */.  u32 serial_
13f18 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54  type;       /* T
13f19 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69  ype field */.  i
13f1a 6e 74 20 63 6f 6e 74 61 69 6e 73 4e 75 6c 6c 20  nt containsNull 
13f1b 3d 20 30 3b 20 20 2f 2a 20 54 72 75 65 20 69 66  = 0;  /* True if
13f1c 20 61 6e 79 20 6f 66 20 74 68 65 20 64 61 74 61   any of the data
13f1d 20 66 69 65 6c 64 73 20 61 72 65 20 4e 55 4c 4c   fields are NULL
13f1e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61   */.  Mem *pData
13f1f 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0;           /* 
13f20 42 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 73 74  Bottom of the st
13f21 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61  ack */.  int lea
13f22 76 65 4f 6e 53 74 61 63 6b 3b 20 20 20 20 20 20  veOnStack;      
13f23 2f 2a 20 49 66 20 74 72 75 65 2c 20 6c 65 61 76  /* If true, leav
13f24 65 20 74 68 65 20 65 6e 74 72 69 65 73 20 6f 6e  e the entries on
13f25 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20   the stack */.  
13f26 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20  int nField;     
13f27 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
13f28 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68   of fields in th
13f29 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  e record */.  in
13f2a 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 3b 20 20 20  t jumpIfNull;   
13f2b 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
13f2c 65 20 69 66 20 6e 6f 6e 2d 7a 65 72 6f 20 61 6e  e if non-zero an
13f2d 64 20 61 6e 79 20 65 6e 74 72 69 65 73 20 61 72  d any entries ar
13f2e 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 6e 74  e NULL. */.  int
13f2f 20 61 64 64 52 6f 77 69 64 3b 20 20 20 20 20 20   addRowid;      
13f30 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 61      /* True to a
13f31 70 70 65 6e 64 20 61 20 72 6f 77 69 64 20 63 6f  ppend a rowid co
13f32 6c 75 6d 6e 20 61 74 20 74 68 65 20 65 6e 64 20  lumn at the end 
13f33 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 69  */.  char *zAffi
13f34 6e 69 74 79 3b 20 20 20 20 20 20 20 2f 2a 20 54  nity;       /* T
13f35 68 65 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  he affinity stri
13f36 6e 67 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ng for the recor
13f37 64 20 2a 2f 0a 20 20 69 6e 74 20 66 69 6c 65 5f  d */.  int file_
13f38 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a  format;       /*
13f39 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20   File format to 
13f3a 75 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67  use for encoding
13f3b 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
13f3c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13f3d 53 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e  Space used in zN
13f3e 65 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20  ewRecord[] */.  
13f3f 63 68 61 72 20 7a 54 65 6d 70 5b 4e 42 46 53 5d  char zTemp[NBFS]
13f40 3b 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20  ;      /* Space 
13f41 74 6f 20 68 6f 6c 64 20 73 6d 61 6c 6c 20 72 65  to hold small re
13f42 63 6f 72 64 73 20 2a 2f 0a 0a 20 20 6c 65 61 76  cords */..  leav
13f43 65 4f 6e 53 74 61 63 6b 20 3d 20 28 28 70 4f 70  eOnStack = ((pOp
13f44 2d 3e 70 31 3c 30 29 3f 31 3a 30 29 3b 0a 20 20  ->p1<0)?1:0);.  
13f45 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 31  nField = pOp->p1
13f46 20 2a 20 28 6c 65 61 76 65 4f 6e 53 74 61 63 6b   * (leaveOnStack
13f47 3f 2d 31 3a 31 29 3b 0a 20 20 6a 75 6d 70 49 66  ?-1:1);.  jumpIf
13f48 4e 75 6c 6c 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  Null = pOp->p2;.
13f49 20 20 61 64 64 52 6f 77 69 64 20 3d 20 70 4f 70    addRowid = pOp
13f4a 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4d 61 6b  ->opcode==OP_Mak
13f4b 65 49 64 78 52 65 63 3b 0a 20 20 7a 41 66 66 69  eIdxRec;.  zAffi
13f4c 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a  nity = pOp->p3;.
13f4d 0a 20 20 70 44 61 74 61 30 20 3d 20 26 70 54 6f  .  pData0 = &pTo
13f4e 73 5b 31 2d 6e 46 69 65 6c 64 5d 3b 0a 20 20 61  s[1-nField];.  a
13f4f 73 73 65 72 74 28 20 70 44 61 74 61 30 3e 3d 70  ssert( pData0>=p
13f50 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 63 6f  ->aStack );.  co
13f51 6e 74 61 69 6e 73 4e 75 6c 6c 20 3d 20 30 3b 0a  ntainsNull = 0;.
13f52 20 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20    file_format = 
13f53 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  p->minWriteFileF
13f54 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f  ormat;..  /* Loo
13f55 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c  p through the el
13f56 65 6d 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c  ements that will
13f57 20 6d 61 6b 65 20 75 70 20 74 68 65 20 72 65 63   make up the rec
13f58 6f 72 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20  ord to figure.  
13f59 2a 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20  ** out how much 
13f5a 73 70 61 63 65 20 69 73 20 72 65 71 75 69 72 65  space is require
13f5b 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65  d for the new re
13f5c 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  cord..  */.  for
13f5d 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52  (pRec=pData0; pR
13f5e 65 63 3c 3d 70 54 6f 73 3b 20 70 52 65 63 2b 2b  ec<=pTos; pRec++
13f5f 29 7b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a  ){.    int len;.
13f60 20 20 20 20 69 66 28 20 7a 41 66 66 69 6e 69 74      if( zAffinit
13f61 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79  y ){.      apply
13f62 41 66 66 69 6e 69 74 79 28 70 52 65 63 2c 20 7a  Affinity(pRec, z
13f63 41 66 66 69 6e 69 74 79 5b 70 52 65 63 2d 70 44  Affinity[pRec-pD
13f64 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29  ata0], encoding)
13f65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
13f66 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pRec->flags&MEM_
13f67 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 6f  Null ){.      co
13f68 6e 74 61 69 6e 73 4e 75 6c 6c 20 3d 20 31 3b 0a  ntainsNull = 1;.
13f69 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 52      }.    if( pR
13f6a 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65  ec->flags&MEM_Ze
13f6b 72 6f 20 26 26 20 70 52 65 63 2d 3e 6e 3e 30 20  ro && pRec->n>0 
13f6c 29 7b 0a 20 20 20 20 20 20 45 78 70 61 6e 64 42  ){.      ExpandB
13f6d 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20 7d  lob(pRec);.    }
13f6e 0a 20 20 20 20 73 65 72 69 61 6c 5f 74 79 70 65  .    serial_type
13f6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
13f70 72 69 61 6c 54 79 70 65 28 70 52 65 63 2c 20 66  rialType(pRec, f
13f71 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20  ile_format);.   
13f72 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64   len = sqlite3Vd
13f73 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
13f74 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20  serial_type);.  
13f75 20 20 6e 44 61 74 61 20 2b 3d 20 6c 65 6e 3b 0a    nData += len;.
13f76 20 20 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c 69      nHdr += sqli
13f77 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 73 65 72  te3VarintLen(ser
13f78 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69  ial_type);.    i
13f79 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26  f( pRec->flags &
13f7a 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
13f7b 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20     /* Only pure 
13f7c 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42  zero-filled BLOB
13f7d 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74  s can be input t
13f7e 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20  o this Opcode.. 
13f7f 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f       ** We do no
13f80 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69  t allow blobs wi
13f81 74 68 20 61 20 70 72 65 66 69 78 20 61 6e 64 20  th a prefix and 
13f82 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  a zero-filled ta
13f83 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 5a 65  il. */.      nZe
13f84 72 6f 20 2b 3d 20 70 52 65 63 2d 3e 75 2e 69 3b  ro += pRec->u.i;
13f85 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c  .    }else if( l
13f86 65 6e 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72  en ){.      nZer
13f87 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  o = 0;.    }.  }
13f88 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76  ..  /* If we hav
13f89 65 20 74 6f 20 61 70 70 65 6e 64 20 61 20 76 61  e to append a va
13f8a 72 69 6e 74 20 72 6f 77 69 64 20 74 6f 20 74 68  rint rowid to th
13f8b 69 73 20 72 65 63 6f 72 64 2c 20 73 65 74 20 70  is record, set p
13f8c 52 6f 77 69 64 0a 20 20 2a 2a 20 74 6f 20 74 68  Rowid.  ** to th
13f8d 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72  e value of the r
13f8e 6f 77 69 64 20 61 6e 64 20 69 6e 63 72 65 61 73  owid and increas
13f8f 65 20 6e 42 79 74 65 20 62 79 20 74 68 65 20 61  e nByte by the a
13f90 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 0a 20  mount of space. 
13f91 20 2a 2a 20 72 65 71 75 69 72 65 64 20 74 6f 20   ** required to 
13f92 73 74 6f 72 65 20 69 74 2e 0a 20 20 2a 2f 0a 20  store it..  */. 
13f93 20 69 66 28 20 61 64 64 52 6f 77 69 64 20 29 7b   if( addRowid ){
13f94 0a 20 20 20 20 70 52 6f 77 69 64 20 3d 20 26 70  .    pRowid = &p
13f95 54 6f 73 5b 30 2d 6e 46 69 65 6c 64 5d 3b 0a 20  Tos[0-nField];. 
13f96 20 20 20 61 73 73 65 72 74 28 20 70 52 6f 77 69     assert( pRowi
13f97 64 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a  d>=p->aStack );.
13f98 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
13f99 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 52 6f  emIntegerify(pRo
13f9a 77 69 64 29 3b 0a 20 20 20 20 73 65 72 69 61 6c  wid);.    serial
13f9b 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56  _type = sqlite3V
13f9c 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 52  dbeSerialType(pR
13f9d 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 6e 44  owid, 0);.    nD
13f9e 61 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 56 64  ata += sqlite3Vd
13f9f 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
13fa0 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20  serial_type);.  
13fa1 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c 69 74 65    nHdr += sqlite
13fa2 33 56 61 72 69 6e 74 4c 65 6e 28 73 65 72 69 61  3VarintLen(seria
13fa3 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 6e 5a 65  l_type);.    nZe
13fa4 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ro = 0;.  }..  /
13fa5 2a 20 41 64 64 20 74 68 65 20 69 6e 69 74 69 61  * Add the initia
13fa6 6c 20 68 65 61 64 65 72 20 76 61 72 69 6e 74 20  l header varint 
13fa7 61 6e 64 20 74 6f 74 61 6c 20 74 68 65 20 73 69  and total the si
13fa8 7a 65 20 2a 2f 0a 20 20 6e 48 64 72 20 2b 3d 20  ze */.  nHdr += 
13fa9 6e 56 61 72 69 6e 74 20 3d 20 73 71 6c 69 74 65  nVarint = sqlite
13faa 33 56 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29  3VarintLen(nHdr)
13fab 3b 0a 20 20 69 66 28 20 6e 56 61 72 69 6e 74 3c  ;.  if( nVarint<
13fac 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
13fad 28 6e 48 64 72 29 20 29 7b 0a 20 20 20 20 6e 48  (nHdr) ){.    nH
13fae 64 72 2b 2b 3b 0a 20 20 7d 0a 20 20 6e 42 79 74  dr++;.  }.  nByt
13faf 65 20 3d 20 6e 48 64 72 2b 6e 44 61 74 61 2d 6e  e = nHdr+nData-n
13fb0 5a 65 72 6f 3b 0a 20 20 69 66 28 20 6e 42 79 74  Zero;.  if( nByt
13fb1 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  e>SQLITE_MAX_LEN
13fb2 47 54 48 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  GTH ){.    goto 
13fb3 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20  too_big;.  }..  
13fb4 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  /* Allocate spac
13fb5 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65  e for the new re
13fb6 63 6f 72 64 2e 20 2a 2f 0a 20 20 69 66 28 20 6e  cord. */.  if( n
13fb7 42 79 74 65 3e 73 69 7a 65 6f 66 28 7a 54 65 6d  Byte>sizeof(zTem
13fb8 70 29 20 29 7b 0a 20 20 20 20 7a 4e 65 77 52 65  p) ){.    zNewRe
13fb9 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33 44 62  cord = sqlite3Db
13fba 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42  MallocRaw(db, nB
13fbb 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  yte);.    if( !z
13fbc 4e 65 77 52 65 63 6f 72 64 20 29 7b 0a 20 20 20  NewRecord ){.   
13fbd 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
13fbe 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
13fbf 20 20 20 7a 4e 65 77 52 65 63 6f 72 64 20 3d 20     zNewRecord = 
13fc0 28 75 38 2a 29 7a 54 65 6d 70 3b 0a 20 20 7d 0a  (u8*)zTemp;.  }.
13fc1 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  .  /* Write the 
13fc2 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 20 3d 20  record */.  i = 
13fc3 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
13fc4 28 7a 4e 65 77 52 65 63 6f 72 64 2c 20 6e 48 64  (zNewRecord, nHd
13fc5 72 29 3b 0a 20 20 66 6f 72 28 70 52 65 63 3d 70  r);.  for(pRec=p
13fc6 44 61 74 61 30 3b 20 70 52 65 63 3c 3d 70 54 6f  Data0; pRec<=pTo
13fc7 73 3b 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20  s; pRec++){.    
13fc8 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71  serial_type = sq
13fc9 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
13fca 79 70 65 28 70 52 65 63 2c 20 66 69 6c 65 5f 66  ype(pRec, file_f
13fcb 6f 72 6d 61 74 29 3b 0a 20 20 20 20 69 20 2b 3d  ormat);.    i +=
13fcc 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
13fcd 74 28 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 5d  t(&zNewRecord[i]
13fce 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 20  , serial_type); 
13fcf 20 20 20 20 20 2f 2a 20 73 65 72 69 61 6c 20 74       /* serial t
13fd0 79 70 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ype */.  }.  if(
13fd1 20 61 64 64 52 6f 77 69 64 20 29 7b 0a 20 20 20   addRowid ){.   
13fd2 20 69 20 2b 3d 20 73 71 6c 69 74 65 33 50 75 74   i += sqlite3Put
13fd3 56 61 72 69 6e 74 28 26 7a 4e 65 77 52 65 63 6f  Varint(&zNewReco
13fd4 72 64 5b 69 5d 2c 20 73 71 6c 69 74 65 33 56 64  rd[i], sqlite3Vd
13fd5 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 6f  beSerialType(pRo
13fd6 77 69 64 2c 20 30 29 29 3b 0a 20 20 7d 0a 20 20  wid, 0));.  }.  
13fd7 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b  for(pRec=pData0;
13fd8 20 70 52 65 63 3c 3d 70 54 6f 73 3b 20 70 52 65   pRec<=pTos; pRe
13fd9 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61 6c  c++){  /* serial
13fda 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69 20 2b   data */.    i +
13fdb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
13fdc 69 61 6c 50 75 74 28 26 7a 4e 65 77 52 65 63 6f  ialPut(&zNewReco
13fdd 72 64 5b 69 5d 2c 20 6e 42 79 74 65 2d 69 2c 20  rd[i], nByte-i, 
13fde 70 52 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61  pRec, file_forma
13fdf 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 64  t);.  }.  if( ad
13fe0 64 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 69 20  dRowid ){.    i 
13fe1 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  += sqlite3VdbeSe
13fe2 72 69 61 6c 50 75 74 28 26 7a 4e 65 77 52 65 63  rialPut(&zNewRec
13fe3 6f 72 64 5b 69 5d 2c 20 6e 42 79 74 65 2d 69 2c  ord[i], nByte-i,
13fe4 20 70 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 7d   pRowid, 0);.  }
13fe5 0a 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 42  .  assert( i==nB
13fe6 79 74 65 20 29 3b 0a 0a 20 20 2f 2a 20 50 6f 70  yte );..  /* Pop
13fe7 20 65 6e 74 72 69 65 73 20 6f 66 66 20 74 68 65   entries off the
13fe8 20 73 74 61 63 6b 20 69 66 20 72 65 71 75 69 72   stack if requir
13fe9 65 64 2e 20 50 75 73 68 20 74 68 65 20 6e 65 77  ed. Push the new
13fea 20 72 65 63 6f 72 64 20 6f 6e 2e 20 2a 2f 0a 20   record on. */. 
13feb 20 69 66 28 20 21 6c 65 61 76 65 4f 6e 53 74 61   if( !leaveOnSta
13fec 63 6b 20 29 7b 0a 20 20 20 20 70 6f 70 53 74 61  ck ){.    popSta
13fed 63 6b 28 26 70 54 6f 73 2c 20 6e 46 69 65 6c 64  ck(&pTos, nField
13fee 2b 61 64 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a  +addRowid);.  }.
13fef 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 70 54 6f 73    pTos++;.  pTos
13ff0 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20 20 69  ->n = nByte;.  i
13ff1 66 28 20 6e 42 79 74 65 3c 3d 73 69 7a 65 6f 66  f( nByte<=sizeof
13ff2 28 7a 54 65 6d 70 29 20 29 7b 0a 20 20 20 20 61  (zTemp) ){.    a
13ff3 73 73 65 72 74 28 20 7a 4e 65 77 52 65 63 6f 72  ssert( zNewRecor
13ff4 64 3d 3d 28 75 6e 73 69 67 6e 65 64 20 63 68 61  d==(unsigned cha
13ff5 72 20 2a 29 7a 54 65 6d 70 20 29 3b 0a 20 20 20  r *)zTemp );.   
13ff6 20 70 54 6f 73 2d 3e 7a 20 3d 20 70 54 6f 73 2d   pTos->z = pTos-
13ff7 3e 7a 53 68 6f 72 74 3b 0a 20 20 20 20 6d 65 6d  >zShort;.    mem
13ff8 63 70 79 28 70 54 6f 73 2d 3e 7a 53 68 6f 72 74  cpy(pTos->zShort
13ff9 2c 20 7a 54 65 6d 70 2c 20 6e 42 79 74 65 29 3b  , zTemp, nByte);
13ffa 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73  .    pTos->flags
13ffb 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45   = MEM_Blob | ME
13ffc 4d 5f 53 68 6f 72 74 3b 0a 20 20 7d 65 6c 73 65  M_Short;.  }else
13ffd 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 4e  {.    assert( zN
13ffe 65 77 52 65 63 6f 72 64 21 3d 28 75 6e 73 69 67  ewRecord!=(unsig
13fff 6e 65 64 20 63 68 61 72 20 2a 29 7a 54 65 6d 70  ned char *)zTemp
14000 20 29 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 7a 20   );.    pTos->z 
14001 3d 20 28 63 68 61 72 2a 29 7a 4e 65 77 52 65 63  = (char*)zNewRec
14002 6f 72 64 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66  ord;.    pTos->f
14003 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20  lags = MEM_Blob 
14004 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 70  | MEM_Dyn;.    p
14005 54 6f 73 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20  Tos->xDel = 0;. 
14006 20 7d 0a 20 20 69 66 28 20 6e 5a 65 72 6f 20 29   }.  if( nZero )
14007 7b 0a 20 20 20 20 70 54 6f 73 2d 3e 75 2e 69 20  {.    pTos->u.i 
14008 3d 20 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 54 6f  = nZero;.    pTo
14009 73 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  s->flags |= MEM_
1400a 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 70 54 6f 73  Zero;.  }.  pTos
1400b 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
1400c 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65  TF8;  /* In case
1400d 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 65   the blob is eve
1400e 72 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 74  r converted to t
1400f 65 78 74 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  ext */..  /* If 
14010 61 20 4e 55 4c 4c 20 77 61 73 20 65 6e 63 6f 75  a NULL was encou
14011 6e 74 65 72 65 64 20 61 6e 64 20 6a 75 6d 70 49  ntered and jumpI
14012 66 4e 75 6c 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72  fNull is non-zer
14013 6f 2c 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  o, take the jump
14014 2e 20 2a 2f 0a 20 20 69 66 28 20 6a 75 6d 70 49  . */.  if( jumpI
14015 66 4e 75 6c 6c 20 26 26 20 63 6f 6e 74 61 69 6e  fNull && contain
14016 73 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 63 20  sNull ){.    pc 
14017 3d 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 2d 20 31  = jumpIfNull - 1
14018 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
14019 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 61  ../* Opcode: Sta
1401a 74 65 6d 65 6e 74 20 50 31 20 2a 20 2a 0a 2a 2a  tement P1 * *.**
1401b 0a 2a 2a 20 42 65 67 69 6e 20 61 6e 20 69 6e 64  .** Begin an ind
1401c 69 76 69 64 75 61 6c 20 73 74 61 74 65 6d 65 6e  ividual statemen
1401d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68  t transaction wh
1401e 69 63 68 20 69 73 20 70 61 72 74 20 6f 66 20 61  ich is part of a
1401f 20 6c 61 72 67 65 72 0a 2a 2a 20 42 45 47 49 4e   larger.** BEGIN
14020 2e 2e 43 4f 4d 4d 49 54 20 74 72 61 6e 73 61 63  ..COMMIT transac
14021 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e  tion.  This is n
14022 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 74 68  eeded so that th
14023 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63  e statement.** c
14024 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  an be rolled bac
14025 6b 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72  k after an error
14026 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
14027 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65  to roll back the
14028 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73  .** entire trans
14029 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 73 74 61  action.  The sta
1402a 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1402b 6f 6e 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69  on will automati
1402c 63 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20  cally.** commit 
1402d 77 68 65 6e 20 74 68 65 20 56 44 42 45 20 68 61  when the VDBE ha
1402e 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  lts..**.** The s
1402f 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65 67 75  tatement is begu
14030 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
14031 65 20 66 69 6c 65 20 77 69 74 68 20 69 6e 64 65  e file with inde
14032 78 20 50 31 2e 20 20 54 68 65 20 6d 61 69 6e 0a  x P1.  The main.
14033 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
14034 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
14035 20 30 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20   0 and the file 
14036 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61  used for tempora
14037 72 79 20 74 61 62 6c 65 73 0a 2a 2a 20 68 61 73  ry tables.** has
14038 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 0a   an index of 1..
14039 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 61 74 65  */.case OP_State
1403a 6d 65 6e 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a  ment: {       /*
1403b 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e   no-push */.  in
1403c 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
1403d 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 69   Btree *pBt;.  i
1403e 66 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d  f( i>=0 && i<db-
1403f 3e 6e 44 62 20 26 26 20 28 70 42 74 20 3d 20 64  >nDb && (pBt = d
14040 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 3d  b->aDb[i].pBt)!=
14041 30 20 26 26 20 21 28 64 62 2d 3e 61 75 74 6f 43  0 && !(db->autoC
14042 6f 6d 6d 69 74 29 20 29 7b 0a 20 20 20 20 61 73  ommit) ){.    as
14043 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
14044 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29  eeIsInTrans(pBt)
14045 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
14046 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
14047 28 31 3c 3c 69 29 29 21 3d 30 20 29 3b 0a 20 20  (1<<i))!=0 );.  
14048 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42 74    if( !sqlite3Bt
14049 72 65 65 49 73 49 6e 53 74 6d 74 28 70 42 74 29  reeIsInStmt(pBt)
1404a 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
1404b 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e  qlite3BtreeBegin
1404c 53 74 6d 74 28 70 42 74 29 3b 0a 20 20 20 20 20  Stmt(pBt);.     
1404d 20 70 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d   p->openedStatem
1404e 65 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ent = 1;.    }. 
1404f 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
14050 2a 20 4f 70 63 6f 64 65 3a 20 41 75 74 6f 43 6f  * Opcode: AutoCo
14051 6d 6d 69 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a  mmit P1 P2 *.**.
14052 2a 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62  ** Set the datab
14053 61 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  ase auto-commit 
14054 66 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72  flag to P1 (1 or
14055 20 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72   0). If P2 is tr
14056 75 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b  ue, roll.** back
14057 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61   any currently a
14058 63 74 69 76 65 20 62 74 72 65 65 20 74 72 61 6e  ctive btree tran
14059 73 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65  sactions. If the
1405a 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76  re are any activ
1405b 65 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20  e.** VMs (apart 
1405c 66 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20  from this one), 
1405d 74 68 65 6e 20 74 68 65 20 43 4f 4d 4d 49 54 20  then the COMMIT 
1405e 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 73 74 61 74  or ROLLBACK stat
1405f 65 6d 65 6e 74 20 66 61 69 6c 73 2e 0a 2a 2a 0a  ement fails..**.
14060 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
14061 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56  ion causes the V
14062 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61  M to halt..*/.ca
14063 73 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74  se OP_AutoCommit
14064 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d  : {       /* no-
14065 70 75 73 68 20 2a 2f 0a 20 20 75 38 20 69 20 3d  push */.  u8 i =
14066 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 38 20 72   pOp->p1;.  u8 r
14067 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f 70 2d 3e 70  ollback = pOp->p
14068 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3d  2;..  assert( i=
14069 3d 31 20 7c 7c 20 69 3d 3d 30 20 29 3b 0a 20 20  =1 || i==0 );.  
1406a 61 73 73 65 72 74 28 20 69 3d 3d 31 20 7c 7c 20  assert( i==1 || 
1406b 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20 29 3b 0a 0a  rollback==0 );..
1406c 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63    assert( db->ac
1406d 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20 29 3b  tiveVdbeCnt>0 );
1406e 20 20 2f 2a 20 41 74 20 6c 65 61 73 74 20 74 68    /* At least th
1406f 69 73 20 6f 6e 65 20 56 4d 20 69 73 20 61 63 74  is one VM is act
14070 69 76 65 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62  ive */..  if( db
14071 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
14072 31 20 26 26 20 69 20 26 26 20 21 64 62 2d 3e 61  1 && i && !db->a
14073 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  utoCommit ){.   
14074 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74   /* If this inst
14075 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  ruction implemen
14076 74 73 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 52  ts a COMMIT or R
14077 4f 4c 4c 42 41 43 4b 2c 20 6f 74 68 65 72 20 56  OLLBACK, other V
14078 4d 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 74  Ms are.    ** st
14079 69 6c 6c 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64  ill running, and
1407a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
1407b 73 20 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e  s active, return
1407c 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61   an error indica
1407d 74 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 61 74  ting.    ** that
1407e 20 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d   the other VMs m
1407f 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72  ust complete fir
14080 73 74 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  st. .    */.    
14081 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
14082 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 63  (&p->zErrMsg, "c
14083 61 6e 6e 6f 74 20 22 2c 20 72 6f 6c 6c 62 61 63  annot ", rollbac
14084 6b 3f 22 72 6f 6c 6c 62 61 63 6b 22 3a 22 63 6f  k?"rollback":"co
14085 6d 6d 69 74 22 2c 20 0a 20 20 20 20 20 20 20 20  mmit", .        
14086 22 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20  " transaction - 
14087 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69  SQL statements i
14088 6e 20 70 72 6f 67 72 65 73 73 22 2c 20 28 63 68  n progress", (ch
14089 61 72 2a 29 30 29 3b 0a 20 20 20 20 72 63 20 3d  ar*)0);.    rc =
1408a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1408b 20 7d 65 6c 73 65 20 69 66 28 20 69 21 3d 64 62   }else if( i!=db
1408c 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
1408d 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20      if( pOp->p2 
1408e 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1408f 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73   i==1 );.      s
14090 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
14091 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 64 62 2d  l(db);.      db-
14092 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
14093 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
14094 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
14095 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20   = i;.      if( 
14096 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28  sqlite3VdbeHalt(
14097 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  p)==SQLITE_BUSY 
14098 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 54  ){.        p->pT
14099 6f 73 20 3d 20 70 54 6f 73 3b 0a 20 20 20 20 20  os = pTos;.     
1409a 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20     p->pc = pc;. 
1409b 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1409c 6f 6d 6d 69 74 20 3d 20 31 2d 69 3b 0a 20 20 20  ommit = 1-i;.   
1409d 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20       p->rc = rc 
1409e 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
1409f 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65         goto vdbe
140a0 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d  _return;.      }
140a1 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
140a2 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
140a3 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
140a4 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d  LITE_DONE;.    }
140a5 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
140a6 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
140a7 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64     }.    goto vd
140a8 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c  be_return;.  }el
140a9 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  se{.    sqlite3S
140aa 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
140ab 72 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 28 21  rMsg,.        (!
140ac 69 29 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 74  i)?"cannot start
140ad 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77   a transaction w
140ae 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74  ithin a transact
140af 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 28  ion":(.        (
140b0 72 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f  rollback)?"canno
140b1 74 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20  t rollback - no 
140b2 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
140b3 63 74 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20  ctive":.        
140b4 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e             "cann
140b5 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74  ot commit - no t
140b6 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
140b7 74 69 76 65 22 29 2c 20 28 63 68 61 72 2a 29 30  tive"), (char*)0
140b8 29 3b 0a 20 20 20 20 20 20 20 20 20 0a 20 20 20  );.         .   
140b9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
140ba 4f 52 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  OR;.  }.  break;
140bb 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  .}../* Opcode: T
140bc 72 61 6e 73 61 63 74 69 6f 6e 20 50 31 20 50 32  ransaction P1 P2
140bd 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61   *.**.** Begin a
140be 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54   transaction.  T
140bf 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65  he transaction e
140c0 6e 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69  nds when a Commi
140c1 74 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a  t or Rollback.**
140c2 20 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75   opcode is encou
140c3 6e 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69  ntered.  Dependi
140c4 6e 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e  ng on the ON CON
140c5 46 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74  FLICT setting, t
140c6 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  he.** transactio
140c7 6e 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20  n might also be 
140c8 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61  rolled back if a
140c9 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  n error is encou
140ca 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31  ntered..**.** P1
140cb 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
140cc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
140cd 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  le on which the 
140ce 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
140cf 2a 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65  * started.  Inde
140d0 78 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20  x 0 is the main 
140d1 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
140d2 64 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65  d index 1 is the
140d3 0a 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f  .** file used fo
140d4 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  r temporary tabl
140d5 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20  es..**.** If P2 
140d6 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
140d7 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61  n a write-transa
140d8 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64  ction is started
140d9 2e 20 20 41 20 52 45 53 45 52 56 45 44 20 6c 6f  .  A RESERVED lo
140da 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65  ck is.** obtaine
140db 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  d on the databas
140dc 65 20 66 69 6c 65 20 77 68 65 6e 20 61 20 77 72  e file when a wr
140dd 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
140de 69 73 20 73 74 61 72 74 65 64 2e 20 20 4e 6f 0a  is started.  No.
140df 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ** other process
140e0 20 63 61 6e 20 73 74 61 72 74 20 61 6e 6f 74 68   can start anoth
140e1 65 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  er write transac
140e2 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69 73 20  tion while this 
140e3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
140e4 2a 20 75 6e 64 65 72 77 61 79 2e 20 20 53 74 61  * underway.  Sta
140e5 72 74 69 6e 67 20 61 20 77 72 69 74 65 20 74 72  rting a write tr
140e6 61 6e 73 61 63 74 69 6f 6e 20 61 6c 73 6f 20 63  ansaction also c
140e7 72 65 61 74 65 73 20 61 20 72 6f 6c 6c 62 61 63  reates a rollbac
140e8 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 0a 2a 2a 20  k journal. A.** 
140e9 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
140ea 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65  n must be starte
140eb 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61  d before any cha
140ec 6e 67 65 73 20 63 61 6e 20 62 65 20 6d 61 64 65  nges can be made
140ed 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   to the.** datab
140ee 61 73 65 2e 20 20 49 66 20 50 32 20 69 73 20 32  ase.  If P2 is 2
140ef 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 65 6e   or greater then
140f0 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
140f1 63 6b 20 69 73 20 61 6c 73 6f 20 6f 62 74 61 69  ck is also obtai
140f2 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 66 69  ned.** on the fi
140f3 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20  le..**.** If P2 
140f4 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20  is zero, then a 
140f5 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74  read-lock is obt
140f6 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74  ained on the dat
140f7 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63  abase file..*/.c
140f8 61 73 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69  ase OP_Transacti
140f9 6f 6e 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e  on: {       /* n
140fa 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20  o-push */.  int 
140fb 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 42  i = pOp->p1;.  B
140fc 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73  tree *pBt;..  as
140fd 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
140fe 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
140ff 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
14100 73 6b 20 26 20 28 31 3c 3c 69 29 29 21 3d 30 20  sk & (1<<i))!=0 
14101 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61  );.  pBt = db->a
14102 44 62 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20 69 66  Db[i].pBt;..  if
14103 28 20 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20  ( pBt ){.    rc 
14104 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  = sqlite3BtreeBe
14105 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20 70 4f  ginTrans(pBt, pO
14106 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69 66 28 20  p->p2);.    if( 
14107 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
14108 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d  ){.      p->pc =
14109 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63   pc;.      p->rc
1410a 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42   = rc = SQLITE_B
1410b 55 53 59 3b 0a 20 20 20 20 20 20 70 2d 3e 70 54  USY;.      p->pT
1410c 6f 73 20 3d 20 70 54 6f 73 3b 0a 20 20 20 20 20  os = pTos;.     
1410d 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72   goto vdbe_retur
1410e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  n;.    }.    if(
1410f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
14110 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 45 41  & rc!=SQLITE_REA
14111 44 4f 4e 4c 59 20 2f 2a 20 26 26 20 72 63 21 3d  DONLY /* && rc!=
14112 53 51 4c 49 54 45 5f 42 55 53 59 20 2a 2f 20 29  SQLITE_BUSY */ )
14113 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f  {.      goto abo
14114 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
14115 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
14116 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
14117 3a 20 52 65 61 64 43 6f 6f 6b 69 65 20 50 31 20  : ReadCookie P1 
14118 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20  P2 *.**.** Read 
14119 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32  cookie number P2
1411a 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50   from database P
1411b 31 20 61 6e 64 20 70 75 73 68 20 69 74 20 6f 6e  1 and push it on
1411c 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  to the stack..**
1411d 20 50 32 3d 3d 30 20 69 73 20 74 68 65 20 73 63   P2==0 is the sc
1411e 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50  hema version.  P
1411f 32 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61  2==1 is the data
14120 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20  base format..** 
14121 50 32 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63  P2==2 is the rec
14122 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63  ommended pager c
14123 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73  ache size, and s
14124 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20  o forth.  P1==0 
14125 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  is.** the main d
14126 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
14127 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P1==1 is the da
14128 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64  tabase file used
14129 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d   to store.** tem
1412a 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
1412b 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 65  *.** If P1 is ne
1412c 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 69  gative, then thi
1412d 73 20 69 73 20 61 20 72 65 71 75 65 73 74 20 74  s is a request t
1412e 6f 20 72 65 61 64 20 74 68 65 20 73 69 7a 65 20  o read the size 
1412f 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65  of a.** database
14130 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 50 32 20  s free-list. P2 
14131 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 31  must be set to 1
14132 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 54   in this case. T
14133 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 64 61 74  he actual.** dat
14134 61 62 61 73 65 20 61 63 63 65 73 73 65 64 20 69  abase accessed i
14135 73 20 28 28 50 31 2b 31 29 2a 2d 31 29 2e 20 46  s ((P1+1)*-1). F
14136 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 20 50 31  or example, a P1
14137 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 2d 31   parameter of -1
14138 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  .** corresponds 
14139 74 6f 20 64 61 74 61 62 61 73 65 20 30 20 28 22  to database 0 ("
1413a 6d 61 69 6e 22 29 2c 20 61 20 50 31 20 6f 66 20  main"), a P1 of 
1413b 2d 32 20 69 73 20 64 61 74 61 62 61 73 65 20 31  -2 is database 1
1413c 20 28 22 74 65 6d 70 22 29 2e 0a 2a 2a 0a 2a 2a   ("temp")..**.**
1413d 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
1413e 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
1413f 65 20 64 61 74 61 62 61 73 65 20 28 65 69 74 68  e database (eith
14140 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  er a transaction
14141 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 61 72  .** must be star
14142 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d 75 73  ted or there mus
14143 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72  t be an open cur
14144 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a 20 65  sor) before.** e
14145 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e  xecuting this in
14146 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
14147 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  se OP_ReadCookie
14148 3a 20 7b 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b  : {.  int iMeta;
14149 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70  .  int iDb = pOp
1414a 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 69 43 6f 6f  ->p1;.  int iCoo
1414b 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 0a  kie = pOp->p2;..
1414c 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1414d 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45  2<SQLITE_N_BTREE
1414e 5f 4d 45 54 41 20 29 3b 0a 20 20 69 66 28 20 69  _META );.  if( i
1414f 44 62 3c 30 20 29 7b 0a 20 20 20 20 69 44 62 20  Db<0 ){.    iDb 
14150 3d 20 28 2d 31 2a 28 69 44 62 2b 31 29 29 3b 0a  = (-1*(iDb+1));.
14151 20 20 20 20 69 43 6f 6f 6b 69 65 20 2a 3d 20 2d      iCookie *= -
14152 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  1;.  }.  assert(
14153 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
14154 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65  b->nDb );.  asse
14155 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  rt( db->aDb[iDb]
14156 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  .pBt!=0 );.  ass
14157 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
14158 73 6b 20 26 20 28 31 3c 3c 69 44 62 29 29 21 3d  sk & (1<<iDb))!=
14159 30 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e  0 );.  /* The in
1415a 64 65 78 69 6e 67 20 6f 66 20 6d 65 74 61 20 76  dexing of meta v
1415b 61 6c 75 65 73 20 61 74 20 74 68 65 20 73 63 68  alues at the sch
1415c 65 6d 61 20 6c 61 79 65 72 20 69 73 20 6f 66 66  ema layer is off
1415d 20 62 79 20 6f 6e 65 20 66 72 6f 6d 0a 20 20 2a   by one from.  *
1415e 2a 20 74 68 65 20 69 6e 64 65 78 69 6e 67 20 69  * the indexing i
1415f 6e 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65  n the btree laye
14160 72 2e 20 20 54 68 65 20 62 74 72 65 65 20 63 6f  r.  The btree co
14161 6e 73 69 64 65 72 73 20 6d 65 74 61 5b 30 5d 20  nsiders meta[0] 
14162 74 6f 0a 20 20 2a 2a 20 62 65 20 74 68 65 20 6e  to.  ** be the n
14163 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61  umber of free pa
14164 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
14165 61 73 65 20 28 61 20 72 65 61 64 2d 6f 6e 6c 79  ase (a read-only
14166 20 76 61 6c 75 65 29 0a 20 20 2a 2a 20 61 6e 64   value).  ** and
14167 20 6d 65 74 61 5b 31 5d 20 74 6f 20 62 65 20 74   meta[1] to be t
14168 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
14169 2e 20 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61  .  The schema la
1416a 79 65 72 20 63 6f 6e 73 69 64 65 72 73 0a 20 20  yer considers.  
1416b 2a 2a 20 6d 65 74 61 5b 31 5d 20 74 6f 20 62 65  ** meta[1] to be
1416c 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
1416d 69 65 2e 20 20 53 6f 20 77 65 20 68 61 76 65 20  ie.  So we have 
1416e 74 6f 20 73 68 69 66 74 20 74 68 65 20 69 6e 64  to shift the ind
1416f 65 78 0a 20 20 2a 2a 20 62 79 20 6f 6e 65 20 69  ex.  ** by one i
14170 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
14171 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a  statement..  */.
14172 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
14173 72 65 65 47 65 74 4d 65 74 61 28 64 62 2d 3e 61  reeGetMeta(db->a
14174 44 62 5b 69 44 62 5d 2e 70 42 74 2c 20 31 20 2b  Db[iDb].pBt, 1 +
14175 20 69 43 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a   iCookie, (u32 *
14176 29 26 69 4d 65 74 61 29 3b 0a 20 20 70 54 6f 73  )&iMeta);.  pTos
14177 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e 75 2e 69 20  ++;.  pTos->u.i 
14178 3d 20 69 4d 65 74 61 3b 0a 20 20 70 54 6f 73 2d  = iMeta;.  pTos-
14179 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1417a 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1417b 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f 6f 6b   Opcode: SetCook
1417c 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  ie P1 P2 *.**.**
1417d 20 57 72 69 74 65 20 74 68 65 20 74 6f 70 20 6f   Write the top o
1417e 66 20 74 68 65 20 73 74 61 63 6b 20 69 6e 74 6f  f the stack into
1417f 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50   cookie number P
14180 32 20 6f 66 20 64 61 74 61 62 61 73 65 20 50 31  2 of database P1
14181 2e 0a 2a 2a 20 50 32 3d 3d 30 20 69 73 20 74 68  ..** P2==0 is th
14182 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  e schema version
14183 2e 20 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20  .  P2==1 is the 
14184 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e  database format.
14185 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65  .** P2==2 is the
14186 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67   recommended pag
14187 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61  er cache size, a
14188 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31  nd so forth.  P1
14189 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61  ==0 is.** the ma
1418a 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
1418b 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68   and P1==1 is th
1418c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1418d 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a  used to store.**
1418e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1418f 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73  s..**.** A trans
14190 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73  action must be s
14191 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78  tarted before ex
14192 65 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63  ecuting this opc
14193 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ode..*/.case OP_
14194 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20  SetCookie: {    
14195 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
14196 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 61 73  .  Db *pDb;.  as
14197 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 53 51  sert( pOp->p2<SQ
14198 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54  LITE_N_BTREE_MET
14199 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  A );.  assert( p
1419a 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1419b 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  ->p1<db->nDb );.
1419c 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1419d 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1419e 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20  p->p1))!=0 );.  
1419f 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70  pDb = &db->aDb[p
141a0 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
141a1 74 28 20 70 44 62 2d 3e 70 42 74 21 3d 30 20 29  t( pDb->pBt!=0 )
141a2 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 73  ;.  assert( pTos
141a3 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20  >=p->aStack );. 
141a4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
141a5 6e 74 65 67 65 72 69 66 79 28 70 54 6f 73 29 3b  ntegerify(pTos);
141a6 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20 61  .  /* See note a
141a7 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69 66 74  bout index shift
141a8 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43 6f  ing on OP_ReadCo
141a9 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20 73  okie */.  rc = s
141aa 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
141ab 65 4d 65 74 61 28 70 44 62 2d 3e 70 42 74 2c 20  eMeta(pDb->pBt, 
141ac 31 2b 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29  1+pOp->p2, (int)
141ad 70 54 6f 73 2d 3e 75 2e 69 29 3b 0a 20 20 69 66  pTos->u.i);.  if
141ae 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a  ( pOp->p2==0 ){.
141af 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20      /* When the 
141b0 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68  schema cookie ch
141b1 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74 68  anges, record th
141b2 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74  e new cookie int
141b3 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 70  ernally */.    p
141b4 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68  Db->pSchema->sch
141b5 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 70 54 6f  ema_cookie = pTo
141b6 73 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e  s->u.i;.    db->
141b7 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
141b8 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
141b9 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
141ba 70 32 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  p2==1 ){.    /* 
141bb 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69  Record changes i
141bc 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61  n the file forma
141bd 74 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53  t */.    pDb->pS
141be 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
141bf 61 74 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b 0a  at = pTos->u.i;.
141c0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 70    }.  assert( (p
141c1 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Tos->flags & MEM
141c2 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 54  _Dyn)==0 );.  pT
141c3 6f 73 2d 2d 3b 0a 20 20 69 66 28 20 70 4f 70 2d  os--;.  if( pOp-
141c4 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a  >p1==1 ){.    /*
141c5 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20   Invalidate all 
141c6 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
141c7 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74 68 65  nts whenever the
141c8 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 0a 20   TEMP database. 
141c9 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 73 20     ** schema is 
141ca 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b 65 74  changed.  Ticket
141cb 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 73 71   #1644 */.    sq
141cc 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
141cd 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
141ce 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
141cf 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 65  }../* Opcode: Ve
141d0 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20 50 32  rifyCookie P1 P2
141d1 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74   *.**.** Check t
141d2 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c 6f 62  he value of glob
141d3 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 72 61  al database para
141d4 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30 20 28  meter number 0 (
141d5 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 76 65  the.** schema ve
141d6 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b 65 20  rsion) and make 
141d7 73 75 72 65 20 69 74 20 69 73 20 65 71 75 61 6c  sure it is equal
141d8 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50 31 20   to P2.  .** P1 
141d9 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
141da 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73 20  number which is 
141db 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  0 for the main d
141dc 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20  atabase file.** 
141dd 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20 66 69  and 1 for the fi
141de 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f  le holding tempo
141df 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20  rary tables and 
141e0 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75 6d 62  some higher numb
141e1 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69 6c 69  er.** for auxili
141e2 61 72 79 20 64 61 74 61 62 61 73 65 73 2e 0a 2a  ary databases..*
141e3 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69 65 20  *.** The cookie 
141e4 63 68 61 6e 67 65 73 20 69 74 73 20 76 61 6c 75  changes its valu
141e5 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 64  e whenever the d
141e6 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63  atabase schema c
141e7 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69 73 20  hanges..** This 
141e8 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 73 65  operation is use
141e9 64 20 74 6f 20 64 65 74 65 63 74 20 77 68 65 6e  d to detect when
141ea 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b 69 65   that the cookie
141eb 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20   has changed.** 
141ec 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 75 72  and that the cur
141ed 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e 65 65  rent process nee
141ee 64 73 20 74 6f 20 72 65 72 65 61 64 20 74 68 65  ds to reread the
141ef 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 45   schema..**.** E
141f0 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 74  ither a transact
141f1 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68 61 76  ion needs to hav
141f2 65 20 62 65 65 6e 20 73 74 61 72 74 65 64 20 6f  e been started o
141f3 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65 65  r an OP_Open nee
141f4 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78 65 63  ds.** to be exec
141f5 75 74 65 64 20 28 74 6f 20 65 73 74 61 62 6c 69  uted (to establi
141f6 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b 29 20  sh a read lock) 
141f7 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70 63 6f  before this opco
141f8 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64  de is.** invoked
141f9 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 65 72  ..*/.case OP_Ver
141fa 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20  ifyCookie: {    
141fb 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
141fc 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20  .  int iMeta;.  
141fd 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 61 73  Btree *pBt;.  as
141fe 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
141ff 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e   && pOp->p1<db->
14200 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
14201 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
14202 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d   (1<<pOp->p1))!=
14203 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d  0 );.  pBt = db-
14204 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42  >aDb[pOp->p1].pB
14205 74 3b 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a  t;.  if( pBt ){.
14206 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14207 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 42 74  BtreeGetMeta(pBt
14208 2c 20 31 2c 20 28 75 33 32 20 2a 29 26 69 4d 65  , 1, (u32 *)&iMe
14209 74 61 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ta);.  }else{.  
1420a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
1420b 3b 0a 20 20 20 20 69 4d 65 74 61 20 3d 20 30 3b  ;.    iMeta = 0;
1420c 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
1420d 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4d 65 74  QLITE_OK && iMet
1420e 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20  a!=pOp->p2 ){.  
1420f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
14210 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
14211 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c  p->zErrMsg = sql
14212 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
14213 20 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d   "database schem
14214 61 20 68 61 73 20 63 68 61 6e 67 65 64 22 29 3b  a has changed");
14215 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73  .    /* If the s
14216 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 66 72 6f  chema-cookie fro
14217 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
14218 69 6c 65 20 6d 61 74 63 68 65 73 20 74 68 65 20  ile matches the 
14219 63 6f 6f 6b 69 65 20 0a 20 20 20 20 2a 2a 20 73  cookie .    ** s
1421a 74 6f 72 65 64 20 77 69 74 68 20 74 68 65 20 69  tored with the i
1421b 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65  n-memory represe
1421c 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
1421d 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20 20 2a 2a  chema, do.    **
1421e 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74 68 65 20   not reload the 
1421f 73 63 68 65 6d 61 20 66 72 6f 6d 20 74 68 65 20  schema from the 
14220 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20  database file.. 
14221 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
14222 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 73 20 61  virtual-tables a
14223 72 65 20 69 6e 20 75 73 65 2c 20 74 68 69 73 20  re in use, this 
14224 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 6e 20 6f  is not just an o
14225 70 74 69 6d 69 73 61 74 69 6f 6e 2e 0a 20 20 20  ptimisation..   
14226 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d 74 61 62   ** Often, v-tab
14227 6c 65 73 20 73 74 6f 72 65 20 74 68 65 69 72 20  les store their 
14228 64 61 74 61 20 69 6e 20 6f 74 68 65 72 20 53 51  data in other SQ
14229 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 77 68 69  Lite tables, whi
1422a 63 68 0a 20 20 20 20 2a 2a 20 61 72 65 20 71 75  ch.    ** are qu
1422b 65 72 69 65 64 20 66 72 6f 6d 20 77 69 74 68 69  eried from withi
1422c 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 20 6f 74  n xNext() and ot
1422d 68 65 72 20 76 2d 74 61 62 6c 65 20 6d 65 74 68  her v-table meth
1422e 6f 64 73 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a  ods using.    **
1422f 20 70 72 65 70 61 72 65 64 20 71 75 65 72 69 65   prepared querie
14230 73 2e 20 49 66 20 73 75 63 68 20 61 20 71 75 65  s. If such a que
14231 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d 64 61 74  ry is out-of-dat
14232 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e  e, we do not wan
14233 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 63  t to.    ** disc
14234 61 72 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ard the database
14235 20 73 63 68 65 6d 61 2c 20 61 73 20 74 68 65 20   schema, as the 
14236 75 73 65 72 20 63 6f 64 65 20 69 6d 70 6c 65 6d  user code implem
14237 65 6e 74 69 6e 67 20 74 68 65 0a 20 20 20 20 2a  enting the.    *
14238 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75 6c 64 20  * v-table would 
14239 68 61 76 65 20 74 6f 20 62 65 20 72 65 61 64 79  have to be ready
1423a 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33   for the sqlite3
1423b 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 20  _vtab structure 
1423c 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a 20 74 6f  itself.    ** to
1423d 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20   be invalidated 
1423e 77 68 65 6e 65 76 65 72 20 73 71 6c 69 74 65 33  whenever sqlite3
1423f 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c 6c 65  _step() is calle
14240 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 0a 20  d from within . 
14241 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 6c 65 20     ** a v-table 
14242 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a 2f 0a 20  method..    */. 
14243 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 70     if( db->aDb[p
14244 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61 2d  Op->p1].pSchema-
14245 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 21 3d  >schema_cookie!=
14246 69 4d 65 74 61 20 29 7b 0a 20 20 20 20 20 20 73  iMeta ){.      s
14247 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
14248 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 70 4f  nalSchema(db, pO
14249 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 0a 20  p->p1);.    }.. 
1424a 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
1424b 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
1424c 74 73 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d  ts(db);.    rc =
1424d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a   SQLITE_SCHEMA;.
1424e 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1424f 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 52  /* Opcode: OpenR
14250 65 61 64 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  ead P1 P2 P3.**.
14251 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f  ** Open a read-o
14252 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74  nly cursor for t
14253 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
14254 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67  e whose root pag
14255 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20  e is.** P2 in a 
14256 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
14257 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
14258 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  e is determined 
14259 62 79 20 61 6e 20 0a 2a 2a 20 69 6e 74 65 67 65  by an .** intege
1425a 72 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6f  r from the top o
1425b 66 20 74 68 65 20 73 74 61 63 6b 2e 20 20 30 20  f the stack.  0 
1425c 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 64  means the main d
1425d 61 74 61 62 61 73 65 20 61 6e 64 0a 2a 2a 20 31  atabase and.** 1
1425e 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62   means the datab
1425f 61 73 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d  ase used for tem
14260 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20  porary tables.  
14261 47 69 76 65 20 74 68 65 20 6e 65 77 20 0a 2a 2a  Give the new .**
14262 20 63 75 72 73 6f 72 20 61 6e 20 69 64 65 6e 74   cursor an ident
14263 69 66 69 65 72 20 6f 66 20 50 31 2e 20 20 54 68  ifier of P1.  Th
14264 65 20 50 31 20 76 61 6c 75 65 73 20 6e 65 65 64  e P1 values need
14265 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f   not be contiguo
14266 75 73 0a 2a 2a 20 62 75 74 20 61 6c 6c 20 50 31  us.** but all P1
14267 20 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20 62   values should b
14268 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73  e small integers
14269 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f  .  It is an erro
1426a 72 20 66 6f 72 0a 2a 2a 20 50 31 20 74 6f 20 62  r for.** P1 to b
1426b 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a  e negative..**.*
1426c 2a 20 49 66 20 50 32 3d 3d 30 20 74 68 65 6e 20  * If P2==0 then 
1426d 74 61 6b 65 20 74 68 65 20 72 6f 6f 74 20 70 61  take the root pa
1426e 67 65 20 6e 75 6d 62 65 72 20 66 72 6f 6d 20 74  ge number from t
1426f 68 65 20 6e 65 78 74 20 6f 66 20 74 68 65 20 73  he next of the s
14270 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  tack..**.** Ther
14271 65 20 77 69 6c 6c 20 62 65 20 61 20 72 65 61 64  e will be a read
14272 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
14273 61 62 61 73 65 20 77 68 65 6e 65 76 65 72 20 74  abase whenever t
14274 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70  here is an.** op
14275 65 6e 20 63 75 72 73 6f 72 2e 20 20 49 66 20 74  en cursor.  If t
14276 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20  he database was 
14277 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f 72 20 74  unlocked prior t
14278 6f 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  o this instructi
14279 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20 72 65 61  on.** then a rea
1427a 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72  d lock is acquir
1427b 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
1427c 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  is instruction. 
1427d 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20   A read.** lock 
1427e 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f  allows other pro
1427f 63 65 73 73 65 73 20 74 6f 20 72 65 61 64 20 74  cesses to read t
14280 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20  he database but 
14281 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79  prohibits.** any
14282 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 66   other process f
14283 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67 20 74 68  rom modifying th
14284 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
14285 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a   read lock is.**
14286 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61   released when a
14287 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 20 63  ll cursors are c
14288 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20  losed.  If this 
14289 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 74 65  instruction atte
1428a 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61  mpts.** to get a
1428b 20 72 65 61 64 20 6c 6f 63 6b 20 62 75 74 20 66   read lock but f
1428c 61 69 6c 73 2c 20 74 68 65 20 73 63 72 69 70 74  ails, the script
1428d 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69 74 68   terminates with
1428e 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55   an.** SQLITE_BU
1428f 53 59 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  SY error code..*
14290 2a 0a 2a 2a 20 54 68 65 20 50 33 20 76 61 6c 75  *.** The P3 valu
14291 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  e is a pointer t
14292 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  o a KeyInfo stru
14293 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e  cture that defin
14294 65 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e  es the.** conten
14295 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20  t and collating 
14296 73 65 71 75 65 6e 63 65 20 6f 66 20 69 6e 64 69  sequence of indi
14297 63 65 73 2e 20 20 50 33 20 69 73 20 4e 55 4c 4c  ces.  P3 is NULL
14298 20 66 6f 72 20 63 75 72 73 6f 72 73 0a 2a 2a 20   for cursors.** 
14299 74 68 61 74 20 61 72 65 20 6e 6f 74 20 70 6f 69  that are not poi
1429a 6e 74 69 6e 67 20 74 6f 20 69 6e 64 69 63 65 73  nting to indices
1429b 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1429c 20 4f 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a 2f   OpenWrite..*/./
1429d 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57 72  * Opcode: OpenWr
1429e 69 74 65 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  ite P1 P2 P3.**.
1429f 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2f 77  ** Open a read/w
142a0 72 69 74 65 20 63 75 72 73 6f 72 20 6e 61 6d 65  rite cursor name
142a1 64 20 50 31 20 6f 6e 20 74 68 65 20 74 61 62 6c  d P1 on the tabl
142a2 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65  e or index whose
142a3 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73   root.** page is
142a4 20 50 32 2e 20 20 49 66 20 50 32 3d 3d 30 20 74   P2.  If P2==0 t
142a5 68 65 6e 20 74 61 6b 65 20 74 68 65 20 72 6f 6f  hen take the roo
142a6 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 72  t page number fr
142a7 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  om the stack..**
142a8 0a 2a 2a 20 54 68 65 20 50 33 20 76 61 6c 75 65  .** The P3 value
142a9 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
142aa 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
142ab 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e 65  ture that define
142ac 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74  s the.** content
142ad 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73   and collating s
142ae 65 71 75 65 6e 63 65 20 6f 66 20 69 6e 64 69 63  equence of indic
142af 65 73 2e 20 20 50 33 20 69 73 20 4e 55 4c 4c 20  es.  P3 is NULL 
142b0 66 6f 72 20 63 75 72 73 6f 72 73 0a 2a 2a 20 74  for cursors.** t
142b1 68 61 74 20 61 72 65 20 6e 6f 74 20 70 6f 69 6e  hat are not poin
142b2 74 69 6e 67 20 74 6f 20 69 6e 64 69 63 65 73 2e  ting to indices.
142b3 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
142b4 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6a 75  ruction works ju
142b5 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65 61 64  st like OpenRead
142b6 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
142b7 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73 6f 72  opens the cursor
142b8 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72 69 74  .** in read/writ
142b9 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61 20 67  e mode.  For a g
142ba 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68 65 72  iven table, ther
142bb 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20  e can be one or 
142bc 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79 0a 2a  more read-only.*
142bd 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61 20 73  * cursors or a s
142be 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69 74 65  ingle read/write
142bf 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f 74 20   cursor but not 
142c0 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  both..**.** See 
142c1 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e 0a 2a  also OpenRead..*
142c2 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 52 65  /.case OP_OpenRe
142c3 61 64 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ad:          /* 
142c4 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65 20  no-push */.case 
142c5 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 20  OP_OpenWrite: { 
142c6 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
142c7 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f   */.  int i = pO
142c8 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70 32 20  p->p1;.  int p2 
142c9 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74  = pOp->p2;.  int
142ca 20 77 72 46 6c 61 67 3b 0a 20 20 42 74 72 65 65   wrFlag;.  Btree
142cb 20 2a 70 58 3b 0a 20 20 69 6e 74 20 69 44 62 3b   *pX;.  int iDb;
142cc 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  .  Cursor *pCur;
142cd 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 0a 20  .  Db *pDb;.  . 
142ce 20 61 73 73 65 72 74 28 20 70 54 6f 73 3e 3d 70   assert( pTos>=p
142cf 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 73 71  ->aStack );.  sq
142d0 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
142d1 67 65 72 69 66 79 28 70 54 6f 73 29 3b 0a 20 20  gerify(pTos);.  
142d2 69 44 62 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b  iDb = pTos->u.i;
142d3 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 6f 73  .  assert( (pTos
142d4 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79  ->flags & MEM_Dy
142d5 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 54 6f 73 2d  n)==0 );.  pTos-
142d6 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  -;.  assert( iDb
142d7 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
142d8 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
142d9 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
142da 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a  (1<<iDb))!=0 );.
142db 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
142dc 5b 69 44 62 5d 3b 0a 20 20 70 58 20 3d 20 70 44  [iDb];.  pX = pD
142dd 62 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74  b->pBt;.  assert
142de 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 69 66 28  ( pX!=0 );.  if(
142df 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
142e0 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a 20 20  _OpenWrite ){.  
142e1 20 20 77 72 46 6c 61 67 20 3d 20 31 3b 0a 20 20    wrFlag = 1;.  
142e2 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65    if( pDb->pSche
142e3 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
142e4 3c 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c  < p->minWriteFil
142e5 65 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20  eFormat ){.     
142e6 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65   p->minWriteFile
142e7 46 6f 72 6d 61 74 20 3d 20 70 44 62 2d 3e 70 53  Format = pDb->pS
142e8 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
142e9 61 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  at;.    }.  }els
142ea 65 7b 0a 20 20 20 20 77 72 46 6c 61 67 20 3d 20  e{.    wrFlag = 
142eb 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 32 3c  0;.  }.  if( p2<
142ec 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
142ed 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63  ( pTos>=p->aStac
142ee 6b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  k );.    sqlite3
142ef 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
142f0 79 28 70 54 6f 73 29 3b 0a 20 20 20 20 70 32 20  y(pTos);.    p2 
142f1 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b 0a 20 20 20  = pTos->u.i;.   
142f2 20 61 73 73 65 72 74 28 20 28 70 54 6f 73 2d 3e   assert( (pTos->
142f3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29  flags & MEM_Dyn)
142f4 3d 3d 30 20 29 3b 0a 20 20 20 20 70 54 6f 73 2d  ==0 );.    pTos-
142f5 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  -;.    assert( p
142f6 32 3e 3d 32 20 29 3b 0a 20 20 7d 0a 20 20 61 73  2>=2 );.  }.  as
142f7 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20  sert( i>=0 );.  
142f8 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43  pCur = allocateC
142f9 75 72 73 6f 72 28 70 2c 20 69 2c 20 69 44 62 29  ursor(p, i, iDb)
142fa 3b 0a 20 20 69 66 28 20 70 43 75 72 3d 3d 30 20  ;.  if( pCur==0 
142fb 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
142fc 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d   pCur->nullRow =
142fd 20 31 3b 0a 20 20 69 66 28 20 70 58 3d 3d 30 20   1;.  if( pX==0 
142fe 29 20 62 72 65 61 6b 3b 0a 20 20 2f 2a 20 57 65  ) break;.  /* We
142ff 20 61 6c 77 61 79 73 20 70 72 6f 76 69 64 65 20   always provide 
14300 61 20 6b 65 79 20 63 6f 6d 70 61 72 69 73 6f 6e  a key comparison
14301 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 66 20 74   function.  If t
14302 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20  he table being. 
14303 20 2a 2a 20 6f 70 65 6e 65 64 20 69 73 20 6f 66   ** opened is of
14304 20 74 79 70 65 20 49 4e 54 4b 45 59 2c 20 74 68   type INTKEY, th
14305 65 20 63 6f 6d 70 61 72 69 73 69 6f 6e 20 66 75  e comparision fu
14306 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 69  nction will be i
14307 67 6e 6f 72 65 64 2e 20 2a 2f 0a 20 20 72 63 20  gnored. */.  rc 
14308 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  = sqlite3BtreeCu
14309 72 73 6f 72 28 70 58 2c 20 70 32 2c 20 77 72 46  rsor(pX, p2, wrF
1430a 6c 61 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20  lag,.           
1430b 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
1430c 64 43 6f 6d 70 61 72 65 2c 20 70 4f 70 2d 3e 70  dCompare, pOp->p
1430d 33 2c 0a 20 20 20 20 20 20 20 20 20 20 20 26 70  3,.           &p
1430e 43 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20  Cur->pCursor);. 
1430f 20 69 66 28 20 70 4f 70 2d 3e 70 33 74 79 70 65   if( pOp->p3type
14310 3d 3d 50 33 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a  ==P3_KEYINFO ){.
14311 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e      pCur->pKeyIn
14312 66 6f 20 3d 20 28 4b 65 79 49 6e 66 6f 2a 29 70  fo = (KeyInfo*)p
14313 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 70 43 75 72  Op->p3;.    pCur
14314 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 26 70 43  ->pIncrKey = &pC
14315 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 69 6e  ur->pKeyInfo->in
14316 63 72 4b 65 79 3b 0a 20 20 20 20 70 43 75 72 2d  crKey;.    pCur-
14317 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d  >pKeyInfo->enc =
14318 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 7d   ENC(p->db);.  }
14319 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 72 2d 3e  else{.    pCur->
1431a 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a 20 20  pKeyInfo = 0;.  
1431b 20 20 70 43 75 72 2d 3e 70 49 6e 63 72 4b 65 79    pCur->pIncrKey
1431c 20 3d 20 26 70 43 75 72 2d 3e 62 6f 67 75 73 49   = &pCur->bogusI
1431d 6e 63 72 4b 65 79 3b 0a 20 20 7d 0a 20 20 73 77  ncrKey;.  }.  sw
1431e 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20  itch( rc ){.    
1431f 63 61 73 65 20 53 51 4c 49 54 45 5f 42 55 53 59  case SQLITE_BUSY
14320 3a 20 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20  : {.      p->pc 
14321 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72  = pc;.      p->r
14322 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  c = rc = SQLITE_
14323 42 55 53 59 3b 0a 20 20 20 20 20 20 70 2d 3e 70  BUSY;.      p->p
14324 54 6f 73 20 3d 20 26 70 54 6f 73 5b 31 20 2b 20  Tos = &pTos[1 + 
14325 28 70 4f 70 2d 3e 70 32 3c 3d 30 29 5d 3b 20 2f  (pOp->p2<=0)]; /
14326 2a 20 4f 70 65 72 61 6e 64 73 20 6d 75 73 74 20  * Operands must 
14327 72 65 6d 61 69 6e 20 6f 6e 20 73 74 61 63 6b 20  remain on stack 
14328 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64  */.      goto vd
14329 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d  be_return;.    }
1432a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
1432b 5f 4f 4b 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  _OK: {.      int
1432c 20 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33   flags = sqlite3
1432d 42 74 72 65 65 46 6c 61 67 73 28 70 43 75 72 2d  BtreeFlags(pCur-
1432e 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >pCursor);.     
1432f 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b   /* Sanity check
14330 69 6e 67 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6c  ing.  Only the l
14331 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f  ower four bits o
14332 66 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65  f the flags byte
14333 20 73 68 6f 75 6c 64 0a 20 20 20 20 20 20 2a 2a   should.      **
14334 20 62 65 20 75 73 65 64 2e 20 20 42 69 74 20 33   be used.  Bit 3
14335 20 28 6d 61 73 6b 20 30 78 30 38 29 20 69 73 20   (mask 0x08) is 
14336 75 6e 70 72 65 64 69 74 61 62 6c 65 2e 20 20 54  unpreditable.  T
14337 68 65 20 6c 6f 77 65 72 20 33 20 62 69 74 73 0a  he lower 3 bits.
14338 20 20 20 20 20 20 2a 2a 20 28 6d 61 73 6b 20 30        ** (mask 0
14339 78 30 37 29 20 73 68 6f 75 6c 64 20 62 65 20 65  x07) should be e
1433a 69 74 68 65 72 20 35 20 28 69 6e 74 6b 65 79 2b  ither 5 (intkey+
1433b 6c 65 61 66 64 61 74 61 20 66 6f 72 20 74 61 62  leafdata for tab
1433c 6c 65 73 29 20 6f 72 0a 20 20 20 20 20 20 2a 2a  les) or.      **
1433d 20 32 20 28 7a 65 72 6f 64 61 74 61 20 66 6f 72   2 (zerodata for
1433e 20 69 6e 64 69 63 65 73 29 2e 20 20 49 66 20 74   indices).  If t
1433f 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20  hese conditions 
14340 61 72 65 20 6e 6f 74 20 6d 65 74 20 69 74 20 63  are not met it c
14341 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 6c 79  an.      ** only
14342 20 6d 65 61 6e 20 74 68 61 74 20 77 65 20 61 72   mean that we ar
14343 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61  e dealing with a
14344 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73   corrupt databas
14345 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2f 0a  e file.      */.
14346 20 20 20 20 20 20 69 66 28 20 28 66 6c 61 67 73        if( (flags
14347 20 26 20 30 78 66 30 29 21 3d 30 20 7c 7c 20 28   & 0xf0)!=0 || (
14348 28 66 6c 61 67 73 20 26 20 30 78 30 37 29 21 3d  (flags & 0x07)!=
14349 35 20 26 26 20 28 66 6c 61 67 73 20 26 20 30 78  5 && (flags & 0x
1434a 30 37 29 21 3d 32 29 20 29 7b 0a 20 20 20 20 20  07)!=2) ){.     
1434b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1434c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1434d 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1434e 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1434f 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 72      }.      pCur
14350 2d 3e 69 73 54 61 62 6c 65 20 3d 20 28 66 6c 61  ->isTable = (fla
14351 67 73 20 26 20 42 54 52 45 45 5f 49 4e 54 4b 45  gs & BTREE_INTKE
14352 59 29 21 3d 30 3b 0a 20 20 20 20 20 20 70 43 75  Y)!=0;.      pCu
14353 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20 28 66 6c  r->isIndex = (fl
14354 61 67 73 20 26 20 42 54 52 45 45 5f 5a 45 52 4f  ags & BTREE_ZERO
14355 44 41 54 41 29 21 3d 30 3b 0a 20 20 20 20 20 20  DATA)!=0;.      
14356 2f 2a 20 49 66 20 50 33 3d 3d 30 20 69 74 20 6d  /* If P3==0 it m
14357 65 61 6e 73 20 77 65 20 61 72 65 20 65 78 70 65  eans we are expe
14358 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74  cted to open a t
14359 61 62 6c 65 2e 20 20 49 66 20 50 33 21 3d 30 20  able.  If P3!=0 
1435a 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65  then.      ** we
1435b 20 65 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70   expect to be op
1435c 65 6e 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20  ening an index. 
1435d 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
1435e 77 68 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20  what happened,. 
1435f 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65       ** then the
14360 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
14361 72 75 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20  rupt.      */.  
14362 20 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69      if( (pCur->i
14363 73 54 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70  sTable && pOp->p
14364 33 74 79 70 65 3d 3d 50 33 5f 4b 45 59 49 4e 46  3type==P3_KEYINF
14365 4f 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43  O).       || (pC
14366 75 72 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70  ur->isIndex && p
14367 4f 70 2d 3e 70 33 74 79 70 65 21 3d 50 33 5f 4b  Op->p3type!=P3_K
14368 45 59 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20  EYINFO) ){.     
14369 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1436a 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1436b 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1436c 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1436d 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
1436e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1436f 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20  e SQLITE_EMPTY: 
14370 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73  {.      pCur->is
14371 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 33 74  Table = pOp->p3t
14372 79 70 65 21 3d 50 33 5f 4b 45 59 49 4e 46 4f 3b  ype!=P3_KEYINFO;
14373 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49  .      pCur->isI
14374 6e 64 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73  ndex = !pCur->is
14375 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 72 63 20  Table;.      rc 
14376 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
14377 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
14378 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
14379 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1437a 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1437b 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
1437c 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f  .}../* Opcode: O
1437d 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 50 31 20  penEphemeral P1 
1437e 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e  P2 P3.**.** Open
1437f 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 50 31   a new cursor P1
14380 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20   to a transient 
14381 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75  table..** The cu
14382 72 73 6f 72 20 69 73 20 61 6c 77 61 79 73 20 6f  rsor is always o
14383 70 65 6e 65 64 20 72 65 61 64 2f 77 72 69 74 65  pened read/write
14384 20 65 76 65 6e 20 69 66 20 0a 2a 2a 20 74 68 65   even if .** the
14385 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69   main database i
14386 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54 68  s read-only.  Th
14387 65 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 20 76  e transient or v
14388 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20  irtual.** table 
14389 69 73 20 64 65 6c 65 74 65 64 20 61 75 74 6f 6d  is deleted autom
1438a 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68  atically when th
1438b 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73  e cursor is clos
1438c 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20  ed..**.** P2 is 
1438d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
1438e 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 76 69 72  lumns in the vir
1438f 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20 54  tual table..** T
14390 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73  he cursor points
14391 20 74 6f 20 61 20 42 54 72 65 65 20 74 61 62 6c   to a BTree tabl
14392 65 20 69 66 20 50 33 3d 3d 30 20 61 6e 64 20 74  e if P3==0 and t
14393 6f 20 61 20 42 54 72 65 65 20 69 6e 64 65 78 0a  o a BTree index.
14394 2a 2a 20 69 66 20 50 33 20 69 73 20 6e 6f 74 20  ** if P3 is not 
14395 30 2e 20 20 49 66 20 50 33 20 69 73 20 6e 6f 74  0.  If P3 is not
14396 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73   NULL, it points
14397 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74   to a KeyInfo st
14398 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20  ructure.** that 
14399 64 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72 6d  defines the form
1439a 61 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74 68  at of keys in th
1439b 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54  e index..**.** T
1439c 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 6f  his opcode was o
1439d 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70 65 6e 54  nce called OpenT
1439e 65 6d 70 2e 20 20 42 75 74 20 74 68 61 74 20 63  emp.  But that c
1439f 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e 66 75 73  reated.** confus
143a0 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 20  ion because the 
143a1 74 65 72 6d 20 22 74 65 6d 70 20 74 61 62 6c 65  term "temp table
143a2 22 2c 20 6d 69 67 68 74 20 72 65 66 65 72 20 65  ", might refer e
143a3 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61 20 54 45  ither.** to a TE
143a4 4d 50 20 74 61 62 6c 65 20 61 74 20 74 68 65 20  MP table at the 
143a5 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 20 74 6f  SQL level, or to
143a6 20 61 20 74 61 62 6c 65 20 6f 70 65 6e 65 64 20   a table opened 
143a7 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f 64  by.** this opcod
143a8 65 2e 20 20 54 68 65 6e 20 74 68 69 73 20 6f 70  e.  Then this op
143a9 63 6f 64 65 20 77 61 73 20 63 61 6c 6c 20 4f 70  code was call Op
143aa 65 6e 56 69 72 74 75 61 6c 2e 20 20 42 75 74 0a  enVirtual.  But.
143ab 2a 2a 20 74 68 61 74 20 63 72 65 61 74 65 64 20  ** that created 
143ac 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 74  confusion with t
143ad 68 65 20 77 68 6f 6c 65 20 76 69 72 74 75 61 6c  he whole virtual
143ae 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a 2a 2f 0a  -table idea..*/.
143af 63 61 73 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65  case OP_OpenEphe
143b0 6d 65 72 61 6c 3a 20 7b 20 20 20 20 20 20 20 2f  meral: {       /
143b1 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69  * no-push */.  i
143b2 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
143b3 20 20 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20    Cursor *pCx;. 
143b4 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e   static const in
143b5 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 0a 20  t openFlags = . 
143b6 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
143b7 5f 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20  _READWRITE |.   
143b8 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43     SQLITE_OPEN_C
143b9 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 53 51  REATE |.      SQ
143ba 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
143bb 49 56 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  IVE |.      SQLI
143bc 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
143bd 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 53 51  CLOSE |.      SQ
143be 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49  LITE_OPEN_TRANSI
143bf 45 4e 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65 72  ENT_DB;..  asser
143c0 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78  t( i>=0 );.  pCx
143c1 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f   = allocateCurso
143c2 72 28 70 2c 20 69 2c 20 2d 31 29 3b 0a 20 20 69  r(p, i, -1);.  i
143c3 66 28 20 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f  f( pCx==0 ) goto
143c4 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 78 2d 3e   no_mem;.  pCx->
143c5 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72  nullRow = 1;.  r
143c6 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
143c7 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 31  Factory(db, 0, 1
143c8 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  , SQLITE_DEFAULT
143c9 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45  _TEMP_CACHE_SIZE
143ca 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20  , openFlags,.   
143cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
143cc 20 20 20 20 20 20 20 20 26 70 43 78 2d 3e 70 42          &pCx->pB
143cd 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
143ce 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
143cf 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
143d0 42 65 67 69 6e 54 72 61 6e 73 28 70 43 78 2d 3e  BeginTrans(pCx->
143d1 70 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69  pBt, 1);.  }.  i
143d2 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
143d3 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20   ){.    /* If a 
143d4 74 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 78 20  transient index 
143d5 69 73 20 72 65 71 75 69 72 65 64 2c 20 63 72 65  is required, cre
143d6 61 74 65 20 69 74 20 62 79 20 63 61 6c 6c 69 6e  ate it by callin
143d7 67 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  g.    ** sqlite3
143d8 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65  BtreeCreateTable
143d9 28 29 20 77 69 74 68 20 74 68 65 20 42 54 52 45  () with the BTRE
143da 45 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61 67 20  E_ZERODATA flag 
143db 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70  before.    ** op
143dc 65 6e 69 6e 67 20 69 74 2e 20 49 66 20 61 20 74  ening it. If a t
143dd 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 69  ransient table i
143de 73 20 72 65 71 75 69 72 65 64 2c 20 6a 75 73 74  s required, just
143df 20 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20   use the.    ** 
143e0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72  automatically cr
143e1 65 61 74 65 64 20 74 61 62 6c 65 20 77 69 74 68  eated table with
143e2 20 72 6f 6f 74 2d 70 61 67 65 20 31 20 28 61 6e   root-page 1 (an
143e3 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a   INTKEY table)..
143e4 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
143e5 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20  Op->p3 ){.      
143e6 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20  int pgno;.      
143e7 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 74  assert( pOp->p3t
143e8 79 70 65 3d 3d 50 33 5f 4b 45 59 49 4e 46 4f 20  ype==P3_KEYINFO 
143e9 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
143ea 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
143eb 54 61 62 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20  Table(pCx->pBt, 
143ec 26 70 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52  &pgno, BTREE_ZER
143ed 4f 44 41 54 41 29 3b 20 0a 20 20 20 20 20 20 69  ODATA); .      i
143ee 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
143ef 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
143f0 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52  rt( pgno==MASTER
143f1 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20  _ROOT+1 );.     
143f2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
143f3 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e  treeCursor(pCx->
143f4 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 20 73 71  pBt, pgno, 1, sq
143f5 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
143f6 6f 6d 70 61 72 65 2c 0a 20 20 20 20 20 20 20 20  ompare,.        
143f7 20 20 20 20 70 4f 70 2d 3e 70 33 2c 20 26 70 43      pOp->p3, &pC
143f8 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20  x->pCursor);.   
143f9 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e       pCx->pKeyIn
143fa 66 6f 20 3d 20 28 4b 65 79 49 6e 66 6f 2a 29 70  fo = (KeyInfo*)p
143fb 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 20 20 20 20  Op->p3;.        
143fc 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65  pCx->pKeyInfo->e
143fd 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b  nc = ENC(p->db);
143fe 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e 70 49  .        pCx->pI
143ff 6e 63 72 4b 65 79 20 3d 20 26 70 43 78 2d 3e 70  ncrKey = &pCx->p
14400 4b 65 79 49 6e 66 6f 2d 3e 69 6e 63 72 4b 65 79  KeyInfo->incrKey
14401 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
14402 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 30  pCx->isTable = 0
14403 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
14404 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
14405 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e  treeCursor(pCx->
14406 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  pBt, MASTER_ROOT
14407 2c 20 31 2c 20 30 2c 20 30 2c 20 26 70 43 78 2d  , 1, 0, 0, &pCx-
14408 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >pCursor);.     
14409 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20   pCx->isTable = 
1440a 31 3b 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 49  1;.      pCx->pI
1440b 6e 63 72 4b 65 79 20 3d 20 26 70 43 78 2d 3e 62  ncrKey = &pCx->b
1440c 6f 67 75 73 49 6e 63 72 4b 65 79 3b 0a 20 20 20  ogusIncrKey;.   
1440d 20 7d 0a 20 20 7d 0a 20 20 70 43 78 2d 3e 6e 46   }.  }.  pCx->nF
1440e 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  ield = pOp->p2;.
1440f 20 20 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d    pCx->isIndex =
14410 20 21 70 43 78 2d 3e 69 73 54 61 62 6c 65 3b 0a   !pCx->isTable;.
14411 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
14412 70 63 6f 64 65 3a 20 4f 70 65 6e 50 73 65 75 64  pcode: OpenPseud
14413 6f 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f  o P1 * *.**.** O
14414 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72  pen a new cursor
14415 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20   that points to 
14416 61 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61  a fake table tha
14417 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  t contains a sin
14418 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61  gle.** row of da
14419 74 61 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74  ta.  Any attempt
1441a 20 74 6f 20 77 72 69 74 65 20 61 20 73 65 63 6f   to write a seco
1441b 6e 64 20 72 6f 77 20 6f 66 20 64 61 74 61 20 63  nd row of data c
1441c 61 75 73 65 73 20 74 68 65 0a 2a 2a 20 66 69 72  auses the.** fir
1441d 73 74 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c  st row to be del
1441e 65 74 65 64 2e 20 20 41 6c 6c 20 64 61 74 61 20  eted.  All data 
1441f 69 73 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20  is deleted when 
14420 74 68 65 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a  the cursor is.**
14421 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41   closed..**.** A
14422 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 72   pseudo-table cr
14423 65 61 74 65 64 20 62 79 20 74 68 69 73 20 6f 70  eated by this op
14424 63 6f 64 65 20 69 73 20 75 73 65 66 75 6c 20 66  code is useful f
14425 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a  or holding the.*
14426 2a 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62  * NEW or OLD tab
14427 6c 65 73 20 69 6e 20 61 20 74 72 69 67 67 65 72  les in a trigger
14428 2e 20 20 41 6c 73 6f 20 75 73 65 64 20 74 6f 20  .  Also used to 
14429 68 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67 6c  hold the a singl
1442a 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74 20  e.** row output 
1442b 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20  from the sorter 
1442c 73 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77 20  so that the row 
1442d 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73 65  can be decompose
1442e 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69  d into.** indivi
1442f 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69  dual columns usi
14430 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e  ng the OP_Column
14431 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65   opcode..*/.case
14432 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20   OP_OpenPseudo: 
14433 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75  {       /* no-pu
14434 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  sh */.  int i = 
14435 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f  pOp->p1;.  Curso
14436 72 20 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74  r *pCx;.  assert
14437 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20  ( i>=0 );.  pCx 
14438 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72  = allocateCursor
14439 28 70 2c 20 69 2c 20 2d 31 29 3b 0a 20 20 69 66  (p, i, -1);.  if
1443a 28 20 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20  ( pCx==0 ) goto 
1443b 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 78 2d 3e 6e  no_mem;.  pCx->n
1443c 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 70 43  ullRow = 1;.  pC
1443d 78 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 3d  x->pseudoTable =
1443e 20 31 3b 0a 20 20 70 43 78 2d 3e 70 49 6e 63 72   1;.  pCx->pIncr
1443f 4b 65 79 20 3d 20 26 70 43 78 2d 3e 62 6f 67 75  Key = &pCx->bogu
14440 73 49 6e 63 72 4b 65 79 3b 0a 20 20 70 43 78 2d  sIncrKey;.  pCx-
14441 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20  >isTable = 1;.  
14442 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 30  pCx->isIndex = 0
14443 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
14444 20 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65 20 50   Opcode: Close P
14445 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73  1 * *.**.** Clos
14446 65 20 61 20 63 75 72 73 6f 72 20 70 72 65 76 69  e a cursor previ
14447 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73 20  ously opened as 
14448 50 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e 6f  P1.  If P1 is no
14449 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f  t.** currently o
1444a 70 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 75  pen, this instru
1444b 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
1444c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f  ..*/.case OP_Clo
1444d 73 65 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e  se: {       /* n
1444e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20  o-push */.  int 
1444f 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69  i = pOp->p1;.  i
14450 66 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  f( i>=0 && i<p->
14451 6e 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73  nCursor ){.    s
14452 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
14453 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72  rsor(p, p->apCsr
14454 5b 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43  [i]);.    p->apC
14455 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  sr[i] = 0;.  }. 
14456 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
14457 63 6f 64 65 3a 20 4d 6f 76 65 47 65 20 50 31 20  code: MoveGe P1 
14458 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74  P2 *.**.** Pop t
14459 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
1445a 61 63 6b 20 61 6e 64 20 75 73 65 20 69 74 73 20  ack and use its 
1445b 76 61 6c 75 65 20 61 73 20 61 20 6b 65 79 2e 20  value as a key. 
1445c 20 52 65 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 63   Reposition.** c
1445d 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1445e 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
1445f 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79  e smallest entry
14460 20 74 68 61 74 20 69 73 20 67 72 65 61 74 65 72   that is greater
14461 0a 2a 2a 20 74 68 61 6e 20 6f 72 20 65 71 75 61  .** than or equa
14462 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 74 68 61  l to the key tha
14463 74 20 77 61 73 20 70 6f 70 70 65 64 20 66 66 72  t was popped ffr
14464 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  om the stack..**
14465 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
14466 20 72 65 63 6f 72 64 73 20 67 72 65 61 74 65 72   records greater
14467 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
14468 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32  o the key and P2
14469 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 7a 65 72 6f   .** is not zero
1446a 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  , then jump to P
1446b 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  2..**.** See als
1446c 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75  o: Found, NotFou
1446d 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 4d 6f  nd, Distinct, Mo
1446e 76 65 4c 74 2c 20 4d 6f 76 65 47 74 2c 20 4d 6f  veLt, MoveGt, Mo
1446f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  veLe.*/./* Opcod
14470 65 3a 20 4d 6f 76 65 47 74 20 50 31 20 50 32 20  e: MoveGt P1 P2 
14471 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20  *.**.** Pop the 
14472 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
14473 20 61 6e 64 20 75 73 65 20 69 74 73 20 76 61 6c   and use its val
14474 75 65 20 61 73 20 61 20 6b 65 79 2e 20 20 52 65  ue as a key.  Re
14475 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 63 75 72 73  position.** curs
14476 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74  or P1 so that it
14477 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73   points to the s
14478 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68  mallest entry th
14479 61 74 20 69 73 20 67 72 65 61 74 65 72 0a 2a 2a  at is greater.**
1447a 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 66 72   than the key fr
1447b 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  om the stack..**
1447c 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1447d 20 72 65 63 6f 72 64 73 20 67 72 65 61 74 65 72   records greater
1447e 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 61 6e   than the key an
1447f 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f  d P2 is not zero
14480 2c 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74  ,.** then jump t
14481 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  o P2..**.** See 
14482 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74  also: Found, Not
14483 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c  Found, Distinct,
14484 20 4d 6f 76 65 4c 74 2c 20 4d 6f 76 65 47 65 2c   MoveLt, MoveGe,
14485 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70   MoveLe.*/./* Op
14486 63 6f 64 65 3a 20 4d 6f 76 65 4c 74 20 50 31 20  code: MoveLt P1 
14487 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74  P2 *.**.** Pop t
14488 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
14489 61 63 6b 20 61 6e 64 20 75 73 65 20 69 74 73 20  ack and use its 
1448a 76 61 6c 75 65 20 61 73 20 61 20 6b 65 79 2e 20  value as a key. 
1448b 20 52 65 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 63   Reposition.** c
1448c 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1448d 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
1448e 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20  e largest entry 
1448f 74 68 61 74 20 69 73 20 6c 65 73 73 0a 2a 2a 20  that is less.** 
14490 74 68 61 6e 20 74 68 65 20 6b 65 79 20 66 72 6f  than the key fro
14491 6d 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20  m the stack..** 
14492 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
14493 72 65 63 6f 72 64 73 20 6c 65 73 73 20 74 68 61  records less tha
14494 6e 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32  n the key and P2
14495 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 0a 2a 2a   is not zero,.**
14496 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
14497 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
14498 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e  : Found, NotFoun
14499 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 4d 6f 76  d, Distinct, Mov
1449a 65 47 74 2c 20 4d 6f 76 65 47 65 2c 20 4d 6f 76  eGt, MoveGe, Mov
1449b 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  eLe.*/./* Opcode
1449c 3a 20 4d 6f 76 65 4c 65 20 50 31 20 50 32 20 2a  : MoveLe P1 P2 *
1449d 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20 74  .**.** Pop the t
1449e 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
1449f 61 6e 64 20 75 73 65 20 69 74 73 20 76 61 6c 75  and use its valu
144a0 65 20 61 73 20 61 20 6b 65 79 2e 20 20 52 65 70  e as a key.  Rep
144a1 6f 73 69 74 69 6f 6e 0a 2a 2a 20 63 75 72 73 6f  osition.** curso
144a2 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20  r P1 so that it 
144a3 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61  points to the la
144a4 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 74  rgest entry that
144a5 20 69 73 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a   is less than.**
144a6 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
144a7 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 70 6f   key that was po
144a8 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74  pped from the st
144a9 61 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  ack..** If there
144aa 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
144ab 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 61  less than or eqa
144ac 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64  l to the key and
144ad 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c   P2 is not zero,
144ae 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  .** then jump to
144af 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
144b0 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46  lso: Found, NotF
144b1 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20  ound, Distinct, 
144b2 4d 6f 76 65 47 74 2c 20 4d 6f 76 65 47 65 2c 20  MoveGt, MoveGe, 
144b3 4d 6f 76 65 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f  MoveLt.*/.case O
144b4 50 5f 4d 6f 76 65 4c 74 3a 20 20 20 20 20 20 20  P_MoveLt:       
144b5 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
144b6 63 61 73 65 20 4f 50 5f 4d 6f 76 65 4c 65 3a 20  case OP_MoveLe: 
144b7 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
144b8 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f  sh */.case OP_Mo
144b9 76 65 47 65 3a 20 20 20 20 20 20 20 20 20 2f 2a  veGe:         /*
144ba 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65   no-push */.case
144bb 20 4f 50 5f 4d 6f 76 65 47 74 3a 20 7b 20 20 20   OP_MoveGt: {   
144bc 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a      /* no-push *
144bd 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
144be 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70  >p1;.  Cursor *p
144bf 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54  C;..  assert( pT
144c0 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b  os>=p->aStack );
144c1 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
144c2 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
144c3 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43  );.  pC = p->apC
144c4 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28  sr[i];.  assert(
144c5 20 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20   pC!=0 );.  if( 
144c6 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29  pC->pCursor!=0 )
144c7 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 6f  {.    int res, o
144c8 63 3b 0a 20 20 20 20 6f 63 20 3d 20 70 4f 70 2d  c;.    oc = pOp-
144c9 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 70 43 2d  >opcode;.    pC-
144ca 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20  >nullRow = 0;.  
144cb 20 20 2a 70 43 2d 3e 70 49 6e 63 72 4b 65 79 20    *pC->pIncrKey 
144cc 3d 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 47 74 20  = oc==OP_MoveGt 
144cd 7c 7c 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 4c 65  || oc==OP_MoveLe
144ce 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73  ;.    if( pC->is
144cf 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69  Table ){.      i
144d0 36 34 20 69 4b 65 79 3b 0a 20 20 20 20 20 20 73  64 iKey;.      s
144d1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
144d2 65 67 65 72 69 66 79 28 70 54 6f 73 29 3b 0a 20  egerify(pTos);. 
144d3 20 20 20 20 20 69 4b 65 79 20 3d 20 69 6e 74 54       iKey = intT
144d4 6f 4b 65 79 28 70 54 6f 73 2d 3e 75 2e 69 29 3b  oKey(pTos->u.i);
144d5 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  .      if( pOp->
144d6 70 32 3d 3d 30 20 26 26 20 70 4f 70 2d 3e 6f 70  p2==0 && pOp->op
144d7 63 6f 64 65 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20  code==OP_MoveGe 
144d8 29 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6d  ){.        pC->m
144d9 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 69 4b  ovetoTarget = iK
144da 65 79 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e  ey;.        pC->
144db 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d  deferredMoveto =
144dc 20 31 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   1;.        asse
144dd 72 74 28 20 28 70 54 6f 73 2d 3e 66 6c 61 67 73  rt( (pTos->flags
144de 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29   & MEM_Dyn)==0 )
144df 3b 0a 20 20 20 20 20 20 20 20 70 54 6f 73 2d 2d  ;.        pTos--
144e0 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
144e1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
144e2 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
144e3 4d 6f 76 65 74 6f 28 70 43 2d 3e 70 43 75 72 73  Moveto(pC->pCurs
144e4 6f 72 2c 20 30 2c 20 28 75 36 34 29 69 4b 65 79  or, 0, (u64)iKey
144e5 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  , 0, &res);.    
144e6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
144e7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
144e8 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
144e9 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a  _error;.      }.
144ea 20 20 20 20 20 20 70 43 2d 3e 6c 61 73 74 52 6f        pC->lastRo
144eb 77 69 64 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b  wid = pTos->u.i;
144ec 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64  .      pC->rowid
144ed 49 73 56 61 6c 69 64 20 3d 20 72 65 73 3d 3d 30  IsValid = res==0
144ee 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
144ef 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 2d     assert( pTos-
144f0 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
144f1 62 20 29 3b 0a 20 20 20 20 20 20 45 78 70 61 6e  b );.      Expan
144f2 64 42 6c 6f 62 28 70 54 6f 73 29 3b 0a 20 20 20  dBlob(pTos);.   
144f3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
144f4 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 2d 3e 70  treeMoveto(pC->p
144f5 43 75 72 73 6f 72 2c 20 70 54 6f 73 2d 3e 7a 2c  Cursor, pTos->z,
144f6 20 70 54 6f 73 2d 3e 6e 2c 20 30 2c 20 26 72 65   pTos->n, 0, &re
144f7 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
144f8 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
144f9 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
144fa 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
144fb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
144fc 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
144fd 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43   0;.    }.    pC
144fe 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
144ff 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61   = 0;.    pC->ca
14500 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
14501 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 2a 70 43  E_STALE;.    *pC
14502 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 30 3b 0a  ->pIncrKey = 0;.
14503 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
14504 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ST.    sqlite3_s
14505 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  earch_count++;.#
14506 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 6f 63  endif.    if( oc
14507 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20 7c 7c 20 6f  ==OP_MoveGe || o
14508 63 3d 3d 4f 50 5f 4d 6f 76 65 47 74 20 29 7b 0a  c==OP_MoveGt ){.
14509 20 20 20 20 20 20 69 66 28 20 72 65 73 3c 30 20        if( res<0 
1450a 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1450b 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
1450c 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72  (pC->pCursor, &r
1450d 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  es);.        if(
1450e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1450f 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
14510 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
14511 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c    pC->rowidIsVal
14512 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  id = 0;.      }e
14513 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 65 73  lse{.        res
14514 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
14515 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
14516 73 73 65 72 74 28 20 6f 63 3d 3d 4f 50 5f 4d 6f  ssert( oc==OP_Mo
14517 76 65 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 4d  veLt || oc==OP_M
14518 6f 76 65 4c 65 20 29 3b 0a 20 20 20 20 20 20 69  oveLe );.      i
14519 66 28 20 72 65 73 3e 3d 30 20 29 7b 0a 20 20 20  f( res>=0 ){.   
1451a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1451b 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70  3BtreePrevious(p
1451c 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73  C->pCursor, &res
1451d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1451e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
1451f 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
14520 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
14521 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
14522 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
14523 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 72 65  e{.        /* re
14524 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67 61 74  s might be negat
14525 69 76 65 20 62 65 63 61 75 73 65 20 74 68 65 20  ive because the 
14526 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20  table is empty. 
14527 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 20 20   Check to.      
14528 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68 69 73    ** see if this
14529 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20   is the case..  
1452a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1452b 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33 42 74   res = sqlite3Bt
1452c 72 65 65 45 6f 66 28 70 43 2d 3e 70 43 75 72 73  reeEof(pC->pCurs
1452d 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  or);.      }.   
1452e 20 7d 0a 20 20 20 20 69 66 28 20 72 65 73 20 29   }.    if( res )
1452f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  {.      if( pOp-
14530 3e 70 32 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  >p2>0 ){.       
14531 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
14532 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
14533 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 75 6c 6c          pC->null
14534 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  Row = 1;.      }
14535 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 52 65 6c  .    }.  }.  Rel
14536 65 61 73 65 28 70 54 6f 73 29 3b 0a 20 20 70 54  ease(pTos);.  pT
14537 6f 73 2d 2d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  os--;.  break;.}
14538 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 73  ../* Opcode: Dis
14539 74 69 6e 63 74 20 50 31 20 50 32 20 2a 0a 2a 2a  tinct P1 P2 *.**
1453a 0a 2a 2a 20 55 73 65 20 74 68 65 20 74 6f 70 20  .** Use the top 
1453b 6f 66 20 74 68 65 20 73 74 61 63 6b 20 61 73 20  of the stack as 
1453c 61 20 72 65 63 6f 72 64 20 63 72 65 61 74 65 64  a record created
1453d 20 75 73 69 6e 67 20 4d 61 6b 65 52 65 63 6f 72   using MakeRecor
1453e 64 2e 20 20 50 31 20 69 73 20 61 0a 2a 2a 20 63  d.  P1 is a.** c
1453f 75 72 73 6f 72 20 6f 6e 20 61 20 74 61 62 6c 65  ursor on a table
14540 20 74 68 61 74 20 64 65 63 6c 61 72 65 64 20 61   that declared a
14541 73 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20  s an index.  If 
14542 74 68 61 74 20 74 61 62 6c 65 20 63 6f 6e 74 61  that table conta
14543 69 6e 73 20 61 6e 0a 2a 2a 20 65 6e 74 72 79 20  ins an.** entry 
14544 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65  that matches the
14545 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63   top of the stac
14546 6b 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 49 66  k fall thru.  If
14547 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
14548 73 74 61 63 6b 0a 2a 2a 20 6d 61 74 63 68 65 73  stack.** matches
14549 20 6e 6f 20 65 6e 74 72 79 20 69 6e 20 50 31 20   no entry in P1 
1454a 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1454b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  .**.** The curso
1454c 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  r is left pointi
1454d 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 69  ng at the matchi
1454e 6e 67 20 65 6e 74 72 79 20 69 66 20 69 74 20 65  ng entry if it e
1454f 78 69 73 74 73 2e 20 20 54 68 65 0a 2a 2a 20 72  xists.  The.** r
14550 65 63 6f 72 64 20 6f 6e 20 74 68 65 20 74 6f 70  ecord on the top
14551 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69 73   of the stack is
14552 20 6e 6f 74 20 70 6f 70 70 65 64 2e 0a 2a 2a 0a   not popped..**.
14553 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
14554 69 6f 6e 20 69 73 20 73 69 6d 69 6c 61 72 20 74  ion is similar t
14555 6f 20 4e 6f 74 46 6f 75 6e 64 20 65 78 63 65 70  o NotFound excep
14556 74 20 74 68 61 74 20 74 68 69 73 20 6f 70 65 72  t that this oper
14557 61 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f  ation.** does no
14558 74 20 70 6f 70 20 74 68 65 20 6b 65 79 20 66 72  t pop the key fr
14559 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  om the stack..**
1455a 0a 2a 2a 20 54 68 65 20 69 6e 73 74 72 75 63 74  .** The instruct
1455b 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 69  ion is used to i
1455c 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 44 49 53  mplement the DIS
1455d 54 49 4e 43 54 20 6f 70 65 72 61 74 6f 72 20 6f  TINCT operator o
1455e 6e 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74  n SELECT.** stat
1455f 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 50 31 20  ements.  The P1 
14560 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 74  table is not a t
14561 72 75 65 20 69 6e 64 65 78 20 62 75 74 20 72 61  rue index but ra
14562 74 68 65 72 20 61 20 72 65 63 6f 72 64 20 6f 66  ther a record of
14563 0a 2a 2a 20 61 6c 6c 20 72 65 73 75 6c 74 73 20  .** all results 
14564 74 68 61 74 20 68 61 76 65 20 70 72 6f 64 75 63  that have produc
14565 65 64 20 73 6f 20 66 61 72 2e 20 20 0a 2a 2a 0a  ed so far.  .**.
14566 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75  ** See also: Fou
14567 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 4d 6f  nd, NotFound, Mo
14568 76 65 54 6f 2c 20 49 73 55 6e 69 71 75 65 2c 20  veTo, IsUnique, 
14569 4e 6f 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20  NotExists.*/./* 
1456a 4f 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31  Opcode: Found P1
1456b 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 54 6f 70 20   P2 *.**.** Top 
1456c 6f 66 20 74 68 65 20 73 74 61 63 6b 20 68 6f 6c  of the stack hol
1456d 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72  ds a blob constr
1456e 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63  ucted by MakeRec
1456f 6f 72 64 2e 20 20 50 31 20 69 73 20 61 6e 20 69  ord.  P1 is an i
14570 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65  ndex..** If an e
14571 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65  ntry that matche
14572 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  s the top of the
14573 20 73 74 61 63 6b 20 65 78 69 73 74 73 20 69 6e   stack exists in
14574 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70   P1 then.** jump
14575 20 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65 20   to P2.  If the 
14576 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
14577 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
14578 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31 0a  any entry in P1.
14579 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  ** then fall thr
1457a 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73 6f  u.  The P1 curso
1457b 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  r is left pointi
1457c 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 69  ng at the matchi
1457d 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20 69  ng entry.** if i
1457e 74 20 65 78 69 73 74 73 2e 20 20 54 68 65 20 62  t exists.  The b
1457f 6c 6f 62 20 69 73 20 70 6f 70 70 65 64 20 6f 66  lob is popped of
14580 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  f the top of the
14581 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68   stack..**.** Th
14582 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
14583 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  s used to implem
14584 65 6e 74 20 74 68 65 20 49 4e 20 6f 70 65 72 61  ent the IN opera
14585 74 6f 72 20 77 68 65 72 65 20 74 68 65 0a 2a 2a  tor where the.**
14586 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
14587 69 73 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  is a SELECT stat
14588 65 6d 65 6e 74 2e 20 20 50 31 20 69 73 20 6e 6f  ement.  P1 is no
14589 74 20 61 20 74 72 75 65 20 69 6e 64 65 78 20 62  t a true index b
1458a 75 74 0a 2a 2a 20 69 73 20 69 6e 73 74 65 61 64  ut.** is instead
1458b 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69 6e 64   a temporary ind
1458c 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  ex that holds th
1458d 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
1458e 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65   SELECT.** state
1458f 6d 65 6e 74 2e 20 20 54 68 69 73 20 69 6e 73 74  ment.  This inst
14590 72 75 63 74 69 6f 6e 20 6a 75 73 74 20 63 68 65  ruction just che
14591 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74 68  cks to see if th
14592 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  e left-hand side
14593 0a 2a 2a 20 6f 66 20 74 68 65 20 49 4e 20 6f 70  .** of the IN op
14594 65 72 61 74 6f 72 20 28 73 74 6f 72 65 64 20 6f  erator (stored o
14595 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
14596 20 73 74 61 63 6b 29 20 65 78 69 73 74 73 20 69   stack) exists i
14597 6e 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20  n the.** result 
14598 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  of the SELECT st
14599 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 53  atement..**.** S
1459a 65 65 20 61 6c 73 6f 3a 20 44 69 73 74 69 6e 63  ee also: Distinc
1459b 74 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 4d 6f 76  t, NotFound, Mov
1459c 65 54 6f 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e  eTo, IsUnique, N
1459d 6f 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f  otExists.*/./* O
1459e 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20  pcode: NotFound 
1459f 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  P1 P2 *.**.** Th
145a0 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
145a1 63 6b 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20  ck holds a blob 
145a2 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d  constructed by M
145a3 61 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69  akeRecord.  P1 i
145a4 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20  s.** an index.  
145a5 49 66 20 6e 6f 20 65 6e 74 72 79 20 65 78 69 73  If no entry exis
145a6 74 73 20 69 6e 20 50 31 20 74 68 61 74 20 6d 61  ts in P1 that ma
145a7 74 63 68 65 73 20 74 68 65 20 62 6c 6f 62 20 74  tches the blob t
145a8 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50  hen jump.** to P
145a9 32 2e 20 20 49 66 20 61 6e 20 65 6e 74 72 79 20  2.  If an entry 
145aa 64 6f 65 73 20 65 78 69 73 74 69 6e 67 2c 20 66  does existing, f
145ab 61 6c 6c 20 74 68 72 6f 75 67 68 2e 20 20 54 68  all through.  Th
145ac 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
145ad 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  .** pointing to 
145ae 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 6d  the entry that m
145af 61 74 63 68 65 73 2e 20 20 54 68 65 20 62 6c 6f  atches.  The blo
145b0 62 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d  b is popped from
145b1 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a   the stack..**.*
145b2 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  * The difference
145b3 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70   between this op
145b4 65 72 61 74 69 6f 6e 20 61 6e 64 20 44 69 73 74  eration and Dist
145b5 69 6e 63 74 20 69 73 20 74 68 61 74 0a 2a 2a 20  inct is that.** 
145b6 44 69 73 74 69 6e 63 74 20 64 6f 65 73 20 6e 6f  Distinct does no
145b7 74 20 70 6f 70 20 74 68 65 20 6b 65 79 20 66 72  t pop the key fr
145b8 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  om the stack..**
145b9 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 69  .** See also: Di
145ba 73 74 69 6e 63 74 2c 20 46 6f 75 6e 64 2c 20 4d  stinct, Found, M
145bb 6f 76 65 54 6f 2c 20 4e 6f 74 45 78 69 73 74 73  oveTo, NotExists
145bc 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61  , IsUnique.*/.ca
145bd 73 65 20 4f 50 5f 44 69 73 74 69 6e 63 74 3a 20  se OP_Distinct: 
145be 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
145bf 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46   */.case OP_NotF
145c0 6f 75 6e 64 3a 20 20 20 20 20 20 20 2f 2a 20 6e  ound:       /* n
145c1 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f  o-push */.case O
145c2 50 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20  P_Found: {      
145c3 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
145c4 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
145c5 31 3b 0a 20 20 69 6e 74 20 61 6c 72 65 61 64 79  1;.  int already
145c6 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 43 75  Exists = 0;.  Cu
145c7 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
145c8 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74  rt( pTos>=p->aSt
145c9 61 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ack );.  assert(
145ca 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
145cb 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72  ursor );.  asser
145cc 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d  t( p->apCsr[i]!=
145cd 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 20 3d  0 );.  if( (pC =
145ce 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70   p->apCsr[i])->p
145cf 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20  Cursor!=0 ){.   
145d0 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 61 73   int res;.    as
145d1 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c  sert( pC->isTabl
145d2 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  e==0 );.    asse
145d3 72 74 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20  rt( pTos->flags 
145d4 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20  & MEM_Blob );.  
145d5 20 20 53 74 72 69 6e 67 69 66 79 28 70 54 6f 73    Stringify(pTos
145d6 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20  , encoding);.   
145d7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
145d8 65 65 4d 6f 76 65 74 6f 28 70 43 2d 3e 70 43 75  eeMoveto(pC->pCu
145d9 72 73 6f 72 2c 20 70 54 6f 73 2d 3e 7a 2c 20 70  rsor, pTos->z, p
145da 54 6f 73 2d 3e 6e 2c 20 30 2c 20 26 72 65 73 29  Tos->n, 0, &res)
145db 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
145dc 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
145dd 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
145de 20 20 61 6c 72 65 61 64 79 45 78 69 73 74 73 20    alreadyExists 
145df 3d 20 28 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20  = (res==0);.    
145e0 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
145e1 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e  to = 0;.    pC->
145e2 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
145e3 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20  CHE_STALE;.  }. 
145e4 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
145e5 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20  ==OP_Found ){.  
145e6 20 20 69 66 28 20 61 6c 72 65 61 64 79 45 78 69    if( alreadyExi
145e7 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e  sts ) pc = pOp->
145e8 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  p2 - 1;.  }else{
145e9 0a 20 20 20 20 69 66 28 20 21 61 6c 72 65 61 64  .    if( !alread
145ea 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20 70  yExists ) pc = p
145eb 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
145ec 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
145ed 65 21 3d 4f 50 5f 44 69 73 74 69 6e 63 74 20 29  e!=OP_Distinct )
145ee 7b 0a 20 20 20 20 52 65 6c 65 61 73 65 28 70 54  {.    Release(pT
145ef 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d 2d 3b  os);.    pTos--;
145f0 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
145f1 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 6e  ./* Opcode: IsUn
145f2 69 71 75 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a  ique P1 P2 *.**.
145f3 2a 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68  ** The top of th
145f4 65 20 73 74 61 63 6b 20 69 73 20 61 6e 20 69 6e  e stack is an in
145f5 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d  teger record num
145f6 62 65 72 2e 20 20 43 61 6c 6c 20 74 68 69 73 0a  ber.  Call this.
145f7 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ** record number
145f8 20 52 2e 20 20 54 68 65 20 6e 65 78 74 20 6f 6e   R.  The next on
145f9 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 61 6e   the stack is an
145fa 20 69 6e 64 65 78 20 6b 65 79 20 63 72 65 61 74   index key creat
145fb 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4d 61 6b 65  ed.** using Make
145fc 49 64 78 52 65 63 2e 20 20 43 61 6c 6c 20 69 74  IdxRec.  Call it
145fd 20 4b 2e 20 20 54 68 69 73 20 69 6e 73 74 72 75   K.  This instru
145fe 63 74 69 6f 6e 20 70 6f 70 73 20 52 20 66 72 6f  ction pops R fro
145ff 6d 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 62  m the.** stack b
14600 75 74 20 69 74 20 6c 65 61 76 65 73 20 4b 20 75  ut it leaves K u
14601 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20  nchanged..**.** 
14602 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 20  P1 is an index. 
14603 20 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64 61   So it has no da
14604 74 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20 63  ta and its key c
14605 6f 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20  onsists of a.** 
14606 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 65 64  record generated
14607 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72   by OP_MakeRecor
14608 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 73 74  d where the last
14609 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 0a 2a   field is the .*
1460a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 65  * rowid of the e
1460b 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 69 6e  ntry that the in
1460c 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a  dex refers to..*
1460d 2a 20 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72  * .** This instr
1460e 75 63 74 69 6f 6e 20 61 73 6b 73 20 69 66 20 74  uction asks if t
1460f 68 65 72 65 20 69 73 20 61 6e 20 65 6e 74 72 79  here is an entry
14610 20 69 6e 20 50 31 20 77 68 65 72 65 20 74 68 65   in P1 where the
14611 0a 2a 2a 20 66 69 65 6c 64 73 20 6d 61 74 63 68  .** fields match
14612 65 73 20 4b 20 62 75 74 20 74 68 65 20 72 6f 77  es K but the row
14613 69 64 20 69 73 20 64 69 66 66 65 72 65 6e 74 20  id is different 
14614 66 72 6f 6d 20 52 2e 0a 2a 2a 20 49 66 20 74 68  from R..** If th
14615 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 65  ere is no such e
14616 6e 74 72 79 2c 20 74 68 65 6e 20 74 68 65 72 65  ntry, then there
14617 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65   is an immediate
14618 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20  .** jump to P2. 
14619 20 49 66 20 61 6e 79 20 65 6e 74 72 79 20 64 6f   If any entry do
1461a 65 73 20 65 78 69 73 74 20 77 68 65 72 65 20 74  es exist where t
1461b 68 65 20 69 6e 64 65 78 20 73 74 72 69 6e 67 0a  he index string.
1461c 2a 2a 20 6d 61 74 63 68 65 73 20 4b 20 62 75 74  ** matches K but
1461d 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
1461e 65 72 20 69 73 20 6e 6f 74 20 52 2c 20 74 68 65  er is not R, the
1461f 6e 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20  n the record.** 
14620 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20  number for that 
14621 65 6e 74 72 79 20 69 73 20 70 75 73 68 65 64 20  entry is pushed 
14622 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61  onto the stack a
14623 6e 64 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 66 61  nd control.** fa
14624 6c 6c 73 20 74 68 72 6f 75 67 68 20 74 6f 20 74  lls through to t
14625 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
14626 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ion..**.** See a
14627 6c 73 6f 3a 20 44 69 73 74 69 6e 63 74 2c 20 4e  lso: Distinct, N
14628 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73  otFound, NotExis
14629 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73  ts, Found.*/.cas
1462a 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b  e OP_IsUnique: {
1462b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
1462c 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  sh */.  int i = 
1462d 70 4f 70 2d 3e 70 31 3b 0a 20 20 4d 65 6d 20 2a  pOp->p1;.  Mem *
1462e 70 4e 6f 73 20 3d 20 26 70 54 6f 73 5b 2d 31 5d  pNos = &pTos[-1]
1462f 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 78 3b  ;.  Cursor *pCx;
14630 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
14631 73 72 3b 0a 20 20 69 36 34 20 52 3b 0a 0a 20 20  sr;.  i64 R;..  
14632 2f 2a 20 50 6f 70 20 74 68 65 20 76 61 6c 75 65  /* Pop the value
14633 20 52 20 6f 66 66 20 74 68 65 20 74 6f 70 20 6f   R off the top o
14634 66 20 74 68 65 20 73 74 61 63 6b 0a 20 20 2a 2f  f the stack.  */
14635 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 73 3e  .  assert( pNos>
14636 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20  =p->aStack );.  
14637 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
14638 74 65 67 65 72 69 66 79 28 70 54 6f 73 29 3b 0a  tegerify(pTos);.
14639 20 20 52 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b    R = pTos->u.i;
1463a 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 6f 73  .  assert( (pTos
1463b 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79  ->flags & MEM_Dy
1463c 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 54 6f 73 2d  n)==0 );.  pTos-
1463d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  -;.  assert( i>=
1463e 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
1463f 72 20 29 3b 0a 20 20 70 43 78 20 3d 20 70 2d 3e  r );.  pCx = p->
14640 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65  apCsr[i];.  asse
14641 72 74 28 20 70 43 78 21 3d 30 20 29 3b 0a 20 20  rt( pCx!=0 );.  
14642 70 43 72 73 72 20 3d 20 70 43 78 2d 3e 70 43 75  pCrsr = pCx->pCu
14643 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 72 73  rsor;.  if( pCrs
14644 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  r!=0 ){.    int 
14645 72 65 73 3b 0a 20 20 20 20 69 36 34 20 76 3b 20  res;.    i64 v; 
14646 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
14647 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 6e 20  ecord number on 
14648 74 68 65 20 50 31 20 65 6e 74 72 79 20 74 68 61  the P1 entry tha
14649 74 20 6d 61 74 63 68 65 73 20 4b 20 2a 2f 0a 20  t matches K */. 
1464a 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 20 20     char *zKey;  
1464b 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f    /* The value o
1464c 66 20 4b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  f K */.    int n
1464d 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  Key;      /* Num
1464e 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1464f 4b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e  K */.    int len
14650 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
14651 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 4b 20  r of bytes in K 
14652 77 69 74 68 6f 75 74 20 74 68 65 20 72 6f 77 69  without the rowi
14653 64 20 61 74 20 74 68 65 20 65 6e 64 20 2a 2f 0a  d at the end */.
14654 20 20 20 20 69 6e 74 20 73 7a 52 6f 77 69 64 3b      int szRowid;
14655 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
14656 65 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 61  e rowid column a
14657 74 20 74 68 65 20 65 6e 64 20 6f 66 20 7a 4b 65  t the end of zKe
14658 79 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b  y */..    /* Mak
14659 65 20 73 75 72 65 20 4b 20 69 73 20 61 20 73 74  e sure K is a st
1465a 72 69 6e 67 20 61 6e 64 20 6d 61 6b 65 20 7a 4b  ring and make zK
1465b 65 79 20 70 6f 69 6e 74 20 74 6f 20 4b 0a 20 20  ey point to K.  
1465c 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
1465d 20 70 4e 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d   pNos->flags & M
1465e 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 53  EM_Blob );.    S
1465f 74 72 69 6e 67 69 66 79 28 70 4e 6f 73 2c 20 65  tringify(pNos, e
14660 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 7a 4b  ncoding);.    zK
14661 65 79 20 3d 20 70 4e 6f 73 2d 3e 7a 3b 0a 20 20  ey = pNos->z;.  
14662 20 20 6e 4b 65 79 20 3d 20 70 4e 6f 73 2d 3e 6e    nKey = pNos->n
14663 3b 0a 0a 20 20 20 20 73 7a 52 6f 77 69 64 20 3d  ;..    szRowid =
14664 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52   sqlite3VdbeIdxR
14665 6f 77 69 64 4c 65 6e 28 28 75 38 2a 29 7a 4b 65  owidLen((u8*)zKe
14666 79 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 6e 4b  y);.    len = nK
14667 65 79 2d 73 7a 52 6f 77 69 64 3b 0a 0a 20 20 20  ey-szRowid;..   
14668 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61   /* Search for a
14669 6e 20 65 6e 74 72 79 20 69 6e 20 50 31 20 77 68  n entry in P1 wh
1466a 65 72 65 20 61 6c 6c 20 62 75 74 20 74 68 65 20  ere all but the 
1466b 6c 61 73 74 20 66 6f 75 72 20 62 79 74 65 73 20  last four bytes 
1466c 6d 61 74 63 68 20 4b 2e 0a 20 20 20 20 2a 2a 20  match K..    ** 
1466d 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73  If there is no s
1466e 75 63 68 20 65 6e 74 72 79 2c 20 6a 75 6d 70 20  uch entry, jump 
1466f 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
14670 32 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  2..    */.    as
14671 73 65 72 74 28 20 70 43 78 2d 3e 64 65 66 65 72  sert( pCx->defer
14672 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a  redMoveto==0 );.
14673 20 20 20 20 70 43 78 2d 3e 63 61 63 68 65 53 74      pCx->cacheSt
14674 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
14675 4c 45 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  LE;.    rc = sql
14676 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28  ite3BtreeMoveto(
14677 70 43 72 73 72 2c 20 7a 4b 65 79 2c 20 6c 65 6e  pCrsr, zKey, len
14678 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  , 0, &res);.    
14679 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1467a 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
1467b 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1467c 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  or;.    }.    if
1467d 28 20 72 65 73 3c 30 20 29 7b 0a 20 20 20 20 20  ( res<0 ){.     
1467e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1467f 65 65 4e 65 78 74 28 70 43 72 73 72 2c 20 26 72  eeNext(pCrsr, &r
14680 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  es);.      if( r
14681 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63  es ){.        pc
14682 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
14683 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
14684 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
14685 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
14686 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 70  eIdxKeyCompare(p
14687 43 78 2c 20 6c 65 6e 2c 20 28 75 38 2a 29 7a 4b  Cx, len, (u8*)zK
14688 65 79 2c 20 26 72 65 73 29 3b 20 0a 20 20 20 20  ey, &res); .    
14689 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1468a 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  K ) goto abort_d
1468b 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1468c 20 69 66 28 20 72 65 73 3e 30 20 29 7b 0a 20 20   if( res>0 ){.  
1468d 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1468e 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61   - 1;.      brea
1468f 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
14690 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
14691 70 43 72 73 72 20 69 73 20 70 6f 69 6e 74 69 6e  pCrsr is pointin
14692 67 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 69 6e  g to an entry in
14693 20 50 31 20 77 68 65 72 65 20 61 6c 6c 20 62 75   P1 where all bu
14694 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6e  t.    ** the fin
14695 61 6c 20 65 6e 74 72 79 20 28 74 68 65 20 72 6f  al entry (the ro
14696 77 69 64 29 20 6d 61 74 63 68 65 73 20 4b 2e 20  wid) matches K. 
14697 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
14698 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 6e 61   the.    ** fina
14699 6c 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69  l rowid column i
1469a 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
1469b 20 52 2e 20 20 49 66 20 69 74 20 65 71 75 61 6c   R.  If it equal
1469c 73 20 52 20 74 68 65 6e 20 6a 75 6d 70 0a 20 20  s R then jump.  
1469d 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79    ** immediately
1469e 20 74 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20   to P2..    */. 
1469f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
146a0 64 62 65 49 64 78 52 6f 77 69 64 28 70 43 72 73  dbeIdxRowid(pCrs
146a1 72 2c 20 26 76 29 3b 0a 20 20 20 20 69 66 28 20  r, &v);.    if( 
146a2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
146a3 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
146a4 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
146a5 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 76 3d      }.    if( v=
146a6 3d 52 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  =R ){.      pc =
146a7 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
146a8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
146a9 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6e  ..    /* The fin
146aa 61 6c 20 76 61 72 69 6e 74 20 6f 66 20 74 68 65  al varint of the
146ab 20 6b 65 79 20 69 73 20 64 69 66 66 65 72 65 6e   key is differen
146ac 74 20 66 72 6f 6d 20 52 2e 20 20 50 75 73 68 20  t from R.  Push 
146ad 69 74 20 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 74  it onto.    ** t
146ae 68 65 20 73 74 61 63 6b 2e 20 20 28 54 68 65 20  he stack.  (The 
146af 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66  record number of
146b0 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 76   an entry that v
146b1 69 6f 6c 61 74 65 73 20 61 20 55 4e 49 51 55 45  iolates a UNIQUE
146b2 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69  .    ** constrai
146b3 6e 74 2e 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20  nt.).    */.    
146b4 70 54 6f 73 2b 2b 3b 0a 20 20 20 20 70 54 6f 73  pTos++;.    pTos
146b5 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20 70  ->u.i = v;.    p
146b6 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Tos->flags = MEM
146b7 5f 49 6e 74 3b 0a 20 20 7d 0a 20 20 62 72 65 61  _Int;.  }.  brea
146b8 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
146b9 20 4e 6f 74 45 78 69 73 74 73 20 50 31 20 50 32   NotExists P1 P2
146ba 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65   *.**.** Use the
146bb 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63   top of the stac
146bc 6b 20 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b  k as a integer k
146bd 65 79 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64  ey.  If a record
146be 20 77 69 74 68 20 74 68 61 74 20 6b 65 79 0a 2a   with that key.*
146bf 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  * does not exist
146c0 20 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c   in table of P1,
146c1 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
146c2 2e 20 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  .  If the record
146c3 0a 2a 2a 20 64 6f 65 73 20 65 78 69 73 74 2c 20  .** does exist, 
146c4 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20  then fall thru. 
146c5 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c   The cursor is l
146c6 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  eft pointing to 
146c7 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 66  the.** record if
146c8 20 69 74 20 65 78 69 73 74 73 2e 20 20 54 68 65   it exists.  The
146c9 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69 73 20   integer key is 
146ca 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20  popped from the 
146cb 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  stack..**.** The
146cc 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
146cd 65 65 6e 20 74 68 69 73 20 6f 70 65 72 61 74 69  een this operati
146ce 6f 6e 20 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20  on and NotFound 
146cf 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20  is that this.** 
146d0 6f 70 65 72 61 74 69 6f 6e 20 61 73 73 75 6d 65  operation assume
146d1 73 20 74 68 65 20 6b 65 79 20 69 73 20 61 6e 20  s the key is an 
146d2 69 6e 74 65 67 65 72 20 61 6e 64 20 74 68 61 74  integer and that
146d3 20 50 31 20 69 73 20 61 20 74 61 62 6c 65 20 77   P1 is a table w
146d4 68 65 72 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75  hereas.** NotFou
146d5 6e 64 20 61 73 73 75 6d 65 73 20 6b 65 79 20 69  nd assumes key i
146d6 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75  s a blob constru
146d7 63 74 65 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65  cted from MakeRe
146d8 63 6f 72 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69  cord and.** P1 i
146d9 73 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  s an index..**.*
146da 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 69 73 74  * See also: Dist
146db 69 6e 63 74 2c 20 46 6f 75 6e 64 2c 20 4d 6f 76  inct, Found, Mov
146dc 65 54 6f 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 49  eTo, NotFound, I
146dd 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20  sUnique.*/.case 
146de 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a 20 7b 20  OP_NotExists: { 
146df 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73         /* no-pus
146e0 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  h */.  int i = p
146e1 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72  Op->p1;.  Cursor
146e2 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
146e3 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72   *pCrsr;.  asser
146e4 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61  t( pTos>=p->aSta
146e5 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ck );.  assert( 
146e6 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
146e7 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74  rsor );.  assert
146e8 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30  ( p->apCsr[i]!=0
146e9 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72   );.  if( (pCrsr
146ea 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73   = (pC = p->apCs
146eb 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21  r[i])->pCursor)!
146ec 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  =0 ){.    int re
146ed 73 3b 0a 20 20 20 20 75 36 34 20 69 4b 65 79 3b  s;.    u64 iKey;
146ee 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f  .    assert( pTo
146ef 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  s->flags & MEM_I
146f0 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nt );.    assert
146f1 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d 3e 69  ( p->apCsr[i]->i
146f2 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 69 4b  sTable );.    iK
146f3 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70 54  ey = intToKey(pT
146f4 6f 73 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 72 63  os->u.i);.    rc
146f5 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
146f6 6f 76 65 74 6f 28 70 43 72 73 72 2c 20 30 2c 20  oveto(pCrsr, 0, 
146f7 69 4b 65 79 2c 20 30 2c 26 72 65 73 29 3b 0a 20  iKey, 0,&res);. 
146f8 20 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64     pC->lastRowid
146f9 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b 0a 20 20   = pTos->u.i;.  
146fa 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c    pC->rowidIsVal
146fb 69 64 20 3d 20 72 65 73 3d 3d 30 3b 0a 20 20 20  id = res==0;.   
146fc 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30   pC->nullRow = 0
146fd 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53  ;.    pC->cacheS
146fe 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
146ff 41 4c 45 3b 0a 20 20 20 20 2f 2a 20 72 65 73 20  ALE;.    /* res 
14700 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74 69  might be uniniti
14701 61 6c 69 7a 65 64 20 69 66 20 72 63 21 3d 53 51  alized if rc!=SQ
14702 4c 49 54 45 5f 4f 4b 2e 20 20 42 75 74 20 69 66  LITE_OK.  But if
14703 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20   rc!=SQLITE_OK. 
14704 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67     ** processing
14705 20 69 73 20 61 62 6f 75 74 20 74 6f 20 61 62 6f   is about to abo
14706 72 74 20 73 6f 20 77 65 20 72 65 61 6c 6c 79 20  rt so we really 
14707 64 6f 20 6e 6f 74 20 63 61 72 65 20 77 68 65 74  do not care whet
14708 68 65 72 20 6f 72 20 6e 6f 74 0a 20 20 20 20 2a  her or not.    *
14709 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * the following 
1470a 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 2e 20 20  jump is taken.  
1470b 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
1470c 20 64 6f 20 6e 6f 74 20 73 74 72 65 73 73 20 6f   do not stress o
1470d 76 65 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 65  ver.    ** the e
1470e 72 72 6f 72 20 74 68 61 74 20 76 61 6c 67 72 69  rror that valgri
1470f 6e 64 20 73 6f 6d 65 74 69 6d 65 73 20 73 68 6f  nd sometimes sho
14710 77 73 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 73  ws on the next s
14711 74 61 74 65 6d 65 6e 74 20 77 68 65 6e 0a 20 20  tatement when.  
14712 20 20 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 6f 65    ** running ioe
14713 72 72 2e 74 65 73 74 20 61 6e 64 20 73 69 6d 69  rr.test and simi
14714 6c 61 72 20 66 61 69 6c 75 72 65 2d 72 65 63 6f  lar failure-reco
14715 76 65 72 79 20 74 65 73 74 20 73 63 72 69 70 74  very test script
14716 73 2e 29 20 2a 2f 0a 20 20 20 20 69 66 28 20 72  s.) */.    if( r
14717 65 73 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  es!=0 ){.      p
14718 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
14719 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64  .      pC->rowid
1471a 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1471b 20 7d 0a 20 20 7d 0a 20 20 52 65 6c 65 61 73 65   }.  }.  Release
1471c 28 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 2d  (pTos);.  pTos--
1471d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1471e 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65 6e 63   Opcode: Sequenc
1471f 65 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50  e P1 * *.**.** P
14720 75 73 68 20 61 6e 20 69 6e 74 65 67 65 72 20 6f  ush an integer o
14721 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 77 68  nto the stack wh
14722 69 63 68 20 69 73 20 74 68 65 20 6e 65 78 74 20  ich is the next 
14723 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 73 65 71  available.** seq
14724 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f 72  uence number for
14725 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 65   cursor P1.  The
14726 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72   sequence number
14727 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f   on the.** curso
14728 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  r is incremented
14729 20 61 66 74 65 72 20 74 68 65 20 70 75 73 68 2e   after the push.
1472a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75  .*/.case OP_Sequ
1472b 65 6e 63 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20  ence: {.  int i 
1472c 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73  = pOp->p1;.  ass
1472d 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53  ert( pTos>=p->aS
1472e 74 61 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74  tack );.  assert
1472f 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
14730 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
14731 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21  rt( p->apCsr[i]!
14732 3d 30 20 29 3b 0a 20 20 70 54 6f 73 2b 2b 3b 0a  =0 );.  pTos++;.
14733 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 70 2d    pTos->u.i = p-
14734 3e 61 70 43 73 72 5b 69 5d 2d 3e 73 65 71 43 6f  >apCsr[i]->seqCo
14735 75 6e 74 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e 66  unt++;.  pTos->f
14736 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
14737 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20    break;.}.../* 
14738 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64  Opcode: NewRowid
14739 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 47   P1 P2 *.**.** G
1473a 65 74 20 61 20 6e 65 77 20 69 6e 74 65 67 65 72  et a new integer
1473b 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28   record number (
1473c 61 2e 6b 2e 61 20 22 72 6f 77 69 64 22 29 20 75  a.k.a "rowid") u
1473d 73 65 64 20 61 73 20 74 68 65 20 6b 65 79 20 74  sed as the key t
1473e 6f 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68  o a table..** Th
1473f 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
14740 69 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c  is not previousl
14741 79 20 75 73 65 64 20 61 73 20 61 20 6b 65 79 20  y used as a key 
14742 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  in the database.
14743 2a 2a 20 74 61 62 6c 65 20 74 68 61 74 20 63 75  ** table that cu
14744 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74  rsor P1 points t
14745 6f 2e 20 20 54 68 65 20 6e 65 77 20 72 65 63 6f  o.  The new reco
14746 72 64 20 6e 75 6d 62 65 72 20 69 73 20 70 75 73  rd number is pus
14747 68 65 64 20 0a 2a 2a 20 6f 6e 74 6f 20 74 68 65  hed .** onto the
14748 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66   stack..**.** If
14749 20 50 32 3e 30 20 74 68 65 6e 20 50 32 20 69 73   P2>0 then P2 is
1474a 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74   a memory cell t
1474b 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6c 61  hat holds the la
1474c 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79  rgest previously
1474d 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72 65  .** generated re
1474e 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 4e 6f  cord number.  No
1474f 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62   new record numb
14750 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20  ers are allowed 
14751 74 6f 20 62 65 20 6c 65 73 73 0a 2a 2a 20 74 68  to be less.** th
14752 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2e 20 20  an this value.  
14753 57 68 65 6e 20 74 68 69 73 20 76 61 6c 75 65 20  When this value 
14754 72 65 61 63 68 65 73 20 69 74 73 20 6d 61 78 69  reaches its maxi
14755 6d 75 6d 2c 20 61 20 53 51 4c 49 54 45 5f 46 55  mum, a SQLITE_FU
14756 4c 4c 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 67  LL.** error is g
14757 65 6e 65 72 61 74 65 64 2e 20 20 54 68 65 20 50  enerated.  The P
14758 32 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73  2 memory cell is
14759 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68   updated with th
1475a 65 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 72  e generated.** r
1475b 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 54  ecord number.  T
1475c 68 69 73 20 50 32 20 6d 65 63 68 61 6e 69 73 6d  his P2 mechanism
1475d 20 69 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70   is used to help
1475e 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a   implement the.*
1475f 2a 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20  * AUTOINCREMENT 
14760 66 65 61 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65  feature..*/.case
14761 20 4f 50 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 0a   OP_NewRowid: {.
14762 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
14763 31 3b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a  1;.  i64 v = 0;.
14764 20 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20    Cursor *pC;.  
14765 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
14766 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  i<p->nCursor );.
14767 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43    assert( p->apC
14768 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66  sr[i]!=0 );.  if
14769 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  ( (pC = p->apCsr
1476a 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 3d 3d 30  [i])->pCursor==0
1476b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a   ){.    /* The z
1476c 65 72 6f 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ero initializati
1476d 6f 6e 20 61 62 6f 76 65 20 69 73 20 61 6c 6c 20  on above is all 
1476e 74 68 61 74 20 69 73 20 6e 65 65 64 65 64 20 2a  that is needed *
1476f 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  /.  }else{.    /
14770 2a 20 54 68 65 20 6e 65 78 74 20 72 6f 77 69 64  * The next rowid
14771 20 6f 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65   or record numbe
14772 72 20 28 64 69 66 66 65 72 65 6e 74 20 74 65 72  r (different ter
14773 6d 73 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a  ms for the same.
14774 20 20 20 20 2a 2a 20 74 68 69 6e 67 29 20 69 73      ** thing) is
14775 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 61 20 74   obtained in a t
14776 77 6f 2d 73 74 65 70 20 61 6c 67 6f 72 69 74 68  wo-step algorith
14777 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  m..    **.    **
14778 20 46 69 72 73 74 20 77 65 20 61 74 74 65 6d 70   First we attemp
14779 74 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6c 61  t to find the la
1477a 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 72  rgest existing r
1477b 6f 77 69 64 20 61 6e 64 20 61 64 64 20 6f 6e 65  owid and add one
1477c 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e  .    ** to that.
1477d 20 20 42 75 74 20 69 66 20 74 68 65 20 6c 61 72    But if the lar
1477e 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f  gest existing ro
1477f 77 69 64 20 69 73 20 61 6c 72 65 61 64 79 20 74  wid is already t
14780 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a  he maximum.    *
14781 2a 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67  * positive integ
14782 65 72 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66  er, we have to f
14783 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
14784 68 65 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a  he second.    **
14785 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 61   probabilistic a
14786 6c 67 6f 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a  lgorithm.    **.
14787 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e      ** The secon
14788 64 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 74  d algorithm is t
14789 6f 20 73 65 6c 65 63 74 20 61 20 72 6f 77 69 64  o select a rowid
1478a 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73   at random and s
1478b 65 65 20 69 66 0a 20 20 20 20 2a 2a 20 69 74 20  ee if.    ** it 
1478c 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 69  already exists i
1478d 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66  n the table.  If
1478e 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69   it does not exi
1478f 73 74 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20  st, we have.    
14790 2a 2a 20 73 75 63 63 65 65 64 65 64 2e 20 20 49  ** succeeded.  I
14791 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77  f the random row
14792 69 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 77  id does exist, w
14793 65 20 73 65 6c 65 63 74 20 61 20 6e 65 77 20 6f  e select a new o
14794 6e 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 72  ne.    ** and tr
14795 79 20 61 67 61 69 6e 2c 20 75 70 20 74 6f 20 31  y again, up to 1
14796 30 30 30 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a  000 times..    *
14797 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 20 61 20 74  *.    ** For a t
14798 61 62 6c 65 20 77 69 74 68 20 6c 65 73 73 20 74  able with less t
14799 68 61 6e 20 32 20 62 69 6c 6c 69 6f 6e 20 65 6e  han 2 billion en
1479a 74 72 69 65 73 2c 20 74 68 65 20 70 72 6f 62 61  tries, the proba
1479b 62 69 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66  bility.    ** of
1479c 20 6e 6f 74 20 66 69 6e 64 69 6e 67 20 61 20 75   not finding a u
1479d 6e 75 73 65 64 20 72 6f 77 69 64 20 69 73 20 61  nused rowid is a
1479e 62 6f 75 74 20 31 2e 30 65 2d 33 30 30 2e 20 20  bout 1.0e-300.  
1479f 54 68 69 73 20 69 73 20 61 20 0a 20 20 20 20 2a  This is a .    *
147a0 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61  * non-zero proba
147a1 62 69 6c 69 74 79 2c 20 62 75 74 20 69 74 20 69  bility, but it i
147a2 73 20 73 74 69 6c 6c 20 76 61 6e 69 73 68 69 6e  s still vanishin
147a3 67 6c 79 20 73 6d 61 6c 6c 20 61 6e 64 20 73 68  gly small and sh
147a4 6f 75 6c 64 0a 20 20 20 20 2a 2a 20 6e 65 76 65  ould.    ** neve
147a5 72 20 63 61 75 73 65 20 61 20 70 72 6f 62 6c 65  r cause a proble
147a6 6d 2e 20 20 59 6f 75 20 61 72 65 20 6d 75 63 68  m.  You are much
147a7 2c 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65  , much more like
147a8 6c 79 20 74 6f 20 68 61 76 65 20 61 0a 20 20 20  ly to have a.   
147a9 20 2a 2a 20 68 61 72 64 77 61 72 65 20 66 61 69   ** hardware fai
147aa 6c 75 72 65 20 74 68 61 6e 20 66 6f 72 20 74 68  lure than for th
147ab 69 73 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20  is algorithm to 
147ac 66 61 69 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  fail..    **.   
147ad 20 2a 2a 20 54 68 65 20 61 6e 61 6c 79 73 69 73   ** The analysis
147ae 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73   in the previous
147af 20 70 61 72 61 67 72 61 70 68 20 61 73 73 75 6d   paragraph assum
147b0 65 73 20 74 68 61 74 20 79 6f 75 20 68 61 76 65  es that you have
147b1 20 61 20 67 6f 6f 64 0a 20 20 20 20 2a 2a 20 73   a good.    ** s
147b2 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20  ource of random 
147b3 6e 75 6d 62 65 72 73 2e 20 20 49 73 20 61 20 6c  numbers.  Is a l
147b4 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20  ibrary function 
147b5 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 0a 20  like lrand48(). 
147b6 20 20 20 2a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67     ** good enoug
147b7 68 3f 20 20 4d 61 79 62 65 2e 20 4d 61 79 62 65  h?  Maybe. Maybe
147b8 20 6e 6f 74 2e 20 49 74 27 73 20 68 61 72 64 20   not. It's hard 
147b9 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20  to know whether 
147ba 74 68 65 72 65 0a 20 20 20 20 2a 2a 20 6d 69 67  there.    ** mig
147bb 68 74 20 62 65 20 73 75 62 74 6c 65 20 62 75 67  ht be subtle bug
147bc 73 20 69 73 20 73 6f 6d 65 20 69 6d 70 6c 65 6d  s is some implem
147bd 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 6c 72 61  entations of lra
147be 6e 64 34 38 28 29 20 74 68 61 74 0a 20 20 20 20  nd48() that.    
147bf 2a 2a 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70  ** could cause p
147c0 72 6f 62 6c 65 6d 73 2e 20 54 6f 20 61 76 6f 69  roblems. To avoi
147c1 64 20 75 6e 63 65 72 74 61 69 6e 74 79 2c 20 53  d uncertainty, S
147c2 51 4c 69 74 65 20 75 73 65 73 20 69 74 73 20 6f  QLite uses its o
147c3 77 6e 20 0a 20 20 20 20 2a 2a 20 72 61 6e 64 6f  wn .    ** rando
147c4 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74  m number generat
147c5 6f 72 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  or based on the 
147c6 52 43 34 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20  RC4 algorithm.. 
147c7 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20     **.    ** To 
147c8 70 72 6f 6d 6f 74 65 20 6c 6f 63 61 6c 69 74 79  promote locality
147c9 20 6f 66 20 72 65 66 65 72 65 6e 63 65 20 66 6f   of reference fo
147ca 72 20 72 65 70 65 74 69 74 69 76 65 20 69 6e 73  r repetitive ins
147cb 65 72 74 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a  erts, the.    **
147cc 20 66 69 72 73 74 20 66 65 77 20 61 74 74 65 6d   first few attem
147cd 70 74 73 20 61 74 20 63 68 6f 73 69 6e 67 20 61  pts at chosing a
147ce 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69   random rowid pi
147cf 63 6b 20 76 61 6c 75 65 73 20 6a 75 73 74 20 61  ck values just a
147d0 20 6c 69 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c   little.    ** l
147d1 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70  arger than the p
147d2 72 65 76 69 6f 75 73 20 72 6f 77 69 64 2e 20 20  revious rowid.  
147d3 54 68 69 73 20 68 61 73 20 62 65 65 6e 20 73 68  This has been sh
147d4 6f 77 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c  own experimental
147d5 6c 79 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75  ly.    ** to dou
147d6 62 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66  ble the speed of
147d7 20 74 68 65 20 43 4f 50 59 20 6f 70 65 72 61 74   the COPY operat
147d8 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
147d9 69 6e 74 20 72 65 73 2c 20 72 78 3d 53 51 4c 49  int res, rx=SQLI
147da 54 45 5f 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20  TE_OK, cnt;.    
147db 69 36 34 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d  i64 x;.    cnt =
147dc 20 30 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c   0;.    if( (sql
147dd 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70  ite3BtreeFlags(p
147de 43 2d 3e 70 43 75 72 73 6f 72 29 26 28 42 54 52  C->pCursor)&(BTR
147df 45 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f  EE_INTKEY|BTREE_
147e0 5a 45 52 4f 44 41 54 41 29 29 20 21 3d 0a 20 20  ZERODATA)) !=.  
147e1 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 49 4e          BTREE_IN
147e2 54 4b 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63  TKEY ){.      rc
147e3 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
147e4 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f  T_BKPT;.      go
147e5 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
147e6 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20  error;.    }.   
147e7 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65   assert( (sqlite
147e8 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e  3BtreeFlags(pC->
147e9 70 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45  pCursor) & BTREE
147ea 5f 49 4e 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20  _INTKEY)!=0 );. 
147eb 20 20 20 61 73 73 65 72 74 28 20 28 73 71 6c 69     assert( (sqli
147ec 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43  te3BtreeFlags(pC
147ed 2d 3e 70 43 75 72 73 6f 72 29 20 26 20 42 54 52  ->pCursor) & BTR
147ee 45 45 5f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20  EE_ZERODATA)==0 
147ef 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
147f0 45 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 20  E_32BIT_ROWID.# 
147f1 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57    define MAX_ROW
147f2 49 44 20 30 78 37 66 66 66 66 66 66 66 0a 23 65  ID 0x7fffffff.#e
147f3 6c 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20  lse.    /* Some 
147f4 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61  compilers compla
147f5 69 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e  in about constan
147f6 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 30  ts of the form 0
147f7 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66  x7ffffffffffffff
147f8 66 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 73  f..    ** Others
147f9 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20   complain about 
147fa 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66  0x7fffffffffffff
147fb 66 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c  fffLL.  The foll
147fc 6f 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d  owing macro seem
147fd 73 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76  s.    ** to prov
147fe 69 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e 74  ide the constant
147ff 20 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c   while making al
14800 6c 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 70  l compilers happ
14801 79 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65  y..    */.#   de
14802 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 20  fine MAX_ROWID  
14803 28 20 28 28 28 75 36 34 29 30 78 37 66 66 66 66  ( (((u64)0x7ffff
14804 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75 36 34  fff)<<32) | (u64
14805 29 30 78 66 66 66 66 66 66 66 66 20 29 0a 23 65  )0xffffffff ).#e
14806 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 21 70  ndif..    if( !p
14807 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69  C->useRandomRowi
14808 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  d ){.      if( p
14809 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69  C->nextRowidVali
1480a 64 20 29 7b 0a 20 20 20 20 20 20 20 20 76 20 3d  d ){.        v =
1480b 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 3b 0a   pC->nextRowid;.
1480c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1480d 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1480e 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d 3e 70  3BtreeLast(pC->p
1480f 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20  Cursor, &res);. 
14810 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
14811 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
14812 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
14813 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
14814 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14815 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20   if( res ){.    
14816 20 20 20 20 20 20 76 20 3d 20 31 3b 0a 20 20 20        v = 1;.   
14817 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
14818 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
14819 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e 70 43  eeKeySize(pC->pC
1481a 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20 20  ursor, &v);.    
1481b 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49        v = keyToI
1481c 6e 74 28 76 29 3b 0a 20 20 20 20 20 20 20 20 20  nt(v);.         
1481d 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52 4f 57 49   if( v==MAX_ROWI
1481e 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  D ){.           
1481f 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f   pC->useRandomRo
14820 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  wid = 1;.       
14821 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
14822 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20        v++;.     
14823 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
14824 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65  .      }..#ifnde
14825 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
14826 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20  TOINCREMENT.    
14827 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 29 7b    if( pOp->p2 ){
14828 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d  .        Mem *pM
14829 65 6d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  em;.        asse
1482a 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26  rt( pOp->p2>0 &&
1482b 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4d 65 6d   pOp->p2<p->nMem
1482c 20 29 3b 20 20 2f 2a 20 50 32 20 69 73 20 61 20   );  /* P2 is a 
1482d 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c  valid memory cel
1482e 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d 65  l */.        pMe
1482f 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  m = &p->aMem[pOp
14830 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 20 73  ->p2];.        s
14831 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
14832 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20  egerify(pMem);. 
14833 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28         assert( (
14834 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
14835 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a  M_Int)!=0 );  /*
14836 20 6d 65 6d 28 50 32 29 20 68 6f 6c 64 73 20 61   mem(P2) holds a
14837 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20  n integer */.   
14838 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 75       if( pMem->u
14839 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c  .i==MAX_ROWID ||
1483a 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f   pC->useRandomRo
1483b 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  wid ){.         
1483c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c   rc = SQLITE_FUL
1483d 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  L;.          got
1483e 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1483f 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rror;.        }.
14840 20 20 20 20 20 20 20 20 69 66 28 20 76 3c 70 4d          if( v<pM
14841 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20  em->u.i+1 ){.   
14842 20 20 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d         v = pMem-
14843 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20 20  >u.i + 1;.      
14844 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 65 6d    }.        pMem
14845 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20 20  ->u.i = v;.     
14846 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20   }.#endif..     
14847 20 69 66 28 20 76 3c 4d 41 58 5f 52 4f 57 49 44   if( v<MAX_ROWID
14848 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e   ){.        pC->
14849 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20 3d  nextRowidValid =
1484a 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e   1;.        pC->
1484b 6e 65 78 74 52 6f 77 69 64 20 3d 20 76 2b 31 3b  nextRowid = v+1;
1484c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1484d 20 20 20 20 20 20 70 43 2d 3e 6e 65 78 74 52 6f        pC->nextRo
1484e 77 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  widValid = 0;.  
1484f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
14850 69 66 28 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f  if( pC->useRando
14851 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  mRowid ){.      
14852 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3d  assert( pOp->p2=
14853 3d 30 20 29 3b 20 20 2f 2a 20 53 51 4c 49 54 45  =0 );  /* SQLITE
14854 5f 46 55 4c 4c 20 6d 75 73 74 20 68 61 76 65 20  _FULL must have 
14855 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72 20 74  occurred prior t
14856 6f 20 74 68 69 73 20 2a 2f 0a 20 20 20 20 20 20  o this */.      
14857 76 20 3d 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77  v = db->priorNew
14858 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63 6e 74  Rowid;.      cnt
14859 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a   = 0;.      do{.
1485a 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d 30          if( v==0
1485b 20 7c 7c 20 63 6e 74 3e 32 20 29 7b 0a 20 20 20   || cnt>2 ){.   
1485c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 61         sqlite3Ra
1485d 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28  ndomness(sizeof(
1485e 76 29 2c 20 26 76 29 3b 0a 20 20 20 20 20 20 20  v), &v);.       
1485f 20 20 20 69 66 28 20 63 6e 74 3c 35 20 29 20 76     if( cnt<5 ) v
14860 20 26 3d 20 30 78 66 66 66 66 66 66 3b 0a 20 20   &= 0xffffff;.  
14861 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
14862 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20         unsigned 
14863 63 68 61 72 20 72 3b 0a 20 20 20 20 20 20 20 20  char r;.        
14864 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f 6d 6e    sqlite3Randomn
14865 65 73 73 28 31 2c 20 26 72 29 3b 0a 20 20 20 20  ess(1, &r);.    
14866 20 20 20 20 20 20 76 20 2b 3d 20 72 20 2b 20 31        v += r + 1
14867 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
14868 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 63      if( v==0 ) c
14869 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
1486a 20 78 20 3d 20 69 6e 74 54 6f 4b 65 79 28 76 29   x = intToKey(v)
1486b 3b 0a 20 20 20 20 20 20 20 20 72 78 20 3d 20 73  ;.        rx = s
1486c 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
1486d 6f 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30  o(pC->pCursor, 0
1486e 2c 20 28 75 36 34 29 78 2c 20 30 2c 20 26 72 65  , (u64)x, 0, &re
1486f 73 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 2b  s);.        cnt+
14870 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28  +;.      }while(
14871 20 63 6e 74 3c 31 30 30 30 20 26 26 20 72 78 3d   cnt<1000 && rx=
14872 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65  =SQLITE_OK && re
14873 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62  s==0 );.      db
14874 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20  ->priorNewRowid 
14875 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 72  = v;.      if( r
14876 78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  x==SQLITE_OK && 
14877 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
14878 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55    rc = SQLITE_FU
14879 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  LL;.        goto
1487a 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1487b 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
1487c 20 7d 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64   }.    pC->rowid
1487d 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1487e 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
1487f 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d  eto = 0;.    pC-
14880 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
14881 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a  ACHE_STALE;.  }.
14882 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 70 54 6f 73    pTos++;.  pTos
14883 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 70 54 6f  ->u.i = v;.  pTo
14884 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  s->flags = MEM_I
14885 6e 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  nt;.  break;.}..
14886 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72  /* Opcode: Inser
14887 74 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a  t P1 P2 P3.**.**
14888 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20   Write an entry 
14889 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f  into the table o
1488a 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20  f cursor P1.  A 
1488b 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20  new entry is.** 
1488c 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f  created if it do
1488d 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78  esn't already ex
1488e 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20  ist or the data 
1488f 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  for an existing.
14890 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72  ** entry is over
14891 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61  written.  The da
14892 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ta is the value 
14893 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  on the top of th
14894 65 0a 2a 2a 20 73 74 61 63 6b 2e 20 20 54 68 65  e.** stack.  The
14895 20 6b 65 79 20 69 73 20 74 68 65 20 6e 65 78 74   key is the next
14896 20 76 61 6c 75 65 20 64 6f 77 6e 20 6f 6e 20 74   value down on t
14897 68 65 20 73 74 61 63 6b 2e 20 20 54 68 65 20 6b  he stack.  The k
14898 65 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6e  ey must.** be an
14899 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65 20 73   integer.  The s
1489a 74 61 63 6b 20 69 73 20 70 6f 70 70 65 64 20 74  tack is popped t
1489b 77 69 63 65 20 62 79 20 74 68 69 73 20 69 6e 73  wice by this ins
1489c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  truction..**.** 
1489d 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43  If the OPFLAG_NC
1489e 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 32  HANGE flag of P2
1489f 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68   is set, then th
148a0 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75  e row change cou
148a1 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65  nt is.** increme
148a2 6e 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20  nted (otherwise 
148a3 6e 6f 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50  not).  If the OP
148a4 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66  FLAG_LASTROWID f
148a5 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73 65 74  lag of P2 is set
148a6 2c 0a 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20  ,.** then rowid 
148a7 69 73 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75  is stored for su
148a8 62 73 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20  bsequent return 
148a9 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  by the.** sqlite
148aa 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
148ab 77 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28  wid() function (
148ac 6f 74 68 65 72 77 69 73 65 20 69 74 27 73 20 75  otherwise it's u
148ad 6e 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a  nmodified)..**.*
148ae 2a 20 50 61 72 61 6d 65 74 65 72 20 50 33 20 6d  * Parameter P3 m
148af 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74  ay point to a st
148b0 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
148b1 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 2c 20  the table-name, 
148b2 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c  or.** may be NUL
148b3 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  L. If it is not 
148b4 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 75  NULL, then the u
148b5 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a 20 28  pdate-hook .** (
148b6 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 65 43  sqlite3.xUpdateC
148b7 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e 76 6f  allback) is invo
148b8 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  ked following a 
148b9 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 65 72  successful inser
148ba 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  t..**.** This in
148bb 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77  struction only w
148bc 6f 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e 20  orks on tables. 
148bd 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20   The equivalent 
148be 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66  instruction.** f
148bf 6f 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f 50  or indices is OP
148c0 5f 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63  _IdxInsert..*/.c
148c1 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 7b  ase OP_Insert: {
148c2 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70           /* no-p
148c3 75 73 68 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4e  ush */.  Mem *pN
148c4 6f 73 20 3d 20 26 70 54 6f 73 5b 2d 31 5d 3b 0a  os = &pTos[-1];.
148c5 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
148c6 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 3b  1;.  Cursor *pC;
148c7 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 73 3e  .  assert( pNos>
148c8 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20  =p->aStack );.  
148c9 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
148ca 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  i<p->nCursor );.
148cb 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43    assert( p->apC
148cc 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66  sr[i]!=0 );.  if
148cd 28 20 28 28 70 43 20 3d 20 70 2d 3e 61 70 43 73  ( ((pC = p->apCs
148ce 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 3d  r[i])->pCursor!=
148cf 30 20 7c 7c 20 70 43 2d 3e 70 73 65 75 64 6f 54  0 || pC->pseudoT
148d0 61 62 6c 65 29 20 29 7b 0a 20 20 20 20 69 36 34  able) ){.    i64
148d1 20 69 4b 65 79 3b 20 20 20 2f 2a 20 54 68 65 20   iKey;   /* The 
148d2 69 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72  integer ROWID or
148d3 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65 63   key for the rec
148d4 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ord to be insert
148d5 65 64 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72  ed */..    asser
148d6 74 28 20 70 4e 6f 73 2d 3e 66 6c 61 67 73 20 26  t( pNos->flags &
148d7 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20   MEM_Int );.    
148d8 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61  assert( pC->isTa
148d9 62 6c 65 20 29 3b 0a 20 20 20 20 69 4b 65 79 20  ble );.    iKey 
148da 3d 20 69 6e 74 54 6f 4b 65 79 28 70 4e 6f 73 2d  = intToKey(pNos-
148db 3e 75 2e 69 29 3b 0a 0a 20 20 20 20 69 66 28 20  >u.i);..    if( 
148dc 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47  pOp->p2 & OPFLAG
148dd 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43  _NCHANGE ) p->nC
148de 68 61 6e 67 65 2b 2b 3b 0a 20 20 20 20 69 66 28  hange++;.    if(
148df 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41   pOp->p2 & OPFLA
148e0 47 5f 4c 41 53 54 52 4f 57 49 44 20 29 20 64 62  G_LASTROWID ) db
148e1 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 4e  ->lastRowid = pN
148e2 6f 73 2d 3e 75 2e 69 3b 0a 20 20 20 20 69 66 28  os->u.i;.    if(
148e3 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61   pC->nextRowidVa
148e4 6c 69 64 20 26 26 20 70 4e 6f 73 2d 3e 75 2e 69  lid && pNos->u.i
148e5 3e 3d 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 20  >=pC->nextRowid 
148e6 29 7b 0a 20 20 20 20 20 20 70 43 2d 3e 6e 65 78  ){.      pC->nex
148e7 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b  tRowidValid = 0;
148e8 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
148e9 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Tos->flags & MEM
148ea 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70  _Null ){.      p
148eb 54 6f 73 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20  Tos->z = 0;.    
148ec 20 20 70 54 6f 73 2d 3e 6e 20 3d 20 30 3b 0a 20    pTos->n = 0;. 
148ed 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
148ee 61 73 73 65 72 74 28 20 70 54 6f 73 2d 3e 66 6c  assert( pTos->fl
148ef 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c  ags & (MEM_Blob|
148f0 4d 45 4d 5f 53 74 72 29 20 29 3b 0a 20 20 20 20  MEM_Str) );.    
148f1 7d 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 70 73  }.    if( pC->ps
148f2 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20  eudoTable ){.   
148f3 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
148f4 70 43 2d 3e 70 44 61 74 61 29 3b 0a 20 20 20 20  pC->pData);.    
148f5 20 20 70 43 2d 3e 69 4b 65 79 20 3d 20 69 4b 65    pC->iKey = iKe
148f6 79 3b 0a 20 20 20 20 20 20 70 43 2d 3e 6e 44 61  y;.      pC->nDa
148f7 74 61 20 3d 20 70 54 6f 73 2d 3e 6e 3b 0a 20 20  ta = pTos->n;.  
148f8 20 20 20 20 69 66 28 20 70 54 6f 73 2d 3e 66 6c      if( pTos->fl
148f9 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b  ags & MEM_Dyn ){
148fa 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 70 44 61  .        pC->pDa
148fb 74 61 20 3d 20 70 54 6f 73 2d 3e 7a 3b 0a 20 20  ta = pTos->z;.  
148fc 20 20 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67        pTos->flag
148fd 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
148fe 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
148ff 20 20 20 70 43 2d 3e 70 44 61 74 61 20 3d 20 73     pC->pData = s
14900 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 70  qlite3_malloc( p
14901 43 2d 3e 6e 44 61 74 61 2b 32 20 29 3b 0a 20 20  C->nData+2 );.  
14902 20 20 20 20 20 20 69 66 28 20 21 70 43 2d 3e 70        if( !pC->p
14903 44 61 74 61 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  Data ) goto no_m
14904 65 6d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  em;.        memc
14905 70 79 28 70 43 2d 3e 70 44 61 74 61 2c 20 70 54  py(pC->pData, pT
14906 6f 73 2d 3e 7a 2c 20 70 43 2d 3e 6e 44 61 74 61  os->z, pC->nData
14907 29 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 70  );.        pC->p
14908 44 61 74 61 5b 70 43 2d 3e 6e 44 61 74 61 5d 20  Data[pC->nData] 
14909 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 43 2d  = 0;.        pC-
1490a 3e 70 44 61 74 61 5b 70 43 2d 3e 6e 44 61 74 61  >pData[pC->nData
1490b 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  +1] = 0;.      }
1490c 0a 20 20 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52  .      pC->nullR
1490d 6f 77 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ow = 0;.    }els
1490e 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a 65  e{.      int nZe
1490f 72 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  ro;.      if( pT
14910 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  os->flags & MEM_
14911 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  Zero ){.        
14912 6e 5a 65 72 6f 20 3d 20 70 54 6f 73 2d 3e 75 2e  nZero = pTos->u.
14913 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  i;.      }else{.
14914 20 20 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20          nZero = 
14915 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
14916 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
14917 65 65 49 6e 73 65 72 74 28 70 43 2d 3e 70 43 75  eeInsert(pC->pCu
14918 72 73 6f 72 2c 20 30 2c 20 69 4b 65 79 2c 0a 20  rsor, 0, iKey,. 
14919 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1491a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 6f               pTo
1491b 73 2d 3e 7a 2c 20 70 54 6f 73 2d 3e 6e 2c 20 6e  s->z, pTos->n, n
1491c 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20 20 20 20  Zero,.          
1491d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1491e 20 20 20 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50      pOp->p2 & OP
1491f 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
14920 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 70 43 2d    }.    .    pC-
14921 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
14922 30 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72  0;.    pC->defer
14923 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
14924 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74     pC->cacheStat
14925 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
14926 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  ;..    /* Invoke
14927 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b   the update-hook
14928 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f   if required. */
14929 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1492a 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55  ITE_OK && db->xU
1492b 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26  pdateCallback &&
1492c 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20   pOp->p3 ){.    
1492d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1492e 62 20 3d 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e  b = db->aDb[pC->
1492f 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
14930 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
14931 62 6c 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  bl = pOp->p3;.  
14932 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 28 28 70      int op = ((p
14933 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47 5f  Op->p2 & OPFLAG_
14934 49 53 55 50 44 41 54 45 29 20 3f 20 53 51 4c 49  ISUPDATE) ? SQLI
14935 54 45 5f 55 50 44 41 54 45 20 3a 20 53 51 4c 49  TE_UPDATE : SQLI
14936 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20 20 20 20  TE_INSERT);.    
14937 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73    assert( pC->is
14938 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 64  Table );.      d
14939 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61  b->xUpdateCallba
1493a 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72  ck(db->pUpdateAr
1493b 67 2c 20 6f 70 2c 20 7a 44 62 2c 20 7a 54 62 6c  g, op, zDb, zTbl
1493c 2c 20 69 4b 65 79 29 3b 0a 20 20 20 20 20 20 61  , iKey);.      a
1493d 73 73 65 72 74 28 20 70 43 2d 3e 69 44 62 3e 3d  ssert( pC->iDb>=
1493e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  0 );.    }.  }. 
1493f 20 70 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c   popStack(&pTos,
14940 20 32 29 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d   2);..  break;.}
14941 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c  ../* Opcode: Del
14942 65 74 65 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  ete P1 P2 P3.**.
14943 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 72 65  ** Delete the re
14944 63 6f 72 64 20 61 74 20 77 68 69 63 68 20 74 68  cord at which th
14945 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 63  e P1 cursor is c
14946 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
14947 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72  g..**.** The cur
14948 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65 66 74  sor will be left
14949 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 69 74   pointing at eit
1494a 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f 72 20  her the next or 
1494b 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20  the previous.** 
1494c 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 74 61  record in the ta
1494d 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20 6c 65  ble. If it is le
1494e 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74  ft pointing at t
1494f 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64 2c 20  he next record, 
14950 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74  then.** the next
14951 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   Next instructio
14952 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f  n will be a no-o
14953 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69 73 20  p.  Hence it is 
14954 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20  OK to delete.** 
14955 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 77 69  a record from wi
14956 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f  thin an Next loo
14957 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  p..**.** If the 
14958 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66  OPFLAG_NCHANGE f
14959 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73 65 74  lag of P2 is set
1495a 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63  , then the row c
1495b 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a  hange count is.*
1495c 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f  * incremented (o
1495d 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a  therwise not)..*
1495e 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 61 20  *.** If P1 is a 
1495f 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68  pseudo-table, th
14960 65 6e 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  en this instruct
14961 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ion is a no-op..
14962 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65 74  */.case OP_Delet
14963 65 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e  e: {        /* n
14964 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20  o-push */.  int 
14965 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43  i = pOp->p1;.  C
14966 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73  ursor *pC;.  ass
14967 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
14968 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70  ->nCursor );.  p
14969 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b  C = p->apCsr[i];
1496a 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30  .  assert( pC!=0
1496b 20 29 3b 0a 20 20 69 66 28 20 70 43 2d 3e 70 43   );.  if( pC->pC
1496c 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20  ursor!=0 ){.    
1496d 69 36 34 20 69 4b 65 79 3b 0a 0a 20 20 20 20 2f  i64 iKey;..    /
1496e 2a 20 49 66 20 74 68 65 20 75 70 64 61 74 65 2d  * If the update-
1496f 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76  hook will be inv
14970 6f 6b 65 64 2c 20 73 65 74 20 69 4b 65 79 20 74  oked, set iKey t
14971 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74  o the rowid of t
14972 68 65 0a 20 20 20 20 2a 2a 20 72 6f 77 20 62 65  he.    ** row be
14973 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 20  ing deleted..   
14974 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   */.    if( db->
14975 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20  xUpdateCallback 
14976 26 26 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20  && pOp->p3 ){.  
14977 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
14978 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20  isTable );.     
14979 20 69 66 28 20 70 43 2d 3e 72 6f 77 69 64 49 73   if( pC->rowidIs
1497a 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Valid ){.       
1497b 20 69 4b 65 79 20 3d 20 70 43 2d 3e 6c 61 73 74   iKey = pC->last
1497c 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 7d 65 6c  Rowid;.      }el
1497d 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  se{.        rc =
1497e 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
1497f 53 69 7a 65 28 70 43 2d 3e 70 43 75 72 73 6f 72  Size(pC->pCursor
14980 2c 20 26 69 4b 65 79 29 3b 0a 20 20 20 20 20 20  , &iKey);.      
14981 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
14982 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
14983 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
14984 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14985 20 69 4b 65 79 20 3d 20 6b 65 79 54 6f 49 6e 74   iKey = keyToInt
14986 28 69 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a  (iKey);.      }.
14987 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
14988 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
14989 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 20  rMoveto(pC);.   
1498a 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61   if( rc ) goto a
1498b 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1498c 72 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  r;.    rc = sqli
1498d 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 70  te3BtreeDelete(p
1498e 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20  C->pCursor);.   
1498f 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61   pC->nextRowidVa
14990 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d  lid = 0;.    pC-
14991 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
14992 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 20  ACHE_STALE;..   
14993 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75   /* Invoke the u
14994 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65  pdate-hook if re
14995 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69  quired. */.    i
14996 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
14997 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43   && db->xUpdateC
14998 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e  allback && pOp->
14999 70 33 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73  p3 ){.      cons
1499a 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62  t char *zDb = db
1499b 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a  ->aDb[pC->iDb].z
1499c 4e 61 6d 65 3b 0a 20 20 20 20 20 20 63 6f 6e 73  Name;.      cons
1499d 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70  t char *zTbl = p
1499e 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 20 20 64 62  Op->p3;.      db
1499f 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63  ->xUpdateCallbac
149a0 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67  k(db->pUpdateArg
149a1 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
149a2 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79   zDb, zTbl, iKey
149a3 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
149a4 20 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20   pC->iDb>=0 );. 
149a5 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
149a6 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47 5f  Op->p2 & OPFLAG_
149a7 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68  NCHANGE ) p->nCh
149a8 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 65 61 6b 3b  ange++;.  break;
149a9 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
149aa 65 73 65 74 43 6f 75 6e 74 20 50 31 20 2a 20 2a  esetCount P1 * *
149ab 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
149ac 64 65 20 72 65 73 65 74 73 20 74 68 65 20 56 4d  de resets the VM
149ad 73 20 69 6e 74 65 72 6e 61 6c 20 63 68 61 6e 67  s internal chang
149ae 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 2e 20  e counter to 0. 
149af 49 66 20 50 31 20 69 73 20 74 72 75 65 2c 0a 2a  If P1 is true,.*
149b0 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  * then the value
149b1 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 20 63   of the change c
149b2 6f 75 6e 74 65 72 20 69 73 20 63 6f 70 69 65 64  ounter is copied
149b3 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
149b4 20 68 61 6e 64 6c 65 0a 2a 2a 20 63 68 61 6e 67   handle.** chang
149b5 65 20 63 6f 75 6e 74 65 72 20 28 72 65 74 75 72  e counter (retur
149b6 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65 6e  ned by subsequen
149b7 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  t calls to sqlit
149b8 65 33 5f 63 68 61 6e 67 65 73 28 29 29 0a 2a 2a  e3_changes()).**
149b9 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 72 65   before it is re
149ba 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 65  set. This is use
149bb 64 20 62 79 20 74 72 69 67 67 65 72 20 70 72 6f  d by trigger pro
149bc 67 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f  grams..*/.case O
149bd 50 5f 52 65 73 65 74 43 6f 75 6e 74 3a 20 7b 20  P_ResetCount: { 
149be 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73         /* no-pus
149bf 68 20 2a 2f 0a 20 20 69 66 28 20 70 4f 70 2d 3e  h */.  if( pOp->
149c0 70 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  p1 ){.    sqlite
149c1 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
149c2 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b  db, p->nChange);
149c3 0a 20 20 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e 67  .  }.  p->nChang
149c4 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a  e = 0;.  break;.
149c5 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f  }../* Opcode: Ro
149c6 77 44 61 74 61 20 50 31 20 2a 20 2a 0a 2a 2a 0a  wData P1 * *.**.
149c7 2a 2a 20 50 75 73 68 20 6f 6e 74 6f 20 74 68 65  ** Push onto the
149c8 20 73 74 61 63 6b 20 74 68 65 20 63 6f 6d 70 6c   stack the compl
149c9 65 74 65 20 72 6f 77 20 64 61 74 61 20 66 6f 72  ete row data for
149ca 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54   cursor P1..** T
149cb 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72  here is no inter
149cc 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  pretation of the
149cd 20 64 61 74 61 2e 20 20 49 74 20 69 73 20 6a 75   data.  It is ju
149ce 73 74 20 63 6f 70 69 65 64 0a 2a 2a 20 6f 6e 74  st copied.** ont
149cf 6f 20 74 68 65 20 73 74 61 63 6b 20 65 78 61 63  o the stack exac
149d0 74 6c 79 20 61 73 20 69 74 20 69 73 20 66 6f 75  tly as it is fou
149d1 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nd in the databa
149d2 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  se file..**.** I
149d3 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
149d4 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  not pointing to 
149d5 61 20 76 61 6c 69 64 20 72 6f 77 2c 20 61 20 4e  a valid row, a N
149d6 55 4c 4c 20 69 73 20 70 75 73 68 65 64 0a 2a 2a  ULL is pushed.**
149d7 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e   onto the stack.
149d8 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .*/./* Opcode: R
149d9 6f 77 4b 65 79 20 50 31 20 2a 20 2a 0a 2a 2a 0a  owKey P1 * *.**.
149da 2a 2a 20 50 75 73 68 20 6f 6e 74 6f 20 74 68 65  ** Push onto the
149db 20 73 74 61 63 6b 20 74 68 65 20 63 6f 6d 70 6c   stack the compl
149dc 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 20  ete row key for 
149dd 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68  cursor P1..** Th
149de 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70  ere is no interp
149df 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  retation of the 
149e0 6b 65 79 2e 20 20 49 74 20 69 73 20 6a 75 73 74  key.  It is just
149e1 20 63 6f 70 69 65 64 0a 2a 2a 20 6f 6e 74 6f 20   copied.** onto 
149e2 74 68 65 20 73 74 61 63 6b 20 65 78 61 63 74 6c  the stack exactl
149e3 79 20 61 73 20 69 74 20 69 73 20 66 6f 75 6e 64  y as it is found
149e4 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
149e5 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
149e6 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f  the cursor is no
149e7 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20  t pointing to a 
149e8 76 61 6c 69 64 20 72 6f 77 2c 20 61 20 4e 55 4c  valid row, a NUL
149e9 4c 20 69 73 20 70 75 73 68 65 64 0a 2a 2a 20 6f  L is pushed.** o
149ea 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a  nto the stack..*
149eb 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 4b 65 79  /.case OP_RowKey
149ec 3a 0a 63 61 73 65 20 4f 50 5f 52 6f 77 44 61 74  :.case OP_RowDat
149ed 61 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70  a: {.  int i = p
149ee 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72  Op->p1;.  Cursor
149ef 20 2a 70 43 3b 0a 20 20 75 33 32 20 6e 3b 0a 0a   *pC;.  u32 n;..
149f0 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 52    /* Note that R
149f1 6f 77 4b 65 79 20 61 6e 64 20 52 6f 77 44 61 74  owKey and RowDat
149f2 61 20 61 72 65 20 72 65 61 6c 6c 79 20 65 78 61  a are really exa
149f3 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 69 6e  ctly the same in
149f4 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 70  struction */.  p
149f5 54 6f 73 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28  Tos++;.  assert(
149f6 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
149f7 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20  ursor );.  pC = 
149f8 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61  p->apCsr[i];.  a
149f9 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62  ssert( pC->isTab
149fa 6c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64  le || pOp->opcod
149fb 65 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a  e==OP_RowKey );.
149fc 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73    assert( pC->is
149fd 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70  Index || pOp->op
149fe 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61  code==OP_RowData
149ff 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
14a00 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 2d  !=0 );.  if( pC-
14a01 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20  >nullRow ){.    
14a02 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pTos->flags = ME
14a03 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 20  M_Null;.  }else 
14a04 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21  if( pC->pCursor!
14a05 3d 30 20 29 7b 0a 20 20 20 20 42 74 43 75 72 73  =0 ){.    BtCurs
14a06 6f 72 20 2a 70 43 72 73 72 20 3d 20 70 43 2d 3e  or *pCrsr = pC->
14a07 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 72 63 20  pCursor;.    rc 
14a08 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
14a09 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20  sorMoveto(pC);. 
14a0a 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
14a0b 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
14a0c 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 43 2d  ror;.    if( pC-
14a0d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20  >nullRow ){.    
14a0e 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
14a0f 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
14a10 62 72 65 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65  break;.    }else
14a11 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78   if( pC->isIndex
14a12 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6e 36   ){.      i64 n6
14a13 34 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  4;.      assert(
14a14 20 21 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b   !pC->isTable );
14a15 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
14a16 72 65 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72  reeKeySize(pCrsr
14a17 2c 20 26 6e 36 34 29 3b 0a 20 20 20 20 20 20 69  , &n64);.      i
14a18 66 28 20 6e 36 34 3e 53 51 4c 49 54 45 5f 4d 41  f( n64>SQLITE_MA
14a19 58 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20  X_LENGTH ){.    
14a1a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
14a1b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
14a1c 6e 20 3d 20 6e 36 34 3b 0a 20 20 20 20 7d 65 6c  n = n64;.    }el
14a1d 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
14a1e 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 70  3BtreeDataSize(p
14a1f 43 72 73 72 2c 20 26 6e 29 3b 0a 20 20 20 20 7d  Crsr, &n);.    }
14a20 0a 20 20 20 20 69 66 28 20 6e 3e 53 51 4c 49 54  .    if( n>SQLIT
14a21 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b 0a  E_MAX_LENGTH ){.
14a22 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62        goto too_b
14a23 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54  ig;.    }.    pT
14a24 6f 73 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 20 69  os->n = n;.    i
14a25 66 28 20 6e 3c 3d 4e 42 46 53 20 29 7b 0a 20 20  f( n<=NBFS ){.  
14a26 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20      pTos->flags 
14a27 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d  = MEM_Blob | MEM
14a28 5f 53 68 6f 72 74 3b 0a 20 20 20 20 20 20 70 54  _Short;.      pT
14a29 6f 73 2d 3e 7a 20 3d 20 70 54 6f 73 2d 3e 7a 53  os->z = pTos->zS
14a2a 68 6f 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  hort;.    }else{
14a2b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 20 3d  .      char *z =
14a2c 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
14a2d 20 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20   n );.      if( 
14a2e 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  z==0 ) goto no_m
14a2f 65 6d 3b 0a 20 20 20 20 20 20 70 54 6f 73 2d 3e  em;.      pTos->
14a30 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62  flags = MEM_Blob
14a31 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20   | MEM_Dyn;.    
14a32 20 20 70 54 6f 73 2d 3e 78 44 65 6c 20 3d 20 30    pTos->xDel = 0
14a33 3b 0a 20 20 20 20 20 20 70 54 6f 73 2d 3e 7a 20  ;.      pTos->z 
14a34 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = z;.    }.    i
14a35 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29  f( pC->isIndex )
14a36 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
14a37 69 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 72  ite3BtreeKey(pCr
14a38 73 72 2c 20 30 2c 20 6e 2c 20 70 54 6f 73 2d 3e  sr, 0, n, pTos->
14a39 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  z);.    }else{. 
14a3a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
14a3b 33 42 74 72 65 65 44 61 74 61 28 70 43 72 73 72  3BtreeData(pCrsr
14a3c 2c 20 30 2c 20 6e 2c 20 70 54 6f 73 2d 3e 7a 29  , 0, n, pTos->z)
14a3d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
14a3e 69 66 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61  if( pC->pseudoTa
14a3f 62 6c 65 20 29 7b 0a 20 20 20 20 70 54 6f 73 2d  ble ){.    pTos-
14a40 3e 6e 20 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a  >n = pC->nData;.
14a41 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
14a42 6e 44 61 74 61 3c 3d 53 51 4c 49 54 45 5f 4d 41  nData<=SQLITE_MA
14a43 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 20 20  X_LENGTH );.    
14a44 70 54 6f 73 2d 3e 7a 20 3d 20 70 43 2d 3e 70 44  pTos->z = pC->pD
14a45 61 74 61 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66  ata;.    pTos->f
14a46 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c  lags = MEM_Blob|
14a47 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 6c  MEM_Ephem;.  }el
14a48 73 65 7b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c  se{.    pTos->fl
14a49 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
14a4a 20 20 7d 0a 20 20 70 54 6f 73 2d 3e 65 6e 63 20    }.  pTos->enc 
14a4b 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20  = SQLITE_UTF8;  
14a4c 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 62  /* In case the b
14a4d 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61 73 74  lob is ever cast
14a4e 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 62 72   to text */.  br
14a4f 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
14a50 65 3a 20 52 6f 77 69 64 20 50 31 20 2a 20 2a 0a  e: Rowid P1 * *.
14a51 2a 2a 0a 2a 2a 20 50 75 73 68 20 6f 6e 74 6f 20  **.** Push onto 
14a52 74 68 65 20 73 74 61 63 6b 20 61 6e 20 69 6e 74  the stack an int
14a53 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
14a54 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62  e key of the tab
14a55 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a  le entry that.**
14a56 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79   P1 is currently
14a57 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2f 0a 63 61   point to..*/.ca
14a58 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 0a 20  se OP_Rowid: {. 
14a59 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31   int i = pOp->p1
14a5a 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a  ;.  Cursor *pC;.
14a5b 20 20 69 36 34 20 76 3b 0a 0a 20 20 61 73 73 65    i64 v;..  asse
14a5c 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
14a5d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43  >nCursor );.  pC
14a5e 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a   = p->apCsr[i];.
14a5f 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20    assert( pC!=0 
14a60 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
14a61 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74  3VdbeCursorMovet
14a62 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 63 20  o(pC);.  if( rc 
14a63 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
14a64 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 70 54 6f  _to_error;.  pTo
14a65 73 2b 2b 3b 0a 20 20 69 66 28 20 70 43 2d 3e 72  s++;.  if( pC->r
14a66 6f 77 69 64 49 73 56 61 6c 69 64 20 29 7b 0a 20  owidIsValid ){. 
14a67 20 20 20 76 20 3d 20 70 43 2d 3e 6c 61 73 74 52     v = pC->lastR
14a68 6f 77 69 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66  owid;.  }else if
14a69 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c  ( pC->pseudoTabl
14a6a 65 20 29 7b 0a 20 20 20 20 76 20 3d 20 6b 65 79  e ){.    v = key
14a6b 54 6f 49 6e 74 28 70 43 2d 3e 69 4b 65 79 29 3b  ToInt(pC->iKey);
14a6c 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d  .  }else if( pC-
14a6d 3e 6e 75 6c 6c 52 6f 77 20 7c 7c 20 70 43 2d 3e  >nullRow || pC->
14a6e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b 0a 20 20  pCursor==0 ){.  
14a6f 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
14a70 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 62 72  MEM_Null;.    br
14a71 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  eak;.  }else{.  
14a72 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43    assert( pC->pC
14a73 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20  ursor!=0 );.    
14a74 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53  sqlite3BtreeKeyS
14a75 69 7a 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c  ize(pC->pCursor,
14a76 20 26 76 29 3b 0a 20 20 20 20 76 20 3d 20 6b 65   &v);.    v = ke
14a77 79 54 6f 49 6e 74 28 76 29 3b 0a 20 20 7d 0a 20  yToInt(v);.  }. 
14a78 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 76 3b 0a   pTos->u.i = v;.
14a79 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
14a7a 4d 45 4d 5f 49 6e 74 3b 0a 20 20 62 72 65 61 6b  MEM_Int;.  break
14a7b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
14a7c 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a 0a 2a  NullRow P1 * *.*
14a7d 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  *.** Move the cu
14a7e 72 73 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c  rsor P1 to a nul
14a7f 6c 20 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43  l row.  Any OP_C
14a80 6f 6c 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73  olumn operations
14a81 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20 77  .** that occur w
14a82 68 69 6c 65 20 74 68 65 20 63 75 72 73 6f 72 20  hile the cursor 
14a83 69 73 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72  is on the null r
14a84 6f 77 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 70  ow will always p
14a85 75 73 68 20 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f  ush .** a NULL o
14a86 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a  nto the stack..*
14a87 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f  /.case OP_NullRo
14a88 77 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e  w: {        /* n
14a89 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20  o-push */.  int 
14a8a 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43  i = pOp->p1;.  C
14a8b 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73  ursor *pC;..  as
14a8c 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
14a8d 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
14a8e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
14a8f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
14a90 30 20 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52  0 );.  pC->nullR
14a91 6f 77 20 3d 20 31 3b 0a 20 20 70 43 2d 3e 72 6f  ow = 1;.  pC->ro
14a92 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
14a93 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
14a94 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20 50  pcode: Last P1 P
14a95 32 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  2 *.**.** The ne
14a96 78 74 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f  xt use of the Ro
14a97 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72  wid or Column or
14a98 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   Next instructio
14a99 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c  n for P1 .** wil
14a9a 6c 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 6c  l refer to the l
14a9b 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ast entry in the
14a9c 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
14a9d 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20  or index..** If 
14a9e 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
14a9f 65 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64 20  ex is empty and 
14aa0 50 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20  P2>0, then jump 
14aa1 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
14aa2 32 2e 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30  2..** If P2 is 0
14aa3 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65   or if the table
14aa4 20 6f 72 20 69 6e 64 65 78 20 69 73 20 6e 6f 74   or index is not
14aa5 20 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72   empty, fall thr
14aa6 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66  ough.** to the f
14aa7 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63  ollowing instruc
14aa8 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
14aa9 5f 4c 61 73 74 3a 20 7b 20 20 20 20 20 20 20 20  _Last: {        
14aaa 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20  /* no-push */.  
14aab 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
14aac 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  .  Cursor *pC;. 
14aad 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
14aae 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  ;..  assert( i>=
14aaf 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
14ab0 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61  r );.  pC = p->a
14ab1 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72  pCsr[i];.  asser
14ab2 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 69 66  t( pC!=0 );.  if
14ab3 28 20 28 70 43 72 73 72 20 3d 20 70 43 2d 3e 70  ( (pCrsr = pC->p
14ab4 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20  Cursor)!=0 ){.  
14ab5 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 72    int res;.    r
14ab6 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
14ab7 4c 61 73 74 28 70 43 72 73 72 2c 20 26 72 65 73  Last(pCrsr, &res
14ab8 29 3b 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52  );.    pC->nullR
14ab9 6f 77 20 3d 20 72 65 73 3b 0a 20 20 20 20 70 43  ow = res;.    pC
14aba 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
14abb 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61   = 0;.    pC->ca
14abc 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
14abd 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 69 66 28  E_STALE;.    if(
14abe 20 72 65 73 20 26 26 20 70 4f 70 2d 3e 70 32 3e   res && pOp->p2>
14abf 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  0 ){.      pc = 
14ac0 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
14ac1 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
14ac2 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b  pC->nullRow = 0;
14ac3 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
14ac4 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72  ../* Opcode: Sor
14ac5 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  t P1 P2 *.**.** 
14ac6 54 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73  This opcode does
14ac7 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d   exactly the sam
14ac8 65 20 74 68 69 6e 67 20 61 73 20 4f 50 5f 52 65  e thing as OP_Re
14ac9 77 69 6e 64 20 65 78 63 65 70 74 20 74 68 61 74  wind except that
14aca 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d 65 6e 74  .** it increment
14acb 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65  s an undocumente
14acc 64 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  d global variabl
14acd 65 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  e used for testi
14ace 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 74 69 6e  ng..**.** Sortin
14acf 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 65  g is accomplishe
14ad0 64 20 62 79 20 77 72 69 74 69 6e 67 20 72 65 63  d by writing rec
14ad1 6f 72 64 73 20 69 6e 74 6f 20 61 20 73 6f 72 74  ords into a sort
14ad2 69 6e 67 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68  ing index,.** th
14ad3 65 6e 20 72 65 77 69 6e 64 69 6e 67 20 74 68 61  en rewinding tha
14ad4 74 20 69 6e 64 65 78 20 61 6e 64 20 70 6c 61 79  t index and play
14ad5 69 6e 67 20 69 74 20 62 61 63 6b 20 66 72 6f 6d  ing it back from
14ad6 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a   beginning to.**
14ad7 20 65 6e 64 2e 20 20 57 65 20 75 73 65 20 74 68   end.  We use th
14ad8 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65  e OP_Sort opcode
14ad9 20 69 6e 73 74 65 61 64 20 6f 66 20 4f 50 5f 52   instead of OP_R
14ada 65 77 69 6e 64 20 74 6f 20 64 6f 20 74 68 65 0a  ewind to do the.
14adb 2a 2a 20 72 65 77 69 6e 64 69 6e 67 20 73 6f 20  ** rewinding so 
14adc 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c 20  that the global 
14add 76 61 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 65  variable will be
14ade 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 6e 64   incremented and
14adf 0a 2a 2a 20 72 65 67 72 65 73 73 69 6f 6e 20 74  .** regression t
14ae0 65 73 74 73 20 63 61 6e 20 64 65 74 65 72 6d 69  ests can determi
14ae1 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ne whether or no
14ae2 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 20  t the optimizer 
14ae3 69 73 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20  is.** correctly 
14ae4 6f 70 74 69 6d 69 7a 69 6e 67 20 6f 75 74 20 73  optimizing out s
14ae5 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  orts..*/.case OP
14ae6 5f 53 6f 72 74 3a 20 7b 20 20 20 20 20 20 20 20  _Sort: {        
14ae7 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 23 69  /* no-push */.#i
14ae8 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
14ae9 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f  .  sqlite3_sort_
14aea 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74  count++;.  sqlit
14aeb 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d  e3_search_count-
14aec 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 46  -;.#endif.  /* F
14aed 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f  all through into
14aee 20 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a   OP_Rewind */.}.
14aef 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e  /* Opcode: Rewin
14af0 64 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  d P1 P2 *.**.** 
14af1 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20  The next use of 
14af2 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c  the Rowid or Col
14af3 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74  umn or Next inst
14af4 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a  ruction for P1 .
14af5 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f  ** will refer to
14af6 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
14af7 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
14af8 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e   table or index.
14af9 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65  .** If the table
14afa 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70   or index is emp
14afb 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65  ty and P2>0, the
14afc 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  n jump immediate
14afd 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20  ly to P2..** If 
14afe 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68  P2 is 0 or if th
14aff 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
14b00 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66   is not empty, f
14b01 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  all through.** t
14b02 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
14b03 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
14b04 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20  case OP_Rewind: 
14b05 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70  {        /* no-p
14b06 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  ush */.  int i =
14b07 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72 73   pOp->p1;.  Curs
14b08 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73  or *pC;.  BtCurs
14b09 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74  or *pCrsr;.  int
14b0a 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 74 28   res;..  assert(
14b0b 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
14b0c 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20  ursor );.  pC = 
14b0d 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61  p->apCsr[i];.  a
14b0e 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a  ssert( pC!=0 );.
14b0f 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 70    if( (pCrsr = p
14b10 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29  C->pCursor)!=0 )
14b11 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
14b12 65 33 42 74 72 65 65 46 69 72 73 74 28 70 43 72  e3BtreeFirst(pCr
14b13 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 70  sr, &res);.    p
14b14 43 2d 3e 61 74 46 69 72 73 74 20 3d 20 72 65 73  C->atFirst = res
14b15 3d 3d 30 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66  ==0;.    pC->def
14b16 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b  erredMoveto = 0;
14b17 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74  .    pC->cacheSt
14b18 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
14b19 4c 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  LE;.  }else{.   
14b1a 20 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20   res = 1;.  }.  
14b1b 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 72 65  pC->nullRow = re
14b1c 73 3b 0a 20 20 69 66 28 20 72 65 73 20 26 26 20  s;.  if( res && 
14b1d 70 4f 70 2d 3e 70 32 3e 30 20 29 7b 0a 20 20 20  pOp->p2>0 ){.   
14b1e 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
14b1f 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
14b20 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65  }../* Opcode: Ne
14b21 78 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  xt P1 P2 *.**.**
14b22 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f 72 20   Advance cursor 
14b23 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f  P1 so that it po
14b24 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74  ints to the next
14b25 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69   key/data pair i
14b26 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f  n its.** table o
14b27 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65  r index.  If the
14b28 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6b  re are no more k
14b29 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74  ey/value pairs t
14b2a 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  hen fall through
14b2b 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  .** to the follo
14b2c 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e  wing instruction
14b2d 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 75  .  But if the cu
14b2e 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77 61 73  rsor advance was
14b2f 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
14b30 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
14b31 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65   to P2..**.** Se
14b32 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a 2a 2f 0a  e also: Prev.*/.
14b33 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 65 76 20  /* Opcode: Prev 
14b34 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 42 61  P1 P2 *.**.** Ba
14b35 63 6b 20 75 70 20 63 75 72 73 6f 72 20 50 31 20  ck up cursor P1 
14b36 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  so that it point
14b37 73 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75  s to the previou
14b38 73 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20  s key/data pair 
14b39 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20  in its.** table 
14b3a 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68  or index.  If th
14b3b 65 72 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f  ere is no previo
14b3c 75 73 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69  us key/value pai
14b3d 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  rs then fall thr
14b3e 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66  ough.** to the f
14b3f 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63  ollowing instruc
14b40 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68  tion.  But if th
14b41 65 20 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20  e cursor backup 
14b42 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a  was successful,.
14b43 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74  ** jump immediat
14b44 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2f 0a 63 61  ely to P2..*/.ca
14b45 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20  se OP_Prev:     
14b46 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
14b47 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a  */.case OP_Next:
14b48 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d   {        /* no-
14b49 70 75 73 68 20 2a 2f 0a 20 20 43 75 72 73 6f 72  push */.  Cursor
14b4a 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
14b4b 20 2a 70 43 72 73 72 3b 0a 0a 20 20 43 48 45 43   *pCrsr;..  CHEC
14b4c 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b  K_FOR_INTERRUPT;
14b4d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
14b4e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
14b4f 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
14b50 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70   pC = p->apCsr[p
14b51 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70  Op->p1];.  if( p
14b52 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72 65 61  C==0 ){.    brea
14b53 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69 63 6b 65  k;  /* See ticke
14b54 74 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20  t #2273 */.  }. 
14b55 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 70 43   if( (pCrsr = pC
14b56 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b  ->pCursor)!=0 ){
14b57 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20  .    int res;.  
14b58 20 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f    if( pC->nullRo
14b59 77 20 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d  w ){.      res =
14b5a 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
14b5b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d       assert( pC-
14b5c 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d  >deferredMoveto=
14b5d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  =0 );.      rc =
14b5e 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
14b5f 5f 4e 65 78 74 20 3f 20 73 71 6c 69 74 65 33 42  _Next ? sqlite3B
14b60 74 72 65 65 4e 65 78 74 28 70 43 72 73 72 2c 20  treeNext(pCrsr, 
14b61 26 72 65 73 29 20 3a 0a 20 20 20 20 20 20 20 20  &res) :.        
14b62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b63 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
14b64 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70  3BtreePrevious(p
14b65 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20  Crsr, &res);.   
14b66 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d     pC->nullRow =
14b67 20 72 65 73 3b 0a 20 20 20 20 20 20 70 43 2d 3e   res;.      pC->
14b68 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
14b69 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d  CHE_STALE;.    }
14b6a 0a 20 20 20 20 69 66 28 20 72 65 73 3d 3d 30 20  .    if( res==0 
14b6b 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
14b6c 70 2d 3e 70 32 20 2d 20 31 3b 0a 23 69 66 64 65  p->p2 - 1;.#ifde
14b6d 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
14b6e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72      sqlite3_sear
14b6f 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64  ch_count++;.#end
14b70 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  if.    }.  }else
14b71 7b 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f  {.    pC->nullRo
14b72 77 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 43 2d  w = 1;.  }.  pC-
14b73 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
14b74 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  0;.  break;.}../
14b75 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 49 6e 73  * Opcode: IdxIns
14b76 65 72 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a  ert P1 P2 *.**.*
14b77 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65  * The top of the
14b78 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61 20 53   stack holds a S
14b79 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61 64  QL index key mad
14b7a 65 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 74  e using either t
14b7b 68 65 0a 2a 2a 20 4d 61 6b 65 49 64 78 52 65 63  he.** MakeIdxRec
14b7c 20 6f 72 20 4d 61 6b 65 52 65 63 6f 72 64 20 69   or MakeRecord i
14b7d 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 54 68  nstructions.  Th
14b7e 69 73 20 6f 70 63 6f 64 65 20 77 72 69 74 65 73  is opcode writes
14b7f 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e 74   that key.** int
14b80 6f 20 74 68 65 20 69 6e 64 65 78 20 50 31 2e 20  o the index P1. 
14b81 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 65 6e   Data for the en
14b82 74 72 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a  try is nil..**.*
14b83 2a 20 50 32 20 69 73 20 61 20 66 6c 61 67 20 74  * P2 is a flag t
14b84 68 61 74 20 70 72 6f 76 69 64 65 73 20 61 20 68  hat provides a h
14b85 69 6e 74 20 74 6f 20 74 68 65 20 62 2d 74 72 65  int to the b-tre
14b86 65 20 6c 61 79 65 72 20 74 68 61 74 20 74 68 69  e layer that thi
14b87 73 0a 2a 2a 20 69 6e 73 65 72 74 20 69 73 20 6c  s.** insert is l
14b88 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61  ikely to be an a
14b89 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ppend..**.** Thi
14b8a 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e  s instruction on
14b8b 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e 64  ly works for ind
14b8c 69 63 65 73 2e 20 20 54 68 65 20 65 71 75 69 76  ices.  The equiv
14b8d 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  alent instructio
14b8e 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73 20  n.** for tables 
14b8f 69 73 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f  is OP_Insert..*/
14b90 0a 63 61 73 65 20 4f 50 5f 49 64 78 49 6e 73 65  .case OP_IdxInse
14b91 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  rt: {        /* 
14b92 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74  no-push */.  int
14b93 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
14b94 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
14b95 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
14b96 20 61 73 73 65 72 74 28 20 70 54 6f 73 3e 3d 70   assert( pTos>=p
14b97 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 61 73  ->aStack );.  as
14b98 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
14b99 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
14b9a 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
14b9b 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  [i]!=0 );.  asse
14b9c 72 74 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20  rt( pTos->flags 
14b9d 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20  & MEM_Blob );.  
14b9e 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 43  if( (pCrsr = (pC
14b9f 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d   = p->apCsr[i])-
14ba0 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a  >pCursor)!=0 ){.
14ba1 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
14ba2 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  isTable==0 );.  
14ba3 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f    rc = ExpandBlo
14ba4 62 28 70 54 6f 73 29 3b 0a 20 20 20 20 69 66 28  b(pTos);.    if(
14ba5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
14ba6 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79  {.      int nKey
14ba7 20 3d 20 70 54 6f 73 2d 3e 6e 3b 0a 20 20 20 20   = pTos->n;.    
14ba8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b    const char *zK
14ba9 65 79 20 3d 20 70 54 6f 73 2d 3e 7a 3b 0a 20 20  ey = pTos->z;.  
14baa 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14bab 42 74 72 65 65 49 6e 73 65 72 74 28 70 43 72 73  BtreeInsert(pCrs
14bac 72 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 22  r, zKey, nKey, "
14bad 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 32  ", 0, 0, pOp->p2
14bae 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
14baf 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
14bb0 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  eto==0 );.      
14bb1 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
14bb2 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
14bb3 20 20 20 7d 0a 20 20 7d 0a 20 20 52 65 6c 65 61     }.  }.  Relea
14bb4 73 65 28 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73  se(pTos);.  pTos
14bb5 2d 2d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  --;.  break;.}..
14bb6 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 65  /* Opcode: IdxDe
14bb7 6c 65 74 65 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a  lete P1 * *.**.*
14bb8 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65  * The top of the
14bb9 20 73 74 61 63 6b 20 69 73 20 61 6e 20 69 6e 64   stack is an ind
14bba 65 78 20 6b 65 79 20 62 75 69 6c 74 20 75 73 69  ex key built usi
14bbb 6e 67 20 74 68 65 20 65 69 74 68 65 72 20 74 68  ng the either th
14bbc 65 0a 2a 2a 20 4d 61 6b 65 49 64 78 52 65 63 20  e.** MakeIdxRec 
14bbd 6f 72 20 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70  or MakeRecord op
14bbe 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f  codes..** This o
14bbf 70 63 6f 64 65 20 72 65 6d 6f 76 65 73 20 74 68  pcode removes th
14bc0 61 74 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  at entry from th
14bc1 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a 63 61 73 65  e index..*/.case
14bc2 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b   OP_IdxDelete: {
14bc3 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
14bc4 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  sh */.  int i = 
14bc5 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f  pOp->p1;.  Curso
14bc6 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f  r *pC;.  BtCurso
14bc7 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65  r *pCrsr;.  asse
14bc8 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74  rt( pTos>=p->aSt
14bc9 61 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ack );.  assert(
14bca 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d   pTos->flags & M
14bcb 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 61 73 73  EM_Blob );.  ass
14bcc 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
14bcd 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
14bce 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b  ssert( p->apCsr[
14bcf 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  i]!=0 );.  if( (
14bd0 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20 70 2d  pCrsr = (pC = p-
14bd1 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72  >apCsr[i])->pCur
14bd2 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 69  sor)!=0 ){.    i
14bd3 6e 74 20 72 65 73 3b 0a 20 20 20 20 72 63 20 3d  nt res;.    rc =
14bd4 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
14bd5 65 74 6f 28 70 43 72 73 72 2c 20 70 54 6f 73 2d  eto(pCrsr, pTos-
14bd6 3e 7a 2c 20 70 54 6f 73 2d 3e 6e 2c 20 30 2c 20  >z, pTos->n, 0, 
14bd7 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72  &res);.    if( r
14bd8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
14bd9 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
14bda 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
14bdb 65 44 65 6c 65 74 65 28 70 43 72 73 72 29 3b 0a  eDelete(pCrsr);.
14bdc 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
14bdd 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  ( pC->deferredMo
14bde 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70  veto==0 );.    p
14bdf 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
14be0 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
14be1 7d 0a 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73  }.  Release(pTos
14be2 29 3b 0a 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 62  );.  pTos--;.  b
14be3 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
14be4 64 65 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20  de: IdxRowid P1 
14be5 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 75 73 68 20 6f  * *.**.** Push o
14be6 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61 6e  nto the stack an
14be7 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69   integer which i
14be8 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  s the last entry
14be9 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 61   in the record a
14bea 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20  t.** the end of 
14beb 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 70 6f  the index key po
14bec 69 6e 74 65 64 20 74 6f 20 62 79 20 63 75 72 73  inted to by curs
14bed 6f 72 20 50 31 2e 20 20 54 68 69 73 20 69 6e 74  or P1.  This int
14bee 65 67 65 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a  eger should be.*
14bef 2a 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74  * the rowid of t
14bf0 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74  he table entry t
14bf1 6f 20 77 68 69 63 68 20 74 68 69 73 20 69 6e 64  o which this ind
14bf2 65 78 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e  ex entry points.
14bf3 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
14bf4 20 52 6f 77 69 64 2c 20 4d 61 6b 65 49 64 78 52   Rowid, MakeIdxR
14bf5 65 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  ec..*/.case OP_I
14bf6 64 78 52 6f 77 69 64 3a 20 7b 0a 20 20 69 6e 74  dxRowid: {.  int
14bf7 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
14bf8 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
14bf9 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a  .  Cursor *pC;..
14bfa 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
14bfb 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
14bfc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
14bfd 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
14bfe 70 54 6f 73 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e  pTos++;.  pTos->
14bff 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
14c00 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d  ;.  if( (pCrsr =
14c01 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b   (pC = p->apCsr[
14c02 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30  i])->pCursor)!=0
14c03 20 29 7b 0a 20 20 20 20 69 36 34 20 72 6f 77 69   ){.    i64 rowi
14c04 64 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  d;..    assert( 
14c05 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
14c06 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  to==0 );.    ass
14c07 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65  ert( pC->isTable
14c08 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ==0 );.    if( p
14c09 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20  C->nullRow ){.  
14c0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20      pTos->flags 
14c0b 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
14c0c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
14c0d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  = sqlite3VdbeIdx
14c0e 52 6f 77 69 64 28 70 43 72 73 72 2c 20 26 72 6f  Rowid(pCrsr, &ro
14c0f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  wid);.      if( 
14c10 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
14c11 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
14c12 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
14c13 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
14c14 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pTos->flags = ME
14c15 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 54 6f  M_Int;.      pTo
14c16 73 2d 3e 75 2e 69 20 3d 20 72 6f 77 69 64 3b 0a  s->u.i = rowid;.
14c17 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
14c18 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
14c19 20 49 64 78 47 54 20 50 31 20 50 32 20 2a 0a 2a   IdxGT P1 P2 *.*
14c1a 2a 0a 2a 2a 20 54 68 65 20 74 6f 70 20 6f 66 20  *.** The top of 
14c1b 74 68 65 20 73 74 61 63 6b 20 69 73 20 61 6e 20  the stack is an 
14c1c 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74  index entry that
14c1d 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44   omits the ROWID
14c1e 2e 20 20 43 6f 6d 70 61 72 65 0a 2a 2a 20 74 68  .  Compare.** th
14c1f 65 20 74 6f 70 20 6f 66 20 73 74 61 63 6b 20 61  e top of stack a
14c20 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78  gainst the index
14c21 20 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72   that P1 is curr
14c22 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74  ently pointing t
14c23 6f 2e 0a 2a 2a 20 49 67 6e 6f 72 65 20 74 68 65  o..** Ignore the
14c24 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31   ROWID on the P1
14c25 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68   index..**.** Th
14c26 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
14c27 63 6b 20 6d 69 67 68 74 20 68 61 76 65 20 66 65  ck might have fe
14c28 77 65 72 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74  wer columns that
14c29 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   P1..**.** If th
14c2a 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79  e P1 index entry
14c2b 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
14c2c 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
14c2d 73 74 61 63 6b 0a 2a 2a 20 74 68 65 6e 20 6a 75  stack.** then ju
14c2e 6d 70 20 74 6f 20 50 32 2e 20 20 4f 74 68 65 72  mp to P2.  Other
14c2f 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67  wise fall throug
14c30 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e  h to the next in
14c31 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 49 6e  struction..** In
14c32 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 74 68   either case, th
14c33 65 20 73 74 61 63 6b 20 69 73 20 70 6f 70 70 65  e stack is poppe
14c34 64 20 6f 6e 63 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70  d once..*/./* Op
14c35 63 6f 64 65 3a 20 49 64 78 47 45 20 50 31 20 50  code: IdxGE P1 P
14c36 32 20 50 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  2 P3.**.** The t
14c37 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
14c38 69 73 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72  is an index entr
14c39 79 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65  y that omits the
14c3a 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65   ROWID.  Compare
14c3b 0a 2a 2a 20 74 68 65 20 74 6f 70 20 6f 66 20 73  .** the top of s
14c3c 74 61 63 6b 20 61 67 61 69 6e 73 74 20 74 68 65  tack against the
14c3d 20 69 6e 64 65 78 20 74 68 61 74 20 50 31 20 69   index that P1 i
14c3e 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
14c3f 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 49 67 6e 6f  ting to..** Igno
14c40 72 65 20 74 68 65 20 52 4f 57 49 44 20 6f 6e 20  re the ROWID on 
14c41 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a  the P1 index..**
14c42 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 69 6e  .** If the P1 in
14c43 64 65 78 20 65 6e 74 72 79 20 69 73 20 67 72 65  dex entry is gre
14c44 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
14c45 61 6c 20 74 6f 20 74 68 65 20 74 6f 70 20 6f 66  al to the top of
14c46 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 74 68   the stack.** th
14c47 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20  en jump to P2.  
14c48 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74  Otherwise fall t
14c49 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
14c4a 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  xt instruction..
14c4b 2a 2a 20 49 6e 20 65 69 74 68 65 72 20 63 61 73  ** In either cas
14c4c 65 2c 20 74 68 65 20 73 74 61 63 6b 20 69 73 20  e, the stack is 
14c4d 70 6f 70 70 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a  popped once..**.
14c4e 2a 2a 20 49 66 20 50 33 20 69 73 20 74 68 65 20  ** If P3 is the 
14c4f 22 2b 22 20 73 74 72 69 6e 67 20 28 6f 72 20 61  "+" string (or a
14c50 6e 79 20 6f 74 68 65 72 20 6e 6f 6e 2d 4e 55 4c  ny other non-NUL
14c51 4c 20 73 74 72 69 6e 67 29 20 74 68 65 6e 20 74  L string) then t
14c52 68 65 0a 2a 2a 20 69 6e 64 65 78 20 74 61 6b 65  he.** index take
14c53 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6f  n from the top o
14c54 66 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 74  f the stack is t
14c55 65 6d 70 6f 72 61 72 69 6c 79 20 69 6e 63 72 65  emporarily incre
14c56 61 73 65 64 20 62 79 0a 2a 2a 20 61 6e 20 65 70  ased by.** an ep
14c57 73 69 6c 6f 6e 20 70 72 69 6f 72 20 74 6f 20 74  silon prior to t
14c58 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20  he comparison.  
14c59 54 68 69 73 20 6d 61 6b 65 20 74 68 65 20 6f 70  This make the op
14c5a 63 6f 64 65 20 77 6f 72 6b 0a 2a 2a 20 6c 69 6b  code work.** lik
14c5b 65 20 49 64 78 47 54 20 65 78 63 65 70 74 20 74  e IdxGT except t
14c5c 68 61 74 20 69 66 20 74 68 65 20 6b 65 79 20 66  hat if the key f
14c5d 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 20 69 73  rom the stack is
14c5e 20 61 20 70 72 65 66 69 78 20 6f 66 0a 2a 2a 20   a prefix of.** 
14c5f 74 68 65 20 6b 65 79 20 69 6e 20 74 68 65 20 63  the key in the c
14c60 75 72 73 6f 72 2c 20 74 68 65 20 72 65 73 75 6c  ursor, the resul
14c61 74 20 69 73 20 66 61 6c 73 65 20 77 68 65 72 65  t is false where
14c62 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 0a 2a  as it would be.*
14c63 2a 20 74 72 75 65 20 77 69 74 68 20 49 64 78 47  * true with IdxG
14c64 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  T..*/./* Opcode:
14c65 20 49 64 78 4c 54 20 50 31 20 50 32 20 50 33 0a   IdxLT P1 P2 P3.
14c66 2a 2a 0a 2a 2a 20 54 68 65 20 74 6f 70 20 6f 66  **.** The top of
14c67 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 61 6e   the stack is an
14c68 20 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61   index entry tha
14c69 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49  t omits the ROWI
14c6a 44 2e 20 20 43 6f 6d 70 61 72 65 0a 2a 2a 20 74  D.  Compare.** t
14c6b 68 65 20 74 6f 70 20 6f 66 20 73 74 61 63 6b 20  he top of stack 
14c6c 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65  against the inde
14c6d 78 20 74 68 61 74 20 50 31 20 69 73 20 63 75 72  x that P1 is cur
14c6e 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
14c6f 74 6f 2e 0a 2a 2a 20 49 67 6e 6f 72 65 20 74 68  to..** Ignore th
14c70 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50  e ROWID on the P
14c71 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49  1 index..**.** I
14c72 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 65  f the P1 index e
14c73 6e 74 72 79 20 69 73 20 6c 65 73 73 20 74 68 61  ntry is less tha
14c74 6e 20 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  n  the top of th
14c75 65 20 73 74 61 63 6b 0a 2a 2a 20 74 68 65 6e 20  e stack.** then 
14c76 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 4f 74 68  jump to P2.  Oth
14c77 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f  erwise fall thro
14c78 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
14c79 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
14c7a 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20  In either case, 
14c7b 74 68 65 20 73 74 61 63 6b 20 69 73 20 70 6f 70  the stack is pop
14c7c 70 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  ped once..**.** 
14c7d 49 66 20 50 33 20 69 73 20 74 68 65 20 22 2b 22  If P3 is the "+"
14c7e 20 73 74 72 69 6e 67 20 28 6f 72 20 61 6e 79 20   string (or any 
14c7f 6f 74 68 65 72 20 6e 6f 6e 2d 4e 55 4c 4c 20 73  other non-NULL s
14c80 74 72 69 6e 67 29 20 74 68 65 6e 20 74 68 65 0a  tring) then the.
14c81 2a 2a 20 69 6e 64 65 78 20 74 61 6b 65 6e 20 66  ** index taken f
14c82 72 6f 6d 20 74 68 65 20 74 6f 70 20 6f 66 20 74  rom the top of t
14c83 68 65 20 73 74 61 63 6b 20 69 73 20 74 65 6d 70  he stack is temp
14c84 6f 72 61 72 69 6c 79 20 69 6e 63 72 65 61 73 65  orarily increase
14c85 64 20 62 79 0a 2a 2a 20 61 6e 20 65 70 73 69 6c  d by.** an epsil
14c86 6f 6e 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  on prior to the 
14c87 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69  comparison.  Thi
14c88 73 20 6d 61 6b 65 73 20 74 68 65 20 6f 70 63 6f  s makes the opco
14c89 64 65 20 77 6f 72 6b 0a 2a 2a 20 6c 69 6b 65 20  de work.** like 
14c8a 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f  IdxLE..*/.case O
14c8b 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20 20  P_IdxLT:        
14c8c 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
14c8d 63 61 73 65 20 4f 50 5f 49 64 78 47 54 3a 20 20  case OP_IdxGT:  
14c8e 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
14c8f 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64  sh */.case OP_Id
14c90 78 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  xGE: {        /*
14c91 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e   no-push */.  in
14c92 74 20 69 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  t i= pOp->p1;.  
14c93 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61  Cursor *pC;..  a
14c94 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
14c95 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
14c96 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73   assert( p->apCs
14c97 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73  r[i]!=0 );.  ass
14c98 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53  ert( pTos>=p->aS
14c99 74 61 63 6b 20 29 3b 0a 20 20 69 66 28 20 28 70  tack );.  if( (p
14c9a 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
14c9b 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a  ->pCursor!=0 ){.
14c9c 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 0a 20      int res;. . 
14c9d 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 2d     assert( pTos-
14c9e 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
14c9f 62 20 29 3b 20 20 2f 2a 20 43 72 65 61 74 65 64  b );  /* Created
14ca0 20 75 73 69 6e 67 20 4f 50 5f 4d 61 6b 65 52 65   using OP_MakeRe
14ca1 63 6f 72 64 20 2a 2f 0a 20 20 20 20 61 73 73 65  cord */.    asse
14ca2 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64  rt( pC->deferred
14ca3 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20  Moveto==0 );.   
14ca4 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 54 6f 73   ExpandBlob(pTos
14ca5 29 3b 0a 20 20 20 20 2a 70 43 2d 3e 70 49 6e 63  );.    *pC->pInc
14ca6 72 4b 65 79 20 3d 20 70 4f 70 2d 3e 70 33 21 3d  rKey = pOp->p3!=
14ca7 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  0;.    assert( p
14ca8 4f 70 2d 3e 70 33 3d 3d 30 20 7c 7c 20 70 4f 70  Op->p3==0 || pOp
14ca9 2d 3e 6f 70 63 6f 64 65 21 3d 4f 50 5f 49 64 78  ->opcode!=OP_Idx
14caa 47 54 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  GT );.    rc = s
14cab 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79  qlite3VdbeIdxKey
14cac 43 6f 6d 70 61 72 65 28 70 43 2c 20 70 54 6f 73  Compare(pC, pTos
14cad 2d 3e 6e 2c 20 28 75 38 2a 29 70 54 6f 73 2d 3e  ->n, (u8*)pTos->
14cae 7a 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 2a 70  z, &res);.    *p
14caf 43 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 30 3b  C->pIncrKey = 0;
14cb0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
14cb1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
14cb2 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
14cb3 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
14cb4 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20  ==OP_IdxLT ){.  
14cb5 20 20 20 20 72 65 73 20 3d 20 2d 72 65 73 3b 0a      res = -res;.
14cb6 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f      }else if( pO
14cb7 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64  p->opcode==OP_Id
14cb8 78 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 73  xGE ){.      res
14cb9 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ++;.    }.    if
14cba 28 20 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20  ( res>0 ){.     
14cbb 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
14cbc 31 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  1 ;.    }.  }.  
14cbd 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20  Release(pTos);. 
14cbe 20 70 54 6f 73 2d 2d 3b 0a 20 20 62 72 65 61 6b   pTos--;.  break
14cbf 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
14cc0 44 65 73 74 72 6f 79 20 50 31 20 50 32 20 2a 0a  Destroy P1 P2 *.
14cc1 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20  **.** Delete an 
14cc2 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 20  entire database 
14cc3 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77  table or index w
14cc4 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69  hose root page i
14cc5 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  n the database.*
14cc6 2a 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20  * file is given 
14cc7 62 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  by P1..**.** The
14cc8 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 65 73   table being des
14cc9 74 72 6f 79 65 64 20 69 73 20 69 6e 20 74 68 65  troyed is in the
14cca 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
14ccb 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20 20 49  ile if P2==0.  I
14ccc 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20  f.** P2==1 then 
14ccd 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
14cce 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20  clear is in the 
14ccf 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
14cd0 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20  se file.** that 
14cd1 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  is used to store
14cd2 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 75   tables create u
14cd3 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50  sing CREATE TEMP
14cd4 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a  ORARY TABLE..**.
14cd5 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d  ** If AUTOVACUUM
14cd6 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68 65 6e   is enabled then
14cd7 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
14cd8 74 68 61 74 20 61 6e 6f 74 68 65 72 20 72 6f 6f  that another roo
14cd9 74 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20  t page.** might 
14cda 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68  be moved into th
14cdb 65 20 6e 65 77 6c 79 20 64 65 6c 65 74 65 64 20  e newly deleted 
14cdc 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f 72 64  root page in ord
14cdd 65 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c 0a 2a  er to keep all.*
14cde 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 63 6f 6e  * root pages con
14cdf 74 69 67 75 6f 75 73 20 61 74 20 74 68 65 20 62  tiguous at the b
14ce0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
14ce1 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 66  database.  The f
14ce2 6f 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 65 20 6f  ormer.** value o
14ce3 66 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  f the root page 
14ce4 74 68 61 74 20 6d 6f 76 65 64 20 2d 20 69 74 73  that moved - its
14ce5 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 74 68   value before th
14ce6 65 20 6d 6f 76 65 20 6f 63 63 75 72 72 65 64 20  e move occurred 
14ce7 2d 0a 2a 2a 20 69 73 20 70 75 73 68 65 64 20 6f  -.** is pushed o
14ce8 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 20 20  nto the stack.  
14ce9 49 66 20 6e 6f 20 70 61 67 65 20 6d 6f 76 65 6d  If no page movem
14cea 65 6e 74 20 77 61 73 20 72 65 71 75 69 72 65 64  ent was required
14ceb 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65   (because.** the
14cec 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f   table being dro
14ced 70 70 65 64 20 77 61 73 20 61 6c 72 65 61 64 79  pped was already
14cee 20 74 68 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e   the last one in
14cef 20 74 68 65 20 64 61 74 61 62 61 73 65 29 20 74   the database) t
14cf0 68 65 6e 0a 2a 2a 20 61 20 7a 65 72 6f 20 69 73  hen.** a zero is
14cf1 20 70 75 73 68 65 64 20 6f 6e 74 6f 20 74 68 65   pushed onto the
14cf2 20 73 74 61 63 6b 2e 20 20 49 66 20 41 55 54 4f   stack.  If AUTO
14cf3 56 41 43 55 55 4d 20 69 73 20 64 69 73 61 62 6c  VACUUM is disabl
14cf4 65 64 0a 2a 2a 20 74 68 65 6e 20 61 20 7a 65 72  ed.** then a zer
14cf5 6f 20 69 73 20 70 75 73 68 65 64 20 6f 6e 74 6f  o is pushed onto
14cf6 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a   the stack..**.*
14cf7 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 6c 65 61  * See also: Clea
14cf8 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 73  r.*/.case OP_Des
14cf9 74 72 6f 79 3a 20 7b 0a 20 20 69 6e 74 20 69 4d  troy: {.  int iM
14cfa 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74  oved;.  int iCnt
14cfb 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
14cfc 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
14cfd 4c 45 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65  LE.  Vdbe *pVdbe
14cfe 3b 0a 20 20 69 43 6e 74 20 3d 20 30 3b 0a 20 20  ;.  iCnt = 0;.  
14cff 66 6f 72 28 70 56 64 62 65 3d 64 62 2d 3e 70 56  for(pVdbe=db->pV
14d00 64 62 65 3b 20 70 56 64 62 65 3b 20 70 56 64 62  dbe; pVdbe; pVdb
14d01 65 3d 70 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b  e=pVdbe->pNext){
14d02 0a 20 20 20 20 69 66 28 20 70 56 64 62 65 2d 3e  .    if( pVdbe->
14d03 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
14d04 43 5f 52 55 4e 20 26 26 20 70 56 64 62 65 2d 3e  C_RUN && pVdbe->
14d05 69 6e 56 74 61 62 4d 65 74 68 6f 64 3c 32 20 26  inVtabMethod<2 &
14d06 26 20 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29  & pVdbe->pc>=0 )
14d07 7b 0a 20 20 20 20 20 20 69 43 6e 74 2b 2b 3b 0a  {.      iCnt++;.
14d08 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a      }.  }.#else.
14d09 20 20 69 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74    iCnt = db->act
14d0a 69 76 65 56 64 62 65 43 6e 74 3b 0a 23 65 6e 64  iveVdbeCnt;.#end
14d0b 69 66 0a 20 20 69 66 28 20 69 43 6e 74 3e 31 20  if.  if( iCnt>1 
14d0c 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
14d0d 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 65 6c  TE_LOCKED;.  }el
14d0e 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
14d0f 69 43 6e 74 3d 3d 31 20 29 3b 0a 20 20 20 20 61  iCnt==1 );.    a
14d10 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65  ssert( (p->btree
14d11 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e  Mask & (1<<pOp->
14d12 70 32 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72  p2))!=0 );.    r
14d13 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
14d14 44 72 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44  DropTable(db->aD
14d15 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20  b[pOp->p2].pBt, 
14d16 70 4f 70 2d 3e 70 31 2c 20 26 69 4d 6f 76 65 64  pOp->p1, &iMoved
14d17 29 3b 0a 20 20 20 20 70 54 6f 73 2b 2b 3b 0a 20  );.    pTos++;. 
14d18 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d     pTos->flags =
14d19 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 54   MEM_Int;.    pT
14d1a 6f 73 2d 3e 75 2e 69 20 3d 20 69 4d 6f 76 65 64  os->u.i = iMoved
14d1b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
14d1c 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
14d1d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
14d1e 49 54 45 5f 4f 4b 20 26 26 20 69 4d 6f 76 65 64  ITE_OK && iMoved
14d1f 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=0 ){.      sql
14d20 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65  ite3RootPageMove
14d21 64 28 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  d(&db->aDb[pOp->
14d22 70 32 5d 2c 20 69 4d 6f 76 65 64 2c 20 70 4f 70  p2], iMoved, pOp
14d23 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  ->p1);.    }.#en
14d24 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  dif.  }.  break;
14d25 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
14d26 6c 65 61 72 20 50 31 20 50 32 20 2a 0a 2a 2a 0a  lear P1 P2 *.**.
14d27 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f  ** Delete all co
14d28 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61  ntents of the da
14d29 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
14d2a 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74  index whose root
14d2b 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20   page.** in the 
14d2c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
14d2d 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 20 42   given by P1.  B
14d2e 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74 72  ut, unlike Destr
14d2f 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65  oy, do not.** re
14d30 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 6f  move the table o
14d31 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65  r index from the
14d32 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
14d33 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
14d34 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 20 69  being clear is i
14d35 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
14d36 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d 3d  ase file if P2==
14d37 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20  0.  If.** P2==1 
14d38 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74  then the table t
14d39 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e  o be clear is in
14d3a 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
14d3b 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20  atabase file.** 
14d3c 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20  that is used to 
14d3d 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65  store tables cre
14d3e 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 45  ate using CREATE
14d3f 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45   TEMPORARY TABLE
14d40 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
14d41 3a 20 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73  : Destroy.*/.cas
14d42 65 20 4f 50 5f 43 6c 65 61 72 3a 20 7b 20 20 20  e OP_Clear: {   
14d43 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
14d44 2a 2f 0a 0a 20 20 2f 2a 20 46 6f 72 20 63 6f 6e  */..  /* For con
14d45 73 69 73 74 65 6e 63 79 20 77 69 74 68 20 74 68  sistency with th
14d46 65 20 77 61 79 20 6f 74 68 65 72 20 66 65 61 74  e way other feat
14d47 75 72 65 73 20 6f 66 20 53 51 4c 69 74 65 20 6f  ures of SQLite o
14d48 70 65 72 61 74 65 0a 20 20 2a 2a 20 77 69 74 68  perate.  ** with
14d49 20 61 20 74 72 75 6e 63 61 74 65 2c 20 77 65 20   a truncate, we 
14d4a 77 69 6c 6c 20 61 6c 73 6f 20 73 6b 69 70 20 74  will also skip t
14d4b 68 65 20 75 70 64 61 74 65 20 63 61 6c 6c 62 61  he update callba
14d4c 63 6b 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20  ck..  */.#if 0. 
14d4d 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
14d4e 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70  ->aDb[pOp->p2].p
14d4f 42 74 3b 0a 20 20 69 66 28 20 64 62 2d 3e 78 55  Bt;.  if( db->xU
14d50 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26  pdateCallback &&
14d51 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20   pOp->p3 ){.    
14d52 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
14d53 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70  = db->aDb[pOp->p
14d54 32 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f  2].zName;.    co
14d55 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d  nst char *zTbl =
14d56 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 42 74   pOp->p3;.    Bt
14d57 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 30  Cursor *pCur = 0
14d58 3b 0a 20 20 20 20 69 6e 74 20 66 69 6e 20 3d 20  ;.    int fin = 
14d59 30 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  0;..    rc = sql
14d5a 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28  ite3BtreeCursor(
14d5b 70 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c  pBt, pOp->p1, 0,
14d5c 20 30 2c 20 30 2c 20 26 70 43 75 72 29 3b 0a 20   0, 0, &pCur);. 
14d5d 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
14d5e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
14d5f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
14d60 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20  error;.    }.   
14d61 20 66 6f 72 28 0a 20 20 20 20 20 20 72 63 3d 73   for(.      rc=s
14d62 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74  qlite3BtreeFirst
14d63 28 70 43 75 72 2c 20 26 66 69 6e 29 3b 20 0a 20  (pCur, &fin); . 
14d64 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f       rc==SQLITE_
14d65 4f 4b 20 26 26 20 21 66 69 6e 3b 20 0a 20 20 20  OK && !fin; .   
14d66 20 20 20 72 63 3d 73 71 6c 69 74 65 33 42 74 72     rc=sqlite3Btr
14d67 65 65 4e 65 78 74 28 70 43 75 72 2c 20 26 66 69  eeNext(pCur, &fi
14d68 6e 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  n).    ){.      
14d69 69 36 34 20 69 4b 65 79 3b 0a 20 20 20 20 20 20  i64 iKey;.      
14d6a 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
14d6b 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26  eKeySize(pCur, &
14d6c 69 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28  iKey);.      if(
14d6d 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 62   rc ){.        b
14d6e 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
14d6f 20 20 20 20 69 4b 65 79 20 3d 20 6b 65 79 54 6f      iKey = keyTo
14d70 49 6e 74 28 69 4b 65 79 29 3b 0a 20 20 20 20 20  Int(iKey);.     
14d71 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c   db->xUpdateCall
14d72 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65  back(db->pUpdate
14d73 41 72 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  Arg, SQLITE_DELE
14d74 54 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69  TE, zDb, zTbl, i
14d75 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Key);.    }.    
14d76 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
14d77 65 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20  eCursor(pCur);. 
14d78 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
14d79 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
14d7a 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
14d7b 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  error;.    }.  }
14d7c 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74  .#endif.  assert
14d7d 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
14d7e 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21  & (1<<pOp->p2))!
14d7f 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  =0 );.  rc = sql
14d80 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61  ite3BtreeClearTa
14d81 62 6c 65 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d  ble(db->aDb[pOp-
14d82 3e 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70  >p2].pBt, pOp->p
14d83 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  1);.  break;.}..
14d84 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74  /* Opcode: Creat
14d85 65 54 61 62 6c 65 20 50 31 20 2a 20 2a 0a 2a 2a  eTable P1 * *.**
14d86 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
14d87 65 77 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  ew table in the 
14d88 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
14d89 6c 65 20 69 66 20 50 32 3d 3d 30 20 6f 72 20 69  le if P2==0 or i
14d8a 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61  n the.** auxilia
14d8b 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ry database file
14d8c 20 69 66 20 50 32 3d 3d 31 2e 20 20 50 75 73 68   if P2==1.  Push
14d8d 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
14d8e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 6f 6f 74  .** for the root
14d8f 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77   page of the new
14d90 20 74 61 62 6c 65 20 6f 6e 74 6f 20 74 68 65 20   table onto the 
14d91 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  stack..**.** The
14d92 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
14d93 65 65 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20  een a table and 
14d94 61 6e 20 69 6e 64 65 78 20 69 73 20 74 68 69 73  an index is this
14d95 3a 20 20 41 20 74 61 62 6c 65 20 6d 75 73 74 0a  :  A table must.
14d96 2a 2a 20 68 61 76 65 20 61 20 34 2d 62 79 74 65  ** have a 4-byte
14d97 20 69 6e 74 65 67 65 72 20 6b 65 79 20 61 6e 64   integer key and
14d98 20 63 61 6e 20 68 61 76 65 20 61 72 62 69 74 72   can have arbitr
14d99 61 72 79 20 64 61 74 61 2e 20 20 41 6e 20 69 6e  ary data.  An in
14d9a 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72  dex.** has an ar
14d9b 62 69 74 72 61 72 79 20 6b 65 79 20 62 75 74 20  bitrary key but 
14d9c 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53  no data..**.** S
14d9d 65 65 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 49  ee also: CreateI
14d9e 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ndex.*/./* Opcod
14d9f 65 3a 20 43 72 65 61 74 65 49 6e 64 65 78 20 50  e: CreateIndex P
14da0 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f  1 * *.**.** Allo
14da1 63 61 74 65 20 61 20 6e 65 77 20 69 6e 64 65 78  cate a new index
14da2 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
14da3 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 32  abase file if P2
14da4 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a  ==0 or in the.**
14da5 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
14da6 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d 3d  ase file if P2==
14da7 31 2e 20 20 50 75 73 68 20 74 68 65 20 70 61 67  1.  Push the pag
14da8 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 0a  e number of the.
14da9 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  ** root page of 
14daa 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 6f 6e  the new index on
14dab 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  to the stack..**
14dac 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74  .** See document
14dad 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61  ation on OP_Crea
14dae 74 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69  teTable for addi
14daf 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
14db0 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  on..*/.case OP_C
14db1 72 65 61 74 65 49 6e 64 65 78 3a 0a 63 61 73 65  reateIndex:.case
14db2 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 3a   OP_CreateTable:
14db3 20 7b 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20   {.  int pgno;. 
14db4 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62   int flags;.  Db
14db5 20 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28   *pDb;.  assert(
14db6 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
14db7 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29  Op->p1<db->nDb )
14db8 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
14db9 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c  btreeMask & (1<<
14dba 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a  pOp->p1))!=0 );.
14dbb 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
14dbc 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
14dbd 65 72 74 28 20 70 44 62 2d 3e 70 42 74 21 3d 30  ert( pDb->pBt!=0
14dbe 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f   );.  if( pOp->o
14dbf 70 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65  pcode==OP_Create
14dc0 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20  Table ){.    /* 
14dc1 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e  flags = BTREE_IN
14dc2 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 66 6c 61  TKEY; */.    fla
14dc3 67 73 20 3d 20 42 54 52 45 45 5f 4c 45 41 46 44  gs = BTREE_LEAFD
14dc4 41 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45 59  ATA|BTREE_INTKEY
14dc5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  ;.  }else{.    f
14dc6 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52  lags = BTREE_ZER
14dc7 4f 44 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20  ODATA;.  }.  rc 
14dc8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72  = sqlite3BtreeCr
14dc9 65 61 74 65 54 61 62 6c 65 28 70 44 62 2d 3e 70  eateTable(pDb->p
14dca 42 74 2c 20 26 70 67 6e 6f 2c 20 66 6c 61 67 73  Bt, &pgno, flags
14dcb 29 3b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 69  );.  pTos++;.  i
14dcc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
14dcd 20 29 7b 0a 20 20 20 20 70 54 6f 73 2d 3e 75 2e   ){.    pTos->u.
14dce 69 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 70 54  i = pgno;.    pT
14dcf 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  os->flags = MEM_
14dd0 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Int;.  }else{.  
14dd1 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
14dd2 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20  MEM_Null;.  }.  
14dd3 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
14dd4 6f 64 65 3a 20 50 61 72 73 65 53 63 68 65 6d 61  ode: ParseSchema
14dd5 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20   P1 P2 P3.**.** 
14dd6 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 61  Read and parse a
14dd7 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ll entries from 
14dd8 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45  the SQLITE_MASTE
14dd9 52 20 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62  R table of datab
14dda 61 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d  ase P1.** that m
14ddb 61 74 63 68 20 74 68 65 20 57 48 45 52 45 20 63  atch the WHERE c
14ddc 6c 61 75 73 65 20 50 33 2e 20 20 50 32 20 69 73  lause P3.  P2 is
14ddd 20 74 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61   the "force" fla
14dde 67 2e 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a  g.   Always do.*
14ddf 2a 20 74 68 65 20 70 61 72 73 69 6e 67 20 69 66  * the parsing if
14de0 20 50 32 20 69 73 20 74 72 75 65 2e 20 20 49 66   P2 is true.  If
14de1 20 50 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68   P2 is false, th
14de2 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
14de3 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66  is a.** no-op if
14de4 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e   the schema is n
14de5 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61  ot currently loa
14de6 64 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ded.  In other w
14de7 6f 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69  ords, if P2.** i
14de8 73 20 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c  s false, the SQL
14de9 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65  ITE_MASTER table
14dea 20 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20   is only parsed 
14deb 69 66 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  if the rest of t
14dec 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20  he.** schema is 
14ded 61 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69  already loaded i
14dee 6e 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74  nto the symbol t
14def 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  able..**.** This
14df0 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
14df1 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 72  the parser to cr
14df2 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75  eate a new virtu
14df3 61 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74  al machine,.** t
14df4 68 65 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77  hen runs the new
14df5 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
14df6 2e 20 20 49 74 20 69 73 20 74 68 75 73 20 61 20  .  It is thus a 
14df7 72 65 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65  reentrant opcode
14df8 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72  ..*/.case OP_Par
14df9 73 65 53 63 68 65 6d 61 3a 20 7b 20 20 20 20 20  seSchema: {     
14dfa 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
14dfb 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20  .  char *zSql;. 
14dfc 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e   int iDb = pOp->
14dfd 70 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  p1;.  const char
14dfe 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 49 6e 69   *zMaster;.  Ini
14dff 74 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a  tData initData;.
14e00 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
14e01 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
14e02 20 29 3b 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e   );.  if( !pOp->
14e03 70 32 20 26 26 20 21 44 62 48 61 73 50 72 6f 70  p2 && !DbHasProp
14e04 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42  erty(db, iDb, DB
14e05 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29  _SchemaLoaded) )
14e06 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  {.    break;.  }
14e07 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 53 43 48  .  zMaster = SCH
14e08 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
14e09 20 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20    initData.db = 
14e0a 64 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 69  db;.  initData.i
14e0b 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  Db = pOp->p1;.  
14e0c 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73  initData.pzErrMs
14e0d 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b  g = &p->zErrMsg;
14e0e 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
14e0f 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20 20  3MPrintf(db,.   
14e10 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20    "SELECT name, 
14e11 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52  rootpage, sql FR
14e12 4f 4d 20 27 25 71 27 2e 25 73 20 57 48 45 52 45  OM '%q'.%s WHERE
14e13 20 25 73 22 2c 0a 20 20 20 20 20 64 62 2d 3e 61   %s",.     db->a
14e14 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a  Db[iDb].zName, z
14e15 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 33 29  Master, pOp->p3)
14e16 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20  ;.  if( zSql==0 
14e17 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
14e18 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
14e19 66 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74 28  f(db);.  assert(
14e1a 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d   db->init.busy==
14e1b 30 20 29 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e  0 );.  db->init.
14e1c 62 75 73 79 20 3d 20 31 3b 0a 20 20 61 73 73 65  busy = 1;.  asse
14e1d 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
14e1e 61 69 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d 20  ailed );.  rc = 
14e1f 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
14e20 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e   zSql, sqlite3In
14e21 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69  itCallback, &ini
14e22 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 69 66 28  tData, 0);.  if(
14e23 20 72 63 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52   rc==SQLITE_ABOR
14e24 54 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61 74  T ) rc = initDat
14e25 61 2e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  a.rc;.  sqlite3_
14e26 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 64 62  free(zSql);.  db
14e27 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b  ->init.busy = 0;
14e28 0a 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  .  sqlite3Safety
14e29 4f 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63  On(db);.  if( rc
14e2a 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
14e2b 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
14e2c 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 20  m;.  }.  break; 
14e2d 20 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65   .}..#if !define
14e2e 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  d(SQLITE_OMIT_AN
14e2f 41 4c 59 5a 45 29 20 26 26 20 21 64 65 66 69 6e  ALYZE) && !defin
14e30 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ed(SQLITE_OMIT_P
14e31 41 52 53 45 52 29 0a 2f 2a 20 4f 70 63 6f 64 65  ARSER)./* Opcode
14e32 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 50  : LoadAnalysis P
14e33 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64  1 * *.**.** Read
14e34 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
14e35 31 20 74 61 62 6c 65 20 66 6f 72 20 64 61 74 61  1 table for data
14e36 62 61 73 65 20 50 31 20 61 6e 64 20 6c 6f 61 64  base P1 and load
14e37 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20   the content.** 
14e38 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 69 6e  of that table in
14e39 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  to the internal 
14e3a 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65  index hash table
14e3b 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75  .  This will cau
14e3c 73 65 0a 2a 2a 20 74 68 65 20 61 6e 61 6c 79 73  se.** the analys
14e3d 69 73 20 74 6f 20 62 65 20 75 73 65 64 20 77 68  is to be used wh
14e3e 65 6e 20 70 72 65 70 61 72 69 6e 67 20 61 6c 6c  en preparing all
14e3f 20 73 75 62 73 65 71 75 65 6e 74 20 71 75 65 72   subsequent quer
14e40 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ies..*/.case OP_
14e41 4c 6f 61 64 41 6e 61 6c 79 73 69 73 3a 20 7b 20  LoadAnalysis: { 
14e42 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73         /* no-pus
14e43 68 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 20 3d  h */.  int iDb =
14e44 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65   pOp->p1;.  asse
14e45 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
14e46 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 72  b<db->nDb );.  r
14e47 63 20 3d 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  c = sqlite3Analy
14e48 73 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29  sisLoad(db, iDb)
14e49 3b 0a 20 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23  ;.  break;  .}.#
14e4a 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
14e4b 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  d(SQLITE_OMIT_AN
14e4c 41 4c 59 5a 45 29 20 26 26 20 21 64 65 66 69 6e  ALYZE) && !defin
14e4d 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ed(SQLITE_OMIT_P
14e4e 41 52 53 45 52 29 20 20 2a 2f 0a 0a 2f 2a 20 4f  ARSER)  */../* O
14e4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 61 62 6c 65  pcode: DropTable
14e50 20 50 31 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 52   P1 * P3.**.** R
14e51 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e  emove the intern
14e52 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64  al (in-memory) d
14e53 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74  ata structures t
14e54 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20  hat describe.** 
14e55 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20  the table named 
14e56 50 33 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  P3 in database P
14e57 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c  1.  This is call
14e58 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65  ed after a table
14e59 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69  .** is dropped i
14e5a 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
14e5b 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
14e5c 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
14e5d 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e  he.** schema con
14e5e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61  sistent with wha
14e5f 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f  t is on disk..*/
14e60 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62  .case OP_DropTab
14e61 6c 65 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  le: {        /* 
14e62 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 73 71 6c  no-push */.  sql
14e63 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
14e64 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 4f 70  eteTable(db, pOp
14e65 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 33 29 3b 0a  ->p1, pOp->p3);.
14e66 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
14e67 70 63 6f 64 65 3a 20 44 72 6f 70 49 6e 64 65 78  pcode: DropIndex
14e68 20 50 31 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 52   P1 * P3.**.** R
14e69 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e  emove the intern
14e6a 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64  al (in-memory) d
14e6b 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74  ata structures t
14e6c 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20  hat describe.** 
14e6d 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 20  the index named 
14e6e 50 33 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  P3 in database P
14e6f 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c  1.  This is call
14e70 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 65  ed after an inde
14e71 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20  x.** is dropped 
14e72 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70  in order to keep
14e73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65   the internal re
14e74 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
14e75 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f  the.** schema co
14e76 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68  nsistent with wh
14e77 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a  at is on disk..*
14e78 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49 6e  /.case OP_DropIn
14e79 64 65 78 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  dex: {        /*
14e7a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 73 71   no-push */.  sq
14e7b 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
14e7c 6c 65 74 65 49 6e 64 65 78 28 64 62 2c 20 70 4f  leteIndex(db, pO
14e7d 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 33 29 3b  p->p1, pOp->p3);
14e7e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
14e7f 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67  Opcode: DropTrig
14e80 67 65 72 20 50 31 20 2a 20 50 33 0a 2a 2a 0a 2a  ger P1 * P3.**.*
14e81 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74  * Remove the int
14e82 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79  ernal (in-memory
14e83 29 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  ) data structure
14e84 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a  s that describe.
14e85 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 6e  ** the trigger n
14e86 61 6d 65 64 20 50 33 20 69 6e 20 64 61 74 61 62  amed P3 in datab
14e87 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 69 73  ase P1.  This is
14e88 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20   called after a 
14e89 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 64 72  trigger.** is dr
14e8a 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74  opped in order t
14e8b 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72  o keep the inter
14e8c 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69  nal representati
14e8d 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68  on of the.** sch
14e8e 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77  ema consistent w
14e8f 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64  ith what is on d
14e90 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  isk..*/.case OP_
14e91 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b 20 20  DropTrigger: {  
14e92 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
14e93 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c   */.  sqlite3Unl
14e94 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67  inkAndDeleteTrig
14e95 67 65 72 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c  ger(db, pOp->p1,
14e96 20 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 62 72 65   pOp->p3);.  bre
14e97 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20  ak;.}...#ifndef 
14e98 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
14e99 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f  GRITY_CHECK./* O
14e9a 70 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79  pcode: Integrity
14e9b 43 6b 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  Ck P1 P2 *.**.**
14e9c 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20   Do an analysis 
14e9d 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79  of the currently
14e9e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20   open database. 
14e9f 20 50 75 73 68 20 6f 6e 74 6f 20 74 68 65 0a 2a   Push onto the.*
14ea0 2a 20 73 74 61 63 6b 20 74 68 65 20 74 65 78 74  * stack the text
14ea1 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73   of an error mes
14ea2 73 61 67 65 20 64 65 73 63 72 69 62 69 6e 67 20  sage describing 
14ea3 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a  any problems..**
14ea4 20 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20   If no problems 
14ea5 61 72 65 20 66 6f 75 6e 64 2c 20 70 75 73 68 20  are found, push 
14ea6 61 20 4e 55 4c 4c 20 6f 6e 74 6f 20 74 68 65 20  a NULL onto the 
14ea7 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  stack..**.** P1 
14ea8 69 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  is the address o
14ea9 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  f a memory cell 
14eaa 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
14eab 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d  e maximum.** num
14eac 62 65 72 20 6f 66 20 61 6c 6c 6f 77 65 64 20 65  ber of allowed e
14ead 72 72 6f 72 73 2e 20 20 41 74 20 6d 6f 73 74 20  rrors.  At most 
14eae 6d 65 6d 5b 50 31 5d 20 65 72 72 6f 72 73 20 77  mem[P1] errors w
14eaf 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e  ill be reported.
14eb0 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
14eb1 64 73 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73  ds, the analysis
14eb2 20 73 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61   stops as soon a
14eb3 73 20 6d 65 6d 5b 50 31 5d 20 65 72 72 6f 72 73  s mem[P1] errors
14eb4 20 61 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20   are .** seen.  
14eb5 4d 65 6d 5b 50 31 5d 20 69 73 20 75 70 64 61 74  Mem[P1] is updat
14eb6 65 64 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62  ed with the numb
14eb7 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d  er of errors rem
14eb8 61 69 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  aining..**.** Th
14eb9 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  e root page numb
14eba 65 72 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65  ers of all table
14ebb 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
14ebc 65 20 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a  e are integer.**
14ebd 20 76 61 6c 75 65 73 20 6f 6e 20 74 68 65 20 73   values on the s
14ebe 74 61 63 6b 2e 20 20 54 68 69 73 20 6f 70 63 6f  tack.  This opco
14ebf 64 65 20 70 75 6c 6c 73 20 61 73 20 6d 61 6e 79  de pulls as many
14ec0 20 69 6e 74 65 67 65 72 73 20 61 73 20 69 74 0a   integers as it.
14ec1 2a 2a 20 63 61 6e 20 6f 66 66 20 6f 66 20 74 68  ** can off of th
14ec2 65 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65 73  e stack and uses
14ec3 20 74 68 6f 73 65 20 6e 75 6d 62 65 72 73 20 61   those numbers a
14ec4 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 73  s the root pages
14ec5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73  ..**.** If P2 is
14ec6 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20 63   not zero, the c
14ec7 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e 20  heck is done on 
14ec8 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61  the auxiliary da
14ec9 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 2c 20  tabase.** file, 
14eca 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61 74  not the main dat
14ecb 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
14ecc 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73  * This opcode is
14ecd 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
14ece 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69 74 79  nt the integrity
14ecf 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e 0a 2a  _check pragma..*
14ed0 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 72  /.case OP_Integr
14ed1 69 74 79 43 6b 3a 20 7b 0a 20 20 69 6e 74 20 6e  ityCk: {.  int n
14ed2 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 2a 61 52 6f  Root;.  int *aRo
14ed3 6f 74 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69  ot;.  int j;.  i
14ed4 6e 74 20 6e 45 72 72 3b 0a 20 20 63 68 61 72 20  nt nErr;.  char 
14ed5 2a 7a 3b 0a 20 20 4d 65 6d 20 2a 70 6e 45 72 72  *z;.  Mem *pnErr
14ed6 3b 0a 0a 20 20 66 6f 72 28 6e 52 6f 6f 74 3d 30  ;..  for(nRoot=0
14ed7 3b 20 26 70 54 6f 73 5b 2d 6e 52 6f 6f 74 5d 3e  ; &pTos[-nRoot]>
14ed8 3d 70 2d 3e 61 53 74 61 63 6b 3b 20 6e 52 6f 6f  =p->aStack; nRoo
14ed9 74 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 70  t++){.    if( (p
14eda 54 6f 73 5b 2d 6e 52 6f 6f 74 5d 2e 66 6c 61 67  Tos[-nRoot].flag
14edb 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20  s & MEM_Int)==0 
14edc 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 61  ) break;.  }.  a
14edd 73 73 65 72 74 28 20 6e 52 6f 6f 74 3e 30 20 29  ssert( nRoot>0 )
14ede 3b 0a 20 20 61 52 6f 6f 74 20 3d 20 73 71 6c 69  ;.  aRoot = sqli
14edf 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
14ee0 6f 66 28 69 6e 74 29 2a 28 6e 52 6f 6f 74 2b 31  of(int)*(nRoot+1
14ee1 29 20 29 3b 0a 20 20 69 66 28 20 61 52 6f 6f 74  ) );.  if( aRoot
14ee2 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
14ee3 6d 3b 0a 20 20 6a 20 3d 20 70 4f 70 2d 3e 70 31  m;.  j = pOp->p1
14ee4 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d 30  ;.  assert( j>=0
14ee5 20 26 26 20 6a 3c 70 2d 3e 6e 4d 65 6d 20 29 3b   && j<p->nMem );
14ee6 0a 20 20 70 6e 45 72 72 20 3d 20 26 70 2d 3e 61  .  pnErr = &p->a
14ee7 4d 65 6d 5b 6a 5d 3b 0a 20 20 61 73 73 65 72 74  Mem[j];.  assert
14ee8 28 20 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20  ( (pnErr->flags 
14ee9 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b  & MEM_Int)!=0 );
14eea 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 52  .  for(j=0; j<nR
14eeb 6f 6f 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 61  oot; j++){.    a
14eec 52 6f 6f 74 5b 6a 5d 20 3d 20 28 70 54 6f 73 2d  Root[j] = (pTos-
14eed 6a 29 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 61  j)->u.i;.  }.  a
14eee 52 6f 6f 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 70  Root[j] = 0;.  p
14eef 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c 20 6e  opStack(&pTos, n
14ef0 52 6f 6f 74 29 3b 0a 20 20 70 54 6f 73 2b 2b 3b  Root);.  pTos++;
14ef1 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
14ef2 70 32 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32  p2>=0 && pOp->p2
14ef3 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
14ef4 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
14ef5 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70  ask & (1<<pOp->p
14ef6 32 29 29 21 3d 30 20 29 3b 0a 20 20 7a 20 3d 20  2))!=0 );.  z = 
14ef7 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65  sqlite3BtreeInte
14ef8 67 72 69 74 79 43 68 65 63 6b 28 64 62 2d 3e 61  grityCheck(db->a
14ef9 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c  Db[pOp->p2].pBt,
14efa 20 61 52 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 0a 20   aRoot, nRoot,. 
14efb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14efc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14efd 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 6e 45 72  pnErr->u.i, &nEr
14efe 72 29 3b 0a 20 20 70 6e 45 72 72 2d 3e 75 2e 69  r);.  pnErr->u.i
14eff 20 2d 3d 20 6e 45 72 72 3b 0a 20 20 69 66 28 20   -= nErr;.  if( 
14f00 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61  nErr==0 ){.    a
14f01 73 73 65 72 74 28 20 7a 3d 3d 30 20 29 3b 0a 20  ssert( z==0 );. 
14f02 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d     pTos->flags =
14f03 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c   MEM_Null;.  }el
14f04 73 65 7b 0a 20 20 20 20 70 54 6f 73 2d 3e 7a 20  se{.    pTos->z 
14f05 3d 20 7a 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 6e  = z;.    pTos->n
14f06 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20   = strlen(z);.  
14f07 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
14f08 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 44 79  MEM_Str | MEM_Dy
14f09 6e 20 7c 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20  n | MEM_Term;.  
14f0a 20 20 70 54 6f 73 2d 3e 78 44 65 6c 20 3d 20 30    pTos->xDel = 0
14f0b 3b 0a 20 20 7d 0a 20 20 70 54 6f 73 2d 3e 65 6e  ;.  }.  pTos->en
14f0c 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
14f0d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
14f0e 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 54 6f  angeEncoding(pTo
14f0f 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  s, encoding);.  
14f10 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 52 6f  sqlite3_free(aRo
14f11 6f 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ot);.  break;.}.
14f12 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
14f13 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
14f14 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63  CHECK */../* Opc
14f15 6f 64 65 3a 20 46 69 66 6f 57 72 69 74 65 20 2a  ode: FifoWrite *
14f16 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65   * *.**.** Write
14f17 20 74 68 65 20 69 6e 74 65 67 65 72 20 6f 6e 20   the integer on 
14f18 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
14f19 74 61 63 6b 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  tack.** into the
14f1a 20 46 69 66 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f   Fifo..*/.case O
14f1b 50 5f 46 69 66 6f 57 72 69 74 65 3a 20 7b 20 20  P_FifoWrite: {  
14f1c 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
14f1d 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
14f1e 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b  os>=p->aStack );
14f1f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
14f20 6d 49 6e 74 65 67 65 72 69 66 79 28 70 54 6f 73  mIntegerify(pTos
14f21 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
14f22 56 64 62 65 46 69 66 6f 50 75 73 68 28 26 70 2d  VdbeFifoPush(&p-
14f23 3e 73 46 69 66 6f 2c 20 70 54 6f 73 2d 3e 75 2e  >sFifo, pTos->u.
14f24 69 29 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  i)==SQLITE_NOMEM
14f25 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f   ){.    goto no_
14f26 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  mem;.  }.  asser
14f27 74 28 20 28 70 54 6f 73 2d 3e 66 6c 61 67 73 20  t( (pTos->flags 
14f28 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b  & MEM_Dyn)==0 );
14f29 0a 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 62 72 65  .  pTos--;.  bre
14f2a 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
14f2b 3a 20 46 69 66 6f 52 65 61 64 20 2a 20 50 32 20  : FifoRead * P2 
14f2c 2a 0a 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  *.**.** Attempt 
14f2d 74 6f 20 72 65 61 64 20 61 20 73 69 6e 67 6c 65  to read a single
14f2e 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68   integer from th
14f2f 65 20 46 69 66 6f 0a 2a 2a 20 61 6e 64 20 70 75  e Fifo.** and pu
14f30 73 68 20 69 74 20 6f 6e 74 6f 20 74 68 65 20 73  sh it onto the s
14f31 74 61 63 6b 2e 20 20 49 66 20 74 68 65 20 46 69  tack.  If the Fi
14f32 66 6f 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 70  fo is empty.** p
14f33 75 73 68 20 6e 6f 74 68 69 6e 67 20 62 75 74 20  ush nothing but 
14f34 69 6e 73 74 65 61 64 20 6a 75 6d 70 20 74 6f 20  instead jump to 
14f35 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46  P2..*/.case OP_F
14f36 69 66 6f 52 65 61 64 3a 20 7b 0a 20 20 69 36 34  ifoRead: {.  i64
14f37 20 76 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f   v;.  CHECK_FOR_
14f38 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 69 66 28  INTERRUPT;.  if(
14f39 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f   sqlite3VdbeFifo
14f3a 50 6f 70 28 26 70 2d 3e 73 46 69 66 6f 2c 20 26  Pop(&p->sFifo, &
14f3b 76 29 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  v)==SQLITE_DONE 
14f3c 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
14f3d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p2 - 1;.  }else
14f3e 7b 0a 20 20 20 20 70 54 6f 73 2b 2b 3b 0a 20 20  {.    pTos++;.  
14f3f 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 76 3b    pTos->u.i = v;
14f40 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73  .    pTos->flags
14f41 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 7d 0a   = MEM_Int;.  }.
14f42 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e    break;.}..#ifn
14f43 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14f44 54 52 49 47 47 45 52 0a 2f 2a 20 4f 70 63 6f 64  TRIGGER./* Opcod
14f45 65 3a 20 43 6f 6e 74 65 78 74 50 75 73 68 20 2a  e: ContextPush *
14f46 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a 20 53 61 76 65   * * .**.** Save
14f47 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62   the current Vdb
14f48 65 20 63 6f 6e 74 65 78 74 20 73 75 63 68 20 74  e context such t
14f49 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
14f4a 73 74 6f 72 65 64 20 62 79 20 61 20 43 6f 6e 74  stored by a Cont
14f4b 65 78 74 50 6f 70 0a 2a 2a 20 6f 70 63 6f 64 65  extPop.** opcode
14f4c 2e 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74  . The context st
14f4d 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e  ores the last in
14f4e 73 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65  sert row id, the
14f4f 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 20   last statement 
14f50 63 68 61 6e 67 65 0a 2a 2a 20 63 6f 75 6e 74 2c  change.** count,
14f51 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
14f52 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67   statement chang
14f53 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65  e count..*/.case
14f54 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73 68 3a   OP_ContextPush:
14f55 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d   {        /* no-
14f56 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20  push */.  int i 
14f57 3d 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  = p->contextStac
14f58 6b 54 6f 70 2b 2b 3b 0a 20 20 43 6f 6e 74 65 78  kTop++;.  Contex
14f59 74 20 2a 70 43 6f 6e 74 65 78 74 3b 0a 0a 20 20  t *pContext;..  
14f5a 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a  assert( i>=0 );.
14f5b 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 54 68 69    /* FIX ME: Thi
14f5c 73 20 73 68 6f 75 6c 64 20 62 65 20 61 6c 6c 6f  s should be allo
14f5d 63 61 74 65 64 20 61 73 20 70 61 72 74 20 6f 66  cated as part of
14f5e 20 74 68 65 20 76 64 62 65 20 61 74 20 63 6f 6d   the vdbe at com
14f5f 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 69  pile-time */.  i
14f60 66 28 20 69 3e 3d 70 2d 3e 63 6f 6e 74 65 78 74  f( i>=p->context
14f61 53 74 61 63 6b 44 65 70 74 68 20 29 7b 0a 20 20  StackDepth ){.  
14f62 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63    p->contextStac
14f63 6b 44 65 70 74 68 20 3d 20 69 2b 31 3b 0a 20 20  kDepth = i+1;.  
14f64 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63    p->contextStac
14f65 6b 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  k = sqlite3DbRea
14f66 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70  llocOrFree(db, p
14f67 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 2c 0a  ->contextStack,.
14f68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14f69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14f6a 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
14f6b 28 43 6f 6e 74 65 78 74 29 2a 28 69 2b 31 29 29  (Context)*(i+1))
14f6c 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 6f 6e  ;.    if( p->con
14f6d 74 65 78 74 53 74 61 63 6b 3d 3d 30 20 29 20 67  textStack==0 ) g
14f6e 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
14f6f 20 20 70 43 6f 6e 74 65 78 74 20 3d 20 26 70 2d    pContext = &p-
14f70 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b 69 5d  >contextStack[i]
14f71 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61  ;.  pContext->la
14f72 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61  stRowid = db->la
14f73 73 74 52 6f 77 69 64 3b 0a 20 20 70 43 6f 6e 74  stRowid;.  pCont
14f74 65 78 74 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70  ext->nChange = p
14f75 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 70 43 6f  ->nChange;.  pCo
14f76 6e 74 65 78 74 2d 3e 73 46 69 66 6f 20 3d 20 70  ntext->sFifo = p
14f77 2d 3e 73 46 69 66 6f 3b 0a 20 20 73 71 6c 69 74  ->sFifo;.  sqlit
14f78 65 33 56 64 62 65 46 69 66 6f 49 6e 69 74 28 26  e3VdbeFifoInit(&
14f79 70 2d 3e 73 46 69 66 6f 29 3b 0a 20 20 62 72 65  p->sFifo);.  bre
14f7a 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
14f7b 3a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a 20 2a  : ContextPop * *
14f7c 20 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74 6f 72   * .**.** Restor
14f7d 65 20 74 68 65 20 56 64 62 65 20 63 6f 6e 74 65  e the Vdbe conte
14f7e 78 74 20 74 6f 20 74 68 65 20 73 74 61 74 65 20  xt to the state 
14f7f 69 74 20 77 61 73 20 69 6e 20 77 68 65 6e 20 63  it was in when c
14f80 6f 6e 74 65 78 74 50 75 73 68 20 77 61 73 20 6c  ontextPush was l
14f81 61 73 74 0a 2a 2a 20 65 78 65 63 75 74 65 64 2e  ast.** executed.
14f82 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 6f   The context sto
14f83 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73  res the last ins
14f84 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 20  ert row id, the 
14f85 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a  last statement.*
14f86 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c 20  * change count, 
14f87 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  and the current 
14f88 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65  statement change
14f89 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20   count..*/.case 
14f8a 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a 20 7b  OP_ContextPop: {
14f8b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
14f8c 73 68 20 2a 2f 0a 20 20 43 6f 6e 74 65 78 74 20  sh */.  Context 
14f8d 2a 70 43 6f 6e 74 65 78 74 20 3d 20 26 70 2d 3e  *pContext = &p->
14f8e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b 2d 2d 70  contextStack[--p
14f8f 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f  ->contextStackTo
14f90 70 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p];.  assert( p-
14f91 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70  >contextStackTop
14f92 3e 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 6c 61 73  >=0 );.  db->las
14f93 74 52 6f 77 69 64 20 3d 20 70 43 6f 6e 74 65 78  tRowid = pContex
14f94 74 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20  t->lastRowid;.  
14f95 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 43 6f  p->nChange = pCo
14f96 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67 65 3b 0a  ntext->nChange;.
14f97 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66    sqlite3VdbeFif
14f98 6f 43 6c 65 61 72 28 26 70 2d 3e 73 46 69 66 6f  oClear(&p->sFifo
14f99 29 3b 0a 20 20 70 2d 3e 73 46 69 66 6f 20 3d 20  );.  p->sFifo = 
14f9a 70 43 6f 6e 74 65 78 74 2d 3e 73 46 69 66 6f 3b  pContext->sFifo;
14f9b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
14f9c 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51  if /* #ifndef SQ
14f9d 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
14f9e 52 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  R */../* Opcode:
14f9f 20 4d 65 6d 53 74 6f 72 65 20 50 31 20 50 32 20   MemStore P1 P2 
14fa0 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  *.**.** Write th
14fa1 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
14fa2 63 6b 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6c  ck into memory l
14fa3 6f 63 61 74 69 6f 6e 20 50 31 2e 0a 2a 2a 20 50  ocation P1..** P
14fa4 31 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 6d  1 should be a sm
14fa5 61 6c 6c 20 69 6e 74 65 67 65 72 20 73 69 6e 63  all integer sinc
14fa6 65 20 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63  e space is alloc
14fa7 61 74 65 64 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20  ated.** for all 
14fa8 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
14fa9 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 50   between 0 and P
14faa 31 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a  1 inclusive..**.
14fab 2a 2a 20 41 66 74 65 72 20 74 68 65 20 64 61 74  ** After the dat
14fac 61 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  a is stored in t
14fad 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
14fae 6f 6e 2c 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b  on, the.** stack
14faf 20 69 73 20 70 6f 70 70 65 64 20 6f 6e 63 65 20   is popped once 
14fb0 69 66 20 50 32 20 69 73 20 31 2e 20 20 49 66 20  if P2 is 1.  If 
14fb1 50 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e  P2 is zero, then
14fb2 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  .** the original
14fb3 20 64 61 74 61 20 72 65 6d 61 69 6e 73 20 6f 6e   data remains on
14fb4 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f 0a 63   the stack..*/.c
14fb5 61 73 65 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 3a  ase OP_MemStore:
14fb6 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d   {        /* no-
14fb7 70 75 73 68 20 2a 2f 0a 20 20 61 73 73 65 72 74  push */.  assert
14fb8 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63  ( pTos>=p->aStac
14fb9 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  k );.  assert( p
14fba 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
14fbb 2d 3e 70 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  ->p1<p->nMem );.
14fbc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
14fbd 62 65 4d 65 6d 4d 6f 76 65 28 26 70 2d 3e 61 4d  beMemMove(&p->aM
14fbe 65 6d 5b 70 4f 70 2d 3e 70 31 5d 2c 20 70 54 6f  em[pOp->p1], pTo
14fbf 73 29 3b 0a 20 20 70 54 6f 73 2d 2d 3b 0a 0a 20  s);.  pTos--;.. 
14fc0 20 2f 2a 20 49 66 20 50 32 20 69 73 20 30 20 74   /* If P2 is 0 t
14fc1 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 20 74 6f  hen fall thru to
14fc2 20 74 68 65 20 6e 65 78 74 20 6f 70 63 6f 64 65   the next opcode
14fc3 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 74 68  , OP_MemLoad, th
14fc4 61 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 72 65 73  at will.  ** res
14fc5 74 6f 72 65 20 74 68 65 20 74 6f 70 20 6f 66 20  tore the top of 
14fc6 74 68 65 20 73 74 61 63 6b 20 74 6f 20 69 74 73  the stack to its
14fc7 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 2e   original value.
14fc8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 70 2d  .  */.  if( pOp-
14fc9 3e 70 32 20 29 7b 0a 20 20 20 20 62 72 65 61 6b  >p2 ){.    break
14fca 3b 0a 20 20 7d 0a 7d 0a 2f 2a 20 4f 70 63 6f 64  ;.  }.}./* Opcod
14fcb 65 3a 20 4d 65 6d 4c 6f 61 64 20 50 31 20 2a 20  e: MemLoad P1 * 
14fcc 2a 0a 2a 2a 0a 2a 2a 20 50 75 73 68 20 61 20 63  *.**.** Push a c
14fcd 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65  opy of the value
14fce 20 69 6e 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74   in memory locat
14fcf 69 6f 6e 20 50 31 20 6f 6e 74 6f 20 74 68 65 20  ion P1 onto the 
14fd0 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  stack..**.** If 
14fd1 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73  the value is a s
14fd2 74 72 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20  tring, then the 
14fd3 76 61 6c 75 65 20 70 75 73 68 65 64 20 69 73 20  value pushed is 
14fd4 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20  a pointer to.** 
14fd5 74 68 65 20 73 74 72 69 6e 67 20 74 68 61 74 20  the string that 
14fd6 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  is stored in the
14fd7 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
14fd8 2e 20 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79  .  If the memory
14fd9 0a 2a 2a 20 6c 6f 63 61 74 69 6f 6e 20 69 73 20  .** location is 
14fda 73 75 62 73 65 71 75 65 6e 74 6c 79 20 63 68 61  subsequently cha
14fdb 6e 67 65 64 20 28 75 73 69 6e 67 20 4f 50 5f 4d  nged (using OP_M
14fdc 65 6d 53 74 6f 72 65 29 20 74 68 65 6e 20 74 68  emStore) then th
14fdd 65 0a 2a 2a 20 76 61 6c 75 65 20 70 75 73 68 65  e.** value pushe
14fde 64 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  d onto the stack
14fdf 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 6f 6f   will change too
14fe0 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d  ..*/.case OP_Mem
14fe1 4c 6f 61 64 3a 20 7b 0a 20 20 69 6e 74 20 69 20  Load: {.  int i 
14fe2 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73  = pOp->p1;.  ass
14fe3 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
14fe4 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 54 6f 73  ->nMem );.  pTos
14fe5 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  ++;.  sqlite3Vdb
14fe6 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
14fe7 70 54 6f 73 2c 20 26 70 2d 3e 61 4d 65 6d 5b 69  pTos, &p->aMem[i
14fe8 5d 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  ], MEM_Ephem);. 
14fe9 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64   break;.}..#ifnd
14fea 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
14feb 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20  UTOINCREMENT./* 
14fec 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50  Opcode: MemMax P
14fed 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20  1 * *.**.** Set 
14fee 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 6d  the value of mem
14fef 6f 72 79 20 63 65 6c 6c 20 50 31 20 74 6f 20 74  ory cell P1 to t
14ff0 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 69 74  he maximum of it
14ff1 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 0a  s current value.
14ff2 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  ** and the value
14ff3 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74   on the top of t
14ff4 68 65 20 73 74 61 63 6b 2e 20 20 54 68 65 20 73  he stack.  The s
14ff5 74 61 63 6b 20 69 73 20 75 6e 63 68 61 6e 67 65  tack is unchange
14ff6 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
14ff7 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73  struction throws
14ff8 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65   an error if the
14ff9 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20   memory cell is 
14ffa 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a  not initially.**
14ffb 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a   an integer..*/.
14ffc 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20  case OP_MemMax: 
14ffd 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70  {        /* no-p
14ffe 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  ush */.  int i =
14fff 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 4d 65 6d 20   pOp->p1;.  Mem 
15000 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  *pMem;.  assert(
15001 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b   pTos>=p->aStack
15002 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e   );.  assert( i>
15003 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 4d 65 6d 20  =0 && i<p->nMem 
15004 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e  );.  pMem = &p->
15005 61 4d 65 6d 5b 69 5d 3b 0a 20 20 73 71 6c 69 74  aMem[i];.  sqlit
15006 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
15007 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 73 71 6c  ify(pMem);.  sql
15008 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
15009 65 72 69 66 79 28 70 54 6f 73 29 3b 0a 20 20 69  erify(pTos);.  i
1500a 66 28 20 70 4d 65 6d 2d 3e 75 2e 69 3c 70 54 6f  f( pMem->u.i<pTo
1500b 73 2d 3e 75 2e 69 29 7b 0a 20 20 20 20 70 4d 65  s->u.i){.    pMe
1500c 6d 2d 3e 75 2e 69 20 3d 20 70 54 6f 73 2d 3e 75  m->u.i = pTos->u
1500d 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  .i;.  }.  break;
1500e 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1500f 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
15010 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70  REMENT */../* Op
15011 63 6f 64 65 3a 20 4d 65 6d 49 6e 63 72 20 50 31  code: MemIncr P1
15012 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 63 72   P2 *.**.** Incr
15013 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 65 67 65  ement the intege
15014 72 20 76 61 6c 75 65 64 20 6d 65 6d 6f 72 79 20  r valued memory 
15015 63 65 6c 6c 20 50 32 20 62 79 20 74 68 65 20 76  cell P2 by the v
15016 61 6c 75 65 20 69 6e 20 50 31 2e 0a 2a 2a 0a 2a  alue in P1..**.*
15017 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
15018 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74  to use this inst
15019 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 6d 65 6d  ruction on a mem
1501a 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20 64 6f  ory cell that do
1501b 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69  es.** not contai
1501c 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41  n an integer.  A
1501d 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c  n assertion faul
1501e 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66  t will result if
1501f 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73   you try..*/.cas
15020 65 20 4f 50 5f 4d 65 6d 49 6e 63 72 3a 20 7b 20  e OP_MemIncr: { 
15021 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73         /* no-pus
15022 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  h */.  int i = p
15023 4f 70 2d 3e 70 32 3b 0a 20 20 4d 65 6d 20 2a 70  Op->p2;.  Mem *p
15024 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 69  Mem;.  assert( i
15025 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 4d 65 6d  >=0 && i<p->nMem
15026 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 26 70 2d   );.  pMem = &p-
15027 3e 61 4d 65 6d 5b 69 5d 3b 0a 20 20 61 73 73 65  >aMem[i];.  asse
15028 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3d  rt( pMem->flags=
15029 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 4d  =MEM_Int );.  pM
1502a 65 6d 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e  em->u.i += pOp->
1502b 70 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  p1;.  break;.}..
1502c 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4d 65 6d  /* Opcode: IfMem
1502d 50 6f 73 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a  Pos P1 P2 *.**.*
1502e 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f  * If the value o
1502f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 31  f memory cell P1
15030 20 69 73 20 31 20 6f 72 20 67 72 65 61 74 65 72   is 1 or greater
15031 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  , jump to P2..**
15032 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61  .** It is illega
15033 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e  l to use this in
15034 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 6d  struction on a m
15035 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20  emory cell that 
15036 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74  does.** not cont
15037 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20  ain an integer. 
15038 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
15039 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  ult will result 
1503a 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63  if you try..*/.c
1503b 61 73 65 20 4f 50 5f 49 66 4d 65 6d 50 6f 73 3a  ase OP_IfMemPos:
1503c 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d   {        /* no-
1503d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20  push */.  int i 
1503e 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 4d 65 6d  = pOp->p1;.  Mem
1503f 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74   *pMem;.  assert
15040 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
15041 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20  Mem );.  pMem = 
15042 26 70 2d 3e 61 4d 65 6d 5b 69 5d 3b 0a 20 20 61  &p->aMem[i];.  a
15043 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61  ssert( pMem->fla
15044 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  gs==MEM_Int );. 
15045 20 69 66 28 20 70 4d 65 6d 2d 3e 75 2e 69 3e 30   if( pMem->u.i>0
15046 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f   ){.     pc = pO
15047 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p2 - 1;.  }. 
15048 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
15049 63 6f 64 65 3a 20 49 66 4d 65 6d 4e 65 67 20 50  code: IfMemNeg P
1504a 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20  1 P2 *.**.** If 
1504b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 6d  the value of mem
1504c 6f 72 79 20 63 65 6c 6c 20 50 31 20 69 73 20 6c  ory cell P1 is l
1504d 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 6a  ess than zero, j
1504e 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a  ump to P2. .**.*
1504f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
15050 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74  to use this inst
15051 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 6d 65 6d  ruction on a mem
15052 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20 64 6f  ory cell that do
15053 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69  es.** not contai
15054 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41  n an integer.  A
15055 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c  n assertion faul
15056 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66  t will result if
15057 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73   you try..*/.cas
15058 65 20 4f 50 5f 49 66 4d 65 6d 4e 65 67 3a 20 7b  e OP_IfMemNeg: {
15059 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
1505a 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  sh */.  int i = 
1505b 70 4f 70 2d 3e 70 31 3b 0a 20 20 4d 65 6d 20 2a  pOp->p1;.  Mem *
1505c 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20  pMem;.  assert( 
1505d 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 4d 65  i>=0 && i<p->nMe
1505e 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 26 70  m );.  pMem = &p
1505f 2d 3e 61 4d 65 6d 5b 69 5d 3b 0a 20 20 61 73 73  ->aMem[i];.  ass
15060 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
15061 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69  ==MEM_Int );.  i
15062 66 28 20 70 4d 65 6d 2d 3e 75 2e 69 3c 30 20 29  f( pMem->u.i<0 )
15063 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
15064 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
15065 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
15066 64 65 3a 20 49 66 4d 65 6d 5a 65 72 6f 20 50 31  de: IfMemZero P1
15067 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74   P2 *.**.** If t
15068 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 6d 6f  he value of memo
15069 72 79 20 63 65 6c 6c 20 50 31 20 69 73 20 65 78  ry cell P1 is ex
1506a 61 63 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f  actly 0, jump to
1506b 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69   P2. .**.** It i
1506c 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65  s illegal to use
1506d 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1506e 6e 20 6f 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65  n on a memory ce
1506f 6c 6c 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20  ll that does.** 
15070 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69  not contain an i
15071 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65  nteger.  An asse
15072 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c  rtion fault will
15073 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74   result if you t
15074 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  ry..*/.case OP_I
15075 66 4d 65 6d 5a 65 72 6f 3a 20 7b 20 20 20 20 20  fMemZero: {     
15076 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
15077 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
15078 70 31 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b  p1;.  Mem *pMem;
15079 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1507a 26 26 20 69 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  && i<p->nMem );.
1507b 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65    pMem = &p->aMe
1507c 6d 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  m[i];.  assert( 
1507d 70 4d 65 6d 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d  pMem->flags==MEM
1507e 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 4d  _Int );.  if( pM
1507f 65 6d 2d 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20 20  em->u.i==0 ){.  
15080 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
15081 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  - 1;.  }.  break
15082 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
15083 4d 65 6d 4e 75 6c 6c 20 50 31 20 2a 20 2a 0a 2a  MemNull P1 * *.*
15084 2a 0a 2a 2a 20 53 74 6f 72 65 20 61 20 4e 55 4c  *.** Store a NUL
15085 4c 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  L in memory cell
15086 20 50 31 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d   P1.*/.case OP_M
15087 65 6d 4e 75 6c 6c 3a 20 7b 0a 20 20 61 73 73 65  emNull: {.  asse
15088 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
15089 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 4d 65  & pOp->p1<p->nMe
1508a 6d 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  m );.  sqlite3Vd
1508b 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 2d  beMemSetNull(&p-
1508c 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 29 3b  >aMem[pOp->p1]);
1508d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1508e 4f 70 63 6f 64 65 3a 20 4d 65 6d 49 6e 74 20 50  Opcode: MemInt P
1508f 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f  1 P2 *.**.** Sto
15090 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  re the integer v
15091 61 6c 75 65 20 50 31 20 69 6e 20 6d 65 6d 6f 72  alue P1 in memor
15092 79 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61  y cell P2..*/.ca
15093 73 65 20 4f 50 5f 4d 65 6d 49 6e 74 3a 20 7b 0a  se OP_MemInt: {.
15094 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
15095 32 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c  2>=0 && pOp->p2<
15096 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 73 71 6c  p->nMem );.  sql
15097 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e  ite3VdbeMemSetIn
15098 74 36 34 28 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t64(&p->aMem[pOp
15099 2d 3e 70 32 5d 2c 20 70 4f 70 2d 3e 70 31 29 3b  ->p2], pOp->p1);
1509a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1509b 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 6f 76 65 20  Opcode: MemMove 
1509c 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f  P1 P2 *.**.** Mo
1509d 76 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ve the content o
1509e 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32  f memory cell P2
1509f 20 6f 76 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20   over to memory 
150a0 63 65 6c 6c 20 50 31 2e 0a 2a 2a 20 41 6e 79 20  cell P1..** Any 
150a1 70 72 69 6f 72 20 63 6f 6e 74 65 6e 74 20 6f 66  prior content of
150a2 20 50 31 20 69 73 20 65 72 61 73 65 64 2e 20 20   P1 is erased.  
150a3 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32 20 69  Memory cell P2 i
150a4 73 20 6c 65 66 74 0a 2a 2a 20 63 6f 6e 74 61 69  s left.** contai
150a5 6e 69 6e 67 20 61 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ning a NULL..*/.
150a6 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 6f 76 65 3a  case OP_MemMove:
150a7 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70   {.  assert( pOp
150a8 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
150a9 70 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  p1<p->nMem );.  
150aa 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
150ab 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 70 2d  =0 && pOp->p2<p-
150ac 3e 6e 4d 65 6d 20 29 3b 0a 20 20 72 63 20 3d 20  >nMem );.  rc = 
150ad 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
150ae 76 65 28 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d  ve(&p->aMem[pOp-
150af 3e 70 31 5d 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70  >p1], &p->aMem[p
150b0 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 62 72 65 61  Op->p2]);.  brea
150b1 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
150b2 20 41 67 67 53 74 65 70 20 50 31 20 50 32 20 50   AggStep P1 P2 P
150b3 33 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  3.**.** Execute 
150b4 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f  the step functio
150b5 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61  n for an aggrega
150b6 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63  te.  The.** func
150b7 74 69 6f 6e 20 68 61 73 20 50 32 20 61 72 67 75  tion has P2 argu
150b8 6d 65 6e 74 73 2e 20 20 50 33 20 69 73 20 61 20  ments.  P3 is a 
150b9 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46  pointer to the F
150ba 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74  uncDef.** struct
150bb 75 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69  ure that specifi
150bc 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  es the function.
150bd 20 20 55 73 65 20 6d 65 6d 6f 72 79 20 6c 6f 63    Use memory loc
150be 61 74 69 6f 6e 0a 2a 2a 20 50 31 20 61 73 20 74  ation.** P1 as t
150bf 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e 0a  he accumulator..
150c0 2a 2a 0a 2a 2a 20 54 68 65 20 50 32 20 61 72 67  **.** The P2 arg
150c1 75 6d 65 6e 74 73 20 61 72 65 20 70 6f 70 70 65  uments are poppe
150c2 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b  d from the stack
150c3 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67  ..*/.case OP_Agg
150c4 53 74 65 70 3a 20 7b 20 20 20 20 20 20 20 20 2f  Step: {        /
150c5 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69  * no-push */.  i
150c6 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  nt n = pOp->p2;.
150c7 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a    int i;.  Mem *
150c8 70 4d 65 6d 2c 20 2a 70 52 65 63 3b 0a 20 20 73  pMem, *pRec;.  s
150c9 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63  qlite3_context c
150ca 74 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  tx;.  sqlite3_va
150cb 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 0a 20 20  lue **apVal;..  
150cc 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a  assert( n>=0 );.
150cd 20 20 70 52 65 63 20 3d 20 26 70 54 6f 73 5b 31    pRec = &pTos[1
150ce 2d 6e 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  -n];.  assert( p
150cf 52 65 63 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29  Rec>=p->aStack )
150d0 3b 0a 20 20 61 70 56 61 6c 20 3d 20 70 2d 3e 61  ;.  apVal = p->a
150d1 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20  pArg;.  assert( 
150d2 61 70 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b  apVal || n==0 );
150d3 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b  .  for(i=0; i<n;
150d4 20 69 2b 2b 2c 20 70 52 65 63 2b 2b 29 7b 0a 20   i++, pRec++){. 
150d5 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 52     apVal[i] = pR
150d6 65 63 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70  ec;.    storeTyp
150d7 65 49 6e 66 6f 28 70 52 65 63 2c 20 65 6e 63 6f  eInfo(pRec, enco
150d8 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 63 74 78  ding);.  }.  ctx
150d9 2e 70 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65  .pFunc = (FuncDe
150da 66 2a 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73  f*)pOp->p3;.  as
150db 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
150dc 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
150dd 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e 70 4d 65  Mem );.  ctx.pMe
150de 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61  m = pMem = &p->a
150df 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
150e0 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63 74 78  pMem->n++;.  ctx
150e1 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  .s.flags = MEM_N
150e2 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 7a 20 3d  ull;.  ctx.s.z =
150e3 20 30 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c   0;.  ctx.s.xDel
150e4 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 64 62   = 0;.  ctx.s.db
150e5 20 3d 20 64 62 3b 0a 20 20 63 74 78 2e 69 73 45   = db;.  ctx.isE
150e6 72 72 6f 72 20 3d 20 30 3b 0a 20 20 63 74 78 2e  rror = 0;.  ctx.
150e7 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 28  pColl = 0;.  if(
150e8 20 63 74 78 2e 70 46 75 6e 63 2d 3e 6e 65 65 64   ctx.pFunc->need
150e9 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20 61  CollSeq ){.    a
150ea 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f  ssert( pOp>p->aO
150eb 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  p );.    assert(
150ec 20 70 4f 70 5b 2d 31 5d 2e 70 33 74 79 70 65 3d   pOp[-1].p3type=
150ed 3d 50 33 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20  =P3_COLLSEQ );. 
150ee 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d     assert( pOp[-
150ef 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f  1].opcode==OP_Co
150f0 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78  llSeq );.    ctx
150f1 2e 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65  .pColl = (CollSe
150f2 71 20 2a 29 70 4f 70 5b 2d 31 5d 2e 70 33 3b 0a  q *)pOp[-1].p3;.
150f3 20 20 7d 0a 20 20 28 63 74 78 2e 70 46 75 6e 63    }.  (ctx.pFunc
150f4 2d 3e 78 53 74 65 70 29 28 26 63 74 78 2c 20 6e  ->xStep)(&ctx, n
150f5 2c 20 61 70 56 61 6c 29 3b 0a 20 20 70 6f 70 53  , apVal);.  popS
150f6 74 61 63 6b 28 26 70 54 6f 73 2c 20 6e 29 3b 0a  tack(&pTos, n);.
150f7 20 20 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f    if( ctx.isErro
150f8 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
150f9 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
150fa 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 5f 76  rrMsg, sqlite3_v
150fb 61 6c 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73  alue_text(&ctx.s
150fc 29 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  ), (char*)0);.  
150fd 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
150fe 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ROR;.  }.  sqlit
150ff 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
15100 28 26 63 74 78 2e 73 29 3b 0a 20 20 62 72 65 61  (&ctx.s);.  brea
15101 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
15102 20 41 67 67 46 69 6e 61 6c 20 50 31 20 50 32 20   AggFinal P1 P2 
15103 50 33 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65  P3.**.** Execute
15104 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 66   the finalizer f
15105 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61  unction for an a
15106 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 73  ggregate.  P1 is
15107 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c  .** the memory l
15108 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20  ocation that is 
15109 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  the accumulator 
1510a 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 74  for the aggregat
1510b 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74  e..**.** P2 is t
1510c 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  he number of arg
1510d 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20  uments that the 
1510e 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 61  step function ta
1510f 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 33 20 69 73  kes and.** P3 is
15110 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
15111 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 68  e FuncDef for th
15112 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  is function.  Th
15113 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  e P2.** argument
15114 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20   is not used by 
15115 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 74  this opcode.  It
15116 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 74   is only there t
15117 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a 2a  o disambiguate.*
15118 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  * functions that
15119 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 6e   can take varyin
1511a 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 67  g numbers of arg
1511b 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20  uments.  The.** 
1511c 50 33 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  P3 argument is o
1511d 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 74  nly needed for t
1511e 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 61  he degenerate ca
1511f 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20  se where.** the 
15120 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61  step function wa
15121 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  s not previously
15122 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65   called..*/.case
15123 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 20   OP_AggFinal: { 
15124 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73         /* no-pus
15125 68 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d  h */.  Mem *pMem
15126 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
15127 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
15128 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  1<p->nMem );.  p
15129 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Mem = &p->aMem[p
1512a 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1512b 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
1512c 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d  & ~(MEM_Null|MEM
1512d 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72  _Agg))==0 );.  r
1512e 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1512f 65 6d 46 69 6e 61 6c 69 7a 65 28 70 4d 65 6d 2c  emFinalize(pMem,
15130 20 28 46 75 6e 63 44 65 66 2a 29 70 4f 70 2d 3e   (FuncDef*)pOp->
15131 70 33 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  p3);.  if( rc==S
15132 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20  QLITE_ERROR ){. 
15133 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
15134 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
15135 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
15136 65 78 74 28 70 4d 65 6d 29 2c 20 28 63 68 61 72  ext(pMem), (char
15137 2a 29 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  *)0);.  }.  if( 
15138 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f  sqlite3VdbeMemTo
15139 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a 20 20  oBig(pMem) ){.  
1513a 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1513b 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1513c 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1513d 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
1513e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
1513f 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
15140 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63  )./* Opcode: Vac
15141 75 75 6d 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  uum * * *.**.** 
15142 56 61 63 75 75 6d 20 74 68 65 20 65 6e 74 69 72  Vacuum the entir
15143 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
15144 73 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 63 61  s opcode will ca
15145 75 73 65 20 6f 74 68 65 72 20 76 69 72 74 75 61  use other virtua
15146 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f  l.** machines to
15147 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64 20   be created and 
15148 72 75 6e 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74  run.  It may not
15149 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
1514a 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e  within.** a tran
1514b 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  saction..*/.case
1514c 20 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 20 20 20   OP_Vacuum: {   
1514d 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
1514e 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
1514f 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
15150 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
15151 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20  o_misuse; .  rc 
15152 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75  = sqlite3RunVacu
15153 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  um(&p->zErrMsg, 
15154 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  db);.  if( sqlit
15155 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
15156 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
15157 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65  to_misuse;.  bre
15158 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ak;.}.#endif..#i
15159 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1515a 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1515b 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e  M)./* Opcode: In
1515c 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a  crVacuum P1 P2 *
1515d 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61  .**.** Perform a
1515e 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20   single step of 
1515f 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20  the incremental 
15160 76 61 63 75 75 6d 20 70 72 6f 63 65 64 75 72 65  vacuum procedure
15161 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 20 64 61   on.** the P1 da
15162 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 76  tabase. If the v
15163 61 63 75 75 6d 20 68 61 73 20 66 69 6e 69 73 68  acuum has finish
15164 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74  ed, jump to inst
15165 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f  ruction.** P2. O
15166 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74  therwise, fall t
15167 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
15168 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  xt instruction..
15169 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 63 72 56  */.case OP_IncrV
1516a 61 63 75 75 6d 3a 20 7b 20 20 20 20 20 20 20 20  acuum: {        
1516b 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20  /* no-push */.  
1516c 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61  Btree *pBt;..  a
1516d 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1516e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d  0 && pOp->p1<db-
1516f 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
15170 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
15171 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21  & (1<<pOp->p1))!
15172 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62  =0 );.  pBt = db
15173 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
15174 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  Bt;.  rc = sqlit
15175 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75  e3BtreeIncrVacuu
15176 6d 28 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  m(pBt);.  if( rc
15177 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
15178 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
15179 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  2 - 1;.    rc = 
1517a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1517b 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1517c 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 78 70  ../* Opcode: Exp
1517d 69 72 65 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a  ire P1 * *.**.**
1517e 20 43 61 75 73 65 20 70 72 65 63 6f 6d 70 69 6c   Cause precompil
1517f 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ed statements to
15180 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e   become expired.
15181 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 74   An expired stat
15182 65 6d 65 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77  ement.** fails w
15183 69 74 68 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  ith an error cod
15184 65 20 6f 66 20 53 51 4c 49 54 45 5f 53 43 48 45  e of SQLITE_SCHE
15185 4d 41 20 69 66 20 69 74 20 69 73 20 65 76 65 72  MA if it is ever
15186 20 65 78 65 63 75 74 65 64 20 0a 2a 2a 20 28 76   executed .** (v
15187 69 61 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ia sqlite3_step(
15188 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31  ))..** .** If P1
15189 20 69 73 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20   is 0, then all 
1518a 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62  SQL statements b
1518b 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 49  ecome expired. I
1518c 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  f P1 is non-zero
1518d 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74  ,.** then only t
1518e 68 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  he currently exe
1518f 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  cuting statement
15190 20 69 73 20 61 66 66 65 63 74 65 64 2e 20 0a 2a   is affected. .*
15191 2f 0a 63 61 73 65 20 4f 50 5f 45 78 70 69 72 65  /.case OP_Expire
15192 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f  : {        /* no
15193 2d 70 75 73 68 20 2a 2f 0a 20 20 69 66 28 20 21  -push */.  if( !
15194 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73  pOp->p1 ){.    s
15195 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
15196 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
15197 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  b);.  }else{.   
15198 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b   p->expired = 1;
15199 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1519a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1519b 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1519c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 62  E./* Opcode: Tab
1519d 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 0a  leLock P1 P2 P3.
1519e 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c  **.** Obtain a l
1519f 6f 63 6b 20 6f 6e 20 61 20 70 61 72 74 69 63 75  ock on a particu
151a0 6c 61 72 20 74 61 62 6c 65 2e 20 54 68 69 73 20  lar table. This 
151a1 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f  instruction is o
151a2 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a  nly used when.**
151a3 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
151a4 65 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61  e feature is ena
151a5 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  bled. .**.** If 
151a6 50 31 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69  P1 is not negati
151a7 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74  ve, then it is t
151a8 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
151a9 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 6e 20 73  database.** in s
151aa 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20 61 6e 64  qlite3.aDb[] and
151ab 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20   a read-lock is 
151ac 72 65 71 75 69 72 65 64 2e 20 49 66 20 50 31 20  required. If P1 
151ad 69 73 20 6e 65 67 61 74 69 76 65 2c 20 61 20 0a  is negative, a .
151ae 2a 2a 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73  ** write-lock is
151af 20 72 65 71 75 69 72 65 64 2e 20 49 6e 20 74 68   required. In th
151b0 69 73 20 63 61 73 65 20 74 68 65 20 69 6e 64 65  is case the inde
151b1 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
151b2 65 20 69 73 20 74 68 65 20 0a 2a 2a 20 61 62 73  e is the .** abs
151b3 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f 66 20 50  olute value of P
151b4 31 20 6d 69 6e 75 73 20 6f 6e 65 20 28 69 44 62  1 minus one (iDb
151b5 20 3d 20 61 62 73 28 50 31 29 20 2d 20 31 3b 29   = abs(P1) - 1;)
151b6 20 61 6e 64 20 61 20 77 72 69 74 65 2d 6c 6f 63   and a write-loc
151b7 6b 20 69 73 0a 2a 2a 20 72 65 71 75 69 72 65 64  k is.** required
151b8 2e 20 0a 2a 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74  . .**.** P2 cont
151b9 61 69 6e 73 20 74 68 65 20 72 6f 6f 74 2d 70 61  ains the root-pa
151ba 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ge of the table 
151bb 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50  to lock..**.** P
151bc 33 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  3 contains a poi
151bd 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65  nter to the name
151be 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   of the table be
151bf 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 68 69 73  ing locked. This
151c0 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64   is only.** used
151c1 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20   to generate an 
151c2 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66  error message if
151c3 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74   the lock cannot
151c4 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f   be obtained..*/
151c5 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c 65 4c 6f  .case OP_TableLo
151c6 63 6b 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  ck: {        /* 
151c7 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74  no-push */.  int
151c8 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a   p1 = pOp->p1; .
151c9 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b    u8 isWriteLock
151ca 20 3d 20 28 70 31 3c 30 29 3b 0a 20 20 69 66 28   = (p1<0);.  if(
151cb 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 29 7b 0a   isWriteLock ){.
151cc 20 20 20 20 70 31 20 3d 20 28 2d 31 2a 70 31 29      p1 = (-1*p1)
151cd 2d 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  -1;.  }.  assert
151ce 28 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 64 62  ( p1>=0 && p1<db
151cf 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
151d0 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
151d1 20 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 20 29   & (1<<p1))!=0 )
151d2 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
151d3 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64  BtreeLockTable(d
151d4 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20  b->aDb[p1].pBt, 
151d5 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 69 74 65  pOp->p2, isWrite
151d6 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 20 72 63 3d  Lock);.  if( rc=
151d7 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29  =SQLITE_LOCKED )
151d8 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
151d9 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61   *z = (const cha
151da 72 20 2a 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20  r *)pOp->p3;.   
151db 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
151dc 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 22  g(&p->zErrMsg, "
151dd 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 69  database table i
151de 73 20 6c 6f 63 6b 65 64 3a 20 22 2c 20 7a 2c 20  s locked: ", z, 
151df 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 7d 0a 20  (char*)0);.  }. 
151e0 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
151e1 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
151e2 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a  SHARED_CACHE */.
151e3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
151e4 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
151e5 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65  E./* Opcode: VBe
151e6 67 69 6e 20 2a 20 2a 20 50 33 0a 2a 2a 0a 2a 2a  gin * * P3.**.**
151e7 20 50 33 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   P3 a pointer to
151e8 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62   an sqlite3_vtab
151e9 20 73 74 72 75 63 74 75 72 65 2e 20 43 61 6c 6c   structure. Call
151ea 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65 74 68   the xBegin meth
151eb 6f 64 20 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20  od .** for that 
151ec 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
151ed 50 5f 56 42 65 67 69 6e 3a 20 7b 20 20 20 2f 2a  P_VBegin: {   /*
151ee 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 72 63   no-push */.  rc
151ef 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 42 65   = sqlite3VtabBe
151f0 67 69 6e 28 64 62 2c 20 28 73 71 6c 69 74 65 33  gin(db, (sqlite3
151f1 5f 76 74 61 62 20 2a 29 70 4f 70 2d 3e 70 33 29  _vtab *)pOp->p3)
151f2 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  ;.  break;.}.#en
151f3 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
151f4 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
151f5 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
151f6 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
151f7 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ABLE./* Opcode: 
151f8 56 43 72 65 61 74 65 20 50 31 20 2a 20 50 33 0a  VCreate P1 * P3.
151f9 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 20  **.** P3 is the 
151fa 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61  name of a virtua
151fb 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62  l table in datab
151fc 61 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65  ase P1. Call the
151fd 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a   xCreate method.
151fe 2a 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c  ** for that tabl
151ff 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43  e..*/.case OP_VC
15200 72 65 61 74 65 3a 20 7b 20 20 20 2f 2a 20 6e 6f  reate: {   /* no
15201 2d 70 75 73 68 20 2a 2f 0a 20 20 72 63 20 3d 20  -push */.  rc = 
15202 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
15203 72 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70  reate(db, pOp->p
15204 31 2c 20 70 4f 70 2d 3e 70 33 2c 20 26 70 2d 3e  1, pOp->p3, &p->
15205 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 61  zErrMsg);.  brea
15206 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
15207 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
15208 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e  ALTABLE */..#ifn
15209 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1520a 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20  VIRTUALTABLE./* 
1520b 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f 79  Opcode: VDestroy
1520c 20 50 31 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 50   P1 * P3.**.** P
1520d 33 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  3 is the name of
1520e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1520f 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e   in database P1.
15210 20 20 43 61 6c 6c 20 74 68 65 20 78 44 65 73 74    Call the xDest
15211 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66  roy method.** of
15212 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a   that table..*/.
15213 63 61 73 65 20 4f 50 5f 56 44 65 73 74 72 6f 79  case OP_VDestroy
15214 3a 20 7b 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68  : {   /* no-push
15215 20 2a 2f 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d   */.  p->inVtabM
15216 65 74 68 6f 64 20 3d 20 32 3b 0a 20 20 72 63 20  ethod = 2;.  rc 
15217 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  = sqlite3VtabCal
15218 6c 44 65 73 74 72 6f 79 28 64 62 2c 20 70 4f 70  lDestroy(db, pOp
15219 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 33 29 3b 0a  ->p1, pOp->p3);.
1521a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
1521b 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a  d = 0;.  break;.
1521c 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1521d 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1521e 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ABLE */..#ifndef
1521f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
15220 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63  TUALTABLE./* Opc
15221 6f 64 65 3a 20 56 4f 70 65 6e 20 50 31 20 2a 20  ode: VOpen P1 * 
15222 50 33 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61  P3.**.** P3 is a
15223 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69   pointer to a vi
15224 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65  rtual table obje
15225 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76  ct, an sqlite3_v
15226 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  tab structure..*
15227 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f 72  * P1 is a cursor
15228 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f   number.  This o
15229 70 63 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75  pcode opens a cu
1522a 72 73 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74  rsor to the virt
1522b 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64  ual.** table and
1522c 20 73 74 6f 72 65 73 20 74 68 61 74 20 63 75 72   stores that cur
1522d 73 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 61  sor in P1..*/.ca
1522e 73 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 20 20  se OP_VOpen: {  
1522f 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20   /* no-push */. 
15230 20 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20   Cursor *pCur = 
15231 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  0;.  sqlite3_vta
15232 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
15233 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20 73 71  ursor = 0;..  sq
15234 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
15235 62 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61  b = (sqlite3_vta
15236 62 20 2a 29 28 70 4f 70 2d 3e 70 33 29 3b 0a 20  b *)(pOp->p3);. 
15237 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
15238 2a 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69  *pModule = (sqli
15239 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74  te3_module *)pVt
1523a 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20  ab->pModule;..  
1523b 61 73 73 65 72 74 28 70 56 74 61 62 20 26 26 20  assert(pVtab && 
1523c 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20  pModule);.  if( 
1523d 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1523e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1523f 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
15240 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  .  rc = pModule-
15241 3e 78 4f 70 65 6e 28 70 56 74 61 62 2c 20 26 70  >xOpen(pVtab, &p
15242 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 69  VtabCursor);.  i
15243 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
15244 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
15245 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
15246 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  e;.  if( SQLITE_
15247 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a  OK==rc ){.    /*
15248 20 49 6e 69 74 69 61 6c 69 73 65 20 73 71 6c 69   Initialise sqli
15249 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1524a 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20  base class */.  
1524b 20 20 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70    pVtabCursor->p
1524c 56 74 61 62 20 3d 20 70 56 74 61 62 3b 0a 0a 20  Vtab = pVtab;.. 
1524d 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 73 65     /* Initialise
1524e 20 76 64 62 65 20 63 75 72 73 6f 72 20 6f 62 6a   vdbe cursor obj
1524f 65 63 74 20 2a 2f 0a 20 20 20 20 70 43 75 72 20  ect */.    pCur 
15250 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72  = allocateCursor
15251 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 2d 31 29  (p, pOp->p1, -1)
15252 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 20 29  ;.    if( pCur )
15253 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 56  {.      pCur->pV
15254 74 61 62 43 75 72 73 6f 72 20 3d 20 70 56 74 61  tabCursor = pVta
15255 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70  bCursor;.      p
15256 43 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70  Cur->pModule = p
15257 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61  VtabCursor->pVta
15258 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20  b->pModule;.    
15259 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d  }else{.      db-
1525a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1525b 31 3b 0a 20 20 20 20 20 20 70 4d 6f 64 75 6c 65  1;.      pModule
1525c 2d 3e 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75  ->xClose(pVtabCu
1525d 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  rsor);.    }.  }
1525e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1525f 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
15260 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
15261 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
15262 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
15263 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
15264 46 69 6c 74 65 72 20 50 31 20 50 32 20 50 33 0a  Filter P1 P2 P3.
15265 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75  **.** P1 is a cu
15266 72 73 6f 72 20 6f 70 65 6e 65 64 20 75 73 69 6e  rsor opened usin
15267 67 20 56 4f 70 65 6e 2e 20 20 50 32 20 69 73 20  g VOpen.  P2 is 
15268 61 6e 20 61 64 64 72 65 73 73 20 74 6f 20 6a 75  an address to ju
15269 6d 70 20 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20  mp to if.** the 
1526a 66 69 6c 74 65 72 65 64 20 72 65 73 75 6c 74 20  filtered result 
1526b 73 65 74 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a  set is empty..**
1526c 0a 2a 2a 20 50 33 20 69 73 20 65 69 74 68 65 72  .** P3 is either
1526d 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e   NULL or a strin
1526e 67 20 74 68 61 74 20 77 61 73 20 67 65 6e 65 72  g that was gener
1526f 61 74 65 64 20 62 79 20 74 68 65 20 78 42 65 73  ated by the xBes
15270 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64  tIndex.** method
15271 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20   of the module. 
15272 20 54 68 65 20 69 6e 74 65 72 70 72 65 74 61 74   The interpretat
15273 69 6f 6e 20 6f 66 20 74 68 65 20 50 33 20 73 74  ion of the P3 st
15274 72 69 6e 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20  ring is left.** 
15275 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d  to the module im
15276 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a  plementation..**
15277 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
15278 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 46 69 6c  invokes the xFil
15279 74 65 72 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68  ter method on th
1527a 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
1527b 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20  specified.** by 
1527c 50 31 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72  P1.  The integer
1527d 20 71 75 65 72 79 20 70 6c 61 6e 20 70 61 72 61   query plan para
1527e 6d 65 74 65 72 20 74 6f 20 78 46 69 6c 74 65 72  meter to xFilter
1527f 20 69 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74   is the top of t
15280 68 65 0a 2a 2a 20 73 74 61 63 6b 2e 20 20 4e 65  he.** stack.  Ne
15281 78 74 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20 73  xt down on the s
15282 74 61 63 6b 20 69 73 20 74 68 65 20 61 72 67 63  tack is the argc
15283 20 70 61 72 61 6d 65 74 65 72 2e 20 20 42 65 6e   parameter.  Ben
15284 65 61 74 68 20 74 68 65 0a 2a 2a 20 6e 65 78 74  eath the.** next
15285 20 6f 66 20 73 74 61 63 6b 20 61 72 65 20 61 72   of stack are ar
15286 67 63 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 61  gc additional pa
15287 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20 61  rameters which a
15288 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20  re passed to.** 
15289 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76 2e  xFilter as argv.
1528a 20 54 68 65 20 74 6f 70 6d 6f 73 74 20 70 61 72   The topmost par
1528b 61 6d 65 74 65 72 20 28 69 2e 65 2e 20 33 72 64  ameter (i.e. 3rd
1528c 20 65 6c 65 6d 65 6e 74 20 70 6f 70 70 65 64 20   element popped 
1528d 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61 63  from.** the stac
1528e 6b 29 20 62 65 63 6f 6d 65 73 20 61 72 67 76 5b  k) becomes argv[
1528f 61 72 67 63 2d 31 5d 20 77 68 65 6e 20 70 61 73  argc-1] when pas
15290 73 65 64 20 74 6f 20 78 46 69 6c 74 65 72 2e 0a  sed to xFilter..
15291 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 65  **.** The intege
15292 72 20 71 75 65 72 79 20 70 6c 61 6e 20 70 61 72  r query plan par
15293 61 6d 65 74 65 72 2c 20 61 72 67 63 2c 20 61 6e  ameter, argc, an
15294 64 20 61 6c 6c 20 61 72 67 76 20 73 74 61 63 6b  d all argv stack
15295 20 76 61 6c 75 65 73 20 0a 2a 2a 20 61 72 65 20   values .** are 
15296 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20  popped from the 
15297 73 74 61 63 6b 20 62 65 66 6f 72 65 20 74 68 69  stack before thi
15298 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f  s instruction co
15299 6d 70 6c 65 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 41  mpletes..**.** A
1529a 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f   jump is made to
1529b 20 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c   P2 if the resul
1529c 74 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74  t set after filt
1529d 65 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 0a  ering would be .
1529e 2a 2a 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 73  ** empty..*/.cas
1529f 65 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20 7b 20  e OP_VFilter: { 
152a0 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
152a1 20 20 69 6e 74 20 6e 41 72 67 3b 0a 0a 20 20 63    int nArg;..  c
152a2 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
152a3 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20  ule *pModule;.. 
152a4 20 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20   Cursor *pCur = 
152a5 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
152a6 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  ];.  assert( pCu
152a7 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29  r->pVtabCursor )
152a8 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 43  ;.  pModule = pC
152a9 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d  ur->pVtabCursor-
152aa 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b  >pVtab->pModule;
152ab 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20  ..  /* Grab the 
152ac 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 61 6e 64  index number and
152ad 20 61 72 67 63 20 70 61 72 61 6d 65 74 65 72 73   argc parameters
152ae 20 6f 66 66 20 74 68 65 20 74 6f 70 20 6f 66 20   off the top of 
152af 74 68 65 20 73 74 61 63 6b 2e 20 2a 2f 0a 20 20  the stack. */.  
152b0 61 73 73 65 72 74 28 20 28 26 70 54 6f 73 5b 2d  assert( (&pTos[-
152b1 31 5d 29 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29  1])>=p->aStack )
152b2 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 6f  ;.  assert( (pTo
152b3 73 5b 30 5d 2e 66 6c 61 67 73 26 4d 45 4d 5f 49  s[0].flags&MEM_I
152b4 6e 74 29 21 3d 30 20 26 26 20 70 54 6f 73 5b 2d  nt)!=0 && pTos[-
152b5 31 5d 2e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e  1].flags==MEM_In
152b6 74 20 29 3b 0a 20 20 6e 41 72 67 20 3d 20 70 54  t );.  nArg = pT
152b7 6f 73 5b 2d 31 5d 2e 75 2e 69 3b 0a 0a 20 20 2f  os[-1].u.i;..  /
152b8 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69  * Invoke the xFi
152b9 6c 74 65 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lter method */. 
152ba 20 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d   {.    int res =
152bb 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   0;.    int i;. 
152bc 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d     Mem **apArg =
152bd 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 66   p->apArg;.    f
152be 6f 72 28 69 20 3d 20 30 3b 20 69 3c 6e 41 72 67  or(i = 0; i<nArg
152bf 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 70  ; i++){.      ap
152c0 41 72 67 5b 69 5d 20 3d 20 26 70 54 6f 73 5b 69  Arg[i] = &pTos[i
152c1 2b 31 2d 32 2d 6e 41 72 67 5d 3b 0a 20 20 20 20  +1-2-nArg];.    
152c2 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28    storeTypeInfo(
152c3 61 70 41 72 67 5b 69 5d 2c 20 30 29 3b 0a 20 20  apArg[i], 0);.  
152c4 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c    }..    if( sql
152c5 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
152c6 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
152c7 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
152c8 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
152c9 64 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  d = 1;.    rc = 
152ca 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65 72  pModule->xFilter
152cb 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73  (pCur->pVtabCurs
152cc 6f 72 2c 20 70 54 6f 73 2d 3e 75 2e 69 2c 20 70  or, pTos->u.i, p
152cd 4f 70 2d 3e 70 33 2c 20 6e 41 72 67 2c 20 61 70  Op->p3, nArg, ap
152ce 41 72 67 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56  Arg);.    p->inV
152cf 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20  tabMethod = 0;. 
152d0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
152d1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
152d2 73 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f  s = pModule->xEo
152d3 66 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  f(pCur->pVtabCur
152d4 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  sor);.    }.    
152d5 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
152d6 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61  yOn(db) ) goto a
152d7 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
152d8 73 65 3b 0a 0a 20 20 20 20 69 66 28 20 72 65 73  se;..    if( res
152d9 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70   ){.      pc = p
152da 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
152db 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 6f 70 20  }.  }..  /* Pop 
152dc 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72  the index number
152dd 2c 20 61 72 67 63 20 76 61 6c 75 65 20 61 6e 64  , argc value and
152de 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 66 20   parameters off 
152df 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 70  the stack */.  p
152e0 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c 20 32  opStack(&pTos, 2
152e1 2b 6e 41 72 67 29 3b 0a 20 20 62 72 65 61 6b 3b  +nArg);.  break;
152e2 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
152e3 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
152e4 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
152e5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
152e6 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
152e7 63 6f 64 65 3a 20 56 52 6f 77 69 64 20 50 31 20  code: VRowid P1 
152e8 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 75 73 68 20 61  * *.**.** Push a
152e9 6e 20 69 6e 74 65 67 65 72 20 6f 6e 74 6f 20 74  n integer onto t
152ea 68 65 20 73 74 61 63 6b 20 77 68 69 63 68 20 69  he stack which i
152eb 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a  s the rowid of.*
152ec 2a 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61  * the virtual-ta
152ed 62 6c 65 20 74 68 61 74 20 74 68 65 20 50 31 20  ble that the P1 
152ee 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69  cursor is pointi
152ef 6e 67 20 74 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ng to..*/.case O
152f0 50 5f 56 52 6f 77 69 64 3a 20 7b 0a 20 20 63 6f  P_VRowid: {.  co
152f1 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
152f2 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20  le *pModule;..  
152f3 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70  Cursor *pCur = p
152f4 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
152f5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
152f6 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b  ->pVtabCursor );
152f7 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 43 75  .  pModule = pCu
152f8 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e  r->pVtabCursor->
152f9 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  pVtab->pModule;.
152fa 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78    if( pModule->x
152fb 52 6f 77 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20  Rowid==0 ){.    
152fc 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
152fd 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 55  (&p->zErrMsg, "U
152fe 6e 73 75 70 70 6f 72 74 65 64 20 6d 6f 64 75 6c  nsupported modul
152ff 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 78 52 6f  e operation: xRo
15300 77 69 64 22 2c 20 30 29 3b 0a 20 20 20 20 72 63  wid", 0);.    rc
15301 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
15302 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
15303 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 6f  sqlite_int64 iRo
15304 77 3b 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69  w;..    if( sqli
15305 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
15306 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
15307 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
15308 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78   rc = pModule->x
15309 52 6f 77 69 64 28 70 43 75 72 2d 3e 70 56 74 61  Rowid(pCur->pVta
1530a 62 43 75 72 73 6f 72 2c 20 26 69 52 6f 77 29 3b  bCursor, &iRow);
1530b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1530c 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1530d 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1530e 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 20 70 54  _misuse;..    pT
1530f 6f 73 2b 2b 3b 0a 20 20 20 20 70 54 6f 73 2d 3e  os++;.    pTos->
15310 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
15311 0a 20 20 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d  .    pTos->u.i =
15312 20 69 52 6f 77 3b 0a 20 20 7d 0a 0a 20 20 62 72   iRow;.  }..  br
15313 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
15314 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
15315 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69  TUALTABLE */..#i
15316 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15317 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
15318 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d  * Opcode: VColum
15319 6e 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  n P1 P2 *.**.** 
1531a 50 75 73 68 20 6f 6e 74 6f 20 74 68 65 20 73 74  Push onto the st
1531b 61 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ack the value of
1531c 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d   the P2-th colum
1531d 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20  n of.** the row 
1531e 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  of the virtual-t
1531f 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 50 31  able that the P1
15320 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74   cursor is point
15321 69 6e 67 20 74 6f 2e 0a 2a 2f 0a 63 61 73 65 20  ing to..*/.case 
15322 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 20 20  OP_VColumn: {.  
15323 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
15324 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a  dule *pModule;..
15325 20 20 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d    Cursor *pCur =
15326 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
15327 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  1];.  assert( pC
15328 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  ur->pVtabCursor 
15329 29 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70  );.  pModule = p
1532a 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
1532b 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  ->pVtab->pModule
1532c 3b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d  ;.  if( pModule-
1532d 3e 78 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20  >xColumn==0 ){. 
1532e 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1532f 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
15330 20 22 55 6e 73 75 70 70 6f 72 74 65 64 20 6d 6f   "Unsupported mo
15331 64 75 6c 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20  dule operation: 
15332 78 43 6f 6c 75 6d 6e 22 2c 20 30 29 3b 0a 20 20  xColumn", 0);.  
15333 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
15334 52 4f 52 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  ROR;.  } else {.
15335 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74      sqlite3_cont
15336 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20  ext sContext;.  
15337 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74 65    memset(&sConte
15338 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 43  xt, 0, sizeof(sC
15339 6f 6e 74 65 78 74 29 29 3b 0a 20 20 20 20 73 43  ontext));.    sC
1533a 6f 6e 74 65 78 74 2e 73 2e 66 6c 61 67 73 20 3d  ontext.s.flags =
1533b 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 73   MEM_Null;.    s
1533c 43 6f 6e 74 65 78 74 2e 73 2e 64 62 20 3d 20 64  Context.s.db = d
1533d 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  b;.    if( sqlit
1533e 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1533f 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
15340 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20  _to_misuse;.    
15341 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  rc = pModule->xC
15342 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61  olumn(pCur->pVta
15343 62 43 75 72 73 6f 72 2c 20 26 73 43 6f 6e 74 65  bCursor, &sConte
15344 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 0a 20  xt, pOp->p2);.. 
15345 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72     /* Copy the r
15346 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e  esult of the fun
15347 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 74 6f 70  ction to the top
15348 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20 57   of the stack. W
15349 65 0a 20 20 20 20 2a 2a 20 64 6f 20 74 68 69 73  e.    ** do this
1534a 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
1534b 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e  hether or not an
1534c 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 74   error occured t
1534d 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 20  o ensure any.   
1534e 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f   ** dynamic allo
1534f 63 61 74 69 6f 6e 20 69 6e 20 73 43 6f 6e 74 65  cation in sConte
15350 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75  xt.s (a Mem stru
15351 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65 64  ct) is  released
15352 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
15353 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
15354 63 6f 64 69 6e 67 28 26 73 43 6f 6e 74 65 78 74  coding(&sContext
15355 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  .s, encoding);. 
15356 20 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 20 20 70     pTos++;.    p
15357 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a  Tos->flags = 0;.
15358 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
15359 65 6d 4d 6f 76 65 28 70 54 6f 73 2c 20 26 73 43  emMove(pTos, &sC
1535a 6f 6e 74 65 78 74 2e 73 29 3b 0a 0a 20 20 20 20  ontext.s);..    
1535b 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1535c 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 20  yOn(db) ){.     
1535d 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1535e 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 7d  to_misuse;.    }
1535f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
15360 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 54  VdbeMemTooBig(pT
15361 6f 73 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  os) ){.      got
15362 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d  o too_big;.    }
15363 0a 20 20 7d 0a 20 20 0a 20 20 62 72 65 61 6b 3b  .  }.  .  break;
15364 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
15365 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
15366 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
15367 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
15368 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
15369 63 6f 64 65 3a 20 56 4e 65 78 74 20 50 31 20 50  code: VNext P1 P
1536a 32 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63  2 *.**.** Advanc
1536b 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
1536c 50 31 20 74 6f 20 74 68 65 20 6e 65 78 74 20 72  P1 to the next r
1536d 6f 77 20 69 6e 20 69 74 73 20 72 65 73 75 6c 74  ow in its result
1536e 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70   set and.** jump
1536f 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   to instruction 
15370 50 32 2e 20 20 4f 72 2c 20 69 66 20 74 68 65 20  P2.  Or, if the 
15371 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  virtual table ha
15372 73 20 72 65 61 63 68 65 64 0a 2a 2a 20 74 68 65  s reached.** the
15373 20 65 6e 64 20 6f 66 20 69 74 73 20 72 65 73 75   end of its resu
15374 6c 74 20 73 65 74 2c 20 74 68 65 6e 20 66 61 6c  lt set, then fal
15375 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
15376 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
15377 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4e  n..*/.case OP_VN
15378 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6e 6f 2d 70  ext: {   /* no-p
15379 75 73 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ush */.  const s
1537a 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
1537b 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65  Module;.  int re
1537c 73 20 3d 20 30 3b 0a 0a 20 20 43 75 72 73 6f 72  s = 0;..  Cursor
1537d 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73   *pCur = p->apCs
1537e 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1537f 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61  sert( pCur->pVta
15380 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 4d 6f  bCursor );.  pMo
15381 64 75 6c 65 20 3d 20 70 43 75 72 2d 3e 70 56 74  dule = pCur->pVt
15382 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d  abCursor->pVtab-
15383 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 66 28 20  >pModule;.  if( 
15384 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 3d 3d  pModule->xNext==
15385 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
15386 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
15387 72 72 4d 73 67 2c 20 22 55 6e 73 75 70 70 6f 72  rrMsg, "Unsuppor
15388 74 65 64 20 6d 6f 64 75 6c 65 20 6f 70 65 72 61  ted module opera
15389 74 69 6f 6e 3a 20 78 4e 65 78 74 22 2c 20 30 29  tion: xNext", 0)
1538a 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1538b 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 20 65 6c 73  E_ERROR;.  } els
1538c 65 20 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  e {.    /* Invok
1538d 65 20 74 68 65 20 78 4e 65 78 74 28 29 20 6d 65  e the xNext() me
1538e 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75  thod of the modu
1538f 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20  le. There is no 
15390 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20 20 20  way for the.    
15391 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d  ** underlying im
15392 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20  plementation to 
15393 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
15394 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75  if one occurs du
15395 72 69 6e 67 0a 20 20 20 20 2a 2a 20 78 4e 65 78  ring.    ** xNex
15396 74 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66  t(). Instead, if
15397 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
15398 2c 20 74 72 75 65 20 69 73 20 72 65 74 75 72 6e  , true is return
15399 65 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74  ed (indicating t
1539a 68 61 74 20 0a 20 20 20 20 2a 2a 20 64 61 74 61  hat .    ** data
1539b 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29 20 61   is available) a
1539c 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  nd the error cod
1539d 65 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20  e returned when 
1539e 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 20 20 2a  xColumn or.    *
1539f 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74  * some other met
153a0 68 6f 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f  hod is next invo
153a1 6b 65 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20  ked on the save 
153a2 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
153a3 72 73 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rsor..    */.   
153a4 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
153a5 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
153a6 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
153a7 73 75 73 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56  suse;.    p->inV
153a8 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20  tabMethod = 1;. 
153a9 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d     rc = pModule-
153aa 3e 78 4e 65 78 74 28 70 43 75 72 2d 3e 70 56 74  >xNext(pCur->pVt
153ab 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 70  abCursor);.    p
153ac 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
153ad 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   0;.    if( rc==
153ae 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
153af 20 20 20 72 65 73 20 3d 20 70 4d 6f 64 75 6c 65     res = pModule
153b0 2d 3e 78 45 6f 66 28 70 43 75 72 2d 3e 70 56 74  ->xEof(pCur->pVt
153b1 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d  abCursor);.    }
153b2 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
153b3 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
153b4 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
153b5 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 20 69 66  _misuse;..    if
153b6 28 20 21 72 65 73 20 29 7b 0a 20 20 20 20 20 20  ( !res ){.      
153b7 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 64  /* If there is d
153b8 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20  ata, jump to P2 
153b9 2a 2f 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  */.      pc = pO
153ba 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d  p->p2 - 1;.    }
153bb 0a 20 20 7d 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d  .  }..  break;.}
153bc 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
153bd 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
153be 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
153bf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
153c0 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
153c1 64 65 3a 20 56 52 65 6e 61 6d 65 20 2a 20 2a 20  de: VRename * * 
153c2 50 33 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61  P3.**.** P3 is a
153c3 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69   pointer to a vi
153c4 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65  rtual table obje
153c5 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76  ct, an sqlite3_v
153c6 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  tab structure..*
153c7 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e  * This opcode in
153c8 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73  vokes the corres
153c9 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20  ponding xRename 
153ca 6d 65 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75  method. The valu
153cb 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 74 6f 70 20  e.** on the top 
153cc 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69 73 20  of the stack is 
153cd 70 6f 70 70 65 64 20 61 6e 64 20 70 61 73 73 65  popped and passe
153ce 64 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61  d as the zName a
153cf 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
153d0 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64  e xRename method
153d1 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 65  ..*/.case OP_VRe
153d2 6e 61 6d 65 3a 20 7b 20 20 20 2f 2a 20 6e 6f 2d  name: {   /* no-
153d3 70 75 73 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65  push */.  sqlite
153d4 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20  3_vtab *pVtab = 
153d5 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29  (sqlite3_vtab *)
153d6 28 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 61 73 73  (pOp->p3);.  ass
153d7 65 72 74 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64  ert( pVtab->pMod
153d8 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a  ule->xRename );.
153d9 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 54 6f  .  Stringify(pTo
153da 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 0a 20  s, encoding);.. 
153db 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
153dc 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
153dd 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
153de 73 75 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 56  suse;.  sqlite3V
153df 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a  tabLock(pVtab);.
153e0 20 20 72 63 20 3d 20 70 56 74 61 62 2d 3e 70 4d    rc = pVtab->pM
153e1 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 28 70  odule->xRename(p
153e2 56 74 61 62 2c 20 70 54 6f 73 2d 3e 7a 29 3b 0a  Vtab, pTos->z);.
153e3 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
153e4 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a  ock(db, pVtab);.
153e5 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
153e6 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
153e7 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
153e8 73 75 73 65 3b 0a 0a 20 20 70 6f 70 53 74 61 63  suse;..  popStac
153e9 6b 28 26 70 54 6f 73 2c 20 31 29 3b 0a 20 20 62  k(&pTos, 1);.  b
153ea 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  reak;.}.#endif..
153eb 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
153ec 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
153ed 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55 70 64  ./* Opcode: VUpd
153ee 61 74 65 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  ate P1 P2 P3.**.
153ef 2a 2a 20 50 33 20 69 73 20 61 20 70 6f 69 6e 74  ** P3 is a point
153f0 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20  er to a virtual 
153f1 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e  table object, an
153f2 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74   sqlite3_vtab st
153f3 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73  ructure..** This
153f4 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
153f5 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
153f6 67 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64  g xUpdate method
153f7 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a 20 61  . P2 values.** a
153f8 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68  re taken from th
153f9 65 20 73 74 61 63 6b 20 74 6f 20 70 61 73 73 20  e stack to pass 
153fa 74 6f 20 74 68 65 20 78 55 70 64 61 74 65 20 69  to the xUpdate i
153fb 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 68 65 0a 2a  nvocation. The.*
153fc 2a 20 76 61 6c 75 65 20 6f 6e 20 74 68 65 20 74  * value on the t
153fd 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
153fe 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
153ff 68 65 20 70 32 74 68 20 65 6c 65 6d 65 6e 74 20  he p2th element 
15400 0a 2a 2a 20 6f 66 20 74 68 65 20 61 72 67 76 20  .** of the argv 
15401 61 72 72 61 79 20 70 61 73 73 65 64 20 74 6f 20  array passed to 
15402 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54  xUpdate..**.** T
15403 68 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f  he xUpdate metho
15404 64 20 77 69 6c 6c 20 64 6f 20 61 20 44 45 4c 45  d will do a DELE
15405 54 45 20 6f 72 20 61 6e 20 49 4e 53 45 52 54 20  TE or an INSERT 
15406 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 68 65 20  or both..** The 
15407 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65 6e 74 20  argv[0] element 
15408 28 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e  (which correspon
15409 64 73 20 74 6f 20 74 68 65 20 50 32 2d 74 68 20  ds to the P2-th 
1540a 65 6c 65 6d 65 6e 74 20 64 6f 77 6e 0a 2a 2a 20  element down.** 
1540b 6f 6e 20 74 68 65 20 73 74 61 63 6b 29 20 69 73  on the stack) is
1540c 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20   the rowid of a 
1540d 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20  row to delete.  
1540e 49 66 20 61 72 67 76 5b 30 5d 20 69 73 0a 2a 2a  If argv[0] is.**
1540f 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 64 65   NULL then no de
15410 6c 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20  letion occurs.  
15411 54 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d  The argv[1] elem
15412 65 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69 64  ent is the rowid
15413 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 72  .** of the new r
15414 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ow.  This can be
15415 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68   NULL to have th
15416 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 0a  e virtual table.
15417 2a 2a 20 73 65 6c 65 63 74 20 74 68 65 20 6e 65  ** select the ne
15418 77 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73 65  w rowid for itse
15419 6c 66 2e 20 20 54 68 65 20 68 69 67 68 65 72 20  lf.  The higher 
1541a 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 0a  elements in the.
1541b 2a 2a 20 73 74 61 63 6b 20 61 72 65 20 74 68 65  ** stack are the
1541c 20 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c 75 6d   values of colum
1541d 6e 73 20 69 6e 20 74 68 65 20 6e 65 77 20 72 6f  ns in the new ro
1541e 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d  w..**.** If P2==
1541f 31 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65 72 74  1 then no insert
15420 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20   is performed.  
15421 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72  argv[0] is the r
15422 6f 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72 6f 77  owid of.** a row
15423 20 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a   to delete..**.*
15424 2a 20 50 31 20 69 73 20 61 20 62 6f 6f 6c 65 61  * P1 is a boolea
15425 6e 20 66 6c 61 67 2e 20 49 66 20 69 74 20 69 73  n flag. If it is
15426 20 73 65 74 20 74 6f 20 74 72 75 65 20 61 6e 64   set to true and
15427 20 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c   the xUpdate cal
15428 6c 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66  l.** is successf
15429 75 6c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ul, then the val
1542a 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
1542b 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
1542c 72 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a 20 69  rt_rowid() .** i
1542d 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c  s set to the val
1542e 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20  ue of the rowid 
1542f 66 6f 72 20 74 68 65 20 72 6f 77 20 6a 75 73 74  for the row just
15430 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 63 61   inserted..*/.ca
15431 73 65 20 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b  se OP_VUpdate: {
15432 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
15433 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
15434 2a 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74 65  *pVtab = (sqlite
15435 33 5f 76 74 61 62 20 2a 29 28 70 4f 70 2d 3e 70  3_vtab *)(pOp->p
15436 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f  3);.  sqlite3_mo
15437 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20  dule *pModule = 
15438 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20  (sqlite3_module 
15439 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  *)pVtab->pModule
1543a 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20 3d 20 70  ;.  int nArg = p
1543b 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74  Op->p2;.  assert
1543c 28 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50  ( pOp->p3type==P
1543d 33 5f 56 54 41 42 20 29 3b 0a 20 20 69 66 28 20  3_VTAB );.  if( 
1543e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65  pModule->xUpdate
1543f 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
15440 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
15441 7a 45 72 72 4d 73 67 2c 20 22 72 65 61 64 2d 6f  zErrMsg, "read-o
15442 6e 6c 79 20 74 61 62 6c 65 22 2c 20 30 29 3b 0a  nly table", 0);.
15443 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
15444 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ERROR;.  }else{.
15445 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73      int i;.    s
15446 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 69  qlite_int64 rowi
15447 64 3b 0a 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41  d;.    Mem **apA
15448 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20  rg = p->apArg;. 
15449 20 20 20 4d 65 6d 20 2a 70 58 20 3d 20 26 70 54     Mem *pX = &pT
1544a 6f 73 5b 31 2d 6e 41 72 67 5d 3b 0a 20 20 20 20  os[1-nArg];.    
1544b 66 6f 72 28 69 20 3d 20 30 3b 20 69 3c 6e 41 72  for(i = 0; i<nAr
1544c 67 3b 20 69 2b 2b 2c 20 70 58 2b 2b 29 7b 0a 20  g; i++, pX++){. 
1544d 20 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e       storeTypeIn
1544e 66 6f 28 70 58 2c 20 30 29 3b 0a 20 20 20 20 20  fo(pX, 0);.     
1544f 20 61 70 41 72 67 5b 69 5d 20 3d 20 70 58 3b 0a   apArg[i] = pX;.
15450 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
15451 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
15452 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
15453 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
15454 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f     sqlite3VtabLo
15455 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 20 20 72  ck(pVtab);.    r
15456 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 55 70  c = pModule->xUp
15457 64 61 74 65 28 70 56 74 61 62 2c 20 6e 41 72 67  date(pVtab, nArg
15458 2c 20 61 70 41 72 67 2c 20 26 72 6f 77 69 64 29  , apArg, &rowid)
15459 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61  ;.    sqlite3Vta
1545a 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61  bUnlock(db, pVta
1545b 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  b);.    if( sqli
1545c 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1545d 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1545e 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20  _to_misuse;.    
1545f 69 66 28 20 70 4f 70 2d 3e 70 31 20 26 26 20 72  if( pOp->p1 && r
15460 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
15461 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41        assert( nA
15462 72 67 3e 31 20 26 26 20 61 70 41 72 67 5b 30 5d  rg>1 && apArg[0]
15463 20 26 26 20 28 61 70 41 72 67 5b 30 5d 2d 3e 66   && (apArg[0]->f
15464 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29  lags&MEM_Null) )
15465 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74  ;.      db->last
15466 52 6f 77 69 64 20 3d 20 72 6f 77 69 64 3b 0a 20  Rowid = rowid;. 
15467 20 20 20 7d 0a 20 20 7d 0a 20 20 70 6f 70 53 74     }.  }.  popSt
15468 61 63 6b 28 26 70 54 6f 73 2c 20 6e 41 72 67 29  ack(&pTos, nArg)
15469 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  ;.  break;.}.#en
1546a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1546b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
1546c 2a 2f 0a 0a 2f 2a 20 41 6e 20 6f 74 68 65 72 20  */../* An other 
1546d 6f 70 63 6f 64 65 20 69 73 20 69 6c 6c 65 67 61  opcode is illega
1546e 6c 2e 2e 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a  l....*/.default:
1546f 20 7b 0a 20 20 61 73 73 65 72 74 28 20 30 20 29   {.  assert( 0 )
15470 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
15471 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15472 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15473 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15474 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15475 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
15476 54 68 65 20 63 61 73 65 73 20 6f 66 20 74 68 65  The cases of the
15477 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e   switch statemen
15478 74 20 61 62 6f 76 65 20 74 68 69 73 20 6c 69 6e  t above this lin
15479 65 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20  e should all be 
1547a 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 62 79 20 36  indented.** by 6
1547b 20 73 70 61 63 65 73 2e 20 20 42 75 74 20 74 68   spaces.  But th
1547c 65 20 6c 65 66 74 2d 6d 6f 73 74 20 36 20 73 70  e left-most 6 sp
1547d 61 63 65 73 20 68 61 76 65 20 62 65 65 6e 20 72  aces have been r
1547e 65 6d 6f 76 65 64 20 74 6f 20 69 6d 70 72 6f 76  emoved to improv
1547f 65 20 74 68 65 0a 2a 2a 20 72 65 61 64 61 62 69  e the.** readabi
15480 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74 68 69 73  lity.  From this
15481 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20   point on down, 
15482 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e  the normal inden
15483 74 61 74 69 6f 6e 20 72 75 6c 65 73 20 61 72 65  tation rules are
15484 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e 0a 2a 2a  .** restored..**
15485 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15486 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15488 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15489 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20  ***********/.   
1548a 20 7d 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20   }..    /* Make 
1548b 73 75 72 65 20 74 68 65 20 73 74 61 63 6b 20 6c  sure the stack l
1548c 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65 78 63  imit was not exc
1548d 65 65 64 65 64 20 2a 2f 0a 20 20 20 20 61 73 73  eeded */.    ass
1548e 65 72 74 28 20 70 54 6f 73 3c 3d 70 53 74 61 63  ert( pTos<=pStac
1548f 6b 4c 69 6d 69 74 20 29 3b 0a 0a 23 69 66 64 65  kLimit );..#ifde
15490 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20  f VDBE_PROFILE. 
15491 20 20 20 7b 0a 20 20 20 20 20 20 6c 6f 6e 67 20     {.      long 
15492 6c 6f 6e 67 20 65 6c 61 70 73 65 20 3d 20 68 77  long elapse = hw
15493 74 69 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a  time() - start;.
15494 20 20 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65        pOp->cycle
15495 73 20 2b 3d 20 65 6c 61 70 73 65 3b 0a 20 20 20  s += elapse;.   
15496 20 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23     pOp->cnt++;.#
15497 69 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72  if 0.        fpr
15498 69 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31  intf(stdout, "%1
15499 30 6c 6c 64 20 22 2c 20 65 6c 61 70 73 65 29 3b  0lld ", elapse);
1549a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1549b 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f  VdbePrintOp(stdo
1549c 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d 3e  ut, origPc, &p->
1549d 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65  aOp[origPc]);.#e
1549e 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ndif.    }.#endi
1549f 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
154a0 5f 54 45 53 54 0a 20 20 20 20 2f 2a 20 4b 65 65  _TEST.    /* Kee
154a1 70 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 73  p track of the s
154a2 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65  ize of the large
154a3 73 74 20 42 4c 4f 42 20 6f 72 20 53 54 52 20 74  st BLOB or STR t
154a4 68 61 74 20 68 61 73 20 61 70 70 65 61 72 65 64  hat has appeared
154a5 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 74  .    ** on the t
154a6 6f 70 20 6f 66 20 74 68 65 20 56 44 42 45 20 73  op of the VDBE s
154a7 74 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tack..    */.   
154a8 20 69 66 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53   if( pTos>=p->aS
154a9 74 61 63 6b 20 26 26 20 28 70 54 6f 73 2d 3e 66  tack && (pTos->f
154aa 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62  lags & (MEM_Blob
154ab 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 0a 20 20  |MEM_Str))!=0.  
154ac 20 20 20 20 20 20 20 26 26 20 70 54 6f 73 2d 3e         && pTos->
154ad 6e 3e 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c  n>sqlite3_max_bl
154ae 6f 62 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  obsize ){.      
154af 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62  sqlite3_max_blob
154b0 73 69 7a 65 20 3d 20 70 54 6f 73 2d 3e 6e 3b 0a  size = pTos->n;.
154b1 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
154b2 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
154b3 6e 67 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74  ng code adds not
154b4 68 69 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75  hing to the actu
154b5 61 6c 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  al functionality
154b6 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70  .    ** of the p
154b7 72 6f 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f  rogram.  It is o
154b8 6e 6c 79 20 68 65 72 65 20 66 6f 72 20 74 65 73  nly here for tes
154b9 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69  ting and debuggi
154ba 6e 67 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68  ng..    ** On th
154bb 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74  e other hand, it
154bc 20 64 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63   does burn CPU c
154bd 79 63 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65  ycles every time
154be 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20   through.    ** 
154bf 74 68 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f  the evaluator lo
154c0 6f 70 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c  op.  So we can l
154c1 65 61 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e  eave it out when
154c2 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e   NDEBUG is defin
154c3 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64  ed..    */.#ifnd
154c4 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a  ef NDEBUG.    /*
154c5 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   Sanity checking
154c6 20 6f 6e 20 74 68 65 20 74 6f 70 20 65 6c 65 6d   on the top elem
154c7 65 6e 74 20 6f 66 20 74 68 65 20 73 74 61 63 6b  ent of the stack
154c8 2e 20 49 66 20 74 68 65 20 70 72 65 76 69 6f 75  . If the previou
154c9 73 0a 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63  s.    ** instruc
154ca 74 69 6f 6e 20 77 61 73 20 56 4e 6f 43 68 61 6e  tion was VNoChan
154cb 67 65 2c 20 74 68 65 6e 20 74 68 65 20 66 6c 61  ge, then the fla
154cc 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  gs field of the 
154cd 74 6f 70 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  top.    ** of th
154ce 65 20 73 74 61 63 6b 20 69 73 20 73 65 74 20 74  e stack is set t
154cf 6f 20 30 2e 20 54 68 69 73 20 69 73 20 74 65 63  o 0. This is tec
154d0 68 6e 69 63 61 6c 6c 79 20 69 6e 76 61 6c 69 64  hnically invalid
154d1 20 66 6f 72 20 61 20 6d 65 6d 6f 72 79 0a 20 20   for a memory.  
154d2 20 20 2a 2a 20 63 65 6c 6c 2c 20 73 6f 20 61 76    ** cell, so av
154d3 6f 69 64 20 63 61 6c 6c 69 6e 67 20 4d 65 6d 53  oid calling MemS
154d4 61 6e 69 74 79 28 29 20 69 6e 20 74 68 69 73 20  anity() in this 
154d5 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  case..    */.   
154d6 20 69 66 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53   if( pTos>=p->aS
154d7 74 61 63 6b 20 26 26 20 70 54 6f 73 2d 3e 66 6c  tack && pTos->fl
154d8 61 67 73 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ags ){.      ass
154d9 65 72 74 28 20 70 54 6f 73 2d 3e 64 62 3d 3d 64  ert( pTos->db==d
154da 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  b );.      sqlit
154db 65 33 56 64 62 65 4d 65 6d 53 61 6e 69 74 79 28  e3VdbeMemSanity(
154dc 70 54 6f 73 29 3b 0a 20 20 20 20 20 20 61 73 73  pTos);.      ass
154dd 65 72 74 28 20 21 73 71 6c 69 74 65 33 56 64 62  ert( !sqlite3Vdb
154de 65 4d 65 6d 54 6f 6f 42 69 67 28 70 54 6f 73 29  eMemTooBig(pTos)
154df 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73   );.    }.    as
154e0 73 65 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20  sert( pc>=-1 && 
154e1 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69  pc<p->nOp );..#i
154e2 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
154e3 47 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 66 6f  G.    /* Code fo
154e4 72 20 74 72 61 63 69 6e 67 20 74 68 65 20 76 64  r tracing the vd
154e5 62 65 20 73 74 61 63 6b 2e 20 2a 2f 0a 20 20 20  be stack. */.   
154e6 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 26 26   if( p->trace &&
154e7 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b   pTos>=p->aStack
154e8 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
154e9 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 70  .      fprintf(p
154ea 2d 3e 74 72 61 63 65 2c 20 22 53 74 61 63 6b 3a  ->trace, "Stack:
154eb 22 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  ");.      for(i=
154ec 30 3b 20 69 3e 2d 35 20 26 26 20 26 70 54 6f 73  0; i>-5 && &pTos
154ed 5b 69 5d 3e 3d 70 2d 3e 61 53 74 61 63 6b 3b 20  [i]>=p->aStack; 
154ee 69 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 69 66  i--){.        if
154ef 28 20 70 54 6f 73 5b 69 5d 2e 66 6c 61 67 73 20  ( pTos[i].flags 
154f0 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
154f1 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
154f2 70 2d 3e 74 72 61 63 65 2c 20 22 20 4e 55 4c 4c  p->trace, " NULL
154f3 22 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ");.        }els
154f4 65 20 69 66 28 20 28 70 54 6f 73 5b 69 5d 2e 66  e if( (pTos[i].f
154f5 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c  lags & (MEM_Int|
154f6 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 4d 5f  MEM_Str))==(MEM_
154f7 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a  Int|MEM_Str) ){.
154f8 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
154f9 66 28 70 2d 3e 74 72 61 63 65 2c 20 22 20 73 69  f(p->trace, " si
154fa 3a 25 6c 6c 64 22 2c 20 70 54 6f 73 5b 69 5d 2e  :%lld", pTos[i].
154fb 75 2e 69 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  u.i);.        }e
154fc 6c 73 65 20 69 66 28 20 70 54 6f 73 5b 69 5d 2e  lse if( pTos[i].
154fd 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
154fe 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72  ){.          fpr
154ff 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 20 22  intf(p->trace, "
15500 20 69 3a 25 6c 6c 64 22 2c 20 70 54 6f 73 5b 69   i:%lld", pTos[i
15501 5d 2e 75 2e 69 29 3b 0a 20 20 20 20 20 20 20 20  ].u.i);.        
15502 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 73 5b 69  }else if( pTos[i
15503 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ].flags & MEM_Re
15504 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  al ){.          
15505 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65  fprintf(p->trace
15506 2c 20 22 20 72 3a 25 67 22 2c 20 70 54 6f 73 5b  , " r:%g", pTos[
15507 69 5d 2e 72 29 3b 0a 20 20 20 20 20 20 20 20 7d  i].r);.        }
15508 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
15509 63 68 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a  char zBuf[200];.
1550a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1550b 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72  3VdbeMemPrettyPr
1550c 69 6e 74 28 26 70 54 6f 73 5b 69 5d 2c 20 7a 42  int(&pTos[i], zB
1550d 75 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66  uf);.          f
1550e 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c  printf(p->trace,
1550f 20 22 20 22 29 3b 0a 20 20 20 20 20 20 20 20 20   " ");.         
15510 20 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63   fprintf(p->trac
15511 65 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a  e, "%s", zBuf);.
15512 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15513 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  }.      if( rc!=
15514 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d 3e 74  0 ) fprintf(p->t
15515 72 61 63 65 2c 22 20 72 63 3d 25 64 22 2c 72 63  race," rc=%d",rc
15516 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  );.      fprintf
15517 28 70 2d 3e 74 72 61 63 65 2c 22 5c 6e 22 29 3b  (p->trace,"\n");
15518 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 20 2f  .    }.#endif  /
15519 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a  * SQLITE_DEBUG *
1551a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44 45  /.#endif  /* NDE
1551b 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a 20 54  BUG */.  }  /* T
1551c 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 6f  he end of the fo
1551d 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 20 6c  r(;;) loop the l
1551e 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 6f 70 63  oops through opc
1551f 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  odes */..  /* If
15520 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70   we reach this p
15521 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 74  oint, it means t
15522 68 61 74 20 65 78 65 63 75 74 69 6f 6e 20 69 73  hat execution is
15523 20 66 69 6e 69 73 68 65 64 2e 0a 20 20 2a 2f 0a   finished..  */.
15524 76 64 62 65 5f 68 61 6c 74 3a 0a 20 20 69 66 28  vdbe_halt:.  if(
15525 20 72 63 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63   rc ){.    p->rc
15526 20 3d 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20   = rc;.    rc = 
15527 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
15528 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
15529 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
1552a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61  .  sqlite3VdbeHa
1552b 6c 74 28 70 29 3b 0a 20 20 70 2d 3e 70 54 6f 73  lt(p);.  p->pTos
1552c 20 3d 20 70 54 6f 73 3b 0a 0a 20 20 2f 2a 20 54   = pTos;..  /* T
1552d 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  his is the only 
1552e 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20  way out of this 
1552f 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68  procedure.  We h
15530 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65  ave to.  ** rele
15531 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20  ase the mutexes 
15532 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77  on btrees that w
15533 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20  ere acquired at 
15534 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f  the.  ** top. */
15535 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20  .vdbe_return:.  
15536 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
15537 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e  xArrayLeave(&p->
15538 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  aMutex);.  retur
15539 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70  n rc;..  /* Jump
1553a 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74   to here if a st
1553b 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72  ring or blob lar
1553c 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  ger than SQLITE_
1553d 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20  MAX_LENGTH.  ** 
1553e 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  is encountered..
1553f 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20    */.too_big:.  
15540 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
15541 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 73  (&p->zErrMsg, "s
15542 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f  tring or blob to
15543 6f 20 62 69 67 22 2c 20 28 63 68 61 72 2a 29 30  o big", (char*)0
15544 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  );.  rc = SQLITE
15545 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 6f 20  _TOOBIG;.  goto 
15546 76 64 62 65 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  vdbe_halt;..  /*
15547 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66   Jump to here if
15548 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
15549 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a  s..  */.no_mem:.
1554a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1554b 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  ed = 1;.  sqlite
1554c 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1554d 45 72 72 4d 73 67 2c 20 22 6f 75 74 20 6f 66 20  ErrMsg, "out of 
1554e 6d 65 6d 6f 72 79 22 2c 20 28 63 68 61 72 2a 29  memory", (char*)
1554f 30 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54  0);.  rc = SQLIT
15550 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f 20  E_NOMEM;.  goto 
15551 76 64 62 65 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  vdbe_halt;..  /*
15552 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f   Jump to here fo
15553 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  r an SQLITE_MISU
15554 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61  SE error..  */.a
15555 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
15556 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54  se:.  rc = SQLIT
15557 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46  E_MISUSE;.  /* F
15558 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62  all thru into ab
15559 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1555a 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74   */..  /* Jump t
1555b 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f  o here for any o
1555c 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74  ther kind of fat
1555d 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22  al error.  The "
1555e 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a  rc" variable.  *
1555f 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68  * should hold th
15560 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a  e error number..
15561 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74    */.abort_due_t
15562 6f 5f 65 72 72 6f 72 3a 0a 20 20 69 66 28 20 70  o_error:.  if( p
15563 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a  ->zErrMsg==0 ){.
15564 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
15565 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20  ocFailed ) rc = 
15566 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15567 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
15568 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
15569 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63  sqlite3ErrStr(rc
1556a 29 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  ), (char*)0);.  
1556b 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 68 61  }.  goto vdbe_ha
1556c 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74  lt;..  /* Jump t
1556d 6f 20 68 65 72 65 20 69 66 20 74 68 65 20 73 71  o here if the sq
1556e 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
1556f 29 20 41 50 49 20 73 65 74 73 20 74 68 65 20 69  ) API sets the i
15570 6e 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c  nterrupt.  ** fl
15571 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64  ag..  */.abort_d
15572 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a  ue_to_interrupt:
15573 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 75  .  assert( db->u
15574 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
15575 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67  );.  if( db->mag
15576 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic!=SQLITE_MAGIC
15577 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 72 63 20  _BUSY ){.    rc 
15578 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
15579 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
1557a 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52   = SQLITE_INTERR
1557b 55 50 54 3b 0a 20 20 7d 0a 20 20 70 2d 3e 72 63  UPT;.  }.  p->rc
1557c 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33   = rc;.  sqlite3
1557d 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1557e 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 45 72  rrMsg, sqlite3Er
1557f 72 53 74 72 28 72 63 29 2c 20 28 63 68 61 72 2a  rStr(rc), (char*
15580 29 30 29 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65  )0);.  goto vdbe
15581 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  _halt;.}../*****
15582 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
15583 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.c ********
15584 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15585 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15586 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
15587 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
15588 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 20  file vdbeblob.c 
15589 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1558a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1558b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1558c 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a  2007 May 1.**.**
1558d 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1558e 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1558f 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
15590 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
15591 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
15592 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
15593 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
15594 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
15595 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
15596 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
15597 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
15598 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
15599 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1559a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1559b 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1559c 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1559d 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1559e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1559f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
155a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
155a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
155a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
155a3 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
155a4 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
155a5 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d  implement increm
155a6 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a  ental BLOB I/O..
155a7 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 62  **.** $Id: vdbeb
155a8 6c 6f 62 2e 63 2c 76 20 31 2e 31 36 20 32 30 30  lob.c,v 1.16 200
155a9 37 2f 30 38 2f 33 30 20 30 31 3a 31 39 3a 35 39  7/08/30 01:19:59
155aa 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
155ab 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
155ac 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a  MIT_INCRBLOB../*
155ad 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65  .** Valid sqlite
155ae 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20  3_blob* handles 
155af 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f  point to Incrblo
155b0 62 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  b structures..*/
155b1 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
155b2 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f  Incrblob Incrblo
155b3 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c  b;.struct Incrbl
155b4 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73  ob {.  int flags
155b5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
155b6 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73  * Copy of "flags
155b7 22 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  " passed to sqli
155b8 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20  te3_blob_open() 
155b9 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20  */.  int nByte; 
155ba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
155bb 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f  Size of open blo
155bc 62 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  b, in bytes */. 
155bd 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20   int iOffset;   
155be 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
155bf 20 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20   offset of blob 
155c0 69 6e 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a  in cursor data *
155c1 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
155c2 73 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  sr;         /* C
155c3 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61  ursor pointing a
155c4 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20  t blob row */.  
155c5 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
155c6 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65  tmt;    /* State
155c7 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72  ment holding cur
155c8 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71  sor open */.  sq
155c9 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
155ca 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73        /* The ass
155cb 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65  ociated database
155cc 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70   */.};../*.** Op
155cd 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  en a blob handle
155ce 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
155cf 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
155d0 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  _open(.  sqlite3
155d1 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 20 20  * db,           
155d2 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
155d3 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
155d4 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
155d5 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
155d6 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
155d7 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
155d8 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74   blob */.  const
155d9 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20   char *zTable,  
155da 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
155db 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62  containing the b
155dc 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  lob */.  const c
155dd 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20  har *zColumn,   
155de 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63   /* The column c
155df 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c  ontaining the bl
155e0 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ob */.  sqlite_i
155e1 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20  nt64 iRow,      
155e2 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61  /* The row conta
155e3 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a  ining the glob *
155e4 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
155e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
155e6 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74  rue -> read/writ
155e7 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20  e access, false 
155e8 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a  -> read-only */.
155e9 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a    sqlite3_blob *
155ea 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e  *ppBlob   /* Han
155eb 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e  dle for accessin
155ec 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72  g the blob retur
155ed 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ned here */.){. 
155ee 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20   int nAttempt = 
155ef 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20  0;.  int iCol;  
155f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
155f1 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e  Index of zColumn
155f2 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a   in row-record *
155f3 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42  /..  /* This VDB
155f4 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20  E program seeks 
155f5 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 20 74  a btree cursor t
155f6 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 64  o the identified
155f7 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f   .  ** db/table/
155f8 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72  row entry. The r
155f9 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20  eason for using 
155fa 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69  a vdbe program i
155fb 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77  nstead.  ** of w
155fc 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75  riting code to u
155fd 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  se the b-tree la
155fe 79 65 72 20 64 69 72 65 63 74 6c 79 20 69 73 20  yer directly is 
155ff 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64  that the.  ** vd
15600 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20  be program will 
15601 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f  take advantage o
15602 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 72  f the various tr
15603 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20  ansaction,.  ** 
15604 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f  locking and erro
15605 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61  r handling infra
15606 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20  structure built 
15607 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20  into the vdbe.. 
15608 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73   **.  ** After s
15609 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f  eeking the curso
1560a 72 2c 20 74 68 65 20 76 64 62 65 20 65 78 65 63  r, the vdbe exec
1560b 75 74 65 73 20 61 6e 20 4f 50 5f 43 61 6c 6c 62  utes an OP_Callb
1560c 61 63 6b 2e 0a 20 20 2a 2a 20 43 6f 64 65 20 65  ack..  ** Code e
1560d 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20 56  xternal to the V
1560e 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f 77  dbe then "borrow
1560f 73 22 20 74 68 65 20 62 2d 74 72 65 65 20 63 75  s" the b-tree cu
15610 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75 73  rsor and.  ** us
15611 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d 65  es it to impleme
15612 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61 64  nt the blob_read
15613 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28 29  (), blob_write()
15614 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62 5f   and .  ** blob_
15615 62 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f 6e  bytes() function
15616 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  s..  **.  ** The
15617 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c   sqlite3_blob_cl
15618 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ose() function f
15619 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64 62  inalizes the vdb
1561a 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a 20  e program,.  ** 
1561b 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68 65  which closes the
1561c 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61   b-tree cursor a
1561d 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63 6f  nd (possibly) co
1561e 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a 20  mmits the .  ** 
1561f 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a  transaction..  *
15620 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
15621 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65 6e   VdbeOpList open
15622 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b  Blob[] = {.    {
15623 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20  OP_Transaction, 
15624 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f 2a  0, 0, 0},     /*
15625 20 30 3a 20 53 74 61 72 74 20 61 20 74 72 61 6e   0: Start a tran
15626 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 7b  saction */.    {
15627 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c  OP_VerifyCookie,
15628 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f 2a   0, 0, 0},    /*
15629 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73 63   1: Check the sc
1562a 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20  hema cookie */. 
1562b 20 20 20 7b 4f 50 5f 49 6e 74 65 67 65 72 2c 20     {OP_Integer, 
1562c 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20  0, 0, 0},       
1562d 20 20 2f 2a 20 32 3a 20 44 61 74 61 62 61 73 65    /* 2: Database
1562e 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 20 20   number */..    
1562f 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f  /* One of the fo
15630 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74  llowing two inst
15631 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c  ructions is repl
15632 61 63 65 64 20 62 79 20 61 6e 0a 20 20 20 20 2a  aced by an.    *
15633 2a 20 4f 50 5f 4e 6f 6f 70 20 62 65 66 6f 72 65  * OP_Noop before
15634 20 65 78 65 63 74 69 6f 6e 2e 0a 20 20 20 20 2a   exection..    *
15635 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 52 65  /.    {OP_OpenRe
15636 61 64 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  ad, 0, 0, 0},   
15637 20 20 20 20 20 2f 2a 20 33 3a 20 4f 70 65 6e 20       /* 3: Open 
15638 63 75 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61  cursor 0 for rea
15639 64 69 6e 67 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  ding */.    {OP_
1563a 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 30 2c  OpenWrite, 0, 0,
1563b 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 34 3a   0},       /* 4:
1563c 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66   Open cursor 0 f
1563d 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 2a 2f  or read/write */
1563e 0a 20 20 20 20 7b 4f 50 5f 53 65 74 4e 75 6d 43  .    {OP_SetNumC
1563f 6f 6c 75 6d 6e 73 2c 20 30 2c 20 30 2c 20 30 7d  olumns, 0, 0, 0}
15640 2c 20 20 20 2f 2a 20 35 3a 20 4e 75 6d 20 63 6f  ,   /* 5: Num co
15641 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 20 2a 2f  ls for cursor */
15642 0a 0a 20 20 20 20 7b 4f 50 5f 56 61 72 69 61 62  ..    {OP_Variab
15643 6c 65 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20  le, 1, 0, 0},   
15644 20 20 20 20 20 2f 2a 20 36 3a 20 50 75 73 68 20       /* 6: Push 
15645 74 68 65 20 72 6f 77 69 64 20 74 6f 20 74 68 65  the rowid to the
15646 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f   stack */.    {O
15647 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 30 2c 20  P_NotExists, 0, 
15648 31 30 2c 20 30 7d 2c 20 20 20 20 20 20 2f 2a 20  10, 0},      /* 
15649 37 3a 20 53 65 65 6b 20 74 68 65 20 63 75 72 73  7: Seek the curs
1564a 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f  or */.    {OP_Co
1564b 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  lumn, 0, 0, 0}, 
1564c 20 20 20 20 20 20 20 20 20 2f 2a 20 38 20 20 2a           /* 8  *
1564d 2f 0a 20 20 20 20 7b 4f 50 5f 43 61 6c 6c 62 61  /.    {OP_Callba
1564e 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  ck, 0, 0, 0},   
1564f 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a 20 20       /* 9  */.  
15650 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20    {OP_Close, 0, 
15651 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20  0, 0},          
15652 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 20 20 7b 4f   /* 10 */.    {O
15653 50 5f 48 61 6c 74 2c 20 30 2c 20 30 2c 20 30 7d  P_Halt, 0, 0, 0}
15654 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
15655 31 31 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64  11 */.  };..  Vd
15656 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74  be *v = 0;.  int
15657 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
15658 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38  .  char zErr[128
15659 5d 3b 0a 0a 20 20 7a 45 72 72 5b 30 5d 20 3d 20  ];..  zErr[0] = 
1565a 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  0;.  sqlite3_mut
1565b 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
1565c 65 78 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20  ex);.  do {.    
1565d 50 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20  Parse sParse;.  
1565e 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a    Table *pTab;..
1565f 20 20 20 20 6d 65 6d 73 65 74 28 26 73 50 61 72      memset(&sPar
15660 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 61  se, 0, sizeof(Pa
15661 72 73 65 29 29 3b 0a 20 20 20 20 73 50 61 72 73  rse));.    sPars
15662 65 2e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20  e.db = db;..    
15663 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  rc = sqlite3Safe
15664 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69 66  tyOn(db);.    if
15665 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
15666 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
15667 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
15668 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72  >mutex);.      r
15669 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1566a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1566b 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20  eEnterAll(db);. 
1566c 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
1566d 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 26 73 50  3LocateTable(&sP
1566e 61 72 73 65 2c 20 7a 54 61 62 6c 65 2c 20 7a 44  arse, zTable, zD
1566f 62 29 3b 0a 20 20 20 20 69 66 28 20 21 70 54 61  b);.    if( !pTa
15670 62 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  b ){.      if( s
15671 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 20 29 7b  Parse.zErrMsg ){
15672 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
15673 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
15674 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 25  (zErr), zErr, "%
15675 73 22 2c 20 73 50 61 72 73 65 2e 7a 45 72 72 4d  s", sParse.zErrM
15676 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  sg);.      }.   
15677 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
15678 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 29 3b  sParse.zErrMsg);
15679 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1567a 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
1567b 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1567c 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  (db);.      sqli
1567d 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
1567e 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  (db);.      goto
1567f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a   blob_open_out;.
15680 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f      }..    /* No
15681 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66 6f  w search pTab fo
15682 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75  r the exact colu
15683 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  mn. */.    for(i
15684 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54  Col=0; iCol < pT
15685 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b  ab->nCol; iCol++
15686 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  ) {.      if( sq
15687 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61  lite3StrICmp(pTa
15688 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
15689 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30  ame, zColumn)==0
1568a 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
1568b 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1568c 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70  .    if( iCol==p
1568d 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
1568e 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1568f 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
15690 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68  , zErr, "no such
15691 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22   column: \"%s\""
15692 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , zColumn);.    
15693 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
15694 52 4f 52 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ROR;.      sqlit
15695 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
15696 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
15697 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
15698 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62  .      goto blob
15699 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d  _open_out;.    }
1569a 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
1569b 76 61 6c 75 65 20 69 73 20 62 65 69 6e 67 20 6f  value is being o
1569c 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e  pened for writin
1569d 67 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68  g, check that th
1569e 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20  e.    ** column 
1569f 69 73 20 6e 6f 74 20 69 6e 64 65 78 65 64 2e 20  is not indexed. 
156a0 49 74 20 69 73 20 61 67 61 69 6e 73 74 20 74 68  It is against th
156a1 65 20 72 75 6c 65 73 20 74 6f 20 6f 70 65 6e 20  e rules to open 
156a2 61 6e 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 65  an.    ** indexe
156a3 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69  d column for wri
156a4 74 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ting..    */.   
156a5 20 69 66 28 20 66 6c 61 67 73 20 29 7b 0a 20 20   if( flags ){.  
156a6 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
156a7 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d  .      for(pIdx=
156a8 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
156a9 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
156aa 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69  Next){.        i
156ab 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 6f  nt j;.        fo
156ac 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e  r(j=0; j<pIdx->n
156ad 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20  Column; j++){.  
156ae 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
156af 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69  ->aiColumn[j]==i
156b0 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Col ){.         
156b1 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
156b2 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
156b3 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20  , zErr,.        
156b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156b5 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 6f 70 65       "cannot ope
156b6 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  n indexed column
156b7 20 66 6f 72 20 77 72 69 74 69 6e 67 22 29 3b 0a   for writing");.
156b8 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
156b9 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
156ba 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
156bb 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
156bc 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
156bd 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
156be 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20  l(db);.         
156bf 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65     goto blob_ope
156c0 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 20  n_out;.         
156c1 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
156c2 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
156c3 76 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  v = sqlite3VdbeC
156c4 72 65 61 74 65 28 64 62 29 3b 0a 20 20 20 20 69  reate(db);.    i
156c5 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 69 6e  f( v ){.      in
156c6 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
156c7 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
156c8 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
156c9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
156ca 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 73  beAddOpList(v, s
156cb 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 2f  izeof(openBlob)/
156cc 73 69 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69 73  sizeof(VdbeOpLis
156cd 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a 0a  t), openBlob);..
156ce 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
156cf 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73 61  re the OP_Transa
156d0 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73  ction */.      s
156d1 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
156d2 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a 20  P1(v, 0, iDb);. 
156d3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
156d4 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20 28  ChangeP2(v, 0, (
156d5 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 29 29 3b  flags ? 1 : 0));
156d6 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69  ..      /* Confi
156d7 67 75 72 65 20 74 68 65 20 4f 50 5f 56 65 72 69  gure the OP_Veri
156d8 66 79 43 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20  fyCookie */.    
156d9 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
156da 6e 67 65 50 31 28 76 2c 20 31 2c 20 69 44 62 29  ngeP1(v, 1, iDb)
156db 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
156dc 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 31  dbeChangeP2(v, 1
156dd 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d  , pTab->pSchema-
156de 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b  >schema_cookie);
156df 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20  ..      /* Make 
156e0 73 75 72 65 20 61 20 6d 75 74 65 78 20 69 73 20  sure a mutex is 
156e1 68 65 6c 64 20 6f 6e 20 74 68 65 20 74 61 62 6c  held on the tabl
156e2 65 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64  e to be accessed
156e3 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
156e4 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
156e5 2c 20 69 44 62 29 3b 20 0a 0a 20 20 20 20 20 20  , iDb); ..      
156e6 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  /* Configure the
156e7 20 64 62 20 6e 75 6d 62 65 72 20 70 75 73 68 65   db number pushe
156e8 64 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  d onto the stack
156e9 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
156ea 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
156eb 20 32 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20   2, iDb);..     
156ec 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68 65   /* Remove eithe
156ed 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72 69  r the OP_OpenWri
156ee 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e 20  te or OpenRead. 
156ef 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20 20  Set the P2 .    
156f0 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 6f    ** parameter o
156f1 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 70  f the other to p
156f2 54 61 62 2d 3e 74 6e 75 6d 2e 20 0a 20 20 20 20  Tab->tnum. .    
156f3 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
156f4 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f  e3VdbeChangeToNo
156f5 6f 70 28 76 2c 20 28 66 6c 61 67 73 20 3f 20 33  op(v, (flags ? 3
156f6 20 3a 20 34 29 2c 20 31 29 3b 0a 20 20 20 20 20   : 4), 1);.     
156f7 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
156f8 67 65 50 32 28 76 2c 20 28 66 6c 61 67 73 20 3f  geP2(v, (flags ?
156f9 20 34 20 3a 20 33 29 2c 20 70 54 61 62 2d 3e 74   4 : 3), pTab->t
156fa 6e 75 6d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  num);..      /* 
156fb 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50  Configure the OP
156fc 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2e 20  _SetNumColumns. 
156fd 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 75  Configure the cu
156fe 72 73 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a  rsor to.      **
156ff 20 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20   think that the 
15700 74 61 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f  table has one mo
15701 72 65 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69  re column than i
15702 74 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a  t really.      *
15703 2a 20 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f  * does. An OP_Co
15704 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65  lumn to retrieve
15705 20 74 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20   this imaginary 
15706 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20  column will.    
15707 20 20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75    ** always retu
15708 72 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20  rn an SQL NULL. 
15709 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 20 62  This is useful b
1570a 65 63 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a  ecause it means.
1570b 20 20 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20        ** we can 
1570c 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e  invoke OP_Column
1570d 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20   to fill in the 
1570e 76 64 62 65 20 63 75 72 73 6f 72 73 20 74 79 70  vdbe cursors typ
1570f 65 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  e .      ** and 
15710 6f 66 66 73 65 74 20 63 61 63 68 65 20 77 69 74  offset cache wit
15711 68 6f 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79  hout causing any
15712 20 49 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   IO..      */.  
15713 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
15714 68 61 6e 67 65 50 32 28 76 2c 20 35 2c 20 70 54  hangeP2(v, 5, pT
15715 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20 20  ab->nCol+1);.   
15716 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c     if( !db->mall
15717 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
15718 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
15719 61 6b 65 52 65 61 64 79 28 76 2c 20 31 2c 20 30  akeReady(v, 1, 0
1571a 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  , 1, 0);.      }
1571b 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 20 20 73  .    }.   .    s
1571c 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1571d 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 72 63 20  All(db);.    rc 
1571e 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  = sqlite3SafetyO
1571f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20  ff(db);.    if( 
15720 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
15721 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
15722 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  d ){.      goto 
15723 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  blob_open_out;. 
15724 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
15725 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73 71  3_bind_int64((sq
15726 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20  lite3_stmt *)v, 
15727 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63  1, iRow);.    rc
15728 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
15729 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
1572a 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  v);.    if( rc!=
1572b 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
1572c 20 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a      nAttempt++;.
1572d 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1572e 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c  e3_finalize((sql
1572f 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a  ite3_stmt *)v);.
15730 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
15731 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
15732 72 72 29 2c 20 7a 45 72 72 2c 20 73 71 6c 69 74  rr), zErr, sqlit
15733 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a  e3_errmsg(db));.
15734 20 20 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20        v = 0;.   
15735 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 41   }.  } while( nA
15736 74 74 65 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d  ttempt<5 && rc==
15737 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 3b  SQLITE_SCHEMA );
15738 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
15739 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a  TE_ROW ){.    /*
1573a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 20   The row-record 
1573b 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20  has been opened 
1573c 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 68  successfully. Ch
1573d 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20  eck that the.   
1573e 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75   ** column in qu
1573f 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  estion contains 
15740 74 65 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20  text or a blob. 
15741 49 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20  If it contains. 
15742 20 20 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 69     ** text, it i
15743 73 20 75 70 20 74 6f 20 74 68 65 20 63 61 6c 6c  s up to the call
15744 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e  er to get the en
15745 63 6f 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 20  coding right..  
15746 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f    */.    Incrblo
15747 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33  b *pBlob;.    u3
15748 32 20 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 73  2 type = v->apCs
15749 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c  r[0]->aType[iCol
1574a 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74 79 70 65  ];..    if( type
1574b 3c 31 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  <12 ){.      sql
1574c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1574d 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72  zeof(zErr), zErr
1574e 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76  , "cannot open v
1574f 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25 73 22  alue of type %s"
15750 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79 70 65  ,.          type
15751 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79 70 65  ==0?"null": type
15752 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69 6e 74  ==7?"real": "int
15753 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b 0a 20  eger".      );. 
15754 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
15755 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 67 6f  _ERROR;.      go
15756 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
15757 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c 6f  ;.    }.    pBlo
15758 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29  b = (Incrblob *)
15759 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
1575a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 49  ero(db, sizeof(I
1575b 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20 20 69  ncrblob));.    i
1575c 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
1575d 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  led ){.      sql
1575e 69 74 65 33 5f 66 72 65 65 28 70 42 6c 6f 62 29  ite3_free(pBlob)
1575f 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f  ;.      goto blo
15760 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  b_open_out;.    
15761 7d 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 66 6c 61  }.    pBlob->fla
15762 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20  gs = flags;.    
15763 70 42 6c 6f 62 2d 3e 70 43 73 72 20 3d 20 20 76  pBlob->pCsr =  v
15764 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 70 43 75 72  ->apCsr[0]->pCur
15765 73 6f 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sor;.    sqlite3
15766 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72  BtreeEnterCursor
15767 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20  (pBlob->pCsr);. 
15768 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
15769 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 70 42 6c  acheOverflow(pBl
1576a 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73  ob->pCsr);.    s
1576b 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1576c 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43  Cursor(pBlob->pC
1576d 73 72 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e  sr);.    pBlob->
1576e 70 53 74 6d 74 20 3d 20 28 73 71 6c 69 74 65 33  pStmt = (sqlite3
1576f 5f 73 74 6d 74 20 2a 29 76 3b 0a 20 20 20 20 70  _stmt *)v;.    p
15770 42 6c 6f 62 2d 3e 69 4f 66 66 73 65 74 20 3d 20  Blob->iOffset = 
15771 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 4f 66  v->apCsr[0]->aOf
15772 66 73 65 74 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20  fset[iCol];.    
15773 70 42 6c 6f 62 2d 3e 6e 42 79 74 65 20 3d 20 73  pBlob->nByte = s
15774 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
15775 54 79 70 65 4c 65 6e 28 74 79 70 65 29 3b 0a 20  TypeLen(type);. 
15776 20 20 20 70 42 6c 6f 62 2d 3e 64 62 20 3d 20 64     pBlob->db = d
15777 62 3b 0a 20 20 20 20 2a 70 70 42 6c 6f 62 20 3d  b;.    *ppBlob =
15778 20 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a   (sqlite3_blob *
15779 29 70 42 6c 6f 62 3b 0a 20 20 20 20 72 63 20 3d  )pBlob;.    rc =
1577a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
1577b 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
1577c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
1577d 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1577e 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72  zeof(zErr), zErr
1577f 2c 20 22 6e 6f 20 73 75 63 68 20 72 6f 77 69 64  , "no such rowid
15780 3a 20 25 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a  : %lld", iRow);.
15781 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
15782 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62  ERROR;.  }..blob
15783 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 7a 45 72  _open_out:.  zEr
15784 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31  r[sizeof(zErr)-1
15785 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20  ] = '\0';.  if( 
15786 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
15787 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
15788 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
15789 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74  _finalize((sqlit
1578a 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20  e3_stmt *)v);.  
1578b 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  }.  sqlite3Error
1578c 28 64 62 2c 20 72 63 2c 20 28 72 63 3d 3d 53 51  (db, rc, (rc==SQ
1578d 4c 49 54 45 5f 4f 4b 3f 30 3a 7a 45 72 72 29 29  LITE_OK?0:zErr))
1578e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1578f 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
15790 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
15791 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
15792 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
15793 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
15794 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 68 61   blob handle tha
15795 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
15796 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a   created using.*
15797 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f  * sqlite3_blob_o
15798 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pen()..*/.SQLITE
15799 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1579a 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69  _blob_close(sqli
1579b 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29  te3_blob *pBlob)
1579c 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20  {.  Incrblob *p 
1579d 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42  = (Incrblob *)pB
1579e 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  lob;.  int rc;..
1579f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66    rc = sqlite3_f
157a0 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74  inalize(p->pStmt
157a1 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
157a2 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
157a3 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66  c;.}../*.** Perf
157a4 6f 72 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72  orm a read or wr
157a5 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e  ite operation on
157a6 20 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69   a blob.*/.stati
157a7 63 20 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72  c int blobReadWr
157a8 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62  ite(.  sqlite3_b
157a9 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76  lob *pBlob, .  v
157aa 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  oid *z, .  int n
157ab 2c 20 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  , .  int iOffset
157ac 2c 20 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c  , .  int (*xCall
157ad 29 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32  )(BtCursor*, u32
157ae 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b  , u32, void*).){
157af 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63  .  int rc;.  Inc
157b0 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72  rblob *p = (Incr
157b1 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20  blob *)pBlob;.  
157b2 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74  Vdbe *v;.  sqlit
157b3 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20  e3 *db = p->db; 
157b4 20 0a 0a 20 20 2f 2a 20 52 65 71 75 65 73 74 20   ..  /* Request 
157b5 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e  is out of range.
157b6 20 52 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69   Return a transi
157b7 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20  ent error. */.  
157b8 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e 29 3e  if( (iOffset+n)>
157b9 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 20 20 20  p->nByte ){.    
157ba 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
157bb 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ROR;.  }.  sqlit
157bc 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
157bd 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a  b->mutex);..  /*
157be 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
157bf 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
157c0 2c 20 74 68 65 6e 20 74 68 65 20 62 6c 6f 62 2d  , then the blob-
157c1 68 61 6e 64 6c 65 20 68 61 73 0a 20 20 2a 2a 20  handle has.  ** 
157c2 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 76  already been inv
157c3 61 6c 69 64 61 74 65 64 2e 20 52 65 74 75 72 6e  alidated. Return
157c4 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 6e   SQLITE_ABORT in
157c5 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f   this case..  */
157c6 0a 20 20 76 20 3d 20 28 56 64 62 65 2a 29 70 2d  .  v = (Vdbe*)p-
157c7 3e 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 76 3d  >pStmt;.  if( v=
157c8 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
157c9 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d  QLITE_ABORT;.  }
157ca 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 61 6c  else{.    /* Cal
157cb 6c 20 65 69 74 68 65 72 20 42 74 72 65 65 44 61  l either BtreeDa
157cc 74 61 28 29 20 6f 72 20 42 74 72 65 65 50 75 74  ta() or BtreePut
157cd 44 61 74 61 28 29 2e 20 49 66 20 53 51 4c 49 54  Data(). If SQLIT
157ce 45 5f 41 42 4f 52 54 20 69 73 0a 20 20 20 20 2a  E_ABORT is.    *
157cf 2a 20 72 65 74 75 72 6e 65 64 2c 20 63 6c 65 61  * returned, clea
157d0 6e 2d 75 70 20 74 68 65 20 73 74 61 74 65 6d 65  n-up the stateme
157d1 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 2a  nt handle..    *
157d2 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  /.    assert( db
157d3 20 3d 3d 20 76 2d 3e 64 62 20 29 3b 0a 20 20 20   == v->db );.   
157d4 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
157d5 65 72 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72  erCursor(p->pCsr
157d6 29 3b 0a 20 20 20 20 72 63 20 3d 20 78 43 61 6c  );.    rc = xCal
157d7 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f 66 66 73  l(p->pCsr, iOffs
157d8 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74 2c 20 6e  et+p->iOffset, n
157d9 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , z);.    sqlite
157da 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f  3BtreeLeaveCurso
157db 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  r(p->pCsr);.    
157dc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 41  if( rc==SQLITE_A
157dd 42 4f 52 54 20 29 7b 0a 20 20 20 20 20 20 73 71  BORT ){.      sq
157de 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
157df 65 28 76 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  e(v);.      p->p
157e0 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65  Stmt = 0;.    }e
157e1 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 65  lse{.      db->e
157e2 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20  rrCode = rc;.   
157e3 20 20 20 76 2d 3e 72 63 20 3d 20 72 63 3b 0a 20     v->rc = rc;. 
157e4 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20     }.  }.  rc = 
157e5 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
157e6 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
157e7 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
157e8 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
157e9 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
157ea 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  Read data from a
157eb 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f   blob handle..*/
157ec 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
157ed 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61  sqlite3_blob_rea
157ee 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  d(sqlite3_blob *
157ef 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a 7a 2c 20  pBlob, void *z, 
157f0 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73  int n, int iOffs
157f1 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c  et){.  return bl
157f2 6f 62 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f  obReadWrite(pBlo
157f3 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74  b, z, n, iOffset
157f4 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  , sqlite3BtreeDa
157f5 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  ta);.}../*.** Wr
157f6 69 74 65 20 64 61 74 61 20 74 6f 20 61 20 62 6c  ite data to a bl
157f7 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  ob handle..*/.SQ
157f8 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
157f9 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
157fa 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42  sqlite3_blob *pB
157fb 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  lob, const void 
157fc 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69  *z, int n, int i
157fd 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72  Offset){.  retur
157fe 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28  n blobReadWrite(
157ff 70 42 6c 6f 62 2c 20 28 76 6f 69 64 20 2a 29 7a  pBlob, (void *)z
15800 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71  , n, iOffset, sq
15801 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74  lite3BtreePutDat
15802 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65  a);.}../*.** Que
15803 72 79 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  ry a blob handle
15804 20 66 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66   for the size of
15805 20 74 68 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a   the data..**.**
15806 20 54 68 65 20 49 6e 63 72 62 6c 6f 62 2e 6e 42   The Incrblob.nB
15807 79 74 65 20 66 69 65 6c 64 20 69 73 20 66 69 78  yte field is fix
15808 65 64 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74  ed for the lifet
15809 69 6d 65 20 6f 66 20 74 68 65 20 49 6e 63 72 62  ime of the Incrb
1580a 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f 20 6d 75 74  lob.** so no mut
1580b 65 78 20 69 73 20 72 65 71 75 69 72 65 64 20 66  ex is required f
1580c 6f 72 20 61 63 63 65 73 73 2e 0a 2a 2f 0a 53 51  or access..*/.SQ
1580d 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1580e 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28  ite3_blob_bytes(
1580f 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42  sqlite3_blob *pB
15810 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62  lob){.  Incrblob
15811 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20   *p = (Incrblob 
15812 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 65 74 75 72  *)pBlob;.  retur
15813 6e 20 70 2d 3e 6e 42 79 74 65 3b 0a 7d 0a 0a 23  n p->nByte;.}..#
15814 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66  endif /* #ifndef
15815 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
15816 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  RBLOB */../*****
15817 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
15818 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a   vdbeblob.c ****
15819 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1581a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1581b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1581c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1581d 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a  file journal.c *
1581e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1581f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15820 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
15821 32 30 30 37 20 41 75 67 75 73 74 20 32 32 0a 2a  2007 August 22.*
15822 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
15823 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
15824 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
15825 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
15826 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
15827 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
15828 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
15829 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1582a 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1582b 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1582c 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1582d 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1582e 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1582f 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
15830 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
15831 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
15832 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
15833 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
15834 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15835 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15836 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15837 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
15838 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6a 6f  .** @(#) $Id: jo
15839 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 37 20 32 30  urnal.c,v 1.7 20
1583a 30 37 2f 30 39 2f 30 36 20 31 33 3a 34 39 3a 33  07/09/06 13:49:3
1583b 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  7 drh Exp $.*/..
1583c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
1583d 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
1583e 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 69  E../*.** This fi
1583f 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  le implements a 
15840 73 70 65 63 69 61 6c 20 6b 69 6e 64 20 6f 66 20  special kind of 
15841 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a  sqlite3_file obj
15842 65 63 74 20 75 73 65 64 0a 2a 2a 20 62 79 20 53  ect used.** by S
15843 51 4c 69 74 65 20 74 6f 20 63 72 65 61 74 65 20  QLite to create 
15844 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 69 66  journal files if
15845 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74   the atomic-writ
15846 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a  e optimization.*
15847 2a 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a  * is enabled..**
15848 0a 2a 2a 20 54 68 65 20 64 69 73 74 69 6e 63 74  .** The distinct
15849 69 76 65 20 63 68 61 72 61 63 74 65 72 69 73 74  ive characterist
1584a 69 63 20 6f 66 20 74 68 69 73 20 73 71 6c 69 74  ic of this sqlit
1584b 65 33 5f 66 69 6c 65 20 69 73 20 74 68 61 74 20  e3_file is that 
1584c 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c 20 6f 6e  the.** actual on
1584d 20 64 69 73 6b 20 66 69 6c 65 20 69 73 20 63 72   disk file is cr
1584e 65 61 74 65 64 20 6c 61 7a 69 6c 79 2e 20 57 68  eated lazily. Wh
1584f 65 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 63  en the file is c
15850 72 65 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 63  reated,.** the c
15851 61 6c 6c 65 72 20 73 70 65 63 69 66 69 65 73 20  aller specifies 
15852 61 20 62 75 66 66 65 72 20 73 69 7a 65 20 66 6f  a buffer size fo
15853 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 62  r an in-memory b
15854 75 66 66 65 72 20 74 6f 0a 2a 2a 20 62 65 20 75  uffer to.** be u
15855 73 65 64 20 74 6f 20 73 65 72 76 69 63 65 20 72  sed to service r
15856 65 61 64 28 29 20 61 6e 64 20 77 72 69 74 65 28  ead() and write(
15857 29 20 72 65 71 75 65 73 74 73 2e 20 54 68 65 20  ) requests. The 
15858 61 63 74 75 61 6c 20 66 69 6c 65 0a 2a 2a 20 6f  actual file.** o
15859 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 63 72  n disk is not cr
1585a 65 61 74 65 64 20 6f 72 20 70 6f 70 75 6c 61 74  eated or populat
1585b 65 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 3a  ed until either:
1585c 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 54 68 65 20  .**.**   1) The 
1585d 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73  in-memory repres
1585e 65 6e 74 61 74 69 6f 6e 20 67 72 6f 77 73 20 74  entation grows t
1585f 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65  oo large for the
15860 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 20   allocated .**  
15861 20 20 20 20 62 75 66 66 65 72 2c 20 6f 72 0a 2a      buffer, or.*
15862 2a 20 20 20 32 29 20 54 68 65 20 78 53 79 6e 63  *   2) The xSync
15863 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c  () method is cal
15864 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a  led..*/..../*.**
15865 20 41 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 6f   A JournalFile o
15866 62 6a 65 63 74 20 69 73 20 61 20 73 75 62 63 6c  bject is a subcl
15867 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66  ass of sqlite3_f
15868 69 6c 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 61  ile used by.** a
15869 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 68  s an open file h
1586a 61 6e 64 6c 65 20 66 6f 72 20 6a 6f 75 72 6e 61  andle for journa
1586b 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75  l files..*/.stru
1586c 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 7b  ct JournalFile {
1586d 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  .  sqlite3_io_me
1586e 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20  thods *pMethod; 
1586f 20 20 20 2f 2a 20 49 2f 4f 20 6d 65 74 68 6f 64     /* I/O method
15870 73 20 6f 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  s on journal fil
15871 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66  es */.  int nBuf
15872 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15873 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
15874 6f 66 20 7a 42 75 66 5b 5d 20 69 6e 20 62 79 74  of zBuf[] in byt
15875 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42  es */.  char *zB
15876 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  uf;             
15877 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
15878 20 74 6f 20 62 75 66 66 65 72 20 6a 6f 75 72 6e   to buffer journ
15879 61 6c 20 77 72 69 74 65 73 20 2a 2f 0a 20 20 69  al writes */.  i
1587a 6e 74 20 69 53 69 7a 65 3b 20 20 20 20 20 20 20  nt iSize;       
1587b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1587c 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 7a 42 75 66  * Amount of zBuf
1587d 5b 5d 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65  [] currently use
1587e 64 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  d */.  int flags
1587f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15880 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
15881 66 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74  flags */.  sqlit
15882 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20  e3_vfs *pVfs;   
15883 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
15884 65 20 22 72 65 61 6c 22 20 75 6e 64 65 72 6c 79  e "real" underly
15885 69 6e 67 20 56 46 53 20 2a 2f 0a 20 20 73 71 6c  ing VFS */.  sql
15886 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 61 6c  ite3_file *pReal
15887 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
15888 54 68 65 20 22 72 65 61 6c 22 20 75 6e 64 65 72  The "real" under
15889 6c 79 69 6e 67 20 66 69 6c 65 20 64 65 73 63 72  lying file descr
1588a 69 70 74 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  iptor */.  const
1588b 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b   char *zJournal;
1588c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
1588d 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  me of the journa
1588e 6c 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 74 79 70  l file */.};.typ
1588f 65 64 65 66 20 73 74 72 75 63 74 20 4a 6f 75 72  edef struct Jour
15890 6e 61 6c 46 69 6c 65 20 4a 6f 75 72 6e 61 6c 46  nalFile JournalF
15891 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 69  ile;../*.** If i
15892 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61  t does not alrea
15893 64 79 20 65 78 69 73 74 73 2c 20 63 72 65 61 74  dy exists, creat
15894 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 74  e and populate t
15895 68 65 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 20  he on-disk file 
15896 0a 2a 2a 20 66 6f 72 20 4a 6f 75 72 6e 61 6c 46  .** for JournalF
15897 69 6c 65 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  ile p..*/.static
15898 20 69 6e 74 20 63 72 65 61 74 65 46 69 6c 65 28   int createFile(
15899 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 29 7b  JournalFile *p){
1589a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1589b 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70 2d  TE_OK;.  if( !p-
1589c 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71  >pReal ){.    sq
1589d 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 61  lite3_file *pRea
1589e 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c  l = (sqlite3_fil
1589f 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 72  e *)&p[1];.    r
158a0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
158a1 6e 28 70 2d 3e 70 56 66 73 2c 20 70 2d 3e 7a 4a  n(p->pVfs, p->zJ
158a2 6f 75 72 6e 61 6c 2c 20 70 52 65 61 6c 2c 20 70  ournal, pReal, p
158a3 2d 3e 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20  ->flags, 0);.   
158a4 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
158a5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  OK ){.      p->p
158a6 52 65 61 6c 20 3d 20 70 52 65 61 6c 3b 0a 20 20  Real = pReal;.  
158a7 20 20 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65      if( p->iSize
158a8 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  >0 ){.        as
158a9 73 65 72 74 28 70 2d 3e 69 53 69 7a 65 3c 3d 70  sert(p->iSize<=p
158aa 2d 3e 6e 42 75 66 29 3b 0a 20 20 20 20 20 20 20  ->nBuf);.       
158ab 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
158ac 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 70  rite(p->pReal, p
158ad 2d 3e 7a 42 75 66 2c 20 70 2d 3e 69 53 69 7a 65  ->zBuf, p->iSize
158ae 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
158af 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
158b0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c   rc;.}../*.** Cl
158b1 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  ose the file..*/
158b2 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
158b3 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69  Close(sqlite3_fi
158b4 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4a 6f 75  le *pJfd){.  Jou
158b5 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
158b6 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
158b7 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61  d;.  if( p->pRea
158b8 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
158b9 4f 73 43 6c 6f 73 65 28 70 2d 3e 70 52 65 61 6c  OsClose(p->pReal
158ba 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
158bb 5f 66 72 65 65 28 70 2d 3e 7a 42 75 66 29 3b 0a  _free(p->zBuf);.
158bc 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
158bd 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  OK;.}../*.** Rea
158be 64 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20  d data from the 
158bf 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
158c0 69 6e 74 20 6a 72 6e 6c 52 65 61 64 28 0a 20 20  int jrnlRead(.  
158c1 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
158c2 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f  fd,    /* The jo
158c3 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20  urnal file from 
158c4 77 68 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f  which to read */
158c5 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20  .  void *zBuf,  
158c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74            /* Put
158c7 20 74 68 65 20 72 65 73 75 6c 74 73 20 68 65 72   the results her
158c8 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c  e */.  int iAmt,
158c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
158ca 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
158cb 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71   to read */.  sq
158cc 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74  lite_int64 iOfst
158cd 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65       /* Begin re
158ce 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66  ading at this of
158cf 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  fset */.){.  int
158d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
158d1 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
158d2 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
158d3 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70   *)pJfd;.  if( p
158d4 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  ->pReal ){.    r
158d5 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
158d6 64 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66  d(p->pReal, zBuf
158d7 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a  , iAmt, iOfst);.
158d8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
158d9 65 72 74 28 20 69 41 6d 74 2b 69 4f 66 73 74 3c  ert( iAmt+iOfst<
158da 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a 20 20 20  =p->iSize );.   
158db 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 70   memcpy(zBuf, &p
158dc 2d 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69  ->zBuf[iOfst], i
158dd 41 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Amt);.  }.  retu
158de 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
158df 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68  Write data to th
158e0 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
158e1 63 20 69 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28  c int jrnlWrite(
158e2 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
158e3 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65  *pJfd,    /* The
158e4 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e   journal file in
158e5 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 74  to which to writ
158e6 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e */.  const voi
158e7 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a  d *zBuf,      /*
158e8 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65   Take data to be
158e9 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65   written from he
158ea 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74  re */.  int iAmt
158eb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
158ec 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
158ed 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20  s to write */.  
158ee 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66  sqlite_int64 iOf
158ef 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20  st     /* Begin 
158f0 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 73 20  writing at this 
158f1 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20  offset into the 
158f2 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  file */.){.  int
158f3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
158f4 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
158f5 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
158f6 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 21   *)pJfd;.  if( !
158f7 70 2d 3e 70 52 65 61 6c 20 26 26 20 28 69 4f 66  p->pReal && (iOf
158f8 73 74 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66  st+iAmt)>p->nBuf
158f9 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65   ){.    rc = cre
158fa 61 74 65 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a  ateFile(p);.  }.
158fb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
158fc 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70  _OK ){.    if( p
158fd 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 20  ->pReal ){.     
158fe 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
158ff 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a  rite(p->pReal, z
15900 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74  Buf, iAmt, iOfst
15901 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
15902 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a      memcpy(&p->z
15903 42 75 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66  Buf[iOfst], zBuf
15904 2c 20 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 69  , iAmt);.      i
15905 66 28 20 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66  f( p->iSize<(iOf
15906 73 74 2b 69 41 6d 74 29 20 29 7b 0a 20 20 20 20  st+iAmt) ){.    
15907 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28      p->iSize = (
15908 69 4f 66 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20  iOfst+iAmt);.   
15909 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1590a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1590b 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68  *.** Truncate th
1590c 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
1590d 63 20 69 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61  c int jrnlTrunca
1590e 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
1590f 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e  *pJfd, sqlite_in
15910 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74  t64 size){.  int
15911 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
15912 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
15913 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
15914 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70   *)pJfd;.  if( p
15915 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  ->pReal ){.    r
15916 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
15917 6e 63 61 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20  ncate(p->pReal, 
15918 73 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69  size);.  }else i
15919 66 28 20 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65  f( size<p->iSize
1591a 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65   ){.    p->iSize
1591b 20 3d 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72   = size;.  }.  r
1591c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1591d 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65  ** Sync the file
1591e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1591f 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33  jrnlSync(sqlite3
15920 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74  _file *pJfd, int
15921 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72   flags){.  int r
15922 63 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  c;.  JournalFile
15923 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69   *p = (JournalFi
15924 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 72 63 20  le *)pJfd;.  rc 
15925 3d 20 63 72 65 61 74 65 46 69 6c 65 28 70 29 3b  = createFile(p);
15926 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
15927 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
15928 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
15929 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b  ->pReal, flags);
1592a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1592b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  ;.}../*.** Query
1592c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1592d 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a   file in bytes..
1592e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1592f 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74  nlFileSize(sqlit
15930 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73  e3_file *pJfd, s
15931 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69  qlite_int64 *pSi
15932 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ze){.  int rc = 
15933 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75  SQLITE_OK;.  Jou
15934 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
15935 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
15936 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61  d;.  if( p->pRea
15937 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
15938 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
15939 70 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29  p->pReal, pSize)
1593a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
1593b 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f  pSize = (sqlite_
1593c 69 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b  int64) p->iSize;
1593d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1593e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65  ;.}../*.** Table
1593f 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20   of methods for 
15940 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69  JournalFile sqli
15941 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e  te3_file object.
15942 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
15943 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
15944 68 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  hods JournalFile
15945 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c  Methods = {.  1,
15946 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15947 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72  iVersion */.  jr
15948 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20  nlClose,     /* 
15949 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c  xClose */.  jrnl
1594a 52 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52  Read,      /* xR
1594b 65 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69  ead */.  jrnlWri
1594c 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74  te,     /* xWrit
1594d 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63  e */.  jrnlTrunc
1594e 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61  ate,  /* xTrunca
1594f 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63  te */.  jrnlSync
15950 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20  ,      /* xSync 
15951 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a  */.  jrnlFileSiz
15952 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65  e,  /* xFileSize
15953 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
15954 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f       /* xLock */
15955 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
15956 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a    /* xUnlock */.
15957 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
15958 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
15959 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20  edLock */.  0,  
1595a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
1595b 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20  ileControl */.  
1595c 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1595d 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f  * xSectorSize */
1595e 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20  .  0            
1595f 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72    /* xDeviceChar
15960 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d  acteristics */.}
15961 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61  ;../* .** Open a
15962 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
15963 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15964 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72   int sqlite3Jour
15965 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  nalOpen(.  sqlit
15966 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
15967 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53        /* The VFS
15968 20 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75   to use for actu
15969 61 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20  al file I/O */. 
1596a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1596b 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  me,         /* N
1596c 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ame of the journ
1596d 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c  al file */.  sql
1596e 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c  ite3_file *pJfd,
1596f 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c          /* Preal
15970 6c 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66  located, blank f
15971 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ile handle */.  
15972 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
15973 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
15974 65 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20  ening flags */. 
15975 20 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20   int nBuf       
15976 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
15977 79 74 65 73 20 62 75 66 66 65 72 65 64 20 62 65  ytes buffered be
15978 66 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65  fore opening the
15979 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f   file */.){.  Jo
1597a 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1597b 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1597c 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20  fd;.  memset(p, 
1597d 30 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  0, sqlite3Journa
1597e 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20  lSize(pVfs));.  
1597f 69 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20  if( nBuf>0 ){.  
15980 20 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69    p->zBuf = sqli
15981 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42  te3MallocZero(nB
15982 75 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d  uf);.    if( !p-
15983 3e 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72  >zBuf ){.      r
15984 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
15985 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  EM;.    }.  }els
15986 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  e{.    return sq
15987 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73  lite3OsOpen(pVfs
15988 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66  , zName, pJfd, f
15989 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  lags, 0);.  }.  
1598a 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f  p->pMethod = &Jo
1598b 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73  urnalFileMethods
1598c 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42  ;.  p->nBuf = nB
1598d 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d  uf;.  p->flags =
1598e 20 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f   flags;.  p->zJo
1598f 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20  urnal = zName;. 
15990 20 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b   p->pVfs = pVfs;
15991 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15992 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  _OK;.}../*.** If
15993 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20   the argument p 
15994 70 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72  points to a Jour
15995 6e 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72  nalFile structur
15996 65 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72  e, and the under
15997 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61  lying.** file ha
15998 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63  s not yet been c
15999 72 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 69  reated, create i
1599a 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45  t now..*/.SQLITE
1599b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1599c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74  ite3JournalCreat
1599d 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
1599e 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65  p){.  if( p->pMe
1599f 74 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46  thods!=&JournalF
159a0 69 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ileMethods ){.  
159a1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
159a2 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OK;.  }.  return
159a3 20 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75   createFile((Jou
159a4 72 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d  rnalFile *)p);.}
159a5 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20  ../* .** Return 
159a6 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
159a7 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20  tes required to 
159a8 73 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46  store a JournalF
159a9 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 76 66  ile that uses vf
159aa 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65  s.** pVfs to cre
159ab 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69  ate the underlyi
159ac 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73  ng on-disk files
159ad 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
159ae 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  ATE int sqlite3J
159af 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74  ournalSize(sqlit
159b0 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20  e3_vfs *pVfs){. 
159b1 20 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73   return (pVfs->s
159b2 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a  zOsFile+sizeof(J
159b3 6f 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a  ournalFile));.}.
159b4 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
159b5 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a  ******* End of j
159b6 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ournal.c *******
159b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
159b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
159b9 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
159ba 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
159bb 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a  le expr.c ******
159bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
159bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
159be 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
159bf 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
159c0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
159c1 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
159c2 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
159c3 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
159c4 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
159c5 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
159c6 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
159c7 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
159c8 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
159c9 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
159ca 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
159cb 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
159cc 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
159cd 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
159ce 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
159cf 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
159d0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
159d1 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
159d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
159d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
159d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
159d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
159d6 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
159d7 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73  ains routines us
159d8 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67  ed for analyzing
159d9 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64   expressions and
159da 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69  .** for generati
159db 6e 67 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  ng VDBE code tha
159dc 74 20 65 76 61 6c 75 61 74 65 73 20 65 78 70 72  t evaluates expr
159dd 65 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74  essions in SQLit
159de 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 65 78  e..**.** $Id: ex
159df 70 72 2e 63 2c 76 20 31 2e 33 31 32 20 32 30 30  pr.c,v 1.312 200
159e0 37 2f 30 39 2f 30 31 20 31 38 3a 32 34 3a 35 35  7/09/01 18:24:55
159e1 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
159e2 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   $.*/../*.** Ret
159e3 75 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74  urn the 'affinit
159e4 79 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  y' of the expres
159e5 73 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e  sion pExpr if an
159e6 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70  y..**.** If pExp
159e7 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61  r is a column, a
159e8 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
159e9 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41  column via an 'A
159ea 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20  S' alias,.** or 
159eb 61 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74  a sub-select wit
159ec 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68  h a column as th
159ed 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20  e return value, 
159ee 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66  then the .** aff
159ef 69 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f  inity of that co
159f0 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64  lumn is returned
159f1 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30  . Otherwise, 0x0
159f2 30 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a  0 is returned,.*
159f3 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20  * indicating no 
159f4 61 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65  affinity for the
159f5 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a   expression..**.
159f6 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52  ** i.e. the WHER
159f7 45 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73  E clause express
159f8 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c  sions in the fol
159f9 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
159fa 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e  s all.** have an
159fb 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a   affinity:.**.**
159fc 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
159fd 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a  (a);.** SELECT *
159fe 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
159ff 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53  ;.** SELECT a AS
15a00 20 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   b FROM t1 WHERE
15a01 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20   b;.** SELECT * 
15a02 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73  FROM t1 WHERE (s
15a03 65 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29  elect a from t1)
15a04 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.*/.SQLITE_PRIV
15a05 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
15a06 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70  ExprAffinity(Exp
15a07 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
15a08 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b   op = pExpr->op;
15a09 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45  .  if( op==TK_SE
15a0a 4c 45 43 54 20 29 7b 0a 20 20 20 20 72 65 74 75  LECT ){.    retu
15a0b 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41 66  rn sqlite3ExprAf
15a0c 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 53  finity(pExpr->pS
15a0d 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61  elect->pEList->a
15a0e 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a  [0].pExpr);.  }.
15a0f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15a10 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f  MIT_CAST.  if( o
15a11 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20  p==TK_CAST ){.  
15a12 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
15a13 41 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45  AffinityType(&pE
15a14 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d  xpr->token);.  }
15a15 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
15a16 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
15a17 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
15a18 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
15a19 75 65 6e 63 65 20 66 6f 72 20 65 78 70 72 65 73  uence for expres
15a1a 73 69 6f 6e 20 70 45 78 70 72 20 74 6f 20 62 65  sion pExpr to be
15a1b 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a   the collating.*
15a1c 2a 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64  * sequence named
15a1d 20 62 79 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65   by pToken.   Re
15a1e 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
15a1f 6f 20 74 68 65 20 72 65 76 69 73 65 64 20 65 78  o the revised ex
15a20 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65  pression..** The
15a21 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
15a22 6e 63 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73  nce is marked as
15a23 20 22 65 78 70 6c 69 63 69 74 22 20 75 73 69 6e   "explicit" usin
15a24 67 20 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c  g the EP_ExpColl
15a25 61 74 65 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e  ate.** flag.  An
15a26 20 65 78 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74   explicit collat
15a27 69 6e 67 20 73 65 71 75 65 6e 63 65 20 77 69 6c  ing sequence wil
15a28 6c 20 6f 76 65 72 72 69 64 65 20 69 6d 70 6c 69  l override impli
15a29 63 69 74 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67  cit.** collating
15a2a 20 73 65 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53   sequences..*/.S
15a2b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
15a2c 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53  pr *sqlite3ExprS
15a2d 65 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50  etColl(Parse *pP
15a2e 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
15a2f 72 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29  r, Token *pName)
15a30 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
15a31 6c 6c 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d  ll;.  if( pExpr=
15a32 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
15a33 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
15a34 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50  LocateCollSeq(pP
15a35 61 72 73 65 2c 20 28 63 68 61 72 2a 29 70 4e 61  arse, (char*)pNa
15a36 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29  me->z, pName->n)
15a37 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b  ;.  if( pColl ){
15a38 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c  .    pExpr->pCol
15a39 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 70  l = pColl;.    p
15a3a 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Expr->flags |= E
15a3b 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
15a3c 7d 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72  }.  return pExpr
15a3d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
15a3e 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f  n the default co
15a3f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
15a40 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73   for the express
15a41 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a  ion pExpr. If.**
15a42 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66   there is no def
15a43 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74  ault collation t
15a44 79 70 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a  ype, return 0..*
15a45 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15a46 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65   CollSeq *sqlite
15a47 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72  3ExprCollSeq(Par
15a48 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
15a49 20 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c   *pExpr){.  Coll
15a4a 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a  Seq *pColl = 0;.
15a4b 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20    if( pExpr ){. 
15a4c 20 20 20 69 6e 74 20 6f 70 3b 0a 20 20 20 20 70     int op;.    p
15a4d 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43  Coll = pExpr->pC
15a4e 6f 6c 6c 3b 0a 20 20 20 20 6f 70 20 3d 20 70 45  oll;.    op = pE
15a4f 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 69 66 28  xpr->op;.    if(
15a50 20 28 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 7c 7c   (op==TK_CAST ||
15a51 20 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 29 20 26   op==TK_UPLUS) &
15a52 26 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  & !pColl ){.    
15a53 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
15a54 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
15a55 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
15a56 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  );.    }.  }.  i
15a57 66 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43  f( sqlite3CheckC
15a58 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
15a59 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43  Coll) ){ .    pC
15a5a 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  oll = 0;.  }.  r
15a5b 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a  eturn pColl;.}..
15a5c 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61  /*.** pExpr is a
15a5d 6e 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63  n operand of a c
15a5e 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74  omparison operat
15a5f 6f 72 2e 20 20 61 66 66 32 20 69 73 20 74 68 65  or.  aff2 is the
15a60 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74  .** type affinit
15a61 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f  y of the other o
15a62 70 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f  perand.  This ro
15a63 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
15a64 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69  e.** type affini
15a65 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  ty that should b
15a66 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63  e used for the c
15a67 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74  omparison operat
15a68 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
15a69 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74  IVATE char sqlit
15a6a 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
15a6b 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63  y(Expr *pExpr, c
15a6c 68 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61  har aff2){.  cha
15a6d 72 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33  r aff1 = sqlite3
15a6e 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78  ExprAffinity(pEx
15a6f 70 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20  pr);.  if( aff1 
15a70 26 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f  && aff2 ){.    /
15a71 2a 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20  * Both sides of 
15a72 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61  the comparison a
15a73 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f  re columns. If o
15a74 6e 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20  ne has numeric. 
15a75 20 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20     ** affinity, 
15a76 75 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77  use that. Otherw
15a77 69 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e  ise use no affin
15a78 69 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ity..    */.    
15a79 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  if( sqlite3IsNum
15a7a 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66  ericAffinity(aff
15a7b 31 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e  1) || sqlite3IsN
15a7c 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61  umericAffinity(a
15a7d 66 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ff2) ){.      re
15a7e 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f  turn SQLITE_AFF_
15a7f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c  NUMERIC;.    }el
15a80 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  se{.      return
15a81 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
15a82 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
15a83 69 66 28 20 21 61 66 66 31 20 26 26 20 21 61 66  if( !aff1 && !af
15a84 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69  f2 ){.    /* Nei
15a85 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65  ther side of the
15a86 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61   comparison is a
15a87 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72   column.  Compar
15a88 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73  e the.    ** res
15a89 75 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20  ults directly.. 
15a8a 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e     */.    return
15a8b 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
15a8c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
15a8d 2a 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20  * One side is a 
15a8e 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65  column, the othe
15a8f 72 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68  r is not. Use th
15a90 65 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69  e columns affini
15a91 74 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ty. */.    asser
15a92 74 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66  t( aff1==0 || af
15a93 66 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74  f2==0 );.    ret
15a94 75 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32  urn (aff1 + aff2
15a95 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
15a96 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61  pExpr is a compa
15a97 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20  rison operator. 
15a98 20 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65   Return the type
15a99 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73   affinity that s
15a9a 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c  hould.** be appl
15a9b 69 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72  ied to both oper
15a9c 61 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f  ands prior to do
15a9d 69 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ing the comparis
15a9e 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  on..*/.static ch
15a9f 61 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66  ar comparisonAff
15aa0 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70  inity(Expr *pExp
15aa1 72 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a  r){.  char aff;.
15aa2 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
15aa3 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45  >op==TK_EQ || pE
15aa4 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c  xpr->op==TK_IN |
15aa5 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  | pExpr->op==TK_
15aa6 4c 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20  LT ||.          
15aa7 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54  pExpr->op==TK_GT
15aa8 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54   || pExpr->op==T
15aa9 4b 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f  K_GE || pExpr->o
15aaa 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20  p==TK_LE ||.    
15aab 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d        pExpr->op=
15aac 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65  =TK_NE );.  asse
15aad 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  rt( pExpr->pLeft
15aae 20 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69   );.  aff = sqli
15aaf 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
15ab0 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20  pExpr->pLeft);. 
15ab1 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67   if( pExpr->pRig
15ab2 68 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20  ht ){.    aff = 
15ab3 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
15ab4 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52  finity(pExpr->pR
15ab5 69 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 0a  ight, aff);.  }.
15ab6 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72    else if( pExpr
15ab7 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
15ab8 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f   aff = sqlite3Co
15ab9 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45  mpareAffinity(pE
15aba 78 70 72 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 45  xpr->pSelect->pE
15abb 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
15abc 2c 20 61 66 66 29 3b 0a 20 20 7d 0a 20 20 65 6c  , aff);.  }.  el
15abd 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20  se if( !aff ){. 
15abe 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
15abf 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20  AFF_NONE;.  }.  
15ac0 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f  return aff;.}../
15ac1 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20  *.** pExpr is a 
15ac2 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65  comparison expre
15ac3 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20  ssion, eg. '=', 
15ac4 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63  '<', IN(...) etc
15ac5 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74  ..** idx_affinit
15ac6 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74  y is the affinit
15ac7 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20  y of an indexed 
15ac8 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74  column. Return t
15ac9 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e  rue.** if the in
15aca 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74  dex with affinit
15acb 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d  y idx_affinity m
15acc 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d  ay be used to im
15acd 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63  plement.** the c
15ace 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78  omparison in pEx
15acf 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pr..*/.SQLITE_PR
15ad0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15ad1 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b  3IndexAffinityOk
15ad2 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
15ad3 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29  ar idx_affinity)
15ad4 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63  {.  char aff = c
15ad5 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74  omparisonAffinit
15ad6 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74  y(pExpr);.  swit
15ad7 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63  ch( aff ){.    c
15ad8 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ase SQLITE_AFF_N
15ad9 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72  ONE:.      retur
15ada 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51  n 1;.    case SQ
15adb 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20  LITE_AFF_TEXT:. 
15adc 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f       return idx_
15add 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
15ade 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64  _AFF_TEXT;.    d
15adf 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65  efault:.      re
15ae0 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75  turn sqlite3IsNu
15ae1 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64  mericAffinity(id
15ae2 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d  x_affinity);.  }
15ae3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
15ae4 20 74 68 65 20 50 31 20 76 61 6c 75 65 20 74 68   the P1 value th
15ae5 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  at should be use
15ae6 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63  d for a binary c
15ae7 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63  omparison.** opc
15ae8 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47  ode (OP_Eq, OP_G
15ae9 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20  e etc.) used to 
15aea 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61  compare pExpr1 a
15aeb 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2a 20 49 66  nd pExpr2..** If
15aec 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20 74   jumpIfNull is t
15aed 72 75 65 2c 20 74 68 65 6e 20 73 65 74 20 74 68  rue, then set th
15aee 65 20 6c 6f 77 20 62 79 74 65 20 6f 66 20 74 68  e low byte of th
15aef 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 50 31  e returned.** P1
15af0 20 76 61 6c 75 65 20 74 6f 20 74 65 6c 6c 20 74   value to tell t
15af1 68 65 20 6f 70 63 6f 64 65 20 74 6f 20 6a 75 6d  he opcode to jum
15af2 70 20 69 66 20 65 69 74 68 65 72 20 65 78 70 72  p if either expr
15af3 65 73 73 69 6f 6e 0a 2a 2a 20 65 76 61 6c 75 61  ession.** evalua
15af4 74 65 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  tes to NULL..*/.
15af5 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e 61 72  static int binar
15af6 79 43 6f 6d 70 61 72 65 50 31 28 45 78 70 72 20  yCompareP1(Expr 
15af7 2a 70 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70  *pExpr1, Expr *p
15af8 45 78 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49  Expr2, int jumpI
15af9 66 4e 75 6c 6c 29 7b 0a 20 20 63 68 61 72 20 61  fNull){.  char a
15afa 66 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ff = sqlite3Expr
15afb 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32 29  Affinity(pExpr2)
15afc 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 69 6e 74  ;.  return ((int
15afd 29 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41  )sqlite3CompareA
15afe 66 66 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20  ffinity(pExpr1, 
15aff 61 66 66 29 29 2b 28 6a 75 6d 70 49 66 4e 75 6c  aff))+(jumpIfNul
15b00 6c 3f 30 78 31 30 30 3a 30 29 3b 0a 7d 0a 0a 2f  l?0x100:0);.}../
15b01 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
15b02 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c  inter to the col
15b03 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
15b04 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75  that should be u
15b05 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e 61  sed by.** a bina
15b06 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70  ry comparison op
15b07 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e 67  erator comparing
15b08 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68   pLeft and pRigh
15b09 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
15b0a 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 65 73  left hand expres
15b0b 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61  sion has a colla
15b0c 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 79  ting sequence ty
15b0d 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a 2a  pe, then it is.*
15b0e 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69 73  * used. Otherwis
15b0f 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  e the collation 
15b10 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65  sequence for the
15b11 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 70 72   right hand expr
15b12 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73 65  ession.** is use
15b13 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75 6c  d, or the defaul
15b14 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e 65  t (BINARY) if ne
15b15 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  ither expression
15b16 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67   has a collating
15b17 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20  .** type..**.** 
15b18 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74 20  Argument pRight 
15b19 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29 20  (but not pLeft) 
15b1a 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f  may be a null po
15b1b 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20 63  inter. In this c
15b1c 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f  ase,.** it is no
15b1d 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a 2f  t considered..*/
15b1e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15b1f 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
15b20 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
15b21 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a 70  lSeq(.  Parse *p
15b22 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 2a  Parse, .  Expr *
15b23 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20 2a  pLeft, .  Expr *
15b24 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c 6c  pRight.){.  Coll
15b25 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61 73  Seq *pColl;.  as
15b26 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20  sert( pLeft );. 
15b27 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67   if( pLeft->flag
15b28 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
15b29 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  e ){.    assert(
15b2a 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29 3b   pLeft->pColl );
15b2b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c 65  .    pColl = pLe
15b2c 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c  ft->pColl;.  }el
15b2d 73 65 20 69 66 28 20 70 52 69 67 68 74 20 26 26  se if( pRight &&
15b2e 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26   pRight->flags &
15b2f 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29   EP_ExpCollate )
15b30 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52  {.    assert( pR
15b31 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20  ight->pColl );. 
15b32 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68     pColl = pRigh
15b33 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c 73  t->pColl;.  }els
15b34 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  e{.    pColl = s
15b35 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
15b36 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29  q(pParse, pLeft)
15b37 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c  ;.    if( !pColl
15b38 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20   ){.      pColl 
15b39 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
15b3a 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 52 69  lSeq(pParse, pRi
15b3b 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ght);.    }.  }.
15b3c 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a    return pColl;.
15b3d 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
15b3e 65 20 63 6f 64 65 20 66 6f 72 20 61 20 63 6f 6d  e code for a com
15b3f 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
15b40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15b41 63 6f 64 65 43 6f 6d 70 61 72 65 28 0a 20 20 50  codeCompare(.  P
15b42 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
15b43 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
15b44 28 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61  (and code genera
15b45 74 69 6e 67 29 20 63 6f 6e 74 65 78 74 20 2a 2f  ting) context */
15b46 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20  .  Expr *pLeft, 
15b47 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74       /* The left
15b48 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78   operand */.  Ex
15b49 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20  pr *pRight,     
15b4a 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65  /* The right ope
15b4b 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  rand */.  int op
15b4c 63 6f 64 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  code,       /* T
15b4d 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70  he comparison op
15b4e 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 64 65  code */.  int de
15b4f 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  st,         /* J
15b50 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65  ump here if true
15b51 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70  .  */.  int jump
15b52 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20  IfNull    /* If 
15b53 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69  true, jump if ei
15b54 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
15b55 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  NULL */.){.  int
15b56 20 70 31 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70   p1 = binaryComp
15b57 61 72 65 50 31 28 70 4c 65 66 74 2c 20 70 52 69  areP1(pLeft, pRi
15b58 67 68 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  ght, jumpIfNull)
15b59 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 33 20  ;.  CollSeq *p3 
15b5a 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43  = sqlite3BinaryC
15b5b 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50  ompareCollSeq(pP
15b5c 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69  arse, pLeft, pRi
15b5d 67 68 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  ght);.  return s
15b5e 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 70 50  qlite3VdbeOp3(pP
15b5f 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 6f 70 63  arse->pVdbe, opc
15b60 6f 64 65 2c 20 70 31 2c 20 64 65 73 74 2c 20 28  ode, p1, dest, (
15b61 76 6f 69 64 2a 29 70 33 2c 20 50 33 5f 43 4f 4c  void*)p3, P3_COL
15b62 4c 53 45 51 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  LSEQ);.}../*.** 
15b63 43 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20  Construct a new 
15b64 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
15b65 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
15b66 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 4d 65 6d  nter to it.  Mem
15b67 6f 72 79 0a 2a 2a 20 66 6f 72 20 74 68 69 73 20  ory.** for this 
15b68 6e 6f 64 65 20 69 73 20 6f 62 74 61 69 6e 65 64  node is obtained
15b69 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
15b6a 6c 6c 6f 63 28 29 2e 20 20 54 68 65 20 63 61 6c  lloc().  The cal
15b6b 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  ling function.**
15b6c 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
15b6d 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20  for making sure 
15b6e 74 68 65 20 6e 6f 64 65 20 65 76 65 6e 74 75 61  the node eventua
15b6f 6c 6c 79 20 67 65 74 73 20 66 72 65 65 64 2e 0a  lly gets freed..
15b70 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
15b71 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
15b72 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  xpr(.  sqlite3 *
15b73 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
15b74 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c  * Handle for sql
15b75 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
15b76 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29  () (may be null)
15b77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
15b78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15b79 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f   Expression opco
15b7a 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  de */.  Expr *pL
15b7b 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  eft,            
15b7c 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 20  /* Left operand 
15b7d 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68  */.  Expr *pRigh
15b7e 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t,           /* 
15b7f 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f  Right operand */
15b80 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a  .  const Token *
15b81 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 41 72  pToken     /* Ar
15b82 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a  gument token */.
15b83 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b  ){.  Expr *pNew;
15b84 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
15b85 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
15b86 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29 29 3b  , sizeof(Expr));
15b87 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
15b88 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 6d 61  {.    /* When ma
15b89 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 64 65 6c 65  lloc fails, dele
15b8a 74 65 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  te pLeft and pRi
15b8b 67 68 74 2e 20 45 78 70 72 65 73 73 69 6f 6e 73  ght. Expressions
15b8c 20 70 61 73 73 65 64 20 74 6f 20 0a 20 20 20 20   passed to .    
15b8d 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
15b8e 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 20   must always be 
15b8f 61 6c 6c 6f 63 61 74 65 64 20 77 69 74 68 20 73  allocated with s
15b90 71 6c 69 74 65 33 45 78 70 72 28 29 20 66 6f 72  qlite3Expr() for
15b91 20 74 68 69 73 20 0a 20 20 20 20 2a 2a 20 72 65   this .    ** re
15b92 61 73 6f 6e 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  ason. .    */.  
15b93 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
15b94 65 74 65 28 70 4c 65 66 74 29 3b 0a 20 20 20 20  ete(pLeft);.    
15b95 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
15b96 65 28 70 52 69 67 68 74 29 3b 0a 20 20 20 20 72  e(pRight);.    r
15b97 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
15b98 4e 65 77 2d 3e 6f 70 20 3d 20 6f 70 3b 0a 20 20  New->op = op;.  
15b99 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70 4c  pNew->pLeft = pL
15b9a 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69  eft;.  pNew->pRi
15b9b 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20  ght = pRight;.  
15b9c 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b  pNew->iAgg = -1;
15b9d 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b  .  if( pToken ){
15b9e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f  .    assert( pTo
15b9f 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20  ken->dyn==0 );. 
15ba0 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20     pNew->span = 
15ba1 70 4e 65 77 2d 3e 74 6f 6b 65 6e 20 3d 20 2a 70  pNew->token = *p
15ba2 54 6f 6b 65 6e 3b 0a 20 20 7d 65 6c 73 65 20 69  Token;.  }else i
15ba3 66 28 20 70 4c 65 66 74 20 29 7b 0a 20 20 20 20  f( pLeft ){.    
15ba4 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a 20 20  if( pRight ){.  
15ba5 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
15ba6 70 61 6e 28 70 4e 65 77 2c 20 26 70 4c 65 66 74  pan(pNew, &pLeft
15ba7 2d 3e 73 70 61 6e 2c 20 26 70 52 69 67 68 74 2d  ->span, &pRight-
15ba8 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 69 66  >span);.      if
15ba9 28 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20  ( pRight->flags 
15baa 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  & EP_ExpCollate 
15bab 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d  ){.        pNew-
15bac 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70  >flags |= EP_Exp
15bad 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20  Collate;.       
15bae 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70   pNew->pColl = p
15baf 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20  Right->pColl;.  
15bb0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
15bb1 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73  if( pLeft->flags
15bb2 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
15bb3 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e   ){.      pNew->
15bb4 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43  flags |= EP_ExpC
15bb5 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 70 4e  ollate;.      pN
15bb6 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66  ew->pColl = pLef
15bb7 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a  t->pColl;.    }.
15bb8 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 45 78    }..  sqlite3Ex
15bb9 70 72 53 65 74 48 65 69 67 68 74 28 70 4e 65 77  prSetHeight(pNew
15bba 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  );.  return pNew
15bbb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 6f 72 6b 73  ;.}../*.** Works
15bbc 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 45 78 70   like sqlite3Exp
15bbd 72 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20  r() except that 
15bbe 69 74 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72  it takes an extr
15bbf 61 20 50 61 72 73 65 2a 0a 2a 2a 20 61 72 67 75  a Parse*.** argu
15bc0 6d 65 6e 74 20 61 6e 64 20 6e 6f 74 69 66 69 65  ment and notifie
15bc1 73 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  s the associated
15bc2 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65   connection obje
15bc3 63 74 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69  ct if malloc fai
15bc4 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ls..*/.SQLITE_PR
15bc5 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
15bc6 74 65 33 50 45 78 70 72 28 0a 20 20 50 61 72 73  te3PExpr(.  Pars
15bc7 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
15bc8 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
15bc9 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
15bca 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
15bcb 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
15bcc 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78  n opcode */.  Ex
15bcd 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20  pr *pLeft,      
15bce 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70        /* Left op
15bcf 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20  erand */.  Expr 
15bd0 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20 20 20  *pRight,        
15bd1 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72     /* Right oper
15bd2 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54  and */.  const T
15bd3 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20  oken *pToken    
15bd4 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b   /* Argument tok
15bd5 65 6e 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72  en */.){.  retur
15bd6 6e 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 50  n sqlite3Expr(pP
15bd7 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c 20 70 4c  arse->db, op, pL
15bd8 65 66 74 2c 20 70 52 69 67 68 74 2c 20 70 54 6f  eft, pRight, pTo
15bd9 6b 65 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  ken);.}../*.** W
15bda 68 65 6e 20 64 6f 69 6e 67 20 61 20 6e 65 73 74  hen doing a nest
15bdb 65 64 20 70 61 72 73 65 2c 20 79 6f 75 20 63 61  ed parse, you ca
15bdc 6e 20 69 6e 63 6c 75 64 65 20 74 65 72 6d 73 20  n include terms 
15bdd 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  in an expression
15bde 0a 2a 2a 20 74 68 61 74 20 6c 6f 6f 6b 20 6c 69  .** that look li
15bdf 6b 65 20 74 68 69 73 3a 20 20 20 23 30 20 23 31  ke this:   #0 #1
15be0 20 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 74   #2 ...  These t
15be1 65 72 6d 73 20 72 65 66 65 72 20 74 6f 20 65 6c  erms refer to el
15be2 65 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 20 74 68 65  ements.** on the
15be3 20 73 74 61 63 6b 2e 20 20 22 23 30 22 20 6d 65   stack.  "#0" me
15be4 61 6e 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74  ans the top of t
15be5 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 22 23 31  he stack..** "#1
15be6 22 20 6d 65 61 6e 73 20 74 68 65 20 6e 65 78 74  " means the next
15be7 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20 73 74 61   down on the sta
15be8 63 6b 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74  ck.  And so fort
15be9 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  h..**.** This ro
15bea 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
15beb 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f  by the parser to
15bec 20 64 65 61 6c 20 77 69 74 68 20 6f 6e 20 6f 66   deal with on of
15bed 20 74 68 6f 73 65 20 74 65 72 6d 73 2e 0a 2a 2a   those terms..**
15bee 20 49 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   It immediately 
15bef 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
15bf0 6f 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75  o store the valu
15bf1 65 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 6c 6f  e in a memory lo
15bf2 63 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 72  cation..** The r
15bf3 65 74 75 72 6e 73 20 61 6e 20 65 78 70 72 65 73  eturns an expres
15bf4 73 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 63  sion that will c
15bf5 6f 64 65 20 74 6f 20 65 78 74 72 61 63 74 20 74  ode to extract t
15bf6 68 65 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a  he value from.**
15bf7 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 6c 6f 63   that memory loc
15bf8 61 74 69 6f 6e 20 61 73 20 6e 65 65 64 65 64 2e  ation as needed.
15bf9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15bfa 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
15bfb 52 65 67 69 73 74 65 72 45 78 70 72 28 50 61 72  RegisterExpr(Par
15bfc 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
15bfd 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 56 64  n *pToken){.  Vd
15bfe 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
15bff 70 56 64 62 65 3b 0a 20 20 45 78 70 72 20 2a 70  pVdbe;.  Expr *p
15c00 3b 0a 20 20 69 6e 74 20 64 65 70 74 68 3b 0a 20  ;.  int depth;. 
15c01 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
15c02 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ted==0 ){.    sq
15c03 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
15c04 61 72 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54  arse, "near \"%T
15c05 5c 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72  \": syntax error
15c06 22 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  ", pToken);.    
15c07 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45  return sqlite3PE
15c08 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
15c09 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ULL, 0, 0, 0);. 
15c0a 20 7d 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20   }.  if( v==0 ) 
15c0b 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20  return 0;.  p = 
15c0c 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
15c0d 72 73 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52  rse, TK_REGISTER
15c0e 2c 20 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b  , 0, 0, pToken);
15c0f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
15c10 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a     return 0;  /*
15c11 20 4d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a   Malloc failed *
15c12 2f 0a 20 20 7d 0a 20 20 64 65 70 74 68 20 3d 20  /.  }.  depth = 
15c13 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70 54 6f  atoi((char*)&pTo
15c14 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 70 2d  ken->z[1]);.  p-
15c15 3e 69 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65  >iTable = pParse
15c16 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 73 71 6c 69  ->nMem++;.  sqli
15c17 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
15c18 4f 50 5f 44 75 70 2c 20 64 65 70 74 68 2c 20 30  OP_Dup, depth, 0
15c19 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
15c1a 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53  AddOp(v, OP_MemS
15c1b 74 6f 72 65 2c 20 70 2d 3e 69 54 61 62 6c 65 2c  tore, p->iTable,
15c1c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b   1);.  return p;
15c1d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69 6e 20 74  .}../*.** Join t
15c1e 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75  wo expressions u
15c1f 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f 70 65 72  sing an AND oper
15c20 61 74 6f 72 2e 20 20 49 66 20 65 69 74 68 65 72  ator.  If either
15c21 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a   expression is.*
15c22 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 6a 75 73  * NULL, then jus
15c23 74 20 72 65 74 75 72 6e 20 74 68 65 20 6f 74 68  t return the oth
15c24 65 72 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  er expression..*
15c25 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15c26 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
15c27 70 72 41 6e 64 28 73 71 6c 69 74 65 33 20 2a 64  prAnd(sqlite3 *d
15c28 62 2c 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20  b, Expr *pLeft, 
15c29 45 78 70 72 20 2a 70 52 69 67 68 74 29 7b 0a 20  Expr *pRight){. 
15c2a 20 69 66 28 20 70 4c 65 66 74 3d 3d 30 20 29 7b   if( pLeft==0 ){
15c2b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 52 69 67  .    return pRig
15c2c 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ht;.  }else if( 
15c2d 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  pRight==0 ){.   
15c2e 20 72 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20   return pLeft;. 
15c2f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
15c30 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 28 64  rn sqlite3Expr(d
15c31 62 2c 20 54 4b 5f 41 4e 44 2c 20 70 4c 65 66 74  b, TK_AND, pLeft
15c32 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20  , pRight, 0);.  
15c33 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
15c34 68 65 20 45 78 70 72 2e 73 70 61 6e 20 66 69 65  he Expr.span fie
15c35 6c 64 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20  ld of the given 
15c36 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 70  expression to sp
15c37 61 6e 20 61 6c 6c 0a 2a 2a 20 74 65 78 74 20 62  an all.** text b
15c38 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 67  etween the two g
15c39 69 76 65 6e 20 74 6f 6b 65 6e 73 2e 0a 2a 2f 0a  iven tokens..*/.
15c3a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
15c3b 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 53  oid sqlite3ExprS
15c3c 70 61 6e 28 45 78 70 72 20 2a 70 45 78 70 72 2c  pan(Expr *pExpr,
15c3d 20 54 6f 6b 65 6e 20 2a 70 4c 65 66 74 2c 20 54   Token *pLeft, T
15c3e 6f 6b 65 6e 20 2a 70 52 69 67 68 74 29 7b 0a 20  oken *pRight){. 
15c3f 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74 21   assert( pRight!
15c40 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
15c41 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 69 66  pLeft!=0 );.  if
15c42 28 20 70 45 78 70 72 20 26 26 20 70 52 69 67 68  ( pExpr && pRigh
15c43 74 2d 3e 7a 20 26 26 20 70 4c 65 66 74 2d 3e 7a  t->z && pLeft->z
15c44 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
15c45 70 4c 65 66 74 2d 3e 64 79 6e 3d 3d 30 20 7c 7c  pLeft->dyn==0 ||
15c46 20 70 4c 65 66 74 2d 3e 7a 5b 70 4c 65 66 74 2d   pLeft->z[pLeft-
15c47 3e 6e 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  >n]==0 );.    if
15c48 28 20 70 4c 65 66 74 2d 3e 64 79 6e 3d 3d 30 20  ( pLeft->dyn==0 
15c49 26 26 20 70 52 69 67 68 74 2d 3e 64 79 6e 3d 3d  && pRight->dyn==
15c4a 30 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72  0 ){.      pExpr
15c4b 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4c 65 66 74  ->span.z = pLeft
15c4c 2d 3e 7a 3b 0a 20 20 20 20 20 20 70 45 78 70 72  ->z;.      pExpr
15c4d 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68  ->span.n = pRigh
15c4e 74 2d 3e 6e 20 2b 20 28 70 52 69 67 68 74 2d 3e  t->n + (pRight->
15c4f 7a 20 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b 0a 20  z - pLeft->z);. 
15c50 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
15c51 70 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 20 3d 20  pExpr->span.z = 
15c52 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
15c53 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20  /*.** Construct 
15c54 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e  a new expression
15c55 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e 63   node for a func
15c56 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69 70  tion with multip
15c57 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e  le.** arguments.
15c58 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15c59 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
15c5a 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72  ExprFunction(Par
15c5b 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
15c5c 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b  List *pList, Tok
15c5d 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45  en *pToken){.  E
15c5e 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 61 73 73  xpr *pNew;.  ass
15c5f 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a 20  ert( pToken );. 
15c60 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
15c61 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72  bMallocZero(pPar
15c62 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 45  se->db, sizeof(E
15c63 78 70 72 29 20 29 3b 0a 20 20 69 66 28 20 70 4e  xpr) );.  if( pN
15c64 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ew==0 ){.    sql
15c65 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
15c66 74 65 28 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76  te(pList); /* Av
15c67 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f  oid leaking memo
15c68 72 79 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66  ry when malloc f
15c69 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74 75  ails */.    retu
15c6a 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77  rn 0;.  }.  pNew
15c6b 2d 3e 6f 70 20 3d 20 54 4b 5f 46 55 4e 43 54 49  ->op = TK_FUNCTI
15c6c 4f 4e 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 73  ON;.  pNew->pLis
15c6d 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 61 73 73  t = pList;.  ass
15c6e 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e  ert( pToken->dyn
15c6f 3d 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 74  ==0 );.  pNew->t
15c70 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a  oken = *pToken;.
15c71 20 20 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70    pNew->span = p
15c72 4e 65 77 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 73  New->token;..  s
15c73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
15c74 67 68 74 28 70 4e 65 77 29 3b 0a 20 20 72 65 74  ght(pNew);.  ret
15c75 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
15c76 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 72 69  ** Assign a vari
15c77 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20 61  able number to a
15c78 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  n expression tha
15c79 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 6c 64  t encodes a wild
15c7a 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f  card.** in the o
15c7b 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74  riginal SQL stat
15c7c 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57  ement.  .**.** W
15c7d 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69 73 74  ildcards consist
15c7e 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  ing of a single 
15c7f 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e 65 64  "?" are assigned
15c80 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e   the next sequen
15c81 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65  tial.** variable
15c82 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57   number..**.** W
15c83 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20  ildcards of the 
15c84 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20  form "?nnn" are 
15c85 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 75 6d  assigned the num
15c86 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d  ber "nnn".  We m
15c87 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e  ake.** sure "nnn
15c88 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20  " is not too be 
15c89 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e 69 61  to avoid a denia
15c8a 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61 74 74  l of service att
15c8b 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  ack when.** the 
15c8c 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  SQL statement co
15c8d 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78 74 65  mes from an exte
15c8e 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a  rnal source..**.
15c8f 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20  ** Wildcards of 
15c90 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20  the form ":aaa" 
15c91 6f 72 20 22 24 61 61 61 22 20 61 72 65 20 61 73  or "$aaa" are as
15c92 73 69 67 6e 65 64 20 74 68 65 20 73 61 6d 65 20  signed the same 
15c93 6e 75 6d 62 65 72 0a 2a 2a 20 61 73 20 74 68 65  number.** as the
15c94 20 70 72 65 76 69 6f 75 73 20 69 6e 73 74 61 6e   previous instan
15c95 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 77  ce of the same w
15c96 69 6c 64 63 61 72 64 2e 20 20 4f 72 20 69 66 20  ildcard.  Or if 
15c97 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
15c98 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  t.** instance of
15c99 20 74 68 65 20 77 69 6c 64 63 61 72 64 2c 20 74   the wildcard, t
15c9a 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e 69 61  he next sequenia
15c9b 6c 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65  l variable numbe
15c9c 72 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64  r is.** assigned
15c9d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
15c9e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
15c9f 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d  ExprAssignVarNum
15ca0 62 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  ber(Parse *pPars
15ca1 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  e, Expr *pExpr){
15ca2 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  .  Token *pToken
15ca3 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
15ca4 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
15ca5 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
15ca6 72 65 74 75 72 6e 3b 0a 20 20 70 54 6f 6b 65 6e  return;.  pToken
15ca7 20 3d 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e   = &pExpr->token
15ca8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b  ;.  assert( pTok
15ca9 65 6e 2d 3e 6e 3e 3d 31 20 29 3b 0a 20 20 61 73  en->n>=1 );.  as
15caa 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 21  sert( pToken->z!
15cab 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
15cac 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 21 3d 30 20  pToken->z[0]!=0 
15cad 29 3b 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d  );.  if( pToken-
15cae 3e 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  >n==1 ){.    /* 
15caf 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20  Wildcard of the 
15cb0 66 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67  form "?".  Assig
15cb1 6e 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61  n the next varia
15cb2 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ble number */.  
15cb3 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
15cb4 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72  = ++pParse->nVar
15cb5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54  ;.  }else if( pT
15cb6 6f 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 3f 27 20  oken->z[0]=='?' 
15cb7 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61  ){.    /* Wildca
15cb8 72 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  rd of the form "
15cb9 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20  ?nnn".  Convert 
15cba 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65  "nnn" to an inte
15cbb 67 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75  ger and.    ** u
15cbc 73 65 20 69 74 20 61 73 20 74 68 65 20 76 61 72  se it as the var
15cbd 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  iable number */.
15cbe 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70      int i;.    p
15cbf 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69  Expr->iTable = i
15cc0 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26   = atoi((char*)&
15cc1 70 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20  pToken->z[1]);. 
15cc2 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e     if( i<1 || i>
15cc3 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
15cc4 42 4c 45 5f 4e 55 4d 42 45 52 20 29 7b 0a 20 20  BLE_NUMBER ){.  
15cc5 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
15cc6 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 61 72  Msg(pParse, "var
15cc7 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73  iable number mus
15cc8 74 20 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20  t be between ?1 
15cc9 61 6e 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20  and ?%d",.      
15cca 20 20 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56      SQLITE_MAX_V
15ccb 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 29 3b  ARIABLE_NUMBER);
15ccc 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
15ccd 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 29 7b  >pParse->nVar ){
15cce 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
15ccf 56 61 72 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20  Var = i;.    }. 
15cd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57   }else{.    /* W
15cd1 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20  ildcards of the 
15cd2 66 6f 72 6d 20 22 3a 61 61 61 22 20 6f 72 20 22  form ":aaa" or "
15cd3 24 61 61 61 22 2e 20 20 52 65 75 73 65 20 74 68  $aaa".  Reuse th
15cd4 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 0a  e same variable.
15cd5 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73      ** number as
15cd6 20 74 68 65 20 70 72 69 6f 72 20 61 70 70 65 61   the prior appea
15cd7 72 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61 6d  rance of the sam
15cd8 65 20 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74 68  e name, or if th
15cd9 65 20 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 68 61  e name.    ** ha
15cda 73 20 6e 65 76 65 72 20 61 70 70 65 61 72 65 64  s never appeared
15cdb 20 62 65 66 6f 72 65 2c 20 72 65 75 73 65 20 74   before, reuse t
15cdc 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65  he same variable
15cdd 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2f 0a 20   number.    */. 
15cde 20 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 20     int i, n;.   
15cdf 20 6e 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a   n = pToken->n;.
15ce0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
15ce1 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b  Parse->nVarExpr;
15ce2 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70   i++){.      Exp
15ce3 72 20 2a 70 45 3b 0a 20 20 20 20 20 20 69 66 28  r *pE;.      if(
15ce4 20 28 70 45 20 3d 20 70 50 61 72 73 65 2d 3e 61   (pE = pParse->a
15ce5 70 56 61 72 45 78 70 72 5b 69 5d 29 21 3d 30 0a  pVarExpr[i])!=0.
15ce6 20 20 20 20 20 20 20 20 20 20 26 26 20 70 45 2d            && pE-
15ce7 3e 74 6f 6b 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20  >token.n==n.    
15ce8 20 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28        && memcmp(
15ce9 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f  pE->token.z, pTo
15cea 6b 65 6e 2d 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b  ken->z, n)==0 ){
15ceb 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
15cec 69 54 61 62 6c 65 20 3d 20 70 45 2d 3e 69 54 61  iTable = pE->iTa
15ced 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ble;.        bre
15cee 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
15cef 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d 70 50 61  }.    if( i>=pPa
15cf0 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 20 29 7b  rse->nVarExpr ){
15cf1 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54  .      pExpr->iT
15cf2 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d  able = ++pParse-
15cf3 3e 6e 56 61 72 3b 0a 20 20 20 20 20 20 69 66 28  >nVar;.      if(
15cf4 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70   pParse->nVarExp
15cf5 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  r>=pParse->nVarE
15cf6 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20  xprAlloc-1 ){.  
15cf7 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56        pParse->nV
15cf8 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d 20 70  arExprAlloc += p
15cf9 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
15cfa 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 20 20  lloc + 10;.     
15cfb 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72     pParse->apVar
15cfc 45 78 70 72 20 3d 0a 20 20 20 20 20 20 20 20 20  Expr =.         
15cfd 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c     sqlite3DbReal
15cfe 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 20 20 20  locOrFree(.     
15cff 20 20 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20           db,.   
15d00 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
15d01 65 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a 20 20  e->apVarExpr,.  
15d02 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72              pPar
15d03 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f  se->nVarExprAllo
15d04 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d  c*sizeof(pParse-
15d05 3e 61 70 56 61 72 45 78 70 72 5b 30 5d 29 0a 20  >apVarExpr[0]). 
15d06 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20             );.  
15d07 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
15d08 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
15d09 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  d ){.        ass
15d0a 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70 56  ert( pParse->apV
15d0b 61 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20  arExpr!=0 );.   
15d0c 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56       pParse->apV
15d0d 61 72 45 78 70 72 5b 70 50 61 72 73 65 2d 3e 6e  arExpr[pParse->n
15d0e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 45 78  VarExpr++] = pEx
15d0f 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  pr;.      }.    
15d10 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 70 50  }.  } .  if( !pP
15d11 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 70 50  arse->nErr && pP
15d12 61 72 73 65 2d 3e 6e 56 61 72 3e 53 51 4c 49 54  arse->nVar>SQLIT
15d13 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e  E_MAX_VARIABLE_N
15d14 55 4d 42 45 52 20 29 7b 0a 20 20 20 20 73 71 6c  UMBER ){.    sql
15d15 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
15d16 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53  rse, "too many S
15d17 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b 0a  QL variables");.
15d18 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63    }.}../*.** Rec
15d19 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20  ursively delete 
15d1a 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
15d1b 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ee..*/.SQLITE_PR
15d1c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
15d1d 65 33 45 78 70 72 44 65 6c 65 74 65 28 45 78 70  e3ExprDelete(Exp
15d1e 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d  r *p){.  if( p==
15d1f 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
15d20 28 20 70 2d 3e 73 70 61 6e 2e 64 79 6e 20 29 20  ( p->span.dyn ) 
15d21 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 63 68  sqlite3_free((ch
15d22 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a 29 3b 0a  ar*)p->span.z);.
15d23 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64    if( p->token.d
15d24 79 6e 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65  yn ) sqlite3_fre
15d25 65 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b 65  e((char*)p->toke
15d26 6e 2e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  n.z);.  sqlite3E
15d27 78 70 72 44 65 6c 65 74 65 28 70 2d 3e 70 4c 65  xprDelete(p->pLe
15d28 66 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  ft);.  sqlite3Ex
15d29 70 72 44 65 6c 65 74 65 28 70 2d 3e 70 52 69 67  prDelete(p->pRig
15d2a 68 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  ht);.  sqlite3Ex
15d2b 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 2d 3e  prListDelete(p->
15d2c 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  pList);.  sqlite
15d2d 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 2d  3SelectDelete(p-
15d2e 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71 6c  >pSelect);.  sql
15d2f 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a  ite3_free(p);.}.
15d30 0a 2f 2a 0a 2a 2a 20 54 68 65 20 45 78 70 72 2e  ./*.** The Expr.
15d31 74 6f 6b 65 6e 20 66 69 65 6c 64 20 6d 69 67 68  token field migh
15d32 74 20 62 65 20 61 20 73 74 72 69 6e 67 20 6c 69  t be a string li
15d33 74 65 72 61 6c 20 74 68 61 74 20 69 73 20 71 75  teral that is qu
15d34 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 73 6f 2c 20  oted..** If so, 
15d35 72 65 6d 6f 76 65 20 74 68 65 20 71 75 6f 74 61  remove the quota
15d36 74 69 6f 6e 20 6d 61 72 6b 73 2e 0a 2a 2f 0a 53  tion marks..*/.S
15d37 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
15d38 69 64 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74  id sqlite3Dequot
15d39 65 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a 64  eExpr(sqlite3 *d
15d3a 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69  b, Expr *p){.  i
15d3b 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  f( ExprHasAnyPro
15d3c 70 65 72 74 79 28 70 2c 20 45 50 5f 44 65 71 75  perty(p, EP_Dequ
15d3d 6f 74 65 64 29 20 29 7b 0a 20 20 20 20 72 65 74  oted) ){.    ret
15d3e 75 72 6e 3b 0a 20 20 7d 0a 20 20 45 78 70 72 53  urn;.  }.  ExprS
15d3f 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  etProperty(p, EP
15d40 5f 44 65 71 75 6f 74 65 64 29 3b 0a 20 20 69 66  _Dequoted);.  if
15d41 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 3d 3d  ( p->token.dyn==
15d42 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
15d43 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70  TokenCopy(db, &p
15d44 2d 3e 74 6f 6b 65 6e 2c 20 26 70 2d 3e 74 6f 6b  ->token, &p->tok
15d45 65 6e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  en);.  }.  sqlit
15d46 65 33 44 65 71 75 6f 74 65 28 28 63 68 61 72 2a  e3Dequote((char*
15d47 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 7d 0a  )p->token.z);.}.
15d48 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
15d49 6f 77 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 72  owing group of r
15d4a 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 65  outines make dee
15d4b 70 20 63 6f 70 69 65 73 20 6f 66 20 65 78 70 72  p copies of expr
15d4c 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72  essions,.** expr
15d4d 65 73 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44  ession lists, ID
15d4e 20 6c 69 73 74 73 2c 20 61 6e 64 20 73 65 6c 65   lists, and sele
15d4f 63 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  ct statements.  
15d50 54 68 65 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a  The copies can.*
15d51 2a 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 79  * be deleted (by
15d52 20 62 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f   being passed to
15d53 20 74 68 65 69 72 20 72 65 73 70 65 63 74 69 76   their respectiv
15d54 65 20 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f  e ...Delete() ro
15d55 75 74 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f  utines).** witho
15d56 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 65  ut effecting the
15d57 20 6f 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a   originals..**.*
15d58 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
15d59 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 73   list, ID, and s
15d5a 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74 75  ource lists retu
15d5b 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78 70  rn by sqlite3Exp
15d5c 72 4c 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73  rListDup(),.** s
15d5d 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28  qlite3IdListDup(
15d5e 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 53 72  ), and sqlite3Sr
15d5f 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e 20 6e  cListDup() can n
15d60 6f 74 20 62 65 20 66 75 72 74 68 65 72 20 65 78  ot be further ex
15d61 70 61 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 75  panded .** by su
15d62 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
15d63 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 70  o sqlite*ListApp
15d64 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a  end() routines..
15d65 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73  **.** Any tables
15d66 20 74 68 61 74 20 74 68 65 20 53 72 63 4c 69 73   that the SrcLis
15d67 74 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f  t might point to
15d68 20 61 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 61   are not duplica
15d69 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
15d6a 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
15d6b 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c 69  ite3ExprDup(sqli
15d6c 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70  te3 *db, Expr *p
15d6d 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b  ){.  Expr *pNew;
15d6e 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
15d6f 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d  turn 0;.  pNew =
15d70 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
15d71 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  Raw(db, sizeof(*
15d72 70 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  p) );.  if( pNew
15d73 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
15d74 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70    memcpy(pNew, p
15d75 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29  , sizeof(*pNew))
15d76 3b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e  ;.  if( p->token
15d77 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65  .z!=0 ){.    pNe
15d78 77 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 28 75 38  w->token.z = (u8
15d79 2a 29 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  *)sqlite3DbStrND
15d7a 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d  up(db, (char*)p-
15d7b 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 2d 3e 74 6f 6b  >token.z, p->tok
15d7c 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 4e 65 77 2d  en.n);.    pNew-
15d7d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 3d 20 31 3b 0a  >token.dyn = 1;.
15d7e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
15d7f 65 72 74 28 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e  ert( pNew->token
15d80 2e 7a 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 70  .z==0 );.  }.  p
15d81 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 30 3b  New->span.z = 0;
15d82 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d  .  pNew->pLeft =
15d83 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
15d84 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20  db, p->pLeft);. 
15d85 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20   pNew->pRight = 
15d86 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
15d87 62 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20  b, p->pRight);. 
15d88 20 70 4e 65 77 2d 3e 70 4c 69 73 74 20 3d 20 73   pNew->pList = s
15d89 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
15d8a 70 28 64 62 2c 20 70 2d 3e 70 4c 69 73 74 29 3b  p(db, p->pList);
15d8b 0a 20 20 70 4e 65 77 2d 3e 70 53 65 6c 65 63 74  .  pNew->pSelect
15d8c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
15d8d 44 75 70 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65  Dup(db, p->pSele
15d8e 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e  ct);.  return pN
15d8f 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  ew;.}.SQLITE_PRI
15d90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
15d91 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74  3TokenCopy(sqlit
15d92 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70  e3 *db, Token *p
15d93 54 6f 2c 20 54 6f 6b 65 6e 20 2a 70 46 72 6f 6d  To, Token *pFrom
15d94 29 7b 0a 20 20 69 66 28 20 70 54 6f 2d 3e 64 79  ){.  if( pTo->dy
15d95 6e 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65  n ) sqlite3_free
15d96 28 28 63 68 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b  ((char*)pTo->z);
15d97 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 20  .  if( pFrom->z 
15d98 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 6e 20 3d 20  ){.    pTo->n = 
15d99 70 46 72 6f 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54  pFrom->n;.    pT
15d9a 6f 2d 3e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69  o->z = (u8*)sqli
15d9b 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
15d9c 20 28 63 68 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a   (char*)pFrom->z
15d9d 2c 20 70 46 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20  , pFrom->n);.   
15d9e 20 70 54 6f 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20   pTo->dyn = 1;. 
15d9f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 2d   }else{.    pTo-
15da0 3e 7a 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51  >z = 0;.  }.}.SQ
15da1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
15da2 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78  rList *sqlite3Ex
15da3 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  prListDup(sqlite
15da4 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20  3 *db, ExprList 
15da5 2a 70 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  *p){.  ExprList 
15da6 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75 63 74 20  *pNew;.  struct 
15da7 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
15da8 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74 65 6d 3b  Item, *pOldItem;
15da9 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
15daa 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
15dab 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
15dac 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
15dad 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29   sizeof(*pNew) )
15dae 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
15daf 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
15db0 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 30  ew->iECursor = 0
15db1 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72 20  ;.  pNew->nExpr 
15db2 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d  = pNew->nAlloc =
15db3 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 4e 65   p->nExpr;.  pNe
15db4 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20 73  w->a = pItem = s
15db5 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
15db6 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 70 72 2a  w(db,  p->nExpr*
15db7 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20  sizeof(p->a[0]) 
15db8 29 3b 0a 20 20 69 66 28 20 70 49 74 65 6d 3d 3d  );.  if( pItem==
15db9 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
15dba 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20  _free(pNew);.   
15dbb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a   return 0;.  } .
15dbc 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e    pOldItem = p->
15dbd 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  a;.  for(i=0; i<
15dbe 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  p->nExpr; i++, p
15dbf 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d  Item++, pOldItem
15dc0 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
15dc1 4e 65 77 45 78 70 72 2c 20 2a 70 4f 6c 64 45 78  NewExpr, *pOldEx
15dc2 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70  pr;.    pItem->p
15dc3 45 78 70 72 20 3d 20 70 4e 65 77 45 78 70 72 20  Expr = pNewExpr 
15dc4 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
15dc5 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 20 3d 20  (db, pOldExpr = 
15dc6 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 29  pOldItem->pExpr)
15dc7 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 45 78  ;.    if( pOldEx
15dc8 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 26 26  pr->span.z!=0 &&
15dc9 20 70 4e 65 77 45 78 70 72 20 29 7b 0a 20 20 20   pNewExpr ){.   
15dca 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 6d 61 6b     /* Always mak
15dcb 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  e a copy of the 
15dcc 73 70 61 6e 20 66 6f 72 20 74 6f 70 2d 6c 65 76  span for top-lev
15dcd 65 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  el expressions i
15dce 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 65  n the.      ** e
15dcf 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20  xpression list. 
15dd0 20 54 68 65 20 6c 6f 67 69 63 20 69 6e 20 53 45   The logic in SE
15dd1 4c 45 43 54 20 70 72 6f 63 65 73 73 69 6e 67 20  LECT processing 
15dd2 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a  that determines.
15dd3 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 61 6d        ** the nam
15dd4 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  es of columns in
15dd5 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
15dd6 6e 65 65 64 73 20 74 68 69 73 20 69 6e 66 6f 72  needs this infor
15dd7 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  mation */.      
15dd8 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79  sqlite3TokenCopy
15dd9 28 64 62 2c 20 26 70 4e 65 77 45 78 70 72 2d 3e  (db, &pNewExpr->
15dda 73 70 61 6e 2c 20 26 70 4f 6c 64 45 78 70 72 2d  span, &pOldExpr-
15ddb 3e 73 70 61 6e 29 3b 0a 20 20 20 20 7d 0a 20 20  >span);.    }.  
15ddc 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 45 78    assert( pNewEx
15ddd 70 72 3d 3d 30 20 7c 7c 20 70 4e 65 77 45 78 70  pr==0 || pNewExp
15dde 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 0a 20 20  r->span.z!=0 .  
15ddf 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4f 6c            || pOl
15de0 64 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 3d 3d 30  dExpr->span.z==0
15de1 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
15de2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
15de3 20 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a   );.    pItem->z
15de4 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Name = sqlite3Db
15de5 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  StrDup(db, pOldI
15de6 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  tem->zName);.   
15de7 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65   pItem->sortOrde
15de8 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 6f  r = pOldItem->so
15de9 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 70 49 74  rtOrder;.    pIt
15dea 65 6d 2d 3e 69 73 41 67 67 20 3d 20 70 4f 6c 64  em->isAgg = pOld
15deb 49 74 65 6d 2d 3e 69 73 41 67 67 3b 0a 20 20 20  Item->isAgg;.   
15dec 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30   pItem->done = 0
15ded 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
15dee 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  New;.}../*.** If
15def 20 63 75 72 73 6f 72 73 2c 20 74 72 69 67 67 65   cursors, trigge
15df0 72 73 2c 20 76 69 65 77 73 20 61 6e 64 20 73 75  rs, views and su
15df1 62 71 75 65 72 69 65 73 20 61 72 65 20 61 6c 6c  bqueries are all
15df2 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a 2a   omitted from.**
15df3 20 74 68 65 20 62 75 69 6c 64 2c 20 74 68 65 6e   the build, then
15df4 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c   none of the fol
15df5 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 2c  lowing routines,
15df6 20 65 78 63 65 70 74 20 66 6f 72 20 0a 2a 2a 20   except for .** 
15df7 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
15df8 28 29 2c 20 63 61 6e 20 62 65 20 63 61 6c 6c 65  (), can be calle
15df9 64 2e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  d. sqlite3Select
15dfa 44 75 70 28 29 20 69 73 20 73 6f 6d 65 74 69 6d  Dup() is sometim
15dfb 65 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69 74  es.** called wit
15dfc 68 20 61 20 4e 55 4c 4c 20 61 72 67 75 6d 65 6e  h a NULL argumen
15dfd 74 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  t..*/.#if !defin
15dfe 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
15dff 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  IEW) || !defined
15e00 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
15e01 47 47 45 52 29 20 5c 0a 20 7c 7c 20 21 64 65 66  GGER) \. || !def
15e02 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
15e03 5f 53 55 42 51 55 45 52 59 29 0a 53 51 4c 49 54  _SUBQUERY).SQLIT
15e04 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73  E_PRIVATE SrcLis
15e05 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  t *sqlite3SrcLis
15e06 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  tDup(sqlite3 *db
15e07 2c 20 53 72 63 4c 69 73 74 20 2a 70 29 7b 0a 20  , SrcList *p){. 
15e08 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a   SrcList *pNew;.
15e09 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
15e0a 42 79 74 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30  Byte;.  if( p==0
15e0b 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e   ) return 0;.  n
15e0c 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70  Byte = sizeof(*p
15e0d 29 20 2b 20 28 70 2d 3e 6e 53 72 63 3e 30 20 3f  ) + (p->nSrc>0 ?
15e0e 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29   sizeof(p->a[0])
15e0f 20 2a 20 28 70 2d 3e 6e 53 72 63 2d 31 29 20 3a   * (p->nSrc-1) :
15e10 20 30 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71   0);.  pNew = sq
15e11 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
15e12 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20  (db, nByte );.  
15e13 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65  if( pNew==0 ) re
15e14 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  turn 0;.  pNew->
15e15 6e 53 72 63 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c  nSrc = pNew->nAl
15e16 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20  loc = p->nSrc;. 
15e17 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
15e18 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  Src; i++){.    s
15e19 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
15e1a 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26  em *pNewItem = &
15e1b 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  pNew->a[i];.    
15e1c 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
15e1d 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20  tem *pOldItem = 
15e1e 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 54 61  &p->a[i];.    Ta
15e1f 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 70  ble *pTab;.    p
15e20 4e 65 77 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  NewItem->zDataba
15e21 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  se = sqlite3DbSt
15e22 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
15e23 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  m->zDatabase);. 
15e24 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61     pNewItem->zNa
15e25 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
15e26 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
15e27 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70  m->zName);.    p
15e28 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20  NewItem->zAlias 
15e29 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
15e2a 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
15e2b 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 70 4e 65  zAlias);.    pNe
15e2c 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20  wItem->jointype 
15e2d 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e  = pOldItem->join
15e2e 74 79 70 65 3b 0a 20 20 20 20 70 4e 65 77 49 74  type;.    pNewIt
15e2f 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f  em->iCursor = pO
15e30 6c 64 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b  ldItem->iCursor;
15e31 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69  .    pNewItem->i
15e32 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 70 4f 6c  sPopulated = pOl
15e33 64 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74  dItem->isPopulat
15e34 65 64 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70  ed;.    pTab = p
15e35 4e 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20  NewItem->pTab = 
15e36 70 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a  pOldItem->pTab;.
15e37 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a      if( pTab ){.
15e38 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66        pTab->nRef
15e39 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e  ++;.    }.    pN
15e3a 65 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20  ewItem->pSelect 
15e3b 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
15e3c 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
15e3d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 70  >pSelect);.    p
15e3e 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73  NewItem->pOn = s
15e3f 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
15e40 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e 29  , pOldItem->pOn)
15e41 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
15e42 70 55 73 69 6e 67 20 3d 20 73 71 6c 69 74 65 33  pUsing = sqlite3
15e43 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 4f  IdListDup(db, pO
15e44 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b  ldItem->pUsing);
15e45 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63  .    pNewItem->c
15e46 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64 49 74 65  olUsed = pOldIte
15e47 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a  m->colUsed;.  }.
15e48 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
15e49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15e4a 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49  IdList *sqlite3I
15e4b 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33  dListDup(sqlite3
15e4c 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 29   *db, IdList *p)
15e4d 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70 4e 65 77  {.  IdList *pNew
15e4e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  ;.  int i;.  if(
15e4f 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
15e50 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
15e51 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
15e52 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20  , sizeof(*pNew) 
15e53 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
15e54 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
15e55 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d  New->nId = pNew-
15e56 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64  >nAlloc = p->nId
15e57 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71  ;.  pNew->a = sq
15e58 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
15e59 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65  (db, p->nId*size
15e5a 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20  of(p->a[0]) );. 
15e5b 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20   if( pNew->a==0 
15e5c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
15e5d 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 72  ree(pNew);.    r
15e5e 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66  eturn 0;.  }.  f
15e5f 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64  or(i=0; i<p->nId
15e60 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
15e61 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a  ct IdList_item *
15e62 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77  pNewItem = &pNew
15e63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75  ->a[i];.    stru
15e64 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a  ct IdList_item *
15e65 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61  pOldItem = &p->a
15e66 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  [i];.    pNewIte
15e67 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  m->zName = sqlit
15e68 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
15e69 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  OldItem->zName);
15e6a 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69  .    pNewItem->i
15e6b 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69  dx = pOldItem->i
15e6c 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dx;.  }.  return
15e6d 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f   pNew;.}.SQLITE_
15e6e 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a  PRIVATE Select *
15e6f 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
15e70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65  (sqlite3 *db, Se
15e71 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 65 6c 65  lect *p){.  Sele
15e72 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20  ct *pNew;.  if( 
15e73 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
15e74 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
15e75 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
15e76 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20   sizeof(*p) );. 
15e77 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72   if( pNew==0 ) r
15e78 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d  eturn 0;.  pNew-
15e79 3e 69 73 44 69 73 74 69 6e 63 74 20 3d 20 70 2d  >isDistinct = p-
15e7a 3e 69 73 44 69 73 74 69 6e 63 74 3b 0a 20 20 70  >isDistinct;.  p
15e7b 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71  New->pEList = sq
15e7c 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
15e7d 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b  (db, p->pEList);
15e7e 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20  .  pNew->pSrc = 
15e7f 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
15e80 70 28 64 62 2c 20 70 2d 3e 70 53 72 63 29 3b 0a  p(db, p->pSrc);.
15e81 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d    pNew->pWhere =
15e82 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
15e83 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a  db, p->pWhere);.
15e84 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79    pNew->pGroupBy
15e85 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
15e86 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72  stDup(db, p->pGr
15e87 6f 75 70 42 79 29 3b 0a 20 20 70 4e 65 77 2d 3e  oupBy);.  pNew->
15e88 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65  pHaving = sqlite
15e89 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e  3ExprDup(db, p->
15e8a 70 48 61 76 69 6e 67 29 3b 0a 20 20 70 4e 65 77  pHaving);.  pNew
15e8b 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c  ->pOrderBy = sql
15e8c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
15e8d 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29  db, p->pOrderBy)
15e8e 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70  ;.  pNew->op = p
15e8f 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50  ->op;.  pNew->pP
15e90 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65  rior = sqlite3Se
15e91 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  lectDup(db, p->p
15e92 50 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 2d 3e  Prior);.  pNew->
15e93 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33  pLimit = sqlite3
15e94 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70  ExprDup(db, p->p
15e95 4c 69 6d 69 74 29 3b 0a 20 20 70 4e 65 77 2d 3e  Limit);.  pNew->
15e96 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65  pOffset = sqlite
15e97 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e  3ExprDup(db, p->
15e98 70 4f 66 66 73 65 74 29 3b 0a 20 20 70 4e 65 77  pOffset);.  pNew
15e99 2d 3e 69 4c 69 6d 69 74 20 3d 20 2d 31 3b 0a 20  ->iLimit = -1;. 
15e9a 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d   pNew->iOffset =
15e9b 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 69 73 52   -1;.  pNew->isR
15e9c 65 73 6f 6c 76 65 64 20 3d 20 70 2d 3e 69 73 52  esolved = p->isR
15e9d 65 73 6f 6c 76 65 64 3b 0a 20 20 70 4e 65 77 2d  esolved;.  pNew-
15e9e 3e 69 73 41 67 67 20 3d 20 70 2d 3e 69 73 41 67  >isAgg = p->isAg
15e9f 67 3b 0a 20 20 70 4e 65 77 2d 3e 75 73 65 73 45  g;.  pNew->usesE
15ea0 70 68 6d 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  phm = 0;.  pNew-
15ea1 3e 64 69 73 61 6c 6c 6f 77 4f 72 64 65 72 42 79  >disallowOrderBy
15ea2 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 70 52   = 0;.  pNew->pR
15ea3 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20  ightmost = 0;.  
15ea4 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70  pNew->addrOpenEp
15ea5 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e  hm[0] = -1;.  pN
15ea6 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d  ew->addrOpenEphm
15ea7 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77  [1] = -1;.  pNew
15ea8 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32  ->addrOpenEphm[2
15ea9 5d 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e  ] = -1;.  return
15eaa 20 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53   pNew;.}.#else.S
15eab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65  QLITE_PRIVATE Se
15eac 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c  lect *sqlite3Sel
15ead 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a  ectDup(sqlite3 *
15eae 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a  db, Select *p){.
15eaf 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29    assert( p==0 )
15eb0 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
15eb1 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41  #endif.../*.** A
15eb2 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74  dd a new element
15eb3 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61   to the end of a
15eb4 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  n expression lis
15eb5 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a  t.  If pList is.
15eb6 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c  ** initially NUL
15eb7 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  L, then create a
15eb8 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20   new expression 
15eb9 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  list..*/.SQLITE_
15eba 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74  PRIVATE ExprList
15ebb 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73   *sqlite3ExprLis
15ebc 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65  tAppend(.  Parse
15ebd 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
15ebe 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
15ebf 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
15ec0 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
15ec1 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68     /* List to wh
15ec2 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d  ich to append. M
15ec3 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  ight be NULL */.
15ec4 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20    Expr *pExpr,  
15ec5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
15ec6 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70  ression to be ap
15ec7 70 65 6e 64 65 64 20 2a 2f 0a 20 20 54 6f 6b 65  pended */.  Toke
15ec8 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20  n *pName        
15ec9 20 20 20 20 2f 2a 20 41 53 20 6b 65 79 77 6f 72      /* AS keywor
15eca 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73  d for the expres
15ecb 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  sion */.){.  sql
15ecc 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
15ecd 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69  e->db;.  if( pLi
15ece 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69  st==0 ){.    pLi
15ecf 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  st = sqlite3DbMa
15ed0 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
15ed1 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b  eof(ExprList) );
15ed2 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  .    if( pList==
15ed3 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
15ed4 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20  no_mem;.    }.  
15ed5 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
15ed6 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20  >nAlloc==0 );.  
15ed7 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e  }.  if( pList->n
15ed8 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45  Alloc<=pList->nE
15ed9 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63  xpr ){.    struc
15eda 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
15edb 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  *a;.    int n = 
15edc 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20  pList->nAlloc*2 
15edd 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c  + 4;.    a = sql
15ede 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
15edf 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69  , pList->a, n*si
15ee0 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d  zeof(pList->a[0]
15ee1 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30  ));.    if( a==0
15ee2 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e   ){.      goto n
15ee3 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  o_mem;.    }.   
15ee4 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20   pList->a = a;. 
15ee5 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63     pList->nAlloc
15ee6 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65   = n;.  }.  asse
15ee7 72 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20  rt( pList->a!=0 
15ee8 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 7c  );.  if( pExpr |
15ee9 7c 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 73  | pName ){.    s
15eea 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
15eeb 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c  tem *pItem = &pL
15eec 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45  ist->a[pList->nE
15eed 78 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d 73  xpr++];.    mems
15eee 65 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 7a  et(pItem, 0, siz
15eef 65 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20 20  eof(*pItem));.  
15ef0 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d    pItem->zName =
15ef1 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
15ef2 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29  Token(db, pName)
15ef3 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78  ;.    pItem->pEx
15ef4 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 7d 0a  pr = pExpr;.  }.
15ef5 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a    return pList;.
15ef6 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 20  .no_mem:     .  
15ef7 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b 69 6e 67  /* Avoid leaking
15ef8 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61 6c 6c 6f   memory if mallo
15ef9 63 20 68 61 73 20 66 61 69 6c 65 64 2e 20 2a 2f  c has failed. */
15efa 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
15efb 6c 65 74 65 28 70 45 78 70 72 29 3b 0a 20 20 73  lete(pExpr);.  s
15efc 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
15efd 6c 65 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 72  lete(pList);.  r
15efe 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
15eff 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
15f00 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20  ion list pEList 
15f01 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68  contains more th
15f02 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e  an iLimit elemen
15f03 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20  ts,.** leave an 
15f04 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
15f05 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49   pParse..*/.SQLI
15f06 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
15f07 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43  sqlite3ExprListC
15f08 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61  heckLength(.  Pa
15f09 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45  rse *pParse,.  E
15f0a 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
15f0b 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 0a 20  .  int iLimit,. 
15f0c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62   const char *zOb
15f0d 6a 65 63 74 0a 29 7b 0a 20 20 69 66 28 20 70 45  ject.){.  if( pE
15f0e 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e  List && pEList->
15f0f 6e 45 78 70 72 3e 69 4c 69 6d 69 74 20 29 7b 0a  nExpr>iLimit ){.
15f10 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
15f11 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
15f12 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e   many columns in
15f13 20 25 73 22 2c 20 7a 4f 62 6a 65 63 74 29 3b 0a   %s", zObject);.
15f14 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69    }.}...#if defi
15f15 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
15f16 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45   || SQLITE_MAX_E
15f17 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 20 54  XPR_DEPTH>0./* T
15f18 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72  he following thr
15f19 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68 65  ee functions, he
15f1a 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 68 65  ightOfExpr(), he
15f1b 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 29  ightOfExprList()
15f1c 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 4f 66  .** and heightOf
15f1d 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 75 73  Select(), are us
15f1e 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
15f1f 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  the maximum heig
15f20 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70  ht.** of any exp
15f21 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66  ression tree ref
15f22 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20 73  erenced by the s
15f23 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20  tructure passed 
15f24 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  as the.** first 
15f25 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
15f26 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d 20  If this maximum 
15f27 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65  height is greate
15f28 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65  r than the curre
15f29 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  nt value pointed
15f2a 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69 67  .** to by pnHeig
15f2b 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 70  ht, the second p
15f2c 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 73  arameter, then s
15f2d 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f 20  et *pnHeight to 
15f2e 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a  that.** value..*
15f2f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65  /.static void he
15f30 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 72 20  ightOfExpr(Expr 
15f31 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68  *p, int *pnHeigh
15f32 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  t){.  if( p ){. 
15f33 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 67 68     if( p->nHeigh
15f34 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a 20  t>*pnHeight ){. 
15f35 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 20 3d       *pnHeight =
15f36 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 20   p->nHeight;.   
15f37 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20   }.  }.}.static 
15f38 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78 70  void heightOfExp
15f39 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a  rList(ExprList *
15f3a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74  p, int *pnHeight
15f3b 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
15f3c 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
15f3d 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72  (i=0; i<p->nExpr
15f3e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68 65  ; i++){.      he
15f3f 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b  ightOfExpr(p->a[
15f40 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 67  i].pExpr, pnHeig
15f41 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ht);.    }.  }.}
15f42 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69  .static void hei
15f43 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 6c 65  ghtOfSelect(Sele
15f44 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65  ct *p, int *pnHe
15f45 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29  ight){.  if( p )
15f46 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  {.    heightOfEx
15f47 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 70 6e  pr(p->pWhere, pn
15f48 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
15f49 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 48 61  ghtOfExpr(p->pHa
15f4a 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 29 3b  ving, pnHeight);
15f4b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
15f4c 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e 48  r(p->pLimit, pnH
15f4d 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
15f4e 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66 66  htOfExpr(p->pOff
15f4f 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a  set, pnHeight);.
15f50 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72      heightOfExpr
15f51 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c 20  List(p->pEList, 
15f52 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
15f53 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28  eightOfExprList(
15f54 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e 48  p->pGroupBy, pnH
15f55 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
15f56 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e  htOfExprList(p->
15f57 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 69 67  pOrderBy, pnHeig
15f58 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
15f59 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 69 6f  fSelect(p->pPrio
15f5a 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  r, pnHeight);.  
15f5b 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
15f5c 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20  he Expr.nHeight 
15f5d 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65 20  variable in the 
15f5e 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64  structure passed
15f5f 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75 6d   as an .** argum
15f60 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 73 69  ent. An expressi
15f61 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 6c 64  on with no child
15f62 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74 20  ren, Expr.pList 
15f63 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65 6c  or .** Expr.pSel
15f64 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 20 61  ect member has a
15f65 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 41 6e   height of 1. An
15f66 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69  y other expressi
15f67 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 69 67  on.** has a heig
15f68 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  ht equal to the 
15f69 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f  maximum height o
15f6a 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a 20  f any other .** 
15f6b 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 72 20  referenced Expr 
15f6c 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  plus one..*/.SQL
15f6d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
15f6e 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
15f6f 65 69 67 68 74 28 45 78 70 72 20 2a 70 29 7b 0a  eight(Expr *p){.
15f70 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20    int nHeight = 
15f71 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70  0;.  heightOfExp
15f72 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65  r(p->pLeft, &nHe
15f73 69 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f  ight);.  heightO
15f74 66 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74 2c  fExpr(p->pRight,
15f75 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65   &nHeight);.  he
15f76 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70  ightOfExprList(p
15f77 2d 3e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68  ->pList, &nHeigh
15f78 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65  t);.  heightOfSe
15f79 6c 65 63 74 28 70 2d 3e 70 53 65 6c 65 63 74 2c  lect(p->pSelect,
15f7a 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 70 2d   &nHeight);.  p-
15f7b 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 69 67  >nHeight = nHeig
15f7c 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ht + 1;.}../*.**
15f7d 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69   Return the maxi
15f7e 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e  mum height of an
15f7f 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  y expression tre
15f80 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20  e referenced.** 
15f81 62 79 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  by the select st
15f82 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61  atement passed a
15f83 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  s an argument..*
15f84 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15f85 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65   int sqlite3Sele
15f86 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65 6c  ctExprHeight(Sel
15f87 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  ect *p){.  int n
15f88 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65  Height = 0;.  he
15f89 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20  ightOfSelect(p, 
15f8a 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74  &nHeight);.  ret
15f8b 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 23  urn nHeight;.}.#
15f8c 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  endif../*.** Del
15f8d 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 65 78  ete an entire ex
15f8e 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a  pression list..*
15f8f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15f90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
15f91 72 4c 69 73 74 44 65 6c 65 74 65 28 45 78 70 72  rListDelete(Expr
15f92 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
15f93 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
15f94 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
15f95 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73  Item;.  if( pLis
15f96 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
15f97 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e   assert( pList->
15f98 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d 3e  a!=0 || (pList->
15f99 6e 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 73  nExpr==0 && pLis
15f9a 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b  t->nAlloc==0) );
15f9b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
15f9c 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d 3e  ->nExpr<=pList->
15f9d 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 28  nAlloc );.  for(
15f9e 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20  pItem=pList->a, 
15f9f 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
15fa0 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
15fa1 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  +){.    sqlite3E
15fa2 78 70 72 44 65 6c 65 74 65 28 70 49 74 65 6d 2d  xprDelete(pItem-
15fa3 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 73 71 6c  >pExpr);.    sql
15fa4 69 74 65 33 5f 66 72 65 65 28 70 49 74 65 6d 2d  ite3_free(pItem-
15fa5 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73  >zName);.  }.  s
15fa6 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 69 73  qlite3_free(pLis
15fa7 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33  t->a);.  sqlite3
15fa8 5f 66 72 65 65 28 70 4c 69 73 74 29 3b 0a 7d 0a  _free(pList);.}.
15fa9 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
15faa 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
15fab 20 43 61 6c 6c 20 78 46 75 6e 63 20 66 6f 72 20   Call xFunc for 
15fac 65 61 63 68 20 6e 6f 64 65 20 76 69 73 69 74 65  each node visite
15fad 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  d..**.** The ret
15fae 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 78  urn value from x
15faf 46 75 6e 63 20 64 65 74 65 72 6d 69 6e 65 73 20  Func determines 
15fb0 77 68 65 74 68 65 72 20 74 68 65 20 74 72 65 65  whether the tree
15fb1 20 77 61 6c 6b 20 63 6f 6e 74 69 6e 75 65 73 2e   walk continues.
15fb2 0a 2a 2a 20 30 20 6d 65 61 6e 73 20 63 6f 6e 74  .** 0 means cont
15fb3 69 6e 75 65 20 77 61 6c 6b 69 6e 67 20 74 68 65  inue walking the
15fb4 20 74 72 65 65 2e 20 20 31 20 6d 65 61 6e 73 20   tree.  1 means 
15fb5 64 6f 20 6e 6f 74 20 77 61 6c 6b 20 63 68 69 6c  do not walk chil
15fb6 64 72 65 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 63  dren.** of the c
15fb7 75 72 72 65 6e 74 20 6e 6f 64 65 20 62 75 74 20  urrent node but 
15fb8 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 73 69  continue with si
15fb9 62 6c 69 6e 67 73 2e 20 20 32 20 6d 65 61 6e 73  blings.  2 means
15fba 20 61 62 61 6e 64 6f 6e 0a 2a 2a 20 74 68 65 20   abandon.** the 
15fbb 74 72 65 65 20 77 61 6c 6b 20 63 6f 6d 70 6c 65  tree walk comple
15fbc 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tely..**.** The 
15fbd 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  return value fro
15fbe 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  m this routine i
15fbf 73 20 31 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74  s 1 to abandon t
15fc0 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a 2a 20  he tree walk.** 
15fc1 61 6e 64 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75  and 0 to continu
15fc2 65 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 49 43 45 3a  e..**.** NOTICE:
15fc3 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64    This routine d
15fc4 6f 65 73 20 2a 6e 6f 74 2a 20 64 65 73 63 65 6e  oes *not* descen
15fc5 64 20 69 6e 74 6f 20 73 75 62 71 75 65 72 69 65  d into subquerie
15fc6 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
15fc7 20 77 61 6c 6b 45 78 70 72 4c 69 73 74 28 45 78   walkExprList(Ex
15fc8 70 72 4c 69 73 74 20 2a 2c 20 69 6e 74 20 28 2a  prList *, int (*
15fc9 29 28 76 6f 69 64 20 2a 2c 20 45 78 70 72 2a 29  )(void *, Expr*)
15fca 2c 20 76 6f 69 64 20 2a 29 3b 0a 73 74 61 74 69  , void *);.stati
15fcb 63 20 69 6e 74 20 77 61 6c 6b 45 78 70 72 54 72  c int walkExprTr
15fcc 65 65 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ee(Expr *pExpr, 
15fcd 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f 69  int (*xFunc)(voi
15fce 64 2a 2c 45 78 70 72 2a 29 2c 20 76 6f 69 64 20  d*,Expr*), void 
15fcf 2a 70 41 72 67 29 7b 0a 20 20 69 6e 74 20 72 63  *pArg){.  int rc
15fd0 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  ;.  if( pExpr==0
15fd1 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72   ) return 0;.  r
15fd2 63 20 3d 20 28 2a 78 46 75 6e 63 29 28 70 41 72  c = (*xFunc)(pAr
15fd3 67 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28  g, pExpr);.  if(
15fd4 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66   rc==0 ){.    if
15fd5 28 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28 70  ( walkExprTree(p
15fd6 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 78 46 75  Expr->pLeft, xFu
15fd7 6e 63 2c 20 70 41 72 67 29 20 29 20 72 65 74 75  nc, pArg) ) retu
15fd8 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 77 61  rn 1;.    if( wa
15fd9 6c 6b 45 78 70 72 54 72 65 65 28 70 45 78 70 72  lkExprTree(pExpr
15fda 2d 3e 70 52 69 67 68 74 2c 20 78 46 75 6e 63 2c  ->pRight, xFunc,
15fdb 20 70 41 72 67 29 20 29 20 72 65 74 75 72 6e 20   pArg) ) return 
15fdc 31 3b 0a 20 20 20 20 69 66 28 20 77 61 6c 6b 45  1;.    if( walkE
15fdd 78 70 72 4c 69 73 74 28 70 45 78 70 72 2d 3e 70  xprList(pExpr->p
15fde 4c 69 73 74 2c 20 78 46 75 6e 63 2c 20 70 41 72  List, xFunc, pAr
15fdf 67 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  g) ) return 1;. 
15fe0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3e 31   }.  return rc>1
15fe1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
15fe2 77 61 6c 6b 45 78 70 72 54 72 65 65 28 29 20 66  walkExprTree() f
15fe3 6f 72 20 65 76 65 72 79 20 65 78 70 72 65 73 73  or every express
15fe4 69 6f 6e 20 69 6e 20 6c 69 73 74 20 70 2e 0a 2a  ion in list p..*
15fe5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c  /.static int wal
15fe6 6b 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69  kExprList(ExprLi
15fe7 73 74 20 2a 70 2c 20 69 6e 74 20 28 2a 78 46 75  st *p, int (*xFu
15fe8 6e 63 29 28 76 6f 69 64 20 2a 2c 20 45 78 70 72  nc)(void *, Expr
15fe9 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  *), void *pArg){
15fea 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
15feb 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
15fec 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 21   *pItem;.  if( !
15fed 70 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  p ) return 0;.  
15fee 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20  for(i=p->nExpr, 
15fef 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b  pItem=p->a; i>0;
15ff0 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i--, pItem++){.
15ff1 20 20 20 20 69 66 28 20 77 61 6c 6b 45 78 70 72      if( walkExpr
15ff2 54 72 65 65 28 70 49 74 65 6d 2d 3e 70 45 78 70  Tree(pItem->pExp
15ff3 72 2c 20 78 46 75 6e 63 2c 20 70 41 72 67 29 20  r, xFunc, pArg) 
15ff4 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a  ) return 1;.  }.
15ff5 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
15ff6 2a 0a 2a 2a 20 43 61 6c 6c 20 77 61 6c 6b 45 78  *.** Call walkEx
15ff7 70 72 54 72 65 65 28 29 20 66 6f 72 20 65 76 65  prTree() for eve
15ff8 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ry expression in
15ff9 20 53 65 6c 65 63 74 20 70 2c 20 6e 6f 74 20 69   Select p, not i
15ffa 6e 63 6c 75 64 69 6e 67 0a 2a 2a 20 65 78 70 72  ncluding.** expr
15ffb 65 73 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65  essions that are
15ffc 20 70 61 72 74 20 6f 66 20 73 75 62 2d 73 65 6c   part of sub-sel
15ffd 65 63 74 73 20 69 6e 20 61 6e 79 20 46 52 4f 4d  ects in any FROM
15ffe 20 63 6c 61 75 73 65 20 6f 72 20 74 68 65 20 4c   clause or the L
15fff 49 4d 49 54 0a 2a 2a 20 6f 72 20 4f 46 46 53 45  IMIT.** or OFFSE
16000 54 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 2e 0a  T expressions...
16001 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61  */.static int wa
16002 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 53 65 6c  lkSelectExpr(Sel
16003 65 63 74 20 2a 70 2c 20 69 6e 74 20 28 2a 78 46  ect *p, int (*xF
16004 75 6e 63 29 28 76 6f 69 64 20 2a 2c 20 45 78 70  unc)(void *, Exp
16005 72 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72 67 29  r*), void *pArg)
16006 7b 0a 20 20 77 61 6c 6b 45 78 70 72 4c 69 73 74  {.  walkExprList
16007 28 70 2d 3e 70 45 4c 69 73 74 2c 20 78 46 75 6e  (p->pEList, xFun
16008 63 2c 20 70 41 72 67 29 3b 0a 20 20 77 61 6c 6b  c, pArg);.  walk
16009 45 78 70 72 54 72 65 65 28 70 2d 3e 70 57 68 65  ExprTree(p->pWhe
1600a 72 65 2c 20 78 46 75 6e 63 2c 20 70 41 72 67 29  re, xFunc, pArg)
1600b 3b 0a 20 20 77 61 6c 6b 45 78 70 72 4c 69 73 74  ;.  walkExprList
1600c 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 78 46  (p->pGroupBy, xF
1600d 75 6e 63 2c 20 70 41 72 67 29 3b 0a 20 20 77 61  unc, pArg);.  wa
1600e 6c 6b 45 78 70 72 54 72 65 65 28 70 2d 3e 70 48  lkExprTree(p->pH
1600f 61 76 69 6e 67 2c 20 78 46 75 6e 63 2c 20 70 41  aving, xFunc, pA
16010 72 67 29 3b 0a 20 20 77 61 6c 6b 45 78 70 72 4c  rg);.  walkExprL
16011 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 79 2c  ist(p->pOrderBy,
16012 20 78 46 75 6e 63 2c 20 70 41 72 67 29 3b 0a 20   xFunc, pArg);. 
16013 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29   if( p->pPrior )
16014 7b 0a 20 20 20 20 77 61 6c 6b 53 65 6c 65 63 74  {.    walkSelect
16015 45 78 70 72 28 70 2d 3e 70 50 72 69 6f 72 2c 20  Expr(p->pPrior, 
16016 78 46 75 6e 63 2c 20 70 41 72 67 29 3b 0a 20 20  xFunc, pArg);.  
16017 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
16018 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
16019 74 69 6e 65 20 69 73 20 64 65 73 69 67 6e 65 64  tine is designed
1601a 20 61 73 20 61 6e 20 78 46 75 6e 63 20 66 6f 72   as an xFunc for
1601b 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28 29 2e   walkExprTree().
1601c 0a 2a 2a 0a 2a 2a 20 70 41 72 67 20 69 73 20 72  .**.** pArg is r
1601d 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20  eally a pointer 
1601e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  to an integer.  
1601f 49 66 20 77 65 20 63 61 6e 20 74 65 6c 6c 20 62  If we can tell b
16020 79 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 61 74 20  y looking.** at 
16021 70 45 78 70 72 20 74 68 61 74 20 74 68 65 20 65  pExpr that the e
16022 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63  xpression that c
16023 6f 6e 74 61 69 6e 73 20 70 45 78 70 72 20 69 73  ontains pExpr is
16024 20 6e 6f 74 20 61 20 63 6f 6e 73 74 61 6e 74 0a   not a constant.
16025 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74  ** expression, t
16026 68 65 6e 20 73 65 74 20 2a 70 41 72 67 20 74 6f  hen set *pArg to
16027 20 30 20 61 6e 64 20 72 65 74 75 72 6e 20 32 20   0 and return 2 
16028 74 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 74  to abandon the t
16029 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 20 49 66 20  ree walk..** If 
1602a 70 45 78 70 72 20 64 6f 65 73 20 64 6f 65 73 20  pExpr does does 
1602b 6e 6f 74 20 64 69 73 71 75 61 6c 69 66 79 20 74  not disqualify t
1602c 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 66 72  he expression fr
1602d 6f 6d 20 62 65 69 6e 67 20 61 20 63 6f 6e 73 74  om being a const
1602e 61 6e 74 0a 2a 2a 20 74 68 65 6e 20 64 6f 20 6e  ant.** then do n
1602f 6f 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 66  othing..**.** Af
16030 74 65 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20  ter walking the 
16031 77 68 6f 6c 65 20 74 72 65 65 2c 20 69 66 20 6e  whole tree, if n
16032 6f 20 6e 6f 64 65 73 20 61 72 65 20 66 6f 75 6e  o nodes are foun
16033 64 20 74 68 61 74 20 64 69 73 71 75 61 6c 69 66  d that disqualif
16034 79 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73  y.** the express
16035 69 6f 6e 20 61 73 20 63 6f 6e 73 74 61 6e 74 2c  ion as constant,
16036 20 74 68 65 6e 20 77 65 20 61 73 73 75 6d 65 20   then we assume 
16037 74 68 65 20 77 68 6f 6c 65 20 65 78 70 72 65 73  the whole expres
16038 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6e 73 74  sion.** is const
16039 61 6e 74 2e 20 20 53 65 65 20 73 71 6c 69 74 65  ant.  See sqlite
1603a 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28  3ExprIsConstant(
1603b 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ) for additional
1603c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
1603d 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72  .static int expr
1603e 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 76  NodeIsConstant(v
1603f 6f 69 64 20 2a 70 41 72 67 2c 20 45 78 70 72 20  oid *pArg, Expr 
16040 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 2a  *pExpr){.  int *
16041 70 4e 20 3d 20 28 69 6e 74 2a 29 70 41 72 67 3b  pN = (int*)pArg;
16042 0a 0a 20 20 2f 2a 20 49 66 20 2a 70 41 72 67 20  ..  /* If *pArg 
16043 69 73 20 33 20 74 68 65 6e 20 61 6e 79 20 74 65  is 3 then any te
16044 72 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  rm of the expres
16045 73 69 6f 6e 20 74 68 61 74 20 63 6f 6d 65 73 20  sion that comes 
16046 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e  from.  ** the ON
16047 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
16048 73 20 6f 66 20 61 20 6a 6f 69 6e 20 64 69 73 71  s of a join disq
16049 75 61 6c 69 66 69 65 73 20 74 68 65 20 65 78 70  ualifies the exp
1604a 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f  ression.  ** fro
1604b 6d 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72  m being consider
1604c 65 64 20 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a  ed constant. */.
1604d 20 20 69 66 28 20 28 2a 70 4e 29 3d 3d 33 20 26    if( (*pN)==3 &
1604e 26 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70  & ExprHasAnyProp
1604f 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46  erty(pExpr, EP_F
16050 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20  romJoin) ){.    
16051 2a 70 4e 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  *pN = 0;.    ret
16052 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 73 77  urn 2;.  }..  sw
16053 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
16054 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69 64  ){.    /* Consid
16055 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  er functions to 
16056 62 65 20 63 6f 6e 73 74 61 6e 74 20 69 66 20 61  be constant if a
16057 6c 6c 20 74 68 65 69 72 20 61 72 67 75 6d 65 6e  ll their argumen
16058 74 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 0a  ts are constant.
16059 20 20 20 20 2a 2a 20 61 6e 64 20 2a 70 41 72 67      ** and *pArg
1605a 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ==2 */.    case 
1605b 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20  TK_FUNCTION:.   
1605c 20 20 20 69 66 28 20 28 2a 70 4e 29 3d 3d 32 20     if( (*pN)==2 
1605d 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
1605e 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
1605f 68 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b  h */.    case TK
16060 5f 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _ID:.    case TK
16061 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73  _COLUMN:.    cas
16062 65 20 54 4b 5f 44 4f 54 3a 0a 20 20 20 20 63 61  e TK_DOT:.    ca
16063 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  se TK_AGG_FUNCTI
16064 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ON:.    case TK_
16065 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 23 69 66 6e  AGG_COLUMN:.#ifn
16066 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
16067 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
16068 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20  e TK_SELECT:.   
16069 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a   case TK_EXISTS:
1606a 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 2a 70  .#endif.      *p
1606b 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74  N = 0;.      ret
1606c 75 72 6e 20 32 3b 0a 20 20 20 20 63 61 73 65 20  urn 2;.    case 
1606d 54 4b 5f 49 4e 3a 0a 20 20 20 20 20 20 69 66 28  TK_IN:.      if(
1606e 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 20   pExpr->pSelect 
1606f 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 4e 20 3d  ){.        *pN =
16070 20 30 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   0;.        retu
16071 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 2;.      }.  
16072 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
16073 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d   return 0;.  }.}
16074 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20  ../*.** Walk an 
16075 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
16076 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68    Return 1 if th
16077 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
16078 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20  constant.** and 
16079 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73  0 if it involves
1607a 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75   variables or fu
1607b 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e 0a 2a 2a  nction calls..**
1607c 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70  .** For the purp
1607d 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 6e  oses of this fun
1607e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d  ction, a double-
1607f 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65  quoted string (e
16080 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 20  x: "abc").** is 
16081 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 72  considered a var
16082 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e 67  iable but a sing
16083 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
16084 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 0a   (ex: 'abc') is.
16085 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a  ** a constant..*
16086 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16087 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
16088 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 20  IsConstant(Expr 
16089 2a 70 29 7b 0a 20 20 69 6e 74 20 69 73 43 6f 6e  *p){.  int isCon
1608a 73 74 20 3d 20 31 3b 0a 20 20 77 61 6c 6b 45 78  st = 1;.  walkEx
1608b 70 72 54 72 65 65 28 70 2c 20 65 78 70 72 4e 6f  prTree(p, exprNo
1608c 64 65 49 73 43 6f 6e 73 74 61 6e 74 2c 20 26 69  deIsConstant, &i
1608d 73 43 6f 6e 73 74 29 3b 0a 20 20 72 65 74 75 72  sConst);.  retur
1608e 6e 20 69 73 43 6f 6e 73 74 3b 0a 7d 0a 0a 2f 2a  n isConst;.}../*
1608f 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72  .** Walk an expr
16090 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65  ession tree.  Re
16091 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78  turn 1 if the ex
16092 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73  pression is cons
16093 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65  tant.** that doe
16094 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66  s no originate f
16095 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53  rom the ON or US
16096 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61  ING clauses of a
16097 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e   join..** Return
16098 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65   0 if it involve
16099 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66  s variables or f
1609a 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72  unction calls or
1609b 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61   terms from.** a
1609c 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  n ON or USING cl
1609d 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ause..*/.SQLITE_
1609e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1609f 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
160a0 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70  tNotJoin(Expr *p
160a1 29 7b 0a 20 20 69 6e 74 20 69 73 43 6f 6e 73 74  ){.  int isConst
160a2 20 3d 20 33 3b 0a 20 20 77 61 6c 6b 45 78 70 72   = 3;.  walkExpr
160a3 54 72 65 65 28 70 2c 20 65 78 70 72 4e 6f 64 65  Tree(p, exprNode
160a4 49 73 43 6f 6e 73 74 61 6e 74 2c 20 26 69 73 43  IsConstant, &isC
160a5 6f 6e 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  onst);.  return 
160a6 69 73 43 6f 6e 73 74 21 3d 30 3b 0a 7d 0a 0a 2f  isConst!=0;.}../
160a7 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70  *.** Walk an exp
160a8 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52  ression tree.  R
160a9 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65  eturn 1 if the e
160aa 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e  xpression is con
160ab 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66 75  stant.** or a fu
160ac 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74 68  nction call with
160ad 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65   constant argume
160ae 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e 64  nts.  Return and
160af 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61   0 if there.** a
160b0 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65 73  re any variables
160b1 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ..**.** For the 
160b2 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73  purposes of this
160b3 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75   function, a dou
160b4 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e  ble-quoted strin
160b5 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a  g (ex: "abc").**
160b6 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61   is considered a
160b7 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20   variable but a 
160b8 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74  single-quoted st
160b9 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29  ring (ex: 'abc')
160ba 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e   is.** a constan
160bb 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
160bc 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
160bd 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72  ExprIsConstantOr
160be 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a 70  Function(Expr *p
160bf 29 7b 0a 20 20 69 6e 74 20 69 73 43 6f 6e 73 74  ){.  int isConst
160c0 20 3d 20 32 3b 0a 20 20 77 61 6c 6b 45 78 70 72   = 2;.  walkExpr
160c1 54 72 65 65 28 70 2c 20 65 78 70 72 4e 6f 64 65  Tree(p, exprNode
160c2 49 73 43 6f 6e 73 74 61 6e 74 2c 20 26 69 73 43  IsConstant, &isC
160c3 6f 6e 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  onst);.  return 
160c4 69 73 43 6f 6e 73 74 21 3d 30 3b 0a 7d 0a 0a 2f  isConst!=0;.}../
160c5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  *.** If the expr
160c6 65 73 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61  ession p codes a
160c7 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65   constant intege
160c8 72 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20  r that is small 
160c9 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74  enough.** to fit
160ca 20 69 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74   in a 32-bit int
160cb 65 67 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61  eger, return 1 a
160cc 6e 64 20 70 75 74 20 74 68 65 20 76 61 6c 75 65  nd put the value
160cd 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a   of the integer.
160ce 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20  ** in *pValue.  
160cf 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
160d0 6e 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  n is not an inte
160d1 67 65 72 20 6f 72 20 69 66 20 69 74 20 69 73 20  ger or if it is 
160d2 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69  too big.** to fi
160d3 74 20 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32  t in a signed 32
160d4 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65  -bit integer, re
160d5 74 75 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65  turn 0 and leave
160d6 20 2a 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67   *pValue unchang
160d7 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
160d8 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
160d9 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45  3ExprIsInteger(E
160da 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61  xpr *p, int *pVa
160db 6c 75 65 29 7b 0a 20 20 73 77 69 74 63 68 28 20  lue){.  switch( 
160dc 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  p->op ){.    cas
160dd 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  e TK_INTEGER: {.
160de 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
160df 33 47 65 74 49 6e 74 33 32 28 28 63 68 61 72 2a  3GetInt32((char*
160e0 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 56 61  )p->token.z, pVa
160e1 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  lue) ){.        
160e2 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
160e3 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
160e4 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
160e5 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20  _UPLUS: {.      
160e6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78  return sqlite3Ex
160e7 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70  prIsInteger(p->p
160e8 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20  Left, pValue);. 
160e9 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
160ea 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20  _UMINUS: {.     
160eb 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 66   int v;.      if
160ec 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
160ed 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c  nteger(p->pLeft,
160ee 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20   &v) ){.        
160ef 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20  *pValue = -v;.  
160f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
160f1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
160f2 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
160f3 65 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20  efault: break;. 
160f4 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
160f5 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
160f6 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e  RUE if the given
160f7 20 73 74 72 69 6e 67 20 69 73 20 61 20 72 6f 77   string is a row
160f8 2d 69 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e  -id column name.
160f9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
160fa 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
160fb 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68 61 72  Rowid(const char
160fc 20 2a 7a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69   *z){.  if( sqli
160fd 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 5f  te3StrICmp(z, "_
160fe 52 4f 57 49 44 5f 22 29 3d 3d 30 20 29 20 72 65  ROWID_")==0 ) re
160ff 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 73 71  turn 1;.  if( sq
16100 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
16101 22 52 4f 57 49 44 22 29 3d 3d 30 20 29 20 72 65  "ROWID")==0 ) re
16102 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 73 71  turn 1;.  if( sq
16103 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
16104 22 4f 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75  "OID")==0 ) retu
16105 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 30  rn 1;.  return 0
16106 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  ;.}../*.** Given
16107 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63   the name of a c
16108 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 6f 72  olumn of the for
16109 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f  m X.Y.Z or Y.Z o
1610a 72 20 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b 20 75  r just Z, look u
1610b 70 0a 2a 2a 20 74 68 61 74 20 6e 61 6d 65 20 69  p.** that name i
1610c 6e 20 74 68 65 20 73 65 74 20 6f 66 20 73 6f 75  n the set of sou
1610d 72 63 65 20 74 61 62 6c 65 73 20 69 6e 20 70 53  rce tables in pS
1610e 72 63 4c 69 73 74 20 61 6e 64 20 6d 61 6b 65 20  rcList and make 
1610f 74 68 65 20 70 45 78 70 72 20 0a 2a 2a 20 65 78  the pExpr .** ex
16110 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 72 65  pression node re
16111 66 65 72 20 62 61 63 6b 20 74 6f 20 74 68 61 74  fer back to that
16112 20 73 6f 75 72 63 65 20 63 6f 6c 75 6d 6e 2e 20   source column. 
16113 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
16114 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61  hanges.** are ma
16115 64 65 20 74 6f 20 70 45 78 70 72 3a 0a 2a 2a 0a  de to pExpr:.**.
16116 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 44 62  **    pExpr->iDb
16117 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20 74             Set t
16118 68 65 20 69 6e 64 65 78 20 69 6e 20 64 62 2d 3e  he index in db->
16119 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74  aDb[] of the dat
1611a 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a  abase holding.**
1611b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1611c 20 20 20 20 20 20 20 20 20 74 68 65 20 74 61 62           the tab
1611d 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  le..**    pExpr-
1611e 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20 20 53  >iTable        S
1611f 65 74 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72  et to the cursor
16120 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
16121 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a 2a  table obtained.*
16122 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
16123 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 70            from p
16124 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20 70  SrcList..**    p
16125 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20  Expr->iColumn   
16126 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63      Set to the c
16127 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74  olumn number wit
16128 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a  hin the table..*
16129 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20  *    pExpr->op  
1612a 20 20 20 20 20 20 20 20 20 20 53 65 74 20 74 6f            Set to
1612b 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20   TK_COLUMN..**  
1612c 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20    pExpr->pLeft  
1612d 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65         Any expre
1612e 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74  ssion this point
1612f 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a  s to is deleted.
16130 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69  **    pExpr->pRi
16131 67 68 74 20 20 20 20 20 20 20 20 41 6e 79 20 65  ght        Any e
16132 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70  xpression this p
16133 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65  oints to is dele
16134 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ted..**.** The p
16135 44 62 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 6e  DbToken is the n
16136 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
16137 61 73 65 20 28 74 68 65 20 22 58 22 29 2e 20 20  ase (the "X").  
16138 54 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62  This value may b
16139 65 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e  e.** NULL meanin
1613a 67 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f  g that name is o
1613b 66 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f  f the form Y.Z o
1613c 72 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61  r Z.  Any availa
1613d 62 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ble database.** 
1613e 63 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68  can be used.  Th
1613f 65 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73  e pTableToken is
16140 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
16141 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29   table (the "Y")
16142 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65  .  This.** value
16143 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20   can be NULL if 
16144 70 44 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73 6f  pDbToken is also
16145 20 4e 55 4c 4c 2e 20 20 49 66 20 70 54 61 62 6c   NULL.  If pTabl
16146 65 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 69  eToken is NULL i
16147 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20  t.** means that 
16148 74 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20  the form of the 
16149 6e 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68  name is Z and th
1614a 61 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20  at columns from 
1614b 61 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e  any table.** can
1614c 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   be used..**.** 
1614d 49 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e  If the name cann
1614e 6f 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75  ot be resolved u
1614f 6e 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65  nambiguously, le
16150 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
16151 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73  sage.** in pPars
16152 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e  e and return non
16153 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a  -zero.  Return z
16154 65 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a  ero on success..
16155 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f  */.static int lo
16156 6f 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73  okupName(.  Pars
16157 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
16158 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
16159 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b  context */.  Tok
1615a 65 6e 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20 20  en *pDbToken,   
1615b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1615c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
1615d 6e 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e  ning table, or N
1615e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ULL */.  Token *
1615f 70 54 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a  pTableToken,  /*
16160 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63   Name of table c
16161 6f 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e  ontaining column
16162 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54  , or NULL */.  T
16163 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b  oken *pColumnTok
16164 65 6e 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  en, /* Name of t
16165 68 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20  he column. */.  
16166 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
16167 2c 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65  ,    /* The name
16168 20 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f   context used to
16169 20 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d   resolve the nam
1616a 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  e */.  Expr *pEx
1616b 70 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  pr          /* M
1616c 61 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f  ake this EXPR no
1616d 64 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  de point to the 
1616e 73 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20  selected column 
1616f 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44  */.){.  char *zD
16170 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  b = 0;       /* 
16171 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
16172 62 61 73 65 2e 20 20 54 68 65 20 22 58 22 20 69  base.  The "X" i
16173 6e 20 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68 61  n X.Y.Z */.  cha
16174 72 20 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20 20  r *zTab = 0;    
16175 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
16176 20 74 61 62 6c 65 2e 20 20 54 68 65 20 22 59 22   table.  The "Y"
16177 20 69 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a   in X.Y.Z or Y.Z
16178 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   */.  char *zCol
16179 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61   = 0;      /* Na
1617a 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  me of the column
1617b 2e 20 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20 20  .  The "Z" */.  
1617c 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
1617d 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1617e 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63  nters */.  int c
1617f 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nt = 0;         
16180 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74  /* Number of mat
16181 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ching column nam
16182 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 54  es */.  int cntT
16183 61 62 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  ab = 0;      /* 
16184 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69  Number of matchi
16185 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a  ng table names *
16186 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
16187 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f  = pParse->db;  /
16188 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 2a  * The database *
16189 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
1618a 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20  st_item *pItem; 
1618b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f 72        /* Use for
1618c 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53   looping over pS
1618d 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a  rcList items */.
1618e 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
1618f 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20  _item *pMatch = 
16190 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63 68  0;  /* The match
16191 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74 65  ing pSrcList ite
16192 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  m */.  NameConte
16193 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43  xt *pTopNC = pNC
16194 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  ;        /* Firs
16195 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e  t namecontext in
16196 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 0a 20 20   the list */..  
16197 61 73 73 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54  assert( pColumnT
16198 6f 6b 65 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54  oken && pColumnT
16199 6f 6b 65 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68  oken->z ); /* Th
1619a 65 20 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e  e Z in X.Y.Z can
1619b 6e 6f 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  not be NULL */. 
1619c 20 7a 44 62 20 3d 20 73 71 6c 69 74 65 33 4e 61   zDb = sqlite3Na
1619d 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
1619e 70 44 62 54 6f 6b 65 6e 29 3b 0a 20 20 7a 54 61  pDbToken);.  zTa
1619f 62 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  b = sqlite3NameF
161a0 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61  romToken(db, pTa
161a1 62 6c 65 54 6f 6b 65 6e 29 3b 0a 20 20 7a 43 6f  bleToken);.  zCo
161a2 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  l = sqlite3NameF
161a3 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f  romToken(db, pCo
161a4 6c 75 6d 6e 54 6f 6b 65 6e 29 3b 0a 20 20 69 66  lumnToken);.  if
161a5 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
161a6 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6c  ed ){.    goto l
161a7 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a 20  ookupname_end;. 
161a8 20 7d 0a 0a 20 20 70 45 78 70 72 2d 3e 69 54 61   }..  pExpr->iTa
161a9 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 77 68 69 6c  ble = -1;.  whil
161aa 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d 3d 30  e( pNC && cnt==0
161ab 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74   ){.    ExprList
161ac 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20 53 72   *pEList;.    Sr
161ad 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20  cList *pSrcList 
161ae 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b  = pNC->pSrcList;
161af 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63 4c 69  ..    if( pSrcLi
161b0 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  st ){.      for(
161b1 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72 63 4c  i=0, pItem=pSrcL
161b2 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63 4c 69  ist->a; i<pSrcLi
161b3 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
161b4 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
161b5 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
161b6 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20        int iDb;. 
161b7 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70         Column *p
161b8 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20 20 20  Col;.  .        
161b9 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54  pTab = pItem->pT
161ba 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ab;.        asse
161bb 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20  rt( pTab!=0 );. 
161bc 20 20 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c         iDb = sql
161bd 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
161be 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
161bf 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 61 73  ema);.        as
161c0 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c  sert( pTab->nCol
161c1 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  >0 );.        if
161c2 28 20 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( zTab ){.      
161c3 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a      if( pItem->z
161c4 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20  Alias ){.       
161c5 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e       char *zTabN
161c6 61 6d 65 20 3d 20 70 49 74 65 6d 2d 3e 7a 41 6c  ame = pItem->zAl
161c7 69 61 73 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ias;.           
161c8 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
161c9 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54  Cmp(zTabName, zT
161ca 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75  ab)!=0 ) continu
161cb 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  e;.          }el
161cc 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
161cd 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d  char *zTabName =
161ce 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20   pTab->zName;.  
161cf 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54            if( zT
161d0 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20 73 71 6c  abName==0 || sql
161d1 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 62  ite3StrICmp(zTab
161d2 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 29  Name, zTab)!=0 )
161d3 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
161d4 20 20 20 20 20 20 20 69 66 28 20 7a 44 62 21 3d         if( zDb!=
161d5 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  0 && sqlite3StrI
161d6 43 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d  Cmp(db->aDb[iDb]
161d7 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 21 3d 30 20  .zName, zDb)!=0 
161d8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
161d9 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
161da 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
161db 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
161dc 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 63         if( 0==(c
161dd 6e 74 54 61 62 2b 2b 29 20 29 7b 0a 20 20 20 20  ntTab++) ){.    
161de 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
161df 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75  ble = pItem->iCu
161e0 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20  rsor;.          
161e1 70 45 78 70 72 2d 3e 70 53 63 68 65 6d 61 20 3d  pExpr->pSchema =
161e2 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
161e3 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
161e4 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20   = pItem;.      
161e5 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28    }.        for(
161e6 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e  j=0, pCol=pTab->
161e7 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  aCol; j<pTab->nC
161e8 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; j++, pCol++)
161e9 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
161ea 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
161eb 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  Col->zName, zCol
161ec 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
161ed 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
161ee 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43  zColl = pTab->aC
161ef 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20  ol[j].zColl;.   
161f0 20 20 20 20 20 20 20 20 20 49 64 4c 69 73 74 20           IdList 
161f1 2a 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20 20  *pUsing;.       
161f2 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
161f3 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
161f4 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69  Table = pItem->i
161f5 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20  Cursor;.        
161f6 20 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74      pMatch = pIt
161f7 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  em;.            
161f8 70 45 78 70 72 2d 3e 70 53 63 68 65 6d 61 20 3d  pExpr->pSchema =
161f9 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
161fa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
161fb 75 62 73 74 69 74 75 74 65 20 74 68 65 20 72 6f  ubstitute the ro
161fc 77 69 64 20 28 63 6f 6c 75 6d 6e 20 2d 31 29 20  wid (column -1) 
161fd 66 6f 72 20 74 68 65 20 49 4e 54 45 47 45 52 20  for the INTEGER 
161fe 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20  PRIMARY KEY */. 
161ff 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
16200 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a 3d 3d 70  ->iColumn = j==p
16201 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20  Tab->iPKey ? -1 
16202 3a 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 20  : j;.           
16203 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
16204 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d   = pTab->aCol[j]
16205 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20  .affinity;.     
16206 20 20 20 20 20 20 20 69 66 28 20 28 70 45 78 70         if( (pExp
16207 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  r->flags & EP_Ex
16208 70 43 6f 6c 6c 61 74 65 29 3d 3d 30 20 29 7b 0a  pCollate)==0 ){.
16209 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1620a 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c  xpr->pColl = sql
1620b 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
1620c 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f  db, ENC(db), zCo
1620d 6c 6c 2c 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20  ll,-1, 0);.     
1620e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1620f 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c       if( i<pSrcL
16210 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20  ist->nSrc-1 ){. 
16211 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
16212 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79   pItem[1].jointy
16213 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20  pe & JT_NATURAL 
16214 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
16215 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61     /* If this ma
16216 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20  tch occurred in 
16217 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f  the left table o
16218 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e  f a natural join
16219 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1621a 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74    ** then skip t
1621b 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 74  he right table t
1621c 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63  o avoid a duplic
1621d 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20  ate match */.   
1621e 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74               pIt
1621f 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  em++;.          
16220 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
16221 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
16222 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74  f( (pUsing = pIt
16223 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30  em[1].pUsing)!=0
16224 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
16225 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d      /* If this m
16226 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61  atch occurs on a
16227 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   column that is 
16228 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61  in the USING cla
16229 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  use.            
1622a 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e      ** of a join
1622b 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72 63  , skip the searc
1622c 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74  h of the right t
1622d 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e  able of the join
1622e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1622f 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64   ** to avoid a d
16230 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74  uplicate match t
16231 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20  here. */.       
16232 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a           int k;.
16233 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16234 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e  for(k=0; k<pUsin
16235 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20  g->nId; k++){.  
16236 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16237 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
16238 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e  mp(pUsing->a[k].
16239 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20  zName, zCol)==0 
1623a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1623b 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a         pItem++;.
1623c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1623d 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20      i++;.       
1623e 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
1623f 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
16240 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16241 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
16242 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
16243 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16244 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
16245 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
16246 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
16247 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16248 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20  MIT_TRIGGER.    
16249 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f  /* If we have no
1624a 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76  t already resolv
1624b 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65  ed the name, the
1624c 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20  n maybe .    ** 
1624d 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72  it is a new.* or
1624e 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61   old.* trigger a
1624f 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63  rgument referenc
16250 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  e.    */.    if(
16251 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21   zDb==0 && zTab!
16252 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20  =0 && cnt==0 && 
16253 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
16254 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54 72  k!=0 ){.      Tr
16255 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72 69  iggerStack *pTri
16256 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61 72  ggerStack = pPar
16257 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a 20  se->trigStack;. 
16258 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
16259 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
1625a 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e  pTriggerStack->n
1625b 65 77 49 64 78 20 21 3d 20 2d 31 20 26 26 20 73  ewIdx != -1 && s
1625c 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6e  qlite3StrICmp("n
1625d 65 77 22 2c 20 7a 54 61 62 29 20 3d 3d 20 30 20  ew", zTab) == 0 
1625e 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  ){.        pExpr
1625f 2d 3e 69 54 61 62 6c 65 20 3d 20 70 54 72 69 67  ->iTable = pTrig
16260 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78  gerStack->newIdx
16261 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
16262 28 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  ( pTriggerStack-
16263 3e 70 54 61 62 20 29 3b 0a 20 20 20 20 20 20 20  >pTab );.       
16264 20 70 54 61 62 20 3d 20 70 54 72 69 67 67 65 72   pTab = pTrigger
16265 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 20  Stack->pTab;.   
16266 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 72     }else if( pTr
16267 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 49  iggerStack->oldI
16268 64 78 20 21 3d 20 2d 31 20 26 26 20 73 71 6c 69  dx != -1 && sqli
16269 74 65 33 53 74 72 49 43 6d 70 28 22 6f 6c 64 22  te3StrICmp("old"
1626a 2c 20 7a 54 61 62 29 3d 3d 30 20 29 7b 0a 20 20  , zTab)==0 ){.  
1626b 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
1626c 62 6c 65 20 3d 20 70 54 72 69 67 67 65 72 53 74  ble = pTriggerSt
1626d 61 63 6b 2d 3e 6f 6c 64 49 64 78 3b 0a 20 20 20  ack->oldIdx;.   
1626e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72       assert( pTr
1626f 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62  iggerStack->pTab
16270 20 29 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62   );.        pTab
16271 20 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b   = pTriggerStack
16272 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a  ->pTab;.      }.
16273 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20  .      if( pTab 
16274 29 7b 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ){ .        int 
16275 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 43 6f  iCol;.        Co
16276 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 70 54 61  lumn *pCol = pTa
16277 62 2d 3e 61 43 6f 6c 3b 0a 0a 20 20 20 20 20 20  b->aCol;..      
16278 20 20 70 45 78 70 72 2d 3e 70 53 63 68 65 6d 61    pExpr->pSchema
16279 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
1627a 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 54 61 62  ;.        cntTab
1627b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  ++;.        for(
1627c 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70  iCol=0; iCol < p
1627d 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b  Tab->nCol; iCol+
1627e 2b 2c 20 70 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20  +, pCol++) {.   
1627f 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
16280 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e  e3StrICmp(pCol->
16281 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20  zName, zCol)==0 
16282 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
16283 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  onst char *zColl
16284 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
16285 6f 6c 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20  ol].zColl;.     
16286 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20         cnt++;.  
16287 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
16288 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d  >iColumn = iCol=
16289 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d  =pTab->iPKey ? -
1628a 31 20 3a 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20  1 : iCol;.      
1628b 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66        pExpr->aff
1628c 69 6e 69 74 79 20 3d 20 70 54 61 62 2d 3e 61 43  inity = pTab->aC
1628d 6f 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74  ol[iCol].affinit
1628e 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  y;.            i
1628f 66 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73  f( (pExpr->flags
16290 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
16291 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
16292 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f        pExpr->pCo
16293 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ll = sqlite3Find
16294 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28  CollSeq(db, ENC(
16295 64 62 29 2c 20 7a 43 6f 6c 6c 2c 2d 31 2c 20 30  db), zColl,-1, 0
16296 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
16297 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78  .            pEx
16298 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b  pr->pTab = pTab;
16299 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
1629a 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
1629b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1629c 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  }.    }.#endif /
1629d 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
1629e 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20  E_OMIT_TRIGGER) 
1629f 2a 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a  */..    /*.    *
162a0 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 61  * Perhaps the na
162a1 6d 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63  me is a referenc
162a2 65 20 74 6f 20 74 68 65 20 52 4f 57 49 44 0a 20  e to the ROWID. 
162a3 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e     */.    if( cn
162a4 74 3d 3d 30 20 26 26 20 63 6e 74 54 61 62 3d 3d  t==0 && cntTab==
162a5 31 20 26 26 20 73 71 6c 69 74 65 33 49 73 52 6f  1 && sqlite3IsRo
162a6 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20  wid(zCol) ){.   
162a7 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20     cnt = 1;.    
162a8 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
162a9 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78   = -1;.      pEx
162aa 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53  pr->affinity = S
162ab 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
162ac 52 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  R;.    }..    /*
162ad 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 69  .    ** If the i
162ae 6e 70 75 74 20 69 73 20 6f 66 20 74 68 65 20 66  nput is of the f
162af 6f 72 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f  orm Z (not Y.Z o
162b0 72 20 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74 68  r X.Y.Z) then th
162b1 65 20 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a 20  e name Z.    ** 
162b2 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f 20 61  might refer to a
162b3 6e 20 72 65 73 75 6c 74 2d 73 65 74 20 61 6c 69  n result-set ali
162b4 61 73 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e  as.  This happen
162b5 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20  s, for example, 
162b6 77 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20 61  when.    ** we a
162b7 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d  re resolving nam
162b8 65 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  es in the WHERE 
162b9 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 66 6f  clause of the fo
162ba 6c 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a  llowing command:
162bb 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
162bc 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53     SELECT a+b AS
162bd 20 78 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48   x FROM table WH
162be 45 52 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a 2a  ERE x<10;.    **
162bf 0a 20 20 20 20 2a 2a 20 49 6e 20 63 61 73 65 73  .    ** In cases
162c0 20 6c 69 6b 65 20 74 68 69 73 2c 20 72 65 70 6c   like this, repl
162c1 61 63 65 20 70 45 78 70 72 20 77 69 74 68 20 61  ace pExpr with a
162c2 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70   copy of the exp
162c3 72 65 73 73 69 6f 6e 20 74 68 61 74 0a 20 20 20  ression that.   
162c4 20 2a 2a 20 66 6f 72 6d 73 20 74 68 65 20 72 65   ** forms the re
162c5 73 75 6c 74 20 73 65 74 20 65 6e 74 72 79 20 28  sult set entry (
162c6 22 61 2b 62 22 20 69 6e 20 74 68 65 20 65 78 61  "a+b" in the exa
162c7 6d 70 6c 65 29 20 61 6e 64 20 72 65 74 75 72 6e  mple) and return
162c8 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20   immediately..  
162c9 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74    ** Note that t
162ca 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  he expression in
162cb 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
162cc 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65  should have alre
162cd 61 64 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20  ady been.    ** 
162ce 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68 65 20  resolved by the 
162cf 74 69 6d 65 20 74 68 65 20 57 48 45 52 45 20 63  time the WHERE c
162d0 6c 61 75 73 65 20 69 73 20 72 65 73 6f 6c 76 65  lause is resolve
162d1 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
162d2 28 20 63 6e 74 3d 3d 30 20 26 26 20 28 70 45 4c  ( cnt==0 && (pEL
162d3 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69 73  ist = pNC->pELis
162d4 74 29 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30  t)!=0 && zTab==0
162d5 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d   ){.      for(j=
162d6 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; j<pEList->nEx
162d7 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; j++){.      
162d8 20 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45    char *zAs = pE
162d9 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65  List->a[j].zName
162da 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 41  ;.        if( zA
162db 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53  s!=0 && sqlite3S
162dc 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c  trICmp(zAs, zCol
162dd 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
162de 20 20 45 78 70 72 20 2a 70 44 75 70 2c 20 2a 70    Expr *pDup, *p
162df 4f 72 69 67 3b 0a 20 20 20 20 20 20 20 20 20 20  Orig;.          
162e0 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
162e1 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72  Left==0 && pExpr
162e2 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20  ->pRight==0 );. 
162e3 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
162e4 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3d 3d 30   pExpr->pList==0
162e5 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   );.          as
162e6 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 53 65  sert( pExpr->pSe
162e7 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lect==0 );.     
162e8 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70 45 4c       pOrig = pEL
162e9 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 3b  ist->a[j].pExpr;
162ea 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
162eb 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 26 26  pNC->allowAgg &&
162ec 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
162ed 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67 29 20  (pOrig, EP_Agg) 
162ee 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
162ef 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
162f0 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f  Parse, "misuse o
162f1 66 20 61 6c 69 61 73 65 64 20 61 67 67 72 65 67  f aliased aggreg
162f2 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b 0a 20  ate %s", zAs);. 
162f3 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
162f4 65 33 5f 66 72 65 65 28 7a 43 6f 6c 29 3b 0a 20  e3_free(zCol);. 
162f5 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
162f6 6e 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  n 2;.          }
162f7 0a 20 20 20 20 20 20 20 20 20 20 70 44 75 70 20  .          pDup 
162f8 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
162f9 28 64 62 2c 20 70 4f 72 69 67 29 3b 0a 20 20 20  (db, pOrig);.   
162fa 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72         if( pExpr
162fb 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
162fc 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20  Collate ){.     
162fd 20 20 20 20 20 20 20 70 44 75 70 2d 3e 70 43 6f         pDup->pCo
162fe 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c  ll = pExpr->pCol
162ff 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  l;.            p
16300 44 75 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  Dup->flags |= EP
16301 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20  _ExpCollate;.   
16302 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
16303 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 73 70     if( pExpr->sp
16304 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33  an.dyn ) sqlite3
16305 5f 66 72 65 65 28 28 63 68 61 72 2a 29 70 45 78  _free((char*)pEx
16306 70 72 2d 3e 73 70 61 6e 2e 7a 29 3b 0a 20 20 20  pr->span.z);.   
16307 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72         if( pExpr
16308 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 29 20 73 71  ->token.dyn ) sq
16309 6c 69 74 65 33 5f 66 72 65 65 28 28 63 68 61 72  lite3_free((char
1630a 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
1630b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  );.          mem
1630c 63 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c  cpy(pExpr, pDup,
1630d 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29   sizeof(*pExpr))
1630e 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1630f 74 65 33 5f 66 72 65 65 28 70 44 75 70 29 3b 0a  te3_free(pDup);.
16310 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d 20            cnt = 
16311 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61  1;.          pMa
16312 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  tch = 0;.       
16313 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62 3d     assert( zTab=
16314 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b 0a  =0 && zDb==0 );.
16315 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6c            goto l
16316 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32 3b  ookupname_end_2;
16317 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
16318 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   } .    }..    /
16319 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 65  * Advance to the
1631a 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74 65   next name conte
1631b 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77 69  xt.  The loop wi
1631c 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69 74  ll exit when eit
1631d 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68 61  her.    ** we ha
1631e 76 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74 3e  ve a match (cnt>
1631f 30 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72 75  0) or when we ru
16320 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63 6f  n out of name co
16321 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a 20  ntexts..    */. 
16322 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 7b     if( cnt==0 ){
16323 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e 43  .      pNC = pNC
16324 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  ->pNext;.    }. 
16325 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66   }..  /*.  ** If
16326 20 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55 4c   X and Y are NUL
16327 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 64  L (in other word
16328 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63 6f  s if only the co
16329 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a 20  lumn name Z is. 
1632a 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61 6e   ** supplied) an
1632b 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 5a  d the value of Z
1632c 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20   is enclosed in 
1632d 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20 74  double-quotes, t
1632e 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61 20  hen.  ** Z is a 
1632f 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 69  string literal i
16330 66 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61 74  f it doesn't mat
16331 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e 61  ch any column na
16332 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20 20  mes.  In that.  
16333 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65 64  ** case, we need
16334 20 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68 74   to return right
16335 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d 61   away and not ma
16336 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74  ke any changes t
16337 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20 20  o.  ** pExpr..  
16338 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65 20  **.  ** Because 
16339 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 73  no reference was
1633a 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20 63   made to outer c
1633b 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e 43  ontexts, the pNC
1633c 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65 6c  ->nRef.  ** fiel
1633d 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e 67  ds are not chang
1633e 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65 78  ed in any contex
1633f 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e  t..  */.  if( cn
16340 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20  t==0 && zTab==0 
16341 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 2d  && pColumnToken-
16342 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 7b 0a 20 20  >z[0]=='"' ){.  
16343 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
16344 43 6f 6c 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Col);.    return
16345 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20   0;.  }..  /*.  
16346 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20  ** cnt==0 means 
16347 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 6d 61  there was not ma
16348 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 61 6e  tch.  cnt>1 mean
16349 73 20 74 68 65 72 65 20 77 65 72 65 20 74 77 6f  s there were two
1634a 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d 61   or.  ** more ma
1634b 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 20 77  tches.  Either w
1634c 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e 20 65  ay, we have an e
1634d 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rror..  */.  if(
1634e 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 20 63   cnt!=1 ){.    c
1634f 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20  har *z = 0;.    
16350 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20  char *zErr;.    
16351 7a 45 72 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20  zErr = cnt==0 ? 
16352 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a  "no such column:
16353 20 25 73 22 20 3a 20 22 61 6d 62 69 67 75 6f 75   %s" : "ambiguou
16354 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25  s column name: %
16355 73 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20  s";.    if( zDb 
16356 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
16357 53 65 74 53 74 72 69 6e 67 28 26 7a 2c 20 7a 44  SetString(&z, zD
16358 62 2c 20 22 2e 22 2c 20 7a 54 61 62 2c 20 22 2e  b, ".", zTab, ".
16359 22 2c 20 7a 43 6f 6c 2c 20 28 63 68 61 72 2a 29  ", zCol, (char*)
1635a 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  0);.    }else if
1635b 28 20 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( zTab ){.      
1635c 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1635d 28 26 7a 2c 20 7a 54 61 62 2c 20 22 2e 22 2c 20  (&z, zTab, ".", 
1635e 7a 43 6f 6c 2c 20 28 63 68 61 72 2a 29 30 29 3b  zCol, (char*)0);
1635f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
16360 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72    z = sqlite3Str
16361 44 75 70 28 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  Dup(zCol);.    }
16362 0a 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20  .    if( z ){.  
16363 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
16364 4d 73 67 28 70 50 61 72 73 65 2c 20 7a 45 72 72  Msg(pParse, zErr
16365 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , z);.      sqli
16366 74 65 33 5f 66 72 65 65 28 7a 29 3b 0a 20 20 20  te3_free(z);.   
16367 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 72 2b     pTopNC->nErr+
16368 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  +;.    }else{.  
16369 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1636a 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  iled = 1;.    }.
1636b 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 63    }..  /* If a c
1636c 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 20 74 61 62  olumn from a tab
1636d 6c 65 20 69 6e 20 70 53 72 63 4c 69 73 74 20 69  le in pSrcList i
1636e 73 20 72 65 66 65 72 65 6e 63 65 64 2c 20 74 68  s referenced, th
1636f 65 6e 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20 74  en record.  ** t
16370 68 69 73 20 66 61 63 74 20 69 6e 20 74 68 65 20  his fact in the 
16371 70 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c  pSrcList.a[].col
16372 55 73 65 64 20 62 69 74 6d 61 73 6b 2e 20 20 43  Used bitmask.  C
16373 6f 6c 75 6d 6e 20 30 20 63 61 75 73 65 73 0a 20  olumn 0 causes. 
16374 20 2a 2a 20 62 69 74 20 30 20 74 6f 20 62 65 20   ** bit 0 to be 
16375 73 65 74 2e 20 20 43 6f 6c 75 6d 6e 20 31 20 73  set.  Column 1 s
16376 65 74 73 20 62 69 74 20 31 2e 20 20 41 6e 64 20  ets bit 1.  And 
16377 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68  so forth.  If th
16378 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75  e.  ** column nu
16379 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20  mber is greater 
1637a 74 68 61 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  than the number 
1637b 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 62  of bits in the b
1637c 69 74 6d 61 73 6b 0a 20 20 2a 2a 20 74 68 65 6e  itmask.  ** then
1637d 20 73 65 74 20 74 68 65 20 68 69 67 68 2d 6f 72   set the high-or
1637e 64 65 72 20 62 69 74 20 6f 66 20 74 68 65 20 62  der bit of the b
1637f 69 74 6d 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69  itmask..  */.  i
16380 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  f( pExpr->iColum
16381 6e 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21 3d  n>=0 && pMatch!=
16382 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  0 ){.    int n =
16383 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
16384 0a 20 20 20 20 69 66 28 20 6e 3e 3d 73 69 7a 65  .    if( n>=size
16385 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 20 29 7b  of(Bitmask)*8 ){
16386 0a 20 20 20 20 20 20 6e 20 3d 20 73 69 7a 65 6f  .      n = sizeo
16387 66 28 42 69 74 6d 61 73 6b 29 2a 38 2d 31 3b 0a  f(Bitmask)*8-1;.
16388 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
16389 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 72 73 6f  ( pMatch->iCurso
1638a 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  r==pExpr->iTable
1638b 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 68 2d 3e   );.    pMatch->
1638c 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 69 74  colUsed |= ((Bit
1638d 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d 0a  mask)1)<<n;.  }.
1638e 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a  .lookupname_end:
1638f 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 61  .  /* Clean up a
16390 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20  nd return.  */. 
16391 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 44   sqlite3_free(zD
16392 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  b);.  sqlite3_fr
16393 65 65 28 7a 54 61 62 29 3b 0a 20 20 73 71 6c 69  ee(zTab);.  sqli
16394 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 45  te3ExprDelete(pE
16395 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70  xpr->pLeft);.  p
16396 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b  Expr->pLeft = 0;
16397 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
16398 6c 65 74 65 28 70 45 78 70 72 2d 3e 70 52 69 67  lete(pExpr->pRig
16399 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52  ht);.  pExpr->pR
1639a 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 70  ight = 0;.  pExp
1639b 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d  r->op = TK_COLUM
1639c 4e 3b 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e  N;.lookupname_en
1639d 64 5f 32 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66  d_2:.  sqlite3_f
1639e 72 65 65 28 7a 43 6f 6c 29 3b 0a 20 20 69 66 28  ree(zCol);.  if(
1639f 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 20 61   cnt==1 ){.    a
163a0 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b  ssert( pNC!=0 );
163a1 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68  .    sqlite3Auth
163a2 52 65 61 64 28 70 50 61 72 73 65 2c 20 70 45 78  Read(pParse, pEx
163a3 70 72 2c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  pr, pNC->pSrcLis
163a4 74 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 61 74  t);.    if( pMat
163a5 63 68 20 26 26 20 21 70 4d 61 74 63 68 2d 3e 70  ch && !pMatch->p
163a6 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
163a7 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 4d  pExpr->pTab = pM
163a8 61 74 63 68 2d 3e 70 54 61 62 3b 0a 20 20 20 20  atch->pTab;.    
163a9 7d 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65  }.    /* Increme
163aa 6e 74 20 74 68 65 20 6e 52 65 66 20 76 61 6c 75  nt the nRef valu
163ab 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f  e on all name co
163ac 6e 74 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e  ntexts from TopN
163ad 43 20 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74  C up to.    ** t
163ae 68 65 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74  he point where t
163af 68 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e  he name matched.
163b0 20 2a 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b   */.    for(;;){
163b1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
163b2 54 6f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20  TopNC!=0 );.    
163b3 20 20 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b    pTopNC->nRef++
163b4 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f 70  ;.      if( pTop
163b5 4e 43 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b  NC==pNC ) break;
163b6 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 20 3d 20  .      pTopNC = 
163b7 70 54 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20  pTopNC->pNext;. 
163b8 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
163b9 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  0;.  } else {.  
163ba 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
163bb 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
163bc 75 74 69 6e 65 20 69 73 20 64 65 73 69 67 6e 65  utine is designe
163bd 64 20 61 73 20 61 6e 20 78 46 75 6e 63 20 66 6f  d as an xFunc fo
163be 72 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28 29  r walkExprTree()
163bf 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20  ..**.** Resolve 
163c0 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 73 20 69  symbolic names i
163c1 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f 70  nto TK_COLUMN op
163c2 65 72 61 74 6f 72 73 20 66 6f 72 20 74 68 65 20  erators for the 
163c3 63 75 72 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65 20  current.** node 
163c4 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
163c5 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20  n tree.  Return 
163c6 30 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 68  0 to continue th
163c7 65 20 73 65 61 72 63 68 20 64 6f 77 6e 0a 2a 2a  e search down.**
163c8 20 74 68 65 20 74 72 65 65 20 6f 72 20 32 20 74   the tree or 2 t
163c9 6f 20 61 62 6f 72 74 20 74 68 65 20 74 72 65 65  o abort the tree
163ca 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   walk..**.** Thi
163cb 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64  s routine also d
163cc 6f 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69  oes error checki
163cd 6e 67 20 61 6e 64 20 6e 61 6d 65 20 72 65 73 6f  ng and name reso
163ce 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66 75  lution for.** fu
163cf 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 54  nction names.  T
163d0 68 65 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20  he operator for 
163d1 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
163d2 6f 6e 73 20 69 73 20 63 68 61 6e 67 65 64 0a 2a  ons is changed.*
163d3 2a 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43  * to TK_AGG_FUNC
163d4 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  TION..*/.static 
163d5 69 6e 74 20 6e 61 6d 65 52 65 73 6f 6c 76 65 72  int nameResolver
163d6 53 74 65 70 28 76 6f 69 64 20 2a 70 41 72 67 2c  Step(void *pArg,
163d7 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
163d8 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
163d9 43 20 3d 20 28 4e 61 6d 65 43 6f 6e 74 65 78 74  C = (NameContext
163da 2a 29 70 41 72 67 3b 0a 20 20 50 61 72 73 65 20  *)pArg;.  Parse 
163db 2a 70 50 61 72 73 65 3b 0a 0a 20 20 69 66 28 20  *pParse;..  if( 
163dc 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
163dd 6e 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n 1;.  assert( p
163de 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 73  NC!=0 );.  pPars
163df 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
163e0 0a 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 41  ..  if( ExprHasA
163e1 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  nyProperty(pExpr
163e2 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 20 29  , EP_Resolved) )
163e3 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 45 78 70   return 1;.  Exp
163e4 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78  rSetProperty(pEx
163e5 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29  pr, EP_Resolved)
163e6 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
163e7 0a 20 20 69 66 28 20 70 4e 43 2d 3e 70 53 72 63  .  if( pNC->pSrc
163e8 4c 69 73 74 20 26 26 20 70 4e 43 2d 3e 70 53 72  List && pNC->pSr
163e9 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20  cList->nAlloc>0 
163ea 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a  ){.    SrcList *
163eb 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e  pSrcList = pNC->
163ec 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e  pSrcList;.    in
163ed 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
163ee 3b 20 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73  ; i<pNC->pSrcLis
163ef 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  t->nSrc; i++){. 
163f0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72       assert( pSr
163f1 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72  cList->a[i].iCur
163f2 73 6f 72 3e 3d 30 20 26 26 20 70 53 72 63 4c 69  sor>=0 && pSrcLi
163f3 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72  st->a[i].iCursor
163f4 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a  <pParse->nTab);.
163f5 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
163f6 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72  .  switch( pExpr
163f7 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 44  ->op ){.    /* D
163f8 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ouble-quoted str
163f9 69 6e 67 73 20 28 65 78 3a 20 22 61 62 63 22 29  ings (ex: "abc")
163fa 20 61 72 65 20 75 73 65 64 20 61 73 20 69 64 65   are used as ide
163fb 6e 74 69 66 69 65 72 73 20 69 66 0a 20 20 20 20  ntifiers if.    
163fc 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 20 20 4f 74  ** possible.  Ot
163fd 68 65 72 77 69 73 65 20 74 68 65 79 20 72 65 6d  herwise they rem
163fe 61 69 6e 20 61 73 20 73 74 72 69 6e 67 73 2e 20  ain as strings. 
163ff 20 53 69 6e 67 6c 65 2d 71 75 6f 74 65 64 0a 20   Single-quoted. 
16400 20 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 28 65     ** strings (e
16401 78 3a 20 27 61 62 63 27 29 20 61 72 65 20 61 6c  x: 'abc') are al
16402 77 61 79 73 20 73 74 72 69 6e 67 20 6c 69 74 65  ways string lite
16403 72 61 6c 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rals..    */.   
16404 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a   case TK_STRING:
16405 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78   {.      if( pEx
16406 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d  pr->token.z[0]==
16407 27 5c 27 27 20 29 20 62 72 65 61 6b 3b 0a 20 20  '\'' ) break;.  
16408 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75      /* Fall thru
16409 20 69 6e 74 6f 20 74 68 65 20 54 4b 5f 49 44 20   into the TK_ID 
1640a 63 61 73 65 20 69 66 20 74 68 69 73 20 69 73 20  case if this is 
1640b 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20  a double-quoted 
1640c 73 74 72 69 6e 67 20 2a 2f 0a 20 20 20 20 7d 0a  string */.    }.
1640d 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64      /* A lone id
1640e 65 6e 74 69 66 69 65 72 20 69 73 20 74 68 65 20  entifier is the 
1640f 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  name of a column
16410 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
16411 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20  e TK_ID: {.     
16412 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72   lookupName(pPar
16413 73 65 2c 20 30 2c 20 30 2c 20 26 70 45 78 70 72  se, 0, 0, &pExpr
16414 2d 3e 74 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70 45  ->token, pNC, pE
16415 78 70 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75  xpr);.      retu
16416 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  rn 1;.    }.  . 
16417 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e 61     /* A table na
16418 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61  me and column na
16419 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0a 20 20  me:     ID.ID.  
1641a 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61 62 61    ** Or a databa
1641b 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  se, table and co
1641c 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44 0a  lumn:  ID.ID.ID.
1641d 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
1641e 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 20 20  TK_DOT: {.      
1641f 54 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 3b 0a  Token *pColumn;.
16420 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 61        Token *pTa
16421 62 6c 65 3b 0a 20 20 20 20 20 20 54 6f 6b 65 6e  ble;.      Token
16422 20 2a 70 44 62 3b 0a 20 20 20 20 20 20 45 78 70   *pDb;.      Exp
16423 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20 20  r *pRight;..    
16424 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c 69 73    /* if( pSrcLis
16425 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a 2f  t==0 ) break; */
16426 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20  .      pRight = 
16427 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
16428 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d       if( pRight-
16429 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20  >op==TK_ID ){.  
1642a 20 20 20 20 20 20 70 44 62 20 3d 20 30 3b 0a 20        pDb = 0;. 
1642b 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20         pTable = 
1642c 26 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 74  &pExpr->pLeft->t
1642d 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 43  oken;.        pC
1642e 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 2d  olumn = &pRight-
1642f 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65  >token;.      }e
16430 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
16431 65 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d  ert( pRight->op=
16432 3d 54 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20  =TK_DOT );.     
16433 20 20 20 70 44 62 20 3d 20 26 70 45 78 70 72 2d     pDb = &pExpr-
16434 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20  >pLeft->token;. 
16435 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20         pTable = 
16436 26 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e  &pRight->pLeft->
16437 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70  token;.        p
16438 43 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74  Column = &pRight
16439 2d 3e 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b  ->pRight->token;
1643a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c  .      }.      l
1643b 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73 65  ookupName(pParse
1643c 2c 20 70 44 62 2c 20 70 54 61 62 6c 65 2c 20 70  , pDb, pTable, p
1643d 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 45 78  Column, pNC, pEx
1643e 70 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  pr);.      retur
1643f 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  n 1;.    }..    
16440 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 6e 63 74  /* Resolve funct
16441 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 20 2a 2f  ion names.    */
16442 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e  .    case TK_CON
16443 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73  ST_FUNC:.    cas
16444 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b  e TK_FUNCTION: {
16445 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
16446 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  *pList = pExpr->
16447 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65  pList;    /* The
16448 20 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a   argument list *
16449 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  /.      int n = 
1644a 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e  pList ? pList->n
1644b 45 78 70 72 20 3a 20 30 3b 20 20 2f 2a 20 4e 75  Expr : 0;  /* Nu
1644c 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
1644d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  s */.      int n
1644e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30 3b  o_such_func = 0;
1644f 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
16450 66 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69  f no such functi
16451 6f 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20  on exists */.   
16452 20 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d     int wrong_num
16453 5f 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20 2f  _args = 0;     /
16454 2a 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67 20  * True if wrong 
16455 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
16456 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  nts */.      int
16457 20 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20 20   is_agg = 0;    
16458 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
16459 20 69 66 20 69 73 20 61 6e 20 61 67 67 72 65 67   if is an aggreg
1645a 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ate function */.
1645b 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
1645c 20 20 20 69 6e 74 20 61 75 74 68 3b 20 20 20 20     int auth;    
1645d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1645e 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  * Authorization 
1645f 74 6f 20 75 73 65 20 74 68 65 20 66 75 6e 63 74  to use the funct
16460 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ion */.      int
16461 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20 20 20   nId;           
16462 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
16463 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
16464 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d   in function nam
16465 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74  e */.      const
16466 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20   char *zId;     
16467 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75         /* The fu
16468 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a  nction name. */.
16469 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70        FuncDef *p
1646a 44 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Def;            
1646b 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
1646c 20 61 62 6f 75 74 20 74 68 65 20 66 75 6e 63 74   about the funct
1646d 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ion */.      int
1646e 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73   enc = ENC(pPars
1646f 65 2d 3e 64 62 29 3b 20 20 2f 2a 20 54 68 65 20  e->db);  /* The 
16470 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e  database encodin
16471 67 20 2a 2f 0a 0a 20 20 20 20 20 20 7a 49 64 20  g */..      zId 
16472 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e  = (char*)pExpr->
16473 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 6e  token.z;.      n
16474 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65  Id = pExpr->toke
16475 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 65 66 20  n.n;.      pDef 
16476 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e  = sqlite3FindFun
16477 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62  ction(pParse->db
16478 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 65  , zId, nId, n, e
16479 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  nc, 0);.      if
1647a 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ( pDef==0 ){.   
1647b 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69       pDef = sqli
1647c 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
1647d 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c  pParse->db, zId,
1647e 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 30   nId, -1, enc, 0
1647f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
16480 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Def==0 ){.      
16481 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63      no_such_func
16482 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65   = 1;.        }e
16483 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 77  lse{.          w
16484 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20  rong_num_args = 
16485 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
16486 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16487 20 20 69 73 5f 61 67 67 20 3d 20 70 44 65 66 2d    is_agg = pDef-
16488 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 20  >xFunc==0;.     
16489 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1648a 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
1648b 54 49 4f 4e 0a 20 20 20 20 20 20 69 66 28 20 70  TION.      if( p
1648c 44 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Def ){.        a
1648d 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75 74  uth = sqlite3Aut
1648e 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
1648f 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 2c 20  QLITE_FUNCTION, 
16490 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20  0, pDef->zName, 
16491 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
16492 61 75 74 68 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  auth!=SQLITE_OK 
16493 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
16494 20 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45   auth==SQLITE_DE
16495 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  NY ){.          
16496 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
16497 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61  g(pParse, "not a
16498 75 74 68 6f 72 69 7a 65 64 20 74 6f 20 75 73 65  uthorized to use
16499 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73 22 2c 0a   function: %s",.
1649a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1649b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1649c 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29      pDef->zName)
1649d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4e  ;.            pN
1649e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20  C->nErr++;.     
1649f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
164a0 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f   pExpr->op = TK_
164a1 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20  NULL;.          
164a2 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
164a3 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64    }.      }.#end
164a4 69 66 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f  if.      if( is_
164a5 61 67 67 20 26 26 20 21 70 4e 43 2d 3e 61 6c 6c  agg && !pNC->all
164a6 6f 77 41 67 67 20 29 7b 0a 20 20 20 20 20 20 20  owAgg ){.       
164a7 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
164a8 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65  (pParse, "misuse
164a9 20 6f 66 20 61 67 67 72 65 67 61 74 65 20 66 75   of aggregate fu
164aa 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 20  nction %.*s()", 
164ab 6e 49 64 2c 7a 49 64 29 3b 0a 20 20 20 20 20 20  nId,zId);.      
164ac 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20    pNC->nErr++;. 
164ad 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20         is_agg = 
164ae 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  0;.      }else i
164af 66 28 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20  f( no_such_func 
164b0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
164b1 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
164b2 65 2c 20 22 6e 6f 20 73 75 63 68 20 66 75 6e 63  e, "no such func
164b3 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49 64  tion: %.*s", nId
164b4 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20  , zId);.        
164b5 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  pNC->nErr++;.   
164b6 20 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 6f     }else if( wro
164b7 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 29 7b 0a 20  ng_num_args ){. 
164b8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
164b9 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 77  rorMsg(pParse,"w
164ba 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 61  rong number of a
164bb 72 67 75 6d 65 6e 74 73 20 74 6f 20 66 75 6e 63  rguments to func
164bc 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 0a 20 20  tion %.*s()",.  
164bd 20 20 20 20 20 20 20 20 20 20 20 6e 49 64 2c 20             nId, 
164be 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e  zId);.        pN
164bf 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20  C->nErr++;.     
164c0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f   }.      if( is_
164c1 61 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70  agg ){.        p
164c2 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47  Expr->op = TK_AG
164c3 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 20 20  G_FUNCTION;.    
164c4 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20      pNC->hasAgg 
164c5 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
164c6 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 20     if( is_agg ) 
164c7 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20  pNC->allowAgg = 
164c8 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  0;.      for(i=0
164c9 3b 20 70 4e 43 2d 3e 6e 45 72 72 3d 3d 30 20 26  ; pNC->nErr==0 &
164ca 26 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  & i<n; i++){.   
164cb 20 20 20 20 20 77 61 6c 6b 45 78 70 72 54 72 65       walkExprTre
164cc 65 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  e(pList->a[i].pE
164cd 78 70 72 2c 20 6e 61 6d 65 52 65 73 6f 6c 76 65  xpr, nameResolve
164ce 72 53 74 65 70 2c 20 70 4e 43 29 3b 0a 20 20 20  rStep, pNC);.   
164cf 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
164d0 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c  s_agg ) pNC->all
164d1 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20  owAgg = 1;.     
164d2 20 2f 2a 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d   /* FIX ME:  Com
164d3 70 75 74 65 20 70 45 78 70 72 2d 3e 61 66 66 69  pute pExpr->affi
164d4 6e 69 74 79 20 62 61 73 65 64 20 6f 6e 20 74 68  nity based on th
164d5 65 20 65 78 70 65 63 74 65 64 20 72 65 74 75 72  e expected retur
164d6 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79 70 65 20  n.      ** type 
164d7 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  of the function 
164d8 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
164d9 72 65 74 75 72 6e 20 69 73 5f 61 67 67 3b 0a 20  return is_agg;. 
164da 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
164db 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
164dc 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45  Y.    case TK_SE
164dd 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54  LECT:.    case T
164de 4b 5f 45 58 49 53 54 53 3a 0a 23 65 6e 64 69 66  K_EXISTS:.#endif
164df 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a  .    case TK_IN:
164e0 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78   {.      if( pEx
164e1 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  pr->pSelect ){. 
164e2 20 20 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20         int nRef 
164e3 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66  = pNC->nRef;.#if
164e4 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
164e5 5f 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69  _CHECK.        i
164e6 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20  f( pNC->isCheck 
164e7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
164e8 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
164e9 72 73 65 2c 22 73 75 62 71 75 65 72 69 65 73 20  rse,"subqueries 
164ea 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48  prohibited in CH
164eb 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22  ECK constraints"
164ec 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  );.        }.#en
164ed 64 69 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69  dif.        sqli
164ee 74 65 33 53 65 6c 65 63 74 52 65 73 6f 6c 76 65  te3SelectResolve
164ef 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
164f0 70 53 65 6c 65 63 74 2c 20 70 4e 43 29 3b 0a 20  pSelect, pNC);. 
164f1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
164f2 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29  NC->nRef>=nRef )
164f3 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 52  ;.        if( nR
164f4 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b  ef!=pNC->nRef ){
164f5 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53  .          ExprS
164f6 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  etProperty(pExpr
164f7 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 3b  , EP_VarSelect);
164f8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
164f9 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
164fa 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
164fb 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
164fc 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
164fd 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 66  ABLE: {.      if
164fe 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29  ( pNC->isCheck )
164ff 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
16500 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
16501 2c 22 70 61 72 61 6d 65 74 65 72 73 20 70 72 6f  ,"parameters pro
16502 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b  hibited in CHECK
16503 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a   constraints");.
16504 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
16505 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
16506 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  f.  }.  return 0
16507 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
16508 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 61 6e  routine walks an
16509 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
1650a 20 61 6e 64 20 72 65 73 6f 6c 76 65 73 20 72 65   and resolves re
1650b 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a 20 74  ferences to.** t
1650c 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 2e 20 20 4e  able columns.  N
1650d 6f 64 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  odes of the form
1650e 20 49 44 2e 49 44 20 6f 72 20 49 44 20 72 65 73   ID.ID or ID res
1650f 6f 6c 76 65 20 69 6e 74 6f 20 61 6e 0a 2a 2a 20  olve into an.** 
16510 69 6e 64 65 78 20 74 6f 20 74 68 65 20 74 61 62  index to the tab
16511 6c 65 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  le in the table 
16512 6c 69 73 74 20 61 6e 64 20 61 20 63 6f 6c 75 6d  list and a colum
16513 6e 20 6f 66 66 73 65 74 2e 20 20 54 68 65 20 0a  n offset.  The .
16514 2a 2a 20 45 78 70 72 2e 6f 70 63 6f 64 65 20 66  ** Expr.opcode f
16515 6f 72 20 73 75 63 68 20 6e 6f 64 65 73 20 69 73  or such nodes is
16516 20 63 68 61 6e 67 65 64 20 74 6f 20 54 4b 5f 43   changed to TK_C
16517 4f 4c 55 4d 4e 2e 20 20 54 68 65 20 45 78 70 72  OLUMN.  The Expr
16518 2e 69 54 61 62 6c 65 0a 2a 2a 20 76 61 6c 75 65  .iTable.** value
16519 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 74   is changed to t
1651a 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
1651b 72 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65  referenced table
1651c 20 69 6e 20 70 54 61 62 4c 69 73 74 0a 2a 2a 20   in pTabList.** 
1651d 70 6c 75 73 20 74 68 65 20 22 62 61 73 65 22 20  plus the "base" 
1651e 76 61 6c 75 65 2e 20 20 54 68 65 20 62 61 73 65  value.  The base
1651f 20 76 61 6c 75 65 20 77 69 6c 6c 20 75 6c 74 69   value will ulti
16520 6d 61 74 65 6c 79 20 62 65 63 6f 6d 65 20 74 68  mately become th
16521 65 0a 2a 2a 20 56 44 42 45 20 63 75 72 73 6f 72  e.** VDBE cursor
16522 20 6e 75 6d 62 65 72 20 66 6f 72 20 61 20 63 75   number for a cu
16523 72 73 6f 72 20 74 68 61 74 20 69 73 20 70 6f 69  rsor that is poi
16524 6e 74 69 6e 67 20 69 6e 74 6f 20 74 68 65 20 72  nting into the r
16525 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20 74 61 62  eferenced.** tab
16526 6c 65 2e 20 20 54 68 65 20 45 78 70 72 2e 69 43  le.  The Expr.iC
16527 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 63  olumn value is c
16528 68 61 6e 67 65 64 20 74 6f 20 74 68 65 20 69 6e  hanged to the in
16529 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  dex of the colum
1652a 6e 20 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 66  n .** of the ref
1652b 65 72 65 6e 63 65 64 20 74 61 62 6c 65 2e 20 20  erenced table.  
1652c 54 68 65 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e  The Expr.iColumn
1652d 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73   value for the s
1652e 70 65 63 69 61 6c 0a 2a 2a 20 52 4f 57 49 44 20  pecial.** ROWID 
1652f 63 6f 6c 75 6d 6e 20 69 73 20 2d 31 2e 20 20 41  column is -1.  A
16530 6e 79 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  ny INTEGER PRIMA
16531 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 69 73  RY KEY column is
16532 20 74 72 69 65 64 20 61 73 20 61 6e 0a 2a 2a 20   tried as an.** 
16533 61 6c 69 61 73 20 66 6f 72 20 52 4f 57 49 44 2e  alias for ROWID.
16534 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 73 6f  .**.** Also reso
16535 6c 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  lve function nam
16536 65 73 20 61 6e 64 20 63 68 65 63 6b 20 74 68 65  es and check the
16537 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 70   functions for p
16538 72 6f 70 65 72 0a 2a 2a 20 75 73 61 67 65 2e 20  roper.** usage. 
16539 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 66   Make sure all f
1653a 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 20 61 72  unction names ar
1653b 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 6e 64  e recognized and
1653c 20 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 0a 2a   all functions.*
1653d 2a 20 68 61 76 65 20 74 68 65 20 63 6f 72 72 65  * have the corre
1653e 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ct number of arg
1653f 75 6d 65 6e 74 73 2e 20 20 4c 65 61 76 65 20 61  uments.  Leave a
16540 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a  n error message.
16541 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45  ** in pParse->zE
16542 72 72 4d 73 67 20 69 66 20 61 6e 79 74 68 69 6e  rrMsg if anythin
16543 67 20 69 73 20 61 6d 69 73 73 2e 20 20 52 65 74  g is amiss.  Ret
16544 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
16545 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  f errors..**.** 
16546 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
16547 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65  n contains aggre
16548 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74  gate functions t
16549 68 65 6e 20 73 65 74 20 74 68 65 20 45 50 5f 41  hen set the EP_A
1654a 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f  gg.** property o
1654b 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
1654c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1654d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
1654e 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28  xprResolveNames(
1654f 20 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20   .  NameContext 
16550 2a 70 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e  *pNC,       /* N
16551 61 6d 65 73 70 61 63 65 20 74 6f 20 72 65 73 6f  amespace to reso
16552 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  lve expressions 
16553 69 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  in. */.  Expr *p
16554 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20 20  Expr            
16555 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
16556 6f 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  on to be analyze
16557 64 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73  d. */.){.  int s
16558 61 76 65 64 48 61 73 41 67 67 3b 0a 20 20 69 66  avedHasAgg;.  if
16559 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74  ( pExpr==0 ) ret
1655a 75 72 6e 20 30 3b 0a 23 69 66 20 64 65 66 69 6e  urn 0;.#if defin
1655b 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
1655c 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  || SQLITE_MAX_EX
1655d 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 69 66 28  PR_DEPTH>0.  if(
1655e 20 28 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74   (pExpr->nHeight
1655f 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48  +pNC->pParse->nH
16560 65 69 67 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41  eight)>SQLITE_MA
16561 58 5f 45 58 50 52 5f 44 45 50 54 48 20 29 7b 0a  X_EXPR_DEPTH ){.
16562 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
16563 4d 73 67 28 70 4e 43 2d 3e 70 50 61 72 73 65 2c  Msg(pNC->pParse,
16564 20 0a 20 20 20 20 20 20 20 22 45 78 70 72 65 73   .       "Expres
16565 73 69 6f 6e 20 74 72 65 65 20 69 73 20 74 6f 6f  sion tree is too
16566 20 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 6d 20   large (maximum 
16567 64 65 70 74 68 20 25 64 29 22 2c 0a 20 20 20 20  depth %d)",.    
16568 20 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58     SQLITE_MAX_EX
16569 50 52 5f 44 45 50 54 48 0a 20 20 20 20 29 3b 0a  PR_DEPTH.    );.
1656a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1656b 7d 0a 20 20 70 4e 43 2d 3e 70 50 61 72 73 65 2d  }.  pNC->pParse-
1656c 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70 45 78 70  >nHeight += pExp
1656d 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e 64  r->nHeight;.#end
1656e 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 67  if.  savedHasAgg
1656f 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a   = pNC->hasAgg;.
16570 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20    pNC->hasAgg = 
16571 30 3b 0a 20 20 77 61 6c 6b 45 78 70 72 54 72 65  0;.  walkExprTre
16572 65 28 70 45 78 70 72 2c 20 6e 61 6d 65 52 65 73  e(pExpr, nameRes
16573 6f 6c 76 65 72 53 74 65 70 2c 20 70 4e 43 29 3b  olverStep, pNC);
16574 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
16575 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 53 51 4c  ITE_TEST) || SQL
16576 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
16577 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 72  TH>0.  pNC->pPar
16578 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 70  se->nHeight -= p
16579 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23  Expr->nHeight;.#
1657a 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e 43 2d  endif.  if( pNC-
1657b 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 20 20 20 45  >nErr>0 ){.    E
1657c 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70  xprSetProperty(p
1657d 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b  Expr, EP_Error);
1657e 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e  .  }.  if( pNC->
1657f 68 61 73 41 67 67 20 29 7b 0a 20 20 20 20 45 78  hasAgg ){.    Ex
16580 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45  prSetProperty(pE
16581 78 70 72 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20  xpr, EP_Agg);.  
16582 7d 65 6c 73 65 20 69 66 28 20 73 61 76 65 64 48  }else if( savedH
16583 61 73 41 67 67 20 29 7b 0a 20 20 20 20 70 4e 43  asAgg ){.    pNC
16584 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20  ->hasAgg = 1;.  
16585 7d 0a 20 20 72 65 74 75 72 6e 20 45 78 70 72 48  }.  return ExprH
16586 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
16587 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a  , EP_Error);.}..
16588 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  /*.** A pointer 
16589 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
1658a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
1658b 65 64 20 74 6f 20 70 61 73 73 20 69 6e 66 6f 72  ed to pass infor
1658c 6d 61 74 69 6f 6e 0a 2a 2a 20 74 68 72 6f 75 67  mation.** throug
1658d 68 20 77 61 6c 6b 45 78 70 72 54 72 65 65 20 69  h walkExprTree i
1658e 6e 74 6f 20 63 6f 64 65 53 75 62 71 75 65 72 79  nto codeSubquery
1658f 53 74 65 70 28 29 2e 0a 2a 2f 0a 74 79 70 65 64  Step()..*/.typed
16590 65 66 20 73 74 72 75 63 74 20 51 75 65 72 79 43  ef struct QueryC
16591 6f 64 65 72 20 51 75 65 72 79 43 6f 64 65 72 3b  oder QueryCoder;
16592 0a 73 74 72 75 63 74 20 51 75 65 72 79 43 6f 64  .struct QueryCod
16593 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  er {.  Parse *pP
16594 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54  arse;       /* T
16595 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
16596 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  xt */.  NameCont
16597 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 2f 2a 20  ext *pNC;    /* 
16598 4e 61 6d 65 73 70 61 63 65 20 6f 66 20 66 69 72  Namespace of fir
16599 73 74 20 65 6e 63 6c 6f 73 69 6e 67 20 71 75 65  st enclosing que
1659a 72 79 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a  ry */.};.../*.**
1659b 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
1659c 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 65  or scalar subque
1659d 72 69 65 73 20 75 73 65 64 20 61 73 20 61 6e 20  ries used as an 
1659e 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e  expression.** an
1659f 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20  d IN operators. 
165a0 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a   Examples:.**.**
165a1 20 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 46       (SELECT a F
165a2 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 20  ROM b)          
165a3 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20  -- subquery.**  
165a4 20 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 43     EXISTS (SELEC
165a5 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d  T a FROM b)   --
165a6 20 45 58 49 53 54 53 20 73 75 62 71 75 65 72 79   EXISTS subquery
165a7 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c  .**     x IN (4,
165a8 35 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 20  5,11)           
165a9 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f     -- IN operato
165aa 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72  r with list on r
165ab 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a  ight-hand side.*
165ac 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45  *     x IN (SELE
165ad 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20  CT a FROM b)    
165ae 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   -- IN operator 
165af 77 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e  with subquery on
165b0 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a   the right.**.**
165b1 20 54 68 65 20 70 45 78 70 72 20 70 61 72 61 6d   The pExpr param
165b2 65 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74  eter describes t
165b3 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  he expression th
165b4 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
165b5 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f  IN.** operator o
165b6 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2f 0a 23  r subquery..*/.#
165b7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
165b8 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49  IT_SUBQUERY.SQLI
165b9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
165ba 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65  sqlite3CodeSubse
165bb 6c 65 63 74 28 50 61 72 73 65 20 2a 70 50 61 72  lect(Parse *pPar
165bc 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  se, Expr *pExpr)
165bd 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64 64 72  {.  int testAddr
165be 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
165bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
165c0 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61 64 64  ne-time test add
165c1 72 65 73 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a  ress */.  Vdbe *
165c2 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
165c3 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
165c4 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
165c5 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64  ...  /* This cod
165c6 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20 69 6e  e must be run in
165c7 20 69 74 73 20 65 6e 74 69 72 65 74 79 20 65 76   its entirety ev
165c8 65 72 79 20 74 69 6d 65 20 69 74 20 69 73 20 65  ery time it is e
165c9 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a 2a 20  ncountered.  ** 
165ca 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f  if any of the fo
165cb 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a  llowing is true:
165cc 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20  .  **.  **    * 
165cd 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   The right-hand 
165ce 73 69 64 65 20 69 73 20 61 20 63 6f 72 72 65 6c  side is a correl
165cf 61 74 65 64 20 73 75 62 71 75 65 72 79 0a 20 20  ated subquery.  
165d0 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69 67  **    *  The rig
165d1 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20  ht-hand side is 
165d2 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  an expression li
165d3 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61  st containing va
165d4 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20 20 20  riables.  **    
165d5 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69 64 65  *  We are inside
165d6 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 0a   a trigger.  **.
165d7 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 20 74    ** If all of t
165d8 68 65 20 61 62 6f 76 65 20 61 72 65 20 66 61 6c  he above are fal
165d9 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20  se, then we can 
165da 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 6a 75  run this code ju
165db 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 61 76  st once.  ** sav
165dc 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c 20 61  e the results, a
165dd 6e 64 20 72 65 75 73 65 20 74 68 65 20 73 61 6d  nd reuse the sam
165de 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75 62 73  e result on subs
165df 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f  equent invocatio
165e0 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ns..  */.  if( !
165e1 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
165e2 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72  ty(pExpr, EP_Var
165e3 53 65 6c 65 63 74 29 20 26 26 20 21 70 50 61 72  Select) && !pPar
165e4 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b  se->trigStack ){
165e5 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d 20 70  .    int mem = p
165e6 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20  Parse->nMem++;. 
165e7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
165e8 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61  dOp(v, OP_MemLoa
165e9 64 2c 20 6d 65 6d 2c 20 30 29 3b 0a 20 20 20 20  d, mem, 0);.    
165ea 74 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74  testAddr = sqlit
165eb 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
165ec 50 5f 49 66 2c 20 30 2c 20 30 29 3b 0a 20 20 20  P_If, 0, 0);.   
165ed 20 61 73 73 65 72 74 28 20 74 65 73 74 41 64 64   assert( testAdd
165ee 72 3e 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  r>0 || pParse->d
165ef 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
165f0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
165f1 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
165f2 6d 49 6e 74 2c 20 31 2c 20 6d 65 6d 29 3b 0a 20  mInt, 1, mem);. 
165f3 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45   }..  switch( pE
165f4 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63  xpr->op ){.    c
165f5 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20  ase TK_IN: {.   
165f6 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79     char affinity
165f7 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20  ;.      KeyInfo 
165f8 6b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69  keyInfo;.      i
165f9 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20  nt addr;        
165fa 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50  /* Address of OP
165fb 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69  _OpenEphemeral i
165fc 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20  nstruction */.. 
165fd 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20       affinity = 
165fe 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
165ff 69 74 79 28 70 45 78 70 72 2d 3e 70 4c 65 66 74  ity(pExpr->pLeft
16600 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68 65  );..      /* Whe
16601 74 68 65 72 20 74 68 69 73 20 69 73 20 61 6e 20  ther this is an 
16602 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29  'x IN(SELECT...)
16603 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c 65  ' or an 'x IN(<e
16604 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20 20 20  xprlist>)'.     
16605 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69   ** expression i
16606 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74 68 65  t is handled the
16607 20 73 61 6d 65 20 77 61 79 2e 20 41 20 76 69 72   same way. A vir
16608 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 0a 20  tual table is . 
16609 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64 20 77       ** filled w
1660a 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65 6c 64  ith single-field
1660b 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65 70 72   index keys repr
1660c 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72 65 73  esenting the res
1660d 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 66 72  ults.      ** fr
1660e 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20 6f 72  om the SELECT or
1660f 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74 3e 2e   the <exprlist>.
16610 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
16611 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20 65 78  ** If the 'x' ex
16612 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f  pression is a co
16613 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72 20 74  lumn value, or t
16614 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20 20 20  he SELECT....   
16615 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20     ** statement 
16616 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75 6d 6e  returns a column
16617 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65   value, then the
16618 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61   affinity of tha
16619 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  t.      ** colum
1661a 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62 75 69  n is used to bui
1661b 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79  ld the index key
1661c 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27 20 61  s. If both 'x' a
1661d 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  nd the.      ** 
1661e 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d  SELECT... statem
1661f 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c  ent are columns,
16620 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66   then numeric af
16621 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 0a 20  finity is used. 
16622 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74 68 65       ** if eithe
16623 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 4d  r column has NUM
16624 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45 52 20  ERIC or INTEGER 
16625 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e 65 69  affinity. If nei
16626 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 27 78  ther.      ** 'x
16627 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45 43 54  ' nor the SELECT
16628 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 61 72  ... statement ar
16629 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20  e columns, then 
1662a 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79  numeric affinity
1662b 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75 73 65  .      ** is use
1662c 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
1662d 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1662e 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
1662f 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
16630 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
16631 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
16632 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  ral, pExpr->iTab
16633 6c 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 6d 65  le, 0);.      me
16634 6d 73 65 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30  mset(&keyInfo, 0
16635 2c 20 73 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f  , sizeof(keyInfo
16636 29 29 3b 0a 20 20 20 20 20 20 6b 65 79 49 6e 66  ));.      keyInf
16637 6f 2e 6e 46 69 65 6c 64 20 3d 20 31 3b 0a 20 20  o.nField = 1;.  
16638 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
16639 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 74 4e 75  ddOp(v, OP_SetNu
1663a 6d 43 6f 6c 75 6d 6e 73 2c 20 70 45 78 70 72 2d  mColumns, pExpr-
1663b 3e 69 54 61 62 6c 65 2c 20 31 29 3b 0a 0a 20 20  >iTable, 1);..  
1663c 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70      if( pExpr->p
1663d 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
1663e 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 20 20    /* Case 1:    
1663f 20 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54   expr IN (SELECT
16640 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20 20 2a 2a   ...).        **
16641 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65 6e 65  .        ** Gene
16642 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72 69  rate code to wri
16643 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  te the results o
16644 66 20 74 68 65 20 73 65 6c 65 63 74 20 69 6e 74  f the select int
16645 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 0a  o the temporary.
16646 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65          ** table
16647 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 6f   allocated and o
16648 70 65 6e 65 64 20 61 62 6f 76 65 2e 0a 20 20 20  pened above..   
16649 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1664a 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 45 78 70  int iParm = pExp
1664b 72 2d 3e 69 54 61 62 6c 65 20 2b 20 20 28 28 28  r->iTable +  (((
1664c 69 6e 74 29 61 66 66 69 6e 69 74 79 29 3c 3c 31  int)affinity)<<1
1664d 36 29 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72  6);.        Expr
1664e 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20  List *pEList;.  
1664f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
16650 45 78 70 72 2d 3e 69 54 61 62 6c 65 26 30 78 30  Expr->iTable&0x0
16651 30 30 30 46 46 46 46 29 3d 3d 70 45 78 70 72 2d  000FFFF)==pExpr-
16652 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20  >iTable );.     
16653 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65     if( sqlite3Se
16654 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 45 78  lect(pParse, pEx
16655 70 72 2d 3e 70 53 65 6c 65 63 74 2c 20 53 52 54  pr->pSelect, SRT
16656 5f 53 65 74 2c 20 69 50 61 72 6d 2c 20 30 2c 20  _Set, iParm, 0, 
16657 30 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  0, 0, 0) ){.    
16658 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
16659 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1665a 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  pEList = pExpr->
1665b 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
1665c 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 4c  .        if( pEL
1665d 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e  ist && pEList->n
1665e 45 78 70 72 3e 30 20 29 7b 20 0a 20 20 20 20 20  Expr>0 ){ .     
1665f 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f       keyInfo.aCo
16660 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42  ll[0] = sqlite3B
16661 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
16662 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70  Seq(pParse, pExp
16663 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20  r->pLeft,.      
16664 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e          pEList->
16665 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[0].pExpr);.   
16666 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
16667 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c  se if( pExpr->pL
16668 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ist ){.        /
16669 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20 65 78  * Case 2:     ex
1666a 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29  pr IN (exprlist)
1666b 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
1666c 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20      ** For each 
1666d 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c  expression, buil
1666e 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66  d an index key f
1666f 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69  rom the evaluati
16670 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  on and.        *
16671 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68  * store it in th
16672 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
16673 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20  e. If <expr> is 
16674 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75  a column, then u
16675 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  se.        ** th
16676 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e  at columns affin
16677 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e  ity when buildin
16678 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66  g index keys. If
16679 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20   <expr> is not. 
1667a 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75         ** a colu
1667b 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20  mn, use numeric 
1667c 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20  affinity..      
1667d 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
1667e 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72   i;.        Expr
1667f 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45  List *pList = pE
16680 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20  xpr->pList;.    
16681 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
16682 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
16683 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 61  ..        if( !a
16684 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20  ffinity ){.     
16685 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20       affinity = 
16686 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
16687 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
16688 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c     keyInfo.aColl
16689 5b 30 5d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  [0] = pExpr->pLe
1668a 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 0a 20 20 20 20  ft->pColl;..    
1668b 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f      /* Loop thro
1668c 75 67 68 20 65 61 63 68 20 65 78 70 72 65 73 73  ugh each express
1668d 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69 73 74  ion in <exprlist
1668e 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f  >. */.        fo
1668f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72  r(i=pList->nExpr
16690 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
16691 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
16692 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
16693 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65  Expr *pE2 = pIte
16694 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20  m->pExpr;..     
16695 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65       /* If the e
16696 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74  xpression is not
16697 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77   constant then w
16698 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20  e will need to. 
16699 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61           ** disa
1669a 62 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61  ble the test tha
1669b 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20  t was generated 
1669c 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73  above that makes
1669d 20 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20   sure.          
1669e 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c  ** this code onl
1669f 79 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e  y executes once.
166a0 20 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20    Because for a 
166a1 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20  non-constant.   
166a2 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73         ** expres
166a3 73 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20  sion we need to 
166a4 72 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20  rerun this code 
166a5 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20  each time..     
166a6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
166a7 20 20 69 66 28 20 74 65 73 74 41 64 64 72 3e 30    if( testAddr>0
166a8 20 26 26 20 21 73 71 6c 69 74 65 33 45 78 70 72   && !sqlite3Expr
166a9 49 73 43 6f 6e 73 74 61 6e 74 28 70 45 32 29 20  IsConstant(pE2) 
166aa 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
166ab 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
166ac 54 6f 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41 64  ToNoop(v, testAd
166ad 64 72 2d 31 2c 20 33 29 3b 0a 20 20 20 20 20 20  dr-1, 3);.      
166ae 20 20 20 20 20 20 74 65 73 74 41 64 64 72 20 3d        testAddr =
166af 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   0;.          }.
166b0 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 76  .          /* Ev
166b1 61 6c 75 61 74 65 20 74 68 65 20 65 78 70 72 65  aluate the expre
166b2 73 73 69 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74  ssion and insert
166b3 20 69 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d   it into the tem
166b4 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20  p table */.     
166b5 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
166b6 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 32  Code(pParse, pE2
166b7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
166b8 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
166b9 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 31 2c  P_MakeRecord, 1,
166ba 20 30 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31   0, &affinity, 1
166bb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
166bc 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
166bd 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70   OP_IdxInsert, p
166be 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 29  Expr->iTable, 0)
166bf 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
166c0 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
166c1 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
166c2 20 61 64 64 72 2c 20 28 76 6f 69 64 20 2a 29 26   addr, (void *)&
166c3 6b 65 79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59 49  keyInfo, P3_KEYI
166c4 4e 46 4f 29 3b 0a 20 20 20 20 20 20 62 72 65 61  NFO);.      brea
166c5 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61  k;.    }..    ca
166c6 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20  se TK_EXISTS:.  
166c7 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54    case TK_SELECT
166c8 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69  : {.      /* Thi
166c9 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63  s has to be a sc
166ca 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65  alar SELECT.  Ge
166cb 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70  nerate code to p
166cc 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  ut the.      ** 
166cd 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65  value of this se
166ce 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79  lect in a memory
166cf 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64   cell and record
166d0 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20   the number.    
166d1 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f    ** of the memo
166d2 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75  ry cell in iColu
166d3 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  mn..      */.   
166d4 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
166d5 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 28 75  Token one = { (u
166d6 38 2a 29 22 31 22 2c 20 30 2c 20 31 20 7d 3b 0a  8*)"1", 0, 1 };.
166d7 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53        Select *pS
166d8 65 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d  el;.      int iM
166d9 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 6f  em;.      int so
166da 70 3b 0a 0a 20 20 20 20 20 20 70 45 78 70 72 2d  p;..      pExpr-
166db 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 4d 65 6d 20  >iColumn = iMem 
166dc 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b  = pParse->nMem++
166dd 3b 0a 20 20 20 20 20 20 70 53 65 6c 20 3d 20 70  ;.      pSel = p
166de 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 3b 0a 20  Expr->pSelect;. 
166df 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
166e0 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b  op==TK_SELECT ){
166e1 0a 20 20 20 20 20 20 20 20 73 6f 70 20 3d 20 53  .        sop = S
166e2 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20  RT_Mem;.        
166e3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
166e4 28 76 2c 20 4f 50 5f 4d 65 6d 4e 75 6c 6c 2c 20  (v, OP_MemNull, 
166e5 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20 20 20  iMem, 0);.      
166e6 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
166e7 2c 20 22 23 20 49 6e 69 74 20 73 75 62 71 75 65  , "# Init subque
166e8 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20  ry result"));.  
166e9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
166ea 20 20 20 73 6f 70 20 3d 20 53 52 54 5f 45 78 69     sop = SRT_Exi
166eb 73 74 73 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  sts;.        sql
166ec 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
166ed 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 30 2c 20 69   OP_MemInt, 0, i
166ee 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64  Mem);.        Vd
166ef 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23  beComment((v, "#
166f0 20 49 6e 69 74 20 45 58 49 53 54 53 20 72 65 73   Init EXISTS res
166f1 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a  ult"));.      }.
166f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
166f3 72 44 65 6c 65 74 65 28 70 53 65 6c 2d 3e 70 4c  rDelete(pSel->pL
166f4 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53 65  imit);.      pSe
166f5 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69  l->pLimit = sqli
166f6 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
166f7 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20   TK_INTEGER, 0, 
166f8 30 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20  0, &one);.      
166f9 69 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63  if( sqlite3Selec
166fa 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20  t(pParse, pSel, 
166fb 73 6f 70 2c 20 69 4d 65 6d 2c 20 30 2c 20 30 2c  sop, iMem, 0, 0,
166fc 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20   0, 0) ){.      
166fd 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
166fe 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
166ff 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
16700 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20 20 20  testAddr ){.    
16701 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
16702 65 72 65 28 76 2c 20 74 65 73 74 41 64 64 72 29  ere(v, testAddr)
16703 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 3b  ;.  }..  return;
16704 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
16705 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
16706 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  Y */../*.** Gene
16707 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63 74  rate an instruct
16708 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 75  ion that will pu
16709 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 64 65  t the integer de
1670a 73 63 72 69 62 65 20 62 79 0a 2a 2a 20 74 65 78  scribe by.** tex
1670b 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 6f 6e 20 74  t z[0..n-1] on t
1670c 68 65 20 73 74 61 63 6b 2e 0a 2a 2f 0a 73 74 61  he stack..*/.sta
1670d 74 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74  tic void codeInt
1670e 65 67 65 72 28 56 64 62 65 20 2a 76 2c 20 63 6f  eger(Vdbe *v, co
1670f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
16710 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a   n){.  assert( z
16711 20 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73 71 6c 69   || v==0 || sqli
16712 74 65 33 56 64 62 65 44 62 28 76 29 2d 3e 6d 61  te3VdbeDb(v)->ma
16713 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
16714 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 69 6e 74  if( z ){.    int
16715 20 69 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   i;.    if( sqli
16716 74 65 33 47 65 74 49 6e 74 33 32 28 7a 2c 20 26  te3GetInt32(z, &
16717 69 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i) ){.      sqli
16718 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
16719 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 30  OP_Integer, i, 0
1671a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1671b 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34   sqlite3FitsIn64
1671c 42 69 74 73 28 7a 29 20 29 7b 0a 20 20 20 20 20  Bits(z) ){.     
1671d 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1671e 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20  v, OP_Int64, 0, 
1671f 30 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7d 65  0, z, n);.    }e
16720 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
16721 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f  e3VdbeOp3(v, OP_
16722 52 65 61 6c 2c 20 30 2c 20 30 2c 20 7a 2c 20 6e  Real, 0, 0, z, n
16723 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
16724 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
16725 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65  code that will e
16726 78 74 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75  xtract the iColu
16727 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f  mn-th column fro
16728 6d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20  m.** table pTab 
16729 61 6e 64 20 70 75 73 68 20 74 68 61 74 20 63 6f  and push that co
1672a 6c 75 6d 6e 20 76 61 6c 75 65 20 6f 6e 20 74 68  lumn value on th
1672b 65 20 73 74 61 63 6b 2e 20 20 54 68 65 72 65 0a  e stack.  There.
1672c 2a 2a 20 69 73 20 61 6e 20 6f 70 65 6e 20 63 75  ** is an open cu
1672d 72 73 6f 72 20 74 6f 20 70 54 61 62 20 69 6e 20  rsor to pTab in 
1672e 69 54 61 62 6c 65 2e 20 20 49 66 20 69 43 6f 6c  iTable.  If iCol
1672f 75 6d 6e 3c 30 20 74 68 65 6e 0a 2a 2a 20 63 6f  umn<0 then.** co
16730 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  de is generated 
16731 74 68 61 74 20 65 78 74 72 61 63 74 73 20 74 68  that extracts th
16732 65 20 72 6f 77 69 64 2e 0a 2a 2f 0a 53 51 4c 49  e rowid..*/.SQLI
16733 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
16734 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47  sqlite3ExprCodeG
16735 65 74 43 6f 6c 75 6d 6e 28 56 64 62 65 20 2a 76  etColumn(Vdbe *v
16736 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69  , Table *pTab, i
16737 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69 6e 74 20  nt iColumn, int 
16738 69 54 61 62 6c 65 29 7b 0a 20 20 69 66 28 20 69  iTable){.  if( i
16739 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20  Column<0 ){.    
1673a 69 6e 74 20 6f 70 20 3d 20 28 70 54 61 62 20 26  int op = (pTab &
1673b 26 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  & IsVirtual(pTab
1673c 29 29 20 3f 20 4f 50 5f 56 52 6f 77 69 64 20 3a  )) ? OP_VRowid :
1673d 20 4f 50 5f 52 6f 77 69 64 3b 0a 20 20 20 20 73   OP_Rowid;.    s
1673e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1673f 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 20 30  v, op, iTable, 0
16740 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
16741 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Tab==0 ){.    sq
16742 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
16743 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61  , OP_Column, iTa
16744 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20  ble, iColumn);. 
16745 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
16746 6f 70 20 3d 20 49 73 56 69 72 74 75 61 6c 28 70  op = IsVirtual(p
16747 54 61 62 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d  Tab) ? OP_VColum
16748 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20  n : OP_Column;. 
16749 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1674a 64 4f 70 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c  dOp(v, op, iTabl
1674b 65 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  e, iColumn);.   
1674c 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
1674d 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69  fault(v, pTab, i
1674e 43 6f 6c 75 6d 6e 29 3b 0a 23 69 66 6e 64 65 66  Column);.#ifndef
1674f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
16750 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20  ATING_POINT.    
16751 69 66 28 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  if( pTab->aCol[i
16752 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79  Column].affinity
16753 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  ==SQLITE_AFF_REA
16754 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L ){.      sqlit
16755 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
16756 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20  P_RealAffinity, 
16757 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  0, 0);.    }.#en
16758 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  dif.  }.}../*.**
16759 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69   Generate code i
1675a 6e 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20  nto the current 
1675b 56 64 62 65 20 74 6f 20 65 76 61 6c 75 61 74 65  Vdbe to evaluate
1675c 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78   the given.** ex
1675d 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 6c 65 61  pression and lea
1675e 76 65 20 74 68 65 20 72 65 73 75 6c 74 20 6f 6e  ve the result on
1675f 20 74 68 65 20 74 6f 70 20 6f 66 20 73 74 61 63   the top of stac
16760 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f  k..**.** This co
16761 64 65 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68  de depends on th
16762 65 20 66 61 63 74 20 74 68 61 74 20 63 65 72 74  e fact that cert
16763 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73  ain token values
16764 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20   (ex: TK_EQ).** 
16765 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 73 20  are the same as 
16766 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20 28 65  opcode values (e
16767 78 3a 20 4f 50 5f 45 71 29 20 74 68 61 74 20 69  x: OP_Eq) that i
16768 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f 72  mplement the cor
16769 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70  responding.** op
1676a 65 72 61 74 69 6f 6e 2e 20 20 53 70 65 63 69 61  eration.  Specia
1676b 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64  l comments in vd
1676c 62 65 2e 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f  be.c and the mko
1676d 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70  pcodeh.awk scrip
1676e 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65  t in.** the make
1676f 20 70 72 6f 63 65 73 73 20 63 61 75 73 65 20 74   process cause t
16770 68 65 73 65 20 76 61 6c 75 65 73 20 74 6f 20 61  hese values to a
16771 6c 69 67 6e 2e 20 20 41 73 73 65 72 74 28 29 73  lign.  Assert()s
16772 20 69 6e 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20   in the code.** 
16773 62 65 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61  below verify tha
16774 74 20 74 68 65 20 6e 75 6d 62 65 72 73 20 61 72  t the numbers ar
16775 65 20 61 6c 69 67 6e 65 64 20 63 6f 72 72 65 63  e aligned correc
16776 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tly..*/.SQLITE_P
16777 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
16778 74 65 33 45 78 70 72 43 6f 64 65 28 50 61 72 73  te3ExprCode(Pars
16779 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1677a 2a 70 45 78 70 72 29 7b 0a 20 20 56 64 62 65 20  *pExpr){.  Vdbe 
1677b 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
1677c 62 65 3b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20  be;.  int op;.  
1677d 69 6e 74 20 73 74 61 63 6b 43 68 6e 67 20 3d 20  int stackChng = 
1677e 31 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20  1;    /* Amount 
1677f 6f 66 20 63 68 61 6e 67 65 20 74 6f 20 73 74 61  of change to sta
16780 63 6b 20 64 65 70 74 68 20 2a 2f 0a 0a 20 20 69  ck depth */..  i
16781 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
16782 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  ;.  if( pExpr==0
16783 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
16784 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e  dbeAddOp(v, OP_N
16785 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ull, 0, 0);.    
16786 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6f 70  return;.  }.  op
16787 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20   = pExpr->op;.  
16788 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20  switch( op ){.  
16789 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f    case TK_AGG_CO
1678a 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67  LUMN: {.      Ag
1678b 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20  gInfo *pAggInfo 
1678c 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66  = pExpr->pAggInf
1678d 6f 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  o;.      struct 
1678e 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f  AggInfo_col *pCo
1678f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61  l = &pAggInfo->a
16790 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d  Col[pExpr->iAgg]
16791 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 41 67  ;.      if( !pAg
16792 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64  gInfo->directMod
16793 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
16794 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
16795 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 70 43 6f   OP_MemLoad, pCo
16796 6c 2d 3e 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20  l->iMem, 0);.   
16797 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
16798 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67 67    }else if( pAgg
16799 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e 67  Info->useSorting
1679a 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Idx ){.        s
1679b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1679c 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41  v, OP_Column, pA
1679d 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67 49  ggInfo->sortingI
1679e 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  dx,.            
1679f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
167a0 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
167a1 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20  olumn);.        
167a2 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
167a3 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73       /* Otherwis
167a4 65 2c 20 66 61 6c 6c 20 74 68 72 75 20 69 6e 74  e, fall thru int
167a5 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20  o the TK_COLUMN 
167a6 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  case */.    }.  
167a7 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e    case TK_COLUMN
167a8 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45  : {.      if( pE
167a9 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 7b  xpr->iTable<0 ){
167aa 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
167ab 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68   only happens wh
167ac 65 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 6b 20  en coding check 
167ad 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20  constraints */. 
167ae 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
167af 50 61 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74 3e  Parse->ckOffset>
167b0 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 );.        sql
167b1 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
167b2 20 4f 50 5f 44 75 70 2c 20 70 50 61 72 73 65 2d   OP_Dup, pParse-
167b3 3e 63 6b 4f 66 66 73 65 74 2d 70 45 78 70 72 2d  >ckOffset-pExpr-
167b4 3e 69 43 6f 6c 75 6d 6e 2d 31 2c 20 31 29 3b 0a  >iColumn-1, 1);.
167b5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
167b6 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
167b7 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 76 2c  CodeGetColumn(v,
167b8 20 70 45 78 70 72 2d 3e 70 54 61 62 2c 20 70 45   pExpr->pTab, pE
167b9 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45  xpr->iColumn, pE
167ba 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20  xpr->iTable);.  
167bb 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
167bc 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
167bd 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  e TK_INTEGER: {.
167be 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65        codeIntege
167bf 72 28 76 2c 20 28 63 68 61 72 2a 29 70 45 78 70  r(v, (char*)pExp
167c0 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70  r->token.z, pExp
167c1 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20  r->token.n);.   
167c2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
167c3 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41      case TK_FLOA
167c4 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  T:.    case TK_S
167c5 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 61  TRING: {.      a
167c6 73 73 65 72 74 28 20 54 4b 5f 46 4c 4f 41 54 3d  ssert( TK_FLOAT=
167c7 3d 4f 50 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20  =OP_Real );.    
167c8 20 20 61 73 73 65 72 74 28 20 54 4b 5f 53 54 52    assert( TK_STR
167c9 49 4e 47 3d 3d 4f 50 5f 53 74 72 69 6e 67 38 20  ING==OP_String8 
167ca 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
167cb 44 65 71 75 6f 74 65 45 78 70 72 28 70 50 61 72  DequoteExpr(pPar
167cc 73 65 2d 3e 64 62 2c 20 70 45 78 70 72 29 3b 0a  se->db, pExpr);.
167cd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
167ce 65 4f 70 33 28 76 2c 20 6f 70 2c 20 30 2c 20 30  eOp3(v, op, 0, 0
167cf 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e  , (char*)pExpr->
167d0 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e  token.z, pExpr->
167d1 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 20 20  token.n);.      
167d2 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
167d3 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b   case TK_NULL: {
167d4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
167d5 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 75  beAddOp(v, OP_Nu
167d6 6c 6c 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  ll, 0, 0);.     
167d7 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69   break;.    }.#i
167d8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
167d9 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20  T_BLOB_LITERAL. 
167da 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a     case TK_BLOB:
167db 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a   {.      int n;.
167dc 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
167dd 20 2a 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72   *z;.      asser
167de 74 28 20 54 4b 5f 42 4c 4f 42 3d 3d 4f 50 5f 48  t( TK_BLOB==OP_H
167df 65 78 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20  exBlob );.      
167e0 6e 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  n = pExpr->token
167e1 2e 6e 20 2d 20 33 3b 0a 20 20 20 20 20 20 7a 20  .n - 3;.      z 
167e2 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e  = (char*)pExpr->
167e3 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20  token.z + 2;.   
167e4 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20     assert( n>=0 
167e5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d  );.      if( n==
167e6 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 20 3d  0 ){.        z =
167e7 20 22 22 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   "";.      }.   
167e8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70     sqlite3VdbeOp
167e9 33 28 76 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 7a  3(v, op, 0, 0, z
167ea 2c 20 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , n);.      brea
167eb 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
167ec 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
167ed 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 73 71  ABLE: {.      sq
167ee 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
167ef 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 70  , OP_Variable, p
167f0 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 29  Expr->iTable, 0)
167f1 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  ;.      if( pExp
167f2 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 31 20 29 7b 0a  r->token.n>1 ){.
167f3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
167f4 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 2d  dbeChangeP3(v, -
167f5 31 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d  1, (char*)pExpr-
167f6 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d  >token.z, pExpr-
167f7 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 20  >token.n);.     
167f8 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
167f9 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
167fa 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20 20  K_REGISTER: {.  
167fb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
167fc 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f  ddOp(v, OP_MemLo
167fd 61 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  ad, pExpr->iTabl
167fe 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65  e, 0);.      bre
167ff 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ak;.    }.#ifnde
16800 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
16801 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  ST.    case TK_C
16802 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  AST: {.      /* 
16803 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  Expressions of t
16804 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54 28  he form:   CAST(
16805 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29 20  pLeft AS token) 
16806 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66  */.      int aff
16807 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20 73  , to_op;.      s
16808 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
16809 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1680a 65 66 74 29 3b 0a 20 20 20 20 20 20 61 66 66 20  eft);.      aff 
1680b 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74  = sqlite3Affinit
1680c 79 54 79 70 65 28 26 70 45 78 70 72 2d 3e 74 6f  yType(&pExpr->to
1680d 6b 65 6e 29 3b 0a 20 20 20 20 20 20 74 6f 5f 6f  ken);.      to_o
1680e 70 20 3d 20 61 66 66 20 2d 20 53 51 4c 49 54 45  p = aff - SQLITE
1680f 5f 41 46 46 5f 54 45 58 54 20 2b 20 4f 50 5f 54  _AFF_TEXT + OP_T
16810 6f 54 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73  oText;.      ass
16811 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
16812 6f 54 65 78 74 20 20 20 20 7c 7c 20 61 66 66 21  oText    || aff!
16813 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54  =SQLITE_AFF_TEXT
16814 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73      );.      ass
16815 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
16816 6f 42 6c 6f 62 20 20 20 20 7c 7c 20 61 66 66 21  oBlob    || aff!
16817 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
16818 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73      );.      ass
16819 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
1681a 6f 4e 75 6d 65 72 69 63 20 7c 7c 20 61 66 66 21  oNumeric || aff!
1681b 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
1681c 52 49 43 20 29 3b 0a 20 20 20 20 20 20 61 73 73  RIC );.      ass
1681d 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
1681e 6f 49 6e 74 20 20 20 20 20 7c 7c 20 61 66 66 21  oInt     || aff!
1681f 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45  =SQLITE_AFF_INTE
16820 47 45 52 20 29 3b 0a 20 20 20 20 20 20 61 73 73  GER );.      ass
16821 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
16822 6f 52 65 61 6c 20 20 20 20 7c 7c 20 61 66 66 21  oReal    || aff!
16823 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c  =SQLITE_AFF_REAL
16824 20 20 20 20 29 3b 0a 20 20 20 20 20 20 73 71 6c      );.      sql
16825 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
16826 20 74 6f 5f 6f 70 2c 20 30 2c 20 30 29 3b 0a 20   to_op, 0, 0);. 
16827 20 20 20 20 20 73 74 61 63 6b 43 68 6e 67 20 3d       stackChng =
16828 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   0;.      break;
16829 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
1682a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
1682b 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b  T */.    case TK
1682c 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LT:.    case TK
1682d 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LE:.    case TK
1682e 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GT:.    case TK
1682f 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GE:.    case TK
16830 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _NE:.    case TK
16831 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  _EQ: {.      ass
16832 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c  ert( TK_LT==OP_L
16833 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
16834 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20  t( TK_LE==OP_Le 
16835 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
16836 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b   TK_GT==OP_Gt );
16837 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
16838 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20  K_GE==OP_Ge );. 
16839 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1683a 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20  EQ==OP_Eq );.   
1683b 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45     assert( TK_NE
1683c 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20  ==OP_Ne );.     
1683d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1683e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
1683f 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 73 71  pLeft);.      sq
16840 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
16841 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69  arse, pExpr->pRi
16842 67 68 74 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  ght);.      code
16843 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
16844 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45  pExpr->pLeft, pE
16845 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c  xpr->pRight, op,
16846 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 74   0, 0);.      st
16847 61 63 6b 43 68 6e 67 20 3d 20 2d 31 3b 0a 20 20  ackChng = -1;.  
16848 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
16849 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44  .    case TK_AND
1684a 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52  :.    case TK_OR
1684b 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c  :.    case TK_PL
1684c 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  US:.    case TK_
1684d 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54  STAR:.    case T
1684e 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73  K_MINUS:.    cas
1684f 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61  e TK_REM:.    ca
16850 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20  se TK_BITAND:.  
16851 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a    case TK_BITOR:
16852 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41  .    case TK_SLA
16853 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  SH:.    case TK_
16854 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65  LSHIFT:.    case
16855 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20   TK_RSHIFT: .   
16856 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a   case TK_CONCAT:
16857 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
16858 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20   TK_AND==OP_And 
16859 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1685a 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b   TK_OR==OP_Or );
1685b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1685c 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29  K_PLUS==OP_Add )
1685d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1685e 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62  TK_MINUS==OP_Sub
1685f 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61  tract );.      a
16860 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f  ssert( TK_REM==O
16861 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20  P_Remainder );. 
16862 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
16863 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e  BITAND==OP_BitAn
16864 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  d );.      asser
16865 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f  t( TK_BITOR==OP_
16866 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61  BitOr );.      a
16867 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d  ssert( TK_SLASH=
16868 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20  =OP_Divide );.  
16869 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
1686a 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c  SHIFT==OP_ShiftL
1686b 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  eft );.      ass
1686c 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d  ert( TK_RSHIFT==
1686d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b  OP_ShiftRight );
1686e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1686f 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e  K_CONCAT==OP_Con
16870 63 61 74 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  cat );.      sql
16871 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
16872 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
16873 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
16874 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
16875 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
16876 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
16877 64 62 65 41 64 64 4f 70 28 76 2c 20 6f 70 2c 20  dbeAddOp(v, op, 
16878 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 74 61  0, 0);.      sta
16879 63 6b 43 68 6e 67 20 3d 20 2d 31 3b 0a 20 20 20  ckChng = -1;.   
1687a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1687b 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e      case TK_UMIN
1687c 55 53 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72  US: {.      Expr
1687d 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
1687e 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 61 73  >pLeft;.      as
1687f 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20  sert( pLeft );. 
16880 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e       if( pLeft->
16881 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20  op==TK_FLOAT || 
16882 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  pLeft->op==TK_IN
16883 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20  TEGER ){.       
16884 20 54 6f 6b 65 6e 20 2a 70 20 3d 20 26 70 4c 65   Token *p = &pLe
16885 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20  ft->token;.     
16886 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 73 71 6c     char *z = sql
16887 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72  ite3MPrintf(pPar
16888 73 65 2d 3e 64 62 2c 20 22 2d 25 2e 2a 73 22 2c  se->db, "-%.*s",
16889 20 70 2d 3e 6e 2c 20 70 2d 3e 7a 29 3b 0a 20 20   p->n, p->z);.  
1688a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d        if( pLeft-
1688b 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b  >op==TK_FLOAT ){
1688c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1688d 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f  e3VdbeOp3(v, OP_
1688e 52 65 61 6c 2c 20 30 2c 20 30 2c 20 7a 2c 20 70  Real, 0, 0, z, p
1688f 2d 3e 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20  ->n+1);.        
16890 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
16891 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20   codeInteger(v, 
16892 7a 2c 20 70 2d 3e 6e 2b 31 29 3b 0a 20 20 20 20  z, p->n+1);.    
16893 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
16894 6c 69 74 65 33 5f 66 72 65 65 28 7a 29 3b 0a 20  lite3_free(z);. 
16895 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
16896 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 46      }.      /* F
16897 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f  all through into
16898 20 54 4b 5f 4e 4f 54 20 2a 2f 0a 20 20 20 20 7d   TK_NOT */.    }
16899 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54  .    case TK_BIT
1689a 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  NOT:.    case TK
1689b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 61 73  _NOT: {.      as
1689c 73 65 72 74 28 20 54 4b 5f 42 49 54 4e 4f 54 3d  sert( TK_BITNOT=
1689d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b 0a 20 20  =OP_BitNot );.  
1689e 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e      assert( TK_N
1689f 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20  OT==OP_Not );.  
168a0 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
168a1 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  ode(pParse, pExp
168a2 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20  r->pLeft);.     
168a3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
168a4 70 28 76 2c 20 6f 70 2c 20 30 2c 20 30 29 3b 0a  p(v, op, 0, 0);.
168a5 20 20 20 20 20 20 73 74 61 63 6b 43 68 6e 67 20        stackChng 
168a6 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  = 0;.      break
168a7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
168a8 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20   TK_ISNULL:.    
168a9 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a  case TK_NOTNULL:
168aa 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 73   {.      int des
168ab 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  t;.      assert(
168ac 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49   TK_ISNULL==OP_I
168ad 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61  sNull );.      a
168ae 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c  ssert( TK_NOTNUL
168af 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b  L==OP_NotNull );
168b0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
168b1 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e  beAddOp(v, OP_In
168b2 74 65 67 65 72 2c 20 31 2c 20 30 29 3b 0a 20 20  teger, 1, 0);.  
168b3 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
168b4 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  ode(pParse, pExp
168b5 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20  r->pLeft);.     
168b6 20 64 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56   dest = sqlite3V
168b7 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
168b8 29 20 2b 20 32 3b 0a 20 20 20 20 20 20 73 71 6c  ) + 2;.      sql
168b9 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
168ba 20 6f 70 2c 20 31 2c 20 64 65 73 74 29 3b 0a 20   op, 1, dest);. 
168bb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
168bc 41 64 64 4f 70 28 76 2c 20 4f 50 5f 41 64 64 49  AddOp(v, OP_AddI
168bd 6d 6d 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20  mm, -1, 0);.    
168be 20 20 73 74 61 63 6b 43 68 6e 67 20 3d 20 30 3b    stackChng = 0;
168bf 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
168c0 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
168c1 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a  AGG_FUNCTION: {.
168c2 20 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70        AggInfo *p
168c3 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41  Info = pExpr->pA
168c4 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66  ggInfo;.      if
168c5 28 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20  ( pInfo==0 ){.  
168c6 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
168c7 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d  orMsg(pParse, "m
168c8 69 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61  isuse of aggrega
168c9 74 65 3a 20 25 54 22 2c 0a 20 20 20 20 20 20 20  te: %T",.       
168ca 20 20 20 20 20 26 70 45 78 70 72 2d 3e 73 70 61       &pExpr->spa
168cb 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  n);.      }else{
168cc 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
168cd 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
168ce 4d 65 6d 4c 6f 61 64 2c 20 70 49 6e 66 6f 2d 3e  MemLoad, pInfo->
168cf 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67  aFunc[pExpr->iAg
168d0 67 5d 2e 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20  g].iMem, 0);.   
168d1 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
168d2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
168d3 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a   TK_CONST_FUNC:.
168d4 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43      case TK_FUNC
168d5 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78  TION: {.      Ex
168d6 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  prList *pList = 
168d7 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20  pExpr->pList;.  
168d8 20 20 20 20 69 6e 74 20 6e 45 78 70 72 20 3d 20      int nExpr = 
168d9 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e  pList ? pList->n
168da 45 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20  Expr : 0;.      
168db 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20  FuncDef *pDef;. 
168dc 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20       int nId;.  
168dd 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
168de 7a 49 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 63  zId;.      int c
168df 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 3b 0a 20 20  onstMask = 0;.  
168e0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
168e1 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
168e2 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20  Parse->db;.     
168e3 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62   u8 enc = ENC(db
168e4 29 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  );.      CollSeq
168e5 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 0a 20 20   *pColl = 0;..  
168e6 20 20 20 20 7a 49 64 20 3d 20 28 63 68 61 72 2a      zId = (char*
168e7 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b  )pExpr->token.z;
168e8 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 70 45 78  .      nId = pEx
168e9 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20  pr->token.n;.   
168ea 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65     pDef = sqlite
168eb 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50  3FindFunction(pP
168ec 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e  arse->db, zId, n
168ed 49 64 2c 20 6e 45 78 70 72 2c 20 65 6e 63 2c 20  Id, nExpr, enc, 
168ee 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  0);.      assert
168ef 28 20 70 44 65 66 21 3d 30 20 29 3b 0a 20 20 20  ( pDef!=0 );.   
168f0 20 20 20 6e 45 78 70 72 20 3d 20 73 71 6c 69 74     nExpr = sqlit
168f1 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
168f2 73 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74  st(pParse, pList
168f3 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
168f4 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
168f5 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73  BLE.      /* Pos
168f6 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74  sibly overload t
168f7 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 74  he function if t
168f8 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
168f9 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 20  t is.      ** a 
168fa 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f  virtual table co
168fb 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  lumn..      **. 
168fc 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69       ** For infi
168fd 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b  x functions (LIK
168fe 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c  E, GLOB, REGEXP,
168ff 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65 20   and MATCH) use 
16900 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 63  the.      ** sec
16901 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f  ond argument, no
16902 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73 20  t the first, as 
16903 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  the argument to 
16904 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  test to.      **
16905 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61 20   see if it is a 
16906 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74  column in a virt
16907 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ual table.  This
16908 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73 65   is done because
16909 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65  .      ** the le
1690a 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e  ft operand of in
1690b 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 74  fix functions (t
1690c 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77 61  he operand we wa
1690d 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63  nt to.      ** c
1690e 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69  ontrol overloadi
1690f 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20 74  ng) ends up as t
16910 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
16911 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20  nt to the.      
16912 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  ** function.  Th
16913 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41 20  e expression "A 
16914 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69 76  glob B" is equiv
16915 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20  alent to .      
16916 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20  ** "glob(B,A).  
16917 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74  We want to use t
16918 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62 20  he A in "A glob 
16919 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20 20  B" to test.     
1691a 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e   ** for function
1691b 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42   overloading.  B
1691c 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42 20  ut we use the B 
1691d 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c  term in "glob(B,
1691e 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  A)"..      */.  
1691f 20 20 20 20 69 66 28 20 6e 45 78 70 72 3e 3d 32      if( nExpr>=2
16920 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61 67   && (pExpr->flag
16921 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e 63  s & EP_InfixFunc
16922 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65  ) ){.        pDe
16923 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f  f = sqlite3VtabO
16924 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28  verloadFunction(
16925 64 62 2c 20 70 44 65 66 2c 20 6e 45 78 70 72 2c  db, pDef, nExpr,
16926 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78   pList->a[1].pEx
16927 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  pr);.      }else
16928 20 69 66 28 20 6e 45 78 70 72 3e 30 20 29 7b 0a   if( nExpr>0 ){.
16929 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73          pDef = s
1692a 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
1692b 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70  adFunction(db, p
1692c 44 65 66 2c 20 6e 45 78 70 72 2c 20 70 4c 69 73  Def, nExpr, pLis
1692d 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a  t->a[0].pExpr);.
1692e 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
1692f 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
16930 6e 45 78 70 72 20 26 26 20 69 3c 33 32 3b 20 69  nExpr && i<32; i
16931 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
16932 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
16933 6e 73 74 61 6e 74 28 70 4c 69 73 74 2d 3e 61 5b  nstant(pList->a[
16934 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  i].pExpr) ){.   
16935 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b         constMask
16936 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20   |= (1<<i);.    
16937 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
16938 28 20 70 44 65 66 2d 3e 6e 65 65 64 43 6f 6c 6c  ( pDef->needColl
16939 53 65 71 20 26 26 20 21 70 43 6f 6c 6c 20 29 7b  Seq && !pColl ){
1693a 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  .          pColl
1693b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1693c 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c  llSeq(pParse, pL
1693d 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
1693e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1693f 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 44    }.      if( pD
16940 65 66 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20  ef->needCollSeq 
16941 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ){.        if( !
16942 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d 20  pColl ) pColl = 
16943 70 50 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66 6c  pParse->db->pDfl
16944 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20  tColl; .        
16945 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
16946 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c  , OP_CollSeq, 0,
16947 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c   0, (char *)pCol
16948 6c 2c 20 50 33 5f 43 4f 4c 4c 53 45 51 29 3b 0a  l, P3_COLLSEQ);.
16949 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
1694a 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1694b 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e  OP_Function, con
1694c 73 74 4d 61 73 6b 2c 20 6e 45 78 70 72 2c 20 28  stMask, nExpr, (
1694d 63 68 61 72 2a 29 70 44 65 66 2c 20 50 33 5f 46  char*)pDef, P3_F
1694e 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 20 73  UNCDEF);.      s
1694f 74 61 63 6b 43 68 6e 67 20 3d 20 31 2d 6e 45 78  tackChng = 1-nEx
16950 70 72 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  pr;.      break;
16951 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
16952 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
16953 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ERY.    case TK_
16954 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 73 65  EXISTS:.    case
16955 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20   TK_SELECT: {.  
16956 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69      if( pExpr->i
16957 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
16958 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65       sqlite3Code
16959 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 65  Subselect(pParse
1695a 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  , pExpr);.      
1695b 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
1695c 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
1695d 65 6d 4c 6f 61 64 2c 20 70 45 78 70 72 2d 3e 69  emLoad, pExpr->i
1695e 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20 20 20  Column, 0);.    
1695f 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
16960 2c 20 22 23 20 6c 6f 61 64 20 73 75 62 71 75 65  , "# load subque
16961 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20  ry result"));.  
16962 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
16963 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a  .    case TK_IN:
16964 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64   {.      int add
16965 72 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66  r;.      char af
16966 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 69 6e  finity;.      in
16967 74 20 63 6b 4f 66 66 73 65 74 20 3d 20 70 50 61  t ckOffset = pPa
16968 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74 3b 0a 20  rse->ckOffset;. 
16969 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65       sqlite3Code
1696a 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 65  Subselect(pParse
1696b 2c 20 70 45 78 70 72 29 3b 0a 0a 20 20 20 20 20  , pExpr);..     
1696c 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   /* Figure out t
1696d 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 75  he affinity to u
1696e 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 6b  se to create a k
1696f 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75  ey from the resu
16970 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  lts.      ** of 
16971 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  the expression. 
16972 61 66 66 69 6e 69 74 79 53 74 72 20 73 74 6f 72  affinityStr stor
16973 65 73 20 61 20 73 74 61 74 69 63 20 73 74 72 69  es a static stri
16974 6e 67 20 73 75 69 74 61 62 6c 65 20 66 6f 72 0a  ng suitable for.
16975 20 20 20 20 20 20 2a 2a 20 50 33 20 6f 66 20 4f        ** P3 of O
16976 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20 20  P_MakeRecord..  
16977 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 66 66      */.      aff
16978 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 73  inity = comparis
16979 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  onAffinity(pExpr
1697a 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
1697b 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1697c 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 30 29 3b  _Integer, 1, 0);
1697d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63  .      pParse->c
1697e 6b 4f 66 66 73 65 74 20 3d 20 28 63 6b 4f 66 66  kOffset = (ckOff
1697f 73 65 74 20 3f 20 28 63 6b 4f 66 66 73 65 74 2b  set ? (ckOffset+
16980 31 29 20 3a 20 30 29 3b 0a 0a 20 20 20 20 20 20  1) : 0);..      
16981 2f 2a 20 43 6f 64 65 20 74 68 65 20 3c 65 78 70  /* Code the <exp
16982 72 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20  r> from "<expr> 
16983 49 4e 20 28 2e 2e 2e 29 22 2e 20 54 68 65 20 74  IN (...)". The t
16984 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 20  emporary table. 
16985 20 20 20 20 20 2a 2a 20 70 45 78 70 72 2d 3e 69       ** pExpr->i
16986 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74  Table contains t
16987 68 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d  he values that m
16988 61 6b 65 20 75 70 20 74 68 65 20 28 2e 2e 2e 29  ake up the (...)
16989 20 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20   set..      */. 
1698a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1698b 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78  Code(pParse, pEx
1698c 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20  pr->pLeft);.    
1698d 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
1698e 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
1698f 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
16990 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
16991 5f 4e 6f 74 4e 75 6c 6c 2c 20 2d 31 2c 20 61 64  _NotNull, -1, ad
16992 64 72 2b 34 29 3b 20 20 20 20 20 20 20 20 20 20  dr+4);          
16993 20 20 2f 2a 20 61 64 64 72 20 2b 20 30 20 2a 2f    /* addr + 0 */
16994 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
16995 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f  beAddOp(v, OP_Po
16996 70 2c 20 32 2c 20 30 29 3b 0a 20 20 20 20 20 20  p, 2, 0);.      
16997 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
16998 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
16999 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
1699a 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1699b 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2b 37  _Goto, 0, addr+7
1699c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1699d 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  VdbeOp3(v, OP_Ma
1699e 6b 65 52 65 63 6f 72 64 2c 20 31 2c 20 30 2c 20  keRecord, 1, 0, 
1699f 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 20 20  &affinity, 1);  
169a0 20 2f 2a 20 61 64 64 72 20 2b 20 34 20 2a 2f 0a   /* addr + 4 */.
169a1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
169a2 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46 6f 75  eAddOp(v, OP_Fou
169a3 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  nd, pExpr->iTabl
169a4 65 2c 20 61 64 64 72 2b 37 29 3b 0a 20 20 20 20  e, addr+7);.    
169a5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
169a6 4f 70 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  Op(v, OP_AddImm,
169a7 20 2d 31 2c 20 30 29 3b 20 20 20 20 20 20 20 20   -1, 0);        
169a8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 64 64            /* add
169a9 72 20 2b 20 36 20 2a 2f 0a 0a 20 20 20 20 20 20  r + 6 */..      
169aa 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
169ab 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  dif.    case TK_
169ac 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20  BETWEEN: {.     
169ad 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70   Expr *pLeft = p
169ae 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20  Expr->pLeft;.   
169af 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
169b0 73 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65 6d 20  st_item *pLItem 
169b1 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  = pExpr->pList->
169b2 61 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  a;.      Expr *p
169b3 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e  Right = pLItem->
169b4 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c  pExpr;.      sql
169b5 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
169b6 72 73 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20  rse, pLeft);.   
169b7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
169b8 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 30  dOp(v, OP_Dup, 0
169b9 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
169ba 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
169bb 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20  se, pRight);.   
169bc 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70     codeCompare(p
169bd 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52  Parse, pLeft, pR
169be 69 67 68 74 2c 20 4f 50 5f 47 65 2c 20 30 2c 20  ight, OP_Ge, 0, 
169bf 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
169c0 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
169c1 5f 50 75 6c 6c 2c 20 31 2c 20 30 29 3b 0a 20 20  _Pull, 1, 0);.  
169c2 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20      pLItem++;.  
169c3 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c 49      pRight = pLI
169c4 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  tem->pExpr;.    
169c5 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
169c6 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  e(pParse, pRight
169c7 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
169c8 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65  pare(pParse, pLe
169c9 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c  ft, pRight, OP_L
169ca 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  e, 0, 0);.      
169cb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
169cc 28 76 2c 20 4f 50 5f 41 6e 64 2c 20 30 2c 20 30  (v, OP_And, 0, 0
169cd 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
169ce 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
169cf 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20  K_UPLUS: {.     
169d0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
169d1 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
169d2 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 73 74  pLeft);.      st
169d3 61 63 6b 43 68 6e 67 20 3d 20 30 3b 0a 20 20 20  ackChng = 0;.   
169d4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
169d5 20 20 20 20 63 61 73 65 20 54 4b 5f 43 41 53 45      case TK_CASE
169d6 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78  : {.      int ex
169d7 70 72 5f 65 6e 64 5f 6c 61 62 65 6c 3b 0a 20 20  pr_end_label;.  
169d8 20 20 20 20 69 6e 74 20 6a 75 6d 70 49 6e 73 74      int jumpInst
169d9 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70  ;.      int nExp
169da 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  r;.      int i;.
169db 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
169dc 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 73 74  pEList;.      st
169dd 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
169de 65 6d 20 2a 61 4c 69 73 74 65 6c 65 6d 3b 0a 0a  em *aListelem;..
169df 20 20 20 20 20 20 61 73 73 65 72 74 28 70 45 78        assert(pEx
169e0 70 72 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 20 20  pr->pList);.    
169e1 20 20 61 73 73 65 72 74 28 28 70 45 78 70 72 2d    assert((pExpr-
169e2 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 25 20  >pList->nExpr % 
169e3 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 20 20 20  2) == 0);.      
169e4 61 73 73 65 72 74 28 70 45 78 70 72 2d 3e 70 4c  assert(pExpr->pL
169e5 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b  ist->nExpr > 0);
169e6 0a 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20  .      pEList = 
169e7 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20  pExpr->pList;.  
169e8 20 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20      aListelem = 
169e9 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20  pEList->a;.     
169ea 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d   nExpr = pEList-
169eb 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 65 78  >nExpr;.      ex
169ec 70 72 5f 65 6e 64 5f 6c 61 62 65 6c 20 3d 20 73  pr_end_label = s
169ed 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
169ee 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66  bel(v);.      if
169ef 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 29  ( pExpr->pLeft )
169f0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
169f1 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
169f2 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  , pExpr->pLeft);
169f3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
169f4 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b  or(i=0; i<nExpr;
169f5 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 20   i=i+2){.       
169f6 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
169f7 28 70 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c  (pParse, aListel
169f8 65 6d 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20  em[i].pExpr);.  
169f9 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
169fa 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  >pLeft ){.      
169fb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
169fc 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20  ddOp(v, OP_Dup, 
169fd 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20  1, 1);.         
169fe 20 6a 75 6d 70 49 6e 73 74 20 3d 20 63 6f 64 65   jumpInst = code
169ff 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
16a00 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 61 4c  pExpr->pLeft, aL
16a01 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72  istelem[i].pExpr
16a02 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
16a03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a04 20 20 20 4f 50 5f 4e 65 2c 20 30 2c 20 31 29 3b     OP_Ne, 0, 1);
16a05 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
16a06 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
16a07 50 5f 50 6f 70 2c 20 31 2c 20 30 29 3b 0a 20 20  P_Pop, 1, 0);.  
16a08 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
16a09 20 20 20 20 20 20 20 6a 75 6d 70 49 6e 73 74 20         jumpInst 
16a0a 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
16a0b 4f 70 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20  Op(v, OP_IfNot, 
16a0c 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d  1, 0);.        }
16a0d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
16a0e 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
16a0f 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e   aListelem[i+1].
16a10 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  pExpr);.        
16a11 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
16a12 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
16a13 65 78 70 72 5f 65 6e 64 5f 6c 61 62 65 6c 29 3b  expr_end_label);
16a14 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
16a15 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
16a16 6a 75 6d 70 49 6e 73 74 29 3b 0a 20 20 20 20 20  jumpInst);.     
16a17 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78   }.      if( pEx
16a18 70 72 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20  pr->pLeft ){.   
16a19 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
16a1a 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c  AddOp(v, OP_Pop,
16a1b 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a   1, 0);.      }.
16a1c 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
16a1d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  >pRight ){.     
16a1e 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
16a1f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
16a20 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20  ->pRight);.     
16a21 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
16a22 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
16a23 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
16a24 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
16a25 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
16a26 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 78 70  olveLabel(v, exp
16a27 72 5f 65 6e 64 5f 6c 61 62 65 6c 29 3b 0a 20 20  r_end_label);.  
16a28 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
16a29 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
16a2a 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
16a2b 20 63 61 73 65 20 54 4b 5f 52 41 49 53 45 3a 20   case TK_RAISE: 
16a2c 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 61  {.      if( !pPa
16a2d 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29  rse->trigStack )
16a2e 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
16a2f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
16a30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
16a31 20 20 20 20 20 20 20 20 20 22 52 41 49 53 45 28           "RAISE(
16a32 29 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73  ) may only be us
16a33 65 64 20 77 69 74 68 69 6e 20 61 20 74 72 69 67  ed within a trig
16a34 67 65 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20  ger-program");. 
16a35 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
16a36 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
16a37 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21   pExpr->iColumn!
16a38 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20  =OE_Ignore ){.  
16a39 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
16a3a 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 4f  Expr->iColumn==O
16a3b 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 0a 20 20  E_Rollback ||.  
16a3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
16a3d 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 3d  Expr->iColumn ==
16a3e 20 4f 45 5f 41 62 6f 72 74 20 7c 7c 0a 20 20 20   OE_Abort ||.   
16a3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
16a40 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 3d 20  xpr->iColumn == 
16a41 4f 45 5f 46 61 69 6c 20 29 3b 0a 20 20 20 20 20  OE_Fail );.     
16a42 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f      sqlite3Dequo
16a43 74 65 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64  teExpr(pParse->d
16a44 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20  b, pExpr);.     
16a45 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f      sqlite3VdbeO
16a46 70 33 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p3(v, OP_Halt, S
16a47 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
16a48 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  , pExpr->iColumn
16a49 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
16a4a 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
16a4b 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c  )pExpr->token.z,
16a4c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29   pExpr->token.n)
16a4d 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b  ;.      } else {
16a4e 0a 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74  .         assert
16a4f 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
16a50 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72 65 20 29 3b   == OE_Ignore );
16a51 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
16a52 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
16a53 5f 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30 2c 20  _ContextPop, 0, 
16a54 30 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c  0);.         sql
16a55 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
16a56 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61   OP_Goto, 0, pPa
16a57 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e  rse->trigStack->
16a58 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20 20 20  ignoreJump);.   
16a59 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
16a5a 74 28 28 76 2c 20 22 23 20 72 61 69 73 65 28 49  t((v, "# raise(I
16a5b 47 4e 4f 52 45 29 22 29 29 3b 0a 20 20 20 20 20  GNORE)"));.     
16a5c 20 7d 0a 20 20 20 20 20 20 73 74 61 63 6b 43 68   }.      stackCh
16a5d 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72  ng = 0;.      br
16a5e 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
16a5f 66 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61  f.  }..  if( pPa
16a60 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74 20 29 7b  rse->ckOffset ){
16a61 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6b 4f  .    pParse->ckO
16a62 66 66 73 65 74 20 2b 3d 20 73 74 61 63 6b 43 68  ffset += stackCh
16a63 6e 67 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ng;.    assert( 
16a64 70 50 61 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74  pParse->ckOffset
16a65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64   );.  }.}..#ifnd
16a66 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
16a67 52 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20 47 65 6e  RIGGER./*.** Gen
16a68 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
16a69 65 76 61 6c 75 74 65 73 20 74 68 65 20 67 69 76  evalutes the giv
16a6a 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  en expression an
16a6b 64 20 6c 65 61 76 65 73 20 74 68 65 20 72 65 73  d leaves the res
16a6c 75 6c 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 74  ult.** on the st
16a6d 61 63 6b 2e 20 20 53 65 65 20 61 6c 73 6f 20 73  ack.  See also s
16a6e 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 29  qlite3ExprCode()
16a6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
16a70 74 69 6e 65 20 6d 69 67 68 74 20 61 6c 73 6f 20  tine might also 
16a71 63 61 63 68 65 20 74 68 65 20 72 65 73 75 6c 74  cache the result
16a72 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20   and modify the 
16a73 70 45 78 70 72 20 74 72 65 65 0a 2a 2a 20 73 6f  pExpr tree.** so
16a74 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 6d 61   that it will ma
16a75 6b 65 20 75 73 65 20 6f 66 20 74 68 65 20 63 61  ke use of the ca
16a76 63 68 65 64 20 72 65 73 75 6c 74 20 6f 6e 20 73  ched result on s
16a77 75 62 73 65 71 75 65 6e 74 20 65 76 61 6c 75 61  ubsequent evalua
16a78 74 69 6f 6e 73 0a 2a 2a 20 72 61 74 68 65 72 20  tions.** rather 
16a79 74 68 61 6e 20 65 76 61 6c 75 61 74 65 20 74 68  than evaluate th
16a7a 65 20 77 68 6f 6c 65 20 65 78 70 72 65 73 73 69  e whole expressi
16a7b 6f 6e 20 61 67 61 69 6e 2e 20 20 54 72 69 76 69  on again.  Trivi
16a7c 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  al expressions a
16a7d 72 65 0a 2a 2a 20 6e 6f 74 20 63 61 63 68 65 64  re.** not cached
16a7e 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73  .  If the expres
16a7f 73 69 6f 6e 20 69 73 20 63 61 63 68 65 64 2c 20  sion is cached, 
16a80 69 74 73 20 72 65 73 75 6c 74 20 69 73 20 73 74  its result is st
16a81 6f 72 65 64 20 69 6e 20 61 20 0a 2a 2a 20 6d 65  ored in a .** me
16a82 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a  mory location..*
16a83 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16a84 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
16a85 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61  rCodeAndCache(Pa
16a86 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
16a87 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 56 64 62  r *pExpr){.  Vdb
16a88 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
16a89 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 4d 65 6d  Vdbe;.  int iMem
16a8a 3b 0a 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61  ;.  int addr1, a
16a8b 64 64 72 32 3b 0a 20 20 69 66 28 20 76 3d 3d 30  ddr2;.  if( v==0
16a8c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 64 64   ) return;.  add
16a8d 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
16a8e 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
16a8f 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
16a90 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 29  e(pParse, pExpr)
16a91 3b 0a 20 20 61 64 64 72 32 20 3d 20 73 71 6c 69  ;.  addr2 = sqli
16a92 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
16a93 64 72 28 76 29 3b 0a 20 20 69 66 28 20 61 64 64  dr(v);.  if( add
16a94 72 32 3e 61 64 64 72 31 2b 31 20 7c 7c 20 73 71  r2>addr1+1 || sq
16a95 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
16a96 2c 20 61 64 64 72 31 29 2d 3e 6f 70 63 6f 64 65  , addr1)->opcode
16a97 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 29 7b  ==OP_Function ){
16a98 0a 20 20 20 20 69 4d 65 6d 20 3d 20 70 45 78 70  .    iMem = pExp
16a99 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 50 61 72  r->iTable = pPar
16a9a 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20  se->nMem++;.    
16a9b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
16a9c 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 2c  (v, OP_MemStore,
16a9d 20 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20 70   iMem, 0);.    p
16a9e 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45  Expr->op = TK_RE
16a9f 47 49 53 54 45 52 3b 0a 20 20 7d 0a 7d 0a 23 65  GISTER;.  }.}.#e
16aa0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  ndif../*.** Gene
16aa1 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 70  rate code that p
16aa2 75 73 68 65 73 20 74 68 65 20 76 61 6c 75 65 20  ushes the value 
16aa3 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74  of every element
16aa4 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a   of the given.**
16aa5 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
16aa6 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e   onto the stack.
16aa7 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
16aa8 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  e number of elem
16aa9 65 6e 74 73 20 70 75 73 68 65 64 20 6f 6e 74 6f  ents pushed onto
16aaa 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f 0a 53   the stack..*/.S
16aab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16aac 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
16aad 65 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72  eExprList(.  Par
16aae 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
16aaf 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
16ab0 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
16ab1 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a 20 54 68   *pList    /* Th
16ab2 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
16ab3 74 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f  t to be coded */
16ab4 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70  .){.  struct Exp
16ab5 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
16ab6 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20  m;.  int i, n;. 
16ab7 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
16ab8 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 20 3d 20  return 0;.  n = 
16ab9 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  pList->nExpr;.  
16aba 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d  for(pItem=pList-
16abb 3e 61 2c 20 69 3d 6e 3b 20 69 3e 30 3b 20 69 2d  >a, i=n; i>0; i-
16abc 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  -, pItem++){.   
16abd 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
16abe 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
16abf 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20 72 65  pExpr);.  }.  re
16ac0 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
16ac1 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
16ac2 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70  or a boolean exp
16ac3 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61  ression such tha
16ac4 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65  t a jump is made
16ac5 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c  .** to the label
16ac6 20 22 64 65 73 74 22 20 69 66 20 74 68 65 20 65   "dest" if the e
16ac7 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75  xpression is tru
16ac8 65 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e 0a  e but execution.
16ac9 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74 72  ** continues str
16aca 61 69 67 68 74 20 74 68 72 75 20 69 66 20 74 68  aight thru if th
16acb 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
16acc 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  false..**.** If 
16acd 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65  the expression e
16ace 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c  valuates to NULL
16acf 20 28 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e   (neither true n
16ad0 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a  or false), then.
16ad1 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  ** take the jump
16ad2 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66 4e 75   if the jumpIfNu
16ad3 6c 6c 20 66 6c 61 67 20 69 73 20 74 72 75 65 2e  ll flag is true.
16ad4 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65  .**.** This code
16ad5 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20   depends on the 
16ad6 66 61 63 74 20 74 68 61 74 20 63 65 72 74 61 69  fact that certai
16ad7 6e 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28  n token values (
16ad8 65 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72  ex: TK_EQ).** ar
16ad9 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 6f 70  e the same as op
16ada 63 6f 64 65 20 76 61 6c 75 65 73 20 28 65 78 3a  code values (ex:
16adb 20 4f 50 5f 45 71 29 20 74 68 61 74 20 69 6d 70   OP_Eq) that imp
16adc 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f 72 72 65  lement the corre
16add 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72  sponding.** oper
16ade 61 74 69 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20  ation.  Special 
16adf 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64 62 65  comments in vdbe
16ae0 2e 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63  .c and the mkopc
16ae1 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20  odeh.awk script 
16ae2 69 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70  in.** the make p
16ae3 72 6f 63 65 73 73 20 63 61 75 73 65 20 74 68 65  rocess cause the
16ae4 73 65 20 76 61 6c 75 65 73 20 74 6f 20 61 6c 69  se values to ali
16ae5 67 6e 2e 20 20 41 73 73 65 72 74 28 29 73 20 69  gn.  Assert()s i
16ae6 6e 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65  n the code.** be
16ae7 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20  low verify that 
16ae8 74 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20  the numbers are 
16ae9 61 6c 69 67 6e 65 64 20 63 6f 72 72 65 63 74 6c  aligned correctl
16aea 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
16aeb 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
16aec 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73  3ExprIfTrue(Pars
16aed 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
16aee 2a 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74  *pExpr, int dest
16aef 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c  , int jumpIfNull
16af0 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
16af1 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
16af2 69 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e  int op = 0;.  in
16af3 74 20 63 6b 4f 66 66 73 65 74 20 3d 20 70 50 61  t ckOffset = pPa
16af4 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74 3b 0a 20  rse->ckOffset;. 
16af5 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20 70 45 78   if( v==0 || pEx
16af6 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  pr==0 ) return;.
16af7 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70    op = pExpr->op
16af8 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  ;.  switch( op )
16af9 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e  {.    case TK_AN
16afa 44 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  D: {.      int d
16afb 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  2 = sqlite3VdbeM
16afc 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
16afd 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
16afe 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45  False(pParse, pE
16aff 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 32 2c 20  xpr->pLeft, d2, 
16b00 21 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20  !jumpIfNull);.  
16b01 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
16b02 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45  fTrue(pParse, pE
16b03 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73  xpr->pRight, des
16b04 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
16b05 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
16b06 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
16b07 20 64 32 29 3b 0a 20 20 20 20 20 20 62 72 65 61   d2);.      brea
16b08 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
16b09 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  e TK_OR: {.     
16b0a 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72   sqlite3ExprIfTr
16b0b 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ue(pParse, pExpr
16b0c 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a  ->pLeft, dest, j
16b0d 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
16b0e 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54    sqlite3ExprIfT
16b0f 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  rue(pParse, pExp
16b10 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c  r->pRight, dest,
16b11 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
16b12 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
16b13 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
16b14 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
16b15 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
16b16 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
16b17 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
16b18 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ull);.      brea
16b19 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
16b1a 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73  e TK_LT:.    cas
16b1b 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73  e TK_LE:.    cas
16b1c 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73  e TK_GT:.    cas
16b1d 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73  e TK_GE:.    cas
16b1e 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73  e TK_NE:.    cas
16b1f 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20  e TK_EQ: {.     
16b20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d   assert( TK_LT==
16b21 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 61  OP_Lt );.      a
16b22 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50  ssert( TK_LE==OP
16b23 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Le );.      ass
16b24 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47  ert( TK_GT==OP_G
16b25 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
16b26 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20  t( TK_GE==OP_Ge 
16b27 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
16b28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b   TK_EQ==OP_Eq );
16b29 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
16b2a 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20  K_NE==OP_Ne );. 
16b2b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
16b2c 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78  Code(pParse, pEx
16b2d 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20  pr->pLeft);.    
16b2e 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
16b2f 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
16b30 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  >pRight);.      
16b31 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
16b32 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
16b33 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
16b34 20 6f 70 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49   op, dest, jumpI
16b35 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72  fNull);.      br
16b36 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
16b37 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20  ase TK_ISNULL:. 
16b38 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55     case TK_NOTNU
16b39 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  LL: {.      asse
16b3a 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f  rt( TK_ISNULL==O
16b3b 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  P_IsNull );.    
16b3c 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54    assert( TK_NOT
16b3d 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c  NULL==OP_NotNull
16b3e 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
16b3f 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
16b40 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  , pExpr->pLeft);
16b41 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
16b42 62 65 41 64 64 4f 70 28 76 2c 20 6f 70 2c 20 31  beAddOp(v, op, 1
16b43 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 62  , dest);.      b
16b44 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
16b45 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
16b46 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20   {.      /* The 
16b47 65 78 70 72 65 73 73 69 6f 6e 20 22 78 20 42 45  expression "x BE
16b48 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 22 20 69  TWEEN y AND z" i
16b49 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73  s implemented as
16b4a 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  :.      **.     
16b4b 20 2a 2a 20 31 20 49 46 20 28 78 20 3c 20 79 29   ** 1 IF (x < y)
16b4c 20 47 4f 54 4f 20 33 0a 20 20 20 20 20 20 2a 2a   GOTO 3.      **
16b4d 20 32 20 49 46 20 28 78 20 3c 3d 20 7a 29 20 47   2 IF (x <= z) G
16b4e 4f 54 4f 20 3c 64 65 73 74 3e 0a 20 20 20 20 20  OTO <dest>.     
16b4f 20 2a 2a 20 33 20 2e 2e 2e 0a 20 20 20 20 20 20   ** 3 ....      
16b50 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
16b51 72 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  r;.      Expr *p
16b52 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
16b53 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20  eft;.      Expr 
16b54 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  *pRight = pExpr-
16b55 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  >pList->a[0].pEx
16b56 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  pr;.      sqlite
16b57 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
16b58 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  , pLeft);.      
16b59 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
16b5a 28 76 2c 20 4f 50 5f 44 75 70 2c 20 30 2c 20 30  (v, OP_Dup, 0, 0
16b5b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
16b5c 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
16b5d 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20   pRight);.      
16b5e 61 64 64 72 20 3d 20 63 6f 64 65 43 6f 6d 70 61  addr = codeCompa
16b5f 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  re(pParse, pLeft
16b60 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c 74 2c  , pRight, OP_Lt,
16b61 20 30 2c 20 21 6a 75 6d 70 49 66 4e 75 6c 6c 29   0, !jumpIfNull)
16b62 3b 0a 0a 20 20 20 20 20 20 70 52 69 67 68 74 20  ;..      pRight 
16b63 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  = pExpr->pList->
16b64 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[1].pExpr;.    
16b65 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
16b66 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  e(pParse, pRight
16b67 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
16b68 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65  pare(pParse, pLe
16b69 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c  ft, pRight, OP_L
16b6a 65 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  e, dest, jumpIfN
16b6b 75 6c 6c 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c  ull);..      sql
16b6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
16b6d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
16b6e 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
16b6f 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
16b70 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 73 71   addr);.      sq
16b71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
16b72 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20 30 29 3b  , OP_Pop, 1, 0);
16b73 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
16b74 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
16b75 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
16b76 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
16b77 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 73   pExpr);.      s
16b78 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
16b79 76 2c 20 4f 50 5f 49 66 2c 20 6a 75 6d 70 49 66  v, OP_If, jumpIf
16b7a 4e 75 6c 6c 2c 20 64 65 73 74 29 3b 0a 20 20 20  Null, dest);.   
16b7b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
16b7c 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 63 6b    }.  pParse->ck
16b7d 4f 66 66 73 65 74 20 3d 20 63 6b 4f 66 66 73 65  Offset = ckOffse
16b7e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  t;.}../*.** Gene
16b7f 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20  rate code for a 
16b80 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69  boolean expressi
16b81 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20 6a  on such that a j
16b82 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74  ump is made.** t
16b83 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65 73  o the label "des
16b84 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65 73  t" if the expres
16b85 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62 75  sion is false bu
16b86 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63  t execution.** c
16b87 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 68  ontinues straigh
16b88 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 78  t thru if the ex
16b89 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65  pression is true
16b8a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
16b8b 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61  xpression evalua
16b8c 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69  tes to NULL (nei
16b8d 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66 61  ther true nor fa
16b8e 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d  lse) then.** jum
16b8f 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 20  p if jumpIfNull 
16b90 69 73 20 74 72 75 65 20 6f 72 20 66 61 6c 6c 20  is true or fall 
16b91 74 68 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49  through if jumpI
16b92 66 4e 75 6c 6c 20 69 73 20 66 61 6c 73 65 2e 0a  fNull is false..
16b93 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16b94 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
16b95 70 72 49 66 46 61 6c 73 65 28 50 61 72 73 65 20  prIfFalse(Parse 
16b96 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
16b97 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c 20  Expr, int dest, 
16b98 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b  int jumpIfNull){
16b99 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
16b9a 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
16b9b 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20  t op = 0;.  int 
16b9c 63 6b 4f 66 66 73 65 74 20 3d 20 70 50 61 72 73  ckOffset = pPars
16b9d 65 2d 3e 63 6b 4f 66 66 73 65 74 3b 0a 20 20 69  e->ckOffset;.  i
16b9e 66 28 20 76 3d 3d 30 20 7c 7c 20 70 45 78 70 72  f( v==0 || pExpr
16b9f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  ==0 ) return;.. 
16ba0 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66   /* The value of
16ba1 20 70 45 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f   pExpr->op and o
16ba2 70 20 61 72 65 20 72 65 6c 61 74 65 64 20 61 73  p are related as
16ba3 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20   follows:.  **. 
16ba4 20 2a 2a 20 20 20 20 20 20 20 70 45 78 70 72 2d   **       pExpr-
16ba5 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 6f  >op            o
16ba6 70 0a 20 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d  p.  **       ---
16ba7 2d 2d 2d 2d 2d 2d 20 20 20 20 20 20 20 20 20 20  ------          
16ba8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20  ----------.  ** 
16ba9 20 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20        TK_ISNULL 
16baa 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e           OP_NotN
16bab 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  ull.  **       T
16bac 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20  K_NOTNULL       
16bad 20 20 4f 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a    OP_IsNull.  **
16bae 20 20 20 20 20 20 20 54 4b 5f 4e 45 20 20 20 20         TK_NE    
16baf 20 20 20 20 20 20 20 20 20 20 4f 50 5f 45 71 0a            OP_Eq.
16bb0 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 45 51    **       TK_EQ
16bb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
16bb2 5f 4e 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  _Ne.  **       T
16bb3 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20  K_GT            
16bb4 20 20 4f 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20    OP_Le.  **    
16bb5 20 20 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20     TK_LE        
16bb6 20 20 20 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a        OP_Gt.  **
16bb7 20 20 20 20 20 20 20 54 4b 5f 47 45 20 20 20 20         TK_GE    
16bb8 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a            OP_Lt.
16bb9 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54    **       TK_LT
16bba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
16bbb 5f 47 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f  _Ge.  **.  ** Fo
16bbc 72 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 6f  r other values o
16bbd 66 20 70 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20  f pExpr->op, op 
16bbe 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  is undefined and
16bbf 20 75 6e 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68   unused..  ** Th
16bc0 65 20 76 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61  e value of TK_ a
16bc1 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73  nd OP_ constants
16bc2 20 61 72 65 20 61 72 72 61 6e 67 65 64 20 73 75   are arranged su
16bc3 63 68 20 74 68 61 74 20 77 65 0a 20 20 2a 2a 20  ch that we.  ** 
16bc4 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 20  can compute the 
16bc5 6d 61 70 70 69 6e 67 20 61 62 6f 76 65 20 75 73  mapping above us
16bc6 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ing the followin
16bc7 67 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20  g expression..  
16bc8 2a 2a 20 41 73 73 65 72 74 28 29 73 20 76 65 72  ** Assert()s ver
16bc9 69 66 79 20 74 68 61 74 20 74 68 65 20 63 6f 6d  ify that the com
16bca 70 75 74 61 74 69 6f 6e 20 69 73 20 63 6f 72 72  putation is corr
16bcb 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d  ect..  */.  op =
16bcc 20 28 28 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b   ((pExpr->op+(TK
16bcd 5f 49 53 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28  _ISNULL&1))^1)-(
16bce 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20  TK_ISNULL&1);.. 
16bcf 20 2f 2a 20 56 65 72 69 66 79 20 63 6f 72 72 65   /* Verify corre
16bd0 63 74 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20  ct alignment of 
16bd1 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73  TK_ and OP_ cons
16bd2 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73  tants.  */.  ass
16bd3 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
16bd4 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d  TK_ISNULL || op=
16bd5 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20  =OP_NotNull );. 
16bd6 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
16bd7 6f 70 21 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c  op!=TK_NOTNULL |
16bd8 7c 20 6f 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20  | op==OP_IsNull 
16bd9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
16bda 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c  pr->op!=TK_NE ||
16bdb 20 6f 70 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20   op==OP_Eq );.  
16bdc 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
16bdd 70 21 3d 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d  p!=TK_EQ || op==
16bde 4f 50 5f 4e 65 20 29 3b 0a 20 20 61 73 73 65 72  OP_Ne );.  asser
16bdf 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
16be0 5f 4c 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65  _LT || op==OP_Ge
16be1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
16be2 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c  xpr->op!=TK_LE |
16be3 7c 20 6f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20  | op==OP_Gt );. 
16be4 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
16be5 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d  op!=TK_GT || op=
16be6 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65  =OP_Le );.  asse
16be7 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
16be8 4b 5f 47 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c  K_GE || op==OP_L
16be9 74 20 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  t );..  switch( 
16bea 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20  pExpr->op ){.   
16beb 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a   case TK_AND: {.
16bec 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
16bed 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
16bee 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64   pExpr->pLeft, d
16bef 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
16bf0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
16bf1 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
16bf2 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
16bf3 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
16bf4 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ll);.      break
16bf5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
16bf6 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20   TK_OR: {.      
16bf7 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33  int d2 = sqlite3
16bf8 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
16bf9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
16bfa 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
16bfb 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
16bfc 64 32 2c 20 21 6a 75 6d 70 49 66 4e 75 6c 6c 29  d2, !jumpIfNull)
16bfd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
16bfe 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
16bff 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
16c00 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
16c01 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ll);.      sqlit
16c02 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
16c03 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20  el(v, d2);.     
16c04 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
16c05 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b    case TK_NOT: {
16c06 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
16c07 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c  prIfTrue(pParse,
16c08 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64   pExpr->pLeft, d
16c09 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
16c0a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
16c0b 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
16c0c 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LT:.    case TK
16c0d 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LE:.    case TK
16c0e 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GT:.    case TK
16c0f 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GE:.    case TK
16c10 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _NE:.    case TK
16c11 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  _EQ: {.      sql
16c12 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
16c13 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
16c14 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
16c15 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
16c16 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
16c17 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
16c18 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  are(pParse, pExp
16c19 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d  r->pLeft, pExpr-
16c1a 3e 70 52 69 67 68 74 2c 20 6f 70 2c 20 64 65 73  >pRight, op, des
16c1b 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
16c1c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
16c1d 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49   }.    case TK_I
16c1e 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20  SNULL:.    case 
16c1f 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20  TK_NOTNULL: {.  
16c20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
16c21 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  ode(pParse, pExp
16c22 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20  r->pLeft);.     
16c23 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
16c24 70 28 76 2c 20 6f 70 2c 20 31 2c 20 64 65 73 74  p(v, op, 1, dest
16c25 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
16c26 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
16c27 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20  K_BETWEEN: {.   
16c28 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
16c29 73 69 6f 6e 20 69 73 20 22 78 20 42 45 54 57 45  sion is "x BETWE
16c2a 45 4e 20 79 20 41 4e 44 20 7a 22 2e 20 49 74 20  EN y AND z". It 
16c2b 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  is implemented a
16c2c 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  s:.      **.    
16c2d 20 20 2a 2a 20 31 20 49 46 20 28 78 20 3e 3d 20    ** 1 IF (x >= 
16c2e 79 29 20 47 4f 54 4f 20 33 0a 20 20 20 20 20 20  y) GOTO 3.      
16c2f 2a 2a 20 32 20 47 4f 54 4f 20 3c 64 65 73 74 3e  ** 2 GOTO <dest>
16c30 0a 20 20 20 20 20 20 2a 2a 20 33 20 49 46 20 28  .      ** 3 IF (
16c31 78 20 3e 20 7a 29 20 47 4f 54 4f 20 3c 64 65 73  x > z) GOTO <des
16c32 74 3e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t>.      */.    
16c33 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
16c34 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20    Expr *pLeft = 
16c35 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
16c36 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74      Expr *pRight
16c37 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d   = pExpr->pList-
16c38 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[0].pExpr;.   
16c39 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
16c3a 64 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  de(pParse, pLeft
16c3b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
16c3c 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
16c3d 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Dup, 0, 0);.    
16c3e 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
16c3f 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  e(pParse, pRight
16c40 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  );.      addr = 
16c41 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
16c42 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20  ntAddr(v);.     
16c43 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
16c44 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67  rse, pLeft, pRig
16c45 68 74 2c 20 4f 50 5f 47 65 2c 20 61 64 64 72 2b  ht, OP_Ge, addr+
16c46 33 2c 20 21 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  3, !jumpIfNull);
16c47 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
16c48 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50  dbeAddOp(v, OP_P
16c49 6f 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20  op, 1, 0);.     
16c4a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
16c4b 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  p(v, OP_Goto, 0,
16c4c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70 52   dest);.      pR
16c4d 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  ight = pExpr->pL
16c4e 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b  ist->a[1].pExpr;
16c4f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
16c50 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
16c51 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 63 6f  Right);.      co
16c52 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65  deCompare(pParse
16c53 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  , pLeft, pRight,
16c54 20 4f 50 5f 47 74 2c 20 64 65 73 74 2c 20 6a 75   OP_Gt, dest, ju
16c55 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
16c56 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
16c57 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
16c58 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
16c59 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
16c5a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
16c5b 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
16c5c 49 66 4e 6f 74 2c 20 6a 75 6d 70 49 66 4e 75 6c  IfNot, jumpIfNul
16c5d 6c 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20  l, dest);.      
16c5e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
16c5f 0a 20 20 70 50 61 72 73 65 2d 3e 63 6b 4f 66 66  .  pParse->ckOff
16c60 73 65 74 20 3d 20 63 6b 4f 66 66 73 65 74 3b 0a  set = ckOffset;.
16c61 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65  }../*.** Do a de
16c62 65 70 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66  ep comparison of
16c63 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 20   two expression 
16c64 74 72 65 65 73 2e 20 20 52 65 74 75 72 6e 20 54  trees.  Return T
16c65 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a  RUE (non-zero).*
16c66 2a 20 69 66 20 74 68 65 79 20 61 72 65 20 69 64  * if they are id
16c67 65 6e 74 69 63 61 6c 20 61 6e 64 20 72 65 74 75  entical and retu
16c68 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 79  rn FALSE if they
16c69 20 64 69 66 66 65 72 20 69 6e 20 61 6e 79 20 77   differ in any w
16c6a 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  ay..**.** Someti
16c6b 6d 65 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  mes this routine
16c6c 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c   will return FAL
16c6d 53 45 20 65 76 65 6e 20 69 66 20 74 68 65 20 74  SE even if the t
16c6e 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a  wo expressions.*
16c6f 2a 20 72 65 61 6c 6c 79 20 61 72 65 20 65 71 75  * really are equ
16c70 69 76 61 6c 65 6e 74 2e 20 20 49 66 20 77 65 20  ivalent.  If we 
16c71 63 61 6e 6e 6f 74 20 70 72 6f 76 65 20 74 68 61  cannot prove tha
16c72 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  t the expression
16c73 73 20 61 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63  s are.** identic
16c74 61 6c 2c 20 77 65 20 72 65 74 75 72 6e 20 46 41  al, we return FA
16c75 4c 53 45 20 6a 75 73 74 20 74 6f 20 62 65 20 73  LSE just to be s
16c76 61 66 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73  afe.  So if this
16c77 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75   routine.** retu
16c78 72 6e 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20  rns false, then 
16c79 79 6f 75 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c  you do not reall
16c7a 79 20 6b 6e 6f 77 20 66 6f 72 20 63 65 72 74 61  y know for certa
16c7b 69 6e 20 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a  in if the two.**
16c7c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
16c7d 20 74 68 65 20 73 61 6d 65 2e 20 20 42 75 74 20   the same.  But 
16c7e 69 66 20 79 6f 75 20 67 65 74 20 61 20 54 52 55  if you get a TRU
16c7f 45 20 72 65 74 75 72 6e 2c 20 74 68 65 6e 20 79  E return, then y
16c80 6f 75 0a 2a 2a 20 63 61 6e 20 62 65 20 73 75 72  ou.** can be sur
16c81 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  e the expression
16c82 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20  s are the same. 
16c83 20 49 6e 20 74 68 65 20 70 6c 61 63 65 73 20 77   In the places w
16c84 68 65 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75  here.** this rou
16c85 74 69 6e 65 20 69 73 20 75 73 65 64 2c 20 69 74  tine is used, it
16c86 20 64 6f 65 73 20 6e 6f 74 20 68 75 72 74 20 74   does not hurt t
16c87 6f 20 67 65 74 20 61 6e 20 65 78 74 72 61 20 46  o get an extra F
16c88 41 4c 53 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a  ALSE - that.** j
16c89 75 73 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74  ust might result
16c8a 20 69 6e 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c   in some slightl
16c8b 79 20 73 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20  y slower code.  
16c8c 42 75 74 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a  But returning.**
16c8d 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 54 52   an incorrect TR
16c8e 55 45 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f  UE could lead to
16c8f 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a   a malfunction..
16c90 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16c91 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
16c92 72 43 6f 6d 70 61 72 65 28 45 78 70 72 20 2a 70  rCompare(Expr *p
16c93 41 2c 20 45 78 70 72 20 2a 70 42 29 7b 0a 20 20  A, Expr *pB){.  
16c94 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 41 3d  int i;.  if( pA=
16c95 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20  =0||pB==0 ){.   
16c96 20 72 65 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a   return pB==pA;.
16c97 20 20 7d 0a 20 20 69 66 28 20 70 41 2d 3e 6f 70    }.  if( pA->op
16c98 21 3d 70 42 2d 3e 6f 70 20 29 20 72 65 74 75 72  !=pB->op ) retur
16c99 6e 20 30 3b 0a 20 20 69 66 28 20 28 70 41 2d 3e  n 0;.  if( (pA->
16c9a 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74 69  flags & EP_Disti
16c9b 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61 67 73  nct)!=(pB->flags
16c9c 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 20   & EP_Distinct) 
16c9d 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
16c9e 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f  ( !sqlite3ExprCo
16c9f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c  mpare(pA->pLeft,
16ca0 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 20 72 65   pB->pLeft) ) re
16ca1 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 73  turn 0;.  if( !s
16ca2 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
16ca3 65 28 70 41 2d 3e 70 52 69 67 68 74 2c 20 70 42  e(pA->pRight, pB
16ca4 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65 74 75  ->pRight) ) retu
16ca5 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e  rn 0;.  if( pA->
16ca6 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 69 66 28  pList ){.    if(
16ca7 20 70 42 2d 3e 70 4c 69 73 74 3d 3d 30 20 29 20   pB->pList==0 ) 
16ca8 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66  return 0;.    if
16ca9 28 20 70 41 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78  ( pA->pList->nEx
16caa 70 72 21 3d 70 42 2d 3e 70 4c 69 73 74 2d 3e 6e  pr!=pB->pList->n
16cab 45 78 70 72 20 29 20 72 65 74 75 72 6e 20 30 3b  Expr ) return 0;
16cac 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
16cad 70 41 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72  pA->pList->nExpr
16cae 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
16caf 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f  ( !sqlite3ExprCo
16cb0 6d 70 61 72 65 28 70 41 2d 3e 70 4c 69 73 74 2d  mpare(pA->pList-
16cb1 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 42 2d  >a[i].pExpr, pB-
16cb2 3e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78  >pList->a[i].pEx
16cb3 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  pr) ){.        r
16cb4 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
16cb5 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
16cb6 66 28 20 70 42 2d 3e 70 4c 69 73 74 20 29 7b 0a  f( pB->pList ){.
16cb7 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
16cb8 7d 0a 20 20 69 66 28 20 70 41 2d 3e 70 53 65 6c  }.  if( pA->pSel
16cb9 65 63 74 20 7c 7c 20 70 42 2d 3e 70 53 65 6c 65  ect || pB->pSele
16cba 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  ct ) return 0;. 
16cbb 20 69 66 28 20 70 41 2d 3e 69 54 61 62 6c 65 21   if( pA->iTable!
16cbc 3d 70 42 2d 3e 69 54 61 62 6c 65 20 7c 7c 20 70  =pB->iTable || p
16cbd 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 42 2d 3e  A->iColumn!=pB->
16cbe 69 43 6f 6c 75 6d 6e 20 29 20 72 65 74 75 72 6e  iColumn ) return
16cbf 20 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e 6f 70   0;.  if( pA->op
16cc0 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70  !=TK_COLUMN && p
16cc1 41 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b 0a 20 20  A->token.z ){.  
16cc2 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e    if( pB->token.
16cc3 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  z==0 ) return 0;
16cc4 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b  .    if( pB->tok
16cc5 65 6e 2e 6e 21 3d 70 41 2d 3e 74 6f 6b 65 6e 2e  en.n!=pA->token.
16cc6 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  n ) return 0;.  
16cc7 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
16cc8 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 70 41 2d  NICmp((char*)pA-
16cc9 3e 74 6f 6b 65 6e 2e 7a 2c 28 63 68 61 72 2a 29  >token.z,(char*)
16cca 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 2c 70 42 2d 3e  pB->token.z,pB->
16ccb 74 6f 6b 65 6e 2e 6e 29 21 3d 30 20 29 7b 0a 20  token.n)!=0 ){. 
16ccc 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
16ccd 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
16cce 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41  n 1;.}.../*.** A
16ccf 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74  dd a new element
16cd0 20 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f   to the pAggInfo
16cd1 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e 20  ->aCol[] array. 
16cd2 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65   Return the inde
16cd3 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20  x of.** the new 
16cd4 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e  element.  Return
16cd5 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
16cd6 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69  er if malloc fai
16cd7 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ls..*/.static in
16cd8 74 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75  t addAggInfoColu
16cd9 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  mn(sqlite3 *db, 
16cda 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b  AggInfo *pInfo){
16cdb 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66  .  int i;.  pInf
16cdc 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65  o->aCol = sqlite
16cdd 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a  3ArrayAllocate(.
16cde 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20         db,.     
16cdf 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20    pInfo->aCol,. 
16ce0 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e        sizeof(pIn
16ce1 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20  fo->aCol[0]),.  
16ce2 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20 26       3,.       &
16ce3 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a  pInfo->nColumn,.
16ce4 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e         &pInfo->n
16ce5 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 20  ColumnAlloc,.   
16ce6 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 65      &i.  );.  re
16ce7 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f  turn i;.}    ../
16ce8 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65  *.** Add a new e
16ce9 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41  lement to the pA
16cea 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20  ggInfo->aFunc[] 
16ceb 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e 20 74  array.  Return t
16cec 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74  he index of.** t
16ced 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20  he new element. 
16cee 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61 74 69   Return a negati
16cef 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c  ve number if mal
16cf0 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74  loc fails..*/.st
16cf1 61 74 69 63 20 69 6e 74 20 61 64 64 41 67 67 49  atic int addAggI
16cf2 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74 65 33 20  nfoFunc(sqlite3 
16cf3 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49  *db, AggInfo *pI
16cf4 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  nfo){.  int i;. 
16cf5 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d 20   pInfo->aFunc = 
16cf6 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f  sqlite3ArrayAllo
16cf7 63 61 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c  cate(.       db,
16cf8 20 0a 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e   .       pInfo->
16cf9 61 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 73 69  aFunc,.       si
16cfa 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 46 75 6e  zeof(pInfo->aFun
16cfb 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c  c[0]),.       3,
16cfc 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e  .       &pInfo->
16cfd 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 26 70  nFunc,.       &p
16cfe 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f 63  Info->nFuncAlloc
16cff 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b  ,.       &i.  );
16d00 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20  .  return i;.}  
16d01 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69    ../*.** This i
16d02 73 20 61 6e 20 78 46 75 6e 63 20 66 6f 72 20 77  s an xFunc for w
16d03 61 6c 6b 45 78 70 72 54 72 65 65 28 29 20 75 73  alkExprTree() us
16d04 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
16d05 0a 2a 2a 20 73 71 6c 69 74 65 33 45 78 70 72 41  .** sqlite3ExprA
16d06 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
16d07 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33  ().  See sqlite3
16d08 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65  ExprAnalyzeAggre
16d09 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61 64 64  gates.** for add
16d0a 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
16d0b 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
16d0c 72 6f 75 74 69 6e 65 20 61 6e 61 6c 79 7a 65 73  routine analyzes
16d0d 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 66   the aggregate f
16d0e 75 6e 63 74 69 6f 6e 20 61 74 20 70 45 78 70 72  unction at pExpr
16d0f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
16d10 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  analyzeAggregate
16d11 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 45 78 70  (void *pArg, Exp
16d12 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
16d13 20 69 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   i;.  NameContex
16d14 74 20 2a 70 4e 43 20 3d 20 28 4e 61 6d 65 43 6f  t *pNC = (NameCo
16d15 6e 74 65 78 74 20 2a 29 70 41 72 67 3b 0a 20 20  ntext *)pArg;.  
16d16 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
16d17 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 53  pNC->pParse;.  S
16d18 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74  rcList *pSrcList
16d19 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
16d1a 3b 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  ;.  AggInfo *pAg
16d1b 67 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e 70 41 67  gInfo = pNC->pAg
16d1c 67 49 6e 66 6f 3b 0a 0a 20 20 73 77 69 74 63 68  gInfo;..  switch
16d1d 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20  ( pExpr->op ){. 
16d1e 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43     case TK_AGG_C
16d1f 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20  OLUMN:.    case 
16d20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20  TK_COLUMN: {.   
16d21 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73     /* Check to s
16d22 65 65 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ee if the column
16d23 20 69 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68   is in one of th
16d24 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  e tables in the 
16d25 46 52 4f 4d 0a 20 20 20 20 20 20 2a 2a 20 63 6c  FROM.      ** cl
16d26 61 75 73 65 20 6f 66 20 74 68 65 20 61 67 67 72  ause of the aggr
16d27 65 67 61 74 65 20 71 75 65 72 79 20 2a 2f 0a 20  egate query */. 
16d28 20 20 20 20 20 69 66 28 20 70 53 72 63 4c 69 73       if( pSrcLis
16d29 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72  t ){.        str
16d2a 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
16d2b 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69   *pItem = pSrcLi
16d2c 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66  st->a;.        f
16d2d 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 4c 69  or(i=0; i<pSrcLi
16d2e 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
16d2f 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
16d30 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66     struct AggInf
16d31 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20  o_col *pCol;.   
16d32 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72         if( pExpr
16d33 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d  ->iTable==pItem-
16d34 3e 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  >iCursor ){.    
16d35 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65          /* If we
16d36 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e   reach this poin
16d37 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  t, it means that
16d38 20 70 45 78 70 72 20 72 65 66 65 72 73 20 74 6f   pExpr refers to
16d39 20 61 20 74 61 62 6c 65 0a 20 20 20 20 20 20 20   a table.       
16d3a 20 20 20 20 20 2a 2a 20 74 68 61 74 20 69 73 20       ** that is 
16d3b 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
16d3c 73 65 20 6f 66 20 74 68 65 20 61 67 67 72 65 67  se of the aggreg
16d3d 61 74 65 20 71 75 65 72 79 2e 20 20 0a 20 20 20  ate query.  .   
16d3e 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20           **.    
16d3f 20 20 20 20 20 20 20 20 2a 2a 20 4d 61 6b 65 20          ** Make 
16d40 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  an entry for the
16d41 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 41 67 67 49   column in pAggI
16d42 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 69 66 20 74  nfo->aCol[] if t
16d43 68 65 72 65 0a 20 20 20 20 20 20 20 20 20 20 20  here.           
16d44 20 2a 2a 20 69 73 20 6e 6f 74 20 61 6e 20 65 6e   ** is not an en
16d45 74 72 79 20 74 68 65 72 65 20 61 6c 72 65 61 64  try there alread
16d46 79 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  y..            *
16d47 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  /.            in
16d48 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  t k;.           
16d49 20 70 43 6f 6c 20 3d 20 70 41 67 67 49 6e 66 6f   pCol = pAggInfo
16d4a 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  ->aCol;.        
16d4b 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70      for(k=0; k<p
16d4c 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e  AggInfo->nColumn
16d4d 3b 20 6b 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; k++, pCol++){.
16d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
16d4f 28 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d  ( pCol->iTable==
16d50 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 26 26  pExpr->iTable &&
16d51 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
16d52 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e     pCol->iColumn
16d53 3d 3d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ==pExpr->iColumn
16d54 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
16d55 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
16d56 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
16d57 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
16d58 20 20 20 20 20 69 66 28 20 28 6b 3e 3d 70 41 67       if( (k>=pAg
16d59 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a  gInfo->nColumn).
16d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
16d5b 28 6b 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 43  (k = addAggInfoC
16d5c 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2d 3e 64 62  olumn(pParse->db
16d5d 2c 20 70 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20  , pAggInfo))>=0 
16d5e 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a  .            ){.
16d5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
16d60 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e  ol = &pAggInfo->
16d61 61 43 6f 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20  aCol[k];.       
16d62 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61         pCol->pTa
16d63 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b  b = pExpr->pTab;
16d64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
16d65 43 6f 6c 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45  Col->iTable = pE
16d66 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20  xpr->iTable;.   
16d67 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
16d68 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72  >iColumn = pExpr
16d69 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
16d6a 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
16d6b 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Mem = pParse->nM
16d6c 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  em++;.          
16d6d 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65      pCol->iSorte
16d6e 72 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20  rColumn = -1;.  
16d6f 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
16d70 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b  ->pExpr = pExpr;
16d71 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
16d72 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72  f( pAggInfo->pGr
16d73 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 20  oupBy ){.       
16d74 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 2c 20           int j, 
16d75 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n;.             
16d76 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 42     ExprList *pGB
16d77 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72   = pAggInfo->pGr
16d78 6f 75 70 42 79 3b 0a 20 20 20 20 20 20 20 20 20  oupBy;.         
16d79 20 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78         struct Ex
16d7a 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 65  prList_item *pTe
16d7b 72 6d 20 3d 20 70 47 42 2d 3e 61 3b 0a 20 20 20  rm = pGB->a;.   
16d7c 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 20 3d               n =
16d7d 20 70 47 42 2d 3e 6e 45 78 70 72 3b 0a 20 20 20   pGB->nExpr;.   
16d7e 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72               for
16d7f 28 6a 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20  (j=0; j<n; j++, 
16d80 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pTerm++){.      
16d81 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72              Expr
16d82 20 2a 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45   *pE = pTerm->pE
16d83 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20  xpr;.           
16d84 20 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f         if( pE->o
16d85 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20  p==TK_COLUMN && 
16d86 70 45 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70  pE->iTable==pExp
16d87 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20  r->iTable &&.   
16d88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16d89 20 20 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d     pE->iColumn==
16d8a 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29  pExpr->iColumn )
16d8b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
16d8c 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72        pCol->iSor
16d8d 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20  terColumn = j;. 
16d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16d8f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
16d90 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
16d91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
16d92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
16d93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
16d94 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43  ( pCol->iSorterC
16d95 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20  olumn<0 ){.     
16d96 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
16d97 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d  >iSorterColumn =
16d98 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74   pAggInfo->nSort
16d99 69 6e 67 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20  ingColumn++;.   
16d9a 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
16d9b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
16d9c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 72 65 20         /* There 
16d9d 69 73 20 6e 6f 77 20 61 6e 20 65 6e 74 72 79 20  is now an entry 
16d9e 66 6f 72 20 70 45 78 70 72 20 69 6e 20 70 41 67  for pExpr in pAg
16d9f 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65  gInfo->aCol[] (e
16da0 69 74 68 65 72 0a 20 20 20 20 20 20 20 20 20 20  ither.          
16da1 20 20 2a 2a 20 62 65 63 61 75 73 65 20 69 74 20    ** because it 
16da2 77 61 73 20 74 68 65 72 65 20 62 65 66 6f 72 65  was there before
16da3 20 6f 72 20 62 65 63 61 75 73 65 20 77 65 20 6a   or because we j
16da4 75 73 74 20 63 72 65 61 74 65 64 20 69 74 29 2e  ust created it).
16da5 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
16da6 43 6f 6e 76 65 72 74 20 74 68 65 20 70 45 78 70  Convert the pExp
16da7 72 20 74 6f 20 62 65 20 61 20 54 4b 5f 41 47 47  r to be a TK_AGG
16da8 5f 43 4f 4c 55 4d 4e 20 72 65 66 65 72 72 69 6e  _COLUMN referrin
16da9 67 20 74 6f 20 74 68 61 74 0a 20 20 20 20 20 20  g to that.      
16daa 20 20 20 20 20 20 2a 2a 20 70 41 67 67 49 6e 66        ** pAggInf
16dab 6f 2d 3e 61 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e  o->aCol[] entry.
16dac 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  .            */.
16dad 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
16dae 72 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41  r->pAggInfo = pA
16daf 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20  ggInfo;.        
16db0 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
16db1 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20  TK_AGG_COLUMN;. 
16db2 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
16db3 2d 3e 69 41 67 67 20 3d 20 6b 3b 0a 20 20 20 20  ->iAgg = k;.    
16db4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
16db5 20 20 20 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e           } /* en
16db6 64 69 66 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  dif pExpr->iTabl
16db7 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f  e==pItem->iCurso
16db8 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 20 2f  r */.        } /
16db9 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f 76 65 72 20  * end loop over 
16dba 70 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 20 20  pSrcList */.    
16dbb 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
16dbc 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
16dbd 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  se TK_AGG_FUNCTI
16dbe 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54  ON: {.      /* T
16dbf 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d  he pNC->nDepth==
16dc0 30 20 74 65 73 74 20 63 61 75 73 65 73 20 61 67  0 test causes ag
16dc1 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
16dc2 73 20 69 6e 20 73 75 62 71 75 65 72 69 65 73 0a  s in subqueries.
16dc3 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69        ** to be i
16dc4 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 20  gnored */.      
16dc5 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d  if( pNC->nDepth=
16dc6 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
16dc7 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
16dc8 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70 6c   pExpr is a dupl
16dc9 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65 72  icate of another
16dca 20 61 67 67 72 65 67 61 74 65 20 0a 20 20 20 20   aggregate .    
16dcb 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20      ** function 
16dcc 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 20  that is already 
16dcd 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f 20  in the pAggInfo 
16dce 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 20  structure.      
16dcf 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74 72    */.        str
16dd0 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63  uct AggInfo_func
16dd1 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49 6e   *pItem = pAggIn
16dd2 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20 20  fo->aFunc;.     
16dd3 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41     for(i=0; i<pA
16dd4 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69  ggInfo->nFunc; i
16dd5 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
16dd6 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
16dd7 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
16dd8 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45 78  Item->pExpr, pEx
16dd9 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  pr) ){.         
16dda 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
16ddb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
16ddc 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 70          if( i>=p
16ddd 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29  AggInfo->nFunc )
16dde 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70  {.          /* p
16ddf 45 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61 6c  Expr is original
16de0 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 6e  .  Make a new en
16de1 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d  try in pAggInfo-
16de2 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20 20  >aFunc[].       
16de3 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
16de4 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61  u8 enc = ENC(pPa
16de5 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20  rse->db);.      
16de6 20 20 20 20 69 20 3d 20 61 64 64 41 67 67 49 6e      i = addAggIn
16de7 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e 64  foFunc(pParse->d
16de8 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20 20  b, pAggInfo);.  
16de9 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 30          if( i>=0
16dea 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
16deb 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e 66  pItem = &pAggInf
16dec 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20 20  o->aFunc[i];.   
16ded 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
16dee 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20  pExpr = pExpr;. 
16def 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
16df0 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d  ->iMem = pParse-
16df1 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20  >nMem++;.       
16df2 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 46 75 6e       pItem->pFun
16df3 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46  c = sqlite3FindF
16df4 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e  unction(pParse->
16df5 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  db,.            
16df6 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 45         (char*)pE
16df7 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45  xpr->token.z, pE
16df8 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20 20  xpr->token.n,.  
16df9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16dfa 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 20 3f 20   pExpr->pList ? 
16dfb 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45  pExpr->pList->nE
16dfc 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c 20 30 29  xpr : 0, enc, 0)
16dfd 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
16dfe 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26  ( pExpr->flags &
16dff 20 45 50 5f 44 69 73 74 69 6e 63 74 20 29 7b 0a   EP_Distinct ){.
16e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
16e01 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d  tem->iDistinct =
16e02 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
16e03 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
16e04 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
16e05 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69 6e    pItem->iDistin
16e06 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20  ct = -1;.       
16e07 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
16e08 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
16e09 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 70 45 78       /* Make pEx
16e0a 70 72 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  pr point to the 
16e0b 61 70 70 72 6f 70 72 69 61 74 65 20 70 41 67 67  appropriate pAgg
16e0c 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 65 6e  Info->aFunc[] en
16e0d 74 72 79 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  try.        */. 
16e0e 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41         pExpr->iA
16e0f 67 67 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20  gg = i;.        
16e10 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20  pExpr->pAggInfo 
16e11 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20  = pAggInfo;.    
16e12 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
16e13 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
16e14 0a 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 6c  .  /* Recursivel
16e15 79 20 77 61 6c 6b 20 73 75 62 71 75 65 72 69 65  y walk subquerie
16e16 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 54 4b  s looking for TK
16e17 5f 43 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20 74 68  _COLUMN nodes th
16e18 61 74 20 6e 65 65 64 0a 20 20 2a 2a 20 74 6f 20  at need.  ** to 
16e19 62 65 20 63 68 61 6e 67 65 64 20 74 6f 20 54 4b  be changed to TK
16e1a 5f 41 47 47 5f 43 4f 4c 55 4d 4e 2e 20 20 42 75  _AGG_COLUMN.  Bu
16e1b 74 20 69 6e 63 72 65 6d 65 6e 74 20 6e 44 65 70  t increment nDep
16e1c 74 68 20 73 6f 20 74 68 61 74 0a 20 20 2a 2a 20  th so that.  ** 
16e1d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
16e1e 6e 6f 64 65 73 20 69 6e 20 73 75 62 71 75 65 72  nodes in subquer
16e1f 69 65 73 20 77 69 6c 6c 20 62 65 20 75 6e 63 68  ies will be unch
16e20 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  anged..  */.  if
16e21 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74  ( pExpr->pSelect
16e22 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65   ){.    pNC->nDe
16e23 70 74 68 2b 2b 3b 0a 20 20 20 20 77 61 6c 6b 53  pth++;.    walkS
16e24 65 6c 65 63 74 45 78 70 72 28 70 45 78 70 72 2d  electExpr(pExpr-
16e25 3e 70 53 65 6c 65 63 74 2c 20 61 6e 61 6c 79 7a  >pSelect, analyz
16e26 65 41 67 67 72 65 67 61 74 65 2c 20 70 4e 43 29  eAggregate, pNC)
16e27 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74  ;.    pNC->nDept
16e28 68 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  h--;.  }.  retur
16e29 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e  n 0;.}../*.** An
16e2a 61 6c 79 7a 65 20 74 68 65 20 67 69 76 65 6e 20  alyze the given 
16e2b 65 78 70 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69  expression looki
16e2c 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65  ng for aggregate
16e2d 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 0a 2a   functions and.*
16e2e 2a 20 66 6f 72 20 76 61 72 69 61 62 6c 65 73 20  * for variables 
16e2f 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20  that need to be 
16e30 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 50 61  added to the pPa
16e31 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61  rse->aAgg[] arra
16e32 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61 64 64 69 74  y..** Make addit
16e33 69 6f 6e 61 6c 20 65 6e 74 72 69 65 73 20 74 6f  ional entries to
16e34 20 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41 67   the pParse->aAg
16e35 67 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65 63  g[] array as nec
16e36 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  essary..**.** Th
16e37 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  is routine shoul
16e38 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64  d only be called
16e39 20 61 66 74 65 72 20 74 68 65 20 65 78 70 72 65   after the expre
16e3a 73 73 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a  ssion has been.*
16e3b 2a 20 61 6e 61 6c 79 7a 65 64 20 62 79 20 73 71  * analyzed by sq
16e3c 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65  lite3ExprResolve
16e3d 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  Names()..**.** I
16e3e 66 20 65 72 72 6f 72 73 20 61 72 65 20 73 65 65  f errors are see
16e3f 6e 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f  n, leave an erro
16e40 72 20 6d 65 73 73 61 67 65 20 69 6e 20 7a 45 72  r message in zEr
16e41 72 4d 73 67 20 61 6e 64 20 72 65 74 75 72 6e 0a  rMsg and return.
16e42 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
16e43 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49   errors..*/.SQLI
16e44 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16e45 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
16e46 65 41 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65  eAggregates(Name
16e47 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 78  Context *pNC, Ex
16e48 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e  pr *pExpr){.  in
16e49 74 20 6e 45 72 72 20 3d 20 70 4e 43 2d 3e 70 50  t nErr = pNC->pP
16e4a 61 72 73 65 2d 3e 6e 45 72 72 3b 0a 20 20 77 61  arse->nErr;.  wa
16e4b 6c 6b 45 78 70 72 54 72 65 65 28 70 45 78 70 72  lkExprTree(pExpr
16e4c 2c 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  , analyzeAggrega
16e4d 74 65 2c 20 70 4e 43 29 3b 0a 20 20 72 65 74 75  te, pNC);.  retu
16e4e 72 6e 20 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e  rn pNC->pParse->
16e4f 6e 45 72 72 20 2d 20 6e 45 72 72 3b 0a 7d 0a 0a  nErr - nErr;.}..
16e50 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
16e51 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
16e52 72 65 67 61 74 65 73 28 29 20 66 6f 72 20 65 76  regates() for ev
16e53 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ery expression i
16e54 6e 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  n an.** expressi
16e55 6f 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e  on list.  Return
16e56 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
16e57 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rrors..**.** If 
16e58 61 6e 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e  an error is foun
16e59 64 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20  d, the analysis 
16e5a 69 73 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f  is cut short..*/
16e5b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16e5c 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 41  int sqlite3ExprA
16e5d 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61  nalyzeAggList(Na
16e5e 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20  meContext *pNC, 
16e5f 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
16e60 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  {.  struct ExprL
16e61 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
16e62 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
16e63 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  nErr = 0;.  if( 
16e64 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72  pList ){.    for
16e65 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c  (pItem=pList->a,
16e66 20 69 3d 30 3b 20 6e 45 72 72 3d 3d 30 20 26 26   i=0; nErr==0 &&
16e67 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
16e68 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
16e69 20 20 20 20 20 20 6e 45 72 72 20 2b 3d 20 73 71        nErr += sq
16e6a 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
16e6b 41 67 67 72 65 67 61 74 65 73 28 70 4e 43 2c 20  Aggregates(pNC, 
16e6c 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20  pItem->pExpr);. 
16e6d 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
16e6e 6e 20 6e 45 72 72 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  n nErr;.}../****
16e6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
16e70 66 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f expr.c *******
16e71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16e72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16e73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
16e74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
16e75 20 66 69 6c 65 20 61 6c 74 65 72 2e 63 20 2a 2a   file alter.c **
16e76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16e77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16e78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
16e79 20 32 30 30 35 20 46 65 62 72 75 61 72 79 20 31   2005 February 1
16e7a 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
16e7b 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
16e7c 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
16e7d 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
16e7e 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
16e7f 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
16e80 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
16e81 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
16e82 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
16e83 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
16e84 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
16e85 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
16e86 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
16e87 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
16e88 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
16e89 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
16e8a 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
16e8b 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
16e8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16e8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16e8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16e8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16e90 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
16e91 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f  ntains C code ro
16e92 75 74 69 6e 65 73 20 74 68 61 74 20 75 73 65 64  utines that used
16e93 20 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44 42   to generate VDB
16e94 45 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 69  E code.** that i
16e95 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 41 4c  mplements the AL
16e96 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e  TER TABLE comman
16e97 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 6c  d..**.** $Id: al
16e98 74 65 72 2e 63 2c 76 20 31 2e 33 32 20 32 30 30  ter.c,v 1.32 200
16e99 37 2f 30 38 2f 32 39 20 31 34 3a 30 36 3a 32 33  7/08/29 14:06:23
16e9a 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
16e9b 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   $.*/../*.** The
16e9c 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
16e9d 6c 65 20 6f 6e 6c 79 20 65 78 69 73 74 73 20 69  le only exists i
16e9e 66 20 77 65 20 61 72 65 20 6e 6f 74 20 6f 6d 69  f we are not omi
16e9f 74 74 69 6e 67 20 74 68 65 0a 2a 2a 20 41 4c 54  tting the.** ALT
16ea0 45 52 20 54 41 42 4c 45 20 6c 6f 67 69 63 20 66  ER TABLE logic f
16ea1 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a  rom the build..*
16ea2 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
16ea3 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45  _OMIT_ALTERTABLE
16ea4 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
16ea5 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  nction is used b
16ea6 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20  y SQL generated 
16ea7 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
16ea8 20 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45   .** ALTER TABLE
16ea9 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69   command. The fi
16eaa 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
16eab 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43 52  the text of a CR
16eac 45 41 54 45 20 54 41 42 4c 45 20 6f 72 0a 2a 2a  EATE TABLE or.**
16ead 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 63 6f   CREATE INDEX co
16eae 6d 6d 61 6e 64 2e 20 54 68 65 20 73 65 63 6f 6e  mmand. The secon
16eaf 64 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d  d is a table nam
16eb0 65 2e 20 54 68 65 20 74 61 62 6c 65 20 6e 61 6d  e. The table nam
16eb1 65 20 69 6e 20 0a 2a 2a 20 74 68 65 20 43 52 45  e in .** the CRE
16eb2 41 54 45 20 54 41 42 4c 45 20 6f 72 20 43 52 45  ATE TABLE or CRE
16eb3 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ATE INDEX statem
16eb4 65 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64 20  ent is replaced 
16eb5 77 69 74 68 20 74 68 65 20 74 68 69 72 64 0a 2a  with the third.*
16eb6 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74  * argument and t
16eb7 68 65 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e  he result return
16eb8 65 64 2e 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a  ed. Examples:.**
16eb9 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d  .** sqlite_renam
16eba 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45 20  e_table('CREATE 
16ebb 54 41 42 4c 45 20 61 62 63 28 61 2c 20 62 2c 20  TABLE abc(a, b, 
16ebc 63 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20  c)', 'def').**  
16ebd 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 54 41     -> 'CREATE TA
16ebe 42 4c 45 20 64 65 66 28 61 2c 20 62 2c 20 63 29  BLE def(a, b, c)
16ebf 27 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72  '.**.** sqlite_r
16ec0 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52 45  ename_table('CRE
16ec1 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20 61  ATE INDEX i ON a
16ec2 62 63 28 61 29 27 2c 20 27 64 65 66 27 29 0a 2a  bc(a)', 'def').*
16ec3 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45  *     -> 'CREATE
16ec4 20 49 4e 44 45 58 20 69 20 4f 4e 20 64 65 66 28   INDEX i ON def(
16ec5 61 2c 20 62 2c 20 63 29 27 0a 2a 2f 0a 73 74 61  a, b, c)'.*/.sta
16ec6 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 54  tic void renameT
16ec7 61 62 6c 65 46 75 6e 63 28 0a 20 20 73 71 6c 69  ableFunc(.  sqli
16ec8 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
16ec9 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
16eca 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
16ecb 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e  e **argv.){.  un
16ecc 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
16ecd 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  t *zSql = sqlite
16ece 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
16ecf 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65  v[0]);.  unsigne
16ed0 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54  d char const *zT
16ed1 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74  ableName = sqlit
16ed2 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
16ed3 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74  gv[1]);..  int t
16ed4 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e  oken;.  Token tn
16ed5 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  ame;.  unsigned 
16ed6 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72  char const *zCsr
16ed7 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c   = zSql;.  int l
16ed8 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  en = 0;.  char *
16ed9 7a 52 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33  zRet;..  sqlite3
16eda 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 75   *db = sqlite3_u
16edb 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
16edc 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69  );..  /* The pri
16edd 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c  nciple used to l
16ede 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  ocate the table 
16edf 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41  name in the CREA
16ee0 54 45 20 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73  TE TABLE .  ** s
16ee1 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74  tatement is that
16ee2 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
16ee3 69 73 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b  is the first tok
16ee4 65 6e 20 74 68 61 74 20 69 73 20 69 6d 6d 65 64  en that is immed
16ee5 69 61 74 65 64 6c 79 0a 20 20 2a 2a 20 66 6f 6c  iatedly.  ** fol
16ee6 6c 6f 77 65 64 20 62 79 20 61 20 6c 65 66 74 20  lowed by a left 
16ee7 70 61 72 65 6e 74 68 65 73 69 73 20 2d 20 54 4b  parenthesis - TK
16ee8 5f 4c 50 20 2d 20 6f 72 20 22 55 53 49 4e 47 22  _LP - or "USING"
16ee9 20 54 4b 5f 55 53 49 4e 47 2e 0a 20 20 2a 2f 0a   TK_USING..  */.
16eea 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
16eeb 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 66 28    do {.      if(
16eec 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20   !*zCsr ){.     
16eed 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66     /* Ran out of
16eee 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69   input before fi
16eef 6e 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 6e 67  nding an opening
16ef0 20 62 72 61 63 6b 65 74 2e 20 52 65 74 75 72 6e   bracket. Return
16ef1 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20   NULL. */.      
16ef2 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
16ef3 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72  }..      /* Stor
16ef4 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  e the token that
16ef5 20 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20   zCsr points to 
16ef6 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20  in tname. */.   
16ef7 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73     tname.z = zCs
16ef8 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e  r;.      tname.n
16ef9 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f   = len;..      /
16efa 2a 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74  * Advance zCsr t
16efb 6f 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  o the next token
16efc 2e 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b  . Store that tok
16efd 65 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65  en type in 'toke
16efe 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64  n',.      ** and
16eff 20 69 74 27 73 20 6c 65 6e 67 74 68 20 69 6e 20   it's length in 
16f00 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65  'len' (to be use
16f01 64 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  d next iteration
16f02 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a   of this loop)..
16f03 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64        */.      d
16f04 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72  o {.        zCsr
16f05 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20   += len;.       
16f06 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65   len = sqlite3Ge
16f07 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f  tToken(zCsr, &to
16f08 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77 68  ken);.      } wh
16f09 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53  ile( token==TK_S
16f0a 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73  PACE );.      as
16f0b 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 20  sert( len>0 );. 
16f0c 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65     } while( toke
16f0d 6e 21 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b 65  n!=TK_LP && toke
16f0e 6e 21 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a 0a  n!=TK_USING );..
16f0f 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74      zRet = sqlit
16f10 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
16f11 2e 2a 73 25 51 25 73 22 2c 20 74 6e 61 6d 65 2e  .*s%Q%s", tname.
16f12 7a 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20  z - zSql, zSql, 
16f13 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65 4e 61  .       zTableNa
16f14 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d  me, tname.z+tnam
16f15 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  e.n);.    sqlite
16f16 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
16f17 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c  ntext, zRet, -1,
16f18 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
16f19 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53    }.}..#ifndef S
16f1a 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
16f1b 45 52 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74  ER./* This funct
16f1c 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 53  ion is used by S
16f1d 51 4c 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  QL generated to 
16f1e 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a  implement the.**
16f1f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d   ALTER TABLE com
16f20 6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 20  mand. The first 
16f21 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
16f22 74 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 45  text of a CREATE
16f23 20 54 52 49 47 47 45 52 20 0a 2a 2a 20 73 74 61   TRIGGER .** sta
16f24 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f  tement. The seco
16f25 6e 64 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61  nd is a table na
16f26 6d 65 2e 20 54 68 65 20 74 61 62 6c 65 20 6e 61  me. The table na
16f27 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  me in the CREATE
16f28 20 0a 2a 2a 20 54 52 49 47 47 45 52 20 73 74 61   .** TRIGGER sta
16f29 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c 61 63  tement is replac
16f2a 65 64 20 77 69 74 68 20 74 68 65 20 74 68 69 72  ed with the thir
16f2b 64 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74  d argument and t
16f2c 68 65 20 72 65 73 75 6c 74 20 0a 2a 2a 20 72 65  he result .** re
16f2d 74 75 72 6e 65 64 2e 20 54 68 69 73 20 69 73 20  turned. This is 
16f2e 61 6e 61 6c 61 67 6f 75 73 20 74 6f 20 72 65 6e  analagous to ren
16f2f 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 29 20 61  ameTableFunc() a
16f30 62 6f 76 65 2c 20 65 78 63 65 70 74 20 66 6f 72  bove, except for
16f31 20 43 52 45 41 54 45 0a 2a 2a 20 54 52 49 47 47   CREATE.** TRIGG
16f32 45 52 2c 20 6e 6f 74 20 43 52 45 41 54 45 20 49  ER, not CREATE I
16f33 4e 44 45 58 20 61 6e 64 20 43 52 45 41 54 45 20  NDEX and CREATE 
16f34 54 41 42 4c 45 2e 0a 2a 2f 0a 73 74 61 74 69 63  TABLE..*/.static
16f35 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 72 69 67   void renameTrig
16f36 67 65 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  gerFunc(.  sqlit
16f37 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
16f38 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
16f39 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
16f3a 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73   **argv.){.  uns
16f3b 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
16f3c 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33   *zSql = sqlite3
16f3d 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
16f3e 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64  [0]);.  unsigned
16f3f 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61   char const *zTa
16f40 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  bleName = sqlite
16f41 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
16f42 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f  v[1]);..  int to
16f43 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61  ken;.  Token tna
16f44 6d 65 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d  me;.  int dist =
16f45 20 33 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   3;.  unsigned c
16f46 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20  har const *zCsr 
16f47 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65  = zSql;.  int le
16f48 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a  n = 0;.  char *z
16f49 52 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 20  Ret;..  sqlite3 
16f4a 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 75 73  *db = sqlite3_us
16f4b 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
16f4c 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e  ;..  /* The prin
16f4d 63 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f  ciple used to lo
16f4e 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e  cate the table n
16f4f 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54  ame in the CREAT
16f50 45 20 54 52 49 47 47 45 52 20 0a 20 20 2a 2a 20  E TRIGGER .  ** 
16f51 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61  statement is tha
16f52 74 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  t the table name
16f53 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 6f   is the first to
16f54 6b 65 6e 20 74 68 61 74 20 69 73 20 69 6d 6d 65  ken that is imme
16f55 64 69 61 74 65 64 6c 79 0a 20 20 2a 2a 20 70 72  diatedly.  ** pr
16f56 65 63 65 64 65 64 20 62 79 20 65 69 74 68 65 72  eceded by either
16f57 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54   TK_ON or TK_DOT
16f58 20 61 6e 64 20 69 6d 6d 65 64 69 61 74 65 64 6c   and immediatedl
16f59 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6f 6e  y followed by on
16f5a 65 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f 57 48 45  e.  ** of TK_WHE
16f5b 4e 2c 20 54 4b 5f 42 45 47 49 4e 20 6f 72 20 54  N, TK_BEGIN or T
16f5c 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66  K_FOR..  */.  if
16f5d 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f  ( zSql ){.    do
16f5e 20 7b 0a 0a 20 20 20 20 20 20 69 66 28 20 21 2a   {..      if( !*
16f5f 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20 20 20  zCsr ){.        
16f60 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e  /* Ran out of in
16f61 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69  put before findi
16f62 6e 67 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  ng the table nam
16f63 65 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  e. Return NULL. 
16f64 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
16f65 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  n;.      }..    
16f66 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74    /* Store the t
16f67 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70  oken that zCsr p
16f68 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d  oints to in tnam
16f69 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d  e. */.      tnam
16f6a 65 2e 7a 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20  e.z = zCsr;.    
16f6b 20 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b    tname.n = len;
16f6c 0a 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e  ..      /* Advan
16f6d 63 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e  ce zCsr to the n
16f6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65  ext token. Store
16f6f 20 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65   that token type
16f70 20 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20   in 'token',.   
16f71 20 20 20 2a 2a 20 61 6e 64 20 69 74 27 73 20 6c     ** and it's l
16f72 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e 27 20 28  ength in 'len' (
16f73 74 6f 20 62 65 20 75 73 65 64 20 6e 65 78 74 20  to be used next 
16f74 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 69  iteration of thi
16f75 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a  s loop)..      *
16f76 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20  /.      do {.   
16f77 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e       zCsr += len
16f78 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20  ;.        len = 
16f79 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28  sqlite3GetToken(
16f7a 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20  zCsr, &token);. 
16f7b 20 20 20 20 20 7d 77 68 69 6c 65 28 20 74 6f 6b       }while( tok
16f7c 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a  en==TK_SPACE );.
16f7d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
16f7e 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  n>0 );..      /*
16f7f 20 56 61 72 69 61 62 6c 65 20 27 64 69 73 74 27   Variable 'dist'
16f80 20 73 74 6f 72 65 73 20 74 68 65 20 6e 75 6d 62   stores the numb
16f81 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65 61  er of tokens rea
16f82 64 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74  d since the most
16f83 0a 20 20 20 20 20 20 2a 2a 20 72 65 63 65 6e 74  .      ** recent
16f84 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b 5f 4f 4e   TK_DOT or TK_ON
16f85 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61  . This means tha
16f86 74 20 77 68 65 6e 20 61 20 57 48 45 4e 2c 20 46  t when a WHEN, F
16f87 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a 20 20 20  OR or BEGIN .   
16f88 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 73 20 72     ** token is r
16f89 65 61 64 20 61 6e 64 20 27 64 69 73 74 27 20 65  ead and 'dist' e
16f8a 71 75 61 6c 73 20 32 2c 20 74 68 65 20 63 6f 6e  quals 2, the con
16f8b 64 69 74 69 6f 6e 20 73 74 61 74 65 64 20 61 62  dition stated ab
16f8c 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ove.      ** to 
16f8d 62 65 20 6d 65 74 2e 0a 20 20 20 20 20 20 2a 2a  be met..      **
16f8e 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74  .      ** Note t
16f8f 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74 20 62 65  hat ON cannot be
16f90 20 61 20 64 61 74 61 62 61 73 65 2c 20 74 61 62   a database, tab
16f91 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d  le or column nam
16f92 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a 2a 20 74  e, so.      ** t
16f93 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
16f94 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 73  to worry about s
16f95 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20 20 20 20  yntax like .    
16f96 20 20 2a 2a 20 22 43 52 45 41 54 45 20 54 52 49    ** "CREATE TRI
16f97 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f 4e 2e 4f  GGER ... ON ON.O
16f98 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20 65 74 63  N BEGIN ..." etc
16f99 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
16f9a 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20 20 20 69   dist++;.      i
16f9b 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 44 4f 54  f( token==TK_DOT
16f9c 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 4f 4e   || token==TK_ON
16f9d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69 73 74   ){.        dist
16f9e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
16f9f 20 20 7d 20 77 68 69 6c 65 28 20 64 69 73 74 21    } while( dist!
16fa0 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21 3d 54 4b  =2 || (token!=TK
16fa1 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65 6e 21 3d  _WHEN && token!=
16fa2 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b 65 6e 21  TK_FOR && token!
16fa3 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b 0a 0a 20  =TK_BEGIN) );.. 
16fa4 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 74     /* Variable t
16fa5 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e  name now contain
16fa6 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  s the token that
16fa7 20 69 73 20 74 68 65 20 6f 6c 64 20 74 61 62 6c   is the old tabl
16fa8 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 69 6e  e-name.    ** in
16fa9 20 74 68 65 20 43 52 45 41 54 45 20 54 52 49 47   the CREATE TRIG
16faa 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20  GER statement.. 
16fab 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65 74 20 3d     */.    zRet =
16fac 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
16fad 64 62 2c 20 22 25 2e 2a 73 25 51 25 73 22 2c 20  db, "%.*s%Q%s", 
16fae 74 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20  tname.z - zSql, 
16faf 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54  zSql, .       zT
16fb0 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e  ableName, tname.
16fb1 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20  z+tname.n);.    
16fb2 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
16fb3 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65  ext(context, zRe
16fb4 74 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66  t, -1, sqlite3_f
16fb5 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  ree);.  }.}.#end
16fb6 69 66 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 5f  if   /* !SQLITE_
16fb7 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
16fb8 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
16fb9 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
16fba 6e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20  ns used to help 
16fbb 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52 20  implement ALTER 
16fbc 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54 45 5f  TABLE.*/.SQLITE_
16fbd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
16fbe 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f  ite3AlterFunctio
16fbf 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ns(sqlite3 *db){
16fc0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
16fc1 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 63 68  struct {.     ch
16fc2 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ar *zName;.     
16fc3 73 69 67 6e 65 64 20 63 68 61 72 20 6e 41 72 67  signed char nArg
16fc4 3b 0a 20 20 20 20 20 76 6f 69 64 20 28 2a 78 46  ;.     void (*xF
16fc5 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
16fc6 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
16fc7 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 20 20 7d  3_value **);.  }
16fc8 20 61 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 20 20   aFuncs[] = {.  
16fc9 20 20 7b 20 22 73 71 6c 69 74 65 5f 72 65 6e 61    { "sqlite_rena
16fca 6d 65 5f 74 61 62 6c 65 22 2c 20 20 20 20 32 2c  me_table",    2,
16fcb 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63   renameTableFunc
16fcc 7d 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  },.#ifndef SQLIT
16fcd 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
16fce 20 20 20 7b 20 22 73 71 6c 69 74 65 5f 72 65 6e     { "sqlite_ren
16fcf 61 6d 65 5f 74 72 69 67 67 65 72 22 2c 20 20 32  ame_trigger",  2
16fd0 2c 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46  , renameTriggerF
16fd1 75 6e 63 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7d  unc},.#endif.  }
16fd2 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f  ;.  int i;..  fo
16fd3 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28  r(i=0; i<sizeof(
16fd4 61 46 75 6e 63 73 29 2f 73 69 7a 65 6f 66 28 61  aFuncs)/sizeof(a
16fd5 46 75 6e 63 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b  Funcs[0]); i++){
16fd6 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61  .    sqlite3Crea
16fd7 74 65 46 75 6e 63 28 64 62 2c 20 61 46 75 6e 63  teFunc(db, aFunc
16fd8 73 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 61 46 75 6e  s[i].zName, aFun
16fd9 63 73 5b 69 5d 2e 6e 41 72 67 2c 0a 20 20 20 20  cs[i].nArg,.    
16fda 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c      SQLITE_UTF8,
16fdb 20 28 76 6f 69 64 20 2a 29 64 62 2c 20 61 46 75   (void *)db, aFu
16fdc 6e 63 73 5b 69 5d 2e 78 46 75 6e 63 2c 20 30 2c  ncs[i].xFunc, 0,
16fdd 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   0);.  }.}../*.*
16fde 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74  * Generate the t
16fdf 65 78 74 20 6f 66 20 61 20 57 48 45 52 45 20 65  ext of a WHERE e
16fe0 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20  xpression which 
16fe1 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73  can be used to s
16fe2 65 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d  elect all.** tem
16fe3 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 73 20  porary triggers 
16fe4 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 66 72  on table pTab fr
16fe5 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65  om the sqlite_te
16fe6 6d 70 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  mp_master table.
16fe7 20 49 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61   If.** table pTa
16fe8 62 20 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61  b has no tempora
16fe9 72 79 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20  ry triggers, or 
16fea 69 73 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64  is itself stored
16feb 20 69 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70   in the .** temp
16fec 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2c 20  orary database, 
16fed 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
16fee 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
16fef 20 2a 77 68 65 72 65 54 65 6d 70 54 72 69 67 67   *whereTempTrigg
16ff0 65 72 73 28 50 61 72 73 65 20 2a 70 50 61 72 73  ers(Parse *pPars
16ff1 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  e, Table *pTab){
16ff2 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
16ff3 67 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72  g;.  char *zWher
16ff4 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 74  e = 0;.  char *t
16ff5 6d 70 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  mp = 0;.  const 
16ff6 53 63 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68  Schema *pTempSch
16ff7 65 6d 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  ema = pParse->db
16ff8 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61  ->aDb[1].pSchema
16ff9 3b 20 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68  ; /* Temp db sch
16ffa 65 6d 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  ema */..  /* If 
16ffb 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  the table is not
16ffc 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20   located in the 
16ffd 74 65 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63  temp-db (in whic
16ffe 68 20 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a  h case NULL is .
16fff 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c    ** returned, l
17000 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
17001 74 61 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74  tables list of t
17002 72 69 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63  riggers. For eac
17003 68 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74  h trigger.  ** t
17004 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20  hat is not part 
17005 6f 66 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73  of the temp-db s
17006 63 68 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61  chema, add a cla
17007 75 73 65 20 74 6f 20 74 68 65 20 57 48 45 52 45  use to the WHERE
17008 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f   .  ** expressio
17009 6e 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70  n being built up
1700a 20 69 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f   in zWhere..  */
1700b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63  .  if( pTab->pSc
1700c 68 65 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d  hema!=pTempSchem
1700d 61 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  a ){.    sqlite3
1700e 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
1700f 62 3b 0a 20 20 20 20 66 6f 72 28 20 70 54 72 69  b;.    for( pTri
17010 67 3d 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  g=pTab->pTrigger
17011 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70  ; pTrig; pTrig=p
17012 54 72 69 67 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  Trig->pNext ){. 
17013 20 20 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e       if( pTrig->
17014 70 53 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63  pSchema==pTempSc
17015 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20  hema ){.        
17016 69 66 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20  if( !zWhere ){. 
17017 20 20 20 20 20 20 20 20 20 7a 57 68 65 72 65 20           zWhere 
17018 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
17019 28 64 62 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20  (db, "name=%Q", 
1701a 70 54 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20  pTrig->name);.  
1701b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1701c 20 20 20 20 20 20 20 74 6d 70 20 3d 20 7a 57 68         tmp = zWh
1701d 65 72 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  ere;.          z
1701e 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d  Where = sqlite3M
1701f 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f  Printf(db, "%s O
17020 52 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65  R name=%Q", zWhe
17021 72 65 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 29  re, pTrig->name)
17022 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
17023 74 65 33 5f 66 72 65 65 28 74 6d 70 29 3b 0a 20  te3_free(tmp);. 
17024 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
17025 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
17026 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a 0a 2f  urn zWhere;.}../
17027 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
17028 64 65 20 74 6f 20 64 72 6f 70 20 61 6e 64 20 72  de to drop and r
17029 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e  eload the intern
1702a 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  al representatio
1702b 6e 20 6f 66 20 74 61 62 6c 65 0a 2a 2a 20 70 54  n of table.** pT
1702c 61 62 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ab from the data
1702d 62 61 73 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20  base, including 
1702e 74 72 69 67 67 65 72 73 20 61 6e 64 20 74 65 6d  triggers and tem
1702f 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 73 2e  porary triggers.
17030 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4e 61  .** Argument zNa
17031 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  me is the name o
17032 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74  f the table in t
17033 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
17034 6d 61 20 61 74 0a 2a 2a 20 74 68 65 20 74 69 6d  ma at.** the tim
17035 65 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20  e the generated 
17036 63 6f 64 65 20 69 73 20 65 78 65 63 75 74 65 64  code is executed
17037 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 69  . This can be di
17038 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20  fferent from.** 
17039 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 69 66 20 74  pTab->zName if t
1703a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1703b 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20  being called to 
1703c 63 6f 64 65 20 70 61 72 74 20 6f 66 20 61 6e 20  code part of an 
1703d 0a 2a 2a 20 22 41 4c 54 45 52 20 54 41 42 4c 45  .** "ALTER TABLE
1703e 20 52 45 4e 41 4d 45 20 54 4f 22 20 73 74 61 74   RENAME TO" stat
1703f 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ement..*/.static
17040 20 76 6f 69 64 20 72 65 6c 6f 61 64 54 61 62 6c   void reloadTabl
17041 65 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70  eSchema(Parse *p
17042 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
17043 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
17044 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a  zName){.  Vdbe *
17045 76 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72  v;.  char *zWher
17046 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  e;.  int iDb;   
17047 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17048 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61  /* Index of data
17049 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
1704a 70 54 61 62 20 2a 2f 0a 23 69 66 6e 64 65 66 20  pTab */.#ifndef 
1704b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1704c 47 45 52 0a 20 20 54 72 69 67 67 65 72 20 2a 70  GER.  Trigger *p
1704d 54 72 69 67 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  Trig;.#endif..  
1704e 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1704f 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
17050 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( !v ) return;. 
17051 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17052 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
17053 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29  exes(pParse->db)
17054 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   );.  iDb = sqli
17055 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
17056 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
17057 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
17058 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b  ssert( iDb>=0 );
17059 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1705a 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
1705b 2f 2a 20 44 72 6f 70 20 61 6e 79 20 74 61 62 6c  /* Drop any tabl
1705c 65 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20  e triggers from 
1705d 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68  the internal sch
1705e 65 6d 61 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54  ema. */.  for(pT
1705f 72 69 67 3d 70 54 61 62 2d 3e 70 54 72 69 67 67  rig=pTab->pTrigg
17060 65 72 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67  er; pTrig; pTrig
17061 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a  =pTrig->pNext){.
17062 20 20 20 20 69 6e 74 20 69 54 72 69 67 44 62 20      int iTrigDb 
17063 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
17064 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
17065 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d  b, pTrig->pSchem
17066 61 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  a);.    assert( 
17067 69 54 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20  iTrigDb==iDb || 
17068 69 54 72 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20  iTrigDb==1 );.  
17069 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33    sqlite3VdbeOp3
1706a 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  (v, OP_DropTrigg
1706b 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30 2c 20  er, iTrigDb, 0, 
1706c 70 54 72 69 67 2d 3e 6e 61 6d 65 2c 20 30 29 3b  pTrig->name, 0);
1706d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
1706e 2a 20 44 72 6f 70 20 74 68 65 20 74 61 62 6c 65  * Drop the table
1706f 20 61 6e 64 20 69 6e 64 65 78 20 66 72 6f 6d 20   and index from 
17070 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68  the internal sch
17071 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ema */.  sqlite3
17072 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 44 72  VdbeOp3(v, OP_Dr
17073 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c  opTable, iDb, 0,
17074 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pTab->zName, 0)
17075 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74  ;..  /* Reload t
17076 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20  he table, index 
17077 61 6e 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72  and permanent tr
17078 69 67 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a  igger schemas. *
17079 2f 0a 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c  /.  zWhere = sql
1707a 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72  ite3MPrintf(pPar
1707b 73 65 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d  se->db, "tbl_nam
1707c 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  e=%Q", zName);. 
1707d 20 69 66 28 20 21 7a 57 68 65 72 65 20 29 20 72   if( !zWhere ) r
1707e 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
1707f 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 50 61  VdbeOp3(v, OP_Pa
17080 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20  rseSchema, iDb, 
17081 30 2c 20 7a 57 68 65 72 65 2c 20 50 33 5f 44 59  0, zWhere, P3_DY
17082 4e 41 4d 49 43 29 3b 0a 0a 23 69 66 6e 64 65 66  NAMIC);..#ifndef
17083 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
17084 47 47 45 52 0a 20 20 2f 2a 20 4e 6f 77 2c 20 69  GGER.  /* Now, i
17085 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e  f the table is n
17086 6f 74 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ot stored in the
17087 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2c 20   temp database, 
17088 72 65 6c 6f 61 64 20 61 6e 79 20 74 65 6d 70 20  reload any temp 
17089 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20  .  ** triggers. 
1708a 44 6f 6e 27 74 20 75 73 65 20 49 4e 28 2e 2e 2e  Don't use IN(...
1708b 29 20 69 6e 20 63 61 73 65 20 53 51 4c 49 54 45  ) in case SQLITE
1708c 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 69  _OMIT_SUBQUERY i
1708d 73 20 64 65 66 69 6e 65 64 2e 20 0a 20 20 2a 2f  s defined. .  */
1708e 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77  .  if( (zWhere=w
1708f 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73  hereTempTriggers
17090 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21  (pParse, pTab))!
17091 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
17092 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 50  3VdbeOp3(v, OP_P
17093 61 72 73 65 53 63 68 65 6d 61 2c 20 31 2c 20 30  arseSchema, 1, 0
17094 2c 20 7a 57 68 65 72 65 2c 20 50 33 5f 44 59 4e  , zWhere, P3_DYN
17095 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  AMIC);.  }.#endi
17096 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  f.}../*.** Gener
17097 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c  ate code to impl
17098 65 6d 65 6e 74 20 74 68 65 20 22 41 4c 54 45 52  ement the "ALTER
17099 20 54 41 42 4c 45 20 78 78 78 20 52 45 4e 41 4d   TABLE xxx RENAM
1709a 45 20 54 4f 20 79 79 79 22 20 0a 2a 2a 20 63 6f  E TO yyy" .** co
1709b 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54  mmand. .*/.SQLIT
1709c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1709d 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d  qlite3AlterRenam
1709e 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20  eTable(.  Parse 
1709f 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
170a0 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
170a1 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53 72 63 4c  ntext. */.  SrcL
170a2 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
170a3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
170a4 6c 65 20 74 6f 20 72 65 6e 61 6d 65 2e 20 2a 2f  le to rename. */
170a5 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20  .  Token *pName 
170a6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
170a7 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61  The new table na
170a8 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  me. */.){.  int 
170a9 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
170aa 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
170ab 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
170ac 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63  the table */.  c
170ad 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
170ae 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
170af 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 44 62   of database iDb
170b0 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
170b1 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
170b2 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 72  /* Table being r
170b3 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 63 68 61 72  enamed */.  char
170b4 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20   *zName = 0;    
170b5 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74 65        /* NULL-te
170b6 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f 6e  rminated version
170b7 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20 0a 20 20   of pName */ .  
170b8 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
170b9 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74  arse->db; /* Dat
170ba 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
170bb 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 4e 61   */.  int nTabNa
170bc 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
170bd 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 54 46  /* Number of UTF
170be 2d 38 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  -8 characters in
170bf 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a 20 20 63   zTabName */.  c
170c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e  onst char *zTabN
170c1 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67  ame;     /* Orig
170c2 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20 74 68 65  inal name of the
170c3 20 74 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65   table */.  Vdbe
170c4 20 2a 76 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   *v;.#ifndef SQL
170c5 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
170c6 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20  .  char *zWhere 
170c7 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
170c8 57 68 65 72 65 20 63 6c 61 75 73 65 20 74 6f 20  Where clause to 
170c9 6c 6f 63 61 74 65 20 74 65 6d 70 20 74 72 69 67  locate temp trig
170ca 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  gers */.#endif. 
170cb 20 69 6e 74 20 69 73 56 69 72 74 75 61 6c 52 65   int isVirtualRe
170cc 6e 61 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 54 72  name = 0;  /* Tr
170cd 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
170ce 76 2d 74 61 62 6c 65 20 77 69 74 68 20 61 6e 20  v-table with an 
170cf 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a 20 20 0a  xRename() */.  .
170d0 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
170d1 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65 78  Failed ) goto ex
170d2 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
170d3 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 2d  .  assert( pSrc-
170d4 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 61 73  >nSrc==1 );.  as
170d5 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
170d6 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
170d7 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b  s(pParse->db) );
170d8 0a 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ..  pTab = sqlit
170d9 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50  e3LocateTable(pP
170da 61 72 73 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d  arse, pSrc->a[0]
170db 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b  .zName, pSrc->a[
170dc 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
170dd 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74   if( !pTab ) got
170de 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
170df 62 6c 65 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  ble;.  iDb = sql
170e0 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
170e1 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
170e2 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
170e3 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
170e4 62 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20  b].zName;..  /* 
170e5 47 65 74 20 61 20 4e 55 4c 4c 20 74 65 72 6d 69  Get a NULL termi
170e6 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  nated version of
170e7 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
170e8 61 6d 65 2e 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20  ame. */.  zName 
170e9 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
170ea 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
170eb 29 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  );.  if( !zName 
170ec 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61  ) goto exit_rena
170ed 6d 65 5f 74 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20  me_table;..  /* 
170ee 43 68 65 63 6b 20 74 68 61 74 20 61 20 74 61 62  Check that a tab
170ef 6c 65 20 6f 72 20 69 6e 64 65 78 20 6e 61 6d 65  le or index name
170f0 64 20 27 7a 4e 61 6d 65 27 20 64 6f 65 73 20 6e  d 'zName' does n
170f1 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
170f2 0a 20 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73  .  ** in databas
170f3 65 20 69 44 62 2e 20 49 66 20 73 6f 2c 20 74 68  e iDb. If so, th
170f4 69 73 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 0a  is is an error..
170f5 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
170f6 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
170f7 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c 20 73  zName, zDb) || s
170f8 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
170f9 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  db, zName, zDb) 
170fa 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
170fb 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
170fc 20 20 20 20 20 20 20 20 22 74 68 65 72 65 20 69          "there i
170fd 73 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65  s already anothe
170fe 72 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  r table or index
170ff 20 77 69 74 68 20 74 68 69 73 20 6e 61 6d 65 3a   with this name:
17100 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
17101 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61    goto exit_rena
17102 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20  me_table;.  }.. 
17103 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 74   /* Make sure it
17104 20 69 73 20 6e 6f 74 20 61 20 73 79 73 74 65 6d   is not a system
17105 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74   table being alt
17106 65 72 65 64 2c 20 6f 72 20 61 20 72 65 73 65 72  ered, or a reser
17107 76 65 64 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 68  ved name.  ** th
17108 61 74 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  at the table is 
17109 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 74 6f  being renamed to
1710a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 74 72  ..  */.  if( str
1710b 6c 65 6e 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  len(pTab->zName)
1710c 3e 36 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33  >6 && 0==sqlite3
1710d 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a  StrNICmp(pTab->z
1710e 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c  Name, "sqlite_",
1710f 20 37 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74   7) ){.    sqlit
17110 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
17111 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79  e, "table %s may
17112 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22   not be altered"
17113 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
17114 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
17115 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  name_table;.  }.
17116 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
17117 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a  =sqlite3CheckObj
17118 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  ectName(pParse, 
17119 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f  zName) ){.    go
1711a 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
1711b 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  able;.  }..#ifnd
1711c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1711d 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f  UTHORIZATION.  /
1711e 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74  * Invoke the aut
1711f 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
17120 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71  ack. */.  if( sq
17121 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
17122 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c  Parse, SQLITE_AL
17123 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20  TER_TABLE, zDb, 
17124 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20  pTab->zName, 0) 
17125 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
17126 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
17127 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64   }.#endif..#ifnd
17128 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
17129 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
1712a 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
1712b 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
1712c 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
1712d 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
1712e 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69  e_table;.  }.  i
1712f 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
17130 62 29 20 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64  b) && pTab->pMod
17131 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61  ->pModule->xRena
17132 6d 65 20 29 7b 0a 20 20 20 20 69 73 56 69 72 74  me ){.    isVirt
17133 75 61 6c 52 65 6e 61 6d 65 20 3d 20 31 3b 0a 20  ualRename = 1;. 
17134 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
17135 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74  Begin a transact
17136 69 6f 6e 20 61 6e 64 20 63 6f 64 65 20 74 68 65  ion and code the
17137 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f   VerifyCookie fo
17138 72 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20  r database iDb. 
17139 0a 20 20 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66  .  ** Then modif
1713a 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  y the schema coo
1713b 6b 69 65 20 28 73 69 6e 63 65 20 74 68 65 20 41  kie (since the A
1713c 4c 54 45 52 20 54 41 42 4c 45 20 6d 6f 64 69 66  LTER TABLE modif
1713d 69 65 73 20 74 68 65 0a 20 20 2a 2a 20 73 63 68  ies the.  ** sch
1713e 65 6d 61 29 2e 20 4f 70 65 6e 20 61 20 73 74 61  ema). Open a sta
1713f 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
17140 6f 6e 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  on if the table 
17141 69 73 20 61 20 76 69 72 74 75 61 6c 0a 20 20 2a  is a virtual.  *
17142 2a 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  * table..  */.  
17143 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
17144 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
17145 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f  ( v==0 ){.    go
17146 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
17147 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  able;.  }.  sqli
17148 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
17149 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 69  ration(pParse, i
1714a 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 2c 20  sVirtualRename, 
1714b 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 43  iDb);.  sqlite3C
1714c 68 61 6e 67 65 43 6f 6f 6b 69 65 28 64 62 2c 20  hangeCookie(db, 
1714d 76 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49  v, iDb);..  /* I
1714e 66 20 74 68 69 73 20 69 73 20 61 20 76 69 72 74  f this is a virt
1714f 75 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b  ual table, invok
17150 65 20 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20  e the xRename() 
17151 66 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a  function if.  **
17152 20 6f 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e   one is defined.
17153 20 54 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63   The xRename() c
17154 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64  allback will mod
17155 69 66 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20  ify the names.  
17156 2a 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72  ** of any resour
17157 63 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  ces used by the 
17158 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  v-table implemen
17159 74 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e  tation (includin
1715a 67 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c  g other.  ** SQL
1715b 69 74 65 20 74 61 62 6c 65 73 29 20 74 68 61 74  ite tables) that
1715c 20 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20   are identified 
1715d 62 79 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  by the name of t
1715e 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
1715f 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
17160 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
17161 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 69 73  ALTABLE.  if( is
17162 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 29 7b  VirtualRename ){
17163 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
17164 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op3(v, OP_String
17165 38 2c 20 30 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20  8, 0, 0, zName, 
17166 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
17167 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 56 52 65  dbeOp3(v, OP_VRe
17168 6e 61 6d 65 2c 20 30 2c 20 30 2c 20 28 63 6f 6e  name, 0, 0, (con
17169 73 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70  st char*)pTab->p
1716a 56 74 61 62 2c 20 50 33 5f 56 54 41 42 29 3b 0a  Vtab, P3_VTAB);.
1716b 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1716c 20 66 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   figure out how 
1716d 6d 61 6e 79 20 55 54 46 2d 38 20 63 68 61 72 61  many UTF-8 chara
1716e 63 74 65 72 73 20 61 72 65 20 69 6e 20 7a 4e 61  cters are in zNa
1716f 6d 65 20 2a 2f 0a 20 20 7a 54 61 62 4e 61 6d 65  me */.  zTabName
17170 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a   = pTab->zName;.
17171 20 20 6e 54 61 62 4e 61 6d 65 20 3d 20 73 71 6c    nTabName = sql
17172 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
17173 7a 54 61 62 4e 61 6d 65 2c 20 2d 31 29 3b 0a 0a  zTabName, -1);..
17174 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20    /* Modify the 
17175 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
17176 62 6c 65 20 74 6f 20 75 73 65 20 74 68 65 20 6e  ble to use the n
17177 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a  ew table name. *
17178 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65  /.  sqlite3Neste
17179 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
1717a 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e       "UPDATE %Q.
1717b 25 73 20 53 45 54 20 22 0a 23 69 66 64 65 66 20  %s SET ".#ifdef 
1717c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1717d 47 45 52 0a 20 20 20 20 20 20 20 20 20 20 22 73  GER.          "s
1717e 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61  ql = sqlite_rena
1717f 6d 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51  me_table(sql, %Q
17180 29 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20 20 20  ), ".#else.     
17181 20 20 20 20 20 22 73 71 6c 20 3d 20 43 41 53 45       "sql = CASE
17182 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
17183 57 48 45 4e 20 74 79 70 65 20 3d 20 27 74 72 69  WHEN type = 'tri
17184 67 67 65 72 27 20 54 48 45 4e 20 73 71 6c 69 74  gger' THEN sqlit
17185 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72  e_rename_trigger
17186 28 73 71 6c 2c 20 25 51 29 22 0a 20 20 20 20 20  (sql, %Q)".     
17187 20 20 20 20 20 20 20 22 45 4c 53 45 20 73 71 6c         "ELSE sql
17188 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  ite_rename_table
17189 28 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20 22  (sql, %Q) END, "
1718a 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
1718b 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51    "tbl_name = %Q
1718c 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 6e  , ".          "n
1718d 61 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 20 20  ame = CASE ".   
1718e 20 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 74           "WHEN t
1718f 79 70 65 3d 27 74 61 62 6c 65 27 20 54 48 45 4e  ype='table' THEN
17190 20 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20   %Q ".          
17191 20 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49 4b    "WHEN name LIK
17192 45 20 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e  E 'sqlite_autoin
17193 64 65 78 25 25 27 20 41 4e 44 20 74 79 70 65 3d  dex%%' AND type=
17194 27 69 6e 64 65 78 27 20 54 48 45 4e 20 22 0a 20  'index' THEN ". 
17195 20 20 20 20 20 20 20 20 20 20 20 20 22 27 73 71              "'sq
17196 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 27  lite_autoindex_'
17197 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72   || %Q || substr
17198 28 6e 61 6d 65 2c 25 64 2b 31 38 2c 31 30 29 20  (name,%d+18,10) 
17199 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45  ".            "E
1719a 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a 20  LSE name END ". 
1719b 20 20 20 20 20 22 57 48 45 52 45 20 74 62 6c 5f       "WHERE tbl_
1719c 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a 20 20  name=%Q AND ".  
1719d 20 20 20 20 20 20 20 20 22 28 74 79 70 65 3d 27          "(type='
1719e 74 61 62 6c 65 27 20 4f 52 20 74 79 70 65 3d 27  table' OR type='
1719f 69 6e 64 65 78 27 20 4f 52 20 74 79 70 65 3d 27  index' OR type='
171a0 74 72 69 67 67 65 72 27 29 3b 22 2c 20 0a 20 20  trigger');", .  
171a1 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f      zDb, SCHEMA_
171a2 54 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d  TABLE(iDb), zNam
171a3 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c  e, zName, zName,
171a4 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45   .#ifndef SQLITE
171a5 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
171a6 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69      zName,.#endi
171a7 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e  f.      zName, n
171a8 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d  TabName, zTabNam
171a9 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20  e.  );..#ifndef 
171aa 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
171ab 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20 49  INCREMENT.  /* I
171ac 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  f the sqlite_seq
171ad 75 65 6e 63 65 20 74 61 62 6c 65 20 65 78 69 73  uence table exis
171ae 74 73 20 69 6e 20 74 68 69 73 20 64 61 74 61 62  ts in this datab
171af 61 73 65 2c 20 74 68 65 6e 20 75 70 64 61 74 65  ase, then update
171b0 20 0a 20 20 2a 2a 20 69 74 20 77 69 74 68 20 74   .  ** it with t
171b1 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d  he new table nam
171b2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  e..  */.  if( sq
171b3 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
171b4 62 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65  b, "sqlite_seque
171b5 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a 20 20  nce", zDb) ){.  
171b6 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
171b7 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
171b8 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e       "UPDATE %Q.
171b9 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
171ba 73 65 74 20 6e 61 6d 65 20 3d 20 25 51 20 57 48  set name = %Q WH
171bb 45 52 45 20 6e 61 6d 65 20 3d 20 25 51 22 2c 0a  ERE name = %Q",.
171bc 20 20 20 20 20 20 20 20 7a 44 62 2c 20 7a 4e 61          zDb, zNa
171bd 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  me, pTab->zName)
171be 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
171bf 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
171c0 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 49  T_TRIGGER.  /* I
171c1 66 20 74 68 65 72 65 20 61 72 65 20 54 45 4d 50  f there are TEMP
171c2 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69   triggers on thi
171c3 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20  s table, modify 
171c4 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f  the sqlite_temp_
171c5 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 62 6c  master.  ** tabl
171c6 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68 69 73  e. Don't do this
171c7 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
171c8 69 6e 67 20 41 4c 54 45 52 65 64 20 69 73 20 69  ing ALTERed is i
171c9 74 73 65 6c 66 20 6c 6f 63 61 74 65 64 20 69 6e  tself located in
171ca 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 20 64  .  ** the temp d
171cb 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  atabase..  */.  
171cc 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72  if( (zWhere=wher
171cd 65 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50  eTempTriggers(pP
171ce 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20  arse, pTab))!=0 
171cf 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  ){.    sqlite3Ne
171d0 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
171d1 2c 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 41  , .        "UPDA
171d2 54 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  TE sqlite_temp_m
171d3 61 73 74 65 72 20 53 45 54 20 22 0a 20 20 20 20  aster SET ".    
171d4 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73          "sql = s
171d5 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69  qlite_rename_tri
171d6 67 67 65 72 28 73 71 6c 2c 20 25 51 29 2c 20 22  gger(sql, %Q), "
171d7 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 74 62  .            "tb
171d8 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a 20 20  l_name = %Q ".  
171d9 20 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45            "WHERE
171da 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20 7a 4e   %s;", zName, zN
171db 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20  ame, zWhere);.  
171dc 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
171dd 57 68 65 72 65 29 3b 0a 20 20 7d 0a 23 65 6e 64  Where);.  }.#end
171de 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6e  if..  /* Drop an
171df 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e 74  d reload the int
171e0 65 72 6e 61 6c 20 74 61 62 6c 65 20 73 63 68 65  ernal table sche
171e1 6d 61 2e 20 2a 2f 0a 20 20 72 65 6c 6f 61 64 54  ma. */.  reloadT
171e2 61 62 6c 65 53 63 68 65 6d 61 28 70 50 61 72 73  ableSchema(pPars
171e3 65 2c 20 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3b  e, pTab, zName);
171e4 0a 0a 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  ..exit_rename_ta
171e5 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  ble:.  sqlite3Sr
171e6 63 4c 69 73 74 44 65 6c 65 74 65 28 70 53 72 63  cListDelete(pSrc
171e7 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
171e8 65 28 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a  e(zName);.}.../*
171e9 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
171ea 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  n is called afte
171eb 72 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42 4c  r an "ALTER TABL
171ec 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 65  E ... ADD" state
171ed 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e  ment.** has been
171ee 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65 6e   parsed. Argumen
171ef 74 20 70 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69  t pColDef contai
171f0 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74  ns the text of t
171f1 68 65 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e  he new.** column
171f2 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a   definition..**.
171f3 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74 72  ** The Table str
171f4 75 63 74 75 72 65 20 70 50 61 72 73 65 2d 3e 70  ucture pParse->p
171f5 4e 65 77 54 61 62 6c 65 20 77 61 73 20 65 78 74  NewTable was ext
171f6 65 6e 64 65 64 20 74 6f 20 69 6e 63 6c 75 64 65  ended to include
171f7 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 6f 6c 75  .** the new colu
171f8 6d 6e 20 64 75 72 69 6e 67 20 70 61 72 73 69 6e  mn during parsin
171f9 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
171fa 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
171fb 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43  3AlterFinishAddC
171fc 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61  olumn(Parse *pPa
171fd 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c  rse, Token *pCol
171fe 44 65 66 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  Def){.  Table *p
171ff 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  New;            
17200 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50 61    /* Copy of pPa
17201 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a  rse->pNewTable *
17202 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
17203 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17204 20 54 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74   Table being alt
17205 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44  ered */.  int iD
17206 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
17207 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
17208 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73  number */.  cons
17209 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
1720a 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
1720b 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73  e name */.  cons
1720c 74 20 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20  t char *zTab;   
1720d 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e        /* Table n
1720e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ame */.  char *z
1720f 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
17210 20 20 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69     /* Null-termi
17211 6e 61 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66  nated column def
17212 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c  inition */.  Col
17213 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20  umn *pCol;      
17214 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
17215 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78  w column */.  Ex
17216 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 20  pr *pDflt;      
17217 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75          /* Defau
17218 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
17219 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20   new column */. 
1721a 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
1721b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1721c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1721d 63 74 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 69 66 28  ction; */..  if(
1721e 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
1721f 72 65 74 75 72 6e 3b 0a 20 20 70 4e 65 77 20 3d  return;.  pNew =
17220 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
17221 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e  le;.  assert( pN
17222 65 77 20 29 3b 0a 0a 20 20 64 62 20 3d 20 70 50  ew );..  db = pP
17223 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65  arse->db;.  asse
17224 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
17225 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
17226 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73  db) );.  iDb = s
17227 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
17228 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e 70 53  dex(db, pNew->pS
17229 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20  chema);.  zDb = 
1722a 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
1722b 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 70 4e 65  me;.  zTab = pNe
1722c 77 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 70 43 6f 6c  w->zName;.  pCol
1722d 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70   = &pNew->aCol[p
1722e 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20  New->nCol-1];.  
1722f 70 44 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44  pDflt = pCol->pD
17230 66 6c 74 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  flt;.  pTab = sq
17231 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
17232 62 2c 20 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20  b, zTab, zDb);. 
17233 20 61 73 73 65 72 74 28 20 70 54 61 62 20 29 3b   assert( pTab );
17234 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
17235 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
17236 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ION.  /* Invoke 
17237 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  the authorizatio
17238 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20  n callback. */. 
17239 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
1723a 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
1723b 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45  LITE_ALTER_TABLE
1723c 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61  , zDb, pTab->zNa
1723d 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65  me, 0) ){.    re
1723e 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
1723f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65  ..  /* If the de
17240 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
17241 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77  the new column w
17242 61 73 20 73 70 65 63 69 66 69 65 64 20 77 69 74  as specified wit
17243 68 20 61 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61  h a .  ** litera
17244 6c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74  l NULL, then set
17245 20 70 44 66 6c 74 20 74 6f 20 30 2e 20 54 68 69   pDflt to 0. Thi
17246 73 20 73 69 6d 70 6c 69 66 69 65 73 20 63 68 65  s simplifies che
17247 63 6b 69 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61  cking.  ** for a
17248 6e 20 53 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75  n SQL NULL defau
17249 6c 74 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20  lt below..  */. 
1724a 20 69 66 28 20 70 44 66 6c 74 20 26 26 20 70 44   if( pDflt && pD
1724b 66 6c 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c  flt->op==TK_NULL
1724c 20 29 7b 0a 20 20 20 20 70 44 66 6c 74 20 3d 20   ){.    pDflt = 
1724d 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  0;.  }..  /* Che
1724e 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65 77 20  ck that the new 
1724f 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70  column is not sp
17250 65 63 69 66 69 65 64 20 61 73 20 50 52 49 4d 41  ecified as PRIMA
17251 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45  RY KEY or UNIQUE
17252 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20  ..  ** If there 
17253 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  is a NOT NULL co
17254 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e 20 74  nstraint, then t
17255 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
17256 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f   for the.  ** co
17257 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65  lumn must not be
17258 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66   NULL..  */.  if
17259 28 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65  ( pCol->isPrimKe
1725a 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  y ){.    sqlite3
1725b 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1725c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 50   "Cannot add a P
1725d 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
1725e 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  n");.    return;
1725f 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d  .  }.  if( pNew-
17260 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73  >pIndex ){.    s
17261 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
17262 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61  Parse, "Cannot a
17263 64 64 20 61 20 55 4e 49 51 55 45 20 63 6f 6c 75  dd a UNIQUE colu
17264 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  mn");.    return
17265 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c  ;.  }.  if( pCol
17266 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44  ->notNull && !pD
17267 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  flt ){.    sqlit
17268 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
17269 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61 6e  e, .        "Can
1726a 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e 55  not add a NOT NU
1726b 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64  LL column with d
1726c 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c  efault value NUL
1726d 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  L");.    return;
1726e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72  .  }..  /* Ensur
1726f 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65 78  e the default ex
17270 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65  pression is some
17271 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69 74  thing that sqlit
17272 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
17273 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c  ).  ** can handl
17274 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52  e (i.e. not CURR
17275 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20  ENT_TIME etc.). 
17276 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20   */.  if( pDflt 
17277 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
17278 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20  alue *pVal;.    
17279 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75 65  if( sqlite3Value
1727a 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 66  FromExpr(db, pDf
1727b 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  lt, SQLITE_UTF8,
1727c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
1727d 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20  , &pVal) ){.    
1727e 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1727f 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65  ed = 1;.      re
17280 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
17281 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20 20  if( !pVal ){.   
17282 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
17283 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e  sg(pParse, "Cann
17284 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20  ot add a column 
17285 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e  with non-constan
17286 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20 20  t default");.   
17287 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
17288 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
17289 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d  eFree(pVal);.  }
1728a 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68  ..  /* Modify th
1728b 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  e CREATE TABLE s
1728c 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a  tatement. */.  z
1728d 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Col = sqlite3DbS
1728e 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
1728f 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43  *)pColDef->z, pC
17290 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28  olDef->n);.  if(
17291 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61   zCol ){.    cha
17292 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b  r *zEnd = &zCol[
17293 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20  pColDef->n-1];. 
17294 20 20 20 77 68 69 6c 65 28 20 28 7a 45 6e 64 3e     while( (zEnd>
17295 7a 43 6f 6c 20 26 26 20 2a 7a 45 6e 64 3d 3d 27  zCol && *zEnd=='
17296 3b 27 29 20 7c 7c 20 69 73 73 70 61 63 65 28 2a  ;') || isspace(*
17297 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
17298 29 7a 45 6e 64 29 20 29 7b 0a 20 20 20 20 20 20  )zEnd) ){.      
17299 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30 27 3b 0a  *zEnd-- = '\0';.
1729a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1729b 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
1729c 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55  rse, .        "U
1729d 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20  PDATE %Q.%s SET 
1729e 22 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c  ".          "sql
1729f 20 3d 20 73 75 62 73 74 72 28 73 71 6c 2c 31 2c   = substr(sql,1,
172a0 25 64 29 20 7c 7c 20 27 2c 20 27 20 7c 7c 20 25  %d) || ', ' || %
172a1 51 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  Q || substr(sql,
172a2 25 64 2c 6c 65 6e 67 74 68 28 73 71 6c 29 29 20  %d,length(sql)) 
172a3 22 0a 20 20 20 20 20 20 20 20 22 57 48 45 52 45  ".        "WHERE
172a4 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20   type = 'table' 
172a5 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20  AND name = %Q", 
172a6 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45  .      zDb, SCHE
172a7 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70  MA_TABLE(iDb), p
172a8 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65  New->addColOffse
172a9 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61  t, zCol, pNew->a
172aa 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20  ddColOffset+1,. 
172ab 20 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b       zTab.    );
172ac 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
172ad 65 28 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20  e(zCol);.  }..  
172ae 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c  /* If the defaul
172af 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e  t value of the n
172b0 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c  ew column is NUL
172b1 4c 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20  L, then set the 
172b2 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61 74  file.  ** format
172b3 20 74 6f 20 32 2e 20 49 66 20 74 68 65 20 64 65   to 2. If the de
172b4 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74  fault value of t
172b5 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73  he new column is
172b6 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a 20   not NULL,.  ** 
172b7 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  the file format 
172b8 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f 0a  becomes 3..  */.
172b9 20 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d    sqlite3Minimum
172ba 46 69 6c 65 46 6f 72 6d 61 74 28 70 50 61 72 73  FileFormat(pPars
172bb 65 2c 20 69 44 62 2c 20 70 44 66 6c 74 20 3f 20  e, iDb, pDflt ? 
172bc 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52 65  3 : 2);..  /* Re
172bd 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  load the schema 
172be 6f 66 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20  of the modified 
172bf 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c 6f  table. */.  relo
172c0 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50  adTableSchema(pP
172c1 61 72 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62  arse, pTab, pTab
172c2 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a  ->zName);.}../*.
172c3 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
172c4 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
172c5 65 20 70 61 72 73 65 72 20 61 66 74 65 72 20 74  e parser after t
172c6 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 6e  he table-name in
172c7 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 41  .** an "ALTER TA
172c8 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 3e  BLE <table-name>
172c9 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 20   ADD" statement 
172ca 69 73 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d  is parsed. Argum
172cb 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 20  ent .** pSrc is 
172cc 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f 66  the full-name of
172cd 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
172ce 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   altered..**.** 
172cf 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b  This routine mak
172d0 65 73 20 61 20 28 70 61 72 74 69 61 6c 29 20 63  es a (partial) c
172d1 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65  opy of the Table
172d2 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 6f   structure.** fo
172d3 72 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  r the table bein
172d4 67 20 61 6c 74 65 72 65 64 20 61 6e 64 20 73 65  g altered and se
172d5 74 73 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62  ts Parse.pNewTab
172d6 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74  le to point.** t
172d7 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 63  o it. Routines c
172d8 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
172d9 73 65 72 20 61 73 20 74 68 65 20 63 6f 6c 75 6d  ser as the colum
172da 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20  n definition.** 
172db 69 73 20 70 61 72 73 65 64 20 28 69 2e 65 2e 20  is parsed (i.e. 
172dc 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
172dd 28 29 29 20 61 64 64 20 74 68 65 20 6e 65 77 20  ()) add the new 
172de 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 0a  Column data to .
172df 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20 54 68 65  ** the copy. The
172e0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62   copy of the Tab
172e1 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  le structure is 
172e2 64 65 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 6e  deleted by token
172e3 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 20  ize.c .** after 
172e4 70 61 72 73 69 6e 67 20 69 73 20 66 69 6e 69 73  parsing is finis
172e5 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 69  hed..**.** Routi
172e6 6e 65 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  ne sqlite3AlterF
172e7 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29  inishAddColumn()
172e8 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
172e9 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63  to complete.** c
172ea 6f 64 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52  oding the "ALTER
172eb 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20   TABLE ... ADD" 
172ec 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51  statement..*/.SQ
172ed 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
172ee 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65  d sqlite3AlterBe
172ef 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72  ginAddColumn(Par
172f0 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
172f1 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61  ist *pSrc){.  Ta
172f2 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61 62  ble *pNew;.  Tab
172f3 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65  le *pTab;.  Vdbe
172f4 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a   *v;.  int iDb;.
172f5 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
172f6 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33  Alloc;.  sqlite3
172f7 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
172f8 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70  b;..  /* Look up
172f9 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
172fa 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a 20 20 61   altered. */.  a
172fb 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
172fc 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  NewTable==0 );. 
172fd 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
172fe 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
172ff 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69 66  exes(db) );.  if
17300 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
17301 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62  ed ) goto exit_b
17302 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
17303 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
17304 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
17305 72 73 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e  rse, pSrc->a[0].
17306 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b 30  zName, pSrc->a[0
17307 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ].zDatabase);.  
17308 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f  if( !pTab ) goto
17309 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f   exit_begin_add_
1730a 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66 6e 64 65 66  column;..#ifndef
1730b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1730c 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
1730d 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1730e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1730f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
17310 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  virtual tables m
17311 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 65  ay not be altere
17312 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  d");.    goto ex
17313 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
17314 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  umn;.  }.#endif.
17315 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
17316 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 61  this is not an a
17317 74 74 65 6d 70 74 20 74 6f 20 41 4c 54 45 52 20  ttempt to ALTER 
17318 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 69 66 28  a view. */.  if(
17319 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
1731a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1731b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43  orMsg(pParse, "C
1731c 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75  annot add a colu
1731d 6d 6e 20 74 6f 20 61 20 76 69 65 77 22 29 3b 0a  mn to a view");.
1731e 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65      goto exit_be
1731f 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
17320 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
17321 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65  Tab->addColOffse
17322 74 3e 30 20 29 3b 0a 20 20 69 44 62 20 3d 20 73  t>0 );.  iDb = s
17323 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
17324 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
17325 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 50 75  chema);..  /* Pu
17326 74 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  t a copy of the 
17327 54 61 62 6c 65 20 73 74 72 75 63 74 20 69 6e 20  Table struct in 
17328 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 20  Parse.pNewTable 
17329 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c  for the.  ** sql
1732a 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29 20  ite3AddColumn() 
1732b 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 66 72 69  function and fri
1732c 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66 79 2e 0a  ends to modify..
1732d 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54    */.  pNew = (T
1732e 61 62 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 4d  able*)sqlite3DbM
1732f 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
17330 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20  zeof(Table));.  
17331 69 66 28 20 21 70 4e 65 77 20 29 20 67 6f 74 6f  if( !pNew ) goto
17332 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f   exit_begin_add_
17333 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65  column;.  pParse
17334 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e  ->pNewTable = pN
17335 65 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66  ew;.  pNew->nRef
17336 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 43   = 1;.  pNew->nC
17337 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b  ol = pTab->nCol;
17338 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d  .  assert( pNew-
17339 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 6e 41 6c  >nCol>0 );.  nAl
1733a 6c 6f 63 20 3d 20 28 28 28 70 4e 65 77 2d 3e 6e  loc = (((pNew->n
1733b 43 6f 6c 2d 31 29 2f 38 29 2a 38 29 2b 38 3b 0a  Col-1)/8)*8)+8;.
1733c 20 20 61 73 73 65 72 74 28 20 6e 41 6c 6c 6f 63    assert( nAlloc
1733d 3e 3d 70 4e 65 77 2d 3e 6e 43 6f 6c 20 26 26 20  >=pNew->nCol && 
1733e 6e 41 6c 6c 6f 63 25 38 3d 3d 30 20 26 26 20 6e  nAlloc%8==0 && n
1733f 41 6c 6c 6f 63 2d 70 4e 65 77 2d 3e 6e 43 6f 6c  Alloc-pNew->nCol
17340 3c 38 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 61 43  <8 );.  pNew->aC
17341 6f 6c 20 3d 20 28 43 6f 6c 75 6d 6e 2a 29 73 71  ol = (Column*)sq
17342 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
17343 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 43 6f 6c  o(db, sizeof(Col
17344 75 6d 6e 29 2a 6e 41 6c 6c 6f 63 29 3b 0a 20 20  umn)*nAlloc);.  
17345 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71  pNew->zName = sq
17346 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
17347 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
17348 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f    if( !pNew->aCo
17349 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d  l || !pNew->zNam
1734a 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  e ){.    db->mal
1734b 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
1734c 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67     goto exit_beg
1734d 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20  in_add_column;. 
1734e 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77   }.  memcpy(pNew
1734f 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43  ->aCol, pTab->aC
17350 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d  ol, sizeof(Colum
17351 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a  n)*pNew->nCol);.
17352 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65    for(i=0; i<pNe
17353 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  w->nCol; i++){. 
17354 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20     Column *pCol 
17355 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d  = &pNew->aCol[i]
17356 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  ;.    pCol->zNam
17357 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
17358 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e  Dup(db, pCol->zN
17359 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e  ame);.    pCol->
1735a 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70  zColl = 0;.    p
1735b 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a  Col->zType = 0;.
1735c 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20      pCol->pDflt 
1735d 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d  = 0;.  }.  pNew-
1735e 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61  >pSchema = db->a
1735f 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b  Db[iDb].pSchema;
17360 0a 20 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f  .  pNew->addColO
17361 66 66 73 65 74 20 3d 20 70 54 61 62 2d 3e 61 64  ffset = pTab->ad
17362 64 43 6f 6c 4f 66 66 73 65 74 3b 0a 20 20 70 4e  dColOffset;.  pN
17363 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 0a 20  ew->nRef = 1;.. 
17364 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e   /* Begin a tran
17365 73 61 63 74 69 6f 6e 20 61 6e 64 20 69 6e 63 72  saction and incr
17366 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61  ement the schema
17367 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a 20 20 73   cookie.  */.  s
17368 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
17369 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
1736a 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 76 20 3d  , 0, iDb);.  v =
1736b 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
1736c 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21  pParse);.  if( !
1736d 76 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65  v ) goto exit_be
1736e 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
1736f 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
17370 6f 6f 6b 69 65 28 64 62 2c 20 76 2c 20 69 44 62  ookie(db, v, iDb
17371 29 3b 0a 0a 65 78 69 74 5f 62 65 67 69 6e 5f 61  );..exit_begin_a
17372 64 64 5f 63 6f 6c 75 6d 6e 3a 0a 20 20 73 71 6c  dd_column:.  sql
17373 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
17374 65 28 70 53 72 63 29 3b 0a 20 20 72 65 74 75 72  e(pSrc);.  retur
17375 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20  n;.}.#endif  /* 
17376 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42  SQLITE_ALTER_TAB
17377 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  LE */../********
17378 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6c  ****** End of al
17379 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ter.c **********
1737a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1737b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1737c 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1737d 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1737e 65 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a  e analyze.c ****
1737f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17381 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
17382 35 20 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a 20 54  5 July 8.**.** T
17383 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
17384 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
17385 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
17386 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
17387 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
17388 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
17389 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1738a 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1738b 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1738c 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1738d 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1738e 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1738f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
17390 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
17391 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
17392 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
17393 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
17394 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17395 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17396 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17397 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17398 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
17399 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
1739a 64 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  de associated wi
1739b 74 68 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63  th the ANALYZE c
1739c 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 40 28  ommand..**.** @(
1739d 23 29 20 24 49 64 3a 20 61 6e 61 6c 79 7a 65 2e  #) $Id: analyze.
1739e 63 2c 76 20 31 2e 32 33 20 32 30 30 37 2f 30 38  c,v 1.23 2007/08
1739f 2f 32 39 20 31 37 3a 34 33 3a 32 30 20 64 72 68  /29 17:43:20 drh
173a0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
173a1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  f SQLITE_OMIT_AN
173a2 41 4c 59 5a 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ALYZE../*.** Thi
173a3 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61  s routine genera
173a4 74 65 73 20 63 6f 64 65 20 74 68 61 74 20 6f 70  tes code that op
173a5 65 6e 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73  ens the sqlite_s
173a6 74 61 74 31 20 74 61 62 6c 65 20 6f 6e 20 63 75  tat1 table on cu
173a7 72 73 6f 72 0a 2a 2a 20 69 53 74 61 74 43 75 72  rsor.** iStatCur
173a8 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73  ..**.** If the s
173a9 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
173aa 65 73 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76  es does not prev
173ab 69 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 69 74  iously exist, it
173ac 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 20   is created..** 
173ad 49 66 20 69 74 20 64 6f 65 73 20 70 72 65 76 69  If it does previ
173ae 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 61 6c 6c  ously exist, all
173af 20 65 6e 74 69 72 65 73 20 61 73 73 6f 63 69 61   entires associa
173b0 74 65 64 20 77 69 74 68 20 74 61 62 6c 65 20 7a  ted with table z
173b1 57 68 65 72 65 0a 2a 2a 20 61 72 65 20 72 65 6d  Where.** are rem
173b2 6f 76 65 64 2e 20 20 49 66 20 7a 57 68 65 72 65  oved.  If zWhere
173b3 3d 3d 30 20 74 68 65 6e 20 61 6c 6c 20 65 6e 74  ==0 then all ent
173b4 72 69 65 73 20 61 72 65 20 72 65 6d 6f 76 65 64  ries are removed
173b5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
173b6 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 0a   openStatTable(.
173b7 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
173b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
173b9 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
173ba 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20    int iDb,      
173bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
173bc 20 64 61 74 61 62 61 73 65 20 77 65 20 61 72 65   database we are
173bd 20 6c 6f 6f 6b 69 6e 67 20 69 6e 20 2a 2f 0a 20   looking in */. 
173be 20 69 6e 74 20 69 53 74 61 74 43 75 72 2c 20 20   int iStatCur,  
173bf 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
173c0 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
173c1 31 20 74 61 62 6c 65 20 6f 6e 20 74 68 69 73 20  1 table on this 
173c2 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73  cursor */.  cons
173c3 74 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 20  t char *zWhere  
173c4 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 6e      /* Delete en
173c5 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  tries associated
173c6 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65   with this table
173c7 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
173c8 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
173c9 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20  b;.  Db *pDb;.  
173ca 69 6e 74 20 69 52 6f 6f 74 50 61 67 65 3b 0a 20  int iRootPage;. 
173cb 20 54 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20   Table *pStat;. 
173cc 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
173cd 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
173ce 29 3b 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  );..  if( v==0 )
173cf 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
173d0 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
173d1 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64  oldsAllMutexes(d
173d2 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  b) );.  assert( 
173d3 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
173d4 3d 3d 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20  ==db );.  pDb = 
173d5 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
173d6 20 69 66 28 20 28 70 53 74 61 74 20 3d 20 73 71   if( (pStat = sq
173d7 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
173d8 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31  b, "sqlite_stat1
173d9 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d  ", pDb->zName))=
173da 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  =0 ){.    /* The
173db 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
173dc 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74 20 65 78  bles does not ex
173dd 69 73 74 2e 20 20 43 72 65 61 74 65 20 69 74 2e  ist.  Create it.
173de 20 20 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74    .    ** Note t
173df 68 61 74 20 61 20 73 69 64 65 2d 65 66 66 65 63  hat a side-effec
173e0 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  t of the CREATE 
173e1 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
173e2 69 73 20 74 6f 20 6c 65 61 76 65 0a 20 20 20 20  is to leave.    
173e3 2a 2a 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20  ** the rootpage 
173e4 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
173e5 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74   on the top of t
173e6 68 65 20 73 74 61 63 6b 2e 20 20 54 68 69 73 20  he stack.  This 
173e7 69 73 0a 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74  is.    ** import
173e8 61 6e 74 20 62 65 63 61 75 73 65 20 74 68 65 20  ant because the 
173e9 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64 65  OpenWrite opcode
173ea 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20 6e   below will be n
173eb 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20  eeding it. */.  
173ec 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
173ed 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
173ee 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45     "CREATE TABLE
173ef 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31   %Q.sqlite_stat1
173f0 28 74 62 6c 2c 69 64 78 2c 73 74 61 74 29 22 2c  (tbl,idx,stat)",
173f1 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d  .      pDb->zNam
173f2 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 52 6f  e.    );.    iRo
173f3 6f 74 50 61 67 65 20 3d 20 30 3b 20 20 2f 2a 20  otPage = 0;  /* 
173f4 43 61 75 73 65 20 72 6f 6f 74 70 61 67 65 20 74  Cause rootpage t
173f5 6f 20 62 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20  o be taken from 
173f6 74 6f 70 20 6f 66 20 73 74 61 63 6b 20 2a 2f 0a  top of stack */.
173f7 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 57 68 65    }else if( zWhe
173f8 72 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  re ){.    /* The
173f9 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
173fa 62 6c 65 20 65 78 69 73 74 73 2e 20 20 44 65 6c  ble exists.  Del
173fb 65 74 65 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  ete all entries 
173fc 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
173fd 20 20 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65      ** the table
173fe 20 7a 57 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20   zWhere. */.    
173ff 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
17400 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
17401 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
17402 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 20 57  Q.sqlite_stat1 W
17403 48 45 52 45 20 74 62 6c 3d 25 51 22 2c 0a 20 20  HERE tbl=%Q",.  
17404 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c       pDb->zName,
17405 20 7a 57 68 65 72 65 0a 20 20 20 20 29 3b 0a 20   zWhere.    );. 
17406 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70     iRootPage = p
17407 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 7d 65  Stat->tnum;.  }e
17408 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  lse{.    /* The 
17409 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
1740a 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
1740b 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 72  s.  Delete all r
1740c 6f 77 73 2e 20 2a 2f 0a 20 20 20 20 69 52 6f 6f  ows. */.    iRoo
1740d 74 50 61 67 65 20 3d 20 70 53 74 61 74 2d 3e 74  tPage = pStat->t
1740e 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  num;.    sqlite3
1740f 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
17410 43 6c 65 61 72 2c 20 70 53 74 61 74 2d 3e 74 6e  Clear, pStat->tn
17411 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 0a 20  um, iDb);.  }.. 
17412 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c   /* Open the sql
17413 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
17414 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 55 6e 6c  for writing. Unl
17415 65 73 73 20 69 74 20 77 61 73 20 63 72 65 61 74  ess it was creat
17416 65 64 0a 20 20 2a 2a 20 62 79 20 74 68 69 73 20  ed.  ** by this 
17417 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 20 6c 6f  vdbe program, lo
17418 63 6b 20 69 74 20 66 6f 72 20 77 72 69 74 69 6e  ck it for writin
17419 67 20 61 74 20 74 68 65 20 73 68 61 72 65 64 2d  g at the shared-
1741a 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 0a 20 20  cache level. .  
1741b 2a 2a 20 49 66 20 74 68 69 73 20 76 64 62 65 20  ** If this vdbe 
1741c 64 69 64 20 63 72 65 61 74 65 20 74 68 65 20 73  did create the s
1741d 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
1741e 65 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74 20  e, then it must 
1741f 68 61 76 65 20 0a 20 20 2a 2a 20 61 6c 72 65 61  have .  ** alrea
17420 64 79 20 6f 62 74 61 69 6e 65 64 20 61 20 73 63  dy obtained a sc
17421 68 65 6d 61 2d 6c 6f 63 6b 2c 20 6d 61 6b 69 6e  hema-lock, makin
17422 67 20 74 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b  g the write-lock
17423 20 72 65 64 75 6e 64 61 6e 74 2e 0a 20 20 2a 2f   redundant..  */
17424 0a 20 20 69 66 28 20 69 52 6f 6f 74 50 61 67 65  .  if( iRootPage
17425 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  >0 ){.    sqlite
17426 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
17427 65 2c 20 69 44 62 2c 20 69 52 6f 6f 74 50 61 67  e, iDb, iRootPag
17428 65 2c 20 31 2c 20 22 73 71 6c 69 74 65 5f 73 74  e, 1, "sqlite_st
17429 61 74 31 22 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  at1");.  }.  sql
1742a 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1742b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 44 62   OP_Integer, iDb
1742c 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 0);.  sqlite3V
1742d 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f  dbeAddOp(v, OP_O
1742e 70 65 6e 57 72 69 74 65 2c 20 69 53 74 61 74 43  penWrite, iStatC
1742f 75 72 2c 20 69 52 6f 6f 74 50 61 67 65 29 3b 0a  ur, iRootPage);.
17430 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
17431 4f 70 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43  Op(v, OP_SetNumC
17432 6f 6c 75 6d 6e 73 2c 20 69 53 74 61 74 43 75 72  olumns, iStatCur
17433 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  , 3);.}../*.** G
17434 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
17435 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f  do an analysis o
17436 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73  f all indices as
17437 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a  sociated with.**
17438 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e   a single table.
17439 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1743a 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28  analyzeOneTable(
1743b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1743c 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f  ,   /* Parser co
1743d 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
1743e 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54   *pTab,     /* T
1743f 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64 69 63  able whose indic
17440 65 73 20 61 72 65 20 74 6f 20 62 65 20 61 6e 61  es are to be ana
17441 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  lyzed */.  int i
17442 53 74 61 74 43 75 72 2c 20 20 20 20 2f 2a 20 43  StatCur,    /* C
17443 75 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65  ursor that write
17444 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f  s to the sqlite_
17445 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20  stat1 table */. 
17446 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20 20 20   int iMem       
17447 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 6d    /* Available m
17448 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20  emory locations 
17449 62 65 67 69 6e 20 68 65 72 65 20 2a 2f 0a 29 7b  begin here */.){
1744a 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
1744b 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20      /* An index 
1744c 74 6f 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65  to being analyze
1744d 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 43  d */.  int iIdxC
1744e 75 72 3b 20 20 20 20 20 2f 2a 20 43 75 72 73 6f  ur;     /* Curso
1744f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 69 6e 64  r number for ind
17450 65 78 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65  ex being analyze
17451 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  d */.  int nCol;
17452 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
17453 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
17454 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 56  the index */.  V
17455 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
17456 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d  /* The virtual m
17457 61 63 68 69 6e 65 20 62 65 69 6e 67 20 62 75 69  achine being bui
17458 6c 74 20 75 70 20 2a 2f 0a 20 20 69 6e 74 20 69  lt up */.  int i
17459 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ;           /* L
1745a 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1745b 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f 70 3b 20   int topOfLoop; 
1745c 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20    /* The top of 
1745d 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
1745e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 2f  t endOfLoop;   /
1745f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * The end of the
17460 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61   loop */.  int a
17461 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ddr;        /* T
17462 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 6e  he address of an
17463 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
17464 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
17465 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64     /* Index of d
17466 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
17467 6e 67 20 70 54 61 62 20 2a 2f 0a 0a 20 20 76 20  ng pTab */..  v 
17468 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
17469 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
1746a 76 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 30 20  v==0 || pTab==0 
1746b 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d  || pTab->pIndex=
1746c 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20  =0 ){.    /* Do 
1746d 6e 6f 20 61 6e 61 6c 79 73 69 73 20 66 6f 72 20  no analysis for 
1746e 74 61 62 6c 65 73 20 74 68 61 74 20 68 61 76 65  tables that have
1746f 20 6e 6f 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20   no indices */. 
17470 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
17471 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17472 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
17473 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29  exes(pParse->db)
17474 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   );.  iDb = sqli
17475 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
17476 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
17477 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
17478 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b  ssert( iDb>=0 );
17479 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1747a 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
1747b 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ON.  if( sqlite3
1747c 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
1747d 2c 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45  , SQLITE_ANALYZE
1747e 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30  , pTab->zName, 0
1747f 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ,.      pParse->
17480 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
17481 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75  me ) ){.    retu
17482 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  rn;.  }.#endif..
17483 20 20 2f 2a 20 45 73 74 61 62 6c 69 73 68 20 61    /* Establish a
17484 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
17485 65 20 74 61 62 6c 65 20 61 74 20 74 68 65 20 73  e table at the s
17486 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65  hared-cache leve
17487 6c 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54  l. */.  sqlite3T
17488 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
17489 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
1748a 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
1748b 29 3b 0a 0a 20 20 69 49 64 78 43 75 72 20 3d 20  );..  iIdxCur = 
1748c 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20  pParse->nTab;.  
1748d 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
1748e 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
1748f 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
17490 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
17491 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
17492 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
17493 70 49 64 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f  pIdx);..    /* O
17494 70 65 6e 20 61 20 63 75 72 73 6f 72 20 74 6f 20  pen a cursor to 
17495 74 68 65 20 69 6e 64 65 78 20 74 6f 20 62 65 20  the index to be 
17496 61 6e 61 6c 79 7a 65 64 0a 20 20 20 20 2a 2f 0a  analyzed.    */.
17497 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3d      assert( iDb=
17498 3d 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f  =sqlite3SchemaTo
17499 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
1749a 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 29  , pIdx->pSchema)
1749b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
1749c 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
1749d 6e 74 65 67 65 72 2c 20 69 44 62 2c 20 30 29 3b  nteger, iDb, 0);
1749e 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
1749f 28 28 76 2c 20 22 23 20 25 73 22 2c 20 70 49 64  ((v, "# %s", pId
174a0 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20  x->zName));.    
174a1 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
174a2 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69  , OP_OpenRead, i
174a3 49 64 78 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e  IdxCur, pIdx->tn
174a4 75 6d 2c 0a 20 20 20 20 20 20 20 20 28 63 68 61  um,.        (cha
174a5 72 20 2a 29 70 4b 65 79 2c 20 50 33 5f 4b 45 59  r *)pKey, P3_KEY
174a6 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20  INFO_HANDOFF);. 
174a7 20 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e     nCol = pIdx->
174a8 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69 66 28  nColumn;.    if(
174a9 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 3e 3d 70 50   iMem+nCol*2>=pP
174aa 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20  arse->nMem ){.  
174ab 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
174ac 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 2b 31   = iMem+nCol*2+1
174ad 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
174ae 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
174af 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
174b0 2c 20 69 49 64 78 43 75 72 2c 20 6e 43 6f 6c 2b  , iIdxCur, nCol+
174b1 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 65 6d 6f  1);..    /* Memo
174b2 72 79 20 63 65 6c 6c 73 20 61 72 65 20 75 73 65  ry cells are use
174b3 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20  d as follows:.  
174b4 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 6d    **.    **    m
174b5 65 6d 5b 69 4d 65 6d 5d 3a 20 20 20 20 20 20 20  em[iMem]:       
174b6 20 20 20 20 20 20 54 68 65 20 74 6f 74 61 6c 20        The total 
174b7 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
174b8 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20  n the table..   
174b9 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b   **    mem[iMem+
174ba 31 5d 3a 20 20 20 20 20 20 20 20 20 20 20 4e 75  1]:           Nu
174bb 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74  mber of distinct
174bc 20 76 61 6c 75 65 73 20 69 6e 20 63 6f 6c 75 6d   values in colum
174bd 6e 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 2e 2e  n 1.    **    ..
174be 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b  ..    **    mem[
174bf 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a 20 20 20 20 20  iMem+nCol]:     
174c0 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73     Number of dis
174c1 74 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20  tinct values in 
174c2 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20 20 2a 2a 20  column N.    ** 
174c3 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c     mem[iMem+nCol
174c4 2b 31 5d 20 20 20 20 20 20 20 4c 61 73 74 20 6f  +1]       Last o
174c5 62 73 65 72 76 65 64 20 76 61 6c 75 65 20 6f 66  bserved value of
174c6 20 63 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a   column 1.    **
174c7 20 20 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20      ....    **  
174c8 20 20 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b    mem[iMem+nCol+
174c9 6e 43 6f 6c 5d 3a 20 20 20 4c 61 73 74 20 6f 62  nCol]:   Last ob
174ca 73 65 72 76 65 64 20 76 61 6c 75 65 20 6f 66 20  served value of 
174cb 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20 20 2a 2a 0a  column N.    **.
174cc 20 20 20 20 2a 2a 20 43 65 6c 6c 73 20 69 4d 65      ** Cells iMe
174cd 6d 20 74 68 72 6f 75 67 68 20 69 4d 65 6d 2b 6e  m through iMem+n
174ce 43 6f 6c 20 61 72 65 20 69 6e 69 74 69 61 6c 69  Col are initiali
174cf 7a 65 64 20 74 6f 20 30 2e 20 20 54 68 65 20 6f  zed to 0.  The o
174d0 74 68 65 72 73 0a 20 20 20 20 2a 2a 20 61 72 65  thers.    ** are
174d1 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
174d2 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  NULL..    */.   
174d3 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 6e 43 6f   for(i=0; i<=nCo
174d4 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  l; i++){.      s
174d5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
174d6 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 30 2c  v, OP_MemInt, 0,
174d7 20 69 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 7d 0a   iMem+i);.    }.
174d8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
174d9 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
174da 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
174db 70 28 76 2c 20 4f 50 5f 4d 65 6d 4e 75 6c 6c 2c  p(v, OP_MemNull,
174dc 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 2c 20   iMem+nCol+i+1, 
174dd 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  0);.    }..    /
174de 2a 20 44 6f 20 74 68 65 20 61 6e 61 6c 79 73 69  * Do the analysi
174df 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 6e  s..    */.    en
174e0 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65  dOfLoop = sqlite
174e1 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
174e2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
174e3 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65  beAddOp(v, OP_Re
174e4 77 69 6e 64 2c 20 69 49 64 78 43 75 72 2c 20 65  wind, iIdxCur, e
174e5 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 74  ndOfLoop);.    t
174e6 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74  opOfLoop = sqlit
174e7 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
174e8 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r(v);.    sqlite
174e9 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
174ea 5f 4d 65 6d 49 6e 63 72 2c 20 31 2c 20 69 4d 65  _MemIncr, 1, iMe
174eb 6d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  m);.    for(i=0;
174ec 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
174ed 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
174ee 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75  AddOp(v, OP_Colu
174ef 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 69 29 3b  mn, iIdxCur, i);
174f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
174f1 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
174f2 6d 4c 6f 61 64 2c 20 69 4d 65 6d 2b 6e 43 6f 6c  mLoad, iMem+nCol
174f3 2b 69 2b 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  +i+1, 0);.      
174f4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
174f5 28 76 2c 20 4f 50 5f 4e 65 2c 20 30 78 31 30 30  (v, OP_Ne, 0x100
174f6 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
174f7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
174f8 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
174f9 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20  endOfLoop);.    
174fa 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
174fb 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 64 64   i++){.      add
174fc 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
174fd 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e  ddOp(v, OP_MemIn
174fe 63 72 2c 20 31 2c 20 69 4d 65 6d 2b 69 2b 31 29  cr, 1, iMem+i+1)
174ff 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
17500 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 74  dbeChangeP2(v, t
17501 6f 70 4f 66 4c 6f 6f 70 20 2b 20 33 2a 69 20 2b  opOfLoop + 3*i +
17502 20 33 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20   3, addr);.     
17503 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
17504 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  p(v, OP_Column, 
17505 69 49 64 78 43 75 72 2c 20 69 29 3b 0a 20 20 20  iIdxCur, i);.   
17506 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
17507 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f  dOp(v, OP_MemSto
17508 72 65 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b  re, iMem+nCol+i+
17509 31 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20  1, 1);.    }.   
1750a 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
1750b 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f  lveLabel(v, endO
1750c 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69  fLoop);.    sqli
1750d 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1750e 4f 50 5f 4e 65 78 74 2c 20 69 49 64 78 43 75 72  OP_Next, iIdxCur
1750f 2c 20 74 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20  , topOfLoop);.  
17510 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
17511 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  Op(v, OP_Close, 
17512 69 49 64 78 43 75 72 2c 20 30 29 3b 0a 0a 20 20  iIdxCur, 0);..  
17513 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72    /* Store the r
17514 65 73 75 6c 74 73 2e 20 20 0a 20 20 20 20 2a 2a  esults.  .    **
17515 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 75  .    ** The resu
17516 6c 74 20 69 73 20 61 20 73 69 6e 67 6c 65 20 72  lt is a single r
17517 6f 77 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ow of the sqlite
17518 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20 20 54  _stat1 table.  T
17519 68 65 20 66 69 72 73 74 0a 20 20 20 20 2a 2a 20  he first.    ** 
1751a 74 77 6f 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20  two columns are 
1751b 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65  the names of the
1751c 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78   table and index
1751d 2e 20 20 54 68 65 20 74 68 69 72 64 20 63 6f 6c  .  The third col
1751e 75 6d 6e 0a 20 20 20 20 2a 2a 20 69 73 20 61 20  umn.    ** is a 
1751f 73 74 72 69 6e 67 20 63 6f 6d 70 6f 73 65 64 20  string composed 
17520 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 69 6e 74  of a list of int
17521 65 67 65 72 20 73 74 61 74 69 73 74 69 63 73 20  eger statistics 
17522 61 62 6f 75 74 20 74 68 65 0a 20 20 20 20 2a 2a  about the.    **
17523 20 69 6e 64 65 78 2e 20 20 54 68 65 20 66 69 72   index.  The fir
17524 73 74 20 69 6e 74 65 67 65 72 20 69 6e 20 74 68  st integer in th
17525 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 74 6f  e list is the to
17526 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  tal number of en
17527 74 69 72 65 73 0a 20 20 20 20 2a 2a 20 69 6e 20  tires.    ** in 
17528 74 68 65 20 69 6e 64 65 78 2e 20 20 54 68 65 72  the index.  Ther
17529 65 20 69 73 20 6f 6e 65 20 61 64 64 69 74 69 6f  e is one additio
1752a 6e 61 6c 20 69 6e 74 65 67 65 72 20 69 6e 20 74  nal integer in t
1752b 68 65 20 6c 69 73 74 20 66 6f 72 20 65 61 63 68  he list for each
1752c 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6f  .    ** column o
1752d 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68  f the table.  Th
1752e 69 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  is additional in
1752f 74 65 67 65 72 20 69 73 20 61 20 67 75 65 73 73  teger is a guess
17530 20 6f 66 20 68 6f 77 20 6d 61 6e 79 0a 20 20 20   of how many.   
17531 20 2a 2a 20 72 6f 77 73 20 6f 66 20 74 68 65 20   ** rows of the 
17532 74 61 62 6c 65 20 74 68 65 20 69 6e 64 65 78 20  table the index 
17533 77 69 6c 6c 20 73 65 6c 65 63 74 2e 20 20 49 66  will select.  If
17534 20 44 20 69 73 20 74 68 65 20 63 6f 75 6e 74 20   D is the count 
17535 6f 66 20 64 69 73 74 69 6e 63 74 0a 20 20 20 20  of distinct.    
17536 2a 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 4b 20  ** values and K 
17537 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  is the total num
17538 62 65 72 20 6f 66 20 72 6f 77 73 2c 20 74 68 65  ber of rows, the
17539 6e 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73  n the integer is
1753a 20 63 6f 6d 70 75 74 65 64 0a 20 20 20 20 2a 2a   computed.    **
1753b 20 61 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20   as:.    **.    
1753c 2a 2a 20 20 20 20 20 20 20 20 49 20 3d 20 28 4b  **        I = (K
1753d 2b 44 2d 31 29 2f 44 0a 20 20 20 20 2a 2a 0a 20  +D-1)/D.    **. 
1753e 20 20 20 2a 2a 20 49 66 20 4b 3d 3d 30 20 74 68     ** If K==0 th
1753f 65 6e 20 6e 6f 20 65 6e 74 72 79 20 69 73 20 6d  en no entry is m
17540 61 64 65 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ade into the sql
17541 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e  ite_stat1 table.
17542 20 20 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3e 30    .    ** If K>0
17543 20 74 68 65 6e 20 69 74 20 69 73 20 61 6c 77 61   then it is alwa
17544 79 73 20 74 68 65 20 63 61 73 65 20 74 68 65 20  ys the case the 
17545 44 3e 30 20 73 6f 20 64 69 76 69 73 69 6f 6e 20  D>0 so division 
17546 62 79 20 7a 65 72 6f 0a 20 20 20 20 2a 2a 20 69  by zero.    ** i
17547 73 20 6e 65 76 65 72 20 70 6f 73 73 69 62 6c 65  s never possible
17548 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
17549 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1754a 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 69 4d 65   OP_MemLoad, iMe
1754b 6d 2c 20 30 29 3b 0a 20 20 20 20 61 64 64 72 20  m, 0);.    addr 
1754c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1754d 4f 70 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20  Op(v, OP_IfNot, 
1754e 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
1754f 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
17550 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53 74 61  P_NewRowid, iSta
17551 74 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 73 71  tCur, 0);.    sq
17552 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
17553 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30  OP_String8, 0, 0
17554 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30  , pTab->zName, 0
17555 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
17556 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69  beOp3(v, OP_Stri
17557 6e 67 38 2c 20 30 2c 20 30 2c 20 70 49 64 78 2d  ng8, 0, 0, pIdx-
17558 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
17559 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1755a 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20  (v, OP_MemLoad, 
1755b 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20 73 71  iMem, 0);.    sq
1755c 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1755d 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30  OP_String8, 0, 0
1755e 2c 20 22 20 22 2c 20 30 29 3b 0a 20 20 20 20 66  , " ", 0);.    f
1755f 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
17560 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
17561 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
17562 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 69 4d 65 6d  OP_MemLoad, iMem
17563 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
17564 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
17565 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 69 4d 65 6d  OP_MemLoad, iMem
17566 2b 69 2b 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  +i+1, 0);.      
17567 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
17568 28 76 2c 20 4f 50 5f 41 64 64 2c 20 30 2c 20 30  (v, OP_Add, 0, 0
17569 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1756a 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1756b 41 64 64 49 6d 6d 2c 20 2d 31 2c 20 30 29 3b 0a  AddImm, -1, 0);.
1756c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1756d 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
1756e 4c 6f 61 64 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20  Load, iMem+i+1, 
1756f 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
17570 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
17571 5f 44 69 76 69 64 65 2c 20 30 2c 20 30 29 3b 0a  _Divide, 0, 0);.
17572 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
17573 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 54 6f 49  eAddOp(v, OP_ToI
17574 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  nt, 0, 0);.     
17575 20 69 66 28 20 69 3d 3d 6e 43 6f 6c 2d 31 20 29   if( i==nCol-1 )
17576 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
17577 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
17578 5f 43 6f 6e 63 61 74 2c 20 6e 43 6f 6c 2a 32 2d  _Concat, nCol*2-
17579 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c  1, 0);.      }el
1757a 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
1757b 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1757c 4f 50 5f 44 75 70 2c 20 31 2c 20 30 29 3b 0a 20  OP_Dup, 1, 0);. 
1757d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1757e 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1757f 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
17580 2c 20 33 2c 20 30 2c 20 22 61 61 61 22 2c 20 30  , 3, 0, "aaa", 0
17581 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
17582 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e  beAddOp(v, OP_In
17583 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2c 20  sert, iStatCur, 
17584 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a  OPFLAG_APPEND);.
17585 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
17586 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
17587 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
17588 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
17589 74 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65  t will cause the
1758a 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 64   most recent ind
1758b 65 78 20 61 6e 61 6c 79 73 69 73 20 74 6f 0a 2a  ex analysis to.*
1758c 2a 20 62 65 20 6c 61 6f 64 65 64 20 69 6e 74 6f  * be laoded into
1758d 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74   internal hash t
1758e 61 62 6c 65 73 20 77 68 65 72 65 20 69 73 20 63  ables where is c
1758f 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 73  an be used..*/.s
17590 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 41  tatic void loadA
17591 6e 61 6c 79 73 69 73 28 50 61 72 73 65 20 2a 70  nalysis(Parse *p
17592 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b  Parse, int iDb){
17593 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
17594 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
17595 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
17596 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
17597 64 64 4f 70 28 76 2c 20 4f 50 5f 4c 6f 61 64 41  ddOp(v, OP_LoadA
17598 6e 61 6c 79 73 69 73 2c 20 69 44 62 2c 20 30 29  nalysis, iDb, 0)
17599 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
1759a 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
1759b 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e 61  t will do an ana
1759c 6c 79 73 69 73 20 6f 66 20 61 6e 20 65 6e 74 69  lysis of an enti
1759d 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a 73  re database.*/.s
1759e 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79  tatic void analy
1759f 7a 65 44 61 74 61 62 61 73 65 28 50 61 72 73 65  zeDatabase(Parse
175a0 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
175a1 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  b){.  sqlite3 *d
175a2 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
175a3 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
175a4 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
175a5 2e 70 53 63 68 65 6d 61 3b 20 20 20 20 2f 2a 20  .pSchema;    /* 
175a6 53 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61  Schema of databa
175a7 73 65 20 69 44 62 20 2a 2f 0a 20 20 48 61 73 68  se iDb */.  Hash
175a8 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69 6e 74 20 69  Elem *k;.  int i
175a9 53 74 61 74 43 75 72 3b 0a 20 20 69 6e 74 20 69  StatCur;.  int i
175aa 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  Mem;..  sqlite3B
175ab 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
175ac 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
175ad 62 29 3b 0a 20 20 69 53 74 61 74 43 75 72 20 3d  b);.  iStatCur =
175ae 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
175af 0a 20 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65  .  openStatTable
175b0 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 69 53  (pParse, iDb, iS
175b1 74 61 74 43 75 72 2c 20 30 29 3b 0a 20 20 69 4d  tatCur, 0);.  iM
175b2 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  em = pParse->nMe
175b3 6d 3b 0a 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74  m;.  for(k=sqlit
175b4 65 48 61 73 68 46 69 72 73 74 28 26 70 53 63 68  eHashFirst(&pSch
175b5 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 6b  ema->tblHash); k
175b6 3b 20 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; k=sqliteHashNe
175b7 78 74 28 6b 29 29 7b 0a 20 20 20 20 54 61 62 6c  xt(k)){.    Tabl
175b8 65 20 2a 70 54 61 62 20 3d 20 28 54 61 62 6c 65  e *pTab = (Table
175b9 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
175ba 28 6b 29 3b 0a 20 20 20 20 61 6e 61 6c 79 7a 65  (k);.    analyze
175bb 4f 6e 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  OneTable(pParse,
175bc 20 70 54 61 62 2c 20 69 53 74 61 74 43 75 72 2c   pTab, iStatCur,
175bd 20 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 6c 6f   iMem);.  }.  lo
175be 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73  adAnalysis(pPars
175bf 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  e, iDb);.}../*.*
175c0 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
175c1 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20  that will do an 
175c2 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 20 73 69  analysis of a si
175c3 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a  ngle table in.**
175c4 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a   a database..*/.
175c5 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c  static void anal
175c6 79 7a 65 54 61 62 6c 65 28 50 61 72 73 65 20 2a  yzeTable(Parse *
175c7 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
175c8 54 61 62 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b  Tab){.  int iDb;
175c9 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b  .  int iStatCur;
175ca 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ..  assert( pTab
175cb 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
175cc 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
175cd 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61  dsAllMutexes(pPa
175ce 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44  rse->db) );.  iD
175cf 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
175d0 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
175d1 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
175d2 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65  ma);.  sqlite3Be
175d3 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
175d4 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
175d5 29 3b 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20  );.  iStatCur = 
175d6 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
175d7 20 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28    openStatTable(
175d8 70 50 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74  pParse, iDb, iSt
175d9 61 74 43 75 72 2c 20 70 54 61 62 2d 3e 7a 4e 61  atCur, pTab->zNa
175da 6d 65 29 3b 0a 20 20 61 6e 61 6c 79 7a 65 4f 6e  me);.  analyzeOn
175db 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  eTable(pParse, p
175dc 54 61 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70  Tab, iStatCur, p
175dd 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  Parse->nMem);.  
175de 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61  loadAnalysis(pPa
175df 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a  rse, iDb);.}../*
175e0 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
175e1 65 20 66 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a  e for the ANALYZ
175e2 45 20 63 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20  E command.  The 
175e3 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
175e4 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65  s routine.** whe
175e5 6e 20 69 74 20 72 65 63 6f 67 6e 69 7a 65 73 20  n it recognizes 
175e6 61 6e 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  an ANALYZE comma
175e7 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  nd..**.**       
175e8 20 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20   ANALYZE        
175e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
175ea 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20      -- 1.**     
175eb 20 20 20 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74     ANALYZE  <dat
175ec 61 62 61 73 65 3e 20 20 20 20 20 20 20 20 20 20  abase>          
175ed 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20        -- 2.**   
175ee 20 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c       ANALYZE  ?<
175ef 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c  database>.?<tabl
175f0 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a  ename>  -- 3.**.
175f1 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73  ** Form 1 causes
175f2 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20   all indices in 
175f3 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
175f4 61 62 61 73 65 73 20 74 6f 20 62 65 20 61 6e 61  abases to be ana
175f5 6c 79 7a 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32  lyzed..** Form 2
175f6 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e   analyzes all in
175f7 64 69 63 65 73 20 74 68 65 20 73 69 6e 67 6c 65  dices the single
175f8 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 2e   database named.
175f9 0a 2a 2a 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79  .** Form 3 analy
175fa 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  zes all indices 
175fb 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
175fc 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e  the named table.
175fd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
175fe 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
175ff 6e 61 6c 79 7a 65 28 50 61 72 73 65 20 2a 70 50  nalyze(Parse *pP
17600 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
17601 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  me1, Token *pNam
17602 65 32 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  e2){.  sqlite3 *
17603 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
17604 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e  .  int iDb;.  in
17605 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20  t i;.  char *z, 
17606 2a 7a 44 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70  *zDb;.  Table *p
17607 54 61 62 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  Tab;.  Token *pT
17608 61 62 6c 65 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20  ableName;..  /* 
17609 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73  Read the databas
1760a 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20  e schema. If an 
1760b 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65  error occurs, le
1760c 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
1760d 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f  sage.  ** and co
1760e 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64  de in pParse and
1760f 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f   return NULL. */
17610 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17611 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
17612 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
17613 62 29 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  b) );.  if( SQLI
17614 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65  TE_OK!=sqlite3Re
17615 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
17616 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
17617 20 20 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65    }..  if( pName
17618 31 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46  1==0 ){.    /* F
17619 6f 72 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20  orm 1:  Analyze 
1761a 65 76 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20  everything */.  
1761b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
1761c 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
1761d 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e    if( i==1 ) con
1761e 74 69 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f  tinue;  /* Do no
1761f 74 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45  t analyze the TE
17620 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  MP database */. 
17621 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61       analyzeData
17622 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b  base(pParse, i);
17623 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
17624 66 28 20 70 4e 61 6d 65 32 3d 3d 30 20 7c 7c 20  f( pName2==0 || 
17625 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29 7b 0a  pName2->n==0 ){.
17626 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32 3a 20 20      /* Form 2:  
17627 41 6e 61 6c 79 7a 65 20 74 68 65 20 64 61 74 61  Analyze the data
17628 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6e 61  base or table na
17629 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d  med */.    iDb =
1762a 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64   sqlite3FindDb(d
1762b 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20  b, pName1);.    
1762c 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20  if( iDb>=0 ){.  
1762d 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62      analyzeDatab
1762e 61 73 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  ase(pParse, iDb)
1762f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
17630 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61     z = sqlite3Na
17631 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
17632 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 70  pName1);.      p
17633 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  Tab = sqlite3Loc
17634 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
17635 20 7a 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71   z, 0);.      sq
17636 6c 69 74 65 33 5f 66 72 65 65 28 7a 29 3b 0a 20  lite3_free(z);. 
17637 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b       if( pTab ){
17638 0a 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65  .        analyze
17639 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
1763a 61 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ab);.      }.   
1763b 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
1763c 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61 6c 79  /* Form 3: Analy
1763d 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71 75 61  ze the fully qua
1763e 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d  lified table nam
1763f 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73  e */.    iDb = s
17640 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
17641 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
17642 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61 62 6c  , pName2, &pTabl
17643 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  eName);.    if( 
17644 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  iDb>=0 ){.      
17645 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
17646 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  b].zName;.      
17647 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  z = sqlite3NameF
17648 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61  romToken(db, pTa
17649 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  bleName);.      
1764a 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20  if( z ){.       
1764b 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
1764c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
1764d 65 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20  e, z, zDb);.    
1764e 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1764f 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  (z);.        if(
17650 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20   pTab ){.       
17651 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28     analyzeTable(
17652 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
17653 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
17654 0a 20 20 20 20 7d 20 20 20 0a 20 20 7d 0a 7d 0a  .    }   .  }.}.
17655 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 6f 20 70  ./*.** Used to p
17656 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ass information 
17657 66 72 6f 6d 20 74 68 65 20 61 6e 61 6c 79 7a 65  from the analyze
17658 72 20 72 65 61 64 65 72 20 74 68 72 6f 75 67 68  r reader through
17659 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 62   to the.** callb
1765a 61 63 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  ack routine..*/.
1765b 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 61  typedef struct a
1765c 6e 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e 61 6c  nalysisInfo anal
1765d 79 73 69 73 49 6e 66 6f 3b 0a 73 74 72 75 63 74  ysisInfo;.struct
1765e 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 7b 0a   analysisInfo {.
1765f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
17660 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61   const char *zDa
17661 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  tabase;.};../*.*
17662 2a 20 54 68 69 73 20 63 61 6c 6c 62 61 63 6b 20  * This callback 
17663 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20  is invoked once 
17664 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 77  for each index w
17665 68 65 6e 20 72 65 61 64 69 6e 67 20 74 68 65 0a  hen reading the.
17666 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  ** sqlite_stat1 
17667 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 20  table.  .**.**  
17668 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d     argv[0] = nam
17669 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 2a  e of the index.*
1766a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d 20 3d 20  *     argv[1] = 
1766b 72 65 73 75 6c 74 73 20 6f 66 20 61 6e 61 6c 79  results of analy
1766c 73 69 73 20 2d 20 6f 6e 20 69 6e 74 65 67 65 72  sis - on integer
1766d 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
1766e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
1766f 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 28 76 6f  nalysisLoader(vo
17670 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 61  id *pData, int a
17671 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76  rgc, char **argv
17672 2c 20 63 68 61 72 20 2a 2a 61 7a 4e 6f 74 55 73  , char **azNotUs
17673 65 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49  ed){.  analysisI
17674 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e  nfo *pInfo = (an
17675 61 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 74  alysisInfo*)pDat
17676 61 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  a;.  Index *pInd
17677 65 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a  ex;.  int i, c;.
17678 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 76    unsigned int v
17679 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
1767a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  z;..  assert( ar
1767b 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61  gc==2 );.  if( a
1767c 72 67 76 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 30  rgv==0 || argv[0
1767d 5d 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 31 5d 3d  ]==0 || argv[1]=
1767e 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1767f 20 30 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78   0;.  }.  pIndex
17680 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e   = sqlite3FindIn
17681 64 65 78 28 70 49 6e 66 6f 2d 3e 64 62 2c 20 61  dex(pInfo->db, a
17682 72 67 76 5b 30 5d 2c 20 70 49 6e 66 6f 2d 3e 7a  rgv[0], pInfo->z
17683 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28  Database);.  if(
17684 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20   pIndex==0 ){.  
17685 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
17686 20 20 7a 20 3d 20 61 72 67 76 5b 31 5d 3b 0a 20    z = argv[1];. 
17687 20 66 6f 72 28 69 3d 30 3b 20 2a 7a 20 26 26 20   for(i=0; *z && 
17688 69 3c 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75  i<=pIndex->nColu
17689 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20  mn; i++){.    v 
1768a 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  = 0;.    while( 
1768b 28 63 3d 7a 5b 30 5d 29 3e 3d 27 30 27 20 26 26  (c=z[0])>='0' &&
1768c 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20   c<='9' ){.     
1768d 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20   v = v*10 + c - 
1768e 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  '0';.      z++;.
1768f 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78      }.    pIndex
17690 2d 3e 61 69 52 6f 77 45 73 74 5b 69 5d 20 3d 20  ->aiRowEst[i] = 
17691 76 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27  v;.    if( *z=='
17692 20 27 20 29 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20   ' ) z++;.  }.  
17693 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
17694 2a 2a 20 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74  ** Load the cont
17695 65 6e 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ent of the sqlit
17696 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 69 6e  e_stat1 table in
17697 74 6f 20 74 68 65 20 69 6e 64 65 78 20 68 61 73  to the index has
17698 68 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c  h tables..*/.SQL
17699 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1769a 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c  sqlite3AnalysisL
1769b 6f 61 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  oad(sqlite3 *db,
1769c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 61 6e 61   int iDb){.  ana
1769d 6c 79 73 69 73 49 6e 66 6f 20 73 49 6e 66 6f 3b  lysisInfo sInfo;
1769e 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a  .  HashElem *i;.
1769f 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20    char *zSql;.  
176a0 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
176a1 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
176a2 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
176a3 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44  sert( db->aDb[iD
176a4 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61  b].pBt!=0 );.  a
176a5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
176a6 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62  reeHoldsMutex(db
176a7 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20  ->aDb[iDb].pBt) 
176a8 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 61  );..  /* Clear a
176a9 6e 79 20 70 72 69 6f 72 20 73 74 61 74 69 73 74  ny prior statist
176aa 69 63 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 73  ics */.  for(i=s
176ab 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
176ac 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
176ad 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 69  hema->idxHash);i
176ae 3b 69 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  ;i=sqliteHashNex
176af 74 28 69 29 29 7b 0a 20 20 20 20 49 6e 64 65 78  t(i)){.    Index
176b0 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48   *pIdx = sqliteH
176b1 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20  ashData(i);.    
176b2 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f  sqlite3DefaultRo
176b3 77 45 73 74 28 70 49 64 78 29 3b 0a 20 20 7d 0a  wEst(pIdx);.  }.
176b4 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d  .  /* Check to m
176b5 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 71 6c  ake sure the sql
176b6 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
176b7 65 78 69 73 74 73 73 20 2a 2f 0a 20 20 73 49 6e  existss */.  sIn
176b8 66 6f 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 49  fo.db = db;.  sI
176b9 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 20 3d 20  nfo.zDatabase = 
176ba 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
176bb 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  me;.  if( sqlite
176bc 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22  3FindTable(db, "
176bd 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 73  sqlite_stat1", s
176be 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3d  Info.zDatabase)=
176bf 3d 30 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72  =0 ){.     retur
176c0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
176c1 20 20 7d 0a 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20    }...  /* Load 
176c2 6e 65 77 20 73 74 61 74 69 73 74 69 63 73 20 6f  new statistics o
176c3 75 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ut of the sqlite
176c4 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f 0a  _stat1 table */.
176c5 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
176c6 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 53 45 4c  MPrintf(db, "SEL
176c7 45 43 54 20 69 64 78 2c 20 73 74 61 74 20 46 52  ECT idx, stat FR
176c8 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  OM %Q.sqlite_sta
176c9 74 31 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  t1",.           
176ca 20 20 20 20 20 20 20 20 20 20 20 20 20 73 49 6e               sIn
176cb 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  fo.zDatabase);. 
176cc 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
176cd 66 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71  f(db);.  rc = sq
176ce 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a  lite3_exec(db, z
176cf 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f 61  Sql, analysisLoa
176d0 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29 3b  der, &sInfo, 0);
176d1 0a 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  .  sqlite3Safety
176d2 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  On(db);.  sqlite
176d3 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
176d4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23  return rc;.}...#
176d5 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
176d6 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a  OMIT_ANALYZE */.
176d7 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
176d8 20 45 6e 64 20 6f 66 20 61 6e 61 6c 79 7a 65 2e   End of analyze.
176d9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
176da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
176db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
176dc 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
176dd 20 42 65 67 69 6e 20 66 69 6c 65 20 61 74 74 61   Begin file atta
176de 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ch.c ***********
176df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
176e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
176e1 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70 72 69  ./*.** 2003 Apri
176e2 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  l 6.**.** The au
176e3 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
176e4 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
176e5 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
176e6 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
176e7 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
176e8 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
176e9 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
176ea 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
176eb 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
176ec 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
176ed 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
176ee 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
176ef 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
176f0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
176f1 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
176f2 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
176f3 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
176f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
176f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
176f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
176f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
176f8 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
176f9 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
176fa 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
176fb 74 68 65 20 41 54 54 41 43 48 20 61 6e 64 20 44  the ATTACH and D
176fc 45 54 41 43 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a  ETACH commands..
176fd 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 74 74 61 63  **.** $Id: attac
176fe 68 2e 63 2c 76 20 31 2e 36 32 20 32 30 30 37 2f  h.c,v 1.62 2007/
176ff 30 39 2f 30 33 20 31 35 3a 31 39 3a 33 35 20 64  09/03 15:19:35 d
17700 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66  rh Exp $.*/..#if
17701 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17702 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a 2a 20 52 65  _ATTACH./*.** Re
17703 73 6f 6c 76 65 20 61 6e 20 65 78 70 72 65 73 73  solve an express
17704 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 61 72  ion that was par
17705 74 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 6f  t of an ATTACH o
17706 72 20 44 45 54 41 43 48 20 73 74 61 74 65 6d 65  r DETACH stateme
17707 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 73  nt. This.** is s
17708 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e  lightly differen
17709 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76 69 6e 67  t from resolving
1770a 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 20 65 78   a normal SQL ex
1770b 70 72 65 73 73 69 6f 6e 2c 20 62 65 63 61 75 73  pression, becaus
1770c 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 69 64 65 6e  e simple.** iden
1770d 74 69 66 69 65 72 73 20 61 72 65 20 74 72 65 61  tifiers are trea
1770e 74 65 64 20 61 73 20 73 74 72 69 6e 67 73 2c 20  ted as strings, 
1770f 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 63 6f 6c  not possible col
17710 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20 61 6c 69  umn names or ali
17711 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e  ases..**.** i.e.
17712 20 69 66 20 74 68 65 20 70 61 72 73 65 72 20 73   if the parser s
17713 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41  ees:.**.**     A
17714 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 61  TTACH DATABASE a
17715 62 63 20 41 53 20 64 65 66 0a 2a 2a 0a 2a 2a 20  bc AS def.**.** 
17716 69 74 20 74 72 65 61 74 73 20 74 68 65 20 74 77  it treats the tw
17717 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 73  o expressions as
17718 20 6c 69 74 65 72 61 6c 20 73 74 72 69 6e 67 73   literal strings
17719 20 27 61 62 63 27 20 61 6e 64 20 27 64 65 66 27   'abc' and 'def'
1771a 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 6c   instead of.** l
1771b 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 6f 6c 75 6d  ooking for colum
1771c 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e  ns of the same n
1771d 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ame..**.** This 
1771e 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 20  only applies to 
1771f 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66  the root node of
17720 20 70 45 78 70 72 2c 20 73 6f 20 74 68 65 20 73   pExpr, so the s
17721 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20  tatement:.**.** 
17722 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42      ATTACH DATAB
17723 41 53 45 20 61 62 63 7c 7c 64 65 66 20 41 53 20  ASE abc||def AS 
17724 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c  'db2'.**.** will
17725 20 66 61 69 6c 20 62 65 63 61 75 73 65 20 6e 65   fail because ne
17726 69 74 68 65 72 20 61 62 63 20 6f 72 20 64 65 66  ither abc or def
17727 20 63 61 6e 20 62 65 20 72 65 73 6f 6c 76 65 64   can be resolved
17728 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
17729 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
1772a 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70  r(NameContext *p
1772b 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70 45 78 70  Name, Expr *pExp
1772c 72 29 0a 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  r).{.  int rc = 
1772d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
1772e 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66   pExpr ){.    if
1772f 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
17730 49 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ID ){.      rc =
17731 20 73 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f   sqlite3ExprReso
17732 6c 76 65 4e 61 6d 65 73 28 70 4e 61 6d 65 2c 20  lveNames(pName, 
17733 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66  pExpr);.      if
17734 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
17735 26 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49  && !sqlite3ExprI
17736 73 43 6f 6e 73 74 61 6e 74 28 70 45 78 70 72 29  sConstant(pExpr)
17737 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
17738 74 65 33 45 72 72 6f 72 4d 73 67 28 70 4e 61 6d  te3ErrorMsg(pNam
17739 65 2d 3e 70 50 61 72 73 65 2c 20 22 69 6e 76 61  e->pParse, "inva
1773a 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 25 54 5c 22  lid name: \"%T\"
1773b 22 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29  ", &pExpr->span)
1773c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1773d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1773e 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
1773f 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f  {.      pExpr->o
17740 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20  p = TK_STRING;. 
17741 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
17742 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
17743 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e 63 74  n SQL user-funct
17744 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 74  ion registered t
17745 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66  o do the work of
17746 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 65   an ATTACH state
17747 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72  ment. The.** thr
17748 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ee arguments to 
17749 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d  the function com
1774a 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20  e directly from 
1774b 61 6e 20 61 74 74 61 63 68 20 73 74 61 74 65 6d  an attach statem
1774c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41  ent:.**.**     A
1774d 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 78  TTACH DATABASE x
1774e 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a 2a 0a 2a   AS y KEY z.**.*
1774f 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c  *     SELECT sql
17750 69 74 65 5f 61 74 74 61 63 68 28 78 2c 20 79 2c  ite_attach(x, y,
17751 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65   z).**.** If the
17752 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45 59 20 7a   optional "KEY z
17753 22 20 73 79 6e 74 61 78 20 69 73 20 6f 6d 69 74  " syntax is omit
17754 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  ted, an SQL NULL
17755 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
17756 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 6d  e.** third argum
17757 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ent..*/.static v
17758 6f 69 64 20 61 74 74 61 63 68 46 75 6e 63 28 0a  oid attachFunc(.
17759 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1775a 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
1775b 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
1775c 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
1775d 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
1775e 20 72 63 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74   rc = 0;.  sqlit
1775f 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
17760 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
17761 78 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  xt);.  const cha
17762 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73  r *zName;.  cons
17763 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20  t char *zFile;. 
17764 20 44 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61   Db *aNew;.  cha
17765 72 20 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a  r *zErrDyn = 0;.
17766 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d    char zErr[128]
17767 3b 0a 0a 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f  ;..  zFile = (co
17768 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
17769 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1776a 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20  gv[0]);.  zName 
1776b 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
1776c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1776d 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69  xt(argv[1]);.  i
1776e 66 28 20 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46  f( zFile==0 ) zF
1776f 69 6c 65 20 3d 20 22 22 3b 0a 20 20 69 66 28 20  ile = "";.  if( 
17770 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65  zName==0 ) zName
17771 20 3d 20 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65   = "";..  /* Che
17772 63 6b 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f  ck for the follo
17773 77 69 6e 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a  wing errors:.  *
17774 2a 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f  *.  **     * Too
17775 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20 64   many attached d
17776 61 74 61 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20  atabases,.  **  
17777 20 20 20 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e     * Transaction
17778 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a   currently open.
17779 20 20 2a 2a 20 20 20 20 20 2a 20 53 70 65 63 69    **     * Speci
1777a 66 69 65 64 20 64 61 74 61 62 61 73 65 20 6e 61  fied database na
1777b 6d 65 20 61 6c 72 65 61 64 79 20 62 65 69 6e 67  me already being
1777c 20 75 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66   used..  */.  if
1777d 28 20 64 62 2d 3e 6e 44 62 3e 3d 53 51 4c 49 54  ( db->nDb>=SQLIT
1777e 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32  E_MAX_ATTACHED+2
1777f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
17780 73 6e 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20  snprintf(.      
17781 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45  sizeof(zErr), zE
17782 72 72 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 61 74  rr, "too many at
17783 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
17784 20 2d 20 6d 61 78 20 25 64 22 2c 20 0a 20 20 20   - max %d", .   
17785 20 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54     SQLITE_MAX_AT
17786 54 41 43 48 45 44 0a 20 20 20 20 29 3b 0a 20 20  TACHED.    );.  
17787 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
17788 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  ror;.  }.  if( !
17789 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
1778a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
1778b 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
1778c 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20  rr), zErr,.     
1778d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1778e 22 63 61 6e 6e 6f 74 20 41 54 54 41 43 48 20 64  "cannot ATTACH d
1778f 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 74  atabase within t
17790 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
17791 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
17792 72 6f 72 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  ror;.  }.  for(i
17793 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
17794 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  ++){.    char *z
17795 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e   = db->aDb[i].zN
17796 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 7a 20 26  ame;.    if( z &
17797 26 20 7a 4e 61 6d 65 20 26 26 20 73 71 6c 69 74  & zName && sqlit
17798 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 4e 61  e3StrICmp(z, zNa
17799 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
1779a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1779b 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1779c 45 72 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20  Err, .          
1779d 20 20 20 20 20 20 20 20 20 20 20 20 20 22 64 61               "da
1779e 74 61 62 61 73 65 20 25 73 20 69 73 20 61 6c 72  tabase %s is alr
1779f 65 61 64 79 20 69 6e 20 75 73 65 22 2c 20 7a 4e  eady in use", zN
177a0 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ame);.      goto
177a1 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
177a2 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
177a3 6c 6c 6f 63 61 74 65 20 74 68 65 20 6e 65 77 20  llocate the new 
177a4 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 62 2d  entry in the db-
177a5 3e 61 44 62 5b 5d 20 61 72 72 61 79 20 61 6e 64  >aDb[] array and
177a6 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20   initialise the 
177a7 73 63 68 65 6d 61 0a 20 20 2a 2a 20 68 61 73 68  schema.  ** hash
177a8 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
177a9 69 66 28 20 64 62 2d 3e 61 44 62 3d 3d 64 62 2d  if( db->aDb==db-
177aa 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20  >aDbStatic ){.  
177ab 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    aNew = sqlite3
177ac 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  _malloc( sizeof(
177ad 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b  db->aDb[0])*3 );
177ae 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30  .    if( aNew==0
177af 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
177b0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
177b1 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
177b2 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 61    }.    memcpy(a
177b3 4e 65 77 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69  New, db->aDb, si
177b4 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29  zeof(db->aDb[0])
177b5 2a 32 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  *2);.  }else{.  
177b6 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    aNew = sqlite3
177b7 5f 72 65 61 6c 6c 6f 63 28 64 62 2d 3e 61 44 62  _realloc(db->aDb
177b8 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  , sizeof(db->aDb
177b9 5b 30 5d 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29  [0])*(db->nDb+1)
177ba 20 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77   );.    if( aNew
177bb 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  ==0 ){.      db-
177bc 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
177bd 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  1;.      return;
177be 0a 20 20 20 20 7d 20 0a 20 20 7d 0a 20 20 64 62  .    } .  }.  db
177bf 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20  ->aDb = aNew;.  
177c0 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b  aNew = &db->aDb[
177c1 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20 20 6d 65  db->nDb++];.  me
177c2 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c 20 73 69  mset(aNew, 0, si
177c3 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b 0a 0a 20  zeof(*aNew));.. 
177c4 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64 61 74   /* Open the dat
177c5 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74  abase file. If t
177c6 68 65 20 62 74 72 65 65 20 69 73 20 73 75 63 63  he btree is succ
177c7 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c  essfully opened,
177c8 20 75 73 65 0a 20 20 2a 2a 20 69 74 20 74 6f 20   use.  ** it to 
177c9 6f 62 74 61 69 6e 20 74 68 65 20 64 61 74 61 62  obtain the datab
177ca 61 73 65 20 73 63 68 65 6d 61 2e 20 41 74 20 74  ase schema. At t
177cb 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 73 63  his point the sc
177cc 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a 20 6f 72  hema may.  ** or
177cd 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 69 74   may not be init
177ce 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f 0a 20 20  ialised..  */.  
177cf 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
177d0 65 46 61 63 74 6f 72 79 28 64 62 2c 20 7a 46 69  eFactory(db, zFi
177d1 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45  le, 0, SQLITE_DE
177d2 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
177d3 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
177d4 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
177d5 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20 53 51 4c  >openFlags | SQL
177d6 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
177d7 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
177d8 20 20 20 20 20 20 20 20 20 20 20 20 20 26 61 4e               &aN
177d9 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20  ew->pBt);.  if( 
177da 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
177db 0a 20 20 20 20 61 4e 65 77 2d 3e 70 53 63 68 65  .    aNew->pSche
177dc 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  ma = sqlite3Sche
177dd 6d 61 47 65 74 28 64 62 2c 20 61 4e 65 77 2d 3e  maGet(db, aNew->
177de 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20 21 61  pBt);.    if( !a
177df 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a  New->pSchema ){.
177e0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
177e1 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c  E_NOMEM;.    }el
177e2 73 65 20 69 66 28 20 61 4e 65 77 2d 3e 70 53 63  se if( aNew->pSc
177e3 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
177e4 74 20 26 26 20 61 4e 65 77 2d 3e 70 53 63 68 65  t && aNew->pSche
177e5 6d 61 2d 3e 65 6e 63 21 3d 45 4e 43 28 64 62 29  ma->enc!=ENC(db)
177e6 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
177e7 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
177e8 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a  f(zErr), zErr, .
177e9 20 20 20 20 20 20 20 20 22 61 74 74 61 63 68 65          "attache
177ea 64 20 64 61 74 61 62 61 73 65 73 20 6d 75 73 74  d databases must
177eb 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 74 65   use the same te
177ec 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d  xt encoding as m
177ed 61 69 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0a  ain database");.
177ee 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63        goto attac
177ef 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  h_error;.    }. 
177f0 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c     sqlite3PagerL
177f1 6f 63 6b 69 6e 67 4d 6f 64 65 28 73 71 6c 69 74  ockingMode(sqlit
177f2 65 33 42 74 72 65 65 50 61 67 65 72 28 61 4e 65  e3BtreePager(aNe
177f3 77 2d 3e 70 42 74 29 2c 20 64 62 2d 3e 64 66 6c  w->pBt), db->dfl
177f4 74 4c 6f 63 6b 4d 6f 64 65 29 3b 0a 20 20 7d 0a  tLockMode);.  }.
177f5 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20    aNew->zName = 
177f6 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
177f7 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 61 4e  db, zName);.  aN
177f8 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c  ew->safety_level
177f9 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c 49 54   = 3;..#if SQLIT
177fa 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 7b 0a  E_HAS_CODEC.  {.
177fb 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73      extern int s
177fc 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63  qlite3CodecAttac
177fd 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  h(sqlite3*, int,
177fe 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
177ff 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e 20 76  t);.    extern v
17800 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 63  oid sqlite3Codec
17801 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33 2a 2c  GetKey(sqlite3*,
17802 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e   int, void**, in
17803 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b 65  t*);.    int nKe
17804 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b 65  y;.    char *zKe
17805 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d 20 73  y;.    int t = s
17806 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
17807 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20  e(argv[2]);.    
17808 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20 20 20  switch( t ){.   
17809 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
1780a 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20 63 61  NTEGER:.      ca
1780b 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
1780c 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44 79 6e  .        zErrDyn
1780d 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
1780e 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69 64 20  up(db, "Invalid 
1780f 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20 20 20  key value");.   
17810 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
17811 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20  _ERROR;.        
17812 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 0a  break;.        .
17813 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
17814 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 63 61  E_TEXT:.      ca
17815 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a  se SQLITE_BLOB:.
17816 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d 20 73          nKey = s
17817 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
17818 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  es(argv[2]);.   
17819 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63 68 61       zKey = (cha
1781a 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
1781b 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d 29 3b  e_blob(argv[2]);
1781c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1781d 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 20  CodecAttach(db, 
1781e 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c  db->nDb-1, zKey,
1781f 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20   nKey);.        
17820 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 63 61  break;..      ca
17821 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 0a  se SQLITE_NULL:.
17822 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 6b 65          /* No ke
17823 79 20 73 70 65 63 69 66 69 65 64 2e 20 20 55 73  y specified.  Us
17824 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 74  e the key from t
17825 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
17826 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
17827 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 64  te3CodecGetKey(d
17828 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 7a  b, 0, (void**)&z
17829 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 20  Key, &nKey);.   
1782a 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65       sqlite3Code
1782b 63 41 74 74 61 63 68 28 64 62 2c 20 64 62 2d 3e  cAttach(db, db->
1782c 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65  nDb-1, zKey, nKe
1782d 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  y);.        brea
1782e 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  k;.    }.  }.#en
1782f 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  dif..  /* If the
17830 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 64   file was opened
17831 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 72   successfully, r
17832 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 66  ead the schema f
17833 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 62  or the new datab
17834 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 69  ase..  ** If thi
17835 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66 20 6f  s fails, or if o
17836 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20  pening the file 
17837 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63 6c 6f  failed, then clo
17838 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20  se the file and 
17839 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65  .  ** remove the
1783a 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20   entry from the 
1783b 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61 79 2e  db->aDb[] array.
1783c 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72 79 74   i.e. put everyt
1783d 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20 77 61  hing back the wa
1783e 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e 64 20  y.  ** we found 
1783f 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  it..  */.  if( r
17840 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
17841 20 20 20 20 73 71 6c 69 74 65 33 53 61 66 65 74      sqlite3Safet
17842 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 72 63 20  yOn(db);.    rc 
17843 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62  = sqlite3Init(db
17844 2c 20 26 7a 45 72 72 44 79 6e 29 3b 0a 20 20 20  , &zErrDyn);.   
17845 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
17846 66 28 64 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28  f(db);.  }.  if(
17847 20 72 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 69   rc ){.    int i
17848 44 62 20 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31  Db = db->nDb - 1
17849 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
1784a 62 3e 3d 32 20 29 3b 0a 20 20 20 20 69 66 28 20  b>=2 );.    if( 
1784b 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
1784c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1784d 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e  3BtreeClose(db->
1784e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 3b 0a 20  aDb[iDb].pBt);. 
1784f 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
17850 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 20  ].pBt = 0;.     
17851 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
17852 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20 20 7d  chema = 0;.    }
17853 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65  .    sqlite3Rese
17854 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
17855 64 62 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e  db, 0);.    db->
17856 6e 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 69  nDb = iDb;.    i
17857 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
17858 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  MEM ){.      db-
17859 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1785a 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  1;.      sqlite3
1785b 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
1785c 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22 6f 75  (zErr),zErr, "ou
1785d 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20  t of memory");. 
1785e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1785f 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
17860 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45  (sizeof(zErr),zE
17861 72 72 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f  rr, "unable to o
17862 70 65 6e 20 64 61 74 61 62 61 73 65 3a 20 25 73  pen database: %s
17863 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20 7d  ", zFile);.    }
17864 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68  .    goto attach
17865 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 0a 20  _error;.  }.  . 
17866 20 72 65 74 75 72 6e 3b 0a 0a 61 74 74 61 63 68   return;..attach
17867 5f 65 72 72 6f 72 3a 0a 20 20 2f 2a 20 52 65 74  _error:.  /* Ret
17868 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 66 20  urn an error if 
17869 77 65 20 67 65 74 20 68 65 72 65 20 2a 2f 0a 20  we get here */. 
1786a 20 69 66 28 20 7a 45 72 72 44 79 6e 20 29 7b 0a   if( zErrDyn ){.
1786b 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
1786c 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
1786d 2c 20 7a 45 72 72 44 79 6e 2c 20 2d 31 29 3b 0a  , zErrDyn, -1);.
1786e 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1786f 28 7a 45 72 72 44 79 6e 29 3b 0a 20 20 7d 65 6c  (zErrDyn);.  }el
17870 73 65 7b 0a 20 20 20 20 7a 45 72 72 5b 73 69 7a  se{.    zErr[siz
17871 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d 20 30  eof(zErr)-1] = 0
17872 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
17873 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
17874 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20  xt, zErr, -1);. 
17875 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53   }.}../*.** An S
17876 51 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  QL user-function
17877 20 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64   registered to d
17878 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e  o the work of an
17879 20 44 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e   DETACH statemen
1787a 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20  t. The.** three 
1787b 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
1787c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64   function come d
1787d 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 20 64  irectly from a d
1787e 65 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a  etach statement:
1787f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43  .**.**     DETAC
17880 48 20 44 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a  H DATABASE x.**.
17881 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 71  **     SELECT sq
17882 6c 69 74 65 5f 64 65 74 61 63 68 28 78 29 0a 2a  lite_detach(x).*
17883 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65  /.static void de
17884 74 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69  tachFunc(.  sqli
17885 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
17886 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
17887 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
17888 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
17889 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
1788a 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
1788b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1788c 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73  xt(argv[0]);.  s
1788d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
1788e 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63  ite3_user_data(c
1788f 6f 6e 74 65 78 74 29 3b 0a 20 20 69 6e 74 20 69  ontext);.  int i
17890 3b 0a 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b  ;.  Db *pDb = 0;
17891 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38  .  char zErr[128
17892 5d 3b 0a 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d  ];..  if( zName=
17893 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b  =0 ) zName = "";
17894 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
17895 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
17896 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
17897 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d  i];.    if( pDb-
17898 3e 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  >pBt==0 ) contin
17899 75 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ue;.    if( sqli
1789a 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e  te3StrICmp(pDb->
1789b 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30  zName, zName)==0
1789c 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20   ) break;.  }.. 
1789d 20 69 66 28 20 69 3e 3d 64 62 2d 3e 6e 44 62 20   if( i>=db->nDb 
1789e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
1789f 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
178a0 45 72 72 29 2c 7a 45 72 72 2c 20 22 6e 6f 20 73  Err),zErr, "no s
178a1 75 63 68 20 64 61 74 61 62 61 73 65 3a 20 25 73  uch database: %s
178a2 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  ", zName);.    g
178a3 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72  oto detach_error
178a4 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 32 20  ;.  }.  if( i<2 
178a5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
178a6 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
178a7 45 72 72 29 2c 7a 45 72 72 2c 20 22 63 61 6e 6e  Err),zErr, "cann
178a8 6f 74 20 64 65 74 61 63 68 20 64 61 74 61 62 61  ot detach databa
178a9 73 65 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  se %s", zName);.
178aa 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f      goto detach_
178ab 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28  error;.  }.  if(
178ac 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
178ad 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
178ae 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
178af 7a 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20  zErr), zErr,.   
178b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
178b1 20 20 22 63 61 6e 6e 6f 74 20 44 45 54 41 43 48    "cannot DETACH
178b2 20 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e   database within
178b3 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a   transaction");.
178b4 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f      goto detach_
178b5 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28  error;.  }.  if(
178b6 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
178b7 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e  nReadTrans(pDb->
178b8 70 42 74 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  pBt) ){.    sqli
178b9 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
178ba 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20  eof(zErr),zErr, 
178bb 22 64 61 74 61 62 61 73 65 20 25 73 20 69 73 20  "database %s is 
178bc 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61 6d 65 29 3b  locked", zName);
178bd 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68  .    goto detach
178be 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 73  _error;.  }..  s
178bf 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
178c0 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 70 44  (pDb->pBt);.  pD
178c1 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 70 44  b->pBt = 0;.  pD
178c2 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a  b->pSchema = 0;.
178c3 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
178c4 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
178c5 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a   0);.  return;..
178c6 64 65 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20  detach_error:.  
178c7 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
178c8 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
178c9 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  rr, -1);.}../*.*
178ca 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75 72 65  * This procedure
178cb 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20   generates VDBE 
178cc 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c  code for a singl
178cd 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  e invocation of 
178ce 65 69 74 68 65 72 20 74 68 65 0a 2a 2a 20 73 71  either the.** sq
178cf 6c 69 74 65 5f 64 65 74 61 63 68 28 29 20 6f 72  lite_detach() or
178d0 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 29   sqlite_attach()
178d1 20 53 51 4c 20 75 73 65 72 20 66 75 6e 63 74 69   SQL user functi
178d2 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ons..*/.static v
178d3 6f 69 64 20 63 6f 64 65 41 74 74 61 63 68 28 0a  oid codeAttach(.
178d4 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
178d5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
178d6 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
178d7 20 20 69 6e 74 20 74 79 70 65 2c 20 20 20 20 20    int type,     
178d8 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72         /* Either
178d9 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20 6f   SQLITE_ATTACH o
178da 72 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48 20  r SQLITE_DETACH 
178db 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
178dc 2a 7a 46 75 6e 63 2c 20 20 20 2f 2a 20 45 69 74  *zFunc,   /* Eit
178dd 68 65 72 20 22 73 71 6c 69 74 65 5f 61 74 74 61  her "sqlite_atta
178de 63 68 22 20 6f 72 20 22 73 71 6c 69 74 65 5f 64  ch" or "sqlite_d
178df 65 74 61 63 68 20 2a 2f 0a 20 20 69 6e 74 20 6e  etach */.  int n
178e0 46 75 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20  Func,           
178e1 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67  /* Number of arg
178e2 73 20 74 6f 20 70 61 73 73 20 74 6f 20 7a 46 75  s to pass to zFu
178e3 6e 63 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 41  nc */.  Expr *pA
178e4 75 74 68 41 72 67 2c 20 20 20 20 20 20 2f 2a 20  uthArg,      /* 
178e5 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61  Expression to pa
178e6 73 73 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74  ss to authorizat
178e7 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ion callback */.
178e8 20 20 45 78 70 72 20 2a 70 46 69 6c 65 6e 61 6d    Expr *pFilenam
178e9 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  e,     /* Name o
178ea 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  f database file 
178eb 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 62 6e 61  */.  Expr *pDbna
178ec 6d 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  me,       /* Nam
178ed 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
178ee 65 20 74 6f 20 75 73 65 20 69 6e 74 65 72 6e 61  e to use interna
178ef 6c 6c 79 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  lly */.  Expr *p
178f0 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Key           /*
178f1 20 44 61 74 61 62 61 73 65 20 6b 65 79 20 66 6f   Database key fo
178f2 72 20 65 6e 63 72 79 70 74 69 6f 6e 20 65 78 74  r encryption ext
178f3 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69  ension */.){.  i
178f4 6e 74 20 72 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e  nt rc;.  NameCon
178f5 74 65 78 74 20 73 4e 61 6d 65 3b 0a 20 20 56 64  text sName;.  Vd
178f6 62 65 20 2a 76 3b 0a 20 20 46 75 6e 63 44 65 66  be *v;.  FuncDef
178f7 20 2a 70 46 75 6e 63 3b 0a 20 20 73 71 6c 69 74   *pFunc;.  sqlit
178f8 65 33 2a 20 64 62 20 3d 20 70 50 61 72 73 65 2d  e3* db = pParse-
178f9 3e 64 62 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  >db;..#ifndef SQ
178fa 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
178fb 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72 74  IZATION.  assert
178fc 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
178fd 65 64 20 7c 7c 20 70 41 75 74 68 41 72 67 20 29  ed || pAuthArg )
178fe 3b 0a 20 20 69 66 28 20 70 41 75 74 68 41 72 67  ;.  if( pAuthArg
178ff 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 41   ){.    char *zA
17900 75 74 68 41 72 67 20 3d 20 73 71 6c 69 74 65 33  uthArg = sqlite3
17901 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
17902 2c 20 26 70 41 75 74 68 41 72 67 2d 3e 73 70 61  , &pAuthArg->spa
17903 6e 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 41 75  n);.    if( !zAu
17904 74 68 41 72 67 20 29 7b 0a 20 20 20 20 20 20 67  thArg ){.      g
17905 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a  oto attach_end;.
17906 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
17907 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
17908 70 50 61 72 73 65 2c 20 74 79 70 65 2c 20 7a 41  pParse, type, zA
17909 75 74 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a 20  uthArg, 0, 0);. 
1790a 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1790b 7a 41 75 74 68 41 72 67 29 3b 0a 20 20 20 20 69  zAuthArg);.    i
1790c 66 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  f(rc!=SQLITE_OK 
1790d 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 74  ){.      goto at
1790e 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a  tach_end;.    }.
1790f 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
17910 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
17911 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 6d 65  IZATION */..  me
17912 6d 73 65 74 28 26 73 4e 61 6d 65 2c 20 30 2c 20  mset(&sName, 0, 
17913 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e 74 65  sizeof(NameConte
17914 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 2e 70 50  xt));.  sName.pP
17915 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 0a  arse = pParse;..
17916 20 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 4c    if( .      SQL
17917 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
17918 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28  solveAttachExpr(
17919 26 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e 61 6d  &sName, pFilenam
1791a 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c  e)) ||.      SQL
1791b 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
1791c 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28  solveAttachExpr(
1791d 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d 65 29  &sName, pDbname)
1791e 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49 54  ) ||.      SQLIT
1791f 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f  E_OK!=(rc = reso
17920 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26 73  lveAttachExpr(&s
17921 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0a 20 20 29  Name, pKey)).  )
17922 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45  {.    pParse->nE
17923 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 61  rr++;.    goto a
17924 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 7d 0a 0a  ttach_end;.  }..
17925 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
17926 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
17927 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
17928 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e 61 6d  pParse, pFilenam
17929 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  e);.  sqlite3Exp
1792a 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 44  rCode(pParse, pD
1792b 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  bname);.  sqlite
1792c 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
1792d 2c 20 70 4b 65 79 29 3b 0a 0a 20 20 61 73 73 65  , pKey);..  asse
1792e 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c  rt( v || db->mal
1792f 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
17930 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
17931 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
17932 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c 20  OP_Function, 0, 
17933 6e 46 75 6e 63 29 3b 0a 20 20 20 20 70 46 75 6e  nFunc);.    pFun
17934 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46  c = sqlite3FindF
17935 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e  unction(db, zFun
17936 63 2c 20 73 74 72 6c 65 6e 28 7a 46 75 6e 63 29  c, strlen(zFunc)
17937 2c 20 6e 46 75 6e 63 2c 20 53 51 4c 49 54 45 5f  , nFunc, SQLITE_
17938 55 54 46 38 2c 30 29 3b 0a 20 20 20 20 73 71 6c  UTF8,0);.    sql
17939 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
1793a 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29  (v, -1, (char *)
1793b 70 46 75 6e 63 2c 20 50 33 5f 46 55 4e 43 44 45  pFunc, P3_FUNCDE
1793c 46 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65  F);..    /* Code
1793d 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20 46   an OP_Expire. F
1793e 6f 72 20 61 6e 20 41 54 54 41 43 48 20 73 74 61  or an ATTACH sta
1793f 74 65 6d 65 6e 74 2c 20 73 65 74 20 50 31 20 74  tement, set P1 t
17940 6f 20 74 72 75 65 20 28 65 78 70 69 72 65 20 74  o true (expire t
17941 68 69 73 0a 20 20 20 20 2a 2a 20 73 74 61 74 65  his.    ** state
17942 6d 65 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20  ment only). For 
17943 44 45 54 41 43 48 2c 20 73 65 74 20 69 74 20 74  DETACH, set it t
17944 6f 20 66 61 6c 73 65 20 28 65 78 70 69 72 65 20  o false (expire 
17945 61 6c 6c 20 65 78 69 73 74 69 6e 67 0a 20 20 20  all existing.   
17946 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e   ** statements).
17947 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
17948 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
17949 4f 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65  OP_Expire, (type
1794a 3d 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29  ==SQLITE_ATTACH)
1794b 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 0a 61 74 74  , 0);.  }.  .att
1794c 61 63 68 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74  ach_end:.  sqlit
1794d 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 46 69  e3ExprDelete(pFi
1794e 6c 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  lename);.  sqlit
1794f 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 44 62  e3ExprDelete(pDb
17950 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  name);.  sqlite3
17951 45 78 70 72 44 65 6c 65 74 65 28 70 4b 65 79 29  ExprDelete(pKey)
17952 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65  ;.}../*.** Calle
17953 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
17954 74 6f 20 63 6f 6d 70 69 6c 65 20 61 20 44 45 54  to compile a DET
17955 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ACH statement..*
17956 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20  *.**     DETACH 
17957 70 44 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54  pDbname.*/.SQLIT
17958 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
17959 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72  qlite3Detach(Par
1795a 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
1795b 20 2a 70 44 62 6e 61 6d 65 29 7b 0a 20 20 63 6f   *pDbname){.  co
1795c 64 65 41 74 74 61 63 68 28 70 50 61 72 73 65 2c  deAttach(pParse,
1795d 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48 2c 20   SQLITE_DETACH, 
1795e 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68 22 2c  "sqlite_detach",
1795f 20 31 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c 20   1, pDbname, 0, 
17960 30 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a  0, pDbname);.}..
17961 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20  /*.** Called by 
17962 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f  the parser to co
17963 6d 70 69 6c 65 20 61 6e 20 41 54 54 41 43 48 20  mpile an ATTACH 
17964 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
17965 20 20 20 20 20 41 54 54 41 43 48 20 70 20 41 53       ATTACH p AS
17966 20 70 44 62 6e 61 6d 65 20 4b 45 59 20 70 4b 65   pDbname KEY pKe
17967 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  y.*/.SQLITE_PRIV
17968 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
17969 41 74 74 61 63 68 28 50 61 72 73 65 20 2a 70 50  Attach(Parse *pP
1796a 61 72 73 65 2c 20 45 78 70 72 20 2a 70 2c 20 45  arse, Expr *p, E
1796b 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 45 78  xpr *pDbname, Ex
1796c 70 72 20 2a 70 4b 65 79 29 7b 0a 20 20 63 6f 64  pr *pKey){.  cod
1796d 65 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20  eAttach(pParse, 
1796e 53 51 4c 49 54 45 5f 41 54 54 41 43 48 2c 20 22  SQLITE_ATTACH, "
1796f 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20  sqlite_attach", 
17970 33 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61 6d 65  3, p, p, pDbname
17971 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e 64 69  , pKey);.}.#endi
17972 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
17973 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a  _ATTACH */../*.*
17974 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20 66  * Register the f
17975 75 6e 63 74 69 6f 6e 73 20 73 71 6c 69 74 65 5f  unctions sqlite_
17976 61 74 74 61 63 68 20 61 6e 64 20 73 71 6c 69 74  attach and sqlit
17977 65 5f 64 65 74 61 63 68 2e 0a 2a 2f 0a 53 51 4c  e_detach..*/.SQL
17978 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
17979 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 46 75   sqlite3AttachFu
1797a 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  nctions(sqlite3 
1797b 2a 64 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  *db){.#ifndef SQ
1797c 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
1797d 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
1797e 69 6e 74 20 65 6e 63 20 3d 20 53 51 4c 49 54 45  int enc = SQLITE
1797f 5f 55 54 46 38 3b 0a 20 20 73 71 6c 69 74 65 33  _UTF8;.  sqlite3
17980 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
17981 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20  sqlite_attach", 
17982 33 2c 20 65 6e 63 2c 20 64 62 2c 20 61 74 74 61  3, enc, db, atta
17983 63 68 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20  chFunc, 0, 0);. 
17984 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
17985 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 64  nc(db, "sqlite_d
17986 65 74 61 63 68 22 2c 20 31 2c 20 65 6e 63 2c 20  etach", 1, enc, 
17987 64 62 2c 20 64 65 74 61 63 68 46 75 6e 63 2c 20  db, detachFunc, 
17988 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  0, 0);.#endif.}.
17989 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
1798a 65 20 61 20 44 62 46 69 78 65 72 20 73 74 72 75  e a DbFixer stru
1798b 63 74 75 72 65 2e 20 20 54 68 69 73 20 72 6f 75  cture.  This rou
1798c 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c  tine must be cal
1798d 6c 65 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20  led prior.** to 
1798e 70 61 73 73 69 6e 67 20 74 68 65 20 73 74 72 75  passing the stru
1798f 63 74 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20  cture to one of 
17990 74 68 65 20 73 71 6c 69 74 65 46 69 78 41 41 41  the sqliteFixAAA
17991 41 28 29 20 72 6f 75 74 69 6e 65 73 20 62 65 6c  A() routines bel
17992 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ow..**.** The re
17993 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63  turn value indic
17994 61 74 65 73 20 77 68 65 74 68 65 72 20 6f 72 20  ates whether or 
17995 6e 6f 74 20 66 69 78 61 74 69 6f 6e 20 69 73 20  not fixation is 
17996 72 65 71 75 69 72 65 64 2e 20 20 54 52 55 45 0a  required.  TRUE.
17997 2a 2a 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e  ** means we do n
17998 65 65 64 20 74 6f 20 66 69 78 20 74 68 65 20 64  eed to fix the d
17999 61 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63  atabase referenc
1799a 65 73 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 20  es, FALSE means 
1799b 77 65 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51  we do not..*/.SQ
1799c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1799d 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
1799e 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78  .  DbFixer *pFix
1799f 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69  ,      /* The fi
179a0 78 65 72 20 74 6f 20 62 65 20 69 6e 69 74 69 61  xer to be initia
179a1 6c 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65  lized */.  Parse
179a2 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f   *pParse,      /
179a3 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73  * Error messages
179a4 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e   will be written
179a5 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69   here */.  int i
179a6 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  Db,            /
179a7 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64 61  * This is the da
179a8 74 61 62 61 73 65 20 74 68 61 74 20 6d 75 73 74  tabase that must
179a9 20 62 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f   be used */.  co
179aa 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c  nst char *zType,
179ab 20 20 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 72    /* "view", "tr
179ac 69 67 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 65  igger", or "inde
179ad 78 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f  x" */.  const To
179ae 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e  ken *pName  /* N
179af 61 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c  ame of the view,
179b0 20 74 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64   trigger, or ind
179b1 65 78 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  ex */.){.  sqlit
179b2 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 69  e3 *db;..  if( i
179b3 44 62 3c 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29  Db<0 || iDb==1 )
179b4 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20   return 0;.  db 
179b5 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
179b6 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e  assert( db->nDb>
179b7 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70  iDb );.  pFix->p
179b8 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
179b9 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62    pFix->zDb = db
179ba 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
179bb 3b 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20  ;.  pFix->zType 
179bc 3d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 2d  = zType;.  pFix-
179bd 3e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a  >pName = pName;.
179be 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
179bf 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
179c0 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e  ng set of routin
179c1 65 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 20  es walk through 
179c2 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 61  the parse tree a
179c3 6e 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73  nd assign.** a s
179c4 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73 65  pecific database
179c5 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 65   to all table re
179c6 66 65 72 65 6e 63 65 73 20 77 68 65 72 65 20 74  ferences where t
179c7 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
179c8 0a 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e 73  .** was left uns
179c9 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
179ca 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61  original SQL sta
179cb 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69  tement.  The pFi
179cc 78 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d  x structure.** m
179cd 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ust have been in
179ce 69 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20 70  itialized by a p
179cf 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c  rior call to sql
179d0 69 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a  ite3FixInit()..*
179d1 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
179d2 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  nes are used to 
179d3 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61  make sure that a
179d4 6e 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65 72  n index, trigger
179d5 2c 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20  , or.** view in 
179d6 6f 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f 65  one database doe
179d7 73 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 6f  s not refer to o
179d8 62 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66 66  bjects in a diff
179d9 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e 0a  erent database..
179da 2a 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 69  ** (Exception: i
179db 6e 64 69 63 65 73 2c 20 74 72 69 67 67 65 72 73  ndices, triggers
179dc 2c 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20 74  , and views in t
179dd 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
179de 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20   are.** allowed 
179df 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 74  to refer to anyt
179e0 68 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65 66  hing.)  If a ref
179e1 65 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69 63  erence is explic
179e2 69 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20  itly made.** to 
179e3 61 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 64  an object in a d
179e4 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73  ifferent databas
179e5 65 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  e, an error mess
179e6 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a  age is added to.
179e7 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  ** pParse->zErrM
179e8 73 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75  sg and these rou
179e9 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e  tines return non
179ea 2d 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79  -zero.  If every
179eb 74 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20  thing.** checks 
179ec 6f 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69  out, these routi
179ed 6e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f  nes return 0..*/
179ee 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
179ef 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72  int sqlite3FixSr
179f0 63 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72  cList(.  DbFixer
179f1 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a   *pFix,       /*
179f2 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   Context of the 
179f3 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72  fixation */.  Sr
179f4 63 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20  cList *pList    
179f5 20 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 65     /* The Source
179f6 20 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20 61   list to check a
179f7 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a  nd modify */.){.
179f8 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74    int i;.  const
179f9 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74   char *zDb;.  st
179fa 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
179fb 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28  m *pItem;..  if(
179fc 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
179fd 72 6e 20 30 3b 0a 20 20 7a 44 62 20 3d 20 70 46  rn 0;.  zDb = pF
179fe 69 78 2d 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 69  ix->zDb;.  for(i
179ff 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d  =0, pItem=pList-
17a00 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72  >a; i<pList->nSr
17a01 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  c; i++, pItem++)
17a02 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d  {.    if( pItem-
17a03 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30 20 29 7b  >zDatabase==0 ){
17a04 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44  .      pItem->zD
17a05 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65  atabase = sqlite
17a06 33 44 62 53 74 72 44 75 70 28 70 46 69 78 2d 3e  3DbStrDup(pFix->
17a07 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 44 62 29  pParse->db, zDb)
17a08 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
17a09 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
17a0a 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c  Item->zDatabase,
17a0b 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  zDb)!=0 ){.     
17a0c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
17a0d 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2c 0a 20  (pFix->pParse,. 
17a0e 20 20 20 20 20 20 20 20 22 25 73 20 25 54 20 63          "%s %T c
17a0f 61 6e 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20  annot reference 
17a10 6f 62 6a 65 63 74 73 20 69 6e 20 64 61 74 61 62  objects in datab
17a11 61 73 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20  ase %s",.       
17a12 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 2c 20 70    pFix->zType, p
17a13 46 69 78 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 65  Fix->pName, pIte
17a14 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  m->zDatabase);. 
17a15 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
17a16 20 20 20 7d 0a 23 69 66 20 21 64 65 66 69 6e 65     }.#if !define
17a17 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
17a18 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  EW) || !defined(
17a19 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
17a1a 47 45 52 29 0a 20 20 20 20 69 66 28 20 73 71 6c  GER).    if( sql
17a1b 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46  ite3FixSelect(pF
17a1c 69 78 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  ix, pItem->pSele
17a1d 63 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ct) ) return 1;.
17a1e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
17a1f 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 74  ixExpr(pFix, pIt
17a20 65 6d 2d 3e 70 4f 6e 29 20 29 20 72 65 74 75 72  em->pOn) ) retur
17a21 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  n 1;.#endif.  }.
17a22 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 69    return 0;.}.#i
17a23 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
17a24 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
17a25 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
17a26 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51  OMIT_TRIGGER).SQ
17a27 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
17a28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63   sqlite3FixSelec
17a29 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  t(.  DbFixer *pF
17a2a 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ix,       /* Con
17a2b 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61  text of the fixa
17a2c 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74  tion */.  Select
17a2d 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 2f   *pSelect      /
17a2e 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
17a2f 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 66 69 78  tement to be fix
17a30 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61  ed to one databa
17a31 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65  se */.){.  while
17a32 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
17a33 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
17a34 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53  xprList(pFix, pS
17a35 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 29 20 29  elect->pEList) )
17a36 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
17a37 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
17a38 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73  sqlite3FixSrcLis
17a39 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d  t(pFix, pSelect-
17a3a 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20 20 20  >pSrc) ){.      
17a3b 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
17a3c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
17a3d 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 65  ixExpr(pFix, pSe
17a3e 6c 65 63 74 2d 3e 70 57 68 65 72 65 29 20 29 7b  lect->pWhere) ){
17a3f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
17a40 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
17a41 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46  qlite3FixExpr(pF
17a42 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 48 61  ix, pSelect->pHa
17a43 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 20 20 72  ving) ){.      r
17a44 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
17a45 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65     pSelect = pSe
17a46 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20  lect->pPrior;.  
17a47 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
17a48 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
17a49 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  nt sqlite3FixExp
17a4a 72 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  r(.  DbFixer *pF
17a4b 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
17a4c 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
17a4d 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  on */.  Expr *pE
17a4e 78 70 72 20 20 20 20 20 20 20 20 2f 2a 20 54 68  xpr        /* Th
17a4f 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
17a50 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20  be fixed to one 
17a51 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20  database */.){. 
17a52 20 77 68 69 6c 65 28 20 70 45 78 70 72 20 29 7b   while( pExpr ){
17a53 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
17a54 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20  FixSelect(pFix, 
17a55 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 29 20  pExpr->pSelect) 
17a56 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
17a57 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
17a58 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c   sqlite3FixExprL
17a59 69 73 74 28 70 46 69 78 2c 20 70 45 78 70 72 2d  ist(pFix, pExpr-
17a5a 3e 70 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20  >pList) ){.     
17a5b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
17a5c 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
17a5d 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 45  FixExpr(pFix, pE
17a5e 78 70 72 2d 3e 70 52 69 67 68 74 29 20 29 7b 0a  xpr->pRight) ){.
17a5f 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
17a60 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72 20      }.    pExpr 
17a61 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a  = pExpr->pLeft;.
17a62 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
17a63 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
17a64 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45   int sqlite3FixE
17a65 78 70 72 4c 69 73 74 28 0a 20 20 44 62 46 69 78  xprList(.  DbFix
17a66 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a  er *pFix,     /*
17a67 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   Context of the 
17a68 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  fixation */.  Ex
17a69 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20  prList *pList   
17a6a 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
17a6b 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 74  on to be fixed t
17a6c 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a  o one database *
17a6d 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
17a6e 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
17a6f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
17a70 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
17a71 74 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d  turn 0;.  for(i=
17a72 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  0, pItem=pList->
17a73 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  a; i<pList->nExp
17a74 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
17a75 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
17a76 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
17a77 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 7b 0a  Item->pExpr) ){.
17a78 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
17a79 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
17a7a 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 0;.}.#endif..
17a7b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
17a7c 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c 49  MIT_TRIGGER.SQLI
17a7d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
17a7e 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65 72  qlite3FixTrigger
17a7f 53 74 65 70 28 0a 20 20 44 62 46 69 78 65 72 20  Step(.  DbFixer 
17a80 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f  *pFix,     /* Co
17a81 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
17a82 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 67  ation */.  Trigg
17a83 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 2f 2a  erStep *pStep /*
17a84 20 54 68 65 20 74 72 69 67 67 65 72 20 73 74 65   The trigger ste
17a85 70 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e  p be fixed to on
17a86 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b  e database */.){
17a87 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70 20  .  while( pStep 
17a88 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
17a89 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 78  e3FixSelect(pFix
17a8a 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74  , pStep->pSelect
17a8b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
17a8c 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
17a8d 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
17a8e 72 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70  r(pFix, pStep->p
17a8f 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 20 20  Where) ){.      
17a90 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
17a91 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
17a92 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c  ixExprList(pFix,
17a93 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73   pStep->pExprLis
17a94 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  t) ){.      retu
17a95 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
17a96 70 53 74 65 70 20 3d 20 70 53 74 65 70 2d 3e 70  pStep = pStep->p
17a97 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Next;.  }.  retu
17a98 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 0;.}.#endif..
17a99 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
17a9a 45 6e 64 20 6f 66 20 61 74 74 61 63 68 2e 63 20  End of attach.c 
17a9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17a9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17a9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
17a9e 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
17a9f 42 65 67 69 6e 20 66 69 6c 65 20 61 75 74 68 2e  Begin file auth.
17aa0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
17aa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17aa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
17aa3 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61 6e 75 61  /*.** 2003 Janua
17aa4 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ry 11.**.** The 
17aa5 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
17aa6 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
17aa7 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
17aa8 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
17aa9 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
17aaa 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
17aab 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
17aac 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
17aad 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
17aae 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
17aaf 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
17ab0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
17ab1 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
17ab2 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
17ab3 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
17ab4 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
17ab5 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
17ab6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ab7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ab8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17aba 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
17abb 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
17abc 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
17abd 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65  t the sqlite3_se
17abe 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 0a 2a  t_authorizer().*
17abf 2a 20 41 50 49 2e 20 20 54 68 69 73 20 66 61 63  * API.  This fac
17ac0 69 6c 69 74 79 20 69 73 20 61 6e 20 6f 70 74 69  ility is an opti
17ac1 6f 6e 61 6c 20 66 65 61 74 75 72 65 20 6f 66 20  onal feature of 
17ac2 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 45 6d  the library.  Em
17ac3 62 65 64 64 65 64 0a 2a 2a 20 73 79 73 74 65 6d  bedded.** system
17ac4 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6e 65  s that do not ne
17ac5 65 64 20 74 68 69 73 20 66 61 63 69 6c 69 74 79  ed this facility
17ac6 20 6d 61 79 20 6f 6d 69 74 20 69 74 20 62 79 20   may omit it by 
17ac7 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74  recompiling.** t
17ac8 68 65 20 6c 69 62 72 61 72 79 20 77 69 74 68 20  he library with 
17ac9 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  -DSQLITE_OMIT_AU
17aca 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2a  THORIZATION=1.**
17acb 0a 2a 2a 20 24 49 64 3a 20 61 75 74 68 2e 63 2c  .** $Id: auth.c,
17acc 76 20 31 2e 32 38 20 32 30 30 37 2f 30 39 2f 30  v 1.28 2007/09/0
17acd 31 20 31 38 3a 32 34 3a 35 35 20 64 61 6e 69 65  1 18:24:55 danie
17ace 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
17acf 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ./*.** All of th
17ad0 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
17ad1 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69 74 74  ile may be omitt
17ad2 65 64 20 62 79 20 64 65 66 69 6e 69 6e 67 20 61  ed by defining a
17ad3 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d 61 63 72 6f   single.** macro
17ad4 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
17ad5 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
17ad6 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 65  ZATION../*.** Se
17ad7 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65 20 61  t or clear the a
17ad8 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74  ccess authorizat
17ad9 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  ion function..**
17ada 0a 2a 2a 20 54 68 65 20 61 63 63 65 73 73 20 61  .** The access a
17adb 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e  uthorization fun
17adc 63 74 69 6f 6e 20 69 73 20 62 65 20 63 61 6c 6c  ction is be call
17add 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f  ed during the co
17ade 6d 70 69 6c 61 74 69 6f 6e 0a 2a 2a 20 70 68 61  mpilation.** pha
17adf 73 65 20 74 6f 20 76 65 72 69 66 79 20 74 68 61  se to verify tha
17ae0 74 20 74 68 65 20 75 73 65 72 20 68 61 73 20 72  t the user has r
17ae1 65 61 64 20 61 6e 64 2f 6f 72 20 77 72 69 74 65  ead and/or write
17ae2 20 61 63 63 65 73 73 20 70 65 72 6d 69 73 73 69   access permissi
17ae3 6f 6e 20 6f 6e 0a 2a 2a 20 76 61 72 69 6f 75 73  on on.** various
17ae4 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 64   fields of the d
17ae5 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 66 69  atabase.  The fi
17ae6 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
17ae7 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  the auth functio
17ae8 6e 0a 2a 2a 20 69 73 20 61 20 63 6f 70 79 20 6f  n.** is a copy o
17ae9 66 20 74 68 65 20 33 72 64 20 61 72 67 75 6d 65  f the 3rd argume
17aea 6e 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  nt to this routi
17aeb 6e 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ne.  The second 
17aec 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74  argument.** to t
17aed 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  he auth function
17aee 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65   is one of these
17aef 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a   constants:.**.*
17af0 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43  *       SQLITE_C
17af1 52 45 41 54 45 5f 49 4e 44 45 58 0a 2a 2a 20 20  REATE_INDEX.**  
17af2 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
17af3 54 45 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20  TE_TABLE.**     
17af4 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
17af5 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20  TEMP_INDEX.**   
17af6 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54      SQLITE_CREAT
17af7 45 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20  E_TEMP_TABLE.** 
17af8 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45        SQLITE_CRE
17af9 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ATE_TEMP_TRIGGER
17afa 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
17afb 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45  _CREATE_TEMP_VIE
17afc 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  W.**       SQLIT
17afd 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45 52  E_CREATE_TRIGGER
17afe 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
17aff 5f 43 52 45 41 54 45 5f 56 49 45 57 0a 2a 2a 20  _CREATE_VIEW.** 
17b00 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 4c        SQLITE_DEL
17b01 45 54 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  ETE.**       SQL
17b02 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 0a 2a  ITE_DROP_INDEX.*
17b03 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
17b04 52 4f 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20  ROP_TABLE.**    
17b05 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54     SQLITE_DROP_T
17b06 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20  EMP_INDEX.**    
17b07 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54     SQLITE_DROP_T
17b08 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20  EMP_TABLE.**    
17b09 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54     SQLITE_DROP_T
17b0a 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20  EMP_TRIGGER.**  
17b0b 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
17b0c 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20  _TEMP_VIEW.**   
17b0d 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f      SQLITE_DROP_
17b0e 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20  TRIGGER.**      
17b0f 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45   SQLITE_DROP_VIE
17b10 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  W.**       SQLIT
17b11 45 5f 49 4e 53 45 52 54 0a 2a 2a 20 20 20 20 20  E_INSERT.**     
17b12 20 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 0a    SQLITE_PRAGMA.
17b13 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
17b14 52 45 41 44 0a 2a 2a 20 20 20 20 20 20 20 53 51  READ.**       SQ
17b15 4c 49 54 45 5f 53 45 4c 45 43 54 0a 2a 2a 20 20  LITE_SELECT.**  
17b16 20 20 20 20 20 53 51 4c 49 54 45 5f 54 52 41 4e       SQLITE_TRAN
17b17 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20  SACTION.**      
17b18 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 0a 2a   SQLITE_UPDATE.*
17b19 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61  *.** The third a
17b1a 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65  nd fourth argume
17b1b 6e 74 73 20 74 6f 20 74 68 65 20 61 75 74 68 20  nts to the auth 
17b1c 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65  function are the
17b1d 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20   name of.** the 
17b1e 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 63 6f  table and the co
17b1f 6c 75 6d 6e 20 74 68 61 74 20 61 72 65 20 62 65  lumn that are be
17b20 69 6e 67 20 61 63 63 65 73 73 65 64 2e 20 20 54  ing accessed.  T
17b21 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  he auth function
17b22 0a 2a 2a 20 73 68 6f 75 6c 64 20 72 65 74 75 72  .** should retur
17b23 6e 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f  n either SQLITE_
17b24 4f 4b 2c 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c  OK, SQLITE_DENY,
17b25 20 6f 72 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52   or SQLITE_IGNOR
17b26 45 2e 20 20 49 66 0a 2a 2a 20 53 51 4c 49 54 45  E.  If.** SQLITE
17b27 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c  _OK is returned,
17b28 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61   it means that a
17b29 63 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64  ccess is allowed
17b2a 2e 20 20 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a  .  SQLITE_DENY.*
17b2b 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  * means that the
17b2c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77   SQL statement w
17b2d 69 6c 6c 20 6e 65 76 65 72 2d 72 75 6e 20 2d 20  ill never-run - 
17b2e 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  the sqlite3_exec
17b2f 28 29 20 63 61 6c 6c 0a 2a 2a 20 77 69 6c 6c 20  () call.** will 
17b30 72 65 74 75 72 6e 20 77 69 74 68 20 61 6e 20 65  return with an e
17b31 72 72 6f 72 2e 20 20 53 51 4c 49 54 45 5f 49 47  rror.  SQLITE_IG
17b32 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20  NORE means that 
17b33 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
17b34 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 72 75 6e 20  t.** should run 
17b35 62 75 74 20 61 74 74 65 6d 70 74 73 20 74 6f 20  but attempts to 
17b36 72 65 61 64 20 74 68 65 20 73 70 65 63 69 66 69  read the specifi
17b37 65 64 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 72  ed column will r
17b38 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 61 6e  eturn NULL.** an
17b39 64 20 61 74 74 65 6d 70 74 73 20 74 6f 20 77 72  d attempts to wr
17b3a 69 74 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77  ite the column w
17b3b 69 6c 6c 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a  ill be ignored..
17b3c 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e 67 20 74 68  **.** Setting th
17b3d 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20  e auth function 
17b3e 74 6f 20 4e 55 4c 4c 20 64 69 73 61 62 6c 65 73  to NULL disables
17b3f 20 74 68 69 73 20 68 6f 6f 6b 2e 20 20 54 68 65   this hook.  The
17b40 20 64 65 66 61 75 6c 74 0a 2a 2a 20 73 65 74 74   default.** sett
17b41 69 6e 67 20 6f 66 20 74 68 65 20 61 75 74 68 20  ing of the auth 
17b42 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c  function is NULL
17b43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
17b44 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  int sqlite3_set_
17b45 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71  authorizer(.  sq
17b46 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74  lite3 *db,.  int
17b47 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c   (*xAuth)(void*,
17b48 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  int,const char*,
17b49 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
17b4a 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
17b4b 61 72 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  ar*),.  void *pA
17b4c 72 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  rg.){.  sqlite3_
17b4d 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
17b4e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78 41  mutex);.  db->xA
17b4f 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 20 20 64  uth = xAuth;.  d
17b50 62 2d 3e 70 41 75 74 68 41 72 67 20 3d 20 70 41  b->pAuthArg = pA
17b51 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  rg;.  sqlite3Exp
17b52 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
17b53 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 73 71 6c  ments(db);.  sql
17b54 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
17b55 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
17b56 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
17b57 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
17b58 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
17b59 20 69 6e 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45   into pParse->zE
17b5a 72 72 4d 73 67 20 74 68 61 74 20 65 78 70 6c 61  rrMsg that expla
17b5b 69 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ins that the.** 
17b5c 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20 61 75  user-supplied au
17b5d 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63  thorization func
17b5e 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61 6e  tion returned an
17b5f 20 69 6c 6c 65 67 61 6c 20 76 61 6c 75 65 2e 0a   illegal value..
17b60 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
17b61 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 74 75  qliteAuthBadRetu
17b62 72 6e 43 6f 64 65 28 50 61 72 73 65 20 2a 70 50  rnCode(Parse *pP
17b63 61 72 73 65 2c 20 69 6e 74 20 72 63 29 7b 0a 20  arse, int rc){. 
17b64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
17b65 28 70 50 61 72 73 65 2c 20 22 69 6c 6c 65 67 61  (pParse, "illega
17b66 6c 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 28  l return value (
17b67 25 64 29 20 66 72 6f 6d 20 74 68 65 20 22 0a 20  %d) from the ". 
17b68 20 20 20 22 61 75 74 68 6f 72 69 7a 61 74 69 6f     "authorizatio
17b69 6e 20 66 75 6e 63 74 69 6f 6e 20 2d 20 73 68 6f  n function - sho
17b6a 75 6c 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b  uld be SQLITE_OK
17b6b 2c 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c  , SQLITE_IGNORE,
17b6c 20 22 0a 20 20 20 20 22 6f 72 20 53 51 4c 49 54   ".    "or SQLIT
17b6d 45 5f 44 45 4e 59 22 2c 20 72 63 29 3b 0a 20 20  E_DENY", rc);.  
17b6e 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
17b6f 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a  ITE_ERROR;.}../*
17b70 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 20 73 68  .** The pExpr sh
17b71 6f 75 6c 64 20 62 65 20 61 20 54 4b 5f 43 4f 4c  ould be a TK_COL
17b72 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  UMN expression. 
17b73 20 54 68 65 20 74 61 62 6c 65 20 72 65 66 65 72   The table refer
17b74 72 65 64 20 74 6f 0a 2a 2a 20 69 73 20 69 6e 20  red to.** is in 
17b75 70 54 61 62 4c 69 73 74 20 6f 72 20 65 6c 73 65  pTabList or else
17b76 20 69 74 20 69 73 20 74 68 65 20 4e 45 57 20 6f   it is the NEW o
17b77 72 20 4f 4c 44 20 74 61 62 6c 65 20 6f 66 20 61  r OLD table of a
17b78 20 74 72 69 67 67 65 72 2e 20 20 0a 2a 2a 20 43   trigger.  .** C
17b79 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69  heck to see if i
17b7a 74 20 69 73 20 4f 4b 20 74 6f 20 72 65 61 64 20  t is OK to read 
17b7b 74 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20  this particular 
17b7c 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  column..**.** If
17b7d 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69   the auth functi
17b7e 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  on returns SQLIT
17b7f 45 5f 49 47 4e 4f 52 45 2c 20 63 68 61 6e 67 65  E_IGNORE, change
17b80 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 0a   the TK_COLUMN .
17b81 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  ** instruction i
17b82 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c 4c 2e 20 20  nto a TK_NULL.  
17b83 49 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  If the auth func
17b84 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c  tion returns SQL
17b85 49 54 45 5f 44 45 4e 59 2c 0a 2a 2a 20 74 68 65  ITE_DENY,.** the
17b86 6e 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72  n generate an er
17b87 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ror..*/.SQLITE_P
17b88 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
17b89 74 65 33 41 75 74 68 52 65 61 64 28 0a 20 20 50  te3AuthRead(.  P
17b8a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
17b8b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
17b8c 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
17b8d 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
17b8e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
17b8f 72 65 73 73 69 6f 6e 20 74 6f 20 63 68 65 63 6b  ression to check
17b90 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 6f   authorization o
17b91 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  n */.  SrcList *
17b92 70 54 61 62 4c 69 73 74 20 20 20 20 20 2f 2a 20  pTabList     /* 
17b93 41 6c 6c 20 74 61 62 6c 65 20 74 68 61 74 20 70  All table that p
17b94 45 78 70 72 20 6d 69 67 68 74 20 72 65 66 65 72  Expr might refer
17b95 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69   to */.){.  sqli
17b96 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
17b97 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  ->db;.  int rc;.
17b98 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
17b99 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74  0;      /* The t
17b9a 61 62 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20  able being read 
17b9b 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
17b9c 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61  *zCol;     /* Na
17b9d 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  me of the column
17b9e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
17b9f 0a 20 20 69 6e 74 20 69 53 72 63 3b 20 20 20 20  .  int iSrc;    
17ba0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
17ba1 78 20 69 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61  x in pTabList->a
17ba2 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e  [] of table bein
17ba3 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73  g read */.  cons
17ba4 74 20 63 68 61 72 20 2a 7a 44 42 61 73 65 3b 20  t char *zDBase; 
17ba5 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
17ba6 61 62 61 73 65 20 62 65 69 6e 67 20 61 63 63 65  abase being acce
17ba7 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65  ssed */.  Trigge
17ba8 72 53 74 61 63 6b 20 2a 70 53 74 61 63 6b 3b 20  rStack *pStack; 
17ba9 2f 2a 20 54 68 65 20 73 74 61 63 6b 20 6f 66 20  /* The stack of 
17baa 63 75 72 72 65 6e 74 20 74 72 69 67 67 65 72 73  current triggers
17bab 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
17bac 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
17bad 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
17bae 64 61 74 61 62 61 73 65 20 74 68 65 20 65 78 70  database the exp
17baf 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73 20 74  ression refers t
17bb0 6f 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e  o */..  if( db->
17bb1 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74 75 72  xAuth==0 ) retur
17bb2 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  n;.  if( pExpr->
17bb3 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
17bb4 72 65 74 75 72 6e 3b 0a 20 20 69 44 62 20 3d 20  return;.  iDb = 
17bb5 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
17bb6 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
17bb7 20 70 45 78 70 72 2d 3e 70 53 63 68 65 6d 61 29   pExpr->pSchema)
17bb8 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b  ;.  if( iDb<0 ){
17bb9 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74 74 65 6d  .    /* An attem
17bba 70 74 20 74 6f 20 72 65 61 64 20 61 20 63 6f 6c  pt to read a col
17bbb 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20 73 75 62  umn out of a sub
17bbc 71 75 65 72 79 20 6f 72 20 6f 74 68 65 72 0a 20  query or other. 
17bbd 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20     ** temporary 
17bbe 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 65  table. */.    re
17bbf 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
17bc0 69 53 72 63 3d 30 3b 20 70 54 61 62 4c 69 73 74  iSrc=0; pTabList
17bc1 20 26 26 20 69 53 72 63 3c 70 54 61 62 4c 69 73   && iSrc<pTabLis
17bc2 74 2d 3e 6e 53 72 63 3b 20 69 53 72 63 2b 2b 29  t->nSrc; iSrc++)
17bc3 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d  {.    if( pExpr-
17bc4 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69 73  >iTable==pTabLis
17bc5 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72 73  t->a[iSrc].iCurs
17bc6 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  or ) break;.  }.
17bc7 20 20 69 66 28 20 69 53 72 63 3e 3d 30 20 26 26    if( iSrc>=0 &&
17bc8 20 70 54 61 62 4c 69 73 74 20 26 26 20 69 53 72   pTabList && iSr
17bc9 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  c<pTabList->nSrc
17bca 20 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 70   ){.    pTab = p
17bcb 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d  TabList->a[iSrc]
17bcc 2e 70 54 61 62 3b 0a 20 20 7d 65 6c 73 65 20 69  .pTab;.  }else i
17bcd 66 28 20 28 70 53 74 61 63 6b 20 3d 20 70 50 61  f( (pStack = pPa
17bce 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 29 21  rse->trigStack)!
17bcf 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  =0 ){.    /* Thi
17bd0 73 20 6d 75 73 74 20 62 65 20 61 6e 20 61 74 74  s must be an att
17bd1 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 65  empt to read the
17bd2 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75   NEW or OLD pseu
17bd3 64 6f 2d 74 61 62 6c 65 73 0a 20 20 20 20 2a 2a  do-tables.    **
17bd4 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a 20   of a trigger.. 
17bd5 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
17bd6 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d  ( pExpr->iTable=
17bd7 3d 70 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20  =pStack->newIdx 
17bd8 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  || pExpr->iTable
17bd9 3d 3d 70 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78  ==pStack->oldIdx
17bda 20 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70   );.    pTab = p
17bdb 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 7d  Stack->pTab;.  }
17bdc 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
17bdd 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
17bde 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30  Expr->iColumn>=0
17bdf 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
17be0 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70  pExpr->iColumn<p
17be1 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20  Tab->nCol );.   
17be2 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43   zCol = pTab->aC
17be3 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  ol[pExpr->iColum
17be4 6e 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73  n].zName;.  }els
17be5 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b 65  e if( pTab->iPKe
17be6 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  y>=0 ){.    asse
17be7 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c  rt( pTab->iPKey<
17be8 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
17be9 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
17bea 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d  Col[pTab->iPKey]
17beb 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b  .zName;.  }else{
17bec 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f 57  .    zCol = "ROW
17bed 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ID";.  }.  asser
17bee 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
17bef 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44  <db->nDb );.  zD
17bf0 42 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69  Base = db->aDb[i
17bf1 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 20  Db].zName;.  rc 
17bf2 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e  = db->xAuth(db->
17bf3 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54 45  pAuthArg, SQLITE
17bf4 5f 52 45 41 44 2c 20 70 54 61 62 2d 3e 7a 4e 61  _READ, pTab->zNa
17bf5 6d 65 2c 20 7a 43 6f 6c 2c 20 7a 44 42 61 73 65  me, zCol, zDBase
17bf6 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
17bf7 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74      pParse->zAut
17bf8 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28  hContext);.  if(
17bf9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f   rc==SQLITE_IGNO
17bfa 52 45 20 29 7b 0a 20 20 20 20 70 45 78 70 72 2d  RE ){.    pExpr-
17bfb 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20  >op = TK_NULL;. 
17bfc 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
17bfd 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20  QLITE_DENY ){.  
17bfe 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32 20    if( db->nDb>2 
17bff 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20 20 20  || iDb!=0 ){.   
17c00 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
17c01 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63 65  sg(pParse, "acce
17c02 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73 20 69  ss to %s.%s.%s i
17c03 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20 0a  s prohibited", .
17c04 20 20 20 20 20 20 20 20 20 7a 44 42 61 73 65 2c           zDBase,
17c05 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
17c06 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ol);.    }else{.
17c07 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
17c08 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61  orMsg(pParse, "a
17c09 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20 69  ccess to %s.%s i
17c0a 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 70 54  s prohibited",pT
17c0b 61 62 2d 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29 3b  ab->zName,zCol);
17c0c 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
17c0d 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41  e->rc = SQLITE_A
17c0e 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  UTH;.  }else if(
17c0f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
17c10 7b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68  {.    sqliteAuth
17c11 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50  BadReturnCode(pP
17c12 61 72 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 7d  arse, rc);.  }.}
17c13 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75  ../*.** Do an au
17c14 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63  thorization chec
17c15 6b 20 75 73 69 6e 67 20 74 68 65 20 63 6f 64 65  k using the code
17c16 20 61 6e 64 20 61 72 67 75 6d 65 6e 74 73 20 67   and arguments g
17c17 69 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  iven.  Return.**
17c18 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f   either SQLITE_O
17c19 4b 20 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49  K (zero) or SQLI
17c1a 54 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c  TE_IGNORE or SQL
17c1b 49 54 45 5f 44 45 4e 59 2e 20 20 49 66 20 53 51  ITE_DENY.  If SQ
17c1c 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20  LITE_DENY.** is 
17c1d 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 74  returned, then t
17c1e 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 61  he error count a
17c1f 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  nd error message
17c20 20 69 6e 20 70 50 61 72 73 65 20 61 72 65 0a 2a   in pParse are.*
17c21 2a 20 6d 6f 64 69 66 69 65 64 20 61 70 70 72 6f  * modified appro
17c22 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c  priately..*/.SQL
17c23 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
17c24 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
17c25 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
17c26 65 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20  e,.  int code,. 
17c27 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
17c28 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  g1,.  const char
17c29 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74   *zArg2,.  const
17c2a 20 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a   char *zArg3.){.
17c2b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
17c2c 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
17c2d 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27  t rc;..  /* Don'
17c2e 74 20 64 6f 20 61 6e 79 20 61 75 74 68 6f 72 69  t do any authori
17c2f 7a 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66  zation checks if
17c30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
17c31 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20 20   initialising.  
17c32 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 72  ** or if the par
17c33 73 65 72 20 69 73 20 62 65 69 6e 67 20 69 6e 76  ser is being inv
17c34 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  oked from within
17c35 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
17c36 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66  _vtab..  */.  if
17c37 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ( db->init.busy 
17c38 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  || IN_DECLARE_VT
17c39 41 42 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  AB ){.    return
17c3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
17c3b 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68  .  if( db->xAuth
17c3c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
17c3d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
17c3e 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74  .  rc = db->xAut
17c3f 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20  h(db->pAuthArg, 
17c40 63 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72  code, zArg1, zAr
17c41 67 32 2c 20 7a 41 72 67 33 2c 20 70 50 61 72 73  g2, zArg3, pPars
17c42 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29  e->zAuthContext)
17c43 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
17c44 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 73  TE_DENY ){.    s
17c45 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
17c46 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68  Parse, "not auth
17c47 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 70 50  orized");.    pP
17c48 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
17c49 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20  E_AUTH;.  }else 
17c4a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
17c4b 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  K && rc!=SQLITE_
17c4c 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 72 63  IGNORE ){.    rc
17c4d 20 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a   = SQLITE_DENY;.
17c4e 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61      sqliteAuthBa
17c4f 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72  dReturnCode(pPar
17c50 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72  se, rc);.  }.  r
17c51 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
17c52 2a 2a 20 50 75 73 68 20 61 6e 20 61 75 74 68 6f  ** Push an autho
17c53 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  rization context
17c54 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  .  After this ro
17c55 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
17c56 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72   the.** zArg3 ar
17c57 67 75 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f 72  gument to author
17c58 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
17c59 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65  s will be zConte
17c5a 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70  xt until.** popp
17c5b 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73  ed.  Or if pPars
17c5c 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69  e==0, this routi
17c5d 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
17c5e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17c5f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
17c60 68 43 6f 6e 74 65 78 74 50 75 73 68 28 0a 20 20  hContextPush(.  
17c61 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20  Parse *pParse,. 
17c62 20 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43   AuthContext *pC
17c63 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74  ontext, .  const
17c64 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a   char *zContext.
17c65 29 7b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70  ){.  pContext->p
17c66 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
17c67 20 20 69 66 28 20 70 50 61 72 73 65 20 29 7b 0a    if( pParse ){.
17c68 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41      pContext->zA
17c69 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61  uthContext = pPa
17c6a 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
17c6b 74 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a  t;.    pParse->z
17c6c 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43  AuthContext = zC
17c6d 6f 6e 74 65 78 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ontext;.  }.}../
17c6e 2a 0a 2a 2a 20 50 6f 70 20 61 6e 20 61 75 74 68  *.** Pop an auth
17c6f 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78  orization contex
17c70 74 20 74 68 61 74 20 77 61 73 20 70 72 65 76 69  t that was previ
17c71 6f 75 73 6c 79 20 70 75 73 68 65 64 0a 2a 2a 20  ously pushed.** 
17c72 62 79 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  by sqlite3AuthCo
17c73 6e 74 65 78 74 50 75 73 68 0a 2a 2f 0a 53 51 4c  ntextPush.*/.SQL
17c74 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
17c75 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
17c76 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65  extPop(AuthConte
17c77 78 74 20 2a 70 43 6f 6e 74 65 78 74 29 7b 0a 20  xt *pContext){. 
17c78 20 69 66 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70   if( pContext->p
17c79 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f  Parse ){.    pCo
17c7a 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a  ntext->pParse->z
17c7b 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 43  AuthContext = pC
17c7c 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e  ontext->zAuthCon
17c7d 74 65 78 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65  text;.    pConte
17c7e 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 30 3b 0a  xt->pParse = 0;.
17c7f 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
17c80 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
17c81 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 2f  HORIZATION */../
17c82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
17c83 6e 64 20 6f 66 20 61 75 74 68 2e 63 20 2a 2a 2a  nd of auth.c ***
17c84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
17c87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
17c88 65 67 69 6e 20 66 69 6c 65 20 62 75 69 6c 64 2e  egin file build.
17c89 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
17c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
17c8c 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
17c8d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
17c8e 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
17c8f 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
17c90 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
17c91 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
17c92 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
17c93 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
17c94 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
17c95 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
17c96 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
17c97 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
17c98 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
17c99 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
17c9a 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
17c9b 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
17c9c 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
17c9d 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
17c9e 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
17c9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ca1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ca2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ca3 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
17ca4 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
17ca5 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
17ca6 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74   are called by t
17ca7 68 65 20 53 51 4c 69 74 65 20 70 61 72 73 65 72  he SQLite parser
17ca8 0a 2a 2a 20 77 68 65 6e 20 73 79 6e 74 61 78 20  .** when syntax 
17ca9 72 75 6c 65 73 20 61 72 65 20 72 65 64 75 63 65  rules are reduce
17caa 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73  d.  The routines
17cab 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 68 61   in this file ha
17cac 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c  ndle the.** foll
17cad 6f 77 69 6e 67 20 6b 69 6e 64 73 20 6f 66 20 53  owing kinds of S
17cae 51 4c 20 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a  QL syntax:.**.**
17caf 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
17cb0 45 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 54 41  E.**     DROP TA
17cb1 42 4c 45 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  BLE.**     CREAT
17cb2 45 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 44  E INDEX.**     D
17cb3 52 4f 50 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20  ROP INDEX.**    
17cb4 20 63 72 65 61 74 69 6e 67 20 49 44 20 6c 69 73   creating ID lis
17cb5 74 73 0a 2a 2a 20 20 20 20 20 42 45 47 49 4e 20  ts.**     BEGIN 
17cb6 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20  TRANSACTION.**  
17cb7 20 20 20 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 20     COMMIT.**    
17cb8 20 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a 20   ROLLBACK.**.** 
17cb9 24 49 64 3a 20 62 75 69 6c 64 2e 63 2c 76 20 31  $Id: build.c,v 1
17cba 2e 34 34 34 20 32 30 30 37 2f 30 39 2f 30 33 20  .444 2007/09/03 
17cbb 31 35 3a 31 39 3a 33 35 20 64 72 68 20 45 78 70  15:19:35 drh Exp
17cbc 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   $.*/../*.** Thi
17cbd 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
17cbe 6c 65 64 20 77 68 65 6e 20 61 20 6e 65 77 20 53  led when a new S
17cbf 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  QL statement is 
17cc0 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20  beginning to.** 
17cc1 62 65 20 70 61 72 73 65 64 2e 20 20 49 6e 69 74  be parsed.  Init
17cc2 69 61 6c 69 7a 65 20 74 68 65 20 70 50 61 72 73  ialize the pPars
17cc3 65 20 73 74 72 75 63 74 75 72 65 20 61 73 20 6e  e structure as n
17cc4 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eeded..*/.SQLITE
17cc5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
17cc6 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28  lite3BeginParse(
17cc7 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
17cc8 6e 74 20 65 78 70 6c 61 69 6e 46 6c 61 67 29 7b  nt explainFlag){
17cc9 0a 20 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  .  pParse->expla
17cca 69 6e 20 3d 20 65 78 70 6c 61 69 6e 46 6c 61 67  in = explainFlag
17ccb 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  ;.  pParse->nVar
17ccc 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66   = 0;.}..#ifndef
17ccd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
17cce 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20  RED_CACHE./*.** 
17ccf 54 68 65 20 54 61 62 6c 65 4c 6f 63 6b 20 73 74  The TableLock st
17cd0 72 75 63 74 75 72 65 20 69 73 20 6f 6e 6c 79 20  ructure is only 
17cd1 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69  used by the sqli
17cd2 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61  te3TableLock() a
17cd3 6e 64 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c  nd.** codeTableL
17cd4 6f 63 6b 73 28 29 20 66 75 6e 63 74 69 6f 6e 73  ocks() functions
17cd5 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c  ..*/.struct Tabl
17cd6 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 69 44  eLock {.  int iD
17cd7 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
17cd8 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
17cd9 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
17cda 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  ble to be locked
17cdb 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 3b 20   */.  int iTab; 
17cdc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
17cdd 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  e root page of t
17cde 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c  he table to be l
17cdf 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 73  ocked */.  u8 is
17ce0 57 72 69 74 65 4c 6f 63 6b 3b 20 20 20 20 20 20  WriteLock;      
17ce1 2f 2a 20 54 72 75 65 20 66 6f 72 20 77 72 69 74  /* True for writ
17ce2 65 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 66  e lock.  False f
17ce3 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a  or a read lock *
17ce4 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
17ce5 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65  zName;   /* Name
17ce6 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
17ce7 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72  .};../*.** Recor
17ce8 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20  d the fact that 
17ce9 77 65 20 77 61 6e 74 20 74 6f 20 6c 6f 63 6b 20  we want to lock 
17cea 61 20 74 61 62 6c 65 20 61 74 20 72 75 6e 2d 74  a table at run-t
17ceb 69 6d 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ime.  .**.** The
17cec 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63   table to be loc
17ced 6b 65 64 20 68 61 73 20 72 6f 6f 74 20 70 61 67  ked has root pag
17cee 65 20 69 54 61 62 20 61 6e 64 20 69 73 20 66 6f  e iTab and is fo
17cef 75 6e 64 20 69 6e 20 64 61 74 61 62 61 73 65 20  und in database 
17cf0 69 44 62 2e 0a 2a 2a 20 41 20 72 65 61 64 20 6f  iDb..** A read o
17cf1 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 63  r a write lock c
17cf2 61 6e 20 62 65 20 74 61 6b 65 6e 20 64 65 70 65  an be taken depe
17cf3 6e 64 69 6e 67 20 6f 6e 20 69 73 57 72 69 74 65  nding on isWrite
17cf4 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lock..**.** This
17cf5 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 72 65   routine just re
17cf6 63 6f 72 64 73 20 74 68 65 20 66 61 63 74 20 74  cords the fact t
17cf7 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  hat the lock is 
17cf8 64 65 73 69 72 65 64 2e 20 20 54 68 65 0a 2a 2a  desired.  The.**
17cf9 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 74 68   code to make th
17cfa 65 20 6c 6f 63 6b 20 6f 63 63 75 72 20 69 73 20  e lock occur is 
17cfb 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 6c  generated by a l
17cfc 61 74 65 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  ater call to.** 
17cfd 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29  codeTableLocks()
17cfe 20 77 68 69 63 68 20 6f 63 63 75 72 73 20 64 75   which occurs du
17cff 72 69 6e 67 20 73 71 6c 69 74 65 33 46 69 6e 69  ring sqlite3Fini
17d00 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a 53  shCoding()..*/.S
17d01 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
17d02 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  id sqlite3TableL
17d03 6f 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ock(.  Parse *pP
17d04 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
17d05 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
17d06 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20    int iDb,      
17d07 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
17d08 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
17d09 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62  ntaining the tab
17d0a 6c 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20  le to lock */.  
17d0b 69 6e 74 20 69 54 61 62 2c 20 20 20 20 20 20 20  int iTab,       
17d0c 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20     /* Root page 
17d0d 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61  number of the ta
17d0e 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  ble to be locked
17d0f 20 2a 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65   */.  u8 isWrite
17d10 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 54 72 75 65  Lock,    /* True
17d11 20 66 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63   for a write loc
17d12 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  k */.  const cha
17d13 72 20 2a 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d  r *zName  /* Nam
17d14 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
17d15 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 29  o be locked */.)
17d16 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
17d17 20 6e 42 79 74 65 73 3b 0a 20 20 54 61 62 6c 65   nBytes;.  Table
17d18 4c 6f 63 6b 20 2a 70 3b 0a 0a 20 20 69 66 28 20  Lock *p;..  if( 
17d19 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 72 65 74  iDb<0 ){.    ret
17d1a 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28  urn;.  }..  for(
17d1b 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e  i=0; i<pParse->n
17d1c 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b  TableLock; i++){
17d1d 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 73 65  .    p = &pParse
17d1e 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b  ->aTableLock[i];
17d1f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 44 62 3d  .    if( p->iDb=
17d20 3d 69 44 62 20 26 26 20 70 2d 3e 69 54 61 62 3d  =iDb && p->iTab=
17d21 3d 69 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70  =iTab ){.      p
17d22 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20  ->isWriteLock = 
17d23 28 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20  (p->isWriteLock 
17d24 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b  || isWriteLock);
17d25 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
17d26 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74     }.  }..  nByt
17d27 65 73 20 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c  es = sizeof(Tabl
17d28 65 4c 6f 63 6b 29 20 2a 20 28 70 50 61 72 73 65  eLock) * (pParse
17d29 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31 29 3b  ->nTableLock+1);
17d2a 0a 20 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c  .  pParse->aTabl
17d2b 65 4c 6f 63 6b 20 3d 20 0a 20 20 20 20 20 20 73  eLock = .      s
17d2c 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
17d2d 72 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62  rFree(pParse->db
17d2e 2c 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65  , pParse->aTable
17d2f 4c 6f 63 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20  Lock, nBytes);. 
17d30 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 54 61   if( pParse->aTa
17d31 62 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70  bleLock ){.    p
17d32 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62   = &pParse->aTab
17d33 6c 65 4c 6f 63 6b 5b 70 50 61 72 73 65 2d 3e 6e  leLock[pParse->n
17d34 54 61 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a 20 20  TableLock++];.  
17d35 20 20 70 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a    p->iDb = iDb;.
17d36 20 20 20 20 70 2d 3e 69 54 61 62 20 3d 20 69 54      p->iTab = iT
17d37 61 62 3b 0a 20 20 20 20 70 2d 3e 69 73 57 72 69  ab;.    p->isWri
17d38 74 65 4c 6f 63 6b 20 3d 20 69 73 57 72 69 74 65  teLock = isWrite
17d39 4c 6f 63 6b 3b 0a 20 20 20 20 70 2d 3e 7a 4e 61  Lock;.    p->zNa
17d3a 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7d 65  me = zName;.  }e
17d3b 6c 73 65 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  lse{.    pParse-
17d3c 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b  >nTableLock = 0;
17d3d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d  .    pParse->db-
17d3e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
17d3f 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
17d40 43 6f 64 65 20 61 6e 20 4f 50 5f 54 61 62 6c 65  Code an OP_Table
17d41 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e  Lock instruction
17d42 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20   for each table 
17d43 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 0a 2a 2a  locked by the.**
17d44 20 73 74 61 74 65 6d 65 6e 74 20 28 63 6f 6e 66   statement (conf
17d45 69 67 75 72 65 64 20 62 79 20 63 61 6c 6c 73 20  igured by calls 
17d46 74 6f 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  to sqlite3TableL
17d47 6f 63 6b 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69  ock())..*/.stati
17d48 63 20 76 6f 69 64 20 63 6f 64 65 54 61 62 6c 65  c void codeTable
17d49 4c 6f 63 6b 73 28 50 61 72 73 65 20 2a 70 50 61  Locks(Parse *pPa
17d4a 72 73 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  rse){.  int i;. 
17d4b 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 0a 0a   Vdbe *pVdbe; ..
17d4c 20 20 69 66 28 20 30 3d 3d 28 70 56 64 62 65 20    if( 0==(pVdbe 
17d4d 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
17d4e 28 70 50 61 72 73 65 29 29 20 29 7b 0a 20 20 20  (pParse)) ){.   
17d4f 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
17d50 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73  for(i=0; i<pPars
17d51 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69  e->nTableLock; i
17d52 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f  ++){.    TableLo
17d53 63 6b 20 2a 70 20 3d 20 26 70 50 61 72 73 65 2d  ck *p = &pParse-
17d54 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a  >aTableLock[i];.
17d55 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e      int p1 = p->
17d56 69 44 62 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  iDb;.    if( p->
17d57 69 73 57 72 69 74 65 4c 6f 63 6b 20 29 7b 0a 20  isWriteLock ){. 
17d58 20 20 20 20 20 70 31 20 3d 20 2d 31 2a 28 70 31       p1 = -1*(p1
17d59 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  +1);.    }.    s
17d5a 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 70 56  qlite3VdbeOp3(pV
17d5b 64 62 65 2c 20 4f 50 5f 54 61 62 6c 65 4c 6f 63  dbe, OP_TableLoc
17d5c 6b 2c 20 70 31 2c 20 70 2d 3e 69 54 61 62 2c 20  k, p1, p->iTab, 
17d5d 70 2d 3e 7a 4e 61 6d 65 2c 20 50 33 5f 53 54 41  p->zName, P3_STA
17d5e 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73  TIC);.  }.}.#els
17d5f 65 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64 65  e.  #define code
17d60 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23 65  TableLocks(x).#e
17d61 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
17d62 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
17d63 65 64 20 61 66 74 65 72 20 61 20 73 69 6e 67 6c  ed after a singl
17d64 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
17d65 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72 73  has been.** pars
17d66 65 64 20 61 6e 64 20 61 20 56 44 42 45 20 70 72  ed and a VDBE pr
17d67 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65  ogram to execute
17d68 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20   that statement 
17d69 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 70  has been.** prep
17d6a 61 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74  ared.  This rout
17d6b 69 6e 65 20 70 75 74 73 20 74 68 65 20 66 69 6e  ine puts the fin
17d6c 69 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20 6f  ishing touches o
17d6d 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70 72  n the.** VDBE pr
17d6e 6f 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74 73  ogram and resets
17d6f 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75   the pParse stru
17d70 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e 65  cture for the ne
17d71 78 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a  xt.** parse..**.
17d72 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  ** Note that if 
17d73 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65  an error occurre
17d74 64 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74  d, it might be t
17d75 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a 20  he case that.** 
17d76 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61 73  no VDBE code was
17d77 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 53   generated..*/.S
17d78 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
17d79 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  id sqlite3Finish
17d7a 43 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70 50  Coding(Parse *pP
17d7b 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
17d7c 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b   *db;.  Vdbe *v;
17d7d 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
17d7e 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  >db;.  if( db->m
17d7f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
17d80 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72  turn;.  if( pPar
17d81 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 74  se->nested ) ret
17d82 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 50 61 72  urn;.  if( !pPar
17d83 73 65 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20  se->pVdbe ){.   
17d84 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d   if( pParse->rc=
17d85 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50  =SQLITE_OK && pP
17d86 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20  arse->nErr ){.  
17d87 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
17d88 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
17d89 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
17d8a 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67   }.  }..  /* Beg
17d8b 69 6e 20 62 79 20 67 65 6e 65 72 61 74 69 6e 67  in by generating
17d8c 20 73 6f 6d 65 20 74 65 72 6d 69 6e 61 74 69 6f   some terminatio
17d8d 6e 20 63 6f 64 65 20 61 74 20 74 68 65 20 65 6e  n code at the en
17d8e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 76 64  d of the.  ** vd
17d8f 62 65 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2f 0a  be program.  */.
17d90 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
17d91 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
17d92 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
17d93 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
17d94 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b   OP_Halt, 0, 0);
17d95 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6f  ..    /* The coo
17d96 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e  kie mask contain
17d97 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20 65 61  s one bit for ea
17d98 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ch database file
17d99 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 28 42   open..    ** (B
17d9a 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61 69 6e  it 0 is for main
17d9b 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72 20 74  , bit 1 is for t
17d9c 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74  emp, and so fort
17d9d 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a 20 20  h.)  Bits are.  
17d9e 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65 61 63    ** set for eac
17d9f 68 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  h database that 
17da0 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65 72 61  is used.  Genera
17da1 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61 72 74  te code to start
17da2 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61   a.    ** transa
17da3 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20 75 73  ction on each us
17da4 65 64 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  ed database and 
17da5 74 6f 20 76 65 72 69 66 79 20 74 68 65 20 73 63  to verify the sc
17da6 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 20 20  hema cookie.    
17da7 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20  ** on each used 
17da8 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f  database..    */
17da9 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d  .    if( pParse-
17daa 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 29 7b  >cookieGoto>0 ){
17dab 0a 20 20 20 20 20 20 75 33 32 20 6d 61 73 6b 3b  .      u32 mask;
17dac 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a  .      int iDb;.
17dad 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
17dae 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 50 61  eJumpHere(v, pPa
17daf 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 2d  rse->cookieGoto-
17db0 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 44  1);.      for(iD
17db1 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 44 62  b=0, mask=1; iDb
17db2 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b 3c 3c  <db->nDb; mask<<
17db3 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20  =1, iDb++){.    
17db4 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20 26 20      if( (mask & 
17db5 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61  pParse->cookieMa
17db6 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  sk)==0 ) continu
17db7 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e;.        sqlit
17db8 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
17db9 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20  v, iDb);.       
17dba 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
17dbb 70 28 76 2c 20 4f 50 5f 54 72 61 6e 73 61 63 74  p(v, OP_Transact
17dbc 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73 6b 20  ion, iDb, (mask 
17dbd 26 20 70 50 61 72 73 65 2d 3e 77 72 69 74 65 4d  & pParse->writeM
17dbe 61 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20 20 20  ask)!=0);.      
17dbf 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
17dc0 4f 70 28 76 2c 20 4f 50 5f 56 65 72 69 66 79 43  Op(v, OP_VerifyC
17dc1 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 70 50 61 72  ookie, iDb, pPar
17dc2 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b  se->cookieValue[
17dc3 69 44 62 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23  iDb]);.      }.#
17dc4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
17dc5 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
17dc6 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65        if( pParse
17dc7 2d 3e 70 56 69 72 74 75 61 6c 4c 6f 63 6b 20 29  ->pVirtualLock )
17dc8 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
17dc9 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29 70  vtab = (char *)p
17dca 50 61 72 73 65 2d 3e 70 56 69 72 74 75 61 6c 4c  Parse->pVirtualL
17dcb 6f 63 6b 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20  ock->pVtab;.    
17dcc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f      sqlite3VdbeO
17dcd 70 33 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 2c  p3(v, OP_VBegin,
17dce 20 30 2c 20 30 2c 20 76 74 61 62 2c 20 50 33 5f   0, 0, vtab, P3_
17dcf 56 54 41 42 29 3b 0a 20 20 20 20 20 20 7d 0a 23  VTAB);.      }.#
17dd0 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20  endif..      /* 
17dd1 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 63 6f 6f  Once all the coo
17dd2 6b 69 65 73 20 68 61 76 65 20 62 65 65 6e 20 76  kies have been v
17dd3 65 72 69 66 69 65 64 20 61 6e 64 20 74 72 61 6e  erified and tran
17dd4 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 65 64 2c  sactions opened,
17dd5 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62 74 61 69   .      ** obtai
17dd6 6e 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74  n the required t
17dd7 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54 68 69 73  able-locks. This
17dd8 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65   is a no-op unle
17dd9 73 73 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  ss the .      **
17dda 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65   shared-cache fe
17ddb 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64  ature is enabled
17ddc 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
17ddd 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28   codeTableLocks(
17dde 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
17ddf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
17de0 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70  v, OP_Goto, 0, p
17de1 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
17de2 6f 29 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64  o);.    }..#ifnd
17de3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
17de4 52 41 43 45 0a 20 20 20 20 2f 2a 20 41 64 64 20  RACE.    /* Add 
17de5 61 20 4e 6f 2d 6f 70 20 74 68 61 74 20 63 6f 6e  a No-op that con
17de6 74 61 69 6e 73 20 74 68 65 20 63 6f 6d 70 6c 65  tains the comple
17de7 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63  te text of the c
17de8 6f 6d 70 69 6c 65 64 20 53 51 4c 0a 20 20 20 20  ompiled SQL.    
17de9 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20  ** statement as 
17dea 69 74 73 20 50 33 20 61 72 67 75 6d 65 6e 74 2e  its P3 argument.
17deb 20 20 54 68 69 73 20 64 6f 65 73 20 6e 6f 74 20    This does not 
17dec 63 68 61 6e 67 65 20 74 68 65 20 66 75 6e 63 74  change the funct
17ded 69 6f 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20  ionality.    ** 
17dee 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20  of the program. 
17def 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
17df0 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69  his is used to i
17df1 6d 70 6c 65 6d 65 6e 74 20 73 71 6c 69 74 65 33  mplement sqlite3
17df2 5f 74 72 61 63 65 28 29 2e 0a 20 20 20 20 2a 2f  _trace()..    */
17df3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
17df4 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 6f 70 2c 20  Op3(v, OP_Noop, 
17df5 30 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 7a 53  0, 0, pParse->zS
17df6 71 6c 2c 20 70 50 61 72 73 65 2d 3e 7a 54 61 69  ql, pParse->zTai
17df7 6c 2d 70 50 61 72 73 65 2d 3e 7a 53 71 6c 29 3b  l-pParse->zSql);
17df8 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
17df9 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 2a 2f 0a  E_OMIT_TRACE */.
17dfa 20 20 7d 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74    }...  /* Get t
17dfb 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20  he VDBE program 
17dfc 72 65 61 64 79 20 66 6f 72 20 65 78 65 63 75 74  ready for execut
17dfd 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76  ion.  */.  if( v
17dfe 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   && pParse->nErr
17dff 3d 3d 30 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c  ==0 && !db->mall
17e00 6f 63 46 61 69 6c 65 64 20 29 7b 0a 23 69 66 64  ocFailed ){.#ifd
17e01 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
17e02 20 20 20 20 46 49 4c 45 20 2a 74 72 61 63 65 20      FILE *trace 
17e03 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  = (db->flags & S
17e04 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 29  QLITE_VdbeTrace)
17e05 21 3d 30 20 3f 20 73 74 64 6f 75 74 20 3a 20 30  !=0 ? stdout : 0
17e06 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
17e07 65 54 72 61 63 65 28 76 2c 20 74 72 61 63 65 29  eTrace(v, trace)
17e08 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c  ;.#endif.    sql
17e09 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64  ite3VdbeMakeRead
17e0a 79 28 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56 61  y(v, pParse->nVa
17e0b 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b  r, pParse->nMem+
17e0c 33 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  3,.             
17e0d 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72              pPar
17e0e 73 65 2d 3e 6e 54 61 62 2b 33 2c 20 70 50 61 72  se->nTab+3, pPar
17e0f 73 65 2d 3e 65 78 70 6c 61 69 6e 29 3b 0a 20 20  se->explain);.  
17e10 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
17e11 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
17e12 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73  pParse->colNames
17e13 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Set = 0;.  }else
17e14 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d   if( pParse->rc=
17e15 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17e16 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
17e17 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
17e18 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20  .  pParse->nTab 
17e19 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  = 0;.  pParse->n
17e1a 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 50 61 72 73  Mem = 0;.  pPars
17e1b 65 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 70  e->nSet = 0;.  p
17e1c 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b  Parse->nVar = 0;
17e1d 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  .  pParse->cooki
17e1e 65 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 70 50 61  eMask = 0;.  pPa
17e1f 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20  rse->cookieGoto 
17e20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75  = 0;.}../*.** Ru
17e21 6e 20 74 68 65 20 70 61 72 73 65 72 20 61 6e 64  n the parser and
17e22 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
17e23 72 65 63 75 72 73 69 76 65 6c 79 20 69 6e 20 6f  recursively in o
17e24 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65  rder to generate
17e25 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 74 68 65  .** code for the
17e26 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 67   SQL statement g
17e27 69 76 65 6e 20 6f 6e 74 6f 20 74 68 65 20 65 6e  iven onto the en
17e28 64 20 6f 66 20 74 68 65 20 70 50 61 72 73 65 20  d of the pParse 
17e29 63 6f 6e 74 65 78 74 0a 2a 2a 20 63 75 72 72 65  context.** curre
17e2a 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
17e2b 72 75 63 74 69 6f 6e 2e 20 20 57 68 65 6e 20 74  ruction.  When t
17e2c 68 65 20 70 61 72 73 65 72 20 69 73 20 72 75 6e  he parser is run
17e2d 20 72 65 63 75 72 73 69 76 65 6c 79 0a 2a 2a 20   recursively.** 
17e2e 74 68 69 73 20 77 61 79 2c 20 74 68 65 20 66 69  this way, the fi
17e2f 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 69 73 20 6e  nal OP_Halt is n
17e30 6f 74 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20  ot appended and 
17e31 6f 74 68 65 72 20 69 6e 69 74 69 61 6c 69 7a 61  other initializa
17e32 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 66 69 6e 61  tion.** and fina
17e33 6c 69 7a 61 74 69 6f 6e 20 73 74 65 70 73 20 61  lization steps a
17e34 72 65 20 6f 6d 69 74 74 65 64 20 62 65 63 61 75  re omitted becau
17e35 73 65 20 74 68 6f 73 65 20 61 72 65 20 68 61 6e  se those are han
17e36 64 6c 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20  dling by the.** 
17e37 6f 75 74 65 72 6d 6f 73 74 20 70 61 72 73 65 72  outermost parser
17e38 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 65 76 65 72  ..**.** Not ever
17e39 79 74 68 69 6e 67 20 69 73 20 6e 65 73 74 61 62  ything is nestab
17e3a 6c 65 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69  le.  This facili
17e3b 74 79 20 69 73 20 64 65 73 69 67 6e 65 64 20 74  ty is designed t
17e3c 6f 20 70 65 72 6d 69 74 0a 2a 2a 20 49 4e 53 45  o permit.** INSE
17e3d 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 20  RT, UPDATE, and 
17e3e 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e  DELETE operation
17e3f 73 20 61 67 61 69 6e 73 74 20 53 51 4c 49 54 45  s against SQLITE
17e40 5f 4d 41 53 54 45 52 2e 20 20 55 73 65 0a 2a 2a  _MASTER.  Use.**
17e41 20 63 61 72 65 20 69 66 20 79 6f 75 20 64 65 63   care if you dec
17e42 69 64 65 20 74 6f 20 74 72 79 20 74 6f 20 75 73  ide to try to us
17e43 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  e this routine f
17e44 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 75  or some other pu
17e45 72 70 6f 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  rposes..*/.SQLIT
17e46 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
17e47 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
17e48 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
17e49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
17e4a 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
17e4b 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
17e4c 20 2a 7a 53 71 6c 3b 0a 23 20 64 65 66 69 6e 65   *zSql;.# define
17e4d 20 53 41 56 45 5f 53 5a 20 20 28 73 69 7a 65 6f   SAVE_SZ  (sizeo
17e4e 66 28 50 61 72 73 65 29 20 2d 20 6f 66 66 73 65  f(Parse) - offse
17e4f 74 6f 66 28 50 61 72 73 65 2c 6e 56 61 72 29 29  tof(Parse,nVar))
17e50 0a 20 20 63 68 61 72 20 73 61 76 65 42 75 66 5b  .  char saveBuf[
17e51 53 41 56 45 5f 53 5a 5d 3b 0a 0a 20 20 69 66 28  SAVE_SZ];..  if(
17e52 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
17e53 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
17e54 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
17e55 3c 31 30 20 29 3b 20 20 2f 2a 20 4e 65 73 74 69  <10 );  /* Nesti
17e56 6e 67 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62  ng should only b
17e57 65 20 6f 66 20 6c 69 6d 69 74 65 64 20 64 65 70  e of limited dep
17e58 74 68 20 2a 2f 0a 20 20 76 61 5f 73 74 61 72 74  th */.  va_start
17e59 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
17e5a 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 56   zSql = sqlite3V
17e5b 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e  MPrintf(pParse->
17e5c 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
17e5d 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
17e5e 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b    if( zSql==0 ){
17e5f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d  .    pParse->db-
17e60 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
17e61 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20  1;.    return;  
17e62 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73   /* A malloc mus
17e63 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f  t have failed */
17e64 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e  .  }.  pParse->n
17e65 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70  ested++;.  memcp
17e66 79 28 73 61 76 65 42 75 66 2c 20 26 70 50 61 72  y(saveBuf, &pPar
17e67 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53  se->nVar, SAVE_S
17e68 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 50  Z);.  memset(&pP
17e69 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53  arse->nVar, 0, S
17e6a 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74  AVE_SZ);.  sqlit
17e6b 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72  e3RunParser(pPar
17e6c 73 65 2c 20 7a 53 71 6c 2c 20 30 29 3b 0a 20 20  se, zSql, 0);.  
17e6d 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
17e6e 6c 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70 50  l);.  memcpy(&pP
17e6f 61 72 73 65 2d 3e 6e 56 61 72 2c 20 73 61 76 65  arse->nVar, save
17e70 42 75 66 2c 20 53 41 56 45 5f 53 5a 29 3b 0a 20  Buf, SAVE_SZ);. 
17e71 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 2d   pParse->nested-
17e72 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61  -;.}../*.** Loca
17e73 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  te the in-memory
17e74 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
17e75 64 65 73 63 72 69 62 65 73 20 61 20 70 61 72 74  describes a part
17e76 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a  icular database.
17e77 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e 20 74  ** table given t
17e78 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20  he name of that 
17e79 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f  table and (optio
17e7a 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65 20  nally) the name 
17e7b 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  of the.** databa
17e7c 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  se containing th
17e7d 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e  e table.  Return
17e7e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75   NULL if not fou
17e7f 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61  nd..**.** If zDa
17e80 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c 6c  tabase is 0, all
17e81 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 73   databases are s
17e82 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 65 20  earched for the 
17e83 74 61 62 6c 65 20 61 6e 64 20 74 68 65 0a 2a 2a  table and the.**
17e84 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20   first matching 
17e85 74 61 62 6c 65 20 69 73 20 72 65 74 75 72 6e 65  table is returne
17e86 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e 67  d.  (No checking
17e87 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20 74   for duplicate t
17e88 61 62 6c 65 0a 2a 2a 20 6e 61 6d 65 73 20 69 73  able.** names is
17e89 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65 61   done.)  The sea
17e8a 72 63 68 20 6f 72 64 65 72 20 69 73 20 54 45 4d  rch order is TEM
17e8b 50 20 66 69 72 73 74 2c 20 74 68 65 6e 20 4d 41  P first, then MA
17e8c 49 4e 2c 20 74 68 65 6e 20 61 6e 79 0a 2a 2a 20  IN, then any.** 
17e8d 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
17e8e 73 65 73 20 61 64 64 65 64 20 75 73 69 6e 67 20  ses added using 
17e8f 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61  the ATTACH comma
17e90 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  nd..**.** See al
17e91 73 6f 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  so sqlite3Locate
17e92 54 61 62 6c 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49  Table()..*/.SQLI
17e93 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65  TE_PRIVATE Table
17e94 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62   *sqlite3FindTab
17e95 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  le(sqlite3 *db, 
17e96 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
17e97 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
17e98 44 61 74 61 62 61 73 65 29 7b 0a 20 20 54 61 62  Database){.  Tab
17e99 6c 65 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74  le *p = 0;.  int
17e9a 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 4e   i;.  assert( zN
17e9b 61 6d 65 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28  ame!=0 );.  for(
17e9c 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69  i=OMIT_TEMPDB; i
17e9d 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
17e9e 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32      int j = (i<2
17e9f 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f  ) ? i^1 : i;   /
17ea0 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65  * Search TEMP be
17ea1 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20  fore MAIN */.   
17ea2 20 69 66 28 20 7a 44 61 74 61 62 61 73 65 21 3d   if( zDatabase!=
17ea3 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  0 && sqlite3StrI
17ea4 43 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64  Cmp(zDatabase, d
17ea5 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29  b->aDb[j].zName)
17ea6 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
17ea7 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
17ea8 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d  Find(&db->aDb[j]
17ea9 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  .pSchema->tblHas
17eaa 68 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e  h, zName, strlen
17eab 28 7a 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 20 20  (zName)+1);.    
17eac 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a 20  if( p ) break;. 
17ead 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
17eae 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74  ../*.** Locate t
17eaf 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72  he in-memory str
17eb0 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
17eb1 72 69 62 65 73 20 61 20 70 61 72 74 69 63 75 6c  ribes a particul
17eb2 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74  ar database.** t
17eb3 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e  able given the n
17eb4 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c  ame of that tabl
17eb5 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c  e and (optionall
17eb6 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  y) the name of t
17eb7 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  he.** database c
17eb8 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
17eb9 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  ble.  Return NUL
17eba 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 20  L if not found. 
17ebb 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e 0a 2a   Also leave an.*
17ebc 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  * error message 
17ebd 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  in pParse->zErrM
17ebe 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69  sg..**.** The di
17ebf 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
17ec0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6e   this routine an
17ec1 64 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  d sqlite3FindTab
17ec2 6c 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69  le() is that thi
17ec3 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6c 65 61  s.** routine lea
17ec4 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ves an error mes
17ec5 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e  sage in pParse->
17ec6 7a 45 72 72 4d 73 67 20 77 68 65 72 65 0a 2a 2a  zErrMsg where.**
17ec7 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
17ec8 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f  e() does not..*/
17ec9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
17eca 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f  Table *sqlite3Lo
17ecb 63 61 74 65 54 61 62 6c 65 28 50 61 72 73 65 20  cateTable(Parse 
17ecc 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
17ecd 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73  har *zName, cons
17ece 74 20 63 68 61 72 20 2a 7a 44 62 61 73 65 29 7b  t char *zDbase){
17ecf 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a 20 20  .  Table *p;..  
17ed0 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61  /* Read the data
17ed1 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20  base schema. If 
17ed2 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
17ed3 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20   leave an error 
17ed4 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64  message.  ** and
17ed5 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20   code in pParse 
17ed6 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  and return NULL.
17ed7 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
17ed8 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
17ed9 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
17eda 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
17edb 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69 74    }..  p = sqlit
17edc 65 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61 72  e3FindTable(pPar
17edd 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a  se->db, zName, z
17ede 44 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d  Dbase);.  if( p=
17edf 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 44  =0 ){.    if( zD
17ee0 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 73 71  base ){.      sq
17ee1 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
17ee2 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74  arse, "no such t
17ee3 61 62 6c 65 3a 20 25 73 2e 25 73 22 2c 20 7a 44  able: %s.%s", zD
17ee4 62 61 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  base, zName);.  
17ee5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
17ee6 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
17ee7 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
17ee8 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 4e 61 6d  table: %s", zNam
17ee9 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  e);.    }.    pP
17eea 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d  arse->checkSchem
17eeb 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  a = 1;.  }.  ret
17eec 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
17eed 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65  Locate the in-me
17eee 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 20 74  mory structure t
17eef 68 61 74 20 64 65 73 63 72 69 62 65 73 20 0a 2a  hat describes .*
17ef0 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69  * a particular i
17ef1 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65 20 6e  ndex given the n
17ef2 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e 64 65  ame of that inde
17ef3 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61 6d  x.** and the nam
17ef4 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
17ef5 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
17ef6 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 52 65  the index..** Re
17ef7 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74  turn NULL if not
17ef8 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66   found..**.** If
17ef9 20 7a 44 61 74 61 62 61 73 65 20 69 73 20 30 2c   zDatabase is 0,
17efa 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 61   all databases a
17efb 72 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20  re searched for 
17efc 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64  the.** table and
17efd 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68   the first match
17efe 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72 65 74  ing index is ret
17eff 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65 63  urned.  (No chec
17f00 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 70 6c  king.** for dupl
17f01 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61 6d 65  icate index name
17f02 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 68 65  s is done.)  The
17f03 20 73 65 61 72 63 68 20 6f 72 64 65 72 20 69 73   search order is
17f04 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74 2c 20  .** TEMP first, 
17f05 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20  then MAIN, then 
17f06 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64 61  any auxiliary da
17f07 74 61 62 61 73 65 73 20 61 64 64 65 64 0a 2a 2a  tabases added.**
17f08 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43   using the ATTAC
17f09 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51  H command..*/.SQ
17f0a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64  LITE_PRIVATE Ind
17f0b 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49  ex *sqlite3FindI
17f0c 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62  ndex(sqlite3 *db
17f0d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
17f0e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ame, const char 
17f0f 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a  *zDb){.  Index *
17f10 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a  p = 0;.  int i;.
17f11 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d    for(i=OMIT_TEM
17f12 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  PDB; i<db->nDb; 
17f13 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20  i++){.    int j 
17f14 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20  = (i<2) ? i^1 : 
17f15 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45  i;  /* Search TE
17f16 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a  MP before MAIN *
17f17 2f 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53  /.    Schema *pS
17f18 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
17f19 6a 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20  j].pSchema;.    
17f1a 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69 74  if( zDb && sqlit
17f1b 65 33 53 74 72 49 43 6d 70 28 7a 44 62 2c 20 64  e3StrICmp(zDb, d
17f1c 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29  b->aDb[j].zName)
17f1d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
17f1e 20 61 73 73 65 72 74 28 20 70 53 63 68 65 6d 61   assert( pSchema
17f1f 20 7c 7c 20 28 6a 3d 3d 31 20 26 26 20 21 64 62   || (j==1 && !db
17f20 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 20 29 3b  ->aDb[1].pBt) );
17f21 0a 20 20 20 20 69 66 28 20 70 53 63 68 65 6d 61  .    if( pSchema
17f22 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71   ){.      p = sq
17f23 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70  lite3HashFind(&p
17f24 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c  Schema->idxHash,
17f25 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 7a   zName, strlen(z
17f26 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 20 20 7d 0a  Name)+1);.    }.
17f27 20 20 20 20 69 66 28 20 70 20 29 20 62 72 65 61      if( p ) brea
17f28 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  k;.  }.  return 
17f29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c  p;.}../*.** Recl
17f2a 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20 75  aim the memory u
17f2b 73 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78 0a  sed by an index.
17f2c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
17f2d 72 65 65 49 6e 64 65 78 28 49 6e 64 65 78 20 2a  reeIndex(Index *
17f2e 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  p){.  sqlite3_fr
17f2f 65 65 28 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a  ee(p->zColAff);.
17f30 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
17f31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  );.}../*.** Remo
17f32 76 65 20 74 68 65 20 67 69 76 65 6e 20 69 6e 64  ve the given ind
17f33 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65  ex from the inde
17f34 78 20 68 61 73 68 20 74 61 62 6c 65 2c 20 61 6e  x hash table, an
17f35 64 20 66 72 65 65 0a 2a 2a 20 69 74 73 20 6d 65  d free.** its me
17f36 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 2e  mory structures.
17f37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78  .**.** The index
17f38 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   is removed from
17f39 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
17f3a 73 68 20 74 61 62 6c 65 73 20 62 75 74 0a 2a 2a  sh tables but.**
17f3b 20 69 74 20 69 73 20 6e 6f 74 20 75 6e 6c 69 6e   it is not unlin
17f3c 6b 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62  ked from the Tab
17f3d 6c 65 20 74 68 61 74 20 69 74 20 69 6e 64 65 78  le that it index
17f3e 65 73 2e 0a 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67  es..** Unlinking
17f3f 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20   from the Table 
17f40 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 62 79 20  must be done by 
17f41 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
17f42 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
17f43 76 6f 69 64 20 73 71 6c 69 74 65 44 65 6c 65 74  void sqliteDelet
17f44 65 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29  eIndex(Index *p)
17f45 7b 0a 20 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b  {.  Index *pOld;
17f46 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
17f47 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b  Name = p->zName;
17f48 0a 0a 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74  ..  pOld = sqlit
17f49 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d  e3HashInsert(&p-
17f4a 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  >pSchema->idxHas
17f4b 68 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e  h, zName, strlen
17f4c 28 20 7a 4e 61 6d 65 29 2b 31 2c 20 30 29 3b 0a  ( zName)+1, 0);.
17f4d 20 20 61 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d    assert( pOld==
17f4e 30 20 7c 7c 20 70 4f 6c 64 3d 3d 70 20 29 3b 0a  0 || pOld==p );.
17f4f 20 20 66 72 65 65 49 6e 64 65 78 28 70 29 3b 0a    freeIndex(p);.
17f50 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  }../*.** For the
17f51 20 69 6e 64 65 78 20 63 61 6c 6c 65 64 20 7a 49   index called zI
17f52 64 78 4e 61 6d 65 20 77 68 69 63 68 20 69 73 20  dxName which is 
17f53 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74  found in the dat
17f54 61 62 61 73 65 20 69 44 62 2c 0a 2a 2a 20 75 6e  abase iDb,.** un
17f55 6c 69 6b 65 20 74 68 61 74 20 69 6e 64 65 78 20  like that index 
17f56 66 72 6f 6d 20 69 74 73 20 54 61 62 6c 65 20 74  from its Table t
17f57 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 69  hen remove the i
17f58 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  ndex from.** the
17f59 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c   index hash tabl
17f5a 65 20 61 6e 64 20 66 72 65 65 20 61 6c 6c 20 6d  e and free all m
17f5b 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73  emory structures
17f5c 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
17f5d 69 74 68 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a  ith the index..*
17f5e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17f5f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c   void sqlite3Unl
17f60 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65  inkAndDeleteInde
17f61 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  x(sqlite3 *db, i
17f62 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68  nt iDb, const ch
17f63 61 72 20 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a 20  ar *zIdxName){. 
17f64 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a   Index *pIndex;.
17f65 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 48 61 73    int len;.  Has
17f66 68 20 2a 70 48 61 73 68 20 3d 20 26 64 62 2d 3e  h *pHash = &db->
17f67 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
17f68 2d 3e 69 64 78 48 61 73 68 3b 0a 0a 20 20 6c 65  ->idxHash;..  le
17f69 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 49 64 78 4e  n = strlen(zIdxN
17f6a 61 6d 65 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d  ame);.  pIndex =
17f6b 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
17f6c 72 74 28 70 48 61 73 68 2c 20 7a 49 64 78 4e 61  rt(pHash, zIdxNa
17f6d 6d 65 2c 20 6c 65 6e 2b 31 2c 20 30 29 3b 0a 20  me, len+1, 0);. 
17f6e 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20   if( pIndex ){. 
17f6f 20 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 70     if( pIndex->p
17f70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70  Table->pIndex==p
17f71 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 70  Index ){.      p
17f72 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70  Index->pTable->p
17f73 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e  Index = pIndex->
17f74 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65  pNext;.    }else
17f75 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70  {.      Index *p
17f76 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70 49  ;.      for(p=pI
17f77 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49  ndex->pTable->pI
17f78 6e 64 65 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e  ndex; p && p->pN
17f79 65 78 74 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70  ext!=pIndex; p=p
17f7a 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20  ->pNext){}.     
17f7b 20 69 66 28 20 70 20 26 26 20 70 2d 3e 70 4e 65   if( p && p->pNe
17f7c 78 74 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20  xt==pIndex ){.  
17f7d 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
17f7e 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a   pIndex->pNext;.
17f7f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
17f80 20 20 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64    freeIndex(pInd
17f81 65 78 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66  ex);.  }.  db->f
17f82 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49  lags |= SQLITE_I
17f83 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a  nternChanges;.}.
17f84 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c  ./*.** Erase all
17f85 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74   schema informat
17f86 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d  ion from the in-
17f87 6d 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c  memory hash tabl
17f88 65 73 20 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c  es of.** a singl
17f89 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
17f8a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
17f8b 6c 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d  led to reclaim m
17f8c 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20  emory.** before 
17f8d 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6c 6f  the database clo
17f8e 73 65 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f  ses.  It is also
17f8f 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61   called during a
17f90 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20   rollback.** if 
17f91 74 68 65 72 65 20 77 65 72 65 20 73 63 68 65 6d  there were schem
17f92 61 20 63 68 61 6e 67 65 73 20 64 75 72 69 6e 67  a changes during
17f93 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
17f94 20 6f 72 20 69 66 20 61 0a 2a 2a 20 73 63 68 65   or if a.** sche
17f95 6d 61 2d 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74  ma-cookie mismat
17f96 63 68 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  ch occurs..**.**
17f97 20 49 66 20 69 44 62 3c 3d 30 20 74 68 65 6e 20   If iDb<=0 then 
17f98 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e  reset the intern
17f99 61 6c 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73  al schema tables
17f9a 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73   for all databas
17f9b 65 0a 2a 2a 20 66 69 6c 65 73 2e 20 20 49 66 20  e.** files.  If 
17f9c 69 44 62 3e 3d 32 20 74 68 65 6e 20 72 65 73 65  iDb>=2 then rese
17f9d 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  t the internal s
17f9e 63 68 65 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74  chema for only t
17f9f 68 65 0a 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c  he.** single fil
17fa0 65 20 69 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a  e indicated..*/.
17fa1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
17fa2 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74  oid sqlite3Reset
17fa3 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73  InternalSchema(s
17fa4 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
17fa5 69 44 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  iDb){.  int i, j
17fa6 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  ;..  assert( iDb
17fa7 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
17fa8 44 62 20 29 3b 0a 20 20 66 6f 72 28 69 3d 69 44  Db );.  for(i=iD
17fa9 62 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  b; i<db->nDb; i+
17faa 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20  +){.    Db *pDb 
17fab 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20  = &db->aDb[i];. 
17fac 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68     if( pDb->pSch
17fad 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ema ){.      sql
17fae 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 70  ite3SchemaFree(p
17faf 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  Db->pSchema);.  
17fb0 20 20 7d 0a 20 20 20 20 69 66 28 20 69 44 62 3e    }.    if( iDb>
17fb1 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a  0 ) return;.  }.
17fb2 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 30    assert( iDb==0
17fb3 20 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20   );.  db->flags 
17fb4 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72  &= ~SQLITE_Inter
17fb5 6e 43 68 61 6e 67 65 73 3b 0a 0a 20 20 2f 2a 20  nChanges;..  /* 
17fb6 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  If one or more o
17fb7 66 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  f the auxiliary 
17fb8 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 68  database files h
17fb9 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c 0a  as been closed,.
17fba 20 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76 65    ** then remove
17fbb 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20 61   them from the a
17fbc 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
17fbd 65 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b 65  e list.  We take
17fbe 20 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72 74   the.  ** opport
17fbf 75 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69 73  unity to do this
17fc0 20 68 65 72 65 20 73 69 6e 63 65 20 77 65 20 68   here since we h
17fc1 61 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65 64  ave just deleted
17fc2 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a 2a   all of the.  **
17fc3 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61 62   schema hash tab
17fc4 6c 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f 72  les and therefor
17fc5 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f  e do not have to
17fc6 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65   make any change
17fc7 73 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f 66  s.  ** to any of
17fc8 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a 20   those tables.. 
17fc9 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
17fca 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
17fcb 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a 70      struct Db *p
17fcc 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d  Db = &db->aDb[i]
17fcd 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70  ;.    if( pDb->p
17fce 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  Bt==0 ){.      i
17fcf 66 28 20 70 44 62 2d 3e 70 41 75 78 20 26 26 20  f( pDb->pAux && 
17fd0 70 44 62 2d 3e 78 46 72 65 65 41 75 78 20 29 20  pDb->xFreeAux ) 
17fd1 70 44 62 2d 3e 78 46 72 65 65 41 75 78 28 70 44  pDb->xFreeAux(pD
17fd2 62 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 20 20  b->pAux);.      
17fd3 70 44 62 2d 3e 70 41 75 78 20 3d 20 30 3b 0a 20  pDb->pAux = 0;. 
17fd4 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
17fd5 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  =j=2; i<db->nDb;
17fd6 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63   i++){.    struc
17fd7 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d  t Db *pDb = &db-
17fd8 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28  >aDb[i];.    if(
17fd9 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a   pDb->pBt==0 ){.
17fda 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
17fdb 65 65 28 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  ee(pDb->zName);.
17fdc 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65        pDb->zName
17fdd 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e 74   = 0;.      cont
17fde 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  inue;.    }.    
17fdf 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20 20  if( j<i ){.     
17fe0 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 62   db->aDb[j] = db
17fe1 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d 0a  ->aDb[i];.    }.
17fe2 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 6d      j++;.  }.  m
17fe3 65 6d 73 65 74 28 26 64 62 2d 3e 61 44 62 5b 6a  emset(&db->aDb[j
17fe4 5d 2c 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d 6a  ], 0, (db->nDb-j
17fe5 29 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  )*sizeof(db->aDb
17fe6 5b 6a 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44 62  [j]));.  db->nDb
17fe7 20 3d 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d 3e   = j;.  if( db->
17fe8 6e 44 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61 44  nDb<=2 && db->aD
17fe9 62 21 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63  b!=db->aDbStatic
17fea 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64   ){.    memcpy(d
17feb 62 2d 3e 61 44 62 53 74 61 74 69 63 2c 20 64 62  b->aDbStatic, db
17fec 2d 3e 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66 28  ->aDb, 2*sizeof(
17fed 64 62 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20 20  db->aDb[0]));.  
17fee 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64    sqlite3_free(d
17fef 62 2d 3e 61 44 62 29 3b 0a 20 20 20 20 64 62 2d  b->aDb);.    db-
17ff0 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44 62 53 74  >aDb = db->aDbSt
17ff1 61 74 69 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  atic;.  }.}../*.
17ff2 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
17ff3 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  is called when a
17ff4 20 63 6f 6d 6d 69 74 20 6f 63 63 75 72 73 2e 0a   commit occurs..
17ff5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17ff6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
17ff7 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e  mmitInternalChan
17ff8 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ges(sqlite3 *db)
17ff9 7b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d  {.  db->flags &=
17ffa 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43   ~SQLITE_InternC
17ffb 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  hanges;.}../*.**
17ffc 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d   Clear the colum
17ffd 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 61 20 74  n names from a t
17ffe 61 62 6c 65 20 6f 72 20 76 69 65 77 2e 0a 2a 2f  able or view..*/
17fff 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
18000 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61  iteResetColumnNa
18001 6d 65 73 28 54 61 62 6c 65 20 2a 70 54 61 62 6c  mes(Table *pTabl
18002 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43  e){.  int i;.  C
18003 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 61  olumn *pCol;.  a
18004 73 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30  ssert( pTable!=0
18005 20 29 3b 0a 20 20 69 66 28 20 28 70 43 6f 6c 20   );.  if( (pCol 
18006 3d 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 21  = pTable->aCol)!
18007 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  =0 ){.    for(i=
18008 30 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e 43 6f  0; i<pTable->nCo
18009 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; i++, pCol++){
1800a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
1800b 72 65 65 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29  ree(pCol->zName)
1800c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1800d 78 70 72 44 65 6c 65 74 65 28 70 43 6f 6c 2d 3e  xprDelete(pCol->
1800e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 73 71  pDflt);.      sq
1800f 6c 69 74 65 33 5f 66 72 65 65 28 70 43 6f 6c 2d  lite3_free(pCol-
18010 3e 7a 54 79 70 65 29 3b 0a 20 20 20 20 20 20 73  >zType);.      s
18011 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 6f 6c  qlite3_free(pCol
18012 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a  ->zColl);.    }.
18013 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
18014 28 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a  (pTable->aCol);.
18015 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 61 43    }.  pTable->aC
18016 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62 6c 65  ol = 0;.  pTable
18017 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f  ->nCol = 0;.}../
18018 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20  *.** Remove the 
18019 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72 75  memory data stru
1801a 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74 65  ctures associate
1801b 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e  d with the given
1801c 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f 20 63  .** Table.  No c
1801d 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
1801e 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69 73 20  to disk by this 
1801f 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54  routine..**.** T
18020 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74  his routine just
18021 20 64 65 6c 65 74 65 73 20 74 68 65 20 64 61 74   deletes the dat
18022 61 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 74  a structure.  It
18023 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b   does not unlink
18024 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 64 61  .** the table da
18025 74 61 20 73 74 72 75 63 74 75 72 65 20 66 72 6f  ta structure fro
18026 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
18027 2e 20 20 4e 6f 72 20 64 6f 65 73 20 69 74 20 72  .  Nor does it r
18028 65 6d 6f 76 65 0a 2a 2a 20 66 6f 72 65 69 67 6e  emove.** foreign
18029 20 6b 65 79 73 20 66 72 6f 6d 20 74 68 65 20 73   keys from the s
1802a 71 6c 69 74 65 2e 61 46 4b 65 79 20 68 61 73 68  qlite.aFKey hash
1802b 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69 74 20   table.  But it 
1802c 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a 2a 20  does destroy.** 
1802d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
1802e 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63 65 73  s of the indices
1802f 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79   and foreign key
18030 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
18031 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e  h .** the table.
18032 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18033 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
18034 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c 65  eleteTable(Table
18035 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49 6e 64   *pTable){.  Ind
18036 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70 4e 65  ex *pIndex, *pNe
18037 78 74 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65  xt;.  FKey *pFKe
18038 79 2c 20 2a 70 4e 65 78 74 46 4b 65 79 3b 0a 0a  y, *pNextFKey;..
18039 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20    if( pTable==0 
1803a 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20  ) return;..  /* 
1803b 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68  Do not delete th
1803c 65 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 74 68  e table until th
1803d 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
1803e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e 20  t reaches zero. 
1803f 2a 2f 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52 65  */.  pTable->nRe
18040 66 2d 2d 3b 0a 20 20 69 66 28 20 70 54 61 62 6c  f--;.  if( pTabl
18041 65 2d 3e 6e 52 65 66 3e 30 20 29 7b 0a 20 20 20  e->nRef>0 ){.   
18042 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
18043 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e  ssert( pTable->n
18044 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  Ref==0 );..  /* 
18045 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69 63  Delete all indic
18046 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
18047 74 68 20 74 68 69 73 20 74 61 62 6c 65 0a 20 20  th this table.  
18048 2a 2f 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 20  */.  for(pIndex 
18049 3d 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  = pTable->pIndex
1804a 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78  ; pIndex; pIndex
1804b 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65  =pNext){.    pNe
1804c 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65  xt = pIndex->pNe
1804d 78 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  xt;.    assert( 
1804e 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 3d  pIndex->pSchema=
1804f 3d 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61  =pTable->pSchema
18050 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 44 65   );.    sqliteDe
18051 6c 65 74 65 49 6e 64 65 78 28 70 49 6e 64 65 78  leteIndex(pIndex
18052 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  );.  }..#ifndef 
18053 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
18054 49 47 4e 5f 4b 45 59 0a 20 20 2f 2a 20 44 65 6c  IGN_KEY.  /* Del
18055 65 74 65 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20  ete all foreign 
18056 6b 65 79 73 20 61 73 73 6f 63 69 61 74 65 64 20  keys associated 
18057 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 2e  with this table.
18058 20 20 54 68 65 20 6b 65 79 73 0a 20 20 2a 2a 20    The keys.  ** 
18059 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65  should have alre
1805a 61 64 79 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65  ady been unlinke
1805b 64 20 66 72 6f 6d 20 74 68 65 20 70 53 63 68 65  d from the pSche
1805c 6d 61 2d 3e 61 46 4b 65 79 20 68 61 73 68 20 74  ma->aFKey hash t
1805d 61 62 6c 65 20 0a 20 20 2a 2f 0a 20 20 66 6f 72  able .  */.  for
1805e 28 70 46 4b 65 79 3d 70 54 61 62 6c 65 2d 3e 70  (pFKey=pTable->p
1805f 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b  FKey; pFKey; pFK
18060 65 79 3d 70 4e 65 78 74 46 4b 65 79 29 7b 0a 20  ey=pNextFKey){. 
18061 20 20 20 70 4e 65 78 74 46 4b 65 79 20 3d 20 70     pNextFKey = p
18062 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b  FKey->pNextFrom;
18063 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
18064 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70 54  ite3HashFind(&pT
18065 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 61  able->pSchema->a
18066 46 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20  FKey,.          
18067 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18068 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 73 74 72   pFKey->zTo, str
18069 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a 54 6f 29 2b  len(pFKey->zTo)+
1806a 31 29 21 3d 70 46 4b 65 79 20 29 3b 0a 20 20 20  1)!=pFKey );.   
1806b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46   sqlite3_free(pF
1806c 4b 65 79 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Key);.  }.#endif
1806d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68  ..  /* Delete th
1806e 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
1806f 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20  e itself..  */. 
18070 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75   sqliteResetColu
18071 6d 6e 4e 61 6d 65 73 28 70 54 61 62 6c 65 29 3b  mnNames(pTable);
18072 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
18073 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a  pTable->zName);.
18074 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
18075 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 3b  Table->zColAff);
18076 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
18077 44 65 6c 65 74 65 28 70 54 61 62 6c 65 2d 3e 70  Delete(pTable->p
18078 53 65 6c 65 63 74 29 3b 0a 23 69 66 6e 64 65 66  Select);.#ifndef
18079 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
1807a 43 4b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  CK.  sqlite3Expr
1807b 44 65 6c 65 74 65 28 70 54 61 62 6c 65 2d 3e 70  Delete(pTable->p
1807c 43 68 65 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 20  Check);.#endif. 
1807d 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61   sqlite3VtabClea
1807e 72 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c  r(pTable);.  sql
1807f 69 74 65 33 5f 66 72 65 65 28 70 54 61 62 6c 65  ite3_free(pTable
18080 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69  );.}../*.** Unli
18081 6e 6b 20 74 68 65 20 67 69 76 65 6e 20 74 61 62  nk the given tab
18082 6c 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  le from the hash
18083 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68 65 20   tables and the 
18084 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74 61  delete the.** ta
18085 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 77 69  ble structure wi
18086 74 68 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63  th all its indic
18087 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b  es and foreign k
18088 65 79 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  eys..*/.SQLITE_P
18089 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1808a 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
1808b 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20  teTable(sqlite3 
1808c 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f  *db, int iDb, co
1808d 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61  nst char *zTabNa
1808e 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  me){.  Table *p;
1808f 0a 20 20 46 4b 65 79 20 2a 70 46 31 2c 20 2a 70  .  FKey *pF1, *p
18090 46 32 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a  F2;.  Db *pDb;..
18091 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
18092 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
18093 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
18094 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
18095 7a 54 61 62 4e 61 6d 65 20 26 26 20 7a 54 61 62  zTabName && zTab
18096 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62  Name[0] );.  pDb
18097 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
18098 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48  ;.  p = sqlite3H
18099 61 73 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e  ashInsert(&pDb->
1809a 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
1809b 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 73 74 72 6c  , zTabName, strl
1809c 65 6e 28 7a 54 61 62 4e 61 6d 65 29 2b 31 2c 30  en(zTabName)+1,0
1809d 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 23 69  );.  if( p ){.#i
1809e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1809f 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
180a0 20 20 66 6f 72 28 70 46 31 3d 70 2d 3e 70 46 4b    for(pF1=p->pFK
180a1 65 79 3b 20 70 46 31 3b 20 70 46 31 3d 70 46 31  ey; pF1; pF1=pF1
180a2 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20  ->pNextFrom){.  
180a3 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 74      int nTo = st
180a4 72 6c 65 6e 28 70 46 31 2d 3e 7a 54 6f 29 20 2b  rlen(pF1->zTo) +
180a5 20 31 3b 0a 20 20 20 20 20 20 70 46 32 20 3d 20   1;.      pF2 = 
180a6 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
180a7 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61  &pDb->pSchema->a
180a8 46 4b 65 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20  FKey, pF1->zTo, 
180a9 6e 54 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nTo);.      if( 
180aa 70 46 32 3d 3d 70 46 31 20 29 7b 0a 20 20 20 20  pF2==pF1 ){.    
180ab 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
180ac 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 63 68  nsert(&pDb->pSch
180ad 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 31 2d  ema->aFKey, pF1-
180ae 3e 7a 54 6f 2c 20 6e 54 6f 2c 20 70 46 31 2d 3e  >zTo, nTo, pF1->
180af 70 4e 65 78 74 54 6f 29 3b 0a 20 20 20 20 20 20  pNextTo);.      
180b0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77  }else{.        w
180b1 68 69 6c 65 28 20 70 46 32 20 26 26 20 70 46 32  hile( pF2 && pF2
180b2 2d 3e 70 4e 65 78 74 54 6f 21 3d 70 46 31 20 29  ->pNextTo!=pF1 )
180b3 7b 20 70 46 32 3d 70 46 32 2d 3e 70 4e 65 78 74  { pF2=pF2->pNext
180b4 54 6f 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66  To; }.        if
180b5 28 20 70 46 32 20 29 7b 0a 20 20 20 20 20 20 20  ( pF2 ){.       
180b6 20 20 20 70 46 32 2d 3e 70 4e 65 78 74 54 6f 20     pF2->pNextTo 
180b7 3d 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f 3b 0a  = pF1->pNextTo;.
180b8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
180b9 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
180ba 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
180bb 54 61 62 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20  Table(p);.  }.  
180bc 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
180bd 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
180be 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  s;.}../*.** Give
180bf 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74 75 72  n a token, retur
180c0 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  n a string that 
180c1 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
180c2 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a 2a 20  text of that.** 
180c3 74 6f 6b 65 6e 20 77 69 74 68 20 61 6e 79 20 71  token with any q
180c4 75 6f 74 61 74 69 6f 6e 73 20 72 65 6d 6f 76 65  uotations remove
180c5 64 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  d.  Space to hol
180c6 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  d the returned s
180c7 74 72 69 6e 67 0a 2a 2a 20 69 73 20 6f 62 74 61  tring.** is obta
180c8 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
180c9 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73  Malloc() and mus
180ca 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68  t be freed by th
180cb 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e  e calling.** fun
180cc 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b  ction..**.** Tok
180cd 65 6e 73 20 61 72 65 20 6f 66 74 65 6e 20 6a 75  ens are often ju
180ce 73 74 20 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f  st pointers into
180cf 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
180d0 4c 20 74 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a  L text and so.**
180d1 20 61 72 65 20 6e 6f 74 20 5c 30 30 30 20 74 65   are not \000 te
180d2 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 61 72 65  rminated and are
180d3 20 6e 6f 74 20 70 65 72 73 69 73 74 65 6e 74 2e   not persistent.
180d4 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73    The returned s
180d5 74 72 69 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30  tring.** is \000
180d6 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20   terminated and 
180d7 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a  is persistent..*
180d8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
180d9 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61   char *sqlite3Na
180da 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69  meFromToken(sqli
180db 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a  te3 *db, Token *
180dc 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  pName){.  char *
180dd 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61  zName;.  if( pNa
180de 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20  me ){.    zName 
180df 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
180e0 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 4e  up(db, (char*)pN
180e1 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e  ame->z, pName->n
180e2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
180e3 71 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20  quote(zName);.  
180e4 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65  }else{.    zName
180e5 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
180e6 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  rn zName;.}../*.
180e7 2a 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69  ** Open the sqli
180e8 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
180e9 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61  stored in databa
180ea 73 65 20 6e 75 6d 62 65 72 20 69 44 62 20 66 6f  se number iDb fo
180eb 72 0a 2a 2a 20 77 72 69 74 69 6e 67 2e 20 54 68  r.** writing. Th
180ec 65 20 74 61 62 6c 65 20 69 73 20 6f 70 65 6e 65  e table is opene
180ed 64 20 75 73 69 6e 67 20 63 75 72 73 6f 72 20 30  d using cursor 0
180ee 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
180ef 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
180f0 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28  OpenMasterTable(
180f1 50 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69 44  Parse *p, int iD
180f2 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  b){.  Vdbe *v = 
180f3 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
180f4 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c  );.  sqlite3Tabl
180f5 65 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41  eLock(p, iDb, MA
180f6 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 53 43  STER_ROOT, 1, SC
180f7 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 29  HEMA_TABLE(iDb))
180f8 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
180f9 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67  ddOp(v, OP_Integ
180fa 65 72 2c 20 69 44 62 2c 20 30 29 3b 0a 20 20 73  er, iDb, 0);.  s
180fb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
180fc 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c  v, OP_OpenWrite,
180fd 20 30 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 29   0, MASTER_ROOT)
180fe 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
180ff 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 74 4e 75  ddOp(v, OP_SetNu
18100 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 35 29 3b  mColumns, 0, 5);
18101 20 2f 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65   /* sqlite_maste
18102 72 20 68 61 73 20 35 20 63 6f 6c 75 6d 6e 73 20  r has 5 columns 
18103 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  */.}../*.** The 
18104 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 63 6f 6e  token *pName con
18105 74 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f  tains the name o
18106 66 20 61 20 64 61 74 61 62 61 73 65 20 28 65 69  f a database (ei
18107 74 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 0a 2a  ther "main" or.*
18108 2a 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65 20  * "temp" or the 
18109 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61 63  name of an attac
1810a 68 65 64 20 64 62 29 2e 20 54 68 69 73 20 72 6f  hed db). This ro
1810b 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
1810c 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68  e.** index of th
1810d 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65  e named database
1810e 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f   in db->aDb[], o
1810f 72 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65  r -1 if the name
18110 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f  d db .** does no
18111 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49  t exist..*/.SQLI
18112 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18113 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c  qlite3FindDb(sql
18114 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20  ite3 *db, Token 
18115 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69  *pName){.  int i
18116 20 3d 20 2d 31 3b 20 20 20 20 2f 2a 20 44 61 74   = -1;    /* Dat
18117 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  abase number */.
18118 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
18119 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
1811a 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20  aracters in the 
1811b 6e 61 6d 65 20 2a 2f 0a 20 20 44 62 20 2a 70 44  name */.  Db *pD
1811c 62 3b 20 20 20 20 20 20 20 2f 2a 20 41 20 64 61  b;       /* A da
1811d 74 61 62 61 73 65 20 77 68 6f 73 65 20 6e 61 6d  tabase whose nam
1811e 65 20 73 70 61 63 65 20 69 73 20 62 65 69 6e 67  e space is being
1811f 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63   searched */.  c
18120 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a  har *zName;   /*
18121 20 4e 61 6d 65 20 77 65 20 61 72 65 20 73 65 61   Name we are sea
18122 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 0a 20  rching for */.. 
18123 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
18124 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
18125 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  , pName);.  if( 
18126 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 6e 20 3d  zName ){.    n =
18127 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a   strlen(zName);.
18128 20 20 20 20 66 6f 72 28 69 3d 28 64 62 2d 3e 6e      for(i=(db->n
18129 44 62 2d 31 29 2c 20 70 44 62 3d 26 64 62 2d 3e  Db-1), pDb=&db->
1812a 61 44 62 5b 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d  aDb[i]; i>=0; i-
1812b 2d 2c 20 70 44 62 2d 2d 29 7b 0a 20 20 20 20 20  -, pDb--){.     
1812c 20 69 66 28 20 28 21 4f 4d 49 54 5f 54 45 4d 50   if( (!OMIT_TEMP
1812d 44 42 20 7c 7c 20 69 21 3d 31 20 29 20 26 26 20  DB || i!=1 ) && 
1812e 6e 3d 3d 73 74 72 6c 65 6e 28 70 44 62 2d 3e 7a  n==strlen(pDb->z
1812f 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20 20 20  Name) && .      
18130 20 20 20 20 30 3d 3d 73 71 6c 69 74 65 33 53 74      0==sqlite3St
18131 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65  rICmp(pDb->zName
18132 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  , zName) ){.    
18133 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
18134 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
18135 69 74 65 33 5f 66 72 65 65 28 7a 4e 61 6d 65 29  ite3_free(zName)
18136 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  ;.  }.  return i
18137 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c  ;.}../* The tabl
18138 65 20 6f 72 20 76 69 65 77 20 6f 72 20 74 72 69  e or view or tri
18139 67 67 65 72 20 6e 61 6d 65 20 69 73 20 70 61 73  gger name is pas
1813a 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74  sed to this rout
1813b 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a  ine via tokens.*
1813c 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61  * pName1 and pNa
1813d 6d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c  me2. If the tabl
1813e 65 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79  e name was fully
1813f 20 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20   qualified, for 
18140 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43  example:.**.** C
18141 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e  REATE TABLE xxx.
18142 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a  yyy (...);.** .*
18143 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73  * Then pName1 is
18144 20 73 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e   set to "xxx" an
18145 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20  d pName2 "yyy". 
18146 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
18147 64 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  d if.** the tabl
18148 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75  e name is not fu
18149 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69  lly qualified, i
1814a 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54  .e.:.**.** CREAT
1814b 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29  E TABLE yyy(...)
1814c 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61  ;.**.** Then pNa
1814d 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79  me1 is set to "y
1814e 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69  yy" and pName2 i
1814f 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  s ""..**.** This
18150 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
18151 65 20 2a 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e  e *ppUnqual poin
18152 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20  ter to point at 
18153 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65  the token (pName
18154 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20  1 or.** pName2) 
18155 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20  that stores the 
18156 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c  unqualified tabl
18157 65 20 6e 61 6d 65 2e 20 20 54 68 65 20 69 6e 64  e name.  The ind
18158 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74  ex of the.** dat
18159 61 62 61 73 65 20 22 78 78 78 22 20 69 73 20 72  abase "xxx" is r
1815a 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
1815b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1815c 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
1815d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
1815e 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
1815f 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
18160 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
18161 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
18162 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e1,      /* The 
18163 22 78 78 78 22 20 69 6e 20 74 68 65 20 6e 61 6d  "xxx" in the nam
18164 65 20 22 78 78 78 2e 79 79 79 22 20 6f 72 20 22  e "xxx.yyy" or "
18165 78 78 78 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  xxx" */.  Token 
18166 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a  *pName2,      /*
18167 20 54 68 65 20 22 79 79 79 22 20 69 6e 20 74 68   The "yyy" in th
18168 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22  e name "xxx.yyy"
18169 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55   */.  Token **pU
1816a 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20 57 72 69  nqual     /* Wri
1816b 74 65 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69  te the unqualifi
1816c 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68  ed object name h
1816d 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
1816e 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
1816f 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
18170 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ase holding the 
18171 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69  object */.  sqli
18172 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
18173 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 4e 61  ->db;..  if( pNa
18174 6d 65 32 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e  me2 && pName2->n
18175 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  >0 ){.    assert
18176 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
18177 20 29 3b 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c   );.    *pUnqual
18178 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 20 20 20 69   = pName2;.    i
18179 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  Db = sqlite3Find
1817a 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a  Db(db, pName1);.
1817b 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b      if( iDb<0 ){
1817c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1817d 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1817e 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65  unknown database
1817f 20 25 54 22 2c 20 70 4e 61 6d 65 31 29 3b 0a 20   %T", pName1);. 
18180 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72       pParse->nEr
18181 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72  r++;.      retur
18182 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  n -1;.    }.  }e
18183 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
18184 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 30   db->init.iDb==0
18185 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73   || db->init.bus
18186 79 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 64  y );.    iDb = d
18187 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20 20 20  b->init.iDb;.   
18188 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d   *pUnqual = pNam
18189 65 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e1;.  }.  return
1818a 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   iDb;.}../*.** T
1818b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
1818c 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20  sed to check if 
1818d 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67  the UTF-8 string
1818e 20 7a 4e 61 6d 65 20 69 73 20 61 20 6c 65 67 61   zName is a lega
1818f 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 66 69 65 64  l.** unqualified
18190 20 6e 61 6d 65 20 66 6f 72 20 61 20 6e 65 77 20   name for a new 
18191 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 28 74  schema object (t
18192 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 76 69 65  able, index, vie
18193 77 20 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72 29  w or.** trigger)
18194 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 61 72 65 20  . All names are 
18195 6c 65 67 61 6c 20 65 78 63 65 70 74 20 74 68 6f  legal except tho
18196 73 65 20 74 68 61 74 20 62 65 67 69 6e 20 77 69  se that begin wi
18197 74 68 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a  th the string.**
18198 20 22 73 71 6c 69 74 65 5f 22 20 28 69 6e 20 75   "sqlite_" (in u
18199 70 70 65 72 2c 20 6c 6f 77 65 72 20 6f 72 20 6d  pper, lower or m
1819a 69 78 65 64 20 63 61 73 65 29 2e 20 54 68 69 73  ixed case). This
1819b 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20   portion of the 
1819c 6e 61 6d 65 73 70 61 63 65 0a 2a 2a 20 69 73 20  namespace.** is 
1819d 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e 74  reserved for int
1819e 65 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f 0a 53 51  ernal use..*/.SQ
1819f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
181a0 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a   sqlite3CheckObj
181a1 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 70  ectName(Parse *p
181a2 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
181a3 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 66 28  r *zName){.  if(
181a4 20 21 70 50 61 72 73 65 2d 3e 64 62 2d 3e 69 6e   !pParse->db->in
181a5 69 74 2e 62 75 73 79 20 26 26 20 70 50 61 72 73  it.busy && pPars
181a6 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 0a 20 20  e->nested==0 .  
181a7 20 20 20 20 20 20 20 20 26 26 20 28 70 50 61 72          && (pPar
181a8 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  se->db->flags & 
181a9 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65  SQLITE_WriteSche
181aa 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20  ma)==0.         
181ab 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74   && 0==sqlite3St
181ac 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 20 22 73  rNICmp(zName, "s
181ad 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a 20  qlite_", 7) ){. 
181ae 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
181af 73 67 28 70 50 61 72 73 65 2c 20 22 6f 62 6a 65  sg(pParse, "obje
181b0 63 74 20 6e 61 6d 65 20 72 65 73 65 72 76 65 64  ct name reserved
181b1 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
181b2 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  e: %s", zName);.
181b3 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
181b4 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72  E_ERROR;.  }.  r
181b5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
181b6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20  .}../*.** Begin 
181b7 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20 6e  constructing a n
181b8 65 77 20 74 61 62 6c 65 20 72 65 70 72 65 73 65  ew table represe
181b9 6e 74 61 74 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72  ntation in memor
181ba 79 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 74  y.  This is.** t
181bb 68 65 20 66 69 72 73 74 20 6f 66 20 73 65 76 65  he first of seve
181bc 72 61 6c 20 61 63 74 69 6f 6e 20 72 6f 75 74 69  ral action routi
181bd 6e 65 73 20 74 68 61 74 20 67 65 74 20 63 61 6c  nes that get cal
181be 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 0a  led in response.
181bf 2a 2a 20 74 6f 20 61 20 43 52 45 41 54 45 20 54  ** to a CREATE T
181c0 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
181c1 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c 20   In particular, 
181c2 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
181c3 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 74 65 72 20  called.** after 
181c4 73 65 65 69 6e 67 20 74 6f 6b 65 6e 73 20 22 43  seeing tokens "C
181c5 52 45 41 54 45 22 20 61 6e 64 20 22 54 41 42 4c  REATE" and "TABL
181c6 45 22 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65  E" and the table
181c7 20 6e 61 6d 65 2e 20 54 68 65 20 69 73 54 65 6d   name. The isTem
181c8 70 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74 72 75  p.** flag is tru
181c9 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 73  e if the table s
181ca 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65 64 20  hould be stored 
181cb 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  in the auxiliary
181cc 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
181cd 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 20  e instead of in 
181ce 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
181cf 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73  e file.  This is
181d0 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 65 20 63 61   normally the ca
181d1 73 65 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 22  se.** when the "
181d2 54 45 4d 50 22 20 6f 72 20 22 54 45 4d 50 4f 52  TEMP" or "TEMPOR
181d3 41 52 59 22 20 6b 65 79 77 6f 72 64 20 6f 63 63  ARY" keyword occ
181d4 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 0a 2a  urs in between.*
181d5 2a 20 43 52 45 41 54 45 20 61 6e 64 20 54 41 42  * CREATE and TAB
181d6 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  LE..**.** The ne
181d7 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 69  w table record i
181d8 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e  s initialized an
181d9 64 20 70 75 74 20 69 6e 20 70 50 61 72 73 65 2d  d put in pParse-
181da 3e 70 4e 65 77 54 61 62 6c 65 2e 0a 2a 2a 20 41  >pNewTable..** A
181db 73 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 43 52  s more of the CR
181dc 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
181dd 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c 20  ment is parsed, 
181de 61 64 64 69 74 69 6f 6e 61 6c 20 61 63 74 69 6f  additional actio
181df 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69  n.** routines wi
181e0 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20  ll be called to 
181e1 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61  add more informa
181e2 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 72 65 63  tion to this rec
181e3 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65  ord..** At the e
181e4 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  nd of the CREATE
181e5 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
181e6 2c 20 74 68 65 20 73 71 6c 69 74 65 33 45 6e 64  , the sqlite3End
181e7 54 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 0a  Table() routine.
181e8 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ** is called to 
181e9 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 63 6f 6e  complete the con
181ea 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65  struction of the
181eb 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72   new table recor
181ec 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
181ed 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
181ee 33 53 74 61 72 74 54 61 62 6c 65 28 0a 20 20 50  3StartTable(.  P
181ef 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
181f0 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
181f1 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  t */.  Token *pN
181f2 61 6d 65 31 2c 20 20 20 2f 2a 20 46 69 72 73 74  ame1,   /* First
181f3 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d   part of the nam
181f4 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f  e of the table o
181f5 72 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65  r view */.  Toke
181f6 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 2f 2a 20  n *pName2,   /* 
181f7 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 74  Second part of t
181f8 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
181f9 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a  able or view */.
181fa 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20    int isTemp,   
181fb 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
181fc 69 73 20 69 73 20 61 20 54 45 4d 50 20 74 61 62  is is a TEMP tab
181fd 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69  le */.  int isVi
181fe 65 77 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ew,      /* True
181ff 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56 49   if this is a VI
18200 45 57 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69  EW */.  int isVi
18201 72 74 75 61 6c 2c 20 20 20 2f 2a 20 54 72 75 65  rtual,   /* True
18202 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56 49   if this is a VI
18203 52 54 55 41 4c 20 74 61 62 6c 65 20 2a 2f 0a 20  RTUAL table */. 
18204 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20   int noErr      
18205 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20    /* Do nothing 
18206 69 66 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79  if table already
18207 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20   exists */.){.  
18208 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 0a 20  Table *pTable;. 
18209 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30   char *zName = 0
1820a 3b 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  ; /* The name of
1820b 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 2a   the new table *
1820c 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
1820d 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1820e 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69  Vdbe *v;.  int i
1820f 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44  Db;         /* D
18210 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 74  atabase number t
18211 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 61 62  o create the tab
18212 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  le in */.  Token
18213 20 2a 70 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 55   *pName;    /* U
18214 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20  nqualified name 
18215 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
18216 63 72 65 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20  create */..  /* 
18217 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65  The table or vie
18218 77 20 6e 61 6d 65 20 74 6f 20 63 72 65 61 74 65  w name to create
18219 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68   is passed to th
1821a 69 73 20 72 6f 75 74 69 6e 65 20 76 69 61 20 74  is routine via t
1821b 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70 4e 61 6d 65  okens.  ** pName
1821c 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49 66  1 and pName2. If
1821d 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
1821e 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69 66  was fully qualif
1821f 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ied, for example
18220 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41  :.  **.  ** CREA
18221 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79 79  TE TABLE xxx.yyy
18222 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 20 0a 20 20   (...);.  ** .  
18223 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69  ** Then pName1 i
18224 73 20 73 65 74 20 74 6f 20 22 78 78 78 22 20 61  s set to "xxx" a
18225 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e  nd pName2 "yyy".
18226 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61   On the other ha
18227 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 74  nd if.  ** the t
18228 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74  able name is not
18229 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64   fully qualified
1822a 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a 0a 20 20 2a  , i.e.:.  **.  *
1822b 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 79  * CREATE TABLE y
1822c 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a 20 20  yy(...);.  **.  
1822d 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69  ** Then pName1 i
1822e 73 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 61  s set to "yyy" a
1822f 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e  nd pName2 is "".
18230 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63  .  **.  ** The c
18231 61 6c 6c 20 62 65 6c 6f 77 20 73 65 74 73 20 74  all below sets t
18232 68 65 20 70 4e 61 6d 65 20 70 6f 69 6e 74 65 72  he pName pointer
18233 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
18234 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f   token (pName1 o
18235 72 0a 20 20 2a 2a 20 70 4e 61 6d 65 32 29 20 74  r.  ** pName2) t
18236 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 75  hat stores the u
18237 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65  nqualified table
18238 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 72 69 61   name. The varia
18239 62 6c 65 20 69 44 62 20 69 73 0a 20 20 2a 2a 20  ble iDb is.  ** 
1823a 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78  set to the index
1823b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1823c 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20   that the table 
1823d 6f 72 20 76 69 65 77 20 69 73 20 74 6f 20 62 65  or view is to be
1823e 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e  .  ** created in
1823f 2e 0a 20 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73  ..  */.  iDb = s
18240 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
18241 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
18242 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65  , pName2, &pName
18243 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29  );.  if( iDb<0 )
18244 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21   return;.  if( !
18245 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
18246 73 54 65 6d 70 20 26 26 20 69 44 62 3e 31 20 29  sTemp && iDb>1 )
18247 7b 0a 20 20 20 20 2f 2a 20 49 66 20 63 72 65 61  {.    /* If crea
18248 74 69 6e 67 20 61 20 74 65 6d 70 20 74 61 62 6c  ting a temp tabl
18249 65 2c 20 74 68 65 20 6e 61 6d 65 20 6d 61 79 20  e, the name may 
1824a 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65 64  not be qualified
1824b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45   */.    sqlite3E
1824c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1824d 22 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65  "temporary table
1824e 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 75 6e   name must be un
1824f 71 75 61 6c 69 66 69 65 64 22 29 3b 0a 20 20 20  qualified");.   
18250 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
18251 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
18252 26 26 20 69 73 54 65 6d 70 20 29 20 69 44 62 20  && isTemp ) iDb 
18253 3d 20 31 3b 0a 0a 20 20 70 50 61 72 73 65 2d 3e  = 1;..  pParse->
18254 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70 4e  sNameToken = *pN
18255 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 73  ame;.  zName = s
18256 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
18257 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
18258 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
18259 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 53   return;.  if( S
1825a 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
1825b 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
1825c 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20  (pParse, zName) 
1825d 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69  ){.    goto begi
1825e 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20  n_table_error;. 
1825f 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69   }.  if( db->ini
18260 74 2e 69 44 62 3d 3d 31 20 29 20 69 73 54 65 6d  t.iDb==1 ) isTem
18261 70 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53  p = 1;.#ifndef S
18262 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
18263 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72  RIZATION.  asser
18264 74 28 20 28 69 73 54 65 6d 70 20 26 20 31 29 3d  t( (isTemp & 1)=
18265 3d 69 73 54 65 6d 70 20 29 3b 0a 20 20 7b 0a 20  =isTemp );.  {. 
18266 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20     int code;.   
18267 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d   char *zDb = db-
18268 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
18269 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1826a 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
1826b 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c  , SQLITE_INSERT,
1826c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 73   SCHEMA_TABLE(is
1826d 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29 20 29  Temp), 0, zDb) )
1826e 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67  {.      goto beg
1826f 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
18270 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73      }.    if( is
18271 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66  View ){.      if
18272 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
18273 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20  & isTemp ){.    
18274 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
18275 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49  E_CREATE_TEMP_VI
18276 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EW;.      }else{
18277 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
18278 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49  SQLITE_CREATE_VI
18279 45 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EW;.      }.    
1827a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
1827b 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
1827c 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 20   isTemp ){.     
1827d 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
1827e 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42  _CREATE_TEMP_TAB
1827f 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  LE;.      }else{
18280 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
18281 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41  SQLITE_CREATE_TA
18282 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  BLE;.      }.   
18283 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 56 69   }.    if( !isVi
18284 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74 65 33  rtual && sqlite3
18285 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
18286 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 30  , code, zName, 0
18287 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20  , zDb) ){.      
18288 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
18289 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  _error;.    }.  
1828a 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d  }.#endif..  /* M
1828b 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e 65 77  ake sure the new
1828c 20 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f 65 73   table name does
1828d 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 74   not collide wit
1828e 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20  h an existing.  
1828f 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c  ** index or tabl
18290 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 61  e name in the sa
18291 6d 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 73  me database.  Is
18292 73 75 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  sue an error mes
18293 73 61 67 65 20 69 66 0a 20 20 2a 2a 20 69 74 20  sage if.  ** it 
18294 64 6f 65 73 2e 20 54 68 65 20 65 78 63 65 70 74  does. The except
18295 69 6f 6e 20 69 73 20 69 66 20 74 68 65 20 73 74  ion is if the st
18296 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 70 61  atement being pa
18297 72 73 65 64 20 77 61 73 20 70 61 73 73 65 64 0a  rsed was passed.
18298 20 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c 69 74    ** to an sqlit
18299 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28  e3_declare_vtab(
1829a 29 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61 74 20  ) call. In that 
1829b 63 61 73 65 20 6f 6e 6c 79 20 74 68 65 20 63 6f  case only the co
1829c 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a 2a 20  lumn names.  ** 
1829d 61 6e 64 20 74 79 70 65 73 20 77 69 6c 6c 20 62  and types will b
1829e 65 20 75 73 65 64 2c 20 73 6f 20 74 68 65 72 65  e used, so there
1829f 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74   is no need to t
182a0 65 73 74 20 66 6f 72 20 6e 61 6d 65 73 70 61 63  est for namespac
182a1 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69 6f 6e  e.  ** collision
182a2 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 49  s..  */.  if( !I
182a3 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
182a4 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
182a5 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
182a6 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
182a7 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67  {.      goto beg
182a8 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
182a9 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 6c 65      }.    pTable
182aa 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
182ab 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 64  ble(db, zName, d
182ac 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
182ad 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  e);.    if( pTab
182ae 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  le ){.      if( 
182af 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20  !noErr ){.      
182b0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
182b1 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65  g(pParse, "table
182b2 20 25 54 20 61 6c 72 65 61 64 79 20 65 78 69 73   %T already exis
182b3 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20  ts", pName);.   
182b4 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20     }.      goto 
182b5 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
182b6 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  r;.    }.    if(
182b7 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65   sqlite3FindInde
182b8 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 30 29 21  x(db, zName, 0)!
182b9 3d 30 20 26 26 20 28 69 44 62 3d 3d 30 20 7c 7c  =0 && (iDb==0 ||
182ba 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 29   !db->init.busy)
182bb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
182bc 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
182bd 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65  , "there is alre
182be 61 64 79 20 61 6e 20 69 6e 64 65 78 20 6e 61 6d  ady an index nam
182bf 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  ed %s", zName);.
182c0 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e        goto begin
182c1 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20  _table_error;.  
182c2 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 54 61 62 6c    }.  }..  pTabl
182c3 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  e = sqlite3DbMal
182c4 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
182c5 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 66  of(Table));.  if
182c6 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 7b 0a 20  ( pTable==0 ){. 
182c7 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
182c8 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 70 50 61  led = 1;.    pPa
182c9 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
182ca 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 70 50 61 72  _NOMEM;.    pPar
182cb 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  se->nErr++;.    
182cc 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
182cd 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 54  _error;.  }.  pT
182ce 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e  able->zName = zN
182cf 61 6d 65 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 69  ame;.  pTable->i
182d0 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 70 54 61  PKey = -1;.  pTa
182d1 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64  ble->pSchema = d
182d2 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
182d3 65 6d 61 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e  ema;.  pTable->n
182d4 52 65 66 20 3d 20 31 3b 0a 20 20 69 66 28 20 70  Ref = 1;.  if( p
182d5 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
182d6 20 29 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65   ) sqlite3Delete
182d7 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e  Table(pParse->pN
182d8 65 77 54 61 62 6c 65 29 3b 0a 20 20 70 50 61 72  ewTable);.  pPar
182d9 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20  se->pNewTable = 
182da 70 54 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 49 66  pTable;..  /* If
182db 20 74 68 69 73 20 69 73 20 74 68 65 20 6d 61 67   this is the mag
182dc 69 63 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  ic sqlite_sequen
182dd 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79  ce table used by
182de 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 2c 0a   autoincrement,.
182df 20 20 2a 2a 20 74 68 65 6e 20 72 65 63 6f 72 64    ** then record
182e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
182e1 69 73 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  is table in the 
182e2 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  main database st
182e3 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 73 6f 20  ructure.  ** so 
182e4 74 68 61 74 20 49 4e 53 45 52 54 20 63 61 6e 20  that INSERT can 
182e5 66 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20 65  find the table e
182e6 61 73 69 6c 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e  asily..  */.#ifn
182e7 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
182e8 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
182e9 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73  if( !pParse->nes
182ea 74 65 64 20 26 26 20 73 74 72 63 6d 70 28 7a 4e  ted && strcmp(zN
182eb 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 73 65 71  ame, "sqlite_seq
182ec 75 65 6e 63 65 22 29 3d 3d 30 20 29 7b 0a 20 20  uence")==0 ){.  
182ed 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d    pTable->pSchem
182ee 61 2d 3e 70 53 65 71 54 61 62 20 3d 20 70 54 61  a->pSeqTab = pTa
182ef 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ble;.  }.#endif.
182f0 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65  .  /* Begin gene
182f1 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20  rating the code 
182f2 74 68 61 74 20 77 69 6c 6c 20 69 6e 73 65 72 74  that will insert
182f3 20 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72   the table recor
182f4 64 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20  d into.  ** the 
182f5 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
182f6 62 6c 65 2e 20 20 4e 6f 74 65 20 69 6e 20 70 61  ble.  Note in pa
182f7 72 74 69 63 75 6c 61 72 20 74 68 61 74 20 77 65  rticular that we
182f8 20 6d 75 73 74 20 67 6f 20 61 68 65 61 64 0a 20   must go ahead. 
182f9 20 2a 2a 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65   ** and allocate
182fa 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
182fb 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  er for the table
182fc 20 65 6e 74 72 79 20 6e 6f 77 2e 20 20 42 65 66   entry now.  Bef
182fd 6f 72 65 20 61 6e 79 0a 20 20 2a 2a 20 50 52 49  ore any.  ** PRI
182fe 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
182ff 55 45 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20  UE keywords are 
18300 70 61 72 73 65 64 2e 20 20 54 68 6f 73 65 20 6b  parsed.  Those k
18301 65 79 77 6f 72 64 73 20 77 69 6c 6c 20 63 61 75  eywords will cau
18302 73 65 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20  se.  ** indices 
18303 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 61 6e  to be created an
18304 64 20 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f  d the table reco
18305 72 64 20 6d 75 73 74 20 63 6f 6d 65 20 62 65 66  rd must come bef
18306 6f 72 65 20 74 68 65 20 0a 20 20 2a 2a 20 69 6e  ore the .  ** in
18307 64 69 63 65 73 2e 20 20 48 65 6e 63 65 2c 20 74  dices.  Hence, t
18308 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
18309 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 6d   for the table m
1830a 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64  ust be allocated
1830b 0a 20 20 2a 2a 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  .  ** now..  */.
1830c 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
1830d 62 75 73 79 20 26 26 20 28 76 20 3d 20 73 71 6c  busy && (v = sql
1830e 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
1830f 73 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69  se))!=0 ){.    i
18310 6e 74 20 6c 62 6c 3b 0a 20 20 20 20 69 6e 74 20  nt lbl;.    int 
18311 66 69 6c 65 46 6f 72 6d 61 74 3b 0a 20 20 20 20  fileFormat;.    
18312 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
18313 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
18314 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66  e, 0, iDb);..#if
18315 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18316 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
18317 20 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 20    if( isVirtual 
18318 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
18319 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1831a 56 42 65 67 69 6e 2c 20 30 2c 20 30 29 3b 0a 20  VBegin, 0, 0);. 
1831b 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
1831c 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20   /* If the file 
1831d 66 6f 72 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64  format and encod
1831e 69 6e 67 20 69 6e 20 74 68 65 20 64 61 74 61 62  ing in the datab
1831f 61 73 65 20 68 61 76 65 20 6e 6f 74 20 62 65 65  ase have not bee
18320 6e 20 73 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73  n set, .    ** s
18321 65 74 20 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20  et them now..   
18322 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
18323 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52  dbeAddOp(v, OP_R
18324 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  eadCookie, iDb, 
18325 31 29 3b 20 20 20 2f 2a 20 66 69 6c 65 5f 66 6f  1);   /* file_fo
18326 72 6d 61 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69  rmat */.    sqli
18327 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
18328 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 6c 62  (v, iDb);.    lb
18329 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  l = sqlite3VdbeM
1832a 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
1832b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1832c 70 28 76 2c 20 4f 50 5f 49 66 2c 20 30 2c 20 6c  p(v, OP_If, 0, l
1832d 62 6c 29 3b 0a 20 20 20 20 66 69 6c 65 46 6f 72  bl);.    fileFor
1832e 6d 61 74 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73  mat = (db->flags
1832f 20 26 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79   & SQLITE_Legacy
18330 46 69 6c 65 46 6d 74 29 21 3d 30 20 3f 0a 20 20  FileFmt)!=0 ?.  
18331 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18332 31 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46  1 : SQLITE_MAX_F
18333 49 4c 45 5f 46 4f 52 4d 41 54 3b 0a 20 20 20 20  ILE_FORMAT;.    
18334 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
18335 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
18336 66 69 6c 65 46 6f 72 6d 61 74 2c 20 30 29 3b 0a  fileFormat, 0);.
18337 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
18338 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 74 43 6f  ddOp(v, OP_SetCo
18339 6f 6b 69 65 2c 20 69 44 62 2c 20 31 29 3b 0a 20  okie, iDb, 1);. 
1833a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1833b 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
1833c 72 2c 20 45 4e 43 28 64 62 29 2c 20 30 29 3b 0a  r, ENC(db), 0);.
1833d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1833e 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 74 43 6f  ddOp(v, OP_SetCo
1833f 6f 6b 69 65 2c 20 69 44 62 2c 20 34 29 3b 0a 20  okie, iDb, 4);. 
18340 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
18341 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c 62  solveLabel(v, lb
18342 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73  l);..    /* This
18343 20 6a 75 73 74 20 63 72 65 61 74 65 73 20 61 20   just creates a 
18344 70 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 65 63  place-holder rec
18345 6f 72 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74  ord in the sqlit
18346 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a  e_master table..
18347 20 20 20 20 2a 2a 20 54 68 65 20 72 65 63 6f 72      ** The recor
18348 64 20 63 72 65 61 74 65 64 20 64 6f 65 73 20 6e  d created does n
18349 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 74 68  ot contain anyth
1834a 69 6e 67 20 79 65 74 2e 20 20 49 74 20 77 69 6c  ing yet.  It wil
1834b 6c 20 62 65 20 72 65 70 6c 61 63 65 64 0a 20 20  l be replaced.  
1834c 20 20 2a 2a 20 62 79 20 74 68 65 20 72 65 61 6c    ** by the real
1834d 20 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 20 67   entry in code g
1834e 65 6e 65 72 61 74 65 64 20 61 74 20 73 71 6c 69  enerated at sqli
1834f 74 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 20  te3EndTable().. 
18350 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
18351 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 6e   rowid for the n
18352 65 77 20 65 6e 74 72 79 20 69 73 20 6c 65 66 74  ew entry is left
18353 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74   on the top of t
18354 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2a  he stack..    **
18355 20 54 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65   The rowid value
18356 20 69 73 20 6e 65 65 64 65 64 20 62 79 20 74 68   is needed by th
18357 65 20 63 6f 64 65 20 74 68 61 74 20 73 71 6c 69  e code that sqli
18358 74 65 33 45 6e 64 54 61 62 6c 65 20 77 69 6c 6c  te3EndTable will
18359 0a 20 20 20 20 2a 2a 20 67 65 6e 65 72 61 74 65  .    ** generate
1835a 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 20 21 64 65  ..    */.#if !de
1835b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1835c 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69  T_VIEW) || !defi
1835d 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1835e 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20  VIRTUALTABLE).  
1835f 20 20 69 66 28 20 69 73 56 69 65 77 20 7c 7c 20    if( isView || 
18360 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20  isVirtual ){.   
18361 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
18362 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
18363 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 65  r, 0, 0);.    }e
18364 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b  lse.#endif.    {
18365 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
18366 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 72  beAddOp(v, OP_Cr
18367 65 61 74 65 54 61 62 6c 65 2c 20 69 44 62 2c 20  eateTable, iDb, 
18368 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  0);.    }.    sq
18369 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54  lite3OpenMasterT
1836a 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
1836b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1836c 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65  beAddOp(v, OP_Ne
1836d 77 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b 0a 20  wRowid, 0, 0);. 
1836e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1836f 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 30  dOp(v, OP_Dup, 0
18370 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
18371 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
18372 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a 20 20  _Null, 0, 0);.  
18373 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
18374 4f 70 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  Op(v, OP_Insert,
18375 20 30 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e   0, OPFLAG_APPEN
18376 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  D);.    sqlite3V
18377 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
18378 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20  lose, 0, 0);.   
18379 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1837a 70 28 76 2c 20 4f 50 5f 50 75 6c 6c 2c 20 31 2c  p(v, OP_Pull, 1,
1837b 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e   0);.  }..  /* N
1837c 6f 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72  ormal (non-error
1837d 29 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 72  ) return. */.  r
1837e 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20  eturn;..  /* If 
1837f 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
18380 20 77 65 20 6a 75 6d 70 20 68 65 72 65 20 2a 2f   we jump here */
18381 0a 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72  .begin_table_err
18382 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  or:.  sqlite3_fr
18383 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74  ee(zName);.  ret
18384 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  urn;.}../*.** Th
18385 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  is macro is used
18386 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 77 6f 20   to compare two 
18387 73 74 72 69 6e 67 73 20 69 6e 20 61 20 63 61 73  strings in a cas
18388 65 2d 69 6e 73 65 6e 73 69 74 69 76 65 20 6d 61  e-insensitive ma
18389 6e 6e 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 73  nner..** It is s
1838a 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74  lightly faster t
1838b 68 61 6e 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  han calling sqli
1838c 74 65 33 53 74 72 49 43 6d 70 28 29 20 64 69 72  te3StrICmp() dir
1838d 65 63 74 6c 79 2c 20 62 75 74 0a 2a 2a 20 70 72  ectly, but.** pr
1838e 6f 64 75 63 65 73 20 6c 61 72 67 65 72 20 63 6f  oduces larger co
1838f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e  de..**.** WARNIN
18390 47 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73  G: This macro is
18391 20 6e 6f 74 20 63 6f 6d 70 61 74 69 62 6c 65 20   not compatible 
18392 77 69 74 68 20 74 68 65 20 73 74 72 63 6d 70 28  with the strcmp(
18393 29 20 66 61 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20  ) family. It.** 
18394 72 65 74 75 72 6e 73 20 74 72 75 65 20 69 66 20  returns true if 
18395 74 68 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20  the two strings 
18396 61 72 65 20 65 71 75 61 6c 2c 20 6f 74 68 65 72  are equal, other
18397 77 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23  wise false..*/.#
18398 64 65 66 69 6e 65 20 53 54 52 49 43 4d 50 28 78  define STRICMP(x
18399 2c 20 79 29 20 28 5c 0a 73 71 6c 69 74 65 33 55  , y) (\.sqlite3U
1839a 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e  pperToLower[*(un
1839b 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28 78  signed char *)(x
1839c 29 5d 3d 3d 20 20 20 5c 0a 73 71 6c 69 74 65 33  )]==   \.sqlite3
1839d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75  UpperToLower[*(u
1839e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28  nsigned char *)(
1839f 79 29 5d 20 20 20 20 20 5c 0a 26 26 20 73 71 6c  y)]     \.&& sql
183a0 69 74 65 33 53 74 72 49 43 6d 70 28 28 78 29 2b  ite3StrICmp((x)+
183a1 31 2c 28 79 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f  1,(y)+1)==0 )../
183a2 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 63  *.** Add a new c
183a3 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20 74 61 62  olumn to the tab
183a4 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69  le currently bei
183a5 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a  ng constructed..
183a6 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  **.** The parser
183a7 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
183a8 69 6e 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  ine once for eac
183a9 68 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61  h column declara
183aa 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 20 43 52 45  tion.** in a CRE
183ab 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
183ac 65 6e 74 2e 20 20 73 71 6c 69 74 65 33 53 74 61  ent.  sqlite3Sta
183ad 72 74 54 61 62 6c 65 28 29 20 67 65 74 73 20 63  rtTable() gets c
183ae 61 6c 6c 65 64 0a 2a 2a 20 66 69 72 73 74 20 74  alled.** first t
183af 6f 20 67 65 74 20 74 68 69 6e 67 73 20 67 6f 69  o get things goi
183b0 6e 67 2e 20 20 54 68 65 6e 20 74 68 69 73 20 72  ng.  Then this r
183b1 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
183b2 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 63 6f 6c   for each.** col
183b3 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  umn..*/.SQLITE_P
183b4 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
183b5 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72  te3AddColumn(Par
183b6 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
183b7 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 54 61 62  n *pName){.  Tab
183b8 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a  le *p;.  int i;.
183b9 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c    char *z;.  Col
183ba 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 69 66 28  umn *pCol;.  if(
183bb 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e   (p = pParse->pN
183bc 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65  ewTable)==0 ) re
183bd 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  turn;.  if( p->n
183be 43 6f 6c 2b 31 3e 53 51 4c 49 54 45 5f 4d 41 58  Col+1>SQLITE_MAX
183bf 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 73  _COLUMN ){.    s
183c0 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
183c1 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
183c2 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73 22 2c   columns on %s",
183c3 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20   p->zName);.    
183c4 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 20  return;.  }.  z 
183c5 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
183c6 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64  mToken(pParse->d
183c7 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b, pName);.  if(
183c8 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
183c9 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
183ca 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
183cb 69 66 28 20 53 54 52 49 43 4d 50 28 7a 2c 20 70  if( STRICMP(z, p
183cc 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29  ->aCol[i].zName)
183cd 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
183ce 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
183cf 2c 20 22 64 75 70 6c 69 63 61 74 65 20 63 6f 6c  , "duplicate col
183d0 75 6d 6e 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a  umn name: %s", z
183d1 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
183d2 5f 66 72 65 65 28 7a 29 3b 0a 20 20 20 20 20 20  _free(z);.      
183d3 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
183d4 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f 6c  }.  if( (p->nCol
183d5 20 26 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20 20   & 0x7)==0 ){.  
183d6 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b 0a    Column *aNew;.
183d7 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74      aNew = sqlit
183d8 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 50 61 72  e3DbRealloc(pPar
183d9 73 65 2d 3e 64 62 2c 70 2d 3e 61 43 6f 6c 2c 28  se->db,p->aCol,(
183da 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65 6f  p->nCol+8)*sizeo
183db 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a  f(p->aCol[0]));.
183dc 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20      if( aNew==0 
183dd 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
183de 5f 66 72 65 65 28 7a 29 3b 0a 20 20 20 20 20 20  _free(z);.      
183df 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
183e0 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77    p->aCol = aNew
183e1 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26  ;.  }.  pCol = &
183e2 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d  p->aCol[p->nCol]
183e3 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c  ;.  memset(pCol,
183e4 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43   0, sizeof(p->aC
183e5 6f 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d  ol[0]));.  pCol-
183e6 3e 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20  >zName = z;. .  
183e7 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
183e8 6f 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64  o type specified
183e9 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74  , columns have t
183ea 68 65 20 64 65 66 61 75 6c 74 20 61 66 66 69 6e  he default affin
183eb 69 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e  ity.  ** 'NONE'.
183ec 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74   If there is a t
183ed 79 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 74  ype specified, t
183ee 68 65 6e 20 73 71 6c 69 74 65 33 41 64 64 43 6f  hen sqlite3AddCo
183ef 6c 75 6d 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a  lumnType() will.
183f0 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e    ** be called n
183f1 65 78 74 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d  ext to set pCol-
183f2 3e 61 66 66 69 6e 69 74 79 20 63 6f 72 72 65 63  >affinity correc
183f3 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c  tly..  */.  pCol
183f4 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c  ->affinity = SQL
183f5 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
183f6 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a  p->nCol++;.}../*
183f7 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
183f8 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
183f9 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69  e parser while i
183fa 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a  n the middle of.
183fb 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45  ** parsing a CRE
183fc 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
183fd 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20 4e 55 4c  ent.  A "NOT NUL
183fe 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61  L" constraint ha
183ff 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65 6e 20 6f  s.** been seen o
18400 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 69  n a column.  Thi
18401 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
18402 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67 20  he notNull flag 
18403 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  on.** the column
18404 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
18405 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a   construction..*
18406 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18407 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
18408 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70  NotNull(Parse *p
18409 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72  Parse, int onErr
1840a 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  or){.  Table *p;
1840b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1840c 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  (p = pParse->pNe
1840d 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74  wTable)==0 ) ret
1840e 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43  urn;.  i = p->nC
1840f 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 3e 3d 30  ol-1;.  if( i>=0
18410 20 29 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f   ) p->aCol[i].no
18411 74 4e 75 6c 6c 20 3d 20 6f 6e 45 72 72 6f 72 3b  tNull = onError;
18412 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74  .}../*.** Scan t
18413 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6e  he column type n
18414 61 6d 65 20 7a 54 79 70 65 20 28 6c 65 6e 67 74  ame zType (lengt
18415 68 20 6e 54 79 70 65 29 20 61 6e 64 20 72 65 74  h nType) and ret
18416 75 72 6e 20 74 68 65 0a 2a 2a 20 61 73 73 6f 63  urn the.** assoc
18417 69 61 74 65 64 20 61 66 66 69 6e 69 74 79 20 74  iated affinity t
18418 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ype..**.** This 
18419 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20 63  routine does a c
1841a 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20  ase-independent 
1841b 73 65 61 72 63 68 20 6f 66 20 7a 54 79 70 65 20  search of zType 
1841c 66 6f 72 20 74 68 65 20 0a 2a 2a 20 73 75 62 73  for the .** subs
1841d 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 66 6f  trings in the fo
1841e 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 2e 20 49  llowing table. I
1841f 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62  f one of the sub
18420 73 74 72 69 6e 67 73 20 69 73 0a 2a 2a 20 66 6f  strings is.** fo
18421 75 6e 64 2c 20 74 68 65 20 63 6f 72 72 65 73 70  und, the corresp
18422 6f 6e 64 69 6e 67 20 61 66 66 69 6e 69 74 79 20  onding affinity 
18423 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
18424 7a 54 79 70 65 20 63 6f 6e 74 61 69 6e 73 0a 2a  zType contains.*
18425 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  * more than one 
18426 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67  of the substring
18427 73 2c 20 65 6e 74 72 69 65 73 20 74 6f 77 61 72  s, entries towar
18428 64 20 74 68 65 20 74 6f 70 20 6f 66 20 0a 2a 2a  d the top of .**
18429 20 74 68 65 20 74 61 62 6c 65 20 74 61 6b 65 20   the table take 
1842a 70 72 69 6f 72 69 74 79 2e 20 46 6f 72 20 65 78  priority. For ex
1842b 61 6d 70 6c 65 2c 20 69 66 20 7a 54 79 70 65 20  ample, if zType 
1842c 69 73 20 27 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a  is 'BLOBINT', .*
1842d 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  * SQLITE_AFF_INT
1842e 45 47 45 52 20 69 73 20 72 65 74 75 72 6e 65 64  EGER is returned
1842f 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 73 74 72 69 6e  ..**.** Substrin
18430 67 20 20 20 20 20 7c 20 41 66 66 69 6e 69 74 79  g     | Affinity
18431 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .** ------------
18432 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
18433 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 20  ----.** 'INT'   
18434 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
18435 46 46 5f 49 4e 54 45 47 45 52 0a 2a 2a 20 27 43  FF_INTEGER.** 'C
18436 48 41 52 27 20 20 20 20 20 20 20 20 7c 20 53 51  HAR'        | SQ
18437 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a  LITE_AFF_TEXT.**
18438 20 27 43 4c 4f 42 27 20 20 20 20 20 20 20 20 7c   'CLOB'        |
18439 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1843a 0a 2a 2a 20 27 54 45 58 54 27 20 20 20 20 20 20  .** 'TEXT'      
1843b 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54    | SQLITE_AFF_T
1843c 45 58 54 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 20  EXT.** 'BLOB'   
1843d 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
1843e 46 5f 4e 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c 27  F_NONE.** 'REAL'
1843f 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
18440 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c  _AFF_REAL.** 'FL
18441 4f 41 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c  OA'        | SQL
18442 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20  ITE_AFF_REAL.** 
18443 27 44 4f 55 42 27 20 20 20 20 20 20 20 20 7c 20  'DOUB'        | 
18444 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a  SQLITE_AFF_REAL.
18445 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66  **.** If none of
18446 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 20   the substrings 
18447 69 6e 20 74 68 65 20 61 62 6f 76 65 20 74 61 62  in the above tab
18448 6c 65 20 61 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a  le are found,.**
18449 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45   SQLITE_AFF_NUME
1844a 52 49 43 20 69 73 20 72 65 74 75 72 6e 65 64 2e  RIC is returned.
1844b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1844c 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 41  TE char sqlite3A
1844d 66 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73  ffinityType(cons
1844e 74 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b  t Token *pType){
1844f 0a 20 20 75 33 32 20 68 20 3d 20 30 3b 0a 20 20  .  u32 h = 0;.  
18450 63 68 61 72 20 61 66 66 20 3d 20 53 51 4c 49 54  char aff = SQLIT
18451 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20  E_AFF_NUMERIC;. 
18452 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
18453 63 68 61 72 20 2a 7a 49 6e 20 3d 20 70 54 79 70  char *zIn = pTyp
18454 65 2d 3e 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e  e->z;.  const un
18455 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 6e  signed char *zEn
18456 64 20 3d 20 26 70 54 79 70 65 2d 3e 7a 5b 70 54  d = &pType->z[pT
18457 79 70 65 2d 3e 6e 5d 3b 0a 0a 20 20 77 68 69 6c  ype->n];..  whil
18458 65 28 20 7a 49 6e 21 3d 7a 45 6e 64 20 29 7b 0a  e( zIn!=zEnd ){.
18459 20 20 20 20 68 20 3d 20 28 68 3c 3c 38 29 20 2b      h = (h<<8) +
1845a 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c   sqlite3UpperToL
1845b 6f 77 65 72 5b 2a 7a 49 6e 5d 3b 0a 20 20 20 20  ower[*zIn];.    
1845c 7a 49 6e 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68  zIn++;.    if( h
1845d 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 68  ==(('c'<<24)+('h
1845e 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b  '<<16)+('a'<<8)+
1845f 27 72 27 29 20 29 7b 20 20 20 20 20 20 20 20 20  'r') ){         
18460 20 20 20 20 2f 2a 20 43 48 41 52 20 2a 2f 0a 20      /* CHAR */. 
18461 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54       aff = SQLIT
18462 45 5f 41 46 46 5f 54 45 58 54 3b 20 0a 20 20 20  E_AFF_TEXT; .   
18463 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28   }else if( h==((
18464 27 63 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31  'c'<<24)+('l'<<1
18465 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29  6)+('o'<<8)+'b')
18466 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 43 4c 4f   ){       /* CLO
18467 42 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d  B */.      aff =
18468 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
18469 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1846a 68 3d 3d 28 28 27 74 27 3c 3c 32 34 29 2b 28 27  h==(('t'<<24)+('
1846b 65 27 3c 3c 31 36 29 2b 28 27 78 27 3c 3c 38 29  e'<<16)+('x'<<8)
1846c 2b 27 74 27 29 20 29 7b 20 20 20 20 20 20 20 2f  +'t') ){       /
1846d 2a 20 54 45 58 54 20 2a 2f 0a 20 20 20 20 20 20  * TEXT */.      
1846e 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
1846f 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65  _TEXT;.    }else
18470 20 69 66 28 20 68 3d 3d 28 28 27 62 27 3c 3c 32   if( h==(('b'<<2
18471 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f  4)+('l'<<16)+('o
18472 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20  '<<8)+'b')      
18473 20 20 20 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a 20      /* BLOB */. 
18474 20 20 20 20 20 20 20 26 26 20 28 61 66 66 3d 3d         && (aff==
18475 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
18476 49 43 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54  IC || aff==SQLIT
18477 45 5f 41 46 46 5f 52 45 41 4c 29 20 29 7b 0a 20  E_AFF_REAL) ){. 
18478 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54       aff = SQLIT
18479 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66 6e  E_AFF_NONE;.#ifn
1847a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1847b 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
1847c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d     }else if( h==
1847d 28 28 27 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c  (('r'<<24)+('e'<
1847e 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c  <16)+('a'<<8)+'l
1847f 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  ')          /* R
18480 45 41 4c 20 2a 2f 0a 20 20 20 20 20 20 20 20 26  EAL */.        &
18481 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46  & aff==SQLITE_AF
18482 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20  F_NUMERIC ){.   
18483 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
18484 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65  AFF_REAL;.    }e
18485 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 66 27  lse if( h==(('f'
18486 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b  <<24)+('l'<<16)+
18487 28 27 6f 27 3c 3c 38 29 2b 27 61 27 29 20 20 20  ('o'<<8)+'a')   
18488 20 20 20 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a         /* FLOA *
18489 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66  /.        && aff
1848a 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  ==SQLITE_AFF_NUM
1848b 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66  ERIC ){.      af
1848c 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52  f = SQLITE_AFF_R
1848d 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  EAL;.    }else i
1848e 66 28 20 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29  f( h==(('d'<<24)
1848f 2b 28 27 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c  +('o'<<16)+('u'<
18490 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20  <8)+'b')        
18491 20 20 2f 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20    /* DOUB */.   
18492 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c       && aff==SQL
18493 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20  ITE_AFF_NUMERIC 
18494 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  ){.      aff = S
18495 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a  QLITE_AFF_REAL;.
18496 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65  #endif.    }else
18497 20 69 66 28 20 28 68 26 30 78 30 30 46 46 46 46   if( (h&0x00FFFF
18498 46 46 29 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b  FF)==(('i'<<16)+
18499 28 27 6e 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b  ('n'<<8)+'t') ){
1849a 20 20 20 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20      /* INT */.  
1849b 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
1849c 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20  _AFF_INTEGER;.  
1849d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1849e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61  .  }..  return a
1849f 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ff;.}../*.** Thi
184a0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
184a1 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
184a2 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d  r while in the m
184a3 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73  iddle of.** pars
184a4 69 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42  ing a CREATE TAB
184a5 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  LE statement.  T
184a6 68 65 20 70 46 69 72 73 74 20 74 6f 6b 65 6e 20  he pFirst token 
184a7 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  is the first.** 
184a8 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 65 71  token in the seq
184a9 75 65 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20  uence of tokens 
184aa 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68  that describe th
184ab 65 20 74 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a  e type of the.**
184ac 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c   column currentl
184ad 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
184ae 74 69 6f 6e 2e 20 20 20 70 4c 61 73 74 20 69 73  tion.   pLast is
184af 20 74 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a   the last token.
184b0 2a 2a 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e  ** in the sequen
184b1 63 65 2e 20 20 55 73 65 20 74 68 69 73 20 69 6e  ce.  Use this in
184b2 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e  formation to con
184b3 73 74 72 75 63 74 20 61 20 73 74 72 69 6e 67 0a  struct a string.
184b4 2a 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ** that contains
184b5 20 74 68 65 20 74 79 70 65 6e 61 6d 65 20 6f 66   the typename of
184b6 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   the column and 
184b7 73 74 6f 72 65 20 74 68 61 74 20 73 74 72 69 6e  store that strin
184b8 67 0a 2a 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a  g.** in zType..*
184b9 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  / .SQLITE_PRIVAT
184ba 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
184bb 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73  dColumnType(Pars
184bc 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
184bd 20 2a 70 54 79 70 65 29 7b 0a 20 20 54 61 62 6c   *pType){.  Tabl
184be 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  e *p;.  int i;. 
184bf 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 0a   Column *pCol;..
184c0 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73    if( (p = pPars
184c1 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30  e->pNewTable)==0
184c2 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d   ) return;.  i =
184c3 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66   p->nCol-1;.  if
184c4 28 20 69 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a  ( i<0 ) return;.
184c5 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f    pCol = &p->aCo
184c6 6c 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f  l[i];.  sqlite3_
184c7 66 72 65 65 28 70 43 6f 6c 2d 3e 7a 54 79 70 65  free(pCol->zType
184c8 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65  );.  pCol->zType
184c9 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
184ca 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e  omToken(pParse->
184cb 64 62 2c 20 70 54 79 70 65 29 3b 0a 20 20 70 43  db, pType);.  pC
184cc 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73  ol->affinity = s
184cd 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79  qlite3AffinityTy
184ce 70 65 28 70 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a  pe(pType);.}../*
184cf 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69  .** The expressi
184d0 6f 6e 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  on is the defaul
184d1 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
184d2 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64  most recently ad
184d3 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66  ded column.** of
184d4 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65   the table curre
184d5 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
184d6 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44  ruction..**.** D
184d7 65 66 61 75 6c 74 20 76 61 6c 75 65 20 65 78 70  efault value exp
184d8 72 65 73 73 69 6f 6e 73 20 6d 75 73 74 20 62 65  ressions must be
184d9 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69 73   constant.  Rais
184da 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69  e an exception i
184db 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f 74  f this.** is not
184dc 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a   the case..**.**
184dd 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
184de 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
184df 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74  arser while in t
184e0 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20  he middle of.** 
184e1 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45  parsing a CREATE
184e2 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
184e3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
184e4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
184e5 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28  AddDefaultValue(
184e6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
184e7 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 54  xpr *pExpr){.  T
184e8 61 62 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d  able *p;.  Colum
184e9 6e 20 2a 70 43 6f 6c 3b 0a 20 20 69 66 28 20 28  n *pCol;.  if( (
184ea 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
184eb 54 61 62 6c 65 29 21 3d 30 20 29 7b 0a 20 20 20  Table)!=0 ){.   
184ec 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f   pCol = &(p->aCo
184ed 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a 20  l[p->nCol-1]);. 
184ee 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45     if( !sqlite3E
184ef 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46  xprIsConstantOrF
184f0 75 6e 63 74 69 6f 6e 28 70 45 78 70 72 29 20 29  unction(pExpr) )
184f1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
184f2 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
184f3 22 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f  "default value o
184f4 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69 73  f column [%s] is
184f5 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22 2c 0a   not constant",.
184f6 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
184f7 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73  zName);.    }els
184f8 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  e{.      Expr *p
184f9 43 6f 70 79 3b 0a 20 20 20 20 20 20 73 71 6c 69  Copy;.      sqli
184fa 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
184fb 2d 3e 64 62 3b 0a 20 20 20 20 20 20 73 71 6c 69  ->db;.      sqli
184fc 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 43  te3ExprDelete(pC
184fd 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20  ol->pDflt);.    
184fe 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20    pCol->pDflt = 
184ff 70 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 45  pCopy = sqlite3E
18500 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72  xprDup(db, pExpr
18501 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  );.      if( pCo
18502 70 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  py ){.        sq
18503 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64  lite3TokenCopy(d
18504 62 2c 20 26 70 43 6f 70 79 2d 3e 73 70 61 6e 2c  b, &pCopy->span,
18505 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a   &pExpr->span);.
18506 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
18507 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  }.  sqlite3ExprD
18508 65 6c 65 74 65 28 70 45 78 70 72 29 3b 0a 7d 0a  elete(pExpr);.}.
18509 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65  ./*.** Designate
1850a 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
1850b 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20   for the table. 
1850c 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74   pList is a list
1850d 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66   of names .** of
1850e 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f   columns that fo
1850f 72 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  rm the primary k
18510 65 79 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73  ey.  If pList is
18511 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a   NULL, then the.
18512 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  ** most recently
18513 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66   added column of
18514 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   the table is th
18515 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a  e primary key..*
18516 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e  *.** A table can
18517 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e   have at most on
18518 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20  e primary key.  
18519 49 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72  If the table alr
1851a 65 61 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72  eady has.** a pr
1851b 69 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74  imary key (and t
1851c 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e  his is the secon
1851d 64 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74  d primary key) t
1851e 68 65 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a  hen create an.**
1851f 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66   error..**.** If
18520 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
18521 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20   is on a single 
18522 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74  column whose dat
18523 61 74 79 70 65 20 69 73 20 49 4e 54 45 47 45 52  atype is INTEGER
18524 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c  ,.** then we wil
18525 6c 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 61  l try to use tha
18526 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20  t column as the 
18527 72 6f 77 69 64 2e 20 20 53 65 74 20 74 68 65 20  rowid.  Set the 
18528 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66  Table.iPKey.** f
18529 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c  ield of the tabl
1852a 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
1852b 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69  tion to be the i
1852c 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49  ndex of the.** I
1852d 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
1852e 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c  EY column.  Tabl
1852f 65 2e 69 50 4b 65 79 20 69 73 20 73 65 74 20 74  e.iPKey is set t
18530 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73  o -1 if there is
18531 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50  .** no INTEGER P
18532 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a  RIMARY KEY..**.*
18533 2a 20 49 66 20 74 68 65 20 6b 65 79 20 69 73 20  * If the key is 
18534 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50  not an INTEGER P
18535 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
18536 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 75 65   create a unique
18537 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68  .** index for th
18538 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78  e key.  No index
18539 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20   is created for 
1853a 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
1853b 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  KEYs..*/.SQLITE_
1853c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1853d 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
1853e 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  y(.  Parse *pPar
1853f 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
18540 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
18541 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
18542 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c   /* List of fiel
18543 64 20 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e  d names to be in
18544 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  dexed */.  int o
18545 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20  nError,      /* 
18546 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
18547 61 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e  a uniqueness con
18548 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61  flict */.  int a
18549 75 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20  utoInc,      /* 
1854a 54 72 75 65 20 69 66 20 74 68 65 20 41 55 54 4f  True if the AUTO
1854b 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72  INCREMENT keywor
1854c 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  d is present */.
1854d 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20    int sortOrder 
1854e 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f      /* SQLITE_SO
1854f 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53  _ASC or SQLITE_S
18550 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54  O_DESC */.){.  T
18551 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
18552 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
18553 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20    char *zType = 
18554 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20  0;.  int iCol = 
18555 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61  -1, i;.  if( pTa
18556 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  b==0 || IN_DECLA
18557 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70  RE_VTAB ) goto p
18558 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b  rimary_key_exit;
18559 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 68 61 73  .  if( pTab->has
1855a 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20 20 73  PrimKey ){.    s
1855b 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1855c 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 74  Parse, .      "t
1855d 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 20  able \"%s\" has 
1855e 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 72  more than one pr
1855f 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61 62  imary key", pTab
18560 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
18561 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65  to primary_key_e
18562 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d  xit;.  }.  pTab-
18563 3e 68 61 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b  >hasPrimKey = 1;
18564 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
18565 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54  ){.    iCol = pT
18566 61 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20  ab->nCol - 1;.  
18567 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f    pTab->aCol[iCo
18568 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31  l].isPrimKey = 1
18569 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  ;.  }else{.    f
1856a 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
1856b 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
1856c 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
1856d 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iCol<pTab->nCol;
1856e 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   iCol++){.      
1856f 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
18570 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d  ICmp(pList->a[i]
18571 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43  .zName, pTab->aC
18572 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d  ol[iCol].zName)=
18573 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
18574 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
18575 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
18576 66 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  f( iCol<pTab->nC
18577 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54  ol ){.        pT
18578 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69  ab->aCol[iCol].i
18579 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20  sPrimKey = 1;.  
1857a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1857b 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  if( pList->nExpr
1857c 3e 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a  >1 ) iCol = -1;.
1857d 20 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d    }.  if( iCol>=
1857e 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e  0 && iCol<pTab->
1857f 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70  nCol ){.    zTyp
18580 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  e = pTab->aCol[i
18581 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a  Col].zType;.  }.
18582 20 20 69 66 28 20 7a 54 79 70 65 20 26 26 20 73    if( zType && s
18583 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54  qlite3StrICmp(zT
18584 79 70 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d  ype, "INTEGER")=
18585 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 73 6f  =0.        && so
18586 72 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f  rtOrder==SQLITE_
18587 53 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 70 54  SO_ASC ){.    pT
18588 61 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c  ab->iPKey = iCol
18589 3b 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65 79 43  ;.    pTab->keyC
1858a 6f 6e 66 20 3d 20 6f 6e 45 72 72 6f 72 3b 0a 20  onf = onError;. 
1858b 20 20 20 70 54 61 62 2d 3e 61 75 74 6f 49 6e 63     pTab->autoInc
1858c 20 3d 20 61 75 74 6f 49 6e 63 3b 0a 20 20 7d 65   = autoInc;.  }e
1858d 6c 73 65 20 69 66 28 20 61 75 74 6f 49 6e 63 20  lse if( autoInc 
1858e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
1858f 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
18590 4d 45 4e 54 0a 20 20 20 20 73 71 6c 69 74 65 33  MENT.    sqlite3
18591 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
18592 20 22 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20   "AUTOINCREMENT 
18593 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20  is only allowed 
18594 6f 6e 20 61 6e 20 22 0a 20 20 20 20 20 20 20 22  on an ".       "
18595 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
18596 4b 45 59 22 29 3b 0a 23 65 6e 64 69 66 0a 20 20  KEY");.#endif.  
18597 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
18598 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50  e3CreateIndex(pP
18599 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 70  arse, 0, 0, 0, p
1859a 4c 69 73 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 30  List, onError, 0
1859b 2c 20 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c 20  , 0, sortOrder, 
1859c 30 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20  0);.    pList = 
1859d 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61 72 79 5f  0;.  }..primary_
1859e 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69  key_exit:.  sqli
1859f 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
185a0 65 28 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75  e(pList);.  retu
185a1 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  rn;.}../*.** Add
185a2 20 61 20 6e 65 77 20 43 48 45 43 4b 20 63 6f 6e   a new CHECK con
185a3 73 74 72 61 69 6e 74 20 74 6f 20 74 68 65 20 74  straint to the t
185a4 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
185a5 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
185a6 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
185a7 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
185a8 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61  3AddCheckConstra
185a9 69 6e 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  int(.  Parse *pP
185aa 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73  arse,    /* Pars
185ab 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
185ac 20 45 78 70 72 20 2a 70 43 68 65 63 6b 45 78 70   Expr *pCheckExp
185ad 72 20 20 2f 2a 20 54 68 65 20 63 68 65 63 6b 20  r  /* The check 
185ae 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b  expression */.){
185af 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
185b0 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54 61 62  OMIT_CHECK.  Tab
185b1 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73  le *pTab = pPars
185b2 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
185b3 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
185b4 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
185b5 70 54 61 62 20 26 26 20 21 49 4e 5f 44 45 43 4c  pTab && !IN_DECL
185b6 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20  ARE_VTAB ){.    
185b7 2f 2a 20 54 68 65 20 43 48 45 43 4b 20 65 78 70  /* The CHECK exp
185b8 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20  ression must be 
185b9 64 75 70 6c 69 63 61 74 65 64 20 73 6f 20 74 68  duplicated so th
185ba 61 74 20 74 6f 6b 65 6e 73 20 72 65 66 65 72 0a  at tokens refer.
185bb 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6c 6c 6f 63      ** to malloc
185bc 65 64 20 73 70 61 63 65 20 61 6e 64 20 6e 6f 74  ed space and not
185bd 20 74 68 65 20 28 65 70 68 65 6d 65 72 61 6c 29   the (ephemeral)
185be 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45   text of the CRE
185bf 41 54 45 20 54 41 42 4c 45 0a 20 20 20 20 2a 2a  ATE TABLE.    **
185c0 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
185c1 20 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 3d    pTab->pCheck =
185c2 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
185c3 64 62 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b  db, pTab->pCheck
185c4 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
185c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185c6 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
185c7 44 75 70 28 64 62 2c 20 70 43 68 65 63 6b 45 78  Dup(db, pCheckEx
185c8 70 72 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  pr));.  }.#endif
185c9 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
185ca 6c 65 74 65 28 70 43 68 65 63 6b 45 78 70 72 29  lete(pCheckExpr)
185cb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
185cc 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e  he collation fun
185cd 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73  ction of the mos
185ce 74 20 72 65 63 65 6e 74 6c 79 20 70 61 72 73 65  t recently parse
185cf 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a  d table column.*
185d0 2a 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65 71  * to the CollSeq
185d1 20 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54   given..*/.SQLIT
185d2 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
185d3 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65  qlite3AddCollate
185d4 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72  Type(Parse *pPar
185d5 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
185d6 7a 54 79 70 65 2c 20 69 6e 74 20 6e 54 79 70 65  zType, int nType
185d7 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
185d8 20 69 6e 74 20 69 3b 0a 0a 20 20 69 66 28 20 28   int i;..  if( (
185d9 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
185da 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75  Table)==0 ) retu
185db 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f  rn;.  i = p->nCo
185dc 6c 2d 31 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69  l-1;..  if( sqli
185dd 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
185de 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c 20  (pParse, zType, 
185df 6e 54 79 70 65 29 20 29 7b 0a 20 20 20 20 49 6e  nType) ){.    In
185e0 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 70  dex *pIdx;.    p
185e1 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 20  ->aCol[i].zColl 
185e2 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
185e3 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  up(pParse->db, z
185e4 54 79 70 65 2c 20 6e 54 79 70 65 29 3b 0a 20 20  Type, nType);.  
185e5 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63  .    /* If the c
185e6 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65  olumn is declare
185e7 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49  d as "<name> PRI
185e8 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45  MARY KEY COLLATE
185e9 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a   <type>",.    **
185ea 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d   then an index m
185eb 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65  ay have been cre
185ec 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c  ated on this col
185ed 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20  umn before the. 
185ee 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20     ** collation 
185ef 74 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20  type was added. 
185f0 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20  Correct this if 
185f1 69 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a  it is the case..
185f2 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70      */.    for(p
185f3 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70  Idx=p->pIndex; p
185f4 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
185f5 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73  pNext){.      as
185f6 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c  sert( pIdx->nCol
185f7 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
185f8 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  if( pIdx->aiColu
185f9 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20  mn[0]==i ){.    
185fa 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c      pIdx->azColl
185fb 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d  [0] = p->aCol[i]
185fc 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  .zColl;.      }.
185fd 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
185fe 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
185ff 20 72 65 74 75 72 6e 73 20 74 68 65 20 63 6f 6c   returns the col
18600 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
18601 66 6f 72 20 64 61 74 61 62 61 73 65 20 6e 61 74  for database nat
18602 69 76 65 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f  ive text.** enco
18603 64 69 6e 67 20 69 64 65 6e 74 69 66 69 65 64 20  ding identified 
18604 62 79 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4e  by the string zN
18605 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d  ame, length nNam
18606 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
18607 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74  requested collat
18608 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20  ion sequence is 
18609 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f  not available, o
1860a 72 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a  r not available.
1860b 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ** in the databa
1860c 73 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69  se native encodi
1860d 6e 67 2c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  ng, the collatio
1860e 6e 20 66 61 63 74 6f 72 79 20 69 73 20 69 6e 76  n factory is inv
1860f 6f 6b 65 64 20 74 6f 0a 2a 2a 20 72 65 71 75 65  oked to.** reque
18610 73 74 20 69 74 2e 20 49 66 20 74 68 65 20 63 6f  st it. If the co
18611 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20  llation factory 
18612 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6c 79 20  does not supply 
18613 73 75 63 68 20 61 20 73 65 71 75 65 6e 63 65 2c  such a sequence,
18614 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 71 75  .** and the sequ
18615 65 6e 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c  ence is availabl
18616 65 20 69 6e 20 61 6e 6f 74 68 65 72 20 74 65 78  e in another tex
18617 74 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e  t encoding, then
18618 20 74 68 61 74 20 69 73 0a 2a 2a 20 72 65 74 75   that is.** retu
18619 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a  rned instead..**
1861a 0a 2a 2a 20 49 66 20 6e 6f 20 76 65 72 73 69 6f  .** If no versio
1861b 6e 73 20 6f 66 20 74 68 65 20 72 65 71 75 65 73  ns of the reques
1861c 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 73  ted collations s
1861d 65 71 75 65 6e 63 65 20 61 72 65 20 61 76 61 69  equence are avai
1861e 6c 61 62 6c 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f  lable, or.** ano
1861f 74 68 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72  ther error occur
18620 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  s, NULL is retur
18621 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ned and an error
18622 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e   message written
18623 20 69 6e 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2e   into.** pParse.
18624 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
18625 69 6e 65 20 69 73 20 61 20 77 72 61 70 70 65 72  ine is a wrapper
18626 20 61 72 6f 75 6e 64 20 73 71 6c 69 74 65 33 46   around sqlite3F
18627 69 6e 64 43 6f 6c 6c 53 65 71 28 29 2e 20 20 54  indCollSeq().  T
18628 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69  his routine.** i
18629 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61  nvokes the colla
1862a 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 69 66 20  tion factory if 
1862b 74 68 65 20 6e 61 6d 65 64 20 63 6f 6c 6c 61 74  the named collat
1862c 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  ion cannot be fo
1862d 75 6e 64 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72  und.** and gener
1862e 61 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  ates an error me
1862f 73 73 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ssage..*/.SQLITE
18630 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
18631 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43   *sqlite3LocateC
18632 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50  ollSeq(Parse *pP
18633 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
18634 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61   *zName, int nNa
18635 6d 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  me){.  sqlite3 *
18636 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
18637 0a 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28  .  u8 enc = ENC(
18638 64 62 29 3b 0a 20 20 75 38 20 69 6e 69 74 62 75  db);.  u8 initbu
18639 73 79 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 62 75  sy = db->init.bu
1863a 73 79 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  sy;.  CollSeq *p
1863b 43 6f 6c 6c 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d  Coll;..  pColl =
1863c 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
1863d 53 65 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61  Seq(db, enc, zNa
1863e 6d 65 2c 20 6e 4e 61 6d 65 2c 20 69 6e 69 74 62  me, nName, initb
1863f 75 73 79 29 3b 0a 20 20 69 66 28 20 21 69 6e 69  usy);.  if( !ini
18640 74 62 75 73 79 20 26 26 20 28 21 70 43 6f 6c 6c  tbusy && (!pColl
18641 20 7c 7c 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70   || !pColl->xCmp
18642 29 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  ) ){.    pColl =
18643 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53   sqlite3GetCollS
18644 65 71 28 64 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e  eq(db, pColl, zN
18645 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
18646 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20   if( !pColl ){. 
18647 20 20 20 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30       if( nName<0
18648 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 4e 61 6d   ){.        nNam
18649 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65  e = strlen(zName
1864a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1864b 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1864c 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
1864d 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  h collation sequ
1864e 65 6e 63 65 3a 20 25 2e 2a 73 22 2c 20 6e 4e 61  ence: %.*s", nNa
1864f 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  me, zName);.    
18650 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20    pColl = 0;.   
18651 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
18652 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a   pColl;.}.../*.*
18653 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
18654 74 68 61 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d  that will increm
18655 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63  ent the schema c
18656 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ookie..**.** The
18657 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69   schema cookie i
18658 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
18659 69 6e 65 20 77 68 65 6e 20 74 68 65 20 73 63 68  ine when the sch
1865a 65 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64  ema for the.** d
1865b 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e  atabase changes.
1865c 20 20 41 66 74 65 72 20 65 61 63 68 20 73 63 68    After each sch
1865d 65 6d 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20  ema change, the 
1865e 63 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20  cookie value.** 
1865f 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61  changes.  When a
18660 20 70 72 6f 63 65 73 73 20 66 69 72 73 74 20 72   process first r
18661 65 61 64 73 20 74 68 65 20 73 63 68 65 6d 61 20  eads the schema 
18662 69 74 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a  it records the.*
18663 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65  * cookie.  There
18664 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65 72 20  after, whenever 
18665 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63 65 73  it goes to acces
18666 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a  s the database,.
18667 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74 68 65  ** it checks the
18668 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20   cookie to make 
18669 73 75 72 65 20 74 68 65 20 73 63 68 65 6d 61 20  sure the schema 
1866a 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a  has not changed.
1866b 2a 2a 20 73 69 6e 63 65 20 69 74 20 77 61 73 20  ** since it was 
1866c 6c 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  last read..**.**
1866d 20 54 68 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f   This plan is no
1866e 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c  t completely bul
1866f 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69  let-proof.  It i
18670 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a  s possible for.*
18671 2a 20 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20  * the schema to 
18672 63 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20  change multiple 
18673 74 69 6d 65 73 20 61 6e 64 20 66 6f 72 20 74 68  times and for th
18674 65 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a  e cookie to be.*
18675 2a 20 73 65 74 20 62 61 63 6b 20 74 6f 20 70 72  * set back to pr
18676 69 6f 72 20 76 61 6c 75 65 2e 20 20 42 75 74 20  ior value.  But 
18677 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61  schema changes a
18678 72 65 20 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a  re infrequent.**
18679 20 61 6e 64 20 74 68 65 20 70 72 6f 62 61 62 69   and the probabi
1867a 6c 69 74 79 20 6f 66 20 68 69 74 74 69 6e 67 20  lity of hitting 
1867b 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20  the same cookie 
1867c 76 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a  value is only.**
1867d 20 31 20 63 68 61 6e 63 65 20 69 6e 20 32 5e 33   1 chance in 2^3
1867e 32 2e 20 20 53 6f 20 77 65 27 72 65 20 73 61 66  2.  So we're saf
1867f 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c  e enough..*/.SQL
18680 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
18681 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
18682 6f 6b 69 65 28 73 71 6c 69 74 65 33 20 2a 64 62  okie(sqlite3 *db
18683 2c 20 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69  , Vdbe *v, int i
18684 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64  Db){.  sqlite3Vd
18685 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e  beAddOp(v, OP_In
18686 74 65 67 65 72 2c 20 64 62 2d 3e 61 44 62 5b 69  teger, db->aDb[i
18687 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68  Db].pSchema->sch
18688 65 6d 61 5f 63 6f 6f 6b 69 65 2b 31 2c 20 30 29  ema_cookie+1, 0)
18689 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
1868a 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 74 43 6f  ddOp(v, OP_SetCo
1868b 6f 6b 69 65 2c 20 69 44 62 2c 20 30 29 3b 0a 7d  okie, iDb, 0);.}
1868c 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 75 72 65 20  ../*.** Measure 
1868d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
1868e 61 72 61 63 74 65 72 73 20 6e 65 65 64 65 64 20  aracters needed 
1868f 74 6f 20 6f 75 74 70 75 74 20 74 68 65 20 67 69  to output the gi
18690 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65  ven.** identifie
18691 72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 72  r.  The number r
18692 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 73  eturned includes
18693 20 61 6e 79 20 71 75 6f 74 65 73 20 75 73 65 64   any quotes used
18694 0a 2a 2a 20 62 75 74 20 64 6f 65 73 20 6e 6f 74  .** but does not
18695 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 6c   include the nul
18696 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a  l terminator..**
18697 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74 65  .** The estimate
18698 20 69 73 20 63 6f 6e 73 65 72 76 61 74 69 76 65   is conservative
18699 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 20 6c  .  It might be l
1869a 61 72 67 65 72 20 74 68 61 74 20 77 68 61 74 20  arger that what 
1869b 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e 65 65  is.** really nee
1869c 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ded..*/.static i
1869d 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 63  nt identLength(c
1869e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
1869f 20 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28 6e 3d   int n;.  for(n=
186a0 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b 2b 29  0; *z; n++, z++)
186a1 7b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 22  {.    if( *z=='"
186a2 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a 20 20 7d 0a  ' ){ n++; }.  }.
186a3 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 32 3b 0a    return n + 2;.
186a4 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61  }../*.** Write a
186a5 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f 6e 74  n identifier ont
186a6 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
186a7 20 67 69 76 65 6e 20 73 74 72 69 6e 67 2e 20 20   given string.  
186a8 41 64 64 0a 2a 2a 20 71 75 6f 74 65 20 63 68 61  Add.** quote cha
186a9 72 61 63 74 65 72 73 20 61 73 20 6e 65 65 64 65  racters as neede
186aa 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
186ab 64 20 69 64 65 6e 74 50 75 74 28 63 68 61 72 20  d identPut(char 
186ac 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 2c 20 63  *z, int *pIdx, c
186ad 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 64 65 6e  har *zSignedIden
186ae 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  t){.  unsigned c
186af 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20 28 75  har *zIdent = (u
186b0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 53  nsigned char*)zS
186b1 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 20 69 6e  ignedIdent;.  in
186b2 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75 6f 74  t i, j, needQuot
186b3 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 78 3b 0a  e;.  i = *pIdx;.
186b4 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e    for(j=0; zIden
186b5 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  t[j]; j++){.    
186b6 69 66 28 20 21 69 73 61 6c 6e 75 6d 28 7a 49 64  if( !isalnum(zId
186b7 65 6e 74 5b 6a 5d 29 20 26 26 20 7a 49 64 65 6e  ent[j]) && zIden
186b8 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 62 72 65 61  t[j]!='_' ) brea
186b9 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 64 51 75 6f  k;.  }.  needQuo
186ba 74 65 20 3d 20 20 7a 49 64 65 6e 74 5b 6a 5d 21  te =  zIdent[j]!
186bb 3d 30 20 7c 7c 20 69 73 64 69 67 69 74 28 7a 49  =0 || isdigit(zI
186bc 64 65 6e 74 5b 30 5d 29 0a 20 20 20 20 20 20 20  dent[0]).       
186bd 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71             || sq
186be 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65  lite3KeywordCode
186bf 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f  (zIdent, j)!=TK_
186c0 49 44 3b 0a 20 20 69 66 28 20 6e 65 65 64 51 75  ID;.  if( needQu
186c1 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27  ote ) z[i++] = '
186c2 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a  "';.  for(j=0; z
186c3 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a  Ident[j]; j++){.
186c4 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64      z[i++] = zId
186c5 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20  ent[j];.    if( 
186c6 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29  zIdent[j]=='"' )
186c7 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20   z[i++] = '"';. 
186c8 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f   }.  if( needQuo
186c9 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22  te ) z[i++] = '"
186ca 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b 0a 20  ';.  z[i] = 0;. 
186cb 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a 0a 2f   *pIdx = i;.}../
186cc 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 20  *.** Generate a 
186cd 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
186ce 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72 69 61  tement appropria
186cf 74 65 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  te for the given
186d0 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65 6d 6f  .** table.  Memo
186d1 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 74  ry to hold the t
186d2 65 78 74 20 6f 66 20 74 68 65 20 73 74 61 74 65  ext of the state
186d3 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e 65 64  ment is obtained
186d4 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  .** from sqliteM
186d5 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 74  alloc() and must
186d6 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65   be freed by the
186d7 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
186d8 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  n..*/.static cha
186d9 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65 53 74  r *createTableSt
186da 6d 74 28 54 61 62 6c 65 20 2a 70 2c 20 69 6e 74  mt(Table *p, int
186db 20 69 73 54 65 6d 70 29 7b 0a 20 20 69 6e 74 20   isTemp){.  int 
186dc 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20  i, k, n;.  char 
186dd 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a  *zStmt;.  char *
186de 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a  zSep, *zSep2, *z
186df 45 6e 64 2c 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d  End, *z;.  Colum
186e0 6e 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30  n *pCol;.  n = 0
186e1 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70  ;.  for(pCol = p
186e2 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70  ->aCol, i=0; i<p
186e3 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  ->nCol; i++, pCo
186e4 6c 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69  l++){.    n += i
186e5 64 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d  dentLength(pCol-
186e6 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7a 20 3d  >zName);.    z =
186e7 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20   pCol->zType;.  
186e8 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20    if( z ){.     
186e9 20 6e 20 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 29   n += (strlen(z)
186ea 20 2b 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   + 1);.    }.  }
186eb 0a 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e  .  n += identLen
186ec 67 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  gth(p->zName);. 
186ed 20 69 66 28 20 6e 3c 35 30 20 29 7b 0a 20 20 20   if( n<50 ){.   
186ee 20 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 20 20   zSep = "";.    
186ef 7a 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20  zSep2 = ",";.   
186f0 20 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d   zEnd = ")";.  }
186f1 65 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d  else{.    zSep =
186f2 20 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65   "\n  ";.    zSe
186f3 70 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20  p2 = ",\n  ";.  
186f4 20 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a    zEnd = "\n)";.
186f5 20 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20    }.  n += 35 + 
186f6 36 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74  6*p->nCol;.  zSt
186f7 6d 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  mt = sqlite3_mal
186f8 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 69 66 28 20  loc( n );.  if( 
186f9 7a 53 74 6d 74 3d 3d 30 20 29 20 72 65 74 75 72  zStmt==0 ) retur
186fa 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  n 0;.  sqlite3_s
186fb 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74 6d 74  nprintf(n, zStmt
186fc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
186fd 20 20 20 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42      !OMIT_TEMPDB
186fe 26 26 69 73 54 65 6d 70 20 3f 20 22 43 52 45 41  &&isTemp ? "CREA
186ff 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 22 3a  TE TEMP TABLE ":
18700 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 22 29  "CREATE TABLE ")
18701 3b 0a 20 20 6b 20 3d 20 73 74 72 6c 65 6e 28 7a  ;.  k = strlen(z
18702 53 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50 75  Stmt);.  identPu
18703 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e  t(zStmt, &k, p->
18704 7a 4e 61 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 5b  zName);.  zStmt[
18705 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f  k++] = '(';.  fo
18706 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20  r(pCol=p->aCol, 
18707 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
18708 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20  i++, pCol++){.  
18709 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1870a 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b  tf(n-k, &zStmt[k
1870b 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20  ], zSep);.    k 
1870c 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 53 74 6d 74  += strlen(&zStmt
1870d 5b 6b 5d 29 3b 0a 20 20 20 20 7a 53 65 70 20 3d  [k]);.    zSep =
1870e 20 7a 53 65 70 32 3b 0a 20 20 20 20 69 64 65 6e   zSep2;.    iden
1870f 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20  tPut(zStmt, &k, 
18710 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pCol->zName);.  
18711 20 20 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c 2d    if( (z = pCol-
18712 3e 7a 54 79 70 65 29 21 3d 30 20 29 7b 0a 20 20  >zType)!=0 ){.  
18713 20 20 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d      zStmt[k++] =
18714 20 27 20 27 3b 0a 20 20 20 20 20 20 61 73 73 65   ' ';.      asse
18715 72 74 28 20 73 74 72 6c 65 6e 28 7a 29 2b 6b 2b  rt( strlen(z)+k+
18716 31 3c 3d 6e 20 29 3b 0a 20 20 20 20 20 20 73 71  1<=n );.      sq
18717 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
18718 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22  -k, &zStmt[k], "
18719 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 6b  %s", z);.      k
1871a 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20   += strlen(z);. 
1871b 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
1871c 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c  e3_snprintf(n-k,
1871d 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73 22   &zStmt[k], "%s"
1871e 2c 20 7a 45 6e 64 29 3b 0a 20 20 72 65 74 75 72  , zEnd);.  retur
1871f 6e 20 7a 53 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  n zStmt;.}../*.*
18720 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
18721 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 70 6f  s called to repo
18722 72 74 20 74 68 65 20 66 69 6e 61 6c 20 22 29 22  rt the final ")"
18723 20 74 68 61 74 20 74 65 72 6d 69 6e 61 74 65 73   that terminates
18724 0a 2a 2a 20 61 20 43 52 45 41 54 45 20 54 41 42  .** a CREATE TAB
18725 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  LE statement..**
18726 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 73 74  .** The table st
18727 72 75 63 74 75 72 65 20 74 68 61 74 20 6f 74 68  ructure that oth
18728 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e  er action routin
18729 65 73 20 68 61 76 65 20 62 65 65 6e 20 62 75 69  es have been bui
1872a 6c 64 69 6e 67 0a 2a 2a 20 69 73 20 61 64 64 65  lding.** is adde
1872b 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61  d to the interna
1872c 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2c 20 61  l hash tables, a
1872d 73 73 75 6d 69 6e 67 20 6e 6f 20 65 72 72 6f 72  ssuming no error
1872e 73 20 68 61 76 65 0a 2a 2a 20 6f 63 63 75 72 72  s have.** occurr
1872f 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6e 74  ed..**.** An ent
18730 72 79 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ry for the table
18731 20 69 73 20 6d 61 64 65 20 69 6e 20 74 68 65 20   is made in the 
18732 6d 61 73 74 65 72 20 74 61 62 6c 65 20 6f 6e 20  master table on 
18733 64 69 73 6b 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20  disk, unless.** 
18734 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 6f 72  this is a tempor
18735 61 72 79 20 74 61 62 6c 65 20 6f 72 20 64 62 2d  ary table or db-
18736 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 2e 20 20  >init.busy==1.  
18737 57 68 65 6e 20 64 62 2d 3e 69 6e 69 74 2e 62 75  When db->init.bu
18738 73 79 3d 3d 31 0a 2a 2a 20 69 74 20 6d 65 61 6e  sy==1.** it mean
18739 73 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67  s we are reading
1873a 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
1873b 65 72 20 74 61 62 6c 65 20 62 65 63 61 75 73 65  er table because
1873c 20 77 65 20 6a 75 73 74 0a 2a 2a 20 63 6f 6e 6e   we just.** conn
1873d 65 63 74 65 64 20 74 6f 20 74 68 65 20 64 61 74  ected to the dat
1873e 61 62 61 73 65 20 6f 72 20 62 65 63 61 75 73 65  abase or because
1873f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
18740 65 72 20 74 61 62 6c 65 20 68 61 73 0a 2a 2a 20  er table has.** 
18741 72 65 63 65 6e 74 6c 79 20 63 68 61 6e 67 65 64  recently changed
18742 2c 20 73 6f 20 74 68 65 20 65 6e 74 72 79 20 66  , so the entry f
18743 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20 61 6c  or this table al
18744 72 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 0a  ready exists in.
18745 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  ** the sqlite_ma
18746 73 74 65 72 20 74 61 62 6c 65 2e 20 20 57 65 20  ster table.  We 
18747 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 63  do not want to c
18748 72 65 61 74 65 20 69 74 20 61 67 61 69 6e 2e 0a  reate it again..
18749 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 53 65  **.** If the pSe
1874a 6c 65 63 74 20 61 72 67 75 6d 65 6e 74 20 69 73  lect argument is
1874b 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 6d 65   not NULL, it me
1874c 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 72 6f  ans that this ro
1874d 75 74 69 6e 65 0a 2a 2a 20 77 61 73 20 63 61 6c  utine.** was cal
1874e 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  led to create a 
1874f 74 61 62 6c 65 20 67 65 6e 65 72 61 74 65 64 20  table generated 
18750 66 72 6f 6d 20 61 20 0a 2a 2a 20 22 43 52 45 41  from a .** "CREA
18751 54 45 20 54 41 42 4c 45 20 2e 2e 2e 20 41 53 20  TE TABLE ... AS 
18752 53 45 4c 45 43 54 20 2e 2e 2e 22 20 73 74 61 74  SELECT ..." stat
18753 65 6d 65 6e 74 2e 20 20 54 68 65 20 63 6f 6c 75  ement.  The colu
18754 6d 6e 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20 74  mn names of.** t
18755 68 65 20 6e 65 77 20 74 61 62 6c 65 20 77 69 6c  he new table wil
18756 6c 20 6d 61 74 63 68 20 74 68 65 20 72 65 73 75  l match the resu
18757 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45  lt set of the SE
18758 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  LECT..*/.SQLITE_
18759 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1875a 69 74 65 33 45 6e 64 54 61 62 6c 65 28 0a 20 20  ite3EndTable(.  
1875b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1875c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
1875d 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f   context */.  To
1875e 6b 65 6e 20 2a 70 43 6f 6e 73 2c 20 20 20 20 20  ken *pCons,     
1875f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 27 2c 27        /* The ','
18760 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74 68 65   token after the
18761 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 64 65 66   last column def
18762 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  n. */.  Token *p
18763 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  End,            
18764 2f 2a 20 54 68 65 20 66 69 6e 61 6c 20 27 29 27  /* The final ')'
18765 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 43 52   token in the CR
18766 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20  EATE TABLE */.  
18767 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
18768 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63          /* Selec
18769 74 20 66 72 6f 6d 20 61 20 22 43 52 45 41 54 45  t from a "CREATE
1876a 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 22 20   ... AS SELECT" 
1876b 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
1876c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1876d 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1876e 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 66 28 20  int iDb;..  if( 
1876f 28 70 45 6e 64 3d 3d 30 20 26 26 20 70 53 65 6c  (pEnd==0 && pSel
18770 65 63 74 3d 3d 30 29 20 7c 7c 20 70 50 61 72 73  ect==0) || pPars
18771 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
18772 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 7b 0a  allocFailed ) {.
18773 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
18774 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e    p = pParse->pN
18775 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
18776 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  ==0 ) return;.. 
18777 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 69 6e   assert( !db->in
18778 69 74 2e 62 75 73 79 20 7c 7c 20 21 70 53 65 6c  it.busy || !pSel
18779 65 63 74 20 29 3b 0a 0a 20 20 69 44 62 20 3d 20  ect );..  iDb = 
1877a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1877b 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68  ndex(db, p->pSch
1877c 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ema);..#ifndef S
1877d 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
1877e 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61  .  /* Resolve na
1877f 6d 65 73 20 69 6e 20 61 6c 6c 20 43 48 45 43 4b  mes in all CHECK
18780 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 70 72   constraint expr
18781 65 73 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  essions..  */.  
18782 69 66 28 20 70 2d 3e 70 43 68 65 63 6b 20 29 7b  if( p->pCheck ){
18783 0a 20 20 20 20 53 72 63 4c 69 73 74 20 73 53 72  .    SrcList sSr
18784 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
18785 20 20 20 20 20 2f 2a 20 46 61 6b 65 20 53 72 63       /* Fake Src
18786 4c 69 73 74 20 66 6f 72 20 70 50 61 72 73 65 2d  List for pParse-
18787 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20 20  >pNewTable */.  
18788 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
18789 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C;              
1878a 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
1878b 74 20 66 6f 72 20 70 50 61 72 73 65 2d 3e 70 4e  t for pParse->pN
1878c 65 77 54 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20  ewTable */..    
1878d 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
1878e 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
1878f 20 20 6d 65 6d 73 65 74 28 26 73 53 72 63 2c 20    memset(&sSrc, 
18790 30 2c 20 73 69 7a 65 6f 66 28 73 53 72 63 29 29  0, sizeof(sSrc))
18791 3b 0a 20 20 20 20 73 53 72 63 2e 6e 53 72 63 20  ;.    sSrc.nSrc 
18792 3d 20 31 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b  = 1;.    sSrc.a[
18793 30 5d 2e 7a 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e  0].zName = p->zN
18794 61 6d 65 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b  ame;.    sSrc.a[
18795 30 5d 2e 70 54 61 62 20 3d 20 70 3b 0a 20 20 20  0].pTab = p;.   
18796 20 73 53 72 63 2e 61 5b 30 5d 2e 69 43 75 72 73   sSrc.a[0].iCurs
18797 6f 72 20 3d 20 2d 31 3b 0a 20 20 20 20 73 4e 43  or = -1;.    sNC
18798 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
18799 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69  ;.    sNC.pSrcLi
1879a 73 74 20 3d 20 26 73 53 72 63 3b 0a 20 20 20 20  st = &sSrc;.    
1879b 73 4e 43 2e 69 73 43 68 65 63 6b 20 3d 20 31 3b  sNC.isCheck = 1;
1879c 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1879d 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73  ExprResolveNames
1879e 28 26 73 4e 43 2c 20 70 2d 3e 70 43 68 65 63 6b  (&sNC, p->pCheck
1879f 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
187a0 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  n;.    }.  }.#en
187a1 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
187a2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
187a3 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  K) */..  /* If t
187a4 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  he db->init.busy
187a5 20 69 73 20 31 20 69 74 20 6d 65 61 6e 73 20 77   is 1 it means w
187a6 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68  e are reading th
187a7 65 20 53 51 4c 20 6f 66 66 20 74 68 65 0a 20 20  e SQL off the.  
187a8 2a 2a 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65  ** "sqlite_maste
187a9 72 22 20 6f 72 20 22 73 71 6c 69 74 65 5f 74 65  r" or "sqlite_te
187aa 6d 70 5f 6d 61 73 74 65 72 22 20 74 61 62 6c 65  mp_master" table
187ab 20 6f 6e 20 74 68 65 20 64 69 73 6b 2e 0a 20 20   on the disk..  
187ac 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74 20 77 72 69  ** So do not wri
187ad 74 65 20 74 6f 20 74 68 65 20 64 69 73 6b 20 61  te to the disk a
187ae 67 61 69 6e 2e 20 20 45 78 74 72 61 63 74 20 74  gain.  Extract t
187af 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  he root page num
187b0 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65  ber.  ** for the
187b1 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20   table from the 
187b2 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
187b3 20 66 69 65 6c 64 2e 20 20 28 54 68 65 20 70 61   field.  (The pa
187b4 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 73  ge number.  ** s
187b5 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20  hould have been 
187b6 70 75 74 20 74 68 65 72 65 20 62 79 20 74 68 65  put there by the
187b7 20 73 71 6c 69 74 65 4f 70 65 6e 43 62 20 72 6f   sqliteOpenCb ro
187b8 75 74 69 6e 65 2e 29 0a 20 20 2a 2f 0a 20 20 69  utine.).  */.  i
187b9 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
187ba 20 29 7b 0a 20 20 20 20 70 2d 3e 74 6e 75 6d 20   ){.    p->tnum 
187bb 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e  = db->init.newTn
187bc 75 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  um;.  }..  /* If
187bd 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e   not initializin
187be 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  g, then create a
187bf 20 72 65 63 6f 72 64 20 66 6f 72 20 74 68 65 20   record for the 
187c0 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69  new table.  ** i
187c1 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53  n the SQLITE_MAS
187c2 54 45 52 20 74 61 62 6c 65 20 6f 66 20 74 68 65  TER table of the
187c3 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20   database.  The 
187c4 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 0a 20 20  record number.  
187c5 2a 2a 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74  ** for the new t
187c6 61 62 6c 65 20 65 6e 74 72 79 20 73 68 6f 75 6c  able entry shoul
187c7 64 20 61 6c 72 65 61 64 79 20 62 65 20 6f 6e 20  d already be on 
187c8 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 2a 2a 0a  the stack..  **.
187c9 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20    ** If this is 
187ca 61 20 54 45 4d 50 4f 52 41 52 59 20 74 61 62 6c  a TEMPORARY tabl
187cb 65 2c 20 77 72 69 74 65 20 74 68 65 20 65 6e 74  e, write the ent
187cc 72 79 20 69 6e 74 6f 20 74 68 65 20 61 75 78 69  ry into the auxi
187cd 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69 6c 65 20  liary.  ** file 
187ce 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 74 6f 20  instead of into 
187cf 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
187d0 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69  e file..  */.  i
187d1 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
187d2 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a  y ){.    int n;.
187d3 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20      Vdbe *v;.   
187d4 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20   char *zType;   
187d5 20 2f 2a 20 22 76 69 65 77 22 20 6f 72 20 22 74   /* "view" or "t
187d6 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20 63 68 61  able" */.    cha
187d7 72 20 2a 7a 54 79 70 65 32 3b 20 20 20 2f 2a 20  r *zType2;   /* 
187d8 22 56 49 45 57 22 20 6f 72 20 22 54 41 42 4c 45  "VIEW" or "TABLE
187d9 22 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  " */.    char *z
187da 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54 65 78 74  Stmt;    /* Text
187db 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54   of the CREATE T
187dc 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20 56  ABLE or CREATE V
187dd 49 45 57 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  IEW statement */
187de 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65  ..    v = sqlite
187df 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
187e0 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29  ;.    if( v==0 )
187e1 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 73 71   return;..    sq
187e2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
187e3 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30  , OP_Close, 0, 0
187e4 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74  );..    /* Creat
187e5 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 66  e the rootpage f
187e6 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  or the new table
187e7 20 61 6e 64 20 70 75 73 68 20 69 74 20 6f 6e 74   and push it ont
187e8 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 20  o the stack..   
187e9 20 2a 2a 20 41 20 76 69 65 77 20 68 61 73 20 6e   ** A view has n
187ea 6f 20 72 6f 6f 74 70 61 67 65 2c 20 73 6f 20 6a  o rootpage, so j
187eb 75 73 74 20 70 75 73 68 20 61 20 7a 65 72 6f 20  ust push a zero 
187ec 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 66  onto the stack f
187ed 6f 72 0a 20 20 20 20 2a 2a 20 76 69 65 77 73 2e  or.    ** views.
187ee 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 7a 54 79    Initialize zTy
187ef 70 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  pe at the same t
187f0 69 6d 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ime..    */.    
187f1 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d 3d  if( p->pSelect==
187f2 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  0 ){.      /* A 
187f3 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20 2a 2f  regular table */
187f4 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22  .      zType = "
187f5 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a 54  table";.      zT
187f6 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22 3b 0a  ype2 = "TABLE";.
187f7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
187f8 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 6c  MIT_VIEW.    }el
187f9 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 76  se{.      /* A v
187fa 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79  iew */.      zTy
187fb 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 20  pe = "view";.   
187fc 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56 49 45     zType2 = "VIE
187fd 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  W";.#endif.    }
187fe 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
187ff 20 69 73 20 61 20 43 52 45 41 54 45 20 54 41 42   is a CREATE TAB
18800 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43 54 20  LE xx AS SELECT 
18801 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68 65  ..., execute the
18802 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 73   SELECT.    ** s
18803 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 75  tatement to popu
18804 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74 61 62  late the new tab
18805 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 67  le. The root-pag
18806 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  e number for the
18807 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 6c  .    ** new tabl
18808 65 20 69 73 20 6f 6e 20 74 68 65 20 74 6f 70 20  e is on the top 
18809 6f 66 20 74 68 65 20 76 64 62 65 20 73 74 61 63  of the vdbe stac
1880a 6b 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  k..    **.    **
1880b 20 4f 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54   Once the SELECT
1880c 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20   has been coded 
1880d 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  by sqlite3Select
1880e 28 29 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20  (), it is in a. 
1880f 20 20 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73     ** suitable s
18810 74 61 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f  tate to query fo
18811 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  r the column nam
18812 65 73 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20  es and types to 
18813 62 65 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62  be used.    ** b
18814 79 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e  y the new table.
18815 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41  .    **.    ** A
18816 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 77 72   shared-cache wr
18817 69 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20  ite-lock is not 
18818 72 65 71 75 69 72 65 64 20 74 6f 20 77 72 69 74  required to writ
18819 65 20 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62  e to the new tab
1881a 6c 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20  le,.    ** as a 
1881b 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74  schema-lock must
1881c 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
1881d 65 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63  en obtained to c
1881e 72 65 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a  reate it. Since.
1881f 20 20 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d      ** a schema-
18820 6c 6f 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c  lock excludes al
18821 6c 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  l other database
18822 20 75 73 65 72 73 2c 20 74 68 65 20 77 72 69 74   users, the writ
18823 65 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20  e-lock would.   
18824 20 2a 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74   ** be redundant
18825 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
18826 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
18827 20 20 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62    Table *pSelTab
18828 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
18829 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44  dbeAddOp(v, OP_D
1882a 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  up, 0, 0);.     
1882b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1882c 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  p(v, OP_Integer,
1882d 20 69 44 62 2c 20 30 29 3b 0a 20 20 20 20 20 20   iDb, 0);.      
1882e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1882f 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  (v, OP_OpenWrite
18830 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 70  , 1, 0);.      p
18831 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 32 3b  Parse->nTab = 2;
18832 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
18833 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65  lect(pParse, pSe
18834 6c 65 63 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c  lect, SRT_Table,
18835 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b   1, 0, 0, 0, 0);
18836 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
18837 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c  beAddOp(v, OP_Cl
18838 6f 73 65 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  ose, 1, 0);.    
18839 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
1883a 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rr==0 ){.       
1883b 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74   pSelTab = sqlit
1883c 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
1883d 65 63 74 28 70 50 61 72 73 65 2c 20 30 2c 20 70  ect(pParse, 0, p
1883e 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20  Select);.       
1883f 20 69 66 28 20 70 53 65 6c 54 61 62 3d 3d 30 20   if( pSelTab==0 
18840 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  ) return;.      
18841 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f    assert( p->aCo
18842 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  l==0 );.        
18843 70 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61  p->nCol = pSelTa
18844 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20  b->nCol;.       
18845 20 70 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54   p->aCol = pSelT
18846 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20  ab->aCol;.      
18847 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20    pSelTab->nCol 
18848 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 53 65  = 0;.        pSe
18849 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a  lTab->aCol = 0;.
1884a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1884b 65 6c 65 74 65 54 61 62 6c 65 28 70 53 65 6c 54  eleteTable(pSelT
1884c 61 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ab);.      }.   
1884d 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75   }..    /* Compu
1884e 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  te the complete 
1884f 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41  text of the CREA
18850 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  TE statement */.
18851 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 20      if( pSelect 
18852 29 7b 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d  ){.      zStmt =
18853 20 63 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74   createTableStmt
18854 28 70 2c 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d  (p, p->pSchema==
18855 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
18856 6d 61 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ma);.    }else{.
18857 20 20 20 20 20 20 6e 20 3d 20 70 45 6e 64 2d 3e        n = pEnd->
18858 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  z - pParse->sNam
18859 65 54 6f 6b 65 6e 2e 7a 20 2b 20 31 3b 0a 20 20  eToken.z + 1;.  
1885a 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69      zStmt = sqli
1885b 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a  te3MPrintf(db, .
1885c 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54            "CREAT
1885d 45 20 25 73 20 25 2e 2a 73 22 2c 20 7a 54 79 70  E %s %.*s", zTyp
1885e 65 32 2c 20 6e 2c 20 70 50 61 72 73 65 2d 3e 73  e2, n, pParse->s
1885f 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20 20  NameToken.z.    
18860 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20    );.    }..    
18861 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 68  /* A slot for th
18862 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c 72  e record has alr
18863 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
18864 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ted in the .    
18865 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  ** SQLITE_MASTER
18866 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73 74   table.  We just
18867 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65 20   need to update 
18868 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20 61  that slot with a
18869 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e  ll.    ** the in
1886a 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76 65 20  formation we've 
1886b 63 6f 6c 6c 65 63 74 65 64 2e 20 20 54 68 65 20  collected.  The 
1886c 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 70 72  rowid for the pr
1886d 65 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a  eallocated.    *
1886e 2a 20 73 6c 6f 74 20 69 73 20 74 68 65 20 32 6e  * slot is the 2n
1886f 64 20 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74  d item on the st
18870 61 63 6b 2e 20 20 54 68 65 20 74 6f 70 20 6f 66  ack.  The top of
18871 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 74 68   the stack is th
18872 65 0a 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61  e.    ** root pa
18873 67 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74  ge for the new t
18874 61 62 6c 65 20 28 6f 72 20 61 20 30 20 69 66 20  able (or a 0 if 
18875 74 68 69 73 20 69 73 20 61 20 76 69 65 77 29 2e  this is a view).
18876 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
18877 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
18878 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50  Parse,.      "UP
18879 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20 20 20  DATE %Q.%s ".   
1887a 20 20 20 20 20 20 22 53 45 54 20 74 79 70 65 3d        "SET type=
1887b 27 25 73 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74  '%s', name=%Q, t
1887c 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74  bl_name=%Q, root
1887d 70 61 67 65 3d 23 30 2c 20 73 71 6c 3d 25 51 20  page=#0, sql=%Q 
1887e 22 0a 20 20 20 20 20 20 20 22 57 48 45 52 45 20  ".       "WHERE 
1887f 72 6f 77 69 64 3d 23 31 22 2c 0a 20 20 20 20 20  rowid=#1",.     
18880 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
18881 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ame, SCHEMA_TABL
18882 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20 7a 54  E(iDb),.      zT
18883 79 70 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e  ype,.      p->zN
18884 61 6d 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e  ame,.      p->zN
18885 61 6d 65 2c 0a 20 20 20 20 20 20 7a 53 74 6d 74  ame,.      zStmt
18886 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69  .    );.    sqli
18887 74 65 33 5f 66 72 65 65 28 7a 53 74 6d 74 29 3b  te3_free(zStmt);
18888 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e  .    sqlite3Chan
18889 67 65 43 6f 6f 6b 69 65 28 64 62 2c 20 76 2c 20  geCookie(db, v, 
1888a 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  iDb);..#ifndef S
1888b 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
1888c 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20  NCREMENT.    /* 
1888d 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
1888e 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74  we need to creat
1888f 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65 71 75  e an sqlite_sequ
18890 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72 0a 20  ence table for. 
18891 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20 74 72     ** keeping tr
18892 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63 72 65  ack of autoincre
18893 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20 20 2a  ment keys..    *
18894 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 75 74  /.    if( p->aut
18895 6f 49 6e 63 20 29 7b 0a 20 20 20 20 20 20 44 62  oInc ){.      Db
18896 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
18897 5b 69 44 62 5d 3b 0a 20 20 20 20 20 20 69 66 28  [iDb];.      if(
18898 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70   pDb->pSchema->p
18899 53 65 71 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20  SeqTab==0 ){.   
1889a 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74       sqlite3Nest
1889b 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
1889c 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54            "CREAT
1889d 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c 69 74  E TABLE %Q.sqlit
1889e 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d 65 2c  e_sequence(name,
1889f 73 65 71 29 22 2c 0a 20 20 20 20 20 20 20 20 20  seq)",.         
188a0 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20   pDb->zName.    
188a1 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20      );.      }. 
188a2 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
188a3 20 2f 2a 20 52 65 70 61 72 73 65 20 65 76 65 72   /* Reparse ever
188a4 79 74 68 69 6e 67 20 74 6f 20 75 70 64 61 74 65  ything to update
188a5 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 64 61   our internal da
188a6 74 61 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f  ta structures */
188a7 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
188a8 4f 70 33 28 76 2c 20 4f 50 5f 50 61 72 73 65 53  Op3(v, OP_ParseS
188a9 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 0a 20  chema, iDb, 0,. 
188aa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50         sqlite3MP
188ab 72 69 6e 74 66 28 64 62 2c 20 22 74 62 6c 5f 6e  rintf(db, "tbl_n
188ac 61 6d 65 3d 27 25 71 27 22 2c 70 2d 3e 7a 4e 61  ame='%q'",p->zNa
188ad 6d 65 29 2c 20 50 33 5f 44 59 4e 41 4d 49 43 29  me), P3_DYNAMIC)
188ae 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64  ;.  }...  /* Add
188af 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 74 68   the table to th
188b0 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72  e in-memory repr
188b1 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
188b2 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  e database..  */
188b3 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e  .  if( db->init.
188b4 62 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e  busy && pParse->
188b5 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 54  nErr==0 ){.    T
188b6 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20  able *pOld;.    
188b7 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 0a 20 20  FKey *pFKey; .  
188b8 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
188b9 61 20 3d 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a  a = p->pSchema;.
188ba 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74      pOld = sqlit
188bb 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 53  e3HashInsert(&pS
188bc 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
188bd 70 2d 3e 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e  p->zName, strlen
188be 28 70 2d 3e 7a 4e 61 6d 65 29 2b 31 2c 70 29 3b  (p->zName)+1,p);
188bf 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20 29 7b  .    if( pOld ){
188c0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
188c1 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d 61  ==pOld );  /* Ma
188c2 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66  lloc must have f
188c3 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48 61 73  ailed inside Has
188c4 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20 20  hInsert() */.   
188c5 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
188c6 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  led = 1;.      r
188c7 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 23 69 66  eturn;.    }.#if
188c8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
188c9 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20  _FOREIGN_KEY.   
188ca 20 66 6f 72 28 70 46 4b 65 79 3d 70 2d 3e 70 46   for(pFKey=p->pF
188cb 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65  Key; pFKey; pFKe
188cc 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72  y=pFKey->pNextFr
188cd 6f 6d 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20  om){.      void 
188ce 2a 64 61 74 61 3b 0a 20 20 20 20 20 20 69 6e 74  *data;.      int
188cf 20 6e 54 6f 20 3d 20 73 74 72 6c 65 6e 28 70 46   nTo = strlen(pF
188d0 4b 65 79 2d 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20  Key->zTo) + 1;. 
188d1 20 20 20 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78       pFKey->pNex
188d2 74 54 6f 20 3d 20 73 71 6c 69 74 65 33 48 61 73  tTo = sqlite3Has
188d3 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e  hFind(&pSchema->
188d4 61 46 4b 65 79 2c 20 70 46 4b 65 79 2d 3e 7a 54  aFKey, pFKey->zT
188d5 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20 20 20 20 64  o, nTo);.      d
188d6 61 74 61 20 3d 20 73 71 6c 69 74 65 33 48 61 73  ata = sqlite3Has
188d7 68 49 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61  hInsert(&pSchema
188d8 2d 3e 61 46 4b 65 79 2c 20 70 46 4b 65 79 2d 3e  ->aFKey, pFKey->
188d9 7a 54 6f 2c 20 6e 54 6f 2c 20 70 46 4b 65 79 29  zTo, nTo, pFKey)
188da 3b 0a 20 20 20 20 20 20 69 66 28 20 64 61 74 61  ;.      if( data
188db 3d 3d 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 20  ==(void *)pFKey 
188dc 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d  ){.        db->m
188dd 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
188de 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
188df 65 6e 64 69 66 0a 20 20 20 20 70 50 61 72 73 65  endif.    pParse
188e0 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b  ->pNewTable = 0;
188e1 0a 20 20 20 20 64 62 2d 3e 6e 54 61 62 6c 65 2b  .    db->nTable+
188e2 2b 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  +;.    db->flags
188e3 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
188e4 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66 6e 64  nChanges;..#ifnd
188e5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
188e6 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 20 69 66  LTERTABLE.    if
188e7 28 20 21 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b  ( !p->pSelect ){
188e8 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
188e9 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73  r *zName = (cons
188ea 74 20 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d  t char *)pParse-
188eb 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a 20  >sNameToken.z;. 
188ec 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a       int nName;.
188ed 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
188ee 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e 73 20  Select && pCons 
188ef 26 26 20 70 45 6e 64 20 29 3b 0a 20 20 20 20 20  && pEnd );.     
188f0 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d 30   if( pCons->z==0
188f1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6e   ){.        pCon
188f2 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20  s = pEnd;.      
188f3 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20  }.      nName = 
188f4 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 43  (const char *)pC
188f5 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e 61 6d 65 3b 0a  ons->z - zName;.
188f6 20 20 20 20 20 20 70 2d 3e 61 64 64 43 6f 6c 4f        p->addColO
188f7 66 66 73 65 74 20 3d 20 31 33 20 2b 20 73 71 6c  ffset = 13 + sql
188f8 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
188f9 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
188fa 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
188fb 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
188fc 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a  E_OMIT_VIEW./*.*
188fd 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
188fe 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
188ff 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61  in order to crea
18900 74 65 20 61 20 6e 65 77 20 56 49 45 57 0a 2a 2f  te a new VIEW.*/
18901 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18902 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
18903 74 65 56 69 65 77 28 0a 20 20 50 61 72 73 65 20  teView(.  Parse 
18904 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
18905 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
18906 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
18907 70 42 65 67 69 6e 2c 20 20 20 20 20 2f 2a 20 54  pBegin,     /* T
18908 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20  he CREATE token 
18909 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 65 20  that begins the 
1890a 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 54  statement */.  T
1890b 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
1890c 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74    /* The token t
1890d 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61  hat holds the na
1890e 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a  me of the view *
1890f 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
18910 32 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f  2,     /* The to
18911 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ken that holds t
18912 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76  he name of the v
18913 69 65 77 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  iew */.  Select 
18914 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 41  *pSelect,   /* A
18915 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
18916 74 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f  t that will beco
18917 6d 65 20 74 68 65 20 6e 65 77 20 76 69 65 77 20  me the new view 
18918 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c  */.  int isTemp,
18919 20 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20          /* TRUE 
1891a 66 6f 72 20 61 20 54 45 4d 50 4f 52 41 52 59 20  for a TEMPORARY 
1891b 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  view */.  int no
1891c 45 72 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Err          /* 
1891d 53 75 70 70 72 65 73 73 20 65 72 72 6f 72 20 6d  Suppress error m
1891e 65 73 73 61 67 65 73 20 69 66 20 56 49 45 57 20  essages if VIEW 
1891f 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a  already exists *
18920 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  /.){.  Table *p;
18921 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 63 6f 6e 73  .  int n;.  cons
18922 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
18923 2a 7a 3b 0a 20 20 54 6f 6b 65 6e 20 73 45 6e 64  *z;.  Token sEnd
18924 3b 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78  ;.  DbFixer sFix
18925 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  ;.  Token *pName
18926 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73  ;.  int iDb;.  s
18927 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
18928 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20  rse->db;..  if( 
18929 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30 20 29  pParse->nVar>0 )
1892a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1892b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70  orMsg(pParse, "p
1892c 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 6f  arameters are no
1892d 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76 69 65  t allowed in vie
1892e 77 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ws");.    sqlite
1892f 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 53  3SelectDelete(pS
18930 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75  elect);.    retu
18931 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  rn;.  }.  sqlite
18932 33 53 74 61 72 74 54 61 62 6c 65 28 70 50 61 72  3StartTable(pPar
18933 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
18934 65 32 2c 20 69 73 54 65 6d 70 2c 20 31 2c 20 30  e2, isTemp, 1, 0
18935 2c 20 6e 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20  , noErr);.  p = 
18936 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
18937 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c  e;.  if( p==0 ||
18938 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b   pParse->nErr ){
18939 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .    sqlite3Sele
1893a 63 74 44 65 6c 65 74 65 28 70 53 65 6c 65 63 74  ctDelete(pSelect
1893b 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
1893c 20 7d 0a 20 20 73 71 6c 69 74 65 33 54 77 6f 50   }.  sqlite3TwoP
1893d 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  artName(pParse, 
1893e 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
1893f 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20 3d  &pName);.  iDb =
18940 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
18941 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63  Index(db, p->pSc
18942 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 73 71 6c  hema);.  if( sql
18943 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69  ite3FixInit(&sFi
18944 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20  x, pParse, iDb, 
18945 22 76 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a 20  "view", pName). 
18946 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69 78     && sqlite3Fix
18947 53 65 6c 65 63 74 28 26 73 46 69 78 2c 20 70 53  Select(&sFix, pS
18948 65 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20 20 20  elect).  ){.    
18949 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
1894a 65 74 65 28 70 53 65 6c 65 63 74 29 3b 0a 20 20  ete(pSelect);.  
1894b 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
1894c 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20   /* Make a copy 
1894d 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 53 45  of the entire SE
1894e 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
1894f 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  hat defines the 
18950 76 69 65 77 2e 0a 20 20 2a 2a 20 54 68 69 73 20  view..  ** This 
18951 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20 74  will force all t
18952 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20  he Expr.token.z 
18953 76 61 6c 75 65 73 20 74 6f 20 62 65 20 64 79 6e  values to be dyn
18954 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c  amically.  ** al
18955 6c 6f 63 61 74 65 64 20 72 61 74 68 65 72 20 74  located rather t
18956 68 61 6e 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  han point to the
18957 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 2d 20   input string - 
18958 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74  which means that
18959 0a 20 20 2a 2a 20 74 68 65 79 20 77 69 6c 6c 20  .  ** they will 
1895a 70 65 72 73 69 73 74 20 61 66 74 65 72 20 74 68  persist after th
1895b 65 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65  e current sqlite
1895c 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 72 65  3_exec() call re
1895d 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d  turns..  */.  p-
1895e 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  >pSelect = sqlit
1895f 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
18960 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71 6c 69  pSelect);.  sqli
18961 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
18962 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28 20  pSelect);.  if( 
18963 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
18964 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
18965 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 69    }.  if( !db->i
18966 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
18967 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
18968 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
18969 2c 20 70 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , p);.  }..  /* 
1896a 4c 6f 63 61 74 65 20 74 68 65 20 65 6e 64 20 6f  Locate the end o
1896b 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 45  f the CREATE VIE
1896c 57 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4d 61  W statement.  Ma
1896d 6b 65 20 73 45 6e 64 20 70 6f 69 6e 74 20 74 6f  ke sEnd point to
1896e 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 2e 0a 20  .  ** the end.. 
1896f 20 2a 2f 0a 20 20 73 45 6e 64 20 3d 20 70 50 61   */.  sEnd = pPa
18970 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 3b  rse->sLastToken;
18971 0a 20 20 69 66 28 20 73 45 6e 64 2e 7a 5b 30 5d  .  if( sEnd.z[0]
18972 21 3d 30 20 26 26 20 73 45 6e 64 2e 7a 5b 30 5d  !=0 && sEnd.z[0]
18973 21 3d 27 3b 27 20 29 7b 0a 20 20 20 20 73 45 6e  !=';' ){.    sEn
18974 64 2e 7a 20 2b 3d 20 73 45 6e 64 2e 6e 3b 0a 20  d.z += sEnd.n;. 
18975 20 7d 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 30 3b   }.  sEnd.n = 0;
18976 0a 20 20 6e 20 3d 20 73 45 6e 64 2e 7a 20 2d 20  .  n = sEnd.z - 
18977 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 7a 20 3d  pBegin->z;.  z =
18978 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
18979 20 63 68 61 72 2a 29 70 42 65 67 69 6e 2d 3e 7a   char*)pBegin->z
1897a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 26  ;.  while( n>0 &
1897b 26 20 28 7a 5b 6e 2d 31 5d 3d 3d 27 3b 27 20 7c  & (z[n-1]==';' |
1897c 7c 20 69 73 73 70 61 63 65 28 7a 5b 6e 2d 31 5d  | isspace(z[n-1]
1897d 29 29 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 73  )) ){ n--; }.  s
1897e 45 6e 64 2e 7a 20 3d 20 26 7a 5b 6e 2d 31 5d 3b  End.z = &z[n-1];
1897f 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 31 3b 0a 0a  .  sEnd.n = 1;..
18980 20 20 2f 2a 20 55 73 65 20 73 71 6c 69 74 65 33    /* Use sqlite3
18981 45 6e 64 54 61 62 6c 65 28 29 20 74 6f 20 61 64  EndTable() to ad
18982 64 20 74 68 65 20 76 69 65 77 20 74 6f 20 74 68  d the view to th
18983 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  e SQLITE_MASTER 
18984 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74  table */.  sqlit
18985 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73  e3EndTable(pPars
18986 65 2c 20 30 2c 20 26 73 45 6e 64 2c 20 30 29 3b  e, 0, &sEnd, 0);
18987 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e  .  return;.}.#en
18988 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
18989 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 23 69 66 20  IT_VIEW */..#if 
1898a 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1898b 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
1898c 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1898d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
1898e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65  ./*.** The Table
1898f 20 73 74 72 75 63 74 75 72 65 20 70 54 61 62 6c   structure pTabl
18990 65 20 69 73 20 72 65 61 6c 6c 79 20 61 20 56 49  e is really a VI
18991 45 57 2e 20 20 46 69 6c 6c 20 69 6e 20 74 68 65  EW.  Fill in the
18992 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65   names of.** the
18993 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
18994 76 69 65 77 20 69 6e 20 74 68 65 20 70 54 61 62  view in the pTab
18995 6c 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 52  le structure.  R
18996 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
18997 0a 2a 2a 20 6f 66 20 65 72 72 6f 72 73 2e 20 20  .** of errors.  
18998 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 73  If an error is s
18999 65 65 6e 20 6c 65 61 76 65 20 61 6e 20 65 72 72  een leave an err
1899a 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
1899b 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a  arse->zErrMsg..*
1899c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1899d 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77   int sqlite3View
1899e 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50  GetColumnNames(P
1899f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
189a0 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20  ble *pTable){.  
189a1 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 20  Table *pSelTab; 
189a2 20 20 2f 2a 20 41 20 66 61 6b 65 20 74 61 62 6c    /* A fake tabl
189a3 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77 65 20  e from which we 
189a4 67 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 73  get the result s
189a5 65 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  et */.  Select *
189a6 70 53 65 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 70  pSel;     /* Cop
189a7 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  y of the SELECT 
189a8 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
189a9 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e  the view */.  in
189aa 74 20 6e 45 72 72 20 3d 20 30 3b 20 20 20 20 20  t nErr = 0;     
189ab 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72  /* Number of err
189ac 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20  ors encountered 
189ad 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20  */.  int n;     
189ae 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
189af 61 72 69 6c 79 20 68 6f 6c 64 73 20 74 68 65 20  arily holds the 
189b0 6e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72  number of cursor
189b1 73 20 61 73 73 69 67 6e 65 64 20 2a 2f 0a 20 20  s assigned */.  
189b2 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
189b3 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 44 61  arse->db;  /* Da
189b4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
189b5 6e 20 66 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72  n for malloc err
189b6 6f 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ors */..  assert
189b7 28 20 70 54 61 62 6c 65 20 29 3b 0a 0a 23 69 66  ( pTable );..#if
189b8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
189b9 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
189ba 69 66 28 20 73 71 6c 69 74 65 33 56 74 61 62 43  if( sqlite3VtabC
189bb 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50 61 72 73  allConnect(pPars
189bc 65 2c 20 70 54 61 62 6c 65 29 20 29 7b 0a 20 20  e, pTable) ){.  
189bd 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
189be 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28  ERROR;.  }.  if(
189bf 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 6c   IsVirtual(pTabl
189c0 65 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  e) ) return 0;.#
189c1 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
189c2 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
189c3 20 20 2f 2a 20 41 20 70 6f 73 69 74 69 76 65 20    /* A positive 
189c4 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68 65 20 63  nCol means the c
189c5 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 66 6f 72  olumns names for
189c6 20 74 68 69 73 20 76 69 65 77 20 61 72 65 0a 20   this view are. 
189c7 20 2a 2a 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77   ** already know
189c8 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  n..  */.  if( pT
189c9 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 20 29 20 72  able->nCol>0 ) r
189ca 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 41  eturn 0;..  /* A
189cb 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c 20 69   negative nCol i
189cc 73 20 61 20 73 70 65 63 69 61 6c 20 6d 61 72 6b  s a special mark
189cd 65 72 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20  er meaning that 
189ce 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  we are currently
189cf 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20  .  ** trying to 
189d0 63 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6c 75  compute the colu
189d1 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 66 20 77 65  mn names.  If we
189d2 20 65 6e 74 65 72 20 74 68 69 73 20 72 6f 75 74   enter this rout
189d3 69 6e 65 20 77 69 74 68 0a 20 20 2a 2a 20 61 20  ine with.  ** a 
189d4 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c 2c 20 69  negative nCol, i
189d5 74 20 6d 65 61 6e 73 20 74 77 6f 20 6f 72 20 6d  t means two or m
189d6 6f 72 65 20 76 69 65 77 73 20 66 6f 72 6d 20 61  ore views form a
189d7 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 74 68 69 73   loop, like this
189d8 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
189d9 43 52 45 41 54 45 20 56 49 45 57 20 6f 6e 65 20  CREATE VIEW one 
189da 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  AS SELECT * FROM
189db 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 20 20 20 43   two;.  **     C
189dc 52 45 41 54 45 20 56 49 45 57 20 74 77 6f 20 41  REATE VIEW two A
189dd 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  S SELECT * FROM 
189de 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  one;.  **.  ** A
189df 63 74 75 61 6c 6c 79 2c 20 74 68 69 73 20 65 72  ctually, this er
189e0 72 6f 72 20 69 73 20 63 61 75 67 68 74 20 70 72  ror is caught pr
189e1 65 76 69 6f 75 73 6c 79 20 61 6e 64 20 73 6f 20  eviously and so 
189e2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65  the following te
189e3 73 74 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 61  st.  ** should a
189e4 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 42 75 74  lways fail.  But
189e5 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 69   we will leave i
189e6 74 20 69 6e 20 70 6c 61 63 65 20 6a 75 73 74 20  t in place just 
189e7 74 6f 20 62 65 20 73 61 66 65 2e 0a 20 20 2a 2f  to be safe..  */
189e8 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e  .  if( pTable->n
189e9 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c  Col<0 ){.    sql
189ea 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
189eb 72 73 65 2c 20 22 76 69 65 77 20 25 73 20 69 73  rse, "view %s is
189ec 20 63 69 72 63 75 6c 61 72 6c 79 20 64 65 66 69   circularly defi
189ed 6e 65 64 22 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e  ned", pTable->zN
189ee 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
189ef 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   1;.  }.  assert
189f0 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 3d  ( pTable->nCol>=
189f1 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65  0 );..  /* If we
189f2 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69   get this far, i
189f3 74 20 6d 65 61 6e 73 20 77 65 20 6e 65 65 64 20  t means we need 
189f4 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 74  to compute the t
189f5 61 62 6c 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2a  able names..  **
189f6 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 63   Note that the c
189f7 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 52 65  all to sqlite3Re
189f8 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28  sultSetOfSelect(
189f9 29 20 77 69 6c 6c 20 65 78 70 61 6e 64 20 61 6e  ) will expand an
189fa 79 0a 20 20 2a 2a 20 22 2a 22 20 65 6c 65 6d 65  y.  ** "*" eleme
189fb 6e 74 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  nts in the resul
189fc 74 73 20 73 65 74 20 6f 66 20 74 68 65 20 76 69  ts set of the vi
189fd 65 77 20 61 6e 64 20 77 69 6c 6c 20 61 73 73 69  ew and will assi
189fe 67 6e 20 63 75 72 73 6f 72 73 0a 20 20 2a 2a 20  gn cursors.  ** 
189ff 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20  to the elements 
18a00 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
18a01 73 65 2e 20 20 42 75 74 20 77 65 20 64 6f 20 6e  se.  But we do n
18a02 6f 74 20 77 61 6e 74 20 74 68 65 73 65 20 63 68  ot want these ch
18a03 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65  anges.  ** to be
18a04 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20 53 6f 20   permanent.  So 
18a05 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20  the computation 
18a06 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20 63 6f 70  is done on a cop
18a07 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a  y of the SELECT.
18a08 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74    ** statement t
18a09 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  hat defines the 
18a0a 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  view..  */.  ass
18a0b 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70 53 65  ert( pTable->pSe
18a0c 6c 65 63 74 20 29 3b 0a 20 20 70 53 65 6c 20 3d  lect );.  pSel =
18a0d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
18a0e 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53  p(db, pTable->pS
18a0f 65 6c 65 63 74 29 3b 0a 20 20 69 66 28 20 70 53  elect);.  if( pS
18a10 65 6c 20 29 7b 0a 20 20 20 20 6e 20 3d 20 70 50  el ){.    n = pP
18a11 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20  arse->nTab;.    
18a12 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73  sqlite3SrcListAs
18a13 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72  signCursors(pPar
18a14 73 65 2c 20 70 53 65 6c 2d 3e 70 53 72 63 29 3b  se, pSel->pSrc);
18a15 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f  .    pTable->nCo
18a16 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 70 53 65 6c  l = -1;.    pSel
18a17 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73  Tab = sqlite3Res
18a18 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70  ultSetOfSelect(p
18a19 50 61 72 73 65 2c 20 30 2c 20 70 53 65 6c 29 3b  Parse, 0, pSel);
18a1a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61  .    pParse->nTa
18a1b 62 20 3d 20 6e 3b 0a 20 20 20 20 69 66 28 20 70  b = n;.    if( p
18a1c 53 65 6c 54 61 62 20 29 7b 0a 20 20 20 20 20 20  SelTab ){.      
18a1d 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
18a1e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  aCol==0 );.     
18a1f 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
18a20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20  pSelTab->nCol;. 
18a21 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f       pTable->aCo
18a22 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f  l = pSelTab->aCo
18a23 6c 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62  l;.      pSelTab
18a24 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->nCol = 0;.    
18a25 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20    pSelTab->aCol 
18a26 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
18a27 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 53  e3DeleteTable(pS
18a28 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 70 54  elTab);.      pT
18a29 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 66  able->pSchema->f
18a2a 6c 61 67 73 20 7c 3d 20 44 42 5f 55 6e 72 65 73  lags |= DB_Unres
18a2b 65 74 56 69 65 77 73 3b 0a 20 20 20 20 7d 65 6c  etViews;.    }el
18a2c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62 6c 65  se{.      pTable
18a2d 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->nCol = 0;.    
18a2e 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 7d 0a    nErr++;.    }.
18a2f 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
18a30 74 44 65 6c 65 74 65 28 70 53 65 6c 29 3b 0a 20  tDelete(pSel);. 
18a31 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 6e 45   } else {.    nE
18a32 72 72 2b 2b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  rr++;.  }.#endif
18a33 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
18a34 56 49 45 57 20 2a 2f 0a 20 20 72 65 74 75 72 6e  VIEW */.  return
18a35 20 6e 45 72 72 3b 20 20 0a 7d 0a 23 65 6e 64 69   nErr;  .}.#endi
18a36 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
18a37 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
18a38 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
18a39 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
18a3a 41 42 4c 45 29 20 2a 2f 0a 0a 23 69 66 6e 64 65  ABLE) */..#ifnde
18a3b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
18a3c 45 57 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74  EW./*.** Clear t
18a3d 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
18a3e 66 72 6f 6d 20 65 76 65 72 79 20 56 49 45 57 20  from every VIEW 
18a3f 69 6e 20 64 61 74 61 62 61 73 65 20 69 64 78 2e  in database idx.
18a40 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
18a41 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41  sqliteViewResetA
18a42 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ll(sqlite3 *db, 
18a43 69 6e 74 20 69 64 78 29 7b 0a 20 20 48 61 73 68  int idx){.  Hash
18a44 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 66 28 20 21  Elem *i;.  if( !
18a45 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62  DbHasProperty(db
18a46 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65  , idx, DB_Unrese
18a47 74 56 69 65 77 73 29 20 29 20 72 65 74 75 72 6e  tViews) ) return
18a48 3b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65  ;.  for(i=sqlite
18a49 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61  HashFirst(&db->a
18a4a 44 62 5b 69 64 78 5d 2e 70 53 63 68 65 6d 61 2d  Db[idx].pSchema-
18a4b 3e 74 62 6c 48 61 73 68 29 3b 20 69 3b 69 3d 73  >tblHash); i;i=s
18a4c 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29  qliteHashNext(i)
18a4d 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
18a4e 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  ab = sqliteHashD
18a4f 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28 20  ata(i);.    if( 
18a50 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pTab->pSelect ){
18a51 0a 20 20 20 20 20 20 73 71 6c 69 74 65 52 65 73  .      sqliteRes
18a52 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 54  etColumnNames(pT
18a53 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ab);.    }.  }. 
18a54 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79   DbClearProperty
18a55 28 64 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72  (db, idx, DB_Unr
18a56 65 73 65 74 56 69 65 77 73 29 3b 0a 7d 0a 23 65  esetViews);.}.#e
18a57 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  lse.# define sql
18a58 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28  iteViewResetAll(
18a59 41 2c 42 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53  A,B).#endif /* S
18a5a 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20  QLITE_OMIT_VIEW 
18a5b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  */../*.** This f
18a5c 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
18a5d 64 20 62 79 20 74 68 65 20 56 44 42 45 20 74 6f  d by the VDBE to
18a5e 20 61 64 6a 75 73 74 20 74 68 65 20 69 6e 74 65   adjust the inte
18a5f 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a 20 75  rnal schema.** u
18a60 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 68  sed by SQLite wh
18a61 65 6e 20 74 68 65 20 62 74 72 65 65 20 6c 61 79  en the btree lay
18a62 65 72 20 6d 6f 76 65 73 20 61 20 74 61 62 6c 65  er moves a table
18a63 20 72 6f 6f 74 20 70 61 67 65 2e 20 54 68 65 0a   root page. The.
18a64 2a 2a 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20  ** root-page of 
18a65 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  a table or index
18a66 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
18a67 20 68 61 73 20 63 68 61 6e 67 65 64 20 66 72 6f   has changed fro
18a68 6d 20 69 46 72 6f 6d 0a 2a 2a 20 74 6f 20 69 54  m iFrom.** to iT
18a69 6f 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20  o..**.** Ticket 
18a6a 23 31 37 32 38 3a 20 20 54 68 65 20 73 79 6d 62  #1728:  The symb
18a6b 6f 6c 20 74 61 62 6c 65 20 6d 69 67 68 74 20 73  ol table might s
18a6c 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 69 6e 66  till contain inf
18a6d 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74  ormation.** on t
18a6e 61 62 6c 65 73 20 61 6e 64 2f 6f 72 20 69 6e 64  ables and/or ind
18a6f 69 63 65 73 20 74 68 61 74 20 61 72 65 20 74 68  ices that are th
18a70 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62 65 69  e process of bei
18a71 6e 67 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 49  ng deleted..** I
18a72 66 20 79 6f 75 20 61 72 65 20 75 6e 6c 75 63 6b  f you are unluck
18a73 79 2c 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20  y, one of those 
18a74 64 65 6c 65 74 65 64 20 69 6e 64 69 63 65 73 20  deleted indices 
18a75 6f 72 20 74 61 62 6c 65 73 20 6d 69 67 68 74 0a  or tables might.
18a76 2a 2a 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  ** have the same
18a77 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72   rootpage number
18a78 20 61 73 20 74 68 65 20 72 65 61 6c 20 74 61 62   as the real tab
18a79 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 68 61 74  le or index that
18a7a 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 6d 6f 76   is.** being mov
18a7b 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e 6e 6f  ed.  So we canno
18a7c 74 20 73 74 6f 70 20 73 65 61 72 63 68 69 6e 67  t stop searching
18a7d 20 61 66 74 65 72 20 74 68 65 20 66 69 72 73 74   after the first
18a7e 20 6d 61 74 63 68 20 0a 2a 2a 20 62 65 63 61 75   match .** becau
18a7f 73 65 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  se the first mat
18a80 63 68 20 6d 69 67 68 74 20 62 65 20 66 6f 72 20  ch might be for 
18a81 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 6c 65 74  one of the delet
18a82 65 64 20 69 6e 64 69 63 65 73 0a 2a 2a 20 6f 72  ed indices.** or
18a83 20 74 61 62 6c 65 73 20 61 6e 64 20 6e 6f 74 20   tables and not 
18a84 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65 78 20  the table/index 
18a85 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c 79  that is actually
18a86 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 0a 2a 2a   being moved..**
18a87 20 57 65 20 6d 75 73 74 20 63 6f 6e 74 69 6e 75   We must continu
18a88 65 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20  e looping until 
18a89 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64 20 69  all tables and i
18a8a 6e 64 69 63 65 73 20 77 69 74 68 0a 2a 2a 20 72  ndices with.** r
18a8b 6f 6f 74 70 61 67 65 3d 3d 69 46 72 6f 6d 20 68  ootpage==iFrom h
18a8c 61 76 65 20 62 65 65 6e 20 63 6f 6e 76 65 72 74  ave been convert
18a8d 65 64 20 74 6f 20 68 61 76 65 20 61 20 72 6f 6f  ed to have a roo
18a8e 74 70 61 67 65 20 6f 66 20 69 54 6f 0a 2a 2a 20  tpage of iTo.** 
18a8f 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63  in order to be c
18a90 65 72 74 61 69 6e 20 74 68 61 74 20 77 65 20 67  ertain that we g
18a91 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f 6e 65  ot the right one
18a92 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
18a93 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18a94 55 55 4d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  UUM.SQLITE_PRIVA
18a95 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
18a96 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62 20  ootPageMoved(Db 
18a97 2a 70 44 62 2c 20 69 6e 74 20 69 46 72 6f 6d 2c  *pDb, int iFrom,
18a98 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 48 61 73   int iTo){.  Has
18a99 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20  hElem *pElem;.  
18a9a 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 0a 20 20  Hash *pHash;..  
18a9b 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53  pHash = &pDb->pS
18a9c 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a  chema->tblHash;.
18a9d 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69    for(pElem=sqli
18a9e 74 65 48 61 73 68 46 69 72 73 74 28 70 48 61 73  teHashFirst(pHas
18a9f 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d  h); pElem; pElem
18aa0 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
18aa1 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61 62  pElem)){.    Tab
18aa2 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74  le *pTab = sqlit
18aa3 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29  eHashData(pElem)
18aa4 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e  ;.    if( pTab->
18aa5 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20  tnum==iFrom ){. 
18aa6 20 20 20 20 20 70 54 61 62 2d 3e 74 6e 75 6d 20       pTab->tnum 
18aa7 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d  = iTo;.    }.  }
18aa8 0a 20 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d  .  pHash = &pDb-
18aa9 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  >pSchema->idxHas
18aaa 68 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73  h;.  for(pElem=s
18aab 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 70  qliteHashFirst(p
18aac 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45  Hash); pElem; pE
18aad 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  lem=sqliteHashNe
18aae 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20  xt(pElem)){.    
18aaf 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71  Index *pIdx = sq
18ab0 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c  liteHashData(pEl
18ab1 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64  em);.    if( pId
18ab2 78 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29  x->tnum==iFrom )
18ab3 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 74 6e  {.      pIdx->tn
18ab4 75 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a  um = iTo;.    }.
18ab5 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a    }.}.#endif../*
18ab6 0a 2a 2a 20 57 72 69 74 65 20 63 6f 64 65 20 74  .** Write code t
18ab7 6f 20 65 72 61 73 65 20 74 68 65 20 74 61 62 6c  o erase the tabl
18ab8 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65  e with root-page
18ab9 20 69 54 61 62 6c 65 20 66 72 6f 6d 20 64 61 74   iTable from dat
18aba 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 6c  abase iDb..** Al
18abb 73 6f 20 77 72 69 74 65 20 63 6f 64 65 20 74 6f  so write code to
18abc 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69   modify the sqli
18abd 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
18abe 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73 63 68  and internal sch
18abf 65 6d 61 0a 2a 2a 20 69 66 20 61 20 72 6f 6f 74  ema.** if a root
18ac0 2d 70 61 67 65 20 6f 66 20 61 6e 6f 74 68 65 72  -page of another
18ac1 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 20   table is moved 
18ac2 62 79 20 74 68 65 20 62 74 72 65 65 2d 6c 61 79  by the btree-lay
18ac3 65 72 20 77 68 69 6c 73 74 0a 2a 2a 20 65 72 61  er whilst.** era
18ac4 73 69 6e 67 20 69 54 61 62 6c 65 20 28 74 68 69  sing iTable (thi
18ac5 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69 74  s can happen wit
18ac6 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  h an auto-vacuum
18ac7 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 20 0a   database)..*/ .
18ac8 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 73 74  static void dest
18ac9 72 6f 79 52 6f 6f 74 50 61 67 65 28 50 61 72 73  royRootPage(Pars
18aca 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
18acb 54 61 62 6c 65 2c 20 69 6e 74 20 69 44 62 29 7b  Table, int iDb){
18acc 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
18acd 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
18ace 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  se);.  sqlite3Vd
18acf 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 65  beAddOp(v, OP_De
18ad0 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c 20 69  stroy, iTable, i
18ad1 44 62 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  Db);.#ifndef SQL
18ad2 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18ad3 55 55 4d 0a 20 20 2f 2a 20 4f 50 5f 44 65 73 74  UUM.  /* OP_Dest
18ad4 72 6f 79 20 70 75 73 68 65 73 20 61 6e 20 69 6e  roy pushes an in
18ad5 74 65 67 65 72 20 6f 6e 74 6f 20 74 68 65 20 73  teger onto the s
18ad6 74 61 63 6b 2e 20 49 66 20 74 68 69 73 20 69 6e  tack. If this in
18ad7 74 65 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f  teger.  ** is no
18ad8 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20  n-zero, then it 
18ad9 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  is the root page
18ada 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62   number of a tab
18adb 6c 65 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a  le moved to.  **
18adc 20 6c 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65   location iTable
18add 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
18ade 63 6f 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68  code modifies th
18adf 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
18ae0 74 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65  table to.  ** re
18ae1 66 6c 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a  flect this..  **
18ae2 0a 20 20 2a 2a 20 54 68 65 20 22 23 30 22 20 69  .  ** The "#0" i
18ae3 6e 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 73  n the SQL is a s
18ae4 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20  pecial constant 
18ae5 74 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65  that means whate
18ae6 76 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69  ver value.  ** i
18ae7 73 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20  s on the top of 
18ae8 74 68 65 20 73 74 61 63 6b 2e 20 20 53 65 65 20  the stack.  See 
18ae9 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 45  sqlite3RegisterE
18aea 78 70 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 73 71  xpr()..  */.  sq
18aeb 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
18aec 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22  (pParse, .     "
18aed 55 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54  UPDATE %Q.%s SET
18aee 20 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45   rootpage=%d WHE
18aef 52 45 20 23 30 20 41 4e 44 20 72 6f 6f 74 70 61  RE #0 AND rootpa
18af0 67 65 3d 23 30 22 2c 0a 20 20 20 20 20 70 50 61  ge=#0",.     pPa
18af1 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62  rse->db->aDb[iDb
18af2 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ].zName, SCHEMA_
18af3 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61 62  TABLE(iDb), iTab
18af4 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  le);.#endif.}../
18af5 2a 0a 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20  *.** Write VDBE 
18af6 63 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74 61  code to erase ta
18af7 62 6c 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c  ble pTab and all
18af8 20 61 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69   associated indi
18af9 63 65 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20  ces on disk..** 
18afa 43 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20 74  Code to update t
18afb 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
18afc 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65   tables and inte
18afd 72 6e 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69  rnal schema defi
18afe 6e 69 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61  nitions.** in ca
18aff 73 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62  se a root-page b
18b00 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74  elonging to anot
18b01 68 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76  her table is mov
18b02 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20  ed by the btree 
18b03 6c 61 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f  layer.** is also
18b04 20 61 64 64 65 64 20 28 74 68 69 73 20 63 61 6e   added (this can
18b05 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20   happen with an 
18b06 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
18b07 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  base)..*/.static
18b08 20 76 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62   void destroyTab
18b09 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  le(Parse *pParse
18b0a 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
18b0b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
18b0c 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18b0d 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69  Index *pIdx;.  i
18b0e 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
18b0f 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
18b10 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
18b11 70 53 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74  pSchema);.  dest
18b12 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
18b13 73 65 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  se, pTab->tnum, 
18b14 69 44 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78  iDb);.  for(pIdx
18b15 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
18b16 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
18b17 70 4e 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74  pNext){.    dest
18b18 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
18b19 73 65 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  se, pIdx->tnum, 
18b1a 69 44 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  iDb);.  }.#else.
18b1b 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61    /* If the data
18b1c 62 61 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f  base may be auto
18b1d 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20  -vacuum capable 
18b1e 28 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  (if SQLITE_OMIT_
18b1f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20  AUTOVACUUM.  ** 
18b20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
18b21 20 74 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f   then it is impo
18b22 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50  rtant to call OP
18b23 5f 44 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a  _Destroy on the.
18b24 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69    ** table and i
18b25 6e 64 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20  ndex root-pages 
18b26 69 6e 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69  in order, starti
18b27 6e 67 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65  ng with the nume
18b28 72 69 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61  rically .  ** la
18b29 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20  rgest root-page 
18b2a 6e 75 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61  number. This gua
18b2b 72 61 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e  rantees that non
18b2c 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61  e of the root-pa
18b2d 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64  ges.  ** to be d
18b2e 65 73 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f  estroyed is relo
18b2f 63 61 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c  cated by an earl
18b30 69 65 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20  ier OP_Destroy. 
18b31 69 2e 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a  i.e. if the.  **
18b32 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20   following were 
18b33 63 6f 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  coded:.  **.  **
18b34 20 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a   OP_Destroy 4 0.
18b35 20 20 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50    ** ....  ** OP
18b36 5f 44 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a  _Destroy 5 0.  *
18b37 2a 0a 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20  *.  ** and root 
18b38 70 61 67 65 20 35 20 68 61 70 70 65 6e 65 64 20  page 5 happened 
18b39 74 6f 20 62 65 20 74 68 65 20 6c 61 72 67 65 73  to be the larges
18b3a 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62  t root-page numb
18b3b 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64  er in the.  ** d
18b3c 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f  atabase, then ro
18b3d 6f 74 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20  ot page 5 would 
18b3e 62 65 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65  be moved to page
18b3f 20 34 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20   4 by the .  ** 
18b40 22 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22  "OP_Destroy 4 0"
18b41 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62   opcode. The sub
18b42 73 65 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74  sequent "OP_Dest
18b43 72 6f 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68  roy 5 0" would h
18b44 69 74 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c  it.  ** a free-l
18b45 69 73 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20  ist page..  */. 
18b46 20 69 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62   int iTab = pTab
18b47 2d 3e 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44  ->tnum;.  int iD
18b48 65 73 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20  estroyed = 0;.. 
18b49 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20   while( 1 ){.   
18b4a 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
18b4b 20 20 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d    int iLargest =
18b4c 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65   0;..    if( iDe
18b4d 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54  stroyed==0 || iT
18b4e 61 62 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b  ab<iDestroyed ){
18b4f 0a 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20  .      iLargest 
18b50 3d 20 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20  = iTab;.    }.  
18b51 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
18b52 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
18b53 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
18b54 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64 78  {.      int iIdx
18b55 20 3d 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20   = pIdx->tnum;. 
18b56 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
18b57 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62  x->pSchema==pTab
18b58 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ->pSchema );.   
18b59 20 20 20 69 66 28 20 28 69 44 65 73 74 72 6f 79     if( (iDestroy
18b5a 65 64 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69  ed==0 || (iIdx<i
18b5b 44 65 73 74 72 6f 79 65 64 29 29 20 26 26 20 69  Destroyed)) && i
18b5c 49 64 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a  Idx>iLargest ){.
18b5d 20 20 20 20 20 20 20 20 69 4c 61 72 67 65 73 74          iLargest
18b5e 20 3d 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d   = iIdx;.      }
18b5f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
18b60 4c 61 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20  Largest==0 ){.  
18b61 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
18b62 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
18b63 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
18b64 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
18b65 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
18b66 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65  chema);.      de
18b67 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50  stroyRootPage(pP
18b68 61 72 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20  arse, iLargest, 
18b69 69 44 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73  iDb);.      iDes
18b6a 74 72 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73  troyed = iLarges
18b6b 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  t;.    }.  }.#en
18b6c 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dif.}../*.** Thi
18b6d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
18b6e 6c 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f  led to do the wo
18b6f 72 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42  rk of a DROP TAB
18b70 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  LE statement..**
18b71 20 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61   pName is the na
18b72 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
18b73 74 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a  to be dropped..*
18b74 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18b75 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
18b76 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  pTable(Parse *pP
18b77 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
18b78 4e 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77  Name, int isView
18b79 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20  , int noErr){.  
18b7a 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56  Table *pTab;.  V
18b7b 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65  dbe *v;.  sqlite
18b7c 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
18b7d 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a  db;.  int iDb;..
18b7e 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
18b7f 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
18b80 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
18b81 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
18b82 6c 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  le;.  }.  assert
18b83 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31  ( pName->nSrc==1
18b84 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   );.  pTab = sql
18b85 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
18b86 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 2d 3e 61  pParse, pName->a
18b87 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65  [0].zName, pName
18b88 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65  ->a[0].zDatabase
18b89 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d  );..  if( pTab==
18b8a 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 6f 45  0 ){.    if( noE
18b8b 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rr ){.      sqli
18b8c 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70 50  te3ErrorClear(pP
18b8d 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  arse);.    }.   
18b8e 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
18b8f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 44 62  table;.  }.  iDb
18b90 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
18b91 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
18b92 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
18b93 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
18b94 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 23  iDb<db->nDb );.#
18b95 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18b96 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
18b97 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64  .  {.    int cod
18b98 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
18b99 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41  r *zTab = SCHEMA
18b9a 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20  _TABLE(iDb);.   
18b9b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
18b9c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
18b9d 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74  zName;.    const
18b9e 20 63 68 61 72 20 2a 7a 41 72 67 32 20 3d 20 30   char *zArg2 = 0
18b9f 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
18ba0 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
18ba1 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  e, SQLITE_DELETE
18ba2 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 29  , zTab, 0, zDb))
18ba3 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
18ba4 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
18ba5 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69    }.    if( isVi
18ba6 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ew ){.      if( 
18ba7 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
18ba8 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  iDb==1 ){.      
18ba9 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
18baa 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 3b 0a  DROP_TEMP_VIEW;.
18bab 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
18bac 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
18bad 54 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a 20 20  TE_DROP_VIEW;.  
18bae 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
18baf 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
18bb0 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65  LTABLE.    }else
18bb1 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
18bb2 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 69 66  Tab) ){.      if
18bb3 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
18bb4 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
18bb5 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
18bb6 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64       goto exit_d
18bb7 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 20  rop_table;.     
18bb8 20 7d 0a 20 20 20 20 20 20 63 6f 64 65 20 3d 20   }.      code = 
18bb9 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42  SQLITE_DROP_VTAB
18bba 4c 45 3b 0a 20 20 20 20 20 20 7a 41 72 67 32 20  LE;.      zArg2 
18bbb 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e  = pTab->pMod->zN
18bbc 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ame;.#endif.    
18bbd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
18bbe 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
18bbf 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20   iDb==1 ){.     
18bc0 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
18bc1 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45  _DROP_TEMP_TABLE
18bc2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
18bc3 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
18bc4 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b  LITE_DROP_TABLE;
18bc5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
18bc6 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
18bc7 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
18bc8 63 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  code, pTab->zNam
18bc9 65 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20 29  e, zArg2, zDb) )
18bca 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
18bcb 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
18bcc 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
18bcd 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
18bce 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
18bcf 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  TE, pTab->zName,
18bd0 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
18bd1 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
18bd2 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20  _table;.    }.  
18bd3 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  }.#endif.  if( p
18bd4 54 61 62 2d 3e 72 65 61 64 4f 6e 6c 79 20 7c 7c  Tab->readOnly ||
18bd5 20 70 54 61 62 3d 3d 64 62 2d 3e 61 44 62 5b 69   pTab==db->aDb[i
18bd6 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 70 53 65  Db].pSchema->pSe
18bd7 71 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  qTab ){.    sqli
18bd8 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
18bd9 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61  se, "table %s ma
18bda 79 20 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64  y not be dropped
18bdb 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
18bdc 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
18bdd 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a  rop_table;.  }..
18bde 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18bdf 4d 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e  MIT_VIEW.  /* En
18be0 73 75 72 65 20 44 52 4f 50 20 54 41 42 4c 45 20  sure DROP TABLE 
18be1 69 73 20 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61  is not used on a
18be2 20 76 69 65 77 2c 20 61 6e 64 20 44 52 4f 50 20   view, and DROP 
18be3 56 49 45 57 20 69 73 20 6e 6f 74 20 75 73 65 64  VIEW is not used
18be4 0a 20 20 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65  .  ** on a table
18be5 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56  ..  */.  if( isV
18be6 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65  iew && pTab->pSe
18be7 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73  lect==0 ){.    s
18be8 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
18be9 50 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50  Parse, "use DROP
18bea 20 54 41 42 4c 45 20 74 6f 20 64 65 6c 65 74 65   TABLE to delete
18beb 20 74 61 62 6c 65 20 25 73 22 2c 20 70 54 61 62   table %s", pTab
18bec 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
18bed 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
18bee 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69  le;.  }.  if( !i
18bef 73 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70  sView && pTab->p
18bf0 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71  Select ){.    sq
18bf1 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
18bf2 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20  arse, "use DROP 
18bf3 56 49 45 57 20 74 6f 20 64 65 6c 65 74 65 20 76  VIEW to delete v
18bf4 69 65 77 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a  iew %s", pTab->z
18bf5 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
18bf6 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
18bf7 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
18bf8 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
18bf9 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61  to remove the ta
18bfa 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 6d 61 73  ble from the mas
18bfb 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f  ter table.  ** o
18bfc 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76  n disk..  */.  v
18bfd 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
18bfe 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
18bff 20 76 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65   v ){.    Trigge
18c00 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20 20 20  r *pTrigger;.   
18c01 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e   Db *pDb = &db->
18c02 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 73 71  aDb[iDb];.    sq
18c03 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
18c04 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
18c05 20 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64   0, iDb);..#ifnd
18c06 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
18c07 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
18c08 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
18c09 61 62 29 20 29 7b 0a 20 20 20 20 20 20 56 64 62  ab) ){.      Vdb
18c0a 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
18c0b 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
18c0c 20 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20       if( v ){.  
18c0d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
18c0e 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 56 42 65  eAddOp(v, OP_VBe
18c0f 67 69 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  gin, 0, 0);.    
18c10 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
18c11 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c  ..    /* Drop al
18c12 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63  l triggers assoc
18c13 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74  iated with the t
18c14 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70  able being dropp
18c15 65 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20  ed. Code.    ** 
18c16 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  is generated to 
18c17 72 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66  remove entries f
18c18 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  rom sqlite_maste
18c19 72 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20  r and/or.    ** 
18c1a 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
18c1b 65 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  er if required..
18c1c 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67      */.    pTrig
18c1d 67 65 72 20 3d 20 70 54 61 62 2d 3e 70 54 72 69  ger = pTab->pTri
18c1e 67 67 65 72 3b 0a 20 20 20 20 77 68 69 6c 65 28  gger;.    while(
18c1f 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
18c20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67     assert( pTrig
18c21 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54  ger->pSchema==pT
18c22 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a  ab->pSchema || .
18c23 20 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67            pTrigg
18c24 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d  er->pSchema==db-
18c25 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20  >aDb[1].pSchema 
18c26 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
18c27 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 70  DropTriggerPtr(p
18c28 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 29  Parse, pTrigger)
18c29 3b 0a 20 20 20 20 20 20 70 54 72 69 67 67 65 72  ;.      pTrigger
18c2a 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65   = pTrigger->pNe
18c2b 78 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64  xt;.    }..#ifnd
18c2c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
18c2d 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20  UTOINCREMENT.   
18c2e 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 65   /* Remove any e
18c2f 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 73 71  ntries of the sq
18c30 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
18c31 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ble associated w
18c32 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74  ith.    ** the t
18c33 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70  able being dropp
18c34 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65  ed. This is done
18c35 20 62 65 66 6f 72 65 20 74 68 65 20 74 61 62 6c   before the tabl
18c36 65 20 69 73 20 64 72 6f 70 70 65 64 0a 20 20 20  e is dropped.   
18c37 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65 65   ** at the btree
18c38 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20   level, in case 
18c39 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65  the sqlite_seque
18c3a 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20  nce table needs 
18c3b 74 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 20 61  to.    ** move a
18c3c 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68  s a result of th
18c3d 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70  e drop (can happ
18c3e 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75  en in auto-vacuu
18c3f 6d 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f 0a  m mode)..    */.
18c40 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 61 75      if( pTab->au
18c41 74 6f 49 6e 63 20 29 7b 0a 20 20 20 20 20 20 73  toInc ){.      s
18c42 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
18c43 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
18c44 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
18c45 73 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  s.sqlite_sequenc
18c46 65 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22  e WHERE name=%Q"
18c47 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a  ,.        pDb->z
18c48 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Name, pTab->zNam
18c49 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  e.      );.    }
18c4a 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
18c4b 44 72 6f 70 20 61 6c 6c 20 53 51 4c 49 54 45 5f  Drop all SQLITE_
18c4c 4d 41 53 54 45 52 20 74 61 62 6c 65 20 61 6e 64  MASTER table and
18c4d 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74   index entries t
18c4e 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65  hat refer to the
18c4f 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54  .    ** table. T
18c50 68 65 20 70 72 6f 67 72 61 6d 20 6e 61 6d 65 20  he program name 
18c51 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 74 68  loops through th
18c52 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61  e master table a
18c53 6e 64 20 64 65 6c 65 74 65 73 0a 20 20 20 20 2a  nd deletes.    *
18c54 2a 20 65 76 65 72 79 20 72 6f 77 20 74 68 61 74  * every row that
18c55 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62   refers to a tab
18c56 6c 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e  le of the same n
18c57 61 6d 65 20 61 73 20 74 68 65 20 6f 6e 65 20 62  ame as the one b
18c58 65 69 6e 67 0a 20 20 20 20 2a 2a 20 64 72 6f 70  eing.    ** drop
18c59 70 65 64 2e 20 54 72 69 67 67 65 72 73 20 61 72  ped. Triggers ar
18c5a 65 20 68 61 6e 64 6c 65 64 20 73 65 70 65 72 61  e handled sepera
18c5b 74 65 6c 79 20 62 65 63 61 75 73 65 20 61 20 74  tely because a t
18c5c 72 69 67 67 65 72 20 63 61 6e 20 62 65 0a 20 20  rigger can be.  
18c5d 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 20    ** created in 
18c5e 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73  the temp databas
18c5f 65 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f  e that refers to
18c60 20 61 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f 74   a table in anot
18c61 68 65 72 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  her.    ** datab
18c62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
18c63 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
18c64 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  se(pParse, .    
18c65 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
18c66 20 25 51 2e 25 73 20 57 48 45 52 45 20 74 62 6c   %Q.%s WHERE tbl
18c67 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 20 74 79 70  _name=%Q and typ
18c68 65 21 3d 27 74 72 69 67 67 65 72 27 22 2c 0a 20  e!='trigger'",. 
18c69 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d         pDb->zNam
18c6a 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  e, SCHEMA_TABLE(
18c6b 69 44 62 29 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  iDb), pTab->zNam
18c6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21 69 73 56  e);.    if( !isV
18c6d 69 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61  iew && !IsVirtua
18c6e 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
18c6f 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50   destroyTable(pP
18c70 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
18c71 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76   }..    /* Remov
18c72 65 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72  e the table entr
18c73 79 20 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20  y from SQLite's 
18c74 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20  internal schema 
18c75 61 6e 64 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a  and modify.    *
18c76 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  * the schema coo
18c77 6b 69 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  kie..    */.    
18c78 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
18c79 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ab) ){.      sql
18c7a 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
18c7b 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 62 2c  P_VDestroy, iDb,
18c7c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   0, pTab->zName,
18c7d 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73   0);.    }.    s
18c7e 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c  qlite3VdbeOp3(v,
18c7f 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 69   OP_DropTable, i
18c80 44 62 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  Db, 0, pTab->zNa
18c81 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  me, 0);.    sqli
18c82 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28  te3ChangeCookie(
18c83 64 62 2c 20 76 2c 20 69 44 62 29 3b 0a 20 20 7d  db, v, iDb);.  }
18c84 0a 20 20 73 71 6c 69 74 65 56 69 65 77 52 65 73  .  sqliteViewRes
18c85 65 74 41 6c 6c 28 64 62 2c 20 69 44 62 29 3b 0a  etAll(db, iDb);.
18c86 0a 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65  .exit_drop_table
18c87 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
18c88 73 74 44 65 6c 65 74 65 28 70 4e 61 6d 65 29 3b  stDelete(pName);
18c89 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
18c8a 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
18c8b 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
18c8c 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20   foreign key on 
18c8d 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72  the table.** cur
18c8e 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
18c8f 73 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f  struction.  pFro
18c90 6d 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 20  mCol determines 
18c91 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  which columns.**
18c92 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
18c93 74 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74  table point to t
18c94 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20  he foreign key. 
18c95 20 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20   If pFromCol==0 
18c96 74 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20  then.** connect 
18c97 74 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20 6c  the key to the l
18c98 61 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72  ast column inser
18c99 74 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68 65  ted.  pTo is the
18c9a 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20   name of.** the 
18c9b 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74  table referred t
18c9c 6f 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 20  o.  pToCol is a 
18c9d 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69  list of tables i
18c9e 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70  n the other.** p
18c9f 54 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74 68  To table that th
18ca0 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f  e foreign key po
18ca1 69 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 20  ints to.  flags 
18ca2 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20  contains all.** 
18ca3 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
18ca4 74 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 72  t the conflict r
18ca5 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69  esolution algori
18ca6 74 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a 2a  thms specified.*
18ca7 2a 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45  * in the ON DELE
18ca8 54 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 6e  TE, ON UPDATE an
18ca9 64 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 75  d ON INSERT clau
18caa 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b  ses..**.** An FK
18cab 65 79 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ey structure is 
18cac 63 72 65 61 74 65 64 20 61 6e 64 20 61 64 64 65  created and adde
18cad 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63  d to the table c
18cae 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65  urrently.** unde
18caf 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69  r construction i
18cb0 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e  n the pParse->pN
18cb1 65 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 20 20  ewTable field.  
18cb2 54 68 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a 20  The new FKey.** 
18cb3 69 73 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69 6e  is not linked in
18cb4 74 6f 20 64 62 2d 3e 61 46 4b 65 79 20 61 74 20  to db->aFKey at 
18cb5 74 68 69 73 20 70 6f 69 6e 74 20 2d 20 74 68 61  this point - tha
18cb6 74 20 64 6f 65 73 20 6e 6f 74 20 68 61 70 70 65  t does not happe
18cb7 6e 0a 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69 74  n.** until sqlit
18cb8 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a 2a  e3EndTable()..**
18cb9 0a 2a 2a 20 54 68 65 20 66 6f 72 65 69 67 6e 20  .** The foreign 
18cba 6b 65 79 20 69 73 20 73 65 74 20 66 6f 72 20 49  key is set for I
18cbb 4d 4d 45 44 49 41 54 45 20 70 72 6f 63 65 73 73  MMEDIATE process
18cbc 69 6e 67 2e 20 20 41 20 73 75 62 73 65 71 75 65  ing.  A subseque
18cbd 6e 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71  nt call.** to sq
18cbe 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67  lite3DeferForeig
18cbf 6e 4b 65 79 28 29 20 6d 69 67 68 74 20 63 68 61  nKey() might cha
18cc0 6e 67 65 20 74 68 69 73 20 74 6f 20 44 45 46 45  nge this to DEFE
18cc1 52 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  RRED..*/.SQLITE_
18cc2 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
18cc3 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67  ite3CreateForeig
18cc4 6e 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70  nKey(.  Parse *p
18cc5 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
18cc6 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
18cc7 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
18cc8 46 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c  FromCol,  /* Col
18cc9 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62  umns in this tab
18cca 6c 65 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f  le that point to
18ccb 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a   other table */.
18ccc 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20    Token *pTo,   
18ccd 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
18cce 66 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c  f the other tabl
18ccf 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
18cd0 2a 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43  *pToCol,    /* C
18cd1 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74  olumns in the ot
18cd2 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  her table */.  i
18cd3 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 20  nt flags        
18cd4 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20      /* Conflict 
18cd5 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72  resolution algor
18cd6 69 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 23 69 66  ithms. */.){.#if
18cd7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18cd8 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 46  _FOREIGN_KEY.  F
18cd9 4b 65 79 20 2a 70 46 4b 65 79 20 3d 20 30 3b 0a  Key *pFKey = 0;.
18cda 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61    Table *p = pPa
18cdb 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
18cdc 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69    int nByte;.  i
18cdd 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c  nt i;.  int nCol
18cde 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20  ;.  char *z;..  
18cdf 61 73 73 65 72 74 28 20 70 54 6f 21 3d 30 20 29  assert( pTo!=0 )
18ce0 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
18ce1 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
18ce2 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
18ce3 29 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20  ) goto fk_end;. 
18ce4 20 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30   if( pFromCol==0
18ce5 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   ){.    int iCol
18ce6 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20   = p->nCol-1;.  
18ce7 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 67    if( iCol<0 ) g
18ce8 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20  oto fk_end;.    
18ce9 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54  if( pToCol && pT
18cea 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29  oCol->nExpr!=1 )
18ceb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
18cec 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
18ced 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20  "foreign key on 
18cee 25 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73  %s".         " s
18cef 68 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20  hould reference 
18cf0 6f 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20  only one column 
18cf1 6f 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20  of table %T",.  
18cf2 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69         p->aCol[i
18cf3 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29  Col].zName, pTo)
18cf4 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f  ;.      goto fk_
18cf5 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  end;.    }.    n
18cf6 43 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  Col = 1;.  }else
18cf7 20 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70   if( pToCol && p
18cf8 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46  ToCol->nExpr!=pF
18cf9 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b  romCol->nExpr ){
18cfa 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
18cfb 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20  rMsg(pParse,.   
18cfc 20 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20       "number of 
18cfd 63 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69  columns in forei
18cfe 67 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20  gn key does not 
18cff 6d 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72  match the number
18d00 20 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63   of ".        "c
18d01 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65  olumns in the re
18d02 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29  ferenced table")
18d03 3b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e  ;.    goto fk_en
18d04 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  d;.  }else{.    
18d05 6e 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d  nCol = pFromCol-
18d06 3e 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42  >nExpr;.  }.  nB
18d07 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46  yte = sizeof(*pF
18d08 4b 65 79 29 20 2b 20 6e 43 6f 6c 2a 73 69 7a 65  Key) + nCol*size
18d09 6f 66 28 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30  of(pFKey->aCol[0
18d0a 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b 20 31 3b  ]) + pTo->n + 1;
18d0b 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b  .  if( pToCol ){
18d0c 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
18d0d 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 3b 20 69  pToCol->nExpr; i
18d0e 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65  ++){.      nByte
18d0f 20 2b 3d 20 73 74 72 6c 65 6e 28 70 54 6f 43 6f   += strlen(pToCo
18d10 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b  l->a[i].zName) +
18d11 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
18d12 70 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 44  pFKey = sqlite3D
18d13 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72  bMallocZero(pPar
18d14 73 65 2d 3e 64 62 2c 20 6e 42 79 74 65 20 29 3b  se->db, nByte );
18d15 0a 20 20 69 66 28 20 70 46 4b 65 79 3d 3d 30 20  .  if( pFKey==0 
18d16 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65  ){.    goto fk_e
18d17 6e 64 3b 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d  nd;.  }.  pFKey-
18d18 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a 20 20 70 46  >pFrom = p;.  pF
18d19 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 3d  Key->pNextFrom =
18d1a 20 70 2d 3e 70 46 4b 65 79 3b 0a 20 20 7a 20 3d   p->pFKey;.  z =
18d1b 20 28 63 68 61 72 2a 29 26 70 46 4b 65 79 5b 31   (char*)&pFKey[1
18d1c 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  ];.  pFKey->aCol
18d1d 20 3d 20 28 73 74 72 75 63 74 20 73 43 6f 6c 4d   = (struct sColM
18d1e 61 70 2a 29 7a 3b 0a 20 20 7a 20 2b 3d 20 73 69  ap*)z;.  z += si
18d1f 7a 65 6f 66 28 73 74 72 75 63 74 20 73 43 6f 6c  zeof(struct sCol
18d20 4d 61 70 29 2a 6e 43 6f 6c 3b 0a 20 20 70 46 4b  Map)*nCol;.  pFK
18d21 65 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20 20 6d  ey->zTo = z;.  m
18d22 65 6d 63 70 79 28 7a 2c 20 70 54 6f 2d 3e 7a 2c  emcpy(z, pTo->z,
18d23 20 70 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b 70 54   pTo->n);.  z[pT
18d24 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 7a 20 2b  o->n] = 0;.  z +
18d25 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 70 46  = pTo->n+1;.  pF
18d26 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 30  Key->pNextTo = 0
18d27 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 20  ;.  pFKey->nCol 
18d28 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66 28 20 70 46  = nCol;.  if( pF
18d29 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20  romCol==0 ){.   
18d2a 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e   pFKey->aCol[0].
18d2b 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e 43 6f 6c 2d  iFrom = p->nCol-
18d2c 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
18d2d 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
18d2e 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74   i++){.      int
18d2f 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d   j;.      for(j=
18d30 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b  0; j<p->nCol; j+
18d31 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
18d32 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
18d33 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c  ->aCol[j].zName,
18d34 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e   pFromCol->a[i].
18d35 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
18d36 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 61 43         pFKey->aC
18d37 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 3d 20 6a 3b  ol[i].iFrom = j;
18d38 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
18d39 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
18d3a 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3e    }.      if( j>
18d3b 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20  =p->nCol ){.    
18d3c 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
18d3d 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
18d3e 20 20 20 20 20 20 20 22 75 6e 6b 6e 6f 77 6e 20         "unknown 
18d3f 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c 22 20 69 6e  column \"%s\" in
18d40 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66   foreign key def
18d41 69 6e 69 74 69 6f 6e 22 2c 20 0a 20 20 20 20 20  inition", .     
18d42 20 20 20 20 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61       pFromCol->a
18d43 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
18d44 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b      goto fk_end;
18d45 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
18d46 20 7d 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 20   }.  if( pToCol 
18d47 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
18d48 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
18d49 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c      int n = strl
18d4a 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e  en(pToCol->a[i].
18d4b 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 46  zName);.      pF
18d4c 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f  Key->aCol[i].zCo
18d4d 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d  l = z;.      mem
18d4e 63 70 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61  cpy(z, pToCol->a
18d4f 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20  [i].zName, n);. 
18d50 20 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20       z[n] = 0;. 
18d51 20 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20       z += n+1;. 
18d52 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79     }.  }.  pFKey
18d53 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 30  ->isDeferred = 0
18d54 3b 0a 20 20 70 46 4b 65 79 2d 3e 64 65 6c 65 74  ;.  pFKey->delet
18d55 65 43 6f 6e 66 20 3d 20 66 6c 61 67 73 20 26 20  eConf = flags & 
18d56 30 78 66 66 3b 0a 20 20 70 46 4b 65 79 2d 3e 75  0xff;.  pFKey->u
18d57 70 64 61 74 65 43 6f 6e 66 20 3d 20 28 66 6c 61  pdateConf = (fla
18d58 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78 66 66  gs >> 8 ) & 0xff
18d59 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e 73 65 72  ;.  pFKey->inser
18d5a 74 43 6f 6e 66 20 3d 20 28 66 6c 61 67 73 20 3e  tConf = (flags >
18d5b 3e 20 31 36 20 29 20 26 20 30 78 66 66 3b 0a 0a  > 16 ) & 0xff;..
18d5c 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 66 6f    /* Link the fo
18d5d 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 74 68 65  reign key to the
18d5e 20 74 61 62 6c 65 20 61 73 20 74 68 65 20 6c 61   table as the la
18d5f 73 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a 20 20  st step..  */.  
18d60 70 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b 65 79  p->pFKey = pFKey
18d61 3b 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b 0a 0a  ;.  pFKey = 0;..
18d62 66 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65  fk_end:.  sqlite
18d63 33 5f 66 72 65 65 28 70 46 4b 65 79 29 3b 0a 23  3_free(pFKey);.#
18d64 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
18d65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  d(SQLITE_OMIT_FO
18d66 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20  REIGN_KEY) */.  
18d67 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
18d68 65 6c 65 74 65 28 70 46 72 6f 6d 43 6f 6c 29 3b  elete(pFromCol);
18d69 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
18d6a 73 74 44 65 6c 65 74 65 28 70 54 6f 43 6f 6c 29  stDelete(pToCol)
18d6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
18d6c 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
18d6d 64 20 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41  d when an INITIA
18d6e 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72  LLY IMMEDIATE or
18d6f 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
18d70 52 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73  RED.** clause is
18d71 20 73 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66   seen as part of
18d72 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64   a foreign key d
18d73 65 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20  efinition.  The 
18d74 69 73 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61  isDeferred.** pa
18d75 72 61 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72  rameter is 1 for
18d76 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
18d77 52 45 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e  RED and 0 for IN
18d78 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54  ITIALLY IMMEDIAT
18d79 45 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69  E..** The behavi
18d7a 6f 72 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  or of the most r
18d7b 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20  ecently created 
18d7c 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61  foreign key is a
18d7d 64 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72  djusted.** accor
18d7e 64 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  dingly..*/.SQLIT
18d7f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
18d80 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69  qlite3DeferForei
18d81 67 6e 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61  gnKey(Parse *pPa
18d82 72 73 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72  rse, int isDefer
18d83 72 65 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  red){.#ifndef SQ
18d84 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
18d85 4e 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70  N_KEY.  Table *p
18d86 54 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b  Tab;.  FKey *pFK
18d87 65 79 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20  ey;.  if( (pTab 
18d88 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
18d89 62 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65  ble)==0 || (pFKe
18d8a 79 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29  y = pTab->pFKey)
18d8b 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
18d8c 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
18d8d 64 20 3d 20 69 73 44 65 66 65 72 72 65 64 3b 0a  d = isDeferred;.
18d8e 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
18d8f 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
18d90 61 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e  at will erase an
18d91 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a  d refill index *
18d92 70 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a  pIdx.  This is.*
18d93 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61  * used to initia
18d94 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65  lize a newly cre
18d95 61 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f  ated index or to
18d96 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a   recompute the.*
18d97 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20  * content of an 
18d98 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  index in respons
18d99 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63  e to a REINDEX c
18d9a 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66  ommand..**.** if
18d9b 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20   memRootPage is 
18d9c 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74  not negative, it
18d9d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
18d9e 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a  index is newly.*
18d9f 2a 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20  * created.  The 
18da0 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 73 70 65 63  memory cell spec
18da1 69 66 69 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74  ified by memRoot
18da2 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  Page contains th
18da3 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6e  e.** root page n
18da4 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64  umber of the ind
18da5 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f 6f 74 50  ex.  If memRootP
18da6 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c  age is negative,
18da7 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 64   then.** the ind
18da8 65 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ex already exist
18da9 73 20 61 6e 64 20 6d 75 73 74 20 62 65 20 63 6c  s and must be cl
18daa 65 61 72 65 64 20 62 65 66 6f 72 65 20 62 65 69  eared before bei
18dab 6e 67 20 72 65 66 69 6c 6c 65 64 20 61 6e 64 0a  ng refilled and.
18dac 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  ** the root page
18dad 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69   number of the i
18dae 6e 64 65 78 20 69 73 20 74 61 6b 65 6e 20 66 72  ndex is taken fr
18daf 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e  om pIndex->tnum.
18db0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
18db1 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64  sqlite3RefillInd
18db2 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ex(Parse *pParse
18db3 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c  , Index *pIndex,
18db4 20 69 6e 74 20 6d 65 6d 52 6f 6f 74 50 61 67 65   int memRootPage
18db5 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
18db6 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c   = pIndex->pTabl
18db7 65 3b 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65  e;  /* The table
18db8 20 74 68 61 74 20 69 73 20 69 6e 64 65 78 65 64   that is indexed
18db9 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d   */.  int iTab =
18dba 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 20 20   pParse->nTab;  
18dbb 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75       /* Btree cu
18dbc 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20 70 54  rsor used for pT
18dbd 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78  ab */.  int iIdx
18dbe 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
18dbf 31 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20  1;     /* Btree 
18dc0 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20  cursor used for 
18dc1 70 49 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20  pIndex */.  int 
18dc2 61 64 64 72 31 3b 20 20 20 20 20 20 20 20 20 20  addr1;          
18dc3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
18dc4 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f 66 20  dress of top of 
18dc5 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 74 6e  loop */.  int tn
18dc6 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
18dc7 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
18dc8 20 70 61 67 65 20 6f 66 20 69 6e 64 65 78 20 2a   page of index *
18dc9 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
18dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18dcb 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
18dcc 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69  ode into this vi
18dcd 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f  rtual machine */
18dce 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
18dcf 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
18dd0 20 20 2f 2a 20 4b 65 79 49 6e 66 6f 20 66 6f 72    /* KeyInfo for
18dd1 20 69 6e 64 65 78 20 2a 2f 0a 20 20 73 71 6c 69   index */.  sqli
18dd2 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
18dd3 2d 3e 64 62 3b 20 20 20 20 20 20 2f 2a 20 54 68  ->db;      /* Th
18dd4 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
18dd5 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ction */.  int i
18dd6 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
18dd7 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49  maToIndex(db, pI
18dd8 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  ndex->pSchema);.
18dd9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18dda 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
18ddb 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ON.  if( sqlite3
18ddc 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
18ddd 2c 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 45 58  , SQLITE_REINDEX
18dde 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c  , pIndex->zName,
18ddf 20 30 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44   0,.      db->aD
18de0 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29  b[iDb].zName ) )
18de1 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
18de2 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52  }.#endif..  /* R
18de3 65 71 75 69 72 65 20 61 20 77 72 69 74 65 2d 6c  equire a write-l
18de4 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ock on the table
18de5 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 69 73   to perform this
18de6 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
18de7 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
18de8 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
18de9 61 62 2d 3e 74 6e 75 6d 2c 20 31 2c 20 70 54 61  ab->tnum, 1, pTa
18dea 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 76 20  b->zName);..  v 
18deb 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
18dec 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
18ded 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  v==0 ) return;. 
18dee 20 69 66 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65   if( memRootPage
18def 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
18df0 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
18df1 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d 65 6d 52 6f  P_MemLoad, memRo
18df2 6f 74 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20  otPage, 0);.    
18df3 74 6e 75 6d 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  tnum = 0;.  }els
18df4 65 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20 70 49  e{.    tnum = pI
18df5 6e 64 65 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20  ndex->tnum;.    
18df6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
18df7 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 74 6e  (v, OP_Clear, tn
18df8 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20  um, iDb);.  }.  
18df9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
18dfa 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
18dfb 69 44 62 2c 20 30 29 3b 0a 20 20 70 4b 65 79 20  iDb, 0);.  pKey 
18dfc 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65  = sqlite3IndexKe
18dfd 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49  yinfo(pParse, pI
18dfe 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  ndex);.  sqlite3
18dff 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  VdbeOp3(v, OP_Op
18e00 65 6e 57 72 69 74 65 2c 20 69 49 64 78 2c 20 74  enWrite, iIdx, t
18e01 6e 75 6d 2c 20 28 63 68 61 72 20 2a 29 70 4b 65  num, (char *)pKe
18e02 79 2c 20 50 33 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P3_KEYINFO_HA
18e03 4e 44 4f 46 46 29 3b 0a 20 20 73 71 6c 69 74 65  NDOFF);.  sqlite
18e04 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
18e05 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54  e, iTab, iDb, pT
18e06 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
18e07 3b 0a 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69  ;.  addr1 = sqli
18e08 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
18e09 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62 2c  OP_Rewind, iTab,
18e0a 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 65   0);.  sqlite3Ge
18e0b 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 76  nerateIndexKey(v
18e0c 2c 20 70 49 6e 64 65 78 2c 20 69 54 61 62 29 3b  , pIndex, iTab);
18e0d 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 6f  .  if( pIndex->o
18e0e 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
18e0f 29 7b 0a 20 20 20 20 69 6e 74 20 63 75 72 61 64  ){.    int curad
18e10 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
18e11 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
18e12 20 20 20 20 69 6e 74 20 61 64 64 72 32 20 3d 20      int addr2 = 
18e13 63 75 72 61 64 64 72 2b 34 3b 0a 20 20 20 20 73  curaddr+4;.    s
18e14 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
18e15 50 32 28 76 2c 20 63 75 72 61 64 64 72 2d 31 2c  P2(v, curaddr-1,
18e16 20 61 64 64 72 32 29 3b 0a 20 20 20 20 73 71 6c   addr2);.    sql
18e17 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
18e18 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61 62 2c   OP_Rowid, iTab,
18e19 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
18e1a 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
18e1b 41 64 64 49 6d 6d 2c 20 31 2c 20 30 29 3b 0a 20  AddImm, 1, 0);. 
18e1c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
18e1d 64 4f 70 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71  dOp(v, OP_IsUniq
18e1e 75 65 2c 20 69 49 64 78 2c 20 61 64 64 72 32 29  ue, iIdx, addr2)
18e1f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
18e20 65 4f 70 33 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  eOp3(v, OP_Halt,
18e21 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
18e22 4e 54 2c 20 4f 45 5f 41 62 6f 72 74 2c 0a 20 20  NT, OE_Abort,.  
18e23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18e24 20 20 22 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d    "indexed colum
18e25 6e 73 20 61 72 65 20 6e 6f 74 20 75 6e 69 71 75  ns are not uniqu
18e26 65 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a  e", P3_STATIC);.
18e27 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
18e28 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
18e29 61 64 64 72 32 3d 3d 73 71 6c 69 74 65 33 56 64  addr2==sqlite3Vd
18e2a 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
18e2b 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   );.  }.  sqlite
18e2c 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
18e2d 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64 78  _IdxInsert, iIdx
18e2e 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 0);.  sqlite3V
18e2f 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e  dbeAddOp(v, OP_N
18e30 65 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 31  ext, iTab, addr1
18e31 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  +1);.  sqlite3Vd
18e32 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
18e33 64 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  dr1);.  sqlite3V
18e34 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
18e35 6c 6f 73 65 2c 20 69 54 61 62 2c 20 30 29 3b 0a  lose, iTab, 0);.
18e36 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
18e37 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  Op(v, OP_Close, 
18e38 69 49 64 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  iIdx, 0);.}../*.
18e39 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
18e3a 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53 51 4c  index for an SQL
18e3b 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65 31 2e   table.  pName1.
18e3c 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20 6e 61  pName2 is the na
18e3d 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  me of the index 
18e3e 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69 73 74  .** and pTblList
18e3f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
18e40 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69  the table that i
18e41 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e  s to be indexed.
18e42 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a 2a 20    Both will .** 
18e43 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20 70 72  be NULL for a pr
18e44 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61 6e 20  imary key or an 
18e45 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 63 72  index that is cr
18e46 65 61 74 65 64 20 74 6f 20 73 61 74 69 73 66 79  eated to satisfy
18e47 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63 6f 6e   a.** UNIQUE con
18e48 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70 54 61  straint.  If pTa
18e49 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78 20 61  ble and pIndex a
18e4a 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70 50 61  re NULL, use pPa
18e4b 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 0a 2a  rse->pNewTable.*
18e4c 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 74  * as the table t
18e4d 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 70  o be indexed.  p
18e4e 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
18e4f 20 69 73 20 61 20 74 61 62 6c 65 20 74 68 61 74   is a table that
18e50 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79   is.** currently
18e51 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74   being construct
18e52 65 64 20 62 79 20 61 20 43 52 45 41 54 45 20 54  ed by a CREATE T
18e53 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
18e54 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73 20 61  **.** pList is a
18e55 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
18e56 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
18e57 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65 20 4e   pList will be N
18e58 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a 20 69  ULL if this.** i
18e59 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20  s a primary key 
18e5a 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73 74 72  or unique-constr
18e5b 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f 73 74  aint on the most
18e5c 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 61   recent column a
18e5d 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 74  dded.** to the t
18e5e 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
18e5f 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
18e60 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  n.  .*/.SQLITE_P
18e61 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
18e62 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 0a  te3CreateIndex(.
18e63 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
18e64 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e 66 6f       /* All info
18e65 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
18e66 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 54 6f  is parse */.  To
18e67 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20  ken *pName1,    
18e68 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f   /* First part o
18e69 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61  f index name. Ma
18e6a 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54  y be NULL */.  T
18e6b 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
18e6c 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74    /* Second part
18e6d 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20   of index name. 
18e6e 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
18e6f 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c 4e 61   SrcList *pTblNa
18e70 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20  me, /* Table to 
18e71 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61 72 73  index. Use pPars
18e72 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 66 20  e->pNewTable if 
18e73 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  0 */.  ExprList 
18e74 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 20 6c  *pList,   /* A l
18e75 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74  ist of columns t
18e76 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a  o be indexed */.
18e77 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
18e78 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74       /* OE_Abort
18e79 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f  , OE_Ignore, OE_
18e7a 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e  Replace, or OE_N
18e7b 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  one */.  Token *
18e7c 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a 20 54  pStart,     /* T
18e7d 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20  he CREATE token 
18e7e 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 69 73  that begins this
18e7f 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
18e80 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20  Token *pEnd,    
18e81 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20 74 68     /* The ")" th
18e82 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20 43 52  at closes the CR
18e83 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65  EATE INDEX state
18e84 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f  ment */.  int so
18e85 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f 2a 20  rtOrder,     /* 
18e86 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 70 72  Sort order of pr
18e87 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e 20 70  imary key when p
18e88 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20 20  List==NULL */.  
18e89 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 20 20  int ifNotExist  
18e8a 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f 72     /* Omit error
18e8b 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65 61 64   if index alread
18e8c 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
18e8d 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30   Table *pTab = 0
18e8e 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74  ;     /* Table t
18e8f 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a  o be indexed */.
18e90 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 20    Index *pIndex 
18e91 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e  = 0;   /* The in
18e92 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74 65  dex to be create
18e93 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  d */.  char *zNa
18e94 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e  me = 0;     /* N
18e95 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ame of the index
18e96 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b   */.  int nName;
18e97 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
18e98 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
18e99 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20  rs in zName */. 
18e9a 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f 6b   int i, j;.  Tok
18e9b 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20 20  en nullId;      
18e9c 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e 20    /* Fake token 
18e9d 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 44 20  for an empty ID 
18e9e 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 78 65  list */.  DbFixe
18e9f 72 20 73 46 69 78 3b 20 20 20 20 20 20 20 20 2f  r sFix;        /
18ea0 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e 67 20  * For assigning 
18ea1 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73 20 74  database names t
18ea2 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  o pTable */.  in
18ea3 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b  t sortOrderMask;
18ea4 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f 72     /* 1 to honor
18ea5 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78 2e 20   DESC in index. 
18ea6 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a 2f   0 to ignore. */
18ea7 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
18ea8 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
18ea9 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20  b *pDb;         
18eaa 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69      /* The speci
18eab 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  fic table contai
18eac 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65 64  ning the indexed
18ead 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
18eae 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
18eaf 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
18eb0 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61  the database tha
18eb1 74 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74  t is being writt
18eb2 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  en */.  Token *p
18eb3 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a 20  Name = 0;    /* 
18eb4 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65  Unqualified name
18eb5 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74 6f   of the index to
18eb6 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 74 72   create */.  str
18eb7 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
18eb8 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f 2a  m *pListItem; /*
18eb9 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
18eba 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  r pList */.  int
18ebb 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45 78   nCol;.  int nEx
18ebc 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  tra = 0;.  char 
18ebd 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 69 66 28 20  *zExtra;..  if( 
18ebe 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
18ebf 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
18ec0 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56   || IN_DECLARE_V
18ec1 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  TAB ){.    goto 
18ec2 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
18ec3 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  x;.  }..  /*.  *
18ec4 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62 6c 65  * Find the table
18ec5 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69   that is to be i
18ec6 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72 6e 20  ndexed.  Return 
18ec7 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75  early if not fou
18ec8 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  nd..  */.  if( p
18ec9 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20  TblName!=0 ){.. 
18eca 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74 77     /* Use the tw
18ecb 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61 6d  o-part index nam
18ecc 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  e to determine t
18ecd 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 20  he database .   
18ece 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66 6f   ** to search fo
18ecf 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46 69  r the table. 'Fi
18ed0 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  x' the table nam
18ed1 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20 20 20  e to this db.   
18ed2 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69   ** before looki
18ed3 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c 65 2e  ng up the table.
18ed4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
18ed5 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20 70 4e  rt( pName1 && pN
18ed6 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 62 20  ame2 );.    iDb 
18ed7 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
18ed8 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
18ed9 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
18eda 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44  ame);.    if( iD
18edb 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  b<0 ) goto exit_
18edc 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 23  create_index;..#
18edd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18ede 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a  IT_TEMPDB.    /*
18edf 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61   If the index na
18ee0 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69  me was unqualifi
18ee1 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74 68 65  ed, check if the
18ee2 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a   the table.    *
18ee3 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c  * is a temp tabl
18ee4 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68  e. If so, set th
18ee5 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 31 2e  e database to 1.
18ee6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 61 62  .    */.    pTab
18ee7 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
18ee8 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  tLookup(pParse, 
18ee9 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 69  pTblName);.    i
18eea 66 28 20 70 4e 61 6d 65 32 20 26 26 20 70 4e 61  f( pName2 && pNa
18eeb 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61  me2->n==0 && pTa
18eec 62 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65  b && pTab->pSche
18eed 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ma==db->aDb[1].p
18eee 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20  Schema ){.      
18eef 69 44 62 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23  iDb = 1;.    }.#
18ef0 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 73  endif..    if( s
18ef1 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73  qlite3FixInit(&s
18ef2 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62  Fix, pParse, iDb
18ef3 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e 61 6d 65  , "index", pName
18ef4 29 20 26 26 0a 20 20 20 20 20 20 20 20 73 71 6c  ) &&.        sql
18ef5 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 26  ite3FixSrcList(&
18ef6 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65 29 0a  sFix, pTblName).
18ef7 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20      ){.      /* 
18ef8 42 65 63 61 75 73 65 20 74 68 65 20 70 61 72 73  Because the pars
18ef9 65 72 20 63 6f 6e 73 74 72 75 63 74 73 20 70 54  er constructs pT
18efa 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20 73 69  blName from a si
18efb 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c  ngle identifier,
18efc 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
18efd 33 46 69 78 53 72 63 4c 69 73 74 20 63 61 6e 20  3FixSrcList can 
18efe 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f 0a 20  never fail. */. 
18eff 20 20 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a       assert(0);.
18f00 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 20 3d      }.    pTab =
18f01 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
18f02 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 62 6c  ble(pParse, pTbl
18f03 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  Name->a[0].zName
18f04 2c 20 0a 20 20 20 20 20 20 20 20 70 54 62 6c 4e  , .        pTblN
18f05 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62  ame->a[0].zDatab
18f06 61 73 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70  ase);.    if( !p
18f07 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  Tab ) goto exit_
18f08 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
18f09 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
18f0a 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3d 3d  b[iDb].pSchema==
18f0b 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
18f0c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
18f0d 73 65 72 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29  sert( pName==0 )
18f0e 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50 61  ;.    pTab = pPa
18f0f 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
18f10 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 20      if( !pTab ) 
18f11 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
18f12 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 44 62 20  _index;.    iDb 
18f13 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
18f14 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
18f15 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7d 0a 20  >pSchema);.  }. 
18f16 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
18f17 69 44 62 5d 3b 0a 0a 20 20 69 66 28 20 70 54 61  iDb];..  if( pTa
18f18 62 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e  b==0 || pParse->
18f19 6e 45 72 72 20 29 20 67 6f 74 6f 20 65 78 69 74  nErr ) goto exit
18f1a 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
18f1b 20 69 66 28 20 70 54 61 62 2d 3e 72 65 61 64 4f   if( pTab->readO
18f1c 6e 6c 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nly ){.    sqlit
18f1d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
18f1e 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79  e, "table %s may
18f1f 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22   not be indexed"
18f20 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
18f21 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
18f22 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  eate_index;.  }.
18f23 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18f24 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70  MIT_VIEW.  if( p
18f25 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
18f26 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
18f27 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65  Msg(pParse, "vie
18f28 77 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e  ws may not be in
18f29 64 65 78 65 64 22 29 3b 0a 20 20 20 20 67 6f 74  dexed");.    got
18f2a 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
18f2b 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  dex;.  }.#endif.
18f2c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18f2d 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
18f2e 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
18f2f 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71  (pTab) ){.    sq
18f30 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
18f31 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74  arse, "virtual t
18f32 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65  ables may not be
18f33 20 69 6e 64 65 78 65 64 22 29 3b 0a 20 20 20 20   indexed");.    
18f34 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
18f35 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64  _index;.  }.#end
18f36 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69  if..  /*.  ** Fi
18f37 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  nd the name of t
18f38 68 65 20 69 6e 64 65 78 2e 20 20 4d 61 6b 65 20  he index.  Make 
18f39 73 75 72 65 20 74 68 65 72 65 20 69 73 20 6e 6f  sure there is no
18f3a 74 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65  t already anothe
18f3b 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20  r.  ** index or 
18f3c 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 73  table with the s
18f3d 61 6d 65 20 6e 61 6d 65 2e 20 20 0a 20 20 2a 2a  ame name.  .  **
18f3e 0a 20 20 2a 2a 20 45 78 63 65 70 74 69 6f 6e 3a  .  ** Exception:
18f3f 20 20 49 66 20 77 65 20 61 72 65 20 72 65 61 64    If we are read
18f40 69 6e 67 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ing the names of
18f41 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e 64 69 63   permanent indic
18f42 65 73 20 66 72 6f 6d 20 74 68 65 0a 20 20 2a 2a  es from the.  **
18f43 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
18f44 61 62 6c 65 20 28 62 65 63 61 75 73 65 20 73 6f  able (because so
18f45 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
18f46 20 63 68 61 6e 67 65 64 20 74 68 65 20 73 63 68   changed the sch
18f47 65 6d 61 29 20 61 6e 64 0a 20 20 2a 2a 20 6f 6e  ema) and.  ** on
18f48 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6e  e of the index n
18f49 61 6d 65 73 20 63 6f 6c 6c 69 64 65 73 20 77 69  ames collides wi
18f4a 74 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  th the name of a
18f4b 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
18f4c 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65 78 2c 20   or.  ** index, 
18f4d 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 63 6f 6e  then we will con
18f4e 74 69 6e 75 65 20 74 6f 20 70 72 6f 63 65 73 73  tinue to process
18f4f 20 74 68 69 73 20 69 6e 64 65 78 2e 0a 20 20 2a   this index..  *
18f50 2a 0a 20 20 2a 2a 20 49 66 20 70 4e 61 6d 65 3d  *.  ** If pName=
18f51 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  =0 it means that
18f52 20 77 65 20 61 72 65 0a 20 20 2a 2a 20 64 65 61   we are.  ** dea
18f53 6c 69 6e 67 20 77 69 74 68 20 61 20 70 72 69 6d  ling with a prim
18f54 61 72 79 20 6b 65 79 20 6f 72 20 55 4e 49 51 55  ary key or UNIQU
18f55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 57  E constraint.  W
18f56 65 20 68 61 76 65 20 74 6f 20 69 6e 76 65 6e 74  e have to invent
18f57 20 6f 75 72 0a 20 20 2a 2a 20 6f 77 6e 20 6e 61   our.  ** own na
18f58 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  me..  */.  if( p
18f59 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d  Name ){.    zNam
18f5a 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
18f5b 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61  romToken(db, pNa
18f5c 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  me);.    if( SQL
18f5d 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52  ITE_OK!=sqlite3R
18f5e 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
18f5f 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72  ) ) goto exit_cr
18f60 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
18f61 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 67  if( zName==0 ) g
18f62 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
18f63 69 6e 64 65 78 3b 0a 20 20 20 20 69 66 28 20 53  index;.    if( S
18f64 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
18f65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
18f66 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20  (pParse, zName) 
18f67 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
18f68 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
18f69 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
18f6a 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
18f6b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
18f6c 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
18f6d 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
18f6e 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61  ) goto exit_crea
18f6f 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20  te_index;.      
18f70 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54  if( sqlite3FindT
18f71 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  able(db, zName, 
18f72 30 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  0)!=0 ){.       
18f73 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
18f74 28 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 20  (pParse, "there 
18f75 69 73 20 61 6c 72 65 61 64 79 20 61 20 74 61 62  is already a tab
18f76 6c 65 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e  le named %s", zN
18f77 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f  ame);.        go
18f78 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
18f79 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ndex;.      }.  
18f7a 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
18f7b 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
18f7c 20 7a 4e 61 6d 65 2c 20 70 44 62 2d 3e 7a 4e 61   zName, pDb->zNa
18f7d 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)!=0 ){.      
18f7e 69 66 28 20 21 69 66 4e 6f 74 45 78 69 73 74 20  if( !ifNotExist 
18f7f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
18f80 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
18f81 65 2c 20 22 69 6e 64 65 78 20 25 73 20 61 6c 72  e, "index %s alr
18f82 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 7a 4e  eady exists", zN
18f83 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ame);.      }.  
18f84 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
18f85 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
18f86 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  }.  }else{.    c
18f87 68 61 72 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20  har zBuf[30];.  
18f88 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 49 6e 64    int n;.    Ind
18f89 65 78 20 2a 70 4c 6f 6f 70 3b 0a 20 20 20 20 66  ex *pLoop;.    f
18f8a 6f 72 28 70 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70  or(pLoop=pTab->p
18f8b 49 6e 64 65 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f  Index, n=1; pLoo
18f8c 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e  p; pLoop=pLoop->
18f8d 70 4e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20  pNext, n++){}.  
18f8e 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
18f8f 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c  tf(sizeof(zBuf),
18f90 7a 42 75 66 2c 22 5f 25 64 22 2c 6e 29 3b 0a 20  zBuf,"_%d",n);. 
18f91 20 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20     zName = 0;.  
18f92 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
18f93 6e 67 28 26 7a 4e 61 6d 65 2c 20 22 73 71 6c 69  ng(&zName, "sqli
18f94 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 22 2c 20  te_autoindex_", 
18f95 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 42 75  pTab->zName, zBu
18f96 66 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  f, (char*)0);.  
18f97 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
18f98 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
18f99 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
18f9a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
18f9b 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
18f9c 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  }.  }..  /* Chec
18f9d 6b 20 66 6f 72 20 61 75 74 68 6f 72 69 7a 61 74  k for authorizat
18f9e 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61 6e  ion to create an
18f9f 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23 69 66   index..  */.#if
18fa0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18fa1 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
18fa2 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
18fa3 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e  r *zDb = pDb->zN
18fa4 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ame;.    if( sql
18fa5 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
18fa6 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53  arse, SQLITE_INS
18fa7 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ERT, SCHEMA_TABL
18fa8 45 28 69 44 62 29 2c 20 30 2c 20 7a 44 62 29 20  E(iDb), 0, zDb) 
18fa9 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
18faa 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
18fab 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 53  .    }.    i = S
18fac 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44  QLITE_CREATE_IND
18fad 45 58 3b 0a 20 20 20 20 69 66 28 20 21 4f 4d 49  EX;.    if( !OMI
18fae 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
18faf 3d 31 20 29 20 69 20 3d 20 53 51 4c 49 54 45 5f  =1 ) i = SQLITE_
18fb0 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45  CREATE_TEMP_INDE
18fb1 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  X;.    if( sqlit
18fb2 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
18fb3 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20 70 54  se, i, zName, pT
18fb4 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  ab->zName, zDb) 
18fb5 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
18fb6 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
18fb7 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
18fb8 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c 69 73 74  f..  /* If pList
18fb9 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  ==0, it means th
18fba 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63  is routine was c
18fbb 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65 20 61 20  alled to make a 
18fbc 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79  primary.  ** key
18fbd 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 61 73 74   out of the last
18fbe 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64 20 74 6f   column added to
18fbf 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72   the table under
18fc0 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20   construction.. 
18fc1 20 2a 2a 20 53 6f 20 63 72 65 61 74 65 20 61 20   ** So create a 
18fc2 66 61 6b 65 20 6c 69 73 74 20 74 6f 20 73 69 6d  fake list to sim
18fc3 75 6c 61 74 65 20 74 68 69 73 2e 0a 20 20 2a 2f  ulate this..  */
18fc4 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
18fc5 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 7a 20  ){.    nullId.z 
18fc6 3d 20 28 75 38 2a 29 70 54 61 62 2d 3e 61 43 6f  = (u8*)pTab->aCo
18fc7 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 5d 2e  l[pTab->nCol-1].
18fc8 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75 6c 6c 49  zName;.    nullI
18fc9 64 2e 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63 68  d.n = strlen((ch
18fca 61 72 2a 29 6e 75 6c 6c 49 64 2e 7a 29 3b 0a 20  ar*)nullId.z);. 
18fcb 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
18fcc 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
18fcd 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 26  (pParse, 0, 0, &
18fce 6e 75 6c 6c 49 64 29 3b 0a 20 20 20 20 69 66 28  nullId);.    if(
18fcf 20 70 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f   pList==0 ) goto
18fd0 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
18fd1 65 78 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61  ex;.    pList->a
18fd2 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20  [0].sortOrder = 
18fd3 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 0a  sortOrder;.  }..
18fd4 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
18fd5 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 6f  how many bytes o
18fd6 66 20 73 70 61 63 65 20 61 72 65 20 72 65 71 75  f space are requ
18fd7 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 65 78  ired to store ex
18fd8 70 6c 69 63 69 74 6c 79 0a 20 20 2a 2a 20 73 70  plicitly.  ** sp
18fd9 65 63 69 66 69 65 64 20 63 6f 6c 6c 61 74 69 6f  ecified collatio
18fda 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73  n sequence names
18fdb 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
18fdc 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ; i<pList->nExpr
18fdd 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  ; i++){.    Expr
18fde 20 2a 70 45 78 70 72 20 3d 20 70 4c 69 73 74 2d   *pExpr = pList-
18fdf 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
18fe0 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20   if( pExpr ){.  
18fe1 20 20 20 20 6e 45 78 74 72 61 20 2b 3d 20 28 31      nExtra += (1
18fe2 20 2b 20 73 74 72 6c 65 6e 28 70 45 78 70 72 2d   + strlen(pExpr-
18fe3 3e 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 29 3b  >pColl->zName));
18fe4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
18fe5 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20   .  ** Allocate 
18fe6 74 68 65 20 69 6e 64 65 78 20 73 74 72 75 63 74  the index struct
18fe7 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e 4e 61  ure. .  */.  nNa
18fe8 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d  me = strlen(zNam
18fe9 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70 4c 69  e);.  nCol = pLi
18fea 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e  st->nExpr;.  pIn
18feb 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  dex = sqlite3DbM
18fec 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20  allocZero(db, . 
18fed 20 20 20 20 20 73 69 7a 65 6f 66 28 49 6e 64 65       sizeof(Inde
18fee 78 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20  x) +            
18fef 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72 75 63    /* Index struc
18ff0 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 20 20 73  ture  */.      s
18ff1 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f 6c 20  izeof(int)*nCol 
18ff2 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  +           /* I
18ff3 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 20 20  ndex.aiColumn   
18ff4 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  */.      sizeof(
18ff5 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29 20 2b 20  int)*(nCol+1) + 
18ff6 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61        /* Index.a
18ff7 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a 20 20 20  iRowEst   */.   
18ff8 20 20 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a     sizeof(char *
18ff9 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20  )*nCol +        
18ffa 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20  /* Index.azColl 
18ffb 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a      */.      siz
18ffc 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20 2b 20 20  eof(u8)*nCol +  
18ffd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
18ffe 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f  ex.aSortOrder */
18fff 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20 31  .      nName + 1
19000 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   +              
19001 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 7a 4e 61      /* Index.zNa
19002 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  me      */.     
19003 20 6e 45 78 74 72 61 20 20 20 20 20 20 20 20 20   nExtra         
19004 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19005 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   Collation seque
19006 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 29  nce names */.  )
19007 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
19008 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
19009 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
1900a 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70 49  _index;.  }.  pI
1900b 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 28  ndex->azColl = (
1900c 63 68 61 72 2a 2a 29 28 26 70 49 6e 64 65 78 5b  char**)(&pIndex[
1900d 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61  1]);.  pIndex->a
1900e 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 20 2a  iColumn = (int *
1900f 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c  )(&pIndex->azCol
19010 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64  l[nCol]);.  pInd
19011 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 3d 20 28  ex->aiRowEst = (
19012 75 6e 73 69 67 6e 65 64 20 2a 29 28 26 70 49 6e  unsigned *)(&pIn
19013 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 43  dex->aiColumn[nC
19014 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  ol]);.  pIndex->
19015 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  aSortOrder = (u8
19016 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 69 52   *)(&pIndex->aiR
19017 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29 3b 0a  owEst[nCol+1]);.
19018 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 20    pIndex->zName 
19019 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64  = (char *)(&pInd
1901a 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e  ex->aSortOrder[n
1901b 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74 72 61 20  Col]);.  zExtra 
1901c 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64  = (char *)(&pInd
1901d 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 2b  ex->zName[nName+
1901e 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 49  1]);.  memcpy(pI
1901f 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  ndex->zName, zNa
19020 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20  me, nName+1);.  
19021 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 20 3d  pIndex->pTable =
19022 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 65 78 2d   pTab;.  pIndex-
19023 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74  >nColumn = pList
19024 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64 65  ->nExpr;.  pInde
19025 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 6f 6e 45  x->onError = onE
19026 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  rror;.  pIndex->
19027 61 75 74 6f 49 6e 64 65 78 20 3d 20 70 4e 61 6d  autoIndex = pNam
19028 65 3d 3d 30 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  e==0;.  pIndex->
19029 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44  pSchema = db->aD
1902a 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a  b[iDb].pSchema;.
1902b 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73  .  /* Check to s
1902c 65 65 20 69 66 20 77 65 20 73 68 6f 75 6c 64 20  ee if we should 
1902d 68 6f 6e 6f 72 20 44 45 53 43 20 72 65 71 75 65  honor DESC reque
1902e 73 74 73 20 6f 6e 20 69 6e 64 65 78 20 63 6f 6c  sts on index col
1902f 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20  umns.  */.  if( 
19030 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
19031 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 29 7b 0a  le_format>=4 ){.
19032 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73      sortOrderMas
19033 6b 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 48 6f 6e  k = -1;   /* Hon
19034 6f 72 20 44 45 53 43 20 2a 2f 0a 20 20 7d 65 6c  or DESC */.  }el
19035 73 65 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65  se{.    sortOrde
19036 72 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 2f 2a  rMask = 0;    /*
19037 20 49 67 6e 6f 72 65 20 44 45 53 43 20 2a 2f 0a   Ignore DESC */.
19038 20 20 7d 0a 0a 20 20 2f 2a 20 53 63 61 6e 20 74    }..  /* Scan t
19039 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20  he names of the 
1903a 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74  columns of the t
1903b 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78  able to be index
1903c 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 61 64  ed and.  ** load
1903d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69   the column indi
1903e 63 65 73 20 69 6e 74 6f 20 74 68 65 20 49 6e 64  ces into the Ind
1903f 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 20 52  ex structure.  R
19040 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 0a 20  eport an error. 
19041 20 2a 2a 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d   ** if any colum
19042 6e 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a  n is not found..
19043 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20    */.  for(i=0, 
19044 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69 73 74 2d  pListItem=pList-
19045 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  >a; i<pList->nEx
19046 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73 74 49 74  pr; i++, pListIt
19047 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74  em++){.    const
19048 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61 6d 65 20   char *zColName 
19049 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 7a 4e 61  = pListItem->zNa
1904a 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a  me;.    Column *
1904b 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20 69 6e 74  pTabCol;.    int
1904c 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72   requestedSortOr
1904d 64 65 72 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  der;.    char *z
1904e 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Coll;           
1904f 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61          /* Colla
19050 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
19051 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f 72 28 6a  me */..    for(j
19052 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70 54 61 62  =0, pTabCol=pTab
19053 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e  ->aCol; j<pTab->
19054 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 61 62 43  nCol; j++, pTabC
19055 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  ol++){.      if(
19056 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
19057 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61 62 43 6f  zColName, pTabCo
19058 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62  l->zName)==0 ) b
19059 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1905a 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f  if( j>=pTab->nCo
1905b 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
1905c 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1905d 65 2c 20 22 74 61 62 6c 65 20 25 73 20 68 61 73  e, "table %s has
1905e 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64   no column named
1905f 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 70 54   %s",.        pT
19060 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 4e  ab->zName, zColN
19061 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ame);.      goto
19062 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
19063 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a  ex;.    }.    /*
19064 20 54 4f 44 4f 3a 20 20 41 64 64 20 61 20 74 65   TODO:  Add a te
19065 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  st to make sure 
19066 74 68 61 74 20 74 68 65 20 73 61 6d 65 20 63 6f  that the same co
19067 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6e 61 6d 65  lumn is not name
19068 64 0a 20 20 20 20 2a 2a 20 6d 6f 72 65 20 74 68  d.    ** more th
19069 61 6e 20 6f 6e 63 65 20 77 69 74 68 69 6e 20 74  an once within t
1906a 68 65 20 73 61 6d 65 20 69 6e 64 65 78 2e 20 20  he same index.  
1906b 4f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 69  Only the first i
1906c 6e 73 74 61 6e 63 65 20 6f 66 0a 20 20 20 20 2a  nstance of.    *
1906d 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c  * the column wil
1906e 6c 20 65 76 65 72 20 62 65 20 75 73 65 64 20 62  l ever be used b
1906f 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e  y the optimizer.
19070 20 20 4e 6f 74 65 20 74 68 61 74 20 75 73 69 6e    Note that usin
19071 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d  g the.    ** sam
19072 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65 20 74 68  e column more th
19073 61 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f 74 20 62  an once cannot b
19074 65 20 61 6e 20 65 72 72 6f 72 20 62 65 63 61 75  e an error becau
19075 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 0a 20  se that would . 
19076 20 20 20 2a 2a 20 62 72 65 61 6b 20 62 61 63 6b     ** break back
19077 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
19078 69 74 79 20 2d 20 69 74 20 6e 65 65 64 73 20 74  ity - it needs t
19079 6f 20 62 65 20 61 20 77 61 72 6e 69 6e 67 2e 0a  o be a warning..
1907a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e 64 65      */.    pInde
1907b 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d  x->aiColumn[i] =
1907c 20 6a 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73   j;.    if( pLis
1907d 74 49 74 65 6d 2d 3e 70 45 78 70 72 20 29 7b 0a  tItem->pExpr ){.
1907e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c        assert( pL
1907f 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e  istItem->pExpr->
19080 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 7a  pColl );.      z
19081 43 6f 6c 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20  Coll = zExtra;. 
19082 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
19083 72 69 6e 74 66 28 6e 45 78 74 72 61 2c 20 7a 45  rintf(nExtra, zE
19084 78 74 72 61 2c 20 22 25 73 22 2c 20 70 4c 69 73  xtra, "%s", pLis
19085 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43  tItem->pExpr->pC
19086 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  oll->zName);.   
19087 20 20 20 7a 45 78 74 72 61 20 2b 3d 20 28 73 74     zExtra += (st
19088 72 6c 65 6e 28 7a 43 6f 6c 6c 29 20 2b 20 31 29  rlen(zColl) + 1)
19089 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1908a 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d     zColl = pTab-
1908b 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a  >aCol[j].zColl;.
1908c 20 20 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c        if( !zColl
1908d 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c   ){.        zCol
1908e 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  l = db->pDfltCol
1908f 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  l->zName;.      
19090 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
19091 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26  !db->init.busy &
19092 26 20 21 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  & !sqlite3Locate
19093 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
19094 7a 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20  zColl, -1) ){.  
19095 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
19096 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
19097 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a  }.    pIndex->az
19098 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b  Coll[i] = zColl;
19099 0a 20 20 20 20 72 65 71 75 65 73 74 65 64 53 6f  .    requestedSo
1909a 72 74 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 49  rtOrder = pListI
1909b 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26  tem->sortOrder &
1909c 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a   sortOrderMask;.
1909d 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72      pIndex->aSor
1909e 74 4f 72 64 65 72 5b 69 5d 20 3d 20 72 65 71 75  tOrder[i] = requ
1909f 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a  estedSortOrder;.
190a0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 66    }.  sqlite3Def
190a1 61 75 6c 74 52 6f 77 45 73 74 28 70 49 6e 64 65  aultRowEst(pInde
190a2 78 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d  x);..  if( pTab=
190a3 3d 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62  =pParse->pNewTab
190a4 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  le ){.    /* Thi
190a5 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65  s routine has be
190a6 65 6e 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65  en called to cre
190a7 61 74 65 20 61 6e 20 61 75 74 6f 6d 61 74 69 63  ate an automatic
190a8 20 69 6e 64 65 78 20 61 73 20 61 0a 20 20 20 20   index as a.    
190a9 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 61 20 50  ** result of a P
190aa 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e  RIMARY KEY or UN
190ab 49 51 55 45 20 63 6c 61 75 73 65 20 6f 6e 20 61  IQUE clause on a
190ac 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69   column definiti
190ad 6f 6e 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20  on, or.    ** a 
190ae 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55  PRIMARY KEY or U
190af 4e 49 51 55 45 20 63 6c 61 75 73 65 20 66 6f 6c  NIQUE clause fol
190b0 6c 6f 77 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d  lowing the colum
190b1 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20  n definitions.. 
190b2 20 20 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f     ** i.e. one o
190b3 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  f:.    **.    **
190b4 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 28   CREATE TABLE t(
190b5 78 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 79  x PRIMARY KEY, y
190b6 29 3b 0a 20 20 20 20 2a 2a 20 43 52 45 41 54 45  );.    ** CREATE
190b7 20 54 41 42 4c 45 20 74 28 78 2c 20 79 2c 20 55   TABLE t(x, y, U
190b8 4e 49 51 55 45 28 78 2c 20 79 29 29 3b 0a 20 20  NIQUE(x, y));.  
190b9 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68    **.    ** Eith
190ba 65 72 20 77 61 79 2c 20 63 68 65 63 6b 20 74 6f  er way, check to
190bb 20 73 65 65 20 69 66 20 74 68 65 20 74 61 62 6c   see if the tabl
190bc 65 20 61 6c 72 65 61 64 79 20 68 61 73 20 73 75  e already has su
190bd 63 68 20 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a  ch an index. If.
190be 20 20 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74      ** so, don't
190bf 20 62 6f 74 68 65 72 20 63 72 65 61 74 69 6e 67   bother creating
190c0 20 74 68 69 73 20 6f 6e 65 2e 20 54 68 69 73 20   this one. This 
190c1 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 0a  only applies to.
190c2 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63      ** automatic
190c3 61 6c 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64  ally created ind
190c4 69 63 65 73 2e 20 55 73 65 72 73 20 63 61 6e 20  ices. Users can 
190c5 64 6f 20 61 73 20 74 68 65 79 20 77 69 73 68 20  do as they wish 
190c6 77 69 74 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c  with.    ** expl
190c7 69 63 69 74 20 69 6e 64 69 63 65 73 2e 0a 20 20  icit indices..  
190c8 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a    */.    Index *
190c9 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49  pIdx;.    for(pI
190ca 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
190cb 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
190cc 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
190cd 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 61 73 73  int k;.      ass
190ce 65 72 74 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72  ert( pIdx->onErr
190cf 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 20  or!=OE_None );. 
190d0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
190d1 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 3b 0a  x->autoIndex );.
190d2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
190d3 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  ndex->onError!=O
190d4 45 5f 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20 20 20  E_None );..     
190d5 20 69 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75   if( pIdx->nColu
190d6 6d 6e 21 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c  mn!=pIndex->nCol
190d7 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  umn ) continue;.
190d8 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b        for(k=0; k
190d9 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pIdx->nColumn; 
190da 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  k++){.        co
190db 6e 73 74 20 63 68 61 72 20 2a 7a 31 20 3d 20 70  nst char *z1 = p
190dc 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a  Idx->azColl[k];.
190dd 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
190de 61 72 20 2a 7a 32 20 3d 20 70 49 6e 64 65 78 2d  ar *z2 = pIndex-
190df 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20  >azColl[k];.    
190e0 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69      if( pIdx->ai
190e1 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64 65  Column[k]!=pInde
190e2 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20 29  x->aiColumn[k] )
190e3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
190e4 69 66 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f  if( pIdx->aSortO
190e5 72 64 65 72 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d  rder[k]!=pIndex-
190e6 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6b 5d 20 29  >aSortOrder[k] )
190e7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
190e8 69 66 28 20 7a 31 21 3d 7a 32 20 26 26 20 73 71  if( z1!=z2 && sq
190e9 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31 2c  lite3StrICmp(z1,
190ea 20 7a 32 29 20 29 20 62 72 65 61 6b 3b 0a 20 20   z2) ) break;.  
190eb 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
190ec 6b 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  k==pIdx->nColumn
190ed 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
190ee 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70  pIdx->onError!=p
190ef 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29  Index->onError )
190f0 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
190f1 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  his constraint c
190f2 72 65 61 74 65 73 20 74 68 65 20 73 61 6d 65 20  reates the same 
190f3 69 6e 64 65 78 20 61 73 20 61 20 70 72 65 76 69  index as a previ
190f4 6f 75 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ous.          **
190f5 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 70 65 63   constraint spec
190f6 69 66 69 65 64 20 73 6f 6d 65 77 68 65 72 65 20  ified somewhere 
190f7 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41  in the CREATE TA
190f8 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20  BLE statement.. 
190f9 20 20 20 20 20 20 20 20 20 2a 2a 20 48 6f 77 65           ** Howe
190fa 76 65 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c  ver the ON CONFL
190fb 49 43 54 20 63 6c 61 75 73 65 73 20 61 72 65 20  ICT clauses are 
190fc 64 69 66 66 65 72 65 6e 74 2e 20 49 66 20 62 6f  different. If bo
190fd 74 68 20 74 68 69 73 20 0a 20 20 20 20 20 20 20  th this .       
190fe 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74     ** constraint
190ff 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75   and the previou
19100 73 20 65 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e  s equivalent con
19101 73 74 72 61 69 6e 74 20 68 61 76 65 20 65 78 70  straint have exp
19102 6c 69 63 69 74 0a 20 20 20 20 20 20 20 20 20 20  licit.          
19103 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63  ** ON CONFLICT c
19104 6c 61 75 73 65 73 20 74 68 69 73 20 69 73 20 61  lauses this is a
19105 6e 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69  n error. Otherwi
19106 73 65 2c 20 75 73 65 20 74 68 65 0a 20 20 20 20  se, use the.    
19107 20 20 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69        ** explici
19108 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 65  tly specified be
19109 68 61 76 69 6f 75 72 20 66 6f 72 20 74 68 65 20  haviour for the 
1910a 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20 20  index..         
1910b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66   */.          if
1910c 28 20 21 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  ( !(pIdx->onErro
1910d 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c  r==OE_Default ||
1910e 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72   pIndex->onError
1910f 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29 20 29 7b  ==OE_Default) ){
19110 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
19111 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
19112 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
19113 20 20 20 20 20 20 22 63 6f 6e 66 6c 69 63 74 69        "conflicti
19114 6e 67 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63  ng ON CONFLICT c
19115 6c 61 75 73 65 73 20 73 70 65 63 69 66 69 65 64  lauses specified
19116 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  ", 0);.         
19117 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
19118 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d   pIdx->onError==
19119 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
1911a 20 20 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e            pIdx->
1911b 6f 6e 45 72 72 6f 72 20 3d 20 70 49 6e 64 65 78  onError = pIndex
1911c 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20  ->onError;.     
1911d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1911e 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78  .        goto ex
1911f 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
19120 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
19121 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68   }..  /* Link th
19122 65 20 6e 65 77 20 49 6e 64 65 78 20 73 74 72 75  e new Index stru
19123 63 74 75 72 65 20 74 6f 20 69 74 73 20 74 61 62  cture to its tab
19124 6c 65 20 61 6e 64 20 74 6f 20 74 68 65 20 6f 74  le and to the ot
19125 68 65 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f  her.  ** in-memo
19126 72 79 20 64 61 74 61 62 61 73 65 20 73 74 72 75  ry database stru
19127 63 74 75 72 65 73 2e 20 0a 20 20 2a 2f 0a 20 20  ctures. .  */.  
19128 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
19129 79 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  y ){.    Index *
1912a 70 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  p;.    p = sqlit
1912b 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 49  e3HashInsert(&pI
1912c 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69  ndex->pSchema->i
1912d 64 78 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20  dxHash, .       
1912e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1912f 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c    pIndex->zName,
19130 20 73 74 72 6c 65 6e 28 70 49 6e 64 65 78 2d 3e   strlen(pIndex->
19131 7a 4e 61 6d 65 29 2b 31 2c 20 70 49 6e 64 65 78  zName)+1, pIndex
19132 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  );.    if( p ){.
19133 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 3d        assert( p=
19134 3d 70 49 6e 64 65 78 20 29 3b 20 20 2f 2a 20 4d  =pIndex );  /* M
19135 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20  alloc must have 
19136 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20  failed */.      
19137 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
19138 20 3d 20 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f   = 1;.      goto
19139 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
1913a 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  ex;.    }.    db
1913b 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
1913c 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b  E_InternChanges;
1913d 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d  .    if( pTblNam
1913e 65 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49  e!=0 ){.      pI
1913f 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d  ndex->tnum = db-
19140 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20  >init.newTnum;. 
19141 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
19142 66 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62  f the db->init.b
19143 75 73 79 20 69 73 20 30 20 74 68 65 6e 20 63 72  usy is 0 then cr
19144 65 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f  eate the index o
19145 6e 20 64 69 73 6b 2e 20 20 54 68 69 73 0a 20 20  n disk.  This.  
19146 2a 2a 20 69 6e 76 6f 6c 76 65 73 20 77 72 69 74  ** involves writ
19147 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20 69 6e  ing the index in
19148 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20 74 61  to the master ta
19149 62 6c 65 20 61 6e 64 20 66 69 6c 6c 69 6e 67 20  ble and filling 
1914a 69 6e 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65  in the.  ** inde
1914b 78 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65  x with the curre
1914c 6e 74 20 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74  nt table content
1914d 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  s..  **.  ** The
1914e 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69   db->init.busy i
1914f 73 20 30 20 77 68 65 6e 20 74 68 65 20 75 73 65  s 0 when the use
19150 72 20 66 69 72 73 74 20 65 6e 74 65 72 73 20 61  r first enters a
19151 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 0a 20   CREATE INDEX . 
19152 20 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20 64 62   ** command.  db
19153 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31  ->init.busy is 1
19154 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65   when a database
19155 20 69 73 20 6f 70 65 6e 65 64 20 61 6e 64 20 0a   is opened and .
19156 20 20 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45    ** CREATE INDE
19157 58 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  X statements are
19158 20 72 65 61 64 20 6f 75 74 20 6f 66 20 74 68 65   read out of the
19159 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20   master table.  
1915a 49 6e 0a 20 20 2a 2a 20 74 68 65 20 6c 61 74 74  In.  ** the latt
1915b 65 72 20 63 61 73 65 20 74 68 65 20 69 6e 64 65  er case the inde
1915c 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  x already exists
1915d 20 6f 6e 20 64 69 73 6b 2c 20 77 68 69 63 68 20   on disk, which 
1915e 69 73 20 77 68 79 0a 20 20 2a 2a 20 77 65 20 64  is why.  ** we d
1915f 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 72 65 63  on't want to rec
19160 72 65 61 74 65 20 69 74 2e 0a 20 20 2a 2a 0a 20  reate it..  **. 
19161 20 2a 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d   ** If pTblName=
19162 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73  =0 it means this
19163 20 69 6e 64 65 78 20 69 73 20 67 65 6e 65 72 61   index is genera
19164 74 65 64 20 61 73 20 61 20 70 72 69 6d 61 72 79  ted as a primary
19165 20 6b 65 79 0a 20 20 2a 2a 20 6f 72 20 55 4e 49   key.  ** or UNI
19166 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f  QUE constraint o
19167 66 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  f a CREATE TABLE
19168 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 69 6e   statement.  Sin
19169 63 65 20 74 68 65 20 74 61 62 6c 65 0a 20 20 2a  ce the table.  *
1916a 2a 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20  * has just been 
1916b 63 72 65 61 74 65 64 2c 20 69 74 20 63 6f 6e 74  created, it cont
1916c 61 69 6e 73 20 6e 6f 20 64 61 74 61 20 61 6e 64  ains no data and
1916d 20 74 68 65 20 69 6e 64 65 78 20 69 6e 69 74 69   the index initi
1916e 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 73  alization.  ** s
1916f 74 65 70 20 63 61 6e 20 62 65 20 73 6b 69 70 70  tep can be skipp
19170 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20  ed..  */.  else 
19171 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
19172 79 3d 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65  y==0 ){.    Vdbe
19173 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a   *v;.    char *z
19174 53 74 6d 74 3b 0a 20 20 20 20 69 6e 74 20 69 4d  Stmt;.    int iM
19175 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  em = pParse->nMe
19176 6d 2b 2b 3b 0a 0a 20 20 20 20 76 20 3d 20 73 71  m++;..    v = sq
19177 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
19178 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d  rse);.    if( v=
19179 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63  =0 ) goto exit_c
1917a 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20  reate_index;... 
1917b 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65     /* Create the
1917c 20 72 6f 6f 74 70 61 67 65 20 66 6f 72 20 74 68   rootpage for th
1917d 65 20 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20  e index.    */. 
1917e 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
1917f 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
19180 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20  arse, 1, iDb);. 
19181 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19182 64 4f 70 28 76 2c 20 4f 50 5f 43 72 65 61 74 65  dOp(v, OP_Create
19183 49 6e 64 65 78 2c 20 69 44 62 2c 20 30 29 3b 0a  Index, iDb, 0);.
19184 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
19185 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74  ddOp(v, OP_MemSt
19186 6f 72 65 2c 20 69 4d 65 6d 2c 20 30 29 3b 0a 0a  ore, iMem, 0);..
19187 20 20 20 20 2f 2a 20 47 61 74 68 65 72 20 74 68      /* Gather th
19188 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20  e complete text 
19189 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 49 4e  of the CREATE IN
1918a 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  DEX statement in
1918b 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 7a 53  to.    ** the zS
1918c 74 6d 74 20 76 61 72 69 61 62 6c 65 0a 20 20 20  tmt variable.   
1918d 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 74 61   */.    if( pSta
1918e 72 74 20 26 26 20 70 45 6e 64 20 29 7b 0a 20 20  rt && pEnd ){.  
1918f 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65 64 20 69      /* A named i
19190 6e 64 65 78 20 77 69 74 68 20 61 6e 20 65 78 70  ndex with an exp
19191 6c 69 63 69 74 20 43 52 45 41 54 45 20 49 4e 44  licit CREATE IND
19192 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  EX statement */.
19193 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71        zStmt = sq
19194 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
19195 20 22 43 52 45 41 54 45 25 73 20 49 4e 44 45 58   "CREATE%s INDEX
19196 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20 20 20 20   %.*s",.        
19197 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65  onError==OE_None
19198 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 51 55 45   ? "" : " UNIQUE
19199 22 2c 0a 20 20 20 20 20 20 20 20 70 45 6e 64 2d  ",.        pEnd-
1919a 3e 7a 20 2d 20 70 4e 61 6d 65 2d 3e 7a 20 2b 20  >z - pName->z + 
1919b 31 2c 0a 20 20 20 20 20 20 20 20 70 4e 61 6d 65  1,.        pName
1919c 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ->z);.    }else{
1919d 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 61 75 74  .      /* An aut
1919e 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 63 72 65  omatic index cre
1919f 61 74 65 64 20 62 79 20 61 20 50 52 49 4d 41 52  ated by a PRIMAR
191a0 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20  Y KEY or UNIQUE 
191a1 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  constraint */.  
191a2 20 20 20 20 2f 2a 20 7a 53 74 6d 74 20 3d 20 73      /* zStmt = s
191a3 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22 22  qlite3MPrintf(""
191a4 29 3b 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d  ); */.      zStm
191a5 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  t = 0;.    }..  
191a6 20 20 2f 2a 20 41 64 64 20 61 6e 20 65 6e 74 72    /* Add an entr
191a7 79 20 69 6e 20 73 71 6c 69 74 65 5f 6d 61 73 74  y in sqlite_mast
191a8 65 72 20 66 6f 72 20 74 68 69 73 20 69 6e 64 65  er for this inde
191a9 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  x.    */.    sql
191aa 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
191ab 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
191ac 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51   "INSERT INTO %Q
191ad 2e 25 73 20 56 41 4c 55 45 53 28 27 69 6e 64 65  .%s VALUES('inde
191ae 78 27 2c 25 51 2c 25 51 2c 23 30 2c 25 51 29 3b  x',%Q,%Q,#0,%Q);
191af 22 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61  ",.        db->a
191b0 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
191b1 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
191b2 2c 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78  ,.        pIndex
191b3 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  ->zName,.       
191b4 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20   pTab->zName,.  
191b5 20 20 20 20 20 20 7a 53 74 6d 74 0a 20 20 20 20        zStmt.    
191b6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
191b7 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f  beAddOp(v, OP_Po
191b8 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 73 71  p, 1, 0);.    sq
191b9 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 74 6d 74  lite3_free(zStmt
191ba 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6c 6c 20  );..    /* Fill 
191bb 74 68 65 20 69 6e 64 65 78 20 77 69 74 68 20 64  the index with d
191bc 61 74 61 20 61 6e 64 20 72 65 70 61 72 73 65 20  ata and reparse 
191bd 74 68 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65  the schema. Code
191be 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20 20   an OP_Expire.  
191bf 20 20 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61    ** to invalida
191c0 74 65 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70 69  te all pre-compi
191c1 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  led statements..
191c2 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
191c3 54 62 6c 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20  TblName ){.     
191c4 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e   sqlite3RefillIn
191c5 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64  dex(pParse, pInd
191c6 65 78 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20  ex, iMem);.     
191c7 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
191c8 6f 6b 69 65 28 64 62 2c 20 76 2c 20 69 44 62 29  okie(db, v, iDb)
191c9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
191ca 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 50 61 72  dbeOp3(v, OP_Par
191cb 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30  seSchema, iDb, 0
191cc 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  ,.         sqlit
191cd 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
191ce 61 6d 65 3d 27 25 71 27 22 2c 20 70 49 6e 64 65  ame='%q'", pInde
191cf 78 2d 3e 7a 4e 61 6d 65 29 2c 20 50 33 5f 44 59  x->zName), P3_DY
191d0 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 73 71  NAMIC);.      sq
191d1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
191d2 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20  , OP_Expire, 0, 
191d3 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  0);.    }.  }.. 
191d4 20 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20   /* When adding 
191d5 61 6e 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20  an index to the 
191d6 6c 69 73 74 20 6f 66 20 69 6e 64 69 63 65 73 20  list of indices 
191d7 66 6f 72 20 61 20 74 61 62 6c 65 2c 20 6d 61 6b  for a table, mak
191d8 65 0a 20 20 2a 2a 20 73 75 72 65 20 61 6c 6c 20  e.  ** sure all 
191d9 69 6e 64 69 63 65 73 20 6c 61 62 65 6c 65 64 20  indices labeled 
191da 4f 45 5f 52 65 70 6c 61 63 65 20 63 6f 6d 65 20  OE_Replace come 
191db 61 66 74 65 72 20 61 6c 6c 20 74 68 6f 73 65 20  after all those 
191dc 6c 61 62 65 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f  labeled.  ** OE_
191dd 49 67 6e 6f 72 65 2e 20 20 54 68 69 73 20 69 73  Ignore.  This is
191de 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 74   necessary for t
191df 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61  he correct opera
191e0 74 69 6f 6e 20 6f 66 20 55 50 44 41 54 45 0a 20  tion of UPDATE. 
191e1 20 2a 2a 20 61 6e 64 20 49 4e 53 45 52 54 2e 0a   ** and INSERT..
191e2 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69    */.  if( db->i
191e3 6e 69 74 2e 62 75 73 79 20 7c 7c 20 70 54 62 6c  nit.busy || pTbl
191e4 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Name==0 ){.    i
191e5 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52  f( onError!=OE_R
191e6 65 70 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e  eplace || pTab->
191e7 70 49 6e 64 65 78 3d 3d 30 0a 20 20 20 20 20 20  pIndex==0.      
191e8 20 20 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64     || pTab->pInd
191e9 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  ex->onError==OE_
191ea 52 65 70 6c 61 63 65 29 7b 0a 20 20 20 20 20 20  Replace){.      
191eb 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20  pIndex->pNext = 
191ec 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20  pTab->pIndex;.  
191ed 20 20 20 20 70 54 61 62 2d 3e 70 49 6e 64 65 78      pTab->pIndex
191ee 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d   = pIndex;.    }
191ef 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65  else{.      Inde
191f0 78 20 2a 70 4f 74 68 65 72 20 3d 20 70 54 61 62  x *pOther = pTab
191f1 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20  ->pIndex;.      
191f2 77 68 69 6c 65 28 20 70 4f 74 68 65 72 2d 3e 70  while( pOther->p
191f3 4e 65 78 74 20 26 26 20 70 4f 74 68 65 72 2d 3e  Next && pOther->
191f4 70 4e 65 78 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d  pNext->onError!=
191f5 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20  OE_Replace ){.  
191f6 20 20 20 20 20 20 70 4f 74 68 65 72 20 3d 20 70        pOther = p
191f7 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20  Other->pNext;.  
191f8 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 6e 64      }.      pInd
191f9 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68  ex->pNext = pOth
191fa 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  er->pNext;.     
191fb 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d   pOther->pNext =
191fc 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20   pIndex;.    }. 
191fd 20 20 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20     pIndex = 0;. 
191fe 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75   }..  /* Clean u
191ff 70 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67  p before exiting
19200 20 2a 2f 0a 65 78 69 74 5f 63 72 65 61 74 65 5f   */.exit_create_
19201 69 6e 64 65 78 3a 0a 20 20 69 66 28 20 70 49 6e  index:.  if( pIn
19202 64 65 78 20 29 7b 0a 20 20 20 20 66 72 65 65 49  dex ){.    freeI
19203 6e 64 65 78 28 70 49 6e 64 65 78 29 3b 0a 20 20  ndex(pIndex);.  
19204 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  }.  sqlite3ExprL
19205 69 73 74 44 65 6c 65 74 65 28 70 4c 69 73 74 29  istDelete(pList)
19206 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  ;.  sqlite3SrcLi
19207 73 74 44 65 6c 65 74 65 28 70 54 62 6c 4e 61 6d  stDelete(pTblNam
19208 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  e);.  sqlite3_fr
19209 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74  ee(zName);.  ret
1920a 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  urn;.}../*.** Ge
1920b 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 6d  nerate code to m
1920c 61 6b 65 20 73 75 72 65 20 74 68 65 20 66 69 6c  ake sure the fil
1920d 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65 72 20  e format number 
1920e 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46  is at least minF
1920f 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20 67 65  ormat..** The ge
19210 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77 69 6c  nerated code wil
19211 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 66  l increase the f
19212 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65  ile format numbe
19213 72 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a  r if necessary..
19214 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19215 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69  E void sqlite3Mi
19216 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28  nimumFileFormat(
19217 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
19218 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69 6e 46  nt iDb, int minF
19219 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65 20 2a  ormat){.  Vdbe *
1921a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  v;.  v = sqlite3
1921b 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1921c 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
1921d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1921e 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69  (v, OP_ReadCooki
1921f 65 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20 20 20  e, iDb, 1);.    
19220 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
19221 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20  tree(v, iDb);.  
19222 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19223 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  Op(v, OP_Integer
19224 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20 30 29 3b  , minFormat, 0);
19225 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19226 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 65 2c 20  AddOp(v, OP_Ge, 
19227 30 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  0, sqlite3VdbeCu
19228 72 72 65 6e 74 41 64 64 72 28 76 29 2b 33 29 3b  rrentAddr(v)+3);
19229 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1922a 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65  AddOp(v, OP_Inte
1922b 67 65 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20  ger, minFormat, 
1922c 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1922d 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53  dbeAddOp(v, OP_S
1922e 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 31  etCookie, iDb, 1
1922f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
19230 46 69 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e 61  Fill the Index.a
19231 69 52 6f 77 45 73 74 5b 5d 20 61 72 72 61 79 20  iRowEst[] array 
19232 77 69 74 68 20 64 65 66 61 75 6c 74 20 69 6e 66  with default inf
19233 6f 72 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72  ormation - infor
19234 6d 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20  mation.** to be 
19235 75 73 65 64 20 77 68 65 6e 20 77 65 20 68 61 76  used when we hav
19236 65 20 6e 6f 74 20 72 75 6e 20 74 68 65 20 41 4e  e not run the AN
19237 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ALYZE command..*
19238 2a 0a 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d  *.** aiRowEst[0]
19239 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f 20 63   is suppose to c
1923a 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65  ontain the numbe
1923b 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
1923c 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 53   the index..** S
1923d 69 6e 63 65 20 77 65 20 64 6f 20 6e 6f 74 20 6b  ince we do not k
1923e 6e 6f 77 2c 20 67 75 65 73 73 20 31 20 6d 69 6c  now, guess 1 mil
1923f 6c 69 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74 5b  lion.  aiRowEst[
19240 31 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74  1] is an estimat
19241 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  e of the.** numb
19242 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
19243 65 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74  e table that mat
19244 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61  ch any particula
19245 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a  r value of the.*
19246 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f  * first column o
19247 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 61 69  f the index.  ai
19248 52 6f 77 45 73 74 5b 32 5d 20 69 73 20 61 6e 20  RowEst[2] is an 
19249 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
1924a 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f 77  number.** of row
1924b 73 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79  s that match any
1924c 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6d 62   particular comb
1924d 69 6e 69 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  iniation of the 
1924e 66 69 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a  first 2 columns.
1924f 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  ** of the index.
19250 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20    And so forth. 
19251 20 49 74 20 6d 75 73 74 20 61 6c 77 61 79 73 20   It must always 
19252 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
19253 0a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .*.**           
19254 61 69 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52  aiRowEst[N]<=aiR
19255 6f 77 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20  owEst[N-1].**   
19256 20 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74          aiRowEst
19257 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61  [N]>=1.**.** Apa
19258 72 74 20 66 72 6f 6d 20 74 68 61 74 2c 20 77 65  rt from that, we
19259 20 68 61 76 65 20 6c 69 74 74 6c 65 20 74 6f 20   have little to 
1925a 67 6f 20 6f 6e 20 62 65 73 69 64 65 73 20 69 6e  go on besides in
1925b 74 75 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a  tuition as to.**
1925c 20 68 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d 20   how aiRowEst[] 
1925d 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61  should be initia
1925e 6c 69 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d 62  lized.  The numb
1925f 65 72 73 20 67 65 6e 65 72 61 74 65 64 20 68 65  ers generated he
19260 72 65 0a 2a 2a 20 61 72 65 20 62 61 73 65 64 20  re.** are based 
19261 6f 6e 20 74 79 70 69 63 61 6c 20 76 61 6c 75 65  on typical value
19262 73 20 66 6f 75 6e 64 20 69 6e 20 61 63 74 75 61  s found in actua
19263 6c 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53 51  l indices..*/.SQ
19264 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19265 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74  d sqlite3Default
19266 52 6f 77 45 73 74 28 49 6e 64 65 78 20 2a 70 49  RowEst(Index *pI
19267 64 78 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  dx){.  unsigned 
19268 2a 61 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77  *a = pIdx->aiRow
19269 45 73 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Est;.  int i;.  
1926a 61 73 73 65 72 74 28 20 61 21 3d 30 20 29 3b 0a  assert( a!=0 );.
1926b 20 20 61 5b 30 5d 20 3d 20 31 30 30 30 30 30 30    a[0] = 1000000
1926c 3b 0a 20 20 66 6f 72 28 69 3d 70 49 64 78 2d 3e  ;.  for(i=pIdx->
1926d 6e 43 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69  nColumn; i>=5; i
1926e 2d 2d 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20  --){.    a[i] = 
1926f 35 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  5;.  }.  while( 
19270 69 3e 3d 31 20 29 7b 0a 20 20 20 20 61 5b 69 5d  i>=1 ){.    a[i]
19271 20 3d 20 31 31 20 2d 20 69 3b 0a 20 20 20 20 69   = 11 - i;.    i
19272 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49  --;.  }.  if( pI
19273 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
19274 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 61 5b 70 49  None ){.    a[pI
19275 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31  dx->nColumn] = 1
19276 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
19277 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
19278 20 64 72 6f 70 20 61 6e 20 65 78 69 73 74 69 6e   drop an existin
19279 67 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 20  g named index.  
1927a 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
1927b 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 44  implements the D
1927c 52 4f 50 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ROP INDEX statem
1927d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
1927e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1927f 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72  te3DropIndex(Par
19280 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
19281 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20  ist *pName, int 
19282 69 66 45 78 69 73 74 73 29 7b 0a 20 20 49 6e 64  ifExists){.  Ind
19283 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64  ex *pIndex;.  Vd
19284 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
19285 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
19286 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20  b;.  int iDb;.. 
19287 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
19288 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
19289 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
1928a 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65  o exit_drop_inde
1928b 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  x;.  }.  assert(
1928c 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20   pName->nSrc==1 
1928d 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
1928e 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53  OK!=sqlite3ReadS
1928f 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
19290 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
19291 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  rop_index;.  }. 
19292 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65   pIndex = sqlite
19293 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 70  3FindIndex(db, p
19294 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  Name->a[0].zName
19295 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44  , pName->a[0].zD
19296 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20  atabase);.  if( 
19297 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20  pIndex==0 ){.   
19298 20 69 66 28 20 21 69 66 45 78 69 73 74 73 20 29   if( !ifExists )
19299 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1929a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1929b 22 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20  "no such index: 
1929c 25 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a  %S", pName, 0);.
1929d 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
1929e 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20  ->checkSchema = 
1929f 31 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  1;.    goto exit
192a0 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d  _drop_index;.  }
192a1 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 61  .  if( pIndex->a
192a2 75 74 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  utoIndex ){.    
192a3 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
192a4 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20 61  pParse, "index a
192a5 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 55  ssociated with U
192a6 4e 49 51 55 45 20 22 0a 20 20 20 20 20 20 22 6f  NIQUE ".      "o
192a7 72 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  r PRIMARY KEY co
192a8 6e 73 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 20  nstraint cannot 
192a9 62 65 20 64 72 6f 70 70 65 64 22 2c 20 30 29 3b  be dropped", 0);
192aa 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
192ab 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  rop_index;.  }. 
192ac 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
192ad 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
192ae 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29  pIndex->pSchema)
192af 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
192b0 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
192b1 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  ION.  {.    int 
192b2 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
192b3 4f 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 61  OP_INDEX;.    Ta
192b4 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64  ble *pTab = pInd
192b5 65 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20  ex->pTable;.    
192b6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
192b7 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
192b8 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
192b9 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48  char *zTab = SCH
192ba 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
192bb 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
192bc 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
192bd 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20   SQLITE_DELETE, 
192be 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b  zTab, 0, zDb) ){
192bf 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
192c0 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20  _drop_index;.   
192c1 20 7d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54   }.    if( !OMIT
192c2 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 20 29  _TEMPDB && iDb )
192c3 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
192c4 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a  ROP_TEMP_INDEX;.
192c5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
192c6 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
192c7 20 63 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e 7a   code, pIndex->z
192c8 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Name, pTab->zNam
192c9 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  e, zDb) ){.     
192ca 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
192cb 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d  index;.    }.  }
192cc 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65  .#endif..  /* Ge
192cd 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72  nerate code to r
192ce 65 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20  emove the index 
192cf 61 6e 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 73  and from the mas
192d0 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76  ter table */.  v
192d1 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
192d2 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
192d3 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
192d4 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
192d5 72 73 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c  rse,.       "DEL
192d6 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57  ETE FROM %Q.%s W
192d7 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20  HERE name=%Q",. 
192d8 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
192d9 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
192da 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20  _TABLE(iDb),.   
192db 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d      pIndex->zNam
192dc 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c  e.    );.    sql
192dd 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
192de 28 64 62 2c 20 76 2c 20 69 44 62 29 3b 0a 20 20  (db, v, iDb);.  
192df 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67    destroyRootPag
192e0 65 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78  e(pParse, pIndex
192e1 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20  ->tnum, iDb);.  
192e2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33    sqlite3VdbeOp3
192e3 28 76 2c 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78  (v, OP_DropIndex
192e4 2c 20 69 44 62 2c 20 30 2c 20 70 49 6e 64 65 78  , iDb, 0, pIndex
192e5 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d  ->zName, 0);.  }
192e6 0a 0a 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65  ..exit_drop_inde
192e7 78 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  x:.  sqlite3SrcL
192e8 69 73 74 44 65 6c 65 74 65 28 70 4e 61 6d 65 29  istDelete(pName)
192e9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41 72 72 61  ;.}../*.** pArra
192ea 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  y is a pointer t
192eb 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 6f 62  o an array of ob
192ec 6a 65 63 74 73 2e 20 20 45 61 63 68 20 6f 62 6a  jects.  Each obj
192ed 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72  ect in the.** ar
192ee 72 61 79 20 69 73 20 73 7a 45 6e 74 72 79 20 62  ray is szEntry b
192ef 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 20 54  ytes in size.  T
192f0 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  his routine allo
192f1 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6f  cates a new.** o
192f2 62 6a 65 63 74 20 6f 6e 20 74 68 65 20 65 6e 64  bject on the end
192f3 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e 0a 2a   of the array..*
192f4 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79 20 69 73  *.** *pnEntry is
192f5 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
192f6 6e 74 72 69 65 73 20 61 6c 72 65 61 64 79 20 69  ntries already i
192f7 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c 6c 6f 63  n use.  *pnAlloc
192f8 20 69 73 0a 2a 2a 20 74 68 65 20 70 72 65 76 69   is.** the previ
192f9 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
192fa 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61  size of the arra
192fb 79 2e 20 20 69 6e 69 74 53 69 7a 65 20 69 73 20  y.  initSize is 
192fc 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65 64  the.** suggested
192fd 20 69 6e 69 74 69 61 6c 20 61 72 72 61 79 20 73   initial array s
192fe 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  ize allocation..
192ff 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20  **.** The index 
19300 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79  of the new entry
19301 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
19302 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  *pIdx..**.** Thi
19303 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
19304 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
19305 68 65 20 61 72 72 61 79 20 6f 66 20 6f 62 6a 65  he array of obje
19306 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 69  cts.  This.** mi
19307 67 68 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ght be the same 
19308 61 73 20 74 68 65 20 70 41 72 72 61 79 20 70 61  as the pArray pa
19309 72 61 6d 65 74 65 72 20 6f 72 20 69 74 20 6d 69  rameter or it mi
1930a 67 68 74 20 62 65 20 61 20 64 69 66 66 65 72 65  ght be a differe
1930b 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66  nt.** pointer if
1930c 20 74 68 65 20 61 72 72 61 79 20 77 61 73 20 72   the array was r
1930d 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  esized..*/.SQLIT
1930e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
1930f 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f  sqlite3ArrayAllo
19310 63 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 20  cate(.  sqlite3 
19311 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6e  *db,      /* Con
19312 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66  nection to notif
19313 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  y of malloc fail
19314 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ures */.  void *
19315 70 41 72 72 61 79 2c 20 20 20 20 20 2f 2a 20 41  pArray,     /* A
19316 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e  rray of objects.
19317 20 20 4d 69 67 68 74 20 62 65 20 72 65 61 6c 6c    Might be reall
19318 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ocated */.  int 
19319 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 20 2f 2a  szEntry,      /*
1931a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 6f 62   Size of each ob
1931b 6a 65 63 74 20 69 6e 20 74 68 65 20 61 72 72 61  ject in the arra
1931c 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 69 74 53  y */.  int initS
1931d 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53 75 67 67  ize,     /* Sugg
1931e 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20 61 6c  ested initial al
1931f 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c 65  location, in ele
19320 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  ments */.  int *
19321 70 6e 45 6e 74 72 79 2c 20 20 20 20 20 2f 2a 20  pnEntry,     /* 
19322 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63 74  Number of object
19323 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  s currently in u
19324 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 41  se */.  int *pnA
19325 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20 43 75 72  lloc,     /* Cur
19326 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65  rent size of the
19327 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20   allocation, in 
19328 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  elements */.  in
19329 74 20 2a 70 49 64 78 20 20 20 20 20 20 20 20 20  t *pIdx         
1932a 2f 2a 20 57 72 69 74 65 20 74 68 65 20 69 6e 64  /* Write the ind
1932b 65 78 20 6f 66 20 61 20 6e 65 77 20 73 6c 6f 74  ex of a new slot
1932c 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 63 68   here */.){.  ch
1932d 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 2a 70 6e  ar *z;.  if( *pn
1932e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41 6c 6c 6f  Entry >= *pnAllo
1932f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70  c ){.    void *p
19330 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e 65 77  New;.    int new
19331 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77 53 69 7a  Size;.    newSiz
19332 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63 29 2a 32  e = (*pnAlloc)*2
19333 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a 20 20 20   + initSize;.   
19334 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
19335 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 41 72  bRealloc(db, pAr
19336 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a 73 7a 45  ray, newSize*szE
19337 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28 20 70  ntry);.    if( p
19338 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  New==0 ){.      
19339 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20  *pIdx = -1;.    
1933a 20 20 72 65 74 75 72 6e 20 70 41 72 72 61 79 3b    return pArray;
1933b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 6e 41 6c  .    }.    *pnAl
1933c 6c 6f 63 20 3d 20 6e 65 77 53 69 7a 65 3b 0a 20  loc = newSize;. 
1933d 20 20 20 70 41 72 72 61 79 20 3d 20 70 4e 65 77     pArray = pNew
1933e 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61  ;.  }.  z = (cha
1933f 72 2a 29 70 41 72 72 61 79 3b 0a 20 20 6d 65 6d  r*)pArray;.  mem
19340 73 65 74 28 26 7a 5b 2a 70 6e 45 6e 74 72 79 20  set(&z[*pnEntry 
19341 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30 2c 20 73  * szEntry], 0, s
19342 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70 49 64 78  zEntry);.  *pIdx
19343 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 2b   = *pnEntry;.  +
19344 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 72 65 74  +*pnEntry;.  ret
19345 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d 0a 0a 2f  urn pArray;.}../
19346 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65  *.** Append a ne
19347 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
19348 20 67 69 76 65 6e 20 49 64 4c 69 73 74 2e 20 20   given IdList.  
19349 43 72 65 61 74 65 20 61 20 6e 65 77 20 49 64 4c  Create a new IdL
1934a 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62  ist if.** need b
1934b 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 49  e..**.** A new I
1934c 64 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e 65  dList is returne
1934d 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6d 61  d, or NULL if ma
1934e 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f  lloc() fails..*/
1934f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19350 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49  IdList *sqlite3I
19351 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69  dListAppend(sqli
19352 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20  te3 *db, IdList 
19353 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70  *pList, Token *p
19354 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 69 3b  Token){.  int i;
19355 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
19356 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73  ){.    pList = s
19357 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
19358 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 64  ro(db, sizeof(Id
19359 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28  List) );.    if(
1935a 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
1935b 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d  rn 0;.    pList-
1935c 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 7d  >nAlloc = 0;.  }
1935d 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 73 71  .  pList->a = sq
1935e 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61  lite3ArrayAlloca
1935f 74 65 28 0a 20 20 20 20 20 20 64 62 2c 0a 20 20  te(.      db,.  
19360 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c 0a 20 20      pList->a,.  
19361 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 69 73 74      sizeof(pList
19362 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 20 20 35  ->a[0]),.      5
19363 2c 0a 20 20 20 20 20 20 26 70 4c 69 73 74 2d 3e  ,.      &pList->
19364 6e 49 64 2c 0a 20 20 20 20 20 20 26 70 4c 69 73  nId,.      &pLis
19365 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20  t->nAlloc,.     
19366 20 26 69 0a 20 20 29 3b 0a 20 20 69 66 28 20 69   &i.  );.  if( i
19367 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <0 ){.    sqlite
19368 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 70 4c  3IdListDelete(pL
19369 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ist);.    return
1936a 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d   0;.  }.  pList-
1936b 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71  >a[i].zName = sq
1936c 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
1936d 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a  en(db, pToken);.
1936e 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a    return pList;.
1936f 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
19370 61 6e 20 49 64 4c 69 73 74 2e 0a 2a 2f 0a 53 51  an IdList..*/.SQ
19371 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19372 64 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44  d sqlite3IdListD
19373 65 6c 65 74 65 28 49 64 4c 69 73 74 20 2a 70 4c  elete(IdList *pL
19374 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ist){.  int i;. 
19375 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
19376 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
19377 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b  0; i<pList->nId;
19378 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
19379 65 33 5f 66 72 65 65 28 70 4c 69 73 74 2d 3e 61  e3_free(pList->a
1937a 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a  [i].zName);.  }.
1937b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1937c 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69  List->a);.  sqli
1937d 74 65 33 5f 66 72 65 65 28 70 4c 69 73 74 29 3b  te3_free(pList);
1937e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1937f 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70 4c   the index in pL
19380 69 73 74 20 6f 66 20 74 68 65 20 69 64 65 6e 74  ist of the ident
19381 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 64 2e  ifier named zId.
19382 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a 20 69    Return -1.** i
19383 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  f not found..*/.
19384 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19385 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  nt sqlite3IdList
19386 49 6e 64 65 78 28 49 64 4c 69 73 74 20 2a 70 4c  Index(IdList *pL
19387 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ist, const char 
19388 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69  *zName){.  int i
19389 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
1938a 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
1938b 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
1938c 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nId; i++){.   
1938d 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1938e 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  Cmp(pList->a[i].
1938f 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30  zName, zName)==0
19390 20 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 7d   ) return i;.  }
19391 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a  .  return -1;.}.
19392 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20  ./*.** Append a 
19393 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 74  new table name t
19394 6f 20 74 68 65 20 67 69 76 65 6e 20 53 72 63 4c  o the given SrcL
19395 69 73 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e  ist.  Create a n
19396 65 77 20 53 72 63 4c 69 73 74 20 69 66 0a 2a 2a  ew SrcList if.**
19397 20 6e 65 65 64 20 62 65 2e 20 20 41 20 6e 65 77   need be.  A new
19398 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74 65   entry is create
19399 64 20 69 6e 20 74 68 65 20 53 72 63 4c 69 73 74  d in the SrcList
1939a 20 65 76 65 6e 20 69 66 20 70 54 6f 6b 65 6e 20   even if pToken 
1939b 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41  is NULL..**.** A
1939c 20 6e 65 77 20 53 72 63 4c 69 73 74 20 69 73 20   new SrcList is 
1939d 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c  returned, or NUL
1939e 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61  L if malloc() fa
1939f 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44  ils..**.** If pD
193a0 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 6e  atabase is not n
193a1 75 6c 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  ull, it means th
193a2 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61 73  at the table has
193a3 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a 2a 2a 20   an optional.** 
193a4 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 70 72  database name pr
193a5 65 66 69 78 2e 20 20 4c 69 6b 65 20 74 68 69 73  efix.  Like this
193a6 3a 20 20 22 64 61 74 61 62 61 73 65 2e 74 61 62  :  "database.tab
193a7 6c 65 22 2e 20 20 54 68 65 20 70 44 61 74 61 62  le".  The pDatab
193a8 61 73 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f  ase.** points to
193a9 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
193aa 61 6e 64 20 74 68 65 20 70 54 61 62 6c 65 20 70  and the pTable p
193ab 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 64 61 74  oints to the dat
193ac 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a 2a 20 54  abase name..** T
193ad 68 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a  he SrcList.a[].z
193ae 4e 61 6d 65 20 66 69 65 6c 64 20 69 73 20 66 69  Name field is fi
193af 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 74 61  lled with the ta
193b0 62 6c 65 20 6e 61 6d 65 20 77 68 69 63 68 20 6d  ble name which m
193b1 69 67 68 74 0a 2a 2a 20 63 6f 6d 65 20 66 72 6f  ight.** come fro
193b2 6d 20 70 54 61 62 6c 65 20 28 69 66 20 70 44 61  m pTable (if pDa
193b3 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 29 20  tabase is NULL) 
193b4 6f 72 20 66 72 6f 6d 20 70 44 61 74 61 62 61 73  or from pDatabas
193b5 65 2e 20 20 0a 2a 2a 20 53 72 63 4c 69 73 74 2e  e.  .** SrcList.
193b6 61 5b 5d 2e 7a 44 61 74 61 62 61 73 65 20 69 73  a[].zDatabase is
193b7 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65   filled with the
193b8 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 66   database name f
193b9 72 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a 2a 20 6f  rom pTable,.** o
193ba 72 20 77 69 74 68 20 4e 55 4c 4c 20 69 66 20 6e  r with NULL if n
193bb 6f 20 64 61 74 61 62 61 73 65 20 69 73 20 73 70  o database is sp
193bc 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  ecified..**.** I
193bd 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
193be 66 20 63 61 6c 6c 20 6c 69 6b 65 20 74 68 69 73  f call like this
193bf 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
193c0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
193c1 70 65 6e 64 28 44 2c 41 2c 42 2c 30 29 3b 0a 2a  pend(D,A,B,0);.*
193c2 2a 0a 2a 2a 20 54 68 65 6e 20 42 20 69 73 20 61  *.** Then B is a
193c3 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20   table name and 
193c4 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
193c5 65 20 69 73 20 75 6e 73 70 65 63 69 66 69 65 64  e is unspecified
193c6 2e 20 20 49 66 20 63 61 6c 6c 65 64 0a 2a 2a 20  .  If called.** 
193c7 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
193c8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
193c9 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c  SrcListAppend(D,
193ca 41 2c 42 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 54 68  A,B,C);.**.** Th
193cb 65 6e 20 43 20 69 73 20 74 68 65 20 74 61 62 6c  en C is the tabl
193cc 65 20 6e 61 6d 65 20 61 6e 64 20 42 20 69 73 20  e name and B is 
193cd 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
193ce 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
193cf 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
193d0 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
193d1 6e 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  nd(.  sqlite3 *d
193d2 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  b,        /* Con
193d3 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66  nection to notif
193d4 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  y of malloc fail
193d5 75 72 65 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73  ures */.  SrcLis
193d6 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a  t *pList,     /*
193d7 20 41 70 70 65 6e 64 20 74 6f 20 74 68 69 73 20   Append to this 
193d8 53 72 63 4c 69 73 74 2e 20 4e 55 4c 4c 20 63 72  SrcList. NULL cr
193d9 65 61 74 65 73 20 61 20 6e 65 77 20 53 72 63 4c  eates a new SrcL
193da 69 73 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ist */.  Token *
193db 70 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20  pTable,      /* 
193dc 54 61 62 6c 65 20 74 6f 20 61 70 70 65 6e 64 20  Table to append 
193dd 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74  */.  Token *pDat
193de 61 62 61 73 65 20 20 20 20 2f 2a 20 44 61 74 61  abase    /* Data
193df 62 61 73 65 20 6f 66 20 74 68 65 20 74 61 62 6c  base of the tabl
193e0 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74  e */.){.  struct
193e1 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
193e2 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73  Item;.  if( pLis
193e3 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73  t==0 ){.    pLis
193e4 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
193e5 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
193e6 6f 66 28 53 72 63 4c 69 73 74 29 20 29 3b 0a 20  of(SrcList) );. 
193e7 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20     if( pList==0 
193e8 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
193e9 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20  pList->nAlloc = 
193ea 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69  1;.  }.  if( pLi
193eb 73 74 2d 3e 6e 53 72 63 3e 3d 70 4c 69 73 74 2d  st->nSrc>=pList-
193ec 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 53  >nAlloc ){.    S
193ed 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20  rcList *pNew;.  
193ee 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20    pList->nAlloc 
193ef 2a 3d 20 32 3b 0a 20 20 20 20 70 4e 65 77 20 3d  *= 2;.    pNew =
193f0 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
193f1 63 28 64 62 2c 20 70 4c 69 73 74 2c 0a 20 20 20  c(db, pList,.   
193f2 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65              size
193f3 6f 66 28 2a 70 4c 69 73 74 29 20 2b 20 28 70 4c  of(*pList) + (pL
193f4 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2d 31 29 2a 73  ist->nAlloc-1)*s
193f5 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30  izeof(pList->a[0
193f6 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  ]) );.    if( pN
193f7 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ew==0 ){.      s
193f8 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
193f9 65 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 20 20  ete(pList);.    
193fa 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
193fb 7d 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 4e  }.    pList = pN
193fc 65 77 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20  ew;.  }.  pItem 
193fd 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73  = &pList->a[pLis
193fe 74 2d 3e 6e 53 72 63 5d 3b 0a 20 20 6d 65 6d 73  t->nSrc];.  mems
193ff 65 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 7a  et(pItem, 0, siz
19400 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29  eof(pList->a[0])
19401 29 3b 0a 20 20 69 66 28 20 70 44 61 74 61 62 61  );.  if( pDataba
19402 73 65 20 26 26 20 70 44 61 74 61 62 61 73 65 2d  se && pDatabase-
19403 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 61  >z==0 ){.    pDa
19404 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 7d 0a  tabase = 0;.  }.
19405 20 20 69 66 28 20 70 44 61 74 61 62 61 73 65 20    if( pDatabase 
19406 26 26 20 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  && pTable ){.   
19407 20 54 6f 6b 65 6e 20 2a 70 54 65 6d 70 20 3d 20   Token *pTemp = 
19408 70 44 61 74 61 62 61 73 65 3b 0a 20 20 20 20 70  pDatabase;.    p
19409 44 61 74 61 62 61 73 65 20 3d 20 70 54 61 62 6c  Database = pTabl
1940a 65 3b 0a 20 20 20 20 70 54 61 62 6c 65 20 3d 20  e;.    pTable = 
1940b 70 54 65 6d 70 3b 0a 20 20 7d 0a 20 20 70 49 74  pTemp;.  }.  pIt
1940c 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  em->zName = sqli
1940d 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
1940e 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 20 20  (db, pTable);.  
1940f 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pItem->zDatabase
19410 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
19411 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 61 74  omToken(db, pDat
19412 61 62 61 73 65 29 3b 0a 20 20 70 49 74 65 6d 2d  abase);.  pItem-
19413 3e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20  >iCursor = -1;. 
19414 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61   pItem->isPopula
19415 74 65 64 20 3d 20 30 3b 0a 20 20 70 4c 69 73 74  ted = 0;.  pList
19416 2d 3e 6e 53 72 63 2b 2b 3b 0a 20 20 72 65 74 75  ->nSrc++;.  retu
19417 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a  rn pList;.}../*.
19418 2a 2a 20 41 73 73 69 67 6e 20 63 75 72 73 6f 72  ** Assign cursor
19419 73 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20  s to all tables 
1941a 69 6e 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f 0a  in a SrcList.*/.
1941b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1941c 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
1941d 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
1941e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
1941f 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a  rcList *pList){.
19420 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
19421 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
19422 70 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  pItem;.  assert(
19423 70 4c 69 73 74 20 7c 7c 20 70 50 61 72 73 65 2d  pList || pParse-
19424 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
19425 64 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  d );.  if( pList
19426 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c   ){.    for(i=0,
19427 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b   pItem=pList->a;
19428 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20   i<pList->nSrc; 
19429 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1942a 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
1942b 69 43 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65  iCursor>=0 ) bre
1942c 61 6b 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d  ak;.      pItem-
1942d 3e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73  >iCursor = pPars
1942e 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20  e->nTab++;.     
1942f 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c   if( pItem->pSel
19430 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ect ){.        s
19431 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73  qlite3SrcListAss
19432 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73  ignCursors(pPars
19433 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  e, pItem->pSelec
19434 74 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20  t->pSrc);.      
19435 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  }.    }.  }.}../
19436 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  *.** Delete an e
19437 6e 74 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e  ntire SrcList in
19438 63 6c 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20  cluding all its 
19439 73 75 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  substructure..*/
1943a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1943b 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
1943c 69 73 74 44 65 6c 65 74 65 28 53 72 63 4c 69 73  istDelete(SrcLis
1943d 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  t *pList){.  int
1943e 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63   i;.  struct Src
1943f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
19440 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
19441 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
19442 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c  (pItem=pList->a,
19443 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e   i=0; i<pList->n
19444 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
19445 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  +){.    sqlite3_
19446 66 72 65 65 28 70 49 74 65 6d 2d 3e 7a 44 61 74  free(pItem->zDat
19447 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71 6c 69  abase);.    sqli
19448 74 65 33 5f 66 72 65 65 28 70 49 74 65 6d 2d 3e  te3_free(pItem->
19449 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69  zName);.    sqli
1944a 74 65 33 5f 66 72 65 65 28 70 49 74 65 6d 2d 3e  te3_free(pItem->
1944b 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 73 71 6c  zAlias);.    sql
1944c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
1944d 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20  pItem->pTab);.  
1944e 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1944f 65 6c 65 74 65 28 70 49 74 65 6d 2d 3e 70 53 65  elete(pItem->pSe
19450 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  lect);.    sqlit
19451 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 49 74  e3ExprDelete(pIt
19452 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 73 71  em->pOn);.    sq
19453 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
19454 65 28 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29  e(pItem->pUsing)
19455 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
19456 66 72 65 65 28 70 4c 69 73 74 29 3b 0a 7d 0a 0a  free(pList);.}..
19457 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
19458 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  ne is called by 
19459 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 61 64  the parser to ad
1945a 64 20 61 20 6e 65 77 20 74 65 72 6d 20 74 6f 20  d a new term to 
1945b 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61 20  the.** end of a 
1945c 67 72 6f 77 69 6e 67 20 46 52 4f 4d 20 63 6c 61  growing FROM cla
1945d 75 73 65 2e 20 20 54 68 65 20 22 70 22 20 70 61  use.  The "p" pa
1945e 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 70  rameter is the p
1945f 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 46 52  art of.** the FR
19460 4f 4d 20 63 6c 61 75 73 65 20 74 68 61 74 20 68  OM clause that h
19461 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
19462 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 20 22 70  constructed.  "p
19463 22 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20  " is NULL.** if 
19464 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
19465 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52  t term of the FR
19466 4f 4d 20 63 6c 61 75 73 65 2e 20 20 70 54 61 62  OM clause.  pTab
19467 6c 65 20 61 6e 64 20 70 44 61 74 61 62 61 73 65  le and pDatabase
19468 0a 2a 2a 20 61 72 65 20 74 68 65 20 6e 61 6d 65  .** are the name
19469 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e   of the table an
1946a 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64  d database named
1946b 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
1946c 75 73 65 20 74 65 72 6d 2e 0a 2a 2a 20 70 44 61  use term..** pDa
1946d 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 20 69  tabase is NULL i
1946e 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  f the database n
1946f 61 6d 65 20 71 75 61 6c 69 66 69 65 72 20 69 73  ame qualifier is
19470 20 6d 69 73 73 69 6e 67 20 2d 20 74 68 65 0a 2a   missing - the.*
19471 2a 20 75 73 75 61 6c 20 63 61 73 65 2e 20 20 49  * usual case.  I
19472 66 20 74 68 65 20 74 65 72 6d 20 68 61 73 20 61  f the term has a
19473 20 61 6c 69 61 73 2c 20 74 68 65 6e 20 70 41 6c   alias, then pAl
19474 69 61 73 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ias points to th
19475 65 0a 2a 2a 20 61 6c 69 61 73 20 74 6f 6b 65 6e  e.** alias token
19476 2e 20 20 49 66 20 74 68 65 20 74 65 72 6d 20 69  .  If the term i
19477 73 20 61 20 73 75 62 71 75 65 72 79 2c 20 74 68  s a subquery, th
19478 65 6e 20 70 53 75 62 71 75 65 72 79 20 69 73 20  en pSubquery is 
19479 74 68 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74  the.** SELECT st
1947a 61 74 65 6d 65 6e 74 20 74 68 61 74 20 74 68 65  atement that the
1947b 20 73 75 62 71 75 65 72 79 20 65 6e 63 6f 64 65   subquery encode
1947c 73 2e 20 20 54 68 65 20 70 54 61 62 6c 65 20 61  s.  The pTable a
1947d 6e 64 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20  nd.** pDatabase 
1947e 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 4e  parameters are N
1947f 55 4c 4c 20 66 6f 72 20 73 75 62 71 75 65 72 69  ULL for subqueri
19480 65 73 2e 20 20 54 68 65 20 70 4f 6e 20 61 6e 64  es.  The pOn and
19481 20 70 55 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d   pUsing.** param
19482 65 74 65 72 73 20 61 72 65 20 74 68 65 20 63 6f  eters are the co
19483 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 4f 4e 20  ntent of the ON 
19484 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
19485 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  s..**.** Return 
19486 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 77 68  a new SrcList wh
19487 69 63 68 20 65 6e 63 6f 64 65 73 20 69 73 20 74  ich encodes is t
19488 68 65 20 46 52 4f 4d 20 77 69 74 68 20 74 68 65  he FROM with the
19489 20 6e 65 77 0a 2a 2a 20 74 65 72 6d 20 61 64 64   new.** term add
1948a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1948b 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
1948c 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
1948d 65 6e 64 46 72 6f 6d 54 65 72 6d 28 0a 20 20 50  endFromTerm(.  P
1948e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1948f 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
19490 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
19491 72 63 4c 69 73 74 20 2a 70 2c 20 20 20 20 20 20  rcList *p,      
19492 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65         /* The le
19493 66 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 46  ft part of the F
19494 52 4f 4d 20 63 6c 61 75 73 65 20 61 6c 72 65 61  ROM clause alrea
19495 64 79 20 73 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b  dy seen */.  Tok
19496 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20  en *pTable,     
19497 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
19498 74 68 65 20 74 61 62 6c 65 20 74 6f 20 61 64 64  the table to add
19499 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   to the FROM cla
1949a 75 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  use */.  Token *
1949b 70 44 61 74 61 62 61 73 65 2c 20 20 20 20 20 20  pDatabase,      
1949c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1949d 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
1949e 69 6e 67 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20  ing pTable */.  
1949f 54 6f 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20 20  Token *pAlias,  
194a0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
194a1 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  ight-hand side o
194a2 66 20 74 68 65 20 41 53 20 73 75 62 65 78 70 72  f the AS subexpr
194a3 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65  ession */.  Sele
194a4 63 74 20 2a 70 53 75 62 71 75 65 72 79 2c 20 20  ct *pSubquery,  
194a5 20 20 20 20 2f 2a 20 41 20 73 75 62 71 75 65 72      /* A subquer
194a6 79 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20  y used in place 
194a7 6f 66 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20  of a table name 
194a8 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c 20  */.  Expr *pOn, 
194a9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
194aa 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66  The ON clause of
194ab 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c   a join */.  IdL
194ac 69 73 74 20 2a 70 55 73 69 6e 67 20 20 20 20 20  ist *pUsing     
194ad 20 20 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e       /* The USIN
194ae 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f  G clause of a jo
194af 69 6e 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63  in */.){.  struc
194b0 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
194b1 70 49 74 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33  pItem;.  sqlite3
194b2 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
194b3 62 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  b;.  p = sqlite3
194b4 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62  SrcListAppend(db
194b5 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 61  , p, pTable, pDa
194b6 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  tabase);.  if( p
194b7 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 53 72 63 3d 3d  ==0 || p->nSrc==
194b8 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
194b9 45 78 70 72 44 65 6c 65 74 65 28 70 4f 6e 29 3b  ExprDelete(pOn);
194ba 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69  .    sqlite3IdLi
194bb 73 74 44 65 6c 65 74 65 28 70 55 73 69 6e 67 29  stDelete(pUsing)
194bc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
194bd 65 63 74 44 65 6c 65 74 65 28 70 53 75 62 71 75  ectDelete(pSubqu
194be 65 72 79 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ery);.    return
194bf 20 70 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20   p;.  }.  pItem 
194c0 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d  = &p->a[p->nSrc-
194c1 31 5d 3b 0a 20 20 69 66 28 20 70 41 6c 69 61 73  1];.  if( pAlias
194c2 20 26 26 20 70 41 6c 69 61 73 2d 3e 6e 20 29 7b   && pAlias->n ){
194c3 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  .    pItem->zAli
194c4 61 73 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  as = sqlite3Name
194c5 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 41  FromToken(db, pA
194c6 6c 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70 49 74  lias);.  }.  pIt
194c7 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53  em->pSelect = pS
194c8 75 62 71 75 65 72 79 3b 0a 20 20 70 49 74 65 6d  ubquery;.  pItem
194c9 2d 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20 70  ->pOn = pOn;.  p
194ca 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 70  Item->pUsing = p
194cb 55 73 69 6e 67 3b 0a 20 20 72 65 74 75 72 6e 20  Using;.  return 
194cc 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  p;.}../*.** When
194cd 20 62 75 69 6c 64 69 6e 67 20 75 70 20 61 20 46   building up a F
194ce 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e 20 74 68  ROM clause in th
194cf 65 20 70 61 72 73 65 72 2c 20 74 68 65 20 6a 6f  e parser, the jo
194d0 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69  in operator.** i
194d1 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 74 74 61  s initially atta
194d2 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65 66 74  ched to the left
194d3 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 74 20 74   operand.  But t
194d4 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
194d5 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 74 68 65  r.** expects the
194d6 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 74   join operator t
194d7 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 69 67 68  o be on the righ
194d8 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73  t operand.  This
194d9 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 68 69 66   routine.** Shif
194da 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70 65 72  ts all join oper
194db 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 66 74 20  ators from left 
194dc 74 6f 20 72 69 67 68 74 20 66 6f 72 20 61 6e 20  to right for an 
194dd 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a 2a 20 63  entire FROM.** c
194de 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61  lause..**.** Exa
194df 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 20 74 68  mple: Suppose th
194e0 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b 65 20 74  e join is like t
194e1 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
194e2 20 20 20 20 20 41 20 6e 61 74 75 72 61 6c 20 63       A natural c
194e3 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a 0a 2a  ross join B.**.*
194e4 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 69  * The operator i
194e5 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f 73 73  s "natural cross
194e6 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41 20 61   join".  The A a
194e7 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20 61 72  nd B operands ar
194e8 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 70  e stored.** in p
194e9 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e 61 5b  ->a[0] and p->a[
194ea 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  1], respectively
194eb 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 69 6e  .  The parser in
194ec 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73 20 74  itially stores t
194ed 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 77  he.** operator w
194ee 69 74 68 20 41 2e 20 20 54 68 69 73 20 72 6f 75  ith A.  This rou
194ef 74 69 6e 65 20 73 68 69 66 74 73 20 74 68 61 74  tine shifts that
194f0 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72 20 74   operator over t
194f1 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  o B..*/.SQLITE_P
194f2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
194f3 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
194f4 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 20  oinType(SrcList 
194f5 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20  *p){.  if( p && 
194f6 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69 6e 74 20  p->a ){.    int 
194f7 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 70 2d 3e  i;.    for(i=p->
194f8 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d  nSrc-1; i>0; i--
194f9 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 5b 69 5d  ){.      p->a[i]
194fa 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 2d 3e 61  .jointype = p->a
194fb 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65 3b 0a  [i-1].jointype;.
194fc 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 5b 30      }.    p->a[0
194fd 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a  ].jointype = 0;.
194fe 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67    }.}../*.** Beg
194ff 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
19500 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19501 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
19502 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28  eginTransaction(
19503 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
19504 6e 74 20 74 79 70 65 29 7b 0a 20 20 73 71 6c 69  nt type){.  sqli
19505 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20  te3 *db;.  Vdbe 
19506 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  *v;.  int i;..  
19507 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 7c 7c  if( pParse==0 ||
19508 20 28 64 62 3d 70 50 61 72 73 65 2d 3e 64 62 29   (db=pParse->db)
19509 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44 62 5b 30  ==0 || db->aDb[0
1950a 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72  ].pBt==0 ) retur
1950b 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  n;.  if( pParse-
1950c 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
1950d 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
1950e 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rn;.  if( sqlite
1950f 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
19510 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41  e, SQLITE_TRANSA
19511 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22 2c 20  CTION, "BEGIN", 
19512 30 2c 20 30 29 20 29 20 72 65 74 75 72 6e 3b 0a  0, 0) ) return;.
19513 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
19514 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
19515 20 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e   if( !v ) return
19516 3b 0a 20 20 69 66 28 20 74 79 70 65 21 3d 54 4b  ;.  if( type!=TK
19517 5f 44 45 46 45 52 52 45 44 20 29 7b 0a 20 20 20  _DEFERRED ){.   
19518 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
19519 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
1951a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1951b 70 28 76 2c 20 4f 50 5f 54 72 61 6e 73 61 63 74  p(v, OP_Transact
1951c 69 6f 6e 2c 20 69 2c 20 28 74 79 70 65 3d 3d 54  ion, i, (type==T
1951d 4b 5f 45 58 43 4c 55 53 49 56 45 29 2b 31 29 3b  K_EXCLUSIVE)+1);
1951e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1951f 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
19520 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
19521 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
19522 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74  v, OP_AutoCommit
19523 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , 0, 0);.}../*.*
19524 2a 20 43 6f 6d 6d 69 74 20 61 20 74 72 61 6e 73  * Commit a trans
19525 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45  action.*/.SQLITE
19526 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19527 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73  lite3CommitTrans
19528 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50  action(Parse *pP
19529 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
1952a 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b   *db;.  Vdbe *v;
1952b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d  ..  if( pParse==
1952c 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 65 2d  0 || (db=pParse-
1952d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61  >db)==0 || db->a
1952e 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72  Db[0].pBt==0 ) r
1952f 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61  eturn;.  if( pPa
19530 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
19531 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
19532 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71  return;.  if( sq
19533 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
19534 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52  Parse, SQLITE_TR
19535 41 4e 53 41 43 54 49 4f 4e 2c 20 22 43 4f 4d 4d  ANSACTION, "COMM
19536 49 54 22 2c 20 30 2c 20 30 29 20 29 20 72 65 74  IT", 0, 0) ) ret
19537 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69  urn;..  v = sqli
19538 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
19539 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  e);.  if( v ){. 
1953a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1953b 64 4f 70 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f  dOp(v, OP_AutoCo
1953c 6d 6d 69 74 2c 20 31 2c 20 30 29 3b 0a 20 20 7d  mmit, 1, 0);.  }
1953d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61  .}../*.** Rollba
1953e 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ck a transaction
1953f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19540 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
19541 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69  ollbackTransacti
19542 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
19543 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
19544 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  ;.  Vdbe *v;..  
19545 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 7c 7c  if( pParse==0 ||
19546 20 28 64 62 3d 70 50 61 72 73 65 2d 3e 64 62 29   (db=pParse->db)
19547 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44 62 5b 30  ==0 || db->aDb[0
19548 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72  ].pBt==0 ) retur
19549 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  n;.  if( pParse-
1954a 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
1954b 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
1954c 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rn;.  if( sqlite
1954d 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
1954e 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41  e, SQLITE_TRANSA
1954f 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c 42 41 43 4b  CTION, "ROLLBACK
19550 22 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75 72  ", 0, 0) ) retur
19551 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  n;..  v = sqlite
19552 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
19553 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
19554 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19555 70 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d  p(v, OP_AutoComm
19556 69 74 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 0a 7d  it, 1, 1);.  }.}
19557 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
19558 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62  e the TEMP datab
19559 61 73 65 20 69 73 20 6f 70 65 6e 20 61 6e 64 20  ase is open and 
1955a 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73  available for us
1955b 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68  e.  Return.** th
1955c 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
1955d 72 73 2e 20 20 4c 65 61 76 65 20 61 6e 79 20 65  rs.  Leave any e
1955e 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e  rror messages in
1955f 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75   the pParse stru
19560 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
19561 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19562 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61  ite3OpenTempData
19563 62 61 73 65 28 50 61 72 73 65 20 2a 70 50 61 72  base(Parse *pPar
19564 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  se){.  sqlite3 *
19565 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
19566 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31  .  if( db->aDb[1
19567 5d 2e 70 42 74 3d 3d 30 20 26 26 20 21 70 50 61  ].pBt==0 && !pPa
19568 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a  rse->explain ){.
19569 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
1956a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74  static const int
1956b 20 66 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20   flags = .      
1956c 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
1956d 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20  READWRITE |.    
1956e 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
1956f 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20  N_CREATE |.     
19570 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
19571 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20  _EXCLUSIVE |.   
19572 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
19573 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
19574 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c   |.          SQL
19575 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42  ITE_OPEN_TEMP_DB
19576 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
19577 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
19578 64 62 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45  db, 0, 0, SQLITE
19579 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1957a 49 5a 45 2c 20 66 6c 61 67 73 2c 0a 20 20 20 20  IZE, flags,.    
1957b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1957c 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64 62               &db
1957d 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 3b 0a 20  ->aDb[1].pBt);. 
1957e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1957f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
19580 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
19581 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f  arse, "unable to
19582 20 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72   open a temporar
19583 79 20 64 61 74 61 62 61 73 65 20 22 0a 20 20 20  y database ".   
19584 20 20 20 20 20 22 66 69 6c 65 20 66 6f 72 20 73       "file for s
19585 74 6f 72 69 6e 67 20 74 65 6d 70 6f 72 61 72 79  toring temporary
19586 20 74 61 62 6c 65 73 22 29 3b 0a 20 20 20 20 20   tables");.     
19587 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 72 63   pParse->rc = rc
19588 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
19589 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1958a 64 62 2d 3e 66 6c 61 67 73 20 26 20 21 64 62 2d  db->flags & !db-
1958b 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
1958c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1958d 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73  3BtreeBeginTrans
1958e 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 2c  (db->aDb[1].pBt,
1958f 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   1);.      if( r
19590 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
19591 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
19592 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
19593 22 75 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 61  "unable to get a
19594 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 22   write lock on "
19595 0a 20 20 20 20 20 20 20 20 20 20 22 74 68 65 20  .          "the 
19596 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
19597 73 65 20 66 69 6c 65 22 29 3b 0a 20 20 20 20 20  se file");.     
19598 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
19599 72 63 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  rc;.        retu
1959a 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
1959b 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1959c 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
1959d 6d 61 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ma );.  }.  retu
1959e 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  rn 0;.}../*.** G
1959f 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64  enerate VDBE cod
195a0 65 20 74 68 61 74 20 77 69 6c 6c 20 76 65 72 69  e that will veri
195a1 66 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  fy the schema co
195a2 6f 6b 69 65 20 61 6e 64 20 73 74 61 72 74 0a 2a  okie and start.*
195a3 2a 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63  * a read-transac
195a4 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d  tion for all nam
195a5 65 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ed database file
195a6 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69  s..**.** It is i
195a7 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 6c  mportant that al
195a8 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73  l schema cookies
195a9 20 62 65 20 76 65 72 69 66 69 65 64 20 61 6e 64   be verified and
195aa 20 61 6c 6c 0a 2a 2a 20 72 65 61 64 20 74 72 61   all.** read tra
195ab 6e 73 61 63 74 69 6f 6e 73 20 62 65 20 73 74 61  nsactions be sta
195ac 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 74  rted before anyt
195ad 68 69 6e 67 20 65 6c 73 65 20 68 61 70 70 65 6e  hing else happen
195ae 73 20 69 6e 0a 2a 2a 20 74 68 65 20 56 44 42 45  s in.** the VDBE
195af 20 70 72 6f 67 72 61 6d 2e 20 20 42 75 74 20 74   program.  But t
195b0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20  his routine can 
195b1 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  be called after 
195b2 6d 75 63 68 20 6f 74 68 65 72 0a 2a 2a 20 63 6f  much other.** co
195b3 64 65 20 68 61 73 20 62 65 65 6e 20 67 65 6e 65  de has been gene
195b4 72 61 74 65 64 2e 20 20 53 6f 20 68 65 72 65 20  rated.  So here 
195b5 69 73 20 77 68 61 74 20 77 65 20 64 6f 3a 0a 2a  is what we do:.*
195b6 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
195b7 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ime this routine
195b8 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 63   is called, we c
195b9 6f 64 65 20 61 6e 20 4f 50 5f 47 6f 74 6f 20 74  ode an OP_Goto t
195ba 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70  hat.** will jump
195bb 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65   to a subroutine
195bc 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
195bd 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 65  he program.  The
195be 6e 20 77 65 0a 2a 2a 20 72 65 63 6f 72 64 20 65  n we.** record e
195bf 76 65 72 79 20 64 61 74 61 62 61 73 65 20 74 68  very database th
195c0 61 74 20 6e 65 65 64 73 20 69 74 73 20 73 63 68  at needs its sch
195c1 65 6d 61 20 76 65 72 69 66 69 65 64 20 69 6e 20  ema verified in 
195c2 74 68 65 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 63  the.** pParse->c
195c3 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65 6c 64 2e  ookieMask field.
195c4 20 20 4c 61 74 65 72 2c 20 61 66 74 65 72 20 61    Later, after a
195c5 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61  ll other code ha
195c6 73 20 62 65 65 6e 0a 2a 2a 20 67 65 6e 65 72 61  s been.** genera
195c7 74 65 64 2c 20 74 68 65 20 73 75 62 72 6f 75 74  ted, the subrout
195c8 69 6e 65 20 74 68 61 74 20 64 6f 65 73 20 74 68  ine that does th
195c9 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63  e cookie verific
195ca 61 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 73 74  ations and.** st
195cb 61 72 74 73 20 74 68 65 20 74 72 61 6e 73 61 63  arts the transac
195cc 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20 63 6f  tions will be co
195cd 64 65 64 20 61 6e 64 20 74 68 65 20 4f 50 5f 47  ded and the OP_G
195ce 6f 74 6f 20 50 32 20 76 61 6c 75 65 0a 2a 2a 20  oto P2 value.** 
195cf 77 69 6c 6c 20 62 65 20 6d 61 64 65 20 74 6f 20  will be made to 
195d0 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73 75  point to that su
195d1 62 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 67  broutine.  The g
195d2 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  eneration of the
195d3 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76 65 72 69 66  .** cookie verif
195d4 69 63 61 74 69 6f 6e 20 73 75 62 72 6f 75 74 69  ication subrouti
195d5 6e 65 20 63 6f 64 65 20 68 61 70 70 65 6e 73 20  ne code happens 
195d6 69 6e 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  in sqlite3Finish
195d7 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20  Coding()..**.** 
195d8 49 66 20 69 44 62 3c 30 20 74 68 65 6e 20 63 6f  If iDb<0 then co
195d9 64 65 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f  de the OP_Goto o
195da 6e 6c 79 20 2d 20 64 6f 6e 27 74 20 73 65 74 20  nly - don't set 
195db 66 6c 61 67 20 74 6f 20 76 65 72 69 66 79 20 74  flag to verify t
195dc 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20  he.** schema on 
195dd 61 6e 79 20 64 61 74 61 62 61 73 65 73 2e 20 20  any databases.  
195de 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
195df 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 74 68 65   to position the
195e0 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c   OP_Goto.** earl
195e1 79 20 69 6e 20 74 68 65 20 63 6f 64 65 2c 20 62  y in the code, b
195e2 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77 20 69 66  efore we know if
195e3 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 74 61   any database ta
195e4 62 6c 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65  bles will be use
195e5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
195e6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
195e7 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
195e8 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  a(Parse *pParse,
195e9 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c   int iDb){.  sql
195ea 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65  ite3 *db;.  Vdbe
195eb 20 2a 76 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b   *v;.  int mask;
195ec 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
195ed 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
195ee 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
195ef 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e  urn;  /* This on
195f0 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20 74 68  ly happens if th
195f1 65 72 65 20 77 61 73 20 61 20 70 72 69 6f 72 20  ere was a prior 
195f2 65 72 72 6f 72 20 2a 2f 0a 20 20 64 62 20 3d 20  error */.  db = 
195f3 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
195f4 28 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  ( pParse->cookie
195f5 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 70  Goto==0 ){.    p
195f6 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
195f7 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  o = sqlite3VdbeA
195f8 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  ddOp(v, OP_Goto,
195f9 20 30 2c 20 30 29 2b 31 3b 0a 20 20 7d 0a 20 20   0, 0)+1;.  }.  
195fa 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20  if( iDb>=0 ){.  
195fb 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
195fc 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73  ->nDb );.    ass
195fd 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62  ert( db->aDb[iDb
195fe 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69 44 62 3d  ].pBt!=0 || iDb=
195ff 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
19600 28 20 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41 58  ( iDb<SQLITE_MAX
19601 5f 41 54 54 41 43 48 45 44 2b 32 20 29 3b 0a 20  _ATTACHED+2 );. 
19602 20 20 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 44 62     mask = 1<<iDb
19603 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 72 73  ;.    if( (pPars
19604 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 26 20  e->cookieMask & 
19605 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20  mask)==0 ){.    
19606 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65    pParse->cookie
19607 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20  Mask |= mask;.  
19608 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b      pParse->cook
19609 69 65 56 61 6c 75 65 5b 69 44 62 5d 20 3d 20 64  ieValue[iDb] = d
1960a 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
1960b 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b  ema->schema_cook
1960c 69 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 4f  ie;.      if( !O
1960d 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44  MIT_TEMPDB && iD
1960e 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  b==1 ){.        
1960f 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44  sqlite3OpenTempD
19610 61 74 61 62 61 73 65 28 70 50 61 72 73 65 29 3b  atabase(pParse);
19611 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
19612 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65   }.}../*.** Gene
19613 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74  rate VDBE code t
19614 68 61 74 20 70 72 65 70 61 72 65 73 20 66 6f 72  hat prepares for
19615 20 64 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61 74   doing an operat
19616 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67 68  ion that.** migh
19617 74 20 63 68 61 6e 67 65 20 74 68 65 20 64 61 74  t change the dat
19618 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  abase..**.** Thi
19619 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73  s routine starts
1961a 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69   a new transacti
1961b 6f 6e 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74  on if we are not
1961c 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 0a   already within.
1961d 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ** a transaction
1961e 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 6c 72  .  If we are alr
1961f 65 61 64 79 20 77 69 74 68 69 6e 20 61 20 74 72  eady within a tr
19620 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
19621 61 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20  a checkpoint.** 
19622 69 73 20 73 65 74 20 69 66 20 74 68 65 20 73 65  is set if the se
19623 74 53 74 61 74 65 6d 65 6e 74 20 70 61 72 61 6d  tStatement param
19624 65 74 65 72 20 69 73 20 74 72 75 65 2e 20 20 41  eter is true.  A
19625 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75   checkpoint shou
19626 6c 64 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f 72  ld.** be set for
19627 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74   operations that
19628 20 6d 69 67 68 74 20 66 61 69 6c 20 28 64 75 65   might fail (due
19629 20 74 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74   to a constraint
1962a 29 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65  ) part of.** the
1962b 20 77 61 79 20 74 68 72 6f 75 67 68 20 61 6e 64   way through and
1962c 20 77 68 69 63 68 20 77 69 6c 6c 20 6e 65 65 64   which will need
1962d 20 74 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77 72   to undo some wr
1962e 69 74 65 73 20 77 69 74 68 6f 75 74 20 68 61 76  ites without hav
1962f 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 61  ing to.** rollba
19630 63 6b 20 74 68 65 20 77 68 6f 6c 65 20 74 72 61  ck the whole tra
19631 6e 73 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20 6f  nsaction.  For o
19632 70 65 72 61 74 69 6f 6e 73 20 77 68 65 72 65 20  perations where 
19633 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  all constraints.
19634 2a 2a 20 63 61 6e 20 62 65 20 63 68 65 63 6b 65  ** can be checke
19635 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61  d before any cha
19636 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f  nges are made to
19637 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 69   the database, i
19638 74 20 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e 65  t is never.** ne
19639 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20  cessary to undo 
1963a 61 20 77 72 69 74 65 20 61 6e 64 20 74 68 65 20  a write and the 
1963b 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c  checkpoint shoul
1963c 64 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2a  d not be set..**
1963d 0a 2a 2a 20 4f 6e 6c 79 20 64 61 74 61 62 61 73  .** Only databas
1963e 65 20 69 44 62 20 61 6e 64 20 74 68 65 20 74 65  e iDb and the te
1963f 6d 70 20 64 61 74 61 62 61 73 65 20 61 72 65 20  mp database are 
19640 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 62 79  made writable by
19641 20 74 68 69 73 20 63 61 6c 6c 2e 0a 2a 2a 20 49   this call..** I
19642 66 20 69 44 62 3d 3d 30 2c 20 74 68 65 6e 20 74  f iDb==0, then t
19643 68 65 20 6d 61 69 6e 20 61 6e 64 20 74 65 6d 70  he main and temp
19644 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 6d   databases are m
19645 61 64 65 20 77 72 69 74 61 62 6c 65 2e 20 20 20  ade writable.   
19646 49 66 0a 2a 2a 20 69 44 62 3d 3d 31 20 74 68 65  If.** iDb==1 the
19647 6e 20 6f 6e 6c 79 20 74 68 65 20 74 65 6d 70 20  n only the temp 
19648 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 64 65  database is made
19649 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66 20 69   writable.  If i
1964a 44 62 3e 31 20 74 68 65 6e 20 74 68 65 0a 2a 2a  Db>1 then the.**
1964b 20 73 70 65 63 69 66 69 65 64 20 61 75 78 69 6c   specified auxil
1964c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 61 6e  iary database an
1964d 64 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62  d the temp datab
1964e 61 73 65 20 61 72 65 20 6d 61 64 65 20 77 72 69  ase are made wri
1964f 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
19650 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19651 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
19652 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 20 2a  peration(Parse *
19653 70 50 61 72 73 65 2c 20 69 6e 74 20 73 65 74 53  pParse, int setS
19654 74 61 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69 44  tatement, int iD
19655 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  b){.  Vdbe *v = 
19656 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
19657 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
19658 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  =0 ) return;.  s
19659 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
1965a 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
1965b 44 62 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 77  Db);.  pParse->w
1965c 72 69 74 65 4d 61 73 6b 20 7c 3d 20 31 3c 3c 69  riteMask |= 1<<i
1965d 44 62 3b 0a 20 20 69 66 28 20 73 65 74 53 74 61  Db;.  if( setSta
1965e 74 65 6d 65 6e 74 20 26 26 20 70 50 61 72 73 65  tement && pParse
1965f 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20  ->nested==0 ){. 
19660 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19661 64 4f 70 28 76 2c 20 4f 50 5f 53 74 61 74 65 6d  dOp(v, OP_Statem
19662 65 6e 74 2c 20 69 44 62 2c 20 30 29 3b 0a 20 20  ent, iDb, 0);.  
19663 7d 0a 20 20 69 66 28 20 28 4f 4d 49 54 5f 54 45  }.  if( (OMIT_TE
19664 4d 50 44 42 20 7c 7c 20 69 44 62 21 3d 31 29 20  MPDB || iDb!=1) 
19665 26 26 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61  && pParse->db->a
19666 44 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a  Db[1].pBt!=0 ){.
19667 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
19668 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
19669 50 61 72 73 65 2c 20 73 65 74 53 74 61 74 65 6d  Parse, setStatem
1966a 65 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  ent, 1);.  }.}..
1966b 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
1966c 65 65 20 69 66 20 70 49 6e 64 65 78 20 75 73 65  ee if pIndex use
1966d 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  s the collating 
1966e 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20  sequence pColl. 
1966f 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20   Return.** true 
19670 69 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20 66  if it does and f
19671 61 6c 73 65 20 69 66 20 69 74 20 64 6f 65 73 20  alse if it does 
19672 6e 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  not..*/.#ifndef 
19673 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e  SQLITE_OMIT_REIN
19674 44 45 58 0a 73 74 61 74 69 63 20 69 6e 74 20 63  DEX.static int c
19675 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63 6f  ollationMatch(co
19676 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c  nst char *zColl,
19677 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 29 7b   Index *pIndex){
19678 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
19679 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d 3e 6e  i=0; i<pIndex->n
1967a 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
1967b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
1967c 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c  = pIndex->azColl
1967d 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  [i];.    if( z==
1967e 7a 43 6f 6c 6c 20 7c 7c 20 28 7a 20 26 26 20 7a  zColl || (z && z
1967f 43 6f 6c 6c 20 26 26 20 30 3d 3d 73 71 6c 69 74  Coll && 0==sqlit
19680 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f  e3StrICmp(z, zCo
19681 6c 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ll)) ){.      re
19682 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
19683 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
19684 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
19685 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69  compute all indi
19686 63 65 73 20 6f 66 20 70 54 61 62 20 74 68 61 74  ces of pTab that
19687 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69   use the collati
19688 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c  ng sequence pCol
19689 6c 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d  l..** If pColl==
1968a 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65  0 then recompute
1968b 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
1968c 70 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  pTab..*/.#ifndef
1968d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49   SQLITE_OMIT_REI
1968e 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64  NDEX.static void
1968f 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50 61   reindexTable(Pa
19690 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
19691 6c 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20 63  le *pTab, char c
19692 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20  onst *zColl){.  
19693 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20  Index *pIndex;  
19694 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
19695 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74  n index associat
19696 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a  ed with pTab */.
19697 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54  .  for(pIndex=pT
19698 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64  ab->pIndex; pInd
19699 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65  ex; pIndex=pInde
1969a 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  x->pNext){.    i
1969b 66 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63  f( zColl==0 || c
1969c 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43  ollationMatch(zC
1969d 6f 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0a  oll, pIndex) ){.
1969e 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
1969f 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
196a0 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
196a1 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
196a2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65  .      sqlite3Be
196a3 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
196a4 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
196a5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
196a6 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72  RefillIndex(pPar
196a7 73 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b  se, pIndex, -1);
196a8 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e  .    }.  }.}.#en
196a9 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d  dif../*.** Recom
196aa 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73  pute all indices
196ab 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69   of all tables i
196ac 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20  n all databases 
196ad 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e 64  where the.** ind
196ae 69 63 65 73 20 75 73 65 20 74 68 65 20 63 6f 6c  ices use the col
196af 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
196b0 70 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c  pColl.  If pColl
196b1 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75  ==0 then recompu
196b2 74 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63 65  te.** all indice
196b3 73 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a 2f  s everywhere..*/
196b4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
196b5 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61  OMIT_REINDEX.sta
196b6 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78  tic void reindex
196b7 44 61 74 61 62 61 73 65 73 28 50 61 72 73 65 20  Databases(Parse 
196b8 2a 70 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f  *pParse, char co
196b9 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44  nst *zColl){.  D
196ba 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20  b *pDb;         
196bb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
196bc 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
196bd 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
196be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
196bf 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
196c0 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f   index number */
196c1 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
196c2 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f   pParse->db;   /
196c3 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
196c4 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 48  onnection */.  H
196c5 61 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20 20  ashElem *k;     
196c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
196c7 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74  r looping over t
196c8 61 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f 0a  ables in pDb */.
196c9 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
196ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
196cb 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20   A table in the 
196cc 64 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20 66  database */..  f
196cd 6f 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64 62  or(iDb=0, pDb=db
196ce 2d 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e 6e  ->aDb; iDb<db->n
196cf 44 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b 2b  Db; iDb++, pDb++
196d0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
196d1 44 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72  Db!=0 );.    for
196d2 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (k=sqliteHashFir
196d3 73 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61  st(&pDb->pSchema
196d4 2d 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b 20  ->tblHash);  k; 
196d5 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  k=sqliteHashNext
196d6 28 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61 62  (k)){.      pTab
196d7 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74   = (Table*)sqlit
196d8 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20  eHashData(k);.  
196d9 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65      reindexTable
196da 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a  (pParse, pTab, z
196db 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Coll);.    }.  }
196dc 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
196dd 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
196de 6f 72 20 74 68 65 20 52 45 49 4e 44 45 58 20 63  or the REINDEX c
196df 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  ommand..**.**   
196e0 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 20 20       REINDEX    
196e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
196e2 20 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a 20          -- 1.** 
196e3 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20         REINDEX  
196e4 3c 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20 20  <collation>     
196e5 20 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a 2a            -- 2.*
196e6 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58  *        REINDEX
196e7 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c    ?<database>.?<
196e8 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20 33  tablename>  -- 3
196e9 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44  .**        REIND
196ea 45 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e  EX  ?<database>.
196eb 3f 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d 2d  ?<indexname>  --
196ec 20 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20   4.**.** Form 1 
196ed 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69 63  causes all indic
196ee 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63 68  es in all attach
196ef 65 64 20 64 61 74 61 62 61 73 65 73 20 74 6f 20  ed databases to 
196f0 62 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20 46  be rebuilt..** F
196f1 6f 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20 61  orm 2 rebuilds a
196f2 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c  ll indices in al
196f3 6c 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74  l databases that
196f4 20 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a 2a   use the named.*
196f5 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  * collating func
196f6 74 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20 61  tion.  Forms 3 a
196f7 6e 64 20 34 20 72 65 62 75 69 6c 64 20 74 68 65  nd 4 rebuild the
196f8 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72 20   named index or 
196f9 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 61  all.** indices a
196fa 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
196fb 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a  he named table..
196fc 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
196fd 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 53  E_OMIT_REINDEX.S
196fe 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
196ff 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65  id sqlite3Reinde
19700 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  x(Parse *pParse,
19701 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
19702 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a  Token *pName2){.
19703 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
19704 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
19705 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
19706 6e 63 65 20 74 6f 20 62 65 20 72 65 69 6e 64 65  nce to be reinde
19707 78 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  xed, or NULL */.
19708 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20    char *z;      
19709 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1970a 20 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65   Name of a table
1970b 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63   or index */.  c
1970c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20  onst char *zDb; 
1970d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
1970e 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1970f 73 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  se */.  Table *p
19710 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tab;            
19711 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69      /* A table i
19712 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  n the database *
19713 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  /.  Index *pInde
19714 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
19715 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f  /* An index asso
19716 63 69 61 74 65 64 20 77 69 74 68 20 70 54 61 62  ciated with pTab
19717 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
19718 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19719 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1971a 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a  e index number *
1971b 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
1971c 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
1971d 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1971e 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
1971f 54 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b  Token *pObjName;
19720 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
19721 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
19722 20 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 65 20   or index to be 
19723 72 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a 20 20  reindexed */..  
19724 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61  /* Read the data
19725 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20  base schema. If 
19726 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
19727 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20   leave an error 
19728 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64  message.  ** and
19729 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20   code in pParse 
1972a 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  and return NULL.
1972b 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
1972c 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
1972d 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
1972e 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
1972f 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d  }..  if( pName1=
19730 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 2d 3e 7a 3d  =0 || pName1->z=
19731 3d 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65  =0 ){.    reinde
19732 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72 73  xDatabases(pPars
19733 65 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72  e, 0);.    retur
19734 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  n;.  }else if( p
19735 4e 61 6d 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d  Name2==0 || pNam
19736 65 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  e2->z==0 ){.    
19737 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e  assert( pName1->
19738 7a 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  z );.    pColl =
19739 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
1973a 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c  Seq(db, ENC(db),
1973b 20 28 63 68 61 72 2a 29 70 4e 61 6d 65 31 2d 3e   (char*)pName1->
1973c 7a 2c 20 70 4e 61 6d 65 31 2d 3e 6e 2c 20 30 29  z, pName1->n, 0)
1973d 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ;.    if( pColl 
1973e 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
1973f 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  Coll = sqlite3Db
19740 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 6f 6e  StrNDup(db, (con
19741 73 74 20 63 68 61 72 20 2a 29 70 4e 61 6d 65 31  st char *)pName1
19742 2d 3e 7a 2c 20 70 4e 61 6d 65 31 2d 3e 6e 29 3b  ->z, pName1->n);
19743 0a 20 20 20 20 20 20 69 66 28 20 7a 43 6f 6c 6c  .      if( zColl
19744 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 69 6e   ){.        rein
19745 64 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61  dexDatabases(pPa
19746 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20  rse, zColl);.   
19747 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
19748 65 28 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20  e(zColl);.      
19749 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  }.      return;.
1974a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 44 62 20      }.  }.  iDb 
1974b 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
1974c 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
1974d 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4f  me1, pName2, &pO
1974e 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69  bjName);.  if( i
1974f 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  Db<0 ) return;. 
19750 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65   z = sqlite3Name
19751 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4f  FromToken(db, pO
19752 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a  bjName);.  if( z
19753 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
19754 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
19755 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62  b].zName;.  pTab
19756 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
19757 62 6c 65 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b  ble(db, z, zDb);
19758 0a 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20  .  if( pTab ){. 
19759 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28     reindexTable(
1975a 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 29  pParse, pTab, 0)
1975b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
1975c 65 65 28 7a 29 3b 0a 20 20 20 20 72 65 74 75 72  ee(z);.    retur
1975d 6e 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20  n;.  }.  pIndex 
1975e 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  = sqlite3FindInd
1975f 65 78 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a  ex(db, z, zDb);.
19760 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
19761 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20  );.  if( pIndex 
19762 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ){.    sqlite3Be
19763 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
19764 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
19765 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
19766 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65  fillIndex(pParse
19767 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20  , pIndex, -1);. 
19768 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
19769 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1976a 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c 65  (pParse, "unable
1976b 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65   to identify the
1976c 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 72 65   object to be re
1976d 69 6e 64 65 78 65 64 22 29 3b 0a 7d 0a 23 65 6e  indexed");.}.#en
1976e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
1976f 6e 20 61 20 64 79 6e 61 6d 69 63 6c 79 20 61 6c  n a dynamicly al
19770 6c 6f 63 61 74 65 64 20 4b 65 79 49 6e 66 6f 20  located KeyInfo 
19771 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 63  structure that c
19772 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 77 69  an be used.** wi
19773 74 68 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f  th OP_OpenRead o
19774 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 74  r OP_OpenWrite t
19775 6f 20 61 63 63 65 73 73 20 64 61 74 61 62 61 73  o access databas
19776 65 20 69 6e 64 65 78 20 70 49 64 78 2e 0a 2a 2a  e index pIdx..**
19777 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
19778 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  l, a pointer to 
19779 74 68 65 20 6e 65 77 20 73 74 72 75 63 74 75 72  the new structur
1977a 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  e is returned. I
1977b 6e 20 74 68 69 73 20 63 61 73 65 0a 2a 2a 20 74  n this case.** t
1977c 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
1977d 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c  ponsible for cal
1977e 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65  ling sqlite3_fre
1977f 65 28 29 20 6f 6e 20 74 68 65 20 72 65 74 75 72  e() on the retur
19780 6e 65 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e  ned .** pointer.
19781 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
19782 75 72 73 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f  urs (out of memo
19783 72 79 20 6f 72 20 6d 69 73 73 69 6e 67 20 63 6f  ry or missing co
19784 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 73 65 71 75  llation .** sequ
19785 65 6e 63 65 29 2c 20 4e 55 4c 4c 20 69 73 20 72  ence), NULL is r
19786 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20  eturned and the 
19787 73 74 61 74 65 20 6f 66 20 70 50 61 72 73 65 20  state of pParse 
19788 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65  updated to refle
19789 63 74 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e  ct.** the error.
1978a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1978b 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69  TE KeyInfo *sqli
1978c 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
1978d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49  Parse *pParse, I
1978e 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69  ndex *pIdx){.  i
1978f 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c  nt i;.  int nCol
19790 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   = pIdx->nColumn
19791 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d  ;.  int nBytes =
19792 20 73 69 7a 65 6f 66 28 4b 65 79 49 6e 66 6f 29   sizeof(KeyInfo)
19793 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65   + (nCol-1)*size
19794 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 6e  of(CollSeq*) + n
19795 43 6f 6c 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  Col;.  KeyInfo *
19796 70 4b 65 79 20 3d 20 28 4b 65 79 49 6e 66 6f 20  pKey = (KeyInfo 
19797 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
19798 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62  cZero(pParse->db
19799 2c 20 6e 42 79 74 65 73 29 3b 0a 0a 20 20 69 66  , nBytes);..  if
1979a 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 70 4b  ( pKey ){.    pK
1979b 65 79 2d 3e 64 62 20 3d 20 70 50 61 72 73 65 2d  ey->db = pParse-
1979c 3e 64 62 3b 0a 20 20 20 20 70 4b 65 79 2d 3e 61  >db;.    pKey->a
1979d 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 20  SortOrder = (u8 
1979e 2a 29 26 28 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b  *)&(pKey->aColl[
1979f 6e 43 6f 6c 5d 29 3b 0a 20 20 20 20 61 73 73 65  nCol]);.    asse
197a0 72 74 28 20 26 70 4b 65 79 2d 3e 61 53 6f 72 74  rt( &pKey->aSort
197a1 4f 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d 26 28 28  Order[nCol]==&((
197a2 28 75 38 20 2a 29 70 4b 65 79 29 5b 6e 42 79 74  (u8 *)pKey)[nByt
197a3 65 73 5d 29 20 29 3b 0a 20 20 20 20 66 6f 72 28  es]) );.    for(
197a4 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
197a5 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
197a6 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43  Coll = pIdx->azC
197a7 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73  oll[i];.      as
197a8 73 65 72 74 28 20 7a 43 6f 6c 6c 20 29 3b 0a 20  sert( zColl );. 
197a9 20 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f 6c 6c       pKey->aColl
197aa 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  [i] = sqlite3Loc
197ab 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
197ac 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20  e, zColl, -1);. 
197ad 20 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74       pKey->aSort
197ae 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 64 78 2d  Order[i] = pIdx-
197af 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a  >aSortOrder[i];.
197b0 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79 2d 3e      }.    pKey->
197b1 6e 46 69 65 6c 64 20 3d 20 6e 43 6f 6c 3b 0a 20  nField = nCol;. 
197b2 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65   }..  if( pParse
197b3 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 73 71  ->nErr ){.    sq
197b4 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 29  lite3_free(pKey)
197b5 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 30 3b 0a  ;.    pKey = 0;.
197b6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4b 65    }.  return pKe
197b7 79 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  y;.}../*********
197b8 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 75 69  ***** End of bui
197b9 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ld.c ***********
197ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197bc 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
197bd 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
197be 20 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a   callback.c ****
197bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197c1 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
197c2 20 4d 61 79 20 32 33 20 0a 2a 2a 0a 2a 2a 20 54   May 23 .**.** T
197c3 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
197c4 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
197c5 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
197c6 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
197c7 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
197c8 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
197c9 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
197ca 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
197cb 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
197cc 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
197cd 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
197ce 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
197cf 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
197d0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
197d1 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
197d2 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
197d3 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
197d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197d8 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
197d9 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
197da 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20   functions used 
197db 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 69 6e  to access the in
197dc 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c  ternal hash tabl
197dd 65 73 0a 2a 2a 20 6f 66 20 75 73 65 72 20 64 65  es.** of user de
197de 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20  fined functions 
197df 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  and collation se
197e0 71 75 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 24  quences..**.** $
197e1 49 64 3a 20 63 61 6c 6c 62 61 63 6b 2e 63 2c 76  Id: callback.c,v
197e2 20 31 2e 32 33 20 32 30 30 37 2f 30 38 2f 32 39   1.23 2007/08/29
197e3 20 31 32 3a 33 31 3a 32 36 20 64 61 6e 69 65 6c   12:31:26 daniel
197e4 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
197e5 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
197e6 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65  e 'collation nee
197e7 64 65 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f  ded' callback to
197e8 20 72 65 71 75 65 73 74 20 61 20 63 6f 6c 6c 61   request a colla
197e9 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a  tion sequence.**
197ea 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
197eb 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f   text encoding o
197ec 66 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65  f name zName, le
197ed 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 20 49  ngth nName..** I
197ee 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  f the collation 
197ef 73 65 71 75 65 6e 63 65 0a 2a 2f 0a 73 74 61 74  sequence.*/.stat
197f0 69 63 20 76 6f 69 64 20 63 61 6c 6c 43 6f 6c 6c  ic void callColl
197f1 4e 65 65 64 65 64 28 73 71 6c 69 74 65 33 20 2a  Needed(sqlite3 *
197f2 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
197f3 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65  zName, int nName
197f4 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  ){.  assert( !db
197f5 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 7c 7c  ->xCollNeeded ||
197f6 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65   !db->xCollNeede
197f7 64 31 36 20 29 3b 0a 20 20 69 66 28 20 6e 4e 61  d16 );.  if( nNa
197f8 6d 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73  me<0 ) nName = s
197f9 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20  trlen(zName);.  
197fa 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  if( db->xCollNee
197fb 64 65 64 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ded ){.    char 
197fc 2a 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c  *zExternal = sql
197fd 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
197fe 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
197ff 0a 20 20 20 20 69 66 28 20 21 7a 45 78 74 65 72  .    if( !zExter
19800 6e 61 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  nal ) return;.  
19801 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65    db->xCollNeede
19802 64 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65  d(db->pCollNeede
19803 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74 29 45  dArg, db, (int)E
19804 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72 6e 61  NC(db), zExterna
19805 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  l);.    sqlite3_
19806 66 72 65 65 28 7a 45 78 74 65 72 6e 61 6c 29 3b  free(zExternal);
19807 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
19808 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20  ITE_OMIT_UTF16. 
19809 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65   if( db->xCollNe
1980a 65 64 65 64 31 36 20 29 7b 0a 20 20 20 20 63 68  eded16 ){.    ch
1980b 61 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72  ar const *zExter
1980c 6e 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nal;.    sqlite3
1980d 5f 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73  _value *pTmp = s
1980e 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64  qlite3ValueNew(d
1980f 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b);.    sqlite3V
19810 61 6c 75 65 53 65 74 53 74 72 28 70 54 6d 70 2c  alueSetStr(pTmp,
19811 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 53   nName, zName, S
19812 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
19813 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
19814 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69  zExternal = sqli
19815 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 54 6d  te3ValueText(pTm
19816 70 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  p, SQLITE_UTF16N
19817 41 54 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20  ATIVE);.    if( 
19818 7a 45 78 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20  zExternal ){.   
19819 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64     db->xCollNeed
1981a 65 64 31 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65  ed16(db->pCollNe
1981b 65 64 65 64 41 72 67 2c 20 64 62 2c 20 28 69 6e  ededArg, db, (in
1981c 74 29 45 4e 43 28 64 62 29 2c 20 7a 45 78 74 65  t)ENC(db), zExte
1981d 72 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rnal);.    }.   
1981e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
1981f 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e  e(pTmp);.  }.#en
19820 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dif.}../*.** Thi
19821 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
19822 6c 65 64 20 69 66 20 74 68 65 20 63 6f 6c 6c 61  led if the colla
19823 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 66 61 69  tion factory fai
19824 6c 73 20 74 6f 20 64 65 6c 69 76 65 72 20 61 0a  ls to deliver a.
19825 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e  ** collation fun
19826 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 62 65 73  ction in the bes
19827 74 20 65 6e 63 6f 64 69 6e 67 20 62 75 74 20 74  t encoding but t
19828 68 65 72 65 20 6d 61 79 20 62 65 20 6f 74 68 65  here may be othe
19829 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66  r versions.** of
1982a 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20   this collation 
1982b 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74  function (for ot
1982c 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  her text encodin
1982d 67 73 29 20 61 76 61 69 6c 61 62 6c 65 2e 20 55  gs) available. U
1982e 73 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65  se one.** of the
1982f 73 65 20 69 6e 73 74 65 61 64 20 69 66 20 74 68  se instead if th
19830 65 79 20 65 78 69 73 74 2e 20 41 76 6f 69 64 20  ey exist. Avoid 
19831 61 20 55 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d  a UTF-8 <-> UTF-
19832 31 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66  16 conversion if
19833 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f  .** possible..*/
19834 0a 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 74  .static int synt
19835 68 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33  hCollSeq(sqlite3
19836 20 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70   *db, CollSeq *p
19837 43 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  Coll){.  CollSeq
19838 20 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72   *pColl2;.  char
19839 20 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61   *z = pColl->zNa
1983a 6d 65 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 74  me;.  int n = st
1983b 72 6c 65 6e 28 7a 29 3b 0a 20 20 69 6e 74 20 69  rlen(z);.  int i
1983c 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
1983d 20 75 38 20 61 45 6e 63 5b 5d 20 3d 20 7b 20 53   u8 aEnc[] = { S
1983e 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
1983f 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53  QLITE_UTF16LE, S
19840 51 4c 49 54 45 5f 55 54 46 38 20 7d 3b 0a 20 20  QLITE_UTF8 };.  
19841 66 6f 72 28 69 3d 30 3b 20 69 3c 33 3b 20 69 2b  for(i=0; i<3; i+
19842 2b 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 32 20 3d  +){.    pColl2 =
19843 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
19844 53 65 71 28 64 62 2c 20 61 45 6e 63 5b 69 5d 2c  Seq(db, aEnc[i],
19845 20 7a 2c 20 6e 2c 20 30 29 3b 0a 20 20 20 20 69   z, n, 0);.    i
19846 66 28 20 70 43 6f 6c 6c 32 2d 3e 78 43 6d 70 21  f( pColl2->xCmp!
19847 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  =0 ){.      memc
19848 70 79 28 70 43 6f 6c 6c 2c 20 70 43 6f 6c 6c 32  py(pColl, pColl2
19849 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  , sizeof(CollSeq
1984a 29 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 2d  ));.      pColl-
1984b 3e 78 44 65 6c 20 3d 20 30 3b 20 20 20 20 20 20  >xDel = 0;      
1984c 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 70     /* Do not cop
1984d 79 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  y the destructor
1984e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
1984f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
19850 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  }.  }.  return S
19851 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a  QLITE_ERROR;.}..
19852 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
19853 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ion is responsib
19854 6c 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20  le for invoking 
19855 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
19856 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 0a 2a  ctory callback.*
19857 2a 20 6f 72 20 73 75 62 73 74 69 74 75 74 69 6e  * or substitutin
19858 67 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  g a collation se
19859 71 75 65 6e 63 65 20 6f 66 20 61 20 64 69 66 66  quence of a diff
1985a 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67 20 77  erent encoding w
1985b 68 65 6e 20 74 68 65 0a 2a 2a 20 72 65 71 75 65  hen the.** reque
1985c 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  sted collation s
1985d 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61  equence is not a
1985e 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20  vailable in the 
1985f 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 0a  database native.
19860 2a 2a 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20  ** encoding..** 
19861 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74  .** If it is not
19862 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 43 6f 6c   NULL, then pCol
19863 6c 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20  l must point to 
19864 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74  the database nat
19865 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 0a 2a 2a  ive encoding .**
19866 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
19867 6e 63 65 20 77 69 74 68 20 6e 61 6d 65 20 7a 4e  nce with name zN
19868 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d  ame, length nNam
19869 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  e..**.** The ret
1986a 75 72 6e 20 76 61 6c 75 65 20 69 73 20 65 69 74  urn value is eit
1986b 68 65 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  her the collatio
1986c 6e 20 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65  n sequence to be
1986d 20 75 73 65 64 20 69 6e 20 64 61 74 61 62 61 73   used in databas
1986e 65 0a 2a 2a 20 64 62 20 66 6f 72 20 63 6f 6c 6c  e.** db for coll
1986f 61 74 69 6f 6e 20 74 79 70 65 20 6e 61 6d 65 20  ation type name 
19870 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e  zName, length nN
19871 61 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20 69 66  ame, or NULL, if
19872 20 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a   no collation.**
19873 20 73 65 71 75 65 6e 63 65 20 63 61 6e 20 62 65   sequence can be
19874 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54   found..*/.SQLIT
19875 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
19876 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c  q *sqlite3GetCol
19877 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65 33 2a  lSeq(.  sqlite3*
19878 20 64 62 2c 20 0a 20 20 43 6f 6c 6c 53 65 71 20   db, .  CollSeq 
19879 2a 70 43 6f 6c 6c 2c 20 0a 20 20 63 6f 6e 73 74  *pColl, .  const
1987a 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
1987b 20 69 6e 74 20 6e 4e 61 6d 65 0a 29 7b 0a 20 20   int nName.){.  
1987c 43 6f 6c 6c 53 65 71 20 2a 70 3b 0a 0a 20 20 70  CollSeq *p;..  p
1987d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20   = pColl;.  if( 
1987e 21 70 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71  !p ){.    p = sq
1987f 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
19880 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 4e  (db, ENC(db), zN
19881 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a  ame, nName, 0);.
19882 20 20 7d 0a 20 20 69 66 28 20 21 70 20 7c 7c 20    }.  if( !p || 
19883 21 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20  !p->xCmp ){.    
19884 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20  /* No collation 
19885 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 69 73  sequence of this
19886 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 65   type for this e
19887 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 67 69 73  ncoding is regis
19888 74 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20 43 61  tered..    ** Ca
19889 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ll the collation
1988a 20 66 61 63 74 6f 72 79 20 74 6f 20 73 65 65 20   factory to see 
1988b 69 66 20 69 74 20 63 61 6e 20 73 75 70 70 6c 79  if it can supply
1988c 20 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a 20 20   us with one..  
1988d 20 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 6f 6c    */.    callCol
1988e 6c 4e 65 65 64 65 64 28 64 62 2c 20 7a 4e 61 6d  lNeeded(db, zNam
1988f 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 70  e, nName);.    p
19890 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
19891 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62  llSeq(db, ENC(db
19892 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  ), zName, nName,
19893 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70   0);.  }.  if( p
19894 20 26 26 20 21 70 2d 3e 78 43 6d 70 20 26 26 20   && !p->xCmp && 
19895 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64 62 2c  synthCollSeq(db,
19896 20 70 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 30   p) ){.    p = 0
19897 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
19898 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20 29 3b  !p || p->xCmp );
19899 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
1989a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1989b 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  ne is called on 
1989c 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
1989d 65 6e 63 65 20 62 65 66 6f 72 65 20 69 74 20 69  ence before it i
1989e 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 68 65  s used to.** che
1989f 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 64 65  ck that it is de
198a0 66 69 6e 65 64 2e 20 41 6e 20 75 6e 64 65 66 69  fined. An undefi
198a1 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ned collation se
198a2 71 75 65 6e 63 65 20 65 78 69 73 74 73 20 77 68  quence exists wh
198a3 65 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65  en.** a database
198a4 20 69 73 20 6c 6f 61 64 65 64 20 74 68 61 74 20   is loaded that 
198a5 63 6f 6e 74 61 69 6e 73 20 72 65 66 65 72 65 6e  contains referen
198a6 63 65 73 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e  ces to collation
198a7 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 74 68   sequences.** th
198a8 61 74 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e  at have not been
198a9 20 64 65 66 69 6e 65 64 20 62 79 20 73 71 6c 69   defined by sqli
198aa 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
198ab 74 69 6f 6e 28 29 20 65 74 63 2e 0a 2a 2a 0a 2a  tion() etc..**.*
198ac 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 74  * If required, t
198ad 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c  his routine call
198ae 73 20 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e  s the 'collation
198af 20 6e 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63   needed' callbac
198b0 6b 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20  k to.** request 
198b1 61 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  a definition of 
198b2 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
198b3 71 75 65 6e 63 65 2e 20 49 66 20 74 68 69 73 20  quence. If this 
198b4 64 6f 65 73 6e 27 74 20 77 6f 72 6b 2c 20 0a 2a  doesn't work, .*
198b5 2a 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20  * an equivalent 
198b6 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
198b7 63 65 20 74 68 61 74 20 75 73 65 73 20 61 20 74  ce that uses a t
198b8 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 64 69 66  ext encoding dif
198b9 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74  ferent.** from t
198ba 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
198bb 20 69 73 20 73 75 62 73 74 69 74 75 74 65 64 2c   is substituted,
198bc 20 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c   if one is avail
198bd 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
198be 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
198bf 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28  te3CheckCollSeq(
198c0 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 43  Parse *pParse, C
198c1 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a  ollSeq *pColl){.
198c2 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
198c3 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
198c4 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e  Name = pColl->zN
198c5 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71  ame;.    CollSeq
198c6 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *p = sqlite3Get
198c7 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2d 3e  CollSeq(pParse->
198c8 64 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65  db, pColl, zName
198c9 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 21  , -1);.    if( !
198ca 70 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  p ){.      if( p
198cb 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29  Parse->nErr==0 )
198cc 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
198cd 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
198ce 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61  , "no such colla
198cf 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25  tion sequence: %
198d0 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
198d1 20 20 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65    }.      pParse
198d2 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20  ->nErr++;.      
198d3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
198d4 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  ROR;.    }.    a
198d5 73 73 65 72 74 28 20 70 3d 3d 70 43 6f 6c 6c 20  ssert( p==pColl 
198d6 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
198d7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a  SQLITE_OK;.}....
198d8 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 6e 64  /*.** Locate and
198d9 20 72 65 74 75 72 6e 20 61 6e 20 65 6e 74 72 79   return an entry
198da 20 66 72 6f 6d 20 74 68 65 20 64 62 2e 61 43 6f   from the db.aCo
198db 6c 6c 53 65 71 20 68 61 73 68 20 74 61 62 6c 65  llSeq hash table
198dc 2e 20 49 66 20 74 68 65 20 65 6e 74 72 79 0a 2a  . If the entry.*
198dd 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7a  * specified by z
198de 4e 61 6d 65 20 61 6e 64 20 6e 4e 61 6d 65 20 69  Name and nName i
198df 73 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20  s not found and 
198e0 70 61 72 61 6d 65 74 65 72 20 27 63 72 65 61 74  parameter 'creat
198e1 65 27 20 69 73 0a 2a 2a 20 74 72 75 65 2c 20 74  e' is.** true, t
198e2 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77  hen create a new
198e3 20 65 6e 74 72 79 2e 20 4f 74 68 65 72 77 69 73   entry. Otherwis
198e4 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  e return NULL..*
198e5 2a 0a 2a 2a 20 45 61 63 68 20 70 6f 69 6e 74 65  *.** Each pointe
198e6 72 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  r stored in the 
198e7 73 71 6c 69 74 65 33 2e 61 43 6f 6c 6c 53 65 71  sqlite3.aCollSeq
198e8 20 68 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74   hash table cont
198e9 61 69 6e 73 20 61 6e 0a 2a 2a 20 61 72 72 61 79  ains an.** array
198ea 20 6f 66 20 74 68 72 65 65 20 43 6f 6c 6c 53 65   of three CollSe
198eb 71 20 73 74 72 75 63 74 75 72 65 73 2e 20 54 68  q structures. Th
198ec 65 20 66 69 72 73 74 20 69 73 20 74 68 65 20 63  e first is the c
198ed 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
198ee 65 0a 2a 2a 20 70 72 65 66 66 65 72 72 65 64 20  e.** prefferred 
198ef 66 6f 72 20 55 54 46 2d 38 2c 20 74 68 65 20 73  for UTF-8, the s
198f0 65 63 6f 6e 64 20 55 54 46 2d 31 36 6c 65 2c 20  econd UTF-16le, 
198f1 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 55 54  and the third UT
198f2 46 2d 31 36 62 65 2e 0a 2a 2a 0a 2a 2a 20 53 74  F-16be..**.** St
198f3 6f 72 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  ored immediately
198f4 20 61 66 74 65 72 20 74 68 65 20 74 68 72 65 65   after the three
198f5 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
198f6 6e 63 65 73 20 69 73 20 61 20 63 6f 70 79 20 6f  nces is a copy o
198f7 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69  f.** the collati
198f8 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
198f9 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  . A pointer to t
198fa 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 73 74  his string is st
198fb 6f 72 65 64 20 69 6e 0a 2a 2a 20 65 61 63 68 20  ored in.** each 
198fc 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
198fd 63 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ce structure..*/
198fe 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20  .static CollSeq 
198ff 2a 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72  *findCollSeqEntr
19900 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  y(.  sqlite3 *db
19901 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
19902 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 4e 61  zName,.  int nNa
19903 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65  me,.  int create
19904 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  .){.  CollSeq *p
19905 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 6e 4e 61 6d  Coll;.  if( nNam
19906 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73 74  e<0 ) nName = st
19907 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 70  rlen(zName);.  p
19908 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48 61  Coll = sqlite3Ha
19909 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c  shFind(&db->aCol
1990a 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  lSeq, zName, nNa
1990b 6d 65 29 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70  me);..  if( 0==p
1990c 43 6f 6c 6c 20 26 26 20 63 72 65 61 74 65 20 29  Coll && create )
1990d 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71  {.    pColl = sq
1990e 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
1990f 6f 28 64 62 2c 20 33 2a 73 69 7a 65 6f 66 28 2a  o(db, 3*sizeof(*
19910 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20 2b  pColl) + nName +
19911 20 31 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43   1 );.    if( pC
19912 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c  oll ){.      Col
19913 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 30 3b 0a  lSeq *pDel = 0;.
19914 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a        pColl[0].z
19915 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70  Name = (char*)&p
19916 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70  Coll[3];.      p
19917 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53 51  Coll[0].enc = SQ
19918 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20  LITE_UTF8;.     
19919 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65 20   pColl[1].zName 
1991a 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b  = (char*)&pColl[
1991b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b  3];.      pColl[
1991c 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  1].enc = SQLITE_
1991d 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 20 20 70  UTF16LE;.      p
1991e 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20  Coll[2].zName = 
1991f 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d  (char*)&pColl[3]
19920 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d  ;.      pColl[2]
19921 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  .enc = SQLITE_UT
19922 46 31 36 42 45 3b 0a 20 20 20 20 20 20 6d 65 6d  F16BE;.      mem
19923 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61  cpy(pColl[0].zNa
19924 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  me, zName, nName
19925 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30  );.      pColl[0
19926 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d  ].zName[nName] =
19927 20 30 3b 0a 20 20 20 20 20 20 70 44 65 6c 20 3d   0;.      pDel =
19928 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
19929 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71  rt(&db->aCollSeq
1992a 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65  , pColl[0].zName
1992b 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29 3b  , nName, pColl);
1992c 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20  ..      /* If a 
1992d 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
1992e 20 6f 63 63 75 72 65 64 20 69 6e 20 73 71 6c 69   occured in sqli
1992f 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 29 2c  te3HashInsert(),
19930 20 69 74 20 77 69 6c 6c 20 0a 20 20 20 20 20 20   it will .      
19931 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 70 43  ** return the pC
19932 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20 62  oll pointer to b
19933 65 20 64 65 6c 65 74 65 64 20 28 62 65 63 61 75  e deleted (becau
19934 73 65 20 69 74 20 77 61 73 6e 27 74 20 61 64 64  se it wasn't add
19935 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  ed.      ** to t
19936 68 65 20 68 61 73 68 20 74 61 62 6c 65 29 2e 0a  he hash table)..
19937 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
19938 73 73 65 72 74 28 20 70 44 65 6c 3d 3d 30 20 7c  ssert( pDel==0 |
19939 7c 20 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20 29 3b  | pDel==pColl );
1993a 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 6c 21  .      if( pDel!
1993b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62  =0 ){.        db
1993c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1993d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   1;.        sqli
1993e 74 65 33 5f 66 72 65 65 28 70 44 65 6c 29 3b 0a  te3_free(pDel);.
1993f 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
19940 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
19941 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43  .  }.  return pC
19942 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61  oll;.}../*.** Pa
19943 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f  rameter zName po
19944 69 6e 74 73 20 74 6f 20 61 20 55 54 46 2d 38 20  ints to a UTF-8 
19945 65 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 20 6e  encoded string n
19946 4e 61 6d 65 20 62 79 74 65 73 20 6c 6f 6e 67 2e  Name bytes long.
19947 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 43  .** Return the C
19948 6f 6c 6c 53 65 71 2a 20 70 6f 69 6e 74 65 72 20  ollSeq* pointer 
19949 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  for the collatio
1994a 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64  n sequence named
1994b 20 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72 20 74 68   zName.** for th
1994c 65 20 65 6e 63 6f 64 69 6e 67 20 27 65 6e 63 27  e encoding 'enc'
1994d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1994e 73 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a 2a 20 49  se 'db'..**.** I
1994f 66 20 74 68 65 20 65 6e 74 72 79 20 73 70 65 63  f the entry spec
19950 69 66 69 65 64 20 69 73 20 6e 6f 74 20 66 6f 75  ified is not fou
19951 6e 64 20 61 6e 64 20 27 63 72 65 61 74 65 27 20  nd and 'create' 
19952 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72  is true, then cr
19953 65 61 74 65 20 61 0a 2a 2a 20 6e 65 77 20 65 6e  eate a.** new en
19954 74 72 79 2e 20 20 4f 74 68 65 72 77 69 73 65 20  try.  Otherwise 
19955 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a  return NULL..**.
19956 2a 2a 20 41 20 73 65 70 61 72 61 74 65 20 66 75  ** A separate fu
19957 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 4c 6f  nction sqlite3Lo
19958 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 73  cateCollSeq() is
19959 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e   a wrapper aroun
1995a 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  d.** this routin
1995b 65 2e 20 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  e.  sqlite3Locat
1995c 65 43 6f 6c 6c 53 65 71 28 29 20 69 6e 76 6f 6b  eCollSeq() invok
1995d 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  es the collation
1995e 20 66 61 63 74 6f 72 79 0a 2a 2a 20 69 66 20 6e   factory.** if n
1995f 65 63 65 73 73 61 72 79 20 61 6e 64 20 67 65 6e  ecessary and gen
19960 65 72 61 74 65 73 20 61 6e 20 65 72 72 6f 72 20  erates an error 
19961 6d 65 73 73 61 67 65 20 69 66 20 74 68 65 20 63  message if the c
19962 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
19963 65 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66  e.** cannot be f
19964 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ound..*/.SQLITE_
19965 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
19966 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c  *sqlite3FindColl
19967 53 65 71 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  Seq(.  sqlite3 *
19968 64 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20  db,.  u8 enc,.  
19969 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1996a 65 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 0a  e,.  int nName,.
1996b 20 20 69 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a    int create.){.
1996c 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
1996d 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b  ;.  if( zName ){
1996e 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 66 69 6e  .    pColl = fin
1996f 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28 64 62  dCollSeqEntry(db
19970 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  , zName, nName, 
19971 63 72 65 61 74 65 29 3b 0a 20 20 7d 65 6c 73 65  create);.  }else
19972 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62  {.    pColl = db
19973 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d  ->pDfltColl;.  }
19974 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54  .  assert( SQLIT
19975 45 5f 55 54 46 38 3d 3d 31 20 26 26 20 53 51 4c  E_UTF8==1 && SQL
19976 49 54 45 5f 55 54 46 31 36 4c 45 3d 3d 32 20 26  ITE_UTF16LE==2 &
19977 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  & SQLITE_UTF16BE
19978 3d 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==3 );.  assert(
19979 20 65 6e 63 3e 3d 53 51 4c 49 54 45 5f 55 54 46   enc>=SQLITE_UTF
1997a 38 20 26 26 20 65 6e 63 3c 3d 53 51 4c 49 54 45  8 && enc<=SQLITE
1997b 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66  _UTF16BE );.  if
1997c 28 20 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20  ( pColl ) pColl 
1997d 2b 3d 20 65 6e 63 2d 31 3b 0a 20 20 72 65 74 75  += enc-1;.  retu
1997e 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  rn pColl;.}../*.
1997f 2a 2a 20 4c 6f 63 61 74 65 20 61 20 75 73 65 72  ** Locate a user
19980 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20   function given 
19981 61 20 6e 61 6d 65 2c 20 61 20 6e 75 6d 62 65 72  a name, a number
19982 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e   of arguments an
19983 64 20 61 20 66 6c 61 67 0a 2a 2a 20 69 6e 64 69  d a flag.** indi
19984 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 74  cating whether t
19985 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66  he function pref
19986 65 72 73 20 55 54 46 2d 31 36 20 6f 76 65 72 20  ers UTF-16 over 
19987 55 54 46 2d 38 2e 20 20 52 65 74 75 72 6e 20 61  UTF-8.  Return a
19988 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  .** pointer to t
19989 68 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  he FuncDef struc
1998a 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e 65  ture that define
1998b 73 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 2c  s that function,
1998c 20 6f 72 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55   or return.** NU
1998d 4c 4c 20 69 66 20 74 68 65 20 66 75 6e 63 74 69  LL if the functi
1998e 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  on does not exis
1998f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
19990 63 72 65 61 74 65 46 6c 61 67 20 61 72 67 75 6d  createFlag argum
19991 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 65  ent is true, the
19992 6e 20 61 20 6e 65 77 20 28 62 6c 61 6e 6b 29 20  n a new (blank) 
19993 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63  FuncDef.** struc
19994 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20  ture is created 
19995 61 6e 64 20 6c 69 6b 65 64 20 69 6e 74 6f 20 74  and liked into t
19996 68 65 20 22 64 62 22 20 73 74 72 75 63 74 75 72  he "db" structur
19997 65 20 69 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61 74  e if a.** no mat
19998 63 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70  ching function p
19999 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74 65  reviously existe
1999a 64 2e 20 20 57 68 65 6e 20 63 72 65 61 74 65 46  d.  When createF
1999b 6c 61 67 20 69 73 20 74 72 75 65 0a 2a 2a 20 61  lag is true.** a
1999c 6e 64 20 74 68 65 20 6e 41 72 67 20 70 61 72 61  nd the nArg para
1999d 6d 65 74 65 72 20 69 73 20 2d 31 2c 20 74 68 65  meter is -1, the
1999e 6e 20 6f 6e 6c 79 20 61 20 66 75 6e 63 74 69 6f  n only a functio
1999f 6e 20 74 68 61 74 20 61 63 63 65 70 74 73 0a 2a  n that accepts.*
199a0 2a 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20  * any number of 
199a1 61 72 67 75 6d 65 6e 74 73 20 77 69 6c 6c 20 62  arguments will b
199a2 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  e returned..**.*
199a3 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20  * If createFlag 
199a4 69 73 20 66 61 6c 73 65 20 61 6e 64 20 6e 41 72  is false and nAr
199a5 67 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 74 68  g is -1, then th
199a6 65 20 66 69 72 73 74 20 76 61 6c 69 64 0a 2a 2a  e first valid.**
199a7 20 66 75 6e 63 74 69 6f 6e 20 66 6f 75 6e 64 20   function found 
199a8 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 41 20  is returned.  A 
199a9 66 75 6e 63 74 69 6f 6e 20 69 73 20 76 61 6c 69  function is vali
199aa 64 20 69 66 20 65 69 74 68 65 72 20 78 46 75 6e  d if either xFun
199ab 63 0a 2a 2a 20 6f 72 20 78 53 74 65 70 20 69 73  c.** or xStep is
199ac 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a   non-zero..**.**
199ad 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69   If createFlag i
199ae 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 20  s false, then a 
199af 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
199b0 65 20 72 65 71 75 69 72 65 64 20 6e 61 6d 65 20  e required name 
199b1 61 6e 64 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  and.** number of
199b2 20 61 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62   arguments may b
199b3 65 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e 20  e returned even 
199b4 69 66 20 74 68 65 20 65 54 65 78 74 52 65 70 20  if the eTextRep 
199b5 66 6c 61 67 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  flag does not.**
199b6 20 6d 61 74 63 68 20 74 68 61 74 20 72 65 71 75   match that requ
199b7 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ested..*/.SQLITE
199b8 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66  _PRIVATE FuncDef
199b9 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e   *sqlite3FindFun
199ba 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  ction(.  sqlite3
199bb 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 41   *db,       /* A
199bc 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20  n open database 
199bd 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
199be 2a 7a 4e 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20  *zName, /* Name 
199bf 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  of the function.
199c0 20 20 4e 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d 69    Not null-termi
199c1 6e 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  nated */.  int n
199c2 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  Name,         /*
199c3 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
199c4 63 74 65 72 73 20 69 6e 20 74 68 65 20 6e 61 6d  cters in the nam
199c5 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c  e */.  int nArg,
199c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
199c7 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
199c8 2e 20 20 2d 31 20 6d 65 61 6e 73 20 61 6e 79 20  .  -1 means any 
199c9 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 65  number */.  u8 e
199ca 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  nc,            /
199cb 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 78 74  * Preferred text
199cc 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69   encoding */.  i
199cd 6e 74 20 63 72 65 61 74 65 46 6c 61 67 20 20 20  nt createFlag   
199ce 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20    /* Create new 
199cf 65 6e 74 72 79 20 69 66 20 74 72 75 65 20 61 6e  entry if true an
199d0 64 20 64 6f 65 73 20 6e 6f 74 20 6f 74 68 65 72  d does not other
199d1 77 69 73 65 20 65 78 69 73 74 20 2a 2f 0a 29 7b  wise exist */.){
199d2 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 20 20  .  FuncDef *p;  
199d3 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
199d4 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
199d5 20 46 75 6e 63 44 65 66 20 2a 70 46 69 72 73 74   FuncDef *pFirst
199d6 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 75  ;    /* First fu
199d7 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 69 73  nction with this
199d8 20 6e 61 6d 65 20 2a 2f 0a 20 20 46 75 6e 63 44   name */.  FuncD
199d9 65 66 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 2f  ef *pBest = 0; /
199da 2a 20 42 65 73 74 20 6d 61 74 63 68 20 66 6f 75  * Best match fou
199db 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69  nd so far */.  i
199dc 6e 74 20 62 65 73 74 6d 61 74 63 68 20 3d 20 30  nt bestmatch = 0
199dd 3b 20 20 0a 0a 0a 20 20 61 73 73 65 72 74 28 20  ;  ...  assert( 
199de 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc==SQLITE_UTF8
199df 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f   || enc==SQLITE_
199e0 55 54 46 31 36 4c 45 20 7c 7c 20 65 6e 63 3d 3d  UTF16LE || enc==
199e1 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29  SQLITE_UTF16BE )
199e2 3b 0a 20 20 69 66 28 20 6e 41 72 67 3c 2d 31 20  ;.  if( nArg<-1 
199e3 29 20 6e 41 72 67 20 3d 20 2d 31 3b 0a 0a 20 20  ) nArg = -1;..  
199e4 70 46 69 72 73 74 20 3d 20 28 46 75 6e 63 44 65  pFirst = (FuncDe
199e5 66 2a 29 73 71 6c 69 74 65 33 48 61 73 68 46 69  f*)sqlite3HashFi
199e6 6e 64 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 7a  nd(&db->aFunc, z
199e7 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
199e8 66 6f 72 28 70 3d 70 46 69 72 73 74 3b 20 70 3b  for(p=pFirst; p;
199e9 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
199ea 20 20 2f 2a 20 44 75 72 69 6e 67 20 74 68 65 20    /* During the 
199eb 73 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 62  search for the b
199ec 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  est function def
199ed 69 6e 69 74 69 6f 6e 2c 20 62 65 73 74 6d 61 74  inition, bestmat
199ee 63 68 20 69 73 20 73 65 74 0a 20 20 20 20 2a 2a  ch is set.    **
199ef 20 61 73 20 66 6f 6c 6c 6f 77 73 20 74 6f 20 69   as follows to i
199f0 6e 64 69 63 61 74 65 20 74 68 65 20 71 75 61 6c  ndicate the qual
199f1 69 74 79 20 6f 66 20 74 68 65 20 6d 61 74 63 68  ity of the match
199f2 20 77 69 74 68 20 74 68 65 20 64 65 66 69 6e 69   with the defini
199f3 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 70 6f 69 6e  tion.    ** poin
199f4 74 65 64 20 74 6f 20 62 79 20 70 42 65 73 74 3a  ted to by pBest:
199f5 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 30  .    **.    ** 0
199f6 3a 20 70 42 65 73 74 20 69 73 20 4e 55 4c 4c 2e  : pBest is NULL.
199f7 20 4e 6f 20 6d 61 74 63 68 20 68 61 73 20 62 65   No match has be
199f8 65 6e 20 66 6f 75 6e 64 2e 0a 20 20 20 20 2a 2a  en found..    **
199f9 20 31 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61   1: A variable a
199fa 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f  rguments functio
199fb 6e 20 74 68 61 74 20 70 72 65 66 65 72 73 20 55  n that prefers U
199fc 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d  TF-8 when a UTF-
199fd 31 36 0a 20 20 20 20 2a 2a 20 20 20 20 65 6e 63  16.    **    enc
199fe 6f 64 69 6e 67 20 69 73 20 72 65 71 75 65 73 74  oding is request
199ff 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73  ed, or vice vers
19a00 61 2e 0a 20 20 20 20 2a 2a 20 32 3a 20 41 20 76  a..    ** 2: A v
19a01 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e 74  ariable argument
19a02 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  s function that 
19a03 75 73 65 73 20 55 54 46 2d 31 36 42 45 20 77 68  uses UTF-16BE wh
19a04 65 6e 20 55 54 46 2d 31 36 4c 45 20 69 73 0a 20  en UTF-16LE is. 
19a05 20 20 20 2a 2a 20 20 20 20 72 65 71 75 65 73 74     **    request
19a06 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73  ed, or vice vers
19a07 61 2e 0a 20 20 20 20 2a 2a 20 33 3a 20 41 20 76  a..    ** 3: A v
19a08 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e 74  ariable argument
19a09 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 69 6e 67  s function using
19a0a 20 74 68 65 20 73 61 6d 65 20 74 65 78 74 20 65   the same text e
19a0b 6e 63 6f 64 69 6e 67 2e 0a 20 20 20 20 2a 2a 20  ncoding..    ** 
19a0c 34 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 69  4: A function wi
19a0d 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75 6d  th the exact num
19a0e 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
19a0f 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74 0a   requested that.
19a10 20 20 20 20 2a 2a 20 20 20 20 70 72 65 66 65 72      **    prefer
19a11 73 20 55 54 46 2d 38 20 77 68 65 6e 20 61 20 55  s UTF-8 when a U
19a12 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 69  TF-16 encoding i
19a13 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20  s requested, or 
19a14 76 69 63 65 20 76 65 72 73 61 2e 0a 20 20 20 20  vice versa..    
19a15 2a 2a 20 35 3a 20 41 20 66 75 6e 63 74 69 6f 6e  ** 5: A function
19a16 20 77 69 74 68 20 74 68 65 20 65 78 61 63 74 20   with the exact 
19a17 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
19a18 6e 74 73 20 72 65 71 75 65 73 74 65 64 20 74 68  nts requested th
19a19 61 74 0a 20 20 20 20 2a 2a 20 20 20 20 70 72 65  at.    **    pre
19a1a 66 65 72 73 20 55 54 46 2d 31 36 4c 45 20 77 68  fers UTF-16LE wh
19a1b 65 6e 20 55 54 46 2d 31 36 42 45 20 69 73 20 72  en UTF-16BE is r
19a1c 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63  equested, or vic
19a1d 65 20 76 65 72 73 61 2e 0a 20 20 20 20 2a 2a 20  e versa..    ** 
19a1e 36 3a 20 41 6e 20 65 78 61 63 74 20 6d 61 74 63  6: An exact matc
19a1f 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  h..    **.    **
19a20 20 41 20 6c 61 72 67 65 72 20 76 61 6c 75 65 20   A larger value 
19a21 6f 66 20 27 6d 61 74 63 68 71 75 61 6c 27 20 69  of 'matchqual' i
19a22 6e 64 69 63 61 74 65 73 20 61 20 6d 6f 72 65 20  ndicates a more 
19a23 64 65 73 69 72 61 62 6c 65 20 6d 61 74 63 68 2e  desirable match.
19a24 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
19a25 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 70  p->nArg==-1 || p
19a26 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20  ->nArg==nArg || 
19a27 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20  nArg==-1 ){.    
19a28 20 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 31 3b    int match = 1;
19a29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61            /* Qua
19a2a 6c 69 74 79 20 6f 66 20 74 68 69 73 20 6d 61 74  lity of this mat
19a2b 63 68 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ch */.      if( 
19a2c 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c  p->nArg==nArg ||
19a2d 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20   nArg==-1 ){.   
19a2e 20 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b 0a       match = 4;.
19a2f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
19a30 28 20 65 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45  ( enc==p->iPrefE
19a31 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61  nc ){.        ma
19a32 74 63 68 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20  tch += 2;.      
19a33 7d 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28  }.      else if(
19a34 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54   (enc==SQLITE_UT
19a35 46 31 36 4c 45 20 26 26 20 70 2d 3e 69 50 72 65  F16LE && p->iPre
19a36 66 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  fEnc==SQLITE_UTF
19a37 31 36 42 45 29 20 7c 7c 0a 20 20 20 20 20 20 20  16BE) ||.       
19a38 20 20 20 20 20 20 20 20 28 65 6e 63 3d 3d 53 51          (enc==SQ
19a39 4c 49 54 45 5f 55 54 46 31 36 42 45 20 26 26 20  LITE_UTF16BE && 
19a3a 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c  p->iPrefEnc==SQL
19a3b 49 54 45 5f 55 54 46 31 36 4c 45 29 20 29 7b 0a  ITE_UTF16LE) ){.
19a3c 20 20 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d          match +=
19a3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   1;.      }..   
19a3e 20 20 20 69 66 28 20 6d 61 74 63 68 3e 62 65 73     if( match>bes
19a3f 74 6d 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20  tmatch ){.      
19a40 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20 20 20    pBest = p;.   
19a41 20 20 20 20 20 62 65 73 74 6d 61 74 63 68 20 3d       bestmatch =
19a42 20 6d 61 74 63 68 3b 0a 20 20 20 20 20 20 7d 0a   match;.      }.
19a43 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
19a44 49 66 20 74 68 65 20 63 72 65 61 74 65 46 6c 61  If the createFla
19a45 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  g parameter is t
19a46 72 75 65 2c 20 61 6e 64 20 74 68 65 20 73 65 61  rue, and the sea
19a47 63 68 20 64 69 64 20 6e 6f 74 20 72 65 76 65 61  ch did not revea
19a48 6c 20 61 6e 0a 20 20 2a 2a 20 65 78 61 63 74 20  l an.  ** exact 
19a49 6d 61 74 63 68 20 66 6f 72 20 74 68 65 20 6e 61  match for the na
19a4a 6d 65 2c 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  me, number of ar
19a4b 67 75 6d 65 6e 74 73 20 61 6e 64 20 65 6e 63 6f  guments and enco
19a4c 64 69 6e 67 2c 20 74 68 65 6e 20 61 64 64 20 61  ding, then add a
19a4d 0a 20 20 2a 2a 20 6e 65 77 20 65 6e 74 72 79 20  .  ** new entry 
19a4e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
19a4f 65 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 2e  e and return it.
19a50 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 72 65 61  .  */.  if( crea
19a51 74 65 46 6c 61 67 20 26 26 20 62 65 73 74 6d 61  teFlag && bestma
19a52 74 63 68 3c 36 20 26 26 20 0a 20 20 20 20 20 20  tch<6 && .      
19a53 28 70 42 65 73 74 20 3d 20 73 71 6c 69 74 65 33  (pBest = sqlite3
19a54 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
19a55 20 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 2b   sizeof(*pBest)+
19a56 6e 4e 61 6d 65 29 29 21 3d 30 20 29 7b 0a 20 20  nName))!=0 ){.  
19a57 20 20 70 42 65 73 74 2d 3e 6e 41 72 67 20 3d 20    pBest->nArg = 
19a58 6e 41 72 67 3b 0a 20 20 20 20 70 42 65 73 74 2d  nArg;.    pBest-
19a59 3e 70 4e 65 78 74 20 3d 20 70 46 69 72 73 74 3b  >pNext = pFirst;
19a5a 0a 20 20 20 20 70 42 65 73 74 2d 3e 69 50 72 65  .    pBest->iPre
19a5b 66 45 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 20 20  fEnc = enc;.    
19a5c 6d 65 6d 63 70 79 28 70 42 65 73 74 2d 3e 7a 4e  memcpy(pBest->zN
19a5d 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
19a5e 65 29 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a  e);.    pBest->z
19a5f 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b  Name[nName] = 0;
19a60 0a 20 20 20 20 69 66 28 20 70 42 65 73 74 3d 3d  .    if( pBest==
19a61 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
19a62 74 28 26 64 62 2d 3e 61 46 75 6e 63 2c 70 42 65  t(&db->aFunc,pBe
19a63 73 74 2d 3e 7a 4e 61 6d 65 2c 6e 4e 61 6d 65 2c  st->zName,nName,
19a64 28 76 6f 69 64 2a 29 70 42 65 73 74 29 20 29 7b  (void*)pBest) ){
19a65 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
19a66 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
19a67 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
19a68 70 42 65 73 74 29 3b 0a 20 20 20 20 20 20 72 65  pBest);.      re
19a69 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
19a6a 7d 0a 0a 20 20 69 66 28 20 70 42 65 73 74 20 26  }..  if( pBest &
19a6b 26 20 28 70 42 65 73 74 2d 3e 78 53 74 65 70 20  & (pBest->xStep 
19a6c 7c 7c 20 70 42 65 73 74 2d 3e 78 46 75 6e 63 20  || pBest->xFunc 
19a6d 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 29 20 29  || createFlag) )
19a6e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 65  {.    return pBe
19a6f 73 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  st;.  }.  return
19a70 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   0;.}../*.** Fre
19a71 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20  e all resources 
19a72 68 65 6c 64 20 62 79 20 74 68 65 20 73 63 68 65  held by the sche
19a73 6d 61 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  ma structure. Th
19a74 65 20 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e 74  e void* argument
19a75 20 70 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61 20   points.** at a 
19a76 53 63 68 65 6d 61 20 73 74 72 75 63 74 2e 20 54  Schema struct. T
19a77 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  his function doe
19a78 73 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74  s not call sqlit
19a79 65 33 5f 66 72 65 65 28 29 20 6f 6e 20 74 68 65  e3_free() on the
19a7a 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 74 73   .** pointer its
19a7b 65 6c 66 2c 20 69 74 20 6a 75 73 74 20 63 6c 65  elf, it just cle
19a7c 61 6e 73 20 75 70 20 73 75 62 73 69 64 75 61 72  ans up subsiduar
19a7d 79 20 72 65 73 6f 75 72 63 65 73 20 28 69 2e 65  y resources (i.e
19a7e 2e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a  . the contents.*
19a7f 2a 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  * of the schema 
19a80 68 61 73 68 20 74 61 62 6c 65 73 29 2e 0a 2a 2f  hash tables)..*/
19a81 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19a82 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65  void sqlite3Sche
19a83 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  maFree(void *p){
19a84 0a 20 20 48 61 73 68 20 74 65 6d 70 31 3b 0a 20  .  Hash temp1;. 
19a85 20 48 61 73 68 20 74 65 6d 70 32 3b 0a 20 20 48   Hash temp2;.  H
19a86 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a  ashElem *pElem;.
19a87 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
19a88 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 3b  a = (Schema *)p;
19a89 0a 0a 20 20 74 65 6d 70 31 20 3d 20 70 53 63 68  ..  temp1 = pSch
19a8a 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20  ema->tblHash;.  
19a8b 74 65 6d 70 32 20 3d 20 70 53 63 68 65 6d 61 2d  temp2 = pSchema-
19a8c 3e 74 72 69 67 48 61 73 68 3b 0a 20 20 73 71 6c  >trigHash;.  sql
19a8d 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53  ite3HashInit(&pS
19a8e 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 2c  chema->trigHash,
19a8f 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52   SQLITE_HASH_STR
19a90 49 4e 47 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  ING, 0);.  sqlit
19a91 65 33 48 61 73 68 43 6c 65 61 72 28 26 70 53 63  e3HashClear(&pSc
19a92 68 65 6d 61 2d 3e 61 46 4b 65 79 29 3b 0a 20 20  hema->aFKey);.  
19a93 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
19a94 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  (&pSchema->idxHa
19a95 73 68 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d  sh);.  for(pElem
19a96 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
19a97 28 26 74 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b  (&temp2); pElem;
19a98 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73   pElem=sqliteHas
19a99 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20  hNext(pElem)){. 
19a9a 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
19a9b 54 72 69 67 67 65 72 28 28 54 72 69 67 67 65 72  Trigger((Trigger
19a9c 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
19a9d 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20 20  (pElem));.  }.  
19a9e 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
19a9f 28 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c 69  (&temp2);.  sqli
19aa0 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 63  te3HashInit(&pSc
19aa1 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 53  hema->tblHash, S
19aa2 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e  QLITE_HASH_STRIN
19aa3 47 2c 20 30 29 3b 0a 20 20 66 6f 72 28 70 45 6c  G, 0);.  for(pEl
19aa4 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  em=sqliteHashFir
19aa5 73 74 28 26 74 65 6d 70 31 29 3b 20 70 45 6c 65  st(&temp1); pEle
19aa6 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48  m; pElem=sqliteH
19aa7 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b  ashNext(pElem)){
19aa8 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
19aa9 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74   = sqliteHashDat
19aaa 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20 73 71  a(pElem);.    sq
19aab 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
19aac 28 70 54 61 62 29 3b 0a 20 20 7d 0a 20 20 73 71  (pTab);.  }.  sq
19aad 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
19aae 74 65 6d 70 31 29 3b 0a 20 20 70 53 63 68 65 6d  temp1);.  pSchem
19aaf 61 2d 3e 70 53 65 71 54 61 62 20 3d 20 30 3b 0a  a->pSeqTab = 0;.
19ab0 20 20 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73    pSchema->flags
19ab1 20 26 3d 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f   &= ~DB_SchemaLo
19ab2 61 64 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  aded;.}../*.** F
19ab3 69 6e 64 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ind and return t
19ab4 68 65 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69  he schema associ
19ab5 61 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65  ated with a BTre
19ab6 65 2e 20 20 43 72 65 61 74 65 0a 2a 2a 20 61 20  e.  Create.** a 
19ab7 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73  new one if neces
19ab8 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sary..*/.SQLITE_
19ab9 50 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a  PRIVATE Schema *
19aba 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
19abb 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74  (sqlite3 *db, Bt
19abc 72 65 65 20 2a 70 42 74 29 7b 0a 20 20 53 63 68  ree *pBt){.  Sch
19abd 65 6d 61 20 2a 20 70 3b 0a 20 20 69 66 28 20 70  ema * p;.  if( p
19abe 42 74 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 53  Bt ){.    p = (S
19abf 63 68 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 42  chema *)sqlite3B
19ac0 74 72 65 65 53 63 68 65 6d 61 28 70 42 74 2c 20  treeSchema(pBt, 
19ac1 73 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 2c 20  sizeof(Schema), 
19ac2 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65  sqlite3SchemaFre
19ac3 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
19ac4 20 70 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73   p = (Schema *)s
19ac5 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
19ac6 28 73 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 29  (sizeof(Schema))
19ac7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 29  ;.  }.  if( !p )
19ac8 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
19ac9 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65  Failed = 1;.  }e
19aca 6c 73 65 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66  lse if ( 0==p->f
19acb 69 6c 65 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20  ile_format ){.  
19acc 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
19acd 74 28 26 70 2d 3e 74 62 6c 48 61 73 68 2c 20 53  t(&p->tblHash, S
19ace 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e  QLITE_HASH_STRIN
19acf 47 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  G, 0);.    sqlit
19ad0 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 69  e3HashInit(&p->i
19ad1 64 78 48 61 73 68 2c 20 53 51 4c 49 54 45 5f 48  dxHash, SQLITE_H
19ad2 41 53 48 5f 53 54 52 49 4e 47 2c 20 30 29 3b 0a  ASH_STRING, 0);.
19ad3 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
19ad4 6e 69 74 28 26 70 2d 3e 74 72 69 67 48 61 73 68  nit(&p->trigHash
19ad5 2c 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54  , SQLITE_HASH_ST
19ad6 52 49 4e 47 2c 20 30 29 3b 0a 20 20 20 20 73 71  RING, 0);.    sq
19ad7 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70  lite3HashInit(&p
19ad8 2d 3e 61 46 4b 65 79 2c 20 53 51 4c 49 54 45 5f  ->aFKey, SQLITE_
19ad9 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b  HASH_STRING, 1);
19ada 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d 20 53 51  .    p->enc = SQ
19adb 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 7d 0a 20  LITE_UTF8;.  }. 
19adc 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
19add 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
19ade 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e 63 20  d of callback.c 
19adf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ae1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
19ae2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
19ae3 67 69 6e 20 66 69 6c 65 20 64 65 6c 65 74 65 2e  gin file delete.
19ae4 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
19ae5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ae6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
19ae7 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
19ae8 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
19ae9 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
19aea 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
19aeb 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
19aec 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
19aed 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
19aee 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
19aef 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
19af0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
19af1 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
19af2 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
19af3 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
19af4 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
19af5 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
19af6 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
19af7 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
19af8 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
19af9 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
19afa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19afb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19afc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19afd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19afe 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
19aff 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
19b00 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  e routines that 
19b01 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68  are called by th
19b02 65 20 70 61 72 73 65 72 0a 2a 2a 20 69 6e 20 6f  e parser.** in o
19b03 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65  rder to generate
19b04 20 63 6f 64 65 20 66 6f 72 20 44 45 4c 45 54 45   code for DELETE
19b05 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74 73   FROM statements
19b06 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 65 6c  ..**.** $Id: del
19b07 65 74 65 2e 63 2c 76 20 31 2e 31 33 30 20 32 30  ete.c,v 1.130 20
19b08 30 37 2f 30 38 2f 31 36 20 30 34 3a 33 30 3a 34  07/08/16 04:30:4
19b09 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  0 drh Exp $.*/..
19b0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 75 70 20 65 76  /*.** Look up ev
19b0b 65 72 79 20 74 61 62 6c 65 20 74 68 61 74 20 69  ery table that i
19b0c 73 20 6e 61 6d 65 64 20 69 6e 20 70 53 72 63 2e  s named in pSrc.
19b0d 20 20 49 66 20 61 6e 79 20 74 61 62 6c 65 20 69    If any table i
19b0e 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 0a 2a 2a 20  s not found,.** 
19b0f 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  add an error mes
19b10 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 2d 3e  sage to pParse->
19b11 7a 45 72 72 4d 73 67 20 61 6e 64 20 72 65 74 75  zErrMsg and retu
19b12 72 6e 20 4e 55 4c 4c 2e 20 20 49 66 20 61 6c 6c  rn NULL.  If all
19b13 20 74 61 62 6c 65 73 0a 2a 2a 20 61 72 65 20 66   tables.** are f
19b14 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 61 20 70  ound, return a p
19b15 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 61  ointer to the la
19b16 73 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  st table..*/.SQL
19b17 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
19b18 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  e *sqlite3SrcLis
19b19 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 70  tLookup(Parse *p
19b1a 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a  Parse, SrcList *
19b1b 70 53 72 63 29 7b 0a 20 20 54 61 62 6c 65 20 2a  pSrc){.  Table *
19b1c 70 54 61 62 20 3d 20 30 3b 0a 20 20 69 6e 74 20  pTab = 0;.  int 
19b1d 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  i;.  struct SrcL
19b1e 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
19b1f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65  .  for(i=0, pIte
19b20 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3c 70 53 72  m=pSrc->a; i<pSr
19b21 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49  c->nSrc; i++, pI
19b22 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 70 54 61 62  tem++){.    pTab
19b23 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
19b24 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 49  Table(pParse, pI
19b25 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65  tem->zName, pIte
19b26 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  m->zDatabase);. 
19b27 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
19b28 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 54 61  Table(pItem->pTa
19b29 62 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70  b);.    pItem->p
19b2a 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20  Tab = pTab;.    
19b2b 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
19b2c 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a    pTab->nRef++;.
19b2d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
19b2e 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pTab;.}../*.*
19b2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  * Check to make 
19b30 73 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 74  sure the given t
19b31 61 62 6c 65 20 69 73 20 77 72 69 74 61 62 6c 65  able is writable
19b32 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 0a  .  If it is not.
19b33 2a 2a 20 77 72 69 74 61 62 6c 65 2c 20 67 65 6e  ** writable, gen
19b34 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d  erate an error m
19b35 65 73 73 61 67 65 20 61 6e 64 20 72 65 74 75 72  essage and retur
19b36 6e 20 31 2e 20 20 49 66 20 69 74 20 69 73 0a 2a  n 1.  If it is.*
19b37 2a 20 77 72 69 74 61 62 6c 65 20 72 65 74 75 72  * writable retur
19b38 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  n 0;.*/.SQLITE_P
19b39 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19b3a 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 72  e3IsReadOnly(Par
19b3b 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
19b3c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 76 69 65  e *pTab, int vie
19b3d 77 4f 6b 29 7b 0a 20 20 69 66 28 20 28 70 54 61  wOk){.  if( (pTa
19b3e 62 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 28  b->readOnly && (
19b3f 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67  pParse->db->flag
19b40 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65  s & SQLITE_Write
19b41 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20  Schema)==0.     
19b42 20 20 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65     && pParse->ne
19b43 73 74 65 64 3d 3d 30 29 20 0a 23 69 66 6e 64 65  sted==0) .#ifnde
19b44 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
19b45 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20  RTUALTABLE.     
19b46 20 7c 7c 20 28 70 54 61 62 2d 3e 70 4d 6f 64 20   || (pTab->pMod 
19b47 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70  && pTab->pMod->p
19b48 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 3d  Module->xUpdate=
19b49 3d 30 29 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a  =0).#endif.  ){.
19b4a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
19b4b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
19b4c 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
19b4d 20 6d 6f 64 69 66 69 65 64 22 2c 20 70 54 61 62   modified", pTab
19b4e 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ->zName);.    re
19b4f 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 69 66 6e  turn 1;.  }.#ifn
19b50 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19b51 56 49 45 57 0a 20 20 69 66 28 20 21 76 69 65 77  VIEW.  if( !view
19b52 4f 6b 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c  Ok && pTab->pSel
19b53 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ect ){.    sqlit
19b54 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
19b55 65 2c 22 63 61 6e 6e 6f 74 20 6d 6f 64 69 66 79  e,"cannot modify
19b56 20 25 73 20 62 65 63 61 75 73 65 20 69 74 20 69   %s because it i
19b57 73 20 61 20 76 69 65 77 22 2c 70 54 61 62 2d 3e  s a view",pTab->
19b58 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  zName);.    retu
19b59 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  rn 1;.  }.#endif
19b5a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
19b5b 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
19b5c 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70  ode that will op
19b5d 65 6e 20 61 20 74 61 62 6c 65 20 66 6f 72 20 72  en a table for r
19b5e 65 61 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  eading..*/.SQLIT
19b5f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19b60 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
19b61 0a 20 20 50 61 72 73 65 20 2a 70 2c 20 20 20 20  .  Parse *p,    
19b62 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
19b63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44  ode into this VD
19b64 42 45 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  BE */.  int iCur
19b65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  ,       /* The c
19b66 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
19b67 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  the table */.  i
19b68 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 2f  nt iDb,        /
19b69 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69  * The database i
19b6a 6e 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e  ndex in sqlite3.
19b6b 61 44 62 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c 65  aDb[] */.  Table
19b6c 20 2a 70 54 61 62 2c 20 20 20 20 2f 2a 20 54 68   *pTab,    /* Th
19b6d 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6f 70  e table to be op
19b6e 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  ened */.  int op
19b6f 63 6f 64 65 20 20 20 20 20 20 2f 2a 20 4f 50 5f  code      /* OP_
19b70 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f  OpenRead or OP_O
19b71 70 65 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20  penWrite */.){. 
19b72 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 66 28 20   Vdbe *v;.  if( 
19b73 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
19b74 29 20 72 65 74 75 72 6e 3b 0a 20 20 76 20 3d 20  ) return;.  v = 
19b75 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
19b76 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63  );.  assert( opc
19b77 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74  ode==OP_OpenWrit
19b78 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f  e || opcode==OP_
19b79 4f 70 65 6e 52 65 61 64 20 29 3b 0a 20 20 73 71  OpenRead );.  sq
19b7a 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70  lite3TableLock(p
19b7b 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75  , iDb, pTab->tnu
19b7c 6d 2c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f  m, (opcode==OP_O
19b7d 70 65 6e 57 72 69 74 65 29 2c 20 70 54 61 62 2d  penWrite), pTab-
19b7e 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  >zName);.  sqlit
19b7f 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19b80 50 5f 49 6e 74 65 67 65 72 2c 20 69 44 62 2c 20  P_Integer, iDb, 
19b81 30 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e  0);.  VdbeCommen
19b82 74 28 28 76 2c 20 22 23 20 25 73 22 2c 20 70 54  t((v, "# %s", pT
19b83 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 73  ab->zName));.  s
19b84 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
19b85 76 2c 20 6f 70 63 6f 64 65 2c 20 69 43 75 72 2c  v, opcode, iCur,
19b86 20 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20   pTab->tnum);.  
19b87 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19b88 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c  (v, OP_SetNumCol
19b89 75 6d 6e 73 2c 20 69 43 75 72 2c 20 70 54 61 62  umns, iCur, pTab
19b8a 2d 3e 6e 43 6f 6c 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  ->nCol);.}.../*.
19b8b 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
19b8c 20 66 6f 72 20 61 20 44 45 4c 45 54 45 20 46 52   for a DELETE FR
19b8d 4f 4d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  OM statement..**
19b8e 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46  .**     DELETE F
19b8f 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57  ROM table_wxyz W
19b90 48 45 52 45 20 61 3c 35 20 41 4e 44 20 62 20 4e  HERE a<5 AND b N
19b91 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20  OT NULL;.**     
19b92 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f              \___
19b93 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f  _____/       \__
19b94 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a  ______________/.
19b95 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
19b96 20 20 20 20 70 54 61 62 4c 69 73 74 20 20 20 20      pTabList    
19b97 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65            pWhere
19b98 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19b99 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
19b9a 65 6c 65 74 65 46 72 6f 6d 28 0a 20 20 50 61 72  eleteFrom(.  Par
19b9b 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
19b9c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
19b9d 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
19b9e 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
19b9f 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  ,     /* The tab
19ba0 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77 65  le from which we
19ba1 20 73 68 6f 75 6c 64 20 64 65 6c 65 74 65 20 74   should delete t
19ba2 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70 72 20  hings */.  Expr 
19ba3 2a 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20  *pWhere         
19ba4 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
19ba5 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e  lause.  May be n
19ba6 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  ull */.){.  Vdbe
19ba7 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
19ba8 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
19ba9 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
19baa 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  e */.  Table *pT
19bab 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ab;           /*
19bac 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   The table from 
19bad 77 68 69 63 68 20 72 65 63 6f 72 64 73 20 77 69  which records wi
19bae 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f  ll be deleted */
19baf 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
19bb0 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  Db;       /* Nam
19bb1 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f  e of database ho
19bb2 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20  lding pTab */.  
19bb3 69 6e 74 20 65 6e 64 2c 20 61 64 64 72 20 3d 20  int end, addr = 
19bb4 30 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 75 70  0;     /* A coup
19bb5 6c 65 20 61 64 64 72 65 73 73 65 73 20 6f 66 20  le addresses of 
19bb6 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a  generated code *
19bb7 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
19bb8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
19bb9 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
19bba 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66  WhereInfo *pWInf
19bbb 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  o;     /* Inform
19bbc 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
19bbd 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
19bbe 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
19bbf 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
19bc0 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64  looping over ind
19bc1 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ices of the tabl
19bc2 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  e */.  int iCur;
19bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19bc4 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d   VDBE Cursor num
19bc5 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a  ber for pTab */.
19bc6 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
19bc7 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e           /* Main
19bc8 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
19bc9 75 72 65 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e  ure */.  AuthCon
19bca 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20 20  text sContext;  
19bcb 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e  /* Authorization
19bcc 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
19bcd 74 20 6f 6c 64 49 64 78 20 3d 20 2d 31 3b 20 20  t oldIdx = -1;  
19bce 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 66       /* Cursor f
19bcf 6f 72 20 74 68 65 20 4f 4c 44 20 74 61 62 6c 65  or the OLD table
19bd0 20 6f 66 20 41 46 54 45 52 20 74 72 69 67 67 65   of AFTER trigge
19bd1 72 73 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  rs */.  NameCont
19bd2 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 2f  ext sNC;       /
19bd3 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 74  * Name context t
19bd4 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73  o resolve expres
19bd5 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69 6e  sions in */.  in
19bd6 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
19bd7 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
19bd8 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74   number */.  int
19bd9 20 6d 65 6d 43 6e 74 20 3d 20 30 3b 20 20 20 20   memCnt = 0;    
19bda 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
19bdb 6c 6c 20 75 73 65 64 20 66 6f 72 20 63 68 61 6e  ll used for chan
19bdc 67 65 20 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a 0a  ge counting */..
19bdd 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19bde 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e  MIT_TRIGGER.  in
19bdf 74 20 69 73 56 69 65 77 3b 20 20 20 20 20 20 20  t isView;       
19be0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
19be1 75 65 20 69 66 20 61 74 74 65 6d 70 74 69 6e 67  ue if attempting
19be2 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20   to delete from 
19be3 61 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20  a view */.  int 
19be4 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 3d  triggers_exist =
19be5 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65   0;      /* True
19be6 20 69 66 20 61 6e 79 20 74 72 69 67 67 65 72 73   if any triggers
19be7 20 65 78 69 73 74 20 2a 2f 0a 23 65 6e 64 69 66   exist */.#endif
19be8 0a 0a 20 20 73 43 6f 6e 74 65 78 74 2e 70 50 61  ..  sContext.pPa
19be9 72 73 65 20 3d 20 30 3b 0a 20 20 64 62 20 3d 20  rse = 0;.  db = 
19bea 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
19beb 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
19bec 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
19bed 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64  ed ){.    goto d
19bee 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
19bef 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  up;.  }.  assert
19bf0 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  ( pTabList->nSrc
19bf1 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  ==1 );..  /* Loc
19bf2 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 77 68  ate the table wh
19bf3 69 63 68 20 77 65 20 77 61 6e 74 20 74 6f 20 64  ich we want to d
19bf4 65 6c 65 74 65 2e 20 20 54 68 69 73 20 74 61 62  elete.  This tab
19bf5 6c 65 20 68 61 73 20 74 6f 20 62 65 0a 20 20 2a  le has to be.  *
19bf6 2a 20 70 75 74 20 69 6e 20 61 6e 20 53 72 63 4c  * put in an SrcL
19bf7 69 73 74 20 73 74 72 75 63 74 75 72 65 20 62 65  ist structure be
19bf8 63 61 75 73 65 20 73 6f 6d 65 20 6f 66 20 74 68  cause some of th
19bf9 65 20 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65  e subroutines we
19bfa 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61  .  ** will be ca
19bfb 6c 6c 69 6e 67 20 61 72 65 20 64 65 73 69 67 6e  lling are design
19bfc 65 64 20 74 6f 20 77 6f 72 6b 20 77 69 74 68 20  ed to work with 
19bfd 6d 75 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20  multiple tables 
19bfe 61 6e 64 20 65 78 70 65 63 74 0a 20 20 2a 2a 20  and expect.  ** 
19bff 61 6e 20 53 72 63 4c 69 73 74 2a 20 70 61 72 61  an SrcList* para
19c00 6d 65 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66  meter instead of
19c01 20 6a 75 73 74 20 61 20 54 61 62 6c 65 2a 20 70   just a Table* p
19c02 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20  arameter..  */. 
19c03 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53   pTab = sqlite3S
19c04 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61  rcListLookup(pPa
19c05 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a  rse, pTabList);.
19c06 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20    if( pTab==0 ) 
19c07 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
19c08 6d 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a  m_cleanup;..  /*
19c09 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77   Figure out if w
19c0a 65 20 68 61 76 65 20 61 6e 79 20 74 72 69 67 67  e have any trigg
19c0b 65 72 73 20 61 6e 64 20 69 66 20 74 68 65 20 74  ers and if the t
19c0c 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20  able being.  ** 
19c0d 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 69 73 20  deleted from is 
19c0e 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e  a view.  */.#ifn
19c0f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19c10 54 52 49 47 47 45 52 0a 20 20 74 72 69 67 67 65  TRIGGER.  trigge
19c11 72 73 5f 65 78 69 73 74 20 3d 20 73 71 6c 69 74  rs_exist = sqlit
19c12 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28  e3TriggersExist(
19c13 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b  pParse, pTab, TK
19c14 5f 44 45 4c 45 54 45 2c 20 30 29 3b 0a 20 20 69  _DELETE, 0);.  i
19c15 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53  sView = pTab->pS
19c16 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a  elect!=0;.#else.
19c17 23 20 64 65 66 69 6e 65 20 74 72 69 67 67 65 72  # define trigger
19c18 73 5f 65 78 69 73 74 20 30 0a 23 20 64 65 66 69  s_exist 0.# defi
19c19 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
19c1a 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
19c1b 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64  _OMIT_VIEW.# und
19c1c 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69  ef isView.# defi
19c1d 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
19c1e 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  if..  if( sqlite
19c1f 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72  3IsReadOnly(pPar
19c20 73 65 2c 20 70 54 61 62 2c 20 74 72 69 67 67 65  se, pTab, trigge
19c21 72 73 5f 65 78 69 73 74 29 20 29 7b 0a 20 20 20  rs_exist) ){.   
19c22 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
19c23 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  m_cleanup;.  }. 
19c24 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
19c25 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
19c26 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
19c27 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
19c28 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 62 20 3d  ->nDb );.  zDb =
19c29 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
19c2a 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ame;.  if( sqlit
19c2b 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
19c2c 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54  se, SQLITE_DELET
19c2d 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  E, pTab->zName, 
19c2e 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67  0, zDb) ){.    g
19c2f 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
19c30 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
19c31 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20 72 65  /* If pTab is re
19c32 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d 61 6b  ally a view, mak
19c33 65 20 73 75 72 65 20 69 74 20 68 61 73 20 62 65  e sure it has be
19c34 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a  en initialized..
19c35 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
19c36 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
19c37 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
19c38 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64  b) ){.    goto d
19c39 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
19c3a 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c  up;.  }..  /* Al
19c3b 6c 6f 63 61 74 65 20 61 20 63 75 72 73 6f 72 20  locate a cursor 
19c3c 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
19c3d 65 20 6f 6c 64 2e 2a 20 64 61 74 61 20 66 6f 72  e old.* data for
19c3e 20 61 20 74 72 69 67 67 65 72 2e 0a 20 20 2a 2f   a trigger..  */
19c3f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f  .  if( triggers_
19c40 65 78 69 73 74 20 29 7b 20 0a 20 20 20 20 6f 6c  exist ){ .    ol
19c41 64 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e  dIdx = pParse->n
19c42 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Tab++;.  }..  /*
19c43 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c   Resolve the col
19c44 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 74 68 65  umn names in the
19c45 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20   WHERE clause.. 
19c46 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
19c47 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20  abList->nSrc==1 
19c48 29 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61 62  );.  iCur = pTab
19c49 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 73  List->a[0].iCurs
19c4a 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  or = pParse->nTa
19c4b 62 2b 2b 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73  b++;.  memset(&s
19c4c 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e  NC, 0, sizeof(sN
19c4d 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73  C));.  sNC.pPars
19c4e 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 4e  e = pParse;.  sN
19c4f 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61  C.pSrcList = pTa
19c50 62 4c 69 73 74 3b 0a 20 20 69 66 28 20 73 71 6c  bList;.  if( sql
19c51 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e  ite3ExprResolveN
19c52 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 72  ames(&sNC, pWher
19c53 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64  e) ){.    goto d
19c54 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
19c55 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  up;.  }..  /* St
19c56 61 72 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e  art the view con
19c57 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20  text.  */.  if( 
19c58 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71  isView ){.    sq
19c59 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
19c5a 50 75 73 68 28 70 50 61 72 73 65 2c 20 26 73 43  Push(pParse, &sC
19c5b 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e  ontext, pTab->zN
19c5c 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ame);.  }..  /* 
19c5d 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
19c5e 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20   code..  */.  v 
19c5f 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
19c60 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
19c61 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  v==0 ){.    goto
19c62 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
19c63 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
19c64 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
19c65 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43  0 ) sqlite3VdbeC
19c66 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a  ountChanges(v);.
19c67 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
19c68 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
19c69 72 73 65 2c 20 74 72 69 67 67 65 72 73 5f 65 78  rse, triggers_ex
19c6a 69 73 74 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a  ist, iDb);..  /*
19c6b 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69 6e   If we are tryin
19c6c 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d  g to delete from
19c6d 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65   a view, realize
19c6e 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0a   that view into.
19c6f 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61 6c    ** a ephemeral
19c70 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
19c71 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
19c72 20 53 65 6c 65 63 74 20 2a 70 56 69 65 77 20 3d   Select *pView =
19c73 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
19c74 70 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 65 6c  p(db, pTab->pSel
19c75 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ect);.    sqlite
19c76 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
19c77 70 56 69 65 77 2c 20 53 52 54 5f 45 70 68 65 6d  pView, SRT_Ephem
19c78 54 61 62 2c 20 69 43 75 72 2c 20 30 2c 20 30 2c  Tab, iCur, 0, 0,
19c79 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
19c7a 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
19c7b 70 56 69 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 2f  pView);.  }..  /
19c7c 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
19c7d 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20   counter of the 
19c7e 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 64  number of rows d
19c7f 65 6c 65 74 65 64 2c 20 69 66 0a 20 20 2a 2a 20  eleted, if.  ** 
19c80 77 65 20 61 72 65 20 63 6f 75 6e 74 69 6e 67 20  we are counting 
19c81 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rows..  */.  if(
19c82 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c   db->flags & SQL
19c83 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b  ITE_CountRows ){
19c84 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d 20 70 50  .    memCnt = pP
19c85 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20  arse->nMem++;.  
19c86 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19c87 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c  Op(v, OP_MemInt,
19c88 20 30 2c 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 7d   0, memCnt);.  }
19c89 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63  ..  /* Special c
19c8a 61 73 65 3a 20 41 20 44 45 4c 45 54 45 20 77 69  ase: A DELETE wi
19c8b 74 68 6f 75 74 20 61 20 57 48 45 52 45 20 63 6c  thout a WHERE cl
19c8c 61 75 73 65 20 64 65 6c 65 74 65 73 20 65 76 65  ause deletes eve
19c8d 72 79 74 68 69 6e 67 2e 0a 20 20 2a 2a 20 49 74  rything..  ** It
19c8e 20 69 73 20 65 61 73 69 65 72 20 6a 75 73 74 20   is easier just 
19c8f 74 6f 20 65 72 61 73 65 20 74 68 65 20 77 68 6f  to erase the who
19c90 6c 65 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 2c  le table.  Note,
19c91 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 20   however, that. 
19c92 20 2a 2a 20 74 68 69 73 20 6d 65 61 6e 73 20 74   ** this means t
19c93 68 61 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e  hat the row chan
19c94 67 65 20 63 6f 75 6e 74 20 77 69 6c 6c 20 62 65  ge count will be
19c95 20 69 6e 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f   incorrect..  */
19c96 0a 20 20 69 66 28 20 70 57 68 65 72 65 3d 3d 30  .  if( pWhere==0
19c97 20 26 26 20 21 74 72 69 67 67 65 72 73 5f 65 78   && !triggers_ex
19c98 69 73 74 20 26 26 20 21 49 73 56 69 72 74 75 61  ist && !IsVirtua
19c99 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 69  l(pTab) ){.    i
19c9a 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  f( db->flags & S
19c9b 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
19c9c 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 63  ){.      /* If c
19c9d 6f 75 6e 74 69 6e 67 20 72 6f 77 73 20 64 65 6c  ounting rows del
19c9e 65 74 65 64 2c 20 6a 75 73 74 20 63 6f 75 6e 74  eted, just count
19c9f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
19ca0 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 65 6e  r of.      ** en
19ca1 74 72 69 65 73 20 69 6e 20 74 68 65 20 74 61 62  tries in the tab
19ca2 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  le. */.      int
19ca3 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c   endOfLoop = sql
19ca4 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
19ca5 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  l(v);.      int 
19ca6 61 64 64 72 32 3b 0a 20 20 20 20 20 20 69 66 28  addr2;.      if(
19ca7 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20   !isView ){.    
19ca8 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
19ca9 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75  able(pParse, iCu
19caa 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50  r, iDb, pTab, OP
19cab 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20  _OpenRead);.    
19cac 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
19cad 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
19cae 5f 52 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 73  _Rewind, iCur, s
19caf 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
19cb0 74 41 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20  tAddr(v)+2);.   
19cb1 20 20 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74     addr2 = sqlit
19cb2 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19cb3 50 5f 4d 65 6d 49 6e 63 72 2c 20 31 2c 20 6d 65  P_MemIncr, 1, me
19cb4 6d 43 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  mCnt);.      sql
19cb5 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
19cb6 20 4f 50 5f 4e 65 78 74 2c 20 69 43 75 72 2c 20   OP_Next, iCur, 
19cb7 61 64 64 72 32 29 3b 0a 20 20 20 20 20 20 73 71  addr2);.      sq
19cb8 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
19cb9 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f  Label(v, endOfLo
19cba 6f 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  op);.      sqlit
19cbb 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19cbc 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30  P_Close, iCur, 0
19cbd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
19cbe 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20   !isView ){.    
19cbf 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19cc0 4f 70 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20  Op(v, OP_Clear, 
19cc1 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pTab->tnum, iDb)
19cc2 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 61  ;.      if( !pPa
19cc3 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20  rse->nested ){. 
19cc4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19cc5 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 2d 31  beChangeP3(v, -1
19cc6 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50  , pTab->zName, P
19cc7 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  3_STATIC);.     
19cc8 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64   }.      for(pId
19cc9 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
19cca 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
19ccb 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20  >pNext){.       
19ccc 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70   assert( pIdx->p
19ccd 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
19cce 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 20  chema );.       
19ccf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19cd0 70 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70  p(v, OP_Clear, p
19cd1 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b  Idx->tnum, iDb);
19cd2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
19cd3 20 7d 20 0a 20 20 2f 2a 20 54 68 65 20 75 73 75   } .  /* The usu
19cd4 61 6c 20 63 61 73 65 3a 20 54 68 65 72 65 20 69  al case: There i
19cd5 73 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65  s a WHERE clause
19cd6 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73   so we have to s
19cd7 63 61 6e 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a  can through.  **
19cd8 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 70   the table and p
19cd9 69 63 6b 20 77 68 69 63 68 20 72 65 63 6f 72 64  ick which record
19cda 73 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 2a  s to delete..  *
19cdb 2f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  /.  else{.    /*
19cdc 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62   Begin the datab
19cdd 61 73 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f 0a  ase scan.    */.
19cde 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c      pWInfo = sql
19cdf 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70  ite3WhereBegin(p
19ce0 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
19ce1 20 70 57 68 65 72 65 2c 20 30 29 3b 0a 20 20 20   pWhere, 0);.   
19ce2 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29   if( pWInfo==0 )
19ce3 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
19ce4 6d 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 20 20  m_cleanup;..    
19ce5 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20  /* Remember the 
19ce6 72 6f 77 69 64 20 6f 66 20 65 76 65 72 79 20 69  rowid of every i
19ce7 74 65 6d 20 74 6f 20 62 65 20 64 65 6c 65 74 65  tem to be delete
19ce8 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
19ce9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
19cea 2c 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  , IsVirtual(pTab
19ceb 29 20 3f 20 4f 50 5f 56 52 6f 77 69 64 20 3a 20  ) ? OP_VRowid : 
19cec 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20  OP_Rowid, iCur, 
19ced 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
19cee 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46  dbeAddOp(v, OP_F
19cef 69 66 6f 57 72 69 74 65 2c 20 30 2c 20 30 29 3b  ifoWrite, 0, 0);
19cf0 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  .    if( db->fla
19cf1 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e  gs & SQLITE_Coun
19cf2 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 20 20 73  tRows ){.      s
19cf3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
19cf4 76 2c 20 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 31  v, OP_MemIncr, 1
19cf5 2c 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 20 20 7d  , memCnt);.    }
19cf6 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68 65  ..    /* End the
19cf7 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c   database scan l
19cf8 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  oop..    */.    
19cf9 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
19cfa 70 57 49 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a  pWInfo);..    /*
19cfb 20 4f 70 65 6e 20 74 68 65 20 70 73 65 75 64 6f   Open the pseudo
19cfc 2d 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 73  -table used to s
19cfd 74 6f 72 65 20 4f 4c 44 20 69 66 20 74 68 65 72  tore OLD if ther
19cfe 65 20 61 72 65 20 74 72 69 67 67 65 72 73 2e 0a  e are triggers..
19cff 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 74      */.    if( t
19d00 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b  riggers_exist ){
19d01 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
19d02 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f 70  beAddOp(v, OP_Op
19d03 65 6e 50 73 65 75 64 6f 2c 20 6f 6c 64 49 64 78  enPseudo, oldIdx
19d04 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
19d05 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
19d06 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
19d07 2c 20 6f 6c 64 49 64 78 2c 20 70 54 61 62 2d 3e  , oldIdx, pTab->
19d08 6e 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  nCol);.    }..  
19d09 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 76 65 72    /* Delete ever
19d0a 79 20 69 74 65 6d 20 77 68 6f 73 65 20 6b 65 79  y item whose key
19d0b 20 77 61 73 20 77 72 69 74 74 65 6e 20 74 6f 20   was written to 
19d0c 74 68 65 20 6c 69 73 74 20 64 75 72 69 6e 67 20  the list during 
19d0d 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
19d0e 61 73 65 20 73 63 61 6e 2e 20 20 57 65 20 68 61  ase scan.  We ha
19d0f 76 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 65  ve to delete ite
19d10 6d 73 20 61 66 74 65 72 20 74 68 65 20 73 63 61  ms after the sca
19d11 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 0a 20 20  n is complete.  
19d12 20 20 2a 2a 20 62 65 63 61 75 73 65 20 64 65 6c    ** because del
19d13 65 74 69 6e 67 20 61 6e 20 69 74 65 6d 20 63 61  eting an item ca
19d14 6e 20 63 68 61 6e 67 65 20 74 68 65 20 73 63 61  n change the sca
19d15 6e 20 6f 72 64 65 72 2e 0a 20 20 20 20 2a 2f 0a  n order..    */.
19d16 20 20 20 20 65 6e 64 20 3d 20 73 71 6c 69 74 65      end = sqlite
19d17 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
19d18 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  );..    /* This 
19d19 69 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  is the beginning
19d1a 20 6f 66 20 74 68 65 20 64 65 6c 65 74 65 20 6c   of the delete l
19d1b 6f 6f 70 20 77 68 65 6e 20 74 68 65 72 65 20 61  oop when there a
19d1c 72 65 0a 20 20 20 20 2a 2a 20 72 6f 77 20 74 72  re.    ** row tr
19d1d 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20  iggers..    */. 
19d1e 20 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f     if( triggers_
19d1f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 61  exist ){.      a
19d20 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
19d21 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46 69 66  eAddOp(v, OP_Fif
19d22 6f 52 65 61 64 2c 20 30 2c 20 65 6e 64 29 3b 0a  oRead, 0, end);.
19d23 20 20 20 20 20 20 69 66 28 20 21 69 73 56 69 65        if( !isVie
19d24 77 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  w ){.        sql
19d25 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
19d26 20 4f 50 5f 44 75 70 2c 20 30 2c 20 30 29 3b 0a   OP_Dup, 0, 0);.
19d27 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
19d28 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
19d29 20 69 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62   iCur, iDb, pTab
19d2a 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a  , OP_OpenRead);.
19d2b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
19d2c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
19d2d 2c 20 4f 50 5f 4d 6f 76 65 47 65 2c 20 69 43 75  , OP_MoveGe, iCu
19d2e 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  r, 0);.      sql
19d2f 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
19d30 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c   OP_Rowid, iCur,
19d31 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
19d32 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19d33 50 5f 52 6f 77 44 61 74 61 2c 20 69 43 75 72 2c  P_RowData, iCur,
19d34 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
19d35 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19d36 50 5f 49 6e 73 65 72 74 2c 20 6f 6c 64 49 64 78  P_Insert, oldIdx
19d37 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
19d38 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20  !isView ){.     
19d39 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19d3a 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
19d3b 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20   iCur, 0);.     
19d3c 20 7d 0a 0a 20 20 20 20 20 20 28 76 6f 69 64 29   }..      (void)
19d3d 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
19d3e 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 54 4b  igger(pParse, TK
19d3f 5f 44 45 4c 45 54 45 2c 20 30 2c 20 54 52 49 47  _DELETE, 0, TRIG
19d40 47 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62  GER_BEFORE, pTab
19d41 2c 0a 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20  ,.          -1, 
19d42 6f 6c 64 49 64 78 2c 20 28 70 50 61 72 73 65 2d  oldIdx, (pParse-
19d43 3e 74 72 69 67 53 74 61 63 6b 29 3f 70 50 61 72  >trigStack)?pPar
19d44 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e 6f  se->trigStack->o
19d45 72 63 6f 6e 66 3a 4f 45 5f 44 65 66 61 75 6c 74  rconf:OE_Default
19d46 2c 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 72  ,.          addr
19d47 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
19d48 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20  ( !isView ){.   
19d49 20 20 20 2f 2a 20 4f 70 65 6e 20 63 75 72 73 6f     /* Open curso
19d4a 72 73 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  rs for the table
19d4b 20 77 65 20 61 72 65 20 64 65 6c 65 74 69 6e 67   we are deleting
19d4c 20 66 72 6f 6d 20 61 6e 64 20 61 6c 6c 20 69 74   from and all it
19d4d 73 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64 69 63  s.      ** indic
19d4e 65 73 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  es.  If there ar
19d4f 65 20 72 6f 77 20 74 72 69 67 67 65 72 73 2c 20  e row triggers, 
19d50 74 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e 73  this happens ins
19d51 69 64 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  ide the.      **
19d52 20 4f 50 5f 46 69 66 6f 52 65 61 64 20 6c 6f 6f   OP_FifoRead loo
19d53 70 20 62 65 63 61 75 73 65 20 74 68 65 20 63 75  p because the cu
19d54 72 73 6f 72 20 68 61 76 65 20 74 6f 20 61 6c 6c  rsor have to all
19d55 20 62 65 20 63 6c 6f 73 65 64 0a 20 20 20 20 20   be closed.     
19d56 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 74   ** before the t
19d57 72 69 67 67 65 72 20 66 69 72 65 73 2e 20 20 49  rigger fires.  I
19d58 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72  f there are no r
19d59 6f 77 20 74 72 69 67 67 65 72 73 2c 20 74 68 65  ow triggers, the
19d5a 0a 20 20 20 20 20 20 2a 2a 20 63 75 72 73 6f 72  .      ** cursor
19d5b 73 20 61 72 65 20 6f 70 65 6e 65 64 20 6f 6e 6c  s are opened onl
19d5c 79 20 6f 6e 63 65 20 6f 6e 20 74 68 65 20 6f 75  y once on the ou
19d5d 74 73 69 64 65 20 74 68 65 20 6c 6f 6f 70 2e 0a  tside the loop..
19d5e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
19d5f 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41  qlite3OpenTableA
19d60 6e 64 49 6e 64 69 63 65 73 28 70 50 61 72 73 65  ndIndices(pParse
19d61 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 4f 50  , pTab, iCur, OP
19d62 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 0a 20 20  _OpenWrite);..  
19d63 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
19d64 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
19d65 74 68 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70 20  the delete loop 
19d66 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e  when there are n
19d67 6f 0a 20 20 20 20 20 20 2a 2a 20 72 6f 77 20 74  o.      ** row t
19d68 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 20  riggers */.     
19d69 20 69 66 28 20 21 74 72 69 67 67 65 72 73 5f 65   if( !triggers_e
19d6a 78 69 73 74 20 29 7b 20 0a 20 20 20 20 20 20 20  xist ){ .       
19d6b 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
19d6c 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46  dbeAddOp(v, OP_F
19d6d 69 66 6f 52 65 61 64 2c 20 30 2c 20 65 6e 64 29  ifoRead, 0, end)
19d6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
19d6f 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 72   /* Delete the r
19d70 6f 77 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ow */.#ifndef SQ
19d71 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
19d72 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 69 66 28  LTABLE.      if(
19d73 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
19d74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72   ){.        pPar
19d75 73 65 2d 3e 70 56 69 72 74 75 61 6c 4c 6f 63 6b  se->pVirtualLock
19d76 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20   = pTab;.       
19d77 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
19d78 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20 30  v, OP_VUpdate, 0
19d79 2c 20 31 2c 20 28 63 6f 6e 73 74 20 63 68 61 72  , 1, (const char
19d7a 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50  *)pTab->pVtab, P
19d7b 33 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20 7d  3_VTAB);.      }
19d7c 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
19d7d 20 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69    {.        sqli
19d7e 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65  te3GenerateRowDe
19d7f 6c 65 74 65 28 64 62 2c 20 76 2c 20 70 54 61 62  lete(db, v, pTab
19d80 2c 20 69 43 75 72 2c 20 70 50 61 72 73 65 2d 3e  , iCur, pParse->
19d81 6e 65 73 74 65 64 3d 3d 30 29 3b 0a 20 20 20 20  nested==0);.    
19d82 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
19d83 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 72  * If there are r
19d84 6f 77 20 74 72 69 67 67 65 72 73 2c 20 63 6c 6f  ow triggers, clo
19d85 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 74  se all cursors t
19d86 68 65 6e 20 69 6e 76 6f 6b 65 0a 20 20 20 20 2a  hen invoke.    *
19d87 2a 20 74 68 65 20 41 46 54 45 52 20 74 72 69 67  * the AFTER trig
19d88 67 65 72 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20  gers.    */.    
19d89 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69  if( triggers_exi
19d8a 73 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  st ){.      if( 
19d8b 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20  !isView ){.     
19d8c 20 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78     for(i=1, pIdx
19d8d 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
19d8e 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70  Idx; i++, pIdx=p
19d8f 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
19d90 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19d91 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c  beAddOp(v, OP_Cl
19d92 6f 73 65 2c 20 69 43 75 72 20 2b 20 69 2c 20 70  ose, iCur + i, p
19d93 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20  Idx->tnum);.    
19d94 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
19d95 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
19d96 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72  , OP_Close, iCur
19d97 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
19d98 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
19d99 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
19d9a 70 50 61 72 73 65 2c 20 54 4b 5f 44 45 4c 45 54  pParse, TK_DELET
19d9b 45 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46  E, 0, TRIGGER_AF
19d9c 54 45 52 2c 20 70 54 61 62 2c 20 2d 31 2c 0a 20  TER, pTab, -1,. 
19d9d 20 20 20 20 20 20 20 20 20 6f 6c 64 49 64 78 2c           oldIdx,
19d9e 20 28 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74   (pParse->trigSt
19d9f 61 63 6b 29 3f 70 50 61 72 73 65 2d 3e 74 72 69  ack)?pParse->tri
19da0 67 53 74 61 63 6b 2d 3e 6f 72 63 6f 6e 66 3a 4f  gStack->orconf:O
19da1 45 5f 44 65 66 61 75 6c 74 2c 0a 20 20 20 20 20  E_Default,.     
19da2 20 20 20 20 20 61 64 64 72 29 3b 0a 20 20 20 20       addr);.    
19da3 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66  }..    /* End of
19da4 20 74 68 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70   the delete loop
19da5 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
19da6 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47  dbeAddOp(v, OP_G
19da7 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20  oto, 0, addr);. 
19da8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
19da9 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e  solveLabel(v, en
19daa 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73  d);..    /* Clos
19dab 65 20 74 68 65 20 63 75 72 73 6f 72 73 20 61 66  e the cursors af
19dac 74 65 72 20 74 68 65 20 6c 6f 6f 70 20 69 66 20  ter the loop if 
19dad 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 6f 77  there are no row
19dae 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20   triggers */.   
19daf 20 69 66 28 20 21 74 72 69 67 67 65 72 73 5f 65   if( !triggers_e
19db0 78 69 73 74 20 26 26 20 21 49 73 56 69 72 74 75  xist && !IsVirtu
19db1 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
19db2 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d    for(i=1, pIdx=
19db3 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
19db4 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49  dx; i++, pIdx=pI
19db5 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
19db6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
19db7 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  ddOp(v, OP_Close
19db8 2c 20 69 43 75 72 20 2b 20 69 2c 20 70 49 64 78  , iCur + i, pIdx
19db9 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 7d  ->tnum);.      }
19dba 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
19dbb 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c  beAddOp(v, OP_Cl
19dbc 6f 73 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20  ose, iCur, 0);. 
19dbd 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20     }.  }..  /*. 
19dbe 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e   ** Return the n
19dbf 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68  umber of rows th
19dc0 61 74 20 77 65 72 65 20 64 65 6c 65 74 65 64 2e  at were deleted.
19dc1 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
19dc2 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61   is .  ** genera
19dc3 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73  ting code becaus
19dc4 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73  e of a call to s
19dc5 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
19dc6 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a  e(), do not.  **
19dc7 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   invoke the call
19dc8 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  back function.. 
19dc9 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c   */.  if( db->fl
19dca 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
19dcb 6e 74 52 6f 77 73 20 26 26 20 70 50 61 72 73 65  ntRows && pParse
19dcc 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 21  ->nested==0 && !
19dcd 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
19dce 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  k ){.    sqlite3
19dcf 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
19dd0 4d 65 6d 4c 6f 61 64 2c 20 6d 65 6d 43 6e 74 2c  MemLoad, memCnt,
19dd1 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
19dd2 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
19dd3 43 61 6c 6c 62 61 63 6b 2c 20 31 2c 20 30 29 3b  Callback, 1, 0);
19dd4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19dd5 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
19dd6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
19dd7 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
19dd8 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
19dd9 22 72 6f 77 73 20 64 65 6c 65 74 65 64 22 2c 20  "rows deleted", 
19dda 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  P3_STATIC);.  }.
19ddb 0a 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65  .delete_from_cle
19ddc 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 41  anup:.  sqlite3A
19ddd 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26 73  uthContextPop(&s
19dde 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69  Context);.  sqli
19ddf 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
19de0 28 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71  (pTabList);.  sq
19de1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
19de2 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72  pWhere);.  retur
19de3 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  n;.}../*.** This
19de4 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
19de5 65 73 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  es VDBE code tha
19de6 74 20 63 61 75 73 65 73 20 61 20 73 69 6e 67 6c  t causes a singl
19de7 65 20 72 6f 77 20 6f 66 20 61 0a 2a 2a 20 73 69  e row of a.** si
19de8 6e 67 6c 65 20 74 61 62 6c 65 20 74 6f 20 62 65  ngle table to be
19de9 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   deleted..**.** 
19dea 54 68 65 20 56 44 42 45 20 6d 75 73 74 20 62 65  The VDBE must be
19deb 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   in a particular
19dec 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73   state when this
19ded 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
19dee 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72 65  ed..** These are
19def 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74   the requirement
19df0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41  s:.**.**   1.  A
19df1 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
19df2 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70  or pointing to p
19df3 54 61 62 2c 20 74 68 65 20 74 61 62 6c 65 20 63  Tab, the table c
19df4 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f  ontaining the ro
19df5 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20 62 65  w.**       to be
19df6 20 64 65 6c 65 74 65 64 2c 20 6d 75 73 74 20 62   deleted, must b
19df7 65 20 6f 70 65 6e 65 64 20 61 73 20 63 75 72 73  e opened as curs
19df8 6f 72 20 6e 75 6d 62 65 72 20 22 62 61 73 65 22  or number "base"
19df9 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65  ..**.**   2.  Re
19dfa 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73  ad/write cursors
19dfb 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73   for all indices
19dfc 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20 62 65   of pTab must be
19dfd 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20   open as.**     
19dfe 20 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20    cursor number 
19dff 62 61 73 65 2b 69 20 66 6f 72 20 74 68 65 20 69  base+i for the i
19e00 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a  -th index..**.**
19e01 20 20 20 33 2e 20 20 54 68 65 20 72 65 63 6f 72     3.  The recor
19e02 64 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  d number of the 
19e03 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65  row to be delete
19e04 64 20 6d 75 73 74 20 62 65 20 6f 6e 20 74 68 65  d must be on the
19e05 20 74 6f 70 0a 2a 2a 20 20 20 20 20 20 20 6f 66   top.**       of
19e06 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a   the stack..**.*
19e07 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70  * This routine p
19e08 6f 70 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74  ops the top of t
19e09 68 65 20 73 74 61 63 6b 20 74 6f 20 72 65 6d 6f  he stack to remo
19e0a 76 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  ve the record nu
19e0b 6d 62 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 6e  mber.** and then
19e0c 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
19e0d 74 6f 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74  to remove both t
19e0e 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20  he table record 
19e0f 61 6e 64 20 61 6c 6c 20 69 6e 64 65 78 0a 2a 2a  and all index.**
19e10 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 70 6f   entries that po
19e11 69 6e 74 20 74 6f 20 74 68 61 74 20 72 65 63 6f  int to that reco
19e12 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rd..*/.SQLITE_PR
19e13 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
19e14 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
19e15 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ete(.  sqlite3 *
19e16 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65  db,       /* The
19e17 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
19e18 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20 2a  ning the index *
19e19 2f 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20  /.  Vdbe *v,    
19e1a 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61         /* Genera
19e1b 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69  te code into thi
19e1c 73 20 56 44 42 45 20 2a 2f 0a 20 20 54 61 62 6c  s VDBE */.  Tabl
19e1d 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f  e *pTab,       /
19e1e 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  * Table containi
19e1f 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65  ng the row to be
19e20 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e   deleted */.  in
19e21 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20  t iCur,         
19e22 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
19e23 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  r for the table 
19e24 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 20 20  */.  int count  
19e25 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65          /* Incre
19e26 6d 65 6e 74 20 74 68 65 20 72 6f 77 20 63 68 61  ment the row cha
19e27 6e 67 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 29  nge counter */.)
19e28 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20  {.  int addr;.  
19e29 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
19e2a 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f  beAddOp(v, OP_No
19e2b 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 30  tExists, iCur, 0
19e2c 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 65 6e 65  );.  sqlite3Gene
19e2d 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
19e2e 74 65 28 76 2c 20 70 54 61 62 2c 20 69 43 75 72  te(v, pTab, iCur
19e2f 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 0);.  sqlite3V
19e30 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44  dbeAddOp(v, OP_D
19e31 65 6c 65 74 65 2c 20 69 43 75 72 2c 20 28 63 6f  elete, iCur, (co
19e32 75 6e 74 3f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e  unt?OPFLAG_NCHAN
19e33 47 45 3a 30 29 29 3b 0a 20 20 69 66 28 20 63 6f  GE:0));.  if( co
19e34 75 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  unt ){.    sqlit
19e35 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76  e3VdbeChangeP3(v
19e36 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  , -1, pTab->zNam
19e37 65 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20  e, P3_STATIC);. 
19e38 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
19e39 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
19e3a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
19e3b 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
19e3c 65 73 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  es VDBE code tha
19e3d 74 20 63 61 75 73 65 73 20 74 68 65 20 64 65 6c  t causes the del
19e3e 65 74 69 6f 6e 20 6f 66 20 61 6c 6c 0a 2a 2a 20  etion of all.** 
19e3f 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61 73  index entries as
19e40 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
19e41 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20  single row of a 
19e42 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2a  single table..**
19e43 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73  .** The VDBE mus
19e44 74 20 62 65 20 69 6e 20 61 20 70 61 72 74 69 63  t be in a partic
19e45 75 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e 20  ular state when 
19e46 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
19e47 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65  called..** These
19e48 20 61 72 65 20 74 68 65 20 72 65 71 75 69 72 65   are the require
19e49 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ments:.**.**   1
19e4a 2e 20 20 41 20 72 65 61 64 2f 77 72 69 74 65 20  .  A read/write 
19e4b 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
19e4c 74 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61 62  to pTab, the tab
19e4d 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
19e4e 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74  e row.**       t
19e4f 6f 20 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75  o be deleted, mu
19e50 73 74 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20  st be opened as 
19e51 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 69  cursor number "i
19e52 43 75 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e  Cur"..**.**   2.
19e53 20 20 52 65 61 64 2f 77 72 69 74 65 20 63 75 72    Read/write cur
19e54 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64  sors for all ind
19e55 69 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73  ices of pTab mus
19e56 74 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20  t be open as.** 
19e57 20 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d        cursor num
19e58 62 65 72 20 69 43 75 72 2b 69 20 66 6f 72 20 74  ber iCur+i for t
19e59 68 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a  he i-th index..*
19e5a 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 22  *.**   3.  The "
19e5b 69 43 75 72 22 20 63 75 72 73 6f 72 20 6d 75 73  iCur" cursor mus
19e5c 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f  t be pointing to
19e5d 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 69 73   the row that is
19e5e 20 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20   to be.**       
19e5f 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  deleted..*/.SQLI
19e60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19e61 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
19e62 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 0a 20  owIndexDelete(. 
19e63 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20   Vdbe *v,       
19e64 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
19e65 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56  code into this V
19e66 44 42 45 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  DBE */.  Table *
19e67 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54  pTab,       /* T
19e68 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
19e69 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65  the row to be de
19e6a 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  leted */.  int i
19e6b 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Cur,          /*
19e6c 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
19e6d 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  or the table */.
19e6e 20 20 63 68 61 72 20 2a 61 49 64 78 55 73 65 64    char *aIdxUsed
19e6f 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 64 65 6c       /* Only del
19e70 65 74 65 20 69 66 20 61 49 64 78 55 73 65 64 21  ete if aIdxUsed!
19e71 3d 30 20 26 26 20 61 49 64 78 55 73 65 64 5b 69  =0 && aIdxUsed[i
19e72 5d 21 3d 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ]!=0 */.){.  int
19e73 20 69 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   i;.  Index *pId
19e74 78 3b 0a 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70  x;..  for(i=1, p
19e75 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
19e76 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64  ; pIdx; i++, pId
19e77 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
19e78 20 20 20 20 69 66 28 20 61 49 64 78 55 73 65 64      if( aIdxUsed
19e79 21 3d 30 20 26 26 20 61 49 64 78 55 73 65 64 5b  !=0 && aIdxUsed[
19e7a 69 2d 31 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  i-1]==0 ) contin
19e7b 75 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  ue;.    sqlite3G
19e7c 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28  enerateIndexKey(
19e7d 76 2c 20 70 49 64 78 2c 20 69 43 75 72 29 3b 0a  v, pIdx, iCur);.
19e7e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
19e7f 64 64 4f 70 28 76 2c 20 4f 50 5f 49 64 78 44 65  ddOp(v, OP_IdxDe
19e80 6c 65 74 65 2c 20 69 43 75 72 2b 69 2c 20 30 29  lete, iCur+i, 0)
19e81 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
19e82 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
19e83 74 20 77 69 6c 6c 20 61 73 73 65 6d 62 6c 65 20  t will assemble 
19e84 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 61 6e 64  an index key and
19e85 20 70 75 74 20 69 74 20 6f 6e 20 74 68 65 20 74   put it on the t
19e86 6f 70 0a 2a 2a 20 6f 66 20 74 68 65 20 74 61 63  op.** of the tac
19e87 6b 2e 20 20 54 68 65 20 6b 65 79 20 77 69 74 68  k.  The key with
19e88 20 62 65 20 66 6f 72 20 69 6e 64 65 78 20 70 49   be for index pI
19e89 64 78 20 77 68 69 63 68 20 69 73 20 61 6e 20 69  dx which is an i
19e8a 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a  ndex on pTab..**
19e8b 20 69 43 75 72 20 69 73 20 74 68 65 20 69 6e 64   iCur is the ind
19e8c 65 78 20 6f 66 20 61 20 63 75 72 73 6f 72 20 6f  ex of a cursor o
19e8d 70 65 6e 20 6f 6e 20 74 68 65 20 70 54 61 62 20  pen on the pTab 
19e8e 74 61 62 6c 65 20 61 6e 64 20 70 6f 69 6e 74 69  table and pointi
19e8f 6e 67 20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e 74  ng to.** the ent
19e90 72 79 20 74 68 61 74 20 6e 65 65 64 73 20 69 6e  ry that needs in
19e91 64 65 78 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  dexing..*/.SQLIT
19e92 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19e93 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e  qlite3GenerateIn
19e94 64 65 78 4b 65 79 28 0a 20 20 56 64 62 65 20 2a  dexKey(.  Vdbe *
19e95 76 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  v,           /* 
19e96 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
19e97 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a  to this VDBE */.
19e98 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20    Index *pIdx,  
19e99 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
19e9a 78 20 66 6f 72 20 77 68 69 63 68 20 74 6f 20 67  x for which to g
19e9b 65 6e 65 72 61 74 65 20 61 20 6b 65 79 20 2a 2f  enerate a key */
19e9c 0a 20 20 69 6e 74 20 69 43 75 72 20 20 20 20 20  .  int iCur     
19e9d 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
19e9e 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70  number for the p
19e9f 49 64 78 2d 3e 70 54 61 62 6c 65 20 74 61 62 6c  Idx->pTable tabl
19ea0 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6a 3b  e */.){.  int j;
19ea1 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
19ea2 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 0a   pIdx->pTable;..
19ea3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19ea4 4f 70 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  Op(v, OP_Rowid, 
19ea5 69 43 75 72 2c 20 30 29 3b 0a 20 20 66 6f 72 28  iCur, 0);.  for(
19ea6 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f  j=0; j<pIdx->nCo
19ea7 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  lumn; j++){.    
19ea8 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d 3e  int idx = pIdx->
19ea9 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20  aiColumn[j];.   
19eaa 20 69 66 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e   if( idx==pTab->
19eab 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73  iPKey ){.      s
19eac 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
19ead 76 2c 20 4f 50 5f 44 75 70 2c 20 6a 2c 20 30 29  v, OP_Dup, j, 0)
19eae 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
19eaf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19eb0 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  dOp(v, OP_Column
19eb1 2c 20 69 43 75 72 2c 20 69 64 78 29 3b 0a 20 20  , iCur, idx);.  
19eb2 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d      sqlite3Colum
19eb3 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62  nDefault(v, pTab
19eb4 2c 20 69 64 78 29 3b 0a 20 20 20 20 7d 0a 20 20  , idx);.    }.  
19eb5 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
19eb6 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 49  ddOp(v, OP_MakeI
19eb7 64 78 52 65 63 2c 20 70 49 64 78 2d 3e 6e 43 6f  dxRec, pIdx->nCo
19eb8 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20 73 71 6c 69  lumn, 0);.  sqli
19eb9 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79  te3IndexAffinity
19eba 53 74 72 28 76 2c 20 70 49 64 78 29 3b 0a 7d 0a  Str(v, pIdx);.}.
19ebb 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
19ebc 20 45 6e 64 20 6f 66 20 64 65 6c 65 74 65 2e 63   End of delete.c
19ebd 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
19ebe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ebf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
19ec0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
19ec1 20 42 65 67 69 6e 20 66 69 6c 65 20 66 75 6e 63   Begin file func
19ec2 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
19ec3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ec4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
19ec5 0a 2f 2a 0a 2a 2a 20 32 30 30 32 20 46 65 62 72  ./*.** 2002 Febr
19ec6 75 61 72 79 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68  uary 23.**.** Th
19ec7 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
19ec8 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
19ec9 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
19eca 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
19ecb 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
19ecc 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
19ecd 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
19ece 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
19ecf 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
19ed0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
19ed1 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
19ed2 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
19ed3 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
19ed4 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
19ed5 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
19ed6 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
19ed7 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
19ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19edb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19edc 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
19edd 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
19ede 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61   C functions tha
19edf 74 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69  t implement vari
19ee0 6f 75 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74  ous SQL.** funct
19ee1 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20  ions of SQLite. 
19ee2 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73   .**.** There is
19ee3 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74   only one export
19ee4 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69  ed symbol in thi
19ee5 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e  s file - the fun
19ee6 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 52  ction.** sqliteR
19ee7 65 67 69 73 74 65 72 42 75 69 6c 64 69 6e 46 75  egisterBuildinFu
19ee8 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20  nctions() found 
19ee9 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66  at the bottom of
19eea 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c   the file..** Al
19eeb 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73  l other code has
19eec 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a   file scope..**.
19eed 2a 2a 20 24 49 64 3a 20 66 75 6e 63 2e 63 2c 76  ** $Id: func.c,v
19eee 20 31 2e 31 37 34 20 32 30 30 37 2f 30 39 2f 30   1.174 2007/09/0
19eef 33 20 31 31 3a 30 34 3a 32 32 20 64 61 6e 69 65  3 11:04:22 danie
19ef0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
19ef1 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
19ef2 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  he collating fun
19ef3 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64  ction associated
19ef4 20 77 69 74 68 20 61 20 66 75 6e 63 74 69 6f 6e   with a function
19ef5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c  ..*/.static Coll
19ef6 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 46  Seq *sqlite3GetF
19ef7 75 6e 63 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74  uncCollSeq(sqlit
19ef8 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
19ef9 65 78 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  ext){.  return c
19efa 6f 6e 74 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d  ontext->pColl;.}
19efb 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
19efc 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 6f  tation of the no
19efd 6e 2d 61 67 67 72 65 67 61 74 65 20 6d 69 6e 28  n-aggregate min(
19efe 29 20 61 6e 64 20 6d 61 78 28 29 20 66 75 6e 63  ) and max() func
19eff 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20  tions.*/.static 
19f00 76 6f 69 64 20 6d 69 6e 6d 61 78 46 75 6e 63 28  void minmaxFunc(
19f01 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
19f02 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
19f03 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
19f04 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
19f05 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
19f06 74 20 6d 61 73 6b 3b 20 20 20 20 2f 2a 20 30 20  t mask;    /* 0 
19f07 66 6f 72 20 6d 69 6e 28 29 20 6f 72 20 30 78 66  for min() or 0xf
19f08 66 66 66 66 66 66 66 20 66 6f 72 20 6d 61 78 28  fffffff for max(
19f09 29 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 73 74  ) */.  int iBest
19f0a 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  ;.  CollSeq *pCo
19f0b 6c 6c 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d  ll;..  if( argc=
19f0c 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d  =0 ) return;.  m
19f0d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 5f 75 73  ask = sqlite3_us
19f0e 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
19f0f 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b 0a 20 20  ==0 ? 0 : -1;.  
19f10 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
19f11 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f  etFuncCollSeq(co
19f12 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74  ntext);.  assert
19f13 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20 61 73 73  ( pColl );.  ass
19f14 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c  ert( mask==-1 ||
19f15 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 69 42   mask==0 );.  iB
19f16 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  est = 0;.  if( s
19f17 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
19f18 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
19f19 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  TE_NULL ) return
19f1a 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61  ;.  for(i=1; i<a
19f1b 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  rgc; i++){.    i
19f1c 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
19f1d 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29 3d 3d  _type(argv[i])==
19f1e 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65  SQLITE_NULL ) re
19f1f 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 28 73  turn;.    if( (s
19f20 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
19f21 28 61 72 67 76 5b 69 42 65 73 74 5d 2c 20 61 72  (argv[iBest], ar
19f22 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e 6d 61  gv[i], pColl)^ma
19f23 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  sk)>=0 ){.      
19f24 69 42 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d  iBest = i;.    }
19f25 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72  .  }.  sqlite3_r
19f26 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74  esult_value(cont
19f27 65 78 74 2c 20 61 72 67 76 5b 69 42 65 73 74 5d  ext, argv[iBest]
19f28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
19f29 72 6e 20 74 68 65 20 74 79 70 65 20 6f 66 20 74  rn the type of t
19f2a 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  he argument..*/.
19f2b 73 74 61 74 69 63 20 76 6f 69 64 20 74 79 70 65  static void type
19f2c 6f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  ofFunc(.  sqlite
19f2d 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
19f2e 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
19f2f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
19f30 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73  **argv.){.  cons
19f31 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20  t char *z = 0;. 
19f32 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33   switch( sqlite3
19f33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
19f34 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65  [0]) ){.    case
19f35 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 20 20   SQLITE_NULL:   
19f36 20 7a 20 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20   z = "null";    
19f37 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
19f38 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20  SQLITE_INTEGER: 
19f39 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b 20 62  z = "integer"; b
19f3a 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
19f3b 51 4c 49 54 45 5f 54 45 58 54 3a 20 20 20 20 7a  QLITE_TEXT:    z
19f3c 20 3d 20 22 74 65 78 74 22 3b 20 20 20 20 62 72   = "text";    br
19f3d 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51  eak;.    case SQ
19f3e 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20  LITE_FLOAT:   z 
19f3f 3d 20 22 72 65 61 6c 22 3b 20 20 20 20 62 72 65  = "real";    bre
19f40 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
19f41 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d  ITE_BLOB:    z =
19f42 20 22 62 6c 6f 62 22 3b 20 20 20 20 62 72 65 61   "blob";    brea
19f43 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  k;.  }.  sqlite3
19f44 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
19f45 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c  text, z, -1, SQL
19f46 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a  ITE_STATIC);.}..
19f47 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
19f48 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e  ation of the len
19f49 67 74 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a  gth() function.*
19f4a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
19f4b 6e 67 74 68 46 75 6e 63 28 0a 20 20 73 71 6c 69  ngthFunc(.  sqli
19f4c 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
19f4d 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
19f4e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
19f4f 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e  e **argv.){.  in
19f50 74 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74  t len;..  assert
19f51 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 73  ( argc==1 );.  s
19f52 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76  witch( sqlite3_v
19f53 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
19f54 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  ]) ){.    case S
19f55 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20  QLITE_BLOB:.    
19f56 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
19f57 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 53 51  GER:.    case SQ
19f58 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20  LITE_FLOAT: {.  
19f59 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
19f5a 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20  lt_int(context, 
19f5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
19f5c 74 65 73 28 61 72 67 76 5b 30 5d 29 29 3b 0a 20  tes(argv[0]));. 
19f5d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
19f5e 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
19f5f 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20  E_TEXT: {.      
19f60 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
19f61 68 61 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33  har *z = sqlite3
19f62 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
19f63 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [0]);.      if( 
19f64 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  z==0 ) return;. 
19f65 20 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20       len = 0;.  
19f66 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 29 7b      while( *z ){
19f67 0a 20 20 20 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a  .        len++;.
19f68 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53          SQLITE_S
19f69 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20  KIP_UTF8(z);.   
19f6a 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
19f6b 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f  e3_result_int(co
19f6c 6e 74 65 78 74 2c 20 6c 65 6e 29 3b 0a 20 20 20  ntext, len);.   
19f6d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
19f6e 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
19f6f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
19f70 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74  ult_null(context
19f71 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
19f72 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
19f73 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
19f74 6e 20 6f 66 20 74 68 65 20 61 62 73 28 29 20 66  n of the abs() f
19f75 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69  unction.*/.stati
19f76 63 20 76 6f 69 64 20 61 62 73 46 75 6e 63 28 73  c void absFunc(s
19f77 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
19f78 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
19f79 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
19f7a 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65   **argv){.  asse
19f7b 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20  rt( argc==1 );. 
19f7c 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33   switch( sqlite3
19f7d 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
19f7e 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65  [0]) ){.    case
19f7f 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a   SQLITE_INTEGER:
19f80 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 56 61   {.      i64 iVa
19f81 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
19f82 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29  e_int64(argv[0])
19f83 3b 0a 20 20 20 20 20 20 69 66 28 20 69 56 61 6c  ;.      if( iVal
19f84 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  <0 ){.        if
19f85 28 20 28 69 56 61 6c 3c 3c 31 29 3d 3d 30 20 29  ( (iVal<<1)==0 )
19f86 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
19f87 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
19f88 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e 74 65 67  (context, "integ
19f89 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 20 2d 31  er overflow", -1
19f8a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
19f8b 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  urn;.        }. 
19f8c 20 20 20 20 20 20 20 69 56 61 6c 20 3d 20 2d 69         iVal = -i
19f8d 56 61 6c 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20  Val;.      } .  
19f8e 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
19f8f 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
19f90 2c 20 69 56 61 6c 29 3b 0a 20 20 20 20 20 20 62  , iVal);.      b
19f91 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
19f92 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  case SQLITE_NULL
19f93 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
19f94 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f  3_result_null(co
19f95 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72  ntext);.      br
19f96 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
19f97 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
19f98 64 6f 75 62 6c 65 20 72 56 61 6c 20 3d 20 73 71  double rVal = sq
19f99 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
19f9a 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  le(argv[0]);.   
19f9b 20 20 20 69 66 28 20 72 56 61 6c 3c 30 20 29 20     if( rVal<0 ) 
19f9c 72 56 61 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20  rVal = -rVal;.  
19f9d 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
19f9e 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78  lt_double(contex
19f9f 74 2c 20 72 56 61 6c 29 3b 0a 20 20 20 20 20 20  t, rVal);.      
19fa0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
19fa1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
19fa2 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
19fa3 73 75 62 73 74 72 28 29 20 66 75 6e 63 74 69 6f  substr() functio
19fa4 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74 72 28  n..**.** substr(
19fa5 78 2c 70 31 2c 70 32 29 20 20 72 65 74 75 72 6e  x,p1,p2)  return
19fa6 73 20 70 32 20 63 68 61 72 61 63 74 65 72 73 20  s p2 characters 
19fa7 6f 66 20 78 5b 5d 20 62 65 67 69 6e 6e 69 6e 67  of x[] beginning
19fa8 20 77 69 74 68 20 70 31 2e 0a 2a 2a 20 70 31 20   with p1..** p1 
19fa9 69 73 20 31 2d 69 6e 64 65 78 65 64 2e 20 20 53  is 1-indexed.  S
19faa 6f 20 73 75 62 73 74 72 28 78 2c 31 2c 31 29 20  o substr(x,1,1) 
19fab 72 65 74 75 72 6e 73 20 74 68 65 20 66 69 72 73  returns the firs
19fac 74 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 6f  t character.** o
19fad 66 20 78 2e 20 20 49 66 20 78 20 69 73 20 74 65  f x.  If x is te
19fae 78 74 2c 20 74 68 65 6e 20 77 65 20 61 63 74 75  xt, then we actu
19faf 61 6c 6c 79 20 63 6f 75 6e 74 20 55 54 46 2d 38  ally count UTF-8
19fb0 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20   characters..** 
19fb1 49 66 20 78 20 69 73 20 61 20 62 6c 6f 62 2c 20  If x is a blob, 
19fb2 74 68 65 6e 20 77 65 20 63 6f 75 6e 74 20 62 79  then we count by
19fb3 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 31  tes..**.** If p1
19fb4 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
19fb5 65 6e 20 77 65 20 62 65 67 69 6e 20 61 62 73 28  en we begin abs(
19fb6 70 31 29 20 66 72 6f 6d 20 74 68 65 20 65 6e 64  p1) from the end
19fb7 20 6f 66 20 78 5b 5d 2e 0a 2a 2f 0a 73 74 61 74   of x[]..*/.stat
19fb8 69 63 20 76 6f 69 64 20 73 75 62 73 74 72 46 75  ic void substrFu
19fb9 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
19fba 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
19fbb 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
19fbc 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
19fbd 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  gv.){.  const un
19fbe 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a  signed char *z;.
19fbf 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
19fc0 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74   char *z2;.  int
19fc1 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 70 30 74 79   len;.  int p0ty
19fc2 70 65 3b 0a 20 20 69 36 34 20 70 31 2c 20 70 32  pe;.  i64 p1, p2
19fc3 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
19fc4 63 3d 3d 33 20 29 3b 0a 20 20 70 30 74 79 70 65  c==3 );.  p0type
19fc5 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
19fc6 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a  _type(argv[0]);.
19fc7 20 20 69 66 28 20 70 30 74 79 70 65 3d 3d 53 51    if( p0type==SQ
19fc8 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20  LITE_BLOB ){.   
19fc9 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76   len = sqlite3_v
19fca 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
19fcb 30 5d 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c  0]);.    z = sql
19fcc 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
19fcd 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66  argv[0]);.    if
19fce 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( z==0 ) return;
19fcf 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e  .    assert( len
19fd0 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ==sqlite3_value_
19fd1 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20 29  bytes(argv[0]) )
19fd2 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
19fd3 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
19fd4 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
19fd5 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72      if( z==0 ) r
19fd6 65 74 75 72 6e 3b 0a 20 20 20 20 6c 65 6e 20 3d  eturn;.    len =
19fd7 20 30 3b 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a   0;.    for(z2=z
19fd8 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a 20  ; *z2; len++){. 
19fd9 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50       SQLITE_SKIP
19fda 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d  _UTF8(z2);.    }
19fdb 0a 20 20 7d 0a 20 20 70 31 20 3d 20 73 71 6c 69  .  }.  p1 = sqli
19fdc 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
19fdd 67 76 5b 31 5d 29 3b 0a 20 20 70 32 20 3d 20 73  gv[1]);.  p2 = s
19fde 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
19fdf 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66 28  (argv[2]);.  if(
19fe0 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 70 31 20   p1<0 ){.    p1 
19fe1 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20  += len;.    if( 
19fe2 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32  p1<0 ){.      p2
19fe3 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 70 31   += p1;.      p1
19fe4 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   = 0;.    }.  }e
19fe5 6c 73 65 20 69 66 28 20 70 31 3e 30 20 29 7b 0a  lse if( p1>0 ){.
19fe6 20 20 20 20 70 31 2d 2d 3b 0a 20 20 7d 0a 20 20      p1--;.  }.  
19fe7 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e 20 29 7b  if( p1+p2>len ){
19fe8 0a 20 20 20 20 70 32 20 3d 20 6c 65 6e 2d 70 31  .    p2 = len-p1
19fe9 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 30 74 79  ;.  }.  if( p0ty
19fea 70 65 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20  pe!=SQLITE_BLOB 
19feb 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a  ){.    while( *z
19fec 20 26 26 20 70 31 20 29 7b 0a 20 20 20 20 20 20   && p1 ){.      
19fed 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
19fee 28 7a 29 3b 0a 20 20 20 20 20 20 70 31 2d 2d 3b  (z);.      p1--;
19fef 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 7a  .    }.    for(z
19ff0 32 3d 7a 3b 20 2a 7a 32 20 26 26 20 70 32 3b 20  2=z; *z2 && p2; 
19ff1 70 32 2d 2d 29 7b 0a 20 20 20 20 20 20 53 51 4c  p2--){.      SQL
19ff2 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32  ITE_SKIP_UTF8(z2
19ff3 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
19ff4 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
19ff5 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
19ff6 29 7a 2c 20 7a 32 2d 7a 2c 20 53 51 4c 49 54 45  )z, z2-z, SQLITE
19ff7 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
19ff8 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 32  else{.    if( p2
19ff9 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 20  <0 ) p2 = 0;.   
19ffa 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
19ffb 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63  blob(context, (c
19ffc 68 61 72 2a 29 26 7a 5b 70 31 5d 2c 20 70 32 2c  har*)&z[p1], p2,
19ffd 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
19ffe 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  T);.  }.}../*.**
19fff 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
1a000 6f 66 20 74 68 65 20 72 6f 75 6e 64 28 29 20 66  of the round() f
1a001 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69  unction.*/.stati
1a002 63 20 76 6f 69 64 20 72 6f 75 6e 64 46 75 6e 63  c void roundFunc
1a003 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1a004 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61   *context, int a
1a005 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
1a006 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 69 6e  ue **argv){.  in
1a007 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 75 62 6c  t n = 0;.  doubl
1a008 65 20 72 3b 0a 20 20 63 68 61 72 20 7a 42 75 66  e r;.  char zBuf
1a009 5b 35 30 30 5d 3b 20 20 2f 2a 20 6c 61 72 67 65  [500];  /* large
1a00a 72 20 74 68 61 6e 20 74 68 65 20 25 66 20 72 65  r than the %f re
1a00b 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1a00c 74 68 65 20 6c 61 72 67 65 73 74 20 64 6f 75 62  the largest doub
1a00d 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  le */.  assert( 
1a00e 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d  argc==1 || argc=
1a00f 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63  =2 );.  if( argc
1a010 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66 28 20 53  ==2 ){.    if( S
1a011 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69  QLITE_NULL==sqli
1a012 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
1a013 72 67 76 5b 31 5d 29 20 29 20 72 65 74 75 72 6e  rgv[1]) ) return
1a014 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65  ;.    n = sqlite
1a015 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
1a016 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e  [1]);.    if( n>
1a017 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a 20 20 20  30 ) n = 30;.   
1a018 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30   if( n<0 ) n = 0
1a019 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
1a01a 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
1a01b 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[0])==SQLITE_
1a01c 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20  NULL ) return;. 
1a01d 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   r = sqlite3_val
1a01e 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30  ue_double(argv[0
1a01f 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e  ]);.  sqlite3_sn
1a020 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42  printf(sizeof(zB
1a021 75 66 29 2c 7a 42 75 66 2c 22 25 2e 2a 66 22 2c  uf),zBuf,"%.*f",
1a022 6e 2c 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 41  n,r);.  sqlite3A
1a023 74 6f 46 28 7a 42 75 66 2c 20 26 72 29 3b 0a 20  toF(zBuf, &r);. 
1a024 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a025 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
1a026 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  r);.}../*.** All
1a027 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65  ocate nByte byte
1a028 73 20 6f 66 20 73 70 61 63 65 20 75 73 69 6e 67  s of space using
1a029 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1a02a 29 2e 20 49 66 20 74 68 65 0a 2a 2a 20 61 6c 6c  ). If the.** all
1a02b 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 63  ocation fails, c
1a02c 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73 75  all sqlite3_resu
1a02d 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29  lt_error_nomem()
1a02e 20 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20 74 68   to notify.** th
1a02f 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1a030 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20  e that malloc() 
1a031 68 61 73 20 66 61 69 6c 65 64 2e 0a 2a 2f 0a 73  has failed..*/.s
1a032 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 74  tatic void *cont
1a033 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65  extMalloc(sqlite
1a034 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1a035 78 74 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a  xt, int nByte){.
1a036 20 20 63 68 61 72 20 2a 7a 20 3d 20 73 71 6c 69    char *z = sqli
1a037 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65  te3_malloc(nByte
1a038 29 3b 0a 20 20 69 66 28 20 21 7a 20 26 26 20 6e  );.  if( !z && n
1a039 42 79 74 65 3e 30 20 29 7b 0a 20 20 20 20 73 71  Byte>0 ){.    sq
1a03a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1a03b 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74  or_nomem(context
1a03c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1a03d 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  z;.}../*.** Impl
1a03e 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
1a03f 65 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c 6f  e upper() and lo
1a040 77 65 72 28 29 20 53 51 4c 20 66 75 6e 63 74 69  wer() SQL functi
1a041 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ons..*/.static v
1a042 6f 69 64 20 75 70 70 65 72 46 75 6e 63 28 73 71  oid upperFunc(sq
1a043 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
1a044 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
1a045 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
1a046 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20  **argv){.  char 
1a047 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  *z1;.  const cha
1a048 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20  r *z2;.  int i, 
1a049 6e 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20  n;.  if( argc<1 
1a04a 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d  || SQLITE_NULL==
1a04b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
1a04c 70 65 28 61 72 67 76 5b 30 5d 29 20 29 20 72 65  pe(argv[0]) ) re
1a04d 74 75 72 6e 3b 0a 20 20 7a 32 20 3d 20 28 63 68  turn;.  z2 = (ch
1a04e 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
1a04f 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
1a050 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
1a051 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
1a052 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79  0]);.  /* Verify
1a053 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74   that the call t
1a054 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20  o _bytes() does 
1a055 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74  not invalidate t
1a056 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74  he _text() point
1a057 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  er */.  assert( 
1a058 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74  z2==(char*)sqlit
1a059 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1a05a 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  gv[0]) );.  if( 
1a05b 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63  z2 ){.    z1 = c
1a05c 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
1a05d 74 65 78 74 2c 20 6e 2b 31 29 3b 0a 20 20 20 20  text, n+1);.    
1a05e 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20  if( z1 ){.      
1a05f 6d 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e  memcpy(z1, z2, n
1a060 2b 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  +1);.      for(i
1a061 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b  =0; z1[i]; i++){
1a062 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d  .        z1[i] =
1a063 20 74 6f 75 70 70 65 72 28 7a 31 5b 69 5d 29 3b   toupper(z1[i]);
1a064 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1a065 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
1a066 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20  xt(context, z1, 
1a067 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65  -1, sqlite3_free
1a068 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73  );.    }.  }.}.s
1a069 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72  tatic void lower
1a06a 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Func(sqlite3_con
1a06b 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
1a06c 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
1a06d 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a  _value **argv){.
1a06e 20 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f    char *z1;.  co
1a06f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20  nst char *z2;.  
1a070 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20  int i, n;.  if( 
1a071 61 72 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45  argc<1 || SQLITE
1a072 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76  _NULL==sqlite3_v
1a073 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
1a074 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a  ]) ) return;.  z
1a075 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  2 = (char*)sqlit
1a076 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1a077 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71  gv[0]);.  n = sq
1a078 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1a079 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a  s(argv[0]);.  /*
1a07a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
1a07b 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28   call to _bytes(
1a07c 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c  ) does not inval
1a07d 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28  idate the _text(
1a07e 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61  ) pointer */.  a
1a07f 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72  ssert( z2==(char
1a080 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
1a081 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b  text(argv[0]) );
1a082 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20  .  if( z2 ){.   
1a083 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c   z1 = contextMal
1a084 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 2b 31  loc(context, n+1
1a085 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b  );.    if( z1 ){
1a086 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31  .      memcpy(z1
1a087 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20  , z2, n+1);.    
1a088 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d    for(i=0; z1[i]
1a089 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
1a08a 7a 31 5b 69 5d 20 3d 20 74 6f 6c 6f 77 65 72 28  z1[i] = tolower(
1a08b 7a 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  z1[i]);.      }.
1a08c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
1a08d 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
1a08e 74 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74  t, z1, -1, sqlit
1a08f 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a  e3_free);.    }.
1a090 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
1a091 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
1a092 68 65 20 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c  he IFNULL(), NVL
1a093 28 29 2c 20 61 6e 64 20 43 4f 41 4c 45 53 43 45  (), and COALESCE
1a094 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a  () functions.  .
1a095 2a 2a 20 41 6c 6c 20 74 68 72 65 65 20 64 6f 20  ** All three do 
1a096 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e 20  the same thing. 
1a097 20 54 68 65 79 20 72 65 74 75 72 6e 20 74 68 65   They return the
1a098 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a   first non-NULL.
1a099 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  ** argument..*/.
1a09a 73 74 61 74 69 63 20 76 6f 69 64 20 69 66 6e 75  static void ifnu
1a09b 6c 6c 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  llFunc(.  sqlite
1a09c 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1a09d 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
1a09e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1a09f 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
1a0a0 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
1a0a1 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  argc; i++){.    
1a0a2 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21  if( SQLITE_NULL!
1a0a3 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
1a0a4 79 70 65 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a  ype(argv[i]) ){.
1a0a5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
1a0a6 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
1a0a7 78 74 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20  xt, argv[i]);.  
1a0a8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1a0a9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
1a0aa 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1a0ab 72 61 6e 64 6f 6d 28 29 2e 20 20 52 65 74 75 72  random().  Retur
1a0ac 6e 20 61 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67  n a random integ
1a0ad 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20  er.  .*/.static 
1a0ae 76 6f 69 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28  void randomFunc(
1a0af 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1a0b0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
1a0b1 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
1a0b2 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
1a0b3 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  ){.  sqlite_int6
1a0b4 34 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 52 61  4 r;.  sqlite3Ra
1a0b5 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28  ndomness(sizeof(
1a0b6 72 29 2c 20 26 72 29 3b 0a 20 20 69 66 28 20 28  r), &r);.  if( (
1a0b7 72 3c 3c 31 29 3d 3d 30 20 29 20 72 20 3d 20 30  r<<1)==0 ) r = 0
1a0b8 3b 20 20 2f 2a 20 50 72 65 76 65 6e 74 20 30 78  ;  /* Prevent 0x
1a0b9 38 30 30 30 2e 2e 2e 2e 20 61 73 20 74 68 65 20  8000.... as the 
1a0ba 72 65 73 75 6c 74 20 73 6f 20 74 68 61 74 20 77  result so that w
1a0bb 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  e */.           
1a0bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a0bd 2a 20 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20  * can always do 
1a0be 61 62 73 28 29 20 6f 66 20 74 68 65 20 72 65 73  abs() of the res
1a0bf 75 6c 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ult */.  sqlite3
1a0c0 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f  _result_int64(co
1a0c1 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a  ntext, r);.}../*
1a0c2 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
1a0c3 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62  on of randomblob
1a0c4 28 4e 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72  (N).  Return a r
1a0c5 61 6e 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68  andom blob.** th
1a0c6 61 74 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f  at is N bytes lo
1a0c7 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
1a0c8 69 64 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20  id randomBlob(. 
1a0c9 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1a0ca 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
1a0cb 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
1a0cc 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
1a0cd 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69  .  int n;.  unsi
1a0ce 67 6e 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20  gned char *p;.  
1a0cf 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
1a0d0 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  );.  n = sqlite3
1a0d1 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
1a0d2 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29  0]);.  if( n<1 )
1a0d3 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d  {.    n = 1;.  }
1a0d4 0a 20 20 69 66 28 20 6e 3e 53 51 4c 49 54 45 5f  .  if( n>SQLITE_
1a0d5 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20  MAX_LENGTH ){.  
1a0d6 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1a0d7 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f  _error_toobig(co
1a0d8 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74 75  ntext);.    retu
1a0d9 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 63 6f  rn;.  }.  p = co
1a0da 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
1a0db 65 78 74 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70  ext, n);.  if( p
1a0dc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52   ){.    sqlite3R
1a0dd 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20 70 29 3b  andomness(n, p);
1a0de 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
1a0df 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  ult_blob(context
1a0e0 2c 20 28 63 68 61 72 2a 29 70 2c 20 6e 2c 20 73  , (char*)p, n, s
1a0e1 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
1a0e2 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
1a0e3 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
1a0e4 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77   last_insert_row
1a0e5 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  id() SQL functio
1a0e6 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a 2a  n.  The return.*
1a0e7 2a 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73  * value is the s
1a0e8 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74  ame as the sqlit
1a0e9 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
1a0ea 6f 77 69 64 28 29 20 41 50 49 20 66 75 6e 63 74  owid() API funct
1a0eb 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
1a0ec 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f  oid last_insert_
1a0ed 72 6f 77 69 64 28 0a 20 20 73 71 6c 69 74 65 33  rowid(.  sqlite3
1a0ee 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
1a0ef 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 2c 20 0a  t, .  int arg, .
1a0f0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1a0f1 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69  **argv.){.  sqli
1a0f2 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
1a0f3 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74  3_user_data(cont
1a0f4 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ext);.  sqlite3_
1a0f5 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e  result_int64(con
1a0f6 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 6c 61  text, sqlite3_la
1a0f7 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
1a0f8 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  db));.}../*.** I
1a0f9 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
1a0fa 20 74 68 65 20 63 68 61 6e 67 65 73 28 29 20 53   the changes() S
1a0fb 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
1a0fc 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
1a0fd 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73  s the.** same as
1a0fe 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61   the sqlite3_cha
1a0ff 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74  nges() API funct
1a100 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
1a101 6f 69 64 20 63 68 61 6e 67 65 73 28 0a 20 20 73  oid changes(.  s
1a102 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a103 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
1a104 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  rg,.  sqlite3_va
1a105 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
1a106 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
1a107 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
1a108 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69  context);.  sqli
1a109 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
1a10a 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
1a10b 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a  changes(db));.}.
1a10c 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
1a10d 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f 74  ation of the tot
1a10e 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 53 51 4c  al_changes() SQL
1a10f 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
1a110 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a  return value is.
1a111 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  ** the same as t
1a112 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  he sqlite3_total
1a113 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49 20 66  _changes() API f
1a114 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
1a115 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 5f 63 68  ic void total_ch
1a116 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65 33  anges(.  sqlite3
1a117 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
1a118 74 2c 0a 20 20 69 6e 74 20 61 72 67 2c 0a 20 20  t,.  int arg,.  
1a119 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
1a11a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65  argv.){.  sqlite
1a11b 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
1a11c 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
1a11d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  t);.  sqlite3_re
1a11e 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
1a11f 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  , sqlite3_total_
1a120 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a  changes(db));.}.
1a121 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75  ./*.** A structu
1a122 72 65 20 64 65 66 69 6e 69 6e 67 20 68 6f 77 20  re defining how 
1a123 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 79 6c 65  to do GLOB-style
1a124 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f   comparisons..*/
1a125 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49  .struct compareI
1a126 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 74 63 68  nfo {.  u8 match
1a127 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f  All;.  u8 matchO
1a128 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65  ne;.  u8 matchSe
1a129 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 3b 0a  t;.  u8 noCase;.
1a12a 7d 3b 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  };..static const
1a12b 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49   struct compareI
1a12c 6e 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b  nfo globInfo = {
1a12d 20 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27 2c 20   '*', '?', '[', 
1a12e 30 20 7d 3b 0a 2f 2a 20 54 68 65 20 63 6f 72 72  0 };./* The corr
1a12f 65 63 74 20 53 51 4c 2d 39 32 20 62 65 68 61 76  ect SQL-92 behav
1a130 69 6f 72 20 69 73 20 66 6f 72 20 74 68 65 20 4c  ior is for the L
1a131 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  IKE operator to 
1a132 69 67 6e 6f 72 65 0a 2a 2a 20 63 61 73 65 2e 20  ignore.** case. 
1a133 20 54 68 75 73 20 20 27 61 27 20 4c 49 4b 45 20   Thus  'a' LIKE 
1a134 27 41 27 20 77 6f 75 6c 64 20 62 65 20 74 72 75  'A' would be tru
1a135 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  e. */.static con
1a136 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72  st struct compar
1a137 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 4e 6f  eInfo likeInfoNo
1a138 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c  rm = { '%', '_',
1a139 20 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a 20 49 66     0, 1 };./* If
1a13a 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e   SQLITE_CASE_SEN
1a13b 53 49 54 49 56 45 5f 4c 49 4b 45 20 69 73 20 64  SITIVE_LIKE is d
1a13c 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
1a13d 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 0a 2a   LIKE operator.*
1a13e 2a 20 69 73 20 63 61 73 65 20 73 65 6e 73 69 74  * is case sensit
1a13f 69 76 65 20 63 61 75 73 69 6e 67 20 27 61 27 20  ive causing 'a' 
1a140 4c 49 4b 45 20 27 41 27 20 74 6f 20 62 65 20 66  LIKE 'A' to be f
1a141 61 6c 73 65 20 2a 2f 0a 73 74 61 74 69 63 20 63  alse */.static c
1a142 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70  onst struct comp
1a143 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f  areInfo likeInfo
1a144 41 6c 74 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27  Alt = { '%', '_'
1a145 2c 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 0a  ,   0, 0 };../*.
1a146 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20 55  ** Compare two U
1a147 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f 72  TF-8 strings for
1a148 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65 20   equality where 
1a149 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
1a14a 20 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e 74 69 61   can.** potentia
1a14b 6c 6c 79 20 62 65 20 61 20 22 67 6c 6f 62 22 20  lly be a "glob" 
1a14c 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 52 65 74  expression.  Ret
1a14d 75 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20  urn true (1) if 
1a14e 74 68 65 79 0a 2a 2a 20 61 72 65 20 74 68 65 20  they.** are the 
1a14f 73 61 6d 65 20 61 6e 64 20 66 61 6c 73 65 20 28  same and false (
1a150 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20 64  0) if they are d
1a151 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ifferent..**.** 
1a152 47 6c 6f 62 62 69 6e 67 20 72 75 6c 65 73 3a 0a  Globbing rules:.
1a153 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 2a 27 20 20  **.**      '*'  
1a154 20 20 20 20 20 4d 61 74 63 68 65 73 20 61 6e 79       Matches any
1a155 20 73 65 71 75 65 6e 63 65 20 6f 66 20 7a 65 72   sequence of zer
1a156 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61 72 61 63  o or more charac
1a157 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ters..**.**     
1a158 20 27 3f 27 20 20 20 20 20 20 20 4d 61 74 63 68   '?'       Match
1a159 65 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63  es exactly one c
1a15a 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  haracter..**.** 
1a15b 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20 20 4d      [...]      M
1a15c 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61  atches one chara
1a15d 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 65 6e  cter from the en
1a15e 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66 0a 2a  closed list of.*
1a15f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
1a160 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
1a161 2a 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d 20 20 20  **     [^...]   
1a162 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68    Matches one ch
1a163 61 72 61 63 74 65 72 20 6e 6f 74 20 69 6e 20 74  aracter not in t
1a164 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74  he enclosed list
1a165 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65  ..**.** With the
1a166 20 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e 2e 2e 2e   [...] and [^...
1a167 5d 20 6d 61 74 63 68 69 6e 67 2c 20 61 20 27 5d  ] matching, a ']
1a168 27 20 63 68 61 72 61 63 74 65 72 20 63 61 6e 20  ' character can 
1a169 62 65 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69  be included.** i
1a16a 6e 20 74 68 65 20 6c 69 73 74 20 62 79 20 6d 61  n the list by ma
1a16b 6b 69 6e 67 20 69 74 20 74 68 65 20 66 69 72 73  king it the firs
1a16c 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74 65  t character afte
1a16d 72 20 27 5b 27 20 6f 72 20 27 5e 27 2e 20 20 41  r '[' or '^'.  A
1a16e 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 63 68 61  .** range of cha
1a16f 72 61 63 74 65 72 73 20 63 61 6e 20 62 65 20 73  racters can be s
1a170 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 27  pecified using '
1a171 2d 27 2e 20 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a  -'.  Example:.**
1a172 20 22 5b 61 2d 7a 5d 22 20 6d 61 74 63 68 65 73   "[a-z]" matches
1a173 20 61 6e 79 20 73 69 6e 67 6c 65 20 6c 6f 77 65   any single lowe
1a174 72 2d 63 61 73 65 20 6c 65 74 74 65 72 2e 20 20  r-case letter.  
1a175 54 6f 20 6d 61 74 63 68 20 61 20 27 2d 27 2c 20  To match a '-', 
1a176 6d 61 6b 65 0a 2a 2a 20 69 74 20 74 68 65 20 6c  make.** it the l
1a177 61 73 74 20 63 68 61 72 61 63 74 65 72 20 69 6e  ast character in
1a178 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a   the list..**.**
1a179 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1a17a 20 75 73 75 61 6c 6c 79 20 71 75 69 63 6b 2c 20   usually quick, 
1a17b 62 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a 32 20  but can be N**2 
1a17c 69 6e 20 74 68 65 20 77 6f 72 73 74 20 63 61 73  in the worst cas
1a17d 65 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74 73 3a 20  e..**.** Hints: 
1a17e 74 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f 72 20  to match '*' or 
1a17f 27 3f 27 2c 20 70 75 74 20 74 68 65 6d 20 69 6e  '?', put them in
1a180 20 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74 68 69   "[]".  Like thi
1a181 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
1a182 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20 20 20   abc[*]xyz      
1a183 20 20 4d 61 74 63 68 65 73 20 22 61 62 63 2a 78    Matches "abc*x
1a184 79 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73 74 61 74  yz" only.*/.stat
1a185 69 63 20 69 6e 74 20 70 61 74 74 65 72 6e 43 6f  ic int patternCo
1a186 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20 75  mpare(.  const u
1a187 38 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20 20  8 *zPattern,    
1a188 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1a189 20 67 6c 6f 62 20 70 61 74 74 65 72 6e 20 2a 2f   glob pattern */
1a18a 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 74  .  const u8 *zSt
1a18b 72 69 6e 67 2c 20 20 20 20 20 20 20 20 20 20 20  ring,           
1a18c 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e      /* The strin
1a18d 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61  g to compare aga
1a18e 69 6e 73 74 20 74 68 65 20 67 6c 6f 62 20 2a 2f  inst the glob */
1a18f 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  .  const struct 
1a190 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e  compareInfo *pIn
1a191 66 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69  fo, /* Informati
1a192 6f 6e 20 61 62 6f 75 74 20 68 6f 77 20 74 6f 20  on about how to 
1a193 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 65 20 2a  do the compare *
1a194 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 65 73  /.  const int es
1a195 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
1a196 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61       /* The esca
1a197 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  pe character */.
1a198 29 7b 0a 20 20 69 6e 74 20 63 2c 20 63 32 3b 0a  ){.  int c, c2;.
1a199 20 20 69 6e 74 20 69 6e 76 65 72 74 3b 0a 20 20    int invert;.  
1a19a 69 6e 74 20 73 65 65 6e 3b 0a 20 20 75 38 20 6d  int seen;.  u8 m
1a19b 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d  atchOne = pInfo-
1a19c 3e 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38 20  >matchOne;.  u8 
1a19d 6d 61 74 63 68 41 6c 6c 20 3d 20 70 49 6e 66 6f  matchAll = pInfo
1a19e 2d 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75 38  ->matchAll;.  u8
1a19f 20 6d 61 74 63 68 53 65 74 20 3d 20 70 49 6e 66   matchSet = pInf
1a1a0 6f 2d 3e 6d 61 74 63 68 53 65 74 3b 0a 20 20 75  o->matchSet;.  u
1a1a1 38 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e 66 6f  8 noCase = pInfo
1a1a2 2d 3e 6e 6f 43 61 73 65 3b 20 0a 20 20 69 6e 74  ->noCase; .  int
1a1a3 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b   prevEscape = 0;
1a1a4 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1a1a5 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 61  the previous cha
1a1a6 72 61 63 74 65 72 20 77 61 73 20 27 65 73 63 61  racter was 'esca
1a1a7 70 65 27 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28  pe' */..  while(
1a1a8 20 28 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66   (c = sqlite3Utf
1a1a9 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 30  8Read(zPattern,0
1a1aa 2c 26 7a 50 61 74 74 65 72 6e 29 29 21 3d 30 20  ,&zPattern))!=0 
1a1ab 29 7b 0a 20 20 20 20 69 66 28 20 21 70 72 65 76  ){.    if( !prev
1a1ac 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61 74  Escape && c==mat
1a1ad 63 68 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20 77  chAll ){.      w
1a1ae 68 69 6c 65 28 20 28 63 3d 73 71 6c 69 74 65 33  hile( (c=sqlite3
1a1af 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
1a1b0 6e 2c 30 2c 26 7a 50 61 74 74 65 72 6e 29 29 20  n,0,&zPattern)) 
1a1b1 3d 3d 20 6d 61 74 63 68 41 6c 6c 0a 20 20 20 20  == matchAll.    
1a1b2 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 63 20             || c 
1a1b3 3d 3d 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20  == matchOne ){. 
1a1b4 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 6d 61         if( c==ma
1a1b5 74 63 68 4f 6e 65 20 26 26 20 73 71 6c 69 74 65  tchOne && sqlite
1a1b6 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e  3Utf8Read(zStrin
1a1b7 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3d  g, 0, &zString)=
1a1b8 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1a1b9 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
1a1ba 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1a1bb 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20    if( c==0 ){.  
1a1bc 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1a1bd 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1a1be 63 3d 3d 65 73 63 20 29 7b 0a 20 20 20 20 20 20  c==esc ){.      
1a1bf 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66    c = sqlite3Utf
1a1c0 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20  8Read(zPattern, 
1a1c1 30 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  0, &zPattern);. 
1a1c2 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20         if( c==0 
1a1c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
1a1c4 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  urn 0;.        }
1a1c5 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1a1c6 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a   c==matchSet ){.
1a1c7 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1a1c8 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 20 20 20  esc==0 );       
1a1c9 20 20 2f 2a 20 54 68 69 73 20 69 73 20 47 4c 4f    /* This is GLO
1a1ca 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20  B, not LIKE */. 
1a1cb 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d         assert( m
1a1cc 61 74 63 68 53 65 74 3c 30 78 38 30 20 29 3b 20  atchSet<0x80 ); 
1a1cd 20 2f 2a 20 27 5b 27 20 69 73 20 61 20 73 69 6e   /* '[' is a sin
1a1ce 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 74  gle-byte charact
1a1cf 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68  er */.        wh
1a1d0 69 6c 65 28 20 2a 7a 53 74 72 69 6e 67 20 26 26  ile( *zString &&
1a1d1 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28   patternCompare(
1a1d2 26 7a 50 61 74 74 65 72 6e 5b 2d 31 5d 2c 7a 53  &zPattern[-1],zS
1a1d3 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63 29  tring,pInfo,esc)
1a1d4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1a1d5 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
1a1d6 38 28 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20  8(zString);.    
1a1d7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
1a1d8 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 21 3d 30  turn *zString!=0
1a1d9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1a1da 77 68 69 6c 65 28 20 28 63 32 20 3d 20 73 71 6c  while( (c2 = sql
1a1db 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74  ite3Utf8Read(zSt
1a1dc 72 69 6e 67 2c 30 2c 26 7a 53 74 72 69 6e 67 29  ring,0,&zString)
1a1dd 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
1a1de 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20  if( noCase ){.  
1a1df 20 20 20 20 20 20 20 20 63 32 20 3d 20 63 32 3c          c2 = c2<
1a1e0 30 78 38 30 20 3f 20 73 71 6c 69 74 65 33 55 70  0x80 ? sqlite3Up
1a1e1 70 65 72 54 6f 4c 6f 77 65 72 5b 63 32 5d 20 3a  perToLower[c2] :
1a1e2 20 63 32 3b 0a 20 20 20 20 20 20 20 20 20 20 63   c2;.          c
1a1e3 20 3d 20 63 3c 30 78 38 30 20 3f 20 73 71 6c 69   = c<0x80 ? sqli
1a1e4 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
1a1e5 63 5d 20 3a 20 63 3b 0a 20 20 20 20 20 20 20 20  c] : c;.        
1a1e6 20 20 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30    while( c2 != 0
1a1e7 20 26 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20   && c2 != c ){. 
1a1e8 20 20 20 20 20 20 20 20 20 20 20 63 32 20 3d 20             c2 = 
1a1e9 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
1a1ea 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74  zString, 0, &zSt
1a1eb 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20  ring);.         
1a1ec 20 20 20 69 66 28 20 63 32 3c 30 78 38 30 20 29     if( c2<0x80 )
1a1ed 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 70 70   c2 = sqlite3Upp
1a1ee 65 72 54 6f 4c 6f 77 65 72 5b 63 32 5d 3b 0a 20  erToLower[c2];. 
1a1ef 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1a1f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a1f1 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 21 3d      while( c2 !=
1a1f2 20 30 20 26 26 20 63 32 20 21 3d 20 63 20 29 7b   0 && c2 != c ){
1a1f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 32 20  .            c2 
1a1f4 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
1a1f5 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a  d(zString, 0, &z
1a1f6 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20  String);.       
1a1f7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1a1f8 20 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30         if( c2==0
1a1f9 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
1a1fa 20 20 20 20 20 69 66 28 20 70 61 74 74 65 72 6e       if( pattern
1a1fb 43 6f 6d 70 61 72 65 28 7a 50 61 74 74 65 72 6e  Compare(zPattern
1a1fc 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65  ,zString,pInfo,e
1a1fd 73 63 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  sc) ) return 1;.
1a1fe 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
1a1ff 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73  turn 0;.    }els
1a200 65 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70  e if( !prevEscap
1a201 65 20 26 26 20 63 3d 3d 6d 61 74 63 68 4f 6e 65  e && c==matchOne
1a202 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71   ){.      if( sq
1a203 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
1a204 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69  tring, 0, &zStri
1a205 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ng)==0 ){.      
1a206 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
1a207 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
1a208 28 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b  ( c==matchSet ){
1a209 0a 20 20 20 20 20 20 69 6e 74 20 70 72 69 6f 72  .      int prior
1a20a 5f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73  _c = 0;.      as
1a20b 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b 20  sert( esc==0 ); 
1a20c 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20     /* This only 
1a20d 6f 63 63 75 72 73 20 66 6f 72 20 47 4c 4f 42 2c  occurs for GLOB,
1a20e 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20   not LIKE */.   
1a20f 20 20 20 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20     seen = 0;.   
1a210 20 20 20 69 6e 76 65 72 74 20 3d 20 30 3b 0a 20     invert = 0;. 
1a211 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33       c = sqlite3
1a212 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67  Utf8Read(zString
1a213 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a  , 0, &zString);.
1a214 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29        if( c==0 )
1a215 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
1a216 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66   c2 = sqlite3Utf
1a217 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20  8Read(zPattern, 
1a218 30 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  0, &zPattern);. 
1a219 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5e 27       if( c2=='^'
1a21a 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 65   ){.        inve
1a21b 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  rt = 1;.        
1a21c 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
1a21d 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30  Read(zPattern, 0
1a21e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20  , &zPattern);.  
1a21f 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1a220 63 32 3d 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20  c2==']' ){.     
1a221 20 20 20 69 66 28 20 63 3d 3d 27 5d 27 20 29 20     if( c==']' ) 
1a222 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20  seen = 1;.      
1a223 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
1a224 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c  f8Read(zPattern,
1a225 20 30 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a   0, &zPattern);.
1a226 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68        }.      wh
1a227 69 6c 65 28 20 63 32 20 26 26 20 63 32 21 3d 27  ile( c2 && c2!='
1a228 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ]' ){.        if
1a229 28 20 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50 61  ( c2=='-' && zPa
1a22a 74 74 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 26  ttern[0]!=']' &&
1a22b 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30 20   zPattern[0]!=0 
1a22c 26 26 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b 0a  && prior_c>0 ){.
1a22d 20 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73            c2 = s
1a22e 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
1a22f 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61  Pattern, 0, &zPa
1a230 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20  ttern);.        
1a231 20 20 69 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63    if( c>=prior_c
1a232 20 26 26 20 63 3c 3d 63 32 20 29 20 73 65 65 6e   && c<=c2 ) seen
1a233 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
1a234 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20  prior_c = 0;.   
1a235 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1a236 20 20 20 20 20 20 69 66 28 20 63 3d 3d 63 32 20        if( c==c2 
1a237 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
1a238 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  een = 1;.       
1a239 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
1a23a 72 69 6f 72 5f 63 20 3d 20 63 32 3b 0a 20 20 20  rior_c = c2;.   
1a23b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
1a23c 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
1a23d 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30 2c  ead(zPattern, 0,
1a23e 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20   &zPattern);.   
1a23f 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63     }.      if( c
1a240 32 3d 3d 30 20 7c 7c 20 28 73 65 65 6e 20 5e 20  2==0 || (seen ^ 
1a241 69 6e 76 65 72 74 29 3d 3d 30 20 29 7b 0a 20 20  invert)==0 ){.  
1a242 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1a243 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
1a244 65 20 69 66 28 20 65 73 63 3d 3d 63 20 26 26 20  e if( esc==c && 
1a245 21 70 72 65 76 45 73 63 61 70 65 20 29 7b 0a 20  !prevEscape ){. 
1a246 20 20 20 20 20 70 72 65 76 45 73 63 61 70 65 20       prevEscape 
1a247 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 1;.    }else{.
1a248 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
1a249 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69  e3Utf8Read(zStri
1a24a 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29  ng, 0, &zString)
1a24b 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61  ;.      if( noCa
1a24c 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20  se ){.        c 
1a24d 3d 20 63 3c 30 78 38 30 20 3f 20 73 71 6c 69 74  = c<0x80 ? sqlit
1a24e 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63  e3UpperToLower[c
1a24f 5d 20 3a 20 63 3b 0a 20 20 20 20 20 20 20 20 63  ] : c;.        c
1a250 32 20 3d 20 63 32 3c 30 78 38 30 20 3f 20 73 71  2 = c2<0x80 ? sq
1a251 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
1a252 72 5b 63 32 5d 20 3a 20 63 32 3b 0a 20 20 20 20  r[c2] : c2;.    
1a253 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 21    }.      if( c!
1a254 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20 20 72  =c2 ){.        r
1a255 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
1a256 0a 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70  .      prevEscap
1a257 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  e = 0;.    }.  }
1a258 0a 20 20 72 65 74 75 72 6e 20 2a 7a 53 74 72 69  .  return *zStri
1a259 6e 67 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ng==0;.}../*.** 
1a25a 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72  Count the number
1a25b 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74   of times that t
1a25c 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  he LIKE operator
1a25d 20 28 6f 72 20 47 4c 4f 42 20 77 68 69 63 68 20   (or GLOB which 
1a25e 69 73 0a 2a 2a 20 6a 75 73 74 20 61 20 76 61 72  is.** just a var
1a25f 69 61 74 69 6f 6e 20 6f 66 20 4c 49 4b 45 29 20  iation of LIKE) 
1a260 67 65 74 73 20 63 61 6c 6c 65 64 2e 20 20 54 68  gets called.  Th
1a261 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  is is used for t
1a262 65 73 74 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a  esting.** only..
1a263 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1a264 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
1a265 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 6b   int sqlite3_lik
1a266 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  e_count = 0;.#en
1a267 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  dif.../*.** Impl
1a268 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
1a269 65 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 6e  e like() SQL fun
1a26a 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e  ction.  This fun
1a26b 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73  ction implements
1a26c 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e  .** the build-in
1a26d 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20   LIKE operator. 
1a26e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
1a26f 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
1a270 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70 61  ion is the.** pa
1a271 74 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 65  ttern and the se
1a272 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
1a273 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 6f   the string.  So
1a274 2c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  , the SQL statem
1a275 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ents:.**.**     
1a276 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a    A LIKE B.**.**
1a277 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
1a278 61 73 20 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a  as like(B,A)..**
1a279 0a 2a 2a 20 54 68 69 73 20 73 61 6d 65 20 66 75  .** This same fu
1a27a 6e 63 74 69 6f 6e 20 28 77 69 74 68 20 61 20 64  nction (with a d
1a27b 69 66 66 65 72 65 6e 74 20 63 6f 6d 70 61 72 65  ifferent compare
1a27c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 29 20  Info structure) 
1a27d 63 6f 6d 70 75 74 65 73 0a 2a 2a 20 74 68 65 20  computes.** the 
1a27e 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  GLOB operator..*
1a27f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 69  /.static void li
1a280 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  keFunc(.  sqlite
1a281 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1a282 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c  xt, .  int argc,
1a283 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   .  sqlite3_valu
1a284 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
1a285 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1a286 72 20 2a 7a 41 2c 20 2a 7a 42 3b 0a 20 20 69 6e  r *zA, *zB;.  in
1a287 74 20 65 73 63 61 70 65 20 3d 20 30 3b 0a 0a 20  t escape = 0;.. 
1a288 20 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 61   zB = sqlite3_va
1a289 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
1a28a 29 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 65  );.  zA = sqlite
1a28b 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
1a28c 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d  v[1]);..  /* Lim
1a28d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  it the length of
1a28e 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f   the LIKE or GLO
1a28f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 6f  B pattern to avo
1a290 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a  id problems.  **
1a291 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 69   of deep recursi
1a292 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 76  on and N*N behav
1a293 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 6f  ior in patternCo
1a294 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 20  mpare()..  */.  
1a295 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
1a296 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
1a297 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45  >SQLITE_MAX_LIKE
1a298 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20  _PATTERN_LENGTH 
1a299 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
1a29a 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
1a29b 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47 4c  ext, "LIKE or GL
1a29c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20 63  OB pattern too c
1a29d 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20 20  omplex", -1);.  
1a29e 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1a29f 61 73 73 65 72 74 28 20 7a 42 3d 3d 73 71 6c 69  assert( zB==sqli
1a2a0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
1a2a1 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 45  rgv[0]) );  /* E
1a2a2 6e 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f 74 20  ncoding did not 
1a2a3 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 69 66 28  change */..  if(
1a2a4 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20   argc==3 ){.    
1a2a5 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 68  /* The escape ch
1a2a6 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 6d  aracter string m
1a2a7 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 61  ust consist of a
1a2a8 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68   single UTF-8 ch
1a2a9 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
1a2aa 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
1a2ab 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20  n an error..    
1a2ac 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  */.    const uns
1a2ad 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 73 63  igned char *zEsc
1a2ae 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1a2af 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a  _text(argv[2]);.
1a2b0 20 20 20 20 69 66 28 20 7a 45 73 63 3d 3d 30 20      if( zEsc==0 
1a2b1 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66  ) return;.    if
1a2b2 28 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61  ( sqlite3Utf8Cha
1a2b3 72 4c 65 6e 28 28 63 68 61 72 2a 29 7a 45 73 63  rLen((char*)zEsc
1a2b4 2c 20 2d 31 29 21 3d 31 20 29 7b 0a 20 20 20 20  , -1)!=1 ){.    
1a2b5 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1a2b6 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
1a2b7 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41  .          "ESCA
1a2b8 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75  PE expression mu
1a2b9 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63  st be a single c
1a2ba 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b 0a  haracter", -1);.
1a2bb 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
1a2bc 20 20 7d 0a 20 20 20 20 65 73 63 61 70 65 20 3d    }.    escape =
1a2bd 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
1a2be 28 7a 45 73 63 2c 20 30 2c 20 26 7a 45 73 63 29  (zEsc, 0, &zEsc)
1a2bf 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 41 20 26  ;.  }.  if( zA &
1a2c0 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 74 72 75  & zB ){.    stru
1a2c1 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a  ct compareInfo *
1a2c2 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 5f  pInfo = sqlite3_
1a2c3 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
1a2c4 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  t);.#ifdef SQLIT
1a2c5 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
1a2c6 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b  e3_like_count++;
1a2c7 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20 20  .#endif.    .   
1a2c8 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a2c9 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 61 74  int(context, pat
1a2ca 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 42 2c 20  ternCompare(zB, 
1a2cb 7a 41 2c 20 70 49 6e 66 6f 2c 20 65 73 63 61 70  zA, pInfo, escap
1a2cc 65 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  e));.  }.}../*.*
1a2cd 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
1a2ce 20 6f 66 20 74 68 65 20 4e 55 4c 4c 49 46 28 78   of the NULLIF(x
1a2cf 2c 79 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  ,y) function.  T
1a2d0 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65  he result is the
1a2d1 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65   first.** argume
1a2d2 6e 74 20 69 66 20 74 68 65 20 61 72 67 75 6d 65  nt if the argume
1a2d3 6e 74 73 20 61 72 65 20 64 69 66 66 65 72 65 6e  nts are differen
1a2d4 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69  t.  The result i
1a2d5 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 0a 2a 2a  s NULL if the.**
1a2d6 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 65   arguments are e
1a2d7 71 75 61 6c 20 74 6f 20 65 61 63 68 20 6f 74 68  qual to each oth
1a2d8 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
1a2d9 69 64 20 6e 75 6c 6c 69 66 46 75 6e 63 28 0a 20  id nullifFunc(. 
1a2da 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1a2db 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
1a2dc 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
1a2dd 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
1a2de 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
1a2df 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75  l = sqlite3GetFu
1a2e0 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78  ncCollSeq(contex
1a2e1 74 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  t);.  if( sqlite
1a2e2 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67 76  3MemCompare(argv
1a2e3 5b 30 5d 2c 20 61 72 67 76 5b 31 5d 2c 20 70 43  [0], argv[1], pC
1a2e4 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  oll)!=0 ){.    s
1a2e5 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
1a2e6 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  lue(context, arg
1a2e7 76 5b 30 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  v[0]);.  }.}../*
1a2e8 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
1a2e9 6f 6e 20 6f 66 20 74 68 65 20 56 45 52 53 49 4f  on of the VERSIO
1a2ea 4e 28 2a 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  N(*) function.  
1a2eb 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68  The result is th
1a2ec 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20  e version.** of 
1a2ed 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
1a2ee 72 79 20 74 68 61 74 20 69 73 20 72 75 6e 6e 69  ry that is runni
1a2ef 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
1a2f0 69 64 20 76 65 72 73 69 6f 6e 46 75 6e 63 28 0a  id versionFunc(.
1a2f1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1a2f2 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
1a2f3 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
1a2f4 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
1a2f5 7b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  {.  sqlite3_resu
1a2f6 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
1a2f7 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e   sqlite3_version
1a2f8 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
1a2f9 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 72 72 61  TIC);.}../* Arra
1a2fa 79 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67  y for converting
1a2fb 20 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74 65 73   from half-bytes
1a2fc 20 28 6e 79 62 62 6c 65 73 29 20 69 6e 74 6f 20   (nybbles) into 
1a2fd 41 53 43 49 49 20 68 65 78 0a 2a 2a 20 64 69 67  ASCII hex.** dig
1a2fe 69 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63  its. */.static c
1a2ff 6f 6e 73 74 20 63 68 61 72 20 68 65 78 64 69 67  onst char hexdig
1a300 69 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30 27 2c  its[] = {.  '0',
1a301 20 27 31 27 2c 20 27 32 27 2c 20 27 33 27 2c 20   '1', '2', '3', 
1a302 27 34 27 2c 20 27 35 27 2c 20 27 36 27 2c 20 27  '4', '5', '6', '
1a303 37 27 2c 0a 20 20 27 38 27 2c 20 27 39 27 2c 20  7',.  '8', '9', 
1a304 27 41 27 2c 20 27 42 27 2c 20 27 43 27 2c 20 27  'A', 'B', 'C', '
1a305 44 27 2c 20 27 45 27 2c 20 27 46 27 20 0a 7d 3b  D', 'E', 'F' .};
1a306 0a 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49 4d 45  ../*.** EXPERIME
1a307 4e 54 41 4c 20 2d 20 54 68 69 73 20 69 73 20 6e  NTAL - This is n
1a308 6f 74 20 61 6e 20 6f 66 66 69 63 69 61 6c 20 66  ot an official f
1a309 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 69 6e  unction.  The in
1a30a 74 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a 20 63  terface may.** c
1a30b 68 61 6e 67 65 2e 20 20 54 68 69 73 20 66 75 6e  hange.  This fun
1a30c 63 74 69 6f 6e 20 6d 61 79 20 64 69 73 61 70 70  ction may disapp
1a30d 65 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77 72 69  ear.  Do not wri
1a30e 74 65 20 63 6f 64 65 20 74 68 61 74 20 64 65 70  te code that dep
1a30f 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69 73 20  ends.** on this 
1a310 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
1a311 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
1a312 66 20 74 68 65 20 51 55 4f 54 45 28 29 20 66 75  f the QUOTE() fu
1a313 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75  nction.  This fu
1a314 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73  nction takes a s
1a315 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  ingle.** argumen
1a316 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d  t.  If the argum
1a317 65 6e 74 20 69 73 20 6e 75 6d 65 72 69 63 2c 20  ent is numeric, 
1a318 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
1a319 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 0a   is the same as.
1a31a 2a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e  ** the argument.
1a31b 20 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e    If the argumen
1a31c 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1a31d 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74  eturn value is t
1a31e 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 4e 55  he string.** "NU
1a31f 4c 4c 22 2e 20 20 4f 74 68 65 72 77 69 73 65 2c  LL".  Otherwise,
1a320 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
1a321 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73 69 6e   enclosed in sin
1a322 67 6c 65 20 71 75 6f 74 65 73 20 77 69 74 68 0a  gle quotes with.
1a323 2a 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 20  ** single-quote 
1a324 65 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74 61 74  escapes..*/.stat
1a325 69 63 20 76 6f 69 64 20 71 75 6f 74 65 46 75 6e  ic void quoteFun
1a326 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
1a327 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
1a328 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
1a329 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 69  lue **argv){.  i
1a32a 66 28 20 61 72 67 63 3c 31 20 29 20 72 65 74 75  f( argc<1 ) retu
1a32b 72 6e 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71  rn;.  switch( sq
1a32c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
1a32d 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20  (argv[0]) ){.   
1a32e 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c   case SQLITE_NUL
1a32f 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L: {.      sqlit
1a330 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
1a331 6f 6e 74 65 78 74 2c 20 22 4e 55 4c 4c 22 2c 20  ontext, "NULL", 
1a332 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  4, SQLITE_STATIC
1a333 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1a334 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
1a335 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20  QLITE_INTEGER:. 
1a336 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
1a337 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 71  LOAT: {.      sq
1a338 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
1a339 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76  ue(context, argv
1a33a 5b 30 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61  [0]);.      brea
1a33b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1a33c 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b  e SQLITE_BLOB: {
1a33d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 65  .      char *zTe
1a33e 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 68  xt = 0;.      ch
1a33f 61 72 20 63 6f 6e 73 74 20 2a 7a 42 6c 6f 62 20  ar const *zBlob 
1a340 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1a341 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20  blob(argv[0]);. 
1a342 20 20 20 20 20 69 6e 74 20 6e 42 6c 6f 62 20 3d       int nBlob =
1a343 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
1a344 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20  ytes(argv[0]);. 
1a345 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 42 6c       assert( zBl
1a346 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  ob==sqlite3_valu
1a347 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20  e_blob(argv[0]) 
1a348 29 3b 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e  ); /* No encodin
1a349 67 20 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 20  g change */..   
1a34a 20 20 20 69 66 28 20 32 2a 6e 42 6c 6f 62 2b 34     if( 2*nBlob+4
1a34b 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47  >SQLITE_MAX_LENG
1a34c 54 48 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  TH ){.        sq
1a34d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1a34e 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
1a34f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  t);.        retu
1a350 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
1a351 20 20 7a 54 65 78 74 20 3d 20 28 63 68 61 72 20    zText = (char 
1a352 2a 29 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28  *)contextMalloc(
1a353 63 6f 6e 74 65 78 74 2c 20 28 32 2a 6e 42 6c 6f  context, (2*nBlo
1a354 62 29 2b 34 29 3b 20 0a 20 20 20 20 20 20 69 66  b)+4); .      if
1a355 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20  ( zText ){.     
1a356 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
1a357 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c    for(i=0; i<nBl
1a358 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ob; i++){.      
1a359 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b      zText[(i*2)+
1a35a 32 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28  2] = hexdigits[(
1a35b 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30  zBlob[i]>>4)&0x0
1a35c 46 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54  F];.          zT
1a35d 65 78 74 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68  ext[(i*2)+3] = h
1a35e 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b  exdigits[(zBlob[
1a35f 69 5d 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 20  i])&0x0F];.     
1a360 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 54 65     }.        zTe
1a361 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20  xt[(nBlob*2)+2] 
1a362 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20  = '\'';.        
1a363 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b  zText[(nBlob*2)+
1a364 33 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  3] = '\0';.     
1a365 20 20 20 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58     zText[0] = 'X
1a366 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74  ';.        zText
1a367 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20  [1] = '\'';.    
1a368 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
1a369 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
1a36a 20 7a 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49   zText, -1, SQLI
1a36b 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
1a36c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
1a36d 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20 20  ree(zText);.    
1a36e 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1a36f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1a370 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20  SQLITE_TEXT: {. 
1a371 20 20 20 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20       int i,j;.  
1a372 20 20 20 20 75 36 34 20 6e 3b 0a 20 20 20 20 20      u64 n;.     
1a373 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1a374 63 68 61 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c  char *zArg = sql
1a375 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1a376 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
1a377 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20  char *z;..      
1a378 69 66 28 20 7a 41 72 67 3d 3d 30 20 29 20 72 65  if( zArg==0 ) re
1a379 74 75 72 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28  turn;.      for(
1a37a 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69  i=0, n=0; zArg[i
1a37b 5d 3b 20 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72  ]; i++){ if( zAr
1a37c 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b  g[i]=='\'' ) n++
1a37d 3b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 2b  ; }.      if( i+
1a37e 6e 2b 33 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  n+3>SQLITE_MAX_L
1a37f 45 4e 47 54 48 20 29 7b 0a 20 20 20 20 20 20 20  ENGTH ){.       
1a380 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a381 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e  error_toobig(con
1a382 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 72  text);.        r
1a383 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
1a384 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 78 74       z = context
1a385 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
1a386 69 2b 6e 2b 33 29 3b 0a 20 20 20 20 20 20 69 66  i+n+3);.      if
1a387 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ( z ){.        z
1a388 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20  [0] = '\'';.    
1a389 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31      for(i=0, j=1
1a38a 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b  ; zArg[i]; i++){
1a38b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b  .          z[j++
1a38c 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20  ] = zArg[i];.   
1a38d 20 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b         if( zArg[
1a38e 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20  i]=='\'' ){.    
1a38f 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
1a390 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20   '\'';.         
1a391 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1a392 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c       z[j++] = '\
1a393 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d  '';.        z[j]
1a394 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71   = 0;.        sq
1a395 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1a396 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c  t(context, z, j,
1a397 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
1a398 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1a399 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  }.}../*.** The h
1a39a 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ex() function.  
1a39b 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 61 72  Interpret the ar
1a39c 67 75 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62  gument as a blob
1a39d 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68  .  Return.** a h
1a39e 65 78 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65  exadecimal rende
1a39f 72 69 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f  ring as text..*/
1a3a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 78  .static void hex
1a3a1 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
1a3a2 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
1a3a3 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
1a3a4 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
1a3a5 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  argv.){.  int i,
1a3a6 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   n;.  const unsi
1a3a7 67 6e 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62  gned char *pBlob
1a3a8 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20  ;.  char *zHex, 
1a3a9 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  *z;.  assert( ar
1a3aa 67 63 3d 3d 31 20 29 3b 0a 20 20 70 42 6c 6f 62  gc==1 );.  pBlob
1a3ab 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1a3ac 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a  _blob(argv[0]);.
1a3ad 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    n = sqlite3_va
1a3ae 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
1a3af 5d 29 3b 0a 20 20 69 66 28 20 6e 2a 32 2b 31 3e  ]);.  if( n*2+1>
1a3b0 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
1a3b1 48 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  H ){.    sqlite3
1a3b2 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
1a3b3 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20  obig(context);. 
1a3b4 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
1a3b5 20 61 73 73 65 72 74 28 20 70 42 6c 6f 62 3d 3d   assert( pBlob==
1a3b6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
1a3b7 6f 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20  ob(argv[0]) );  
1a3b8 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63  /* No encoding c
1a3b9 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a  hange */.  z = z
1a3ba 48 65 78 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c  Hex = contextMal
1a3bb 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 2a 32  loc(context, n*2
1a3bc 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 7a 48 65   + 1);.  if( zHe
1a3bd 78 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  x ){.    for(i=0
1a3be 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 6f  ; i<n; i++, pBlo
1a3bf 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  b++){.      unsi
1a3c0 67 6e 65 64 20 63 68 61 72 20 63 20 3d 20 2a 70  gned char c = *p
1a3c1 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a 28 7a 2b  Blob;.      *(z+
1a3c2 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28  +) = hexdigits[(
1a3c3 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 20 20 20  c>>4)&0xf];.    
1a3c4 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69    *(z++) = hexdi
1a3c5 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a 20 20 20  gits[c&0xf];.   
1a3c6 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30 3b 0a 20   }.    *z = 0;. 
1a3c7 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
1a3c8 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
1a3c9 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 74  zHex, n*2, sqlit
1a3ca 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a  e3_free);.  }.}.
1a3cb 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62  ./*.** The zerob
1a3cc 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20  lob(N) function 
1a3cd 72 65 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66  returns a zero-f
1a3ce 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73 69  illed blob of si
1a3cf 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73  ze N bytes..*/.s
1a3d0 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 62  tatic void zerob
1a3d1 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  lobFunc(.  sqlit
1a3d2 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
1a3d3 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
1a3d4 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1a3d5 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 36 34   **argv.){.  i64
1a3d6 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72   n;.  assert( ar
1a3d7 67 63 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 73  gc==1 );.  n = s
1a3d8 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
1a3d9 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  64(argv[0]);.  i
1a3da 66 28 20 6e 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  f( n>SQLITE_MAX_
1a3db 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71  LENGTH ){.    sq
1a3dc 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1a3dd 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
1a3de 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
1a3df 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a3e0 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  zeroblob(context
1a3e1 2c 20 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  , n);.  }.}../*.
1a3e2 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65 28 29  ** The replace()
1a3e3 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65   function.  Thre
1a3e4 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  e arguments are 
1a3e5 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63 61 6c  all strings: cal
1a3e6 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20  l.** them A, B, 
1a3e7 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73 75 6c  and C. The resul
1a3e8 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74 72 69  t is also a stri
1a3e9 6e 67 20 77 68 69 63 68 20 69 73 20 64 65 72 69  ng which is deri
1a3ea 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79  ved.** from A by
1a3eb 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65 72 79   replacing every
1a3ec 20 6f 63 63 75 72 61 6e 63 65 20 6f 66 20 42 20   occurance of B 
1a3ed 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d 61 74  with C.  The mat
1a3ee 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 78  ch.** must be ex
1a3ef 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 20  act.  Collating 
1a3f0 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 6e 6f  sequences are no
1a3f1 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  t used..*/.stati
1a3f2 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65 46 75  c void replaceFu
1a3f3 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
1a3f4 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
1a3f5 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
1a3f6 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
1a3f7 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  gv.){.  const un
1a3f8 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74  signed char *zSt
1a3f9 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r;        /* The
1a3fa 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 41 20   input string A 
1a3fb 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  */.  const unsig
1a3fc 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74 74 65  ned char *zPatte
1a3fd 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 70 61  rn;    /* The pa
1a3fe 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42 20 2a  ttern string B *
1a3ff 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  /.  const unsign
1a400 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b 20 20  ed char *zRep;  
1a401 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 70        /* The rep
1a402 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e 67 20  lacement string 
1a403 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  C */.  unsigned 
1a404 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20  char *zOut;     
1a405 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1a406 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  output */.  int 
1a407 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20  nStr;           
1a408 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1a409 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50  zStr */.  int nP
1a40a 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20 20 20  attern;         
1a40b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 50     /* Size of zP
1a40c 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  attern */.  int 
1a40d 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20 20 20  nRep;           
1a40e 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1a40f 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20 6e 4f  zRep */.  i64 nO
1a410 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
1a411 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 69     /* Maximum si
1a412 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 20  ze of zOut */.  
1a413 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20  int loopLimit;  
1a414 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
1a415 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d 69 67   zStr[] that mig
1a416 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74 65 72  ht match zPatter
1a417 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  n[] */.  int i, 
1a418 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
1a419 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1a41a 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  rs */..  assert(
1a41b 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 7a 53   argc==3 );.  zS
1a41c 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  tr = sqlite3_val
1a41d 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
1a41e 3b 0a 20 20 69 66 28 20 7a 53 74 72 3d 3d 30 20  ;.  if( zStr==0 
1a41f 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 53 74 72  ) return;.  nStr
1a420 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1a421 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b  _bytes(argv[0]);
1a422 0a 20 20 61 73 73 65 72 74 28 20 7a 53 74 72 3d  .  assert( zStr=
1a423 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
1a424 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 20  ext(argv[0]) ); 
1a425 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20   /* No encoding 
1a426 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 50 61 74  change */.  zPat
1a427 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  tern = sqlite3_v
1a428 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
1a429 5d 29 3b 0a 20 20 69 66 28 20 7a 50 61 74 74 65  ]);.  if( zPatte
1a42a 72 6e 3d 3d 30 20 7c 7c 20 7a 50 61 74 74 65 72  rn==0 || zPatter
1a42b 6e 5b 30 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e  n[0]==0 ) return
1a42c 3b 0a 20 20 6e 50 61 74 74 65 72 6e 20 3d 20 73  ;.  nPattern = s
1a42d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
1a42e 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 61  es(argv[1]);.  a
1a42f 73 73 65 72 74 28 20 7a 50 61 74 74 65 72 6e 3d  ssert( zPattern=
1a430 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
1a431 65 78 74 28 61 72 67 76 5b 31 5d 29 20 29 3b 20  ext(argv[1]) ); 
1a432 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20   /* No encoding 
1a433 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 52 65 70  change */.  zRep
1a434 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1a435 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a  _text(argv[2]);.
1a436 20 20 69 66 28 20 7a 52 65 70 3d 3d 30 20 29 20    if( zRep==0 ) 
1a437 72 65 74 75 72 6e 3b 0a 20 20 6e 52 65 70 20 3d  return;.  nRep =
1a438 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
1a439 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20  ytes(argv[2]);. 
1a43a 20 61 73 73 65 72 74 28 20 7a 52 65 70 3d 3d 73   assert( zRep==s
1a43b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1a43c 74 28 61 72 67 76 5b 32 5d 29 20 29 3b 0a 20 20  t(argv[2]) );.  
1a43d 6e 4f 75 74 20 3d 20 6e 53 74 72 20 2b 20 31 3b  nOut = nStr + 1;
1a43e 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 75 74 3c  .  assert( nOut<
1a43f 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
1a440 48 20 29 3b 0a 20 20 7a 4f 75 74 20 3d 20 63 6f  H );.  zOut = co
1a441 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
1a442 65 78 74 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b  ext, (int)nOut);
1a443 0a 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29  .  if( zOut==0 )
1a444 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
1a445 7d 0a 20 20 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20  }.  loopLimit = 
1a446 6e 53 74 72 20 2d 20 6e 50 61 74 74 65 72 6e 3b  nStr - nPattern;
1a447 20 20 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20    .  for(i=j=0; 
1a448 69 3c 3d 6c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b  i<=loopLimit; i+
1a449 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 53 74 72  +){.    if( zStr
1a44a 5b 69 5d 21 3d 7a 50 61 74 74 65 72 6e 5b 30 5d  [i]!=zPattern[0]
1a44b 20 7c 7c 20 6d 65 6d 63 6d 70 28 26 7a 53 74 72   || memcmp(&zStr
1a44c 5b 69 5d 2c 20 7a 50 61 74 74 65 72 6e 2c 20 6e  [i], zPattern, n
1a44d 50 61 74 74 65 72 6e 29 20 29 7b 0a 20 20 20 20  Pattern) ){.    
1a44e 20 20 7a 4f 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53    zOut[j++] = zS
1a44f 74 72 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65  tr[i];.    }else
1a450 7b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 6c 64  {.      u8 *zOld
1a451 3b 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20  ;.      nOut += 
1a452 6e 52 65 70 20 2d 20 6e 50 61 74 74 65 72 6e 3b  nRep - nPattern;
1a453 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 75 74 3e  .      if( nOut>
1a454 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47  =SQLITE_MAX_LENG
1a455 54 48 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  TH ){.        sq
1a456 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1a457 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
1a458 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
1a459 74 65 33 5f 66 72 65 65 28 7a 4f 75 74 29 3b 0a  te3_free(zOut);.
1a45a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
1a45b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4f        }.      zO
1a45c 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 20 20  ld = zOut;.     
1a45d 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f   zOut = sqlite3_
1a45e 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c 20 28 69  realloc(zOut, (i
1a45f 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 20 20 20 20  nt)nOut);.      
1a460 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20  if( zOut==0 ){. 
1a461 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
1a462 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
1a463 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  m(context);.    
1a464 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1a465 28 7a 4f 6c 64 29 3b 0a 20 20 20 20 20 20 20 20  (zOld);.        
1a466 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
1a467 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 4f        memcpy(&zO
1a468 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c 20 6e 52 65  ut[j], zRep, nRe
1a469 70 29 3b 0a 20 20 20 20 20 20 6a 20 2b 3d 20 6e  p);.      j += n
1a46a 52 65 70 3b 0a 20 20 20 20 20 20 69 20 2b 3d 20  Rep;.      i += 
1a46b 6e 50 61 74 74 65 72 6e 2d 31 3b 0a 20 20 20 20  nPattern-1;.    
1a46c 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
1a46d 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d 6e 4f 75 74  j+nStr-i+1==nOut
1a46e 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 7a 4f   );.  memcpy(&zO
1a46f 75 74 5b 6a 5d 2c 20 26 7a 53 74 72 5b 69 5d 2c  ut[j], &zStr[i],
1a470 20 6e 53 74 72 2d 69 29 3b 0a 20 20 6a 20 2b 3d   nStr-i);.  j +=
1a471 20 6e 53 74 72 20 2d 20 69 3b 0a 20 20 61 73 73   nStr - i;.  ass
1a472 65 72 74 28 20 6a 3c 3d 6e 4f 75 74 20 29 3b 0a  ert( j<=nOut );.
1a473 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 30 3b 0a 20    zOut[j] = 0;. 
1a474 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a475 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63  text(context, (c
1a476 68 61 72 2a 29 7a 4f 75 74 2c 20 6a 2c 20 73 71  har*)zOut, j, sq
1a477 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a  lite3_free);.}..
1a478 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
1a479 74 69 6f 6e 20 6f 66 20 74 68 65 20 54 52 49 4d  tion of the TRIM
1a47a 28 29 2c 20 4c 54 52 49 4d 28 29 2c 20 61 6e 64  (), LTRIM(), and
1a47b 20 52 54 52 49 4d 28 29 20 66 75 6e 63 74 69 6f   RTRIM() functio
1a47c 6e 73 2e 0a 2a 2a 20 54 68 65 20 75 73 65 72 64  ns..** The userd
1a47d 61 74 61 20 69 73 20 30 78 31 20 66 6f 72 20 6c  ata is 0x1 for l
1a47e 65 66 74 20 74 72 69 6d 2c 20 30 78 32 20 66 6f  eft trim, 0x2 fo
1a47f 72 20 72 69 67 68 74 20 74 72 69 6d 2c 20 30 78  r right trim, 0x
1a480 33 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73  3 for both..*/.s
1a481 74 61 74 69 63 20 76 6f 69 64 20 74 72 69 6d 46  tatic void trimF
1a482 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
1a483 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
1a484 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
1a485 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
1a486 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75  rgv.){.  const u
1a487 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
1a488 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  n;         /* In
1a489 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
1a48a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1a48b 68 61 72 20 2a 7a 43 68 61 72 53 65 74 3b 20 20  har *zCharSet;  
1a48c 20 20 2f 2a 20 53 65 74 20 6f 66 20 63 68 61 72    /* Set of char
1a48d 61 63 74 65 72 73 20 74 6f 20 74 72 69 6d 20 2a  acters to trim *
1a48e 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b 20 20 20 20  /.  int nIn;    
1a48f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a490 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1a491 6f 66 20 62 79 74 65 73 20 69 6e 20 69 6e 70 75  of bytes in inpu
1a492 74 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  t */.  int flags
1a493 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a494 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 74           /* 1: t
1a495 72 69 6d 6c 65 66 74 20 20 32 3a 20 74 72 69 6d  rimleft  2: trim
1a496 72 69 67 68 74 20 20 33 3a 20 74 72 69 6d 20 2a  right  3: trim *
1a497 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
1a498 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a499 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1a49a 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67  unter */.  unsig
1a49b 6e 65 64 20 63 68 61 72 20 2a 61 4c 65 6e 3b 20  ned char *aLen; 
1a49c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a49d 4c 65 6e 67 74 68 20 6f 66 20 65 61 63 68 20 63  Length of each c
1a49e 68 61 72 61 63 74 65 72 20 69 6e 20 7a 43 68 61  haracter in zCha
1a49f 72 53 65 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rSet */.  const 
1a4a0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a  unsigned char **
1a4a1 61 7a 43 68 61 72 3b 20 20 20 20 20 2f 2a 20 49  azChar;     /* I
1a4a2 6e 64 69 76 69 64 75 61 6c 20 63 68 61 72 61 63  ndividual charac
1a4a3 74 65 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74  ters in zCharSet
1a4a4 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b   */.  int nChar;
1a4a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a4a6 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1a4a7 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
1a4a8 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 0a  in zCharSet */..
1a4a9 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
1a4aa 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
1a4ab 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )==SQLITE_NULL )
1a4ac 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
1a4ad 7d 0a 20 20 7a 49 6e 20 3d 20 73 71 6c 69 74 65  }.  zIn = sqlite
1a4ae 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
1a4af 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e  v[0]);.  if( zIn
1a4b0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1a4b1 6e 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  nIn = sqlite3_va
1a4b2 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
1a4b3 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 49  ]);.  assert( zI
1a4b4 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  n==sqlite3_value
1a4b5 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29  _text(argv[0]) )
1a4b6 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 31 20  ;.  if( argc==1 
1a4b7 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
1a4b8 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1a4b9 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d 20 7b 20 31  r lenOne[] = { 1
1a4ba 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63   };.    static c
1a4bb 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1a4bc 61 72 20 2a 61 7a 4f 6e 65 5b 5d 20 3d 20 7b 20  ar *azOne[] = { 
1a4bd 28 75 38 2a 29 22 20 22 20 7d 3b 0a 20 20 20 20  (u8*)" " };.    
1a4be 6e 43 68 61 72 20 3d 20 31 3b 0a 20 20 20 20 61  nChar = 1;.    a
1a4bf 4c 65 6e 20 3d 20 28 75 38 2a 29 6c 65 6e 4f 6e  Len = (u8*)lenOn
1a4c0 65 3b 0a 20 20 20 20 61 7a 43 68 61 72 20 3d 20  e;.    azChar = 
1a4c1 61 7a 4f 6e 65 3b 0a 20 20 20 20 7a 43 68 61 72  azOne;.    zChar
1a4c2 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Set = 0;.  }else
1a4c3 20 69 66 28 20 28 7a 43 68 61 72 53 65 74 20 3d   if( (zCharSet =
1a4c4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1a4c5 65 78 74 28 61 72 67 76 5b 31 5d 29 29 3d 3d 30  ext(argv[1]))==0
1a4c6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
1a4c7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e    }else{.    con
1a4c8 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1a4c9 20 2a 7a 3b 0a 20 20 20 20 66 6f 72 28 7a 3d 7a   *z;.    for(z=z
1a4ca 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d 30  CharSet, nChar=0
1a4cb 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a  ; *z; nChar++){.
1a4cc 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
1a4cd 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 7d  P_UTF8(z);.    }
1a4ce 0a 20 20 20 20 69 66 28 20 6e 43 68 61 72 3e 30  .    if( nChar>0
1a4cf 20 29 7b 0a 20 20 20 20 20 20 61 7a 43 68 61 72   ){.      azChar
1a4d0 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63   = contextMalloc
1a4d1 28 63 6f 6e 74 65 78 74 2c 20 6e 43 68 61 72 2a  (context, nChar*
1a4d2 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31  (sizeof(char*)+1
1a4d3 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 7a  ));.      if( az
1a4d4 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Char==0 ){.     
1a4d5 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
1a4d6 20 7d 0a 20 20 20 20 20 20 61 4c 65 6e 20 3d 20   }.      aLen = 
1a4d7 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
1a4d8 26 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a  &azChar[nChar];.
1a4d9 20 20 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61        for(z=zCha
1a4da 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a  rSet, nChar=0; *
1a4db 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20  z; nChar++){.   
1a4dc 20 20 20 20 20 61 7a 43 68 61 72 5b 6e 43 68 61       azChar[nCha
1a4dd 72 5d 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20  r] = z;.        
1a4de 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
1a4df 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 61 4c 65  (z);.        aLe
1a4e0 6e 5b 6e 43 68 61 72 5d 20 3d 20 7a 20 2d 20 61  n[nChar] = z - a
1a4e1 7a 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20  zChar[nChar];.  
1a4e2 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1a4e3 20 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b    if( nChar>0 ){
1a4e4 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 28 69 6e  .    flags = (in
1a4e5 74 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  t)sqlite3_user_d
1a4e6 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ata(context);.  
1a4e7 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 31 20    if( flags & 1 
1a4e8 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ){.      while( 
1a4e9 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  nIn>0 ){.       
1a4ea 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 20 20   int len;.      
1a4eb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68    for(i=0; i<nCh
1a4ec 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ar; i++){.      
1a4ed 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69      len = aLen[i
1a4ee 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
1a4ef 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 61 7a 43   memcmp(zIn, azC
1a4f0 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d 3d 30 20  har[i], len)==0 
1a4f1 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
1a4f2 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69   }.        if( i
1a4f3 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b 3b  >=nChar ) break;
1a4f4 0a 20 20 20 20 20 20 20 20 7a 49 6e 20 2b 3d 20  .        zIn += 
1a4f5 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e  len;.        nIn
1a4f6 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d   -= len;.      }
1a4f7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66  .    }.    if( f
1a4f8 6c 61 67 73 20 26 20 32 20 29 7b 0a 20 20 20 20  lags & 2 ){.    
1a4f9 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29    while( nIn>0 )
1a4fa 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 65  {.        int le
1a4fb 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  n;.        for(i
1a4fc 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b  =0; i<nChar; i++
1a4fd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  ){.          len
1a4fe 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20   = aLen[i];.    
1a4ff 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e        if( len<=n
1a500 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28 26 7a 49  In && memcmp(&zI
1a501 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61 7a 43 68 61  n[nIn-len],azCha
1a502 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30 20 29 20 62  r[i],len)==0 ) b
1a503 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
1a504 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 6e          if( i>=n
1a505 43 68 61 72 20 29 20 62 72 65 61 6b 3b 0a 20 20  Char ) break;.  
1a506 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e        nIn -= len
1a507 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1a508 20 20 20 20 69 66 28 20 7a 43 68 61 72 53 65 74      if( zCharSet
1a509 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1a50a 33 5f 66 72 65 65 28 61 7a 43 68 61 72 29 3b 0a  3_free(azChar);.
1a50b 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
1a50c 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
1a50d 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29  context, (char*)
1a50e 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51 4c 49 54 45  zIn, nIn, SQLITE
1a50f 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a  _TRANSIENT);.}..
1a510 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 4f  #ifdef SQLITE_SO
1a511 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  UNDEX./*.** Comp
1a512 75 74 65 20 74 68 65 20 73 6f 75 6e 64 65 78 20  ute the soundex 
1a513 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 77 6f  encoding of a wo
1a514 72 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rd..*/.static vo
1a515 69 64 20 73 6f 75 6e 64 65 78 46 75 6e 63 28 0a  id soundexFunc(.
1a516 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1a517 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
1a518 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
1a519 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
1a51a 7b 0a 20 20 63 68 61 72 20 7a 52 65 73 75 6c 74  {.  char zResult
1a51b 5b 38 5d 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20  [8];.  const u8 
1a51c 2a 7a 49 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  *zIn;.  int i, j
1a51d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
1a51e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69   unsigned char i
1a51f 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  Code[] = {.    0
1a520 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
1a521 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
1a522 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20  0, 0, 0, 0, 0,. 
1a523 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30     0, 0, 0, 0, 0
1a524 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
1a525 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
1a526 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  0,.    0, 0, 0, 
1a527 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
1a528 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
1a529 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c   0, 0,.    0, 0,
1a52a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
1a52b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
1a52c 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30  , 0, 0, 0,.    0
1a52d 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c  , 0, 1, 2, 3, 0,
1a52e 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20   1, 2, 0, 0, 2, 
1a52f 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20  2, 4, 5, 5, 0,. 
1a530 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33     1, 2, 6, 2, 3
1a531 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c  , 0, 1, 0, 2, 0,
1a532 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   2, 0, 0, 0, 0, 
1a533 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20  0,.    0, 0, 1, 
1a534 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30  2, 3, 0, 1, 2, 0
1a535 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c  , 0, 2, 2, 4, 5,
1a536 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c   5, 0,.    1, 2,
1a537 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20   6, 2, 3, 0, 1, 
1a538 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30  0, 2, 0, 2, 0, 0
1a539 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 7d 3b 0a  , 0, 0, 0,.  };.
1a53a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
1a53b 31 20 29 3b 0a 20 20 7a 49 6e 20 3d 20 28 75 38  1 );.  zIn = (u8
1a53c 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
1a53d 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
1a53e 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20 7a 49   if( zIn==0 ) zI
1a53f 6e 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 66  n = (u8*)"";.  f
1a540 6f 72 28 69 3d 30 3b 20 7a 49 6e 5b 69 5d 20 26  or(i=0; zIn[i] &
1a541 26 20 21 69 73 61 6c 70 68 61 28 7a 49 6e 5b 69  & !isalpha(zIn[i
1a542 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28  ]); i++){}.  if(
1a543 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75   zIn[i] ){.    u
1a544 38 20 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f  8 prevcode = iCo
1a545 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b  de[zIn[i]&0x7f];
1a546 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20  .    zResult[0] 
1a547 3d 20 74 6f 75 70 70 65 72 28 7a 49 6e 5b 69 5d  = toupper(zIn[i]
1a548 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20  );.    for(j=1; 
1a549 6a 3c 34 20 26 26 20 7a 49 6e 5b 69 5d 3b 20 69  j<4 && zIn[i]; i
1a54a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 63  ++){.      int c
1a54b 6f 64 65 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b  ode = iCode[zIn[
1a54c 69 5d 26 30 78 37 66 5d 3b 0a 20 20 20 20 20 20  i]&0x7f];.      
1a54d 69 66 28 20 63 6f 64 65 3e 30 20 29 7b 0a 20 20  if( code>0 ){.  
1a54e 20 20 20 20 20 20 69 66 28 20 63 6f 64 65 21 3d        if( code!=
1a54f 70 72 65 76 63 6f 64 65 20 29 7b 0a 20 20 20 20  prevcode ){.    
1a550 20 20 20 20 20 20 70 72 65 76 63 6f 64 65 20 3d        prevcode =
1a551 20 63 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 20   code;.         
1a552 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20   zResult[j++] = 
1a553 63 6f 64 65 20 2b 20 27 30 27 3b 0a 20 20 20 20  code + '0';.    
1a554 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
1a555 65 7b 0a 20 20 20 20 20 20 20 20 70 72 65 76 63  e{.        prevc
1a556 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ode = 0;.      }
1a557 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65  .    }.    while
1a558 28 20 6a 3c 34 20 29 7b 0a 20 20 20 20 20 20 7a  ( j<4 ){.      z
1a559 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 27 30  Result[j++] = '0
1a55a 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 52 65  ';.    }.    zRe
1a55b 73 75 6c 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20  sult[j] = 0;.   
1a55c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a55d 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52  text(context, zR
1a55e 65 73 75 6c 74 2c 20 34 2c 20 53 51 4c 49 54 45  esult, 4, SQLITE
1a55f 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
1a560 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
1a561 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
1a562 6e 74 65 78 74 2c 20 22 3f 30 30 30 22 2c 20 34  ntext, "?000", 4
1a563 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
1a564 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ;.  }.}.#endif..
1a565 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a566 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
1a567 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74  ON./*.** A funct
1a568 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 73 20 61  ion that loads a
1a569 20 73 68 61 72 65 64 2d 6c 69 62 72 61 72 79 20   shared-library 
1a56a 65 78 74 65 6e 73 69 6f 6e 20 74 68 65 6e 20 72  extension then r
1a56b 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  eturns NULL..*/.
1a56c 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64  static void load
1a56d 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  Ext(sqlite3_cont
1a56e 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
1a56f 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
1a570 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
1a571 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
1a572 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  le = (const char
1a573 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
1a574 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
1a575 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
1a576 72 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  roc;.  sqlite3 *
1a577 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65  db = sqlite3_use
1a578 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b  r_data(context);
1a579 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
1a57a 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67   = 0;..  if( arg
1a57b 63 3d 3d 32 20 29 7b 0a 20 20 20 20 7a 50 72 6f  c==2 ){.    zPro
1a57c 63 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  c = (const char 
1a57d 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
1a57e 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
1a57f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 50 72 6f   }else{.    zPro
1a580 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  c = 0;.  }.  if(
1a581 20 7a 46 69 6c 65 20 26 26 20 73 71 6c 69 74 65   zFile && sqlite
1a582 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
1a583 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f  (db, zFile, zPro
1a584 63 2c 20 26 7a 45 72 72 4d 73 67 29 20 29 7b 0a  c, &zErrMsg) ){.
1a585 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
1a586 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
1a587 2c 20 7a 45 72 72 4d 73 67 2c 20 2d 31 29 3b 0a  , zErrMsg, -1);.
1a588 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1a589 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d  (zErrMsg);.  }.}
1a58a 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
1a58b 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a  SQLITE_TEST./*.*
1a58c 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1a58d 67 65 6e 65 72 61 74 65 73 20 61 20 73 74 72 69  generates a stri
1a58e 6e 67 20 6f 66 20 72 61 6e 64 6f 6d 20 63 68 61  ng of random cha
1a58f 72 61 63 74 65 72 73 2e 20 20 55 73 65 64 20 66  racters.  Used f
1a590 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67  or.** generating
1a591 20 74 65 73 74 20 64 61 74 61 2e 0a 2a 2f 0a 73   test data..*/.s
1a592 74 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64 53  tatic void randS
1a593 74 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  tr(sqlite3_conte
1a594 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
1a595 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
1a596 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
1a597 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
1a598 69 67 6e 65 64 20 63 68 61 72 20 7a 53 72 63 5b  igned char zSrc[
1a599 5d 20 3d 20 0a 20 20 20 20 20 22 61 62 63 64 65  ] = .     "abcde
1a59a 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75  fghijklmnopqrstu
1a59b 76 77 78 79 7a 22 0a 20 20 20 20 20 22 41 42 43  vwxyz".     "ABC
1a59c 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53  DEFGHIJKLMNOPQRS
1a59d 54 55 56 57 58 59 5a 22 0a 20 20 20 20 20 22 30  TUVWXYZ".     "0
1a59e 31 32 33 34 35 36 37 38 39 22 0a 20 20 20 20 20  123456789".     
1a59f 22 2e 2d 21 2c 3a 2a 5e 2b 3d 5f 7c 3f 2f 3c 3e  ".-!,:*^+=_|?/<>
1a5a0 20 22 3b 0a 20 20 69 6e 74 20 69 4d 69 6e 2c 20   ";.  int iMin, 
1a5a1 69 4d 61 78 2c 20 6e 2c 20 72 2c 20 69 3b 0a 20  iMax, n, r, i;. 
1a5a2 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a   unsigned char z
1a5a3 42 75 66 5b 31 30 30 30 5d 3b 0a 0a 20 20 2f 2a  Buf[1000];..  /*
1a5a4 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 20 70   It used to be p
1a5a5 6f 73 73 69 62 6c 65 20 74 6f 20 63 61 6c 6c 20  ossible to call 
1a5a6 72 61 6e 64 73 74 72 28 29 20 77 69 74 68 20 61  randstr() with a
1a5a7 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ny number of arg
1a5a8 75 6d 65 6e 74 73 2c 0a 20 20 2a 2a 20 62 75 74  uments,.  ** but
1a5a9 20 6e 6f 77 20 69 74 20 69 73 20 72 65 67 69 73   now it is regis
1a5aa 74 65 72 65 64 20 77 69 74 68 20 53 51 4c 69 74  tered with SQLit
1a5ab 65 20 61 73 20 72 65 71 75 69 72 69 6e 67 20 65  e as requiring e
1a5ac 78 61 63 74 6c 79 20 32 2e 0a 20 20 2a 2f 0a 20  xactly 2..  */. 
1a5ad 20 61 73 73 65 72 74 28 61 72 67 63 3d 3d 32 29   assert(argc==2)
1a5ae 3b 0a 0a 20 20 69 4d 69 6e 20 3d 20 73 71 6c 69  ;..  iMin = sqli
1a5af 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
1a5b0 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 69 4d  gv[0]);.  if( iM
1a5b1 69 6e 3c 30 20 29 20 69 4d 69 6e 20 3d 20 30 3b  in<0 ) iMin = 0;
1a5b2 0a 20 20 69 66 28 20 69 4d 69 6e 3e 3d 73 69 7a  .  if( iMin>=siz
1a5b3 65 6f 66 28 7a 42 75 66 29 20 29 20 69 4d 69 6e  eof(zBuf) ) iMin
1a5b4 20 3d 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d   = sizeof(zBuf)-
1a5b5 31 3b 0a 20 20 69 4d 61 78 20 3d 20 73 71 6c 69  1;.  iMax = sqli
1a5b6 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
1a5b7 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 69 4d  gv[1]);.  if( iM
1a5b8 61 78 3c 69 4d 69 6e 20 29 20 69 4d 61 78 20 3d  ax<iMin ) iMax =
1a5b9 20 69 4d 69 6e 3b 0a 20 20 69 66 28 20 69 4d 61   iMin;.  if( iMa
1a5ba 78 3e 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 20  x>=sizeof(zBuf) 
1a5bb 29 20 69 4d 61 78 20 3d 20 73 69 7a 65 6f 66 28  ) iMax = sizeof(
1a5bc 7a 42 75 66 29 2d 31 3b 0a 20 20 6e 20 3d 20 69  zBuf)-1;.  n = i
1a5bd 4d 69 6e 3b 0a 20 20 69 66 28 20 69 4d 61 78 3e  Min;.  if( iMax>
1a5be 69 4d 69 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69  iMin ){.    sqli
1a5bf 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28 73 69  te3Randomness(si
1a5c0 7a 65 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20 20  zeof(r), &r);.  
1a5c1 20 20 72 20 26 3d 20 30 78 37 66 66 66 66 66 66    r &= 0x7ffffff
1a5c2 66 3b 0a 20 20 20 20 6e 20 2b 3d 20 72 25 28 69  f;.    n += r%(i
1a5c3 4d 61 78 20 2b 20 31 20 2d 20 69 4d 69 6e 29 3b  Max + 1 - iMin);
1a5c4 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
1a5c5 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 3b  <sizeof(zBuf) );
1a5c6 0a 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f 6d  .  sqlite3Random
1a5c7 6e 65 73 73 28 6e 2c 20 7a 42 75 66 29 3b 0a 20  ness(n, zBuf);. 
1a5c8 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
1a5c9 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66 5b 69 5d  ++){.    zBuf[i]
1a5ca 20 3d 20 7a 53 72 63 5b 7a 42 75 66 5b 69 5d 25   = zSrc[zBuf[i]%
1a5cb 28 73 69 7a 65 6f 66 28 7a 53 72 63 29 2d 31 29  (sizeof(zSrc)-1)
1a5cc 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6e 5d  ];.  }.  zBuf[n]
1a5cd 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
1a5ce 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
1a5cf 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 42 75 66  ext, (char*)zBuf
1a5d0 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  , n, SQLITE_TRAN
1a5d1 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66  SIENT);.}.#endif
1a5d2 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20   /* SQLITE_TEST 
1a5d3 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
1a5d4 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 65  E_TEST./*.** The
1a5d5 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 53   following two S
1a5d6 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  QL functions are
1a5d7 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 72 65   used to test re
1a5d8 74 75 72 6e 69 6e 67 20 61 20 74 65 78 74 0a 2a  turning a text.*
1a5d9 2a 20 72 65 73 75 6c 74 20 77 69 74 68 20 61 20  * result with a 
1a5da 64 65 73 74 72 75 63 74 6f 72 2e 20 46 75 6e 63  destructor. Func
1a5db 74 69 6f 6e 20 27 74 65 73 74 5f 64 65 73 74 72  tion 'test_destr
1a5dc 75 63 74 6f 72 27 20 74 61 6b 65 73 20 6f 6e 65  uctor' takes one
1a5dd 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 61 6e 64   argument.** and
1a5de 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d   returns the sam
1a5df 65 20 61 72 67 75 6d 65 6e 74 20 69 6e 74 65 72  e argument inter
1a5e0 70 72 65 74 65 64 20 61 73 20 54 45 58 54 2e 20  preted as TEXT. 
1a5e1 41 20 64 65 73 74 72 75 63 74 6f 72 20 69 73 0a  A destructor is.
1a5e2 2a 2a 20 70 61 73 73 65 64 20 77 69 74 68 20 74  ** passed with t
1a5e3 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
1a5e4 74 5f 74 65 78 74 28 29 20 63 61 6c 6c 2e 0a 2a  t_text() call..*
1a5e5 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  *.** SQL functio
1a5e6 6e 20 27 74 65 73 74 5f 64 65 73 74 72 75 63 74  n 'test_destruct
1a5e7 6f 72 5f 63 6f 75 6e 74 27 20 72 65 74 75 72 6e  or_count' return
1a5e8 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
1a5e9 6f 75 74 73 74 61 6e 64 69 6e 67 20 0a 2a 2a 20  outstanding .** 
1a5ea 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65  allocations made
1a5eb 20 62 79 20 27 74 65 73 74 5f 64 65 73 74 72 75   by 'test_destru
1a5ec 63 74 6f 72 27 3b 0a 2a 2a 0a 2a 2a 20 57 41 52  ctor';.**.** WAR
1a5ed 4e 49 4e 47 3a 20 4e 6f 74 20 74 68 72 65 61 64  NING: Not thread
1a5ee 73 61 66 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  safe..*/.static 
1a5ef 69 6e 74 20 74 65 73 74 5f 64 65 73 74 72 75 63  int test_destruc
1a5f0 74 6f 72 5f 63 6f 75 6e 74 5f 76 61 72 20 3d 20  tor_count_var = 
1a5f1 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  0;.static void d
1a5f2 65 73 74 72 75 63 74 6f 72 28 76 6f 69 64 20 2a  estructor(void *
1a5f3 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c  p){.  char *zVal
1a5f4 20 3d 20 28 63 68 61 72 20 2a 29 70 3b 0a 20 20   = (char *)p;.  
1a5f5 61 73 73 65 72 74 28 7a 56 61 6c 29 3b 0a 20 20  assert(zVal);.  
1a5f6 7a 56 61 6c 2d 2d 3b 0a 20 20 73 71 6c 69 74 65  zVal--;.  sqlite
1a5f7 33 5f 66 72 65 65 28 7a 56 61 6c 29 3b 0a 20 20  3_free(zVal);.  
1a5f8 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72 5f  test_destructor_
1a5f9 63 6f 75 6e 74 5f 76 61 72 2d 2d 3b 0a 7d 0a 73  count_var--;.}.s
1a5fa 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 5f  tatic void test_
1a5fb 64 65 73 74 72 75 63 74 6f 72 28 0a 20 20 73 71  destructor(.  sq
1a5fc 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1a5fd 43 74 78 2c 20 0a 20 20 69 6e 74 20 6e 41 72 67  Ctx, .  int nArg
1a5fe 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
1a5ff 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68  e **argv.){.  ch
1a600 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 69 6e 74 20  ar *zVal;.  int 
1a601 6c 65 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  len;.  sqlite3 *
1a602 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65  db = sqlite3_use
1a603 72 5f 64 61 74 61 28 70 43 74 78 29 3b 0a 20 0a  r_data(pCtx);. .
1a604 20 20 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f    test_destructo
1a605 72 5f 63 6f 75 6e 74 5f 76 61 72 2b 2b 3b 0a 20  r_count_var++;. 
1a606 20 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31   assert( nArg==1
1a607 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   );.  if( sqlite
1a608 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
1a609 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[0])==SQLITE_NU
1a60a 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6c  LL ) return;.  l
1a60b 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  en = sqlite3Valu
1a60c 65 42 79 74 65 73 28 61 72 67 76 5b 30 5d 2c 20  eBytes(argv[0], 
1a60d 45 4e 43 28 64 62 29 29 3b 20 0a 20 20 7a 56 61  ENC(db)); .  zVa
1a60e 6c 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  l = contextMallo
1a60f 63 28 70 43 74 78 2c 20 6c 65 6e 2b 33 29 3b 0a  c(pCtx, len+3);.
1a610 20 20 69 66 28 20 21 7a 56 61 6c 20 29 7b 0a 20    if( !zVal ){. 
1a611 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
1a612 20 7a 56 61 6c 5b 6c 65 6e 2b 31 5d 20 3d 20 30   zVal[len+1] = 0
1a613 3b 0a 20 20 7a 56 61 6c 5b 6c 65 6e 2b 32 5d 20  ;.  zVal[len+2] 
1a614 3d 20 30 3b 0a 20 20 7a 56 61 6c 2b 2b 3b 0a 20  = 0;.  zVal++;. 
1a615 20 6d 65 6d 63 70 79 28 7a 56 61 6c 2c 20 73 71   memcpy(zVal, sq
1a616 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 61  lite3ValueText(a
1a617 72 67 76 5b 30 5d 2c 20 45 4e 43 28 64 62 29 29  rgv[0], ENC(db))
1a618 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28 20 45 4e  , len);.  if( EN
1a619 43 28 64 62 29 3d 3d 53 51 4c 49 54 45 5f 55 54  C(db)==SQLITE_UT
1a61a 46 38 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  F8 ){.    sqlite
1a61b 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
1a61c 74 78 2c 20 7a 56 61 6c 2c 20 2d 31 2c 20 64 65  tx, zVal, -1, de
1a61d 73 74 72 75 63 74 6f 72 29 3b 0a 23 69 66 6e 64  structor);.#ifnd
1a61e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
1a61f 54 46 31 36 0a 20 20 7d 65 6c 73 65 20 69 66 28  TF16.  }else if(
1a620 20 45 4e 43 28 64 62 29 3d 3d 53 51 4c 49 54 45   ENC(db)==SQLITE
1a621 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20  _UTF16LE ){.    
1a622 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
1a623 65 78 74 31 36 6c 65 28 70 43 74 78 2c 20 7a 56  ext16le(pCtx, zV
1a624 61 6c 2c 20 2d 31 2c 20 64 65 73 74 72 75 63 74  al, -1, destruct
1a625 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  or);.  }else{.  
1a626 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1a627 5f 74 65 78 74 31 36 62 65 28 70 43 74 78 2c 20  _text16be(pCtx, 
1a628 7a 56 61 6c 2c 20 2d 31 2c 20 64 65 73 74 72 75  zVal, -1, destru
1a629 63 74 6f 72 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  ctor);.#endif /*
1a62a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1a62b 31 36 20 2a 2f 0a 20 20 7d 0a 7d 0a 73 74 61 74  16 */.  }.}.stat
1a62c 69 63 20 76 6f 69 64 20 74 65 73 74 5f 64 65 73  ic void test_des
1a62d 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74 28 0a 20  tructor_count(. 
1a62e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1a62f 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 6e   *pCtx, .  int n
1a630 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  Arg,.  sqlite3_v
1a631 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
1a632 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a633 69 6e 74 28 70 43 74 78 2c 20 74 65 73 74 5f 64  int(pCtx, test_d
1a634 65 73 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74 5f  estructor_count_
1a635 76 61 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  var);.}.#endif /
1a636 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f  * SQLITE_TEST */
1a637 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1a638 54 45 53 54 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  TEST./*.** Routi
1a639 6e 65 73 20 66 6f 72 20 74 65 73 74 69 6e 67 20  nes for testing 
1a63a 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  the sqlite3_get_
1a63b 61 75 78 64 61 74 61 28 29 20 61 6e 64 20 73 71  auxdata() and sq
1a63c 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
1a63d 61 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  a().** interface
1a63e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74  ..**.** The test
1a63f 5f 61 75 78 64 61 74 61 28 29 20 53 51 4c 20 66  _auxdata() SQL f
1a640 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
1a641 20 74 6f 20 72 65 67 69 73 74 65 72 20 65 61 63   to register eac
1a642 68 20 6f 66 20 69 74 73 20 61 72 67 75 6d 65 6e  h of its argumen
1a643 74 73 0a 2a 2a 20 61 73 20 61 75 78 69 6c 69 61  ts.** as auxilia
1a644 72 79 20 64 61 74 61 2e 20 20 49 66 20 74 68 65  ry data.  If the
1a645 72 65 20 61 72 65 20 6e 6f 20 70 72 69 6f 72 20  re are no prior 
1a646 72 65 67 69 73 74 72 61 74 69 6f 6e 73 20 6f 66  registrations of
1a647 20 61 75 78 20 64 61 74 61 20 66 6f 72 0a 2a 2a   aux data for.**
1a648 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20 28   that argument (
1a649 6d 65 61 6e 69 6e 67 20 74 68 65 20 61 72 67 75  meaning the argu
1a64a 6d 65 6e 74 20 69 73 20 6e 6f 74 20 61 20 63 6f  ment is not a co
1a64b 6e 73 74 61 6e 74 20 6f 72 20 74 68 69 73 20 69  nstant or this i
1a64c 73 20 69 74 73 20 66 69 72 73 74 0a 2a 2a 20 63  s its first.** c
1a64d 61 6c 6c 29 20 74 68 65 6e 20 74 68 65 20 72 65  all) then the re
1a64e 73 75 6c 74 20 66 6f 72 20 74 68 61 74 20 61 72  sult for that ar
1a64f 67 75 6d 65 6e 74 20 69 73 20 30 2e 20 20 49 66  gument is 0.  If
1a650 20 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f   there is a prio
1a651 72 0a 2a 2a 20 72 65 67 69 73 74 72 61 74 69 6f  r.** registratio
1a652 6e 2c 20 74 68 65 20 72 65 73 75 6c 74 20 66 6f  n, the result fo
1a653 72 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20  r that argument 
1a654 69 73 20 31 2e 20 20 54 68 65 20 6f 76 65 72 61  is 1.  The overa
1a655 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20  ll result.** is 
1a656 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 61  the individual a
1a657 72 67 75 6d 65 6e 74 20 72 65 73 75 6c 74 73 20  rgument results 
1a658 73 65 70 61 72 61 74 65 64 20 62 79 20 73 70 61  separated by spa
1a659 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ces..*/.static v
1a65a 6f 69 64 20 66 72 65 65 5f 74 65 73 74 5f 61 75  oid free_test_au
1a65b 78 64 61 74 61 28 76 6f 69 64 20 2a 70 29 20 7b  xdata(void *p) {
1a65c 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
1a65d 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65  }.static void te
1a65e 73 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73 71  st_auxdata(.  sq
1a65f 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1a660 43 74 78 2c 20 0a 20 20 69 6e 74 20 6e 41 72 67  Ctx, .  int nArg
1a661 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
1a662 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e  e **argv.){.  in
1a663 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65  t i;.  char *zRe
1a664 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  t = contextMallo
1a665 63 28 70 43 74 78 2c 20 6e 41 72 67 2a 32 29 3b  c(pCtx, nArg*2);
1a666 0a 20 20 69 66 28 20 21 7a 52 65 74 20 29 20 72  .  if( !zRet ) r
1a667 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28  eturn;.  memset(
1a668 7a 52 65 74 2c 20 30 2c 20 6e 41 72 67 2a 32 29  zRet, 0, nArg*2)
1a669 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
1a66a 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  Arg; i++){.    c
1a66b 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 3d 20 28  har const *z = (
1a66c 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
1a66d 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d  lue_text(argv[i]
1a66e 29 3b 0a 20 20 20 20 69 66 28 20 7a 20 29 7b 0a  );.    if( z ){.
1a66f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 75 78        char *zAux
1a670 20 3d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61   = sqlite3_get_a
1a671 75 78 64 61 74 61 28 70 43 74 78 2c 20 69 29 3b  uxdata(pCtx, i);
1a672 0a 20 20 20 20 20 20 69 66 28 20 7a 41 75 78 20  .      if( zAux 
1a673 29 7b 0a 20 20 20 20 20 20 20 20 7a 52 65 74 5b  ){.        zRet[
1a674 69 2a 32 5d 20 3d 20 27 31 27 3b 0a 20 20 20 20  i*2] = '1';.    
1a675 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
1a676 41 75 78 2c 20 7a 29 20 29 7b 0a 20 20 20 20 20  Aux, z) ){.     
1a677 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
1a678 75 6c 74 5f 65 72 72 6f 72 28 70 43 74 78 2c 20  ult_error(pCtx, 
1a679 22 41 75 78 69 6c 61 72 79 20 64 61 74 61 20 63  "Auxilary data c
1a67a 6f 72 72 75 70 74 69 6f 6e 22 2c 20 2d 31 29 3b  orruption", -1);
1a67b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1a67c 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  n;.        }.   
1a67d 20 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 20     }else {.     
1a67e 20 20 20 7a 52 65 74 5b 69 2a 32 5d 20 3d 20 27     zRet[i*2] = '
1a67f 30 27 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  0';.      }..   
1a680 20 20 20 7a 41 75 78 20 3d 20 63 6f 6e 74 65 78     zAux = contex
1a681 74 4d 61 6c 6c 6f 63 28 70 43 74 78 2c 20 73 74  tMalloc(pCtx, st
1a682 72 6c 65 6e 28 7a 29 2b 31 29 3b 0a 20 20 20 20  rlen(z)+1);.    
1a683 20 20 69 66 28 20 7a 41 75 78 20 29 7b 0a 20 20    if( zAux ){.  
1a684 20 20 20 20 20 20 73 74 72 63 70 79 28 7a 41 75        strcpy(zAu
1a685 78 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 73  x, z);.        s
1a686 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
1a687 74 61 28 70 43 74 78 2c 20 69 2c 20 7a 41 75 78  ta(pCtx, i, zAux
1a688 2c 20 66 72 65 65 5f 74 65 73 74 5f 61 75 78 64  , free_test_auxd
1a689 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ata);.      }.  
1a68a 20 20 20 20 7a 52 65 74 5b 69 2a 32 2b 31 5d 20      zRet[i*2+1] 
1a68b 3d 20 27 20 27 3b 0a 20 20 20 20 7d 0a 20 20 7d  = ' ';.    }.  }
1a68c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
1a68d 74 5f 74 65 78 74 28 70 43 74 78 2c 20 7a 52 65  t_text(pCtx, zRe
1a68e 74 2c 20 32 2a 6e 41 72 67 2d 31 2c 20 66 72 65  t, 2*nArg-1, fre
1a68f 65 5f 74 65 73 74 5f 61 75 78 64 61 74 61 29 3b  e_test_auxdata);
1a690 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1a691 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 23 69 66  ITE_TEST */..#if
1a692 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1a693 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e  /*.** A function
1a694 20 74 6f 20 74 65 73 74 20 65 72 72 6f 72 20 72   to test error r
1a695 65 70 6f 72 74 69 6e 67 20 66 72 6f 6d 20 75 73  eporting from us
1a696 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 20 54 68  er functions. Th
1a697 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72  is function.** r
1a698 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66  eturns a copy of
1a699 20 69 74 27 73 20 66 69 72 73 74 20 61 72 67 75   it's first argu
1a69a 6d 65 6e 74 20 61 73 20 61 6e 20 65 72 72 6f 72  ment as an error
1a69b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1a69c 20 74 65 73 74 5f 65 72 72 6f 72 28 0a 20 20 73   test_error(.  s
1a69d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a69e 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 6e 41 72  pCtx, .  int nAr
1a69f 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  g,.  sqlite3_val
1a6a0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73  ue **argv.){.  s
1a6a1 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1a6a2 72 6f 72 28 70 43 74 78 2c 20 28 63 68 61 72 2a  ror(pCtx, (char*
1a6a3 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1a6a4 65 78 74 28 61 72 67 76 5b 30 5d 29 2c 20 30 29  ext(argv[0]), 0)
1a6a5 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1a6a6 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a  LITE_TEST */../*
1a6a7 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
1a6a8 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1a6a9 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
1a6aa 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20   the context of 
1a6ab 61 0a 2a 2a 20 73 75 6d 28 29 20 6f 72 20 61 76  a.** sum() or av
1a6ac 67 28 29 20 61 67 67 72 65 67 61 74 65 20 63 6f  g() aggregate co
1a6ad 6d 70 75 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79  mputation..*/.ty
1a6ae 70 65 64 65 66 20 73 74 72 75 63 74 20 53 75 6d  pedef struct Sum
1a6af 43 74 78 20 53 75 6d 43 74 78 3b 0a 73 74 72 75  Ctx SumCtx;.stru
1a6b0 63 74 20 53 75 6d 43 74 78 20 7b 0a 20 20 64 6f  ct SumCtx {.  do
1a6b1 75 62 6c 65 20 72 53 75 6d 3b 20 20 20 20 20 20  uble rSum;      
1a6b2 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  /* Floating poin
1a6b3 74 20 73 75 6d 20 2a 2f 0a 20 20 69 36 34 20 69  t sum */.  i64 i
1a6b4 53 75 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Sum;         /* 
1a6b5 49 6e 74 65 67 65 72 20 73 75 6d 20 2a 2f 20 20  Integer sum */  
1a6b6 20 0a 20 20 69 36 34 20 63 6e 74 3b 20 20 20 20   .  i64 cnt;    
1a6b7 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1a6b8 6f 66 20 65 6c 65 6d 65 6e 74 73 20 73 75 6d 6d  of elements summ
1a6b9 65 64 20 2a 2f 0a 20 20 75 38 20 6f 76 65 72 66  ed */.  u8 overf
1a6ba 6c 6f 77 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  low;      /* Tru
1a6bb 65 20 69 66 20 69 6e 74 65 67 65 72 20 6f 76 65  e if integer ove
1a6bc 72 66 6c 6f 77 20 73 65 65 6e 20 2a 2f 0a 20 20  rflow seen */.  
1a6bd 75 38 20 61 70 70 72 6f 78 3b 20 20 20 20 20 20  u8 approx;      
1a6be 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 6e    /* True if non
1a6bf 2d 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77  -integer value w
1a6c0 61 73 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20  as input to the 
1a6c1 73 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  sum */.};../*.**
1a6c2 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74   Routines used t
1a6c3 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 73 75  o compute the su
1a6c4 6d 2c 20 61 76 65 72 61 67 65 2c 20 61 6e 64 20  m, average, and 
1a6c5 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  total..**.** The
1a6c6 20 53 55 4d 28 29 20 66 75 6e 63 74 69 6f 6e 20   SUM() function 
1a6c7 66 6f 6c 6c 6f 77 73 20 74 68 65 20 28 62 72 6f  follows the (bro
1a6c8 6b 65 6e 29 20 53 51 4c 20 73 74 61 6e 64 61 72  ken) SQL standar
1a6c9 64 20 77 68 69 63 68 20 6d 65 61 6e 73 0a 2a 2a  d which means.**
1a6ca 20 74 68 61 74 20 69 74 20 72 65 74 75 72 6e 73   that it returns
1a6cb 20 4e 55 4c 4c 20 69 66 20 69 74 20 73 75 6d 73   NULL if it sums
1a6cc 20 6f 76 65 72 20 6e 6f 20 69 6e 70 75 74 73 2e   over no inputs.
1a6cd 20 20 54 4f 54 41 4c 20 72 65 74 75 72 6e 73 0a    TOTAL returns.
1a6ce 2a 2a 20 30 2e 30 20 69 6e 20 74 68 61 74 20 63  ** 0.0 in that c
1a6cf 61 73 65 2e 20 20 49 6e 20 61 64 64 69 74 69 6f  ase.  In additio
1a6d0 6e 2c 20 54 4f 54 41 4c 20 61 6c 77 61 79 73 20  n, TOTAL always 
1a6d1 72 65 74 75 72 6e 73 20 61 20 66 6c 6f 61 74 20  returns a float 
1a6d2 77 68 65 72 65 0a 2a 2a 20 53 55 4d 20 6d 69 67  where.** SUM mig
1a6d3 68 74 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74  ht return an int
1a6d4 65 67 65 72 20 69 66 20 69 74 20 6e 65 76 65 72  eger if it never
1a6d5 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 66 6c   encounters a fl
1a6d6 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20  oating point.** 
1a6d7 76 61 6c 75 65 2e 20 20 54 4f 54 41 4c 20 6e 65  value.  TOTAL ne
1a6d8 76 65 72 20 66 61 69 6c 73 2c 20 62 75 74 20 53  ver fails, but S
1a6d9 55 4d 20 6d 69 67 68 74 20 74 68 72 6f 75 67 68  UM might through
1a6da 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66   an exception if
1a6db 0a 2a 2a 20 69 74 20 6f 76 65 72 66 6c 6f 77 73  .** it overflows
1a6dc 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a   an integer..*/.
1a6dd 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d 53  static void sumS
1a6de 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  tep(sqlite3_cont
1a6df 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
1a6e0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
1a6e1 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
1a6e2 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 69 6e   SumCtx *p;.  in
1a6e3 74 20 74 79 70 65 3b 0a 20 20 61 73 73 65 72 74  t type;.  assert
1a6e4 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 70  ( argc==1 );.  p
1a6e5 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
1a6e6 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
1a6e7 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29  text, sizeof(*p)
1a6e8 29 3b 0a 20 20 74 79 70 65 20 3d 20 73 71 6c 69  );.  type = sqli
1a6e9 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69  te3_value_numeri
1a6ea 63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b  c_type(argv[0]);
1a6eb 0a 20 20 69 66 28 20 70 20 26 26 20 74 79 70 65  .  if( p && type
1a6ec 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b  !=SQLITE_NULL ){
1a6ed 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20  .    p->cnt++;. 
1a6ee 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53 51 4c     if( type==SQL
1a6ef 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20  ITE_INTEGER ){. 
1a6f0 20 20 20 20 20 69 36 34 20 76 20 3d 20 73 71 6c       i64 v = sql
1a6f1 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
1a6f2 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
1a6f3 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a 20   p->rSum += v;. 
1a6f4 20 20 20 20 20 69 66 28 20 28 70 2d 3e 61 70 70       if( (p->app
1a6f5 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77 29  rox|p->overflow)
1a6f6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
1a6f7 36 34 20 69 4e 65 77 53 75 6d 20 3d 20 70 2d 3e  64 iNewSum = p->
1a6f8 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20 20 20 20  iSum + v;.      
1a6f9 20 20 69 6e 74 20 73 31 20 3d 20 70 2d 3e 69 53    int s1 = p->iS
1a6fa 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36  um >> (sizeof(i6
1a6fb 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20  4)*8-1);.       
1a6fc 20 69 6e 74 20 73 32 20 3d 20 76 20 20 20 20 20   int s2 = v     
1a6fd 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34    >> (sizeof(i64
1a6fe 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  )*8-1);.        
1a6ff 69 6e 74 20 73 33 20 3d 20 69 4e 65 77 53 75 6d  int s3 = iNewSum
1a700 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34 29   >> (sizeof(i64)
1a701 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 70  *8-1);.        p
1a702 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20 28 73 31  ->overflow = (s1
1a703 26 73 32 26 7e 73 33 29 20 7c 20 28 7e 73 31 26  &s2&~s3) | (~s1&
1a704 7e 73 32 26 73 33 29 3b 0a 20 20 20 20 20 20 20  ~s2&s3);.       
1a705 20 70 2d 3e 69 53 75 6d 20 3d 20 69 4e 65 77 53   p->iSum = iNewS
1a706 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  um;.      }.    
1a707 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e  }else{.      p->
1a708 72 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65 33 5f  rSum += sqlite3_
1a709 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67  value_double(arg
1a70a 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e  v[0]);.      p->
1a70b 61 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20 20 20  approx = 1;.    
1a70c 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  }.  }.}.static v
1a70d 6f 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a 65 28  oid sumFinalize(
1a70e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1a70f 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d  *context){.  Sum
1a710 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71  Ctx *p;.  p = sq
1a711 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
1a712 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
1a713 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20   0);.  if( p && 
1a714 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20  p->cnt>0 ){.    
1a715 69 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20  if( p->overflow 
1a716 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1a717 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
1a718 6e 74 65 78 74 2c 22 69 6e 74 65 67 65 72 20 6f  ntext,"integer o
1a719 76 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a 20 20  verflow",-1);.  
1a71a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 61    }else if( p->a
1a71b 70 70 72 6f 78 20 29 7b 0a 20 20 20 20 20 20 73  pprox ){.      s
1a71c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f  qlite3_result_do
1a71d 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d  uble(context, p-
1a71e 3e 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73  >rSum);.    }els
1a71f 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
1a720 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f  _result_int64(co
1a721 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d 29 3b  ntext, p->iSum);
1a722 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61  .    }.  }.}.sta
1a723 74 69 63 20 76 6f 69 64 20 61 76 67 46 69 6e 61  tic void avgFina
1a724 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lize(sqlite3_con
1a725 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a  text *context){.
1a726 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70    SumCtx *p;.  p
1a727 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
1a728 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
1a729 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  text, 0);.  if( 
1a72a 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b  p && p->cnt>0 ){
1a72b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
1a72c 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65  ult_double(conte
1a72d 78 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64 6f 75  xt, p->rSum/(dou
1a72e 62 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20 20 7d  ble)p->cnt);.  }
1a72f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  .}.static void t
1a730 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73 71 6c  otalFinalize(sql
1a731 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
1a732 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78  ntext){.  SumCtx
1a733 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74   *p;.  p = sqlit
1a734 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
1a735 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29  text(context, 0)
1a736 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
1a737 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78  lt_double(contex
1a738 74 2c 20 70 20 3f 20 70 2d 3e 72 53 75 6d 20 3a  t, p ? p->rSum :
1a739 20 30 2e 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   0.0);.}../*.** 
1a73a 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  The following st
1a73b 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
1a73c 61 63 6b 20 6f 66 20 73 74 61 74 65 20 69 6e 66  ack of state inf
1a73d 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  ormation for the
1a73e 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67 67 72  .** count() aggr
1a73f 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  egate function..
1a740 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
1a741 74 20 43 6f 75 6e 74 43 74 78 20 43 6f 75 6e 74  t CountCtx Count
1a742 43 74 78 3b 0a 73 74 72 75 63 74 20 43 6f 75 6e  Ctx;.struct Coun
1a743 74 43 74 78 20 7b 0a 20 20 69 36 34 20 6e 3b 0a  tCtx {.  i64 n;.
1a744 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  };../*.** Routin
1a745 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  es to implement 
1a746 74 68 65 20 63 6f 75 6e 74 28 29 20 61 67 67 72  the count() aggr
1a747 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  egate function..
1a748 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
1a749 6f 75 6e 74 53 74 65 70 28 73 71 6c 69 74 65 33  ountStep(sqlite3
1a74a 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
1a74b 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
1a74c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
1a74d 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a  v){.  CountCtx *
1a74e 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  p;.  p = sqlite3
1a74f 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
1a750 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65  xt(context, size
1a751 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 28  of(*p));.  if( (
1a752 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c 49 54  argc==0 || SQLIT
1a753 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f  E_NULL!=sqlite3_
1a754 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
1a755 30 5d 29 29 20 26 26 20 70 20 29 7b 0a 20 20 20  0])) && p ){.   
1a756 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 7d 20 20   p->n++;.  }.}  
1a757 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f   .static void co
1a758 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69  untFinalize(sqli
1a759 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
1a75a 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43 74  text){.  CountCt
1a75b 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
1a75c 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
1a75d 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
1a75e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
1a75f 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78  ult_int64(contex
1a760 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30 29  t, p ? p->n : 0)
1a761 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  ;.}../*.** Routi
1a762 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  nes to implement
1a763 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29   min() and max()
1a764 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1a765 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ions..*/.static 
1a766 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70 28  void minmaxStep(
1a767 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1a768 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72  *context, int ar
1a769 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
1a76a 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 4d 65 6d  e **argv){.  Mem
1a76b 20 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20 2a   *pArg  = (Mem *
1a76c 29 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20  )argv[0];.  Mem 
1a76d 2a 70 42 65 73 74 3b 0a 0a 20 20 69 66 28 20 73  *pBest;..  if( s
1a76e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
1a76f 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
1a770 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  TE_NULL ) return
1a771 3b 0a 20 20 70 42 65 73 74 20 3d 20 28 4d 65 6d  ;.  pBest = (Mem
1a772 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65   *)sqlite3_aggre
1a773 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
1a774 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 42  text, sizeof(*pB
1a775 65 73 74 29 29 3b 0a 20 20 69 66 28 20 21 70 42  est));.  if( !pB
1a776 65 73 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  est ) return;.. 
1a777 20 69 66 28 20 70 42 65 73 74 2d 3e 66 6c 61 67   if( pBest->flag
1a778 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61 78  s ){.    int max
1a779 3b 0a 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a 20  ;.    int cmp;. 
1a77a 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
1a77b 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75  l = sqlite3GetFu
1a77c 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78  ncCollSeq(contex
1a77d 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  t);.    /* This 
1a77e 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 69 73  step function is
1a77f 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20 74   used for both t
1a780 68 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78  he min() and max
1a781 28 29 20 61 67 67 72 65 67 61 74 65 73 2c 0a 20  () aggregates,. 
1a782 20 20 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 64     ** the only d
1a783 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
1a784 6e 20 74 68 65 20 74 77 6f 20 62 65 69 6e 67 20  n the two being 
1a785 74 68 61 74 20 74 68 65 20 73 65 6e 73 65 20 6f  that the sense o
1a786 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6d  f the.    ** com
1a787 70 61 72 69 73 6f 6e 20 69 73 20 69 6e 76 65 72  parison is inver
1a788 74 65 64 2e 20 46 6f 72 20 74 68 65 20 6d 61 78  ted. For the max
1a789 28 29 20 61 67 67 72 65 67 61 74 65 2c 20 74 68  () aggregate, th
1a78a 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  e.    ** sqlite3
1a78b 5f 75 73 65 72 5f 64 61 74 61 28 29 20 66 75 6e  _user_data() fun
1a78c 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 28 76  ction returns (v
1a78d 6f 69 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d 69  oid *)-1. For mi
1a78e 6e 28 29 20 69 74 0a 20 20 20 20 2a 2a 20 72 65  n() it.    ** re
1a78f 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 64 62  turns (void *)db
1a790 2c 20 77 68 65 72 65 20 64 62 20 69 73 20 74 68  , where db is th
1a791 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62  e sqlite3* datab
1a792 61 73 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 20  ase pointer..   
1a793 20 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 74 68   ** Therefore th
1a794 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74  e next statement
1a795 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 27   sets variable '
1a796 6d 61 78 27 20 74 6f 20 31 20 66 6f 72 20 74 68  max' to 1 for th
1a797 65 20 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20 61  e max().    ** a
1a798 67 67 72 65 67 61 74 65 2c 20 6f 72 20 30 20 66  ggregate, or 0 f
1a799 6f 72 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a 2f  or min()..    */
1a79a 0a 20 20 20 20 6d 61 78 20 3d 20 73 71 6c 69 74  .    max = sqlit
1a79b 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e  e3_user_data(con
1a79c 74 65 78 74 29 21 3d 30 3b 0a 20 20 20 20 63 6d  text)!=0;.    cm
1a79d 70 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f  p = sqlite3MemCo
1a79e 6d 70 61 72 65 28 70 42 65 73 74 2c 20 70 41 72  mpare(pBest, pAr
1a79f 67 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69  g, pColl);.    i
1a7a0 66 28 20 28 6d 61 78 20 26 26 20 63 6d 70 3c 30  f( (max && cmp<0
1a7a1 29 20 7c 7c 20 28 21 6d 61 78 20 26 26 20 63 6d  ) || (!max && cm
1a7a2 70 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71  p>0) ){.      sq
1a7a3 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79  lite3VdbeMemCopy
1a7a4 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20  (pBest, pArg);. 
1a7a5 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
1a7a6 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1a7a7 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72 67  Copy(pBest, pArg
1a7a8 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
1a7a9 76 6f 69 64 20 6d 69 6e 4d 61 78 46 69 6e 61 6c  void minMaxFinal
1a7aa 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ize(sqlite3_cont
1a7ab 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
1a7ac 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1a7ad 70 52 65 73 3b 0a 20 20 70 52 65 73 20 3d 20 28  pRes;.  pRes = (
1a7ae 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 29  sqlite3_value *)
1a7af 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
1a7b0 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
1a7b1 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 52 65  t, 0);.  if( pRe
1a7b2 73 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52 65  s ){.    if( pRe
1a7b3 73 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20 20  s->flags ){.    
1a7b4 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1a7b5 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20  _value(context, 
1a7b6 70 52 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pRes);.    }.   
1a7b7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1a7b8 65 6c 65 61 73 65 28 70 52 65 73 29 3b 0a 20 20  elease(pRes);.  
1a7b9 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  }.}.../*.** This
1a7ba 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74   function regist
1a7bb 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20  ered all of the 
1a7bc 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e  above C function
1a7bd 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63  s as SQL.** func
1a7be 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f  tions.  This sho
1a7bf 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20  uld be the only 
1a7c0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
1a7c1 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74  file with.** ext
1a7c2 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a  ernal linkage..*
1a7c3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a7c4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
1a7c5 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63  isterBuiltinFunc
1a7c6 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  tions(sqlite3 *d
1a7c7 62 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  b){.  static con
1a7c8 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20  st struct {.    
1a7c9 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
1a7ca 20 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e     signed char n
1a7cb 41 72 67 3b 0a 20 20 20 20 20 75 38 20 61 72 67  Arg;.     u8 arg
1a7cc 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  Type;           
1a7cd 2f 2a 20 66 66 3a 20 64 62 20 20 20 31 3a 20 30  /* ff: db   1: 0
1a7ce 2c 20 32 3a 20 31 2c 20 33 3a 20 32 2c 2e 2e 2e  , 2: 1, 3: 2,...
1a7cf 20 20 4e 3a 20 20 4e 2d 31 2e 20 2a 2f 0a 20 20    N:  N-1. */.  
1a7d0 20 20 20 75 38 20 65 54 65 78 74 52 65 70 3b 20     u8 eTextRep; 
1a7d1 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 55           /* 1: U
1a7d2 54 46 2d 31 36 2e 20 20 30 3a 20 55 54 46 2d 38  TF-16.  0: UTF-8
1a7d3 20 2a 2f 0a 20 20 20 20 20 75 38 20 6e 65 65 64   */.     u8 need
1a7d4 43 6f 6c 6c 53 65 71 3b 0a 20 20 20 20 20 76 6f  CollSeq;.     vo
1a7d5 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
1a7d6 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
1a7d7 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
1a7d8 2a 29 3b 0a 20 20 7d 20 61 46 75 6e 63 73 5b 5d  *);.  } aFuncs[]
1a7d9 20 3d 20 7b 0a 20 20 20 20 7b 20 22 6d 69 6e 22   = {.    { "min"
1a7da 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1a7db 2d 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54  -1, 0, SQLITE_UT
1a7dc 46 38 2c 20 20 20 20 31 2c 20 6d 69 6e 6d 61 78  F8,    1, minmax
1a7dd 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 6d  Func },.    { "m
1a7de 69 6e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  in",            
1a7df 20 20 20 20 30 2c 20 30 2c 20 53 51 4c 49 54 45      0, 0, SQLITE
1a7e0 5f 55 54 46 38 2c 20 20 20 20 31 2c 20 30 20 20  _UTF8,    1, 0  
1a7e1 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b          },.    {
1a7e2 20 22 6d 61 78 22 2c 20 20 20 20 20 20 20 20 20   "max",         
1a7e3 20 20 20 20 20 20 2d 31 2c 20 31 2c 20 53 51 4c        -1, 1, SQL
1a7e4 49 54 45 5f 55 54 46 38 2c 20 20 20 20 31 2c 20  ITE_UTF8,    1, 
1a7e5 6d 69 6e 6d 61 78 46 75 6e 63 20 7d 2c 0a 20 20  minmaxFunc },.  
1a7e6 20 20 7b 20 22 6d 61 78 22 2c 20 20 20 20 20 20    { "max",      
1a7e7 20 20 20 20 20 20 20 20 20 20 30 2c 20 31 2c 20            0, 1, 
1a7e8 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
1a7e9 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 7d 2c  1, 0          },
1a7ea 0a 20 20 20 20 7b 20 22 74 79 70 65 6f 66 22 2c  .    { "typeof",
1a7eb 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
1a7ec 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  0, SQLITE_UTF8, 
1a7ed 20 20 20 30 2c 20 74 79 70 65 6f 66 46 75 6e 63     0, typeofFunc
1a7ee 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 65 6e 67 74   },.    { "lengt
1a7ef 68 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  h",             
1a7f0 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46  1, 0, SQLITE_UTF
1a7f1 38 2c 20 20 20 20 30 2c 20 6c 65 6e 67 74 68 46  8,    0, lengthF
1a7f2 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 73 75  unc },.    { "su
1a7f3 62 73 74 72 22 2c 20 20 20 20 20 20 20 20 20 20  bstr",          
1a7f4 20 20 20 33 2c 20 30 2c 20 53 51 4c 49 54 45 5f     3, 0, SQLITE_
1a7f5 55 54 46 38 2c 20 20 20 20 30 2c 20 73 75 62 73  UTF8,    0, subs
1a7f6 74 72 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20  trFunc },.    { 
1a7f7 22 61 62 73 22 2c 20 20 20 20 20 20 20 20 20 20  "abs",          
1a7f8 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c 49        1, 0, SQLI
1a7f9 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 61  TE_UTF8,    0, a
1a7fa 62 73 46 75 6e 63 20 20 20 20 7d 2c 0a 20 20 20  bsFunc    },.   
1a7fb 20 7b 20 22 72 6f 75 6e 64 22 2c 20 20 20 20 20   { "round",     
1a7fc 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53           1, 0, S
1a7fd 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30  QLITE_UTF8,    0
1a7fe 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 7d 2c 0a  , roundFunc  },.
1a7ff 20 20 20 20 7b 20 22 72 6f 75 6e 64 22 2c 20 20      { "round",  
1a800 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30              2, 0
1a801 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
1a802 20 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20    0, roundFunc  
1a803 7d 2c 0a 20 20 20 20 7b 20 22 75 70 70 65 72 22  },.    { "upper"
1a804 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ,              1
1a805 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 0, SQLITE_UTF8
1a806 2c 20 20 20 20 30 2c 20 75 70 70 65 72 46 75 6e  ,    0, upperFun
1a807 63 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 6f 77  c  },.    { "low
1a808 65 72 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  er",            
1a809 20 20 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55    1, 0, SQLITE_U
1a80a 54 46 38 2c 20 20 20 20 30 2c 20 6c 6f 77 65 72  TF8,    0, lower
1a80b 46 75 6e 63 20 20 7d 2c 0a 20 20 20 20 7b 20 22  Func  },.    { "
1a80c 63 6f 61 6c 65 73 63 65 22 2c 20 20 20 20 20 20  coalesce",      
1a80d 20 20 20 20 2d 31 2c 20 30 2c 20 53 51 4c 49 54      -1, 0, SQLIT
1a80e 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 69 66  E_UTF8,    0, if
1a80f 6e 75 6c 6c 46 75 6e 63 20 7d 2c 0a 20 20 20 20  nullFunc },.    
1a810 7b 20 22 63 6f 61 6c 65 73 63 65 22 2c 20 20 20  { "coalesce",   
1a811 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 53 51          0, 0, SQ
1a812 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c  LITE_UTF8,    0,
1a813 20 30 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20   0          },. 
1a814 20 20 20 7b 20 22 63 6f 61 6c 65 73 63 65 22 2c     { "coalesce",
1a815 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
1a816 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
1a817 20 30 2c 20 30 20 20 20 20 20 20 20 20 20 20 7d   0, 0          }
1a818 2c 0a 20 20 20 20 7b 20 22 68 65 78 22 2c 20 20  ,.    { "hex",  
1a819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
1a81a 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   0, SQLITE_UTF8,
1a81b 20 20 20 20 30 2c 20 68 65 78 46 75 6e 63 20 20      0, hexFunc  
1a81c 20 20 7d 2c 0a 20 20 20 20 7b 20 22 69 66 6e 75    },.    { "ifnu
1a81d 6c 6c 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  ll",            
1a81e 20 32 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54   2, 0, SQLITE_UT
1a81f 46 38 2c 20 20 20 20 31 2c 20 69 66 6e 75 6c 6c  F8,    1, ifnull
1a820 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 72  Func },.    { "r
1a821 61 6e 64 6f 6d 22 2c 20 20 20 20 20 20 20 20 20  andom",         
1a822 20 20 20 2d 31 2c 20 30 2c 20 53 51 4c 49 54 45     -1, 0, SQLITE
1a823 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 72 61 6e  _UTF8,    0, ran
1a824 64 6f 6d 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b  domFunc },.    {
1a825 20 22 72 61 6e 64 6f 6d 62 6c 6f 62 22 2c 20 20   "randomblob",  
1a826 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c         1, 0, SQL
1a827 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20  ITE_UTF8,    0, 
1a828 72 61 6e 64 6f 6d 42 6c 6f 62 20 7d 2c 0a 20 20  randomBlob },.  
1a829 20 20 7b 20 22 6e 75 6c 6c 69 66 22 2c 20 20 20    { "nullif",   
1a82a 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20            2, 0, 
1a82b 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
1a82c 31 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20 7d 2c  1, nullifFunc },
1a82d 0a 20 20 20 20 7b 20 22 73 71 6c 69 74 65 5f 76  .    { "sqlite_v
1a82e 65 72 73 69 6f 6e 22 2c 20 20 20 20 20 30 2c 20  ersion",     0, 
1a82f 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  0, SQLITE_UTF8, 
1a830 20 20 20 30 2c 20 76 65 72 73 69 6f 6e 46 75 6e     0, versionFun
1a831 63 7d 2c 0a 20 20 20 20 7b 20 22 71 75 6f 74 65  c},.    { "quote
1a832 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
1a833 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46  1, 0, SQLITE_UTF
1a834 38 2c 20 20 20 20 30 2c 20 71 75 6f 74 65 46 75  8,    0, quoteFu
1a835 6e 63 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 61  nc  },.    { "la
1a836 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 22  st_insert_rowid"
1a837 2c 20 20 30 2c 20 30 78 66 66 2c 20 53 51 4c 49  ,  0, 0xff, SQLI
1a838 54 45 5f 55 54 46 38 2c 20 30 2c 20 6c 61 73 74  TE_UTF8, 0, last
1a839 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 20 7d 2c  _insert_rowid },
1a83a 0a 20 20 20 20 7b 20 22 63 68 61 6e 67 65 73 22  .    { "changes"
1a83b 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20  ,            0, 
1a83c 30 78 66 66 2c 20 53 51 4c 49 54 45 5f 55 54 46  0xff, SQLITE_UTF
1a83d 38 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20 20  8, 0, changes   
1a83e 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b          },.    {
1a83f 20 22 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 22   "total_changes"
1a840 2c 20 20 20 20 20 20 30 2c 20 30 78 66 66 2c 20  ,      0, 0xff, 
1a841 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20  SQLITE_UTF8, 0, 
1a842 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 20 20 20  total_changes   
1a843 20 20 7d 2c 0a 20 20 20 20 7b 20 22 72 65 70 6c    },.    { "repl
1a844 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20 20  ace",           
1a845 20 33 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54   3, 0, SQLITE_UT
1a846 46 38 2c 20 20 20 20 30 2c 20 72 65 70 6c 61 63  F8,    0, replac
1a847 65 46 75 6e 63 20 20 20 20 20 20 20 7d 2c 0a 20  eFunc       },. 
1a848 20 20 20 7b 20 22 6c 74 72 69 6d 22 2c 20 20 20     { "ltrim",   
1a849 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 31 2c             1, 1,
1a84a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
1a84b 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20   0, trimFunc    
1a84c 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22        },.    { "
1a84d 6c 74 72 69 6d 22 2c 20 20 20 20 20 20 20 20 20  ltrim",         
1a84e 20 20 20 20 20 32 2c 20 31 2c 20 53 51 4c 49 54       2, 1, SQLIT
1a84f 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 74 72  E_UTF8,    0, tr
1a850 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 20  imFunc          
1a851 7d 2c 0a 20 20 20 20 7b 20 22 72 74 72 69 6d 22  },.    { "rtrim"
1a852 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ,              1
1a853 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 2, SQLITE_UTF8
1a854 2c 20 20 20 20 30 2c 20 74 72 69 6d 46 75 6e 63  ,    0, trimFunc
1a855 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20            },.   
1a856 20 7b 20 22 72 74 72 69 6d 22 2c 20 20 20 20 20   { "rtrim",     
1a857 20 20 20 20 20 20 20 20 20 32 2c 20 32 2c 20 53           2, 2, S
1a858 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30  QLITE_UTF8,    0
1a859 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20  , trimFunc      
1a85a 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 74 72      },.    { "tr
1a85b 69 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  im",            
1a85c 20 20 20 31 2c 20 33 2c 20 53 51 4c 49 54 45 5f     1, 3, SQLITE_
1a85d 55 54 46 38 2c 20 20 20 20 30 2c 20 74 72 69 6d  UTF8,    0, trim
1a85e 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 7d 2c  Func          },
1a85f 0a 20 20 20 20 7b 20 22 74 72 69 6d 22 2c 20 20  .    { "trim",  
1a860 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20               2, 
1a861 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  3, SQLITE_UTF8, 
1a862 20 20 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20     0, trimFunc  
1a863 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b          },.    {
1a864 20 22 7a 65 72 6f 62 6c 6f 62 22 2c 20 20 20 20   "zeroblob",    
1a865 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c         1, 0, SQL
1a866 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20  ITE_UTF8,    0, 
1a867 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20 20  zeroblobFunc    
1a868 20 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c 49    },.#ifdef SQLI
1a869 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20 20 7b  TE_SOUNDEX.    {
1a86a 20 22 73 6f 75 6e 64 65 78 22 2c 20 20 20 20 20   "soundex",     
1a86b 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c         1, 0, SQL
1a86c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20  ITE_UTF8,    0, 
1a86d 73 6f 75 6e 64 65 78 46 75 6e 63 7d 2c 0a 23 65  soundexFunc},.#e
1a86e 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
1a86f 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
1a870 54 45 4e 53 49 4f 4e 0a 20 20 20 20 7b 20 22 6c  TENSION.    { "l
1a871 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 22 2c 20  oad_extension", 
1a872 20 20 20 20 31 2c 20 30 78 66 66 2c 20 53 51 4c      1, 0xff, SQL
1a873 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 6c 6f 61  ITE_UTF8, 0, loa
1a874 64 45 78 74 20 7d 2c 0a 20 20 20 20 7b 20 22 6c  dExt },.    { "l
1a875 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 22 2c 20  oad_extension", 
1a876 20 20 20 20 32 2c 20 30 78 66 66 2c 20 53 51 4c      2, 0xff, SQL
1a877 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 6c 6f 61  ITE_UTF8, 0, loa
1a878 64 45 78 74 20 7d 2c 0a 23 65 6e 64 69 66 0a 23  dExt },.#endif.#
1a879 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1a87a 54 0a 20 20 20 20 7b 20 22 72 61 6e 64 73 74 72  T.    { "randstr
1a87b 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
1a87c 20 32 2c 20 30 2c 20 20 20 20 53 51 4c 49 54 45   2, 0,    SQLITE
1a87d 5f 55 54 46 38 2c 20 30 2c 20 72 61 6e 64 53 74  _UTF8, 0, randSt
1a87e 72 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 74  r    },.    { "t
1a87f 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72 22 2c  est_destructor",
1a880 20 20 20 20 20 20 20 31 2c 20 30 78 66 66 2c 20         1, 0xff, 
1a881 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20  SQLITE_UTF8, 0, 
1a882 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72 7d  test_destructor}
1a883 2c 0a 20 20 20 20 7b 20 22 74 65 73 74 5f 64 65  ,.    { "test_de
1a884 73 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74 22 2c  structor_count",
1a885 20 30 2c 20 30 2c 20 20 20 20 53 51 4c 49 54 45   0, 0,    SQLITE
1a886 5f 55 54 46 38 2c 20 30 2c 20 74 65 73 74 5f 64  _UTF8, 0, test_d
1a887 65 73 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74 7d  estructor_count}
1a888 2c 0a 20 20 20 20 7b 20 22 74 65 73 74 5f 61 75  ,.    { "test_au
1a889 78 64 61 74 61 22 2c 20 20 20 20 20 20 20 20 20  xdata",         
1a88a 2d 31 2c 20 30 2c 20 20 20 20 53 51 4c 49 54 45  -1, 0,    SQLITE
1a88b 5f 55 54 46 38 2c 20 30 2c 20 74 65 73 74 5f 61  _UTF8, 0, test_a
1a88c 75 78 64 61 74 61 7d 2c 0a 20 20 20 20 7b 20 22  uxdata},.    { "
1a88d 74 65 73 74 5f 65 72 72 6f 72 22 2c 20 20 20 20  test_error",    
1a88e 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20          1, 0,   
1a88f 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
1a890 20 74 65 73 74 5f 65 72 72 6f 72 7d 2c 0a 23 65   test_error},.#e
1a891 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 73 74 61 74  ndif.  };.  stat
1a892 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
1a893 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d  {.    char *zNam
1a894 65 3b 0a 20 20 20 20 73 69 67 6e 65 64 20 63 68  e;.    signed ch
1a895 61 72 20 6e 41 72 67 3b 0a 20 20 20 20 75 38 20  ar nArg;.    u8 
1a896 61 72 67 54 79 70 65 3b 0a 20 20 20 20 75 38 20  argType;.    u8 
1a897 6e 65 65 64 43 6f 6c 6c 53 65 71 3b 0a 20 20 20  needCollSeq;.   
1a898 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
1a899 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
1a89a 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
1a89b 65 2a 2a 29 3b 0a 20 20 20 20 76 6f 69 64 20 28  e**);.    void (
1a89c 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 71 6c 69  *xFinalize)(sqli
1a89d 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20  te3_context*);. 
1a89e 20 7d 20 61 41 67 67 73 5b 5d 20 3d 20 7b 0a 20   } aAggs[] = {. 
1a89f 20 20 20 7b 20 22 6d 69 6e 22 2c 20 20 20 20 31     { "min",    1
1a8a0 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74  , 0, 1, minmaxSt
1a8a1 65 70 2c 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61  ep,   minMaxFina
1a8a2 6c 69 7a 65 20 7d 2c 0a 20 20 20 20 7b 20 22 6d  lize },.    { "m
1a8a3 61 78 22 2c 20 20 20 20 31 2c 20 31 2c 20 31 2c  ax",    1, 1, 1,
1a8a4 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 6d   minmaxStep,   m
1a8a5 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 7d 2c  inMaxFinalize },
1a8a6 0a 20 20 20 20 7b 20 22 73 75 6d 22 2c 20 20 20  .    { "sum",   
1a8a7 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65   1, 0, 0, sumSte
1a8a8 70 2c 20 20 20 20 20 20 73 75 6d 46 69 6e 61 6c  p,      sumFinal
1a8a9 69 7a 65 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20  ize    },.    { 
1a8aa 22 74 6f 74 61 6c 22 2c 20 20 31 2c 20 30 2c 20  "total",  1, 0, 
1a8ab 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20  0, sumStep,     
1a8ac 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 20 20   totalFinalize  
1a8ad 20 20 7d 2c 0a 20 20 20 20 7b 20 22 61 76 67 22    },.    { "avg"
1a8ae 2c 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75  ,    1, 0, 0, su
1a8af 6d 53 74 65 70 2c 20 20 20 20 20 20 61 76 67 46  mStep,      avgF
1a8b0 69 6e 61 6c 69 7a 65 20 20 20 20 7d 2c 0a 20 20  inalize    },.  
1a8b1 20 20 7b 20 22 63 6f 75 6e 74 22 2c 20 20 30 2c    { "count",  0,
1a8b2 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65 70   0, 0, countStep
1a8b3 2c 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69  ,    countFinali
1a8b4 7a 65 20 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f  ze  },.    { "co
1a8b5 75 6e 74 22 2c 20 20 31 2c 20 30 2c 20 30 2c 20  unt",  1, 0, 0, 
1a8b6 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 63 6f  countStep,    co
1a8b7 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 7d 2c 0a  untFinalize  },.
1a8b8 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20    };.  int i;.. 
1a8b9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65   for(i=0; i<size
1a8ba 6f 66 28 61 46 75 6e 63 73 29 2f 73 69 7a 65 6f  of(aFuncs)/sizeo
1a8bb 66 28 61 46 75 6e 63 73 5b 30 5d 29 3b 20 69 2b  f(aFuncs[0]); i+
1a8bc 2b 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 41  +){.    void *pA
1a8bd 72 67 3b 0a 20 20 20 20 75 38 20 61 72 67 54 79  rg;.    u8 argTy
1a8be 70 65 20 3d 20 61 46 75 6e 63 73 5b 69 5d 2e 61  pe = aFuncs[i].a
1a8bf 72 67 54 79 70 65 3b 0a 20 20 20 20 69 66 28 20  rgType;.    if( 
1a8c0 61 72 67 54 79 70 65 3d 3d 30 78 66 66 20 29 7b  argType==0xff ){
1a8c1 0a 20 20 20 20 20 20 70 41 72 67 20 3d 20 64 62  .      pArg = db
1a8c2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1a8c3 20 20 20 70 41 72 67 20 3d 20 28 76 6f 69 64 2a     pArg = (void*
1a8c4 29 28 69 6e 74 29 61 72 67 54 79 70 65 3b 0a 20  )(int)argType;. 
1a8c5 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1a8c6 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 61  CreateFunc(db, a
1a8c7 46 75 6e 63 73 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  Funcs[i].zName, 
1a8c8 61 46 75 6e 63 73 5b 69 5d 2e 6e 41 72 67 2c 0a  aFuncs[i].nArg,.
1a8c9 20 20 20 20 20 20 20 20 61 46 75 6e 63 73 5b 69          aFuncs[i
1a8ca 5d 2e 65 54 65 78 74 52 65 70 2c 20 70 41 72 67  ].eTextRep, pArg
1a8cb 2c 20 61 46 75 6e 63 73 5b 69 5d 2e 78 46 75 6e  , aFuncs[i].xFun
1a8cc 63 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  c, 0, 0);.    if
1a8cd 28 20 61 46 75 6e 63 73 5b 69 5d 2e 6e 65 65 64  ( aFuncs[i].need
1a8ce 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20 20  CollSeq ){.     
1a8cf 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 20   FuncDef *pFunc 
1a8d0 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e  = sqlite3FindFun
1a8d1 63 74 69 6f 6e 28 64 62 2c 20 61 46 75 6e 63 73  ction(db, aFuncs
1a8d2 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20  [i].zName, .    
1a8d3 20 20 20 20 20 20 73 74 72 6c 65 6e 28 61 46 75        strlen(aFu
1a8d4 6e 63 73 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 20 61  ncs[i].zName), a
1a8d5 46 75 6e 63 73 5b 69 5d 2e 6e 41 72 67 2c 20 61  Funcs[i].nArg, a
1a8d6 46 75 6e 63 73 5b 69 5d 2e 65 54 65 78 74 52 65  Funcs[i].eTextRe
1a8d7 70 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  p, 0);.      if(
1a8d8 20 70 46 75 6e 63 20 26 26 20 61 46 75 6e 63 73   pFunc && aFuncs
1a8d9 5b 69 5d 2e 6e 65 65 64 43 6f 6c 6c 53 65 71 20  [i].needCollSeq 
1a8da 29 7b 0a 20 20 20 20 20 20 20 20 70 46 75 6e 63  ){.        pFunc
1a8db 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20 3d 20  ->needCollSeq = 
1a8dc 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
1a8dd 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
1a8de 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41  ITE_OMIT_ALTERTA
1a8df 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 41 6c 74  BLE.  sqlite3Alt
1a8e0 65 72 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b  erFunctions(db);
1a8e1 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
1a8e2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53  SQLITE_OMIT_PARS
1a8e3 45 52 0a 20 20 73 71 6c 69 74 65 33 41 74 74 61  ER.  sqlite3Atta
1a8e4 63 68 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b  chFunctions(db);
1a8e5 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d  .#endif.  for(i=
1a8e6 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 41 67 67  0; i<sizeof(aAgg
1a8e7 73 29 2f 73 69 7a 65 6f 66 28 61 41 67 67 73 5b  s)/sizeof(aAggs[
1a8e8 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76  0]); i++){.    v
1a8e9 6f 69 64 20 2a 70 41 72 67 20 3d 20 28 76 6f 69  oid *pArg = (voi
1a8ea 64 2a 29 28 69 6e 74 29 61 41 67 67 73 5b 69 5d  d*)(int)aAggs[i]
1a8eb 2e 61 72 67 54 79 70 65 3b 0a 20 20 20 20 73 71  .argType;.    sq
1a8ec 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
1a8ed 64 62 2c 20 61 41 67 67 73 5b 69 5d 2e 7a 4e 61  db, aAggs[i].zNa
1a8ee 6d 65 2c 20 61 41 67 67 73 5b 69 5d 2e 6e 41 72  me, aAggs[i].nAr
1a8ef 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g, SQLITE_UTF8, 
1a8f0 0a 20 20 20 20 20 20 20 20 70 41 72 67 2c 20 30  .        pArg, 0
1a8f1 2c 20 61 41 67 67 73 5b 69 5d 2e 78 53 74 65 70  , aAggs[i].xStep
1a8f2 2c 20 61 41 67 67 73 5b 69 5d 2e 78 46 69 6e 61  , aAggs[i].xFina
1a8f3 6c 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 61  lize);.    if( a
1a8f4 41 67 67 73 5b 69 5d 2e 6e 65 65 64 43 6f 6c 6c  Aggs[i].needColl
1a8f5 53 65 71 20 29 7b 0a 20 20 20 20 20 20 46 75 6e  Seq ){.      Fun
1a8f6 63 44 65 66 20 2a 70 46 75 6e 63 20 3d 20 73 71  cDef *pFunc = sq
1a8f7 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
1a8f8 6e 28 20 64 62 2c 20 61 41 67 67 73 5b 69 5d 2e  n( db, aAggs[i].
1a8f9 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
1a8fa 20 73 74 72 6c 65 6e 28 61 41 67 67 73 5b 69 5d   strlen(aAggs[i]
1a8fb 2e 7a 4e 61 6d 65 29 2c 20 61 41 67 67 73 5b 69  .zName), aAggs[i
1a8fc 5d 2e 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55  ].nArg, SQLITE_U
1a8fd 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  TF8, 0);.      i
1a8fe 66 28 20 70 46 75 6e 63 20 26 26 20 61 41 67 67  f( pFunc && aAgg
1a8ff 73 5b 69 5d 2e 6e 65 65 64 43 6f 6c 6c 53 65 71  s[i].needCollSeq
1a900 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 75 6e   ){.        pFun
1a901 63 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20 3d  c->needCollSeq =
1a902 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1a903 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  }.  }.  sqlite3R
1a904 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46  egisterDateTimeF
1a905 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 20 20  unctions(db);.  
1a906 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
1a907 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 6e 74  ailed ){.    int
1a908 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76   rc = sqlite3_ov
1a909 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
1a90a 64 62 2c 20 22 4d 41 54 43 48 22 2c 20 32 29 3b  db, "MATCH", 2);
1a90b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
1a90c 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
1a90d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1a90e 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1a90f 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
1a910 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1a911 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  iled = 1;.    }.
1a912 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
1a913 45 5f 53 53 45 0a 20 20 28 76 6f 69 64 29 73 71  E_SSE.  (void)sq
1a914 6c 69 74 65 33 53 73 65 46 75 6e 63 74 69 6f 6e  lite3SseFunction
1a915 73 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a 23 69  s(db);.#endif.#i
1a916 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53 45  fdef SQLITE_CASE
1a917 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 0a  _SENSITIVE_LIKE.
1a918 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65    sqlite3Registe
1a919 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 64  rLikeFunctions(d
1a91a 62 2c 20 31 29 3b 0a 23 65 6c 73 65 0a 20 20 73  b, 1);.#else.  s
1a91b 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69  qlite3RegisterLi
1a91c 6b 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20  keFunctions(db, 
1a91d 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  0);.#endif.}../*
1a91e 0a 2a 2a 20 53 65 74 20 74 68 65 20 4c 49 4b 45  .** Set the LIKE
1a91f 4f 50 54 20 66 6c 61 67 20 6f 6e 20 74 68 65 20  OPT flag on the 
1a920 32 2d 61 72 67 75 6d 65 6e 74 20 66 75 6e 63 74  2-argument funct
1a921 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76  ion with the giv
1a922 65 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74  en name..*/.stat
1a923 69 63 20 76 6f 69 64 20 73 65 74 4c 69 6b 65 4f  ic void setLikeO
1a924 70 74 46 6c 61 67 28 73 71 6c 69 74 65 33 20 2a  ptFlag(sqlite3 *
1a925 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
1a926 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67 56  zName, int flagV
1a927 61 6c 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  al){.  FuncDef *
1a928 70 44 65 66 3b 0a 20 20 70 44 65 66 20 3d 20 73  pDef;.  pDef = s
1a929 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
1a92a 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73 74  on(db, zName, st
1a92b 72 6c 65 6e 28 7a 4e 61 6d 65 29 2c 20 32 2c 20  rlen(zName), 2, 
1a92c 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b  SQLITE_UTF8, 0);
1a92d 0a 20 20 69 66 28 20 70 44 65 66 20 29 7b 0a 20  .  if( pDef ){. 
1a92e 20 20 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 3d     pDef->flags =
1a92f 20 66 6c 61 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a   flagVal;.  }.}.
1a930 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
1a931 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 4c 49 4b  the built-in LIK
1a932 45 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74  E and GLOB funct
1a933 69 6f 6e 73 2e 20 20 54 68 65 20 63 61 73 65 53  ions.  The caseS
1a934 65 6e 73 69 74 69 76 65 0a 2a 2a 20 70 61 72 61  ensitive.** para
1a935 6d 65 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73  meter determines
1a936 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1a937 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
1a938 72 20 69 73 20 63 61 73 65 0a 2a 2a 20 73 65 6e  r is case.** sen
1a939 73 69 74 69 76 65 2e 20 20 47 4c 4f 42 20 69 73  sitive.  GLOB is
1a93a 20 61 6c 77 61 79 73 20 63 61 73 65 20 73 65 6e   always case sen
1a93b 73 69 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54  sitive..*/.SQLIT
1a93c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a93d 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69  qlite3RegisterLi
1a93e 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  keFunctions(sqli
1a93f 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 63 61 73  te3 *db, int cas
1a940 65 53 65 6e 73 69 74 69 76 65 29 7b 0a 20 20 73  eSensitive){.  s
1a941 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
1a942 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 69 66 28 20  o *pInfo;.  if( 
1a943 63 61 73 65 53 65 6e 73 69 74 69 76 65 20 29 7b  caseSensitive ){
1a944 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74  .    pInfo = (st
1a945 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
1a946 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a  *)&likeInfoAlt;.
1a947 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e    }else{.    pIn
1a948 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d  fo = (struct com
1a949 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49  pareInfo*)&likeI
1a94a 6e 66 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73  nfoNorm;.  }.  s
1a94b 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
1a94c 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 32 2c 20  (db, "like", 2, 
1a94d 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 70 49 6e  SQLITE_UTF8, pIn
1a94e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c  fo, likeFunc, 0,
1a94f 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 72   0);.  sqlite3Cr
1a950 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c 69  eateFunc(db, "li
1a951 6b 65 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55  ke", 3, SQLITE_U
1a952 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65  TF8, pInfo, like
1a953 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73  Func, 0, 0);.  s
1a954 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
1a955 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20  (db, "glob", 2, 
1a956 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 0a 20 20  SQLITE_UTF8, .  
1a957 20 20 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70      (struct comp
1a958 61 72 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e  areInfo*)&globIn
1a959 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c  fo, likeFunc, 0,
1a95a 30 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74  0);.  setLikeOpt
1a95b 46 6c 61 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c  Flag(db, "glob",
1a95c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
1a95d 45 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  E | SQLITE_FUNC_
1a95e 43 41 53 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65  CASE);.  setLike
1a95f 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b  OptFlag(db, "lik
1a960 65 22 2c 20 0a 20 20 20 20 20 20 63 61 73 65 53  e", .      caseS
1a961 65 6e 73 69 74 69 76 65 20 3f 20 28 53 51 4c 49  ensitive ? (SQLI
1a962 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53  TE_FUNC_LIKE | S
1a963 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29  QLITE_FUNC_CASE)
1a964 20 3a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c   : SQLITE_FUNC_L
1a965 49 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  IKE);.}../*.** p
1a966 45 78 70 72 20 70 6f 69 6e 74 73 20 74 6f 20 61  Expr points to a
1a967 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69  n expression whi
1a968 63 68 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  ch implements a 
1a969 66 75 6e 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a  function.  If.**
1a96a 20 69 74 20 69 73 20 61 70 70 72 6f 70 72 69 61   it is appropria
1a96b 74 65 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20  te to apply the 
1a96c 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f  LIKE optimizatio
1a96d 6e 20 74 6f 20 74 68 61 74 20 66 75 6e 63 74 69  n to that functi
1a96e 6f 6e 0a 2a 2a 20 74 68 65 6e 20 73 65 74 20 61  on.** then set a
1a96f 57 63 5b 30 5d 20 74 68 72 6f 75 67 68 20 61 57  Wc[0] through aW
1a970 63 5b 32 5d 20 74 6f 20 74 68 65 20 77 69 6c 64  c[2] to the wild
1a971 63 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20  card characters 
1a972 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52  and.** return TR
1a973 55 45 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63  UE.  If the func
1a974 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49  tion is not a LI
1a975 4b 45 2d 73 74 79 6c 65 20 66 75 6e 63 74 69 6f  KE-style functio
1a976 6e 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e  n then.** return
1a977 20 46 41 4c 53 45 2e 0a 2a 2f 0a 53 51 4c 49 54   FALSE..*/.SQLIT
1a978 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1a979 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74  lite3IsLikeFunct
1a97a 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ion(sqlite3 *db,
1a97b 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
1a97c 74 20 2a 70 49 73 4e 6f 63 61 73 65 2c 20 63 68  t *pIsNocase, ch
1a97d 61 72 20 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63  ar *aWc){.  Func
1a97e 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 69 66 28  Def *pDef;.  if(
1a97f 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46   pExpr->op!=TK_F
1a980 55 4e 43 54 49 4f 4e 20 7c 7c 20 21 70 45 78 70  UNCTION || !pExp
1a981 72 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  r->pList ){.    
1a982 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1a983 69 66 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  if( pExpr->pList
1a984 2d 3e 6e 45 78 70 72 21 3d 32 20 29 7b 0a 20 20  ->nExpr!=2 ){.  
1a985 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1a986 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
1a987 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  FindFunction(db,
1a988 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74   (char*)pExpr->t
1a989 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74  oken.z, pExpr->t
1a98a 6f 6b 65 6e 2e 6e 2c 20 32 2c 0a 20 20 20 20 20  oken.n, 2,.     
1a98b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a98c 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55          SQLITE_U
1a98d 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  TF8, 0);.  if( p
1a98e 44 65 66 3d 3d 30 20 7c 7c 20 28 70 44 65 66 2d  Def==0 || (pDef-
1a98f 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
1a990 46 55 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b  FUNC_LIKE)==0 ){
1a991 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
1a992 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 65 6d   }..  /* The mem
1a993 63 70 79 28 29 20 73 74 61 74 65 6d 65 6e 74 20  cpy() statement 
1a994 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
1a995 20 77 69 6c 64 63 61 72 64 20 63 68 61 72 61 63   wildcard charac
1a996 74 65 72 73 20 61 72 65 0a 20 20 2a 2a 20 74 68  ters are.  ** th
1a997 65 20 66 69 72 73 74 20 74 68 72 65 65 20 73 74  e first three st
1a998 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  atements in the 
1a999 63 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75  compareInfo stru
1a99a 63 74 75 72 65 2e 20 20 54 68 65 0a 20 20 2a 2a  cture.  The.  **
1a99b 20 61 73 73 65 72 74 73 28 29 20 74 68 61 74 20   asserts() that 
1a99c 66 6f 6c 6c 6f 77 20 76 65 72 69 66 79 20 74 68  follow verify th
1a99d 61 74 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20  at assumption.  
1a99e 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 61 57 63 2c  */.  memcpy(aWc,
1a99f 20 70 44 65 66 2d 3e 70 55 73 65 72 44 61 74 61   pDef->pUserData
1a9a0 2c 20 33 29 3b 0a 20 20 61 73 73 65 72 74 28 20  , 3);.  assert( 
1a9a1 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f  (char*)&likeInfo
1a9a2 41 6c 74 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c  Alt == (char*)&l
1a9a3 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68  ikeInfoAlt.match
1a9a4 41 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  All );.  assert(
1a9a5 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49   &((char*)&likeI
1a9a6 6e 66 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20 28 63  nfoAlt)[1] == (c
1a9a7 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c  har*)&likeInfoAl
1a9a8 74 2e 6d 61 74 63 68 4f 6e 65 20 29 3b 0a 20 20  t.matchOne );.  
1a9a9 61 73 73 65 72 74 28 20 26 28 28 63 68 61 72 2a  assert( &((char*
1a9aa 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 32  )&likeInfoAlt)[2
1a9ab 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b  ] == (char*)&lik
1a9ac 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 53 65  eInfoAlt.matchSe
1a9ad 74 20 29 3b 0a 20 20 2a 70 49 73 4e 6f 63 61 73  t );.  *pIsNocas
1a9ae 65 20 3d 20 28 70 44 65 66 2d 3e 66 6c 61 67 73  e = (pDef->flags
1a9af 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43   & SQLITE_FUNC_C
1a9b0 41 53 45 29 3d 3d 30 3b 0a 20 20 72 65 74 75 72  ASE)==0;.  retur
1a9b1 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  n 1;.}../*******
1a9b2 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
1a9b3 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  unc.c **********
1a9b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9b6 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1a9b7 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1a9b8 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a  le insert.c ****
1a9b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9bb 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1a9bc 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
1a9bd 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1a9be 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1a9bf 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1a9c0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1a9c1 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1a9c2 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1a9c3 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1a9c4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1a9c5 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1a9c6 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1a9c7 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1a9c8 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1a9c9 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1a9ca 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1a9cb 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1a9cc 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1a9cd 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1a9ce 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1a9cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1a9d3 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1a9d4 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
1a9d5 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
1a9d6 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
1a9d7 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20  er.** to handle 
1a9d8 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
1a9d9 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s in SQLite..**.
1a9da 2a 2a 20 24 49 64 3a 20 69 6e 73 65 72 74 2e 63  ** $Id: insert.c
1a9db 2c 76 20 31 2e 31 39 32 20 32 30 30 37 2f 30 39  ,v 1.192 2007/09
1a9dc 2f 30 33 20 31 37 3a 33 30 3a 30 37 20 64 61 6e  /03 17:30:07 dan
1a9dd 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
1a9de 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50 33 20  /../*.** Set P3 
1a9df 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
1a9e0 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70  ntly inserted op
1a9e1 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e  code to a column
1a9e2 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73 74 72   affinity.** str
1a9e3 69 6e 67 20 66 6f 72 20 69 6e 64 65 78 20 70 49  ing for index pI
1a9e4 64 78 2e 20 41 20 63 6f 6c 75 6d 6e 20 61 66 66  dx. A column aff
1a9e5 69 6e 69 74 79 20 73 74 72 69 6e 67 20 68 61 73  inity string has
1a9e6 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 0a 2a   one character.*
1a9e7 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  * for each colum
1a9e8 6e 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  n in the table, 
1a9e9 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
1a9ea 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
1a9eb 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20   column:.**.**  
1a9ec 43 68 61 72 61 63 74 65 72 20 20 20 20 20 20 43  Character      C
1a9ed 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a  olumn affinity.*
1a9ee 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *  -------------
1a9ef 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a9f0 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 20 20  -.**  'a'       
1a9f1 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 27 62       TEXT.**  'b
1a9f2 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 4f 4e  '            NON
1a9f3 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 20 20  E.**  'c'       
1a9f4 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a 20       NUMERIC.** 
1a9f5 20 27 64 27 20 20 20 20 20 20 20 20 20 20 20 20   'd'            
1a9f6 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 27 20  INTEGER.**  'e' 
1a9f7 20 20 20 20 20 20 20 20 20 20 20 52 45 41 4c 0a             REAL.
1a9f8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a9f9 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e  E void sqlite3In
1a9fa 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56  dexAffinityStr(V
1a9fb 64 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70  dbe *v, Index *p
1a9fc 49 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64  Idx){.  if( !pId
1a9fd 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20  x->zColAff ){.  
1a9fe 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
1a9ff 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66  ime a column aff
1aa00 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72  inity string for
1aa01 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e   a particular in
1aa02 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65  dex is.    ** re
1aa03 71 75 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c  quired, it is al
1aa04 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75  located and popu
1aa05 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74 20 69  lated here. It i
1aa06 73 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73  s then stored as
1aa07 0a 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72  .    ** a member
1aa08 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73 74   of the Index st
1aa09 72 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73  ructure for subs
1aa0a 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20  equent use..    
1aa0b 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f  **.    ** The co
1aa0c 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
1aa0d 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75  ring will eventu
1aa0e 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  ally be deleted 
1aa0f 62 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  by.    ** sqlite
1aa10 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68  DeleteIndex() wh
1aa11 65 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  en the Index str
1aa12 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73  ucture itself is
1aa13 20 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20   cleaned.    ** 
1aa14 75 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  up..    */.    i
1aa15 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20  nt n;.    Table 
1aa16 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54  *pTab = pIdx->pT
1aa17 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  able;.    sqlite
1aa18 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56  3 *db = sqlite3V
1aa19 64 62 65 44 62 28 76 29 3b 0a 20 20 20 20 70 49  dbeDb(v);.    pI
1aa1a 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63  dx->zColAff = (c
1aa1b 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  har *)sqlite3DbM
1aa1c 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 70 49  allocZero(db, pI
1aa1d 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a  dx->nColumn+1);.
1aa1e 20 20 20 20 69 66 28 20 21 70 49 64 78 2d 3e 7a      if( !pIdx->z
1aa1f 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20  ColAff ){.      
1aa20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
1aa21 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 49 64    for(n=0; n<pId
1aa22 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29  x->nColumn; n++)
1aa23 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 7a 43  {.      pIdx->zC
1aa24 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61 62 2d  olAff[n] = pTab-
1aa25 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f  >aCol[pIdx->aiCo
1aa26 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e 69 74  lumn[n]].affinit
1aa27 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64  y;.    }.    pId
1aa28 78 2d 3e 7a 43 6f 6c 41 66 66 5b 70 49 64 78 2d  x->zColAff[pIdx-
1aa29 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 27 5c 30 27  >nColumn] = '\0'
1aa2a 3b 0a 20 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65  ;.  }. .  sqlite
1aa2b 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
1aa2c 20 2d 31 2c 20 70 49 64 78 2d 3e 7a 43 6f 6c 41   -1, pIdx->zColA
1aa2d 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ff, 0);.}../*.**
1aa2e 20 53 65 74 20 50 33 20 6f 66 20 74 68 65 20 6d   Set P3 of the m
1aa2f 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73  ost recently ins
1aa30 65 72 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20  erted opcode to 
1aa31 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  a column affinit
1aa32 79 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20  y.** string for 
1aa33 74 61 62 6c 65 20 70 54 61 62 2e 20 41 20 63 6f  table pTab. A co
1aa34 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
1aa35 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61  ring has one cha
1aa36 72 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61  racter.** for ea
1aa37 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65  ch column indexe
1aa38 64 20 62 79 20 74 68 65 20 69 6e 64 65 78 2c 20  d by the index, 
1aa39 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
1aa3a 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
1aa3b 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a  .** column:.**.*
1aa3c 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 20 20  *  Character    
1aa3d 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74    Column affinit
1aa3e 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  y.**  ----------
1aa3f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1aa40 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20  ----.**  'a'    
1aa41 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20          TEXT.** 
1aa42 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20 20   'b'            
1aa43 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20  NONE.**  'c'    
1aa44 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a          NUMERIC.
1aa45 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 20 20  **  'd'         
1aa46 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27     INTEGER.**  '
1aa47 65 27 20 20 20 20 20 20 20 20 20 20 20 20 52 45  e'            RE
1aa48 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  AL.*/.SQLITE_PRI
1aa49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1aa4a 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74  3TableAffinitySt
1aa4b 72 28 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65  r(Vdbe *v, Table
1aa4c 20 2a 70 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68   *pTab){.  /* Th
1aa4d 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 63  e first time a c
1aa4e 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
1aa4f 74 72 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74  tring for a part
1aa50 69 63 75 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a  icular table.  *
1aa51 2a 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 69  * is required, i
1aa52 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  t is allocated a
1aa53 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72  nd populated her
1aa54 65 2e 20 49 74 20 69 73 20 74 68 65 6e 20 0a 20  e. It is then . 
1aa55 20 2a 2a 20 73 74 6f 72 65 64 20 61 73 20 61 20   ** stored as a 
1aa56 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 54 61  member of the Ta
1aa57 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f  ble structure fo
1aa58 72 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65  r subsequent use
1aa59 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
1aa5a 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
1aa5b 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e  string will even
1aa5c 74 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65  tually be delete
1aa5d 64 20 62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65  d by.  ** sqlite
1aa5e 33 44 65 6c 65 74 65 54 61 62 6c 65 28 29 20 77  3DeleteTable() w
1aa5f 68 65 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74  hen the Table st
1aa60 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69  ructure itself i
1aa61 73 20 63 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20  s cleaned up..  
1aa62 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e  */.  if( !pTab->
1aa63 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 63  zColAff ){.    c
1aa64 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20  har *zColAff;.  
1aa65 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c    int i;.    sql
1aa66 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
1aa67 65 33 56 64 62 65 44 62 28 76 29 3b 0a 0a 20 20  e3VdbeDb(v);..  
1aa68 20 20 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61    zColAff = (cha
1aa69 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  r *)sqlite3DbMal
1aa6a 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 70 54 61 62  locZero(db, pTab
1aa6b 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 69  ->nCol+1);.    i
1aa6c 66 28 20 21 7a 43 6f 6c 41 66 66 20 29 7b 0a 20  f( !zColAff ){. 
1aa6d 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
1aa6e 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   }..    for(i=0;
1aa6f 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
1aa70 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 41  ++){.      zColA
1aa71 66 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e 61 43  ff[i] = pTab->aC
1aa72 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3b 0a  ol[i].affinity;.
1aa73 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 41 66      }.    zColAf
1aa74 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d 20  f[pTab->nCol] = 
1aa75 27 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61 62 2d  '\0';..    pTab-
1aa76 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c 41  >zColAff = zColA
1aa77 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  ff;.  }..  sqlit
1aa78 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76  e3VdbeChangeP3(v
1aa79 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43 6f 6c  , -1, pTab->zCol
1aa7a 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Aff, 0);.}../*.*
1aa7b 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * Return non-zer
1aa7c 6f 20 69 66 20 53 45 4c 45 43 54 20 73 74 61 74  o if SELECT stat
1aa7d 65 6d 65 6e 74 20 70 20 6f 70 65 6e 73 20 74 68  ement p opens th
1aa7e 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f  e table with roo
1aa7f 74 70 61 67 65 0a 2a 2a 20 69 54 61 62 20 69 6e  tpage.** iTab in
1aa80 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 20   database iDb.  
1aa81 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
1aa82 73 65 65 20 69 66 20 61 20 73 74 61 74 65 6d 65  see if a stateme
1aa83 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 0a  nt of the form .
1aa84 2a 2a 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  ** "INSERT INTO 
1aa85 3c 69 44 62 2c 20 69 54 61 62 3e 20 53 45 4c 45  <iDb, iTab> SELE
1aa86 43 54 20 2e 2e 2e 22 20 63 61 6e 20 72 75 6e 20  CT ..." can run 
1aa87 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74 65  without using te
1aa88 6d 70 6f 72 61 72 79 0a 2a 2a 20 74 61 62 6c 65  mporary.** table
1aa89 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 73   for the results
1aa8a 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20   of the SELECT. 
1aa8b 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69  .**.** No checki
1aa8c 6e 67 20 69 73 20 64 6f 6e 65 20 66 6f 72 20 73  ng is done for s
1aa8d 75 62 2d 73 65 6c 65 63 74 73 20 74 68 61 74 20  ub-selects that 
1aa8e 61 72 65 20 70 61 72 74 20 6f 66 20 65 78 70 72  are part of expr
1aa8f 65 73 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74  essions..*/.stat
1aa90 69 63 20 69 6e 74 20 73 65 6c 65 63 74 52 65 61  ic int selectRea
1aa91 64 73 54 61 62 6c 65 28 53 65 6c 65 63 74 20 2a  dsTable(Select *
1aa92 70 2c 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  p, Schema *pSche
1aa93 6d 61 2c 20 69 6e 74 20 69 54 61 62 29 7b 0a 20  ma, int iTab){. 
1aa94 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
1aa95 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
1aa96 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 2d 3e 70  Item;.  if( p->p
1aa97 53 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Src==0 ) return 
1aa98 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  0;.  for(i=0, pI
1aa99 74 65 6d 3d 70 2d 3e 70 53 72 63 2d 3e 61 3b 20  tem=p->pSrc->a; 
1aa9a 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b  i<p->pSrc->nSrc;
1aa9b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
1aa9c 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70      if( pItem->p
1aa9d 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
1aa9e 69 66 28 20 73 65 6c 65 63 74 52 65 61 64 73 54  if( selectReadsT
1aa9f 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 53 65 6c  able(pItem->pSel
1aaa0 65 63 74 2c 20 70 53 63 68 65 6d 61 2c 20 69 54  ect, pSchema, iT
1aaa1 61 62 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ab) ) return 1;.
1aaa2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1aaa3 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 54 61 62   if( pItem->pTab
1aaa4 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68 65  ->pSchema==pSche
1aaa5 6d 61 20 26 26 20 70 49 74 65 6d 2d 3e 70 54 61  ma && pItem->pTa
1aaa6 62 2d 3e 74 6e 75 6d 3d 3d 69 54 61 62 20 29 20  b->tnum==iTab ) 
1aaa7 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1aaa8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
1aaa9 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1aaaa 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
1aaab 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  MENT./*.** Write
1aaac 20 6f 75 74 20 63 6f 64 65 20 74 6f 20 69 6e 69   out code to ini
1aaad 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 74 6f  tialize the auto
1aaae 69 6e 63 72 65 6d 65 6e 74 20 6c 6f 67 69 63 2e  increment logic.
1aaaf 20 20 54 68 69 73 20 63 6f 64 65 0a 2a 2a 20 6c    This code.** l
1aab0 6f 6f 6b 73 20 75 70 20 74 68 65 20 63 75 72 72  ooks up the curr
1aab1 65 6e 74 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e  ent autoincremen
1aab2 74 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 73  t value in the s
1aab3 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a 2a  qlite_sequence.*
1aab4 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  * table and stor
1aab5 65 73 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e  es that value in
1aab6 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20   a memory cell. 
1aab7 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   Code generated 
1aab8 62 79 0a 2a 2a 20 61 75 74 6f 49 6e 63 53 74 65  by.** autoIncSte
1aab9 70 28 29 20 77 69 6c 6c 20 6b 65 65 70 20 74 68  p() will keep th
1aaba 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 68  at memory cell h
1aabb 6f 6c 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65  olding the large
1aabc 73 74 0a 2a 2a 20 72 6f 77 69 64 20 76 61 6c 75  st.** rowid valu
1aabd 65 2e 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74  e.  Code generat
1aabe 65 64 20 62 79 20 61 75 74 6f 49 6e 63 45 6e 64  ed by autoIncEnd
1aabf 28 29 20 77 69 6c 6c 20 77 72 69 74 65 20 74 68  () will write th
1aac0 65 20 6e 65 77 0a 2a 2a 20 6c 61 72 67 65 73 74  e new.** largest
1aac1 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f   value of the co
1aac2 75 6e 74 65 72 20 62 61 63 6b 20 69 6e 74 6f 20  unter back into 
1aac3 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65  the sqlite_seque
1aac4 6e 63 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  nce table..**.**
1aac5 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
1aac6 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20  turns the index 
1aac7 6f 66 20 74 68 65 20 6d 65 6d 5b 5d 20 63 65 6c  of the mem[] cel
1aac8 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  l that contains.
1aac9 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72  ** the maximum r
1aaca 6f 77 69 64 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a  owid counter..**
1aacb 0a 2a 2a 20 54 77 6f 20 6d 65 6d 6f 72 79 20 63  .** Two memory c
1aacc 65 6c 6c 73 20 61 72 65 20 61 6c 6c 6f 63 61 74  ells are allocat
1aacd 65 64 2e 20 20 54 68 65 20 6e 65 78 74 20 6d 65  ed.  The next me
1aace 6d 6f 72 79 20 63 65 6c 6c 20 61 66 74 65 72 20  mory cell after 
1aacf 74 68 65 0a 2a 2a 20 6f 6e 65 20 72 65 74 75 72  the.** one retur
1aad0 6e 65 64 20 68 6f 6c 64 73 20 74 68 65 20 72 6f  ned holds the ro
1aad1 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65  wid in sqlite_se
1aad2 71 75 65 6e 63 65 20 77 68 65 72 65 20 77 65 20  quence where we 
1aad3 77 69 6c 6c 0a 2a 2a 20 77 72 69 74 65 20 62 61  will.** write ba
1aad4 63 6b 20 74 68 65 20 72 65 76 69 73 65 64 20 6d  ck the revised m
1aad5 61 78 69 6d 75 6d 20 72 6f 77 69 64 2e 0a 2a 2f  aximum rowid..*/
1aad6 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f  .static int auto
1aad7 49 6e 63 42 65 67 69 6e 28 0a 20 20 50 61 72 73  IncBegin(.  Pars
1aad8 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1aad9 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1aada 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
1aadb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1aadc 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
1aadd 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
1aade 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
1aadf 61 62 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ab         /* Th
1aae0 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 77  e table we are w
1aae1 72 69 74 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a  riting to */.){.
1aae2 20 20 69 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b    int memId = 0;
1aae3 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 61 75 74  .  if( pTab->aut
1aae4 6f 49 6e 63 20 29 7b 0a 20 20 20 20 56 64 62 65  oInc ){.    Vdbe
1aae5 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
1aae6 64 62 65 3b 0a 20 20 20 20 44 62 20 2a 70 44 62  dbe;.    Db *pDb
1aae7 20 3d 20 26 70 50 61 72 73 65 2d 3e 64 62 2d 3e   = &pParse->db->
1aae8 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 69 6e  aDb[iDb];.    in
1aae9 74 20 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d  t iCur = pParse-
1aaea 3e 6e 54 61 62 3b 0a 20 20 20 20 69 6e 74 20 61  >nTab;.    int a
1aaeb 64 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ddr;.    assert(
1aaec 20 76 20 29 3b 0a 20 20 20 20 61 64 64 72 20 3d   v );.    addr =
1aaed 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
1aaee 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
1aaef 6d 65 6d 49 64 20 3d 20 70 50 61 72 73 65 2d 3e  memId = pParse->
1aaf0 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72  nMem+1;.    pPar
1aaf1 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32 3b 0a 20  se->nMem += 2;. 
1aaf2 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61     sqlite3OpenTa
1aaf3 62 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75 72  ble(pParse, iCur
1aaf4 2c 20 69 44 62 2c 20 70 44 62 2d 3e 70 53 63 68  , iDb, pDb->pSch
1aaf5 65 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50  ema->pSeqTab, OP
1aaf6 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20  _OpenRead);.    
1aaf7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1aaf8 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
1aaf9 43 75 72 2c 20 61 64 64 72 2b 31 33 29 3b 0a 20  Cur, addr+13);. 
1aafa 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1aafb 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  dOp(v, OP_Column
1aafc 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20  , iCur, 0);.    
1aafd 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
1aafe 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
1aaff 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   0, pTab->zName,
1ab00 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
1ab01 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1ab02 4e 65 2c 20 30 78 31 30 30 2c 20 61 64 64 72 2b  Ne, 0x100, addr+
1ab03 31 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  12);.    sqlite3
1ab04 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1ab05 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 30 29 3b  Rowid, iCur, 0);
1ab06 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ab07 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53  AddOp(v, OP_MemS
1ab08 74 6f 72 65 2c 20 6d 65 6d 49 64 2d 31 2c 20 31  tore, memId-1, 1
1ab09 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1ab0a 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f  beAddOp(v, OP_Co
1ab0b 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 31 29 3b 0a  lumn, iCur, 1);.
1ab0c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ab0d 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74  ddOp(v, OP_MemSt
1ab0e 6f 72 65 2c 20 6d 65 6d 49 64 2c 20 31 29 3b 0a  ore, memId, 1);.
1ab0f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ab10 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  ddOp(v, OP_Goto,
1ab11 20 30 2c 20 61 64 64 72 2b 31 33 29 3b 0a 20 20   0, addr+13);.  
1ab12 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ab13 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  Op(v, OP_Next, i
1ab14 43 75 72 2c 20 61 64 64 72 2b 34 29 3b 0a 20 20  Cur, addr+4);.  
1ab15 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ab16 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  Op(v, OP_Close, 
1ab17 69 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  iCur, 0);.  }.  
1ab18 72 65 74 75 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a  return memId;.}.
1ab19 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74 68  ./*.** Update th
1ab1a 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  e maximum rowid 
1ab1b 66 6f 72 20 61 6e 20 61 75 74 6f 69 6e 63 72 65  for an autoincre
1ab1c 6d 65 6e 74 20 63 61 6c 63 75 6c 61 74 69 6f 6e  ment calculation
1ab1d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1ab1e 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20 63  tine should be c
1ab1f 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 74  alled when the t
1ab20 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
1ab21 68 6f 6c 64 73 20 61 0a 2a 2a 20 6e 65 77 20 72  holds a.** new r
1ab22 6f 77 69 64 20 74 68 61 74 20 69 73 20 61 62 6f  owid that is abo
1ab23 75 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  ut to be inserte
1ab24 64 2e 20 20 49 66 20 74 68 61 74 20 6e 65 77 20  d.  If that new 
1ab25 72 6f 77 69 64 20 69 73 0a 2a 2a 20 6c 61 72 67  rowid is.** larg
1ab26 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69  er than the maxi
1ab27 6d 75 6d 20 72 6f 77 69 64 20 69 6e 20 74 68 65  mum rowid in the
1ab28 20 6d 65 6d 49 64 20 6d 65 6d 6f 72 79 20 63 65   memId memory ce
1ab29 6c 6c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  ll, then the.** 
1ab2a 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 75  memory cell is u
1ab2b 70 64 61 74 65 64 2e 20 20 54 68 65 20 73 74 61  pdated.  The sta
1ab2c 63 6b 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  ck is unchanged.
1ab2d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1ab2e 61 75 74 6f 49 6e 63 53 74 65 70 28 50 61 72 73  autoIncStep(Pars
1ab2f 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6d  e *pParse, int m
1ab30 65 6d 49 64 29 7b 0a 20 20 69 66 28 20 6d 65 6d  emId){.  if( mem
1ab31 49 64 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  Id>0 ){.    sqli
1ab32 74 65 33 56 64 62 65 41 64 64 4f 70 28 70 50 61  te3VdbeAddOp(pPa
1ab33 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d  rse->pVdbe, OP_M
1ab34 65 6d 4d 61 78 2c 20 6d 65 6d 49 64 2c 20 30 29  emMax, memId, 0)
1ab35 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
1ab36 66 74 65 72 20 64 6f 69 6e 67 20 6f 6e 65 20 6f  fter doing one o
1ab37 72 20 6d 6f 72 65 20 69 6e 73 65 72 74 73 2c 20  r more inserts, 
1ab38 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69  the maximum rowi
1ab39 64 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69  d is stored.** i
1ab3a 6e 20 6d 65 6d 5b 6d 65 6d 49 64 5d 2e 20 20 47  n mem[memId].  G
1ab3b 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
1ab3c 77 72 69 74 65 20 74 68 69 73 20 76 61 6c 75 65  write this value
1ab3d 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 0a 2a   back into the.*
1ab3e 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  * the sqlite_seq
1ab3f 75 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  uence table..*/.
1ab40 73 74 61 74 69 63 20 76 6f 69 64 20 61 75 74 6f  static void auto
1ab41 49 6e 63 45 6e 64 28 0a 20 20 50 61 72 73 65 20  IncEnd(.  Parse 
1ab42 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
1ab43 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
1ab44 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ext */.  int iDb
1ab45 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ,           /* I
1ab46 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
1ab47 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
1ab48 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
1ab49 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 61 62  ab,       /* Tab
1ab4a 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72 74  le we are insert
1ab4b 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e  ing into */.  in
1ab4c 74 20 6d 65 6d 49 64 20 20 20 20 20 20 20 20 20  t memId         
1ab4d 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
1ab4e 68 6f 6c 64 69 6e 67 20 74 68 65 20 6d 61 78 69  holding the maxi
1ab4f 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a  mum rowid */.){.
1ab50 20 20 69 66 28 20 70 54 61 62 2d 3e 61 75 74 6f    if( pTab->auto
1ab51 49 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  Inc ){.    int i
1ab52 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Cur = pParse->nT
1ab53 61 62 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20  ab;.    Vdbe *v 
1ab54 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
1ab55 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26  .    Db *pDb = &
1ab56 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b  pParse->db->aDb[
1ab57 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 61 64  iDb];.    int ad
1ab58 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  dr;.    assert( 
1ab59 76 20 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20  v );.    addr = 
1ab5a 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
1ab5b 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73  ntAddr(v);.    s
1ab5c 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
1ab5d 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44  pParse, iCur, iD
1ab5e 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  b, pDb->pSchema-
1ab5f 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65  >pSeqTab, OP_Ope
1ab60 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 73 71 6c  nWrite);.    sql
1ab61 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1ab62 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d 65 6d   OP_MemLoad, mem
1ab63 49 64 2d 31 2c 20 30 29 3b 0a 20 20 20 20 73 71  Id-1, 0);.    sq
1ab64 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ab65 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 2d 31  , OP_NotNull, -1
1ab66 2c 20 61 64 64 72 2b 37 29 3b 0a 20 20 20 20 73  , addr+7);.    s
1ab67 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1ab68 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20 30 29  v, OP_Pop, 1, 0)
1ab69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1ab6a 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 77  eAddOp(v, OP_New
1ab6b 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 30 29 3b  Rowid, iCur, 0);
1ab6c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ab6d 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op3(v, OP_String
1ab6e 38 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a  8, 0, 0, pTab->z
1ab6f 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71  Name, 0);.    sq
1ab70 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ab71 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d 65  , OP_MemLoad, me
1ab72 6d 49 64 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  mId, 0);.    sql
1ab73 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1ab74 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
1ab75 32 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  2, 0);.    sqlit
1ab76 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ab77 50 5f 49 6e 73 65 72 74 2c 20 69 43 75 72 2c 20  P_Insert, iCur, 
1ab78 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a  OPFLAG_APPEND);.
1ab79 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ab7a 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  ddOp(v, OP_Close
1ab7b 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a  , iCur, 0);.  }.
1ab7c 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49 66  }.#else./*.** If
1ab7d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1ab7e 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64 65  OINCREMENT is de
1ab7f 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20  fined, then the 
1ab80 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a  three routines.*
1ab81 2a 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c 20  * above are all 
1ab82 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66 69  no-ops.*/.# defi
1ab83 6e 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28  ne autoIncBegin(
1ab84 41 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65 66  A,B,C) (0).# def
1ab85 69 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70 28  ine autoIncStep(
1ab86 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 61 75  A,B).# define au
1ab87 74 6f 49 6e 63 45 6e 64 28 41 2c 42 2c 43 2c 44  toIncEnd(A,B,C,D
1ab88 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ).#endif /* SQLI
1ab89 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
1ab8a 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a 20 46 6f  EMENT */.../* Fo
1ab8b 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
1ab8c 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n */.static int 
1ab8d 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e  xferOptimization
1ab8e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1ab8f 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
1ab90 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
1ab91 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20   Table *pDest,  
1ab92 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
1ab93 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72  ble we are inser
1ab94 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 53  ting into */.  S
1ab95 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
1ab96 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54       /* A SELECT
1ab97 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73   statement to us
1ab98 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f  e as the data so
1ab99 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  urce */.  int on
1ab9a 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20  Error,          
1ab9b 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65  /* How to handle
1ab9c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f   constraint erro
1ab9d 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 44  rs */.  int iDbD
1ab9e 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a  est           /*
1ab9f 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 66   The database of
1aba0 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a   pDest */.);../*
1aba1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1aba2 20 69 73 20 63 61 6c 6c 20 74 6f 20 68 61 6e 64   is call to hand
1aba3 6c 65 20 53 51 4c 20 6f 66 20 74 68 65 20 66 6f  le SQL of the fo
1aba4 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a 2a  llowing forms:.*
1aba5 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74 20 69  *.**    insert i
1aba6 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c 49 53  nto TABLE (IDLIS
1aba7 54 29 20 76 61 6c 75 65 73 28 45 58 50 52 4c 49  T) values(EXPRLI
1aba8 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74  ST).**    insert
1aba9 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c   into TABLE (IDL
1abaa 49 53 54 29 20 73 65 6c 65 63 74 0a 2a 2a 0a 2a  IST) select.**.*
1abab 2a 20 54 68 65 20 49 44 4c 49 53 54 20 66 6f 6c  * The IDLIST fol
1abac 6c 6f 77 69 6e 67 20 74 68 65 20 74 61 62 6c 65  lowing the table
1abad 20 6e 61 6d 65 20 69 73 20 61 6c 77 61 79 73 20   name is always 
1abae 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 6f 6d  optional.  If om
1abaf 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 61  itted,.** then a
1abb0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f 6c   list of all col
1abb1 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 74 61 62  umns for the tab
1abb2 6c 65 20 69 73 20 73 75 62 73 74 69 74 75 74 65  le is substitute
1abb3 64 2e 20 20 54 68 65 20 49 44 4c 49 53 54 0a 2a  d.  The IDLIST.*
1abb4 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65  * appears in the
1abb5 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61 6d 65 74   pColumn paramet
1abb6 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20 69 73 20  er.  pColumn is 
1abb7 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53 54 20 69  NULL if IDLIST i
1abb8 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a  s omitted..**.**
1abb9 20 54 68 65 20 70 4c 69 73 74 20 70 61 72 61 6d   The pList param
1abba 65 74 65 72 20 68 6f 6c 64 73 20 45 58 50 52 4c  eter holds EXPRL
1abbb 49 53 54 20 69 6e 20 74 68 65 20 66 69 72 73 74  IST in the first
1abbc 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 49 4e 53   form of the INS
1abbd 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ERT.** statement
1abbe 20 61 62 6f 76 65 2c 20 61 6e 64 20 70 53 65 6c   above, and pSel
1abbf 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20 20 46 6f  ect is NULL.  Fo
1abc0 72 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f 72  r the second for
1abc1 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20 4e  m, pList is.** N
1abc2 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65 63 74 20  ULL and pSelect 
1abc3 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1abc4 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74 65  the select state
1abc5 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 67 65 6e  ment used to gen
1abc6 65 72 61 74 65 0a 2a 2a 20 64 61 74 61 20 66 6f  erate.** data fo
1abc7 72 20 74 68 65 20 69 6e 73 65 72 74 2e 0a 2a 2a  r the insert..**
1abc8 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e  .** The code gen
1abc9 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77 73 20 6f  erated follows o
1abca 6e 65 20 6f 66 20 66 6f 75 72 20 74 65 6d 70 6c  ne of four templ
1abcb 61 74 65 73 2e 20 20 46 6f 72 20 61 20 73 69 6d  ates.  For a sim
1abcc 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74 20 77 69  ple.** select wi
1abcd 74 68 20 64 61 74 61 20 63 6f 6d 69 6e 67 20 66  th data coming f
1abce 72 6f 6d 20 61 20 56 41 4c 55 45 53 20 63 6c 61  rom a VALUES cla
1abcf 75 73 65 2c 20 74 68 65 20 63 6f 64 65 20 65 78  use, the code ex
1abd0 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63 65 20 73  ecutes.** once s
1abd1 74 72 61 69 67 68 74 20 64 6f 77 6e 20 74 68 72  traight down thr
1abd2 6f 75 67 68 2e 20 20 54 68 65 20 74 65 6d 70 6c  ough.  The templ
1abd3 61 74 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74  ate looks like t
1abd4 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
1abd5 20 20 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75     open write cu
1abd6 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20  rsor to <table> 
1abd7 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a  and its indices.
1abd8 2a 2a 20 20 20 20 20 20 20 20 20 70 75 74 73 20  **         puts 
1abd9 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 65 78  VALUES clause ex
1abda 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f 20 74  pressions onto t
1abdb 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20 20 20  he stack.**     
1abdc 20 20 20 20 77 72 69 74 65 20 74 68 65 20 72 65      write the re
1abdd 73 75 6c 74 69 6e 67 20 72 65 63 6f 72 64 20 69  sulting record i
1abde 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20  nto <table>.**  
1abdf 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 0a 2a         cleanup.*
1abe0 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 65 20 72  *.** The three r
1abe1 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c 61 74  emaining templat
1abe2 65 73 20 61 73 73 75 6d 65 20 74 68 65 20 73 74  es assume the st
1abe3 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68  atement is of th
1abe4 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 49  e form.**.**   I
1abe5 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c  NSERT INTO <tabl
1abe6 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a 2a 2a  e> SELECT ....**
1abe7 0a 2a 2a 20 49 66 20 74 68 65 20 53 45 4c 45 43  .** If the SELEC
1abe8 54 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20 74  T clause is of t
1abe9 68 65 20 72 65 73 74 72 69 63 74 65 64 20 66 6f  he restricted fo
1abea 72 6d 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f  rm "SELECT * FRO
1abeb 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a 2a 2a  M <table2>" -.**
1abec 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20   in other words 
1abed 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 70 75  if the SELECT pu
1abee 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20  lls all columns 
1abef 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61  from a single ta
1abf0 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 72 65  ble.** and there
1abf1 20 69 73 20 6e 6f 20 57 48 45 52 45 20 6f 72 20   is no WHERE or 
1abf2 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55 50 20 42  LIMIT or GROUP B
1abf3 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c  Y or ORDER BY cl
1abf4 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20 69 66  auses, and.** if
1abf5 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20 3c 74   <table2> and <t
1abf6 61 62 6c 65 31 3e 20 61 72 65 20 64 69 73 74 69  able1> are disti
1abf7 6e 63 74 20 74 61 62 6c 65 73 20 62 75 74 20 68  nct tables but h
1abf8 61 76 65 20 69 64 65 6e 74 69 63 61 6c 0a 2a 2a  ave identical.**
1abf9 20 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c 75 64   schemas, includ
1abfa 69 6e 67 20 61 6c 6c 20 74 68 65 20 73 61 6d 65  ing all the same
1abfb 20 69 6e 64 69 63 65 73 2c 20 74 68 65 6e 20 61   indices, then a
1abfc 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a   special optimiz
1abfd 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f  ation.** is invo
1abfe 6b 65 64 20 74 68 61 74 20 63 6f 70 69 65 73 20  ked that copies 
1abff 72 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d  raw records from
1ac00 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74   <table2> over t
1ac01 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a 20 53  o <table1>..** S
1ac02 65 65 20 74 68 65 20 78 66 65 72 4f 70 74 69 6d  ee the xferOptim
1ac03 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63 74 69  ization() functi
1ac04 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d 70 6c 65  on for the imple
1ac05 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69  mentation of thi
1ac06 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e 20 20  s.** template.  
1ac07 54 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f  This is the seco
1ac08 6e 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a  nd template..**.
1ac09 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20  **         open 
1ac0a 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 74  a write cursor t
1ac0b 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20  o <table>.**    
1ac0c 20 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20 63       open read c
1ac0d 75 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65 32  ursor on <table2
1ac0e 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72 61  >.**         tra
1ac0f 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64  nsfer all record
1ac10 73 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f 76  s in <table2> ov
1ac11 65 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a  er to <table>.**
1ac12 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63           close c
1ac13 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20  ursors.**       
1ac14 20 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78 20    foreach index 
1ac15 6f 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  on <table>.**   
1ac16 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77          open a w
1ac17 72 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20 74  rite cursor on t
1ac18 68 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65 78  he <table> index
1ac19 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70  .**           op
1ac1a 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72  en a read cursor
1ac1b 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f   on the correspo
1ac1c 6e 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20 69  nding <table2> i
1ac1d 6e 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20  ndex.**         
1ac1e 20 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72    transfer all r
1ac1f 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20  ecords from the 
1ac20 72 65 61 64 20 74 6f 20 74 68 65 20 77 72 69 74  read to the writ
1ac21 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20  e cursors.**    
1ac22 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72         close cur
1ac23 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  sors.**         
1ac24 65 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a  end foreach.**.*
1ac25 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 6d 70  * The third temp
1ac26 6c 61 74 65 20 69 73 20 66 6f 72 20 77 68 65 6e  late is for when
1ac27 20 74 68 65 20 73 65 63 6f 6e 64 20 74 65 6d 70   the second temp
1ac28 6c 61 74 65 20 64 6f 65 73 20 6e 6f 74 20 61 70  late does not ap
1ac29 70 6c 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53  ply.** and the S
1ac2a 45 4c 45 43 54 20 63 6c 61 75 73 65 20 64 6f 65  ELECT clause doe
1ac2b 73 20 6e 6f 74 20 72 65 61 64 20 66 72 6f 6d 20  s not read from 
1ac2c 3c 74 61 62 6c 65 3e 20 61 74 20 61 6e 79 20 74  <table> at any t
1ac2d 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65  ime..** The gene
1ac2e 72 61 74 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f  rated code follo
1ac2f 77 73 20 74 68 69 73 20 74 65 6d 70 6c 61 74 65  ws this template
1ac30 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
1ac31 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41  goto B.**      A
1ac32 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 20  : setup for the 
1ac33 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
1ac34 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20    loop over the 
1ac35 72 6f 77 73 20 69 6e 20 74 68 65 20 53 45 4c 45  rows in the SELE
1ac36 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
1ac37 67 6f 73 75 62 20 43 0a 2a 2a 20 20 20 20 20 20  gosub C.**      
1ac38 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20     end loop.**  
1ac39 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61         cleanup a
1ac3a 66 74 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a  fter the SELECT.
1ac3b 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  **         goto 
1ac3c 44 0a 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65  D.**      B: ope
1ac3d 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 74  n write cursor t
1ac3e 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64 20 69 74  o <table> and it
1ac3f 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 20 20 20  s indices.**    
1ac40 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20 20       goto A.**  
1ac41 20 20 20 20 43 3a 20 69 6e 73 65 72 74 20 74 68      C: insert th
1ac42 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20  e select result 
1ac43 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20  into <table>.** 
1ac44 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 0a 2a          return.*
1ac45 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e 75  *      D: cleanu
1ac46 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72  p.**.** The four
1ac47 74 68 20 74 65 6d 70 6c 61 74 65 20 69 73 20 75  th template is u
1ac48 73 65 64 20 69 66 20 74 68 65 20 69 6e 73 65 72  sed if the inser
1ac49 74 20 73 74 61 74 65 6d 65 6e 74 20 74 61 6b 65  t statement take
1ac4a 73 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 73 20  s its.** values 
1ac4b 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 62 75  from a SELECT bu
1ac4c 74 20 74 68 65 20 64 61 74 61 20 69 73 20 62 65  t the data is be
1ac4d 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74  ing inserted int
1ac4e 6f 20 61 20 74 61 62 6c 65 0a 2a 2a 20 74 68 61  o a table.** tha
1ac4f 74 20 69 73 20 61 6c 73 6f 20 72 65 61 64 20 61  t is also read a
1ac50 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 53 45  s part of the SE
1ac51 4c 45 43 54 2e 20 20 49 6e 20 74 68 65 20 74 68  LECT.  In the th
1ac52 69 72 64 20 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20  ird form,.** we 
1ac53 68 61 76 65 20 74 6f 20 75 73 65 20 61 20 69 6e  have to use a in
1ac54 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
1ac55 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65   to store the re
1ac56 73 75 6c 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20  sults of.** the 
1ac57 73 65 6c 65 63 74 2e 20 20 54 68 65 20 74 65 6d  select.  The tem
1ac58 70 6c 61 74 65 20 69 73 20 6c 69 6b 65 20 74 68  plate is like th
1ac59 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  is:.**.**       
1ac5a 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20    goto B.**     
1ac5b 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68   A: setup for th
1ac5c 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  e SELECT.**     
1ac5d 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68      loop over th
1ac5e 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  e tables in the 
1ac5f 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
1ac60 20 20 20 20 67 6f 73 75 62 20 43 0a 2a 2a 20 20      gosub C.**  
1ac61 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a         end loop.
1ac62 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
1ac63 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c  up after the SEL
1ac64 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 67  ECT.**         g
1ac65 6f 74 6f 20 44 0a 2a 2a 20 20 20 20 20 20 43 3a  oto D.**      C:
1ac66 20 69 6e 73 65 72 74 20 74 68 65 20 73 65 6c 65   insert the sele
1ac67 63 74 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 74  ct result into t
1ac68 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  he intermediate 
1ac69 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20  table.**        
1ac6a 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
1ac6b 42 3a 20 6f 70 65 6e 20 61 20 63 75 72 73 6f 72  B: open a cursor
1ac6c 20 74 6f 20 61 6e 20 69 6e 74 65 72 6d 65 64 69   to an intermedi
1ac6d 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  ate table.**    
1ac6e 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20 20       goto A.**  
1ac6f 20 20 20 20 44 3a 20 6f 70 65 6e 20 77 72 69 74      D: open writ
1ac70 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62  e cursor to <tab
1ac71 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69  le> and its indi
1ac72 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 6c  ces.**         l
1ac73 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 69 6e 74  oop over the int
1ac74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 0a  ermediate table.
1ac75 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 61  **           tra
1ac76 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f 72  nsfer values for
1ac77 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74  m intermediate t
1ac78 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65  able into <table
1ac79 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64  >.**         end
1ac7a 20 74 68 65 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20   the loop.**    
1ac7b 20 20 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a       cleanup.*/.
1ac7c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1ac7d 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73 65 72  oid sqlite3Inser
1ac7e 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
1ac7f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61  se,        /* Pa
1ac80 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
1ac81 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
1ac82 69 73 74 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ist,    /* Name 
1ac83 6f 66 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  of table into wh
1ac84 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 72  ich we are inser
1ac85 74 69 6e 67 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ting */.  ExprLi
1ac86 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20  st *pList,      
1ac87 2f 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65  /* List of value
1ac88 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  s to be inserted
1ac89 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
1ac8a 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41  elect,      /* A
1ac8b 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1ac8c 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20  t to use as the 
1ac8d 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20  data source */. 
1ac8e 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e   IdList *pColumn
1ac8f 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e  ,      /* Column
1ac90 20 6e 61 6d 65 73 20 63 6f 72 72 65 73 70 6f 6e   names correspon
1ac91 64 69 6e 67 20 74 6f 20 49 44 4c 49 53 54 2e 20  ding to IDLIST. 
1ac92 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
1ac93 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f             /* Ho
1ac94 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73  w to handle cons
1ac95 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f  traint errors */
1ac96 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  .){.  Table *pTa
1ac97 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  b;          /* T
1ac98 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65  he table to inse
1ac99 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 68 61  rt into */.  cha
1ac9a 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20 20  r *zTab;        
1ac9b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
1ac9c 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  e table into whi
1ac9d 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74  ch we are insert
1ac9e 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ing */.  const c
1ac9f 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 2f  har *zDb;      /
1aca0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
1aca1 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74  tabase holding t
1aca2 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  his table */.  i
1aca3 6e 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20 20 20  nt i, j, idx;   
1aca4 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1aca5 6e 74 65 72 73 20 2a 2f 0a 20 20 56 64 62 65 20  nters */.  Vdbe 
1aca6 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
1aca7 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
1aca8 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74  e into this virt
1aca9 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20  ual machine */. 
1acaa 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
1acab 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
1acac 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63  oping over indic
1acad 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  es of the table 
1acae 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
1acaf 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ;          /* Nu
1acb0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1acb1 69 6e 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  in the data */. 
1acb2 20 69 6e 74 20 62 61 73 65 20 3d 20 30 3b 20 20   int base = 0;  
1acb3 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43         /* VDBE C
1acb4 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
1acb5 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69   pTab */.  int i
1acb6 43 6f 6e 74 3d 30 2c 69 42 72 65 61 6b 3d 30 3b  Cont=0,iBreak=0;
1acb7 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 61 6e   /* Beginning an
1acb8 64 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f  d end of the loo
1acb9 70 20 6f 76 65 72 20 73 72 63 54 61 62 20 2a 2f  p over srcTab */
1acba 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
1acbb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1acbc 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  main database st
1acbd 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74  ructure */.  int
1acbe 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b   keyColumn = -1;
1acbf 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 68 61     /* Column tha
1acc0 74 20 69 73 20 74 68 65 20 49 4e 54 45 47 45 52  t is the INTEGER
1acc1 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a   PRIMARY KEY */.
1acc2 20 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b    int endOfLoop;
1acc3 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c          /* Label
1acc4 20 66 6f 72 20 74 68 65 20 65 6e 64 20 6f 66 20   for the end of 
1acc5 74 68 65 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f  the insertion lo
1acc6 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 54  op */.  int useT
1acc7 65 6d 70 54 61 62 6c 65 20 3d 20 30 3b 20 2f 2a  empTable = 0; /*
1acc8 20 53 74 6f 72 65 20 53 45 4c 45 43 54 20 72 65   Store SELECT re
1acc9 73 75 6c 74 73 20 69 6e 20 69 6e 74 65 72 6d 65  sults in interme
1acca 64 69 61 74 65 20 74 61 62 6c 65 20 2a 2f 0a 20  diate table */. 
1accb 20 69 6e 74 20 73 72 63 54 61 62 20 3d 20 30 3b   int srcTab = 0;
1accc 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 63         /* Data c
1accd 6f 6d 65 73 20 66 72 6f 6d 20 74 68 69 73 20 74  omes from this t
1acce 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20  emporary cursor 
1accf 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20  if >=0 */.  int 
1acd0 69 53 65 6c 65 63 74 4c 6f 6f 70 20 3d 20 30 3b  iSelectLoop = 0;
1acd1 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
1acd2 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65 6d  code that implem
1acd3 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43 54 20  ents the SELECT 
1acd4 2a 2f 0a 20 20 69 6e 74 20 69 43 6c 65 61 6e 75  */.  int iCleanu
1acd5 70 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 64  p = 0;     /* Ad
1acd6 64 72 65 73 73 20 6f 66 20 74 68 65 20 63 6c 65  dress of the cle
1acd7 61 6e 75 70 20 63 6f 64 65 20 2a 2f 0a 20 20 69  anup code */.  i
1acd8 6e 74 20 69 49 6e 73 65 72 74 42 6c 6f 63 6b 20  nt iInsertBlock 
1acd9 3d 20 30 3b 20 2f 2a 20 41 64 64 72 65 73 73 20  = 0; /* Address 
1acda 6f 66 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  of the subroutin
1acdb 65 20 75 73 65 64 20 74 6f 20 69 6e 73 65 72 74  e used to insert
1acdc 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 69   data */.  int i
1acdd 43 6e 74 4d 65 6d 20 3d 20 30 3b 20 20 20 20 20  CntMem = 0;     
1acde 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
1acdf 75 73 65 64 20 66 6f 72 20 74 68 65 20 72 6f 77  used for the row
1ace0 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
1ace1 74 20 6e 65 77 49 64 78 20 3d 20 2d 31 3b 20 20  t newIdx = -1;  
1ace2 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 6f      /* Cursor fo
1ace3 72 20 74 68 65 20 4e 45 57 20 74 61 62 6c 65 20  r the NEW table 
1ace4 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20  */.  Db *pDb;   
1ace5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1ace6 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
1ace7 69 6e 69 6e 67 20 74 61 62 6c 65 20 62 65 69 6e  ining table bein
1ace8 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
1ace9 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 65 72  */.  int counter
1acea 4d 65 6d 20 3d 20 30 3b 20 20 20 2f 2a 20 4d 65  Mem = 0;   /* Me
1aceb 6d 6f 72 79 20 63 65 6c 6c 20 68 6f 6c 64 69 6e  mory cell holdin
1acec 67 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20  g AUTOINCREMENT 
1aced 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
1acee 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 30 3b   appendFlag = 0;
1acef 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
1acf0 65 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65  e insert is like
1acf1 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
1acf2 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  nd */.  int iDb;
1acf3 0a 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20  ..  int nHidden 
1acf4 3d 20 30 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  = 0;..#ifndef SQ
1acf5 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1acf6 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20  R.  int isView; 
1acf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1acf8 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65 6d  /* True if attem
1acf9 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72 74 20  pting to insert 
1acfa 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f 0a 20  into a view */. 
1acfb 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f 65 78   int triggers_ex
1acfc 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ist = 0;     /* 
1acfd 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61 72  True if there ar
1acfe 65 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74  e FOR EACH ROW t
1acff 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69  riggers */.#endi
1ad00 66 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  f..  db = pParse
1ad01 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72  ->db;.  if( pPar
1ad02 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
1ad03 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1ad04 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
1ad05 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
1ad06 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  /* Locate the ta
1ad07 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
1ad08 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74  e will be insert
1ad09 69 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74  ing new informat
1ad0a 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ion..  */.  asse
1ad0b 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  rt( pTabList->nS
1ad0c 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61 62 20  rc==1 );.  zTab 
1ad0d 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d  = pTabList->a[0]
1ad0e 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 7a 54  .zName;.  if( zT
1ad0f 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20 69 6e 73  ab==0 ) goto ins
1ad10 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70  ert_cleanup;.  p
1ad11 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
1ad12 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
1ad13 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  e, pTabList);.  
1ad14 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20  if( pTab==0 ){. 
1ad15 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
1ad16 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 44  leanup;.  }.  iD
1ad17 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1ad18 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
1ad19 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
1ad1a 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e  ssert( iDb<db->n
1ad1b 44 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  Db );.  pDb = &d
1ad1c 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 7a  b->aDb[iDb];.  z
1ad1d 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b  Db = pDb->zName;
1ad1e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
1ad1f 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
1ad20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 70  SQLITE_INSERT, p
1ad21 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a  Tab->zName, 0, z
1ad22 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  Db) ){.    goto 
1ad23 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
1ad24 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65    }..  /* Figure
1ad25 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20   out if we have 
1ad26 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64  any triggers and
1ad27 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
1ad28 69 6e 67 0a 20 20 2a 2a 20 69 6e 73 65 72 74 65  ing.  ** inserte
1ad29 64 20 69 6e 74 6f 20 69 73 20 61 20 76 69 65 77  d into is a view
1ad2a 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
1ad2b 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1ad2c 52 0a 20 20 74 72 69 67 67 65 72 73 5f 65 78 69  R.  triggers_exi
1ad2d 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  st = sqlite3Trig
1ad2e 67 65 72 73 45 78 69 73 74 28 70 50 61 72 73 65  gersExist(pParse
1ad2f 2c 20 70 54 61 62 2c 20 54 4b 5f 49 4e 53 45 52  , pTab, TK_INSER
1ad30 54 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77 20  T, 0);.  isView 
1ad31 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21  = pTab->pSelect!
1ad32 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  =0;.#else.# defi
1ad33 6e 65 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  ne triggers_exis
1ad34 74 20 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56  t 0.# define isV
1ad35 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23 69 66  iew 0.#endif.#if
1ad36 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ad37 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56  VIEW.# undef isV
1ad38 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56  iew.# define isV
1ad39 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20  iew 0.#endif..  
1ad3a 2f 2a 20 45 6e 73 75 72 65 20 74 68 61 74 3a 0a  /* Ensure that:.
1ad3b 20 20 2a 20 20 28 61 29 20 74 68 65 20 74 61 62    *  (a) the tab
1ad3c 6c 65 20 69 73 20 6e 6f 74 20 72 65 61 64 2d 6f  le is not read-o
1ad3d 6e 6c 79 2c 20 0a 20 20 2a 20 20 28 62 29 20 74  nly, .  *  (b) t
1ad3e 68 61 74 20 69 66 20 69 74 20 69 73 20 61 20 76  hat if it is a v
1ad3f 69 65 77 20 74 68 65 6e 20 4f 4e 20 49 4e 53 45  iew then ON INSE
1ad40 52 54 20 74 72 69 67 67 65 72 73 20 65 78 69 73  RT triggers exis
1ad41 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  t.  */.  if( sql
1ad42 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70  ite3IsReadOnly(p
1ad43 50 61 72 73 65 2c 20 70 54 61 62 2c 20 74 72 69  Parse, pTab, tri
1ad44 67 67 65 72 73 5f 65 78 69 73 74 29 20 29 7b 0a  ggers_exist) ){.
1ad45 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
1ad46 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
1ad47 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 29  ssert( pTab!=0 )
1ad48 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20  ;..  /* If pTab 
1ad49 69 73 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77  is really a view
1ad4a 2c 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 68  , make sure it h
1ad4b 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  as been initiali
1ad4c 7a 65 64 2e 0a 20 20 2a 2a 20 56 69 65 77 47 65  zed..  ** ViewGe
1ad4d 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 69  tColumnNames() i
1ad4e 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 70 54 61  s a no-op if pTa
1ad4f 62 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20  b is not a view 
1ad50 28 6f 72 20 76 69 72 74 75 61 6c 20 0a 20 20 2a  (or virtual .  *
1ad51 2a 20 6d 6f 64 75 6c 65 20 74 61 62 6c 65 29 2e  * module table).
1ad52 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
1ad53 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
1ad54 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
1ad55 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ab) ){.    goto 
1ad56 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
1ad57 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61    }..  /* Alloca
1ad58 74 65 20 61 20 56 44 42 45 0a 20 20 2a 2f 0a 20  te a VDBE.  */. 
1ad59 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
1ad5a 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
1ad5b 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 69  f( v==0 ) goto i
1ad5c 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
1ad5d 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
1ad5e 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65 33  ted==0 ) sqlite3
1ad5f 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73  VdbeCountChanges
1ad60 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65  (v);.  sqlite3Be
1ad61 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
1ad62 6e 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  n(pParse, pSelec
1ad63 74 20 7c 7c 20 74 72 69 67 67 65 72 73 5f 65 78  t || triggers_ex
1ad64 69 73 74 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a  ist, iDb);..  /*
1ad65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 72 6f   if there are ro
1ad66 77 20 74 72 69 67 67 65 72 73 2c 20 61 6c 6c 6f  w triggers, allo
1ad67 63 61 74 65 20 61 20 74 65 6d 70 20 74 61 62 6c  cate a temp tabl
1ad68 65 20 66 6f 72 20 6e 65 77 2e 2a 20 72 65 66 65  e for new.* refe
1ad69 72 65 6e 63 65 73 2e 20 2a 2f 0a 20 20 69 66 28  rences. */.  if(
1ad6a 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20   triggers_exist 
1ad6b 29 7b 0a 20 20 20 20 6e 65 77 49 64 78 20 3d 20  ){.    newIdx = 
1ad6c 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
1ad6d 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
1ad6e 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50  ITE_OMIT_XFER_OP
1ad6f 54 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74  T.  /* If the st
1ad70 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68  atement is of th
1ad71 65 20 66 6f 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a  e form.  **.  **
1ad72 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e         INSERT IN
1ad73 54 4f 20 3c 74 61 62 6c 65 31 3e 20 53 45 4c 45  TO <table1> SELE
1ad74 43 54 20 2a 20 46 52 4f 4d 20 3c 74 61 62 6c 65  CT * FROM <table
1ad75 32 3e 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  2>;.  **.  ** Th
1ad76 65 6e 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d  en special optim
1ad77 69 7a 61 74 69 6f 6e 73 20 63 61 6e 20 62 65 20  izations can be 
1ad78 61 70 70 6c 69 65 64 20 74 68 61 74 20 6d 61 6b  applied that mak
1ad79 65 20 74 68 65 20 74 72 61 6e 73 66 65 72 0a 20  e the transfer. 
1ad7a 20 2a 2a 20 76 65 72 79 20 66 61 73 74 20 61 6e   ** very fast an
1ad7b 64 20 77 68 69 63 68 20 72 65 64 75 63 65 20 66  d which reduce f
1ad7c 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  ragmentation of 
1ad7d 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20  indices..  */.  
1ad7e 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26  if( pColumn==0 &
1ad7f 26 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69  & xferOptimizati
1ad80 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  on(pParse, pTab,
1ad81 20 70 53 65 6c 65 63 74 2c 20 6f 6e 45 72 72 6f   pSelect, onErro
1ad82 72 2c 20 69 44 62 29 20 29 7b 0a 20 20 20 20 61  r, iDb) ){.    a
1ad83 73 73 65 72 74 28 20 21 74 72 69 67 67 65 72 73  ssert( !triggers
1ad84 5f 65 78 69 73 74 20 29 3b 0a 20 20 20 20 61 73  _exist );.    as
1ad85 73 65 72 74 28 20 70 4c 69 73 74 3d 3d 30 20 29  sert( pList==0 )
1ad86 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  ;.    goto inser
1ad87 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23  t_cleanup;.  }.#
1ad88 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1ad89 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f  OMIT_XFER_OPT */
1ad8a 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
1ad8b 73 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45  s an AUTOINCREME
1ad8c 4e 54 20 74 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75  NT table, look u
1ad8d 70 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e  p the sequence n
1ad8e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 20 20 2a  umber in the.  *
1ad8f 2a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  * sqlite_sequenc
1ad90 65 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  e table and stor
1ad91 65 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63  e it in memory c
1ad92 65 6c 6c 20 63 6f 75 6e 74 65 72 4d 65 6d 2e 20  ell counterMem. 
1ad93 20 41 6c 73 6f 0a 20 20 2a 2a 20 72 65 6d 65 6d   Also.  ** remem
1ad94 62 65 72 20 74 68 65 20 72 6f 77 69 64 20 6f 66  ber the rowid of
1ad95 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
1ad96 65 6e 63 65 20 74 61 62 6c 65 20 65 6e 74 72 79  ence table entry
1ad97 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 0a   in memory cell.
1ad98 20 20 2a 2a 20 63 6f 75 6e 74 65 72 52 6f 77 69    ** counterRowi
1ad99 64 2e 0a 20 20 2a 2f 0a 20 20 63 6f 75 6e 74 65  d..  */.  counte
1ad9a 72 4d 65 6d 20 3d 20 61 75 74 6f 49 6e 63 42 65  rMem = autoIncBe
1ad9b 67 69 6e 28 70 50 61 72 73 65 2c 20 69 44 62 2c  gin(pParse, iDb,
1ad9c 20 70 54 61 62 29 3b 0a 0a 20 20 2f 2a 20 46 69   pTab);..  /* Fi
1ad9d 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
1ad9e 79 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74  y columns of dat
1ad9f 61 20 61 72 65 20 73 75 70 70 6c 69 65 64 2e 20  a are supplied. 
1ada0 20 49 66 20 74 68 65 20 64 61 74 61 0a 20 20 2a   If the data.  *
1ada1 2a 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d  * is coming from
1ada2 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
1ada3 65 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 73  ent, then this s
1ada4 74 65 70 20 61 6c 73 6f 20 67 65 6e 65 72 61 74  tep also generat
1ada5 65 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 65 20  es.  ** all the 
1ada6 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  code to implemen
1ada7 74 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  t the SELECT sta
1ada8 74 65 6d 65 6e 74 20 61 6e 64 20 69 6e 76 6f 6b  tement and invok
1ada9 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 0a 20  e a subroutine. 
1adaa 20 2a 2a 20 74 6f 20 70 72 6f 63 65 73 73 20 65   ** to process e
1adab 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 72  ach row of the r
1adac 65 73 75 6c 74 2e 20 28 54 65 6d 70 6c 61 74 65  esult. (Template
1adad 20 32 2e 29 20 49 66 20 74 68 65 20 53 45 4c 45   2.) If the SELE
1adae 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  CT.  ** statemen
1adaf 74 20 75 73 65 73 20 74 68 65 20 74 68 65 20 74  t uses the the t
1adb0 61 62 6c 65 20 74 68 61 74 20 69 73 20 62 65 69  able that is bei
1adb1 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ng inserted into
1adb2 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20  , then the.  ** 
1adb3 73 75 62 72 6f 75 74 69 6e 65 20 69 73 20 61 6c  subroutine is al
1adb4 73 6f 20 63 6f 64 65 64 20 68 65 72 65 2e 20 20  so coded here.  
1adb5 54 68 61 74 20 73 75 62 72 6f 75 74 69 6e 65 20  That subroutine 
1adb6 73 74 6f 72 65 73 20 74 68 65 20 53 45 4c 45 43  stores the SELEC
1adb7 54 0a 20 20 2a 2a 20 72 65 73 75 6c 74 73 20 69  T.  ** results i
1adb8 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  n a temporary ta
1adb9 62 6c 65 2e 20 28 54 65 6d 70 6c 61 74 65 20 33  ble. (Template 3
1adba 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  .).  */.  if( pS
1adbb 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20  elect ){.    /* 
1adbc 44 61 74 61 20 69 73 20 63 6f 6d 69 6e 67 20 66  Data is coming f
1adbd 72 6f 6d 20 61 20 53 45 4c 45 43 54 2e 20 20 47  rom a SELECT.  G
1adbe 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
1adbf 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 53  implement that S
1adc0 45 4c 45 43 54 0a 20 20 20 20 2a 2f 0a 20 20 20  ELECT.    */.   
1adc1 20 69 6e 74 20 72 63 2c 20 69 49 6e 69 74 43 6f   int rc, iInitCo
1adc2 64 65 3b 0a 20 20 20 20 69 49 6e 69 74 43 6f 64  de;.    iInitCod
1adc3 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  e = sqlite3VdbeA
1adc4 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  ddOp(v, OP_Goto,
1adc5 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 53 65 6c   0, 0);.    iSel
1adc6 65 63 74 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65  ectLoop = sqlite
1adc7 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
1adc8 28 76 29 3b 0a 20 20 20 20 69 49 6e 73 65 72 74  (v);.    iInsert
1adc9 42 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 56  Block = sqlite3V
1adca 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1adcb 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65  ..    /* Resolve
1adcc 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73   the expressions
1adcd 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73   in the SELECT s
1adce 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 65 78 65  tatement and exe
1adcf 63 75 74 65 20 69 74 2e 20 2a 2f 0a 20 20 20 20  cute it. */.    
1add0 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  rc = sqlite3Sele
1add1 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ct(pParse, pSele
1add2 63 74 2c 20 53 52 54 5f 53 75 62 72 6f 75 74 69  ct, SRT_Subrouti
1add3 6e 65 2c 20 69 49 6e 73 65 72 74 42 6c 6f 63 6b  ne, iInsertBlock
1add4 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20 69  ,0,0,0,0);.    i
1add5 66 28 20 72 63 20 7c 7c 20 70 50 61 72 73 65 2d  f( rc || pParse-
1add6 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
1add7 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1add8 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
1add9 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 0a 20  leanup;.    }.. 
1adda 20 20 20 69 43 6c 65 61 6e 75 70 20 3d 20 73 71     iCleanup = sq
1addb 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1addc 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  el(v);.    sqlit
1addd 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1adde 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43 6c 65 61  P_Goto, 0, iClea
1addf 6e 75 70 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nup);.    assert
1ade0 28 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  ( pSelect->pELis
1ade1 74 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e  t );.    nColumn
1ade2 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69   = pSelect->pELi
1ade3 73 74 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20 20 20  st->nExpr;..    
1ade4 2f 2a 20 53 65 74 20 75 73 65 54 65 6d 70 54 61  /* Set useTempTa
1ade5 62 6c 65 20 74 6f 20 54 52 55 45 20 69 66 20 74  ble to TRUE if t
1ade6 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1ade7 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1ade8 74 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20  t.    ** should 
1ade9 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
1adea 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
1adeb 65 2e 20 20 53 65 74 20 74 6f 20 46 41 4c 53 45  e.  Set to FALSE
1adec 20 69 66 20 65 61 63 68 0a 20 20 20 20 2a 2a 20   if each.    ** 
1aded 72 6f 77 20 6f 66 20 74 68 65 20 53 45 4c 45 43  row of the SELEC
1adee 54 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e  T can be written
1adef 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 74   directly into t
1adf0 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 2e  he result table.
1adf1 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41  .    **.    ** A
1adf2 20 74 65 6d 70 20 74 61 62 6c 65 20 6d 75 73 74   temp table must
1adf3 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65 20   be used if the 
1adf4 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61  table being upda
1adf5 74 65 64 20 69 73 20 61 6c 73 6f 20 6f 6e 65 0a  ted is also one.
1adf6 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61      ** of the ta
1adf7 62 6c 65 73 20 62 65 69 6e 67 20 72 65 61 64 20  bles being read 
1adf8 62 79 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  by the SELECT st
1adf9 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 75  atement.  Also u
1adfa 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 74 65 6d  se a .    ** tem
1adfb 70 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 63  p table in the c
1adfc 61 73 65 20 6f 66 20 72 6f 77 20 74 72 69 67 67  ase of row trigg
1adfd 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ers..    */.    
1adfe 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69  if( triggers_exi
1adff 73 74 20 7c 7c 20 73 65 6c 65 63 74 52 65 61 64  st || selectRead
1ae00 73 54 61 62 6c 65 28 70 53 65 6c 65 63 74 2c 70  sTable(pSelect,p
1ae01 54 61 62 2d 3e 70 53 63 68 65 6d 61 2c 70 54 61  Tab->pSchema,pTa
1ae02 62 2d 3e 74 6e 75 6d 29 20 29 7b 0a 20 20 20 20  b->tnum) ){.    
1ae03 20 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d    useTempTable =
1ae04 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69   1;.    }..    i
1ae05 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
1ae06 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65  ){.      /* Gene
1ae07 72 61 74 65 20 74 68 65 20 73 75 62 72 6f 75 74  rate the subrout
1ae08 69 6e 65 20 74 68 61 74 20 53 45 4c 45 43 54 20  ine that SELECT 
1ae09 63 61 6c 6c 73 20 74 6f 20 70 72 6f 63 65 73 73  calls to process
1ae0a 20 65 61 63 68 20 72 6f 77 20 6f 66 0a 20 20 20   each row of.   
1ae0b 20 20 20 2a 2a 20 74 68 65 20 72 65 73 75 6c 74     ** the result
1ae0c 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72 65 73  .  Store the res
1ae0d 75 6c 74 20 69 6e 20 61 20 74 65 6d 70 6f 72 61  ult in a tempora
1ae0e 72 79 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a  ry table.      *
1ae0f 2f 0a 20 20 20 20 20 20 73 72 63 54 61 62 20 3d  /.      srcTab =
1ae10 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
1ae11 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ae12 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
1ae13 2c 20 69 49 6e 73 65 72 74 42 6c 6f 63 6b 29 3b  , iInsertBlock);
1ae14 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ae15 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61  beAddOp(v, OP_Ma
1ae16 6b 65 52 65 63 6f 72 64 2c 20 6e 43 6f 6c 75 6d  keRecord, nColum
1ae17 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  n, 0);.      sql
1ae18 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1ae19 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 73 72   OP_NewRowid, sr
1ae1a 63 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20 20  cTab, 0);.      
1ae1b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ae1c 28 76 2c 20 4f 50 5f 50 75 6c 6c 2c 20 31 2c 20  (v, OP_Pull, 1, 
1ae1d 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
1ae1e 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1ae1f 5f 49 6e 73 65 72 74 2c 20 73 72 63 54 61 62 2c  _Insert, srcTab,
1ae20 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b   OPFLAG_APPEND);
1ae21 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ae22 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65  beAddOp(v, OP_Re
1ae23 74 75 72 6e 2c 20 30 2c 20 30 29 3b 0a 0a 20 20  turn, 0, 0);..  
1ae24 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
1ae25 77 69 6e 67 20 63 6f 64 65 20 72 75 6e 73 20 66  wing code runs f
1ae26 69 72 73 74 20 62 65 63 61 75 73 65 20 74 68 65  irst because the
1ae27 20 47 4f 54 4f 20 61 74 20 74 68 65 20 76 65 72   GOTO at the ver
1ae28 79 20 74 6f 70 0a 20 20 20 20 20 20 2a 2a 20 6f  y top.      ** o
1ae29 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 6a 75  f the program ju
1ae2a 6d 70 73 20 74 6f 20 69 74 2e 20 20 43 72 65 61  mps to it.  Crea
1ae2b 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  te the temporary
1ae2c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6a 75 6d   table, then jum
1ae2d 70 0a 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20  p.      ** back 
1ae2e 75 70 20 61 6e 64 20 65 78 65 63 75 74 65 20 74  up and execute t
1ae2f 68 65 20 53 45 4c 45 43 54 20 63 6f 64 65 20 61  he SELECT code a
1ae30 62 6f 76 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  bove..      */. 
1ae31 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ae32 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 49 6e 69  JumpHere(v, iIni
1ae33 74 43 6f 64 65 29 3b 0a 20 20 20 20 20 20 73 71  tCode);.      sq
1ae34 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ae35 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
1ae36 61 6c 2c 20 73 72 63 54 61 62 2c 20 30 29 3b 0a  al, srcTab, 0);.
1ae37 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ae38 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 74  eAddOp(v, OP_Set
1ae39 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 73 72 63 54  NumColumns, srcT
1ae3a 61 62 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ab, nColumn);.  
1ae3b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ae3c 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  ddOp(v, OP_Goto,
1ae3d 20 30 2c 20 69 53 65 6c 65 63 74 4c 6f 6f 70 29   0, iSelectLoop)
1ae3e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1ae3f 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
1ae40 76 2c 20 69 43 6c 65 61 6e 75 70 29 3b 0a 20 20  v, iCleanup);.  
1ae41 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
1ae42 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
1ae43 72 65 28 76 2c 20 69 49 6e 69 74 43 6f 64 65 29  re(v, iInitCode)
1ae44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1ae45 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20  .    /* This is 
1ae46 74 68 65 20 63 61 73 65 20 69 66 20 74 68 65 20  the case if the 
1ae47 64 61 74 61 20 66 6f 72 20 74 68 65 20 49 4e 53  data for the INS
1ae48 45 52 54 20 69 73 20 63 6f 6d 69 6e 67 20 66 72  ERT is coming fr
1ae49 6f 6d 20 61 20 56 41 4c 55 45 53 0a 20 20 20 20  om a VALUES.    
1ae4a 2a 2a 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f  ** clause.    */
1ae4b 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  .    NameContext
1ae4c 20 73 4e 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74   sNC;.    memset
1ae4d 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
1ae4e 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e  (sNC));.    sNC.
1ae4f 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
1ae50 0a 20 20 20 20 73 72 63 54 61 62 20 3d 20 2d 31  .    srcTab = -1
1ae51 3b 0a 20 20 20 20 75 73 65 54 65 6d 70 54 61 62  ;.    useTempTab
1ae52 6c 65 20 3d 20 30 3b 0a 20 20 20 20 6e 43 6f 6c  le = 0;.    nCol
1ae53 75 6d 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c  umn = pList ? pL
1ae54 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a  ist->nExpr : 0;.
1ae55 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
1ae56 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
1ae57 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45      if( sqlite3E
1ae58 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28  xprResolveNames(
1ae59 26 73 4e 43 2c 20 70 4c 69 73 74 2d 3e 61 5b 69  &sNC, pList->a[i
1ae5a 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ].pExpr) ){.    
1ae5b 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
1ae5c 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
1ae5d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1ae5e 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e   Make sure the n
1ae5f 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1ae60 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
1ae61 61 74 61 20 6d 61 74 63 68 65 73 20 74 68 65 20  ata matches the 
1ae62 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 63  number.  ** of c
1ae63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 73  olumns to be ins
1ae64 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74  erted into the t
1ae65 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
1ae66 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1ae67 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
1ae68 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
1ae69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 48 69 64 64  ++){.      nHidd
1ae6a 65 6e 20 2b 3d 20 28 49 73 48 69 64 64 65 6e 43  en += (IsHiddenC
1ae6b 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f  olumn(&pTab->aCo
1ae6c 6c 5b 69 5d 29 20 3f 20 31 20 3a 20 30 29 3b 0a  l[i]) ? 1 : 0);.
1ae6d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
1ae6e 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43  pColumn==0 && nC
1ae6f 6f 6c 75 6d 6e 20 26 26 20 6e 43 6f 6c 75 6d 6e  olumn && nColumn
1ae70 21 3d 28 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48  !=(pTab->nCol-nH
1ae71 69 64 64 65 6e 29 20 29 7b 0a 20 20 20 20 73 71  idden) ){.    sq
1ae72 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1ae73 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22 74  arse, .       "t
1ae74 61 62 6c 65 20 25 53 20 68 61 73 20 25 64 20 63  able %S has %d c
1ae75 6f 6c 75 6d 6e 73 20 62 75 74 20 25 64 20 76 61  olumns but %d va
1ae76 6c 75 65 73 20 77 65 72 65 20 73 75 70 70 6c 69  lues were suppli
1ae77 65 64 22 2c 0a 20 20 20 20 20 20 20 70 54 61 62  ed",.       pTab
1ae78 4c 69 73 74 2c 20 30 2c 20 70 54 61 62 2d 3e 6e  List, 0, pTab->n
1ae79 43 6f 6c 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  Col, nColumn);. 
1ae7a 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
1ae7b 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
1ae7c 28 20 70 43 6f 6c 75 6d 6e 21 3d 30 20 26 26 20  ( pColumn!=0 && 
1ae7d 6e 43 6f 6c 75 6d 6e 21 3d 70 43 6f 6c 75 6d 6e  nColumn!=pColumn
1ae7e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 73 71 6c  ->nId ){.    sql
1ae7f 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1ae80 72 73 65 2c 20 22 25 64 20 76 61 6c 75 65 73 20  rse, "%d values 
1ae81 66 6f 72 20 25 64 20 63 6f 6c 75 6d 6e 73 22 2c  for %d columns",
1ae82 20 6e 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 75 6d   nColumn, pColum
1ae83 6e 2d 3e 6e 49 64 29 3b 0a 20 20 20 20 67 6f 74  n->nId);.    got
1ae84 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
1ae85 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
1ae86 68 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  he INSERT statem
1ae87 65 6e 74 20 69 6e 63 6c 75 64 65 64 20 61 6e 20  ent included an 
1ae88 49 44 4c 49 53 54 20 74 65 72 6d 2c 20 74 68 65  IDLIST term, the
1ae89 6e 20 6d 61 6b 65 20 73 75 72 65 0a 20 20 2a 2a  n make sure.  **
1ae8a 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66   all elements of
1ae8b 20 74 68 65 20 49 44 4c 49 53 54 20 72 65 61 6c   the IDLIST real
1ae8c 6c 79 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 6f  ly are columns o
1ae8d 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  f the table and 
1ae8e 0a 20 20 2a 2a 20 72 65 6d 65 6d 62 65 72 20 74  .  ** remember t
1ae8f 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65  he column indice
1ae90 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  s..  **.  ** If 
1ae91 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e  the table has an
1ae92 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
1ae93 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   KEY column and 
1ae94 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a  that column.  **
1ae95 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65   is named in the
1ae96 20 49 44 4c 49 53 54 2c 20 74 68 65 6e 20 72 65   IDLIST, then re
1ae97 63 6f 72 64 20 69 6e 20 74 68 65 20 6b 65 79 43  cord in the keyC
1ae98 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 0a 20  olumn variable. 
1ae99 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 69 6e   ** the index in
1ae9a 74 6f 20 49 44 4c 49 53 54 20 6f 66 20 74 68 65  to IDLIST of the
1ae9b 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c   primary key col
1ae9c 75 6d 6e 2e 20 20 6b 65 79 43 6f 6c 75 6d 6e 20  umn.  keyColumn 
1ae9d 69 73 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65  is.  ** the inde
1ae9e 78 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79  x of the primary
1ae9f 20 6b 65 79 20 61 73 20 69 74 20 61 70 70 65 61   key as it appea
1aea0 72 73 20 69 6e 20 49 44 4c 49 53 54 2c 20 6e 6f  rs in IDLIST, no
1aea1 74 20 61 73 0a 20 20 2a 2a 20 69 73 20 61 70 70  t as.  ** is app
1aea2 65 61 72 73 20 69 6e 20 74 68 65 20 6f 72 69 67  ears in the orig
1aea3 69 6e 61 6c 20 74 61 62 6c 65 2e 20 20 28 54 68  inal table.  (Th
1aea4 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70  e index of the p
1aea5 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20  rimary.  ** key 
1aea6 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
1aea7 74 61 62 6c 65 20 69 73 20 70 54 61 62 2d 3e 69  table is pTab->i
1aea8 50 4b 65 79 2e 29 0a 20 20 2a 2f 0a 20 20 69 66  PKey.).  */.  if
1aea9 28 20 70 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ( pColumn ){.   
1aeaa 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c   for(i=0; i<pCol
1aeab 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a  umn->nId; i++){.
1aeac 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61        pColumn->a
1aead 5b 69 5d 2e 69 64 78 20 3d 20 2d 31 3b 0a 20 20  [i].idx = -1;.  
1aeae 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
1aeaf 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b   i<pColumn->nId;
1aeb0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72   i++){.      for
1aeb1 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  (j=0; j<pTab->nC
1aeb2 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; j++){.      
1aeb3 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1aeb4 49 43 6d 70 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b  ICmp(pColumn->a[
1aeb5 69 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  i].zName, pTab->
1aeb6 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d  aCol[j].zName)==
1aeb7 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  0 ){.          p
1aeb8 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78  Column->a[i].idx
1aeb9 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20   = j;.          
1aeba 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( j==pTab->iPK
1aebb 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
1aebc 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b    keyColumn = i;
1aebd 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1aebe 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1aebf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1aec0 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61        if( j>=pTa
1aec1 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
1aec2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
1aec3 52 6f 77 69 64 28 70 43 6f 6c 75 6d 6e 2d 3e 61  Rowid(pColumn->a
1aec4 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  [i].zName) ){.  
1aec5 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d          keyColum
1aec6 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 7d  n = i;.        }
1aec7 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1aec8 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1aec9 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25  pParse, "table %
1aeca 53 20 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20  S has no column 
1aecb 6e 61 6d 65 64 20 25 73 22 2c 0a 20 20 20 20 20  named %s",.     
1aecc 20 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73           pTabLis
1aecd 74 2c 20 30 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 61  t, 0, pColumn->a
1aece 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
1aecf 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45        pParse->nE
1aed0 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  rr++;.          
1aed1 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
1aed2 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nup;.        }. 
1aed3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1aed4 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
1aed5 69 73 20 6e 6f 20 49 44 4c 49 53 54 20 74 65 72  is no IDLIST ter
1aed6 6d 20 62 75 74 20 74 68 65 20 74 61 62 6c 65 20  m but the table 
1aed7 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20 70  has an integer p
1aed8 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 2c  rimary.  ** key,
1aed9 20 74 68 65 20 73 65 74 20 74 68 65 20 6b 65 79   the set the key
1aeda 43 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 20  Column variable 
1aedb 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  to the primary k
1aedc 65 79 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a  ey column index.
1aedd 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67    ** in the orig
1aede 69 6e 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e  inal table defin
1aedf 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  ition..  */.  if
1aee0 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20  ( pColumn==0 && 
1aee1 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20  nColumn>0 ){.   
1aee2 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 70 54 61   keyColumn = pTa
1aee3 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 7d 0a 0a 20  b->iPKey;.  }.. 
1aee4 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 74 65 6d   /* Open the tem
1aee5 70 20 74 61 62 6c 65 20 66 6f 72 20 46 4f 52 20  p table for FOR 
1aee6 45 41 43 48 20 52 4f 57 20 74 72 69 67 67 65 72  EACH ROW trigger
1aee7 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74 72 69  s.  */.  if( tri
1aee8 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20  ggers_exist ){. 
1aee9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1aeea 64 4f 70 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73  dOp(v, OP_OpenPs
1aeeb 65 75 64 6f 2c 20 6e 65 77 49 64 78 2c 20 30 29  eudo, newIdx, 0)
1aeec 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1aeed 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 74  eAddOp(v, OP_Set
1aeee 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 6e 65 77 49  NumColumns, newI
1aeef 64 78 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b  dx, pTab->nCol);
1aef0 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20 49  .  }.    .  /* I
1aef1 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f  nitialize the co
1aef2 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 62  unt of rows to b
1aef3 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f 0a  e inserted.  */.
1aef4 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
1aef5 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
1aef6 77 73 20 29 7b 0a 20 20 20 20 69 43 6e 74 4d 65  ws ){.    iCntMe
1aef7 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  m = pParse->nMem
1aef8 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ++;.    sqlite3V
1aef9 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
1aefa 65 6d 49 6e 74 2c 20 30 2c 20 69 43 6e 74 4d 65  emInt, 0, iCntMe
1aefb 6d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70  m);.  }..  /* Op
1aefc 65 6e 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e  en tables and in
1aefd 64 69 63 65 73 20 69 66 20 74 68 65 72 65 20 61  dices if there a
1aefe 72 65 20 6e 6f 20 72 6f 77 20 74 72 69 67 67 65  re no row trigge
1aeff 72 73 20 2a 2f 0a 20 20 69 66 28 20 21 74 72 69  rs */.  if( !tri
1af00 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20  ggers_exist ){. 
1af01 20 20 20 62 61 73 65 20 3d 20 70 50 61 72 73 65     base = pParse
1af02 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 73 71 6c 69  ->nTab;.    sqli
1af03 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49  te3OpenTableAndI
1af04 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70  ndices(pParse, p
1af05 54 61 62 2c 20 62 61 73 65 2c 20 4f 50 5f 4f 70  Tab, base, OP_Op
1af06 65 6e 57 72 69 74 65 29 3b 0a 20 20 7d 0a 0a 20  enWrite);.  }.. 
1af07 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 20   /* If the data 
1af08 73 6f 75 72 63 65 20 69 73 20 61 20 74 65 6d 70  source is a temp
1af09 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 74 68 65  orary table, the
1af0a 6e 20 77 65 20 68 61 76 65 20 74 6f 20 63 72 65  n we have to cre
1af0b 61 74 65 0a 20 20 2a 2a 20 61 20 6c 6f 6f 70 20  ate.  ** a loop 
1af0c 62 65 63 61 75 73 65 20 74 68 65 72 65 20 6d 69  because there mi
1af0d 67 68 74 20 62 65 20 6d 75 6c 74 69 70 6c 65 20  ght be multiple 
1af0e 72 6f 77 73 20 6f 66 20 64 61 74 61 2e 20 20 49  rows of data.  I
1af0f 66 20 74 68 65 20 64 61 74 61 0a 20 20 2a 2a 20  f the data.  ** 
1af10 73 6f 75 72 63 65 20 69 73 20 61 20 73 75 62 72  source is a subr
1af11 6f 75 74 69 6e 65 20 63 61 6c 6c 20 66 72 6f 6d  outine call from
1af12 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
1af13 65 6d 65 6e 74 2c 20 74 68 65 6e 20 77 65 20 6e  ement, then we n
1af14 65 65 64 0a 20 20 2a 2a 20 74 6f 20 6c 61 75 6e  eed.  ** to laun
1af15 63 68 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  ch the SELECT st
1af16 61 74 65 6d 65 6e 74 20 70 72 6f 63 65 73 73 69  atement processi
1af17 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75  ng..  */.  if( u
1af18 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20  seTempTable ){. 
1af19 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69     iBreak = sqli
1af1a 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
1af1b 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  (v);.    sqlite3
1af1c 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1af1d 52 65 77 69 6e 64 2c 20 73 72 63 54 61 62 2c 20  Rewind, srcTab, 
1af1e 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 69 43 6f  iBreak);.    iCo
1af1f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nt = sqlite3Vdbe
1af20 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
1af21 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c    }else if( pSel
1af22 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ect ){.    sqlit
1af23 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1af24 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 53 65 6c 65  P_Goto, 0, iSele
1af25 63 74 4c 6f 6f 70 29 3b 0a 20 20 20 20 73 71 6c  ctLoop);.    sql
1af26 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
1af27 61 62 65 6c 28 76 2c 20 69 49 6e 73 65 72 74 42  abel(v, iInsertB
1af28 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  lock);.  }..  /*
1af29 20 52 75 6e 20 74 68 65 20 42 45 46 4f 52 45 20   Run the BEFORE 
1af2a 61 6e 64 20 49 4e 53 54 45 41 44 20 4f 46 20 74  and INSTEAD OF t
1af2b 72 69 67 67 65 72 73 2c 20 69 66 20 74 68 65 72  riggers, if ther
1af2c 65 20 61 72 65 20 61 6e 79 0a 20 20 2a 2f 0a 20  e are any.  */. 
1af2d 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c   endOfLoop = sql
1af2e 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
1af2f 6c 28 76 29 3b 0a 20 20 69 66 28 20 74 72 69 67  l(v);.  if( trig
1af30 67 65 72 73 5f 65 78 69 73 74 20 26 20 54 52 49  gers_exist & TRI
1af31 47 47 45 52 5f 42 45 46 4f 52 45 20 29 7b 0a 0a  GGER_BEFORE ){..
1af32 20 20 20 20 2f 2a 20 62 75 69 6c 64 20 74 68 65      /* build the
1af33 20 4e 45 57 2e 2a 20 72 65 66 65 72 65 6e 63 65   NEW.* reference
1af34 20 72 6f 77 2e 20 20 4e 6f 74 65 20 74 68 61 74   row.  Note that
1af35 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
1af36 49 4e 54 45 47 45 52 0a 20 20 20 20 2a 2a 20 50  INTEGER.    ** P
1af37 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 74 6f 20  RIMARY KEY into 
1af38 77 68 69 63 68 20 61 20 4e 55 4c 4c 20 69 73 20  which a NULL is 
1af39 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 2c 20  being inserted, 
1af3a 74 68 61 74 20 4e 55 4c 4c 20 77 69 6c 6c 20 62  that NULL will b
1af3b 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61  e.    ** transla
1af3c 74 65 64 20 69 6e 74 6f 20 61 20 75 6e 69 71 75  ted into a uniqu
1af3d 65 20 49 44 20 66 6f 72 20 74 68 65 20 72 6f 77  e ID for the row
1af3e 2e 20 20 42 75 74 20 6f 6e 20 61 20 42 45 46 4f  .  But on a BEFO
1af3f 52 45 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20  RE trigger,.    
1af40 2a 2a 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f  ** we do not kno
1af41 77 20 77 68 61 74 20 74 68 65 20 75 6e 69 71 75  w what the uniqu
1af42 65 20 49 44 20 77 69 6c 6c 20 62 65 20 28 62 65  e ID will be (be
1af43 63 61 75 73 65 20 74 68 65 20 69 6e 73 65 72 74  cause the insert
1af44 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20   has.    ** not 
1af45 68 61 70 70 65 6e 65 64 20 79 65 74 29 20 73 6f  happened yet) so
1af46 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20 61   we substitute a
1af47 20 72 6f 77 69 64 20 6f 66 20 2d 31 0a 20 20 20   rowid of -1.   
1af48 20 2a 2f 0a 20 20 20 20 69 66 28 20 6b 65 79 43   */.    if( keyC
1af49 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20  olumn<0 ){.     
1af4a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1af4b 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  p(v, OP_Integer,
1af4c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c   -1, 0);.    }el
1af4d 73 65 20 69 66 28 20 75 73 65 54 65 6d 70 54 61  se if( useTempTa
1af4e 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ble ){.      sql
1af4f 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1af50 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54   OP_Column, srcT
1af51 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 29 3b 0a  ab, keyColumn);.
1af52 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1af53 20 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74   assert( pSelect
1af54 3d 3d 30 20 29 3b 20 20 2f 2a 20 4f 74 68 65 72  ==0 );  /* Other
1af55 77 69 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c  wise useTempTabl
1af56 65 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20  e is true */.   
1af57 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
1af58 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74  de(pParse, pList
1af59 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70  ->a[keyColumn].p
1af5a 45 78 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c  Expr);.      sql
1af5b 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1af5c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 2d 31 2c   OP_NotNull, -1,
1af5d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
1af5e 65 6e 74 41 64 64 72 28 76 29 2b 33 29 3b 0a 20  entAddr(v)+3);. 
1af5f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1af60 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c  AddOp(v, OP_Pop,
1af61 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71   1, 0);.      sq
1af62 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1af63 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31  , OP_Integer, -1
1af64 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
1af65 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1af66 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 30 2c  OP_MustBeInt, 0,
1af67 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   0);.    }..    
1af68 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76 65 20 74  /* Cannot have t
1af69 72 69 67 67 65 72 73 20 6f 6e 20 61 20 76 69 72  riggers on a vir
1af6a 74 75 61 6c 20 74 61 62 6c 65 2e 20 49 66 20 69  tual table. If i
1af6b 74 20 77 65 72 65 20 70 6f 73 73 69 62 6c 65 2c  t were possible,
1af6c 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 62 6c 6f  .    ** this blo
1af6d 63 6b 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f  ck would have to
1af6e 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 68 69 64   account for hid
1af6f 64 65 6e 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20  den column..    
1af70 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 21 49  */.    assert(!I
1af71 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 29 3b  sVirtual(pTab));
1af72 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20  ..    /* Create 
1af73 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 64  the new column d
1af74 61 74 61 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  ata.    */.    f
1af75 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
1af76 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
1af77 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30    if( pColumn==0
1af78 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20   ){.        j = 
1af79 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  i;.      }else{.
1af7a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
1af7b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b   j<pColumn->nId;
1af7c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
1af7d 20 69 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b   if( pColumn->a[
1af7e 6a 5d 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61  j].idx==i ) brea
1af7f 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
1af80 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
1af81 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f  Column && j>=pCo
1af82 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20  lumn->nId ){.   
1af83 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1af84 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61  Code(pParse, pTa
1af85 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74  b->aCol[i].pDflt
1af86 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
1af87 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
1af88 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1af89 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1af8a 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62  P_Column, srcTab
1af8b 2c 20 6a 29 3b 20 0a 20 20 20 20 20 20 7d 65 6c  , j); .      }el
1af8c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  se{.        asse
1af8d 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29  rt( pSelect==0 )
1af8e 3b 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 75  ; /* Otherwise u
1af8f 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20 74  seTempTable is t
1af90 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73  rue */.        s
1af91 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e  qlite3ExprCodeAn
1af92 64 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 70  dCache(pParse, p
1af93 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  List->a[j].pExpr
1af94 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1af95 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1af96 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65  AddOp(v, OP_Make
1af97 52 65 63 6f 72 64 2c 20 70 54 61 62 2d 3e 6e 43  Record, pTab->nC
1af98 6f 6c 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20  ol, 0);..    /* 
1af99 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e  If this is an IN
1af9a 53 45 52 54 20 6f 6e 20 61 20 76 69 65 77 20 77  SERT on a view w
1af9b 69 74 68 20 61 6e 20 49 4e 53 54 45 41 44 20 4f  ith an INSTEAD O
1af9c 46 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72  F INSERT trigger
1af9d 2c 0a 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20  ,.    ** do not 
1af9e 61 74 74 65 6d 70 74 20 61 6e 79 20 63 6f 6e 76  attempt any conv
1af9f 65 72 73 69 6f 6e 73 20 62 65 66 6f 72 65 20 61  ersions before a
1afa0 73 73 65 6d 62 6c 69 6e 67 20 74 68 65 20 72 65  ssembling the re
1afa1 63 6f 72 64 2e 0a 20 20 20 20 2a 2a 20 49 66 20  cord..    ** If 
1afa2 74 68 69 73 20 69 73 20 61 20 72 65 61 6c 20 74  this is a real t
1afa3 61 62 6c 65 2c 20 61 74 74 65 6d 70 74 20 63 6f  able, attempt co
1afa4 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 72 65 71  nversions as req
1afa5 75 69 72 65 64 20 62 79 20 74 68 65 0a 20 20 20  uired by the.   
1afa6 20 2a 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e   ** table column
1afa7 20 61 66 66 69 6e 69 74 69 65 73 2e 0a 20 20 20   affinities..   
1afa8 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56   */.    if( !isV
1afa9 69 65 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  iew ){.      sql
1afaa 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74  ite3TableAffinit
1afab 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20  yStr(v, pTab);. 
1afac 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1afad 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1afae 49 6e 73 65 72 74 2c 20 6e 65 77 49 64 78 2c 20  Insert, newIdx, 
1afaf 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65  0);..    /* Fire
1afb0 20 42 45 46 4f 52 45 20 6f 72 20 49 4e 53 54 45   BEFORE or INSTE
1afb1 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a  AD OF triggers *
1afb2 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
1afb3 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
1afb4 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 53 45 52  pParse, TK_INSER
1afb5 54 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45  T, 0, TRIGGER_BE
1afb6 46 4f 52 45 2c 20 70 54 61 62 2c 20 0a 20 20 20  FORE, pTab, .   
1afb7 20 20 20 20 20 6e 65 77 49 64 78 2c 20 2d 31 2c       newIdx, -1,
1afb8 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c   onError, endOfL
1afb9 6f 6f 70 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  oop) ){.      go
1afba 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
1afbb 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  p;.    }.  }..  
1afbc 2f 2a 20 49 66 20 61 6e 79 20 74 72 69 67 67 65  /* If any trigge
1afbd 72 73 20 65 78 69 73 74 73 2c 20 74 68 65 20 6f  rs exists, the o
1afbe 70 65 6e 69 6e 67 20 6f 66 20 74 61 62 6c 65 73  pening of tables
1afbf 20 61 6e 64 20 69 6e 64 69 63 65 73 20 69 73 20   and indices is 
1afc0 64 65 66 65 72 72 65 64 0a 20 20 2a 2a 20 75 6e  deferred.  ** un
1afc1 74 69 6c 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  til now..  */.  
1afc2 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69  if( triggers_exi
1afc3 73 74 20 26 26 20 21 69 73 56 69 65 77 20 29 7b  st && !isView ){
1afc4 0a 20 20 20 20 62 61 73 65 20 3d 20 70 50 61 72  .    base = pPar
1afc5 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 73 71  se->nTab;.    sq
1afc6 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e  lite3OpenTableAn
1afc7 64 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c  dIndices(pParse,
1afc8 20 70 54 61 62 2c 20 62 61 73 65 2c 20 4f 50 5f   pTab, base, OP_
1afc9 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 7d 0a  OpenWrite);.  }.
1afca 0a 20 20 2f 2a 20 50 75 73 68 20 74 68 65 20 72  .  /* Push the r
1afcb 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72  ecord number for
1afcc 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 6f   the new entry o
1afcd 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 20 20  nto the stack.  
1afce 54 68 65 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20  The.  ** record 
1afcf 6e 75 6d 62 65 72 20 69 73 20 61 20 72 61 6e 64  number is a rand
1afd0 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65 20 69 6e  omly generate in
1afd1 74 65 67 65 72 20 63 72 65 61 74 65 64 20 62 79  teger created by
1afd2 20 4e 65 77 52 6f 77 69 64 0a 20 20 2a 2a 20 65   NewRowid.  ** e
1afd3 78 63 65 70 74 20 77 68 65 6e 20 74 68 65 20 74  xcept when the t
1afd4 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45  able has an INTE
1afd5 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
1afd6 63 6f 6c 75 6d 6e 2c 20 69 6e 20 77 68 69 63 68  column, in which
1afd7 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 72  .  ** case the r
1afd8 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
1afd9 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 61 74  the same as that
1afda 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2f 0a 20   column. .  */. 
1afdb 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a   if( !isView ){.
1afdc 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61      if( IsVirtua
1afdd 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
1afde 20 2f 2a 20 54 68 65 20 72 6f 77 20 74 68 61 74   /* The row that
1afdf 20 74 68 65 20 56 55 70 64 61 74 65 20 6f 70 63   the VUpdate opc
1afe0 6f 64 65 20 77 69 6c 6c 20 64 65 6c 65 74 65 3a  ode will delete:
1afe1 20 20 6e 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20    none */.      
1afe2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1afe3 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
1afe4 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  0);.    }.    if
1afe5 28 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 20 29  ( keyColumn>=0 )
1afe6 7b 0a 20 20 20 20 20 20 69 66 28 20 75 73 65 54  {.      if( useT
1afe7 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
1afe8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1afe9 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  ddOp(v, OP_Colum
1afea 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f  n, srcTab, keyCo
1afeb 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c  lumn);.      }el
1afec 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29  se if( pSelect )
1afed 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1afee 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1afef 5f 44 75 70 2c 20 6e 43 6f 6c 75 6d 6e 20 2d 20  _Dup, nColumn - 
1aff0 6b 65 79 43 6f 6c 75 6d 6e 20 2d 20 31 2c 20 31  keyColumn - 1, 1
1aff1 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1aff2 20 20 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a          VdbeOp *
1aff3 70 4f 70 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  pOp;.        sql
1aff4 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1aff5 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65  rse, pList->a[ke
1aff6 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 29 3b  yColumn].pExpr);
1aff7 0a 20 20 20 20 20 20 20 20 70 4f 70 20 3d 20 73  .        pOp = s
1aff8 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28  qlite3VdbeGetOp(
1aff9 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  v, sqlite3VdbeCu
1affa 72 72 65 6e 74 41 64 64 72 28 76 29 20 2d 20 31  rrentAddr(v) - 1
1affb 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
1affc 4f 70 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64  Op && pOp->opcod
1affd 65 3d 3d 4f 50 5f 4e 75 6c 6c 20 29 7b 0a 20 20  e==OP_Null ){.  
1affe 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c          appendFl
1afff 61 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ag = 1;.        
1b000 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
1b001 4f 50 5f 4e 65 77 52 6f 77 69 64 3b 0a 20 20 20  OP_NewRowid;.   
1b002 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d         pOp->p1 =
1b003 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20   base;.         
1b004 20 70 4f 70 2d 3e 70 32 20 3d 20 63 6f 75 6e 74   pOp->p2 = count
1b005 65 72 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 7d  erMem;.        }
1b006 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
1b007 2a 20 49 66 20 74 68 65 20 50 52 49 4d 41 52 59  * If the PRIMARY
1b008 20 4b 45 59 20 65 78 70 72 65 73 73 69 6f 6e 20   KEY expression 
1b009 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 75 73  is NULL, then us
1b00a 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 0a 20 20  e OP_NewRowid.  
1b00b 20 20 20 20 2a 2a 20 74 6f 20 67 65 6e 65 72 61      ** to genera
1b00c 74 65 20 61 20 75 6e 69 71 75 65 20 70 72 69 6d  te a unique prim
1b00d 61 72 79 20 6b 65 79 20 76 61 6c 75 65 2e 0a 20  ary key value.. 
1b00e 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
1b00f 28 20 21 61 70 70 65 6e 64 46 6c 61 67 20 29 7b  ( !appendFlag ){
1b010 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1b011 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b012 4e 6f 74 4e 75 6c 6c 2c 20 2d 31 2c 20 73 71 6c  NotNull, -1, sql
1b013 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
1b014 64 64 72 28 76 29 2b 33 29 3b 0a 20 20 20 20 20  ddr(v)+3);.     
1b015 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1b016 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31  dOp(v, OP_Pop, 1
1b017 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
1b018 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b019 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62  , OP_NewRowid, b
1b01a 61 73 65 2c 20 63 6f 75 6e 74 65 72 4d 65 6d 29  ase, counterMem)
1b01b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1b01c 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b01d 5f 4d 75 73 74 42 65 49 6e 74 2c 20 30 2c 20 30  _MustBeInt, 0, 0
1b01e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1b01f 65 6c 73 65 20 69 66 28 20 49 73 56 69 72 74 75  else if( IsVirtu
1b020 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
1b021 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1b022 4f 70 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  Op(v, OP_Null, 0
1b023 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
1b024 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1b025 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65  beAddOp(v, OP_Ne
1b026 77 52 6f 77 69 64 2c 20 62 61 73 65 2c 20 63 6f  wRowid, base, co
1b027 75 6e 74 65 72 4d 65 6d 29 3b 0a 20 20 20 20 20  unterMem);.     
1b028 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b   appendFlag = 1;
1b029 0a 20 20 20 20 7d 0a 20 20 20 20 61 75 74 6f 49  .    }.    autoI
1b02a 6e 63 53 74 65 70 28 70 50 61 72 73 65 2c 20 63  ncStep(pParse, c
1b02b 6f 75 6e 74 65 72 4d 65 6d 29 3b 0a 0a 20 20 20  ounterMem);..   
1b02c 20 2f 2a 20 50 75 73 68 20 6f 6e 74 6f 20 74 68   /* Push onto th
1b02d 65 20 73 74 61 63 6b 2c 20 64 61 74 61 20 66 6f  e stack, data fo
1b02e 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66  r all columns of
1b02f 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 2c 20   the new entry, 
1b030 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a  beginning.    **
1b031 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20   with the first 
1b032 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20  column..    */. 
1b033 20 20 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 0a     nHidden = 0;.
1b034 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1b035 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
1b036 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 70 54  .      if( i==pT
1b037 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
1b038 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75       /* The valu
1b039 65 20 6f 66 20 74 68 65 20 49 4e 54 45 47 45 52  e of the INTEGER
1b03a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
1b03b 75 6d 6e 20 69 73 20 61 6c 77 61 79 73 20 61 20  umn is always a 
1b03c 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2a  NULL..        **
1b03d 20 57 68 65 6e 65 76 65 72 20 74 68 69 73 20 63   Whenever this c
1b03e 6f 6c 75 6d 6e 20 69 73 20 72 65 61 64 2c 20 74  olumn is read, t
1b03f 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
1b040 20 77 69 6c 6c 20 62 65 20 73 75 62 73 74 69 74   will be substit
1b041 75 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  uted.        ** 
1b042 69 6e 20 69 74 73 20 70 6c 61 63 65 2e 20 20 53  in its place.  S
1b043 6f 20 77 69 6c 6c 20 66 69 6c 6c 20 74 68 69 73  o will fill this
1b044 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 61 20 4e   column with a N
1b045 55 4c 4c 20 74 6f 20 61 76 6f 69 64 0a 20 20 20  ULL to avoid.   
1b046 20 20 20 20 20 2a 2a 20 74 61 6b 69 6e 67 20 75       ** taking u
1b047 70 20 64 61 74 61 20 73 70 61 63 65 20 77 69 74  p data space wit
1b048 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  h information th
1b049 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  at will never be
1b04a 20 75 73 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20   used. */.      
1b04b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1b04c 4f 70 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  Op(v, OP_Null, 0
1b04d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 63 6f  , 0);.        co
1b04e 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
1b04f 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d        if( pColum
1b050 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n==0 ){.        
1b051 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75  if( IsHiddenColu
1b052 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  mn(&pTab->aCol[i
1b053 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ]) ){.          
1b054 61 73 73 65 72 74 28 20 49 73 56 69 72 74 75 61  assert( IsVirtua
1b055 6c 28 70 54 61 62 29 20 29 3b 0a 20 20 20 20 20  l(pTab) );.     
1b056 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 20       j = -1;.   
1b057 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b         nHidden++
1b058 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1b059 0a 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20 69  .          j = i
1b05a 20 2d 20 6e 48 69 64 64 65 6e 3b 0a 20 20 20 20   - nHidden;.    
1b05b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
1b05c 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  e{.        for(j
1b05d 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e  =0; j<pColumn->n
1b05e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  Id; j++){.      
1b05f 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 2d      if( pColumn-
1b060 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29 20 62  >a[j].idx==i ) b
1b061 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
1b062 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1b063 28 20 6a 3c 30 20 7c 7c 20 6e 43 6f 6c 75 6d 6e  ( j<0 || nColumn
1b064 3d 3d 30 20 7c 7c 20 28 70 43 6f 6c 75 6d 6e 20  ==0 || (pColumn 
1b065 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e  && j>=pColumn->n
1b066 49 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Id) ){.        s
1b067 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1b068 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f  Parse, pTab->aCo
1b069 6c 5b 69 5d 2e 70 44 66 6c 74 29 3b 0a 20 20 20  l[i].pDflt);.   
1b06a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65     }else if( use
1b06b 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
1b06c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b06d 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75  AddOp(v, OP_Colu
1b06e 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 29 3b 20  mn, srcTab, j); 
1b06f 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1b070 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
1b071 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1b072 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20  ddOp(v, OP_Dup, 
1b073 69 2b 6e 43 6f 6c 75 6d 6e 2d 6a 2b 49 73 56 69  i+nColumn-j+IsVi
1b074 72 74 75 61 6c 28 70 54 61 62 29 2c 20 31 29 3b  rtual(pTab), 1);
1b075 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1b076 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1b077 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c  rCode(pParse, pL
1b078 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 29  ist->a[j].pExpr)
1b079 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1b07a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  .    /* Generate
1b07b 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63   code to check c
1b07c 6f 6e 73 74 72 61 69 6e 74 73 20 61 6e 64 20 67  onstraints and g
1b07d 65 6e 65 72 61 74 65 20 69 6e 64 65 78 20 6b 65  enerate index ke
1b07e 79 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 6f  ys and.    ** do
1b07f 20 74 68 65 20 69 6e 73 65 72 74 69 6f 6e 2e 0a   the insertion..
1b080 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
1b081 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1b082 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20  ALTABLE.    if( 
1b083 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1b084 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
1b085 3e 70 56 69 72 74 75 61 6c 4c 6f 63 6b 20 3d 20  >pVirtualLock = 
1b086 70 54 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69  pTab;.      sqli
1b087 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
1b088 5f 56 55 70 64 61 74 65 2c 20 31 2c 20 70 54 61  _VUpdate, 1, pTa
1b089 62 2d 3e 6e 43 6f 6c 2b 32 2c 0a 20 20 20 20 20  b->nCol+2,.     
1b08a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b08b 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 61  (const char*)pTa
1b08c 62 2d 3e 70 56 74 61 62 2c 20 50 33 5f 56 54 41  b->pVtab, P3_VTA
1b08d 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65  B);.    }else.#e
1b08e 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20  ndif.    {.     
1b08f 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
1b090 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
1b091 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62  (pParse, pTab, b
1b092 61 73 65 2c 20 30 2c 20 6b 65 79 43 6f 6c 75 6d  ase, 0, keyColum
1b093 6e 3e 3d 30 2c 0a 20 20 20 20 20 20 20 20 20 20  n>=0,.          
1b094 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b095 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 6f 6e             0, on
1b096 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70  Error, endOfLoop
1b097 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1b098 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f  CompleteInsertio
1b099 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  n(pParse, pTab, 
1b09a 62 61 73 65 2c 20 30 2c 30 2c 30 2c 0a 20 20 20  base, 0,0,0,.   
1b09b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b09c 20 20 20 20 20 20 20 20 20 28 74 72 69 67 67 65           (trigge
1b09d 72 73 5f 65 78 69 73 74 20 26 20 54 52 49 47 47  rs_exist & TRIGG
1b09e 45 52 5f 41 46 54 45 52 29 21 3d 30 20 3f 20 6e  ER_AFTER)!=0 ? n
1b09f 65 77 49 64 78 20 3a 20 2d 31 2c 0a 20 20 20 20  ewIdx : -1,.    
1b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0a1 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c          appendFl
1b0a2 61 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ag);.    }.  }..
1b0a3 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
1b0a4 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 68  count of rows th
1b0a5 61 74 20 61 72 65 20 69 6e 73 65 72 74 65 64 0a  at are inserted.
1b0a6 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e    */.  if( (db->
1b0a7 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
1b0a8 6f 75 6e 74 52 6f 77 73 29 21 3d 30 20 29 7b 0a  ountRows)!=0 ){.
1b0a9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1b0aa 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e  ddOp(v, OP_MemIn
1b0ab 63 72 2c 20 31 2c 20 69 43 6e 74 4d 65 6d 29 3b  cr, 1, iCntMem);
1b0ac 0a 20 20 7d 0a 0a 20 20 69 66 28 20 74 72 69 67  .  }..  if( trig
1b0ad 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20  gers_exist ){.  
1b0ae 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74    /* Close all t
1b0af 61 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f 0a  ables opened */.
1b0b0 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20      if( !isView 
1b0b1 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b0b2 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b0b3 43 6c 6f 73 65 2c 20 62 61 73 65 2c 20 30 29 3b  Close, base, 0);
1b0b4 0a 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 31  .      for(idx=1
1b0b5 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
1b0b6 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
1b0b7 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 64 78  pIdx->pNext, idx
1b0b8 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ++){.        sql
1b0b9 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b0ba 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 64 78 2b 62   OP_Close, idx+b
1b0bb 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  ase, 0);.      }
1b0bc 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
1b0bd 6f 64 65 20 41 46 54 45 52 20 74 72 69 67 67 65  ode AFTER trigge
1b0be 72 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  rs */.    if( sq
1b0bf 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
1b0c0 67 65 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  ger(pParse, TK_I
1b0c1 4e 53 45 52 54 2c 20 30 2c 20 54 52 49 47 47 45  NSERT, 0, TRIGGE
1b0c2 52 5f 41 46 54 45 52 2c 20 70 54 61 62 2c 0a 20  R_AFTER, pTab,. 
1b0c3 20 20 20 20 20 20 20 20 20 6e 65 77 49 64 78 2c           newIdx,
1b0c4 20 2d 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e   -1, onError, en
1b0c5 64 4f 66 4c 6f 6f 70 29 20 29 7b 0a 20 20 20 20  dOfLoop) ){.    
1b0c6 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
1b0c7 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  eanup;.    }.  }
1b0c8 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f  ..  /* The botto
1b0c9 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2c 20 69  m of the loop, i
1b0ca 66 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63  f the data sourc
1b0cb 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74  e is a SELECT st
1b0cc 61 74 65 6d 65 6e 74 0a 20 20 2a 2f 0a 20 20 73  atement.  */.  s
1b0cd 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
1b0ce 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c  eLabel(v, endOfL
1b0cf 6f 6f 70 29 3b 0a 20 20 69 66 28 20 75 73 65 54  oop);.  if( useT
1b0d0 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
1b0d1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b0d2 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 72 63  (v, OP_Next, src
1b0d3 54 61 62 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20  Tab, iCont);.   
1b0d4 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
1b0d5 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 72 65  lveLabel(v, iBre
1b0d6 61 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ak);.    sqlite3
1b0d7 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b0d8 43 6c 6f 73 65 2c 20 73 72 63 54 61 62 2c 20 30  Close, srcTab, 0
1b0d9 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
1b0da 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71  Select ){.    sq
1b0db 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b0dc 2c 20 4f 50 5f 50 6f 70 2c 20 6e 43 6f 6c 75 6d  , OP_Pop, nColum
1b0dd 6e 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  n, 0);.    sqlit
1b0de 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1b0df 50 5f 52 65 74 75 72 6e 2c 20 30 2c 20 30 29 3b  P_Return, 0, 0);
1b0e0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b0e1 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
1b0e2 69 43 6c 65 61 6e 75 70 29 3b 0a 20 20 7d 0a 0a  iCleanup);.  }..
1b0e3 20 20 69 66 28 20 21 74 72 69 67 67 65 72 73 5f    if( !triggers_
1b0e4 65 78 69 73 74 20 26 26 20 21 49 73 56 69 72 74  exist && !IsVirt
1b0e5 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
1b0e6 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61   /* Close all ta
1b0e7 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f 0a 20  bles opened */. 
1b0e8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1b0e9 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
1b0ea 20 62 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 66   base, 0);.    f
1b0eb 6f 72 28 69 64 78 3d 31 2c 20 70 49 64 78 3d 70  or(idx=1, pIdx=p
1b0ec 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
1b0ed 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
1b0ee 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a 20 20 20  ext, idx++){.   
1b0ef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1b0f0 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
1b0f1 20 69 64 78 2b 62 61 73 65 2c 20 30 29 3b 0a 20   idx+base, 0);. 
1b0f2 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55     }.  }..  /* U
1b0f3 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  pdate the sqlite
1b0f4 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
1b0f5 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63  by storing the c
1b0f6 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20  ontent of the.  
1b0f7 2a 2a 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65  ** counter value
1b0f8 20 69 6e 20 6d 65 6d 6f 72 79 20 63 6f 75 6e 74   in memory count
1b0f9 65 72 4d 65 6d 20 62 61 63 6b 20 69 6e 74 6f 20  erMem back into 
1b0fa 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65  the sqlite_seque
1b0fb 6e 63 65 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 0a  nce.  ** table..
1b0fc 20 20 2a 2f 0a 20 20 61 75 74 6f 49 6e 63 45 6e    */.  autoIncEn
1b0fd 64 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  d(pParse, iDb, p
1b0fe 54 61 62 2c 20 63 6f 75 6e 74 65 72 4d 65 6d 29  Tab, counterMem)
1b0ff 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74  ;..  /*.  ** Ret
1b100 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1b101 66 20 72 6f 77 73 20 69 6e 73 65 72 74 65 64 2e  f rows inserted.
1b102 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
1b103 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61   is .  ** genera
1b104 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73  ting code becaus
1b105 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73  e of a call to s
1b106 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
1b107 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a  e(), do not.  **
1b108 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   invoke the call
1b109 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  back function.. 
1b10a 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c   */.  if( db->fl
1b10b 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
1b10c 6e 74 52 6f 77 73 20 26 26 20 70 50 61 72 73 65  ntRows && pParse
1b10d 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 21  ->nested==0 && !
1b10e 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
1b10f 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  k ){.    sqlite3
1b110 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b111 4d 65 6d 4c 6f 61 64 2c 20 69 43 6e 74 4d 65 6d  MemLoad, iCntMem
1b112 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1b113 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b114 5f 43 61 6c 6c 62 61 63 6b 2c 20 31 2c 20 30 29  _Callback, 1, 0)
1b115 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1b116 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
1b117 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1b118 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
1b119 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
1b11a 20 22 72 6f 77 73 20 69 6e 73 65 72 74 65 64 22   "rows inserted"
1b11b 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P3_STATIC);.  
1b11c 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  }..insert_cleanu
1b11d 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  p:.  sqlite3SrcL
1b11e 69 73 74 44 65 6c 65 74 65 28 70 54 61 62 4c 69  istDelete(pTabLi
1b11f 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  st);.  sqlite3Ex
1b120 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 4c 69  prListDelete(pLi
1b121 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  st);.  sqlite3Se
1b122 6c 65 63 74 44 65 6c 65 74 65 28 70 53 65 6c 65  lectDelete(pSele
1b123 63 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64  ct);.  sqlite3Id
1b124 4c 69 73 74 44 65 6c 65 74 65 28 70 43 6f 6c 75  ListDelete(pColu
1b125 6d 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  mn);.}../*.** Ge
1b126 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64  nerate code to d
1b127 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  o a constraint c
1b128 68 65 63 6b 20 70 72 69 6f 72 20 74 6f 20 61 6e  heck prior to an
1b129 20 49 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50   INSERT or an UP
1b12a 44 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  DATE..**.** When
1b12b 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1b12c 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 73 74 61   called, the sta
1b12d 63 6b 20 63 6f 6e 74 61 69 6e 73 20 28 66 72 6f  ck contains (fro
1b12e 6d 20 62 6f 74 74 6f 6d 20 74 6f 20 74 6f 70 29  m bottom to top)
1b12f 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** the followin
1b130 67 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a 20  g values:.**.** 
1b131 20 20 20 31 2e 20 20 54 68 65 20 72 6f 77 69 64     1.  The rowid
1b132 20 6f 66 20 74 68 65 20 72 6f 77 20 74 6f 20 62   of the row to b
1b133 65 20 75 70 64 61 74 65 64 20 62 65 66 6f 72 65  e updated before
1b134 20 74 68 65 20 75 70 64 61 74 65 2e 20 20 54 68   the update.  Th
1b135 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 76 61 6c  is.**        val
1b136 75 65 20 69 73 20 6f 6d 69 74 74 65 64 20 75 6e  ue is omitted un
1b137 6c 65 73 73 20 77 65 20 61 72 65 20 64 6f 69 6e  less we are doin
1b138 67 20 61 6e 20 55 50 44 41 54 45 20 74 68 61 74  g an UPDATE that
1b139 20 69 6e 76 6f 6c 76 65 73 20 61 0a 2a 2a 20 20   involves a.**  
1b13a 20 20 20 20 20 20 63 68 61 6e 67 65 20 74 6f 20        change to 
1b13b 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
1b13c 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 32 2e 20 20  r..**.**    2.  
1b13d 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  The rowid of the
1b13e 20 72 6f 77 20 61 66 74 65 72 20 74 68 65 20 75   row after the u
1b13f 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  pdate..**.**    
1b140 33 2e 20 20 54 68 65 20 64 61 74 61 20 69 6e 20  3.  The data in 
1b141 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  the first column
1b142 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61 66   of the entry af
1b143 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a  ter the update..
1b144 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20 44 61 74  **.**    i.  Dat
1b145 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65 20 63 6f  a from middle co
1b146 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20  lumns....**.**  
1b147 20 20 4e 2e 20 20 54 68 65 20 64 61 74 61 20 69    N.  The data i
1b148 6e 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  n the last colum
1b149 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61  n of the entry a
1b14a 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e  fter the update.
1b14b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 72  .**.** The old r
1b14c 6f 77 69 64 20 73 68 6f 77 6e 20 61 73 20 65 6e  owid shown as en
1b14d 74 72 79 20 28 31 29 20 61 62 6f 76 65 20 69 73  try (1) above is
1b14e 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65 73 73 20   omitted unless 
1b14f 62 6f 74 68 20 69 73 55 70 64 61 74 65 0a 2a 2a  both isUpdate.**
1b150 20 61 6e 64 20 72 6f 77 69 64 43 68 6e 67 20 61   and rowidChng a
1b151 72 65 20 31 2e 20 20 69 73 55 70 64 61 74 65 20  re 1.  isUpdate 
1b152 69 73 20 74 72 75 65 20 66 6f 72 20 55 50 44 41  is true for UPDA
1b153 54 45 73 20 61 6e 64 20 66 61 6c 73 65 20 66 6f  TEs and false fo
1b154 72 0a 2a 2a 20 49 4e 53 45 52 54 73 20 61 6e 64  r.** INSERTs and
1b155 20 72 6f 77 69 64 43 68 6e 67 20 69 73 20 74 72   rowidChng is tr
1b156 75 65 20 69 66 20 74 68 65 20 72 65 63 6f 72 64  ue if the record
1b157 20 6e 75 6d 62 65 72 20 69 73 20 62 65 69 6e 67   number is being
1b158 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
1b159 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  The code generat
1b15a 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
1b15b 6e 65 20 70 75 73 68 65 73 20 61 64 64 69 74 69  ne pushes additi
1b15c 6f 6e 61 6c 20 65 6e 74 72 69 65 73 20 6f 6e 74  onal entries ont
1b15d 6f 0a 2a 2a 20 74 68 65 20 73 74 61 63 6b 20 77  o.** the stack w
1b15e 68 69 63 68 20 61 72 65 20 74 68 65 20 6b 65 79  hich are the key
1b15f 73 20 66 6f 72 20 6e 65 77 20 69 6e 64 65 78 20  s for new index 
1b160 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 20  entries for the 
1b161 6e 65 77 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 54  new record..** T
1b162 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64 65  he order of inde
1b163 78 20 6b 65 79 73 20 69 73 20 74 68 65 20 73 61  x keys is the sa
1b164 6d 65 20 61 73 20 74 68 65 20 6f 72 64 65 72 20  me as the order 
1b165 6f 66 20 74 68 65 20 69 6e 64 69 63 65 73 20 6f  of the indices o
1b166 6e 0a 2a 2a 20 74 68 65 20 70 54 61 62 6c 65 2d  n.** the pTable-
1b167 3e 70 49 6e 64 65 78 20 6c 69 73 74 2e 20 20 41  >pIndex list.  A
1b168 20 6b 65 79 20 69 73 20 6f 6e 6c 79 20 63 72 65   key is only cre
1b169 61 74 65 64 20 66 6f 72 20 69 6e 64 65 78 20 69  ated for index i
1b16a 20 69 66 20 0a 2a 2a 20 61 49 64 78 55 73 65 64   if .** aIdxUsed
1b16b 21 3d 30 20 61 6e 64 20 61 49 64 78 55 73 65 64  !=0 and aIdxUsed
1b16c 5b 69 5d 21 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68  [i]!=0..**.** Th
1b16d 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20  is routine also 
1b16e 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
1b16f 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69  o check constrai
1b170 6e 74 73 2e 20 20 4e 4f 54 20 4e 55 4c 4c 2c 0a  nts.  NOT NULL,.
1b171 2a 2a 20 43 48 45 43 4b 2c 20 61 6e 64 20 55 4e  ** CHECK, and UN
1b172 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 73  IQUE constraints
1b173 20 61 72 65 20 61 6c 6c 20 63 68 65 63 6b 65 64   are all checked
1b174 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61 69  .  If a constrai
1b175 6e 74 20 66 61 69 6c 73 2c 0a 2a 2a 20 74 68 65  nt fails,.** the
1b176 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
1b177 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72 66  e action is perf
1b178 6f 72 6d 65 64 2e 20 20 54 68 65 72 65 20 61 72  ormed.  There ar
1b179 65 20 66 69 76 65 20 70 6f 73 73 69 62 6c 65 0a  e five possible.
1b17a 2a 2a 20 61 63 74 69 6f 6e 73 3a 20 52 4f 4c 4c  ** actions: ROLL
1b17b 42 41 43 4b 2c 20 41 42 4f 52 54 2c 20 46 41 49  BACK, ABORT, FAI
1b17c 4c 2c 20 52 45 50 4c 41 43 45 2c 20 61 6e 64 20  L, REPLACE, and 
1b17d 49 47 4e 4f 52 45 2e 0a 2a 2a 0a 2a 2a 20 20 43  IGNORE..**.**  C
1b17e 6f 6e 73 74 72 61 69 6e 74 20 74 79 70 65 20 20  onstraint type  
1b17f 41 63 74 69 6f 6e 20 20 20 20 20 20 20 57 68 61  Action       Wha
1b180 74 20 48 61 70 70 65 6e 73 0a 2a 2a 20 20 2d 2d  t Happens.**  --
1b181 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d  -------------  -
1b182 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 2d 2d 2d 2d  ---------   ----
1b183 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b184 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b185 2d 2d 2d 2d 0a 2a 2a 20 20 61 6e 79 20 20 20 20  ----.**  any    
1b186 20 20 20 20 20 20 20 20 20 20 52 4f 4c 4c 42 41            ROLLBA
1b187 43 4b 20 20 20 20 20 54 68 65 20 63 75 72 72 65  CK     The curre
1b188 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
1b189 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e  s rolled back an
1b18a 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
1b18b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b18c 20 20 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63      sqlite3_exec
1b18d 28 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65 64  () returns immed
1b18e 69 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a  iately with a.**
1b18f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b191 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53  return code of S
1b192 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
1b193 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20  ..**.**  any    
1b194 20 20 20 20 20 20 20 20 20 20 41 42 4f 52 54 20            ABORT 
1b195 20 20 20 20 20 20 20 42 61 63 6b 20 6f 75 74 20         Back out 
1b196 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 74 68 65  changes from the
1b197 20 63 75 72 72 65 6e 74 20 63 6f 6d 6d 61 6e 64   current command
1b198 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1b199 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b19a 20 20 20 6f 6e 6c 79 20 28 64 6f 20 6e 6f 74 20     only (do not 
1b19b 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65 20 72 6f  do a complete ro
1b19c 6c 6c 62 61 63 6b 29 20 74 68 65 6e 0a 2a 2a 20  llback) then.** 
1b19d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b19e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
1b19f 61 75 73 65 20 73 71 6c 69 74 65 33 5f 65 78 65  ause sqlite3_exe
1b1a0 63 28 29 20 74 6f 20 72 65 74 75 72 6e 20 69 6d  c() to return im
1b1a1 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 20 20 20  mediately.**    
1b1a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1a3 20 20 20 20 20 20 20 20 20 20 20 20 77 69 74 68              with
1b1a4 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
1b1a5 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20  NT..**.**  any  
1b1a6 20 20 20 20 20 20 20 20 20 20 20 20 46 41 49 4c              FAIL
1b1a7 20 20 20 20 20 20 20 20 20 53 71 6c 69 74 65 5f           Sqlite_
1b1a8 65 78 65 63 28 29 20 72 65 74 75 72 6e 73 20 69  exec() returns i
1b1a9 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68 20  mmediately with 
1b1aa 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  a.**            
1b1ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1ac 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65 20      return code 
1b1ad 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  of SQLITE_CONSTR
1b1ae 41 49 4e 54 2e 20 20 54 68 65 0a 2a 2a 20 20 20  AINT.  The.**   
1b1af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 72 61               tra
1b1b1 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  nsaction is not 
1b1b2 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20  rolled back and 
1b1b3 61 6e 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  any.**          
1b1b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1b5 20 20 20 20 20 20 70 72 69 6f 72 20 63 68 61 6e        prior chan
1b1b6 67 65 73 20 61 72 65 20 72 65 74 61 69 6e 65 64  ges are retained
1b1b7 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20  ..**.**  any    
1b1b8 20 20 20 20 20 20 20 20 20 20 49 47 4e 4f 52 45            IGNORE
1b1b9 20 20 20 20 20 20 20 54 68 65 20 72 65 63 6f 72         The recor
1b1ba 64 20 6e 75 6d 62 65 72 20 61 6e 64 20 64 61 74  d number and dat
1b1bb 61 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d  a is popped from
1b1bc 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1b1bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1be 20 20 20 74 68 65 20 73 74 61 63 6b 20 61 6e 64     the stack and
1b1bf 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6d 6d   there is an imm
1b1c0 65 64 69 61 74 65 20 6a 75 6d 70 0a 2a 2a 20 20  ediate jump.**  
1b1c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
1b1c3 20 6c 61 62 65 6c 20 69 67 6e 6f 72 65 44 65 73   label ignoreDes
1b1c4 74 2e 0a 2a 2a 0a 2a 2a 20 20 4e 4f 54 20 4e 55  t..**.**  NOT NU
1b1c5 4c 4c 20 20 20 20 20 20 20 20 20 52 45 50 4c 41  LL         REPLA
1b1c6 43 45 20 20 20 20 20 20 54 68 65 20 4e 55 4c 4c  CE      The NULL
1b1c7 20 76 61 6c 75 65 20 69 73 20 72 65 70 6c 61 63   value is replac
1b1c8 65 20 62 79 20 74 68 65 20 64 65 66 61 75 6c 74  e by the default
1b1c9 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1b1ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1cb 20 20 20 76 61 6c 75 65 20 66 6f 72 20 74 68 61     value for tha
1b1cc 74 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68  t column.  If th
1b1cd 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a  e default value.
1b1ce 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1b1cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1d0 20 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 61    is NULL, the a
1b1d1 63 74 69 6f 6e 20 69 73 20 74 68 65 20 73 61 6d  ction is the sam
1b1d2 65 20 61 73 20 41 42 4f 52 54 2e 0a 2a 2a 0a 2a  e as ABORT..**.*
1b1d3 2a 20 20 55 4e 49 51 55 45 20 20 20 20 20 20 20  *  UNIQUE       
1b1d4 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20 20      REPLACE     
1b1d5 20 54 68 65 20 6f 74 68 65 72 20 72 6f 77 20 74   The other row t
1b1d6 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77 69  hat conflicts wi
1b1d7 74 68 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20  th the row.**   
1b1d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1d9 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 69               bei
1b1da 6e 67 20 69 6e 73 65 72 74 65 64 20 69 73 20 72  ng inserted is r
1b1db 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 43  emoved..**.**  C
1b1dc 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20 20  HECK            
1b1dd 52 45 50 4c 41 43 45 20 20 20 20 20 20 49 6c 6c  REPLACE      Ill
1b1de 65 67 61 6c 2e 20 20 54 68 65 20 72 65 73 75 6c  egal.  The resul
1b1df 74 73 20 69 6e 20 61 6e 20 65 78 63 65 70 74 69  ts in an excepti
1b1e0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 69 63 68 20  on..**.** Which 
1b1e1 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 69  action to take i
1b1e2 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20  s determined by 
1b1e3 74 68 65 20 6f 76 65 72 72 69 64 65 45 72 72 6f  the overrideErro
1b1e4 72 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20  r parameter..** 
1b1e5 4f 72 20 69 66 20 6f 76 65 72 72 69 64 65 45 72  Or if overrideEr
1b1e6 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 2c  ror==OE_Default,
1b1e7 20 74 68 65 6e 20 74 68 65 20 70 50 61 72 73 65   then the pParse
1b1e8 2d 3e 6f 6e 45 72 72 6f 72 20 70 61 72 61 6d 65  ->onError parame
1b1e9 74 65 72 0a 2a 2a 20 69 73 20 75 73 65 64 2e 20  ter.** is used. 
1b1ea 20 4f 72 20 69 66 20 70 50 61 72 73 65 2d 3e 6f   Or if pParse->o
1b1eb 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
1b1ec 6c 74 20 74 68 65 6e 20 74 68 65 20 6f 6e 45 72  lt then the onEr
1b1ed 72 6f 72 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72  ror value.** for
1b1ee 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
1b1ef 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  is used..**.** T
1b1f0 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69  he calling routi
1b1f1 6e 65 20 6d 75 73 74 20 6f 70 65 6e 20 61 20 72  ne must open a r
1b1f2 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
1b1f3 20 66 6f 72 20 70 54 61 62 20 77 69 74 68 0a 2a   for pTab with.*
1b1f4 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * cursor number 
1b1f5 22 62 61 73 65 22 2e 20 20 41 6c 6c 20 69 6e 64  "base".  All ind
1b1f6 69 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73  ices of pTab mus
1b1f7 74 20 61 6c 73 6f 20 68 61 76 65 20 6f 70 65 6e  t also have open
1b1f8 0a 2a 2a 20 72 65 61 64 2f 77 72 69 74 65 20 63  .** read/write c
1b1f9 75 72 73 6f 72 73 20 77 69 74 68 20 63 75 72 73  ursors with curs
1b1fa 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65 2b 69  or number base+i
1b1fb 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 75   for the i-th cu
1b1fc 72 73 6f 72 2e 0a 2a 2a 20 45 78 63 65 70 74 2c  rsor..** Except,
1b1fd 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
1b1fe 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 61  possibility of a
1b1ff 20 52 45 50 4c 41 43 45 20 61 63 74 69 6f 6e 20   REPLACE action 
1b200 74 68 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20  then.** cursors 
1b201 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  do not need to b
1b202 65 20 6f 70 65 6e 20 66 6f 72 20 69 6e 64 69 63  e open for indic
1b203 65 73 20 77 68 65 72 65 20 61 49 64 78 55 73 65  es where aIdxUse
1b204 64 5b 69 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 49  d[i]==0..**.** I
1b205 66 20 74 68 65 20 69 73 55 70 64 61 74 65 20 66  f the isUpdate f
1b206 6c 61 67 20 69 73 20 74 72 75 65 2c 20 69 74 20  lag is true, it 
1b207 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 22  means that the "
1b208 62 61 73 65 22 20 63 75 72 73 6f 72 20 69 73 0a  base" cursor is.
1b209 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 70 6f 69  ** initially poi
1b20a 6e 74 69 6e 67 20 74 6f 20 61 6e 20 65 6e 74 72  nting to an entr
1b20b 79 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20  y that is being 
1b20c 75 70 64 61 74 65 64 2e 20 20 54 68 65 20 69 73  updated.  The is
1b20d 55 70 64 61 74 65 0a 2a 2a 20 66 6c 61 67 20 63  Update.** flag c
1b20e 61 75 73 65 73 20 65 78 74 72 61 20 63 6f 64 65  auses extra code
1b20f 20 74 6f 20 62 65 20 67 65 6e 65 72 61 74 65 64   to be generated
1b210 20 73 6f 20 74 68 61 74 20 74 68 65 20 22 62 61   so that the "ba
1b211 73 65 22 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73  se" cursor.** is
1b212 20 73 74 69 6c 6c 20 70 6f 69 6e 74 69 6e 67 20   still pointing 
1b213 61 74 20 74 68 65 20 73 61 6d 65 20 65 6e 74 72  at the same entr
1b214 79 20 61 66 74 65 72 20 74 68 65 20 72 6f 75 74  y after the rout
1b215 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20  ine returns..** 
1b216 57 69 74 68 6f 75 74 20 74 68 65 20 69 73 55 70  Without the isUp
1b217 64 61 74 65 20 66 6c 61 67 2c 20 74 68 65 20 22  date flag, the "
1b218 62 61 73 65 22 20 63 75 72 73 6f 72 20 6d 69 67  base" cursor mig
1b219 68 74 20 62 65 20 6d 6f 76 65 64 2e 0a 2a 2f 0a  ht be moved..*/.
1b21a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1b21b 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72  oid sqlite3Gener
1b21c 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65  ateConstraintChe
1b21d 63 6b 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50  cks(.  Parse *pP
1b21e 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
1b21f 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
1b220 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
1b221 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  b,        /* the
1b222 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
1b223 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
1b224 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65  ng */.  int base
1b225 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ,           /* I
1b226 6e 64 65 78 20 6f 66 20 61 20 72 65 61 64 2f 77  ndex of a read/w
1b227 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e  rite cursor poin
1b228 74 69 6e 67 20 61 74 20 70 54 61 62 20 2a 2f 0a  ting at pTab */.
1b229 20 20 63 68 61 72 20 2a 61 49 64 78 55 73 65 64    char *aIdxUsed
1b22a 2c 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 69  ,     /* Which i
1b22b 6e 64 69 63 65 73 20 61 72 65 20 75 73 65 64 2e  ndices are used.
1b22c 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 61 6c 6c    NULL means all
1b22d 20 61 72 65 20 75 73 65 64 20 2a 2f 0a 20 20 69   are used */.  i
1b22e 6e 74 20 72 6f 77 69 64 43 68 6e 67 2c 20 20 20  nt rowidChng,   
1b22f 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
1b230 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
1b231 77 69 6c 6c 20 63 68 61 6e 67 65 20 2a 2f 0a 20  will change */. 
1b232 20 69 6e 74 20 69 73 55 70 64 61 74 65 2c 20 20   int isUpdate,  
1b233 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
1b234 20 55 50 44 41 54 45 2c 20 46 61 6c 73 65 20 66   UPDATE, False f
1b235 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69  or INSERT */.  i
1b236 6e 74 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72  nt overrideError
1b237 2c 20 20 2f 2a 20 4f 76 65 72 72 69 64 65 20 6f  ,  /* Override o
1b238 6e 45 72 72 6f 72 20 74 6f 20 74 68 69 73 20 69  nError to this i
1b239 66 20 6e 6f 74 20 4f 45 5f 44 65 66 61 75 6c 74  f not OE_Default
1b23a 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65   */.  int ignore
1b23b 44 65 73 74 20 20 20 20 20 20 2f 2a 20 4a 75 6d  Dest      /* Jum
1b23c 70 20 74 6f 20 74 68 69 73 20 6c 61 62 65 6c 20  p to this label 
1b23d 6f 6e 20 61 6e 20 4f 45 5f 49 67 6e 6f 72 65 20  on an OE_Ignore 
1b23e 72 65 73 6f 6c 75 74 69 6f 6e 20 2a 2f 0a 29 7b  resolution */.){
1b23f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65  .  int i;.  Vdbe
1b240 20 2a 76 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b   *v;.  int nCol;
1b241 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 3b 0a  .  int onError;.
1b242 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 69 6e    int addr;.  in
1b243 74 20 65 78 74 72 61 3b 0a 20 20 69 6e 74 20 69  t extra;.  int i
1b244 43 75 72 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49  Cur;.  Index *pI
1b245 64 78 3b 0a 20 20 69 6e 74 20 73 65 65 6e 52 65  dx;.  int seenRe
1b246 70 6c 61 63 65 20 3d 20 30 3b 0a 20 20 69 6e 74  place = 0;.  int
1b247 20 6a 75 6d 70 49 6e 73 74 31 3d 30 2c 20 6a 75   jumpInst1=0, ju
1b248 6d 70 49 6e 73 74 32 3b 0a 20 20 69 6e 74 20 68  mpInst2;.  int h
1b249 61 73 54 77 6f 52 6f 77 69 64 73 20 3d 20 28 69  asTwoRowids = (i
1b24a 73 55 70 64 61 74 65 20 26 26 20 72 6f 77 69 64  sUpdate && rowid
1b24b 43 68 6e 67 29 3b 0a 0a 20 20 76 20 3d 20 73 71  Chng);..  v = sq
1b24c 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
1b24d 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rse);.  assert( 
1b24e 76 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  v!=0 );.  assert
1b24f 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d  ( pTab->pSelect=
1b250 3d 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74  =0 );  /* This t
1b251 61 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49  able is not a VI
1b252 45 57 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70  EW */.  nCol = p
1b253 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 0a 20 20 2f 2a  Tab->nCol;..  /*
1b254 20 54 65 73 74 20 61 6c 6c 20 4e 4f 54 20 4e 55   Test all NOT NU
1b255 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a  LL constraints..
1b256 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
1b257 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
1b258 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69    if( i==pTab->i
1b259 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 63 6f  PKey ){.      co
1b25a 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20  ntinue;.    }.  
1b25b 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61 62    onError = pTab
1b25c 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c  ->aCol[i].notNul
1b25d 6c 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72  l;.    if( onErr
1b25e 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 20 63 6f  or==OE_None ) co
1b25f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
1b260 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f  overrideError!=O
1b261 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20  E_Default ){.   
1b262 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65     onError = ove
1b263 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20  rrideError;.    
1b264 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f  }else if( onErro
1b265 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b  r==OE_Default ){
1b266 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
1b267 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d   OE_Abort;.    }
1b268 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72  .    if( onError
1b269 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 26 26 20  ==OE_Replace && 
1b26a 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44  pTab->aCol[i].pD
1b26b 66 6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  flt==0 ){.      
1b26c 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
1b26d 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  rt;.    }.    sq
1b26e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b26f 2c 20 4f 50 5f 44 75 70 2c 20 6e 43 6f 6c 2d 31  , OP_Dup, nCol-1
1b270 2d 69 2c 20 31 29 3b 0a 20 20 20 20 61 64 64 72  -i, 1);.    addr
1b271 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1b272 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  dOp(v, OP_NotNul
1b273 6c 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 61 73  l, 1, 0);.    as
1b274 73 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f  sert( onError==O
1b275 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e  E_Rollback || on
1b276 45 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20  Error==OE_Abort 
1b277 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46  || onError==OE_F
1b278 61 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f  ail.        || o
1b279 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72  nError==OE_Ignor
1b27a 65 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  e || onError==OE
1b27b 5f 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20  _Replace );.    
1b27c 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20  switch( onError 
1b27d 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  ){.      case OE
1b27e 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20  _Rollback:.     
1b27f 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a   case OE_Abort:.
1b280 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61        case OE_Fa
1b281 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 68  il: {.        ch
1b282 61 72 20 2a 7a 4d 73 67 20 3d 20 30 3b 0a 20 20  ar *zMsg = 0;.  
1b283 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b284 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 48 61 6c  eAddOp(v, OP_Hal
1b285 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  t, SQLITE_CONSTR
1b286 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a  AINT, onError);.
1b287 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
1b288 65 74 53 74 72 69 6e 67 28 26 7a 4d 73 67 2c 20  etString(&zMsg, 
1b289 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 2e 22  pTab->zName, "."
1b28a 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e  , pTab->aCol[i].
1b28b 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
1b28c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
1b28d 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c   may not be NULL
1b28e 22 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  ", (char*)0);.  
1b28f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b290 65 43 68 61 6e 67 65 50 33 28 76 2c 20 2d 31 2c  eChangeP3(v, -1,
1b291 20 7a 4d 73 67 2c 20 50 33 5f 44 59 4e 41 4d 49   zMsg, P3_DYNAMI
1b292 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  C);.        brea
1b293 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
1b294 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a   case OE_Ignore:
1b295 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
1b296 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1b297 50 5f 50 6f 70 2c 20 6e 43 6f 6c 2b 31 2b 68 61  P_Pop, nCol+1+ha
1b298 73 54 77 6f 52 6f 77 69 64 73 2c 20 30 29 3b 0a  sTwoRowids, 0);.
1b299 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1b29a 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47  dbeAddOp(v, OP_G
1b29b 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65  oto, 0, ignoreDe
1b29c 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  st);.        bre
1b29d 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1b29e 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c 61 63    case OE_Replac
1b29f 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e: {.        sql
1b2a0 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1b2a1 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  rse, pTab->aCol[
1b2a2 69 5d 2e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20  i].pDflt);.     
1b2a3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1b2a4 64 4f 70 28 76 2c 20 4f 50 5f 50 75 73 68 2c 20  dOp(v, OP_Push, 
1b2a5 6e 43 6f 6c 2d 69 2c 20 30 29 3b 0a 20 20 20 20  nCol-i, 0);.    
1b2a6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1b2a7 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
1b2a8 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1b2a9 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 0a  (v, addr);.  }..
1b2aa 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20 43 48    /* Test all CH
1b2ab 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  ECK constraints.
1b2ac 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
1b2ad 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20  ITE_OMIT_CHECK. 
1b2ae 20 69 66 28 20 70 54 61 62 2d 3e 70 43 68 65 63   if( pTab->pChec
1b2af 6b 20 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62  k && (pParse->db
1b2b0 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1b2b1 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 29 3d 3d  _IgnoreChecks)==
1b2b2 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 6c 6c  0 ){.    int all
1b2b3 4f 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  Ok = sqlite3Vdbe
1b2b4 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
1b2b5 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
1b2b6 2d 3e 63 6b 4f 66 66 73 65 74 3d 3d 30 20 29 3b  ->ckOffset==0 );
1b2b7 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6b 4f  .    pParse->ckO
1b2b8 66 66 73 65 74 20 3d 20 6e 43 6f 6c 3b 0a 20 20  ffset = nCol;.  
1b2b9 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54    sqlite3ExprIfT
1b2ba 72 75 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  rue(pParse, pTab
1b2bb 2d 3e 70 43 68 65 63 6b 2c 20 61 6c 6c 4f 6b 2c  ->pCheck, allOk,
1b2bc 20 31 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   1);.    assert(
1b2bd 20 70 50 61 72 73 65 2d 3e 63 6b 4f 66 66 73 65   pParse->ckOffse
1b2be 74 3d 3d 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 70  t==nCol );.    p
1b2bf 50 61 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74 20  Parse->ckOffset 
1b2c0 3d 20 30 3b 0a 20 20 20 20 6f 6e 45 72 72 6f 72  = 0;.    onError
1b2c1 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72   = overrideError
1b2c2 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 3f 20 6f  !=OE_Default ? o
1b2c3 76 65 72 72 69 64 65 45 72 72 6f 72 20 3a 20 4f  verrideError : O
1b2c4 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28  E_Abort;.    if(
1b2c5 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e   onError==OE_Ign
1b2c6 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ore ){.      sql
1b2c7 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b2c8 20 4f 50 5f 50 6f 70 2c 20 6e 43 6f 6c 2b 31 2b   OP_Pop, nCol+1+
1b2c9 68 61 73 54 77 6f 52 6f 77 69 64 73 2c 20 30 29  hasTwoRowids, 0)
1b2ca 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1b2cb 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47  dbeAddOp(v, OP_G
1b2cc 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65  oto, 0, ignoreDe
1b2cd 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  st);.    }else{.
1b2ce 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b2cf 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 48 61 6c  eAddOp(v, OP_Hal
1b2d0 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  t, SQLITE_CONSTR
1b2d1 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a  AINT, onError);.
1b2d2 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1b2d3 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1b2d4 6c 28 76 2c 20 61 6c 6c 4f 6b 29 3b 0a 20 20 7d  l(v, allOk);.  }
1b2d5 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
1b2d6 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1b2d7 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20  CHECK) */..  /* 
1b2d8 49 66 20 77 65 20 68 61 76 65 20 61 6e 20 49 4e  If we have an IN
1b2d9 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
1b2da 59 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  Y, make sure the
1b2db 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a   primary key.  *
1b2dc 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63  * of the new rec
1b2dd 6f 72 64 20 64 6f 65 73 20 6e 6f 74 20 70 72 65  ord does not pre
1b2de 76 69 6f 75 73 6c 79 20 65 78 69 73 74 2e 20 20  viously exist.  
1b2df 45 78 63 65 70 74 2c 20 69 66 20 74 68 69 73 0a  Except, if this.
1b2e0 20 20 2a 2a 20 69 73 20 61 6e 20 55 50 44 41 54    ** is an UPDAT
1b2e1 45 20 61 6e 64 20 74 68 65 20 70 72 69 6d 61 72  E and the primar
1b2e2 79 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 68 61  y key is not cha
1b2e3 6e 67 69 6e 67 2c 20 74 68 61 74 20 69 73 20 4f  nging, that is O
1b2e4 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 6f  K..  */.  if( ro
1b2e5 77 69 64 43 68 6e 67 20 29 7b 0a 20 20 20 20 6f  widChng ){.    o
1b2e6 6e 45 72 72 6f 72 20 3d 20 70 54 61 62 2d 3e 6b  nError = pTab->k
1b2e7 65 79 43 6f 6e 66 3b 0a 20 20 20 20 69 66 28 20  eyConf;.    if( 
1b2e8 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f  overrideError!=O
1b2e9 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20  E_Default ){.   
1b2ea 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65     onError = ove
1b2eb 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20  rrideError;.    
1b2ec 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f  }else if( onErro
1b2ed 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b  r==OE_Default ){
1b2ee 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
1b2ef 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d   OE_Abort;.    }
1b2f0 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 69 73  .    .    if( is
1b2f1 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Update ){.      
1b2f2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b2f3 28 76 2c 20 4f 50 5f 44 75 70 2c 20 6e 43 6f 6c  (v, OP_Dup, nCol
1b2f4 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  +1, 1);.      sq
1b2f5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b2f6 2c 20 4f 50 5f 44 75 70 2c 20 6e 43 6f 6c 2b 31  , OP_Dup, nCol+1
1b2f7 2c 20 31 29 3b 0a 20 20 20 20 20 20 6a 75 6d 70  , 1);.      jump
1b2f8 49 6e 73 74 31 20 3d 20 73 71 6c 69 74 65 33 56  Inst1 = sqlite3V
1b2f9 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 45  dbeAddOp(v, OP_E
1b2fa 71 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  q, 0, 0);.    }.
1b2fb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1b2fc 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20  ddOp(v, OP_Dup, 
1b2fd 6e 43 6f 6c 2c 20 31 29 3b 0a 20 20 20 20 6a 75  nCol, 1);.    ju
1b2fe 6d 70 49 6e 73 74 32 20 3d 20 73 71 6c 69 74 65  mpInst2 = sqlite
1b2ff 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b300 5f 4e 6f 74 45 78 69 73 74 73 2c 20 62 61 73 65  _NotExists, base
1b301 2c 20 30 29 3b 0a 20 20 20 20 73 77 69 74 63 68  , 0);.    switch
1b302 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20  ( onError ){.   
1b303 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
1b304 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20        onError = 
1b305 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  OE_Abort;.      
1b306 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69    /* Fall thru i
1b307 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
1b308 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  e */.      }.   
1b309 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62     case OE_Rollb
1b30a 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ack:.      case 
1b30b 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20  OE_Abort:.      
1b30c 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a  case OE_Fail: {.
1b30d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1b30e 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 48 61 6c  dbeOp3(v, OP_Hal
1b30f 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  t, SQLITE_CONSTR
1b310 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 0a 20  AINT, onError,. 
1b311 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b312 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 52 59          "PRIMARY
1b313 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75 6e 69   KEY must be uni
1b314 71 75 65 22 2c 20 50 33 5f 53 54 41 54 49 43 29  que", P3_STATIC)
1b315 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1b316 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
1b317 61 73 65 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20  ase OE_Replace: 
1b318 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1b319 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
1b31a 78 44 65 6c 65 74 65 28 76 2c 20 70 54 61 62 2c  xDelete(v, pTab,
1b31b 20 62 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 20   base, 0);.     
1b31c 20 20 20 69 66 28 20 69 73 55 70 64 61 74 65 20     if( isUpdate 
1b31d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1b31e 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b31f 20 4f 50 5f 44 75 70 2c 20 6e 43 6f 6c 2b 68 61   OP_Dup, nCol+ha
1b320 73 54 77 6f 52 6f 77 69 64 73 2c 20 31 29 3b 0a  sTwoRowids, 1);.
1b321 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1b322 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b323 5f 4d 6f 76 65 47 65 2c 20 62 61 73 65 2c 20 30  _MoveGe, base, 0
1b324 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1b325 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65       seenReplace
1b326 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72   = 1;.        br
1b327 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1b328 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
1b329 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73  e: {.        ass
1b32a 65 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65  ert( seenReplace
1b32b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73  ==0 );.        s
1b32c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1b32d 76 2c 20 4f 50 5f 50 6f 70 2c 20 6e 43 6f 6c 2b  v, OP_Pop, nCol+
1b32e 31 2b 68 61 73 54 77 6f 52 6f 77 69 64 73 2c 20  1+hasTwoRowids, 
1b32f 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
1b330 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1b331 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f  OP_Goto, 0, igno
1b332 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20  reDest);.       
1b333 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1b334 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1b335 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1b336 20 6a 75 6d 70 49 6e 73 74 32 29 3b 0a 20 20 20   jumpInst2);.   
1b337 20 69 66 28 20 69 73 55 70 64 61 74 65 20 29 7b   if( isUpdate ){
1b338 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1b339 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 75  beJumpHere(v, ju
1b33a 6d 70 49 6e 73 74 31 29 3b 0a 20 20 20 20 20 20  mpInst1);.      
1b33b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b33c 28 76 2c 20 4f 50 5f 44 75 70 2c 20 6e 43 6f 6c  (v, OP_Dup, nCol
1b33d 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  +1, 1);.      sq
1b33e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b33f 2c 20 4f 50 5f 4d 6f 76 65 47 65 2c 20 62 61 73  , OP_MoveGe, bas
1b340 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  e, 0);.    }.  }
1b341 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20  ..  /* Test all 
1b342 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e  UNIQUE constrain
1b343 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 20 65  ts by creating e
1b344 6e 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20  ntries for each 
1b345 55 4e 49 51 55 45 0a 20 20 2a 2a 20 69 6e 64 65  UNIQUE.  ** inde
1b346 78 20 61 6e 64 20 6d 61 6b 69 6e 67 20 73 75 72  x and making sur
1b347 65 20 74 68 61 74 20 64 75 70 6c 69 63 61 74 65  e that duplicate
1b348 20 65 6e 74 72 69 65 73 20 64 6f 20 6e 6f 74 20   entries do not 
1b349 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 0a 20  already exist.. 
1b34a 20 2a 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20   ** Add the new 
1b34b 72 65 63 6f 72 64 73 20 74 6f 20 74 68 65 20 69  records to the i
1b34c 6e 64 69 63 65 73 20 61 73 20 77 65 20 67 6f 2e  ndices as we go.
1b34d 0a 20 20 2a 2f 0a 20 20 65 78 74 72 61 20 3d 20  .  */.  extra = 
1b34e 2d 31 3b 0a 20 20 66 6f 72 28 69 43 75 72 3d 30  -1;.  for(iCur=0
1b34f 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
1b350 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
1b351 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 43 75  pIdx->pNext, iCu
1b352 72 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 49  r++){.    if( aI
1b353 64 78 55 73 65 64 20 26 26 20 61 49 64 78 55 73  dxUsed && aIdxUs
1b354 65 64 5b 69 43 75 72 5d 3d 3d 30 20 29 20 63 6f  ed[iCur]==0 ) co
1b355 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 53 6b 69 70  ntinue;  /* Skip
1b356 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20   unused indices 
1b357 2a 2f 0a 20 20 20 20 65 78 74 72 61 2b 2b 3b 0a  */.    extra++;.
1b358 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61  .    /* Create a
1b359 20 6b 65 79 20 66 6f 72 20 61 63 63 65 73 73 69   key for accessi
1b35a 6e 67 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74  ng the index ent
1b35b 72 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  ry */.    sqlite
1b35c 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b35d 5f 44 75 70 2c 20 6e 43 6f 6c 2b 65 78 74 72 61  _Dup, nCol+extra
1b35e 2c 20 31 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  , 1);.    for(i=
1b35f 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; i<pIdx->nColu
1b360 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
1b361 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d 3e  int idx = pIdx->
1b362 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20  aiColumn[i];.   
1b363 20 20 20 69 66 28 20 69 64 78 3d 3d 70 54 61 62     if( idx==pTab
1b364 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
1b365 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1b366 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 69  dOp(v, OP_Dup, i
1b367 2b 65 78 74 72 61 2b 6e 43 6f 6c 2b 31 2c 20 31  +extra+nCol+1, 1
1b368 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1b369 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1b36a 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44  dbeAddOp(v, OP_D
1b36b 75 70 2c 20 69 2b 65 78 74 72 61 2b 6e 43 6f 6c  up, i+extra+nCol
1b36c 2d 69 64 78 2c 20 31 29 3b 0a 20 20 20 20 20 20  -idx, 1);.      
1b36d 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6a 75 6d 70  }.    }.    jump
1b36e 49 6e 73 74 31 20 3d 20 73 71 6c 69 74 65 33 56  Inst1 = sqlite3V
1b36f 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
1b370 61 6b 65 49 64 78 52 65 63 2c 20 70 49 64 78 2d  akeIdxRec, pIdx-
1b371 3e 6e 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20  >nColumn, 0);.  
1b372 20 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66    sqlite3IndexAf
1b373 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64  finityStr(v, pId
1b374 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64  x);..    /* Find
1b375 20 6f 75 74 20 77 68 61 74 20 61 63 74 69 6f 6e   out what action
1b376 20 74 6f 20 74 61 6b 65 20 69 6e 20 63 61 73 65   to take in case
1b377 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e 64   there is an ind
1b378 65 78 69 6e 67 20 63 6f 6e 66 6c 69 63 74 20 2a  exing conflict *
1b379 2f 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20  /.    onError = 
1b37a 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20  pIdx->onError;. 
1b37b 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d     if( onError==
1b37c 4f 45 5f 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e  OE_None ) contin
1b37d 75 65 3b 20 20 2f 2a 20 70 49 64 78 20 69 73 20  ue;  /* pIdx is 
1b37e 6e 6f 74 20 61 20 55 4e 49 51 55 45 20 69 6e 64  not a UNIQUE ind
1b37f 65 78 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 76  ex */.    if( ov
1b380 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f  errideError!=OE_
1b381 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20  Default ){.     
1b382 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72   onError = overr
1b383 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65  ideError;.    }e
1b384 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d  lse if( onError=
1b385 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
1b386 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f       onError = O
1b387 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
1b388 20 20 20 69 66 28 20 73 65 65 6e 52 65 70 6c 61     if( seenRepla
1b389 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
1b38a 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f  onError==OE_Igno
1b38b 72 65 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f  re ) onError = O
1b38c 45 5f 52 65 70 6c 61 63 65 3b 0a 20 20 20 20 20  E_Replace;.     
1b38d 20 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f   else if( onErro
1b38e 72 3d 3d 4f 45 5f 46 61 69 6c 20 29 20 6f 6e 45  r==OE_Fail ) onE
1b38f 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
1b390 0a 20 20 20 20 7d 0a 20 20 20 20 0a 0a 20 20 20  .    }.    ..   
1b391 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   /* Check to see
1b392 20 69 66 20 74 68 65 20 6e 65 77 20 69 6e 64 65   if the new inde
1b393 78 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20  x entry will be 
1b394 75 6e 69 71 75 65 20 2a 2f 0a 20 20 20 20 73 71  unique */.    sq
1b395 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b396 2c 20 4f 50 5f 44 75 70 2c 20 65 78 74 72 61 2b  , OP_Dup, extra+
1b397 6e 43 6f 6c 2b 31 2b 68 61 73 54 77 6f 52 6f 77  nCol+1+hasTwoRow
1b398 69 64 73 2c 20 31 29 3b 0a 20 20 20 20 6a 75 6d  ids, 1);.    jum
1b399 70 49 6e 73 74 32 20 3d 20 73 71 6c 69 74 65 33  pInst2 = sqlite3
1b39a 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b39b 49 73 55 6e 69 71 75 65 2c 20 62 61 73 65 2b 69  IsUnique, base+i
1b39c 43 75 72 2b 31 2c 20 30 29 3b 0a 0a 20 20 20 20  Cur+1, 0);..    
1b39d 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
1b39e 20 74 68 61 74 20 65 78 65 63 75 74 65 73 20 69   that executes i
1b39f 66 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20  f the new index 
1b3a0 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 75 6e 69  entry is not uni
1b3a1 71 75 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  que */.    asser
1b3a2 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  t( onError==OE_R
1b3a3 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72  ollback || onErr
1b3a4 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20  or==OE_Abort || 
1b3a5 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c  onError==OE_Fail
1b3a6 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72  .        || onEr
1b3a7 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c  ror==OE_Ignore |
1b3a8 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  | onError==OE_Re
1b3a9 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73 77 69  place );.    swi
1b3aa 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a  tch( onError ){.
1b3ab 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f        case OE_Ro
1b3ac 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61  llback:.      ca
1b3ad 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20  se OE_Abort:.   
1b3ae 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a     case OE_Fail:
1b3af 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a   {.        int j
1b3b0 2c 20 6e 31 2c 20 6e 32 3b 0a 20 20 20 20 20 20  , n1, n2;.      
1b3b1 20 20 63 68 61 72 20 7a 45 72 72 4d 73 67 5b 32    char zErrMsg[2
1b3b2 30 30 5d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  00];.        sql
1b3b3 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1b3b4 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29 2c 20 7a  zeof(zErrMsg), z
1b3b5 45 72 72 4d 73 67 2c 0a 20 20 20 20 20 20 20 20  ErrMsg,.        
1b3b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b3b7 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31   pIdx->nColumn>1
1b3b8 20 3f 20 22 63 6f 6c 75 6d 6e 73 20 22 20 3a 20   ? "columns " : 
1b3b9 22 63 6f 6c 75 6d 6e 20 22 29 3b 0a 20 20 20 20  "column ");.    
1b3ba 20 20 20 20 6e 31 20 3d 20 73 74 72 6c 65 6e 28      n1 = strlen(
1b3bb 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
1b3bc 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64    for(j=0; j<pId
1b3bd 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 20 6e 31  x->nColumn && n1
1b3be 3c 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29  <sizeof(zErrMsg)
1b3bf 2d 33 30 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  -30; j++){.     
1b3c0 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20       char *zCol 
1b3c1 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64  = pTab->aCol[pId
1b3c2 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e  x->aiColumn[j]].
1b3c3 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
1b3c4 20 6e 32 20 3d 20 73 74 72 6c 65 6e 28 7a 43 6f   n2 = strlen(zCo
1b3c5 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  l);.          if
1b3c6 28 20 6a 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ( j>0 ){.       
1b3c7 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1b3c8 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
1b3c9 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d  rMsg)-n1, &zErrM
1b3ca 73 67 5b 6e 31 5d 2c 20 22 2c 20 22 29 3b 0a 20  sg[n1], ", ");. 
1b3cb 20 20 20 20 20 20 20 20 20 20 20 6e 31 20 2b 3d             n1 +=
1b3cc 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   2;.          }.
1b3cd 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 31            if( n1
1b3ce 2b 6e 32 3e 73 69 7a 65 6f 66 28 7a 45 72 72 4d  +n2>sizeof(zErrM
1b3cf 73 67 29 2d 33 30 20 29 7b 0a 20 20 20 20 20 20  sg)-30 ){.      
1b3d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1b3d1 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
1b3d2 72 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72  rrMsg)-n1, &zErr
1b3d3 4d 73 67 5b 6e 31 5d 2c 20 22 2e 2e 2e 22 29 3b  Msg[n1], "...");
1b3d4 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 31 20  .            n1 
1b3d5 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20  += 3;.          
1b3d6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1b3d7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1b3d8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1b3d9 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
1b3da 72 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72  rrMsg)-n1, &zErr
1b3db 4d 73 67 5b 6e 31 5d 2c 20 22 25 73 22 2c 20 7a  Msg[n1], "%s", z
1b3dc 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Col);.          
1b3dd 20 20 6e 31 20 2b 3d 20 6e 32 3b 0a 20 20 20 20    n1 += n2;.    
1b3de 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1b3df 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
1b3e0 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1b3e1 66 28 7a 45 72 72 4d 73 67 29 2d 6e 31 2c 20 26  f(zErrMsg)-n1, &
1b3e2 7a 45 72 72 4d 73 67 5b 6e 31 5d 2c 20 0a 20 20  zErrMsg[n1], .  
1b3e3 20 20 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e            pIdx->
1b3e4 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 20 61 72  nColumn>1 ? " ar
1b3e5 65 20 6e 6f 74 20 75 6e 69 71 75 65 22 20 3a 20  e not unique" : 
1b3e6 22 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 22  " is not unique"
1b3e7 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1b3e8 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f  e3VdbeOp3(v, OP_
1b3e9 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e  Halt, SQLITE_CON
1b3ea 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72  STRAINT, onError
1b3eb 2c 20 7a 45 72 72 4d 73 67 2c 20 30 29 3b 0a 20  , zErrMsg, 0);. 
1b3ec 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1b3ed 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
1b3ee 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20   OE_Ignore: {.  
1b3ef 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65        assert( se
1b3f0 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a  enReplace==0 );.
1b3f1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1b3f2 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50  dbeAddOp(v, OP_P
1b3f3 6f 70 2c 20 6e 43 6f 6c 2b 65 78 74 72 61 2b 33  op, nCol+extra+3
1b3f4 2b 68 61 73 54 77 6f 52 6f 77 69 64 73 2c 20 30  +hasTwoRowids, 0
1b3f5 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1b3f6 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1b3f7 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72  P_Goto, 0, ignor
1b3f8 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20  eDest);.        
1b3f9 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1b3fa 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65 70       case OE_Rep
1b3fb 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 20 20 20  lace: {.        
1b3fc 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
1b3fd 6f 77 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  owDelete(pParse-
1b3fe 3e 64 62 2c 20 76 2c 20 70 54 61 62 2c 20 62 61  >db, v, pTab, ba
1b3ff 73 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  se, 0);.        
1b400 69 66 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a  if( isUpdate ){.
1b401 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1b402 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b403 5f 44 75 70 2c 20 6e 43 6f 6c 2b 65 78 74 72 61  _Dup, nCol+extra
1b404 2b 31 2b 68 61 73 54 77 6f 52 6f 77 69 64 73 2c  +1+hasTwoRowids,
1b405 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   1);.          s
1b406 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1b407 76 2c 20 4f 50 5f 4d 6f 76 65 47 65 2c 20 62 61  v, OP_MoveGe, ba
1b408 73 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  se, 0);.        
1b409 7d 0a 20 20 20 20 20 20 20 20 73 65 65 6e 52 65  }.        seenRe
1b40a 70 6c 61 63 65 20 3d 20 31 3b 0a 20 20 20 20 20  place = 1;.     
1b40b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1b40c 7d 0a 20 20 20 20 7d 0a 23 69 66 20 4e 55 4c 4c  }.    }.#if NULL
1b40d 5f 44 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e  _DISTINCT_FOR_UN
1b40e 49 51 55 45 0a 20 20 20 20 73 71 6c 69 74 65 33  IQUE.    sqlite3
1b40f 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
1b410 6a 75 6d 70 49 6e 73 74 31 29 3b 0a 23 65 6e 64  jumpInst1);.#end
1b411 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  if.    sqlite3Vd
1b412 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 75  beJumpHere(v, ju
1b413 6d 70 49 6e 73 74 32 29 3b 0a 20 20 7d 0a 7d 0a  mpInst2);.  }.}.
1b414 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1b415 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
1b416 64 65 20 74 6f 20 66 69 6e 69 73 68 20 74 68 65  de to finish the
1b417 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54   INSERT or UPDAT
1b418 45 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 74  E operation.** t
1b419 68 61 74 20 77 61 73 20 73 74 61 72 74 65 64 20  hat was started 
1b41a 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20  by a prior call 
1b41b 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  to sqlite3Genera
1b41c 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
1b41d 6b 73 2e 0a 2a 2a 20 54 68 65 20 73 74 61 63 6b  ks..** The stack
1b41e 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 6b 65   must contain ke
1b41f 79 73 20 66 6f 72 20 61 6c 6c 20 61 63 74 69 76  ys for all activ
1b420 65 20 69 6e 64 69 63 65 73 20 66 6f 6c 6c 6f 77  e indices follow
1b421 65 64 20 62 79 20 64 61 74 61 0a 2a 2a 20 61 6e  ed by data.** an
1b422 64 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20  d the rowid for 
1b423 74 68 65 20 6e 65 77 20 65 6e 74 72 79 2e 20 20  the new entry.  
1b424 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 72 65  This routine cre
1b425 61 74 65 73 20 74 68 65 20 6e 65 77 0a 2a 2a 20  ates the new.** 
1b426 65 6e 74 72 69 65 73 20 69 6e 20 61 6c 6c 20 69  entries in all i
1b427 6e 64 69 63 65 73 20 61 6e 64 20 69 6e 20 74 68  ndices and in th
1b428 65 20 6d 61 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a  e main table..**
1b429 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74  .** The argument
1b42a 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  s to this routin
1b42b 65 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20  e should be the 
1b42c 73 61 6d 65 20 61 73 20 74 68 65 20 66 69 72 73  same as the firs
1b42d 74 20 73 69 78 0a 2a 2a 20 61 72 67 75 6d 65 6e  t six.** argumen
1b42e 74 73 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e  ts to sqlite3Gen
1b42f 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
1b430 68 65 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  hecks..*/.SQLITE
1b431 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1b432 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73  lite3CompleteIns
1b433 65 72 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20  ertion(.  Parse 
1b434 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
1b435 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
1b436 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
1b437 70 54 61 62 2c 20 20 20 20 20 20 20 20 2f 2a 20  pTab,        /* 
1b438 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  the table into w
1b439 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65  hich we are inse
1b43a 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62  rting */.  int b
1b43b 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ase,           /
1b43c 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 72 65 61  * Index of a rea
1b43d 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70  d/write cursor p
1b43e 6f 69 6e 74 69 6e 67 20 61 74 20 70 54 61 62 20  ointing at pTab 
1b43f 2a 2f 0a 20 20 63 68 61 72 20 2a 61 49 64 78 55  */.  char *aIdxU
1b440 73 65 64 2c 20 20 20 20 20 2f 2a 20 57 68 69 63  sed,     /* Whic
1b441 68 20 69 6e 64 69 63 65 73 20 61 72 65 20 75 73  h indices are us
1b442 65 64 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20  ed.  NULL means 
1b443 61 6c 6c 20 61 72 65 20 75 73 65 64 20 2a 2f 0a  all are used */.
1b444 20 20 69 6e 74 20 72 6f 77 69 64 43 68 6e 67 2c    int rowidChng,
1b445 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1b446 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
1b447 65 72 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 2a  er will change *
1b448 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74 65  /.  int isUpdate
1b449 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ,       /* True 
1b44a 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c 73  for UPDATE, Fals
1b44b 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a  e for INSERT */.
1b44c 20 20 69 6e 74 20 6e 65 77 49 64 78 2c 20 20 20    int newIdx,   
1b44d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1b44e 66 20 4e 45 57 20 74 61 62 6c 65 20 66 6f 72 20  f NEW table for 
1b44f 74 72 69 67 67 65 72 73 2e 20 20 2d 31 20 69 66  triggers.  -1 if
1b450 20 6e 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61   none */.  int a
1b451 70 70 65 6e 64 42 69 61 73 20 20 20 20 20 20 2f  ppendBias      /
1b452 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
1b453 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61  s likely to be a
1b454 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 29 7b 0a 20  n append */.){. 
1b455 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a   int i;.  Vdbe *
1b456 76 3b 0a 20 20 69 6e 74 20 6e 49 64 78 3b 0a 20  v;.  int nIdx;. 
1b457 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
1b458 69 6e 74 20 70 69 6b 5f 66 6c 61 67 73 3b 0a 0a  int pik_flags;..
1b459 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
1b45a 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1b45b 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a  assert( v!=0 );.
1b45c 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
1b45d 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f  pSelect==0 );  /
1b45e 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69 73 20  * This table is 
1b45f 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a 20 20  not a VIEW */.  
1b460 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78  for(nIdx=0, pIdx
1b461 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
1b462 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
1b463 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d  pNext, nIdx++){}
1b464 0a 20 20 66 6f 72 28 69 3d 6e 49 64 78 2d 31 3b  .  for(i=nIdx-1;
1b465 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20   i>=0; i--){.   
1b466 20 69 66 28 20 61 49 64 78 55 73 65 64 20 26 26   if( aIdxUsed &&
1b467 20 61 49 64 78 55 73 65 64 5b 69 5d 3d 3d 30 20   aIdxUsed[i]==0 
1b468 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
1b469 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b46a 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
1b46b 2c 20 62 61 73 65 2b 69 2b 31 2c 20 30 29 3b 0a  , base+i+1, 0);.
1b46c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
1b46d 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b  eAddOp(v, OP_Mak
1b46e 65 52 65 63 6f 72 64 2c 20 70 54 61 62 2d 3e 6e  eRecord, pTab->n
1b46f 43 6f 6c 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  Col, 0);.  sqlit
1b470 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53  e3TableAffinityS
1b471 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 23 69 66  tr(v, pTab);.#if
1b472 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b473 5f 54 52 49 47 47 45 52 0a 20 20 69 66 28 20 6e  _TRIGGER.  if( n
1b474 65 77 49 64 78 3e 3d 30 20 29 7b 0a 20 20 20 20  ewIdx>=0 ){.    
1b475 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b476 28 76 2c 20 4f 50 5f 44 75 70 2c 20 31 2c 20 30  (v, OP_Dup, 1, 0
1b477 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1b478 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75  beAddOp(v, OP_Du
1b479 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 73 71  p, 1, 0);.    sq
1b47a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b47b 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77  , OP_Insert, new
1b47c 49 64 78 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e  Idx, 0);.  }.#en
1b47d 64 69 66 0a 20 20 69 66 28 20 70 50 61 72 73 65  dif.  if( pParse
1b47e 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20  ->nested ){.    
1b47f 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a 20  pik_flags = 0;. 
1b480 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b 5f   }else{.    pik_
1b481 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f 4e  flags = OPFLAG_N
1b482 43 48 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b 5f  CHANGE;.    pik_
1b483 66 6c 61 67 73 20 7c 3d 20 28 69 73 55 70 64 61  flags |= (isUpda
1b484 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44 41  te?OPFLAG_ISUPDA
1b485 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  TE:OPFLAG_LASTRO
1b486 57 49 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  WID);.  }.  if( 
1b487 61 70 70 65 6e 64 42 69 61 73 20 29 7b 0a 20 20  appendBias ){.  
1b488 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f    pik_flags |= O
1b489 50 46 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20 20  PFLAG_APPEND;.  
1b48a 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
1b48b 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 73 65 72  ddOp(v, OP_Inser
1b48c 74 2c 20 62 61 73 65 2c 20 70 69 6b 5f 66 6c 61  t, base, pik_fla
1b48d 67 73 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72  gs);.  if( !pPar
1b48e 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20  se->nested ){.  
1b48f 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1b490 6e 67 65 50 33 28 76 2c 20 2d 31 2c 20 70 54 61  ngeP3(v, -1, pTa
1b491 62 2d 3e 7a 4e 61 6d 65 2c 20 50 33 5f 53 54 41  b->zName, P3_STA
1b492 54 49 43 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 69  TIC);.  }.  .  i
1b493 66 28 20 69 73 55 70 64 61 74 65 20 26 26 20 72  f( isUpdate && r
1b494 6f 77 69 64 43 68 6e 67 20 29 7b 0a 20 20 20 20  owidChng ){.    
1b495 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b496 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20 30  (v, OP_Pop, 1, 0
1b497 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1b498 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
1b499 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20 63 75 72  at will open cur
1b49a 73 6f 72 73 20 66 6f 72 20 61 20 74 61 62 6c 65  sors for a table
1b49b 20 61 6e 64 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20   and for all.** 
1b49c 69 6e 64 69 63 65 73 20 6f 66 20 74 68 61 74 20  indices of that 
1b49d 74 61 62 6c 65 2e 20 20 54 68 65 20 22 62 61 73  table.  The "bas
1b49e 65 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  e" parameter is 
1b49f 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  the cursor numbe
1b4a0 72 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 74 68  r used.** for th
1b4a1 65 20 74 61 62 6c 65 2e 20 20 49 6e 64 69 63 65  e table.  Indice
1b4a2 73 20 61 72 65 20 6f 70 65 6e 65 64 20 6f 6e 20  s are opened on 
1b4a3 73 75 62 73 65 71 75 65 6e 74 20 63 75 72 73 6f  subsequent curso
1b4a4 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rs..*/.SQLITE_PR
1b4a5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1b4a6 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e  e3OpenTableAndIn
1b4a7 64 69 63 65 73 28 0a 20 20 50 61 72 73 65 20 2a  dices(.  Parse *
1b4a8 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72  pParse,   /* Par
1b4a9 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1b4aa 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
1b4ab 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62     /* Table to b
1b4ac 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e  e opened */.  in
1b4ad 74 20 62 61 73 65 2c 20 20 20 20 20 20 20 20 2f  t base,        /
1b4ae 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
1b4af 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 65 20  assigned to the 
1b4b0 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  table */.  int o
1b4b1 70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  p           /* O
1b4b2 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50  P_OpenRead or OP
1b4b3 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a 29 7b  _OpenWrite */.){
1b4b4 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1b4b5 69 44 62 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49  iDb;.  Index *pI
1b4b6 64 78 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a  dx;.  Vdbe *v;..
1b4b7 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
1b4b8 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 3b 0a  pTab) ) return;.
1b4b9 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
1b4ba 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
1b4bb 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
1b4bc 53 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73  Schema);.  v = s
1b4bd 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1b4be 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28  arse);.  assert(
1b4bf 20 76 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74   v!=0 );.  sqlit
1b4c0 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
1b4c1 73 65 2c 20 62 61 73 65 2c 20 69 44 62 2c 20 70  se, base, iDb, p
1b4c2 54 61 62 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28  Tab, op);.  for(
1b4c3 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=1, pIdx=pTab->
1b4c4 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
1b4c5 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
1b4c6 69 2b 2b 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66  i++){.    KeyInf
1b4c7 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  o *pKey = sqlite
1b4c8 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
1b4c9 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20  arse, pIdx);.   
1b4ca 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70   assert( pIdx->p
1b4cb 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
1b4cc 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c  chema );.    sql
1b4cd 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b4ce 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 44 62   OP_Integer, iDb
1b4cf 2c 20 30 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  , 0);.    VdbeCo
1b4d0 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 25 73 22  mment((v, "# %s"
1b4d1 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  , pIdx->zName));
1b4d2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b4d3 4f 70 33 28 76 2c 20 6f 70 2c 20 69 2b 62 61 73  Op3(v, op, i+bas
1b4d4 65 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 28  e, pIdx->tnum, (
1b4d5 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 33 5f 4b  char*)pKey, P3_K
1b4d6 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
1b4d7 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
1b4d8 65 2d 3e 6e 54 61 62 3c 3d 62 61 73 65 2b 69 20  e->nTab<=base+i 
1b4d9 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ){.    pParse->n
1b4da 54 61 62 20 3d 20 62 61 73 65 2b 69 3b 0a 20 20  Tab = base+i;.  
1b4db 7d 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  }.}...#ifdef SQL
1b4dc 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54  ITE_TEST./*.** T
1b4dd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f  he following glo
1b4de 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
1b4df 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e  incremented when
1b4e0 65 76 65 72 20 74 68 65 0a 2a 2a 20 74 72 61 6e  ever the.** tran
1b4e1 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
1b4e2 6e 20 69 73 20 75 73 65 64 2e 20 20 54 68 69 73  n is used.  This
1b4e3 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
1b4e4 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73  ting.** purposes
1b4e5 20 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20   only - to make 
1b4e6 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 66 65  sure the transfe
1b4e7 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 72  r optimization r
1b4e8 65 61 6c 6c 79 0a 2a 2a 20 69 73 20 68 61 70 70  eally.** is happ
1b4e9 65 6e 69 6e 67 20 77 68 65 6e 20 69 74 20 69 73  ening when it is
1b4ea 20 73 75 70 70 6f 73 65 20 74 6f 2e 0a 2a 2f 0a   suppose to..*/.
1b4eb 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b4ec 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63  qlite3_xferopt_c
1b4ed 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  ount;.#endif /* 
1b4ee 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a  SQLITE_TEST */..
1b4ef 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b4f0 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a  OMIT_XFER_OPT./*
1b4f1 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 63 6f 6c  .** Check to col
1b4f2 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20 74 6f 20  lation names to 
1b4f3 73 65 65 20 69 66 20 74 68 65 79 20 61 72 65 20  see if they are 
1b4f4 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73  compatible..*/.s
1b4f5 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f  tatic int xferCo
1b4f6 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f  mpatibleCollatio
1b4f7 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31  n(const char *z1
1b4f8 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32  , const char *z2
1b4f9 29 7b 0a 20 20 69 66 28 20 7a 31 3d 3d 30 20 29  ){.  if( z1==0 )
1b4fa 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 32 3d  {.    return z2=
1b4fb 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 32  =0;.  }.  if( z2
1b4fc 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1b4fd 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
1b4fe 6e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  n sqlite3StrICmp
1b4ff 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a  (z1, z2)==0;.}..
1b500 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
1b501 73 65 65 20 69 66 20 69 6e 64 65 78 20 70 53 72  see if index pSr
1b502 63 20 69 73 20 63 6f 6d 70 61 74 69 62 6c 65 20  c is compatible 
1b503 61 73 20 61 20 73 6f 75 72 63 65 20 6f 66 20 64  as a source of d
1b504 61 74 61 0a 2a 2a 20 66 6f 72 20 69 6e 64 65 78  ata.** for index
1b505 20 70 44 65 73 74 20 69 6e 20 61 6e 20 69 6e 73   pDest in an ins
1b506 65 72 74 20 74 72 61 6e 73 66 65 72 20 6f 70 74  ert transfer opt
1b507 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20  imization.  The 
1b508 72 75 6c 65 73 0a 2a 2a 20 66 6f 72 20 61 20 63  rules.** for a c
1b509 6f 6d 70 61 74 69 62 6c 65 20 69 6e 64 65 78 3a  ompatible index:
1b50a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  .**.**    *   Th
1b50b 65 20 69 6e 64 65 78 20 69 73 20 6f 76 65 72 20  e index is over 
1b50c 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20  the same set of 
1b50d 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20  columns.**    * 
1b50e 20 20 54 68 65 20 73 61 6d 65 20 44 45 53 43 20    The same DESC 
1b50f 61 6e 64 20 41 53 43 20 6d 61 72 6b 69 6e 67 73  and ASC markings
1b510 20 6f 63 63 75 72 73 20 6f 6e 20 61 6c 6c 20 63   occurs on all c
1b511 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20  olumns.**    *  
1b512 20 54 68 65 20 73 61 6d 65 20 6f 6e 45 72 72 6f   The same onErro
1b513 72 20 70 72 6f 63 65 73 73 69 6e 67 20 28 4f 45  r processing (OE
1b514 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72  _Abort, OE_Ignor
1b515 65 2c 20 65 74 63 29 0a 2a 2a 20 20 20 20 2a 20  e, etc).**    * 
1b516 20 20 54 68 65 20 73 61 6d 65 20 63 6f 6c 6c 61    The same colla
1b517 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 6e  ting sequence on
1b518 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a   each column.*/.
1b519 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43  static int xferC
1b51a 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 49  ompatibleIndex(I
1b51b 6e 64 65 78 20 2a 70 44 65 73 74 2c 20 49 6e 64  ndex *pDest, Ind
1b51c 65 78 20 2a 70 53 72 63 29 7b 0a 20 20 69 6e 74  ex *pSrc){.  int
1b51d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   i;.  assert( pD
1b51e 65 73 74 20 26 26 20 70 53 72 63 20 29 3b 0a 20  est && pSrc );. 
1b51f 20 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e   assert( pDest->
1b520 70 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 70 54  pTable!=pSrc->pT
1b521 61 62 6c 65 20 29 3b 0a 20 20 69 66 28 20 70 44  able );.  if( pD
1b522 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53  est->nColumn!=pS
1b523 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  rc->nColumn ){. 
1b524 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
1b525 2a 20 44 69 66 66 65 72 65 6e 74 20 6e 75 6d 62  * Different numb
1b526 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f  er of columns */
1b527 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74  .  }.  if( pDest
1b528 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 53 72 63 2d  ->onError!=pSrc-
1b529 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  >onError ){.    
1b52a 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44  return 0;   /* D
1b52b 69 66 66 65 72 65 6e 74 20 63 6f 6e 66 6c 69 63  ifferent conflic
1b52c 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72  t resolution str
1b52d 61 74 65 67 69 65 73 20 2a 2f 0a 20 20 7d 0a 20  ategies */.  }. 
1b52e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63   for(i=0; i<pSrc
1b52f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
1b530 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61  .    if( pSrc->a
1b531 69 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70 44 65 73  iColumn[i]!=pDes
1b532 74 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29  t->aiColumn[i] )
1b533 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
1b534 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
1b535 20 63 6f 6c 75 6d 6e 73 20 69 6e 64 65 78 65 64   columns indexed
1b536 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
1b537 28 20 70 53 72 63 2d 3e 61 53 6f 72 74 4f 72 64  ( pSrc->aSortOrd
1b538 65 72 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61 53  er[i]!=pDest->aS
1b539 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20  ortOrder[i] ){. 
1b53a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
1b53b 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 73 6f   /* Different so
1b53c 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a 20 20 20  rt orders */.   
1b53d 20 7d 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d   }.    if( pSrc-
1b53e 3e 61 7a 43 6f 6c 6c 5b 69 5d 21 3d 70 44 65 73  >azColl[i]!=pDes
1b53f 74 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20 29 7b 0a  t->azColl[i] ){.
1b540 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
1b541 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 73    /* Different s
1b542 6f 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a 20 20  ort orders */.  
1b543 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
1b544 20 6e 6f 20 74 65 73 74 20 61 62 6f 76 65 20 66   no test above f
1b545 61 69 6c 73 20 74 68 65 6e 20 74 68 65 20 69 6e  ails then the in
1b546 64 69 63 65 73 20 6d 75 73 74 20 62 65 20 63 6f  dices must be co
1b547 6d 70 61 74 69 62 6c 65 20 2a 2f 0a 20 20 72 65  mpatible */.  re
1b548 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
1b549 20 41 74 74 65 6d 70 74 20 74 68 65 20 74 72 61   Attempt the tra
1b54a 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  nsfer optimizati
1b54b 6f 6e 20 6f 6e 20 49 4e 53 45 52 54 73 20 6f 66  on on INSERTs of
1b54c 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20   the form.**.** 
1b54d 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
1b54e 74 61 62 31 20 53 45 4c 45 43 54 20 2a 20 46 52  tab1 SELECT * FR
1b54f 4f 4d 20 74 61 62 32 3b 0a 2a 2a 0a 2a 2a 20 54  OM tab2;.**.** T
1b550 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
1b551 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74   is only attempt
1b552 65 64 20 69 66 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ed if.**.**    (
1b553 31 29 20 20 74 61 62 31 20 61 6e 64 20 74 61 62  1)  tab1 and tab
1b554 32 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c  2 have identical
1b555 20 73 63 68 65 6d 61 73 20 69 6e 63 6c 75 64 69   schemas includi
1b556 6e 67 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 20 20  ng all the.**   
1b557 20 20 20 20 20 20 73 61 6d 65 20 69 6e 64 69 63        same indic
1b558 65 73 20 61 6e 64 20 63 6f 6e 73 74 72 61 69 6e  es and constrain
1b559 74 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20  ts.**.**    (2) 
1b55a 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 61   tab1 and tab2 a
1b55b 72 65 20 64 69 66 66 65 72 65 6e 74 20 74 61 62  re different tab
1b55c 6c 65 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29  les.**.**    (3)
1b55d 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20    There must be 
1b55e 6e 6f 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  no triggers on t
1b55f 61 62 31 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29  ab1.**.**    (4)
1b560 20 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74    The result set
1b561 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
1b562 74 61 74 65 6d 65 6e 74 20 69 73 20 22 2a 22 0a  tatement is "*".
1b563 2a 2a 0a 2a 2a 20 20 20 20 28 35 29 20 20 54 68  **.**    (5)  Th
1b564 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
1b565 6e 74 20 68 61 73 20 6e 6f 20 57 48 45 52 45 2c  nt has no WHERE,
1b566 20 48 41 56 49 4e 47 2c 20 4f 52 44 45 52 20 42   HAVING, ORDER B
1b567 59 2c 20 47 52 4f 55 50 20 42 59 2c 0a 2a 2a 20  Y, GROUP BY,.** 
1b568 20 20 20 20 20 20 20 20 6f 72 20 4c 49 4d 49 54          or LIMIT
1b569 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
1b56a 20 20 28 36 29 20 20 54 68 65 20 53 45 4c 45 43    (6)  The SELEC
1b56b 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  T statement is a
1b56c 20 73 69 6d 70 6c 65 20 28 6e 6f 74 20 61 20 63   simple (not a c
1b56d 6f 6d 70 6f 75 6e 64 29 20 73 65 6c 65 63 74 20  ompound) select 
1b56e 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  that.**         
1b56f 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 74 61  contains only ta
1b570 62 32 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63  b2 in its FROM c
1b571 6c 61 75 73 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lause.**.** This
1b572 20 6d 65 74 68 6f 64 20 66 6f 72 20 69 6d 70 6c   method for impl
1b573 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 49 4e 53  ementing the INS
1b574 45 52 54 20 74 72 61 6e 73 66 65 72 73 20 72 61  ERT transfers ra
1b575 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a  w records from.*
1b576 2a 20 74 61 62 32 20 6f 76 65 72 20 74 6f 20 74  * tab2 over to t
1b577 61 62 31 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e  ab1.  The column
1b578 73 20 61 72 65 20 6e 6f 74 20 64 65 63 6f 64 65  s are not decode
1b579 64 2e 20 20 52 61 77 20 72 65 63 6f 72 64 73 20  d.  Raw records 
1b57a 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 69  from.** the indi
1b57b 63 65 73 20 6f 66 20 74 61 62 32 20 61 72 65 20  ces of tab2 are 
1b57c 74 72 61 6e 73 66 65 72 65 64 20 74 6f 20 74 61  transfered to ta
1b57d 62 31 20 61 73 20 77 65 6c 6c 2e 20 20 49 6e 20  b1 as well.  In 
1b57e 73 6f 20 64 6f 69 6e 67 2c 0a 2a 2a 20 74 68 65  so doing,.** the
1b57f 20 72 65 73 75 6c 74 69 6e 67 20 74 61 62 31 20   resulting tab1 
1b580 68 61 73 20 6d 75 63 68 20 6c 65 73 73 20 66 72  has much less fr
1b581 61 67 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a  agmentation..**.
1b582 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1b583 72 65 74 75 72 6e 73 20 54 52 55 45 20 69 66 20  returns TRUE if 
1b584 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
1b585 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 20 20   is attempted.  
1b586 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65  If any.** of the
1b587 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76   conditions abov
1b588 65 20 66 61 69 6c 20 73 6f 20 74 68 61 74 20 74  e fail so that t
1b589 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
1b58a 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65  should not.** be
1b58b 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 65 6e   attempted, then
1b58c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
1b58d 74 75 72 6e 73 20 46 41 4c 53 45 2e 0a 2a 2f 0a  turns FALSE..*/.
1b58e 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 4f  static int xferO
1b58f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50  ptimization(.  P
1b590 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1b591 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63       /* Parser c
1b592 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
1b593 65 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 20  e *pDest,       
1b594 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 77    /* The table w
1b595 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
1b596 69 6e 74 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74  into */.  Select
1b597 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
1b598 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 74  /* A SELECT stat
1b599 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 61 73 20  ement to use as 
1b59a 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65 20  the data source 
1b59b 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
1b59c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  ,          /* Ho
1b59d 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73  w to handle cons
1b59e 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f  traint errors */
1b59f 0a 20 20 69 6e 74 20 69 44 62 44 65 73 74 20 20  .  int iDbDest  
1b5a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1b5a1 64 61 74 61 62 61 73 65 20 6f 66 20 70 44 65 73  database of pDes
1b5a2 74 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69  t */.){.  ExprLi
1b5a3 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20  st *pEList;     
1b5a4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1b5a5 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
1b5a6 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  the SELECT */.  
1b5a7 54 61 62 6c 65 20 2a 70 53 72 63 3b 20 20 20 20  Table *pSrc;    
1b5a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b5a9 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69 6e   /* The table in
1b5aa 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
1b5ab 20 6f 66 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20   of SELECT */.  
1b5ac 49 6e 64 65 78 20 2a 70 53 72 63 49 64 78 2c 20  Index *pSrcIdx, 
1b5ad 2a 70 44 65 73 74 49 64 78 3b 20 20 20 20 20 20  *pDestIdx;      
1b5ae 20 2f 2a 20 53 6f 75 72 63 65 20 61 6e 64 20 64   /* Source and d
1b5af 65 73 74 69 6e 61 74 69 6f 6e 20 69 6e 64 69 63  estination indic
1b5b0 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  es */.  struct S
1b5b1 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
1b5b2 65 6d 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 65  em;      /* An e
1b5b3 6c 65 6d 65 6e 74 20 6f 66 20 70 53 65 6c 65 63  lement of pSelec
1b5b4 74 2d 3e 70 53 72 63 20 2a 2f 0a 20 20 69 6e 74  t->pSrc */.  int
1b5b5 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1b5b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b5b7 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
1b5b8 0a 20 20 69 6e 74 20 69 44 62 53 72 63 3b 20 20  .  int iDbSrc;  
1b5b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b5ba 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1b5bb 61 73 65 20 6f 66 20 70 53 72 63 20 2a 2f 0a 20  ase of pSrc */. 
1b5bc 20 69 6e 74 20 69 53 72 63 2c 20 69 44 65 73 74   int iSrc, iDest
1b5bd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1b5be 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 66 72 6f    /* Cursors fro
1b5bf 6d 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73  m source and des
1b5c0 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  tination */.  in
1b5c1 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 20  t addr1, addr2; 
1b5c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b5c3 2a 20 4c 6f 6f 70 20 61 64 64 72 65 73 73 65 73  * Loop addresses
1b5c4 20 2a 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79 44   */.  int emptyD
1b5c5 65 73 74 54 65 73 74 3b 20 20 20 20 20 20 20 20  estTest;        
1b5c6 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
1b5c7 73 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65 6d  s of test for em
1b5c8 70 74 79 20 70 44 65 73 74 20 2a 2f 0a 20 20 69  pty pDest */.  i
1b5c9 6e 74 20 65 6d 70 74 79 53 72 63 54 65 73 74 3b  nt emptySrcTest;
1b5ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b5cb 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 65  /* Address of te
1b5cc 73 74 20 66 6f 72 20 65 6d 70 74 79 20 70 53 72  st for empty pSr
1b5cd 63 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  c */.  Vdbe *v; 
1b5ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b5cf 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
1b5d0 44 42 45 20 77 65 20 61 72 65 20 62 75 69 6c 64  DBE we are build
1b5d1 69 6e 67 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f  ing */.  KeyInfo
1b5d2 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 20   *pKey;         
1b5d3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79            /* Key
1b5d4 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
1b5d5 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69   an index */.  i
1b5d6 6e 74 20 63 6f 75 6e 74 65 72 4d 65 6d 3b 20 20  nt counterMem;  
1b5d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b5d8 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74  /* Memory regist
1b5d9 65 72 20 75 73 65 64 20 62 79 20 41 55 54 4f 49  er used by AUTOI
1b5da 4e 43 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74  NC */.  int dest
1b5db 48 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20 30  HasUniqueIdx = 0
1b5dc 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
1b5dd 20 69 66 20 70 44 65 73 74 20 68 61 73 20 61 20   if pDest has a 
1b5de 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a  UNIQUE index */.
1b5df 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 3d 3d  .  if( pSelect==
1b5e0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1b5e1 30 3b 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20  0;   /* Must be 
1b5e2 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 49 4e 53  of the form  INS
1b5e3 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c  ERT INTO ... SEL
1b5e4 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20  ECT ... */.  }. 
1b5e5 20 69 66 28 20 70 44 65 73 74 2d 3e 70 54 72 69   if( pDest->pTri
1b5e6 67 67 65 72 20 29 7b 0a 20 20 20 20 72 65 74 75  gger ){.    retu
1b5e7 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20  rn 0;   /* tab1 
1b5e8 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 74 72  must not have tr
1b5e9 69 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 23 69  iggers */.  }.#i
1b5ea 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1b5eb 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1b5ec 20 69 66 28 20 70 44 65 73 74 2d 3e 69 73 56 69   if( pDest->isVi
1b5ed 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74  rtual ){.    ret
1b5ee 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31  urn 0;   /* tab1
1b5ef 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76   must not be a v
1b5f0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
1b5f1 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
1b5f2 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66   onError==OE_Def
1b5f3 61 75 6c 74 20 29 7b 0a 20 20 20 20 6f 6e 45 72  ault ){.    onEr
1b5f4 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ror = OE_Abort;.
1b5f5 20 20 7d 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f    }.  if( onErro
1b5f6 72 21 3d 4f 45 5f 41 62 6f 72 74 20 26 26 20 6f  r!=OE_Abort && o
1b5f7 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62  nError!=OE_Rollb
1b5f8 61 63 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ack ){.    retur
1b5f9 6e 20 30 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74  n 0;   /* Cannot
1b5fa 20 64 6f 20 4f 52 20 52 45 50 4c 41 43 45 20 6f   do OR REPLACE o
1b5fb 72 20 4f 52 20 49 47 4e 4f 52 45 20 6f 72 20 4f  r OR IGNORE or O
1b5fc 52 20 46 41 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20  R FAIL */.  }.  
1b5fd 61 73 73 65 72 74 28 70 53 65 6c 65 63 74 2d 3e  assert(pSelect->
1b5fe 70 53 72 63 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f  pSrc);   /* allo
1b5ff 63 61 74 65 64 20 65 76 65 6e 20 69 66 20 74 68  cated even if th
1b600 65 72 65 20 69 73 20 6e 6f 20 46 52 4f 4d 20 63  ere is no FROM c
1b601 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70  lause */.  if( p
1b602 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 6e 53  Select->pSrc->nS
1b603 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 72 65 74  rc!=1 ){.    ret
1b604 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d  urn 0;   /* FROM
1b605 20 63 6c 61 75 73 65 20 6d 75 73 74 20 68 61 76   clause must hav
1b606 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 74 65  e exactly one te
1b607 72 6d 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  rm */.  }.  if( 
1b608 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 61  pSelect->pSrc->a
1b609 5b 30 5d 2e 70 53 65 6c 65 63 74 20 29 7b 0a 20  [0].pSelect ){. 
1b60a 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
1b60b 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 63 61  * FROM clause ca
1b60c 6e 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73  nnot contain a s
1b60d 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20  ubquery */.  }. 
1b60e 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 57   if( pSelect->pW
1b60f 68 65 72 65 20 29 7b 0a 20 20 20 20 72 65 74 75  here ){.    retu
1b610 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
1b611 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61  T may not have a
1b612 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
1b613 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65  .  }.  if( pSele
1b614 63 74 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  ct->pOrderBy ){.
1b615 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
1b616 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f  /* SELECT may no
1b617 74 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20  t have an ORDER 
1b618 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d  BY clause */.  }
1b619 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65 65  .  /* Do not nee
1b61a 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 61 20  d to test for a 
1b61b 48 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20 20  HAVING clause.  
1b61c 49 66 20 48 41 56 49 4e 47 20 69 73 20 70 72 65  If HAVING is pre
1b61d 73 65 6e 74 20 62 75 74 0a 20 20 2a 2a 20 74 68  sent but.  ** th
1b61e 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20  ere is no ORDER 
1b61f 42 59 2c 20 77 65 20 77 69 6c 6c 20 67 65 74 20  BY, we will get 
1b620 61 6e 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69  an error. */.  i
1b621 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 47 72 6f  f( pSelect->pGro
1b622 75 70 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75  upBy ){.    retu
1b623 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
1b624 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61  T may not have a
1b625 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1b626 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53   */.  }.  if( pS
1b627 65 6c 65 63 74 2d 3e 70 4c 69 6d 69 74 20 29 7b  elect->pLimit ){
1b628 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
1b629 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e   /* SELECT may n
1b62a 6f 74 20 68 61 76 65 20 61 20 4c 49 4d 49 54 20  ot have a LIMIT 
1b62b 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20  clause */.  }.  
1b62c 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d  assert( pSelect-
1b62d 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20  >pOffset==0 );  
1b62e 2f 2a 20 4d 75 73 74 20 62 65 20 73 6f 20 69 66  /* Must be so if
1b62f 20 70 4c 69 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20   pLimit==0 */.  
1b630 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72  if( pSelect->pPr
1b631 69 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ior ){.    retur
1b632 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54  n 0;   /* SELECT
1b633 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 63 6f   may not be a co
1b634 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 2a 2f 0a  mpound query */.
1b635 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63    }.  if( pSelec
1b636 74 2d 3e 69 73 44 69 73 74 69 6e 63 74 20 29 7b  t->isDistinct ){
1b637 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
1b638 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e   /* SELECT may n
1b639 6f 74 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a  ot be DISTINCT *
1b63a 2f 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74 20 3d  /.  }.  pEList =
1b63b 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
1b63c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69  ;.  assert( pELi
1b63d 73 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  st!=0 );.  if( p
1b63e 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20  EList->nExpr!=1 
1b63f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1b640 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74     /* The result
1b641 20 73 65 74 20 6d 75 73 74 20 68 61 76 65 20 65   set must have e
1b642 78 61 63 74 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d  xactly one colum
1b643 6e 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72  n */.  }.  asser
1b644 74 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  t( pEList->a[0].
1b645 70 45 78 70 72 20 29 3b 0a 20 20 69 66 28 20 70  pExpr );.  if( p
1b646 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
1b647 72 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b  r->op!=TK_ALL ){
1b648 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
1b649 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73   /* The result s
1b64a 65 74 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  et must be the s
1b64b 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f 72 20  pecial operator 
1b64c 22 2a 22 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a  "*" */.  }..  /*
1b64d 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 77   At this point w
1b64e 65 20 68 61 76 65 20 65 73 74 61 62 6c 69 73 68  e have establish
1b64f 65 64 20 74 68 61 74 20 74 68 65 20 73 74 61 74  ed that the stat
1b650 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65 0a  ement is of the.
1b651 20 20 2a 2a 20 63 6f 72 72 65 63 74 20 73 79 6e    ** correct syn
1b652 74 61 63 74 69 63 20 66 6f 72 6d 20 74 6f 20 70  tactic form to p
1b653 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68  articipate in th
1b654 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  is optimization.
1b655 20 20 4e 6f 77 0a 20 20 2a 2a 20 77 65 20 68 61    Now.  ** we ha
1b656 76 65 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20  ve to check the 
1b657 73 65 6d 61 6e 74 69 63 73 2e 0a 20 20 2a 2f 0a  semantics..  */.
1b658 20 20 70 49 74 65 6d 20 3d 20 70 53 65 6c 65 63    pItem = pSelec
1b659 74 2d 3e 70 53 72 63 2d 3e 61 3b 0a 20 20 70 53  t->pSrc->a;.  pS
1b65a 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  rc = sqlite3Loca
1b65b 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
1b65c 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49  pItem->zName, pI
1b65d 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  tem->zDatabase);
1b65e 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29  .  if( pSrc==0 )
1b65f 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
1b660 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
1b661 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69   does not contai
1b662 6e 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 2a  n a real table *
1b663 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 72 63  /.  }.  if( pSrc
1b664 3d 3d 70 44 65 73 74 20 29 7b 0a 20 20 20 20 72  ==pDest ){.    r
1b665 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61  eturn 0;   /* ta
1b666 62 31 20 61 6e 64 20 74 61 62 32 20 6d 61 79 20  b1 and tab2 may 
1b667 6e 6f 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  not be the same 
1b668 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 69 66  table */.  }.#if
1b669 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b66a 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1b66b 69 66 28 20 70 53 72 63 2d 3e 69 73 56 69 72 74  if( pSrc->isVirt
1b66c 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ual ){.    retur
1b66d 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d  n 0;   /* tab2 m
1b66e 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72  ust not be a vir
1b66f 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
1b670 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  }.#endif.  if( p
1b671 53 72 63 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Src->pSelect ){.
1b672 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
1b673 2f 2a 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20  /* tab2 may not 
1b674 62 65 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 7d  be a view */.  }
1b675 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6e 43  .  if( pDest->nC
1b676 6f 6c 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 20 29  ol!=pSrc->nCol )
1b677 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
1b678 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
1b679 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62 65 20 74  olumns must be t
1b67a 68 65 20 73 61 6d 65 20 69 6e 20 74 61 62 31 20  he same in tab1 
1b67b 61 6e 64 20 74 61 62 32 20 2a 2f 0a 20 20 7d 0a  and tab2 */.  }.
1b67c 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50 4b    if( pDest->iPK
1b67d 65 79 21 3d 70 53 72 63 2d 3e 69 50 4b 65 79 20  ey!=pSrc->iPKey 
1b67e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1b67f 20 20 20 2f 2a 20 42 6f 74 68 20 74 61 62 6c 65     /* Both table
1b680 73 20 6d 75 73 74 20 68 61 76 65 20 74 68 65 20  s must have the 
1b681 73 61 6d 65 20 49 4e 54 45 47 45 52 20 50 52 49  same INTEGER PRI
1b682 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 0a  MARY KEY */.  }.
1b683 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 44 65    for(i=0; i<pDe
1b684 73 74 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  st->nCol; i++){.
1b685 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 61      if( pDest->a
1b686 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 21  Col[i].affinity!
1b687 3d 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 61  =pSrc->aCol[i].a
1b688 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20  ffinity ){.     
1b689 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a   return 0;    /*
1b68a 20 41 66 66 69 6e 69 74 79 20 6d 75 73 74 20 62   Affinity must b
1b68b 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61 6c  e the same on al
1b68c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 20  l columns */.   
1b68d 20 7d 0a 20 20 20 20 69 66 28 20 21 78 66 65 72   }.    if( !xfer
1b68e 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74  CompatibleCollat
1b68f 69 6f 6e 28 70 44 65 73 74 2d 3e 61 43 6f 6c 5b  ion(pDest->aCol[
1b690 69 5d 2e 7a 43 6f 6c 6c 2c 20 70 53 72 63 2d 3e  i].zColl, pSrc->
1b691 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 29 20 29  aCol[i].zColl) )
1b692 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
1b693 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e  ;    /* Collatin
1b694 67 20 73 65 71 75 65 6e 63 65 20 6d 75 73 74 20  g sequence must 
1b695 62 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61  be the same on a
1b696 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  ll columns */.  
1b697 20 20 7d 0a 20 20 20 20 69 66 28 20 70 44 65 73    }.    if( pDes
1b698 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75  t->aCol[i].notNu
1b699 6c 6c 20 26 26 20 21 70 53 72 63 2d 3e 61 43 6f  ll && !pSrc->aCo
1b69a 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a  l[i].notNull ){.
1b69b 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
1b69c 20 20 20 2f 2a 20 74 61 62 32 20 6d 75 73 74 20     /* tab2 must 
1b69d 62 65 20 4e 4f 54 20 4e 55 4c 4c 20 69 66 20 74  be NOT NULL if t
1b69e 61 62 31 20 69 73 20 2a 2f 0a 20 20 20 20 7d 0a  ab1 is */.    }.
1b69f 20 20 7d 0a 20 20 66 6f 72 28 70 44 65 73 74 49    }.  for(pDestI
1b6a0 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65 78  dx=pDest->pIndex
1b6a1 3b 20 70 44 65 73 74 49 64 78 3b 20 70 44 65 73  ; pDestIdx; pDes
1b6a2 74 49 64 78 3d 70 44 65 73 74 49 64 78 2d 3e 70  tIdx=pDestIdx->p
1b6a3 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
1b6a4 44 65 73 74 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  DestIdx->onError
1b6a5 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20  !=OE_None ){.   
1b6a6 20 20 20 64 65 73 74 48 61 73 55 6e 69 71 75 65     destHasUnique
1b6a7 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  Idx = 1;.    }. 
1b6a8 20 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d 70     for(pSrcIdx=p
1b6a9 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 70 53 72  Src->pIndex; pSr
1b6aa 63 49 64 78 3b 20 70 53 72 63 49 64 78 3d 70 53  cIdx; pSrcIdx=pS
1b6ab 72 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  rcIdx->pNext){. 
1b6ac 20 20 20 20 20 69 66 28 20 78 66 65 72 43 6f 6d       if( xferCom
1b6ad 70 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44 65  patibleIndex(pDe
1b6ae 73 74 49 64 78 2c 20 70 53 72 63 49 64 78 29 20  stIdx, pSrcIdx) 
1b6af 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
1b6b0 20 20 20 69 66 28 20 70 53 72 63 49 64 78 3d 3d     if( pSrcIdx==
1b6b1 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
1b6b2 6e 20 30 3b 20 20 20 20 2f 2a 20 70 44 65 73 74  n 0;    /* pDest
1b6b3 49 64 78 20 68 61 73 20 6e 6f 20 63 6f 72 72 65  Idx has no corre
1b6b4 73 70 6f 6e 64 69 6e 67 20 69 6e 64 65 78 20 69  sponding index i
1b6b5 6e 20 70 53 72 63 20 2a 2f 0a 20 20 20 20 7d 0a  n pSrc */.    }.
1b6b6 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
1b6b7 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
1b6b8 69 66 28 20 70 44 65 73 74 2d 3e 70 43 68 65 63  if( pDest->pChec
1b6b9 6b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78 70  k && !sqlite3Exp
1b6ba 72 43 6f 6d 70 61 72 65 28 70 53 72 63 2d 3e 70  rCompare(pSrc->p
1b6bb 43 68 65 63 6b 2c 20 70 44 65 73 74 2d 3e 70 43  Check, pDest->pC
1b6bc 68 65 63 6b 29 20 29 7b 0a 20 20 20 20 72 65 74  heck) ){.    ret
1b6bd 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 61 62 6c  urn 0;   /* Tabl
1b6be 65 73 20 68 61 76 65 20 64 69 66 66 65 72 65 6e  es have differen
1b6bf 74 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  t CHECK constrai
1b6c0 6e 74 73 2e 20 20 54 69 63 6b 65 74 20 23 32 32  nts.  Ticket #22
1b6c1 35 32 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66  52 */.  }.#endif
1b6c2 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74  ..  /* If we get
1b6c3 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65   this far, it me
1b6c4 61 6e 73 20 65 69 74 68 65 72 3a 0a 20 20 2a 2a  ans either:.  **
1b6c5 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20 57 65 20  .  **    *   We 
1b6c6 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20 74 68  can always do th
1b6c7 65 20 74 72 61 6e 73 66 65 72 20 69 66 20 74 68  e transfer if th
1b6c8 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73  e table contains
1b6c9 20 61 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   an.  **        
1b6ca 61 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d 61  an integer prima
1b6cb 72 79 20 6b 65 79 0a 20 20 2a 2a 0a 20 20 2a 2a  ry key.  **.  **
1b6cc 20 20 20 20 2a 20 20 20 57 65 20 63 61 6e 20 63      *   We can c
1b6cd 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 64 6f 20  onditionally do 
1b6ce 74 68 65 20 74 72 61 6e 73 66 65 72 20 69 66 20  the transfer if 
1b6cf 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a  the destination.
1b6d0 20 20 2a 2a 20 20 20 20 20 20 20 20 74 61 62 6c    **        tabl
1b6d1 65 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a 2f  e is empty..  */
1b6d2 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1b6d3 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 78 66  EST.  sqlite3_xf
1b6d4 65 72 6f 70 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  eropt_count++;.#
1b6d5 65 6e 64 69 66 0a 20 20 69 44 62 53 72 63 20 3d  endif.  iDbSrc =
1b6d6 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1b6d7 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
1b6d8 2c 20 70 53 72 63 2d 3e 70 53 63 68 65 6d 61 29  , pSrc->pSchema)
1b6d9 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ;.  v = sqlite3G
1b6da 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1b6db 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
1b6dc 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
1b6dd 2c 20 69 44 62 53 72 63 29 3b 0a 20 20 69 53 72  , iDbSrc);.  iSr
1b6de 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  c = pParse->nTab
1b6df 2b 2b 3b 0a 20 20 69 44 65 73 74 20 3d 20 70 50  ++;.  iDest = pP
1b6e0 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
1b6e1 63 6f 75 6e 74 65 72 4d 65 6d 20 3d 20 61 75 74  counterMem = aut
1b6e2 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65  oIncBegin(pParse
1b6e3 2c 20 69 44 62 44 65 73 74 2c 20 70 44 65 73 74  , iDbDest, pDest
1b6e4 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e  );.  sqlite3Open
1b6e5 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
1b6e6 65 73 74 2c 20 69 44 62 44 65 73 74 2c 20 70 44  est, iDbDest, pD
1b6e7 65 73 74 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  est, OP_OpenWrit
1b6e8 65 29 3b 0a 20 20 69 66 28 20 28 70 44 65 73 74  e);.  if( (pDest
1b6e9 2d 3e 69 50 4b 65 79 3c 30 20 26 26 20 70 44 65  ->iPKey<0 && pDe
1b6ea 73 74 2d 3e 70 49 6e 64 65 78 21 3d 30 29 20 7c  st->pIndex!=0) |
1b6eb 7c 20 64 65 73 74 48 61 73 55 6e 69 71 75 65 49  | destHasUniqueI
1b6ec 64 78 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  dx ){.    /* If 
1b6ed 74 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 68 61  tables do not ha
1b6ee 76 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  ve an INTEGER PR
1b6ef 49 4d 41 52 59 20 4b 45 59 20 61 6e 64 20 74 68  IMARY KEY and th
1b6f0 65 72 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 69  ere.    ** are i
1b6f1 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 6f 70  ndices to be cop
1b6f2 69 65 64 20 61 6e 64 20 74 68 65 20 64 65 73 74  ied and the dest
1b6f3 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 65  ination is not e
1b6f4 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20  mpty,.    ** we 
1b6f5 68 61 76 65 20 74 6f 20 64 69 73 61 6c 6c 6f 77  have to disallow
1b6f6 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70   the transfer op
1b6f7 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65 63 61 75  timization becau
1b6f8 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68  se the.    ** th
1b6f9 65 20 72 6f 77 69 64 73 20 6d 69 67 68 74 20 63  e rowids might c
1b6fa 68 61 6e 67 65 20 77 68 69 63 68 20 77 69 6c 6c  hange which will
1b6fb 20 6d 65 73 73 20 75 70 20 69 6e 64 65 78 69 6e   mess up indexin
1b6fc 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  g..    **.    **
1b6fd 20 4f 72 20 69 66 20 74 68 65 20 64 65 73 74 69   Or if the desti
1b6fe 6e 61 74 69 6f 6e 20 68 61 73 20 61 20 55 4e 49  nation has a UNI
1b6ff 51 55 45 20 69 6e 64 65 78 20 61 6e 64 20 69 73  QUE index and is
1b700 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20 20   not empty,.    
1b701 2a 2a 20 77 65 20 61 6c 73 6f 20 64 69 73 61 6c  ** we also disal
1b702 6c 6f 77 20 74 68 65 20 74 72 61 6e 73 66 65 72  low the transfer
1b703 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65   optimization be
1b704 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 0a  cause we cannot.
1b705 20 20 20 20 2a 2a 20 69 6e 73 75 72 65 20 74 68      ** insure th
1b706 61 74 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69  at all entries i
1b707 6e 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 44  n the union of D
1b708 45 53 54 20 61 6e 64 20 53 52 43 20 77 69 6c 6c  EST and SRC will
1b709 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e 69 71 75   be.    ** uniqu
1b70a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64  e..    */.    ad
1b70b 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
1b70c 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65 77  eAddOp(v, OP_Rew
1b70d 69 6e 64 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a  ind, iDest, 0);.
1b70e 20 20 20 20 65 6d 70 74 79 44 65 73 74 54 65 73      emptyDestTes
1b70f 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  t = sqlite3VdbeA
1b710 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  ddOp(v, OP_Goto,
1b711 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
1b712 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1b713 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d 65 6c  v, addr1);.  }el
1b714 73 65 7b 0a 20 20 20 20 65 6d 70 74 79 44 65 73  se{.    emptyDes
1b715 74 54 65 73 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  tTest = 0;.  }. 
1b716 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
1b717 65 28 70 50 61 72 73 65 2c 20 69 53 72 63 2c 20  e(pParse, iSrc, 
1b718 69 44 62 53 72 63 2c 20 70 53 72 63 2c 20 4f 50  iDbSrc, pSrc, OP
1b719 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 65 6d  _OpenRead);.  em
1b71a 70 74 79 53 72 63 54 65 73 74 20 3d 20 73 71 6c  ptySrcTest = sql
1b71b 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b71c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72 63   OP_Rewind, iSrc
1b71d 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 44 65 73  , 0);.  if( pDes
1b71e 74 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20  t->iPKey>=0 ){. 
1b71f 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
1b720 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1b721 50 5f 52 6f 77 69 64 2c 20 69 53 72 63 2c 20 30  P_Rowid, iSrc, 0
1b722 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1b723 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75  beAddOp(v, OP_Du
1b724 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 64  p, 0, 0);.    ad
1b725 64 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr2 = sqlite3Vdb
1b726 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74  eAddOp(v, OP_Not
1b727 45 78 69 73 74 73 2c 20 69 44 65 73 74 2c 20 30  Exists, iDest, 0
1b728 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1b729 62 65 4f 70 33 28 76 2c 20 4f 50 5f 48 61 6c 74  beOp3(v, OP_Halt
1b72a 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  , SQLITE_CONSTRA
1b72b 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 0a 20  INT, onError, . 
1b72c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b72d 20 20 20 20 20 22 50 52 49 4d 41 52 59 20 4b 45       "PRIMARY KE
1b72e 59 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65  Y must be unique
1b72f 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20  ", P3_STATIC);. 
1b730 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
1b731 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 32 29  mpHere(v, addr2)
1b732 3b 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65  ;.    autoIncSte
1b733 70 28 70 50 61 72 73 65 2c 20 63 6f 75 6e 74 65  p(pParse, counte
1b734 72 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 20 69  rMem);.  }else i
1b735 66 28 20 70 44 65 73 74 2d 3e 70 49 6e 64 65 78  f( pDest->pIndex
1b736 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 72 31  ==0 ){.    addr1
1b737 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1b738 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  dOp(v, OP_NewRow
1b739 69 64 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20  id, iDest, 0);. 
1b73a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72   }else{.    addr
1b73b 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
1b73c 64 64 4f 70 28 76 2c 20 4f 50 5f 52 6f 77 69 64  ddOp(v, OP_Rowid
1b73d 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 20 20  , iSrc, 0);.    
1b73e 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e 61  assert( pDest->a
1b73f 75 74 6f 49 6e 63 3d 3d 30 20 29 3b 0a 20 20 7d  utoInc==0 );.  }
1b740 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1b741 64 4f 70 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74  dOp(v, OP_RowDat
1b742 61 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 73  a, iSrc, 0);.  s
1b743 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c  qlite3VdbeOp3(v,
1b744 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 44 65 73   OP_Insert, iDes
1b745 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
1b746 20 20 20 20 20 20 20 4f 50 46 4c 41 47 5f 4e 43         OPFLAG_NC
1b747 48 41 4e 47 45 7c 4f 50 46 4c 41 47 5f 4c 41 53  HANGE|OPFLAG_LAS
1b748 54 52 4f 57 49 44 7c 4f 50 46 4c 41 47 5f 41 50  TROWID|OPFLAG_AP
1b749 50 45 4e 44 2c 0a 20 20 20 20 20 20 20 20 20 20  PEND,.          
1b74a 20 20 20 20 20 20 20 20 20 20 70 44 65 73 74 2d            pDest-
1b74b 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 73 71  >zName, 0);.  sq
1b74c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b74d 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c  , OP_Next, iSrc,
1b74e 20 61 64 64 72 31 29 3b 0a 20 20 61 75 74 6f 49   addr1);.  autoI
1b74f 6e 63 45 6e 64 28 70 50 61 72 73 65 2c 20 69 44  ncEnd(pParse, iD
1b750 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20 63 6f  bDest, pDest, co
1b751 75 6e 74 65 72 4d 65 6d 29 3b 0a 20 20 66 6f 72  unterMem);.  for
1b752 28 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 2d  (pDestIdx=pDest-
1b753 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49 64  >pIndex; pDestId
1b754 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65 73  x; pDestIdx=pDes
1b755 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  tIdx->pNext){.  
1b756 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d 70 53    for(pSrcIdx=pS
1b757 72 63 2d 3e 70 49 6e 64 65 78 3b 20 70 53 72 63  rc->pIndex; pSrc
1b758 49 64 78 3b 20 70 53 72 63 49 64 78 3d 70 53 72  Idx; pSrcIdx=pSr
1b759 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  cIdx->pNext){.  
1b75a 20 20 20 20 69 66 28 20 78 66 65 72 43 6f 6d 70      if( xferComp
1b75b 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44 65 73  atibleIndex(pDes
1b75c 74 49 64 78 2c 20 70 53 72 63 49 64 78 29 20 29  tIdx, pSrcIdx) )
1b75d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1b75e 20 20 61 73 73 65 72 74 28 20 70 53 72 63 49 64    assert( pSrcId
1b75f 78 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  x );.    sqlite3
1b760 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b761 43 6c 6f 73 65 2c 20 69 53 72 63 2c 20 30 29 3b  Close, iSrc, 0);
1b762 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b763 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73  AddOp(v, OP_Clos
1b764 65 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20  e, iDest, 0);.  
1b765 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1b766 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  Op(v, OP_Integer
1b767 2c 20 69 44 62 53 72 63 2c 20 30 29 3b 0a 20 20  , iDbSrc, 0);.  
1b768 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33    pKey = sqlite3
1b769 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61  IndexKeyinfo(pPa
1b76a 72 73 65 2c 20 70 53 72 63 49 64 78 29 3b 0a 20  rse, pSrcIdx);. 
1b76b 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
1b76c 76 2c 20 22 23 20 25 73 22 2c 20 70 53 72 63 49  v, "# %s", pSrcI
1b76d 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  dx->zName));.   
1b76e 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1b76f 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20  v, OP_OpenRead, 
1b770 69 53 72 63 2c 20 70 53 72 63 49 64 78 2d 3e 74  iSrc, pSrcIdx->t
1b771 6e 75 6d 2c 20 0a 20 20 20 20 20 20 20 20 20 20  num, .          
1b772 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
1b773 70 4b 65 79 2c 20 50 33 5f 4b 45 59 49 4e 46 4f  pKey, P3_KEYINFO
1b774 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 73  _HANDOFF);.    s
1b775 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1b776 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
1b777 44 62 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20  DbDest, 0);.    
1b778 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e  pKey = sqlite3In
1b779 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
1b77a 65 2c 20 70 44 65 73 74 49 64 78 29 3b 0a 20 20  e, pDestIdx);.  
1b77b 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
1b77c 2c 20 22 23 20 25 73 22 2c 20 70 44 65 73 74 49  , "# %s", pDestI
1b77d 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  dx->zName));.   
1b77e 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1b77f 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c  v, OP_OpenWrite,
1b780 20 69 44 65 73 74 2c 20 70 44 65 73 74 49 64 78   iDest, pDestIdx
1b781 2d 3e 74 6e 75 6d 2c 20 0a 20 20 20 20 20 20 20  ->tnum, .       
1b782 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
1b783 72 2a 29 70 4b 65 79 2c 20 50 33 5f 4b 45 59 49  r*)pKey, P3_KEYI
1b784 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
1b785 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
1b786 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b787 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30  _Rewind, iSrc, 0
1b788 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1b789 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 6f  beAddOp(v, OP_Ro
1b78a 77 4b 65 79 2c 20 69 53 72 63 2c 20 30 29 3b 0a  wKey, iSrc, 0);.
1b78b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1b78c 64 64 4f 70 28 76 2c 20 4f 50 5f 49 64 78 49 6e  ddOp(v, OP_IdxIn
1b78d 73 65 72 74 2c 20 69 44 65 73 74 2c 20 31 29 3b  sert, iDest, 1);
1b78e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b78f 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74  AddOp(v, OP_Next
1b790 2c 20 69 53 72 63 2c 20 61 64 64 72 31 2b 31 29  , iSrc, addr1+1)
1b791 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1b792 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
1b793 72 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  r1);.  }.  sqlit
1b794 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1b795 2c 20 65 6d 70 74 79 53 72 63 54 65 73 74 29 3b  , emptySrcTest);
1b796 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1b797 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
1b798 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 73 71 6c   iSrc, 0);.  sql
1b799 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b79a 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74   OP_Close, iDest
1b79b 2c 20 30 29 3b 0a 20 20 69 66 28 20 65 6d 70 74  , 0);.  if( empt
1b79c 79 44 65 73 74 54 65 73 74 20 29 7b 0a 20 20 20  yDestTest ){.   
1b79d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1b79e 70 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  p(v, OP_Halt, SQ
1b79f 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20  LITE_OK, 0);.   
1b7a0 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1b7a1 48 65 72 65 28 76 2c 20 65 6d 70 74 79 44 65 73  Here(v, emptyDes
1b7a2 74 54 65 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  tTest);.    sqli
1b7a3 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1b7a4 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c  OP_Close, iDest,
1b7a5 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   0);.    return 
1b7a6 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
1b7a7 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a  return 1;.  }.}.
1b7a8 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1b7a9 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a  _OMIT_XFER_OPT *
1b7aa 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
1b7ab 2a 2a 20 45 6e 64 20 6f 66 20 69 6e 73 65 72 74  ** End of insert
1b7ac 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1b7ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7af 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1b7b0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 65  ** Begin file le
1b7b1 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  gacy.c *********
1b7b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7b4 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
1b7b5 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
1b7b6 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1b7b7 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1b7b8 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1b7b9 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1b7ba 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1b7bb 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1b7bc 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1b7bd 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1b7be 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1b7bf 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1b7c0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1b7c1 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1b7c2 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1b7c3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1b7c4 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1b7c5 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1b7c6 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1b7c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69  *********.** Mai
1b7cc 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 53  n file for the S
1b7cd 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20  QLite library.  
1b7ce 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  The routines in 
1b7cf 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70  this file.** imp
1b7d0 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67 72  lement the progr
1b7d1 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65 20  ammer interface 
1b7d2 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  to the library. 
1b7d3 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20   Routines in.** 
1b7d4 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65 20  other files are 
1b7d5 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65  for internal use
1b7d6 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20 73   by SQLite and s
1b7d7 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20  hould not be.** 
1b7d8 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65 72  accessed by user
1b7d9 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  s of the library
1b7da 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c 65 67  ..**.** $Id: leg
1b7db 61 63 79 2e 63 2c 76 20 31 2e 32 32 20 32 30 30  acy.c,v 1.22 200
1b7dc 37 2f 30 38 2f 32 39 20 31 32 3a 33 31 3a 32 36  7/08/29 12:31:26
1b7dd 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
1b7de 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78   $.*/.../*.** Ex
1b7df 65 63 75 74 65 20 53 51 4c 20 63 6f 64 65 2e 20  ecute SQL code. 
1b7e0 20 52 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74   Return one of t
1b7e1 68 65 20 53 51 4c 49 54 45 5f 20 73 75 63 63 65  he SQLITE_ succe
1b7e2 73 73 2f 66 61 69 6c 75 72 65 0a 2a 2a 20 63 6f  ss/failure.** co
1b7e3 64 65 73 2e 20 20 41 6c 73 6f 20 77 72 69 74 65  des.  Also write
1b7e4 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1b7e5 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  e into memory ob
1b7e6 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d  tained from.** m
1b7e7 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65  alloc() and make
1b7e8 20 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74   *pzErrMsg point
1b7e9 20 74 6f 20 74 68 61 74 20 6d 65 73 73 61 67 65   to that message
1b7ea 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
1b7eb 51 4c 20 69 73 20 61 20 71 75 65 72 79 2c 20 74  QL is a query, t
1b7ec 68 65 6e 20 66 6f 72 20 65 61 63 68 20 72 6f 77  hen for each row
1b7ed 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 72 65   in the query re
1b7ee 73 75 6c 74 0a 2a 2a 20 74 68 65 20 78 43 61 6c  sult.** the xCal
1b7ef 6c 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e  lback() function
1b7f0 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 70 41 72   is called.  pAr
1b7f1 67 20 62 65 63 6f 6d 65 73 20 74 68 65 20 66 69  g becomes the fi
1b7f2 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
1b7f3 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29 2e 20  to xCallback(). 
1b7f4 20 49 66 20 78 43 61 6c 6c 62 61 63 6b 3d 4e 55   If xCallback=NU
1b7f5 4c 4c 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c 62  LL then no callb
1b7f6 61 63 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65  ack.** is invoke
1b7f7 64 2c 20 65 76 65 6e 20 66 6f 72 20 71 75 65 72  d, even for quer
1b7f8 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ies..*/.SQLITE_A
1b7f9 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
1b7fa 78 65 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  xec(.  sqlite3 *
1b7fb 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
1b7fc 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1b7fd 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  se on which the 
1b7fe 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a  SQL executes */.
1b7ff 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
1b800 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ql,           /*
1b801 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65   The SQL to be e
1b802 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 73 71 6c  xecuted */.  sql
1b803 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 20 78 43  ite3_callback xC
1b804 61 6c 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e 76 6f  allback, /* Invo
1b805 6b 65 20 74 68 69 73 20 63 61 6c 6c 62 61 63 6b  ke this callback
1b806 20 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 76 6f   routine */.  vo
1b807 69 64 20 2a 70 41 72 67 2c 20 20 20 20 20 20 20  id *pArg,       
1b808 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
1b809 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78  st argument to x
1b80a 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a 20 20  Callback() */.  
1b80b 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20  char **pzErrMsg 
1b80c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
1b80d 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61  rite error messa
1b80e 67 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ges here */.){. 
1b80f 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1b810 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  _OK;.  const cha
1b811 72 20 2a 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20  r *zLeftover;.  
1b812 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1b813 74 6d 74 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  tmt = 0;.  char 
1b814 2a 2a 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 0a 20  **azCols = 0;.. 
1b815 20 69 6e 74 20 6e 52 65 74 72 79 20 3d 20 30 3b   int nRetry = 0;
1b816 0a 20 20 69 6e 74 20 6e 43 61 6c 6c 62 61 63 6b  .  int nCallback
1b817 3b 0a 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30  ;..  if( zSql==0
1b818 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
1b819 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  _OK;..  sqlite3_
1b81a 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
1b81b 6d 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28  mutex);.  while(
1b81c 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20   (rc==SQLITE_OK 
1b81d 7c 7c 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 53  || (rc==SQLITE_S
1b81e 43 48 45 4d 41 20 26 26 20 28 2b 2b 6e 52 65 74  CHEMA && (++nRet
1b81f 72 79 29 3c 32 29 29 20 26 26 20 7a 53 71 6c 5b  ry)<2)) && zSql[
1b820 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43  0] ){.    int nC
1b821 6f 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 2a 61  ol;.    char **a
1b822 7a 56 61 6c 73 20 3d 20 30 3b 0a 0a 20 20 20 20  zVals = 0;..    
1b823 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 72  pStmt = 0;.    r
1b824 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  c = sqlite3_prep
1b825 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31  are(db, zSql, -1
1b826 2c 20 26 70 53 74 6d 74 2c 20 26 7a 4c 65 66 74  , &pStmt, &zLeft
1b827 6f 76 65 72 29 3b 0a 20 20 20 20 61 73 73 65 72  over);.    asser
1b828 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
1b829 20 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29 3b 0a   || pStmt==0 );.
1b82a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1b82b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63  TE_OK ){.      c
1b82c 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20  ontinue;.    }. 
1b82d 20 20 20 69 66 28 20 21 70 53 74 6d 74 20 29 7b     if( !pStmt ){
1b82e 0a 20 20 20 20 20 20 2f 2a 20 74 68 69 73 20 68  .      /* this h
1b82f 61 70 70 65 6e 73 20 66 6f 72 20 61 20 63 6f 6d  appens for a com
1b830 6d 65 6e 74 20 6f 72 20 77 68 69 74 65 2d 73 70  ment or white-sp
1b831 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 7a 53 71  ace */.      zSq
1b832 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20  l = zLeftover;. 
1b833 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1b834 20 20 20 7d 0a 0a 20 20 20 20 6e 43 61 6c 6c 62     }..    nCallb
1b835 61 63 6b 20 3d 20 30 3b 0a 0a 20 20 20 20 6e 43  ack = 0;..    nC
1b836 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ol = sqlite3_col
1b837 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29  umn_count(pStmt)
1b838 3b 0a 20 20 20 20 61 7a 43 6f 6c 73 20 3d 20 73  ;.    azCols = s
1b839 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1b83a 72 6f 28 64 62 2c 20 32 2a 6e 43 6f 6c 2a 73 69  ro(db, 2*nCol*si
1b83b 7a 65 6f 66 28 63 6f 6e 73 74 20 63 68 61 72 20  zeof(const char 
1b83c 2a 29 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28  *) + 1);.    if(
1b83d 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20 20   azCols==0 ){.  
1b83e 20 20 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75      goto exec_ou
1b83f 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 68  t;.    }..    wh
1b840 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 20 20  ile( 1 ){.      
1b841 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 72 63 20  int i;.      rc 
1b842 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  = sqlite3_step(p
1b843 53 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  Stmt);..      /*
1b844 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   Invoke the call
1b845 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 66  back function if
1b846 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20   required */.   
1b847 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63 6b     if( xCallback
1b848 20 26 26 20 28 53 51 4c 49 54 45 5f 52 4f 57 3d   && (SQLITE_ROW=
1b849 3d 72 63 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  =rc || .        
1b84a 20 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45 3d 3d    (SQLITE_DONE==
1b84b 72 63 20 26 26 20 21 6e 43 61 6c 6c 62 61 63 6b  rc && !nCallback
1b84c 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53 51   && db->flags&SQ
1b84d 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63  LITE_NullCallbac
1b84e 6b 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  k)) ){.        i
1b84f 66 28 20 30 3d 3d 6e 43 61 6c 6c 62 61 63 6b 20  f( 0==nCallback 
1b850 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  ){.          for
1b851 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
1b852 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
1b853 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28 63 68 61  azCols[i] = (cha
1b854 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  r *)sqlite3_colu
1b855 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c 20 69  mn_name(pStmt, i
1b856 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
1b857 20 20 20 20 20 20 20 20 20 6e 43 61 6c 6c 62 61           nCallba
1b858 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ck++;.        }.
1b859 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
1b85a 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
1b85b 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 20 3d          azVals =
1b85c 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a   &azCols[nCol];.
1b85d 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
1b85e 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
1b85f 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 56  .            azV
1b860 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a  als[i] = (char *
1b861 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
1b862 74 65 78 74 28 70 53 74 6d 74 2c 20 69 29 3b 0a  text(pStmt, i);.
1b863 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1b864 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1b865 28 20 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67  ( xCallback(pArg
1b866 2c 20 6e 43 6f 6c 2c 20 61 7a 56 61 6c 73 2c 20  , nCol, azVals, 
1b867 61 7a 43 6f 6c 73 29 20 29 7b 0a 20 20 20 20 20  azCols) ){.     
1b868 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1b869 5f 41 42 4f 52 54 3b 0a 20 20 20 20 20 20 20 20  _ABORT;.        
1b86a 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b    goto exec_out;
1b86b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1b86c 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63   }..      if( rc
1b86d 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  !=SQLITE_ROW ){.
1b86e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1b86f 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
1b870 74 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 70 53  tmt);.        pS
1b871 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  tmt = 0;.       
1b872 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1b873 53 43 48 45 4d 41 20 29 7b 0a 20 20 20 20 20 20  SCHEMA ){.      
1b874 20 20 20 20 6e 52 65 74 72 79 20 3d 20 30 3b 0a      nRetry = 0;.
1b875 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20 3d            zSql =
1b876 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20 20   zLeftover;.    
1b877 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 73        while( iss
1b878 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63  pace((unsigned c
1b879 68 61 72 29 7a 53 71 6c 5b 30 5d 29 20 29 20 7a  har)zSql[0]) ) z
1b87a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  Sql++;.        }
1b87b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1b87c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
1b87d 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1b87e 61 7a 43 6f 6c 73 29 3b 0a 20 20 20 20 61 7a 43  azCols);.    azC
1b87f 6f 6c 73 20 3d 20 30 3b 0a 20 20 7d 0a 0a 65 78  ols = 0;.  }..ex
1b880 65 63 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 53  ec_out:.  if( pS
1b881 74 6d 74 20 29 20 73 71 6c 69 74 65 33 5f 66 69  tmt ) sqlite3_fi
1b882 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20  nalize(pStmt);. 
1b883 20 69 66 28 20 61 7a 43 6f 6c 73 20 29 20 73 71   if( azCols ) sq
1b884 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 6f 6c  lite3_free(azCol
1b885 73 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  s);..  rc = sqli
1b886 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
1b887 63 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  c);.  if( rc!=SQ
1b888 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 3d 3d 73  LITE_OK && rc==s
1b889 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64  qlite3_errcode(d
1b88a 62 29 20 26 26 20 70 7a 45 72 72 4d 73 67 20 29  b) && pzErrMsg )
1b88b 7b 0a 20 20 20 20 69 6e 74 20 6e 45 72 72 4d 73  {.    int nErrMs
1b88c 67 20 3d 20 31 20 2b 20 73 74 72 6c 65 6e 28 73  g = 1 + strlen(s
1b88d 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62  qlite3_errmsg(db
1b88e 29 29 3b 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73  ));.    *pzErrMs
1b88f 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  g = sqlite3_mall
1b890 6f 63 28 6e 45 72 72 4d 73 67 29 3b 0a 20 20 20  oc(nErrMsg);.   
1b891 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67 20 29   if( *pzErrMsg )
1b892 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 2a  {.      memcpy(*
1b893 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74 65  pzErrMsg, sqlite
1b894 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 6e 45  3_errmsg(db), nE
1b895 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20  rrMsg);.    }.  
1b896 7d 65 6c 73 65 20 69 66 28 20 70 7a 45 72 72 4d  }else if( pzErrM
1b897 73 67 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  sg ){.    *pzErr
1b898 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  Msg = 0;.  }..  
1b899 61 73 73 65 72 74 28 20 28 72 63 26 64 62 2d 3e  assert( (rc&db->
1b89a 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a  errMask)==rc );.
1b89b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1b89c 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
1b89d 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1b89e 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1b89f 2a 20 45 6e 64 20 6f 66 20 6c 65 67 61 63 79 2e  * End of legacy.
1b8a0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1b8a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8a3 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1b8a4 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 6f 61  * Begin file loa
1b8a5 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  dext.c *********
1b8a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8a8 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e  /./*.** 2006 Jun
1b8a9 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  e 7.**.** The au
1b8aa 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1b8ab 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1b8ac 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1b8ad 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1b8ae 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1b8af 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1b8b0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1b8b1 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1b8b2 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
1b8b3 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
1b8b4 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
1b8b5 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
1b8b6 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
1b8b7 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
1b8b8 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
1b8b9 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1b8ba 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1b8bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8bf 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
1b8c0 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
1b8c1 65 64 20 74 6f 20 64 79 6e 61 6d 69 63 61 6c 6c  ed to dynamicall
1b8c2 79 20 6c 6f 61 64 20 65 78 74 65 6e 73 69 6f 6e  y load extension
1b8c3 73 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 53 51  s into.** the SQ
1b8c4 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  Lite library..*/
1b8c5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b8c6 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
1b8c7 49 4f 4e 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c  ION..#define SQL
1b8c8 49 54 45 5f 43 4f 52 45 20 31 20 20 2f 2a 20 44  ITE_CORE 1  /* D
1b8c9 69 73 61 62 6c 65 20 74 68 65 20 41 50 49 20 72  isable the API r
1b8ca 65 64 65 66 69 6e 69 74 69 6f 6e 20 69 6e 20 73  edefinition in s
1b8cb 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2f 0a 2f  qlite3ext.h */./
1b8cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
1b8cd 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33 65 78  nclude sqlite3ex
1b8ce 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  t.h in the middl
1b8cf 65 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a  e of loadext.c *
1b8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1b8d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1b8d2 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65  egin file sqlite
1b8d3 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  3ext.h *********
1b8d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1b8d6 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20 37  *.** 2006 June 7
1b8d7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1b8d8 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1b8d9 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1b8da 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1b8db 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1b8dc 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1b8dd 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1b8de 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1b8df 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1b8e0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1b8e1 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1b8e2 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1b8e3 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1b8e4 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1b8e5 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1b8e6 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1b8e7 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1b8e8 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1b8e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1b8ed 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
1b8ee 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
1b8ef 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
1b8f0 20 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a 20 73   for use by.** s
1b8f1 68 61 72 65 64 20 6c 69 62 72 61 72 69 65 73 20  hared libraries 
1b8f2 74 68 61 74 20 77 61 6e 74 20 74 6f 20 62 65 20  that want to be 
1b8f3 69 6d 70 6f 72 74 65 64 20 61 73 20 65 78 74 65  imported as exte
1b8f4 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 61  nsions into.** a
1b8f5 6e 20 53 51 4c 69 74 65 20 69 6e 73 74 61 6e 63  n SQLite instanc
1b8f6 65 2e 20 20 53 68 61 72 65 64 20 6c 69 62 72 61  e.  Shared libra
1b8f7 72 69 65 73 20 74 68 61 74 20 69 6e 74 65 6e 64  ries that intend
1b8f8 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a 2a 2a   to be loaded.**
1b8f9 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20 62   as extensions b
1b8fa 79 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 20  y SQLite should 
1b8fb 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20 66 69  #include this fi
1b8fc 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 2a  le instead of .*
1b8fd 2a 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2a 0a  * sqlite3.h..**.
1b8fe 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73 71 6c  ** @(#) $Id: sql
1b8ff 69 74 65 33 65 78 74 2e 68 2c 76 20 31 2e 31 37  ite3ext.h,v 1.17
1b900 20 32 30 30 37 2f 30 38 2f 33 31 20 31 36 3a 31   2007/08/31 16:1
1b901 31 3a 33 36 20 64 72 68 20 45 78 70 20 24 0a 2a  1:36 drh Exp $.*
1b902 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  /.#ifndef _SQLIT
1b903 45 33 45 58 54 5f 48 5f 0a 23 64 65 66 69 6e 65  E3EXT_H_.#define
1b904 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a   _SQLITE3EXT_H_.
1b905 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1b906 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
1b907 69 6e 65 73 20 73 71 6c 69 74 65 33 5f 61 70 69  ines sqlite3_api
1b908 5f 72 6f 75 74 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a  _routines;../*.*
1b909 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1b90a 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 20 70  structure hold p
1b90b 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 6f  ointers to all o
1b90c 66 20 74 68 65 20 53 51 4c 69 74 65 20 41 50 49  f the SQLite API
1b90d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  .** routines..**
1b90e 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20 49 6e  .** WARNING:  In
1b90f 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61   order to mainta
1b910 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  in backwards com
1b911 70 61 74 69 62 69 6c 69 74 79 2c 20 61 64 64 20  patibility, add 
1b912 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  new.** interface
1b913 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  s to the end of 
1b914 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
1b915 6e 6c 79 2e 20 20 49 66 20 79 6f 75 20 69 6e 73  nly.  If you ins
1b916 65 72 74 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72  ert new.** inter
1b917 66 61 63 65 73 20 69 6e 20 74 68 65 20 6d 69 64  faces in the mid
1b918 64 6c 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  dle of this stru
1b919 63 74 75 72 65 2c 20 74 68 65 6e 20 6f 6c 64 65  cture, then olde
1b91a 72 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 76  r different.** v
1b91b 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
1b91c 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  e will not be ab
1b91d 6c 65 20 74 6f 20 6c 6f 61 64 20 65 61 63 68 20  le to load each 
1b91e 6f 74 68 65 72 73 20 73 68 61 72 65 64 0a 2a 2a  others shared.**
1b91f 20 6c 69 62 72 61 72 69 65 73 21 0a 2a 2f 0a 73   libraries!.*/.s
1b920 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70  truct sqlite3_ap
1b921 69 5f 72 6f 75 74 69 6e 65 73 20 7b 0a 20 20 76  i_routines {.  v
1b922 6f 69 64 20 2a 20 28 2a 61 67 67 72 65 67 61 74  oid * (*aggregat
1b923 65 5f 63 6f 6e 74 65 78 74 29 28 73 71 6c 69 74  e_context)(sqlit
1b924 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20  e3_context*,int 
1b925 6e 42 79 74 65 73 29 3b 0a 20 20 69 6e 74 20 20  nBytes);.  int  
1b926 28 2a 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e  (*aggregate_coun
1b927 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
1b928 78 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  xt*);.  int  (*b
1b929 69 6e 64 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65  ind_blob)(sqlite
1b92a 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73  3_stmt*,int,cons
1b92b 74 20 76 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f  t void*,int n,vo
1b92c 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20  id(*)(void*));. 
1b92d 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75   int  (*bind_dou
1b92e 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ble)(sqlite3_stm
1b92f 74 2a 2c 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0a  t*,int,double);.
1b930 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e    int  (*bind_in
1b931 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t)(sqlite3_stmt*
1b932 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ,int,int);.  int
1b933 20 20 28 2a 62 69 6e 64 5f 69 6e 74 36 34 29 28    (*bind_int64)(
1b934 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
1b935 74 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b  t,sqlite_int64);
1b936 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 6e  .  int  (*bind_n
1b937 75 6c 6c 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ull)(sqlite3_stm
1b938 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20  t*,int);.  int  
1b939 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  (*bind_parameter
1b93a 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f  _count)(sqlite3_
1b93b 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28  stmt*);.  int  (
1b93c 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  *bind_parameter_
1b93d 69 6e 64 65 78 29 28 73 71 6c 69 74 65 33 5f 73  index)(sqlite3_s
1b93e 74 6d 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  tmt*,const char*
1b93f 7a 4e 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20  zName);.  const 
1b940 63 68 61 72 20 2a 20 28 2a 62 69 6e 64 5f 70 61  char * (*bind_pa
1b941 72 61 6d 65 74 65 72 5f 6e 61 6d 65 29 28 73 71  rameter_name)(sq
1b942 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
1b943 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f  ;.  int  (*bind_
1b944 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 73 74  text)(sqlite3_st
1b945 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  mt*,int,const ch
1b946 61 72 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a  ar*,int n,void(*
1b947 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74  )(void*));.  int
1b948 20 20 28 2a 62 69 6e 64 5f 74 65 78 74 31 36 29    (*bind_text16)
1b949 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
1b94a 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
1b94b 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  nt,void(*)(void*
1b94c 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e  ));.  int  (*bin
1b94d 64 5f 76 61 6c 75 65 29 28 73 71 6c 69 74 65 33  d_value)(sqlite3
1b94e 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74  _stmt*,int,const
1b94f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29   sqlite3_value*)
1b950 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f  ;.  int  (*busy_
1b951 68 61 6e 64 6c 65 72 29 28 73 71 6c 69 74 65 33  handler)(sqlite3
1b952 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69  *,int(*)(void*,i
1b953 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  nt),void*);.  in
1b954 74 20 20 28 2a 62 75 73 79 5f 74 69 6d 65 6f 75  t  (*busy_timeou
1b955 74 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  t)(sqlite3*,int 
1b956 6d 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 68  ms);.  int  (*ch
1b957 61 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a 29  anges)(sqlite3*)
1b958 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65  ;.  int  (*close
1b959 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69  )(sqlite3*);.  i
1b95a 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f  nt  (*collation_
1b95b 6e 65 65 64 65 64 29 28 73 71 6c 69 74 65 33 2a  needed)(sqlite3*
1b95c 2c 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76  ,void*,void(*)(v
1b95d 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
1b95e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
1b95f 20 63 68 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20   char*));.  int 
1b960 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65   (*collation_nee
1b961 64 65 64 31 36 29 28 73 71 6c 69 74 65 33 2a 2c  ded16)(sqlite3*,
1b962 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f  void*,void(*)(vo
1b963 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
1b964 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
1b965 76 6f 69 64 2a 29 29 3b 0a 20 20 63 6f 6e 73 74  void*));.  const
1b966 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
1b967 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73  _blob)(sqlite3_s
1b968 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
1b969 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
1b96a 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f 73  bytes)(sqlite3_s
1b96b 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
1b96c 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
1b96d 62 79 74 65 73 31 36 29 28 73 71 6c 69 74 65 33  bytes16)(sqlite3
1b96e 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
1b96f 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d  ;.  int  (*colum
1b970 6e 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33  n_count)(sqlite3
1b971 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20  _stmt*pStmt);.  
1b972 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63  const char * (*c
1b973 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
1b974 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
1b975 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
1b976 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
1b977 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36  _database_name16
1b978 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
1b979 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  int);.  const ch
1b97a 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65  ar * (*column_de
1b97b 63 6c 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f  cltype)(sqlite3_
1b97c 73 74 6d 74 2a 2c 69 6e 74 20 69 29 3b 0a 20 20  stmt*,int i);.  
1b97d 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
1b97e 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36  olumn_decltype16
1b97f 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
1b980 69 6e 74 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20  int);.  double  
1b981 28 2a 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 29  (*column_double)
1b982 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
1b983 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20  nt iCol);.  int 
1b984 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28 73   (*column_int)(s
1b985 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
1b986 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65   iCol);.  sqlite
1b987 5f 69 6e 74 36 34 20 20 28 2a 63 6f 6c 75 6d 6e  _int64  (*column
1b988 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f  _int64)(sqlite3_
1b989 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b  stmt*,int iCol);
1b98a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
1b98b 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28 73  (*column_name)(s
1b98c 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
1b98d 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
1b98e 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  * (*column_name1
1b98f 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
1b990 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63  ,int);.  const c
1b991 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f  har * (*column_o
1b992 72 69 67 69 6e 5f 6e 61 6d 65 29 28 73 71 6c 69  rigin_name)(sqli
1b993 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
1b994 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
1b995 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  *column_origin_n
1b996 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ame16)(sqlite3_s
1b997 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
1b998 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75  st char * (*colu
1b999 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 28 73  mn_table_name)(s
1b99a 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
1b99b 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
1b99c 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  * (*column_table
1b99d 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33  _name16)(sqlite3
1b99e 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
1b99f 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1b9a0 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65  ar * (*column_te
1b9a1 78 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  xt)(sqlite3_stmt
1b9a2 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63  *,int iCol);.  c
1b9a3 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f  onst void * (*co
1b9a4 6c 75 6d 6e 5f 74 65 78 74 31 36 29 28 73 71 6c  lumn_text16)(sql
1b9a5 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
1b9a6 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  Col);.  int  (*c
1b9a7 6f 6c 75 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69  olumn_type)(sqli
1b9a8 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
1b9a9 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  ol);.  sqlite3_v
1b9aa 61 6c 75 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76  alue* (*column_v
1b9ab 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74  alue)(sqlite3_st
1b9ac 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
1b9ad 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6d 6d 69 74   void * (*commit
1b9ae 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c  _hook)(sqlite3*,
1b9af 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f  int(*)(void*),vo
1b9b0 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  id*);.  int  (*c
1b9b1 6f 6d 70 6c 65 74 65 29 28 63 6f 6e 73 74 20 63  omplete)(const c
1b9b2 68 61 72 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20  har*sql);.  int 
1b9b3 20 28 2a 63 6f 6d 70 6c 65 74 65 31 36 29 28 63   (*complete16)(c
1b9b4 6f 6e 73 74 20 76 6f 69 64 2a 73 71 6c 29 3b 0a  onst void*sql);.
1b9b5 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f    int  (*create_
1b9b6 63 6f 6c 6c 61 74 69 6f 6e 29 28 73 71 6c 69 74  collation)(sqlit
1b9b7 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
1b9b8 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29  int,void*,int(*)
1b9b9 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
1b9ba 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
1b9bb 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20   void*));.  int 
1b9bc 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74   (*create_collat
1b9bd 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c  ion16)(sqlite3*,
1b9be 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
1b9bf 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69  void*,int(*)(voi
1b9c0 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
1b9c1 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
1b9c2 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  d*));.  int  (*c
1b9c3 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 29 28  reate_function)(
1b9c4 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
1b9c5 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69  har*,int,int,voi
1b9c6 64 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  d*,void (*xFunc)
1b9c7 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b9c8 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
1b9c9 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53  lue**),void (*xS
1b9ca 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
1b9cb 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
1b9cc 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20  3_value**),void 
1b9cd 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65  (*xFinal)(sqlite
1b9ce 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20  3_context*));.  
1b9cf 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75  int  (*create_fu
1b9d0 6e 63 74 69 6f 6e 31 36 29 28 73 71 6c 69 74 65  nction16)(sqlite
1b9d1 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  3*,const void*,i
1b9d2 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69  nt,int,void*,voi
1b9d3 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
1b9d4 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
1b9d5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
1b9d6 2c 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73  ,void (*xStep)(s
1b9d7 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
1b9d8 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
1b9d9 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e  e**),void (*xFin
1b9da 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  al)(sqlite3_cont
1b9db 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a  ext*));.  int (*
1b9dc 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73  create_module)(s
1b9dd 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
1b9de 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65  ar*,const sqlite
1b9df 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29  3_module*,void*)
1b9e0 3b 0a 20 20 69 6e 74 20 20 28 2a 64 61 74 61 5f  ;.  int  (*data_
1b9e1 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73  count)(sqlite3_s
1b9e2 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71  tmt*pStmt);.  sq
1b9e3 6c 69 74 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e  lite3 * (*db_han
1b9e4 64 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  dle)(sqlite3_stm
1b9e5 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63  t*);.  int (*dec
1b9e6 6c 61 72 65 5f 76 74 61 62 29 28 73 71 6c 69 74  lare_vtab)(sqlit
1b9e7 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  e3*,const char*)
1b9e8 3b 0a 20 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c  ;.  int  (*enabl
1b9e9 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 29 28  e_shared_cache)(
1b9ea 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65  int);.  int  (*e
1b9eb 72 72 63 6f 64 65 29 28 73 71 6c 69 74 65 33 2a  rrcode)(sqlite3*
1b9ec 64 62 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  db);.  const cha
1b9ed 72 20 2a 20 28 2a 65 72 72 6d 73 67 29 28 73 71  r * (*errmsg)(sq
1b9ee 6c 69 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74  lite3*);.  const
1b9ef 20 76 6f 69 64 20 2a 20 28 2a 65 72 72 6d 73 67   void * (*errmsg
1b9f0 31 36 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  16)(sqlite3*);. 
1b9f1 20 69 6e 74 20 20 28 2a 65 78 65 63 29 28 73 71   int  (*exec)(sq
1b9f2 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
1b9f3 72 2a 2c 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62  r*,sqlite3_callb
1b9f4 61 63 6b 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a  ack,void*,char**
1b9f5 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78 70 69  );.  int  (*expi
1b9f6 72 65 64 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  red)(sqlite3_stm
1b9f7 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69  t*);.  int  (*fi
1b9f8 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f  nalize)(sqlite3_
1b9f9 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76  stmt*pStmt);.  v
1b9fa 6f 69 64 20 20 28 2a 66 72 65 65 29 28 76 6f 69  oid  (*free)(voi
1b9fb 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66  d*);.  void  (*f
1b9fc 72 65 65 5f 74 61 62 6c 65 29 28 63 68 61 72 2a  ree_table)(char*
1b9fd 2a 72 65 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20  *result);.  int 
1b9fe 20 28 2a 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69   (*get_autocommi
1b9ff 74 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  t)(sqlite3*);.  
1ba00 76 6f 69 64 20 2a 20 28 2a 67 65 74 5f 61 75 78  void * (*get_aux
1ba01 64 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f  data)(sqlite3_co
1ba02 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69  ntext*,int);.  i
1ba03 6e 74 20 20 28 2a 67 65 74 5f 74 61 62 6c 65 29  nt  (*get_table)
1ba04 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
1ba05 63 68 61 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e  char*,char***,in
1ba06 74 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b  t*,int*,char**);
1ba07 0a 20 20 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c  .  int  (*global
1ba08 5f 72 65 63 6f 76 65 72 29 28 76 6f 69 64 29 3b  _recover)(void);
1ba09 0a 20 20 76 6f 69 64 20 20 28 2a 69 6e 74 65 72  .  void  (*inter
1ba0a 72 75 70 74 78 29 28 73 71 6c 69 74 65 33 2a 29  ruptx)(sqlite3*)
1ba0b 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
1ba0c 20 20 28 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f    (*last_insert_
1ba0d 72 6f 77 69 64 29 28 73 71 6c 69 74 65 33 2a 29  rowid)(sqlite3*)
1ba0e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
1ba0f 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e 29 28 76   (*libversion)(v
1ba10 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c  oid);.  int  (*l
1ba11 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
1ba12 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20  )(void);.  void 
1ba13 2a 28 2a 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b  *(*malloc)(int);
1ba14 0a 20 20 63 68 61 72 20 2a 20 28 2a 6d 70 72 69  .  char * (*mpri
1ba15 6e 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  ntf)(const char*
1ba16 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,...);.  int  (*
1ba17 6f 70 65 6e 29 28 63 6f 6e 73 74 20 63 68 61 72  open)(const char
1ba18 2a 2c 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20  *,sqlite3**);.  
1ba19 69 6e 74 20 20 28 2a 6f 70 65 6e 31 36 29 28 63  int  (*open16)(c
1ba1a 6f 6e 73 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74  onst void*,sqlit
1ba1b 65 33 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  e3**);.  int  (*
1ba1c 70 72 65 70 61 72 65 29 28 73 71 6c 69 74 65 33  prepare)(sqlite3
1ba1d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
1ba1e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a  t,sqlite3_stmt**
1ba1f 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a  ,const char**);.
1ba20 20 20 69 6e 74 20 20 28 2a 70 72 65 70 61 72 65    int  (*prepare
1ba21 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  16)(sqlite3*,con
1ba22 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c  st void*,int,sql
1ba23 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73  ite3_stmt**,cons
1ba24 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69  t void**);.  voi
1ba25 64 20 2a 20 28 2a 70 72 6f 66 69 6c 65 29 28 73  d * (*profile)(s
1ba26 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28  qlite3*,void(*)(
1ba27 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
1ba28 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  *,sqlite_uint64)
1ba29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20  ,void*);.  void 
1ba2a 20 28 2a 70 72 6f 67 72 65 73 73 5f 68 61 6e 64   (*progress_hand
1ba2b 6c 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  ler)(sqlite3*,in
1ba2c 74 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c  t,int(*)(void*),
1ba2d 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  void*);.  void *
1ba2e 28 2a 72 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a  (*realloc)(void*
1ba2f 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,int);.  int  (*
1ba30 72 65 73 65 74 29 28 73 71 6c 69 74 65 33 5f 73  reset)(sqlite3_s
1ba31 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f  tmt*pStmt);.  vo
1ba32 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f  id  (*result_blo
1ba33 62 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  b)(sqlite3_conte
1ba34 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  xt*,const void*,
1ba35 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
1ba36 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  *));.  void  (*r
1ba37 65 73 75 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71  esult_double)(sq
1ba38 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64  lite3_context*,d
1ba39 6f 75 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20  ouble);.  void  
1ba3a 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 29 28  (*result_error)(
1ba3b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
1ba3c 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
1ba3d 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
1ba3e 75 6c 74 5f 65 72 72 6f 72 31 36 29 28 73 71 6c  ult_error16)(sql
1ba3f 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f  ite3_context*,co
1ba40 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a  nst void*,int);.
1ba41 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
1ba42 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 63 6f  _int)(sqlite3_co
1ba43 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76  ntext*,int);.  v
1ba44 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e  oid  (*result_in
1ba45 74 36 34 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  t64)(sqlite3_con
1ba46 74 65 78 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74  text*,sqlite_int
1ba47 36 34 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  64);.  void  (*r
1ba48 65 73 75 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69  esult_null)(sqli
1ba49 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20  te3_context*);. 
1ba4a 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f   void  (*result_
1ba4b 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 63 6f  text)(sqlite3_co
1ba4c 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61  ntext*,const cha
1ba4d 72 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  r*,int,void(*)(v
1ba4e 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20  oid*));.  void  
1ba4f 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36 29  (*result_text16)
1ba50 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1ba51 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
1ba52 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
1ba53 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
1ba54 75 6c 74 5f 74 65 78 74 31 36 62 65 29 28 73 71  ult_text16be)(sq
1ba55 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
1ba56 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
1ba57 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
1ba58 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
1ba59 5f 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74  _text16le)(sqlit
1ba5a 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
1ba5b 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64  t void*,int,void
1ba5c 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
1ba5d 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 76 61  oid  (*result_va
1ba5e 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lue)(sqlite3_con
1ba5f 74 65 78 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61  text*,sqlite3_va
1ba60 6c 75 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20  lue*);.  void * 
1ba61 28 2a 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29  (*rollback_hook)
1ba62 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a  (sqlite3*,void(*
1ba63 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b  )(void*),void*);
1ba64 0a 20 20 69 6e 74 20 20 28 2a 73 65 74 5f 61 75  .  int  (*set_au
1ba65 74 68 6f 72 69 7a 65 72 29 28 73 71 6c 69 74 65  thorizer)(sqlite
1ba66 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  3*,int(*)(void*,
1ba67 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  int,const char*,
1ba68 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
1ba69 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
1ba6a 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76  ar*),void*);.  v
1ba6b 6f 69 64 20 20 28 2a 73 65 74 5f 61 75 78 64 61  oid  (*set_auxda
1ba6c 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ta)(sqlite3_cont
1ba6d 65 78 74 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76  ext*,int,void*,v
1ba6e 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b  oid (*)(void*));
1ba6f 0a 20 20 63 68 61 72 20 2a 20 28 2a 73 6e 70 72  .  char * (*snpr
1ba70 69 6e 74 66 29 28 69 6e 74 2c 63 68 61 72 2a 2c  intf)(int,char*,
1ba71 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29  const char*,...)
1ba72 3b 0a 20 20 69 6e 74 20 20 28 2a 73 74 65 70 29  ;.  int  (*step)
1ba73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
1ba74 0a 20 20 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f  .  int  (*table_
1ba75 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29  column_metadata)
1ba76 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
1ba77 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
1ba78 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68  *,const char*,ch
1ba79 61 72 20 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20  ar const**,char 
1ba7a 63 6f 6e 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74  const**,int*,int
1ba7b 2a 2c 69 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20  *,int*);.  void 
1ba7c 20 28 2a 74 68 72 65 61 64 5f 63 6c 65 61 6e 75   (*thread_cleanu
1ba7d 70 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20  p)(void);.  int 
1ba7e 20 28 2a 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73   (*total_changes
1ba7f 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76  )(sqlite3*);.  v
1ba80 6f 69 64 20 2a 20 28 2a 74 72 61 63 65 29 28 73  oid * (*trace)(s
1ba81 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 78 54  qlite3*,void(*xT
1ba82 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  race)(void*,cons
1ba83 74 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b  t char*),void*);
1ba84 0a 20 20 69 6e 74 20 20 28 2a 74 72 61 6e 73 66  .  int  (*transf
1ba85 65 72 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c  er_bindings)(sql
1ba86 69 74 65 33 5f 73 74 6d 74 2a 2c 73 71 6c 69 74  ite3_stmt*,sqlit
1ba87 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 76 6f 69  e3_stmt*);.  voi
1ba88 64 20 2a 20 28 2a 75 70 64 61 74 65 5f 68 6f 6f  d * (*update_hoo
1ba89 6b 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64  k)(sqlite3*,void
1ba8a 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 20 2c 63  (*)(void*,int ,c
1ba8b 68 61 72 20 63 6f 6e 73 74 2a 2c 63 68 61 72 20  har const*,char 
1ba8c 63 6f 6e 73 74 2a 2c 73 71 6c 69 74 65 5f 69 6e  const*,sqlite_in
1ba8d 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76  t64),void*);.  v
1ba8e 6f 69 64 20 2a 20 28 2a 75 73 65 72 5f 64 61 74  oid * (*user_dat
1ba8f 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  a)(sqlite3_conte
1ba90 78 74 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  xt*);.  const vo
1ba91 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 62 6c 6f  id * (*value_blo
1ba92 62 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  b)(sqlite3_value
1ba93 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c  *);.  int  (*val
1ba94 75 65 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65  ue_bytes)(sqlite
1ba95 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74  3_value*);.  int
1ba96 20 20 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 31    (*value_bytes1
1ba97 36 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  6)(sqlite3_value
1ba98 2a 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a  *);.  double  (*
1ba99 76 61 6c 75 65 5f 64 6f 75 62 6c 65 29 28 73 71  value_double)(sq
1ba9a 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
1ba9b 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 69 6e   int  (*value_in
1ba9c 74 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t)(sqlite3_value
1ba9d 2a 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  *);.  sqlite_int
1ba9e 36 34 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 36  64  (*value_int6
1ba9f 34 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  4)(sqlite3_value
1baa0 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c  *);.  int  (*val
1baa1 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 29  ue_numeric_type)
1baa2 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
1baa3 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
1baa4 65 64 20 63 68 61 72 20 2a 20 28 2a 76 61 6c 75  ed char * (*valu
1baa5 65 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f  e_text)(sqlite3_
1baa6 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74  value*);.  const
1baa7 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f   void * (*value_
1baa8 74 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f  text16)(sqlite3_
1baa9 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74  value*);.  const
1baaa 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f   void * (*value_
1baab 74 65 78 74 31 36 62 65 29 28 73 71 6c 69 74 65  text16be)(sqlite
1baac 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e  3_value*);.  con
1baad 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75  st void * (*valu
1baae 65 5f 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69  e_text16le)(sqli
1baaf 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69  te3_value*);.  i
1bab0 6e 74 20 20 28 2a 76 61 6c 75 65 5f 74 79 70 65  nt  (*value_type
1bab1 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
1bab2 29 3b 0a 20 20 63 68 61 72 20 2a 28 2a 76 6d 70  );.  char *(*vmp
1bab3 72 69 6e 74 66 29 28 63 6f 6e 73 74 20 63 68 61  rintf)(const cha
1bab4 72 2a 2c 76 61 5f 6c 69 73 74 29 3b 0a 20 20 2f  r*,va_list);.  /
1bab5 2a 20 41 64 64 65 64 20 3f 3f 3f 20 2a 2f 0a 20  * Added ??? */. 
1bab6 20 69 6e 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f   int (*overload_
1bab7 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65  function)(sqlite
1bab8 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  3*, const char *
1bab9 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e  zFuncName, int n
1baba 41 72 67 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64  Arg);.  /* Added
1babb 20 62 79 20 33 2e 33 2e 31 33 20 2a 2f 0a 20 20   by 3.3.13 */.  
1babc 69 6e 74 20 28 2a 70 72 65 70 61 72 65 5f 76 32  int (*prepare_v2
1babd 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
1babe 20 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74   char*,int,sqlit
1babf 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20  e3_stmt**,const 
1bac0 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28  char**);.  int (
1bac1 2a 70 72 65 70 61 72 65 31 36 5f 76 32 29 28 73  *prepare16_v2)(s
1bac2 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f  qlite3*,const vo
1bac3 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  id*,int,sqlite3_
1bac4 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69  stmt**,const voi
1bac5 64 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 63 6c  d**);.  int (*cl
1bac6 65 61 72 5f 62 69 6e 64 69 6e 67 73 29 28 73 71  ear_bindings)(sq
1bac7 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
1bac8 2f 2a 20 41 64 64 65 64 20 62 79 20 33 2e 34 2e  /* Added by 3.4.
1bac9 31 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 72 65  1 */.  int (*cre
1baca 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 29 28 73  ate_module_v2)(s
1bacb 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
1bacc 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65  ar*,const sqlite
1bacd 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 2c  3_module*,void*,
1bace 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29  void (*xDestroy)
1bacf 28 76 6f 69 64 20 2a 29 29 3b 0a 20 20 2f 2a 20  (void *));.  /* 
1bad0 41 64 64 65 64 20 62 79 20 33 2e 35 2e 30 20 2a  Added by 3.5.0 *
1bad1 2f 0a 20 20 69 6e 74 20 28 2a 62 69 6e 64 5f 7a  /.  int (*bind_z
1bad2 65 72 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  eroblob)(sqlite3
1bad3 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  _stmt*,int,int);
1bad4 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 62 79  .  int (*blob_by
1bad5 74 65 73 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f  tes)(sqlite3_blo
1bad6 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f  b*);.  int (*blo
1bad7 62 5f 63 6c 6f 73 65 29 28 73 71 6c 69 74 65 33  b_close)(sqlite3
1bad8 5f 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28  _blob*);.  int (
1bad9 2a 62 6c 6f 62 5f 6f 70 65 6e 29 28 73 71 6c 69  *blob_open)(sqli
1bada 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
1badb 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
1badc 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33  st char*,sqlite3
1badd 5f 69 6e 74 36 34 2c 69 6e 74 2c 73 71 6c 69 74  _int64,int,sqlit
1bade 65 33 5f 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69 6e  e3_blob**);.  in
1badf 74 20 28 2a 62 6c 6f 62 5f 72 65 61 64 29 28 73  t (*blob_read)(s
1bae0 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f 69  qlite3_blob*,voi
1bae1 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69  d*,int,int);.  i
1bae2 6e 74 20 28 2a 62 6c 6f 62 5f 77 72 69 74 65 29  nt (*blob_write)
1bae3 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 63  (sqlite3_blob*,c
1bae4 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69  onst void*,int,i
1bae5 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65  nt);.  int (*cre
1bae6 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
1bae7 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
1bae8 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a   char*,int,void*
1bae9 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e  ,int(*)(void*,in
1baea 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
1baeb 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 76  t,const void*),v
1baec 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
1baed 20 20 69 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e    int (*file_con
1baee 74 72 6f 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63  trol)(sqlite3*,c
1baef 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76  onst char*,int,v
1baf0 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33  oid*);.  sqlite3
1baf1 5f 69 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f  _int64 (*memory_
1baf2 68 69 67 68 77 61 74 65 72 29 28 69 6e 74 29 3b  highwater)(int);
1baf3 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
1baf4 20 28 2a 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28   (*memory_used)(
1baf5 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  void);.  sqlite3
1baf6 5f 6d 75 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f  _mutex *(*mutex_
1baf7 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76  alloc)(int);.  v
1baf8 6f 69 64 20 28 2a 6d 75 74 65 78 5f 65 6e 74 65  oid (*mutex_ente
1baf9 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  r)(sqlite3_mutex
1bafa 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74  *);.  void (*mut
1bafb 65 78 5f 66 72 65 65 29 28 73 71 6c 69 74 65 33  ex_free)(sqlite3
1bafc 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64  _mutex*);.  void
1bafd 20 28 2a 6d 75 74 65 78 5f 6c 65 61 76 65 29 28   (*mutex_leave)(
1bafe 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
1baff 0a 20 20 69 6e 74 20 28 2a 6d 75 74 65 78 5f 74  .  int (*mutex_t
1bb00 72 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ry)(sqlite3_mute
1bb01 78 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 6f 70 65  x*);.  int (*ope
1bb02 6e 5f 76 32 29 28 63 6f 6e 73 74 20 63 68 61 72  n_v2)(const char
1bb03 2a 2c 73 71 6c 69 74 65 33 2a 2a 2c 69 6e 74 2c  *,sqlite3**,int,
1bb04 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
1bb05 69 6e 74 20 28 2a 72 65 6c 65 61 73 65 5f 6d 65  int (*release_me
1bb06 6d 6f 72 79 29 28 69 6e 74 29 3b 0a 20 20 76 6f  mory)(int);.  vo
1bb07 69 64 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f  id (*result_erro
1bb08 72 5f 6e 6f 6d 65 6d 29 28 73 71 6c 69 74 65 33  r_nomem)(sqlite3
1bb09 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f  _context*);.  vo
1bb0a 69 64 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f  id (*result_erro
1bb0b 72 5f 74 6f 6f 62 69 67 29 28 73 71 6c 69 74 65  r_toobig)(sqlite
1bb0c 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69  3_context*);.  i
1bb0d 6e 74 20 28 2a 73 6c 65 65 70 29 28 69 6e 74 29  nt (*sleep)(int)
1bb0e 3b 0a 20 20 76 6f 69 64 20 28 2a 73 6f 66 74 5f  ;.  void (*soft_
1bb0f 68 65 61 70 5f 6c 69 6d 69 74 29 28 69 6e 74 29  heap_limit)(int)
1bb10 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ;.  sqlite3_vfs 
1bb11 2a 28 2a 76 66 73 5f 66 69 6e 64 29 28 63 6f 6e  *(*vfs_find)(con
1bb12 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74  st char*);.  int
1bb13 20 28 2a 76 66 73 5f 72 65 67 69 73 74 65 72 29   (*vfs_register)
1bb14 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 69 6e  (sqlite3_vfs*,in
1bb15 74 29 3b 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f  t);.  int (*vfs_
1bb16 75 6e 72 65 67 69 73 74 65 72 29 28 73 71 6c 69  unregister)(sqli
1bb17 74 65 33 5f 76 66 73 2a 29 3b 0a 7d 3b 0a 0a 2f  te3_vfs*);.};../
1bb18 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1bb19 6e 67 20 6d 61 63 72 6f 73 20 72 65 64 65 66 69  ng macros redefi
1bb1a 6e 65 20 74 68 65 20 41 50 49 20 72 6f 75 74 69  ne the API routi
1bb1b 6e 65 73 20 73 6f 20 74 68 61 74 20 74 68 65 79  nes so that they
1bb1c 20 61 72 65 0a 2a 2a 20 72 65 64 69 72 65 63 74   are.** redirect
1bb1d 65 64 20 74 68 72 6f 75 67 68 74 20 74 68 65 20  ed throught the 
1bb1e 67 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33 5f 61  global sqlite3_a
1bb1f 70 69 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  pi structure..**
1bb20 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
1bb21 66 69 6c 65 20 69 73 20 61 6c 73 6f 20 75 73 65  file is also use
1bb22 64 20 62 79 20 74 68 65 20 6c 6f 61 64 65 78 74  d by the loadext
1bb23 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 0a 2a  .c source file.*
1bb24 2a 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 6d  * (part of the m
1bb25 61 69 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 61  ain SQLite libra
1bb26 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74 65  ry - not an exte
1bb27 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61 74 0a 2a  nsion) so that.*
1bb28 2a 20 69 74 20 63 61 6e 20 67 65 74 20 61 63 63  * it can get acc
1bb29 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ess to the sqlit
1bb2a 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
1bb2b 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 65 66  structure.** def
1bb2c 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20 74 68  inition.  But th
1bb2d 65 20 6d 61 69 6e 20 6c 69 62 72 61 72 79 20 64  e main library d
1bb2e 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20  oes not want to 
1bb2f 72 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20  redefine.** the 
1bb30 41 50 49 2e 20 20 53 6f 20 74 68 65 20 72 65 64  API.  So the red
1bb31 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72 6f 73  efinition macros
1bb32 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64 20   are only valid 
1bb33 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  if the.** SQLITE
1bb34 5f 43 4f 52 45 20 6d 61 63 72 6f 73 20 69 73 20  _CORE macros is 
1bb35 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69  undefined..*/.#i
1bb36 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52  fndef SQLITE_COR
1bb37 45 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  E.#define sqlite
1bb38 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
1bb39 65 78 74 20 20 20 20 20 20 73 71 6c 69 74 65 33  ext      sqlite3
1bb3a 5f 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f  _api->aggregate_
1bb3b 63 6f 6e 74 65 78 74 0a 23 64 65 66 69 6e 65 20  context.#define 
1bb3c 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
1bb3d 65 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 73  e_count        s
1bb3e 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67 72  qlite3_api->aggr
1bb3f 65 67 61 74 65 5f 63 6f 75 6e 74 0a 23 64 65 66  egate_count.#def
1bb40 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
1bb41 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
1bb42 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bb43 62 69 6e 64 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  bind_blob.#defin
1bb44 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  e sqlite3_bind_d
1bb45 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  ouble           
1bb46 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
1bb47 6e 64 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e  nd_double.#defin
1bb48 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  e sqlite3_bind_i
1bb49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
1bb4a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
1bb4b 6e 64 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73  nd_int.#define s
1bb4c 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
1bb4d 34 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  4             sq
1bb4e 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
1bb4f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71  int64.#define sq
1bb50 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 20  lite3_bind_null 
1bb51 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1bb52 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 6e  ite3_api->bind_n
1bb53 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ull.#define sqli
1bb54 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
1bb55 65 72 5f 63 6f 75 6e 74 20 20 20 73 71 6c 69 74  er_count   sqlit
1bb56 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
1bb57 61 6d 65 74 65 72 5f 63 6f 75 6e 74 0a 23 64 65  ameter_count.#de
1bb58 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
1bb59 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
1bb5a 78 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  x   sqlite3_api-
1bb5b 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
1bb5c 69 6e 64 65 78 0a 23 64 65 66 69 6e 65 20 73 71  index.#define sq
1bb5d 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
1bb5e 65 74 65 72 5f 6e 61 6d 65 20 20 20 20 73 71 6c  eter_name    sql
1bb5f 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70  ite3_api->bind_p
1bb60 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 0a 23 64  arameter_name.#d
1bb61 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
1bb62 6e 64 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  nd_text         
1bb63 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1bb64 2d 3e 62 69 6e 64 5f 74 65 78 74 0a 23 64 65 66  ->bind_text.#def
1bb65 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
1bb66 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20  _text16         
1bb67 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bb68 62 69 6e 64 5f 74 65 78 74 31 36 0a 23 64 65 66  bind_text16.#def
1bb69 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
1bb6a 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20  _value          
1bb6b 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bb6c 62 69 6e 64 5f 76 61 6c 75 65 0a 23 64 65 66 69  bind_value.#defi
1bb6d 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  ne sqlite3_busy_
1bb6e 68 61 6e 64 6c 65 72 20 20 20 20 20 20 20 20 20  handler         
1bb6f 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
1bb70 75 73 79 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66  usy_handler.#def
1bb71 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79  ine sqlite3_busy
1bb72 5f 74 69 6d 65 6f 75 74 20 20 20 20 20 20 20 20  _timeout        
1bb73 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bb74 62 75 73 79 5f 74 69 6d 65 6f 75 74 0a 23 64 65  busy_timeout.#de
1bb75 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 68 61  fine sqlite3_cha
1bb76 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20 20  nges            
1bb77 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bb78 3e 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65  >changes.#define
1bb79 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 20 20   sqlite3_close  
1bb7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bb7b 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 6f  sqlite3_api->clo
1bb7c 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
1bb7d 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
1bb7e 64 65 64 20 20 20 20 20 20 20 73 71 6c 69 74 65  ded       sqlite
1bb7f 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e  3_api->collation
1bb80 5f 6e 65 65 64 65 64 0a 23 64 65 66 69 6e 65 20  _needed.#define 
1bb81 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
1bb82 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 73  n_needed16     s
1bb83 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c  qlite3_api->coll
1bb84 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 0a 23  ation_needed16.#
1bb85 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1bb86 6f 6c 75 6d 6e 5f 62 6c 6f 62 20 20 20 20 20 20  olumn_blob      
1bb87 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1bb88 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 0a 23  i->column_blob.#
1bb89 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1bb8a 6f 6c 75 6d 6e 5f 62 79 74 65 73 20 20 20 20 20  olumn_bytes     
1bb8b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1bb8c 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 0a  i->column_bytes.
1bb8d 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1bb8e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 20 20  column_bytes16  
1bb8f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1bb90 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  pi->column_bytes
1bb91 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
1bb92 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 20  e3_column_count 
1bb93 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1bb94 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 63 6f  3_api->column_co
1bb95 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
1bb96 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
1bb97 61 73 65 5f 6e 61 6d 65 20 20 20 73 71 6c 69 74  ase_name   sqlit
1bb98 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
1bb99 61 74 61 62 61 73 65 5f 6e 61 6d 65 0a 23 64 65  atabase_name.#de
1bb9a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
1bb9b 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
1bb9c 65 31 36 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  e16 sqlite3_api-
1bb9d 3e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  >column_database
1bb9e 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20  _name16.#define 
1bb9f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
1bba0 65 63 6c 74 79 70 65 20 20 20 20 20 20 20 20 73  ecltype        s
1bba1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
1bba2 6d 6e 5f 64 65 63 6c 74 79 70 65 0a 23 64 65 66  mn_decltype.#def
1bba3 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1bba4 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20  mn_decltype16   
1bba5 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bba6 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
1bba7 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
1bba8 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20  3_column_double 
1bba9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1bbaa 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 6f 75  _api->column_dou
1bbab 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
1bbac 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 20  te3_column_int  
1bbad 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bbae 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69  e3_api->column_i
1bbaf 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
1bbb0 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 20  e3_column_int64 
1bbb1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1bbb2 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e  3_api->column_in
1bbb3 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t64.#define sqli
1bbb4 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20  te3_column_name 
1bbb5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bbb6 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e  e3_api->column_n
1bbb7 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
1bbb8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
1bbb9 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
1bbba 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e  e3_api->column_n
1bbbb 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
1bbbc 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
1bbbd 67 69 6e 5f 6e 61 6d 65 20 20 20 20 20 73 71 6c  gin_name     sql
1bbbe 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
1bbbf 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 0a 23 64 65  _origin_name.#de
1bbc0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
1bbc1 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
1bbc2 36 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  6   sqlite3_api-
1bbc3 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
1bbc4 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
1bbc5 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
1bbc6 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 73 71 6c  le_name      sql
1bbc7 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
1bbc8 5f 74 61 62 6c 65 5f 6e 61 6d 65 0a 23 64 65 66  _table_name.#def
1bbc9 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1bbca 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20  mn_table_name16 
1bbcb 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bbcc 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
1bbcd 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  e16.#define sqli
1bbce 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 20  te3_column_text 
1bbcf 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bbd0 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
1bbd1 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
1bbd2 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
1bbd3 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
1bbd4 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
1bbd5 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ext16.#define sq
1bbd6 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
1bbd7 65 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  e            sql
1bbd8 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
1bbd9 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71  _type.#define sq
1bbda 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
1bbdb 75 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  ue           sql
1bbdc 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
1bbdd 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73  _value.#define s
1bbde 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
1bbdf 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ok            sq
1bbe0 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 6d 69  lite3_api->commi
1bbe1 74 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73  t_hook.#define s
1bbe2 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20  qlite3_complete 
1bbe3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1bbe4 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c  lite3_api->compl
1bbe5 65 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ete.#define sqli
1bbe6 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20  te3_complete16  
1bbe7 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bbe8 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65 74 65  e3_api->complete
1bbe9 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
1bbea 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
1bbeb 69 6f 6e 20 20 20 20 20 20 20 73 71 6c 69 74 65  ion       sqlite
1bbec 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f  3_api->create_co
1bbed 6c 6c 61 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20  llation.#define 
1bbee 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
1bbef 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20 73  ollation16     s
1bbf0 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
1bbf1 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 0a 23  te_collation16.#
1bbf2 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1bbf3 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 20  reate_function  
1bbf4 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1bbf5 69 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63 74 69  i->create_functi
1bbf6 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  on.#define sqlit
1bbf7 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
1bbf8 6f 6e 31 36 20 20 20 20 20 20 73 71 6c 69 74 65  on16      sqlite
1bbf9 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75  3_api->create_fu
1bbfa 6e 63 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65  nction16.#define
1bbfb 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
1bbfc 6d 6f 64 75 6c 65 20 20 20 20 20 20 20 20 20 20  module          
1bbfd 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65  sqlite3_api->cre
1bbfe 61 74 65 5f 6d 6f 64 75 6c 65 0a 23 64 65 66 69  ate_module.#defi
1bbff 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
1bc00 65 5f 6d 6f 64 75 6c 65 5f 76 32 20 20 20 20 20  e_module_v2     
1bc01 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
1bc02 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 0a  reate_module_v2.
1bc03 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1bc04 64 61 74 61 5f 63 6f 75 6e 74 20 20 20 20 20 20  data_count      
1bc05 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1bc06 70 69 2d 3e 64 61 74 61 5f 63 6f 75 6e 74 0a 23  pi->data_count.#
1bc07 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
1bc08 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20  b_handle        
1bc09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1bc0a 69 2d 3e 64 62 5f 68 61 6e 64 6c 65 0a 23 64 65  i->db_handle.#de
1bc0b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63  fine sqlite3_dec
1bc0c 6c 61 72 65 5f 76 74 61 62 20 20 20 20 20 20 20  lare_vtab       
1bc0d 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bc0e 3e 64 65 63 6c 61 72 65 5f 76 74 61 62 0a 23 64  >declare_vtab.#d
1bc0f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e  efine sqlite3_en
1bc10 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
1bc11 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  e    sqlite3_api
1bc12 2d 3e 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  ->enable_shared_
1bc13 63 61 63 68 65 0a 23 64 65 66 69 6e 65 20 73 71  cache.#define sq
1bc14 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 20 20 20  lite3_errcode   
1bc15 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1bc16 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 63 6f 64  ite3_api->errcod
1bc17 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
1bc18 33 5f 65 72 72 6d 73 67 20 20 20 20 20 20 20 20  3_errmsg        
1bc19 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1bc1a 5f 61 70 69 2d 3e 65 72 72 6d 73 67 0a 23 64 65  _api->errmsg.#de
1bc1b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
1bc1c 6d 73 67 31 36 20 20 20 20 20 20 20 20 20 20 20  msg16           
1bc1d 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bc1e 3e 65 72 72 6d 73 67 31 36 0a 23 64 65 66 69 6e  >errmsg16.#defin
1bc1f 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 20  e sqlite3_exec  
1bc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bc21 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78   sqlite3_api->ex
1bc22 65 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ec.#define sqlit
1bc23 65 33 5f 65 78 70 69 72 65 64 20 20 20 20 20 20  e3_expired      
1bc24 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1bc25 33 5f 61 70 69 2d 3e 65 78 70 69 72 65 64 0a 23  3_api->expired.#
1bc26 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66  define sqlite3_f
1bc27 69 6e 61 6c 69 7a 65 20 20 20 20 20 20 20 20 20  inalize         
1bc28 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1bc29 69 2d 3e 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66  i->finalize.#def
1bc2a 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ine sqlite3_free
1bc2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bc2c 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bc2d 66 72 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  free.#define sql
1bc2e 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20  ite3_free_table 
1bc2f 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1bc30 74 65 33 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61  te3_api->free_ta
1bc31 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
1bc32 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d  te3_get_autocomm
1bc33 69 74 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  it         sqlit
1bc34 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f  e3_api->get_auto
1bc35 63 6f 6d 6d 69 74 0a 23 64 65 66 69 6e 65 20 73  commit.#define s
1bc36 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
1bc37 74 61 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ta            sq
1bc38 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61  lite3_api->get_a
1bc39 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  uxdata.#define s
1bc3a 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
1bc3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1bc3c 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74  lite3_api->get_t
1bc3d 61 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  able.#define sql
1bc3e 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f  ite3_global_reco
1bc3f 76 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69  ver         sqli
1bc40 74 65 33 5f 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f  te3_api->global_
1bc41 72 65 63 6f 76 65 72 0a 23 64 65 66 69 6e 65 20  recover.#define 
1bc42 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
1bc43 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  t              s
1bc44 71 6c 69 74 65 33 5f 61 70 69 2d 3e 69 6e 74 65  qlite3_api->inte
1bc45 72 72 75 70 74 78 0a 23 64 65 66 69 6e 65 20 73  rruptx.#define s
1bc46 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
1bc47 72 74 5f 72 6f 77 69 64 20 20 20 20 20 20 73 71  rt_rowid      sq
1bc48 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 61 73 74 5f  lite3_api->last_
1bc49 69 6e 73 65 72 74 5f 72 6f 77 69 64 0a 23 64 65  insert_rowid.#de
1bc4a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 62  fine sqlite3_lib
1bc4b 76 65 72 73 69 6f 6e 20 20 20 20 20 20 20 20 20  version         
1bc4c 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bc4d 3e 6c 69 62 76 65 72 73 69 6f 6e 0a 23 64 65 66  >libversion.#def
1bc4e 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  ine sqlite3_libv
1bc4f 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 20 20 20  ersion_number   
1bc50 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bc51 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
1bc52 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
1bc53 33 5f 6d 61 6c 6c 6f 63 20 20 20 20 20 20 20 20  3_malloc        
1bc54 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1bc55 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63 0a 23 64 65  _api->malloc.#de
1bc56 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 70 72  fine sqlite3_mpr
1bc57 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20  intf            
1bc58 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bc59 3e 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65  >mprintf.#define
1bc5a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 20 20 20   sqlite3_open   
1bc5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bc5c 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65  sqlite3_api->ope
1bc5d 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  n.#define sqlite
1bc5e 33 5f 6f 70 65 6e 31 36 20 20 20 20 20 20 20 20  3_open16        
1bc5f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1bc60 5f 61 70 69 2d 3e 6f 70 65 6e 31 36 0a 23 64 65  _api->open16.#de
1bc61 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
1bc62 70 61 72 65 20 20 20 20 20 20 20 20 20 20 20 20  pare            
1bc63 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bc64 3e 70 72 65 70 61 72 65 0a 23 64 65 66 69 6e 65  >prepare.#define
1bc65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1bc66 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20  16              
1bc67 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
1bc68 70 61 72 65 31 36 0a 23 64 65 66 69 6e 65 20 73  pare16.#define s
1bc69 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
1bc6a 32 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  2             sq
1bc6b 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61  lite3_api->prepa
1bc6c 72 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71  re_v2.#define sq
1bc6d 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
1bc6e 76 32 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  v2           sql
1bc6f 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72  ite3_api->prepar
1bc70 65 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  e16_v2.#define s
1bc71 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20  qlite3_profile  
1bc72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1bc73 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 66 69  lite3_api->profi
1bc74 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
1bc75 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64  e3_progress_hand
1bc76 6c 65 72 20 20 20 20 20 20 20 73 71 6c 69 74 65  ler       sqlite
1bc77 33 5f 61 70 69 2d 3e 70 72 6f 67 72 65 73 73 5f  3_api->progress_
1bc78 68 61 6e 64 6c 65 72 0a 23 64 65 66 69 6e 65 20  handler.#define 
1bc79 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 20  sqlite3_realloc 
1bc7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
1bc7b 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 61 6c  qlite3_api->real
1bc7c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  loc.#define sqli
1bc7d 74 65 33 5f 72 65 73 65 74 20 20 20 20 20 20 20  te3_reset       
1bc7e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bc7f 65 33 5f 61 70 69 2d 3e 72 65 73 65 74 0a 23 64  e3_api->reset.#d
1bc80 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
1bc81 73 75 6c 74 5f 62 6c 6f 62 20 20 20 20 20 20 20  sult_blob       
1bc82 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1bc83 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f 62 0a 23 64  ->result_blob.#d
1bc84 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
1bc85 73 75 6c 74 5f 64 6f 75 62 6c 65 20 20 20 20 20  sult_double     
1bc86 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1bc87 2d 3e 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 0a  ->result_double.
1bc88 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1bc89 72 65 73 75 6c 74 5f 65 72 72 6f 72 20 20 20 20  result_error    
1bc8a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1bc8b 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72  pi->result_error
1bc8c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1bc8d 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 20  _result_error16 
1bc8e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1bc8f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f  api->result_erro
1bc90 72 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  r16.#define sqli
1bc91 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 20  te3_result_int  
1bc92 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bc93 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69  e3_api->result_i
1bc94 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
1bc95 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 20  e3_result_int64 
1bc96 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1bc97 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e  3_api->result_in
1bc98 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t64.#define sqli
1bc99 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
1bc9a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bc9b 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 6e  e3_api->result_n
1bc9c 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ull.#define sqli
1bc9d 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 20  te3_result_text 
1bc9e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bc9f 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74  e3_api->result_t
1bca0 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
1bca1 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
1bca2 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
1bca3 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74  e3_api->result_t
1bca4 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ext16.#define sq
1bca5 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1bca6 74 31 36 62 65 20 20 20 20 20 20 20 20 73 71 6c  t16be        sql
1bca7 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
1bca8 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e  _text16be.#defin
1bca9 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
1bcaa 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20  _text16le       
1bcab 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
1bcac 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 0a 23 64  sult_text16le.#d
1bcad 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
1bcae 73 75 6c 74 5f 76 61 6c 75 65 20 20 20 20 20 20  sult_value      
1bcaf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1bcb0 2d 3e 72 65 73 75 6c 74 5f 76 61 6c 75 65 0a 23  ->result_value.#
1bcb1 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
1bcb2 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 20 20 20 20  ollback_hook    
1bcb3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1bcb4 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  i->rollback_hook
1bcb5 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1bcb6 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20  _set_authorizer 
1bcb7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1bcb8 61 70 69 2d 3e 73 65 74 5f 61 75 74 68 6f 72 69  api->set_authori
1bcb9 7a 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  zer.#define sqli
1bcba 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 20  te3_set_auxdata 
1bcbb 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bcbc 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 78 64  e3_api->set_auxd
1bcbd 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
1bcbe 74 65 33 5f 73 6e 70 72 69 6e 74 66 20 20 20 20  te3_snprintf    
1bcbf 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bcc0 65 33 5f 61 70 69 2d 3e 73 6e 70 72 69 6e 74 66  e3_api->snprintf
1bcc1 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1bcc2 5f 73 74 65 70 20 20 20 20 20 20 20 20 20 20 20  _step           
1bcc3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1bcc4 61 70 69 2d 3e 73 74 65 70 0a 23 64 65 66 69 6e  api->step.#defin
1bcc5 65 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f  e sqlite3_table_
1bcc6 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20  column_metadata 
1bcc7 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 61   sqlite3_api->ta
1bcc8 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64  ble_column_metad
1bcc9 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
1bcca 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e  te3_thread_clean
1bccb 75 70 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  up         sqlit
1bccc 65 33 5f 61 70 69 2d 3e 74 68 72 65 61 64 5f 63  e3_api->thread_c
1bccd 6c 65 61 6e 75 70 0a 23 64 65 66 69 6e 65 20 73  leanup.#define s
1bcce 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
1bccf 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 73 71  nges          sq
1bcd0 6c 69 74 65 33 5f 61 70 69 2d 3e 74 6f 74 61 6c  lite3_api->total
1bcd1 5f 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65  _changes.#define
1bcd2 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20   sqlite3_trace  
1bcd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bcd4 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 72 61  sqlite3_api->tra
1bcd5 63 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ce.#define sqlit
1bcd6 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64  e3_transfer_bind
1bcd7 69 6e 67 73 20 20 20 20 20 20 73 71 6c 69 74 65  ings      sqlite
1bcd8 33 5f 61 70 69 2d 3e 74 72 61 6e 73 66 65 72 5f  3_api->transfer_
1bcd9 62 69 6e 64 69 6e 67 73 0a 23 64 65 66 69 6e 65  bindings.#define
1bcda 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f   sqlite3_update_
1bcdb 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20  hook            
1bcdc 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75 70 64  sqlite3_api->upd
1bcdd 61 74 65 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65  ate_hook.#define
1bcde 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
1bcdf 74 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ta              
1bce0 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75 73 65  sqlite3_api->use
1bce1 72 5f 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  r_data.#define s
1bce2 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
1bce3 62 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  b             sq
1bce4 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
1bce5 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71  _blob.#define sq
1bce6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1bce7 73 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  s            sql
1bce8 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
1bce9 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73 71  bytes.#define sq
1bcea 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1bceb 73 31 36 20 20 20 20 20 20 20 20 20 20 73 71 6c  s16          sql
1bcec 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
1bced 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e 65 20  bytes16.#define 
1bcee 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
1bcef 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 73  uble           s
1bcf0 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
1bcf1 65 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65  e_double.#define
1bcf2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
1bcf3 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
1bcf4 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
1bcf5 75 65 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73  ue_int.#define s
1bcf6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
1bcf7 36 34 20 20 20 20 20 20 20 20 20 20 20 20 73 71  64            sq
1bcf8 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
1bcf9 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73  _int64.#define s
1bcfa 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d  qlite3_value_num
1bcfb 65 72 69 63 5f 74 79 70 65 20 20 20 20 20 73 71  eric_type     sq
1bcfc 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
1bcfd 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 0a 23 64  _numeric_type.#d
1bcfe 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
1bcff 6c 75 65 5f 74 65 78 74 20 20 20 20 20 20 20 20  lue_text        
1bd00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1bd01 2d 3e 76 61 6c 75 65 5f 74 65 78 74 0a 23 64 65  ->value_text.#de
1bd02 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
1bd03 75 65 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  ue_text16       
1bd04 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bd05 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 0a 23 64  >value_text16.#d
1bd06 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
1bd07 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20 20 20  lue_text16be    
1bd08 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1bd09 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65  ->value_text16be
1bd0a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1bd0b 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 20  _value_text16le 
1bd0c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1bd0d 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31  api->value_text1
1bd0e 36 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  6le.#define sqli
1bd0f 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 20  te3_value_type  
1bd10 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bd11 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 79  e3_api->value_ty
1bd12 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  pe.#define sqlit
1bd13 65 33 5f 76 6d 70 72 69 6e 74 66 20 20 20 20 20  e3_vmprintf     
1bd14 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1bd15 33 5f 61 70 69 2d 3e 76 6d 70 72 69 6e 74 66 0a  3_api->vmprintf.
1bd16 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1bd17 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f  overload_functio
1bd18 6e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  n      sqlite3_a
1bd19 70 69 2d 3e 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  pi->overload_fun
1bd1a 63 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71  ction.#define sq
1bd1b 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1bd1c 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1bd1d 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72  ite3_api->prepar
1bd1e 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  e_v2.#define sql
1bd1f 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
1bd20 32 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  2           sqli
1bd21 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
1bd22 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71  16_v2.#define sq
1bd23 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
1bd24 69 6e 67 73 20 20 20 20 20 20 20 20 20 73 71 6c  ings         sql
1bd25 69 74 65 33 5f 61 70 69 2d 3e 63 6c 65 61 72 5f  ite3_api->clear_
1bd26 62 69 6e 64 69 6e 67 73 0a 23 64 65 66 69 6e 65  bindings.#define
1bd27 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65   sqlite3_bind_ze
1bd28 72 6f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20  roblob          
1bd29 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e  sqlite3_api->bin
1bd2a 64 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69  d_zeroblob.#defi
1bd2b 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
1bd2c 62 79 74 65 73 20 20 20 20 20 20 20 20 20 20 20  bytes           
1bd2d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
1bd2e 6c 6f 62 5f 62 79 74 65 73 0a 23 64 65 66 69 6e  lob_bytes.#defin
1bd2f 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63  e sqlite3_blob_c
1bd30 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 20  lose            
1bd31 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c   sqlite3_api->bl
1bd32 6f 62 5f 63 6c 6f 73 65 0a 23 64 65 66 69 6e 65  ob_close.#define
1bd33 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70   sqlite3_blob_op
1bd34 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  en              
1bd35 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f  sqlite3_api->blo
1bd36 62 5f 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73  b_open.#define s
1bd37 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64  qlite3_blob_read
1bd38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1bd39 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f  lite3_api->blob_
1bd3a 72 65 61 64 0a 23 64 65 66 69 6e 65 20 73 71 6c  read.#define sql
1bd3b 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 20  ite3_blob_write 
1bd3c 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1bd3d 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 77 72  te3_api->blob_wr
1bd3e 69 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ite.#define sqli
1bd3f 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
1bd40 74 69 6f 6e 5f 76 32 20 20 20 20 73 71 6c 69 74  tion_v2    sqlit
1bd41 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63  e3_api->create_c
1bd42 6f 6c 6c 61 74 69 6f 6e 5f 76 32 0a 23 64 65 66  ollation_v2.#def
1bd43 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ine sqlite3_file
1bd44 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20 20 20 20  _control        
1bd45 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1bd46 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 0a 23 64 65  file_control.#de
1bd47 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d  fine sqlite3_mem
1bd48 6f 72 79 5f 68 69 67 68 77 61 74 65 72 20 20 20  ory_highwater   
1bd49 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bd4a 3e 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65  >memory_highwate
1bd4b 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
1bd4c 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 20 20 20  3_memory_used   
1bd4d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1bd4e 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f 75 73 65  _api->memory_use
1bd4f 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  d.#define sqlite
1bd50 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 20 20 20  3_mutex_alloc   
1bd51 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1bd52 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 61 6c 6c 6f  _api->mutex_allo
1bd53 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  c.#define sqlite
1bd54 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 20 20 20  3_mutex_enter   
1bd55 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1bd56 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 65 6e 74 65  _api->mutex_ente
1bd57 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
1bd58 33 5f 6d 75 74 65 78 5f 66 72 65 65 20 20 20 20  3_mutex_free    
1bd59 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1bd5a 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 66 72 65 65  _api->mutex_free
1bd5b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1bd5c 5f 6d 75 74 65 78 5f 6c 65 61 76 65 20 20 20 20  _mutex_leave    
1bd5d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1bd5e 61 70 69 2d 3e 6d 75 74 65 78 5f 6c 65 61 76 65  api->mutex_leave
1bd5f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1bd60 5f 6d 75 74 65 78 5f 74 72 79 20 20 20 20 20 20  _mutex_try      
1bd61 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1bd62 61 70 69 2d 3e 6d 75 74 65 78 5f 74 72 79 0a 23  api->mutex_try.#
1bd63 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f  define sqlite3_o
1bd64 70 65 6e 5f 76 32 20 20 20 20 20 20 20 20 20 20  pen_v2          
1bd65 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1bd66 69 2d 3e 6f 70 65 6e 5f 76 32 0a 23 64 65 66 69  i->open_v2.#defi
1bd67 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  ne sqlite3_relea
1bd68 73 65 5f 6d 65 6d 6f 72 79 20 20 20 20 20 20 20  se_memory       
1bd69 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
1bd6a 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 0a 23 64  elease_memory.#d
1bd6b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
1bd6c 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
1bd6d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1bd6e 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  ->result_error_n
1bd6f 6f 6d 65 6d 0a 23 64 65 66 69 6e 65 20 73 71 6c  omem.#define sql
1bd70 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
1bd71 72 5f 74 6f 6f 62 69 67 20 20 20 20 73 71 6c 69  r_toobig    sqli
1bd72 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
1bd73 65 72 72 6f 72 5f 74 6f 6f 62 69 67 0a 23 64 65  error_toobig.#de
1bd74 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 6c 65  fine sqlite3_sle
1bd75 65 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ep              
1bd76 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1bd77 3e 73 6c 65 65 70 0a 23 64 65 66 69 6e 65 20 73  >sleep.#define s
1bd78 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
1bd79 5f 6c 69 6d 69 74 20 20 20 20 20 20 20 20 73 71  _limit        sq
1bd7a 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6f 66 74 5f  lite3_api->soft_
1bd7b 68 65 61 70 5f 6c 69 6d 69 74 0a 23 64 65 66 69  heap_limit.#defi
1bd7c 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66  ne sqlite3_vfs_f
1bd7d 69 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  ind             
1bd7e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
1bd7f 66 73 5f 66 69 6e 64 0a 23 64 65 66 69 6e 65 20  fs_find.#define 
1bd80 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
1bd81 73 74 65 72 20 20 20 20 20 20 20 20 20 20 20 73  ster           s
1bd82 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 66 73 5f  qlite3_api->vfs_
1bd83 72 65 67 69 73 74 65 72 0a 23 64 65 66 69 6e 65  register.#define
1bd84 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
1bd85 65 67 69 73 74 65 72 20 20 20 20 20 20 20 20 20  egister         
1bd86 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 66 73  sqlite3_api->vfs
1bd87 5f 75 6e 72 65 67 69 73 74 65 72 0a 23 65 6e 64  _unregister.#end
1bd88 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52  if /* SQLITE_COR
1bd89 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51  E */..#define SQ
1bd8a 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49  LITE_EXTENSION_I
1bd8b 4e 49 54 31 20 20 20 20 20 63 6f 6e 73 74 20 73  NIT1     const s
1bd8c 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
1bd8d 6e 65 73 20 2a 73 71 6c 69 74 65 33 5f 61 70 69  nes *sqlite3_api
1bd8e 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ;.#define SQLITE
1bd8f 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32  _EXTENSION_INIT2
1bd90 28 76 29 20 20 73 71 6c 69 74 65 33 5f 61 70 69  (v)  sqlite3_api
1bd91 20 3d 20 76 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a   = v;..#endif /*
1bd92 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 20   _SQLITE3EXT_H_ 
1bd93 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
1bd94 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
1bd95 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  e3ext.h ********
1bd96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd98 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1bd99 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
1bd9a 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
1bd9b 20 69 6e 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a   in loadext.c **
1bd9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd9d 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20  **/../*.** Some 
1bd9e 41 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65  API routines are
1bd9f 20 6f 6d 69 74 74 65 64 20 77 68 65 6e 20 76 61   omitted when va
1bda0 72 69 6f 75 73 20 66 65 61 74 75 72 65 73 20 61  rious features a
1bda1 72 65 0a 2a 2a 20 65 78 63 6c 75 64 65 64 20 66  re.** excluded f
1bda2 72 6f 6d 20 61 20 62 75 69 6c 64 20 6f 66 20 53  rom a build of S
1bda3 51 4c 69 74 65 2e 20 20 53 75 62 73 74 69 74 75  QLite.  Substitu
1bda4 74 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  te a NULL pointe
1bda5 72 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 6d 69 73  r.** for any mis
1bda6 73 69 6e 67 20 41 50 49 73 2e 0a 2a 2f 0a 23 69  sing APIs..*/.#i
1bda7 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  fndef SQLITE_ENA
1bda8 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44  BLE_COLUMN_METAD
1bda9 41 54 41 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ATA.# define sql
1bdaa 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
1bdab 62 61 73 65 5f 6e 61 6d 65 20 20 20 30 0a 23 20  base_name   0.# 
1bdac 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1bdad 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
1bdae 61 6d 65 31 36 20 30 0a 23 20 64 65 66 69 6e 65  ame16 0.# define
1bdaf 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1bdb0 74 61 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20  table_name      
1bdb1 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1bdb2 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
1bdb3 6e 61 6d 65 31 36 20 20 20 20 30 0a 23 20 64 65  name16    0.# de
1bdb4 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
1bdb5 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20  umn_origin_name 
1bdb6 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
1bdb7 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
1bdb8 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a  igin_name16   0.
1bdb9 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1bdba 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  _table_column_me
1bdbb 74 61 64 61 74 61 20 20 30 0a 23 65 6e 64 69 66  tadata  0.#endif
1bdbc 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1bdbd 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
1bdbe 4f 4e 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  ON.# define sqli
1bdbf 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
1bdc0 65 72 20 20 20 20 20 20 20 20 20 30 0a 23 65 6e  er         0.#en
1bdc1 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
1bdc2 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 23 20  TE_OMIT_UTF16.# 
1bdc3 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
1bdc4 69 6e 64 5f 74 65 78 74 31 36 20 20 20 20 20 20  ind_text16      
1bdc5 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1bdc6 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
1bdc7 6f 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20  on_needed16     
1bdc8 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1bdc9 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
1bdca 70 65 31 36 20 20 20 20 20 20 30 0a 23 20 64 65  pe16      0.# de
1bdcb 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
1bdcc 75 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20  umn_name16      
1bdcd 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
1bdce 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1bdcf 78 74 31 36 20 20 20 20 20 20 20 20 20 20 30 0a  xt16          0.
1bdd0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1bdd1 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20 20 20 20  _complete16     
1bdd2 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
1bdd3 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
1bdd4 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20  e_collation16   
1bdd5 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
1bdd6 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
1bdd7 74 69 6f 6e 31 36 20 20 20 20 20 20 30 0a 23 20  tion16      0.# 
1bdd8 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65  define sqlite3_e
1bdd9 72 72 6d 73 67 31 36 20 20 20 20 20 20 20 20 20  rrmsg16         
1bdda 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1bddb 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20   sqlite3_open16 
1bddc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bddd 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1bdde 65 33 5f 70 72 65 70 61 72 65 31 36 20 20 20 20  e3_prepare16    
1bddf 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
1bde0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
1bde1 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20 20  pare16_v2       
1bde2 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
1bde3 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1bde4 72 6f 72 31 36 20 20 20 20 20 20 20 20 20 30 0a  ror16         0.
1bde5 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1bde6 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20  _result_text16  
1bde7 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
1bde8 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
1bde9 74 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20  t_text16be      
1bdea 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
1bdeb 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
1bdec 31 36 6c 65 20 20 20 20 20 20 20 20 30 0a 23 20  16le        0.# 
1bded 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
1bdee 61 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20 20  alue_text16     
1bdef 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1bdf0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1bdf1 65 78 74 31 36 62 65 20 20 20 20 20 20 20 20 20  ext16be         
1bdf2 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1bdf3 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c  e3_value_text16l
1bdf4 65 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65  e         0.# de
1bdf5 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
1bdf6 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
1bdf7 65 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20 73  e16 0.# define s
1bdf8 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
1bdf9 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a  ble_name16    0.
1bdfa 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1bdfb 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
1bdfc 61 6d 65 31 36 20 20 20 30 0a 23 65 6e 64 69 66  ame16   0.#endif
1bdfd 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1bdfe 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 23 20  OMIT_COMPLETE.# 
1bdff 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1be00 6f 6d 70 6c 65 74 65 20 30 0a 23 20 64 65 66 69  omplete 0.# defi
1be01 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
1be02 65 74 65 31 36 20 30 0a 23 65 6e 64 69 66 0a 0a  ete16 0.#endif..
1be03 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
1be04 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c  IT_PROGRESS_CALL
1be05 42 41 43 4b 0a 23 20 64 65 66 69 6e 65 20 73 71  BACK.# define sq
1be06 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
1be07 61 6e 64 6c 65 72 20 30 0a 23 65 6e 64 69 66 0a  andler 0.#endif.
1be08 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
1be09 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1be0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
1be0b 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20  3_create_module 
1be0c 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1be0d 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
1be0e 5f 76 32 20 30 0a 23 20 64 65 66 69 6e 65 20 73  _v2 0.# define s
1be0f 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
1be10 74 61 62 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69  tab 0.#endif..#i
1be11 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1be12 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 23 20  _SHARED_CACHE.# 
1be13 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65  define sqlite3_e
1be14 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63  nable_shared_cac
1be15 68 65 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66  he 0.#endif..#if
1be16 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1be17 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 73  TRACE.# define s
1be18 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20  qlite3_profile  
1be19 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
1be1a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20 20  sqlite3_trace   
1be1b 20 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a        0.#endif..
1be1c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
1be1d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a 23 20 64  IT_GET_TABLE.# d
1be1e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72  efine sqlite3_fr
1be1f 65 65 5f 74 61 62 6c 65 20 20 20 20 30 0a 23 20  ee_table    0.# 
1be20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67  define sqlite3_g
1be21 65 74 5f 74 61 62 6c 65 20 20 20 20 20 30 0a 23  et_table     0.#
1be22 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
1be23 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
1be24 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 70 6f  ture contains po
1be25 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 53 51  inters to all SQ
1be26 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e 65  Lite API routine
1be27 73 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  s..** A pointer 
1be28 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
1be29 65 20 69 73 20 70 61 73 73 65 64 20 69 6e 74 6f  e is passed into
1be2a 20 65 78 74 65 6e 73 69 6f 6e 73 20 77 68 65 6e   extensions when
1be2b 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 6c 6f 61   they are.** loa
1be2c 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ded so that the 
1be2d 65 78 74 65 6e 73 69 6f 6e 20 63 61 6e 20 6d 61  extension can ma
1be2e 6b 65 20 63 61 6c 6c 73 20 62 61 63 6b 20 69 6e  ke calls back in
1be2f 74 6f 20 74 68 65 20 53 51 4c 69 74 65 0a 2a 2a  to the SQLite.**
1be30 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20   library..**.** 
1be31 57 68 65 6e 20 61 64 64 69 6e 67 20 6e 65 77 20  When adding new 
1be32 41 50 49 73 2c 20 61 64 64 20 74 68 65 6d 20 74  APIs, add them t
1be33 6f 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  o the bottom of 
1be34 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  this structure.*
1be35 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72  * in order to pr
1be36 65 73 65 72 76 65 20 62 61 63 6b 77 61 72 64 73  eserve backwards
1be37 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
1be38 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 73  **.** Extensions
1be39 20 74 68 61 74 20 75 73 65 20 6e 65 77 65 72 20   that use newer 
1be3a 41 50 49 73 20 73 68 6f 75 6c 64 20 66 69 72 73  APIs should firs
1be3b 74 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71  t call the.** sq
1be3c 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
1be3d 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b  _number() to mak
1be3e 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
1be3f 41 50 49 20 74 68 65 79 0a 2a 2a 20 69 6e 74 65  API they.** inte
1be40 6e 64 20 74 6f 20 75 73 65 20 69 73 20 73 75 70  nd to use is sup
1be41 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 6c 69  ported by the li
1be42 62 72 61 72 79 2e 20 20 45 78 74 65 6e 73 69 6f  brary.  Extensio
1be43 6e 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 73  ns should.** als
1be44 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  o check to make 
1be45 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70 6f  sure that the po
1be46 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e  inter to the fun
1be47 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20  ction is.** not 
1be48 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61 6c 6c  NULL before call
1be49 69 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54  ing it..*/.SQLIT
1be4a 45 5f 41 50 49 20 63 6f 6e 73 74 20 73 71 6c 69  E_API const sqli
1be4b 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
1be4c 20 73 71 6c 69 74 65 33 5f 61 70 69 73 20 3d 20   sqlite3_apis = 
1be4d 7b 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72  {.  sqlite3_aggr
1be4e 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 2c 0a 20  egate_context,. 
1be4f 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
1be50 74 65 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69  te_count,.  sqli
1be51 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 2c 0a 20  te3_bind_blob,. 
1be52 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f   sqlite3_bind_do
1be53 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  uble,.  sqlite3_
1be54 62 69 6e 64 5f 69 6e 74 2c 0a 20 20 73 71 6c 69  bind_int,.  sqli
1be55 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 2c 0a  te3_bind_int64,.
1be56 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e    sqlite3_bind_n
1be57 75 6c 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ull,.  sqlite3_b
1be58 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
1be59 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  unt,.  sqlite3_b
1be5a 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
1be5b 64 65 78 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  dex,.  sqlite3_b
1be5c 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
1be5d 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  me,.  sqlite3_bi
1be5e 6e 64 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74  nd_text,.  sqlit
1be5f 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 2c 0a  e3_bind_text16,.
1be60 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76    sqlite3_bind_v
1be61 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  alue,.  sqlite3_
1be62 62 75 73 79 5f 68 61 6e 64 6c 65 72 2c 0a 20 20  busy_handler,.  
1be63 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
1be64 65 6f 75 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  eout,.  sqlite3_
1be65 63 68 61 6e 67 65 73 2c 0a 20 20 73 71 6c 69 74  changes,.  sqlit
1be66 65 33 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69  e3_close,.  sqli
1be67 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
1be68 65 64 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  eded,.  sqlite3_
1be69 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
1be6a 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
1be6b 6c 75 6d 6e 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c  lumn_blob,.  sql
1be6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
1be6d 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  s,.  sqlite3_col
1be6e 75 6d 6e 5f 62 79 74 65 73 31 36 2c 0a 20 20 73  umn_bytes16,.  s
1be6f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
1be70 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  unt,.  sqlite3_c
1be71 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
1be72 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ame,.  sqlite3_c
1be73 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
1be74 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ame16,.  sqlite3
1be75 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
1be76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
1be77 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 2c 0a 20  mn_decltype16,. 
1be78 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1be79 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65  double,.  sqlite
1be7a 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 2c 0a 20 20  3_column_int,.  
1be7b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
1be7c 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f  nt64,.  sqlite3_
1be7d 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 2c 0a 20 20 73  column_name,.  s
1be7e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
1be7f 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  me16,.  sqlite3_
1be80 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
1be81 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  me,.  sqlite3_co
1be82 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
1be83 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
1be84 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c  lumn_table_name,
1be85 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
1be86 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 2c 0a  n_table_name16,.
1be87 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1be88 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33  _text,.  sqlite3
1be89 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 2c 0a  _column_text16,.
1be8a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1be8b 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  _type,.  sqlite3
1be8c 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 2c 0a 20  _column_value,. 
1be8d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f   sqlite3_commit_
1be8e 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f  hook,.  sqlite3_
1be8f 63 6f 6d 70 6c 65 74 65 2c 0a 20 20 73 71 6c 69  complete,.  sqli
1be90 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 2c 0a  te3_complete16,.
1be91 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
1be92 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 0a 20 20 73 71  _collation,.  sq
1be93 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
1be94 6c 61 74 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69  lation16,.  sqli
1be95 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
1be96 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion,.  sqlite3_c
1be97 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
1be98 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ,.  sqlite3_crea
1be99 74 65 5f 6d 6f 64 75 6c 65 2c 0a 20 20 73 71 6c  te_module,.  sql
1be9a 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 2c  ite3_data_count,
1be9b 0a 20 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  .  sqlite3_db_ha
1be9c 6e 64 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ndle,.  sqlite3_
1be9d 64 65 63 6c 61 72 65 5f 76 74 61 62 2c 0a 20 20  declare_vtab,.  
1be9e 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73  sqlite3_enable_s
1be9f 68 61 72 65 64 5f 63 61 63 68 65 2c 0a 20 20 73  hared_cache,.  s
1bea0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 2c 0a  qlite3_errcode,.
1bea1 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67    sqlite3_errmsg
1bea2 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  ,.  sqlite3_errm
1bea3 73 67 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  sg16,.  sqlite3_
1bea4 65 78 65 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  exec,.  sqlite3_
1bea5 65 78 70 69 72 65 64 2c 0a 20 20 73 71 6c 69 74  expired,.  sqlit
1bea6 65 33 5f 66 69 6e 61 6c 69 7a 65 2c 0a 20 20 73  e3_finalize,.  s
1bea7 71 6c 69 74 65 33 5f 66 72 65 65 2c 0a 20 20 73  qlite3_free,.  s
1bea8 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
1bea9 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74  e,.  sqlite3_get
1beaa 5f 61 75 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20 73  _autocommit,.  s
1beab 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
1beac 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65  ta,.  sqlite3_ge
1bead 74 5f 74 61 62 6c 65 2c 0a 20 20 30 2c 20 20 20  t_table,.  0,   
1beae 20 20 2f 2a 20 57 61 73 20 73 71 6c 69 74 65 33    /* Was sqlite3
1beaf 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28  _global_recover(
1beb0 29 2c 20 62 75 74 20 74 68 61 74 20 66 75 6e 63  ), but that func
1beb1 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74  tion is deprecat
1beb2 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
1beb3 69 6e 74 65 72 72 75 70 74 2c 0a 20 20 73 71 6c  interrupt,.  sql
1beb4 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
1beb5 5f 72 6f 77 69 64 2c 0a 20 20 73 71 6c 69 74 65  _rowid,.  sqlite
1beb6 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c 0a 20 20  3_libversion,.  
1beb7 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
1beb8 6f 6e 5f 6e 75 6d 62 65 72 2c 0a 20 20 73 71 6c  on_number,.  sql
1beb9 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20 73  ite3_malloc,.  s
1beba 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 2c 0a  qlite3_mprintf,.
1bebb 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 2c 0a    sqlite3_open,.
1bebc 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36    sqlite3_open16
1bebd 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ,.  sqlite3_prep
1bebe 61 72 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70  are,.  sqlite3_p
1bebf 72 65 70 61 72 65 31 36 2c 0a 20 20 73 71 6c 69  repare16,.  sqli
1bec0 74 65 33 5f 70 72 6f 66 69 6c 65 2c 0a 20 20 73  te3_profile,.  s
1bec1 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f  qlite3_progress_
1bec2 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74  handler,.  sqlit
1bec3 65 33 5f 72 65 61 6c 6c 6f 63 2c 0a 20 20 73 71  e3_realloc,.  sq
1bec4 6c 69 74 65 33 5f 72 65 73 65 74 2c 0a 20 20 73  lite3_reset,.  s
1bec5 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
1bec6 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  ob,.  sqlite3_re
1bec7 73 75 6c 74 5f 64 6f 75 62 6c 65 2c 0a 20 20 73  sult_double,.  s
1bec8 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1bec9 72 6f 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  ror,.  sqlite3_r
1beca 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 2c 0a 20  esult_error16,. 
1becb 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1becc 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  int,.  sqlite3_r
1becd 65 73 75 6c 74 5f 69 6e 74 36 34 2c 0a 20 20 73  esult_int64,.  s
1bece 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
1becf 6c 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  ll,.  sqlite3_re
1bed0 73 75 6c 74 5f 74 65 78 74 2c 0a 20 20 73 71 6c  sult_text,.  sql
1bed1 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
1bed2 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  16,.  sqlite3_re
1bed3 73 75 6c 74 5f 74 65 78 74 31 36 62 65 2c 0a 20  sult_text16be,. 
1bed4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1bed5 74 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69  text16le,.  sqli
1bed6 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
1bed7 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c  ,.  sqlite3_roll
1bed8 62 61 63 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c  back_hook,.  sql
1bed9 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
1beda 7a 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73  zer,.  sqlite3_s
1bedb 65 74 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71  et_auxdata,.  sq
1bedc 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 2c 0a  lite3_snprintf,.
1bedd 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 2c 0a    sqlite3_step,.
1bede 20 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f    sqlite3_table_
1bedf 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 2c  column_metadata,
1bee0 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  .  sqlite3_threa
1bee1 64 5f 63 6c 65 61 6e 75 70 2c 0a 20 20 73 71 6c  d_cleanup,.  sql
1bee2 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
1bee3 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 72  es,.  sqlite3_tr
1bee4 61 63 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74  ace,.  sqlite3_t
1bee5 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73  ransfer_bindings
1bee6 2c 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64 61  ,.  sqlite3_upda
1bee7 74 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74  te_hook,.  sqlit
1bee8 65 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20 20  e3_user_data,.  
1bee9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
1beea 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ob,.  sqlite3_va
1beeb 6c 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c  lue_bytes,.  sql
1beec 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
1beed 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  16,.  sqlite3_va
1beee 6c 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71  lue_double,.  sq
1beef 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 2c  lite3_value_int,
1bef0 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1bef1 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65  _int64,.  sqlite
1bef2 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
1bef3 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  type,.  sqlite3_
1bef4 76 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73 71  value_text,.  sq
1bef5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1bef6 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  16,.  sqlite3_va
1bef7 6c 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20 20  lue_text16be,.  
1bef8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bef9 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65  xt16le,.  sqlite
1befa 33 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20 20  3_value_type,.  
1befb 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66  sqlite3_vmprintf
1befc 2c 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ,.  /*.  ** The 
1befd 6f 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65 74  original API set
1befe 20 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c 6c   ends here.  All
1beff 20 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e 20   extensions can 
1bf00 63 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f 66  call any.  ** of
1bf01 20 74 68 65 20 41 50 49 73 20 61 62 6f 76 65 20   the APIs above 
1bf02 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74 68  provided that th
1bf03 65 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74  e pointer is not
1bf04 20 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a 2a   NULL.  But.  **
1bf05 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
1bf06 41 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  APIs that follow
1bf07 2c 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f 75  , extension shou
1bf08 6c 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20 2a  ld check the.  *
1bf09 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  * sqlite3_libver
1bf0a 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f  sion_number() to
1bf0b 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20   make sure they 
1bf0c 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
1bf0d 0a 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79 20  .  ** a library 
1bf0e 74 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f 75  that is new enou
1bf0f 67 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68  gh to support th
1bf10 61 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a 2a  at API..  ******
1bf11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf15 2a 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ***.  */.  sqlit
1bf16 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
1bf17 74 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  tion,..  /*.  **
1bf18 20 41 64 64 65 64 20 61 66 74 65 72 20 33 2e 33   Added after 3.3
1bf19 2e 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  .13.  */.  sqlit
1bf1a 65 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a 20  e3_prepare_v2,. 
1bf1b 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1bf1c 31 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33  16_v2,.  sqlite3
1bf1d 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 2c  _clear_bindings,
1bf1e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
1bf1f 64 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a 2f  d for 3.4.1.  */
1bf20 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
1bf21 65 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20 20  e_module_v2,..  
1bf22 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f  /*.  ** Added fo
1bf23 72 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20 73  r 3.5.0.  */.  s
1bf24 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f  qlite3_bind_zero
1bf25 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f  blob,.  sqlite3_
1bf26 62 6c 6f 62 5f 62 79 74 65 73 2c 0a 20 20 73 71  blob_bytes,.  sq
1bf27 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
1bf28 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ,.  sqlite3_blob
1bf29 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33  _open,.  sqlite3
1bf2a 5f 62 6c 6f 62 5f 72 65 61 64 2c 0a 20 20 73 71  _blob_read,.  sq
1bf2b 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65  lite3_blob_write
1bf2c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ,.  sqlite3_crea
1bf2d 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 2c  te_collation_v2,
1bf2e 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  .  sqlite3_file_
1bf2f 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74  control,.  sqlit
1bf30 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
1bf31 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ter,.  sqlite3_m
1bf32 65 6d 6f 72 79 5f 75 73 65 64 2c 0a 23 69 66 64  emory_used,.#ifd
1bf33 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
1bf34 4e 4f 4f 50 0a 20 20 30 2c 20 0a 20 20 30 2c 20  NOOP.  0, .  0, 
1bf35 0a 20 20 30 2c 0a 20 20 30 2c 0a 20 20 30 2c 0a  .  0,.  0,.  0,.
1bf36 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 5f  #else.  sqlite3_
1bf37 6d 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20 73  mutex_alloc,.  s
1bf38 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1bf39 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  er,.  sqlite3_mu
1bf3a 74 65 78 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69  tex_free,.  sqli
1bf3b 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 2c  te3_mutex_leave,
1bf3c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1bf3d 5f 74 72 79 2c 0a 23 65 6e 64 69 66 0a 20 20 73  _try,.#endif.  s
1bf3e 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 2c 0a  qlite3_open_v2,.
1bf3f 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73    sqlite3_releas
1bf40 65 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73 71 6c 69  e_memory,.  sqli
1bf41 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
1bf42 5f 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c 69 74 65  _nomem,.  sqlite
1bf43 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
1bf44 6f 6f 62 69 67 2c 0a 20 20 73 71 6c 69 74 65 33  oobig,.  sqlite3
1bf45 5f 73 6c 65 65 70 2c 0a 20 20 73 71 6c 69 74 65  _sleep,.  sqlite
1bf46 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
1bf47 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  t,.  sqlite3_vfs
1bf48 5f 66 69 6e 64 2c 0a 20 20 73 71 6c 69 74 65 33  _find,.  sqlite3
1bf49 5f 76 66 73 5f 72 65 67 69 73 74 65 72 2c 0a 20  _vfs_register,. 
1bf4a 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
1bf4b 65 67 69 73 74 65 72 2c 0a 7d 3b 0a 0a 2f 2a 0a  egister,.};../*.
1bf4c 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f  ** Attempt to lo
1bf4d 61 64 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74  ad an SQLite ext
1bf4e 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63  ension library c
1bf4f 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
1bf50 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20 20  file.** zFile.  
1bf51 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20  The entry point 
1bf52 69 73 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f 63  is zProc.  zProc
1bf53 20 6d 61 79 20 62 65 20 30 20 69 6e 20 77 68 69   may be 0 in whi
1bf54 63 68 20 63 61 73 65 20 61 0a 2a 2a 20 64 65 66  ch case a.** def
1bf55 61 75 6c 74 20 65 6e 74 72 79 20 70 6f 69 6e 74  ault entry point
1bf56 20 6e 61 6d 65 20 28 73 71 6c 69 74 65 33 5f 65   name (sqlite3_e
1bf57 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 29 20 69  xtension_init) i
1bf58 73 20 75 73 65 64 2e 20 20 55 73 65 0a 2a 2a 20  s used.  Use.** 
1bf59 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e  of the default n
1bf5a 61 6d 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ame is recommend
1bf5b 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ed..**.** Return
1bf5c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
1bf5d 63 63 65 73 73 20 61 6e 64 20 53 51 4c 49 54 45  ccess and SQLITE
1bf5e 5f 45 52 52 4f 52 20 69 66 20 73 6f 6d 65 74 68  _ERROR if someth
1bf5f 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a  ing goes wrong..
1bf60 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
1bf61 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45  r occurs and pzE
1bf62 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20  rrMsg is not 0, 
1bf63 74 68 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72 72  then fill *pzErr
1bf64 4d 73 67 20 77 69 74 68 20 0a 2a 2a 20 65 72 72  Msg with .** err
1bf65 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 2e  or message text.
1bf66 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
1bf67 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72  nction should fr
1bf68 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a 2a  ee this memory.*
1bf69 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c  * by calling sql
1bf6a 69 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a  ite3_free()..*/.
1bf6b 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
1bf6c 65 33 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28  e3LoadExtension(
1bf6d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1bf6e 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64           /* Load
1bf6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69   the extension i
1bf70 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73  nto this databas
1bf71 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
1bf72 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
1bf73 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ile,    /* Name 
1bf74 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  of the shared li
1bf75 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67  brary containing
1bf76 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20   extension */.  
1bf77 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f  const char *zPro
1bf78 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70  c,    /* Entry p
1bf79 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69  oint.  Use "sqli
1bf7a 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e  te3_extension_in
1bf7b 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20 63 68  it" if 0 */.  ch
1bf7c 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
1bf7d 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72      /* Put error
1bf7e 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66   message here if
1bf7f 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 73   not 0 */.){.  s
1bf80 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1bf81 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 76   = db->pVfs;.  v
1bf82 6f 69 64 20 2a 68 61 6e 64 6c 65 3b 0a 20 20 69  oid *handle;.  i
1bf83 6e 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69  nt (*xInit)(sqli
1bf84 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73  te3*,char**,cons
1bf85 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
1bf86 75 74 69 6e 65 73 2a 29 3b 0a 20 20 63 68 61 72  utines*);.  char
1bf87 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a 20   *zErrmsg = 0;. 
1bf88 20 76 6f 69 64 20 2a 2a 61 48 61 6e 64 6c 65 3b   void **aHandle;
1bf89 0a 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31  ..  /* Ticket #1
1bf8a 38 36 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61  863.  To avoid a
1bf8b 20 63 72 65 61 74 69 6e 67 20 73 65 63 75 72 69   creating securi
1bf8c 74 79 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  ty problems for 
1bf8d 6f 6c 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69  older.  ** appli
1bf8e 63 61 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c  cations that rel
1bf8f 69 6e 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65  ink against newe
1bf90 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  r versions of SQ
1bf91 4c 69 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61  Lite, the.  ** a
1bf92 62 69 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f  bility to run lo
1bf93 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  ad_extension is 
1bf94 74 75 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65  turned off by de
1bf95 66 61 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a  fault.  One.  **
1bf96 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74   must call sqlit
1bf97 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65  e3_enable_load_e
1bf98 78 74 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75  xtension() to tu
1bf99 72 6e 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  rn on extension.
1bf9a 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f    ** loading.  O
1bf9b 74 68 65 72 77 69 73 65 20 79 6f 75 20 67 65 74  therwise you get
1bf9c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
1bf9d 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rror..  */.  if(
1bf9e 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
1bf9f 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69  LITE_LoadExtensi
1bfa0 6f 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  on)==0 ){.    if
1bfa1 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
1bfa2 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
1bfa3 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
1bfa4 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22  "not authorized"
1bfa5 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
1bfa6 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1bfa7 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72  ;.  }..  if( zPr
1bfa8 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72  oc==0 ){.    zPr
1bfa9 6f 63 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78  oc = "sqlite3_ex
1bfaa 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20  tension_init";. 
1bfab 20 7d 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73   }..  handle = s
1bfac 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70  qlite3OsDlOpen(p
1bfad 56 66 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69  Vfs, zFile);.  i
1bfae 66 28 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a  f( handle==0 ){.
1bfaf 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
1bfb0 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a   ){.      char z
1bfb1 45 72 72 5b 32 35 36 5d 3b 0a 20 20 20 20 20 20  Err[256];.      
1bfb2 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72  zErr[sizeof(zErr
1bfb3 29 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20  )-1] = '\0';.   
1bfb4 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1bfb5 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
1bfb6 2d 31 2c 20 7a 45 72 72 2c 20 0a 20 20 20 20 20  -1, zErr, .     
1bfb7 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
1bfb8 6f 70 65 6e 20 73 68 61 72 65 64 20 6c 69 62 72  open shared libr
1bfb9 61 72 79 20 5b 25 73 5d 22 2c 20 7a 46 69 6c 65  ary [%s]", zFile
1bfba 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1bfbb 4f 73 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20  OsDlError(pVfs, 
1bfbc 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20  sizeof(zErr)-1, 
1bfbd 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 2a 70 7a  zErr);.      *pz
1bfbe 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
1bfbf 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 45 72  DbStrDup(db, zEr
1bfc0 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  r);.    }.    re
1bfc1 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1bfc2 52 3b 0a 20 20 7d 0a 20 20 78 49 6e 69 74 20 3d  R;.  }.  xInit =
1bfc3 20 28 69 6e 74 28 2a 29 28 73 71 6c 69 74 65 33   (int(*)(sqlite3
1bfc4 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73  *,char**,const s
1bfc5 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
1bfc6 6e 65 73 2a 29 29 0a 20 20 20 20 20 20 20 20 20  nes*)).         
1bfc7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1bfc8 33 4f 73 44 6c 53 79 6d 28 70 56 66 73 2c 20 68  3OsDlSym(pVfs, h
1bfc9 61 6e 64 6c 65 2c 20 7a 50 72 6f 63 29 3b 0a 20  andle, zProc);. 
1bfca 20 69 66 28 20 78 49 6e 69 74 3d 3d 30 20 29 7b   if( xInit==0 ){
1bfcb 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73  .    if( pzErrMs
1bfcc 67 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  g ){.      char 
1bfcd 7a 45 72 72 5b 32 35 36 5d 3b 0a 20 20 20 20 20  zErr[256];.     
1bfce 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72   zErr[sizeof(zEr
1bfcf 72 29 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  r)-1] = '\0';.  
1bfd0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1bfd1 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
1bfd2 29 2d 31 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20  )-1, zErr,.     
1bfd3 20 20 20 20 20 22 6e 6f 20 65 6e 74 72 79 20 70       "no entry p
1bfd4 6f 69 6e 74 20 5b 25 73 5d 20 69 6e 20 73 68 61  oint [%s] in sha
1bfd5 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25 73 5d  red library [%s]
1bfd6 22 2c 20 7a 50 72 6f 63 2c 7a 46 69 6c 65 29 3b  ", zProc,zFile);
1bfd7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
1bfd8 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 73 69  DlError(pVfs, si
1bfd9 7a 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45  zeof(zErr)-1, zE
1bfda 72 72 29 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72  rr);.      *pzEr
1bfdb 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62  rMsg = sqlite3Db
1bfdc 53 74 72 44 75 70 28 64 62 2c 20 7a 45 72 72 29  StrDup(db, zErr)
1bfdd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
1bfde 73 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 68  sDlClose(pVfs, h
1bfdf 61 6e 64 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  andle);.    }.  
1bfe0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1bfe1 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 20 69  ERROR;.  }else i
1bfe2 66 28 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45  f( xInit(db, &zE
1bfe3 72 72 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 5f  rrmsg, &sqlite3_
1bfe4 61 70 69 73 29 20 29 7b 0a 20 20 20 20 69 66 28  apis) ){.    if(
1bfe5 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
1bfe6 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
1bfe7 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
1bfe8 65 72 72 6f 72 20 64 75 72 69 6e 67 20 69 6e 69  error during ini
1bfe9 74 69 61 6c 69 7a 61 74 69 6f 6e 3a 20 25 73 22  tialization: %s"
1bfea 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  , zErrmsg);.    
1bfeb 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
1bfec 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  ee(zErrmsg);.   
1bfed 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73   sqlite3OsDlClos
1bfee 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 29 3b  e(pVfs, handle);
1bfef 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1bff0 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
1bff1 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20 6e   /* Append the n
1bff2 65 77 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  ew shared librar
1bff3 79 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20  y handle to the 
1bff4 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 20 61  db->aExtension a
1bff5 72 72 61 79 2e 20 2a 2f 0a 20 20 64 62 2d 3e 6e  rray. */.  db->n
1bff6 45 78 74 65 6e 73 69 6f 6e 2b 2b 3b 0a 20 20 61  Extension++;.  a
1bff7 48 61 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65 33  Handle = sqlite3
1bff8 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
1bff9 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a   sizeof(handle)*
1bffa 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 29 3b  db->nExtension);
1bffb 0a 20 20 69 66 28 20 61 48 61 6e 64 6c 65 3d 3d  .  if( aHandle==
1bffc 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1bffd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1bffe 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 45 78 74  }.  if( db->nExt
1bfff 65 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20 20 20 20  ension>0 ){.    
1c000 6d 65 6d 63 70 79 28 61 48 61 6e 64 6c 65 2c 20  memcpy(aHandle, 
1c001 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 2c 20  db->aExtension, 
1c002 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 28  sizeof(handle)*(
1c003 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2d 31  db->nExtension-1
1c004 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ));.  }.  sqlite
1c005 33 5f 66 72 65 65 28 64 62 2d 3e 61 45 78 74 65  3_free(db->aExte
1c006 6e 73 69 6f 6e 29 3b 0a 20 20 64 62 2d 3e 61 45  nsion);.  db->aE
1c007 78 74 65 6e 73 69 6f 6e 20 3d 20 61 48 61 6e 64  xtension = aHand
1c008 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61 45 78 74 65  le;..  db->aExte
1c009 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78 74 65 6e  nsion[db->nExten
1c00a 73 69 6f 6e 2d 31 5d 20 3d 20 68 61 6e 64 6c 65  sion-1] = handle
1c00b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1c00c 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  E_OK;.}.SQLITE_A
1c00d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
1c00e 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20  oad_extension(. 
1c00f 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
1c010 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74         /* Load t
1c011 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74  he extension int
1c012 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
1c013 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
1c014 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
1c015 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
1c016 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
1c017 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65  ary containing e
1c018 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f  xtension */.  co
1c019 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c  nst char *zProc,
1c01a 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69      /* Entry poi
1c01b 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69 74 65  nt.  Use "sqlite
1c01c 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
1c01d 22 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72  " if 0 */.  char
1c01e 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20   **pzErrMsg     
1c01f 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d    /* Put error m
1c020 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e  essage here if n
1c021 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ot 0 */.){.  int
1c022 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d   rc;.  sqlite3_m
1c023 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
1c024 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71  utex);.  rc = sq
1c025 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e 73 69  lite3LoadExtensi
1c026 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50  on(db, zFile, zP
1c027 72 6f 63 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  roc, pzErrMsg);.
1c028 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1c029 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
1c02a 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1c02b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 69  ../*.** Call thi
1c02c 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74  s routine when t
1c02d 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1c02e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 69 6e  ection is closin
1c02f 67 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f  g in order.** to
1c030 20 63 6c 65 61 6e 20 75 70 20 6c 6f 61 64 65 64   clean up loaded
1c031 20 65 78 74 65 6e 73 69 6f 6e 73 0a 2a 2f 0a 53   extensions.*/.S
1c032 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1c033 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45  id sqlite3CloseE
1c034 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65  xtensions(sqlite
1c035 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b  3 *db){.  int i;
1c036 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1c037 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
1c038 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f  ->mutex) );.  fo
1c039 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 45 78  r(i=0; i<db->nEx
1c03a 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b 0a 20  tension; i++){. 
1c03b 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c     sqlite3OsDlCl
1c03c 6f 73 65 28 64 62 2d 3e 70 56 66 73 2c 20 64 62  ose(db->pVfs, db
1c03d 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b 69 5d 29  ->aExtension[i])
1c03e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1c03f 66 72 65 65 28 64 62 2d 3e 61 45 78 74 65 6e 73  free(db->aExtens
1c040 69 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  ion);.}../*.** E
1c041 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65  nable or disable
1c042 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
1c043 6e 67 2e 20 20 45 78 74 65 6e 73 69 6f 6e 20 6c  ng.  Extension l
1c044 6f 61 64 69 6e 67 20 69 73 20 64 69 73 61 62 6c  oading is disabl
1c045 65 64 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c 74  ed by.** default
1c046 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70   so as not to op
1c047 65 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c 65  en security hole
1c048 73 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69  s in older appli
1c049 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  cations..*/.SQLI
1c04a 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1c04b 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65  e3_enable_load_e
1c04c 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65 33  xtension(sqlite3
1c04d 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29   *db, int onoff)
1c04e 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
1c04f 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
1c050 78 29 3b 0a 20 20 69 66 28 20 6f 6e 6f 66 66 20  x);.  if( onoff 
1c051 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  ){.    db->flags
1c052 20 7c 3d 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45   |= SQLITE_LoadE
1c053 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 65 6c 73  xtension;.  }els
1c054 65 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  e{.    db->flags
1c055 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 6f 61 64   &= ~SQLITE_Load
1c056 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 0a 20  Extension;.  }. 
1c057 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1c058 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
1c059 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1c05a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
1c05b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65  e following obje
1c05c 63 74 20 68 6f 6c 64 73 20 74 68 65 20 6c 69 73  ct holds the lis
1c05d 74 20 6f 66 20 61 75 74 6f 6d 61 74 69 63 61 6c  t of automatical
1c05e 6c 79 20 6c 6f 61 64 65 64 0a 2a 2a 20 65 78 74  ly loaded.** ext
1c05f 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  ensions..**.** T
1c060 68 69 73 20 6c 69 73 74 20 69 73 20 73 68 61 72  his list is shar
1c061 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64  ed across thread
1c062 73 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d  s.  The SQLITE_M
1c063 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
1c064 45 52 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74  ER.** mutex must
1c065 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 61   be held while a
1c066 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 6c 69  ccessing this li
1c067 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  st..*/.static st
1c068 72 75 63 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78  ruct {.  int nEx
1c069 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  t;        /* Num
1c06a 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
1c06b 6e 20 61 45 78 74 5b 5d 20 2a 2f 20 20 20 20 20  n aExt[] */     
1c06c 20 20 20 20 20 0a 20 20 76 6f 69 64 20 2a 2a 61       .  void **a
1c06d 45 78 74 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e  Ext;     /* Poin
1c06e 74 65 72 73 20 74 6f 20 74 68 65 20 65 78 74 65  ters to the exte
1c06f 6e 73 69 6f 6e 20 69 6e 69 74 20 66 75 6e 63 74  nsion init funct
1c070 69 6f 6e 73 20 2a 2f 0a 7d 20 61 75 74 6f 65 78  ions */.} autoex
1c071 74 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 0a  t = { 0, 0 };...
1c072 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
1c073 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b   statically link
1c074 65 64 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61  ed extension tha
1c075 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
1c076 6c 79 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20  ly.** loaded by 
1c077 65 76 65 72 79 20 6e 65 77 20 64 61 74 61 62 61  every new databa
1c078 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
1c079 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1c07a 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78   sqlite3_auto_ex
1c07b 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 2a 78 49  tension(void *xI
1c07c 6e 69 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  nit){.  int i;. 
1c07d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1c07e 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  _OK;.  sqlite3_m
1c07f 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71  utex *mutex = sq
1c080 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
1c081 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
1c082 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20  TATIC_MASTER);. 
1c083 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1c084 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66  nter(mutex);.  f
1c085 6f 72 28 69 3d 30 3b 20 69 3c 61 75 74 6f 65 78  or(i=0; i<autoex
1c086 74 2e 6e 45 78 74 3b 20 69 2b 2b 29 7b 0a 20 20  t.nExt; i++){.  
1c087 20 20 69 66 28 20 61 75 74 6f 65 78 74 2e 61 45    if( autoext.aE
1c088 78 74 5b 69 5d 3d 3d 78 49 6e 69 74 20 29 20 62  xt[i]==xInit ) b
1c089 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  reak;.  }.  if( 
1c08a 69 3d 3d 61 75 74 6f 65 78 74 2e 6e 45 78 74 20  i==autoext.nExt 
1c08b 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  ){.    int nByte
1c08c 20 3d 20 28 61 75 74 6f 65 78 74 2e 6e 45 78 74   = (autoext.nExt
1c08d 2b 31 29 2a 73 69 7a 65 6f 66 28 61 75 74 6f 65  +1)*sizeof(autoe
1c08e 78 74 2e 61 45 78 74 5b 30 5d 29 3b 0a 20 20 20  xt.aExt[0]);.   
1c08f 20 76 6f 69 64 20 2a 2a 61 4e 65 77 3b 0a 20 20   void **aNew;.  
1c090 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    aNew = sqlite3
1c091 5f 72 65 61 6c 6c 6f 63 28 61 75 74 6f 65 78 74  _realloc(autoext
1c092 2e 61 45 78 74 2c 20 6e 42 79 74 65 29 3b 0a 20  .aExt, nByte);. 
1c093 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29     if( aNew==0 )
1c094 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1c095 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
1c096 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 75 74 6f  else{.      auto
1c097 65 78 74 2e 61 45 78 74 20 3d 20 61 4e 65 77 3b  ext.aExt = aNew;
1c098 0a 20 20 20 20 20 20 61 75 74 6f 65 78 74 2e 61  .      autoext.a
1c099 45 78 74 5b 61 75 74 6f 65 78 74 2e 6e 45 78 74  Ext[autoext.nExt
1c09a 5d 20 3d 20 78 49 6e 69 74 3b 0a 20 20 20 20 20  ] = xInit;.     
1c09b 20 61 75 74 6f 65 78 74 2e 6e 45 78 74 2b 2b 3b   autoext.nExt++;
1c09c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
1c09d 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1c09e 28 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72  (mutex);.  asser
1c09f 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72 63  t( (rc&0xff)==rc
1c0a0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
1c0a1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20  .}../*.** Reset 
1c0a2 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 65 78  the automatic ex
1c0a3 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
1c0a4 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 53 51  mechanism..*/.SQ
1c0a5 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1c0a6 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f  lite3_reset_auto
1c0a7 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29  _extension(void)
1c0a8 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
1c0a9 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  x *mutex = sqlit
1c0aa 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
1c0ab 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
1c0ac 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71  IC_MASTER);.  sq
1c0ad 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1c0ae 72 28 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69  r(mutex);.  sqli
1c0af 74 65 33 5f 66 72 65 65 28 61 75 74 6f 65 78 74  te3_free(autoext
1c0b0 2e 61 45 78 74 29 3b 0a 20 20 61 75 74 6f 65 78  .aExt);.  autoex
1c0b1 74 2e 61 45 78 74 20 3d 20 30 3b 0a 20 20 61 75  t.aExt = 0;.  au
1c0b2 74 6f 65 78 74 2e 6e 45 78 74 20 3d 20 30 3b 0a  toext.nExt = 0;.
1c0b3 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1c0b4 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a  leave(mutex);.}.
1c0b5 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 61 6c 6c 20  ./*.** Load all 
1c0b6 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73  automatic extens
1c0b7 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
1c0b8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1c0b9 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e  te3AutoLoadExten
1c0ba 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  sions(sqlite3 *d
1c0bb 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  b){.  int i;.  i
1c0bc 6e 74 20 67 6f 20 3d 20 31 3b 0a 20 20 69 6e 74  nt go = 1;.  int
1c0bd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1c0be 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28  .  int (*xInit)(
1c0bf 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c  sqlite3*,char**,
1c0c0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
1c0c1 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 0a 20  i_routines*);.. 
1c0c2 20 69 66 28 20 61 75 74 6f 65 78 74 2e 6e 45 78   if( autoext.nEx
1c0c3 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43  t==0 ){.    /* C
1c0c4 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61 72 6c  ommon case: earl
1c0c5 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20 65 76  y out without ev
1c0c6 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 61 63  ery having to ac
1c0c7 71 75 69 72 65 20 61 20 6d 75 74 65 78 20 2a 2f  quire a mutex */
1c0c8 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1c0c9 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 66 6f 72  TE_OK;.  }.  for
1c0ca 28 69 3d 30 3b 20 67 6f 3b 20 69 2b 2b 29 7b 0a  (i=0; go; i++){.
1c0cb 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 6d 73      char *zErrms
1c0cc 67 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  g = 0;.    sqlit
1c0cd 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20  e3_mutex *mutex 
1c0ce 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  = sqlite3_mutex_
1c0cf 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  alloc(SQLITE_MUT
1c0d0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
1c0d1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1c0d2 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
1c0d3 29 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 61 75  );.    if( i>=au
1c0d4 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20 20  toext.nExt ){.  
1c0d5 20 20 20 20 78 49 6e 69 74 20 3d 20 30 3b 0a 20      xInit = 0;. 
1c0d6 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20       go = 0;.   
1c0d7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 78 49   }else{.      xI
1c0d8 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71  nit = (int(*)(sq
1c0d9 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f  lite3*,char**,co
1c0da 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
1c0db 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20 20 20  routines*)).    
1c0dc 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 65 78            autoex
1c0dd 74 2e 61 45 78 74 5b 69 5d 3b 0a 20 20 20 20 7d  t.aExt[i];.    }
1c0de 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1c0df 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b  ex_leave(mutex);
1c0e0 0a 20 20 20 20 69 66 28 20 78 49 6e 69 74 20 26  .    if( xInit &
1c0e1 26 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45 72  & xInit(db, &zEr
1c0e2 72 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 5f 61  rmsg, &sqlite3_a
1c0e3 70 69 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71  pis) ){.      sq
1c0e4 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
1c0e5 51 4c 49 54 45 5f 45 52 52 4f 52 2c 0a 20 20 20  QLITE_ERROR,.   
1c0e6 20 20 20 20 20 20 20 20 20 22 61 75 74 6f 6d 61           "automa
1c0e7 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  tic extension lo
1c0e8 61 64 69 6e 67 20 66 61 69 6c 65 64 3a 20 25 73  ading failed: %s
1c0e9 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  ", zErrmsg);.   
1c0ea 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 20     go = 0;.     
1c0eb 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1c0ec 4f 52 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  OR;.      sqlite
1c0ed 33 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b  3_free(zErrmsg);
1c0ee 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1c0ef 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69  urn rc;.}..#endi
1c0f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1c0f1 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20  _LOAD_EXTENSION 
1c0f2 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
1c0f3 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61 64 65  *** End of loade
1c0f4 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xt.c ***********
1c0f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0f7 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1c0f8 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
1c0f9 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ragma.c ********
1c0fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0fc 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41  **/./*.** 2003 A
1c0fd 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
1c0fe 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1c0ff 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1c100 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1c101 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1c102 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1c103 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1c104 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1c105 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1c106 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1c107 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1c108 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1c109 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1c10a 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1c10b 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1c10c 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1c10d 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1c10e 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1c10f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c111 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c112 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c113 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
1c114 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
1c115 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
1c116 6e 74 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f  nt the PRAGMA co
1c117 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  mmand..**.** $Id
1c118 3a 20 70 72 61 67 6d 61 2e 63 2c 76 20 31 2e 31  : pragma.c,v 1.1
1c119 34 39 20 32 30 30 37 2f 30 38 2f 33 31 20 31 38  49 2007/08/31 18
1c11a 3a 33 34 3a 35 39 20 64 72 68 20 45 78 70 20 24  :34:59 drh Exp $
1c11b 0a 2a 2f 0a 0a 2f 2a 20 49 67 6e 6f 72 65 20 74  .*/../* Ignore t
1c11c 68 69 73 20 77 68 6f 6c 65 20 66 69 6c 65 20 69  his whole file i
1c11d 66 20 70 72 61 67 6d 61 73 20 61 72 65 20 64 69  f pragmas are di
1c11e 73 61 62 6c 65 64 0a 2a 2f 0a 23 69 66 20 21 64  sabled.*/.#if !d
1c11f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1c120 49 54 5f 50 52 41 47 4d 41 29 20 26 26 20 21 64  IT_PRAGMA) && !d
1c121 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1c122 49 54 5f 50 41 52 53 45 52 29 0a 0a 2f 2a 0a 2a  IT_PARSER)../*.*
1c123 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20  * Interpret the 
1c124 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20  given string as 
1c125 61 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2e 20  a safety level. 
1c126 20 52 65 74 75 72 6e 20 30 20 66 6f 72 20 4f 46   Return 0 for OF
1c127 46 2c 0a 2a 2a 20 31 20 66 6f 72 20 4f 4e 20 6f  F,.** 1 for ON o
1c128 72 20 4e 4f 52 4d 41 4c 20 61 6e 64 20 32 20 66  r NORMAL and 2 f
1c129 6f 72 20 46 55 4c 4c 2e 20 20 52 65 74 75 72 6e  or FULL.  Return
1c12a 20 31 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20   1 for an empty 
1c12b 6f 72 20 0a 2a 2a 20 75 6e 72 65 63 6f 67 6e 69  or .** unrecogni
1c12c 7a 65 64 20 73 74 72 69 6e 67 20 61 72 67 75 6d  zed string argum
1c12d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ent..**.** Note 
1c12e 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20  that the values 
1c12f 72 65 74 75 72 6e 65 64 20 61 72 65 20 6f 6e 65  returned are one
1c130 20 6c 65 73 73 20 74 68 61 74 20 74 68 65 20 76   less that the v
1c131 61 6c 75 65 73 20 74 68 61 74 0a 2a 2a 20 73 68  alues that.** sh
1c132 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 69  ould be passed i
1c133 6e 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65  nto sqlite3Btree
1c134 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 29  SetSafetyLevel()
1c135 2e 20 20 54 68 65 20 69 73 20 64 6f 6e 65 0a 2a  .  The is done.*
1c136 2a 20 74 6f 20 73 75 70 70 6f 72 74 20 6c 65 67  * to support leg
1c137 61 63 79 20 53 51 4c 20 63 6f 64 65 2e 20 20 54  acy SQL code.  T
1c138 68 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20  he safety level 
1c139 75 73 65 64 20 74 6f 20 62 65 20 62 6f 6f 6c 65  used to be boole
1c13a 61 6e 0a 2a 2a 20 61 6e 64 20 6f 6c 64 65 72 20  an.** and older 
1c13b 73 63 72 69 70 74 73 20 6d 61 79 20 68 61 76 65  scripts may have
1c13c 20 75 73 65 64 20 6e 75 6d 62 65 72 73 20 30 20   used numbers 0 
1c13d 66 6f 72 20 4f 46 46 20 61 6e 64 20 31 20 66 6f  for OFF and 1 fo
1c13e 72 20 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  r ON..*/.static 
1c13f 69 6e 74 20 67 65 74 53 61 66 65 74 79 4c 65 76  int getSafetyLev
1c140 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  el(const char *z
1c141 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1c142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c143 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33  /* 123456789 123
1c144 34 35 36 37 38 39 20 2a 2f 0a 20 20 73 74 61 74  456789 */.  stat
1c145 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 54  ic const char zT
1c146 65 78 74 5b 5d 20 3d 20 22 6f 6e 6f 66 66 61 6c  ext[] = "onoffal
1c147 73 65 79 65 73 74 72 75 65 66 75 6c 6c 22 3b 0a  seyestruefull";.
1c148 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
1c149 38 20 69 4f 66 66 73 65 74 5b 5d 20 3d 20 7b 30  8 iOffset[] = {0
1c14a 2c 20 31 2c 20 32 2c 20 34 2c 20 39 2c 20 31 32  , 1, 2, 4, 9, 12
1c14b 2c 20 31 36 7d 3b 0a 20 20 73 74 61 74 69 63 20  , 16};.  static 
1c14c 63 6f 6e 73 74 20 75 38 20 69 4c 65 6e 67 74 68  const u8 iLength
1c14d 5b 5d 20 3d 20 7b 32 2c 20 32 2c 20 33 2c 20 35  [] = {2, 2, 3, 5
1c14e 2c 20 33 2c 20 34 2c 20 34 7d 3b 0a 20 20 73 74  , 3, 4, 4};.  st
1c14f 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69 56  atic const u8 iV
1c150 61 6c 75 65 5b 5d 20 3d 20 20 7b 31 2c 20 30 2c  alue[] =  {1, 0,
1c151 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32 7d 3b   0, 0, 1, 1, 2};
1c152 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69  .  int i, n;.  i
1c153 66 28 20 69 73 64 69 67 69 74 28 2a 7a 29 20 29  f( isdigit(*z) )
1c154 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 74 6f  {.    return ato
1c155 69 28 7a 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20  i(z);.  }.  n = 
1c156 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 66 6f 72  strlen(z);.  for
1c157 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 69  (i=0; i<sizeof(i
1c158 4c 65 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a 20  Length); i++){. 
1c159 20 20 20 69 66 28 20 69 4c 65 6e 67 74 68 5b 69     if( iLength[i
1c15a 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53  ]==n && sqlite3S
1c15b 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 69  trNICmp(&zText[i
1c15c 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d  Offset[i]],z,n)=
1c15d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
1c15e 72 6e 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20 20  rn iValue[i];.  
1c15f 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1c160 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74   1;.}../*.** Int
1c161 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
1c162 20 73 74 72 69 6e 67 20 61 73 20 61 20 62 6f 6f   string as a boo
1c163 6c 65 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73  lean value..*/.s
1c164 74 61 74 69 63 20 69 6e 74 20 67 65 74 42 6f 6f  tatic int getBoo
1c165 6c 65 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  lean(const char 
1c166 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 65  *z){.  return ge
1c167 74 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 29 26  tSafetyLevel(z)&
1c168 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  1;.}../*.** Inte
1c169 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
1c16a 73 74 72 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b  string as a lock
1c16b 69 6e 67 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a  ing mode value..
1c16c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
1c16d 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e  tLockingMode(con
1c16e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
1c16f 66 28 20 7a 20 29 7b 0a 20 20 20 20 69 66 28 20  f( z ){.    if( 
1c170 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
1c171 70 28 7a 2c 20 22 65 78 63 6c 75 73 69 76 65 22  p(z, "exclusive"
1c172 29 20 29 20 72 65 74 75 72 6e 20 50 41 47 45 52  ) ) return PAGER
1c173 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
1c174 4c 55 53 49 56 45 3b 0a 20 20 20 20 69 66 28 20  LUSIVE;.    if( 
1c175 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
1c176 70 28 7a 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29  p(z, "normal") )
1c177 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f   return PAGER_LO
1c178 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
1c179 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 50  ;.  }.  return P
1c17a 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
1c17b 5f 51 55 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64  _QUERY;.}..#ifnd
1c17c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1c17d 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
1c17e 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69  Interpret the gi
1c17f 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 6e  ven string as an
1c180 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64   auto-vacuum mod
1c181 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  e value..**.** T
1c182 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
1c183 69 6e 67 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66  ings, "none", "f
1c184 75 6c 6c 22 20 61 6e 64 20 22 69 6e 63 72 65 6d  ull" and "increm
1c185 65 6e 74 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61  ental" are .** a
1c186 63 63 65 70 74 61 62 6c 65 2c 20 61 73 20 61 72  cceptable, as ar
1c187 65 20 74 68 65 69 72 20 6e 75 6d 65 72 69 63 20  e their numeric 
1c188 65 71 75 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20  equivalents: 0, 
1c189 31 20 61 6e 64 20 32 20 72 65 73 70 65 63 74 69  1 and 2 respecti
1c18a 76 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  vely..*/.static 
1c18b 69 6e 74 20 67 65 74 41 75 74 6f 56 61 63 75 75  int getAutoVacuu
1c18c 6d 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  m(const char *z)
1c18d 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
1c18e 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
1c18f 6d 70 28 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20  mp(z, "none") ) 
1c190 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54  return BTREE_AUT
1c191 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20  OVACUUM_NONE;.  
1c192 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
1c193 72 49 43 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29  rICmp(z, "full")
1c194 20 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f   ) return BTREE_
1c195 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b  AUTOVACUUM_FULL;
1c196 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65  .  if( 0==sqlite
1c197 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63  3StrICmp(z, "inc
1c198 72 65 6d 65 6e 74 61 6c 22 29 20 29 20 72 65 74  remental") ) ret
1c199 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41  urn BTREE_AUTOVA
1c19a 43 55 55 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d  CUUM_INCR;.  i =
1c19b 20 61 74 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75   atoi(z);.  retu
1c19c 72 6e 20 28 28 69 3e 3d 30 26 26 69 3c 3d 32 29  rn ((i>=0&&i<=2)
1c19d 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  ?i:0);.}.#endif 
1c19e 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
1c19f 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1c1a0 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1c1a1 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
1c1a2 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 74  RAGMAS./*.** Int
1c1a3 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
1c1a4 20 73 74 72 69 6e 67 20 61 73 20 61 20 74 65 6d   string as a tem
1c1a5 70 20 64 62 20 6c 6f 63 61 74 69 6f 6e 2e 20 52  p db location. R
1c1a6 65 74 75 72 6e 20 31 20 66 6f 72 20 66 69 6c 65  eturn 1 for file
1c1a7 0a 2a 2a 20 62 61 63 6b 65 64 20 74 65 6d 70 6f  .** backed tempo
1c1a8 72 61 72 79 20 64 61 74 61 62 61 73 65 73 2c 20  rary databases, 
1c1a9 32 20 66 6f 72 20 74 68 65 20 52 65 64 2d 42 6c  2 for the Red-Bl
1c1aa 61 63 6b 20 74 72 65 65 20 69 6e 20 6d 65 6d 6f  ack tree in memo
1c1ab 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 61  ry database.** a
1c1ac 6e 64 20 30 20 74 6f 20 75 73 65 20 74 68 65 20  nd 0 to use the 
1c1ad 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 64 65 66  compile-time def
1c1ae 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ault..*/.static 
1c1af 69 6e 74 20 67 65 74 54 65 6d 70 53 74 6f 72 65  int getTempStore
1c1b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
1c1b1 0a 20 20 69 66 28 20 7a 5b 30 5d 3e 3d 27 30 27  .  if( z[0]>='0'
1c1b2 20 26 26 20 7a 5b 30 5d 3c 3d 27 32 27 20 29 7b   && z[0]<='2' ){
1c1b3 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 5b 30 5d  .    return z[0]
1c1b4 20 2d 20 27 30 27 3b 0a 20 20 7d 65 6c 73 65 20   - '0';.  }else 
1c1b5 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1c1b6 6d 70 28 7a 2c 20 22 66 69 6c 65 22 29 3d 3d 30  mp(z, "file")==0
1c1b7 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
1c1b8 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
1c1b9 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
1c1ba 22 6d 65 6d 6f 72 79 22 29 3d 3d 30 20 29 7b 0a  "memory")==0 ){.
1c1bb 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
1c1bc 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
1c1bd 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  n 0;.  }.}.#endi
1c1be 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45  f /* SQLITE_PAGE
1c1bf 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  R_PRAGMAS */..#i
1c1c0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1c1c1 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
1c1c2 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65  /*.** Invalidate
1c1c3 20 74 65 6d 70 20 73 74 6f 72 61 67 65 2c 20 65   temp storage, e
1c1c4 69 74 68 65 72 20 77 68 65 6e 20 74 68 65 20 74  ither when the t
1c1c5 65 6d 70 20 73 74 6f 72 61 67 65 20 69 73 20 63  emp storage is c
1c1c6 68 61 6e 67 65 64 0a 2a 2a 20 66 72 6f 6d 20 64  hanged.** from d
1c1c7 65 66 61 75 6c 74 2c 20 6f 72 20 77 68 65 6e 20  efault, or when 
1c1c8 27 66 69 6c 65 27 20 61 6e 64 20 74 68 65 20 74  'file' and the t
1c1c9 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
1c1ca 6f 72 79 20 68 61 73 20 63 68 61 6e 67 65 64 0a  ory has changed.
1c1cb 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e  */.static int in
1c1cc 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72  validateTempStor
1c1cd 61 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  age(Parse *pPars
1c1ce 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e){.  sqlite3 *d
1c1cf 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1c1d0 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d    if( db->aDb[1]
1c1d1 2e 70 42 74 21 3d 30 20 29 7b 0a 20 20 20 20 69  .pBt!=0 ){.    i
1c1d2 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
1c1d3 69 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  it ){.      sqli
1c1d4 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1c1d5 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 73  se, "temporary s
1c1d6 74 6f 72 61 67 65 20 63 61 6e 6e 6f 74 20 62 65  torage cannot be
1c1d7 20 63 68 61 6e 67 65 64 20 22 0a 20 20 20 20 20   changed ".     
1c1d8 20 20 20 22 66 72 6f 6d 20 77 69 74 68 69 6e 20     "from within 
1c1d9 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b  a transaction");
1c1da 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1c1db 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1c1dc 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  }.    sqlite3Btr
1c1dd 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b  eeClose(db->aDb[
1c1de 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 64 62 2d  1].pBt);.    db-
1c1df 3e 61 44 62 5b 31 5d 2e 70 42 74 20 3d 20 30 3b  >aDb[1].pBt = 0;
1c1e0 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65  .    sqlite3Rese
1c1e1 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
1c1e2 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65  db, 0);.  }.  re
1c1e3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1c1e4 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1c1e5 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  TE_PAGER_PRAGMAS
1c1e6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1c1e7 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
1c1e8 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66 20  RAGMAS./*.** If 
1c1e9 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73  the TEMP databas
1c1ea 65 20 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73 65  e is open, close
1c1eb 20 69 74 20 61 6e 64 20 6d 61 72 6b 20 74 68 65   it and mark the
1c1ec 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1c1ed 0a 2a 2a 20 61 73 20 6e 65 65 64 69 6e 67 20 72  .** as needing r
1c1ee 65 6c 6f 61 64 69 6e 67 2e 20 20 54 68 69 73 20  eloading.  This 
1c1ef 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 77 68 65  must be done whe
1c1f0 6e 20 75 73 69 6e 67 20 74 68 65 20 54 45 4d 50  n using the TEMP
1c1f1 5f 53 54 4f 52 45 0a 2a 2a 20 6f 72 20 44 45 46  _STORE.** or DEF
1c1f2 41 55 4c 54 5f 54 45 4d 50 5f 53 54 4f 52 45 20  AULT_TEMP_STORE 
1c1f3 70 72 61 67 6d 61 73 2e 0a 2a 2f 0a 73 74 61 74  pragmas..*/.stat
1c1f4 69 63 20 69 6e 74 20 63 68 61 6e 67 65 54 65 6d  ic int changeTem
1c1f5 70 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a  pStorage(Parse *
1c1f6 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
1c1f7 61 72 20 2a 7a 53 74 6f 72 61 67 65 54 79 70 65  ar *zStorageType
1c1f8 29 7b 0a 20 20 69 6e 74 20 74 73 20 3d 20 67 65  ){.  int ts = ge
1c1f9 74 54 65 6d 70 53 74 6f 72 65 28 7a 53 74 6f 72  tTempStore(zStor
1c1fa 61 67 65 54 79 70 65 29 3b 0a 20 20 73 71 6c 69  ageType);.  sqli
1c1fb 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1c1fc 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ->db;.  if( db->
1c1fd 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 74 73 20 29  temp_store==ts )
1c1fe 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1c1ff 4b 3b 0a 20 20 69 66 28 20 69 6e 76 61 6c 69 64  K;.  if( invalid
1c200 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 20  ateTempStorage( 
1c201 70 50 61 72 73 65 20 29 20 21 3d 20 53 51 4c 49  pParse ) != SQLI
1c202 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
1c203 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1c204 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 74 65 6d 70  ;.  }.  db->temp
1c205 5f 73 74 6f 72 65 20 3d 20 74 73 3b 0a 20 20 72  _store = ts;.  r
1c206 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1c207 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1c208 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  ITE_PAGER_PRAGMA
1c209 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  S */../*.** Gene
1c20a 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65 74  rate code to ret
1c20b 75 72 6e 20 61 20 73 69 6e 67 6c 65 20 69 6e 74  urn a single int
1c20c 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73  eger value..*/.s
1c20d 74 61 74 69 63 20 76 6f 69 64 20 72 65 74 75 72  tatic void retur
1c20e 6e 53 69 6e 67 6c 65 49 6e 74 28 50 61 72 73 65  nSingleInt(Parse
1c20f 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
1c210 63 68 61 72 20 2a 7a 4c 61 62 65 6c 2c 20 69 6e  char *zLabel, in
1c211 74 20 76 61 6c 75 65 29 7b 0a 20 20 56 64 62 65  t value){.  Vdbe
1c212 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
1c213 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1c214 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c215 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1c216 76 61 6c 75 65 2c 20 30 29 3b 0a 20 20 69 66 28  value, 0);.  if(
1c217 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
1c218 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1c219 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1c21a 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
1c21b 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1c21c 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
1c21d 4e 41 4d 45 2c 20 7a 4c 61 62 65 6c 2c 20 50 33  NAME, zLabel, P3
1c21e 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20  _STATIC);.  }.  
1c21f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c220 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c  (v, OP_Callback,
1c221 20 31 2c 20 30 29 3b 0a 7d 0a 0a 23 69 66 6e 64   1, 0);.}..#ifnd
1c222 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
1c223 4c 41 47 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a  LAG_PRAGMAS./*.*
1c224 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1c225 66 20 7a 52 69 67 68 74 20 61 6e 64 20 7a 4c 65  f zRight and zLe
1c226 66 74 20 72 65 66 65 72 20 74 6f 20 61 20 70 72  ft refer to a pr
1c227 61 67 6d 61 20 74 68 61 74 20 71 75 65 72 69 65  agma that querie
1c228 73 0a 2a 2a 20 6f 72 20 63 68 61 6e 67 65 73 20  s.** or changes 
1c229 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c 61 67 73  one of the flags
1c22a 20 69 6e 20 64 62 2d 3e 66 6c 61 67 73 2e 20 20   in db->flags.  
1c22b 52 65 74 75 72 6e 20 31 20 69 66 20 73 6f 20 61  Return 1 if so a
1c22c 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2a 20  nd 0 if not..** 
1c22d 41 6c 73 6f 2c 20 69 6d 70 6c 65 6d 65 6e 74 20  Also, implement 
1c22e 74 68 65 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 73  the pragma..*/.s
1c22f 74 61 74 69 63 20 69 6e 74 20 66 6c 61 67 50 72  tatic int flagPr
1c230 61 67 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72  agma(Parse *pPar
1c231 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
1c232 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61  zLeft, const cha
1c233 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 73 74  r *zRight){.  st
1c234 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
1c235 74 20 73 50 72 61 67 6d 61 54 79 70 65 20 7b 0a  t sPragmaType {.
1c236 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1c237 7a 4e 61 6d 65 3b 20 20 2f 2a 20 4e 61 6d 65 20  zName;  /* Name 
1c238 6f 66 20 74 68 65 20 70 72 61 67 6d 61 20 2a 2f  of the pragma */
1c239 0a 20 20 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20  .    int mask;  
1c23a 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
1c23b 20 66 6f 72 20 74 68 65 20 64 62 2d 3e 66 6c 61   for the db->fla
1c23c 67 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20  gs value */.  } 
1c23d 61 50 72 61 67 6d 61 5b 5d 20 3d 20 7b 0a 20 20  aPragma[] = {.  
1c23e 20 20 7b 20 22 66 75 6c 6c 5f 63 6f 6c 75 6d 6e    { "full_column
1c23f 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20 20 20 20  _names",        
1c240 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61  SQLITE_FullColNa
1c241 6d 65 73 20 20 7d 2c 0a 20 20 20 20 7b 20 22 73  mes  },.    { "s
1c242 68 6f 72 74 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  hort_column_name
1c243 73 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54 45  s",       SQLITE
1c244 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 20 7d  _ShortColNames }
1c245 2c 0a 20 20 20 20 7b 20 22 63 6f 75 6e 74 5f 63  ,.    { "count_c
1c246 68 61 6e 67 65 73 22 2c 20 20 20 20 20 20 20 20  hanges",        
1c247 20 20 20 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74      SQLITE_Count
1c248 52 6f 77 73 20 20 20 20 20 7d 2c 0a 20 20 20 20  Rows     },.    
1c249 7b 20 22 65 6d 70 74 79 5f 72 65 73 75 6c 74 5f  { "empty_result_
1c24a 63 61 6c 6c 62 61 63 6b 73 22 2c 20 20 20 53 51  callbacks",   SQ
1c24b 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63  LITE_NullCallbac
1c24c 6b 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 65 67  k  },.    { "leg
1c24d 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 22  acy_file_format"
1c24e 2c 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4c  ,       SQLITE_L
1c24f 65 67 61 63 79 46 69 6c 65 46 6d 74 20 7d 2c 0a  egacyFileFmt },.
1c250 20 20 20 20 7b 20 22 66 75 6c 6c 66 73 79 6e 63      { "fullfsync
1c251 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
1c252 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79    SQLITE_FullFSy
1c253 6e 63 20 20 20 20 20 7d 2c 0a 23 69 66 64 65 66  nc     },.#ifdef
1c254 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
1c255 20 20 7b 20 22 73 71 6c 5f 74 72 61 63 65 22 2c    { "sql_trace",
1c256 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c257 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20  SQLITE_SqlTrace 
1c258 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76       },.    { "v
1c259 64 62 65 5f 6c 69 73 74 69 6e 67 22 2c 20 20 20  dbe_listing",   
1c25a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
1c25b 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 7d  _VdbeListing   }
1c25c 2c 0a 20 20 20 20 7b 20 22 76 64 62 65 5f 74 72  ,.    { "vdbe_tr
1c25d 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20 20  ace",           
1c25e 20 20 20 20 53 51 4c 49 54 45 5f 56 64 62 65 54      SQLITE_VdbeT
1c25f 72 61 63 65 20 20 20 20 20 7d 2c 0a 23 65 6e 64  race     },.#end
1c260 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
1c261 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20  E_OMIT_CHECK.   
1c262 20 7b 20 22 69 67 6e 6f 72 65 5f 63 68 65 63 6b   { "ignore_check
1c263 5f 63 6f 6e 73 74 72 61 69 6e 74 73 22 2c 20 53  _constraints", S
1c264 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63  QLITE_IgnoreChec
1c265 6b 73 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20  ks  },.#endif.  
1c266 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
1c267 6e 67 20 69 73 20 56 45 52 59 20 65 78 70 65 72  ng is VERY exper
1c268 69 6d 65 6e 74 61 6c 20 2a 2f 0a 20 20 20 20 7b  imental */.    {
1c269 20 22 77 72 69 74 61 62 6c 65 5f 73 63 68 65 6d   "writable_schem
1c26a 61 22 2c 20 20 20 20 20 20 20 20 20 20 53 51 4c  a",          SQL
1c26b 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 7c  ITE_WriteSchema|
1c26c 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
1c26d 6f 64 65 20 7d 2c 0a 20 20 20 20 7b 20 22 6f 6d  ode },.    { "om
1c26e 69 74 5f 72 65 61 64 6c 6f 63 6b 22 2c 20 20 20  it_readlock",   
1c26f 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
1c270 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 7d 2c  NoReadlock    },
1c271 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 4d  ..    /* TODO: M
1c272 61 79 62 65 20 69 74 20 73 68 6f 75 6c 64 6e 27  aybe it shouldn'
1c273 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f  t be possible to
1c274 20 63 68 61 6e 67 65 20 74 68 65 20 52 65 61 64   change the Read
1c275 55 6e 63 6f 6d 6d 69 74 74 65 64 0a 20 20 20 20  Uncommitted.    
1c276 2a 2a 20 66 6c 61 67 20 69 66 20 74 68 65 72 65  ** flag if there
1c277 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 20   are any active 
1c278 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20  statements. */. 
1c279 20 20 20 7b 20 22 72 65 61 64 5f 75 6e 63 6f 6d     { "read_uncom
1c27a 6d 69 74 74 65 64 22 2c 20 20 20 20 20 20 20 20  mitted",        
1c27b 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f   SQLITE_ReadUnco
1c27c 6d 6d 69 74 74 65 64 20 7d 2c 0a 20 20 7d 3b 0a  mmitted },.  };.
1c27d 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74    int i;.  const
1c27e 20 73 74 72 75 63 74 20 73 50 72 61 67 6d 61 54   struct sPragmaT
1c27f 79 70 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d  ype *p;.  for(i=
1c280 30 2c 20 70 3d 61 50 72 61 67 6d 61 3b 20 69 3c  0, p=aPragma; i<
1c281 73 69 7a 65 6f 66 28 61 50 72 61 67 6d 61 29 2f  sizeof(aPragma)/
1c282 73 69 7a 65 6f 66 28 61 50 72 61 67 6d 61 5b 30  sizeof(aPragma[0
1c283 5d 29 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  ]); i++, p++){. 
1c284 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
1c285 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 70 2d 3e  rICmp(zLeft, p->
1c286 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
1c287 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
1c288 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20   pParse->db;.   
1c289 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20     Vdbe *v;.    
1c28a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
1c28b 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1c28c 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20      if( v ){.   
1c28d 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d       if( zRight=
1c28e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1c28f 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
1c290 70 50 61 72 73 65 2c 20 70 2d 3e 7a 4e 61 6d 65  pParse, p->zName
1c291 2c 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 70  , (db->flags & p
1c292 2d 3e 6d 61 73 6b 29 21 3d 30 20 29 3b 0a 20 20  ->mask)!=0 );.  
1c293 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1c294 20 20 20 20 20 20 20 69 66 28 20 67 65 74 42 6f         if( getBo
1c295 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 20 29 7b  olean(zRight) ){
1c296 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d  .            db-
1c297 3e 66 6c 61 67 73 20 7c 3d 20 70 2d 3e 6d 61 73  >flags |= p->mas
1c298 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  k;.          }el
1c299 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
1c29a 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 70 2d  db->flags &= ~p-
1c29b 3e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20  >mask;.         
1c29c 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1c29d 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
1c29e 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
1c29f 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
1c2a0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1c2a1 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 20  IT_FLAG_PRAGMAS 
1c2a2 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73  */../*.** Proces
1c2a3 73 20 61 20 70 72 61 67 6d 61 20 73 74 61 74 65  s a pragma state
1c2a4 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 72  ment.  .**.** Pr
1c2a5 61 67 6d 61 73 20 61 72 65 20 6f 66 20 74 68 69  agmas are of thi
1c2a6 73 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  s form:.**.**   
1c2a7 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
1c2a8 61 73 65 2e 5d 69 64 20 5b 3d 20 76 61 6c 75 65  ase.]id [= value
1c2a9 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 65 6e  ].**.** The iden
1c2aa 74 69 66 69 65 72 20 6d 69 67 68 74 20 61 6c 73  tifier might als
1c2ab 6f 20 62 65 20 61 20 73 74 72 69 6e 67 2e 20 20  o be a string.  
1c2ac 54 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73  The value is a s
1c2ad 74 72 69 6e 67 2c 20 61 6e 64 0a 2a 2a 20 69 64  tring, and.** id
1c2ae 65 6e 74 69 66 69 65 72 2c 20 6f 72 20 61 20 6e  entifier, or a n
1c2af 75 6d 62 65 72 2e 20 20 49 66 20 6d 69 6e 75 73  umber.  If minus
1c2b0 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68  Flag is true, th
1c2b1 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 0a  en the value is.
1c2b2 2a 2a 20 61 20 6e 75 6d 62 65 72 20 74 68 61 74  ** a number that
1c2b3 20 77 61 73 20 70 72 65 63 65 64 65 64 20 62 79   was preceded by
1c2b4 20 61 20 6d 69 6e 75 73 20 73 69 67 6e 2e 0a 2a   a minus sign..*
1c2b5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 65 66 74  *.** If the left
1c2b6 20 73 69 64 65 20 69 73 20 22 64 61 74 61 62 61   side is "databa
1c2b7 73 65 2e 69 64 22 20 74 68 65 6e 20 70 49 64 31  se.id" then pId1
1c2b8 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
1c2b9 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 70 49 64   name.** and pId
1c2ba 32 20 69 73 20 74 68 65 20 69 64 2e 20 20 49 66  2 is the id.  If
1c2bb 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69   the left side i
1c2bc 73 20 6a 75 73 74 20 22 69 64 22 20 74 68 65 6e  s just "id" then
1c2bd 20 70 49 64 31 20 69 73 20 74 68 65 0a 2a 2a 20   pId1 is the.** 
1c2be 69 64 20 61 6e 64 20 70 49 64 32 20 69 73 20 61  id and pId2 is a
1c2bf 6e 79 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2e  ny empty string.
1c2c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1c2c1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
1c2c2 72 61 67 6d 61 28 0a 20 20 50 61 72 73 65 20 2a  ragma(.  Parse *
1c2c3 70 50 61 72 73 65 2c 20 0a 20 20 54 6f 6b 65 6e  pParse, .  Token
1c2c4 20 2a 70 49 64 31 2c 20 20 20 20 20 20 20 20 2f   *pId1,        /
1c2c5 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20  * First part of 
1c2c6 5b 64 61 74 61 62 61 73 65 2e 5d 69 64 20 66 69  [database.]id fi
1c2c7 65 6c 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  eld */.  Token *
1c2c8 70 49 64 32 2c 20 20 20 20 20 20 20 20 2f 2a 20  pId2,        /* 
1c2c9 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 5b  Second part of [
1c2ca 64 61 74 61 62 61 73 65 2e 5d 69 64 20 66 69 65  database.]id fie
1c2cb 6c 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  ld, or NULL */. 
1c2cc 20 54 6f 6b 65 6e 20 2a 70 56 61 6c 75 65 2c 20   Token *pValue, 
1c2cd 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 66 6f       /* Token fo
1c2ce 72 20 3c 76 61 6c 75 65 3e 2c 20 6f 72 20 4e 55  r <value>, or NU
1c2cf 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 75  LL */.  int minu
1c2d0 73 46 6c 61 67 20 20 20 20 20 20 20 2f 2a 20 54  sFlag       /* T
1c2d1 72 75 65 20 69 66 20 61 20 27 2d 27 20 73 69 67  rue if a '-' sig
1c2d2 6e 20 70 72 65 63 65 64 65 64 20 3c 76 61 6c 75  n preceded <valu
1c2d3 65 3e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20  e> */.){.  char 
1c2d4 2a 7a 4c 65 66 74 20 3d 20 30 3b 20 20 20 20 20  *zLeft = 0;     
1c2d5 20 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61    /* Nul-termina
1c2d6 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ted UTF-8 string
1c2d7 20 3c 69 64 3e 20 2a 2f 0a 20 20 63 68 61 72 20   <id> */.  char 
1c2d8 2a 7a 52 69 67 68 74 20 3d 20 30 3b 20 20 20 20  *zRight = 0;    
1c2d9 20 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61    /* Nul-termina
1c2da 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ted UTF-8 string
1c2db 20 3c 76 61 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c   <value>, or NUL
1c2dc 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  L */.  const cha
1c2dd 72 20 2a 7a 44 62 20 3d 20 30 3b 20 20 20 2f 2a  r *zDb = 0;   /*
1c2de 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   The database na
1c2df 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  me */.  Token *p
1c2e0 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Id;            /
1c2e1 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 3c 69 64  * Pointer to <id
1c2e2 3e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  > token */.  int
1c2e3 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
1c2e4 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
1c2e5 69 6e 64 65 78 20 66 6f 72 20 3c 64 61 74 61 62  index for <datab
1c2e6 61 73 65 3e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ase> */.  sqlite
1c2e7 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1c2e8 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20  db;.  Db *pDb;. 
1c2e9 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
1c2ea 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1c2eb 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
1c2ec 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 6e  return;..  /* In
1c2ed 74 65 72 70 72 65 74 20 74 68 65 20 5b 64 61 74  terpret the [dat
1c2ee 61 62 61 73 65 2e 5d 20 70 61 72 74 20 6f 66 20  abase.] part of 
1c2ef 74 68 65 20 70 72 61 67 6d 61 20 73 74 61 74 65  the pragma state
1c2f0 6d 65 6e 74 2e 20 69 44 62 20 69 73 20 74 68 65  ment. iDb is the
1c2f1 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74  .  ** index of t
1c2f2 68 65 20 64 61 74 61 62 61 73 65 20 74 68 69 73  he database this
1c2f3 20 70 72 61 67 6d 61 20 69 73 20 62 65 69 6e 67   pragma is being
1c2f4 20 61 70 70 6c 69 65 64 20 74 6f 20 69 6e 20 64   applied to in d
1c2f5 62 2e 61 44 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44  b.aDb[]. */.  iD
1c2f6 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
1c2f7 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
1c2f8 49 64 31 2c 20 70 49 64 32 2c 20 26 70 49 64 29  Id1, pId2, &pId)
1c2f9 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 20  ;.  if( iDb<0 ) 
1c2fa 72 65 74 75 72 6e 3b 0a 20 20 70 44 62 20 3d 20  return;.  pDb = 
1c2fb 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a  &db->aDb[iDb];..
1c2fc 20 20 2f 2a 20 49 66 20 74 68 65 20 74 65 6d 70    /* If the temp
1c2fd 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65   database has be
1c2fe 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 6e 61  en explicitly na
1c2ff 6d 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  med as part of t
1c300 68 65 20 0a 20 20 2a 2a 20 70 72 61 67 6d 61 2c  he .  ** pragma,
1c301 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
1c302 20 6f 70 65 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69   open. .  */.  i
1c303 66 28 20 69 44 62 3d 3d 31 20 26 26 20 73 71 6c  f( iDb==1 && sql
1c304 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61  ite3OpenTempData
1c305 62 61 73 65 28 70 50 61 72 73 65 29 20 29 7b 0a  base(pParse) ){.
1c306 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
1c307 0a 20 20 7a 4c 65 66 74 20 3d 20 73 71 6c 69 74  .  zLeft = sqlit
1c308 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1c309 64 62 2c 20 70 49 64 29 3b 0a 20 20 69 66 28 20  db, pId);.  if( 
1c30a 21 7a 4c 65 66 74 20 29 20 72 65 74 75 72 6e 3b  !zLeft ) return;
1c30b 0a 20 20 69 66 28 20 6d 69 6e 75 73 46 6c 61 67  .  if( minusFlag
1c30c 20 29 7b 0a 20 20 20 20 7a 52 69 67 68 74 20 3d   ){.    zRight =
1c30d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1c30e 64 62 2c 20 22 2d 25 54 22 2c 20 70 56 61 6c 75  db, "-%T", pValu
1c30f 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
1c310 20 7a 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65   zRight = sqlite
1c311 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
1c312 62 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 7d 0a  b, pValue);.  }.
1c313 0a 20 20 7a 44 62 20 3d 20 28 28 69 44 62 3e 30  .  zDb = ((iDb>0
1c314 29 3f 70 44 62 2d 3e 7a 4e 61 6d 65 3a 30 29 3b  )?pDb->zName:0);
1c315 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
1c316 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
1c317 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 2c 20 7a  SQLITE_PRAGMA, z
1c318 4c 65 66 74 2c 20 7a 52 69 67 68 74 2c 20 7a 44  Left, zRight, zD
1c319 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70  b) ){.    goto p
1c31a 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 7d 0a 20  ragma_out;.  }. 
1c31b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1c31c 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
1c31d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  AS.  /*.  **  PR
1c31e 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
1c31f 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69  default_cache_si
1c320 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  ze.  **  PRAGMA 
1c321 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66 61 75  [database.]defau
1c322 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0a  lt_cache_size=N.
1c323 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69    **.  ** The fi
1c324 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73  rst form reports
1c325 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 65 72   the current per
1c326 73 69 73 74 65 6e 74 20 73 65 74 74 69 6e 67 20  sistent setting 
1c327 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 67  for the.  ** pag
1c328 65 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20 54  e cache size.  T
1c329 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
1c32a 64 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  d is the maximum
1c32b 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20   number of.  ** 
1c32c 70 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67  pages in the pag
1c32d 65 20 63 61 63 68 65 2e 20 20 54 68 65 20 73 65  e cache.  The se
1c32e 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 62  cond form sets b
1c32f 6f 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 0a  oth the current.
1c330 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20    ** page cache 
1c331 73 69 7a 65 20 76 61 6c 75 65 20 61 6e 64 20 74  size value and t
1c332 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 70 61  he persistent pa
1c333 67 65 20 63 61 63 68 65 20 73 69 7a 65 20 76 61  ge cache size va
1c334 6c 75 65 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20  lue.  ** stored 
1c335 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1c336 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  file..  **.  ** 
1c337 54 68 65 20 64 65 66 61 75 6c 74 20 63 61 63 68  The default cach
1c338 65 20 73 69 7a 65 20 69 73 20 73 74 6f 72 65 64  e size is stored
1c339 20 69 6e 20 6d 65 74 61 2d 76 61 6c 75 65 20 32   in meta-value 2
1c33a 20 6f 66 20 70 61 67 65 20 31 20 6f 66 20 74 68   of page 1 of th
1c33b 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  e.  ** database 
1c33c 66 69 6c 65 2e 20 20 54 68 65 20 63 61 63 68 65  file.  The cache
1c33d 20 73 69 7a 65 20 69 73 20 61 63 74 75 61 6c 6c   size is actuall
1c33e 79 20 74 68 65 20 61 62 73 6f 6c 75 74 65 20 76  y the absolute v
1c33f 61 6c 75 65 20 6f 66 0a 20 20 2a 2a 20 74 68 69  alue of.  ** thi
1c340 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  s memory locatio
1c341 6e 2e 20 20 54 68 65 20 73 69 67 6e 20 6f 66 20  n.  The sign of 
1c342 6d 65 74 61 2d 76 61 6c 75 65 20 32 20 64 65 74  meta-value 2 det
1c343 65 72 6d 69 6e 65 73 20 74 68 65 0a 20 20 2a 2a  ermines the.  **
1c344 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 73 65 74   synchronous set
1c345 74 69 6e 67 2e 20 20 41 20 6e 65 67 61 74 69 76  ting.  A negativ
1c346 65 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 73 79  e value means sy
1c347 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66  nchronous is off
1c348 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 6f 73 69  .  ** and a posi
1c349 74 69 76 65 20 76 61 6c 75 65 20 6d 65 61 6e 73  tive value means
1c34a 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20   synchronous is 
1c34b 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  on..  */.  if( s
1c34c 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
1c34d 65 66 74 2c 22 64 65 66 61 75 6c 74 5f 63 61 63  eft,"default_cac
1c34e 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a  he_size")==0 ){.
1c34f 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
1c350 20 56 64 62 65 4f 70 4c 69 73 74 20 67 65 74 43   VdbeOpList getC
1c351 61 63 68 65 53 69 7a 65 5b 5d 20 3d 20 7b 0a 20  acheSize[] = {. 
1c352 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f       { OP_ReadCo
1c353 6f 6b 69 65 2c 20 20 30 2c 20 32 2c 20 20 20 20  okie,  0, 2,    
1c354 20 20 20 20 30 7d 2c 20 20 2f 2a 20 30 20 2a 2f      0},  /* 0 */
1c355 0a 20 20 20 20 20 20 7b 20 4f 50 5f 41 62 73 56  .      { OP_AbsV
1c356 61 6c 75 65 2c 20 20 20 20 30 2c 20 30 2c 20 20  alue,    0, 0,  
1c357 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20        0},.      
1c358 7b 20 4f 50 5f 44 75 70 2c 20 20 20 20 20 20 20  { OP_Dup,       
1c359 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30    0, 0,        0
1c35a 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e  },.      { OP_In
1c35b 74 65 67 65 72 2c 20 20 20 20 20 30 2c 20 30 2c  teger,     0, 0,
1c35c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20          0},.    
1c35d 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20    { OP_Ne,      
1c35e 20 20 20 20 30 2c 20 36 2c 20 20 20 20 20 20 20      0, 6,       
1c35f 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f   0},.      { OP_
1c360 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30 2c 20  Integer,     0, 
1c361 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 2f  0,        0},  /
1c362 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f  * 5 */.      { O
1c363 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 20 20 20 31  P_Callback,    1
1c364 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a  , 0,        0},.
1c365 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 61      };.    int a
1c366 64 64 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ddr;.    if( sql
1c367 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
1c368 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
1c369 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71  agma_out;.    sq
1c36a 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
1c36b 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20  ee(v, iDb);.    
1c36c 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20  if( !zRight ){. 
1c36d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c36e 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
1c36f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1c370 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
1c371 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
1c372 2c 20 22 63 61 63 68 65 5f 73 69 7a 65 22 2c 20  , "cache_size", 
1c373 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P3_STATIC);.    
1c374 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
1c375 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c  VdbeAddOpList(v,
1c376 20 41 72 72 61 79 53 69 7a 65 28 67 65 74 43 61   ArraySize(getCa
1c377 63 68 65 53 69 7a 65 29 2c 20 67 65 74 43 61 63  cheSize), getCac
1c378 68 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 73  heSize);.      s
1c379 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1c37a 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29  P1(v, addr, iDb)
1c37b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1c37c 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
1c37d 64 64 72 2b 35 2c 20 53 51 4c 49 54 45 5f 44 45  ddr+5, SQLITE_DE
1c37e 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
1c37f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1c380 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61      int size = a
1c381 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  toi(zRight);.   
1c382 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20     if( size<0 ) 
1c383 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20  size = -size;.  
1c384 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
1c385 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
1c386 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
1c387 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c388 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74  eAddOp(v, OP_Int
1c389 65 67 65 72 2c 20 73 69 7a 65 2c 20 30 29 3b 0a  eger, size, 0);.
1c38a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c38b 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65 61  eAddOp(v, OP_Rea
1c38c 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 32 29  dCookie, iDb, 2)
1c38d 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
1c38e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1c38f 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
1c390 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
1c391 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c392 4f 50 5f 47 65 2c 20 30 2c 20 61 64 64 72 2b 33  OP_Ge, 0, addr+3
1c393 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1c394 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1c395 4e 65 67 61 74 69 76 65 2c 20 30 2c 20 30 29 3b  Negative, 0, 0);
1c396 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1c397 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65  beAddOp(v, OP_Se
1c398 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 32 29  tCookie, iDb, 2)
1c399 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 53 63  ;.      pDb->pSc
1c39a 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
1c39b 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 73   = size;.      s
1c39c 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
1c39d 63 68 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74  cheSize(pDb->pBt
1c39e 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  , pDb->pSchema->
1c39f 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20  cache_size);.   
1c3a0 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a   }.  }else..  /*
1c3a1 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
1c3a2 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 73 69  atabase.]page_si
1c3a3 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  ze.  **  PRAGMA 
1c3a4 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f  [database.]page_
1c3a5 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a  size=N.  **.  **
1c3a6 20 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20   The first form 
1c3a7 72 65 70 6f 72 74 73 20 74 68 65 20 63 75 72 72  reports the curr
1c3a8 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72 20  ent setting for 
1c3a9 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
1c3aa 65 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 62  e page size in b
1c3ab 79 74 65 73 2e 20 20 54 68 65 20 73 65 63 6f 6e  ytes.  The secon
1c3ac 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65 0a  d form sets the.
1c3ad 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61    ** database pa
1c3ae 67 65 20 73 69 7a 65 20 76 61 6c 75 65 2e 20 20  ge size value.  
1c3af 54 68 65 20 76 61 6c 75 65 20 63 61 6e 20 6f 6e  The value can on
1c3b0 6c 79 20 62 65 20 73 65 74 20 69 66 0a 20 20 2a  ly be set if.  *
1c3b1 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  * the database h
1c3b2 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20  as not yet been 
1c3b3 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  created..  */.  
1c3b4 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1c3b5 6d 70 28 7a 4c 65 66 74 2c 22 70 61 67 65 5f 73  mp(zLeft,"page_s
1c3b6 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ize")==0 ){.    
1c3b7 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62  Btree *pBt = pDb
1c3b8 2d 3e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 21  ->pBt;.    if( !
1c3b9 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
1c3ba 69 6e 74 20 73 69 7a 65 20 3d 20 70 42 74 20 3f  int size = pBt ?
1c3bb 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
1c3bc 50 61 67 65 53 69 7a 65 28 70 42 74 29 20 3a 20  PageSize(pBt) : 
1c3bd 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  0;.      returnS
1c3be 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
1c3bf 20 22 70 61 67 65 5f 73 69 7a 65 22 2c 20 73 69   "page_size", si
1c3c0 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ze);.    }else{.
1c3c1 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20        /* Malloc 
1c3c2 6d 61 79 20 66 61 69 6c 20 77 68 65 6e 20 73 65  may fail when se
1c3c3 74 74 69 6e 67 20 74 68 65 20 70 61 67 65 2d 73  tting the page-s
1c3c4 69 7a 65 2c 20 61 73 20 74 68 65 72 65 20 69 73  ize, as there is
1c3c5 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0a 20 20 20   an internal.   
1c3c6 20 20 20 2a 2a 20 62 75 66 66 65 72 20 74 68 61     ** buffer tha
1c3c7 74 20 74 68 65 20 70 61 67 65 72 20 6d 6f 64 75  t the pager modu
1c3c8 6c 65 20 72 65 73 69 7a 65 73 20 75 73 69 6e 67  le resizes using
1c3c9 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
1c3ca 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ()..      */.   
1c3cb 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f     if( SQLITE_NO
1c3cc 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 42 74 72 65  MEM==sqlite3Btre
1c3cd 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 42 74  eSetPageSize(pBt
1c3ce 2c 20 61 74 6f 69 28 7a 52 69 67 68 74 29 2c 20  , atoi(zRight), 
1c3cf 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 64  -1) ){.        d
1c3d0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1c3d1 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
1c3d2 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a   }.  }else..  /*
1c3d3 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
1c3d4 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67  atabase.]max_pag
1c3d5 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50 52  e_count.  **  PR
1c3d6 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
1c3d7 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d 4e  max_page_count=N
1c3d8 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  **.  ** The f
1c3d9 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74  irst form report
1c3da 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  s the current se
1c3db 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20  tting for the.  
1c3dc 2a 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  ** maximum numbe
1c3dd 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
1c3de 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1c3df 20 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63 6f    The .  ** seco
1c3e0 6e 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74 73  nd form attempts
1c3e1 20 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20   to change this 
1c3e2 73 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a 20  setting.  Both. 
1c3e3 20 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72 6e   ** forms return
1c3e4 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
1c3e5 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ting..  */.  if(
1c3e6 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1c3e7 7a 4c 65 66 74 2c 22 6d 61 78 5f 70 61 67 65 5f  zLeft,"max_page_
1c3e8 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20  count")==0 ){.  
1c3e9 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70    Btree *pBt = p
1c3ea 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e 74  Db->pBt;.    int
1c3eb 20 6e 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20 20   newMax = 0;.   
1c3ec 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20   if( zRight ){. 
1c3ed 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 61 74       newMax = at
1c3ee 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  oi(zRight);.    
1c3ef 7d 0a 20 20 20 20 69 66 28 20 70 42 74 20 29 7b  }.    if( pBt ){
1c3f0 0a 20 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20  .      newMax = 
1c3f1 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50  sqlite3BtreeMaxP
1c3f2 61 67 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65  ageCount(pBt, ne
1c3f3 77 4d 61 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  wMax);.    }.   
1c3f4 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74   returnSingleInt
1c3f5 28 70 50 61 72 73 65 2c 20 22 6d 61 78 5f 70 61  (pParse, "max_pa
1c3f6 67 65 5f 63 6f 75 6e 74 22 2c 20 6e 65 77 4d 61  ge_count", newMa
1c3f7 78 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f  x);.  }else..  /
1c3f8 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
1c3f9 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e  database.]lockin
1c3fa 67 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41  g_mode.  **  PRA
1c3fb 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c  GMA [database.]l
1c3fc 6f 63 6b 69 6e 67 5f 6d 6f 64 65 20 3d 20 28 6e  ocking_mode = (n
1c3fd 6f 72 6d 61 6c 7c 65 78 63 6c 75 73 69 76 65 29  ormal|exclusive)
1c3fe 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
1c3ff 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
1c400 2c 22 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 29  ,"locking_mode")
1c401 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ==0 ){.    const
1c402 20 63 68 61 72 20 2a 7a 52 65 74 20 3d 20 22 6e   char *zRet = "n
1c403 6f 72 6d 61 6c 22 3b 0a 20 20 20 20 69 6e 74 20  ormal";.    int 
1c404 65 4d 6f 64 65 20 3d 20 67 65 74 4c 6f 63 6b 69  eMode = getLocki
1c405 6e 67 4d 6f 64 65 28 7a 52 69 67 68 74 29 3b 0a  ngMode(zRight);.
1c406 0a 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e  .    if( pId2->n
1c407 3d 3d 30 20 26 26 20 65 4d 6f 64 65 3d 3d 50 41  ==0 && eMode==PA
1c408 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
1c409 51 55 45 52 59 20 29 7b 0a 20 20 20 20 20 20 2f  QUERY ){.      /
1c40a 2a 20 53 69 6d 70 6c 65 20 22 50 52 41 47 4d 41  * Simple "PRAGMA
1c40b 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3b 22 20   locking_mode;" 
1c40c 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20  statement. This 
1c40d 69 73 20 61 20 71 75 65 72 79 20 66 6f 72 0a 20  is a query for. 
1c40e 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72       ** the curr
1c40f 65 6e 74 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b  ent default lock
1c410 69 6e 67 20 6d 6f 64 65 20 28 77 68 69 63 68 20  ing mode (which 
1c411 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74  may be different
1c412 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   to.      ** the
1c413 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 6f 66   locking-mode of
1c414 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1c415 73 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  se)..      */.  
1c416 20 20 20 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e      eMode = db->
1c417 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b 0a 20 20  dfltLockMode;.  
1c418 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50    }else{.      P
1c419 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20  ager *pPager;.  
1c41a 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d      if( pId2->n=
1c41b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
1c41c 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   This indicates 
1c41d 74 68 61 74 20 6e 6f 20 64 61 74 61 62 61 73 65  that no database
1c41e 20 6e 61 6d 65 20 77 61 73 20 73 70 65 63 69 66   name was specif
1c41f 69 65 64 20 61 73 20 70 61 72 74 0a 20 20 20 20  ied as part.    
1c420 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 50 52      ** of the PR
1c421 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e  AGMA command. In
1c422 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6c   this case the l
1c423 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 6d 75 73 74  ocking-mode must
1c424 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73   be.        ** s
1c425 65 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68  et on all attach
1c426 65 64 20 64 61 74 61 62 61 73 65 73 2c 20 61 73  ed databases, as
1c427 20 77 65 6c 6c 20 61 73 20 74 68 65 20 6d 61 69   well as the mai
1c428 6e 20 64 62 20 66 69 6c 65 2e 0a 20 20 20 20 20  n db file..     
1c429 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
1c42a 20 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c 69 74   Also, the sqlit
1c42b 65 33 2e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20  e3.dfltLockMode 
1c42c 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20  variable is set 
1c42d 73 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 20  so that.        
1c42e 2a 2a 20 61 6e 79 20 73 75 62 73 65 71 75 65 6e  ** any subsequen
1c42f 74 6c 79 20 61 74 74 61 63 68 65 64 20 64 61 74  tly attached dat
1c430 61 62 61 73 65 73 20 61 6c 73 6f 20 75 73 65 20  abases also use 
1c431 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 20 20  the specified.  
1c432 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67        ** locking
1c433 20 6d 6f 64 65 2e 0a 20 20 20 20 20 20 20 20 2a   mode..        *
1c434 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69  /.        int ii
1c435 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1c436 28 70 44 62 3d 3d 26 64 62 2d 3e 61 44 62 5b 30  (pDb==&db->aDb[0
1c437 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  ]);.        for(
1c438 69 69 3d 32 3b 20 69 69 3c 64 62 2d 3e 6e 44 62  ii=2; ii<db->nDb
1c439 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ; ii++){.       
1c43a 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69     pPager = sqli
1c43b 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62  te3BtreePager(db
1c43c 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74 29 3b 0a  ->aDb[ii].pBt);.
1c43d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1c43e 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64  3PagerLockingMod
1c43f 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29  e(pPager, eMode)
1c440 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1c441 20 20 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b      db->dfltLock
1c442 4d 6f 64 65 20 3d 20 65 4d 6f 64 65 3b 0a 20 20  Mode = eMode;.  
1c443 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
1c444 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
1c445 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29  ePager(pDb->pBt)
1c446 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  ;.      eMode = 
1c447 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
1c448 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20  ingMode(pPager, 
1c449 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a 20  eMode);.    }.. 
1c44a 20 20 20 61 73 73 65 72 74 28 65 4d 6f 64 65 3d     assert(eMode=
1c44b 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f  =PAGER_LOCKINGMO
1c44c 44 45 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64 65  DE_NORMAL||eMode
1c44d 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
1c44e 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a  ODE_EXCLUSIVE);.
1c44f 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 50      if( eMode==P
1c450 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
1c451 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20  _EXCLUSIVE ){.  
1c452 20 20 20 20 7a 52 65 74 20 3d 20 22 65 78 63 6c      zRet = "excl
1c453 75 73 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20 20  usive";.    }.  
1c454 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1c455 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
1c456 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1c457 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
1c458 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f  OLNAME_NAME, "lo
1c459 63 6b 69 6e 67 5f 6d 6f 64 65 22 2c 20 50 33 5f  cking_mode", P3_
1c45a 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c  STATIC);.    sql
1c45b 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
1c45c 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c  P_String8, 0, 0,
1c45d 20 7a 52 65 74 2c 20 30 29 3b 0a 20 20 20 20 73   zRet, 0);.    s
1c45e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1c45f 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20  v, OP_Callback, 
1c460 31 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  1, 0);.  }else.#
1c461 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1c462 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
1c463 41 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  AS */..  /*.  **
1c464 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
1c465 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 0a  se.]auto_vacuum.
1c466 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
1c467 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63  tabase.]auto_vac
1c468 75 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uum=N.  **.  ** 
1c469 47 65 74 20 6f 72 20 73 65 74 20 74 68 65 20 28  Get or set the (
1c46a 62 6f 6f 6c 65 61 6e 29 20 76 61 6c 75 65 20 6f  boolean) value o
1c46b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 27  f the database '
1c46c 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 61 72  auto-vacuum' par
1c46d 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 23 69 66  ameter..  */.#if
1c46e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1c46f 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66  _AUTOVACUUM.  if
1c470 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1c471 28 7a 4c 65 66 74 2c 22 61 75 74 6f 5f 76 61 63  (zLeft,"auto_vac
1c472 75 75 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  uum")==0 ){.    
1c473 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62  Btree *pBt = pDb
1c474 2d 3e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73  ->pBt;.    if( s
1c475 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
1c476 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
1c477 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75    goto pragma_ou
1c478 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
1c479 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
1c47a 20 20 69 6e 74 20 61 75 74 6f 5f 76 61 63 75 75    int auto_vacuu
1c47b 6d 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 70  m = .          p
1c47c 42 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65  Bt ? sqlite3Btre
1c47d 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70  eGetAutoVacuum(p
1c47e 42 74 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46  Bt) : SQLITE_DEF
1c47f 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3b  AULT_AUTOVACUUM;
1c480 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e  .      returnSin
1c481 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22  gleInt(pParse, "
1c482 61 75 74 6f 5f 76 61 63 75 75 6d 22 2c 20 61 75  auto_vacuum", au
1c483 74 6f 5f 76 61 63 75 75 6d 29 3b 0a 20 20 20 20  to_vacuum);.    
1c484 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
1c485 20 65 41 75 74 6f 20 3d 20 67 65 74 41 75 74 6f   eAuto = getAuto
1c486 56 61 63 75 75 6d 28 7a 52 69 67 68 74 29 3b 0a  Vacuum(zRight);.
1c487 20 20 20 20 20 20 69 66 28 20 65 41 75 74 6f 3e        if( eAuto>
1c488 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
1c489 20 43 61 6c 6c 20 53 65 74 41 75 74 6f 56 61 63   Call SetAutoVac
1c48a 75 75 6d 28 29 20 74 6f 20 73 65 74 20 69 6e 69  uum() to set ini
1c48b 74 69 61 6c 69 7a 65 20 74 68 65 20 69 6e 74 65  tialize the inte
1c48c 72 6e 61 6c 20 61 75 74 6f 20 61 6e 64 0a 20 20  rnal auto and.  
1c48d 20 20 20 20 20 20 2a 2a 20 69 6e 63 72 2d 76 61        ** incr-va
1c48e 63 75 75 6d 20 66 6c 61 67 73 2e 20 54 68 69 73  cuum flags. This
1c48f 20 69 73 20 72 65 71 75 69 72 65 64 20 69 6e 20   is required in 
1c490 63 61 73 65 20 74 68 69 73 20 63 6f 6e 6e 65 63  case this connec
1c491 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  tion.        ** 
1c492 63 72 65 61 74 65 73 20 74 68 65 20 64 61 74 61  creates the data
1c493 62 61 73 65 20 66 69 6c 65 2e 20 49 74 20 69 73  base file. It is
1c494 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
1c495 69 74 20 69 73 20 63 72 65 61 74 65 64 0a 20 20  it is created.  
1c496 20 20 20 20 20 20 2a 2a 20 61 73 20 61 6e 20 61        ** as an a
1c497 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62  uto-vacuum capab
1c498 6c 65 20 64 62 2e 0a 20 20 20 20 20 20 20 20 2a  le db..        *
1c499 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63  /.        int rc
1c49a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53   = sqlite3BtreeS
1c49b 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74  etAutoVacuum(pBt
1c49c 2c 20 65 41 75 74 6f 29 3b 0a 20 20 20 20 20 20  , eAuto);.      
1c49d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1c49e 5f 4f 4b 20 26 26 20 28 65 41 75 74 6f 3d 3d 31  _OK && (eAuto==1
1c49f 20 7c 7c 20 65 41 75 74 6f 3d 3d 32 29 20 29 7b   || eAuto==2) ){
1c4a0 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68  .          /* Wh
1c4a1 65 6e 20 73 65 74 74 69 6e 67 20 74 68 65 20 61  en setting the a
1c4a2 75 74 6f 5f 76 61 63 75 75 6d 20 6d 6f 64 65 20  uto_vacuum mode 
1c4a3 74 6f 20 65 69 74 68 65 72 20 22 66 75 6c 6c 22  to either "full"
1c4a4 20 6f 72 20 0a 20 20 20 20 20 20 20 20 20 20 2a   or .          *
1c4a5 2a 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 2c  * "incremental",
1c4a6 20 77 72 69 74 65 20 74 68 65 20 76 61 6c 75 65   write the value
1c4a7 20 6f 66 20 6d 65 74 61 5b 36 5d 20 69 6e 20 74   of meta[6] in t
1c4a8 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
1c4a9 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42        ** file. B
1c4aa 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f  efore writing to
1c4ab 20 6d 65 74 61 5b 36 5d 2c 20 63 68 65 63 6b 20   meta[6], check 
1c4ac 74 68 61 74 20 6d 65 74 61 5b 33 5d 20 69 6e 64  that meta[3] ind
1c4ad 69 63 61 74 65 73 0a 20 20 20 20 20 20 20 20 20  icates.         
1c4ae 20 2a 2a 20 74 68 61 74 20 74 68 69 73 20 72 65   ** that this re
1c4af 61 6c 6c 79 20 69 73 20 61 6e 20 61 75 74 6f 2d  ally is an auto-
1c4b0 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64  vacuum capable d
1c4b1 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20  atabase..       
1c4b2 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
1c4b3 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
1c4b4 65 4f 70 4c 69 73 74 20 73 65 74 4d 65 74 61 36  eOpList setMeta6
1c4b5 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20  [] = {.         
1c4b6 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74     { OP_Transact
1c4b7 69 6f 6e 2c 20 20 20 20 30 2c 20 20 20 20 20 20  ion,    0,      
1c4b8 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20 20           1,     
1c4b9 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a     0},    /* 0 *
1c4ba 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
1c4bb 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20  OP_ReadCookie,  
1c4bc 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
1c4bd 20 20 20 20 33 2c 20 20 20 20 20 20 20 20 30 7d      3,        0}
1c4be 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20  ,    /* 1 */.   
1c4bf 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66           { OP_If
1c4c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c  ,             0,
1c4c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1c4c2 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
1c4c3 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 2 */.        
1c4c4 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20 20      { OP_Halt,  
1c4c5 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
1c4c6 4f 4b 2c 20 20 20 20 20 20 20 4f 45 5f 41 62 6f  OK,       OE_Abo
1c4c7 72 74 2c 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20  rt, 0},    /* 3 
1c4c8 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
1c4c9 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20   OP_Integer,    
1c4ca 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
1c4cb 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 30       0,        0
1c4cc 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20  },    /* 4 */.  
1c4cd 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
1c4ce 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30  etCookie,      0
1c4cf 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1c4d0 36 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20  6,        0},   
1c4d1 20 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 20   /* 5 */.       
1c4d2 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20     };.          
1c4d3 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20  int iAddr;.     
1c4d4 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c       iAddr = sql
1c4d5 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
1c4d6 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73  t(v, ArraySize(s
1c4d7 65 74 4d 65 74 61 36 29 2c 20 73 65 74 4d 65 74  etMeta6), setMet
1c4d8 61 36 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  a6);.          s
1c4d9 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1c4da 50 31 28 76 2c 20 69 41 64 64 72 2c 20 69 44 62  P1(v, iAddr, iDb
1c4db 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1c4dc 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
1c4dd 28 76 2c 20 69 41 64 64 72 2b 31 2c 20 69 44 62  (v, iAddr+1, iDb
1c4de 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1c4df 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
1c4e0 28 76 2c 20 69 41 64 64 72 2b 32 2c 20 69 41 64  (v, iAddr+2, iAd
1c4e1 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20  dr+4);.         
1c4e2 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1c4e3 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b 34 2c  geP1(v, iAddr+4,
1c4e4 20 65 41 75 74 6f 2d 31 29 3b 0a 20 20 20 20 20   eAuto-1);.     
1c4e5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c4e6 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64  ChangeP1(v, iAdd
1c4e7 72 2b 35 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  r+5, iDb);.     
1c4e8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c4e9 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62  UsesBtree(v, iDb
1c4ea 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1c4eb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
1c4ec 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a  se.#endif..  /*.
1c4ed 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
1c4ee 74 61 62 61 73 65 2e 5d 69 6e 63 72 65 6d 65 6e  tabase.]incremen
1c4ef 74 61 6c 5f 76 61 63 75 75 6d 28 4e 29 0a 20 20  tal_vacuum(N).  
1c4f0 2a 2a 0a 20 20 2a 2a 20 44 6f 20 4e 20 73 74 65  **.  ** Do N ste
1c4f1 70 73 20 6f 66 20 69 6e 63 72 65 6d 65 6e 74 61  ps of incrementa
1c4f2 6c 20 76 61 63 75 75 6d 69 6e 67 20 6f 6e 20 61  l vacuuming on a
1c4f3 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a   database..  */.
1c4f4 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c4f5 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1c4f6 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1c4f7 43 6d 70 28 7a 4c 65 66 74 2c 22 69 6e 63 72 65  Cmp(zLeft,"incre
1c4f8 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 29 3d  mental_vacuum")=
1c4f9 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4c  =0 ){.    int iL
1c4fa 69 6d 69 74 2c 20 61 64 64 72 3b 0a 20 20 20 20  imit, addr;.    
1c4fb 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
1c4fc 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
1c4fd 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67  .      goto prag
1c4fe 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  ma_out;.    }.  
1c4ff 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20    if( zRight==0 
1c500 7c 7c 20 21 73 71 6c 69 74 65 33 47 65 74 49 6e  || !sqlite3GetIn
1c501 74 33 32 28 7a 52 69 67 68 74 2c 20 26 69 4c 69  t32(zRight, &iLi
1c502 6d 69 74 29 20 7c 7c 20 69 4c 69 6d 69 74 3c 3d  mit) || iLimit<=
1c503 30 20 29 7b 0a 20 20 20 20 20 20 69 4c 69 6d 69  0 ){.      iLimi
1c504 74 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a  t = 0x7fffffff;.
1c505 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1c506 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
1c507 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20  tion(pParse, 0, 
1c508 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
1c509 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1c50a 5f 4d 65 6d 49 6e 74 2c 20 69 4c 69 6d 69 74 2c  _MemInt, iLimit,
1c50b 20 30 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20   0);.    addr = 
1c50c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c50d 28 76 2c 20 4f 50 5f 49 6e 63 72 56 61 63 75 75  (v, OP_IncrVacuu
1c50e 6d 2c 20 69 44 62 2c 20 30 29 3b 0a 20 20 20 20  m, iDb, 0);.    
1c50f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c510 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c  (v, OP_Callback,
1c511 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
1c512 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c513 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 2d 31 2c 20  OP_MemIncr, -1, 
1c514 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1c515 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
1c516 66 4d 65 6d 50 6f 73 2c 20 30 2c 20 61 64 64 72  fMemPos, 0, addr
1c517 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1c518 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
1c519 64 72 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  dr);.  }else.#en
1c51a 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
1c51b 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
1c51c 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a  RAGMAS.  /*.  **
1c51d 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
1c51e 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 0a 20  se.]cache_size. 
1c51f 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
1c520 61 62 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a  abase.]cache_siz
1c521 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  e=N.  **.  ** Th
1c522 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70  e first form rep
1c523 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74  orts the current
1c524 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 66   local setting f
1c525 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65  or the.  ** page
1c526 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68   cache size.  Th
1c527 65 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20  e local setting 
1c528 63 61 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74  can be different
1c529 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 70   from.  ** the p
1c52a 65 72 73 69 73 74 65 6e 74 20 63 61 63 68 65 20  ersistent cache 
1c52b 73 69 7a 65 20 76 61 6c 75 65 20 74 68 61 74 20  size value that 
1c52c 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  is stored in the
1c52d 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66   database.  ** f
1c52e 69 6c 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65  ile itself.  The
1c52f 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
1c530 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  is the maximum n
1c531 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61  umber of.  ** pa
1c532 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20  ges in the page 
1c533 63 61 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f  cache.  The seco
1c534 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65  nd form sets the
1c535 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20 70 61 67 65   local.  ** page
1c536 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75   cache size valu
1c537 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  e.  It does not 
1c538 63 68 61 6e 67 65 20 74 68 65 20 70 65 72 73 69  change the persi
1c539 73 74 65 6e 74 0a 20 20 2a 2a 20 63 61 63 68 65  stent.  ** cache
1c53a 20 73 69 7a 65 20 73 74 6f 72 65 64 20 6f 6e 20   size stored on 
1c53b 74 68 65 20 64 69 73 6b 20 73 6f 20 74 68 65 20  the disk so the 
1c53c 63 61 63 68 65 20 73 69 7a 65 20 77 69 6c 6c 20  cache size will 
1c53d 72 65 76 65 72 74 0a 20 20 2a 2a 20 74 6f 20 69  revert.  ** to i
1c53e 74 73 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  ts default value
1c53f 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
1c540 73 65 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64  se is closed and
1c541 20 72 65 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 20   reopened..  ** 
1c542 4e 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f  N should be a po
1c543 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2e 0a  sitive integer..
1c544 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
1c545 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
1c546 22 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30  "cache_size")==0
1c547 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
1c548 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
1c549 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
1c54a 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28  gma_out;.    if(
1c54b 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
1c54c 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
1c54d 74 28 70 50 61 72 73 65 2c 20 22 63 61 63 68 65  t(pParse, "cache
1c54e 5f 73 69 7a 65 22 2c 20 70 44 62 2d 3e 70 53 63  _size", pDb->pSc
1c54f 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
1c550 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1c551 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61      int size = a
1c552 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  toi(zRight);.   
1c553 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20     if( size<0 ) 
1c554 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20  size = -size;.  
1c555 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
1c556 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73  ->cache_size = s
1c557 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ize;.      sqlit
1c558 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
1c559 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
1c55a 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
1c55b 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20  e_size);.    }. 
1c55c 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
1c55d 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f  *   PRAGMA temp_
1c55e 73 74 6f 72 65 0a 20 20 2a 2a 20 20 20 50 52 41  store.  **   PRA
1c55f 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 20 3d  GMA temp_store =
1c560 20 22 64 65 66 61 75 6c 74 22 7c 22 6d 65 6d 6f   "default"|"memo
1c561 72 79 22 7c 22 66 69 6c 65 22 0a 20 20 2a 2a 0a  ry"|"file".  **.
1c562 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73    ** Return or s
1c563 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c  et the local val
1c564 75 65 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73  ue of the temp_s
1c565 74 6f 72 65 20 66 6c 61 67 2e 20 20 43 68 61 6e  tore flag.  Chan
1c566 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f  ging.  ** the lo
1c567 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e  cal value does n
1c568 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20  ot make changes 
1c569 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65  to the disk file
1c56a 20 61 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74   and the default
1c56b 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c  .  ** value will
1c56c 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 68 65   be restored the
1c56d 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 64   next time the d
1c56e 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
1c56f 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74  d..  **.  ** Not
1c570 65 20 74 68 61 74 20 69 74 20 69 73 20 70 6f 73  e that it is pos
1c571 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 6c 69  sible for the li
1c572 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69  brary compile-ti
1c573 6d 65 20 6f 70 74 69 6f 6e 73 20 74 6f 0a 20 20  me options to.  
1c574 2a 2a 20 6f 76 65 72 72 69 64 65 20 74 68 69 73  ** override this
1c575 20 73 65 74 74 69 6e 67 0a 20 20 2a 2f 0a 20 20   setting.  */.  
1c576 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1c577 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f  mp(zLeft, "temp_
1c578 73 74 6f 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20  store")==0 ){.  
1c579 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
1c57a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e  .      returnSin
1c57b 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22  gleInt(pParse, "
1c57c 74 65 6d 70 5f 73 74 6f 72 65 22 2c 20 64 62 2d  temp_store", db-
1c57d 3e 74 65 6d 70 5f 73 74 6f 72 65 29 3b 0a 20 20  >temp_store);.  
1c57e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
1c57f 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65  hangeTempStorage
1c580 28 70 50 61 72 73 65 2c 20 7a 52 69 67 68 74 29  (pParse, zRight)
1c581 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
1c582 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41  .  /*.  **   PRA
1c583 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64  GMA temp_store_d
1c584 69 72 65 63 74 6f 72 79 0a 20 20 2a 2a 20 20 20  irectory.  **   
1c585 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72  PRAGMA temp_stor
1c586 65 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 22 22  e_directory = ""
1c587 7c 22 64 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65  |"directory_name
1c588 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  ".  **.  ** Retu
1c589 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f  rn or set the lo
1c58a 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65  cal value of the
1c58b 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65   temp_store_dire
1c58c 63 74 6f 72 79 20 66 6c 61 67 2e 20 20 43 68 61  ctory flag.  Cha
1c58d 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 76  nging.  ** the v
1c58e 61 6c 75 65 20 73 65 74 73 20 61 20 73 70 65 63  alue sets a spec
1c58f 69 66 69 63 20 64 69 72 65 63 74 6f 72 79 20 74  ific directory t
1c590 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 65  o be used for te
1c591 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 20  mporary files.. 
1c592 20 2a 2a 20 53 65 74 74 69 6e 67 20 74 6f 20 61   ** Setting to a
1c593 20 6e 75 6c 6c 20 73 74 72 69 6e 67 20 72 65 76   null string rev
1c594 65 72 74 73 20 74 6f 20 74 68 65 20 64 65 66 61  erts to the defa
1c595 75 6c 74 20 74 65 6d 70 6f 72 61 72 79 20 64 69  ult temporary di
1c596 72 65 63 74 6f 72 79 20 73 65 61 72 63 68 2e 0a  rectory search..
1c597 20 20 2a 2a 20 49 66 20 74 65 6d 70 6f 72 61 72    ** If temporar
1c598 79 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 63  y directory is c
1c599 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 69 6e 76  hanged, then inv
1c59a 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61  alidateTempStora
1c59b 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20  ge..  **.  */.  
1c59c 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1c59d 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f  mp(zLeft, "temp_
1c59e 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22  store_directory"
1c59f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
1c5a0 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
1c5a1 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d   if( sqlite3_tem
1c5a2 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20  p_directory ){. 
1c5a3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c5a4 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
1c5a5 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
1c5a6 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1c5a7 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
1c5a8 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20  NAME, .         
1c5a9 20 20 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64     "temp_store_d
1c5aa 69 72 65 63 74 6f 72 79 22 2c 20 50 33 5f 53 54  irectory", P3_ST
1c5ab 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
1c5ac 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c  qlite3VdbeOp3(v,
1c5ad 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
1c5ae 30 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  0, sqlite3_temp_
1c5af 64 69 72 65 63 74 6f 72 79 2c 20 30 29 3b 0a 20  directory, 0);. 
1c5b0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c5b1 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61  beAddOp(v, OP_Ca
1c5b2 6c 6c 62 61 63 6b 2c 20 31 2c 20 30 29 3b 0a 20  llback, 1, 0);. 
1c5b3 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
1c5b4 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 52 69 67  {.      if( zRig
1c5b5 68 74 5b 30 5d 20 0a 20 20 20 20 20 20 20 26 26  ht[0] .       &&
1c5b6 20 21 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73   !sqlite3OsAcces
1c5b7 73 28 64 62 2d 3e 70 56 66 73 2c 20 7a 52 69 67  s(db->pVfs, zRig
1c5b8 68 74 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ht, SQLITE_ACCES
1c5b9 53 5f 52 45 41 44 57 52 49 54 45 29 20 0a 20 20  S_READWRITE) .  
1c5ba 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 73      ){.        s
1c5bb 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1c5bc 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 20 77 72  Parse, "not a wr
1c5bd 69 74 61 62 6c 65 20 64 69 72 65 63 74 6f 72 79  itable directory
1c5be 22 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  ");.        goto
1c5bf 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
1c5c0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 54     }.      if( T
1c5c1 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20  EMP_STORE==0.   
1c5c2 20 20 20 20 7c 7c 20 28 54 45 4d 50 5f 53 54 4f      || (TEMP_STO
1c5c3 52 45 3d 3d 31 20 26 26 20 64 62 2d 3e 74 65 6d  RE==1 && db->tem
1c5c4 70 5f 73 74 6f 72 65 3c 3d 31 29 0a 20 20 20 20  p_store<=1).    
1c5c5 20 20 20 7c 7c 20 28 54 45 4d 50 5f 53 54 4f 52     || (TEMP_STOR
1c5c6 45 3d 3d 32 20 26 26 20 64 62 2d 3e 74 65 6d 70  E==2 && db->temp
1c5c7 5f 73 74 6f 72 65 3d 3d 31 29 0a 20 20 20 20 20  _store==1).     
1c5c8 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61   ){.        inva
1c5c9 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67  lidateTempStorag
1c5ca 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  e(pParse);.     
1c5cb 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1c5cc 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 74 65  _free(sqlite3_te
1c5cd 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20  mp_directory);. 
1c5ce 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b       if( zRight[
1c5cf 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  0] ){.        sq
1c5d0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
1c5d1 74 6f 72 79 20 3d 20 7a 52 69 67 68 74 3b 0a 20  tory = zRight;. 
1c5d2 20 20 20 20 20 20 20 7a 52 69 67 68 74 20 3d 20         zRight = 
1c5d3 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
1c5d4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1c5d5 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
1c5d6 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1c5d7 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a  }.  }else..  /*.
1c5d8 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
1c5d9 61 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f  atabase.]synchro
1c5da 6e 6f 75 73 0a 20 20 2a 2a 20 20 20 50 52 41 47  nous.  **   PRAG
1c5db 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79  MA [database.]sy
1c5dc 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c 4f 4e  nchronous=OFF|ON
1c5dd 7c 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20 20 2a  |NORMAL|FULL.  *
1c5de 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72  *.  ** Return or
1c5df 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76   set the local v
1c5e0 61 6c 75 65 20 6f 66 20 74 68 65 20 73 79 6e 63  alue of the sync
1c5e1 68 72 6f 6e 6f 75 73 20 66 6c 61 67 2e 20 20 43  hronous flag.  C
1c5e2 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65  hanging.  ** the
1c5e3 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f 65   local value doe
1c5e4 73 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67  s not make chang
1c5e5 65 73 20 74 6f 20 74 68 65 20 64 69 73 6b 20 66  es to the disk f
1c5e6 69 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a  ile and the.  **
1c5e7 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77   default value w
1c5e8 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20  ill be restored 
1c5e9 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68  the next time th
1c5ea 65 20 64 61 74 61 62 61 73 65 20 69 73 0a 20 20  e database is.  
1c5eb 2a 2a 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a  ** opened..  */.
1c5ec 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1c5ed 49 43 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e 63  ICmp(zLeft,"sync
1c5ee 68 72 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b 0a  hronous")==0 ){.
1c5ef 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
1c5f0 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
1c5f1 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
1c5f2 6f 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52  out;.    if( !zR
1c5f3 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65  ight ){.      re
1c5f4 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
1c5f5 61 72 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e 6f  arse, "synchrono
1c5f6 75 73 22 2c 20 70 44 62 2d 3e 73 61 66 65 74 79  us", pDb->safety
1c5f7 5f 6c 65 76 65 6c 2d 31 29 3b 0a 20 20 20 20 7d  _level-1);.    }
1c5f8 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
1c5f9 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20  !db->autoCommit 
1c5fa 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1c5fb 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1c5fc 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
1c5fd 22 53 61 66 65 74 79 20 6c 65 76 65 6c 20 6d 61  "Safety level ma
1c5fe 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64  y not be changed
1c5ff 20 69 6e 73 69 64 65 20 61 20 74 72 61 6e 73 61   inside a transa
1c600 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 7d  ction");.      }
1c601 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44  else{.        pD
1c602 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20  b->safety_level 
1c603 3d 20 67 65 74 53 61 66 65 74 79 4c 65 76 65 6c  = getSafetyLevel
1c604 28 7a 52 69 67 68 74 29 2b 31 3b 0a 20 20 20 20  (zRight)+1;.    
1c605 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
1c606 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
1c607 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
1c608 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65  AGMAS */..#ifnde
1c609 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
1c60a 41 47 5f 50 52 41 47 4d 41 53 0a 20 20 69 66 28  AG_PRAGMAS.  if(
1c60b 20 66 6c 61 67 50 72 61 67 6d 61 28 70 50 61 72   flagPragma(pPar
1c60c 73 65 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68  se, zLeft, zRigh
1c60d 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  t) ){.    /* The
1c60e 20 66 6c 61 67 50 72 61 67 6d 61 28 29 20 73 75   flagPragma() su
1c60f 62 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65  broutine also ge
1c610 6e 65 72 61 74 65 73 20 61 6e 79 20 6e 65 63 65  nerates any nece
1c611 73 73 61 72 79 20 63 6f 64 65 0a 20 20 20 20 2a  ssary code.    *
1c612 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69  * there is nothi
1c613 6e 67 20 6d 6f 72 65 20 74 6f 20 64 6f 20 68 65  ng more to do he
1c614 72 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 0a 23 65  re */.  }else.#e
1c615 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1c616 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53  MIT_FLAG_PRAGMAS
1c617 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1c618 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  ITE_OMIT_SCHEMA_
1c619 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a  PRAGMAS.  /*.  *
1c61a 2a 20 20 20 50 52 41 47 4d 41 20 74 61 62 6c 65  *   PRAGMA table
1c61b 5f 69 6e 66 6f 28 3c 74 61 62 6c 65 3e 29 0a 20  _info(<table>). 
1c61c 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20   **.  ** Return 
1c61d 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 66 6f 72  a single row for
1c61e 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   each column of 
1c61f 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e  the named table.
1c620 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a   The columns of.
1c621 20 20 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 65    ** the returne
1c622 64 20 64 61 74 61 20 73 65 74 20 61 72 65 3a 0a  d data set are:.
1c623 20 20 2a 2a 0a 20 20 2a 2a 20 63 69 64 3a 20 20    **.  ** cid:  
1c624 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 69 64 20        Column id 
1c625 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 6c  (numbered from l
1c626 65 66 74 20 74 6f 20 72 69 67 68 74 2c 20 73 74  eft to right, st
1c627 61 72 74 69 6e 67 20 61 74 20 30 29 0a 20 20 2a  arting at 0).  *
1c628 2a 20 6e 61 6d 65 3a 20 20 20 20 20 20 20 43 6f  * name:       Co
1c629 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a 2a 20 74  lumn name.  ** t
1c62a 79 70 65 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d  ype:       Colum
1c62b 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79  n declaration ty
1c62c 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e 75 6c 6c  pe..  ** notnull
1c62d 3a 20 20 20 20 54 72 75 65 20 69 66 20 27 4e 4f  :    True if 'NO
1c62e 54 20 4e 55 4c 4c 27 20 69 73 20 70 61 72 74 20  T NULL' is part 
1c62f 6f 66 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72  of column declar
1c630 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 66 6c 74 5f  ation.  ** dflt_
1c631 76 61 6c 75 65 3a 20 54 68 65 20 64 65 66 61 75  value: The defau
1c632 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
1c633 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e 79 2e   column, if any.
1c634 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
1c635 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
1c636 2c 20 22 74 61 62 6c 65 5f 69 6e 66 6f 22 29 3d  , "table_info")=
1c637 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a  =0 && zRight ){.
1c638 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
1c639 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1c63a 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
1c63b 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
1c63c 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20 3d  _out;.    pTab =
1c63d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
1c63e 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44  e(db, zRight, zD
1c63f 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  b);.    if( pTab
1c640 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
1c641 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 69 64 64  .      int nHidd
1c642 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 43 6f  en = 0;.      Co
1c643 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 20 20  lumn *pCol;.    
1c644 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1c645 4e 75 6d 43 6f 6c 73 28 76 2c 20 36 29 3b 0a 20  NumCols(v, 6);. 
1c646 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c647 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
1c648 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
1c649 63 69 64 22 2c 20 50 33 5f 53 54 41 54 49 43 29  cid", P3_STATIC)
1c64a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1c64b 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
1c64c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
1c64d 2c 20 22 6e 61 6d 65 22 2c 20 50 33 5f 53 54 41  , "name", P3_STA
1c64e 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
1c64f 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1c650 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 2, COLNAME_
1c651 4e 41 4d 45 2c 20 22 74 79 70 65 22 2c 20 50 33  NAME, "type", P3
1c652 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
1c653 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1c654 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e  lName(v, 3, COLN
1c655 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f 74 6e 75  AME_NAME, "notnu
1c656 6c 6c 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b  ll", P3_STATIC);
1c657 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1c658 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
1c659 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  4, COLNAME_NAME,
1c65a 20 22 64 66 6c 74 5f 76 61 6c 75 65 22 2c 20 50   "dflt_value", P
1c65b 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  3_STATIC);.     
1c65c 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
1c65d 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c  olName(v, 5, COL
1c65e 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c  NAME_NAME, "pk",
1c65f 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P3_STATIC);.   
1c660 20 20 20 73 71 6c 69 74 65 33 56 69 65 77 47 65     sqlite3ViewGe
1c661 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
1c662 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
1c663 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d    for(i=0, pCol=
1c664 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c 70 54  pTab->aCol; i<pT
1c665 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70  ab->nCol; i++, p
1c666 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Col++){.        
1c667 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 44 66  const Token *pDf
1c668 6c 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  lt;.        if( 
1c669 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 70  IsHiddenColumn(p
1c66a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Col) ){.        
1c66b 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20    nHidden++;.   
1c66c 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
1c66d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1c66e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1c66f 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
1c670 72 2c 20 69 2d 6e 48 69 64 64 65 6e 2c 20 30 29  r, i-nHidden, 0)
1c671 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1c672 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53  3VdbeOp3(v, OP_S
1c673 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c 20 70 43  tring8, 0, 0, pC
1c674 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ol->zName, 0);. 
1c675 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c676 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69  beOp3(v, OP_Stri
1c677 6e 67 38 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  ng8, 0, 0,.     
1c678 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70        pCol->zTyp
1c679 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20  e ? pCol->zType 
1c67a 3a 20 22 22 2c 20 30 29 3b 0a 20 20 20 20 20 20  : "", 0);.      
1c67b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1c67c 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  Op(v, OP_Integer
1c67d 2c 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 2c  , pCol->notNull,
1c67e 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
1c67f 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 26 26 20   pCol->pDflt && 
1c680 28 70 44 66 6c 74 20 3d 20 26 70 43 6f 6c 2d 3e  (pDflt = &pCol->
1c681 70 44 66 6c 74 2d 3e 73 70 61 6e 29 2d 3e 7a 20  pDflt->span)->z 
1c682 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1c683 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
1c684 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c  P_String8, 0, 0,
1c685 20 28 63 68 61 72 2a 29 70 44 66 6c 74 2d 3e 7a   (char*)pDflt->z
1c686 2c 20 70 44 66 6c 74 2d 3e 6e 29 3b 0a 20 20 20  , pDflt->n);.   
1c687 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1c688 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c689 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 75 6c  eAddOp(v, OP_Nul
1c68a 6c 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  l, 0, 0);.      
1c68b 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
1c68c 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c68d 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c  OP_Integer, pCol
1c68e 2d 3e 69 73 50 72 69 6d 4b 65 79 2c 20 30 29 3b  ->isPrimKey, 0);
1c68f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c690 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1c691 43 61 6c 6c 62 61 63 6b 2c 20 36 2c 20 30 29 3b  Callback, 6, 0);
1c692 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1c693 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71   }else..  if( sq
1c694 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
1c695 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e 66 6f 22  ft, "index_info"
1c696 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
1c697 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  {.    Index *pId
1c698 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  x;.    Table *pT
1c699 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ab;.    if( sqli
1c69a 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
1c69b 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
1c69c 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 49 64  gma_out;.    pId
1c69d 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  x = sqlite3FindI
1c69e 6e 64 65 78 28 64 62 2c 20 7a 52 69 67 68 74 2c  ndex(db, zRight,
1c69f 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
1c6a0 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Idx ){.      int
1c6a1 20 69 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   i;.      pTab =
1c6a2 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20   pIdx->pTable;. 
1c6a3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c6a4 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29  SetNumCols(v, 3)
1c6a5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1c6a6 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
1c6a7 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
1c6a8 2c 20 22 73 65 71 6e 6f 22 2c 20 50 33 5f 53 54  , "seqno", P3_ST
1c6a9 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
1c6aa 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
1c6ab 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  me(v, 1, COLNAME
1c6ac 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20 50 33  _NAME, "cid", P3
1c6ad 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
1c6ae 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1c6af 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e  lName(v, 2, COLN
1c6b0 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
1c6b1 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P3_STATIC);.  
1c6b2 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1c6b3 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
1c6b4 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  +){.        int 
1c6b5 63 6e 75 6d 20 3d 20 70 49 64 78 2d 3e 61 69 43  cnum = pIdx->aiC
1c6b6 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20  olumn[i];.      
1c6b7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1c6b8 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  Op(v, OP_Integer
1c6b9 2c 20 69 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , i, 0);.       
1c6ba 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1c6bb 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  p(v, OP_Integer,
1c6bc 20 63 6e 75 6d 2c 20 30 29 3b 0a 20 20 20 20 20   cnum, 0);.     
1c6bd 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
1c6be 3e 6e 43 6f 6c 3e 63 6e 75 6d 20 29 3b 0a 20 20  >nCol>cnum );.  
1c6bf 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c6c0 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e  eOp3(v, OP_Strin
1c6c1 67 38 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e  g8, 0, 0, pTab->
1c6c2 61 43 6f 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65  aCol[cnum].zName
1c6c3 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
1c6c4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1c6c5 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 33  , OP_Callback, 3
1c6c6 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
1c6c7 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69    }.  }else..  i
1c6c8 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1c6c9 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f  p(zLeft, "index_
1c6ca 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69  list")==0 && zRi
1c6cb 67 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78  ght ){.    Index
1c6cc 20 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c   *pIdx;.    Tabl
1c6cd 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
1c6ce 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
1c6cf 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
1c6d0 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
1c6d1 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
1c6d2 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
1c6d3 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20  ight, zDb);.    
1c6d4 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
1c6d5 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
1c6d6 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1c6d7 20 20 20 20 70 49 64 78 20 3d 20 70 54 61 62 2d      pIdx = pTab-
1c6d8 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 69  >pIndex;.      i
1c6d9 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
1c6da 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20     int i = 0; . 
1c6db 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c6dc 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
1c6dd 33 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  3);.        sqli
1c6de 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1c6df 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
1c6e0 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 50 33 5f  NAME, "seq", P3_
1c6e1 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
1c6e2 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
1c6e3 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c  olName(v, 1, COL
1c6e4 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65  NAME_NAME, "name
1c6e5 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20  ", P3_STATIC);. 
1c6e6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c6e7 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
1c6e8 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
1c6e9 20 22 75 6e 69 71 75 65 22 2c 20 50 33 5f 53 54   "unique", P3_ST
1c6ea 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 77  ATIC);.        w
1c6eb 68 69 6c 65 28 70 49 64 78 29 7b 0a 20 20 20 20  hile(pIdx){.    
1c6ec 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c6ed 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74  eAddOp(v, OP_Int
1c6ee 65 67 65 72 2c 20 69 2c 20 30 29 3b 0a 20 20 20  eger, i, 0);.   
1c6ef 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c6f0 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69  beOp3(v, OP_Stri
1c6f1 6e 67 38 2c 20 30 2c 20 30 2c 20 70 49 64 78 2d  ng8, 0, 0, pIdx-
1c6f2 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
1c6f3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c6f4 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74  eAddOp(v, OP_Int
1c6f5 65 67 65 72 2c 20 70 49 64 78 2d 3e 6f 6e 45 72  eger, pIdx->onEr
1c6f6 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 2c 20 30 29  ror!=OE_None, 0)
1c6f7 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1c6f8 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c6f9 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 33 2c 20  OP_Callback, 3, 
1c6fa 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b  0);.          ++
1c6fb 69 3b 0a 20 20 20 20 20 20 20 20 20 20 70 49 64  i;.          pId
1c6fc 78 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b  x = pIdx->pNext;
1c6fd 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1c6fe 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
1c6ff 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
1c700 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64  trICmp(zLeft, "d
1c701 61 74 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d  atabase_list")==
1c702 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
1c703 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
1c704 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
1c705 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
1c706 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  out;.    sqlite3
1c707 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
1c708 2c 20 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 3);.    sqlite
1c709 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
1c70a 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
1c70b 4d 45 2c 20 22 73 65 71 22 2c 20 50 33 5f 53 54  ME, "seq", P3_ST
1c70c 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  ATIC);.    sqlit
1c70d 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1c70e 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 1, COLNAME_N
1c70f 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 50 33 5f  AME, "name", P3_
1c710 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c  STATIC);.    sql
1c711 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
1c712 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45  me(v, 2, COLNAME
1c713 5f 4e 41 4d 45 2c 20 22 66 69 6c 65 22 2c 20 50  _NAME, "file", P
1c714 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66  3_STATIC);.    f
1c715 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
1c716 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  b; i++){.      i
1c717 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  f( db->aDb[i].pB
1c718 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  t==0 ) continue;
1c719 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
1c71a 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 21  b->aDb[i].zName!
1c71b 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
1c71c 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c71d 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 30  OP_Integer, i, 0
1c71e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1c71f 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74  VdbeOp3(v, OP_St
1c720 72 69 6e 67 38 2c 20 30 2c 20 30 2c 20 64 62 2d  ring8, 0, 0, db-
1c721 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 30  >aDb[i].zName, 0
1c722 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1c723 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74  VdbeOp3(v, OP_St
1c724 72 69 6e 67 38 2c 20 30 2c 20 30 2c 0a 20 20 20  ring8, 0, 0,.   
1c725 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1c726 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
1c727 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c  db->aDb[i].pBt),
1c728 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
1c729 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1c72a 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 33 2c 20 30  P_Callback, 3, 0
1c72b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
1c72c 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
1c72d 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 63  trICmp(zLeft, "c
1c72e 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22 29 3d  ollation_list")=
1c72f 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20  =0 ){.    int i 
1c730 3d 20 30 3b 0a 20 20 20 20 48 61 73 68 45 6c 65  = 0;.    HashEle
1c731 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74 65  m *p;.    sqlite
1c732 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
1c733 76 2c 20 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  v, 2);.    sqlit
1c734 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1c735 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
1c736 41 4d 45 2c 20 22 73 65 71 22 2c 20 50 33 5f 53  AME, "seq", P3_S
1c737 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
1c738 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1c739 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
1c73a 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 50 33  NAME, "name", P3
1c73b 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f  _STATIC);.    fo
1c73c 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(p=sqliteHashFi
1c73d 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  rst(&db->aCollSe
1c73e 71 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48  q); p; p=sqliteH
1c73f 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20  ashNext(p)){.   
1c740 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
1c741 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73  l = (CollSeq *)s
1c742 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29  qliteHashData(p)
1c743 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1c744 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
1c745 6e 74 65 67 65 72 2c 20 69 2b 2b 2c 20 30 29 3b  nteger, i++, 0);
1c746 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1c747 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69  beOp3(v, OP_Stri
1c748 6e 67 38 2c 20 30 2c 20 30 2c 20 70 43 6f 6c 6c  ng8, 0, 0, pColl
1c749 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
1c74a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1c74b 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61  dOp(v, OP_Callba
1c74c 63 6b 2c 20 32 2c 20 30 29 3b 0a 20 20 20 20 7d  ck, 2, 0);.    }
1c74d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
1c74e 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
1c74f 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 20 2a 2f  CHEMA_PRAGMAS */
1c750 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1c751 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
1c752 59 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  Y.  if( sqlite3S
1c753 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66  trICmp(zLeft, "f
1c754 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73 74 22  oreign_key_list"
1c755 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
1c756 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 3b  {.    FKey *pFK;
1c757 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
1c758 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1c759 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
1c75a 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
1c75b 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20  a_out;.    pTab 
1c75c 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
1c75d 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a  le(db, zRight, z
1c75e 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Db);.    if( pTa
1c75f 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 73  b ){.      v = s
1c760 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1c761 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 46 4b  arse);.      pFK
1c762 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 3b 0a   = pTab->pFKey;.
1c763 20 20 20 20 20 20 69 66 28 20 70 46 4b 20 29 7b        if( pFK ){
1c764 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d  .        int i =
1c765 20 30 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c   0; .        sql
1c766 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
1c767 6c 73 28 76 2c 20 35 29 3b 0a 20 20 20 20 20 20  ls(v, 5);.      
1c768 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1c769 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
1c76a 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 64 22  LNAME_NAME, "id"
1c76b 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P3_STATIC);.  
1c76c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c76d 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
1c76e 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
1c76f 22 73 65 71 22 2c 20 50 33 5f 53 54 41 54 49 43  "seq", P3_STATIC
1c770 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1c771 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1c772 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
1c773 41 4d 45 2c 20 22 74 61 62 6c 65 22 2c 20 50 33  AME, "table", P3
1c774 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
1c775 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1c776 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f  ColName(v, 3, CO
1c777 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 72 6f  LNAME_NAME, "fro
1c778 6d 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a  m", P3_STATIC);.
1c779 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1c77a 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
1c77b 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   4, COLNAME_NAME
1c77c 2c 20 22 74 6f 22 2c 20 50 33 5f 53 54 41 54 49  , "to", P3_STATI
1c77d 43 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  C);.        whil
1c77e 65 28 70 46 4b 29 7b 0a 20 20 20 20 20 20 20 20  e(pFK){.        
1c77f 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
1c780 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46     for(j=0; j<pF
1c781 4b 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20  K->nCol; j++){. 
1c782 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
1c783 2a 7a 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f  *zCol = pFK->aCo
1c784 6c 5b 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20  l[j].zCol;.     
1c785 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c786 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e  beAddOp(v, OP_In
1c787 74 65 67 65 72 2c 20 69 2c 20 30 29 3b 0a 20 20  teger, i, 0);.  
1c788 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1c789 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1c78a 5f 49 6e 74 65 67 65 72 2c 20 6a 2c 20 30 29 3b  _Integer, j, 0);
1c78b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1c78c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
1c78d 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c  P_String8, 0, 0,
1c78e 20 70 46 4b 2d 3e 7a 54 6f 2c 20 30 29 3b 0a 20   pFK->zTo, 0);. 
1c78f 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1c790 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f  e3VdbeOp3(v, OP_
1c791 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c 0a 20  String8, 0, 0,. 
1c792 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c793 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
1c794 2d 3e 61 43 6f 6c 5b 70 46 4b 2d 3e 61 43 6f 6c  ->aCol[pFK->aCol
1c795 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a 4e 61 6d 65  [j].iFrom].zName
1c796 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
1c797 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33    sqlite3VdbeOp3
1c798 28 76 2c 20 7a 43 6f 6c 20 3f 20 4f 50 5f 53 74  (v, zCol ? OP_St
1c799 72 69 6e 67 38 20 3a 20 4f 50 5f 4e 75 6c 6c 2c  ring8 : OP_Null,
1c79a 20 30 2c 20 30 2c 20 7a 43 6f 6c 2c 20 30 29 3b   0, 0, zCol, 0);
1c79b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1c79c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1c79d 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 35 2c   OP_Callback, 5,
1c79e 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   0);.          }
1c79f 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69 3b 0a  .          ++i;.
1c7a0 20 20 20 20 20 20 20 20 20 20 70 46 4b 20 3d 20            pFK = 
1c7a1 70 46 4b 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a  pFK->pNextFrom;.
1c7a2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1c7a3 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
1c7a4 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
1c7a5 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
1c7a6 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 0a  OREIGN_KEY) */..
1c7a7 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
1c7a8 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1c7a9 43 6d 70 28 7a 4c 65 66 74 2c 20 22 70 61 72 73  Cmp(zLeft, "pars
1c7aa 65 72 5f 74 72 61 63 65 22 29 3d 3d 30 20 29 7b  er_trace")==0 ){
1c7ab 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20  .    if( zRight 
1c7ac 29 7b 0a 20 20 20 20 20 20 69 66 28 20 67 65 74  ){.      if( get
1c7ad 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 20  Boolean(zRight) 
1c7ae 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1c7af 65 33 50 61 72 73 65 72 54 72 61 63 65 28 73 74  e3ParserTrace(st
1c7b0 64 65 72 72 2c 20 22 70 61 72 73 65 72 3a 20 22  derr, "parser: "
1c7b1 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1c7b2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
1c7b3 61 72 73 65 72 54 72 61 63 65 28 30 2c 20 30 29  arserTrace(0, 0)
1c7b4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1c7b5 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a    }else.#endif..
1c7b6 20 20 2f 2a 20 52 65 69 6e 73 74 61 6c 6c 20 74    /* Reinstall t
1c7b7 68 65 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42  he LIKE and GLOB
1c7b8 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65   functions.  The
1c7b9 20 76 61 72 69 61 6e 74 20 6f 66 20 4c 49 4b 45   variant of LIKE
1c7ba 0a 20 20 2a 2a 20 75 73 65 64 20 77 69 6c 6c 20  .  ** used will 
1c7bb 62 65 20 63 61 73 65 20 73 65 6e 73 69 74 69 76  be case sensitiv
1c7bc 65 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64 69  e or not dependi
1c7bd 6e 67 20 6f 6e 20 74 68 65 20 52 48 53 2e 0a 20  ng on the RHS.. 
1c7be 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
1c7bf 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
1c7c0 22 63 61 73 65 5f 73 65 6e 73 69 74 69 76 65 5f  "case_sensitive_
1c7c1 6c 69 6b 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  like")==0 ){.   
1c7c2 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20   if( zRight ){. 
1c7c3 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 67 69       sqlite3Regi
1c7c4 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e  sterLikeFunction
1c7c5 73 28 64 62 2c 20 67 65 74 42 6f 6f 6c 65 61 6e  s(db, getBoolean
1c7c6 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 20 20 7d  (zRight));.    }
1c7c7 0a 20 20 7d 65 6c 73 65 0a 0a 23 69 66 6e 64 65  .  }else..#ifnde
1c7c8 66 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49  f SQLITE_INTEGRI
1c7c9 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d  TY_CHECK_ERROR_M
1c7ca 41 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  AX.# define SQLI
1c7cb 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  TE_INTEGRITY_CHE
1c7cc 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 20 31 30 30  CK_ERROR_MAX 100
1c7cd 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1c7ce 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
1c7cf 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 20 20 69  EGRITY_CHECK.  i
1c7d0 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1c7d1 70 28 7a 4c 65 66 74 2c 20 22 69 6e 74 65 67 72  p(zLeft, "integr
1c7d2 69 74 79 5f 63 68 65 63 6b 22 29 3d 3d 30 20 29  ity_check")==0 )
1c7d3 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20  {.    int i, j, 
1c7d4 61 64 64 72 2c 20 6d 78 45 72 72 3b 0a 0a 20 20  addr, mxErr;..  
1c7d5 20 20 2f 2a 20 43 6f 64 65 20 74 68 61 74 20 61    /* Code that a
1c7d6 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e  ppears at the en
1c7d7 64 20 6f 66 20 74 68 65 20 69 6e 74 65 67 72 69  d of the integri
1c7d8 74 79 20 63 68 65 63 6b 2e 20 20 49 66 20 6e 6f  ty check.  If no
1c7d9 20 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20 6d 65   error.    ** me
1c7da 73 73 61 67 65 73 20 68 61 76 65 20 62 65 65 6e  ssages have been
1c7db 20 67 65 6e 65 72 61 74 65 64 2c 20 6f 75 74 70   generated, outp
1c7dc 75 74 20 4f 4b 2e 20 20 4f 74 68 65 72 77 69 73  ut OK.  Otherwis
1c7dd 65 20 6f 75 74 70 75 74 20 74 68 65 0a 20 20 20  e output the.   
1c7de 20 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67   ** error messag
1c7df 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61  e.    */.    sta
1c7e0 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
1c7e1 4c 69 73 74 20 65 6e 64 43 6f 64 65 5b 5d 20 3d  List endCode[] =
1c7e2 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4d 65   {.      { OP_Me
1c7e3 6d 4c 6f 61 64 2c 20 20 20 20 20 30 2c 20 30 2c  mLoad,     0, 0,
1c7e4 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20          0},.    
1c7e5 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20    { OP_Integer, 
1c7e6 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20      0, 0,       
1c7e7 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f   0},.      { OP_
1c7e8 4e 65 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20  Ne,          0, 
1c7e9 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20  0,        0},   
1c7ea 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 2 */.      {
1c7eb 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
1c7ec 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 22 6f   0, 0,        "o
1c7ed 6b 22 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f  k"},.      { OP_
1c7ee 43 61 6c 6c 62 61 63 6b 2c 20 20 20 20 31 2c 20  Callback,    1, 
1c7ef 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  0,        0},.  
1c7f0 20 20 7d 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69    };..    /* Ini
1c7f1 74 69 61 6c 69 7a 65 20 74 68 65 20 56 44 42 45  tialize the VDBE
1c7f2 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20   program */.    
1c7f3 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
1c7f4 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20  chema(pParse) ) 
1c7f5 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
1c7f6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1c7f7 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
1c7f8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1c7f9 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
1c7fa 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
1c7fb 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b  "integrity_check
1c7fc 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 0a  ", P3_STATIC);..
1c7fd 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d      /* Set the m
1c7fe 61 78 69 6d 75 6d 20 65 72 72 6f 72 20 63 6f 75  aximum error cou
1c7ff 6e 74 20 2a 2f 0a 20 20 20 20 6d 78 45 72 72 20  nt */.    mxErr 
1c800 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49  = SQLITE_INTEGRI
1c801 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d  TY_CHECK_ERROR_M
1c802 41 58 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67  AX;.    if( zRig
1c803 68 74 20 29 7b 0a 20 20 20 20 20 20 6d 78 45 72  ht ){.      mxEr
1c804 72 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29  r = atoi(zRight)
1c805 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 78 45 72  ;.      if( mxEr
1c806 72 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r<=0 ){.        
1c807 6d 78 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49  mxErr = SQLITE_I
1c808 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45  NTEGRITY_CHECK_E
1c809 52 52 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 20 20  RROR_MAX;.      
1c80a 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1c80b 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c80c 4f 50 5f 4d 65 6d 49 6e 74 2c 20 6d 78 45 72 72  OP_MemInt, mxErr
1c80d 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f  , 0);..    /* Do
1c80e 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68   an integrity ch
1c80f 65 63 6b 20 6f 6e 20 65 61 63 68 20 64 61 74 61  eck on each data
1c810 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20  base file */.   
1c811 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
1c812 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
1c813 20 48 61 73 68 45 6c 65 6d 20 2a 78 3b 0a 20 20   HashElem *x;.  
1c814 20 20 20 20 48 61 73 68 20 2a 70 54 62 6c 73 3b      Hash *pTbls;
1c815 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d  .      int cnt =
1c816 20 30 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 4f   0;..      if( O
1c817 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 3d  MIT_TEMPDB && i=
1c818 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  =1 ) continue;..
1c819 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64        sqlite3Cod
1c81a 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
1c81b 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 20 20  arse, i);.      
1c81c 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1c81d 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66  beAddOp(v, OP_If
1c81e 4d 65 6d 50 6f 73 2c 20 30 2c 20 30 29 3b 0a 20  MemPos, 0, 0);. 
1c81f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c820 41 64 64 4f 70 28 76 2c 20 4f 50 5f 48 61 6c 74  AddOp(v, OP_Halt
1c821 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  , 0, 0);.      s
1c822 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
1c823 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20  re(v, addr);..  
1c824 20 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74      /* Do an int
1c825 65 67 72 69 74 79 20 63 68 65 63 6b 20 6f 66 20  egrity check of 
1c826 74 68 65 20 42 2d 54 72 65 65 0a 20 20 20 20 20  the B-Tree.     
1c827 20 2a 2f 0a 20 20 20 20 20 20 70 54 62 6c 73 20   */.      pTbls 
1c828 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53  = &db->aDb[i].pS
1c829 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a  chema->tblHash;.
1c82a 20 20 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69        for(x=sqli
1c82b 74 65 48 61 73 68 46 69 72 73 74 28 70 54 62 6c  teHashFirst(pTbl
1c82c 73 29 3b 20 78 3b 20 78 3d 73 71 6c 69 74 65 48  s); x; x=sqliteH
1c82d 61 73 68 4e 65 78 74 28 78 29 29 7b 0a 20 20 20  ashNext(x)){.   
1c82e 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
1c82f 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74   = sqliteHashDat
1c830 61 28 78 29 3b 0a 20 20 20 20 20 20 20 20 49 6e  a(x);.        In
1c831 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 20  dex *pIdx;.     
1c832 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1c833 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
1c834 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30  r, pTab->tnum, 0
1c835 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b  );.        cnt++
1c836 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 49  ;.        for(pI
1c837 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
1c838 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
1c839 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
1c83a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1c83b 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67  ddOp(v, OP_Integ
1c83c 65 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  er, pIdx->tnum, 
1c83d 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e  0);.          cn
1c83e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  t++;.        }. 
1c83f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1c840 20 63 6e 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e   cnt==0 ) contin
1c841 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ue;.      sqlite
1c842 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1c843 5f 49 6e 74 65 67 72 69 74 79 43 6b 2c 20 30 2c  _IntegrityCk, 0,
1c844 20 69 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20   i);.      addr 
1c845 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1c846 4f 70 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  Op(v, OP_IsNull,
1c847 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 73   -1, 0);.      s
1c848 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c  qlite3VdbeOp3(v,
1c849 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
1c84a 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  0,.         sqli
1c84b 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
1c84c 2a 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20  *** in database 
1c84d 25 73 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61  %s ***\n", db->a
1c84e 44 62 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20  Db[i].zName),.  
1c84f 20 20 20 20 20 20 20 50 33 5f 44 59 4e 41 4d 49         P3_DYNAMI
1c850 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
1c851 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1c852 5f 50 75 6c 6c 2c 20 31 2c 20 30 29 3b 0a 20 20  _Pull, 1, 0);.  
1c853 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1c854 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6e 63 61  ddOp(v, OP_Conca
1c855 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 0, 0);.      
1c856 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c857 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c  (v, OP_Callback,
1c858 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71   1, 0);.      sq
1c859 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1c85a 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20  e(v, addr);..   
1c85b 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
1c85c 61 6c 6c 20 74 68 65 20 69 6e 64 69 63 65 73 20  all the indices 
1c85d 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  are constructed 
1c85e 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 20 20 20  correctly..     
1c85f 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 78 3d   */.      for(x=
1c860 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
1c861 70 54 62 6c 73 29 3b 20 78 3b 20 78 3d 73 71 6c  pTbls); x; x=sql
1c862 69 74 65 48 61 73 68 4e 65 78 74 28 78 29 29 7b  iteHashNext(x)){
1c863 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a  .        Table *
1c864 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73  pTab = sqliteHas
1c865 68 44 61 74 61 28 78 29 3b 0a 20 20 20 20 20 20  hData(x);.      
1c866 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
1c867 20 20 20 20 20 20 20 69 6e 74 20 6c 6f 6f 70 54         int loopT
1c868 6f 70 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  op;..        if(
1c869 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30   pTab->pIndex==0
1c86a 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1c86b 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
1c86c 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c86d 4f 50 5f 49 66 4d 65 6d 50 6f 73 2c 20 30 2c 20  OP_IfMemPos, 0, 
1c86e 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
1c86f 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c870 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a  OP_Halt, 0, 0);.
1c871 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1c872 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
1c873 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ddr);.        sq
1c874 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e  lite3OpenTableAn
1c875 64 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c  dIndices(pParse,
1c876 20 70 54 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65   pTab, 1, OP_Ope
1c877 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 20 20  nRead);.        
1c878 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c879 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 30  (v, OP_MemInt, 0
1c87a 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f  , 1);.        lo
1c87b 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  opTop = sqlite3V
1c87c 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52  dbeAddOp(v, OP_R
1c87d 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b 0a 20 20  ewind, 1, 0);.  
1c87e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c87f 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
1c880 49 6e 63 72 2c 20 31 2c 20 31 29 3b 0a 20 20 20  Incr, 1, 1);.   
1c881 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49       for(j=0, pI
1c882 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
1c883 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
1c884 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20  ->pNext, j++){. 
1c885 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6d 70           int jmp
1c886 32 3b 0a 20 20 20 20 20 20 20 20 20 20 73 74 61  2;.          sta
1c887 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
1c888 4c 69 73 74 20 69 64 78 45 72 72 5b 5d 20 3d 20  List idxErr[] = 
1c889 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  {.            { 
1c88a 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 20 20 20 2d  OP_MemIncr,    -
1c88b 31 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20  1,  0,  0},.    
1c88c 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72          { OP_Str
1c88d 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 30 2c  ing8,     0,  0,
1c88e 20 20 22 72 6f 77 69 64 20 22 7d 2c 0a 20 20 20    "rowid "},.   
1c88f 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 6f           { OP_Ro
1c890 77 69 64 2c 20 20 20 20 20 20 20 31 2c 20 20 30  wid,       1,  0
1c891 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  0},.         
1c892 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
1c893 20 20 20 20 20 30 2c 20 20 30 2c 20 20 22 20 6d       0,  0,  " m
1c894 69 73 73 69 6e 67 20 66 72 6f 6d 20 69 6e 64 65  issing from inde
1c895 78 20 22 7d 2c 0a 20 20 20 20 20 20 20 20 20 20  x "},.          
1c896 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20    { OP_String8, 
1c897 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20      0,  0,  0}, 
1c898 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20     /* 4 */.     
1c899 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63         { OP_Conc
1c89a 61 74 2c 20 20 20 20 20 20 32 2c 20 20 30 2c 20  at,      2,  0, 
1c89b 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0},.           
1c89c 20 7b 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20   { OP_Callback, 
1c89d 20 20 20 31 2c 20 20 30 2c 20 20 30 7d 2c 0a 20     1,  0,  0},. 
1c89e 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20           };.    
1c89f 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e        sqlite3Gen
1c8a0 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 76 2c  erateIndexKey(v,
1c8a1 20 70 49 64 78 2c 20 31 29 3b 0a 20 20 20 20 20   pIdx, 1);.     
1c8a2 20 20 20 20 20 6a 6d 70 32 20 3d 20 73 71 6c 69       jmp2 = sqli
1c8a3 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c8a4 4f 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32 2c 20 30  OP_Found, j+2, 0
1c8a5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64  );.          add
1c8a6 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
1c8a7 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61  ddOpList(v, Arra
1c8a8 79 53 69 7a 65 28 69 64 78 45 72 72 29 2c 20 69  ySize(idxErr), i
1c8a9 64 78 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20  dxErr);.        
1c8aa 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1c8ab 6e 67 65 50 33 28 76 2c 20 61 64 64 72 2b 34 2c  ngeP3(v, addr+4,
1c8ac 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 33   pIdx->zName, P3
1c8ad 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
1c8ae 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
1c8af 75 6d 70 48 65 72 65 28 76 2c 20 6a 6d 70 32 29  umpHere(v, jmp2)
1c8b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1c8b1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1c8b2 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  ddOp(v, OP_Next,
1c8b3 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29 3b 0a   1, loopTop+1);.
1c8b4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1c8b5 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6c  dbeJumpHere(v, l
1c8b6 6f 6f 70 54 6f 70 29 3b 0a 20 20 20 20 20 20 20  oopTop);.       
1c8b7 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70   for(j=0, pIdx=p
1c8b8 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
1c8b9 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
1c8ba 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  ext, j++){.     
1c8bb 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
1c8bc 74 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6e 74  t VdbeOpList cnt
1c8bd 49 64 78 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  Idx[] = {.      
1c8be 20 20 20 20 20 20 20 7b 20 4f 50 5f 4d 65 6d 49         { OP_MemI
1c8bf 6e 74 2c 20 20 20 20 20 20 20 30 2c 20 20 32 2c  nt,       0,  2,
1c8c0 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    0},.          
1c8c1 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20     { OP_Rewind, 
1c8c2 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d        0,  0,  0}
1c8c3 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20  ,  /* 1 */.     
1c8c4 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 4d 65 6d          { OP_Mem
1c8c5 49 6e 63 72 2c 20 20 20 20 20 20 31 2c 20 20 32  Incr,      1,  2
1c8c6 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  0},.         
1c8c7 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20      { OP_Next,  
1c8c8 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30         0,  0,  0
1c8c9 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20  },  /* 3 */.    
1c8ca 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 4d 65           { OP_Me
1c8cb 6d 4c 6f 61 64 2c 20 20 20 20 20 20 31 2c 20 20  mLoad,      1,  
1c8cc 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20  0,  0},.        
1c8cd 20 20 20 20 20 7b 20 4f 50 5f 4d 65 6d 4c 6f 61       { OP_MemLoa
1c8ce 64 2c 20 20 20 20 20 20 32 2c 20 20 30 2c 20 20  d,      2,  0,  
1c8cf 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0},.            
1c8d0 20 7b 20 4f 50 5f 45 71 2c 20 20 20 20 20 20 20   { OP_Eq,       
1c8d1 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20      0,  0,  0}, 
1c8d2 20 2f 2a 20 36 20 2a 2f 0a 20 20 20 20 20 20 20   /* 6 */.       
1c8d3 20 20 20 20 20 20 7b 20 4f 50 5f 4d 65 6d 49 6e        { OP_MemIn
1c8d4 63 72 2c 20 20 20 20 20 2d 31 2c 20 20 30 2c 20  cr,     -1,  0, 
1c8d5 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0},.           
1c8d6 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20    { OP_String8, 
1c8d7 20 20 20 20 20 30 2c 20 20 30 2c 20 20 22 77 72       0,  0,  "wr
1c8d8 6f 6e 67 20 23 20 6f 66 20 65 6e 74 72 69 65 73  ong # of entries
1c8d9 20 69 6e 20 69 6e 64 65 78 20 22 7d 2c 0a 20 20   in index "},.  
1c8da 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
1c8db 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c  String8,      0,
1c8dc 20 20 30 2c 20 20 30 7d 2c 20 20 2f 2a 20 39 20    0,  0},  /* 9 
1c8dd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
1c8de 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20  { OP_Concat,    
1c8df 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 0a 20     0,  0,  0},. 
1c8e0 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
1c8e1 5f 43 61 6c 6c 62 61 63 6b 2c 20 20 20 20 20 31  _Callback,     1
1c8e2 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  0,  0},.     
1c8e3 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20       };.        
1c8e4 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75 6d    if( pIdx->tnum
1c8e5 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
1c8e6 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d            addr =
1c8e7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1c8e8 70 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f 73  p(v, OP_IfMemPos
1c8e9 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
1c8ea 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1c8eb 64 4f 70 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  dOp(v, OP_Halt, 
1c8ec 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  0, 0);.         
1c8ed 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1c8ee 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20  Here(v, addr);. 
1c8ef 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20           addr = 
1c8f0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c8f1 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a  List(v, ArraySiz
1c8f2 65 28 63 6e 74 49 64 78 29 2c 20 63 6e 74 49 64  e(cntIdx), cntId
1c8f3 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  x);.          sq
1c8f4 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1c8f5 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 6a 2b 32  1(v, addr+1, j+2
1c8f6 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1c8f7 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
1c8f8 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 64 64 72  (v, addr+1, addr
1c8f9 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +4);.          s
1c8fa 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1c8fb 50 31 28 76 2c 20 61 64 64 72 2b 33 2c 20 6a 2b  P1(v, addr+3, j+
1c8fc 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  2);.          sq
1c8fd 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1c8fe 32 28 76 2c 20 61 64 64 72 2b 33 2c 20 61 64 64  2(v, addr+3, add
1c8ff 72 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+2);.          
1c900 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1c901 65 72 65 28 76 2c 20 61 64 64 72 2b 36 29 3b 0a  ere(v, addr+6);.
1c902 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1c903 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
1c904 20 61 64 64 72 2b 39 2c 20 70 49 64 78 2d 3e 7a   addr+9, pIdx->z
1c905 4e 61 6d 65 2c 20 50 33 5f 53 54 41 54 49 43 29  Name, P3_STATIC)
1c906 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1c907 20 20 7d 20 0a 20 20 20 20 7d 0a 20 20 20 20 61    } .    }.    a
1c908 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1c909 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
1c90a 72 61 79 53 69 7a 65 28 65 6e 64 43 6f 64 65 29  raySize(endCode)
1c90b 2c 20 65 6e 64 43 6f 64 65 29 3b 0a 20 20 20 20  , endCode);.    
1c90c 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1c90d 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 6d  eP1(v, addr+1, m
1c90e 78 45 72 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  xErr);.    sqlit
1c90f 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1c910 2c 20 61 64 64 72 2b 32 29 3b 0a 20 20 7d 65 6c  , addr+2);.  }el
1c911 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
1c912 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
1c913 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66  TY_CHECK */..#if
1c914 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1c915 5f 55 54 46 31 36 0a 20 20 2f 2a 0a 20 20 2a 2a  _UTF16.  /*.  **
1c916 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64 69     PRAGMA encodi
1c917 6e 67 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  ng.  **   PRAGMA
1c918 20 65 6e 63 6f 64 69 6e 67 20 3d 20 22 75 74 66   encoding = "utf
1c919 2d 38 22 7c 22 75 74 66 2d 31 36 22 7c 22 75 74  -8"|"utf-16"|"ut
1c91a 66 2d 31 36 6c 65 22 7c 22 75 74 66 2d 31 36 62  f-16le"|"utf-16b
1c91b 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20  e".  **.  ** In 
1c91c 69 74 27 73 20 66 69 72 73 74 20 66 6f 72 6d 2c  it's first form,
1c91d 20 74 68 69 73 20 70 72 61 67 6d 61 20 72 65 74   this pragma ret
1c91e 75 72 6e 73 20 74 68 65 20 65 6e 63 6f 64 69 6e  urns the encodin
1c91f 67 20 6f 66 20 74 68 65 20 6d 61 69 6e 0a 20 20  g of the main.  
1c920 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 49 66 20  ** database. If 
1c921 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
1c922 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c  not initialized,
1c923 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a   it is initializ
1c924 65 64 20 6e 6f 77 2e 0a 20 20 2a 2a 0a 20 20 2a  ed now..  **.  *
1c925 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72  * The second for
1c926 6d 20 6f 66 20 74 68 69 73 20 70 72 61 67 6d 61  m of this pragma
1c927 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74   is a no-op if t
1c928 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1c929 20 66 69 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6e   file.  ** has n
1c92a 6f 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ot already been 
1c92b 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 6e 20  initialized. In 
1c92c 74 68 69 73 20 63 61 73 65 20 69 74 20 73 65 74  this case it set
1c92d 73 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20  s the default.  
1c92e 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 74  ** encoding that
1c92f 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f   will be used fo
1c930 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
1c931 61 73 65 20 66 69 6c 65 20 69 66 20 61 20 6e 65  ase file if a ne
1c932 77 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20 63  w file.  ** is c
1c933 72 65 61 74 65 64 2e 20 49 66 20 61 6e 20 65 78  reated. If an ex
1c934 69 73 74 69 6e 67 20 6d 61 69 6e 20 64 61 74 61  isting main data
1c935 62 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65  base file is ope
1c936 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 20 20  ned, then the.  
1c937 2a 2a 20 64 65 66 61 75 6c 74 20 74 65 78 74 20  ** default text 
1c938 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65  encoding for the
1c939 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61   existing databa
1c93a 73 65 20 69 73 20 75 73 65 64 2e 0a 20 20 2a 2a  se is used..  **
1c93b 20 0a 20 20 2a 2a 20 49 6e 20 61 6c 6c 20 63 61   .  ** In all ca
1c93c 73 65 73 20 6e 65 77 20 64 61 74 61 62 61 73 65  ses new database
1c93d 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
1c93e 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61  the ATTACH comma
1c93f 6e 64 20 61 72 65 0a 20 20 2a 2a 20 63 72 65 61  nd are.  ** crea
1c940 74 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 73  ted to use the s
1c941 61 6d 65 20 64 65 66 61 75 6c 74 20 74 65 78 74  ame default text
1c942 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 65   encoding as the
1c943 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20   main database. 
1c944 49 66 0a 20 20 2a 2a 20 74 68 65 20 6d 61 69 6e  If.  ** the main
1c945 20 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f   database has no
1c946 74 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  t been initializ
1c947 65 64 20 61 6e 64 2f 6f 72 20 63 72 65 61 74 65  ed and/or create
1c948 64 20 77 68 65 6e 20 41 54 54 41 43 48 0a 20 20  d when ATTACH.  
1c949 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2c 20  ** is executed, 
1c94a 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66  this is done bef
1c94b 6f 72 65 20 74 68 65 20 41 54 54 41 43 48 20 6f  ore the ATTACH o
1c94c 70 65 72 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20  peration..  **. 
1c94d 20 2a 2a 20 49 6e 20 74 68 65 20 73 65 63 6f 6e   ** In the secon
1c94e 64 20 66 6f 72 6d 20 74 68 69 73 20 70 72 61 67  d form this prag
1c94f 6d 61 20 73 65 74 73 20 74 68 65 20 74 65 78 74  ma sets the text
1c950 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20   encoding to be 
1c951 75 73 65 64 20 69 6e 0a 20 20 2a 2a 20 6e 65 77  used in.  ** new
1c952 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
1c953 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68  created using th
1c954 69 73 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  is database hand
1c955 6c 65 2e 20 49 74 20 69 73 20 6f 6e 6c 79 0a 20  le. It is only. 
1c956 20 2a 2a 20 75 73 65 66 75 6c 20 69 66 20 69 6e   ** useful if in
1c957 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  voked immediatel
1c958 79 20 61 66 74 65 72 20 74 68 65 20 6d 61 69 6e  y after the main
1c959 20 64 61 74 61 62 61 73 65 20 69 0a 20 20 2a 2f   database i.  */
1c95a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
1c95b 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 65 6e  rICmp(zLeft, "en
1c95c 63 6f 64 69 6e 67 22 29 3d 3d 30 20 29 7b 0a 20  coding")==0 ){. 
1c95d 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
1c95e 73 74 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 7b  struct EncName {
1c95f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61  .      char *zNa
1c960 6d 65 3b 0a 20 20 20 20 20 20 75 38 20 65 6e 63  me;.      u8 enc
1c961 3b 0a 20 20 20 20 7d 20 65 6e 63 6e 61 6d 65 73  ;.    } encnames
1c962 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 22  [] = {.      { "
1c963 55 54 46 2d 38 22 2c 20 20 20 20 53 51 4c 49 54  UTF-8",    SQLIT
1c964 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 7d 2c  E_UTF8        },
1c965 0a 20 20 20 20 20 20 7b 20 22 55 54 46 38 22 2c  .      { "UTF8",
1c966 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38       SQLITE_UTF8
1c967 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20          },.     
1c968 20 7b 20 22 55 54 46 2d 31 36 6c 65 22 2c 20 53   { "UTF-16le", S
1c969 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20  QLITE_UTF16LE   
1c96a 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54    },.      { "UT
1c96b 46 31 36 6c 65 22 2c 20 20 53 51 4c 49 54 45 5f  F16le",  SQLITE_
1c96c 55 54 46 31 36 4c 45 20 20 20 20 20 7d 2c 0a 20  UTF16LE     },. 
1c96d 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36 62 65       { "UTF-16be
1c96e 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  ", SQLITE_UTF16B
1c96f 45 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b  E     },.      {
1c970 20 22 55 54 46 31 36 62 65 22 2c 20 20 53 51 4c   "UTF16be",  SQL
1c971 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20  ITE_UTF16BE     
1c972 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d  },.      { "UTF-
1c973 31 36 22 2c 20 20 20 30 20 20 20 20 20 20 20 20  16",   0        
1c974 20 20 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20            }, /* 
1c975 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
1c976 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55  VE */.      { "U
1c977 54 46 31 36 22 2c 20 20 20 20 30 20 20 20 20 20  TF16",    0     
1c978 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 20               }, 
1c979 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  /* SQLITE_UTF16N
1c97a 41 54 49 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b  ATIVE */.      {
1c97b 20 30 2c 20 30 20 7d 0a 20 20 20 20 7d 3b 0a 20   0, 0 }.    };. 
1c97c 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20     const struct 
1c97d 45 6e 63 4e 61 6d 65 20 2a 70 45 6e 63 3b 0a 20  EncName *pEnc;. 
1c97e 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
1c97f 7b 20 20 20 20 2f 2a 20 22 50 52 41 47 4d 41 20  {    /* "PRAGMA 
1c980 65 6e 63 6f 64 69 6e 67 22 20 2a 2f 0a 20 20 20  encoding" */.   
1c981 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
1c982 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
1c983 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
1c984 75 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ut;.      sqlite
1c985 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
1c986 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  v, 1);.      sql
1c987 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
1c988 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
1c989 5f 4e 41 4d 45 2c 20 22 65 6e 63 6f 64 69 6e 67  _NAME, "encoding
1c98a 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20  ", P3_STATIC);. 
1c98b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c98c 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53 74 72 69  AddOp(v, OP_Stri
1c98d 6e 67 38 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ng8, 0, 0);.    
1c98e 20 20 66 6f 72 28 70 45 6e 63 3d 26 65 6e 63 6e    for(pEnc=&encn
1c98f 61 6d 65 73 5b 30 5d 3b 20 70 45 6e 63 2d 3e 7a  ames[0]; pEnc->z
1c990 4e 61 6d 65 3b 20 70 45 6e 63 2b 2b 29 7b 0a 20  Name; pEnc++){. 
1c991 20 20 20 20 20 20 20 69 66 28 20 70 45 6e 63 2d         if( pEnc-
1c992 3e 65 6e 63 3d 3d 45 4e 43 28 70 50 61 72 73 65  >enc==ENC(pParse
1c993 2d 3e 64 62 29 20 29 7b 0a 20 20 20 20 20 20 20  ->db) ){.       
1c994 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1c995 61 6e 67 65 50 33 28 76 2c 20 2d 31 2c 20 70 45  angeP3(v, -1, pE
1c996 6e 63 2d 3e 7a 4e 61 6d 65 2c 20 50 33 5f 53 54  nc->zName, P3_ST
1c997 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20  ATIC);.         
1c998 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1c999 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
1c99a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c99b 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c  (v, OP_Callback,
1c99c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73   1, 0);.    }els
1c99d 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e{              
1c99e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 22 50 52            /* "PR
1c99f 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20  AGMA encoding = 
1c9a0 58 58 58 22 20 2a 2f 0a 20 20 20 20 20 20 2f 2a  XXX" */.      /*
1c9a1 20 4f 6e 6c 79 20 63 68 61 6e 67 65 20 74 68 65   Only change the
1c9a2 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65   value of sqlite
1c9a3 2e 65 6e 63 20 69 66 20 74 68 65 20 64 61 74 61  .enc if the data
1c9a4 62 61 73 65 20 68 61 6e 64 6c 65 20 69 73 20 6e  base handle is n
1c9a5 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 69 74  ot.      ** init
1c9a6 69 61 6c 69 7a 65 64 2e 20 49 66 20 74 68 65 20  ialized. If the 
1c9a7 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 78  main database ex
1c9a8 69 73 74 73 2c 20 74 68 65 20 6e 65 77 20 73 71  ists, the new sq
1c9a9 6c 69 74 65 2e 65 6e 63 20 76 61 6c 75 65 0a 20  lite.enc value. 
1c9aa 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20       ** will be 
1c9ab 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e  overwritten when
1c9ac 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e   the schema is n
1c9ad 65 78 74 20 6c 6f 61 64 65 64 2e 20 49 66 20 69  ext loaded. If i
1c9ae 74 20 64 6f 65 73 20 6e 6f 74 0a 20 20 20 20 20  t does not.     
1c9af 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73   ** already exis
1c9b0 74 73 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 63  ts, it will be c
1c9b1 72 65 61 74 65 64 20 74 6f 20 75 73 65 20 74 68  reated to use th
1c9b2 65 20 6e 65 77 20 65 6e 63 6f 64 69 6e 67 20 76  e new encoding v
1c9b3 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  alue..      */. 
1c9b4 20 20 20 20 20 69 66 28 20 0a 20 20 20 20 20 20       if( .      
1c9b5 20 20 21 28 44 62 48 61 73 50 72 6f 70 65 72 74    !(DbHasPropert
1c9b6 79 28 64 62 2c 20 30 2c 20 44 42 5f 53 63 68 65  y(db, 0, DB_Sche
1c9b7 6d 61 4c 6f 61 64 65 64 29 29 20 7c 7c 20 0a 20  maLoaded)) || . 
1c9b8 20 20 20 20 20 20 20 44 62 48 61 73 50 72 6f 70         DbHasProp
1c9b9 65 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f 45  erty(db, 0, DB_E
1c9ba 6d 70 74 79 29 20 0a 20 20 20 20 20 20 29 7b 0a  mpty) .      ){.
1c9bb 20 20 20 20 20 20 20 20 66 6f 72 28 70 45 6e 63          for(pEnc
1c9bc 3d 26 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b 20 70  =&encnames[0]; p
1c9bd 45 6e 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45 6e 63  Enc->zName; pEnc
1c9be 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
1c9bf 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  f( 0==sqlite3Str
1c9c0 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 70 45 6e  ICmp(zRight, pEn
1c9c1 63 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  c->zName) ){.   
1c9c2 20 20 20 20 20 20 20 20 20 45 4e 43 28 70 50 61           ENC(pPa
1c9c3 72 73 65 2d 3e 64 62 29 20 3d 20 70 45 6e 63 2d  rse->db) = pEnc-
1c9c4 3e 65 6e 63 20 3f 20 70 45 6e 63 2d 3e 65 6e 63  >enc ? pEnc->enc
1c9c5 20 3a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e   : SQLITE_UTF16N
1c9c6 41 54 49 56 45 3b 0a 20 20 20 20 20 20 20 20 20  ATIVE;.         
1c9c7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1c9c8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
1c9c9 20 20 20 20 20 20 20 20 69 66 28 20 21 70 45 6e          if( !pEn
1c9ca 63 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  c->zName ){.    
1c9cb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1c9cc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75  orMsg(pParse, "u
1c9cd 6e 73 75 70 70 6f 72 74 65 64 20 65 6e 63 6f 64  nsupported encod
1c9ce 69 6e 67 3a 20 25 73 22 2c 20 7a 52 69 67 68 74  ing: %s", zRight
1c9cf 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1c9d0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
1c9d1 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
1c9d2 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1c9d3 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1c9d4 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45  E_OMIT_SCHEMA_VE
1c9d5 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 0a 20 20  RSION_PRAGMAS.  
1c9d6 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  /*.  **   PRAGMA
1c9d7 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65   [database.]sche
1c9d8 6d 61 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20  ma_version.  ** 
1c9d9 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
1c9da 73 65 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69  se.]schema_versi
1c9db 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20  on = <integer>. 
1c9dc 20 2a 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   **.  **   PRAGM
1c9dd 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 75 73 65  A [database.]use
1c9de 72 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20  r_version.  **  
1c9df 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
1c9e0 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 20  e.]user_version 
1c9e1 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a 2a  = <integer>.  **
1c9e2 0a 20 20 2a 2a 20 54 68 65 20 70 72 61 67 6d 61  .  ** The pragma
1c9e3 27 73 20 73 63 68 65 6d 61 5f 76 65 72 73 69 6f  's schema_versio
1c9e4 6e 20 61 6e 64 20 75 73 65 72 5f 76 65 72 73 69  n and user_versi
1c9e5 6f 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20 73  on are used to s
1c9e6 65 74 20 6f 72 20 67 65 74 0a 20 20 2a 2a 20 74  et or get.  ** t
1c9e7 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1c9e8 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61  schema-version a
1c9e9 6e 64 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 2c  nd user-version,
1c9ea 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 42   respectively. B
1c9eb 6f 74 68 0a 20 20 2a 2a 20 74 68 65 20 73 63 68  oth.  ** the sch
1c9ec 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20  ema-version and 
1c9ed 74 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f 6e  the user-version
1c9ee 20 61 72 65 20 33 32 2d 62 69 74 20 73 69 67 6e   are 32-bit sign
1c9ef 65 64 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a  ed integers.  **
1c9f0 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
1c9f1 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e 0a  atabase header..
1c9f2 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 63    **.  ** The sc
1c9f3 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 69 73 20 75  hema-cookie is u
1c9f4 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6d 61 6e 69  sually only mani
1c9f5 70 75 6c 61 74 65 64 20 69 6e 74 65 72 6e 61 6c  pulated internal
1c9f6 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74  ly by SQLite. It
1c9f7 0a 20 20 2a 2a 20 69 73 20 69 6e 63 72 65 6d 65  .  ** is increme
1c9f8 6e 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 77  nted by SQLite w
1c9f9 68 65 6e 65 76 65 72 20 74 68 65 20 64 61 74 61  henever the data
1c9fa 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 6d  base schema is m
1c9fb 6f 64 69 66 69 65 64 20 28 62 79 0a 20 20 2a 2a  odified (by.  **
1c9fc 20 63 72 65 61 74 69 6e 67 20 6f 72 20 64 72 6f   creating or dro
1c9fd 70 70 69 6e 67 20 61 20 74 61 62 6c 65 20 6f 72  pping a table or
1c9fe 20 69 6e 64 65 78 29 2e 20 54 68 65 20 73 63 68   index). The sch
1c9ff 65 6d 61 20 76 65 72 73 69 6f 6e 20 69 73 20 75  ema version is u
1ca00 73 65 64 20 62 79 0a 20 20 2a 2a 20 53 51 4c 69  sed by.  ** SQLi
1ca01 74 65 20 65 61 63 68 20 74 69 6d 65 20 61 20 71  te each time a q
1ca02 75 65 72 79 20 69 73 20 65 78 65 63 75 74 65 64  uery is executed
1ca03 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
1ca04 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63  the internal cac
1ca05 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 73  he.  ** of the s
1ca06 63 68 65 6d 61 20 75 73 65 64 20 77 68 65 6e 20  chema used when 
1ca07 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51  compiling the SQ
1ca08 4c 20 71 75 65 72 79 20 6d 61 74 63 68 65 73 20  L query matches 
1ca09 74 68 65 20 73 63 68 65 6d 61 20 6f 66 0a 20 20  the schema of.  
1ca0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
1ca0b 61 67 61 69 6e 73 74 20 77 68 69 63 68 20 74 68  against which th
1ca0c 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 65 72 79  e compiled query
1ca0d 20 69 73 20 61 63 74 75 61 6c 6c 79 20 65 78 65   is actually exe
1ca0e 63 75 74 65 64 2e 0a 20 20 2a 2a 20 53 75 62 76  cuted..  ** Subv
1ca0f 65 72 74 69 6e 67 20 74 68 69 73 20 6d 65 63 68  erting this mech
1ca10 61 6e 69 73 6d 20 62 79 20 75 73 69 6e 67 20 22  anism by using "
1ca11 50 52 41 47 4d 41 20 73 63 68 65 6d 61 5f 76 65  PRAGMA schema_ve
1ca12 72 73 69 6f 6e 22 20 74 6f 20 6d 6f 64 69 66 79  rsion" to modify
1ca13 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61  .  ** the schema
1ca14 2d 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 74 65  -version is pote
1ca15 6e 74 69 61 6c 6c 79 20 64 61 6e 67 65 72 6f 75  ntially dangerou
1ca16 73 20 61 6e 64 20 6d 61 79 20 6c 65 61 64 20 74  s and may lead t
1ca17 6f 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 63  o program.  ** c
1ca18 72 61 73 68 65 73 20 6f 72 20 64 61 74 61 62 61  rashes or databa
1ca19 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 55  se corruption. U
1ca1a 73 65 20 77 69 74 68 20 63 61 75 74 69 6f 6e 21  se with caution!
1ca1b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 75  .  **.  ** The u
1ca1c 73 65 72 2d 76 65 72 73 69 6f 6e 20 69 73 20 6e  ser-version is n
1ca1d 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  ot used internal
1ca1e 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74  ly by SQLite. It
1ca1f 20 6d 61 79 20 62 65 20 75 73 65 64 20 62 79 0a   may be used by.
1ca20 20 20 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e    ** application
1ca21 73 20 66 6f 72 20 61 6e 79 20 70 75 72 70 6f 73  s for any purpos
1ca22 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  e..  */.  if( sq
1ca23 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
1ca24 66 74 2c 20 22 73 63 68 65 6d 61 5f 76 65 72 73  ft, "schema_vers
1ca25 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20  ion")==0 .   || 
1ca26 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
1ca27 4c 65 66 74 2c 20 22 75 73 65 72 5f 76 65 72 73  Left, "user_vers
1ca28 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20  ion")==0 .   || 
1ca29 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
1ca2a 4c 65 66 74 2c 20 22 66 72 65 65 6c 69 73 74 5f  Left, "freelist_
1ca2b 63 6f 75 6e 74 22 29 3d 3d 30 20 0a 20 20 29 7b  count")==0 .  ){
1ca2c 0a 0a 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69  ..    int iCooki
1ca2d 65 3b 20 20 20 2f 2a 20 43 6f 6f 6b 69 65 20 69  e;   /* Cookie i
1ca2e 6e 64 65 78 2e 20 30 20 66 6f 72 20 73 63 68 65  ndex. 0 for sche
1ca2f 6d 61 2d 63 6f 6f 6b 69 65 2c 20 36 20 66 6f 72  ma-cookie, 6 for
1ca30 20 75 73 65 72 2d 63 6f 6f 6b 69 65 2e 20 2a 2f   user-cookie. */
1ca31 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ca32 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62  UsesBtree(v, iDb
1ca33 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 7a  );.    switch( z
1ca34 4c 65 66 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20  Left[0] ){.     
1ca35 20 63 61 73 65 20 27 73 27 3a 20 63 61 73 65 20   case 's': case 
1ca36 27 53 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f  'S':.        iCo
1ca37 6f 6b 69 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  okie = 0;.      
1ca38 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1ca39 61 73 65 20 27 66 27 3a 20 63 61 73 65 20 27 46  ase 'f': case 'F
1ca3a 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b  ':.        iCook
1ca3b 69 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ie = 1;.        
1ca3c 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62 2b 31  iDb = (-1*(iDb+1
1ca3d 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ));.        asse
1ca3e 72 74 28 69 44 62 3c 3d 30 29 3b 0a 20 20 20 20  rt(iDb<=0);.    
1ca3f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1ca40 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
1ca41 20 20 69 43 6f 6f 6b 69 65 20 3d 20 35 3b 0a 20    iCookie = 5;. 
1ca42 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1ca43 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a 52 69    }..    if( zRi
1ca44 67 68 74 20 26 26 20 69 44 62 3e 3d 30 20 29 7b  ght && iDb>=0 ){
1ca45 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  .      /* Write 
1ca46 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  the specified co
1ca47 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20  okie value */.  
1ca48 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
1ca49 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65 74 43   VdbeOpList setC
1ca4a 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  ookie[] = {.    
1ca4b 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63      { OP_Transac
1ca4c 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20 31 2c 20  tion,    0,  1, 
1ca4d 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a   0},    /* 0 */.
1ca4e 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74          { OP_Int
1ca4f 65 67 65 72 2c 20 20 20 20 20 20 20 20 30 2c 20  eger,        0, 
1ca50 20 30 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31   0,  0},    /* 1
1ca51 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50   */.        { OP
1ca52 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20  _SetCookie,     
1ca53 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20 20   0,  0,  0},    
1ca54 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 7d 3b  /* 2 */.      };
1ca55 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20  .      int addr 
1ca56 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1ca57 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
1ca58 69 7a 65 28 73 65 74 43 6f 6f 6b 69 65 29 2c 20  ize(setCookie), 
1ca59 73 65 74 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20  setCookie);.    
1ca5a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1ca5b 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69  ngeP1(v, addr, i
1ca5c 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
1ca5d 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
1ca5e 2c 20 61 64 64 72 2b 31 2c 20 61 74 6f 69 28 7a  , addr+1, atoi(z
1ca5f 52 69 67 68 74 29 29 3b 0a 20 20 20 20 20 20 73  Right));.      s
1ca60 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1ca61 50 31 28 76 2c 20 61 64 64 72 2b 32 2c 20 69 44  P1(v, addr+2, iD
1ca62 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
1ca63 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
1ca64 20 61 64 64 72 2b 32 2c 20 69 43 6f 6f 6b 69 65   addr+2, iCookie
1ca65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1ca66 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
1ca67 73 70 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65  specified cookie
1ca68 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20   value */.      
1ca69 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
1ca6a 65 4f 70 4c 69 73 74 20 72 65 61 64 43 6f 6f 6b  eOpList readCook
1ca6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  ie[] = {.       
1ca6c 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65   { OP_ReadCookie
1ca6d 2c 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30  ,      0,  0,  0
1ca6e 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20  },    /* 0 */.  
1ca6f 20 20 20 20 20 20 7b 20 4f 50 5f 43 61 6c 6c 62        { OP_Callb
1ca70 61 63 6b 2c 20 20 20 20 20 20 20 20 31 2c 20 20  ack,        1,  
1ca71 30 2c 20 20 30 7d 0a 20 20 20 20 20 20 7d 3b 0a  0,  0}.      };.
1ca72 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d        int addr =
1ca73 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ca74 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
1ca75 7a 65 28 72 65 61 64 43 6f 6f 6b 69 65 29 2c 20  ze(readCookie), 
1ca76 72 65 61 64 43 6f 6f 6b 69 65 29 3b 0a 20 20 20  readCookie);.   
1ca77 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1ca78 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20  angeP1(v, addr, 
1ca79 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
1ca7a 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1ca7b 76 2c 20 61 64 64 72 2c 20 69 43 6f 6f 6b 69 65  v, addr, iCookie
1ca7c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1ca7d 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
1ca7e 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
1ca7f 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1ca80 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
1ca81 4e 41 4d 45 2c 20 7a 4c 65 66 74 2c 20 50 33 5f  NAME, zLeft, P3_
1ca82 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
1ca83 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
1ca84 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1ca85 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50  SCHEMA_VERSION_P
1ca86 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 20 64  RAGMAS */..#if d
1ca87 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
1ca88 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  BUG) || defined(
1ca89 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 2f  SQLITE_TEST).  /
1ca8a 2a 0a 20 20 2a 2a 20 52 65 70 6f 72 74 20 74 68  *.  ** Report th
1ca8b 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  e current state 
1ca8c 6f 66 20 66 69 6c 65 20 6c 6f 67 73 20 66 6f 72  of file logs for
1ca8d 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0a 20   all databases. 
1ca8e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
1ca8f 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
1ca90 22 6c 6f 63 6b 5f 73 74 61 74 75 73 22 29 3d 3d  "lock_status")==
1ca91 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20  0 ){.    static 
1ca92 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
1ca93 74 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d 20 3d  t azLockName[] =
1ca94 20 7b 0a 20 20 20 20 20 20 22 75 6e 6c 6f 63 6b   {.      "unlock
1ca95 65 64 22 2c 20 22 73 68 61 72 65 64 22 2c 20 22  ed", "shared", "
1ca96 72 65 73 65 72 76 65 64 22 2c 20 22 70 65 6e 64  reserved", "pend
1ca97 69 6e 67 22 2c 20 22 65 78 63 6c 75 73 69 76 65  ing", "exclusive
1ca98 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  ".    };.    int
1ca99 20 69 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20   i;.    Vdbe *v 
1ca9a 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1ca9b 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71  (pParse);.    sq
1ca9c 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
1ca9d 6f 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20 20 73  ols(v, 2);.    s
1ca9e 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
1ca9f 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
1caa0 4d 45 5f 4e 41 4d 45 2c 20 22 64 61 74 61 62 61  ME_NAME, "databa
1caa1 73 65 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b  se", P3_STATIC);
1caa2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1caa3 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
1caa4 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
1caa5 73 74 61 74 75 73 22 2c 20 50 33 5f 53 54 41 54  status", P3_STAT
1caa6 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  IC);.    for(i=0
1caa7 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1caa8 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a  ){.      Btree *
1caa9 70 42 74 3b 0a 20 20 20 20 20 20 50 61 67 65 72  pBt;.      Pager
1caaa 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20   *pPager;.      
1caab 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61  const char *zSta
1caac 74 65 20 3d 20 22 75 6e 6b 6e 6f 77 6e 22 3b 0a  te = "unknown";.
1caad 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
1caae 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69     if( db->aDb[i
1caaf 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 20 63 6f 6e  ].zName==0 ) con
1cab0 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 73 71 6c  tinue;.      sql
1cab1 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
1cab2 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c  P_String8, 0, 0,
1cab3 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
1cab4 65 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20  e, P3_STATIC);. 
1cab5 20 20 20 20 20 70 42 74 20 3d 20 64 62 2d 3e 61       pBt = db->a
1cab6 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
1cab7 20 69 66 28 20 70 42 74 3d 3d 30 20 7c 7c 20 28   if( pBt==0 || (
1cab8 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
1cab9 42 74 72 65 65 50 61 67 65 72 28 70 42 74 29 29  BtreePager(pBt))
1caba 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ==0 ){.        z
1cabb 53 74 61 74 65 20 3d 20 22 63 6c 6f 73 65 64 22  State = "closed"
1cabc 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1cabd 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63  ( sqlite3_file_c
1cabe 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 2d 3e 61  ontrol(db, db->a
1cabf 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 0a 20 20  Db[i].zName, .  
1cac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cac1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cac2 20 20 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f     SQLITE_FCNTL_
1cac3 4c 4f 43 4b 53 54 41 54 45 2c 20 26 6a 29 3d 3d  LOCKSTATE, &j)==
1cac4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1cac5 20 20 20 20 20 20 7a 53 74 61 74 65 20 3d 20 61        zState = a
1cac6 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a 5d 3b 0a 20 20  zLockName[j];.  
1cac7 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1cac8 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
1cac9 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c 20  _String8, 0, 0, 
1caca 7a 53 74 61 74 65 2c 20 50 33 5f 53 54 41 54 49  zState, P3_STATI
1cacb 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
1cacc 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1cacd 5f 43 61 6c 6c 62 61 63 6b 2c 20 32 2c 20 30 29  _Callback, 2, 0)
1cace 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
1cacf 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
1cad0 51 4c 49 54 45 5f 53 53 45 0a 20 20 2f 2a 0a 20  QLITE_SSE.  /*. 
1cad1 20 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   ** Check to see
1cad2 20 69 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   if the sqlite_s
1cad3 74 61 74 65 6d 65 6e 74 73 20 74 61 62 6c 65 20  tatements table 
1cad4 65 78 69 73 74 73 2e 20 20 43 72 65 61 74 65 20  exists.  Create 
1cad5 69 74 0a 20 20 2a 2a 20 69 66 20 69 74 20 64 6f  it.  ** if it do
1cad6 65 73 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 69  es not..  */.  i
1cad7 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1cad8 70 28 7a 4c 65 66 74 2c 20 22 63 72 65 61 74 65  p(zLeft, "create
1cad9 5f 73 71 6c 69 74 65 5f 73 74 61 74 65 6d 65 6e  _sqlite_statemen
1cada 74 5f 74 61 62 6c 65 22 29 3d 3d 30 20 29 7b 0a  t_table")==0 ){.
1cadb 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73      extern int s
1cadc 71 6c 69 74 65 33 43 72 65 61 74 65 53 74 61 74  qlite3CreateStat
1cadd 65 6d 65 6e 74 73 54 61 62 6c 65 28 50 61 72 73  ementsTable(Pars
1cade 65 2a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  e*);.    sqlite3
1cadf 43 72 65 61 74 65 53 74 61 74 65 6d 65 6e 74 73  CreateStatements
1cae0 54 61 62 6c 65 28 70 50 61 72 73 65 29 3b 0a 20  Table(pParse);. 
1cae1 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23   }else.#endif..#
1cae2 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  if SQLITE_HAS_CO
1cae3 44 45 43 0a 20 20 69 66 28 20 73 71 6c 69 74 65  DEC.  if( sqlite
1cae4 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
1cae5 22 6b 65 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20  "key")==0 ){.   
1cae6 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c   sqlite3_key(db,
1cae7 20 7a 52 69 67 68 74 2c 20 73 74 72 6c 65 6e 28   zRight, strlen(
1cae8 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73  zRight));.  }els
1cae9 65 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  e.#endif.#if SQL
1caea 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 7c 7c  ITE_HAS_CODEC ||
1caeb 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1caec 45 4e 41 42 4c 45 5f 43 45 52 4f 44 29 0a 20 20  ENABLE_CEROD).  
1caed 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1caee 6d 70 28 7a 4c 65 66 74 2c 20 22 61 63 74 69 76  mp(zLeft, "activ
1caef 61 74 65 5f 65 78 74 65 6e 73 69 6f 6e 73 22 29  ate_extensions")
1caf0 3d 3d 30 20 29 7b 0a 23 69 66 20 53 51 4c 49 54  ==0 ){.#if SQLIT
1caf1 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 20 20  E_HAS_CODEC.    
1caf2 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
1caf3 43 6d 70 28 7a 52 69 67 68 74 2c 20 22 73 65 65  Cmp(zRight, "see
1caf4 2d 22 2c 20 34 29 3d 3d 30 20 29 7b 0a 20 20 20  -", 4)==0 ){.   
1caf5 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73     extern void s
1caf6 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f  qlite3_activate_
1caf7 73 65 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  see(const char*)
1caf8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1caf9 61 63 74 69 76 61 74 65 5f 73 65 65 28 26 7a 52  activate_see(&zR
1cafa 69 67 68 74 5b 34 5d 29 3b 0a 20 20 20 20 7d 0a  ight[4]);.    }.
1cafb 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
1cafc 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f  LITE_ENABLE_CERO
1cafd 44 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  D.    if( sqlite
1cafe 33 53 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74  3StrNICmp(zRight
1caff 2c 20 22 63 65 72 6f 64 2d 22 2c 20 36 29 3d 3d  , "cerod-", 6)==
1cb00 30 20 29 7b 0a 20 20 20 20 20 20 65 78 74 65 72  0 ){.      exter
1cb01 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 61  n void sqlite3_a
1cb02 63 74 69 76 61 74 65 5f 63 65 72 6f 64 28 63 6f  ctivate_cerod(co
1cb03 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20  nst char*);.    
1cb04 20 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61    sqlite3_activa
1cb05 74 65 5f 63 65 72 6f 64 28 26 7a 52 69 67 68 74  te_cerod(&zRight
1cb06 5b 36 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  [6]);.    }.#end
1cb07 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  if.  }.#endif.. 
1cb08 20 7b 7d 0a 0a 20 20 69 66 28 20 76 20 29 7b 0a   {}..  if( v ){.
1cb09 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f      /* Code an O
1cb0a 50 5f 45 78 70 69 72 65 20 61 74 20 74 68 65 20  P_Expire at the 
1cb0b 65 6e 64 20 6f 66 20 65 61 63 68 20 50 52 41 47  end of each PRAG
1cb0c 4d 41 20 70 72 6f 67 72 61 6d 20 74 6f 20 63 61  MA program to ca
1cb0d 75 73 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 56  use.    ** the V
1cb0e 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  DBE implementing
1cb0f 20 74 68 65 20 70 72 61 67 6d 61 20 74 6f 20 65   the pragma to e
1cb10 78 70 69 72 65 2e 20 4d 6f 73 74 20 28 61 6c 6c  xpire. Most (all
1cb11 3f 29 20 70 72 61 67 6d 61 73 0a 20 20 20 20 2a  ?) pragmas.    *
1cb12 2a 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64  * are only valid
1cb13 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 78   for a single ex
1cb14 65 63 75 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  ecution..    */.
1cb15 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1cb16 64 64 4f 70 28 76 2c 20 4f 50 5f 45 78 70 69 72  ddOp(v, OP_Expir
1cb17 65 2c 20 31 2c 20 30 29 3b 0a 0a 20 20 20 20 2f  e, 1, 0);..    /
1cb18 2a 0a 20 20 20 20 2a 2a 20 52 65 73 65 74 20 74  *.    ** Reset t
1cb19 68 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2c  he safety level,
1cb1a 20 69 6e 20 63 61 73 65 20 74 68 65 20 66 75 6c   in case the ful
1cb1b 6c 66 73 79 6e 63 20 66 6c 61 67 20 6f 72 20 73  lfsync flag or s
1cb1c 79 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 20 20 2a  ynchronous.    *
1cb1d 2a 20 73 65 74 74 69 6e 67 20 63 68 61 6e 67 65  * setting change
1cb1e 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65  d..    */.#ifnde
1cb1f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  f SQLITE_OMIT_PA
1cb20 47 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 20 20  GER_PRAGMAS.    
1cb21 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  if( db->autoComm
1cb22 69 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  it ){.      sqli
1cb23 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74  te3BtreeSetSafet
1cb24 79 4c 65 76 65 6c 28 70 44 62 2d 3e 70 42 74 2c  yLevel(pDb->pBt,
1cb25 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76   pDb->safety_lev
1cb26 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  el,.            
1cb27 20 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26       (db->flags&
1cb28 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63  SQLITE_FullFSync
1cb29 29 21 3d 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  )!=0);.    }.#en
1cb2a 64 69 66 0a 20 20 7d 0a 70 72 61 67 6d 61 5f 6f  dif.  }.pragma_o
1cb2b 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  ut:.  sqlite3_fr
1cb2c 65 65 28 7a 4c 65 66 74 29 3b 0a 20 20 73 71 6c  ee(zLeft);.  sql
1cb2d 69 74 65 33 5f 66 72 65 65 28 7a 52 69 67 68 74  ite3_free(zRight
1cb2e 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  );.}..#endif /* 
1cb2f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47  SQLITE_OMIT_PRAG
1cb30 4d 41 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4d 49  MA || SQLITE_OMI
1cb31 54 5f 50 41 52 53 45 52 20 2a 2f 0a 0a 2f 2a 2a  T_PARSER */../**
1cb32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
1cb33 20 6f 66 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a   of pragma.c ***
1cb34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1cb37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1cb38 69 6e 20 66 69 6c 65 20 70 72 65 70 61 72 65 2e  in file prepare.
1cb39 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1cb3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1cb3c 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 35 0a 2a  ** 2005 May 25.*
1cb3d 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1cb3e 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1cb3f 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1cb40 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1cb41 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
1cb42 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1cb43 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1cb44 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1cb45 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1cb46 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1cb47 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1cb48 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1cb49 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1cb4a 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1cb4b 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1cb4c 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1cb4d 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1cb4e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1cb4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1cb53 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
1cb54 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ins the implemen
1cb55 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
1cb56 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 0a  lite3_prepare().
1cb57 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e  ** interface, an
1cb58 64 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  d routines that 
1cb59 63 6f 6e 74 72 69 62 75 74 65 20 74 6f 20 6c 6f  contribute to lo
1cb5a 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
1cb5b 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20 66 72 6f  se schema.** fro
1cb5c 6d 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 24 49  m disk..**.** $I
1cb5d 64 3a 20 70 72 65 70 61 72 65 2e 63 2c 76 20 31  d: prepare.c,v 1
1cb5e 2e 36 30 20 32 30 30 37 2f 30 38 2f 32 39 20 31  .60 2007/08/29 1
1cb5f 32 3a 33 31 3a 32 37 20 64 61 6e 69 65 6c 6b 31  2:31:27 danielk1
1cb60 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
1cb61 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 69  .** Fill the Ini
1cb62 74 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20  tData structure 
1cb63 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
1cb64 73 73 61 67 65 20 74 68 61 74 20 69 6e 64 69 63  ssage that indic
1cb65 61 74 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65  ates.** that the
1cb66 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
1cb67 72 75 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rupt..*/.static 
1cb68 76 6f 69 64 20 63 6f 72 72 75 70 74 53 63 68 65  void corruptSche
1cb69 6d 61 28 49 6e 69 74 44 61 74 61 20 2a 70 44 61  ma(InitData *pDa
1cb6a 74 61 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ta, const char *
1cb6b 7a 45 78 74 72 61 29 7b 0a 20 20 69 66 28 20 21  zExtra){.  if( !
1cb6c 70 44 61 74 61 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  pData->db->mallo
1cb6d 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73  cFailed ){.    s
1cb6e 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1cb6f 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c  pData->pzErrMsg,
1cb70 20 22 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 61   "malformed data
1cb71 62 61 73 65 20 73 63 68 65 6d 61 22 2c 0a 20 20  base schema",.  
1cb72 20 20 20 20 20 7a 45 78 74 72 61 21 3d 30 20 26       zExtra!=0 &
1cb73 26 20 7a 45 78 74 72 61 5b 30 5d 21 3d 30 20 3f  & zExtra[0]!=0 ?
1cb74 20 22 20 2d 20 22 20 3a 20 28 63 68 61 72 2a 29   " - " : (char*)
1cb75 30 2c 20 7a 45 78 74 72 61 2c 20 28 63 68 61 72  0, zExtra, (char
1cb76 2a 29 30 29 3b 0a 20 20 7d 0a 20 20 70 44 61 74  *)0);.  }.  pDat
1cb77 61 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 43  a->rc = SQLITE_C
1cb78 4f 52 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ORRUPT;.}../*.**
1cb79 20 54 68 69 73 20 69 73 20 74 68 65 20 63 61 6c   This is the cal
1cb7a 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 66 6f  lback routine fo
1cb7b 72 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20  r the code that 
1cb7c 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 0a  initializes the.
1cb7d 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 53 65  ** database.  Se
1cb7e 65 20 73 71 6c 69 74 65 33 49 6e 69 74 28 29 20  e sqlite3Init() 
1cb7f 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69  below for additi
1cb80 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1cb81 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1cb82 65 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64  e is also called
1cb83 20 66 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61 72   from the OP_Par
1cb84 73 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 20  seSchema opcode 
1cb85 6f 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a 0a  of the VDBE..**.
1cb86 2a 2a 20 45 61 63 68 20 63 61 6c 6c 62 61 63 6b  ** Each callback
1cb87 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f   contains the fo
1cb88 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74  llowing informat
1cb89 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61  ion:.**.**     a
1cb8a 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66  rgv[0] = name of
1cb8b 20 74 68 69 6e 67 20 62 65 69 6e 67 20 63 72 65   thing being cre
1cb8c 61 74 65 64 0a 2a 2a 20 20 20 20 20 61 72 67 76  ated.**     argv
1cb8d 5b 31 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65 20  [1] = root page 
1cb8e 6e 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65  number for table
1cb8f 20 6f 72 20 69 6e 64 65 78 2e 20 30 20 66 6f 72   or index. 0 for
1cb90 20 74 72 69 67 67 65 72 20 6f 72 20 76 69 65 77   trigger or view
1cb91 2e 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 32 5d  ..**     argv[2]
1cb92 20 3d 20 53 51 4c 20 74 65 78 74 20 66 6f 72 20   = SQL text for 
1cb93 74 68 65 20 43 52 45 41 54 45 20 73 74 61 74 65  the CREATE state
1cb94 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49  ment..**.*/.SQLI
1cb95 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1cb96 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61  qlite3InitCallba
1cb97 63 6b 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c 20  ck(void *pInit, 
1cb98 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a  int argc, char *
1cb99 2a 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 61 7a  *argv, char **az
1cb9a 43 6f 6c 4e 61 6d 65 29 7b 0a 20 20 49 6e 69 74  ColName){.  Init
1cb9b 44 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 49  Data *pData = (I
1cb9c 6e 69 74 44 61 74 61 2a 29 70 49 6e 69 74 3b 0a  nitData*)pInit;.
1cb9d 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1cb9e 70 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 6e 74  pData->db;.  int
1cb9f 20 69 44 62 20 3d 20 70 44 61 74 61 2d 3e 69 44   iDb = pData->iD
1cba0 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  b;..  assert( sq
1cba1 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1cba2 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
1cba3 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 53 51 4c   pData->rc = SQL
1cba4 49 54 45 5f 4f 4b 3b 0a 20 20 44 62 43 6c 65 61  ITE_OK;.  DbClea
1cba5 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44  rProperty(db, iD
1cba6 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20  b, DB_Empty);.  
1cba7 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1cba8 69 6c 65 64 20 29 7b 0a 20 20 20 20 63 6f 72 72  iled ){.    corr
1cba9 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c  uptSchema(pData,
1cbaa 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   0);.    return 
1cbab 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1cbac 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  }..  assert( arg
1cbad 63 3d 3d 33 20 29 3b 0a 20 20 69 66 28 20 61 72  c==3 );.  if( ar
1cbae 67 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  gv==0 ) return 0
1cbaf 3b 20 20 20 2f 2a 20 4d 69 67 68 74 20 68 61 70  ;   /* Might hap
1cbb0 70 65 6e 20 69 66 20 45 4d 50 54 59 5f 52 45 53  pen if EMPTY_RES
1cbb1 55 4c 54 5f 43 41 4c 4c 42 41 43 4b 53 20 61 72  ULT_CALLBACKS ar
1cbb2 65 20 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 61 72  e on */.  if( ar
1cbb3 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  gv[1]==0 ){.    
1cbb4 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44  corruptSchema(pD
1cbb5 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 72 65 74  ata, 0);.    ret
1cbb6 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 1;.  }.  ass
1cbb7 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
1cbb8 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
1cbb9 69 66 28 20 61 72 67 76 5b 32 5d 20 26 26 20 61  if( argv[2] && a
1cbba 72 67 76 5b 32 5d 5b 30 5d 20 29 7b 0a 20 20 20  rgv[2][0] ){.   
1cbbb 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 61 72   /* Call the par
1cbbc 73 65 72 20 74 6f 20 70 72 6f 63 65 73 73 20 61  ser to process a
1cbbd 20 43 52 45 41 54 45 20 54 41 42 4c 45 2c 20 49   CREATE TABLE, I
1cbbe 4e 44 45 58 20 6f 72 20 56 49 45 57 2e 0a 20 20  NDEX or VIEW..  
1cbbf 20 20 2a 2a 20 42 75 74 20 62 65 63 61 75 73 65    ** But because
1cbc0 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69   db->init.busy i
1cbc1 73 20 73 65 74 20 74 6f 20 31 2c 20 6e 6f 20 56  s set to 1, no V
1cbc2 44 42 45 20 63 6f 64 65 20 69 73 20 67 65 6e 65  DBE code is gene
1cbc3 72 61 74 65 64 0a 20 20 20 20 2a 2a 20 6f 72 20  rated.    ** or 
1cbc4 65 78 65 63 75 74 65 64 2e 20 20 41 6c 6c 20 74  executed.  All t
1cbc5 68 65 20 70 61 72 73 65 72 20 64 6f 65 73 20 69  he parser does i
1cbc6 73 20 62 75 69 6c 64 20 74 68 65 20 69 6e 74 65  s build the inte
1cbc7 72 6e 61 6c 20 64 61 74 61 0a 20 20 20 20 2a 2a  rnal data.    **
1cbc8 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74   structures that
1cbc9 20 64 65 73 63 72 69 62 65 20 74 68 65 20 74 61   describe the ta
1cbca 62 6c 65 2c 20 69 6e 64 65 78 2c 20 6f 72 20 76  ble, index, or v
1cbcb 69 65 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  iew..    */.    
1cbcc 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20  char *zErr;.    
1cbcd 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65  int rc;.    asse
1cbce 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  rt( db->init.bus
1cbcf 79 20 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69  y );.    db->ini
1cbd0 74 2e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20  t.iDb = iDb;.   
1cbd1 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75   db->init.newTnu
1cbd2 6d 20 3d 20 61 74 6f 69 28 61 72 67 76 5b 31 5d  m = atoi(argv[1]
1cbd3 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1cbd4 74 65 33 5f 65 78 65 63 28 64 62 2c 20 61 72 67  te3_exec(db, arg
1cbd5 76 5b 32 5d 2c 20 30 2c 20 30 2c 20 26 7a 45 72  v[2], 0, 0, &zEr
1cbd6 72 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74  r);.    db->init
1cbd7 2e 69 44 62 20 3d 20 30 3b 0a 20 20 20 20 61 73  .iDb = 0;.    as
1cbd8 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
1cbd9 5f 4f 4b 20 7c 7c 20 7a 45 72 72 3d 3d 30 20 29  _OK || zErr==0 )
1cbda 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  ;.    if( SQLITE
1cbdb 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20  _OK!=rc ){.     
1cbdc 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b   pData->rc = rc;
1cbdd 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
1cbde 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
1cbdf 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
1cbe0 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
1cbe1 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21     }else if( rc!
1cbe2 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50  =SQLITE_INTERRUP
1cbe3 54 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 72  T ){.        cor
1cbe4 72 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61  ruptSchema(pData
1cbe5 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d  , zErr);.      }
1cbe6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
1cbe7 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 20  ree(zErr);.     
1cbe8 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1cbe9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1cbea 20 49 66 20 74 68 65 20 53 51 4c 20 63 6f 6c 75   If the SQL colu
1cbeb 6d 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74 20 6d  mn is blank it m
1cbec 65 61 6e 73 20 74 68 69 73 20 69 73 20 61 6e 20  eans this is an 
1cbed 69 6e 64 65 78 20 74 68 61 74 0a 20 20 20 20 2a  index that.    *
1cbee 2a 20 77 61 73 20 63 72 65 61 74 65 64 20 74 6f  * was created to
1cbef 20 62 65 20 74 68 65 20 50 52 49 4d 41 52 59 20   be the PRIMARY 
1cbf0 4b 45 59 20 6f 72 20 74 6f 20 66 75 6c 66 69 6c  KEY or to fulfil
1cbf1 6c 20 61 20 55 4e 49 51 55 45 0a 20 20 20 20 2a  l a UNIQUE.    *
1cbf2 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f 72  * constraint for
1cbf3 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e   a CREATE TABLE.
1cbf4 20 20 54 68 65 20 69 6e 64 65 78 20 73 68 6f 75    The index shou
1cbf5 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a  ld have already.
1cbf6 20 20 20 20 2a 2a 20 62 65 65 6e 20 63 72 65 61      ** been crea
1cbf7 74 65 64 20 77 68 65 6e 20 77 65 20 70 72 6f 63  ted when we proc
1cbf8 65 73 73 65 64 20 74 68 65 20 43 52 45 41 54 45  essed the CREATE
1cbf9 20 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77 65 20   TABLE.  All we 
1cbfa 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64  have.    ** to d
1cbfb 6f 20 68 65 72 65 20 69 73 20 72 65 63 6f 72 64  o here is record
1cbfc 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
1cbfd 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 69  umber for that i
1cbfe 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ndex..    */.   
1cbff 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a   Index *pIndex;.
1cc00 20 20 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c      pIndex = sql
1cc01 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62  ite3FindIndex(db
1cc02 2c 20 61 72 67 76 5b 30 5d 2c 20 64 62 2d 3e 61  , argv[0], db->a
1cc03 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a  Db[iDb].zName);.
1cc04 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d      if( pIndex==
1cc05 30 20 7c 7c 20 70 49 6e 64 65 78 2d 3e 74 6e 75  0 || pIndex->tnu
1cc06 6d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  m!=0 ){.      /*
1cc07 20 54 68 69 73 20 63 61 6e 20 6f 63 63 75 72 20   This can occur 
1cc08 69 66 20 74 68 65 72 65 20 65 78 69 73 74 73 20  if there exists 
1cc09 61 6e 20 69 6e 64 65 78 20 6f 6e 20 61 20 54 45  an index on a TE
1cc0a 4d 50 20 74 61 62 6c 65 20 77 68 69 63 68 0a 20  MP table which. 
1cc0b 20 20 20 20 20 2a 2a 20 68 61 73 20 74 68 65 20       ** has the 
1cc0c 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 61 6e 6f  same name as ano
1cc0d 74 68 65 72 20 69 6e 64 65 78 20 6f 6e 20 61 20  ther index on a 
1cc0e 70 65 72 6d 61 6e 65 6e 74 20 69 6e 64 65 78 2e  permanent index.
1cc0f 20 20 53 69 6e 63 65 0a 20 20 20 20 20 20 2a 2a    Since.      **
1cc10 20 74 68 65 20 70 65 72 6d 61 6e 65 6e 74 20 74   the permanent t
1cc11 61 62 6c 65 20 69 73 20 68 69 64 64 65 6e 20 62  able is hidden b
1cc12 79 20 74 68 65 20 54 45 4d 50 20 74 61 62 6c 65  y the TEMP table
1cc13 2c 20 77 65 20 63 61 6e 20 61 6c 73 6f 0a 20 20  , we can also.  
1cc14 20 20 20 20 2a 2a 20 73 61 66 65 6c 79 20 69 67      ** safely ig
1cc15 6e 6f 72 65 20 74 68 65 20 69 6e 64 65 78 20 6f  nore the index o
1cc16 6e 20 74 68 65 20 70 65 72 6d 61 6e 65 6e 74 20  n the permanent 
1cc17 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  table..      */.
1cc18 20 20 20 20 20 20 2f 2a 20 44 6f 20 4e 6f 74 68        /* Do Noth
1cc19 69 6e 67 20 2a 2f 3b 0a 20 20 20 20 7d 65 6c 73  ing */;.    }els
1cc1a 65 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d  e{.      pIndex-
1cc1b 3e 74 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72 67  >tnum = atoi(arg
1cc1c 76 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  v[1]);.    }.  }
1cc1d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1cc1e 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
1cc1f 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62 61   read the databa
1cc20 73 65 20 73 63 68 65 6d 61 20 61 6e 64 20 69 6e  se schema and in
1cc21 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72 6e 61  itialize interna
1cc22 6c 0a 2a 2a 20 64 61 74 61 20 73 74 72 75 63 74  l.** data struct
1cc23 75 72 65 73 20 66 6f 72 20 61 20 73 69 6e 67 6c  ures for a singl
1cc24 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1cc25 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74    The index of t
1cc26 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
1cc27 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20  ile is given by 
1cc28 69 44 62 2e 20 20 69 44 62 3d 3d 30 20 69 73 20  iDb.  iDb==0 is 
1cc29 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61 69  used for the mai
1cc2a 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20  n.** database.  
1cc2b 69 44 62 3d 3d 31 20 73 68 6f 75 6c 64 20 6e 65  iDb==1 should ne
1cc2c 76 65 72 20 62 65 20 75 73 65 64 2e 20 20 69 44  ver be used.  iD
1cc2d 62 3e 3d 32 20 69 73 20 75 73 65 64 20 66 6f 72  b>=2 is used for
1cc2e 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61  .** auxiliary da
1cc2f 74 61 62 61 73 65 73 2e 20 20 52 65 74 75 72 6e  tabases.  Return
1cc30 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49   one of the SQLI
1cc31 54 45 5f 20 65 72 72 6f 72 20 63 6f 64 65 73 20  TE_ error codes 
1cc32 74 6f 0a 2a 2a 20 69 6e 64 69 63 61 74 65 20 73  to.** indicate s
1cc33 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72  uccess or failur
1cc34 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1cc35 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28   sqlite3InitOne(
1cc36 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
1cc37 20 69 44 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45   iDb, char **pzE
1cc38 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 72 63  rrMsg){.  int rc
1cc39 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 63 75  ;.  BtCursor *cu
1cc3a 72 4d 61 69 6e 3b 0a 20 20 69 6e 74 20 73 69 7a  rMain;.  int siz
1cc3b 65 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  e;.  Table *pTab
1cc3c 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 63  ;.  Db *pDb;.  c
1cc3d 68 61 72 20 63 6f 6e 73 74 20 2a 61 7a 41 72 67  har const *azArg
1cc3e 5b 34 5d 3b 0a 20 20 69 6e 74 20 6d 65 74 61 5b  [4];.  int meta[
1cc3f 31 30 5d 3b 0a 20 20 49 6e 69 74 44 61 74 61 20  10];.  InitData 
1cc40 69 6e 69 74 44 61 74 61 3b 0a 20 20 63 68 61 72  initData;.  char
1cc41 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72 53   const *zMasterS
1cc42 63 68 65 6d 61 3b 0a 20 20 63 68 61 72 20 63 6f  chema;.  char co
1cc43 6e 73 74 20 2a 7a 4d 61 73 74 65 72 4e 61 6d 65  nst *zMasterName
1cc44 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28   = SCHEMA_TABLE(
1cc45 69 44 62 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  iDb);..  /*.  **
1cc46 20 54 68 65 20 6d 61 73 74 65 72 20 64 61 74 61   The master data
1cc47 62 61 73 65 20 74 61 62 6c 65 20 68 61 73 20 61  base table has a
1cc48 20 73 74 72 75 63 74 75 72 65 20 6c 69 6b 65 20   structure like 
1cc49 74 68 69 73 0a 20 20 2a 2f 0a 20 20 73 74 61 74  this.  */.  stat
1cc4a 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6d 61  ic const char ma
1cc4b 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20 3d 20  ster_schema[] = 
1cc4c 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41  .     "CREATE TA
1cc4d 42 4c 45 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  BLE sqlite_maste
1cc4e 72 28 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 79  r(\n".     "  ty
1cc4f 70 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20  pe text,\n".    
1cc50 20 22 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e   "  name text,\n
1cc51 22 0a 20 20 20 20 20 22 20 20 74 62 6c 5f 6e 61  ".     "  tbl_na
1cc52 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20  me text,\n".    
1cc53 20 22 20 20 72 6f 6f 74 70 61 67 65 20 69 6e 74   "  rootpage int
1cc54 65 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20  eger,\n".     " 
1cc55 20 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20   sql text\n".   
1cc56 20 20 22 29 22 0a 20 20 3b 0a 23 69 66 6e 64 65    ")".  ;.#ifnde
1cc57 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
1cc58 4d 50 44 42 0a 20 20 73 74 61 74 69 63 20 63 6f  MPDB.  static co
1cc59 6e 73 74 20 63 68 61 72 20 74 65 6d 70 5f 6d 61  nst char temp_ma
1cc5a 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20 3d 20  ster_schema[] = 
1cc5b 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54 45  .     "CREATE TE
1cc5c 4d 50 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f  MP TABLE sqlite_
1cc5d 74 65 6d 70 5f 6d 61 73 74 65 72 28 5c 6e 22 0a  temp_master(\n".
1cc5e 20 20 20 20 20 22 20 20 74 79 70 65 20 74 65 78       "  type tex
1cc5f 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 6e 61  t,\n".     "  na
1cc60 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20  me text,\n".    
1cc61 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74 65 78   "  tbl_name tex
1cc62 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 72 6f  t,\n".     "  ro
1cc63 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72 2c 5c  otpage integer,\
1cc64 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c 20 74  n".     "  sql t
1cc65 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29 22 0a  ext\n".     ")".
1cc66 20 20 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66    ;.#else.  #def
1cc67 69 6e 65 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f  ine temp_master_
1cc68 73 63 68 65 6d 61 20 30 0a 23 65 6e 64 69 66 0a  schema 0.#endif.
1cc69 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
1cc6a 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
1cc6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
1cc6c 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
1cc6d 6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ma );.  assert( 
1cc6e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1cc6f 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
1cc70 0a 0a 20 20 2f 2a 20 7a 4d 61 73 74 65 72 53 63  ..  /* zMasterSc
1cc71 68 65 6d 61 20 61 6e 64 20 7a 49 6e 69 74 53 63  hema and zInitSc
1cc72 72 69 70 74 20 61 72 65 20 73 65 74 20 74 6f 20  ript are set to 
1cc73 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6d 61 73  point at the mas
1cc74 74 65 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20  ter schema.  ** 
1cc75 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 61 74 69  and initialisati
1cc76 6f 6e 20 73 63 72 69 70 74 20 61 70 70 72 6f 70  on script approp
1cc77 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 64 61  riate for the da
1cc78 74 61 62 61 73 65 20 62 65 69 6e 67 0a 20 20 2a  tabase being.  *
1cc79 2a 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 20 7a  * initialised. z
1cc7a 4d 61 73 74 65 72 4e 61 6d 65 20 69 73 20 74 68  MasterName is th
1cc7b 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61  e name of the ma
1cc7c 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  ster table..  */
1cc7d 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d  .  if( !OMIT_TEM
1cc7e 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b  PDB && iDb==1 ){
1cc7f 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63 68 65  .    zMasterSche
1cc80 6d 61 20 3d 20 74 65 6d 70 5f 6d 61 73 74 65 72  ma = temp_master
1cc81 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 65 6c 73 65  _schema;.  }else
1cc82 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63 68  {.    zMasterSch
1cc83 65 6d 61 20 3d 20 6d 61 73 74 65 72 5f 73 63 68  ema = master_sch
1cc84 65 6d 61 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74  ema;.  }.  zMast
1cc85 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f  erName = SCHEMA_
1cc86 54 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20 20 2f  TABLE(iDb);..  /
1cc87 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20  * Construct the 
1cc88 73 63 68 65 6d 61 20 74 61 62 6c 65 73 2e 20 20  schema tables.  
1cc89 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 61 66 65  */.  sqlite3Safe
1cc8a 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 61 7a 41  tyOff(db);.  azA
1cc8b 72 67 5b 30 5d 20 3d 20 7a 4d 61 73 74 65 72 4e  rg[0] = zMasterN
1cc8c 61 6d 65 3b 0a 20 20 61 7a 41 72 67 5b 31 5d 20  ame;.  azArg[1] 
1cc8d 3d 20 22 31 22 3b 0a 20 20 61 7a 41 72 67 5b 32  = "1";.  azArg[2
1cc8e 5d 20 3d 20 7a 4d 61 73 74 65 72 53 63 68 65 6d  ] = zMasterSchem
1cc8f 61 3b 0a 20 20 61 7a 41 72 67 5b 33 5d 20 3d 20  a;.  azArg[3] = 
1cc90 30 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 64 62  0;.  initData.db
1cc91 20 3d 20 64 62 3b 0a 20 20 69 6e 69 74 44 61 74   = db;.  initDat
1cc92 61 2e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 69  a.iDb = iDb;.  i
1cc93 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67  nitData.pzErrMsg
1cc94 20 3d 20 70 7a 45 72 72 4d 73 67 3b 0a 20 20 72   = pzErrMsg;.  r
1cc95 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 43  c = sqlite3InitC
1cc96 61 6c 6c 62 61 63 6b 28 26 69 6e 69 74 44 61 74  allback(&initDat
1cc97 61 2c 20 33 2c 20 28 63 68 61 72 20 2a 2a 29 61  a, 3, (char **)a
1cc98 7a 41 72 67 2c 20 30 29 3b 0a 20 20 69 66 28 20  zArg, 0);.  if( 
1cc99 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rc ){.    sqlite
1cc9a 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
1cc9b 20 20 20 72 63 20 3d 20 69 6e 69 74 44 61 74 61     rc = initData
1cc9c 2e 72 63 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72  .rc;.    goto er
1cc9d 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70  ror_out;.  }.  p
1cc9e 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Tab = sqlite3Fin
1cc9f 64 54 61 62 6c 65 28 64 62 2c 20 7a 4d 61 73 74  dTable(db, zMast
1cca0 65 72 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b  erName, db->aDb[
1cca1 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69  iDb].zName);.  i
1cca2 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 70  f( pTab ){.    p
1cca3 54 61 62 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20  Tab->readOnly = 
1cca4 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  1;.  }.  sqlite3
1cca5 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 0a 20  SafetyOn(db);.. 
1cca6 20 2f 2a 20 43 72 65 61 74 65 20 61 20 63 75 72   /* Create a cur
1cca7 73 6f 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  sor to hold the 
1cca8 64 61 74 61 62 61 73 65 20 6f 70 65 6e 0a 20 20  database open.  
1cca9 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  */.  pDb = &db->
1ccaa 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66 28 20  aDb[iDb];.  if( 
1ccab 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20  pDb->pBt==0 ){. 
1ccac 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
1ccad 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b  PDB && iDb==1 ){
1ccae 0a 20 20 20 20 20 20 44 62 53 65 74 50 72 6f 70  .      DbSetProp
1ccaf 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53  erty(db, 1, DB_S
1ccb0 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20  chemaLoaded);.  
1ccb1 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53    }.    return S
1ccb2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
1ccb3 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1ccb4 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 72  r(pDb->pBt);.  r
1ccb5 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1ccb6 43 75 72 73 6f 72 28 70 44 62 2d 3e 70 42 74 2c  Cursor(pDb->pBt,
1ccb7 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 30 2c   MASTER_ROOT, 0,
1ccb8 20 30 2c 20 30 2c 20 26 63 75 72 4d 61 69 6e 29   0, 0, &curMain)
1ccb9 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1ccba 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c  TE_OK && rc!=SQL
1ccbb 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20  ITE_EMPTY ){.   
1ccbc 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1ccbd 67 28 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69  g(pzErrMsg, sqli
1ccbe 74 65 33 45 72 72 53 74 72 28 72 63 29 2c 20 28  te3ErrStr(rc), (
1ccbf 63 68 61 72 2a 29 30 29 3b 0a 20 20 20 20 73 71  char*)0);.    sq
1ccc0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
1ccc1 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 67  pDb->pBt);.    g
1ccc2 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20  oto error_out;. 
1ccc3 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65   }..  /* Get the
1ccc4 20 64 61 74 61 62 61 73 65 20 6d 65 74 61 20 69   database meta i
1ccc5 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a  nformation..  **
1ccc6 0a 20 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65  .  ** Meta value
1ccc7 73 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73  s are as follows
1ccc8 3a 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30  :.  **    meta[0
1ccc9 5d 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69  ]   Schema cooki
1ccca 65 2e 20 20 43 68 61 6e 67 65 73 20 77 69 74 68  e.  Changes with
1cccb 20 65 61 63 68 20 73 63 68 65 6d 61 20 63 68 61   each schema cha
1cccc 6e 67 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74  nge..  **    met
1cccd 61 5b 31 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d  a[1]   File form
1ccce 61 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79  at of schema lay
1cccf 65 72 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  er..  **    meta
1ccd0 5b 32 5d 20 20 20 53 69 7a 65 20 6f 66 20 74 68  [2]   Size of th
1ccd1 65 20 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20  e page cache..  
1ccd2 2a 2a 20 20 20 20 6d 65 74 61 5b 33 5d 20 20 20  **    meta[3]   
1ccd3 55 73 65 20 66 72 65 65 6c 69 73 74 20 69 66 20  Use freelist if 
1ccd4 30 2e 20 20 41 75 74 6f 76 61 63 75 75 6d 20 69  0.  Autovacuum i
1ccd5 66 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a  f greater than z
1ccd6 65 72 6f 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74  ero..  **    met
1ccd7 61 5b 34 5d 20 20 20 44 62 20 74 65 78 74 20 65  a[4]   Db text e
1ccd8 6e 63 6f 64 69 6e 67 2e 20 31 3a 55 54 46 2d 38  ncoding. 1:UTF-8
1ccd9 20 32 3a 55 54 46 2d 31 36 4c 45 20 33 3a 55 54   2:UTF-16LE 3:UT
1ccda 46 2d 31 36 42 45 0a 20 20 2a 2a 20 20 20 20 6d  F-16BE.  **    m
1ccdb 65 74 61 5b 35 5d 20 20 20 54 68 65 20 75 73 65  eta[5]   The use
1ccdc 72 20 63 6f 6f 6b 69 65 2e 20 55 73 65 64 20 62  r cookie. Used b
1ccdd 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  y the applicatio
1ccde 6e 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  n..  **    meta[
1ccdf 36 5d 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c  6]   Incremental
1cce0 2d 76 61 63 75 75 6d 20 66 6c 61 67 2e 0a 20 20  -vacuum flag..  
1cce1 2a 2a 20 20 20 20 6d 65 74 61 5b 37 5d 0a 20 20  **    meta[7].  
1cce2 2a 2a 20 20 20 20 6d 65 74 61 5b 38 5d 0a 20 20  **    meta[8].  
1cce3 2a 2a 20 20 20 20 6d 65 74 61 5b 39 5d 0a 20 20  **    meta[9].  
1cce4 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68  **.  ** Note: Th
1cce5 65 20 23 64 65 66 69 6e 65 64 20 53 51 4c 49 54  e #defined SQLIT
1cce6 45 5f 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69  E_UTF* symbols i
1cce7 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f  n sqliteInt.h co
1cce8 72 72 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a  rrespond to.  **
1cce9 20 74 68 65 20 70 6f 73 73 69 62 6c 65 20 76 61   the possible va
1ccea 6c 75 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e  lues of meta[4].
1cceb 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
1ccec 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1cced 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
1ccee 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
1ccef 4f 4b 20 26 26 20 69 3c 73 69 7a 65 6f 66 28 6d  OK && i<sizeof(m
1ccf0 65 74 61 29 2f 73 69 7a 65 6f 66 28 6d 65 74 61  eta)/sizeof(meta
1ccf1 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  [0]); i++){.    
1ccf2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1ccf3 72 65 65 47 65 74 4d 65 74 61 28 70 44 62 2d 3e  reeGetMeta(pDb->
1ccf4 70 42 74 2c 20 69 2b 31 2c 20 28 75 33 32 20 2a  pBt, i+1, (u32 *
1ccf5 29 26 6d 65 74 61 5b 69 5d 29 3b 0a 20 20 20 20  )&meta[i]);.    
1ccf6 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  }.    if( rc ){.
1ccf7 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74        sqlite3Set
1ccf8 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c  String(pzErrMsg,
1ccf9 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72   sqlite3ErrStr(r
1ccfa 63 29 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20  c), (char*)0);. 
1ccfb 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1ccfc 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 63 75 72  eCloseCursor(cur
1ccfd 4d 61 69 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c  Main);.      sql
1ccfe 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1ccff 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20  Db->pBt);.      
1cd00 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a  goto error_out;.
1cd01 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1cd02 20 20 20 6d 65 6d 73 65 74 28 6d 65 74 61 2c 20     memset(meta, 
1cd03 30 2c 20 73 69 7a 65 6f 66 28 6d 65 74 61 29 29  0, sizeof(meta))
1cd04 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63  ;.  }.  pDb->pSc
1cd05 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
1cd06 6b 69 65 20 3d 20 6d 65 74 61 5b 30 5d 3b 0a 0a  kie = meta[0];..
1cd07 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20    /* If opening 
1cd08 61 20 6e 6f 6e 2d 65 6d 70 74 79 20 64 61 74 61  a non-empty data
1cd09 62 61 73 65 2c 20 63 68 65 63 6b 20 74 68 65 20  base, check the 
1cd0a 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 46  text encoding. F
1cd0b 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 69 6e  or the.  ** main
1cd0c 20 64 61 74 61 62 61 73 65 2c 20 73 65 74 20 73   database, set s
1cd0d 71 6c 69 74 65 33 2e 65 6e 63 20 74 6f 20 74 68  qlite3.enc to th
1cd0e 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  e encoding of th
1cd0f 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
1cd10 0a 20 20 2a 2a 20 46 6f 72 20 61 6e 20 61 74 74  .  ** For an att
1cd11 61 63 68 65 64 20 64 62 2c 20 69 74 20 69 73 20  ached db, it is 
1cd12 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20  an error if the 
1cd13 65 6e 63 6f 64 69 6e 67 20 69 73 20 6e 6f 74 20  encoding is not 
1cd14 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 73  the same.  ** as
1cd15 20 73 71 6c 69 74 65 33 2e 65 6e 63 2e 0a 20 20   sqlite3.enc..  
1cd16 2a 2f 0a 20 20 69 66 28 20 6d 65 74 61 5b 34 5d  */.  if( meta[4]
1cd17 20 29 7b 20 20 2f 2a 20 74 65 78 74 20 65 6e 63   ){  /* text enc
1cd18 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 69 66 28  oding */.    if(
1cd19 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20   iDb==0 ){.     
1cd1a 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 74   /* If opening t
1cd1b 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1cd1c 2c 20 73 65 74 20 45 4e 43 28 64 62 29 2e 20 2a  , set ENC(db). *
1cd1d 2f 0a 20 20 20 20 20 20 45 4e 43 28 64 62 29 20  /.      ENC(db) 
1cd1e 3d 20 28 75 38 29 6d 65 74 61 5b 34 5d 3b 0a 20  = (u8)meta[4];. 
1cd1f 20 20 20 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f       db->pDfltCo
1cd20 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ll = sqlite3Find
1cd21 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49  CollSeq(db, SQLI
1cd22 54 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59  TE_UTF8, "BINARY
1cd23 22 2c 20 36 2c 20 30 29 3b 0a 20 20 20 20 7d 65  ", 6, 0);.    }e
1cd24 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  lse{.      /* If
1cd25 20 6f 70 65 6e 69 6e 67 20 61 6e 20 61 74 74 61   opening an atta
1cd26 63 68 65 64 20 64 61 74 61 62 61 73 65 2c 20 74  ched database, t
1cd27 68 65 20 65 6e 63 6f 64 69 6e 67 20 6d 75 63 68  he encoding much
1cd28 20 6d 61 74 63 68 20 45 4e 43 28 64 62 29 20 2a   match ENC(db) *
1cd29 2f 0a 20 20 20 20 20 20 69 66 28 20 6d 65 74 61  /.      if( meta
1cd2a 5b 34 5d 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a  [4]!=ENC(db) ){.
1cd2b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1cd2c 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28  treeCloseCursor(
1cd2d 63 75 72 4d 61 69 6e 29 3b 0a 20 20 20 20 20 20  curMain);.      
1cd2e 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1cd2f 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 22 61 74  ng(pzErrMsg, "at
1cd30 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
1cd31 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73 61   must use the sa
1cd32 6d 65 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  me".            
1cd33 22 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  " text encoding 
1cd34 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  as main database
1cd35 22 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  ", (char*)0);.  
1cd36 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1cd37 65 65 4c 65 61 76 65 28 70 44 62 2d 3e 70 42 74  eeLeave(pDb->pBt
1cd38 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1cd39 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1cd3a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1cd3b 7d 65 6c 73 65 7b 0a 20 20 20 20 44 62 53 65 74  }else{.    DbSet
1cd3c 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62  Property(db, iDb
1cd3d 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20 7d  , DB_Empty);.  }
1cd3e 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  .  pDb->pSchema-
1cd3f 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a  >enc = ENC(db);.
1cd40 0a 20 20 73 69 7a 65 20 3d 20 6d 65 74 61 5b 32  .  size = meta[2
1cd41 5d 3b 0a 20 20 69 66 28 20 73 69 7a 65 3d 3d 30  ];.  if( size==0
1cd42 20 29 7b 20 73 69 7a 65 20 3d 20 53 51 4c 49 54   ){ size = SQLIT
1cd43 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
1cd44 53 49 5a 45 3b 20 7d 0a 20 20 70 44 62 2d 3e 70  SIZE; }.  pDb->p
1cd45 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
1cd46 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 73 71 6c  ze = size;.  sql
1cd47 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
1cd48 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20  eSize(pDb->pBt, 
1cd49 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
1cd4a 63 68 65 5f 73 69 7a 65 29 3b 0a 0a 20 20 2f 2a  che_size);..  /*
1cd4b 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61  .  ** file_forma
1cd4c 74 3d 3d 31 20 20 20 20 56 65 72 73 69 6f 6e 20  t==1    Version 
1cd4d 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69 6c 65  3.0.0..  ** file
1cd4e 5f 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20 56 65  _format==2    Ve
1cd4f 72 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20 2f 2f  rsion 3.1.3.  //
1cd50 20 41 4c 54 45 52 20 54 41 42 4c 45 20 41 44 44   ALTER TABLE ADD
1cd51 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66 69 6c   COLUMN.  ** fil
1cd52 65 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20 20 56  e_format==3    V
1cd53 65 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20 20 2f  ersion 3.1.4.  /
1cd54 2f 20 64 69 74 74 6f 20 62 75 74 20 77 69 74 68  / ditto but with
1cd55 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c   non-NULL defaul
1cd56 74 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  ts.  ** file_for
1cd57 6d 61 74 3d 3d 34 20 20 20 20 56 65 72 73 69 6f  mat==4    Versio
1cd58 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44 45 53  n 3.3.0.  // DES
1cd59 43 20 69 6e 64 69 63 65 73 2e 20 20 42 6f 6f 6c  C indices.  Bool
1cd5a 65 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20  ean constants.  
1cd5b 2a 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d  */.  pDb->pSchem
1cd5c 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d  a->file_format =
1cd5d 20 6d 65 74 61 5b 31 5d 3b 0a 20 20 69 66 28 20   meta[1];.  if( 
1cd5e 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
1cd5f 6c 65 5f 66 6f 72 6d 61 74 3d 3d 30 20 29 7b 0a  le_format==0 ){.
1cd60 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
1cd61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20  ->file_format = 
1cd62 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 62  1;.  }.  if( pDb
1cd63 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
1cd64 66 6f 72 6d 61 74 3e 53 51 4c 49 54 45 5f 4d 41  format>SQLITE_MA
1cd65 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b  X_FILE_FORMAT ){
1cd66 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1cd67 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 63 75 72  eCloseCursor(cur
1cd68 4d 61 69 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  Main);.    sqlit
1cd69 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72  e3SetString(pzEr
1cd6a 72 4d 73 67 2c 20 22 75 6e 73 75 70 70 6f 72 74  rMsg, "unsupport
1cd6b 65 64 20 66 69 6c 65 20 66 6f 72 6d 61 74 22 2c  ed file format",
1cd6c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20 20   (char*)0);.    
1cd6d 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1cd6e 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20  e(pDb->pBt);.   
1cd6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1cd70 52 52 4f 52 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  RROR;.  }...  /*
1cd71 20 52 65 61 64 20 74 68 65 20 73 63 68 65 6d 61   Read the schema
1cd72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74   information out
1cd73 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 74   of the schema t
1cd74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 61 73 73  ables.  */.  ass
1cd75 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75  ert( db->init.bu
1cd76 73 79 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  sy );.  if( rc==
1cd77 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a  SQLITE_EMPTY ){.
1cd78 20 20 20 20 2f 2a 20 46 6f 72 20 61 6e 20 65 6d      /* For an em
1cd79 70 74 79 20 64 61 74 61 62 61 73 65 2c 20 74 68  pty database, th
1cd7a 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74  ere is nothing t
1cd7b 6f 20 72 65 61 64 20 2a 2f 0a 20 20 20 20 72 63  o read */.    rc
1cd7c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1cd7d 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20  }else{.    char 
1cd7e 2a 7a 53 71 6c 3b 0a 20 20 20 20 7a 53 71 6c 20  *zSql;.    zSql 
1cd7f 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
1cd80 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 22 53  (db, .        "S
1cd81 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74  ELECT name, root
1cd82 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27  page, sql FROM '
1cd83 25 71 27 2e 25 73 22 2c 0a 20 20 20 20 20 20 20  %q'.%s",.       
1cd84 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
1cd85 61 6d 65 2c 20 7a 4d 61 73 74 65 72 4e 61 6d 65  ame, zMasterName
1cd86 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 61  );.    sqlite3Sa
1cd87 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
1cd88 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78   rc = sqlite3_ex
1cd89 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c  ec(db, zSql, sql
1cd8a 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b  ite3InitCallback
1cd8b 2c 20 26 69 6e 69 74 44 61 74 61 2c 20 30 29 3b  , &initData, 0);
1cd8c 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1cd8d 49 54 45 5f 41 42 4f 52 54 20 29 20 72 63 20 3d  ITE_ABORT ) rc =
1cd8e 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20   initData.rc;.  
1cd8f 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f    sqlite3SafetyO
1cd90 6e 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74  n(db);.    sqlit
1cd91 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 23  e3_free(zSql);.#
1cd92 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1cd93 49 54 5f 41 4e 41 4c 59 5a 45 0a 20 20 20 20 69  IT_ANALYZE.    i
1cd94 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1cd95 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1cd96 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62  3AnalysisLoad(db
1cd97 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 0a 23 65  , iDb);.    }.#e
1cd98 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33  ndif.    sqlite3
1cd99 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72  BtreeCloseCursor
1cd9a 28 63 75 72 4d 61 69 6e 29 3b 0a 20 20 7d 0a 20  (curMain);.  }. 
1cd9b 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1cd9c 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20  ailed ){.    /* 
1cd9d 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1cd9e 28 70 7a 45 72 72 4d 73 67 2c 20 22 6f 75 74 20  (pzErrMsg, "out 
1cd9f 6f 66 20 6d 65 6d 6f 72 79 22 2c 20 28 63 68 61  of memory", (cha
1cda0 72 2a 29 30 29 3b 20 2a 2f 0a 20 20 20 20 72 63  r*)0); */.    rc
1cda1 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1cda2 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65  .    sqlite3Rese
1cda3 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
1cda4 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  db, 0);.  }.  if
1cda5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1cda6 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51  || (db->flags&SQ
1cda7 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64  LITE_RecoveryMod
1cda8 65 29 29 7b 0a 20 20 20 20 2f 2a 20 42 6c 61 63  e)){.    /* Blac
1cda9 6b 20 6d 61 67 69 63 3a 20 49 66 20 74 68 65 20  k magic: If the 
1cdaa 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
1cdab 6f 64 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c  ode flag is set,
1cdac 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72 0a 20   then consider. 
1cdad 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61     ** the schema
1cdae 20 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20 69 66   loaded, even if
1cdaf 20 65 72 72 6f 72 73 20 6f 63 63 75 72 65 64 2e   errors occured.
1cdb0 20 49 6e 20 74 68 69 73 20 73 69 74 75 61 74 69   In this situati
1cdb1 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  on the .    ** c
1cdb2 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 70  urrent sqlite3_p
1cdb3 72 65 70 61 72 65 28 29 20 6f 70 65 72 61 74 69  repare() operati
1cdb4 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c 20 62 75  on will fail, bu
1cdb5 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  t the following 
1cdb6 6f 6e 65 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20  one.    ** will 
1cdb7 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 70 69  attempt to compi
1cdb8 6c 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  le the supplied 
1cdb9 73 74 61 74 65 6d 65 6e 74 20 61 67 61 69 6e 73  statement agains
1cdba 74 20 77 68 61 74 65 76 65 72 20 73 75 62 73 65  t whatever subse
1cdbb 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  t.    ** of the 
1cdbc 73 63 68 65 6d 61 20 77 61 73 20 6c 6f 61 64 65  schema was loade
1cdbd 64 20 62 65 66 6f 72 65 20 74 68 65 20 65 72 72  d before the err
1cdbe 6f 72 20 6f 63 63 75 72 65 64 2e 20 54 68 65 20  or occured. The 
1cdbf 70 72 69 6d 61 72 79 0a 20 20 20 20 2a 2a 20 70  primary.    ** p
1cdc0 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 69  urpose of this i
1cdc1 73 20 74 6f 20 61 6c 6c 6f 77 20 61 63 63 65 73  s to allow acces
1cdc2 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f  s to the sqlite_
1cdc3 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 20  master table.   
1cdc4 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 69 74   ** even when it
1cdc5 27 73 20 63 6f 6e 74 65 6e 74 73 20 68 61 76 65  's contents have
1cdc6 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 2e   been corrupted.
1cdc7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 44 62 53 65  .    */.    DbSe
1cdc8 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44  tProperty(db, iD
1cdc9 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  b, DB_SchemaLoad
1cdca 65 64 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  ed);.    rc = SQ
1cdcb 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 73  LITE_OK;.  }.  s
1cdcc 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1cdcd 28 70 44 62 2d 3e 70 42 74 29 3b 0a 0a 65 72 72  (pDb->pBt);..err
1cdce 6f 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63  or_out:.  if( rc
1cdcf 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
1cdd0 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
1cdd1 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
1cdd2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1cdd3 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
1cdd4 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69   all database fi
1cdd5 6c 65 73 20 2d 20 74 68 65 20 6d 61 69 6e 20 64  les - the main d
1cdd6 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
1cdd7 65 20 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20 74  e file.** used t
1cdd8 6f 20 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72  o store temporar
1cdd9 79 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 61 6e  y tables, and an
1cdda 79 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 61 74  y additional dat
1cddb 61 62 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 63  abase files.** c
1cddc 72 65 61 74 65 64 20 75 73 69 6e 67 20 41 54 54  reated using ATT
1cddd 41 43 48 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  ACH statements. 
1cdde 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73   Return a succes
1cddf 73 20 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a 2a  s code.  If an.*
1cde0 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  * error occurs, 
1cde1 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d  write an error m
1cde2 65 73 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a 45  essage into *pzE
1cde3 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74  rrMsg..**.** Aft
1cde4 65 72 20 61 20 64 61 74 61 62 61 73 65 20 69 73  er a database is
1cde5 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68   initialized, th
1cde6 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  e DB_SchemaLoade
1cde7 64 20 62 69 74 20 69 73 20 73 65 74 0a 2a 2a 20  d bit is set.** 
1cde8 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68  bit is set in th
1cde9 65 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66  e flags field of
1cdea 20 74 68 65 20 44 62 20 73 74 72 75 63 74 75 72   the Db structur
1cdeb 65 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61  e. If the databa
1cdec 73 65 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20 6f  se.** file was o
1cded 66 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20 74  f zero-length, t
1cdee 68 65 6e 20 74 68 65 20 44 42 5f 45 6d 70 74 79  hen the DB_Empty
1cdef 20 66 6c 61 67 20 69 73 20 61 6c 73 6f 20 73 65   flag is also se
1cdf0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1cdf1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1cdf2 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Init(sqlite3 *db
1cdf3 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73  , char **pzErrMs
1cdf4 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 3b  g){.  int i, rc;
1cdf5 0a 20 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69 6e  .  int commit_in
1cdf6 74 65 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e 66  ternal = !(db->f
1cdf7 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65  lags&SQLITE_Inte
1cdf8 72 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a 20  rnChanges);.  . 
1cdf9 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1cdfa 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
1cdfb 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
1cdfc 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20  db->init.busy ) 
1cdfd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1cdfe 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  ;.  rc = SQLITE_
1cdff 4f 4b 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62  OK;.  db->init.b
1ce00 75 73 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69  usy = 1;.  for(i
1ce01 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
1ce02 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  K && i<db->nDb; 
1ce03 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 44 62  i++){.    if( Db
1ce04 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20  HasProperty(db, 
1ce05 69 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  i, DB_SchemaLoad
1ce06 65 64 29 20 7c 7c 20 69 3d 3d 31 20 29 20 63 6f  ed) || i==1 ) co
1ce07 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d  ntinue;.    rc =
1ce08 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28   sqlite3InitOne(
1ce09 64 62 2c 20 69 2c 20 70 7a 45 72 72 4d 73 67 29  db, i, pzErrMsg)
1ce0a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
1ce0b 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
1ce0c 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
1ce0d 28 64 62 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  (db, i);.    }. 
1ce0e 20 7d 0a 0a 20 20 2f 2a 20 4f 6e 63 65 20 61 6c   }..  /* Once al
1ce0f 6c 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61  l the other data
1ce10 62 61 73 65 73 20 68 61 76 65 20 62 65 65 6e 20  bases have been 
1ce11 69 6e 69 74 69 61 6c 69 73 65 64 2c 20 6c 6f 61  initialised, loa
1ce12 64 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a  d the schema.  *
1ce13 2a 20 66 6f 72 20 74 68 65 20 54 45 4d 50 20 64  * for the TEMP d
1ce14 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 69 73  atabase. This is
1ce15 20 6c 6f 61 64 65 64 20 6c 61 73 74 2c 20 61 73   loaded last, as
1ce16 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
1ce17 73 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6d  se.  ** schema m
1ce18 61 79 20 63 6f 6e 74 61 69 6e 20 72 65 66 65 72  ay contain refer
1ce19 65 6e 63 65 73 20 74 6f 20 6f 62 6a 65 63 74 73  ences to objects
1ce1a 20 69 6e 20 6f 74 68 65 72 20 64 61 74 61 62 61   in other databa
1ce1b 73 65 73 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  ses..  */.#ifnde
1ce1c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
1ce1d 4d 50 44 42 0a 20 20 69 66 28 20 72 63 3d 3d 53  MPDB.  if( rc==S
1ce1e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e  QLITE_OK && db->
1ce1f 6e 44 62 3e 31 20 26 26 20 21 44 62 48 61 73 50  nDb>1 && !DbHasP
1ce20 72 6f 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44  roperty(db, 1, D
1ce21 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20  B_SchemaLoaded) 
1ce22 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1ce23 74 65 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 31  te3InitOne(db, 1
1ce24 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  , pzErrMsg);.   
1ce25 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
1ce26 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
1ce27 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
1ce28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  1);.    }.  }.#e
1ce29 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74  ndif..  db->init
1ce2a 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20 69 66 28  .busy = 0;.  if(
1ce2b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1ce2c 26 20 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61  & commit_interna
1ce2d 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
1ce2e 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68  CommitInternalCh
1ce2f 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 7d 0a 0a  anges(db);.  }..
1ce30 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a    return rc; .}.
1ce31 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1ce32 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  ine is a no-op i
1ce33 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  f the database s
1ce34 63 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79  chema is already
1ce35 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a 2a 2a   initialised..**
1ce36 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
1ce37 73 63 68 65 6d 61 20 69 73 20 6c 6f 61 64 65 64  schema is loaded
1ce38 2e 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  . An error code 
1ce39 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
1ce3a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1ce3b 6e 74 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  nt sqlite3ReadSc
1ce3c 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72  hema(Parse *pPar
1ce3d 73 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  se){.  int rc = 
1ce3e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c  SQLITE_OK;.  sql
1ce3f 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1ce40 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  e->db;.  assert(
1ce41 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1ce42 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
1ce43 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69  ;.  if( !db->ini
1ce44 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 72 63  t.busy ){.    rc
1ce45 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64   = sqlite3Init(d
1ce46 62 2c 20 26 70 50 61 72 73 65 2d 3e 7a 45 72 72  b, &pParse->zErr
1ce47 4d 73 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Msg);.  }.  if( 
1ce48 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1ce49 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
1ce4a 3d 20 72 63 3b 0a 20 20 20 20 70 50 61 72 73 65  = rc;.    pParse
1ce4b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20  ->nErr++;.  }.  
1ce4c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
1ce4d 2a 0a 2a 2a 20 43 68 65 63 6b 20 73 63 68 65 6d  *.** Check schem
1ce4e 61 20 63 6f 6f 6b 69 65 73 20 69 6e 20 61 6c 6c  a cookies in all
1ce4f 20 64 61 74 61 62 61 73 65 73 2e 20 20 49 66 20   databases.  If 
1ce50 61 6e 79 20 63 6f 6f 6b 69 65 20 69 73 20 6f 75  any cookie is ou
1ce51 74 0a 2a 2a 20 6f 66 20 64 61 74 65 2c 20 72 65  t.** of date, re
1ce52 74 75 72 6e 20 30 2e 20 20 49 66 20 61 6c 6c 20  turn 0.  If all 
1ce53 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 61  schema cookies a
1ce54 72 65 20 63 75 72 72 65 6e 74 2c 20 72 65 74 75  re current, retu
1ce55 72 6e 20 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rn 1..*/.static 
1ce56 69 6e 74 20 73 63 68 65 6d 61 49 73 56 61 6c 69  int schemaIsVali
1ce57 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  d(sqlite3 *db){.
1ce58 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74    int iDb;.  int
1ce59 20 72 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20   rc;.  BtCursor 
1ce5a 2a 63 75 72 54 65 6d 70 3b 0a 20 20 69 6e 74 20  *curTemp;.  int 
1ce5b 63 6f 6f 6b 69 65 3b 0a 20 20 69 6e 74 20 61 6c  cookie;.  int al
1ce5c 6c 4f 6b 20 3d 20 31 3b 0a 0a 20 20 61 73 73 65  lOk = 1;..  asse
1ce5d 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1ce5e 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
1ce5f 29 20 29 3b 0a 20 20 66 6f 72 28 69 44 62 3d 30  ) );.  for(iDb=0
1ce60 3b 20 61 6c 6c 4f 6b 20 26 26 20 69 44 62 3c 64  ; allOk && iDb<d
1ce61 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a  b->nDb; iDb++){.
1ce62 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a      Btree *pBt;.
1ce63 20 20 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44      pBt = db->aD
1ce64 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20  b[iDb].pBt;.    
1ce65 69 66 28 20 70 42 74 3d 3d 30 20 29 20 63 6f 6e  if( pBt==0 ) con
1ce66 74 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d 20  tinue;.    rc = 
1ce67 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1ce68 6f 72 28 70 42 74 2c 20 4d 41 53 54 45 52 5f 52  or(pBt, MASTER_R
1ce69 4f 4f 54 2c 20 30 2c 20 30 2c 20 30 2c 20 26 63  OOT, 0, 0, 0, &c
1ce6a 75 72 54 65 6d 70 29 3b 0a 20 20 20 20 69 66 28  urTemp);.    if(
1ce6b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1ce6c 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1ce6d 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
1ce6e 28 70 42 74 2c 20 31 2c 20 28 75 33 32 20 2a 29  (pBt, 1, (u32 *)
1ce6f 26 63 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20  &cookie);.      
1ce70 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1ce71 4b 20 26 26 20 63 6f 6f 6b 69 65 21 3d 64 62 2d  K && cookie!=db-
1ce72 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
1ce73 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  a->schema_cookie
1ce74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 6c 6c 4f   ){.        allO
1ce75 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  k = 0;.      }. 
1ce76 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1ce77 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 63 75 72  eCloseCursor(cur
1ce78 54 65 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Temp);.    }.   
1ce79 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1ce7a 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64  NOMEM ){.      d
1ce7b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1ce7c 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 1;.    }.  }. 
1ce7d 20 72 65 74 75 72 6e 20 61 6c 6c 4f 6b 3b 0a 7d   return allOk;.}
1ce7e 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
1ce7f 61 20 73 63 68 65 6d 61 20 70 6f 69 6e 74 65 72  a schema pointer
1ce80 20 69 6e 74 6f 20 74 68 65 20 69 44 62 20 69 6e   into the iDb in
1ce81 64 65 78 20 74 68 61 74 20 69 6e 64 69 63 61 74  dex that indicat
1ce82 65 73 0a 2a 2a 20 77 68 69 63 68 20 64 61 74 61  es.** which data
1ce83 62 61 73 65 20 66 69 6c 65 20 69 6e 20 64 62 2d  base file in db-
1ce84 3e 61 44 62 5b 5d 20 74 68 65 20 73 63 68 65 6d  >aDb[] the schem
1ce85 61 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a  a refers to..**.
1ce86 2a 2a 20 49 66 20 74 68 65 20 73 61 6d 65 20 64  ** If the same d
1ce87 61 74 61 62 61 73 65 20 69 73 20 61 74 74 61 63  atabase is attac
1ce88 68 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  hed more than on
1ce89 63 65 2c 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  ce, the first.**
1ce8a 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
1ce8b 73 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  se is returned..
1ce8c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1ce8d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68  E int sqlite3Sch
1ce8e 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74  emaToIndex(sqlit
1ce8f 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a  e3 *db, Schema *
1ce90 70 53 63 68 65 6d 61 29 7b 0a 20 20 69 6e 74 20  pSchema){.  int 
1ce91 69 20 3d 20 2d 31 30 30 30 30 30 30 3b 0a 0a 20  i = -1000000;.. 
1ce92 20 2f 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69   /* If pSchema i
1ce93 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 74  s NULL, then ret
1ce94 75 72 6e 20 2d 31 30 30 30 30 30 30 2e 20 54 68  urn -1000000. Th
1ce95 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  is happens when 
1ce96 63 6f 64 65 20 69 6e 20 0a 20 20 2a 2a 20 65 78  code in .  ** ex
1ce97 70 72 2e 63 20 69 73 20 74 72 79 69 6e 67 20 74  pr.c is trying t
1ce98 6f 20 72 65 73 6f 6c 76 65 20 61 20 72 65 66 65  o resolve a refe
1ce99 72 65 6e 63 65 20 74 6f 20 61 20 74 72 61 6e 73  rence to a trans
1ce9a 69 65 6e 74 20 74 61 62 6c 65 20 28 69 2e 65 2e  ient table (i.e.
1ce9b 20 6f 6e 65 0a 20 20 2a 2a 20 63 72 65 61 74 65   one.  ** create
1ce9c 64 20 62 79 20 61 20 73 75 62 2d 73 65 6c 65 63  d by a sub-selec
1ce9d 74 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  t). In this case
1ce9e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
1ce9f 65 20 6f 66 20 74 68 69 73 20 0a 20 20 2a 2a 20  e of this .  ** 
1cea0 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
1cea1 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 0a 20  never be used.. 
1cea2 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 72 65 74 75   **.  ** We retu
1cea3 72 6e 20 2d 31 30 30 30 30 30 30 20 69 6e 73 74  rn -1000000 inst
1cea4 65 61 64 20 6f 66 20 74 68 65 20 6d 6f 72 65 20  ead of the more 
1cea5 75 73 75 61 6c 20 2d 31 20 73 69 6d 70 6c 79 20  usual -1 simply 
1cea6 62 65 63 61 75 73 65 20 75 73 69 6e 67 0a 20 20  because using.  
1cea7 2a 2a 20 2d 31 30 30 30 30 30 30 20 61 73 20 69  ** -1000000 as i
1cea8 6e 63 6f 72 72 65 63 74 6c 79 20 75 73 69 6e 67  ncorrectly using
1cea9 20 2d 31 30 30 30 30 30 30 20 69 6e 64 65 78 20   -1000000 index 
1ceaa 69 6e 74 6f 20 64 62 2d 3e 61 44 62 5b 5d 20 69  into db->aDb[] i
1ceab 73 20 6d 75 63 68 20 0a 20 20 2a 2a 20 6d 6f 72  s much .  ** mor
1ceac 65 20 6c 69 6b 65 6c 79 20 74 6f 20 63 61 75 73  e likely to caus
1cead 65 20 61 20 73 65 67 66 61 75 6c 74 20 74 68 61  e a segfault tha
1ceae 6e 20 2d 31 20 28 6f 66 20 63 6f 75 72 73 65 20  n -1 (of course 
1ceaf 74 68 65 72 65 20 61 72 65 20 61 73 73 65 72 74  there are assert
1ceb0 28 29 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  ().  ** statemen
1ceb1 74 73 20 74 6f 6f 2c 20 62 75 74 20 69 74 20 6e  ts too, but it n
1ceb2 65 76 65 72 20 68 75 72 74 73 20 74 6f 20 70 6c  ever hurts to pl
1ceb3 61 79 20 74 68 65 20 6f 64 64 73 29 2e 0a 20 20  ay the odds)..  
1ceb4 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  */.  assert( sql
1ceb5 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1ceb6 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1ceb7 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a 20  if( pSchema ){. 
1ceb8 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
1ceb9 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
1ceba 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69     if( db->aDb[i
1cebb 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68 65  ].pSchema==pSche
1cebc 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72  ma ){.        br
1cebd 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1cebe 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69   }.    assert( i
1cebf 3e 3d 30 20 26 26 69 3e 3d 30 20 26 26 20 20 69  >=0 &&i>=0 &&  i
1cec0 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7d 0a  <db->nDb );.  }.
1cec1 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f    return i;.}../
1cec2 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65  *.** Compile the
1cec3 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
1cec4 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71  QL statement zSq
1cec5 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65  l into a stateme
1cec6 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  nt handle..*/.SQ
1cec7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1cec8 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 28   sqlite3Prepare(
1cec9 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1ceca 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cecb 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
1cecc 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1cecd 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
1cece 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  /* UTF-8 encoded
1cecf 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
1ced0 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c  */.  int nBytes,
1ced1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ced2 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  * Length of zSql
1ced3 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
1ced4 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  int saveSqlFlag,
1ced5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1ced6 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74 65  e to copy SQL te
1ced7 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  xt into the sqli
1ced8 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71  te3_stmt */.  sq
1ced9 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
1ceda 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
1cedb 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
1cedc 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1cedd 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ent */.  const c
1cede 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
1cedf 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
1cee0 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
1cee1 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 73 50  */.){.  Parse sP
1cee2 61 72 73 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45  arse;.  char *zE
1cee3 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 6e 74  rrMsg = 0;.  int
1cee4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1cee5 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73  .  int i;..  ass
1cee6 65 72 74 28 20 70 70 53 74 6d 74 20 29 3b 0a 20  ert( ppStmt );. 
1cee7 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20   *ppStmt = 0;.  
1cee8 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1cee9 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 72  yOn(db) ){.    r
1ceea 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1ceeb 55 53 45 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  USE;.  }.  asser
1ceec 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
1ceed 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74  iled );.  assert
1ceee 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1ceef 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
1cef0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20  );..  /* If any 
1cef1 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1cef2 65 20 73 63 68 65 6d 61 73 20 61 72 65 20 6c 6f  e schemas are lo
1cef3 63 6b 65 64 2c 20 64 6f 20 6e 6f 74 20 70 72 6f  cked, do not pro
1cef4 63 65 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 63  ceed with.  ** c
1cef5 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20 49 6e 73 74  ompilation. Inst
1cef6 65 61 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ead return SQLIT
1cef7 45 5f 4c 4f 43 4b 45 44 20 69 6d 6d 65 64 69 61  E_LOCKED immedia
1cef8 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  tely..  */.  for
1cef9 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
1cefa 20 69 2b 2b 29 20 7b 0a 20 20 20 20 42 74 72 65   i++) {.    Btre
1cefb 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
1cefc 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
1cefd 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 69 6e   pBt ){.      in
1cefe 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d  t rc;.      rc =
1ceff 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
1cf00 65 6d 61 4c 6f 63 6b 65 64 28 70 42 74 29 3b 0a  emaLocked(pBt);.
1cf01 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
1cf02 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
1cf03 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
1cf04 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  b[i].zName;.    
1cf05 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1cf06 28 64 62 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  (db, SQLITE_LOCK
1cf07 45 44 2c 20 22 64 61 74 61 62 61 73 65 20 73 63  ED, "database sc
1cf08 68 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a 20  hema is locked: 
1cf09 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20  %s", zDb);.     
1cf0a 20 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79     sqlite3Safety
1cf0b 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 20  Off(db);.       
1cf0c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
1cf0d 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20  OCKED;.      }. 
1cf0e 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 6d 65     }.  }.  .  me
1cf0f 6d 73 65 74 28 26 73 50 61 72 73 65 2c 20 30 2c  mset(&sParse, 0,
1cf10 20 73 69 7a 65 6f 66 28 73 50 61 72 73 65 29 29   sizeof(sParse))
1cf11 3b 0a 20 20 73 50 61 72 73 65 2e 64 62 20 3d 20  ;.  sParse.db = 
1cf12 64 62 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  db;.  if( nBytes
1cf13 3e 3d 30 20 26 26 20 7a 53 71 6c 5b 6e 42 79 74  >=0 && zSql[nByt
1cf14 65 73 5d 21 3d 30 20 29 7b 0a 20 20 20 20 63 68  es]!=0 ){.    ch
1cf15 61 72 20 2a 7a 53 71 6c 43 6f 70 79 3b 0a 20 20  ar *zSqlCopy;.  
1cf16 20 20 69 66 28 20 6e 42 79 74 65 73 3e 53 51 4c    if( nBytes>SQL
1cf17 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1cf18 54 48 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  TH ){.      retu
1cf19 72 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  rn SQLITE_TOOBIG
1cf1a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71 6c  ;.    }.    zSql
1cf1b 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 44 62  Copy = sqlite3Db
1cf1c 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 53 71 6c  StrNDup(db, zSql
1cf1d 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69  , nBytes);.    i
1cf1e 66 28 20 7a 53 71 6c 43 6f 70 79 20 29 7b 0a 20  f( zSqlCopy ){. 
1cf1f 20 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50       sqlite3RunP
1cf20 61 72 73 65 72 28 26 73 50 61 72 73 65 2c 20 7a  arser(&sParse, z
1cf21 53 71 6c 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73  SqlCopy, &zErrMs
1cf22 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
1cf23 33 5f 66 72 65 65 28 7a 53 71 6c 43 6f 70 79 29  3_free(zSqlCopy)
1cf24 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 50 61 72  ;.    }.    sPar
1cf25 73 65 2e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  se.zTail = &zSql
1cf26 5b 6e 42 79 74 65 73 5d 3b 0a 20 20 7d 65 6c 73  [nBytes];.  }els
1cf27 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 75  e{.    sqlite3Ru
1cf28 6e 50 61 72 73 65 72 28 26 73 50 61 72 73 65 2c  nParser(&sParse,
1cf29 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29   zSql, &zErrMsg)
1cf2a 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d  ;.  }..  if( db-
1cf2b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
1cf2c 0a 20 20 20 20 73 50 61 72 73 65 2e 72 63 20 3d  .    sParse.rc =
1cf2d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1cf2e 20 7d 0a 20 20 69 66 28 20 73 50 61 72 73 65 2e   }.  if( sParse.
1cf2f 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
1cf30 29 20 73 50 61 72 73 65 2e 72 63 20 3d 20 53 51  ) sParse.rc = SQ
1cf31 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73  LITE_OK;.  if( s
1cf32 50 61 72 73 65 2e 63 68 65 63 6b 53 63 68 65 6d  Parse.checkSchem
1cf33 61 20 26 26 20 21 73 63 68 65 6d 61 49 73 56 61  a && !schemaIsVa
1cf34 6c 69 64 28 64 62 29 20 29 7b 0a 20 20 20 20 73  lid(db) ){.    s
1cf35 50 61 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54  Parse.rc = SQLIT
1cf36 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20  E_SCHEMA;.  }.  
1cf37 69 66 28 20 73 50 61 72 73 65 2e 72 63 3d 3d 53  if( sParse.rc==S
1cf38 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a  QLITE_SCHEMA ){.
1cf39 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
1cf3a 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
1cf3b 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  b, 0);.  }.  if(
1cf3c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1cf3d 64 20 29 7b 0a 20 20 20 20 73 50 61 72 73 65 2e  d ){.    sParse.
1cf3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1cf3f 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 7a 54  M;.  }.  if( pzT
1cf40 61 69 6c 20 29 7b 0a 20 20 20 20 2a 70 7a 54 61  ail ){.    *pzTa
1cf41 69 6c 20 3d 20 73 50 61 72 73 65 2e 7a 54 61 69  il = sParse.zTai
1cf42 6c 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 50  l;.  }.  rc = sP
1cf43 61 72 73 65 2e 72 63 3b 0a 0a 23 69 66 6e 64 65  arse.rc;..#ifnde
1cf44 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  f SQLITE_OMIT_EX
1cf45 50 4c 41 49 4e 0a 20 20 69 66 28 20 72 63 3d 3d  PLAIN.  if( rc==
1cf46 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 50 61  SQLITE_OK && sPa
1cf47 72 73 65 2e 70 56 64 62 65 20 26 26 20 73 50 61  rse.pVdbe && sPa
1cf48 72 73 65 2e 65 78 70 6c 61 69 6e 20 29 7b 0a 20  rse.explain ){. 
1cf49 20 20 20 69 66 28 20 73 50 61 72 73 65 2e 65 78     if( sParse.ex
1cf4a 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20  plain==2 ){.    
1cf4b 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1cf4c 4e 75 6d 43 6f 6c 73 28 73 50 61 72 73 65 2e 70  NumCols(sParse.p
1cf4d 56 64 62 65 2c 20 33 29 3b 0a 20 20 20 20 20 20  Vdbe, 3);.      
1cf4e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1cf4f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64  lName(sParse.pVd
1cf50 62 65 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  be, 0, COLNAME_N
1cf51 41 4d 45 2c 20 22 6f 72 64 65 72 22 2c 20 50 33  AME, "order", P3
1cf52 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
1cf53 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1cf54 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64  lName(sParse.pVd
1cf55 62 65 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  be, 1, COLNAME_N
1cf56 41 4d 45 2c 20 22 66 72 6f 6d 22 2c 20 50 33 5f  AME, "from", P3_
1cf57 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
1cf58 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
1cf59 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62  Name(sParse.pVdb
1cf5a 65 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  e, 2, COLNAME_NA
1cf5b 4d 45 2c 20 22 64 65 74 61 69 6c 22 2c 20 50 33  ME, "detail", P3
1cf5c 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 65  _STATIC);.    }e
1cf5d 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
1cf5e 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1cf5f 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 35  (sParse.pVdbe, 5
1cf60 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1cf61 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73  VdbeSetColName(s
1cf62 50 61 72 73 65 2e 70 56 64 62 65 2c 20 30 2c 20  Parse.pVdbe, 0, 
1cf63 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 61  COLNAME_NAME, "a
1cf64 64 64 72 22 2c 20 50 33 5f 53 54 41 54 49 43 29  ddr", P3_STATIC)
1cf65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1cf66 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50  dbeSetColName(sP
1cf67 61 72 73 65 2e 70 56 64 62 65 2c 20 31 2c 20 43  arse.pVdbe, 1, C
1cf68 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 70  OLNAME_NAME, "op
1cf69 63 6f 64 65 22 2c 20 50 33 5f 53 54 41 54 49 43  code", P3_STATIC
1cf6a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1cf6b 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73  VdbeSetColName(s
1cf6c 50 61 72 73 65 2e 70 56 64 62 65 2c 20 32 2c 20  Parse.pVdbe, 2, 
1cf6d 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70  COLNAME_NAME, "p
1cf6e 31 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a  1", P3_STATIC);.
1cf6f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1cf70 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
1cf71 73 65 2e 70 56 64 62 65 2c 20 33 2c 20 43 4f 4c  se.pVdbe, 3, COL
1cf72 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 32 22 2c  NAME_NAME, "p2",
1cf73 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P3_STATIC);.   
1cf74 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1cf75 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e  tColName(sParse.
1cf76 70 56 64 62 65 2c 20 34 2c 20 43 4f 4c 4e 41 4d  pVdbe, 4, COLNAM
1cf77 45 5f 4e 41 4d 45 2c 20 22 70 33 22 2c 20 50 33  E_NAME, "p3", P3
1cf78 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a  _STATIC);.    }.
1cf79 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
1cf7a 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1cf7b 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 63  ff(db) ){.    rc
1cf7c 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45   = SQLITE_MISUSE
1cf7d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 61 76  ;.  }..  if( sav
1cf7e 65 53 71 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20  eSqlFlag ){.    
1cf7f 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71  sqlite3VdbeSetSq
1cf80 6c 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20  l(sParse.pVdbe, 
1cf81 7a 53 71 6c 2c 20 73 50 61 72 73 65 2e 7a 54 61  zSql, sParse.zTa
1cf82 69 6c 20 2d 20 7a 53 71 6c 29 3b 0a 20 20 7d 0a  il - zSql);.  }.
1cf83 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1cf84 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  _OK || db->mallo
1cf85 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73  cFailed ){.    s
1cf86 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1cf87 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 73  (sqlite3_stmt*)s
1cf88 50 61 72 73 65 2e 70 56 64 62 65 29 3b 0a 20 20  Parse.pVdbe);.  
1cf89 20 20 61 73 73 65 72 74 28 21 28 2a 70 70 53 74    assert(!(*ppSt
1cf8a 6d 74 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  mt));.  }else{. 
1cf8b 20 20 20 2a 70 70 53 74 6d 74 20 3d 20 28 73 71     *ppStmt = (sq
1cf8c 6c 69 74 65 33 5f 73 74 6d 74 2a 29 73 50 61 72  lite3_stmt*)sPar
1cf8d 73 65 2e 70 56 64 62 65 3b 0a 20 20 7d 0a 0a 20  se.pVdbe;.  }.. 
1cf8e 20 69 66 28 20 7a 45 72 72 4d 73 67 20 29 7b 0a   if( zErrMsg ){.
1cf8f 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1cf90 28 64 62 2c 20 72 63 2c 20 22 25 73 22 2c 20 7a  (db, rc, "%s", z
1cf91 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c  ErrMsg);.    sql
1cf92 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d 73  ite3_free(zErrMs
1cf93 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  g);.  }else{.   
1cf94 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
1cf95 2c 20 72 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  , rc, 0);.  }.. 
1cf96 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1cf97 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
1cf98 2f 2a 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  /* sqlite3Releas
1cf99 65 54 68 72 65 61 64 44 61 74 61 28 29 3b 20 2a  eThreadData(); *
1cf9a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26  /.  assert( (rc&
1cf9b 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63  db->errMask)==rc
1cf9c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
1cf9d 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  .}.static int sq
1cf9e 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
1cf9f 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  are(.  sqlite3 *
1cfa0 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
1cfa1 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
1cfa2 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  dle. */.  const 
1cfa3 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
1cfa4 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63      /* UTF-8 enc
1cfa5 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
1cfa6 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  nt. */.  int nBy
1cfa7 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
1cfa8 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
1cfa9 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
1cfaa 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71 6c 46  /.  int saveSqlF
1cfab 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  lag,          /*
1cfac 20 54 72 75 65 20 74 6f 20 63 6f 70 79 20 53 51   True to copy SQ
1cfad 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68 65 20  L text into the 
1cfae 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a  sqlite3_stmt */.
1cfaf 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1cfb0 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f  *ppStmt,    /* O
1cfb1 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  UT: A pointer to
1cfb2 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
1cfb3 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e  atement */.  con
1cfb4 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  st char **pzTail
1cfb5 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
1cfb6 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72  nd of parsed str
1cfb7 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ing */.){.  int 
1cfb8 72 63 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rc;.  if( sqlite
1cfb9 33 53 61 66 65 74 79 43 68 65 63 6b 28 64 62 29  3SafetyCheck(db)
1cfba 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1cfbb 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
1cfbc 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
1cfbd 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
1cfbe 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  x);.  sqlite3Btr
1cfbf 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a  eeEnterAll(db);.
1cfc0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72    rc = sqlite3Pr
1cfc1 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
1cfc2 6e 42 79 74 65 73 2c 20 73 61 76 65 53 71 6c 46  nBytes, saveSqlF
1cfc3 6c 61 67 2c 20 70 70 53 74 6d 74 2c 20 70 7a 54  lag, ppStmt, pzT
1cfc4 61 69 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  ail);.  sqlite3B
1cfc5 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
1cfc6 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1cfc7 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
1cfc8 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
1cfc9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 72 75 6e 20  .}../*.** Rerun 
1cfca 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  the compilation 
1cfcb 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61  of a statement a
1cfcc 66 74 65 72 20 61 20 73 63 68 65 6d 61 20 63 68  fter a schema ch
1cfcd 61 6e 67 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  ange..** Return 
1cfce 74 72 75 65 20 69 66 20 74 68 65 20 73 74 61 74  true if the stat
1cfcf 65 6d 65 6e 74 20 77 61 73 20 72 65 63 6f 6d 70  ement was recomp
1cfd0 69 6c 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c  iled successfull
1cfd1 79 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 66 61 6c  y..** Return fal
1cfd2 73 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61  se if there is a
1cfd3 6e 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20  n error of some 
1cfd4 6b 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  kind..*/.SQLITE_
1cfd5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1cfd6 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64 62  te3Reprepare(Vdb
1cfd7 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
1cfd8 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
1cfd9 2a 70 4e 65 77 3b 0a 20 20 63 6f 6e 73 74 20 63  *pNew;.  const c
1cfda 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 73 71 6c  har *zSql;.  sql
1cfdb 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 61 73 73  ite3 *db;..  ass
1cfdc 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1cfdd 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 56  ex_held(sqlite3V
1cfde 64 62 65 44 62 28 70 29 2d 3e 6d 75 74 65 78 29  dbeDb(p)->mutex)
1cfdf 20 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c   );.  zSql = sql
1cfe0 69 74 65 33 56 64 62 65 47 65 74 53 71 6c 28 70  ite3VdbeGetSql(p
1cfe1 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30  );.  if( zSql==0
1cfe2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1cfe3 3b 0a 20 20 7d 0a 20 20 64 62 20 3d 20 73 71 6c  ;.  }.  db = sql
1cfe4 69 74 65 33 56 64 62 65 44 62 28 70 29 3b 0a 20  ite3VdbeDb(p);. 
1cfe5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1cfe6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
1cfe7 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
1cfe8 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
1cfe9 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c  repare(db, zSql,
1cfea 20 2d 31 2c 20 30 2c 20 26 70 4e 65 77 2c 20 30   -1, 0, &pNew, 0
1cfeb 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
1cfec 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 3d     assert( pNew=
1cfed 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  =0 );.    return
1cfee 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
1cfef 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30   assert( pNew!=0
1cff0 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   );.  }.  sqlite
1cff1 33 56 64 62 65 53 77 61 70 28 28 56 64 62 65 2a  3VdbeSwap((Vdbe*
1cff2 29 70 4e 65 77 2c 20 70 29 3b 0a 20 20 73 71 6c  )pNew, p);.  sql
1cff3 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
1cff4 6e 64 69 6e 67 73 28 70 4e 65 77 2c 20 28 73 71  ndings(pNew, (sq
1cff5 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 29 3b 0a  lite3_stmt*)p);.
1cff6 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
1cff7 65 74 53 74 65 70 52 65 73 75 6c 74 28 28 56 64  etStepResult((Vd
1cff8 62 65 2a 29 70 4e 65 77 29 3b 0a 20 20 73 71 6c  be*)pNew);.  sql
1cff9 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
1cffa 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b 0a 20  ((Vdbe*)pNew);. 
1cffb 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f   return 1;.}.../
1cffc 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e  *.** Two version
1cffd 73 20 6f 66 20 74 68 65 20 6f 66 66 69 63 69 61  s of the officia
1cffe 6c 20 41 50 49 2e 20 20 4c 65 67 61 63 79 20 61  l API.  Legacy a
1cfff 6e 64 20 6e 65 77 20 75 73 65 2e 20 20 49 6e 20  nd new use.  In 
1d000 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65  the legacy.** ve
1d001 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69  rsion, the origi
1d002 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20  nal SQL text is 
1d003 6e 6f 74 20 73 61 76 65 64 20 69 6e 20 74 68 65  not saved in the
1d004 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1d005 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66  ent.** and so if
1d006 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65   a schema change
1d007 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f   occurs, SQLITE_
1d008 53 43 48 45 4d 41 20 69 73 20 72 65 74 75 72 6e  SCHEMA is return
1d009 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
1d00a 5f 73 74 65 70 28 29 2e 20 20 49 6e 20 74 68 65  _step().  In the
1d00b 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20 74 68   new version, th
1d00c 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74  e original SQL t
1d00d 65 78 74 20 69 73 20 72 65 74 61 69 6e 65 64 0a  ext is retained.
1d00e 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74 65  ** and the state
1d00f 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 69  ment is automati
1d010 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64  cally recompiled
1d011 20 69 66 20 61 6e 20 73 63 68 65 6d 61 20 63 68   if an schema ch
1d012 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a  ange.** occurs..
1d013 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1d014 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
1d015 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
1d016 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1d017 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
1d018 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  e. */.  const ch
1d019 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
1d01a 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
1d01b 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
1d01c 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
1d01d 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
1d01e 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
1d01f 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
1d020 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1d021 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f  *ppStmt,    /* O
1d022 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  UT: A pointer to
1d023 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
1d024 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e  atement */.  con
1d025 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  st char **pzTail
1d026 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
1d027 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72  nd of parsed str
1d028 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75  ing */.){.  retu
1d029 72 6e 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e  rn sqlite3LockAn
1d02a 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53 71 6c  dPrepare(db,zSql
1d02b 2c 6e 42 79 74 65 73 2c 30 2c 70 70 53 74 6d 74  ,nBytes,0,ppStmt
1d02c 2c 70 7a 54 61 69 6c 29 3b 0a 7d 0a 53 51 4c 49  ,pzTail);.}.SQLI
1d02d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1d02e 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a 20  e3_prepare_v2(. 
1d02f 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
1d030 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
1d031 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a  tabase handle. *
1d032 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1d033 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
1d034 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
1d035 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
1d036 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
1d037 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d038 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
1d039 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
1d03a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
1d03b 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
1d03c 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
1d03d 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1d03e 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ent */.  const c
1d03f 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
1d040 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
1d041 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
1d042 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  */.){.  return s
1d043 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65  qlite3LockAndPre
1d044 70 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79  pare(db,zSql,nBy
1d045 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c 70 7a 54  tes,1,ppStmt,pzT
1d046 61 69 6c 29 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65  ail);.}...#ifnde
1d047 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
1d048 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c  F16./*.** Compil
1d049 65 20 74 68 65 20 55 54 46 2d 31 36 20 65 6e 63  e the UTF-16 enc
1d04a 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
1d04b 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20 61 20 73  nt zSql into a s
1d04c 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e  tatement handle.
1d04d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
1d04e 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28  qlite3Prepare16(
1d04f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1d050 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d051 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
1d052 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69   */ .  const voi
1d053 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  d *zSql,        
1d054 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
1d055 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
1d056 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
1d057 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1d058 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
1d059 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
1d05a 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67   int saveSqlFlag
1d05b 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ,          /* Tr
1d05c 75 65 20 74 6f 20 73 61 76 65 20 53 51 4c 20 74  ue to save SQL t
1d05d 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ext into the sql
1d05e 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73  ite3_stmt */.  s
1d05f 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
1d060 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
1d061 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
1d062 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
1d063 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
1d064 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20  void **pzTail   
1d065 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
1d066 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
1d067 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 54 68 69 73   */.){.  /* This
1d068 20 66 75 6e 63 74 69 6f 6e 20 63 75 72 72 65 6e   function curren
1d069 74 6c 79 20 77 6f 72 6b 73 20 62 79 20 66 69 72  tly works by fir
1d06a 73 74 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20  st transforming 
1d06b 74 68 65 20 55 54 46 2d 31 36 0a 20 20 2a 2a 20  the UTF-16.  ** 
1d06c 65 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 20 74  encoded string t
1d06d 6f 20 55 54 46 2d 38 2c 20 74 68 65 6e 20 69 6e  o UTF-8, then in
1d06e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 70  voking sqlite3_p
1d06f 72 65 70 61 72 65 28 29 2e 20 54 68 65 0a 20 20  repare(). The.  
1d070 2a 2a 20 74 72 69 63 6b 79 20 62 69 74 20 69 73  ** tricky bit is
1d071 20 66 69 67 75 72 69 6e 67 20 6f 75 74 20 74 68   figuring out th
1d072 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 72 65 74  e pointer to ret
1d073 75 72 6e 20 69 6e 20 2a 70 7a 54 61 69 6c 2e 0a  urn in *pzTail..
1d074 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71    */.  char *zSq
1d075 6c 38 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  l8;.  const char
1d076 20 2a 7a 54 61 69 6c 38 20 3d 20 30 3b 0a 20 20   *zTail8 = 0;.  
1d077 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1d078 4f 4b 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  OK;..  if( sqlit
1d079 65 33 53 61 66 65 74 79 43 68 65 63 6b 28 64 62  e3SafetyCheck(db
1d07a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
1d07b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1d07c 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
1d07d 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
1d07e 65 78 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73  ex);.  zSql8 = s
1d07f 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64  qlite3Utf16to8(d
1d080 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29  b, zSql, nBytes)
1d081 3b 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b  ;.  if( zSql8 ){
1d082 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1d083 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28  3LockAndPrepare(
1d084 64 62 2c 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73  db, zSql8, -1, s
1d085 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74  aveSqlFlag, ppSt
1d086 6d 74 2c 20 26 7a 54 61 69 6c 38 29 3b 0a 20 20  mt, &zTail8);.  
1d087 7d 0a 0a 20 20 69 66 28 20 7a 54 61 69 6c 38 20  }..  if( zTail8 
1d088 26 26 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20  && pzTail ){.   
1d089 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 70   /* If sqlite3_p
1d08a 72 65 70 61 72 65 20 72 65 74 75 72 6e 73 20 61  repare returns a
1d08b 20 74 61 69 6c 20 70 6f 69 6e 74 65 72 2c 20 77   tail pointer, w
1d08c 65 20 63 61 6c 63 75 6c 61 74 65 20 74 68 65 0a  e calculate the.
1d08d 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e      ** equivalen
1d08e 74 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74  t pointer into t
1d08f 68 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  he UTF-16 string
1d090 20 62 79 20 63 6f 75 6e 74 69 6e 67 20 74 68 65   by counting the
1d091 20 75 6e 69 63 6f 64 65 0a 20 20 20 20 2a 2a 20   unicode.    ** 
1d092 63 68 61 72 61 63 74 65 72 73 20 62 65 74 77 65  characters betwe
1d093 65 6e 20 7a 53 71 6c 38 20 61 6e 64 20 7a 54 61  en zSql8 and zTa
1d094 69 6c 38 2c 20 61 6e 64 20 74 68 65 6e 20 72 65  il8, and then re
1d095 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  turning a pointe
1d096 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d  r.    ** the sam
1d097 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  e number of char
1d098 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20  acters into the 
1d099 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 0a 20  UTF-16 string.. 
1d09a 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68     */.    int ch
1d09b 61 72 73 5f 70 61 72 73 65 64 20 3d 20 73 71 6c  ars_parsed = sql
1d09c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
1d09d 7a 53 71 6c 38 2c 20 7a 54 61 69 6c 38 2d 7a 53  zSql8, zTail8-zS
1d09e 71 6c 38 29 3b 0a 20 20 20 20 2a 70 7a 54 61 69  ql8);.    *pzTai
1d09f 6c 20 3d 20 28 75 38 20 2a 29 7a 53 71 6c 20 2b  l = (u8 *)zSql +
1d0a0 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74   sqlite3Utf16Byt
1d0a1 65 4c 65 6e 28 7a 53 71 6c 2c 20 63 68 61 72 73  eLen(zSql, chars
1d0a2 5f 70 61 72 73 65 64 29 3b 0a 20 20 7d 0a 20 20  _parsed);.  }.  
1d0a3 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
1d0a4 6c 38 29 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c  l8); .  rc = sql
1d0a5 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
1d0a6 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
1d0a7 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
1d0a8 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
1d0a9 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f  rc;.}../*.** Two
1d0aa 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
1d0ab 20 6f 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20   official API.  
1d0ac 4c 65 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75  Legacy and new u
1d0ad 73 65 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61  se.  In the lega
1d0ae 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74  cy.** version, t
1d0af 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
1d0b0 74 65 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65  text is not save
1d0b1 64 20 69 6e 20 74 68 65 20 70 72 65 70 61 72 65  d in the prepare
1d0b2 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61  d statement.** a
1d0b3 6e 64 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d  nd so if a schem
1d0b4 61 20 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c  a change occurs,
1d0b5 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69   SQLITE_SCHEMA i
1d0b6 73 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  s returned by.**
1d0b7 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e   sqlite3_step().
1d0b8 20 20 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72    In the new ver
1d0b9 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e  sion, the origin
1d0ba 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 72  al SQL text is r
1d0bb 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74  etained.** and t
1d0bc 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
1d0bd 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65  automatically re
1d0be 63 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73  compiled if an s
1d0bf 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20  chema change.** 
1d0c0 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  occurs..*/.SQLIT
1d0c1 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1d0c2 33 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20 73  3_prepare16(.  s
1d0c3 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
1d0c4 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1d0c5 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20  base handle. */ 
1d0c6 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1d0c7 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
1d0c8 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
1d0c9 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
1d0ca 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
1d0cb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1d0cc 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
1d0cd 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
1d0ce 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
1d0cf 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
1d0d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1d0d1 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1d0d2 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nt */.  const vo
1d0d3 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  id **pzTail     
1d0d4 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
1d0d5 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
1d0d6 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  /.){.  return sq
1d0d7 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28 64  lite3Prepare16(d
1d0d8 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c  b,zSql,nBytes,0,
1d0d9 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a  ppStmt,pzTail);.
1d0da 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
1d0db 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1d0dc 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  16_v2(.  sqlite3
1d0dd 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
1d0de 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
1d0df 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e  andle. */ .  con
1d0e0 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20  st void *zSql,  
1d0e1 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20         /* UTF-8 
1d0e2 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
1d0e3 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20  ement. */.  int 
1d0e4 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
1d0e5 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
1d0e6 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
1d0e7 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
1d0e8 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20  tmt **ppStmt,   
1d0e9 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74   /* OUT: A point
1d0ea 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72  er to the prepar
1d0eb 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ed statement */.
1d0ec 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70    const void **p
1d0ed 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f  zTail       /* O
1d0ee 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  UT: End of parse
1d0ef 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20  d string */.){. 
1d0f0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50   return sqlite3P
1d0f1 72 65 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c  repare16(db,zSql
1d0f2 2c 6e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74  ,nBytes,1,ppStmt
1d0f3 2c 70 7a 54 61 69 6c 29 3b 0a 7d 0a 0a 23 65 6e  ,pzTail);.}..#en
1d0f4 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1d0f5 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a  IT_UTF16 */../**
1d0f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
1d0f7 20 6f 66 20 70 72 65 70 61 72 65 2e 63 20 2a 2a   of prepare.c **
1d0f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1d0fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1d0fc 69 6e 20 66 69 6c 65 20 73 65 6c 65 63 74 2e 63  in file select.c
1d0fd 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1d0fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1d100 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
1d101 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
1d102 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
1d103 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
1d104 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
1d105 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
1d106 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
1d107 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
1d108 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
1d109 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
1d10a 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
1d10b 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
1d10c 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
1d10d 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
1d10e 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
1d10f 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
1d110 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
1d111 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
1d112 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
1d113 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d114 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d115 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d116 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d117 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
1d118 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
1d119 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61   routines that a
1d11a 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  re called by the
1d11b 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61   parser.** to ha
1d11c 6e 64 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74  ndle SELECT stat
1d11d 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65  ements in SQLite
1d11e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73 65 6c  ..**.** $Id: sel
1d11f 65 63 74 2e 63 2c 76 20 31 2e 33 35 39 20 32 30  ect.c,v 1.359 20
1d120 30 37 2f 30 38 2f 33 31 20 31 37 3a 34 32 3a 34  07/08/31 17:42:4
1d121 38 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  8 danielk1977 Ex
1d122 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44  p $.*/.../*.** D
1d123 65 6c 65 74 65 20 61 6c 6c 20 74 68 65 20 63 6f  elete all the co
1d124 6e 74 65 6e 74 20 6f 66 20 61 20 53 65 6c 65 63  ntent of a Selec
1d125 74 20 73 74 72 75 63 74 75 72 65 20 62 75 74 20  t structure but 
1d126 64 6f 20 6e 6f 74 20 64 65 61 6c 6c 6f 63 61 74  do not deallocat
1d127 65 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 20  e.** the select 
1d128 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
1d129 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1d12a 20 63 6c 65 61 72 53 65 6c 65 63 74 28 53 65 6c   clearSelect(Sel
1d12b 65 63 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  ect *p){.  sqlit
1d12c 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
1d12d 28 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73  (p->pEList);.  s
1d12e 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
1d12f 65 74 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20  ete(p->pSrc);.  
1d130 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1d131 65 28 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  e(p->pWhere);.  
1d132 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
1d133 65 6c 65 74 65 28 70 2d 3e 70 47 72 6f 75 70 42  elete(p->pGroupB
1d134 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  y);.  sqlite3Exp
1d135 72 44 65 6c 65 74 65 28 70 2d 3e 70 48 61 76 69  rDelete(p->pHavi
1d136 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  ng);.  sqlite3Ex
1d137 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 2d 3e  prListDelete(p->
1d138 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 73 71 6c  pOrderBy);.  sql
1d139 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
1d13a 28 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 73  (p->pPrior);.  s
1d13b 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1d13c 28 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 73  (p->pLimit);.  s
1d13d 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1d13e 28 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a  (p->pOffset);.}.
1d13f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
1d140 20 61 20 6e 65 77 20 53 65 6c 65 63 74 20 73 74   a new Select st
1d141 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75  ructure and retu
1d142 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
1d143 74 68 61 74 0a 2a 2a 20 73 74 72 75 63 74 75 72  that.** structur
1d144 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1d145 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c  VATE Select *sql
1d146 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 0a 20  ite3SelectNew(. 
1d147 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1d148 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
1d149 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
1d14a 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
1d14b 20 20 20 20 20 2f 2a 20 77 68 69 63 68 20 63 6f       /* which co
1d14c 6c 75 6d 6e 73 20 74 6f 20 69 6e 63 6c 75 64 65  lumns to include
1d14d 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a   in the result *
1d14e 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
1d14f 63 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  c,        /* the
1d150 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20   FROM clause -- 
1d151 77 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20  which tables to 
1d152 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  scan */.  Expr *
1d153 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20  pWhere,         
1d154 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  /* the WHERE cla
1d155 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
1d156 74 20 2a 70 47 72 6f 75 70 42 79 2c 20 20 20 2f  t *pGroupBy,   /
1d157 2a 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63  * the GROUP BY c
1d158 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20  lause */.  Expr 
1d159 2a 70 48 61 76 69 6e 67 2c 20 20 20 20 20 20 20  *pHaving,       
1d15a 20 2f 2a 20 74 68 65 20 48 41 56 49 4e 47 20 63   /* the HAVING c
1d15b 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lause */.  ExprL
1d15c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
1d15d 20 2f 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59   /* the ORDER BY
1d15e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
1d15f 20 69 73 44 69 73 74 69 6e 63 74 2c 20 20 20 20   isDistinct,    
1d160 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 74 68     /* true if th
1d161 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f  e DISTINCT keywo
1d162 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
1d163 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c  .  Expr *pLimit,
1d164 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49           /* LIMI
1d165 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d  T value.  NULL m
1d166 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f  eans not used */
1d167 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74  .  Expr *pOffset
1d168 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53           /* OFFS
1d169 45 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20  ET value.  NULL 
1d16a 6d 65 61 6e 73 20 6e 6f 20 6f 66 66 73 65 74 20  means no offset 
1d16b 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a  */.){.  Select *
1d16c 70 4e 65 77 3b 0a 20 20 53 65 6c 65 63 74 20 73  pNew;.  Select s
1d16d 74 61 6e 64 69 6e 3b 0a 20 20 73 71 6c 69 74 65  tandin;.  sqlite
1d16e 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1d16f 64 62 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  db;.  pNew = sql
1d170 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
1d171 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  (db, sizeof(*pNe
1d172 77 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  w) );.  assert( 
1d173 21 70 4f 66 66 73 65 74 20 7c 7c 20 70 4c 69 6d  !pOffset || pLim
1d174 69 74 20 29 3b 20 20 20 2f 2a 20 43 61 6e 27 74  it );   /* Can't
1d175 20 68 61 76 65 20 4f 46 46 53 45 54 20 77 69 74   have OFFSET wit
1d176 68 6f 75 74 20 4c 49 4d 49 54 2e 20 2a 2f 0a 20  hout LIMIT. */. 
1d177 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a   if( pNew==0 ){.
1d178 20 20 20 20 70 4e 65 77 20 3d 20 26 73 74 61 6e      pNew = &stan
1d179 64 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  din;.    memset(
1d17a 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pNew, 0, sizeof(
1d17b 2a 70 4e 65 77 29 29 3b 0a 20 20 7d 0a 20 20 69  *pNew));.  }.  i
1d17c 66 28 20 70 45 4c 69 73 74 3d 3d 30 20 29 7b 0a  f( pEList==0 ){.
1d17d 20 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c      pEList = sql
1d17e 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
1d17f 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 73 71  nd(pParse, 0, sq
1d180 6c 69 74 65 33 45 78 70 72 28 64 62 2c 54 4b 5f  lite3Expr(db,TK_
1d181 41 4c 4c 2c 30 2c 30 2c 30 29 2c 20 30 29 3b 0a  ALL,0,0,0), 0);.
1d182 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69    }.  pNew->pELi
1d183 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70  st = pEList;.  p
1d184 4e 65 77 2d 3e 70 53 72 63 20 3d 20 70 53 72 63  New->pSrc = pSrc
1d185 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65  ;.  pNew->pWhere
1d186 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 70 4e 65   = pWhere;.  pNe
1d187 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 70 47  w->pGroupBy = pG
1d188 72 6f 75 70 42 79 3b 0a 20 20 70 4e 65 77 2d 3e  roupBy;.  pNew->
1d189 70 48 61 76 69 6e 67 20 3d 20 70 48 61 76 69 6e  pHaving = pHavin
1d18a 67 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65  g;.  pNew->pOrde
1d18b 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a  rBy = pOrderBy;.
1d18c 20 20 70 4e 65 77 2d 3e 69 73 44 69 73 74 69 6e    pNew->isDistin
1d18d 63 74 20 3d 20 69 73 44 69 73 74 69 6e 63 74 3b  ct = isDistinct;
1d18e 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b  .  pNew->op = TK
1d18f 5f 53 45 4c 45 43 54 3b 0a 20 20 61 73 73 65 72  _SELECT;.  asser
1d190 74 28 20 70 4f 66 66 73 65 74 3d 3d 30 20 7c 7c  t( pOffset==0 ||
1d191 20 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20   pLimit!=0 );.  
1d192 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 70  pNew->pLimit = p
1d193 4c 69 6d 69 74 3b 0a 20 20 70 4e 65 77 2d 3e 70  Limit;.  pNew->p
1d194 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74  Offset = pOffset
1d195 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74  ;.  pNew->iLimit
1d196 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 69   = -1;.  pNew->i
1d197 4f 66 66 73 65 74 20 3d 20 2d 31 3b 0a 20 20 70  Offset = -1;.  p
1d198 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  New->addrOpenEph
1d199 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65  m[0] = -1;.  pNe
1d19a 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  w->addrOpenEphm[
1d19b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d  1] = -1;.  pNew-
1d19c 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
1d19d 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 70 4e 65   = -1;.  if( pNe
1d19e 77 3d 3d 26 73 74 61 6e 64 69 6e 29 20 7b 0a 20  w==&standin) {. 
1d19f 20 20 20 63 6c 65 61 72 53 65 6c 65 63 74 28 70     clearSelect(p
1d1a0 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d  New);.    pNew =
1d1a1 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
1d1a2 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
1d1a3 44 65 6c 65 74 65 20 74 68 65 20 67 69 76 65 6e  Delete the given
1d1a4 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72   Select structur
1d1a5 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73  e and all of its
1d1a6 20 73 75 62 73 74 72 75 63 74 75 72 65 73 2e 0a   substructures..
1d1a7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d1a8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
1d1a9 6c 65 63 74 44 65 6c 65 74 65 28 53 65 6c 65 63  lectDelete(Selec
1d1aa 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  t *p){.  if( p )
1d1ab 7b 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63  {.    clearSelec
1d1ac 74 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  t(p);.    sqlite
1d1ad 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d  3_free(p);.  }.}
1d1ae 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 31 20  ../*.** Given 1 
1d1af 74 6f 20 33 20 69 64 65 6e 74 69 66 69 65 72 73  to 3 identifiers
1d1b0 20 70 72 65 63 65 65 64 69 6e 67 20 74 68 65 20   preceeding the 
1d1b1 4a 4f 49 4e 20 6b 65 79 77 6f 72 64 2c 20 64 65  JOIN keyword, de
1d1b2 74 65 72 6d 69 6e 65 20 74 68 65 0a 2a 2a 20 74  termine the.** t
1d1b3 79 70 65 20 6f 66 20 6a 6f 69 6e 2e 20 20 52 65  ype of join.  Re
1d1b4 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20  turn an integer 
1d1b5 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 65 78  constant that ex
1d1b6 70 72 65 73 73 65 73 20 74 68 61 74 20 74 79 70  presses that typ
1d1b7 65 0a 2a 2a 20 69 6e 20 74 65 72 6d 73 20 6f 66  e.** in terms of
1d1b8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   the following b
1d1b9 69 74 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a  it values:.**.**
1d1ba 20 20 20 20 20 4a 54 5f 49 4e 4e 45 52 0a 2a 2a       JT_INNER.**
1d1bb 20 20 20 20 20 4a 54 5f 43 52 4f 53 53 0a 2a 2a       JT_CROSS.**
1d1bc 20 20 20 20 20 4a 54 5f 4f 55 54 45 52 0a 2a 2a       JT_OUTER.**
1d1bd 20 20 20 20 20 4a 54 5f 4e 41 54 55 52 41 4c 0a       JT_NATURAL.
1d1be 2a 2a 20 20 20 20 20 4a 54 5f 4c 45 46 54 0a 2a  **     JT_LEFT.*
1d1bf 2a 20 20 20 20 20 4a 54 5f 52 49 47 48 54 0a 2a  *     JT_RIGHT.*
1d1c0 2a 0a 2a 2a 20 41 20 66 75 6c 6c 20 6f 75 74 65  *.** A full oute
1d1c1 72 20 6a 6f 69 6e 20 69 73 20 74 68 65 20 63 6f  r join is the co
1d1c2 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4a 54 5f  mbination of JT_
1d1c3 4c 45 46 54 20 61 6e 64 20 4a 54 5f 52 49 47 48  LEFT and JT_RIGH
1d1c4 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69  T..**.** If an i
1d1c5 6c 6c 65 67 61 6c 20 6f 72 20 75 6e 73 75 70 70  llegal or unsupp
1d1c6 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20  orted join type 
1d1c7 69 73 20 73 65 65 6e 2c 20 74 68 65 6e 20 73 74  is seen, then st
1d1c8 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ill return.** a 
1d1c9 6a 6f 69 6e 20 74 79 70 65 2c 20 62 75 74 20 70  join type, but p
1d1ca 75 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 74  ut an error in t
1d1cb 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74  he pParse struct
1d1cc 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
1d1cd 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1d1ce 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65  e3JoinType(Parse
1d1cf 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
1d1d0 2a 70 41 2c 20 54 6f 6b 65 6e 20 2a 70 42 2c 20  *pA, Token *pB, 
1d1d1 54 6f 6b 65 6e 20 2a 70 43 29 7b 0a 20 20 69 6e  Token *pC){.  in
1d1d2 74 20 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a  t jointype = 0;.
1d1d3 20 20 54 6f 6b 65 6e 20 2a 61 70 41 6c 6c 5b 33    Token *apAll[3
1d1d4 5d 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 3b 0a 20  ];.  Token *p;. 
1d1d5 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74   static const st
1d1d6 72 75 63 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74  ruct {.    const
1d1d7 20 63 68 61 72 20 7a 4b 65 79 77 6f 72 64 5b 38   char zKeyword[8
1d1d8 5d 3b 0a 20 20 20 20 75 38 20 6e 43 68 61 72 3b  ];.    u8 nChar;
1d1d9 0a 20 20 20 20 75 38 20 63 6f 64 65 3b 0a 20 20  .    u8 code;.  
1d1da 7d 20 6b 65 79 77 6f 72 64 73 5b 5d 20 3d 20 7b  } keywords[] = {
1d1db 0a 20 20 20 20 7b 20 22 6e 61 74 75 72 61 6c 22  .    { "natural"
1d1dc 2c 20 37 2c 20 4a 54 5f 4e 41 54 55 52 41 4c 20  , 7, JT_NATURAL 
1d1dd 7d 2c 0a 20 20 20 20 7b 20 22 6c 65 66 74 22 2c  },.    { "left",
1d1de 20 20 20 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a      4, JT_LEFT|J
1d1df 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20 7b  T_OUTER },.    {
1d1e0 20 22 72 69 67 68 74 22 2c 20 20 20 35 2c 20 4a   "right",   5, J
1d1e1 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52  T_RIGHT|JT_OUTER
1d1e2 20 7d 2c 0a 20 20 20 20 7b 20 22 66 75 6c 6c 22   },.    { "full"
1d1e3 2c 20 20 20 20 34 2c 20 4a 54 5f 4c 45 46 54 7c  ,    4, JT_LEFT|
1d1e4 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45  JT_RIGHT|JT_OUTE
1d1e5 52 20 7d 2c 0a 20 20 20 20 7b 20 22 6f 75 74 65  R },.    { "oute
1d1e6 72 22 2c 20 20 20 35 2c 20 4a 54 5f 4f 55 54 45  r",   5, JT_OUTE
1d1e7 52 20 7d 2c 0a 20 20 20 20 7b 20 22 69 6e 6e 65  R },.    { "inne
1d1e8 72 22 2c 20 20 20 35 2c 20 4a 54 5f 49 4e 4e 45  r",   5, JT_INNE
1d1e9 52 20 7d 2c 0a 20 20 20 20 7b 20 22 63 72 6f 73  R },.    { "cros
1d1ea 73 22 2c 20 20 20 35 2c 20 4a 54 5f 49 4e 4e 45  s",   5, JT_INNE
1d1eb 52 7c 4a 54 5f 43 52 4f 53 53 20 7d 2c 0a 20 20  R|JT_CROSS },.  
1d1ec 7d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  };.  int i, j;. 
1d1ed 20 61 70 41 6c 6c 5b 30 5d 20 3d 20 70 41 3b 0a   apAll[0] = pA;.
1d1ee 20 20 61 70 41 6c 6c 5b 31 5d 20 3d 20 70 42 3b    apAll[1] = pB;
1d1ef 0a 20 20 61 70 41 6c 6c 5b 32 5d 20 3d 20 70 43  .  apAll[2] = pC
1d1f0 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 33  ;.  for(i=0; i<3
1d1f1 20 26 26 20 61 70 41 6c 6c 5b 69 5d 3b 20 69 2b   && apAll[i]; i+
1d1f2 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61 70 41 6c  +){.    p = apAl
1d1f3 6c 5b 69 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  l[i];.    for(j=
1d1f4 30 3b 20 6a 3c 73 69 7a 65 6f 66 28 6b 65 79 77  0; j<sizeof(keyw
1d1f5 6f 72 64 73 29 2f 73 69 7a 65 6f 66 28 6b 65 79  ords)/sizeof(key
1d1f6 77 6f 72 64 73 5b 30 5d 29 3b 20 6a 2b 2b 29 7b  words[0]); j++){
1d1f7 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6e 3d  .      if( p->n=
1d1f8 3d 6b 65 79 77 6f 72 64 73 5b 6a 5d 2e 6e 43 68  =keywords[j].nCh
1d1f9 61 72 20 0a 20 20 20 20 20 20 20 20 20 20 26 26  ar .          &&
1d1fa 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
1d1fb 28 28 63 68 61 72 2a 29 70 2d 3e 7a 2c 20 6b 65  ((char*)p->z, ke
1d1fc 79 77 6f 72 64 73 5b 6a 5d 2e 7a 4b 65 79 77 6f  ywords[j].zKeywo
1d1fd 72 64 2c 20 70 2d 3e 6e 29 3d 3d 30 20 29 7b 0a  rd, p->n)==0 ){.
1d1fe 20 20 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65          jointype
1d1ff 20 7c 3d 20 6b 65 79 77 6f 72 64 73 5b 6a 5d 2e   |= keywords[j].
1d200 63 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 62 72  code;.        br
1d201 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1d202 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d 73 69   }.    if( j>=si
1d203 7a 65 6f 66 28 6b 65 79 77 6f 72 64 73 29 2f 73  zeof(keywords)/s
1d204 69 7a 65 6f 66 28 6b 65 79 77 6f 72 64 73 5b 30  izeof(keywords[0
1d205 5d 29 20 29 7b 0a 20 20 20 20 20 20 6a 6f 69 6e  ]) ){.      join
1d206 74 79 70 65 20 7c 3d 20 4a 54 5f 45 52 52 4f 52  type |= JT_ERROR
1d207 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1d208 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 0a 20     }.  }.  if(. 
1d209 20 20 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20      (jointype & 
1d20a 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54  (JT_INNER|JT_OUT
1d20b 45 52 29 29 3d 3d 28 4a 54 5f 49 4e 4e 45 52 7c  ER))==(JT_INNER|
1d20c 4a 54 5f 4f 55 54 45 52 29 20 7c 7c 0a 20 20 20  JT_OUTER) ||.   
1d20d 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54    (jointype & JT
1d20e 5f 45 52 52 4f 52 29 21 3d 30 0a 20 20 29 7b 0a  _ERROR)!=0.  ){.
1d20f 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1d210 7a 53 70 31 20 3d 20 22 20 22 3b 0a 20 20 20 20  zSp1 = " ";.    
1d211 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 70 32  const char *zSp2
1d212 20 3d 20 22 20 22 3b 0a 20 20 20 20 69 66 28 20   = " ";.    if( 
1d213 70 42 3d 3d 30 20 29 7b 20 7a 53 70 31 2b 2b 3b  pB==0 ){ zSp1++;
1d214 20 7d 0a 20 20 20 20 69 66 28 20 70 43 3d 3d 30   }.    if( pC==0
1d215 20 29 7b 20 7a 53 70 32 2b 2b 3b 20 7d 0a 20 20   ){ zSp2++; }.  
1d216 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1d217 67 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f  g(pParse, "unkno
1d218 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65  wn or unsupporte
1d219 64 20 6a 6f 69 6e 20 74 79 70 65 3a 20 22 0a 20  d join type: ". 
1d21a 20 20 20 20 20 20 22 25 54 25 73 25 54 25 73 25        "%T%s%T%s%
1d21b 54 22 2c 20 70 41 2c 20 7a 53 70 31 2c 20 70 42  T", pA, zSp1, pB
1d21c 2c 20 7a 53 70 32 2c 20 70 43 29 3b 0a 20 20 20  , zSp2, pC);.   
1d21d 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54 5f 49   jointype = JT_I
1d21e 4e 4e 45 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66  NNER;.  }else if
1d21f 28 20 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ( jointype & JT_
1d220 52 49 47 48 54 20 29 7b 0a 20 20 20 20 73 71 6c  RIGHT ){.    sql
1d221 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1d222 72 73 65 2c 20 0a 20 20 20 20 20 20 22 52 49 47  rse, .      "RIG
1d223 48 54 20 61 6e 64 20 46 55 4c 4c 20 4f 55 54 45  HT and FULL OUTE
1d224 52 20 4a 4f 49 4e 73 20 61 72 65 20 6e 6f 74 20  R JOINs are not 
1d225 63 75 72 72 65 6e 74 6c 79 20 73 75 70 70 6f 72  currently suppor
1d226 74 65 64 22 29 3b 0a 20 20 20 20 6a 6f 69 6e 74  ted");.    joint
1d227 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a  ype = JT_INNER;.
1d228 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6a 6f 69    }.  return joi
1d229 6e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ntype;.}../*.** 
1d22a 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78  Return the index
1d22b 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   of a column in 
1d22c 61 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e  a table.  Return
1d22d 20 2d 31 20 69 66 20 74 68 65 20 63 6f 6c 75 6d   -1 if the colum
1d22e 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 74  n.** is not cont
1d22f 61 69 6e 65 64 20 69 6e 20 74 68 65 20 74 61 62  ained in the tab
1d230 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1d231 74 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 54 61  t columnIndex(Ta
1d232 62 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74  ble *pTab, const
1d233 20 63 68 61 72 20 2a 7a 43 6f 6c 29 7b 0a 20 20   char *zCol){.  
1d234 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
1d235 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
1d236 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  i++){.    if( sq
1d237 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61  lite3StrICmp(pTa
1d238 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65  b->aCol[i].zName
1d239 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 20 72 65 74  , zCol)==0 ) ret
1d23a 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74  urn i;.  }.  ret
1d23b 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn -1;.}../*.**
1d23c 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   Set the value o
1d23d 66 20 61 20 74 6f 6b 65 6e 20 74 6f 20 61 20 27  f a token to a '
1d23e 5c 30 30 30 27 2d 74 65 72 6d 69 6e 61 74 65 64  \000'-terminated
1d23f 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74   string..*/.stat
1d240 69 63 20 76 6f 69 64 20 73 65 74 54 6f 6b 65 6e  ic void setToken
1d241 28 54 6f 6b 65 6e 20 2a 70 2c 20 63 6f 6e 73 74  (Token *p, const
1d242 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 70 2d 3e   char *z){.  p->
1d243 7a 20 3d 20 28 75 38 2a 29 7a 3b 0a 20 20 70 2d  z = (u8*)z;.  p-
1d244 3e 6e 20 3d 20 7a 20 3f 20 73 74 72 6c 65 6e 28  >n = z ? strlen(
1d245 7a 29 20 3a 20 30 3b 0a 20 20 70 2d 3e 64 79 6e  z) : 0;.  p->dyn
1d246 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53   = 0;.}../*.** S
1d247 65 74 20 74 68 65 20 74 6f 6b 65 6e 20 74 6f 20  et the token to 
1d248 74 68 65 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65  the double-quote
1d249 64 20 61 6e 64 20 65 73 63 61 70 65 64 20 76 65  d and escaped ve
1d24a 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 72  rsion of the str
1d24b 69 6e 67 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74  ing pointed.** t
1d24c 6f 20 62 79 20 7a 2e 20 46 6f 72 20 65 78 61 6d  o by z. For exam
1d24d 70 6c 65 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 7b 61  ple;.**.**    {a
1d24e 22 62 63 7d 20 20 2d 3e 20 20 7b 22 61 22 22 62  "bc}  ->  {"a""b
1d24f 63 22 7d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  c"}.*/.static vo
1d250 69 64 20 73 65 74 51 75 6f 74 65 64 54 6f 6b 65  id setQuotedToke
1d251 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
1d252 20 54 6f 6b 65 6e 20 2a 70 2c 20 63 6f 6e 73 74   Token *p, const
1d253 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 70 2d 3e   char *z){.  p->
1d254 7a 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  z = (u8 *)sqlite
1d255 33 4d 50 72 69 6e 74 66 28 30 2c 20 22 5c 22 25  3MPrintf(0, "\"%
1d256 77 5c 22 22 2c 20 7a 29 3b 0a 20 20 70 2d 3e 64  w\"", z);.  p->d
1d257 79 6e 20 3d 20 31 3b 0a 20 20 69 66 28 20 70 2d  yn = 1;.  if( p-
1d258 3e 7a 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 20 3d  >z ){.    p->n =
1d259 20 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29   strlen((char *)
1d25a 70 2d 3e 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  p->z);.  }else{.
1d25b 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e      pParse->db->
1d25c 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1d25d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
1d25e 72 65 61 74 65 20 61 6e 20 65 78 70 72 65 73 73  reate an express
1d25f 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 6e 20  ion node for an 
1d260 69 64 65 6e 74 69 66 69 65 72 20 77 69 74 68 20  identifier with 
1d261 74 68 65 20 6e 61 6d 65 20 6f 66 20 7a 4e 61 6d  the name of zNam
1d262 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e.*/.SQLITE_PRIV
1d263 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
1d264 33 43 72 65 61 74 65 49 64 45 78 70 72 28 50 61  3CreateIdExpr(Pa
1d265 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
1d266 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
1d267 0a 20 20 54 6f 6b 65 6e 20 64 75 6d 6d 79 3b 0a  .  Token dummy;.
1d268 20 20 73 65 74 54 6f 6b 65 6e 28 26 64 75 6d 6d    setToken(&dumm
1d269 79 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74  y, zName);.  ret
1d26a 75 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70 72  urn sqlite3PExpr
1d26b 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
1d26c 30 2c 20 30 2c 20 26 64 75 6d 6d 79 29 3b 0a 7d  0, 0, &dummy);.}
1d26d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 74  .../*.** Add a t
1d26e 65 72 6d 20 74 6f 20 74 68 65 20 57 48 45 52 45  erm to the WHERE
1d26f 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 2a   expression in *
1d270 70 70 45 78 70 72 20 74 68 61 74 20 72 65 71 75  ppExpr that requ
1d271 69 72 65 73 20 74 68 65 0a 2a 2a 20 7a 43 6f 6c  ires the.** zCol
1d272 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 65 71   column to be eq
1d273 75 61 6c 20 69 6e 20 74 68 65 20 74 77 6f 20 74  ual in the two t
1d274 61 62 6c 65 73 20 70 54 61 62 31 20 61 6e 64 20  ables pTab1 and 
1d275 70 54 61 62 32 2e 0a 2a 2f 0a 73 74 61 74 69 63  pTab2..*/.static
1d276 20 76 6f 69 64 20 61 64 64 57 68 65 72 65 54 65   void addWhereTe
1d277 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  rm(.  Parse *pPa
1d278 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  rse,           /
1d279 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1d27a 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
1d27b 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20  r *zCol,        
1d27c 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
1d27d 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  olumn */.  const
1d27e 20 54 61 62 6c 65 20 2a 70 54 61 62 31 2c 20 20   Table *pTab1,  
1d27f 20 20 20 20 2f 2a 20 46 69 72 73 74 20 74 61 62      /* First tab
1d280 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
1d281 61 72 20 2a 7a 41 6c 69 61 73 31 2c 20 20 20 20  ar *zAlias1,    
1d282 20 2f 2a 20 41 6c 69 61 73 20 66 6f 72 20 66 69   /* Alias for fi
1d283 72 73 74 20 74 61 62 6c 65 2e 20 20 4d 61 79 20  rst table.  May 
1d284 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e  be NULL */.  con
1d285 73 74 20 54 61 62 6c 65 20 2a 70 54 61 62 32 2c  st Table *pTab2,
1d286 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20        /* Second 
1d287 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  table */.  const
1d288 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 32 2c 20   char *zAlias2, 
1d289 20 20 20 20 2f 2a 20 41 6c 69 61 73 20 66 6f 72      /* Alias for
1d28a 20 73 65 63 6f 6e 64 20 74 61 62 6c 65 2e 20 20   second table.  
1d28b 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
1d28c 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69 6e 54   int iRightJoinT
1d28d 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 56 44 42  able,     /* VDB
1d28e 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65  E cursor for the
1d28f 20 72 69 67 68 74 20 74 61 62 6c 65 20 2a 2f 0a   right table */.
1d290 20 20 45 78 70 72 20 2a 2a 70 70 45 78 70 72 20    Expr **ppExpr 
1d291 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
1d292 64 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 74  d the equality t
1d293 65 72 6d 20 74 6f 20 74 68 69 73 20 65 78 70 72  erm to this expr
1d294 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 45  ession */.){.  E
1d295 78 70 72 20 2a 70 45 31 61 2c 20 2a 70 45 31 62  xpr *pE1a, *pE1b
1d296 2c 20 2a 70 45 31 63 3b 0a 20 20 45 78 70 72 20  , *pE1c;.  Expr 
1d297 2a 70 45 32 61 2c 20 2a 70 45 32 62 2c 20 2a 70  *pE2a, *pE2b, *p
1d298 45 32 63 3b 0a 20 20 45 78 70 72 20 2a 70 45 3b  E2c;.  Expr *pE;
1d299 0a 0a 20 20 70 45 31 61 20 3d 20 73 71 6c 69 74  ..  pE1a = sqlit
1d29a 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28 70  e3CreateIdExpr(p
1d29b 50 61 72 73 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20  Parse, zCol);.  
1d29c 70 45 32 61 20 3d 20 73 71 6c 69 74 65 33 43 72  pE2a = sqlite3Cr
1d29d 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73  eateIdExpr(pPars
1d29e 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20  e, zCol);.  if( 
1d29f 7a 41 6c 69 61 73 31 3d 3d 30 20 29 7b 0a 20 20  zAlias1==0 ){.  
1d2a0 20 20 7a 41 6c 69 61 73 31 20 3d 20 70 54 61 62    zAlias1 = pTab
1d2a1 31 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20  1->zName;.  }.  
1d2a2 70 45 31 62 20 3d 20 73 71 6c 69 74 65 33 43 72  pE1b = sqlite3Cr
1d2a3 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73  eateIdExpr(pPars
1d2a4 65 2c 20 7a 41 6c 69 61 73 31 29 3b 0a 20 20 69  e, zAlias1);.  i
1d2a5 66 28 20 7a 41 6c 69 61 73 32 3d 3d 30 20 29 7b  f( zAlias2==0 ){
1d2a6 0a 20 20 20 20 7a 41 6c 69 61 73 32 20 3d 20 70  .    zAlias2 = p
1d2a7 54 61 62 32 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 7d  Tab2->zName;.  }
1d2a8 0a 20 20 70 45 32 62 20 3d 20 73 71 6c 69 74 65  .  pE2b = sqlite
1d2a9 33 43 72 65 61 74 65 49 64 45 78 70 72 28 70 50  3CreateIdExpr(pP
1d2aa 61 72 73 65 2c 20 7a 41 6c 69 61 73 32 29 3b 0a  arse, zAlias2);.
1d2ab 20 20 70 45 31 63 20 3d 20 73 71 6c 69 74 65 33    pE1c = sqlite3
1d2ac 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
1d2ad 5f 44 4f 54 2c 20 70 45 31 62 2c 20 70 45 31 61  _DOT, pE1b, pE1a
1d2ae 2c 20 30 29 3b 0a 20 20 70 45 32 63 20 3d 20 73  , 0);.  pE2c = s
1d2af 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
1d2b0 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 45 32 62  se, TK_DOT, pE2b
1d2b1 2c 20 70 45 32 61 2c 20 30 29 3b 0a 20 20 70 45  , pE2a, 0);.  pE
1d2b2 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
1d2b3 70 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c 20 70  pParse, TK_EQ, p
1d2b4 45 31 63 2c 20 70 45 32 63 2c 20 30 29 3b 0a 20  E1c, pE2c, 0);. 
1d2b5 20 69 66 28 20 70 45 20 29 7b 0a 20 20 20 20 45   if( pE ){.    E
1d2b6 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70  xprSetProperty(p
1d2b7 45 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b  E, EP_FromJoin);
1d2b8 0a 20 20 20 20 70 45 2d 3e 69 52 69 67 68 74 4a  .    pE->iRightJ
1d2b9 6f 69 6e 54 61 62 6c 65 20 3d 20 69 52 69 67 68  oinTable = iRigh
1d2ba 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a 20 20 7d 0a  tJoinTable;.  }.
1d2bb 20 20 70 45 20 3d 20 73 71 6c 69 74 65 33 45 78    pE = sqlite3Ex
1d2bc 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  prAnd(pParse->db
1d2bd 2c 2a 70 70 45 78 70 72 2c 20 70 45 29 3b 0a 20  ,*ppExpr, pE);. 
1d2be 20 69 66 28 20 70 45 20 29 7b 0a 20 20 20 20 2a   if( pE ){.    *
1d2bf 70 70 45 78 70 72 20 3d 20 70 45 3b 0a 20 20 7d  ppExpr = pE;.  }
1d2c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
1d2c1 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72  e EP_FromJoin pr
1d2c2 6f 70 65 72 74 79 20 6f 6e 20 61 6c 6c 20 74 65  operty on all te
1d2c3 72 6d 73 20 6f 66 20 74 68 65 20 67 69 76 65 6e  rms of the given
1d2c4 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20   expression..** 
1d2c5 41 6e 64 20 73 65 74 20 74 68 65 20 45 78 70 72  And set the Expr
1d2c6 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  .iRightJoinTable
1d2c7 20 74 6f 20 69 54 61 62 6c 65 20 66 6f 72 20 65   to iTable for e
1d2c8 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65  very term in the
1d2c9 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  .** expression..
1d2ca 2a 2a 0a 2a 2a 20 54 68 65 20 45 50 5f 46 72 6f  **.** The EP_Fro
1d2cb 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 69  mJoin property i
1d2cc 73 20 75 73 65 64 20 6f 6e 20 74 65 72 6d 73 20  s used on terms 
1d2cd 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
1d2ce 20 74 6f 20 74 65 6c 6c 0a 2a 2a 20 74 68 65 20   to tell.** the 
1d2cf 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
1d2d0 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63  processing logic
1d2d1 20 74 68 61 74 20 74 68 69 73 20 74 65 72 6d 20   that this term 
1d2d2 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a  is part of the.*
1d2d3 2a 20 6a 6f 69 6e 20 72 65 73 74 72 69 63 74 69  * join restricti
1d2d4 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  on specified in 
1d2d5 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  the ON or USING 
1d2d6 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 74 20 61  clause and not a
1d2d7 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20   part.** of the 
1d2d8 6d 6f 72 65 20 67 65 6e 65 72 61 6c 20 57 48 45  more general WHE
1d2d9 52 45 20 63 6c 61 75 73 65 2e 20 20 54 68 65 73  RE clause.  Thes
1d2da 65 20 74 65 72 6d 73 20 61 72 65 20 6d 6f 76 65  e terms are move
1d2db 64 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a  d over to the.**
1d2dc 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 64 75   WHERE clause du
1d2dd 72 69 6e 67 20 6a 6f 69 6e 20 70 72 6f 63 65 73  ring join proces
1d2de 73 69 6e 67 20 62 75 74 20 77 65 20 6e 65 65 64  sing but we need
1d2df 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61   to remember tha
1d2e0 74 20 74 68 65 79 0a 2a 2a 20 6f 72 69 67 69 6e  t they.** origin
1d2e1 61 74 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f  ated in the ON o
1d2e2 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a  r USING clause..
1d2e3 2a 2a 0a 2a 2a 20 54 68 65 20 45 78 70 72 2e 69  **.** The Expr.i
1d2e4 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74  RightJoinTable t
1d2e5 65 6c 6c 73 20 74 68 65 20 57 48 45 52 45 20 63  ells the WHERE c
1d2e6 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
1d2e7 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 65 78 70   that the.** exp
1d2e8 72 65 73 73 69 6f 6e 20 64 65 70 65 6e 64 73 20  ression depends 
1d2e9 6f 6e 20 74 61 62 6c 65 20 69 52 69 67 68 74 4a  on table iRightJ
1d2ea 6f 69 6e 54 61 62 6c 65 20 65 76 65 6e 20 69 66  oinTable even if
1d2eb 20 74 68 61 74 20 74 61 62 6c 65 20 69 73 20 6e   that table is n
1d2ec 6f 74 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79  ot.** explicitly
1d2ed 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68   mentioned in th
1d2ee 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54  e expression.  T
1d2ef 68 61 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  hat information 
1d2f0 69 73 20 6e 65 65 64 65 64 0a 2a 2a 20 66 6f 72  is needed.** for
1d2f1 20 63 61 73 65 73 20 6c 69 6b 65 20 74 68 69 73   cases like this
1d2f2 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 45 4c 45 43  :.**.**    SELEC
1d2f3 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54  T * FROM t1 LEFT
1d2f4 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61   JOIN t2 ON t1.a
1d2f5 3d 74 32 2e 62 20 41 4e 44 20 74 31 2e 78 3d 35  =t2.b AND t1.x=5
1d2f6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 68 65 72 65  .**.** The where
1d2f7 20 63 6c 61 75 73 65 20 6e 65 65 64 73 20 74 6f   clause needs to
1d2f8 20 64 65 66 65 72 20 74 68 65 20 68 61 6e 64 6c   defer the handl
1d2f9 69 6e 67 20 6f 66 20 74 68 65 20 74 31 2e 78 3d  ing of the t1.x=
1d2fa 35 0a 2a 2a 20 74 65 72 6d 20 75 6e 74 69 6c 20  5.** term until 
1d2fb 61 66 74 65 72 20 74 68 65 20 74 32 20 6c 6f 6f  after the t2 loo
1d2fc 70 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20  p of the join.  
1d2fd 49 6e 20 74 68 61 74 20 77 61 79 2c 20 61 0a 2a  In that way, a.*
1d2fe 2a 20 4e 55 4c 4c 20 74 32 20 72 6f 77 20 77 69  * NULL t2 row wi
1d2ff 6c 6c 20 62 65 20 69 6e 73 65 72 74 65 64 20 77  ll be inserted w
1d300 68 65 6e 65 76 65 72 20 74 31 2e 78 21 3d 35 2e  henever t1.x!=5.
1d301 20 20 49 66 20 77 65 20 64 6f 20 6e 6f 74 0a 2a    If we do not.*
1d302 2a 20 64 65 66 65 72 20 74 68 65 20 68 61 6e 64  * defer the hand
1d303 6c 69 6e 67 20 6f 66 20 74 31 2e 78 3d 35 2c 20  ling of t1.x=5, 
1d304 69 74 20 77 69 6c 6c 20 62 65 20 70 72 6f 63 65  it will be proce
1d305 73 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  ssed immediately
1d306 0a 2a 2a 20 61 66 74 65 72 20 74 68 65 20 74 31  .** after the t1
1d307 20 6c 6f 6f 70 20 61 6e 64 20 72 6f 77 73 20 77   loop and rows w
1d308 69 74 68 20 74 31 2e 78 21 3d 35 20 77 69 6c 6c  ith t1.x!=5 will
1d309 20 6e 65 76 65 72 20 61 70 70 65 61 72 20 69 6e   never appear in
1d30a 0a 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2c 20  .** the output, 
1d30b 77 68 69 63 68 20 69 73 20 69 6e 63 6f 72 72 65  which is incorre
1d30c 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ct..*/.static vo
1d30d 69 64 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 45  id setJoinExpr(E
1d30e 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 54 61 62  xpr *p, int iTab
1d30f 6c 65 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 20  le){.  while( p 
1d310 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72  ){.    ExprSetPr
1d311 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 46 72 6f  operty(p, EP_Fro
1d312 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 70 2d 3e 69  mJoin);.    p->i
1d313 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
1d314 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 73 65 74   iTable;.    set
1d315 4a 6f 69 6e 45 78 70 72 28 70 2d 3e 70 4c 65 66  JoinExpr(p->pLef
1d316 74 2c 20 69 54 61 62 6c 65 29 3b 0a 20 20 20 20  t, iTable);.    
1d317 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20  p = p->pRight;. 
1d318 20 7d 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   } .}../*.** Thi
1d319 73 20 72 6f 75 74 69 6e 65 20 70 72 6f 63 65 73  s routine proces
1d31a 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 69 6e 66  ses the join inf
1d31b 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 53  ormation for a S
1d31c 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
1d31d 0a 2a 2a 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  .** ON and USING
1d31e 20 63 6c 61 75 73 65 73 20 61 72 65 20 63 6f 6e   clauses are con
1d31f 76 65 72 74 65 64 20 69 6e 74 6f 20 65 78 74 72  verted into extr
1d320 61 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57  a terms of the W
1d321 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2a 20  HERE clause..** 
1d322 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 73 20 61 6c  NATURAL joins al
1d323 73 6f 20 63 72 65 61 74 65 20 65 78 74 72 61 20  so create extra 
1d324 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
1d325 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65  ms..**.** The te
1d326 72 6d 73 20 6f 66 20 61 20 46 52 4f 4d 20 63 6c  rms of a FROM cl
1d327 61 75 73 65 20 61 72 65 20 63 6f 6e 74 61 69 6e  ause are contain
1d328 65 64 20 69 6e 20 74 68 65 20 53 65 6c 65 63 74  ed in the Select
1d329 2e 70 53 72 63 20 73 74 72 75 63 74 75 72 65 2e  .pSrc structure.
1d32a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 20 6d 6f 73  .** The left mos
1d32b 74 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 66  t table is the f
1d32c 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 53 65  irst entry in Se
1d32d 6c 65 63 74 2e 70 53 72 63 2e 20 20 54 68 65 20  lect.pSrc.  The 
1d32e 72 69 67 68 74 2d 6d 6f 73 74 0a 2a 2a 20 74 61  right-most.** ta
1d32f 62 6c 65 20 69 73 20 74 68 65 20 6c 61 73 74 20  ble is the last 
1d330 65 6e 74 72 79 2e 20 20 54 68 65 20 6a 6f 69 6e  entry.  The join
1d331 20 6f 70 65 72 61 74 6f 72 20 69 73 20 68 65 6c   operator is hel
1d332 64 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74  d in the entry t
1d333 6f 0a 2a 2a 20 74 68 65 20 6c 65 66 74 2e 20 20  o.** the left.  
1d334 54 68 75 73 20 65 6e 74 72 79 20 30 20 63 6f 6e  Thus entry 0 con
1d335 74 61 69 6e 73 20 74 68 65 20 6a 6f 69 6e 20 6f  tains the join o
1d336 70 65 72 61 74 6f 72 20 66 6f 72 20 74 68 65 20  perator for the 
1d337 6a 6f 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20  join between.** 
1d338 65 6e 74 72 69 65 73 20 30 20 61 6e 64 20 31 2e  entries 0 and 1.
1d339 20 20 41 6e 79 20 4f 4e 20 6f 72 20 55 53 49 4e    Any ON or USIN
1d33a 47 20 63 6c 61 75 73 65 73 20 61 73 73 6f 63 69  G clauses associ
1d33b 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6a 6f  ated with the jo
1d33c 69 6e 20 61 72 65 0a 2a 2a 20 61 6c 73 6f 20 61  in are.** also a
1d33d 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c  ttached to the l
1d33e 65 66 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a  eft entry..**.**
1d33f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
1d340 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
1d341 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75   of errors encou
1d342 6e 74 65 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  ntered..*/.stati
1d343 63 20 69 6e 74 20 73 71 6c 69 74 65 50 72 6f 63  c int sqliteProc
1d344 65 73 73 4a 6f 69 6e 28 50 61 72 73 65 20 2a 70  essJoin(Parse *p
1d345 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70  Parse, Select *p
1d346 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  ){.  SrcList *pS
1d347 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
1d348 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c       /* All tabl
1d349 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
1d34a 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
1d34b 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  , j;            
1d34c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
1d34d 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
1d34e 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
1d34f 69 74 65 6d 20 2a 70 4c 65 66 74 3b 20 20 20 20  item *pLeft;    
1d350 20 2f 2a 20 4c 65 66 74 20 74 61 62 6c 65 20 62   /* Left table b
1d351 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 20  eing joined */. 
1d352 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
1d353 69 74 65 6d 20 2a 70 52 69 67 68 74 3b 20 20 20  item *pRight;   
1d354 20 2f 2a 20 52 69 67 68 74 20 74 61 62 6c 65 20   /* Right table 
1d355 62 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a  being joined */.
1d356 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72  .  pSrc = p->pSr
1d357 63 3b 0a 20 20 70 4c 65 66 74 20 3d 20 26 70 53  c;.  pLeft = &pS
1d358 72 63 2d 3e 61 5b 30 5d 3b 0a 20 20 70 52 69 67  rc->a[0];.  pRig
1d359 68 74 20 3d 20 26 70 4c 65 66 74 5b 31 5d 3b 0a  ht = &pLeft[1];.
1d35a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72    for(i=0; i<pSr
1d35b 63 2d 3e 6e 53 72 63 2d 31 3b 20 69 2b 2b 2c 20  c->nSrc-1; i++, 
1d35c 70 52 69 67 68 74 2b 2b 2c 20 70 4c 65 66 74 2b  pRight++, pLeft+
1d35d 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  +){.    Table *p
1d35e 4c 65 66 74 54 61 62 20 3d 20 70 4c 65 66 74 2d  LeftTab = pLeft-
1d35f 3e 70 54 61 62 3b 0a 20 20 20 20 54 61 62 6c 65  >pTab;.    Table
1d360 20 2a 70 52 69 67 68 74 54 61 62 20 3d 20 70 52   *pRightTab = pR
1d361 69 67 68 74 2d 3e 70 54 61 62 3b 0a 0a 20 20 20  ight->pTab;..   
1d362 20 69 66 28 20 70 4c 65 66 74 54 61 62 3d 3d 30   if( pLeftTab==0
1d363 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d 3d 30   || pRightTab==0
1d364 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20   ) continue;..  
1d365 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 4e 41    /* When the NA
1d366 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 20 69 73  TURAL keyword is
1d367 20 70 72 65 73 65 6e 74 2c 20 61 64 64 20 57 48   present, add WH
1d368 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73  ERE clause terms
1d369 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65 76 65 72   for.    ** ever
1d36a 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68  y column that th
1d36b 65 20 74 77 6f 20 74 61 62 6c 65 73 20 68 61 76  e two tables hav
1d36c 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20 20 20  e in common..   
1d36d 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67   */.    if( pRig
1d36e 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a  ht->jointype & J
1d36f 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20 20  T_NATURAL ){.   
1d370 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70     if( pRight->p
1d371 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d 3e 70 55  On || pRight->pU
1d372 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20  sing ){.        
1d373 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1d374 70 50 61 72 73 65 2c 20 22 61 20 4e 41 54 55 52  pParse, "a NATUR
1d375 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e 6f 74 20  AL join may not 
1d376 68 61 76 65 20 22 0a 20 20 20 20 20 20 20 20 20  have ".         
1d377 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e    "an ON or USIN
1d378 47 20 63 6c 61 75 73 65 22 2c 20 30 29 3b 0a 20  G clause", 0);. 
1d379 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
1d37a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
1d37b 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65 66 74 54  or(j=0; j<pLeftT
1d37c 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a  ab->nCol; j++){.
1d37d 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e          char *zN
1d37e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61 62 2d 3e  ame = pLeftTab->
1d37f 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20  aCol[j].zName;. 
1d380 20 20 20 20 20 20 20 69 66 28 20 63 6f 6c 75 6d         if( colum
1d381 6e 49 6e 64 65 78 28 70 52 69 67 68 74 54 61 62  nIndex(pRightTab
1d382 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20  , zName)>=0 ){. 
1d383 20 20 20 20 20 20 20 20 20 61 64 64 57 68 65 72           addWher
1d384 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20 7a 4e  eTerm(pParse, zN
1d385 61 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c 20 70  ame, pLeftTab, p
1d386 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20  Left->zAlias, . 
1d387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d388 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69               pRi
1d389 67 68 74 54 61 62 2c 20 70 52 69 67 68 74 2d 3e  ghtTab, pRight->
1d38a 7a 41 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20  zAlias,.        
1d38b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d38c 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 69 43        pRight->iC
1d38d 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57 68 65 72  ursor, &p->pWher
1d38e 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 0a 20  e);.          . 
1d38f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1d390 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
1d391 69 73 61 6c 6c 6f 77 20 62 6f 74 68 20 4f 4e 20  isallow both ON 
1d392 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
1d393 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f  s in the same jo
1d394 69 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  in.    */.    if
1d395 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 26 26  ( pRight->pOn &&
1d396 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20   pRight->pUsing 
1d397 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1d398 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1d399 20 22 63 61 6e 6e 6f 74 20 68 61 76 65 20 62 6f   "cannot have bo
1d39a 74 68 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20  th ON and USING 
1d39b 22 0a 20 20 20 20 20 20 20 20 22 63 6c 61 75 73  ".        "claus
1d39c 65 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6a  es in the same j
1d39d 6f 69 6e 22 29 3b 0a 20 20 20 20 20 20 72 65 74  oin");.      ret
1d39e 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20  urn 1;.    }..  
1d39f 20 20 2f 2a 20 41 64 64 20 74 68 65 20 4f 4e 20    /* Add the ON 
1d3a0 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20 65 6e  clause to the en
1d3a1 64 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  d of the WHERE c
1d3a2 6c 61 75 73 65 2c 20 63 6f 6e 6e 65 63 74 65 64  lause, connected
1d3a3 20 62 79 0a 20 20 20 20 2a 2a 20 61 6e 20 41 4e   by.    ** an AN
1d3a4 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20  D operator..    
1d3a5 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68  */.    if( pRigh
1d3a6 74 2d 3e 70 4f 6e 20 29 7b 0a 20 20 20 20 20 20  t->pOn ){.      
1d3a7 73 65 74 4a 6f 69 6e 45 78 70 72 28 70 52 69 67  setJoinExpr(pRig
1d3a8 68 74 2d 3e 70 4f 6e 2c 20 70 52 69 67 68 74 2d  ht->pOn, pRight-
1d3a9 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >iCursor);.     
1d3aa 20 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c   p->pWhere = sql
1d3ab 69 74 65 33 45 78 70 72 41 6e 64 28 70 50 61 72  ite3ExprAnd(pPar
1d3ac 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 57 68 65 72  se->db, p->pWher
1d3ad 65 2c 20 70 52 69 67 68 74 2d 3e 70 4f 6e 29 3b  e, pRight->pOn);
1d3ae 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 70  .      pRight->p
1d3af 4f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  On = 0;.    }.. 
1d3b0 20 20 20 2f 2a 20 43 72 65 61 74 65 20 65 78 74     /* Create ext
1d3b1 72 61 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20  ra terms on the 
1d3b2 57 48 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72  WHERE clause for
1d3b3 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6e 61 6d   each column nam
1d3b4 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  ed.    ** in the
1d3b5 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 20 20   USING clause.  
1d3b6 45 78 61 6d 70 6c 65 3a 20 49 66 20 74 68 65 20  Example: If the 
1d3b7 74 77 6f 20 74 61 62 6c 65 73 20 74 6f 20 62 65  two tables to be
1d3b8 20 6a 6f 69 6e 65 64 20 61 72 65 20 0a 20 20 20   joined are .   
1d3b9 20 2a 2a 20 41 20 61 6e 64 20 42 20 61 6e 64 20   ** A and B and 
1d3ba 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  the USING clause
1d3bb 20 6e 61 6d 65 73 20 58 2c 20 59 2c 20 61 6e 64   names X, Y, and
1d3bc 20 5a 2c 20 74 68 65 6e 20 61 64 64 20 74 68 69   Z, then add thi
1d3bd 73 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  s.    ** to the 
1d3be 57 48 45 52 45 20 63 6c 61 75 73 65 3a 20 20 20  WHERE clause:   
1d3bf 20 41 2e 58 3d 42 2e 58 20 41 4e 44 20 41 2e 59   A.X=B.X AND A.Y
1d3c0 3d 42 2e 59 20 41 4e 44 20 41 2e 5a 3d 42 2e 5a  =B.Y AND A.Z=B.Z
1d3c1 0a 20 20 20 20 2a 2a 20 52 65 70 6f 72 74 20 61  .    ** Report a
1d3c2 6e 20 65 72 72 6f 72 20 69 66 20 61 6e 79 20 63  n error if any c
1d3c3 6f 6c 75 6d 6e 20 6d 65 6e 74 69 6f 6e 65 64 20  olumn mentioned 
1d3c4 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61  in the USING cla
1d3c5 75 73 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f  use is.    ** no
1d3c6 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 62  t contained in b
1d3c7 6f 74 68 20 74 61 62 6c 65 73 20 74 6f 20 62 65  oth tables to be
1d3c8 20 6a 6f 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a   joined..    */.
1d3c9 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
1d3ca 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20  pUsing ){.      
1d3cb 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  IdList *pList = 
1d3cc 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 3b 0a  pRight->pUsing;.
1d3cd 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
1d3ce 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a 2b 2b  <pList->nId; j++
1d3cf 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
1d3d0 2a 7a 4e 61 6d 65 20 3d 20 70 4c 69 73 74 2d 3e  *zName = pList->
1d3d1 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  a[j].zName;.    
1d3d2 20 20 20 20 69 66 28 20 63 6f 6c 75 6d 6e 49 6e      if( columnIn
1d3d3 64 65 78 28 70 4c 65 66 74 54 61 62 2c 20 7a 4e  dex(pLeftTab, zN
1d3d4 61 6d 65 29 3c 30 20 7c 7c 20 63 6f 6c 75 6d 6e  ame)<0 || column
1d3d5 49 6e 64 65 78 28 70 52 69 67 68 74 54 61 62 2c  Index(pRightTab,
1d3d6 20 7a 4e 61 6d 65 29 3c 30 20 29 7b 0a 20 20 20   zName)<0 ){.   
1d3d7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1d3d8 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1d3d9 63 61 6e 6e 6f 74 20 6a 6f 69 6e 20 75 73 69 6e  cannot join usin
1d3da 67 20 63 6f 6c 75 6d 6e 20 25 73 20 2d 20 63 6f  g column %s - co
1d3db 6c 75 6d 6e 20 22 0a 20 20 20 20 20 20 20 20 20  lumn ".         
1d3dc 20 20 20 22 6e 6f 74 20 70 72 65 73 65 6e 74 20     "not present 
1d3dd 69 6e 20 62 6f 74 68 20 74 61 62 6c 65 73 22 2c  in both tables",
1d3de 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
1d3df 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1d3e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
1d3e1 64 64 57 68 65 72 65 54 65 72 6d 28 70 50 61 72  ddWhereTerm(pPar
1d3e2 73 65 2c 20 7a 4e 61 6d 65 2c 20 70 4c 65 66 74  se, zName, pLeft
1d3e3 54 61 62 2c 20 70 4c 65 66 74 2d 3e 7a 41 6c 69  Tab, pLeft->zAli
1d3e4 61 73 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  as, .           
1d3e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3e6 20 70 52 69 67 68 74 54 61 62 2c 20 70 52 69 67   pRightTab, pRig
1d3e7 68 74 2d 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20  ht->zAlias,.    
1d3e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3e9 20 20 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e          pRight->
1d3ea 69 43 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57 68  iCursor, &p->pWh
1d3eb 65 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ere);.      }.  
1d3ec 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1d3ed 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73   0;.}../*.** Ins
1d3ee 65 72 74 20 63 6f 64 65 20 69 6e 74 6f 20 22 76  ert code into "v
1d3ef 22 20 74 68 61 74 20 77 69 6c 6c 20 70 75 73 68  " that will push
1d3f0 20 74 68 65 20 72 65 63 6f 72 64 20 6f 6e 20 74   the record on t
1d3f1 68 65 20 74 6f 70 20 6f 66 20 74 68 65 0a 2a 2a  he top of the.**
1d3f2 20 73 74 61 63 6b 20 69 6e 74 6f 20 74 68 65 20   stack into the 
1d3f3 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  sorter..*/.stati
1d3f4 63 20 76 6f 69 64 20 70 75 73 68 4f 6e 74 6f 53  c void pushOntoS
1d3f5 6f 72 74 65 72 28 0a 20 20 50 61 72 73 65 20 2a  orter(.  Parse *
1d3f6 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
1d3f7 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
1d3f8 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
1d3f9 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 2f 2a  *pOrderBy,    /*
1d3fa 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
1d3fb 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ause */.  Select
1d3fc 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20   *pSelect       
1d3fd 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 53 45   /* The whole SE
1d3fe 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
1d3ff 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
1d400 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
1d401 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
1d402 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65  eExprList(pParse
1d403 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 73  , pOrderBy);.  s
1d404 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1d405 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63 65 2c 20  v, OP_Sequence, 
1d406 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
1d407 6f 72 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  or, 0);.  sqlite
1d408 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1d409 5f 50 75 6c 6c 2c 20 70 4f 72 64 65 72 42 79 2d  _Pull, pOrderBy-
1d40a 3e 6e 45 78 70 72 20 2b 20 31 2c 20 30 29 3b 0a  >nExpr + 1, 0);.
1d40b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d40c 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  Op(v, OP_MakeRec
1d40d 6f 72 64 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e  ord, pOrderBy->n
1d40e 45 78 70 72 20 2b 20 32 2c 20 30 29 3b 0a 20 20  Expr + 2, 0);.  
1d40f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d410 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
1d411 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  , pOrderBy->iECu
1d412 72 73 6f 72 2c 20 30 29 3b 0a 20 20 69 66 28 20  rsor, 0);.  if( 
1d413 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 3e  pSelect->iLimit>
1d414 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 64  =0 ){.    int ad
1d415 64 72 31 2c 20 61 64 64 72 32 3b 0a 20 20 20 20  dr1, addr2;.    
1d416 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56  addr1 = sqlite3V
1d417 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
1d418 66 4d 65 6d 5a 65 72 6f 2c 20 70 53 65 6c 65 63  fMemZero, pSelec
1d419 74 2d 3e 69 4c 69 6d 69 74 2b 31 2c 20 30 29 3b  t->iLimit+1, 0);
1d41a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1d41b 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49  AddOp(v, OP_MemI
1d41c 6e 63 72 2c 20 2d 31 2c 20 70 53 65 6c 65 63 74  ncr, -1, pSelect
1d41d 2d 3e 69 4c 69 6d 69 74 2b 31 29 3b 0a 20 20 20  ->iLimit+1);.   
1d41e 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33   addr2 = sqlite3
1d41f 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1d420 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Goto, 0, 0);.   
1d421 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1d422 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a  Here(v, addr1);.
1d423 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1d424 64 64 4f 70 28 76 2c 20 4f 50 5f 4c 61 73 74 2c  ddOp(v, OP_Last,
1d425 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72   pOrderBy->iECur
1d426 73 6f 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  sor, 0);.    sql
1d427 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1d428 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70 4f 72 64   OP_Delete, pOrd
1d429 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20  erBy->iECursor, 
1d42a 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1d42b 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
1d42c 64 64 72 32 29 3b 0a 20 20 20 20 70 53 65 6c 65  ddr2);.    pSele
1d42d 63 74 2d 3e 69 4c 69 6d 69 74 20 3d 20 2d 31 3b  ct->iLimit = -1;
1d42e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
1d42f 64 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d  d code to implem
1d430 65 6e 74 20 74 68 65 20 4f 46 46 53 45 54 0a 2a  ent the OFFSET.*
1d431 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
1d432 64 65 4f 66 66 73 65 74 28 0a 20 20 56 64 62 65  deOffset(.  Vdbe
1d433 20 2a 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a   *v,          /*
1d434 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69   Generate code i
1d435 6e 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20  nto this VM */. 
1d436 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
1d437 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54     /* The SELECT
1d438 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
1d439 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
1d43a 69 43 6f 6e 74 69 6e 75 65 2c 20 20 20 20 2f 2a  iContinue,    /*
1d43b 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b   Jump here to sk
1d43c 69 70 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  ip the current r
1d43d 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
1d43e 50 6f 70 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Pop          /* 
1d43f 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  Number of times 
1d440 74 6f 20 70 6f 70 20 73 74 61 63 6b 20 77 68 65  to pop stack whe
1d441 6e 20 6a 75 6d 70 69 6e 67 20 2a 2f 0a 29 7b 0a  n jumping */.){.
1d442 20 20 69 66 28 20 70 2d 3e 69 4f 66 66 73 65 74    if( p->iOffset
1d443 3e 3d 30 20 26 26 20 69 43 6f 6e 74 69 6e 75 65  >=0 && iContinue
1d444 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 61  !=0 ){.    int a
1d445 64 64 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ddr;.    sqlite3
1d446 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1d447 4d 65 6d 49 6e 63 72 2c 20 2d 31 2c 20 70 2d 3e  MemIncr, -1, p->
1d448 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 61 64  iOffset);.    ad
1d449 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
1d44a 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66 4d 65  AddOp(v, OP_IfMe
1d44b 6d 4e 65 67 2c 20 70 2d 3e 69 4f 66 66 73 65 74  mNeg, p->iOffset
1d44c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6e 50  , 0);.    if( nP
1d44d 6f 70 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  op>0 ){.      sq
1d44e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1d44f 2c 20 4f 50 5f 50 6f 70 2c 20 6e 50 6f 70 2c 20  , OP_Pop, nPop, 
1d450 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  0);.    }.    sq
1d451 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1d452 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43  , OP_Goto, 0, iC
1d453 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 56 64  ontinue);.    Vd
1d454 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23  beComment((v, "#
1d455 20 73 6b 69 70 20 4f 46 46 53 45 54 20 72 65 63   skip OFFSET rec
1d456 6f 72 64 73 22 29 29 3b 0a 20 20 20 20 73 71 6c  ords"));.    sql
1d457 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1d458 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d  (v, addr);.  }.}
1d459 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65  ../*.** Add code
1d45a 20 74 68 61 74 20 77 69 6c 6c 20 63 68 65 63 6b   that will check
1d45b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
1d45c 65 20 74 6f 70 20 4e 20 65 6c 65 6d 65 6e 74 73  e top N elements
1d45d 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b   of the.** stack
1d45e 20 61 72 65 20 64 69 73 74 69 6e 63 74 2e 20 20   are distinct.  
1d45f 69 54 61 62 20 69 73 20 61 20 73 6f 72 74 69 6e  iTab is a sortin
1d460 67 20 69 6e 64 65 78 20 74 68 61 74 20 68 6f 6c  g index that hol
1d461 64 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a  ds previously.**
1d462 20 73 65 65 6e 20 63 6f 6d 62 69 6e 61 74 69 6f   seen combinatio
1d463 6e 73 20 6f 66 20 74 68 65 20 4e 20 76 61 6c 75  ns of the N valu
1d464 65 73 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79  es.  A new entry
1d465 20 69 73 20 6d 61 64 65 20 69 6e 20 69 54 61 62   is made in iTab
1d466 0a 2a 2a 20 69 66 20 74 68 65 20 63 75 72 72 65  .** if the curre
1d467 6e 74 20 4e 20 76 61 6c 75 65 73 20 61 72 65 20  nt N values are 
1d468 6e 65 77 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d  new..**.** A jum
1d469 70 20 74 6f 20 61 64 64 72 52 65 70 65 61 74 20  p to addrRepeat 
1d46a 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20  is made and the 
1d46b 4e 2b 31 20 76 61 6c 75 65 73 20 61 72 65 20 70  N+1 values are p
1d46c 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a  opped from the.*
1d46d 2a 20 73 74 61 63 6b 20 69 66 20 74 68 65 20 74  * stack if the t
1d46e 6f 70 20 4e 20 65 6c 65 6d 65 6e 74 73 20 61 72  op N elements ar
1d46f 65 20 6e 6f 74 20 64 69 73 74 69 6e 63 74 2e 0a  e not distinct..
1d470 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
1d471 6f 64 65 44 69 73 74 69 6e 63 74 28 0a 20 20 56  odeDistinct(.  V
1d472 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20  dbe *v,         
1d473 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
1d474 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 4d 20  de into this VM 
1d475 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20  */.  int iTab,  
1d476 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 6f 72          /* A sor
1d477 74 69 6e 67 20 69 6e 64 65 78 20 75 73 65 64 20  ting index used 
1d478 74 6f 20 74 65 73 74 20 66 6f 72 20 64 69 73 74  to test for dist
1d479 69 6e 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69 6e  inctness */.  in
1d47a 74 20 61 64 64 72 52 65 70 65 61 74 2c 20 20 20  t addrRepeat,   
1d47b 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65   /* Jump to here
1d47c 20 69 66 20 6e 6f 74 20 64 69 73 74 69 6e 63 74   if not distinct
1d47d 20 2a 2f 0a 20 20 69 6e 74 20 4e 20 20 20 20 20   */.  int N     
1d47e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d47f 74 6f 70 20 4e 20 65 6c 65 6d 65 6e 74 73 20 6f  top N elements o
1d480 66 20 74 68 65 20 73 74 61 63 6b 20 6d 75 73 74  f the stack must
1d481 20 62 65 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a   be distinct */.
1d482 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ){.  sqlite3Vdbe
1d483 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65  AddOp(v, OP_Make
1d484 52 65 63 6f 72 64 2c 20 2d 4e 2c 20 30 29 3b 0a  Record, -N, 0);.
1d485 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d486 4f 70 28 76 2c 20 4f 50 5f 44 69 73 74 69 6e 63  Op(v, OP_Distinc
1d487 74 2c 20 69 54 61 62 2c 20 73 71 6c 69 74 65 33  t, iTab, sqlite3
1d488 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
1d489 76 29 2b 33 29 3b 0a 20 20 73 71 6c 69 74 65 33  v)+3);.  sqlite3
1d48a 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1d48b 50 6f 70 2c 20 4e 2b 31 2c 20 30 29 3b 0a 20 20  Pop, N+1, 0);.  
1d48c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d48d 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
1d48e 61 64 64 72 52 65 70 65 61 74 29 3b 0a 20 20 56  addrRepeat);.  V
1d48f 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
1d490 23 20 73 6b 69 70 20 69 6e 64 69 73 74 69 6e 63  # skip indistinc
1d491 74 20 72 65 63 6f 72 64 73 22 29 29 3b 0a 20 20  t records"));.  
1d492 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d493 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
1d494 2c 20 69 54 61 62 2c 20 30 29 3b 0a 7d 0a 0a 2f  , iTab, 0);.}../
1d495 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
1d496 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
1d497 68 65 6e 20 61 20 53 45 4c 45 43 54 20 69 73 20  hen a SELECT is 
1d498 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 73 75  used within a su
1d499 62 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 28  bexpression.** (
1d49a 65 78 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e 20  example:  "a IN 
1d49b 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74  (SELECT * FROM t
1d49c 61 62 6c 65 29 22 29 20 62 75 74 20 69 74 20 68  able)") but it h
1d49d 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 31 20 72  as more than 1 r
1d49e 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e  esult.** column.
1d49f 20 20 57 65 20 64 6f 20 74 68 69 73 20 69 6e 20    We do this in 
1d4a0 61 20 73 75 62 72 6f 75 74 69 6e 65 20 62 65 63  a subroutine bec
1d4a1 61 75 73 65 20 74 68 65 20 65 72 72 6f 72 20 6f  ause the error o
1d4a2 63 63 75 72 73 20 69 6e 20 6d 75 6c 74 69 70 6c  ccurs in multipl
1d4a3 65 0a 2a 2a 20 70 6c 61 63 65 73 2e 0a 2a 2f 0a  e.** places..*/.
1d4a4 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
1d4a5 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65  ForMultiColumnSe
1d4a6 6c 65 63 74 45 72 72 6f 72 28 50 61 72 73 65 20  lectError(Parse 
1d4a7 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 65 44 65  *pParse, int eDe
1d4a8 73 74 2c 20 69 6e 74 20 6e 45 78 70 72 29 7b 0a  st, int nExpr){.
1d4a9 20 20 69 66 28 20 6e 45 78 70 72 3e 31 20 26 26    if( nExpr>1 &&
1d4aa 20 28 65 44 65 73 74 3d 3d 53 52 54 5f 4d 65 6d   (eDest==SRT_Mem
1d4ab 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f 53   || eDest==SRT_S
1d4ac 65 74 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  et) ){.    sqlit
1d4ad 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1d4ae 65 2c 20 22 6f 6e 6c 79 20 61 20 73 69 6e 67 6c  e, "only a singl
1d4af 65 20 72 65 73 75 6c 74 20 61 6c 6c 6f 77 65 64  e result allowed
1d4b0 20 66 6f 72 20 22 0a 20 20 20 20 20 20 20 22 61   for ".       "a
1d4b1 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 73 20   SELECT that is 
1d4b2 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65  part of an expre
1d4b3 73 73 69 6f 6e 22 29 3b 0a 20 20 20 20 72 65 74  ssion");.    ret
1d4b4 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  urn 1;.  }else{.
1d4b5 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1d4b6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
1d4b7 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65  routine generate
1d4b8 73 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 74  s the code for t
1d4b9 68 65 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65  he inside of the
1d4ba 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20 6f   inner loop.** o
1d4bb 66 20 61 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a  f a SELECT..**.*
1d4bc 2a 20 49 66 20 73 72 63 54 61 62 20 61 6e 64 20  * If srcTab and 
1d4bd 6e 43 6f 6c 75 6d 6e 20 61 72 65 20 62 6f 74 68  nColumn are both
1d4be 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
1d4bf 70 45 4c 69 73 74 20 65 78 70 72 65 73 73 69 6f  pEList expressio
1d4c0 6e 73 0a 2a 2a 20 61 72 65 20 65 76 61 6c 75 61  ns.** are evalua
1d4c1 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ted in order to 
1d4c2 67 65 74 20 74 68 65 20 64 61 74 61 20 66 6f 72  get the data for
1d4c3 20 74 68 69 73 20 72 6f 77 2e 20 20 49 66 20 6e   this row.  If n
1d4c4 43 6f 6c 75 6d 6e 3e 30 0a 2a 2a 20 74 68 65 6e  Column>0.** then
1d4c5 20 64 61 74 61 20 69 73 20 70 75 6c 6c 65 64 20   data is pulled 
1d4c6 66 72 6f 6d 20 73 72 63 54 61 62 20 61 6e 64 20  from srcTab and 
1d4c7 70 45 4c 69 73 74 20 69 73 20 75 73 65 64 20 6f  pEList is used o
1d4c8 6e 6c 79 20 74 6f 20 67 65 74 20 74 68 65 0a 2a  nly to get the.*
1d4c9 2a 20 64 61 74 61 74 79 70 65 73 20 66 6f 72 20  * datatypes for 
1d4ca 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  each column..*/.
1d4cb 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63  static int selec
1d4cc 74 49 6e 6e 65 72 4c 6f 6f 70 28 0a 20 20 50 61  tInnerLoop(.  Pa
1d4cd 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1d4ce 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
1d4cf 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
1d4d0 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
1d4d1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d4d2 63 6f 6d 70 6c 65 74 65 20 73 65 6c 65 63 74 20  complete select 
1d4d3 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
1d4d4 63 6f 64 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  coded */.  ExprL
1d4d5 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20  ist *pEList,    
1d4d6 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 76 61     /* List of va
1d4d7 6c 75 65 73 20 62 65 69 6e 67 20 65 78 74 72 61  lues being extra
1d4d8 63 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 73 72  cted */.  int sr
1d4d9 63 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  cTab,           
1d4da 20 20 2f 2a 20 50 75 6c 6c 20 64 61 74 61 20 66    /* Pull data f
1d4db 72 6f 6d 20 74 68 69 73 20 74 61 62 6c 65 20 2a  rom this table *
1d4dc 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c  /.  int nColumn,
1d4dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1d4de 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1d4df 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74   in the source t
1d4e0 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
1d4e1 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
1d4e2 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c    /* If not NULL
1d4e3 2c 20 73 6f 72 74 20 72 65 73 75 6c 74 73 20 75  , sort results u
1d4e4 73 69 6e 67 20 74 68 69 73 20 6b 65 79 20 2a 2f  sing this key */
1d4e5 0a 20 20 69 6e 74 20 64 69 73 74 69 6e 63 74 2c  .  int distinct,
1d4e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
1d4e7 20 3e 3d 30 2c 20 6d 61 6b 65 20 73 75 72 65 20   >=0, make sure 
1d4e8 72 65 73 75 6c 74 73 20 61 72 65 20 64 69 73 74  results are dist
1d4e9 69 6e 63 74 20 2a 2f 0a 20 20 69 6e 74 20 65 44  inct */.  int eD
1d4ea 65 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  est,            
1d4eb 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70    /* How to disp
1d4ec 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ose of the resul
1d4ed 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72  ts */.  int iPar
1d4ee 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
1d4ef 2f 2a 20 41 6e 20 61 72 67 75 6d 65 6e 74 20 74  /* An argument t
1d4f0 6f 20 74 68 65 20 64 69 73 70 6f 73 61 6c 20 6d  o the disposal m
1d4f1 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69  ethod */.  int i
1d4f2 43 6f 6e 74 69 6e 75 65 2c 20 20 20 20 20 20 20  Continue,       
1d4f3 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
1d4f4 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68  to continue with
1d4f5 20 6e 65 78 74 20 72 6f 77 20 2a 2f 0a 20 20 69   next row */.  i
1d4f6 6e 74 20 69 42 72 65 61 6b 2c 20 20 20 20 20 20  nt iBreak,      
1d4f7 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
1d4f8 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  ere to break out
1d4f9 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f   of the inner lo
1d4fa 6f 70 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 66  op */.  char *af
1d4fb 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f               
1d4fc 2f 2a 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  /* affinity stri
1d4fd 6e 67 20 69 66 20 65 44 65 73 74 20 69 73 20 53  ng if eDest is S
1d4fe 52 54 5f 55 6e 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  RT_Union */.){. 
1d4ff 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
1d500 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
1d501 69 3b 0a 20 20 69 6e 74 20 68 61 73 44 69 73 74  i;.  int hasDist
1d502 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  inct;        /* 
1d503 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53 54  True if the DIST
1d504 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20  INCT keyword is 
1d505 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 69 66  present */..  if
1d506 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( v==0 ) return 
1d507 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c  0;.  assert( pEL
1d508 69 73 74 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  ist!=0 );..  /* 
1d509 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20 4c  If there was a L
1d50a 49 4d 49 54 20 63 6c 61 75 73 65 20 6f 6e 20 74  IMIT clause on t
1d50b 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
1d50c 65 6e 74 2c 20 74 68 65 6e 20 64 6f 20 74 68 65  ent, then do the
1d50d 20 63 68 65 63 6b 0a 20 20 2a 2a 20 74 6f 20 73   check.  ** to s
1d50e 65 65 20 69 66 20 74 68 69 73 20 72 6f 77 20 73  ee if this row s
1d50f 68 6f 75 6c 64 20 62 65 20 6f 75 74 70 75 74 2e  hould be output.
1d510 0a 20 20 2a 2f 0a 20 20 68 61 73 44 69 73 74 69  .  */.  hasDisti
1d511 6e 63 74 20 3d 20 64 69 73 74 69 6e 63 74 3e 3d  nct = distinct>=
1d512 30 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78  0 && pEList->nEx
1d513 70 72 3e 30 3b 0a 20 20 69 66 28 20 70 4f 72 64  pr>0;.  if( pOrd
1d514 65 72 42 79 3d 3d 30 20 26 26 20 21 68 61 73 44  erBy==0 && !hasD
1d515 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 63  istinct ){.    c
1d516 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20  odeOffset(v, p, 
1d517 69 43 6f 6e 74 69 6e 75 65 2c 20 30 29 3b 0a 20  iContinue, 0);. 
1d518 20 7d 0a 0a 20 20 2f 2a 20 50 75 6c 6c 20 74 68   }..  /* Pull th
1d519 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 75  e requested colu
1d51a 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  mns..  */.  if( 
1d51b 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20  nColumn>0 ){.   
1d51c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
1d51d 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
1d51e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d51f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  p(v, OP_Column, 
1d520 73 72 63 54 61 62 2c 20 69 29 3b 0a 20 20 20 20  srcTab, i);.    
1d521 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  }.  }else{.    n
1d522 43 6f 6c 75 6d 6e 20 3d 20 70 45 4c 69 73 74 2d  Column = pEList-
1d523 3e 6e 45 78 70 72 3b 0a 20 20 20 20 73 71 6c 69  >nExpr;.    sqli
1d524 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c  te3ExprCodeExprL
1d525 69 73 74 28 70 50 61 72 73 65 2c 20 70 45 4c 69  ist(pParse, pELi
1d526 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  st);.  }..  /* I
1d527 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b  f the DISTINCT k
1d528 65 79 77 6f 72 64 20 77 61 73 20 70 72 65 73 65  eyword was prese
1d529 6e 74 20 6f 6e 20 74 68 65 20 53 45 4c 45 43 54  nt on the SELECT
1d52a 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20   statement.  ** 
1d52b 61 6e 64 20 74 68 69 73 20 72 6f 77 20 68 61 73  and this row has
1d52c 20 62 65 65 6e 20 73 65 65 6e 20 62 65 66 6f 72   been seen befor
1d52d 65 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6d  e, then do not m
1d52e 61 6b 65 20 74 68 69 73 20 72 6f 77 0a 20 20 2a  ake this row.  *
1d52f 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 72 65  * part of the re
1d530 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  sult..  */.  if(
1d531 20 68 61 73 44 69 73 74 69 6e 63 74 20 29 7b 0a   hasDistinct ){.
1d532 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69      assert( pELi
1d533 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  st!=0 );.    ass
1d534 65 72 74 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78  ert( pEList->nEx
1d535 70 72 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20  pr==nColumn );. 
1d536 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28     codeDistinct(
1d537 76 2c 20 64 69 73 74 69 6e 63 74 2c 20 69 43 6f  v, distinct, iCo
1d538 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d 6e 29  ntinue, nColumn)
1d539 3b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65 72  ;.    if( pOrder
1d53a 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63  By==0 ){.      c
1d53b 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20  odeOffset(v, p, 
1d53c 69 43 6f 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75  iContinue, nColu
1d53d 6d 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  mn);.    }.  }..
1d53e 20 20 69 66 28 20 63 68 65 63 6b 46 6f 72 4d 75    if( checkForMu
1d53f 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45  ltiColumnSelectE
1d540 72 72 6f 72 28 70 50 61 72 73 65 2c 20 65 44 65  rror(pParse, eDe
1d541 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  st, pEList->nExp
1d542 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  r) ){.    return
1d543 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63   0;.  }..  switc
1d544 68 28 20 65 44 65 73 74 20 29 7b 0a 20 20 20 20  h( eDest ){.    
1d545 2f 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c  /* In this mode,
1d546 20 77 72 69 74 65 20 65 61 63 68 20 71 75 65 72   write each quer
1d547 79 20 72 65 73 75 6c 74 20 74 6f 20 74 68 65 20  y result to the 
1d548 6b 65 79 20 6f 66 20 74 68 65 20 74 65 6d 70 6f  key of the tempo
1d549 72 61 72 79 0a 20 20 20 20 2a 2a 20 74 61 62 6c  rary.    ** tabl
1d54a 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a  e iParm..    */.
1d54b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1d54c 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
1d54d 45 43 54 0a 20 20 20 20 63 61 73 65 20 53 52 54  ECT.    case SRT
1d54e 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20 20 20 20 20  _Union: {.      
1d54f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d550 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
1d551 64 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a  d, nColumn, 0);.
1d552 20 20 20 20 20 20 69 66 28 20 61 66 66 20 29 7b        if( aff ){
1d553 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1d554 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20  VdbeChangeP3(v, 
1d555 2d 31 2c 20 61 66 66 2c 20 50 33 5f 53 54 41 54  -1, aff, P3_STAT
1d556 49 43 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  IC);.      }.   
1d557 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1d558 64 4f 70 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  dOp(v, OP_IdxIns
1d559 65 72 74 2c 20 69 50 61 72 6d 2c 20 30 29 3b 0a  ert, iParm, 0);.
1d55a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d55b 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 74   }..    /* Const
1d55c 72 75 63 74 20 61 20 72 65 63 6f 72 64 20 66 72  ruct a record fr
1d55d 6f 6d 20 74 68 65 20 71 75 65 72 79 20 72 65 73  om the query res
1d55e 75 6c 74 2c 20 62 75 74 20 69 6e 73 74 65 61 64  ult, but instead
1d55f 20 6f 66 0a 20 20 20 20 2a 2a 20 73 61 76 69 6e   of.    ** savin
1d560 67 20 74 68 61 74 20 72 65 63 6f 72 64 2c 20 75  g that record, u
1d561 73 65 20 69 74 20 61 73 20 61 20 6b 65 79 20 74  se it as a key t
1d562 6f 20 64 65 6c 65 74 65 20 65 6c 65 6d 65 6e 74  o delete element
1d563 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 68  s from.    ** th
1d564 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
1d565 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a  e iParm..    */.
1d566 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 63      case SRT_Exc
1d567 65 70 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ept: {.      int
1d568 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61 64 64   addr;.      add
1d569 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
1d56a 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  ddOp(v, OP_MakeR
1d56b 65 63 6f 72 64 2c 20 6e 43 6f 6c 75 6d 6e 2c 20  ecord, nColumn, 
1d56c 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
1d56d 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
1d56e 20 2d 31 2c 20 61 66 66 2c 20 50 33 5f 53 54 41   -1, aff, P3_STA
1d56f 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
1d570 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1d571 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 69 50 61  OP_NotFound, iPa
1d572 72 6d 2c 20 61 64 64 72 2b 33 29 3b 0a 20 20 20  rm, addr+3);.   
1d573 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1d574 64 4f 70 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65  dOp(v, OP_Delete
1d575 2c 20 69 50 61 72 6d 2c 20 30 29 3b 0a 20 20 20  , iParm, 0);.   
1d576 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1d577 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53  #endif..    /* S
1d578 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1d579 61 73 20 64 61 74 61 20 75 73 69 6e 67 20 61 20  as data using a 
1d57a 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20  unique key..    
1d57b 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
1d57c 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
1d57d 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
1d57e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d57f 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b  eAddOp(v, OP_Mak
1d580 65 52 65 63 6f 72 64 2c 20 6e 43 6f 6c 75 6d 6e  eRecord, nColumn
1d581 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
1d582 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
1d583 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74      pushOntoSort
1d584 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  er(pParse, pOrde
1d585 72 42 79 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d  rBy, p);.      }
1d586 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
1d587 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1d588 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69  , OP_NewRowid, i
1d589 50 61 72 6d 2c 20 30 29 3b 0a 20 20 20 20 20 20  Parm, 0);.      
1d58a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d58b 4f 70 28 76 2c 20 4f 50 5f 50 75 6c 6c 2c 20 31  Op(v, OP_Pull, 1
1d58c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
1d58d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1d58e 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61  , OP_Insert, iPa
1d58f 72 6d 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e  rm, OPFLAG_APPEN
1d590 44 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  D);.      }.    
1d591 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
1d592 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1d593 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20  MIT_SUBQUERY.   
1d594 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 72   /* If we are cr
1d595 65 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f 72  eating a set for
1d596 20 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53 45   an "expr IN (SE
1d597 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74  LECT ...)" const
1d598 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65  ruct,.    ** the
1d599 6e 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20 62  n there should b
1d59a 65 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20  e a single item 
1d59b 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 57  on the stack.  W
1d59c 72 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a 2a  rite this.    **
1d59d 20 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 73   item into the s
1d59e 65 74 20 74 61 62 6c 65 20 77 69 74 68 20 62 6f  et table with bo
1d59f 67 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f  gus data..    */
1d5a0 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 65  .    case SRT_Se
1d5a1 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61  t: {.      int a
1d5a2 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
1d5a3 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
1d5a4 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  ;.      int addr
1d5a5 32 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  2;..      assert
1d5a6 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
1d5a7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d5a8 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74  eAddOp(v, OP_Not
1d5a9 4e 75 6c 6c 2c 20 2d 31 2c 20 61 64 64 72 31 2b  Null, -1, addr1+
1d5aa 33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  3);.      sqlite
1d5ab 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1d5ac 5f 50 6f 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20  _Pop, 1, 0);.   
1d5ad 20 20 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74     addr2 = sqlit
1d5ae 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1d5af 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20  P_Goto, 0, 0);. 
1d5b0 20 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79       p->affinity
1d5b1 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72   = sqlite3Compar
1d5b2 65 41 66 66 69 6e 69 74 79 28 70 45 4c 69 73 74  eAffinity(pEList
1d5b3 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 28 69 50  ->a[0].pExpr,(iP
1d5b4 61 72 6d 3e 3e 31 36 29 26 30 78 66 66 29 3b 0a  arm>>16)&0xff);.
1d5b5 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72        if( pOrder
1d5b6 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  By ){.        /*
1d5b7 20 41 74 20 66 69 72 73 74 20 67 6c 61 6e 63 65   At first glance
1d5b8 20 79 6f 75 20 77 6f 75 6c 64 20 74 68 69 6e 6b   you would think
1d5b9 20 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69   we could optimi
1d5ba 7a 65 20 6f 75 74 20 74 68 65 0a 20 20 20 20 20  ze out the.     
1d5bb 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20 69     ** ORDER BY i
1d5bc 6e 20 74 68 69 73 20 63 61 73 65 20 73 69 6e 63  n this case sinc
1d5bd 65 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 65  e the order of e
1d5be 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 73 65  ntries in the se
1d5bf 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 6f 65  t.        ** doe
1d5c0 73 20 6e 6f 74 20 6d 61 74 74 65 72 2e 20 20 42  s not matter.  B
1d5c1 75 74 20 74 68 65 72 65 20 6d 69 67 68 74 20 62  ut there might b
1d5c2 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  e a LIMIT clause
1d5c3 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 20 20 20  , in which.     
1d5c4 20 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 6f     ** case the o
1d5c5 72 64 65 72 20 64 6f 65 73 20 6d 61 74 74 65 72  rder does matter
1d5c6 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 73 68   */.        push
1d5c7 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73  OntoSorter(pPars
1d5c8 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 29 3b  e, pOrderBy, p);
1d5c9 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1d5ca 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d5cb 65 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  eOp3(v, OP_MakeR
1d5cc 65 63 6f 72 64 2c 20 31 2c 20 30 2c 20 26 70 2d  ecord, 1, 0, &p-
1d5cd 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20  >affinity, 1);. 
1d5ce 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1d5cf 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 64  beAddOp(v, OP_Id
1d5d0 78 49 6e 73 65 72 74 2c 20 28 69 50 61 72 6d 26  xInsert, (iParm&
1d5d1 30 78 30 30 30 30 46 46 46 46 29 2c 20 30 29 3b  0x0000FFFF), 0);
1d5d2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1d5d3 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
1d5d4 72 65 28 76 2c 20 61 64 64 72 32 29 3b 0a 20 20  re(v, addr2);.  
1d5d5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1d5d6 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79 20  ..    /* If any 
1d5d7 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68 65  row exist in the
1d5d8 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65 63   result set, rec
1d5d9 6f 72 64 20 74 68 61 74 20 66 61 63 74 20 61 6e  ord that fact an
1d5da 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a  d abort..    */.
1d5db 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 69      case SRT_Exi
1d5dc 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  sts: {.      sql
1d5dd 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1d5de 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 31 2c 20 69   OP_MemInt, 1, i
1d5df 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  Parm);.      sql
1d5e0 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1d5e1 20 4f 50 5f 50 6f 70 2c 20 6e 43 6f 6c 75 6d 6e   OP_Pop, nColumn
1d5e2 2c 20 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  , 0);.      /* T
1d5e3 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
1d5e4 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74  will terminate t
1d5e5 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a  he loop for us *
1d5e6 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
1d5e7 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
1d5e8 74 68 69 73 20 69 73 20 61 20 73 63 61 6c 61 72  this is a scalar
1d5e9 20 73 65 6c 65 63 74 20 74 68 61 74 20 69 73 20   select that is 
1d5ea 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65  part of an expre
1d5eb 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20  ssion, then.    
1d5ec 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  ** store the res
1d5ed 75 6c 74 73 20 69 6e 20 74 68 65 20 61 70 70 72  ults in the appr
1d5ee 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72 79 20 63  opriate memory c
1d5ef 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b 20 6f 75  ell and break ou
1d5f0 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  t.    ** of the 
1d5f1 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a  scan loop..    *
1d5f2 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d  /.    case SRT_M
1d5f3 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  em: {.      asse
1d5f4 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29  rt( nColumn==1 )
1d5f5 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64  ;.      if( pOrd
1d5f6 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20  erBy ){.        
1d5f7 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70  pushOntoSorter(p
1d5f8 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c  Parse, pOrderBy,
1d5f9 20 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   p);.      }else
1d5fa 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1d5fb 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1d5fc 5f 4d 65 6d 53 74 6f 72 65 2c 20 69 50 61 72 6d  _MemStore, iParm
1d5fd 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a  , 1);.        /*
1d5fe 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73   The LIMIT claus
1d5ff 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20  e will jump out 
1d600 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20  of the loop for 
1d601 75 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  us */.      }.  
1d602 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1d603 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64  .#endif /* #ifnd
1d604 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
1d605 55 42 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20  UBQUERY */..    
1d606 2f 2a 20 53 65 6e 64 20 74 68 65 20 64 61 74 61  /* Send the data
1d607 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
1d608 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 74 6f 20   function or to 
1d609 61 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 49  a subroutine.  I
1d60a 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 73  n the.    ** cas
1d60b 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e  e of a subroutin
1d60c 65 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  e, the subroutin
1d60d 65 20 69 74 73 65 6c 66 20 69 73 20 72 65 73 70  e itself is resp
1d60e 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 20 20 20 20  onsible for.    
1d60f 2a 2a 20 70 6f 70 70 69 6e 67 20 74 68 65 20 64  ** popping the d
1d610 61 74 61 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ata from the sta
1d611 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ck..    */.    c
1d612 61 73 65 20 53 52 54 5f 53 75 62 72 6f 75 74 69  ase SRT_Subrouti
1d613 6e 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54  ne:.    case SRT
1d614 5f 43 61 6c 6c 62 61 63 6b 3a 20 7b 0a 20 20 20  _Callback: {.   
1d615 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
1d616 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1d617 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1d618 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6e 43  P_MakeRecord, nC
1d619 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20 20 20 20  olumn, 0);.     
1d61a 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65     pushOntoSorte
1d61b 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  r(pParse, pOrder
1d61c 42 79 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 65  By, p);.      }e
1d61d 6c 73 65 20 69 66 28 20 65 44 65 73 74 3d 3d 53  lse if( eDest==S
1d61e 52 54 5f 53 75 62 72 6f 75 74 69 6e 65 20 29 7b  RT_Subroutine ){
1d61f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1d620 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1d621 47 6f 73 75 62 2c 20 30 2c 20 69 50 61 72 6d 29  Gosub, 0, iParm)
1d622 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1d623 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1d624 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61  beAddOp(v, OP_Ca
1d625 6c 6c 62 61 63 6b 2c 20 6e 43 6f 6c 75 6d 6e 2c  llback, nColumn,
1d626 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1d627 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1d628 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1d629 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1d62a 52 29 0a 20 20 20 20 2f 2a 20 44 69 73 63 61 72  R).    /* Discar
1d62b 64 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20  d the results.  
1d62c 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72  This is used for
1d62d 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1d62e 74 73 20 69 6e 73 69 64 65 0a 20 20 20 20 2a 2a  ts inside.    **
1d62f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 54   the body of a T
1d630 52 49 47 47 45 52 2e 20 20 54 68 65 20 70 75 72  RIGGER.  The pur
1d631 70 6f 73 65 20 6f 66 20 73 75 63 68 20 73 65 6c  pose of such sel
1d632 65 63 74 73 20 69 73 20 74 6f 20 63 61 6c 6c 0a  ects is to call.
1d633 20 20 20 20 2a 2a 20 75 73 65 72 2d 64 65 66 69      ** user-defi
1d634 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  ned functions th
1d635 61 74 20 68 61 76 65 20 73 69 64 65 20 65 66 66  at have side eff
1d636 65 63 74 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74  ects.  We do not
1d637 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 61 62 6f   care.    ** abo
1d638 75 74 20 74 68 65 20 61 63 74 75 61 6c 20 72 65  ut the actual re
1d639 73 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c  sults of the sel
1d63a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ect..    */.    
1d63b 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
1d63c 20 61 73 73 65 72 74 28 20 65 44 65 73 74 3d 3d   assert( eDest==
1d63d 53 52 54 5f 44 69 73 63 61 72 64 20 29 3b 0a 20  SRT_Discard );. 
1d63e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1d63f 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c  AddOp(v, OP_Pop,
1d640 20 6e 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20   nColumn, 0);.  
1d641 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1d642 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f  .#endif.  }..  /
1d643 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e  * Jump to the en
1d644 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 66  d of the loop if
1d645 20 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65   the LIMIT is re
1d646 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  ached..  */.  if
1d647 28 20 70 2d 3e 69 4c 69 6d 69 74 3e 3d 30 20 26  ( p->iLimit>=0 &
1d648 26 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b  & pOrderBy==0 ){
1d649 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1d64a 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49  AddOp(v, OP_MemI
1d64b 6e 63 72 2c 20 2d 31 2c 20 70 2d 3e 69 4c 69 6d  ncr, -1, p->iLim
1d64c 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  it);.    sqlite3
1d64d 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1d64e 49 66 4d 65 6d 5a 65 72 6f 2c 20 70 2d 3e 69 4c  IfMemZero, p->iL
1d64f 69 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20  imit, iBreak);. 
1d650 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
1d651 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e  ../*.** Given an
1d652 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1d653 2c 20 67 65 6e 65 72 61 74 65 20 61 20 4b 65 79  , generate a Key
1d654 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  Info structure t
1d655 68 61 74 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74  hat records.** t
1d656 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
1d657 75 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20 65  uence for each e
1d658 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 61  xpression in tha
1d659 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  t expression lis
1d65a 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
1d65b 45 78 70 72 4c 69 73 74 20 69 73 20 61 6e 20 4f  ExprList is an O
1d65c 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
1d65d 20 42 59 20 63 6c 61 75 73 65 20 74 68 65 6e 20   BY clause then 
1d65e 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a  the resulting.**
1d65f 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
1d660 72 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  re is appropriat
1d661 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69  e for initializi
1d662 6e 67 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64  ng a virtual ind
1d663 65 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65  ex to.** impleme
1d664 6e 74 20 74 68 61 74 20 63 6c 61 75 73 65 2e 20  nt that clause. 
1d665 20 49 66 20 74 68 65 20 45 78 70 72 4c 69 73 74   If the ExprList
1d666 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 73   is the result s
1d667 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 0a 2a  et of a SELECT.*
1d668 2a 20 74 68 65 6e 20 74 68 65 20 4b 65 79 49 6e  * then the KeyIn
1d669 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
1d66a 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
1d66b 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20 76  initializing a v
1d66c 69 72 74 75 61 6c 0a 2a 2a 20 69 6e 64 65 78 20  irtual.** index 
1d66d 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 44  to implement a D
1d66e 49 53 54 49 4e 43 54 20 74 65 73 74 2e 0a 2a 2a  ISTINCT test..**
1d66f 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
1d670 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  d the KeyInfo st
1d671 72 75 63 74 75 72 65 20 69 73 20 6f 62 74 61 69  ructure is obtai
1d672 6e 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 20  n from malloc.  
1d673 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66  The calling.** f
1d674 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f  unction is respo
1d675 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 65 69 6e  nsible for seein
1d676 67 20 74 68 61 74 20 74 68 69 73 20 73 74 72 75  g that this stru
1d677 63 74 75 72 65 20 69 73 20 65 76 65 6e 74 75 61  cture is eventua
1d678 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 20 20 41  lly.** freed.  A
1d679 64 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  dd the KeyInfo s
1d67a 74 72 75 63 74 75 72 65 20 74 6f 20 74 68 65 20  tructure to the 
1d67b 50 33 20 66 69 65 6c 64 20 6f 66 20 61 6e 20 6f  P3 field of an o
1d67c 70 63 6f 64 65 20 75 73 69 6e 67 0a 2a 2a 20 50  pcode using.** P
1d67d 33 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  3_KEYINFO_HANDOF
1d67e 46 20 69 73 20 74 68 65 20 75 73 75 61 6c 20 77  F is the usual w
1d67f 61 79 20 6f 66 20 64 65 61 6c 69 6e 67 20 77 69  ay of dealing wi
1d680 74 68 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74  th this..*/.stat
1d681 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b 65 79 49  ic KeyInfo *keyI
1d682 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  nfoFromExprList(
1d683 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
1d684 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  xprList *pList){
1d685 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1d686 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
1d687 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b 65 79 49  nt nExpr;.  KeyI
1d688 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 73 74  nfo *pInfo;.  st
1d689 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
1d68a 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74  em *pItem;.  int
1d68b 20 69 3b 0a 0a 20 20 6e 45 78 70 72 20 3d 20 70   i;..  nExpr = p
1d68c 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70  List->nExpr;.  p
1d68d 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62  Info = sqlite3Db
1d68e 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
1d68f 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 20 2b 20  izeof(*pInfo) + 
1d690 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f  nExpr*(sizeof(Co
1d691 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20  llSeq*)+1) );.  
1d692 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20  if( pInfo ){.   
1d693 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64   pInfo->aSortOrd
1d694 65 72 20 3d 20 28 75 38 2a 29 26 70 49 6e 66 6f  er = (u8*)&pInfo
1d695 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a  ->aColl[nExpr];.
1d696 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c      pInfo->nFiel
1d697 64 20 3d 20 6e 45 78 70 72 3b 0a 20 20 20 20 70  d = nExpr;.    p
1d698 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28  Info->enc = ENC(
1d699 64 62 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  db);.    for(i=0
1d69a 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
1d69b 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  ; i<nExpr; i++, 
1d69c 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
1d69d 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
1d69e 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
1d69f 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
1d6a0 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
1d6a1 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66  pExpr);.      if
1d6a2 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
1d6a3 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e      pColl = db->
1d6a4 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20  pDfltColl;.     
1d6a5 20 7d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e   }.      pInfo->
1d6a6 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c  aColl[i] = pColl
1d6a7 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61  ;.      pInfo->a
1d6a8 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70  SortOrder[i] = p
1d6a9 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b  Item->sortOrder;
1d6aa 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1d6ab 75 72 6e 20 70 49 6e 66 6f 3b 0a 7d 0a 0a 0a 2f  urn pInfo;.}.../
1d6ac 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 6e 65  *.** If the inne
1d6ad 72 20 6c 6f 6f 70 20 77 61 73 20 67 65 6e 65 72  r loop was gener
1d6ae 61 74 65 64 20 75 73 69 6e 67 20 61 20 6e 6f 6e  ated using a non
1d6af 2d 6e 75 6c 6c 20 70 4f 72 64 65 72 42 79 20 61  -null pOrderBy a
1d6b0 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e  rgument,.** then
1d6b1 20 74 68 65 20 72 65 73 75 6c 74 73 20 77 65 72   the results wer
1d6b2 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73 6f  e placed in a so
1d6b3 72 74 65 72 2e 20 20 41 66 74 65 72 20 74 68 65  rter.  After the
1d6b4 20 6c 6f 6f 70 20 69 73 20 74 65 72 6d 69 6e 61   loop is termina
1d6b5 74 65 64 0a 2a 2a 20 77 65 20 6e 65 65 64 20 74  ted.** we need t
1d6b6 6f 20 72 75 6e 20 74 68 65 20 73 6f 72 74 65 72  o run the sorter
1d6b7 20 61 6e 64 20 6f 75 74 70 75 74 20 74 68 65 20   and output the 
1d6b8 72 65 73 75 6c 74 73 2e 20 20 54 68 65 20 66 6f  results.  The fo
1d6b9 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69  llowing.** routi
1d6ba 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  ne generates the
1d6bb 20 63 6f 64 65 20 6e 65 65 64 65 64 20 74 6f 20   code needed to 
1d6bc 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 73 74 61 74  do that..*/.stat
1d6bd 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65  ic void generate
1d6be 53 6f 72 74 54 61 69 6c 28 0a 20 20 50 61 72 73  SortTail(.  Pars
1d6bf 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20  e *pParse,   /* 
1d6c0 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
1d6c1 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
1d6c2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
1d6c3 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ECT statement */
1d6c4 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20  .  Vdbe *v,     
1d6c5 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
1d6c6 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56  code into this V
1d6c7 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  DBE */.  int nCo
1d6c8 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 4e 75 6d  lumn,     /* Num
1d6c9 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f  ber of columns o
1d6ca 66 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  f data */.  int 
1d6cb 65 44 65 73 74 2c 20 20 20 20 20 20 20 2f 2a 20  eDest,       /* 
1d6cc 57 72 69 74 65 20 74 68 65 20 73 6f 72 74 65 64  Write the sorted
1d6cd 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
1d6ce 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 20 20 20  .  int iParm    
1d6cf 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20      /* Optional 
1d6d0 70 61 72 61 6d 65 74 65 72 20 61 73 73 6f 63 69  parameter associ
1d6d1 61 74 65 64 20 77 69 74 68 20 65 44 65 73 74 20  ated with eDest 
1d6d2 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 62 72 6b 20  */.){.  int brk 
1d6d3 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
1d6d4 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69 6e 74  eLabel(v);.  int
1d6d5 20 63 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56   cont = sqlite3V
1d6d6 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1d6d7 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 69  .  int addr;.  i
1d6d8 6e 74 20 69 54 61 62 3b 0a 20 20 69 6e 74 20 70  nt iTab;.  int p
1d6d9 73 65 75 64 6f 54 61 62 20 3d 20 30 3b 0a 20 20  seudoTab = 0;.  
1d6da 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
1d6db 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79  By = p->pOrderBy
1d6dc 3b 0a 0a 20 20 69 54 61 62 20 3d 20 70 4f 72 64  ;..  iTab = pOrd
1d6dd 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 3b 0a  erBy->iECursor;.
1d6de 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54    if( eDest==SRT
1d6df 5f 43 61 6c 6c 62 61 63 6b 20 7c 7c 20 65 44 65  _Callback || eDe
1d6e0 73 74 3d 3d 53 52 54 5f 53 75 62 72 6f 75 74 69  st==SRT_Subrouti
1d6e1 6e 65 20 29 7b 0a 20 20 20 20 70 73 65 75 64 6f  ne ){.    pseudo
1d6e2 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
1d6e3 61 62 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65  ab++;.    sqlite
1d6e4 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1d6e5 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 70 73 65  _OpenPseudo, pse
1d6e6 75 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 20 20  udoTab, 0);.    
1d6e7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d6e8 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c  (v, OP_SetNumCol
1d6e9 75 6d 6e 73 2c 20 70 73 65 75 64 6f 54 61 62 2c  umns, pseudoTab,
1d6ea 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 7d 0a 20   nColumn);.  }. 
1d6eb 20 61 64 64 72 20 3d 20 31 20 2b 20 73 71 6c 69   addr = 1 + sqli
1d6ec 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1d6ed 4f 50 5f 53 6f 72 74 2c 20 69 54 61 62 2c 20 62  OP_Sort, iTab, b
1d6ee 72 6b 29 3b 0a 20 20 63 6f 64 65 4f 66 66 73 65  rk);.  codeOffse
1d6ef 74 28 76 2c 20 70 2c 20 63 6f 6e 74 2c 20 30 29  t(v, p, cont, 0)
1d6f0 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53  ;.  if( eDest==S
1d6f1 52 54 5f 43 61 6c 6c 62 61 63 6b 20 7c 7c 20 65  RT_Callback || e
1d6f2 44 65 73 74 3d 3d 53 52 54 5f 53 75 62 72 6f 75  Dest==SRT_Subrou
1d6f3 74 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  tine ){.    sqli
1d6f4 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1d6f5 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 30  OP_Integer, 1, 0
1d6f6 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1d6f7 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1d6f8 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 70 4f  Column, iTab, pO
1d6f9 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 2b 20  rderBy->nExpr + 
1d6fa 31 29 3b 0a 20 20 73 77 69 74 63 68 28 20 65 44  1);.  switch( eD
1d6fb 65 73 74 20 29 7b 0a 20 20 20 20 63 61 73 65 20  est ){.    case 
1d6fc 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63  SRT_Table:.    c
1d6fd 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62  ase SRT_EphemTab
1d6fe 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
1d6ff 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1d700 5f 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72 6d  _NewRowid, iParm
1d701 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
1d702 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1d703 4f 50 5f 50 75 6c 6c 2c 20 31 2c 20 30 29 3b 0a  OP_Pull, 1, 0);.
1d704 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d705 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 73  eAddOp(v, OP_Ins
1d706 65 72 74 2c 20 69 50 61 72 6d 2c 20 4f 50 46 4c  ert, iParm, OPFL
1d707 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20  AG_APPEND);.    
1d708 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1d709 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1d70a 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
1d70b 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a  case SRT_Set: {.
1d70c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43        assert( nC
1d70d 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20  olumn==1 );.    
1d70e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d70f 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c  Op(v, OP_NotNull
1d710 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 56 64 62  , -1, sqlite3Vdb
1d711 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b  eCurrentAddr(v)+
1d712 33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  3);.      sqlite
1d713 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1d714 5f 50 6f 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20  _Pop, 1, 0);.   
1d715 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1d716 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  dOp(v, OP_Goto, 
1d717 30 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  0, sqlite3VdbeCu
1d718 72 72 65 6e 74 41 64 64 72 28 76 29 2b 33 29 3b  rrentAddr(v)+3);
1d719 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d71a 62 65 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  beOp3(v, OP_Make
1d71b 52 65 63 6f 72 64 2c 20 31 2c 20 30 2c 20 26 70  Record, 1, 0, &p
1d71c 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
1d71d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d71e 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 64 78  eAddOp(v, OP_Idx
1d71f 49 6e 73 65 72 74 2c 20 28 69 50 61 72 6d 26 30  Insert, (iParm&0
1d720 78 30 30 30 30 46 46 46 46 29 2c 20 30 29 3b 0a  x0000FFFF), 0);.
1d721 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d722 20 7d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f   }.    case SRT_
1d723 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  Mem: {.      ass
1d724 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20  ert( nColumn==1 
1d725 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1d726 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1d727 4d 65 6d 53 74 6f 72 65 2c 20 69 50 61 72 6d 2c  MemStore, iParm,
1d728 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68   1);.      /* Th
1d729 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77  e LIMIT clause w
1d72a 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68  ill terminate th
1d72b 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f  e loop for us */
1d72c 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1d72d 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
1d72e 61 73 65 20 53 52 54 5f 43 61 6c 6c 62 61 63 6b  ase SRT_Callback
1d72f 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53  :.    case SRT_S
1d730 75 62 72 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20  ubroutine: {.   
1d731 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
1d732 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d733 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 70  (v, OP_Insert, p
1d734 73 65 75 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20  seudoTab, 0);.  
1d735 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
1d736 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
1d737 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d738 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c  eAddOp(v, OP_Col
1d739 75 6d 6e 2c 20 70 73 65 75 64 6f 54 61 62 2c 20  umn, pseudoTab, 
1d73a 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  i);.      }.    
1d73b 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54    if( eDest==SRT
1d73c 5f 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  _Callback ){.   
1d73d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1d73e 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c 6c  AddOp(v, OP_Call
1d73f 62 61 63 6b 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 30  back, nColumn, 0
1d740 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1d741 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1d742 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47  dbeAddOp(v, OP_G
1d743 6f 73 75 62 2c 20 30 2c 20 69 50 61 72 6d 29 3b  osub, 0, iParm);
1d744 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1d745 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1d746 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
1d747 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a   /* Do nothing *
1d748 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
1d749 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a     }.  }..  /* J
1d74a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ump to the end o
1d74b 66 20 74 68 65 20 6c 6f 6f 70 20 77 68 65 6e 20  f the loop when 
1d74c 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65 61  the LIMIT is rea
1d74d 63 68 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ched.  */.  if( 
1d74e 70 2d 3e 69 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a  p->iLimit>=0 ){.
1d74f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1d750 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e  ddOp(v, OP_MemIn
1d751 63 72 2c 20 2d 31 2c 20 70 2d 3e 69 4c 69 6d 69  cr, -1, p->iLimi
1d752 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
1d753 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
1d754 66 4d 65 6d 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69  fMemZero, p->iLi
1d755 6d 69 74 2c 20 62 72 6b 29 3b 0a 20 20 7d 0a 0a  mit, brk);.  }..
1d756 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20    /* The bottom 
1d757 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f  of the loop.  */
1d758 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
1d759 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 63 6f  solveLabel(v, co
1d75a 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  nt);.  sqlite3Vd
1d75b 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65  beAddOp(v, OP_Ne
1d75c 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 29 3b  xt, iTab, addr);
1d75d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
1d75e 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 62 72  solveLabel(v, br
1d75f 6b 29 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d  k);.  if( eDest=
1d760 3d 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20 7c 7c  =SRT_Callback ||
1d761 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 75 62 72   eDest==SRT_Subr
1d762 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 73 71  outine ){.    sq
1d763 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1d764 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75  , OP_Close, pseu
1d765 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a  doTab, 0);.  }..
1d766 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1d767 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
1d768 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
1d769 20 74 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f   the 'declaratio
1d76a 6e 20 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a  n type' of the.*
1d76b 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  * expression pEx
1d76c 70 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d  pr. The string m
1d76d 61 79 20 62 65 20 74 72 65 61 74 65 64 20 61 73  ay be treated as
1d76e 20 73 74 61 74 69 63 20 62 79 20 74 68 65 20 63   static by the c
1d76f 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  aller..**.** The
1d770 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
1d771 65 20 69 73 20 74 68 65 20 65 78 61 63 74 20 64  e is the exact d
1d772 61 74 61 74 79 70 65 20 64 65 66 69 6e 69 74 69  atatype definiti
1d773 6f 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f  on extracted fro
1d774 6d 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61  m the.** origina
1d775 6c 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  l CREATE TABLE s
1d776 74 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20  tatement if the 
1d777 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
1d778 63 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64  column. The.** d
1d779 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
1d77a 66 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65 6c  for a ROWID fiel
1d77b 64 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45 78  d is INTEGER. Ex
1d77c 61 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 78  actly when an ex
1d77d 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63  pression.** is c
1d77e 6f 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75  onsidered a colu
1d77f 6d 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65  mn can be comple
1d780 78 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e 63  x in the presenc
1d781 65 20 6f 66 20 73 75 62 71 75 65 72 69 65 73 2e  e of subqueries.
1d782 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73   The.** result-s
1d783 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  et expression in
1d784 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c   all of the foll
1d785 6f 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61  owing SELECT sta
1d786 74 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63  tements is .** c
1d787 6f 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75  onsidered a colu
1d788 6d 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  mn by this funct
1d789 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c  ion..**.**   SEL
1d78a 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c  ECT col FROM tbl
1d78b 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53  ;.**   SELECT (S
1d78c 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74  ELECT col FROM t
1d78d 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  bl;.**   SELECT 
1d78e 28 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d  (SELECT col FROM
1d78f 20 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45   tbl);.**   SELE
1d790 43 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45 4c  CT abc FROM (SEL
1d791 45 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20 46  ECT col AS abc F
1d792 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a  ROM tbl);.** .**
1d793 20 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e   The declaration
1d794 20 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78   type for any ex
1d795 70 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74  pression other t
1d796 68 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20  han a column is 
1d797 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  NULL..*/.static 
1d798 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75  const char *colu
1d799 6d 6e 54 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f  mnType(.  NameCo
1d79a 6e 74 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45  ntext *pNC, .  E
1d79b 78 70 72 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f  xpr *pExpr,.  co
1d79c 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69  nst char **pzOri
1d79d 67 69 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63  ginDb,.  const c
1d79e 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61  har **pzOriginTa
1d79f 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
1d7a0 2a 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b  **pzOriginCol.){
1d7a1 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
1d7a2 54 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72  Type = 0;.  char
1d7a3 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44   const *zOriginD
1d7a4 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f  b = 0;.  char co
1d7a5 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20  nst *zOriginTab 
1d7a6 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  = 0;.  char cons
1d7a7 74 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20  t *zOriginCol = 
1d7a8 30 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66  0;.  int j;.  if
1d7a9 28 20 70 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e  ( pExpr==0 || pN
1d7aa 43 2d 3e 70 53 72 63 4c 69 73 74 3d 3d 30 20 29  C->pSrcList==0 )
1d7ab 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77   return 0;..  sw
1d7ac 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
1d7ad 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
1d7ae 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63  GG_COLUMN:.    c
1d7af 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b  ase TK_COLUMN: {
1d7b0 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78  .      /* The ex
1d7b1 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f  pression is a co
1d7b2 6c 75 6d 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65  lumn. Locate the
1d7b3 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d   table the colum
1d7b4 6e 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 20  n is being.     
1d7b5 20 2a 2a 20 65 78 74 72 61 63 74 65 64 20 66 72   ** extracted fr
1d7b6 6f 6d 20 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78  om in NameContex
1d7b7 74 2e 70 53 72 63 4c 69 73 74 2e 20 54 68 69 73  t.pSrcList. This
1d7b8 20 74 61 62 6c 65 20 6d 61 79 20 62 65 20 72 65   table may be re
1d7b9 61 6c 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61  al.      ** data
1d7ba 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 20  base table or a 
1d7bb 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20 20 20  subquery..      
1d7bc 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a  */.      Table *
1d7bd 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20  pTab = 0;       
1d7be 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 73 74       /* Table st
1d7bf 72 75 63 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69  ructure column i
1d7c0 73 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d  s extracted from
1d7c1 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74   */.      Select
1d7c2 20 2a 70 53 20 3d 20 30 3b 20 20 20 20 20 20 20   *pS = 0;       
1d7c3 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20        /* Select 
1d7c4 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78  the column is ex
1d7c5 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a  tracted from */.
1d7c6 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d        int iCol =
1d7c7 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
1d7c8 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
1d7c9 6c 75 6d 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a  lumn in pTab */.
1d7ca 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 4e 43        while( pNC
1d7cb 20 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20   && !pTab ){.   
1d7cc 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 54       SrcList *pT
1d7cd 61 62 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  abList = pNC->pS
1d7ce 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20  rcList;.        
1d7cf 66 6f 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69  for(j=0;j<pTabLi
1d7d0 73 74 2d 3e 6e 53 72 63 20 26 26 20 70 54 61 62  st->nSrc && pTab
1d7d1 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73  List->a[j].iCurs
1d7d2 6f 72 21 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c  or!=pExpr->iTabl
1d7d3 65 3b 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20  e;j++);.        
1d7d4 69 66 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e  if( j<pTabList->
1d7d5 6e 53 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  nSrc ){.        
1d7d6 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73    pTab = pTabLis
1d7d7 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20  t->a[j].pTab;.  
1d7d8 20 20 20 20 20 20 20 20 70 53 20 3d 20 70 54 61          pS = pTa
1d7d9 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c  bList->a[j].pSel
1d7da 65 63 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ect;.        }el
1d7db 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e  se{.          pN
1d7dc 43 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a  C = pNC->pNext;.
1d7dd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1d7de 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  }..      if( pTa
1d7df 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  b==0 ){.        
1d7e0 2f 2a 20 46 49 58 20 4d 45 3a 0a 20 20 20 20 20  /* FIX ME:.     
1d7e1 20 20 20 2a 2a 20 54 68 69 73 20 63 61 6e 20 6f     ** This can o
1d7e2 63 63 75 72 73 20 69 66 20 79 6f 75 20 68 61 76  ccurs if you hav
1d7e3 65 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65  e something like
1d7e4 20 22 53 45 4c 45 43 54 20 6e 65 77 2e 78 3b 22   "SELECT new.x;"
1d7e5 20 69 6e 73 69 64 65 0a 20 20 20 20 20 20 20 20   inside.        
1d7e6 2a 2a 20 61 20 74 72 69 67 67 65 72 2e 20 20 49  ** a trigger.  I
1d7e7 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
1d7e8 66 20 79 6f 75 20 72 65 66 65 72 65 6e 63 65 20  f you reference 
1d7e9 74 68 65 20 73 70 65 63 69 61 6c 20 22 6e 65 77  the special "new
1d7ea 22 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62  ".        ** tab
1d7eb 6c 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  le in the result
1d7ec 20 73 65 74 20 6f 66 20 61 20 73 65 6c 65 63 74   set of a select
1d7ed 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 68 61 76  .  We do not hav
1d7ee 65 20 61 20 67 6f 6f 64 20 77 61 79 0a 20 20 20  e a good way.   
1d7ef 20 20 20 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20       ** to find 
1d7f0 74 68 65 20 61 63 74 75 61 6c 20 74 61 62 6c 65  the actual table
1d7f1 20 74 79 70 65 2c 20 73 6f 20 63 61 6c 6c 20 69   type, so call i
1d7f2 74 20 22 54 45 58 54 22 2e 20 20 54 68 69 73 20  t "TEXT".  This 
1d7f3 69 73 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20  is really.      
1d7f4 20 20 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f    ** something o
1d7f5 66 20 61 20 62 75 67 2c 20 62 75 74 20 49 20 64  f a bug, but I d
1d7f6 6f 20 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 74  o not know how t
1d7f7 6f 20 66 69 78 20 69 74 2e 0a 20 20 20 20 20 20  o fix it..      
1d7f8 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
1d7f9 54 68 69 73 20 63 6f 64 65 20 64 6f 65 73 20 6e  This code does n
1d7fa 6f 74 20 70 72 6f 64 75 63 65 20 74 68 65 20 63  ot produce the c
1d7fb 6f 72 72 65 63 74 20 61 6e 73 77 65 72 20 2d 20  orrect answer - 
1d7fc 69 74 20 6a 75 73 74 20 70 72 65 76 65 6e 74 73  it just prevents
1d7fd 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 73 65  .        ** a se
1d7fe 67 66 61 75 6c 74 2e 20 20 53 65 65 20 74 69 63  gfault.  See tic
1d7ff 6b 65 74 20 23 31 32 32 39 2e 0a 20 20 20 20 20  ket #1229..     
1d800 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 54     */.        zT
1d801 79 70 65 20 3d 20 22 54 45 58 54 22 3b 0a 20 20  ype = "TEXT";.  
1d802 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d803 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
1d804 72 74 28 20 70 54 61 62 20 29 3b 0a 20 20 20 20  rt( pTab );.    
1d805 20 20 69 66 28 20 70 53 20 29 7b 0a 20 20 20 20    if( pS ){.    
1d806 20 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c      /* The "tabl
1d807 65 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61  e" is actually a
1d808 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61   sub-select or a
1d809 20 76 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f   view in the FRO
1d80a 4d 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20  M clause.       
1d80b 20 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43   ** of the SELEC
1d80c 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74  T statement. Ret
1d80d 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74  urn the declarat
1d80e 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69  ion type and ori
1d80f 67 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 64  gin.        ** d
1d810 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 73 75  ata for the resu
1d811 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66  lt-set column of
1d812 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 2e   the sub-select.
1d813 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1d814 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20      if( iCol>=0 
1d815 26 26 20 69 43 6f 6c 3c 70 53 2d 3e 70 45 4c 69  && iCol<pS->pELi
1d816 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
1d817 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 43 6f         /* If iCo
1d818 6c 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a  l is less than z
1d819 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 65 78  ero, then the ex
1d81a 70 72 65 73 73 69 6f 6e 20 72 65 71 75 65 73 74  pression request
1d81b 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  s the.          
1d81c 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  ** rowid of the 
1d81d 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 76 69  sub-select or vi
1d81e 65 77 2e 20 54 68 69 73 20 65 78 70 72 65 73 73  ew. This express
1d81f 69 6f 6e 20 69 73 20 6c 65 67 61 6c 20 28 73 65  ion is legal (se
1d820 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  e .          ** 
1d821 74 65 73 74 20 63 61 73 65 20 6d 69 73 63 32 2e  test case misc2.
1d822 32 2e 32 29 20 2d 20 69 74 20 61 6c 77 61 79 73  2.2) - it always
1d823 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55   evaluates to NU
1d824 4c 4c 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  LL..          */
1d825 0a 20 20 20 20 20 20 20 20 20 20 4e 61 6d 65 43  .          NameC
1d826 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20  ontext sNC;.    
1d827 20 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20        Expr *p = 
1d828 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43  pS->pEList->a[iC
1d829 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  ol].pExpr;.     
1d82a 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73       sNC.pSrcLis
1d82b 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20  t = pS->pSrc;.  
1d82c 20 20 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78          sNC.pNex
1d82d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  t = 0;.         
1d82e 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e   sNC.pParse = pN
1d82f 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20  C->pParse;.     
1d830 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c       zType = col
1d831 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c  umnType(&sNC, p,
1d832 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f   &zOriginDb, &zO
1d833 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67  riginTab, &zOrig
1d834 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 20  inCol); .       
1d835 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69   }.      }else i
1d836 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  f( pTab->pSchema
1d837 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41   ){.        /* A
1d838 20 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20   real table */. 
1d839 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
1d83a 70 53 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  pS );.        if
1d83b 28 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20  ( iCol<0 ) iCol 
1d83c 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20  = pTab->iPKey;. 
1d83d 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69         assert( i
1d83e 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c  Col==-1 || (iCol
1d83f 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62  >=0 && iCol<pTab
1d840 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20  ->nCol) );.     
1d841 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
1d842 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65  .          zType
1d843 20 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20   = "INTEGER";.  
1d844 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43          zOriginC
1d845 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20  ol = "rowid";.  
1d846 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1d847 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70         zType = p
1d848 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
1d849 7a 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20  zType;.         
1d84a 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54   zOriginCol = pT
1d84b 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
1d84c 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Name;.        }.
1d84d 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54          zOriginT
1d84e 61 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ab = pTab->zName
1d84f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e  ;.        if( pN
1d850 43 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20  C->pParse ){.   
1d851 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d         int iDb =
1d852 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1d853 49 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73  Index(pNC->pPars
1d854 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
1d855 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20  hema);.         
1d856 20 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43   zOriginDb = pNC
1d857 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  ->pParse->db->aD
1d858 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
1d859 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1d85a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d85b 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1d85c 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
1d85d 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45      case TK_SELE
1d85e 43 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54  CT: {.      /* T
1d85f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1d860 20 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52   a sub-select. R
1d861 65 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72  eturn the declar
1d862 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20  ation type and. 
1d863 20 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69       ** origin i
1d864 6e 66 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67  nfo for the sing
1d865 6c 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  le column in the
1d866 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
1d867 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20  he SELECT.      
1d868 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20  ** statement..  
1d869 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d      */.      Nam
1d86a 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20  eContext sNC;.  
1d86b 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d      Select *pS =
1d86c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 3b   pExpr->pSelect;
1d86d 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d  .      Expr *p =
1d86e 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30   pS->pEList->a[0
1d86f 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73  ].pExpr;.      s
1d870 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53  NC.pSrcList = pS
1d871 2d 3e 70 53 72 63 3b 0a 20 20 20 20 20 20 73 4e  ->pSrc;.      sN
1d872 43 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20  C.pNext = pNC;. 
1d873 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20       sNC.pParse 
1d874 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20  = pNC->pParse;. 
1d875 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c       zType = col
1d876 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c  umnType(&sNC, p,
1d877 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f   &zOriginDb, &zO
1d878 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67  riginTab, &zOrig
1d879 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 62  inCol); .      b
1d87a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
1d87b 69 66 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20  if.  }.  .  if( 
1d87c 70 7a 4f 72 69 67 69 6e 44 62 20 29 7b 0a 20 20  pzOriginDb ){.  
1d87d 20 20 61 73 73 65 72 74 28 20 70 7a 4f 72 69 67    assert( pzOrig
1d87e 69 6e 54 61 62 20 26 26 20 70 7a 4f 72 69 67 69  inTab && pzOrigi
1d87f 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 2a 70 7a 4f  nCol );.    *pzO
1d880 72 69 67 69 6e 44 62 20 3d 20 7a 4f 72 69 67 69  riginDb = zOrigi
1d881 6e 44 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67  nDb;.    *pzOrig
1d882 69 6e 54 61 62 20 3d 20 7a 4f 72 69 67 69 6e 54  inTab = zOriginT
1d883 61 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69  ab;.    *pzOrigi
1d884 6e 43 6f 6c 20 3d 20 7a 4f 72 69 67 69 6e 43 6f  nCol = zOriginCo
1d885 6c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  l;.  }.  return 
1d886 7a 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zType;.}../*.** 
1d887 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
1d888 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65  at will tell the
1d889 20 56 44 42 45 20 74 68 65 20 64 65 63 6c 61 72   VDBE the declar
1d88a 61 74 69 6f 6e 20 74 79 70 65 73 20 6f 66 20 63  ation types of c
1d88b 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65  olumns.** in the
1d88c 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a   result set..*/.
1d88d 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65  static void gene
1d88e 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28  rateColumnTypes(
1d88f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1d890 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72  ,      /* Parser
1d891 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
1d892 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
1d893 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62    /* List of tab
1d894 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  les */.  ExprLis
1d895 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20  t *pEList    /* 
1d896 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65 66 69  Expressions defi
1d897 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ning the result 
1d898 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  set */.){.  Vdbe
1d899 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
1d89a 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  dbe;.  int i;.  
1d89b 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
1d89c 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20  .  sNC.pSrcList 
1d89d 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 4e  = pTabList;.  sN
1d89e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  C.pParse = pPars
1d89f 65 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  e;.  for(i=0; i<
1d8a0 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
1d8a1 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
1d8a2 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
1d8a3 70 45 78 70 72 3b 0a 20 20 20 20 63 6f 6e 73 74  pExpr;.    const
1d8a4 20 63 68 61 72 20 2a 7a 4f 72 69 67 44 62 20 3d   char *zOrigDb =
1d8a5 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   0;.    const ch
1d8a6 61 72 20 2a 7a 4f 72 69 67 54 61 62 20 3d 20 30  ar *zOrigTab = 0
1d8a7 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
1d8a8 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d 20 30 3b 0a   *zOrigCol = 0;.
1d8a9 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1d8aa 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79  zType = columnTy
1d8ab 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72  pe(&sNC, p, &zOr
1d8ac 69 67 44 62 2c 20 26 7a 4f 72 69 67 54 61 62 2c  igDb, &zOrigTab,
1d8ad 20 26 7a 4f 72 69 67 43 6f 6c 29 3b 0a 0a 20 20   &zOrigCol);..  
1d8ae 20 20 2f 2a 20 54 68 65 20 76 64 62 65 20 6d 75    /* The vdbe mu
1d8af 73 74 20 6d 61 6b 65 20 69 74 27 73 20 6f 77 6e  st make it's own
1d8b0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6c   copy of the col
1d8b1 75 6d 6e 2d 74 79 70 65 20 61 6e 64 20 6f 74 68  umn-type and oth
1d8b2 65 72 20 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  er .    ** colum
1d8b3 6e 20 73 70 65 63 69 66 69 63 20 73 74 72 69 6e  n specific strin
1d8b4 67 73 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20  gs, in case the 
1d8b5 73 63 68 65 6d 61 20 69 73 20 72 65 73 65 74 20  schema is reset 
1d8b6 62 65 66 6f 72 65 20 74 68 69 73 0a 20 20 20 20  before this.    
1d8b7 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ** virtual machi
1d8b8 6e 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 20  ne is deleted.. 
1d8b9 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
1d8ba 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
1d8bb 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45  v, i, COLNAME_DE
1d8bc 43 4c 54 59 50 45 2c 20 7a 54 79 70 65 2c 20 50  CLTYPE, zType, P
1d8bd 33 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  3_TRANSIENT);.  
1d8be 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1d8bf 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
1d8c0 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 2c 20  LNAME_DATABASE, 
1d8c1 7a 4f 72 69 67 44 62 2c 20 50 33 5f 54 52 41 4e  zOrigDb, P3_TRAN
1d8c2 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69  SIENT);.    sqli
1d8c3 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1d8c4 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, i, COLNAME_
1d8c5 54 41 42 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c  TABLE, zOrigTab,
1d8c6 20 50 33 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a   P3_TRANSIENT);.
1d8c7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
1d8c8 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
1d8c9 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 2c 20  COLNAME_COLUMN, 
1d8ca 7a 4f 72 69 67 43 6f 6c 2c 20 50 33 5f 54 52 41  zOrigCol, P3_TRA
1d8cb 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a  NSIENT);.  }.}..
1d8cc 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1d8cd 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 74 65  ode that will te
1d8ce 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68 65 20  ll the VDBE the 
1d8cf 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  names of columns
1d8d0 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c  .** in the resul
1d8d1 74 20 73 65 74 2e 20 20 54 68 69 73 20 69 6e 66  t set.  This inf
1d8d2 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
1d8d3 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 0a   to provide the.
1d8d4 2a 2a 20 61 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65  ** azCol[] value
1d8d5 73 20 69 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  s in the callbac
1d8d6 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
1d8d7 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e  d generateColumn
1d8d8 4e 61 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a  Names(.  Parse *
1d8d9 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
1d8da 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
1d8db 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
1d8dc 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20  bList,  /* List 
1d8dd 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 45  of tables */.  E
1d8de 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
1d8df 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
1d8e0 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 72  s defining the r
1d8e1 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29 7b 0a  esult set */.){.
1d8e2 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
1d8e3 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
1d8e4 20 69 2c 20 6a 3b 0a 20 20 73 71 6c 69 74 65 33   i, j;.  sqlite3
1d8e5 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
1d8e6 62 3b 0a 20 20 69 6e 74 20 66 75 6c 6c 4e 61 6d  b;.  int fullNam
1d8e7 65 73 2c 20 73 68 6f 72 74 4e 61 6d 65 73 3b 0a  es, shortNames;.
1d8e8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d8e9 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 2f  OMIT_EXPLAIN.  /
1d8ea 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
1d8eb 45 58 50 4c 41 49 4e 2c 20 73 6b 69 70 20 74 68  EXPLAIN, skip th
1d8ec 69 73 20 73 74 65 70 20 2a 2f 0a 20 20 69 66 28  is step */.  if(
1d8ed 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
1d8ee 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
1d8ef 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73    }.#endif..  as
1d8f0 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20  sert( v!=0 );.  
1d8f1 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e  if( pParse->colN
1d8f2 61 6d 65 73 53 65 74 20 7c 7c 20 76 3d 3d 30 20  amesSet || v==0 
1d8f3 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
1d8f4 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  led ) return;.  
1d8f5 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73  pParse->colNames
1d8f6 53 65 74 20 3d 20 31 3b 0a 20 20 66 75 6c 6c 4e  Set = 1;.  fullN
1d8f7 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67  ames = (db->flag
1d8f8 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43  s & SQLITE_FullC
1d8f9 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20 20 73  olNames)!=0;.  s
1d8fa 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28 64 62 2d  hortNames = (db-
1d8fb 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
1d8fc 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29 21 3d  ShortColNames)!=
1d8fd 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  0;.  sqlite3Vdbe
1d8fe 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 70 45  SetNumCols(v, pE
1d8ff 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20  List->nExpr);.  
1d900 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73  for(i=0; i<pELis
1d901 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
1d902 20 20 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 20      Expr *p;.   
1d903 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69   p = pEList->a[i
1d904 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28  ].pExpr;.    if(
1d905 20 70 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65   p==0 ) continue
1d906 3b 0a 20 20 20 20 69 66 28 20 70 45 4c 69 73 74  ;.    if( pEList
1d907 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29 7b 0a  ->a[i].zName ){.
1d908 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
1d909 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d  e = pEList->a[i]
1d90a 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 73 71  .zName;.      sq
1d90b 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
1d90c 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
1d90d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 73  E_NAME, zName, s
1d90e 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 29 3b 0a 20  trlen(zName));. 
1d90f 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1d910 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e     }.    if( p->
1d911 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26  op==TK_COLUMN &&
1d912 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 20 20   pTabList ){.   
1d913 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
1d914 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c        char *zCol
1d915 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  ;.      int iCol
1d916 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20   = p->iColumn;. 
1d917 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
1d918 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 26  pTabList->nSrc &
1d919 26 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d  & pTabList->a[j]
1d91a 2e 69 43 75 72 73 6f 72 21 3d 70 2d 3e 69 54 61  .iCursor!=p->iTa
1d91b 62 6c 65 3b 20 6a 2b 2b 29 7b 7d 0a 20 20 20 20  ble; j++){}.    
1d91c 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54 61 62    assert( j<pTab
1d91d 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20 20  List->nSrc );.  
1d91e 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c      pTab = pTabL
1d91f 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a  ist->a[j].pTab;.
1d920 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30        if( iCol<0
1d921 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e   ) iCol = pTab->
1d922 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61 73 73  iPKey;.      ass
1d923 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c  ert( iCol==-1 ||
1d924 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f   (iCol>=0 && iCo
1d925 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b  l<pTab->nCol) );
1d926 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
1d927 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f  0 ){.        zCo
1d928 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20  l = "rowid";.   
1d929 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d92a 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
1d92b 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b  Col[iCol].zName;
1d92c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1d92d 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20 26  f( !shortNames &
1d92e 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 26 26 20  & !fullNames && 
1d92f 70 2d 3e 73 70 61 6e 2e 7a 20 26 26 20 70 2d 3e  p->span.z && p->
1d930 73 70 61 6e 2e 7a 5b 30 5d 20 29 7b 0a 20 20 20  span.z[0] ){.   
1d931 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1d932 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
1d933 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 28   COLNAME_NAME, (
1d934 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a 2c  char*)p->span.z,
1d935 20 70 2d 3e 73 70 61 6e 2e 6e 29 3b 0a 20 20 20   p->span.n);.   
1d936 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 75 6c     }else if( ful
1d937 6c 4e 61 6d 65 73 20 7c 7c 20 28 21 73 68 6f 72  lNames || (!shor
1d938 74 4e 61 6d 65 73 20 26 26 20 70 54 61 62 4c 69  tNames && pTabLi
1d939 73 74 2d 3e 6e 53 72 63 3e 31 29 20 29 7b 0a 20  st->nSrc>1) ){. 
1d93a 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61         char *zNa
1d93b 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  me = 0;.        
1d93c 63 68 61 72 20 2a 7a 54 61 62 3b 0a 20 0a 20 20  char *zTab;. .  
1d93d 20 20 20 20 20 20 7a 54 61 62 20 3d 20 70 54 61        zTab = pTa
1d93e 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 41 6c 69  bList->a[j].zAli
1d93f 61 73 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  as;.        if( 
1d940 66 75 6c 6c 4e 61 6d 65 73 20 7c 7c 20 7a 54 61  fullNames || zTa
1d941 62 3d 3d 30 20 29 20 7a 54 61 62 20 3d 20 70 54  b==0 ) zTab = pT
1d942 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ab->zName;.     
1d943 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1d944 69 6e 67 28 26 7a 4e 61 6d 65 2c 20 7a 54 61 62  ing(&zName, zTab
1d945 2c 20 22 2e 22 2c 20 7a 43 6f 6c 2c 20 28 63 68  , ".", zCol, (ch
1d946 61 72 2a 29 30 29 3b 0a 20 20 20 20 20 20 20 20  ar*)0);.        
1d947 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1d948 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
1d949 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c  AME_NAME, zName,
1d94a 20 50 33 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20   P3_DYNAMIC);.  
1d94b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1d94c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1d94d 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
1d94e 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 43 6f  OLNAME_NAME, zCo
1d94f 6c 2c 20 73 74 72 6c 65 6e 28 7a 43 6f 6c 29 29  l, strlen(zCol))
1d950 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
1d951 6c 73 65 20 69 66 28 20 70 2d 3e 73 70 61 6e 2e  lse if( p->span.
1d952 7a 20 26 26 20 70 2d 3e 73 70 61 6e 2e 7a 5b 30  z && p->span.z[0
1d953 5d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ] ){.      sqlit
1d954 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1d955 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, i, COLNAME_N
1d956 41 4d 45 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73  AME, (char*)p->s
1d957 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e  pan.z, p->span.n
1d958 29 3b 0a 20 20 20 20 20 20 2f 2a 20 73 71 6c 69  );.      /* sqli
1d959 74 65 33 56 64 62 65 43 6f 6d 70 72 65 73 73 53  te3VdbeCompressS
1d95a 70 61 63 65 28 76 2c 20 61 64 64 72 29 3b 20 2a  pace(v, addr); *
1d95b 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  /.    }else{.   
1d95c 20 20 20 63 68 61 72 20 7a 4e 61 6d 65 5b 33 30     char zName[30
1d95d 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ];.      assert(
1d95e 20 70 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d   p->op!=TK_COLUM
1d95f 4e 20 7c 7c 20 70 54 61 62 4c 69 73 74 3d 3d 30  N || pTabList==0
1d960 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1d961 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1d962 66 28 7a 4e 61 6d 65 29 2c 20 7a 4e 61 6d 65 2c  f(zName), zName,
1d963 20 22 63 6f 6c 75 6d 6e 25 64 22 2c 20 69 2b 31   "column%d", i+1
1d964 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1d965 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
1d966 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
1d967 45 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  E, zName, 0);.  
1d968 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65 72 61    }.  }.  genera
1d969 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 70 50  teColumnTypes(pP
1d96a 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
1d96b 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69 66 6e  pEList);.}..#ifn
1d96c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1d96d 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
1d96e 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68  /*.** Name of th
1d96f 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65  e connection ope
1d970 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f 72 20  rator, used for 
1d971 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 0a  error messages..
1d972 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
1d973 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70 4e 61  char *selectOpNa
1d974 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20 63 68  me(int id){.  ch
1d975 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63 68 28  ar *z;.  switch(
1d976 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20   id ){.    case 
1d977 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20 7a 20  TK_ALL:       z 
1d978 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20 20  = "UNION ALL";  
1d979 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
1d97a 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20 7a   TK_INTERSECT: z
1d97b 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22 3b 20   = "INTERSECT"; 
1d97c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
1d97d 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20 20 20  e TK_EXCEPT:    
1d97e 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20 20 20  z = "EXCEPT";   
1d97f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
1d980 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20  fault:          
1d981 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20 20 20   z = "UNION";   
1d982 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
1d983 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e   return z;.}.#en
1d984 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1d985 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
1d986 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72  CT */../*.** For
1d987 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
1d988 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1d989 72 65 70 53 65 6c 65 63 74 53 74 6d 74 28 50 61  repSelectStmt(Pa
1d98a 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a  rse*, Select*);.
1d98b 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 53  ./*.** Given a S
1d98c 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c  ELECT statement,
1d98d 20 67 65 6e 65 72 61 74 65 20 61 20 54 61 62 6c   generate a Tabl
1d98e 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  e structure that
1d98f 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 74 68   describes.** th
1d990 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
1d991 74 68 61 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a  that SELECT..*/.
1d992 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
1d993 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52 65 73  able *sqlite3Res
1d994 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 50  ultSetOfSelect(P
1d995 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 68  arse *pParse, ch
1d996 61 72 20 2a 7a 54 61 62 4e 61 6d 65 2c 20 53 65  ar *zTabName, Se
1d997 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a  lect *pSelect){.
1d998 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
1d999 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 45 78 70   int i, j;.  Exp
1d99a 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20  rList *pEList;. 
1d99b 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20 2a   Column *aCol, *
1d99c 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20  pCol;.  sqlite3 
1d99d 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1d99e 3b 0a 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c  ;..  while( pSel
1d99f 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 53  ect->pPrior ) pS
1d9a0 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d  elect = pSelect-
1d9a1 3e 70 50 72 69 6f 72 3b 0a 20 20 69 66 28 20 70  >pPrior;.  if( p
1d9a2 72 65 70 53 65 6c 65 63 74 53 74 6d 74 28 70 50  repSelectStmt(pP
1d9a3 61 72 73 65 2c 20 70 53 65 6c 65 63 74 29 20 29  arse, pSelect) )
1d9a4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
1d9a5 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
1d9a6 33 53 65 6c 65 63 74 52 65 73 6f 6c 76 65 28 70  3SelectResolve(p
1d9a7 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
1d9a8 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  0) ){.    return
1d9a9 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d   0;.  }.  pTab =
1d9aa 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1d9ab 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
1d9ac 54 61 62 6c 65 29 20 29 3b 0a 20 20 69 66 28 20  Table) );.  if( 
1d9ad 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 72  pTab==0 ){.    r
1d9ae 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
1d9af 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  Tab->nRef = 1;. 
1d9b0 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d 20 7a   pTab->zName = z
1d9b1 54 61 62 4e 61 6d 65 20 3f 20 73 71 6c 69 74 65  TabName ? sqlite
1d9b2 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54  3DbStrDup(db, zT
1d9b3 61 62 4e 61 6d 65 29 20 3a 20 30 3b 0a 20 20 70  abName) : 0;.  p
1d9b4 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d  EList = pSelect-
1d9b5 3e 70 45 4c 69 73 74 3b 0a 20 20 70 54 61 62 2d  >pEList;.  pTab-
1d9b6 3e 6e 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d 3e  >nCol = pEList->
1d9b7 6e 45 78 70 72 3b 0a 20 20 61 73 73 65 72 74 28  nExpr;.  assert(
1d9b8 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b   pTab->nCol>0 );
1d9b9 0a 20 20 70 54 61 62 2d 3e 61 43 6f 6c 20 3d 20  .  pTab->aCol = 
1d9ba 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  aCol = sqlite3Db
1d9bb 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
1d9bc 69 7a 65 6f 66 28 70 54 61 62 2d 3e 61 43 6f 6c  izeof(pTab->aCol
1d9bd 5b 30 5d 29 2a 70 54 61 62 2d 3e 6e 43 6f 6c 29  [0])*pTab->nCol)
1d9be 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f  ;.  for(i=0, pCo
1d9bf 6c 3d 61 43 6f 6c 3b 20 69 3c 70 54 61 62 2d 3e  l=aCol; i<pTab->
1d9c0 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b  nCol; i++, pCol+
1d9c1 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 2c  +){.    Expr *p,
1d9c2 20 2a 70 52 3b 0a 20 20 20 20 63 68 61 72 20 2a   *pR;.    char *
1d9c3 7a 54 79 70 65 3b 0a 20 20 20 20 63 68 61 72 20  zType;.    char 
1d9c4 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 6e 74 20  *zName;.    int 
1d9c5 6e 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 6c 53  nName;.    CollS
1d9c6 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 69  eq *pColl;.    i
1d9c7 6e 74 20 63 6e 74 3b 0a 20 20 20 20 4e 61 6d 65  nt cnt;.    Name
1d9c8 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
1d9c9 20 0a 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20   .    /* Get an 
1d9ca 61 70 70 72 6f 70 72 69 61 74 65 20 6e 61 6d 65  appropriate name
1d9cb 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a   for the column.
1d9cc 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70      */.    p = p
1d9cd 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  EList->a[i].pExp
1d9ce 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  r;.    assert( p
1d9cf 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70  ->pRight==0 || p
1d9d0 2d 3e 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e  ->pRight->token.
1d9d1 7a 3d 3d 30 20 7c 7c 20 70 2d 3e 70 52 69 67 68  z==0 || p->pRigh
1d9d2 74 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 30  t->token.z[0]!=0
1d9d3 20 29 3b 0a 20 20 20 20 69 66 28 20 28 7a 4e 61   );.    if( (zNa
1d9d4 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69  me = pEList->a[i
1d9d5 5d 2e 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20  ].zName)!=0 ){. 
1d9d6 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63       /* If the c
1d9d7 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 61  olumn contains a
1d9d8 6e 20 22 41 53 20 3c 6e 61 6d 65 3e 22 20 70 68  n "AS <name>" ph
1d9d9 72 61 73 65 2c 20 75 73 65 20 3c 6e 61 6d 65 3e  rase, use <name>
1d9da 20 61 73 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a   as the name */.
1d9db 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71        zName = sq
1d9dc 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
1d9dd 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65  , zName);.    }e
1d9de 6c 73 65 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54  lse if( p->op==T
1d9df 4b 5f 44 4f 54 20 0a 20 20 20 20 20 20 20 20 20  K_DOT .         
1d9e0 20 20 20 20 20 26 26 20 28 70 52 3d 70 2d 3e 70       && (pR=p->p
1d9e1 52 69 67 68 74 29 21 3d 30 20 26 26 20 70 52 2d  Right)!=0 && pR-
1d9e2 3e 74 6f 6b 65 6e 2e 7a 20 26 26 20 70 52 2d 3e  >token.z && pR->
1d9e3 74 6f 6b 65 6e 2e 7a 5b 30 5d 20 29 7b 0a 20 20  token.z[0] ){.  
1d9e4 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d      /* For colum
1d9e5 6e 73 20 6f 66 20 74 68 65 20 66 72 6f 6d 20 41  ns of the from A
1d9e6 2e 42 20 75 73 65 20 42 20 61 73 20 74 68 65 20  .B use B as the 
1d9e7 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a 4e  name */.      zN
1d9e8 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
1d9e9 69 6e 74 66 28 64 62 2c 20 22 25 54 22 2c 20 26  intf(db, "%T", &
1d9ea 70 52 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  pR->token);.    
1d9eb 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 73 70 61  }else if( p->spa
1d9ec 6e 2e 7a 20 26 26 20 70 2d 3e 73 70 61 6e 2e 7a  n.z && p->span.z
1d9ed 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  [0] ){.      /* 
1d9ee 55 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  Use the original
1d9ef 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6c   text of the col
1d9f0 75 6d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61  umn expression a
1d9f1 73 20 69 74 73 20 6e 61 6d 65 20 2a 2f 0a 20 20  s its name */.  
1d9f2 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69      zName = sqli
1d9f3 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
1d9f4 25 54 22 2c 20 26 70 2d 3e 73 70 61 6e 29 3b 0a  %T", &p->span);.
1d9f5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1d9f6 20 2f 2a 20 49 66 20 61 6c 6c 20 65 6c 73 65 20   /* If all else 
1d9f7 66 61 69 6c 73 2c 20 6d 61 6b 65 20 75 70 20 61  fails, make up a
1d9f8 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a   name */.      z
1d9f9 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
1d9fa 72 69 6e 74 66 28 64 62 2c 20 22 63 6f 6c 75 6d  rintf(db, "colum
1d9fb 6e 25 64 22 2c 20 69 2b 31 29 3b 0a 20 20 20 20  n%d", i+1);.    
1d9fc 7d 0a 20 20 20 20 69 66 28 20 21 7a 4e 61 6d 65  }.    if( !zName
1d9fd 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1d9fe 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 64 62  iled ){.      db
1d9ff 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1da00 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   1;.      sqlite
1da01 33 5f 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20  3_free(zName);. 
1da02 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65       sqlite3Dele
1da03 74 65 54 61 62 6c 65 28 70 54 61 62 29 3b 0a 20  teTable(pTab);. 
1da04 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
1da05 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1da06 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a  Dequote(zName);.
1da07 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  .    /* Make sur
1da08 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  e the column nam
1da09 65 20 69 73 20 75 6e 69 71 75 65 2e 20 20 49 66  e is unique.  If
1da0a 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74   the name is not
1da0b 20 75 6e 69 71 75 65 2c 0a 20 20 20 20 2a 2a 20   unique,.    ** 
1da0c 61 70 70 65 6e 64 20 61 20 69 6e 74 65 67 65 72  append a integer
1da0d 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 73 6f 20   to the name so 
1da0e 74 68 61 74 20 69 74 20 62 65 63 6f 6d 65 73 20  that it becomes 
1da0f 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a 20  unique..    */. 
1da10 20 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65     nName = strle
1da11 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 6f  n(zName);.    fo
1da12 72 28 6a 3d 63 6e 74 3d 30 3b 20 6a 3c 69 3b 20  r(j=cnt=0; j<i; 
1da13 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
1da14 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 61  sqlite3StrICmp(a
1da15 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e  Col[j].zName, zN
1da16 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ame)==0 ){.     
1da17 20 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20     zName[nName] 
1da18 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  = 0;.        zNa
1da19 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
1da1a 6e 74 66 28 64 62 2c 20 22 25 7a 3a 25 64 22 2c  ntf(db, "%z:%d",
1da1b 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29 3b 0a   zName, ++cnt);.
1da1c 20 20 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a          j = -1;.
1da1d 20 20 20 20 20 20 20 20 69 66 28 20 7a 4e 61 6d          if( zNam
1da1e 65 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  e==0 ) break;.  
1da1f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1da20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e  pCol->zName = zN
1da21 61 6d 65 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 74  ame;..    /* Get
1da22 20 74 68 65 20 74 79 70 65 6e 61 6d 65 2c 20 74   the typename, t
1da23 79 70 65 20 61 66 66 69 6e 69 74 79 2c 20 61 6e  ype affinity, an
1da24 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  d collating sequ
1da25 65 6e 63 65 20 66 6f 72 20 74 68 65 0a 20 20 20  ence for the.   
1da26 20 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20   ** column..    
1da27 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73  */.    memset(&s
1da28 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e  NC, 0, sizeof(sN
1da29 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72  C));.    sNC.pSr
1da2a 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d  cList = pSelect-
1da2b 3e 70 53 72 63 3b 0a 20 20 20 20 7a 54 79 70 65  >pSrc;.    zType
1da2c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
1da2d 75 70 28 64 62 2c 20 63 6f 6c 75 6d 6e 54 79 70  up(db, columnTyp
1da2e 65 28 26 73 4e 43 2c 20 70 2c 20 30 2c 20 30 2c  e(&sNC, p, 0, 0,
1da2f 20 30 29 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e   0));.    pCol->
1da30 7a 54 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20  zType = zType;. 
1da31 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74     pCol->affinit
1da32 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  y = sqlite3ExprA
1da33 66 66 69 6e 69 74 79 28 70 29 3b 0a 20 20 20 20  ffinity(p);.    
1da34 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
1da35 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
1da36 65 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70  e, p);.    if( p
1da37 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43  Coll ){.      pC
1da38 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69  ol->zColl = sqli
1da39 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
1da3a 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
1da3b 20 20 20 7d 0a 20 20 7d 0a 20 20 70 54 61 62 2d     }.  }.  pTab-
1da3c 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 72  >iPKey = -1;.  r
1da3d 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f  eturn pTab;.}../
1da3e 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 53  *.** Prepare a S
1da3f 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1da40 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 62  for processing b
1da41 79 20 64 6f 69 6e 67 20 74 68 65 20 66 6f 6c 6c  y doing the foll
1da42 6f 77 69 6e 67 0a 2a 2a 20 74 68 69 6e 67 73 3a  owing.** things:
1da43 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 4d  .**.**    (1)  M
1da44 61 6b 65 20 73 75 72 65 20 56 44 42 45 20 63 75  ake sure VDBE cu
1da45 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61 76  rsor numbers hav
1da46 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20  e been assigned 
1da47 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20 20 20 20  to every.**     
1da48 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74      element of t
1da49 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a  he FROM clause..
1da4a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 46 69  **.**    (2)  Fi
1da4b 6c 6c 20 69 6e 20 74 68 65 20 70 54 61 62 4c 69  ll in the pTabLi
1da4c 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66 69 65  st->a[].pTab fie
1da4d 6c 64 73 20 69 6e 20 74 68 65 20 53 72 63 4c 69  lds in the SrcLi
1da4e 73 74 20 74 68 61 74 20 0a 2a 2a 20 20 20 20 20  st that .**     
1da4f 20 20 20 20 64 65 66 69 6e 65 73 20 46 52 4f 4d      defines FROM
1da50 20 63 6c 61 75 73 65 2e 20 20 57 68 65 6e 20 76   clause.  When v
1da51 69 65 77 73 20 61 70 70 65 61 72 20 69 6e 20 74  iews appear in t
1da52 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0a  he FROM clause,.
1da53 2a 2a 20 20 20 20 20 20 20 20 20 66 69 6c 6c 20  **         fill 
1da54 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 53  pTabList->a[].pS
1da55 65 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f 70  elect with a cop
1da56 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  y of the SELECT 
1da57 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20  statement.**    
1da58 20 20 20 20 20 74 68 61 74 20 69 6d 70 6c 65 6d       that implem
1da59 65 6e 74 73 20 74 68 65 20 76 69 65 77 2e 20 20  ents the view.  
1da5a 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f  A copy is made o
1da5b 66 20 74 68 65 20 76 69 65 77 27 73 20 53 45 4c  f the view's SEL
1da5c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  ECT.**         s
1da5d 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68 61 74  tatement so that
1da5e 20 77 65 20 63 61 6e 20 66 72 65 65 6c 79 20 6d   we can freely m
1da5f 6f 64 69 66 79 20 6f 72 20 64 65 6c 65 74 65 20  odify or delete 
1da60 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a  that statement.*
1da61 2a 20 20 20 20 20 20 20 20 20 77 69 74 68 6f 75  *         withou
1da62 74 20 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74  t worrying about
1da63 20 6d 65 73 73 69 6e 67 20 75 70 20 74 68 65 20   messing up the 
1da64 70 72 65 73 69 73 74 65 6e 74 20 72 65 70 72 65  presistent repre
1da65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20  sentation.**    
1da66 20 20 20 20 20 6f 66 20 74 68 65 20 76 69 65 77       of the view
1da67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20  ..**.**    (3)  
1da68 41 64 64 20 74 65 72 6d 73 20 74 6f 20 74 68 65  Add terms to the
1da69 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f   WHERE clause to
1da6a 20 61 63 63 6f 6d 6f 64 61 74 65 20 74 68 65 20   accomodate the 
1da6b 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 0a  NATURAL keyword.
1da6c 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e 20 6a 6f  **         on jo
1da6d 69 6e 73 20 61 6e 64 20 74 68 65 20 4f 4e 20 61  ins and the ON a
1da6e 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20  nd USING clause 
1da6f 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a 2a 20  of joins..**.** 
1da70 20 20 20 28 34 29 20 20 53 63 61 6e 20 74 68 65     (4)  Scan the
1da71 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
1da72 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1da73 65 74 20 28 70 45 4c 69 73 74 29 20 6c 6f 6f 6b  et (pEList) look
1da74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 66  ing.**         f
1da75 6f 72 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20  or instances of 
1da76 74 68 65 20 22 2a 22 20 6f 70 65 72 61 74 6f 72  the "*" operator
1da77 20 6f 72 20 74 68 65 20 54 41 42 4c 45 2e 2a 20   or the TABLE.* 
1da78 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 20 20 20  operator..**    
1da79 20 20 20 20 20 49 66 20 66 6f 75 6e 64 2c 20 65       If found, e
1da7a 78 70 61 6e 64 20 65 61 63 68 20 22 2a 22 20 74  xpand each "*" t
1da7b 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75 6d  o be every colum
1da7c 6e 20 69 6e 20 65 76 65 72 79 20 74 61 62 6c 65  n in every table
1da7d 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 6e 64 20  .**         and 
1da7e 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65 20 65 76  TABLE.* to be ev
1da7f 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 54 41  ery column in TA
1da80 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  BLE..**.** Retur
1da81 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  n 0 on success. 
1da82 20 49 66 20 74 68 65 72 65 20 61 72 65 20 70 72   If there are pr
1da83 6f 62 6c 65 6d 73 2c 20 6c 65 61 76 65 20 61 6e  oblems, leave an
1da84 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a   error message.*
1da85 2a 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20  * in pParse and 
1da86 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e  return non-zero.
1da87 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1da88 72 65 70 53 65 6c 65 63 74 53 74 6d 74 28 50 61  repSelectStmt(Pa
1da89 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c  rse *pParse, Sel
1da8a 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  ect *p){.  int i
1da8b 2c 20 6a 2c 20 6b 2c 20 72 63 3b 0a 20 20 53 72  , j, k, rc;.  Sr
1da8c 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b  cList *pTabList;
1da8d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
1da8e 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 53 72  ist;.  struct Sr
1da8f 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f  cList_item *pFro
1da90 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  m;.  sqlite3 *db
1da91 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
1da92 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 2d    if( p==0 || p-
1da93 3e 70 53 72 63 3d 3d 30 20 7c 7c 20 64 62 2d 3e  >pSrc==0 || db->
1da94 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1da95 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1da96 7d 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70  }.  pTabList = p
1da97 2d 3e 70 53 72 63 3b 0a 20 20 70 45 4c 69 73 74  ->pSrc;.  pEList
1da98 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20   = p->pEList;.. 
1da99 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 63 75   /* Make sure cu
1da9a 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61 76  rsor numbers hav
1da9b 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20  e been assigned 
1da9c 74 6f 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69  to all entries i
1da9d 6e 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20  n.  ** the FROM 
1da9e 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 53 45  clause of the SE
1da9f 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
1daa0 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 72    */.  sqlite3Sr
1daa1 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f  cListAssignCurso
1daa2 72 73 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 53  rs(pParse, p->pS
1daa3 72 63 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20  rc);..  /* Look 
1daa4 75 70 20 65 76 65 72 79 20 74 61 62 6c 65 20 6e  up every table n
1daa5 61 6d 65 64 20 69 6e 20 74 68 65 20 46 52 4f 4d  amed in the FROM
1daa6 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73   clause of the s
1daa7 65 6c 65 63 74 2e 20 20 49 66 0a 20 20 2a 2a 20  elect.  If.  ** 
1daa8 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20  an entry of the 
1daa9 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61  FROM clause is a
1daaa 20 73 75 62 71 75 65 72 79 20 69 6e 73 74 65 61   subquery instea
1daab 64 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20  d of a table or 
1daac 76 69 65 77 2c 0a 20 20 2a 2a 20 74 68 65 6e 20  view,.  ** then 
1daad 63 72 65 61 74 65 20 61 20 74 72 61 6e 73 69 65  create a transie
1daae 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63 74 75  nt table structu
1daaf 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 74  re to describe t
1dab0 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20 2a  he subquery..  *
1dab1 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72  /.  for(i=0, pFr
1dab2 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20  om=pTabList->a; 
1dab3 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
1dab4 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b  ; i++, pFrom++){
1dab5 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
1dab6 3b 0a 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d  ;.    if( pFrom-
1dab7 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20 20 20  >pTab!=0 ){.    
1dab8 20 20 2f 2a 20 54 68 69 73 20 73 74 61 74 65 6d    /* This statem
1dab9 65 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20  ent has already 
1daba 62 65 65 6e 20 70 72 65 70 61 72 65 64 2e 20 20  been prepared.  
1dabb 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  There is no need
1dabc 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 67 6f 20  .      ** to go 
1dabd 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20  further. */.    
1dabe 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 29    assert( i==0 )
1dabf 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
1dac0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1dac1 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20  pFrom->zName==0 
1dac2 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
1dac3 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
1dac4 20 20 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71        /* A sub-q
1dac5 75 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d  uery in the FROM
1dac6 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c   clause of a SEL
1dac7 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  ECT */.      ass
1dac8 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 53 65 6c  ert( pFrom->pSel
1dac9 65 63 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  ect!=0 );.      
1daca 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61  if( pFrom->zAlia
1dacb 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  s==0 ){.        
1dacc 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61 73 20 3d 0a  pFrom->zAlias =.
1dacd 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1dace 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71  3MPrintf(db, "sq
1dacf 6c 69 74 65 5f 73 75 62 71 75 65 72 79 5f 25 70  lite_subquery_%p
1dad0 5f 22 2c 20 28 76 6f 69 64 2a 29 70 46 72 6f 6d  _", (void*)pFrom
1dad1 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  ->pSelect);.    
1dad2 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
1dad3 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30  ( pFrom->pTab==0
1dad4 20 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d   );.      pFrom-
1dad5 3e 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20 0a  >pTab = pTab = .
1dad6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1dad7 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74  esultSetOfSelect
1dad8 28 70 50 61 72 73 65 2c 20 70 46 72 6f 6d 2d 3e  (pParse, pFrom->
1dad9 7a 41 6c 69 61 73 2c 20 70 46 72 6f 6d 2d 3e 70  zAlias, pFrom->p
1dada 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 69  Select);.      i
1dadb 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20  f( pTab==0 ){.  
1dadc 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1dadd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a        }.      /*
1dade 20 54 68 65 20 69 73 45 70 68 65 6d 20 66 6c 61   The isEphem fla
1dadf 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  g indicates that
1dae0 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
1dae1 74 75 72 65 20 68 61 73 20 62 65 65 6e 0a 20 20  ture has been.  
1dae2 20 20 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c      ** dynamical
1dae3 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  ly allocated and
1dae4 20 6d 61 79 20 62 65 20 66 72 65 65 64 20 61 74   may be freed at
1dae5 20 61 6e 79 20 74 69 6d 65 2e 20 20 49 6e 20 6f   any time.  In o
1dae6 74 68 65 72 20 77 6f 72 64 73 2c 0a 20 20 20 20  ther words,.    
1dae7 20 20 2a 2a 20 70 54 61 62 20 69 73 20 6e 6f 74    ** pTab is not
1dae8 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 70   pointing to a p
1dae9 65 72 73 69 73 74 65 6e 74 20 74 61 62 6c 65 20  ersistent table 
1daea 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64  structure that d
1daeb 65 66 69 6e 65 73 0a 20 20 20 20 20 20 2a 2a 20  efines.      ** 
1daec 70 61 72 74 20 6f 66 20 74 68 65 20 73 63 68 65  part of the sche
1daed 6d 61 2e 20 2a 2f 0a 20 20 20 20 20 20 70 54 61  ma. */.      pTa
1daee 62 2d 3e 69 73 45 70 68 65 6d 20 3d 20 31 3b 0a  b->isEphem = 1;.
1daef 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65  #endif.    }else
1daf0 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 72  {.      /* An or
1daf1 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f 72 20  dinary table or 
1daf2 76 69 65 77 20 6e 61 6d 65 20 69 6e 20 74 68 65  view name in the
1daf3 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
1daf4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46        assert( pF
1daf5 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b 0a  rom->pTab==0 );.
1daf6 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61        pFrom->pTa
1daf7 62 20 3d 20 70 54 61 62 20 3d 20 0a 20 20 20 20  b = pTab = .    
1daf8 20 20 20 20 73 71 6c 69 74 65 33 4c 6f 63 61 74      sqlite3Locat
1daf9 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 70 46  eTable(pParse,pF
1dafa 72 6f 6d 2d 3e 7a 4e 61 6d 65 2c 70 46 72 6f 6d  rom->zName,pFrom
1dafb 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
1dafc 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20      if( pTab==0 
1dafd 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
1dafe 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
1daff 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b     pTab->nRef++;
1db00 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1db01 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
1db02 7c 7c 20 21 64 65 66 69 6e 65 64 20 28 53 51 4c  || !defined (SQL
1db03 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1db04 54 41 42 4c 45 29 0a 20 20 20 20 20 20 69 66 28  TABLE).      if(
1db05 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 7c   pTab->pSelect |
1db06 7c 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  | IsVirtual(pTab
1db07 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
1db08 57 65 20 72 65 61 63 68 20 68 65 72 65 20 69 66  We reach here if
1db09 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65   the named table
1db0a 20 69 73 20 61 20 72 65 61 6c 6c 79 20 61 20 76   is a really a v
1db0b 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  iew */.        i
1db0c 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  f( sqlite3ViewGe
1db0d 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
1db0e 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20  rse, pTab) ){.  
1db0f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
1db10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1db11 20 20 20 20 2f 2a 20 49 66 20 70 46 72 6f 6d 2d      /* If pFrom-
1db12 3e 70 53 65 6c 65 63 74 21 3d 30 20 69 74 20 6d  >pSelect!=0 it m
1db13 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c  eans we are deal
1db14 69 6e 67 20 77 69 74 68 20 61 0a 20 20 20 20 20  ing with a.     
1db15 20 20 20 2a 2a 20 76 69 65 77 20 77 69 74 68 69     ** view withi
1db16 6e 20 61 20 76 69 65 77 2e 20 20 54 68 65 20 53  n a view.  The S
1db17 45 4c 45 43 54 20 73 74 72 75 63 74 75 72 65 20  ELECT structure 
1db18 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
1db19 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 69  .        ** copi
1db1a 65 64 20 62 79 20 74 68 65 20 6f 75 74 65 72 20  ed by the outer 
1db1b 76 69 65 77 20 73 6f 20 77 65 20 63 61 6e 20 73  view so we can s
1db1c 6b 69 70 20 74 68 65 20 63 6f 70 79 20 73 74 65  kip the copy ste
1db1d 70 20 68 65 72 65 0a 20 20 20 20 20 20 20 20 2a  p here.        *
1db1e 2a 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 76  * in the inner v
1db1f 69 65 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  iew..        */.
1db20 20 20 20 20 20 20 20 20 69 66 28 20 70 46 72 6f          if( pFro
1db21 6d 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b  m->pSelect==0 ){
1db22 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d  .          pFrom
1db23 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ->pSelect = sqli
1db24 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
1db25 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 29 3b   pTab->pSelect);
1db26 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1db27 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a   }.#endif.    }.
1db28 20 20 7d 0a 0a 20 20 2f 2a 20 50 72 6f 63 65 73    }..  /* Proces
1db29 73 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72  s NATURAL keywor
1db2a 64 73 2c 20 61 6e 64 20 4f 4e 20 61 6e 64 20 55  ds, and ON and U
1db2b 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20  SING clauses of 
1db2c 6a 6f 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  joins..  */.  if
1db2d 28 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a  ( sqliteProcessJ
1db2e 6f 69 6e 28 70 50 61 72 73 65 2c 20 70 29 20 29  oin(pParse, p) )
1db2f 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 2f 2a   return 1;..  /*
1db30 20 46 6f 72 20 65 76 65 72 79 20 22 2a 22 20 74   For every "*" t
1db31 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 74 68  hat occurs in th
1db32 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20 69  e column list, i
1db33 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73 20  nsert the names 
1db34 6f 66 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c 75  of.  ** all colu
1db35 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65  mns in all table
1db36 73 2e 20 20 41 6e 64 20 66 6f 72 20 65 76 65 72  s.  And for ever
1db37 79 20 54 41 42 4c 45 2e 2a 20 69 6e 73 65 72 74  y TABLE.* insert
1db38 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20   the names.  ** 
1db39 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69  of all columns i
1db3a 6e 20 54 41 42 4c 45 2e 20 20 54 68 65 20 70 61  n TABLE.  The pa
1db3b 72 73 65 72 20 69 6e 73 65 72 74 65 64 20 61 20  rser inserted a 
1db3c 73 70 65 63 69 61 6c 20 65 78 70 72 65 73 73 69  special expressi
1db3d 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 20 74 68 65  on.  ** with the
1db3e 20 54 4b 5f 41 4c 4c 20 6f 70 65 72 61 74 6f 72   TK_ALL operator
1db3f 20 66 6f 72 20 65 61 63 68 20 22 2a 22 20 74 68   for each "*" th
1db40 61 74 20 69 74 20 66 6f 75 6e 64 20 69 6e 20 74  at it found in t
1db41 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0a  he column list..
1db42 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    ** The followi
1db43 6e 67 20 63 6f 64 65 20 6a 75 73 74 20 68 61 73  ng code just has
1db44 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 54   to locate the T
1db45 4b 5f 41 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e  K_ALL expression
1db46 73 20 61 6e 64 20 65 78 70 61 6e 64 0a 20 20 2a  s and expand.  *
1db47 2a 20 65 61 63 68 20 6f 6e 65 20 74 6f 20 74 68  * each one to th
1db48 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f  e list of all co
1db49 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62  lumns in all tab
1db4a 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  les..  **.  ** T
1db4b 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 20 6a 75  he first loop ju
1db4c 73 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65  st checks to see
1db4d 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   if there are an
1db4e 79 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 73 0a  y "*" operators.
1db4f 20 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 20 65    ** that need e
1db50 78 70 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20  xpanding..  */. 
1db51 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69   for(k=0; k<pELi
1db52 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b  st->nExpr; k++){
1db53 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20  .    Expr *pE = 
1db54 70 45 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45 78  pEList->a[k].pEx
1db55 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 2d 3e  pr;.    if( pE->
1db56 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72 65  op==TK_ALL ) bre
1db57 61 6b 3b 0a 20 20 20 20 69 66 28 20 70 45 2d 3e  ak;.    if( pE->
1db58 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20 70 45  op==TK_DOT && pE
1db59 2d 3e 70 52 69 67 68 74 20 26 26 20 70 45 2d 3e  ->pRight && pE->
1db5a 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 41  pRight->op==TK_A
1db5b 4c 4c 0a 20 20 20 20 20 20 20 20 20 26 26 20 70  LL.         && p
1db5c 45 2d 3e 70 4c 65 66 74 20 26 26 20 70 45 2d 3e  E->pLeft && pE->
1db5d 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44  pLeft->op==TK_ID
1db5e 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20   ) break;.  }.  
1db5f 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 6b 3c  rc = 0;.  if( k<
1db60 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b  pEList->nExpr ){
1db61 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 49  .    /*.    ** I
1db62 66 20 77 65 20 67 65 74 20 68 65 72 65 20 69 74  f we get here it
1db63 20 6d 65 61 6e 73 20 74 68 65 20 72 65 73 75 6c   means the resul
1db64 74 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6f  t set contains o
1db65 6e 65 20 6f 72 20 6d 6f 72 65 20 22 2a 22 0a 20  ne or more "*". 
1db66 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 73 20     ** operators 
1db67 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20  that need to be 
1db68 65 78 70 61 6e 64 65 64 2e 20 20 4c 6f 6f 70 20  expanded.  Loop 
1db69 74 68 72 6f 75 67 68 20 65 61 63 68 20 65 78 70  through each exp
1db6a 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20 69  ression.    ** i
1db6b 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
1db6c 20 61 6e 64 20 65 78 70 61 6e 64 20 74 68 65 6d   and expand them
1db6d 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20 20   one by one..   
1db6e 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 45   */.    struct E
1db6f 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 20  xprList_item *a 
1db70 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20  = pEList->a;.   
1db71 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 20   ExprList *pNew 
1db72 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 66 6c 61  = 0;.    int fla
1db73 67 73 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  gs = pParse->db-
1db74 3e 66 6c 61 67 73 3b 0a 20 20 20 20 69 6e 74 20  >flags;.    int 
1db75 6c 6f 6e 67 4e 61 6d 65 73 20 3d 20 28 66 6c 61  longNames = (fla
1db76 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c 6c  gs & SQLITE_Full
1db77 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 20 26 26 0a  ColNames)!=0 &&.
1db78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1db79 20 20 20 20 20 20 28 66 6c 61 67 73 20 26 20 53        (flags & S
1db7a 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61  QLITE_ShortColNa
1db7b 6d 65 73 29 3d 3d 30 3b 0a 0a 20 20 20 20 66 6f  mes)==0;..    fo
1db7c 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74 2d  r(k=0; k<pEList-
1db7d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20  >nExpr; k++){.  
1db7e 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 61      Expr *pE = a
1db7f 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [k].pExpr;.     
1db80 20 69 66 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f   if( pE->op!=TK_
1db81 41 4c 4c 20 26 26 0a 20 20 20 20 20 20 20 20 20  ALL &&.         
1db82 20 20 28 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f    (pE->op!=TK_DO
1db83 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68 74 3d  T || pE->pRight=
1db84 3d 30 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68 74  =0 || pE->pRight
1db85 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20 29 7b  ->op!=TK_ALL) ){
1db86 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
1db87 20 70 61 72 74 69 63 75 6c 61 72 20 65 78 70 72   particular expr
1db88 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  ession does not 
1db89 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70 61 6e  need to be expan
1db8a 64 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ded..        */.
1db8b 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73          pNew = s
1db8c 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
1db8d 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4e 65  pend(pParse, pNe
1db8e 77 2c 20 61 5b 6b 5d 2e 70 45 78 70 72 2c 20 30  w, a[k].pExpr, 0
1db8f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
1db90 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20  New ){.         
1db91 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e   pNew->a[pNew->n
1db92 45 78 70 72 2d 31 5d 2e 7a 4e 61 6d 65 20 3d 20  Expr-1].zName = 
1db93 61 5b 6b 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  a[k].zName;.    
1db94 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1db95 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20       rc = 1;.   
1db96 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
1db97 5b 6b 5d 2e 70 45 78 70 72 20 3d 20 30 3b 0a 20  [k].pExpr = 0;. 
1db98 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 4e 61 6d         a[k].zNam
1db99 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  e = 0;.      }el
1db9a 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  se{.        /* T
1db9b 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 69  his expression i
1db9c 73 20 61 20 22 2a 22 20 6f 72 20 61 20 22 54 41  s a "*" or a "TA
1db9d 42 4c 45 2e 2a 22 20 61 6e 64 20 6e 65 65 64 73  BLE.*" and needs
1db9e 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20 2a   to be.        *
1db9f 2a 20 65 78 70 61 6e 64 65 64 2e 20 2a 2f 0a 20  * expanded. */. 
1dba0 20 20 20 20 20 20 20 69 6e 74 20 74 61 62 6c 65         int table
1dba1 53 65 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 2f  Seen = 0;      /
1dba2 2a 20 53 65 74 20 74 6f 20 31 20 77 68 65 6e 20  * Set to 1 when 
1dba3 54 41 42 4c 45 20 6d 61 74 63 68 65 73 20 2a 2f  TABLE matches */
1dba4 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
1dba5 54 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  TName;          
1dba6 20 20 2f 2a 20 74 65 78 74 20 6f 66 20 6e 61 6d    /* text of nam
1dba7 65 20 6f 66 20 54 41 42 4c 45 20 2a 2f 0a 20 20  e of TABLE */.  
1dba8 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70        if( pE->op
1dba9 3d 3d 54 4b 5f 44 4f 54 20 26 26 20 70 45 2d 3e  ==TK_DOT && pE->
1dbaa 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  pLeft ){.       
1dbab 20 20 20 7a 54 4e 61 6d 65 20 3d 20 73 71 6c 69     zTName = sqli
1dbac 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
1dbad 28 64 62 2c 20 26 70 45 2d 3e 70 4c 65 66 74 2d  (db, &pE->pLeft-
1dbae 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20  >token);.       
1dbaf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1dbb0 20 20 7a 54 4e 61 6d 65 20 3d 20 30 3b 0a 20 20    zTName = 0;.  
1dbb1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1dbb2 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70  for(i=0, pFrom=p
1dbb3 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54  TabList->a; i<pT
1dbb4 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
1dbb5 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20  +, pFrom++){.   
1dbb6 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54         Table *pT
1dbb7 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62  ab = pFrom->pTab
1dbb8 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72  ;.          char
1dbb9 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 46 72   *zTabName = pFr
1dbba 6f 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20  om->zAlias;.    
1dbbb 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61        if( zTabNa
1dbbc 6d 65 3d 3d 30 20 7c 7c 20 7a 54 61 62 4e 61 6d  me==0 || zTabNam
1dbbd 65 5b 30 5d 3d 3d 30 20 29 7b 20 0a 20 20 20 20  e[0]==0 ){ .    
1dbbe 20 20 20 20 20 20 20 20 7a 54 61 62 4e 61 6d 65          zTabName
1dbbf 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a   = pTab->zName;.
1dbc0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1dbc1 20 20 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65        if( zTName
1dbc2 20 26 26 20 28 7a 54 61 62 4e 61 6d 65 3d 3d 30   && (zTabName==0
1dbc3 20 7c 7c 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 3d   || zTabName[0]=
1dbc4 3d 30 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20  =0 || .         
1dbc5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
1dbc6 74 72 49 43 6d 70 28 7a 54 4e 61 6d 65 2c 20 7a  trICmp(zTName, z
1dbc7 54 61 62 4e 61 6d 65 29 21 3d 30 29 20 29 7b 0a  TabName)!=0) ){.
1dbc8 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74              cont
1dbc9 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  inue;.          
1dbca 7d 0a 20 20 20 20 20 20 20 20 20 20 74 61 62 6c  }.          tabl
1dbcb 65 53 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20  eSeen = 1;.     
1dbcc 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
1dbcd 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
1dbce 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 45 78  {.            Ex
1dbcf 70 72 20 2a 70 45 78 70 72 2c 20 2a 70 52 69 67  pr *pExpr, *pRig
1dbd0 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ht;.            
1dbd1 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54  char *zName = pT
1dbd2 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  ab->aCol[j].zNam
1dbd3 65 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20  e;..            
1dbd4 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 69  /* If a column i
1dbd5 73 20 6d 61 72 6b 65 64 20 61 73 20 27 68 69 64  s marked as 'hid
1dbd6 64 65 6e 27 20 28 63 75 72 72 65 6e 74 6c 79 20  den' (currently 
1dbd7 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 0a 20 20  only possible.  
1dbd8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72            ** for
1dbd9 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 29   virtual tables)
1dbda 2c 20 64 6f 20 6e 6f 74 20 69 6e 63 6c 75 64 65  , do not include
1dbdb 20 69 74 20 69 6e 20 74 68 65 20 65 78 70 61 6e   it in the expan
1dbdc 64 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  ded.            
1dbdd 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 6c 69  ** result-set li
1dbde 73 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  st..            
1dbdf 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  */.            i
1dbe0 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  f( IsHiddenColum
1dbe1 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  n(&pTab->aCol[j]
1dbe2 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
1dbe3 20 20 20 61 73 73 65 72 74 28 49 73 56 69 72 74     assert(IsVirt
1dbe4 75 61 6c 28 70 54 61 62 29 29 3b 0a 20 20 20 20  ual(pTab));.    
1dbe5 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
1dbe6 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
1dbe7 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  }..            i
1dbe8 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20  f( i>0 ){.      
1dbe9 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53          struct S
1dbea 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65  rcList_item *pLe
1dbeb 66 74 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e  ft = &pTabList->
1dbec 61 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20  a[i-1];.        
1dbed 20 20 20 20 20 20 69 66 28 20 28 70 4c 65 66 74        if( (pLeft
1dbee 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a  [1].jointype & J
1dbef 54 5f 4e 41 54 55 52 41 4c 29 21 3d 30 20 26 26  T_NATURAL)!=0 &&
1dbf0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1dbf1 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 49           columnI
1dbf2 6e 64 65 78 28 70 4c 65 66 74 2d 3e 70 54 61 62  ndex(pLeft->pTab
1dbf3 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20  , zName)>=0 ){. 
1dbf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1dbf5 2a 20 49 6e 20 61 20 4e 41 54 55 52 41 4c 20 6a  * In a NATURAL j
1dbf6 6f 69 6e 2c 20 6f 6d 69 74 20 74 68 65 20 6a 6f  oin, omit the jo
1dbf7 69 6e 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20  in columns from 
1dbf8 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20  the .           
1dbf9 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 6f 6e       ** table on
1dbfa 20 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20   the right */.  
1dbfb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
1dbfc 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
1dbfd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1dbfe 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1dbff 33 49 64 4c 69 73 74 49 6e 64 65 78 28 70 4c 65  3IdListIndex(pLe
1dc00 66 74 5b 31 5d 2e 70 55 73 69 6e 67 2c 20 7a 4e  ft[1].pUsing, zN
1dc01 61 6d 65 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20  ame)>=0 ){.     
1dc02 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1dc03 20 61 20 6a 6f 69 6e 20 77 69 74 68 20 61 20 55   a join with a U
1dc04 53 49 4e 47 20 63 6c 61 75 73 65 2c 20 6f 6d 69  SING clause, omi
1dc05 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  t columns in the
1dc06 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1dc07 20 2a 2a 20 75 73 69 6e 67 20 63 6c 61 75 73 65   ** using clause
1dc08 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20   from the table 
1dc09 6f 6e 20 74 68 65 20 72 69 67 68 74 2e 20 2a 2f  on the right. */
1dc0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1dc0b 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
1dc0c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1dc0d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1dc0e 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 73 71       pRight = sq
1dc0f 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
1dc10 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
1dc11 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
1dc12 69 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 20  if( pRight==0 ) 
1dc13 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1dc14 20 20 20 73 65 74 51 75 6f 74 65 64 54 6f 6b 65     setQuotedToke
1dc15 6e 28 70 50 61 72 73 65 2c 20 26 70 52 69 67 68  n(pParse, &pRigh
1dc16 74 2d 3e 74 6f 6b 65 6e 2c 20 7a 4e 61 6d 65 29  t->token, zName)
1dc17 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
1dc18 28 20 7a 54 61 62 4e 61 6d 65 20 26 26 20 28 6c  ( zTabName && (l
1dc19 6f 6e 67 4e 61 6d 65 73 20 7c 7c 20 70 54 61 62  ongNames || pTab
1dc1a 4c 69 73 74 2d 3e 6e 53 72 63 3e 31 29 20 29 7b  List->nSrc>1) ){
1dc1b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45  .              E
1dc1c 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 73 71 6c  xpr *pLeft = sql
1dc1d 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
1dc1e 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 30  , TK_ID, 0, 0, 0
1dc1f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
1dc20 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33   pExpr = sqlite3
1dc21 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
1dc22 5f 44 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52 69  _DOT, pLeft, pRi
1dc23 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ght, 0);.       
1dc24 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72         if( pExpr
1dc25 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
1dc26 20 20 20 20 20 20 20 20 20 20 20 73 65 74 51 75             setQu
1dc27 6f 74 65 64 54 6f 6b 65 6e 28 70 50 61 72 73 65  otedToken(pParse
1dc28 2c 20 26 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2c  , &pLeft->token,
1dc29 20 7a 54 61 62 4e 61 6d 65 29 3b 0a 20 20 20 20   zTabName);.    
1dc2a 20 20 20 20 20 20 20 20 20 20 73 65 74 54 6f 6b            setTok
1dc2b 65 6e 28 26 70 45 78 70 72 2d 3e 73 70 61 6e 2c  en(&pExpr->span,
1dc2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1dc2d 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e      sqlite3MPrin
1dc2e 74 66 28 64 62 2c 20 22 25 73 2e 25 73 22 2c 20  tf(db, "%s.%s", 
1dc2f 7a 54 61 62 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29  zTabName, zName)
1dc30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
1dc31 20 70 45 78 70 72 2d 3e 73 70 61 6e 2e 64 79 6e   pExpr->span.dyn
1dc32 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
1dc33 20 20 20 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e      pExpr->token
1dc34 2e 7a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  .z = 0;.        
1dc35 20 20 20 20 20 20 70 45 78 70 72 2d 3e 74 6f 6b        pExpr->tok
1dc36 65 6e 2e 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  en.n = 0;.      
1dc37 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 74          pExpr->t
1dc38 6f 6b 65 6e 2e 64 79 6e 20 3d 20 30 3b 0a 20 20  oken.dyn = 0;.  
1dc39 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
1dc3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
1dc3b 45 78 70 72 20 3d 20 70 52 69 67 68 74 3b 0a 20  Expr = pRight;. 
1dc3c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
1dc3d 70 72 2d 3e 73 70 61 6e 20 3d 20 70 45 78 70 72  pr->span = pExpr
1dc3e 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20  ->token;.       
1dc3f 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73 70         pExpr->sp
1dc40 61 6e 2e 64 79 6e 20 3d 20 30 3b 0a 20 20 20 20  an.dyn = 0;.    
1dc41 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1dc42 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61        if( longNa
1dc43 6d 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  mes ){.         
1dc44 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
1dc45 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
1dc46 64 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20  d(pParse, pNew, 
1dc47 70 45 78 70 72 2c 20 26 70 45 78 70 72 2d 3e 73  pExpr, &pExpr->s
1dc48 70 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pan);.          
1dc49 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1dc4a 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71         pNew = sq
1dc4b 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
1dc4c 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4e 65 77  end(pParse, pNew
1dc4d 2c 20 70 45 78 70 72 2c 20 26 70 52 69 67 68 74  , pExpr, &pRight
1dc4e 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20  ->token);.      
1dc4f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1dc50 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1dc51 20 20 20 20 20 20 69 66 28 20 21 74 61 62 6c 65        if( !table
1dc52 53 65 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  Seen ){.        
1dc53 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 29 7b 0a    if( zTName ){.
1dc54 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1dc55 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1dc56 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62  se, "no such tab
1dc57 6c 65 3a 20 25 73 22 2c 20 7a 54 4e 61 6d 65 29  le: %s", zTName)
1dc58 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
1dc59 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  e{.            s
1dc5a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1dc5b 50 61 72 73 65 2c 20 22 6e 6f 20 74 61 62 6c 65  Parse, "no table
1dc5c 73 20 73 70 65 63 69 66 69 65 64 22 29 3b 0a 20  s specified");. 
1dc5d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1dc5e 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20       rc = 1;.   
1dc5f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
1dc60 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 4e 61  qlite3_free(zTNa
1dc61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  me);.      }.   
1dc62 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78   }.    sqlite3Ex
1dc63 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 45 4c  prListDelete(pEL
1dc64 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70 45 4c  ist);.    p->pEL
1dc65 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ist = pNew;.  }.
1dc66 20 20 69 66 28 20 70 2d 3e 70 45 4c 69 73 74 20    if( p->pEList 
1dc67 26 26 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  && p->pEList->nE
1dc68 78 70 72 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 43  xpr>SQLITE_MAX_C
1dc69 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 73 71 6c  OLUMN ){.    sql
1dc6a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1dc6b 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63  rse, "too many c
1dc6c 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73 75 6c 74  olumns in result
1dc6d 20 73 65 74 22 29 3b 0a 20 20 20 20 72 63 20 3d   set");.    rc =
1dc6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1dc6f 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c   }.  if( db->mal
1dc70 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1dc71 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
1dc72 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  EM;.  }.  return
1dc73 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20   rc;.}..#ifndef 
1dc74 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
1dc75 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a  OUND_SELECT./*.*
1dc76 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
1dc77 73 73 6f 63 69 61 74 65 73 20 65 6e 74 72 69 65  ssociates entrie
1dc78 73 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59  s in an ORDER BY
1dc79 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1dc7a 20 77 69 74 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 73   with.** columns
1dc7b 20 69 6e 20 61 20 72 65 73 75 6c 74 2e 20 20 46   in a result.  F
1dc7c 6f 72 20 65 61 63 68 20 4f 52 44 45 52 20 42 59  or each ORDER BY
1dc7d 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65   expression, the
1dc7e 20 6f 70 63 6f 64 65 20 6f 66 0a 2a 2a 20 74 68   opcode of.** th
1dc7f 65 20 74 6f 70 2d 6c 65 76 65 6c 20 6e 6f 64 65  e top-level node
1dc80 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 54   is changed to T
1dc81 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 74 68 65  K_COLUMN and the
1dc82 20 69 43 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 6f   iColumn value o
1dc83 66 0a 2a 2a 20 74 68 65 20 74 6f 70 2d 6c 65 76  f.** the top-lev
1dc84 65 6c 20 6e 6f 64 65 20 69 73 20 66 69 6c 6c 65  el node is fille
1dc85 64 20 69 6e 20 77 69 74 68 20 63 6f 6c 75 6d 6e  d in with column
1dc86 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20   number and the 
1dc87 69 54 61 62 6c 65 0a 2a 2a 20 76 61 6c 75 65 20  iTable.** value 
1dc88 6f 66 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c  of the top-level
1dc89 20 6e 6f 64 65 20 69 73 20 66 69 6c 6c 65 64 20   node is filled 
1dc8a 77 69 74 68 20 69 54 61 62 6c 65 20 70 61 72 61  with iTable para
1dc8b 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  meter..**.** If 
1dc8c 74 68 65 72 65 20 61 72 65 20 70 72 69 6f 72 20  there are prior 
1dc8d 53 45 4c 45 43 54 20 63 6c 61 75 73 65 73 2c 20  SELECT clauses, 
1dc8e 74 68 65 79 20 61 72 65 20 70 72 6f 63 65 73 73  they are process
1dc8f 65 64 20 66 69 72 73 74 2e 20 20 41 20 6d 61 74  ed first.  A mat
1dc90 63 68 0a 2a 2a 20 69 6e 20 61 6e 20 65 61 72 6c  ch.** in an earl
1dc91 69 65 72 20 53 45 4c 45 43 54 20 74 61 6b 65 73  ier SELECT takes
1dc92 20 70 72 65 63 65 64 65 6e 63 65 20 6f 76 65 72   precedence over
1dc93 20 61 20 6c 61 74 65 72 20 53 45 4c 45 43 54 2e   a later SELECT.
1dc94 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 65 6e 74 72 79  .**.** Any entry
1dc95 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6d   that does not m
1dc96 61 74 63 68 20 69 73 20 66 6c 61 67 67 65 64 20  atch is flagged 
1dc97 61 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68  as an error.  Th
1dc98 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65  e number.** of e
1dc99 72 72 6f 72 73 20 69 73 20 72 65 74 75 72 6e 65  rrors is returne
1dc9a 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
1dc9b 20 6d 61 74 63 68 4f 72 64 65 72 62 79 54 6f 43   matchOrderbyToC
1dc9c 6f 6c 75 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a  olumn(.  Parse *
1dc9d 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
1dc9e 20 2f 2a 20 41 20 70 6c 61 63 65 20 74 6f 20 6c   /* A place to l
1dc9f 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61  eave error messa
1dca0 67 65 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ges */.  Select 
1dca1 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 20  *pSelect,       
1dca2 20 2f 2a 20 4d 61 74 63 68 20 74 6f 20 72 65 73   /* Match to res
1dca3 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  ult columns of t
1dca4 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  his SELECT */.  
1dca5 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
1dca6 42 79 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 4f  By,     /* The O
1dca7 52 44 45 52 20 42 59 20 76 61 6c 75 65 73 20 74  RDER BY values t
1dca8 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20  o match against 
1dca9 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74  columns */.  int
1dcaa 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20   iTable,        
1dcab 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74       /* Insert t
1dcac 68 69 73 20 76 61 6c 75 65 20 69 6e 20 69 54 61  his value in iTa
1dcad 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 75 73  ble */.  int mus
1dcae 74 43 6f 6d 70 6c 65 74 65 20 20 20 20 20 20 20  tComplete       
1dcaf 20 2f 2a 20 49 66 20 54 52 55 45 20 61 6c 6c 20   /* If TRUE all 
1dcb0 4f 52 44 45 52 20 42 59 73 20 6d 75 73 74 20 6d  ORDER BYs must m
1dcb1 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  atch */.){.  int
1dcb2 20 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 6e 74   nErr = 0;.  int
1dcb3 20 69 2c 20 6a 3b 0a 20 20 45 78 70 72 4c 69 73   i, j;.  ExprLis
1dcb4 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 71 6c  t *pEList;.  sql
1dcb5 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1dcb6 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 53  e->db;..  if( pS
1dcb7 65 6c 65 63 74 3d 3d 30 20 7c 7c 20 70 4f 72 64  elect==0 || pOrd
1dcb8 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e  erBy==0 ) return
1dcb9 20 31 3b 0a 20 20 69 66 28 20 6d 75 73 74 43 6f   1;.  if( mustCo
1dcba 6d 70 6c 65 74 65 20 29 7b 0a 20 20 20 20 66 6f  mplete ){.    fo
1dcbb 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42  r(i=0; i<pOrderB
1dcbc 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 20  y->nExpr; i++){ 
1dcbd 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64  pOrderBy->a[i].d
1dcbe 6f 6e 65 20 3d 20 30 3b 20 7d 0a 20 20 7d 0a 20  one = 0; }.  }. 
1dcbf 20 69 66 28 20 70 72 65 70 53 65 6c 65 63 74 53   if( prepSelectS
1dcc0 74 6d 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  tmt(pParse, pSel
1dcc1 65 63 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ect) ){.    retu
1dcc2 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 1;.  }.  if( 
1dcc3 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20  pSelect->pPrior 
1dcc4 29 7b 0a 20 20 20 20 69 66 28 20 6d 61 74 63 68  ){.    if( match
1dcc5 4f 72 64 65 72 62 79 54 6f 43 6f 6c 75 6d 6e 28  OrderbyToColumn(
1dcc6 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2d  pParse, pSelect-
1dcc7 3e 70 50 72 69 6f 72 2c 20 70 4f 72 64 65 72 42  >pPrior, pOrderB
1dcc8 79 2c 20 69 54 61 62 6c 65 2c 20 30 29 20 29 7b  y, iTable, 0) ){
1dcc9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
1dcca 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 45 4c  .    }.  }.  pEL
1dccb 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ist = pSelect->p
1dccc 45 4c 69 73 74 3b 0a 20 20 66 6f 72 28 69 3d 30  EList;.  for(i=0
1dccd 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ; i<pOrderBy->nE
1dcce 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  xpr; i++){.    s
1dccf 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
1dcd0 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20  tem *pItem;.    
1dcd1 45 78 70 72 20 2a 70 45 20 3d 20 70 4f 72 64 65  Expr *pE = pOrde
1dcd2 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  rBy->a[i].pExpr;
1dcd3 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20  .    int iCol = 
1dcd4 2d 31 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4c  -1;.    char *zL
1dcd5 61 62 65 6c 3b 0a 0a 20 20 20 20 69 66 28 20 70  abel;..    if( p
1dcd6 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f  OrderBy->a[i].do
1dcd7 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ne ) continue;. 
1dcd8 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
1dcd9 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20  prIsInteger(pE, 
1dcda 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  &iCol) ){.      
1dcdb 69 66 28 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20 69  if( iCol<=0 || i
1dcdc 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  Col>pEList->nExp
1dcdd 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  r ){.        sql
1dcde 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1dcdf 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22  rse,.          "
1dce0 4f 52 44 45 52 20 42 59 20 70 6f 73 69 74 69 6f  ORDER BY positio
1dce1 6e 20 25 64 20 73 68 6f 75 6c 64 20 62 65 20 62  n %d should be b
1dce2 65 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22  etween 1 and %d"
1dce3 2c 0a 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c  ,.          iCol
1dce4 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
1dce5 3b 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b  ;.        nErr++
1dce6 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1dce7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1dce8 66 28 20 21 6d 75 73 74 43 6f 6d 70 6c 65 74 65  f( !mustComplete
1dce9 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1dcea 20 20 20 69 43 6f 6c 2d 2d 3b 0a 20 20 20 20 7d     iCol--;.    }
1dceb 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20  .    if( iCol<0 
1dcec 26 26 20 28 7a 4c 61 62 65 6c 20 3d 20 73 71 6c  && (zLabel = sql
1dced 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
1dcee 6e 28 64 62 2c 20 26 70 45 2d 3e 74 6f 6b 65 6e  n(db, &pE->token
1dcef 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 66  ))!=0 ){.      f
1dcf0 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 45  or(j=0, pItem=pE
1dcf1 4c 69 73 74 2d 3e 61 3b 20 6a 3c 70 45 4c 69 73  List->a; j<pELis
1dcf2 74 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 2c 20 70  t->nExpr; j++, p
1dcf3 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
1dcf4 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
1dcf5 20 20 20 20 20 20 69 6e 74 20 69 73 4d 61 74 63        int isMatc
1dcf6 68 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  h;.        if( p
1dcf7 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20  Item->zName ){. 
1dcf8 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d           zName =
1dcf9 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
1dcfa 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  (db, pItem->zNam
1dcfb 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  e);.        }els
1dcfc 65 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 4e 61  e{.          zNa
1dcfd 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  me = sqlite3Name
1dcfe 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70  FromToken(db, &p
1dcff 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 74 6f 6b  Item->pExpr->tok
1dd00 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  en);.        }. 
1dd01 20 20 20 20 20 20 20 69 73 4d 61 74 63 68 20 3d         isMatch =
1dd02 20 7a 4e 61 6d 65 20 26 26 20 73 71 6c 69 74 65   zName && sqlite
1dd03 33 53 74 72 49 43 6d 70 28 7a 4e 61 6d 65 2c 20  3StrICmp(zName, 
1dd04 7a 4c 61 62 65 6c 29 3d 3d 30 3b 0a 20 20 20 20  zLabel)==0;.    
1dd05 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1dd06 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  (zName);.       
1dd07 20 69 66 28 20 69 73 4d 61 74 63 68 20 29 7b 0a   if( isMatch ){.
1dd08 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d            iCol =
1dd09 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   j;.          br
1dd0a 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
1dd0b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
1dd0c 69 74 65 33 5f 66 72 65 65 28 7a 4c 61 62 65 6c  ite3_free(zLabel
1dd0d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
1dd0e 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20   iCol>=0 ){.    
1dd0f 20 20 70 45 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f    pE->op = TK_CO
1dd10 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 70 45 2d 3e  LUMN;.      pE->
1dd11 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a  iColumn = iCol;.
1dd12 20 20 20 20 20 20 70 45 2d 3e 69 54 61 62 6c 65        pE->iTable
1dd13 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 20   = iTable;.     
1dd14 20 70 45 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a   pE->iAgg = -1;.
1dd15 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e        pOrderBy->
1dd16 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 31 3b 0a 20  a[i].done = 1;. 
1dd17 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 75 73     }else if( mus
1dd18 74 43 6f 6d 70 6c 65 74 65 20 29 7b 0a 20 20 20  tComplete ){.   
1dd19 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1dd1a 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  sg(pParse,.     
1dd1b 20 20 20 22 4f 52 44 45 52 20 42 59 20 74 65 72     "ORDER BY ter
1dd1c 6d 20 6e 75 6d 62 65 72 20 25 64 20 64 6f 65 73  m number %d does
1dd1d 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 72   not match any r
1dd1e 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 22 2c 20 69  esult column", i
1dd1f 2b 31 29 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b  +1);.      nErr+
1dd20 2b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  +;.      break;.
1dd21 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1dd22 72 6e 20 6e 45 72 72 3b 20 20 0a 7d 0a 23 65 6e  rn nErr;  .}.#en
1dd23 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53  dif /* #ifndef S
1dd24 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
1dd25 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f  UND_SELECT */../
1dd26 2a 0a 2a 2a 20 47 65 74 20 61 20 56 44 42 45 20  *.** Get a VDBE 
1dd27 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 61  for the given pa
1dd28 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 43  rser context.  C
1dd29 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 20  reate a new one 
1dd2a 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a  if necessary..**
1dd2b 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
1dd2c 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c  urs, return NULL
1dd2d 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d 65 73   and leave a mes
1dd2e 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2e 0a  sage in pParse..
1dd2f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1dd30 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33 47  E Vdbe *sqlite3G
1dd31 65 74 56 64 62 65 28 50 61 72 73 65 20 2a 70 50  etVdbe(Parse *pP
1dd32 61 72 73 65 29 7b 0a 20 20 56 64 62 65 20 2a 76  arse){.  Vdbe *v
1dd33 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
1dd34 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  ;.  if( v==0 ){.
1dd35 20 20 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e      v = pParse->
1dd36 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 56  pVdbe = sqlite3V
1dd37 64 62 65 43 72 65 61 74 65 28 70 50 61 72 73 65  dbeCreate(pParse
1dd38 2d 3e 64 62 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ->db);.  }.  ret
1dd39 75 72 6e 20 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn v;.}.../*.**
1dd3a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 69 4c 69   Compute the iLi
1dd3b 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20  mit and iOffset 
1dd3c 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 53 45  fields of the SE
1dd3d 4c 45 43 54 20 62 61 73 65 64 20 6f 6e 20 74 68  LECT based on th
1dd3e 65 0a 2a 2a 20 70 4c 69 6d 69 74 20 61 6e 64 20  e.** pLimit and 
1dd3f 70 4f 66 66 73 65 74 20 65 78 70 72 65 73 73 69  pOffset expressi
1dd40 6f 6e 73 2e 20 20 70 4c 69 6d 69 74 20 61 6e 64  ons.  pLimit and
1dd41 20 70 4f 66 66 73 65 74 20 68 6f 6c 64 20 74 68   pOffset hold th
1dd42 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a  e expressions.**
1dd43 20 74 68 61 74 20 61 70 70 65 61 72 20 69 6e 20   that appear in 
1dd44 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
1dd45 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72   statement after
1dd46 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f   the LIMIT and O
1dd47 46 46 53 45 54 0a 2a 2a 20 6b 65 79 77 6f 72 64  FFSET.** keyword
1dd48 73 2e 20 20 4f 72 20 4e 55 4c 4c 20 69 66 20 74  s.  Or NULL if t
1dd49 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 61 72  hose keywords ar
1dd4a 65 20 6f 6d 69 74 74 65 64 2e 20 69 4c 69 6d 69  e omitted. iLimi
1dd4b 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 0a 2a  t and iOffset .*
1dd4c 2a 20 61 72 65 20 74 68 65 20 69 6e 74 65 67 65  * are the intege
1dd4d 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65  r memory registe
1dd4e 72 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 63 6f  r numbers for co
1dd4f 75 6e 74 65 72 73 20 75 73 65 64 20 74 6f 20 63  unters used to c
1dd50 6f 6d 70 75 74 65 20 0a 2a 2a 20 74 68 65 20 6c  ompute .** the l
1dd51 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 2e  imit and offset.
1dd52 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
1dd53 20 6c 69 6d 69 74 20 61 6e 64 2f 6f 72 20 6f 66   limit and/or of
1dd54 66 73 65 74 2c 20 74 68 65 6e 20 0a 2a 2a 20 69  fset, then .** i
1dd55 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
1dd56 74 20 61 72 65 20 6e 65 67 61 74 69 76 65 2e 0a  t are negative..
1dd57 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1dd58 6e 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 76  ne changes the v
1dd59 61 6c 75 65 73 20 6f 66 20 69 4c 69 6d 69 74 20  alues of iLimit 
1dd5a 61 6e 64 20 69 4f 66 66 73 65 74 20 6f 6e 6c 79  and iOffset only
1dd5b 20 69 66 0a 2a 2a 20 61 20 6c 69 6d 69 74 20 6f   if.** a limit o
1dd5c 72 20 6f 66 66 73 65 74 20 69 73 20 64 65 66 69  r offset is defi
1dd5d 6e 65 64 20 62 79 20 70 4c 69 6d 69 74 20 61 6e  ned by pLimit an
1dd5e 64 20 70 4f 66 66 73 65 74 2e 20 20 69 4c 69 6d  d pOffset.  iLim
1dd5f 69 74 20 61 6e 64 0a 2a 2a 20 69 4f 66 66 73 65  it and.** iOffse
1dd60 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  t should have be
1dd61 65 6e 20 70 72 65 73 65 74 20 74 6f 20 61 70 70  en preset to app
1dd62 72 6f 70 72 69 61 74 65 20 64 65 66 61 75 6c 74  ropriate default
1dd63 20 76 61 6c 75 65 73 0a 2a 2a 20 28 75 73 75 61   values.** (usua
1dd64 6c 6c 79 20 62 75 74 20 6e 6f 74 20 61 6c 77 61  lly but not alwa
1dd65 79 73 20 2d 31 29 20 70 72 69 6f 72 20 74 6f 20  ys -1) prior to 
1dd66 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
1dd67 74 69 6e 65 2e 0a 2a 2a 20 4f 6e 6c 79 20 69 66  tine..** Only if
1dd68 20 70 4c 69 6d 69 74 21 3d 30 20 6f 72 20 70 4f   pLimit!=0 or pO
1dd69 66 66 73 65 74 21 3d 30 20 64 6f 20 74 68 65 20  ffset!=0 do the 
1dd6a 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72 73 20  limit registers 
1dd6b 67 65 74 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64  get.** redefined
1dd6c 2e 20 20 54 68 65 20 55 4e 49 4f 4e 20 41 4c 4c  .  The UNION ALL
1dd6d 20 6f 70 65 72 61 74 6f 72 20 75 73 65 73 20 74   operator uses t
1dd6e 68 69 73 20 70 72 6f 70 65 72 74 79 20 74 6f 20  his property to 
1dd6f 66 6f 72 63 65 0a 2a 2a 20 74 68 65 20 72 65 75  force.** the reu
1dd70 73 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c  se of the same l
1dd71 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20  imit and offset 
1dd72 72 65 67 69 73 74 65 72 73 20 61 63 72 6f 73 73  registers across
1dd73 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 53 45 4c   multiple.** SEL
1dd74 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ECT statements..
1dd75 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
1dd76 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73  omputeLimitRegis
1dd77 74 65 72 73 28 50 61 72 73 65 20 2a 70 50 61 72  ters(Parse *pPar
1dd78 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69  se, Select *p, i
1dd79 6e 74 20 69 42 72 65 61 6b 29 7b 0a 20 20 56 64  nt iBreak){.  Vd
1dd7a 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74  be *v = 0;.  int
1dd7b 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 69   iLimit = 0;.  i
1dd7c 6e 74 20 69 4f 66 66 73 65 74 3b 0a 20 20 69 6e  nt iOffset;.  in
1dd7d 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a  t addr1, addr2;.
1dd7e 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 22 4c 49 4d  .  /* .  ** "LIM
1dd7f 49 54 20 2d 31 22 20 61 6c 77 61 79 73 20 73 68  IT -1" always sh
1dd80 6f 77 73 20 61 6c 6c 20 72 6f 77 73 2e 20 20 54  ows all rows.  T
1dd81 68 65 72 65 20 69 73 20 73 6f 6d 65 0a 20 20 2a  here is some.  *
1dd82 2a 20 63 6f 6e 74 72 61 76 65 72 73 79 20 61 62  * contraversy ab
1dd83 6f 75 74 20 77 68 61 74 20 74 68 65 20 63 6f 72  out what the cor
1dd84 72 65 63 74 20 62 65 68 61 76 69 6f 72 20 73 68  rect behavior sh
1dd85 6f 75 6c 64 20 62 65 2e 0a 20 20 2a 2a 20 54 68  ould be..  ** Th
1dd86 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d  e current implem
1dd87 65 6e 74 61 74 69 6f 6e 20 69 6e 74 65 72 70 72  entation interpr
1dd88 65 74 73 20 22 4c 49 4d 49 54 20 30 22 20 74 6f  ets "LIMIT 0" to
1dd89 20 6d 65 61 6e 0a 20 20 2a 2a 20 6e 6f 20 72 6f   mean.  ** no ro
1dd8a 77 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ws..  */.  if( p
1dd8b 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  ->pLimit ){.    
1dd8c 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 69 4c 69 6d  p->iLimit = iLim
1dd8d 69 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  it = pParse->nMe
1dd8e 6d 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  m;.    pParse->n
1dd8f 4d 65 6d 20 2b 3d 20 32 3b 0a 20 20 20 20 76 20  Mem += 2;.    v 
1dd90 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1dd91 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
1dd92 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
1dd93 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
1dd94 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d 3e  Code(pParse, p->
1dd95 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 73 71 6c  pLimit);.    sql
1dd96 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1dd97 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 30   OP_MustBeInt, 0
1dd98 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1dd99 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1dd9a 5f 4d 65 6d 53 74 6f 72 65 2c 20 69 4c 69 6d 69  _MemStore, iLimi
1dd9b 74 2c 20 31 29 3b 0a 20 20 20 20 56 64 62 65 43  t, 1);.    VdbeC
1dd9c 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 4c 49  omment((v, "# LI
1dd9d 4d 49 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a  MIT counter"));.
1dd9e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1dd9f 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66 4d 65 6d  ddOp(v, OP_IfMem
1dda0 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 2c 20 69 42  Zero, iLimit, iB
1dda1 72 65 61 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74  reak);.    sqlit
1dda2 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1dda3 50 5f 4d 65 6d 4c 6f 61 64 2c 20 69 4c 69 6d 69  P_MemLoad, iLimi
1dda4 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  t, 0);.  }.  if(
1dda5 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b 0a 20   p->pOffset ){. 
1dda6 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20     p->iOffset = 
1dda7 69 4f 66 66 73 65 74 20 3d 20 70 50 61 72 73 65  iOffset = pParse
1dda8 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 76 20  ->nMem++;.    v 
1dda9 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1ddaa 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
1ddab 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
1ddac 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
1ddad 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d 3e  Code(pParse, p->
1ddae 70 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71  pOffset);.    sq
1ddaf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ddb0 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
1ddb1 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
1ddb2 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ddb3 50 5f 4d 65 6d 53 74 6f 72 65 2c 20 69 4f 66 66  P_MemStore, iOff
1ddb4 73 65 74 2c 20 70 2d 3e 70 4c 69 6d 69 74 3d 3d  set, p->pLimit==
1ddb5 30 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  0);.    VdbeComm
1ddb6 65 6e 74 28 28 76 2c 20 22 23 20 4f 46 46 53 45  ent((v, "# OFFSE
1ddb7 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20  T counter"));.  
1ddb8 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
1ddb9 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1ddba 5f 49 66 4d 65 6d 50 6f 73 2c 20 69 4f 66 66 73  _IfMemPos, iOffs
1ddbb 65 74 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  et, 0);.    sqli
1ddbc 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1ddbd 4f 50 5f 50 6f 70 2c 20 31 2c 20 30 29 3b 0a 20  OP_Pop, 1, 0);. 
1ddbe 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ddbf 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
1ddc0 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  r, 0, 0);.    sq
1ddc1 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1ddc2 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20  e(v, addr1);.   
1ddc3 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29   if( p->pLimit )
1ddc4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1ddc5 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 41  dbeAddOp(v, OP_A
1ddc6 64 64 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d  dd, 0, 0);.    }
1ddc7 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4c  .  }.  if( p->pL
1ddc8 69 6d 69 74 20 29 7b 0a 20 20 20 20 61 64 64 72  imit ){.    addr
1ddc9 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
1ddca 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66 4d 65 6d  ddOp(v, OP_IfMem
1ddcb 50 6f 73 2c 20 69 4c 69 6d 69 74 2c 20 30 29 3b  Pos, iLimit, 0);
1ddcc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ddcd 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c  AddOp(v, OP_Pop,
1ddce 20 31 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   1, 0);.    sqli
1ddcf 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1ddd0 4f 50 5f 4d 65 6d 49 6e 74 2c 20 2d 31 2c 20 69  OP_MemInt, -1, i
1ddd1 4c 69 6d 69 74 2b 31 29 3b 0a 20 20 20 20 61 64  Limit+1);.    ad
1ddd2 64 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr2 = sqlite3Vdb
1ddd3 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74  eAddOp(v, OP_Got
1ddd4 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  o, 0, 0);.    sq
1ddd5 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1ddd6 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20  e(v, addr1);.   
1ddd7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ddd8 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72 65  p(v, OP_MemStore
1ddd9 2c 20 69 4c 69 6d 69 74 2b 31 2c 20 31 29 3b 0a  , iLimit+1, 1);.
1ddda 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
1dddb 28 76 2c 20 22 23 20 4c 49 4d 49 54 2b 4f 46 46  (v, "# LIMIT+OFF
1dddc 53 45 54 22 29 29 3b 0a 20 20 20 20 73 71 6c 69  SET"));.    sqli
1dddd 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1ddde 76 2c 20 61 64 64 72 32 29 3b 0a 20 20 7d 0a 7d  v, addr2);.  }.}
1dddf 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
1dde0 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78   a virtual index
1dde1 20 74 6f 20 75 73 65 20 66 6f 72 20 73 6f 72 74   to use for sort
1dde2 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ing..*/.static v
1dde3 6f 69 64 20 63 72 65 61 74 65 53 6f 72 74 69 6e  oid createSortin
1dde4 67 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50  gIndex(Parse *pP
1dde5 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c  arse, Select *p,
1dde6 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
1dde7 72 42 79 29 7b 0a 20 20 69 66 28 20 70 4f 72 64  rBy){.  if( pOrd
1dde8 65 72 42 79 20 29 7b 0a 20 20 20 20 69 6e 74 20  erBy ){.    int 
1dde9 61 64 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74  addr;.    assert
1ddea 28 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  ( pOrderBy->iECu
1ddeb 72 73 6f 72 3d 3d 30 20 29 3b 0a 20 20 20 20 70  rsor==0 );.    p
1ddec 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
1dded 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
1ddee 2b 2b 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73  ++;.    addr = s
1ddef 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1ddf0 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f  pParse->pVdbe, O
1ddf1 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
1ddf2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1ddf3 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 72               pOr
1ddf4 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c  derBy->iECursor,
1ddf5 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
1ddf6 2b 31 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  +1);.    assert(
1ddf7 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
1ddf8 5b 32 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20  [2] == -1 );.   
1ddf9 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
1ddfa 5b 32 5d 20 3d 20 61 64 64 72 3b 0a 20 20 7d 0a  [2] = addr;.  }.
1ddfb 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1ddfc 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
1ddfd 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 52 65 74  SELECT./*.** Ret
1ddfe 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69  urn the appropri
1ddff 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ate collating se
1de00 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 69  quence for the i
1de01 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  Col-th column of
1de02 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73  .** the result s
1de03 65 74 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f  et for the compo
1de04 75 6e 64 2d 73 65 6c 65 63 74 20 73 74 61 74 65  und-select state
1de05 6d 65 6e 74 20 22 70 22 2e 20 20 52 65 74 75 72  ment "p".  Retur
1de06 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 74 68 65  n NULL if.** the
1de07 20 63 6f 6c 75 6d 6e 20 68 61 73 20 6e 6f 20 64   column has no d
1de08 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67  efault collating
1de09 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a   sequence..**.**
1de0a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   The collating s
1de0b 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20  equence for the 
1de0c 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20  compound select 
1de0d 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68  is taken from th
1de0e 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74  e.** left-most t
1de0f 65 72 6d 20 6f 66 20 74 68 65 20 73 65 6c 65 63  erm of the selec
1de10 74 20 74 68 61 74 20 68 61 73 20 61 20 63 6f 6c  t that has a col
1de11 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e  lating sequence.
1de12 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53  .*/.static CollS
1de13 65 71 20 2a 6d 75 6c 74 69 53 65 6c 65 63 74 43  eq *multiSelectC
1de14 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50  ollSeq(Parse *pP
1de15 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c  arse, Select *p,
1de16 20 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 20 43 6f   int iCol){.  Co
1de17 6c 6c 53 65 71 20 2a 70 52 65 74 3b 0a 20 20 69  llSeq *pRet;.  i
1de18 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a  f( p->pPrior ){.
1de19 20 20 20 20 70 52 65 74 20 3d 20 6d 75 6c 74 69      pRet = multi
1de1a 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50  SelectCollSeq(pP
1de1b 61 72 73 65 2c 20 70 2d 3e 70 50 72 69 6f 72 2c  arse, p->pPrior,
1de1c 20 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b   iCol);.  }else{
1de1d 0a 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20  .    pRet = 0;. 
1de1e 20 7d 0a 20 20 69 66 28 20 70 52 65 74 3d 3d 30   }.  if( pRet==0
1de1f 20 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 73   ){.    pRet = s
1de20 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
1de21 71 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c  q(pParse, p->pEL
1de22 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78  ist->a[iCol].pEx
1de23 70 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  pr);.  }.  retur
1de24 6e 20 70 52 65 74 3b 0a 7d 0a 23 65 6e 64 69 66  n pRet;.}.#endif
1de25 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1de26 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20  COMPOUND_SELECT 
1de27 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1de28 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
1de29 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 54 68  _SELECT./*.** Th
1de2a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1de2b 6c 6c 65 64 20 74 6f 20 70 72 6f 63 65 73 73 20  lled to process 
1de2c 61 20 71 75 65 72 79 20 74 68 61 74 20 69 73 20  a query that is 
1de2d 72 65 61 6c 6c 79 20 74 68 65 20 75 6e 69 6f 6e  really the union
1de2e 0a 2a 2a 20 6f 72 20 69 6e 74 65 72 73 65 63 74  .** or intersect
1de2f 69 6f 6e 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f  ion of two or mo
1de30 72 65 20 73 65 70 61 72 61 74 65 20 71 75 65 72  re separate quer
1de31 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 22 70 22 20 70  ies..**.** "p" p
1de32 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 72 69 67  oints to the rig
1de33 68 74 2d 6d 6f 73 74 20 6f 66 20 74 68 65 20 74  ht-most of the t
1de34 77 6f 20 71 75 65 72 69 65 73 2e 20 20 74 68 65  wo queries.  the
1de35 20 71 75 65 72 79 20 6f 6e 20 74 68 65 0a 2a 2a   query on the.**
1de36 20 6c 65 66 74 20 69 73 20 70 2d 3e 70 50 72 69   left is p->pPri
1de37 6f 72 2e 20 20 54 68 65 20 6c 65 66 74 20 71 75  or.  The left qu
1de38 65 72 79 20 63 6f 75 6c 64 20 61 6c 73 6f 20 62  ery could also b
1de39 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65  e a compound que
1de3a 72 79 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63  ry.** in which c
1de3b 61 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ase this routine
1de3c 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
1de3d 72 65 63 75 72 73 69 76 65 6c 79 2e 20 0a 2a 2a  recursively. .**
1de3e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20  .** The results 
1de3f 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65  of the total que
1de40 72 79 20 61 72 65 20 74 6f 20 62 65 20 77 72 69  ry are to be wri
1de41 74 74 65 6e 20 69 6e 74 6f 20 61 20 64 65 73 74  tten into a dest
1de42 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 79  ination.** of ty
1de43 70 65 20 65 44 65 73 74 20 77 69 74 68 20 70 61  pe eDest with pa
1de44 72 61 6d 65 74 65 72 20 69 50 61 72 6d 2e 0a 2a  rameter iParm..*
1de45 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 20 31 3a 20  *.** Example 1: 
1de46 20 43 6f 6e 73 69 64 65 72 20 61 20 74 68 72 65   Consider a thre
1de47 65 2d 77 61 79 20 63 6f 6d 70 6f 75 6e 64 20 53  e-way compound S
1de48 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  QL statement..**
1de49 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61  .**     SELECT a
1de4a 20 46 52 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 53   FROM t1 UNION S
1de4b 45 4c 45 43 54 20 62 20 46 52 4f 4d 20 74 32 20  ELECT b FROM t2 
1de4c 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 63 20 46  UNION SELECT c F
1de4d 52 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a 20 54 68 69  ROM t3.**.** Thi
1de4e 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70  s statement is p
1de4f 61 72 73 65 64 20 75 70 20 61 73 20 66 6f 6c 6c  arsed up as foll
1de50 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ows:.**.**     S
1de51 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0a  ELECT c FROM t3.
1de52 2a 2a 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20  **      |.**    
1de53 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43    `----->  SELEC
1de54 54 20 62 20 46 52 4f 4d 20 74 32 0a 2a 2a 20 20  T b FROM t2.**  
1de55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a                |.
1de56 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1de57 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45    `------>  SELE
1de58 43 54 20 61 20 46 52 4f 4d 20 74 31 0a 2a 2a 0a  CT a FROM t1.**.
1de59 2a 2a 20 54 68 65 20 61 72 72 6f 77 73 20 69 6e  ** The arrows in
1de5a 20 74 68 65 20 64 69 61 67 72 61 6d 20 61 62 6f   the diagram abo
1de5b 76 65 20 72 65 70 72 65 73 65 6e 74 20 74 68 65  ve represent the
1de5c 20 53 65 6c 65 63 74 2e 70 50 72 69 6f 72 20 70   Select.pPrior p
1de5d 6f 69 6e 74 65 72 2e 0a 2a 2a 20 53 6f 20 69 66  ointer..** So if
1de5e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1de5f 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 20 65   called with p e
1de60 71 75 61 6c 20 74 6f 20 74 68 65 20 74 33 20 71  qual to the t3 q
1de61 75 65 72 79 2c 20 74 68 65 6e 0a 2a 2a 20 70 50  uery, then.** pP
1de62 72 69 6f 72 20 77 69 6c 6c 20 62 65 20 74 68 65  rior will be the
1de63 20 74 32 20 71 75 65 72 79 2e 20 20 70 2d 3e 6f   t2 query.  p->o
1de64 70 20 77 69 6c 6c 20 62 65 20 54 4b 5f 55 4e 49  p will be TK_UNI
1de65 4f 4e 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ON in this case.
1de66 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65 20 74 68  .**.** Notice th
1de67 61 74 20 62 65 63 61 75 73 65 20 6f 66 20 74 68  at because of th
1de68 65 20 77 61 79 20 53 51 4c 69 74 65 20 70 61 72  e way SQLite par
1de69 73 65 73 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  ses compound SEL
1de6a 45 43 54 73 2c 20 74 68 65 0a 2a 2a 20 69 6e 64  ECTs, the.** ind
1de6b 69 76 69 64 75 61 6c 20 73 65 6c 65 63 74 73 20  ividual selects 
1de6c 61 6c 77 61 79 73 20 67 72 6f 75 70 20 66 72 6f  always group fro
1de6d 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2e  m left to right.
1de6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
1de6f 75 6c 74 69 53 65 6c 65 63 74 28 0a 20 20 50 61  ultiSelect(.  Pa
1de70 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1de71 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1de72 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
1de73 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
1de74 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d    /* The right-m
1de75 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74  ost of SELECTs t
1de76 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20  o be coded */.  
1de77 69 6e 74 20 65 44 65 73 74 2c 20 20 20 20 20 20  int eDest,      
1de78 20 20 20 20 20 20 2f 2a 20 5c 5f 5f 5f 20 20 53        /* \___  S
1de79 74 6f 72 65 20 71 75 65 72 79 20 72 65 73 75 6c  tore query resul
1de7a 74 73 20 61 73 20 73 70 65 63 69 66 69 65 64 20  ts as specified 
1de7b 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 2c 20  */.  int iParm, 
1de7c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2f 20             /* / 
1de7d 20 20 20 20 62 79 20 74 68 65 73 65 20 74 77 6f      by these two
1de7e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 20 20   parameters.    
1de7f 20 20 20 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a       */.  char *
1de80 61 66 66 20 20 20 20 20 20 20 20 20 20 20 20 20  aff             
1de81 2f 2a 20 49 66 20 65 44 65 73 74 20 69 73 20 53  /* If eDest is S
1de82 52 54 5f 55 6e 69 6f 6e 2c 20 74 68 65 20 61 66  RT_Union, the af
1de83 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 2a 2f  finity string */
1de84 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
1de85 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 53  QLITE_OK;   /* S
1de86 75 63 63 65 73 73 20 63 6f 64 65 20 66 72 6f 6d  uccess code from
1de87 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f   a subroutine */
1de88 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f  .  Select *pPrio
1de89 72 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 6f 74  r;       /* Anot
1de8a 68 65 72 20 53 45 4c 45 43 54 20 69 6d 6d 65 64  her SELECT immed
1de8b 69 61 74 65 6c 79 20 74 6f 20 6f 75 72 20 6c 65  iately to our le
1de8c 66 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  ft */.  Vdbe *v;
1de8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1de8e 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
1de8f 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20  o this VDBE */. 
1de90 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
1de91 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1de92 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
1de93 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
1de94 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
1de95 64 65 72 42 79 3b 20 20 20 2f 2a 20 54 68 65 20  derBy;   /* The 
1de96 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
1de97 6f 6e 20 70 20 2a 2f 0a 20 20 69 6e 74 20 61 53  on p */.  int aS
1de98 65 74 50 32 5b 32 5d 3b 20 20 20 20 20 20 20 20  etP2[2];        
1de99 2f 2a 20 53 65 74 20 50 32 20 76 61 6c 75 65 20  /* Set P2 value 
1de9a 6f 66 20 74 68 65 73 65 20 6f 70 20 74 6f 20 6e  of these op to n
1de9b 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1de9c 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 74 50 32   */.  int nSetP2
1de9d 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e   = 0;       /* N
1de9e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 69  umber of slots i
1de9f 6e 20 61 53 65 74 50 32 5b 5d 20 75 73 65 64 20  n aSetP2[] used 
1dea0 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  */..  /* Make su
1dea1 72 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f  re there is no O
1dea2 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49 54  RDER BY or LIMIT
1dea3 20 63 6c 61 75 73 65 20 6f 6e 20 70 72 69 6f 72   clause on prior
1dea4 20 53 45 4c 45 43 54 73 2e 20 20 4f 6e 6c 79 0a   SELECTs.  Only.
1dea5 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 28 72    ** the last (r
1dea6 69 67 68 74 2d 6d 6f 73 74 29 20 53 45 4c 45 43  ight-most) SELEC
1dea7 54 20 69 6e 20 74 68 65 20 73 65 72 69 65 73 20  T in the series 
1dea8 6d 61 79 20 68 61 76 65 20 61 6e 20 4f 52 44 45  may have an ORDE
1dea9 52 20 42 59 20 6f 72 20 4c 49 4d 49 54 2e 0a 20  R BY or LIMIT.. 
1deaa 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c   */.  if( p==0 |
1deab 7c 20 70 2d 3e 70 50 72 69 6f 72 3d 3d 30 20 29  | p->pPrior==0 )
1deac 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  {.    rc = 1;.  
1dead 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
1deae 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70  ect_end;.  }.  p
1deaf 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f  Prior = p->pPrio
1deb0 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72  r;.  assert( pPr
1deb1 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 21  ior->pRightmost!
1deb2 3d 70 50 72 69 6f 72 20 29 3b 0a 20 20 61 73 73  =pPrior );.  ass
1deb3 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69  ert( pPrior->pRi
1deb4 67 68 74 6d 6f 73 74 3d 3d 70 2d 3e 70 52 69 67  ghtmost==p->pRig
1deb5 68 74 6d 6f 73 74 20 29 3b 0a 20 20 69 66 28 20  htmost );.  if( 
1deb6 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79  pPrior->pOrderBy
1deb7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1deb8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
1deb9 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
1deba 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65  should come afte
1debb 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22  r %s not before"
1debc 2c 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70  ,.      selectOp
1debd 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20  Name(p->op));.  
1debe 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f    rc = 1;.    go
1debf 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
1dec0 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  end;.  }.  if( p
1dec1 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 7b  Prior->pLimit ){
1dec2 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1dec3 72 4d 73 67 28 70 50 61 72 73 65 2c 22 4c 49 4d  rMsg(pParse,"LIM
1dec4 49 54 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64  IT clause should
1dec5 20 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e   come after %s n
1dec6 6f 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20  ot before",.    
1dec7 20 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70    selectOpName(p
1dec8 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d  ->op));.    rc =
1dec9 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c   1;.    goto mul
1deca 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
1decb 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
1decc 72 65 20 77 65 20 68 61 76 65 20 61 20 76 61 6c  re we have a val
1decd 69 64 20 71 75 65 72 79 20 65 6e 67 69 6e 65 2e  id query engine.
1dece 20 20 49 66 20 6e 6f 74 2c 20 63 72 65 61 74 65    If not, create
1decf 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f   a new one..  */
1ded0 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
1ded1 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
1ded2 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20   if( v==0 ){.   
1ded3 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74   rc = 1;.    got
1ded4 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
1ded5 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72  nd;.  }..  /* Cr
1ded6 65 61 74 65 20 74 68 65 20 64 65 73 74 69 6e 61  eate the destina
1ded7 74 69 6f 6e 20 74 65 6d 70 6f 72 61 72 79 20 74  tion temporary t
1ded8 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72  able if necessar
1ded9 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 44 65  y.  */.  if( eDe
1deda 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62  st==SRT_EphemTab
1dedb 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1dedc 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20  p->pEList );.   
1dedd 20 61 73 73 65 72 74 28 20 6e 53 65 74 50 32 3c   assert( nSetP2<
1dede 73 69 7a 65 6f 66 28 61 53 65 74 50 32 29 2f 73  sizeof(aSetP2)/s
1dedf 69 7a 65 6f 66 28 61 53 65 74 50 32 5b 30 5d 29  izeof(aSetP2[0])
1dee0 20 29 3b 0a 20 20 20 20 61 53 65 74 50 32 5b 6e   );.    aSetP2[n
1dee1 53 65 74 50 32 2b 2b 5d 20 3d 20 73 71 6c 69 74  SetP2++] = sqlit
1dee2 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1dee3 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
1dee4 20 69 50 61 72 6d 2c 20 30 29 3b 0a 20 20 20 20   iParm, 0);.    
1dee5 65 44 65 73 74 20 3d 20 53 52 54 5f 54 61 62 6c  eDest = SRT_Tabl
1dee6 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e  e;.  }..  /* Gen
1dee7 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
1dee8 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68  he left and righ
1dee9 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  t SELECT stateme
1deea 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 4f 72 64  nts..  */.  pOrd
1deeb 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
1deec 42 79 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d  By;.  switch( p-
1deed 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
1deee 54 4b 5f 41 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  TK_ALL: {.      
1deef 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20  if( pOrderBy==0 
1def0 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61  ){.        int a
1def1 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ddr = 0;.       
1def2 20 61 73 73 65 72 74 28 20 21 70 50 72 69 6f 72   assert( !pPrior
1def3 2d 3e 70 4c 69 6d 69 74 20 29 3b 0a 20 20 20 20  ->pLimit );.    
1def4 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d      pPrior->pLim
1def5 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a  it = p->pLimit;.
1def6 20 20 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e          pPrior->
1def7 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66  pOffset = p->pOf
1def8 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 72 63  fset;.        rc
1def9 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
1defa 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
1defb 20 65 44 65 73 74 2c 20 69 50 61 72 6d 2c 20 30   eDest, iParm, 0
1defc 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b 0a 20 20  , 0, 0, aff);.  
1defd 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20        p->pLimit 
1defe 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  = 0;.        p->
1deff 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
1df00 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
1df01 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75           goto mu
1df02 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
1df03 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1df04 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b    p->pPrior = 0;
1df05 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4c 69 6d  .        p->iLim
1df06 69 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69 4c 69  it = pPrior->iLi
1df07 6d 69 74 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  mit;.        p->
1df08 69 4f 66 66 73 65 74 20 3d 20 70 50 72 69 6f 72  iOffset = pPrior
1df09 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
1df0a 20 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74     if( p->iLimit
1df0b 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
1df0c 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
1df0d 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
1df0e 66 4d 65 6d 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69  fMemZero, p->iLi
1df0f 6d 69 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  mit, 0);.       
1df10 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
1df11 76 2c 20 22 23 20 4a 75 6d 70 20 61 68 65 61 64  v, "# Jump ahead
1df12 20 69 66 20 4c 49 4d 49 54 20 72 65 61 63 68 65   if LIMIT reache
1df13 64 22 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  d"));.        }.
1df14 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1df15 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
1df16 65 2c 20 70 2c 20 65 44 65 73 74 2c 20 69 50 61  e, p, eDest, iPa
1df17 72 6d 2c 20 30 2c 20 30 2c 20 30 2c 20 61 66 66  rm, 0, 0, 0, aff
1df18 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50  );.        p->pP
1df19 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20  rior = pPrior;. 
1df1a 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
1df1b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1df1c 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
1df1d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1df1e 20 20 20 20 69 66 28 20 61 64 64 72 20 29 7b 0a      if( addr ){.
1df1f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1df20 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1df21 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20   addr);.        
1df22 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
1df23 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
1df24 2a 20 46 6f 72 20 55 4e 49 4f 4e 20 41 4c 4c 20  * For UNION ALL 
1df25 2e 2e 2e 20 4f 52 44 45 52 20 42 59 20 66 61 6c  ... ORDER BY fal
1df26 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
1df27 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20   next case */.  
1df28 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1df29 45 58 43 45 50 54 3a 0a 20 20 20 20 63 61 73 65  EXCEPT:.    case
1df2a 20 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0a 20 20 20   TK_UNION: {.   
1df2b 20 20 20 69 6e 74 20 75 6e 69 6f 6e 54 61 62 3b     int unionTab;
1df2c 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
1df2d 6d 62 65 72 20 6f 66 20 74 68 65 20 74 65 6d 70  mber of the temp
1df2e 6f 72 61 72 79 20 74 61 62 6c 65 20 68 6f 6c 64  orary table hold
1df2f 69 6e 67 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20  ing result */.  
1df30 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 20      int op = 0; 
1df31 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
1df32 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69 6f  he SRT_ operatio
1df33 6e 73 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 73  ns to apply to s
1df34 65 6c 66 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  elf */.      int
1df35 20 70 72 69 6f 72 4f 70 3b 20 20 20 20 20 2f 2a   priorOp;     /*
1df36 20 54 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74   The SRT_ operat
1df37 69 6f 6e 20 74 6f 20 61 70 70 6c 79 20 74 6f 20  ion to apply to 
1df38 70 72 69 6f 72 20 73 65 6c 65 63 74 73 20 2a 2f  prior selects */
1df39 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69  .      Expr *pLi
1df3a 6d 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 20 2f  mit, *pOffset; /
1df3b 2a 20 53 61 76 65 64 20 76 61 6c 75 65 73 20 6f  * Saved values o
1df3c 66 20 70 2d 3e 6e 4c 69 6d 69 74 20 61 6e 64 20  f p->nLimit and 
1df3d 70 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f 0a 20 20  p->nOffset */.  
1df3e 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 0a 20      int addr;.. 
1df3f 20 20 20 20 20 70 72 69 6f 72 4f 70 20 3d 20 70       priorOp = p
1df40 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 3f 20 53  ->op==TK_ALL ? S
1df41 52 54 5f 54 61 62 6c 65 20 3a 20 53 52 54 5f 55  RT_Table : SRT_U
1df42 6e 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20  nion;.      if( 
1df43 65 44 65 73 74 3d 3d 70 72 69 6f 72 4f 70 20 26  eDest==priorOp &
1df44 26 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 26 26  & pOrderBy==0 &&
1df45 20 21 70 2d 3e 70 4c 69 6d 69 74 20 26 26 20 21   !p->pLimit && !
1df46 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b 0a 20 20  p->pOffset ){.  
1df47 20 20 20 20 20 20 2f 2a 20 57 65 20 63 61 6e 20        /* We can 
1df48 72 65 75 73 65 20 61 20 74 65 6d 70 6f 72 61 72  reuse a temporar
1df49 79 20 74 61 62 6c 65 20 67 65 6e 65 72 61 74 65  y table generate
1df4a 64 20 62 79 20 61 20 53 45 4c 45 43 54 20 74 6f  d by a SELECT to
1df4b 20 6f 75 72 0a 20 20 20 20 20 20 20 20 2a 2a 20   our.        ** 
1df4c 72 69 67 68 74 2e 0a 20 20 20 20 20 20 20 20 2a  right..        *
1df4d 2f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54  /.        unionT
1df4e 61 62 20 3d 20 69 50 61 72 6d 3b 0a 20 20 20 20  ab = iParm;.    
1df4f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1df50 20 2f 2a 20 57 65 20 77 69 6c 6c 20 6e 65 65 64   /* We will need
1df51 20 74 6f 20 63 72 65 61 74 65 20 6f 75 72 20 6f   to create our o
1df52 77 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  wn temporary tab
1df53 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 20  le to hold the. 
1df54 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6d         ** interm
1df55 65 64 69 61 74 65 20 72 65 73 75 6c 74 73 2e 0a  ediate results..
1df56 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1df57 20 20 20 75 6e 69 6f 6e 54 61 62 20 3d 20 70 50     unionTab = pP
1df58 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
1df59 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72        if( pOrder
1df5a 42 79 20 26 26 20 6d 61 74 63 68 4f 72 64 65 72  By && matchOrder
1df5b 62 79 54 6f 43 6f 6c 75 6d 6e 28 70 50 61 72 73  byToColumn(pPars
1df5c 65 2c 20 70 2c 20 70 4f 72 64 65 72 42 79 2c 20  e, p, pOrderBy, 
1df5d 75 6e 69 6f 6e 54 61 62 2c 31 29 20 29 7b 0a 20  unionTab,1) ){. 
1df5e 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b           rc = 1;
1df5f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1df60 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
1df61 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1df62 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
1df63 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1df64 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
1df65 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20   unionTab, 0);. 
1df66 20 20 20 20 20 20 20 69 66 28 20 70 72 69 6f 72         if( prior
1df67 4f 70 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 7b  Op==SRT_Table ){
1df68 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1df69 74 28 20 6e 53 65 74 50 32 3c 73 69 7a 65 6f 66  t( nSetP2<sizeof
1df6a 28 61 53 65 74 50 32 29 2f 73 69 7a 65 6f 66 28  (aSetP2)/sizeof(
1df6b 61 53 65 74 50 32 5b 30 5d 29 20 29 3b 0a 20 20  aSetP2[0]) );.  
1df6c 20 20 20 20 20 20 20 20 61 53 65 74 50 32 5b 6e          aSetP2[n
1df6d 53 65 74 50 32 2b 2b 5d 20 3d 20 61 64 64 72 3b  SetP2++] = addr;
1df6e 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1df6f 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1df70 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  ( p->addrOpenEph
1df71 6d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20  m[0] == -1 );.  
1df72 20 20 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f          p->addrO
1df73 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 61 64 64  penEphm[0] = add
1df74 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  r;.          p->
1df75 70 52 69 67 68 74 6d 6f 73 74 2d 3e 75 73 65 73  pRightmost->uses
1df76 45 70 68 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20  Ephm = 1;.      
1df77 20 20 7d 0a 20 20 20 20 20 20 20 20 63 72 65 61    }.        crea
1df78 74 65 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 70  teSortingIndex(p
1df79 50 61 72 73 65 2c 20 70 2c 20 70 4f 72 64 65 72  Parse, p, pOrder
1df7a 42 79 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  By);.        ass
1df7b 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29  ert( p->pEList )
1df7c 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
1df7d 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45 4c   /* Code the SEL
1df7e 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74  ECT statements t
1df7f 6f 20 6f 75 72 20 6c 65 66 74 0a 20 20 20 20 20  o our left.     
1df80 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
1df81 28 20 21 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65  ( !pPrior->pOrde
1df82 72 42 79 20 29 3b 0a 20 20 20 20 20 20 72 63 20  rBy );.      rc 
1df83 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
1df84 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20  pParse, pPrior, 
1df85 70 72 69 6f 72 4f 70 2c 20 75 6e 69 6f 6e 54 61  priorOp, unionTa
1df86 62 2c 20 30 2c 20 30 2c 20 30 2c 20 61 66 66 29  b, 0, 0, 0, aff)
1df87 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
1df88 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d  {.        goto m
1df89 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b  ulti_select_end;
1df8a 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1df8b 2f 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72  /* Code the curr
1df8c 65 6e 74 20 53 45 4c 45 43 54 20 73 74 61 74 65  ent SELECT state
1df8d 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ment.      */.  
1df8e 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f      switch( p->o
1df8f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 63 61  p ){.         ca
1df90 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20 6f  se TK_EXCEPT:  o
1df91 70 20 3d 20 53 52 54 5f 45 78 63 65 70 74 3b 20  p = SRT_Except; 
1df92 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1df93 20 20 63 61 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a    case TK_UNION:
1df94 20 20 20 6f 70 20 3d 20 53 52 54 5f 55 6e 69 6f     op = SRT_Unio
1df95 6e 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  n;    break;.   
1df96 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4c        case TK_AL
1df97 4c 3a 20 20 20 20 20 6f 70 20 3d 20 53 52 54 5f  L:     op = SRT_
1df98 54 61 62 6c 65 3b 20 20 20 20 62 72 65 61 6b 3b  Table;    break;
1df99 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1df9a 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20  ->pPrior = 0;.  
1df9b 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20      p->pOrderBy 
1df9c 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 64 69  = 0;.      p->di
1df9d 73 61 6c 6c 6f 77 4f 72 64 65 72 42 79 20 3d 20  sallowOrderBy = 
1df9e 70 4f 72 64 65 72 42 79 21 3d 30 3b 0a 20 20 20  pOrderBy!=0;.   
1df9f 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70     pLimit = p->p
1dfa0 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
1dfa1 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
1dfa2 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70    pOffset = p->p
1dfa3 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d  Offset;.      p-
1dfa4 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >pOffset = 0;.  
1dfa5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1dfa6 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
1dfa7 2c 20 6f 70 2c 20 75 6e 69 6f 6e 54 61 62 2c 20  , op, unionTab, 
1dfa8 30 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b 0a 20  0, 0, 0, aff);. 
1dfa9 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6c       /* Query fl
1dfaa 61 74 74 65 6e 69 6e 67 20 69 6e 20 73 71 6c 69  attening in sqli
1dfab 74 65 33 53 65 6c 65 63 74 28 29 20 6d 69 67 68  te3Select() migh
1dfac 74 20 72 65 66 69 6c 6c 20 70 2d 3e 70 4f 72 64  t refill p->pOrd
1dfad 65 72 42 79 2e 0a 20 20 20 20 20 20 2a 2a 20 42  erBy..      ** B
1dfae 65 20 73 75 72 65 20 74 6f 20 64 65 6c 65 74 65  e sure to delete
1dfaf 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 74 68   p->pOrderBy, th
1dfb0 65 72 65 66 6f 72 65 2c 20 74 6f 20 61 76 6f 69  erefore, to avoi
1dfb1 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e  d a memory leak.
1dfb2 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1dfb3 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
1dfb4 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  p->pOrderBy);.  
1dfb5 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20      p->pPrior = 
1dfb6 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d  pPrior;.      p-
1dfb7 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64  >pOrderBy = pOrd
1dfb8 65 72 42 79 3b 0a 20 20 20 20 20 20 73 71 6c 69  erBy;.      sqli
1dfb9 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 2d  te3ExprDelete(p-
1dfba 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20  >pLimit);.      
1dfbb 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d  p->pLimit = pLim
1dfbc 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66  it;.      p->pOf
1dfbd 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a  fset = pOffset;.
1dfbe 20 20 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20        p->iLimit 
1dfbf 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 2d 3e 69  = -1;.      p->i
1dfc0 4f 66 66 73 65 74 20 3d 20 2d 31 3b 0a 20 20 20  Offset = -1;.   
1dfc1 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
1dfc2 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f       goto multi_
1dfc3 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
1dfc4 20 20 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 43    }...      /* C
1dfc5 6f 6e 76 65 72 74 20 74 68 65 20 64 61 74 61 20  onvert the data 
1dfc6 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  in the temporary
1dfc7 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 61 74   table into what
1dfc8 65 76 65 72 20 66 6f 72 6d 0a 20 20 20 20 20 20  ever form.      
1dfc9 2a 2a 20 69 74 20 69 73 20 74 68 61 74 20 77 65  ** it is that we
1dfca 20 63 75 72 72 65 6e 74 6c 79 20 6e 65 65 64 2e   currently need.
1dfcb 0a 20 20 20 20 20 20 2a 2f 20 20 20 20 20 20 0a  .      */      .
1dfcc 20 20 20 20 20 20 69 66 28 20 65 44 65 73 74 21        if( eDest!
1dfcd 3d 70 72 69 6f 72 4f 70 20 7c 7c 20 75 6e 69 6f  =priorOp || unio
1dfce 6e 54 61 62 21 3d 69 50 61 72 6d 20 29 7b 0a 20  nTab!=iParm ){. 
1dfcf 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74         int iCont
1dfd0 2c 20 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74  , iBreak, iStart
1dfd1 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1dfd2 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20  ( p->pEList );. 
1dfd3 20 20 20 20 20 20 20 69 66 28 20 65 44 65 73 74         if( eDest
1dfd4 3d 3d 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20 29  ==SRT_Callback )
1dfd5 7b 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c 65  {.          Sele
1dfd6 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a  ct *pFirst = p;.
1dfd7 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
1dfd8 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20   pFirst->pPrior 
1dfd9 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 73  ) pFirst = pFirs
1dfda 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20  t->pPrior;.     
1dfdb 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c       generateCol
1dfdc 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
1dfdd 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69   0, pFirst->pELi
1dfde 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  st);.        }. 
1dfdf 20 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d 20         iBreak = 
1dfe0 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
1dfe1 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  abel(v);.       
1dfe2 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33   iCont = sqlite3
1dfe3 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
1dfe4 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
1dfe5 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28  eLimitRegisters(
1dfe6 70 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61  pParse, p, iBrea
1dfe7 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  k);.        sqli
1dfe8 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1dfe9 4f 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69 6f 6e  OP_Rewind, union
1dfea 54 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20  Tab, iBreak);.  
1dfeb 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 73        iStart = s
1dfec 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
1dfed 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
1dfee 20 20 72 63 20 3d 20 73 65 6c 65 63 74 49 6e 6e    rc = selectInn
1dfef 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70  erLoop(pParse, p
1dff0 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 75 6e 69  , p->pEList, uni
1dff1 6f 6e 54 61 62 2c 20 70 2d 3e 70 45 4c 69 73 74  onTab, p->pEList
1dff2 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20  ->nExpr,.       
1dff3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dff4 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 2c 20        pOrderBy, 
1dff5 2d 31 2c 20 65 44 65 73 74 2c 20 69 50 61 72 6d  -1, eDest, iParm
1dff6 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
1dff7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dff8 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 2c 20 30  iCont, iBreak, 0
1dff9 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1dffa 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  c ){.          r
1dffb 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  c = 1;.         
1dffc 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
1dffd 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20  ct_end;.        
1dffe 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
1dfff 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1e000 6c 28 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20  l(v, iCont);.   
1e001 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1e002 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74  AddOp(v, OP_Next
1e003 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 69 53 74 61  , unionTab, iSta
1e004 72 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  rt);.        sql
1e005 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
1e006 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b  abel(v, iBreak);
1e007 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1e008 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1e009 43 6c 6f 73 65 2c 20 75 6e 69 6f 6e 54 61 62 2c  Close, unionTab,
1e00a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1e00b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1e00c 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45      case TK_INTE
1e00d 52 53 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 69  RSECT: {.      i
1e00e 6e 74 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20  nt tab1, tab2;. 
1e00f 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20       int iCont, 
1e010 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a  iBreak, iStart;.
1e011 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d        Expr *pLim
1e012 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20  it, *pOffset;.  
1e013 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 0a 20      int addr;.. 
1e014 20 20 20 20 20 2f 2a 20 49 4e 54 45 52 53 45 43       /* INTERSEC
1e015 54 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66  T is different f
1e016 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 20 73  rom the others s
1e017 69 6e 63 65 20 69 74 20 72 65 71 75 69 72 65 73  ince it requires
1e018 0a 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 74 65  .      ** two te
1e019 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20  mporary tables. 
1e01a 20 48 65 6e 63 65 20 69 74 20 68 61 73 20 69 74   Hence it has it
1e01b 73 20 6f 77 6e 20 63 61 73 65 2e 20 20 42 65 67  s own case.  Beg
1e01c 69 6e 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 61  in.      ** by a
1e01d 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20 74 61  llocating the ta
1e01e 62 6c 65 73 20 77 65 20 77 69 6c 6c 20 6e 65 65  bles we will nee
1e01f 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
1e020 20 20 74 61 62 31 20 3d 20 70 50 61 72 73 65 2d    tab1 = pParse-
1e021 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 74  >nTab++;.      t
1e022 61 62 32 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  ab2 = pParse->nT
1e023 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  ab++;.      if( 
1e024 70 4f 72 64 65 72 42 79 20 26 26 20 6d 61 74 63  pOrderBy && matc
1e025 68 4f 72 64 65 72 62 79 54 6f 43 6f 6c 75 6d 6e  hOrderbyToColumn
1e026 28 70 50 61 72 73 65 2c 70 2c 70 4f 72 64 65 72  (pParse,p,pOrder
1e027 42 79 2c 74 61 62 31 2c 31 29 20 29 7b 0a 20 20  By,tab1,1) ){.  
1e028 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20        rc = 1;.  
1e029 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69        goto multi
1e02a 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20  _select_end;.   
1e02b 20 20 20 7d 0a 20 20 20 20 20 20 63 72 65 61 74     }.      creat
1e02c 65 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 70 50  eSortingIndex(pP
1e02d 61 72 73 65 2c 20 70 2c 20 70 4f 72 64 65 72 42  arse, p, pOrderB
1e02e 79 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20  y);..      addr 
1e02f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1e030 4f 70 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  Op(v, OP_OpenEph
1e031 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30 29  emeral, tab1, 0)
1e032 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1e033 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
1e034 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20  0] == -1 );.    
1e035 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
1e036 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20  m[0] = addr;.   
1e037 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74     p->pRightmost
1e038 2d 3e 75 73 65 73 45 70 68 6d 20 3d 20 31 3b 0a  ->usesEphm = 1;.
1e039 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
1e03a 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20 20  >pEList );..    
1e03b 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45    /* Code the SE
1e03c 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65 66  LECTs to our lef
1e03d 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79  t into temporary
1e03e 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a 20   table "tab1".. 
1e03f 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63       */.      rc
1e040 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
1e041 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
1e042 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 61 62 31   SRT_Union, tab1
1e043 2c 20 30 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b  , 0, 0, 0, aff);
1e044 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
1e045 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
1e046 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
1e047 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
1e048 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65  * Code the curre
1e049 6e 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74  nt SELECT into t
1e04a 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22  emporary table "
1e04b 74 61 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20  tab2".      */. 
1e04c 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
1e04d 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1e04e 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
1e04f 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20  , tab2, 0);.    
1e050 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64    assert( p->add
1e051 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20  rOpenEphm[1] == 
1e052 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  -1 );.      p->a
1e053 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d  ddrOpenEphm[1] =
1e054 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e   addr;.      p->
1e055 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
1e056 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c    pLimit = p->pL
1e057 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  imit;.      p->p
1e058 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  Limit = 0;.     
1e059 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f   pOffset = p->pO
1e05a 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e  ffset;.      p->
1e05b 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
1e05c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
1e05d 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c  elect(pParse, p,
1e05e 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 61 62 32   SRT_Union, tab2
1e05f 2c 20 30 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b  , 0, 0, 0, aff);
1e060 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  .      p->pPrior
1e061 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20   = pPrior;.     
1e062 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
1e063 74 65 28 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20  te(p->pLimit);. 
1e064 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d       p->pLimit =
1e065 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70   pLimit;.      p
1e066 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66  ->pOffset = pOff
1e067 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 72  set;.      if( r
1e068 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  c ){.        got
1e069 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
1e06a 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  nd;.      }..   
1e06b 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
1e06c 6f 64 65 20 74 6f 20 74 61 6b 65 20 74 68 65 20  ode to take the 
1e06d 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6f 66 20  intersection of 
1e06e 74 68 65 20 74 77 6f 20 74 65 6d 70 6f 72 61 72  the two temporar
1e06f 79 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65  y.      ** table
1e070 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  s..      */.    
1e071 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
1e072 69 73 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ist );.      if(
1e073 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 61 6c 6c   eDest==SRT_Call
1e074 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  back ){.        
1e075 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d  Select *pFirst =
1e076 20 70 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c   p;.        whil
1e077 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f  e( pFirst->pPrio
1e078 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69  r ) pFirst = pFi
1e079 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  rst->pPrior;.   
1e07a 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c       generateCol
1e07b 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
1e07c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69   0, pFirst->pELi
1e07d 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  st);.      }.   
1e07e 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69     iBreak = sqli
1e07f 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
1e080 28 76 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e 74  (v);.      iCont
1e081 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
1e082 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
1e083 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65    computeLimitRe
1e084 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20  gisters(pParse, 
1e085 70 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  p, iBreak);.    
1e086 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e087 4f 70 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  Op(v, OP_Rewind,
1e088 20 74 61 62 31 2c 20 69 42 72 65 61 6b 29 3b 0a   tab1, iBreak);.
1e089 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 73        iStart = s
1e08a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1e08b 76 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 74 61  v, OP_RowKey, ta
1e08c 62 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  b1, 0);.      sq
1e08d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1e08e 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74  , OP_NotFound, t
1e08f 61 62 32 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20  ab2, iCont);.   
1e090 20 20 20 72 63 20 3d 20 73 65 6c 65 63 74 49 6e     rc = selectIn
1e091 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20  nerLoop(pParse, 
1e092 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 74 61  p, p->pEList, ta
1e093 62 31 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  b1, p->pEList->n
1e094 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20  Expr,.          
1e095 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e096 20 20 20 70 4f 72 64 65 72 42 79 2c 20 2d 31 2c     pOrderBy, -1,
1e097 20 65 44 65 73 74 2c 20 69 50 61 72 6d 2c 20 0a   eDest, iParm, .
1e098 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e099 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f               iCo
1e09a 6e 74 2c 20 69 42 72 65 61 6b 2c 20 30 29 3b 0a  nt, iBreak, 0);.
1e09b 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
1e09c 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a          rc = 1;.
1e09d 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
1e09e 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
1e09f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
1e0a0 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
1e0a1 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 29 3b 0a  abel(v, iCont);.
1e0a2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1e0a3 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 78  eAddOp(v, OP_Nex
1e0a4 74 2c 20 74 61 62 31 2c 20 69 53 74 61 72 74 29  t, tab1, iStart)
1e0a5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1e0a6 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
1e0a7 76 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  v, iBreak);.    
1e0a8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e0a9 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  Op(v, OP_Close, 
1e0aa 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20 20 20  tab2, 0);.      
1e0ab 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e0ac 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61  (v, OP_Close, ta
1e0ad 62 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72  b1, 0);.      br
1e0ae 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  eak;.    }.  }..
1e0af 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61    /* Make sure a
1e0b0 6c 6c 20 53 45 4c 45 43 54 73 20 69 6e 20 74 68  ll SELECTs in th
1e0b1 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 76 65  e statement have
1e0b2 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72   the same number
1e0b3 20 6f 66 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a   of elements.  *
1e0b4 2a 20 69 6e 20 74 68 65 69 72 20 72 65 73 75 6c  * in their resul
1e0b5 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20 20 61  t sets..  */.  a
1e0b6 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74  ssert( p->pEList
1e0b7 20 26 26 20 70 50 72 69 6f 72 2d 3e 70 45 4c 69   && pPrior->pELi
1e0b8 73 74 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  st );.  if( p->p
1e0b9 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70 50  EList->nExpr!=pP
1e0ba 72 69 6f 72 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  rior->pEList->nE
1e0bb 78 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  xpr ){.    sqlit
1e0bc 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1e0bd 65 2c 20 22 53 45 4c 45 43 54 73 20 74 6f 20 74  e, "SELECTs to t
1e0be 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68  he left and righ
1e0bf 74 20 6f 66 20 25 73 22 0a 20 20 20 20 20 20 22  t of %s".      "
1e0c0 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 68 65   do not have the
1e0c1 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
1e0c2 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c  result columns",
1e0c3 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d   selectOpName(p-
1e0c4 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  >op));.    rc = 
1e0c5 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  1;.    goto mult
1e0c6 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
1e0c7 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  }..  /* Set the 
1e0c8 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
1e0c9 73 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 74  s in temporary t
1e0ca 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 6e 43 6f  ables.  */.  nCo
1e0cb 6c 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  l = p->pEList->n
1e0cc 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 6e  Expr;.  while( n
1e0cd 53 65 74 50 32 20 29 7b 0a 20 20 20 20 73 71 6c  SetP2 ){.    sql
1e0ce 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
1e0cf 28 76 2c 20 61 53 65 74 50 32 5b 2d 2d 6e 53 65  (v, aSetP2[--nSe
1e0d0 74 50 32 5d 2c 20 6e 43 6f 6c 29 3b 0a 20 20 7d  tP2], nCol);.  }
1e0d1 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 63  ..  /* Compute c
1e0d2 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1e0d3 65 73 20 75 73 65 64 20 62 79 20 65 69 74 68 65  es used by eithe
1e0d4 72 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  r the ORDER BY c
1e0d5 6c 61 75 73 65 20 6f 72 0a 20 20 2a 2a 20 62 79  lause or.  ** by
1e0d6 20 61 6e 79 20 74 65 6d 70 6f 72 61 72 79 20 74   any temporary t
1e0d7 61 62 6c 65 73 20 6e 65 65 64 65 64 20 74 6f 20  ables needed to 
1e0d8 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f  implement the co
1e0d9 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a 20  mpound select.. 
1e0da 20 2a 2a 20 41 74 74 61 63 68 20 74 68 65 20 4b   ** Attach the K
1e0db 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
1e0dc 20 74 6f 20 61 6c 6c 20 74 65 6d 70 6f 72 61 72   to all temporar
1e0dd 79 20 74 61 62 6c 65 73 2e 20 20 49 6e 76 6f 6b  y tables.  Invok
1e0de 65 20 74 68 65 0a 20 20 2a 2a 20 4f 52 44 45 52  e the.  ** ORDER
1e0df 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 69   BY processing i
1e0e0 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52  f there is an OR
1e0e1 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 20  DER BY clause.. 
1e0e2 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 73 65   **.  ** This se
1e0e3 63 74 69 6f 6e 20 69 73 20 72 75 6e 20 62 79 20  ction is run by 
1e0e4 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 53  the right-most S
1e0e5 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1e0e6 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 45 4c 45 43  only..  ** SELEC
1e0e7 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  T statements to 
1e0e8 74 68 65 20 6c 65 66 74 20 61 6c 77 61 79 73 20  the left always 
1e0e9 73 6b 69 70 20 74 68 69 73 20 70 61 72 74 2e 20  skip this part. 
1e0ea 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a   The right-most.
1e0eb 20 20 2a 2a 20 53 45 4c 45 43 54 20 6d 69 67 68    ** SELECT migh
1e0ec 74 20 61 6c 73 6f 20 73 6b 69 70 20 74 68 69 73  t also skip this
1e0ed 20 70 61 72 74 20 69 66 20 69 74 20 68 61 73 20   part if it has 
1e0ee 6e 6f 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  no ORDER BY clau
1e0ef 73 65 20 61 6e 64 0a 20 20 2a 2a 20 6e 6f 20 74  se and.  ** no t
1e0f0 65 6d 70 20 74 61 62 6c 65 73 20 61 72 65 20 72  emp tables are r
1e0f1 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f 0a 20 20  equired..  */.  
1e0f2 69 66 28 20 70 4f 72 64 65 72 42 79 20 7c 7c 20  if( pOrderBy || 
1e0f3 70 2d 3e 75 73 65 73 45 70 68 6d 20 29 7b 0a 20  p->usesEphm ){. 
1e0f4 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
1e0f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e0f6 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
1e0f7 20 2a 2f 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20   */.    KeyInfo 
1e0f8 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 20 20  *pKeyInfo;      
1e0f9 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69        /* Collati
1e0fa 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
1e0fb 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
1e0fc 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 4c  /.    Select *pL
1e0fd 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  oop;            
1e0fe 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
1e0ff 6e 67 20 74 68 72 6f 75 67 68 20 53 45 4c 45 43  ng through SELEC
1e100 54 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a  T statements */.
1e101 20 20 20 20 69 6e 74 20 6e 4b 65 79 43 6f 6c 3b      int nKeyCol;
1e102 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e103 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
1e104 6e 74 72 69 65 73 20 69 6e 20 70 4b 65 79 49 6e  ntries in pKeyIn
1e105 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20  fo->aCol[] */.  
1e106 20 20 43 6f 6c 6c 53 65 71 20 2a 2a 61 70 43 6f    CollSeq **apCo
1e107 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
1e108 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74  /* For looping t
1e109 68 72 6f 75 67 68 20 70 4b 65 79 49 6e 66 6f 2d  hrough pKeyInfo-
1e10a 3e 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 20 20  >aColl[] */.    
1e10b 43 6f 6c 6c 53 65 71 20 2a 2a 61 43 6f 70 79 3b  CollSeq **aCopy;
1e10c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e10d 20 41 20 63 6f 70 79 20 6f 66 20 70 4b 65 79 49   A copy of pKeyI
1e10e 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a  nfo->aColl[] */.
1e10f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1e110 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29 3b  pRightmost==p );
1e111 0a 20 20 20 20 6e 4b 65 79 43 6f 6c 20 3d 20 6e  .    nKeyCol = n
1e112 43 6f 6c 20 2b 20 28 70 4f 72 64 65 72 42 79 20  Col + (pOrderBy 
1e113 3f 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  ? pOrderBy->nExp
1e114 72 20 3a 20 30 29 3b 0a 20 20 20 20 70 4b 65 79  r : 0);.    pKey
1e115 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62  Info = sqlite3Db
1e116 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73  MallocZero(pPars
1e117 65 2d 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 20  e->db,.         
1e118 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
1e119 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 2b  zeof(*pKeyInfo)+
1e11a 6e 4b 65 79 43 6f 6c 2a 28 73 69 7a 65 6f 66 28  nKeyCol*(sizeof(
1e11b 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 31 29 29 3b  CollSeq*) + 1));
1e11c 0a 20 20 20 20 69 66 28 20 21 70 4b 65 79 49 6e  .    if( !pKeyIn
1e11d 66 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  fo ){.      rc =
1e11e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1e11f 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f       goto multi_
1e120 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
1e121 7d 0a 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d  }..    pKeyInfo-
1e122 3e 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73  >enc = ENC(pPars
1e123 65 2d 3e 64 62 29 3b 0a 20 20 20 20 70 4b 65 79  e->db);.    pKey
1e124 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e  Info->nField = n
1e125 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d  Col;..    for(i=
1e126 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65 79 49 6e  0, apColl=pKeyIn
1e127 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c 6e 43 6f  fo->aColl; i<nCo
1e128 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c 6c 2b 2b  l; i++, apColl++
1e129 29 7b 0a 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c  ){.      *apColl
1e12a 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f   = multiSelectCo
1e12b 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2c  llSeq(pParse, p,
1e12c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20 30   i);.      if( 0
1e12d 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  ==*apColl ){.   
1e12e 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d 20 70       *apColl = p
1e12f 50 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74  Parse->db->pDflt
1e130 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
1e131 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 70 4c 6f    }..    for(pLo
1e132 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f  op=p; pLoop; pLo
1e133 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72  op=pLoop->pPrior
1e134 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  ){.      for(i=0
1e135 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<2; i++){.   
1e136 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20       int addr = 
1e137 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45  pLoop->addrOpenE
1e138 70 68 6d 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  phm[i];.        
1e139 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20  if( addr<0 ){.  
1e13a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 5b 30          /* If [0
1e13b 5d 20 69 73 20 75 6e 75 73 65 64 20 74 68 65 6e  ] is unused then
1e13c 20 5b 31 5d 20 69 73 20 61 6c 73 6f 20 75 6e 75   [1] is also unu
1e13d 73 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e 0a  sed.  So we can.
1e13e 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 77            ** alw
1e13f 61 79 73 20 73 61 66 65 6c 79 20 61 62 6f 72 74  ays safely abort
1e140 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20   as soon as the 
1e141 66 69 72 73 74 20 75 6e 75 73 65 64 20 73 6c 6f  first unused slo
1e142 74 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20  t is found */.  
1e143 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1e144 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45  pLoop->addrOpenE
1e145 70 68 6d 5b 31 5d 3c 30 20 29 3b 0a 20 20 20 20  phm[1]<0 );.    
1e146 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e147 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
1e148 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1e149 50 32 28 76 2c 20 61 64 64 72 2c 20 6e 43 6f 6c  P2(v, addr, nCol
1e14a 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1e14b 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76  e3VdbeChangeP3(v
1e14c 2c 20 61 64 64 72 2c 20 28 63 68 61 72 2a 29 70  , addr, (char*)p
1e14d 4b 65 79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59 49  KeyInfo, P3_KEYI
1e14e 4e 46 4f 29 3b 0a 20 20 20 20 20 20 20 20 70 4c  NFO);.        pL
1e14f 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  oop->addrOpenEph
1e150 6d 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20  m[i] = -1;.     
1e151 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
1e152 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
1e153 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
1e154 69 73 74 5f 69 74 65 6d 20 2a 70 4f 54 65 72 6d  ist_item *pOTerm
1e155 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 3b 0a   = pOrderBy->a;.
1e156 20 20 20 20 20 20 69 6e 74 20 6e 4f 72 64 65 72        int nOrder
1e157 42 79 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42  ByExpr = pOrderB
1e158 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  y->nExpr;.      
1e159 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20  int addr;.      
1e15a 75 38 20 2a 70 53 6f 72 74 4f 72 64 65 72 3b 0a  u8 *pSortOrder;.
1e15b 0a 20 20 20 20 20 20 2f 2a 20 52 65 75 73 65 20  .      /* Reuse 
1e15c 74 68 65 20 73 61 6d 65 20 70 4b 65 79 49 6e 66  the same pKeyInf
1e15d 6f 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20  o for the ORDER 
1e15e 42 59 20 61 73 20 77 61 73 20 75 73 65 64 20 61  BY as was used a
1e15f 62 6f 76 65 20 66 6f 72 0a 20 20 20 20 20 20 2a  bove for.      *
1e160 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  * the compound s
1e161 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73  elect statements
1e162 2e 20 20 45 78 63 65 70 74 20 77 65 20 68 61 76  .  Except we hav
1e163 65 20 74 6f 20 63 68 61 6e 67 65 20 6f 75 74 20  e to change out 
1e164 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 4b 65  the.      ** pKe
1e165 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d 20 76  yInfo->aColl[] v
1e166 61 6c 75 65 73 2e 20 20 53 6f 6d 65 20 6f 66 20  alues.  Some of 
1e167 74 68 65 20 61 43 6f 6c 6c 5b 5d 20 76 61 6c 75  the aColl[] valu
1e168 65 73 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20  es will be.     
1e169 20 2a 2a 20 72 65 75 73 65 64 20 77 68 65 6e 20   ** reused when 
1e16a 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 74 68 65  constructing the
1e16b 20 70 4b 65 79 49 6e 66 6f 20 66 6f 72 20 74 68   pKeyInfo for th
1e16c 65 20 4f 52 44 45 52 20 42 59 2c 20 73 6f 20 6d  e ORDER BY, so m
1e16d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 61 20 63  ake.      ** a c
1e16e 6f 70 79 2e 20 20 53 75 66 66 69 63 69 65 6e 74  opy.  Sufficient
1e16f 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 62   space to hold b
1e170 6f 74 68 20 74 68 65 20 6e 43 6f 6c 20 65 6e 74  oth the nCol ent
1e171 72 69 65 73 20 66 6f 72 0a 20 20 20 20 20 20 2a  ries for.      *
1e172 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  * the compound s
1e173 65 6c 65 63 74 20 61 6e 64 20 74 68 65 20 6e 4f  elect and the nO
1e174 72 64 65 72 62 79 45 78 70 72 20 65 6e 74 72 69  rderbyExpr entri
1e175 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52  es for the ORDER
1e176 20 42 59 0a 20 20 20 20 20 20 2a 2a 20 77 61 73   BY.      ** was
1e177 20 61 6c 6c 6f 63 61 74 65 64 20 61 62 6f 76 65   allocated above
1e178 2e 20 20 42 75 74 20 77 65 20 6e 65 65 64 20 74  .  But we need t
1e179 6f 20 6d 6f 76 65 20 74 68 65 20 63 6f 6d 70 6f  o move the compo
1e17a 75 6e 64 20 73 65 6c 65 63 74 0a 20 20 20 20 20  und select.     
1e17b 20 2a 2a 20 65 6e 74 72 69 65 73 20 6f 75 74 20   ** entries out 
1e17c 6f 66 20 74 68 65 20 77 61 79 20 62 65 66 6f 72  of the way befor
1e17d 65 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 74  e constructing t
1e17e 68 65 20 4f 52 44 45 52 20 42 59 20 65 6e 74 72  he ORDER BY entr
1e17f 69 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 6f  ies..      ** Mo
1e180 76 65 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  ve the compound 
1e181 73 65 6c 65 63 74 20 65 6e 74 72 69 65 73 20 69  select entries i
1e182 6e 74 6f 20 61 43 6f 70 79 5b 5d 20 77 68 65 72  nto aCopy[] wher
1e183 65 20 74 68 65 79 20 63 61 6e 20 62 65 0a 20 20  e they can be.  
1e184 20 20 20 20 2a 2a 20 61 63 63 65 73 73 65 64 20      ** accessed 
1e185 61 6e 64 20 72 65 75 73 65 64 20 77 68 65 6e 20  and reused when 
1e186 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 74 68 65  constructing the
1e187 20 4f 52 44 45 52 20 42 59 20 65 6e 74 72 69 65   ORDER BY entrie
1e188 73 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65 63 61  s..      ** Beca
1e189 75 73 65 20 6e 43 6f 6c 20 6d 69 67 68 74 20 62  use nCol might b
1e18a 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  e greater than o
1e18b 72 20 6c 65 73 73 20 74 68 61 6e 20 6e 4f 72 64  r less than nOrd
1e18c 65 72 42 79 45 78 70 72 0a 20 20 20 20 20 20 2a  erByExpr.      *
1e18d 2a 20 77 65 20 68 61 76 65 20 74 6f 20 75 73 65  * we have to use
1e18e 20 6d 65 6d 6d 6f 76 65 28 29 20 77 68 65 6e 20   memmove() when 
1e18f 64 6f 69 6e 67 20 74 68 65 20 63 6f 70 79 2e 0a  doing the copy..
1e190 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
1e191 43 6f 70 79 20 3d 20 26 70 4b 65 79 49 6e 66 6f  Copy = &pKeyInfo
1e192 2d 3e 61 43 6f 6c 6c 5b 6e 4f 72 64 65 72 42 79  ->aColl[nOrderBy
1e193 45 78 70 72 5d 3b 0a 20 20 20 20 20 20 70 53 6f  Expr];.      pSo
1e194 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49 6e  rtOrder = pKeyIn
1e195 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d  fo->aSortOrder =
1e196 20 28 75 38 2a 29 26 61 43 6f 70 79 5b 6e 43 6f   (u8*)&aCopy[nCo
1e197 6c 5d 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76  l];.      memmov
1e198 65 28 61 43 6f 70 79 2c 20 70 4b 65 79 49 6e 66  e(aCopy, pKeyInf
1e199 6f 2d 3e 61 43 6f 6c 6c 2c 20 6e 43 6f 6c 2a 73  o->aColl, nCol*s
1e19a 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 29  izeof(CollSeq*))
1e19b 3b 0a 0a 20 20 20 20 20 20 61 70 43 6f 6c 6c 20  ;..      apColl 
1e19c 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  = pKeyInfo->aCol
1e19d 6c 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  l;.      for(i=0
1e19e 3b 20 69 3c 6e 4f 72 64 65 72 42 79 45 78 70 72  ; i<nOrderByExpr
1e19f 3b 20 69 2b 2b 2c 20 70 4f 54 65 72 6d 2b 2b 2c  ; i++, pOTerm++,
1e1a0 20 61 70 43 6f 6c 6c 2b 2b 2c 20 70 53 6f 72 74   apColl++, pSort
1e1a1 4f 72 64 65 72 2b 2b 29 7b 0a 20 20 20 20 20 20  Order++){.      
1e1a2 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20    Expr *pExpr = 
1e1a3 70 4f 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20  pOTerm->pExpr;. 
1e1a4 20 20 20 20 20 20 20 69 66 28 20 28 70 45 78 70         if( (pExp
1e1a5 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  r->flags & EP_Ex
1e1a6 70 43 6f 6c 6c 61 74 65 29 20 29 7b 0a 20 20 20  pCollate) ){.   
1e1a7 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1e1a8 45 78 70 72 2d 3e 70 43 6f 6c 6c 21 3d 30 20 29  Expr->pColl!=0 )
1e1a9 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 61 70 43  ;.          *apC
1e1aa 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f  oll = pExpr->pCo
1e1ab 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ll;.        }els
1e1ac 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 61 70  e{.          *ap
1e1ad 43 6f 6c 6c 20 3d 20 61 43 6f 70 79 5b 70 45 78  Coll = aCopy[pEx
1e1ae 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 3b 0a 20 20  pr->iColumn];.  
1e1af 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e1b0 2a 70 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f  *pSortOrder = pO
1e1b1 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b  Term->sortOrder;
1e1b2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
1e1b3 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74  ssert( p->pRight
1e1b4 6d 6f 73 74 3d 3d 70 20 29 3b 0a 20 20 20 20 20  most==p );.     
1e1b5 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72   assert( p->addr
1e1b6 4f 70 65 6e 45 70 68 6d 5b 32 5d 3e 3d 30 20 29  OpenEphm[2]>=0 )
1e1b7 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 70  ;.      addr = p
1e1b8 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32  ->addrOpenEphm[2
1e1b9 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ];.      sqlite3
1e1ba 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
1e1bb 61 64 64 72 2c 20 70 2d 3e 70 4f 72 64 65 72 42  addr, p->pOrderB
1e1bc 79 2d 3e 6e 45 78 70 72 2b 32 29 3b 0a 20 20 20  y->nExpr+2);.   
1e1bd 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69     pKeyInfo->nFi
1e1be 65 6c 64 20 3d 20 6e 4f 72 64 65 72 42 79 45 78  eld = nOrderByEx
1e1bf 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  pr;.      sqlite
1e1c0 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
1e1c1 20 61 64 64 72 2c 20 28 63 68 61 72 2a 29 70 4b   addr, (char*)pK
1e1c2 65 79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59 49 4e  eyInfo, P3_KEYIN
1e1c3 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
1e1c4 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b     pKeyInfo = 0;
1e1c5 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 53  .      generateS
1e1c6 6f 72 74 54 61 69 6c 28 70 50 61 72 73 65 2c 20  ortTail(pParse, 
1e1c7 70 2c 20 76 2c 20 70 2d 3e 70 45 4c 69 73 74 2d  p, v, p->pEList-
1e1c8 3e 6e 45 78 70 72 2c 20 65 44 65 73 74 2c 20 69  >nExpr, eDest, i
1e1c9 50 61 72 6d 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Parm);.    }..  
1e1ca 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1e1cb 4b 65 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a 6d  KeyInfo);.  }..m
1e1cc 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3a  ulti_select_end:
1e1cd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1e1ce 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1e1cf 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
1e1d0 45 4c 45 43 54 20 2a 2f 0a 0a 23 69 66 6e 64 65  ELECT */..#ifnde
1e1d1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1e1d2 45 57 0a 2f 2a 20 46 6f 72 77 61 72 64 20 44 65  EW./* Forward De
1e1d3 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 73 74  clarations */.st
1e1d4 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 45  atic void substE
1e1d5 78 70 72 4c 69 73 74 28 73 71 6c 69 74 65 33 2a  xprList(sqlite3*
1e1d6 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
1e1d7 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74  , ExprList*);.st
1e1d8 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53  atic void substS
1e1d9 65 6c 65 63 74 28 73 71 6c 69 74 65 33 2a 2c 20  elect(sqlite3*, 
1e1da 53 65 6c 65 63 74 20 2a 2c 20 69 6e 74 2c 20 45  Select *, int, E
1e1db 78 70 72 4c 69 73 74 20 2a 29 3b 0a 0a 2f 2a 0a  xprList *);../*.
1e1dc 2a 2a 20 53 63 61 6e 20 74 68 72 6f 75 67 68 20  ** Scan through 
1e1dd 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70  the expression p
1e1de 45 78 70 72 2e 20 20 52 65 70 6c 61 63 65 20 65  Expr.  Replace e
1e1df 76 65 72 79 20 72 65 66 65 72 65 6e 63 65 20 74  very reference t
1e1e0 6f 0a 2a 2a 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  o.** a column in
1e1e1 20 74 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 54   table number iT
1e1e2 61 62 6c 65 20 77 69 74 68 20 61 20 63 6f 70 79  able with a copy
1e1e3 20 6f 66 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d   of the iColumn-
1e1e4 74 68 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 70  th.** entry in p
1e1e5 45 4c 69 73 74 2e 20 20 28 42 75 74 20 6c 65 61  EList.  (But lea
1e1e6 76 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ve references to
1e1e7 20 74 68 65 20 52 4f 57 49 44 20 63 6f 6c 75 6d   the ROWID colum
1e1e8 6e 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e  n .** unchanged.
1e1e9 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ).**.** This rou
1e1ea 74 69 6e 65 20 69 73 20 70 61 72 74 20 6f 66 20  tine is part of 
1e1eb 74 68 65 20 66 6c 61 74 74 65 6e 69 6e 67 20 70  the flattening p
1e1ec 72 6f 63 65 64 75 72 65 2e 20 20 41 20 73 75 62  rocedure.  A sub
1e1ed 71 75 65 72 79 0a 2a 2a 20 77 68 6f 73 65 20 72  query.** whose r
1e1ee 65 73 75 6c 74 20 73 65 74 20 69 73 20 64 65 66  esult set is def
1e1ef 69 6e 65 64 20 62 79 20 70 45 4c 69 73 74 20 61  ined by pEList a
1e1f0 70 70 65 61 72 73 20 61 73 20 65 6e 74 72 79 20  ppears as entry 
1e1f1 69 6e 20 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63  in the.** FROM c
1e1f2 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
1e1f3 54 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20  T such that the 
1e1f4 56 44 42 45 20 63 75 72 73 6f 72 20 61 73 73 69  VDBE cursor assi
1e1f5 67 6e 65 64 20 74 6f 20 74 68 61 74 0a 2a 2a 20  gned to that.** 
1e1f6 46 4f 52 4d 20 63 6c 61 75 73 65 20 65 6e 74 72  FORM clause entr
1e1f7 79 20 69 73 20 69 54 61 62 6c 65 2e 20 20 54 68  y is iTable.  Th
1e1f8 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20  is routine make 
1e1f9 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 0a 2a  the necessary .*
1e1fa 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 70 45 78  * changes to pEx
1e1fb 70 72 20 73 6f 20 74 68 61 74 20 69 74 20 72 65  pr so that it re
1e1fc 66 65 72 73 20 64 69 72 65 63 74 6c 79 20 74 6f  fers directly to
1e1fd 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
1e1fe 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 71  e.** of the subq
1e1ff 75 65 72 79 20 72 61 74 68 65 72 20 74 68 65 20  uery rather the 
1e200 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
1e201 65 20 73 75 62 71 75 65 72 79 2e 0a 2a 2f 0a 73  e subquery..*/.s
1e202 74 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74  tatic void subst
1e203 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20  Expr(.  sqlite3 
1e204 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 52  *db,        /* R
1e205 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
1e206 6f 72 73 20 74 6f 20 74 68 69 73 20 63 6f 6e 6e  ors to this conn
1e207 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72  ection */.  Expr
1e208 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
1e209 2f 2a 20 45 78 70 72 20 69 6e 20 77 68 69 63 68  /* Expr in which
1e20a 20 73 75 62 73 74 69 74 75 74 69 6f 6e 20 6f 63   substitution oc
1e20b 63 75 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54  curs */.  int iT
1e20c 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  able,         /*
1e20d 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75 62   Table to be sub
1e20e 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45 78  stituted */.  Ex
1e20f 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20  prList *pEList  
1e210 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20    /* Substitute 
1e211 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 29  expressions */.)
1e212 7b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  {.  if( pExpr==0
1e213 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
1e214 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43   pExpr->op==TK_C
1e215 4f 4c 55 4d 4e 20 26 26 20 70 45 78 70 72 2d 3e  OLUMN && pExpr->
1e216 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 29  iTable==iTable )
1e217 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d  {.    if( pExpr-
1e218 3e 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20  >iColumn<0 ){.  
1e219 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
1e21a 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c  TK_NULL;.    }el
1e21b 73 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  se{.      Expr *
1e21c 70 4e 65 77 3b 0a 20 20 20 20 20 20 61 73 73 65  pNew;.      asse
1e21d 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 26 26  rt( pEList!=0 &&
1e21e 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c   pExpr->iColumn<
1e21f 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b  pEList->nExpr );
1e220 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1e221 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26  Expr->pLeft==0 &
1e222 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d  & pExpr->pRight=
1e223 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 70 4c 69  =0 && pExpr->pLi
1e224 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  st==0 );.      p
1e225 4e 65 77 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  New = pEList->a[
1e226 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e  pExpr->iColumn].
1e227 70 45 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73  pExpr;.      ass
1e228 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a  ert( pNew!=0 );.
1e229 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
1e22a 3d 20 70 4e 65 77 2d 3e 6f 70 3b 0a 20 20 20 20  = pNew->op;.    
1e22b 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1e22c 3e 70 4c 65 66 74 3d 3d 30 20 29 3b 0a 20 20 20  >pLeft==0 );.   
1e22d 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20     pExpr->pLeft 
1e22e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
1e22f 28 64 62 2c 20 70 4e 65 77 2d 3e 70 4c 65 66 74  (db, pNew->pLeft
1e230 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1e231 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d   pExpr->pRight==
1e232 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72  0 );.      pExpr
1e233 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74  ->pRight = sqlit
1e234 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4e  e3ExprDup(db, pN
1e235 65 77 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20  ew->pRight);.   
1e236 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1e237 2d 3e 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20  ->pList==0 );.  
1e238 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 69 73 74      pExpr->pList
1e239 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
1e23a 73 74 44 75 70 28 64 62 2c 20 70 4e 65 77 2d 3e  stDup(db, pNew->
1e23b 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70 45  pList);.      pE
1e23c 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 4e  xpr->iTable = pN
1e23d 65 77 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20  ew->iTable;.    
1e23e 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
1e23f 70 4e 65 77 2d 3e 70 54 61 62 3b 0a 20 20 20 20  pNew->pTab;.    
1e240 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
1e241 20 3d 20 70 4e 65 77 2d 3e 69 43 6f 6c 75 6d 6e   = pNew->iColumn
1e242 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69  ;.      pExpr->i
1e243 41 67 67 20 3d 20 70 4e 65 77 2d 3e 69 41 67 67  Agg = pNew->iAgg
1e244 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54  ;.      sqlite3T
1e245 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 45  okenCopy(db, &pE
1e246 78 70 72 2d 3e 74 6f 6b 65 6e 2c 20 26 70 4e 65  xpr->token, &pNe
1e247 77 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  w->token);.     
1e248 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
1e249 79 28 64 62 2c 20 26 70 45 78 70 72 2d 3e 73 70  y(db, &pExpr->sp
1e24a 61 6e 2c 20 26 70 4e 65 77 2d 3e 73 70 61 6e 29  an, &pNew->span)
1e24b 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70  ;.      pExpr->p
1e24c 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
1e24d 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 4e  SelectDup(db, pN
1e24e 65 77 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  ew->pSelect);.  
1e24f 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73      pExpr->flags
1e250 20 3d 20 70 4e 65 77 2d 3e 66 6c 61 67 73 3b 0a   = pNew->flags;.
1e251 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1e252 20 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c     substExpr(db,
1e253 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 69   pExpr->pLeft, i
1e254 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
1e255 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64 62      substExpr(db
1e256 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
1e257 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
1e258 3b 0a 20 20 20 20 73 75 62 73 74 53 65 6c 65 63  ;.    substSelec
1e259 74 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 53 65  t(db, pExpr->pSe
1e25a 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45  lect, iTable, pE
1e25b 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62 73 74  List);.    subst
1e25c 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 45 78  ExprList(db, pEx
1e25d 70 72 2d 3e 70 4c 69 73 74 2c 20 69 54 61 62 6c  pr->pList, iTabl
1e25e 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a  e, pEList);.  }.
1e25f 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75  }.static void su
1e260 62 73 74 45 78 70 72 4c 69 73 74 28 0a 20 20 73  bstExprList(.  s
1e261 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
1e262 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61      /* Report ma
1e263 6c 6c 6f 63 20 65 72 72 6f 72 73 20 68 65 72 65  lloc errors here
1e264 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1e265 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 4c 69  pList,     /* Li
1e266 73 74 20 74 6f 20 73 63 61 6e 20 61 6e 64 20 69  st to scan and i
1e267 6e 20 77 68 69 63 68 20 74 6f 20 6d 61 6b 65 20  n which to make 
1e268 73 75 62 73 74 69 74 75 74 65 73 20 2a 2f 0a 20  substitutes */. 
1e269 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20   int iTable,    
1e26a 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74        /* Table t
1e26b 6f 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64  o be substituted
1e26c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1e26d 70 45 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75  pEList     /* Su
1e26e 62 73 74 69 74 75 74 65 20 76 61 6c 75 65 73 20  bstitute values 
1e26f 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
1e270 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
1e271 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
1e272 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  0; i<pList->nExp
1e273 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 75 62  r; i++){.    sub
1e274 73 74 45 78 70 72 28 64 62 2c 20 70 4c 69 73 74  stExpr(db, pList
1e275 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 69 54  ->a[i].pExpr, iT
1e276 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
1e277 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
1e278 20 73 75 62 73 74 53 65 6c 65 63 74 28 0a 20 20   substSelect(.  
1e279 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
1e27a 20 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d       /* Report m
1e27b 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 68 65 72  alloc errors her
1e27c 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  e */.  Select *p
1e27d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ,           /* S
1e27e 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1e27f 69 6e 20 77 68 69 63 68 20 74 6f 20 6d 61 6b 65  in which to make
1e280 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20 2a   substitutions *
1e281 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
1e282 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
1e283 65 20 74 6f 20 62 65 20 72 65 70 6c 61 63 65 64  e to be replaced
1e284 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1e285 70 45 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75  pEList     /* Su
1e286 62 73 74 69 74 75 74 65 20 76 61 6c 75 65 73 20  bstitute values 
1e287 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 21 70 20 29  */.){.  if( !p )
1e288 20 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73 74   return;.  subst
1e289 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e  ExprList(db, p->
1e28a 70 45 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c 20  pEList, iTable, 
1e28b 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74  pEList);.  subst
1e28c 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e  ExprList(db, p->
1e28d 70 47 72 6f 75 70 42 79 2c 20 69 54 61 62 6c 65  pGroupBy, iTable
1e28e 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62  , pEList);.  sub
1e28f 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70  stExprList(db, p
1e290 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 54 61 62  ->pOrderBy, iTab
1e291 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73  le, pEList);.  s
1e292 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d 3e  ubstExpr(db, p->
1e293 70 48 61 76 69 6e 67 2c 20 69 54 61 62 6c 65 2c  pHaving, iTable,
1e294 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
1e295 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 57 68  tExpr(db, p->pWh
1e296 65 72 65 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ere, iTable, pEL
1e297 69 73 74 29 3b 0a 20 20 73 75 62 73 74 53 65 6c  ist);.  substSel
1e298 65 63 74 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f  ect(db, p->pPrio
1e299 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  r, iTable, pELis
1e29a 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  t);.}.#endif /* 
1e29b 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1e29c 4f 4d 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 23  OMIT_VIEW) */..#
1e29d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1e29e 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68  IT_VIEW./*.** Th
1e29f 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d  is routine attem
1e2a0 70 74 73 20 74 6f 20 66 6c 61 74 74 65 6e 20 73  pts to flatten s
1e2a1 75 62 71 75 65 72 69 65 73 20 69 6e 20 6f 72 64  ubqueries in ord
1e2a2 65 72 20 74 6f 20 73 70 65 65 64 0a 2a 2a 20 65  er to speed.** e
1e2a3 78 65 63 75 74 69 6f 6e 2e 20 20 49 74 20 72 65  xecution.  It re
1e2a4 74 75 72 6e 73 20 31 20 69 66 20 69 74 20 6d 61  turns 1 if it ma
1e2a5 6b 65 73 20 63 68 61 6e 67 65 73 20 61 6e 64 20  kes changes and 
1e2a6 30 20 69 66 20 6e 6f 20 66 6c 61 74 74 65 6e 69  0 if no flatteni
1e2a7 6e 67 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2a  ng.** occurs..**
1e2a8 0a 2a 2a 20 54 6f 20 75 6e 64 65 72 73 74 61 6e  .** To understan
1e2a9 64 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66  d the concept of
1e2aa 20 66 6c 61 74 74 65 6e 69 6e 67 2c 20 63 6f 6e   flattening, con
1e2ab 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77  sider the follow
1e2ac 69 6e 67 0a 2a 2a 20 71 75 65 72 79 3a 0a 2a 2a  ing.** query:.**
1e2ad 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61  .**     SELECT a
1e2ae 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 2b   FROM (SELECT x+
1e2af 79 20 41 53 20 61 20 46 52 4f 4d 20 74 31 20 57  y AS a FROM t1 W
1e2b0 48 45 52 45 20 7a 3c 31 30 30 29 20 57 48 45 52  HERE z<100) WHER
1e2b1 45 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  E a>5.**.** The 
1e2b2 64 65 66 61 75 6c 74 20 77 61 79 20 6f 66 20 69  default way of i
1e2b3 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 69 73  mplementing this
1e2b4 20 71 75 65 72 79 20 69 73 20 74 6f 20 65 78 65   query is to exe
1e2b5 63 75 74 65 20 74 68 65 0a 2a 2a 20 73 75 62 71  cute the.** subq
1e2b6 75 65 72 79 20 66 69 72 73 74 20 61 6e 64 20 73  uery first and s
1e2b7 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
1e2b8 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20   in a temporary 
1e2b9 74 61 62 6c 65 2c 20 74 68 65 6e 0a 2a 2a 20 72  table, then.** r
1e2ba 75 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  un the outer que
1e2bb 72 79 20 6f 6e 20 74 68 61 74 20 74 65 6d 70 6f  ry on that tempo
1e2bc 72 61 72 79 20 74 61 62 6c 65 2e 20 20 54 68 69  rary table.  Thi
1e2bd 73 20 72 65 71 75 69 72 65 73 20 74 77 6f 0a 2a  s requires two.*
1e2be 2a 20 70 61 73 73 65 73 20 6f 76 65 72 20 74 68  * passes over th
1e2bf 65 20 64 61 74 61 2e 20 20 46 75 72 74 68 65 72  e data.  Further
1e2c0 6d 6f 72 65 2c 20 62 65 63 61 75 73 65 20 74 68  more, because th
1e2c1 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
1e2c2 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 69 6e 64 69  e.** has no indi
1e2c3 63 65 73 2c 20 74 68 65 20 57 48 45 52 45 20 63  ces, the WHERE c
1e2c4 6c 61 75 73 65 20 6f 6e 20 74 68 65 20 6f 75 74  lause on the out
1e2c5 65 72 20 71 75 65 72 79 20 63 61 6e 6e 6f 74 20  er query cannot 
1e2c6 62 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a 65 64 2e  be.** optimized.
1e2c7 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1e2c8 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
1e2c9 72 65 77 72 69 74 65 20 71 75 65 72 69 65 73 20  rewrite queries 
1e2ca 73 75 63 68 20 61 73 20 74 68 65 20 61 62 6f 76  such as the abov
1e2cb 65 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e 67  e into.** a sing
1e2cc 6c 65 20 66 6c 61 74 20 73 65 6c 65 63 74 2c 20  le flat select, 
1e2cd 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
1e2ce 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b 79 20       SELECT x+y 
1e2cf 41 53 20 61 20 46 52 4f 4d 20 74 31 20 57 48 45  AS a FROM t1 WHE
1e2d0 52 45 20 7a 3c 31 30 30 20 41 4e 44 20 61 3e 35  RE z<100 AND a>5
1e2d1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
1e2d2 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 74 68  generated for th
1e2d3 69 73 20 73 69 6d 70 69 66 69 63 61 74 69 6f 6e  is simpification
1e2d4 20 67 69 76 65 73 20 74 68 65 20 73 61 6d 65 20   gives the same 
1e2d5 72 65 73 75 6c 74 0a 2a 2a 20 62 75 74 20 6f 6e  result.** but on
1e2d6 6c 79 20 68 61 73 20 74 6f 20 73 63 61 6e 20 74  ly has to scan t
1e2d7 68 65 20 64 61 74 61 20 6f 6e 63 65 2e 20 20 41  he data once.  A
1e2d8 6e 64 20 62 65 63 61 75 73 65 20 69 6e 64 69 63  nd because indic
1e2d9 65 73 20 6d 69 67 68 74 20 0a 2a 2a 20 65 78 69  es might .** exi
1e2da 73 74 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20  st on the table 
1e2db 74 31 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20 73  t1, a complete s
1e2dc 63 61 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20  can of the data 
1e2dd 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 76 6f 69  might be.** avoi
1e2de 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6c 61 74 74  ded..**.** Flatt
1e2df 65 6e 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 74  ening is only at
1e2e0 74 65 6d 70 74 65 64 20 69 66 20 61 6c 6c 20 6f  tempted if all o
1e2e1 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
1e2e2 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20  are true:.**.** 
1e2e3 20 20 28 31 29 20 20 54 68 65 20 73 75 62 71 75    (1)  The subqu
1e2e4 65 72 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65  ery and the oute
1e2e5 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62  r query do not b
1e2e6 6f 74 68 20 75 73 65 20 61 67 67 72 65 67 61 74  oth use aggregat
1e2e7 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20  es..**.**   (2) 
1e2e8 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
1e2e9 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74   not an aggregat
1e2ea 65 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  e or the outer q
1e2eb 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f  uery is not a jo
1e2ec 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20  in..**.**   (3) 
1e2ed 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
1e2ee 20 6e 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f   not the right o
1e2ef 70 65 72 61 6e 64 20 6f 66 20 61 20 6c 65 66 74  perand of a left
1e2f0 20 6f 75 74 65 72 20 6a 6f 69 6e 2c 20 6f 72 0a   outer join, or.
1e2f1 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20 73 75  **        the su
1e2f2 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 69 74  bquery is not it
1e2f3 73 65 6c 66 20 61 20 6a 6f 69 6e 2e 20 20 28 54  self a join.  (T
1e2f4 69 63 6b 65 74 20 23 33 30 36 29 0a 2a 2a 0a 2a  icket #306).**.*
1e2f5 2a 20 20 20 28 34 29 20 20 54 68 65 20 73 75 62  *   (4)  The sub
1e2f6 71 75 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53  query is not DIS
1e2f7 54 49 4e 43 54 20 6f 72 20 74 68 65 20 6f 75 74  TINCT or the out
1e2f8 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20  er query is not 
1e2f9 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  a join..**.**   
1e2fa 28 35 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (5)  The subquer
1e2fb 79 20 69 73 20 6e 6f 74 20 44 49 53 54 49 4e 43  y is not DISTINC
1e2fc 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  T or the outer q
1e2fd 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
1e2fe 65 0a 2a 2a 20 20 20 20 20 20 20 20 61 67 67 72  e.**        aggr
1e2ff 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  egates..**.**   
1e300 28 36 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (6)  The subquer
1e301 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61  y does not use a
1e302 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 68 65  ggregates or the
1e303 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
1e304 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 44 49  not.**        DI
1e305 53 54 49 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20 20 20  STINCT..**.**   
1e306 28 37 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (7)  The subquer
1e307 79 20 68 61 73 20 61 20 46 52 4f 4d 20 63 6c 61  y has a FROM cla
1e308 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 38 29  use..**.**   (8)
1e309 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64    The subquery d
1e30a 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49  oes not use LIMI
1e30b 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  T or the outer q
1e30c 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f  uery is not a jo
1e30d 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 39 29 20  in..**.**   (9) 
1e30e 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
1e30f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54  es not use LIMIT
1e310 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
1e311 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65  ery does not use
1e312 0a 2a 2a 20 20 20 20 20 20 20 20 61 67 67 72 65  .**        aggre
1e313 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31  gates..**.**  (1
1e314 30 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  0)  The subquery
1e315 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67   does not use ag
1e316 67 72 65 67 61 74 65 73 20 6f 72 20 74 68 65 20  gregates or the 
1e317 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73  outer query does
1e318 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 75   not.**        u
1e319 73 65 20 4c 49 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20  se LIMIT..**.** 
1e31a 20 28 31 31 29 20 20 54 68 65 20 73 75 62 71 75   (11)  The subqu
1e31b 65 72 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65  ery and the oute
1e31c 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62  r query do not b
1e31d 6f 74 68 20 68 61 76 65 20 4f 52 44 45 52 20 42  oth have ORDER B
1e31e 59 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a  Y clauses..**.**
1e31f 20 20 28 31 32 29 20 20 54 68 65 20 73 75 62 71    (12)  The subq
1e320 75 65 72 79 20 69 73 20 6e 6f 74 20 74 68 65 20  uery is not the 
1e321 72 69 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20  right term of a 
1e322 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
1e323 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  or the.**       
1e324 20 73 75 62 71 75 65 72 79 20 68 61 73 20 6e 6f   subquery has no
1e325 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
1e326 28 61 64 64 65 64 20 62 79 20 74 69 63 6b 65 74  (added by ticket
1e327 20 23 33 35 30 29 0a 2a 2a 0a 2a 2a 20 20 28 31   #350).**.**  (1
1e328 33 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  3)  The subquery
1e329 20 61 6e 64 20 6f 75 74 65 72 20 71 75 65 72 79   and outer query
1e32a 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 75 73 65   do not both use
1e32b 20 4c 49 4d 49 54 0a 2a 2a 0a 2a 2a 20 20 28 31   LIMIT.**.**  (1
1e32c 34 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  4)  The subquery
1e32d 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4f 46   does not use OF
1e32e 46 53 45 54 0a 2a 2a 0a 2a 2a 20 20 28 31 35 29  FSET.**.**  (15)
1e32f 20 20 54 68 65 20 6f 75 74 65 72 20 71 75 65 72    The outer quer
1e330 79 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  y is not part of
1e331 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
1e332 63 74 20 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20  ct or the.**    
1e333 20 20 20 20 73 75 62 71 75 65 72 79 20 64 6f 65      subquery doe
1e334 73 20 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20  s not have both 
1e335 61 6e 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20  an ORDER BY and 
1e336 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a  a LIMIT clause..
1e337 2a 2a 20 20 20 20 20 20 20 20 28 53 65 65 20 74  **        (See t
1e338 69 63 6b 65 74 20 23 32 33 33 39 29 0a 2a 2a 0a  icket #2339).**.
1e339 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69  ** In this routi
1e33a 6e 65 2c 20 74 68 65 20 22 70 22 20 70 61 72 61  ne, the "p" para
1e33b 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74  meter is a point
1e33c 65 72 20 74 6f 20 74 68 65 20 6f 75 74 65 72 20  er to the outer 
1e33d 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20 73 75  query..** The su
1e33e 62 71 75 65 72 79 20 69 73 20 70 2d 3e 70 53 72  bquery is p->pSr
1e33f 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 20 20 69 73  c->a[iFrom].  is
1e340 41 67 67 20 69 73 20 74 72 75 65 20 69 66 20 74  Agg is true if t
1e341 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 0a 2a  he outer query.*
1e342 2a 20 75 73 65 73 20 61 67 67 72 65 67 61 74 65  * uses aggregate
1e343 73 20 61 6e 64 20 73 75 62 71 75 65 72 79 49 73  s and subqueryIs
1e344 41 67 67 20 69 73 20 74 72 75 65 20 69 66 20 74  Agg is true if t
1e345 68 65 20 73 75 62 71 75 65 72 79 20 75 73 65 73  he subquery uses
1e346 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a   aggregates..**.
1e347 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e 69 6e 67  ** If flattening
1e348 20 69 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65   is not attempte
1e349 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  d, this routine 
1e34a 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 72  is a no-op and r
1e34b 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 20 49 66 20  eturns 0..** If 
1e34c 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 61 74  flattening is at
1e34d 74 65 6d 70 74 65 64 20 74 68 69 73 20 72 6f 75  tempted this rou
1e34e 74 69 6e 65 20 72 65 74 75 72 6e 73 20 31 2e 0a  tine returns 1..
1e34f 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65  **.** All of the
1e350 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 61 6c   expression anal
1e351 79 73 69 73 20 6d 75 73 74 20 6f 63 63 75 72 20  ysis must occur 
1e352 6f 6e 20 62 6f 74 68 20 74 68 65 20 6f 75 74 65  on both the oute
1e353 72 20 71 75 65 72 79 20 61 6e 64 0a 2a 2a 20 74  r query and.** t
1e354 68 65 20 73 75 62 71 75 65 72 79 20 62 65 66 6f  he subquery befo
1e355 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  re this routine 
1e356 72 75 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  runs..*/.static 
1e357 69 6e 74 20 66 6c 61 74 74 65 6e 53 75 62 71 75  int flattenSubqu
1e358 65 72 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ery(.  sqlite3 *
1e359 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 44  db,         /* D
1e35a 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1e35b 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  on */.  Select *
1e35c 70 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p,           /* 
1e35d 54 68 65 20 70 61 72 65 6e 74 20 6f 72 20 6f 75  The parent or ou
1e35e 74 65 72 20 53 45 4c 45 43 54 20 73 74 61 74 65  ter SELECT state
1e35f 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 46  ment */.  int iF
1e360 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f  rom,           /
1e361 2a 20 49 6e 64 65 78 20 69 6e 20 70 2d 3e 70 53  * Index in p->pS
1e362 72 63 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 20 69  rc->a[] of the i
1e363 6e 6e 65 72 20 73 75 62 71 75 65 72 79 20 2a 2f  nner subquery */
1e364 0a 20 20 69 6e 74 20 69 73 41 67 67 2c 20 20 20  .  int isAgg,   
1e365 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1e366 69 66 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20  if outer SELECT 
1e367 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66  uses aggregate f
1e368 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  unctions */.  in
1e369 74 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20  t subqueryIsAgg 
1e36a 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
1e36b 65 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20  e subquery uses 
1e36c 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
1e36d 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65  ons */.){.  Sele
1e36e 63 74 20 2a 70 53 75 62 3b 20 20 20 20 20 20 20  ct *pSub;       
1e36f 2f 2a 20 54 68 65 20 69 6e 6e 65 72 20 71 75 65  /* The inner que
1e370 72 79 20 6f 72 20 22 73 75 62 71 75 65 72 79 22  ry or "subquery"
1e371 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
1e372 53 72 63 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  Src;      /* The
1e373 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
1e374 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
1e375 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
1e376 75 62 53 72 63 3b 20 20 20 2f 2a 20 54 68 65 20  ubSrc;   /* The 
1e377 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
1e378 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20  he subquery */. 
1e379 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1e37a 3b 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75  ;    /* The resu
1e37b 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 6f 75  lt set of the ou
1e37c 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 20 20 69  ter query */.  i
1e37d 6e 74 20 69 50 61 72 65 6e 74 3b 20 20 20 20 20  nt iParent;     
1e37e 20 20 20 2f 2a 20 56 44 42 45 20 63 75 72 73 6f     /* VDBE curso
1e37f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  r number of the 
1e380 70 53 75 62 20 72 65 73 75 6c 74 20 73 65 74 20  pSub result set 
1e381 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20  temp table */.  
1e382 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1e383 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1e384 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ter */.  Expr *p
1e385 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 20 20  Where;          
1e386 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1e387 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
1e388 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
1e389 74 5f 69 74 65 6d 20 2a 70 53 75 62 69 74 65 6d  t_item *pSubitem
1e38a 3b 20 20 20 2f 2a 20 54 68 65 20 73 75 62 71 75  ;   /* The subqu
1e38b 65 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65  ery */..  /* Che
1e38c 63 6b 20 74 6f 20 73 65 65 20 69 66 20 66 6c 61  ck to see if fla
1e38d 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69  ttening is permi
1e38e 74 74 65 64 2e 20 20 52 65 74 75 72 6e 20 30 20  tted.  Return 0 
1e38f 69 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 69  if not..  */.  i
1e390 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
1e391 20 30 3b 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e   0;.  pSrc = p->
1e392 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  pSrc;.  assert( 
1e393 70 53 72 63 20 26 26 20 69 46 72 6f 6d 3e 3d 30  pSrc && iFrom>=0
1e394 20 26 26 20 69 46 72 6f 6d 3c 70 53 72 63 2d 3e   && iFrom<pSrc->
1e395 6e 53 72 63 20 29 3b 0a 20 20 70 53 75 62 69 74  nSrc );.  pSubit
1e396 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 46  em = &pSrc->a[iF
1e397 72 6f 6d 5d 3b 0a 20 20 70 53 75 62 20 3d 20 70  rom];.  pSub = p
1e398 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74  Subitem->pSelect
1e399 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 75 62  ;.  assert( pSub
1e39a 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 41  !=0 );.  if( isA
1e39b 67 67 20 26 26 20 73 75 62 71 75 65 72 79 49 73  gg && subqueryIs
1e39c 41 67 67 20 29 20 72 65 74 75 72 6e 20 30 3b 20  Agg ) return 0; 
1e39d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e39e 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28  /* Restriction (
1e39f 31 29 20 20 2a 2f 0a 20 20 69 66 28 20 73 75 62  1)  */.  if( sub
1e3a0 71 75 65 72 79 49 73 41 67 67 20 26 26 20 70 53  queryIsAgg && pS
1e3a1 72 63 2d 3e 6e 53 72 63 3e 31 20 29 20 72 65 74  rc->nSrc>1 ) ret
1e3a2 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
1e3a3 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28  /* Restriction (
1e3a4 32 29 20 20 2a 2f 0a 20 20 70 53 75 62 53 72 63  2)  */.  pSubSrc
1e3a5 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b 0a 20   = pSub->pSrc;. 
1e3a6 20 61 73 73 65 72 74 28 20 70 53 75 62 53 72 63   assert( pSubSrc
1e3a7 20 29 3b 0a 20 20 2f 2a 20 50 72 69 6f 72 20 74   );.  /* Prior t
1e3a8 6f 20 76 65 72 73 69 6f 6e 20 33 2e 31 2e 32 2c  o version 3.1.2,
1e3a9 20 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20   when LIMIT and 
1e3aa 4f 46 46 53 45 54 20 68 61 64 20 74 6f 20 62 65  OFFSET had to be
1e3ab 20 73 69 6d 70 6c 65 20 63 6f 6e 73 74 61 6e 74   simple constant
1e3ac 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20 61 72 62 69  s,.  ** not arbi
1e3ad 74 72 61 72 79 20 65 78 70 72 65 73 73 73 69 6f  trary expresssio
1e3ae 6e 73 2c 20 77 65 20 61 6c 6c 6f 77 65 64 20 73  ns, we allowed s
1e3af 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e 67 20 6f 66  ome combining of
1e3b0 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
1e3b1 54 0a 20 20 2a 2a 20 62 65 63 61 75 73 65 20 74  T.  ** because t
1e3b2 68 65 79 20 63 6f 75 6c 64 20 62 65 20 63 6f 6d  hey could be com
1e3b3 70 75 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  puted at compile
1e3b4 2d 74 69 6d 65 2e 20 20 42 75 74 20 77 68 65 6e  -time.  But when
1e3b5 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
1e3b6 54 0a 20 20 2a 2a 20 62 65 63 61 6d 65 20 61 72  T.  ** became ar
1e3b7 62 69 74 72 61 72 79 20 65 78 70 72 65 73 73 69  bitrary expressi
1e3b8 6f 6e 73 2c 20 77 65 20 77 65 72 65 20 66 6f 72  ons, we were for
1e3b9 63 65 64 20 74 6f 20 61 64 64 20 72 65 73 74 72  ced to add restr
1e3ba 69 63 74 69 6f 6e 73 20 28 31 33 29 0a 20 20 2a  ictions (13).  *
1e3bb 2a 20 61 6e 64 20 28 31 34 29 2e 20 2a 2f 0a 20  * and (14). */. 
1e3bc 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69   if( pSub->pLimi
1e3bd 74 20 26 26 20 70 2d 3e 70 4c 69 6d 69 74 20 29  t && p->pLimit )
1e3be 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
1e3bf 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
1e3c0 69 63 74 69 6f 6e 20 28 31 33 29 20 2a 2f 0a 20  iction (13) */. 
1e3c1 20 69 66 28 20 70 53 75 62 2d 3e 70 4f 66 66 73   if( pSub->pOffs
1e3c2 65 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  et ) return 0;  
1e3c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e3c4 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
1e3c5 69 63 74 69 6f 6e 20 28 31 34 29 20 2a 2f 0a 20  iction (14) */. 
1e3c6 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f   if( p->pRightmo
1e3c7 73 74 20 26 26 20 70 53 75 62 2d 3e 70 4c 69 6d  st && pSub->pLim
1e3c8 69 74 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64  it && pSub->pOrd
1e3c9 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75  erBy ){.    retu
1e3ca 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
1e3cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e3cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e3cd 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
1e3ce 28 31 35 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  (15) */.  }.  if
1e3cf 28 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63 3d  ( pSubSrc->nSrc=
1e3d0 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  =0 ) return 0;  
1e3d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e3d2 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
1e3d3 69 6f 6e 20 28 37 29 20 20 2a 2f 0a 20 20 69 66  ion (7)  */.  if
1e3d4 28 20 28 70 53 75 62 2d 3e 69 73 44 69 73 74 69  ( (pSub->isDisti
1e3d5 6e 63 74 20 7c 7c 20 70 53 75 62 2d 3e 70 4c 69  nct || pSub->pLi
1e3d6 6d 69 74 29 20 0a 20 20 20 20 20 20 20 20 20 26  mit) .         &
1e3d7 26 20 28 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20  & (pSrc->nSrc>1 
1e3d8 7c 7c 20 69 73 41 67 67 29 20 29 7b 20 20 20 20  || isAgg) ){    
1e3d9 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
1e3da 74 69 6f 6e 73 20 28 34 29 28 35 29 28 38 29 28  tions (4)(5)(8)(
1e3db 39 29 20 2a 2f 0a 20 20 20 20 20 72 65 74 75 72  9) */.     retur
1e3dc 6e 20 30 3b 20 20 20 20 20 20 20 0a 20 20 7d 0a  n 0;       .  }.
1e3dd 20 20 69 66 28 20 70 2d 3e 69 73 44 69 73 74 69    if( p->isDisti
1e3de 6e 63 74 20 26 26 20 73 75 62 71 75 65 72 79 49  nct && subqueryI
1e3df 73 41 67 67 20 29 20 72 65 74 75 72 6e 20 30 3b  sAgg ) return 0;
1e3e0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
1e3e1 72 69 63 74 69 6f 6e 20 28 36 29 20 20 2a 2f 0a  riction (6)  */.
1e3e2 20 20 69 66 28 20 28 70 2d 3e 64 69 73 61 6c 6c    if( (p->disall
1e3e3 6f 77 4f 72 64 65 72 42 79 20 7c 7c 20 70 2d 3e  owOrderBy || p->
1e3e4 70 4f 72 64 65 72 42 79 29 20 26 26 20 70 53 75  pOrderBy) && pSu
1e3e5 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  b->pOrderBy ){. 
1e3e6 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
1e3e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e3e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e3e9 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
1e3ea 69 63 74 69 6f 6e 20 28 31 31 29 20 2a 2f 0a 20  iction (11) */. 
1e3eb 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 72 69 63   }..  /* Restric
1e3ec 74 69 6f 6e 20 33 3a 20 20 49 66 20 74 68 65 20  tion 3:  If the 
1e3ed 73 75 62 71 75 65 72 79 20 69 73 20 61 20 6a 6f  subquery is a jo
1e3ee 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68  in, make sure th
1e3ef 65 20 73 75 62 71 75 65 72 79 20 69 73 20 0a 20  e subquery is . 
1e3f0 20 2a 2a 20 6e 6f 74 20 75 73 65 64 20 61 73 20   ** not used as 
1e3f1 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  the right operan
1e3f2 64 20 6f 66 20 61 6e 20 6f 75 74 65 72 20 6a 6f  d of an outer jo
1e3f3 69 6e 2e 20 20 45 78 61 6d 70 6c 65 73 20 6f 66  in.  Examples of
1e3f4 20 77 68 79 20 74 68 69 73 0a 20 20 2a 2a 20 69   why this.  ** i
1e3f5 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20  s not allowed:. 
1e3f6 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
1e3f7 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a   t1 LEFT OUTER J
1e3f8 4f 49 4e 20 28 74 32 20 4a 4f 49 4e 20 74 33 29  OIN (t2 JOIN t3)
1e3f9 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65  .  **.  ** If we
1e3fa 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62 6f   flatten the abo
1e3fb 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65 74  ve, we would get
1e3fc 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
1e3fd 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55 54 45     (t1 LEFT OUTE
1e3fe 52 20 4a 4f 49 4e 20 74 32 29 20 4a 4f 49 4e 20  R JOIN t2) JOIN 
1e3ff 74 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20 77 68 69  t3.  **.  ** whi
1e400 63 68 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c  ch is not at all
1e401 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e   the same thing.
1e402 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62  .  */.  if( pSub
1e403 53 72 63 2d 3e 6e 53 72 63 3e 31 20 26 26 20 28  Src->nSrc>1 && (
1e404 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pSubitem->jointy
1e405 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d  pe & JT_OUTER)!=
1e406 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1e407 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73  0;.  }..  /* Res
1e408 74 72 69 63 74 69 6f 6e 20 31 32 3a 20 20 49 66  triction 12:  If
1e409 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
1e40a 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
1e40b 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74  nd of a left out
1e40c 65 72 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20 6d 61  er.  ** join, ma
1e40d 6b 65 20 73 75 72 65 20 74 68 65 20 73 75 62 71  ke sure the subq
1e40e 75 65 72 79 20 68 61 73 20 6e 6f 20 57 48 45 52  uery has no WHER
1e40f 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 20 41  E clause..  ** A
1e410 6e 20 65 78 61 6d 70 6c 65 73 20 6f 66 20 77 68  n examples of wh
1e411 79 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6c  y this is not al
1e412 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  lowed:.  **.  **
1e413 20 20 20 20 20 20 20 20 20 74 31 20 4c 45 46 54           t1 LEFT
1e414 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 53 45 4c   OUTER JOIN (SEL
1e415 45 43 54 20 2a 20 46 52 4f 4d 20 74 32 20 57 48  ECT * FROM t2 WH
1e416 45 52 45 20 74 32 2e 78 3e 30 29 0a 20 20 2a 2a  ERE t2.x>0).  **
1e417 0a 20 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74  .  ** If we flat
1e418 74 65 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77  ten the above, w
1e419 65 20 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a  e would get.  **
1e41a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74  .  **         (t
1e41b 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  1 LEFT OUTER JOI
1e41c 4e 20 74 32 29 20 57 48 45 52 45 20 74 32 2e 78  N t2) WHERE t2.x
1e41d 3e 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 75 74  >0.  **.  ** But
1e41e 20 74 68 65 20 74 32 2e 78 3e 30 20 74 65 73 74   the t2.x>0 test
1e41f 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 66 61 69   will always fai
1e420 6c 20 6f 6e 20 61 20 4e 55 4c 4c 20 72 6f 77 20  l on a NULL row 
1e421 6f 66 20 74 32 2c 20 77 68 69 63 68 0a 20 20 2a  of t2, which.  *
1e422 2a 20 65 66 66 65 63 74 69 76 65 6c 79 20 63 6f  * effectively co
1e423 6e 76 65 72 74 73 20 74 68 65 20 4f 55 54 45 52  nverts the OUTER
1e424 20 4a 4f 49 4e 20 69 6e 74 6f 20 61 6e 20 49 4e   JOIN into an IN
1e425 4e 45 52 20 4a 4f 49 4e 2e 0a 20 20 2a 2f 0a 20  NER JOIN..  */. 
1e426 20 69 66 28 20 28 70 53 75 62 69 74 65 6d 2d 3e   if( (pSubitem->
1e427 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f 55  jointype & JT_OU
1e428 54 45 52 29 21 3d 30 20 26 26 20 70 53 75 62 2d  TER)!=0 && pSub-
1e429 3e 70 57 68 65 72 65 21 3d 30 20 29 7b 0a 20 20  >pWhere!=0 ){.  
1e42a 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1e42b 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63  .  /* If we reac
1e42c 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74  h this point, it
1e42d 20 6d 65 61 6e 73 20 66 6c 61 74 74 65 6e 69 6e   means flattenin
1e42e 67 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 66  g is permitted f
1e42f 6f 72 20 74 68 65 0a 20 20 2a 2a 20 69 46 72 6f  or the.  ** iFro
1e430 6d 2d 74 68 20 65 6e 74 72 79 20 6f 66 20 74 68  m-th entry of th
1e431 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e  e FROM clause in
1e432 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
1e433 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 6f 76  ..  */..  /* Mov
1e434 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 46 52 4f  e all of the FRO
1e435 4d 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  M elements of th
1e436 65 20 73 75 62 71 75 65 72 79 20 69 6e 74 6f 20  e subquery into 
1e437 74 68 65 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f  the.  ** the FRO
1e438 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  M clause of the 
1e439 6f 75 74 65 72 20 71 75 65 72 79 2e 20 20 42 65  outer query.  Be
1e43a 66 6f 72 65 20 64 6f 69 6e 67 20 74 68 69 73 2c  fore doing this,
1e43b 20 72 65 6d 65 6d 62 65 72 0a 20 20 2a 2a 20 74   remember.  ** t
1e43c 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
1e43d 20 66 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61   for the origina
1e43e 6c 20 6f 75 74 65 72 20 71 75 65 72 79 20 46 52  l outer query FR
1e43f 4f 4d 20 65 6c 65 6d 65 6e 74 20 69 6e 0a 20 20  OM element in.  
1e440 2a 2a 20 69 50 61 72 65 6e 74 2e 20 20 54 68 65  ** iParent.  The
1e441 20 69 50 61 72 65 6e 74 20 63 75 72 73 6f 72 20   iParent cursor 
1e442 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73  will never be us
1e443 65 64 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20  ed.  Subsequent 
1e444 63 6f 64 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 73  code.  ** will s
1e445 63 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20  can expressions 
1e446 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 69 50 61 72  looking for iPar
1e447 65 6e 74 20 72 65 66 65 72 65 6e 63 65 73 20 61  ent references a
1e448 6e 64 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20  nd replace.  ** 
1e449 74 68 6f 73 65 20 72 65 66 65 72 65 6e 63 65 73  those references
1e44a 20 77 69 74 68 20 65 78 70 72 65 73 73 69 6f 6e   with expression
1e44b 73 20 74 68 61 74 20 72 65 73 6f 6c 76 65 20 74  s that resolve t
1e44c 6f 20 74 68 65 20 73 75 62 71 75 65 72 79 20 46  o the subquery F
1e44d 52 4f 4d 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74  ROM.  ** element
1e44e 73 20 77 65 20 61 72 65 20 6e 6f 77 20 63 6f 70  s we are now cop
1e44f 79 69 6e 67 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20  ying in..  */.  
1e450 69 50 61 72 65 6e 74 20 3d 20 70 53 75 62 69 74  iParent = pSubit
1e451 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 7b  em->iCursor;.  {
1e452 0a 20 20 20 20 69 6e 74 20 6e 53 75 62 53 72 63  .    int nSubSrc
1e453 20 3d 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63   = pSubSrc->nSrc
1e454 3b 0a 20 20 20 20 69 6e 74 20 6a 6f 69 6e 74 79  ;.    int jointy
1e455 70 65 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 6a  pe = pSubitem->j
1e456 6f 69 6e 74 79 70 65 3b 0a 0a 20 20 20 20 73 71  ointype;..    sq
1e457 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
1e458 28 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 29  (pSubitem->pTab)
1e459 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
1e45a 65 65 28 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61  ee(pSubitem->zDa
1e45b 74 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71 6c  tabase);.    sql
1e45c 69 74 65 33 5f 66 72 65 65 28 70 53 75 62 69 74  ite3_free(pSubit
1e45d 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  em->zName);.    
1e45e 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 53 75  sqlite3_free(pSu
1e45f 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  bitem->zAlias);.
1e460 20 20 20 20 69 66 28 20 6e 53 75 62 53 72 63 3e      if( nSubSrc>
1e461 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65  1 ){.      int e
1e462 78 74 72 61 20 3d 20 6e 53 75 62 53 72 63 20 2d  xtra = nSubSrc -
1e463 20 31 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   1;.      for(i=
1e464 31 3b 20 69 3c 6e 53 75 62 53 72 63 3b 20 69 2b  1; i<nSubSrc; i+
1e465 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 53 72 63  +){.        pSrc
1e466 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
1e467 74 41 70 70 65 6e 64 28 64 62 2c 20 70 53 72 63  tAppend(db, pSrc
1e468 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  , 0, 0);.      }
1e469 0a 20 20 20 20 20 20 70 2d 3e 70 53 72 63 20 3d  .      p->pSrc =
1e46a 20 70 53 72 63 3b 0a 20 20 20 20 20 20 66 6f 72   pSrc;.      for
1e46b 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b  (i=pSrc->nSrc-1;
1e46c 20 69 2d 65 78 74 72 61 3e 3d 69 46 72 6f 6d 3b   i-extra>=iFrom;
1e46d 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 70   i--){.        p
1e46e 53 72 63 2d 3e 61 5b 69 5d 20 3d 20 70 53 72 63  Src->a[i] = pSrc
1e46f 2d 3e 61 5b 69 2d 65 78 74 72 61 5d 3b 0a 20 20  ->a[i-extra];.  
1e470 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1e471 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 75 62 53  for(i=0; i<nSubS
1e472 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rc; i++){.      
1e473 70 53 72 63 2d 3e 61 5b 69 2b 69 46 72 6f 6d 5d  pSrc->a[i+iFrom]
1e474 20 3d 20 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d   = pSubSrc->a[i]
1e475 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  ;.      memset(&
1e476 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 2c 20 30  pSubSrc->a[i], 0
1e477 2c 20 73 69 7a 65 6f 66 28 70 53 75 62 53 72 63  , sizeof(pSubSrc
1e478 2d 3e 61 5b 69 5d 29 29 3b 0a 20 20 20 20 7d 0a  ->a[i]));.    }.
1e479 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 46 72 6f      pSrc->a[iFro
1e47a 6d 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 6a 6f  m].jointype = jo
1e47b 69 6e 74 79 70 65 3b 0a 20 20 7d 0a 0a 20 20 2f  intype;.  }..  /
1e47c 2a 20 4e 6f 77 20 62 65 67 69 6e 20 73 75 62 73  * Now begin subs
1e47d 74 69 74 75 74 69 6e 67 20 73 75 62 71 75 65 72  tituting subquer
1e47e 79 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70  y result set exp
1e47f 72 65 73 73 69 6f 6e 73 20 66 6f 72 20 0a 20 20  ressions for .  
1e480 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ** references to
1e481 20 74 68 65 20 69 50 61 72 65 6e 74 20 69 6e 20   the iParent in 
1e482 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e  the outer query.
1e483 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 45 78 61 6d  .  ** .  ** Exam
1e484 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ple:.  **.  **  
1e485 20 53 45 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31   SELECT a+5, b*1
1e486 30 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78  0 FROM (SELECT x
1e487 2a 33 20 41 53 20 61 2c 20 79 2b 31 30 20 41 53  *3 AS a, y+10 AS
1e488 20 62 20 46 52 4f 4d 20 74 31 29 20 57 48 45 52   b FROM t1) WHER
1e489 45 20 61 3e 62 3b 0a 20 20 2a 2a 20 20 20 5c 20  E a>b;.  **   \ 
1e48a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e48b 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
1e48c 5f 5f 20 73 75 62 71 75 65 72 79 20 5f 5f 5f 5f  __ subquery ____
1e48d 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 20 20  ______/         
1e48e 20 2f 0a 20 20 2a 2a 20 20 20 20 5c 5f 5f 5f 5f   /.  **    \____
1e48f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
1e490 5f 20 6f 75 74 65 72 20 71 75 65 72 79 20 5f 5f  _ outer query __
1e491 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
1e492 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 20 20  ____________/.  
1e493 2a 2a 0a 20 20 2a 2a 20 57 65 20 6c 6f 6f 6b 20  **.  ** We look 
1e494 61 74 20 65 76 65 72 79 20 65 78 70 72 65 73 73  at every express
1e495 69 6f 6e 20 69 6e 20 74 68 65 20 6f 75 74 65 72  ion in the outer
1e496 20 71 75 65 72 79 20 61 6e 64 20 65 76 65 72 79   query and every
1e497 20 70 6c 61 63 65 20 77 65 20 73 65 65 0a 20 20   place we see.  
1e498 2a 2a 20 22 61 22 20 77 65 20 73 75 62 73 74 69  ** "a" we substi
1e499 74 75 74 65 20 22 78 2a 33 22 20 61 6e 64 20 65  tute "x*3" and e
1e49a 76 65 72 79 20 70 6c 61 63 65 20 77 65 20 73 65  very place we se
1e49b 65 20 22 62 22 20 77 65 20 73 75 62 73 74 69 74  e "b" we substit
1e49c 75 74 65 20 22 79 2b 31 30 22 2e 0a 20 20 2a 2f  ute "y+10"..  */
1e49d 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 45  .  pList = p->pE
1e49e 4c 69 73 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  List;.  for(i=0;
1e49f 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
1e4a0 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20   i++){.    Expr 
1e4a1 2a 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20  *pExpr;.    if( 
1e4a2 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  pList->a[i].zNam
1e4a3 65 3d 3d 30 20 26 26 20 28 70 45 78 70 72 20 3d  e==0 && (pExpr =
1e4a4 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
1e4a5 70 72 29 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 29  pr)->span.z!=0 )
1e4a6 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61  {.      pList->a
1e4a7 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 0a 20 20 20  [i].zName = .   
1e4a8 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1e4a9 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
1e4aa 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 73 70 61  char*)pExpr->spa
1e4ab 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 73 70 61 6e  n.z, pExpr->span
1e4ac 2e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  .n);.    }.  }. 
1e4ad 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
1e4ae 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69 50  b, p->pEList, iP
1e4af 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c  arent, pSub->pEL
1e4b0 69 73 74 29 3b 0a 20 20 69 66 28 20 69 73 41 67  ist);.  if( isAg
1e4b1 67 20 29 7b 0a 20 20 20 20 73 75 62 73 74 45 78  g ){.    substEx
1e4b2 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 47  prList(db, p->pG
1e4b3 72 6f 75 70 42 79 2c 20 69 50 61 72 65 6e 74 2c  roupBy, iParent,
1e4b4 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
1e4b5 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64 62      substExpr(db
1e4b6 2c 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50  , p->pHaving, iP
1e4b7 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c  arent, pSub->pEL
1e4b8 69 73 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ist);.  }.  if( 
1e4b9 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29  pSub->pOrderBy )
1e4ba 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  {.    assert( p-
1e4bb 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a  >pOrderBy==0 );.
1e4bc 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20      p->pOrderBy 
1e4bd 3d 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79  = pSub->pOrderBy
1e4be 3b 0a 20 20 20 20 70 53 75 62 2d 3e 70 4f 72 64  ;.    pSub->pOrd
1e4bf 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  erBy = 0;.  }els
1e4c0 65 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42  e if( p->pOrderB
1e4c1 79 20 29 7b 0a 20 20 20 20 73 75 62 73 74 45 78  y ){.    substEx
1e4c2 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 4f  prList(db, p->pO
1e4c3 72 64 65 72 42 79 2c 20 69 50 61 72 65 6e 74 2c  rderBy, iParent,
1e4c4 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
1e4c5 20 20 7d 0a 20 20 69 66 28 20 70 53 75 62 2d 3e    }.  if( pSub->
1e4c6 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 70 57  pWhere ){.    pW
1e4c7 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78  here = sqlite3Ex
1e4c8 70 72 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e  prDup(db, pSub->
1e4c9 70 57 68 65 72 65 29 3b 0a 20 20 7d 65 6c 73 65  pWhere);.  }else
1e4ca 7b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 30  {.    pWhere = 0
1e4cb 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 75 62 71  ;.  }.  if( subq
1e4cc 75 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20  ueryIsAgg ){.   
1e4cd 20 61 73 73 65 72 74 28 20 70 2d 3e 70 48 61 76   assert( p->pHav
1e4ce 69 6e 67 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d  ing==0 );.    p-
1e4cf 3e 70 48 61 76 69 6e 67 20 3d 20 70 2d 3e 70 57  >pHaving = p->pW
1e4d0 68 65 72 65 3b 0a 20 20 20 20 70 2d 3e 70 57 68  here;.    p->pWh
1e4d1 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20  ere = pWhere;.  
1e4d2 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20    substExpr(db, 
1e4d3 70 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72  p->pHaving, iPar
1e4d4 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73  ent, pSub->pELis
1e4d5 74 29 3b 0a 20 20 20 20 70 2d 3e 70 48 61 76 69  t);.    p->pHavi
1e4d6 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ng = sqlite3Expr
1e4d7 41 6e 64 28 64 62 2c 20 70 2d 3e 70 48 61 76 69  And(db, p->pHavi
1e4d8 6e 67 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ng, .           
1e4d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e4da 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1e4db 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 48  Dup(db, pSub->pH
1e4dc 61 76 69 6e 67 29 29 3b 0a 20 20 20 20 61 73 73  aving));.    ass
1e4dd 65 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79  ert( p->pGroupBy
1e4de 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 70 47  ==0 );.    p->pG
1e4df 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33  roupBy = sqlite3
1e4e0 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
1e4e1 70 53 75 62 2d 3e 70 47 72 6f 75 70 42 79 29 3b  pSub->pGroupBy);
1e4e2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 75  .  }else{.    su
1e4e3 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70  bstExpr(db, p->p
1e4e4 57 68 65 72 65 2c 20 69 50 61 72 65 6e 74 2c 20  Where, iParent, 
1e4e5 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  pSub->pEList);. 
1e4e6 20 20 20 70 2d 3e 70 57 68 65 72 65 20 3d 20 73     p->pWhere = s
1e4e7 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62  qlite3ExprAnd(db
1e4e8 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 70 57 68  , p->pWhere, pWh
1e4e9 65 72 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ere);.  }..  /* 
1e4ea 54 68 65 20 66 6c 61 74 74 65 6e 65 64 20 71 75  The flattened qu
1e4eb 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74 20  ery is distinct 
1e4ec 69 66 20 65 69 74 68 65 72 20 74 68 65 20 69 6e  if either the in
1e4ed 6e 65 72 20 6f 72 20 74 68 65 0a 20 20 2a 2a 20  ner or the.  ** 
1e4ee 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 64  outer query is d
1e4ef 69 73 74 69 6e 63 74 2e 20 0a 20 20 2a 2f 0a 20  istinct. .  */. 
1e4f0 20 70 2d 3e 69 73 44 69 73 74 69 6e 63 74 20 3d   p->isDistinct =
1e4f1 20 70 2d 3e 69 73 44 69 73 74 69 6e 63 74 20 7c   p->isDistinct |
1e4f2 7c 20 70 53 75 62 2d 3e 69 73 44 69 73 74 69 6e  | pSub->isDistin
1e4f3 63 74 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53  ct;..  /*.  ** S
1e4f4 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 28  ELECT ... FROM (
1e4f5 53 45 4c 45 43 54 20 2e 2e 2e 20 4c 49 4d 49 54  SELECT ... LIMIT
1e4f6 20 61 20 4f 46 46 53 45 54 20 62 29 20 4c 49 4d   a OFFSET b) LIM
1e4f7 49 54 20 78 20 4f 46 46 53 45 54 20 79 3b 0a 20  IT x OFFSET y;. 
1e4f8 20 2a 2a 0a 20 20 2a 2a 20 4f 6e 65 20 69 73 20   **.  ** One is 
1e4f9 74 65 6d 70 74 65 64 20 74 6f 20 74 72 79 20 74  tempted to try t
1e4fa 6f 20 61 64 64 20 61 20 61 6e 64 20 62 20 74 6f  o add a and b to
1e4fb 20 63 6f 6d 62 69 6e 65 20 74 68 65 20 6c 69 6d   combine the lim
1e4fc 69 74 73 2e 20 20 42 75 74 20 74 68 69 73 0a 20  its.  But this. 
1e4fd 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72   ** does not wor
1e4fe 6b 20 69 66 20 65 69 74 68 65 72 20 6c 69 6d 69  k if either limi
1e4ff 74 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 20  t is negative.. 
1e500 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e   */.  if( pSub->
1e501 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 70 2d  pLimit ){.    p-
1e502 3e 70 4c 69 6d 69 74 20 3d 20 70 53 75 62 2d 3e  >pLimit = pSub->
1e503 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70 53 75 62  pLimit;.    pSub
1e504 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->pLimit = 0;.  
1e505 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 61 6c 6c 79  }..  /* Finially
1e506 2c 20 64 65 6c 65 74 65 20 77 68 61 74 20 69 73  , delete what is
1e507 20 6c 65 66 74 20 6f 66 20 74 68 65 20 73 75 62   left of the sub
1e508 71 75 65 72 79 20 61 6e 64 20 72 65 74 75 72 6e  query and return
1e509 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 2e 0a 20  .  ** success.. 
1e50a 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c   */.  sqlite3Sel
1e50b 65 63 74 44 65 6c 65 74 65 28 70 53 75 62 29 3b  ectDelete(pSub);
1e50c 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
1e50d 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1e50e 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f 2a  OMIT_VIEW */../*
1e50f 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20  .** Analyze the 
1e510 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1e511 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 61 6e   passed in as an
1e512 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 65 65   argument to see
1e513 20 69 66 20 69 74 0a 2a 2a 20 69 73 20 61 20 73   if it.** is a s
1e514 69 6d 70 6c 65 20 6d 69 6e 28 29 20 6f 72 20 6d  imple min() or m
1e515 61 78 28 29 20 71 75 65 72 79 2e 20 20 49 66 20  ax() query.  If 
1e516 69 74 20 69 73 20 61 6e 64 20 74 68 69 73 20 71  it is and this q
1e517 75 65 72 79 20 63 61 6e 20 62 65 0a 2a 2a 20 73  uery can be.** s
1e518 61 74 69 73 66 69 65 64 20 75 73 69 6e 67 20 61  atisfied using a
1e519 20 73 69 6e 67 6c 65 20 73 65 65 6b 20 74 6f 20   single seek to 
1e51a 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 72  the beginning or
1e51b 20 65 6e 64 20 6f 66 20 61 6e 20 69 6e 64 65 78   end of an index
1e51c 2c 0a 2a 2a 20 74 68 65 6e 20 67 65 6e 65 72 61  ,.** then genera
1e51d 74 65 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20  te the code for 
1e51e 74 68 69 73 20 53 45 4c 45 43 54 20 61 6e 64 20  this SELECT and 
1e51f 72 65 74 75 72 6e 20 31 2e 20 20 49 66 20 74 68  return 1.  If th
1e520 69 73 20 69 73 20 6e 6f 74 20 61 20 0a 2a 2a 20  is is not a .** 
1e521 73 69 6d 70 6c 65 20 6d 69 6e 28 29 20 6f 72 20  simple min() or 
1e522 6d 61 78 28 29 20 71 75 65 72 79 2c 20 74 68 65  max() query, the
1e523 6e 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 0a 2a  n return 0;.**.*
1e524 2a 20 41 20 73 69 6d 70 6c 79 20 6d 69 6e 28 29  * A simply min()
1e525 20 6f 72 20 6d 61 78 28 29 20 71 75 65 72 79 20   or max() query 
1e526 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
1e527 0a 2a 2a 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54  .**.**    SELECT
1e528 20 6d 69 6e 28 61 29 20 46 52 4f 4d 20 74 61 62   min(a) FROM tab
1e529 6c 65 3b 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54  le;.**    SELECT
1e52a 20 6d 61 78 28 61 29 20 46 52 4f 4d 20 74 61 62   max(a) FROM tab
1e52b 6c 65 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 71 75  le;.**.** The qu
1e52c 65 72 79 20 6d 61 79 20 68 61 76 65 20 6f 6e 6c  ery may have onl
1e52d 79 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  y a single table
1e52e 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 61 72 67   in its FROM arg
1e52f 75 6d 65 6e 74 2e 20 20 54 68 65 72 65 0a 2a 2a  ument.  There.**
1e530 20 63 61 6e 20 62 65 20 6e 6f 20 47 52 4f 55 50   can be no GROUP
1e531 20 42 59 20 6f 72 20 48 41 56 49 4e 47 20 6f 72   BY or HAVING or
1e532 20 57 48 45 52 45 20 63 6c 61 75 73 65 73 2e 20   WHERE clauses. 
1e533 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   The result set 
1e534 6d 75 73 74 0a 2a 2a 20 62 65 20 74 68 65 20 6d  must.** be the m
1e535 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 20 6f 66  in() or max() of
1e536 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   a single column
1e537 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
1e538 54 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e  The column.** in
1e539 20 74 68 65 20 6d 69 6e 28 29 20 6f 72 20 6d 61   the min() or ma
1e53a 78 28 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  x() function mus
1e53b 74 20 62 65 20 69 6e 64 65 78 65 64 2e 0a 2a 2a  t be indexed..**
1e53c 0a 2a 2a 20 54 68 65 20 70 61 72 61 6d 65 74 65  .** The paramete
1e53d 72 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  rs to this routi
1e53e 6e 65 20 61 72 65 20 74 68 65 20 73 61 6d 65 20  ne are the same 
1e53f 61 73 20 66 6f 72 20 73 71 6c 69 74 65 33 53 65  as for sqlite3Se
1e540 6c 65 63 74 28 29 2e 0a 2a 2a 20 53 65 65 20 74  lect()..** See t
1e541 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
1e542 74 20 6f 6e 20 74 68 61 74 20 72 6f 75 74 69 6e  t on that routin
1e543 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
1e544 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
1e545 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70  .static int simp
1e546 6c 65 4d 69 6e 4d 61 78 51 75 65 72 79 28 50 61  leMinMaxQuery(Pa
1e547 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c  rse *pParse, Sel
1e548 65 63 74 20 2a 70 2c 20 69 6e 74 20 65 44 65 73  ect *p, int eDes
1e549 74 2c 20 69 6e 74 20 69 50 61 72 6d 29 7b 0a 20  t, int iParm){. 
1e54a 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20   Expr *pExpr;.  
1e54b 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 54 61 62 6c  int iCol;.  Tabl
1e54c 65 20 2a 70 54 61 62 3b 0a 20 20 49 6e 64 65 78  e *pTab;.  Index
1e54d 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 62 61   *pIdx;.  int ba
1e54e 73 65 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  se;.  Vdbe *v;. 
1e54f 20 69 6e 74 20 73 65 65 6b 4f 70 3b 0a 20 20 45   int seekOp;.  E
1e550 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
1e551 20 2a 70 4c 69 73 74 2c 20 65 4c 69 73 74 3b 0a   *pList, eList;.
1e552 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1e553 74 5f 69 74 65 6d 20 65 4c 69 73 74 49 74 65 6d  t_item eListItem
1e554 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  ;.  SrcList *pSr
1e555 63 3b 0a 20 20 69 6e 74 20 62 72 6b 3b 0a 20 20  c;.  int brk;.  
1e556 69 6e 74 20 69 44 62 3b 0a 0a 20 20 2f 2a 20 43  int iDb;..  /* C
1e557 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1e558 68 69 73 20 71 75 65 72 79 20 69 73 20 61 20 73  his query is a s
1e559 69 6d 70 6c 65 20 6d 69 6e 28 29 20 6f 72 20 6d  imple min() or m
1e55a 61 78 28 29 20 71 75 65 72 79 2e 20 20 52 65 74  ax() query.  Ret
1e55b 75 72 6e 0a 20 20 2a 2a 20 7a 65 72 6f 20 69 66  urn.  ** zero if
1e55c 20 69 74 20 69 73 20 20 6e 6f 74 2e 0a 20 20 2a   it is  not..  *
1e55d 2f 0a 20 20 69 66 28 20 70 2d 3e 70 47 72 6f 75  /.  if( p->pGrou
1e55e 70 42 79 20 7c 7c 20 70 2d 3e 70 48 61 76 69 6e  pBy || p->pHavin
1e55f 67 20 7c 7c 20 70 2d 3e 70 57 68 65 72 65 20 29  g || p->pWhere )
1e560 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 53 72   return 0;.  pSr
1e561 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69  c = p->pSrc;.  i
1e562 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31  f( pSrc->nSrc!=1
1e563 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
1e564 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
1e565 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  t;.  if( pEList-
1e566 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75  >nExpr!=1 ) retu
1e567 72 6e 20 30 3b 0a 20 20 70 45 78 70 72 20 3d 20  rn 0;.  pExpr = 
1e568 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
1e569 70 72 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  pr;.  if( pExpr-
1e56a 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43  >op!=TK_AGG_FUNC
1e56b 54 49 4f 4e 20 29 20 72 65 74 75 72 6e 20 30 3b  TION ) return 0;
1e56c 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72  .  pList = pExpr
1e56d 2d 3e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  ->pList;.  if( p
1e56e 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 4c 69 73 74  List==0 || pList
1e56f 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74  ->nExpr!=1 ) ret
1e570 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 78  urn 0;.  if( pEx
1e571 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 33 20 29  pr->token.n!=3 )
1e572 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
1e573 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
1e574 28 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74  ((char*)pExpr->t
1e575 6f 6b 65 6e 2e 7a 2c 22 6d 69 6e 22 2c 33 29 3d  oken.z,"min",3)=
1e576 3d 30 20 29 7b 0a 20 20 20 20 73 65 65 6b 4f 70  =0 ){.    seekOp
1e577 20 3d 20 4f 50 5f 52 65 77 69 6e 64 3b 0a 20 20   = OP_Rewind;.  
1e578 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
1e579 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a  3StrNICmp((char*
1e57a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c  )pExpr->token.z,
1e57b 22 6d 61 78 22 2c 33 29 3d 3d 30 20 29 7b 0a 20  "max",3)==0 ){. 
1e57c 20 20 20 73 65 65 6b 4f 70 20 3d 20 4f 50 5f 4c     seekOp = OP_L
1e57d 61 73 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ast;.  }else{.  
1e57e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1e57f 20 20 70 45 78 70 72 20 3d 20 70 4c 69 73 74 2d    pExpr = pList-
1e580 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69  >a[0].pExpr;.  i
1e581 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  f( pExpr->op!=TK
1e582 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e  _COLUMN ) return
1e583 20 30 3b 0a 20 20 69 43 6f 6c 20 3d 20 70 45 78   0;.  iCol = pEx
1e584 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 70  pr->iColumn;.  p
1e585 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d  Tab = pSrc->a[0]
1e586 2e 70 54 61 62 3b 0a 0a 20 20 2f 2a 20 54 68 69  .pTab;..  /* Thi
1e587 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63  s optimization c
1e588 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69  annot be used wi
1e589 74 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  th virtual table
1e58a 73 2e 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69  s. */.  if( IsVi
1e58b 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65  rtual(pTab) ) re
1e58c 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 49 66  turn 0;..  /* If
1e58d 20 77 65 20 67 65 74 20 74 6f 20 68 65 72 65 2c   we get to here,
1e58e 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 71 75   it means the qu
1e58f 65 72 79 20 69 73 20 6f 66 20 74 68 65 20 63 6f  ery is of the co
1e590 72 72 65 63 74 20 66 6f 72 6d 2e 0a 20 20 2a 2a  rrect form..  **
1e591 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73   Check to make s
1e592 75 72 65 20 77 65 20 68 61 76 65 20 61 6e 20 69  ure we have an i
1e593 6e 64 65 78 20 61 6e 64 20 6d 61 6b 65 20 70 49  ndex and make pI
1e594 64 78 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a  dx point to the.
1e595 20 20 2a 2a 20 61 70 70 72 6f 70 72 69 61 74 65    ** appropriate
1e596 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 20   index.  If the 
1e597 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 20 69  min() or max() i
1e598 73 20 6f 6e 20 61 6e 20 49 4e 54 45 47 45 52 20  s on an INTEGER 
1e599 50 52 49 4d 41 52 59 0a 20 20 2a 2a 20 6b 65 79  PRIMARY.  ** key
1e59a 20 63 6f 6c 75 6d 6e 2c 20 6e 6f 20 69 6e 64 65   column, no inde
1e59b 78 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 73  x is necessary s
1e59c 6f 20 73 65 74 20 70 49 64 78 20 74 6f 20 4e 55  o set pIdx to NU
1e59d 4c 4c 2e 20 20 49 66 20 6e 6f 0a 20 20 2a 2a 20  LL.  If no.  ** 
1e59e 75 73 61 62 6c 65 20 69 6e 64 65 78 20 69 73 20  usable index is 
1e59f 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 30 2e  found, return 0.
1e5a0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 43 6f 6c  .  */.  if( iCol
1e5a1 3c 30 20 29 7b 0a 20 20 20 20 70 49 64 78 20 3d  <0 ){.    pIdx =
1e5a2 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
1e5a3 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
1e5a4 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
1e5a5 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78  lSeq(pParse, pEx
1e5a6 70 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f  pr);.    if( pCo
1e5a7 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ll==0 ) return 0
1e5a8 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70  ;.    for(pIdx=p
1e5a9 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
1e5aa 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
1e5ab 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ext){.      asse
1e5ac 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  rt( pIdx->nColum
1e5ad 6e 3e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66  n>=1 );.      if
1e5ae 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
1e5af 5b 30 5d 3d 3d 69 43 6f 6c 20 26 26 20 0a 20 20  [0]==iCol && .  
1e5b0 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c 69 74          0==sqlit
1e5b1 65 33 53 74 72 49 43 6d 70 28 70 49 64 78 2d 3e  e3StrICmp(pIdx->
1e5b2 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 70 43 6f 6c 6c  azColl[0], pColl
1e5b3 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ->zName) ){.    
1e5b4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1e5b5 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
1e5b6 20 70 49 64 78 3d 3d 30 20 29 20 72 65 74 75 72   pIdx==0 ) retur
1e5b7 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  n 0;.  }..  /* I
1e5b8 64 65 6e 74 69 66 79 20 63 6f 6c 75 6d 6e 20 74  dentify column t
1e5b9 79 70 65 73 20 69 66 20 77 65 20 77 69 6c 6c 20  ypes if we will 
1e5ba 62 65 20 75 73 69 6e 67 20 74 68 65 20 63 61 6c  be using the cal
1e5bb 6c 62 61 63 6b 2e 20 20 54 68 69 73 0a 20 20 2a  lback.  This.  *
1e5bc 2a 20 73 74 65 70 20 69 73 20 73 6b 69 70 70 65  * step is skippe
1e5bd 64 20 69 66 20 74 68 65 20 6f 75 74 70 75 74 20  d if the output 
1e5be 69 73 20 67 6f 69 6e 67 20 74 6f 20 61 20 74 61  is going to a ta
1e5bf 62 6c 65 20 6f 72 20 61 20 6d 65 6d 6f 72 79 20  ble or a memory 
1e5c0 63 65 6c 6c 2e 0a 20 20 2a 2a 20 54 68 65 20 63  cell..  ** The c
1e5c1 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 68 61 76 65  olumn names have
1e5c2 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 67 65   already been ge
1e5c3 6e 65 72 61 74 65 64 20 69 6e 20 74 68 65 20 63  nerated in the c
1e5c4 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e  alling function.
1e5c5 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69  .  */.  v = sqli
1e5c6 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
1e5c7 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e);.  if( v==0 )
1e5c8 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a   return 0;..  /*
1e5c9 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 69   If the output i
1e5ca 73 20 64 65 73 74 69 6e 65 64 20 66 6f 72 20 61  s destined for a
1e5cb 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1e5cc 2c 20 6f 70 65 6e 20 74 68 61 74 20 74 61 62 6c  , open that tabl
1e5cd 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 44  e..  */.  if( eD
1e5ce 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61  est==SRT_EphemTa
1e5cf 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
1e5d0 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1e5d1 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 69  OpenEphemeral, i
1e5d2 50 61 72 6d 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20  Parm, 1);.  }.. 
1e5d3 20 2f 2a 20 47 65 6e 65 72 61 74 69 6e 67 20 63   /* Generating c
1e5d4 6f 64 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20  ode to find the 
1e5d5 6d 69 6e 20 6f 72 20 74 68 65 20 6d 61 78 2e 20  min or the max. 
1e5d6 20 42 61 73 69 63 61 6c 6c 79 20 61 6c 6c 20 77   Basically all w
1e5d7 65 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64  e have.  ** to d
1e5d8 6f 20 69 73 20 66 69 6e 64 20 74 68 65 20 66 69  o is find the fi
1e5d9 72 73 74 20 6f 72 20 74 68 65 20 6c 61 73 74 20  rst or the last 
1e5da 65 6e 74 72 79 20 69 6e 20 74 68 65 20 63 68 6f  entry in the cho
1e5db 73 65 6e 20 69 6e 64 65 78 2e 20 20 49 66 0a 20  sen index.  If. 
1e5dc 20 2a 2a 20 74 68 65 20 6d 69 6e 28 29 20 6f 72   ** the min() or
1e5dd 20 6d 61 78 28 29 20 69 73 20 6f 6e 20 74 68 65   max() is on the
1e5de 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
1e5df 20 4b 45 59 2c 20 74 68 65 6e 20 66 69 6e 64 20   KEY, then find 
1e5e0 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 6f  the first.  ** o
1e5e1 72 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20  r last entry in 
1e5e2 74 68 65 20 6d 61 69 6e 20 74 61 62 6c 65 2e 0a  the main table..
1e5e3 20 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71 6c    */.  iDb = sql
1e5e4 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
1e5e5 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
1e5e6 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
1e5e7 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 7c  assert( iDb>=0 |
1e5e8 7c 20 70 54 61 62 2d 3e 69 73 45 70 68 65 6d 20  | pTab->isEphem 
1e5e9 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65  );.  sqlite3Code
1e5ea 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61  VerifySchema(pPa
1e5eb 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c  rse, iDb);.  sql
1e5ec 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50  ite3TableLock(pP
1e5ed 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d  arse, iDb, pTab-
1e5ee 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e  >tnum, 0, pTab->
1e5ef 7a 4e 61 6d 65 29 3b 0a 20 20 62 61 73 65 20 3d  zName);.  base =
1e5f0 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 69 43 75 72   pSrc->a[0].iCur
1e5f1 73 6f 72 3b 0a 20 20 62 72 6b 20 3d 20 73 71 6c  sor;.  brk = sql
1e5f2 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
1e5f3 6c 28 76 29 3b 0a 20 20 63 6f 6d 70 75 74 65 4c  l(v);.  computeL
1e5f4 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50  imitRegisters(pP
1e5f5 61 72 73 65 2c 20 70 2c 20 62 72 6b 29 3b 0a 20  arse, p, brk);. 
1e5f6 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e   if( pSrc->a[0].
1e5f7 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20  pSelect==0 ){.  
1e5f8 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
1e5f9 6c 65 28 70 50 61 72 73 65 2c 20 62 61 73 65 2c  le(pParse, base,
1e5fa 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f   iDb, pTab, OP_O
1e5fb 70 65 6e 52 65 61 64 29 3b 0a 20 20 7d 0a 20 20  penRead);.  }.  
1e5fc 69 66 28 20 70 49 64 78 3d 3d 30 20 29 7b 0a 20  if( pIdx==0 ){. 
1e5fd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e5fe 64 4f 70 28 76 2c 20 73 65 65 6b 4f 70 2c 20 62  dOp(v, seekOp, b
1e5ff 61 73 65 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  ase, 0);.  }else
1e600 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68  {.    /* Even th
1e601 6f 75 67 68 20 74 68 65 20 63 75 72 73 6f 72 20  ough the cursor 
1e602 75 73 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65  used to open the
1e603 20 69 6e 64 65 78 20 68 65 72 65 20 69 73 20 63   index here is c
1e604 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 61 73 20  losed.    ** as 
1e605 73 6f 6f 6e 20 61 73 20 61 20 73 69 6e 67 6c 65  soon as a single
1e606 20 76 61 6c 75 65 20 68 61 73 20 62 65 65 6e 20   value has been 
1e607 72 65 61 64 20 66 72 6f 6d 20 69 74 2c 20 61 6c  read from it, al
1e608 6c 6f 63 61 74 65 20 69 74 0a 20 20 20 20 2a 2a  locate it.    **
1e609 20 75 73 69 6e 67 20 28 70 50 61 72 73 65 2d 3e   using (pParse->
1e60a 6e 54 61 62 2b 2b 29 20 74 6f 20 70 72 65 76 65  nTab++) to preve
1e60b 6e 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 64  nt the cursor id
1e60c 20 66 72 6f 6d 20 62 65 69 6e 67 20 0a 20 20 20   from being .   
1e60d 20 2a 2a 20 72 65 75 73 65 64 2e 20 54 68 69 73   ** reused. This
1e60e 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f   is important fo
1e60f 72 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 66 20  r statements of 
1e610 74 68 65 20 66 6f 72 6d 20 0a 20 20 20 20 2a 2a  the form .    **
1e611 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 20   "INSERT INTO x 
1e612 53 45 4c 45 43 54 20 6d 61 78 28 29 20 46 52 4f  SELECT max() FRO
1e613 4d 20 78 22 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  M x"..    */.   
1e614 20 69 6e 74 20 69 49 64 78 3b 0a 20 20 20 20 4b   int iIdx;.    K
1e615 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73  eyInfo *pKey = s
1e616 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
1e617 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29  fo(pParse, pIdx)
1e618 3b 0a 20 20 20 20 69 49 64 78 20 3d 20 70 50 61  ;.    iIdx = pPa
1e619 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
1e61a 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70   assert( pIdx->p
1e61b 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
1e61c 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c  chema );.    sql
1e61d 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1e61e 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 44 62   OP_Integer, iDb
1e61f 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1e620 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 4f  3VdbeOp3(v, OP_O
1e621 70 65 6e 52 65 61 64 2c 20 69 49 64 78 2c 20 70  penRead, iIdx, p
1e622 49 64 78 2d 3e 74 6e 75 6d 2c 20 0a 20 20 20 20  Idx->tnum, .    
1e623 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c      (char*)pKey,
1e624 20 50 33 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P3_KEYINFO_HAND
1e625 4f 46 46 29 3b 0a 20 20 20 20 69 66 28 20 73 65  OFF);.    if( se
1e626 65 6b 4f 70 3d 3d 4f 50 5f 52 65 77 69 6e 64 20  ekOp==OP_Rewind 
1e627 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e628 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1e629 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Null, 0, 0);.   
1e62a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e62b 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  dOp(v, OP_MakeRe
1e62c 63 6f 72 64 2c 20 31 2c 20 30 29 3b 0a 20 20 20  cord, 1, 0);.   
1e62d 20 20 20 73 65 65 6b 4f 70 20 3d 20 4f 50 5f 4d     seekOp = OP_M
1e62e 6f 76 65 47 74 3b 0a 20 20 20 20 7d 0a 20 20 20  oveGt;.    }.   
1e62f 20 69 66 28 20 70 49 64 78 2d 3e 61 53 6f 72 74   if( pIdx->aSort
1e630 4f 72 64 65 72 5b 30 5d 3d 3d 53 51 4c 49 54 45  Order[0]==SQLITE
1e631 5f 53 4f 5f 44 45 53 43 20 29 7b 0a 20 20 20 20  _SO_DESC ){.    
1e632 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 32 35 31    /* Ticket #251
1e633 34 3a 20 69 6e 76 65 72 74 20 74 68 65 20 73 65  4: invert the se
1e634 65 6b 20 6f 70 65 72 61 74 6f 72 20 69 66 20 77  ek operator if w
1e635 65 20 61 72 65 20 75 73 69 6e 67 0a 20 20 20 20  e are using.    
1e636 20 20 2a 2a 20 61 20 64 65 73 63 65 6e 64 69 6e    ** a descendin
1e637 67 20 69 6e 64 65 78 2e 20 2a 2f 0a 20 20 20 20  g index. */.    
1e638 20 20 69 66 28 20 73 65 65 6b 4f 70 3d 3d 4f 50    if( seekOp==OP
1e639 5f 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20  _Last ){.       
1e63a 20 73 65 65 6b 4f 70 20 3d 20 4f 50 5f 52 65 77   seekOp = OP_Rew
1e63b 69 6e 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ind;.      }else
1e63c 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1e63d 28 20 73 65 65 6b 4f 70 3d 3d 4f 50 5f 4d 6f 76  ( seekOp==OP_Mov
1e63e 65 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20 73  eGt );.        s
1e63f 65 65 6b 4f 70 20 3d 20 4f 50 5f 4d 6f 76 65 4c  eekOp = OP_MoveL
1e640 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
1e641 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1e642 41 64 64 4f 70 28 76 2c 20 73 65 65 6b 4f 70 2c  AddOp(v, seekOp,
1e643 20 69 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 73   iIdx, 0);.    s
1e644 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1e645 76 2c 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c 20  v, OP_IdxRowid, 
1e646 69 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 73 71  iIdx, 0);.    sq
1e647 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1e648 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78  , OP_Close, iIdx
1e649 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1e64a 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1e64b 5f 4d 6f 76 65 47 65 2c 20 62 61 73 65 2c 20 30  _MoveGe, base, 0
1e64c 29 3b 0a 20 20 7d 0a 20 20 65 4c 69 73 74 2e 6e  );.  }.  eList.n
1e64d 45 78 70 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 73  Expr = 1;.  mems
1e64e 65 74 28 26 65 4c 69 73 74 49 74 65 6d 2c 20 30  et(&eListItem, 0
1e64f 2c 20 73 69 7a 65 6f 66 28 65 4c 69 73 74 49 74  , sizeof(eListIt
1e650 65 6d 29 29 3b 0a 20 20 65 4c 69 73 74 2e 61 20  em));.  eList.a 
1e651 3d 20 26 65 4c 69 73 74 49 74 65 6d 3b 0a 20 20  = &eListItem;.  
1e652 65 4c 69 73 74 2e 61 5b 30 5d 2e 70 45 78 70 72  eList.a[0].pExpr
1e653 20 3d 20 70 45 78 70 72 3b 0a 20 20 73 65 6c 65   = pExpr;.  sele
1e654 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72  ctInnerLoop(pPar
1e655 73 65 2c 20 70 2c 20 26 65 4c 69 73 74 2c 20 30  se, p, &eList, 0
1e656 2c 20 30 2c 20 30 2c 20 2d 31 2c 20 65 44 65 73  , 0, 0, -1, eDes
1e657 74 2c 20 69 50 61 72 6d 2c 20 62 72 6b 2c 20 62  t, iParm, brk, b
1e658 72 6b 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  rk, 0);.  sqlite
1e659 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1e65a 6c 28 76 2c 20 62 72 6b 29 3b 0a 20 20 73 71 6c  l(v, brk);.  sql
1e65b 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1e65c 20 4f 50 5f 43 6c 6f 73 65 2c 20 62 61 73 65 2c   OP_Close, base,
1e65d 20 30 29 3b 0a 20 20 0a 20 20 72 65 74 75 72 6e   0);.  .  return
1e65e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61   1;.}../*.** Ana
1e65f 6c 79 7a 65 20 61 6e 64 20 4f 52 44 45 52 20 42  lyze and ORDER B
1e660 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c  Y or GROUP BY cl
1e661 61 75 73 65 20 69 6e 20 61 20 53 45 4c 45 43 54  ause in a SELECT
1e662 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74   statement.  Ret
1e663 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  urn.** the numbe
1e664 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e  r of errors seen
1e665 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 4f 52 44 45 52  ..**.** An ORDER
1e666 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
1e667 69 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70  is a list of exp
1e668 72 65 73 73 69 6f 6e 73 2e 20 20 49 66 20 61 6e  ressions.  If an
1e669 79 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  y expression.** 
1e66a 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  is an integer co
1e66b 6e 73 74 61 6e 74 2c 20 74 68 65 6e 20 74 68 61  nstant, then tha
1e66c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  t expression is 
1e66d 72 65 70 6c 61 63 65 64 20 62 79 20 74 68 65 0a  replaced by the.
1e66e 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
1e66f 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65   entry in the re
1e670 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74 61  sult set..*/.sta
1e671 74 69 63 20 69 6e 74 20 70 72 6f 63 65 73 73 4f  tic int processO
1e672 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e  rderGroupBy(.  N
1e673 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
1e674 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e       /* Name con
1e675 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45  text of the SELE
1e676 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  CT statement. */
1e677 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
1e678 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20  derBy,   /* The 
1e679 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
1e67a 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62  P BY clause to b
1e67b 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20  e processed */. 
1e67c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
1e67d 70 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72  pe     /* Either
1e67e 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f   "ORDER" or "GRO
1e67f 55 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 69  UP", as appropri
1e680 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ate */.){.  int 
1e681 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  i;.  ExprList *p
1e682 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c  EList = pNC->pEL
1e683 69 73 74 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  ist;     /* The 
1e684 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
1e685 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 50 61  e SELECT */.  Pa
1e686 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 4e  rse *pParse = pN
1e687 43 2d 3e 70 50 61 72 73 65 3b 20 20 20 20 20 2f  C->pParse;     /
1e688 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74  * The result set
1e689 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a   of the SELECT *
1e68a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69  /.  assert( pELi
1e68b 73 74 20 29 3b 0a 0a 20 20 69 66 28 20 70 4f 72  st );..  if( pOr
1e68c 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75 72  derBy==0 ) retur
1e68d 6e 20 30 3b 0a 20 20 69 66 28 20 70 4f 72 64 65  n 0;.  if( pOrde
1e68e 72 42 79 2d 3e 6e 45 78 70 72 3e 53 51 4c 49 54  rBy->nExpr>SQLIT
1e68f 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  E_MAX_COLUMN ){.
1e690 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1e691 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
1e692 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 25   many terms in %
1e693 73 20 42 59 20 63 6c 61 75 73 65 22 2c 20 7a 54  s BY clause", zT
1e694 79 70 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ype);.    return
1e695 20 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d   1;.  }.  for(i=
1e696 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  0; i<pOrderBy->n
1e697 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
1e698 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 45 78  int iCol;.    Ex
1e699 70 72 20 2a 70 45 20 3d 20 70 4f 72 64 65 72 42  pr *pE = pOrderB
1e69a 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  y->a[i].pExpr;. 
1e69b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
1e69c 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20  prIsInteger(pE, 
1e69d 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  &iCol) ){.      
1e69e 69 66 28 20 69 43 6f 6c 3e 30 20 26 26 20 69 43  if( iCol>0 && iC
1e69f 6f 6c 3c 3d 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ol<=pEList->nExp
1e6a0 72 20 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c  r ){.        Col
1e6a1 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 45  lSeq *pColl = pE
1e6a2 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20  ->pColl;.       
1e6a3 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 45 2d   int flags = pE-
1e6a4 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43  >flags & EP_ExpC
1e6a5 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20  ollate;.        
1e6a6 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1e6a7 65 28 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70  e(pE);.        p
1e6a8 45 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  E = sqlite3ExprD
1e6a9 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  up(pParse->db, p
1e6aa 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 2d 31 5d  EList->a[iCol-1]
1e6ab 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20  .pExpr);.       
1e6ac 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e   pOrderBy->a[i].
1e6ad 70 45 78 70 72 20 3d 20 70 45 3b 0a 20 20 20 20  pExpr = pE;.    
1e6ae 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 26 26      if( pColl &&
1e6af 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20   flags ){.      
1e6b0 20 20 20 20 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20      pE->pColl = 
1e6b1 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 20  pColl;.         
1e6b2 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d 20 66 6c   pE->flags |= fl
1e6b3 61 67 73 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ags;.        }. 
1e6b4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1e6b5 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1e6b6 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
1e6b7 20 20 20 20 20 20 20 20 22 25 73 20 42 59 20 63          "%s BY c
1e6b8 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 25 64 20  olumn number %d 
1e6b9 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73  out of range - s
1e6ba 68 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 20 20  hould be ".     
1e6bb 20 20 20 20 20 20 22 62 65 74 77 65 65 6e 20 31        "between 1
1e6bc 20 61 6e 64 20 25 64 22 2c 20 7a 54 79 70 65 2c   and %d", zType,
1e6bd 20 69 43 6f 6c 2c 20 70 45 4c 69 73 74 2d 3e 6e   iCol, pEList->n
1e6be 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72  Expr);.        r
1e6bf 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
1e6c0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
1e6c1 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76  qlite3ExprResolv
1e6c2 65 4e 61 6d 65 73 28 70 4e 43 2c 20 70 45 29 20  eNames(pNC, pE) 
1e6c3 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1e6c4 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
1e6c5 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1e6c6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
1e6c7 65 73 6f 6c 76 65 73 20 61 6e 79 20 6e 61 6d 65  esolves any name
1e6c8 73 20 75 73 65 64 20 69 6e 20 74 68 65 20 72 65  s used in the re
1e6c9 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a  sult set of the.
1e6ca 2a 2a 20 73 75 70 70 6c 69 65 64 20 53 45 4c 45  ** supplied SELE
1e6cb 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 49 66  CT statement. If
1e6cc 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
1e6cd 65 6d 65 6e 74 20 62 65 69 6e 67 20 72 65 73 6f  ement being reso
1e6ce 6c 76 65 64 0a 2a 2a 20 69 73 20 61 20 73 75 62  lved.** is a sub
1e6cf 2d 73 65 6c 65 63 74 2c 20 74 68 65 6e 20 70 4f  -select, then pO
1e6d0 75 74 65 72 4e 43 20 69 73 20 61 20 70 6f 69 6e  uterNC is a poin
1e6d1 74 65 72 20 74 6f 20 74 68 65 20 4e 61 6d 65 43  ter to the NameC
1e6d2 6f 6e 74 65 78 74 20 0a 2a 2a 20 6f 66 20 74 68  ontext .** of th
1e6d3 65 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54 2e  e parent SELECT.
1e6d4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1e6d5 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65  TE int sqlite3Se
1e6d6 6c 65 63 74 52 65 73 6f 6c 76 65 28 0a 20 20 50  lectResolve(.  P
1e6d7 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1e6d8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
1e6d9 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
1e6da 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
1e6db 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
1e6dc 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1e6dd 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a  being coded. */.
1e6de 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1e6df 4f 75 74 65 72 4e 43 20 20 2f 2a 20 54 68 65 20  OuterNC  /* The 
1e6e0 6f 75 74 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65  outer name conte
1e6e1 78 74 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 2e  xt. May be NULL.
1e6e2 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73   */.){.  ExprLis
1e6e3 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20  t *pEList;      
1e6e4 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 73 65      /* Result se
1e6e5 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  t. */.  int i;  
1e6e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e6e7 20 20 20 2f 2a 20 46 6f 72 2d 6c 6f 6f 70 20 76     /* For-loop v
1e6e8 61 72 69 61 62 6c 65 20 75 73 65 64 20 69 6e 20  ariable used in 
1e6e9 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 73 20  multiple places 
1e6ea 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  */.  NameContext
1e6eb 20 73 4e 43 3b 20 20 20 20 20 20 20 20 20 20 20   sNC;           
1e6ec 2f 2a 20 4c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f  /* Local name-co
1e6ed 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
1e6ee 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20  ist *pGroupBy;  
1e6ef 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67 72 6f        /* The gro
1e6f0 75 70 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a  up by clause */.
1e6f1 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 72 6f  .  /* If this ro
1e6f2 75 74 69 6e 65 20 68 61 73 20 72 75 6e 20 62 65  utine has run be
1e6f3 66 6f 72 65 2c 20 72 65 74 75 72 6e 20 69 6d 6d  fore, return imm
1e6f4 65 64 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 69  ediately. */.  i
1e6f5 66 28 20 70 2d 3e 69 73 52 65 73 6f 6c 76 65 64  f( p->isResolved
1e6f6 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1e6f7 21 70 4f 75 74 65 72 4e 43 20 29 3b 0a 20 20 20  !pOuterNC );.   
1e6f8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1e6f9 4b 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 73 52 65  K;.  }.  p->isRe
1e6fa 73 6f 6c 76 65 64 20 3d 20 31 3b 0a 0a 20 20 2f  solved = 1;..  /
1e6fb 2a 20 49 66 20 74 68 65 72 65 20 68 61 76 65 20  * If there have 
1e6fc 61 6c 72 65 61 64 79 20 62 65 65 6e 20 65 72 72  already been err
1e6fd 6f 72 73 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  ors, do nothing.
1e6fe 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65   */.  if( pParse
1e6ff 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 20 20 20  ->nErr>0 ){.    
1e700 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1e701 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50  ROR;.  }..  /* P
1e702 72 65 70 61 72 65 20 74 68 65 20 73 65 6c 65 63  repare the selec
1e703 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69  t statement. Thi
1e704 73 20 63 61 6c 6c 20 77 69 6c 6c 20 61 6c 6c 6f  s call will allo
1e705 63 61 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73  cate all cursors
1e706 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 74  .  ** required t
1e707 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 74 61 62  o handle the tab
1e708 6c 65 73 20 61 6e 64 20 73 75 62 71 75 65 72 69  les and subqueri
1e709 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
1e70a 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  lause..  */.  if
1e70b 28 20 70 72 65 70 53 65 6c 65 63 74 53 74 6d 74  ( prepSelectStmt
1e70c 28 70 50 61 72 73 65 2c 20 70 29 20 29 7b 0a 20  (pParse, p) ){. 
1e70d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1e70e 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f  _ERROR;.  }..  /
1e70f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78  * Resolve the ex
1e710 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
1e711 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
1e712 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65 73 65  T clauses. These
1e713 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c  .  ** are not al
1e714 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74  lowed to refer t
1e715 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20  o any names, so 
1e716 70 61 73 73 20 61 6e 20 65 6d 70 74 79 20 4e 61  pass an empty Na
1e717 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a  meContext..  */.
1e718 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
1e719 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
1e71a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
1e71b 50 61 72 73 65 3b 0a 20 20 69 66 28 20 73 71 6c  Parse;.  if( sql
1e71c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e  ite3ExprResolveN
1e71d 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4c  ames(&sNC, p->pL
1e71e 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20 20 73  imit) ||.      s
1e71f 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76  qlite3ExprResolv
1e720 65 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e  eNames(&sNC, p->
1e721 70 4f 66 66 73 65 74 29 20 29 7b 0a 20 20 20 20  pOffset) ){.    
1e722 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1e723 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  ROR;.  }..  /* S
1e724 65 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c 20  et up the local 
1e725 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20  name-context to 
1e726 70 61 73 73 20 74 6f 20 45 78 70 72 52 65 73 6f  pass to ExprReso
1e727 6c 76 65 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20  lveNames() to.  
1e728 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68 65 20 65  ** resolve the e
1e729 78 70 72 65 73 73 69 6f 6e 2d 6c 69 73 74 2e 0a  xpression-list..
1e72a 20 20 2a 2f 0a 20 20 73 4e 43 2e 61 6c 6c 6f 77    */.  sNC.allow
1e72b 41 67 67 20 3d 20 31 3b 0a 20 20 73 4e 43 2e 70  Agg = 1;.  sNC.p
1e72c 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72  SrcList = p->pSr
1e72d 63 3b 0a 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d  c;.  sNC.pNext =
1e72e 20 70 4f 75 74 65 72 4e 43 3b 0a 0a 20 20 2f 2a   pOuterNC;..  /*
1e72f 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69   Resolve names i
1e730 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
1e731 2e 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20  . */.  pEList = 
1e732 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28  p->pEList;.  if(
1e733 20 21 70 45 4c 69 73 74 20 29 20 72 65 74 75 72   !pEList ) retur
1e734 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1e735 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
1e736 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
1e737 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d  {.    Expr *pX =
1e738 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45   pEList->a[i].pE
1e739 78 70 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  xpr;.    if( sql
1e73a 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e  ite3ExprResolveN
1e73b 61 6d 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29  ames(&sNC, pX) )
1e73c 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
1e73d 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1e73e 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
1e73f 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 67 67  there are no agg
1e740 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
1e741 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73   in the result-s
1e742 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50  et, and no GROUP
1e743 20 42 59 20 0a 20 20 2a 2a 20 65 78 70 72 65 73   BY .  ** expres
1e744 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c  sion, do not all
1e745 6f 77 20 61 67 67 72 65 67 61 74 65 73 20 69 6e  ow aggregates in
1e746 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65   any of the othe
1e747 72 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20  r expressions.. 
1e748 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70   */.  assert( !p
1e749 2d 3e 69 73 41 67 67 20 29 3b 0a 20 20 70 47 72  ->isAgg );.  pGr
1e74a 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
1e74b 70 42 79 3b 0a 20 20 69 66 28 20 70 47 72 6f 75  pBy;.  if( pGrou
1e74c 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 67  pBy || sNC.hasAg
1e74d 67 20 29 7b 0a 20 20 20 20 70 2d 3e 69 73 41 67  g ){.    p->isAg
1e74e 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  g = 1;.  }else{.
1e74f 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67      sNC.allowAgg
1e750 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
1e751 49 66 20 61 20 48 41 56 49 4e 47 20 63 6c 61 75  If a HAVING clau
1e752 73 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 74  se is present, t
1e753 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62  hen there must b
1e754 65 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61  e a GROUP BY cla
1e755 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  use..  */.  if( 
1e756 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 70  p->pHaving && !p
1e757 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 73  GroupBy ){.    s
1e758 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1e759 50 61 72 73 65 2c 20 22 61 20 47 52 4f 55 50 20  Parse, "a GROUP 
1e75a 42 59 20 63 6c 61 75 73 65 20 69 73 20 72 65 71  BY clause is req
1e75b 75 69 72 65 64 20 62 65 66 6f 72 65 20 48 41 56  uired before HAV
1e75c 49 4e 47 22 29 3b 0a 20 20 20 20 72 65 74 75 72  ING");.    retur
1e75d 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1e75e 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68    }..  /* Add th
1e75f 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
1e760 74 20 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f  t to the name-co
1e761 6e 74 65 78 74 20 62 65 66 6f 72 65 20 70 61 72  ntext before par
1e762 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 6f 74  sing the.  ** ot
1e763 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20  her expressions 
1e764 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  in the SELECT st
1e765 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  atement. This is
1e766 20 73 6f 20 74 68 61 74 0a 20 20 2a 2a 20 65 78   so that.  ** ex
1e767 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
1e768 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 28 65   WHERE clause (e
1e769 74 63 2e 29 20 63 61 6e 20 72 65 66 65 72 20 74  tc.) can refer t
1e76a 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 62 79  o expressions by
1e76b 0a 20 20 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e  .  ** aliases in
1e76c 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
1e76d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 69 6e 6f 72  .  **.  ** Minor
1e76e 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20   point: If this 
1e76f 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65  is the case, the
1e770 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
1e771 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 72 65   will be.  ** re
1e772 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72 20 65  -evaluated for e
1e773 61 63 68 20 72 65 66 65 72 65 6e 63 65 20 74 6f  ach reference to
1e774 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 73 4e 43 2e   it..  */.  sNC.
1e775 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69  pEList = p->pELi
1e776 73 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  st;.  if( sqlite
1e777 33 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65  3ExprResolveName
1e778 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72  s(&sNC, p->pWher
1e779 65 29 20 7c 7c 0a 20 20 20 20 20 73 71 6c 69 74  e) ||.     sqlit
1e77a 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d  e3ExprResolveNam
1e77b 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48 61 76  es(&sNC, p->pHav
1e77c 69 6e 67 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ing) ){.    retu
1e77d 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
1e77e 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 50  .  }.  if( p->pP
1e77f 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 69  rior==0 ){.    i
1e780 66 28 20 70 72 6f 63 65 73 73 4f 72 64 65 72 47  f( processOrderG
1e781 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2d 3e  roupBy(&sNC, p->
1e782 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52  pOrderBy, "ORDER
1e783 22 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 70 72  ") ||.        pr
1e784 6f 63 65 73 73 4f 72 64 65 72 47 72 6f 75 70 42  ocessOrderGroupB
1e785 79 28 26 73 4e 43 2c 20 70 47 72 6f 75 70 42 79  y(&sNC, pGroupBy
1e786 2c 20 22 47 52 4f 55 50 22 29 20 29 7b 0a 20 20  , "GROUP") ){.  
1e787 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1e788 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
1e789 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65   }..  if( pParse
1e78a 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1e78b 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
1e78c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1e78d 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
1e78e 72 65 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  re the GROUP BY 
1e78f 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20  clause does not 
1e790 63 6f 6e 74 61 69 6e 20 61 67 67 72 65 67 61 74  contain aggregat
1e791 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  e functions..  *
1e792 2f 0a 20 20 69 66 28 20 70 47 72 6f 75 70 42 79  /.  if( pGroupBy
1e793 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45   ){.    struct E
1e794 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
1e795 74 65 6d 3b 0a 20 20 0a 20 20 20 20 66 6f 72 28  tem;.  .    for(
1e796 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75  i=0, pItem=pGrou
1e797 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75 70  pBy->a; i<pGroup
1e798 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  By->nExpr; i++, 
1e799 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
1e79a 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
1e79b 72 74 79 28 70 49 74 65 6d 2d 3e 70 45 78 70 72  rty(pItem->pExpr
1e79c 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20  , EP_Agg) ){.   
1e79d 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1e79e 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 67  rMsg(pParse, "ag
1e79f 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
1e7a0 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65  s are not allowe
1e7a1 64 20 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20  d in ".         
1e7a2 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 59     "the GROUP BY
1e7a3 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 20   clause");.     
1e7a4 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1e7a5 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a  _ERROR;.      }.
1e7a6 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1e7a7 49 66 20 74 68 69 73 20 69 73 20 6f 6e 65 20 53  If this is one S
1e7a8 45 4c 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f  ELECT of a compo
1e7a9 75 6e 64 2c 20 62 65 20 73 75 72 65 20 74 6f 20  und, be sure to 
1e7aa 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 0a 20 20  resolve names.  
1e7ab 2a 2a 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20  ** in the other 
1e7ac 53 45 4c 45 43 54 73 2e 0a 20 20 2a 2f 0a 20 20  SELECTs..  */.  
1e7ad 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b  if( p->pPrior ){
1e7ae 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
1e7af 74 65 33 53 65 6c 65 63 74 52 65 73 6f 6c 76 65  te3SelectResolve
1e7b0 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 50 72 69  (pParse, p->pPri
1e7b1 6f 72 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20  or, pOuterNC);. 
1e7b2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
1e7b3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1e7b4 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74  }.}../*.** Reset
1e7b5 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 61   the aggregate a
1e7b6 63 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a  ccumulator..**.*
1e7b7 2a 20 54 68 65 20 61 67 67 72 65 67 61 74 65 20  * The aggregate 
1e7b8 61 63 63 75 6d 75 6c 61 74 6f 72 20 69 73 20 61  accumulator is a
1e7b9 20 73 65 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63   set of memory c
1e7ba 65 6c 6c 73 20 74 68 61 74 20 68 6f 6c 64 0a 2a  ells that hold.*
1e7bb 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72  * intermediate r
1e7bc 65 73 75 6c 74 73 20 77 68 69 6c 65 20 63 61 6c  esults while cal
1e7bd 63 75 6c 61 74 69 6e 67 20 61 6e 20 61 67 67 72  culating an aggr
1e7be 65 67 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a 20  egate.  This.** 
1e7bf 72 6f 75 74 69 6e 65 20 73 69 6d 70 6c 79 20 73  routine simply s
1e7c0 74 6f 72 65 73 20 4e 55 4c 4c 73 20 69 6e 20 61  tores NULLs in a
1e7c1 6c 6c 20 6f 66 20 74 68 6f 73 65 20 6d 65 6d 6f  ll of those memo
1e7c2 72 79 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61  ry cells..*/.sta
1e7c3 74 69 63 20 76 6f 69 64 20 72 65 73 65 74 41 63  tic void resetAc
1e7c4 63 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20  cumulator(Parse 
1e7c5 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f  *pParse, AggInfo
1e7c6 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56   *pAggInfo){.  V
1e7c7 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
1e7c8 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b  >pVdbe;.  int i;
1e7c9 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
1e7ca 6f 5f 66 75 6e 63 20 2a 70 46 75 6e 63 3b 0a 20  o_func *pFunc;. 
1e7cb 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 6e   if( pAggInfo->n
1e7cc 46 75 6e 63 2b 70 41 67 67 49 6e 66 6f 2d 3e 6e  Func+pAggInfo->n
1e7cd 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
1e7ce 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66   return;.  }.  f
1e7cf 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e  or(i=0; i<pAggIn
1e7d0 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  fo->nColumn; i++
1e7d1 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1e7d2 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1e7d3 6d 4e 75 6c 6c 2c 20 70 41 67 67 49 6e 66 6f 2d  mNull, pAggInfo-
1e7d4 3e 61 43 6f 6c 5b 69 5d 2e 69 4d 65 6d 2c 20 30  >aCol[i].iMem, 0
1e7d5 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 46 75  );.  }.  for(pFu
1e7d6 6e 63 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  nc=pAggInfo->aFu
1e7d7 6e 63 2c 20 69 3d 30 3b 20 69 3c 70 41 67 67 49  nc, i=0; i<pAggI
1e7d8 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c  nfo->nFunc; i++,
1e7d9 20 70 46 75 6e 63 2b 2b 29 7b 0a 20 20 20 20 73   pFunc++){.    s
1e7da 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1e7db 76 2c 20 4f 50 5f 4d 65 6d 4e 75 6c 6c 2c 20 70  v, OP_MemNull, p
1e7dc 46 75 6e 63 2d 3e 69 4d 65 6d 2c 20 30 29 3b 0a  Func->iMem, 0);.
1e7dd 20 20 20 20 69 66 28 20 70 46 75 6e 63 2d 3e 69      if( pFunc->i
1e7de 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20  Distinct>=0 ){. 
1e7df 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20       Expr *pE = 
1e7e0 70 46 75 6e 63 2d 3e 70 45 78 70 72 3b 0a 20 20  pFunc->pExpr;.  
1e7e1 20 20 20 20 69 66 28 20 70 45 2d 3e 70 4c 69 73      if( pE->pLis
1e7e2 74 3d 3d 30 20 7c 7c 20 70 45 2d 3e 70 4c 69 73  t==0 || pE->pLis
1e7e3 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20  t->nExpr!=1 ){. 
1e7e4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1e7e5 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1e7e6 44 49 53 54 49 4e 43 54 20 69 6e 20 61 67 67 72  DISTINCT in aggr
1e7e7 65 67 61 74 65 20 6d 75 73 74 20 62 65 20 66 6f  egate must be fo
1e7e8 6c 6c 6f 77 65 64 20 22 0a 20 20 20 20 20 20 20  llowed ".       
1e7e9 20 20 20 20 22 62 79 20 61 6e 20 65 78 70 72 65      "by an expre
1e7ea 73 73 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 20  ssion");.       
1e7eb 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63   pFunc->iDistinc
1e7ec 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 65  t = -1;.      }e
1e7ed 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 4b 65 79  lse{.        Key
1e7ee 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d  Info *pKeyInfo =
1e7ef 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72   keyInfoFromExpr
1e7f0 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 45 2d  List(pParse, pE-
1e7f1 3e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  >pList);.       
1e7f2 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1e7f3 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
1e7f4 72 61 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73  ral, pFunc->iDis
1e7f5 74 69 6e 63 74 2c 20 30 2c 20 0a 20 20 20 20 20  tinct, 0, .     
1e7f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e7f7 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
1e7f8 49 6e 66 6f 2c 20 50 33 5f 4b 45 59 49 4e 46 4f  Info, P3_KEYINFO
1e7f9 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20  _HANDOFF);.     
1e7fa 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
1e7fb 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
1e7fc 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65 20   OP_AggFinalize 
1e7fd 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65 72 79  opcode for every
1e7fe 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1e7ff 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 41 67  ion.** in the Ag
1e800 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  gInfo structure.
1e801 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1e802 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74  finalizeAggFunct
1e803 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50 61 72  ions(Parse *pPar
1e804 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
1e805 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
1e806 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
1e807 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
1e808 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
1e809 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69 3d 30  c *pF;.  for(i=0
1e80a 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61  , pF=pAggInfo->a
1e80b 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f  Func; i<pAggInfo
1e80c 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46  ->nFunc; i++, pF
1e80d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73  ++){.    ExprLis
1e80e 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70  t *pList = pF->p
1e80f 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20  Expr->pList;.   
1e810 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1e811 76 2c 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20  v, OP_AggFinal, 
1e812 70 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20  pF->iMem, pList 
1e813 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a  ? pList->nExpr :
1e814 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
1e815 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a            (void*
1e816 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 33 5f 46  )pF->pFunc, P3_F
1e817 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a 7d 0a 0a  UNCDEF);.  }.}..
1e818 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74 68 65  /*.** Update the
1e819 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d 65 6d   accumulator mem
1e81a 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 6e  ory cells for an
1e81b 20 61 67 67 72 65 67 61 74 65 20 62 61 73 65 64   aggregate based
1e81c 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72 65   on.** the curre
1e81d 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  nt cursor positi
1e81e 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
1e81f 69 64 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c  id updateAccumul
1e820 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61 72  ator(Parse *pPar
1e821 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
1e822 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
1e823 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
1e824 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
1e825 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
1e826 63 20 2a 70 46 3b 0a 20 20 73 74 72 75 63 74 20  c *pF;.  struct 
1e827 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 3b  AggInfo_col *pC;
1e828 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69  ..  pAggInfo->di
1e829 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20  rectMode = 1;.  
1e82a 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41 67 67  for(i=0, pF=pAgg
1e82b 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70  Info->aFunc; i<p
1e82c 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20  AggInfo->nFunc; 
1e82d 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 20  i++, pF++){.    
1e82e 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 69 6e  int nArg;.    in
1e82f 74 20 61 64 64 72 4e 65 78 74 20 3d 20 30 3b 0a  t addrNext = 0;.
1e830 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
1e831 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d  ist = pF->pExpr-
1e832 3e 70 4c 69 73 74 3b 0a 20 20 20 20 69 66 28 20  >pList;.    if( 
1e833 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 6e  pList ){.      n
1e834 41 72 67 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78  Arg = pList->nEx
1e835 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  pr;.      sqlite
1e836 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
1e837 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 29  t(pParse, pList)
1e838 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1e839 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20     nArg = 0;.   
1e83a 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e 69   }.    if( pF->i
1e83b 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20  Distinct>=0 ){. 
1e83c 20 20 20 20 20 61 64 64 72 4e 65 78 74 20 3d 20       addrNext = 
1e83d 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
1e83e 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 61  abel(v);.      a
1e83f 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31 20 29  ssert( nArg==1 )
1e840 3b 0a 20 20 20 20 20 20 63 6f 64 65 44 69 73 74  ;.      codeDist
1e841 69 6e 63 74 28 76 2c 20 70 46 2d 3e 69 44 69 73  inct(v, pF->iDis
1e842 74 69 6e 63 74 2c 20 61 64 64 72 4e 65 78 74 2c  tinct, addrNext,
1e843 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   1);.    }.    i
1e844 66 28 20 70 46 2d 3e 70 46 75 6e 63 2d 3e 6e 65  f( pF->pFunc->ne
1e845 65 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20  edCollSeq ){.   
1e846 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
1e847 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 72  l = 0;.      str
1e848 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1e849 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20  m *pItem;.      
1e84a 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 61 73 73  int j;.      ass
1e84b 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b  ert( pList!=0 );
1e84c 20 20 2f 2a 20 70 4c 69 73 74 21 3d 30 20 69 66    /* pList!=0 if
1e84d 20 70 46 2d 3e 70 46 75 6e 63 2d 3e 6e 65 65 64   pF->pFunc->need
1e84e 43 6f 6c 6c 53 65 71 20 69 73 20 74 72 75 65 20  CollSeq is true 
1e84f 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  */.      for(j=0
1e850 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
1e851 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a 3c 6e 41  ; !pColl && j<nA
1e852 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b  rg; j++, pItem++
1e853 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
1e854 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1e855 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 49  llSeq(pParse, pI
1e856 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  tem->pExpr);.   
1e857 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
1e858 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  pColl ){.       
1e859 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d   pColl = pParse-
1e85a 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a  >db->pDfltColl;.
1e85b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
1e85c 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1e85d 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30  OP_CollSeq, 0, 0
1e85e 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c  , (char *)pColl,
1e85f 20 50 33 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20   P3_COLLSEQ);.  
1e860 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
1e861 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 41 67 67  dbeOp3(v, OP_Agg
1e862 53 74 65 70 2c 20 70 46 2d 3e 69 4d 65 6d 2c 20  Step, pF->iMem, 
1e863 6e 41 72 67 2c 20 28 76 6f 69 64 2a 29 70 46 2d  nArg, (void*)pF-
1e864 3e 70 46 75 6e 63 2c 20 50 33 5f 46 55 4e 43 44  >pFunc, P3_FUNCD
1e865 45 46 29 3b 0a 20 20 20 20 69 66 28 20 61 64 64  EF);.    if( add
1e866 72 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 73  rNext ){.      s
1e867 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
1e868 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 4e 65  eLabel(v, addrNe
1e869 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  xt);.    }.  }. 
1e86a 20 66 6f 72 28 69 3d 30 2c 20 70 43 3d 70 41 67   for(i=0, pC=pAg
1e86b 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70  gInfo->aCol; i<p
1e86c 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75  AggInfo->nAccumu
1e86d 6c 61 74 6f 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b  lator; i++, pC++
1e86e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
1e86f 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
1e870 43 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 73  C->pExpr);.    s
1e871 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1e872 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 2c 20  v, OP_MemStore, 
1e873 70 43 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20  pC->iMem, 1);.  
1e874 7d 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69  }.  pAggInfo->di
1e875 72 65 63 74 4d 6f 64 65 20 3d 20 30 3b 0a 7d 0a  rectMode = 0;.}.
1e876 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1e877 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 67 69   code for the gi
1e878 76 65 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65  ven SELECT state
1e879 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
1e87a 72 65 73 75 6c 74 73 20 61 72 65 20 64 69 73 74  results are dist
1e87b 72 69 62 75 74 65 64 20 69 6e 20 76 61 72 69 6f  ributed in vario
1e87c 75 73 20 77 61 79 73 20 64 65 70 65 6e 64 69 6e  us ways dependin
1e87d 67 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75  g on the.** valu
1e87e 65 20 6f 66 20 65 44 65 73 74 20 61 6e 64 20 69  e of eDest and i
1e87f 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  Parm..**.**     
1e880 65 44 65 73 74 20 56 61 6c 75 65 20 20 20 20 20  eDest Value     
1e881 20 20 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20    Result.**     
1e882 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
1e883 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e884 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e885 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
1e886 20 20 20 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20     SRT_Callback 
1e887 20 20 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61     Invoke the ca
1e888 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61 63 68 20  llback for each 
1e889 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
1e88a 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  t..**.**     SRT
1e88b 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 53 74 6f  _Mem         Sto
1e88c 72 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20  re first result 
1e88d 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69  in memory cell i
1e88e 50 61 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  Parm.**.**     S
1e88f 52 54 5f 53 65 74 20 20 20 20 20 20 20 20 20 53  RT_Set         S
1e890 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20  tore results as 
1e891 6b 65 79 73 20 6f 66 20 74 61 62 6c 65 20 69 50  keys of table iP
1e892 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  arm..**.**     S
1e893 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 53  RT_Union       S
1e894 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20  tore results as 
1e895 61 20 6b 65 79 20 69 6e 20 61 20 74 65 6d 70 6f  a key in a tempo
1e896 72 61 72 79 20 74 61 62 6c 65 20 69 50 61 72 6d  rary table iParm
1e897 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45  .**.**     SRT_E
1e898 78 63 65 70 74 20 20 20 20 20 20 52 65 6d 6f 76  xcept      Remov
1e899 65 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 74  e results from t
1e89a 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
1e89b 6c 65 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20  le iParm..**.** 
1e89c 20 20 20 20 53 52 54 5f 54 61 62 6c 65 20 20 20      SRT_Table   
1e89d 20 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74      Store result
1e89e 73 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 74  s in temporary t
1e89f 61 62 6c 65 20 69 50 61 72 6d 0a 2a 2a 0a 2a 2a  able iParm.**.**
1e8a0 20 54 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65   The table above
1e8a1 20 69 73 20 69 6e 63 6f 6d 70 6c 65 74 65 2e 20   is incomplete. 
1e8a2 20 41 64 64 69 74 69 6f 6e 61 6c 20 65 44 69 73   Additional eDis
1e8a3 74 20 76 61 6c 75 65 20 68 61 76 65 20 62 65 20  t value have be 
1e8a4 61 64 64 65 64 0a 2a 2a 20 73 69 6e 63 65 20 74  added.** since t
1e8a5 68 69 73 20 63 6f 6d 6d 65 6e 74 20 77 61 73 20  his comment was 
1e8a6 77 72 69 74 74 65 6e 2e 20 20 53 65 65 20 74 68  written.  See th
1e8a7 65 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f  e selectInnerLoo
1e8a8 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  p() function for
1e8a9 0a 2a 2a 20 61 20 63 6f 6d 70 6c 65 74 65 20 6c  .** a complete l
1e8aa 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 61 6c  isting of the al
1e8ab 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20  lowed values of 
1e8ac 65 44 65 73 74 20 61 6e 64 20 74 68 65 69 72 20  eDest and their 
1e8ad 6d 65 61 6e 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20  meanings..**.** 
1e8ae 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
1e8af 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
1e8b0 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20 61  of errors.  If a
1e8b1 6e 79 20 65 72 72 6f 72 73 20 61 72 65 0a 2a 2a  ny errors are.**
1e8b2 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68   encountered, th
1e8b3 65 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  en an appropriat
1e8b4 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
1e8b5 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a 20 70 50  is left in.** pP
1e8b6 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a  arse->zErrMsg..*
1e8b7 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1e8b8 65 20 64 6f 65 73 20 4e 4f 54 20 66 72 65 65 20  e does NOT free 
1e8b9 74 68 65 20 53 65 6c 65 63 74 20 73 74 72 75 63  the Select struc
1e8ba 74 75 72 65 20 70 61 73 73 65 64 20 69 6e 2e 20  ture passed in. 
1e8bb 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20   The.** calling 
1e8bc 66 75 6e 63 74 69 6f 6e 20 6e 65 65 64 73 20 74  function needs t
1e8bd 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a  o do that..**.**
1e8be 20 54 68 65 20 70 50 61 72 65 6e 74 2c 20 70 61   The pParent, pa
1e8bf 72 65 6e 74 54 61 62 2c 20 61 6e 64 20 2a 70 50  rentTab, and *pP
1e8c0 61 72 65 6e 74 41 67 67 20 66 69 65 6c 64 73 20  arentAgg fields 
1e8c1 61 72 65 20 66 69 6c 6c 65 64 20 69 6e 20 69 66  are filled in if
1e8c2 20 74 68 69 73 0a 2a 2a 20 53 45 4c 45 43 54 20   this.** SELECT 
1e8c3 69 73 20 61 20 73 75 62 71 75 65 72 79 2e 20 20  is a subquery.  
1e8c4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79  This routine may
1e8c5 20 74 72 79 20 74 6f 20 63 6f 6d 62 69 6e 65 20   try to combine 
1e8c6 74 68 69 73 20 53 45 4c 45 43 54 0a 2a 2a 20 77  this SELECT.** w
1e8c7 69 74 68 20 69 74 73 20 70 61 72 65 6e 74 20 74  ith its parent t
1e8c8 6f 20 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20  o form a single 
1e8c9 66 6c 61 74 20 71 75 65 72 79 2e 20 20 49 6e 20  flat query.  In 
1e8ca 73 6f 20 64 6f 69 6e 67 2c 20 69 74 20 6d 69 67  so doing, it mig
1e8cb 68 74 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65  ht.** change the
1e8cc 20 70 61 72 65 6e 74 20 71 75 65 72 79 20 66 72   parent query fr
1e8cd 6f 6d 20 61 20 6e 6f 6e 2d 61 67 67 72 65 67 61  om a non-aggrega
1e8ce 74 65 20 74 6f 20 61 6e 20 61 67 67 72 65 67 61  te to an aggrega
1e8cf 74 65 20 71 75 65 72 79 2e 0a 2a 2a 20 46 6f 72  te query..** For
1e8d0 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 74 68   that reason, th
1e8d1 65 20 70 50 61 72 65 6e 74 41 67 67 20 66 6c 61  e pParentAgg fla
1e8d2 67 20 69 73 20 70 61 73 73 65 64 20 61 73 20 61  g is passed as a
1e8d3 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 69 74 0a   pointer, so it.
1e8d4 2a 2a 20 63 61 6e 20 62 65 20 63 68 61 6e 67 65  ** can be change
1e8d5 64 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65  d..**.** Example
1e8d6 20 31 3a 20 20 20 54 68 65 20 6d 65 61 6e 69 6e   1:   The meanin
1e8d7 67 20 6f 66 20 74 68 65 20 70 50 61 72 65 6e 74  g of the pParent
1e8d8 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
1e8d9 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  *    SELECT * FR
1e8da 4f 4d 20 74 31 20 4a 4f 49 4e 20 28 53 45 4c 45  OM t1 JOIN (SELE
1e8db 43 54 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46  CT x, count(*) F
1e8dc 52 4f 4d 20 74 32 29 20 4a 4f 49 4e 20 74 33 3b  ROM t2) JOIN t3;
1e8dd 0a 2a 2a 20 20 20 20 5c 20 20 20 20 20 20 20 20  .**    \        
1e8de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f                \_
1e8df 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79 20  ______ subquery 
1e8e0 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 20  _______/        
1e8e1 2f 0a 2a 2a 20 20 20 20 20 5c 20 20 20 20 20 20  /.**     \      
1e8e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e8e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e8e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e8e5 2f 0a 2a 2a 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f  /.**      \_____
1e8e6 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20  _______________ 
1e8e7 6f 75 74 65 72 20 71 75 65 72 79 20 5f 5f 5f 5f  outer query ____
1e8e8 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
1e8e9 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1e8ea 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f  ine is called fo
1e8eb 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
1e8ec 79 20 66 69 72 73 74 2e 20 20 20 46 6f 72 20 74  y first.   For t
1e8ed 68 61 74 20 63 61 6c 6c 2c 0a 2a 2a 20 70 50 61  hat call,.** pPa
1e8ee 72 65 6e 74 20 77 69 6c 6c 20 62 65 20 4e 55 4c  rent will be NUL
1e8ef 4c 2e 20 20 44 75 72 69 6e 67 20 74 68 65 20 70  L.  During the p
1e8f0 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68 65  rocessing of the
1e8f1 20 6f 75 74 65 72 20 71 75 65 72 79 2c 20 74 68   outer query, th
1e8f2 69 73 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  is .** routine i
1e8f3 73 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73 69  s called recursi
1e8f4 76 65 6c 79 20 74 6f 20 68 61 6e 64 6c 65 20 74  vely to handle t
1e8f5 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 46 6f  he subquery.  Fo
1e8f6 72 20 74 68 65 20 72 65 63 75 72 73 69 76 65 0a  r the recursive.
1e8f7 2a 2a 20 63 61 6c 6c 2c 20 70 50 61 72 65 6e 74  ** call, pParent
1e8f8 20 77 69 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 74   will point to t
1e8f9 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20  he outer query. 
1e8fa 20 42 65 63 61 75 73 65 20 74 68 65 20 73 75 62   Because the sub
1e8fb 71 75 65 72 79 20 69 73 0a 2a 2a 20 74 68 65 20  query is.** the 
1e8fc 73 65 63 6f 6e 64 20 65 6c 65 6d 65 6e 74 20 69  second element i
1e8fd 6e 20 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f  n a three-way jo
1e8fe 69 6e 2c 20 74 68 65 20 70 61 72 65 6e 74 54 61  in, the parentTa
1e8ff 62 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c  b parameter will
1e900 0a 2a 2a 20 62 65 20 31 20 28 74 68 65 20 32 6e  .** be 1 (the 2n
1e901 64 20 76 61 6c 75 65 20 6f 66 20 61 20 30 2d 69  d value of a 0-i
1e902 6e 64 65 78 65 64 20 61 72 72 61 79 2e 29 0a 2a  ndexed array.).*
1e903 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1e904 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65   int sqlite3Sele
1e905 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ct(.  Parse *pPa
1e906 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
1e907 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
1e908 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
1e909 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
1e90a 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
1e90b 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64  tement being cod
1e90c 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 65 44 65  ed. */.  int eDe
1e90d 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  st,             
1e90e 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73  /* How to dispos
1e90f 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73  e of the results
1e910 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 2c   */.  int iParm,
1e911 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e912 41 20 70 61 72 61 6d 65 74 65 72 20 75 73 65 64  A parameter used
1e913 20 62 79 20 74 68 65 20 65 44 65 73 74 20 64 69   by the eDest di
1e914 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a 2f  sposal method */
1e915 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 61 72 65  .  Select *pPare
1e916 6e 74 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 6f  nt,       /* Ano
1e917 74 68 65 72 20 53 45 4c 45 43 54 20 66 6f 72 20  ther SELECT for 
1e918 77 68 69 63 68 20 74 68 69 73 20 69 73 20 61 20  which this is a 
1e919 73 75 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20 69  sub-query */.  i
1e91a 6e 74 20 70 61 72 65 6e 74 54 61 62 2c 20 20 20  nt parentTab,   
1e91b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
1e91c 6e 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 20  n pParent->pSrc 
1e91d 6f 66 20 74 68 69 73 20 71 75 65 72 79 20 2a 2f  of this query */
1e91e 0a 20 20 69 6e 74 20 2a 70 50 61 72 65 6e 74 41  .  int *pParentA
1e91f 67 67 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75  gg,       /* Tru
1e920 65 20 69 66 20 70 50 61 72 65 6e 74 20 75 73 65  e if pParent use
1e921 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  s aggregate func
1e922 74 69 6f 6e 73 20 2a 2f 0a 20 20 63 68 61 72 20  tions */.  char 
1e923 2a 61 66 66 20 20 20 20 20 20 20 20 20 20 20 20  *aff            
1e924 20 20 2f 2a 20 49 66 20 65 44 65 73 74 20 69 73    /* If eDest is
1e925 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 68 65 20   SRT_Union, the 
1e926 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
1e927 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  */.){.  int i, j
1e928 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1e929 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
1e92a 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
1e92b 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 52  pWInfo;     /* R
1e92c 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74  eturn from sqlit
1e92d 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a  e3WhereBegin() *
1e92e 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
1e92f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1e930 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
1e931 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
1e932 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 73  tion */.  int is
1e933 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Agg;            
1e934 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 73 65 6c   /* True for sel
1e935 65 63 74 20 6c 69 73 74 73 20 6c 69 6b 65 20 22  ect lists like "
1e936 63 6f 75 6e 74 28 2a 29 22 20 2a 2f 0a 20 20 45  count(*)" */.  E
1e937 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
1e938 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
1e939 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 65 78 74 72   columns to extr
1e93a 61 63 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69 73  act. */.  SrcLis
1e93b 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 20  t *pTabList;    
1e93c 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
1e93d 65 73 20 74 6f 20 73 65 6c 65 63 74 20 66 72 6f  es to select fro
1e93e 6d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  m */.  Expr *pWh
1e93f 65 72 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ere;          /*
1e940 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
1e941 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20  e.  May be NULL 
1e942 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
1e943 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54  OrderBy;    /* T
1e944 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
1e945 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  se.  May be NULL
1e946 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1e947 70 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20  pGroupBy;    /* 
1e948 54 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  The GROUP BY cla
1e949 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  use.  May be NUL
1e94a 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61  L */.  Expr *pHa
1e94b 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  ving;         /*
1e94c 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75   The HAVING clau
1e94d 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  se.  May be NULL
1e94e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69 73 74   */.  int isDist
1e94f 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  inct;        /* 
1e950 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53 54  True if the DIST
1e951 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20  INCT keyword is 
1e952 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  present */.  int
1e953 20 64 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20   distinct;      
1e954 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20      /* Table to 
1e955 75 73 65 20 66 6f 72 20 74 68 65 20 64 69 73 74  use for the dist
1e956 69 6e 63 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e  inct set */.  in
1e957 74 20 72 63 20 3d 20 31 3b 20 20 20 20 20 20 20  t rc = 1;       
1e958 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
1e959 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 69   return from thi
1e95a 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  s function */.  
1e95b 69 6e 74 20 61 64 64 72 53 6f 72 74 49 6e 64 65  int addrSortInde
1e95c 78 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  x;     /* Addres
1e95d 73 20 6f 66 20 61 6e 20 4f 50 5f 4f 70 65 6e 45  s of an OP_OpenE
1e95e 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63  phemeral instruc
1e95f 74 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66  tion */.  AggInf
1e960 6f 20 73 41 67 67 49 6e 66 6f 3b 20 20 20 20 20  o sAggInfo;     
1e961 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
1e962 75 73 65 64 20 62 79 20 61 67 67 72 65 67 61 74  used by aggregat
1e963 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 69  e queries */.  i
1e964 6e 74 20 69 45 6e 64 3b 20 20 20 20 20 20 20 20  nt iEnd;        
1e965 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
1e966 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   of the end of t
1e967 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 73 71  he query */.  sq
1e968 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
1e969 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1e96a 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1e96b 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73  */..  db = pPars
1e96c 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 3d 3d  e->db;.  if( p==
1e96d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
1e96e 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 2d  ailed || pParse-
1e96f 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 72 65 74  >nErr ){.    ret
1e970 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 1;.  }.  if(
1e971 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
1e972 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
1e973 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20 30  _SELECT, 0, 0, 0
1e974 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
1e975 6d 65 6d 73 65 74 28 26 73 41 67 67 49 6e 66 6f  memset(&sAggInfo
1e976 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41 67 67  , 0, sizeof(sAgg
1e977 49 6e 66 6f 29 29 3b 0a 0a 23 69 66 6e 64 65 66  Info));..#ifndef
1e978 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
1e979 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 2f  POUND_SELECT.  /
1e97a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 72  * If there is ar
1e97b 65 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  e a sequence of 
1e97c 71 75 65 72 69 65 73 2c 20 64 6f 20 74 68 65 20  queries, do the 
1e97d 65 61 72 6c 69 65 72 20 6f 6e 65 73 20 66 69 72  earlier ones fir
1e97e 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  st..  */.  if( p
1e97f 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20  ->pPrior ){.    
1e980 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73  if( p->pRightmos
1e981 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 65  t==0 ){.      Se
1e982 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 0a 20 20 20  lect *pLoop;.   
1e983 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a     int cnt = 0;.
1e984 20 20 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d        for(pLoop=
1e985 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d  p; pLoop; pLoop=
1e986 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 2c 20 63  pLoop->pPrior, c
1e987 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70  nt++){.        p
1e988 4c 6f 6f 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  Loop->pRightmost
1e989 20 3d 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = p;.      }.  
1e98a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4d      if( SQLITE_M
1e98b 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  AX_COMPOUND_SELE
1e98c 43 54 3e 30 20 26 26 20 63 6e 74 3e 53 51 4c 49  CT>0 && cnt>SQLI
1e98d 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f  TE_MAX_COMPOUND_
1e98e 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 20 20  SELECT ){.      
1e98f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1e990 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d  g(pParse, "too m
1e991 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 63 6f 6d  any terms in com
1e992 70 6f 75 6e 64 20 53 45 4c 45 43 54 22 29 3b 0a  pound SELECT");.
1e993 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
1e994 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1e995 20 20 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 69      return multi
1e996 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
1e997 2c 20 65 44 65 73 74 2c 20 69 50 61 72 6d 2c 20  , eDest, iParm, 
1e998 61 66 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  aff);.  }.#endif
1e999 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70  ..  pOrderBy = p
1e99a 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66  ->pOrderBy;.  if
1e99b 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72  ( IgnorableOrder
1e99c 62 79 28 65 44 65 73 74 29 20 29 7b 0a 20 20 20  by(eDest) ){.   
1e99d 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30   p->pOrderBy = 0
1e99e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
1e99f 74 65 33 53 65 6c 65 63 74 52 65 73 6f 6c 76 65  te3SelectResolve
1e9a0 28 70 50 61 72 73 65 2c 20 70 2c 20 30 29 20 29  (pParse, p, 0) )
1e9a1 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63  {.    goto selec
1e9a2 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 2d 3e  t_end;.  }.  p->
1e9a3 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65  pOrderBy = pOrde
1e9a4 72 42 79 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  rBy;..  /* Make 
1e9a5 6c 6f 63 61 6c 20 63 6f 70 69 65 73 20 6f 66 20  local copies of 
1e9a6 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 66  the parameters f
1e9a7 6f 72 20 74 68 69 73 20 71 75 65 72 79 2e 0a 20  or this query.. 
1e9a8 20 2a 2f 0a 20 20 70 54 61 62 4c 69 73 74 20 3d   */.  pTabList =
1e9a9 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70 57 68 65   p->pSrc;.  pWhe
1e9aa 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b 0a  re = p->pWhere;.
1e9ab 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e    pGroupBy = p->
1e9ac 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 48 61 76  pGroupBy;.  pHav
1e9ad 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76 69 6e 67  ing = p->pHaving
1e9ae 3b 0a 20 20 69 73 41 67 67 20 3d 20 70 2d 3e 69  ;.  isAgg = p->i
1e9af 73 41 67 67 3b 0a 20 20 69 73 44 69 73 74 69 6e  sAgg;.  isDistin
1e9b0 63 74 20 3d 20 70 2d 3e 69 73 44 69 73 74 69 6e  ct = p->isDistin
1e9b1 63 74 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70  ct;.  pEList = p
1e9b2 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20  ->pEList;.  if( 
1e9b3 70 45 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f  pEList==0 ) goto
1e9b4 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20   select_end;..  
1e9b5 2f 2a 20 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20  /* .  ** Do not 
1e9b6 65 76 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  even attempt to 
1e9b7 67 65 6e 65 72 61 74 65 20 61 6e 79 20 63 6f 64  generate any cod
1e9b8 65 20 69 66 20 77 65 20 68 61 76 65 20 61 6c 72  e if we have alr
1e9b9 65 61 64 79 20 73 65 65 6e 0a 20 20 2a 2a 20 65  eady seen.  ** e
1e9ba 72 72 6f 72 73 20 62 65 66 6f 72 65 20 74 68 69  rrors before thi
1e9bb 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73  s routine starts
1e9bc 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
1e9bd 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 20 67 6f  rse->nErr>0 ) go
1e9be 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a  to select_end;..
1e9bf 20 20 2f 2a 20 49 66 20 77 72 69 74 69 6e 67 20    /* If writing 
1e9c0 74 6f 20 6d 65 6d 6f 72 79 20 6f 72 20 67 65 6e  to memory or gen
1e9c1 65 72 61 74 69 6e 67 20 61 20 73 65 74 0a 20 20  erating a set.  
1e9c2 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  ** only a single
1e9c3 20 63 6f 6c 75 6d 6e 20 6d 61 79 20 62 65 20 6f   column may be o
1e9c4 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e  utput..  */.#ifn
1e9c5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1e9c6 53 55 42 51 55 45 52 59 0a 20 20 69 66 28 20 63  SUBQUERY.  if( c
1e9c7 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75  heckForMultiColu
1e9c8 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50  mnSelectError(pP
1e9c9 61 72 73 65 2c 20 65 44 65 73 74 2c 20 70 45 4c  arse, eDest, pEL
1e9ca 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20  ist->nExpr) ){. 
1e9cb 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65     goto select_e
1e9cc 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  nd;.  }.#endif..
1e9cd 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 69 73    /* ORDER BY is
1e9ce 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 73 6f 6d   ignored for som
1e9cf 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 73 2e 0a  e destinations..
1e9d0 20 20 2a 2f 0a 20 20 69 66 28 20 49 67 6e 6f 72    */.  if( Ignor
1e9d1 61 62 6c 65 4f 72 64 65 72 62 79 28 65 44 65 73  ableOrderby(eDes
1e9d2 74 29 20 29 7b 0a 20 20 20 20 70 4f 72 64 65 72  t) ){.    pOrder
1e9d3 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  By = 0;.  }..  /
1e9d4 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69  * Begin generati
1e9d5 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  ng code..  */.  
1e9d6 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1e9d7 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
1e9d8 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65  ( v==0 ) goto se
1e9d9 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20  lect_end;..  /* 
1e9da 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
1e9db 72 20 61 6c 6c 20 73 75 62 2d 71 75 65 72 69 65  r all sub-querie
1e9dc 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
1e9dd 61 75 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64  ause.  */.#if !d
1e9de 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1e9df 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20  IT_SUBQUERY) || 
1e9e0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1e9e1 4f 4d 49 54 5f 56 49 45 57 29 0a 20 20 66 6f 72  OMIT_VIEW).  for
1e9e2 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74  (i=0; i<pTabList
1e9e3 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
1e9e4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
1e9e5 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 20  avedAuthContext 
1e9e6 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 65 65  = 0;.    int nee
1e9e7 64 52 65 73 74 6f 72 65 43 6f 6e 74 65 78 74 3b  dRestoreContext;
1e9e8 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
1e9e9 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
1e9ea 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  = &pTabList->a[i
1e9eb 5d 3b 0a 0a 20 20 20 20 69 66 28 20 70 49 74 65  ];..    if( pIte
1e9ec 6d 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 7c 7c  m->pSelect==0 ||
1e9ed 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61   pItem->isPopula
1e9ee 74 65 64 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ted ) continue;.
1e9ef 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a      if( pItem->z
1e9f0 4e 61 6d 65 21 3d 30 20 29 7b 0a 20 20 20 20 20  Name!=0 ){.     
1e9f1 20 7a 53 61 76 65 64 41 75 74 68 43 6f 6e 74 65   zSavedAuthConte
1e9f2 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75  xt = pParse->zAu
1e9f3 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20  thContext;.     
1e9f4 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
1e9f5 6e 74 65 78 74 20 3d 20 70 49 74 65 6d 2d 3e 7a  ntext = pItem->z
1e9f6 4e 61 6d 65 3b 0a 20 20 20 20 20 20 6e 65 65 64  Name;.      need
1e9f7 52 65 73 74 6f 72 65 43 6f 6e 74 65 78 74 20 3d  RestoreContext =
1e9f8 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
1e9f9 20 20 20 20 20 6e 65 65 64 52 65 73 74 6f 72 65       needRestore
1e9fa 43 6f 6e 74 65 78 74 20 3d 20 30 3b 0a 20 20 20  Context = 0;.   
1e9fb 20 7d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53   }.#if defined(S
1e9fc 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 53  QLITE_TEST) || S
1e9fd 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
1e9fe 45 50 54 48 3e 30 0a 20 20 20 20 2f 2a 20 49 6e  EPTH>0.    /* In
1e9ff 63 72 65 6d 65 6e 74 20 50 61 72 73 65 2e 6e 48  crement Parse.nH
1ea00 65 69 67 68 74 20 62 79 20 74 68 65 20 68 65 69  eight by the hei
1ea01 67 68 74 20 6f 66 20 74 68 65 20 6c 61 72 67 65  ght of the large
1ea02 73 74 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20  st expression.  
1ea03 20 20 2a 2a 20 74 72 65 65 20 72 65 66 65 72 65    ** tree refere
1ea04 64 20 74 6f 20 62 79 20 74 68 69 73 2c 20 74 68  d to by this, th
1ea05 65 20 70 61 72 65 6e 74 20 73 65 6c 65 63 74 2e  e parent select.
1ea06 20 54 68 65 20 63 68 69 6c 64 20 73 65 6c 65 63   The child selec
1ea07 74 0a 20 20 20 20 2a 2a 20 6d 61 79 20 63 6f 6e  t.    ** may con
1ea08 74 61 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  tain expression 
1ea09 74 72 65 65 73 20 6f 66 20 61 74 20 6d 6f 73 74  trees of at most
1ea0a 0a 20 20 20 20 2a 2a 20 28 53 51 4c 49 54 45 5f  .    ** (SQLITE_
1ea0b 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 2d 50  MAX_EXPR_DEPTH-P
1ea0c 61 72 73 65 2e 6e 48 65 69 67 68 74 29 20 68 65  arse.nHeight) he
1ea0d 69 67 68 74 2e 20 54 68 69 73 20 69 73 20 61 20  ight. This is a 
1ea0e 62 69 74 0a 20 20 20 20 2a 2a 20 6d 6f 72 65 20  bit.    ** more 
1ea0f 63 6f 6e 73 65 72 76 61 74 69 76 65 20 74 68 61  conservative tha
1ea10 6e 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75 74  n necessary, but
1ea11 20 6d 75 63 68 20 65 61 73 69 65 72 20 74 68 61   much easier tha
1ea12 6e 20 65 6e 66 6f 72 63 69 6e 67 0a 20 20 20 20  n enforcing.    
1ea13 2a 2a 20 61 6e 20 65 78 61 63 74 20 6c 69 6d 69  ** an exact limi
1ea14 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50  t..    */.    pP
1ea15 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d  arse->nHeight +=
1ea16 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
1ea17 70 72 48 65 69 67 68 74 28 70 29 3b 0a 23 65 6e  prHeight(p);.#en
1ea18 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 53  dif.    sqlite3S
1ea19 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 49  elect(pParse, pI
1ea1a 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 53 52  tem->pSelect, SR
1ea1b 54 5f 45 70 68 65 6d 54 61 62 2c 20 0a 20 20 20  T_EphemTab, .   
1ea1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
1ea1d 74 65 6d 2d 3e 69 43 75 72 73 6f 72 2c 20 70 2c  tem->iCursor, p,
1ea1e 20 69 2c 20 26 69 73 41 67 67 2c 20 30 29 3b 0a   i, &isAgg, 0);.
1ea1f 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
1ea20 54 45 5f 54 45 53 54 29 20 7c 7c 20 53 51 4c 49  TE_TEST) || SQLI
1ea21 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
1ea22 48 3e 30 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  H>0.    pParse->
1ea23 6e 48 65 69 67 68 74 20 2d 3d 20 73 71 6c 69 74  nHeight -= sqlit
1ea24 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
1ea25 68 74 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20  ht(p);.#endif.  
1ea26 20 20 69 66 28 20 6e 65 65 64 52 65 73 74 6f 72    if( needRestor
1ea27 65 43 6f 6e 74 65 78 74 20 29 7b 0a 20 20 20 20  eContext ){.    
1ea28 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43    pParse->zAuthC
1ea29 6f 6e 74 65 78 74 20 3d 20 7a 53 61 76 65 64 41  ontext = zSavedA
1ea2a 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  uthContext;.    
1ea2b 7d 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d  }.    pTabList =
1ea2c 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 70 57   p->pSrc;.    pW
1ea2d 68 65 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65  here = p->pWhere
1ea2e 3b 0a 20 20 20 20 69 66 28 20 21 49 67 6e 6f 72  ;.    if( !Ignor
1ea2f 61 62 6c 65 4f 72 64 65 72 62 79 28 65 44 65 73  ableOrderby(eDes
1ea30 74 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 72 64  t) ){.      pOrd
1ea31 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
1ea32 42 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 47  By;.    }.    pG
1ea33 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f  roupBy = p->pGro
1ea34 75 70 42 79 3b 0a 20 20 20 20 70 48 61 76 69 6e  upBy;.    pHavin
1ea35 67 20 3d 20 70 2d 3e 70 48 61 76 69 6e 67 3b 0a  g = p->pHaving;.
1ea36 20 20 20 20 69 73 44 69 73 74 69 6e 63 74 20 3d      isDistinct =
1ea37 20 70 2d 3e 69 73 44 69 73 74 69 6e 63 74 3b 0a   p->isDistinct;.
1ea38 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1ea39 20 43 68 65 63 6b 20 66 6f 72 20 74 68 65 20 73   Check for the s
1ea3a 70 65 63 69 61 6c 20 63 61 73 65 20 6f 66 20 61  pecial case of a
1ea3b 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 20   min() or max() 
1ea3c 66 75 6e 63 74 69 6f 6e 20 62 79 20 69 74 73 65  function by itse
1ea3d 6c 66 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72  lf.  ** in the r
1ea3e 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 2a 2f 0a  esult set..  */.
1ea3f 20 20 69 66 28 20 73 69 6d 70 6c 65 4d 69 6e 4d    if( simpleMinM
1ea40 61 78 51 75 65 72 79 28 70 50 61 72 73 65 2c 20  axQuery(pParse, 
1ea41 70 2c 20 65 44 65 73 74 2c 20 69 50 61 72 6d 29  p, eDest, iParm)
1ea42 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a   ){.    rc = 0;.
1ea43 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f      goto select_
1ea44 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  end;.  }..  /* C
1ea45 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1ea46 68 69 73 20 69 73 20 61 20 73 75 62 71 75 65 72  his is a subquer
1ea47 79 20 74 68 61 74 20 63 61 6e 20 62 65 20 22 66  y that can be "f
1ea48 6c 61 74 74 65 6e 65 64 22 20 69 6e 74 6f 20 69  lattened" into i
1ea49 74 73 20 70 61 72 65 6e 74 2e 0a 20 20 2a 2a 20  ts parent..  ** 
1ea4a 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73  If flattening is
1ea4b 20 61 20 70 6f 73 73 69 62 6c 69 74 79 2c 20 64   a possiblity, d
1ea4c 6f 20 73 6f 20 61 6e 64 20 72 65 74 75 72 6e 20  o so and return 
1ea4d 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 0a 20  immediately.  . 
1ea4e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
1ea4f 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69  TE_OMIT_VIEW.  i
1ea50 66 28 20 70 50 61 72 65 6e 74 20 26 26 20 70 50  f( pParent && pP
1ea51 61 72 65 6e 74 41 67 67 20 26 26 0a 20 20 20 20  arentAgg &&.    
1ea52 20 20 66 6c 61 74 74 65 6e 53 75 62 71 75 65 72    flattenSubquer
1ea53 79 28 64 62 2c 20 70 50 61 72 65 6e 74 2c 20 70  y(db, pParent, p
1ea54 61 72 65 6e 74 54 61 62 2c 20 2a 70 50 61 72 65  arentTab, *pPare
1ea55 6e 74 41 67 67 2c 20 69 73 41 67 67 29 20 29 7b  ntAgg, isAgg) ){
1ea56 0a 20 20 20 20 69 66 28 20 69 73 41 67 67 20 29  .    if( isAgg )
1ea57 20 2a 70 50 61 72 65 6e 74 41 67 67 20 3d 20 31   *pParentAgg = 1
1ea58 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63  ;.    goto selec
1ea59 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64 69  t_end;.  }.#endi
1ea5a 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  f..  /* If there
1ea5b 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20   is an ORDER BY 
1ea5c 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 68 69  clause, then thi
1ea5d 73 20 73 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 69  s sorting.  ** i
1ea5e 6e 64 65 78 20 6d 69 67 68 74 20 65 6e 64 20 75  ndex might end u
1ea5f 70 20 62 65 69 6e 67 20 75 6e 75 73 65 64 20 69  p being unused i
1ea60 66 20 74 68 65 20 64 61 74 61 20 63 61 6e 20 62  f the data can b
1ea61 65 20 0a 20 20 2a 2a 20 65 78 74 72 61 63 74 65  e .  ** extracte
1ea62 64 20 69 6e 20 70 72 65 2d 73 6f 72 74 65 64 20  d in pre-sorted 
1ea63 6f 72 64 65 72 2e 20 20 49 66 20 74 68 61 74 20  order.  If that 
1ea64 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65  is the case, the
1ea65 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 50 5f 4f 70  n the.  ** OP_Op
1ea66 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74  enEphemeral inst
1ea67 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  ruction will be 
1ea68 63 68 61 6e 67 65 64 20 74 6f 20 61 6e 20 4f 50  changed to an OP
1ea69 5f 4e 6f 6f 70 20 6f 6e 63 65 0a 20 20 2a 2a 20  _Noop once.  ** 
1ea6a 77 65 20 66 69 67 75 72 65 20 6f 75 74 20 74 68  we figure out th
1ea6b 61 74 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  at the sorting i
1ea6c 6e 64 65 78 20 69 73 20 6e 6f 74 20 6e 65 65 64  ndex is not need
1ea6d 65 64 2e 20 20 54 68 65 20 61 64 64 72 53 6f 72  ed.  The addrSor
1ea6e 74 49 6e 64 65 78 0a 20 20 2a 2a 20 76 61 72 69  tIndex.  ** vari
1ea6f 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20  able is used to 
1ea70 66 61 63 69 6c 69 74 61 74 65 20 74 68 61 74 20  facilitate that 
1ea71 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69  change..  */.  i
1ea72 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
1ea73 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
1ea74 49 6e 66 6f 3b 0a 20 20 20 20 69 66 28 20 70 50  Info;.    if( pP
1ea75 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20  arse->nErr ){.  
1ea76 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f      goto select_
1ea77 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  end;.    }.    p
1ea78 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66  KeyInfo = keyInf
1ea79 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50  oFromExprList(pP
1ea7a 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 29 3b  arse, pOrderBy);
1ea7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69  .    pOrderBy->i
1ea7c 45 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65  ECursor = pParse
1ea7d 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 2d  ->nTab++;.    p-
1ea7e 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
1ea7f 20 3d 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78   = addrSortIndex
1ea80 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   =.      sqlite3
1ea81 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  VdbeOp3(v, OP_Op
1ea82 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 4f 72  enEphemeral, pOr
1ea83 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c  derBy->iECursor,
1ea84 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
1ea85 2b 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  +2,             
1ea86 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
1ea87 4b 65 79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59 49  KeyInfo, P3_KEYI
1ea88 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
1ea89 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 53  }else{.    addrS
1ea8a 6f 72 74 49 6e 64 65 78 20 3d 20 2d 31 3b 0a 20  ortIndex = -1;. 
1ea8b 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
1ea8c 6f 75 74 70 75 74 20 69 73 20 64 65 73 74 69 6e  output is destin
1ea8d 65 64 20 66 6f 72 20 61 20 74 65 6d 70 6f 72 61  ed for a tempora
1ea8e 72 79 20 74 61 62 6c 65 2c 20 6f 70 65 6e 20 74  ry table, open t
1ea8f 68 61 74 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  hat table..  */.
1ea90 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54    if( eDest==SRT
1ea91 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a 20 20 20  _EphemTab ){.   
1ea92 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ea93 70 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  p(v, OP_OpenEphe
1ea94 6d 65 72 61 6c 2c 20 69 50 61 72 6d 2c 20 70 45  meral, iParm, pE
1ea95 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20  List->nExpr);.  
1ea96 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  }..  /* Set the 
1ea97 6c 69 6d 69 74 65 72 2e 0a 20 20 2a 2f 0a 20 20  limiter..  */.  
1ea98 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64  iEnd = sqlite3Vd
1ea99 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
1ea9a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65    computeLimitRe
1ea9b 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20  gisters(pParse, 
1ea9c 70 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20  p, iEnd);..  /* 
1ea9d 4f 70 65 6e 20 61 20 76 69 72 74 75 61 6c 20 69  Open a virtual i
1ea9e 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20  ndex to use for 
1ea9f 74 68 65 20 64 69 73 74 69 6e 63 74 20 73 65 74  the distinct set
1eaa0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 44  ..  */.  if( isD
1eaa1 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 4b  istinct ){.    K
1eaa2 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
1eaa3 3b 0a 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d  ;.    distinct =
1eaa4 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
1eaa5 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20  .    pKeyInfo = 
1eaa6 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c  keyInfoFromExprL
1eaa7 69 73 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 70  ist(pParse, p->p
1eaa8 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  EList);.    sqli
1eaa9 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
1eaaa 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
1eaab 64 69 73 74 69 6e 63 74 2c 20 30 2c 20 0a 20 20  distinct, 0, .  
1eaac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eaad 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
1eaae 79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59 49 4e 46  yInfo, P3_KEYINF
1eaaf 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65  O_HANDOFF);.  }e
1eab0 6c 73 65 7b 0a 20 20 20 20 64 69 73 74 69 6e 63  lse{.    distinc
1eab1 74 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f  t = -1;.  }..  /
1eab2 2a 20 41 67 67 72 65 67 61 74 65 20 61 6e 64 20  * Aggregate and 
1eab3 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75  non-aggregate qu
1eab4 65 72 69 65 73 20 61 72 65 20 68 61 6e 64 6c 65  eries are handle
1eab5 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 2a 2f  d differently */
1eab6 0a 20 20 69 66 28 20 21 69 73 41 67 67 20 26 26  .  if( !isAgg &&
1eab7 20 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a   pGroupBy==0 ){.
1eab8 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65      /* This case
1eab9 20 69 73 20 66 6f 72 20 6e 6f 6e 2d 61 67 67 72   is for non-aggr
1eaba 65 67 61 74 65 20 71 75 65 72 69 65 73 0a 20 20  egate queries.  
1eabb 20 20 2a 2a 20 42 65 67 69 6e 20 74 68 65 20 64    ** Begin the d
1eabc 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20 20  atabase scan.   
1eabd 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d   */.    pWInfo =
1eabe 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
1eabf 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
1eac0 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70 4f  ist, pWhere, &pO
1eac1 72 64 65 72 42 79 29 3b 0a 20 20 20 20 69 66 28  rderBy);.    if(
1eac2 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74   pWInfo==0 ) got
1eac3 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20  o select_end;.. 
1eac4 20 20 20 2f 2a 20 49 66 20 73 6f 72 74 69 6e 67     /* If sorting
1eac5 20 69 6e 64 65 78 20 74 68 61 74 20 77 61 73 20   index that was 
1eac6 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 69  created by a pri
1eac7 6f 72 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  or OP_OpenEpheme
1eac8 72 61 6c 20 0a 20 20 20 20 2a 2a 20 69 6e 73 74  ral .    ** inst
1eac9 72 75 63 74 69 6f 6e 20 65 6e 64 65 64 20 75 70  ruction ended up
1eaca 20 6e 6f 74 20 62 65 69 6e 67 20 6e 65 65 64 65   not being neede
1eacb 64 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20 74  d, then change t
1eacc 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  he OP_OpenEpheme
1eacd 72 61 6c 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20  ral.    ** into 
1eace 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a 20 20 20 20  an OP_Noop..    
1eacf 2a 2f 0a 20 20 20 20 69 66 28 20 61 64 64 72 53  */.    if( addrS
1ead0 6f 72 74 49 6e 64 65 78 3e 3d 30 20 26 26 20 70  ortIndex>=0 && p
1ead1 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20  OrderBy==0 ){.  
1ead2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1ead3 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61  hangeToNoop(v, a
1ead4 64 64 72 53 6f 72 74 49 6e 64 65 78 2c 20 31 29  ddrSortIndex, 1)
1ead5 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f  ;.      p->addrO
1ead6 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b  penEphm[2] = -1;
1ead7 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 55  .    }..    /* U
1ead8 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  se the standard 
1ead9 69 6e 6e 65 72 20 6c 6f 6f 70 0a 20 20 20 20 2a  inner loop.    *
1eada 2f 0a 20 20 20 20 69 66 28 20 73 65 6c 65 63 74  /.    if( select
1eadb 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65  InnerLoop(pParse
1eadc 2c 20 70 2c 20 70 45 4c 69 73 74 2c 20 30 2c 20  , p, pEList, 0, 
1eadd 30 2c 20 70 4f 72 64 65 72 42 79 2c 20 64 69 73  0, pOrderBy, dis
1eade 74 69 6e 63 74 2c 20 65 44 65 73 74 2c 0a 20 20  tinct, eDest,.  
1eadf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eae0 20 20 69 50 61 72 6d 2c 20 70 57 49 6e 66 6f 2d    iParm, pWInfo-
1eae1 3e 69 43 6f 6e 74 69 6e 75 65 2c 20 70 57 49 6e  >iContinue, pWIn
1eae2 66 6f 2d 3e 69 42 72 65 61 6b 2c 20 61 66 66 29  fo->iBreak, aff)
1eae3 20 29 7b 0a 20 20 20 20 20 20 20 67 6f 74 6f 20   ){.       goto 
1eae4 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
1eae5 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68  }..    /* End th
1eae6 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20  e database scan 
1eae7 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  loop..    */.   
1eae8 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
1eae9 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 65 6c 73  (pWInfo);.  }els
1eaea 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69  e{.    /* This i
1eaeb 73 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67  s the processing
1eaec 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 71   for aggregate q
1eaed 75 65 72 69 65 73 20 2a 2f 0a 20 20 20 20 4e 61  ueries */.    Na
1eaee 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
1eaef 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
1eaf0 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  t for processing
1eaf1 20 61 67 67 72 65 67 61 74 65 20 69 6e 66 6f 72   aggregate infor
1eaf2 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e  mation */.    in
1eaf3 74 20 69 41 4d 65 6d 3b 20 20 20 20 20 20 20 20  t iAMem;        
1eaf4 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d 20 61    /* First Mem a
1eaf5 64 64 72 65 73 73 20 66 6f 72 20 73 74 6f 72 69  ddress for stori
1eaf6 6e 67 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50  ng current GROUP
1eaf7 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69   BY */.    int i
1eaf8 42 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  BMem;          /
1eaf9 2a 20 46 69 72 73 74 20 4d 65 6d 20 61 64 64 72  * First Mem addr
1eafa 65 73 73 20 66 6f 72 20 70 72 65 76 69 6f 75 73  ess for previous
1eafb 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20   GROUP BY */.   
1eafc 20 69 6e 74 20 69 55 73 65 46 6c 61 67 3b 20 20   int iUseFlag;  
1eafd 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72       /* Mem addr
1eafe 65 73 73 20 68 6f 6c 64 69 6e 67 20 66 6c 61 67  ess holding flag
1eaff 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
1eb00 20 61 74 20 6c 65 61 73 74 0a 20 20 20 20 20 20   at least.      
1eb01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb02 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20 6f 66 20    ** one row of 
1eb03 74 68 65 20 69 6e 70 75 74 20 74 6f 20 74 68 65  the input to the
1eb04 20 61 67 67 72 65 67 61 74 6f 72 20 68 61 73 20   aggregator has 
1eb05 62 65 65 6e 0a 20 20 20 20 20 20 20 20 20 20 20  been.           
1eb06 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
1eb07 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20 20  processed */.   
1eb08 20 69 6e 74 20 69 41 62 6f 72 74 46 6c 61 67 3b   int iAbortFlag;
1eb09 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72       /* Mem addr
1eb0a 65 73 73 20 77 68 69 63 68 20 63 61 75 73 65 73  ess which causes
1eb0b 20 71 75 65 72 79 20 61 62 6f 72 74 20 69 66 20   query abort if 
1eb0c 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 20 20  positive */.    
1eb0d 69 6e 74 20 67 72 6f 75 70 42 79 53 6f 72 74 3b  int groupBySort;
1eb0e 20 20 20 20 2f 2a 20 52 6f 77 73 20 63 6f 6d 65      /* Rows come
1eb0f 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 69 6e 20   from source in 
1eb10 47 52 4f 55 50 20 42 59 20 6f 72 64 65 72 20 2a  GROUP BY order *
1eb11 2f 0a 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66  /...    /* The f
1eb12 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
1eb13 65 73 20 68 6f 6c 64 20 61 64 64 72 65 73 73 65  es hold addresse
1eb14 73 20 6f 72 20 6c 61 62 65 6c 73 20 66 6f 72 20  s or labels for 
1eb15 70 61 72 74 73 20 6f 66 20 74 68 65 0a 20 20 20  parts of the.   
1eb16 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68   ** virtual mach
1eb17 69 6e 65 20 70 72 6f 67 72 61 6d 20 77 65 20 61  ine program we a
1eb18 72 65 20 70 75 74 74 69 6e 67 20 74 6f 67 65 74  re putting toget
1eb19 68 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61  her */.    int a
1eb1a 64 64 72 4f 75 74 70 75 74 52 6f 77 3b 20 20 20  ddrOutputRow;   
1eb1b 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 73     /* Start of s
1eb1c 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6f  ubroutine that o
1eb1d 75 74 70 75 74 73 20 61 20 72 65 73 75 6c 74 20  utputs a result 
1eb1e 72 6f 77 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61  row */.    int a
1eb1f 64 64 72 53 65 74 41 62 6f 72 74 3b 20 20 20 20  ddrSetAbort;    
1eb20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 61 62     /* Set the ab
1eb21 6f 72 74 20 66 6c 61 67 20 61 6e 64 20 72 65 74  ort flag and ret
1eb22 75 72 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61  urn */.    int a
1eb23 64 64 72 49 6e 69 74 69 61 6c 69 7a 65 4c 6f 6f  ddrInitializeLoo
1eb24 70 3b 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 63  p; /* Start of c
1eb25 6f 64 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c  ode that initial
1eb26 69 7a 65 73 20 74 68 65 20 69 6e 70 75 74 20 6c  izes the input l
1eb27 6f 6f 70 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61  oop */.    int a
1eb28 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 3b 20 20 20  ddrTopOfLoop;   
1eb29 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65     /* Top of the
1eb2a 20 69 6e 70 75 74 20 6c 6f 6f 70 20 2a 2f 0a 20   input loop */. 
1eb2b 20 20 20 69 6e 74 20 61 64 64 72 47 72 6f 75 70     int addrGroup
1eb2c 42 79 43 68 61 6e 67 65 3b 20 20 2f 2a 20 43 6f  ByChange;  /* Co
1eb2d 64 65 20 74 68 61 74 20 72 75 6e 73 20 77 68 65  de that runs whe
1eb2e 6e 20 61 6e 79 20 47 52 4f 55 50 20 42 59 20 74  n any GROUP BY t
1eb2f 65 72 6d 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20  erm changes */. 
1eb30 20 20 20 69 6e 74 20 61 64 64 72 50 72 6f 63 65     int addrProce
1eb31 73 73 52 6f 77 3b 20 20 20 20 20 2f 2a 20 43 6f  ssRow;     /* Co
1eb32 64 65 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20  de to process a 
1eb33 73 69 6e 67 6c 65 20 69 6e 70 75 74 20 72 6f 77  single input row
1eb34 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72   */.    int addr
1eb35 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  End;            
1eb36 2f 2a 20 45 6e 64 20 6f 66 20 61 6c 6c 20 70 72  /* End of all pr
1eb37 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 20 20 20 20  ocessing */.    
1eb38 69 6e 74 20 61 64 64 72 53 6f 72 74 69 6e 67 49  int addrSortingI
1eb39 64 78 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 4f  dx;     /* The O
1eb3a 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
1eb3b 66 6f 72 20 74 68 65 20 73 6f 72 74 69 6e 67 20  for the sorting 
1eb3c 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74  index */.    int
1eb3d 20 61 64 64 72 52 65 73 65 74 3b 20 20 20 20 20   addrReset;     
1eb3e 20 20 20 20 20 2f 2a 20 53 75 62 72 6f 75 74 69       /* Subrouti
1eb3f 6e 65 20 66 6f 72 20 72 65 73 65 74 74 69 6e 67  ne for resetting
1eb40 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1eb41 20 2a 2f 0a 0a 20 20 20 20 61 64 64 72 45 6e 64   */..    addrEnd
1eb42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
1eb43 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20  keLabel(v);..   
1eb44 20 2f 2a 20 43 6f 6e 76 65 72 74 20 54 4b 5f 43   /* Convert TK_C
1eb45 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20 69 6e 74 6f  OLUMN nodes into
1eb46 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61   TK_AGG_COLUMN a
1eb47 6e 64 20 6d 61 6b 65 20 65 6e 74 72 69 65 73 20  nd make entries 
1eb48 69 6e 0a 20 20 20 20 2a 2a 20 73 41 67 67 49 6e  in.    ** sAggIn
1eb49 66 6f 20 66 6f 72 20 61 6c 6c 20 54 4b 5f 41 47  fo for all TK_AG
1eb4a 47 5f 46 55 4e 43 54 49 4f 4e 20 6e 6f 64 65 73  G_FUNCTION nodes
1eb4b 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20   in expressions 
1eb4c 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 53 45  of the.    ** SE
1eb4d 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
1eb4e 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65      */.    memse
1eb4f 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
1eb50 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43  f(sNC));.    sNC
1eb51 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
1eb52 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69  ;.    sNC.pSrcLi
1eb53 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20  st = pTabList;. 
1eb54 20 20 20 73 4e 43 2e 70 41 67 67 49 6e 66 6f 20     sNC.pAggInfo 
1eb55 3d 20 26 73 41 67 67 49 6e 66 6f 3b 0a 20 20 20  = &sAggInfo;.   
1eb56 20 73 41 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69   sAggInfo.nSorti
1eb57 6e 67 43 6f 6c 75 6d 6e 20 3d 20 70 47 72 6f 75  ngColumn = pGrou
1eb58 70 42 79 20 3f 20 70 47 72 6f 75 70 42 79 2d 3e  pBy ? pGroupBy->
1eb59 6e 45 78 70 72 2b 31 20 3a 20 30 3b 0a 20 20 20  nExpr+1 : 0;.   
1eb5a 20 73 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70   sAggInfo.pGroup
1eb5b 42 79 20 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20  By = pGroupBy;. 
1eb5c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
1eb5d 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74  prAnalyzeAggList
1eb5e 28 26 73 4e 43 2c 20 70 45 4c 69 73 74 29 20 29  (&sNC, pEList) )
1eb5f 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c  {.      goto sel
1eb60 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20  ect_end;.    }. 
1eb61 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
1eb62 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74  prAnalyzeAggList
1eb63 28 26 73 4e 43 2c 20 70 4f 72 64 65 72 42 79 29  (&sNC, pOrderBy)
1eb64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
1eb65 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d  elect_end;.    }
1eb66 0a 20 20 20 20 69 66 28 20 70 48 61 76 69 6e 67  .    if( pHaving
1eb67 20 26 26 20 73 71 6c 69 74 65 33 45 78 70 72 41   && sqlite3ExprA
1eb68 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
1eb69 28 26 73 4e 43 2c 20 70 48 61 76 69 6e 67 29 20  (&sNC, pHaving) 
1eb6a 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65  ){.      goto se
1eb6b 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a  lect_end;.    }.
1eb6c 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 41 63      sAggInfo.nAc
1eb6d 63 75 6d 75 6c 61 74 6f 72 20 3d 20 73 41 67 67  cumulator = sAgg
1eb6e 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20  Info.nColumn;.  
1eb6f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67    for(i=0; i<sAg
1eb70 67 49 6e 66 6f 2e 6e 46 75 6e 63 3b 20 69 2b 2b  gInfo.nFunc; i++
1eb71 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
1eb72 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
1eb73 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 73 41 67  ggList(&sNC, sAg
1eb74 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70  gInfo.aFunc[i].p
1eb75 45 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 7b 0a  Expr->pList) ){.
1eb76 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c          goto sel
1eb77 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
1eb78 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
1eb79 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1eb7a 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e  ) goto select_en
1eb7b 64 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65  d;..    /* Proce
1eb7c 73 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67  ssing for aggreg
1eb7d 61 74 65 73 20 77 69 74 68 20 47 52 4f 55 50 20  ates with GROUP 
1eb7e 42 59 20 69 73 20 76 65 72 79 20 64 69 66 66 65  BY is very diffe
1eb7f 72 65 6e 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20  rent and.    ** 
1eb80 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 65  much more comple
1eb81 78 20 74 68 61 20 61 67 67 72 65 67 61 74 65 73  x tha aggregates
1eb82 20 77 69 74 68 6f 75 74 20 61 20 47 52 4f 55 50   without a GROUP
1eb83 20 42 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   BY..    */.    
1eb84 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a  if( pGroupBy ){.
1eb85 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
1eb86 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 4b 65 79  KeyInfo;  /* Key
1eb87 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
1eb88 66 6f 72 20 74 68 65 20 67 72 6f 75 70 20 62 79  for the group by
1eb89 20 63 6c 61 75 73 65 20 2a 2f 0a 0a 20 20 20 20   clause */..    
1eb8a 20 20 2f 2a 20 43 72 65 61 74 65 20 6c 61 62 65    /* Create labe
1eb8b 6c 73 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20  ls that we will 
1eb8c 62 65 20 6e 65 65 64 69 6e 67 0a 20 20 20 20 20  be needing.     
1eb8d 20 2a 2f 0a 20 20 20 20 20 0a 20 20 20 20 20 20   */.     .      
1eb8e 61 64 64 72 49 6e 69 74 69 61 6c 69 7a 65 4c 6f  addrInitializeLo
1eb8f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  op = sqlite3Vdbe
1eb90 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
1eb91 20 20 20 20 61 64 64 72 47 72 6f 75 70 42 79 43      addrGroupByC
1eb92 68 61 6e 67 65 20 3d 20 73 71 6c 69 74 65 33 56  hange = sqlite3V
1eb93 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1eb94 0a 20 20 20 20 20 20 61 64 64 72 50 72 6f 63 65  .      addrProce
1eb95 73 73 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56  ssRow = sqlite3V
1eb96 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1eb97 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  ..      /* If th
1eb98 65 72 65 20 69 73 20 61 20 47 52 4f 55 50 20 42  ere is a GROUP B
1eb99 59 20 63 6c 61 75 73 65 20 77 65 20 6d 69 67 68  Y clause we migh
1eb9a 74 20 6e 65 65 64 20 61 20 73 6f 72 74 69 6e 67  t need a sorting
1eb9b 20 69 6e 64 65 78 20 74 6f 0a 20 20 20 20 20 20   index to.      
1eb9c 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 69 74 2e  ** implement it.
1eb9d 20 20 41 6c 6c 6f 63 61 74 65 20 74 68 61 74 20    Allocate that 
1eb9e 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 6e 6f  sorting index no
1eb9f 77 2e 20 20 49 66 20 69 74 20 74 75 72 6e 73 20  w.  If it turns 
1eba0 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20 74 68 61  out.      ** tha
1eba1 74 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64  t we do not need
1eba2 20 69 74 20 61 66 74 65 72 20 61 6c 6c 2c 20 74   it after all, t
1eba3 68 65 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  he OpenEphemeral
1eba4 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 20 20 20   instruction.   
1eba5 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f     ** will be co
1eba6 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20 4e  nverted into a N
1eba7 6f 6f 70 2e 20 20 0a 20 20 20 20 20 20 2a 2f 0a  oop.  .      */.
1eba8 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 73        sAggInfo.s
1eba9 6f 72 74 69 6e 67 49 64 78 20 3d 20 70 50 61 72  ortingIdx = pPar
1ebaa 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
1ebab 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79    pKeyInfo = key
1ebac 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74  InfoFromExprList
1ebad 28 70 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42  (pParse, pGroupB
1ebae 79 29 3b 0a 20 20 20 20 20 20 61 64 64 72 53 6f  y);.      addrSo
1ebaf 72 74 69 6e 67 49 64 78 20 3d 0a 20 20 20 20 20  rtingIdx =.     
1ebb0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ebb1 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70  Op3(v, OP_OpenEp
1ebb2 68 65 6d 65 72 61 6c 2c 20 73 41 67 67 49 6e 66  hemeral, sAggInf
1ebb3 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20  o.sortingIdx,.  
1ebb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ebb5 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e         sAggInfo.
1ebb6 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2c 0a  nSortingColumn,.
1ebb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ebb8 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
1ebb9 70 4b 65 79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59  pKeyInfo, P3_KEY
1ebba 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 0a  INFO_HANDOFF);..
1ebbb 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c        /* Initial
1ebbc 69 7a 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  ize memory locat
1ebbd 69 6f 6e 73 20 75 73 65 64 20 62 79 20 47 52 4f  ions used by GRO
1ebbe 55 50 20 42 59 20 61 67 67 72 65 67 61 74 65 20  UP BY aggregate 
1ebbf 70 72 6f 63 65 73 73 69 6e 67 0a 20 20 20 20 20  processing.     
1ebc0 20 2a 2f 0a 20 20 20 20 20 20 69 55 73 65 46 6c   */.      iUseFl
1ebc1 61 67 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  ag = pParse->nMe
1ebc2 6d 2b 2b 3b 0a 20 20 20 20 20 20 69 41 62 6f 72  m++;.      iAbor
1ebc3 74 46 6c 61 67 20 3d 20 70 50 61 72 73 65 2d 3e  tFlag = pParse->
1ebc4 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 69 41  nMem++;.      iA
1ebc5 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Mem = pParse->nM
1ebc6 65 6d 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  em;.      pParse
1ebc7 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70  ->nMem += pGroup
1ebc8 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20  By->nExpr;.     
1ebc9 20 69 42 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d   iBMem = pParse-
1ebca 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 70 50 61  >nMem;.      pPa
1ebcb 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72  rse->nMem += pGr
1ebcc 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20  oupBy->nExpr;.  
1ebcd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ebce 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e  ddOp(v, OP_MemIn
1ebcf 74 2c 20 30 2c 20 69 41 62 6f 72 74 46 6c 61 67  t, 0, iAbortFlag
1ebd0 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
1ebd1 6d 65 6e 74 28 28 76 2c 20 22 23 20 63 6c 65 61  ment((v, "# clea
1ebd2 72 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29 3b  r abort flag"));
1ebd3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ebd4 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1ebd5 6d 49 6e 74 2c 20 30 2c 20 69 55 73 65 46 6c 61  mInt, 0, iUseFla
1ebd6 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  g);.      VdbeCo
1ebd7 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 69 6e 64  mment((v, "# ind
1ebd8 69 63 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f  icate accumulato
1ebd9 72 20 65 6d 70 74 79 22 29 29 3b 0a 20 20 20 20  r empty"));.    
1ebda 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ebdb 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  Op(v, OP_Goto, 0
1ebdc 2c 20 61 64 64 72 49 6e 69 74 69 61 6c 69 7a 65  , addrInitialize
1ebdd 4c 6f 6f 70 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  Loop);..      /*
1ebde 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72   Generate a subr
1ebdf 6f 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70  outine that outp
1ebe0 75 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  uts a single row
1ebe1 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 0a 20   of the result. 
1ebe2 20 20 20 20 20 2a 2a 20 73 65 74 2e 20 20 54 68       ** set.  Th
1ebe3 69 73 20 73 75 62 72 6f 75 74 69 6e 65 20 66 69  is subroutine fi
1ebe4 72 73 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65  rst looks at the
1ebe5 20 69 55 73 65 46 6c 61 67 2e 20 20 49 66 20 69   iUseFlag.  If i
1ebe6 55 73 65 46 6c 61 67 0a 20 20 20 20 20 20 2a 2a  UseFlag.      **
1ebe7 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
1ebe8 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20   equal to zero, 
1ebe9 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69  the subroutine i
1ebea 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20  s a no-op.  If. 
1ebeb 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63       ** the proc
1ebec 65 73 73 69 6e 67 20 63 61 6c 6c 73 20 66 6f 72  essing calls for
1ebed 20 74 68 65 20 71 75 65 72 79 20 74 6f 20 61 62   the query to ab
1ebee 6f 72 74 2c 20 74 68 69 73 20 73 75 62 72 6f 75  ort, this subrou
1ebef 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e  tine.      ** in
1ebf0 63 72 65 6d 65 6e 74 73 20 74 68 65 20 69 41 62  crements the iAb
1ebf1 6f 72 74 46 6c 61 67 20 6d 65 6d 6f 72 79 20 6c  ortFlag memory l
1ebf2 6f 63 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 72  ocation before r
1ebf3 65 74 75 72 6e 69 6e 67 20 69 6e 0a 20 20 20 20  eturning in.    
1ebf4 20 20 2a 2a 20 6f 72 64 65 72 20 74 6f 20 73 69    ** order to si
1ebf5 67 6e 61 6c 20 74 68 65 20 63 61 6c 6c 65 72 20  gnal the caller 
1ebf6 74 6f 20 61 62 6f 72 74 2e 0a 20 20 20 20 20 20  to abort..      
1ebf7 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 53 65 74  */.      addrSet
1ebf8 41 62 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56  Abort = sqlite3V
1ebf9 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
1ebfa 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1ebfb 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1ebfc 4d 65 6d 49 6e 74 2c 20 31 2c 20 69 41 62 6f 72  MemInt, 1, iAbor
1ebfd 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64  tFlag);.      Vd
1ebfe 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23  beComment((v, "#
1ebff 20 73 65 74 20 61 62 6f 72 74 20 66 6c 61 67 22   set abort flag"
1ec00 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
1ec01 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1ec02 5f 52 65 74 75 72 6e 2c 20 30 2c 20 30 29 3b 0a  _Return, 0, 0);.
1ec03 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75 74        addrOutput
1ec04 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Row = sqlite3Vdb
1ec05 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
1ec06 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ec07 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66  beAddOp(v, OP_If
1ec08 4d 65 6d 50 6f 73 2c 20 69 55 73 65 46 6c 61 67  MemPos, iUseFlag
1ec09 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b  , addrOutputRow+
1ec0a 32 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  2);.      VdbeCo
1ec0b 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 47 72 6f  mment((v, "# Gro
1ec0c 75 70 62 79 20 72 65 73 75 6c 74 20 67 65 6e 65  upby result gene
1ec0d 72 61 74 6f 72 20 65 6e 74 72 79 20 70 6f 69 6e  rator entry poin
1ec0e 74 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  t"));.      sqli
1ec0f 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1ec10 4f 50 5f 52 65 74 75 72 6e 2c 20 30 2c 20 30 29  OP_Return, 0, 0)
1ec11 3b 0a 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65  ;.      finalize
1ec12 41 67 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61  AggFunctions(pPa
1ec13 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b  rse, &sAggInfo);
1ec14 0a 20 20 20 20 20 20 69 66 28 20 70 48 61 76 69  .      if( pHavi
1ec15 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ng ){.        sq
1ec16 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
1ec17 28 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e 67  (pParse, pHaving
1ec18 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b  , addrOutputRow+
1ec19 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1, 1);.      }. 
1ec1a 20 20 20 20 20 72 63 20 3d 20 73 65 6c 65 63 74       rc = select
1ec1b 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65  InnerLoop(pParse
1ec1c 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20  , p, p->pEList, 
1ec1d 30 2c 20 30 2c 20 70 4f 72 64 65 72 42 79 2c 0a  0, 0, pOrderBy,.
1ec1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec1f 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74 69             disti
1ec20 6e 63 74 2c 20 65 44 65 73 74 2c 20 69 50 61 72  nct, eDest, iPar
1ec21 6d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  m, .            
1ec22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
1ec23 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 31 2c 20  ddrOutputRow+1, 
1ec24 61 64 64 72 53 65 74 41 62 6f 72 74 2c 20 61 66  addrSetAbort, af
1ec25 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  f);.      if( rc
1ec26 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
1ec27 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20   select_end;.   
1ec28 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1ec29 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ec2a 50 5f 52 65 74 75 72 6e 2c 20 30 2c 20 30 29 3b  P_Return, 0, 0);
1ec2b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
1ec2c 6e 74 28 28 76 2c 20 22 23 20 65 6e 64 20 67 72  nt((v, "# end gr
1ec2d 6f 75 70 62 79 20 72 65 73 75 6c 74 20 67 65 6e  oupby result gen
1ec2e 65 72 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20  erator"));..    
1ec2f 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
1ec30 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
1ec31 77 69 6c 6c 20 72 65 73 65 74 20 74 68 65 20 67  will reset the g
1ec32 72 6f 75 70 2d 62 79 20 61 63 63 75 6d 75 6c 61  roup-by accumula
1ec33 74 6f 72 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  tor.      */.   
1ec34 20 20 20 61 64 64 72 52 65 73 65 74 20 3d 20 73     addrReset = s
1ec35 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
1ec36 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
1ec37 72 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72  resetAccumulator
1ec38 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
1ec39 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  fo);.      sqlit
1ec3a 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ec3b 50 5f 52 65 74 75 72 6e 2c 20 30 2c 20 30 29 3b  P_Return, 0, 0);
1ec3c 0a 0a 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e  ..      /* Begin
1ec3d 20 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c   a loop that wil
1ec3e 6c 20 65 78 74 72 61 63 74 20 61 6c 6c 20 73 6f  l extract all so
1ec3f 75 72 63 65 20 72 6f 77 73 20 69 6e 20 47 52 4f  urce rows in GRO
1ec40 55 50 20 42 59 20 6f 72 64 65 72 2e 0a 20 20 20  UP BY order..   
1ec41 20 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 74     ** This might
1ec42 20 69 6e 76 6f 6c 76 65 20 74 77 6f 20 73 65 70   involve two sep
1ec43 61 72 61 74 65 20 6c 6f 6f 70 73 20 77 69 74 68  arate loops with
1ec44 20 61 6e 20 4f 50 5f 53 6f 72 74 20 69 6e 20 62   an OP_Sort in b
1ec45 65 74 77 65 65 6e 2c 20 6f 72 0a 20 20 20 20 20  etween, or.     
1ec46 20 2a 2a 20 69 74 20 6d 69 67 68 74 20 62 65 20   ** it might be 
1ec47 61 20 73 69 6e 67 6c 65 20 6c 6f 6f 70 20 74 68  a single loop th
1ec48 61 74 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78  at uses an index
1ec49 20 74 6f 20 65 78 74 72 61 63 74 20 69 6e 66 6f   to extract info
1ec4a 72 6d 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  rmation.      **
1ec4b 20 69 6e 20 74 68 65 20 72 69 67 68 74 20 6f 72   in the right or
1ec4c 64 65 72 20 74 6f 20 62 65 67 69 6e 20 77 69 74  der to begin wit
1ec4d 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  h..      */.    
1ec4e 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
1ec4f 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
1ec50 72 49 6e 69 74 69 61 6c 69 7a 65 4c 6f 6f 70 29  rInitializeLoop)
1ec51 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1ec52 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47  dbeAddOp(v, OP_G
1ec53 6f 73 75 62 2c 20 30 2c 20 61 64 64 72 52 65 73  osub, 0, addrRes
1ec54 65 74 29 3b 0a 20 20 20 20 20 20 70 57 49 6e 66  et);.      pWInf
1ec55 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65  o = sqlite3Where
1ec56 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54  Begin(pParse, pT
1ec57 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20  abList, pWhere, 
1ec58 26 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20  &pGroupBy);.    
1ec59 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20    if( pWInfo==0 
1ec5a 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e  ) goto select_en
1ec5b 64 3b 0a 20 20 20 20 20 20 69 66 28 20 70 47 72  d;.      if( pGr
1ec5c 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20  oupBy==0 ){.    
1ec5d 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 74 69 6d      /* The optim
1ec5e 69 7a 65 72 20 69 73 20 61 62 6c 65 20 74 6f 20  izer is able to 
1ec5f 64 65 6c 69 76 65 72 20 72 6f 77 73 20 69 6e 20  deliver rows in 
1ec60 67 72 6f 75 70 20 62 79 20 6f 72 64 65 72 20 73  group by order s
1ec61 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 20  o.        ** we 
1ec62 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 73  do not have to s
1ec63 6f 72 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70 65  ort.  The OP_Ope
1ec64 6e 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65  nEphemeral table
1ec65 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20   will be.       
1ec66 20 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c 61   ** cancelled la
1ec67 74 65 72 20 62 65 63 61 75 73 65 20 77 65 20 73  ter because we s
1ec68 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73 65  till need to use
1ec69 20 74 68 65 20 70 4b 65 79 49 6e 66 6f 0a 20 20   the pKeyInfo.  
1ec6a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1ec6b 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70   pGroupBy = p->p
1ec6c 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20 20  GroupBy;.       
1ec6d 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 30   groupBySort = 0
1ec6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1ec6f 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 73 20 61         /* Rows a
1ec70 72 65 20 63 6f 6d 69 6e 67 20 6f 75 74 20 69 6e  re coming out in
1ec71 20 75 6e 64 65 74 65 72 6d 69 6e 65 64 20 6f 72   undetermined or
1ec72 64 65 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f  der.  We have to
1ec73 20 70 75 73 68 0a 20 20 20 20 20 20 20 20 2a 2a   push.        **
1ec74 20 65 61 63 68 20 72 6f 77 20 69 6e 74 6f 20 61   each row into a
1ec75 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 20   sorting index, 
1ec76 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 66 69  terminate the fi
1ec77 72 73 74 20 6c 6f 6f 70 2c 0a 20 20 20 20 20 20  rst loop,.      
1ec78 20 20 2a 2a 20 74 68 65 6e 20 6c 6f 6f 70 20 6f    ** then loop o
1ec79 76 65 72 20 74 68 65 20 73 6f 72 74 69 6e 67 20  ver the sorting 
1ec7a 69 6e 64 65 78 20 69 6e 20 6f 72 64 65 72 20 74  index in order t
1ec7b 6f 20 67 65 74 20 74 68 65 20 6f 75 74 70 75 74  o get the output
1ec7c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 73  .        ** in s
1ec7d 6f 72 74 65 64 20 6f 72 64 65 72 0a 20 20 20 20  orted order.    
1ec7e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 67      */.        g
1ec7f 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 31 3b 0a  roupBySort = 1;.
1ec80 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1ec81 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
1ec82 70 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79  pParse, pGroupBy
1ec83 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1ec84 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ec85 50 5f 53 65 71 75 65 6e 63 65 2c 20 73 41 67 67  P_Sequence, sAgg
1ec86 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c  Info.sortingIdx,
1ec87 20 30 29 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d   0);.        j =
1ec88 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
1ec89 2b 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  +1;.        for(
1ec8a 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e  i=0; i<sAggInfo.
1ec8b 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
1ec8c 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20           struct 
1ec8d 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f  AggInfo_col *pCo
1ec8e 6c 20 3d 20 26 73 41 67 67 49 6e 66 6f 2e 61 43  l = &sAggInfo.aC
1ec8f 6f 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20  ol[i];.         
1ec90 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74   if( pCol->iSort
1ec91 65 72 43 6f 6c 75 6d 6e 3c 6a 20 29 20 63 6f 6e  erColumn<j ) con
1ec92 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20  tinue;.         
1ec93 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1ec94 47 65 74 43 6f 6c 75 6d 6e 28 76 2c 20 70 43 6f  GetColumn(v, pCo
1ec95 6c 2d 3e 70 54 61 62 2c 20 70 43 6f 6c 2d 3e 69  l->pTab, pCol->i
1ec96 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 2d 3e 69 54  Column, pCol->iT
1ec97 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 20  able);.         
1ec98 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a   j++;.        }.
1ec99 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1ec9a 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
1ec9b 61 6b 65 52 65 63 6f 72 64 2c 20 6a 2c 20 30 29  akeRecord, j, 0)
1ec9c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1ec9d 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1ec9e 5f 49 64 78 49 6e 73 65 72 74 2c 20 73 41 67 67  _IdxInsert, sAgg
1ec9f 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c  Info.sortingIdx,
1eca0 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
1eca1 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49  ite3WhereEnd(pWI
1eca2 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 71  nfo);.        sq
1eca3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1eca4 2c 20 4f 50 5f 53 6f 72 74 2c 20 73 41 67 67 49  , OP_Sort, sAggI
1eca5 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20  nfo.sortingIdx, 
1eca6 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20  addrEnd);.      
1eca7 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
1eca8 2c 20 22 23 20 47 52 4f 55 50 20 42 59 20 73 6f  , "# GROUP BY so
1eca9 72 74 22 29 29 3b 0a 20 20 20 20 20 20 20 20 73  rt"));.        s
1ecaa 41 67 67 49 6e 66 6f 2e 75 73 65 53 6f 72 74 69  AggInfo.useSorti
1ecab 6e 67 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 20  ngIdx = 1;.     
1ecac 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 45 76 61   }..      /* Eva
1ecad 6c 75 61 74 65 20 74 68 65 20 63 75 72 72 65 6e  luate the curren
1ecae 74 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73  t GROUP BY terms
1ecaf 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 62 30   and store in b0
1ecb0 2c 20 62 31 2c 20 62 32 2e 2e 2e 0a 20 20 20 20  , b1, b2....    
1ecb1 20 20 2a 2a 20 28 62 30 20 69 73 20 6d 65 6d 6f    ** (b0 is memo
1ecb2 72 79 20 6c 6f 63 61 74 69 6f 6e 20 69 42 4d 65  ry location iBMe
1ecb3 6d 2b 30 2c 20 62 31 20 69 73 20 69 42 4d 65 6d  m+0, b1 is iBMem
1ecb4 2b 31 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  +1, and so forth
1ecb5 29 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20  ).      ** Then 
1ecb6 63 6f 6d 70 61 72 65 20 74 68 65 20 63 75 72 72  compare the curr
1ecb7 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74 65 72  ent GROUP BY ter
1ecb8 6d 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 47  ms against the G
1ecb9 52 4f 55 50 20 42 59 20 74 65 72 6d 73 0a 20 20  ROUP BY terms.  
1ecba 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20      ** from the 
1ecbb 70 72 65 76 69 6f 75 73 20 72 6f 77 20 63 75 72  previous row cur
1ecbc 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e  rently stored in
1ecbd 20 61 30 2c 20 61 31 2c 20 61 32 2e 2e 2e 0a 20   a0, a1, a2.... 
1ecbe 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64       */.      ad
1ecbf 64 72 54 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71  drTopOfLoop = sq
1ecc0 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
1ecc1 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66  Addr(v);.      f
1ecc2 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f 75 70  or(j=0; j<pGroup
1ecc3 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b  By->nExpr; j++){
1ecc4 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 72 6f  .        if( gro
1ecc5 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20  upBySort ){.    
1ecc6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ecc7 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c  eAddOp(v, OP_Col
1ecc8 75 6d 6e 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f  umn, sAggInfo.so
1ecc9 72 74 69 6e 67 49 64 78 2c 20 6a 29 3b 0a 20 20  rtingIdx, j);.  
1ecca 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1eccb 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e         sAggInfo.
1eccc 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a  directMode = 1;.
1eccd 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1ecce 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
1eccf 2c 20 70 47 72 6f 75 70 42 79 2d 3e 61 5b 6a 5d  , pGroupBy->a[j]
1ecd0 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20  .pExpr);.       
1ecd1 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
1ecd2 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ecd3 50 5f 4d 65 6d 53 74 6f 72 65 2c 20 69 42 4d 65  P_MemStore, iBMe
1ecd4 6d 2b 6a 2c 20 6a 3c 70 47 72 6f 75 70 42 79 2d  m+j, j<pGroupBy-
1ecd5 3e 6e 45 78 70 72 2d 31 29 3b 0a 20 20 20 20 20  >nExpr-1);.     
1ecd6 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70   }.      for(j=p
1ecd7 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2d 31  GroupBy->nExpr-1
1ecd8 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 29 7b 0a 20 20  ; j>=0; j--){.  
1ecd9 20 20 20 20 20 20 69 66 28 20 6a 3c 70 47 72 6f        if( j<pGro
1ecda 75 70 42 79 2d 3e 6e 45 78 70 72 2d 31 20 29 7b  upBy->nExpr-1 ){
1ecdb 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1ecdc 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ecdd 50 5f 4d 65 6d 4c 6f 61 64 2c 20 69 42 4d 65 6d  P_MemLoad, iBMem
1ecde 2b 6a 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  +j, 0);.        
1ecdf 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
1ece0 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1ece1 5f 4d 65 6d 4c 6f 61 64 2c 20 69 41 4d 65 6d 2b  _MemLoad, iAMem+
1ece2 6a 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  j, 0);.        i
1ece3 66 28 20 6a 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( j==0 ){.     
1ece4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ece5 41 64 64 4f 70 28 76 2c 20 4f 50 5f 45 71 2c 20  AddOp(v, OP_Eq, 
1ece6 30 78 32 30 30 2c 20 61 64 64 72 50 72 6f 63 65  0x200, addrProce
1ece7 73 73 52 6f 77 29 3b 0a 20 20 20 20 20 20 20 20  ssRow);.        
1ece8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1ece9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ecea 70 28 76 2c 20 4f 50 5f 4e 65 2c 20 30 78 32 30  p(v, OP_Ne, 0x20
1eceb 30 2c 20 61 64 64 72 47 72 6f 75 70 42 79 43 68  0, addrGroupByCh
1ecec 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  ange);.        }
1eced 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ecee 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20  VdbeChangeP3(v, 
1ecef 2d 31 2c 20 28 76 6f 69 64 2a 29 70 4b 65 79 49  -1, (void*)pKeyI
1ecf0 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 2c 20 50  nfo->aColl[j], P
1ecf1 33 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20  3_COLLSEQ);.    
1ecf2 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65    }..      /* Ge
1ecf3 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
1ecf4 20 72 75 6e 73 20 77 68 65 6e 65 76 65 72 20 74   runs whenever t
1ecf5 68 65 20 47 52 4f 55 50 20 42 59 20 63 68 61 6e  he GROUP BY chan
1ecf6 67 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20 43 68  ges..      ** Ch
1ecf7 61 6e 67 65 20 69 6e 20 74 68 65 20 47 52 4f 55  ange in the GROU
1ecf8 50 20 42 59 20 61 72 65 20 64 65 74 65 63 74 65  P BY are detecte
1ecf9 64 20 62 79 20 74 68 65 20 70 72 65 76 69 6f 75  d by the previou
1ecfa 73 20 63 6f 64 65 0a 20 20 20 20 20 20 2a 2a 20  s code.      ** 
1ecfb 62 6c 6f 63 6b 2e 20 20 49 66 20 74 68 65 72 65  block.  If there
1ecfc 20 77 65 72 65 20 6e 6f 20 63 68 61 6e 67 65 73   were no changes
1ecfd 2c 20 74 68 69 73 20 62 6c 6f 63 6b 20 69 73 20  , this block is 
1ecfe 73 6b 69 70 70 65 64 2e 0a 20 20 20 20 20 20 2a  skipped..      *
1ecff 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20  *.      ** This 
1ed00 63 6f 64 65 20 63 6f 70 69 65 73 20 63 75 72 72  code copies curr
1ed01 65 6e 74 20 67 72 6f 75 70 20 62 79 20 74 65 72  ent group by ter
1ed02 6d 73 20 69 6e 20 62 30 2c 62 31 2c 62 32 2c 2e  ms in b0,b1,b2,.
1ed03 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72  ...      ** over
1ed04 20 74 6f 20 61 30 2c 61 31 2c 61 32 2e 20 20 49   to a0,a1,a2.  I
1ed05 74 20 74 68 65 6e 20 63 61 6c 6c 73 20 74 68 65  t then calls the
1ed06 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69   output subrouti
1ed07 6e 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  ne.      ** and 
1ed08 72 65 73 65 74 73 20 74 68 65 20 61 67 67 72 65  resets the aggre
1ed09 67 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72  gate accumulator
1ed0a 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 70 72   registers in pr
1ed0b 65 70 61 72 61 74 69 6f 6e 0a 20 20 20 20 20 20  eparation.      
1ed0c 2a 2a 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20  ** for the next 
1ed0d 47 52 4f 55 50 20 42 59 20 62 61 74 63 68 2e 0a  GROUP BY batch..
1ed0e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1ed0f 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
1ed10 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 47 72  eLabel(v, addrGr
1ed11 6f 75 70 42 79 43 68 61 6e 67 65 29 3b 0a 20 20  oupByChange);.  
1ed12 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
1ed13 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20  GroupBy->nExpr; 
1ed14 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 73 71  j++){.        sq
1ed15 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ed16 2c 20 4f 50 5f 4d 65 6d 4d 6f 76 65 2c 20 69 41  , OP_MemMove, iA
1ed17 4d 65 6d 2b 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b  Mem+j, iBMem+j);
1ed18 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1ed19 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1ed1a 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 30 2c 20  v, OP_Gosub, 0, 
1ed1b 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b 0a  addrOutputRow);.
1ed1c 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
1ed1d 74 28 28 76 2c 20 22 23 20 6f 75 74 70 75 74 20  t((v, "# output 
1ed1e 6f 6e 65 20 72 6f 77 22 29 29 3b 0a 20 20 20 20  one row"));.    
1ed1f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ed20 4f 70 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f  Op(v, OP_IfMemPo
1ed21 73 2c 20 69 41 62 6f 72 74 46 6c 61 67 2c 20 61  s, iAbortFlag, a
1ed22 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 56  ddrEnd);.      V
1ed23 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
1ed24 23 20 63 68 65 63 6b 20 61 62 6f 72 74 20 66 6c  # check abort fl
1ed25 61 67 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  ag"));.      sql
1ed26 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1ed27 20 4f 50 5f 47 6f 73 75 62 2c 20 30 2c 20 61 64   OP_Gosub, 0, ad
1ed28 64 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20 20  drReset);.      
1ed29 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
1ed2a 22 23 20 72 65 73 65 74 20 61 63 63 75 6d 75 6c  "# reset accumul
1ed2b 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20 20  ator"));..      
1ed2c 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 67  /* Update the ag
1ed2d 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c 61  gregate accumula
1ed2e 74 6f 72 73 20 62 61 73 65 64 20 6f 6e 20 74 68  tors based on th
1ed2f 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 20 20 20  e content of.   
1ed30 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e     ** the curren
1ed31 74 20 72 6f 77 0a 20 20 20 20 20 20 2a 2f 0a 20  t row.      */. 
1ed32 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ed33 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
1ed34 61 64 64 72 50 72 6f 63 65 73 73 52 6f 77 29 3b  addrProcessRow);
1ed35 0a 20 20 20 20 20 20 75 70 64 61 74 65 41 63 63  .      updateAcc
1ed36 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65 2c  umulator(pParse,
1ed37 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20   &sAggInfo);.   
1ed38 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ed39 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74  dOp(v, OP_MemInt
1ed3a 2c 20 31 2c 20 69 55 73 65 46 6c 61 67 29 3b 0a  , 1, iUseFlag);.
1ed3b 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
1ed3c 74 28 28 76 2c 20 22 23 20 69 6e 64 69 63 61 74  t((v, "# indicat
1ed3d 65 20 64 61 74 61 20 69 6e 20 61 63 63 75 6d 75  e data in accumu
1ed3e 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20  lator"));..     
1ed3f 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6c   /* End of the l
1ed40 6f 6f 70 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  oop.      */.   
1ed41 20 20 20 69 66 28 20 67 72 6f 75 70 42 79 53 6f     if( groupBySo
1ed42 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  rt ){.        sq
1ed43 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ed44 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 41 67 67 49  , OP_Next, sAggI
1ed45 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20  nfo.sortingIdx, 
1ed46 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 29 3b 0a  addrTopOfLoop);.
1ed47 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1ed48 20 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72       sqlite3Wher
1ed49 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20  eEnd(pWInfo);.  
1ed4a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ed4b 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c  eChangeToNoop(v,
1ed4c 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64 78 2c   addrSortingIdx,
1ed4d 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   1);.      }..  
1ed4e 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 74 68      /* Output th
1ed4f 65 20 66 69 6e 61 6c 20 72 6f 77 20 6f 66 20 72  e final row of r
1ed50 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2f 0a 20  esult.      */. 
1ed51 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ed52 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 73 75  AddOp(v, OP_Gosu
1ed53 62 2c 20 30 2c 20 61 64 64 72 4f 75 74 70 75 74  b, 0, addrOutput
1ed54 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65  Row);.      Vdbe
1ed55 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 6f  Comment((v, "# o
1ed56 75 74 70 75 74 20 66 69 6e 61 6c 20 72 6f 77 22  utput final row"
1ed57 29 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 7d  ));.      .    }
1ed58 20 2f 2a 20 65 6e 64 69 66 20 70 47 72 6f 75 70   /* endif pGroup
1ed59 42 79 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 7b  By */.    else {
1ed5a 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63  .      /* This c
1ed5b 61 73 65 20 72 75 6e 73 20 69 66 20 74 68 65 20  ase runs if the 
1ed5c 61 67 67 72 65 67 61 74 65 20 68 61 73 20 6e 6f  aggregate has no
1ed5d 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1ed5e 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20  .  The.      ** 
1ed5f 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 6d 75  processing is mu
1ed60 63 68 20 73 69 6d 70 6c 65 72 20 73 69 6e 63 65  ch simpler since
1ed61 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61   there is only a
1ed62 20 73 69 6e 67 6c 65 20 72 6f 77 0a 20 20 20 20   single row.    
1ed63 20 20 2a 2a 20 6f 66 20 6f 75 74 70 75 74 2e 0a    ** of output..
1ed64 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
1ed65 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28  esetAccumulator(
1ed66 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66  pParse, &sAggInf
1ed67 6f 29 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f  o);.      pWInfo
1ed68 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42   = sqlite3WhereB
1ed69 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61  egin(pParse, pTa
1ed6a 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30  bList, pWhere, 0
1ed6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 57 49  );.      if( pWI
1ed6c 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65  nfo==0 ) goto se
1ed6d 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20  lect_end;.      
1ed6e 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f  updateAccumulato
1ed6f 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49  r(pParse, &sAggI
1ed70 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  nfo);.      sqli
1ed71 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
1ed72 66 6f 29 3b 0a 20 20 20 20 20 20 66 69 6e 61 6c  fo);.      final
1ed73 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28  izeAggFunctions(
1ed74 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66  pParse, &sAggInf
1ed75 6f 29 3b 0a 20 20 20 20 20 20 70 4f 72 64 65 72  o);.      pOrder
1ed76 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  By = 0;.      if
1ed77 28 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20  ( pHaving ){.   
1ed78 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1ed79 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
1ed7a 70 48 61 76 69 6e 67 2c 20 61 64 64 72 45 6e 64  pHaving, addrEnd
1ed7b 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 1);.      }.  
1ed7c 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
1ed7d 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
1ed7e 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20  ->pEList, 0, 0, 
1ed7f 30 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20  0, -1, .        
1ed80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 44                eD
1ed81 65 73 74 2c 20 69 50 61 72 6d 2c 20 61 64 64 72  est, iParm, addr
1ed82 45 6e 64 2c 20 61 64 64 72 45 6e 64 2c 20 61 66  End, addrEnd, af
1ed83 66 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  f);.    }.    sq
1ed84 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
1ed85 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 45 6e 64  Label(v, addrEnd
1ed86 29 3b 0a 20 20 20 20 0a 20 20 7d 20 2f 2a 20 65  );.    .  } /* e
1ed87 6e 64 69 66 20 61 67 67 72 65 67 61 74 65 20 71  ndif aggregate q
1ed88 75 65 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  uery */..  /* If
1ed89 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44   there is an ORD
1ed8a 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68  ER BY clause, th
1ed8b 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 73 6f  en we need to so
1ed8c 72 74 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20  rt the results. 
1ed8d 20 2a 2a 20 61 6e 64 20 73 65 6e 64 20 74 68 65   ** and send the
1ed8e 6d 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  m to the callbac
1ed8f 6b 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20  k one by one..  
1ed90 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  */.  if( pOrderB
1ed91 79 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74  y ){.    generat
1ed92 65 53 6f 72 74 54 61 69 6c 28 70 50 61 72 73 65  eSortTail(pParse
1ed93 2c 20 70 2c 20 76 2c 20 70 45 4c 69 73 74 2d 3e  , p, v, pEList->
1ed94 6e 45 78 70 72 2c 20 65 44 65 73 74 2c 20 69 50  nExpr, eDest, iP
1ed95 61 72 6d 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  arm);.  }..#ifnd
1ed96 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
1ed97 55 42 51 55 45 52 59 0a 20 20 2f 2a 20 49 66 20  UBQUERY.  /* If 
1ed98 74 68 69 73 20 77 61 73 20 61 20 73 75 62 71 75  this was a subqu
1ed99 65 72 79 2c 20 77 65 20 68 61 76 65 20 6e 6f 77  ery, we have now
1ed9a 20 63 6f 6e 76 65 72 74 65 64 20 74 68 65 20 73   converted the s
1ed9b 75 62 71 75 65 72 79 20 69 6e 74 6f 20 61 0a 20  ubquery into a. 
1ed9c 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61   ** temporary ta
1ed9d 62 6c 65 2e 20 20 53 6f 20 73 65 74 20 74 68 65  ble.  So set the
1ed9e 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 2e 69 73   SrcList_item.is
1ed9f 50 6f 70 75 6c 61 74 65 64 20 66 6c 61 67 20 74  Populated flag t
1eda0 6f 20 70 72 65 76 65 6e 74 0a 20 20 2a 2a 20 74  o prevent.  ** t
1eda1 68 69 73 20 73 75 62 71 75 65 72 79 20 66 72 6f  his subquery fro
1eda2 6d 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65  m being evaluate
1eda3 64 20 61 67 61 69 6e 20 61 6e 64 20 74 6f 20 66  d again and to f
1eda4 6f 72 63 65 20 74 68 65 20 75 73 65 20 6f 66 0a  orce the use of.
1eda5 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 6f 72 61    ** the tempora
1eda6 72 79 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20  ry table..  */. 
1eda7 20 69 66 28 20 70 50 61 72 65 6e 74 20 29 7b 0a   if( pParent ){.
1eda8 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
1eda9 65 6e 74 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3e  ent->pSrc->nSrc>
1edaa 70 61 72 65 6e 74 54 61 62 20 29 3b 0a 20 20 20  parentTab );.   
1edab 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
1edac 2d 3e 70 53 72 63 2d 3e 61 5b 70 61 72 65 6e 74  ->pSrc->a[parent
1edad 54 61 62 5d 2e 70 53 65 6c 65 63 74 3d 3d 70 20  Tab].pSelect==p 
1edae 29 3b 0a 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  );.    pParent->
1edaf 70 53 72 63 2d 3e 61 5b 70 61 72 65 6e 74 54 61  pSrc->a[parentTa
1edb0 62 5d 2e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d  b].isPopulated =
1edb1 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a   1;.  }.#endif..
1edb2 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
1edb3 6f 20 73 6b 69 70 20 74 68 69 73 20 71 75 65 72  o skip this quer
1edb4 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  y.  */.  sqlite3
1edb5 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
1edb6 28 76 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a  (v, iEnd);..  /*
1edb7 20 54 68 65 20 53 45 4c 45 43 54 20 77 61 73 20   The SELECT was 
1edb8 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 64  successfully cod
1edb9 65 64 2e 20 20 20 53 65 74 20 74 68 65 20 72 65  ed.   Set the re
1edba 74 75 72 6e 20 63 6f 64 65 20 74 6f 20 30 0a 20  turn code to 0. 
1edbb 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20   ** to indicate 
1edbc 6e 6f 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a  no errors..  */.
1edbd 20 20 72 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20    rc = 0;..  /* 
1edbe 43 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 20 74 6f  Control jumps to
1edbf 20 68 65 72 65 20 69 66 20 61 6e 20 65 72 72 6f   here if an erro
1edc0 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
1edc1 20 61 62 6f 76 65 2c 20 6f 72 20 75 70 6f 6e 0a   above, or upon.
1edc2 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20    ** successful 
1edc3 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 53 45  coding of the SE
1edc4 4c 45 43 54 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63  LECT..  */.selec
1edc5 74 5f 65 6e 64 3a 0a 0a 20 20 2f 2a 20 49 64 65  t_end:..  /* Ide
1edc6 6e 74 69 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ntify column nam
1edc7 65 73 20 69 66 20 77 65 20 77 69 6c 6c 20 62 65  es if we will be
1edc8 20 75 73 69 6e 67 20 74 68 65 6d 20 69 6e 20 61   using them in a
1edc9 20 63 61 6c 6c 62 61 63 6b 2e 20 20 54 68 69 73   callback.  This
1edca 0a 20 20 2a 2a 20 73 74 65 70 20 69 73 20 73 6b  .  ** step is sk
1edcb 69 70 70 65 64 20 69 66 20 74 68 65 20 6f 75 74  ipped if the out
1edcc 70 75 74 20 69 73 20 67 6f 69 6e 67 20 74 6f 20  put is going to 
1edcd 73 6f 6d 65 20 6f 74 68 65 72 20 64 65 73 74 69  some other desti
1edce 6e 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69  nation..  */.  i
1edcf 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1edd0 20 26 26 20 65 44 65 73 74 3d 3d 53 52 54 5f 43   && eDest==SRT_C
1edd1 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 67  allback ){.    g
1edd2 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d  enerateColumnNam
1edd3 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  es(pParse, pTabL
1edd4 69 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ist, pEList);.  
1edd5 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  }..  sqlite3_fre
1edd6 65 28 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29  e(sAggInfo.aCol)
1edd7 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
1edd8 28 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 29  (sAggInfo.aFunc)
1edd9 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1edda 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
1eddb 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
1eddc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1eddd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1edde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1eddf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ede0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1ede1 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1ede2 63 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f 72  code is used for
1ede3 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62   testing and deb
1ede4 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68  ugging only.  Th
1ede5 65 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 66  e code.** that f
1ede6 6f 6c 6c 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20  ollows does not 
1ede7 61 70 70 65 61 72 20 69 6e 20 6e 6f 72 6d 61 6c  appear in normal
1ede8 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54   builds..**.** T
1ede9 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
1edea 65 20 75 73 65 64 20 74 6f 20 70 72 69 6e 74 20  e used to print 
1edeb 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  out the content 
1edec 6f 66 20 61 6c 6c 20 6f 72 20 70 61 72 74 20 6f  of all or part o
1eded 66 20 61 20 0a 2a 2a 20 70 61 72 73 65 20 73 74  f a .** parse st
1edee 72 75 63 74 75 72 65 73 20 73 75 63 68 20 61 73  ructures such as
1edef 20 53 65 6c 65 63 74 20 6f 72 20 45 78 70 72 2e   Select or Expr.
1edf0 20 20 53 75 63 68 20 70 72 69 6e 74 6f 75 74 73    Such printouts
1edf1 20 61 72 65 20 75 73 65 66 75 6c 0a 2a 2a 20 66   are useful.** f
1edf2 6f 72 20 68 65 6c 70 69 6e 67 20 74 6f 20 75 6e  or helping to un
1edf3 64 65 72 73 74 61 6e 64 20 77 68 61 74 20 69 73  derstand what is
1edf4 20 68 61 70 70 65 6e 69 6e 67 20 69 6e 73 69 64   happening insid
1edf5 65 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  e the code gener
1edf6 61 74 6f 72 0a 2a 2a 20 64 75 72 69 6e 67 20 74  ator.** during t
1edf7 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  he execution of 
1edf8 63 6f 6d 70 6c 65 78 20 53 45 4c 45 43 54 20 73  complex SELECT s
1edf9 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
1edfa 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 61   These routine a
1edfb 72 65 20 6e 6f 74 20 63 61 6c 6c 65 64 20 61 6e  re not called an
1edfc 79 77 68 65 72 65 20 66 72 6f 6d 20 77 69 74 68  ywhere from with
1edfd 69 6e 20 74 68 65 20 6e 6f 72 6d 61 6c 0a 2a 2a  in the normal.**
1edfe 20 63 6f 64 65 20 62 61 73 65 2e 20 20 54 68 65   code base.  The
1edff 6e 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74  n are intended t
1ee00 6f 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  o be called from
1ee01 20 77 69 74 68 69 6e 20 74 68 65 20 64 65 62 75   within the debu
1ee02 67 67 65 72 0a 2a 2a 20 6f 72 20 66 72 6f 6d 20  gger.** or from 
1ee03 74 65 6d 70 6f 72 61 72 79 20 22 70 72 69 6e 74  temporary "print
1ee04 66 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  f" statements in
1ee05 73 65 72 74 65 64 20 66 6f 72 20 64 65 62 75 67  serted for debug
1ee06 67 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ging..*/.SQLITE_
1ee07 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1ee08 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 45 78  ite3PrintExpr(Ex
1ee09 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  pr *p){.  if( p-
1ee0a 3e 74 6f 6b 65 6e 2e 7a 20 26 26 20 70 2d 3e 74  >token.z && p->t
1ee0b 6f 6b 65 6e 2e 6e 3e 30 20 29 7b 0a 20 20 20 20  oken.n>0 ){.    
1ee0c 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
1ee0d 74 66 28 22 28 25 2e 2a 73 22 2c 20 70 2d 3e 74  tf("(%.*s", p->t
1ee0e 6f 6b 65 6e 2e 6e 2c 20 70 2d 3e 74 6f 6b 65 6e  oken.n, p->token
1ee0f 2e 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  .z);.  }else{.  
1ee10 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
1ee11 69 6e 74 66 28 22 28 25 64 22 2c 20 70 2d 3e 6f  intf("(%d", p->o
1ee12 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  p);.  }.  if( p-
1ee13 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 71  >pLeft ){.    sq
1ee14 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
1ee15 28 22 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  (" ");.    sqlit
1ee16 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70  e3PrintExpr(p->p
1ee17 4c 65 66 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Left);.  }.  if(
1ee18 20 70 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20   p->pRight ){.  
1ee19 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
1ee1a 69 6e 74 66 28 22 20 22 29 3b 0a 20 20 20 20 73  intf(" ");.    s
1ee1b 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28  qlite3PrintExpr(
1ee1c 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a  p->pRight);.  }.
1ee1d 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
1ee1e 69 6e 74 66 28 22 29 22 29 3b 0a 7d 0a 53 51 4c  intf(")");.}.SQL
1ee1f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1ee20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70   sqlite3PrintExp
1ee21 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a  rList(ExprList *
1ee22 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
1ee23 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c  .  for(i=0; i<pL
1ee24 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
1ee25 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69  {.    sqlite3Pri
1ee26 6e 74 45 78 70 72 28 70 4c 69 73 74 2d 3e 61 5b  ntExpr(pList->a[
1ee27 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 69  i].pExpr);.    i
1ee28 66 28 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  f( i<pList->nExp
1ee29 72 2d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  r-1 ){.      sql
1ee2a 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
1ee2b 22 2c 20 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ", ");.    }.  }
1ee2c 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
1ee2d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
1ee2e 69 6e 74 53 65 6c 65 63 74 28 53 65 6c 65 63 74  intSelect(Select
1ee2f 20 2a 70 2c 20 69 6e 74 20 69 6e 64 65 6e 74 29   *p, int indent)
1ee30 7b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67  {.  sqlite3Debug
1ee31 50 72 69 6e 74 66 28 22 25 2a 73 53 45 4c 45 43  Printf("%*sSELEC
1ee32 54 28 25 70 29 20 22 2c 20 69 6e 64 65 6e 74 2c  T(%p) ", indent,
1ee33 20 22 22 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74   "", p);.  sqlit
1ee34 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28  e3PrintExprList(
1ee35 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73 71  p->pEList);.  sq
1ee36 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
1ee37 28 22 5c 6e 22 29 3b 0a 20 20 69 66 28 20 70 2d  ("\n");.  if( p-
1ee38 3e 70 53 72 63 20 29 7b 0a 20 20 20 20 63 68 61  >pSrc ){.    cha
1ee39 72 20 2a 7a 50 72 65 66 69 78 3b 0a 20 20 20 20  r *zPrefix;.    
1ee3a 69 6e 74 20 69 3b 0a 20 20 20 20 7a 50 72 65 66  int i;.    zPref
1ee3b 69 78 20 3d 20 22 46 52 4f 4d 22 3b 0a 20 20 20  ix = "FROM";.   
1ee3c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70   for(i=0; i<p->p
1ee3d 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  Src->nSrc; i++){
1ee3e 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72  .      struct Sr
1ee3f 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
1ee40 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b  m = &p->pSrc->a[
1ee41 69 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  i];.      sqlite
1ee42 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a  3DebugPrintf("%*
1ee43 73 20 22 2c 20 69 6e 64 65 6e 74 2b 36 2c 20 7a  s ", indent+6, z
1ee44 50 72 65 66 69 78 29 3b 0a 20 20 20 20 20 20 7a  Prefix);.      z
1ee45 50 72 65 66 69 78 20 3d 20 22 22 3b 0a 20 20 20  Prefix = "";.   
1ee46 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53     if( pItem->pS
1ee47 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
1ee48 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
1ee49 6e 74 66 28 22 28 5c 6e 22 29 3b 0a 20 20 20 20  ntf("(\n");.    
1ee4a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
1ee4b 53 65 6c 65 63 74 28 70 49 74 65 6d 2d 3e 70 53  Select(pItem->pS
1ee4c 65 6c 65 63 74 2c 20 69 6e 64 65 6e 74 2b 31 30  elect, indent+10
1ee4d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1ee4e 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
1ee4f 2a 73 29 22 2c 20 69 6e 64 65 6e 74 2b 38 2c 20  *s)", indent+8, 
1ee50 22 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  "");.      }else
1ee51 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d   if( pItem->zNam
1ee52 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
1ee53 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
1ee54 22 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61  "%s", pItem->zNa
1ee55 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  me);.      }.   
1ee56 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 54     if( pItem->pT
1ee57 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ab ){.        sq
1ee58 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
1ee59 28 22 28 74 61 62 6c 65 3a 20 25 73 29 22 2c 20  ("(table: %s)", 
1ee5a 70 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61  pItem->pTab->zNa
1ee5b 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  me);.      }.   
1ee5c 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41     if( pItem->zA
1ee5d 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20  lias ){.        
1ee5e 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
1ee5f 74 66 28 22 20 41 53 20 25 73 22 2c 20 70 49 74  tf(" AS %s", pIt
1ee60 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20  em->zAlias);.   
1ee61 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
1ee62 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 2d 31  <p->pSrc->nSrc-1
1ee63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1ee64 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
1ee65 2c 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ,");.      }.   
1ee66 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
1ee67 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 20  rintf("\n");.   
1ee68 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e   }.  }.  if( p->
1ee69 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 73 71  pWhere ){.    sq
1ee6a 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
1ee6b 28 22 25 2a 73 20 57 48 45 52 45 20 22 2c 20 69  ("%*s WHERE ", i
1ee6c 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20  ndent, "");.    
1ee6d 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
1ee6e 28 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20  (p->pWhere);.   
1ee6f 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
1ee70 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20  ntf("\n");.  }. 
1ee71 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79   if( p->pGroupBy
1ee72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
1ee73 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20  ebugPrintf("%*s 
1ee74 47 52 4f 55 50 20 42 59 20 22 2c 20 69 6e 64 65  GROUP BY ", inde
1ee75 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c  nt, "");.    sql
1ee76 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73  ite3PrintExprLis
1ee77 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a  t(p->pGroupBy);.
1ee78 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
1ee79 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20  Printf("\n");.  
1ee7a 7d 0a 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69  }.  if( p->pHavi
1ee7b 6e 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ng ){.    sqlite
1ee7c 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a  3DebugPrintf("%*
1ee7d 73 20 48 41 56 49 4e 47 20 22 2c 20 69 6e 64 65  s HAVING ", inde
1ee7e 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c  nt, "");.    sql
1ee7f 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d  ite3PrintExpr(p-
1ee80 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 73  >pHaving);.    s
1ee81 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
1ee82 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69  f("\n");.  }.  i
1ee83 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29  f( p->pOrderBy )
1ee84 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
1ee85 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 4f 52  ugPrintf("%*s OR
1ee86 44 45 52 20 42 59 20 22 2c 20 69 6e 64 65 6e 74  DER BY ", indent
1ee87 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  , "");.    sqlit
1ee88 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28  e3PrintExprList(
1ee89 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  p->pOrderBy);.  
1ee8a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
1ee8b 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a  intf("\n");.  }.
1ee8c 7d 0a 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  }./* End of the 
1ee8d 73 74 72 75 63 74 75 72 65 20 64 65 62 75 67 20  structure debug 
1ee8e 70 72 69 6e 74 69 6e 67 20 63 6f 64 65 0a 2a 2a  printing code.**
1ee8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ee90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ee91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ee92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ee93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e  ***********/.#en
1ee94 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
1ee95 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64  QLITE_TEST) || d
1ee96 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
1ee97 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  BUG) */../******
1ee98 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1ee99 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  select.c *******
1ee9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ee9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ee9c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1ee9d 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1ee9e 69 6c 65 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a  ile table.c ****
1ee9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1eea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1eea1 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1eea2 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
1eea3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1eea4 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1eea5 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1eea6 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1eea7 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1eea8 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1eea9 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1eeaa 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1eeab 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1eeac 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1eead 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1eeae 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1eeaf 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1eeb0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1eeb1 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1eeb2 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1eeb3 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1eeb4 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1eeb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1eeb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1eeb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1eeb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1eeb9 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1eeba 74 61 69 6e 73 20 74 68 65 20 73 71 6c 69 74 65  tains the sqlite
1eebb 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 61 6e  3_get_table() an
1eebc 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  d sqlite3_free_t
1eebd 61 62 6c 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66  able().** interf
1eebe 61 63 65 20 72 6f 75 74 69 6e 65 73 2e 20 20 54  ace routines.  T
1eebf 68 65 73 65 20 61 72 65 20 6a 75 73 74 20 77 72  hese are just wr
1eec0 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68  appers around th
1eec1 65 20 6d 61 69 6e 0a 2a 2a 20 69 6e 74 65 72 66  e main.** interf
1eec2 61 63 65 20 72 6f 75 74 69 6e 65 20 6f 66 20 73  ace routine of s
1eec3 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a  qlite3_exec()..*
1eec4 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
1eec5 6e 65 73 20 61 72 65 20 69 6e 20 61 20 73 65 70  nes are in a sep
1eec6 61 72 61 74 65 20 66 69 6c 65 73 20 73 6f 20 74  arate files so t
1eec7 68 61 74 20 74 68 65 79 20 77 69 6c 6c 20 6e 6f  hat they will no
1eec8 74 20 62 65 20 6c 69 6e 6b 65 64 0a 2a 2a 20 69  t be linked.** i
1eec9 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 75  f they are not u
1eeca 73 65 64 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  sed..*/..#ifndef
1eecb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54   SQLITE_OMIT_GET
1eecc 5f 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68  _TABLE../*.** Th
1eecd 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
1eece 75 73 65 64 20 74 6f 20 70 61 73 73 20 64 61 74  used to pass dat
1eecf 61 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67  a from sqlite3_g
1eed0 65 74 5f 74 61 62 6c 65 28 29 20 74 68 72 6f 75  et_table() throu
1eed1 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c  gh.** to the cal
1eed2 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69  lback function i
1eed3 73 20 75 73 65 73 20 74 6f 20 62 75 69 6c 64 20  s uses to build 
1eed4 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 74  the result..*/.t
1eed5 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
1eed6 62 52 65 73 75 6c 74 20 7b 0a 20 20 63 68 61 72  bResult {.  char
1eed7 20 2a 2a 61 7a 52 65 73 75 6c 74 3b 0a 20 20 63   **azResult;.  c
1eed8 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 0a 20 20  har *zErrMsg;.  
1eed9 69 6e 74 20 6e 52 65 73 75 6c 74 3b 0a 20 20 69  int nResult;.  i
1eeda 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 69 6e 74  nt nAlloc;.  int
1eedb 20 6e 52 6f 77 3b 0a 20 20 69 6e 74 20 6e 43 6f   nRow;.  int nCo
1eedc 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e 44 61 74  lumn;.  int nDat
1eedd 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 7d 20 54  a;.  int rc;.} T
1eede 61 62 52 65 73 75 6c 74 3b 0a 0a 2f 2a 0a 2a 2a  abResult;../*.**
1eedf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1eee0 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72   called once for
1eee1 20 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65   each row in the
1eee2 20 72 65 73 75 6c 74 20 74 61 62 6c 65 2e 20 20   result table.  
1eee3 49 74 73 20 6a 6f 62 0a 2a 2a 20 69 73 20 74 6f  Its job.** is to
1eee4 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 54 61 62   fill in the Tab
1eee5 52 65 73 75 6c 74 20 73 74 72 75 63 74 75 72 65  Result structure
1eee6 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2c 20   appropriately, 
1eee7 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 65 77 0a 2a  allocating new.*
1eee8 2a 20 6d 65 6d 6f 72 79 20 61 73 20 6e 65 63 65  * memory as nece
1eee9 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssary..*/.static
1eeea 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74   int sqlite3_get
1eeeb 5f 74 61 62 6c 65 5f 63 62 28 76 6f 69 64 20 2a  _table_cb(void *
1eeec 70 41 72 67 2c 20 69 6e 74 20 6e 43 6f 6c 2c 20  pArg, int nCol, 
1eeed 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61  char **argv, cha
1eeee 72 20 2a 2a 63 6f 6c 76 29 7b 0a 20 20 54 61 62  r **colv){.  Tab
1eeef 52 65 73 75 6c 74 20 2a 70 20 3d 20 28 54 61 62  Result *p = (Tab
1eef0 52 65 73 75 6c 74 2a 29 70 41 72 67 3b 0a 20 20  Result*)pArg;.  
1eef1 69 6e 74 20 6e 65 65 64 3b 0a 20 20 69 6e 74 20  int need;.  int 
1eef2 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20  i;.  char *z;.. 
1eef3 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
1eef4 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70  ere is enough sp
1eef5 61 63 65 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75  ace in p->azResu
1eef6 6c 74 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79  lt to hold every
1eef7 74 68 69 6e 67 0a 20 20 2a 2a 20 77 65 20 6e 65  thing.  ** we ne
1eef8 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 66  ed to remember f
1eef9 72 6f 6d 20 74 68 69 73 20 69 6e 76 6f 63 61 74  rom this invocat
1eefa 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 62  ion of the callb
1eefb 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ack..  */.  if( 
1eefc 70 2d 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72  p->nRow==0 && ar
1eefd 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 65  gv!=0 ){.    nee
1eefe 64 20 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20 7d 65  d = nCol*2;.  }e
1eeff 6c 73 65 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20  lse{.    need = 
1ef00 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nCol;.  }.  if( 
1ef01 70 2d 3e 6e 44 61 74 61 20 2b 20 6e 65 65 64 20  p->nData + need 
1ef02 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a  >= p->nAlloc ){.
1ef03 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77      char **azNew
1ef04 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20  ;.    p->nAlloc 
1ef05 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20  = p->nAlloc*2 + 
1ef06 6e 65 65 64 20 2b 20 31 3b 0a 20 20 20 20 61 7a  need + 1;.    az
1ef07 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  New = sqlite3_re
1ef08 61 6c 6c 6f 63 28 20 70 2d 3e 61 7a 52 65 73 75  alloc( p->azResu
1ef09 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  lt, sizeof(char*
1ef0a 29 2a 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  )*p->nAlloc );. 
1ef0b 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20     if( azNew==0 
1ef0c 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61  ) goto malloc_fa
1ef0d 69 6c 65 64 3b 0a 20 20 20 20 70 2d 3e 61 7a 52  iled;.    p->azR
1ef0e 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20  esult = azNew;. 
1ef0f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73   }..  /* If this
1ef10 20 69 73 20 74 68 65 20 66 69 72 73 74 20 72 6f   is the first ro
1ef11 77 2c 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65  w, then generate
1ef12 20 61 6e 20 65 78 74 72 61 20 72 6f 77 20 63 6f   an extra row co
1ef13 6e 74 61 69 6e 69 6e 67 0a 20 20 2a 2a 20 74 68  ntaining.  ** th
1ef14 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63  e names of all c
1ef15 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  olumns..  */.  i
1ef16 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29 7b  f( p->nRow==0 ){
1ef17 0a 20 20 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20  .    p->nColumn 
1ef18 3d 20 6e 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28  = nCol;.    for(
1ef19 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
1ef1a 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 6f 6c  ){.      if( col
1ef1b 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  v[i]==0 ){.     
1ef1c 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d     z = sqlite3_m
1ef1d 70 72 69 6e 74 66 28 22 22 29 3b 0a 20 20 20 20  printf("");.    
1ef1e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1ef1f 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72   z = sqlite3_mpr
1ef20 69 6e 74 66 28 22 25 73 22 2c 20 63 6f 6c 76 5b  intf("%s", colv[
1ef21 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i]);.      }.   
1ef22 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70     p->azResult[p
1ef23 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a  ->nData++] = z;.
1ef24 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
1ef25 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 6e 43  ( p->nColumn!=nC
1ef26 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ol ){.    sqlite
1ef27 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1ef28 45 72 72 4d 73 67 2c 0a 20 20 20 20 20 20 20 22  ErrMsg,.       "
1ef29 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
1ef2a 65 28 29 20 63 61 6c 6c 65 64 20 77 69 74 68 20  e() called with 
1ef2b 74 77 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 63 6f  two or more inco
1ef2c 6d 70 61 74 69 62 6c 65 20 71 75 65 72 69 65 73  mpatible queries
1ef2d 22 2c 0a 20 20 20 20 20 20 20 28 63 68 61 72 2a  ",.       (char*
1ef2e 29 30 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  )0);.    p->rc =
1ef2f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1ef30 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
1ef31 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72  ..  /* Copy over
1ef32 20 74 68 65 20 72 6f 77 20 64 61 74 61 0a 20 20   the row data.  
1ef33 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 21 3d 30  */.  if( argv!=0
1ef34 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
1ef35 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
1ef36 20 20 20 20 20 69 66 28 20 61 72 67 76 5b 69 5d       if( argv[i]
1ef37 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ==0 ){.        z
1ef38 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
1ef39 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  e{.        int n
1ef3a 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 69   = strlen(argv[i
1ef3b 5d 29 2b 31 3b 0a 20 20 20 20 20 20 20 20 7a 20  ])+1;.        z 
1ef3c 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
1ef3d 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ( n );.        i
1ef3e 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d  f( z==0 ) goto m
1ef3f 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20  alloc_failed;.  
1ef40 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20        memcpy(z, 
1ef41 61 72 67 76 5b 69 5d 2c 20 6e 29 3b 0a 20 20 20  argv[i], n);.   
1ef42 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 61 7a     }.      p->az
1ef43 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b  Result[p->nData+
1ef44 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20  +] = z;.    }.  
1ef45 20 20 70 2d 3e 6e 52 6f 77 2b 2b 3b 0a 20 20 7d    p->nRow++;.  }
1ef46 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6d 61  .  return 0;..ma
1ef47 6c 6c 6f 63 5f 66 61 69 6c 65 64 3a 0a 20 20 70  lloc_failed:.  p
1ef48 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
1ef49 4d 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  MEM;.  return 1;
1ef4a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  .}../*.** Query 
1ef4b 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 42  the database.  B
1ef4c 75 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e  ut instead of in
1ef4d 76 6f 6b 69 6e 67 20 61 20 63 61 6c 6c 62 61 63  voking a callbac
1ef4e 6b 20 66 6f 72 20 65 61 63 68 20 72 6f 77 2c 0a  k for each row,.
1ef4f 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66 6f 72 20  ** malloc() for 
1ef50 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  space to hold th
1ef51 65 20 72 65 73 75 6c 74 20 61 6e 64 20 72 65 74  e result and ret
1ef52 75 72 6e 20 74 68 65 20 65 6e 74 69 72 65 20 72  urn the entire r
1ef53 65 73 75 6c 74 73 0a 2a 2a 20 61 74 20 74 68 65  esults.** at the
1ef54 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74   conclusion of t
1ef55 68 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  he call..**.** T
1ef56 68 65 20 72 65 73 75 6c 74 20 74 68 61 74 20 69  he result that i
1ef57 73 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 2a 2a  s written to ***
1ef58 70 61 7a 52 65 73 75 6c 74 20 69 73 20 68 65 6c  pazResult is hel
1ef59 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  d in memory obta
1ef5a 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c  ined.** from mal
1ef5b 6c 6f 63 28 29 2e 20 20 42 75 74 20 74 68 65 20  loc().  But the 
1ef5c 63 61 6c 6c 65 72 20 63 61 6e 6e 6f 74 20 66 72  caller cannot fr
1ef5d 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 64  ee this memory d
1ef5e 69 72 65 63 74 6c 79 2e 20 20 0a 2a 2a 20 49 6e  irectly.  .** In
1ef5f 73 74 65 61 64 2c 20 74 68 65 20 65 6e 74 69 72  stead, the entir
1ef60 65 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 62  e table should b
1ef61 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  e passed to sqli
1ef62 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
1ef63 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c   when.** the cal
1ef64 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69  ling procedure i
1ef65 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67  s finished using
1ef66 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41   it..*/.SQLITE_A
1ef67 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  PI int sqlite3_g
1ef68 65 74 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69  et_table(.  sqli
1ef69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
1ef6a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
1ef6b 61 74 61 62 61 73 65 20 6f 6e 20 77 68 69 63 68  atabase on which
1ef6c 20 74 68 65 20 53 51 4c 20 65 78 65 63 75 74 65   the SQL execute
1ef6d 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
1ef6e 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
1ef6f 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f     /* The SQL to
1ef70 20 62 65 20 65 78 65 63 75 74 65 64 20 2a 2f 0a   be executed */.
1ef71 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73    char ***pazRes
1ef72 75 6c 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ult,          /*
1ef73 20 57 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   Write the resul
1ef74 74 20 74 61 62 6c 65 20 68 65 72 65 20 2a 2f 0a  t table here */.
1ef75 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20    int *pnRow,   
1ef76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ef77 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65   Write the numbe
1ef78 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  r of rows in the
1ef79 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a   result here */.
1ef7a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c    int *pnColumn,
1ef7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ef7c 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65   Write the numbe
1ef7d 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  r of columns of 
1ef7e 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20  result here */. 
1ef7f 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
1ef80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ef81 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73  Write error mess
1ef82 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ages here */.){.
1ef83 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 61 62 52    int rc;.  TabR
1ef84 65 73 75 6c 74 20 72 65 73 3b 0a 20 20 69 66 28  esult res;.  if(
1ef85 20 70 61 7a 52 65 73 75 6c 74 3d 3d 30 20 29 7b   pazResult==0 ){
1ef86 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1ef87 52 52 4f 52 3b 20 7d 0a 20 20 2a 70 61 7a 52 65  RROR; }.  *pazRe
1ef88 73 75 6c 74 20 3d 20 30 3b 0a 20 20 69 66 28 20  sult = 0;.  if( 
1ef89 70 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f  pnColumn ) *pnCo
1ef8a 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20  lumn = 0;.  if( 
1ef8b 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d  pnRow ) *pnRow =
1ef8c 20 30 3b 0a 20 20 72 65 73 2e 7a 45 72 72 4d 73   0;.  res.zErrMs
1ef8d 67 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 52 65  g = 0;.  res.nRe
1ef8e 73 75 6c 74 20 3d 20 30 3b 0a 20 20 72 65 73 2e  sult = 0;.  res.
1ef8f 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 72 65 73 2e  nRow = 0;.  res.
1ef90 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 72  nColumn = 0;.  r
1ef91 65 73 2e 6e 44 61 74 61 20 3d 20 31 3b 0a 20 20  es.nData = 1;.  
1ef92 72 65 73 2e 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b  res.nAlloc = 20;
1ef93 0a 20 20 72 65 73 2e 72 63 20 3d 20 53 51 4c 49  .  res.rc = SQLI
1ef94 54 45 5f 4f 4b 3b 0a 20 20 72 65 73 2e 61 7a 52  TE_OK;.  res.azR
1ef95 65 73 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f  esult = sqlite3_
1ef96 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 63  malloc( sizeof(c
1ef97 68 61 72 2a 29 2a 72 65 73 2e 6e 41 6c 6c 6f 63  har*)*res.nAlloc
1ef98 20 29 3b 0a 20 20 69 66 28 20 72 65 73 2e 61 7a   );.  if( res.az
1ef99 52 65 73 75 6c 74 3d 3d 30 20 29 20 72 65 74 75  Result==0 ) retu
1ef9a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1ef9b 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b  .  res.azResult[
1ef9c 30 5d 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73  0] = 0;.  rc = s
1ef9d 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
1ef9e 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 5f 67 65  zSql, sqlite3_ge
1ef9f 74 5f 74 61 62 6c 65 5f 63 62 2c 20 26 72 65 73  t_table_cb, &res
1efa0 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 23 69 66  , pzErrMsg);.#if
1efa1 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 71  ndef NDEBUG.  sq
1efa2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1efa3 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
1efa4 61 73 73 65 72 74 28 28 72 63 26 64 62 2d 3e 65  assert((rc&db->e
1efa5 72 72 4d 61 73 6b 29 3d 3d 72 63 20 26 26 20 28  rrMask)==rc && (
1efa6 72 65 73 2e 72 63 26 64 62 2d 3e 65 72 72 4d 61  res.rc&db->errMa
1efa7 73 6b 29 3d 3d 72 65 73 2e 72 63 29 3b 0a 20 20  sk)==res.rc);.  
1efa8 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1efa9 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
1efaa 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72 65 73  #endif.  if( res
1efab 2e 61 7a 52 65 73 75 6c 74 20 29 7b 0a 20 20 20  .azResult ){.   
1efac 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
1efad 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 29  res.azResult[0])
1efae 3e 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e 6e 44  >= sizeof(res.nD
1efaf 61 74 61 29 20 29 3b 0a 20 20 20 20 72 65 73 2e  ata) );.    res.
1efb0 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 28 63  azResult[0] = (c
1efb1 68 61 72 2a 29 72 65 73 2e 6e 44 61 74 61 3b 0a  har*)res.nData;.
1efb2 20 20 7d 0a 20 20 69 66 28 20 28 72 63 26 30 78    }.  if( (rc&0x
1efb3 66 66 29 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52  ff)==SQLITE_ABOR
1efb4 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  T ){.    sqlite3
1efb5 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73  _free_table(&res
1efb6 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20  .azResult[1]);. 
1efb7 20 20 20 69 66 28 20 72 65 73 2e 7a 45 72 72 4d     if( res.zErrM
1efb8 73 67 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  sg ){.      if( 
1efb9 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
1efba 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1efbb 28 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  (*pzErrMsg);.   
1efbc 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
1efbd 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
1efbe 28 22 25 73 22 2c 72 65 73 2e 7a 45 72 72 4d 73  ("%s",res.zErrMs
1efbf 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
1efc0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72    sqlite3_free(r
1efc1 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  es.zErrMsg);.   
1efc2 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d   }.    sqlite3_m
1efc3 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
1efc4 75 74 65 78 29 3b 0a 20 20 20 20 64 62 2d 3e 65  utex);.    db->e
1efc5 72 72 43 6f 64 65 20 3d 20 72 65 73 2e 72 63 3b  rrCode = res.rc;
1efc6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1efc7 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
1efc8 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ex);.    return 
1efc9 72 65 73 2e 72 63 3b 0a 20 20 7d 0a 20 20 73 71  res.rc;.  }.  sq
1efca 6c 69 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a  lite3_free(res.z
1efcb 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72  ErrMsg);.  if( r
1efcc 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1efcd 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1efce 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65  _table(&res.azRe
1efcf 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 72 65  sult[1]);.    re
1efd0 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69  turn rc;.  }.  i
1efd1 66 28 20 72 65 73 2e 6e 41 6c 6c 6f 63 3e 72 65  f( res.nAlloc>re
1efd2 73 2e 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 63  s.nData ){.    c
1efd3 68 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20  har **azNew;.   
1efd4 20 61 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   azNew = sqlite3
1efd5 5f 72 65 61 6c 6c 6f 63 28 20 72 65 73 2e 61 7a  _realloc( res.az
1efd6 52 65 73 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63  Result, sizeof(c
1efd7 68 61 72 2a 29 2a 28 72 65 73 2e 6e 44 61 74 61  har*)*(res.nData
1efd8 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20 61  +1) );.    if( a
1efd9 7a 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  zNew==0 ){.     
1efda 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
1efdb 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c  ble(&res.azResul
1efdc 74 5b 31 5d 29 3b 0a 20 20 20 20 20 20 72 65 74  t[1]);.      ret
1efdd 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1efde 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 73 2e  ;.    }.    res.
1efdf 6e 41 6c 6c 6f 63 20 3d 20 72 65 73 2e 6e 44 61  nAlloc = res.nDa
1efe0 74 61 2b 31 3b 0a 20 20 20 20 72 65 73 2e 61 7a  ta+1;.    res.az
1efe1 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a  Result = azNew;.
1efe2 20 20 7d 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74    }.  *pazResult
1efe3 20 3d 20 26 72 65 73 2e 61 7a 52 65 73 75 6c 74   = &res.azResult
1efe4 5b 31 5d 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c  [1];.  if( pnCol
1efe5 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20  umn ) *pnColumn 
1efe6 3d 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20  = res.nColumn;. 
1efe7 20 69 66 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e   if( pnRow ) *pn
1efe8 52 6f 77 20 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a  Row = res.nRow;.
1efe9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1efea 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1efeb 6e 65 20 66 72 65 65 73 20 74 68 65 20 73 70 61  ne frees the spa
1efec 63 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67  ce the sqlite3_g
1efed 65 74 5f 74 61 62 6c 65 28 29 20 6d 61 6c 6c 6f  et_table() mallo
1efee 63 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ced..*/.SQLITE_A
1efef 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1eff0 66 72 65 65 5f 74 61 62 6c 65 28 0a 20 20 63 68  free_table(.  ch
1eff1 61 72 20 2a 2a 61 7a 52 65 73 75 6c 74 20 20 20  ar **azResult   
1eff2 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
1eff3 6c 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  lt returned from
1eff4 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65   from sqlite3_ge
1eff5 74 5f 74 61 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a  t_table() */.){.
1eff6 20 20 69 66 28 20 61 7a 52 65 73 75 6c 74 20 29    if( azResult )
1eff7 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  {.    int i, n;.
1eff8 20 20 20 20 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a      azResult--;.
1eff9 20 20 20 20 69 66 28 20 61 7a 52 65 73 75 6c 74      if( azResult
1effa 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1effb 20 20 6e 20 3d 20 28 69 6e 74 29 61 7a 52 65 73    n = (int)azRes
1effc 75 6c 74 5b 30 5d 3b 0a 20 20 20 20 66 6f 72 28  ult[0];.    for(
1effd 69 3d 31 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 20  i=1; i<n; i++){ 
1effe 69 66 28 20 61 7a 52 65 73 75 6c 74 5b 69 5d 20  if( azResult[i] 
1efff 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61  ) sqlite3_free(a
1f000 7a 52 65 73 75 6c 74 5b 69 5d 29 3b 20 7d 0a 20  zResult[i]); }. 
1f001 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1f002 61 7a 52 65 73 75 6c 74 29 3b 0a 20 20 7d 0a 7d  azResult);.  }.}
1f003 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
1f004 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c  TE_OMIT_GET_TABL
1f005 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
1f006 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 61 62  ***** End of tab
1f007 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  le.c ***********
1f008 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f009 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f00a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1f00b 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1f00c 20 74 72 69 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a   trigger.c *****
1f00d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f00e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f00f 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 54  ****/./*.**.** T
1f010 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1f011 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1f012 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1f013 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1f014 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1f015 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1f016 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1f017 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1f018 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1f019 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1f01a 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1f01b 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1f01c 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1f01d 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1f01e 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1f01f 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1f020 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1f021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f022 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f023 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f024 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f025 2a 2a 2a 2a 2a 2a 2a 0a 2a 0a 2a 2f 0a 0a 23 69  *******.*.*/..#i
1f026 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f027 54 5f 54 52 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20  T_TRIGGER./*.** 
1f028 44 65 6c 65 74 65 20 61 20 6c 69 6e 6b 65 64 20  Delete a linked 
1f029 6c 69 73 74 20 6f 66 20 54 72 69 67 67 65 72 53  list of TriggerS
1f02a 74 65 70 20 73 74 72 75 63 74 75 72 65 73 2e 0a  tep structures..
1f02b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f02c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
1f02d 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
1f02e 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
1f02f 69 67 67 65 72 53 74 65 70 29 7b 0a 20 20 77 68  iggerStep){.  wh
1f030 69 6c 65 28 20 70 54 72 69 67 67 65 72 53 74 65  ile( pTriggerSte
1f031 70 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  p ){.    Trigger
1f032 53 74 65 70 20 2a 20 70 54 6d 70 20 3d 20 70 54  Step * pTmp = pT
1f033 72 69 67 67 65 72 53 74 65 70 3b 0a 20 20 20 20  riggerStep;.    
1f034 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 70  pTriggerStep = p
1f035 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 4e 65  TriggerStep->pNe
1f036 78 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 54 6d  xt;..    if( pTm
1f037 70 2d 3e 74 61 72 67 65 74 2e 64 79 6e 20 29 20  p->target.dyn ) 
1f038 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 63 68  sqlite3_free((ch
1f039 61 72 2a 29 70 54 6d 70 2d 3e 74 61 72 67 65 74  ar*)pTmp->target
1f03a 2e 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .z);.    sqlite3
1f03b 45 78 70 72 44 65 6c 65 74 65 28 70 54 6d 70 2d  ExprDelete(pTmp-
1f03c 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71  >pWhere);.    sq
1f03d 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
1f03e 65 74 65 28 70 54 6d 70 2d 3e 70 45 78 70 72 4c  ete(pTmp->pExprL
1f03f 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
1f040 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 54  3SelectDelete(pT
1f041 6d 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  mp->pSelect);.  
1f042 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
1f043 65 6c 65 74 65 28 70 54 6d 70 2d 3e 70 49 64 4c  elete(pTmp->pIdL
1f044 69 73 74 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  ist);..    sqlit
1f045 65 33 5f 66 72 65 65 28 70 54 6d 70 29 3b 0a 20  e3_free(pTmp);. 
1f046 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
1f047 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
1f048 65 20 70 61 72 73 65 72 20 77 68 65 6e 20 69 74  e parser when it
1f049 20 73 65 65 73 20 61 20 43 52 45 41 54 45 20 54   sees a CREATE T
1f04a 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
1f04b 0a 2a 2a 20 75 70 20 74 6f 20 74 68 65 20 70 6f  .** up to the po
1f04c 69 6e 74 20 6f 66 20 74 68 65 20 42 45 47 49 4e  int of the BEGIN
1f04d 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67   before the trig
1f04e 67 65 72 20 61 63 74 69 6f 6e 73 2e 20 20 41 20  ger actions.  A 
1f04f 54 72 69 67 67 65 72 0a 2a 2a 20 73 74 72 75 63  Trigger.** struc
1f050 74 75 72 65 20 69 73 20 67 65 6e 65 72 61 74 65  ture is generate
1f051 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 69  d based on the i
1f052 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61 69 6c  nformation avail
1f053 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 64 0a  able and stored.
1f054 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e  ** in pParse->pN
1f055 65 77 54 72 69 67 67 65 72 2e 20 20 41 66 74 65  ewTrigger.  Afte
1f056 72 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63  r the trigger ac
1f057 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20  tions have been 
1f058 70 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a 20 73  parsed, the.** s
1f059 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
1f05a 67 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ger() function i
1f05b 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70  s called to comp
1f05c 6c 65 74 65 20 74 68 65 20 74 72 69 67 67 65 72  lete the trigger
1f05d 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  .** construction
1f05e 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c   process..*/.SQL
1f05f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1f060 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69   sqlite3BeginTri
1f061 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70  gger(.  Parse *p
1f062 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54  Parse,      /* T
1f063 68 65 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  he parse context
1f064 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54   of the CREATE T
1f065 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
1f066 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
1f067 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  me1,      /* The
1f068 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69   name of the tri
1f069 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  gger */.  Token 
1f06a 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a  *pName2,      /*
1f06b 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
1f06c 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 69 6e   trigger */.  in
1f06d 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20  t tr_tm,        
1f06e 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 42    /* One of TK_B
1f06f 45 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45 52 2c  EFORE, TK_AFTER,
1f070 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20   TK_INSTEAD */. 
1f071 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
1f072 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
1f073 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50 44  K_INSERT, TK_UPD
1f074 41 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a  ATE, TK_DELETE *
1f075 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c  /.  IdList *pCol
1f076 75 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c 75 6d  umns,   /* colum
1f077 6e 20 6c 69 73 74 20 69 66 20 74 68 69 73 20 69  n list if this i
1f078 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20 74  s an UPDATE OF t
1f079 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 72 63 4c  rigger */.  SrcL
1f07a 69 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ist *pTableName,
1f07b 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
1f07c 68 65 20 74 61 62 6c 65 2f 76 69 65 77 20 74 68  he table/view th
1f07d 65 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65  e trigger applie
1f07e 73 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72 20 2a  s to */.  Expr *
1f07f 70 57 68 65 6e 2c 20 20 20 20 20 20 20 20 2f 2a  pWhen,        /*
1f080 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a   WHEN clause */.
1f081 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20    int isTemp,   
1f082 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1f083 20 74 68 65 20 54 45 4d 50 4f 52 41 52 59 20 6b   the TEMPORARY k
1f084 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e  eyword is presen
1f085 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72  t */.  int noErr
1f086 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75             /* Su
1f087 70 70 72 65 73 73 20 65 72 72 6f 72 73 20 69 66  ppress errors if
1f088 20 74 68 65 20 74 72 69 67 67 65 72 20 61 6c 72   the trigger alr
1f089 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29  eady exists */.)
1f08a 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  {.  Trigger *pTr
1f08b 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 54 61 62  igger = 0;.  Tab
1f08c 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68 61 72  le *pTab;.  char
1f08d 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20   *zName = 0;    
1f08e 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1f08f 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  he trigger */.  
1f090 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1f091 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
1f092 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
1f093 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1f094 61 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ase to store the
1f095 20 74 72 69 67 67 65 72 20 69 6e 20 2a 2f 0a 20   trigger in */. 
1f096 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20   Token *pName;  
1f097 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f098 75 6e 71 75 61 6c 69 66 69 65 64 20 64 62 20 6e  unqualified db n
1f099 61 6d 65 20 2a 2f 0a 20 20 44 62 46 69 78 65 72  ame */.  DbFixer
1f09a 20 73 46 69 78 3b 0a 20 20 69 6e 74 20 69 54 61   sFix;.  int iTa
1f09b 62 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  bDb;..  assert( 
1f09c 70 4e 61 6d 65 31 21 3d 30 20 29 3b 20 20 20 2f  pName1!=0 );   /
1f09d 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69 67 68  * pName1->z migh
1f09e 74 20 62 65 20 4e 55 4c 4c 2c 20 62 75 74 20 6e  t be NULL, but n
1f09f 6f 74 20 70 4e 61 6d 65 31 20 69 74 73 65 6c 66  ot pName1 itself
1f0a0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4e   */.  assert( pN
1f0a1 61 6d 65 32 21 3d 30 20 29 3b 0a 20 20 69 66 28  ame2!=0 );.  if(
1f0a2 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 2f   isTemp ){.    /
1f0a3 2a 20 49 66 20 54 45 4d 50 20 77 61 73 20 73 70  * If TEMP was sp
1f0a4 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 74 68  ecified, then th
1f0a5 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 6d  e trigger name m
1f0a6 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66  ay not be qualif
1f0a7 69 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ied. */.    if( 
1f0a8 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20  pName2->n>0 ){. 
1f0a9 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1f0aa 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65  rMsg(pParse, "te
1f0ab 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 20  mporary trigger 
1f0ac 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 71 75 61  may not have qua
1f0ad 6c 69 66 69 65 64 20 6e 61 6d 65 22 29 3b 0a 20  lified name");. 
1f0ae 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65       goto trigge
1f0af 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  r_cleanup;.    }
1f0b0 0a 20 20 20 20 69 44 62 20 3d 20 31 3b 0a 20 20  .    iDb = 1;.  
1f0b1 20 20 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 31    pName = pName1
1f0b2 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
1f0b3 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65  * Figure out the
1f0b4 20 64 62 20 74 68 61 74 20 74 68 65 20 74 68 65   db that the the
1f0b5 20 74 72 69 67 67 65 72 20 77 69 6c 6c 20 62 65   trigger will be
1f0b6 20 63 72 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20   created in */. 
1f0b7 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
1f0b8 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72  TwoPartName(pPar
1f0b9 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
1f0ba 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 20  e2, &pName);.   
1f0bb 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20   if( iDb<0 ){.  
1f0bc 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
1f0bd 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
1f0be 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1f0bf 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 77 61   trigger name wa
1f0c0 73 20 75 6e 71 75 61 6c 69 66 69 65 64 2c 20 61  s unqualified, a
1f0c1 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  nd the table is 
1f0c2 61 20 74 65 6d 70 20 74 61 62 6c 65 2c 0a 20 20  a temp table,.  
1f0c3 2a 2a 20 74 68 65 6e 20 73 65 74 20 69 44 62 20  ** then set iDb 
1f0c4 74 6f 20 31 20 74 6f 20 63 72 65 61 74 65 20 74  to 1 to create t
1f0c5 68 65 20 74 72 69 67 67 65 72 20 69 6e 20 74 68  he trigger in th
1f0c6 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
1f0c7 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 73 71  base..  ** If sq
1f0c8 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b  lite3SrcListLook
1f0c9 75 70 28 29 20 72 65 74 75 72 6e 73 20 30 2c 20  up() returns 0, 
1f0ca 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 74  indicating the t
1f0cb 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 0a 20 20  able does not.  
1f0cc 2a 2a 20 65 78 69 73 74 2c 20 74 68 65 20 65 72  ** exist, the er
1f0cd 72 6f 72 20 69 73 20 63 61 75 67 68 74 20 62 79  ror is caught by
1f0ce 20 74 68 65 20 62 6c 6f 63 6b 20 62 65 6c 6f 77   the block below
1f0cf 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 54  ..  */.  if( !pT
1f0d0 61 62 6c 65 4e 61 6d 65 20 7c 7c 20 64 62 2d 3e  ableName || db->
1f0d1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1f0d2 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
1f0d3 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
1f0d4 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72  pTab = sqlite3Sr
1f0d5 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72  cListLookup(pPar
1f0d6 73 65 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b  se, pTableName);
1f0d7 0a 20 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e  .  if( pName2->n
1f0d8 3d 3d 30 20 26 26 20 70 54 61 62 20 26 26 20 70  ==0 && pTab && p
1f0d9 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62  Tab->pSchema==db
1f0da 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61  ->aDb[1].pSchema
1f0db 20 29 7b 0a 20 20 20 20 69 44 62 20 3d 20 31 3b   ){.    iDb = 1;
1f0dc 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72  .  }..  /* Ensur
1f0dd 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  e the table name
1f0de 20 6d 61 74 63 68 65 73 20 64 61 74 61 62 61 73   matches databas
1f0df 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 61 74 20  e name and that 
1f0e0 74 68 65 20 74 61 62 6c 65 20 65 78 69 73 74 73  the table exists
1f0e1 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   */.  if( db->ma
1f0e2 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
1f0e3 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
1f0e4 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  p;.  assert( pTa
1f0e5 62 6c 65 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31  bleName->nSrc==1
1f0e6 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   );.  if( sqlite
1f0e7 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20  3FixInit(&sFix, 
1f0e8 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 74 72  pParse, iDb, "tr
1f0e9 69 67 67 65 72 22 2c 20 70 4e 61 6d 65 29 20 26  igger", pName) &
1f0ea 26 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  & .      sqlite3
1f0eb 46 69 78 53 72 63 4c 69 73 74 28 26 73 46 69 78  FixSrcList(&sFix
1f0ec 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 20 29 7b  , pTableName) ){
1f0ed 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
1f0ee 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
1f0ef 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53   pTab = sqlite3S
1f0f0 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61  rcListLookup(pPa
1f0f1 72 73 65 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29  rse, pTableName)
1f0f2 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 7b  ;.  if( !pTab ){
1f0f3 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c  .    /* The tabl
1f0f4 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  e does not exist
1f0f5 2e 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 74 72  . */.    goto tr
1f0f6 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
1f0f7 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75   }.  if( IsVirtu
1f0f8 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
1f0f9 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1f0fa 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
1f0fb 63 72 65 61 74 65 20 74 72 69 67 67 65 72 73 20  create triggers 
1f0fc 6f 6e 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  on virtual table
1f0fd 73 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72  s");.    goto tr
1f0fe 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
1f0ff 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74   }..  /* Check t
1f100 68 61 74 20 74 68 65 20 74 72 69 67 67 65 72 20  hat the trigger 
1f101 6e 61 6d 65 20 69 73 20 6e 6f 74 20 72 65 73 65  name is not rese
1f102 72 76 65 64 20 61 6e 64 20 74 68 61 74 20 6e 6f  rved and that no
1f103 20 74 72 69 67 67 65 72 20 6f 66 20 74 68 65 0a   trigger of the.
1f104 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e    ** specified n
1f105 61 6d 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  ame exists */.  
1f106 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
1f107 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
1f108 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21   pName);.  if( !
1f109 7a 4e 61 6d 65 20 7c 7c 20 53 51 4c 49 54 45 5f  zName || SQLITE_
1f10a 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b  OK!=sqlite3Check
1f10b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73  ObjectName(pPars
1f10c 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  e, zName) ){.   
1f10d 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
1f10e 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28  eanup;.  }.  if(
1f10f 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
1f110 28 26 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  (&(db->aDb[iDb].
1f111 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73  pSchema->trigHas
1f112 68 29 2c 20 7a 4e 61 6d 65 2c 73 74 72 6c 65 6e  h), zName,strlen
1f113 28 7a 4e 61 6d 65 29 29 20 29 7b 0a 20 20 20 20  (zName)) ){.    
1f114 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20  if( !noErr ){.  
1f115 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1f116 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 72 69  Msg(pParse, "tri
1f117 67 67 65 72 20 25 54 20 61 6c 72 65 61 64 79 20  gger %T already 
1f118 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b  exists", pName);
1f119 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20  .    }.    goto 
1f11a 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
1f11b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f  .  }..  /* Do no
1f11c 74 20 63 72 65 61 74 65 20 61 20 74 72 69 67 67  t create a trigg
1f11d 65 72 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74  er on a system t
1f11e 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 73 71  able */.  if( sq
1f11f 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54  lite3StrNICmp(pT
1f120 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69  ab->zName, "sqli
1f121 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20  te_", 7)==0 ){. 
1f122 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f123 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
1f124 6f 74 20 63 72 65 61 74 65 20 74 72 69 67 67 65  ot create trigge
1f125 72 20 6f 6e 20 73 79 73 74 65 6d 20 74 61 62 6c  r on system tabl
1f126 65 22 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  e");.    pParse-
1f127 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74  >nErr++;.    got
1f128 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
1f129 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 4e 53  p;.  }..  /* INS
1f12a 54 45 41 44 20 6f 66 20 74 72 69 67 67 65 72 73  TEAD of triggers
1f12b 20 61 72 65 20 6f 6e 6c 79 20 66 6f 72 20 76 69   are only for vi
1f12c 65 77 73 20 61 6e 64 20 76 69 65 77 73 20 6f 6e  ews and views on
1f12d 6c 79 20 73 75 70 70 6f 72 74 20 49 4e 53 54 45  ly support INSTE
1f12e 41 44 0a 20 20 2a 2a 20 6f 66 20 74 72 69 67 67  AD.  ** of trigg
1f12f 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ers..  */.  if( 
1f130 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 26 26  pTab->pSelect &&
1f131 20 74 72 5f 74 6d 21 3d 54 4b 5f 49 4e 53 54 45   tr_tm!=TK_INSTE
1f132 41 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  AD ){.    sqlite
1f133 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1f134 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65  , "cannot create
1f135 20 25 73 20 74 72 69 67 67 65 72 20 6f 6e 20 76   %s trigger on v
1f136 69 65 77 3a 20 25 53 22 2c 20 0a 20 20 20 20 20  iew: %S", .     
1f137 20 20 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f     (tr_tm == TK_
1f138 42 45 46 4f 52 45 29 3f 22 42 45 46 4f 52 45 22  BEFORE)?"BEFORE"
1f139 3a 22 41 46 54 45 52 22 2c 20 70 54 61 62 6c 65  :"AFTER", pTable
1f13a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 67 6f  Name, 0);.    go
1f13b 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
1f13c 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70  up;.  }.  if( !p
1f13d 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 26 26 20  Tab->pSelect && 
1f13e 74 72 5f 74 6d 3d 3d 54 4b 5f 49 4e 53 54 45 41  tr_tm==TK_INSTEA
1f13f 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  D ){.    sqlite3
1f140 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1f141 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20   "cannot create 
1f142 49 4e 53 54 45 41 44 20 4f 46 22 0a 20 20 20 20  INSTEAD OF".    
1f143 20 20 20 20 22 20 74 72 69 67 67 65 72 20 6f 6e      " trigger on
1f144 20 74 61 62 6c 65 3a 20 25 53 22 2c 20 70 54 61   table: %S", pTa
1f145 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  bleName, 0);.   
1f146 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
1f147 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 54 61  eanup;.  }.  iTa
1f148 62 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  bDb = sqlite3Sch
1f149 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
1f14a 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a  Tab->pSchema);..
1f14b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f14c 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
1f14d 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f  N.  {.    int co
1f14e 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41  de = SQLITE_CREA
1f14f 54 45 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20  TE_TRIGGER;.    
1f150 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
1f151 3d 20 64 62 2d 3e 61 44 62 5b 69 54 61 62 44 62  = db->aDb[iTabDb
1f152 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e  ].zName;.    con
1f153 73 74 20 63 68 61 72 20 2a 7a 44 62 54 72 69 67  st char *zDbTrig
1f154 20 3d 20 69 73 54 65 6d 70 20 3f 20 64 62 2d 3e   = isTemp ? db->
1f155 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3a 20 7a  aDb[1].zName : z
1f156 44 62 3b 0a 20 20 20 20 69 66 28 20 69 54 61 62  Db;.    if( iTab
1f157 44 62 3d 3d 31 20 7c 7c 20 69 73 54 65 6d 70 20  Db==1 || isTemp 
1f158 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  ) code = SQLITE_
1f159 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47  CREATE_TEMP_TRIG
1f15a 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  GER;.    if( sql
1f15b 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
1f15c 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d  arse, code, zNam
1f15d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  e, pTab->zName, 
1f15e 7a 44 62 54 72 69 67 29 20 29 7b 0a 20 20 20 20  zDbTrig) ){.    
1f15f 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
1f160 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
1f161 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
1f162 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
1f163 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43  QLITE_INSERT, SC
1f164 48 45 4d 41 5f 54 41 42 4c 45 28 69 54 61 62 44  HEMA_TABLE(iTabD
1f165 62 29 2c 30 2c 7a 44 62 29 29 7b 0a 20 20 20 20  b),0,zDb)){.    
1f166 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
1f167 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
1f168 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
1f169 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65  NSTEAD OF trigge
1f16a 72 73 20 63 61 6e 20 6f 6e 6c 79 20 61 70 70 65  rs can only appe
1f16b 61 72 20 6f 6e 20 76 69 65 77 73 20 61 6e 64 20  ar on views and 
1f16c 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 0a  BEFORE triggers.
1f16d 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 61 70 70 65    ** cannot appe
1f16e 61 72 20 6f 6e 20 76 69 65 77 73 2e 20 20 53 6f  ar on views.  So
1f16f 20 77 65 20 6d 69 67 68 74 20 61 73 20 77 65 6c   we might as wel
1f170 6c 20 74 72 61 6e 73 6c 61 74 65 20 65 76 65 72  l translate ever
1f171 79 0a 20 20 2a 2a 20 49 4e 53 54 45 41 44 20 4f  y.  ** INSTEAD O
1f172 46 20 74 72 69 67 67 65 72 20 69 6e 74 6f 20 61  F trigger into a
1f173 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 2e   BEFORE trigger.
1f174 20 20 49 74 20 73 69 6d 70 6c 69 66 69 65 73 20    It simplifies 
1f175 63 6f 64 65 0a 20 20 2a 2a 20 65 6c 73 65 77 68  code.  ** elsewh
1f176 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 20 28  ere..  */.  if (
1f177 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f 49 4e 53 54  tr_tm == TK_INST
1f178 45 41 44 29 7b 0a 20 20 20 20 74 72 5f 74 6d 20  EAD){.    tr_tm 
1f179 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 0a 20 20 7d  = TK_BEFORE;.  }
1f17a 0a 0a 20 20 2f 2a 20 42 75 69 6c 64 20 74 68 65  ..  /* Build the
1f17b 20 54 72 69 67 67 65 72 20 6f 62 6a 65 63 74 20   Trigger object 
1f17c 2a 2f 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20  */.  pTrigger = 
1f17d 28 54 72 69 67 67 65 72 2a 29 73 71 6c 69 74 65  (Trigger*)sqlite
1f17e 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
1f17f 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72  , sizeof(Trigger
1f180 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67  ));.  if( pTrigg
1f181 65 72 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69  er==0 ) goto tri
1f182 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
1f183 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 20 3d  pTrigger->name =
1f184 20 7a 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20   zName;.  zName 
1f185 3d 20 30 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  = 0;.  pTrigger-
1f186 3e 74 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  >table = sqlite3
1f187 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 54 61  DbStrDup(db, pTa
1f188 62 6c 65 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e  bleName->a[0].zN
1f189 61 6d 65 29 3b 0a 20 20 70 54 72 69 67 67 65 72  ame);.  pTrigger
1f18a 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e  ->pSchema = db->
1f18b 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
1f18c 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 54  ;.  pTrigger->pT
1f18d 61 62 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d  abSchema = pTab-
1f18e 3e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 72 69  >pSchema;.  pTri
1f18f 67 67 65 72 2d 3e 6f 70 20 3d 20 6f 70 3b 0a 20  gger->op = op;. 
1f190 20 70 54 72 69 67 67 65 72 2d 3e 74 72 5f 74 6d   pTrigger->tr_tm
1f191 20 3d 20 74 72 5f 74 6d 3d 3d 54 4b 5f 42 45 46   = tr_tm==TK_BEF
1f192 4f 52 45 20 3f 20 54 52 49 47 47 45 52 5f 42 45  ORE ? TRIGGER_BE
1f193 46 4f 52 45 20 3a 20 54 52 49 47 47 45 52 5f 41  FORE : TRIGGER_A
1f194 46 54 45 52 3b 0a 20 20 70 54 72 69 67 67 65 72  FTER;.  pTrigger
1f195 2d 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65  ->pWhen = sqlite
1f196 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 57 68  3ExprDup(db, pWh
1f197 65 6e 29 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  en);.  pTrigger-
1f198 3e 70 43 6f 6c 75 6d 6e 73 20 3d 20 73 71 6c 69  >pColumns = sqli
1f199 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c  te3IdListDup(db,
1f19a 20 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71   pColumns);.  sq
1f19b 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64  lite3TokenCopy(d
1f19c 62 2c 20 26 70 54 72 69 67 67 65 72 2d 3e 6e 61  b, &pTrigger->na
1f19d 6d 65 54 6f 6b 65 6e 2c 70 4e 61 6d 65 29 3b 0a  meToken,pName);.
1f19e 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
1f19f 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3d 3d 30  ->pNewTrigger==0
1f1a0 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e   );.  pParse->pN
1f1a1 65 77 54 72 69 67 67 65 72 20 3d 20 70 54 72 69  ewTrigger = pTri
1f1a2 67 67 65 72 3b 0a 0a 74 72 69 67 67 65 72 5f 63  gger;..trigger_c
1f1a3 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65  leanup:.  sqlite
1f1a4 33 5f 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20  3_free(zName);. 
1f1a5 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
1f1a6 65 6c 65 74 65 28 70 54 61 62 6c 65 4e 61 6d 65  elete(pTableName
1f1a7 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
1f1a8 73 74 44 65 6c 65 74 65 28 70 43 6f 6c 75 6d 6e  stDelete(pColumn
1f1a9 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  s);.  sqlite3Exp
1f1aa 72 44 65 6c 65 74 65 28 70 57 68 65 6e 29 3b 0a  rDelete(pWhen);.
1f1ab 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70    if( !pParse->p
1f1ac 4e 65 77 54 72 69 67 67 65 72 20 29 7b 0a 20 20  NewTrigger ){.  
1f1ad 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
1f1ae 72 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29  rigger(pTrigger)
1f1af 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
1f1b0 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
1f1b1 4e 65 77 54 72 69 67 67 65 72 3d 3d 70 54 72 69  NewTrigger==pTri
1f1b2 67 67 65 72 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  gger );.  }.}../
1f1b3 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1f1b4 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  e is called afte
1f1b5 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 74 72 69  r all of the tri
1f1b6 67 67 65 72 20 61 63 74 69 6f 6e 73 20 68 61 76  gger actions hav
1f1b7 65 20 62 65 65 6e 20 70 61 72 73 65 64 0a 2a 2a  e been parsed.**
1f1b8 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d   in order to com
1f1b9 70 6c 65 74 65 20 74 68 65 20 70 72 6f 63 65 73  plete the proces
1f1ba 73 20 6f 66 20 62 75 69 6c 64 69 6e 67 20 74 68  s of building th
1f1bb 65 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51  e trigger..*/.SQ
1f1bc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1f1bd 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54  d sqlite3FinishT
1f1be 72 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20  rigger(.  Parse 
1f1bf 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
1f1c0 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
1f1c1 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ext */.  Trigger
1f1c2 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73 74 2c  Step *pStepList,
1f1c3 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 65   /* The triggere
1f1c4 64 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 54  d program */.  T
1f1c5 6f 6b 65 6e 20 2a 70 41 6c 6c 20 20 20 20 20 20  oken *pAll      
1f1c6 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20         /* Token 
1f1c7 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
1f1c8 68 65 20 63 6f 6d 70 6c 65 74 65 20 43 52 45 41  he complete CREA
1f1c9 54 45 20 54 52 49 47 47 45 52 20 2a 2f 0a 29 7b  TE TRIGGER */.){
1f1ca 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
1f1cb 67 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 68  g = 0;     /* Th
1f1cc 65 20 74 72 69 67 67 65 72 20 77 68 6f 73 65 20  e trigger whose 
1f1cd 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20  construction is 
1f1ce 66 69 6e 69 73 68 69 6e 67 20 75 70 20 2a 2f 0a  finishing up */.
1f1cf 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1f1d0 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20  pParse->db;  /* 
1f1d1 54 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  The database */.
1f1d2 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a    DbFixer sFix;.
1f1d3 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
1f1d4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f1d5 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  Database contain
1f1d6 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20  ing the trigger 
1f1d7 2a 2f 0a 0a 20 20 70 54 72 69 67 20 3d 20 70 50  */..  pTrig = pP
1f1d8 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
1f1d9 72 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  r;.  pParse->pNe
1f1da 77 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20  wTrigger = 0;.  
1f1db 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
1f1dc 20 7c 7c 20 21 70 54 72 69 67 20 29 20 67 6f 74   || !pTrig ) got
1f1dd 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73 68 5f  o triggerfinish_
1f1de 63 6c 65 61 6e 75 70 3b 0a 20 20 69 44 62 20 3d  cleanup;.  iDb =
1f1df 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1f1e0 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
1f1e1 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61  , pTrig->pSchema
1f1e2 29 3b 0a 20 20 70 54 72 69 67 2d 3e 73 74 65 70  );.  pTrig->step
1f1e3 5f 6c 69 73 74 20 3d 20 70 53 74 65 70 4c 69 73  _list = pStepLis
1f1e4 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65  t;.  while( pSte
1f1e5 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 70 53 74  pList ){.    pSt
1f1e6 65 70 4c 69 73 74 2d 3e 70 54 72 69 67 20 3d 20  epList->pTrig = 
1f1e7 70 54 72 69 67 3b 0a 20 20 20 20 70 53 74 65 70  pTrig;.    pStep
1f1e8 4c 69 73 74 20 3d 20 70 53 74 65 70 4c 69 73 74  List = pStepList
1f1e9 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
1f1ea 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69  f( sqlite3FixIni
1f1eb 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c  t(&sFix, pParse,
1f1ec 20 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c   iDb, "trigger",
1f1ed 20 26 70 54 72 69 67 2d 3e 6e 61 6d 65 54 6f 6b   &pTrig->nameTok
1f1ee 65 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20 26  en) .          &
1f1ef 26 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 67  & sqlite3FixTrig
1f1f0 67 65 72 53 74 65 70 28 26 73 46 69 78 2c 20 70  gerStep(&sFix, p
1f1f1 54 72 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 29  Trig->step_list)
1f1f2 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69   ){.    goto tri
1f1f3 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e  ggerfinish_clean
1f1f4 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66  up;.  }..  /* if
1f1f5 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e 69 74   we are not init
1f1f6 69 61 6c 69 7a 69 6e 67 2c 20 61 6e 64 20 74 68  ializing, and th
1f1f7 69 73 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f  is trigger is no
1f1f8 74 20 6f 6e 20 61 20 54 45 4d 50 20 74 61 62 6c  t on a TEMP tabl
1f1f9 65 2c 20 0a 20 20 2a 2a 20 62 75 69 6c 64 20 74  e, .  ** build t
1f1fa 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
1f1fb 20 65 6e 74 72 79 0a 20 20 2a 2f 0a 20 20 69 66   entry.  */.  if
1f1fc 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
1f1fd 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
1f1fe 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
1f1ff 69 6e 73 65 72 74 54 72 69 67 5b 5d 20 3d 20 7b  insertTrig[] = {
1f200 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 77 52  .      { OP_NewR
1f201 6f 77 69 64 2c 20 20 20 30 2c 20 30 2c 20 20 30  owid,   0, 0,  0
1f202 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20            },.   
1f203 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
1f204 20 20 20 20 30 2c 20 30 2c 20 20 22 74 72 69 67      0, 0,  "trig
1f205 67 65 72 22 20 20 7d 2c 0a 20 20 20 20 20 20 7b  ger"  },.      {
1f206 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
1f207 30 2c 20 30 2c 20 20 30 20 20 20 20 20 20 20 20  0, 0,  0        
1f208 20 20 7d 2c 20 20 2f 2a 20 32 3a 20 74 72 69 67    },  /* 2: trig
1f209 67 65 72 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  ger name */.    
1f20a 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20    { OP_String8, 
1f20b 20 20 20 30 2c 20 30 2c 20 20 30 20 20 20 20 20     0, 0,  0     
1f20c 20 20 20 20 20 7d 2c 20 20 2f 2a 20 33 3a 20 74       },  /* 3: t
1f20d 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  able name */.   
1f20e 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
1f20f 20 20 20 20 30 2c 20 30 2c 20 20 30 20 20 20 20      0, 0,  0    
1f210 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b        },.      {
1f211 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
1f212 30 2c 20 30 2c 20 20 22 43 52 45 41 54 45 20 54  0, 0,  "CREATE T
1f213 52 49 47 47 45 52 20 22 7d 2c 0a 20 20 20 20 20  RIGGER "},.     
1f214 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
1f215 20 20 30 2c 20 30 2c 20 20 30 20 20 20 20 20 20    0, 0,  0      
1f216 20 20 20 20 7d 2c 20 20 2f 2a 20 36 3a 20 53 51      },  /* 6: SQ
1f217 4c 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  L */.      { OP_
1f218 43 6f 6e 63 61 74 2c 20 20 20 20 20 30 2c 20 30  Concat,     0, 0
1f219 2c 20 20 30 20 20 20 20 20 20 20 20 20 20 7d 2c  ,  0          },
1f21a 20 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4d 61 6b   .      { OP_Mak
1f21b 65 52 65 63 6f 72 64 2c 20 35 2c 20 30 2c 20 20  eRecord, 5, 0,  
1f21c 22 61 61 61 64 61 22 20 20 20 20 7d 2c 0a 20 20  "aaada"    },.  
1f21d 20 20 20 20 7b 20 4f 50 5f 49 6e 73 65 72 74 2c      { OP_Insert,
1f21e 20 20 20 20 20 30 2c 20 30 2c 20 20 30 20 20 20       0, 0,  0   
1f21f 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7d 3b         },.    };
1f220 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20  .    int addr;. 
1f221 20 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 20     Vdbe *v;..   
1f222 20 2f 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72   /* Make an entr
1f223 79 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  y in the sqlite_
1f224 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a  master table */.
1f225 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
1f226 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1f227 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67      if( v==0 ) g
1f228 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73  oto triggerfinis
1f229 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 73  h_cleanup;.    s
1f22a 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
1f22b 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
1f22c 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 0, iDb);.    s
1f22d 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72  qlite3OpenMaster
1f22e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
1f22f 62 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73  b);.    addr = s
1f230 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
1f231 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
1f232 28 69 6e 73 65 72 74 54 72 69 67 29 2c 20 69 6e  (insertTrig), in
1f233 73 65 72 74 54 72 69 67 29 3b 0a 20 20 20 20 73  sertTrig);.    s
1f234 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f235 50 33 28 76 2c 20 61 64 64 72 2b 32 2c 20 70 54  P3(v, addr+2, pT
1f236 72 69 67 2d 3e 6e 61 6d 65 2c 20 30 29 3b 20 0a  rig->name, 0); .
1f237 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1f238 68 61 6e 67 65 50 33 28 76 2c 20 61 64 64 72 2b  hangeP3(v, addr+
1f239 33 2c 20 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c  3, pTrig->table,
1f23a 20 30 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65   0); .    sqlite
1f23b 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
1f23c 20 61 64 64 72 2b 36 2c 20 28 63 68 61 72 2a 29   addr+6, (char*)
1f23d 70 41 6c 6c 2d 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e  pAll->z, pAll->n
1f23e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
1f23f 61 6e 67 65 43 6f 6f 6b 69 65 28 64 62 2c 20 76  angeCookie(db, v
1f240 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
1f241 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1f242 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 29 3b  OP_Close, 0, 0);
1f243 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1f244 4f 70 33 28 76 2c 20 4f 50 5f 50 61 72 73 65 53  Op3(v, OP_ParseS
1f245 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 73  chema, iDb, 0, s
1f246 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 0a 20  qlite3MPrintf(. 
1f247 20 20 20 20 20 20 20 64 62 2c 20 22 74 79 70 65         db, "type
1f248 3d 27 74 72 69 67 67 65 72 27 20 41 4e 44 20 6e  ='trigger' AND n
1f249 61 6d 65 3d 27 25 71 27 22 2c 20 70 54 72 69 67  ame='%q'", pTrig
1f24a 2d 3e 6e 61 6d 65 29 2c 20 50 33 5f 44 59 4e 41  ->name), P3_DYNA
1f24b 4d 49 43 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a  MIC.    );.  }..
1f24c 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
1f24d 75 73 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  usy ){.    int n
1f24e 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
1f24f 62 3b 0a 20 20 20 20 54 72 69 67 67 65 72 20 2a  b;.    Trigger *
1f250 70 44 65 6c 3b 0a 20 20 20 20 70 44 65 6c 20 3d  pDel;.    pDel =
1f251 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
1f252 72 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d  rt(&db->aDb[iDb]
1f253 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61  .pSchema->trigHa
1f254 73 68 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  sh, .           
1f255 20 20 20 20 20 20 20 20 20 20 70 54 72 69 67 2d            pTrig-
1f256 3e 6e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 70 54  >name, strlen(pT
1f257 72 69 67 2d 3e 6e 61 6d 65 29 2c 20 70 54 72 69  rig->name), pTri
1f258 67 29 3b 0a 20 20 20 20 69 66 28 20 70 44 65 6c  g);.    if( pDel
1f259 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1f25a 28 20 70 44 65 6c 3d 3d 70 54 72 69 67 20 29 3b  ( pDel==pTrig );
1f25b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
1f25c 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
1f25d 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 66     goto triggerf
1f25e 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20  inish_cleanup;. 
1f25f 20 20 20 7d 0a 20 20 20 20 6e 20 3d 20 73 74 72     }.    n = str
1f260 6c 65 6e 28 70 54 72 69 67 2d 3e 74 61 62 6c 65  len(pTrig->table
1f261 29 20 2b 20 31 3b 0a 20 20 20 20 70 54 61 62 20  ) + 1;.    pTab 
1f262 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
1f263 64 28 26 70 54 72 69 67 2d 3e 70 54 61 62 53 63  d(&pTrig->pTabSc
1f264 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70  hema->tblHash, p
1f265 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b  Trig->table, n);
1f266 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61  .    assert( pTa
1f267 62 21 3d 30 20 29 3b 0a 20 20 20 20 70 54 72 69  b!=0 );.    pTri
1f268 67 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d  g->pNext = pTab-
1f269 3e 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20 70  >pTrigger;.    p
1f26a 54 61 62 2d 3e 70 54 72 69 67 67 65 72 20 3d 20  Tab->pTrigger = 
1f26b 70 54 72 69 67 3b 0a 20 20 20 20 70 54 72 69 67  pTrig;.    pTrig
1f26c 20 3d 20 30 3b 0a 20 20 7d 0a 0a 74 72 69 67 67   = 0;.  }..trigg
1f26d 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70  erfinish_cleanup
1f26e 3a 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74  :.  sqlite3Delet
1f26f 65 54 72 69 67 67 65 72 28 70 54 72 69 67 29 3b  eTrigger(pTrig);
1f270 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 72  .  assert( !pPar
1f271 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20  se->pNewTrigger 
1f272 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65  );.  sqlite3Dele
1f273 74 65 54 72 69 67 67 65 72 53 74 65 70 28 70 53  teTriggerStep(pS
1f274 74 65 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a  tepList);.}../*.
1f275 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f  ** Make a copy o
1f276 66 20 61 6c 6c 20 63 6f 6d 70 6f 6e 65 6e 74 73  f all components
1f277 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 74 72   of the given tr
1f278 69 67 67 65 72 20 73 74 65 70 2e 20 20 54 68 69  igger step.  Thi
1f279 73 20 68 61 73 0a 2a 2a 20 74 68 65 20 65 66 66  s has.** the eff
1f27a 65 63 74 20 6f 66 20 63 6f 70 79 69 6e 67 20 61  ect of copying a
1f27b 6c 6c 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20  ll Expr.token.z 
1f27c 76 61 6c 75 65 73 20 69 6e 74 6f 20 6d 65 6d 6f  values into memo
1f27d 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  ry obtained.** f
1f27e 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
1f27f 6f 63 28 29 2e 20 20 41 73 20 69 6e 69 74 69 61  oc().  As initia
1f280 6c 6c 79 20 63 72 65 61 74 65 64 2c 20 74 68 65  lly created, the
1f281 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61   Expr.token.z va
1f282 6c 75 65 73 0a 2a 2a 20 61 6c 6c 20 70 6f 69 6e  lues.** all poin
1f283 74 20 74 6f 20 74 68 65 20 69 6e 70 75 74 20 73  t to the input s
1f284 74 72 69 6e 67 20 74 68 61 74 20 77 61 73 20 66  tring that was f
1f285 65 64 20 74 6f 20 74 68 65 20 70 61 72 73 65 72  ed to the parser
1f286 2e 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 73  .  But that.** s
1f287 74 72 69 6e 67 20 69 73 20 65 70 68 65 6d 65 72  tring is ephemer
1f288 61 6c 20 2d 20 69 74 20 77 69 6c 6c 20 67 6f 20  al - it will go 
1f289 61 77 61 79 20 61 73 20 73 6f 6f 6e 20 61 73 20  away as soon as 
1f28a 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  the sqlite3_exec
1f28b 28 29 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20  ().** call that 
1f28c 73 74 61 72 74 65 64 20 74 68 65 20 70 61 72 73  started the pars
1f28d 65 72 20 65 78 69 74 73 2e 20 20 54 68 69 73 20  er exits.  This 
1f28e 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73 20 61 20  routine makes a 
1f28f 70 65 72 73 69 73 74 65 6e 74 0a 2a 2a 20 63 6f  persistent.** co
1f290 70 79 20 6f 66 20 61 6c 6c 20 74 68 65 20 45 78  py of all the Ex
1f291 70 72 2e 74 6f 6b 65 6e 2e 7a 20 73 74 72 69 6e  pr.token.z strin
1f292 67 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 54  gs so that the T
1f293 72 69 67 67 65 72 53 74 65 70 20 73 74 72 75 63  riggerStep struc
1f294 74 75 72 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  ture.** will be 
1f295 76 61 6c 69 64 20 65 76 65 6e 20 61 66 74 65 72  valid even after
1f296 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65   the sqlite3_exe
1f297 63 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  c() call returns
1f298 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1f299 20 73 71 6c 69 74 65 50 65 72 73 69 73 74 54 72   sqlitePersistTr
1f29a 69 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65  iggerStep(sqlite
1f29b 33 20 2a 64 62 2c 20 54 72 69 67 67 65 72 53 74  3 *db, TriggerSt
1f29c 65 70 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  ep *p){.  if( p-
1f29d 3e 74 61 72 67 65 74 2e 7a 20 29 7b 0a 20 20 20  >target.z ){.   
1f29e 20 70 2d 3e 74 61 72 67 65 74 2e 7a 20 3d 20 28   p->target.z = (
1f29f 75 38 2a 29 73 71 6c 69 74 65 33 44 62 53 74 72  u8*)sqlite3DbStr
1f2a0 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
1f2a1 70 2d 3e 74 61 72 67 65 74 2e 7a 2c 20 70 2d 3e  p->target.z, p->
1f2a2 74 61 72 67 65 74 2e 6e 29 3b 0a 20 20 20 20 70  target.n);.    p
1f2a3 2d 3e 74 61 72 67 65 74 2e 64 79 6e 20 3d 20 31  ->target.dyn = 1
1f2a4 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
1f2a5 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 53 65  Select ){.    Se
1f2a6 6c 65 63 74 20 2a 70 4e 65 77 20 3d 20 73 71 6c  lect *pNew = sql
1f2a7 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
1f2a8 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  , p->pSelect);. 
1f2a9 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
1f2aa 44 65 6c 65 74 65 28 70 2d 3e 70 53 65 6c 65 63  Delete(p->pSelec
1f2ab 74 29 3b 0a 20 20 20 20 70 2d 3e 70 53 65 6c 65  t);.    p->pSele
1f2ac 63 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20  ct = pNew;.  }. 
1f2ad 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29   if( p->pWhere )
1f2ae 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77  {.    Expr *pNew
1f2af 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
1f2b0 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29  p(db, p->pWhere)
1f2b1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
1f2b2 72 44 65 6c 65 74 65 28 70 2d 3e 70 57 68 65 72  rDelete(p->pWher
1f2b3 65 29 3b 0a 20 20 20 20 70 2d 3e 70 57 68 65 72  e);.    p->pWher
1f2b4 65 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20  e = pNew;.  }.  
1f2b5 69 66 28 20 70 2d 3e 70 45 78 70 72 4c 69 73 74  if( p->pExprList
1f2b6 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74   ){.    ExprList
1f2b7 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
1f2b8 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
1f2b9 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a 20  p->pExprList);. 
1f2ba 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
1f2bb 73 74 44 65 6c 65 74 65 28 70 2d 3e 70 45 78 70  stDelete(p->pExp
1f2bc 72 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70  rList);.    p->p
1f2bd 45 78 70 72 4c 69 73 74 20 3d 20 70 4e 65 77 3b  ExprList = pNew;
1f2be 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 49  .  }.  if( p->pI
1f2bf 64 4c 69 73 74 20 29 7b 0a 20 20 20 20 49 64 4c  dList ){.    IdL
1f2c0 69 73 74 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69  ist *pNew = sqli
1f2c1 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c  te3IdListDup(db,
1f2c2 20 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a 20 20   p->pIdList);.  
1f2c3 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
1f2c4 65 6c 65 74 65 28 70 2d 3e 70 49 64 4c 69 73 74  elete(p->pIdList
1f2c5 29 3b 0a 20 20 20 20 70 2d 3e 70 49 64 4c 69 73  );.    p->pIdLis
1f2c6 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a  t = pNew;.  }.}.
1f2c7 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 53 45  ./*.** Turn a SE
1f2c8 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 28  LECT statement (
1f2c9 74 68 61 74 20 74 68 65 20 70 53 65 6c 65 63 74  that the pSelect
1f2ca 20 70 61 72 61 6d 65 74 65 72 20 70 6f 69 6e 74   parameter point
1f2cb 73 20 74 6f 29 20 69 6e 74 6f 0a 2a 2a 20 61 20  s to) into.** a 
1f2cc 74 72 69 67 67 65 72 20 73 74 65 70 2e 20 20 52  trigger step.  R
1f2cd 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1f2ce 74 6f 20 61 20 54 72 69 67 67 65 72 53 74 65 70  to a TriggerStep
1f2cf 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
1f2d0 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
1f2d1 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
1f2d2 77 68 65 6e 20 69 74 20 66 69 6e 64 73 20 61 20  when it finds a 
1f2d3 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1f2d4 20 69 6e 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61   in.** body of a
1f2d5 20 54 52 49 47 47 45 52 2e 20 20 0a 2a 2f 0a 53   TRIGGER.  .*/.S
1f2d6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72  QLITE_PRIVATE Tr
1f2d7 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
1f2d8 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53  e3TriggerSelectS
1f2d9 74 65 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  tep(sqlite3 *db,
1f2da 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
1f2db 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  ){.  TriggerStep
1f2dc 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 20 3d   *pTriggerStep =
1f2dd 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1f2de 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
1f2df 54 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20  TriggerStep));. 
1f2e0 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74 65   if( pTriggerSte
1f2e1 70 3d 3d 30 20 29 20 7b 0a 20 20 20 20 73 71 6c  p==0 ) {.    sql
1f2e2 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
1f2e3 28 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72  (pSelect);.    r
1f2e4 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
1f2e5 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70  pTriggerStep->op
1f2e6 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20   = TK_SELECT;.  
1f2e7 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53  pTriggerStep->pS
1f2e8 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 3b  elect = pSelect;
1f2e9 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
1f2ea 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66  >orconf = OE_Def
1f2eb 61 75 6c 74 3b 0a 20 20 73 71 6c 69 74 65 50 65  ault;.  sqlitePe
1f2ec 72 73 69 73 74 54 72 69 67 67 65 72 53 74 65 70  rsistTriggerStep
1f2ed 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74 65  (db, pTriggerSte
1f2ee 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54  p);..  return pT
1f2ef 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f  riggerStep;.}../
1f2f0 2a 0a 2a 2a 20 42 75 69 6c 64 20 61 20 74 72 69  *.** Build a tri
1f2f1 67 67 65 72 20 73 74 65 70 20 6f 75 74 20 6f 66  gger step out of
1f2f2 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65   an INSERT state
1f2f3 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20  ment.  Return a 
1f2f4 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68  pointer.** to th
1f2f5 65 20 6e 65 77 20 74 72 69 67 67 65 72 20 73 74  e new trigger st
1f2f6 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  ep..**.** The pa
1f2f7 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
1f2f8 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20  routine when it 
1f2f9 73 65 65 73 20 61 6e 20 49 4e 53 45 52 54 20 69  sees an INSERT i
1f2fa 6e 73 69 64 65 20 74 68 65 0a 2a 2a 20 62 6f 64  nside the.** bod
1f2fb 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a  y of a trigger..
1f2fc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f2fd 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73  E TriggerStep *s
1f2fe 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73  qlite3TriggerIns
1f2ff 65 72 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74  ertStep(.  sqlit
1f300 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f  e3 *db,        /
1f301 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1f302 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54  onnection */.  T
1f303 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65  oken *pTableName
1f304 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  ,  /* Name of th
1f305 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  e table into whi
1f306 63 68 20 77 65 20 69 6e 73 65 72 74 20 2a 2f 0a  ch we insert */.
1f307 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d    IdList *pColum
1f308 6e 2c 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  n,    /* List of
1f309 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 61 62   columns in pTab
1f30a 6c 65 4e 61 6d 65 20 74 6f 20 69 6e 73 65 72 74  leName to insert
1f30b 20 69 6e 74 6f 20 2a 2f 0a 20 20 45 78 70 72 4c   into */.  ExprL
1f30c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 2f  ist *pEList,   /
1f30d 2a 20 54 68 65 20 56 41 4c 55 45 20 63 6c 61 75  * The VALUE clau
1f30e 73 65 3a 20 61 20 6c 69 73 74 20 6f 66 20 76 61  se: a list of va
1f30f 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72  lues to be inser
1f310 74 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ted */.  Select 
1f311 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 2f 2a 20  *pSelect,    /* 
1f312 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
1f313 6e 74 20 74 68 61 74 20 73 75 70 70 6c 69 65 73  nt that supplies
1f314 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74   values */.  int
1f315 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20   orconf         
1f316 20 2f 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63 74   /* The conflict
1f317 20 61 6c 67 6f 72 69 74 68 6d 20 28 4f 45 5f 41   algorithm (OE_A
1f318 62 6f 72 74 2c 20 4f 45 5f 52 65 70 6c 61 63 65  bort, OE_Replace
1f319 2c 20 65 74 63 2e 29 20 2a 2f 0a 29 7b 0a 20 20  , etc.) */.){.  
1f31a 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
1f31b 69 67 67 65 72 53 74 65 70 3b 0a 0a 20 20 61 73  iggerStep;..  as
1f31c 73 65 72 74 28 70 45 4c 69 73 74 20 3d 3d 20 30  sert(pEList == 0
1f31d 20 7c 7c 20 70 53 65 6c 65 63 74 20 3d 3d 20 30   || pSelect == 0
1f31e 29 3b 0a 20 20 61 73 73 65 72 74 28 70 45 4c 69  );.  assert(pELi
1f31f 73 74 20 21 3d 20 30 20 7c 7c 20 70 53 65 6c 65  st != 0 || pSele
1f320 63 74 20 21 3d 20 30 20 7c 7c 20 64 62 2d 3e 6d  ct != 0 || db->m
1f321 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 0a 20  allocFailed);.. 
1f322 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20   pTriggerStep = 
1f323 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
1f324 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
1f325 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20  riggerStep));.  
1f326 69 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70  if( pTriggerStep
1f327 20 29 7b 0a 20 20 20 20 70 54 72 69 67 67 65 72   ){.    pTrigger
1f328 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e  Step->op = TK_IN
1f329 53 45 52 54 3b 0a 20 20 20 20 70 54 72 69 67 67  SERT;.    pTrigg
1f32a 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20  erStep->pSelect 
1f32b 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70  = pSelect;.    p
1f32c 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
1f32d 67 65 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61  get  = *pTableNa
1f32e 6d 65 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  me;.    pTrigger
1f32f 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 20 3d 20  Step->pIdList = 
1f330 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 54 72  pColumn;.    pTr
1f331 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72  iggerStep->pExpr
1f332 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20  List = pEList;. 
1f333 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
1f334 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66  >orconf = orconf
1f335 3b 0a 20 20 20 20 73 71 6c 69 74 65 50 65 72 73  ;.    sqlitePers
1f336 69 73 74 54 72 69 67 67 65 72 53 74 65 70 28 64  istTriggerStep(d
1f337 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29  b, pTriggerStep)
1f338 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
1f339 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
1f33a 74 65 28 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  te(pColumn);.   
1f33b 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
1f33c 44 65 6c 65 74 65 28 70 45 4c 69 73 74 29 3b 0a  Delete(pEList);.
1f33d 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
1f33e 74 44 65 6c 65 74 65 28 70 53 65 6c 65 63 74 29  tDelete(pSelect)
1f33f 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
1f340 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a  pTriggerStep;.}.
1f341 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74  ./*.** Construct
1f342 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70 20   a trigger step 
1f343 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
1f344 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
1f345 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a  ent and return.*
1f346 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  * a pointer to t
1f347 68 61 74 20 74 72 69 67 67 65 72 20 73 74 65 70  hat trigger step
1f348 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63 61  .  The parser ca
1f349 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
1f34a 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65 73   when it.** sees
1f34b 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
1f34c 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65 20  ment inside the 
1f34d 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54 45  body of a CREATE
1f34e 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c   TRIGGER..*/.SQL
1f34f 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67  ITE_PRIVATE Trig
1f350 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
1f351 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65  TriggerUpdateSte
1f352 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  p(.  sqlite3 *db
1f353 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
1f354 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1f355 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  tion */.  Token 
1f356 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 2f  *pTableName,   /
1f357 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
1f358 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61 74 65  ble to be update
1f359 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  d */.  ExprList 
1f35a 2a 70 45 4c 69 73 74 2c 20 20 20 20 2f 2a 20 54  *pEList,    /* T
1f35b 68 65 20 53 45 54 20 63 6c 61 75 73 65 3a 20 6c  he SET clause: l
1f35c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 61 6e  ist of column an
1f35d 64 20 6e 65 77 20 76 61 6c 75 65 73 20 2a 2f 0a  d new values */.
1f35e 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20    Expr *pWhere, 
1f35f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
1f360 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
1f361 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20  int orconf      
1f362 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 66       /* The conf
1f363 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 2e 20  lict algorithm. 
1f364 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67  (OE_Abort, OE_Ig
1f365 6e 6f 72 65 2c 20 65 74 63 29 20 2a 2f 0a 29 7b  nore, etc) */.){
1f366 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
1f367 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73  pTriggerStep = s
1f368 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1f369 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72  ro(db, sizeof(Tr
1f36a 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69  iggerStep));.  i
1f36b 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70 3d  f( pTriggerStep=
1f36c 3d 30 20 29 7b 0a 20 20 20 20 20 73 71 6c 69 74  =0 ){.     sqlit
1f36d 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
1f36e 28 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 73  (pEList);.     s
1f36f 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1f370 28 70 57 68 65 72 65 29 3b 0a 20 20 20 20 20 72  (pWhere);.     r
1f371 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
1f372 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70  pTriggerStep->op
1f373 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20   = TK_UPDATE;.  
1f374 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61  pTriggerStep->ta
1f375 72 67 65 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e  rget  = *pTableN
1f376 61 6d 65 3b 0a 20 20 70 54 72 69 67 67 65 72 53  ame;.  pTriggerS
1f377 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d  tep->pExprList =
1f378 20 70 45 4c 69 73 74 3b 0a 20 20 70 54 72 69 67   pEList;.  pTrig
1f379 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20  gerStep->pWhere 
1f37a 3d 20 70 57 68 65 72 65 3b 0a 20 20 70 54 72 69  = pWhere;.  pTri
1f37b 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66  ggerStep->orconf
1f37c 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 73 71 6c   = orconf;.  sql
1f37d 69 74 65 50 65 72 73 69 73 74 54 72 69 67 67 65  itePersistTrigge
1f37e 72 53 74 65 70 28 64 62 2c 20 70 54 72 69 67 67  rStep(db, pTrigg
1f37f 65 72 53 74 65 70 29 3b 0a 0a 20 20 72 65 74 75  erStep);..  retu
1f380 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b  rn pTriggerStep;
1f381 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72  .}../*.** Constr
1f382 75 63 74 20 61 20 74 72 69 67 67 65 72 20 73 74  uct a trigger st
1f383 65 70 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ep that implemen
1f384 74 73 20 61 20 44 45 4c 45 54 45 20 73 74 61 74  ts a DELETE stat
1f385 65 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e  ement and return
1f386 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
1f387 20 74 68 61 74 20 74 72 69 67 67 65 72 20 73 74   that trigger st
1f388 65 70 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  ep.  The parser 
1f389 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
1f38a 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65  ne when it.** se
1f38b 65 73 20 61 20 44 45 4c 45 54 45 20 73 74 61 74  es a DELETE stat
1f38c 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65  ement inside the
1f38d 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54   body of a CREAT
1f38e 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51  E TRIGGER..*/.SQ
1f38f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69  LITE_PRIVATE Tri
1f390 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
1f391 33 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74  3TriggerDeleteSt
1f392 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ep(.  sqlite3 *d
1f393 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
1f394 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
1f395 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  tion */.  Token 
1f396 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20  *pTableName,    
1f397 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66    /* The table f
1f398 72 6f 6d 20 77 68 69 63 68 20 72 6f 77 73 20 61  rom which rows a
1f399 72 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  re deleted */.  
1f39a 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20  Expr *pWhere    
1f39b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
1f39c 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 29  HERE clause */.)
1f39d 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
1f39e 2a 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20  *pTriggerStep = 
1f39f 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
1f3a0 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
1f3a1 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20  riggerStep));.  
1f3a2 69 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70  if( pTriggerStep
1f3a3 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1f3a4 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 57 68  e3ExprDelete(pWh
1f3a5 65 72 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ere);.    return
1f3a6 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54 72 69 67   0;.  }..  pTrig
1f3a7 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b  gerStep->op = TK
1f3a8 5f 44 45 4c 45 54 45 3b 0a 20 20 70 54 72 69 67  _DELETE;.  pTrig
1f3a9 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74 20  gerStep->target 
1f3aa 20 3d 20 2a 70 54 61 62 6c 65 4e 61 6d 65 3b 0a   = *pTableName;.
1f3ab 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
1f3ac 70 57 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b  pWhere = pWhere;
1f3ad 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
1f3ae 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66  >orconf = OE_Def
1f3af 61 75 6c 74 3b 0a 20 20 73 71 6c 69 74 65 50 65  ault;.  sqlitePe
1f3b0 72 73 69 73 74 54 72 69 67 67 65 72 53 74 65 70  rsistTriggerStep
1f3b1 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74 65  (db, pTriggerSte
1f3b2 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54  p);..  return pT
1f3b3 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f  riggerStep;.}../
1f3b4 2a 20 0a 2a 2a 20 52 65 63 75 72 73 69 76 65 6c  * .** Recursivel
1f3b5 79 20 64 65 6c 65 74 65 20 61 20 54 72 69 67 67  y delete a Trigg
1f3b6 65 72 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a  er structure.*/.
1f3b7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1f3b8 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
1f3b9 65 54 72 69 67 67 65 72 28 54 72 69 67 67 65 72  eTrigger(Trigger
1f3ba 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 69   *pTrigger){.  i
1f3bb 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20 29  f( pTrigger==0 )
1f3bc 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
1f3bd 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53  e3DeleteTriggerS
1f3be 74 65 70 28 70 54 72 69 67 67 65 72 2d 3e 73 74  tep(pTrigger->st
1f3bf 65 70 5f 6c 69 73 74 29 3b 0a 20 20 73 71 6c 69  ep_list);.  sqli
1f3c0 74 65 33 5f 66 72 65 65 28 70 54 72 69 67 67 65  te3_free(pTrigge
1f3c1 72 2d 3e 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  r->name);.  sqli
1f3c2 74 65 33 5f 66 72 65 65 28 70 54 72 69 67 67 65  te3_free(pTrigge
1f3c3 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 20 73 71 6c  r->table);.  sql
1f3c4 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
1f3c5 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 29 3b  Trigger->pWhen);
1f3c6 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  .  sqlite3IdList
1f3c7 44 65 6c 65 74 65 28 70 54 72 69 67 67 65 72 2d  Delete(pTrigger-
1f3c8 3e 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 69 66  >pColumns);.  if
1f3c9 28 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65  ( pTrigger->name
1f3ca 54 6f 6b 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69  Token.dyn ) sqli
1f3cb 74 65 33 5f 66 72 65 65 28 28 63 68 61 72 2a 29  te3_free((char*)
1f3cc 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f  pTrigger->nameTo
1f3cd 6b 65 6e 2e 7a 29 3b 0a 20 20 73 71 6c 69 74 65  ken.z);.  sqlite
1f3ce 33 5f 66 72 65 65 28 70 54 72 69 67 67 65 72 29  3_free(pTrigger)
1f3cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
1f3d0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1f3d1 65 64 20 74 6f 20 64 72 6f 70 20 61 20 74 72 69  ed to drop a tri
1f3d2 67 67 65 72 20 66 72 6f 6d 20 74 68 65 20 64 61  gger from the da
1f3d3 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 0a  tabase schema. .
1f3d4 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20 62  **.** This may b
1f3d5 65 20 63 61 6c 6c 65 64 20 64 69 72 65 63 74 6c  e called directl
1f3d6 79 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65  y from the parse
1f3d7 72 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20  r and therefore 
1f3d8 69 64 65 6e 74 69 66 69 65 73 0a 2a 2a 20 74 68  identifies.** th
1f3d9 65 20 74 72 69 67 67 65 72 20 62 79 20 6e 61 6d  e trigger by nam
1f3da 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 44  e.  The sqlite3D
1f3db 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 29 20  ropTriggerPtr() 
1f3dc 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65  routine does the
1f3dd 0a 2a 2a 20 73 61 6d 65 20 6a 6f 62 20 61 73 20  .** same job as 
1f3de 74 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 63  this routine exc
1f3df 65 70 74 20 69 74 20 74 61 6b 65 73 20 61 20 70  ept it takes a p
1f3e0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74 72  ointer to the tr
1f3e1 69 67 67 65 72 0a 2a 2a 20 69 6e 73 74 65 61 64  igger.** instead
1f3e2 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20   of the trigger 
1f3e3 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53 51 4c 49 54 45  name..**/.SQLITE
1f3e4 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1f3e5 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72  lite3DropTrigger
1f3e6 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1f3e7 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20  SrcList *pName, 
1f3e8 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 72  int noErr){.  Tr
1f3e9 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 20  igger *pTrigger 
1f3ea 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  = 0;.  int i;.  
1f3eb 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
1f3ec 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1f3ed 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d  Name;.  int nNam
1f3ee 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e;.  sqlite3 *db
1f3ef 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
1f3f0 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
1f3f1 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 64 72  Failed ) goto dr
1f3f2 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  op_trigger_clean
1f3f3 75 70 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45  up;.  if( SQLITE
1f3f4 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
1f3f5 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
1f3f6 7b 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f  {.    goto drop_
1f3f7 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
1f3f8 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
1f3f9 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29  pName->nSrc==1 )
1f3fa 3b 0a 20 20 7a 44 62 20 3d 20 70 4e 61 6d 65 2d  ;.  zDb = pName-
1f3fb 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 3b  >a[0].zDatabase;
1f3fc 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 4e 61 6d 65  .  zName = pName
1f3fd 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->a[0].zName;.  
1f3fe 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a  nName = strlen(z
1f3ff 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f  Name);.  for(i=O
1f400 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62  MIT_TEMPDB; i<db
1f401 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
1f402 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f   int j = (i<2) ?
1f403 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65   i^1 : i;  /* Se
1f404 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65  arch TEMP before
1f405 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28   MAIN */.    if(
1f406 20 7a 44 62 20 26 26 20 73 71 6c 69 74 65 33 53   zDb && sqlite3S
1f407 74 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 6a  trICmp(db->aDb[j
1f408 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 20  ].zName, zDb) ) 
1f409 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 54  continue;.    pT
1f40a 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33  rigger = sqlite3
1f40b 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61  HashFind(&(db->a
1f40c 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  Db[j].pSchema->t
1f40d 72 69 67 48 61 73 68 29 2c 20 7a 4e 61 6d 65 2c  rigHash), zName,
1f40e 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   nName);.    if(
1f40f 20 70 54 72 69 67 67 65 72 20 29 20 62 72 65 61   pTrigger ) brea
1f410 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 54  k;.  }.  if( !pT
1f411 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66  rigger ){.    if
1f412 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20  ( !noErr ){.    
1f413 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1f414 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75  g(pParse, "no su
1f415 63 68 20 74 72 69 67 67 65 72 3a 20 25 53 22 2c  ch trigger: %S",
1f416 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20   pName, 0);.    
1f417 7d 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f  }.    goto drop_
1f418 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
1f419 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 72  .  }.  sqlite3Dr
1f41a 6f 70 54 72 69 67 67 65 72 50 74 72 28 70 50 61  opTriggerPtr(pPa
1f41b 72 73 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a  rse, pTrigger);.
1f41c 0a 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c  .drop_trigger_cl
1f41d 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33  eanup:.  sqlite3
1f41e 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 4e  SrcListDelete(pN
1f41f 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ame);.}../*.** R
1f420 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1f421 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  to the Table str
1f422 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 74  ucture for the t
1f423 61 62 6c 65 20 74 68 61 74 20 61 20 74 72 69 67  able that a trig
1f424 67 65 72 0a 2a 2a 20 69 73 20 73 65 74 20 6f 6e  ger.** is set on
1f425 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61 62 6c  ..*/.static Tabl
1f426 65 20 2a 74 61 62 6c 65 4f 66 54 72 69 67 67 65  e *tableOfTrigge
1f427 72 28 54 72 69 67 67 65 72 20 2a 70 54 72 69 67  r(Trigger *pTrig
1f428 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  ger){.  int n = 
1f429 73 74 72 6c 65 6e 28 70 54 72 69 67 67 65 72 2d  strlen(pTrigger-
1f42a 3e 74 61 62 6c 65 29 20 2b 20 31 3b 0a 20 20 72  >table) + 1;.  r
1f42b 65 74 75 72 6e 20 73 71 6c 69 74 65 33 48 61 73  eturn sqlite3Has
1f42c 68 46 69 6e 64 28 26 70 54 72 69 67 67 65 72 2d  hFind(&pTrigger-
1f42d 3e 70 54 61 62 53 63 68 65 6d 61 2d 3e 74 62 6c  >pTabSchema->tbl
1f42e 48 61 73 68 2c 20 70 54 72 69 67 67 65 72 2d 3e  Hash, pTrigger->
1f42f 74 61 62 6c 65 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f  table, n);.}.../
1f430 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 74 72 69 67  *.** Drop a trig
1f431 67 65 72 20 67 69 76 65 6e 20 61 20 70 6f 69 6e  ger given a poin
1f432 74 65 72 20 74 6f 20 74 68 61 74 20 74 72 69 67  ter to that trig
1f433 67 65 72 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ger. .*/.SQLITE_
1f434 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1f435 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50  ite3DropTriggerP
1f436 74 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  tr(Parse *pParse
1f437 2c 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67  , Trigger *pTrig
1f438 67 65 72 29 7b 0a 20 20 54 61 62 6c 65 20 20 20  ger){.  Table   
1f439 2a 70 54 61 62 6c 65 3b 0a 20 20 56 64 62 65 20  *pTable;.  Vdbe 
1f43a 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *v;.  sqlite3 *d
1f43b 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1f43c 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 44    int iDb;..  iD
1f43d 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1f43e 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
1f43f 3e 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70  >db, pTrigger->p
1f440 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72  Schema);.  asser
1f441 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
1f442 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 54  <db->nDb );.  pT
1f443 61 62 6c 65 20 3d 20 74 61 62 6c 65 4f 66 54 72  able = tableOfTr
1f444 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29 3b  igger(pTrigger);
1f445 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
1f446 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
1f447 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 3d 3d  Table->pSchema==
1f448 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d  pTrigger->pSchem
1f449 61 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b 0a 23  a || iDb==1 );.#
1f44a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f44b 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
1f44c 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64  .  {.    int cod
1f44d 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
1f44e 54 52 49 47 47 45 52 3b 0a 20 20 20 20 63 6f 6e  TRIGGER;.    con
1f44f 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64  st char *zDb = d
1f450 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1f451 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
1f452 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41  r *zTab = SCHEMA
1f453 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20  _TABLE(iDb);.   
1f454 20 69 66 28 20 69 44 62 3d 3d 31 20 29 20 63 6f   if( iDb==1 ) co
1f455 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50  de = SQLITE_DROP
1f456 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 3b 0a 20  _TEMP_TRIGGER;. 
1f457 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
1f458 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
1f459 63 6f 64 65 2c 20 70 54 72 69 67 67 65 72 2d 3e  code, pTrigger->
1f45a 6e 61 6d 65 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e  name, pTable->zN
1f45b 61 6d 65 2c 20 7a 44 62 29 20 7c 7c 0a 20 20 20  ame, zDb) ||.   
1f45c 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 68     sqlite3AuthCh
1f45d 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
1f45e 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c  TE_DELETE, zTab,
1f45f 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
1f460 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
1f461 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1f462 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
1f463 6f 20 64 65 73 74 72 6f 79 20 74 68 65 20 64 61  o destroy the da
1f464 74 61 62 61 73 65 20 72 65 63 6f 72 64 20 6f 66  tabase record of
1f465 20 74 68 65 20 74 72 69 67 67 65 72 2e 0a 20 20   the trigger..  
1f466 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  */.  assert( pTa
1f467 62 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ble!=0 );.  if( 
1f468 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  (v = sqlite3GetV
1f469 64 62 65 28 70 50 61 72 73 65 29 29 21 3d 30 20  dbe(pParse))!=0 
1f46a 29 7b 0a 20 20 20 20 69 6e 74 20 62 61 73 65 3b  ){.    int base;
1f46b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
1f46c 74 20 56 64 62 65 4f 70 4c 69 73 74 20 64 72 6f  t VdbeOpList dro
1f46d 70 54 72 69 67 67 65 72 5b 5d 20 3d 20 7b 0a 20  pTrigger[] = {. 
1f46e 20 20 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64       { OP_Rewind
1f46f 2c 20 20 20 20 20 30 2c 20 41 44 44 52 28 39 29  ,     0, ADDR(9)
1f470 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f  ,  0},.      { O
1f471 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 30 2c  P_String8,    0,
1f472 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 2f   0,        0}, /
1f473 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f  * 1 */.      { O
1f474 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 30 2c  P_Column,     0,
1f475 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20   1,        0},. 
1f476 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20       { OP_Ne,   
1f477 20 20 20 20 20 20 30 2c 20 41 44 44 52 28 38 29        0, ADDR(8)
1f478 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f  ,  0},.      { O
1f479 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 30 2c  P_String8,    0,
1f47a 20 30 2c 20 20 20 20 20 20 20 20 22 74 72 69 67   0,        "trig
1f47b 67 65 72 22 7d 2c 0a 20 20 20 20 20 20 7b 20 4f  ger"},.      { O
1f47c 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 30 2c  P_Column,     0,
1f47d 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20   0,        0},. 
1f47e 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20       { OP_Ne,   
1f47f 20 20 20 20 20 20 30 2c 20 41 44 44 52 28 38 29        0, ADDR(8)
1f480 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f  ,  0},.      { O
1f481 50 5f 44 65 6c 65 74 65 2c 20 20 20 20 20 30 2c  P_Delete,     0,
1f482 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20   0,        0},. 
1f483 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20       { OP_Next, 
1f484 20 20 20 20 20 20 30 2c 20 41 44 44 52 28 31 29        0, ADDR(1)
1f485 2c 20 20 30 7d 2c 20 2f 2a 20 38 20 2a 2f 0a 20  ,  0}, /* 8 */. 
1f486 20 20 20 7d 3b 0a 0a 20 20 20 20 73 71 6c 69 74     };..    sqlit
1f487 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
1f488 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
1f489 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
1f48a 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c  e3OpenMasterTabl
1f48b 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
1f48c 20 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69 74      base = sqlit
1f48d 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
1f48e 76 2c 20 20 41 72 72 61 79 53 69 7a 65 28 64 72  v,  ArraySize(dr
1f48f 6f 70 54 72 69 67 67 65 72 29 2c 20 64 72 6f 70  opTrigger), drop
1f490 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 73 71  Trigger);.    sq
1f491 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1f492 33 28 76 2c 20 62 61 73 65 2b 31 2c 20 70 54 72  3(v, base+1, pTr
1f493 69 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 30 29 3b  igger->name, 0);
1f494 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e  .    sqlite3Chan
1f495 67 65 43 6f 6f 6b 69 65 28 64 62 2c 20 76 2c 20  geCookie(db, v, 
1f496 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
1f497 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1f498 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20  _Close, 0, 0);. 
1f499 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70     sqlite3VdbeOp
1f49a 33 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67  3(v, OP_DropTrig
1f49b 67 65 72 2c 20 69 44 62 2c 20 30 2c 20 70 54 72  ger, iDb, 0, pTr
1f49c 69 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 30 29 3b  igger->name, 0);
1f49d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
1f49e 6d 6f 76 65 20 61 20 74 72 69 67 67 65 72 20 66  move a trigger f
1f49f 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
1f4a0 6c 65 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74  les of the sqlit
1f4a1 65 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  e* pointer..*/.S
1f4a2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1f4a3 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
1f4a4 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72  AndDeleteTrigger
1f4a5 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
1f4a6 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
1f4a7 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 54 72 69  r *zName){.  Tri
1f4a8 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a  gger *pTrigger;.
1f4a9 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 74    int nName = st
1f4aa 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 70  rlen(zName);.  p
1f4ab 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
1f4ac 33 48 61 73 68 49 6e 73 65 72 74 28 26 28 64 62  3HashInsert(&(db
1f4ad 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
1f4ae 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 2c 0a 20  ma->trigHash),. 
1f4af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e                zN
1f4b1 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a  ame, nName, 0);.
1f4b2 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29    if( pTrigger )
1f4b3 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  {.    Table *pTa
1f4b4 62 6c 65 20 3d 20 74 61 62 6c 65 4f 66 54 72 69  ble = tableOfTri
1f4b5 67 67 65 72 28 70 54 72 69 67 67 65 72 29 3b 0a  gger(pTrigger);.
1f4b6 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
1f4b7 6c 65 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  le!=0 );.    if(
1f4b8 20 70 54 61 62 6c 65 2d 3e 70 54 72 69 67 67 65   pTable->pTrigge
1f4b9 72 20 3d 3d 20 70 54 72 69 67 67 65 72 20 29 7b  r == pTrigger ){
1f4ba 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70  .      pTable->p
1f4bb 54 72 69 67 67 65 72 20 3d 20 70 54 72 69 67 67  Trigger = pTrigg
1f4bc 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  er->pNext;.    }
1f4bd 65 6c 73 65 7b 0a 20 20 20 20 20 20 54 72 69 67  else{.      Trig
1f4be 67 65 72 20 2a 63 63 20 3d 20 70 54 61 62 6c 65  ger *cc = pTable
1f4bf 2d 3e 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20  ->pTrigger;.    
1f4c0 20 20 77 68 69 6c 65 28 20 63 63 20 29 7b 20 0a    while( cc ){ .
1f4c1 20 20 20 20 20 20 20 20 69 66 28 20 63 63 2d 3e          if( cc->
1f4c2 70 4e 65 78 74 20 3d 3d 20 70 54 72 69 67 67 65  pNext == pTrigge
1f4c3 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  r ){.          c
1f4c4 63 2d 3e 70 4e 65 78 74 20 3d 20 63 63 2d 3e 70  c->pNext = cc->p
1f4c5 4e 65 78 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Next->pNext;.   
1f4c6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1f4c7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1f4c8 63 63 20 3d 20 63 63 2d 3e 70 4e 65 78 74 3b 0a  cc = cc->pNext;.
1f4c9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
1f4ca 73 65 72 74 28 63 63 29 3b 0a 20 20 20 20 7d 0a  sert(cc);.    }.
1f4cb 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
1f4cc 65 54 72 69 67 67 65 72 28 70 54 72 69 67 67 65  eTrigger(pTrigge
1f4cd 72 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67  r);.    db->flag
1f4ce 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
1f4cf 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 0a 7d  rnChanges;.  }.}
1f4d0 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69  ../*.** pEList i
1f4d1 73 20 74 68 65 20 53 45 54 20 63 6c 61 75 73 65  s the SET clause
1f4d2 20 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73 74   of an UPDATE st
1f4d3 61 74 65 6d 65 6e 74 2e 20 20 45 61 63 68 20 65  atement.  Each e
1f4d4 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 45 4c 69 73  ntry.** in pELis
1f4d5 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  t is of the form
1f4d6 61 74 20 3c 69 64 3e 3d 3c 65 78 70 72 3e 2e 20  at <id>=<expr>. 
1f4d7 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 65   If any of the e
1f4d8 6e 74 72 69 65 73 0a 2a 2a 20 69 6e 20 70 45 4c  ntries.** in pEL
1f4d9 69 73 74 20 68 61 76 65 20 61 6e 20 3c 69 64 3e  ist have an <id>
1f4da 20 77 68 69 63 68 20 6d 61 74 63 68 65 73 20 61   which matches a
1f4db 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 6e 20  n identifier in 
1f4dc 70 49 64 4c 69 73 74 2c 0a 2a 2a 20 74 68 65 6e  pIdList,.** then
1f4dd 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49   return TRUE.  I
1f4de 66 20 70 49 64 4c 69 73 74 3d 3d 4e 55 4c 4c 2c  f pIdList==NULL,
1f4df 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 6e 73   then it is cons
1f4e0 69 64 65 72 65 64 20 61 0a 2a 2a 20 77 69 6c 64  idered a.** wild
1f4e1 63 61 72 64 20 74 68 61 74 20 6d 61 74 63 68 65  card that matche
1f4e2 73 20 61 6e 79 74 68 69 6e 67 2e 20 20 4c 69 6b  s anything.  Lik
1f4e3 65 77 69 73 65 20 69 66 20 70 45 4c 69 73 74 3d  ewise if pEList=
1f4e4 3d 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20 69 74  =NULL then.** it
1f4e5 20 6d 61 74 63 68 65 73 20 61 6e 79 74 68 69 6e   matches anythin
1f4e6 67 20 73 6f 20 61 6c 77 61 79 73 20 72 65 74 75  g so always retu
1f4e7 72 6e 20 74 72 75 65 2e 20 20 52 65 74 75 72 6e  rn true.  Return
1f4e8 20 66 61 6c 73 65 20 6f 6e 6c 79 0a 2a 2a 20 69   false only.** i
1f4e9 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
1f4ea 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tch..*/.static i
1f4eb 6e 74 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76  nt checkColumnOv
1f4ec 65 72 4c 61 70 28 49 64 4c 69 73 74 20 2a 70 49  erLap(IdList *pI
1f4ed 64 4c 69 73 74 2c 20 45 78 70 72 4c 69 73 74 20  dList, ExprList 
1f4ee 2a 70 45 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20  *pEList){.  int 
1f4ef 65 3b 0a 20 20 69 66 28 20 21 70 49 64 4c 69 73  e;.  if( !pIdLis
1f4f0 74 20 7c 7c 20 21 70 45 4c 69 73 74 20 29 20 72  t || !pEList ) r
1f4f1 65 74 75 72 6e 20 31 3b 0a 20 20 66 6f 72 28 65  eturn 1;.  for(e
1f4f2 3d 30 3b 20 65 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; e<pEList->nE
1f4f3 78 70 72 3b 20 65 2b 2b 29 7b 0a 20 20 20 20 69  xpr; e++){.    i
1f4f4 66 28 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  f( sqlite3IdList
1f4f5 49 6e 64 65 78 28 70 49 64 4c 69 73 74 2c 20 70  Index(pIdList, p
1f4f6 45 4c 69 73 74 2d 3e 61 5b 65 5d 2e 7a 4e 61 6d  EList->a[e].zNam
1f4f7 65 29 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 31  e)>=0 ) return 1
1f4f8 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
1f4f9 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ; .}../*.** Retu
1f4fa 72 6e 20 61 20 62 69 74 20 76 65 63 74 6f 72 20  rn a bit vector 
1f4fb 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 61 74  to indicate what
1f4fc 20 6b 69 6e 64 20 6f 66 20 74 72 69 67 67 65 72   kind of trigger
1f4fd 73 20 65 78 69 73 74 20 66 6f 72 20 6f 70 65 72  s exist for oper
1f4fe 61 74 69 6f 6e 0a 2a 2a 20 22 6f 70 22 20 6f 6e  ation.** "op" on
1f4ff 20 74 61 62 6c 65 20 70 54 61 62 2e 20 20 49 66   table pTab.  If
1f500 20 70 43 68 61 6e 67 65 73 20 69 73 20 6e 6f 74   pChanges is not
1f501 20 4e 55 4c 4c 20 74 68 65 6e 20 69 74 20 69 73   NULL then it is
1f502 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d   a list of colum
1f503 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 62  ns.** that are b
1f504 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 20 54  eing updated.  T
1f505 72 69 67 67 65 72 73 20 6f 6e 6c 79 20 6d 61 74  riggers only mat
1f506 63 68 20 69 66 20 74 68 65 20 4f 4e 20 63 6c 61  ch if the ON cla
1f507 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72  use of the.** tr
1f508 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e  igger definition
1f509 20 6f 76 65 72 6c 61 70 73 20 74 68 65 20 73 65   overlaps the se
1f50a 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 62 65 69  t of columns bei
1f50b 6e 67 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a  ng updated..**.*
1f50c 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 62  * The returned b
1f50d 69 74 20 76 65 63 74 6f 72 20 69 73 20 73 6f 6d  it vector is som
1f50e 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  e combination of
1f50f 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20   TRIGGER_BEFORE 
1f510 61 6e 64 0a 2a 2a 20 54 52 49 47 47 45 52 5f 41  and.** TRIGGER_A
1f511 46 54 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  FTER..*/.SQLITE_
1f512 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1f513 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74  te3TriggersExist
1f514 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1f515 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  e,          /* U
1f516 73 65 64 20 74 6f 20 63 68 65 63 6b 20 66 6f 72  sed to check for
1f517 20 72 65 63 75 72 73 69 76 65 20 74 72 69 67 67   recursive trigg
1f518 65 72 73 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ers */.  Table *
1f519 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  pTab,           
1f51a 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68   /* The table th
1f51b 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  e contains the t
1f51c 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74  riggers */.  int
1f51d 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
1f51e 20 20 20 20 20 2f 2a 20 6f 6e 65 20 6f 66 20 54       /* one of T
1f51f 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 49 4e 53  K_DELETE, TK_INS
1f520 45 52 54 2c 20 54 4b 5f 55 50 44 41 54 45 20 2a  ERT, TK_UPDATE *
1f521 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
1f522 68 61 6e 67 65 73 20 20 20 20 20 20 2f 2a 20 43  hanges      /* C
1f523 6f 6c 75 6d 6e 73 20 74 68 61 74 20 63 68 61 6e  olumns that chan
1f524 67 65 20 69 6e 20 61 6e 20 55 50 44 41 54 45 20  ge in an UPDATE 
1f525 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a  statement */.){.
1f526 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
1f527 67 65 72 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 20  ger;.  int mask 
1f528 3d 20 30 3b 0a 0a 20 20 70 54 72 69 67 67 65 72  = 0;..  pTrigger
1f529 20 3d 20 49 73 56 69 72 74 75 61 6c 28 70 54 61   = IsVirtual(pTa
1f52a 62 29 20 3f 20 30 20 3a 20 70 54 61 62 2d 3e 70  b) ? 0 : pTab->p
1f52b 54 72 69 67 67 65 72 3b 0a 20 20 77 68 69 6c 65  Trigger;.  while
1f52c 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  ( pTrigger ){.  
1f52d 20 20 69 66 28 20 70 54 72 69 67 67 65 72 2d 3e    if( pTrigger->
1f52e 6f 70 3d 3d 6f 70 20 26 26 20 63 68 65 63 6b 43  op==op && checkC
1f52f 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28 70 54 72  olumnOverLap(pTr
1f530 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e 73 2c  igger->pColumns,
1f531 20 70 43 68 61 6e 67 65 73 29 20 29 7b 0a 20 20   pChanges) ){.  
1f532 20 20 20 20 6d 61 73 6b 20 7c 3d 20 70 54 72 69      mask |= pTri
1f533 67 67 65 72 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20  gger->tr_tm;.   
1f534 20 7d 0a 20 20 20 20 70 54 72 69 67 67 65 72 20   }.    pTrigger 
1f535 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78  = pTrigger->pNex
1f536 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
1f537 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  mask;.}../*.** C
1f538 6f 6e 76 65 72 74 20 74 68 65 20 70 53 74 65 70  onvert the pStep
1f539 2d 3e 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 69  ->target token i
1f53a 6e 74 6f 20 61 20 53 72 63 4c 69 73 74 20 61 6e  nto a SrcList an
1f53b 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  d return a point
1f53c 65 72 0a 2a 2a 20 74 6f 20 74 68 61 74 20 53 72  er.** to that Sr
1f53d 63 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  cList..**.** Thi
1f53e 73 20 72 6f 75 74 69 6e 65 20 61 64 64 73 20 61  s routine adds a
1f53f 20 73 70 65 63 69 66 69 63 20 64 61 74 61 62 61   specific databa
1f540 73 65 20 6e 61 6d 65 2c 20 69 66 20 6e 65 65 64  se name, if need
1f541 65 64 2c 20 74 6f 20 74 68 65 20 74 61 72 67 65  ed, to the targe
1f542 74 20 77 68 65 6e 0a 2a 2a 20 66 6f 72 6d 69 6e  t when.** formin
1f543 67 20 74 68 65 20 53 72 63 4c 69 73 74 2e 20 20  g the SrcList.  
1f544 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20  This prevents a 
1f545 74 72 69 67 67 65 72 20 69 6e 20 6f 6e 65 20 64  trigger in one d
1f546 61 74 61 62 61 73 65 20 66 72 6f 6d 0a 2a 2a 20  atabase from.** 
1f547 72 65 66 65 72 72 69 6e 67 20 74 6f 20 61 20 74  referring to a t
1f548 61 72 67 65 74 20 69 6e 20 61 6e 6f 74 68 65 72  arget in another
1f549 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 65   database.  An e
1f54a 78 63 65 70 74 69 6f 6e 20 69 73 20 77 68 65 6e  xception is when
1f54b 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20   the.** trigger 
1f54c 69 73 20 69 6e 20 54 45 4d 50 20 69 6e 20 77 68  is in TEMP in wh
1f54d 69 63 68 20 63 61 73 65 20 69 74 20 63 61 6e 20  ich case it can 
1f54e 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6f 74 68  refer to any oth
1f54f 65 72 20 64 61 74 61 62 61 73 65 20 69 74 0a 2a  er database it.*
1f550 2a 20 77 61 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74  * wants..*/.stat
1f551 69 63 20 53 72 63 4c 69 73 74 20 2a 74 61 72 67  ic SrcList *targ
1f552 65 74 53 72 63 4c 69 73 74 28 0a 20 20 50 61 72  etSrcList(.  Par
1f553 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
1f554 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
1f555 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72   context */.  Tr
1f556 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
1f557 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65     /* The trigge
1f558 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  r containing the
1f559 20 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 2a 2f   target token */
1f55a 0a 29 7b 0a 20 20 54 6f 6b 65 6e 20 73 44 62 3b  .){.  Token sDb;
1f55b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 75             /* Du
1f55c 6d 6d 79 20 64 61 74 61 62 61 73 65 20 6e 61 6d  mmy database nam
1f55d 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  e token */.  int
1f55e 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
1f55f 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
1f560 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 75 73  e database to us
1f561 65 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  e */.  SrcList *
1f562 70 53 72 63 3b 20 20 20 20 20 20 20 2f 2a 20 53  pSrc;       /* S
1f563 72 63 4c 69 73 74 20 74 6f 20 62 65 20 72 65 74  rcList to be ret
1f564 75 72 6e 65 64 20 2a 2f 0a 0a 20 20 69 44 62 20  urned */..  iDb 
1f565 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
1f566 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
1f567 62 2c 20 70 53 74 65 70 2d 3e 70 54 72 69 67 2d  b, pStep->pTrig-
1f568 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28  >pSchema);.  if(
1f569 20 69 44 62 3d 3d 30 20 7c 7c 20 69 44 62 3e 3d   iDb==0 || iDb>=
1f56a 32 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  2 ){.    assert(
1f56b 20 69 44 62 3c 70 50 61 72 73 65 2d 3e 64 62 2d   iDb<pParse->db-
1f56c 3e 6e 44 62 20 29 3b 0a 20 20 20 20 73 44 62 2e  >nDb );.    sDb.
1f56d 7a 20 3d 20 28 75 38 2a 29 70 50 61 72 73 65 2d  z = (u8*)pParse-
1f56e 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e  >db->aDb[iDb].zN
1f56f 61 6d 65 3b 0a 20 20 20 20 73 44 62 2e 6e 20 3d  ame;.    sDb.n =
1f570 20 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 73   strlen((char*)s
1f571 44 62 2e 7a 29 3b 0a 20 20 20 20 70 53 72 63 20  Db.z);.    pSrc 
1f572 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
1f573 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
1f574 62 2c 20 30 2c 20 26 73 44 62 2c 20 26 70 53 74  b, 0, &sDb, &pSt
1f575 65 70 2d 3e 74 61 72 67 65 74 29 3b 0a 20 20 7d  ep->target);.  }
1f576 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 53 72 63   else {.    pSrc
1f577 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
1f578 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
1f579 64 62 2c 20 30 2c 20 26 70 53 74 65 70 2d 3e 74  db, 0, &pStep->t
1f57a 61 72 67 65 74 2c 20 30 29 3b 0a 20 20 7d 0a 20  arget, 0);.  }. 
1f57b 20 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 7d 0a   return pSrc;.}.
1f57c 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1f57d 56 44 42 45 20 63 6f 64 65 20 66 6f 72 20 7a 65  VDBE code for ze
1f57e 72 6f 20 6f 72 20 6d 6f 72 65 20 73 74 61 74 65  ro or more state
1f57f 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65  ments inside the
1f580 20 62 6f 64 79 20 6f 66 20 61 0a 2a 2a 20 74 72   body of a.** tr
1f581 69 67 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74  igger.  .*/.stat
1f582 69 63 20 69 6e 74 20 63 6f 64 65 54 72 69 67 67  ic int codeTrigg
1f583 65 72 50 72 6f 67 72 61 6d 28 0a 20 20 50 61 72  erProgram(.  Par
1f584 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
1f585 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
1f586 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
1f587 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
1f588 53 74 65 70 4c 69 73 74 2c 20 20 20 2f 2a 20 4c  StepList,   /* L
1f589 69 73 74 20 6f 66 20 73 74 61 74 65 6d 65 6e 74  ist of statement
1f58a 73 20 69 6e 73 69 64 65 20 74 68 65 20 74 72 69  s inside the tri
1f58b 67 67 65 72 20 62 6f 64 79 20 2a 2f 0a 20 20 69  gger body */.  i
1f58c 6e 74 20 6f 72 63 6f 6e 66 69 6e 20 20 20 20 20  nt orconfin     
1f58d 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66           /* Conf
1f58e 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 2e 20  lict algorithm. 
1f58f 28 4f 45 5f 41 62 6f 72 74 2c 20 65 74 63 29 20  (OE_Abort, etc) 
1f590 2a 2f 20 20 0a 29 7b 0a 20 20 54 72 69 67 67 65  */  .){.  Trigge
1f591 72 53 74 65 70 20 2a 20 70 54 72 69 67 67 65 72  rStep * pTrigger
1f592 53 74 65 70 20 3d 20 70 53 74 65 70 4c 69 73 74  Step = pStepList
1f593 3b 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 0a  ;.  int orconf;.
1f594 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
1f595 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c  se->pVdbe;.  sql
1f596 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1f597 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74  e->db;..  assert
1f598 28 20 70 54 72 69 67 67 65 72 53 74 65 70 21 3d  ( pTriggerStep!=
1f599 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  0 );.  assert( v
1f59a 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  !=0 );.  sqlite3
1f59b 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1f59c 43 6f 6e 74 65 78 74 50 75 73 68 2c 20 30 2c 20  ContextPush, 0, 
1f59d 30 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e  0);.  VdbeCommen
1f59e 74 28 28 76 2c 20 22 23 20 62 65 67 69 6e 20 74  t((v, "# begin t
1f59f 72 69 67 67 65 72 20 25 73 22 2c 20 70 53 74 65  rigger %s", pSte
1f5a0 70 4c 69 73 74 2d 3e 70 54 72 69 67 2d 3e 6e 61  pList->pTrig->na
1f5a1 6d 65 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70  me));.  while( p
1f5a2 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
1f5a3 20 20 20 6f 72 63 6f 6e 66 20 3d 20 28 6f 72 63     orconf = (orc
1f5a4 6f 6e 66 69 6e 20 3d 3d 20 4f 45 5f 44 65 66 61  onfin == OE_Defa
1f5a5 75 6c 74 29 3f 70 54 72 69 67 67 65 72 53 74 65  ult)?pTriggerSte
1f5a6 70 2d 3e 6f 72 63 6f 6e 66 3a 6f 72 63 6f 6e 66  p->orconf:orconf
1f5a7 69 6e 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  in;.    pParse->
1f5a8 74 72 69 67 53 74 61 63 6b 2d 3e 6f 72 63 6f 6e  trigStack->orcon
1f5a9 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 20 20  f = orconf;.    
1f5aa 73 77 69 74 63 68 28 20 70 54 72 69 67 67 65 72  switch( pTrigger
1f5ab 53 74 65 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  Step->op ){.    
1f5ac 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54    case TK_SELECT
1f5ad 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 65 6c 65  : {.        Sele
1f5ae 63 74 20 2a 73 73 20 3d 20 73 71 6c 69 74 65 33  ct *ss = sqlite3
1f5af 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54  SelectDup(db, pT
1f5b0 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
1f5b1 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ect);.        if
1f5b2 28 20 73 73 20 29 7b 0a 20 20 20 20 20 20 20 20  ( ss ){.        
1f5b3 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 52    sqlite3SelectR
1f5b4 65 73 6f 6c 76 65 28 70 50 61 72 73 65 2c 20 73  esolve(pParse, s
1f5b5 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  s, 0);.         
1f5b6 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
1f5b7 50 61 72 73 65 2c 20 73 73 2c 20 53 52 54 5f 44  Parse, ss, SRT_D
1f5b8 69 73 63 61 72 64 2c 20 30 2c 20 30 2c 20 30 2c  iscard, 0, 0, 0,
1f5b9 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
1f5ba 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1f5bb 65 6c 65 74 65 28 73 73 29 3b 0a 20 20 20 20 20  elete(ss);.     
1f5bc 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65     }.        bre
1f5bd 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1f5be 20 20 63 61 73 65 20 54 4b 5f 55 50 44 41 54 45    case TK_UPDATE
1f5bf 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c  : {.        SrcL
1f5c0 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 20 20 20  ist *pSrc;.     
1f5c1 20 20 20 70 53 72 63 20 3d 20 74 61 72 67 65 74     pSrc = target
1f5c2 53 72 63 4c 69 73 74 28 70 50 61 72 73 65 2c 20  SrcList(pParse, 
1f5c3 70 54 72 69 67 67 65 72 53 74 65 70 29 3b 0a 20  pTriggerStep);. 
1f5c4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1f5c5 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65  beAddOp(v, OP_Re
1f5c6 73 65 74 43 6f 75 6e 74 2c 20 30 2c 20 30 29 3b  setCount, 0, 0);
1f5c7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1f5c8 55 70 64 61 74 65 28 70 50 61 72 73 65 2c 20 70  Update(pParse, p
1f5c9 53 72 63 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Src,.           
1f5ca 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1f5cb 4c 69 73 74 44 75 70 28 64 62 2c 20 70 54 72 69  ListDup(db, pTri
1f5cc 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c  ggerStep->pExprL
1f5cd 69 73 74 29 2c 20 0a 20 20 20 20 20 20 20 20 20  ist), .         
1f5ce 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
1f5cf 70 72 44 75 70 28 64 62 2c 20 70 54 72 69 67 67  prDup(db, pTrigg
1f5d0 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 29 2c  erStep->pWhere),
1f5d1 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20   orconf);.      
1f5d2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f5d3 4f 70 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f  Op(v, OP_ResetCo
1f5d4 75 6e 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  unt, 1, 0);.    
1f5d5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1f5d6 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b   }.      case TK
1f5d7 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20 20 20 20  _INSERT: {.     
1f5d8 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
1f5d9 3b 0a 20 20 20 20 20 20 20 20 70 53 72 63 20 3d  ;.        pSrc =
1f5da 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70   targetSrcList(p
1f5db 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 53  Parse, pTriggerS
1f5dc 74 65 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71  tep);.        sq
1f5dd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1f5de 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c  , OP_ResetCount,
1f5df 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
1f5e0 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50  sqlite3Insert(pP
1f5e1 61 72 73 65 2c 20 70 53 72 63 2c 0a 20 20 20 20  arse, pSrc,.    
1f5e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1f5e3 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 54 72  rListDup(db, pTr
1f5e4 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72  iggerStep->pExpr
1f5e5 4c 69 73 74 29 2c 20 0a 20 20 20 20 20 20 20 20  List), .        
1f5e6 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1f5e7 75 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53  up(db, pTriggerS
1f5e8 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 2c 20 0a  tep->pSelect), .
1f5e9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1f5ea 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70  3IdListDup(db, p
1f5eb 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 49 64  TriggerStep->pId
1f5ec 4c 69 73 74 29 2c 20 6f 72 63 6f 6e 66 29 3b 0a  List), orconf);.
1f5ed 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1f5ee 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52  dbeAddOp(v, OP_R
1f5ef 65 73 65 74 43 6f 75 6e 74 2c 20 31 2c 20 30 29  esetCount, 1, 0)
1f5f0 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1f5f1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
1f5f2 61 73 65 20 54 4b 5f 44 45 4c 45 54 45 3a 20 7b  ase TK_DELETE: {
1f5f3 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74  .        SrcList
1f5f4 20 2a 70 53 72 63 3b 0a 20 20 20 20 20 20 20 20   *pSrc;.        
1f5f5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f5f6 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e  (v, OP_ResetCoun
1f5f7 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 0, 0);.      
1f5f8 20 20 70 53 72 63 20 3d 20 74 61 72 67 65 74 53    pSrc = targetS
1f5f9 72 63 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  rcList(pParse, p
1f5fa 54 72 69 67 67 65 72 53 74 65 70 29 3b 0a 20 20  TriggerStep);.  
1f5fb 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c        sqlite3Del
1f5fc 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c 20  eteFrom(pParse, 
1f5fd 70 53 72 63 2c 20 0a 20 20 20 20 20 20 20 20 20  pSrc, .         
1f5fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5ff 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
1f600 64 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70  db, pTriggerStep
1f601 2d 3e 70 57 68 65 72 65 29 29 3b 0a 20 20 20 20  ->pWhere));.    
1f602 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f603 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65 73 65 74  ddOp(v, OP_Reset
1f604 43 6f 75 6e 74 2c 20 31 2c 20 30 29 3b 0a 20 20  Count, 1, 0);.  
1f605 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1f606 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75     }.      defau
1f607 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73 73 65  lt:.        asse
1f608 72 74 28 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20  rt(0);.    } .  
1f609 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d    pTriggerStep =
1f60a 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
1f60b 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Next;.  }.  sqli
1f60c 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1f60d 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30  OP_ContextPop, 0
1f60e 2c 20 30 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d  , 0);.  VdbeComm
1f60f 65 6e 74 28 28 76 2c 20 22 23 20 65 6e 64 20 74  ent((v, "# end t
1f610 72 69 67 67 65 72 20 25 73 22 2c 20 70 53 74 65  rigger %s", pSte
1f611 70 4c 69 73 74 2d 3e 70 54 72 69 67 2d 3e 6e 61  pList->pTrig->na
1f612 6d 65 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  me));..  return 
1f613 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
1f614 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f   is called to co
1f615 64 65 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20  de FOR EACH ROW 
1f616 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  triggers..**.** 
1f617 57 68 65 6e 20 74 68 65 20 63 6f 64 65 20 74 68  When the code th
1f618 61 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  at this function
1f619 20 67 65 6e 65 72 61 74 65 73 20 69 73 20 65 78   generates is ex
1f61a 65 63 75 74 65 64 2c 20 74 68 65 20 66 6f 6c 6c  ecuted, the foll
1f61b 6f 77 69 6e 67 20 0a 2a 2a 20 6d 75 73 74 20 62  owing .** must b
1f61c 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 31 2e  e true:.**.** 1.
1f61d 20 4e 6f 20 63 75 72 73 6f 72 73 20 6d 61 79 20   No cursors may 
1f61e 62 65 20 6f 70 65 6e 20 69 6e 20 74 68 65 20 6d  be open in the m
1f61f 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 28  ain database.  (
1f620 42 75 74 20 6e 65 77 49 64 78 20 61 6e 64 20 6f  But newIdx and o
1f621 6c 64 49 64 78 0a 2a 2a 20 20 20 20 63 61 6e 20  ldIdx.**    can 
1f622 62 65 20 69 6e 64 69 63 65 73 20 6f 66 20 63 75  be indices of cu
1f623 72 73 6f 72 73 20 69 6e 20 74 65 6d 70 6f 72 61  rsors in tempora
1f624 72 79 20 74 61 62 6c 65 73 2e 20 20 53 65 65 20  ry tables.  See 
1f625 62 65 6c 6f 77 2e 29 0a 2a 2a 0a 2a 2a 20 32 2e  below.).**.** 2.
1f626 20 49 66 20 74 68 65 20 74 72 69 67 67 65 72 73   If the triggers
1f627 20 62 65 69 6e 67 20 63 6f 64 65 64 20 61 72 65   being coded are
1f628 20 4f 4e 20 49 4e 53 45 52 54 20 6f 72 20 4f 4e   ON INSERT or ON
1f629 20 55 50 44 41 54 45 20 74 72 69 67 67 65 72 73   UPDATE triggers
1f62a 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 61 20 74  , then.**    a t
1f62b 65 6d 70 6f 72 61 72 79 20 76 64 62 65 20 63 75  emporary vdbe cu
1f62c 72 73 6f 72 20 28 69 6e 64 65 78 20 6e 65 77 49  rsor (index newI
1f62d 64 78 29 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  dx) must be open
1f62e 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20 61 74   and pointing at
1f62f 0a 2a 2a 20 20 20 20 61 20 72 6f 77 20 63 6f 6e  .**    a row con
1f630 74 61 69 6e 69 6e 67 20 76 61 6c 75 65 73 20 74  taining values t
1f631 6f 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64  o be substituted
1f632 20 66 6f 72 20 6e 65 77 2e 2a 20 65 78 70 72 65   for new.* expre
1f633 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 2a 2a  ssions in the.**
1f634 20 20 20 20 74 72 69 67 67 65 72 20 70 72 6f 67      trigger prog
1f635 72 61 6d 28 73 29 2e 0a 2a 2a 0a 2a 2a 20 33 2e  ram(s)..**.** 3.
1f636 20 49 66 20 74 68 65 20 74 72 69 67 67 65 72 73   If the triggers
1f637 20 62 65 69 6e 67 20 63 6f 64 65 64 20 61 72 65   being coded are
1f638 20 4f 4e 20 44 45 4c 45 54 45 20 6f 72 20 4f 4e   ON DELETE or ON
1f639 20 55 50 44 41 54 45 20 74 72 69 67 67 65 72 73   UPDATE triggers
1f63a 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 61 20 74  , then.**    a t
1f63b 65 6d 70 6f 72 61 72 79 20 76 64 62 65 20 63 75  emporary vdbe cu
1f63c 72 73 6f 72 20 28 69 6e 64 65 78 20 6f 6c 64 49  rsor (index oldI
1f63d 64 78 29 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  dx) must be open
1f63e 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20 61 74   and pointing at
1f63f 0a 2a 2a 20 20 20 20 61 20 72 6f 77 20 63 6f 6e  .**    a row con
1f640 74 61 69 6e 69 6e 67 20 76 61 6c 75 65 73 20 74  taining values t
1f641 6f 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64  o be substituted
1f642 20 66 6f 72 20 6f 6c 64 2e 2a 20 65 78 70 72 65   for old.* expre
1f643 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 2a 2a  ssions in the.**
1f644 20 20 20 20 74 72 69 67 67 65 72 20 70 72 6f 67      trigger prog
1f645 72 61 6d 28 73 29 2e 0a 2a 2a 0a 2a 2f 0a 53 51  ram(s)..**.*/.SQ
1f646 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1f647 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
1f648 72 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20  rigger(.  Parse 
1f649 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
1f64a 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
1f64b 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
1f64c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
1f64d 20 6f 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54   of TK_UPDATE, T
1f64e 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 44 45 4c  K_INSERT, TK_DEL
1f64f 45 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ETE */.  ExprLis
1f650 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a  t *pChanges,  /*
1f651 20 43 68 61 6e 67 65 73 20 6c 69 73 74 20 66 6f   Changes list fo
1f652 72 20 61 6e 79 20 55 50 44 41 54 45 20 4f 46 20  r any UPDATE OF 
1f653 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e  triggers */.  in
1f654 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20  t tr_tm,        
1f655 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49     /* One of TRI
1f656 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49  GGER_BEFORE, TRI
1f657 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20  GGER_AFTER */.  
1f658 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
1f659 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
1f65a 65 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65  e to code trigge
1f65b 72 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74  rs from */.  int
1f65c 20 6e 65 77 49 64 78 2c 20 20 20 20 20 20 20 20   newIdx,        
1f65d 20 20 2f 2a 20 54 68 65 20 69 6e 64 69 63 65 20    /* The indice 
1f65e 6f 66 20 74 68 65 20 22 6e 65 77 22 20 72 6f 77  of the "new" row
1f65f 20 74 6f 20 61 63 63 65 73 73 20 2a 2f 0a 20 20   to access */.  
1f660 69 6e 74 20 6f 6c 64 49 64 78 2c 20 20 20 20 20  int oldIdx,     
1f661 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 69       /* The indi
1f662 63 65 20 6f 66 20 74 68 65 20 22 6f 6c 64 22 20  ce of the "old" 
1f663 72 6f 77 20 74 6f 20 61 63 63 65 73 73 20 2a 2f  row to access */
1f664 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 2c 20 20  .  int orconf,  
1f665 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f          /* ON CO
1f666 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f  NFLICT policy */
1f667 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d  .  int ignoreJum
1f668 70 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72  p       /* Instr
1f669 75 63 74 69 6f 6e 20 74 6f 20 6a 75 6d 70 20 74  uction to jump t
1f66a 6f 20 66 6f 72 20 52 41 49 53 45 28 49 47 4e 4f  o for RAISE(IGNO
1f66b 52 45 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67  RE) */.){.  Trig
1f66c 67 65 72 20 2a 70 3b 0a 20 20 54 72 69 67 67 65  ger *p;.  Trigge
1f66d 72 53 74 61 63 6b 20 74 72 69 67 53 74 61 63 6b  rStack trigStack
1f66e 45 6e 74 72 79 3b 0a 0a 20 20 61 73 73 65 72 74  Entry;..  assert
1f66f 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45  (op == TK_UPDATE
1f670 20 7c 7c 20 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53   || op == TK_INS
1f671 45 52 54 20 7c 7c 20 6f 70 20 3d 3d 20 54 4b 5f  ERT || op == TK_
1f672 44 45 4c 45 54 45 29 3b 0a 20 20 61 73 73 65 72  DELETE);.  asser
1f673 74 28 74 72 5f 74 6d 20 3d 3d 20 54 52 49 47 47  t(tr_tm == TRIGG
1f674 45 52 5f 42 45 46 4f 52 45 20 7c 7c 20 74 72 5f  ER_BEFORE || tr_
1f675 74 6d 20 3d 3d 20 54 52 49 47 47 45 52 5f 41 46  tm == TRIGGER_AF
1f676 54 45 52 20 29 3b 0a 0a 20 20 61 73 73 65 72 74  TER );..  assert
1f677 28 6e 65 77 49 64 78 20 21 3d 20 2d 31 20 7c 7c  (newIdx != -1 ||
1f678 20 6f 6c 64 49 64 78 20 21 3d 20 2d 31 29 3b 0a   oldIdx != -1);.
1f679 0a 20 20 66 6f 72 28 70 3d 70 54 61 62 2d 3e 70  .  for(p=pTab->p
1f67a 54 72 69 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d  Trigger; p; p=p-
1f67b 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74  >pNext){.    int
1f67c 20 66 69 72 65 5f 74 68 69 73 20 3d 20 30 3b 0a   fire_this = 0;.
1f67d 0a 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e  .    /* Determin
1f67e 65 20 77 68 65 74 68 65 72 20 77 65 20 73 68 6f  e whether we sho
1f67f 75 6c 64 20 63 6f 64 65 20 74 68 69 73 20 74 72  uld code this tr
1f680 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 69 66 28  igger */.    if(
1f681 20 0a 20 20 20 20 20 20 70 2d 3e 6f 70 3d 3d 6f   .      p->op==o
1f682 70 20 26 26 20 0a 20 20 20 20 20 20 70 2d 3e 74  p && .      p->t
1f683 72 5f 74 6d 3d 3d 74 72 5f 74 6d 20 26 26 20 0a  r_tm==tr_tm && .
1f684 20 20 20 20 20 20 28 70 2d 3e 70 53 63 68 65 6d        (p->pSchem
1f685 61 3d 3d 70 2d 3e 70 54 61 62 53 63 68 65 6d 61  a==p->pTabSchema
1f686 20 7c 7c 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d   || p->pSchema==
1f687 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b  pParse->db->aDb[
1f688 31 5d 2e 70 53 63 68 65 6d 61 29 20 26 26 0a 20  1].pSchema) &&. 
1f689 20 20 20 20 20 28 6f 70 21 3d 54 4b 5f 55 50 44       (op!=TK_UPD
1f68a 41 54 45 7c 7c 21 70 2d 3e 70 43 6f 6c 75 6d 6e  ATE||!p->pColumn
1f68b 73 7c 7c 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76  s||checkColumnOv
1f68c 65 72 4c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e  erLap(p->pColumn
1f68d 73 2c 70 43 68 61 6e 67 65 73 29 29 0a 20 20 20  s,pChanges)).   
1f68e 20 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65   ){.      Trigge
1f68f 72 53 74 61 63 6b 20 2a 70 53 3b 20 20 20 20 20  rStack *pS;     
1f690 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
1f691 72 69 67 67 65 72 2d 73 74 61 63 6b 20 65 6e 74  rigger-stack ent
1f692 72 79 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28  ry */.      for(
1f693 70 53 3d 70 50 61 72 73 65 2d 3e 74 72 69 67 53  pS=pParse->trigS
1f694 74 61 63 6b 3b 20 70 53 20 26 26 20 70 21 3d 70  tack; pS && p!=p
1f695 53 2d 3e 70 54 72 69 67 67 65 72 3b 20 70 53 3d  S->pTrigger; pS=
1f696 70 53 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 20  pS->pNext){}.   
1f697 20 20 20 69 66 28 20 21 70 53 20 29 7b 0a 20 20     if( !pS ){.  
1f698 20 20 20 20 20 20 66 69 72 65 5f 74 68 69 73 20        fire_this 
1f699 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66  = 1;.      }.#if
1f69a 20 30 20 20 20 20 2f 2a 20 47 69 76 65 20 6e 6f   0    /* Give no
1f69b 20 77 61 72 6e 69 6e 67 20 66 6f 72 20 72 65 63   warning for rec
1f69c 75 72 73 69 76 65 20 74 72 69 67 67 65 72 73 2e  ursive triggers.
1f69d 20 20 4a 75 73 74 20 64 6f 20 6e 6f 74 20 64 6f    Just do not do
1f69e 20 74 68 65 6d 20 2a 2f 0a 20 20 20 20 20 20 65   them */.      e
1f69f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
1f6a0 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1f6a1 72 73 65 2c 20 22 72 65 63 75 72 73 69 76 65 20  rse, "recursive 
1f6a2 74 72 69 67 67 65 72 73 20 6e 6f 74 20 73 75 70  triggers not sup
1f6a3 70 6f 72 74 65 64 20 28 25 73 29 22 2c 0a 20 20  ported (%s)",.  
1f6a4 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6e 61 6d            p->nam
1f6a5 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  e);.        retu
1f6a6 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
1f6a7 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
1f6a8 20 20 20 20 7d 0a 20 0a 20 20 20 20 69 66 28 20      }. .    if( 
1f6a9 66 69 72 65 5f 74 68 69 73 20 29 7b 0a 20 20 20  fire_this ){.   
1f6aa 20 20 20 69 6e 74 20 65 6e 64 54 72 69 67 67 65     int endTrigge
1f6ab 72 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 20  r;.      Expr * 
1f6ac 77 68 65 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  whenExpr;.      
1f6ad 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e  AuthContext sCon
1f6ae 74 65 78 74 3b 0a 20 20 20 20 20 20 4e 61 6d 65  text;.      Name
1f6af 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 0a 20 20  Context sNC;..  
1f6b0 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c      memset(&sNC,
1f6b1 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
1f6b2 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72  ;.      sNC.pPar
1f6b3 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 0a 20 20  se = pParse;..  
1f6b4 20 20 20 20 2f 2a 20 50 75 73 68 20 61 6e 20 65      /* Push an e
1f6b5 6e 74 72 79 20 6f 6e 20 74 6f 20 74 68 65 20 74  ntry on to the t
1f6b6 72 69 67 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a  rigger stack */.
1f6b7 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b 45        trigStackE
1f6b8 6e 74 72 79 2e 70 54 72 69 67 67 65 72 20 3d 20  ntry.pTrigger = 
1f6b9 70 3b 0a 20 20 20 20 20 20 74 72 69 67 53 74 61  p;.      trigSta
1f6ba 63 6b 45 6e 74 72 79 2e 6e 65 77 49 64 78 20 3d  ckEntry.newIdx =
1f6bb 20 6e 65 77 49 64 78 3b 0a 20 20 20 20 20 20 74   newIdx;.      t
1f6bc 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c  rigStackEntry.ol
1f6bd 64 49 64 78 20 3d 20 6f 6c 64 49 64 78 3b 0a 20  dIdx = oldIdx;. 
1f6be 20 20 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e       trigStackEn
1f6bf 74 72 79 2e 70 54 61 62 20 3d 20 70 54 61 62 3b  try.pTab = pTab;
1f6c0 0a 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b  .      trigStack
1f6c1 45 6e 74 72 79 2e 70 4e 65 78 74 20 3d 20 70 50  Entry.pNext = pP
1f6c2 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b  arse->trigStack;
1f6c3 0a 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b  .      trigStack
1f6c4 45 6e 74 72 79 2e 69 67 6e 6f 72 65 4a 75 6d 70  Entry.ignoreJump
1f6c5 20 3d 20 69 67 6e 6f 72 65 4a 75 6d 70 3b 0a 20   = ignoreJump;. 
1f6c6 20 20 20 20 20 70 50 61 72 73 65 2d 3e 74 72 69       pParse->tri
1f6c7 67 53 74 61 63 6b 20 3d 20 26 74 72 69 67 53 74  gStack = &trigSt
1f6c8 61 63 6b 45 6e 74 72 79 3b 0a 20 20 20 20 20 20  ackEntry;.      
1f6c9 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
1f6ca 78 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 26  xtPush(pParse, &
1f6cb 73 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 6e 61 6d  sContext, p->nam
1f6cc 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 63 6f  e);..      /* co
1f6cd 64 65 20 74 68 65 20 57 48 45 4e 20 63 6c 61 75  de the WHEN clau
1f6ce 73 65 20 2a 2f 0a 20 20 20 20 20 20 65 6e 64 54  se */.      endT
1f6cf 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33  rigger = sqlite3
1f6d0 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 70 50  VdbeMakeLabel(pP
1f6d1 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20  arse->pVdbe);.  
1f6d2 20 20 20 20 77 68 65 6e 45 78 70 72 20 3d 20 73      whenExpr = s
1f6d3 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 70 50  qlite3ExprDup(pP
1f6d4 61 72 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 57 68  arse->db, p->pWh
1f6d5 65 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  en);.      if( s
1f6d6 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76  qlite3ExprResolv
1f6d7 65 4e 61 6d 65 73 28 26 73 4e 43 2c 20 77 68 65  eNames(&sNC, whe
1f6d8 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  nExpr) ){.      
1f6d9 20 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74    pParse->trigSt
1f6da 61 63 6b 20 3d 20 74 72 69 67 53 74 61 63 6b 45  ack = trigStackE
1f6db 6e 74 72 79 2e 70 4e 65 78 74 3b 0a 20 20 20 20  ntry.pNext;.    
1f6dc 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
1f6dd 65 6c 65 74 65 28 77 68 65 6e 45 78 70 72 29 3b  elete(whenExpr);
1f6de 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1f6df 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
1f6e0 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
1f6e1 6c 73 65 28 70 50 61 72 73 65 2c 20 77 68 65 6e  lse(pParse, when
1f6e2 45 78 70 72 2c 20 65 6e 64 54 72 69 67 67 65 72  Expr, endTrigger
1f6e3 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
1f6e4 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 77 68  te3ExprDelete(wh
1f6e5 65 6e 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20  enExpr);..      
1f6e6 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72  codeTriggerProgr
1f6e7 61 6d 28 70 50 61 72 73 65 2c 20 70 2d 3e 73 74  am(pParse, p->st
1f6e8 65 70 5f 6c 69 73 74 2c 20 6f 72 63 6f 6e 66 29  ep_list, orconf)
1f6e9 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 50 6f 70  ; ..      /* Pop
1f6ea 20 74 68 65 20 65 6e 74 72 79 20 6f 66 66 20 74   the entry off t
1f6eb 68 65 20 74 72 69 67 67 65 72 20 73 74 61 63 6b  he trigger stack
1f6ec 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 72 73 65   */.      pParse
1f6ed 2d 3e 74 72 69 67 53 74 61 63 6b 20 3d 20 74 72  ->trigStack = tr
1f6ee 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 4e 65  igStackEntry.pNe
1f6ef 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  xt;.      sqlite
1f6f0 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28  3AuthContextPop(
1f6f1 26 73 43 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 20  &sContext);..   
1f6f2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
1f6f3 73 6f 6c 76 65 4c 61 62 65 6c 28 70 50 61 72 73  solveLabel(pPars
1f6f4 65 2d 3e 70 56 64 62 65 2c 20 65 6e 64 54 72 69  e->pVdbe, endTri
1f6f5 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  gger);.    }.  }
1f6f6 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
1f6f7 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
1f6f8 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
1f6f9 49 47 47 45 52 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  IGGER) */../****
1f6fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1f6fb 66 20 74 72 69 67 67 65 72 2e 63 20 2a 2a 2a 2a  f trigger.c ****
1f6fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f6fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f6fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1f6ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1f700 20 66 69 6c 65 20 75 70 64 61 74 65 2e 63 20 2a   file update.c *
1f701 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f702 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f703 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1f704 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
1f705 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
1f706 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1f707 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1f708 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1f709 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1f70a 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1f70b 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1f70c 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1f70d 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1f70e 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1f70f 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1f710 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1f711 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1f712 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1f713 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1f714 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1f715 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1f716 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1f717 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f719 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f71a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f71b 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
1f71c 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
1f71d 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65  outines that are
1f71e 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
1f71f 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64  arser.** to hand
1f720 6c 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  le UPDATE statem
1f721 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ents..**.** $Id:
1f722 20 75 70 64 61 74 65 2e 63 2c 76 20 31 2e 31 34   update.c,v 1.14
1f723 30 20 32 30 30 37 2f 30 38 2f 31 36 20 31 30 3a  0 2007/08/16 10:
1f724 30 39 3a 30 33 20 64 61 6e 69 65 6c 6b 31 39 37  09:03 danielk197
1f725 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e  7 Exp $.*/..#ifn
1f726 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f727 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20  VIRTUALTABLE./* 
1f728 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
1f729 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ion */.static vo
1f72a 69 64 20 75 70 64 61 74 65 56 69 72 74 75 61 6c  id updateVirtual
1f72b 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
1f72c 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
1f72d 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
1f72e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
1f72f 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 2f  t *pSrc,       /
1f730 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74 61  * The virtual ta
1f731 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64 69 66 69  ble to be modifi
1f732 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ed */.  Table *p
1f733 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Tab,         /* 
1f734 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  The virtual tabl
1f735 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
1f736 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54  *pChanges,  /* T
1f737 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68  he columns to ch
1f738 61 6e 67 65 20 69 6e 20 74 68 65 20 55 50 44 41  ange in the UPDA
1f739 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  TE statement */.
1f73a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 45 78    Expr *pRowidEx
1f73b 70 72 2c 20 20 20 20 2f 2a 20 45 78 70 72 65 73  pr,    /* Expres
1f73c 73 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63  sion used to rec
1f73d 6f 6d 70 75 74 65 20 74 68 65 20 72 6f 77 69 64  ompute the rowid
1f73e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66   */.  int *aXRef
1f73f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  ,          /* Ma
1f740 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d  pping from colum
1f741 6e 73 20 6f 66 20 70 54 61 62 20 74 6f 20 65 6e  ns of pTab to en
1f742 74 72 69 65 73 20 69 6e 20 70 43 68 61 6e 67 65  tries in pChange
1f743 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  s */.  Expr *pWh
1f744 65 72 65 20 20 20 20 20 20 20 20 20 2f 2a 20 57  ere         /* W
1f745 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74  HERE clause of t
1f746 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
1f747 65 6e 74 20 2a 2f 0a 29 3b 0a 23 65 6e 64 69 66  ent */.);.#endif
1f748 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1f749 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1f74a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 6f 73 74 20  ./*.** The most 
1f74b 72 65 63 65 6e 74 6c 79 20 63 6f 64 65 64 20 69  recently coded i
1f74c 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61  nstruction was a
1f74d 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72  n OP_Column to r
1f74e 65 74 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 69  etrieve the.** i
1f74f 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61  -th column of ta
1f750 62 6c 65 20 70 54 61 62 2e 20 54 68 69 73 20 72  ble pTab. This r
1f751 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
1f752 50 33 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  P3 parameter of 
1f753 74 68 65 20 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d  the .** OP_Colum
1f754 6e 20 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74  n to the default
1f755 20 76 61 6c 75 65 2c 20 69 66 20 61 6e 79 2e 0a   value, if any..
1f756 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  **.** The defaul
1f757 74 20 76 61 6c 75 65 20 6f 66 20 61 20 63 6f 6c  t value of a col
1f758 75 6d 6e 20 69 73 20 73 70 65 63 69 66 69 65 64  umn is specified
1f759 20 62 79 20 61 20 44 45 46 41 55 4c 54 20 63 6c   by a DEFAULT cl
1f75a 61 75 73 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20  ause in the .** 
1f75b 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  column definitio
1f75c 6e 2e 20 54 68 69 73 20 77 61 73 20 65 69 74 68  n. This was eith
1f75d 65 72 20 73 75 70 70 6c 69 65 64 20 62 79 20 74  er supplied by t
1f75e 68 65 20 75 73 65 72 20 77 68 65 6e 20 74 68 65  he user when the
1f75f 20 74 61 62 6c 65 0a 2a 2a 20 77 61 73 20 63 72   table.** was cr
1f760 65 61 74 65 64 2c 20 6f 72 20 61 64 64 65 64 20  eated, or added 
1f761 6c 61 74 65 72 20 74 6f 20 74 68 65 20 74 61 62  later to the tab
1f762 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 62 79  le definition by
1f763 20 61 6e 20 41 4c 54 45 52 20 54 41 42 4c 45 0a   an ALTER TABLE.
1f764 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 49 66 20 74  ** command. If t
1f765 68 65 20 6c 61 74 74 65 72 2c 20 74 68 65 6e 20  he latter, then 
1f766 74 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 73 20  the row-records 
1f767 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 74 72  in the table btr
1f768 65 65 20 6f 6e 20 64 69 73 6b 0a 2a 2a 20 6d 61  ee on disk.** ma
1f769 79 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  y not contain a 
1f76a 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f  value for the co
1f76b 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20 64 65 66  lumn and the def
1f76c 61 75 6c 74 20 76 61 6c 75 65 2c 20 74 61 6b 65  ault value, take
1f76d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 50 33  n.** from the P3
1f76e 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68   parameter of th
1f76f 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 69 6e 73 74  e OP_Column inst
1f770 72 75 63 74 69 6f 6e 2c 20 69 73 20 72 65 74 75  ruction, is retu
1f771 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a  rned instead..**
1f772 20 49 66 20 74 68 65 20 66 6f 72 6d 65 72 2c 20   If the former, 
1f773 74 68 65 6e 20 61 6c 6c 20 72 6f 77 2d 72 65 63  then all row-rec
1f774 6f 72 64 73 20 61 72 65 20 67 75 61 72 61 6e 74  ords are guarant
1f775 65 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 20 61  eed to include a
1f776 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68   value.** for th
1f777 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65  e column and the
1f778 20 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 74   P3 value is not
1f779 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a   required..**.**
1f77a 20 43 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69   Column definiti
1f77b 6f 6e 73 20 63 72 65 61 74 65 64 20 62 79 20 61  ons created by a
1f77c 6e 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f  n ALTER TABLE co
1f77d 6d 6d 61 6e 64 20 6d 61 79 20 6f 6e 6c 79 20 68  mmand may only h
1f77e 61 76 65 20 0a 2a 2a 20 6c 69 74 65 72 61 6c 20  ave .** literal 
1f77f 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 73  default values s
1f780 70 65 63 69 66 69 65 64 3a 20 61 20 6e 75 6d 62  pecified: a numb
1f781 65 72 2c 20 6e 75 6c 6c 20 6f 72 20 61 20 73 74  er, null or a st
1f782 72 69 6e 67 2e 20 28 49 66 20 61 20 6d 6f 72 65  ring. (If a more
1f783 0a 2a 2a 20 63 6f 6d 70 6c 69 63 61 74 65 64 20  .** complicated 
1f784 64 65 66 61 75 6c 74 20 65 78 70 72 65 73 73 69  default expressi
1f785 6f 6e 20 76 61 6c 75 65 20 77 61 73 20 70 72 6f  on value was pro
1f786 76 69 64 65 64 2c 20 69 74 20 69 73 20 65 76 61  vided, it is eva
1f787 6c 75 61 74 65 64 20 0a 2a 2a 20 77 68 65 6e 20  luated .** when 
1f788 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20  the ALTER TABLE 
1f789 69 73 20 65 78 65 63 75 74 65 64 20 61 6e 64 20  is executed and 
1f78a 6f 6e 65 20 6f 66 20 74 68 65 20 6c 69 74 65 72  one of the liter
1f78b 61 6c 20 76 61 6c 75 65 73 20 77 72 69 74 74 65  al values writte
1f78c 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 71  n.** into the sq
1f78d 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
1f78e 65 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 66  e.).**.** Theref
1f78f 6f 72 65 2c 20 74 68 65 20 50 33 20 70 61 72 61  ore, the P3 para
1f790 6d 65 74 65 72 20 69 73 20 6f 6e 6c 79 20 72 65  meter is only re
1f791 71 75 69 72 65 64 20 69 66 20 74 68 65 20 64 65  quired if the de
1f792 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 0a  fault value for.
1f793 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73  ** the column is
1f794 20 61 20 6c 69 74 65 72 61 6c 20 6e 75 6d 62 65   a literal numbe
1f795 72 2c 20 73 74 72 69 6e 67 20 6f 72 20 6e 75 6c  r, string or nul
1f796 6c 2e 20 54 68 65 20 73 71 6c 69 74 65 33 56 61  l. The sqlite3Va
1f797 6c 75 65 46 72 6f 6d 45 78 70 72 28 29 0a 2a 2a  lueFromExpr().**
1f798 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 70   function is cap
1f799 61 62 6c 65 20 6f 66 20 74 72 61 6e 73 66 6f 72  able of transfor
1f79a 6d 69 6e 67 20 74 68 65 73 65 20 74 79 70 65 73  ming these types
1f79b 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20   of expressions 
1f79c 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  into.** sqlite3_
1f79d 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a  value objects..*
1f79e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f79f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c   void sqlite3Col
1f7a0 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20  umnDefault(Vdbe 
1f7a1 2a 76 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  *v, Table *pTab,
1f7a2 20 69 6e 74 20 69 29 7b 0a 20 20 69 66 28 20 70   int i){.  if( p
1f7a3 54 61 62 20 26 26 20 21 70 54 61 62 2d 3e 70 53  Tab && !pTab->pS
1f7a4 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
1f7a5 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1f7a6 75 65 3b 0a 20 20 20 20 75 38 20 65 6e 63 20 3d  ue;.    u8 enc =
1f7a7 20 45 4e 43 28 73 71 6c 69 74 65 33 56 64 62 65   ENC(sqlite3Vdbe
1f7a8 44 62 28 76 29 29 3b 0a 20 20 20 20 43 6f 6c 75  Db(v));.    Colu
1f7a9 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62  mn *pCol = &pTab
1f7aa 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 61  ->aCol[i];.    a
1f7ab 73 73 65 72 74 28 20 69 3c 70 54 61 62 2d 3e 6e  ssert( i<pTab->n
1f7ac 43 6f 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  Col );.    sqlit
1f7ad 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
1f7ae 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
1f7af 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 2c 20 65  , pCol->pDflt, e
1f7b0 6e 63 2c 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  nc, pCol->affini
1f7b1 74 79 2c 20 26 70 56 61 6c 75 65 29 3b 0a 20 20  ty, &pValue);.  
1f7b2 20 20 69 66 28 20 70 56 61 6c 75 65 20 29 7b 0a    if( pValue ){.
1f7b3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f7b4 65 43 68 61 6e 67 65 50 33 28 76 2c 20 2d 31 2c  eChangeP3(v, -1,
1f7b5 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
1f7b6 56 61 6c 75 65 2c 20 50 33 5f 4d 45 4d 29 3b 0a  Value, P3_MEM);.
1f7b7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f7b8 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
1f7b9 20 22 23 20 25 73 2e 25 73 22 2c 20 70 54 61 62   "# %s.%s", pTab
1f7ba 2d 3e 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d 3e 7a  ->zName, pCol->z
1f7bb 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20  Name));.    }.  
1f7bc 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65  }.}../*.** Proce
1f7bd 73 73 20 61 6e 20 55 50 44 41 54 45 20 73 74 61  ss an UPDATE sta
1f7be 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20  tement..**.**   
1f7bf 55 50 44 41 54 45 20 4f 52 20 49 47 4e 4f 52 45  UPDATE OR IGNORE
1f7c0 20 74 61 62 6c 65 5f 77 78 79 7a 20 53 45 54 20   table_wxyz SET 
1f7c1 61 3d 62 2c 20 63 3d 64 20 57 48 45 52 45 20 65  a=b, c=d WHERE e
1f7c2 3c 35 20 41 4e 44 20 66 20 4e 4f 54 20 4e 55 4c  <5 AND f NOT NUL
1f7c3 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5c  L;.**          \
1f7c4 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f 5f 5f  _______/ \______
1f7c5 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 2f  __/     \______/
1f7c6 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f         \________
1f7c7 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20 20 20  ________/.*     
1f7c8 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 20         onError  
1f7c9 20 70 54 61 62 4c 69 73 74 20 20 20 20 20 20 70   pTabList      p
1f7ca 43 68 61 6e 67 65 73 20 20 20 20 20 20 20 20 20  Changes         
1f7cb 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53 51      pWhere.*/.SQ
1f7cc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1f7cd 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28  d sqlite3Update(
1f7ce 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1f7cf 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
1f7d0 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
1f7d1 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
1f7d2 61 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54  abList,     /* T
1f7d3 68 65 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63  he table in whic
1f7d4 68 20 77 65 20 73 68 6f 75 6c 64 20 63 68 61 6e  h we should chan
1f7d5 67 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45  ge things */.  E
1f7d6 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
1f7d7 73 2c 20 20 20 20 2f 2a 20 54 68 69 6e 67 73 20  s,    /* Things 
1f7d8 74 6f 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f  to be changed */
1f7d9 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c  .  Expr *pWhere,
1f7da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1f7db 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
1f7dc 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20  May be null */. 
1f7dd 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20   int onError    
1f7de 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74          /* How t
1f7df 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61  o handle constra
1f7e0 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b  int errors */.){
1f7e1 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20  .  int i, j;    
1f7e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1f7e3 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
1f7e4 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
1f7e5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
1f7e6 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61 74 65  ble to be update
1f7e7 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 20  d */.  int addr 
1f7e8 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
1f7e9 20 56 44 42 45 20 69 6e 73 74 72 75 63 74 69 6f   VDBE instructio
1f7ea 6e 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  n address of the
1f7eb 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6c 6f   start of the lo
1f7ec 6f 70 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66  op */.  WhereInf
1f7ed 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f  o *pWInfo;     /
1f7ee 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * Information ab
1f7ef 6f 75 74 20 74 68 65 20 57 48 45 52 45 20 63 6c  out the WHERE cl
1f7f0 61 75 73 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a  ause */.  Vdbe *
1f7f1 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
1f7f2 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
1f7f3 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20  database engine 
1f7f4 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
1f7f5 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;           /* F
1f7f6 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
1f7f7 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74  indices */.  int
1f7f8 20 6e 49 64 78 3b 20 20 20 20 20 20 20 20 20 20   nIdx;          
1f7f9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1f7fa 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 6e 65   indices that ne
1f7fb 65 64 20 75 70 64 61 74 69 6e 67 20 2a 2f 0a 20  ed updating */. 
1f7fc 20 69 6e 74 20 6e 49 64 78 54 6f 74 61 6c 3b 20   int nIdxTotal; 
1f7fd 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
1f7fe 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 64 69 63   number of indic
1f7ff 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  es */.  int iCur
1f800 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1f801 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75  * VDBE Cursor nu
1f802 6d 62 65 72 20 6f 66 20 70 54 61 62 20 2a 2f 0a  mber of pTab */.
1f803 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
1f804 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f805 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75  database structu
1f806 72 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 2a  re */.  Index **
1f807 61 70 49 64 78 20 3d 20 30 3b 20 20 20 20 20 2f  apIdx = 0;     /
1f808 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 69 6e  * An array of in
1f809 64 69 63 65 73 20 74 68 61 74 20 6e 65 65 64 20  dices that need 
1f80a 75 70 64 61 74 69 6e 67 20 74 6f 6f 20 2a 2f 0a  updating too */.
1f80b 20 20 63 68 61 72 20 2a 61 49 64 78 55 73 65 64    char *aIdxUsed
1f80c 20 3d 20 30 3b 20 20 20 20 2f 2a 20 61 49 64 78   = 0;    /* aIdx
1f80d 55 73 65 64 5b 69 5d 3d 3d 31 20 69 66 20 74 68  Used[i]==1 if th
1f80e 65 20 69 2d 74 68 20 69 6e 64 65 78 20 69 73 20  e i-th index is 
1f80f 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  used */.  int *a
1f810 58 52 65 66 20 3d 20 30 3b 20 20 20 20 20 20 20  XRef = 0;       
1f811 20 2f 2a 20 61 58 52 65 66 5b 69 5d 20 69 73 20   /* aXRef[i] is 
1f812 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70 43 68  the index in pCh
1f813 61 6e 67 65 73 2d 3e 61 5b 5d 20 6f 66 20 74 68  anges->a[] of th
1f814 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e.              
1f815 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e             ** an
1f816 20 65 78 70 72 65 73 73 69 6f 6e 20 66 6f 72 20   expression for 
1f817 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20  the i-th column 
1f818 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  of the table..  
1f819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f81a 20 20 20 20 20 20 20 2a 2a 20 61 58 52 65 66 5b         ** aXRef[
1f81b 69 5d 3d 3d 2d 31 20 69 66 20 74 68 65 20 69 2d  i]==-1 if the i-
1f81c 74 68 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74  th column is not
1f81d 20 63 68 61 6e 67 65 64 2e 20 2a 2f 0a 20 20 69   changed. */.  i
1f81e 6e 74 20 63 68 6e 67 52 6f 77 69 64 3b 20 20 20  nt chngRowid;   
1f81f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1f820 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
1f821 65 72 20 69 73 20 62 65 69 6e 67 20 63 68 61 6e  er is being chan
1f822 67 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ged */.  Expr *p
1f823 52 6f 77 69 64 45 78 70 72 20 3d 20 30 3b 20 20  RowidExpr = 0;  
1f824 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 64 65  /* Expression de
1f825 66 69 6e 69 6e 67 20 74 68 65 20 6e 65 77 20 72  fining the new r
1f826 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 2a 2f 0a  ecord number */.
1f827 20 20 69 6e 74 20 6f 70 65 6e 41 6c 6c 20 3d 20    int openAll = 
1f828 30 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  0;       /* True
1f829 20 69 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20   if all indices 
1f82a 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 65  need to be opene
1f82b 64 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65  d */.  AuthConte
1f82c 78 74 20 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a  xt sContext;  /*
1f82d 20 54 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69   The authorizati
1f82e 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  on context */.  
1f82f 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
1f830 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61         /* The na
1f831 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 72 65  me-context to re
1f832 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e  solve expression
1f833 73 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44  s in */.  int iD
1f834 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
1f835 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
1f836 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
1f837 65 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 20  e being updated 
1f838 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 43 6e 74 20  */.  int memCnt 
1f839 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  = 0;        /* M
1f83a 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20  emory cell used 
1f83b 66 6f 72 20 63 6f 75 6e 74 69 6e 67 20 72 6f 77  for counting row
1f83c 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 0a 23 69  s changed */..#i
1f83d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f83e 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20  T_TRIGGER.  int 
1f83f 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20  isView;         
1f840 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 79 69           /* Tryi
1f841 6e 67 20 74 6f 20 75 70 64 61 74 65 20 61 20 76  ng to update a v
1f842 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 74 72 69  iew */.  int tri
1f843 67 67 65 72 73 5f 65 78 69 73 74 20 3d 20 30 3b  ggers_exist = 0;
1f844 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1f845 20 61 6e 79 20 72 6f 77 20 74 72 69 67 67 65 72   any row trigger
1f846 73 20 65 78 69 73 74 20 2a 2f 0a 23 65 6e 64 69  s exist */.#endi
1f847 66 0a 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 20  f..  int newIdx 
1f848 20 20 20 20 20 3d 20 2d 31 3b 20 20 2f 2a 20 69       = -1;  /* i
1f849 6e 64 65 78 20 6f 66 20 74 72 69 67 67 65 72 20  ndex of trigger 
1f84a 22 6e 65 77 22 20 74 65 6d 70 20 74 61 62 6c 65  "new" temp table
1f84b 20 20 20 20 20 20 20 2a 2f 0a 20 20 69 6e 74 20         */.  int 
1f84c 6f 6c 64 49 64 78 20 20 20 20 20 20 3d 20 2d 31  oldIdx      = -1
1f84d 3b 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 74  ;  /* index of t
1f84e 72 69 67 67 65 72 20 22 6f 6c 64 22 20 74 65 6d  rigger "old" tem
1f84f 70 20 74 61 62 6c 65 20 20 20 20 20 20 20 2a 2f  p table       */
1f850 0a 0a 20 20 73 43 6f 6e 74 65 78 74 2e 70 50 61  ..  sContext.pPa
1f851 72 73 65 20 3d 20 30 3b 0a 20 20 64 62 20 3d 20  rse = 0;.  db = 
1f852 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
1f853 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
1f854 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
1f855 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75  ed ){.    goto u
1f856 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
1f857 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61   }.  assert( pTa
1f858 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29  bList->nSrc==1 )
1f859 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  ;..  /* Locate t
1f85a 68 65 20 74 61 62 6c 65 20 77 68 69 63 68 20 77  he table which w
1f85b 65 20 77 61 6e 74 20 74 6f 20 75 70 64 61 74 65  e want to update
1f85c 2e 20 0a 20 20 2a 2f 0a 20 20 70 54 61 62 20 3d  . .  */.  pTab =
1f85d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c   sqlite3SrcListL
1f85e 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54  ookup(pParse, pT
1f85f 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70  abList);.  if( p
1f860 54 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70  Tab==0 ) goto up
1f861 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
1f862 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1f863 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
1f864 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
1f865 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 46 69 67  hema);..  /* Fig
1f866 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 68 61  ure out if we ha
1f867 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 73 20  ve any triggers 
1f868 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c 65  and if the table
1f869 20 62 65 69 6e 67 0a 20 20 2a 2a 20 75 70 64 61   being.  ** upda
1f86a 74 65 64 20 69 73 20 61 20 76 69 65 77 0a 20 20  ted is a view.  
1f86b 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1f86c 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
1f86d 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20   triggers_exist 
1f86e 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
1f86f 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70  sExist(pParse, p
1f870 54 61 62 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20  Tab, TK_UPDATE, 
1f871 70 43 68 61 6e 67 65 73 29 3b 0a 20 20 69 73 56  pChanges);.  isV
1f872 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c  iew = pTab->pSel
1f873 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20  ect!=0;.#else.# 
1f874 64 65 66 69 6e 65 20 74 72 69 67 67 65 72 73 5f  define triggers_
1f875 65 78 69 73 74 20 30 0a 23 20 64 65 66 69 6e 65  exist 0.# define
1f876 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66   isView 0.#endif
1f877 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
1f878 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66  MIT_VIEW.# undef
1f879 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65   isView.# define
1f87a 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66   isView 0.#endif
1f87b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  ..  if( sqlite3I
1f87c 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65  sReadOnly(pParse
1f87d 2c 20 70 54 61 62 2c 20 74 72 69 67 67 65 72 73  , pTab, triggers
1f87e 5f 65 78 69 73 74 29 20 29 7b 0a 20 20 20 20 67  _exist) ){.    g
1f87f 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
1f880 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  up;.  }.  if( sq
1f881 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
1f882 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
1f883 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74  pTab) ){.    got
1f884 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
1f885 3b 0a 20 20 7d 0a 20 20 61 58 52 65 66 20 3d 20  ;.  }.  aXRef = 
1f886 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
1f887 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e  aw(db, sizeof(in
1f888 74 29 20 2a 20 70 54 61 62 2d 3e 6e 43 6f 6c 20  t) * pTab->nCol 
1f889 29 3b 0a 20 20 69 66 28 20 61 58 52 65 66 3d 3d  );.  if( aXRef==
1f88a 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
1f88b 63 6c 65 61 6e 75 70 3b 0a 20 20 66 6f 72 28 69  cleanup;.  for(i
1f88c 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
1f88d 3b 20 69 2b 2b 29 20 61 58 52 65 66 5b 69 5d 20  ; i++) aXRef[i] 
1f88e 3d 20 2d 31 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  = -1;..  /* If t
1f88f 68 65 72 65 20 61 72 65 20 46 4f 52 20 45 41 43  here are FOR EAC
1f890 48 20 52 4f 57 20 74 72 69 67 67 65 72 73 2c 20  H ROW triggers, 
1f891 61 6c 6c 6f 63 61 74 65 20 63 75 72 73 6f 72 73  allocate cursors
1f892 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73 70   for the.  ** sp
1f893 65 63 69 61 6c 20 4f 4c 44 20 61 6e 64 20 4e 45  ecial OLD and NE
1f894 57 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20  W tables.  */.  
1f895 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69  if( triggers_exi
1f896 73 74 20 29 7b 0a 20 20 20 20 6e 65 77 49 64 78  st ){.    newIdx
1f897 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
1f898 2b 3b 0a 20 20 20 20 6f 6c 64 49 64 78 20 3d 20  +;.    oldIdx = 
1f899 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
1f89a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61    }..  /* Alloca
1f89b 74 65 20 61 20 63 75 72 73 6f 72 73 20 66 6f 72  te a cursors for
1f89c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1f89d 73 65 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72  se table and for
1f89e 20 61 6c 6c 20 69 6e 64 69 63 65 73 2e 0a 20 20   all indices..  
1f89f 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 63 75 72  ** The index cur
1f8a0 73 6f 72 73 20 6d 69 67 68 74 20 6e 6f 74 20 62  sors might not b
1f8a1 65 20 75 73 65 64 2c 20 62 75 74 20 69 66 20 74  e used, but if t
1f8a2 68 65 79 20 61 72 65 20 75 73 65 64 20 74 68 65  hey are used the
1f8a3 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6f  y.  ** need to o
1f8a4 63 63 75 72 20 72 69 67 68 74 20 61 66 74 65 72  ccur right after
1f8a5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 75   the database cu
1f8a6 72 73 6f 72 2e 20 20 53 6f 20 67 6f 20 61 68 65  rsor.  So go ahe
1f8a7 61 64 20 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 6f  ad and.  ** allo
1f8a8 63 61 74 65 20 65 6e 6f 75 67 68 20 73 70 61 63  cate enough spac
1f8a9 65 2c 20 6a 75 73 74 20 69 6e 20 63 61 73 65 2e  e, just in case.
1f8aa 0a 20 20 2a 2f 0a 20 20 70 54 61 62 4c 69 73 74  .  */.  pTabList
1f8ab 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d  ->a[0].iCursor =
1f8ac 20 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e   iCur = pParse->
1f8ad 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49  nTab++;.  for(pI
1f8ae 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
1f8af 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
1f8b0 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50  ->pNext){.    pP
1f8b1 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
1f8b2 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  }..  /* Initiali
1f8b3 7a 65 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74  ze the name-cont
1f8b4 65 78 74 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  ext */.  memset(
1f8b5 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
1f8b6 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61  sNC));.  sNC.pPa
1f8b7 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
1f8b8 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
1f8b9 54 61 62 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52  TabList;..  /* R
1f8ba 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d  esolve the colum
1f8bb 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 74  n names in all t
1f8bc 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f  he expressions o
1f8bd 66 20 74 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68  f the.  ** of th
1f8be 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  e UPDATE stateme
1f8bf 6e 74 2e 20 20 41 6c 73 6f 20 66 69 6e 64 20 74  nt.  Also find t
1f8c0 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a  he column index.
1f8c1 20 20 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f    ** for each co
1f8c2 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61 74  lumn to be updat
1f8c3 65 64 20 69 6e 20 74 68 65 20 70 43 68 61 6e 67  ed in the pChang
1f8c4 65 73 20 61 72 72 61 79 2e 20 20 46 6f 72 20 65  es array.  For e
1f8c5 61 63 68 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20  ach.  ** column 
1f8c6 74 6f 20 62 65 20 75 70 64 61 74 65 64 2c 20 6d  to be updated, m
1f8c7 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65  ake sure we have
1f8c8 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74   authorization t
1f8c9 6f 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 74 68  o change.  ** th
1f8ca 61 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a  at column..  */.
1f8cb 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 30 3b    chngRowid = 0;
1f8cc 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43  .  for(i=0; i<pC
1f8cd 68 61 6e 67 65 73 2d 3e 6e 45 78 70 72 3b 20 69  hanges->nExpr; i
1f8ce 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
1f8cf 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e  ite3ExprResolveN
1f8d0 61 6d 65 73 28 26 73 4e 43 2c 20 70 43 68 61 6e  ames(&sNC, pChan
1f8d1 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ges->a[i].pExpr)
1f8d2 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75   ){.      goto u
1f8d3 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
1f8d4 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30     }.    for(j=0
1f8d5 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; j<pTab->nCol; 
1f8d6 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
1f8d7 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
1f8d8 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  Tab->aCol[j].zNa
1f8d9 6d 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b  me, pChanges->a[
1f8da 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  i].zName)==0 ){.
1f8db 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70          if( j==p
1f8dc 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
1f8dd 20 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77 69          chngRowi
1f8de 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
1f8df 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20 70 43   pRowidExpr = pC
1f8e0 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78  hanges->a[i].pEx
1f8e1 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  pr;.        }.  
1f8e2 20 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d        aXRef[j] =
1f8e3 20 69 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   i;.        brea
1f8e4 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1f8e5 0a 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62  .    if( j>=pTab
1f8e6 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
1f8e7 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77  if( sqlite3IsRow
1f8e8 69 64 28 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69  id(pChanges->a[i
1f8e9 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ].zName) ){.    
1f8ea 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20      chngRowid = 
1f8eb 31 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 77 69  1;.        pRowi
1f8ec 64 45 78 70 72 20 3d 20 70 43 68 61 6e 67 65 73  dExpr = pChanges
1f8ed 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
1f8ee 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f8ef 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f8f0 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
1f8f1 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 25 73 22 2c  uch column: %s",
1f8f2 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e   pChanges->a[i].
1f8f3 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
1f8f4 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
1f8f5 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nup;.      }.   
1f8f6 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1f8f7 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
1f8f8 54 49 4f 4e 0a 20 20 20 20 7b 0a 20 20 20 20 20  TION.    {.     
1f8f9 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72   int rc;.      r
1f8fa 63 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43  c = sqlite3AuthC
1f8fb 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
1f8fc 49 54 45 5f 55 50 44 41 54 45 2c 20 70 54 61 62  ITE_UPDATE, pTab
1f8fd 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  ->zName,.       
1f8fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f8ff 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a      pTab->aCol[j
1f900 5d 2e 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62  ].zName, db->aDb
1f901 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
1f902 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1f903 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20  TE_DENY ){.     
1f904 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
1f905 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 65  leanup;.      }e
1f906 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
1f907 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20  TE_IGNORE ){.   
1f908 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20       aXRef[j] = 
1f909 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
1f90a 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  }.#endif.  }..  
1f90b 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  /* Allocate memo
1f90c 72 79 20 66 6f 72 20 74 68 65 20 61 72 72 61 79  ry for the array
1f90d 20 61 70 49 64 78 5b 5d 20 61 6e 64 20 66 69 6c   apIdx[] and fil
1f90e 6c 20 69 74 20 77 69 74 68 20 70 6f 69 6e 74 65  l it with pointe
1f90f 72 73 20 74 6f 20 65 76 65 72 79 0a 20 20 2a 2a  rs to every.  **
1f910 20 69 6e 64 65 78 20 74 68 61 74 20 6e 65 65 64   index that need
1f911 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e  s to be updated.
1f912 20 20 49 6e 64 69 63 65 73 20 6f 6e 6c 79 20 6e    Indices only n
1f913 65 65 64 20 75 70 64 61 74 69 6e 67 20 69 66 20  eed updating if 
1f914 74 68 65 69 72 0a 20 20 2a 2a 20 6b 65 79 20 69  their.  ** key i
1f915 6e 63 6c 75 64 65 73 20 6f 6e 65 20 6f 66 20 74  ncludes one of t
1f916 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 64  he columns named
1f917 20 69 6e 20 70 43 68 61 6e 67 65 73 20 6f 72 20   in pChanges or 
1f918 69 66 20 74 68 65 20 72 65 63 6f 72 64 0a 20 20  if the record.  
1f919 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ** number of the
1f91a 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20   original table 
1f91b 65 6e 74 72 79 20 69 73 20 63 68 61 6e 67 69 6e  entry is changin
1f91c 67 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 6e 49  g..  */.  for(nI
1f91d 64 78 3d 6e 49 64 78 54 6f 74 61 6c 3d 30 2c 20  dx=nIdxTotal=0, 
1f91e 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
1f91f 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
1f920 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78 54  dx->pNext, nIdxT
1f921 6f 74 61 6c 2b 2b 29 7b 0a 20 20 20 20 69 66 28  otal++){.    if(
1f922 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20   chngRowid ){.  
1f923 20 20 20 20 69 20 3d 20 30 3b 0a 20 20 20 20 7d      i = 0;.    }
1f924 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 66 6f 72  else {.      for
1f925 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43  (i=0; i<pIdx->nC
1f926 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
1f927 20 20 20 20 20 69 66 28 20 61 58 52 65 66 5b 70       if( aXRef[p
1f928 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
1f929 5d 3e 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  ]>=0 ) break;.  
1f92a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1f92b 69 66 28 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c  if( i<pIdx->nCol
1f92c 75 6d 6e 20 29 20 6e 49 64 78 2b 2b 3b 0a 20 20  umn ) nIdx++;.  
1f92d 7d 0a 20 20 69 66 28 20 6e 49 64 78 54 6f 74 61  }.  if( nIdxTota
1f92e 6c 3e 30 20 29 7b 0a 20 20 20 20 61 70 49 64 78  l>0 ){.    apIdx
1f92f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1f930 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
1f931 28 49 6e 64 65 78 2a 29 20 2a 20 6e 49 64 78 20  (Index*) * nIdx 
1f932 2b 20 6e 49 64 78 54 6f 74 61 6c 20 29 3b 0a 20  + nIdxTotal );. 
1f933 20 20 20 69 66 28 20 61 70 49 64 78 3d 3d 30 20     if( apIdx==0 
1f934 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c  ) goto update_cl
1f935 65 61 6e 75 70 3b 0a 20 20 20 20 61 49 64 78 55  eanup;.    aIdxU
1f936 73 65 64 20 3d 20 28 63 68 61 72 2a 29 26 61 70  sed = (char*)&ap
1f937 49 64 78 5b 6e 49 64 78 5d 3b 0a 20 20 7d 0a 20  Idx[nIdx];.  }. 
1f938 20 66 6f 72 28 6e 49 64 78 3d 6a 3d 30 2c 20 70   for(nIdx=j=0, p
1f939 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
1f93a 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
1f93b 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a  x->pNext, j++){.
1f93c 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69      if( chngRowi
1f93d 64 20 29 7b 0a 20 20 20 20 20 20 69 20 3d 20 30  d ){.      i = 0
1f93e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1f93f 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49     for(i=0; i<pI
1f940 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  dx->nColumn; i++
1f941 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  ){.        if( a
1f942 58 52 65 66 5b 70 49 64 78 2d 3e 61 69 43 6f 6c  XRef[pIdx->aiCol
1f943 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 20 62 72 65  umn[i]]>=0 ) bre
1f944 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1f945 7d 0a 20 20 20 20 69 66 28 20 69 3c 70 49 64 78  }.    if( i<pIdx
1f946 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
1f947 20 20 20 61 70 49 64 78 5b 6e 49 64 78 2b 2b 5d     apIdx[nIdx++]
1f948 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20 61   = pIdx;.      a
1f949 49 64 78 55 73 65 64 5b 6a 5d 20 3d 20 31 3b 0a  IdxUsed[j] = 1;.
1f94a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f94b 20 61 49 64 78 55 73 65 64 5b 6a 5d 20 3d 20 30   aIdxUsed[j] = 0
1f94c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
1f94d 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69  * Begin generati
1f94e 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  ng code..  */.  
1f94f 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1f950 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
1f951 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70  ( v==0 ) goto up
1f952 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
1f953 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
1f954 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56  ed==0 ) sqlite3V
1f955 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28  dbeCountChanges(
1f956 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67  v);.  sqlite3Beg
1f957 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
1f958 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29  (pParse, 1, iDb)
1f959 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
1f95a 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1f95b 42 4c 45 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c  BLE.  /* Virtual
1f95c 20 74 61 62 6c 65 73 20 6d 75 73 74 20 62 65 20   tables must be 
1f95d 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65  handled separate
1f95e 6c 79 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69  ly */.  if( IsVi
1f95f 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
1f960 20 20 20 75 70 64 61 74 65 56 69 72 74 75 61 6c     updateVirtual
1f961 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
1f962 61 62 4c 69 73 74 2c 20 70 54 61 62 2c 20 70 43  abList, pTab, pC
1f963 68 61 6e 67 65 73 2c 20 70 52 6f 77 69 64 45 78  hanges, pRowidEx
1f964 70 72 2c 20 61 58 52 65 66 2c 0a 20 20 20 20 20  pr, aXRef,.     
1f965 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f966 20 20 70 57 68 65 72 65 29 3b 0a 20 20 20 20 70    pWhere);.    p
1f967 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20 20 70  Where = 0;.    p
1f968 54 61 62 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20  TabList = 0;.   
1f969 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
1f96a 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  anup;.  }.#endif
1f96b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
1f96c 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
1f96d 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65  in all the expre
1f96e 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20  ssions in the.  
1f96f 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  ** WHERE clause.
1f970 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
1f971 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e 61  te3ExprResolveNa
1f972 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65  mes(&sNC, pWhere
1f973 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70  ) ){.    goto up
1f974 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
1f975 7d 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68  }..  /* Start th
1f976 65 20 76 69 65 77 20 63 6f 6e 74 65 78 74 0a 20  e view context. 
1f977 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77   */.  if( isView
1f978 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   ){.    sqlite3A
1f979 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70  uthContextPush(p
1f97a 50 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74  Parse, &sContext
1f97b 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
1f97c 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
1f97d 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 75 70  are trying to up
1f97e 64 61 74 65 20 61 20 76 69 65 77 2c 20 72 65 61  date a view, rea
1f97f 6c 69 7a 65 20 74 68 61 74 20 76 69 65 77 20 69  lize that view i
1f980 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d  nto.  ** a ephem
1f981 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  eral table..  */
1f982 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b  .  if( isView ){
1f983 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 56 69  .    Select *pVi
1f984 65 77 3b 0a 20 20 20 20 70 56 69 65 77 20 3d 20  ew;.    pView = 
1f985 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
1f986 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 65 6c 65  (db, pTab->pSele
1f987 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ct);.    sqlite3
1f988 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
1f989 56 69 65 77 2c 20 53 52 54 5f 45 70 68 65 6d 54  View, SRT_EphemT
1f98a 61 62 2c 20 69 43 75 72 2c 20 30 2c 20 30 2c 20  ab, iCur, 0, 0, 
1f98b 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
1f98c 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
1f98d 56 69 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  View);.  }..  /*
1f98e 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62   Begin the datab
1f98f 61 73 65 20 73 63 61 6e 0a 20 20 2a 2f 0a 20 20  ase scan.  */.  
1f990 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  pWInfo = sqlite3
1f991 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73  WhereBegin(pPars
1f992 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68  e, pTabList, pWh
1f993 65 72 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ere, 0);.  if( p
1f994 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20  WInfo==0 ) goto 
1f995 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
1f996 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74  .  /* Remember t
1f997 68 65 20 72 6f 77 69 64 20 6f 66 20 65 76 65 72  he rowid of ever
1f998 79 20 69 74 65 6d 20 74 6f 20 62 65 20 75 70 64  y item to be upd
1f999 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ated..  */.  sql
1f99a 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1f99b 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1f99c 20 3f 20 4f 50 5f 56 52 6f 77 69 64 20 3a 20 4f   ? OP_VRowid : O
1f99d 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 30  P_Rowid, iCur, 0
1f99e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1f99f 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46 69 66 6f  AddOp(v, OP_Fifo
1f9a0 57 72 69 74 65 2c 20 30 2c 20 30 29 3b 0a 0a 20  Write, 0, 0);.. 
1f9a1 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74 61   /* End the data
1f9a2 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a  base scan loop..
1f9a3 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 57 68    */.  sqlite3Wh
1f9a4 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
1f9a5 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
1f9a6 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 75 70   the count of up
1f9a7 64 61 74 65 64 20 72 6f 77 73 0a 20 20 2a 2f 0a  dated rows.  */.
1f9a8 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
1f9a9 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
1f9aa 77 73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74  ws && !pParse->t
1f9ab 72 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20  rigStack ){.    
1f9ac 6d 65 6d 43 6e 74 20 3d 20 70 50 61 72 73 65 2d  memCnt = pParse-
1f9ad 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73 71 6c  >nMem++;.    sql
1f9ae 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1f9af 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 30 2c 20 6d   OP_MemInt, 0, m
1f9b0 65 6d 43 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 69  emCnt);.  }..  i
1f9b1 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  f( triggers_exis
1f9b2 74 20 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65 61  t ){.    /* Crea
1f9b3 74 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73  te pseudo-tables
1f9b4 20 66 6f 72 20 4e 45 57 20 61 6e 64 20 4f 4c 44   for NEW and OLD
1f9b5 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
1f9b6 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1f9b7 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6f  OP_OpenPseudo, o
1f9b8 6c 64 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 73  ldIdx, 0);.    s
1f9b9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1f9ba 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75  v, OP_SetNumColu
1f9bb 6d 6e 73 2c 20 6f 6c 64 49 64 78 2c 20 70 54 61  mns, oldIdx, pTa
1f9bc 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71  b->nCol);.    sq
1f9bd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1f9be 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c  , OP_OpenPseudo,
1f9bf 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a 20 20 20   newIdx, 0);.   
1f9c0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f9c1 70 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f  p(v, OP_SetNumCo
1f9c2 6c 75 6d 6e 73 2c 20 6e 65 77 49 64 78 2c 20 70  lumns, newIdx, p
1f9c3 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a 20 20 20  Tab->nCol);..   
1f9c4 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74   /* The top of t
1f9c5 68 65 20 75 70 64 61 74 65 20 6c 6f 6f 70 20 66  he update loop f
1f9c6 6f 72 20 77 68 65 6e 20 74 68 65 72 65 20 61 72  or when there ar
1f9c7 65 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20  e triggers..    
1f9c8 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71  */.    addr = sq
1f9c9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1f9ca 2c 20 4f 50 5f 46 69 66 6f 52 65 61 64 2c 20 30  , OP_FifoRead, 0
1f9cb 2c 20 30 29 3b 0a 0a 20 20 20 20 69 66 28 20 21  , 0);..    if( !
1f9cc 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20  isView ){.      
1f9cd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f9ce 28 76 2c 20 4f 50 5f 44 75 70 2c 20 30 2c 20 30  (v, OP_Dup, 0, 0
1f9cf 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f9d0 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1f9d1 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Dup, 0, 0);.    
1f9d2 20 20 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72 73    /* Open a curs
1f9d3 6f 72 20 61 6e 64 20 6d 61 6b 65 20 69 74 20 70  or and make it p
1f9d4 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 65 63 6f  oint to the reco
1f9d5 72 64 20 74 68 61 74 20 69 73 0a 20 20 20 20 20  rd that is.     
1f9d6 20 2a 2a 20 62 65 69 6e 67 20 75 70 64 61 74 65   ** being update
1f9d7 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
1f9d8 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
1f9d9 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c  le(pParse, iCur,
1f9da 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f   iDb, pTab, OP_O
1f9db 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 7d 0a  penRead);.    }.
1f9dc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f9dd 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65 47  ddOp(v, OP_MoveG
1f9de 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a 0a 20 20  e, iCur, 0);..  
1f9df 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68    /* Generate th
1f9e0 65 20 4f 4c 44 20 74 61 62 6c 65 0a 20 20 20 20  e OLD table.    
1f9e1 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
1f9e2 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 6f  beAddOp(v, OP_Ro
1f9e3 77 69 64 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20  wid, iCur, 0);. 
1f9e4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1f9e5 64 4f 70 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74  dOp(v, OP_RowDat
1f9e6 61 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20  a, iCur, 0);.   
1f9e7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f9e8 70 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  p(v, OP_Insert, 
1f9e9 6f 6c 64 49 64 78 2c 20 30 29 3b 0a 0a 20 20 20  oldIdx, 0);..   
1f9ea 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65   /* Generate the
1f9eb 20 4e 45 57 20 74 61 62 6c 65 0a 20 20 20 20 2a   NEW table.    *
1f9ec 2f 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f  /.    if( chngRo
1f9ed 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  wid ){.      sql
1f9ee 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43  ite3ExprCodeAndC
1f9ef 61 63 68 65 28 70 50 61 72 73 65 2c 20 70 52 6f  ache(pParse, pRo
1f9f0 77 69 64 45 78 70 72 29 3b 0a 20 20 20 20 7d 65  widExpr);.    }e
1f9f1 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
1f9f2 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1f9f3 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 30  P_Rowid, iCur, 0
1f9f4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
1f9f5 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
1f9f6 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
1f9f7 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( i==pTab->iPK
1f9f8 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ey ){.        sq
1f9f9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1f9fa 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29  , OP_Null, 0, 0)
1f9fb 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e  ;.        contin
1f9fc 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ue;.      }.    
1f9fd 20 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b 0a    j = aXRef[i];.
1f9fe 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20 29 7b        if( j<0 ){
1f9ff 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1fa00 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1fa01 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 29  Column, iCur, i)
1fa02 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1fa03 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76  3ColumnDefault(v
1fa04 2c 20 70 54 61 62 2c 20 69 29 3b 0a 20 20 20 20  , pTab, i);.    
1fa05 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1fa06 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1fa07 41 6e 64 43 61 63 68 65 28 70 50 61 72 73 65 2c  AndCache(pParse,
1fa08 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e   pChanges->a[j].
1fa09 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a  pExpr);.      }.
1fa0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1fa0b 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1fa0c 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 54 61  _MakeRecord, pTa
1fa0d 62 2d 3e 6e 43 6f 6c 2c 20 30 29 3b 0a 20 20 20  b->nCol, 0);.   
1fa0e 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a   if( !isView ){.
1fa0f 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62        sqlite3Tab
1fa10 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  leAffinityStr(v,
1fa11 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20   pTab);.    }.  
1fa12 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
1fa13 72 72 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  rr ) goto update
1fa14 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 73 71  _cleanup;.    sq
1fa15 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1fa16 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77  , OP_Insert, new
1fa17 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  Idx, 0);.    if(
1fa18 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20   !isView ){.    
1fa19 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fa1a 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  Op(v, OP_Close, 
1fa1b 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  iCur, 0);.    }.
1fa1c 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 74 68 65  .    /* Fire the
1fa1d 20 42 45 46 4f 52 45 20 61 6e 64 20 49 4e 53 54   BEFORE and INST
1fa1e 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 0a  EAD OF triggers.
1fa1f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73      */.    if( s
1fa20 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
1fa21 67 67 65 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  gger(pParse, TK_
1fa22 55 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73  UPDATE, pChanges
1fa23 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  , TRIGGER_BEFORE
1fa24 2c 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20  , pTab,.        
1fa25 20 20 6e 65 77 49 64 78 2c 20 6f 6c 64 49 64 78    newIdx, oldIdx
1fa26 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72 29  , onError, addr)
1fa27 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75   ){.      goto u
1fa28 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
1fa29 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
1fa2a 21 69 73 56 69 65 77 20 26 26 20 21 49 73 56 69  !isView && !IsVi
1fa2b 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
1fa2c 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 4f 70     /* .    ** Op
1fa2d 65 6e 20 65 76 65 72 79 20 69 6e 64 65 78 20 74  en every index t
1fa2e 68 61 74 20 6e 65 65 64 73 20 75 70 64 61 74 69  hat needs updati
1fa2f 6e 67 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69  ng.  Note that i
1fa30 66 20 61 6e 79 0a 20 20 20 20 2a 2a 20 69 6e 64  f any.    ** ind
1fa31 65 78 20 63 6f 75 6c 64 20 70 6f 74 65 6e 74 69  ex could potenti
1fa32 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 61 20 52 45  ally invoke a RE
1fa33 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74 20 72  PLACE conflict r
1fa34 65 73 6f 6c 75 74 69 6f 6e 20 0a 20 20 20 20 2a  esolution .    *
1fa35 2a 20 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 77  * action, then w
1fa36 65 20 6e 65 65 64 20 74 6f 20 6f 70 65 6e 20 61  e need to open a
1fa37 6c 6c 20 69 6e 64 69 63 65 73 20 62 65 63 61 75  ll indices becau
1fa38 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64  se we might need
1fa39 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 64 65  .    ** to be de
1fa3a 6c 65 74 69 6e 67 20 73 6f 6d 65 20 72 65 63 6f  leting some reco
1fa3b 72 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  rds..    */.    
1fa3c 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
1fa3d 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69  (pParse, iCur, i
1fa3e 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65  Db, pTab, OP_Ope
1fa3f 6e 57 72 69 74 65 29 3b 20 0a 20 20 20 20 69 66  nWrite); .    if
1fa40 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ( onError==OE_Re
1fa41 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20 6f  place ){.      o
1fa42 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20  penAll = 1;.    
1fa43 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6f 70 65  }else{.      ope
1fa44 6e 41 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  nAll = 0;.      
1fa45 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
1fa46 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
1fa47 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
1fa48 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
1fa49 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ->onError==OE_Re
1fa4a 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20  place ){.       
1fa4b 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a     openAll = 1;.
1fa4c 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
1fa4d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1fa4e 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72   }.    }.    for
1fa4f 28 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d  (i=0, pIdx=pTab-
1fa50 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
1fa51 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c  Idx=pIdx->pNext,
1fa52 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
1fa53 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 49 64 78   openAll || aIdx
1fa54 55 73 65 64 5b 69 5d 20 29 7b 0a 20 20 20 20 20  Used[i] ){.     
1fa55 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
1fa56 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
1fa57 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
1fa58 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71  Idx);.        sq
1fa59 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1fa5a 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 44  , OP_Integer, iD
1fa5b 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  b, 0);.        s
1fa5c 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c  qlite3VdbeOp3(v,
1fa5d 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69   OP_OpenWrite, i
1fa5e 43 75 72 2b 69 2b 31 2c 20 70 49 64 78 2d 3e 74  Cur+i+1, pIdx->t
1fa5f 6e 75 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  num,.           
1fa60 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
1fa61 72 2a 29 70 4b 65 79 2c 20 50 33 5f 4b 45 59 49  r*)pKey, P3_KEYI
1fa62 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
1fa63 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1fa64 61 72 73 65 2d 3e 6e 54 61 62 3e 69 43 75 72 2b  arse->nTab>iCur+
1fa65 69 2b 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  i+1 );.      }. 
1fa66 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 6f     }..    /* Loo
1fa67 70 20 6f 76 65 72 20 65 76 65 72 79 20 72 65 63  p over every rec
1fa68 6f 72 64 20 74 68 61 74 20 6e 65 65 64 73 20 75  ord that needs u
1fa69 70 64 61 74 69 6e 67 2e 20 20 57 65 20 68 61 76  pdating.  We hav
1fa6a 65 20 74 6f 20 6c 6f 61 64 0a 20 20 20 20 2a 2a  e to load.    **
1fa6b 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 66 6f   the old data fo
1fa6c 72 20 65 61 63 68 20 72 65 63 6f 72 64 20 74 6f  r each record to
1fa6d 20 62 65 20 75 70 64 61 74 65 64 20 62 65 63 61   be updated beca
1fa6e 75 73 65 20 73 6f 6d 65 20 63 6f 6c 75 6d 6e 73  use some columns
1fa6f 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 6e 6f  .    ** might no
1fa70 74 20 63 68 61 6e 67 65 20 61 6e 64 20 77 65 20  t change and we 
1fa71 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 6f 70  will need to cop
1fa72 79 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e  y the old value.
1fa73 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68  .    ** Also, th
1fa74 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20 6e 65  e old data is ne
1fa75 65 64 65 64 20 74 6f 20 64 65 6c 65 74 65 20 74  eded to delete t
1fa76 68 65 20 6f 6c 64 20 69 6e 64 65 78 20 65 6e 74  he old index ent
1fa77 72 69 65 73 2e 0a 20 20 20 20 2a 2a 20 53 6f 20  ries..    ** So 
1fa78 6d 61 6b 65 20 74 68 65 20 63 75 72 73 6f 72 20  make the cursor 
1fa79 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6f 6c 64  point at the old
1fa7a 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a   record..    */.
1fa7b 20 20 20 20 69 66 28 20 21 74 72 69 67 67 65 72      if( !trigger
1fa7c 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20  s_exist ){.     
1fa7d 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
1fa7e 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46  dbeAddOp(v, OP_F
1fa7f 69 66 6f 52 65 61 64 2c 20 30 2c 20 30 29 3b 0a  ifoRead, 0, 0);.
1fa80 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1fa81 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70  eAddOp(v, OP_Dup
1fa82 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  , 0, 0);.    }. 
1fa83 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fa84 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  dOp(v, OP_NotExi
1fa85 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 29  sts, iCur, addr)
1fa86 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ;..    /* If the
1fa87 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77   record number w
1fa88 69 6c 6c 20 63 68 61 6e 67 65 2c 20 70 75 73 68  ill change, push
1fa89 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
1fa8a 65 72 20 61 73 20 69 74 0a 20 20 20 20 2a 2a 20  er as it.    ** 
1fa8b 77 69 6c 6c 20 62 65 20 61 66 74 65 72 20 74 68  will be after th
1fa8c 65 20 75 70 64 61 74 65 2e 20 28 54 68 65 20 6f  e update. (The o
1fa8d 6c 64 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ld record number
1fa8e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 20 20   is currently.  
1fa8f 20 20 2a 2a 20 6f 6e 20 74 6f 70 20 6f 66 20 74    ** on top of t
1fa90 68 65 20 73 74 61 63 6b 2e 29 0a 20 20 20 20 2a  he stack.).    *
1fa91 2f 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f  /.    if( chngRo
1fa92 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  wid ){.      sql
1fa93 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1fa94 72 73 65 2c 20 70 52 6f 77 69 64 45 78 70 72 29  rse, pRowidExpr)
1fa95 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1fa96 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
1fa97 75 73 74 42 65 49 6e 74 2c 20 30 2c 20 30 29 3b  ustBeInt, 0, 0);
1fa98 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
1fa99 6f 6d 70 75 74 65 20 6e 65 77 20 64 61 74 61 20  ompute new data 
1fa9a 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72 64 2e  for this record.
1fa9b 20 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f    .    */.    fo
1fa9c 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
1fa9d 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
1fa9e 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50   if( i==pTab->iP
1fa9f 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Key ){.        s
1faa0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1faa1 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30  v, OP_Null, 0, 0
1faa2 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69  );.        conti
1faa3 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nue;.      }.   
1faa4 20 20 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b     j = aXRef[i];
1faa5 0a 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20 29  .      if( j<0 )
1faa6 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1faa7 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1faa8 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69  _Column, iCur, i
1faa9 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1faaa 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28  e3ColumnDefault(
1faab 76 2c 20 70 54 61 62 2c 20 69 29 3b 0a 20 20 20  v, pTab, i);.   
1faac 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1faad 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
1faae 65 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67  e(pParse, pChang
1faaf 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 29 3b  es->a[j].pExpr);
1fab0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1fab1 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74 72      /* Do constr
1fab2 61 69 6e 74 20 63 68 65 63 6b 73 0a 20 20 20 20  aint checks.    
1fab3 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65  */.    sqlite3Ge
1fab4 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  nerateConstraint
1fab5 43 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20 70  Checks(pParse, p
1fab6 54 61 62 2c 20 69 43 75 72 2c 20 61 49 64 78 55  Tab, iCur, aIdxU
1fab7 73 65 64 2c 20 63 68 6e 67 52 6f 77 69 64 2c 20  sed, chngRowid, 
1fab8 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
1fab9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1faba 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 2c 20 61        onError, a
1fabb 64 64 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65  ddr);..    /* De
1fabc 6c 65 74 65 20 74 68 65 20 6f 6c 64 20 69 6e 64  lete the old ind
1fabd 69 63 65 73 20 66 6f 72 20 74 68 65 20 63 75 72  ices for the cur
1fabe 72 65 6e 74 20 72 65 63 6f 72 64 2e 0a 20 20 20  rent record..   
1fabf 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47   */.    sqlite3G
1fac0 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44  enerateRowIndexD
1fac1 65 6c 65 74 65 28 76 2c 20 70 54 61 62 2c 20 69  elete(v, pTab, i
1fac2 43 75 72 2c 20 61 49 64 78 55 73 65 64 29 3b 0a  Cur, aIdxUsed);.
1fac3 0a 20 20 20 20 2f 2a 20 49 66 20 63 68 61 6e 67  .    /* If chang
1fac4 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 6e  ing the record n
1fac5 75 6d 62 65 72 2c 20 64 65 6c 65 74 65 20 74 68  umber, delete th
1fac6 65 20 6f 6c 64 20 72 65 63 6f 72 64 2e 0a 20 20  e old record..  
1fac7 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 68 6e    */.    if( chn
1fac8 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  gRowid ){.      
1fac9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1faca 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69  (v, OP_Delete, i
1facb 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a  Cur, 0);.    }..
1facc 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68      /* Create th
1facd 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72  e new index entr
1face 69 65 73 20 61 6e 64 20 74 68 65 20 6e 65 77 20  ies and the new 
1facf 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20  record..    */. 
1fad0 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65     sqlite3Comple
1fad1 74 65 49 6e 73 65 72 74 69 6f 6e 28 70 50 61 72  teInsertion(pPar
1fad2 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20  se, pTab, iCur, 
1fad3 61 49 64 78 55 73 65 64 2c 20 63 68 6e 67 52 6f  aIdxUsed, chngRo
1fad4 77 69 64 2c 20 31 2c 20 2d 31 2c 20 30 29 3b 0a  wid, 1, -1, 0);.
1fad5 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d    }..  /* Increm
1fad6 65 6e 74 20 74 68 65 20 72 6f 77 20 63 6f 75 6e  ent the row coun
1fad7 74 65 72 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ter .  */.  if( 
1fad8 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
1fad9 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20  TE_CountRows && 
1fada 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  !pParse->trigSta
1fadb 63 6b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ck){.    sqlite3
1fadc 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1fadd 4d 65 6d 49 6e 63 72 2c 20 31 2c 20 6d 65 6d 43  MemIncr, 1, memC
1fade 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  nt);.  }..  /* I
1fadf 66 20 74 68 65 72 65 20 61 72 65 20 74 72 69 67  f there are trig
1fae0 67 65 72 73 2c 20 63 6c 6f 73 65 20 61 6c 6c 20  gers, close all 
1fae1 74 68 65 20 63 75 72 73 6f 72 73 20 61 66 74 65  the cursors afte
1fae2 72 20 65 61 63 68 20 69 74 65 72 61 74 69 6f 6e  r each iteration
1fae3 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 74 68  .  ** through th
1fae4 65 20 6c 6f 6f 70 2e 20 20 54 68 65 20 66 69 72  e loop.  The fir
1fae5 65 20 74 68 65 20 61 66 74 65 72 20 74 72 69 67  e the after trig
1fae6 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  gers..  */.  if(
1fae7 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20   triggers_exist 
1fae8 29 7b 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  ){.    if( !isVi
1fae9 65 77 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  ew ){.      for(
1faea 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=0, pIdx=pTab->
1faeb 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
1faec 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
1faed 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  i++){.        if
1faee 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 49 64  ( openAll || aId
1faef 78 55 73 65 64 5b 69 5d 20 29 0a 20 20 20 20 20  xUsed[i] ).     
1faf0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1faf1 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73  AddOp(v, OP_Clos
1faf2 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20 30 29 3b  e, iCur+i+1, 0);
1faf3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1faf4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1faf5 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75  v, OP_Close, iCu
1faf6 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  r, 0);.    }.   
1faf7 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 64 65   if( sqlite3Code
1faf8 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
1faf9 65 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43  e, TK_UPDATE, pC
1fafa 68 61 6e 67 65 73 2c 20 54 52 49 47 47 45 52 5f  hanges, TRIGGER_
1fafb 41 46 54 45 52 2c 20 70 54 61 62 2c 20 0a 20 20  AFTER, pTab, .  
1fafc 20 20 20 20 20 20 20 20 6e 65 77 49 64 78 2c 20          newIdx, 
1fafd 6f 6c 64 49 64 78 2c 20 6f 6e 45 72 72 6f 72 2c  oldIdx, onError,
1fafe 20 61 64 64 72 29 20 29 7b 0a 20 20 20 20 20 20   addr) ){.      
1faff 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
1fb00 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  nup;.    }.  }..
1fb01 20 20 2f 2a 20 52 65 70 65 61 74 20 74 68 65 20    /* Repeat the 
1fb02 61 62 6f 76 65 20 77 69 74 68 20 74 68 65 20 6e  above with the n
1fb03 65 78 74 20 72 65 63 6f 72 64 20 74 6f 20 62 65  ext record to be
1fb04 20 75 70 64 61 74 65 64 2c 20 75 6e 74 69 6c 0a   updated, until.
1fb05 20 20 2a 2a 20 61 6c 6c 20 72 65 63 6f 72 64 20    ** all record 
1fb06 73 65 6c 65 63 74 65 64 20 62 79 20 74 68 65 20  selected by the 
1fb07 57 48 45 52 45 20 63 6c 61 75 73 65 20 68 61 76  WHERE clause hav
1fb08 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 2e 0a  e been updated..
1fb09 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
1fb0a 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f  beAddOp(v, OP_Go
1fb0b 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20  to, 0, addr);.  
1fb0c 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1fb0d 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20  ere(v, addr);.. 
1fb0e 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61   /* Close all ta
1fb0f 62 6c 65 73 20 69 66 20 74 68 65 72 65 20 77 65  bles if there we
1fb10 72 65 20 6e 6f 20 46 4f 52 20 45 41 43 48 20 52  re no FOR EACH R
1fb11 4f 57 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  OW triggers */. 
1fb12 20 69 66 28 20 21 74 72 69 67 67 65 72 73 5f 65   if( !triggers_e
1fb13 78 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28  xist ){.    for(
1fb14 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=0, pIdx=pTab->
1fb15 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
1fb16 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
1fb17 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
1fb18 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 49 64 78 55  openAll || aIdxU
1fb19 73 65 64 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20  sed[i] ){.      
1fb1a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fb1b 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  Op(v, OP_Close, 
1fb1c 69 43 75 72 2b 69 2b 31 2c 20 30 29 3b 0a 20 20  iCur+i+1, 0);.  
1fb1d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1fb1e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fb1f 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
1fb20 75 72 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b  ur, 0);.  }else{
1fb21 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1fb22 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73  AddOp(v, OP_Clos
1fb23 65 2c 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a 20  e, newIdx, 0);. 
1fb24 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fb25 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
1fb26 20 6f 6c 64 49 64 78 2c 20 30 29 3b 0a 20 20 7d   oldIdx, 0);.  }
1fb27 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75  ..  /*.  ** Retu
1fb28 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
1fb29 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 20   rows that were 
1fb2a 63 68 61 6e 67 65 64 2e 20 49 66 20 74 68 69 73  changed. If this
1fb2b 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a   routine is .  *
1fb2c 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64  * generating cod
1fb2d 65 20 62 65 63 61 75 73 65 20 6f 66 20 61 20 63  e because of a c
1fb2e 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65  all to sqlite3Ne
1fb2f 73 74 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20  stedParse(), do 
1fb30 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20  not.  ** invoke 
1fb31 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
1fb32 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  ction..  */.  if
1fb33 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
1fb34 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26  LITE_CountRows &
1fb35 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53  & !pParse->trigS
1fb36 74 61 63 6b 20 26 26 20 70 50 61 72 73 65 2d 3e  tack && pParse->
1fb37 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  nested==0 ){.   
1fb38 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fb39 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c  p(v, OP_MemLoad,
1fb3a 20 6d 65 6d 43 6e 74 2c 20 30 29 3b 0a 20 20 20   memCnt, 0);.   
1fb3b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fb3c 70 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b  p(v, OP_Callback
1fb3d 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 1, 0);.    sql
1fb3e 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
1fb3f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71  ls(v, 1);.    sq
1fb40 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
1fb41 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
1fb42 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 75 70  E_NAME, "rows up
1fb43 64 61 74 65 64 22 2c 20 50 33 5f 53 54 41 54 49  dated", P3_STATI
1fb44 43 29 3b 0a 20 20 7d 0a 0a 75 70 64 61 74 65 5f  C);.  }..update_
1fb45 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74  cleanup:.  sqlit
1fb46 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70  e3AuthContextPop
1fb47 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73  (&sContext);.  s
1fb48 71 6c 69 74 65 33 5f 66 72 65 65 28 61 70 49 64  qlite3_free(apId
1fb49 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  x);.  sqlite3_fr
1fb4a 65 65 28 61 58 52 65 66 29 3b 0a 20 20 73 71 6c  ee(aXRef);.  sql
1fb4b 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
1fb4c 65 28 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73  e(pTabList);.  s
1fb4d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
1fb4e 6c 65 74 65 28 70 43 68 61 6e 67 65 73 29 3b 0a  lete(pChanges);.
1fb4f 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
1fb50 65 74 65 28 70 57 68 65 72 65 29 3b 0a 20 20 72  ete(pWhere);.  r
1fb51 65 74 75 72 6e 3b 0a 7d 0a 0a 23 69 66 6e 64 65  eturn;.}..#ifnde
1fb52 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1fb53 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a  RTUALTABLE./*.**
1fb54 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
1fb55 6f 72 20 61 6e 20 55 50 44 41 54 45 20 6f 66 20  or an UPDATE of 
1fb56 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
1fb57 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 61 74  .**.** The strat
1fb58 65 67 79 20 69 73 20 74 68 61 74 20 77 65 20 63  egy is that we c
1fb59 72 65 61 74 65 20 61 6e 20 65 70 68 65 6d 65 72  reate an ephemer
1fb5a 69 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20 63  ial table that c
1fb5b 6f 6e 74 61 69 6e 73 0a 2a 2a 20 66 6f 72 20 65  ontains.** for e
1fb5c 61 63 68 20 72 6f 77 20 74 6f 20 62 65 20 63 68  ach row to be ch
1fb5d 61 6e 67 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 28  anged:.**.**   (
1fb5e 41 29 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c  A)  The original
1fb5f 20 72 6f 77 69 64 20 6f 66 20 74 68 61 74 20 72   rowid of that r
1fb60 6f 77 2e 0a 2a 2a 20 20 20 28 42 29 20 20 54 68  ow..**   (B)  Th
1fb61 65 20 72 65 76 69 73 65 64 20 72 6f 77 69 64 20  e revised rowid 
1fb62 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 28 6e 6f  for the row. (no
1fb63 74 65 31 29 0a 2a 2a 20 20 20 28 43 29 20 20 54  te1).**   (C)  T
1fb64 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 65 76  he content of ev
1fb65 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  ery column in th
1fb66 65 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  e row..**.** The
1fb67 6e 20 77 65 20 6c 6f 6f 70 20 6f 76 65 72 20 74  n we loop over t
1fb68 68 69 73 20 65 70 68 65 6d 65 72 61 6c 20 74 61  his ephemeral ta
1fb69 62 6c 65 20 61 6e 64 20 66 6f 72 20 65 61 63 68  ble and for each
1fb6a 20 72 6f 77 20 69 6e 0a 2a 2a 20 74 68 65 20 65   row in.** the e
1fb6b 70 68 65 72 6d 65 72 61 6c 20 74 61 62 6c 65 20  phermeral table 
1fb6c 63 61 6c 6c 20 56 55 70 64 61 74 65 2e 0a 2a 2a  call VUpdate..**
1fb6d 0a 2a 2a 20 57 68 65 6e 20 66 69 6e 69 73 68 65  .** When finishe
1fb6e 64 2c 20 64 72 6f 70 20 74 68 65 20 65 70 68 65  d, drop the ephe
1fb6f 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  meral table..**.
1fb70 2a 2a 20 28 6e 6f 74 65 31 29 20 41 63 74 75 61  ** (note1) Actua
1fb71 6c 6c 79 2c 20 69 66 20 77 65 20 6b 6e 6f 77 20  lly, if we know 
1fb72 69 6e 20 61 64 76 61 6e 63 65 20 74 68 61 74 20  in advance that 
1fb73 28 41 29 20 69 73 20 61 6c 77 61 79 73 20 74 68  (A) is always th
1fb74 65 20 73 61 6d 65 0a 2a 2a 20 61 73 20 28 42 29  e same.** as (B)
1fb75 20 77 65 20 6f 6e 6c 79 20 73 74 6f 72 65 20 28   we only store (
1fb76 41 29 2c 20 74 68 65 6e 20 64 75 70 6c 69 63 61  A), then duplica
1fb77 74 65 20 28 41 29 20 77 68 65 6e 20 70 75 6c 6c  te (A) when pull
1fb78 69 6e 67 0a 2a 2a 20 69 74 20 6f 75 74 20 6f 66  ing.** it out of
1fb79 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74   the ephemeral t
1fb7a 61 62 6c 65 20 62 65 66 6f 72 65 20 63 61 6c 6c  able before call
1fb7b 69 6e 67 20 56 55 70 64 61 74 65 2e 0a 2a 2f 0a  ing VUpdate..*/.
1fb7c 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61  static void upda
1fb7d 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0a  teVirtualTable(.
1fb7e 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1fb7f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
1fb80 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1fb81 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
1fb82 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  ,       /* The v
1fb83 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20  irtual table to 
1fb84 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20  be modified */. 
1fb85 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
1fb86 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
1fb87 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
1fb88 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
1fb89 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75  es,  /* The colu
1fb8a 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e  mns to change in
1fb8b 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74   the UPDATE stat
1fb8c 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  ement */.  Expr 
1fb8d 2a 70 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20  *pRowid,        
1fb8e 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73  /* Expression us
1fb8f 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20  ed to recompute 
1fb90 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  the rowid */.  i
1fb91 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20  nt *aXRef,      
1fb92 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66      /* Mapping f
1fb93 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70  rom columns of p
1fb94 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69  Tab to entries i
1fb95 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20  n pChanges */.  
1fb96 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20  Expr *pWhere    
1fb97 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c       /* WHERE cl
1fb98 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41  ause of the UPDA
1fb99 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  TE statement */.
1fb9a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
1fb9b 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f  Parse->pVdbe;  /
1fb9c 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  * Virtual machin
1fb9d 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
1fb9e 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69  tion */.  ExprLi
1fb9f 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 30 3b 20  st *pEList = 0; 
1fba0 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c      /* The resul
1fba1 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c  t set of the SEL
1fba2 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ECT statement */
1fba3 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
1fba4 63 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  ct = 0;      /* 
1fba5 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
1fba6 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ment */.  Expr *
1fba7 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
1fba8 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79      /* Temporary
1fba9 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
1fbaa 20 69 6e 74 20 65 70 68 65 6d 54 61 62 3b 20 20   int ephemTab;  
1fbab 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
1fbac 62 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ble holding the 
1fbad 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45  result of the SE
1fbae 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  LECT */.  int i;
1fbaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbb0 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1fbb1 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ter */.  int add
1fbb2 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1fbb3 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
1fbb4 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a   top of loop */.
1fbb5 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1fbb6 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44  pParse->db; /* D
1fbb7 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1fbb8 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 43 6f 6e 73  on */..  /* Cons
1fbb9 74 72 75 63 74 20 74 68 65 20 53 45 4c 45 43 54  truct the SELECT
1fbba 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
1fbbb 77 69 6c 6c 20 66 69 6e 64 20 74 68 65 20 6e 65  will find the ne
1fbbc 77 20 76 61 6c 75 65 73 20 66 6f 72 0a 20 20 2a  w values for.  *
1fbbd 2a 20 61 6c 6c 20 75 70 64 61 74 65 64 20 72 6f  * all updated ro
1fbbe 77 73 2e 20 0a 20 20 2a 2f 0a 20 20 70 45 4c 69  ws. .  */.  pELi
1fbbf 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
1fbc0 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
1fbc1 65 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20  e, 0, .         
1fbc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbc3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
1fbc4 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72  reateIdExpr(pPar
1fbc5 73 65 2c 20 22 5f 72 6f 77 69 64 5f 22 29 2c 20  se, "_rowid_"), 
1fbc6 30 29 3b 0a 20 20 69 66 28 20 70 52 6f 77 69 64  0);.  if( pRowid
1fbc7 20 29 7b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d   ){.    pEList =
1fbc8 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
1fbc9 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
1fbca 45 4c 69 73 74 2c 0a 20 20 20 20 20 20 20 20 20  EList,.         
1fbcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbcc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1fbcd 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 52 6f  3ExprDup(db, pRo
1fbce 77 69 64 29 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  wid), 0);.  }.  
1fbcf 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 69 50  assert( pTab->iP
1fbd0 4b 65 79 3c 30 20 29 3b 0a 20 20 66 6f 72 28 69  Key<0 );.  for(i
1fbd1 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
1fbd2 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
1fbd3 61 58 52 65 66 5b 69 5d 3e 3d 30 20 29 7b 0a 20  aXRef[i]>=0 ){. 
1fbd4 20 20 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c       pExpr = sql
1fbd5 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
1fbd6 70 43 68 61 6e 67 65 73 2d 3e 61 5b 61 58 52 65  pChanges->a[aXRe
1fbd7 66 5b 69 5d 5d 2e 70 45 78 70 72 29 3b 0a 20 20  f[i]].pExpr);.  
1fbd8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
1fbd9 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 43 72  Expr = sqlite3Cr
1fbda 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73  eateIdExpr(pPars
1fbdb 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
1fbdc 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  .zName);.    }. 
1fbdd 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69     pEList = sqli
1fbde 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
1fbdf 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  d(pParse, pEList
1fbe0 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d  , pExpr, 0);.  }
1fbe1 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  .  pSelect = sql
1fbe2 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50  ite3SelectNew(pP
1fbe3 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 53  arse, pEList, pS
1fbe4 72 63 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30  rc, pWhere, 0, 0
1fbe5 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
1fbe6 20 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68   .  /* Create th
1fbe7 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
1fbe8 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 68 65  e into which the
1fbe9 20 75 70 64 61 74 65 20 72 65 73 75 6c 74 73 20   update results 
1fbea 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 73 74 6f  will.  ** be sto
1fbeb 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  red..  */.  asse
1fbec 72 74 28 20 76 20 29 3b 0a 20 20 65 70 68 65 6d  rt( v );.  ephem
1fbed 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
1fbee 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56  ab++;.  sqlite3V
1fbef 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f  dbeAddOp(v, OP_O
1fbf0 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 65 70  penEphemeral, ep
1fbf1 68 65 6d 54 61 62 2c 20 70 54 61 62 2d 3e 6e 43  hemTab, pTab->nC
1fbf2 6f 6c 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29  ol+1+(pRowid!=0)
1fbf3 29 3b 0a 0a 20 20 2f 2a 20 66 69 6c 6c 20 74 68  );..  /* fill th
1fbf4 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
1fbf5 65 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  e .  */.  sqlite
1fbf6 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
1fbf7 70 53 65 6c 65 63 74 2c 20 53 52 54 5f 54 61 62  pSelect, SRT_Tab
1fbf8 6c 65 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 2c  le, ephemTab, 0,
1fbf9 20 30 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 2f 2a   0, 0, 0);..  /*
1fbfa 0a 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  .  ** Generate c
1fbfb 6f 64 65 20 74 6f 20 73 63 61 6e 20 74 68 65 20  ode to scan the 
1fbfc 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
1fbfd 61 6e 64 20 63 61 6c 6c 20 56 44 65 6c 65 74 65  and call VDelete
1fbfe 20 61 6e 64 0a 20 20 2a 2a 20 56 49 6e 73 65 72   and.  ** VInser
1fbff 74 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  t.  */.  sqlite3
1fc00 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1fc01 52 65 77 69 6e 64 2c 20 65 70 68 65 6d 54 61 62  Rewind, ephemTab
1fc02 2c 20 30 29 3b 0a 20 20 61 64 64 72 20 3d 20 73  , 0);.  addr = s
1fc03 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
1fc04 74 41 64 64 72 28 76 29 3b 0a 20 20 73 71 6c 69  tAddr(v);.  sqli
1fc05 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1fc06 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 65 70 68 65  OP_Column,  ephe
1fc07 6d 54 61 62 2c 20 30 29 3b 0a 20 20 69 66 28 20  mTab, 0);.  if( 
1fc08 70 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 73 71  pRowid ){.    sq
1fc09 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1fc0a 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68  , OP_Column, eph
1fc0b 65 6d 54 61 62 2c 20 31 29 3b 0a 20 20 7d 65 6c  emTab, 1);.  }el
1fc0c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  se{.    sqlite3V
1fc0d 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44  dbeAddOp(v, OP_D
1fc0e 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20  up, 0, 0);.  }. 
1fc0f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
1fc10 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
1fc11 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fc12 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  Op(v, OP_Column,
1fc13 20 65 70 68 65 6d 54 61 62 2c 20 69 2b 31 2b 28   ephemTab, i+1+(
1fc14 70 52 6f 77 69 64 21 3d 30 29 29 3b 0a 20 20 7d  pRowid!=0));.  }
1fc15 0a 20 20 70 50 61 72 73 65 2d 3e 70 56 69 72 74  .  pParse->pVirt
1fc16 75 61 6c 4c 6f 63 6b 20 3d 20 70 54 61 62 3b 0a  ualLock = pTab;.
1fc17 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33    sqlite3VdbeOp3
1fc18 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20  (v, OP_VUpdate, 
1fc19 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c  0, pTab->nCol+2,
1fc1a 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1fc1b 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68         (const ch
1fc1c 61 72 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 2c  ar*)pTab->pVtab,
1fc1d 20 50 33 5f 56 54 41 42 29 3b 0a 20 20 73 71 6c   P3_VTAB);.  sql
1fc1e 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1fc1f 20 4f 50 5f 4e 65 78 74 2c 20 65 70 68 65 6d 54   OP_Next, ephemT
1fc20 61 62 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c  ab, addr);.  sql
1fc21 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1fc22 28 76 2c 20 61 64 64 72 2d 31 29 3b 0a 20 20 73  (v, addr-1);.  s
1fc23 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1fc24 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 65 70 68  v, OP_Close, eph
1fc25 65 6d 54 61 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a  emTab, 0);..  /*
1fc26 20 43 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 73 71   Cleanup */.  sq
1fc27 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
1fc28 65 28 70 53 65 6c 65 63 74 29 3b 20 20 0a 7d 0a  e(pSelect);  .}.
1fc29 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1fc2a 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1fc2b 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  LE */../********
1fc2c 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 70  ****** End of up
1fc2d 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  date.c *********
1fc2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc30 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1fc31 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1fc32 65 20 76 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a  e vacuum.c *****
1fc33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc35 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1fc36 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20  3 April 6.**.** 
1fc37 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
1fc38 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
1fc39 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
1fc3a 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
1fc3b 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
1fc3c 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
1fc3d 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
1fc3e 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1fc3f 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
1fc40 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1fc41 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1fc42 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
1fc43 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
1fc44 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
1fc45 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
1fc46 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
1fc47 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
1fc48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc4c 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
1fc4d 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
1fc4e 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
1fc4f 65 6d 65 6e 74 20 74 68 65 20 56 41 43 55 55 4d  ement the VACUUM
1fc50 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
1fc51 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65  Most of the code
1fc52 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61   in this file ma
1fc53 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20  y be omitted by 
1fc54 64 65 66 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20  defining the.** 
1fc55 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55  SQLITE_OMIT_VACU
1fc56 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20  UM macro..**.** 
1fc57 24 49 64 3a 20 76 61 63 75 75 6d 2e 63 2c 76 20  $Id: vacuum.c,v 
1fc58 31 2e 37 33 20 32 30 30 37 2f 30 38 2f 32 39 20  1.73 2007/08/29 
1fc59 31 32 3a 33 31 3a 32 38 20 64 61 6e 69 65 6c 6b  12:31:28 danielk
1fc5a 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23  1977 Exp $.*/..#
1fc5b 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1fc5c 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20  TE_OMIT_VACUUM) 
1fc5d 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
1fc5e 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a  TE_OMIT_ATTACH).
1fc5f 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 7a 53  /*.** Execute zS
1fc60 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65 20 64  ql on database d
1fc61 62 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  b. Return an err
1fc62 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  or code..*/.stat
1fc63 69 63 20 69 6e 74 20 65 78 65 63 53 71 6c 28 73  ic int execSql(s
1fc64 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
1fc65 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20  t char *zSql){. 
1fc66 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
1fc67 53 74 6d 74 3b 0a 20 20 69 66 28 20 21 7a 53 71  Stmt;.  if( !zSq
1fc68 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
1fc69 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1fc6a 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  }.  if( SQLITE_O
1fc6b 4b 21 3d 73 71 6c 69 74 65 33 5f 70 72 65 70 61  K!=sqlite3_prepa
1fc6c 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
1fc6d 20 26 70 53 74 6d 74 2c 20 30 29 20 29 7b 0a 20   &pStmt, 0) ){. 
1fc6e 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
1fc6f 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0a 20  3_errcode(db);. 
1fc70 20 7d 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49   }.  while( SQLI
1fc71 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f  TE_ROW==sqlite3_
1fc72 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 7d 0a  step(pStmt) ){}.
1fc73 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1fc74 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
1fc75 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75  ;.}../*.** Execu
1fc76 74 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62  te zSql on datab
1fc77 61 73 65 20 64 62 2e 20 54 68 65 20 73 74 61 74  ase db. The stat
1fc78 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 65 78  ement returns ex
1fc79 61 63 74 6c 79 0a 2a 2a 20 6f 6e 65 20 63 6f 6c  actly.** one col
1fc7a 75 6d 6e 2e 20 45 78 65 63 75 74 65 20 74 68 69  umn. Execute thi
1fc7b 73 20 61 73 20 53 51 4c 20 6f 6e 20 74 68 65 20  s as SQL on the 
1fc7c 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 0a 2a  same database..*
1fc7d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 65  /.static int exe
1fc7e 63 45 78 65 63 53 71 6c 28 73 71 6c 69 74 65 33  cExecSql(sqlite3
1fc7f 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
1fc80 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74   *zSql){.  sqlit
1fc81 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a  e3_stmt *pStmt;.
1fc82 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20    int rc;..  rc 
1fc83 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
1fc84 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20  e(db, zSql, -1, 
1fc85 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66  &pStmt, 0);.  if
1fc86 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1fc87 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
1fc88 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f  while( SQLITE_RO
1fc89 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
1fc8a 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 72 63  pStmt) ){.    rc
1fc8b 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 28   = execSql(db, (
1fc8c 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f  char*)sqlite3_co
1fc8d 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c  lumn_text(pStmt,
1fc8e 20 30 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63   0));.    if( rc
1fc8f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1fc90 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e       sqlite3_fin
1fc91 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20  alize(pStmt);.  
1fc92 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1fc93 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
1fc94 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  rn sqlite3_final
1fc95 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f  ize(pStmt);.}../
1fc96 2a 0a 2a 2a 20 54 68 65 20 6e 6f 6e 2d 73 74 61  *.** The non-sta
1fc97 6e 64 61 72 64 20 56 41 43 55 55 4d 20 63 6f 6d  ndard VACUUM com
1fc98 6d 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20  mand is used to 
1fc99 63 6c 65 61 6e 20 75 70 20 74 68 65 20 64 61 74  clean up the dat
1fc9a 61 62 61 73 65 2c 0a 2a 2a 20 63 6f 6c 6c 61 70  abase,.** collap
1fc9b 73 65 20 66 72 65 65 20 73 70 61 63 65 2c 20 65  se free space, e
1fc9c 74 63 2e 20 20 49 74 20 69 73 20 6d 6f 64 65 6c  tc.  It is model
1fc9d 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 56 41  led after the VA
1fc9e 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20  CUUM command.** 
1fc9f 69 6e 20 50 6f 73 74 67 72 65 53 51 4c 2e 0a 2a  in PostgreSQL..*
1fca0 2a 0a 2a 2a 20 49 6e 20 76 65 72 73 69 6f 6e 20  *.** In version 
1fca1 31 2e 30 2e 78 20 6f 66 20 53 51 4c 69 74 65 2c  1.0.x of SQLite,
1fca2 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d   the VACUUM comm
1fca3 61 6e 64 20 77 6f 75 6c 64 20 63 61 6c 6c 0a 2a  and would call.*
1fca4 2a 20 67 64 62 6d 5f 72 65 6f 72 67 61 6e 69 7a  * gdbm_reorganiz
1fca5 65 28 29 20 6f 6e 20 61 6c 6c 20 74 68 65 20 64  e() on all the d
1fca6 61 74 61 62 61 73 65 20 74 61 62 6c 65 73 2e 20  atabase tables. 
1fca7 20 42 75 74 20 62 65 67 69 6e 6e 69 6e 67 0a 2a   But beginning.*
1fca8 2a 20 77 69 74 68 20 32 2e 30 2e 30 2c 20 53 51  * with 2.0.0, SQ
1fca9 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75  Lite no longer u
1fcaa 73 65 73 20 47 44 42 4d 20 73 6f 20 74 68 69 73  ses GDBM so this
1fcab 20 63 6f 6d 6d 61 6e 64 20 68 61 73 0a 2a 2a 20   command has.** 
1fcac 62 65 63 6f 6d 65 20 61 20 6e 6f 2d 6f 70 2e 0a  become a no-op..
1fcad 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1fcae 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
1fcaf 63 75 75 6d 28 50 61 72 73 65 20 2a 70 50 61 72  cuum(Parse *pPar
1fcb0 73 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  se){.  Vdbe *v =
1fcb1 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
1fcb2 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
1fcb3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1fcb4 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 56  dbeAddOp(v, OP_V
1fcb5 61 63 75 75 6d 2c 20 30 2c 20 30 29 3b 0a 20 20  acuum, 0, 0);.  
1fcb6 7d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  }.  return;.}../
1fcb7 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1fcb8 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
1fcb9 20 4f 50 5f 56 61 63 75 75 6d 20 6f 70 63 6f 64   OP_Vacuum opcod
1fcba 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e 0a 2a  e of the VDBE..*
1fcbb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1fcbc 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 56   int sqlite3RunV
1fcbd 61 63 75 75 6d 28 63 68 61 72 20 2a 2a 70 7a 45  acuum(char **pzE
1fcbe 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 20 2a  rrMsg, sqlite3 *
1fcbf 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  db){.  int rc = 
1fcc0 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f  SQLITE_OK;     /
1fcc1 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72  * Return code fr
1fcc2 6f 6d 20 73 65 72 76 69 63 65 20 72 6f 75 74 69  om service routi
1fcc3 6e 65 73 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  nes */.  Btree *
1fcc4 70 4d 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20  pMain;          
1fcc5 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1fcc6 20 62 65 69 6e 67 20 76 61 63 75 75 6d 65 64 20   being vacuumed 
1fcc7 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 54 65 6d  */.  Btree *pTem
1fcc8 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p;           /* 
1fcc9 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  The temporary da
1fcca 74 61 62 61 73 65 20 77 65 20 76 61 63 75 75 6d  tabase we vacuum
1fccb 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20   into */.  char 
1fccc 2a 7a 53 71 6c 20 3d 20 30 3b 20 20 20 20 20 20  *zSql = 0;      
1fccd 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d     /* SQL statem
1fcce 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61  ents */.  int sa
1fccf 76 65 64 5f 66 6c 61 67 73 3b 20 20 20 20 20 20  ved_flags;      
1fcd0 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65    /* Saved value
1fcd1 20 6f 66 20 74 68 65 20 64 62 2d 3e 66 6c 61 67   of the db->flag
1fcd2 73 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 20 3d  s */.  Db *pDb =
1fcd3 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
1fcd4 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 64 65  * Database to de
1fcd5 74 61 63 68 20 61 74 20 65 6e 64 20 6f 66 20 76  tach at end of v
1fcd6 61 63 75 75 6d 20 2a 2f 0a 0a 20 20 2f 2a 20 53  acuum */..  /* S
1fcd7 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ave the current 
1fcd8 76 61 6c 75 65 20 6f 66 20 74 68 65 20 77 72 69  value of the wri
1fcd9 74 65 2d 73 63 68 65 6d 61 20 66 6c 61 67 20 62  te-schema flag b
1fcda 65 66 6f 72 65 20 73 65 74 74 69 6e 67 20 69 74  efore setting it
1fcdb 2e 20 2a 2f 0a 20 20 73 61 76 65 64 5f 66 6c 61  . */.  saved_fla
1fcdc 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a  gs = db->flags;.
1fcdd 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
1fcde 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d  QLITE_WriteSchem
1fcdf 61 20 7c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72  a | SQLITE_Ignor
1fce0 65 43 68 65 63 6b 73 3b 0a 0a 20 20 69 66 28 20  eChecks;..  if( 
1fce1 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20  !db->autoCommit 
1fce2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
1fce3 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67  tString(pzErrMsg
1fce4 2c 20 22 63 61 6e 6e 6f 74 20 56 41 43 55 55 4d  , "cannot VACUUM
1fce5 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74   from within a t
1fce6 72 61 6e 73 61 63 74 69 6f 6e 22 2c 20 0a 20 20  ransaction", .  
1fce7 20 20 20 20 20 28 63 68 61 72 2a 29 30 29 3b 0a       (char*)0);.
1fce8 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1fce9 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20  ERROR;.    goto 
1fcea 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
1fceb 20 7d 0a 20 20 70 4d 61 69 6e 20 3d 20 64 62 2d   }.  pMain = db-
1fcec 3e 61 44 62 5b 30 5d 2e 70 42 74 3b 0a 0a 20 20  >aDb[0].pBt;..  
1fced 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20 74 65  /* Attach the te
1fcee 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
1fcef 20 61 73 20 27 76 61 63 75 75 6d 5f 64 62 27 2e   as 'vacuum_db'.
1fcf0 20 54 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73   The synchronous
1fcf1 20 70 72 61 67 6d 61 0a 20 20 2a 2a 20 63 61 6e   pragma.  ** can
1fcf2 20 62 65 20 73 65 74 20 74 6f 20 27 6f 66 66 27   be set to 'off'
1fcf3 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 2c 20   for this file, 
1fcf4 61 73 20 69 74 20 69 73 20 6e 6f 74 20 72 65 63  as it is not rec
1fcf5 6f 76 65 72 65 64 20 69 66 20 61 20 63 72 61 73  overed if a cras
1fcf6 68 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 6e  h.  ** occurs an
1fcf7 79 77 61 79 2e 20 54 68 65 20 69 6e 74 65 67 72  yway. The integr
1fcf8 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 62  ity of the datab
1fcf9 61 73 65 20 69 73 20 6d 61 69 6e 74 61 69 6e 65  ase is maintaine
1fcfa 64 20 62 79 20 61 0a 20 20 2a 2a 20 28 70 6f 73  d by a.  ** (pos
1fcfb 73 69 62 6c 79 20 73 79 6e 63 68 72 6f 6e 6f 75  sibly synchronou
1fcfc 73 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  s) transaction o
1fcfd 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 6d 61 69  pened on the mai
1fcfe 6e 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72  n database befor
1fcff 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74  e.  ** sqlite3Bt
1fd00 72 65 65 43 6f 70 79 46 69 6c 65 28 29 20 69 73  reeCopyFile() is
1fd01 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2a 0a 20 20   called..  **.  
1fd02 2a 2a 20 41 6e 20 6f 70 74 69 6d 69 73 61 74 69  ** An optimisati
1fd03 6f 6e 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 75  on would be to u
1fd04 73 65 20 61 20 6e 6f 6e 2d 6a 6f 75 72 6e 61 6c  se a non-journal
1fd05 65 64 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a 20  ed pager..  */. 
1fd06 20 7a 53 71 6c 20 3d 20 22 41 54 54 41 43 48 20   zSql = "ATTACH 
1fd07 27 27 20 41 53 20 76 61 63 75 75 6d 5f 64 62 3b  '' AS vacuum_db;
1fd08 22 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71  ";.  rc = execSq
1fd09 6c 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 69  l(db, zSql);.  i
1fd0a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1fd0b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
1fd0c 61 63 75 75 6d 3b 0a 20 20 70 44 62 20 3d 20 26  acuum;.  pDb = &
1fd0d 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d  db->aDb[db->nDb-
1fd0e 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74  1];.  assert( st
1fd0f 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b 64 62 2d  rcmp(db->aDb[db-
1fd10 3e 6e 44 62 2d 31 5d 2e 7a 4e 61 6d 65 2c 22 76  >nDb-1].zName,"v
1fd11 61 63 75 75 6d 5f 64 62 22 29 3d 3d 30 20 29 3b  acuum_db")==0 );
1fd12 0a 20 20 70 54 65 6d 70 20 3d 20 64 62 2d 3e 61  .  pTemp = db->a
1fd13 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 2e 70 42  Db[db->nDb-1].pB
1fd14 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  t;.  sqlite3Btre
1fd15 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 54 65  eSetPageSize(pTe
1fd16 6d 70 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65  mp, sqlite3Btree
1fd17 47 65 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69  GetPageSize(pMai
1fd18 6e 29 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  n),.     sqlite3
1fd19 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28  BtreeGetReserve(
1fd1a 70 4d 61 69 6e 29 29 3b 0a 20 20 69 66 28 20 64  pMain));.  if( d
1fd1b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1fd1c 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
1fd1d 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
1fd1e 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
1fd1f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1fd20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
1fd21 61 67 65 53 69 7a 65 28 70 54 65 6d 70 29 3d 3d  ageSize(pTemp)==
1fd22 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
1fd23 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 29 20 29  ageSize(pMain) )
1fd24 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c  ;.  rc = execSql
1fd25 28 64 62 2c 20 22 50 52 41 47 4d 41 20 76 61 63  (db, "PRAGMA vac
1fd26 75 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e 6f  uum_db.synchrono
1fd27 75 73 3d 4f 46 46 22 29 3b 0a 20 20 69 66 28 20  us=OFF");.  if( 
1fd28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1fd29 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  .    goto end_of
1fd2a 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23 69  _vacuum;.  }..#i
1fd2b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1fd2c 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 73  T_AUTOVACUUM.  s
1fd2d 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
1fd2e 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 2c 20  toVacuum(pTemp, 
1fd2f 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41  sqlite3BtreeGetA
1fd30 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e 29  utoVacuum(pMain)
1fd31 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
1fd32 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74  Begin a transact
1fd33 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78  ion */.  rc = ex
1fd34 65 63 53 71 6c 28 64 62 2c 20 22 42 45 47 49 4e  ecSql(db, "BEGIN
1fd35 20 45 58 43 4c 55 53 49 56 45 3b 22 29 3b 0a 20   EXCLUSIVE;");. 
1fd36 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1fd37 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
1fd38 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 51  _vacuum;..  /* Q
1fd39 75 65 72 79 20 74 68 65 20 73 63 68 65 6d 61 20  uery the schema 
1fd3a 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  of the main data
1fd3b 62 61 73 65 2e 20 43 72 65 61 74 65 20 61 20 6d  base. Create a m
1fd3c 69 72 72 6f 72 20 73 63 68 65 6d 61 0a 20 20 2a  irror schema.  *
1fd3d 2a 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61  * in the tempora
1fd3e 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  ry database..  *
1fd3f 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  /.  rc = execExe
1fd40 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
1fd41 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20  "SELECT 'CREATE 
1fd42 54 41 42 4c 45 20 76 61 63 75 75 6d 5f 64 62 2e  TABLE vacuum_db.
1fd43 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  ' || substr(sql,
1fd44 31 34 2c 31 30 30 30 30 30 30 30 30 29 20 22 0a  14,100000000) ".
1fd45 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71        "  FROM sq
1fd46 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52  lite_master WHER
1fd47 45 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41  E type='table' A
1fd48 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65  ND name!='sqlite
1fd49 5f 73 65 71 75 65 6e 63 65 27 22 0a 20 20 20 20  _sequence'".    
1fd4a 20 20 22 20 20 20 41 4e 44 20 72 6f 6f 74 70 61    "   AND rootpa
1fd4b 67 65 3e 30 22 0a 20 20 29 3b 0a 20 20 69 66 28  ge>0".  );.  if(
1fd4c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1fd4d 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
1fd4e 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63  uum;.  rc = exec
1fd4f 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20  ExecSql(db, .   
1fd50 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45 41     "SELECT 'CREA
1fd51 54 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d 5f  TE INDEX vacuum_
1fd52 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73  db.' || substr(s
1fd53 71 6c 2c 31 34 2c 31 30 30 30 30 30 30 30 30 29  ql,14,100000000)
1fd54 22 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20  ".      "  FROM 
1fd55 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
1fd56 45 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52  ERE sql LIKE 'CR
1fd57 45 41 54 45 20 49 4e 44 45 58 20 25 27 20 22 29  EATE INDEX %' ")
1fd58 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1fd59 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
1fd5a 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63  _of_vacuum;.  rc
1fd5b 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64   = execExecSql(d
1fd5c 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43  b, .      "SELEC
1fd5d 54 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45  T 'CREATE UNIQUE
1fd5e 20 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62   INDEX vacuum_db
1fd5f 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c  .' || substr(sql
1fd60 2c 32 31 2c 31 30 30 30 30 30 30 30 30 29 20 22  ,21,100000000) "
1fd61 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73  .      "  FROM s
1fd62 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
1fd63 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45  RE sql LIKE 'CRE
1fd64 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58  ATE UNIQUE INDEX
1fd65 20 25 27 22 29 3b 0a 20 20 69 66 28 20 72 63 21   %'");.  if( rc!
1fd66 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
1fd67 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
1fd68 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f  ..  /* Loop thro
1fd69 75 67 68 20 74 68 65 20 74 61 62 6c 65 73 20 69  ugh the tables i
1fd6a 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
1fd6b 61 73 65 2e 20 46 6f 72 20 65 61 63 68 2c 20 64  ase. For each, d
1fd6c 6f 0a 20 20 2a 2a 20 61 6e 20 22 49 4e 53 45 52  o.  ** an "INSER
1fd6d 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62  T INTO vacuum_db
1fd6e 2e 78 78 78 20 53 45 4c 45 43 54 20 2a 20 46 52  .xxx SELECT * FR
1fd6f 4f 4d 20 78 78 78 3b 22 20 74 6f 20 63 6f 70 79  OM xxx;" to copy
1fd70 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  .  ** the conten
1fd71 74 73 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72  ts to the tempor
1fd72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ary database..  
1fd73 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  */.  rc = execEx
1fd74 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20  ecSql(db, .     
1fd75 20 22 53 45 4c 45 43 54 20 27 49 4e 53 45 52 54   "SELECT 'INSERT
1fd76 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e   INTO vacuum_db.
1fd77 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
1fd78 20 22 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53   ".      "|| ' S
1fd79 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 27 20 7c  ELECT * FROM ' |
1fd7a 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c  | quote(name) ||
1fd7b 20 27 3b 27 22 0a 20 20 20 20 20 20 22 46 52 4f   ';'".      "FRO
1fd7c 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  M sqlite_master 
1fd7d 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74  ".      "WHERE t
1fd7e 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e  ype = 'table' AN
1fd7f 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f  D name!='sqlite_
1fd80 73 65 71 75 65 6e 63 65 27 20 22 0a 20 20 20 20  sequence' ".    
1fd81 20 20 22 20 20 41 4e 44 20 72 6f 6f 74 70 61 67    "  AND rootpag
1fd82 65 3e 30 22 0a 0a 20 20 29 3b 0a 20 20 69 66 28  e>0"..  );.  if(
1fd83 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1fd84 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
1fd85 75 75 6d 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20  uum;..  /* Copy 
1fd86 6f 76 65 72 20 74 68 65 20 73 65 71 75 65 6e 63  over the sequenc
1fd87 65 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 72  e table.  */.  r
1fd88 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28  c = execExecSql(
1fd89 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45  db, .      "SELE
1fd8a 43 54 20 27 44 45 4c 45 54 45 20 46 52 4f 4d 20  CT 'DELETE FROM 
1fd8b 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71  vacuum_db.' || q
1fd8c 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b  uote(name) || ';
1fd8d 27 20 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20  ' ".      "FROM 
1fd8e 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65  vacuum_db.sqlite
1fd8f 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e 61  _master WHERE na
1fd90 6d 65 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65  me='sqlite_seque
1fd91 6e 63 65 27 20 22 0a 20 20 29 3b 0a 20 20 69 66  nce' ".  );.  if
1fd92 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1fd93 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
1fd94 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65  cuum;.  rc = exe
1fd95 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
1fd96 20 20 20 20 22 53 45 4c 45 43 54 20 27 49 4e 53      "SELECT 'INS
1fd97 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f  ERT INTO vacuum_
1fd98 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61  db.' || quote(na
1fd99 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c 7c 20  me) ".      "|| 
1fd9a 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  ' SELECT * FROM 
1fd9b 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
1fd9c 20 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20   || ';' ".      
1fd9d 22 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e  "FROM vacuum_db.
1fd9e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
1fd9f 45 52 45 20 6e 61 6d 65 3d 3d 27 73 71 6c 69 74  ERE name=='sqlit
1fda0 65 5f 73 65 71 75 65 6e 63 65 27 3b 22 0a 20 20  e_sequence';".  
1fda1 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1fda2 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
1fda3 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 0a 20  d_of_vacuum;... 
1fda4 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 74 72 69   /* Copy the tri
1fda5 67 67 65 72 73 2c 20 76 69 65 77 73 2c 20 61 6e  ggers, views, an
1fda6 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  d virtual tables
1fda7 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64   from the main d
1fda8 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 6f 76 65  atabase.  ** ove
1fda9 72 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61  r to the tempora
1fdaa 72 79 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f  ry database.  No
1fdab 6e 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65  ne of these obje
1fdac 63 74 73 20 68 61 73 20 61 6e 79 0a 20 20 2a 2a  cts has any.  **
1fdad 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 6f 72   associated stor
1fdae 61 67 65 2c 20 73 6f 20 61 6c 6c 20 77 65 20 68  age, so all we h
1fdaf 61 76 65 20 74 6f 20 64 6f 20 69 73 20 63 6f 70  ave to do is cop
1fdb0 79 20 74 68 65 69 72 20 65 6e 74 72 69 65 73 0a  y their entries.
1fdb1 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 51    ** from the SQ
1fdb2 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
1fdb3 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65  e..  */.  rc = e
1fdb4 78 65 63 53 71 6c 28 64 62 2c 0a 20 20 20 20 20  xecSql(db,.     
1fdb5 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61   "INSERT INTO va
1fdb6 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d  cuum_db.sqlite_m
1fdb7 61 73 74 65 72 20 22 0a 20 20 20 20 20 20 22 20  aster ".      " 
1fdb8 20 53 45 4c 45 43 54 20 74 79 70 65 2c 20 6e 61   SELECT type, na
1fdb9 6d 65 2c 20 74 62 6c 5f 6e 61 6d 65 2c 20 72 6f  me, tbl_name, ro
1fdba 6f 74 70 61 67 65 2c 20 73 71 6c 22 0a 20 20 20  otpage, sql".   
1fdbb 20 20 20 22 20 20 20 20 46 52 4f 4d 20 73 71 6c     "    FROM sql
1fdbc 69 74 65 5f 6d 61 73 74 65 72 22 0a 20 20 20 20  ite_master".    
1fdbd 20 20 22 20 20 20 57 48 45 52 45 20 74 79 70 65    "   WHERE type
1fdbe 3d 27 76 69 65 77 27 20 4f 52 20 74 79 70 65 3d  ='view' OR type=
1fdbf 27 74 72 69 67 67 65 72 27 22 0a 20 20 20 20 20  'trigger'".     
1fdc0 20 22 20 20 20 20 20 20 4f 52 20 28 74 79 70 65   "      OR (type
1fdc1 3d 27 74 61 62 6c 65 27 20 41 4e 44 20 72 6f 6f  ='table' AND roo
1fdc2 74 70 61 67 65 3d 30 29 22 0a 20 20 29 3b 0a 20  tpage=0)".  );. 
1fdc3 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65   if( rc ) goto e
1fdc4 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20  nd_of_vacuum;.. 
1fdc5 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
1fdc6 74 2c 20 75 6e 6c 65 73 73 20 74 68 65 20 6d 61  t, unless the ma
1fdc7 69 6e 20 64 62 20 77 61 73 20 63 6f 6d 70 6c 65  in db was comple
1fdc8 74 65 6c 79 20 65 6d 70 74 79 2c 20 74 68 65 72  tely empty, ther
1fdc9 65 20 69 73 20 6e 6f 77 20 61 0a 20 20 2a 2a 20  e is now a.  ** 
1fdca 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
1fdcb 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 20 64   on the vacuum d
1fdcc 61 74 61 62 61 73 65 2c 20 62 75 74 20 6e 6f 74  atabase, but not
1fdcd 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   on the main dat
1fdce 61 62 61 73 65 2e 0a 20 20 2a 2a 20 4f 70 65 6e  abase..  ** Open
1fdcf 20 61 20 62 74 72 65 65 20 6c 65 76 65 6c 20 74   a btree level t
1fdd0 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
1fdd1 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
1fdd2 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 61 0a 20   This allows a. 
1fdd3 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69   ** call to sqli
1fdd4 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
1fdd5 28 29 2e 20 54 68 65 20 6d 61 69 6e 20 64 61 74  (). The main dat
1fdd6 61 62 61 73 65 20 62 74 72 65 65 20 6c 65 76 65  abase btree leve
1fdd7 6c 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  l.  ** transacti
1fdd8 6f 6e 20 69 73 20 74 68 65 6e 20 63 6f 6d 6d 69  on is then commi
1fdd9 74 74 65 64 2c 20 73 6f 20 74 68 65 20 53 51 4c  tted, so the SQL
1fdda 20 6c 65 76 65 6c 20 6e 65 76 65 72 20 6b 6e 6f   level never kno
1fddb 77 73 20 69 74 20 77 61 73 0a 20 20 2a 2a 20 6f  ws it was.  ** o
1fddc 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e  pened for writin
1fddd 67 2e 20 54 68 69 73 20 77 61 79 2c 20 74 68 65  g. This way, the
1fdde 20 53 51 4c 20 74 72 61 6e 73 61 63 74 69 6f 6e   SQL transaction
1fddf 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
1fde0 74 68 65 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61  the.  ** tempora
1fde1 72 79 20 64 61 74 61 62 61 73 65 20 6e 65 76 65  ry database neve
1fde2 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 63 6f  r needs to be co
1fde3 6d 6d 69 74 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  mmitted..  */.  
1fde4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1fde5 4b 20 29 7b 0a 20 20 20 20 75 33 32 20 6d 65 74  K ){.    u32 met
1fde6 61 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20  a;.    int i;.. 
1fde7 20 20 20 2f 2a 20 54 68 69 73 20 61 72 72 61 79     /* This array
1fde8 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63   determines whic
1fde9 68 20 6d 65 74 61 20 6d 65 74 61 20 76 61 6c 75  h meta meta valu
1fdea 65 73 20 61 72 65 20 70 72 65 73 65 72 76 65 64  es are preserved
1fdeb 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 76   in the.    ** v
1fdec 61 63 75 75 6d 2e 20 20 45 76 65 6e 20 65 6e 74  acuum.  Even ent
1fded 72 69 65 73 20 61 72 65 20 74 68 65 20 6d 65 74  ries are the met
1fdee 61 20 76 61 6c 75 65 20 6e 75 6d 62 65 72 20 61  a value number a
1fdef 6e 64 20 6f 64 64 20 65 6e 74 72 69 65 73 0a 20  nd odd entries. 
1fdf0 20 20 20 2a 2a 20 61 72 65 20 61 6e 20 69 6e 63     ** are an inc
1fdf1 72 65 6d 65 6e 74 20 74 6f 20 61 70 70 6c 79 20  rement to apply 
1fdf2 74 6f 20 74 68 65 20 6d 65 74 61 20 76 61 6c 75  to the meta valu
1fdf3 65 20 61 66 74 65 72 20 74 68 65 20 76 61 63 75  e after the vacu
1fdf4 75 6d 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 69  um..    ** The i
1fdf5 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 73 65 64  ncrement is used
1fdf6 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65   to increase the
1fdf7 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 73   schema cookie s
1fdf8 6f 20 74 68 61 74 20 6f 74 68 65 72 0a 20 20 20  o that other.   
1fdf9 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20   ** connections 
1fdfa 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  to the same data
1fdfb 62 61 73 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 74  base will know t
1fdfc 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68  o reread the sch
1fdfd 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ema..    */.    
1fdfe 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
1fdff 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 70 79  igned char aCopy
1fe00 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 31 2c  [] = {.       1,
1fe01 20 31 2c 20 20 20 20 2f 2a 20 41 64 64 20 6f 6e   1,    /* Add on
1fe02 65 20 74 6f 20 74 68 65 20 6f 6c 64 20 73 63 68  e to the old sch
1fe03 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20  ema cookie */.  
1fe04 20 20 20 20 20 33 2c 20 30 2c 20 20 20 20 2f 2a       3, 0,    /*
1fe05 20 50 72 65 73 65 72 76 65 20 74 68 65 20 64 65   Preserve the de
1fe06 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
1fe07 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 20   size */.       
1fe08 35 2c 20 30 2c 20 20 20 20 2f 2a 20 50 72 65 73  5, 0,    /* Pres
1fe09 65 72 76 65 20 74 68 65 20 64 65 66 61 75 6c 74  erve the default
1fe0a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
1fe0b 2f 0a 20 20 20 20 20 20 20 36 2c 20 30 2c 20 20  /.       6, 0,  
1fe0c 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74 68    /* Preserve th
1fe0d 65 20 75 73 65 72 20 76 65 72 73 69 6f 6e 20 2a  e user version *
1fe0e 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 61 73  /.    };..    as
1fe0f 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33  sert( 1==sqlite3
1fe10 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70  BtreeIsInTrans(p
1fe11 54 65 6d 70 29 20 29 3b 0a 20 20 20 20 61 73 73  Temp) );.    ass
1fe12 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42  ert( 1==sqlite3B
1fe13 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 4d  treeIsInTrans(pM
1fe14 61 69 6e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  ain) );..    /* 
1fe15 43 6f 70 79 20 42 74 72 65 65 20 6d 65 74 61 20  Copy Btree meta 
1fe16 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 66 6f  values */.    fo
1fe17 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28  r(i=0; i<sizeof(
1fe18 61 43 6f 70 79 29 2f 73 69 7a 65 6f 66 28 61 43  aCopy)/sizeof(aC
1fe19 6f 70 79 5b 30 5d 29 3b 20 69 2b 3d 32 29 7b 0a  opy[0]); i+=2){.
1fe1a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1fe1b 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
1fe1c 4d 61 69 6e 2c 20 61 43 6f 70 79 5b 69 5d 2c 20  Main, aCopy[i], 
1fe1d 26 6d 65 74 61 29 3b 0a 20 20 20 20 20 20 69 66  &meta);.      if
1fe1e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1fe1f 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
1fe20 63 75 75 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d  cuum;.      rc =
1fe21 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
1fe22 61 74 65 4d 65 74 61 28 70 54 65 6d 70 2c 20 61  ateMeta(pTemp, a
1fe23 43 6f 70 79 5b 69 5d 2c 20 6d 65 74 61 2b 61 43  Copy[i], meta+aC
1fe24 6f 70 79 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 20  opy[i+1]);.     
1fe25 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1fe26 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
1fe27 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a  _vacuum;.    }..
1fe28 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1fe29 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 70 4d  BtreeCopyFile(pM
1fe2a 61 69 6e 2c 20 70 54 65 6d 70 29 3b 0a 20 20 20  ain, pTemp);.   
1fe2b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1fe2c 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
1fe2d 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20  _vacuum;.    rc 
1fe2e 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
1fe2f 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a 20 20 20  mmit(pTemp);.   
1fe30 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1fe31 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
1fe32 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20  _vacuum;.    rc 
1fe33 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
1fe34 6d 6d 69 74 28 70 4d 61 69 6e 29 3b 0a 20 20 7d  mmit(pMain);.  }
1fe35 0a 0a 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3a  ..end_of_vacuum:
1fe36 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 74 68  .  /* Restore th
1fe37 65 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65  e original value
1fe38 20 6f 66 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f   of db->flags */
1fe39 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 73  .  db->flags = s
1fe3a 61 76 65 64 5f 66 6c 61 67 73 3b 0a 0a 20 20 2f  aved_flags;..  /
1fe3b 2a 20 43 75 72 72 65 6e 74 6c 79 20 74 68 65 72  * Currently ther
1fe3c 65 20 69 73 20 61 6e 20 53 51 4c 20 6c 65 76 65  e is an SQL leve
1fe3d 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  l transaction op
1fe3e 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d  en on the vacuum
1fe3f 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  .  ** database. 
1fe40 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c  No locks are hel
1fe41 64 20 6f 6e 20 61 6e 79 20 6f 74 68 65 72 20 66  d on any other f
1fe42 69 6c 65 73 20 28 73 69 6e 63 65 20 74 68 65 20  iles (since the 
1fe43 6d 61 69 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77  main file.  ** w
1fe44 61 73 20 63 6f 6d 6d 69 74 74 65 64 20 61 74 20  as committed at 
1fe45 74 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c 29  the btree level)
1fe46 2e 20 53 6f 20 69 74 20 73 61 66 65 20 74 6f 20  . So it safe to 
1fe47 65 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74  end the transact
1fe48 69 6f 6e 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75  ion.  ** by manu
1fe49 61 6c 6c 79 20 73 65 74 74 69 6e 67 20 74 68 65  ally setting the
1fe4a 20 61 75 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67   autoCommit flag
1fe4b 20 74 6f 20 74 72 75 65 20 61 6e 64 20 64 65 74   to true and det
1fe4c 61 63 68 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20  aching the.  ** 
1fe4d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e  vacuum database.
1fe4e 20 54 68 65 20 76 61 63 75 75 6d 5f 64 62 20 6a   The vacuum_db j
1fe4f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64  ournal file is d
1fe50 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20  eleted when the 
1fe51 70 61 67 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c  pager.  ** is cl
1fe52 6f 73 65 64 20 62 79 20 74 68 65 20 44 45 54 41  osed by the DETA
1fe53 43 48 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61  CH..  */.  db->a
1fe54 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a  utoCommit = 1;..
1fe55 20 20 69 66 28 20 70 44 62 20 29 7b 0a 20 20 20    if( pDb ){.   
1fe56 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
1fe57 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  se(pDb->pBt);.  
1fe58 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a    pDb->pBt = 0;.
1fe59 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
1fe5a 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c   = 0;.  }..  sql
1fe5b 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
1fe5c 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
1fe5d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1fe5e 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
1fe5f 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 26 26  E_OMIT_VACUUM &&
1fe60 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54   SQLITE_OMIT_ATT
1fe61 41 43 48 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ACH */../*******
1fe62 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
1fe63 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  acuum.c ********
1fe64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe66 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1fe67 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1fe68 6c 65 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a  le vtab.c ******
1fe69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe6b 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1fe6c 30 36 20 4a 75 6e 65 20 31 30 0a 2a 2a 0a 2a 2a  06 June 10.**.**
1fe6d 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1fe6e 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1fe6f 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1fe70 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1fe71 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1fe72 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1fe73 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1fe74 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1fe75 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1fe76 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1fe77 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1fe78 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1fe79 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1fe7a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1fe7b 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1fe7c 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1fe7d 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1fe7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe82 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
1fe83 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1fe84 63 6f 64 65 20 75 73 65 64 20 74 6f 20 68 65 6c  code used to hel
1fe85 70 20 69 6d 70 6c 65 6d 65 6e 74 20 76 69 72 74  p implement virt
1fe86 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a  ual tables..**.*
1fe87 2a 20 24 49 64 3a 20 76 74 61 62 2e 63 2c 76 20  * $Id: vtab.c,v 
1fe88 31 2e 35 37 20 32 30 30 37 2f 30 39 2f 30 34 20  1.57 2007/09/04 
1fe89 31 35 3a 33 38 3a 35 38 20 64 61 6e 69 65 6c 6b  15:38:58 danielk
1fe8a 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69  1977 Exp $.*/.#i
1fe8b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1fe8c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 0a  T_VIRTUALTABLE..
1fe8d 73 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74  static int creat
1fe8e 65 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74  eModule(.  sqlit
1fe8f 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
1fe90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
1fe91 74 61 62 61 73 65 20 69 6e 20 77 68 69 63 68 20  tabase in which 
1fe92 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73 74  module is regist
1fe93 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ered */.  const 
1fe94 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
1fe95 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
1fe96 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  e assigned to th
1fe97 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63  is module */.  c
1fe98 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1fe99 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f  ule *pModule,  /
1fe9a 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  * The definition
1fe9b 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a   of the module *
1fe9c 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20  /.  void *pAux, 
1fe9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fe9e 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70      /* Context p
1fe9f 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61  ointer for xCrea
1fea0 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20  te/xConnect */. 
1fea1 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
1fea2 29 28 76 6f 69 64 20 2a 29 20 20 20 20 20 20 20  )(void *)       
1fea3 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72   /* Module destr
1fea4 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a  uctor function *
1fea5 2f 0a 29 20 7b 0a 20 20 69 6e 74 20 72 63 2c 20  /.) {.  int rc, 
1fea6 6e 4e 61 6d 65 3b 0a 20 20 4d 6f 64 75 6c 65 20  nName;.  Module 
1fea7 2a 70 4d 6f 64 3b 0a 0a 20 20 73 71 6c 69 74 65  *pMod;..  sqlite
1fea8 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
1fea9 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 6e 4e 61 6d  ->mutex);.  nNam
1feaa 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65  e = strlen(zName
1feab 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64  );.  pMod = (Mod
1feac 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  ule *)sqlite3DbM
1fead 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
1feae 65 6f 66 28 4d 6f 64 75 6c 65 29 20 2b 20 6e 4e  eof(Module) + nN
1feaf 61 6d 65 20 2b 20 31 29 3b 0a 20 20 69 66 28 20  ame + 1);.  if( 
1feb0 70 4d 6f 64 20 29 7b 0a 20 20 20 20 63 68 61 72  pMod ){.    char
1feb1 20 2a 7a 43 6f 70 79 20 3d 20 28 63 68 61 72 20   *zCopy = (char 
1feb2 2a 29 28 26 70 4d 6f 64 5b 31 5d 29 3b 0a 20 20  *)(&pMod[1]);.  
1feb3 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79 2c 20    memcpy(zCopy, 
1feb4 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b  zName, nName+1);
1feb5 0a 20 20 20 20 70 4d 6f 64 2d 3e 7a 4e 61 6d 65  .    pMod->zName
1feb6 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20 70 4d   = zCopy;.    pM
1feb7 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d  od->pModule = pM
1feb8 6f 64 75 6c 65 3b 0a 20 20 20 20 70 4d 6f 64 2d  odule;.    pMod-
1feb9 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 20  >pAux = pAux;.  
1feba 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79    pMod->xDestroy
1febb 20 3d 20 78 44 65 73 74 72 6f 79 3b 0a 20 20 20   = xDestroy;.   
1febc 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 20   pMod = (Module 
1febd 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  *)sqlite3HashIns
1febe 65 72 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  ert(&db->aModule
1febf 2c 20 7a 43 6f 70 79 2c 20 6e 4e 61 6d 65 2c 20  , zCopy, nName, 
1fec0 28 76 6f 69 64 2a 29 70 4d 6f 64 29 3b 0a 20 20  (void*)pMod);.  
1fec1 20 20 69 66 28 20 70 4d 6f 64 20 26 26 20 70 4d    if( pMod && pM
1fec2 6f 64 2d 3e 78 44 65 73 74 72 6f 79 20 29 7b 0a  od->xDestroy ){.
1fec3 20 20 20 20 20 20 70 4d 6f 64 2d 3e 78 44 65 73        pMod->xDes
1fec4 74 72 6f 79 28 70 4d 6f 64 2d 3e 70 41 75 78 29  troy(pMod->pAux)
1fec5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1fec6 74 65 33 5f 66 72 65 65 28 70 4d 6f 64 29 3b 0a  te3_free(pMod);.
1fec7 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
1fec8 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
1fec9 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 63 20  b, 0);.  }.  rc 
1feca 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
1fecb 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b  (db, SQLITE_OK);
1fecc 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1fecd 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
1fece 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1fecf 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e  }.../*.** Extern
1fed0 61 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20  al API function 
1fed1 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  used to create a
1fed2 20 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62   new virtual-tab
1fed3 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51  le module..*/.SQ
1fed4 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1fed5 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
1fed6 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  le(.  sqlite3 *d
1fed7 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
1fed8 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
1fed9 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c  e in which modul
1feda 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20  e is registered 
1fedb 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1fedc 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
1fedd 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73       /* Name ass
1fede 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f  igned to this mo
1fedf 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  dule */.  const 
1fee0 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
1fee1 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65  pModule,  /* The
1fee2 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74   definition of t
1fee3 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76  he module */.  v
1fee4 6f 69 64 20 2a 70 41 75 78 20 20 20 20 20 20 20  oid *pAux       
1fee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fee6 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  * Context pointe
1fee7 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43  r for xCreate/xC
1fee8 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 72  onnect */.){.  r
1fee9 65 74 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75  eturn createModu
1feea 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d  le(db, zName, pM
1feeb 6f 64 75 6c 65 2c 20 70 41 75 78 2c 20 30 29 3b  odule, pAux, 0);
1feec 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e  .}../*.** Extern
1feed 61 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20  al API function 
1feee 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  used to create a
1feef 20 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62   new virtual-tab
1fef0 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51  le module..*/.SQ
1fef1 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1fef2 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
1fef3 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  le_v2(.  sqlite3
1fef4 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
1fef5 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1fef6 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f  base in which mo
1fef7 64 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72  dule is register
1fef8 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
1fef9 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
1fefa 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
1fefb 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73  assigned to this
1fefc 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
1fefd 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
1fefe 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20  e *pModule,  /* 
1feff 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  The definition o
1ff00 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  f the module */.
1ff01 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20    void *pAux,   
1ff02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff03 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69    /* Context poi
1ff04 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61 74 65  nter for xCreate
1ff05 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76  /xConnect */.  v
1ff06 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28  oid (*xDestroy)(
1ff07 76 6f 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f  void *)        /
1ff08 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63  * Module destruc
1ff09 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  tor function */.
1ff0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61  ){.  return crea
1ff0b 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61  teModule(db, zNa
1ff0c 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75  me, pModule, pAu
1ff0d 78 2c 20 78 44 65 73 74 72 6f 79 29 3b 0a 7d 0a  x, xDestroy);.}.
1ff0e 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20  ./*.** Lock the 
1ff0f 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 6f  virtual table so
1ff10 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20   that it cannot 
1ff11 62 65 20 64 69 73 63 6f 6e 6e 65 63 74 65 64 2e  be disconnected.
1ff12 0a 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73 74 2e 20  .** Locks nest. 
1ff13 20 45 76 65 72 79 20 6c 6f 63 6b 20 73 68 6f 75   Every lock shou
1ff14 6c 64 20 68 61 76 65 20 61 20 63 6f 72 72 65 73  ld have a corres
1ff15 70 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63 6b 2e 0a  ponding unlock..
1ff16 2a 2a 20 49 66 20 61 6e 20 75 6e 6c 6f 63 6b 20  ** If an unlock 
1ff17 69 73 20 6f 6d 69 74 74 65 64 2c 20 72 65 73 6f  is omitted, reso
1ff18 75 72 63 65 73 20 6c 65 61 6b 73 20 77 69 6c 6c  urces leaks will
1ff19 20 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a 2a 2a 20   occur.  .**.** 
1ff1a 49 66 20 61 20 64 69 73 63 6f 6e 6e 65 63 74 20  If a disconnect 
1ff1b 69 73 20 61 74 74 65 6d 70 74 65 64 20 77 68 69  is attempted whi
1ff1c 6c 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  le a virtual tab
1ff1d 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2c 0a 2a 2a  le is locked,.**
1ff1e 20 74 68 65 20 64 69 73 63 6f 6e 6e 65 63 74 20   the disconnect 
1ff1f 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69  is deferred unti
1ff20 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 68 61 76 65  l all locks have
1ff21 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 2a   been removed..*
1ff22 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ff23 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
1ff24 62 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 76 74  bLock(sqlite3_vt
1ff25 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 70 56  ab *pVtab){.  pV
1ff26 74 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a  tab->nRef++;.}..
1ff27 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 61 20 76  /*.** Unlock a v
1ff28 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 57  irtual table.  W
1ff29 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63  hen the last loc
1ff2a 6b 20 69 73 20 72 65 6d 6f 76 65 64 2c 0a 2a 2a  k is removed,.**
1ff2b 20 64 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 20   disconnect the 
1ff2c 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
1ff2d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ff2e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
1ff2f 62 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 20  bUnlock(sqlite3 
1ff30 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61  *db, sqlite3_vta
1ff31 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 70 56 74  b *pVtab){.  pVt
1ff32 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73  ab->nRef--;.  as
1ff33 73 65 72 74 28 64 62 29 3b 0a 20 20 61 73 73 65  sert(db);.  asse
1ff34 72 74 28 21 73 71 6c 69 74 65 33 53 61 66 65 74  rt(!sqlite3Safet
1ff35 79 43 68 65 63 6b 28 64 62 29 29 3b 0a 20 20 69  yCheck(db));.  i
1ff36 66 28 20 70 56 74 61 62 2d 3e 6e 52 65 66 3d 3d  f( pVtab->nRef==
1ff37 30 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d  0 ){.    if( db-
1ff38 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d  >magic==SQLITE_M
1ff39 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20  AGIC_BUSY ){.   
1ff3a 20 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79     sqlite3Safety
1ff3b 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 70  Off(db);.      p
1ff3c 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  Vtab->pModule->x
1ff3d 44 69 73 63 6f 6e 6e 65 63 74 28 70 56 74 61 62  Disconnect(pVtab
1ff3e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1ff3f 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
1ff40 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
1ff41 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d   pVtab->pModule-
1ff42 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70 56 74  >xDisconnect(pVt
1ff43 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ab);.    }.  }.}
1ff44 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e  ../*.** Clear an
1ff45 79 20 61 6e 64 20 61 6c 6c 20 76 69 72 74 75 61  y and all virtua
1ff46 6c 2d 74 61 62 6c 65 20 69 6e 66 6f 72 6d 61 74  l-table informat
1ff47 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 54 61 62  ion from the Tab
1ff48 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 54 68  le record..** Th
1ff49 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1ff4a 6c 6c 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c  lled, for exampl
1ff4b 65 2c 20 6a 75 73 74 20 62 65 66 6f 72 65 20 64  e, just before d
1ff4c 65 6c 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c  eleting the Tabl
1ff4d 65 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a  e.** record..*/.
1ff4e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1ff4f 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 43  oid sqlite3VtabC
1ff50 6c 65 61 72 28 54 61 62 6c 65 20 2a 70 29 7b 0a  lear(Table *p){.
1ff51 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1ff52 70 56 74 61 62 20 3d 20 70 2d 3e 70 56 74 61 62  pVtab = p->pVtab
1ff53 3b 0a 20 20 69 66 28 20 70 56 74 61 62 20 29 7b  ;.  if( pVtab ){
1ff54 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1ff55 70 4d 6f 64 20 26 26 20 70 2d 3e 70 4d 6f 64 2d  pMod && p->pMod-
1ff56 3e 70 4d 6f 64 75 6c 65 20 29 3b 0a 20 20 20 20  >pModule );.    
1ff57 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
1ff58 6b 28 70 2d 3e 70 53 63 68 65 6d 61 2d 3e 64 62  k(p->pSchema->db
1ff59 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20 70 2d  , pVtab);.    p-
1ff5a 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20 20 7d 0a  >pVtab = 0;.  }.
1ff5b 20 20 69 66 28 20 70 2d 3e 61 7a 4d 6f 64 75 6c    if( p->azModul
1ff5c 65 41 72 67 20 29 7b 0a 20 20 20 20 69 6e 74 20  eArg ){.    int 
1ff5d 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  i;.    for(i=0; 
1ff5e 69 3c 70 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b  i<p->nModuleArg;
1ff5f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
1ff60 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 4d  ite3_free(p->azM
1ff61 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b 0a 20 20  oduleArg[i]);.  
1ff62 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
1ff63 66 72 65 65 28 70 2d 3e 61 7a 4d 6f 64 75 6c 65  free(p->azModule
1ff64 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  Arg);.  }.}../*.
1ff65 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 6d 6f 64  ** Add a new mod
1ff66 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ule argument to 
1ff67 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
1ff68 41 72 67 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 74  Arg[]..** The st
1ff69 72 69 6e 67 20 69 73 20 6e 6f 74 20 63 6f 70 69  ring is not copi
1ff6a 65 64 20 2d 20 74 68 65 20 70 6f 69 6e 74 65 72  ed - the pointer
1ff6b 20 69 73 20 73 74 6f 72 65 64 2e 20 20 54 68 65   is stored.  The
1ff6c 0a 2a 2a 20 73 74 72 69 6e 67 20 77 69 6c 6c 20  .** string will 
1ff6d 62 65 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74  be freed automat
1ff6e 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20  ically when the 
1ff6f 74 61 62 6c 65 20 69 73 0a 2a 2a 20 64 65 6c 65  table is.** dele
1ff70 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ted..*/.static v
1ff71 6f 69 64 20 61 64 64 4d 6f 64 75 6c 65 41 72 67  oid addModuleArg
1ff72 75 6d 65 6e 74 28 73 71 6c 69 74 65 33 20 2a 64  ument(sqlite3 *d
1ff73 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  b, Table *pTable
1ff74 2c 20 63 68 61 72 20 2a 7a 41 72 67 29 7b 0a 20  , char *zArg){. 
1ff75 20 69 6e 74 20 69 20 3d 20 70 54 61 62 6c 65 2d   int i = pTable-
1ff76 3e 6e 4d 6f 64 75 6c 65 41 72 67 2b 2b 3b 0a 20  >nModuleArg++;. 
1ff77 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69   int nBytes = si
1ff78 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 28 31 2b  zeof(char *)*(1+
1ff79 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41  pTable->nModuleA
1ff7a 72 67 29 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a  rg);.  char **az
1ff7b 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 61 7a 4d  ModuleArg;.  azM
1ff7c 6f 64 75 6c 65 41 72 67 20 3d 20 73 71 6c 69 74  oduleArg = sqlit
1ff7d 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20  e3DbRealloc(db, 
1ff7e 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
1ff7f 41 72 67 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20  Arg, nBytes);.  
1ff80 69 66 28 20 61 7a 4d 6f 64 75 6c 65 41 72 67 3d  if( azModuleArg=
1ff81 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b  =0 ){.    int j;
1ff82 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
1ff83 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73  i; j++){.      s
1ff84 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 61 62  qlite3_free(pTab
1ff85 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  le->azModuleArg[
1ff86 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  j]);.    }.    s
1ff87 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 41 72 67  qlite3_free(zArg
1ff88 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
1ff89 72 65 65 28 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  ree(pTable->azMo
1ff8a 64 75 6c 65 41 72 67 29 3b 0a 20 20 20 20 70 54  duleArg);.    pT
1ff8b 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67  able->nModuleArg
1ff8c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
1ff8d 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69     azModuleArg[i
1ff8e 5d 20 3d 20 7a 41 72 67 3b 0a 20 20 20 20 61 7a  ] = zArg;.    az
1ff8f 4d 6f 64 75 6c 65 41 72 67 5b 69 2b 31 5d 20 3d  ModuleArg[i+1] =
1ff90 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65   0;.  }.  pTable
1ff91 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20  ->azModuleArg = 
1ff92 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 7d 0a 0a  azModuleArg;.}..
1ff93 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  /*.** The parser
1ff94 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
1ff95 69 6e 65 20 77 68 65 6e 20 69 74 20 66 69 72 73  ine when it firs
1ff96 74 20 73 65 65 73 20 61 20 43 52 45 41 54 45 20  t sees a CREATE 
1ff97 56 49 52 54 55 41 4c 20 54 41 42 4c 45 0a 2a 2a  VIRTUAL TABLE.**
1ff98 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
1ff99 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 68 61 73   module name has
1ff9a 20 62 65 65 6e 20 70 61 72 73 65 64 2c 20 62 75   been parsed, bu
1ff9b 74 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 6c  t the optional l
1ff9c 69 73 74 0a 2a 2a 20 6f 66 20 70 61 72 61 6d 65  ist.** of parame
1ff9d 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ters that follow
1ff9e 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   the module name
1ff9f 20 61 72 65 20 73 74 69 6c 6c 20 70 65 6e 64 69   are still pendi
1ffa0 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
1ffa1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1ffa2 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65  e3VtabBeginParse
1ffa3 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1ffa4 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
1ffa5 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1ffa6 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c    Token *pName1,
1ffa7 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
1ffa8 6f 66 20 6e 65 77 20 74 61 62 6c 65 2c 20 6f 72  of new table, or
1ffa9 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a   database name *
1ffaa 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
1ffab 32 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  2,        /* Nam
1ffac 65 20 6f 66 20 6e 65 77 20 74 61 62 6c 65 20 6f  e of new table o
1ffad 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65  r NULL */.  Toke
1ffae 6e 20 2a 70 4d 6f 64 75 6c 65 4e 61 6d 65 20 20  n *pModuleName  
1ffaf 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1ffb0 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 65 20   module for the 
1ffb1 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
1ffb2 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
1ffb3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1ffb4 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
1ffb5 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 63  table is being c
1ffb6 72 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 54  reated in */.  T
1ffb7 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20  able *pTable;   
1ffb8 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
1ffb9 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
1ffba 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
1ffbb 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1ffbc 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1ffbd 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  */..  if( pParse
1ffbe 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
1ffbf 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 65  LITE_SharedCache
1ffc0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1ffc1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1ffc2 22 43 61 6e 6e 6f 74 20 75 73 65 20 76 69 72 74  "Cannot use virt
1ffc3 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 73 68  ual tables in sh
1ffc4 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 22  ared-cache mode"
1ffc5 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
1ffc6 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 53 74 61   }..  sqlite3Sta
1ffc7 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  rtTable(pParse, 
1ffc8 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
1ffc9 30 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 70  0, 0, 1, 0);.  p
1ffca 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e  Table = pParse->
1ffcb 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28  pNewTable;.  if(
1ffcc 20 70 54 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 50   pTable==0 || pP
1ffcd 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74  arse->nErr ) ret
1ffce 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 30  urn;.  assert( 0
1ffcf 3d 3d 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  ==pTable->pIndex
1ffd0 20 29 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72   );..  db = pPar
1ffd1 73 65 2d 3e 64 62 3b 0a 20 20 69 44 62 20 3d 20  se->db;.  iDb = 
1ffd2 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1ffd3 6e 64 65 78 28 64 62 2c 20 70 54 61 62 6c 65 2d  ndex(db, pTable-
1ffd4 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
1ffd5 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a  ert( iDb>=0 );..
1ffd6 20 20 70 54 61 62 6c 65 2d 3e 69 73 56 69 72 74    pTable->isVirt
1ffd7 75 61 6c 20 3d 20 31 3b 0a 20 20 70 54 61 62 6c  ual = 1;.  pTabl
1ffd8 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20  e->nModuleArg = 
1ffd9 30 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72  0;.  addModuleAr
1ffda 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c  gument(db, pTabl
1ffdb 65 2c 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72  e, sqlite3NameFr
1ffdc 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4d 6f 64  omToken(db, pMod
1ffdd 75 6c 65 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64  uleName));.  add
1ffde 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64  ModuleArgument(d
1ffdf 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74  b, pTable, sqlit
1ffe0 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 64  e3DbStrDup(db, d
1ffe1 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1ffe2 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65  e));.  addModule
1ffe3 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61  Argument(db, pTa
1ffe4 62 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74  ble, sqlite3DbSt
1ffe5 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d  rDup(db, pTable-
1ffe6 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 70 50 61 72  >zName));.  pPar
1ffe7 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e  se->sNameToken.n
1ffe8 20 3d 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e   = pModuleName->
1ffe9 7a 20 2b 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d  z + pModuleName-
1ffea 3e 6e 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 3b 0a  >n - pName1->z;.
1ffeb 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ffec 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
1ffed 4f 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67  ON.  /* Creating
1ffee 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1ffef 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74   invokes the aut
1fff0 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
1fff1 61 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20  ack twice..  ** 
1fff2 54 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61  The first invoca
1fff3 74 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20  tion, to obtain 
1fff4 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e  permission to IN
1fff5 53 45 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20  SERT a row into 
1fff6 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f  the.  ** sqlite_
1fff7 6d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61  master table, ha
1fff8 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d  s already been m
1fff9 61 64 65 20 62 79 20 73 71 6c 69 74 65 33 53 74  ade by sqlite3St
1fffa 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a  artTable()..  **
1fffb 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c   The second call
1fffc 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d  , to obtain perm
1fffd 69 73 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65  ission to create
1fffe 20 74 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d   the table, is m
1ffff 61 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  ade now..  */.  
20000 69 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  if( pTable->azMo
20001 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73  duleArg ){.    s
20002 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
20003 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43  pParse, SQLITE_C
20004 52 45 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54  REATE_VTABLE, pT
20005 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20  able->zName, .  
20006 20 20 20 20 20 20 20 20 20 20 70 54 61 62 6c 65            pTable
20007 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
20008 2c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  , pParse->db->aD
20009 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
2000a 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
2000b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2000c 74 61 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65  takes the module
2000d 20 61 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68   argument that h
2000e 61 73 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61  as been accumula
2000f 74 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73  ting.** in pPars
20010 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70  e->zArg[] and ap
20011 70 65 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20  pends it to the 
20012 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74  list of argument
20013 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74  s on the.** virt
20014 75 61 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e  ual table curren
20015 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
20016 75 63 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65  uction in pParse
20017 2d 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ->pTable..*/.sta
20018 74 69 63 20 76 6f 69 64 20 61 64 64 41 72 67 75  tic void addArgu
20019 6d 65 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65  mentToVtab(Parse
2001a 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28   *pParse){.  if(
2001b 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
2001c 26 26 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  && pParse->pNewT
2001d 61 62 6c 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73  able ){.    cons
2001e 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e  t char *z = (con
2001f 73 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d  st char*)pParse-
20020 3e 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74  >sArg.z;.    int
20021 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72   n = pParse->sAr
20022 67 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g.n;.    sqlite3
20023 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
20024 62 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75 6c 65  b;.    addModule
20025 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 50 61  Argument(db, pPa
20026 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20  rse->pNewTable, 
20027 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
20028 28 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d  (db, z, n));.  }
20029 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  .}../*.** The pa
2002a 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
2002b 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 74 68  routine after th
2002c 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  e CREATE VIRTUAL
2002d 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
2002e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 6d  .** has been com
2002f 70 6c 65 74 65 6c 79 20 70 61 72 73 65 64 2e 0a  pletely parsed..
20030 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20031 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
20032 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61  abFinishParse(Pa
20033 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
20034 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54 61 62  en *pEnd){.  Tab
20035 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
20036 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65   /* The table be
20037 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20  ing constructed 
20038 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
20039 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
2003a 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2003b 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ion */.  char *z
2003c 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 2f 2a 20  Module;      /* 
2003d 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  The module name 
2003e 6f 66 20 74 68 65 20 74 61 62 6c 65 3a 20 55 53  of the table: US
2003f 49 4e 47 20 6d 6f 64 75 6c 65 6e 61 6d 65 20 2a  ING modulename *
20040 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  /.  Module *pMod
20041 20 3d 20 30 3b 0a 0a 20 20 61 64 64 41 72 67 75   = 0;..  addArgu
20042 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73  mentToVtab(pPars
20043 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41  e);.  pParse->sA
20044 72 67 2e 7a 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  rg.z = 0;..  /* 
20045 4c 6f 6f 6b 75 70 20 74 68 65 20 6d 6f 64 75 6c  Lookup the modul
20046 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 70 54 61  e name. */.  pTa
20047 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  b = pParse->pNew
20048 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61  Table;.  if( pTa
20049 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  b==0 ) return;. 
2004a 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
2004b 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d  ;.  if( pTab->nM
2004c 6f 64 75 6c 65 41 72 67 3c 31 20 29 20 72 65 74  oduleArg<1 ) ret
2004d 75 72 6e 3b 0a 20 20 7a 4d 6f 64 75 6c 65 20 3d  urn;.  zModule =
2004e 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
2004f 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20  rg[0];.  pMod = 
20050 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  (Module *)sqlite
20051 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61  3HashFind(&db->a
20052 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64 75 6c 65 2c  Module, zModule,
20053 20 73 74 72 6c 65 6e 28 7a 4d 6f 64 75 6c 65 29   strlen(zModule)
20054 29 3b 0a 20 20 70 54 61 62 2d 3e 70 4d 6f 64 20  );.  pTab->pMod 
20055 3d 20 70 4d 6f 64 3b 0a 20 20 0a 20 20 2f 2a 20  = pMod;.  .  /* 
20056 49 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49  If the CREATE VI
20057 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
20058 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 65  ement is being e
20059 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 0a 20  ntered for the. 
2005a 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 28   ** first time (
2005b 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69  in other words i
2005c 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2005d 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20  ble is actually 
2005e 62 65 69 6e 67 0a 20 20 2a 2a 20 63 72 65 61 74  being.  ** creat
2005f 65 64 20 6e 6f 77 20 69 6e 73 74 65 61 64 20 6f  ed now instead o
20060 66 20 6a 75 73 74 20 62 65 69 6e 67 20 72 65 61  f just being rea
20061 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 5f  d out of sqlite_
20062 6d 61 73 74 65 72 29 20 74 68 65 6e 0a 20 20 2a  master) then.  *
20063 2a 20 64 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20  * do additional 
20064 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 77  initialization w
20065 6f 72 6b 20 61 6e 64 20 73 74 6f 72 65 20 74 68  ork and store th
20066 65 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74  e statement text
20067 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c  .  ** in the sql
20068 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
20069 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  ..  */.  if( !db
2006a 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2006b 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a     char *zStmt;.
2006c 20 20 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65      char *zWhere
2006d 3b 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20  ;.    int iDb;. 
2006e 20 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 20     Vdbe *v;..   
2006f 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
20070 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
20071 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54   the CREATE VIRT
20072 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d  UAL TABLE statem
20073 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ent */.    if( p
20074 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 50 61  End ){.      pPa
20075 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
20076 6e 20 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 50  n = pEnd->z - pP
20077 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
20078 2e 7a 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20  .z + pEnd->n;.  
20079 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20 3d 20    }.    zStmt = 
2007a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2007b 62 2c 20 22 43 52 45 41 54 45 20 56 49 52 54 55  b, "CREATE VIRTU
2007c 41 4c 20 54 41 42 4c 45 20 25 54 22 2c 20 26 70  AL TABLE %T", &p
2007d 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
2007e 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c  n);..    /* A sl
2007f 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ot for the recor
20080 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  d has already be
20081 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  en allocated in 
20082 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49  the .    ** SQLI
20083 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e  TE_MASTER table.
20084 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74    We just need t
20085 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c  o update that sl
20086 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20  ot with all.    
20087 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  ** the informati
20088 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74  on we've collect
20089 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ed.  .    **.   
2008a 20 2a 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74   ** The top of t
2008b 68 65 20 73 74 61 63 6b 20 69 73 20 74 68 65 20  he stack is the 
2008c 72 6f 6f 74 70 61 67 65 20 61 6c 6c 6f 63 61 74  rootpage allocat
2008d 65 64 20 62 79 20 73 71 6c 69 74 65 33 53 74 61  ed by sqlite3Sta
2008e 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a  rtTable()..    *
2008f 2a 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20  * This value is 
20090 61 6c 77 61 79 73 20 30 20 61 6e 64 20 69 73 20  always 0 and is 
20091 69 67 6e 6f 72 65 64 2c 20 61 20 76 69 72 74 75  ignored, a virtu
20092 61 6c 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f  al table does no
20093 74 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a 20  t have a.    ** 
20094 72 6f 6f 74 70 61 67 65 2e 20 54 68 65 20 6e 65  rootpage. The ne
20095 78 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20  xt entry on the 
20096 73 74 61 63 6b 20 69 73 20 74 68 65 20 72 6f 77  stack is the row
20097 69 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  id of the record
20098 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 73  .    ** in the s
20099 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
2009a 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  le..    */.    i
2009b 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
2009c 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
2009d 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
2009e 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
2009f 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
200a0 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 73     "UPDATE %Q.%s
200a1 20 22 0a 20 20 20 20 20 20 20 20 20 22 53 45 54   ".         "SET
200a2 20 74 79 70 65 3d 27 74 61 62 6c 65 27 2c 20 6e   type='table', n
200a3 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65  ame=%Q, tbl_name
200a4 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 30 2c  =%Q, rootpage=0,
200a5 20 73 71 6c 3d 25 51 20 22 0a 20 20 20 20 20 20   sql=%Q ".      
200a6 20 22 57 48 45 52 45 20 72 6f 77 69 64 3d 23 31   "WHERE rowid=#1
200a7 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62  ",.      db->aDb
200a8 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48  [iDb].zName, SCH
200a9 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a  EMA_TABLE(iDb),.
200aa 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
200ab 65 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  e,.      pTab->z
200ac 4e 61 6d 65 2c 0a 20 20 20 20 20 20 7a 53 74 6d  Name,.      zStm
200ad 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c  t.    );.    sql
200ae 69 74 65 33 5f 66 72 65 65 28 7a 53 74 6d 74 29  ite3_free(zStmt)
200af 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65  ;.    v = sqlite
200b0 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
200b1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
200b2 6e 67 65 43 6f 6f 6b 69 65 28 64 62 2c 20 76 2c  ngeCookie(db, v,
200b3 20 69 44 62 29 3b 0a 0a 20 20 20 20 73 71 6c 69   iDb);..    sqli
200b4 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
200b5 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30 29  OP_Expire, 0, 0)
200b6 3b 0a 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73  ;.    zWhere = s
200b7 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
200b8 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70  , "name='%q'", p
200b9 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
200ba 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
200bb 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  v, OP_ParseSchem
200bc 61 2c 20 69 44 62 2c 20 31 2c 20 7a 57 68 65 72  a, iDb, 1, zWher
200bd 65 2c 20 50 33 5f 44 59 4e 41 4d 49 43 29 3b 0a  e, P3_DYNAMIC);.
200be 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f      sqlite3VdbeO
200bf 70 33 28 76 2c 20 4f 50 5f 56 43 72 65 61 74 65  p3(v, OP_VCreate
200c0 2c 20 69 44 62 2c 20 30 2c 20 70 54 61 62 2d 3e  , iDb, 0, pTab->
200c1 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 70 54  zName, strlen(pT
200c2 61 62 2d 3e 7a 4e 61 6d 65 29 20 2b 20 31 29 3b  ab->zName) + 1);
200c3 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65  .  }..  /* If we
200c4 20 61 72 65 20 72 65 72 65 61 64 69 6e 67 20 74   are rereading t
200c5 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
200c6 20 74 61 62 6c 65 20 63 72 65 61 74 65 20 74 68   table create th
200c7 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a  e in-memory.  **
200c8 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74   record of the t
200c9 61 62 6c 65 2e 20 49 66 20 74 68 65 20 6d 6f 64  able. If the mod
200ca 75 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20  ule has already 
200cb 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 2c  been registered,
200cc 0a 20 20 2a 2a 20 61 6c 73 6f 20 63 61 6c 6c 20  .  ** also call 
200cd 74 68 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74  the xConnect met
200ce 68 6f 64 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20  hod here..  */. 
200cf 20 65 6c 73 65 20 7b 0a 20 20 20 20 54 61 62 6c   else {.    Tabl
200d0 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 53 63 68  e *pOld;.    Sch
200d1 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70  ema *pSchema = p
200d2 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20  Tab->pSchema;.  
200d3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
200d4 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d  ame = pTab->zNam
200d5 65 3b 0a 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65  e;.    int nName
200d6 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29   = strlen(zName)
200d7 20 2b 20 31 3b 0a 20 20 20 20 70 4f 6c 64 20 3d   + 1;.    pOld =
200d8 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
200d9 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c  rt(&pSchema->tbl
200da 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Hash, zName, nNa
200db 6d 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 69  me, pTab);.    i
200dc 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20  f( pOld ){.     
200dd 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
200de 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 61 73 73  d = 1;.      ass
200df 65 72 74 28 20 70 54 61 62 3d 3d 70 4f 6c 64 20  ert( pTab==pOld 
200e0 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75  );  /* Malloc mu
200e1 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 69  st have failed i
200e2 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 74  nside HashInsert
200e3 28 29 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  () */.      retu
200e4 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53  rn;.    }.    pS
200e5 63 68 65 6d 61 2d 3e 64 62 20 3d 20 70 50 61 72  chema->db = pPar
200e6 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 50 61 72  se->db;.    pPar
200e7 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20  se->pNewTable = 
200e8 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
200e9 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
200ea 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68   this routine wh
200eb 65 6e 20 69 74 20 73 65 65 73 20 74 68 65 20 66  en it sees the f
200ec 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 6f 66  irst token.** of
200ed 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
200ee 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  the module name 
200ef 69 6e 20 61 20 43 52 45 41 54 45 20 56 49 52 54  in a CREATE VIRT
200f0 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d  UAL TABLE statem
200f1 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
200f2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
200f3 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 50  te3VtabArgInit(P
200f4 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
200f5 20 61 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74   addArgumentToVt
200f6 61 62 28 70 50 61 72 73 65 29 3b 0a 20 20 70 50  ab(pParse);.  pP
200f7 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30  arse->sArg.z = 0
200f8 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72 67  ;.  pParse->sArg
200f9 2e 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  .n = 0;.}../*.**
200fa 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
200fb 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  s this routine f
200fc 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 61 66  or each token af
200fd 74 65 72 20 74 68 65 20 66 69 72 73 74 20 74 6f  ter the first to
200fe 6b 65 6e 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 67  ken.** in an arg
200ff 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64  ument to the mod
20100 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43 52  ule name in a CR
20101 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
20102 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  LE statement..*/
20103 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20104 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
20105 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 65 20  ArgExtend(Parse 
20106 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a  *pParse, Token *
20107 70 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 72  p){.  Token *pAr
20108 67 20 3d 20 26 70 50 61 72 73 65 2d 3e 73 41 72  g = &pParse->sAr
20109 67 3b 0a 20 20 69 66 28 20 70 41 72 67 2d 3e 7a  g;.  if( pArg->z
2010a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 41 72 67 2d  ==0 ){.    pArg-
2010b 3e 7a 20 3d 20 70 2d 3e 7a 3b 0a 20 20 20 20 70  >z = p->z;.    p
2010c 41 72 67 2d 3e 6e 20 3d 20 70 2d 3e 6e 3b 0a 20  Arg->n = p->n;. 
2010d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
2010e 72 74 28 70 41 72 67 2d 3e 7a 20 3c 20 70 2d 3e  rt(pArg->z < p->
2010f 7a 29 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20  z);.    pArg->n 
20110 3d 20 28 70 2d 3e 7a 20 2b 20 70 2d 3e 6e 20 2d  = (p->z + p->n -
20111 20 70 41 72 67 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d   pArg->z);.  }.}
20112 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61  ../*.** Invoke a
20113 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63   virtual table c
20114 6f 6e 73 74 72 75 63 74 6f 72 20 28 65 69 74 68  onstructor (eith
20115 65 72 20 78 43 72 65 61 74 65 20 6f 72 20 78 43  er xCreate or xC
20116 6f 6e 6e 65 63 74 29 2e 20 54 68 65 0a 2a 2a 20  onnect). The.** 
20117 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66  pointer to the f
20118 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b  unction to invok
20119 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
2011a 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  he fourth parame
2011b 74 65 72 0a 2a 2a 20 74 6f 20 74 68 69 73 20 70  ter.** to this p
2011c 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
2011d 74 69 63 20 69 6e 74 20 76 74 61 62 43 61 6c 6c  tic int vtabCall
2011e 43 6f 6e 73 74 72 75 63 74 6f 72 28 0a 20 20 73  Constructor(.  s
2011f 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 54  qlite3 *db, .  T
20120 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 4d 6f  able *pTab,.  Mo
20121 64 75 6c 65 20 2a 70 4d 6f 64 2c 0a 20 20 69 6e  dule *pMod,.  in
20122 74 20 28 2a 78 43 6f 6e 73 74 72 75 63 74 29 28  t (*xConstruct)(
20123 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69  sqlite3*,void*,i
20124 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f  nt,const char*co
20125 6e 73 74 2a 2c 73 71 6c 69 74 65 33 5f 76 74 61  nst*,sqlite3_vta
20126 62 2a 2a 2c 63 68 61 72 2a 2a 29 2c 0a 20 20 63  b**,char**),.  c
20127 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20  har **pzErr.){. 
20128 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 72   int rc;.  int r
20129 63 32 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  c2;.  sqlite3_vt
2012a 61 62 20 2a 70 56 74 61 62 20 3d 20 30 3b 0a 20  ab *pVtab = 0;. 
2012b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
2012c 73 74 2a 61 7a 41 72 67 20 3d 20 28 63 6f 6e 73  st*azArg = (cons
2012d 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 29 70  t char *const*)p
2012e 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  Tab->azModuleArg
2012f 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20 3d 20 70  ;.  int nArg = p
20130 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b  Tab->nModuleArg;
20131 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20  .  char *zErr = 
20132 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4d 6f 64 75  0;.  char *zModu
20133 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  leName = sqlite3
20134 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22  MPrintf(db, "%s"
20135 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
20136 0a 20 20 69 66 28 20 21 7a 4d 6f 64 75 6c 65 4e  .  if( !zModuleN
20137 61 6d 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ame ){.    retur
20138 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
20139 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 21    }..  assert( !
2013a 64 62 2d 3e 70 56 54 61 62 20 29 3b 0a 20 20 61  db->pVTab );.  a
2013b 73 73 65 72 74 28 20 78 43 6f 6e 73 74 72 75 63  ssert( xConstruc
2013c 74 20 29 3b 0a 0a 20 20 64 62 2d 3e 70 56 54 61  t );..  db->pVTa
2013d 62 20 3d 20 70 54 61 62 3b 0a 20 20 72 63 20 3d  b = pTab;.  rc =
2013e 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
2013f 66 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74 28  f(db);.  assert(
20140 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
20141 3b 0a 20 20 72 63 20 3d 20 78 43 6f 6e 73 74 72  ;.  rc = xConstr
20142 75 63 74 28 64 62 2c 20 70 4d 6f 64 2d 3e 70 41  uct(db, pMod->pA
20143 75 78 2c 20 6e 41 72 67 2c 20 61 7a 41 72 67 2c  ux, nArg, azArg,
20144 20 26 70 56 74 61 62 2c 20 26 7a 45 72 72 29 3b   &pVtab, &zErr);
20145 0a 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33  .  rc2 = sqlite3
20146 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
20147 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
20148 4b 20 26 26 20 70 56 74 61 62 20 29 7b 0a 20 20  K && pVtab ){.  
20149 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65    pVtab->pModule
2014a 20 3d 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65   = pMod->pModule
2014b 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e 6e 52 65  ;.    pVtab->nRe
2014c 66 20 3d 20 31 3b 0a 20 20 20 20 70 54 61 62 2d  f = 1;.    pTab-
2014d 3e 70 56 74 61 62 20 3d 20 70 56 74 61 62 3b 0a  >pVtab = pVtab;.
2014e 20 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54    }..  if( SQLIT
2014f 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK!=rc ){.    
20150 69 66 28 20 7a 45 72 72 3d 3d 30 20 29 7b 0a 20  if( zErr==0 ){. 
20151 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71       *pzErr = sq
20152 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
20153 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74 72 75   "vtable constru
20154 63 74 6f 72 20 66 61 69 6c 65 64 3a 20 25 73 22  ctor failed: %s"
20155 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a  , zModuleName);.
20156 20 20 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20      }else {.    
20157 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
20158 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
20159 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  s", zErr);.     
2015a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
2015b 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  rr);.    }.  }el
2015c 73 65 20 69 66 28 20 64 62 2d 3e 70 56 54 61 62  se if( db->pVTab
2015d 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   ){.    const ch
2015e 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 22 76  ar *zFormat = "v
2015f 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f  table constructo
20160 72 20 64 69 64 20 6e 6f 74 20 64 65 63 6c 61 72  r did not declar
20161 65 20 73 63 68 65 6d 61 3a 20 25 73 22 3b 0a 20  e schema: %s";. 
20162 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69     *pzErr = sqli
20163 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a  te3MPrintf(db, z
20164 46 6f 72 6d 61 74 2c 20 70 54 61 62 2d 3e 7a 4e  Format, pTab->zN
20165 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ame);.    rc = S
20166 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
20167 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49   .  if( rc==SQLI
20168 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
20169 3d 20 72 63 32 3b 0a 20 20 7d 0a 20 20 64 62 2d  = rc2;.  }.  db-
2016a 3e 70 56 54 61 62 20 3d 20 30 3b 0a 20 20 73 71  >pVTab = 0;.  sq
2016b 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 6f 64 75  lite3_free(zModu
2016c 6c 65 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49  leName);..  /* I
2016d 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 65 6e  f everything wen
2016e 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 70  t according to p
2016f 6c 61 6e 2c 20 6c 6f 6f 70 20 74 68 72 6f 75 67  lan, loop throug
20170 68 20 74 68 65 20 63 6f 6c 75 6d 6e 73 0a 20 20  h the columns.  
20171 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ** of the table 
20172 74 6f 20 73 65 65 20 69 66 20 61 6e 79 20 6f 66  to see if any of
20173 20 74 68 65 6d 20 63 6f 6e 74 61 69 6e 20 74 68   them contain th
20174 65 20 74 6f 6b 65 6e 20 22 68 69 64 64 65 6e 22  e token "hidden"
20175 2e 0a 20 20 2a 2a 20 49 66 20 73 6f 2c 20 73 65  ..  ** If so, se
20176 74 20 74 68 65 20 43 6f 6c 75 6d 6e 2e 69 73 48  t the Column.isH
20177 69 64 64 65 6e 20 66 6c 61 67 20 61 6e 64 20 72  idden flag and r
20178 65 6d 6f 76 65 20 74 68 65 20 74 6f 6b 65 6e 20  emove the token 
20179 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 74 79  from.  ** the ty
2017a 70 65 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a  pe string..  */.
2017b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2017c 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  _OK ){.    int i
2017d 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69 43 6f  Col;.    for(iCo
2017e 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e  l=0; iCol<pTab->
2017f 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20  nCol; iCol++){. 
20180 20 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65       char *zType
20181 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
20182 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20 20  ol].zType;.     
20183 20 69 6e 74 20 6e 54 79 70 65 3b 0a 20 20 20 20   int nType;.    
20184 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 20    int i = 0;.   
20185 20 20 20 69 66 28 20 21 7a 54 79 70 65 20 29 20     if( !zType ) 
20186 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
20187 6e 54 79 70 65 20 3d 20 73 74 72 6c 65 6e 28 7a  nType = strlen(z
20188 54 79 70 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Type);.      if(
20189 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
2018a 28 22 68 69 64 64 65 6e 22 2c 20 7a 54 79 70 65  ("hidden", zType
2018b 2c 20 36 29 20 7c 7c 20 28 7a 54 79 70 65 5b 36  , 6) || (zType[6
2018c 5d 20 26 26 20 7a 54 79 70 65 5b 36 5d 21 3d 27  ] && zType[6]!='
2018d 20 27 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66   ') ){.        f
2018e 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79 70 65 3b  or(i=0; i<nType;
2018f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
20190 20 69 66 28 20 28 30 3d 3d 73 71 6c 69 74 65 33   if( (0==sqlite3
20191 53 74 72 4e 49 43 6d 70 28 22 20 68 69 64 64 65  StrNICmp(" hidde
20192 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d 2c 20 37  n", &zType[i], 7
20193 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26  )).           &&
20194 20 28 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 5c   (zType[i+7]=='\
20195 30 27 20 7c 7c 20 7a 54 79 70 65 5b 69 2b 37 5d  0' || zType[i+7]
20196 3d 3d 27 20 27 29 0a 20 20 20 20 20 20 20 20 20  ==' ').         
20197 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
20198 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  i++;.           
20199 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2019a 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2019b 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2019c 69 3c 6e 54 79 70 65 20 29 7b 0a 20 20 20 20 20  i<nType ){.     
2019d 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2019e 20 20 69 6e 74 20 6e 44 65 6c 20 3d 20 36 20 2b    int nDel = 6 +
2019f 20 28 7a 54 79 70 65 5b 69 2b 36 5d 20 3f 20 31   (zType[i+6] ? 1
201a0 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 66   : 0);.        f
201a1 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44 65 6c 29  or(j=i; (j+nDel)
201a2 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29 7b 0a 20  <=nType; j++){. 
201a3 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 5b 6a           zType[j
201a4 5d 20 3d 20 7a 54 79 70 65 5b 6a 2b 6e 44 65 6c  ] = zType[j+nDel
201a5 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ];.        }.   
201a6 20 20 20 20 20 69 66 28 20 7a 54 79 70 65 5b 69       if( zType[i
201a7 5d 3d 3d 27 5c 30 27 20 26 26 20 69 3e 30 20 29  ]=='\0' && i>0 )
201a8 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
201a9 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d 3d 3d 27  rt(zType[i-1]=='
201aa 20 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a   ');.          z
201ab 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27 5c 30 27  Type[i-1] = '\0'
201ac 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
201ad 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69      pTab->aCol[i
201ae 43 6f 6c 5d 2e 69 73 48 69 64 64 65 6e 20 3d 20  Col].isHidden = 
201af 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
201b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
201b1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
201b2 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f  function is invo
201b3 6b 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  ked by the parse
201b4 72 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43  r to call the xC
201b5 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 0a  onnect() method.
201b6 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  ** of the virtua
201b7 6c 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49 66  l table pTab. If
201b8 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
201b9 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  , an error code 
201ba 69 73 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20  is returned .** 
201bb 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6c 65 66  and an error lef
201bc 74 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2a 0a  t in pParse..**.
201bd 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20  ** This call is 
201be 61 20 6e 6f 2d 6f 70 20 69 66 20 74 61 62 6c 65  a no-op if table
201bf 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76   pTab is not a v
201c0 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  irtual table..*/
201c1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
201c2 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
201c3 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65  allConnect(Parse
201c4 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20   *pParse, Table 
201c5 2a 70 54 61 62 29 7b 0a 20 20 4d 6f 64 75 6c 65  *pTab){.  Module
201c6 20 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74 20 72 63   *pMod;.  int rc
201c7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
201c8 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20 21 70   if( !pTab || !p
201c9 54 61 62 2d 3e 69 73 56 69 72 74 75 61 6c 20 7c  Tab->isVirtual |
201ca 7c 20 70 54 61 62 2d 3e 70 56 74 61 62 20 29 7b  | pTab->pVtab ){
201cb 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
201cc 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4d  TE_OK;.  }..  pM
201cd 6f 64 20 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 3b  od = pTab->pMod;
201ce 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a  .  if( !pMod ){.
201cf 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
201d0 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e  zModule = pTab->
201d1 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a  azModuleArg[0];.
201d2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
201d3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
201d4 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22  such module: %s"
201d5 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20  , zModule);.    
201d6 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
201d7 52 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  R;.  } else {.  
201d8 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30    char *zErr = 0
201d9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ;.    sqlite3 *d
201da 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
201db 20 20 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c      rc = vtabCal
201dc 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c  lConstructor(db,
201dd 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f   pTab, pMod, pMo
201de 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6e  d->pModule->xCon
201df 6e 65 63 74 2c 20 26 7a 45 72 72 29 3b 0a 20 20  nect, &zErr);.  
201e0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
201e1 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
201e2 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
201e3 72 73 65 2c 20 22 25 73 22 2c 20 7a 45 72 72 29  rse, "%s", zErr)
201e4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
201e5 74 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a  te3_free(zErr);.
201e6 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
201e7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74  ;.}../*.** Add t
201e8 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
201e9 20 70 56 74 61 62 20 74 6f 20 74 68 65 20 61 72   pVtab to the ar
201ea 72 61 79 20 73 71 6c 69 74 65 33 2e 61 56 54 72  ray sqlite3.aVTr
201eb 61 6e 73 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  ans[]..*/.static
201ec 20 69 6e 74 20 61 64 64 54 6f 56 54 72 61 6e 73   int addToVTrans
201ed 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73 71  (sqlite3 *db, sq
201ee 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
201ef 62 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  b){.  const int 
201f0 41 52 52 41 59 5f 49 4e 43 52 20 3d 20 35 3b 0a  ARRAY_INCR = 5;.
201f1 0a 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20 73  .  /* Grow the s
201f2 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
201f3 72 72 61 79 20 69 66 20 72 65 71 75 69 72 65 64  rray if required
201f4 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 6e   */.  if( (db->n
201f5 56 54 72 61 6e 73 25 41 52 52 41 59 5f 49 4e 43  VTrans%ARRAY_INC
201f6 52 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  R)==0 ){.    sql
201f7 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72  ite3_vtab **aVTr
201f8 61 6e 73 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79  ans;.    int nBy
201f9 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 73 71 6c  tes = sizeof(sql
201fa 69 74 65 33 5f 76 74 61 62 20 2a 29 20 2a 20 28  ite3_vtab *) * (
201fb 64 62 2d 3e 6e 56 54 72 61 6e 73 20 2b 20 41 52  db->nVTrans + AR
201fc 52 41 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20 61  RAY_INCR);.    a
201fd 56 54 72 61 6e 73 20 3d 20 73 71 6c 69 74 65 33  VTrans = sqlite3
201fe 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 28 76  DbRealloc(db, (v
201ff 6f 69 64 20 2a 29 64 62 2d 3e 61 56 54 72 61 6e  oid *)db->aVTran
20200 73 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  s, nBytes);.    
20201 69 66 28 20 21 61 56 54 72 61 6e 73 20 29 7b 0a  if( !aVTrans ){.
20202 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
20203 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
20204 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61 56 54  .    memset(&aVT
20205 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72 61 6e 73  rans[db->nVTrans
20206 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c  ], 0, sizeof(sql
20207 69 74 65 33 5f 76 74 61 62 20 2a 29 2a 41 52 52  ite3_vtab *)*ARR
20208 41 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20 64 62  AY_INCR);.    db
20209 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61 56 54 72  ->aVTrans = aVTr
2020a 61 6e 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  ans;.  }..  /* A
2020b 64 64 20 70 56 74 61 62 20 74 6f 20 74 68 65 20  dd pVtab to the 
2020c 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 61  end of sqlite3.a
2020d 56 54 72 61 6e 73 20 2a 2f 0a 20 20 64 62 2d 3e  VTrans */.  db->
2020e 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72  aVTrans[db->nVTr
2020f 61 6e 73 2b 2b 5d 20 3d 20 70 56 74 61 62 3b 0a  ans++] = pVtab;.
20210 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63    sqlite3VtabLoc
20211 6b 28 70 56 74 61 62 29 3b 0a 20 20 72 65 74 75  k(pVtab);.  retu
20212 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
20213 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
20214 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20  tion is invoked 
20215 62 79 20 74 68 65 20 76 64 62 65 20 74 6f 20 63  by the vdbe to c
20216 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65 20  all the xCreate 
20217 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65  method.** of the
20218 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e   virtual table n
20219 61 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74  amed zTab in dat
2021a 61 62 61 73 65 20 69 44 62 2e 20 0a 2a 2a 0a 2a  abase iDb. .**.*
2021b 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
2021c 63 75 72 73 2c 20 2a 70 7a 45 72 72 20 69 73 20  curs, *pzErr is 
2021d 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 6e 20  set to point an 
2021e 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75  an English langu
2021f 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 69  age.** descripti
20220 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  on of the error 
20221 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 58 58  and an SQLITE_XX
20222 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  X error code is 
20223 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 6e 20  returned..** In 
20224 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61  this case the ca
20225 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73  ller must call s
20226 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 6f 6e  qlite3_free() on
20227 20 2a 70 7a 45 72 72 2e 0a 2a 2f 0a 53 51 4c 49   *pzErr..*/.SQLI
20228 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
20229 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72  qlite3VtabCallCr
2022a 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  eate(sqlite3 *db
2022b 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74  , int iDb, const
2022c 20 63 68 61 72 20 2a 7a 54 61 62 2c 20 63 68 61   char *zTab, cha
2022d 72 20 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e  r **pzErr){.  in
2022e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
2022f 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
20230 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b  .  Module *pMod;
20231 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
20232 4d 6f 64 75 6c 65 3b 0a 0a 20 20 70 54 61 62 20  Module;..  pTab 
20233 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
20234 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d  le(db, zTab, db-
20235 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
20236 3b 0a 20 20 61 73 73 65 72 74 28 70 54 61 62 20  ;.  assert(pTab 
20237 26 26 20 70 54 61 62 2d 3e 69 73 56 69 72 74 75  && pTab->isVirtu
20238 61 6c 20 26 26 20 21 70 54 61 62 2d 3e 70 56 74  al && !pTab->pVt
20239 61 62 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 70 54  ab);.  pMod = pT
2023a 61 62 2d 3e 70 4d 6f 64 3b 0a 20 20 7a 4d 6f 64  ab->pMod;.  zMod
2023b 75 6c 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f  ule = pTab->azMo
2023c 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 0a 20 20 2f  duleArg[0];..  /
2023d 2a 20 49 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  * If the module 
2023e 68 61 73 20 62 65 65 6e 20 72 65 67 69 73 74 65  has been registe
2023f 72 65 64 20 61 6e 64 20 69 6e 63 6c 75 64 65 73  red and includes
20240 20 61 20 43 72 65 61 74 65 20 6d 65 74 68 6f 64   a Create method
20241 2c 20 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 69  , .  ** invoke i
20242 74 20 6e 6f 77 2e 20 49 66 20 74 68 65 20 6d 6f  t now. If the mo
20243 64 75 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65  dule has not bee
20244 6e 20 72 65 67 69 73 74 65 72 65 64 2c 20 72 65  n registered, re
20245 74 75 72 6e 20 61 6e 20 0a 20 20 2a 2a 20 65 72  turn an .  ** er
20246 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ror. Otherwise, 
20247 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 20 20 2a 2f  do nothing..  */
20248 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a  .  if( !pMod ){.
20249 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c      *pzErr = sql
2024a 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2024b 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a  "no such module:
2024c 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a   %s", zModule);.
2024d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2024e 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ERROR;.  }else{.
2024f 20 20 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c      rc = vtabCal
20250 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c  lConstructor(db,
20251 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f   pTab, pMod, pMo
20252 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65  d->pModule->xCre
20253 61 74 65 2c 20 70 7a 45 72 72 29 3b 0a 20 20 7d  ate, pzErr);.  }
20254 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
20255 54 45 5f 4f 4b 20 26 26 20 70 54 61 62 2d 3e 70  TE_OK && pTab->p
20256 56 74 61 62 20 29 7b 0a 20 20 20 20 20 20 72 63  Vtab ){.      rc
20257 20 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64   = addToVTrans(d
20258 62 2c 20 70 54 61 62 2d 3e 70 56 74 61 62 29 3b  b, pTab->pVtab);
20259 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
2025a 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
2025b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
2025c 64 20 74 6f 20 73 65 74 20 74 68 65 20 73 63 68  d to set the sch
2025d 65 6d 61 20 6f 66 20 61 20 76 69 72 74 75 61 6c  ema of a virtual
2025e 20 74 61 62 6c 65 2e 20 20 49 74 20 69 73 20 6f   table.  It is o
2025f 6e 6c 79 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20  nly.** valid to 
20260 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69  call this functi
20261 6f 6e 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  on from within t
20262 68 65 20 78 43 72 65 61 74 65 28 29 20 6f 72 20  he xCreate() or 
20263 78 43 6f 6e 6e 65 63 74 28 29 20 6f 66 20 61 0a  xConnect() of a.
20264 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
20265 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   module..*/.SQLI
20266 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
20267 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28  e3_declare_vtab(
20268 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
20269 73 74 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65  st char *zCreate
2026a 54 61 62 6c 65 29 7b 0a 20 20 50 61 72 73 65 20  Table){.  Parse 
2026b 73 50 61 72 73 65 3b 0a 0a 20 20 69 6e 74 20 72  sParse;..  int r
2026c 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
2026d 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
2026e 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a  char *zErr = 0;.
2026f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
20270 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
20271 29 3b 0a 20 20 70 54 61 62 20 3d 20 64 62 2d 3e  );.  pTab = db->
20272 70 56 54 61 62 3b 0a 20 20 69 66 28 20 21 70 54  pVTab;.  if( !pT
20273 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ab ){.    sqlite
20274 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
20275 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20  E_MISUSE, 0);.  
20276 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
20277 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
20278 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
20279 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
2027a 20 20 61 73 73 65 72 74 28 70 54 61 62 2d 3e 69    assert(pTab->i
2027b 73 56 69 72 74 75 61 6c 20 26 26 20 70 54 61 62  sVirtual && pTab
2027c 2d 3e 6e 43 6f 6c 3d 3d 30 20 26 26 20 70 54 61  ->nCol==0 && pTa
2027d 62 2d 3e 61 43 6f 6c 3d 3d 30 29 3b 0a 0a 20 20  b->aCol==0);..  
2027e 6d 65 6d 73 65 74 28 26 73 50 61 72 73 65 2c 20  memset(&sParse, 
2027f 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29  0, sizeof(Parse)
20280 29 3b 0a 20 20 73 50 61 72 73 65 2e 64 65 63 6c  );.  sParse.decl
20281 61 72 65 56 74 61 62 20 3d 20 31 3b 0a 20 20 73  areVtab = 1;.  s
20282 50 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 0a  Parse.db = db;..
20283 20 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 4c    if( .      SQL
20284 49 54 45 5f 4f 4b 20 3d 3d 20 73 71 6c 69 74 65  ITE_OK == sqlite
20285 33 52 75 6e 50 61 72 73 65 72 28 26 73 50 61 72  3RunParser(&sPar
20286 73 65 2c 20 7a 43 72 65 61 74 65 54 61 62 6c 65  se, zCreateTable
20287 2c 20 26 7a 45 72 72 29 20 26 26 20 0a 20 20 20  , &zErr) && .   
20288 20 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61     sParse.pNewTa
20289 62 6c 65 20 26 26 20 0a 20 20 20 20 20 20 21 73  ble && .      !s
2028a 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d  Parse.pNewTable-
2028b 3e 70 53 65 6c 65 63 74 20 26 26 20 0a 20 20 20  >pSelect && .   
2028c 20 20 20 21 73 50 61 72 73 65 2e 70 4e 65 77 54     !sParse.pNewT
2028d 61 62 6c 65 2d 3e 69 73 56 69 72 74 75 61 6c 20  able->isVirtual 
2028e 0a 20 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e  .  ){.    pTab->
2028f 61 43 6f 6c 20 3d 20 73 50 61 72 73 65 2e 70 4e  aCol = sParse.pN
20290 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20  ewTable->aCol;. 
20291 20 20 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20     pTab->nCol = 
20292 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65  sParse.pNewTable
20293 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 73 50 61 72  ->nCol;.    sPar
20294 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43  se.pNewTable->nC
20295 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 73 50 61 72  ol = 0;.    sPar
20296 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43  se.pNewTable->aC
20297 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e  ol = 0;.    db->
20298 70 56 54 61 62 20 3d 20 30 3b 0a 20 20 7d 20 65  pVTab = 0;.  } e
20299 6c 73 65 20 7b 0a 20 20 20 20 73 71 6c 69 74 65  lse {.    sqlite
2029a 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
2029b 45 5f 45 52 52 4f 52 2c 20 7a 45 72 72 29 3b 0a  E_ERROR, zErr);.
2029c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2029d 28 7a 45 72 72 29 3b 0a 20 20 20 20 72 63 20 3d  (zErr);.    rc =
2029e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2029f 20 7d 0a 20 20 73 50 61 72 73 65 2e 64 65 63 6c   }.  sParse.decl
202a0 61 72 65 56 74 61 62 20 3d 20 30 3b 0a 0a 20 20  areVtab = 0;..  
202a1 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
202a2 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  ((sqlite3_stmt*)
202a3 73 50 61 72 73 65 2e 70 56 64 62 65 29 3b 0a 20  sParse.pVdbe);. 
202a4 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
202a5 62 6c 65 28 73 50 61 72 73 65 2e 70 4e 65 77 54  ble(sParse.pNewT
202a6 61 62 6c 65 29 3b 0a 20 20 73 50 61 72 73 65 2e  able);.  sParse.
202a7 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 0a  pNewTable = 0;..
202a8 20 20 61 73 73 65 72 74 28 20 28 72 63 26 30 78    assert( (rc&0x
202a9 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 72 63 20  ff)==rc );.  rc 
202aa 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
202ab 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
202ac 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
202ad 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
202ae 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
202af 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
202b0 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68  is invoked by th
202b1 65 20 76 64 62 65 20 74 6f 20 63 61 6c 6c 20 74  e vdbe to call t
202b2 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68  he xDestroy meth
202b3 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72  od.** of the vir
202b4 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 64  tual table named
202b5 20 7a 54 61 62 20 69 6e 20 64 61 74 61 62 61 73   zTab in databas
202b6 65 20 69 44 62 2e 20 54 68 69 73 20 6f 63 63 75  e iDb. This occu
202b7 72 73 0a 2a 2a 20 77 68 65 6e 20 61 20 44 52 4f  rs.** when a DRO
202b8 50 20 54 41 42 4c 45 20 69 73 20 6d 65 6e 74 69  P TABLE is menti
202b9 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  oned..**.** This
202ba 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70   call is a no-op
202bb 20 69 66 20 7a 54 61 62 20 69 73 20 6e 6f 74 20   if zTab is not 
202bc 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
202bd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
202be 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
202bf 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 73 71  abCallDestroy(sq
202c0 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
202c1 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  Db, const char *
202c2 7a 54 61 62 29 0a 7b 0a 20 20 69 6e 74 20 72 63  zTab).{.  int rc
202c3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
202c4 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20 20  Table *pTab;..  
202c5 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
202c6 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
202c7 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
202c8 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28  Name);.  assert(
202c9 70 54 61 62 29 3b 0a 20 20 69 66 28 20 70 54 61  pTab);.  if( pTa
202ca 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20  b->pVtab ){.    
202cb 69 6e 74 20 28 2a 78 44 65 73 74 72 6f 79 29 28  int (*xDestroy)(
202cc 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
202cd 54 61 62 29 20 3d 20 70 54 61 62 2d 3e 70 4d 6f  Tab) = pTab->pMo
202ce 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73  d->pModule->xDes
202cf 74 72 6f 79 3b 0a 20 20 20 20 72 63 20 3d 20 73  troy;.    rc = s
202d0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
202d1 64 62 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  db);.    assert(
202d2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
202d3 3b 0a 20 20 20 20 69 66 28 20 78 44 65 73 74 72  ;.    if( xDestr
202d4 6f 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  oy ){.      rc =
202d5 20 78 44 65 73 74 72 6f 79 28 70 54 61 62 2d 3e   xDestroy(pTab->
202d6 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  pVtab);.    }.  
202d7 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f    sqlite3SafetyO
202d8 6e 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72  n(db);.    if( r
202d9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
202da 20 20 20 20 20 20 70 54 61 62 2d 3e 70 56 74 61        pTab->pVta
202db 62 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  b = 0;.    }.  }
202dc 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
202dd 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
202de 63 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 65 69  ction invokes ei
202df 74 68 65 72 20 74 68 65 20 78 52 6f 6c 6c 62 61  ther the xRollba
202e0 63 6b 20 6f 72 20 78 43 6f 6d 6d 69 74 20 6d 65  ck or xCommit me
202e1 74 68 6f 64 0a 2a 2a 20 6f 66 20 65 61 63 68 20  thod.** of each 
202e2 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
202e3 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71 6c  ables in the sql
202e4 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72  ite3.aVTrans arr
202e5 61 79 2e 20 54 68 65 20 6d 65 74 68 6f 64 0a 2a  ay. The method.*
202e6 2a 20 63 61 6c 6c 65 64 20 69 73 20 69 64 65 6e  * called is iden
202e7 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65  tified by the se
202e8 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22  cond argument, "
202e9 6f 66 66 73 65 74 22 2c 20 77 68 69 63 68 20 69  offset", which i
202ea 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20  s.** the offset 
202eb 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 20 74 6f  of the method to
202ec 20 63 61 6c 6c 20 69 6e 20 74 68 65 20 73 71 6c   call in the sql
202ed 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73 74 72 75  ite3_module stru
202ee 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cture..**.** The
202ef 20 61 72 72 61 79 20 69 73 20 63 6c 65 61 72 65   array is cleare
202f0 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67  d after invoking
202f1 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 2e 20   the callbacks. 
202f2 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
202f3 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28 73 71  callFinaliser(sq
202f4 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f  lite3 *db, int o
202f5 66 66 73 65 74 29 7b 0a 20 20 69 6e 74 20 69 3b  ffset){.  int i;
202f6 0a 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61  .  if( db->aVTra
202f7 6e 73 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ns ){.    for(i=
202f8 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73  0; i<db->nVTrans
202f9 20 26 26 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b   && db->aVTrans[
202fa 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
202fb 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
202fc 74 61 62 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e  tab = db->aVTran
202fd 73 5b 69 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20  s[i];.      int 
202fe 28 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61  (*x)(sqlite3_vta
202ff 62 20 2a 29 3b 0a 20 20 20 20 20 20 78 20 3d 20  b *);.      x = 
20300 2a 28 69 6e 74 20 28 2a 2a 29 28 73 71 6c 69 74  *(int (**)(sqlit
20301 65 33 5f 76 74 61 62 20 2a 29 29 28 28 63 68 61  e3_vtab *))((cha
20302 72 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75  r *)pVtab->pModu
20303 6c 65 20 2b 20 6f 66 66 73 65 74 29 3b 0a 20 20  le + offset);.  
20304 20 20 20 20 69 66 28 20 78 20 29 20 78 28 70 56      if( x ) x(pV
20305 74 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  tab);.      sqli
20306 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62  te3VtabUnlock(db
20307 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a  , pVtab);.    }.
20308 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
20309 28 64 62 2d 3e 61 56 54 72 61 6e 73 29 3b 0a 20  (db->aVTrans);. 
2030a 20 20 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20 3d     db->nVTrans =
2030b 20 30 3b 0a 20 20 20 20 64 62 2d 3e 61 56 54 72   0;.    db->aVTr
2030c 61 6e 73 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  ans = 0;.  }.}..
2030d 2f 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e  /*.** If argumen
2030e 74 20 72 63 32 20 69 73 20 6e 6f 74 20 53 51 4c  t rc2 is not SQL
2030f 49 54 45 5f 4f 4b 2c 20 74 68 65 6e 20 72 65 74  ITE_OK, then ret
20310 75 72 6e 20 69 74 20 61 6e 64 20 64 6f 20 6e 6f  urn it and do no
20311 74 68 69 6e 67 2e 20 0a 2a 2a 20 4f 74 68 65 72  thing. .** Other
20312 77 69 73 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65  wise, invoke the
20313 20 78 53 79 6e 63 20 6d 65 74 68 6f 64 20 6f 66   xSync method of
20314 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62   all virtual tab
20315 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73  les in the .** s
20316 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
20317 72 72 61 79 2e 20 52 65 74 75 72 6e 20 74 68 65  rray. Return the
20318 20 65 72 72 6f 72 20 63 6f 64 65 20 66 6f 72 20   error code for 
20319 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20  the first error 
2031a 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 73 2c  .** that occurs,
2031b 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66   or SQLITE_OK if
2031c 20 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65 72 61   all xSync opera
2031d 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63 65 73  tions are succes
2031e 73 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sful..*/.SQLITE_
2031f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
20320 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69  te3VtabSync(sqli
20321 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 72 63 32  te3 *db, int rc2
20322 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
20323 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
20324 3b 0a 20 20 69 6e 74 20 72 63 73 61 66 65 74 79  ;.  int rcsafety
20325 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ;.  sqlite3_vtab
20326 20 2a 2a 61 56 54 72 61 6e 73 20 3d 20 64 62 2d   **aVTrans = db-
20327 3e 61 56 54 72 61 6e 73 3b 0a 20 20 69 66 28 20  >aVTrans;.  if( 
20328 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  rc2!=SQLITE_OK )
20329 20 72 65 74 75 72 6e 20 72 63 32 3b 0a 0a 20 20   return rc2;..  
2032a 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  rc = sqlite3Safe
2032b 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 64 62 2d  tyOff(db);.  db-
2032c 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20  >aVTrans = 0;.  
2032d 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
2032e 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e  ITE_OK && i<db->
2032f 6e 56 54 72 61 6e 73 20 26 26 20 61 56 54 72 61  nVTrans && aVTra
20330 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  ns[i]; i++){.   
20331 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
20332 56 74 61 62 20 3d 20 61 56 54 72 61 6e 73 5b 69  Vtab = aVTrans[i
20333 5d 3b 0a 20 20 20 20 69 6e 74 20 28 2a 78 29 28  ];.    int (*x)(
20334 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 3b  sqlite3_vtab *);
20335 0a 20 20 20 20 78 20 3d 20 70 56 74 61 62 2d 3e  .    x = pVtab->
20336 70 4d 6f 64 75 6c 65 2d 3e 78 53 79 6e 63 3b 0a  pModule->xSync;.
20337 20 20 20 20 69 66 28 20 78 20 29 7b 0a 20 20 20      if( x ){.   
20338 20 20 20 72 63 20 3d 20 78 28 70 56 74 61 62 29     rc = x(pVtab)
20339 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62  ;.    }.  }.  db
2033a 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61 56 54 72  ->aVTrans = aVTr
2033b 61 6e 73 3b 0a 20 20 72 63 73 61 66 65 74 79 20  ans;.  rcsafety 
2033c 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  = sqlite3SafetyO
2033d 6e 28 64 62 29 3b 0a 0a 20 20 69 66 28 20 72 63  n(db);..  if( rc
2033e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
2033f 20 20 20 72 63 20 3d 20 72 63 73 61 66 65 74 79     rc = rcsafety
20340 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
20341 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  c;.}../*.** Invo
20342 6b 65 20 74 68 65 20 78 52 6f 6c 6c 62 61 63 6b  ke the xRollback
20343 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76   method of all v
20344 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e  irtual tables in
20345 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33   the .** sqlite3
20346 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20  .aVTrans array. 
20347 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61  Then clear the a
20348 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a  rray itself..*/.
20349 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2034a 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  nt sqlite3VtabRo
2034b 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 20 2a  llback(sqlite3 *
2034c 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c  db){.  callFinal
2034d 69 73 65 72 28 64 62 2c 20 28 69 6e 74 29 28 26  iser(db, (int)(&
2034e 28 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  ((sqlite3_module
2034f 20 2a 29 30 29 2d 3e 78 52 6f 6c 6c 62 61 63 6b   *)0)->xRollback
20350 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ));.  return SQL
20351 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
20352 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 43 6f 6d   Invoke the xCom
20353 6d 69 74 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c  mit method of al
20354 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  l virtual tables
20355 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69   in the .** sqli
20356 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
20357 79 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68  y. Then clear th
20358 65 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a  e array itself..
20359 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2035a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
2035b 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20  bCommit(sqlite3 
2035c 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61  *db){.  callFina
2035d 6c 69 73 65 72 28 64 62 2c 20 28 69 6e 74 29 28  liser(db, (int)(
2035e 26 28 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  &((sqlite3_modul
2035f 65 20 2a 29 30 29 2d 3e 78 43 6f 6d 6d 69 74 29  e *)0)->xCommit)
20360 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
20361 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
20362 49 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  If the virtual t
20363 61 62 6c 65 20 70 56 74 61 62 20 73 75 70 70 6f  able pVtab suppo
20364 72 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 74  rts the transact
20365 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  ion interface.**
20366 20 28 78 42 65 67 69 6e 2f 78 52 6f 6c 6c 62 61   (xBegin/xRollba
20367 63 6b 2f 78 43 6f 6d 6d 69 74 20 61 6e 64 20 6f  ck/xCommit and o
20368 70 74 69 6f 6e 61 6c 6c 79 20 78 53 79 6e 63 29  ptionally xSync)
20369 20 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69   and a transacti
2036a 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 75 72  on is.** not cur
2036b 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 69 6e 76  rently open, inv
2036c 6f 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d  oke the xBegin m
2036d 65 74 68 6f 64 20 6e 6f 77 2e 0a 2a 2a 0a 2a 2a  ethod now..**.**
2036e 20 49 66 20 74 68 65 20 78 42 65 67 69 6e 20 63   If the xBegin c
2036f 61 6c 6c 20 69 73 20 73 75 63 63 65 73 73 66 75  all is successfu
20370 6c 2c 20 70 6c 61 63 65 20 74 68 65 20 73 71 6c  l, place the sql
20371 69 74 65 33 5f 76 74 61 62 20 70 6f 69 6e 74 65  ite3_vtab pointe
20372 72 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69  r.** in the sqli
20373 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
20374 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
20375 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
20376 56 74 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65  VtabBegin(sqlite
20377 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f 76  3 *db, sqlite3_v
20378 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 69  tab *pVtab){.  i
20379 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
2037a 4b 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  K;.  const sqlit
2037b 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
2037c 6c 65 3b 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61  le;..  /* Specia
2037d 6c 20 63 61 73 65 3a 20 49 66 20 64 62 2d 3e 61  l case: If db->a
2037e 56 54 72 61 6e 73 20 69 73 20 4e 55 4c 4c 20 61  VTrans is NULL a
2037f 6e 64 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20 69  nd db->nVTrans i
20380 73 20 67 72 65 61 74 65 72 0a 20 20 2a 2a 20 74  s greater.  ** t
20381 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
20382 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
20383 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f  being called fro
20384 6d 20 77 69 74 68 69 6e 20 61 0a 20 20 2a 2a 20  m within a.  ** 
20385 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 78  virtual module x
20386 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 2e  Sync() callback.
20387 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
20388 6f 20 77 72 69 74 65 20 74 6f 20 0a 20 20 2a 2a  o write to .  **
20389 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20   virtual module 
2038a 74 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 63  tables in this c
2038b 61 73 65 2c 20 73 6f 20 72 65 74 75 72 6e 20 53  ase, so return S
2038c 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20  QLITE_LOCKED..  
2038d 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 64 62 2d 3e  */.  if( 0==db->
2038e 61 56 54 72 61 6e 73 20 26 26 20 64 62 2d 3e 6e  aVTrans && db->n
2038f 56 54 72 61 6e 73 3e 30 20 29 7b 0a 20 20 20 20  VTrans>0 ){.    
20390 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
20391 43 4b 45 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20  CKED;.  }.  if( 
20392 21 70 56 74 61 62 20 29 7b 0a 20 20 20 20 72 65  !pVtab ){.    re
20393 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
20394 20 20 7d 20 0a 20 20 70 4d 6f 64 75 6c 65 20 3d    } .  pModule =
20395 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b   pVtab->pModule;
20396 0a 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d  ..  if( pModule-
20397 3e 78 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 69  >xBegin ){.    i
20398 6e 74 20 69 3b 0a 0a 0a 20 20 20 20 2f 2a 20 49  nt i;...    /* I
20399 66 20 70 56 74 61 62 20 69 73 20 61 6c 72 65 61  f pVtab is alrea
2039a 64 79 20 69 6e 20 74 68 65 20 61 56 54 72 61 6e  dy in the aVTran
2039b 73 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e 20  s array, return 
2039c 65 61 72 6c 79 20 2a 2f 0a 20 20 20 20 66 6f 72  early */.    for
2039d 28 69 3d 30 3b 20 28 69 3c 64 62 2d 3e 6e 56 54  (i=0; (i<db->nVT
2039e 72 61 6e 73 29 20 26 26 20 30 21 3d 64 62 2d 3e  rans) && 0!=db->
2039f 61 56 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29  aVTrans[i]; i++)
203a0 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e  {.      if( db->
203a1 61 56 54 72 61 6e 73 5b 69 5d 3d 3d 70 56 74 61  aVTrans[i]==pVta
203a2 62 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  b ){.        ret
203a3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
203a4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
203a5 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
203a6 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 2a 2f  xBegin method */
203a7 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  .    rc = pModul
203a8 65 2d 3e 78 42 65 67 69 6e 28 70 56 74 61 62 29  e->xBegin(pVtab)
203a9 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
203aa 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
203ab 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
203ac 7d 0a 0a 20 20 20 20 72 63 20 3d 20 61 64 64 54  }..    rc = addT
203ad 6f 56 54 72 61 6e 73 28 64 62 2c 20 70 56 74 61  oVTrans(db, pVta
203ae 62 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  b);.  }.  return
203af 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
203b0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
203b1 72 20 28 70 44 65 66 29 20 69 73 20 61 20 66 75  r (pDef) is a fu
203b2 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
203b3 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 73  ation.  The.** s
203b4 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
203b5 28 70 45 78 70 72 29 20 69 73 20 74 68 65 20 66  (pExpr) is the f
203b6 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
203b7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
203b8 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 20 61  ** If pExpr is a
203b9 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72   column in a vir
203ba 74 75 61 6c 20 74 61 62 6c 65 2c 20 74 68 65 6e  tual table, then
203bb 20 6c 65 74 20 74 68 65 20 76 69 72 74 75 61 6c   let the virtual
203bc 0a 2a 2a 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d  .** table implem
203bd 65 6e 74 61 74 69 6f 6e 20 68 61 76 65 20 61 6e  entation have an
203be 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20   opportunity to 
203bf 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66 75 6e  overload the fun
203c0 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ction..**.** Thi
203c1 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
203c2 64 20 74 6f 20 61 6c 6c 6f 77 20 76 69 72 74 75  d to allow virtu
203c3 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  al table impleme
203c4 6e 74 61 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 6f  ntations to.** o
203c5 76 65 72 6c 6f 61 64 20 4d 41 54 43 48 2c 20 4c  verload MATCH, L
203c6 49 4b 45 2c 20 47 4c 4f 42 2c 20 61 6e 64 20 52  IKE, GLOB, and R
203c7 45 47 45 58 50 20 6f 70 65 72 61 74 6f 72 73 2e  EGEXP operators.
203c8 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 65 69  .**.** Return ei
203c9 74 68 65 72 20 74 68 65 20 70 44 65 66 20 61 72  ther the pDef ar
203ca 67 75 6d 65 6e 74 20 28 69 6e 64 69 63 61 74 69  gument (indicati
203cb 6e 67 20 6e 6f 20 63 68 61 6e 67 65 29 20 6f 72  ng no change) or
203cc 20 61 20 0a 2a 2a 20 6e 65 77 20 46 75 6e 63 44   a .** new FuncD
203cd 65 66 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ef structure tha
203ce 74 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 65  t is marked as e
203cf 70 68 65 6d 65 72 61 6c 20 75 73 69 6e 67 20 74  phemeral using t
203d0 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4e  he.** SQLITE_FUN
203d1 43 5f 45 50 48 45 4d 20 66 6c 61 67 2e 0a 2a 2f  C_EPHEM flag..*/
203d2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
203d3 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33  FuncDef *sqlite3
203d4 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63  VtabOverloadFunc
203d5 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  tion(.  sqlite3 
203d6 2a 64 62 2c 20 20 20 20 2f 2a 20 44 61 74 61 62  *db,    /* Datab
203d7 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66  ase connection f
203d8 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6d 61 6c  or reporting mal
203d9 6c 6f 63 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a  loc problems */.
203da 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 2c    FuncDef *pDef,
203db 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f    /* Function to
203dc 20 70 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f   possibly overlo
203dd 61 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67  ad */.  int nArg
203de 2c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ,       /* Numbe
203df 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
203e0 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
203e1 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  /.  Expr *pExpr 
203e2 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
203e3 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e  ument to the fun
203e4 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 54 61  ction */.){.  Ta
203e5 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c  ble *pTab;.  sql
203e6 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
203e7 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  ;.  sqlite3_modu
203e8 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 76 6f 69 64  le *pMod;.  void
203e9 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
203ea 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
203eb 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b  qlite3_value**);
203ec 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20  .  void *pArg;. 
203ed 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 77 3b 0a   FuncDef *pNew;.
203ee 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72    int rc;.  char
203ef 20 2a 7a 4c 6f 77 65 72 4e 61 6d 65 3b 0a 20 20   *zLowerName;.  
203f0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
203f1 3b 0a 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  ;...  /* Check t
203f2 6f 20 73 65 65 20 74 68 65 20 6c 65 66 74 20 6f  o see the left o
203f3 70 65 72 61 6e 64 20 69 73 20 61 20 63 6f 6c 75  perand is a colu
203f4 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20  mn in a virtual 
203f5 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70  table */.  if( p
203f6 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  Expr==0 ) return
203f7 20 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45 78   pDef;.  if( pEx
203f8 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pr->op!=TK_COLUM
203f9 4e 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b  N ) return pDef;
203fa 0a 20 20 70 54 61 62 20 3d 20 70 45 78 70 72 2d  .  pTab = pExpr-
203fb 3e 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61  >pTab;.  if( pTa
203fc 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44  b==0 ) return pD
203fd 65 66 3b 0a 20 20 69 66 28 20 21 70 54 61 62 2d  ef;.  if( !pTab-
203fe 3e 69 73 56 69 72 74 75 61 6c 20 29 20 72 65 74  >isVirtual ) ret
203ff 75 72 6e 20 70 44 65 66 3b 0a 20 20 70 56 74 61  urn pDef;.  pVta
20400 62 20 3d 20 70 54 61 62 2d 3e 70 56 74 61 62 3b  b = pTab->pVtab;
20401 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62  .  assert( pVtab
20402 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
20403 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 21   pVtab->pModule!
20404 3d 30 20 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28  =0 );.  pMod = (
20405 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
20406 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b  )pVtab->pModule;
20407 0a 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 46 69  .  if( pMod->xFi
20408 6e 64 46 75 6e 63 74 69 6f 6e 3d 3d 30 20 29 20  ndFunction==0 ) 
20409 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 0a 20  return pDef;. . 
2040a 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 78 46 75   /* Call the xFu
2040b 6e 63 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f  ncFunction metho
2040c 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61 6c  d on the virtual
2040d 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
2040e 61 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 73 65  ation.  ** to se
2040f 65 20 69 66 20 74 68 65 20 69 6d 70 6c 65 6d 65  e if the impleme
20410 6e 74 61 74 69 6f 6e 20 77 61 6e 74 73 20 74 6f  ntation wants to
20411 20 6f 76 65 72 6c 6f 61 64 20 74 68 69 73 20 66   overload this f
20412 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2f 0a 20 20  unction .  */.  
20413 7a 4c 6f 77 65 72 4e 61 6d 65 20 3d 20 73 71 6c  zLowerName = sql
20414 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
20415 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pDef->zName);. 
20416 20 69 66 28 20 7a 4c 6f 77 65 72 4e 61 6d 65 20   if( zLowerName 
20417 29 7b 0a 20 20 20 20 66 6f 72 28 7a 3d 28 75 6e  ){.    for(z=(un
20418 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 4c 6f  signed char*)zLo
20419 77 65 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b 2b  werName; *z; z++
2041a 29 7b 0a 20 20 20 20 20 20 2a 7a 20 3d 20 73 71  ){.      *z = sq
2041b 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
2041c 72 5b 2a 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  r[*z];.    }.   
2041d 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 46 69 6e   rc = pMod->xFin
2041e 64 46 75 6e 63 74 69 6f 6e 28 70 56 74 61 62 2c  dFunction(pVtab,
2041f 20 6e 41 72 67 2c 20 7a 4c 6f 77 65 72 4e 61 6d   nArg, zLowerNam
20420 65 2c 20 26 78 46 75 6e 63 2c 20 26 70 41 72 67  e, &xFunc, &pArg
20421 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
20422 72 65 65 28 7a 4c 6f 77 65 72 4e 61 6d 65 29 3b  ree(zLowerName);
20423 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 30  .  }.  if( rc==0
20424 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
20425 44 65 66 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  Def;.  }..  /* C
20426 72 65 61 74 65 20 61 20 6e 65 77 20 65 70 68 65  reate a new ephe
20427 6d 65 72 61 6c 20 66 75 6e 63 74 69 6f 6e 20 64  meral function d
20428 65 66 69 6e 69 74 69 6f 6e 20 66 6f 72 20 74 68  efinition for th
20429 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 20 20 2a  e overloaded.  *
2042a 2a 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  * function */.  
2042b 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
2042c 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2042d 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20 73  izeof(*pNew) + s
2042e 74 72 6c 65 6e 28 70 44 65 66 2d 3e 7a 4e 61 6d  trlen(pDef->zNam
2042f 65 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  e) );.  if( pNew
20430 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
20431 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 20 20 2a 70  n pDef;.  }.  *p
20432 4e 65 77 20 3d 20 2a 70 44 65 66 3b 0a 20 20 6d  New = *pDef;.  m
20433 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 4e 61 6d  emcpy(pNew->zNam
20434 65 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20  e, pDef->zName, 
20435 73 74 72 6c 65 6e 28 70 44 65 66 2d 3e 7a 4e 61  strlen(pDef->zNa
20436 6d 65 29 2b 31 29 3b 0a 20 20 70 4e 65 77 2d 3e  me)+1);.  pNew->
20437 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b 0a 20  xFunc = xFunc;. 
20438 20 70 4e 65 77 2d 3e 70 55 73 65 72 44 61 74 61   pNew->pUserData
20439 20 3d 20 70 41 72 67 3b 0a 20 20 70 4e 65 77 2d   = pArg;.  pNew-
2043a 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
2043b 5f 46 55 4e 43 5f 45 50 48 45 4d 3b 0a 20 20 72  _FUNC_EPHEM;.  r
2043c 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 23  eturn pNew;.}..#
2043d 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2043e 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2043f 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
20440 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 74 61  ***** End of vta
20441 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  b.c ************
20442 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20443 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20444 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
20445 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
20446 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   where.c *******
20447 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20448 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20449 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
2044a 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
2044b 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2044c 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2044d 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2044e 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2044f 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
20450 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
20451 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
20452 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
20453 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
20454 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
20455 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
20456 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
20457 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
20458 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
20459 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2045a 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2045b 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2045c 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2045d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2045e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2045f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
20461 54 68 69 73 20 6d 6f 64 75 6c 65 20 63 6f 6e 74  This module cont
20462 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61 74  ains C code that
20463 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20   generates VDBE 
20464 63 6f 64 65 20 75 73 65 64 20 74 6f 20 70 72 6f  code used to pro
20465 63 65 73 73 0a 2a 2a 20 74 68 65 20 57 48 45 52  cess.** the WHER
20466 45 20 63 6c 61 75 73 65 20 6f 66 20 53 51 4c 20  E clause of SQL 
20467 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 69  statements.  Thi
20468 73 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 70 6f  s module is repo
20469 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 67 65  nsible for.** ge
2046a 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64  nerating the cod
2046b 65 20 74 68 61 74 20 6c 6f 6f 70 73 20 74 68 72  e that loops thr
2046c 6f 75 67 68 20 61 20 74 61 62 6c 65 20 6c 6f 6f  ough a table loo
2046d 6b 69 6e 67 20 66 6f 72 20 61 70 70 6c 69 63 61  king for applica
2046e 62 6c 65 0a 2a 2a 20 72 6f 77 73 2e 20 20 49 6e  ble.** rows.  In
2046f 64 69 63 65 73 20 61 72 65 20 73 65 6c 65 63 74  dices are select
20470 65 64 20 61 6e 64 20 75 73 65 64 20 74 6f 20 73  ed and used to s
20471 70 65 65 64 20 74 68 65 20 73 65 61 72 63 68 20  peed the search 
20472 77 68 65 6e 20 64 6f 69 6e 67 0a 2a 2a 20 73 6f  when doing.** so
20473 20 69 73 20 61 70 70 6c 69 63 61 62 6c 65 2e 20   is applicable. 
20474 20 42 65 63 61 75 73 65 20 74 68 69 73 20 6d 6f   Because this mo
20475 64 75 6c 65 20 69 73 20 72 65 73 70 6f 6e 73 69  dule is responsi
20476 62 6c 65 20 66 6f 72 20 73 65 6c 65 63 74 69 6e  ble for selectin
20477 67 0a 2a 2a 20 69 6e 64 69 63 65 73 2c 20 79 6f  g.** indices, yo
20478 75 20 6d 69 67 68 74 20 61 6c 73 6f 20 74 68 69  u might also thi
20479 6e 6b 20 6f 66 20 74 68 69 73 20 6d 6f 64 75 6c  nk of this modul
2047a 65 20 61 73 20 74 68 65 20 22 71 75 65 72 79 20  e as the "query 
2047b 6f 70 74 69 6d 69 7a 65 72 22 2e 0a 2a 2a 0a 2a  optimizer"..**.*
2047c 2a 20 24 49 64 3a 20 77 68 65 72 65 2e 63 2c 76  * $Id: where.c,v
2047d 20 31 2e 32 36 31 20 32 30 30 37 2f 30 39 2f 31   1.261 2007/09/1
2047e 33 20 31 37 3a 35 34 3a 34 30 20 64 72 68 20 45  3 17:54:40 drh E
2047f 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
20480 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74  he number of bit
20481 73 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20  s in a Bitmask. 
20482 20 22 42 4d 53 22 20 6d 65 61 6e 73 20 22 42 69   "BMS" means "Bi
20483 74 4d 61 73 6b 20 53 69 7a 65 22 2e 0a 2a 2f 0a  tMask Size"..*/.
20484 23 64 65 66 69 6e 65 20 42 4d 53 20 20 28 73 69  #define BMS  (si
20485 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 29  zeof(Bitmask)*8)
20486 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f 75  ../*.** Trace ou
20487 74 70 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a 23  tput macros.*/.#
20488 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
20489 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e  E_TEST) || defin
2048a 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
2048b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2048c 73 71 6c 69 74 65 33 5f 77 68 65 72 65 5f 74 72  sqlite3_where_tr
2048d 61 63 65 20 3d 20 30 3b 0a 23 20 64 65 66 69 6e  ace = 0;.# defin
2048e 65 20 57 48 45 52 45 54 52 41 43 45 28 58 29 20  e WHERETRACE(X) 
2048f 20 69 66 28 73 71 6c 69 74 65 33 5f 77 68 65 72   if(sqlite3_wher
20490 65 5f 74 72 61 63 65 29 20 73 71 6c 69 74 65 33  e_trace) sqlite3
20491 44 65 62 75 67 50 72 69 6e 74 66 20 58 0a 23 65  DebugPrintf X.#e
20492 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 57 48 45  lse.# define WHE
20493 52 45 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69  RETRACE(X).#endi
20494 66 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  f../* Forward re
20495 66 65 72 65 6e 63 65 0a 2a 2f 0a 74 79 70 65 64  ference.*/.typed
20496 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 43  ef struct WhereC
20497 6c 61 75 73 65 20 57 68 65 72 65 43 6c 61 75 73  lause WhereClaus
20498 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
20499 74 20 45 78 70 72 4d 61 73 6b 53 65 74 20 45 78  t ExprMaskSet Ex
2049a 70 72 4d 61 73 6b 53 65 74 3b 0a 0a 2f 2a 0a 2a  prMaskSet;../*.*
2049b 2a 20 54 68 65 20 71 75 65 72 79 20 67 65 6e 65  * The query gene
2049c 72 61 74 6f 72 20 75 73 65 73 20 61 6e 20 61 72  rator uses an ar
2049d 72 61 79 20 6f 66 20 69 6e 73 74 61 6e 63 65 73  ray of instances
2049e 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
2049f 72 65 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 69 74  re to.** help it
204a0 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 73 75 62   analyze the sub
204a1 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  expressions of t
204a2 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
204a3 20 20 45 61 63 68 20 57 48 45 52 45 0a 2a 2a 20    Each WHERE.** 
204a4 63 6c 61 75 73 65 20 73 75 62 65 78 70 72 65 73  clause subexpres
204a5 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65  sion is separate
204a6 64 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72  d from the other
204a7 73 20 62 79 20 61 6e 20 41 4e 44 20 6f 70 65 72  s by an AND oper
204a8 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  ator..**.** All 
204a9 57 68 65 72 65 54 65 72 6d 73 20 61 72 65 20 63  WhereTerms are c
204aa 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20  ollected into a 
204ab 73 69 6e 67 6c 65 20 57 68 65 72 65 43 6c 61 75  single WhereClau
204ac 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 0a  se structure.  .
204ad 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
204ae 20 69 64 65 6e 74 69 74 79 20 68 6f 6c 64 73 3a   identity holds:
204af 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 57 68  .**.**        Wh
204b0 65 72 65 54 65 72 6d 2e 70 57 43 2d 3e 61 5b 57  ereTerm.pWC->a[W
204b1 68 65 72 65 54 65 72 6d 2e 69 64 78 5d 20 3d 3d  hereTerm.idx] ==
204b2 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a 0a 2a 2a   WhereTerm.**.**
204b3 20 57 68 65 6e 20 61 20 74 65 72 6d 20 69 73 20   When a term is 
204b4 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
204b5 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
204b6 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 0a 2a 2a  X <op> <expr>.**
204b7 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61  .** where X is a
204b8 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 6e 64   column name and
204b9 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66 20   <op> is one of 
204ba 63 65 72 74 61 69 6e 20 6f 70 65 72 61 74 6f 72  certain operator
204bb 73 2c 0a 2a 2a 20 74 68 65 6e 20 57 68 65 72 65  s,.** then Where
204bc 54 65 72 6d 2e 6c 65 66 74 43 75 72 73 6f 72 20  Term.leftCursor 
204bd 61 6e 64 20 57 68 65 72 65 54 65 72 6d 2e 6c 65  and WhereTerm.le
204be 66 74 43 6f 6c 75 6d 6e 20 72 65 63 6f 72 64 20  ftColumn record 
204bf 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75  the.** cursor nu
204c0 6d 62 65 72 20 61 6e 64 20 63 6f 6c 75 6d 6e 20  mber and column 
204c1 6e 75 6d 62 65 72 20 66 6f 72 20 58 2e 20 20 57  number for X.  W
204c2 68 65 72 65 54 65 72 6d 2e 6f 70 65 72 61 74 6f  hereTerm.operato
204c3 72 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65  r records.** the
204c4 20 3c 6f 70 3e 20 75 73 69 6e 67 20 61 20 62 69   <op> using a bi
204c5 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 64  tmask encoding d
204c6 65 66 69 6e 65 64 20 62 79 20 57 4f 5f 78 78 78  efined by WO_xxx
204c7 20 62 65 6c 6f 77 2e 20 20 54 68 65 0a 2a 2a 20   below.  The.** 
204c8 75 73 65 20 6f 66 20 61 20 62 69 74 6d 61 73 6b  use of a bitmask
204c9 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68   encoding for th
204ca 65 20 6f 70 65 72 61 74 6f 72 20 61 6c 6c 6f 77  e operator allow
204cb 73 20 75 73 20 74 6f 20 73 65 61 72 63 68 0a 2a  s us to search.*
204cc 2a 20 71 75 69 63 6b 6c 79 20 66 6f 72 20 74 65  * quickly for te
204cd 72 6d 73 20 74 68 61 74 20 6d 61 74 63 68 20 61  rms that match a
204ce 6e 79 20 6f 66 20 73 65 76 65 72 61 6c 20 64 69  ny of several di
204cf 66 66 65 72 65 6e 74 20 6f 70 65 72 61 74 6f 72  fferent operator
204d0 73 2e 0a 2a 2a 0a 2a 2a 20 70 72 65 72 65 71 52  s..**.** prereqR
204d1 69 67 68 74 20 61 6e 64 20 70 72 65 72 65 71 41  ight and prereqA
204d2 6c 6c 20 72 65 63 6f 72 64 20 73 65 74 73 20 6f  ll record sets o
204d3 66 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  f cursor numbers
204d4 2c 0a 2a 2a 20 62 75 74 20 74 68 65 79 20 64 6f  ,.** but they do
204d5 20 73 6f 20 69 6e 64 69 72 65 63 74 6c 79 2e 20   so indirectly. 
204d6 20 41 20 73 69 6e 67 6c 65 20 45 78 70 72 4d 61   A single ExprMa
204d7 73 6b 53 65 74 20 73 74 72 75 63 74 75 72 65 20  skSet structure 
204d8 74 72 61 6e 73 6c 61 74 65 73 0a 2a 2a 20 63 75  translates.** cu
204d9 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 6e 74 6f  rsor number into
204da 20 62 69 74 73 20 61 6e 64 20 74 68 65 20 74 72   bits and the tr
204db 61 6e 73 6c 61 74 65 64 20 62 69 74 20 69 73 20  anslated bit is 
204dc 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70 72  stored in the pr
204dd 65 72 65 71 0a 2a 2a 20 66 69 65 6c 64 73 2e 20  ereq.** fields. 
204de 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e   The translation
204df 20 69 73 20 75 73 65 64 20 69 6e 20 6f 72 64 65   is used in orde
204e0 72 20 74 6f 20 6d 61 78 69 6d 69 7a 65 20 74 68  r to maximize th
204e1 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62  e number of.** b
204e2 69 74 73 20 74 68 61 74 20 77 69 6c 6c 20 66 69  its that will fi
204e3 74 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20  t in a Bitmask. 
204e4 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
204e5 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62   numbers might b
204e6 65 0a 2a 2a 20 73 70 72 65 61 64 20 6f 75 74 20  e.** spread out 
204e7 6f 76 65 72 20 74 68 65 20 6e 6f 6e 2d 6e 65 67  over the non-neg
204e8 61 74 69 76 65 20 69 6e 74 65 67 65 72 73 2e 20  ative integers. 
204e9 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68   For example, th
204ea 65 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62  e cursor.** numb
204eb 65 72 73 20 6d 69 67 68 74 20 62 65 20 33 2c 20  ers might be 3, 
204ec 38 2c 20 39 2c 20 31 30 2c 20 32 30 2c 20 32 33  8, 9, 10, 20, 23
204ed 2c 20 34 31 2c 20 61 6e 64 20 34 35 2e 20 20 54  , 41, and 45.  T
204ee 68 65 20 45 78 70 72 4d 61 73 6b 53 65 74 0a 2a  he ExprMaskSet.*
204ef 2a 20 74 72 61 6e 73 6c 61 74 65 73 20 74 68 65  * translates the
204f0 73 65 20 73 70 61 72 73 65 20 63 75 72 73 6f 72  se sparse cursor
204f1 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f   numbers into co
204f2 6e 73 65 63 75 74 69 76 65 20 69 6e 74 65 67 65  nsecutive intege
204f3 72 73 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20  rs.** beginning 
204f4 77 69 74 68 20 30 20 69 6e 20 6f 72 64 65 72 20  with 0 in order 
204f5 74 6f 20 6d 61 6b 65 20 74 68 65 20 62 65 73 74  to make the best
204f6 20 70 6f 73 73 69 62 6c 65 20 75 73 65 20 6f 66   possible use of
204f7 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a   the available.*
204f8 2a 20 62 69 74 73 20 69 6e 20 74 68 65 20 42 69  * bits in the Bi
204f9 74 6d 61 73 6b 2e 20 20 53 6f 2c 20 69 6e 20 74  tmask.  So, in t
204fa 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65  he example above
204fb 2c 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  , the cursor num
204fc 62 65 72 73 0a 2a 2a 20 77 6f 75 6c 64 20 62 65  bers.** would be
204fd 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 69 6e 74   mapped into int
204fe 65 67 65 72 73 20 30 20 74 68 72 6f 75 67 68 20  egers 0 through 
204ff 37 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  7..*/.typedef st
20500 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20 57  ruct WhereTerm W
20501 68 65 72 65 54 65 72 6d 3b 0a 73 74 72 75 63 74  hereTerm;.struct
20502 20 57 68 65 72 65 54 65 72 6d 20 7b 0a 20 20 45   WhereTerm {.  E
20503 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20  xpr *pExpr;     
20504 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
20505 72 20 74 6f 20 74 68 65 20 73 75 62 65 78 70 72  r to the subexpr
20506 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 31 36 20  ession */.  i16 
20507 69 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20  iParent;        
20508 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 70      /* Disable p
20509 57 43 2d 3e 61 5b 69 50 61 72 65 6e 74 5d 20 77  WC->a[iParent] w
2050a 68 65 6e 20 74 68 69 73 20 74 65 72 6d 20 64 69  hen this term di
2050b 73 61 62 6c 65 64 20 2a 2f 0a 20 20 69 31 36 20  sabled */.  i16 
2050c 6c 65 66 74 43 75 72 73 6f 72 3b 20 20 20 20 20  leftCursor;     
2050d 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
2050e 6d 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58 20  mber of X in "X 
2050f 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a  <op> <expr>" */.
20510 20 20 69 31 36 20 6c 65 66 74 43 6f 6c 75 6d 6e    i16 leftColumn
20511 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ;         /* Col
20512 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 20 58 20  umn number of X 
20513 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72  in "X <op> <expr
20514 3e 22 20 2a 2f 0a 20 20 75 31 36 20 65 4f 70 65  >" */.  u16 eOpe
20515 72 61 74 6f 72 3b 20 20 20 20 20 20 20 20 20 20  rator;          
20516 2f 2a 20 41 20 57 4f 5f 78 78 20 76 61 6c 75 65  /* A WO_xx value
20517 20 64 65 73 63 72 69 62 69 6e 67 20 3c 6f 70 3e   describing <op>
20518 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 3b 20   */.  u8 flags; 
20519 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2051a 20 42 69 74 20 66 6c 61 67 73 2e 20 20 53 65 65   Bit flags.  See
2051b 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 38 20 6e   below */.  u8 n
2051c 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20 20  Child;          
2051d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2051e 20 63 68 69 6c 64 72 65 6e 20 74 68 61 74 20 6d   children that m
2051f 75 73 74 20 64 69 73 61 62 6c 65 20 75 73 20 2a  ust disable us *
20520 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
20521 2a 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 54  *pWC;       /* T
20522 68 65 20 63 6c 61 75 73 65 20 74 68 69 73 20 74  he clause this t
20523 65 72 6d 20 69 73 20 70 61 72 74 20 6f 66 20 2a  erm is part of *
20524 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72  /.  Bitmask prer
20525 65 71 52 69 67 68 74 3b 20 20 20 20 2f 2a 20 42  eqRight;    /* B
20526 69 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c 65 73  itmask of tables
20527 20 75 73 65 64 20 62 79 20 70 52 69 67 68 74 20   used by pRight 
20528 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65  */.  Bitmask pre
20529 72 65 71 41 6c 6c 3b 20 20 20 20 20 20 2f 2a 20  reqAll;      /* 
2052a 42 69 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c 65  Bitmask of table
2052b 73 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20  s referenced by 
2052c 70 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  p */.};../*.** A
2052d 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66  llowed values of
2052e 20 57 68 65 72 65 54 65 72 6d 2e 66 6c 61 67 73   WhereTerm.flags
2052f 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d  .*/.#define TERM
20530 5f 44 59 4e 41 4d 49 43 20 20 20 20 30 78 30 31  _DYNAMIC    0x01
20531 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61     /* Need to ca
20532 6c 6c 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  ll sqlite3ExprDe
20533 6c 65 74 65 28 70 45 78 70 72 29 20 2a 2f 0a 23  lete(pExpr) */.#
20534 64 65 66 69 6e 65 20 54 45 52 4d 5f 56 49 52 54  define TERM_VIRT
20535 55 41 4c 20 20 20 20 30 78 30 32 20 20 20 2f 2a  UAL    0x02   /*
20536 20 41 64 64 65 64 20 62 79 20 74 68 65 20 6f 70   Added by the op
20537 74 69 6d 69 7a 65 72 2e 20 20 44 6f 20 6e 6f 74  timizer.  Do not
20538 20 63 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65   code */.#define
20539 20 54 45 52 4d 5f 43 4f 44 45 44 20 20 20 20 20   TERM_CODED     
2053a 20 30 78 30 34 20 20 20 2f 2a 20 54 68 69 73 20   0x04   /* This 
2053b 74 65 72 6d 20 69 73 20 61 6c 72 65 61 64 79 20  term is already 
2053c 63 6f 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  coded */.#define
2053d 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 20 20 20   TERM_COPIED    
2053e 20 30 78 30 38 20 20 20 2f 2a 20 48 61 73 20 61   0x08   /* Has a
2053f 20 63 68 69 6c 64 20 2a 2f 0a 23 64 65 66 69 6e   child */.#defin
20540 65 20 54 45 52 4d 5f 4f 52 5f 4f 4b 20 20 20 20  e TERM_OR_OK    
20541 20 20 30 78 31 30 20 20 20 2f 2a 20 55 73 65 64    0x10   /* Used
20542 20 64 75 72 69 6e 67 20 4f 52 2d 63 6c 61 75 73   during OR-claus
20543 65 20 70 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a  e processing */.
20544 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
20545 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
20546 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f  ing structure ho
20547 6c 64 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74  lds all informat
20548 69 6f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20 57  ion about a.** W
20549 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 6f  HERE clause.  Mo
2054a 73 74 6c 79 20 74 68 69 73 20 69 73 20 61 20 63  stly this is a c
2054b 6f 6e 74 61 69 6e 65 72 20 66 6f 72 20 6f 6e 65  ontainer for one
2054c 20 6f 72 20 6d 6f 72 65 20 57 68 65 72 65 54 65   or more WhereTe
2054d 72 6d 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  rms..*/.struct W
2054e 68 65 72 65 43 6c 61 75 73 65 20 7b 0a 20 20 50  hereClause {.  P
2054f 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
20550 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
20551 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
20552 0a 20 20 45 78 70 72 4d 61 73 6b 53 65 74 20 2a  .  ExprMaskSet *
20553 70 4d 61 73 6b 53 65 74 3b 20 20 20 2f 2a 20 4d  pMaskSet;   /* M
20554 61 70 70 69 6e 67 20 6f 66 20 74 61 62 6c 65 20  apping of table 
20555 69 6e 64 69 63 65 73 20 74 6f 20 62 69 74 6d 61  indices to bitma
20556 73 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65  sks */.  int nTe
20557 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
20558 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
20559 65 72 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  erms */.  int nS
2055a 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  lot;            
2055b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2055c 65 6e 74 72 69 65 73 20 69 6e 20 61 5b 5d 20 2a  entries in a[] *
2055d 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61  /.  WhereTerm *a
2055e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2055f 45 61 63 68 20 61 5b 5d 20 64 65 73 63 72 69 62  Each a[] describ
20560 65 73 20 61 20 74 65 72 6d 20 6f 66 20 74 68 65  es a term of the
20561 20 57 48 45 52 45 20 63 6c 75 61 73 65 20 2a 2f   WHERE cluase */
20562 0a 20 20 57 68 65 72 65 54 65 72 6d 20 61 53 74  .  WhereTerm aSt
20563 61 74 69 63 5b 31 30 5d 3b 20 20 20 2f 2a 20 49  atic[10];   /* I
20564 6e 69 74 69 61 6c 20 73 74 61 74 69 63 20 73 70  nitial static sp
20565 61 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 7d  ace for a[] */.}
20566 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
20567 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
20568 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
20569 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 61  keeps track of a
2056a 20 6d 61 70 70 69 6e 67 0a 2a 2a 20 62 65 74 77   mapping.** betw
2056b 65 65 6e 20 56 44 42 45 20 63 75 72 73 6f 72 20  een VDBE cursor 
2056c 6e 75 6d 62 65 72 73 20 61 6e 64 20 62 69 74 73  numbers and bits
2056d 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b 73   of the bitmasks
2056e 20 69 6e 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a   in WhereTerm..*
2056f 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 63 75  *.** The VDBE cu
20570 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65  rsor numbers are
20571 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 20   small integers 
20572 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 0a 2a 2a  contained in .**
20573 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 2e 69 43   SrcList_item.iC
20574 75 72 73 6f 72 20 61 6e 64 20 45 78 70 72 2e 69  ursor and Expr.i
20575 54 61 62 6c 65 20 66 69 65 6c 64 73 2e 20 20 46  Table fields.  F
20576 6f 72 20 61 6e 79 20 67 69 76 65 6e 20 57 48 45  or any given WHE
20577 52 45 20 0a 2a 2a 20 63 6c 61 75 73 65 2c 20 74  RE .** clause, t
20578 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
20579 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 67 69  s might not begi
2057a 6e 20 77 69 74 68 20 30 20 61 6e 64 20 74 68 65  n with 0 and the
2057b 79 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6e 74 61  y might.** conta
2057c 69 6e 20 67 61 70 73 20 69 6e 20 74 68 65 20 6e  in gaps in the n
2057d 75 6d 62 65 72 69 6e 67 20 73 65 71 75 65 6e 63  umbering sequenc
2057e 65 2e 20 20 42 75 74 20 77 65 20 77 61 6e 74 20  e.  But we want 
2057f 74 6f 20 6d 61 6b 65 20 6d 61 78 69 6d 75 6d 0a  to make maximum.
20580 2a 2a 20 75 73 65 20 6f 66 20 74 68 65 20 62 69  ** use of the bi
20581 74 73 20 69 6e 20 6f 75 72 20 62 69 74 6d 61 73  ts in our bitmas
20582 6b 73 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  ks.  This struct
20583 75 72 65 20 70 72 6f 76 69 64 65 73 20 61 20 6d  ure provides a m
20584 61 70 70 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 74  apping.** from t
20585 68 65 20 73 70 61 72 73 65 20 63 75 72 73 6f 72  he sparse cursor
20586 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f   numbers into co
20587 6e 73 65 63 75 74 69 76 65 20 69 6e 74 65 67 65  nsecutive intege
20588 72 73 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20  rs beginning.** 
20589 77 69 74 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66  with 0..**.** If
2058a 20 45 78 70 72 4d 61 73 6b 53 65 74 2e 69 78 5b   ExprMaskSet.ix[
2058b 41 5d 3d 3d 42 20 69 74 20 6d 65 61 6e 73 20 74  A]==B it means t
2058c 68 61 74 20 54 68 65 20 41 2d 74 68 20 62 69 74  hat The A-th bit
2058d 20 6f 66 20 61 20 42 69 74 6d 61 73 6b 0a 2a 2a   of a Bitmask.**
2058e 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 56 44 42   corresponds VDB
2058f 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
20590 42 2e 20 20 54 68 65 20 41 2d 74 68 20 62 69 74  B.  The A-th bit
20591 20 6f 66 20 61 20 62 69 74 6d 61 73 6b 20 69 73   of a bitmask is
20592 20 31 3c 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72   1<<A..**.** For
20593 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
20594 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78   WHERE clause ex
20595 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 68  pression used th
20596 65 73 65 20 56 44 42 45 0a 2a 2a 20 63 75 72 73  ese VDBE.** curs
20597 6f 72 73 3a 20 20 34 2c 20 35 2c 20 38 2c 20 32  ors:  4, 5, 8, 2
20598 39 2c 20 35 37 2c 20 37 33 2e 20 20 54 68 65 6e  9, 57, 73.  Then
20599 20 74 68 65 20 20 45 78 70 72 4d 61 73 6b 53 65   the  ExprMaskSe
2059a 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77  t structure.** w
2059b 6f 75 6c 64 20 6d 61 70 20 74 68 6f 73 65 20 63  ould map those c
2059c 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
2059d 74 6f 20 62 69 74 73 20 30 20 74 68 72 6f 75 67  to bits 0 throug
2059e 68 20 35 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  h 5..**.** Note 
2059f 74 68 61 74 20 74 68 65 20 6d 61 70 70 69 6e 67  that the mapping
205a0 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
205a1 69 6c 79 20 6f 72 64 65 72 65 64 2e 20 20 49 6e  ily ordered.  In
205a2 20 74 68 65 20 65 78 61 6d 70 6c 65 0a 2a 2a 20   the example.** 
205a3 61 62 6f 76 65 2c 20 74 68 65 20 6d 61 70 70 69  above, the mappi
205a4 6e 67 20 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65  ng might go like
205a5 20 74 68 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d   this:  4->3, 5-
205a6 3e 31 2c 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c  >1, 8->2, 29->0,
205a7 0a 2a 2a 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34  .** 57->5, 73->4
205a8 2e 20 20 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39  .  Or one of 719
205a9 20 6f 74 68 65 72 20 63 6f 6d 62 69 6e 61 74 69   other combinati
205aa 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 75 73 65  ons might be use
205ab 64 2e 20 49 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f  d. It.** does no
205ac 74 20 72 65 61 6c 6c 79 20 6d 61 74 74 65 72 2e  t really matter.
205ad 20 20 57 68 61 74 20 69 73 20 69 6d 70 6f 72 74    What is import
205ae 61 6e 74 20 69 73 20 74 68 61 74 20 73 70 61 72  ant is that spar
205af 73 65 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d  se cursor.** num
205b0 62 65 72 73 20 61 6c 6c 20 67 65 74 20 6d 61 70  bers all get map
205b1 70 65 64 20 69 6e 74 6f 20 62 69 74 20 6e 75 6d  ped into bit num
205b2 62 65 72 73 20 74 68 61 74 20 62 65 67 69 6e 20  bers that begin 
205b3 77 69 74 68 20 30 20 61 6e 64 20 63 6f 6e 74 61  with 0 and conta
205b4 69 6e 0a 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a 2a  in.** no gaps..*
205b5 2f 0a 73 74 72 75 63 74 20 45 78 70 72 4d 61 73  /.struct ExprMas
205b6 6b 53 65 74 20 7b 0a 20 20 69 6e 74 20 6e 3b 20  kSet {.  int n; 
205b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
205b8 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
205b9 20 6f 66 20 61 73 73 69 67 6e 65 64 20 63 75 72   of assigned cur
205ba 73 6f 72 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  sor values */.  
205bb 69 6e 74 20 69 78 5b 73 69 7a 65 6f 66 28 42 69  int ix[sizeof(Bi
205bc 74 6d 61 73 6b 29 2a 38 5d 3b 20 20 20 20 2f 2a  tmask)*8];    /*
205bd 20 43 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64   Cursor assigned
205be 20 74 6f 20 65 61 63 68 20 62 69 74 20 2a 2f 0a   to each bit */.
205bf 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 6d 61  };.../*.** Bitma
205c0 73 6b 73 20 66 6f 72 20 74 68 65 20 6f 70 65 72  sks for the oper
205c1 61 74 6f 72 73 20 74 68 61 74 20 69 6e 64 69 63  ators that indic
205c2 65 73 20 61 72 65 20 61 62 6c 65 20 74 6f 20 65  es are able to e
205c3 78 70 6c 6f 69 74 2e 20 20 41 6e 0a 2a 2a 20 4f  xploit.  An.** O
205c4 52 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  R-ed combination
205c5 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75 65 73   of these values
205c6 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 68 65   can be used whe
205c7 6e 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 0a  n searching for.
205c8 2a 2a 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  ** terms in the 
205c9 77 68 65 72 65 20 63 6c 61 75 73 65 2e 0a 2a 2f  where clause..*/
205ca 0a 23 64 65 66 69 6e 65 20 57 4f 5f 49 4e 20 20  .#define WO_IN  
205cb 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 4f 5f     1.#define WO_
205cc 45 51 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65  EQ     2.#define
205cd 20 57 4f 5f 4c 54 20 20 20 20 20 28 57 4f 5f 45   WO_LT     (WO_E
205ce 51 3c 3c 28 54 4b 5f 4c 54 2d 54 4b 5f 45 51 29  Q<<(TK_LT-TK_EQ)
205cf 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c 45 20  ).#define WO_LE 
205d0 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f      (WO_EQ<<(TK_
205d1 4c 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69  LE-TK_EQ)).#defi
205d2 6e 65 20 57 4f 5f 47 54 20 20 20 20 20 28 57 4f  ne WO_GT     (WO
205d3 5f 45 51 3c 3c 28 54 4b 5f 47 54 2d 54 4b 5f 45  _EQ<<(TK_GT-TK_E
205d4 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47  Q)).#define WO_G
205d5 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54  E     (WO_EQ<<(T
205d6 4b 5f 47 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65  K_GE-TK_EQ)).#de
205d7 66 69 6e 65 20 57 4f 5f 4d 41 54 43 48 20 20 36  fine WO_MATCH  6
205d8 34 0a 23 64 65 66 69 6e 65 20 57 4f 5f 49 53 4e  4.#define WO_ISN
205d9 55 4c 4c 20 31 32 38 0a 0a 2f 2a 0a 2a 2a 20 56  ULL 128../*.** V
205da 61 6c 75 65 20 66 6f 72 20 66 6c 61 67 73 20 72  alue for flags r
205db 65 74 75 72 6e 65 64 20 62 79 20 62 65 73 74 49  eturned by bestI
205dc 6e 64 65 78 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20  ndex().  .**.** 
205dd 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66  The least signif
205de 69 63 61 6e 74 20 62 79 74 65 20 69 73 20 72 65  icant byte is re
205df 73 65 72 76 65 64 20 61 73 20 61 20 6d 61 73 6b  served as a mask
205e0 20 66 6f 72 20 57 4f 5f 20 76 61 6c 75 65 73 20   for WO_ values 
205e1 61 62 6f 76 65 2e 0a 2a 2a 20 54 68 65 20 57 68  above..** The Wh
205e2 65 72 65 4c 65 76 65 6c 2e 66 6c 61 67 73 20 66  ereLevel.flags f
205e3 69 65 6c 64 20 69 73 20 75 73 75 61 6c 6c 79 20  ield is usually 
205e4 73 65 74 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f  set to WO_IN|WO_
205e5 45 51 7c 57 4f 5f 49 53 4e 55 4c 4c 2e 0a 2a 2a  EQ|WO_ISNULL..**
205e6 20 42 75 74 20 69 66 20 74 68 65 20 74 61 62 6c   But if the tabl
205e7 65 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74  e is the right t
205e8 61 62 6c 65 20 6f 66 20 61 20 6c 65 66 74 20 6a  able of a left j
205e9 6f 69 6e 2c 20 57 68 65 72 65 4c 65 76 65 6c 2e  oin, WhereLevel.
205ea 66 6c 61 67 73 0a 2a 2a 20 69 73 20 73 65 74 20  flags.** is set 
205eb 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 2e 20  to WO_IN|WO_EQ. 
205ec 20 54 68 65 20 57 68 65 72 65 4c 65 76 65 6c 2e   The WhereLevel.
205ed 66 6c 61 67 73 20 66 69 65 6c 64 20 63 61 6e 20  flags field can 
205ee 74 68 65 6e 20 62 65 20 75 73 65 64 20 61 73 0a  then be used as.
205ef 2a 2a 20 74 68 65 20 22 6f 70 22 20 70 61 72 61  ** the "op" para
205f0 6d 65 74 65 72 20 74 6f 20 66 69 6e 64 54 65 72  meter to findTer
205f1 6d 20 77 68 65 6e 20 77 65 20 61 72 65 20 72 65  m when we are re
205f2 73 6f 6c 76 69 6e 67 20 65 71 75 61 6c 69 74 79  solving equality
205f3 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a   constraints..**
205f4 20 49 53 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69   ISNULL constrai
205f5 6e 74 73 20 77 69 6c 6c 20 74 68 65 6e 20 6e 6f  nts will then no
205f6 74 20 62 65 20 75 73 65 64 20 6f 6e 20 74 68 65  t be used on the
205f7 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
205f8 61 20 6c 65 66 74 0a 2a 2a 20 6a 6f 69 6e 2e 20  a left.** join. 
205f9 20 54 69 63 6b 65 74 73 20 23 32 31 37 37 20 61   Tickets #2177 a
205fa 6e 64 20 23 32 31 38 39 2e 0a 2a 2f 0a 23 64 65  nd #2189..*/.#de
205fb 66 69 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44  fine WHERE_ROWID
205fc 5f 45 51 20 20 20 20 20 30 78 30 30 30 31 30 30  _EQ     0x000100
205fd 20 20 20 2f 2a 20 72 6f 77 69 64 3d 45 58 50 52     /* rowid=EXPR
205fe 20 6f 72 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e   or rowid IN (..
205ff 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  .) */.#define WH
20600 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20  ERE_ROWID_RANGE 
20601 20 30 78 30 30 30 32 30 30 20 20 20 2f 2a 20 72   0x000200   /* r
20602 6f 77 69 64 3c 45 58 50 52 20 61 6e 64 2f 6f 72  owid<EXPR and/or
20603 20 72 6f 77 69 64 3e 45 58 50 52 20 2a 2f 0a 23   rowid>EXPR */.#
20604 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c  define WHERE_COL
20605 55 4d 4e 5f 45 51 20 20 20 20 30 78 30 30 31 30  UMN_EQ    0x0010
20606 30 30 20 20 20 2f 2a 20 78 3d 45 58 50 52 20 6f  00   /* x=EXPR o
20607 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a  r x IN (...) */.
20608 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f  #define WHERE_CO
20609 4c 55 4d 4e 5f 52 41 4e 47 45 20 30 78 30 30 32  LUMN_RANGE 0x002
2060a 30 30 30 20 20 20 2f 2a 20 78 3c 45 58 50 52 20  000   /* x<EXPR 
2060b 61 6e 64 2f 6f 72 20 78 3e 45 58 50 52 20 2a 2f  and/or x>EXPR */
2060c 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43  .#define WHERE_C
2060d 4f 4c 55 4d 4e 5f 49 4e 20 20 20 20 30 78 30 30  OLUMN_IN    0x00
2060e 34 30 30 30 20 20 20 2f 2a 20 78 20 49 4e 20 28  4000   /* x IN (
2060f 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ...) */.#define 
20610 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20  WHERE_TOP_LIMIT 
20611 20 20 20 30 78 30 31 30 30 30 30 20 20 20 2f 2a     0x010000   /*
20612 20 78 3c 45 58 50 52 20 6f 72 20 78 3c 3d 45 58   x<EXPR or x<=EX
20613 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f  PR constraint */
20614 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 42  .#define WHERE_B
20615 54 4d 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 32  TM_LIMIT    0x02
20616 30 30 30 30 20 20 20 2f 2a 20 78 3e 45 58 50 52  0000   /* x>EXPR
20617 20 6f 72 20 78 3e 3d 45 58 50 52 20 63 6f 6e 73   or x>=EXPR cons
20618 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  traint */.#defin
20619 65 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  e WHERE_IDX_ONLY
2061a 20 20 20 20 20 30 78 30 38 30 30 30 30 20 20 20       0x080000   
2061b 2f 2a 20 55 73 65 20 69 6e 64 65 78 20 6f 6e 6c  /* Use index onl
2061c 79 20 2d 20 6f 6d 69 74 20 74 61 62 6c 65 20 2a  y - omit table *
2061d 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2061e 4f 52 44 45 52 42 59 20 20 20 20 20 20 30 78 31  ORDERBY      0x1
2061f 30 30 30 30 30 20 20 20 2f 2a 20 4f 75 74 70 75  00000   /* Outpu
20620 74 20 77 69 6c 6c 20 61 70 70 65 61 72 20 69 6e  t will appear in
20621 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 2a   correct order *
20622 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
20623 52 45 56 45 52 53 45 20 20 20 20 20 20 30 78 32  REVERSE      0x2
20624 30 30 30 30 30 20 20 20 2f 2a 20 53 63 61 6e 20  00000   /* Scan 
20625 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
20626 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
20627 45 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 30  E_UNIQUE       0
20628 78 34 30 30 30 30 30 20 20 20 2f 2a 20 53 65 6c  x400000   /* Sel
20629 65 63 74 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61  ects no more tha
2062a 6e 20 6f 6e 65 20 72 6f 77 20 2a 2f 0a 23 64 65  n one row */.#de
2062b 66 69 6e 65 20 57 48 45 52 45 5f 56 49 52 54 55  fine WHERE_VIRTU
2062c 41 4c 54 41 42 4c 45 20 30 78 38 30 30 30 30 30  ALTABLE 0x800000
2062d 20 20 20 2f 2a 20 55 73 65 20 76 69 72 74 75 61     /* Use virtua
2062e 6c 2d 74 61 62 6c 65 20 70 72 6f 63 65 73 73 69  l-table processi
2062f 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  ng */../*.** Ini
20630 74 69 61 6c 69 7a 65 20 61 20 70 72 65 61 6c 6c  tialize a preall
20631 6f 63 61 74 65 64 20 57 68 65 72 65 43 6c 61 75  ocated WhereClau
20632 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  se structure..*/
20633 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65  .static void whe
20634 72 65 43 6c 61 75 73 65 49 6e 69 74 28 0a 20 20  reClauseInit(.  
20635 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
20636 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
20637 57 68 65 72 65 43 6c 61 75 73 65 20 74 6f 20 62  WhereClause to b
20638 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  e initialized */
20639 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2063a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
2063b 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
2063c 78 74 20 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b  xt */.  ExprMask
2063d 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 20 20 20  Set *pMaskSet   
2063e 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d   /* Mapping from
2063f 20 74 61 62 6c 65 20 69 6e 64 69 63 65 73 20 74   table indices t
20640 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 29 7b  o bitmasks */.){
20641 0a 20 20 70 57 43 2d 3e 70 50 61 72 73 65 20 3d  .  pWC->pParse =
20642 20 70 50 61 72 73 65 3b 0a 20 20 70 57 43 2d 3e   pParse;.  pWC->
20643 70 4d 61 73 6b 53 65 74 20 3d 20 70 4d 61 73 6b  pMaskSet = pMask
20644 53 65 74 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72  Set;.  pWC->nTer
20645 6d 20 3d 20 30 3b 0a 20 20 70 57 43 2d 3e 6e 53  m = 0;.  pWC->nS
20646 6c 6f 74 20 3d 20 41 72 72 61 79 53 69 7a 65 28  lot = ArraySize(
20647 70 57 43 2d 3e 61 53 74 61 74 69 63 29 3b 0a 20  pWC->aStatic);. 
20648 20 70 57 43 2d 3e 61 20 3d 20 70 57 43 2d 3e 61   pWC->a = pWC->a
20649 53 74 61 74 69 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Static;.}../*.**
2064a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 20 57 68   Deallocate a Wh
2064b 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74  ereClause struct
2064c 75 72 65 2e 20 20 54 68 65 20 57 68 65 72 65 43  ure.  The WhereC
2064d 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65 0a  lause structure.
2064e 2a 2a 20 69 74 73 65 6c 66 20 69 73 20 6e 6f 74  ** itself is not
2064f 20 66 72 65 65 64 2e 20 20 54 68 69 73 20 72 6f   freed.  This ro
20650 75 74 69 6e 65 20 69 73 20 74 68 65 20 69 6e 76  utine is the inv
20651 65 72 73 65 20 6f 66 20 77 68 65 72 65 43 6c 61  erse of whereCla
20652 75 73 65 49 6e 69 74 28 29 2e 0a 2a 2f 0a 73 74  useInit()..*/.st
20653 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43  atic void whereC
20654 6c 61 75 73 65 43 6c 65 61 72 28 57 68 65 72 65  lauseClear(Where
20655 43 6c 61 75 73 65 20 2a 70 57 43 29 7b 0a 20 20  Clause *pWC){.  
20656 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 54 65  int i;.  WhereTe
20657 72 6d 20 2a 61 3b 0a 20 20 66 6f 72 28 69 3d 70  rm *a;.  for(i=p
20658 57 43 2d 3e 6e 54 65 72 6d 2d 31 2c 20 61 3d 70  WC->nTerm-1, a=p
20659 57 43 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d  WC->a; i>=0; i--
2065a 2c 20 61 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , a++){.    if( 
2065b 61 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f  a->flags & TERM_
2065c 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20  DYNAMIC ){.     
2065d 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2065e 74 65 28 61 2d 3e 70 45 78 70 72 29 3b 0a 20 20  te(a->pExpr);.  
2065f 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 57    }.  }.  if( pW
20660 43 2d 3e 61 21 3d 70 57 43 2d 3e 61 53 74 61 74  C->a!=pWC->aStat
20661 69 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ic ){.    sqlite
20662 33 5f 66 72 65 65 28 70 57 43 2d 3e 61 29 3b 0a  3_free(pWC->a);.
20663 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
20664 20 61 20 6e 65 77 20 65 6e 74 72 69 65 73 20 74   a new entries t
20665 6f 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73  o the WhereClaus
20666 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 6e  e structure.  In
20667 63 72 65 61 73 65 20 74 68 65 20 61 6c 6c 6f 63  crease the alloc
20668 61 74 65 64 0a 2a 2a 20 73 70 61 63 65 20 61 73  ated.** space as
20669 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a   necessary..**.*
2066a 2a 20 49 66 20 74 68 65 20 66 6c 61 67 73 20 61  * If the flags a
2066b 72 67 75 6d 65 6e 74 20 69 6e 63 6c 75 64 65 73  rgument includes
2066c 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 2c 20 74   TERM_DYNAMIC, t
2066d 68 65 6e 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  hen responsibili
2066e 74 79 0a 2a 2a 20 66 6f 72 20 66 72 65 65 69 6e  ty.** for freein
2066f 67 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  g the expression
20670 20 70 20 69 73 20 61 73 73 75 6d 65 64 20 62 79   p is assumed by
20671 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
20672 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 57   object..**.** W
20673 41 52 4e 49 4e 47 3a 20 20 54 68 69 73 20 72 6f  ARNING:  This ro
20674 75 74 69 6e 65 20 6d 69 67 68 74 20 72 65 61 6c  utine might real
20675 6c 6f 63 61 74 65 20 74 68 65 20 73 70 61 63 65  locate the space
20676 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a   used to store.*
20677 2a 20 57 68 65 72 65 54 65 72 6d 73 2e 20 20 41  * WhereTerms.  A
20678 6c 6c 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 57  ll pointers to W
20679 68 65 72 65 54 65 72 6d 73 20 73 68 6f 75 6c 64  hereTerms should
2067a 20 62 65 20 69 6e 76 61 6c 69 64 65 64 20 61 66   be invalided af
2067b 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74  ter.** calling t
2067c 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 53 75  his routine.  Su
2067d 63 68 20 70 6f 69 6e 74 65 72 73 20 6d 61 79 20  ch pointers may 
2067e 62 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64  be reinitialized
2067f 20 62 79 20 72 65 66 65 72 65 6e 63 69 6e 67 0a   by referencing.
20680 2a 2a 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20  ** the pWC->a[] 
20681 61 72 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  array..*/.static
20682 20 69 6e 74 20 77 68 65 72 65 43 6c 61 75 73 65   int whereClause
20683 49 6e 73 65 72 74 28 57 68 65 72 65 43 6c 61 75  Insert(WhereClau
20684 73 65 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70  se *pWC, Expr *p
20685 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
20686 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
20687 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 69  ;.  int idx;.  i
20688 66 28 20 70 57 43 2d 3e 6e 54 65 72 6d 3e 3d 70  f( pWC->nTerm>=p
20689 57 43 2d 3e 6e 53 6c 6f 74 20 29 7b 0a 20 20 20  WC->nSlot ){.   
2068a 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 6c 64   WhereTerm *pOld
2068b 20 3d 20 70 57 43 2d 3e 61 3b 0a 20 20 20 20 70   = pWC->a;.    p
2068c 57 43 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 5f  WC->a = sqlite3_
2068d 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 70  malloc( sizeof(p
2068e 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e  WC->a[0])*pWC->n
2068f 53 6c 6f 74 2a 32 20 29 3b 0a 20 20 20 20 69 66  Slot*2 );.    if
20690 28 20 70 57 43 2d 3e 61 3d 3d 30 20 29 7b 0a 20  ( pWC->a==0 ){. 
20691 20 20 20 20 20 70 57 43 2d 3e 70 50 61 72 73 65       pWC->pParse
20692 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
20693 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66  ed = 1;.      if
20694 28 20 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 44  ( flags & TERM_D
20695 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20  YNAMIC ){.      
20696 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
20697 65 74 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a  ete(p);.      }.
20698 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
20699 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79      }.    memcpy
2069a 28 70 57 43 2d 3e 61 2c 20 70 4f 6c 64 2c 20 73  (pWC->a, pOld, s
2069b 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29  izeof(pWC->a[0])
2069c 2a 70 57 43 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20  *pWC->nTerm);.  
2069d 20 20 69 66 28 20 70 4f 6c 64 21 3d 70 57 43 2d    if( pOld!=pWC-
2069e 3e 61 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  >aStatic ){.    
2069f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
206a0 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Old);.    }.    
206a1 70 57 43 2d 3e 6e 53 6c 6f 74 20 2a 3d 20 32 3b  pWC->nSlot *= 2;
206a2 0a 20 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26  .  }.  pTerm = &
206a3 70 57 43 2d 3e 61 5b 69 64 78 20 3d 20 70 57 43  pWC->a[idx = pWC
206a4 2d 3e 6e 54 65 72 6d 5d 3b 0a 20 20 70 57 43 2d  ->nTerm];.  pWC-
206a5 3e 6e 54 65 72 6d 2b 2b 3b 0a 20 20 70 54 65 72  >nTerm++;.  pTer
206a6 6d 2d 3e 70 45 78 70 72 20 3d 20 70 3b 0a 20 20  m->pExpr = p;.  
206a7 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 3d 20 66  pTerm->flags = f
206a8 6c 61 67 73 3b 0a 20 20 70 54 65 72 6d 2d 3e 70  lags;.  pTerm->p
206a9 57 43 20 3d 20 70 57 43 3b 0a 20 20 70 54 65 72  WC = pWC;.  pTer
206aa 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b  m->iParent = -1;
206ab 0a 20 20 72 65 74 75 72 6e 20 69 64 78 3b 0a 7d  .  return idx;.}
206ac 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
206ad 74 69 6e 65 20 69 64 65 6e 74 69 66 69 65 73 20  tine identifies 
206ae 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 69  subexpressions i
206af 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
206b0 73 65 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68  se where.** each
206b1 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69   subexpression i
206b2 73 20 73 65 70 61 72 61 74 65 64 20 62 79 20 74  s separated by t
206b3 68 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 20  he AND operator 
206b4 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a  or some other.**
206b5 20 6f 70 65 72 61 74 6f 72 20 73 70 65 63 69 66   operator specif
206b6 69 65 64 20 69 6e 20 74 68 65 20 6f 70 20 70 61  ied in the op pa
206b7 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 57 68  rameter.  The Wh
206b8 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74  ereClause struct
206b9 75 72 65 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64  ure.** is filled
206ba 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
206bb 6f 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  o subexpressions
206bc 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  .  For example:.
206bd 2a 2a 0a 2a 2a 20 20 20 20 57 48 45 52 45 20 20  **.**    WHERE  
206be 61 3d 3d 27 68 65 6c 6c 6f 27 20 41 4e 44 20 63  a=='hello' AND c
206bf 6f 61 6c 65 73 63 65 28 62 2c 31 31 29 3c 31 30  oalesce(b,11)<10
206c0 20 41 4e 44 20 28 63 2b 31 32 21 3d 64 20 4f 52   AND (c+12!=d OR
206c1 20 63 3d 3d 32 32 29 0a 2a 2a 20 20 20 20 20 20   c==22).**      
206c2 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20       \________/ 
206c3 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
206c4 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f  ____/     \_____
206c5 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20  ___________/.** 
206c6 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b             slot[
206c7 30 5d 20 20 20 20 20 20 20 20 20 20 20 20 73 6c  0]            sl
206c8 6f 74 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20  ot[1]           
206c9 20 20 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a 2a      slot[2].**.*
206ca 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 57  * The original W
206cb 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 70  HERE clause in p
206cc 45 78 70 72 20 69 73 20 75 6e 61 6c 74 65 72 65  Expr is unaltere
206cd 64 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75  d.  All this rou
206ce 74 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 69 73 20  tine.** does is 
206cf 6d 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74 72  make slot[] entr
206d0 69 65 73 20 70 6f 69 6e 74 20 74 6f 20 73 75 62  ies point to sub
206d1 73 74 72 75 63 74 75 72 65 20 77 69 74 68 69 6e  structure within
206d2 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e   pExpr..**.** In
206d3 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
206d4 6e 74 65 6e 63 65 20 61 6e 64 20 69 6e 20 74 68  ntence and in th
206d5 65 20 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f 74  e diagram, "slot
206d6 5b 5d 22 20 72 65 66 65 72 73 20 74 6f 0a 2a 2a  []" refers to.**
206d7 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
206d8 2e 61 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 69  .a[] array.  Thi
206d9 73 20 61 72 72 61 79 20 67 72 6f 77 73 20 61 73  s array grows as
206da 20 6e 65 65 64 65 64 20 74 6f 20 63 6f 6e 74 61   needed to conta
206db 69 6e 0a 2a 2a 20 61 6c 6c 20 74 65 72 6d 73 20  in.** all terms 
206dc 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
206dd 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  use..*/.static v
206de 6f 69 64 20 77 68 65 72 65 53 70 6c 69 74 28 57  oid whereSplit(W
206df 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
206e0 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
206e1 74 20 6f 70 29 7b 0a 20 20 69 66 28 20 70 45 78  t op){.  if( pEx
206e2 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  pr==0 ) return;.
206e3 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21    if( pExpr->op!
206e4 3d 6f 70 20 29 7b 0a 20 20 20 20 77 68 65 72 65  =op ){.    where
206e5 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
206e6 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d  , pExpr, 0);.  }
206e7 65 6c 73 65 7b 0a 20 20 20 20 77 68 65 72 65 53  else{.    whereS
206e8 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d  plit(pWC, pExpr-
206e9 3e 70 4c 65 66 74 2c 20 6f 70 29 3b 0a 20 20 20  >pLeft, op);.   
206ea 20 77 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c   whereSplit(pWC,
206eb 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
206ec 6f 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  op);.  }.}../*.*
206ed 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 20  * Initialize an 
206ee 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20  expression mask 
206ef 73 65 74 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69  set.*/.#define i
206f0 6e 69 74 4d 61 73 6b 53 65 74 28 50 29 20 20 6d  nitMaskSet(P)  m
206f1 65 6d 73 65 74 28 50 2c 20 30 2c 20 73 69 7a 65  emset(P, 0, size
206f2 6f 66 28 2a 50 29 29 0a 0a 2f 2a 0a 2a 2a 20 52  of(*P))../*.** R
206f3 65 74 75 72 6e 20 74 68 65 20 62 69 74 6d 61 73  eturn the bitmas
206f4 6b 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  k for the given 
206f5 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20  cursor number.  
206f6 52 65 74 75 72 6e 20 30 20 69 66 0a 2a 2a 20 69  Return 0 if.** i
206f7 43 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e  Cursor is not in
206f8 20 74 68 65 20 73 65 74 2e 0a 2a 2f 0a 73 74 61   the set..*/.sta
206f9 74 69 63 20 42 69 74 6d 61 73 6b 20 67 65 74 4d  tic Bitmask getM
206fa 61 73 6b 28 45 78 70 72 4d 61 73 6b 53 65 74 20  ask(ExprMaskSet 
206fb 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e 74 20 69  *pMaskSet, int i
206fc 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74 20 69  Cursor){.  int i
206fd 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
206fe 4d 61 73 6b 53 65 74 2d 3e 6e 3b 20 69 2b 2b 29  MaskSet->n; i++)
206ff 7b 0a 20 20 20 20 69 66 28 20 70 4d 61 73 6b 53  {.    if( pMaskS
20700 65 74 2d 3e 69 78 5b 69 5d 3d 3d 69 43 75 72 73  et->ix[i]==iCurs
20701 6f 72 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  or ){.      retu
20702 72 6e 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c  rn ((Bitmask)1)<
20703 3c 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  <i;.    }.  }.  
20704 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
20705 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
20706 6d 61 73 6b 20 66 6f 72 20 63 75 72 73 6f 72 20  mask for cursor 
20707 69 43 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  iCursor..**.** T
20708 68 65 72 65 20 69 73 20 6f 6e 65 20 63 75 72 73  here is one curs
20709 6f 72 20 70 65 72 20 74 61 62 6c 65 20 69 6e 20  or per table in 
2070a 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
2070b 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a    The number of.
2070c 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  ** tables in the
2070d 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20   FROM clause is 
2070e 6c 69 6d 69 74 65 64 20 62 79 20 61 20 74 65 73  limited by a tes
2070f 74 20 65 61 72 6c 79 20 69 6e 20 74 68 65 0a 2a  t early in the.*
20710 2a 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  * sqlite3WhereBe
20711 67 69 6e 28 29 20 72 6f 75 74 69 6e 65 2e 20 20  gin() routine.  
20712 53 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  So we know that 
20713 74 68 65 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78  the pMaskSet->ix
20714 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c 6c  [].** array will
20715 20 6e 65 76 65 72 20 6f 76 65 72 66 6c 6f 77 2e   never overflow.
20716 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
20717 63 72 65 61 74 65 4d 61 73 6b 28 45 78 70 72 4d  createMask(ExprM
20718 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
20719 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a  , int iCursor){.
2071a 20 20 61 73 73 65 72 74 28 20 70 4d 61 73 6b 53    assert( pMaskS
2071b 65 74 2d 3e 6e 20 3c 20 41 72 72 61 79 53 69 7a  et->n < ArraySiz
2071c 65 28 70 4d 61 73 6b 53 65 74 2d 3e 69 78 29 20  e(pMaskSet->ix) 
2071d 29 3b 0a 20 20 70 4d 61 73 6b 53 65 74 2d 3e 69  );.  pMaskSet->i
2071e 78 5b 70 4d 61 73 6b 53 65 74 2d 3e 6e 2b 2b 5d  x[pMaskSet->n++]
2071f 20 3d 20 69 43 75 72 73 6f 72 3b 0a 7d 0a 0a 2f   = iCursor;.}../
20720 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
20721 65 20 77 61 6c 6b 73 20 28 72 65 63 75 72 73 69  e walks (recursi
20722 76 65 6c 79 29 20 61 6e 20 65 78 70 72 65 73 73  vely) an express
20723 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 67 65 6e  ion tree and gen
20724 65 72 61 74 65 73 0a 2a 2a 20 61 20 62 69 74 6d  erates.** a bitm
20725 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77  ask indicating w
20726 68 69 63 68 20 74 61 62 6c 65 73 20 61 72 65 20  hich tables are 
20727 75 73 65 64 20 69 6e 20 74 68 61 74 20 65 78 70  used in that exp
20728 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e  ression.** tree.
20729 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20  .**.** In order 
2072a 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  for this routine
2072b 20 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20 63 61   to work, the ca
2072c 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d  lling function m
2072d 75 73 74 20 68 61 76 65 0a 2a 2a 20 70 72 65 76  ust have.** prev
2072e 69 6f 75 73 6c 79 20 69 6e 76 6f 6b 65 64 20 73  iously invoked s
2072f 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76  qlite3ExprResolv
20730 65 4e 61 6d 65 73 28 29 20 6f 6e 20 74 68 65 20  eNames() on the 
20731 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 53 65 65  expression.  See
20732 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 63  .** the header c
20733 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 61 74 20 72  omment on that r
20734 6f 75 74 69 6e 65 20 66 6f 72 20 61 64 64 69 74  outine for addit
20735 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
20736 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  n..** The sqlite
20737 33 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65  3ExprResolveName
20738 73 28 29 20 72 6f 75 74 69 6e 65 73 20 6c 6f 6f  s() routines loo
20739 6b 73 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61  ks for column na
2073a 6d 65 73 20 61 6e 64 0a 2a 2a 20 73 65 74 73 20  mes and.** sets 
2073b 74 68 65 69 72 20 6f 70 63 6f 64 65 73 20 74 6f  their opcodes to
2073c 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 74   TK_COLUMN and t
2073d 68 65 69 72 20 45 78 70 72 2e 69 54 61 62 6c 65  heir Expr.iTable
2073e 20 66 69 65 6c 64 73 20 74 6f 0a 2a 2a 20 74 68   fields to.** th
2073f 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
20740 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c  mber of the tabl
20741 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
20742 20 6a 75 73 74 20 68 61 73 20 74 6f 0a 2a 2a 20   just has to.** 
20743 74 72 61 6e 73 6c 61 74 65 20 74 68 65 20 63 75  translate the cu
20744 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74  rsor numbers int
20745 6f 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73  o bitmask values
20746 20 61 6e 64 20 4f 52 20 61 6c 6c 0a 2a 2a 20 74   and OR all.** t
20747 68 65 20 62 69 74 6d 61 73 6b 73 20 74 6f 67 65  he bitmasks toge
20748 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ther..*/.static 
20749 42 69 74 6d 61 73 6b 20 65 78 70 72 4c 69 73 74  Bitmask exprList
2074a 54 61 62 6c 65 55 73 61 67 65 28 45 78 70 72 4d  TableUsage(ExprM
2074b 61 73 6b 53 65 74 2a 2c 20 45 78 70 72 4c 69 73  askSet*, ExprLis
2074c 74 2a 29 3b 0a 73 74 61 74 69 63 20 42 69 74 6d  t*);.static Bitm
2074d 61 73 6b 20 65 78 70 72 53 65 6c 65 63 74 54 61  ask exprSelectTa
2074e 62 6c 65 55 73 61 67 65 28 45 78 70 72 4d 61 73  bleUsage(ExprMas
2074f 6b 53 65 74 2a 2c 20 53 65 6c 65 63 74 2a 29 3b  kSet*, Select*);
20750 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20  .static Bitmask 
20751 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 45  exprTableUsage(E
20752 78 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  xprMaskSet *pMas
20753 6b 53 65 74 2c 20 45 78 70 72 20 2a 70 29 7b 0a  kSet, Expr *p){.
20754 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d    Bitmask mask =
20755 20 30 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   0;.  if( p==0 )
20756 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
20757 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d   p->op==TK_COLUM
20758 4e 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20 3d 20  N ){.    mask = 
20759 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
2075a 2c 20 70 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20  , p->iTable);.  
2075b 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 20    return mask;. 
2075c 20 7d 0a 20 20 6d 61 73 6b 20 3d 20 65 78 70 72   }.  mask = expr
2075d 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2075e 53 65 74 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b  Set, p->pRight);
2075f 0a 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54  .  mask |= exprT
20760 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
20761 65 74 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20  et, p->pLeft);. 
20762 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73   mask |= exprLis
20763 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
20764 6b 53 65 74 2c 20 70 2d 3e 70 4c 69 73 74 29 3b  kSet, p->pList);
20765 0a 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 53  .  mask |= exprS
20766 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28  electTableUsage(
20767 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70 53 65  pMaskSet, p->pSe
20768 6c 65 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  lect);.  return 
20769 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42  mask;.}.static B
2076a 69 74 6d 61 73 6b 20 65 78 70 72 4c 69 73 74 54  itmask exprListT
2076b 61 62 6c 65 55 73 61 67 65 28 45 78 70 72 4d 61  ableUsage(ExprMa
2076c 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c  skSet *pMaskSet,
2076d 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2076e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 69  ){.  int i;.  Bi
2076f 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a  tmask mask = 0;.
20770 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20    if( pList ){. 
20771 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c     for(i=0; i<pL
20772 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
20773 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20  {.      mask |= 
20774 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
20775 4d 61 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e  MaskSet, pList->
20776 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[i].pExpr);.   
20777 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
20778 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42  mask;.}.static B
20779 69 74 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63  itmask exprSelec
2077a 74 54 61 62 6c 65 55 73 61 67 65 28 45 78 70 72  tTableUsage(Expr
2077b 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2077c 74 2c 20 53 65 6c 65 63 74 20 2a 70 53 29 7b 0a  t, Select *pS){.
2077d 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d    Bitmask mask =
2077e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 20   0;.  while( pS 
2077f 29 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  ){.    mask |= e
20780 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
20781 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e  e(pMaskSet, pS->
20782 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 6d 61 73  pEList);.    mas
20783 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
20784 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
20785 2c 20 70 53 2d 3e 70 47 72 6f 75 70 42 79 29 3b  , pS->pGroupBy);
20786 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
20787 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
20788 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 4f  pMaskSet, pS->pO
20789 72 64 65 72 42 79 29 3b 0a 20 20 20 20 6d 61 73  rderBy);.    mas
2078a 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
2078b 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53  age(pMaskSet, pS
2078c 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 6d  ->pWhere);.    m
2078d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65  ask |= exprTable
2078e 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2078f 70 53 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20  pS->pHaving);.  
20790 20 20 70 53 20 3d 20 70 53 2d 3e 70 50 72 69 6f    pS = pS->pPrio
20791 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
20792 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  mask;.}../*.** R
20793 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
20794 65 20 67 69 76 65 6e 20 6f 70 65 72 61 74 6f 72  e given operator
20795 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 6f   is one of the o
20796 70 65 72 61 74 6f 72 73 20 74 68 61 74 20 69 73  perators that is
20797 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20  .** allowed for 
20798 61 6e 20 69 6e 64 65 78 61 62 6c 65 20 57 48 45  an indexable WHE
20799 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 20  RE clause term. 
2079a 20 54 68 65 20 61 6c 6c 6f 77 65 64 20 6f 70 65   The allowed ope
2079b 72 61 74 6f 72 73 20 61 72 65 0a 2a 2a 20 22 3d  rators are.** "=
2079c 22 2c 20 22 3c 22 2c 20 22 3e 22 2c 20 22 3c 3d  ", "<", ">", "<=
2079d 22 2c 20 22 3e 3d 22 2c 20 61 6e 64 20 22 49 4e  ", ">=", and "IN
2079e 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  "..*/.static int
2079f 20 61 6c 6c 6f 77 65 64 4f 70 28 69 6e 74 20 6f   allowedOp(int o
207a0 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 54 4b  p){.  assert( TK
207a1 5f 47 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _GT>TK_EQ && TK_
207a2 47 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  GT<TK_GE );.  as
207a3 73 65 72 74 28 20 54 4b 5f 4c 54 3e 54 4b 5f 45  sert( TK_LT>TK_E
207a4 51 20 26 26 20 54 4b 5f 4c 54 3c 54 4b 5f 47 45  Q && TK_LT<TK_GE
207a5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
207a6 5f 4c 45 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _LE>TK_EQ && TK_
207a7 4c 45 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  LE<TK_GE );.  as
207a8 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f  sert( TK_GE==TK_
207a9 45 51 2b 34 20 29 3b 0a 20 20 72 65 74 75 72 6e  EQ+4 );.  return
207aa 20 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 28 6f   op==TK_IN || (o
207ab 70 3e 3d 54 4b 5f 45 51 20 26 26 20 6f 70 3c 3d  p>=TK_EQ && op<=
207ac 54 4b 5f 47 45 29 20 7c 7c 20 6f 70 3d 3d 54 4b  TK_GE) || op==TK
207ad 5f 49 53 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a  _ISNULL;.}../*.*
207ae 2a 20 53 77 61 70 20 74 77 6f 20 6f 62 6a 65 63  * Swap two objec
207af 74 73 20 6f 66 20 74 79 70 65 20 54 2e 0a 2a 2f  ts of type T..*/
207b0 0a 23 64 65 66 69 6e 65 20 53 57 41 50 28 54 59  .#define SWAP(TY
207b1 50 45 2c 41 2c 42 29 20 7b 54 59 50 45 20 74 3d  PE,A,B) {TYPE t=
207b2 41 3b 20 41 3d 42 3b 20 42 3d 74 3b 7d 0a 0a 2f  A; A=B; B=t;}../
207b3 2a 0a 2a 2a 20 43 6f 6d 6d 75 74 65 20 61 20 63  *.** Commute a c
207b4 6f 6d 70 61 72 69 73 69 6f 6e 20 6f 70 65 72 61  omparision opera
207b5 74 6f 72 2e 20 20 45 78 70 72 65 73 73 69 6f 6e  tor.  Expression
207b6 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58  s of the form "X
207b7 20 6f 70 20 59 22 0a 2a 2a 20 61 72 65 20 63 6f   op Y".** are co
207b8 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 22 59 20  nverted into "Y 
207b9 6f 70 20 58 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  op X"..**.** If 
207ba 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
207bb 65 6e 63 65 20 69 73 20 61 73 73 6f 63 69 61 74  ence is associat
207bc 65 64 20 77 69 74 68 20 65 69 74 68 65 72 20 74  ed with either t
207bd 68 65 20 6c 65 66 74 20 6f 72 20 72 69 67 68 74  he left or right
207be 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65 20  .** side of the 
207bf 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 69 74 20 72  comparison, it r
207c0 65 6d 61 69 6e 73 20 61 73 73 6f 63 69 61 74 65  emains associate
207c1 64 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  d with the same 
207c2 73 69 64 65 20 61 66 74 65 72 0a 2a 2a 20 74 68  side after.** th
207c3 65 20 63 6f 6d 6d 75 74 61 74 69 6f 6e 2e 20 53  e commutation. S
207c4 6f 20 22 59 20 63 6f 6c 6c 61 74 65 20 4e 4f 43  o "Y collate NOC
207c5 41 53 45 20 6f 70 20 58 22 20 62 65 63 6f 6d 65  ASE op X" become
207c6 73 20 0a 2a 2a 20 22 58 20 63 6f 6c 6c 61 74 65  s .** "X collate
207c7 20 4e 4f 43 41 53 45 20 6f 70 20 59 22 2e 20 54   NOCASE op Y". T
207c8 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 61  his is because a
207c9 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ny collation seq
207ca 75 65 6e 63 65 20 6f 6e 0a 2a 2a 20 74 68 65 20  uence on.** the 
207cb 6c 65 66 74 20 68 61 6e 64 20 73 69 64 65 20 6f  left hand side o
207cc 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  f a comparison o
207cd 76 65 72 72 69 64 65 73 20 61 6e 79 20 63 6f 6c  verrides any col
207ce 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
207cf 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 74 6f 20  .** attached to 
207d0 74 68 65 20 72 69 67 68 74 2e 20 46 6f 72 20 74  the right. For t
207d1 68 65 20 73 61 6d 65 20 72 65 61 73 6f 6e 20 74  he same reason t
207d2 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  he EP_ExpCollate
207d3 20 66 6c 61 67 0a 2a 2a 20 69 73 20 6e 6f 74 20   flag.** is not 
207d4 63 6f 6d 6d 75 74 65 64 2e 0a 2a 2f 0a 73 74 61  commuted..*/.sta
207d5 74 69 63 20 76 6f 69 64 20 65 78 70 72 43 6f 6d  tic void exprCom
207d6 6d 75 74 65 28 45 78 70 72 20 2a 70 45 78 70 72  mute(Expr *pExpr
207d7 29 7b 0a 20 20 75 31 36 20 65 78 70 52 69 67 68  ){.  u16 expRigh
207d8 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 52 69 67  t = (pExpr->pRig
207d9 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  ht->flags & EP_E
207da 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 75 31  xpCollate);.  u1
207db 36 20 65 78 70 4c 65 66 74 20 3d 20 28 70 45 78  6 expLeft = (pEx
207dc 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73  pr->pLeft->flags
207dd 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
207de 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 6c 6c  );.  assert( all
207df 6f 77 65 64 4f 70 28 70 45 78 70 72 2d 3e 6f 70  owedOp(pExpr->op
207e0 29 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ) && pExpr->op!=
207e1 54 4b 5f 49 4e 20 29 3b 0a 20 20 53 57 41 50 28  TK_IN );.  SWAP(
207e2 43 6f 6c 6c 53 65 71 2a 2c 70 45 78 70 72 2d 3e  CollSeq*,pExpr->
207e3 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 2c 70 45  pRight->pColl,pE
207e4 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c  xpr->pLeft->pCol
207e5 6c 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69  l);.  pExpr->pRi
207e6 67 68 74 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45  ght->flags = (pE
207e7 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61  xpr->pRight->fla
207e8 67 73 20 26 20 7e 45 50 5f 45 78 70 43 6f 6c 6c  gs & ~EP_ExpColl
207e9 61 74 65 29 20 7c 20 65 78 70 4c 65 66 74 3b 0a  ate) | expLeft;.
207ea 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e    pExpr->pLeft->
207eb 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e  flags = (pExpr->
207ec 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 7e  pLeft->flags & ~
207ed 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c  EP_ExpCollate) |
207ee 20 65 78 70 52 69 67 68 74 3b 0a 20 20 53 57 41   expRight;.  SWA
207ef 50 28 45 78 70 72 2a 2c 70 45 78 70 72 2d 3e 70  P(Expr*,pExpr->p
207f0 52 69 67 68 74 2c 70 45 78 70 72 2d 3e 70 4c 65  Right,pExpr->pLe
207f1 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72  ft);.  if( pExpr
207f2 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20  ->op>=TK_GT ){. 
207f3 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54     assert( TK_LT
207f4 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 0a 20 20 20  ==TK_GT+2 );.   
207f5 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d   assert( TK_GE==
207f6 54 4b 5f 4c 45 2b 32 20 29 3b 0a 20 20 20 20 61  TK_LE+2 );.    a
207f7 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f  ssert( TK_GT>TK_
207f8 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  EQ );.    assert
207f9 28 20 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b  ( TK_GT<TK_LE );
207fa 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
207fb 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26 26  pr->op>=TK_GT &&
207fc 20 70 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47   pExpr->op<=TK_G
207fd 45 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e  E );.    pExpr->
207fe 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70  op = ((pExpr->op
207ff 2d 54 4b 5f 47 54 29 5e 32 29 2b 54 4b 5f 47 54  -TK_GT)^2)+TK_GT
20800 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
20801 72 61 6e 73 6c 61 74 65 20 66 72 6f 6d 20 54 4b  ranslate from TK
20802 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  _xx operator to 
20803 57 4f 5f 78 78 20 62 69 74 6d 61 73 6b 2e 0a 2a  WO_xx bitmask..*
20804 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65  /.static int ope
20805 72 61 74 6f 72 4d 61 73 6b 28 69 6e 74 20 6f 70  ratorMask(int op
20806 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 61 73  ){.  int c;.  as
20807 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28  sert( allowedOp(
20808 6f 70 29 20 29 3b 0a 20 20 69 66 28 20 6f 70 3d  op) );.  if( op=
20809 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 63 20  =TK_IN ){.    c 
2080a 3d 20 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65  = WO_IN;.  }else
2080b 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55   if( op==TK_ISNU
2080c 4c 4c 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f  LL ){.    c = WO
2080d 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65  _ISNULL;.  }else
2080e 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 45 51 3c  {.    c = WO_EQ<
2080f 3c 28 6f 70 2d 54 4b 5f 45 51 29 3b 0a 20 20 7d  <(op-TK_EQ);.  }
20810 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54  .  assert( op!=T
20811 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d 3d 57  K_ISNULL || c==W
20812 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 61 73  O_ISNULL );.  as
20813 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 4e 20  sert( op!=TK_IN 
20814 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20  || c==WO_IN );. 
20815 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
20816 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51 20 29  EQ || c==WO_EQ )
20817 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
20818 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f 5f 4c  TK_LT || c==WO_L
20819 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
2081a 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d 3d 57  p!=TK_LE || c==W
2081b 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73 65 72 74  O_LE );.  assert
2081c 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 63  ( op!=TK_GT || c
2081d 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20 61 73 73  ==WO_GT );.  ass
2081e 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 45 20 7c  ert( op!=TK_GE |
2081f 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20 20  | c==WO_GE );.  
20820 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a 0a  return c;.}../*.
20821 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20  ** Search for a 
20822 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52  term in the WHER
20823 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 69 73  E clause that is
20824 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20   of the form "X 
20825 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0a 2a 2a 20  <op> <expr>".** 
20826 77 68 65 72 65 20 58 20 69 73 20 61 20 72 65 66  where X is a ref
20827 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 69 43  erence to the iC
20828 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 69  olumn of table i
20829 43 75 72 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20  Cur and <op> is 
2082a 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 57 4f  one of.** the WO
2082b 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 63 6f 64  _xx operator cod
2082c 65 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  es specified by 
2082d 74 68 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72  the op parameter
2082e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  ..** Return a po
2082f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74 65 72  inter to the ter
20830 6d 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 20  m.  Return 0 if 
20831 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74  not found..*/.st
20832 61 74 69 63 20 57 68 65 72 65 54 65 72 6d 20 2a  atic WhereTerm *
20833 66 69 6e 64 54 65 72 6d 28 0a 20 20 57 68 65 72  findTerm(.  Wher
20834 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
20835 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
20836 6c 61 75 73 65 20 74 6f 20 62 65 20 73 65 61 72  lause to be sear
20837 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ched */.  int iC
20838 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ur,             
20839 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
2083a 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 69 6e 74   of LHS */.  int
2083b 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20   iColumn,       
2083c 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
2083d 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20  ber of LHS */.  
2083e 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2083f 2c 20 20 20 20 20 2f 2a 20 52 48 53 20 6d 75 73  ,     /* RHS mus
20840 74 20 6e 6f 74 20 6f 76 65 72 6c 61 70 20 77 69  t not overlap wi
20841 74 68 20 74 68 69 73 20 6d 61 73 6b 20 2a 2f 0a  th this mask */.
20842 20 20 75 31 36 20 6f 70 2c 20 20 20 20 20 20 20    u16 op,       
20843 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
20844 6f 66 20 57 4f 5f 78 78 20 76 61 6c 75 65 73 20  of WO_xx values 
20845 64 65 73 63 72 69 62 69 6e 67 20 6f 70 65 72 61  describing opera
20846 74 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  tor */.  Index *
20847 70 49 64 78 20 20 20 20 20 20 20 20 20 20 20 2f  pIdx           /
20848 2a 20 4d 75 73 74 20 62 65 20 63 6f 6d 70 61 74  * Must be compat
20849 69 62 6c 65 20 77 69 74 68 20 74 68 69 73 20 69  ible with this i
2084a 6e 64 65 78 2c 20 69 66 20 6e 6f 74 20 4e 55 4c  ndex, if not NUL
2084b 4c 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54  L */.){.  WhereT
2084c 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e  erm *pTerm;.  in
2084d 74 20 6b 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d  t k;.  for(pTerm
2084e 3d 70 57 43 2d 3e 61 2c 20 6b 3d 70 57 43 2d 3e  =pWC->a, k=pWC->
2084f 6e 54 65 72 6d 3b 20 6b 3b 20 6b 2d 2d 2c 20 70  nTerm; k; k--, p
20850 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Term++){.    if(
20851 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73   pTerm->leftCurs
20852 6f 72 3d 3d 69 43 75 72 0a 20 20 20 20 20 20 20  or==iCur.       
20853 26 26 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65  && (pTerm->prere
20854 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61 64  qRight & notRead
20855 79 29 3d 3d 30 0a 20 20 20 20 20 20 20 26 26 20  y)==0.       && 
20856 70 54 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75 6d  pTerm->leftColum
20857 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20  n==iColumn.     
20858 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 65 4f 70    && (pTerm->eOp
20859 65 72 61 74 6f 72 20 26 20 6f 70 29 21 3d 30 0a  erator & op)!=0.
2085a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28      ){.      if(
2085b 20 69 43 75 72 3e 3d 30 20 26 26 20 70 49 64 78   iCur>=0 && pIdx
2085c 20 26 26 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72   && pTerm->eOper
2085d 61 74 6f 72 21 3d 57 4f 5f 49 53 4e 55 4c 4c 20  ator!=WO_ISNULL 
2085e 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20  ){.        Expr 
2085f 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  *pX = pTerm->pEx
20860 70 72 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c  pr;.        Coll
20861 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20  Seq *pColl;.    
20862 20 20 20 20 63 68 61 72 20 69 64 78 61 66 66 3b      char idxaff;
20863 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  .        int j;.
20864 20 20 20 20 20 20 20 20 50 61 72 73 65 20 2a 70          Parse *p
20865 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61  Parse = pWC->pPa
20866 72 73 65 3b 0a 0a 20 20 20 20 20 20 20 20 69 64  rse;..        id
20867 78 61 66 66 20 3d 20 70 49 64 78 2d 3e 70 54 61  xaff = pIdx->pTa
20868 62 6c 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d  ble->aCol[iColum
20869 6e 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  n].affinity;.   
2086a 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
2086b 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b  3IndexAffinityOk
2086c 28 70 58 2c 20 69 64 78 61 66 66 29 20 29 20 63  (pX, idxaff) ) c
2086d 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20  ontinue;..      
2086e 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
2086f 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
20870 71 75 65 6e 63 65 20 72 65 71 75 69 72 65 64 20  quence required 
20871 66 72 6f 6d 20 61 6e 20 69 6e 64 65 78 20 66 6f  from an index fo
20872 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20  r.        ** it 
20873 74 6f 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72  to be useful for
20874 20 6f 70 74 69 6d 69 73 69 6e 67 20 65 78 70 72   optimising expr
20875 65 73 73 69 6f 6e 20 70 58 2e 20 53 74 6f 72 65  ession pX. Store
20876 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a   this.        **
20877 20 76 61 6c 75 65 20 69 6e 20 76 61 72 69 61 62   value in variab
20878 6c 65 20 70 43 6f 6c 6c 2e 0a 20 20 20 20 20 20  le pColl..      
20879 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73    */.        ass
2087a 65 72 74 28 70 58 2d 3e 70 4c 65 66 74 29 3b 0a  ert(pX->pLeft);.
2087b 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
2087c 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
2087d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  pareCollSeq(pPar
2087e 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70  se, pX->pLeft, p
2087f 58 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20  X->pRight);.    
20880 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
20881 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c  {.          pCol
20882 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  l = pParse->db->
20883 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20  pDfltColl;.     
20884 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 66 6f     }..        fo
20885 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e  r(j=0; j<pIdx->n
20886 43 6f 6c 75 6d 6e 20 26 26 20 70 49 64 78 2d 3e  Column && pIdx->
20887 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 21 3d 69 43 6f  aiColumn[j]!=iCo
20888 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 7d 0a 20 20 20  lumn; j++){}.   
20889 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 70       assert( j<p
2088a 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 3b 0a  Idx->nColumn );.
2088b 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
2088c 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c  te3StrICmp(pColl
2088d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 64 78 2d 3e 61  ->zName, pIdx->a
2088e 7a 43 6f 6c 6c 5b 6a 5d 29 20 29 20 63 6f 6e 74  zColl[j]) ) cont
2088f 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
20890 20 20 20 20 72 65 74 75 72 6e 20 70 54 65 72 6d      return pTerm
20891 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
20892 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f  turn 0;.}../* Fo
20893 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20  rward reference 
20894 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65  */.static void e
20895 78 70 72 41 6e 61 6c 79 7a 65 28 53 72 63 4c 69  xprAnalyze(SrcLi
20896 73 74 2a 2c 20 57 68 65 72 65 43 6c 61 75 73 65  st*, WhereClause
20897 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *, int);../*.** 
20898 43 61 6c 6c 20 65 78 70 72 41 6e 61 6c 79 7a 65  Call exprAnalyze
20899 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e   on all terms in
2089a 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e   a WHERE clause.
2089b 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2f 0a 73 74 61 74    .**.**.*/.stat
2089c 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c  ic void exprAnal
2089d 79 7a 65 41 6c 6c 28 0a 20 20 53 72 63 4c 69 73  yzeAll(.  SrcLis
2089e 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20  t *pTabList,    
2089f 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63     /* the FROM c
208a0 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65  lause */.  Where
208a1 43 6c 61 75 73 65 20 2a 70 57 43 20 20 20 20 20  Clause *pWC     
208a2 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45      /* the WHERE
208a3 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 61 6e   clause to be an
208a4 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 69  alyzed */.){.  i
208a5 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 70 57  nt i;.  for(i=pW
208a6 43 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30  C->nTerm-1; i>=0
208a7 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 65 78 70 72  ; i--){.    expr
208a8 41 6e 61 6c 79 7a 65 28 70 54 61 62 4c 69 73 74  Analyze(pTabList
208a9 2c 20 70 57 43 2c 20 69 29 3b 0a 20 20 7d 0a 7d  , pWC, i);.  }.}
208aa 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
208ab 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d  _OMIT_LIKE_OPTIM
208ac 49 5a 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 43 68  IZATION./*.** Ch
208ad 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
208ae 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69  e given expressi
208af 6f 6e 20 69 73 20 61 20 4c 49 4b 45 20 6f 72 20  on is a LIKE or 
208b0 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 74 68  GLOB operator th
208b1 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 70 74  at.** can be opt
208b2 69 6d 69 7a 65 64 20 75 73 69 6e 67 20 69 6e 65  imized using ine
208b3 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
208b4 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 54 52 55  nts.  Return TRU
208b5 45 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 73 6f  E if it is.** so
208b6 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e 6f   and false if no
208b7 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65  t..**.** In orde
208b8 72 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74  r for the operat
208b9 6f 72 20 74 6f 20 62 65 20 6f 70 74 69 6d 69 7a  or to be optimiz
208ba 69 62 6c 65 2c 20 74 68 65 20 52 48 53 20 6d 75  ible, the RHS mu
208bb 73 74 20 62 65 20 61 20 73 74 72 69 6e 67 0a 2a  st be a string.*
208bc 2a 20 6c 69 74 65 72 61 6c 20 74 68 61 74 20 64  * literal that d
208bd 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69  oes not begin wi
208be 74 68 20 61 20 77 69 6c 64 63 61 72 64 2e 20 20  th a wildcard.  
208bf 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
208c0 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 0a 20 20 73  sLikeOrGlob(.  s
208c1 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
208c2 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
208c3 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
208c4 72 2c 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20  r,      /* Test 
208c5 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20  this expression 
208c6 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 50 61 74 74  */.  int *pnPatt
208c7 65 72 6e 2c 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ern,   /* Number
208c8 20 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64   of non-wildcard
208c9 20 70 72 65 66 69 78 20 63 68 61 72 61 63 74 65   prefix characte
208ca 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 73  rs */.  int *pis
208cb 43 6f 6d 70 6c 65 74 65 20 20 2f 2a 20 54 72 75  Complete  /* Tru
208cc 65 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 77 69  e if the only wi
208cd 6c 64 63 61 72 64 20 69 73 20 25 20 69 6e 20 74  ldcard is % in t
208ce 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74 65  he last characte
208cf 72 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  r */.){.  const 
208d0 63 68 61 72 20 2a 7a 3b 0a 20 20 45 78 70 72 20  char *z;.  Expr 
208d1 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74 3b  *pRight, *pLeft;
208d2 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
208d3 73 74 3b 0a 20 20 69 6e 74 20 63 2c 20 63 6e 74  st;.  int c, cnt
208d4 3b 0a 20 20 69 6e 74 20 6e 6f 43 61 73 65 3b 0a  ;.  int noCase;.
208d5 20 20 63 68 61 72 20 77 63 5b 33 5d 3b 0a 20 20    char wc[3];.  
208d6 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
208d7 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49  .  if( !sqlite3I
208d8 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 64 62  sLikeFunction(db
208d9 2c 20 70 45 78 70 72 2c 20 26 6e 6f 43 61 73 65  , pExpr, &noCase
208da 2c 20 77 63 29 20 29 7b 0a 20 20 20 20 72 65 74  , wc) ){.    ret
208db 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69  urn 0;.  }.  pLi
208dc 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73  st = pExpr->pLis
208dd 74 3b 0a 20 20 70 52 69 67 68 74 20 3d 20 70 4c  t;.  pRight = pL
208de 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b  ist->a[0].pExpr;
208df 0a 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6f  .  if( pRight->o
208e0 70 21 3d 54 4b 5f 53 54 52 49 4e 47 20 29 7b 0a  p!=TK_STRING ){.
208e1 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
208e2 7d 0a 20 20 70 4c 65 66 74 20 3d 20 70 4c 69 73  }.  pLeft = pLis
208e3 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20  t->a[1].pExpr;. 
208e4 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 21 3d   if( pLeft->op!=
208e5 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  TK_COLUMN ){.   
208e6 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
208e7 20 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e   pColl = pLeft->
208e8 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 70 43 6f  pColl;.  if( pCo
208e9 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ll==0 ){.    /* 
208ea 54 4f 44 4f 3a 20 43 6f 76 65 72 61 67 65 20 74  TODO: Coverage t
208eb 65 73 74 69 6e 67 20 64 6f 65 73 6e 27 74 20 67  esting doesn't g
208ec 65 74 20 74 68 69 73 20 63 61 73 65 2e 20 49 73  et this case. Is
208ed 20 69 74 20 61 63 74 75 61 6c 6c 79 20 70 6f 73   it actually pos
208ee 73 69 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72  sible.    ** for
208ef 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   an expression o
208f0 66 20 74 79 70 65 20 54 4b 5f 43 4f 4c 55 4d 4e  f type TK_COLUMN
208f1 20 74 6f 20 6e 6f 74 20 68 61 76 65 20 61 6e 20   to not have an 
208f2 61 73 73 69 67 6e 65 64 20 63 6f 6c 6c 61 74 69  assigned collati
208f3 6f 6e 20 0a 20 20 20 20 2a 2a 20 73 65 71 75 65  on .    ** seque
208f4 6e 63 65 20 61 74 20 74 68 69 73 20 70 6f 69 6e  nce at this poin
208f5 74 3f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43  t?.    */.    pC
208f6 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43  oll = db->pDfltC
208f7 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28  oll;.  }.  if( (
208f8 70 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c  pColl->type!=SQL
208f9 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20  ITE_COLL_BINARY 
208fa 7c 7c 20 6e 6f 43 61 73 65 29 20 26 26 0a 20 20  || noCase) &&.  
208fb 20 20 20 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65      (pColl->type
208fc 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f  !=SQLITE_COLL_NO
208fd 43 41 53 45 20 7c 7c 20 21 6e 6f 43 61 73 65 29  CASE || !noCase)
208fe 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
208ff 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
20900 65 71 75 6f 74 65 45 78 70 72 28 64 62 2c 20 70  equoteExpr(db, p
20901 52 69 67 68 74 29 3b 0a 20 20 7a 20 3d 20 28 63  Right);.  z = (c
20902 68 61 72 20 2a 29 70 52 69 67 68 74 2d 3e 74 6f  har *)pRight->to
20903 6b 65 6e 2e 7a 3b 0a 20 20 66 6f 72 28 63 6e 74  ken.z;.  for(cnt
20904 3d 30 3b 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d  =0; (c=z[cnt])!=
20905 30 20 26 26 20 63 21 3d 77 63 5b 30 5d 20 26 26  0 && c!=wc[0] &&
20906 20 63 21 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d   c!=wc[1] && c!=
20907 77 63 5b 32 5d 3b 20 63 6e 74 2b 2b 29 7b 7d 0a  wc[2]; cnt++){}.
20908 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 7c 7c 20    if( cnt==0 || 
20909 32 35 35 3d 3d 28 75 38 29 7a 5b 63 6e 74 5d 20  255==(u8)z[cnt] 
2090a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2090b 0a 20 20 7d 0a 20 20 2a 70 69 73 43 6f 6d 70 6c  .  }.  *pisCompl
2090c 65 74 65 20 3d 20 7a 5b 63 6e 74 5d 3d 3d 77 63  ete = z[cnt]==wc
2090d 5b 30 5d 20 26 26 20 7a 5b 63 6e 74 2b 31 5d 3d  [0] && z[cnt+1]=
2090e 3d 30 3b 0a 20 20 2a 70 6e 50 61 74 74 65 72 6e  =0;.  *pnPattern
2090f 20 3d 20 63 6e 74 3b 0a 20 20 72 65 74 75 72 6e   = cnt;.  return
20910 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   1;.}.#endif /* 
20911 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45  SQLITE_OMIT_LIKE
20912 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
20913 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
20914 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
20915 42 4c 45 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  BLE./*.** Check 
20916 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 67 69  to see if the gi
20917 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ven expression i
20918 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a  s of the form.**
20919 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6c 75  .**         colu
2091a 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 0a 2a 2a  mn MATCH expr.**
2091b 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 74 68 65  .** If it is the
2091c 6e 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20  n return TRUE.  
2091d 49 66 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 46  If not, return F
2091e 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ALSE..*/.static 
2091f 69 6e 74 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c  int isMatchOfCol
20920 75 6d 6e 28 0a 20 20 45 78 70 72 20 2a 70 45 78  umn(.  Expr *pEx
20921 70 72 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20  pr      /* Test 
20922 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20  this expression 
20923 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73 74  */.){.  ExprList
20924 20 2a 70 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20   *pList;..  if( 
20925 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55  pExpr->op!=TK_FU
20926 4e 43 54 49 4f 4e 20 29 7b 0a 20 20 20 20 72 65  NCTION ){.    re
20927 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
20928 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  ( pExpr->token.n
20929 21 3d 35 20 7c 7c 0a 20 20 20 20 20 20 20 73 71  !=5 ||.       sq
2092a 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63  lite3StrNICmp((c
2092b 6f 6e 73 74 20 63 68 61 72 2a 29 70 45 78 70 72  onst char*)pExpr
2092c 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 61 74 63 68  ->token.z,"match
2092d 22 2c 35 29 21 3d 30 20 29 7b 0a 20 20 20 20 72  ",5)!=0 ){.    r
2092e 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
2092f 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  List = pExpr->pL
20930 69 73 74 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  ist;.  if( pList
20931 2d 3e 6e 45 78 70 72 21 3d 32 20 29 7b 0a 20 20  ->nExpr!=2 ){.  
20932 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
20933 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b 31    if( pList->a[1
20934 5d 2e 70 45 78 70 72 2d 3e 6f 70 20 21 3d 20 54  ].pExpr->op != T
20935 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20  K_COLUMN ){.    
20936 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
20937 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
20938 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
20939 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
2093a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  /../*.** If the 
2093b 70 42 61 73 65 20 65 78 70 72 65 73 73 69 6f 6e  pBase expression
2093c 20 6f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 74   originated in t
2093d 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63  he ON or USING c
2093e 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 61 20 6a 6f  lause of.** a jo
2093f 69 6e 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65  in, then transfe
20940 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  r the appropriat
20941 65 20 6d 61 72 6b 69 6e 67 73 20 6f 76 65 72 20  e markings over 
20942 74 6f 20 64 65 72 69 76 65 64 2e 0a 2a 2f 0a 73  to derived..*/.s
20943 74 61 74 69 63 20 76 6f 69 64 20 74 72 61 6e 73  tatic void trans
20944 66 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28  ferJoinMarkings(
20945 45 78 70 72 20 2a 70 44 65 72 69 76 65 64 2c 20  Expr *pDerived, 
20946 45 78 70 72 20 2a 70 42 61 73 65 29 7b 0a 20 20  Expr *pBase){.  
20947 70 44 65 72 69 76 65 64 2d 3e 66 6c 61 67 73 20  pDerived->flags 
20948 7c 3d 20 70 42 61 73 65 2d 3e 66 6c 61 67 73 20  |= pBase->flags 
20949 26 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 3b 0a 20  & EP_FromJoin;. 
2094a 20 70 44 65 72 69 76 65 64 2d 3e 69 52 69 67 68   pDerived->iRigh
2094b 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20 70 42 61  tJoinTable = pBa
2094c 73 65 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61  se->iRightJoinTa
2094d 62 6c 65 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66  ble;.}..#if !def
2094e 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2094f 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  _OR_OPTIMIZATION
20950 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
20951 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
20952 52 59 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  RY)./*.** Return
20953 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76   TRUE if the giv
20954 65 6e 20 74 65 72 6d 20 6f 66 20 61 6e 20 4f 52  en term of an OR
20955 20 63 6c 61 75 73 65 20 63 61 6e 20 62 65 20 63   clause can be c
20956 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 69 6e 74 6f  onverted.** into
20957 20 61 6e 20 49 4e 20 63 6c 61 75 73 65 2e 20 20   an IN clause.  
20958 54 68 65 20 69 43 75 72 73 6f 72 20 61 6e 64 20  The iCursor and 
20959 69 43 6f 6c 75 6d 6e 20 64 65 66 69 6e 65 20 74  iColumn define t
2095a 68 65 20 6c 65 66 74 2d 68 61 6e 64 0a 2a 2a 20  he left-hand.** 
2095b 73 69 64 65 20 6f 66 20 74 68 65 20 49 4e 20 63  side of the IN c
2095c 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lause..**.** The
2095d 20 63 6f 6e 74 65 78 74 20 69 73 20 74 68 61 74   context is that
2095e 20 77 65 20 68 61 76 65 20 6d 75 6c 74 69 70 6c   we have multipl
2095f 65 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20 65  e OR-connected e
20960 71 75 61 6c 69 74 79 20 74 65 72 6d 73 0a 2a 2a  quality terms.**
20961 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
20962 2a 20 20 20 20 20 20 20 20 20 20 20 61 3d 3c 65  *           a=<e
20963 78 70 72 31 3e 20 4f 52 20 20 61 3d 3c 65 78 70  xpr1> OR  a=<exp
20964 72 32 3e 20 4f 52 20 62 3d 3c 65 78 70 72 33 3e  r2> OR b=<expr3>
20965 20 20 4f 52 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 54    OR ....**.** T
20966 68 65 20 70 4f 72 54 65 72 6d 20 69 6e 70 75 74  he pOrTerm input
20967 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
20968 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
20969 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66  a single term of
2096a 0a 2a 2a 20 74 68 69 73 20 4f 52 20 63 6c 61 75  .** this OR clau
2096b 73 65 2e 20 20 49 6e 20 6f 72 64 65 72 20 66 6f  se.  In order fo
2096c 72 20 74 68 65 20 74 65 72 6d 20 74 6f 20 62 65  r the term to be
2096d 20 61 20 63 6f 6e 64 69 64 61 74 65 20 66 6f 72   a condidate for
2096e 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74  .** conversion t
2096f 6f 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72  o an IN operator
20970 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
20971 6d 75 73 74 20 62 65 20 74 72 75 65 3a 0a 2a 2a  must be true:.**
20972 0a 2a 2a 20 20 20 20 20 2a 20 20 54 68 65 20 6c  .**     *  The l
20973 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  eft-hand side of
20974 20 74 68 65 20 74 65 72 6d 20 6d 75 73 74 20 62   the term must b
20975 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 68 69  e the column whi
20976 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 69 73 20  ch.**        is 
20977 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 69 43  identified by iC
20978 75 72 73 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d  ursor and iColum
20979 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20  n..**.**     *  
2097a 49 66 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  If the right-han
2097b 64 20 73 69 64 65 20 69 73 20 61 6c 73 6f 20 61  d side is also a
2097c 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68   column, then th
2097d 65 20 61 66 66 69 6e 69 74 69 65 73 0a 2a 2a 20  e affinities.** 
2097e 20 20 20 20 20 20 20 6f 66 20 62 6f 74 68 20 72         of both r
2097f 69 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73 69  ight and left si
20980 64 65 73 20 6d 75 73 74 20 62 65 20 73 75 63 68  des must be such
20981 20 74 68 61 74 20 6e 6f 20 74 79 70 65 0a 2a 2a   that no type.**
20982 20 20 20 20 20 20 20 20 63 6f 6e 76 65 72 73 69          conversi
20983 6f 6e 73 20 61 72 65 20 72 65 71 75 69 72 65 64  ons are required
20984 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2e 20 20   on the right.  
20985 28 54 69 63 6b 65 74 20 23 32 32 34 39 29 0a 2a  (Ticket #2249).*
20986 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 20 6f 66 20  *.** If both of 
20987 74 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73  these conditions
20988 20 61 72 65 20 74 72 75 65 2c 20 74 68 65 6e 20   are true, then 
20989 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74  return true.  Ot
2098a 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72  herwise.** retur
2098b 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a 73 74 61 74  n false..*/.stat
2098c 69 63 20 69 6e 74 20 6f 72 54 65 72 6d 49 73 4f  ic int orTermIsO
2098d 70 74 43 61 6e 64 69 64 61 74 65 28 57 68 65 72  ptCandidate(Wher
2098e 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 2c 20  eTerm *pOrTerm, 
2098f 69 6e 74 20 69 43 75 72 73 6f 72 2c 20 69 6e 74  int iCursor, int
20990 20 69 43 6f 6c 75 6d 6e 29 7b 0a 20 20 69 6e 74   iColumn){.  int
20991 20 61 66 66 4c 65 66 74 2c 20 61 66 66 52 69 67   affLeft, affRig
20992 68 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  ht;.  assert( pO
20993 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  rTerm->eOperator
20994 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 69 66 28  ==WO_EQ );.  if(
20995 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75   pOrTerm->leftCu
20996 72 73 6f 72 21 3d 69 43 75 72 73 6f 72 20 29 7b  rsor!=iCursor ){
20997 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
20998 20 7d 0a 20 20 69 66 28 20 70 4f 72 54 65 72 6d   }.  if( pOrTerm
20999 2d 3e 6c 65 66 74 43 6f 6c 75 6d 6e 21 3d 69 43  ->leftColumn!=iC
2099a 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 72 65 74  olumn ){.    ret
2099b 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 66 66  urn 0;.  }.  aff
2099c 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45  Right = sqlite3E
2099d 78 70 72 41 66 66 69 6e 69 74 79 28 70 4f 72 54  xprAffinity(pOrT
2099e 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67  erm->pExpr->pRig
2099f 68 74 29 3b 0a 20 20 69 66 28 20 61 66 66 52 69  ht);.  if( affRi
209a0 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ght==0 ){.    re
209a1 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 66  turn 1;.  }.  af
209a2 66 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  fLeft = sqlite3E
209a3 78 70 72 41 66 66 69 6e 69 74 79 28 70 4f 72 54  xprAffinity(pOrT
209a4 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66  erm->pExpr->pLef
209a5 74 29 3b 0a 20 20 69 66 28 20 61 66 66 52 69 67  t);.  if( affRig
209a6 68 74 21 3d 61 66 66 4c 65 66 74 20 29 7b 0a 20  ht!=affLeft ){. 
209a7 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
209a8 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
209a9 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
209aa 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 74  e if the given t
209ab 65 72 6d 20 6f 66 20 61 6e 20 4f 52 20 63 6c 61  erm of an OR cla
209ac 75 73 65 20 63 61 6e 20 62 65 20 69 67 6e 6f 72  use can be ignor
209ad 65 64 20 64 75 72 69 6e 67 0a 2a 2a 20 61 20 63  ed during.** a c
209ae 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72  heck to make sur
209af 65 20 61 6c 6c 20 4f 52 20 74 65 72 6d 73 20 61  e all OR terms a
209b0 72 65 20 63 61 6e 64 69 64 61 74 65 73 20 66 6f  re candidates fo
209b1 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a  r optimization..
209b2 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** In other word
209b3 73 2c 20 72 65 74 75 72 6e 20 74 72 75 65 20 69  s, return true i
209b4 66 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20  f a call to the 
209b5 6f 72 54 65 72 6d 49 73 4f 70 74 43 61 6e 64 69  orTermIsOptCandi
209b6 64 61 74 65 28 29 0a 2a 2a 20 61 62 6f 76 65 20  date().** above 
209b7 72 65 74 75 72 6e 65 64 20 66 61 6c 73 65 20 62  returned false b
209b8 75 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63  ut it is not nec
209b9 65 73 73 61 72 79 20 74 6f 20 64 69 73 71 75 61  essary to disqua
209ba 6c 69 66 79 20 74 68 65 0a 2a 2a 20 6f 70 74 69  lify the.** opti
209bb 6d 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  mization..**.** 
209bc 53 75 70 70 6f 73 65 20 74 68 65 20 6f 72 69 67  Suppose the orig
209bd 69 6e 61 6c 20 4f 52 20 70 68 72 61 73 65 20 77  inal OR phrase w
209be 61 73 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  as this:.**.**  
209bf 20 20 20 20 20 20 20 20 20 61 3d 34 20 20 4f 52           a=4  OR
209c0 20 20 61 3d 31 31 20 20 4f 52 20 20 61 3d 62 0a    a=11  OR  a=b.
209c1 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 6e 61  **.** During ana
209c2 6c 79 73 69 73 2c 20 74 68 65 20 74 68 69 72 64  lysis, the third
209c3 20 74 65 72 6d 20 67 65 74 73 20 66 6c 69 70 70   term gets flipp
209c4 65 64 20 61 72 6f 75 6e 64 20 61 6e 64 20 64 75  ed around and du
209c5 70 6c 69 63 61 74 65 0a 2a 2a 20 73 6f 20 74 68  plicate.** so th
209c6 61 74 20 77 65 20 61 72 65 20 6c 65 66 74 20 77  at we are left w
209c7 69 74 68 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ith this:.**.** 
209c8 20 20 20 20 20 20 20 20 20 20 61 3d 34 20 20 4f            a=4  O
209c9 52 20 20 61 3d 31 31 20 20 4f 52 20 20 61 3d 62  R  a=11  OR  a=b
209ca 20 20 4f 52 20 20 62 3d 61 0a 2a 2a 0a 2a 2a 20    OR  b=a.**.** 
209cb 53 69 6e 63 65 20 74 68 65 20 6c 61 73 74 20 74  Since the last t
209cc 77 6f 20 74 65 72 6d 73 20 61 72 65 20 64 75 70  wo terms are dup
209cd 6c 69 63 61 74 65 73 2c 20 6f 6e 6c 79 20 6f 6e  licates, only on
209ce 65 20 6f 66 20 74 68 65 6d 0a 2a 2a 20 68 61 73  e of them.** has
209cf 20 74 6f 20 71 75 61 6c 69 66 79 20 69 6e 20 6f   to qualify in o
209d0 72 64 65 72 20 66 6f 72 20 74 68 65 20 77 68 6f  rder for the who
209d1 6c 65 20 70 68 72 61 73 65 20 74 6f 20 71 75 61  le phrase to qua
209d2 6c 69 66 79 2e 20 20 57 68 65 6e 0a 2a 2a 20 74  lify.  When.** t
209d3 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
209d4 61 6c 6c 65 64 2c 20 77 65 20 6b 6e 6f 77 20 74  alled, we know t
209d5 68 61 74 20 70 4f 72 54 65 72 6d 20 64 69 64 20  hat pOrTerm did 
209d6 6e 6f 74 20 71 75 61 6c 69 66 79 2e 0a 2a 2a 20  not qualify..** 
209d7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 65 72  This routine mer
209d8 65 6c 79 20 63 68 65 63 6b 73 20 74 6f 20 73 65  ely checks to se
209d9 65 20 69 66 20 70 4f 72 54 65 72 6d 20 68 61 73  e if pOrTerm has
209da 20 61 20 64 75 70 6c 69 63 61 74 65 20 74 68 61   a duplicate tha
209db 74 0a 2a 2a 20 6d 69 67 68 74 20 71 75 61 6c 69  t.** might quali
209dc 66 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73  fy.  If there is
209dd 20 61 20 64 75 70 6c 69 63 61 74 65 20 74 68 61   a duplicate tha
209de 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  t has not yet be
209df 65 6e 0a 2a 2a 20 64 69 73 71 75 61 6c 69 66 69  en.** disqualifi
209e0 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  ed, then return 
209e1 74 72 75 65 2e 20 20 49 66 20 74 68 65 72 65 20  true.  If there 
209e2 61 72 65 20 6e 6f 20 64 75 70 6c 69 63 61 74 65  are no duplicate
209e3 73 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 64 75 70  s, or.** the dup
209e4 6c 69 63 61 74 65 20 68 61 73 20 61 6c 73 6f 20  licate has also 
209e5 62 65 65 6e 20 64 69 73 71 75 61 6c 69 66 65 64  been disqualifed
209e6 2c 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a  , return false..
209e7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 72  */.static int or
209e8 54 65 72 6d 48 61 73 4f 6b 44 75 70 6c 69 63 61  TermHasOkDuplica
209e9 74 65 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a  te(WhereClause *
209ea 70 4f 72 2c 20 57 68 65 72 65 54 65 72 6d 20 2a  pOr, WhereTerm *
209eb 70 4f 72 54 65 72 6d 29 7b 0a 20 20 69 66 28 20  pOrTerm){.  if( 
209ec 70 4f 72 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26  pOrTerm->flags &
209ed 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 29 7b 0a   TERM_COPIED ){.
209ee 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
209ef 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d  he original term
209f0 2e 20 20 54 68 65 20 64 75 70 6c 69 63 61 74 65  .  The duplicate
209f1 20 69 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20   is to the left 
209f2 68 61 64 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e  had.    ** has n
209f3 6f 74 20 79 65 74 20 62 65 65 6e 20 61 6e 61 6c  ot yet been anal
209f4 79 7a 65 64 20 61 6e 64 20 74 68 75 73 20 68 61  yzed and thus ha
209f5 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 64  s not yet been d
209f6 69 73 71 75 61 6c 69 66 69 65 64 2e 20 2a 2f 0a  isqualified. */.
209f7 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
209f8 7d 0a 20 20 69 66 28 20 28 70 4f 72 54 65 72 6d  }.  if( (pOrTerm
209f9 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 56  ->flags & TERM_V
209fa 49 52 54 55 41 4c 29 21 3d 30 0a 20 20 20 20 20  IRTUAL)!=0.     
209fb 26 26 20 28 70 4f 72 2d 3e 61 5b 70 4f 72 54 65  && (pOr->a[pOrTe
209fc 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 2e 66 6c 61  rm->iParent].fla
209fd 67 73 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29  gs & TERM_OR_OK)
209fe 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  !=0 ){.    /* Th
209ff 69 73 20 69 73 20 61 20 64 75 70 6c 69 63 61 74  is is a duplicat
20a00 65 20 74 65 72 6d 2e 20 20 54 68 65 20 6f 72 69  e term.  The ori
20a01 67 69 6e 61 6c 20 71 75 61 6c 69 66 69 65 64 20  ginal qualified 
20a02 73 6f 20 74 68 69 73 20 6f 6e 65 0a 20 20 20 20  so this one.    
20a03 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  ** does not have
20a04 20 74 6f 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75   to. */.    retu
20a05 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54  rn 1;.  }.  /* T
20a06 68 69 73 20 69 73 20 65 69 74 68 65 72 20 61 20  his is either a 
20a07 73 69 6e 67 6c 65 74 6f 6e 20 74 65 72 6d 20 6f  singleton term o
20a08 72 20 65 6c 73 65 20 69 74 20 69 73 20 61 20 64  r else it is a d
20a09 75 70 6c 69 63 61 74 65 20 66 6f 72 0a 20 20 2a  uplicate for.  *
20a0a 2a 20 77 68 69 63 68 20 74 68 65 20 6f 72 69 67  * which the orig
20a0b 69 6e 61 6c 20 64 69 64 20 6e 6f 74 20 71 75 61  inal did not qua
20a0c 6c 69 66 79 2e 20 20 45 69 74 68 65 72 20 77 61  lify.  Either wa
20a0d 79 20 77 65 20 61 72 65 20 64 6f 6e 65 20 66 6f  y we are done fo
20a0e 72 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30  r. */.  return 0
20a0f 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
20a10 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
20a11 54 49 4d 49 5a 41 54 49 4f 4e 20 26 26 20 21 53  TIMIZATION && !S
20a12 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
20a13 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ERY */../*.** Th
20a14 65 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73 20  e input to this 
20a15 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 57 68  routine is an Wh
20a16 65 72 65 54 65 72 6d 20 73 74 72 75 63 74 75 72  ereTerm structur
20a17 65 20 77 69 74 68 20 6f 6e 6c 79 20 74 68 65 0a  e with only the.
20a18 2a 2a 20 22 70 45 78 70 72 22 20 66 69 65 6c 64  ** "pExpr" field
20a19 20 66 69 6c 6c 65 64 20 69 6e 2e 20 20 54 68 65   filled in.  The
20a1a 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75   job of this rou
20a1b 74 69 6e 65 20 69 73 20 74 6f 20 61 6e 61 6c 79  tine is to analy
20a1c 7a 65 20 74 68 65 0a 2a 2a 20 73 75 62 65 78 70  ze the.** subexp
20a1d 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 6f 70 75  ression and popu
20a1e 6c 61 74 65 20 61 6c 6c 20 74 68 65 20 6f 74 68  late all the oth
20a1f 65 72 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  er fields of the
20a20 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a 20 73 74   WhereTerm.** st
20a21 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49  ructure..**.** I
20a22 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
20a23 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   is of the form 
20a24 22 3c 65 78 70 72 3e 20 3c 6f 70 3e 20 58 22 20  "<expr> <op> X" 
20a25 69 74 20 67 65 74 73 20 63 6f 6d 6d 75 74 65 64  it gets commuted
20a26 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 6e 64  .** to the stand
20a27 61 72 64 20 66 6f 72 6d 20 6f 66 20 22 58 20 3c  ard form of "X <
20a28 6f 70 3e 20 3c 65 78 70 72 3e 22 2e 20 20 49 66  op> <expr>".  If
20a29 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20a2a 69 73 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72  is of.** the for
20a2b 6d 20 22 58 20 3c 6f 70 3e 20 59 22 20 77 68 65  m "X <op> Y" whe
20a2c 72 65 20 62 6f 74 68 20 58 20 61 6e 64 20 59 20  re both X and Y 
20a2d 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65  are columns, the
20a2e 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a  n the original.*
20a2f 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  * expression is 
20a30 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 61 20  unchanged and a 
20a31 6e 65 77 20 76 69 72 74 75 61 6c 20 65 78 70 72  new virtual expr
20a32 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f  ession of the fo
20a33 72 6d 0a 2a 2a 20 22 59 20 3c 6f 70 3e 20 58 22  rm.** "Y <op> X"
20a34 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65   is added to the
20a35 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61 6e   WHERE clause an
20a36 64 20 61 6e 61 6c 79 7a 65 64 20 73 65 70 61 72  d analyzed separ
20a37 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ately..*/.static
20a38 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a   void exprAnalyz
20a39 65 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  e(.  SrcList *pS
20a3a 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  rc,            /
20a3b 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
20a3c 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  e */.  WhereClau
20a3d 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20  se *pWC,        
20a3e 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c   /* the WHERE cl
20a3f 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64  ause */.  int id
20a40 78 54 65 72 6d 20 20 20 20 20 20 20 20 20 20 20  xTerm           
20a41 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
20a42 74 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61  the term to be a
20a43 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20  nalyzed */.){.  
20a44 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
20a45 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
20a46 72 6d 5d 3b 0a 20 20 45 78 70 72 4d 61 73 6b 53  rm];.  ExprMaskS
20a47 65 74 20 2a 70 4d 61 73 6b 53 65 74 20 3d 20 70  et *pMaskSet = p
20a48 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20  WC->pMaskSet;.  
20a49 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54  Expr *pExpr = pT
20a4a 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 42 69  erm->pExpr;.  Bi
20a4b 74 6d 61 73 6b 20 70 72 65 72 65 71 4c 65 66 74  tmask prereqLeft
20a4c 3b 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72  ;.  Bitmask prer
20a4d 65 71 41 6c 6c 3b 0a 20 20 69 6e 74 20 6e 50 61  eqAll;.  int nPa
20a4e 74 74 65 72 6e 3b 0a 20 20 69 6e 74 20 69 73 43  ttern;.  int isC
20a4f 6f 6d 70 6c 65 74 65 3b 0a 20 20 69 6e 74 20 6f  omplete;.  int o
20a50 70 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  p;.  Parse *pPar
20a51 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65  se = pWC->pParse
20a52 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
20a53 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
20a54 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
20a55 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
20a56 20 20 70 72 65 72 65 71 4c 65 66 74 20 3d 20 65    prereqLeft = e
20a57 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
20a58 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 70  askSet, pExpr->p
20a59 4c 65 66 74 29 3b 0a 20 20 6f 70 20 3d 20 70 45  Left);.  op = pE
20a5a 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f  xpr->op;.  if( o
20a5b 70 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20  p==TK_IN ){.    
20a5c 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
20a5d 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20  Right==0 );.    
20a5e 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
20a5f 68 74 20 3d 20 65 78 70 72 4c 69 73 74 54 61 62  ht = exprListTab
20a60 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
20a61 2c 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 0a  , pExpr->pList).
20a62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20a63 20 20 20 20 20 20 20 20 20 20 7c 20 65 78 70 72            | expr
20a64 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65  SelectTableUsage
20a65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
20a66 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65  ->pSelect);.  }e
20a67 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49  lse if( op==TK_I
20a68 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 54 65  SNULL ){.    pTe
20a69 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
20a6a 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
20a6b 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52    pTerm->prereqR
20a6c 69 67 68 74 20 3d 20 65 78 70 72 54 61 62 6c 65  ight = exprTable
20a6d 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
20a6e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a  pExpr->pRight);.
20a6f 20 20 7d 0a 20 20 70 72 65 72 65 71 41 6c 6c 20    }.  prereqAll 
20a70 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
20a71 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
20a72 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73  );.  if( ExprHas
20a73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
20a74 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a  EP_FromJoin) ){.
20a75 20 20 20 20 70 72 65 72 65 71 41 6c 6c 20 7c 3d      prereqAll |=
20a76 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65   getMask(pMaskSe
20a77 74 2c 20 70 45 78 70 72 2d 3e 69 52 69 67 68 74  t, pExpr->iRight
20a78 4a 6f 69 6e 54 61 62 6c 65 29 3b 0a 20 20 7d 0a  JoinTable);.  }.
20a79 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41    pTerm->prereqA
20a7a 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a  ll = prereqAll;.
20a7b 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72    pTerm->leftCur
20a7c 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72  sor = -1;.  pTer
20a7d 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b  m->iParent = -1;
20a7e 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  .  pTerm->eOpera
20a7f 74 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 61  tor = 0;.  if( a
20a80 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 26 26 20  llowedOp(op) && 
20a81 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69  (pTerm->prereqRi
20a82 67 68 74 20 26 20 70 72 65 72 65 71 4c 65 66 74  ght & prereqLeft
20a83 29 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72  )==0 ){.    Expr
20a84 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
20a85 3e 70 4c 65 66 74 3b 0a 20 20 20 20 45 78 70 72  >pLeft;.    Expr
20a86 20 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72   *pRight = pExpr
20a87 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66  ->pRight;.    if
20a88 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pLeft->op==TK_
20a89 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
20a8a 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
20a8b 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c  r = pLeft->iTabl
20a8c 65 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  e;.      pTerm->
20a8d 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65  leftColumn = pLe
20a8e 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  ft->iColumn;.   
20a8f 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61     pTerm->eOpera
20a90 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61  tor = operatorMa
20a91 73 6b 28 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20  sk(op);.    }.  
20a92 20 20 69 66 28 20 70 52 69 67 68 74 20 26 26 20    if( pRight && 
20a93 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 43  pRight->op==TK_C
20a94 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 57  OLUMN ){.      W
20a95 68 65 72 65 54 65 72 6d 20 2a 70 4e 65 77 3b 0a  hereTerm *pNew;.
20a96 20 20 20 20 20 20 45 78 70 72 20 2a 70 44 75 70        Expr *pDup
20a97 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72  ;.      if( pTer
20a98 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3e 3d 30  m->leftCursor>=0
20a99 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
20a9a 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  idxNew;.        
20a9b 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
20a9c 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 29  prDup(db, pExpr)
20a9d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62  ;.        if( db
20a9e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
20a9f 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
20aa0 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 44  te3ExprDelete(pD
20aa1 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  up);.          r
20aa2 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d  eturn;.        }
20aa3 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65 77 20  .        idxNew 
20aa4 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
20aa5 65 72 74 28 70 57 43 2c 20 70 44 75 70 2c 20 54  ert(pWC, pDup, T
20aa6 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
20aa7 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
20aa8 20 20 20 69 66 28 20 69 64 78 4e 65 77 3d 3d 30     if( idxNew==0
20aa9 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20   ) return;.     
20aaa 20 20 20 70 4e 65 77 20 3d 20 26 70 57 43 2d 3e     pNew = &pWC->
20aab 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20  a[idxNew];.     
20aac 20 20 20 70 4e 65 77 2d 3e 69 50 61 72 65 6e 74     pNew->iParent
20aad 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
20aae 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
20aaf 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20  ->a[idxTerm];.  
20ab0 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68        pTerm->nCh
20ab1 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ild = 1;.       
20ab2 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d   pTerm->flags |=
20ab3 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20   TERM_COPIED;.  
20ab4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20ab5 20 20 20 70 44 75 70 20 3d 20 70 45 78 70 72 3b     pDup = pExpr;
20ab6 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
20ab7 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20  pTerm;.      }. 
20ab8 20 20 20 20 20 65 78 70 72 43 6f 6d 6d 75 74 65       exprCommute
20ab9 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20 70 4c  (pDup);.      pL
20aba 65 66 74 20 3d 20 70 44 75 70 2d 3e 70 4c 65 66  eft = pDup->pLef
20abb 74 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c  t;.      pNew->l
20abc 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66  eftCursor = pLef
20abd 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20  t->iTable;.     
20abe 20 70 4e 65 77 2d 3e 6c 65 66 74 43 6f 6c 75 6d   pNew->leftColum
20abf 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75  n = pLeft->iColu
20ac0 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  mn;.      pNew->
20ac1 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 70 72  prereqRight = pr
20ac2 65 72 65 71 4c 65 66 74 3b 0a 20 20 20 20 20 20  ereqLeft;.      
20ac3 70 4e 65 77 2d 3e 70 72 65 72 65 71 41 6c 6c 20  pNew->prereqAll 
20ac4 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20 20  = prereqAll;.   
20ac5 20 20 20 70 4e 65 77 2d 3e 65 4f 70 65 72 61 74     pNew->eOperat
20ac6 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61 73  or = operatorMas
20ac7 6b 28 70 44 75 70 2d 3e 6f 70 29 3b 0a 20 20 20  k(pDup->op);.   
20ac8 20 7d 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20   }.  }..#ifndef 
20ac9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57  SQLITE_OMIT_BETW
20aca 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  EEN_OPTIMIZATION
20acb 0a 20 20 2f 2a 20 49 66 20 61 20 74 65 72 6d 20  .  /* If a term 
20acc 69 73 20 74 68 65 20 42 45 54 57 45 45 4e 20 6f  is the BETWEEN o
20acd 70 65 72 61 74 6f 72 2c 20 63 72 65 61 74 65 20  perator, create 
20ace 74 77 6f 20 6e 65 77 20 76 69 72 74 75 61 6c 20  two new virtual 
20acf 74 65 72 6d 73 0a 20 20 2a 2a 20 74 68 61 74 20  terms.  ** that 
20ad0 64 65 66 69 6e 65 20 74 68 65 20 72 61 6e 67 65  define the range
20ad1 20 74 68 61 74 20 74 68 65 20 42 45 54 57 45 45   that the BETWEE
20ad2 4e 20 69 6d 70 6c 65 6d 65 6e 74 73 2e 0a 20 20  N implements..  
20ad3 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20 70 45  */.  else if( pE
20ad4 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 42 45 54 57  xpr->op==TK_BETW
20ad5 45 45 4e 20 29 7b 0a 20 20 20 20 45 78 70 72 4c  EEN ){.    ExprL
20ad6 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78  ist *pList = pEx
20ad7 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 69  pr->pList;.    i
20ad8 6e 74 20 69 3b 0a 20 20 20 20 73 74 61 74 69 63  nt i;.    static
20ad9 20 63 6f 6e 73 74 20 75 38 20 6f 70 73 5b 5d 20   const u8 ops[] 
20ada 3d 20 7b 54 4b 5f 47 45 2c 20 54 4b 5f 4c 45 7d  = {TK_GE, TK_LE}
20adb 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  ;.    assert( pL
20adc 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  ist!=0 );.    as
20add 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78  sert( pList->nEx
20ade 70 72 3d 3d 32 20 29 3b 0a 20 20 20 20 66 6f 72  pr==2 );.    for
20adf 28 69 3d 30 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b  (i=0; i<2; i++){
20ae0 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65  .      Expr *pNe
20ae1 77 45 78 70 72 3b 0a 20 20 20 20 20 20 69 6e 74  wExpr;.      int
20ae2 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 70   idxNew;.      p
20ae3 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65  NewExpr = sqlite
20ae4 33 45 78 70 72 28 64 62 2c 20 6f 70 73 5b 69 5d  3Expr(db, ops[i]
20ae5 2c 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  , sqlite3ExprDup
20ae6 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  (db, pExpr->pLef
20ae7 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t),.            
20ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ae9 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
20aea 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  db, pList->a[i].
20aeb 70 45 78 70 72 29 2c 20 30 29 3b 0a 20 20 20 20  pExpr), 0);.    
20aec 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65    idxNew = where
20aed 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
20aee 2c 20 70 4e 65 77 45 78 70 72 2c 20 54 45 52 4d  , pNewExpr, TERM
20aef 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59  _VIRTUAL|TERM_DY
20af0 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 65 78  NAMIC);.      ex
20af1 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20  prAnalyze(pSrc, 
20af2 70 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20  pWC, idxNew);.  
20af3 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
20af4 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20  ->a[idxTerm];.  
20af5 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65      pWC->a[idxNe
20af6 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78  w].iParent = idx
20af7 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Term;.    }.    
20af8 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20  pTerm->nChild = 
20af9 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  2;.  }.#endif /*
20afa 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45 54   SQLITE_OMIT_BET
20afb 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  WEEN_OPTIMIZATIO
20afc 4e 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e  N */..#if !defin
20afd 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  ed(SQLITE_OMIT_O
20afe 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20  R_OPTIMIZATION) 
20aff 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
20b00 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
20b01 29 0a 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74  ).  /* Attempt t
20b02 6f 20 63 6f 6e 76 65 72 74 20 4f 52 2d 63 6f 6e  o convert OR-con
20b03 6e 65 63 74 65 64 20 74 65 72 6d 73 20 69 6e 74  nected terms int
20b04 6f 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72  o an IN operator
20b05 20 73 6f 20 74 68 61 74 0a 20 20 2a 2a 20 74 68   so that.  ** th
20b06 65 79 20 63 61 6e 20 6d 61 6b 65 20 75 73 65 20  ey can make use 
20b07 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 45 78 61  of indices.  Exa
20b08 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  mple:.  **.  ** 
20b09 20 20 20 20 20 78 20 3d 20 65 78 70 72 31 20 20       x = expr1  
20b0a 4f 52 20 20 65 78 70 72 32 20 3d 20 78 20 20 4f  OR  expr2 = x  O
20b0b 52 20 20 78 20 3d 20 65 78 70 72 33 0a 20 20 2a  R  x = expr3.  *
20b0c 2a 0a 20 20 2a 2a 20 69 73 20 63 6f 6e 76 65 72  *.  ** is conver
20b0d 74 65 64 20 69 6e 74 6f 0a 20 20 2a 2a 0a 20 20  ted into.  **.  
20b0e 2a 2a 20 20 20 20 20 20 78 20 49 4e 20 28 65 78  **      x IN (ex
20b0f 70 72 31 2c 65 78 70 72 32 2c 65 78 70 72 33 29  pr1,expr2,expr3)
20b10 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
20b11 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 75 73  optimization mus
20b12 74 20 62 65 20 6f 6d 69 74 74 65 64 20 69 66 20  t be omitted if 
20b13 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 69 73  OMIT_SUBQUERY is
20b14 20 64 65 66 69 6e 65 64 20 62 65 63 61 75 73 65   defined because
20b15 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 69 6c  .  ** the compil
20b16 65 72 20 66 6f 72 20 74 68 65 20 74 68 65 20 49  er for the the I
20b17 4e 20 6f 70 65 72 61 74 6f 72 20 69 73 20 70 61  N operator is pa
20b18 72 74 20 6f 66 20 73 75 62 2d 71 75 65 72 69 65  rt of sub-querie
20b19 73 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69  s..  */.  else i
20b1a 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  f( pExpr->op==TK
20b1b 5f 4f 52 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f  _OR ){.    int o
20b1c 6b 3b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b  k;.    int i, j;
20b1d 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  .    int iColumn
20b1e 2c 20 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 57  , iCursor;.    W
20b1f 68 65 72 65 43 6c 61 75 73 65 20 73 4f 72 3b 0a  hereClause sOr;.
20b20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
20b21 4f 72 54 65 72 6d 3b 0a 0a 20 20 20 20 61 73 73  OrTerm;..    ass
20b22 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 66 6c 61  ert( (pTerm->fla
20b23 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49  gs & TERM_DYNAMI
20b24 43 29 3d 3d 30 20 29 3b 0a 20 20 20 20 77 68 65  C)==0 );.    whe
20b25 72 65 43 6c 61 75 73 65 49 6e 69 74 28 26 73 4f  reClauseInit(&sO
20b26 72 2c 20 70 57 43 2d 3e 70 50 61 72 73 65 2c 20  r, pWC->pParse, 
20b27 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 20 20 77  pMaskSet);.    w
20b28 68 65 72 65 53 70 6c 69 74 28 26 73 4f 72 2c 20  hereSplit(&sOr, 
20b29 70 45 78 70 72 2c 20 54 4b 5f 4f 52 29 3b 0a 20  pExpr, TK_OR);. 
20b2a 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c     exprAnalyzeAl
20b2b 6c 28 70 53 72 63 2c 20 26 73 4f 72 29 3b 0a 20  l(pSrc, &sOr);. 
20b2c 20 20 20 61 73 73 65 72 74 28 20 73 4f 72 2e 6e     assert( sOr.n
20b2d 54 65 72 6d 3e 3d 32 20 29 3b 0a 20 20 20 20 6a  Term>=2 );.    j
20b2e 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20   = 0;.    do{.  
20b2f 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 73 4f      assert( j<sO
20b30 72 2e 6e 54 65 72 6d 20 29 3b 0a 20 20 20 20 20  r.nTerm );.     
20b31 20 69 43 6f 6c 75 6d 6e 20 3d 20 73 4f 72 2e 61   iColumn = sOr.a
20b32 5b 6a 5d 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a  [j].leftColumn;.
20b33 20 20 20 20 20 20 69 43 75 72 73 6f 72 20 3d 20        iCursor = 
20b34 73 4f 72 2e 61 5b 6a 5d 2e 6c 65 66 74 43 75 72  sOr.a[j].leftCur
20b35 73 6f 72 3b 0a 20 20 20 20 20 20 6f 6b 20 3d 20  sor;.      ok = 
20b36 69 43 75 72 73 6f 72 3e 3d 30 3b 0a 20 20 20 20  iCursor>=0;.    
20b37 20 20 66 6f 72 28 69 3d 73 4f 72 2e 6e 54 65 72    for(i=sOr.nTer
20b38 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 73 4f 72  m-1, pOrTerm=sOr
20b39 2e 61 3b 20 69 3e 3d 30 20 26 26 20 6f 6b 3b 20  .a; i>=0 && ok; 
20b3a 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b  i--, pOrTerm++){
20b3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 72  .        if( pOr
20b3c 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21  Term->eOperator!
20b3d 3d 57 4f 5f 45 51 20 29 7b 0a 20 20 20 20 20 20  =WO_EQ ){.      
20b3e 20 20 20 20 67 6f 74 6f 20 6f 72 5f 6e 6f 74 5f      goto or_not_
20b3f 70 6f 73 73 69 62 6c 65 3b 0a 20 20 20 20 20 20  possible;.      
20b40 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
20b41 6f 72 54 65 72 6d 49 73 4f 70 74 43 61 6e 64 69  orTermIsOptCandi
20b42 64 61 74 65 28 70 4f 72 54 65 72 6d 2c 20 69 43  date(pOrTerm, iC
20b43 75 72 73 6f 72 2c 20 69 43 6f 6c 75 6d 6e 29 20  ursor, iColumn) 
20b44 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72  ){.          pOr
20b45 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 54  Term->flags |= T
20b46 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20  ERM_OR_OK;.     
20b47 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 72 54     }else if( orT
20b48 65 72 6d 48 61 73 4f 6b 44 75 70 6c 69 63 61 74  ermHasOkDuplicat
20b49 65 28 26 73 4f 72 2c 20 70 4f 72 54 65 72 6d 29  e(&sOr, pOrTerm)
20b4a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   ){.          pO
20b4b 72 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 3d 20  rTerm->flags &= 
20b4c 7e 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20  ~TERM_OR_OK;.   
20b4d 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
20b4e 20 20 20 20 20 20 6f 6b 20 3d 20 30 3b 0a 20 20        ok = 0;.  
20b4f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
20b50 20 20 20 20 7d 77 68 69 6c 65 28 20 21 6f 6b 20      }while( !ok 
20b51 26 26 20 28 73 4f 72 2e 61 5b 6a 2b 2b 5d 2e 66  && (sOr.a[j++].f
20b52 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49  lags & TERM_COPI
20b53 45 44 29 21 3d 30 20 26 26 20 6a 3c 32 20 29 3b  ED)!=0 && j<2 );
20b54 0a 20 20 20 20 69 66 28 20 6f 6b 20 29 7b 0a 20  .    if( ok ){. 
20b55 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
20b56 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  List = 0;.      
20b57 45 78 70 72 20 2a 70 4e 65 77 2c 20 2a 70 44 75  Expr *pNew, *pDu
20b58 70 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  p;.      Expr *p
20b59 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Left = 0;.      
20b5a 66 6f 72 28 69 3d 73 4f 72 2e 6e 54 65 72 6d 2d  for(i=sOr.nTerm-
20b5b 31 2c 20 70 4f 72 54 65 72 6d 3d 73 4f 72 2e 61  1, pOrTerm=sOr.a
20b5c 3b 20 69 3e 3d 30 20 26 26 20 6f 6b 3b 20 69 2d  ; i>=0 && ok; i-
20b5d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20  -, pOrTerm++){. 
20b5e 20 20 20 20 20 20 20 69 66 28 20 28 70 4f 72 54         if( (pOrT
20b5f 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45 52  erm->flags & TER
20b60 4d 5f 4f 52 5f 4f 4b 29 3d 3d 30 20 29 20 63 6f  M_OR_OK)==0 ) co
20b61 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
20b62 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
20b63 70 72 44 75 70 28 64 62 2c 20 70 4f 72 54 65 72  prDup(db, pOrTer
20b64 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74  m->pExpr->pRight
20b65 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74  );.        pList
20b66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
20b67 73 74 41 70 70 65 6e 64 28 70 57 43 2d 3e 70 50  stAppend(pWC->pP
20b68 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 70 44 75  arse, pList, pDu
20b69 70 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70  p, 0);.        p
20b6a 4c 65 66 74 20 3d 20 70 4f 72 54 65 72 6d 2d 3e  Left = pOrTerm->
20b6b 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
20b6c 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
20b6d 72 74 28 20 70 4c 65 66 74 21 3d 30 20 29 3b 0a  rt( pLeft!=0 );.
20b6e 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c        pDup = sql
20b6f 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
20b70 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 70 4e  pLeft);.      pN
20b71 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
20b72 28 64 62 2c 20 54 4b 5f 49 4e 2c 20 70 44 75 70  (db, TK_IN, pDup
20b73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  , 0, 0);.      i
20b74 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  f( pNew ){.     
20b75 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20     int idxNew;. 
20b76 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 4a         transferJ
20b77 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 70 4e 65 77  oinMarkings(pNew
20b78 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  , pExpr);.      
20b79 20 20 70 4e 65 77 2d 3e 70 4c 69 73 74 20 3d 20    pNew->pList = 
20b7a 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69  pList;.        i
20b7b 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61  dxNew = whereCla
20b7c 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
20b7d 4e 65 77 2c 20 54 45 52 4d 5f 56 49 52 54 55 41  New, TERM_VIRTUA
20b7e 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b  L|TERM_DYNAMIC);
20b7f 0a 20 20 20 20 20 20 20 20 65 78 70 72 41 6e 61  .        exprAna
20b80 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20  lyze(pSrc, pWC, 
20b81 69 64 78 4e 65 77 29 3b 0a 20 20 20 20 20 20 20  idxNew);.       
20b82 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
20b83 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20  [idxTerm];.     
20b84 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77     pWC->a[idxNew
20b85 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ].iParent = idxT
20b86 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  erm;.        pTe
20b87 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a  rm->nChild = 1;.
20b88 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
20b89 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
20b8a 4c 69 73 74 44 65 6c 65 74 65 28 70 4c 69 73 74  ListDelete(pList
20b8b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
20b8c 0a 6f 72 5f 6e 6f 74 5f 70 6f 73 73 69 62 6c 65  .or_not_possible
20b8d 3a 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75 73  :.    whereClaus
20b8e 65 43 6c 65 61 72 28 26 73 4f 72 29 3b 0a 20 20  eClear(&sOr);.  
20b8f 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
20b90 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
20b91 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e  IZATION */..#ifn
20b92 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
20b93 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  LIKE_OPTIMIZATIO
20b94 4e 0a 20 20 2f 2a 20 41 64 64 20 63 6f 6e 73 74  N.  /* Add const
20b95 72 61 69 6e 74 73 20 74 6f 20 72 65 64 75 63 65  raints to reduce
20b96 20 74 68 65 20 73 65 61 72 63 68 20 73 70 61 63   the search spac
20b97 65 20 6f 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47  e on a LIKE or G
20b98 4c 4f 42 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f  LOB.  ** operato
20b99 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  r..  */.  if( is
20b9a 4c 69 6b 65 4f 72 47 6c 6f 62 28 64 62 2c 20 70  LikeOrGlob(db, p
20b9b 45 78 70 72 2c 20 26 6e 50 61 74 74 65 72 6e 2c  Expr, &nPattern,
20b9c 20 26 69 73 43 6f 6d 70 6c 65 74 65 29 20 29 7b   &isComplete) ){
20b9d 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74  .    Expr *pLeft
20b9e 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 45  , *pRight;.    E
20b9f 78 70 72 20 2a 70 53 74 72 31 2c 20 2a 70 53 74  xpr *pStr1, *pSt
20ba0 72 32 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e  r2;.    Expr *pN
20ba1 65 77 45 78 70 72 31 2c 20 2a 70 4e 65 77 45 78  ewExpr1, *pNewEx
20ba2 70 72 32 3b 0a 20 20 20 20 69 6e 74 20 69 64 78  pr2;.    int idx
20ba3 4e 65 77 31 2c 20 69 64 78 4e 65 77 32 3b 0a 0a  New1, idxNew2;..
20ba4 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70      pLeft = pExp
20ba5 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  r->pList->a[1].p
20ba6 45 78 70 72 3b 0a 20 20 20 20 70 52 69 67 68 74  Expr;.    pRight
20ba7 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d   = pExpr->pList-
20ba8 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[0].pExpr;.   
20ba9 20 70 53 74 72 31 20 3d 20 73 71 6c 69 74 65 33   pStr1 = sqlite3
20baa 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
20bab 5f 53 54 52 49 4e 47 2c 20 30 2c 20 30 2c 20 30  _STRING, 0, 0, 0
20bac 29 3b 0a 20 20 20 20 69 66 28 20 70 53 74 72 31  );.    if( pStr1
20bad 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
20bae 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26  3TokenCopy(db, &
20baf 70 53 74 72 31 2d 3e 74 6f 6b 65 6e 2c 20 26 70  pStr1->token, &p
20bb0 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29 3b 0a 20  Right->token);. 
20bb1 20 20 20 20 20 70 53 74 72 31 2d 3e 74 6f 6b 65       pStr1->toke
20bb2 6e 2e 6e 20 3d 20 6e 50 61 74 74 65 72 6e 3b 0a  n.n = nPattern;.
20bb3 20 20 20 20 20 20 70 53 74 72 31 2d 3e 66 6c 61        pStr1->fla
20bb4 67 73 20 3d 20 45 50 5f 44 65 71 75 6f 74 65 64  gs = EP_Dequoted
20bb5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74 72  ;.    }.    pStr
20bb6 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  2 = sqlite3ExprD
20bb7 75 70 28 64 62 2c 20 70 53 74 72 31 29 3b 0a 20  up(db, pStr1);. 
20bb8 20 20 20 69 66 28 20 70 53 74 72 32 20 29 7b 0a     if( pStr2 ){.
20bb9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
20bba 74 72 32 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 29  tr2->token.dyn )
20bbb 3b 0a 20 20 20 20 20 20 2b 2b 2a 28 75 38 2a 29  ;.      ++*(u8*)
20bbc 26 70 53 74 72 32 2d 3e 74 6f 6b 65 6e 2e 7a 5b  &pStr2->token.z[
20bbd 6e 50 61 74 74 65 72 6e 2d 31 5d 3b 0a 20 20 20  nPattern-1];.   
20bbe 20 7d 0a 20 20 20 20 70 4e 65 77 45 78 70 72 31   }.    pNewExpr1
20bbf 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
20bc0 70 50 61 72 73 65 2c 20 54 4b 5f 47 45 2c 20 73  pParse, TK_GE, s
20bc1 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
20bc2 2c 70 4c 65 66 74 29 2c 20 70 53 74 72 31 2c 20  ,pLeft), pStr1, 
20bc3 30 29 3b 0a 20 20 20 20 69 64 78 4e 65 77 31 20  0);.    idxNew1 
20bc4 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
20bc5 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70  ert(pWC, pNewExp
20bc6 72 31 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  r1, TERM_VIRTUAL
20bc7 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
20bc8 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28      exprAnalyze(
20bc9 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65  pSrc, pWC, idxNe
20bca 77 31 29 3b 0a 20 20 20 20 70 4e 65 77 45 78 70  w1);.    pNewExp
20bcb 72 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  r2 = sqlite3PExp
20bcc 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4c 54 2c  r(pParse, TK_LT,
20bcd 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
20bce 64 62 2c 70 4c 65 66 74 29 2c 20 70 53 74 72 32  db,pLeft), pStr2
20bcf 2c 20 30 29 3b 0a 20 20 20 20 69 64 78 4e 65 77  , 0);.    idxNew
20bd0 32 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49  2 = whereClauseI
20bd1 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45  nsert(pWC, pNewE
20bd2 78 70 72 32 2c 20 54 45 52 4d 5f 56 49 52 54 55  xpr2, TERM_VIRTU
20bd3 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
20bd4 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a  ;.    exprAnalyz
20bd5 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78  e(pSrc, pWC, idx
20bd6 4e 65 77 32 29 3b 0a 20 20 20 20 70 54 65 72 6d  New2);.    pTerm
20bd7 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
20bd8 72 6d 5d 3b 0a 20 20 20 20 69 66 28 20 69 73 43  rm];.    if( isC
20bd9 6f 6d 70 6c 65 74 65 20 29 7b 0a 20 20 20 20 20  omplete ){.     
20bda 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 31 5d   pWC->a[idxNew1]
20bdb 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65  .iParent = idxTe
20bdc 72 6d 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61  rm;.      pWC->a
20bdd 5b 69 64 78 4e 65 77 32 5d 2e 69 50 61 72 65 6e  [idxNew2].iParen
20bde 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
20bdf 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64     pTerm->nChild
20be0 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 2;.    }.  }.
20be1 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
20be2 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d  _OMIT_LIKE_OPTIM
20be3 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e  IZATION */..#ifn
20be4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
20be5 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f  VIRTUALTABLE.  /
20be6 2a 20 41 64 64 20 61 20 57 4f 5f 4d 41 54 43 48  * Add a WO_MATCH
20be7 20 61 75 78 69 6c 69 61 72 79 20 74 65 72 6d 20   auxiliary term 
20be8 74 6f 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  to the constrain
20be9 74 20 73 65 74 20 69 66 20 74 68 65 0a 20 20 2a  t set if the.  *
20bea 2a 20 63 75 72 72 65 6e 74 20 65 78 70 72 65 73  * current expres
20beb 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66  sion is of the f
20bec 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e 20 4d 41 54  orm:  column MAT
20bed 43 48 20 65 78 70 72 2e 0a 20 20 2a 2a 20 54 68  CH expr..  ** Th
20bee 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  is information i
20bef 73 20 75 73 65 64 20 62 79 20 74 68 65 20 78 42  s used by the xB
20bf0 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 73  estIndex methods
20bf1 20 6f 66 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c   of.  ** virtual
20bf2 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 6e 61   tables.  The na
20bf3 74 69 76 65 20 71 75 65 72 79 20 6f 70 74 69 6d  tive query optim
20bf4 69 7a 65 72 20 64 6f 65 73 20 6e 6f 74 20 61 74  izer does not at
20bf5 74 65 6d 70 74 0a 20 20 2a 2a 20 74 6f 20 64 6f  tempt.  ** to do
20bf6 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68 20 4d   anything with M
20bf7 41 54 43 48 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  ATCH functions..
20bf8 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4d 61 74    */.  if( isMat
20bf9 63 68 4f 66 43 6f 6c 75 6d 6e 28 70 45 78 70 72  chOfColumn(pExpr
20bfa 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 78  ) ){.    int idx
20bfb 4e 65 77 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  New;.    Expr *p
20bfc 52 69 67 68 74 2c 20 2a 70 4c 65 66 74 3b 0a 20  Right, *pLeft;. 
20bfd 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e     WhereTerm *pN
20bfe 65 77 54 65 72 6d 3b 0a 20 20 20 20 42 69 74 6d  ewTerm;.    Bitm
20bff 61 73 6b 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e  ask prereqColumn
20c00 2c 20 70 72 65 72 65 71 45 78 70 72 3b 0a 0a 20  , prereqExpr;.. 
20c01 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78 70     pRight = pExp
20c02 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  r->pList->a[0].p
20c03 45 78 70 72 3b 0a 20 20 20 20 70 4c 65 66 74 20  Expr;.    pLeft 
20c04 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  = pExpr->pList->
20c05 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[1].pExpr;.    
20c06 70 72 65 72 65 71 45 78 70 72 20 3d 20 65 78 70  prereqExpr = exp
20c07 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
20c08 6b 53 65 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  kSet, pRight);. 
20c09 20 20 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 20     prereqColumn 
20c0a 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
20c0b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c 65 66 74  (pMaskSet, pLeft
20c0c 29 3b 0a 20 20 20 20 69 66 28 20 28 70 72 65 72  );.    if( (prer
20c0d 65 71 45 78 70 72 20 26 20 70 72 65 72 65 71 43  eqExpr & prereqC
20c0e 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  olumn)==0 ){.   
20c0f 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70     Expr *pNewExp
20c10 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 45 78 70  r;.      pNewExp
20c11 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  r = sqlite3Expr(
20c12 64 62 2c 20 54 4b 5f 4d 41 54 43 48 2c 20 30 2c  db, TK_MATCH, 0,
20c13 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
20c14 64 62 2c 20 70 52 69 67 68 74 29 2c 20 30 29 3b  db, pRight), 0);
20c15 0a 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20  .      idxNew = 
20c16 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
20c17 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 2c  t(pWC, pNewExpr,
20c18 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45   TERM_VIRTUAL|TE
20c19 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  RM_DYNAMIC);.   
20c1a 20 20 20 70 4e 65 77 54 65 72 6d 20 3d 20 26 70     pNewTerm = &p
20c1b 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a 20  WC->a[idxNew];. 
20c1c 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70       pNewTerm->p
20c1d 72 65 72 65 71 52 69 67 68 74 20 3d 20 70 72 65  rereqRight = pre
20c1e 72 65 71 45 78 70 72 3b 0a 20 20 20 20 20 20 70  reqExpr;.      p
20c1f 4e 65 77 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  NewTerm->leftCur
20c20 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61  sor = pLeft->iTa
20c21 62 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  ble;.      pNewT
20c22 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75 6d 6e 20  erm->leftColumn 
20c23 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e  = pLeft->iColumn
20c24 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ;.      pNewTerm
20c25 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f  ->eOperator = WO
20c26 5f 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 70 4e  _MATCH;.      pN
20c27 65 77 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20  ewTerm->iParent 
20c28 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20  = idxTerm;.     
20c29 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
20c2a 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20  [idxTerm];.     
20c2b 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d   pTerm->nChild =
20c2c 20 31 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d   1;.      pTerm-
20c2d 3e 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43  >flags |= TERM_C
20c2e 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 70 4e 65  OPIED;.      pNe
20c2f 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c  wTerm->prereqAll
20c30 20 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71   = pTerm->prereq
20c31 41 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  All;.    }.  }.#
20c32 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
20c33 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
20c34 45 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  E */.}../*.** Re
20c35 74 75 72 6e 20 54 52 55 45 20 69 66 20 61 6e 79  turn TRUE if any
20c36 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
20c37 6f 6e 73 20 69 6e 20 70 4c 69 73 74 2d 3e 61 5b  ons in pList->a[
20c38 69 46 69 72 73 74 2e 2e 2e 5d 20 63 6f 6e 74 61  iFirst...] conta
20c39 69 6e 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63  in.** a referenc
20c3a 65 20 74 6f 20 61 6e 79 20 74 61 62 6c 65 20 6f  e to any table o
20c3b 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 69 42  ther than the iB
20c3c 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ase table..*/.st
20c3d 61 74 69 63 20 69 6e 74 20 72 65 66 65 72 65 6e  atic int referen
20c3e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28 0a  cesOtherTables(.
20c3f 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
20c40 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  t,          /* S
20c41 65 61 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e  earch expression
20c42 73 20 69 6e 20 74 68 73 20 6c 69 73 74 20 2a 2f  s in ths list */
20c43 0a 20 20 45 78 70 72 4d 61 73 6b 53 65 74 20 2a  .  ExprMaskSet *
20c44 70 4d 61 73 6b 53 65 74 2c 20 20 20 20 2f 2a 20  pMaskSet,    /* 
20c45 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62  Mapping from tab
20c46 6c 65 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a  les to bitmaps *
20c47 2f 0a 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20  /.  int iFirst, 
20c48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20c49 20 42 65 20 73 65 61 72 63 68 69 6e 67 20 77 69   Be searching wi
20c4a 74 68 20 74 68 65 20 69 46 69 72 73 74 2d 74 68  th the iFirst-th
20c4b 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
20c4c 20 69 6e 74 20 69 42 61 73 65 20 20 20 20 20 20   int iBase      
20c4d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 67             /* Ig
20c4e 6e 6f 72 65 20 72 65 66 65 72 65 6e 63 65 73 20  nore references 
20c4f 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  to this table */
20c50 0a 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 61 6c  .){.  Bitmask al
20c51 6c 6f 77 65 64 20 3d 20 7e 67 65 74 4d 61 73 6b  lowed = ~getMask
20c52 28 70 4d 61 73 6b 53 65 74 2c 20 69 42 61 73 65  (pMaskSet, iBase
20c53 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 46 69 72  );.  while( iFir
20c54 73 74 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  st<pList->nExpr 
20c55 29 7b 0a 20 20 20 20 69 66 28 20 28 65 78 70 72  ){.    if( (expr
20c56 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
20c57 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 46  Set, pList->a[iF
20c58 69 72 73 74 2b 2b 5d 2e 70 45 78 70 72 29 26 61  irst++].pExpr)&a
20c59 6c 6c 6f 77 65 64 29 21 3d 30 20 29 7b 0a 20 20  llowed)!=0 ){.  
20c5a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
20c5b 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
20c5c 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68   0;.}.../*.** Th
20c5d 69 73 20 72 6f 75 74 69 6e 65 20 64 65 63 69 64  is routine decid
20c5e 65 73 20 69 66 20 70 49 64 78 20 63 61 6e 20 62  es if pIdx can b
20c5f 65 20 75 73 65 64 20 74 6f 20 73 61 74 69 73 66  e used to satisf
20c60 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 2a  y the ORDER BY.*
20c61 2a 20 63 6c 61 75 73 65 2e 20 20 49 66 20 69 74  * clause.  If it
20c62 20 63 61 6e 2c 20 69 74 20 72 65 74 75 72 6e 73   can, it returns
20c63 20 31 2e 20 20 49 66 20 70 49 64 78 20 63 61 6e   1.  If pIdx can
20c64 6e 6f 74 20 73 61 74 69 73 66 79 20 74 68 65 0a  not satisfy the.
20c65 2a 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  ** ORDER BY clau
20c66 73 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  se, this routine
20c67 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 0a 2a   returns 0..**.*
20c68 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e  * pOrderBy is an
20c69 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
20c6a 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 73   from a SELECT s
20c6b 74 61 74 65 6d 65 6e 74 2e 20 20 70 54 61 62 20  tatement.  pTab 
20c6c 69 73 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d  is the.** left-m
20c6d 6f 73 74 20 74 61 62 6c 65 20 69 6e 20 74 68 65  ost table in the
20c6e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
20c6f 74 68 61 74 20 73 61 6d 65 20 53 45 4c 45 43 54  that same SELECT
20c70 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 0a 2a   statement and.*
20c71 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  * the table has 
20c72 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  a cursor number 
20c73 6f 66 20 22 62 61 73 65 22 2e 20 20 70 49 64 78  of "base".  pIdx
20c74 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20   is an index on 
20c75 70 54 61 62 2e 0a 2a 2a 0a 2a 2a 20 6e 45 71 43  pTab..**.** nEqC
20c76 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ol is the number
20c77 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70   of columns of p
20c78 49 64 78 20 74 68 61 74 20 61 72 65 20 75 73 65  Idx that are use
20c79 64 20 61 73 20 65 71 75 61 6c 69 74 79 0a 2a 2a  d as equality.**
20c7a 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 41   constraints.  A
20c7b 6e 79 20 6f 66 20 74 68 65 73 65 20 63 6f 6c 75  ny of these colu
20c7c 6d 6e 73 20 6d 61 79 20 62 65 20 6d 69 73 73 69  mns may be missi
20c7d 6e 67 20 66 72 6f 6d 20 74 68 65 20 4f 52 44 45  ng from the ORDE
20c7e 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 20 61  R BY.** clause a
20c7f 6e 64 20 74 68 65 20 6d 61 74 63 68 20 63 61 6e  nd the match can
20c80 20 73 74 69 6c 6c 20 62 65 20 61 20 73 75 63 63   still be a succ
20c81 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 74  ess..**.** All t
20c82 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45  erms of the ORDE
20c83 52 20 42 59 20 74 68 61 74 20 6d 61 74 63 68 20  R BY that match 
20c84 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65  against the inde
20c85 78 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  x must be either
20c86 0a 2a 2a 20 41 53 43 20 6f 72 20 44 45 53 43 2e  .** ASC or DESC.
20c87 20 20 28 54 65 72 6d 73 20 6f 66 20 74 68 65 20    (Terms of the 
20c88 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
20c89 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
20c8a 61 20 55 4e 49 51 55 45 0a 2a 2a 20 69 6e 64 65  a UNIQUE.** inde
20c8b 78 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  x do not need to
20c8c 20 73 61 74 69 73 66 79 20 74 68 69 73 20 63 6f   satisfy this co
20c8d 6e 73 74 72 61 69 6e 74 2e 29 20 20 54 68 65 20  nstraint.)  The 
20c8e 2a 70 62 52 65 76 20 76 61 6c 75 65 20 69 73 0a  *pbRev value is.
20c8f 2a 2a 20 73 65 74 20 74 6f 20 31 20 69 66 20 74  ** set to 1 if t
20c90 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
20c91 73 65 20 69 73 20 61 6c 6c 20 44 45 53 43 20 61  se is all DESC a
20c92 6e 64 20 69 74 20 69 73 20 73 65 74 20 74 6f 20  nd it is set to 
20c93 30 20 69 66 0a 2a 2a 20 74 68 65 20 4f 52 44 45  0 if.** the ORDE
20c94 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61  R BY clause is a
20c95 6c 6c 20 41 53 43 2e 0a 2a 2f 0a 73 74 61 74 69  ll ASC..*/.stati
20c96 63 20 69 6e 74 20 69 73 53 6f 72 74 69 6e 67 49  c int isSortingI
20c97 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70  ndex(.  Parse *p
20c98 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
20c99 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
20c9a 78 74 20 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b  xt */.  ExprMask
20c9b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 20  Set *pMaskSet,  
20c9c 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20  /* Mapping from 
20c9d 74 61 62 6c 65 20 69 6e 64 69 63 65 73 20 74 6f  table indices to
20c9e 20 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 49 6e   bitmaps */.  In
20c9f 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20  dex *pIdx,      
20ca0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
20ca1 65 78 20 77 65 20 61 72 65 20 74 65 73 74 69 6e  ex we are testin
20ca2 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 2c  g */.  int base,
20ca3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
20ca4 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
20ca5 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 74 6f  for the table to
20ca6 20 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a 20 20   be sorted */.  
20ca7 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
20ca8 42 79 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 4f  By,     /* The O
20ca9 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
20caa 2f 0a 20 20 69 6e 74 20 6e 45 71 43 6f 6c 2c 20  /.  int nEqCol, 
20cab 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
20cac 75 6d 62 65 72 20 6f 66 20 69 6e 64 65 78 20 63  umber of index c
20cad 6f 6c 75 6d 6e 73 20 77 69 74 68 20 3d 3d 20 63  olumns with == c
20cae 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
20caf 69 6e 74 20 2a 70 62 52 65 76 20 20 20 20 20 20  int *pbRev      
20cb0 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74          /* Set t
20cb1 6f 20 31 20 69 66 20 4f 52 44 45 52 20 42 59 20  o 1 if ORDER BY 
20cb2 69 73 20 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20  is DESC */.){.  
20cb3 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
20cb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20cb5 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
20cb6 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72   */.  int sortOr
20cb7 64 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  der = 0;        
20cb8 20 20 20 20 20 20 2f 2a 20 58 4f 52 20 6f 66 20        /* XOR of 
20cb9 69 6e 64 65 78 20 61 6e 64 20 4f 52 44 45 52 20  index and ORDER 
20cba 42 59 20 73 6f 72 74 20 64 69 72 65 63 74 69 6f  BY sort directio
20cbb 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d  n */.  int nTerm
20cbc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
20cbd 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
20cbe 20 6f 66 20 4f 52 44 45 52 20 42 59 20 74 65 72   of ORDER BY ter
20cbf 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45  ms */.  struct E
20cc0 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54  xprList_item *pT
20cc1 65 72 6d 3b 20 20 20 20 2f 2a 20 41 20 74 65 72  erm;    /* A ter
20cc2 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  m of the ORDER B
20cc3 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 71  Y clause */.  sq
20cc4 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
20cc5 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72  se->db;..  asser
20cc6 74 28 20 70 4f 72 64 65 72 42 79 21 3d 30 20 29  t( pOrderBy!=0 )
20cc7 3b 0a 20 20 6e 54 65 72 6d 20 3d 20 70 4f 72 64  ;.  nTerm = pOrd
20cc8 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 61  erBy->nExpr;.  a
20cc9 73 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20 29  ssert( nTerm>0 )
20cca 3b 0a 0a 20 20 2f 2a 20 4d 61 74 63 68 20 74 65  ;..  /* Match te
20ccb 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
20ccc 20 42 59 20 63 6c 61 75 73 65 20 61 67 61 69 6e   BY clause again
20ccd 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20  st columns of.  
20cce 2a 2a 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20  ** the index..  
20ccf 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61  **.  ** Note tha
20cd0 74 20 69 6e 64 69 63 65 73 20 68 61 76 65 20 70  t indices have p
20cd1 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72 65 67  Idx->nColumn reg
20cd2 75 6c 61 72 20 63 6f 6c 75 6d 6e 73 20 70 6c 75  ular columns plu
20cd3 73 0a 20 20 2a 2a 20 6f 6e 65 20 61 64 64 69 74  s.  ** one addit
20cd4 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20 63 6f 6e  ional column con
20cd5 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 69  taining the rowi
20cd6 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 63 6f  d.  The rowid co
20cd7 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65  lumn.  ** of the
20cd8 20 69 6e 64 65 78 20 69 73 20 61 6c 73 6f 20 61   index is also a
20cd9 6c 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20  llowed to match 
20cda 61 67 61 69 6e 73 74 20 74 68 65 20 4f 52 44 45  against the ORDE
20cdb 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65  R BY.  ** clause
20cdc 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a  ..  */.  for(i=j
20cdd 3d 30 2c 20 70 54 65 72 6d 3d 70 4f 72 64 65 72  =0, pTerm=pOrder
20cde 42 79 2d 3e 61 3b 20 6a 3c 6e 54 65 72 6d 20 26  By->a; j<nTerm &
20cdf 26 20 69 3c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75  & i<=pIdx->nColu
20ce0 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78  mn; i++){.    Ex
20ce1 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
20ce2 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
20ce3 6f 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  on of the ORDER 
20ce4 42 59 20 70 54 65 72 6d 20 2a 2f 0a 20 20 20 20  BY pTerm */.    
20ce5 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
20ce6 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74     /* The collat
20ce7 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ing sequence of 
20ce8 70 45 78 70 72 20 2a 2f 0a 20 20 20 20 69 6e 74  pExpr */.    int
20ce9 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 20   termSortOrder; 
20cea 2f 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 66 6f  /* Sort order fo
20ceb 72 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20  r this term */. 
20cec 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20     int iColumn; 
20ced 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74        /* The i-t
20cee 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
20cef 69 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20 72  index.  -1 for r
20cf0 6f 77 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  owid */.    int 
20cf1 69 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 2f  iSortOrder;    /
20cf2 2a 20 31 20 66 6f 72 20 44 45 53 43 2c 20 30 20  * 1 for DESC, 0 
20cf3 66 6f 72 20 41 53 43 20 6f 6e 20 74 68 65 20 69  for ASC on the i
20cf4 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a  -th index term *
20cf5 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  /.    const char
20cf6 20 2a 7a 43 6f 6c 6c 3b 20 2f 2a 20 4e 61 6d 65   *zColl; /* Name
20cf7 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e   of the collatin
20cf8 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 69  g sequence for i
20cf9 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a  -th index term *
20cfa 2f 0a 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70  /..    pExpr = p
20cfb 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
20cfc 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
20cfd 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78  TK_COLUMN || pEx
20cfe 70 72 2d 3e 69 54 61 62 6c 65 21 3d 62 61 73 65  pr->iTable!=base
20cff 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 6e   ){.      /* Can
20d00 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65   not use an inde
20d01 78 20 73 6f 72 74 20 6f 6e 20 61 6e 79 74 68 69  x sort on anythi
20d02 6e 67 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61  ng that is not a
20d03 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 0a 20   column in the. 
20d04 20 20 20 20 20 2a 2a 20 6c 65 66 74 2d 6d 6f 73       ** left-mos
20d05 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 46  t table of the F
20d06 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
20d07 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
20d08 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
20d09 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
20d0a 70 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a  pParse, pExpr);.
20d0b 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
20d0c 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20  {.      pColl = 
20d0d 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20  db->pDfltColl;. 
20d0e 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 70     }.    if( i<p
20d0f 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  Idx->nColumn ){.
20d10 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20        iColumn = 
20d11 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  pIdx->aiColumn[i
20d12 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f  ];.      if( iCo
20d13 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54 61 62  lumn==pIdx->pTab
20d14 6c 65 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  le->iPKey ){.   
20d15 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d       iColumn = -
20d16 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
20d17 20 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 49   iSortOrder = pI
20d18 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69  dx->aSortOrder[i
20d19 5d 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d  ];.      zColl =
20d1a 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d   pIdx->azColl[i]
20d1b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
20d1c 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b     iColumn = -1;
20d1d 0a 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64 65  .      iSortOrde
20d1e 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 43 6f  r = 0;.      zCo
20d1f 6c 6c 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  ll = pColl->zNam
20d20 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
20d21 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21   pExpr->iColumn!
20d22 3d 69 43 6f 6c 75 6d 6e 20 7c 7c 20 73 71 6c 69  =iColumn || sqli
20d23 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c  te3StrICmp(pColl
20d24 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29 20  ->zName, zColl) 
20d25 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 65 72 6d  ){.      /* Term
20d26 20 6a 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20   j of the ORDER 
20d27 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e  BY clause does n
20d28 6f 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e 20  ot match column 
20d29 69 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 2a  i of the index *
20d2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 3c 6e 45  /.      if( i<nE
20d2b 71 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  qCol ){.        
20d2c 2f 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63  /* If an index c
20d2d 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 63 6f  olumn that is co
20d2e 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d 20  nstrained by == 
20d2f 66 61 69 6c 73 20 74 6f 20 6d 61 74 63 68 20 61  fails to match a
20d30 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44  n.        ** ORD
20d31 45 52 20 42 59 20 74 65 72 6d 2c 20 74 68 61 74  ER BY term, that
20d32 20 69 73 20 4f 4b 2e 20 20 4a 75 73 74 20 69 67   is OK.  Just ig
20d33 6e 6f 72 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e  nore that column
20d34 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 20 20   of the index.  
20d35 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
20d36 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
20d37 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
20d38 2f 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63  /* If an index c
20d39 6f 6c 75 6d 6e 20 66 61 69 6c 73 20 74 6f 20 6d  olumn fails to m
20d3a 61 74 63 68 20 61 6e 64 20 69 73 20 6e 6f 74 20  atch and is not 
20d3b 63 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d  constrained by =
20d3c 3d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  =.        ** the
20d3d 6e 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e  n the index cann
20d3e 6f 74 20 73 61 74 69 73 66 79 20 74 68 65 20 4f  ot satisfy the O
20d3f 52 44 45 52 20 42 59 20 63 6f 6e 73 74 72 61 69  RDER BY constrai
20d40 6e 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  nt..        */. 
20d41 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
20d42 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
20d43 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
20d44 3e 61 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29  >aSortOrder!=0 )
20d45 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
20d46 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d  erm->sortOrder==
20d47 30 20 7c 7c 20 70 54 65 72 6d 2d 3e 73 6f 72 74  0 || pTerm->sort
20d48 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20  Order==1 );.    
20d49 61 73 73 65 72 74 28 20 69 53 6f 72 74 4f 72 64  assert( iSortOrd
20d4a 65 72 3d 3d 30 20 7c 7c 20 69 53 6f 72 74 4f 72  er==0 || iSortOr
20d4b 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65  der==1 );.    te
20d4c 72 6d 53 6f 72 74 4f 72 64 65 72 20 3d 20 69 53  rmSortOrder = iS
20d4d 6f 72 74 4f 72 64 65 72 20 5e 20 70 54 65 72 6d  ortOrder ^ pTerm
20d4e 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  ->sortOrder;.   
20d4f 20 69 66 28 20 69 3e 6e 45 71 43 6f 6c 20 29 7b   if( i>nEqCol ){
20d50 0a 20 20 20 20 20 20 69 66 28 20 74 65 72 6d 53  .      if( termS
20d51 6f 72 74 4f 72 64 65 72 21 3d 73 6f 72 74 4f 72  ortOrder!=sortOr
20d52 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  der ){.        /
20d53 2a 20 49 6e 64 69 63 65 73 20 63 61 6e 20 6f 6e  * Indices can on
20d54 6c 79 20 62 65 20 75 73 65 64 20 69 66 20 61 6c  ly be used if al
20d55 6c 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73  l ORDER BY terms
20d56 20 70 61 73 74 20 74 68 65 0a 20 20 20 20 20 20   past the.      
20d57 20 20 2a 2a 20 65 71 75 61 6c 69 74 79 20 63 6f    ** equality co
20d58 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c  nstraints are al
20d59 6c 20 65 69 74 68 65 72 20 44 45 53 43 20 6f 72  l either DESC or
20d5a 20 41 53 43 2e 20 2a 2f 0a 20 20 20 20 20 20 20   ASC. */.       
20d5b 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
20d5c 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
20d5d 20 20 20 20 73 6f 72 74 4f 72 64 65 72 20 3d 20      sortOrder = 
20d5e 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 0a 20  termSortOrder;. 
20d5f 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20     }.    j++;.  
20d60 20 20 70 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 69    pTerm++;.    i
20d61 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20  f( iColumn<0 && 
20d62 21 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72  !referencesOther
20d63 54 61 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c  Tables(pOrderBy,
20d64 20 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61   pMaskSet, j, ba
20d65 73 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  se) ){.      /* 
20d66 49 66 20 74 68 65 20 69 6e 64 65 78 65 64 20 63  If the indexed c
20d67 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 70 72 69  olumn is the pri
20d68 6d 61 72 79 20 6b 65 79 20 61 6e 64 20 65 76 65  mary key and eve
20d69 72 79 74 68 69 6e 67 20 6d 61 74 63 68 65 73 0a  rything matches.
20d6a 20 20 20 20 20 20 2a 2a 20 73 6f 20 66 61 72 20        ** so far 
20d6b 61 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20  and none of the 
20d6c 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74  ORDER BY terms t
20d6d 6f 20 74 68 65 20 72 69 67 68 74 20 72 65 66 65  o the right refe
20d6e 72 65 6e 63 65 20 6f 74 68 65 72 0a 20 20 20 20  rence other.    
20d6f 20 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74    ** tables in t
20d70 68 65 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 77 65  he join, then we
20d71 20 61 72 65 20 61 73 73 75 72 65 64 20 74 68 61   are assured tha
20d72 74 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 20  t the index can 
20d73 62 65 20 75 73 65 64 20 0a 20 20 20 20 20 20 2a  be used .      *
20d74 2a 20 74 6f 20 73 6f 72 74 20 62 65 63 61 75 73  * to sort becaus
20d75 65 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65  e the primary ke
20d76 79 20 69 73 20 75 6e 69 71 75 65 20 61 6e 64 20  y is unique and 
20d77 73 6f 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 6f  so none of the o
20d78 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 63 6f  ther.      ** co
20d79 6c 75 6d 6e 73 20 77 69 6c 6c 20 6d 61 6b 65 20  lumns will make 
20d7a 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65 0a 20  any difference. 
20d7b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a 20       */.      j 
20d7c 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20  = nTerm;.    }. 
20d7d 20 7d 0a 0a 20 20 2a 70 62 52 65 76 20 3d 20 73   }..  *pbRev = s
20d7e 6f 72 74 4f 72 64 65 72 21 3d 30 3b 0a 20 20 69  ortOrder!=0;.  i
20d7f 66 28 20 6a 3e 3d 6e 54 65 72 6d 20 29 7b 0a 20  f( j>=nTerm ){. 
20d80 20 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20     /* All terms 
20d81 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
20d82 63 6c 61 75 73 65 20 61 72 65 20 63 6f 76 65 72  clause are cover
20d83 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78  ed by this index
20d84 20 73 6f 0a 20 20 20 20 2a 2a 20 74 68 69 73 20   so.    ** this 
20d85 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65  index can be use
20d86 64 20 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20 2a  d for sorting. *
20d87 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  /.    return 1;.
20d88 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 2d 3e    }.  if( pIdx->
20d89 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
20d8a 20 26 26 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f   && i==pIdx->nCo
20d8b 6c 75 6d 6e 0a 20 20 20 20 20 20 26 26 20 21 72  lumn.      && !r
20d8c 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61  eferencesOtherTa
20d8d 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20 70  bles(pOrderBy, p
20d8e 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65  MaskSet, j, base
20d8f 29 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20  ) ){.    /* All 
20d90 74 65 72 6d 73 20 6f 66 20 74 68 69 73 20 69 6e  terms of this in
20d91 64 65 78 20 6d 61 74 63 68 20 73 6f 6d 65 20 70  dex match some p
20d92 72 65 66 69 78 20 6f 66 20 74 68 65 20 4f 52 44  refix of the ORD
20d93 45 52 20 42 59 20 63 6c 61 75 73 65 0a 20 20 20  ER BY clause.   
20d94 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64 65   ** and the inde
20d95 78 20 69 73 20 55 4e 49 51 55 45 20 61 6e 64 20  x is UNIQUE and 
20d96 6e 6f 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20  no terms on the 
20d97 74 61 69 6c 20 6f 66 20 74 68 65 20 4f 52 44 45  tail of the ORDE
20d98 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75  R BY.    ** clau
20d99 73 65 20 72 65 66 65 72 65 6e 63 65 20 6f 74 68  se reference oth
20d9a 65 72 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a  er tables in a j
20d9b 6f 69 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73  oin.  If this is
20d9c 20 61 6c 6c 20 74 72 75 65 20 74 68 65 6e 0a 20   all true then. 
20d9d 20 20 20 2a 2a 20 74 68 65 20 6f 72 64 65 72 20     ** the order 
20d9e 62 79 20 63 6c 61 75 73 65 20 69 73 20 73 75 70  by clause is sup
20d9f 65 72 66 6c 75 6f 75 73 2e 20 2a 2f 0a 20 20 20  erfluous. */.   
20da0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
20da1 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
20da2 0a 2a 2a 20 43 68 65 63 6b 20 74 61 62 6c 65 20  .** Check table 
20da3 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 4f 52  to see if the OR
20da4 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e  DER BY clause in
20da5 20 70 4f 72 64 65 72 42 79 20 63 61 6e 20 62 65   pOrderBy can be
20da6 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79   satisfied.** by
20da7 20 73 6f 72 74 69 6e 67 20 69 6e 20 6f 72 64 65   sorting in orde
20da8 72 20 6f 66 20 52 4f 57 49 44 2e 20 20 52 65 74  r of ROWID.  Ret
20da9 75 72 6e 20 74 72 75 65 20 69 66 20 73 6f 20 61  urn true if so a
20daa 6e 64 20 73 65 74 20 2a 70 62 52 65 76 20 74 6f  nd set *pbRev to
20dab 20 62 65 0a 2a 2a 20 74 72 75 65 20 66 6f 72 20   be.** true for 
20dac 72 65 76 65 72 73 65 20 52 4f 57 49 44 20 61 6e  reverse ROWID an
20dad 64 20 66 61 6c 73 65 20 66 6f 72 20 66 6f 72 77  d false for forw
20dae 61 72 64 20 52 4f 57 49 44 20 6f 72 64 65 72 2e  ard ROWID order.
20daf 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
20db0 6f 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28 0a  ortableByRowid(.
20db1 20 20 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20    int base,     
20db2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
20db3 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
20db4 61 62 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65  able to be sorte
20db5 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  d */.  ExprList 
20db6 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f  *pOrderBy,     /
20db7 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
20db8 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4d  lause */.  ExprM
20db9 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
20dba 2c 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72  ,  /* Mapping fr
20dbb 6f 6d 20 74 61 62 6c 65 73 20 74 6f 20 62 69 74  om tables to bit
20dbc 6d 61 70 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  maps */.  int *p
20dbd 62 52 65 76 20 20 20 20 20 20 20 20 20 20 20 20  bRev            
20dbe 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 69 66    /* Set to 1 if
20dbf 20 4f 52 44 45 52 20 42 59 20 69 73 20 44 45 53   ORDER BY is DES
20dc0 43 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a  C */.){.  Expr *
20dc1 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  p;..  assert( pO
20dc2 72 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 61  rderBy!=0 );.  a
20dc3 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d  ssert( pOrderBy-
20dc4 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 70 20  >nExpr>0 );.  p 
20dc5 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d  = pOrderBy->a[0]
20dc6 2e 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70 2d  .pExpr;.  if( p-
20dc7 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26  >op==TK_COLUMN &
20dc8 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 62 61 73  & p->iTable==bas
20dc9 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d  e && p->iColumn=
20dca 3d 2d 31 0a 20 20 20 20 26 26 20 21 72 65 66 65  =-1.    && !refe
20dcb 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65  rencesOtherTable
20dcc 73 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73  s(pOrderBy, pMas
20dcd 6b 53 65 74 2c 20 31 2c 20 62 61 73 65 29 20 29  kSet, 1, base) )
20dce 7b 0a 20 20 20 20 2a 70 62 52 65 76 20 3d 20 70  {.    *pbRev = p
20dcf 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 73 6f  OrderBy->a[0].so
20dd0 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 72 65 74  rtOrder;.    ret
20dd1 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 1;.  }.  ret
20dd2 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
20dd3 50 72 65 70 61 72 65 20 61 20 63 72 75 64 65 20  Prepare a crude 
20dd4 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
20dd5 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20 74 68 65  logarithm of the
20dd6 20 69 6e 70 75 74 20 76 61 6c 75 65 2e 0a 2a 2a   input value..**
20dd7 20 54 68 65 20 72 65 73 75 6c 74 73 20 6e 65 65   The results nee
20dd8 64 20 6e 6f 74 20 62 65 20 65 78 61 63 74 2e 20  d not be exact. 
20dd9 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73   This is only us
20dda 65 64 20 66 6f 72 20 65 73 74 69 6d 61 74 69 6e  ed for estimatin
20ddb 67 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 63  g.** the total c
20ddc 6f 73 74 20 6f 66 20 70 65 72 66 6f 72 6d 69 6e  ost of performin
20ddd 67 20 6f 70 65 72 61 74 69 6e 67 73 20 77 69 74  g operatings wit
20dde 68 20 4f 28 6c 6f 67 4e 29 20 6f 72 20 4f 28 4e  h O(logN) or O(N
20ddf 6c 6f 67 4e 29 0a 2a 2a 20 63 6f 6d 70 6c 65 78  logN).** complex
20de0 69 74 79 2e 20 20 42 65 63 61 75 73 65 20 4e 20  ity.  Because N 
20de1 69 73 20 6a 75 73 74 20 61 20 67 75 65 73 73 2c  is just a guess,
20de2 20 69 74 20 69 73 20 6e 6f 20 67 72 65 61 74 20   it is no great 
20de3 74 72 61 67 65 64 79 20 69 66 0a 2a 2a 20 6c 6f  tragedy if.** lo
20de4 67 4e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6f  gN is a little o
20de5 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f  ff..*/.static do
20de6 75 62 6c 65 20 65 73 74 4c 6f 67 28 64 6f 75 62  uble estLog(doub
20de7 6c 65 20 4e 29 7b 0a 20 20 64 6f 75 62 6c 65 20  le N){.  double 
20de8 6c 6f 67 4e 20 3d 20 31 3b 0a 20 20 64 6f 75 62  logN = 1;.  doub
20de9 6c 65 20 78 20 3d 20 31 30 3b 0a 20 20 77 68 69  le x = 10;.  whi
20dea 6c 65 28 20 4e 3e 78 20 29 7b 0a 20 20 20 20 6c  le( N>x ){.    l
20deb 6f 67 4e 20 2b 3d 20 31 3b 0a 20 20 20 20 78 20  ogN += 1;.    x 
20dec 2a 3d 20 31 30 3b 0a 20 20 7d 0a 20 20 72 65 74  *= 10;.  }.  ret
20ded 75 72 6e 20 6c 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a  urn logN;.}../*.
20dee 2a 2a 20 54 77 6f 20 72 6f 75 74 69 6e 65 73 20  ** Two routines 
20def 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 74 68 65  for printing the
20df0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 73   content of an s
20df1 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
20df2 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  o.** structure. 
20df3 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   Used for testin
20df4 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20  g and debugging 
20df5 6f 6e 6c 79 2e 20 20 49 66 20 6e 65 69 74 68 65  only.  If neithe
20df6 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  r.** SQLITE_TEST
20df7 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 42 55 47   or SQLITE_DEBUG
20df8 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68   are defined, th
20df9 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  en these routine
20dfa 73 0a 2a 2a 20 61 72 65 20 6e 6f 2d 6f 70 73 2e  s.** are no-ops.
20dfb 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
20dfc 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52  (SQLITE_OMIT_VIR
20dfd 54 55 41 4c 54 41 42 4c 45 29 20 26 26 20 64 65  TUALTABLE) && de
20dfe 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
20dff 55 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20  UG).static void 
20e00 54 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53  TRACE_IDX_INPUTS
20e01 28 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69  (sqlite3_index_i
20e02 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  nfo *p){.  int i
20e03 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
20e04 5f 77 68 65 72 65 5f 74 72 61 63 65 20 29 20 72  _where_trace ) r
20e05 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30  eturn;.  for(i=0
20e06 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69  ; i<p->nConstrai
20e07 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  nt; i++){.    sq
20e08 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
20e09 28 22 20 20 63 6f 6e 73 74 72 61 69 6e 74 5b 25  ("  constraint[%
20e0a 64 5d 3a 20 63 6f 6c 3d 25 64 20 74 65 72 6d 69  d]: col=%d termi
20e0b 64 3d 25 64 20 6f 70 3d 25 64 20 75 73 61 62 6c  d=%d op=%d usabl
20e0c 65 64 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  ed=%d\n",.      
20e0d 20 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43   i,.       p->aC
20e0e 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69 43 6f  onstraint[i].iCo
20e0f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 70 2d 3e  lumn,.       p->
20e10 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69  aConstraint[i].i
20e11 54 65 72 6d 4f 66 66 73 65 74 2c 0a 20 20 20 20  TermOffset,.    
20e12 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e     p->aConstrain
20e13 74 5b 69 5d 2e 6f 70 2c 0a 20 20 20 20 20 20 20  t[i].op,.       
20e14 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
20e15 5d 2e 75 73 61 62 6c 65 29 3b 0a 20 20 7d 0a 20  ].usable);.  }. 
20e16 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
20e17 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20  OrderBy; i++){. 
20e18 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
20e19 72 69 6e 74 66 28 22 20 20 6f 72 64 65 72 62 79  rintf("  orderby
20e1a 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64 20 64 65 73  [%d]: col=%d des
20e1b 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  c=%d\n",.       
20e1c 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f 72  i,.       p->aOr
20e1d 64 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e  derBy[i].iColumn
20e1e 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f 72 64  ,.       p->aOrd
20e1f 65 72 42 79 5b 69 5d 2e 64 65 73 63 29 3b 0a 20  erBy[i].desc);. 
20e20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
20e21 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55   TRACE_IDX_OUTPU
20e22 54 53 28 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  TS(sqlite3_index
20e23 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74  _info *p){.  int
20e24 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74   i;.  if( !sqlit
20e25 65 33 5f 77 68 65 72 65 5f 74 72 61 63 65 20 29  e3_where_trace )
20e26 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
20e27 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72  =0; i<p->nConstr
20e28 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  aint; i++){.    
20e29 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
20e2a 74 66 28 22 20 20 75 73 61 67 65 5b 25 64 5d 3a  tf("  usage[%d]:
20e2b 20 61 72 67 76 49 64 78 3d 25 64 20 6f 6d 69 74   argvIdx=%d omit
20e2c 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69  =%d\n",.       i
20e2d 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
20e2e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
20e2f 61 72 67 76 49 6e 64 65 78 2c 0a 20 20 20 20 20  argvIndex,.     
20e30 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74    p->aConstraint
20e31 55 73 61 67 65 5b 69 5d 2e 6f 6d 69 74 29 3b 0a  Usage[i].omit);.
20e32 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 62    }.  sqlite3Deb
20e33 75 67 50 72 69 6e 74 66 28 22 20 20 69 64 78 4e  ugPrintf("  idxN
20e34 75 6d 3d 25 64 5c 6e 22 2c 20 70 2d 3e 69 64 78  um=%d\n", p->idx
20e35 4e 75 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Num);.  sqlite3D
20e36 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 69 64  ebugPrintf("  id
20e37 78 53 74 72 3d 25 73 5c 6e 22 2c 20 70 2d 3e 69  xStr=%s\n", p->i
20e38 64 78 53 74 72 29 3b 0a 20 20 73 71 6c 69 74 65  dxStr);.  sqlite
20e39 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20  3DebugPrintf("  
20e3a 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3d  orderByConsumed=
20e3b 25 64 5c 6e 22 2c 20 70 2d 3e 6f 72 64 65 72 42  %d\n", p->orderB
20e3c 79 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 73 71  yConsumed);.  sq
20e3d 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
20e3e 28 22 20 20 65 73 74 69 6d 61 74 65 64 43 6f 73  ("  estimatedCos
20e3f 74 3d 25 67 5c 6e 22 2c 20 70 2d 3e 65 73 74 69  t=%g\n", p->esti
20e40 6d 61 74 65 64 43 6f 73 74 29 3b 0a 7d 0a 23 65  matedCost);.}.#e
20e41 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 52 41 43  lse.#define TRAC
20e42 45 5f 49 44 58 5f 49 4e 50 55 54 53 28 41 29 0a  E_IDX_INPUTS(A).
20e43 23 64 65 66 69 6e 65 20 54 52 41 43 45 5f 49 44  #define TRACE_ID
20e44 58 5f 4f 55 54 50 55 54 53 28 41 29 0a 23 65 6e  X_OUTPUTS(A).#en
20e45 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
20e46 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
20e47 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  TABLE./*.** Comp
20e48 75 74 65 20 74 68 65 20 62 65 73 74 20 69 6e 64  ute the best ind
20e49 65 78 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c  ex for a virtual
20e4a 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
20e4b 65 20 62 65 73 74 20 69 6e 64 65 78 20 69 73 20  e best index is 
20e4c 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20  computed by the 
20e4d 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f  xBestIndex metho
20e4e 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
20e4f 0a 2a 2a 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  .** table module
20e50 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
20e51 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61  is really just a
20e52 20 77 72 61 70 70 65 72 20 74 68 61 74 20 73 65   wrapper that se
20e53 74 73 20 75 70 0a 2a 2a 20 74 68 65 20 73 71 6c  ts up.** the sql
20e54 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
20e55 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69  structure that i
20e56 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e  s used to commun
20e57 69 63 61 74 65 20 77 69 74 68 0a 2a 2a 20 78 42  icate with.** xB
20e58 65 73 74 49 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  estIndex..**.** 
20e59 49 6e 20 61 20 6a 6f 69 6e 2c 20 74 68 69 73 20  In a join, this 
20e5a 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 62 65  routine might be
20e5b 20 63 61 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65   called multiple
20e5c 20 74 69 6d 65 73 20 66 6f 72 20 74 68 65 0a 2a   times for the.*
20e5d 2a 20 73 61 6d 65 20 76 69 72 74 75 61 6c 20 74  * same virtual t
20e5e 61 62 6c 65 2e 20 20 54 68 65 20 73 71 6c 69 74  able.  The sqlit
20e5f 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
20e60 72 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74  ructure is creat
20e61 65 64 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61  ed.** and initia
20e62 6c 69 7a 65 64 20 6f 6e 20 74 68 65 20 66 69 72  lized on the fir
20e63 73 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e  st invocation an
20e64 64 20 72 65 75 73 65 64 20 6f 6e 20 61 6c 6c 20  d reused on all 
20e65 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e  subsequent.** in
20e66 76 6f 63 61 74 69 6f 6e 73 2e 20 20 54 68 65 20  vocations.  The 
20e67 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
20e68 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
20e69 61 6c 73 6f 20 75 73 65 64 20 77 68 65 6e 0a 2a  also used when.*
20e6a 2a 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61  * code is genera
20e6b 74 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  ted to access th
20e6c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
20e6d 20 20 54 68 65 20 77 68 65 72 65 49 6e 66 6f 44    The whereInfoD
20e6e 65 6c 65 74 65 28 29 20 0a 2a 2a 20 72 6f 75 74  elete() .** rout
20e6f 69 6e 65 20 74 61 6b 65 73 20 63 61 72 65 20 6f  ine takes care o
20e70 66 20 66 72 65 65 69 6e 67 20 74 68 65 20 73 71  f freeing the sq
20e71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
20e72 20 73 74 72 75 63 74 75 72 65 20 61 66 74 65 72   structure after
20e73 0a 2a 2a 20 65 76 65 72 79 62 6f 64 79 20 68 61  .** everybody ha
20e74 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20  s finished with 
20e75 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f  it..*/.static do
20e76 75 62 6c 65 20 62 65 73 74 56 69 72 74 75 61 6c  uble bestVirtual
20e77 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a  Index(.  Parse *
20e78 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
20e79 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
20e7a 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
20e7b 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
20e7c 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20  *pWC,           
20e7d 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
20e7e 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75  clause */.  stru
20e7f 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
20e80 2a 70 53 72 63 2c 20 20 20 20 20 2f 2a 20 54 68  *pSrc,     /* Th
20e81 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65  e FROM clause te
20e82 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a  rm to search */.
20e83 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
20e84 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  dy,             
20e85 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73   /* Mask of curs
20e86 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74  ors that are not
20e87 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20   available */.  
20e88 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
20e89 42 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  By,            /
20e8a 2a 20 54 68 65 20 6f 72 64 65 72 20 62 79 20 63  * The order by c
20e8b 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  lause */.  int o
20e8c 72 64 65 72 42 79 55 73 61 62 6c 65 2c 20 20 20  rderByUsable,   
20e8d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
20e8e 65 20 69 66 20 77 65 20 63 61 6e 20 70 6f 74 65  e if we can pote
20e8f 6e 74 69 61 6c 20 73 6f 72 74 20 2a 2f 0a 20 20  ntial sort */.  
20e90 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
20e91 66 6f 20 2a 2a 70 70 49 64 78 49 6e 66 6f 20 2f  fo **ppIdxInfo /
20e92 2a 20 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74  * Index informat
20e93 69 6f 6e 20 70 61 73 73 65 64 20 74 6f 20 78 42  ion passed to xB
20e94 65 73 74 49 6e 64 65 78 20 2a 2f 0a 29 7b 0a 20  estIndex */.){. 
20e95 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
20e96 53 72 63 2d 3e 70 54 61 62 3b 0a 20 20 73 71 6c  Src->pTab;.  sql
20e97 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
20e98 2a 70 49 64 78 49 6e 66 6f 3b 0a 20 20 73 74 72  *pIdxInfo;.  str
20e99 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
20e9a 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 49  x_constraint *pI
20e9b 64 78 43 6f 6e 73 3b 0a 20 20 73 74 72 75 63 74  dxCons;.  struct
20e9c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f   sqlite3_index_o
20e9d 72 64 65 72 62 79 20 2a 70 49 64 78 4f 72 64 65  rderby *pIdxOrde
20e9e 72 42 79 3b 0a 20 20 73 74 72 75 63 74 20 73 71  rBy;.  struct sq
20e9f 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
20ea0 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 70 55  traint_usage *pU
20ea1 73 61 67 65 3b 0a 20 20 57 68 65 72 65 54 65 72  sage;.  WhereTer
20ea2 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20  m *pTerm;.  int 
20ea3 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 4f 72 64  i, j;.  int nOrd
20ea4 65 72 42 79 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  erBy;.  int rc;.
20ea5 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 71 6c  .  /* If the sql
20ea6 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
20ea7 73 74 72 75 63 74 75 72 65 20 68 61 73 20 6e 6f  structure has no
20ea8 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c  t been previousl
20ea9 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  y.  ** allocated
20eaa 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64   and initialized
20eab 20 66 6f 72 20 74 68 69 73 20 76 69 72 74 75 61   for this virtua
20eac 6c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 61 6c  l table, then al
20ead 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 61 6e 64 20  locate.  ** and 
20eae 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 20 6e 6f  initialize it no
20eaf 77 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66  w.  */.  pIdxInf
20eb0 6f 20 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a  o = *ppIdxInfo;.
20eb1 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d    if( pIdxInfo==
20eb2 30 20 29 7b 0a 20 20 20 20 57 68 65 72 65 54 65  0 ){.    WhereTe
20eb3 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 20 20 69  rm *pTerm;.    i
20eb4 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 20 20 57 48  nt nTerm;.    WH
20eb5 45 52 45 54 52 41 43 45 28 28 22 52 65 63 6f 6d  ERETRACE(("Recom
20eb6 70 75 74 69 6e 67 20 69 6e 64 65 78 20 69 6e 66  puting index inf
20eb7 6f 20 66 6f 72 20 25 73 2e 2e 2e 5c 6e 22 2c 20  o for %s...\n", 
20eb8 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a  pTab->zName));..
20eb9 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65      /* Count the
20eba 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73 73 69   number of possi
20ebb 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  ble WHERE clause
20ebc 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 65 66   constraints ref
20ebd 65 72 72 69 6e 67 0a 20 20 20 20 2a 2a 20 74 6f  erring.    ** to
20ebe 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 74 61   this virtual ta
20ebf 62 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  ble */.    for(i
20ec0 3d 6e 54 65 72 6d 3d 30 2c 20 70 54 65 72 6d 3d  =nTerm=0, pTerm=
20ec1 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e  pWC->a; i<pWC->n
20ec2 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d  Term; i++, pTerm
20ec3 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
20ec4 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
20ec5 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f   != pSrc->iCurso
20ec6 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  r ) continue;.  
20ec7 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65      if( pTerm->e
20ec8 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20  Operator==WO_IN 
20ec9 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
20eca 20 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 7d    nTerm++;.    }
20ecb 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
20ecc 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
20ecd 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 63 6f  contains only co
20ece 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 75 72  lumns in the cur
20ecf 72 65 6e 74 20 0a 20 20 20 20 2a 2a 20 76 69 72  rent .    ** vir
20ed0 74 75 61 6c 20 74 61 62 6c 65 20 74 68 65 6e 20  tual table then 
20ed1 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  allocate space f
20ed2 6f 72 20 74 68 65 20 61 4f 72 64 65 72 42 79 20  or the aOrderBy 
20ed3 70 61 72 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74  part of.    ** t
20ed4 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
20ed5 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  _info structure.
20ed6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 4f 72 64  .    */.    nOrd
20ed7 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 69 66  erBy = 0;.    if
20ed8 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
20ed9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
20eda 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20  OrderBy->nExpr; 
20edb 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 45 78  i++){.        Ex
20edc 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64  pr *pExpr = pOrd
20edd 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  erBy->a[i].pExpr
20ede 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45  ;.        if( pE
20edf 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
20ee0 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61  MN || pExpr->iTa
20ee1 62 6c 65 21 3d 70 53 72 63 2d 3e 69 43 75 72 73  ble!=pSrc->iCurs
20ee2 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  or ) break;.    
20ee3 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3d    }.      if( i=
20ee4 3d 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  =pOrderBy->nExpr
20ee5 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 4f 72 64   ){.        nOrd
20ee6 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d  erBy = pOrderBy-
20ee7 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a  >nExpr;.      }.
20ee8 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c      }..    /* Al
20ee9 6c 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74  locate the sqlit
20eea 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
20eeb 72 75 63 74 75 72 65 0a 20 20 20 20 2a 2f 0a 20  ructure.    */. 
20eec 20 20 20 70 49 64 78 49 6e 66 6f 20 3d 20 73 71     pIdxInfo = sq
20eed 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
20eee 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69  o(pParse->db, si
20eef 7a 65 6f 66 28 2a 70 49 64 78 49 6e 66 6f 29 0a  zeof(*pIdxInfo).
20ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ef1 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 28               + (
20ef2 73 69 7a 65 6f 66 28 2a 70 49 64 78 43 6f 6e 73  sizeof(*pIdxCons
20ef3 29 20 2b 20 73 69 7a 65 6f 66 28 2a 70 55 73 61  ) + sizeof(*pUsa
20ef4 67 65 29 29 2a 6e 54 65 72 6d 0a 20 20 20 20 20  ge))*nTerm.     
20ef5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ef6 20 20 20 20 20 20 20 20 2b 20 73 69 7a 65 6f 66          + sizeof
20ef7 28 2a 70 49 64 78 4f 72 64 65 72 42 79 29 2a 6e  (*pIdxOrderBy)*n
20ef8 4f 72 64 65 72 42 79 20 29 3b 0a 20 20 20 20 69  OrderBy );.    i
20ef9 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29  f( pIdxInfo==0 )
20efa 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
20efb 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
20efc 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29  "out of memory")
20efd 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
20efe 2e 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  .0;.    }.    *p
20eff 70 49 64 78 49 6e 66 6f 20 3d 20 70 49 64 78 49  pIdxInfo = pIdxI
20f00 6e 66 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69  nfo;..    /* Ini
20f01 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 72 75  tialize the stru
20f02 63 74 75 72 65 2e 20 20 54 68 65 20 73 71 6c 69  cture.  The sqli
20f03 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
20f04 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
20f05 73 0a 20 20 20 20 2a 2a 20 6d 61 6e 79 20 66 69  s.    ** many fi
20f06 65 6c 64 73 20 74 68 61 74 20 61 72 65 20 64 65  elds that are de
20f07 63 6c 61 72 65 64 20 22 63 6f 6e 73 74 22 20 74  clared "const" t
20f08 6f 20 70 72 65 76 65 6e 74 20 78 42 65 73 74 49  o prevent xBestI
20f09 6e 64 65 78 20 66 72 6f 6d 0a 20 20 20 20 2a 2a  ndex from.    **
20f0a 20 63 68 61 6e 67 69 6e 67 20 74 68 65 6d 2e 20   changing them. 
20f0b 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73   We have to do s
20f0c 6f 6d 65 20 66 75 6e 6b 79 20 63 61 73 74 69 6e  ome funky castin
20f0d 67 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 20 20  g in order to.  
20f0e 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20    ** initialize 
20f0f 74 68 6f 73 65 20 66 69 65 6c 64 73 2e 0a 20 20  those fields..  
20f10 20 20 2a 2f 0a 20 20 20 20 70 49 64 78 43 6f 6e    */.    pIdxCon
20f11 73 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69  s = (struct sqli
20f12 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
20f13 61 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 5b  aint*)&pIdxInfo[
20f14 31 5d 3b 0a 20 20 20 20 70 49 64 78 4f 72 64 65  1];.    pIdxOrde
20f15 72 42 79 20 3d 20 28 73 74 72 75 63 74 20 73 71  rBy = (struct sq
20f16 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65  lite3_index_orde
20f17 72 62 79 2a 29 26 70 49 64 78 43 6f 6e 73 5b 6e  rby*)&pIdxCons[n
20f18 54 65 72 6d 5d 3b 0a 20 20 20 20 70 55 73 61 67  Term];.    pUsag
20f19 65 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69  e = (struct sqli
20f1a 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
20f1b 61 69 6e 74 5f 75 73 61 67 65 2a 29 26 70 49 64  aint_usage*)&pId
20f1c 78 4f 72 64 65 72 42 79 5b 6e 4f 72 64 65 72 42  xOrderBy[nOrderB
20f1d 79 5d 3b 0a 20 20 20 20 2a 28 69 6e 74 2a 29 26  y];.    *(int*)&
20f1e 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74  pIdxInfo->nConst
20f1f 72 61 69 6e 74 20 3d 20 6e 54 65 72 6d 3b 0a 20  raint = nTerm;. 
20f20 20 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49     *(int*)&pIdxI
20f21 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20  nfo->nOrderBy = 
20f22 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 2a 28  nOrderBy;.    *(
20f23 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
20f24 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a  ndex_constraint*
20f25 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f  *)&pIdxInfo->aCo
20f26 6e 73 74 72 61 69 6e 74 20 3d 20 70 49 64 78 43  nstraint = pIdxC
20f27 6f 6e 73 3b 0a 20 20 20 20 2a 28 73 74 72 75 63  ons;.    *(struc
20f28 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
20f29 6f 72 64 65 72 62 79 2a 2a 29 26 70 49 64 78 49  orderby**)&pIdxI
20f2a 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79 20 3d 20  nfo->aOrderBy = 
20f2b 70 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 20  pIdxOrderBy;.   
20f2c 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65   *(struct sqlite
20f2d 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
20f2e 6e 74 5f 75 73 61 67 65 2a 2a 29 26 70 49 64 78  nt_usage**)&pIdx
20f2f 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
20f30 74 55 73 61 67 65 20 3d 0a 20 20 20 20 20 20 20  tUsage =.       
20f31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 55                pU
20f35 73 61 67 65 3b 0a 0a 20 20 20 20 66 6f 72 28 69  sage;..    for(i
20f36 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43 2d  =j=0, pTerm=pWC-
20f37 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72 6d  >a; i<pWC->nTerm
20f38 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; i++, pTerm++){
20f39 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
20f3a 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d 20  ->leftCursor != 
20f3b 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20  pSrc->iCursor ) 
20f3c 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
20f3d 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
20f3e 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 20 63 6f  ator==WO_IN ) co
20f3f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 49  ntinue;.      pI
20f40 64 78 43 6f 6e 73 5b 6a 5d 2e 69 43 6f 6c 75 6d  dxCons[j].iColum
20f41 6e 20 3d 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43  n = pTerm->leftC
20f42 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 49 64  olumn;.      pId
20f43 78 43 6f 6e 73 5b 6a 5d 2e 69 54 65 72 6d 4f 66  xCons[j].iTermOf
20f44 66 73 65 74 20 3d 20 69 3b 0a 20 20 20 20 20 20  fset = i;.      
20f45 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d  pIdxCons[j].op =
20f46 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
20f47 72 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  r;.      /* The 
20f48 64 69 72 65 63 74 20 61 73 73 69 67 6e 6d 65 6e  direct assignmen
20f49 74 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75  t in the previou
20f4a 73 20 6c 69 6e 65 20 69 73 20 70 6f 73 73 69 62  s line is possib
20f4b 6c 65 20 6f 6e 6c 79 20 62 65 63 61 75 73 65 0a  le only because.
20f4c 20 20 20 20 20 20 2a 2a 20 74 68 65 20 57 4f 5f        ** the WO_
20f4d 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4e 44 45   and SQLITE_INDE
20f4e 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 20 63 6f  X_CONSTRAINT_ co
20f4f 64 65 73 20 61 72 65 20 69 64 65 6e 74 69 63 61  des are identica
20f50 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a  l.  The.      **
20f51 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72   following asser
20f52 74 73 20 76 65 72 69 66 79 20 74 68 69 73 20 66  ts verify this f
20f53 61 63 74 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73  act. */.      as
20f54 73 65 72 74 28 20 57 4f 5f 45 51 3d 3d 53 51 4c  sert( WO_EQ==SQL
20f55 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
20f56 41 49 4e 54 5f 45 51 20 29 3b 0a 20 20 20 20 20  AINT_EQ );.     
20f57 20 61 73 73 65 72 74 28 20 57 4f 5f 4c 54 3d 3d   assert( WO_LT==
20f58 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
20f59 53 54 52 41 49 4e 54 5f 4c 54 20 29 3b 0a 20 20  STRAINT_LT );.  
20f5a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4c      assert( WO_L
20f5b 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  E==SQLITE_INDEX_
20f5c 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 29 3b  CONSTRAINT_LE );
20f5d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 57  .      assert( W
20f5e 4f 5f 47 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  O_GT==SQLITE_IND
20f5f 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54  EX_CONSTRAINT_GT
20f60 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
20f61 28 20 57 4f 5f 47 45 3d 3d 53 51 4c 49 54 45 5f  ( WO_GE==SQLITE_
20f62 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
20f63 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _GE );.      ass
20f64 65 72 74 28 20 57 4f 5f 4d 41 54 43 48 3d 3d 53  ert( WO_MATCH==S
20f65 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
20f66 54 52 41 49 4e 54 5f 4d 41 54 43 48 20 29 3b 0a  TRAINT_MATCH );.
20f67 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
20f68 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
20f69 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c 57 4f   (WO_EQ|WO_LT|WO
20f6a 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 7c  _LE|WO_GT|WO_GE|
20f6b 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a 20 20 20  WO_MATCH) );.   
20f6c 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     j++;.    }.  
20f6d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72    for(i=0; i<nOr
20f6e 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20  derBy; i++){.   
20f6f 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d     Expr *pExpr =
20f70 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e   pOrderBy->a[i].
20f71 70 45 78 70 72 3b 0a 20 20 20 20 20 20 70 49 64  pExpr;.      pId
20f72 78 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43 6f 6c  xOrderBy[i].iCol
20f73 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  umn = pExpr->iCo
20f74 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 49 64 78  lumn;.      pIdx
20f75 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73 63 20  OrderBy[i].desc 
20f76 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d  = pOrderBy->a[i]
20f77 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  .sortOrder;.    
20f78 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74  }.  }..  /* At t
20f79 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 20 73  his point, the s
20f7a 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
20f7b 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
20f7c 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 73   pIdxInfo points
20f7d 0a 20 20 2a 2a 20 74 6f 20 77 69 6c 6c 20 68 61  .  ** to will ha
20f7e 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  ve been initiali
20f7f 7a 65 64 2c 20 65 69 74 68 65 72 20 64 75 72 69  zed, either duri
20f80 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 69  ng the current i
20f81 6e 76 6f 63 61 74 69 6f 6e 20 6f 72 0a 20 20 2a  nvocation or.  *
20f82 2a 20 64 75 72 69 6e 67 20 73 6f 6d 65 20 70 72  * during some pr
20f83 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20  ior invocation. 
20f84 20 4e 6f 77 20 77 65 20 6a 75 73 74 20 68 61 76   Now we just hav
20f85 65 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74  e to customize t
20f86 68 65 0a 20 20 2a 2a 20 64 65 74 61 69 6c 73 20  he.  ** details 
20f87 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72 20  of pIdxInfo for 
20f88 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e 76 6f  the current invo
20f89 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 73 73 20  cation and pass 
20f8a 69 74 20 74 6f 0a 20 20 2a 2a 20 78 42 65 73 74  it to.  ** xBest
20f8b 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a 0a 20 20 2f  Index..  */..  /
20f8c 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  * The module nam
20f8d 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65  e must be define
20f8e 64 2e 20 41 6c 73 6f 2c 20 62 79 20 74 68 69 73  d. Also, by this
20f8f 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 75 73   point there mus
20f90 74 0a 20 20 2a 2a 20 62 65 20 61 20 70 6f 69 6e  t.  ** be a poin
20f91 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65  ter to an sqlite
20f92 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
20f93 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20 2a 2a  . Otherwise.  **
20f94 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
20f95 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 77 6f 75  olumnNames() wou
20f96 6c 64 20 68 61 76 65 20 70 69 63 6b 65 64 20 75  ld have picked u
20f97 70 20 74 68 65 20 65 72 72 6f 72 2e 20 0a 20 20  p the error. .  
20f98 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  */.  assert( pTa
20f99 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 26  b->azModuleArg &
20f9a 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  & pTab->azModule
20f9b 41 72 67 5b 30 5d 20 29 3b 0a 20 20 61 73 73 65  Arg[0] );.  asse
20f9c 72 74 28 20 70 54 61 62 2d 3e 70 56 74 61 62 20  rt( pTab->pVtab 
20f9d 29 3b 0a 23 69 66 20 30 0a 20 20 69 66 28 20 70  );.#if 0.  if( p
20f9e 54 61 62 2d 3e 70 56 74 61 62 3d 3d 30 20 29 7b  Tab->pVtab==0 ){
20f9f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
20fa0 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e  rMsg(pParse, "un
20fa1 64 65 66 69 6e 65 64 20 6d 6f 64 75 6c 65 20 25  defined module %
20fa2 73 20 66 6f 72 20 74 61 62 6c 65 20 25 73 22 2c  s for table %s",
20fa3 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61  .        pTab->a
20fa4 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 2c 20 70  zModuleArg[0], p
20fa5 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
20fa6 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d   return 0.0;.  }
20fa7 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 65  .#endif..  /* Se
20fa8 74 20 74 68 65 20 61 43 6f 6e 73 74 72 61 69 6e  t the aConstrain
20fa9 74 5b 5d 2e 75 73 61 62 6c 65 20 66 69 65 6c 64  t[].usable field
20faa 73 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  s and initialize
20fab 20 61 6c 6c 20 0a 20 20 2a 2a 20 6f 75 74 70 75   all .  ** outpu
20fac 74 20 76 61 72 69 61 62 6c 65 73 20 74 6f 20 7a  t variables to z
20fad 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61  ero..  **.  ** a
20fae 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61  Constraint[].usa
20faf 62 6c 65 20 69 73 20 74 72 75 65 20 66 6f 72 20  ble is true for 
20fb0 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 65 72  constraints wher
20fb1 65 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  e the right-hand
20fb2 0a 20 20 2a 2a 20 73 69 64 65 20 63 6f 6e 74 61  .  ** side conta
20fb3 69 6e 73 20 6f 6e 6c 79 20 72 65 66 65 72 65 6e  ins only referen
20fb4 63 65 73 20 74 6f 20 74 61 62 6c 65 73 20 74 6f  ces to tables to
20fb5 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
20fb6 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 74 61   current.  ** ta
20fb7 62 6c 65 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ble.  In other w
20fb8 6f 72 64 73 2c 20 69 66 20 74 68 65 20 63 6f 6e  ords, if the con
20fb9 73 74 72 61 69 6e 74 20 69 73 20 6f 66 20 74 68  straint is of th
20fba 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a  e form:.  **.  *
20fbb 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75  *           colu
20fbc 6d 6e 20 3d 20 65 78 70 72 0a 20 20 2a 2a 0a 20  mn = expr.  **. 
20fbd 20 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 20 65   ** and we are e
20fbe 76 61 6c 75 61 74 69 6e 67 20 61 20 6a 6f 69 6e  valuating a join
20fbf 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74  , then the const
20fc0 72 61 69 6e 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20  raint on column 
20fc1 69 73 20 0a 20 20 2a 2a 20 6f 6e 6c 79 20 76 61  is .  ** only va
20fc2 6c 69 64 20 69 66 20 61 6c 6c 20 74 61 62 6c 65  lid if all table
20fc3 73 20 72 65 66 65 72 65 6e 63 65 64 20 69 6e 20  s referenced in 
20fc4 65 78 70 72 20 6f 63 63 75 72 20 74 6f 20 74 68  expr occur to th
20fc5 65 20 6c 65 66 74 0a 20 20 2a 2a 20 6f 66 20 74  e left.  ** of t
20fc6 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
20fc7 69 6e 67 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a  ing column..  **
20fc8 0a 20 20 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74  .  ** The aConst
20fc9 72 61 69 6e 74 73 5b 5d 20 61 72 72 61 79 20 63  raints[] array c
20fca 6f 6e 74 61 69 6e 73 20 65 6e 74 72 69 65 73 20  ontains entries 
20fcb 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69  for all constrai
20fcc 6e 74 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  nts.  ** on the 
20fcd 63 75 72 72 65 6e 74 20 74 61 62 6c 65 2e 20 20  current table.  
20fce 54 68 61 74 20 77 61 79 20 77 65 20 6f 6e 6c 79  That way we only
20fcf 20 68 61 76 65 20 74 6f 20 63 6f 6d 70 75 74 65   have to compute
20fd0 20 69 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 65 76   it once.  ** ev
20fd1 65 6e 20 74 68 6f 75 67 68 20 77 65 20 6d 69 67  en though we mig
20fd2 68 74 20 74 72 79 20 74 6f 20 70 69 63 6b 20 74  ht try to pick t
20fd3 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 6d 75  he best index mu
20fd4 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a 20 20  ltiple times..  
20fd5 2a 2a 20 46 6f 72 20 65 61 63 68 20 61 74 74 65  ** For each atte
20fd6 6d 70 74 20 61 74 20 70 69 63 6b 69 6e 67 20 61  mpt at picking a
20fd7 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 64  n index, the ord
20fd8 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
20fd9 74 68 65 0a 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69  the.  ** join mi
20fda 67 68 74 20 62 65 20 64 69 66 66 65 72 65 6e 74  ght be different
20fdb 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 72   so we have to r
20fdc 65 63 6f 6d 70 75 74 65 20 74 68 65 20 75 73 61  ecompute the usa
20fdd 62 6c 65 20 66 6c 61 67 0a 20 20 2a 2a 20 65 61  ble flag.  ** ea
20fde 63 68 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20  ch time..  */.  
20fdf 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72  pIdxCons = *(str
20fe0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
20fe1 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26  x_constraint**)&
20fe2 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
20fe3 72 61 69 6e 74 3b 0a 20 20 70 55 73 61 67 65 20  raint;.  pUsage 
20fe4 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  = pIdxInfo->aCon
20fe5 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20  straintUsage;.  
20fe6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 49  for(i=0; i<pIdxI
20fe7 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  nfo->nConstraint
20fe8 3b 20 69 2b 2b 2c 20 70 49 64 78 43 6f 6e 73 2b  ; i++, pIdxCons+
20fe9 2b 29 7b 0a 20 20 20 20 6a 20 3d 20 70 49 64 78  +){.    j = pIdx
20fea 43 6f 6e 73 2d 3e 69 54 65 72 6d 4f 66 66 73 65  Cons->iTermOffse
20feb 74 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26  t;.    pTerm = &
20fec 70 57 43 2d 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70  pWC->a[j];.    p
20fed 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 20  IdxCons->usable 
20fee 3d 20 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65  =  (pTerm->prere
20fef 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61 64  qRight & notRead
20ff0 79 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20 6d 65 6d  y)==0;.  }.  mem
20ff1 73 65 74 28 70 55 73 61 67 65 2c 20 30 2c 20 73  set(pUsage, 0, s
20ff2 69 7a 65 6f 66 28 70 55 73 61 67 65 5b 30 5d 29  izeof(pUsage[0])
20ff3 2a 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73  *pIdxInfo->nCons
20ff4 74 72 61 69 6e 74 29 3b 0a 20 20 69 66 28 20 70  traint);.  if( p
20ff5 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46  IdxInfo->needToF
20ff6 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20 20 20  reeIdxStr ){.   
20ff7 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49   sqlite3_free(pI
20ff8 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b  dxInfo->idxStr);
20ff9 0a 20 20 7d 0a 20 20 70 49 64 78 49 6e 66 6f 2d  .  }.  pIdxInfo-
20ffa 3e 69 64 78 53 74 72 20 3d 20 30 3b 0a 20 20 70  >idxStr = 0;.  p
20ffb 49 64 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20  IdxInfo->idxNum 
20ffc 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d  = 0;.  pIdxInfo-
20ffd 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
20ffe 72 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66  r = 0;.  pIdxInf
20fff 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  o->orderByConsum
21000 65 64 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e  ed = 0;.  pIdxIn
21001 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
21002 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  t = SQLITE_BIG_D
21003 42 4c 20 2f 20 32 2e 30 3b 0a 20 20 6e 4f 72 64  BL / 2.0;.  nOrd
21004 65 72 42 79 20 3d 20 70 49 64 78 49 6e 66 6f 2d  erBy = pIdxInfo-
21005 3e 6e 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28  >nOrderBy;.  if(
21006 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65   pIdxInfo->nOrde
21007 72 42 79 20 26 26 20 21 6f 72 64 65 72 42 79 55  rBy && !orderByU
21008 73 61 62 6c 65 20 29 7b 0a 20 20 20 20 2a 28 69  sable ){.    *(i
21009 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e  nt*)&pIdxInfo->n
2100a 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d  OrderBy = 0;.  }
2100b 0a 0a 20 20 73 71 6c 69 74 65 33 53 61 66 65 74  ..  sqlite3Safet
2100c 79 4f 66 66 28 70 50 61 72 73 65 2d 3e 64 62 29  yOff(pParse->db)
2100d 3b 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28  ;.  WHERETRACE((
2100e 22 78 42 65 73 74 49 6e 64 65 78 20 66 6f 72 20  "xBestIndex for 
2100f 25 73 5c 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  %s\n", pTab->zNa
21010 6d 65 29 29 3b 0a 20 20 54 52 41 43 45 5f 49 44  me));.  TRACE_ID
21011 58 5f 49 4e 50 55 54 53 28 70 49 64 78 49 6e 66  X_INPUTS(pIdxInf
21012 6f 29 3b 0a 20 20 72 63 20 3d 20 70 54 61 62 2d  o);.  rc = pTab-
21013 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d  >pVtab->pModule-
21014 3e 78 42 65 73 74 49 6e 64 65 78 28 70 54 61 62  >xBestIndex(pTab
21015 2d 3e 70 56 74 61 62 2c 20 70 49 64 78 49 6e 66  ->pVtab, pIdxInf
21016 6f 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f  o);.  TRACE_IDX_
21017 4f 55 54 50 55 54 53 28 70 49 64 78 49 6e 66 6f  OUTPUTS(pIdxInfo
21018 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
21019 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
2101a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
2101b 45 4d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  EM ){.      pPar
2101c 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
2101d 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65  iled = 1;.    }e
2101e 6c 73 65 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  lse {.      sqli
2101f 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
21020 73 65 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  se, "%s", sqlite
21021 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
21022 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 53    }.    sqlite3S
21023 61 66 65 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e  afetyOn(pParse->
21024 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  db);.  }else{.  
21025 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61    rc = sqlite3Sa
21026 66 65 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e 64  fetyOn(pParse->d
21027 62 29 3b 0a 20 20 7d 0a 20 20 2a 28 69 6e 74 2a  b);.  }.  *(int*
21028 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64  )&pIdxInfo->nOrd
21029 65 72 42 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b  erBy = nOrderBy;
2102a 0a 0a 20 20 72 65 74 75 72 6e 20 70 49 64 78 49  ..  return pIdxI
2102b 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
2102c 73 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  st;.}.#endif /* 
2102d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2102e 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a  UALTABLE */../*.
2102f 2a 2a 20 46 69 6e 64 20 74 68 65 20 62 65 73 74  ** Find the best
21030 20 69 6e 64 65 78 20 66 6f 72 20 61 63 63 65 73   index for acces
21031 73 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61  sing a particula
21032 72 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e  r table.  Return
21033 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f   a pointer.** to
21034 20 74 68 65 20 69 6e 64 65 78 2c 20 66 6c 61 67   the index, flag
21035 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20  s that describe 
21036 68 6f 77 20 74 68 65 20 69 6e 64 65 78 20 73 68  how the index sh
21037 6f 75 6c 64 20 62 65 20 75 73 65 64 2c 20 74 68  ould be used, th
21038 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65  e.** number of e
21039 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2103a 6e 74 73 2c 20 61 6e 64 20 74 68 65 20 22 63 6f  nts, and the "co
2103b 73 74 22 20 66 6f 72 20 74 68 69 73 20 69 6e 64  st" for this ind
2103c 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 6f  ex..**.** The lo
2103d 77 65 73 74 20 63 6f 73 74 20 69 6e 64 65 78 20  west cost index 
2103e 77 69 6e 73 2e 20 20 54 68 65 20 63 6f 73 74 20  wins.  The cost 
2103f 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f  is an estimate o
21040 66 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 0a  f the amount of.
21041 2a 2a 20 43 50 55 20 61 6e 64 20 64 69 73 6b 20  ** CPU and disk 
21042 49 2f 4f 20 6e 65 65 64 20 74 6f 20 70 72 6f 63  I/O need to proc
21043 65 73 73 20 74 68 65 20 72 65 71 75 65 73 74 20  ess the request 
21044 75 73 69 6e 67 20 74 68 65 20 73 65 6c 65 63 74  using the select
21045 65 64 20 69 6e 64 65 78 2e 0a 2a 2a 20 46 61 63  ed index..** Fac
21046 74 6f 72 73 20 74 68 61 74 20 69 6e 66 6c 75 65  tors that influe
21047 6e 63 65 20 63 6f 73 74 20 69 6e 63 6c 75 64 65  nce cost include
21048 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 54 68  :.**.**    *  Th
21049 65 20 65 73 74 69 6d 61 74 65 64 20 6e 75 6d 62  e estimated numb
2104a 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
2104b 77 69 6c 6c 20 62 65 20 72 65 74 72 69 65 76 65  will be retrieve
2104c 64 2e 20 20 28 54 68 65 0a 2a 2a 20 20 20 20 20  d.  (The.**     
2104d 20 20 66 65 77 65 72 20 74 68 65 20 62 65 74 74    fewer the bett
2104e 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  er.).**.**    * 
2104f 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
21050 73 6f 72 74 69 6e 67 20 6d 75 73 74 20 6f 63 63  sorting must occ
21051 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  ur..**.**    *  
21052 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  Whether or not t
21053 68 65 72 65 20 6d 75 73 74 20 62 65 20 73 65 70  here must be sep
21054 61 72 61 74 65 20 6c 6f 6f 6b 75 70 73 20 69 6e  arate lookups in
21055 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e   the.**       in
21056 64 65 78 20 61 6e 64 20 69 6e 20 74 68 65 20 6d  dex and in the m
21057 61 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2f  ain table..**.*/
21058 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20 62  .static double b
21059 65 73 74 49 6e 64 65 78 28 0a 20 20 50 61 72 73  estIndex(.  Pars
2105a 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2105b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2105c 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2105d 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2105e 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20  *pWC,           
2105f 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
21060 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  use */.  struct 
21061 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53  SrcList_item *pS
21062 72 63 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d  rc,  /* The FROM
21063 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20   clause term to 
21064 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d  search */.  Bitm
21065 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20  ask notReady,   
21066 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
21067 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  of cursors that 
21068 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
21069 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
2106a 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20  *pOrderBy,      
2106b 20 20 20 2f 2a 20 54 68 65 20 6f 72 64 65 72 20     /* The order 
2106c 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 49  by clause */.  I
2106d 6e 64 65 78 20 2a 2a 70 70 49 6e 64 65 78 2c 20  ndex **ppIndex, 
2106e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2106f 6b 65 20 2a 70 70 49 6e 64 65 78 20 70 6f 69 6e  ke *ppIndex poin
21070 74 20 74 6f 20 74 68 65 20 62 65 73 74 20 69 6e  t to the best in
21071 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46  dex */.  int *pF
21072 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
21073 20 20 20 20 20 2f 2a 20 50 75 74 20 66 6c 61 67       /* Put flag
21074 73 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 69  s describing thi
21075 73 20 63 68 6f 69 63 65 20 69 6e 20 2a 70 46 6c  s choice in *pFl
21076 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ags */.  int *pn
21077 45 71 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq              
21078 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20       /* Put the 
21079 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20  number of == or 
2107a 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 68  IN constraints h
2107b 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72  ere */.){.  Wher
2107c 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20  eTerm *pTerm;.  
2107d 49 6e 64 65 78 20 2a 62 65 73 74 49 64 78 20 3d  Index *bestIdx =
2107e 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49   0;         /* I
2107f 6e 64 65 78 20 74 68 61 74 20 67 69 76 65 73 20  ndex that gives 
21080 74 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20  the lowest cost 
21081 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6c 6f 77 65  */.  double lowe
21082 73 74 43 6f 73 74 3b 20 20 20 20 20 20 20 20 20  stCost;         
21083 20 2f 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20   /* The cost of 
21084 75 73 69 6e 67 20 62 65 73 74 49 64 78 20 2a 2f  using bestIdx */
21085 0a 20 20 69 6e 74 20 62 65 73 74 46 6c 61 67 73  .  int bestFlags
21086 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f   = 0;          /
21087 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74  * Flags associat
21088 65 64 20 77 69 74 68 20 62 65 73 74 49 64 78 20  ed with bestIdx 
21089 2a 2f 0a 20 20 69 6e 74 20 62 65 73 74 4e 45 71  */.  int bestNEq
2108a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2108b 20 2f 2a 20 42 65 73 74 20 76 61 6c 75 65 20 66   /* Best value f
2108c 6f 72 20 6e 45 71 20 2a 2f 0a 20 20 69 6e 74 20  or nEq */.  int 
2108d 69 43 75 72 20 3d 20 70 53 72 63 2d 3e 69 43 75  iCur = pSrc->iCu
2108e 72 73 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20 63  rsor;   /* The c
2108f 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62  ursor of the tab
21090 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73 73 65  le to be accesse
21091 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50  d */.  Index *pP
21092 72 6f 62 65 3b 20 20 20 20 20 20 20 20 20 20 20  robe;           
21093 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 77     /* An index w
21094 65 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67  e are evaluating
21095 20 2a 2f 0a 20 20 69 6e 74 20 72 65 76 3b 20 20   */.  int rev;  
21096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21097 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 63 61    /* True to sca
21098 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  n in reverse ord
21099 65 72 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  er */.  int flag
2109a 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
2109b 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73      /* Flags ass
2109c 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 50 72  ociated with pPr
2109d 6f 62 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71  obe */.  int nEq
2109e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2109f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
210a0 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74  f == or IN const
210a1 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  raints */.  int 
210a2 65 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20  eqTermMask;     
210a3 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
210a4 6f 66 20 76 61 6c 69 64 20 65 71 75 61 6c 69 74  of valid equalit
210a5 79 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f 0a 20  y operators */. 
210a6 20 64 6f 75 62 6c 65 20 63 6f 73 74 3b 20 20 20   double cost;   
210a7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
210a8 43 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 70 50  Cost of using pP
210a9 72 6f 62 65 20 2a 2f 0a 0a 20 20 57 48 45 52 45  robe */..  WHERE
210aa 54 52 41 43 45 28 28 22 62 65 73 74 49 6e 64 65  TRACE(("bestInde
210ab 78 3a 20 74 62 6c 3d 25 73 20 6e 6f 74 52 65 61  x: tbl=%s notRea
210ac 64 79 3d 25 78 5c 6e 22 2c 20 70 53 72 63 2d 3e  dy=%x\n", pSrc->
210ad 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 6f 74  pTab->zName, not
210ae 52 65 61 64 79 29 29 3b 0a 20 20 6c 6f 77 65 73  Ready));.  lowes
210af 74 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42  tCost = SQLITE_B
210b0 49 47 5f 44 42 4c 3b 0a 20 20 70 50 72 6f 62 65  IG_DBL;.  pProbe
210b1 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e 70   = pSrc->pTab->p
210b2 49 6e 64 65 78 3b 0a 0a 20 20 2f 2a 20 49 66 20  Index;..  /* If 
210b3 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 6e 6f  the table has no
210b4 20 69 6e 64 69 63 65 73 20 61 6e 64 20 74 68 65   indices and the
210b5 72 65 20 61 72 65 20 6e 6f 20 74 65 72 6d 73 20  re are no terms 
210b6 69 6e 20 74 68 65 20 77 68 65 72 65 0a 20 20 2a  in the where.  *
210b7 2a 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65  * clause that re
210b8 66 65 72 20 74 6f 20 74 68 65 20 52 4f 57 49 44  fer to the ROWID
210b9 2c 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e  , then we will n
210ba 65 76 65 72 20 62 65 20 61 62 6c 65 20 74 6f 20  ever be able to 
210bb 64 6f 0a 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67  do.  ** anything
210bc 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 66 75   other than a fu
210bd 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 20 6f 6e  ll table scan on
210be 20 74 68 69 73 20 74 61 62 6c 65 2e 20 20 57 65   this table.  We
210bf 20 6d 69 67 68 74 20 61 73 0a 20 20 2a 2a 20 77   might as.  ** w
210c0 65 6c 6c 20 70 75 74 20 69 74 20 66 69 72 73 74  ell put it first
210c1 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 6f 72 64   in the join ord
210c2 65 72 2e 20 20 54 68 61 74 20 77 61 79 2c 20 70  er.  That way, p
210c3 65 72 68 61 70 73 20 69 74 20 63 61 6e 20 62 65  erhaps it can be
210c4 0a 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 64  .  ** referenced
210c5 20 62 79 20 6f 74 68 65 72 20 74 61 62 6c 65 73   by other tables
210c6 20 69 6e 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20   in the join..  
210c7 2a 2f 0a 20 20 69 66 28 20 70 50 72 6f 62 65 3d  */.  if( pProbe=
210c8 3d 30 20 26 26 0a 20 20 20 20 20 66 69 6e 64 54  =0 &&.     findT
210c9 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d  erm(pWC, iCur, -
210ca 31 2c 20 30 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49  1, 0, WO_EQ|WO_I
210cb 4e 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f  N|WO_LT|WO_LE|WO
210cc 5f 47 54 7c 57 4f 5f 47 45 2c 30 29 3d 3d 30 20  _GT|WO_GE,0)==0 
210cd 26 26 0a 20 20 20 20 20 28 70 4f 72 64 65 72 42  &&.     (pOrderB
210ce 79 3d 3d 30 20 7c 7c 20 21 73 6f 72 74 61 62 6c  y==0 || !sortabl
210cf 65 42 79 52 6f 77 69 64 28 69 43 75 72 2c 20 70  eByRowid(iCur, p
210d0 4f 72 64 65 72 42 79 2c 20 70 57 43 2d 3e 70 4d  OrderBy, pWC->pM
210d1 61 73 6b 53 65 74 2c 20 26 72 65 76 29 29 20 29  askSet, &rev)) )
210d2 7b 0a 20 20 20 20 2a 70 46 6c 61 67 73 20 3d 20  {.    *pFlags = 
210d3 30 3b 0a 20 20 20 20 2a 70 70 49 6e 64 65 78 20  0;.    *ppIndex 
210d4 3d 20 30 3b 0a 20 20 20 20 2a 70 6e 45 71 20 3d  = 0;.    *pnEq =
210d5 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   0;.    return 0
210d6 2e 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  .0;.  }..  /* Ch
210d7 65 63 6b 20 66 6f 72 20 61 20 72 6f 77 69 64 3d  eck for a rowid=
210d8 45 58 50 52 20 6f 72 20 72 6f 77 69 64 20 49 4e  EXPR or rowid IN
210d9 20 28 2e 2e 2e 29 20 63 6f 6e 73 74 72 61 69 6e   (...) constrain
210da 74 73 0a 20 20 2a 2f 0a 20 20 70 54 65 72 6d 20  ts.  */.  pTerm 
210db 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
210dc 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61  iCur, -1, notRea
210dd 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c  dy, WO_EQ|WO_IN,
210de 20 30 29 3b 0a 20 20 69 66 28 20 70 54 65 72 6d   0);.  if( pTerm
210df 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45   ){.    Expr *pE
210e0 78 70 72 3b 0a 20 20 20 20 2a 70 70 49 6e 64 65  xpr;.    *ppInde
210e1 78 20 3d 20 30 3b 0a 20 20 20 20 62 65 73 74 46  x = 0;.    bestF
210e2 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 52 4f 57  lags = WHERE_ROW
210e3 49 44 5f 45 51 3b 0a 20 20 20 20 69 66 28 20 70  ID_EQ;.    if( p
210e4 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
210e5 26 20 57 4f 5f 45 51 20 29 7b 0a 20 20 20 20 20  & WO_EQ ){.     
210e6 20 2f 2a 20 52 6f 77 69 64 3d 3d 20 69 73 20 61   /* Rowid== is a
210e7 6c 77 61 79 73 20 74 68 65 20 62 65 73 74 20 70  lways the best p
210e8 69 63 6b 2e 20 20 4c 6f 6f 6b 20 6e 6f 20 66 75  ick.  Look no fu
210e9 72 74 68 65 72 2e 20 20 42 65 63 61 75 73 65 20  rther.  Because 
210ea 6f 6e 6c 79 0a 20 20 20 20 20 20 2a 2a 20 61 20  only.      ** a 
210eb 73 69 6e 67 6c 65 20 72 6f 77 20 69 73 20 67 65  single row is ge
210ec 6e 65 72 61 74 65 64 2c 20 6f 75 74 70 75 74 20  nerated, output 
210ed 69 73 20 61 6c 77 61 79 73 20 69 6e 20 73 6f 72  is always in sor
210ee 74 65 64 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20  ted order */.   
210ef 20 20 20 2a 70 46 6c 61 67 73 20 3d 20 57 48 45     *pFlags = WHE
210f0 52 45 5f 52 4f 57 49 44 5f 45 51 20 7c 20 57 48  RE_ROWID_EQ | WH
210f1 45 52 45 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20  ERE_UNIQUE;.    
210f2 20 20 2a 70 6e 45 71 20 3d 20 31 3b 0a 20 20 20    *pnEq = 1;.   
210f3 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
210f4 2e 2e 2e 20 62 65 73 74 20 69 73 20 72 6f 77 69  ... best is rowi
210f5 64 5c 6e 22 29 29 3b 0a 20 20 20 20 20 20 72 65  d\n"));.      re
210f6 74 75 72 6e 20 30 2e 30 3b 0a 20 20 20 20 7d 65  turn 0.0;.    }e
210f7 6c 73 65 20 69 66 28 20 28 70 45 78 70 72 20 3d  lse if( (pExpr =
210f8 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 29 2d 3e   pTerm->pExpr)->
210f9 70 4c 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20  pList!=0 ){.    
210fa 20 20 2f 2a 20 52 6f 77 69 64 20 49 4e 20 28 4c    /* Rowid IN (L
210fb 49 53 54 29 3a 20 63 6f 73 74 20 69 73 20 4e 6c  IST): cost is Nl
210fc 6f 67 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74  ogN where N is t
210fd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 69 73  he number of lis
210fe 74 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65  t.      ** eleme
210ff 6e 74 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 6c  nts.  */.      l
21100 6f 77 65 73 74 43 6f 73 74 20 3d 20 70 45 78 70  owestCost = pExp
21101 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  r->pList->nExpr;
21102 0a 20 20 20 20 20 20 6c 6f 77 65 73 74 43 6f 73  .      lowestCos
21103 74 20 2a 3d 20 65 73 74 4c 6f 67 28 6c 6f 77 65  t *= estLog(lowe
21104 73 74 43 6f 73 74 29 3b 0a 20 20 20 20 7d 65 6c  stCost);.    }el
21105 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 52 6f 77  se{.      /* Row
21106 69 64 20 49 4e 20 28 53 45 4c 45 43 54 29 3a 20  id IN (SELECT): 
21107 63 6f 73 74 20 69 73 20 4e 6c 6f 67 4e 20 77 68  cost is NlogN wh
21108 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ere N is the num
21109 62 65 72 20 6f 66 20 72 6f 77 73 0a 20 20 20 20  ber of rows.    
2110a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75    ** in the resu
2110b 6c 74 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20  lt of the inner 
2110c 73 65 6c 65 63 74 2e 20 20 57 65 20 68 61 76 65  select.  We have
2110d 20 6e 6f 20 77 61 79 20 74 6f 20 65 73 74 69 6d   no way to estim
2110e 61 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 61  ate.      ** tha
2110f 74 20 76 61 6c 75 65 20 73 6f 20 6d 61 6b 65 20  t value so make 
21110 61 20 77 69 6c 64 20 67 75 65 73 73 2e 20 2a 2f  a wild guess. */
21111 0a 20 20 20 20 20 20 6c 6f 77 65 73 74 43 6f 73  .      lowestCos
21112 74 20 3d 20 32 30 30 3b 0a 20 20 20 20 7d 0a 20  t = 200;.    }. 
21113 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
21114 2e 2e 2e 20 72 6f 77 69 64 20 49 4e 20 63 6f 73  ... rowid IN cos
21115 74 3a 20 25 2e 39 67 5c 6e 22 2c 20 6c 6f 77 65  t: %.9g\n", lowe
21116 73 74 43 6f 73 74 29 29 3b 0a 20 20 7d 0a 0a 20  stCost));.  }.. 
21117 20 2f 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65   /* Estimate the
21118 20 63 6f 73 74 20 6f 66 20 61 20 74 61 62 6c 65   cost of a table
21119 20 73 63 61 6e 2e 20 20 49 66 20 77 65 20 64 6f   scan.  If we do
2111a 20 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 6d 61   not know how ma
2111b 6e 79 0a 20 20 2a 2a 20 65 6e 74 72 69 65 73 20  ny.  ** entries 
2111c 61 72 65 20 69 6e 20 74 68 65 20 74 61 62 6c 65  are in the table
2111d 2c 20 75 73 65 20 31 20 6d 69 6c 6c 69 6f 6e 20  , use 1 million 
2111e 61 73 20 61 20 67 75 65 73 73 2e 0a 20 20 2a 2f  as a guess..  */
2111f 0a 20 20 63 6f 73 74 20 3d 20 70 50 72 6f 62 65  .  cost = pProbe
21120 20 3f 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77   ? pProbe->aiRow
21121 45 73 74 5b 30 5d 20 3a 20 31 30 30 30 30 30 30  Est[0] : 1000000
21122 3b 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28  ;.  WHERETRACE((
21123 22 2e 2e 2e 20 74 61 62 6c 65 20 73 63 61 6e 20  "... table scan 
21124 62 61 73 65 20 63 6f 73 74 3a 20 25 2e 39 67 5c  base cost: %.9g\
21125 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 66 6c  n", cost));.  fl
21126 61 67 73 20 3d 20 57 48 45 52 45 5f 52 4f 57 49  ags = WHERE_ROWI
21127 44 5f 52 41 4e 47 45 3b 0a 0a 20 20 2f 2a 20 43  D_RANGE;..  /* C
21128 68 65 63 6b 20 66 6f 72 20 63 6f 6e 73 74 72 61  heck for constra
21129 69 6e 74 73 20 6f 6e 20 61 20 72 61 6e 67 65 20  ints on a range 
2112a 6f 66 20 72 6f 77 69 64 73 20 69 6e 20 61 20 74  of rowids in a t
2112b 61 62 6c 65 20 73 63 61 6e 2e 0a 20 20 2a 2f 0a  able scan..  */.
2112c 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65    pTerm = findTe
2112d 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31  rm(pWC, iCur, -1
2112e 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c  , notReady, WO_L
2112f 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f  T|WO_LE|WO_GT|WO
21130 5f 47 45 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  _GE, 0);.  if( p
21131 54 65 72 6d 20 29 7b 0a 20 20 20 20 69 66 28 20  Term ){.    if( 
21132 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
21133 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
21134 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30  , WO_LT|WO_LE, 0
21135 29 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 73  ) ){.      flags
21136 20 7c 3d 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49   |= WHERE_TOP_LI
21137 4d 49 54 3b 0a 20 20 20 20 20 20 63 6f 73 74 20  MIT;.      cost 
21138 2f 3d 20 33 3b 20 20 2f 2a 20 47 75 65 73 73 20  /= 3;  /* Guess 
21139 74 68 61 74 20 72 6f 77 69 64 3c 45 58 50 52 20  that rowid<EXPR 
2113a 65 6c 69 6d 69 6e 61 74 65 73 20 74 77 6f 2d 74  eliminates two-t
2113b 68 69 72 64 73 20 6f 72 20 72 6f 77 73 20 2a 2f  hirds or rows */
2113c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66  .    }.    if( f
2113d 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2113e 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c  r, -1, notReady,
2113f 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30 29   WO_GT|WO_GE, 0)
21140 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 73 20   ){.      flags 
21141 7c 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d  |= WHERE_BTM_LIM
21142 49 54 3b 0a 20 20 20 20 20 20 63 6f 73 74 20 2f  IT;.      cost /
21143 3d 20 33 3b 20 20 2f 2a 20 47 75 65 73 73 20 74  = 3;  /* Guess t
21144 68 61 74 20 72 6f 77 69 64 3e 45 58 50 52 20 65  hat rowid>EXPR e
21145 6c 69 6d 69 6e 61 74 65 73 20 74 77 6f 2d 74 68  liminates two-th
21146 69 72 64 73 20 6f 66 20 72 6f 77 73 20 2a 2f 0a  irds of rows */.
21147 20 20 20 20 7d 0a 20 20 20 20 57 48 45 52 45 54      }.    WHERET
21148 52 41 43 45 28 28 22 2e 2e 2e 20 72 6f 77 69 64  RACE(("... rowid
21149 20 72 61 6e 67 65 20 72 65 64 75 63 65 73 20 63   range reduces c
2114a 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20  ost to %.9g\n", 
2114b 63 6f 73 74 29 29 3b 0a 20 20 7d 65 6c 73 65 7b  cost));.  }else{
2114c 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 30 3b 0a  .    flags = 0;.
2114d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
2114e 20 74 61 62 6c 65 20 73 63 61 6e 20 64 6f 65 73   table scan does
2114f 20 6e 6f 74 20 73 61 74 69 73 66 79 20 74 68 65   not satisfy the
21150 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
21151 2c 20 69 6e 63 72 65 61 73 65 0a 20 20 2a 2a 20  , increase.  ** 
21152 74 68 65 20 63 6f 73 74 20 62 79 20 4e 6c 6f 67  the cost by Nlog
21153 4e 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 65  N to cover the e
21154 78 70 65 6e 73 65 20 6f 66 20 73 6f 72 74 69 6e  xpense of sortin
21155 67 2e 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64  g. */.  if( pOrd
21156 65 72 42 79 20 29 7b 0a 20 20 20 20 69 66 28 20  erBy ){.    if( 
21157 73 6f 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28  sortableByRowid(
21158 69 43 75 72 2c 20 70 4f 72 64 65 72 42 79 2c 20  iCur, pOrderBy, 
21159 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 26  pWC->pMaskSet, &
2115a 72 65 76 29 20 29 7b 0a 20 20 20 20 20 20 66 6c  rev) ){.      fl
2115b 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44  ags |= WHERE_ORD
2115c 45 52 42 59 7c 57 48 45 52 45 5f 52 4f 57 49 44  ERBY|WHERE_ROWID
2115d 5f 52 41 4e 47 45 3b 0a 20 20 20 20 20 20 69 66  _RANGE;.      if
2115e 28 20 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20  ( rev ){.       
2115f 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f   flags |= WHERE_
21160 52 45 56 45 52 53 45 3b 0a 20 20 20 20 20 20 7d  REVERSE;.      }
21161 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
21162 20 20 63 6f 73 74 20 2b 3d 20 63 6f 73 74 2a 65    cost += cost*e
21163 73 74 4c 6f 67 28 63 6f 73 74 29 3b 0a 20 20 20  stLog(cost);.   
21164 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
21165 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63 72  ... sorting incr
21166 65 61 73 65 73 20 63 6f 73 74 20 74 6f 20 25 2e  eases cost to %.
21167 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20  9g\n", cost));. 
21168 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 63     }.  }.  if( c
21169 6f 73 74 3c 6c 6f 77 65 73 74 43 6f 73 74 20 29  ost<lowestCost )
2116a 7b 0a 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74  {.    lowestCost
2116b 20 3d 20 63 6f 73 74 3b 0a 20 20 20 20 62 65 73   = cost;.    bes
2116c 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a  tFlags = flags;.
2116d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
2116e 20 70 53 72 63 20 74 61 62 6c 65 20 69 73 20 74   pSrc table is t
2116f 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
21170 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 74 68  f a LEFT JOIN th
21171 65 6e 20 77 65 20 6d 61 79 20 6e 6f 74 0a 20 20  en we may not.  
21172 2a 2a 20 75 73 65 20 61 6e 20 69 6e 64 65 78 20  ** use an index 
21173 74 6f 20 73 61 74 69 73 66 79 20 49 53 20 4e 55  to satisfy IS NU
21174 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  LL constraints o
21175 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54  n that table.  T
21176 68 69 73 20 69 73 0a 20 20 2a 2a 20 62 65 63 61  his is.  ** beca
21177 75 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 69 67 68  use columns migh
21178 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 4e  t end up being N
21179 55 4c 4c 20 69 66 20 74 68 65 20 74 61 62 6c 65  ULL if the table
2117a 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
2117b 2d 0a 20 20 2a 2a 20 61 20 63 69 72 63 75 6d 73  -.  ** a circums
2117c 74 61 6e 63 65 20 77 68 69 63 68 20 74 68 65 20  tance which the 
2117d 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 68 65 6c  index cannot hel
2117e 70 20 75 73 20 64 69 73 63 6f 76 65 72 2e 20 20  p us discover.  
2117f 54 69 63 6b 65 74 20 23 32 31 37 37 2e 0a 20 20  Ticket #2177..  
21180 2a 2f 0a 20 20 69 66 28 20 28 70 53 72 63 2d 3e  */.  if( (pSrc->
21181 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45  jointype & JT_LE
21182 46 54 29 21 3d 30 20 29 7b 0a 20 20 20 20 65 71  FT)!=0 ){.    eq
21183 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51  TermMask = WO_EQ
21184 7c 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b  |WO_IN;.  }else{
21185 0a 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20  .    eqTermMask 
21186 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f  = WO_EQ|WO_IN|WO
21187 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20  _ISNULL;.  }..  
21188 2f 2a 20 4c 6f 6f 6b 20 61 74 20 65 61 63 68 20  /* Look at each 
21189 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a 20 20 66 6f  index..  */.  fo
2118a 72 28 3b 20 70 50 72 6f 62 65 3b 20 70 50 72 6f  r(; pProbe; pPro
2118b 62 65 3d 70 50 72 6f 62 65 2d 3e 70 4e 65 78 74  be=pProbe->pNext
2118c 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20  ){.    int i;   
2118d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2118e 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2118f 74 65 72 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c  ter */.    doubl
21190 65 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 3d  e inMultiplier =
21191 20 31 3b 0a 0a 20 20 20 20 57 48 45 52 45 54 52   1;..    WHERETR
21192 41 43 45 28 28 22 2e 2e 2e 20 69 6e 64 65 78 20  ACE(("... index 
21193 25 73 3a 5c 6e 22 2c 20 70 50 72 6f 62 65 2d 3e  %s:\n", pProbe->
21194 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 20 20 2f 2a  zName));..    /*
21195 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65   Count the numbe
21196 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
21197 74 68 65 20 69 6e 64 65 78 20 74 68 61 74 20 61  the index that a
21198 72 65 20 73 61 74 69 73 66 69 65 64 0a 20 20 20  re satisfied.   
21199 20 2a 2a 20 62 79 20 78 3d 45 58 50 52 20 63 6f   ** by x=EXPR co
2119a 6e 73 74 72 61 69 6e 74 73 20 6f 72 20 78 20 49  nstraints or x I
2119b 4e 20 28 2e 2e 2e 29 20 63 6f 6e 73 74 72 61 69  N (...) constrai
2119c 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  nts..    */.    
2119d 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 66  flags = 0;.    f
2119e 6f 72 28 69 3d 30 3b 20 69 3c 70 50 72 6f 62 65  or(i=0; i<pProbe
2119f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
211a0 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70  .      int j = p
211a1 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  Probe->aiColumn[
211a2 69 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20  i];.      pTerm 
211a3 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
211a4 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64  iCur, j, notRead
211a5 79 2c 20 65 71 54 65 72 6d 4d 61 73 6b 2c 20 70  y, eqTermMask, p
211a6 50 72 6f 62 65 29 3b 0a 20 20 20 20 20 20 69 66  Probe);.      if
211a7 28 20 70 54 65 72 6d 3d 3d 30 20 29 20 62 72 65  ( pTerm==0 ) bre
211a8 61 6b 3b 0a 20 20 20 20 20 20 66 6c 61 67 73 20  ak;.      flags 
211a9 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  |= WHERE_COLUMN_
211aa 45 51 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  EQ;.      if( pT
211ab 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
211ac 20 57 4f 5f 49 4e 20 29 7b 0a 20 20 20 20 20 20   WO_IN ){.      
211ad 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20    Expr *pExpr = 
211ae 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
211af 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57        flags |= W
211b0 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 3b 0a  HERE_COLUMN_IN;.
211b1 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70          if( pExp
211b2 72 2d 3e 70 53 65 6c 65 63 74 21 3d 30 20 29 7b  r->pSelect!=0 ){
211b3 0a 20 20 20 20 20 20 20 20 20 20 69 6e 4d 75 6c  .          inMul
211b4 74 69 70 6c 69 65 72 20 2a 3d 20 32 35 3b 0a 20  tiplier *= 25;. 
211b5 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
211b6 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 21 3d 30   pExpr->pList!=0
211b7 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
211b8 4d 75 6c 74 69 70 6c 69 65 72 20 2a 3d 20 70 45  Multiplier *= pE
211b9 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70  xpr->pList->nExp
211ba 72 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  r + 1;.        }
211bb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
211bc 20 20 20 63 6f 73 74 20 3d 20 70 50 72 6f 62 65     cost = pProbe
211bd 2d 3e 61 69 52 6f 77 45 73 74 5b 69 5d 20 2a 20  ->aiRowEst[i] * 
211be 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 2a 20 65  inMultiplier * e
211bf 73 74 4c 6f 67 28 69 6e 4d 75 6c 74 69 70 6c 69  stLog(inMultipli
211c0 65 72 29 3b 0a 20 20 20 20 6e 45 71 20 3d 20 69  er);.    nEq = i
211c1 3b 0a 20 20 20 20 69 66 28 20 70 50 72 6f 62 65  ;.    if( pProbe
211c2 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
211c3 6e 65 20 26 26 20 28 66 6c 61 67 73 20 26 20 57  ne && (flags & W
211c4 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29 3d  HERE_COLUMN_IN)=
211c5 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 6e  =0.         && n
211c6 45 71 3d 3d 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c  Eq==pProbe->nCol
211c7 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 66 6c 61  umn ){.      fla
211c8 67 73 20 7c 3d 20 57 48 45 52 45 5f 55 4e 49 51  gs |= WHERE_UNIQ
211c9 55 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 57 48  UE;.    }.    WH
211ca 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e 2e  ERETRACE((".....
211cb 2e 20 6e 45 71 3d 25 64 20 69 6e 4d 75 6c 74 3d  . nEq=%d inMult=
211cc 25 2e 39 67 20 63 6f 73 74 3d 25 2e 39 67 5c 6e  %.9g cost=%.9g\n
211cd 22 2c 6e 45 71 2c 69 6e 4d 75 6c 74 69 70 6c 69  ",nEq,inMultipli
211ce 65 72 2c 63 6f 73 74 29 29 3b 0a 0a 20 20 20 20  er,cost));..    
211cf 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 61 6e 67  /* Look for rang
211d0 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  e constraints.  
211d1 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 45 71    */.    if( nEq
211d2 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e  <pProbe->nColumn
211d3 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20   ){.      int j 
211d4 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75  = pProbe->aiColu
211d5 6d 6e 5b 6e 45 71 5d 3b 0a 20 20 20 20 20 20 70  mn[nEq];.      p
211d6 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
211d7 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f  pWC, iCur, j, no
211d8 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f  tReady, WO_LT|WO
211d9 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c  _LE|WO_GT|WO_GE,
211da 20 70 50 72 6f 62 65 29 3b 0a 20 20 20 20 20 20   pProbe);.      
211db 69 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20 20  if( pTerm ){.   
211dc 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48       flags |= WH
211dd 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45  ERE_COLUMN_RANGE
211de 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 69  ;.        if( fi
211df 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
211e0 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57  , j, notReady, W
211e1 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 70 50 72 6f  O_LT|WO_LE, pPro
211e2 62 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  be) ){.         
211e3 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f   flags |= WHERE_
211e4 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20  TOP_LIMIT;.     
211e5 20 20 20 20 20 63 6f 73 74 20 2f 3d 20 33 3b 0a       cost /= 3;.
211e6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
211e7 20 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70    if( findTerm(p
211e8 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74  WC, iCur, j, not
211e9 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f  Ready, WO_GT|WO_
211ea 47 45 2c 20 70 50 72 6f 62 65 29 20 29 7b 0a 20  GE, pProbe) ){. 
211eb 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c           flags |
211ec 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  = WHERE_BTM_LIMI
211ed 54 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 73  T;.          cos
211ee 74 20 2f 3d 20 33 3b 0a 20 20 20 20 20 20 20 20  t /= 3;.        
211ef 7d 0a 20 20 20 20 20 20 20 20 57 48 45 52 45 54  }.        WHERET
211f0 52 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 72 61  RACE(("...... ra
211f1 6e 67 65 20 72 65 64 75 63 65 73 20 63 6f 73 74  nge reduces cost
211f2 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73   to %.9g\n", cos
211f3 74 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  t));.      }.   
211f4 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 74   }..    /* Add t
211f5 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f  he additional co
211f6 73 74 20 6f 66 20 73 6f 72 74 69 6e 67 20 69 66  st of sorting if
211f7 20 74 68 61 74 20 69 73 20 61 20 66 61 63 74 6f   that is a facto
211f8 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  r..    */.    if
211f9 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
211fa 20 20 20 20 69 66 28 20 28 66 6c 61 67 73 20 26      if( (flags &
211fb 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e   WHERE_COLUMN_IN
211fc 29 3d 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20  )==0 &&.        
211fd 20 20 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65     isSortingInde
211fe 78 28 70 50 61 72 73 65 2c 70 57 43 2d 3e 70 4d  x(pParse,pWC->pM
211ff 61 73 6b 53 65 74 2c 70 50 72 6f 62 65 2c 69 43  askSet,pProbe,iC
21200 75 72 2c 70 4f 72 64 65 72 42 79 2c 6e 45 71 2c  ur,pOrderBy,nEq,
21201 26 72 65 76 29 20 29 7b 0a 20 20 20 20 20 20 20  &rev) ){.       
21202 20 69 66 28 20 66 6c 61 67 73 3d 3d 30 20 29 7b   if( flags==0 ){
21203 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 73  .          flags
21204 20 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f   = WHERE_COLUMN_
21205 52 41 4e 47 45 3b 0a 20 20 20 20 20 20 20 20 7d  RANGE;.        }
21206 0a 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c  .        flags |
21207 3d 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b  = WHERE_ORDERBY;
21208 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65 76  .        if( rev
21209 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c   ){.          fl
2120a 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56  ags |= WHERE_REV
2120b 45 52 53 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ERSE;.        }.
2120c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2120d 20 20 20 20 20 63 6f 73 74 20 2b 3d 20 63 6f 73       cost += cos
2120e 74 2a 65 73 74 4c 6f 67 28 63 6f 73 74 29 3b 0a  t*estLog(cost);.
2120f 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41          WHERETRA
21210 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 6f 72 64 65  CE(("...... orde
21211 72 62 79 20 69 6e 63 72 65 61 73 65 73 20 63 6f  rby increases co
21212 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63  st to %.9g\n", c
21213 6f 73 74 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ost));.      }. 
21214 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65     }..    /* Che
21215 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65 20  ck to see if we 
21216 63 61 6e 20 67 65 74 20 61 77 61 79 20 77 69 74  can get away wit
21217 68 20 75 73 69 6e 67 20 6a 75 73 74 20 74 68 65  h using just the
21218 20 69 6e 64 65 78 20 77 69 74 68 6f 75 74 0a 20   index without. 
21219 20 20 20 2a 2a 20 65 76 65 72 20 72 65 61 64 69     ** ever readi
2121a 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49  ng the table.  I
2121b 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61  f that is the ca
2121c 73 65 2c 20 74 68 65 6e 20 68 61 6c 76 65 20 74  se, then halve t
2121d 68 65 0a 20 20 20 20 2a 2a 20 63 6f 73 74 20 6f  he.    ** cost o
2121e 66 20 74 68 69 73 20 69 6e 64 65 78 2e 0a 20 20  f this index..  
2121f 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61    */.    if( fla
21220 67 73 20 26 26 20 70 53 72 63 2d 3e 63 6f 6c 55  gs && pSrc->colU
21221 73 65 64 20 3c 20 28 28 28 42 69 74 6d 61 73 6b  sed < (((Bitmask
21222 29 31 29 3c 3c 28 42 4d 53 2d 31 29 29 20 29 7b  )1)<<(BMS-1)) ){
21223 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 6d  .      Bitmask m
21224 20 3d 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64   = pSrc->colUsed
21225 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
21226 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
21227 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b  pProbe->nColumn;
21228 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   j++){.        i
21229 6e 74 20 78 20 3d 20 70 50 72 6f 62 65 2d 3e 61  nt x = pProbe->a
2122a 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20  iColumn[j];.    
2122b 20 20 20 20 69 66 28 20 78 3c 42 4d 53 2d 31 20      if( x<BMS-1 
2122c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 20 26  ){.          m &
2122d 3d 20 7e 28 28 28 42 69 74 6d 61 73 6b 29 31 29  = ~(((Bitmask)1)
2122e 3c 3c 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  <<x);.        }.
2122f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
21230 28 20 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( m==0 ){.      
21231 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45    flags |= WHERE
21232 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 20 20 20  _IDX_ONLY;.     
21233 20 20 20 63 6f 73 74 20 2f 3d 20 32 3b 0a 20 20     cost /= 2;.  
21234 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45        WHERETRACE
21235 28 28 22 2e 2e 2e 2e 2e 2e 20 69 64 78 2d 6f 6e  (("...... idx-on
21236 6c 79 20 72 65 64 75 63 65 73 20 63 6f 73 74 20  ly reduces cost 
21237 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74  to %.9g\n", cost
21238 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
21239 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  }..    /* If thi
2123a 73 20 69 6e 64 65 78 20 68 61 73 20 61 63 68 69  s index has achi
2123b 65 76 65 64 20 74 68 65 20 6c 6f 77 65 73 74 20  eved the lowest 
2123c 63 6f 73 74 20 73 6f 20 66 61 72 2c 20 74 68 65  cost so far, the
2123d 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 20 2a 2f  n use it..    */
2123e 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26  .    if( flags &
2123f 26 20 63 6f 73 74 20 3c 20 6c 6f 77 65 73 74 43  & cost < lowestC
21240 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 62 65 73  ost ){.      bes
21241 74 49 64 78 20 3d 20 70 50 72 6f 62 65 3b 0a 20  tIdx = pProbe;. 
21242 20 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20       lowestCost 
21243 3d 20 63 6f 73 74 3b 0a 20 20 20 20 20 20 62 65  = cost;.      be
21244 73 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b  stFlags = flags;
21245 0a 20 20 20 20 20 20 62 65 73 74 4e 45 71 20 3d  .      bestNEq =
21246 20 6e 45 71 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   nEq;.    }.  }.
21247 0a 20 20 2f 2a 20 52 65 70 6f 72 74 20 74 68 65  .  /* Report the
21248 20 62 65 73 74 20 72 65 73 75 6c 74 0a 20 20 2a   best result.  *
21249 2f 0a 20 20 2a 70 70 49 6e 64 65 78 20 3d 20 62  /.  *ppIndex = b
2124a 65 73 74 49 64 78 3b 0a 20 20 57 48 45 52 45 54  estIdx;.  WHERET
2124b 52 41 43 45 28 28 22 62 65 73 74 20 69 6e 64 65  RACE(("best inde
2124c 78 20 69 73 20 25 73 2c 20 63 6f 73 74 3d 25 2e  x is %s, cost=%.
2124d 39 67 2c 20 66 6c 61 67 73 3d 25 78 2c 20 6e 45  9g, flags=%x, nE
2124e 71 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  q=%d\n",.       
2124f 20 62 65 73 74 49 64 78 20 3f 20 62 65 73 74 49   bestIdx ? bestI
21250 64 78 2d 3e 7a 4e 61 6d 65 20 3a 20 22 28 6e 6f  dx->zName : "(no
21251 6e 65 29 22 2c 20 6c 6f 77 65 73 74 43 6f 73 74  ne)", lowestCost
21252 2c 20 62 65 73 74 46 6c 61 67 73 2c 20 62 65 73  , bestFlags, bes
21253 74 4e 45 71 29 29 3b 0a 20 20 2a 70 46 6c 61 67  tNEq));.  *pFlag
21254 73 20 3d 20 62 65 73 74 46 6c 61 67 73 20 7c 20  s = bestFlags | 
21255 65 71 54 65 72 6d 4d 61 73 6b 3b 0a 20 20 2a 70  eqTermMask;.  *p
21256 6e 45 71 20 3d 20 62 65 73 74 4e 45 71 3b 0a 20  nEq = bestNEq;. 
21257 20 72 65 74 75 72 6e 20 6c 6f 77 65 73 74 43 6f   return lowestCo
21258 73 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 69  st;.}.../*.** Di
21259 73 61 62 6c 65 20 61 20 74 65 72 6d 20 69 6e 20  sable a term in 
2125a 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2125b 2e 20 20 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f  .  Except, do no
2125c 74 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 65  t disable the te
2125d 72 6d 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74  rm.** if it cont
2125e 72 6f 6c 73 20 61 20 4c 45 46 54 20 4f 55 54 45  rols a LEFT OUTE
2125f 52 20 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64 69  R JOIN and it di
21260 64 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20  d not originate 
21261 69 6e 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20  in the ON.** or 
21262 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20  USING clause of 
21263 74 68 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a  that join..**.**
21264 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65   Consider the te
21265 72 6d 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20  rm t2.z='ok' in 
21266 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75  the following qu
21267 65 72 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28  eries:.**.**   (
21268 31 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  1)  SELECT * FRO
21269 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74  M t1 LEFT JOIN t
2126a 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57  2 ON t1.a=t2.x W
2126b 48 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a  HERE t2.z='ok'.*
2126c 2a 20 20 20 28 32 29 20 20 53 45 4c 45 43 54 20  *   (2)  SELECT 
2126d 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a  * FROM t1 LEFT J
2126e 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74  OIN t2 ON t1.a=t
2126f 32 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b  2.x AND t2.z='ok
21270 27 0a 2a 2a 20 20 20 28 33 29 20 20 53 45 4c 45  '.**   (3)  SELE
21271 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32  CT * FROM t1, t2
21272 20 57 48 45 52 45 20 74 31 2e 61 3d 74 32 2e 78   WHERE t1.a=t2.x
21273 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a   AND t2.z='ok'.*
21274 2a 0a 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f  *.** The t2.z='o
21275 6b 27 20 69 73 20 64 69 73 61 62 6c 65 64 20 69  k' is disabled i
21276 6e 20 74 68 65 20 69 6e 20 28 32 29 20 62 65 63  n the in (2) bec
21277 61 75 73 65 20 69 74 20 6f 72 69 67 69 6e 61 74  ause it originat
21278 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20  es.** in the ON 
21279 63 6c 61 75 73 65 2e 20 20 54 68 65 20 74 65 72  clause.  The ter
2127a 6d 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e  m is disabled in
2127b 20 28 33 29 20 62 65 63 61 75 73 65 20 69 74 20   (3) because it 
2127c 69 73 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f  is not part.** o
2127d 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a  f a LEFT OUTER J
2127e 4f 49 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74 68  OIN.  In (1), th
2127f 65 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 69  e term is not di
21280 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69  sabled..**.** Di
21281 73 61 62 6c 69 6e 67 20 61 20 74 65 72 6d 20 63  sabling a term c
21282 61 75 73 65 73 20 74 68 61 74 20 74 65 72 6d 20  auses that term 
21283 74 6f 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64  to not be tested
21284 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f   in the inner lo
21285 6f 70 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69  op.** of the joi
21286 6e 2e 20 20 44 69 73 61 62 6c 69 6e 67 20 69 73  n.  Disabling is
21287 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
21288 2e 20 20 57 68 65 6e 20 74 65 72 6d 73 20 61 72  .  When terms ar
21289 65 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62  e satisfied.** b
2128a 79 20 69 6e 64 69 63 65 73 2c 20 77 65 20 64 69  y indices, we di
2128b 73 61 62 6c 65 20 74 68 65 6d 20 74 6f 20 70 72  sable them to pr
2128c 65 76 65 6e 74 20 72 65 64 75 6e 64 61 6e 74 20  event redundant 
2128d 74 65 73 74 73 20 69 6e 20 74 68 65 20 69 6e 6e  tests in the inn
2128e 65 72 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20  er.** loop.  We 
2128f 77 6f 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f  would get the co
21290 72 72 65 63 74 20 72 65 73 75 6c 74 73 20 69 66  rrect results if
21291 20 6e 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76   nothing were ev
21292 65 72 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20  er disabled,.** 
21293 62 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20  but joins might 
21294 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f  run a little slo
21295 77 65 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20  wer.  The trick 
21296 69 73 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73  is to disable as
21297 20 6d 75 63 68 0a 2a 2a 20 61 73 20 77 65 20 63   much.** as we c
21298 61 6e 20 77 69 74 68 6f 75 74 20 64 69 73 61 62  an without disab
21299 6c 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20  ling too much.  
2129a 49 66 20 77 65 20 64 69 73 61 62 6c 65 64 20 69  If we disabled i
2129b 6e 20 28 31 29 2c 20 77 65 27 64 20 67 65 74 0a  n (1), we'd get.
2129c 2a 2a 20 74 68 65 20 77 72 6f 6e 67 20 61 6e 73  ** the wrong ans
2129d 77 65 72 2e 20 20 53 65 65 20 74 69 63 6b 65 74  wer.  See ticket
2129e 20 23 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63   #813..*/.static
2129f 20 76 6f 69 64 20 64 69 73 61 62 6c 65 54 65 72   void disableTer
212a0 6d 28 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c  m(WhereLevel *pL
212a1 65 76 65 6c 2c 20 57 68 65 72 65 54 65 72 6d 20  evel, WhereTerm 
212a2 2a 70 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70  *pTerm){.  if( p
212a3 54 65 72 6d 0a 20 20 20 20 20 20 26 26 20 28 70  Term.      && (p
212a4 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45  Term->flags & TE
212a5 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 0a 20 20 20  RM_CODED)==0.   
212a6 20 20 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 69     && (pLevel->i
212a7 4c 65 66 74 4a 6f 69 6e 3d 3d 30 20 7c 7c 20 45  LeftJoin==0 || E
212a8 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
212a9 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 45 50 5f  Term->pExpr, EP_
212aa 46 72 6f 6d 4a 6f 69 6e 29 29 0a 20 20 29 7b 0a  FromJoin)).  ){.
212ab 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73      pTerm->flags
212ac 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a   |= TERM_CODED;.
212ad 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 69      if( pTerm->i
212ae 50 61 72 65 6e 74 3e 3d 30 20 29 7b 0a 20 20 20  Parent>=0 ){.   
212af 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f     WhereTerm *pO
212b0 74 68 65 72 20 3d 20 26 70 54 65 72 6d 2d 3e 70  ther = &pTerm->p
212b1 57 43 2d 3e 61 5b 70 54 65 72 6d 2d 3e 69 50 61  WC->a[pTerm->iPa
212b2 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20 69 66 28  rent];.      if(
212b3 20 28 2d 2d 70 4f 74 68 65 72 2d 3e 6e 43 68 69   (--pOther->nChi
212b4 6c 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ld)==0 ){.      
212b5 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c    disableTerm(pL
212b6 65 76 65 6c 2c 20 70 4f 74 68 65 72 29 3b 0a 20  evel, pOther);. 
212b7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
212b8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
212b9 74 65 20 63 6f 64 65 20 74 68 61 74 20 62 75 69  te code that bui
212ba 6c 64 73 20 61 20 70 72 6f 62 65 20 66 6f 72 20  lds a probe for 
212bb 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  an index..**.** 
212bc 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20  There should be 
212bd 6e 43 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6f  nColumn values o
212be 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68  n the stack.  Th
212bf 65 20 69 6e 64 65 78 0a 2a 2a 20 74 6f 20 62 65  e index.** to be
212c0 20 70 72 6f 62 65 64 20 69 73 20 70 49 64 78 2e   probed is pIdx.
212c1 20 20 50 6f 70 20 74 68 65 20 76 61 6c 75 65 73    Pop the values
212c2 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 20   from the stack 
212c3 61 6e 64 0a 2a 2a 20 72 65 70 6c 61 63 65 20 74  and.** replace t
212c4 68 65 6d 20 61 6c 6c 20 77 69 74 68 20 61 20 73  hem all with a s
212c5 69 6e 67 6c 65 20 72 65 63 6f 72 64 20 74 68 61  ingle record tha
212c6 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a 2a  t is the index.*
212c7 2a 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 73 74  * problem..*/.st
212c8 61 74 69 63 20 76 6f 69 64 20 62 75 69 6c 64 49  atic void buildI
212c9 6e 64 65 78 50 72 6f 62 65 28 0a 20 20 56 64 62  ndexProbe(.  Vdb
212ca 65 20 2a 76 2c 20 20 20 20 20 20 20 20 2f 2a 20  e *v,        /* 
212cb 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
212cc 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20  to this VM */.  
212cd 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  int nColumn,    
212ce 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  /* The number of
212cf 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 65 63   columns to chec
212d0 6b 20 66 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  k for NULL */.  
212d1 49 6e 64 65 78 20 2a 70 49 64 78 20 20 20 20 20  Index *pIdx     
212d2 2f 2a 20 49 6e 64 65 78 20 74 68 61 74 20 77 65  /* Index that we
212d3 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 69   will be searchi
212d4 6e 67 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  ng */.){.  sqlit
212d5 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
212d6 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6e 43  P_MakeRecord, nC
212d7 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20 73 71 6c  olumn, 0);.  sql
212d8 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
212d9 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b 0a 7d  yStr(v, pIdx);.}
212da 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  .../*.** Generat
212db 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e  e code for a sin
212dc 67 6c 65 20 65 71 75 61 6c 69 74 79 20 74 65 72  gle equality ter
212dd 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  m of the WHERE c
212de 6c 61 75 73 65 2e 20 20 41 6e 20 65 71 75 61 6c  lause.  An equal
212df 69 74 79 0a 2a 2a 20 74 65 72 6d 20 63 61 6e 20  ity.** term can 
212e0 62 65 20 65 69 74 68 65 72 20 58 3d 65 78 70 72  be either X=expr
212e1 20 6f 72 20 58 20 49 4e 20 28 2e 2e 2e 29 2e 20   or X IN (...). 
212e2 20 20 70 54 65 72 6d 20 69 73 20 74 68 65 20 74    pTerm is the t
212e3 65 72 6d 20 74 6f 20 62 65 20 0a 2a 2a 20 63 6f  erm to be .** co
212e4 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ded..**.** The c
212e5 75 72 72 65 6e 74 20 76 61 6c 75 65 20 66 6f 72  urrent value for
212e6 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
212e7 69 73 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 74  is left on the t
212e8 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  op of the stack.
212e9 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63 6f 6e  .**.** For a con
212ea 73 74 72 61 69 6e 74 20 6f 66 20 74 68 65 20 66  straint of the f
212eb 6f 72 6d 20 58 3d 65 78 70 72 2c 20 74 68 65 20  orm X=expr, the 
212ec 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76  expression is ev
212ed 61 6c 75 61 74 65 64 20 61 6e 64 20 69 74 73 0a  aluated and its.
212ee 2a 2a 20 72 65 73 75 6c 74 20 69 73 20 6c 65 66  ** result is lef
212ef 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20  t on the stack. 
212f0 20 46 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73   For constraints
212f1 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 20 49   of the form X I
212f2 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69 73 20  N (...).** this 
212f3 72 6f 75 74 69 6e 65 20 73 65 74 73 20 75 70 20  routine sets up 
212f4 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c  a loop that will
212f5 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6c   iterate over al
212f6 6c 20 76 61 6c 75 65 73 20 6f 66 20 58 2e 0a 2a  l values of X..*
212f7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
212f8 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 0a  deEqualityTerm(.
212f9 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
212fa 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
212fb 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
212fc 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
212fd 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74 65 72  rm,   /* The ter
212fe 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  m of the WHERE c
212ff 6c 61 75 73 65 20 74 6f 20 62 65 20 63 6f 64 65  lause to be code
21300 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65  d */.  WhereLeve
21301 6c 20 2a 70 4c 65 76 65 6c 20 20 2f 2a 20 57 68  l *pLevel  /* Wh
21302 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  en level of the 
21303 46 52 4f 4d 20 63 6c 61 75 73 65 20 77 65 20 61  FROM clause we a
21304 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a 2f  re working on */
21305 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 58 20 3d  .){.  Expr *pX =
21306 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20   pTerm->pExpr;. 
21307 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
21308 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 66 28 20  e->pVdbe;.  if( 
21309 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 29 7b  pX->op==TK_EQ ){
2130a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2130b 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 58 2d  Code(pParse, pX-
2130c 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73  >pRight);.  }els
2130d 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b  e if( pX->op==TK
2130e 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 73  _ISNULL ){.    s
2130f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
21310 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30  v, OP_Null, 0, 0
21311 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
21312 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
21313 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
21314 20 69 54 61 62 3b 0a 20 20 20 20 73 74 72 75 63   iTab;.    struc
21315 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 0a  t InLoop *pIn;..
21316 20 20 20 20 61 73 73 65 72 74 28 20 70 58 2d 3e      assert( pX->
21317 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20  op==TK_IN );.   
21318 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73   sqlite3CodeSubs
21319 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 58  elect(pParse, pX
2131a 29 3b 0a 20 20 20 20 69 54 61 62 20 3d 20 70 58  );.    iTab = pX
2131b 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 73 71  ->iTable;.    sq
2131c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
2131d 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61  , OP_Rewind, iTa
2131e 62 2c 20 30 29 3b 0a 20 20 20 20 56 64 62 65 43  b, 0);.    VdbeC
2131f 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 25 2e  omment((v, "# %.
21320 2a 73 22 2c 20 70 58 2d 3e 73 70 61 6e 2e 6e 2c  *s", pX->span.n,
21321 20 70 58 2d 3e 73 70 61 6e 2e 7a 29 29 3b 0a 20   pX->span.z));. 
21322 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 6e     if( pLevel->n
21323 49 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  In==0 ){.      p
21324 4c 65 76 65 6c 2d 3e 6e 78 74 20 3d 20 73 71 6c  Level->nxt = sql
21325 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
21326 6c 28 76 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  l(v);.    }.    
21327 70 4c 65 76 65 6c 2d 3e 6e 49 6e 2b 2b 3b 0a 20  pLevel->nIn++;. 
21328 20 20 20 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f     pLevel->aInLo
21329 6f 70 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  op = sqlite3DbRe
2132a 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 50 61 72  allocOrFree(pPar
2132b 73 65 2d 3e 64 62 2c 20 70 4c 65 76 65 6c 2d 3e  se->db, pLevel->
2132c 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20 20  aInLoop,.       
2132d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2132e 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a               siz
2132f 65 6f 66 28 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c  eof(pLevel->aInL
21330 6f 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e  oop[0])*pLevel->
21331 6e 49 6e 29 3b 0a 20 20 20 20 70 49 6e 20 3d 20  nIn);.    pIn = 
21332 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 3b  pLevel->aInLoop;
21333 0a 20 20 20 20 69 66 28 20 70 49 6e 20 29 7b 0a  .    if( pIn ){.
21334 20 20 20 20 20 20 70 49 6e 20 2b 3d 20 70 4c 65        pIn += pLe
21335 76 65 6c 2d 3e 6e 49 6e 20 2d 20 31 3b 0a 20 20  vel->nIn - 1;.  
21336 20 20 20 20 70 49 6e 2d 3e 69 43 75 72 20 3d 20      pIn->iCur = 
21337 69 54 61 62 3b 0a 20 20 20 20 20 20 70 49 6e 2d  iTab;.      pIn-
21338 3e 74 6f 70 41 64 64 72 20 3d 20 73 71 6c 69 74  >topAddr = sqlit
21339 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
2133a 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20  P_Column, iTab, 
2133b 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
2133c 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
2133d 5f 49 73 4e 75 6c 6c 2c 20 2d 31 2c 20 30 29 3b  _IsNull, -1, 0);
2133e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2133f 20 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e 20 3d 20    pLevel->nIn = 
21340 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  0;.    }.#endif.
21341 20 20 7d 0a 20 20 64 69 73 61 62 6c 65 54 65 72    }.  disableTer
21342 6d 28 70 4c 65 76 65 6c 2c 20 70 54 65 72 6d 29  m(pLevel, pTerm)
21343 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
21344 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
21345 6c 6c 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20  ll evaluate all 
21346 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72  == and IN constr
21347 61 69 6e 74 73 20 66 6f 72 20 61 6e 0a 2a 2a 20  aints for an.** 
21348 69 6e 64 65 78 2e 20 20 54 68 65 20 76 61 6c 75  index.  The valu
21349 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74  es for all const
2134a 72 61 69 6e 74 73 20 61 72 65 20 6c 65 66 74 20  raints are left 
2134b 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  on the stack..**
2134c 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  .** For example,
2134d 20 63 6f 6e 73 69 64 65 72 20 74 61 62 6c 65 20   consider table 
2134e 74 31 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20  t1(a,b,c,d,e,f) 
2134f 77 69 74 68 20 69 6e 64 65 78 20 69 31 28 61 2c  with index i1(a,
21350 62 2c 63 29 2e 0a 2a 2a 20 53 75 70 70 6f 73 65  b,c)..** Suppose
21351 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
21352 65 20 69 73 20 74 68 69 73 3a 20 20 61 3d 3d 35  e is this:  a==5
21353 20 41 4e 44 20 62 20 49 4e 20 28 31 2c 32 2c 33   AND b IN (1,2,3
21354 29 20 41 4e 44 20 63 3e 35 20 41 4e 44 20 63 3c  ) AND c>5 AND c<
21355 31 30 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20  10.** The index 
21356 68 61 73 20 61 73 20 6d 61 6e 79 20 61 73 20 74  has as many as t
21357 68 72 65 65 20 65 71 75 61 6c 69 74 79 20 63 6f  hree equality co
21358 6e 73 74 72 61 69 6e 74 73 2c 20 62 75 74 20 69  nstraints, but i
21359 6e 20 74 68 69 73 0a 2a 2a 20 65 78 61 6d 70 6c  n this.** exampl
2135a 65 2c 20 74 68 65 20 74 68 69 72 64 20 22 63 22  e, the third "c"
2135b 20 76 61 6c 75 65 20 69 73 20 61 6e 20 69 6e 65   value is an ine
2135c 71 75 61 6c 69 74 79 2e 20 20 53 6f 20 6f 6e 6c  quality.  So onl
2135d 79 20 74 77 6f 20 0a 2a 2a 20 63 6f 6e 73 74 72  y two .** constr
2135e 61 69 6e 74 73 20 61 72 65 20 63 6f 64 65 64 2e  aints are coded.
2135f 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77    This routine w
21360 69 6c 6c 20 67 65 6e 65 72 61 74 65 20 63 6f 64  ill generate cod
21361 65 20 74 6f 20 65 76 61 6c 75 61 74 65 0a 2a 2a  e to evaluate.**
21362 20 61 3d 3d 35 20 61 6e 64 20 62 20 49 4e 20 28   a==5 and b IN (
21363 31 2c 32 2c 33 29 2e 20 20 54 68 65 20 63 75 72  1,2,3).  The cur
21364 72 65 6e 74 20 76 61 6c 75 65 73 20 66 6f 72 20  rent values for 
21365 61 20 61 6e 64 20 62 20 77 69 6c 6c 20 62 65 20  a and b will be 
21366 6c 65 66 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 73  left.** on the s
21367 74 61 63 6b 20 2d 20 61 20 69 73 20 74 68 65 20  tack - a is the 
21368 64 65 65 70 65 73 74 20 61 6e 64 20 62 20 74 68  deepest and b th
21369 65 20 73 68 61 6c 6c 6f 77 65 73 74 2e 0a 2a 2a  e shallowest..**
2136a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70  .** In the examp
2136b 6c 65 20 61 62 6f 76 65 20 6e 45 71 3d 3d 32 2e  le above nEq==2.
2136c 20 20 42 75 74 20 74 68 69 73 20 73 75 62 72 6f    But this subro
2136d 75 74 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20  utine works for 
2136e 61 6e 79 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  any value.** of 
2136f 6e 45 71 20 69 6e 63 6c 75 64 69 6e 67 20 30 2e  nEq including 0.
21370 20 20 49 66 20 6e 45 71 3d 3d 30 2c 20 74 68 69    If nEq==0, thi
21371 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 61  s routine is nea
21372 72 6c 79 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20  rly a no-op..** 
21373 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 69  The only thing i
21374 74 20 64 6f 65 73 20 69 73 20 61 6c 6c 6f 63 61  t does is alloca
21375 74 65 20 74 68 65 20 70 4c 65 76 65 6c 2d 3e 69  te the pLevel->i
21376 4d 65 6d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  Mem memory cell.
21377 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
21378 69 6e 65 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63  ine always alloc
21379 61 74 65 73 20 61 74 20 6c 65 61 73 74 20 6f 6e  ates at least on
2137a 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e  e memory cell an
2137b 64 20 70 75 74 73 0a 2a 2a 20 74 68 65 20 61 64  d puts.** the ad
2137c 64 72 65 73 73 20 6f 66 20 74 68 61 74 20 6d 65  dress of that me
2137d 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 70 4c 65  mory cell in pLe
2137e 76 65 6c 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 20  vel->iMem.  The 
2137f 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 63 61 6c  code that.** cal
21380 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
21381 77 69 6c 6c 20 75 73 65 20 70 4c 65 76 65 6c 2d  will use pLevel-
21382 3e 69 4d 65 6d 20 74 6f 20 73 74 6f 72 65 20 74  >iMem to store t
21383 68 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e 0a 2a  he termination.*
21384 2a 20 6b 65 79 20 76 61 6c 75 65 20 6f 66 20 74  * key value of t
21385 68 65 20 6c 6f 6f 70 2e 20 20 49 66 20 6f 6e 65  he loop.  If one
21386 20 6f 72 20 6d 6f 72 65 20 49 4e 20 6f 70 65 72   or more IN oper
21387 61 74 6f 72 73 20 61 70 70 65 61 72 2c 20 74 68  ators appear, th
21388 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  en.** this routi
21389 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 20  ne allocates an 
2138a 61 64 64 69 74 69 6f 6e 61 6c 20 6e 45 71 20 6d  additional nEq m
2138b 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20  emory cells for 
2138c 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 2e  internal.** use.
2138d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2138e 63 6f 64 65 41 6c 6c 45 71 75 61 6c 69 74 79 54  codeAllEqualityT
2138f 65 72 6d 73 28 0a 20 20 50 61 72 73 65 20 2a 70  erms(.  Parse *p
21390 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
21391 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
21392 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c   */.  WhereLevel
21393 20 2a 70 4c 65 76 65 6c 2c 20 20 20 2f 2a 20 57   *pLevel,   /* W
21394 68 69 63 68 20 6e 65 73 74 65 64 20 6c 6f 6f 70  hich nested loop
21395 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 77 65 20   of the FROM we 
21396 61 72 65 20 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  are coding */.  
21397 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
21398 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45  ,     /* The WHE
21399 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 42  RE clause */.  B
2139a 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 20  itmask notReady 
2139b 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 70 61       /* Which pa
2139c 72 74 73 20 6f 66 20 46 52 4f 4d 20 68 61 76 65  rts of FROM have
2139d 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f   not yet been co
2139e 64 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ded */.){.  int 
2139f 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 6e 45  nEq = pLevel->nE
213a0 71 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  q;        /* The
213a1 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72   number of == or
213a2 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   IN constraints 
213a3 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  to code */.  int
213a4 20 74 65 72 6d 73 49 6e 4d 65 6d 20 3d 20 30 3b   termsInMem = 0;
213a5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
213a6 20 74 72 75 65 2c 20 73 74 6f 72 65 20 76 61 6c   true, store val
213a7 75 65 20 69 6e 20 6d 65 6d 5b 5d 20 63 65 6c 6c  ue in mem[] cell
213a8 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d  s */.  Vdbe *v =
213a9 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20   pParse->pVdbe; 
213aa 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74       /* The virt
213ab 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65  ual machine unde
213ac 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a  r construction *
213ad 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20  /.  Index *pIdx 
213ae 3d 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78 3b 20  = pLevel->pIdx; 
213af 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 62    /* The index b
213b0 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68  eing used for th
213b1 69 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  is loop */.  int
213b2 20 69 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e   iCur = pLevel->
213b3 69 54 61 62 43 75 72 3b 20 20 20 2f 2a 20 54 68  iTabCur;   /* Th
213b4 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20  e cursor of the 
213b5 74 61 62 6c 65 20 2a 2f 0a 20 20 57 68 65 72 65  table */.  Where
213b6 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20  Term *pTerm;    
213b7 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69           /* A si
213b8 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20  ngle constraint 
213b9 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b  term */.  int j;
213ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213bb 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
213bc 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 2f 2a  counter */..  /*
213bd 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
213be 6d 61 6e 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  many memory cell
213bf 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 74  s we will need t
213c0 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 74 68 65  hen allocate the
213c1 6d 2e 0a 20 20 2a 2a 20 57 65 20 61 6c 77 61 79  m..  ** We alway
213c2 73 20 6e 65 65 64 20 61 74 20 6c 65 61 73 74 20  s need at least 
213c3 6f 6e 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72  one used to stor
213c4 65 20 74 68 65 20 6c 6f 6f 70 20 74 65 72 6d 69  e the loop termi
213c5 6e 61 74 6f 72 0a 20 20 2a 2a 20 76 61 6c 75 65  nator.  ** value
213c6 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20  .  If there are 
213c7 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 77 65 27  IN operators we'
213c8 6c 6c 20 6e 65 65 64 20 6f 6e 65 20 66 6f 72 20  ll need one for 
213c9 65 61 63 68 20 3d 3d 20 6f 72 0a 20 20 2a 2a 20  each == or.  ** 
213ca 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20  IN constraint.. 
213cb 20 2a 2f 0a 20 20 70 4c 65 76 65 6c 2d 3e 69 4d   */.  pLevel->iM
213cc 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  em = pParse->nMe
213cd 6d 2b 2b 3b 0a 20 20 69 66 28 20 70 4c 65 76 65  m++;.  if( pLeve
213ce 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45  l->flags & WHERE
213cf 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 29 7b 0a 20 20  _COLUMN_IN ){.  
213d0 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
213d1 3d 20 70 4c 65 76 65 6c 2d 3e 6e 45 71 3b 0a 20  = pLevel->nEq;. 
213d2 20 20 20 74 65 72 6d 73 49 6e 4d 65 6d 20 3d 20     termsInMem = 
213d3 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 76 61  1;.  }..  /* Eva
213d4 6c 75 61 74 65 20 74 68 65 20 65 71 75 61 6c 69  luate the equali
213d5 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20  ty constraints. 
213d6 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49   */.  assert( pI
213d7 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6e 45 71  dx->nColumn>=nEq
213d8 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a   );.  for(j=0; j
213d9 3c 6e 45 71 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  <nEq; j++){.    
213da 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61 69  int k = pIdx->ai
213db 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 70  Column[j];.    p
213dc 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
213dd 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f  pWC, iCur, k, no
213de 74 52 65 61 64 79 2c 20 70 4c 65 76 65 6c 2d 3e  tReady, pLevel->
213df 66 6c 61 67 73 2c 20 70 49 64 78 29 3b 0a 20 20  flags, pIdx);.  
213e0 20 20 69 66 28 20 70 54 65 72 6d 3d 3d 30 20 29    if( pTerm==0 )
213e1 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73 73 65   break;.    asse
213e2 72 74 28 20 28 70 54 65 72 6d 2d 3e 66 6c 61 67  rt( (pTerm->flag
213e3 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d  s & TERM_CODED)=
213e4 3d 30 20 29 3b 0a 20 20 20 20 63 6f 64 65 45 71  =0 );.    codeEq
213e5 75 61 6c 69 74 79 54 65 72 6d 28 70 50 61 72 73  ualityTerm(pPars
213e6 65 2c 20 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c  e, pTerm, pLevel
213e7 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 65 72  );.    if( (pTer
213e8 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
213e9 57 4f 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e 29  WO_ISNULL|WO_IN)
213ea 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )==0 ){.      sq
213eb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
213ec 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74 65 72  , OP_IsNull, ter
213ed 6d 73 49 6e 4d 65 6d 20 3f 20 2d 31 20 3a 20 2d  msInMem ? -1 : -
213ee 28 6a 2b 31 29 2c 20 70 4c 65 76 65 6c 2d 3e 62  (j+1), pLevel->b
213ef 72 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  rk);.    }.    i
213f0 66 28 20 74 65 72 6d 73 49 6e 4d 65 6d 20 29 7b  f( termsInMem ){
213f1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
213f2 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
213f3 6d 53 74 6f 72 65 2c 20 70 4c 65 76 65 6c 2d 3e  mStore, pLevel->
213f4 69 4d 65 6d 2b 6a 2b 31 2c 20 31 29 3b 0a 20 20  iMem+j+1, 1);.  
213f5 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61    }.  }..  /* Ma
213f6 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20  ke sure all the 
213f7 63 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c 75 65  constraint value
213f8 73 20 61 72 65 20 6f 6e 20 74 68 65 20 74 6f 70  s are on the top
213f9 20 6f 66 20 74 68 65 20 73 74 61 63 6b 0a 20 20   of the stack.  
213fa 2a 2f 0a 20 20 69 66 28 20 74 65 72 6d 73 49 6e  */.  if( termsIn
213fb 4d 65 6d 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a  Mem ){.    for(j
213fc 3d 30 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29 7b  =0; j<nEq; j++){
213fd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
213fe 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
213ff 6d 4c 6f 61 64 2c 20 70 4c 65 76 65 6c 2d 3e 69  mLoad, pLevel->i
21400 4d 65 6d 2b 6a 2b 31 2c 20 30 29 3b 0a 20 20 20  Mem+j+1, 0);.   
21401 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 64 65   }.  }.}..#if de
21402 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
21403 54 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  T)./*.** The fol
21404 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 20  lowing variable 
21405 68 6f 6c 64 73 20 61 20 74 65 78 74 20 64 65 73  holds a text des
21406 63 72 69 70 74 69 6f 6e 20 6f 66 20 71 75 65 72  cription of quer
21407 79 20 70 6c 61 6e 20 67 65 6e 65 72 61 74 65 64  y plan generated
21408 0a 2a 2a 20 62 79 20 74 68 65 20 6d 6f 73 74 20  .** by the most 
21409 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73  recent call to s
2140a 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2140b 28 29 2e 20 20 45 61 63 68 20 63 61 6c 6c 20 74  ().  Each call t
2140c 6f 20 57 68 65 72 65 42 65 67 69 6e 0a 2a 2a 20  o WhereBegin.** 
2140d 6f 76 65 72 77 72 69 74 65 73 20 74 68 65 20 70  overwrites the p
2140e 72 65 76 69 6f 75 73 2e 20 20 54 68 69 73 20 69  revious.  This i
2140f 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73  nformation is us
21410 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61  ed for testing a
21411 6e 64 0a 2a 2a 20 61 6e 61 6c 79 73 69 73 20 6f  nd.** analysis o
21412 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  nly..*/.SQLITE_A
21413 50 49 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f  PI char sqlite3_
21414 71 75 65 72 79 5f 70 6c 61 6e 5b 42 4d 53 2a 32  query_plan[BMS*2
21415 2a 34 30 5d 3b 20 20 2f 2a 20 54 65 78 74 20 6f  *40];  /* Text o
21416 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 73 74  f the join */.st
21417 61 74 69 63 20 69 6e 74 20 6e 51 50 6c 61 6e 20  atic int nQPlan 
21418 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
21419 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 73    /* Next free s
2141a 6c 6f 77 20 69 6e 20 5f 71 75 65 72 79 5f 70 6c  low in _query_pl
2141b 61 6e 5b 5d 20 2a 2f 0a 0a 23 65 6e 64 69 66 20  an[] */..#endif 
2141c 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a  /* SQLITE_TEST *
2141d 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  /.../*.** Free a
2141e 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63   WhereInfo struc
2141f 74 75 72 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ture.*/.static v
21420 6f 69 64 20 77 68 65 72 65 49 6e 66 6f 46 72 65  oid whereInfoFre
21421 65 28 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49  e(WhereInfo *pWI
21422 6e 66 6f 29 7b 0a 20 20 69 66 28 20 70 57 49 6e  nfo){.  if( pWIn
21423 66 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  fo ){.    int i;
21424 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
21425 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 20  pWInfo->nLevel; 
21426 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
21427 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
21428 70 49 6e 66 6f 20 3d 20 70 57 49 6e 66 6f 2d 3e  pInfo = pWInfo->
21429 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20  a[i].pIdxInfo;. 
2142a 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 20 29       if( pInfo )
2142b 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  {.        if( pI
2142c 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
2142d 64 78 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20  dxStr ){.       
2142e 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a 20     /* Coverage: 
2142f 44 6f 6e 27 74 20 74 68 69 6e 6b 20 74 68 69 73  Don't think this
21430 20 63 61 6e 20 62 65 20 72 65 61 63 68 65 64 2e   can be reached.
21431 20 42 79 20 74 68 65 20 74 69 6d 65 20 74 68 69   By the time thi
21432 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66  s.          ** f
21433 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
21434 64 2c 20 74 68 65 20 69 6e 64 65 78 2d 73 74 72  d, the index-str
21435 69 6e 67 73 20 68 61 76 65 20 62 65 65 6e 20 70  ings have been p
21436 61 73 73 65 64 0a 20 20 20 20 20 20 20 20 20 20  assed.          
21437 2a 2a 20 74 6f 20 74 68 65 20 76 64 62 65 20 6c  ** to the vdbe l
21438 61 79 65 72 20 66 6f 72 20 64 65 6c 65 74 69 6f  ayer for deletio
21439 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  n..          */.
2143a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2143b 33 5f 66 72 65 65 28 70 49 6e 66 6f 2d 3e 69 64  3_free(pInfo->id
2143c 78 53 74 72 29 3b 0a 20 20 20 20 20 20 20 20 7d  xStr);.        }
2143d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2143e 5f 66 72 65 65 28 70 49 6e 66 6f 29 3b 0a 20 20  _free(pInfo);.  
2143f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
21440 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 57 49  sqlite3_free(pWI
21441 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  nfo);.  }.}.../*
21442 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65  .** Generate the
21443 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
21444 65 20 6c 6f 6f 70 20 75 73 65 64 20 66 6f 72 20  e loop used for 
21445 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
21446 63 65 73 73 69 6e 67 2e 0a 2a 2a 20 54 68 65 20  cessing..** The 
21447 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
21448 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
21449 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65  opaque structure
2144a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a   that contains.*
2144b 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65  * information ne
2144c 65 64 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 74  eded to terminat
2144d 65 20 74 68 65 20 6c 6f 6f 70 2e 20 20 4c 61 74  e the loop.  Lat
2144e 65 72 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  er, the calling 
2144f 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 68 6f 75 6c  routine.** shoul
21450 64 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  d invoke sqlite3
21451 57 68 65 72 65 45 6e 64 28 29 20 77 69 74 68 20  WhereEnd() with 
21452 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
21453 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
21454 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f  n.** in order to
21455 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 57 48   complete the WH
21456 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65  ERE clause proce
21457 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssing..**.** If 
21458 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
21459 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
2145a 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  turns NULL..**.*
2145b 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61  * The basic idea
2145c 20 69 73 20 74 6f 20 64 6f 20 61 20 6e 65 73 74   is to do a nest
2145d 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65 20 6c 6f 6f  ed loop, one loo
2145e 70 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65  p for each table
2145f 20 69 6e 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20   in.** the FROM 
21460 63 6c 61 75 73 65 20 6f 66 20 61 20 73 65 6c 65  clause of a sele
21461 63 74 2e 20 20 28 49 4e 53 45 52 54 20 61 6e 64  ct.  (INSERT and
21462 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
21463 74 73 20 61 72 65 20 74 68 65 0a 2a 2a 20 73 61  ts are the.** sa
21464 6d 65 20 61 73 20 61 20 53 45 4c 45 43 54 20 77  me as a SELECT w
21465 69 74 68 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c  ith only a singl
21466 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  e table in the F
21467 52 4f 4d 20 63 6c 61 75 73 65 2e 29 20 20 46 6f  ROM clause.)  Fo
21468 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66  r.** example, if
21469 20 74 68 65 20 53 51 4c 20 69 73 20 74 68 69 73   the SQL is this
2146a 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 45  :.**.**       SE
2146b 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20  LECT * FROM t1, 
2146c 74 32 2c 20 74 33 20 57 48 45 52 45 20 2e 2e 2e  t2, t3 WHERE ...
2146d 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68 65  ;.**.** Then the
2146e 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
2146f 69 73 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20  is conceptually 
21470 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  like the followi
21471 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66  ng:.**.**      f
21472 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e 20 74  oreach row1 in t
21473 31 20 64 6f 20 20 20 20 20 20 20 5c 20 20 20 20  1 do       \    
21474 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 2a  Code generated.*
21475 2a 20 20 20 20 20 20 20 20 66 6f 72 65 61 63 68  *        foreach
21476 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 20 20   row2 in t2 do  
21477 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74      |-- by sqlit
21478 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 0a 2a  e3WhereBegin().*
21479 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 65 61  *          forea
2147a 63 68 20 72 6f 77 33 20 69 6e 20 74 33 20 64 6f  ch row3 in t3 do
2147b 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20     /.**         
2147c 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20     ....**       
2147d 20 20 20 65 6e 64 20 20 20 20 20 20 20 20 20 20     end          
2147e 20 20 20 20 20 20 20 20 20 20 20 5c 20 20 20 20             \    
2147f 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 2a  Code generated.*
21480 2a 20 20 20 20 20 20 20 20 65 6e 64 20 20 20 20  *        end    
21481 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21482 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74      |-- by sqlit
21483 65 33 57 68 65 72 65 45 6e 64 28 29 0a 2a 2a 20  e3WhereEnd().** 
21484 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20       end        
21485 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21486 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68   /.**.** Note th
21487 61 74 20 74 68 65 20 6c 6f 6f 70 73 20 6d 69 67  at the loops mig
21488 68 74 20 6e 6f 74 20 62 65 20 6e 65 73 74 65 64  ht not be nested
21489 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 69 6e   in the order in
2148a 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20 61   which they.** a
2148b 70 70 65 61 72 20 69 6e 20 74 68 65 20 46 52 4f  ppear in the FRO
2148c 4d 20 63 6c 61 75 73 65 20 69 66 20 61 20 64 69  M clause if a di
2148d 66 66 65 72 65 6e 74 20 6f 72 64 65 72 20 69 73  fferent order is
2148e 20 62 65 74 74 65 72 20 61 62 6c 65 20 74 6f 20   better able to 
2148f 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 69  make.** use of i
21490 6e 64 69 63 65 73 2e 20 20 4e 6f 74 65 20 61 6c  ndices.  Note al
21491 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68 65  so that when the
21492 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 61 70 70   IN operator app
21493 65 61 72 73 20 69 6e 0a 2a 2a 20 74 68 65 20 57  ears in.** the W
21494 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69 74 20  HERE clause, it 
21495 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20  might result in 
21496 61 64 64 69 74 69 6f 6e 61 6c 20 6e 65 73 74 65  additional neste
21497 64 20 6c 6f 6f 70 73 20 66 6f 72 0a 2a 2a 20 73  d loops for.** s
21498 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68 20  canning through 
21499 61 6c 6c 20 76 61 6c 75 65 73 20 6f 6e 20 74 68  all values on th
2149a 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
2149b 65 20 6f 66 20 74 68 65 20 49 4e 2e 0a 2a 2a 0a  e of the IN..**.
2149c 2a 2a 20 54 68 65 72 65 20 61 72 65 20 42 74 72  ** There are Btr
2149d 65 65 20 63 75 72 73 6f 72 73 20 61 73 73 6f 63  ee cursors assoc
2149e 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20  iated with each 
2149f 74 61 62 6c 65 2e 20 20 74 31 20 75 73 65 73 20  table.  t1 uses 
214a0 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72  cursor.** number
214a1 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e   pTabList->a[0].
214a2 69 43 75 72 73 6f 72 2e 20 20 74 32 20 75 73 65  iCursor.  t2 use
214a3 73 20 74 68 65 20 63 75 72 73 6f 72 20 70 54 61  s the cursor pTa
214a4 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69 43 75 72  bList->a[1].iCur
214a5 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20 66  sor..** And so f
214a6 6f 72 74 68 2e 20 20 54 68 69 73 20 72 6f 75 74  orth.  This rout
214a7 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
214a8 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 6f 73 65  de to open those
214a9 20 56 44 42 45 20 63 75 72 73 6f 72 73 0a 2a 2a   VDBE cursors.**
214aa 20 61 6e 64 20 73 71 6c 69 74 65 33 57 68 65 72   and sqlite3Wher
214ab 65 45 6e 64 28 29 20 67 65 6e 65 72 61 74 65 73  eEnd() generates
214ac 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 6c 6f   the code to clo
214ad 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54  se them..**.** T
214ae 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 71 6c  he code that sql
214af 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
214b0 20 67 65 6e 65 72 61 74 65 73 20 6c 65 61 76 65   generates leave
214b1 73 20 74 68 65 20 63 75 72 73 6f 72 73 20 6e 61  s the cursors na
214b2 6d 65 64 0a 2a 2a 20 69 6e 20 70 54 61 62 4c 69  med.** in pTabLi
214b3 73 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74  st pointing at t
214b4 68 65 69 72 20 61 70 70 72 6f 70 72 69 61 74 65  heir appropriate
214b5 20 65 6e 74 72 69 65 73 2e 20 20 54 68 65 20 5b   entries.  The [
214b6 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a 20 63 61 6e  ...] code.** can
214b7 20 75 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61   use OP_Column a
214b8 6e 64 20 4f 50 5f 52 6f 77 69 64 20 6f 70 63 6f  nd OP_Rowid opco
214b9 64 65 73 20 6f 6e 20 74 68 65 73 65 20 63 75 72  des on these cur
214ba 73 6f 72 73 20 74 6f 20 65 78 74 72 61 63 74 0a  sors to extract.
214bb 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  ** data from the
214bc 20 76 61 72 69 6f 75 73 20 74 61 62 6c 65 73 20   various tables 
214bd 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a  of the loop..**.
214be 2a 2a 20 49 66 20 74 68 65 20 57 48 45 52 45 20  ** If the WHERE 
214bf 63 6c 61 75 73 65 20 69 73 20 65 6d 70 74 79 2c  clause is empty,
214c0 20 74 68 65 20 66 6f 72 65 61 63 68 20 6c 6f 6f   the foreach loo
214c1 70 73 20 6d 75 73 74 20 65 61 63 68 20 73 63 61  ps must each sca
214c2 6e 20 74 68 65 69 72 0a 2a 2a 20 65 6e 74 69 72  n their.** entir
214c3 65 20 74 61 62 6c 65 73 2e 20 20 54 68 75 73 20  e tables.  Thus 
214c4 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f 69 6e  a three-way join
214c5 20 69 73 20 61 6e 20 4f 28 4e 5e 33 29 20 6f 70   is an O(N^3) op
214c6 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 66  eration.  But if
214c7 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 73 20 68  .** the tables h
214c8 61 76 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20  ave indices and 
214c9 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73 20  there are terms 
214ca 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
214cb 75 73 65 20 74 68 61 74 0a 2a 2a 20 72 65 66 65  use that.** refe
214cc 72 20 74 6f 20 74 68 6f 73 65 20 69 6e 64 69 63  r to those indic
214cd 65 73 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20 74  es, a complete t
214ce 61 62 6c 65 20 73 63 61 6e 20 63 61 6e 20 62 65  able scan can be
214cf 20 61 76 6f 69 64 65 64 20 61 6e 64 20 74 68 65   avoided and the
214d0 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 72 75  .** code will ru
214d1 6e 20 6d 75 63 68 20 66 61 73 74 65 72 2e 20 20  n much faster.  
214d2 4d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b  Most of the work
214d3 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
214d4 20 69 73 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20   is checking.** 
214d5 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20  to see if there 
214d6 61 72 65 20 69 6e 64 69 63 65 73 20 74 68 61 74  are indices that
214d7 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
214d8 73 70 65 65 64 20 75 70 20 74 68 65 20 6c 6f 6f  speed up the loo
214d9 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20 6f  p..**.** Terms o
214da 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
214db 73 65 20 61 72 65 20 61 6c 73 6f 20 75 73 65 64  se are also used
214dc 20 74 6f 20 6c 69 6d 69 74 20 77 68 69 63 68 20   to limit which 
214dd 72 6f 77 73 20 61 63 74 75 61 6c 6c 79 0a 2a 2a  rows actually.**
214de 20 6d 61 6b 65 20 69 74 20 74 6f 20 74 68 65 20   make it to the 
214df 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20 6d 69 64  "..." in the mid
214e0 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  dle of the loop.
214e1 20 20 41 66 74 65 72 20 65 61 63 68 20 22 66 6f    After each "fo
214e2 72 65 61 63 68 22 2c 0a 2a 2a 20 74 65 72 6d 73  reach",.** terms
214e3 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
214e4 61 75 73 65 20 74 68 61 74 20 75 73 65 20 6f 6e  ause that use on
214e5 6c 79 20 74 65 72 6d 73 20 69 6e 20 74 68 61 74  ly terms in that
214e6 20 6c 6f 6f 70 20 61 6e 64 20 6f 75 74 65 72 0a   loop and outer.
214e7 2a 2a 20 6c 6f 6f 70 73 20 61 72 65 20 65 76 61  ** loops are eva
214e8 6c 75 61 74 65 64 20 61 6e 64 20 69 66 20 66 61  luated and if fa
214e9 6c 73 65 20 61 20 6a 75 6d 70 20 69 73 20 6d 61  lse a jump is ma
214ea 64 65 20 61 72 6f 75 6e 64 20 61 6c 6c 20 73 75  de around all su
214eb 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 6e 65  bsequent.** inne
214ec 72 20 6c 6f 6f 70 73 20 28 6f 72 20 61 72 6f 75  r loops (or arou
214ed 6e 64 20 74 68 65 20 22 2e 2e 2e 22 20 69 66 20  nd the "..." if 
214ee 74 68 65 20 74 65 73 74 20 6f 63 63 75 72 73 20  the test occurs 
214ef 77 69 74 68 69 6e 20 74 68 65 20 69 6e 6e 65 72  within the inner
214f0 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f 6f 70 29 0a  -.** most loop).
214f1 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20 4a 4f 49 4e  **.** OUTER JOIN
214f2 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f 75 74 65 72  S.**.** An outer
214f3 20 6a 6f 69 6e 20 6f 66 20 74 61 62 6c 65 73 20   join of tables 
214f4 74 31 20 61 6e 64 20 74 32 20 69 73 20 63 6f 6e  t1 and t2 is con
214f5 63 65 70 74 61 6c 6c 79 20 63 6f 64 65 64 20 61  ceptally coded a
214f6 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
214f7 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31      foreach row1
214f8 20 69 6e 20 74 31 20 64 6f 0a 2a 2a 20 20 20 20   in t1 do.**    
214f9 20 20 66 6c 61 67 20 3d 20 30 0a 2a 2a 20 20 20    flag = 0.**   
214fa 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 32 20     foreach row2 
214fb 69 6e 20 74 32 20 64 6f 0a 2a 2a 20 20 20 20 20  in t2 do.**     
214fc 20 20 20 73 74 61 72 74 3a 0a 2a 2a 20 20 20 20     start:.**    
214fd 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20        ....**    
214fe 20 20 20 20 20 20 66 6c 61 67 20 3d 20 31 0a 2a        flag = 1.*
214ff 2a 20 20 20 20 20 20 65 6e 64 0a 2a 2a 20 20 20  *      end.**   
21500 20 20 20 69 66 20 66 6c 61 67 3d 3d 30 20 74 68     if flag==0 th
21501 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 6d 6f 76  en.**        mov
21502 65 20 74 68 65 20 72 6f 77 32 20 63 75 72 73 6f  e the row2 curso
21503 72 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 0a  r to a null row.
21504 2a 2a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73  **        goto s
21505 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 66 69 0a  tart.**      fi.
21506 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a 0a 2a 2a 20  **    end.**.** 
21507 4f 52 44 45 52 20 42 59 20 43 4c 41 55 53 45 20  ORDER BY CLAUSE 
21508 50 52 4f 43 45 53 53 49 4e 47 0a 2a 2a 0a 2a 2a  PROCESSING.**.**
21509 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 61   *ppOrderBy is a
2150a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2150b 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2150c 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
2150d 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66 20 74 68 65  ement,.** if the
2150e 72 65 20 69 73 20 6f 6e 65 2e 20 20 49 66 20 74  re is one.  If t
2150f 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52  here is no ORDER
21510 20 42 59 20 63 6c 61 75 73 65 20 6f 72 20 69 66   BY clause or if
21511 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
21512 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   is called from 
21513 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  an UPDATE or DEL
21514 45 54 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  ETE statement, t
21515 68 65 6e 20 70 70 4f 72 64 65 72 42 79 20 69 73  hen ppOrderBy is
21516 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   NULL..**.** If 
21517 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20  an index can be 
21518 75 73 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  used so that the
21519 20 6e 61 74 75 72 61 6c 20 6f 75 74 70 75 74 20   natural output 
2151a 6f 72 64 65 72 20 6f 66 20 74 68 65 20 74 61 62  order of the tab
2151b 6c 65 0a 2a 2a 20 73 63 61 6e 20 69 73 20 63 6f  le.** scan is co
2151c 72 72 65 63 74 20 66 6f 72 20 74 68 65 20 4f 52  rrect for the OR
2151d 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74  DER BY clause, t
2151e 68 65 6e 20 74 68 61 74 20 69 6e 64 65 78 20 69  hen that index i
2151f 73 20 75 73 65 64 20 61 6e 64 0a 2a 2a 20 2a 70  s used and.** *p
21520 70 4f 72 64 65 72 42 79 20 69 73 20 73 65 74 20  pOrderBy is set 
21521 74 6f 20 4e 55 4c 4c 2e 20 20 54 68 69 73 20 69  to NULL.  This i
21522 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
21523 6e 20 74 68 61 74 20 70 72 65 76 65 6e 74 73 20  n that prevents 
21524 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65 73 73 61 72  an.** unnecessar
21525 79 20 73 6f 72 74 20 6f 66 20 74 68 65 20 72 65  y sort of the re
21526 73 75 6c 74 20 73 65 74 20 69 66 20 61 6e 20 69  sult set if an i
21527 6e 64 65 78 20 61 70 70 72 6f 70 72 69 61 74 65  ndex appropriate
21528 20 66 6f 72 20 74 68 65 0a 2a 2a 20 4f 52 44 45   for the.** ORDE
21529 52 20 42 59 20 63 6c 61 75 73 65 20 61 6c 72 65  R BY clause alre
2152a 61 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a  ady exists..**.*
2152b 2a 20 49 66 20 74 68 65 20 77 68 65 72 65 20 63  * If the where c
2152c 6c 61 75 73 65 20 6c 6f 6f 70 73 20 63 61 6e 6e  lause loops cann
2152d 6f 74 20 62 65 20 61 72 72 61 6e 67 65 64 20 74  ot be arranged t
2152e 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f  o provide the co
2152f 72 72 65 63 74 0a 2a 2a 20 6f 75 74 70 75 74 20  rrect.** output 
21530 6f 72 64 65 72 2c 20 74 68 65 6e 20 74 68 65 20  order, then the 
21531 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 75 6e  *ppOrderBy is un
21532 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
21533 54 45 5f 50 52 49 56 41 54 45 20 57 68 65 72 65  TE_PRIVATE Where
21534 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65  Info *sqlite3Whe
21535 72 65 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65  reBegin(.  Parse
21536 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
21537 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
21538 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
21539 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20  ist *pTabList,  
2153a 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61    /* A list of a
2153b 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20  ll tables to be 
2153c 73 63 61 6e 6e 65 64 20 2a 2f 0a 20 20 45 78 70  scanned */.  Exp
2153d 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20  r *pWhere,      
2153e 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
2153f 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
21540 4c 69 73 74 20 2a 2a 70 70 4f 72 64 65 72 42 79  List **ppOrderBy
21541 20 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59    /* An ORDER BY
21542 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e 55 4c 4c   clause, or NULL
21543 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20   */.){.  int i; 
21544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21545 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
21546 74 65 72 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e  ter */.  WhereIn
21547 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20  fo *pWInfo;     
21548 20 20 20 20 2f 2a 20 57 69 6c 6c 20 62 65 63 6f      /* Will beco
21549 6d 65 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  me the return va
2154a 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63  lue of this func
2154b 74 69 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20 2a  tion */.  Vdbe *
2154c 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2154d 65 3b 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74  e;   /* The virt
2154e 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67  ual database eng
2154f 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 62 72 6b  ine */.  int brk
21550 2c 20 63 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20  , cont = 0;     
21551 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 65 73      /* Addresses
21552 20 75 73 65 64 20 64 75 72 69 6e 67 20 63 6f 64   used during cod
21553 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 2a 2f 0a  e generation */.
21554 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
21555 64 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dy;          /* 
21556 43 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65  Cursors that are
21557 20 6e 6f 74 20 79 65 74 20 70 6f 73 69 74 69 6f   not yet positio
21558 6e 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 54 65  ned */.  WhereTe
21559 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20  rm *pTerm;      
2155a 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20      /* A single 
2155b 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52  term in the WHER
2155c 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  E clause */.  Ex
2155d 70 72 4d 61 73 6b 53 65 74 20 6d 61 73 6b 53 65  prMaskSet maskSe
2155e 74 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  t;       /* The 
2155f 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20  expression mask 
21560 73 65 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  set */.  WhereCl
21561 61 75 73 65 20 77 63 3b 20 20 20 20 20 20 20 20  ause wc;        
21562 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
21563 20 63 6c 61 75 73 65 20 69 73 20 64 69 76 69 64   clause is divid
21564 65 64 20 69 6e 74 6f 20 74 68 65 73 65 20 74 65  ed into these te
21565 72 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  rms */.  struct 
21566 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54  SrcList_item *pT
21567 61 62 49 74 65 6d 3b 20 20 2f 2a 20 41 20 73 69  abItem;  /* A si
21568 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20  ngle entry from 
21569 70 54 61 62 4c 69 73 74 20 2a 2f 0a 20 20 57 68  pTabList */.  Wh
2156a 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
2156b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2156c 20 41 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20   A single level 
2156d 69 6e 20 74 68 65 20 70 57 49 6e 66 6f 20 6c 69  in the pWInfo li
2156e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 46 72 6f  st */.  int iFro
2156f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
21570 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
21571 20 75 6e 75 73 65 64 20 46 52 4f 4d 20 63 6c 61   unused FROM cla
21572 75 73 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  use element */. 
21573 20 69 6e 74 20 61 6e 64 46 6c 61 67 73 3b 20 20   int andFlags;  
21574 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
21575 4e 44 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f  ND-ed combinatio
21576 6e 20 6f 66 20 61 6c 6c 20 77 63 2e 61 5b 5d 2e  n of all wc.a[].
21577 66 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74  flags */.  sqlit
21578 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
21579 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2157a 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
2157b 0a 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72  .  /* The number
2157c 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68   of tables in th
2157d 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
2157e 20 6c 69 6d 69 74 65 64 20 62 79 20 74 68 65 20   limited by the 
2157f 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62  number of.  ** b
21580 69 74 73 20 69 6e 20 61 20 42 69 74 6d 61 73 6b  its in a Bitmask
21581 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61   .  */.  if( pTa
21582 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 42 4d 53 20  bList->nSrc>BMS 
21583 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
21584 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
21585 61 74 20 6d 6f 73 74 20 25 64 20 74 61 62 6c 65  at most %d table
21586 73 20 69 6e 20 61 20 6a 6f 69 6e 22 2c 20 42 4d  s in a join", BM
21587 53 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  S);.    return 0
21588 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 70 6c 69  ;.  }..  /* Spli
21589 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  t the WHERE clau
2158a 73 65 20 69 6e 74 6f 20 73 65 70 61 72 61 74 65  se into separate
2158b 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20   subexpressions 
2158c 77 68 65 72 65 20 65 61 63 68 0a 20 20 2a 2a 20  where each.  ** 
2158d 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73  subexpression is
2158e 20 73 65 70 61 72 61 74 65 64 20 62 79 20 61 6e   separated by an
2158f 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20   AND operator.. 
21590 20 2a 2f 0a 20 20 69 6e 69 74 4d 61 73 6b 53 65   */.  initMaskSe
21591 74 28 26 6d 61 73 6b 53 65 74 29 3b 0a 20 20 77  t(&maskSet);.  w
21592 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 26  hereClauseInit(&
21593 77 63 2c 20 70 50 61 72 73 65 2c 20 26 6d 61 73  wc, pParse, &mas
21594 6b 53 65 74 29 3b 0a 20 20 77 68 65 72 65 53 70  kSet);.  whereSp
21595 6c 69 74 28 26 77 63 2c 20 70 57 68 65 72 65 2c  lit(&wc, pWhere,
21596 20 54 4b 5f 41 4e 44 29 3b 0a 20 20 20 20 0a 20   TK_AND);.    . 
21597 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64   /* Allocate and
21598 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
21599 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74  WhereInfo struct
2159a 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ure that will be
2159b 63 6f 6d 65 20 74 68 65 0a 20 20 2a 2a 20 72 65  come the.  ** re
2159c 74 75 72 6e 20 76 61 6c 75 65 2e 0a 20 20 2a 2f  turn value..  */
2159d 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
2159e 64 62 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73  db;.  pWInfo = s
2159f 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
215a0 72 6f 28 64 62 2c 20 20 0a 20 20 20 20 20 20 20  ro(db,  .       
215a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
215a2 69 7a 65 6f 66 28 57 68 65 72 65 49 6e 66 6f 29  izeof(WhereInfo)
215a3 20 2b 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   + pTabList->nSr
215a4 63 2a 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 65  c*sizeof(WhereLe
215a5 76 65 6c 29 29 3b 0a 20 20 69 66 28 20 64 62 2d  vel));.  if( db-
215a6 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
215a7 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42  .    goto whereB
215a8 65 67 69 6e 4e 6f 4d 65 6d 3b 0a 20 20 7d 0a 20  eginNoMem;.  }. 
215a9 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 20   pWInfo->nLevel 
215aa 3d 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  = pTabList->nSrc
215ab 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72  ;.  pWInfo->pPar
215ac 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70  se = pParse;.  p
215ad 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 20  WInfo->pTabList 
215ae 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 70 57  = pTabList;.  pW
215af 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 20 3d 20 73  Info->iBreak = s
215b0 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
215b1 62 65 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 53 70  bel(v);..  /* Sp
215b2 65 63 69 61 6c 20 63 61 73 65 3a 20 61 20 57 48  ecial case: a WH
215b3 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20  ERE clause that 
215b4 69 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20 45 76  is constant.  Ev
215b5 61 6c 75 61 74 65 20 74 68 65 0a 20 20 2a 2a 20  aluate the.  ** 
215b6 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 65  expression and e
215b7 69 74 68 65 72 20 6a 75 6d 70 20 6f 76 65 72 20  ither jump over 
215b8 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20  all of the code 
215b9 6f 72 20 66 61 6c 6c 20 74 68 72 75 2e 0a 20 20  or fall thru..  
215ba 2a 2f 0a 20 20 69 66 28 20 70 57 68 65 72 65 20  */.  if( pWhere 
215bb 26 26 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53  && (pTabList->nS
215bc 72 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  rc==0 || sqlite3
215bd 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f  ExprIsConstantNo
215be 74 4a 6f 69 6e 28 70 57 68 65 72 65 29 29 20 29  tJoin(pWhere)) )
215bf 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
215c0 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
215c1 20 70 57 68 65 72 65 2c 20 70 57 49 6e 66 6f 2d   pWhere, pWInfo-
215c2 3e 69 42 72 65 61 6b 2c 20 31 29 3b 0a 20 20 20  >iBreak, 1);.   
215c3 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 7d   pWhere = 0;.  }
215c4 0a 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 61  ..  /* Analyze a
215c5 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 65 78 70  ll of the subexp
215c6 72 65 73 73 69 6f 6e 73 2e 20 20 4e 6f 74 65 20  ressions.  Note 
215c7 74 68 61 74 20 65 78 70 72 41 6e 61 6c 79 7a 65  that exprAnalyze
215c8 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 61 64  () might.  ** ad
215c9 64 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65  d new virtual te
215ca 72 6d 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64  rms onto the end
215cb 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
215cc 61 75 73 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74  ause.  We do not
215cd 0a 20 20 2a 2a 20 77 61 6e 74 20 74 6f 20 61 6e  .  ** want to an
215ce 61 6c 79 7a 65 20 74 68 65 73 65 20 76 69 72 74  alyze these virt
215cf 75 61 6c 20 74 65 72 6d 73 2c 20 73 6f 20 73 74  ual terms, so st
215d0 61 72 74 20 61 6e 61 6c 79 7a 69 6e 67 20 61 74  art analyzing at
215d1 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 61 6e   the end.  ** an
215d2 64 20 77 6f 72 6b 20 66 6f 72 77 61 72 64 20 73  d work forward s
215d3 6f 20 74 68 61 74 20 74 68 65 20 61 64 64 65 64  o that the added
215d4 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73 20 61   virtual terms a
215d5 72 65 20 6e 65 76 65 72 20 70 72 6f 63 65 73 73  re never process
215d6 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ed..  */.  for(i
215d7 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  =0; i<pTabList->
215d8 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSrc; i++){.    
215d9 63 72 65 61 74 65 4d 61 73 6b 28 26 6d 61 73 6b  createMask(&mask
215da 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61  Set, pTabList->a
215db 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20  [i].iCursor);.  
215dc 7d 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41  }.  exprAnalyzeA
215dd 6c 6c 28 70 54 61 62 4c 69 73 74 2c 20 26 77 63  ll(pTabList, &wc
215de 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
215df 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
215e0 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e   goto whereBegin
215e1 4e 6f 4d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  NoMem;.  }..  /*
215e2 20 43 68 6f 73 65 20 74 68 65 20 62 65 73 74 20   Chose the best 
215e3 69 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72  index to use for
215e4 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74   each table in t
215e5 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a  he FROM clause..
215e6 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c    **.  ** This l
215e7 6f 6f 70 20 66 69 6c 6c 73 20 69 6e 20 74 68 65  oop fills in the
215e8 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64   following field
215e9 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 70  s:.  **.  **   p
215ea 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78 20  WInfo->a[].pIdx 
215eb 20 20 20 20 20 54 68 65 20 69 6e 64 65 78 20 74       The index t
215ec 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 6c  o use for this l
215ed 65 76 65 6c 20 6f 66 20 74 68 65 20 6c 6f 6f 70  evel of the loop
215ee 2e 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  ..  **   pWInfo-
215ef 3e 61 5b 5d 2e 66 6c 61 67 73 20 20 20 20 20 57  >a[].flags     W
215f0 48 45 52 45 5f 78 78 78 20 66 6c 61 67 73 20 61  HERE_xxx flags a
215f1 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70  ssociated with p
215f2 49 64 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66  Idx.  **   pWInf
215f3 6f 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20 20 20 20  o->a[].nEq      
215f4 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d   The number of =
215f5 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61  = and IN constra
215f6 69 6e 74 73 0a 20 20 2a 2a 20 20 20 70 57 49 6e  ints.  **   pWIn
215f7 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20 20  fo->a[].iFrom   
215f8 20 20 57 68 65 6e 20 74 65 72 6d 20 6f 66 20 74    When term of t
215f9 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69  he FROM clause i
215fa 73 20 62 65 69 6e 67 20 63 6f 64 65 64 0a 20 20  s being coded.  
215fb 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  **   pWInfo->a[]
215fc 2e 69 54 61 62 43 75 72 20 20 20 54 68 65 20 56  .iTabCur   The V
215fd 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
215fe 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
215ff 65 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  e.  **   pWInfo-
21600 3e 61 5b 5d 2e 69 49 64 78 43 75 72 20 20 20 54  >a[].iIdxCur   T
21601 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66  he VDBE cursor f
21602 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a  or the index.  *
21603 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70  *.  ** This loop
21604 20 61 6c 73 6f 20 66 69 67 75 72 65 73 20 6f 75   also figures ou
21605 74 20 74 68 65 20 6e 65 73 74 69 6e 67 20 6f 72  t the nesting or
21606 64 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e  der of tables in
21607 20 74 68 65 20 46 52 4f 4d 0a 20 20 2a 2a 20 63   the FROM.  ** c
21608 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6e 6f  lause..  */.  no
21609 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61  tReady = ~(Bitma
2160a 73 6b 29 30 3b 0a 20 20 70 54 61 62 49 74 65 6d  sk)0;.  pTabItem
2160b 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 3b 0a   = pTabList->a;.
2160c 20 20 70 4c 65 76 65 6c 20 3d 20 70 57 49 6e 66    pLevel = pWInf
2160d 6f 2d 3e 61 3b 0a 20 20 61 6e 64 46 6c 61 67 73  o->a;.  andFlags
2160e 20 3d 20 7e 30 3b 0a 20 20 57 48 45 52 45 54 52   = ~0;.  WHERETR
2160f 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69  ACE(("*** Optimi
21610 7a 65 72 20 53 74 61 72 74 20 2a 2a 2a 5c 6e 22  zer Start ***\n"
21611 29 29 3b 0a 20 20 66 6f 72 28 69 3d 69 46 72 6f  ));.  for(i=iFro
21612 6d 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e  m=0, pLevel=pWIn
21613 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73  fo->a; i<pTabLis
21614 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c  t->nSrc; i++, pL
21615 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 49 6e 64  evel++){.    Ind
21616 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
21617 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
21618 78 20 66 6f 72 20 46 52 4f 4d 20 74 61 62 6c 65  x for FROM table
21619 20 61 74 20 70 54 61 62 49 74 65 6d 20 2a 2f 0a   at pTabItem */.
2161a 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20      int flags;  
2161b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2161c 2f 2a 20 46 6c 61 67 73 20 61 73 73 73 6f 63 69  /* Flags asssoci
2161d 61 74 65 64 20 77 69 74 68 20 70 49 64 78 20 2a  ated with pIdx *
2161e 2f 0a 20 20 20 20 69 6e 74 20 6e 45 71 3b 20 20  /.    int nEq;  
2161f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21620 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d    /* Number of =
21621 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69  = or IN constrai
21622 6e 74 73 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c  nts */.    doubl
21623 65 20 63 6f 73 74 3b 20 20 20 20 20 20 20 20 20  e cost;         
21624 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
21625 73 74 20 66 6f 72 20 70 49 64 78 20 2a 2f 0a 20  st for pIdx */. 
21626 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20     int j;       
21627 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
21628 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
21629 65 72 20 46 52 4f 4d 20 74 61 62 6c 65 73 20 2a  er FROM tables *
2162a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 42 65  /.    Index *pBe
2162b 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  st = 0;         
2162c 20 20 2f 2a 20 54 68 65 20 62 65 73 74 20 69 6e    /* The best in
2162d 64 65 78 20 73 65 65 6e 20 73 6f 20 66 61 72 20  dex seen so far 
2162e 2a 2f 0a 20 20 20 20 69 6e 74 20 62 65 73 74 46  */.    int bestF
2162f 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20  lags = 0;       
21630 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f     /* Flags asso
21631 63 69 61 74 65 64 20 77 69 74 68 20 70 42 65 73  ciated with pBes
21632 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 65 73  t */.    int bes
21633 74 4e 45 71 20 3d 20 30 3b 20 20 20 20 20 20 20  tNEq = 0;       
21634 20 20 20 20 20 2f 2a 20 6e 45 71 20 61 73 73 6f       /* nEq asso
21635 63 69 61 74 65 64 20 77 69 74 68 20 70 42 65 73  ciated with pBes
21636 74 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20  t */.    double 
21637 6c 6f 77 65 73 74 43 6f 73 74 3b 20 20 20 20 20  lowestCost;     
21638 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66 20       /* Cost of 
21639 74 68 65 20 70 42 65 73 74 20 2a 2f 0a 20 20 20  the pBest */.   
2163a 20 69 6e 74 20 62 65 73 74 4a 20 3d 20 30 3b 20   int bestJ = 0; 
2163b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2163c 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6a 20 2a  The value of j *
2163d 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 3b  /.    Bitmask m;
2163e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2163f 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 76 61 6c    /* Bitmask val
21640 75 65 20 66 6f 72 20 6a 20 6f 72 20 62 65 73 74  ue for j or best
21641 4a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 6e 63  J */.    int onc
21642 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
21643 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 65       /* True whe
21644 6e 20 66 69 72 73 74 20 74 61 62 6c 65 20 69 73  n first table is
21645 20 73 65 65 6e 20 2a 2f 0a 20 20 20 20 73 71 6c   seen */.    sql
21646 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
21647 2a 70 49 6e 64 65 78 3b 20 2f 2a 20 43 75 72 72  *pIndex; /* Curr
21648 65 6e 74 20 76 69 72 74 75 61 6c 20 69 6e 64 65  ent virtual inde
21649 78 20 2a 2f 0a 0a 20 20 20 20 6c 6f 77 65 73 74  x */..    lowest
2164a 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49  Cost = SQLITE_BI
2164b 47 5f 44 42 4c 3b 0a 20 20 20 20 66 6f 72 28 6a  G_DBL;.    for(j
2164c 3d 69 46 72 6f 6d 2c 20 70 54 61 62 49 74 65 6d  =iFrom, pTabItem
2164d 3d 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d  =&pTabList->a[j]
2164e 3b 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; j<pTabList->nS
2164f 72 63 3b 20 6a 2b 2b 2c 20 70 54 61 62 49 74 65  rc; j++, pTabIte
21650 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  m++){.      int 
21651 64 6f 4e 6f 74 52 65 6f 72 64 65 72 3b 20 20 2f  doNotReorder;  /
21652 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 74  * True if this t
21653 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  able should not 
21654 62 65 20 72 65 6f 72 64 65 72 65 64 20 2a 2f 0a  be reordered */.
21655 0a 20 20 20 20 20 20 64 6f 4e 6f 74 52 65 6f 72  .      doNotReor
21656 64 65 72 20 3d 20 20 28 70 54 61 62 49 74 65 6d  der =  (pTabItem
21657 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54  ->jointype & (JT
21658 5f 4c 45 46 54 7c 4a 54 5f 43 52 4f 53 53 29 29  _LEFT|JT_CROSS))
21659 21 3d 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6f  !=0;.      if( o
2165a 6e 63 65 20 26 26 20 64 6f 4e 6f 74 52 65 6f 72  nce && doNotReor
2165b 64 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  der ) break;.   
2165c 20 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 26     m = getMask(&
2165d 6d 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74 65  maskSet, pTabIte
2165e 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  m->iCursor);.   
2165f 20 20 20 69 66 28 20 28 6d 20 26 20 6e 6f 74 52     if( (m & notR
21660 65 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20  eady)==0 ){.    
21661 20 20 20 20 69 66 28 20 6a 3d 3d 69 46 72 6f 6d      if( j==iFrom
21662 20 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20 20 20 20   ) iFrom++;.    
21663 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
21664 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
21665 72 74 28 20 70 54 61 62 49 74 65 6d 2d 3e 70 54  rt( pTabItem->pT
21666 61 62 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ab );.#ifndef SQ
21667 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
21668 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 69 66 28  LTABLE.      if(
21669 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 49   IsVirtual(pTabI
2166a 74 65 6d 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20  tem->pTab) ){.  
2166b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
2166c 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78  dex_info **ppIdx
2166d 49 6e 66 6f 20 3d 20 26 70 57 49 6e 66 6f 2d 3e  Info = &pWInfo->
2166e 61 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20  a[j].pIdxInfo;. 
2166f 20 20 20 20 20 20 20 63 6f 73 74 20 3d 20 62 65         cost = be
21670 73 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28 70  stVirtualIndex(p
21671 50 61 72 73 65 2c 20 26 77 63 2c 20 70 54 61 62  Parse, &wc, pTab
21672 49 74 65 6d 2c 20 6e 6f 74 52 65 61 64 79 2c 0a  Item, notReady,.
21673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21674 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21675 70 70 4f 72 64 65 72 42 79 20 3f 20 2a 70 70 4f  ppOrderBy ? *ppO
21676 72 64 65 72 42 79 20 3a 20 30 2c 20 69 3d 3d 30  rderBy : 0, i==0
21677 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
21678 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21679 20 20 70 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20    ppIdxInfo);.  
2167a 20 20 20 20 20 20 66 6c 61 67 73 20 3d 20 57 48        flags = WH
2167b 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  ERE_VIRTUALTABLE
2167c 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78  ;.        pIndex
2167d 20 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20   = *ppIdxInfo;. 
2167e 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 64 65         if( pInde
2167f 78 20 26 26 20 70 49 6e 64 65 78 2d 3e 6f 72 64  x && pIndex->ord
21680 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 29 7b 0a  erByConsumed ){.
21681 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20            flags 
21682 3d 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  = WHERE_VIRTUALT
21683 41 42 4c 45 20 7c 20 57 48 45 52 45 5f 4f 52 44  ABLE | WHERE_ORD
21684 45 52 42 59 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ERBY;.        }.
21685 20 20 20 20 20 20 20 20 70 49 64 78 20 3d 20 30          pIdx = 0
21686 3b 0a 20 20 20 20 20 20 20 20 6e 45 71 20 3d 20  ;.        nEq = 
21687 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  0;.        if( (
21688 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f 32  SQLITE_BIG_DBL/2
21689 2e 30 29 3c 63 6f 73 74 20 29 7b 0a 20 20 20 20  .0)<cost ){.    
2168a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 73        /* The cos
2168b 74 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  t is not allowed
2168c 20 74 6f 20 62 65 20 6c 61 72 67 65 72 20 74 68   to be larger th
2168d 61 6e 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42  an SQLITE_BIG_DB
2168e 4c 20 28 74 68 65 0a 20 20 20 20 20 20 20 20 20  L (the.         
2168f 20 2a 2a 20 69 6e 69 74 61 6c 20 76 61 6c 75 65   ** inital value
21690 20 6f 66 20 6c 6f 77 65 73 74 43 6f 73 74 20 69   of lowestCost i
21691 6e 20 74 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20  n this loop. If 
21692 69 74 20 69 73 2c 20 74 68 65 6e 0a 20 20 20 20  it is, then.    
21693 20 20 20 20 20 20 2a 2a 20 74 68 65 20 28 63 6f        ** the (co
21694 73 74 3c 6c 6f 77 65 73 74 43 6f 73 74 29 20 74  st<lowestCost) t
21695 65 73 74 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e  est below will n
21696 65 76 65 72 20 62 65 20 74 72 75 65 20 61 6e 64  ever be true and
21697 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 4c  .          ** pL
21698 65 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 20 6e  evel->pBestIdx n
21699 65 76 65 72 20 73 65 74 2e 0a 20 20 20 20 20 20  ever set..      
2169a 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 20 20      */ .        
2169b 20 20 63 6f 73 74 20 3d 20 28 53 51 4c 49 54 45    cost = (SQLITE
2169c 5f 42 49 47 5f 44 42 4c 2f 32 2e 30 29 3b 0a 20  _BIG_DBL/2.0);. 
2169d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2169e 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 20  else .#endif.   
2169f 20 20 20 7b 0a 20 20 20 20 20 20 20 20 63 6f 73     {.        cos
216a0 74 20 3d 20 62 65 73 74 49 6e 64 65 78 28 70 50  t = bestIndex(pP
216a1 61 72 73 65 2c 20 26 77 63 2c 20 70 54 61 62 49  arse, &wc, pTabI
216a2 74 65 6d 2c 20 6e 6f 74 52 65 61 64 79 2c 0a 20  tem, notReady,. 
216a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
216a4 20 20 20 20 20 20 20 20 28 69 3d 3d 30 20 26 26          (i==0 &&
216a5 20 70 70 4f 72 64 65 72 42 79 29 20 3f 20 2a 70   ppOrderBy) ? *p
216a6 70 4f 72 64 65 72 42 79 20 3a 20 30 2c 0a 20 20  pOrderBy : 0,.  
216a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
216a8 20 20 20 20 20 20 20 26 70 49 64 78 2c 20 26 66         &pIdx, &f
216a9 6c 61 67 73 2c 20 26 6e 45 71 29 3b 0a 20 20 20  lags, &nEq);.   
216aa 20 20 20 20 20 70 49 6e 64 65 78 20 3d 20 30 3b       pIndex = 0;
216ab 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
216ac 66 28 20 63 6f 73 74 3c 6c 6f 77 65 73 74 43 6f  f( cost<lowestCo
216ad 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 6e  st ){.        on
216ae 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ce = 1;.        
216af 6c 6f 77 65 73 74 43 6f 73 74 20 3d 20 63 6f 73  lowestCost = cos
216b0 74 3b 0a 20 20 20 20 20 20 20 20 70 42 65 73 74  t;.        pBest
216b1 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20 20   = pIdx;.       
216b2 20 62 65 73 74 46 6c 61 67 73 20 3d 20 66 6c 61   bestFlags = fla
216b3 67 73 3b 0a 20 20 20 20 20 20 20 20 62 65 73 74  gs;.        best
216b4 4e 45 71 20 3d 20 6e 45 71 3b 0a 20 20 20 20 20  NEq = nEq;.     
216b5 20 20 20 62 65 73 74 4a 20 3d 20 6a 3b 0a 20 20     bestJ = j;.  
216b6 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 42        pLevel->pB
216b7 65 73 74 49 64 78 20 3d 20 70 49 6e 64 65 78 3b  estIdx = pIndex;
216b8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
216b9 66 28 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20  f( doNotReorder 
216ba 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
216bb 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
216bc 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 63 68  *** Optimizer ch
216bd 6f 6f 73 65 20 74 61 62 6c 65 20 25 64 20 66 6f  oose table %d fo
216be 72 20 6c 6f 6f 70 20 25 64 5c 6e 22 2c 20 62 65  r loop %d\n", be
216bf 73 74 4a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  stJ,.           
216c0 70 4c 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61  pLevel-pWInfo->a
216c1 29 29 3b 0a 20 20 20 20 69 66 28 20 28 62 65 73  ));.    if( (bes
216c2 74 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f  tFlags & WHERE_O
216c3 52 44 45 52 42 59 29 21 3d 30 20 29 7b 0a 20 20  RDERBY)!=0 ){.  
216c4 20 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20 3d      *ppOrderBy =
216c5 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 6e   0;.    }.    an
216c6 64 46 6c 61 67 73 20 26 3d 20 62 65 73 74 46 6c  dFlags &= bestFl
216c7 61 67 73 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  ags;.    pLevel-
216c8 3e 66 6c 61 67 73 20 3d 20 62 65 73 74 46 6c 61  >flags = bestFla
216c9 67 73 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  gs;.    pLevel->
216ca 70 49 64 78 20 3d 20 70 42 65 73 74 3b 0a 20 20  pIdx = pBest;.  
216cb 20 20 70 4c 65 76 65 6c 2d 3e 6e 45 71 20 3d 20    pLevel->nEq = 
216cc 62 65 73 74 4e 45 71 3b 0a 20 20 20 20 70 4c 65  bestNEq;.    pLe
216cd 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 20 3d 20 30  vel->aInLoop = 0
216ce 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6e 49  ;.    pLevel->nI
216cf 6e 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70  n = 0;.    if( p
216d0 42 65 73 74 20 29 7b 0a 20 20 20 20 20 20 70 4c  Best ){.      pL
216d1 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20  evel->iIdxCur = 
216d2 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
216d3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
216d4 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
216d5 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20   = -1;.    }.   
216d6 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65   notReady &= ~ge
216d7 74 4d 61 73 6b 28 26 6d 61 73 6b 53 65 74 2c 20  tMask(&maskSet, 
216d8 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
216d9 4a 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  J].iCursor);.   
216da 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 20 3d   pLevel->iFrom =
216db 20 62 65 73 74 4a 3b 0a 20 20 7d 0a 20 20 57 48   bestJ;.  }.  WH
216dc 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f  ERETRACE(("*** O
216dd 70 74 69 6d 69 7a 65 72 20 46 69 6e 69 73 68 65  ptimizer Finishe
216de 64 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 0a 20 20 2f  d ***\n"));..  /
216df 2a 20 49 66 20 74 68 65 20 74 6f 74 61 6c 20 71  * If the total q
216e0 75 65 72 79 20 6f 6e 6c 79 20 73 65 6c 65 63 74  uery only select
216e1 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20  s a single row, 
216e2 74 68 65 6e 20 74 68 65 20 4f 52 44 45 52 20 42  then the ORDER B
216e3 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65 20 69 73  Y.  ** clause is
216e4 20 69 72 72 65 6c 65 76 61 6e 74 2e 0a 20 20 2a   irrelevant..  *
216e5 2f 0a 20 20 69 66 28 20 28 61 6e 64 46 6c 61 67  /.  if( (andFlag
216e6 73 20 26 20 57 48 45 52 45 5f 55 4e 49 51 55 45  s & WHERE_UNIQUE
216e7 29 21 3d 30 20 26 26 20 70 70 4f 72 64 65 72 42  )!=0 && ppOrderB
216e8 79 20 29 7b 0a 20 20 20 20 2a 70 70 4f 72 64 65  y ){.    *ppOrde
216e9 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  rBy = 0;.  }..  
216ea 2f 2a 20 4f 70 65 6e 20 61 6c 6c 20 74 61 62 6c  /* Open all tabl
216eb 65 73 20 69 6e 20 74 68 65 20 70 54 61 62 4c 69  es in the pTabLi
216ec 73 74 20 61 6e 64 20 61 6e 79 20 69 6e 64 69 63  st and any indic
216ed 65 73 20 73 65 6c 65 63 74 65 64 20 66 6f 72 0a  es selected for.
216ee 20 20 2a 2a 20 73 65 61 72 63 68 69 6e 67 20 74    ** searching t
216ef 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a  hose tables..  *
216f0 2f 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56  /.  sqlite3CodeV
216f1 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72  erifySchema(pPar
216f2 73 65 2c 20 2d 31 29 3b 20 2f 2a 20 49 6e 73 65  se, -1); /* Inse
216f3 72 74 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 65  rt the cookie ve
216f4 72 69 66 69 65 72 20 47 6f 74 6f 20 2a 2f 0a 20  rifier Goto */. 
216f5 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c   for(i=0, pLevel
216f6 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54  =pWInfo->a; i<pT
216f7 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
216f8 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20  +, pLevel++){.  
216f9 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
216fa 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 6f     /* Table to o
216fb 70 65 6e 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78  pen */.    Index
216fc 20 2a 70 49 78 3b 20 20 20 20 20 20 2f 2a 20 49   *pIx;      /* I
216fd 6e 64 65 78 20 75 73 65 64 20 74 6f 20 61 63 63  ndex used to acc
216fe 65 73 73 20 70 54 61 62 20 28 69 66 20 61 6e 79  ess pTab (if any
216ff 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62  ) */.    int iDb
21700 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  ;         /* Ind
21701 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 63  ex of database c
21702 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 2f  ontaining table/
21703 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74  index */.    int
21704 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65   iIdxCur = pLeve
21705 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 0a 23 69 66  l->iIdxCur;..#if
21706 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
21707 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 69 66 28  _EXPLAIN.    if(
21708 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
21709 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 63 68 61  ==2 ){.      cha
2170a 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20 73  r *zMsg;.      s
2170b 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2170c 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61  em *pItem = &pTa
2170d 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d  bList->a[pLevel-
2170e 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 20 20 7a  >iFrom];.      z
2170f 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Msg = sqlite3MPr
21710 69 6e 74 66 28 64 62 2c 20 22 54 41 42 4c 45 20  intf(db, "TABLE 
21711 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  %s", pItem->zNam
21712 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  e);.      if( pI
21713 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20  tem->zAlias ){. 
21714 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
21715 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
21716 20 22 25 7a 20 41 53 20 25 73 22 2c 20 7a 4d 73   "%z AS %s", zMs
21717 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73  g, pItem->zAlias
21718 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21719 20 69 66 28 20 28 70 49 78 20 3d 20 70 4c 65 76   if( (pIx = pLev
2171a 65 6c 2d 3e 70 49 64 78 29 21 3d 30 20 29 7b 0a  el->pIdx)!=0 ){.
2171b 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
2171c 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2171d 2c 20 22 25 7a 20 57 49 54 48 20 49 4e 44 45 58  , "%z WITH INDEX
2171e 20 25 73 22 2c 20 7a 4d 73 67 2c 20 70 49 78 2d   %s", zMsg, pIx-
2171f 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d  >zName);.      }
21720 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d  else if( pLevel-
21721 3e 66 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f  >flags & (WHERE_
21722 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52  ROWID_EQ|WHERE_R
21723 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20  OWID_RANGE) ){. 
21724 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
21725 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
21726 20 22 25 7a 20 55 53 49 4e 47 20 50 52 49 4d 41   "%z USING PRIMA
21727 52 59 20 4b 45 59 22 2c 20 7a 4d 73 67 29 3b 0a  RY KEY", zMsg);.
21728 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20        }.#ifndef 
21729 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2172a 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 65  UALTABLE.      e
2172b 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  lse if( pLevel->
2172c 70 42 65 73 74 49 64 78 20 29 7b 0a 20 20 20 20  pBestIdx ){.    
2172d 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65      sqlite3_inde
2172e 78 5f 69 6e 66 6f 20 2a 70 42 65 73 74 49 64 78  x_info *pBestIdx
2172f 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74   = pLevel->pBest
21730 49 64 78 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73  Idx;.        zMs
21731 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  g = sqlite3MPrin
21732 74 66 28 64 62 2c 20 22 25 7a 20 56 49 52 54 55  tf(db, "%z VIRTU
21733 41 4c 20 54 41 42 4c 45 20 49 4e 44 45 58 20 25  AL TABLE INDEX %
21734 64 3a 25 73 22 2c 20 7a 4d 73 67 2c 0a 20 20 20  d:%s", zMsg,.   
21735 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21736 20 70 42 65 73 74 49 64 78 2d 3e 69 64 78 4e 75   pBestIdx->idxNu
21737 6d 2c 20 70 42 65 73 74 49 64 78 2d 3e 69 64 78  m, pBestIdx->idx
21738 53 74 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  Str);.      }.#e
21739 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 70  ndif.      if( p
2173a 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57  Level->flags & W
2173b 48 45 52 45 5f 4f 52 44 45 52 42 59 20 29 7b 0a  HERE_ORDERBY ){.
2173c 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
2173d 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2173e 2c 20 22 25 7a 20 4f 52 44 45 52 20 42 59 22 2c  , "%z ORDER BY",
2173f 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a   zMsg);.      }.
21740 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21741 65 4f 70 33 28 76 2c 20 4f 50 5f 45 78 70 6c 61  eOp3(v, OP_Expla
21742 69 6e 2c 20 69 2c 20 70 4c 65 76 65 6c 2d 3e 69  in, i, pLevel->i
21743 46 72 6f 6d 2c 20 7a 4d 73 67 2c 20 50 33 5f 44  From, zMsg, P3_D
21744 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a 23  YNAMIC);.    }.#
21745 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
21746 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a  OMIT_EXPLAIN */.
21747 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26      pTabItem = &
21748 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76  pTabList->a[pLev
21749 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20  el->iFrom];.    
2174a 70 54 61 62 20 3d 20 70 54 61 62 49 74 65 6d 2d  pTab = pTabItem-
2174b 3e 70 54 61 62 3b 0a 20 20 20 20 69 44 62 20 3d  >pTab;.    iDb =
2174c 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2174d 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
2174e 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2174f 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e  ;.    if( pTab->
21750 69 73 45 70 68 65 6d 20 7c 7c 20 70 54 61 62 2d  isEphem || pTab-
21751 3e 70 53 65 6c 65 63 74 20 29 20 63 6f 6e 74 69  >pSelect ) conti
21752 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  nue;.#ifndef SQL
21753 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
21754 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 70 4c  TABLE.    if( pL
21755 65 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 20 29  evel->pBestIdx )
21756 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 75 72  {.      int iCur
21757 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75   = pTabItem->iCu
21758 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  rsor;.      sqli
21759 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
2175a 5f 56 4f 70 65 6e 2c 20 69 43 75 72 2c 20 30 2c  _VOpen, iCur, 0,
2175b 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54   (const char*)pT
2175c 61 62 2d 3e 70 56 74 61 62 2c 20 50 33 5f 56 54  ab->pVtab, P3_VT
2175d 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23  AB);.    }else.#
2175e 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 28 70  endif.    if( (p
2175f 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57  Level->flags & W
21760 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d  HERE_IDX_ONLY)==
21761 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
21762 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
21763 73 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  se, pTabItem->iC
21764 75 72 73 6f 72 2c 20 69 44 62 2c 20 70 54 61 62  ursor, iDb, pTab
21765 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a  , OP_OpenRead);.
21766 20 20 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e        if( pTab->
21767 6e 43 6f 6c 3c 28 73 69 7a 65 6f 66 28 42 69 74  nCol<(sizeof(Bit
21768 6d 61 73 6b 29 2a 38 29 20 29 7b 0a 20 20 20 20  mask)*8) ){.    
21769 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20 3d 20      Bitmask b = 
2176a 70 54 61 62 49 74 65 6d 2d 3e 63 6f 6c 55 73 65  pTabItem->colUse
2176b 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  d;.        int n
2176c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f   = 0;.        fo
2176d 72 28 3b 20 62 3b 20 62 3d 62 3e 3e 31 2c 20 6e  r(; b; b=b>>1, n
2176e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 73 71  ++){}.        sq
2176f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
21770 32 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65  2(v, sqlite3Vdbe
21771 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d 31  CurrentAddr(v)-1
21772 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73  , n);.        as
21773 73 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d 3e 6e  sert( n<=pTab->n
21774 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Col );.      }. 
21775 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21776 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
21777 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
21778 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61  ab->tnum, 0, pTa
21779 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  b->zName);.    }
2177a 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 54 61  .    pLevel->iTa
2177b 62 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d  bCur = pTabItem-
2177c 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66  >iCursor;.    if
2177d 28 20 28 70 49 78 20 3d 20 70 4c 65 76 65 6c 2d  ( (pIx = pLevel-
2177e 3e 70 49 64 78 29 21 3d 30 20 29 7b 0a 20 20 20  >pIdx)!=0 ){.   
2177f 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
21780 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
21781 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
21782 49 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Ix);.      asser
21783 74 28 20 70 49 78 2d 3e 70 53 63 68 65 6d 61 3d  t( pIx->pSchema=
21784 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
21785 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21786 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
21787 6e 74 65 67 65 72 2c 20 69 44 62 2c 20 30 29 3b  nteger, iDb, 0);
21788 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
21789 6e 74 28 28 76 2c 20 22 23 20 25 73 22 2c 20 70  nt((v, "# %s", p
2178a 49 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  Ix->zName));.   
2178b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70     sqlite3VdbeOp
2178c 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  3(v, OP_OpenRead
2178d 2c 20 69 49 64 78 43 75 72 2c 20 70 49 78 2d 3e  , iIdxCur, pIx->
2178e 74 6e 75 6d 2c 0a 20 20 20 20 20 20 20 20 20 20  tnum,.          
2178f 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
21790 2a 29 70 4b 65 79 2c 20 50 33 5f 4b 45 59 49 4e  *)pKey, P3_KEYIN
21791 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
21792 20 7d 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76   }.    if( (pLev
21793 65 6c 2d 3e 66 6c 61 67 73 20 26 20 28 57 48 45  el->flags & (WHE
21794 52 45 5f 49 44 58 5f 4f 4e 4c 59 7c 57 48 45 52  RE_IDX_ONLY|WHER
21795 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 29 29  E_COLUMN_RANGE))
21796 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  !=0 ){.      /* 
21797 4f 6e 6c 79 20 63 61 6c 6c 20 4f 50 5f 53 65 74  Only call OP_Set
21798 4e 75 6d 43 6f 6c 75 6d 6e 73 20 6f 6e 20 74 68  NumColumns on th
21799 65 20 69 6e 64 65 78 20 69 66 20 77 65 20 6d 69  e index if we mi
2179a 67 68 74 20 6c 61 74 65 72 20 75 73 65 0a 20 20  ght later use.  
2179b 20 20 20 20 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e      ** OP_Column
2179c 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 2a   on the index. *
2179d 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
2179e 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53  dbeAddOp(v, OP_S
2179f 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 69 49  etNumColumns, iI
217a0 64 78 43 75 72 2c 20 70 49 78 2d 3e 6e 43 6f 6c  dxCur, pIx->nCol
217a1 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20  umn+1);.    }.  
217a2 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
217a3 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
217a4 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 57  , iDb);.  }.  pW
217a5 49 6e 66 6f 2d 3e 69 54 6f 70 20 3d 20 73 71 6c  Info->iTop = sql
217a6 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
217a7 64 64 72 28 76 29 3b 0a 0a 20 20 2f 2a 20 47 65  ddr(v);..  /* Ge
217a8 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64 65 20  nerate the code 
217a9 74 6f 20 64 6f 20 74 68 65 20 73 65 61 72 63 68  to do the search
217aa 2e 20 20 45 61 63 68 20 69 74 65 72 61 74 69 6f  .  Each iteratio
217ab 6e 20 6f 66 20 74 68 65 20 66 6f 72 0a 20 20 2a  n of the for.  *
217ac 2a 20 6c 6f 6f 70 20 62 65 6c 6f 77 20 67 65 6e  * loop below gen
217ad 65 72 61 74 65 73 20 63 6f 64 65 20 66 6f 72 20  erates code for 
217ae 61 20 73 69 6e 67 6c 65 20 6e 65 73 74 65 64 20  a single nested 
217af 6c 6f 6f 70 20 6f 66 20 74 68 65 20 56 4d 0a 20  loop of the VM. 
217b0 20 2a 2a 20 70 72 6f 67 72 61 6d 2e 0a 20 20 2a   ** program..  *
217b1 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e  /.  notReady = ~
217b2 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 66 6f  (Bitmask)0;.  fo
217b3 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57  r(i=0, pLevel=pW
217b4 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c  Info->a; i<pTabL
217b5 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
217b6 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 69  pLevel++){.    i
217b7 6e 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20 69 43  nt j;.    int iC
217b8 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69  ur = pTabItem->i
217b9 43 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20  Cursor;  /* The 
217ba 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20  VDBE cursor for 
217bb 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  the table */.   
217bc 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
217bd 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78      /* The index
217be 20 77 65 20 77 69 6c 6c 20 62 65 20 75 73 69 6e   we will be usin
217bf 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 78 74  g */.    int nxt
217c0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  ;           /* W
217c1 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74 6f 20  here to jump to 
217c2 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68  continue with th
217c3 65 20 6e 65 78 74 20 49 4e 20 63 61 73 65 20 2a  e next IN case *
217c4 2f 0a 20 20 20 20 69 6e 74 20 69 49 64 78 43 75  /.    int iIdxCu
217c5 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  r;       /* The 
217c6 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20  VDBE cursor for 
217c7 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20  the index */.   
217c8 20 69 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b 20   int omitTable; 
217c9 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
217ca 65 20 75 73 65 20 74 68 65 20 69 6e 64 65 78 20  e use the index 
217cb 6f 6e 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20  only */.    int 
217cc 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f  bRev;          /
217cd 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e 65 65  * True if we nee
217ce 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65 76  d to scan in rev
217cf 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 0a 20  erse order */.. 
217d0 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70     pTabItem = &p
217d1 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65  TabList->a[pLeve
217d2 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 69  l->iFrom];.    i
217d3 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e  Cur = pTabItem->
217d4 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 49 64  iCursor;.    pId
217d5 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78  x = pLevel->pIdx
217d6 3b 0a 20 20 20 20 69 49 64 78 43 75 72 20 3d 20  ;.    iIdxCur = 
217d7 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b  pLevel->iIdxCur;
217d8 0a 20 20 20 20 62 52 65 76 20 3d 20 28 70 4c 65  .    bRev = (pLe
217d9 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45  vel->flags & WHE
217da 52 45 5f 52 45 56 45 52 53 45 29 21 3d 30 3b 0a  RE_REVERSE)!=0;.
217db 20 20 20 20 6f 6d 69 74 54 61 62 6c 65 20 3d 20      omitTable = 
217dc 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26  (pLevel->flags &
217dd 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29   WHERE_IDX_ONLY)
217de 21 3d 30 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65  !=0;..    /* Cre
217df 61 74 65 20 6c 61 62 65 6c 73 20 66 6f 72 20 74  ate labels for t
217e0 68 65 20 22 62 72 65 61 6b 22 20 61 6e 64 20 22  he "break" and "
217e1 63 6f 6e 74 69 6e 75 65 22 20 69 6e 73 74 72 75  continue" instru
217e2 63 74 69 6f 6e 73 0a 20 20 20 20 2a 2a 20 66 6f  ctions.    ** fo
217e3 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  r the current lo
217e4 6f 70 2e 20 20 4a 75 6d 70 20 74 6f 20 62 72 6b  op.  Jump to brk
217e5 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
217e6 20 61 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2a 20   a loop..    ** 
217e7 4a 75 6d 70 20 74 6f 20 63 6f 6e 74 20 74 6f 20  Jump to cont to 
217e8 67 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  go immediately t
217e9 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65 72 61  o the next itera
217ea 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20 20  tion of the.    
217eb 2a 2a 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2a 0a  ** loop..    **.
217ec 20 20 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 72      ** When ther
217ed 65 20 69 73 20 61 6e 20 49 4e 20 6f 70 65 72 61  e is an IN opera
217ee 74 6f 72 2c 20 77 65 20 61 6c 73 6f 20 68 61 76  tor, we also hav
217ef 65 20 61 20 22 6e 78 74 22 20 6c 61 62 65 6c 20  e a "nxt" label 
217f0 74 68 61 74 0a 20 20 20 20 2a 2a 20 6d 65 61 6e  that.    ** mean
217f1 73 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69  s to continue wi
217f2 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20 76  th the next IN v
217f3 61 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  alue combination
217f4 2e 20 20 57 68 65 6e 0a 20 20 20 20 2a 2a 20 74  .  When.    ** t
217f5 68 65 72 65 20 61 72 65 20 6e 6f 20 49 4e 20 6f  here are no IN o
217f6 70 65 72 61 74 6f 72 73 20 69 6e 20 74 68 65 20  perators in the 
217f7 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65  constraints, the
217f8 20 22 6e 78 74 22 20 6c 61 62 65 6c 0a 20 20 20   "nxt" label.   
217f9 20 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20   ** is the same 
217fa 61 73 20 22 62 72 6b 22 2e 0a 20 20 20 20 2a 2f  as "brk"..    */
217fb 0a 20 20 20 20 62 72 6b 20 3d 20 70 4c 65 76 65  .    brk = pLeve
217fc 6c 2d 3e 62 72 6b 20 3d 20 70 4c 65 76 65 6c 2d  l->brk = pLevel-
217fd 3e 6e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  >nxt = sqlite3Vd
217fe 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
217ff 20 20 20 20 63 6f 6e 74 20 3d 20 70 4c 65 76 65      cont = pLeve
21800 6c 2d 3e 63 6f 6e 74 20 3d 20 73 71 6c 69 74 65  l->cont = sqlite
21801 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
21802 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
21803 69 73 20 69 73 20 74 68 65 20 72 69 67 68 74 20  is is the right 
21804 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20  table of a LEFT 
21805 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c 6f  OUTER JOIN, allo
21806 63 61 74 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20  cate and.    ** 
21807 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6d 65 6d  initialize a mem
21808 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20 72 65  ory cell that re
21809 63 6f 72 64 73 20 69 66 20 74 68 69 73 20 74 61  cords if this ta
2180a 62 6c 65 20 6d 61 74 63 68 65 73 20 61 6e 79 0a  ble matches any.
2180b 20 20 20 20 2a 2a 20 72 6f 77 20 6f 66 20 74 68      ** row of th
2180c 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20  e left table of 
2180d 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 20 20 2a 2f  the join..    */
2180e 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
2180f 3e 69 46 72 6f 6d 3e 30 20 26 26 20 28 70 54 61  >iFrom>0 && (pTa
21810 62 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74 79 70  bItem[0].jointyp
21811 65 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d 30 20  e & JT_LEFT)!=0 
21812 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70 50  ){.      if( !pP
21813 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 20 70 50 61  arse->nMem ) pPa
21814 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20  rse->nMem++;.   
21815 20 20 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74     pLevel->iLeft
21816 4a 6f 69 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e  Join = pParse->n
21817 4d 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c  Mem++;.      sql
21818 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
21819 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 30 2c 20 70   OP_MemInt, 0, p
2181a 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
2181b 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2181c 6d 65 6e 74 28 28 76 2c 20 22 23 20 69 6e 69 74  ment((v, "# init
2181d 20 4c 45 46 54 20 4a 4f 49 4e 20 6e 6f 2d 6d 61   LEFT JOIN no-ma
2181e 74 63 68 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  tch flag"));.   
2181f 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
21820 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
21821 41 42 4c 45 0a 20 20 20 20 69 66 28 20 70 4c 65  ABLE.    if( pLe
21822 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 20 29 7b  vel->pBestIdx ){
21823 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 30  .      /* Case 0
21824 3a 20 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  :  The table is 
21825 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2e  a virtual-table.
21826 20 20 55 73 65 20 74 68 65 20 56 46 69 6c 74 65    Use the VFilte
21827 72 20 61 6e 64 20 56 4e 65 78 74 0a 20 20 20 20  r and VNext.    
21828 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f    **          to
21829 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
2182a 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2182b 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 73 71   int j;.      sq
2182c 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2182d 20 2a 70 42 65 73 74 49 64 78 20 3d 20 70 4c 65   *pBestIdx = pLe
2182e 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 3b 0a 20  vel->pBestIdx;. 
2182f 20 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72       int nConstr
21830 61 69 6e 74 20 3d 20 70 42 65 73 74 49 64 78 2d  aint = pBestIdx-
21831 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20  >nConstraint;.  
21832 20 20 20 20 73 74 72 75 63 74 20 73 71 6c 69 74      struct sqlit
21833 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
21834 69 6e 74 5f 75 73 61 67 65 20 2a 61 55 73 61 67  int_usage *aUsag
21835 65 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20  e =.            
21836 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21837 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21838 20 20 20 20 20 20 70 42 65 73 74 49 64 78 2d 3e        pBestIdx->
21839 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
2183a 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 74  ;.      const st
2183b 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2183c 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 61  ex_constraint *a
2183d 43 6f 6e 73 74 72 61 69 6e 74 20 3d 0a 20 20 20  Constraint =.   
2183e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2183f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
21841 42 65 73 74 49 64 78 2d 3e 61 43 6f 6e 73 74 72  BestIdx->aConstr
21842 61 69 6e 74 3b 0a 0a 20 20 20 20 20 20 66 6f 72  aint;..      for
21843 28 6a 3d 31 3b 20 6a 3c 3d 6e 43 6f 6e 73 74 72  (j=1; j<=nConstr
21844 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  aint; j++){.    
21845 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20      int k;.     
21846 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 6e 43     for(k=0; k<nC
21847 6f 6e 73 74 72 61 69 6e 74 3b 20 6b 2b 2b 29 7b  onstraint; k++){
21848 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61  .          if( a
21849 55 73 61 67 65 5b 6b 5d 2e 61 72 67 76 49 6e 64  Usage[k].argvInd
2184a 65 78 3d 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20  ex==j ){.       
2184b 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 20 3d       int iTerm =
2184c 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6b 5d 2e   aConstraint[k].
2184d 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20 20  iTermOffset;.   
2184e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2184f 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
21850 20 77 63 2e 61 5b 69 54 65 72 6d 5d 2e 70 45 78   wc.a[iTerm].pEx
21851 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20  pr->pRight);.   
21852 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
21853 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
21854 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
21855 28 20 6b 3d 3d 6e 43 6f 6e 73 74 72 61 69 6e 74  ( k==nConstraint
21856 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
21857 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
21858 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
21859 6e 74 65 67 65 72 2c 20 6a 2d 31 2c 20 30 29 3b  nteger, j-1, 0);
2185a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2185b 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e  beAddOp(v, OP_In
2185c 74 65 67 65 72 2c 20 70 42 65 73 74 49 64 78 2d  teger, pBestIdx-
2185d 3e 69 64 78 4e 75 6d 2c 20 30 29 3b 0a 20 20 20  >idxNum, 0);.   
2185e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70     sqlite3VdbeOp
2185f 33 28 76 2c 20 4f 50 5f 56 46 69 6c 74 65 72 2c  3(v, OP_VFilter,
21860 20 69 43 75 72 2c 20 62 72 6b 2c 20 70 42 65 73   iCur, brk, pBes
21861 74 49 64 78 2d 3e 69 64 78 53 74 72 2c 0a 20 20  tIdx->idxStr,.  
21862 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21863 20 20 20 20 70 42 65 73 74 49 64 78 2d 3e 6e 65      pBestIdx->ne
21864 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3f  edToFreeIdxStr ?
21865 20 50 33 5f 4d 50 52 49 4e 54 46 20 3a 20 50 33   P3_MPRINTF : P3
21866 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
21867 70 42 65 73 74 49 64 78 2d 3e 6e 65 65 64 54 6f  pBestIdx->needTo
21868 46 72 65 65 49 64 78 53 74 72 20 3d 20 30 3b 0a  FreeIdxStr = 0;.
21869 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
2186a 3c 70 42 65 73 74 49 64 78 2d 3e 6e 43 6f 6e 73  <pBestIdx->nCons
2186b 74 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20  traint; j++){.  
2186c 20 20 20 20 20 20 69 66 28 20 61 55 73 61 67 65        if( aUsage
2186d 5b 6a 5d 2e 6f 6d 69 74 20 29 7b 0a 20 20 20 20  [j].omit ){.    
2186e 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 20        int iTerm 
2186f 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6a 5d  = aConstraint[j]
21870 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20  .iTermOffset;.  
21871 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54          disableT
21872 65 72 6d 28 70 4c 65 76 65 6c 2c 20 26 77 63 2e  erm(pLevel, &wc.
21873 61 5b 69 54 65 72 6d 5d 29 3b 0a 20 20 20 20 20  a[iTerm]);.     
21874 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
21875 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
21876 4f 50 5f 56 4e 65 78 74 3b 0a 20 20 20 20 20 20  OP_VNext;.      
21877 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75  pLevel->p1 = iCu
21878 72 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  r;.      pLevel-
21879 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62  >p2 = sqlite3Vdb
2187a 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
2187b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  .    }else.#endi
2187c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2187d 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
2187e 0a 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  ..    if( pLevel
2187f 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f  ->flags & WHERE_
21880 52 4f 57 49 44 5f 45 51 20 29 7b 0a 20 20 20 20  ROWID_EQ ){.    
21881 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 57 65    /* Case 1:  We
21882 20 63 61 6e 20 64 69 72 65 63 74 6c 79 20 72 65   can directly re
21883 66 65 72 65 6e 63 65 20 61 20 73 69 6e 67 6c 65  ference a single
21884 20 72 6f 77 20 75 73 69 6e 67 20 61 6e 0a 20 20   row using an.  
21885 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
21886 65 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72 69  equality compari
21887 73 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65 20  son against the 
21888 52 4f 57 49 44 20 66 69 65 6c 64 2e 20 20 4f 72  ROWID field.  Or
21889 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
2188a 20 20 20 77 65 20 72 65 66 65 72 65 6e 63 65 20     we reference 
2188b 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 75 73  multiple rows us
2188c 69 6e 67 20 61 20 22 72 6f 77 69 64 20 49 4e 20  ing a "rowid IN 
2188d 28 2e 2e 2e 29 22 0a 20 20 20 20 20 20 2a 2a 20  (...)".      ** 
2188e 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 72 75           constru
2188f 63 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ct..      */.   
21890 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54     pTerm = findT
21891 65 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20 2d  erm(&wc, iCur, -
21892 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f  1, notReady, WO_
21893 45 51 7c 57 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20  EQ|WO_IN, 0);.  
21894 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
21895 6d 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73  m!=0 );.      as
21896 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70 45 78  sert( pTerm->pEx
21897 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  pr!=0 );.      a
21898 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 6c 65  ssert( pTerm->le
21899 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29  ftCursor==iCur )
2189a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2189b 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  omitTable==0 );.
2189c 20 20 20 20 20 20 63 6f 64 65 45 71 75 61 6c 69        codeEquali
2189d 74 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70  tyTerm(pParse, p
2189e 54 65 72 6d 2c 20 70 4c 65 76 65 6c 29 3b 0a 20  Term, pLevel);. 
2189f 20 20 20 20 20 6e 78 74 20 3d 20 70 4c 65 76 65       nxt = pLeve
218a0 6c 2d 3e 6e 78 74 3b 0a 20 20 20 20 20 20 73 71  l->nxt;.      sq
218a1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
218a2 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
218a3 31 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20 20 73  1, nxt);.      s
218a4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
218a5 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
218a6 20 69 43 75 72 2c 20 6e 78 74 29 3b 0a 20 20 20   iCur, nxt);.   
218a7 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
218a8 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 20 20  v, "pk"));.     
218a9 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50   pLevel->op = OP
218aa 5f 4e 6f 6f 70 3b 0a 20 20 20 20 7d 65 6c 73 65  _Noop;.    }else
218ab 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61   if( pLevel->fla
218ac 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
218ad 5f 52 41 4e 47 45 20 29 7b 0a 20 20 20 20 20 20  _RANGE ){.      
218ae 2f 2a 20 43 61 73 65 20 32 3a 20 20 57 65 20 68  /* Case 2:  We h
218af 61 76 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74  ave an inequalit
218b0 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61  y comparison aga
218b1 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44 20 66  inst the ROWID f
218b2 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ield..      */. 
218b3 20 20 20 20 20 69 6e 74 20 74 65 73 74 4f 70 20       int testOp 
218b4 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20  = OP_Noop;.     
218b5 20 69 6e 74 20 73 74 61 72 74 3b 0a 20 20 20 20   int start;.    
218b6 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 53 74    WhereTerm *pSt
218b7 61 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20 20 20  art, *pEnd;..   
218b8 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54     assert( omitT
218b9 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  able==0 );.     
218ba 20 70 53 74 61 72 74 20 3d 20 66 69 6e 64 54 65   pStart = findTe
218bb 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20 2d 31  rm(&wc, iCur, -1
218bc 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47  , notReady, WO_G
218bd 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a 20 20 20  T|WO_GE, 0);.   
218be 20 20 20 70 45 6e 64 20 3d 20 66 69 6e 64 54 65     pEnd = findTe
218bf 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20 2d 31  rm(&wc, iCur, -1
218c0 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c  , notReady, WO_L
218c1 54 7c 57 4f 5f 4c 45 2c 20 30 29 3b 0a 20 20 20  T|WO_LE, 0);.   
218c2 20 20 20 69 66 28 20 62 52 65 76 20 29 7b 0a 20     if( bRev ){. 
218c3 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 70         pTerm = p
218c4 53 74 61 72 74 3b 0a 20 20 20 20 20 20 20 20 70  Start;.        p
218c5 53 74 61 72 74 20 3d 20 70 45 6e 64 3b 0a 20 20  Start = pEnd;.  
218c6 20 20 20 20 20 20 70 45 6e 64 20 3d 20 70 54 65        pEnd = pTe
218c7 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rm;.      }.    
218c8 20 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a    if( pStart ){.
218c9 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 58          Expr *pX
218ca 3b 0a 20 20 20 20 20 20 20 20 70 58 20 3d 20 70  ;.        pX = p
218cb 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20  Start->pExpr;.  
218cc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58        assert( pX
218cd 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61  !=0 );.        a
218ce 73 73 65 72 74 28 20 70 53 74 61 72 74 2d 3e 6c  ssert( pStart->l
218cf 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20  eftCursor==iCur 
218d0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
218d1 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
218d2 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 29 3b 0a  e, pX->pRight);.
218d3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
218d4 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46  dbeAddOp(v, OP_F
218d5 6f 72 63 65 49 6e 74 2c 20 70 58 2d 3e 6f 70 3d  orceInt, pX->op=
218d6 3d 54 4b 5f 4c 45 20 7c 7c 20 70 58 2d 3e 6f 70  =TK_LE || pX->op
218d7 3d 3d 54 4b 5f 47 54 2c 20 62 72 6b 29 3b 0a 20  ==TK_GT, brk);. 
218d8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
218d9 62 65 41 64 64 4f 70 28 76 2c 20 62 52 65 76 20  beAddOp(v, bRev 
218da 3f 20 4f 50 5f 4d 6f 76 65 4c 74 20 3a 20 4f 50  ? OP_MoveLt : OP
218db 5f 4d 6f 76 65 47 65 2c 20 69 43 75 72 2c 20 62  _MoveGe, iCur, b
218dc 72 6b 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62  rk);.        Vdb
218dd 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 70 6b  eComment((v, "pk
218de 22 29 29 3b 0a 20 20 20 20 20 20 20 20 64 69 73  "));.        dis
218df 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
218e0 20 70 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20   pStart);.      
218e1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
218e2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
218e3 76 2c 20 62 52 65 76 20 3f 20 4f 50 5f 4c 61 73  v, bRev ? OP_Las
218e4 74 20 3a 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  t : OP_Rewind, i
218e5 43 75 72 2c 20 62 72 6b 29 3b 0a 20 20 20 20 20  Cur, brk);.     
218e6 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 6e   }.      if( pEn
218e7 64 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70  d ){.        Exp
218e8 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 20 20 70  r *pX;.        p
218e9 58 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70 72 3b  X = pEnd->pExpr;
218ea 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
218eb 20 70 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20   pX!=0 );.      
218ec 20 20 61 73 73 65 72 74 28 20 70 45 6e 64 2d 3e    assert( pEnd->
218ed 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
218ee 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
218ef 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
218f0 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 29 3b  se, pX->pRight);
218f1 0a 20 20 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  .        pLevel-
218f2 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e  >iMem = pParse->
218f3 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nMem++;.        
218f4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
218f5 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 2c  (v, OP_MemStore,
218f6 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20 31   pLevel->iMem, 1
218f7 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
218f8 58 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 20  X->op==TK_LT || 
218f9 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 29 7b  pX->op==TK_GT ){
218fa 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74 4f  .          testO
218fb 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 4c 65  p = bRev ? OP_Le
218fc 20 3a 20 4f 50 5f 47 65 3b 0a 20 20 20 20 20 20   : OP_Ge;.      
218fd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
218fe 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65 76     testOp = bRev
218ff 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47 74   ? OP_Lt : OP_Gt
21900 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
21901 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28      disableTerm(
21902 70 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b 0a 20  pLevel, pEnd);. 
21903 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 74 61       }.      sta
21904 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rt = sqlite3Vdbe
21905 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
21906 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70        pLevel->op
21907 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72 65   = bRev ? OP_Pre
21908 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20  v : OP_Next;.   
21909 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20     pLevel->p1 = 
2190a 69 43 75 72 3b 0a 20 20 20 20 20 20 70 4c 65 76  iCur;.      pLev
2190b 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72 74 3b 0a  el->p2 = start;.
2190c 20 20 20 20 20 20 69 66 28 20 74 65 73 74 4f 70        if( testOp
2190d 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20  !=OP_Noop ){.   
2190e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2190f 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 6f 77 69  AddOp(v, OP_Rowi
21910 64 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20  d, iCur, 0);.   
21911 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21912 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c  AddOp(v, OP_MemL
21913 6f 61 64 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65  oad, pLevel->iMe
21914 6d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  m, 0);.        s
21915 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
21916 76 2c 20 74 65 73 74 4f 70 2c 20 53 51 4c 49 54  v, testOp, SQLIT
21917 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 7c 30 78  E_AFF_NUMERIC|0x
21918 31 30 30 2c 20 62 72 6b 29 3b 0a 20 20 20 20 20  100, brk);.     
21919 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
2191a 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26   pLevel->flags &
2191b 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41   WHERE_COLUMN_RA
2191c 4e 47 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  NGE ){.      /* 
2191d 43 61 73 65 20 33 3a 20 54 68 65 20 57 48 45 52  Case 3: The WHER
2191e 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 68  E clause term th
2191f 61 74 20 72 65 66 65 72 73 20 74 6f 20 74 68 65  at refers to the
21920 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 20 20   right-most.    
21921 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6c    **         col
21922 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
21923 20 69 73 20 61 6e 20 69 6e 65 71 75 61 6c 69 74   is an inequalit
21924 79 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  y.  For example,
21925 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   if.      **    
21926 20 20 20 20 20 74 68 65 20 69 6e 64 65 78 20 69       the index i
21927 73 20 6f 6e 20 28 78 2c 79 2c 7a 29 20 61 6e 64  s on (x,y,z) and
21928 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
21929 65 20 69 73 20 6f 66 20 74 68 65 0a 20 20 20 20  e is of the.    
2192a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 66 6f 72    **         for
2192b 6d 20 22 78 3d 35 20 41 4e 44 20 79 3c 31 30 22  m "x=5 AND y<10"
2192c 20 74 68 65 6e 20 74 68 69 73 20 63 61 73 65 20   then this case 
2192d 69 73 20 75 73 65 64 2e 20 20 4f 6e 6c 79 20 74  is used.  Only t
2192e 68 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  he.      **     
2192f 20 20 20 20 72 69 67 68 74 2d 6d 6f 73 74 20 63      right-most c
21930 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 61 6e 20  olumn can be an 
21931 69 6e 65 71 75 61 6c 69 74 79 20 2d 20 74 68 65  inequality - the
21932 20 72 65 73 74 20 6d 75 73 74 0a 20 20 20 20 20   rest must.     
21933 20 2a 2a 20 20 20 20 20 20 20 20 20 75 73 65 20   **         use 
21934 74 68 65 20 22 3d 3d 22 20 61 6e 64 20 22 49 4e  the "==" and "IN
21935 22 20 6f 70 65 72 61 74 6f 72 73 2e 0a 20 20 20  " operators..   
21936 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
21937 20 20 20 20 20 20 20 54 68 69 73 20 63 61 73 65         This case
21938 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 77 68   is also used wh
21939 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  en there are no 
2193a 57 48 45 52 45 20 63 6c 61 75 73 65 0a 20 20 20  WHERE clause.   
2193b 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6f     **         co
2193c 6e 73 74 72 61 69 6e 74 73 20 62 75 74 20 61 6e  nstraints but an
2193d 20 69 6e 64 65 78 20 69 73 20 73 65 6c 65 63 74   index is select
2193e 65 64 20 61 6e 79 77 61 79 2c 20 69 6e 20 6f 72  ed anyway, in or
2193f 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  der.      **    
21940 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20 74 68       to force th
21941 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72 20 74  e output order t
21942 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61 6e 20  o conform to an 
21943 4f 52 44 45 52 20 42 59 2e 0a 20 20 20 20 20 20  ORDER BY..      
21944 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 74 61  */.      int sta
21945 72 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 45  rt;.      int nE
21946 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 6e 45 71 3b  q = pLevel->nEq;
21947 0a 20 20 20 20 20 20 69 6e 74 20 74 6f 70 45 71  .      int topEq
21948 3d 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  =0;        /* Tr
21949 75 65 20 69 66 20 74 6f 70 20 6c 69 6d 69 74 20  ue if top limit 
2194a 75 73 65 73 20 3d 3d 2e 20 46 61 6c 73 65 20 69  uses ==. False i
2194b 73 20 73 74 72 69 63 74 6c 79 20 3c 20 2a 2f 0a  s strictly < */.
2194c 20 20 20 20 20 20 69 6e 74 20 62 74 6d 45 71 3d        int btmEq=
2194d 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  0;        /* Tru
2194e 65 20 69 66 20 62 74 6d 20 6c 69 6d 69 74 20 75  e if btm limit u
2194f 73 65 73 20 3d 3d 2e 20 46 61 6c 73 65 20 69 66  ses ==. False if
21950 20 73 74 72 69 63 74 6c 79 20 3e 20 2a 2f 0a 20   strictly > */. 
21951 20 20 20 20 20 69 6e 74 20 74 6f 70 4f 70 2c 20       int topOp, 
21952 62 74 6d 4f 70 3b 20 20 20 2f 2a 20 4f 70 65 72  btmOp;   /* Oper
21953 61 74 6f 72 73 20 66 6f 72 20 74 68 65 20 74 6f  ators for the to
21954 70 20 61 6e 64 20 62 6f 74 74 6f 6d 20 73 65 61  p and bottom sea
21955 72 63 68 20 62 6f 75 6e 64 73 20 2a 2f 0a 20 20  rch bounds */.  
21956 20 20 20 20 69 6e 74 20 74 65 73 74 4f 70 3b 0a      int testOp;.
21957 20 20 20 20 20 20 69 6e 74 20 74 6f 70 4c 69 6d        int topLim
21958 69 74 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 66 6c  it = (pLevel->fl
21959 61 67 73 20 26 20 57 48 45 52 45 5f 54 4f 50 5f  ags & WHERE_TOP_
2195a 4c 49 4d 49 54 29 21 3d 30 3b 0a 20 20 20 20 20  LIMIT)!=0;.     
2195b 20 69 6e 74 20 62 74 6d 4c 69 6d 69 74 20 3d 20   int btmLimit = 
2195c 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26  (pLevel->flags &
2195d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54   WHERE_BTM_LIMIT
2195e 29 21 3d 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  )!=0;..      /* 
2195f 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
21960 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20 63 6f   evaluate all co
21961 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 75  nstraint terms u
21962 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e 0a 20 20  sing == or IN.  
21963 20 20 20 20 2a 2a 20 61 6e 64 20 6c 65 76 65 6c      ** and level
21964 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74   the values of t
21965 68 6f 73 65 20 74 65 72 6d 73 20 6f 6e 20 74 68  hose terms on th
21966 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 20 20 2a  e stack..      *
21967 2f 0a 20 20 20 20 20 20 63 6f 64 65 41 6c 6c 45  /.      codeAllE
21968 71 75 61 6c 69 74 79 54 65 72 6d 73 28 70 50 61  qualityTerms(pPa
21969 72 73 65 2c 20 70 4c 65 76 65 6c 2c 20 26 77 63  rse, pLevel, &wc
2196a 2c 20 6e 6f 74 52 65 61 64 79 29 3b 0a 0a 20 20  , notReady);..  
2196b 20 20 20 20 2f 2a 20 44 75 70 6c 69 63 61 74 65      /* Duplicate
2196c 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 74 65   the equality te
2196d 72 6d 20 76 61 6c 75 65 73 20 62 65 63 61 75 73  rm values becaus
2196e 65 20 74 68 65 79 20 77 69 6c 6c 20 61 6c 6c 20  e they will all 
2196f 62 65 0a 20 20 20 20 20 20 2a 2a 20 75 73 65 64  be.      ** used
21970 20 74 77 69 63 65 3a 20 6f 6e 63 65 20 74 6f 20   twice: once to 
21971 6d 61 6b 65 20 74 68 65 20 74 65 72 6d 69 6e 61  make the termina
21972 74 69 6f 6e 20 6b 65 79 20 61 6e 64 20 6f 6e 63  tion key and onc
21973 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 20 20  e to make the.  
21974 20 20 20 20 2a 2a 20 73 74 61 72 74 20 6b 65 79      ** start key
21975 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
21976 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 45 71 3b   for(j=0; j<nEq;
21977 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 73   j++){.        s
21978 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
21979 76 2c 20 4f 50 5f 44 75 70 2c 20 6e 45 71 2d 31  v, OP_Dup, nEq-1
2197a 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  , 0);.      }.. 
2197b 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f       /* Figure o
2197c 75 74 20 77 68 61 74 20 63 6f 6d 70 61 72 69 73  ut what comparis
2197d 6f 6e 20 6f 70 65 72 61 74 6f 72 73 20 74 6f 20  on operators to 
2197e 75 73 65 20 66 6f 72 20 74 6f 70 20 61 6e 64 20  use for top and 
2197f 62 6f 74 74 6f 6d 20 0a 20 20 20 20 20 20 2a 2a  bottom .      **
21980 20 73 65 61 72 63 68 20 62 6f 75 6e 64 73 2e 20   search bounds. 
21981 46 6f 72 20 61 6e 20 61 73 63 65 6e 64 69 6e 67  For an ascending
21982 20 69 6e 64 65 78 2c 20 74 68 65 20 62 6f 74 74   index, the bott
21983 6f 6d 20 62 6f 75 6e 64 20 69 73 20 61 20 3e 20  om bound is a > 
21984 6f 72 20 3e 3d 0a 20 20 20 20 20 20 2a 2a 20 6f  or >=.      ** o
21985 70 65 72 61 74 6f 72 20 61 6e 64 20 74 68 65 20  perator and the 
21986 74 6f 70 20 62 6f 75 6e 64 20 69 73 20 61 20 3c  top bound is a <
21987 20 6f 72 20 3c 3d 20 6f 70 65 72 61 74 6f 72 2e   or <= operator.
21988 20 20 46 6f 72 20 61 20 64 65 73 63 65 6e 64 69    For a descendi
21989 6e 67 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64 65  ng.      ** inde
2198a 78 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20  x the operators 
2198b 61 72 65 20 72 65 76 65 72 73 65 64 2e 0a 20 20  are reversed..  
2198c 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
2198d 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65   pIdx->aSortOrde
2198e 72 5b 6e 45 71 5d 3d 3d 53 51 4c 49 54 45 5f 53  r[nEq]==SQLITE_S
2198f 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 20 20 20  O_ASC ){.       
21990 20 74 6f 70 4f 70 20 3d 20 57 4f 5f 4c 54 7c 57   topOp = WO_LT|W
21991 4f 5f 4c 45 3b 0a 20 20 20 20 20 20 20 20 62 74  O_LE;.        bt
21992 6d 4f 70 20 3d 20 57 4f 5f 47 54 7c 57 4f 5f 47  mOp = WO_GT|WO_G
21993 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  E;.      }else{.
21994 20 20 20 20 20 20 20 20 74 6f 70 4f 70 20 3d 20          topOp = 
21995 57 4f 5f 47 54 7c 57 4f 5f 47 45 3b 0a 20 20 20  WO_GT|WO_GE;.   
21996 20 20 20 20 20 62 74 6d 4f 70 20 3d 20 57 4f 5f       btmOp = WO_
21997 4c 54 7c 57 4f 5f 4c 45 3b 0a 20 20 20 20 20 20  LT|WO_LE;.      
21998 20 20 53 57 41 50 28 69 6e 74 2c 20 74 6f 70 4c    SWAP(int, topL
21999 69 6d 69 74 2c 20 62 74 6d 4c 69 6d 69 74 29 3b  imit, btmLimit);
2199a 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
2199b 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20  /* Generate the 
2199c 74 65 72 6d 69 6e 61 74 69 6f 6e 20 6b 65 79 2e  termination key.
2199d 20 20 54 68 69 73 20 69 73 20 74 68 65 20 6b 65    This is the ke
2199e 79 20 76 61 6c 75 65 20 74 68 61 74 0a 20 20 20  y value that.   
2199f 20 20 20 2a 2a 20 77 69 6c 6c 20 65 6e 64 20 74     ** will end t
219a0 68 65 20 73 65 61 72 63 68 2e 20 20 54 68 65 72  he search.  Ther
219a1 65 20 69 73 20 6e 6f 20 74 65 72 6d 69 6e 61 74  e is no terminat
219a2 69 6f 6e 20 6b 65 79 20 69 66 20 74 68 65 72 65  ion key if there
219a3 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f  .      ** are no
219a4 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d 73 20   equality terms 
219a5 61 6e 64 20 6e 6f 20 22 58 3c 2e 2e 2e 22 20 74  and no "X<..." t
219a6 65 72 6d 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  erm..      **.  
219a7 20 20 20 20 2a 2a 20 32 30 30 32 2d 44 65 63 2d      ** 2002-Dec-
219a8 30 34 3a 20 4f 6e 20 61 20 72 65 76 65 72 73 65  04: On a reverse
219a9 2d 6f 72 64 65 72 20 73 63 61 6e 2c 20 74 68 65  -order scan, the
219aa 20 73 6f 2d 63 61 6c 6c 65 64 20 22 74 65 72 6d   so-called "term
219ab 69 6e 61 74 69 6f 6e 22 0a 20 20 20 20 20 20 2a  ination".      *
219ac 2a 20 6b 65 79 20 63 6f 6d 70 75 74 65 64 20 68  * key computed h
219ad 65 72 65 20 72 65 61 6c 6c 79 20 65 6e 64 73 20  ere really ends 
219ae 75 70 20 62 65 69 6e 67 20 74 68 65 20 73 74 61  up being the sta
219af 72 74 20 6b 65 79 2e 0a 20 20 20 20 20 20 2a 2f  rt key..      */
219b0 0a 20 20 20 20 20 20 6e 78 74 20 3d 20 70 4c 65  .      nxt = pLe
219b1 76 65 6c 2d 3e 6e 78 74 3b 0a 20 20 20 20 20 20  vel->nxt;.      
219b2 69 66 28 20 74 6f 70 4c 69 6d 69 74 20 29 7b 0a  if( topLimit ){.
219b3 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 58          Expr *pX
219b4 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b 20  ;.        int k 
219b5 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
219b6 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  [j];.        pTe
219b7 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 26 77  rm = findTerm(&w
219b8 63 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52  c, iCur, k, notR
219b9 65 61 64 79 2c 20 74 6f 70 4f 70 2c 20 70 49 64  eady, topOp, pId
219ba 78 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  x);.        asse
219bb 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a  rt( pTerm!=0 );.
219bc 20 20 20 20 20 20 20 20 70 58 20 3d 20 70 54 65          pX = pTe
219bd 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  rm->pExpr;.     
219be 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72     assert( (pTer
219bf 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f  m->flags & TERM_
219c0 43 4f 44 45 44 29 3d 3d 30 20 29 3b 0a 20 20 20  CODED)==0 );.   
219c1 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
219c2 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 58 2d  Code(pParse, pX-
219c3 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  >pRight);.      
219c4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
219c5 4f 70 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  Op(v, OP_IsNull,
219c6 20 2d 28 6e 45 71 2a 32 2b 31 29 2c 20 6e 78 74   -(nEq*2+1), nxt
219c7 29 3b 0a 20 20 20 20 20 20 20 20 74 6f 70 45 71  );.        topEq
219c8 20 3d 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61   = pTerm->eOpera
219c9 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f  tor & (WO_LE|WO_
219ca 47 45 29 3b 0a 20 20 20 20 20 20 20 20 64 69 73  GE);.        dis
219cb 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
219cc 20 70 54 65 72 6d 29 3b 0a 20 20 20 20 20 20 20   pTerm);.       
219cd 20 74 65 73 74 4f 70 20 3d 20 4f 50 5f 49 64 78   testOp = OP_Idx
219ce 47 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  GE;.      }else{
219cf 0a 20 20 20 20 20 20 20 20 74 65 73 74 4f 70 20  .        testOp 
219d0 3d 20 6e 45 71 3e 30 20 3f 20 4f 50 5f 49 64 78  = nEq>0 ? OP_Idx
219d1 47 45 20 3a 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20  GE : OP_Noop;.  
219d2 20 20 20 20 20 20 74 6f 70 45 71 20 3d 20 31 3b        topEq = 1;
219d3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
219d4 66 28 20 74 65 73 74 4f 70 21 3d 4f 50 5f 4e 6f  f( testOp!=OP_No
219d5 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  op ){.        in
219d6 74 20 6e 43 6f 6c 20 3d 20 6e 45 71 20 2b 20 74  t nCol = nEq + t
219d7 6f 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 20  opLimit;.       
219d8 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 3d 20   pLevel->iMem = 
219d9 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a  pParse->nMem++;.
219da 20 20 20 20 20 20 20 20 62 75 69 6c 64 49 6e 64          buildInd
219db 65 78 50 72 6f 62 65 28 76 2c 20 6e 43 6f 6c 2c  exProbe(v, nCol,
219dc 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20   pIdx);.        
219dd 69 66 28 20 62 52 65 76 20 29 7b 0a 20 20 20 20  if( bRev ){.    
219de 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 74        int op = t
219df 6f 70 45 71 20 3f 20 4f 50 5f 4d 6f 76 65 4c 65  opEq ? OP_MoveLe
219e0 20 3a 20 4f 50 5f 4d 6f 76 65 4c 74 3b 0a 20 20   : OP_MoveLt;.  
219e1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
219e2 64 62 65 41 64 64 4f 70 28 76 2c 20 6f 70 2c 20  dbeAddOp(v, op, 
219e3 69 49 64 78 43 75 72 2c 20 6e 78 74 29 3b 0a 20  iIdxCur, nxt);. 
219e4 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
219e5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
219e6 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
219e7 65 6d 53 74 6f 72 65 2c 20 70 4c 65 76 65 6c 2d  emStore, pLevel-
219e8 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 20  >iMem, 1);.     
219e9 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
219ea 20 69 66 28 20 62 52 65 76 20 29 7b 0a 20 20 20   if( bRev ){.   
219eb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
219ec 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4c 61 73 74  AddOp(v, OP_Last
219ed 2c 20 69 49 64 78 43 75 72 2c 20 62 72 6b 29 3b  , iIdxCur, brk);
219ee 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
219ef 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20  /* Generate the 
219f0 73 74 61 72 74 20 6b 65 79 2e 20 20 54 68 69 73  start key.  This
219f1 20 69 73 20 74 68 65 20 6b 65 79 20 74 68 61 74   is the key that
219f2 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c 6f 77   defines the low
219f3 65 72 0a 20 20 20 20 20 20 2a 2a 20 62 6f 75 6e  er.      ** boun
219f4 64 20 6f 6e 20 74 68 65 20 73 65 61 72 63 68 2e  d on the search.
219f5 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 73 74    There is no st
219f6 61 72 74 20 6b 65 79 20 69 66 20 74 68 65 72 65  art key if there
219f7 20 61 72 65 20 6e 6f 0a 20 20 20 20 20 20 2a 2a   are no.      **
219f8 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d 73 20   equality terms 
219f9 61 6e 64 20 69 66 20 74 68 65 72 65 20 69 73 20  and if there is 
219fa 6e 6f 20 22 58 3e 2e 2e 2e 22 20 74 65 72 6d 2e  no "X>..." term.
219fb 20 20 49 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68    In.      ** th
219fc 61 74 20 63 61 73 65 2c 20 67 65 6e 65 72 61 74  at case, generat
219fd 65 20 61 20 22 52 65 77 69 6e 64 22 20 69 6e 73  e a "Rewind" ins
219fe 74 72 75 63 74 69 6f 6e 20 69 6e 20 70 6c 61 63  truction in plac
219ff 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 2a  e of the.      *
21a00 2a 20 73 74 61 72 74 20 6b 65 79 20 73 65 61 72  * start key sear
21a01 63 68 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ch..      **.   
21a02 20 20 20 2a 2a 20 32 30 30 32 2d 44 65 63 2d 30     ** 2002-Dec-0
21a03 34 3a 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f  4: In the case o
21a04 66 20 61 20 72 65 76 65 72 73 65 2d 6f 72 64 65  f a reverse-orde
21a05 72 20 73 65 61 72 63 68 2c 20 74 68 65 20 73 6f  r search, the so
21a06 2d 63 61 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a  -called.      **
21a07 20 22 73 74 61 72 74 22 20 6b 65 79 20 72 65 61   "start" key rea
21a08 6c 6c 79 20 65 6e 64 73 20 75 70 20 62 65 69 6e  lly ends up bein
21a09 67 20 75 73 65 64 20 61 73 20 74 68 65 20 74 65  g used as the te
21a0a 72 6d 69 6e 61 74 69 6f 6e 20 6b 65 79 2e 0a 20  rmination key.. 
21a0b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
21a0c 28 20 62 74 6d 4c 69 6d 69 74 20 29 7b 0a 20 20  ( btmLimit ){.  
21a0d 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b 0a        Expr *pX;.
21a0e 20 20 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20          int k = 
21a0f 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a  pIdx->aiColumn[j
21a10 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d  ];.        pTerm
21a11 20 3d 20 66 69 6e 64 54 65 72 6d 28 26 77 63 2c   = findTerm(&wc,
21a12 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61   iCur, k, notRea
21a13 64 79 2c 20 62 74 6d 4f 70 2c 20 70 49 64 78 29  dy, btmOp, pIdx)
21a14 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
21a15 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20 20  ( pTerm!=0 );.  
21a16 20 20 20 20 20 20 70 58 20 3d 20 70 54 65 72 6d        pX = pTerm
21a17 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ->pExpr;.       
21a18 20 61 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d   assert( (pTerm-
21a19 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f  >flags & TERM_CO
21a1a 44 45 44 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  DED)==0 );.     
21a1b 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
21a1c 64 65 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70  de(pParse, pX->p
21a1d 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20  Right);.        
21a1e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21a1f 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 2d  (v, OP_IsNull, -
21a20 28 6e 45 71 2b 31 29 2c 20 6e 78 74 29 3b 0a 20  (nEq+1), nxt);. 
21a21 20 20 20 20 20 20 20 62 74 6d 45 71 20 3d 20 70         btmEq = p
21a22 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
21a23 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b  & (WO_LE|WO_GE);
21a24 0a 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65  .        disable
21a25 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54 65  Term(pLevel, pTe
21a26 72 6d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  rm);.      }else
21a27 7b 0a 20 20 20 20 20 20 20 20 62 74 6d 45 71 20  {.        btmEq 
21a28 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
21a29 20 20 20 69 66 28 20 6e 45 71 3e 30 20 7c 7c 20     if( nEq>0 || 
21a2a 62 74 6d 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  btmLimit ){.    
21a2b 20 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 6e      int nCol = n
21a2c 45 71 20 2b 20 62 74 6d 4c 69 6d 69 74 3b 0a 20  Eq + btmLimit;. 
21a2d 20 20 20 20 20 20 20 62 75 69 6c 64 49 6e 64 65         buildInde
21a2e 78 50 72 6f 62 65 28 76 2c 20 6e 43 6f 6c 2c 20  xProbe(v, nCol, 
21a2f 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 69  pIdx);.        i
21a30 66 28 20 62 52 65 76 20 29 7b 0a 20 20 20 20 20  f( bRev ){.     
21a31 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 4d 65       pLevel->iMe
21a32 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  m = pParse->nMem
21a33 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  ++;.          sq
21a34 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
21a35 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 2c 20 70  , OP_MemStore, p
21a36 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20 31 29 3b  Level->iMem, 1);
21a37 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74 4f  .          testO
21a38 70 20 3d 20 4f 50 5f 49 64 78 4c 54 3b 0a 20 20  p = OP_IdxLT;.  
21a39 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
21a3a 20 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20         int op = 
21a3b 62 74 6d 45 71 20 3f 20 4f 50 5f 4d 6f 76 65 47  btmEq ? OP_MoveG
21a3c 65 20 3a 20 4f 50 5f 4d 6f 76 65 47 74 3b 0a 20  e : OP_MoveGt;. 
21a3d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21a3e 56 64 62 65 41 64 64 4f 70 28 76 2c 20 6f 70 2c  VdbeAddOp(v, op,
21a3f 20 69 49 64 78 43 75 72 2c 20 6e 78 74 29 3b 0a   iIdxCur, nxt);.
21a40 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21a41 7d 65 6c 73 65 20 69 66 28 20 62 52 65 76 20 29  }else if( bRev )
21a42 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 4f 70  {.        testOp
21a43 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20   = OP_Noop;.    
21a44 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
21a45 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21a46 70 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  p(v, OP_Rewind, 
21a47 69 49 64 78 43 75 72 2c 20 62 72 6b 29 3b 0a 20  iIdxCur, brk);. 
21a48 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
21a49 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 68   Generate the th
21a4a 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f  e top of the loo
21a4b 70 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  p.  If there is 
21a4c 61 20 74 65 72 6d 69 6e 61 74 69 6f 6e 0a 20 20  a termination.  
21a4d 20 20 20 20 2a 2a 20 6b 65 79 20 77 65 20 68 61      ** key we ha
21a4e 76 65 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74  ve to test for t
21a4f 68 61 74 20 6b 65 79 20 61 6e 64 20 61 62 6f 72  hat key and abor
21a50 74 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20  t at the top of 
21a51 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f  the.      ** loo
21a52 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  p..      */.    
21a53 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65    start = sqlite
21a54 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
21a55 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 74  (v);.      if( t
21a56 65 73 74 4f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29  estOp!=OP_Noop )
21a57 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21a58 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
21a59 5f 4d 65 6d 4c 6f 61 64 2c 20 70 4c 65 76 65 6c  _MemLoad, pLevel
21a5a 2d 3e 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20  ->iMem, 0);.    
21a5b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21a5c 64 64 4f 70 28 76 2c 20 74 65 73 74 4f 70 2c 20  ddOp(v, testOp, 
21a5d 69 49 64 78 43 75 72 2c 20 6e 78 74 29 3b 0a 20  iIdxCur, nxt);. 
21a5e 20 20 20 20 20 20 20 69 66 28 20 28 74 6f 70 45         if( (topE
21a5f 71 20 26 26 20 21 62 52 65 76 29 20 7c 7c 20 28  q && !bRev) || (
21a60 21 62 74 6d 45 71 20 26 26 20 62 52 65 76 29 20  !btmEq && bRev) 
21a61 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
21a62 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
21a63 28 76 2c 20 2d 31 2c 20 22 2b 22 2c 20 50 33 5f  (v, -1, "+", P3_
21a64 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
21a65 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
21a66 20 69 66 28 20 74 6f 70 4c 69 6d 69 74 20 7c 20   if( topLimit | 
21a67 62 74 6d 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  btmLimit ){.    
21a68 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21a69 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  ddOp(v, OP_Colum
21a6a 6e 2c 20 69 49 64 78 43 75 72 2c 20 6e 45 71 29  n, iIdxCur, nEq)
21a6b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21a6c 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
21a6d 5f 49 73 4e 75 6c 6c 2c 20 31 2c 20 63 6f 6e 74  _IsNull, 1, cont
21a6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21a6f 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65 20   if( !omitTable 
21a70 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
21a71 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
21a72 50 5f 49 64 78 52 6f 77 69 64 2c 20 69 49 64 78  P_IdxRowid, iIdx
21a73 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Cur, 0);.       
21a74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21a75 70 28 76 2c 20 4f 50 5f 4d 6f 76 65 47 65 2c 20  p(v, OP_MoveGe, 
21a76 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  iCur, 0);.      
21a77 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 63 6f  }..      /* Reco
21a78 72 64 20 74 68 65 20 69 6e 73 74 72 75 63 74 69  rd the instructi
21a79 6f 6e 20 75 73 65 64 20 74 6f 20 74 65 72 6d 69  on used to termi
21a7a 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 0a 20  nate the loop.. 
21a7b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 4c       */.      pL
21a7c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20  evel->op = bRev 
21a7d 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e  ? OP_Prev : OP_N
21a7e 65 78 74 3b 0a 20 20 20 20 20 20 70 4c 65 76 65  ext;.      pLeve
21a7f 6c 2d 3e 70 31 20 3d 20 69 49 64 78 43 75 72 3b  l->p1 = iIdxCur;
21a80 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  .      pLevel->p
21a81 32 20 3d 20 73 74 61 72 74 3b 0a 20 20 20 20 7d  2 = start;.    }
21a82 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d  else if( pLevel-
21a83 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43  >flags & WHERE_C
21a84 4f 4c 55 4d 4e 5f 45 51 20 29 7b 0a 20 20 20 20  OLUMN_EQ ){.    
21a85 20 20 2f 2a 20 43 61 73 65 20 34 3a 20 20 54 68    /* Case 4:  Th
21a86 65 72 65 20 69 73 20 61 6e 20 69 6e 64 65 78 20  ere is an index 
21a87 61 6e 64 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66  and all terms of
21a88 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
21a89 65 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20  e that.      ** 
21a8a 20 20 20 20 20 20 20 20 20 72 65 66 65 72 20 74           refer t
21a8b 6f 20 74 68 65 20 69 6e 64 65 78 20 75 73 69 6e  o the index usin
21a8c 67 20 74 68 65 20 22 3d 3d 22 20 6f 72 20 22 49  g the "==" or "I
21a8d 4e 22 20 6f 70 65 72 61 74 6f 72 73 2e 0a 20 20  N" operators..  
21a8e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74      */.      int
21a8f 20 73 74 61 72 74 3b 0a 20 20 20 20 20 20 69 6e   start;.      in
21a90 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e  t nEq = pLevel->
21a91 6e 45 71 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47  nEq;..      /* G
21a92 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
21a93 65 76 61 6c 75 61 74 65 20 61 6c 6c 20 63 6f 6e  evaluate all con
21a94 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 75 73  straint terms us
21a95 69 6e 67 20 3d 3d 20 6f 72 20 49 4e 0a 20 20 20  ing == or IN.   
21a96 20 20 20 2a 2a 20 61 6e 64 20 6c 65 61 76 65 20     ** and leave 
21a97 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68  the values of th
21a98 6f 73 65 20 74 65 72 6d 73 20 6f 6e 20 74 68 65  ose terms on the
21a99 20 73 74 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2f   stack..      */
21a9a 0a 20 20 20 20 20 20 63 6f 64 65 41 6c 6c 45 71  .      codeAllEq
21a9b 75 61 6c 69 74 79 54 65 72 6d 73 28 70 50 61 72  ualityTerms(pPar
21a9c 73 65 2c 20 70 4c 65 76 65 6c 2c 20 26 77 63 2c  se, pLevel, &wc,
21a9d 20 6e 6f 74 52 65 61 64 79 29 3b 0a 20 20 20 20   notReady);.    
21a9e 20 20 6e 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e    nxt = pLevel->
21a9f 6e 78 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47  nxt;..      /* G
21aa0 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67 6c 65  enerate a single
21aa1 20 6b 65 79 20 74 68 61 74 20 77 69 6c 6c 20 62   key that will b
21aa2 65 20 75 73 65 64 20 74 6f 20 62 6f 74 68 20 73  e used to both s
21aa3 74 61 72 74 20 61 6e 64 20 74 65 72 6d 69 6e 61  tart and termina
21aa4 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  te.      ** the 
21aa5 73 65 61 72 63 68 0a 20 20 20 20 20 20 2a 2f 0a  search.      */.
21aa6 20 20 20 20 20 20 62 75 69 6c 64 49 6e 64 65 78        buildIndex
21aa7 50 72 6f 62 65 28 76 2c 20 6e 45 71 2c 20 70 49  Probe(v, nEq, pI
21aa8 64 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  dx);.      sqlit
21aa9 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
21aaa 50 5f 4d 65 6d 53 74 6f 72 65 2c 20 70 4c 65 76  P_MemStore, pLev
21aab 65 6c 2d 3e 69 4d 65 6d 2c 20 30 29 3b 0a 0a 20  el->iMem, 0);.. 
21aac 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
21aad 20 63 6f 64 65 20 28 31 29 20 74 6f 20 6d 6f 76   code (1) to mov
21aae 65 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 6d  e to the first m
21aaf 61 74 63 68 69 6e 67 20 65 6c 65 6d 65 6e 74 20  atching element 
21ab0 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  of the table..  
21ab1 20 20 20 20 2a 2a 20 54 68 65 6e 20 67 65 6e 65      ** Then gene
21ab2 72 61 74 65 20 63 6f 64 65 20 28 32 29 20 74 68  rate code (2) th
21ab3 61 74 20 6a 75 6d 70 73 20 74 6f 20 22 6e 78 74  at jumps to "nxt
21ab4 22 20 61 66 74 65 72 20 74 68 65 20 63 75 72 73  " after the curs
21ab5 6f 72 20 69 73 20 70 61 73 74 0a 20 20 20 20 20  or is past.     
21ab6 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6d 61 74   ** the last mat
21ab7 63 68 69 6e 67 20 65 6c 65 6d 65 6e 74 20 6f 66  ching element of
21ab8 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65   the table.  The
21ab9 20 63 6f 64 65 20 28 31 29 20 69 73 20 65 78 65   code (1) is exe
21aba 63 75 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 6f  cuted.      ** o
21abb 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  nce to initializ
21abc 65 20 74 68 65 20 73 65 61 72 63 68 2c 20 74 68  e the search, th
21abd 65 20 63 6f 64 65 20 28 32 29 20 69 73 20 65 78  e code (2) is ex
21abe 65 63 75 74 65 64 20 62 65 66 6f 72 65 20 65 61  ecuted before ea
21abf 63 68 0a 20 20 20 20 20 20 2a 2a 20 69 74 65 72  ch.      ** iter
21ac0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61  ation of the sca
21ac1 6e 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  n to see if the 
21ac2 73 63 61 6e 20 68 61 73 20 66 69 6e 69 73 68 65  scan has finishe
21ac3 64 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  d. */.      if( 
21ac4 62 52 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20  bRev ){.        
21ac5 2f 2a 20 53 63 61 6e 20 69 6e 20 72 65 76 65 72  /* Scan in rever
21ac6 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20  se order */.    
21ac7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21ac8 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65 4c  ddOp(v, OP_MoveL
21ac9 65 2c 20 69 49 64 78 43 75 72 2c 20 6e 78 74 29  e, iIdxCur, nxt)
21aca 3b 0a 20 20 20 20 20 20 20 20 73 74 61 72 74 20  ;.        start 
21acb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
21acc 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64  Op(v, OP_MemLoad
21acd 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20  , pLevel->iMem, 
21ace 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
21acf 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
21ad0 4f 50 5f 49 64 78 4c 54 2c 20 69 49 64 78 43 75  OP_IdxLT, iIdxCu
21ad1 72 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20 20 20  r, nxt);.       
21ad2 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50   pLevel->op = OP
21ad3 5f 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 65 6c  _Prev;.      }el
21ad4 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 53  se{.        /* S
21ad5 63 61 6e 20 69 6e 20 74 68 65 20 66 6f 72 77 61  can in the forwa
21ad6 72 64 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20  rd order */.    
21ad7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21ad8 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65 47  ddOp(v, OP_MoveG
21ad9 65 2c 20 69 49 64 78 43 75 72 2c 20 6e 78 74 29  e, iIdxCur, nxt)
21ada 3b 0a 20 20 20 20 20 20 20 20 73 74 61 72 74 20  ;.        start 
21adb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
21adc 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64  Op(v, OP_MemLoad
21add 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20  , pLevel->iMem, 
21ade 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
21adf 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
21ae0 5f 49 64 78 47 45 2c 20 69 49 64 78 43 75 72 2c  _IdxGE, iIdxCur,
21ae1 20 6e 78 74 2c 20 22 2b 22 2c 20 50 33 5f 53 54   nxt, "+", P3_ST
21ae2 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 70  ATIC);.        p
21ae3 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e  Level->op = OP_N
21ae4 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ext;.      }.   
21ae5 20 20 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c     if( !omitTabl
21ae6 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
21ae7 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
21ae8 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c 20 69 49   OP_IdxRowid, iI
21ae9 64 78 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20  dxCur, 0);.     
21aea 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21aeb 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65 47 65  dOp(v, OP_MoveGe
21aec 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20  , iCur, 0);.    
21aed 20 20 7d 0a 20 20 20 20 20 20 70 4c 65 76 65 6c    }.      pLevel
21aee 2d 3e 70 31 20 3d 20 69 49 64 78 43 75 72 3b 0a  ->p1 = iIdxCur;.
21aef 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32        pLevel->p2
21af0 20 3d 20 73 74 61 72 74 3b 0a 20 20 20 20 7d 65   = start;.    }e
21af1 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61  lse{.      /* Ca
21af2 73 65 20 35 3a 20 20 54 68 65 72 65 20 69 73 20  se 5:  There is 
21af3 6e 6f 20 75 73 61 62 6c 65 20 69 6e 64 65 78 2e  no usable index.
21af4 20 20 57 65 20 6d 75 73 74 20 64 6f 20 61 20 63    We must do a c
21af5 6f 6d 70 6c 65 74 65 0a 20 20 20 20 20 20 2a 2a  omplete.      **
21af6 20 20 20 20 20 20 20 20 20 20 73 63 61 6e 20 6f            scan o
21af7 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 61 62  f the entire tab
21af8 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  le..      */.   
21af9 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54     assert( omitT
21afa 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  able==0 );.     
21afb 20 61 73 73 65 72 74 28 20 62 52 65 76 3d 3d 30   assert( bRev==0
21afc 20 29 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c   );.      pLevel
21afd 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 65 78 74 3b 0a  ->op = OP_Next;.
21afe 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31        pLevel->p1
21aff 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 20 20 70   = iCur;.      p
21b00 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 31 20 2b 20  Level->p2 = 1 + 
21b01 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21b02 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
21b03 43 75 72 2c 20 62 72 6b 29 3b 0a 20 20 20 20 7d  Cur, brk);.    }
21b04 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20 26 3d  .    notReady &=
21b05 20 7e 67 65 74 4d 61 73 6b 28 26 6d 61 73 6b 53   ~getMask(&maskS
21b06 65 74 2c 20 69 43 75 72 29 3b 0a 0a 20 20 20 20  et, iCur);..    
21b07 2f 2a 20 49 6e 73 65 72 74 20 63 6f 64 65 20 74  /* Insert code t
21b08 6f 20 74 65 73 74 20 65 76 65 72 79 20 73 75 62  o test every sub
21b09 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
21b0a 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c  can be completel
21b0b 79 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 75 74 65  y.    ** compute
21b0c 64 20 75 73 69 6e 67 20 74 68 65 20 63 75 72 72  d using the curr
21b0d 65 6e 74 20 73 65 74 20 6f 66 20 74 61 62 6c 65  ent set of table
21b0e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  s..    */.    fo
21b0f 72 28 70 54 65 72 6d 3d 77 63 2e 61 2c 20 6a 3d  r(pTerm=wc.a, j=
21b10 77 63 2e 6e 54 65 72 6d 3b 20 6a 3e 30 3b 20 6a  wc.nTerm; j>0; j
21b11 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20  --, pTerm++){.  
21b12 20 20 20 20 45 78 70 72 20 2a 70 45 3b 0a 20 20      Expr *pE;.  
21b13 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 66      if( pTerm->f
21b14 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52  lags & (TERM_VIR
21b15 54 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29  TUAL|TERM_CODED)
21b16 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
21b17 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 70     if( (pTerm->p
21b18 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65  rereqAll & notRe
21b19 61 64 79 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e  ady)!=0 ) contin
21b1a 75 65 3b 0a 20 20 20 20 20 20 70 45 20 3d 20 70  ue;.      pE = p
21b1b 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
21b1c 20 20 20 61 73 73 65 72 74 28 20 70 45 21 3d 30     assert( pE!=0
21b1d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c   );.      if( pL
21b1e 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20  evel->iLeftJoin 
21b1f 26 26 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  && !ExprHasPrope
21b20 72 74 79 28 70 45 2c 20 45 50 5f 46 72 6f 6d 4a  rty(pE, EP_FromJ
21b21 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20  oin) ){.        
21b22 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
21b23 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  }.      sqlite3E
21b24 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
21b25 65 2c 20 70 45 2c 20 63 6f 6e 74 2c 20 31 29 3b  e, pE, cont, 1);
21b26 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c  .      pTerm->fl
21b27 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45  ags |= TERM_CODE
21b28 44 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  D;.    }..    /*
21b29 20 46 6f 72 20 61 20 4c 45 46 54 20 4f 55 54 45   For a LEFT OUTE
21b2a 52 20 4a 4f 49 4e 2c 20 67 65 6e 65 72 61 74 65  R JOIN, generate
21b2b 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
21b2c 72 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20  record the fact 
21b2d 74 68 61 74 0a 20 20 20 20 2a 2a 20 61 74 20 6c  that.    ** at l
21b2e 65 61 73 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20  east one row of 
21b2f 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
21b30 68 61 73 20 6d 61 74 63 68 65 64 20 74 68 65 20  has matched the 
21b31 6c 65 66 74 20 74 61 62 6c 65 2e 20 20 0a 20 20  left table.  .  
21b32 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 65    */.    if( pLe
21b33 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29  vel->iLeftJoin )
21b34 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
21b35 74 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  top = sqlite3Vdb
21b36 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
21b37 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
21b38 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
21b39 6d 49 6e 74 2c 20 31 2c 20 70 4c 65 76 65 6c 2d  mInt, 1, pLevel-
21b3a 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20  >iLeftJoin);.   
21b3b 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
21b3c 76 2c 20 22 23 20 72 65 63 6f 72 64 20 4c 45 46  v, "# record LEF
21b3d 54 20 4a 4f 49 4e 20 68 69 74 22 29 29 3b 0a 20  T JOIN hit"));. 
21b3e 20 20 20 20 20 66 6f 72 28 70 54 65 72 6d 3d 77       for(pTerm=w
21b3f 63 2e 61 2c 20 6a 3d 30 3b 20 6a 3c 77 63 2e 6e  c.a, j=0; j<wc.n
21b40 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d  Term; j++, pTerm
21b41 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
21b42 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20   pTerm->flags & 
21b43 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45  (TERM_VIRTUAL|TE
21b44 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e 74  RM_CODED) ) cont
21b45 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66  inue;.        if
21b46 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  ( (pTerm->prereq
21b47 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21  All & notReady)!
21b48 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
21b49 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
21b4a 54 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b 0a 20  Term->pExpr );. 
21b4b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
21b4c 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
21b4d 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20  , pTerm->pExpr, 
21b4e 63 6f 6e 74 2c 20 31 29 3b 0a 20 20 20 20 20 20  cont, 1);.      
21b4f 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c    pTerm->flags |
21b50 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20  = TERM_CODED;.  
21b51 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
21b52 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
21b53 45 53 54 20 20 2f 2a 20 46 6f 72 20 74 65 73 74  EST  /* For test
21b54 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
21b55 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20  g use only */.  
21b56 2f 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65  /* Record in the
21b57 20 71 75 65 72 79 20 70 6c 61 6e 20 69 6e 66 6f   query plan info
21b58 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
21b59 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 0a  e current table.
21b5a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64    ** and the ind
21b5b 65 78 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  ex used to acces
21b5c 73 20 69 74 20 28 69 66 20 61 6e 79 29 2e 20 20  s it (if any).  
21b5d 49 66 20 74 68 65 20 74 61 62 6c 65 20 69 74 73  If the table its
21b5e 65 6c 66 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20  elf.  ** is not 
21b5f 75 73 65 64 2c 20 69 74 73 20 6e 61 6d 65 20 69  used, its name i
21b60 73 20 6a 75 73 74 20 27 7b 7d 27 2e 20 20 49 66  s just '{}'.  If
21b61 20 6e 6f 20 69 6e 64 65 78 20 69 73 20 75 73 65   no index is use
21b62 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78  d.  ** the index
21b63 20 69 73 20 6c 69 73 74 65 64 20 61 73 20 22 7b   is listed as "{
21b64 7d 22 2e 20 20 49 66 20 74 68 65 20 70 72 69 6d  }".  If the prim
21b65 61 72 79 20 6b 65 79 20 69 73 20 75 73 65 64 20  ary key is used 
21b66 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6e  the.  ** index n
21b67 61 6d 65 20 69 73 20 27 2a 27 2e 0a 20 20 2a 2f  ame is '*'..  */
21b68 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
21b69 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
21b6a 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b  +){.    char *z;
21b6b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20  .    int n;.    
21b6c 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f  pLevel = &pWInfo
21b6d 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 54 61 62  ->a[i];.    pTab
21b6e 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74  Item = &pTabList
21b6f 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f  ->a[pLevel->iFro
21b70 6d 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54 61 62  m];.    z = pTab
21b71 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20  Item->zAlias;.  
21b72 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a 20 3d    if( z==0 ) z =
21b73 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d   pTabItem->pTab-
21b74 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20 3d 20  >zName;.    n = 
21b75 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 20 20 69  strlen(z);.    i
21b76 66 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69  f( n+nQPlan < si
21b77 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75 65  zeof(sqlite3_que
21b78 72 79 5f 70 6c 61 6e 29 2d 31 30 20 29 7b 0a 20  ry_plan)-10 ){. 
21b79 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d       if( pLevel-
21b7a 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  >flags & WHERE_I
21b7b 44 58 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20  DX_ONLY ){.     
21b7c 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
21b7d 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
21b7e 50 6c 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b  Plan], "{}", 2);
21b7f 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  .        nQPlan 
21b80 2b 3d 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  += 2;.      }els
21b81 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  e{.        memcp
21b82 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79  y(&sqlite3_query
21b83 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 7a  _plan[nQPlan], z
21b84 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51  , n);.        nQ
21b85 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20  Plan += n;.     
21b86 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
21b87 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
21b88 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20  an++] = ' ';.   
21b89 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65   }.    if( pLeve
21b8a 6c 2d 3e 66 6c 61 67 73 20 26 20 28 57 48 45 52  l->flags & (WHER
21b8b 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45  E_ROWID_EQ|WHERE
21b8c 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b  _ROWID_RANGE) ){
21b8d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73  .      memcpy(&s
21b8e 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
21b8f 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 2a 20 22 2c  n[nQPlan], "* ",
21b90 20 32 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61   2);.      nQPla
21b91 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 7d 65 6c 73  n += 2;.    }els
21b92 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 49  e if( pLevel->pI
21b93 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d  dx==0 ){.      m
21b94 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71  emcpy(&sqlite3_q
21b95 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
21b96 5d 2c 20 22 7b 7d 20 22 2c 20 33 29 3b 0a 20 20  ], "{} ", 3);.  
21b97 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b      nQPlan += 3;
21b98 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
21b99 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 70 4c 65    n = strlen(pLe
21b9a 76 65 6c 2d 3e 70 49 64 78 2d 3e 7a 4e 61 6d 65  vel->pIdx->zName
21b9b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 2b 6e  );.      if( n+n
21b9c 51 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73  QPlan < sizeof(s
21b9d 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
21b9e 6e 29 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20  n)-2 ){.        
21b9f 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f  memcpy(&sqlite3_
21ba0 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61  query_plan[nQPla
21ba1 6e 5d 2c 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78  n], pLevel->pIdx
21ba2 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20  ->zName, n);.   
21ba3 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e       nQPlan += n
21ba4 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21ba5 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50  3_query_plan[nQP
21ba6 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20  lan++] = ' ';.  
21ba7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
21ba8 20 20 77 68 69 6c 65 28 20 6e 51 50 6c 61 6e 3e    while( nQPlan>
21ba9 30 20 26 26 20 73 71 6c 69 74 65 33 5f 71 75 65  0 && sqlite3_que
21baa 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2d 31  ry_plan[nQPlan-1
21bab 5d 3d 3d 27 20 27 20 29 7b 0a 20 20 20 20 73 71  ]==' ' ){.    sq
21bac 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
21bad 5b 2d 2d 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a  [--nQPlan] = 0;.
21bae 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 71 75    }.  sqlite3_qu
21baf 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
21bb0 20 3d 20 30 3b 0a 20 20 6e 51 50 6c 61 6e 20 3d   = 0;.  nQPlan =
21bb1 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   0;.#endif /* SQ
21bb2 4c 49 54 45 5f 54 45 53 54 20 2f 2f 20 54 65 73  LITE_TEST // Tes
21bb3 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69  ting and debuggi
21bb4 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 0a  ng use only */..
21bb5 20 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20    /* Record the 
21bb6 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 61 64 64  continuation add
21bb7 72 65 73 73 20 69 6e 20 74 68 65 20 57 68 65 72  ress in the Wher
21bb8 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  eInfo structure.
21bb9 20 20 54 68 65 6e 0a 20 20 2a 2a 20 63 6c 65 61    Then.  ** clea
21bba 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 2e  n up and return.
21bbb 0a 20 20 2a 2f 0a 20 20 70 57 49 6e 66 6f 2d 3e  .  */.  pWInfo->
21bbc 69 43 6f 6e 74 69 6e 75 65 20 3d 20 63 6f 6e 74  iContinue = cont
21bbd 3b 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 43  ;.  whereClauseC
21bbe 6c 65 61 72 28 26 77 63 29 3b 0a 20 20 72 65 74  lear(&wc);.  ret
21bbf 75 72 6e 20 70 57 49 6e 66 6f 3b 0a 0a 20 20 2f  urn pWInfo;..  /
21bc0 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 6d  * Jump here if m
21bc1 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a 2f 0a 77  alloc fails */.w
21bc2 68 65 72 65 42 65 67 69 6e 4e 6f 4d 65 6d 3a 0a  hereBeginNoMem:.
21bc3 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65    whereClauseCle
21bc4 61 72 28 26 77 63 29 3b 0a 20 20 77 68 65 72 65  ar(&wc);.  where
21bc5 49 6e 66 6f 46 72 65 65 28 70 57 49 6e 66 6f 29  InfoFree(pWInfo)
21bc6 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
21bc7 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
21bc8 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57  the end of the W
21bc9 48 45 52 45 20 6c 6f 6f 70 2e 20 20 53 65 65 20  HERE loop.  See 
21bca 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 0a 2a 2a 20  comments on .** 
21bcb 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
21bcc 6e 28 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  n() for addition
21bcd 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
21bce 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21bcf 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68  E void sqlite3Wh
21bd0 65 72 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f  ereEnd(WhereInfo
21bd1 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 56 64 62   *pWInfo){.  Vdb
21bd2 65 20 2a 76 20 3d 20 70 57 49 6e 66 6f 2d 3e 70  e *v = pWInfo->p
21bd3 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
21bd4 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 4c 65  int i;.  WhereLe
21bd5 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20 20 53  vel *pLevel;.  S
21bd6 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
21bd7 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c   = pWInfo->pTabL
21bd8 69 73 74 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  ist;..  /* Gener
21bd9 61 74 65 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61  ate loop termina
21bda 74 69 6f 6e 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  tion code..  */.
21bdb 20 20 66 6f 72 28 69 3d 70 54 61 62 4c 69 73 74    for(i=pTabList
21bdc 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 30 3b 20  ->nSrc-1; i>=0; 
21bdd 69 2d 2d 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c  i--){.    pLevel
21bde 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d   = &pWInfo->a[i]
21bdf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21be0 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
21be1 20 70 4c 65 76 65 6c 2d 3e 63 6f 6e 74 29 3b 0a   pLevel->cont);.
21be2 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
21be3 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20  op!=OP_Noop ){. 
21be4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21be5 41 64 64 4f 70 28 76 2c 20 70 4c 65 76 65 6c 2d  AddOp(v, pLevel-
21be6 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70 31 2c  >op, pLevel->p1,
21be7 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0a 20 20   pLevel->p2);.  
21be8 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 76    }.    if( pLev
21be9 65 6c 2d 3e 6e 49 6e 20 29 7b 0a 20 20 20 20 20  el->nIn ){.     
21bea 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 2a   struct InLoop *
21beb 70 49 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  pIn;.      int j
21bec 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21bed 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
21bee 76 2c 20 70 4c 65 76 65 6c 2d 3e 6e 78 74 29 3b  v, pLevel->nxt);
21bef 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 4c 65  .      for(j=pLe
21bf0 76 65 6c 2d 3e 6e 49 6e 2c 20 70 49 6e 3d 26 70  vel->nIn, pIn=&p
21bf1 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 5b 6a  Level->aInLoop[j
21bf2 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70  -1]; j>0; j--, p
21bf3 49 6e 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 73  In--){.        s
21bf4 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
21bf5 72 65 28 76 2c 20 70 49 6e 2d 3e 74 6f 70 41 64  re(v, pIn->topAd
21bf6 64 72 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 73  dr+1);.        s
21bf7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
21bf8 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 70 49 6e 2d  v, OP_Next, pIn-
21bf9 3e 69 43 75 72 2c 20 70 49 6e 2d 3e 74 6f 70 41  >iCur, pIn->topA
21bfa 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ddr);.        sq
21bfb 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
21bfc 65 28 76 2c 20 70 49 6e 2d 3e 74 6f 70 41 64 64  e(v, pIn->topAdd
21bfd 72 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  r-1);.      }.  
21bfe 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
21bff 28 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70  (pLevel->aInLoop
21c00 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
21c01 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
21c02 61 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e  abel(v, pLevel->
21c03 62 72 6b 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  brk);.    if( pL
21c04 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20  evel->iLeftJoin 
21c05 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  ){.      int add
21c06 72 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  r;.      addr = 
21c07 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21c08 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f 73 2c  (v, OP_IfMemPos,
21c09 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
21c0a 69 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  in, 0);.      sq
21c0b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
21c0c 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 54  , OP_NullRow, pT
21c0d 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  abList->a[i].iCu
21c0e 72 73 6f 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  rsor, 0);.      
21c0f 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  if( pLevel->iIdx
21c10 43 75 72 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  Cur>=0 ){.      
21c11 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21c12 4f 70 28 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77  Op(v, OP_NullRow
21c13 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75  , pLevel->iIdxCu
21c14 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r, 0);.      }. 
21c15 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21c16 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f  AddOp(v, OP_Goto
21c17 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 74 6f 70  , 0, pLevel->top
21c18 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21c19 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
21c1a 61 64 64 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  addr);.    }.  }
21c1b 0a 0a 20 20 2f 2a 20 54 68 65 20 22 62 72 65 61  ..  /* The "brea
21c1c 6b 22 20 70 6f 69 6e 74 20 69 73 20 68 65 72 65  k" point is here
21c1d 2c 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20  , just past the 
21c1e 65 6e 64 20 6f 66 20 74 68 65 20 6f 75 74 65 72  end of the outer
21c1f 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 53 65 74 20   loop..  ** Set 
21c20 69 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  it..  */.  sqlit
21c21 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
21c22 65 6c 28 76 2c 20 70 57 49 6e 66 6f 2d 3e 69 42  el(v, pWInfo->iB
21c23 72 65 61 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f  reak);..  /* Clo
21c24 73 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 75  se all of the cu
21c25 72 73 6f 72 73 20 74 68 61 74 20 77 65 72 65 20  rsors that were 
21c26 6f 70 65 6e 65 64 20 62 79 20 73 71 6c 69 74 65  opened by sqlite
21c27 33 57 68 65 72 65 42 65 67 69 6e 2e 0a 20 20 2a  3WhereBegin..  *
21c28 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65  /.  for(i=0, pLe
21c29 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69  vel=pWInfo->a; i
21c2a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pTabList->nSrc;
21c2b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b   i++, pLevel++){
21c2c 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
21c2d 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74  ist_item *pTabIt
21c2e 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e  em = &pTabList->
21c2f 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d  a[pLevel->iFrom]
21c30 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
21c31 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70 54  b = pTabItem->pT
21c32 61 62 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ab;.    assert( 
21c33 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 69  pTab!=0 );.    i
21c34 66 28 20 70 54 61 62 2d 3e 69 73 45 70 68 65 6d  f( pTab->isEphem
21c35 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   || pTab->pSelec
21c36 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  t ) continue;.  
21c37 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 66    if( (pLevel->f
21c38 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
21c39 5f 4f 4e 4c 59 29 3d 3d 30 20 29 7b 0a 20 20 20  _ONLY)==0 ){.   
21c3a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21c3b 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
21c3c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
21c3d 6f 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  or, 0);.    }.  
21c3e 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 49    if( pLevel->pI
21c3f 64 78 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  dx!=0 ){.      s
21c40 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
21c41 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 4c 65  v, OP_Close, pLe
21c42 76 65 6c 2d 3e 69 49 64 78 43 75 72 2c 20 30 29  vel->iIdxCur, 0)
21c43 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
21c44 4d 61 6b 65 20 63 75 72 73 6f 72 20 73 75 62 73  Make cursor subs
21c45 74 69 74 75 74 69 6f 6e 73 20 66 6f 72 20 63 61  titutions for ca
21c46 73 65 73 20 77 68 65 72 65 20 77 65 20 77 61 6e  ses where we wan
21c47 74 20 74 6f 20 75 73 65 0a 20 20 20 20 2a 2a 20  t to use.    ** 
21c48 6a 75 73 74 20 74 68 65 20 69 6e 64 65 78 20 61  just the index a
21c49 6e 64 20 6e 65 76 65 72 20 72 65 66 65 72 65 6e  nd never referen
21c4a 63 65 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  ce the table..  
21c4b 20 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c    ** .    ** Cal
21c4c 6c 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67  ls to the code g
21c4d 65 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77  enerator in betw
21c4e 65 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65  een sqlite3Where
21c4f 42 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a  Begin and.    **
21c50 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
21c51 20 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74   will have creat
21c52 65 64 20 63 6f 64 65 20 74 68 61 74 20 72 65 66  ed code that ref
21c53 65 72 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c  erences the tabl
21c54 65 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c  e.    ** directl
21c55 79 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63  y.  This loop sc
21c56 61 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64  ans all that cod
21c57 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70  e looking for op
21c58 63 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61  codes.    ** tha
21c59 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20  t reference the 
21c5a 74 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72  table and conver
21c5b 74 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63  ts them into opc
21c5c 6f 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a  odes that.    **
21c5d 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 69   reference the i
21c5e 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ndex..    */.   
21c5f 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61   if( pLevel->fla
21c60 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f  gs & WHERE_IDX_O
21c61 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  NLY ){.      int
21c62 20 6b 2c 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20   k, j, last;.   
21c63 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a     VdbeOp *pOp;.
21c64 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
21c65 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78  x = pLevel->pIdx
21c66 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
21c67 20 70 49 64 78 21 3d 30 20 29 3b 0a 20 20 20 20   pIdx!=0 );.    
21c68 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56    pOp = sqlite3V
21c69 64 62 65 47 65 74 4f 70 28 76 2c 20 70 57 49 6e  dbeGetOp(v, pWIn
21c6a 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20 20 20 20 20  fo->iTop);.     
21c6b 20 6c 61 73 74 20 3d 20 73 71 6c 69 74 65 33 56   last = sqlite3V
21c6c 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
21c6d 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70  );.      for(k=p
21c6e 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20 6b 3c 6c  WInfo->iTop; k<l
21c6f 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70 2b 2b 29  ast; k++, pOp++)
21c70 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  {.        if( pO
21c71 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c 2d 3e 69  p->p1!=pLevel->i
21c72 54 61 62 43 75 72 20 29 20 63 6f 6e 74 69 6e 75  TabCur ) continu
21c73 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  e;.        if( p
21c74 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43  Op->opcode==OP_C
21c75 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
21c76 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65     pOp->p1 = pLe
21c77 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20  vel->iIdxCur;.  
21c78 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
21c79 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
21c7a 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
21c7b 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d      if( pOp->p2=
21c7c 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  =pIdx->aiColumn[
21c7d 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j] ){.          
21c7e 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b      pOp->p2 = j;
21c7f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
21c80 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
21c81 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
21c82 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
21c83 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
21c84 50 5f 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  P_Rowid ){.     
21c85 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70       pOp->p1 = p
21c86 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a  Level->iIdxCur;.
21c87 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f            pOp->o
21c88 70 63 6f 64 65 20 3d 20 4f 50 5f 49 64 78 52 6f  pcode = OP_IdxRo
21c89 77 69 64 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  wid;.        }el
21c8a 73 65 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  se if( pOp->opco
21c8b 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 52 6f 77 20 29  de==OP_NullRow )
21c8c 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  {.          pOp-
21c8d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f  >opcode = OP_Noo
21c8e 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  p;.        }.   
21c8f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
21c90 20 20 2f 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e    /* Final clean
21c91 75 70 0a 20 20 2a 2f 0a 20 20 77 68 65 72 65 49  up.  */.  whereI
21c92 6e 66 6f 46 72 65 65 28 70 57 49 6e 66 6f 29 3b  nfoFree(pWInfo);
21c93 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
21c94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
21c95 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a  d of where.c ***
21c96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21c97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21c98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
21c99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
21c9a 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 63  gin file parse.c
21c9b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
21c9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21c9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
21c9e 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74 65   Driver template
21c9f 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70   for the LEMON p
21ca0 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 2e  arser generator.
21ca1 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
21ca2 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
21ca3 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
21ca4 65 20 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 20 46 69  e code..*/./* Fi
21ca5 72 73 74 20 6f 66 66 2c 20 63 6f 64 65 20 69 73  rst off, code is
21ca6 20 69 6e 63 6c 75 64 65 20 77 68 69 63 68 20 66   include which f
21ca7 6f 6c 6c 6f 77 73 20 74 68 65 20 22 69 6e 63 6c  ollows the "incl
21ca8 75 64 65 22 20 64 65 63 6c 61 72 61 74 69 6f 6e  ude" declaration
21ca9 0a 2a 2a 20 69 6e 20 74 68 65 20 69 6e 70 75 74  .** in the input
21caa 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a   file. */.../*.*
21cab 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
21cac 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
21cad 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f  holds informatio
21cae 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 4c  n about the.** L
21caf 49 4d 49 54 20 63 6c 61 75 73 65 20 6f 66 20 61  IMIT clause of a
21cb0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
21cb1 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6d  t..*/.struct Lim
21cb2 69 74 56 61 6c 20 7b 0a 20 20 45 78 70 72 20 2a  itVal {.  Expr *
21cb3 70 4c 69 6d 69 74 3b 20 20 20 20 2f 2a 20 54 68  pLimit;    /* Th
21cb4 65 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69  e LIMIT expressi
21cb5 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66 20 74 68 65  on.  NULL if the
21cb6 72 65 20 69 73 20 6e 6f 20 6c 69 6d 69 74 20 2a  re is no limit *
21cb7 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65  /.  Expr *pOffse
21cb8 74 3b 20 20 20 2f 2a 20 54 68 65 20 4f 46 46 53  t;   /* The OFFS
21cb9 45 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  ET expression.  
21cba 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
21cbb 20 6e 6f 6e 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   none */.};../*.
21cbc 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
21cbd 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
21cbe 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72   is used to stor
21cbf 65 20 74 68 65 20 4c 49 4b 45 2c 0a 2a 2a 20 47  e the LIKE,.** G
21cc0 4c 4f 42 2c 20 4e 4f 54 20 4c 49 4b 45 2c 20 61  LOB, NOT LIKE, a
21cc1 6e 64 20 4e 4f 54 20 47 4c 4f 42 20 6f 70 65 72  nd NOT GLOB oper
21cc2 61 74 6f 72 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  ators..*/.struct
21cc3 20 4c 69 6b 65 4f 70 20 7b 0a 20 20 54 6f 6b 65   LikeOp {.  Toke
21cc4 6e 20 65 4f 70 65 72 61 74 6f 72 3b 20 20 2f 2a  n eOperator;  /*
21cc5 20 22 6c 69 6b 65 22 20 6f 72 20 22 67 6c 6f 62   "like" or "glob
21cc6 22 20 6f 72 20 22 72 65 67 65 78 70 22 20 2a 2f  " or "regexp" */
21cc7 0a 20 20 69 6e 74 20 6e 6f 74 3b 20 20 20 20 20  .  int not;     
21cc8 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
21cc9 68 65 20 4e 4f 54 20 6b 65 79 77 6f 72 64 20 69  he NOT keyword i
21cca 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 7d 3b 0a  s present */.};.
21ccb 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
21ccc 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
21ccd 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65  ing structure de
21cce 73 63 72 69 62 65 73 20 74 68 65 20 65 76 65 6e  scribes the even
21ccf 74 20 6f 66 20 61 0a 2a 2a 20 54 52 49 47 47 45  t of a.** TRIGGE
21cd0 52 2e 20 20 22 61 22 20 69 73 20 74 68 65 20 65  R.  "a" is the e
21cd1 76 65 6e 74 20 74 79 70 65 2c 20 6f 6e 65 20 6f  vent type, one o
21cd2 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f  f TK_UPDATE, TK_
21cd3 49 4e 53 45 52 54 2c 0a 2a 2a 20 54 4b 5f 44 45  INSERT,.** TK_DE
21cd4 4c 45 54 45 2c 20 6f 72 20 54 4b 5f 49 4e 53 54  LETE, or TK_INST
21cd5 45 41 44 2e 20 20 49 66 20 74 68 65 20 65 76 65  EAD.  If the eve
21cd6 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  nt is of the for
21cd7 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 55 50 44  m.**.**      UPD
21cd8 41 54 45 20 4f 4e 20 28 61 2c 62 2c 63 29 0a 2a  ATE ON (a,b,c).*
21cd9 2a 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 22 62  *.** Then the "b
21cda 22 20 49 64 4c 69 73 74 20 72 65 63 6f 72 64 73  " IdList records
21cdb 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63   the list "a,b,c
21cdc 22 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 72 69  "..*/.struct Tri
21cdd 67 45 76 65 6e 74 20 7b 20 69 6e 74 20 61 3b 20  gEvent { int a; 
21cde 49 64 4c 69 73 74 20 2a 20 62 3b 20 7d 3b 0a 0a  IdList * b; };..
21cdf 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
21ce0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
21ce1 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20 41 54  ure holds the AT
21ce2 54 41 43 48 20 6b 65 79 20 61 6e 64 20 74 68 65  TACH key and the
21ce3 20 6b 65 79 20 74 79 70 65 2e 0a 2a 2f 0a 73 74   key type..*/.st
21ce4 72 75 63 74 20 41 74 74 61 63 68 4b 65 79 20 7b  ruct AttachKey {
21ce5 20 69 6e 74 20 74 79 70 65 3b 20 20 54 6f 6b 65   int type;  Toke
21ce6 6e 20 6b 65 79 3b 20 7d 3b 0a 0a 2f 2a 20 4e 65  n key; };../* Ne
21ce7 78 74 20 69 73 20 61 6c 6c 20 74 6f 6b 65 6e 20  xt is all token 
21ce8 76 61 6c 75 65 73 2c 20 69 6e 20 61 20 66 6f 72  values, in a for
21ce9 6d 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75  m suitable for u
21cea 73 65 20 62 79 20 6d 61 6b 65 68 65 61 64 65 72  se by makeheader
21ceb 73 2e 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69  s..** This secti
21cec 6f 6e 20 77 69 6c 6c 20 62 65 20 6e 75 6c 6c 20  on will be null 
21ced 75 6e 6c 65 73 73 20 6c 65 6d 6f 6e 20 69 73 20  unless lemon is 
21cee 72 75 6e 20 77 69 74 68 20 74 68 65 20 2d 6d 20  run with the -m 
21cef 73 77 69 74 63 68 2e 0a 2a 2f 0a 2f 2a 20 0a 2a  switch..*/./* .*
21cf0 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
21cf1 73 20 28 61 6c 6c 20 67 65 6e 65 72 61 74 65 64  s (all generated
21cf2 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62   automatically b
21cf3 79 20 74 68 65 20 70 61 72 73 65 72 20 67 65 6e  y the parser gen
21cf4 65 72 61 74 6f 72 29 0a 2a 2a 20 73 70 65 63 69  erator).** speci
21cf5 66 79 20 74 68 65 20 76 61 72 69 6f 75 73 20 6b  fy the various k
21cf6 69 6e 64 73 20 6f 66 20 74 6f 6b 65 6e 73 20 28  inds of tokens (
21cf7 74 65 72 6d 69 6e 61 6c 73 29 20 74 68 61 74 20  terminals) that 
21cf8 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 75 6e  the parser.** un
21cf9 64 65 72 73 74 61 6e 64 73 2e 20 0a 2a 2a 0a 2a  derstands. .**.*
21cfa 2a 20 45 61 63 68 20 73 79 6d 62 6f 6c 20 68 65  * Each symbol he
21cfb 72 65 20 69 73 20 61 20 74 65 72 6d 69 6e 61 6c  re is a terminal
21cfc 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 65 20 67   symbol in the g
21cfd 72 61 6d 6d 61 72 2e 0a 2a 2f 0a 2f 2a 20 4d 61  rammar..*/./* Ma
21cfe 6b 65 20 73 75 72 65 20 74 68 65 20 49 4e 54 45  ke sure the INTE
21cff 52 46 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64  RFACE macro is d
21d00 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64  efined..*/.#ifnd
21d01 65 66 20 49 4e 54 45 52 46 41 43 45 0a 23 20 64  ef INTERFACE.# d
21d02 65 66 69 6e 65 20 49 4e 54 45 52 46 41 43 45 20  efine INTERFACE 
21d03 31 0a 23 65 6e 64 69 66 0a 2f 2a 20 54 68 65 20  1.#endif./* The 
21d04 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 63 6c 75  next thing inclu
21d05 64 65 64 20 69 73 20 73 65 72 69 65 73 20 6f 66  ded is series of
21d06 20 64 65 66 69 6e 65 73 20 77 68 69 63 68 20 63   defines which c
21d07 6f 6e 74 72 6f 6c 0a 2a 2a 20 76 61 72 69 6f 75  ontrol.** variou
21d08 73 20 61 73 70 65 63 74 73 20 6f 66 20 74 68 65  s aspects of the
21d09 20 67 65 6e 65 72 61 74 65 64 20 70 61 72 73 65   generated parse
21d0a 72 2e 0a 2a 2a 20 20 20 20 59 59 43 4f 44 45 54  r..**    YYCODET
21d0b 59 50 45 20 20 20 20 20 20 20 20 20 69 73 20 74  YPE         is t
21d0c 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65  he data type use
21d0d 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  d for storing te
21d0e 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  rminal.**       
21d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d10 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20  and nonterminal 
21d11 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67  numbers.  "unsig
21d12 6e 65 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20  ned char" is.** 
21d13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d14 20 20 20 20 20 20 75 73 65 64 20 69 66 20 74 68        used if th
21d15 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68  ere are fewer th
21d16 61 6e 20 32 35 30 20 74 65 72 6d 69 6e 61 6c 73  an 250 terminals
21d17 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
21d18 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f            and no
21d19 6e 74 65 72 6d 69 6e 61 6c 73 2e 20 20 22 69 6e  nterminals.  "in
21d1a 74 22 20 69 73 20 75 73 65 64 20 6f 74 68 65 72  t" is used other
21d1b 77 69 73 65 2e 0a 2a 2a 20 20 20 20 59 59 4e 4f  wise..**    YYNO
21d1c 43 4f 44 45 20 20 20 20 20 20 20 20 20 20 20 69  CODE           i
21d1d 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 74 79  s a number of ty
21d1e 70 65 20 59 59 43 4f 44 45 54 59 50 45 20 77 68  pe YYCODETYPE wh
21d1f 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a  ich corresponds.
21d20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
21d21 20 20 20 20 20 20 20 20 20 74 6f 20 6e 6f 20 6c           to no l
21d22 65 67 61 6c 20 74 65 72 6d 69 6e 61 6c 20 6f 72  egal terminal or
21d23 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d   nonterminal num
21d24 62 65 72 2e 20 20 54 68 69 73 0a 2a 2a 20 20 20  ber.  This.**   
21d25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d26 20 20 20 20 6e 75 6d 62 65 72 20 69 73 20 75 73      number is us
21d27 65 64 20 74 6f 20 66 69 6c 6c 20 69 6e 20 65 6d  ed to fill in em
21d28 70 74 79 20 73 6c 6f 74 73 20 6f 66 20 74 68 65  pty slots of the
21d29 20 68 61 73 68 20 0a 2a 2a 20 20 20 20 20 20 20   hash .**       
21d2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d2b 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 59 59 46  table..**    YYF
21d2c 41 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20  ALLBACK         
21d2d 49 66 20 64 65 66 69 6e 65 64 2c 20 74 68 69 73  If defined, this
21d2e 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
21d2f 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65  one or more toke
21d30 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ns.**           
21d31 20 20 20 20 20 20 20 20 20 20 20 20 68 61 76 65              have
21d32 20 66 61 6c 6c 2d 62 61 63 6b 20 76 61 6c 75 65   fall-back value
21d33 73 20 77 68 69 63 68 20 73 68 6f 75 6c 64 20 62  s which should b
21d34 65 20 75 73 65 64 20 69 66 20 74 68 65 0a 2a 2a  e used if the.**
21d35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d36 20 20 20 20 20 20 20 6f 72 69 67 69 6e 61 6c 20         original 
21d37 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b  value of the tok
21d38 65 6e 20 77 69 6c 6c 20 6e 6f 74 20 70 61 72 73  en will not pars
21d39 65 2e 0a 2a 2a 20 20 20 20 59 59 41 43 54 49 4f  e..**    YYACTIO
21d3a 4e 54 59 50 45 20 20 20 20 20 20 20 69 73 20 74  NTYPE       is t
21d3b 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65  he data type use
21d3c 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  d for storing te
21d3d 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  rminal.**       
21d3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d3f 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20  and nonterminal 
21d40 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67  numbers.  "unsig
21d41 6e 65 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20  ned char" is.** 
21d42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d43 20 20 20 20 20 20 75 73 65 64 20 69 66 20 74 68        used if th
21d44 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68  ere are fewer th
21d45 61 6e 20 32 35 30 20 72 75 6c 65 73 20 61 6e 64  an 250 rules and
21d46 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
21d47 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 73            states
21d48 20 63 6f 6d 62 69 6e 65 64 2e 20 20 22 69 6e 74   combined.  "int
21d49 22 20 69 73 20 75 73 65 64 20 6f 74 68 65 72 77  " is used otherw
21d4a 69 73 65 2e 0a 2a 2a 20 20 20 20 73 71 6c 69 74  ise..**    sqlit
21d4b 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50  e3ParserTOKENTYP
21d4c 45 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74  E     is the dat
21d4d 61 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20  a type used for 
21d4e 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73 20 67 69 76  minor tokens giv
21d4f 65 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  en .**          
21d50 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 72               dir
21d51 65 63 74 6c 79 20 74 6f 20 74 68 65 20 70 61 72  ectly to the par
21d52 73 65 72 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b  ser from the tok
21d53 65 6e 69 7a 65 72 2e 0a 2a 2a 20 20 20 20 59 59  enizer..**    YY
21d54 4d 49 4e 4f 52 54 59 50 45 20 20 20 20 20 20 20  MINORTYPE       
21d55 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
21d56 65 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 6d  e used for all m
21d57 69 6e 6f 72 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20  inor tokens..** 
21d58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d59 20 20 20 20 20 20 54 68 69 73 20 69 73 20 74 79        This is ty
21d5a 70 69 63 61 6c 6c 79 20 61 20 75 6e 69 6f 6e 20  pically a union 
21d5b 6f 66 20 6d 61 6e 79 20 74 79 70 65 73 2c 20 6f  of many types, o
21d5c 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ne of.**        
21d5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77                 w
21d5e 68 69 63 68 20 69 73 20 73 71 6c 69 74 65 33 50  hich is sqlite3P
21d5f 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 2e 20  arserTOKENTYPE. 
21d60 20 54 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68   The entry in th
21d61 65 20 75 6e 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  e union.**      
21d62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d63 20 66 6f 72 20 62 61 73 65 20 74 6f 6b 65 6e 73   for base tokens
21d64 20 69 73 20 63 61 6c 6c 65 64 20 22 79 79 30 22   is called "yy0"
21d65 2e 0a 2a 2a 20 20 20 20 59 59 53 54 41 43 4b 44  ..**    YYSTACKD
21d66 45 50 54 48 20 20 20 20 20 20 20 69 73 20 74 68  EPTH       is th
21d67 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  e maximum depth 
21d68 6f 66 20 74 68 65 20 70 61 72 73 65 72 27 73 20  of the parser's 
21d69 73 74 61 63 6b 2e 20 20 49 66 0a 2a 2a 20 20 20  stack.  If.**   
21d6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d6b 20 20 20 20 7a 65 72 6f 20 74 68 65 20 73 74 61      zero the sta
21d6c 63 6b 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c  ck is dynamicall
21d6d 79 20 73 69 7a 65 64 20 75 73 69 6e 67 20 72 65  y sized using re
21d6e 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 73 71  alloc().**    sq
21d6f 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
21d70 44 45 43 4c 20 20 20 20 20 41 20 73 74 61 74 69  DECL     A stati
21d71 63 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61  c variable decla
21d72 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25  ration for the %
21d73 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a  extra_argument.*
21d74 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73  *    sqlite3Pars
21d75 65 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20  erARG_PDECL     
21d76 41 20 70 61 72 61 6d 65 74 65 72 20 64 65 63 6c  A parameter decl
21d77 61 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  aration for the 
21d78 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a  %extra_argument.
21d79 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72  **    sqlite3Par
21d7a 73 65 72 41 52 47 5f 53 54 4f 52 45 20 20 20 20  serARG_STORE    
21d7b 20 43 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 25   Code to store %
21d7c 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 69  extra_argument i
21d7d 6e 74 6f 20 79 79 70 50 61 72 73 65 72 0a 2a 2a  nto yypParser.**
21d7e 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
21d7f 72 41 52 47 5f 46 45 54 43 48 20 20 20 20 20 43  rARG_FETCH     C
21d80 6f 64 65 20 74 6f 20 65 78 74 72 61 63 74 20 25  ode to extract %
21d81 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 66  extra_argument f
21d82 72 6f 6d 20 79 79 70 50 61 72 73 65 72 0a 2a 2a  rom yypParser.**
21d83 20 20 20 20 59 59 4e 53 54 41 54 45 20 20 20 20      YYNSTATE    
21d84 20 20 20 20 20 20 20 74 68 65 20 63 6f 6d 62 69         the combi
21d85 6e 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 74  ned number of st
21d86 61 74 65 73 2e 0a 2a 2a 20 20 20 20 59 59 4e 52  ates..**    YYNR
21d87 55 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 74  ULE            t
21d88 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 75 6c  he number of rul
21d89 65 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61  es in the gramma
21d8a 72 0a 2a 2a 20 20 20 20 59 59 45 52 52 4f 52 53  r.**    YYERRORS
21d8b 59 4d 42 4f 4c 20 20 20 20 20 20 69 73 20 74 68  YMBOL      is th
21d8c 65 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66  e code number of
21d8d 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f   the error symbo
21d8e 6c 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 20 20  l.  If not.**   
21d8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d90 20 20 20 20 64 65 66 69 6e 65 64 2c 20 74 68 65      defined, the
21d91 6e 20 64 6f 20 6e 6f 20 65 72 72 6f 72 20 70 72  n do no error pr
21d92 6f 63 65 73 73 69 6e 67 2e 0a 2a 2f 0a 23 64 65  ocessing..*/.#de
21d93 66 69 6e 65 20 59 59 43 4f 44 45 54 59 50 45 20  fine YYCODETYPE 
21d94 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a 23 64  unsigned char.#d
21d95 65 66 69 6e 65 20 59 59 4e 4f 43 4f 44 45 20 32  efine YYNOCODE 2
21d96 34 38 0a 23 64 65 66 69 6e 65 20 59 59 41 43 54  48.#define YYACT
21d97 49 4f 4e 54 59 50 45 20 75 6e 73 69 67 6e 65 64  IONTYPE unsigned
21d98 20 73 68 6f 72 74 20 69 6e 74 0a 23 64 65 66 69   short int.#defi
21d99 6e 65 20 59 59 57 49 4c 44 43 41 52 44 20 35 39  ne YYWILDCARD 59
21d9a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
21d9b 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20  ParserTOKENTYPE 
21d9c 54 6f 6b 65 6e 0a 74 79 70 65 64 65 66 20 75 6e  Token.typedef un
21d9d 69 6f 6e 20 7b 0a 20 20 73 71 6c 69 74 65 33 50  ion {.  sqlite3P
21d9e 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 79  arserTOKENTYPE y
21d9f 79 30 3b 0a 20 20 69 6e 74 20 79 79 34 36 3b 0a  y0;.  int yy46;.
21da0 20 20 73 74 72 75 63 74 20 4c 69 6b 65 4f 70 20    struct LikeOp 
21da1 79 79 37 32 3b 0a 20 20 45 78 70 72 2a 20 79 79  yy72;.  Expr* yy
21da2 31 37 32 3b 0a 20 20 45 78 70 72 4c 69 73 74 2a  172;.  ExprList*
21da3 20 79 79 31 37 34 3b 0a 20 20 53 65 6c 65 63 74   yy174;.  Select
21da4 2a 20 79 79 32 31 39 3b 0a 20 20 73 74 72 75 63  * yy219;.  struc
21da5 74 20 4c 69 6d 69 74 56 61 6c 20 79 79 32 33 34  t LimitVal yy234
21da6 3b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 2a  ;.  TriggerStep*
21da7 20 79 79 32 34 33 3b 0a 20 20 73 74 72 75 63 74   yy243;.  struct
21da8 20 54 72 69 67 45 76 65 6e 74 20 79 79 33 37 30   TrigEvent yy370
21da9 3b 0a 20 20 53 72 63 4c 69 73 74 2a 20 79 79 33  ;.  SrcList* yy3
21daa 37 33 3b 0a 20 20 45 78 70 72 20 2a 20 79 79 33  73;.  Expr * yy3
21dab 38 36 3b 0a 20 20 73 74 72 75 63 74 20 7b 69 6e  86;.  struct {in
21dac 74 20 76 61 6c 75 65 3b 20 69 6e 74 20 6d 61 73  t value; int mas
21dad 6b 3b 7d 20 79 79 34 30 35 3b 0a 20 20 54 6f 6b  k;} yy405;.  Tok
21dae 65 6e 20 79 79 34 31 30 3b 0a 20 20 49 64 4c 69  en yy410;.  IdLi
21daf 73 74 2a 20 79 79 34 33 32 3b 0a 20 20 69 6e 74  st* yy432;.  int
21db0 20 79 79 34 39 35 3b 0a 7d 20 59 59 4d 49 4e 4f   yy495;.} YYMINO
21db1 52 54 59 50 45 3b 0a 23 69 66 6e 64 65 66 20 59  RTYPE;.#ifndef Y
21db2 59 53 54 41 43 4b 44 45 50 54 48 0a 23 64 65 66  YSTACKDEPTH.#def
21db3 69 6e 65 20 59 59 53 54 41 43 4b 44 45 50 54 48  ine YYSTACKDEPTH
21db4 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 64 65 66   100.#endif.#def
21db5 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65  ine sqlite3Parse
21db6 72 41 52 47 5f 53 44 45 43 4c 20 50 61 72 73 65  rARG_SDECL Parse
21db7 20 2a 70 50 61 72 73 65 3b 0a 23 64 65 66 69 6e   *pParse;.#defin
21db8 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41  e sqlite3ParserA
21db9 52 47 5f 50 44 45 43 4c 20 2c 50 61 72 73 65 20  RG_PDECL ,Parse 
21dba 2a 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65 20  *pParse.#define 
21dbb 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
21dbc 5f 46 45 54 43 48 20 50 61 72 73 65 20 2a 70 50  _FETCH Parse *pP
21dbd 61 72 73 65 20 3d 20 79 79 70 50 61 72 73 65 72  arse = yypParser
21dbe 2d 3e 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65  ->pParse.#define
21dbf 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
21dc0 47 5f 53 54 4f 52 45 20 79 79 70 50 61 72 73 65  G_STORE yypParse
21dc1 72 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  r->pParse = pPar
21dc2 73 65 0a 23 64 65 66 69 6e 65 20 59 59 4e 53 54  se.#define YYNST
21dc3 41 54 45 20 35 38 36 0a 23 64 65 66 69 6e 65 20  ATE 586.#define 
21dc4 59 59 4e 52 55 4c 45 20 33 31 31 0a 23 64 65 66  YYNRULE 311.#def
21dc5 69 6e 65 20 59 59 45 52 52 4f 52 53 59 4d 42 4f  ine YYERRORSYMBO
21dc6 4c 20 31 33 38 0a 23 64 65 66 69 6e 65 20 59 59  L 138.#define YY
21dc7 45 52 52 53 59 4d 44 54 20 79 79 34 39 35 0a 23  ERRSYMDT yy495.#
21dc8 64 65 66 69 6e 65 20 59 59 46 41 4c 4c 42 41 43  define YYFALLBAC
21dc9 4b 20 31 0a 23 64 65 66 69 6e 65 20 59 59 5f 4e  K 1.#define YY_N
21dca 4f 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 28 59  O_ACTION      (Y
21dcb 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b  YNSTATE+YYNRULE+
21dcc 32 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 41 43  2).#define YY_AC
21dcd 43 45 50 54 5f 41 43 54 49 4f 4e 20 20 28 59 59  CEPT_ACTION  (YY
21dce 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31  NSTATE+YYNRULE+1
21dcf 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 45 52 52  ).#define YY_ERR
21dd0 4f 52 5f 41 43 54 49 4f 4e 20 20 20 28 59 59 4e  OR_ACTION   (YYN
21dd1 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 29 0a 0a  STATE+YYNRULE)..
21dd2 2f 2a 20 4e 65 78 74 20 61 72 65 20 74 68 61 74  /* Next are that
21dd3 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20   tables used to 
21dd4 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 61  determine what a
21dd5 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 62 61  ction to take ba
21dd6 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75  sed on the.** cu
21dd7 72 72 65 6e 74 20 73 74 61 74 65 20 61 6e 64 20  rrent state and 
21dd8 6c 6f 6f 6b 61 68 65 61 64 20 74 6f 6b 65 6e 2e  lookahead token.
21dd9 20 20 54 68 65 73 65 20 74 61 62 6c 65 73 20 61    These tables a
21dda 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  re used to imple
21ddb 6d 65 6e 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ment.** function
21ddc 73 20 74 68 61 74 20 74 61 6b 65 20 61 20 73 74  s that take a st
21ddd 61 74 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 6c  ate number and l
21dde 6f 6f 6b 61 68 65 61 64 20 76 61 6c 75 65 20 61  ookahead value a
21ddf 6e 64 20 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20  nd return an.** 
21de0 61 63 74 69 6f 6e 20 69 6e 74 65 67 65 72 2e 20  action integer. 
21de1 20 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20   .**.** Suppose 
21de2 74 68 65 20 61 63 74 69 6f 6e 20 69 6e 74 65 67  the action integ
21de3 65 72 20 69 73 20 4e 2e 20 20 54 68 65 6e 20 74  er is N.  Then t
21de4 68 65 20 61 63 74 69 6f 6e 20 69 73 20 64 65 74  he action is det
21de5 65 72 6d 69 6e 65 64 20 61 73 0a 2a 2a 20 66 6f  ermined as.** fo
21de6 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20 20 20 30 20  llows.**.**   0 
21de7 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45 20  <= N < YYNSTATE 
21de8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21de9 20 53 68 69 66 74 20 4e 2e 20 20 54 68 61 74 20   Shift N.  That 
21dea 69 73 2c 20 70 75 73 68 20 74 68 65 20 6c 6f 6f  is, push the loo
21deb 6b 61 68 65 61 64 0a 2a 2a 20 20 20 20 20 20 20  kahead.**       
21dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ded 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
21dee 6f 6b 65 6e 20 6f 6e 74 6f 20 74 68 65 20 73 74  oken onto the st
21def 61 63 6b 20 61 6e 64 20 67 6f 74 6f 20 73 74 61  ack and goto sta
21df0 74 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20 20 59 59  te N..**.**   YY
21df1 4e 53 54 41 54 45 20 3c 3d 20 4e 20 3c 20 59 59  NSTATE <= N < YY
21df2 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20  NSTATE+YYNRULE  
21df3 20 52 65 64 75 63 65 20 62 79 20 72 75 6c 65 20   Reduce by rule 
21df4 4e 2d 59 59 4e 53 54 41 54 45 2e 0a 2a 2a 0a 2a  N-YYNSTATE..**.*
21df5 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54  *   N == YYNSTAT
21df6 45 2b 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20  E+YYNRULE       
21df7 20 20 20 20 20 20 20 41 20 73 79 6e 74 61 78 20         A syntax 
21df8 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
21df9 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d  ed..**.**   N ==
21dfa 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
21dfb 45 2b 31 20 20 20 20 20 20 20 20 20 20 20 20 54  E+1            T
21dfc 68 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74  he parser accept
21dfd 73 20 69 74 73 20 69 6e 70 75 74 2e 0a 2a 2a 0a  s its input..**.
21dfe 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41  **   N == YYNSTA
21dff 54 45 2b 59 59 4e 52 55 4c 45 2b 32 20 20 20 20  TE+YYNRULE+2    
21e00 20 20 20 20 20 20 20 20 4e 6f 20 73 75 63 68 20          No such 
21e01 61 63 74 69 6f 6e 2e 20 20 44 65 6e 6f 74 65 73  action.  Denotes
21e02 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 20   unused.**      
21e03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e05 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 79 79 5f  slots in the yy_
21e06 61 63 74 69 6f 6e 5b 5d 20 74 61 62 6c 65 2e 0a  action[] table..
21e07 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 74 69 6f 6e  **.** The action
21e08 20 74 61 62 6c 65 20 69 73 20 63 6f 6e 73 74 72   table is constr
21e09 75 63 74 65 64 20 61 73 20 61 20 73 69 6e 67 6c  ucted as a singl
21e0a 65 20 6c 61 72 67 65 20 74 61 62 6c 65 20 6e 61  e large table na
21e0b 6d 65 64 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 2e  med yy_action[].
21e0c 0a 2a 2a 20 47 69 76 65 6e 20 73 74 61 74 65 20  .** Given state 
21e0d 53 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20  S and lookahead 
21e0e 58 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  X, the action is
21e0f 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 0a   computed as.**.
21e10 2a 2a 20 20 20 20 20 20 79 79 5f 61 63 74 69 6f  **      yy_actio
21e11 6e 5b 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74  n[ yy_shift_ofst
21e12 5b 53 5d 20 2b 20 58 20 5d 0a 2a 2a 0a 2a 2a 20  [S] + X ].**.** 
21e13 49 66 20 74 68 65 20 69 6e 64 65 78 20 76 61 6c  If the index val
21e14 75 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74  ue yy_shift_ofst
21e15 5b 53 5d 2b 58 20 69 73 20 6f 75 74 20 6f 66 20  [S]+X is out of 
21e16 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20  range or if the 
21e17 76 61 6c 75 65 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b  value.** yy_look
21e18 61 68 65 61 64 5b 79 79 5f 73 68 69 66 74 5f 6f  ahead[yy_shift_o
21e19 66 73 74 5b 53 5d 2b 58 5d 20 69 73 20 6e 6f 74  fst[S]+X] is not
21e1a 20 65 71 75 61 6c 20 74 6f 20 58 20 6f 72 20 69   equal to X or i
21e1b 66 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  f yy_shift_ofst[
21e1c 53 5d 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74  S].** is equal t
21e1d 6f 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44  o YY_SHIFT_USE_D
21e1e 46 4c 54 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  FLT, it means th
21e1f 61 74 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  at the action is
21e20 20 6e 6f 74 20 69 6e 20 74 68 65 20 74 61 62 6c   not in the tabl
21e21 65 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 79 79  e.** and that yy
21e22 5f 64 65 66 61 75 6c 74 5b 53 5d 20 73 68 6f 75  _default[S] shou
21e23 6c 64 20 62 65 20 75 73 65 64 20 69 6e 73 74 65  ld be used inste
21e24 61 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ad.  .**.** The 
21e25 66 6f 72 6d 75 6c 61 20 61 62 6f 76 65 20 69 73  formula above is
21e26 20 66 6f 72 20 63 6f 6d 70 75 74 69 6e 67 20 74   for computing t
21e27 68 65 20 61 63 74 69 6f 6e 20 77 68 65 6e 20 74  he action when t
21e28 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 0a  he lookahead is.
21e29 2a 2a 20 61 20 74 65 72 6d 69 6e 61 6c 20 73 79  ** a terminal sy
21e2a 6d 62 6f 6c 2e 20 20 49 66 20 74 68 65 20 6c 6f  mbol.  If the lo
21e2b 6f 6b 61 68 65 61 64 20 69 73 20 61 20 6e 6f 6e  okahead is a non
21e2c 2d 74 65 72 6d 69 6e 61 6c 20 28 61 73 20 6f 63  -terminal (as oc
21e2d 63 75 72 73 20 61 66 74 65 72 0a 2a 2a 20 61 20  curs after.** a 
21e2e 72 65 64 75 63 65 20 61 63 74 69 6f 6e 29 20 74  reduce action) t
21e2f 68 65 6e 20 74 68 65 20 79 79 5f 72 65 64 75 63  hen the yy_reduc
21e30 65 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20 69  e_ofst[] array i
21e31 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20  s used in place 
21e32 6f 66 0a 2a 2a 20 74 68 65 20 79 79 5f 73 68 69  of.** the yy_shi
21e33 66 74 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20  ft_ofst[] array 
21e34 61 6e 64 20 59 59 5f 52 45 44 55 43 45 5f 55 53  and YY_REDUCE_US
21e35 45 5f 44 46 4c 54 20 69 73 20 75 73 65 64 20 69  E_DFLT is used i
21e36 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 59 59  n place of.** YY
21e37 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2e  _SHIFT_USE_DFLT.
21e38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
21e39 77 69 6e 67 20 61 72 65 20 74 68 65 20 74 61 62  wing are the tab
21e3a 6c 65 73 20 67 65 6e 65 72 61 74 65 64 20 69 6e  les generated in
21e3b 20 74 68 69 73 20 73 65 63 74 69 6f 6e 3a 0a 2a   this section:.*
21e3c 2a 0a 2a 2a 20 20 79 79 5f 61 63 74 69 6f 6e 5b  *.**  yy_action[
21e3d 5d 20 20 20 20 20 20 20 20 41 20 73 69 6e 67 6c  ]        A singl
21e3e 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  e table containi
21e3f 6e 67 20 61 6c 6c 20 61 63 74 69 6f 6e 73 2e 0a  ng all actions..
21e40 2a 2a 20 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64  **  yy_lookahead
21e41 5b 5d 20 20 20 20 20 41 20 74 61 62 6c 65 20 63  []     A table c
21e42 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6c 6f  ontaining the lo
21e43 6f 6b 61 68 65 61 64 20 66 6f 72 20 65 61 63 68  okahead for each
21e44 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 20 20 20   entry in.**    
21e45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e46 20 79 79 5f 61 63 74 69 6f 6e 2e 20 20 55 73 65   yy_action.  Use
21e47 64 20 74 6f 20 64 65 74 65 63 74 20 68 61 73 68  d to detect hash
21e48 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 2a 2a 20   collisions..** 
21e49 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d   yy_shift_ofst[]
21e4a 20 20 20 20 46 6f 72 20 65 61 63 68 20 73 74 61      For each sta
21e4b 74 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69  te, the offset i
21e4c 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f  nto yy_action fo
21e4d 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
21e4e 20 20 20 20 20 20 20 20 20 73 68 69 66 74 69 6e           shiftin
21e4f 67 20 74 65 72 6d 69 6e 61 6c 73 2e 0a 2a 2a 20  g terminals..** 
21e50 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b   yy_reduce_ofst[
21e51 5d 20 20 20 46 6f 72 20 65 61 63 68 20 73 74 61  ]   For each sta
21e52 74 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69  te, the offset i
21e53 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f  nto yy_action fo
21e54 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
21e55 20 20 20 20 20 20 20 20 20 73 68 69 66 74 69 6e           shiftin
21e56 67 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 73 20  g non-terminals 
21e57 61 66 74 65 72 20 61 20 72 65 64 75 63 65 2e 0a  after a reduce..
21e58 2a 2a 20 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d  **  yy_default[]
21e59 20 20 20 20 20 20 20 44 65 66 61 75 6c 74 20 61         Default a
21e5a 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 73  ction for each s
21e5b 74 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tate..*/.static 
21e5c 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59  const YYACTIONTY
21e5d 50 45 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 3d  PE yy_action[] =
21e5e 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20   {. /*     0 */ 
21e5f 20 20 32 39 30 2c 20 20 38 39 38 2c 20 20 31 32    290,  898,  12
21e60 32 2c 20 20 35 38 35 2c 20 20 34 30 37 2c 20 20  2,  585,  407,  
21e61 31 37 30 2c 20 20 20 20 32 2c 20 20 34 33 37 2c  170,    2,  437,
21e62 20 20 20 36 31 2c 20 20 20 36 31 2c 0a 20 2f 2a     61,   61,. /*
21e63 20 20 20 20 31 30 20 2a 2f 20 20 20 20 36 31 2c      10 */    61,
21e64 20 20 20 36 31 2c 20 20 35 31 37 2c 20 20 20 36     61,  517,   6
21e65 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  3,   63,   63,  
21e66 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c   63,   64,   64,
21e67 20 20 20 36 35 2c 0a 20 2f 2a 20 20 20 20 32 30     65,. /*    20
21e68 20 2a 2f 20 20 20 20 36 35 2c 20 20 20 36 35 2c   */    65,   65,
21e69 20 20 20 36 36 2c 20 20 32 33 31 2c 20 20 34 34     66,  231,  44
21e6a 35 2c 20 20 32 30 39 2c 20 20 34 32 32 2c 20 20  5,  209,  422,  
21e6b 34 32 38 2c 20 20 20 36 38 2c 20 20 20 36 33 2c  428,   68,   63,
21e6c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20  . /*    30 */   
21e6d 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c   63,   63,   63,
21e6e 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
21e6f 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  5,   65,   65,  
21e70 20 36 36 2c 20 20 32 33 31 2c 0a 20 2f 2a 20 20   66,  231,. /*  
21e71 20 20 34 30 20 2a 2f 20 20 20 33 38 39 2c 20 20    40 */   389,  
21e72 33 38 36 2c 20 20 33 39 34 2c 20 20 34 34 39 2c  386,  394,  449,
21e73 20 20 20 36 30 2c 20 20 20 35 39 2c 20 20 32 39     60,   59,  29
21e74 35 2c 20 20 34 33 32 2c 20 20 34 33 33 2c 20 20  5,  432,  433,  
21e75 34 32 39 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a  429,. /*    50 *
21e76 2f 20 20 20 34 32 39 2c 20 20 20 36 32 2c 20 20  /   429,   62,  
21e77 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   62,   61,   61,
21e78 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 32 36     61,   61,  26
21e79 31 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 0a 20  1,   63,   63,. 
21e7a 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 20 36  /*    60 */    6
21e7b 33 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20  3,   63,   64,  
21e7c 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   64,   65,   65,
21e7d 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33     65,   66,  23
21e7e 31 2c 20 20 32 39 30 2c 0a 20 2f 2a 20 20 20 20  1,  290,. /*    
21e7f 37 30 20 2a 2f 20 20 20 34 39 31 2c 20 20 34 39  70 */   491,  49
21e80 32 2c 20 20 34 33 37 2c 20 20 34 38 37 2c 20 20  2,  437,  487,  
21e81 32 30 36 2c 20 20 20 38 31 2c 20 20 20 36 37 2c  206,   81,   67,
21e82 20 20 34 31 37 2c 20 20 20 36 39 2c 20 20 31 35    417,   69,  15
21e83 32 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20  2,. /*    80 */ 
21e84 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
21e85 33 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20  3,   63,   64,  
21e86 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   64,   65,   65,
21e87 20 20 20 36 35 2c 20 20 20 36 36 2c 0a 20 2f 2a     65,   66,. /*
21e88 20 20 20 20 39 30 20 2a 2f 20 20 20 32 33 31 2c      90 */   231,
21e89 20 20 20 36 37 2c 20 20 34 36 30 2c 20 20 20 36     67,  460,   6
21e8a 39 2c 20 20 31 35 32 2c 20 20 34 32 32 2c 20 20  9,  152,  422,  
21e8b 34 32 38 2c 20 20 35 37 31 2c 20 20 32 36 32 2c  428,  571,  262,
21e8c 20 20 20 35 38 2c 0a 20 2f 2a 20 20 20 31 30 30     58,. /*   100
21e8d 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c   */    64,   64,
21e8e 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
21e8f 35 2c 20 20 20 36 36 2c 20 20 32 33 31 2c 20 20  5,   66,  231,  
21e90 33 39 35 2c 20 20 33 39 36 2c 20 20 34 31 39 2c  395,  396,  419,
21e91 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20  . /*   110 */   
21e92 34 31 39 2c 20 20 34 31 39 2c 20 20 32 39 30 2c  419,  419,  290,
21e93 20 20 20 36 30 2c 20 20 20 35 39 2c 20 20 32 39     60,   59,  29
21e94 35 2c 20 20 34 33 32 2c 20 20 34 33 33 2c 20 20  5,  432,  433,  
21e95 34 32 39 2c 20 20 34 32 39 2c 0a 20 2f 2a 20 20  429,  429,. /*  
21e96 20 31 32 30 20 2a 2f 20 20 20 20 36 32 2c 20 20   120 */    62,  
21e97 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   62,   61,   61,
21e98 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 33 31     61,   61,  31
21e99 35 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  5,   63,   63,  
21e9a 20 36 33 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a   63,. /*   130 *
21e9b 2f 20 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20  /    63,   64,  
21e9c 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   64,   65,   65,
21e9d 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33     65,   66,  23
21e9e 31 2c 20 20 34 32 32 2c 20 20 34 32 38 2c 0a 20  1,  422,  428,. 
21e9f 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 39  /*   140 */    9
21ea0 33 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  3,   65,   65,  
21ea1 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33 31 2c   65,   66,  231,
21ea2 20 20 33 39 34 2c 20 20 32 33 31 2c 20 20 34 31    394,  231,  41
21ea3 32 2c 20 20 20 33 34 2c 0a 20 2f 2a 20 20 20 31  2,   34,. /*   1
21ea4 35 30 20 2a 2f 20 20 20 20 35 36 2c 20 20 32 39  50 */    56,  29
21ea5 36 2c 20 20 34 34 30 2c 20 20 34 34 31 2c 20 20  6,  440,  441,  
21ea6 34 30 38 2c 20 20 34 38 36 2c 20 20 20 36 30 2c  408,  486,   60,
21ea7 20 20 20 35 39 2c 20 20 32 39 35 2c 20 20 34 33     59,  295,  43
21ea8 32 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20  2,. /*   160 */ 
21ea9 20 20 34 33 33 2c 20 20 34 32 39 2c 20 20 34 32    433,  429,  42
21eaa 39 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  9,   62,   62,  
21eab 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
21eac 20 20 20 36 31 2c 20 20 34 38 38 2c 0a 20 2f 2a     61,  488,. /*
21ead 20 20 20 31 37 30 20 2a 2f 20 20 20 20 36 33 2c     170 */    63,
21eae 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
21eaf 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20  3,   64,   64,  
21eb0 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   65,   65,   65,
21eb1 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 31 38 30     66,. /*   180
21eb2 20 2a 2f 20 20 20 32 33 31 2c 20 20 32 39 30 2c   */   231,  290,
21eb3 20 20 32 35 35 2c 20 20 35 32 32 2c 20 20 32 39    255,  522,  29
21eb4 33 2c 20 20 35 36 39 2c 20 20 31 31 32 2c 20 20  3,  569,  112,  
21eb5 34 30 36 2c 20 20 35 32 30 2c 20 20 34 34 39 2c  406,  520,  449,
21eb6 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20  . /*   190 */   
21eb7 33 32 39 2c 20 20 33 31 35 2c 20 20 34 30 35 2c  329,  315,  405,
21eb8 20 20 20 32 30 2c 20 20 34 33 37 2c 20 20 33 33     20,  437,  33
21eb9 38 2c 20 20 35 31 37 2c 20 20 33 39 34 2c 20 20  8,  517,  394,  
21eba 35 33 30 2c 20 20 35 32 39 2c 0a 20 2f 2a 20 20  530,  529,. /*  
21ebb 20 32 30 30 20 2a 2f 20 20 20 35 30 33 2c 20 20   200 */   503,  
21ebc 34 34 35 2c 20 20 32 30 39 2c 20 20 35 36 38 2c  445,  209,  568,
21ebd 20 20 35 36 37 2c 20 20 32 30 36 2c 20 20 35 32    567,  206,  52
21ebe 38 2c 20 20 34 32 32 2c 20 20 34 32 38 2c 20 20  8,  422,  428,  
21ebf 31 34 37 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a  147,. /*   210 *
21ec0 2f 20 20 20 31 34 38 2c 20 20 33 39 35 2c 20 20  /   148,  395,  
21ec1 33 39 36 2c 20 20 34 31 32 2c 20 20 20 34 31 2c  396,  412,   41,
21ec2 20 20 32 30 38 2c 20 20 31 34 39 2c 20 20 35 33    208,  149,  53
21ec3 31 2c 20 20 33 37 30 2c 20 20 34 38 37 2c 0a 20  1,  370,  487,. 
21ec4 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 32 35  /*   220 */   25
21ec5 39 2c 20 20 35 36 36 2c 20 20 32 35 37 2c 20 20  9,  566,  257,  
21ec6 34 31 37 2c 20 20 32 39 30 2c 20 20 20 36 30 2c  417,  290,   60,
21ec7 20 20 20 35 39 2c 20 20 32 39 35 2c 20 20 34 33     59,  295,  43
21ec8 32 2c 20 20 34 33 33 2c 0a 20 2f 2a 20 20 20 32  2,  433,. /*   2
21ec9 33 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 34 32  30 */   429,  42
21eca 39 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  9,   62,   62,  
21ecb 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
21ecc 20 20 20 36 31 2c 20 20 33 31 35 2c 20 20 20 36     61,  315,   6
21ecd 33 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20  3,. /*   240 */ 
21ece 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
21ecf 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20  3,   64,   64,  
21ed0 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   65,   65,   65,
21ed1 20 20 20 36 36 2c 20 20 32 33 31 2c 0a 20 2f 2a     66,  231,. /*
21ed2 20 20 20 32 35 30 20 2a 2f 20 20 20 34 32 32 2c     250 */   422,
21ed3 20 20 34 32 38 2c 20 20 34 34 35 2c 20 20 33 33    428,  445,  33
21ed4 31 2c 20 20 32 31 32 2c 20 20 34 31 39 2c 20 20  1,  212,  419,  
21ed5 34 31 39 2c 20 20 34 31 39 2c 20 20 33 36 31 2c  419,  419,  361,
21ed6 20 20 34 33 37 2c 0a 20 2f 2a 20 20 20 32 36 30    437,. /*   260
21ed7 20 2a 2f 20 20 20 34 31 32 2c 20 20 20 34 31 2c   */   412,   41,
21ed8 20 20 33 39 35 2c 20 20 33 39 36 2c 20 20 33 36    395,  396,  36
21ed9 34 2c 20 20 35 36 35 2c 20 20 32 30 38 2c 20 20  4,  565,  208,  
21eda 32 39 30 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  290,   60,   59,
21edb 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20  . /*   270 */   
21edc 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33 33 2c  295,  432,  433,
21edd 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20 20 36    429,  429,   6
21ede 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20  2,   62,   61,  
21edf 20 36 31 2c 20 20 20 36 31 2c 0a 20 2f 2a 20 20   61,   61,. /*  
21ee0 20 32 38 30 20 2a 2f 20 20 20 20 36 31 2c 20 20   280 */    61,  
21ee1 33 39 34 2c 20 20 20 36 33 2c 20 20 20 36 33 2c  394,   63,   63,
21ee2 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
21ee3 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
21ee4 20 36 35 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a   65,. /*   290 *
21ee5 2f 20 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  /    65,   66,  
21ee6 32 33 31 2c 20 20 34 32 32 2c 20 20 34 32 38 2c  231,  422,  428,
21ee7 20 20 34 38 39 2c 20 20 32 39 38 2c 20 20 35 32    489,  298,  52
21ee8 32 2c 20 20 34 37 32 2c 20 20 20 36 36 2c 0a 20  2,  472,   66,. 
21ee9 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 32 33  /*   300 */   23
21eea 31 2c 20 20 32 31 31 2c 20 20 34 37 32 2c 20 20  1,  211,  472,  
21eeb 32 32 34 2c 20 20 34 30 39 2c 20 20 32 38 34 2c  224,  409,  284,
21eec 20 20 35 33 32 2c 20 20 20 32 30 2c 20 20 34 34    532,   20,  44
21eed 37 2c 20 20 35 32 31 2c 0a 20 2f 2a 20 20 20 33  7,  521,. /*   3
21eee 31 30 20 2a 2f 20 20 20 31 36 36 2c 20 20 20 36  10 */   166,   6
21eef 30 2c 20 20 20 35 39 2c 20 20 32 39 35 2c 20 20  0,   59,  295,  
21ef0 34 33 32 2c 20 20 34 33 33 2c 20 20 34 32 39 2c  432,  433,  429,
21ef1 20 20 34 32 39 2c 20 20 20 36 32 2c 20 20 20 36    429,   62,   6
21ef2 32 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20  2,. /*   320 */ 
21ef3 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36     61,   61,   6
21ef4 31 2c 20 20 20 36 31 2c 20 20 34 37 32 2c 20 20  1,   61,  472,  
21ef5 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c   63,   63,   63,
21ef6 20 20 20 36 33 2c 20 20 20 36 34 2c 0a 20 2f 2a     63,   64,. /*
21ef7 20 20 20 33 33 30 20 2a 2f 20 20 20 20 36 34 2c     330 */    64,
21ef8 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
21ef9 35 2c 20 20 20 36 36 2c 20 20 32 33 31 2c 20 20  5,   66,  231,  
21efa 32 30 37 2c 20 20 34 37 38 2c 20 20 33 31 35 2c  207,  478,  315,
21efb 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20 33 34 30     76,. /*   340
21efc 20 2a 2f 20 20 20 32 39 30 2c 20 20 32 33 35 2c   */   290,  235,
21efd 20 20 32 39 38 2c 20 20 20 35 35 2c 20 20 34 38    298,   55,  48
21efe 32 2c 20 20 32 32 35 2c 20 20 33 39 35 2c 20 20  2,  225,  395,  
21eff 33 39 36 2c 20 20 31 37 39 2c 20 20 35 34 35 2c  396,  179,  545,
21f00 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20  . /*   350 */   
21f01 34 39 32 2c 20 20 33 34 33 2c 20 20 33 34 36 2c  492,  343,  346,
21f02 20 20 33 34 37 2c 20 20 20 36 37 2c 20 20 31 35    347,   67,  15
21f03 30 2c 20 20 20 36 39 2c 20 20 31 35 32 2c 20 20  0,   69,  152,  
21f04 33 33 37 2c 20 20 35 32 32 2c 0a 20 2f 2a 20 20  337,  522,. /*  
21f05 20 33 36 30 20 2a 2f 20 20 20 34 31 32 2c 20 20   360 */   412,  
21f06 20 33 35 2c 20 20 33 34 38 2c 20 20 32 33 37 2c   35,  348,  237,
21f07 20 20 32 34 39 2c 20 20 33 36 38 2c 20 20 34 32    249,  368,  42
21f08 32 2c 20 20 34 32 38 2c 20 20 35 37 36 2c 20 20  2,  428,  576,  
21f09 20 32 30 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a   20,. /*   370 *
21f0a 2f 20 20 20 31 36 32 2c 20 20 31 31 36 2c 20 20  /   162,  116,  
21f0b 32 33 39 2c 20 20 33 34 31 2c 20 20 32 34 34 2c  239,  341,  244,
21f0c 20 20 33 34 32 2c 20 20 31 37 34 2c 20 20 33 32    342,  174,  32
21f0d 30 2c 20 20 34 34 30 2c 20 20 34 34 31 2c 0a 20  0,  440,  441,. 
21f0e 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 34 31  /*   380 */   41
21f0f 32 2c 20 20 20 20 33 2c 20 20 20 37 39 2c 20 20  2,    3,   79,  
21f10 32 35 30 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  250,   60,   59,
21f11 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
21f12 33 2c 20 20 34 32 39 2c 0a 20 2f 2a 20 20 20 33  3,  429,. /*   3
21f13 39 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 20 36  90 */   429,   6
21f14 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20  2,   62,   61,  
21f15 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
21f16 20 20 31 37 32 2c 20 20 20 36 33 2c 20 20 20 36    172,   63,   6
21f17 33 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20  3,. /*   400 */ 
21f18 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
21f19 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
21f1a 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
21f1b 20 20 32 33 31 2c 20 20 32 39 30 2c 0a 20 2f 2a    231,  290,. /*
21f1c 20 20 20 34 31 30 20 2a 2f 20 20 20 32 34 39 2c     410 */   249,
21f1d 20 20 35 34 38 2c 20 20 32 33 32 2c 20 20 34 38    548,  232,  48
21f1e 35 2c 20 20 35 30 38 2c 20 20 33 35 31 2c 20 20  5,  508,  351,  
21f1f 33 31 35 2c 20 20 31 31 36 2c 20 20 32 33 39 2c  315,  116,  239,
21f20 20 20 33 34 31 2c 0a 20 2f 2a 20 20 20 34 32 30    341,. /*   420
21f21 20 2a 2f 20 20 20 32 34 34 2c 20 20 33 34 32 2c   */   244,  342,
21f22 20 20 31 37 34 2c 20 20 31 37 39 2c 20 20 33 31    174,  179,  31
21f23 35 2c 20 20 35 32 33 2c 20 20 33 34 33 2c 20 20  5,  523,  343,  
21f24 33 34 36 2c 20 20 33 34 37 2c 20 20 32 35 30 2c  346,  347,  250,
21f25 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20  . /*   430 */   
21f26 32 31 38 2c 20 20 34 31 33 2c 20 20 31 35 33 2c  218,  413,  153,
21f27 20 20 34 36 32 2c 20 20 35 30 39 2c 20 20 34 32    462,  509,  42
21f28 32 2c 20 20 34 32 38 2c 20 20 33 34 38 2c 20 20  2,  428,  348,  
21f29 34 31 32 2c 20 20 20 33 34 2c 0a 20 2f 2a 20 20  412,   34,. /*  
21f2a 20 34 34 30 20 2a 2f 20 20 20 34 36 33 2c 20 20   440 */   463,  
21f2b 32 30 38 2c 20 20 31 37 35 2c 20 20 31 37 33 2c  208,  175,  173,
21f2c 20 20 31 35 38 2c 20 20 32 33 33 2c 20 20 34 31    158,  233,  41
21f2d 32 2c 20 20 20 33 34 2c 20 20 33 33 36 2c 20 20  2,   34,  336,  
21f2e 35 34 37 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a  547,. /*   450 *
21f2f 2f 20 20 20 34 34 37 2c 20 20 33 32 31 2c 20 20  /   447,  321,  
21f30 31 36 36 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  166,   60,   59,
21f31 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
21f32 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 0a 20  3,  429,  429,. 
21f33 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 20 36  /*   460 */    6
21f34 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20  2,   62,   61,  
21f35 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
21f36 20 20 34 31 33 2c 20 20 20 36 33 2c 20 20 20 36    413,   63,   6
21f37 33 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 34  3,   63,. /*   4
21f38 37 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20 36  70 */    63,   6
21f39 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
21f3a 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
21f3b 20 20 32 33 31 2c 20 20 32 39 30 2c 20 20 35 34    231,  290,  54
21f3c 30 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20  0,. /*   480 */ 
21f3d 20 20 33 33 33 2c 20 20 35 31 35 2c 20 20 35 30    333,  515,  50
21f3e 32 2c 20 20 35 33 39 2c 20 20 34 35 34 2c 20 20  2,  539,  454,  
21f3f 35 36 39 2c 20 20 33 30 30 2c 20 20 20 31 39 2c  569,  300,   19,
21f40 20 20 33 32 39 2c 20 20 31 34 32 2c 0a 20 2f 2a    329,  142,. /*
21f41 20 20 20 34 39 30 20 2a 2f 20 20 20 33 31 35 2c     490 */   315,
21f42 20 20 33 38 38 2c 20 20 33 31 35 2c 20 20 33 32    388,  315,  32
21f43 38 2c 20 20 20 20 32 2c 20 20 33 36 30 2c 20 20  8,    2,  360,  
21f44 34 35 35 2c 20 20 32 39 32 2c 20 20 34 38 31 2c  455,  292,  481,
21f45 20 20 33 37 31 2c 0a 20 2f 2a 20 20 20 35 30 30    371,. /*   500
21f46 20 2a 2f 20 20 20 32 36 37 2c 20 20 32 36 36 2c   */   267,  266,
21f47 20 20 32 35 30 2c 20 20 35 36 38 2c 20 20 34 32    250,  568,  42
21f48 32 2c 20 20 34 32 38 2c 20 20 35 38 36 2c 20 20  2,  428,  586,  
21f49 33 38 39 2c 20 20 33 38 36 2c 20 20 34 35 36 2c  389,  386,  456,
21f4a 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20  . /*   510 */   
21f4b 32 30 36 2c 20 20 34 39 33 2c 20 20 34 31 32 2c  206,  493,  412,
21f4c 20 20 20 34 39 2c 20 20 34 31 32 2c 20 20 20 34     49,  412,   4
21f4d 39 2c 20 20 33 30 31 2c 20 20 35 38 33 2c 20 20  9,  301,  583,  
21f4e 38 38 39 2c 20 20 31 35 37 2c 0a 20 2f 2a 20 20  889,  157,. /*  
21f4f 20 35 32 30 20 2a 2f 20 20 20 38 38 39 2c 20 20   520 */   889,  
21f50 34 39 34 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  494,   60,   59,
21f51 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
21f52 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20  3,  429,  429,  
21f53 20 36 32 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a   62,. /*   530 *
21f54 2f 20 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20  /    62,   61,  
21f55 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
21f56 20 20 31 39 39 2c 20 20 20 36 33 2c 20 20 20 36    199,   63,   6
21f57 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 0a 20  3,   63,   63,. 
21f58 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 20 36  /*   540 */    6
21f59 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
21f5a 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
21f5b 20 20 32 33 31 2c 20 20 32 39 30 2c 20 20 33 31    231,  290,  31
21f5c 35 2c 20 20 31 37 39 2c 0a 20 2f 2a 20 20 20 35  5,  179,. /*   5
21f5d 35 30 20 2a 2f 20 20 20 34 33 36 2c 20 20 32 35  50 */   436,  25
21f5e 33 2c 20 20 33 34 33 2c 20 20 33 34 36 2c 20 20  3,  343,  346,  
21f5f 33 34 37 2c 20 20 33 36 38 2c 20 20 31 35 31 2c  347,  368,  151,
21f60 20 20 35 38 30 2c 20 20 33 30 36 2c 20 20 32 34    580,  306,  24
21f61 38 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20  8,. /*   560 */ 
21f62 20 20 33 30 37 2c 20 20 34 35 30 2c 20 20 20 37    307,  450,   7
21f63 35 2c 20 20 33 34 38 2c 20 20 20 37 37 2c 20 20  5,  348,   77,  
21f64 33 38 30 2c 20 20 32 30 38 2c 20 20 34 32 33 2c  380,  208,  423,
21f65 20 20 34 32 34 2c 20 20 34 31 33 2c 0a 20 2f 2a    424,  413,. /*
21f66 20 20 20 35 37 30 20 2a 2f 20 20 20 34 31 32 2c     570 */   412,
21f67 20 20 20 32 37 2c 20 20 33 31 37 2c 20 20 34 32     27,  317,  42
21f68 32 2c 20 20 34 32 38 2c 20 20 34 33 38 2c 20 20  2,  428,  438,  
21f69 20 20 31 2c 20 20 20 32 32 2c 20 20 35 38 33 2c    1,   22,  583,
21f6a 20 20 38 38 38 2c 0a 20 2f 2a 20 20 20 35 38 30    888,. /*   580
21f6b 20 2a 2f 20 20 20 33 39 34 2c 20 20 38 38 38 2c   */   394,  888,
21f6c 20 20 35 34 32 2c 20 20 34 37 36 2c 20 20 33 31    542,  476,  31
21f6d 38 2c 20 20 32 36 31 2c 20 20 34 33 35 2c 20 20  8,  261,  435,  
21f6e 34 33 35 2c 20 20 34 32 36 2c 20 20 34 32 37 2c  435,  426,  427,
21f6f 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20  . /*   590 */   
21f70 34 31 33 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  413,   60,   59,
21f71 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
21f72 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20  3,  429,  429,  
21f73 20 36 32 2c 20 20 20 36 32 2c 0a 20 2f 2a 20 20   62,   62,. /*  
21f74 20 36 30 30 20 2a 2f 20 20 20 20 36 31 2c 20 20   600 */    61,  
21f75 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
21f76 20 20 33 32 36 2c 20 20 20 36 33 2c 20 20 20 36    326,   63,   6
21f77 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  3,   63,   63,  
21f78 20 36 34 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a   64,. /*   610 *
21f79 2f 20 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  /    64,   65,  
21f7a 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
21f7b 20 20 32 33 31 2c 20 20 32 39 30 2c 20 20 34 32    231,  290,  42
21f7c 35 2c 20 20 35 38 30 2c 20 20 33 37 32 2c 0a 20  5,  580,  372,. 
21f7d 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 32 31  /*   620 */   21
21f7e 39 2c 20 20 20 39 32 2c 20 20 35 31 35 2c 20 20  9,   92,  515,  
21f7f 20 20 39 2c 20 20 33 33 34 2c 20 20 33 39 34 2c    9,  334,  394,
21f80 20 20 35 35 35 2c 20 20 33 39 34 2c 20 20 34 35    555,  394,  45
21f81 34 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20 20 36  4,   67,. /*   6
21f82 33 30 20 2a 2f 20 20 20 33 39 34 2c 20 20 20 36  30 */   394,   6
21f83 39 2c 20 20 31 35 32 2c 20 20 33 39 37 2c 20 20  9,  152,  397,  
21f84 33 39 38 2c 20 20 33 39 39 2c 20 20 33 31 38 2c  398,  399,  318,
21f85 20 20 32 33 34 2c 20 20 34 33 35 2c 20 20 34 33    234,  435,  43
21f86 35 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20  5,. /*   640 */ 
21f87 20 20 34 35 35 2c 20 20 33 31 36 2c 20 20 34 32    455,  316,  42
21f88 32 2c 20 20 34 32 38 2c 20 20 32 39 37 2c 20 20  2,  428,  297,  
21f89 33 39 35 2c 20 20 33 39 36 2c 20 20 33 31 38 2c  395,  396,  318,
21f8a 20 20 34 33 30 2c 20 20 34 33 35 2c 0a 20 2f 2a    430,  435,. /*
21f8b 20 20 20 36 35 30 20 2a 2f 20 20 20 34 33 35 2c     650 */   435,
21f8c 20 20 35 37 39 2c 20 20 32 38 39 2c 20 20 34 35    579,  289,  45
21f8d 36 2c 20 20 32 32 30 2c 20 20 33 32 35 2c 20 20  6,  220,  325,  
21f8e 20 20 35 2c 20 20 32 31 37 2c 20 20 35 34 34 2c    5,  217,  544,
21f8f 20 20 32 39 30 2c 0a 20 2f 2a 20 20 20 36 36 30    290,. /*   660
21f90 20 2a 2f 20 20 20 20 36 30 2c 20 20 20 35 39 2c   */    60,   59,
21f91 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
21f92 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20  3,  429,  429,  
21f93 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c   62,   62,   61,
21f94 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20  . /*   670 */   
21f95 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
21f96 20 20 33 39 33 2c 20 20 20 36 33 2c 20 20 20 36    393,   63,   6
21f97 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  3,   63,   63,  
21f98 20 36 34 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20   64,   64,. /*  
21f99 20 36 38 30 20 2a 2f 20 20 20 20 36 35 2c 20 20   680 */    65,  
21f9a 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
21f9b 20 20 32 33 31 2c 20 20 34 32 32 2c 20 20 34 32    231,  422,  42
21f9c 38 2c 20 20 34 38 30 2c 20 20 33 31 31 2c 20 20  8,  480,  311,  
21f9d 33 39 30 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a  390,. /*   690 *
21f9e 2f 20 20 20 33 39 35 2c 20 20 33 39 36 2c 20 20  /   395,  396,  
21f9f 33 39 35 2c 20 20 33 39 36 2c 20 20 32 30 35 2c  395,  396,  205,
21fa0 20 20 33 39 35 2c 20 20 33 39 36 2c 20 20 38 32    395,  396,  82
21fa1 31 2c 20 20 32 37 31 2c 20 20 35 31 35 2c 0a 20  1,  271,  515,. 
21fa2 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20 20 32 34  /*   700 */   24
21fa3 38 2c 20 20 31 39 38 2c 20 20 32 39 30 2c 20 20  8,  198,  290,  
21fa4 20 36 30 2c 20 20 20 35 39 2c 20 20 32 39 35 2c   60,   59,  295,
21fa5 20 20 34 33 32 2c 20 20 34 33 33 2c 20 20 34 32    432,  433,  42
21fa6 39 2c 20 20 34 32 39 2c 0a 20 2f 2a 20 20 20 37  9,  429,. /*   7
21fa7 31 30 20 2a 2f 20 20 20 20 36 32 2c 20 20 20 36  10 */    62,   6
21fa8 32 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20  2,   61,   61,  
21fa9 20 36 31 2c 20 20 20 36 31 2c 20 20 34 36 38 2c   61,   61,  468,
21faa 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
21fab 33 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20  3,. /*   720 */ 
21fac 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36     63,   64,   6
21fad 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  4,   65,   65,  
21fae 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33 31 2c   65,   66,  231,
21faf 20 20 34 32 32 2c 20 20 34 32 38 2c 0a 20 2f 2a    422,  428,. /*
21fb0 20 20 20 37 33 30 20 2a 2f 20 20 20 31 36 39 2c     730 */   169,
21fb1 20 20 31 35 38 2c 20 20 32 36 31 2c 20 20 32 36    158,  261,  26
21fb2 31 2c 20 20 33 30 32 2c 20 20 34 31 33 2c 20 20  1,  302,  413,  
21fb3 32 37 34 2c 20 20 31 31 37 2c 20 20 32 37 32 2c  274,  117,  272,
21fb4 20 20 32 36 31 2c 0a 20 2f 2a 20 20 20 37 34 30    261,. /*   740
21fb5 20 2a 2f 20 20 20 35 31 35 2c 20 20 35 31 35 2c   */   515,  515,
21fb6 20 20 32 36 31 2c 20 20 35 31 35 2c 20 20 31 39    261,  515,  19
21fb7 30 2c 20 20 32 39 30 2c 20 20 20 36 30 2c 20 20  0,  290,   60,  
21fb8 20 37 30 2c 20 20 32 39 35 2c 20 20 34 33 32 2c   70,  295,  432,
21fb9 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20  . /*   750 */   
21fba 34 33 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c  433,  429,  429,
21fbb 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36     62,   62,   6
21fbc 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20  1,   61,   61,  
21fbd 20 36 31 2c 20 20 33 37 37 2c 0a 20 2f 2a 20 20   61,  377,. /*  
21fbe 20 37 36 30 20 2a 2f 20 20 20 20 36 33 2c 20 20   760 */    63,  
21fbf 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c   63,   63,   63,
21fc0 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
21fc1 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  5,   65,   65,  
21fc2 20 36 36 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a   66,. /*   770 *
21fc3 2f 20 20 20 32 33 31 2c 20 20 34 32 32 2c 20 20  /   231,  422,  
21fc4 34 32 38 2c 20 20 33 38 32 2c 20 20 35 35 37 2c  428,  382,  557,
21fc5 20 20 33 30 33 2c 20 20 33 30 34 2c 20 20 32 34    303,  304,  24
21fc6 38 2c 20 20 34 31 33 2c 20 20 33 31 38 2c 0a 20  8,  413,  318,. 
21fc7 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 35 35  /*   780 */   55
21fc8 38 2c 20 20 34 33 35 2c 20 20 34 33 35 2c 20 20  8,  435,  435,  
21fc9 35 35 39 2c 20 20 35 33 38 2c 20 20 33 35 38 2c  559,  538,  358,
21fca 20 20 35 33 38 2c 20 20 33 38 35 2c 20 20 32 39    538,  385,  29
21fcb 30 2c 20 20 31 39 34 2c 0a 20 2f 2a 20 20 20 37  0,  194,. /*   7
21fcc 39 30 20 2a 2f 20 20 20 20 35 39 2c 20 20 32 39  90 */    59,  29
21fcd 35 2c 20 20 34 33 32 2c 20 20 34 33 33 2c 20 20  5,  432,  433,  
21fce 34 32 39 2c 20 20 34 32 39 2c 20 20 20 36 32 2c  429,  429,   62,
21fcf 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36     62,   61,   6
21fd0 31 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20  1,. /*   800 */ 
21fd1 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 33 36     61,   61,  36
21fd2 39 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  9,   63,   63,  
21fd3 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 34 2c   63,   63,   64,
21fd4 20 20 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f 2a     64,   65,. /*
21fd5 20 20 20 38 31 30 20 2a 2f 20 20 20 20 36 35 2c     810 */    65,
21fd6 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33     65,   66,  23
21fd7 31 2c 20 20 34 32 32 2c 20 20 34 32 38 2c 20 20  1,  422,  428,  
21fd8 33 39 34 2c 20 20 32 37 33 2c 20 20 32 34 38 2c  394,  273,  248,
21fd9 20 20 32 34 38 2c 0a 20 2f 2a 20 20 20 38 32 30    248,. /*   820
21fda 20 2a 2f 20 20 20 31 37 30 2c 20 20 32 34 36 2c   */   170,  246,
21fdb 20 20 34 33 37 2c 20 20 34 31 33 2c 20 20 33 38    437,  413,  38
21fdc 34 2c 20 20 33 36 35 2c 20 20 31 37 36 2c 20 20  4,  365,  176,  
21fdd 31 37 37 2c 20 20 31 37 38 2c 20 20 34 36 37 2c  177,  178,  467,
21fde 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20  . /*   830 */   
21fdf 33 30 39 2c 20 20 31 32 31 2c 20 20 31 35 34 2c  309,  121,  154,
21fe0 20 20 31 32 36 2c 20 20 32 39 35 2c 20 20 34 33    126,  295,  43
21fe1 32 2c 20 20 34 33 33 2c 20 20 34 32 39 2c 20 20  2,  433,  429,  
21fe2 34 32 39 2c 20 20 20 36 32 2c 0a 20 2f 2a 20 20  429,   62,. /*  
21fe3 20 38 34 30 20 2a 2f 20 20 20 20 36 32 2c 20 20   840 */    62,  
21fe4 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
21fe5 20 20 20 36 31 2c 20 20 33 31 35 2c 20 20 20 36     61,  315,   6
21fe6 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  3,   63,   63,  
21fe7 20 36 33 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a   63,. /*   850 *
21fe8 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20  /    64,   64,  
21fe9 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   65,   65,   65,
21fea 20 20 20 36 36 2c 20 20 32 33 31 2c 20 20 20 37     66,  231,   7
21feb 32 2c 20 20 33 32 32 2c 20 20 31 37 35 2c 0a 20  2,  322,  175,. 
21fec 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20 20 20 20  /*   860 */     
21fed 34 2c 20 20 33 31 35 2c 20 20 32 36 31 2c 20 20  4,  315,  261,  
21fee 33 31 35 2c 20 20 32 39 34 2c 20 20 32 36 31 2c  315,  294,  261,
21fef 20 20 34 31 33 2c 20 20 34 31 32 2c 20 20 20 32    413,  412,   2
21ff0 38 2c 20 20 33 31 35 2c 0a 20 2f 2a 20 20 20 38  8,  315,. /*   8
21ff1 37 30 20 2a 2f 20 20 20 32 36 31 2c 20 20 33 31  70 */   261,  31
21ff2 35 2c 20 20 33 31 39 2c 20 20 20 37 32 2c 20 20  5,  319,   72,  
21ff3 33 32 32 2c 20 20 33 31 35 2c 20 20 20 20 34 2c  322,  315,    4,
21ff4 20 20 34 31 38 2c 20 20 34 34 33 2c 20 20 34 34    418,  443,  44
21ff5 33 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20  3,. /*   880 */ 
21ff6 20 20 32 39 34 2c 20 20 33 39 35 2c 20 20 33 39    294,  395,  39
21ff7 36 2c 20 20 34 31 32 2c 20 20 20 32 33 2c 20 20  6,  412,   23,  
21ff8 34 31 32 2c 20 20 20 33 32 2c 20 20 34 33 37 2c  412,   32,  437,
21ff9 20 20 33 31 39 2c 20 20 33 32 34 2c 0a 20 2f 2a    319,  324,. /*
21ffa 20 20 20 38 39 30 20 2a 2f 20 20 20 33 32 37 2c     890 */   327,
21ffb 20 20 34 31 32 2c 20 20 20 35 33 2c 20 20 34 31    412,   53,  41
21ffc 32 2c 20 20 20 35 32 2c 20 20 33 31 35 2c 20 20  2,   52,  315,  
21ffd 31 35 36 2c 20 20 34 31 32 2c 20 20 20 39 37 2c  156,  412,   97,
21ffe 20 20 34 34 39 2c 0a 20 2f 2a 20 20 20 39 30 30    449,. /*   900
21fff 20 2a 2f 20 20 20 33 31 35 2c 20 20 31 39 32 2c   */   315,  192,
22000 20 20 33 31 35 2c 20 20 32 37 35 2c 20 20 33 31    315,  275,  31
22001 35 2c 20 20 33 32 34 2c 20 20 33 37 36 2c 20 20  5,  324,  376,  
22002 34 36 39 2c 20 20 35 30 30 2c 20 20 33 31 35 2c  469,  500,  315,
22003 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20  . /*   910 */   
22004 34 37 36 2c 20 20 32 37 37 2c 20 20 34 37 36 2c  476,  277,  476,
22005 20 20 31 36 33 2c 20 20 32 39 32 2c 20 20 34 34    163,  292,  44
22006 39 2c 20 20 33 31 35 2c 20 20 34 31 32 2c 20 20  9,  315,  412,  
22007 20 39 35 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20   95,   74,. /*  
22008 20 39 32 30 20 2a 2f 20 20 20 20 37 33 2c 20 20   920 */    73,  
22009 34 36 37 2c 20 20 34 31 32 2c 20 20 31 30 30 2c  467,  412,  100,
2200a 20 20 34 31 32 2c 20 20 31 30 31 2c 20 20 34 31    412,  101,  41
2200b 32 2c 20 20 31 31 31 2c 20 20 20 37 32 2c 20 20  2,  111,   72,  
2200c 33 31 33 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a  313,. /*   930 *
2200d 2f 20 20 20 33 31 34 2c 20 20 34 31 32 2c 20 20  /   314,  412,  
2200e 31 31 33 2c 20 20 34 31 37 2c 20 20 34 34 36 2c  113,  417,  446,
2200f 20 20 20 37 34 2c 20 20 20 37 33 2c 20 20 34 37     74,   73,  47
22010 39 2c 20 20 34 31 32 2c 20 20 20 31 36 2c 0a 20  9,  412,   16,. 
22011 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 33 37  /*   940 */   37
22012 39 2c 20 20 33 31 35 2c 20 20 31 38 31 2c 20 20  9,  315,  181,  
22013 34 36 35 2c 20 20 20 37 32 2c 20 20 33 31 33 2c  465,   72,  313,
22014 20 20 33 31 34 2c 20 20 20 37 32 2c 20 20 33 32    314,   72,  32
22015 32 2c 20 20 34 31 37 2c 0a 20 2f 2a 20 20 20 39  2,  417,. /*   9
22016 35 30 20 2a 2f 20 20 20 20 20 34 2c 20 20 32 30  50 */     4,  20
22017 36 2c 20 20 33 31 35 2c 20 20 31 38 34 2c 20 20  6,  315,  184,  
22018 32 39 34 2c 20 20 33 31 35 2c 20 20 34 39 37 2c  294,  315,  497,
22019 20 20 34 39 38 2c 20 20 34 37 34 2c 20 20 32 30    498,  474,  20
2201a 36 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20  6,. /*   960 */ 
2201b 20 20 31 37 31 2c 20 20 33 33 39 2c 20 20 33 31    171,  339,  31
2201c 39 2c 20 20 34 31 32 2c 20 20 20 39 38 2c 20 20  9,  412,   98,  
2201d 34 31 39 2c 20 20 34 31 39 2c 20 20 34 31 39 2c  419,  419,  419,
2201e 20 20 34 32 30 2c 20 20 34 32 31 2c 0a 20 2f 2a    420,  421,. /*
2201f 20 20 20 39 37 30 20 2a 2f 20 20 20 20 31 31 2c     970 */    11,
22020 20 20 33 35 39 2c 20 20 33 37 38 2c 20 20 33 30    359,  378,  30
22021 35 2c 20 20 34 31 32 2c 20 20 20 33 33 2c 20 20  5,  412,   33,  
22022 34 31 33 2c 20 20 34 31 32 2c 20 20 20 39 36 2c  413,  412,   96,
22023 20 20 33 32 34 2c 0a 20 2f 2a 20 20 20 39 38 30    324,. /*   980
22024 20 2a 2f 20 20 20 34 35 38 2c 20 20 34 31 39 2c   */   458,  419,
22025 20 20 34 31 39 2c 20 20 34 31 39 2c 20 20 34 32    419,  419,  42
22026 30 2c 20 20 34 32 31 2c 20 20 20 31 31 2c 20 20  0,  421,   11,  
22027 34 31 33 2c 20 20 34 31 31 2c 20 20 34 34 39 2c  413,  411,  449,
22028 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20  . /*   990 */   
22029 34 31 31 2c 20 20 31 36 30 2c 20 20 34 31 30 2c  411,  160,  410,
2202a 20 20 33 31 35 2c 20 20 34 31 30 2c 20 20 34 36    315,  410,  46
2202b 36 2c 20 20 32 32 31 2c 20 20 32 32 32 2c 20 20  6,  221,  222,  
2202c 32 32 33 2c 20 20 31 30 33 2c 0a 20 2f 2a 20 20  223,  103,. /*  
2202d 31 30 30 30 20 2a 2f 20 20 20 20 38 33 2c 20 20  1000 */    83,  
2202e 34 37 31 2c 20 20 33 31 35 2c 20 20 35 30 37 2c  471,  315,  507,
2202f 20 20 35 30 36 2c 20 20 33 31 35 2c 20 20 36 32    506,  315,  62
22030 30 2c 20 20 34 37 35 2c 20 20 33 31 35 2c 20 20  0,  475,  315,  
22031 20 37 34 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a   74,. /*  1010 *
22032 2f 20 20 20 20 37 33 2c 20 20 32 34 35 2c 20 20  /    73,  245,  
22033 32 30 33 2c 20 20 20 32 31 2c 20 20 32 37 39 2c  203,   21,  279,
22034 20 20 34 31 32 2c 20 20 20 32 34 2c 20 20 34 33    412,   24,  43
22035 37 2c 20 20 20 37 32 2c 20 20 33 31 33 2c 0a 20  7,   72,  313,. 
22036 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20 20 33 31  /*  1020 */   31
22037 34 2c 20 20 32 38 30 2c 20 20 33 31 35 2c 20 20  4,  280,  315,  
22038 34 31 37 2c 20 20 34 31 32 2c 20 20 20 35 34 2c  417,  412,   54,
22039 20 20 35 30 35 2c 20 20 34 31 32 2c 20 20 31 31    505,  412,  11
2203a 34 2c 20 20 33 31 35 2c 0a 20 2f 2a 20 20 31 30  4,  315,. /*  10
2203b 33 30 20 2a 2f 20 20 20 34 31 32 2c 20 20 31 31  30 */   412,  11
2203c 35 2c 20 20 35 30 34 2c 20 20 32 30 31 2c 20 20  5,  504,  201,  
2203d 31 34 35 2c 20 20 35 34 37 2c 20 20 32 34 30 2c  145,  547,  240,
2203e 20 20 35 31 30 2c 20 20 35 32 34 2c 20 20 32 30    510,  524,  20
2203f 30 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20  0,. /*  1040 */ 
22040 20 20 33 31 35 2c 20 20 35 31 31 2c 20 20 32 30    315,  511,  20
22041 32 2c 20 20 33 31 35 2c 20 20 34 31 32 2c 20 20  2,  315,  412,  
22042 20 32 35 2c 20 20 33 31 35 2c 20 20 32 34 31 2c   25,  315,  241,
22043 20 20 33 31 35 2c 20 20 20 31 38 2c 0a 20 2f 2a    315,   18,. /*
22044 20 20 31 30 35 30 20 2a 2f 20 20 20 33 31 35 2c    1050 */   315,
22045 20 20 34 31 32 2c 20 20 20 33 36 2c 20 20 33 31    412,   36,  31
22046 35 2c 20 20 32 35 34 2c 20 20 34 31 39 2c 20 20  5,  254,  419,  
22047 34 31 39 2c 20 20 34 31 39 2c 20 20 34 32 30 2c  419,  419,  420,
22048 20 20 34 32 31 2c 0a 20 2f 2a 20 20 31 30 36 30    421,. /*  1060
22049 20 2a 2f 20 20 20 20 31 31 2c 20 20 32 35 36 2c   */    11,  256,
2204a 20 20 34 31 32 2c 20 20 20 33 37 2c 20 20 32 35    412,   37,  25
2204b 38 2c 20 20 34 31 32 2c 20 20 20 32 36 2c 20 20  8,  412,   26,  
2204c 33 31 35 2c 20 20 34 31 32 2c 20 20 20 33 38 2c  315,  412,   38,
2204d 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20  . /*  1070 */   
2204e 34 31 32 2c 20 20 20 33 39 2c 20 20 34 31 32 2c  412,   39,  412,
2204f 20 20 20 34 30 2c 20 20 32 36 30 2c 20 20 34 31     40,  260,  41
22050 32 2c 20 20 20 34 32 2c 20 20 33 31 35 2c 20 20  2,   42,  315,  
22051 35 31 32 2c 20 20 33 31 35 2c 0a 20 2f 2a 20 20  512,  315,. /*  
22052 31 30 38 30 20 2a 2f 20 20 20 31 32 36 2c 20 20  1080 */   126,  
22053 33 31 35 2c 20 20 34 33 37 2c 20 20 33 31 35 2c  315,  437,  315,
22054 20 20 31 38 37 2c 20 20 33 37 35 2c 20 20 32 37    187,  375,  27
22055 36 2c 20 20 32 36 36 2c 20 20 32 36 35 2c 20 20  6,  266,  265,  
22056 34 31 32 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a  412,. /*  1090 *
22057 2f 20 20 20 20 34 33 2c 20 20 32 39 31 2c 20 20  /    43,  291,  
22058 33 31 35 2c 20 20 32 35 32 2c 20 20 33 31 35 2c  315,  252,  315,
22059 20 20 31 32 36 2c 20 20 32 30 36 2c 20 20 35 38    126,  206,  58
2205a 31 2c 20 20 20 20 38 2c 20 20 34 31 32 2c 0a 20  1,    8,  412,. 
2205b 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 20 34  /*  1100 */    4
2205c 34 2c 20 20 34 31 32 2c 20 20 20 32 39 2c 20 20  4,  412,   29,  
2205d 34 31 32 2c 20 20 20 33 30 2c 20 20 34 31 32 2c  412,   30,  412,
2205e 20 20 20 34 35 2c 20 20 33 35 30 2c 20 20 33 36     45,  350,  36
2205f 33 2c 20 20 31 32 36 2c 0a 20 2f 2a 20 20 31 31  3,  126,. /*  11
22060 31 30 20 2a 2f 20 20 20 33 31 35 2c 20 20 35 34  10 */   315,  54
22061 33 2c 20 20 33 31 35 2c 20 20 31 32 36 2c 20 20  3,  315,  126,  
22062 34 31 32 2c 20 20 20 34 36 2c 20 20 34 31 32 2c  412,   46,  412,
22063 20 20 20 34 37 2c 20 20 33 31 35 2c 20 20 33 35     47,  315,  35
22064 35 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20  5,. /*  1120 */ 
22065 20 20 33 38 31 2c 20 20 35 35 31 2c 20 20 33 31    381,  551,  31
22066 35 2c 20 20 31 37 31 2c 20 20 35 35 32 2c 20 20  5,  171,  552,  
22067 33 31 35 2c 20 20 20 39 30 2c 20 20 35 36 32 2c  315,   90,  562,
22068 20 20 35 37 38 2c 20 20 20 39 30 2c 0a 20 2f 2a    578,   90,. /*
22069 20 20 31 31 33 30 20 2a 2f 20 20 20 32 38 38 2c    1130 */   288,
2206a 20 20 33 36 36 2c 20 20 34 31 32 2c 20 20 20 34    366,  412,   4
2206b 38 2c 20 20 34 31 32 2c 20 20 20 33 31 2c 20 20  8,  412,   31,  
2206c 35 38 32 2c 20 20 33 36 37 2c 20 20 32 36 38 2c  582,  367,  268,
2206d 20 20 32 36 39 2c 0a 20 2f 2a 20 20 31 31 34 30    269,. /*  1140
2206e 20 2a 2f 20 20 20 34 31 32 2c 20 20 20 31 30 2c   */   412,   10,
2206f 20 20 32 37 30 2c 20 20 35 35 34 2c 20 20 34 31    270,  554,  41
22070 32 2c 20 20 20 35 30 2c 20 20 35 36 34 2c 20 20  2,   50,  564,  
22071 34 31 32 2c 20 20 20 35 31 2c 20 20 32 37 38 2c  412,   51,  278,
22072 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20  . /*  1150 */   
22073 32 38 31 2c 20 20 32 38 32 2c 20 20 35 37 35 2c  281,  282,  575,
22074 20 20 31 34 34 2c 20 20 34 34 32 2c 20 20 34 30    144,  442,  40
22075 33 2c 20 20 33 32 33 2c 20 20 32 32 36 2c 20 20  3,  323,  226,  
22076 34 34 34 2c 20 20 34 36 31 2c 0a 20 2f 2a 20 20  444,  461,. /*  
22077 31 31 36 30 20 2a 2f 20 20 20 34 36 34 2c 20 20  1160 */   464,  
22078 32 34 32 2c 20 20 35 30 33 2c 20 20 35 35 30 2c  242,  503,  550,
22079 20 20 35 36 31 2c 20 20 35 31 33 2c 20 20 31 36    561,  513,  16
2207a 31 2c 20 20 33 39 32 2c 20 20 34 30 30 2c 20 20  1,  392,  400,  
2207b 35 31 36 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a  516,. /*  1170 *
2207c 2f 20 20 20 34 30 31 2c 20 20 33 34 35 2c 20 20  /   401,  345,  
2207d 34 30 32 2c 20 20 20 20 37 2c 20 20 33 31 32 2c  402,    7,  312,
2207e 20 20 20 38 33 2c 20 20 32 32 37 2c 20 20 33 33     83,  227,  33
2207f 32 2c 20 20 32 32 38 2c 20 20 20 38 32 2c 0a 20  2,  228,   82,. 
22080 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20 20 33 33  /*  1180 */   33
22081 30 2c 20 20 20 35 37 2c 20 20 34 30 38 2c 20 20  0,   57,  408,  
22082 34 31 36 2c 20 20 31 36 38 2c 20 20 20 37 38 2c  416,  168,   78,
22083 20 20 34 35 39 2c 20 20 31 32 33 2c 20 20 32 31    459,  123,  21
22084 30 2c 20 20 34 31 34 2c 0a 20 2f 2a 20 20 31 31  0,  414,. /*  11
22085 39 30 20 2a 2f 20 20 20 20 38 34 2c 20 20 33 33  90 */    84,  33
22086 35 2c 20 20 33 34 30 2c 20 20 32 39 39 2c 20 20  5,  340,  299,  
22087 34 39 36 2c 20 20 35 30 30 2c 20 20 34 39 30 2c  496,  500,  490,
22088 20 20 32 32 39 2c 20 20 34 39 35 2c 20 20 32 34    229,  495,  24
22089 33 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20  3,. /*  1200 */ 
2208a 20 20 31 30 34 2c 20 20 32 34 37 2c 20 20 34 39    104,  247,  49
2208b 39 2c 20 20 35 30 31 2c 20 20 32 33 30 2c 20 20  9,  501,  230,  
2208c 32 38 35 2c 20 20 34 31 35 2c 20 20 32 31 35 2c  285,  415,  215,
2208d 20 20 35 31 34 2c 20 20 35 31 38 2c 0a 20 2f 2a    514,  518,. /*
2208e 20 20 31 32 31 30 20 2a 2f 20 20 20 35 32 35 2c    1210 */   525,
2208f 20 20 35 32 36 2c 20 20 35 31 39 2c 20 20 32 33    526,  519,  23
22090 36 2c 20 20 35 32 37 2c 20 20 34 37 33 2c 20 20  6,  527,  473,  
22091 32 33 38 2c 20 20 33 35 32 2c 20 20 34 37 37 2c  238,  352,  477,
22092 20 20 32 38 36 2c 0a 20 2f 2a 20 20 31 32 32 30    286,. /*  1220
22093 20 2a 2f 20 20 20 31 38 32 2c 20 20 33 35 36 2c   */   182,  356,
22094 20 20 35 33 33 2c 20 20 33 35 34 2c 20 20 31 31    533,  354,  11
22095 39 2c 20 20 31 38 33 2c 20 20 31 38 35 2c 20 20  9,  183,  185,  
22096 20 38 37 2c 20 20 35 34 36 2c 20 20 31 33 30 2c   87,  546,  130,
22097 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20  . /*  1230 */   
22098 31 38 36 2c 20 20 35 33 35 2c 20 20 31 38 38 2c  186,  535,  188,
22099 20 20 31 34 30 2c 20 20 33 36 32 2c 20 20 31 39    140,  362,  19
2209a 31 2c 20 20 35 35 33 2c 20 20 32 31 36 2c 20 20  1,  553,  216,  
2209b 33 37 33 2c 20 20 33 37 34 2c 0a 20 2f 2a 20 20  373,  374,. /*  
2209c 31 32 34 30 20 2a 2f 20 20 20 31 33 31 2c 20 20  1240 */   131,  
2209d 35 36 30 2c 20 20 33 30 38 2c 20 20 31 33 32 2c  560,  308,  132,
2209e 20 20 31 33 33 2c 20 20 35 37 32 2c 20 20 35 37    133,  572,  57
2209f 37 2c 20 20 31 33 36 2c 20 20 32 36 33 2c 20 20  7,  136,  263,  
220a0 31 33 34 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a  134,. /*  1250 *
220a1 2f 20 20 20 31 33 39 2c 20 20 35 33 36 2c 20 20  /   139,  536,  
220a2 35 37 33 2c 20 20 33 39 31 2c 20 20 20 39 31 2c  573,  391,   91,
220a3 20 20 20 39 34 2c 20 20 34 30 34 2c 20 20 35 37     94,  404,  57
220a4 34 2c 20 20 20 39 39 2c 20 20 32 31 34 2c 0a 20  4,   99,  214,. 
220a5 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 31 30  /*  1260 */   10
220a6 32 2c 20 20 36 32 31 2c 20 20 36 32 32 2c 20 20  2,  621,  622,  
220a7 34 33 31 2c 20 20 31 36 34 2c 20 20 34 33 34 2c  431,  164,  434,
220a8 20 20 31 36 35 2c 20 20 20 37 31 2c 20 20 31 34    165,   71,  14
220a9 31 2c 20 20 20 31 37 2c 0a 20 2f 2a 20 20 31 32  1,   17,. /*  12
220aa 37 30 20 2a 2f 20 20 20 34 33 39 2c 20 20 34 34  70 */   439,  44
220ab 38 2c 20 20 34 35 31 2c 20 20 31 35 35 2c 20 20  8,  451,  155,  
220ac 20 20 36 2c 20 20 34 35 32 2c 20 20 34 37 30 2c    6,  452,  470,
220ad 20 20 31 31 30 2c 20 20 31 36 37 2c 20 20 34 35    110,  167,  45
220ae 33 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20  3,. /*  1280 */ 
220af 20 20 34 35 37 2c 20 20 31 32 34 2c 20 20 20 31    457,  124,   1
220b0 33 2c 20 20 32 31 33 2c 20 20 31 32 30 2c 20 20  3,  213,  120,  
220b1 20 38 30 2c 20 20 20 31 32 2c 20 20 31 32 35 2c   80,   12,  125,
220b2 20 20 31 35 39 2c 20 20 34 38 33 2c 0a 20 2f 2a    159,  483,. /*
220b3 20 20 31 32 39 30 20 2a 2f 20 20 20 34 38 34 2c    1290 */   484,
220b4 20 20 20 38 35 2c 20 20 33 31 30 2c 20 20 31 30     85,  310,  10
220b5 35 2c 20 20 31 38 30 2c 20 20 32 35 31 2c 20 20  5,  180,  251,  
220b6 31 30 36 2c 20 20 31 31 38 2c 20 20 20 38 36 2c  106,  118,   86,
220b7 20 20 31 30 37 2c 0a 20 2f 2a 20 20 31 33 30 30    107,. /*  1300
220b8 20 2a 2f 20 20 20 32 34 31 2c 20 20 33 34 34 2c   */   241,  344,
220b9 20 20 33 34 39 2c 20 20 33 35 33 2c 20 20 31 34    349,  353,  14
220ba 33 2c 20 20 35 33 34 2c 20 20 31 32 37 2c 20 20  3,  534,  127,  
220bb 33 35 37 2c 20 20 31 37 31 2c 20 20 31 38 39 2c  357,  171,  189,
220bc 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20  . /*  1310 */   
220bd 32 36 34 2c 20 20 31 30 38 2c 20 20 32 38 37 2c  264,  108,  287,
220be 20 20 31 32 38 2c 20 20 35 34 39 2c 20 20 31 32    128,  549,  12
220bf 39 2c 20 20 31 39 33 2c 20 20 35 33 37 2c 20 20  9,  193,  537,  
220c0 35 34 31 2c 20 20 20 31 34 2c 0a 20 2f 2a 20 20  541,   14,. /*  
220c1 31 33 32 30 20 2a 2f 20 20 20 31 39 35 2c 20 20  1320 */   195,  
220c2 20 38 38 2c 20 20 31 39 36 2c 20 20 35 35 36 2c   88,  196,  556,
220c3 20 20 31 39 37 2c 20 20 31 33 37 2c 20 20 31 33    197,  137,  13
220c4 38 2c 20 20 31 33 35 2c 20 20 20 31 35 2c 20 20  8,  135,   15,  
220c5 35 36 33 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a  563,. /*  1330 *
220c6 2f 20 20 20 35 37 30 2c 20 20 31 30 39 2c 20 20  /   570,  109,  
220c7 32 38 33 2c 20 20 31 34 36 2c 20 20 32 30 34 2c  283,  146,  204,
220c8 20 20 33 38 33 2c 20 20 33 38 37 2c 20 20 38 39    383,  387,  89
220c9 39 2c 20 20 35 38 34 2c 20 20 38 39 39 2c 0a 20  9,  584,  899,. 
220ca 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20 20 38 39  /*  1340 */   89
220cb 39 2c 20 20 38 39 39 2c 20 20 38 39 39 2c 20 20  9,  899,  899,  
220cc 38 39 39 2c 20 20 20 38 39 2c 0a 7d 3b 0a 73 74  899,   89,.};.st
220cd 61 74 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44  atic const YYCOD
220ce 45 54 59 50 45 20 79 79 5f 6c 6f 6f 6b 61 68 65  ETYPE yy_lookahe
220cf 61 64 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20  ad[] = {. /*    
220d0 20 30 20 2a 2f 20 20 20 20 31 36 2c 20 20 31 33   0 */    16,  13
220d1 39 2c 20 20 31 34 30 2c 20 20 31 34 31 2c 20 20  9,  140,  141,  
220d2 31 36 38 2c 20 20 20 32 31 2c 20 20 31 34 34 2c  168,   21,  144,
220d3 20 20 20 32 33 2c 20 20 20 36 39 2c 20 20 20 37     23,   69,   7
220d4 30 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20  0,. /*    10 */ 
220d5 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 37     71,   72,  17
220d6 36 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  6,   74,   75,  
220d7 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
220d8 20 20 20 37 39 2c 20 20 20 38 30 2c 0a 20 2f 2a     79,   80,. /*
220d9 20 20 20 20 32 30 20 2a 2f 20 20 20 20 38 31 2c      20 */    81,
220da 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
220db 34 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  4,   78,   79,  
220dc 20 34 32 2c 20 20 20 34 33 2c 20 20 20 37 33 2c   42,   43,   73,
220dd 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 20 33 30     74,. /*    30
220de 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c   */    75,   76,
220df 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
220e0 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  9,   80,   81,  
220e1 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
220e2 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20  . /*    40 */   
220e3 20 20 31 2c 20 20 20 20 32 2c 20 20 20 32 33 2c    1,    2,   23,
220e4 20 20 20 35 38 2c 20 20 20 36 30 2c 20 20 20 36     58,   60,   6
220e5 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20  1,   62,   63,  
220e6 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f 2a 20 20   64,   65,. /*  
220e7 20 20 35 30 20 2a 2f 20 20 20 20 36 36 2c 20 20    50 */    66,  
220e8 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
220e9 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
220ea 32 2c 20 20 31 34 37 2c 20 20 20 37 34 2c 20 20  2,  147,   74,  
220eb 20 37 35 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a   75,. /*    60 *
220ec 2f 20 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  /    76,   77,  
220ed 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
220ee 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
220ef 33 2c 20 20 20 38 34 2c 20 20 20 31 36 2c 0a 20  3,   84,   16,. 
220f0 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 31 38  /*    70 */   18
220f1 35 2c 20 20 31 38 36 2c 20 20 20 38 38 2c 20 20  5,  186,   88,  
220f2 20 38 38 2c 20 20 31 31 30 2c 20 20 20 32 32 2c   88,  110,   22,
220f3 20 20 32 31 37 2c 20 20 20 39 32 2c 20 20 32 31    217,   92,  21
220f4 39 2c 20 20 32 32 30 2c 0a 20 2f 2a 20 20 20 20  9,  220,. /*    
220f5 38 30 20 2a 2f 20 20 20 20 37 34 2c 20 20 20 37  80 */    74,   7
220f6 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
220f7 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
220f8 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
220f9 33 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20  3,. /*    90 */ 
220fa 20 20 20 38 34 2c 20 20 32 31 37 2c 20 20 32 31     84,  217,  21
220fb 38 2c 20 20 32 31 39 2c 20 20 32 32 30 2c 20 20  8,  219,  220,  
220fc 20 34 32 2c 20 20 20 34 33 2c 20 20 32 33 38 2c   42,   43,  238,
220fd 20 20 31 38 38 2c 20 20 20 34 36 2c 0a 20 2f 2a    188,   46,. /*
220fe 20 20 20 31 30 30 20 2a 2f 20 20 20 20 37 38 2c     100 */    78,
220ff 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38     79,   80,   8
22100 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
22101 20 38 34 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   84,   88,   89,
22102 20 20 31 32 34 2c 0a 20 2f 2a 20 20 20 31 31 30    124,. /*   110
22103 20 2a 2f 20 20 20 31 32 35 2c 20 20 31 32 36 2c   */   125,  126,
22104 20 20 20 31 36 2c 20 20 20 36 30 2c 20 20 20 36     16,   60,   6
22105 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20  1,   62,   63,  
22106 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   64,   65,   66,
22107 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20  . /*   120 */   
22108 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
22109 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2210a 32 2c 20 20 31 34 37 2c 20 20 20 37 34 2c 20 20  2,  147,   74,  
2210b 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20   75,   76,. /*  
2210c 20 31 33 30 20 2a 2f 20 20 20 20 37 37 2c 20 20   130 */    77,  
2210d 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
2210e 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2210f 33 2c 20 20 20 38 34 2c 20 20 20 34 32 2c 20 20  3,   84,   42,  
22110 20 34 33 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a   43,. /*   140 *
22111 2f 20 20 20 20 34 34 2c 20 20 20 38 30 2c 20 20  /    44,   80,  
22112 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
22113 20 20 20 38 34 2c 20 20 20 32 33 2c 20 20 20 38     84,   23,   8
22114 34 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 0a 20  4,  169,  170,. 
22115 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 20 31  /*   150 */    1
22116 39 2c 20 20 31 36 34 2c 20 20 31 36 35 2c 20 20  9,  164,  165,  
22117 31 36 36 2c 20 20 20 32 33 2c 20 20 31 36 39 2c  166,   23,  169,
22118 20 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36     60,   61,   6
22119 32 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 31  2,   63,. /*   1
2211a 36 30 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36  60 */    64,   6
2211b 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  5,   66,   67,  
2211c 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
2211d 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 36     71,   72,  16
2211e 39 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20  9,. /*   170 */ 
2211f 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
22120 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
22121 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   79,   80,   81,
22122 20 20 20 38 32 2c 20 20 20 38 33 2c 0a 20 2f 2a     82,   83,. /*
22123 20 20 20 31 38 30 20 2a 2f 20 20 20 20 38 34 2c     180 */    84,
22124 20 20 20 31 36 2c 20 20 20 31 34 2c 20 20 31 34     16,   14,  14
22125 37 2c 20 20 31 35 30 2c 20 20 31 34 37 2c 20 20  7,  150,  147,  
22126 20 32 31 2c 20 20 31 36 37 2c 20 20 31 36 38 2c   21,  167,  168,
22127 20 20 20 35 38 2c 0a 20 2f 2a 20 20 20 31 39 30     58,. /*   190
22128 20 2a 2f 20 20 20 32 31 31 2c 20 20 31 34 37 2c   */   211,  147,
22129 20 20 31 35 36 2c 20 20 31 35 37 2c 20 20 20 32    156,  157,   2
2212a 33 2c 20 20 32 31 36 2c 20 20 31 37 36 2c 20 20  3,  216,  176,  
2212b 20 32 33 2c 20 20 31 38 31 2c 20 20 31 37 36 2c   23,  181,  176,
2212c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20  . /*   200 */   
2212d 31 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c  177,   78,   79,
2212e 20 20 31 36 35 2c 20 20 31 36 36 2c 20 20 31 31    165,  166,  11
2212f 30 2c 20 20 31 38 33 2c 20 20 20 34 32 2c 20 20  0,  183,   42,  
22130 20 34 33 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20   43,   78,. /*  
22131 20 32 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20   210 */    79,  
22132 20 38 38 2c 20 20 20 38 39 2c 20 20 31 36 39 2c   88,   89,  169,
22133 20 20 31 37 30 2c 20 20 32 32 38 2c 20 20 31 38    170,  228,  18
22134 30 2c 20 20 31 38 31 2c 20 20 31 32 33 2c 20 20  0,  181,  123,  
22135 20 38 38 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a   88,. /*   220 *
22136 2f 20 20 20 20 35 32 2c 20 20 20 39 38 2c 20 20  /    52,   98,  
22137 20 35 34 2c 20 20 20 39 32 2c 20 20 20 31 36 2c   54,   92,   16,
22138 20 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36     60,   61,   6
22139 32 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 0a 20  2,   63,   64,. 
2213a 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 20 36  /*   230 */    6
2213b 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  5,   66,   67,  
2213c 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
2213d 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 34     71,   72,  14
2213e 37 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 32  7,   74,. /*   2
2213f 34 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37  40 */    75,   7
22140 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
22141 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   79,   80,   81,
22142 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
22143 34 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20  4,. /*   250 */ 
22144 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 20 37     42,   43,   7
22145 38 2c 20 20 32 30 39 2c 20 20 32 31 30 2c 20 20  8,  209,  210,  
22146 31 32 34 2c 20 20 31 32 35 2c 20 20 31 32 36 2c  124,  125,  126,
22147 20 20 32 32 34 2c 20 20 20 38 38 2c 0a 20 2f 2a    224,   88,. /*
22148 20 20 20 32 36 30 20 2a 2f 20 20 20 31 36 39 2c     260 */   169,
22149 20 20 31 37 30 2c 20 20 20 38 38 2c 20 20 20 38    170,   88,   8
2214a 39 2c 20 20 32 33 30 2c 20 20 32 32 37 2c 20 20  9,  230,  227,  
2214b 32 32 38 2c 20 20 20 31 36 2c 20 20 20 36 30 2c  228,   16,   60,
2214c 20 20 20 36 31 2c 0a 20 2f 2a 20 20 20 32 37 30     61,. /*   270
2214d 20 2a 2f 20 20 20 20 36 32 2c 20 20 20 36 33 2c   */    62,   63,
2214e 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36     64,   65,   6
2214f 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  6,   67,   68,  
22150 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
22151 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20  . /*   280 */   
22152 20 37 32 2c 20 20 20 32 33 2c 20 20 20 37 34 2c   72,   23,   74,
22153 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
22154 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
22155 20 38 30 2c 20 20 20 38 31 2c 0a 20 2f 2a 20 20   80,   81,. /*  
22156 20 32 39 30 20 2a 2f 20 20 20 20 38 32 2c 20 20   290 */    82,  
22157 20 38 33 2c 20 20 20 38 34 2c 20 20 20 34 32 2c   83,   84,   42,
22158 20 20 20 34 33 2c 20 20 31 36 30 2c 20 20 20 31     43,  160,   1
22159 36 2c 20 20 31 34 37 2c 20 20 31 36 31 2c 20 20  6,  147,  161,  
2215a 20 38 33 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a   83,. /*   300 *
2215b 2f 20 20 20 20 38 34 2c 20 20 32 31 30 2c 20 20  /    84,  210,  
2215c 31 36 31 2c 20 20 31 35 33 2c 20 20 31 36 39 2c  161,  153,  169,
2215d 20 20 31 35 38 2c 20 20 31 35 36 2c 20 20 31 35    158,  156,  15
2215e 37 2c 20 20 31 36 31 2c 20 20 31 36 32 2c 0a 20  7,  161,  162,. 
2215f 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 31 36  /*   310 */   16
22160 33 2c 20 20 20 36 30 2c 20 20 20 36 31 2c 20 20  3,   60,   61,  
22161 20 36 32 2c 20 20 20 36 33 2c 20 20 20 36 34 2c   62,   63,   64,
22162 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36     65,   66,   6
22163 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 33  7,   68,. /*   3
22164 32 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37  20 */    69,   7
22165 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
22166 31 36 31 2c 20 20 20 37 34 2c 20 20 20 37 35 2c  161,   74,   75,
22167 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
22168 38 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20  8,. /*   330 */ 
22169 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38     79,   80,   8
2216a 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
2216b 20 38 34 2c 20 20 31 39 32 2c 20 20 32 30 30 2c   84,  192,  200,
2216c 20 20 31 34 37 2c 20 20 31 33 31 2c 0a 20 2f 2a    147,  131,. /*
2216d 20 20 20 33 34 30 20 2a 2f 20 20 20 20 31 36 2c     340 */    16,
2216e 20 20 32 30 30 2c 20 20 20 31 36 2c 20 20 31 39    200,   16,  19
2216f 39 2c 20 20 20 32 30 2c 20 20 31 39 30 2c 20 20  9,   20,  190,  
22170 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
22171 20 20 31 38 35 2c 0a 20 2f 2a 20 20 20 33 35 30    185,. /*   350
22172 20 2a 2f 20 20 20 31 38 36 2c 20 20 20 39 33 2c   */   186,   93,
22173 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 32 31     94,   95,  21
22174 37 2c 20 20 20 32 32 2c 20 20 32 31 39 2c 20 20  7,   22,  219,  
22175 32 32 30 2c 20 20 31 34 37 2c 20 20 31 34 37 2c  220,  147,  147,
22176 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20  . /*   360 */   
22177 31 36 39 2c 20 20 31 37 30 2c 20 20 31 30 34 2c  169,  170,  104,
22178 20 20 32 30 30 2c 20 20 20 38 34 2c 20 20 31 34    200,   84,  14
22179 37 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  7,   42,   43,  
2217a 31 35 36 2c 20 20 31 35 37 2c 0a 20 2f 2a 20 20  156,  157,. /*  
2217b 20 33 37 30 20 2a 2f 20 20 20 20 39 30 2c 20 20   370 */    90,  
2217c 20 39 31 2c 20 20 20 39 32 2c 20 20 20 39 33 2c   91,   92,   93,
2217d 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20 39     94,   95,   9
2217e 36 2c 20 20 31 36 34 2c 20 20 31 36 35 2c 20 20  6,  164,  165,  
2217f 31 36 36 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a  166,. /*   380 *
22180 2f 20 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  /   169,  170,  
22181 31 33 31 2c 20 20 31 30 33 2c 20 20 20 36 30 2c  131,  103,   60,
22182 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
22183 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 0a 20  3,   64,   65,. 
22184 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 20 36  /*   390 */    6
22185 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  6,   67,   68,  
22186 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
22187 20 20 20 37 32 2c 20 20 31 35 35 2c 20 20 20 37     72,  155,   7
22188 34 2c 20 20 20 37 35 2c 0a 20 2f 2a 20 20 20 34  4,   75,. /*   4
22189 30 30 20 2a 2f 20 20 20 20 37 36 2c 20 20 20 37  00 */    76,   7
2218a 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
2218b 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
2218c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31     83,   84,   1
2218d 36 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20  6,. /*   410 */ 
2218e 20 20 20 38 34 2c 20 20 20 31 31 2c 20 20 32 32     84,   11,  22
2218f 31 2c 20 20 20 32 30 2c 20 20 20 33 30 2c 20 20  1,   20,   30,  
22190 20 31 36 2c 20 20 31 34 37 2c 20 20 20 39 31 2c   16,  147,   91,
22191 20 20 20 39 32 2c 20 20 20 39 33 2c 0a 20 2f 2a     92,   93,. /*
22192 20 20 20 34 32 30 20 2a 2f 20 20 20 20 39 34 2c     420 */    94,
22193 20 20 20 39 35 2c 20 20 20 39 36 2c 20 20 20 39     95,   96,   9
22194 30 2c 20 20 31 34 37 2c 20 20 31 38 31 2c 20 20  0,  147,  181,  
22195 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c   93,   94,   95,
22196 20 20 31 30 33 2c 0a 20 2f 2a 20 20 20 34 33 30    103,. /*   430
22197 20 2a 2f 20 20 20 32 31 32 2c 20 20 31 38 39 2c   */   212,  189,
22198 20 20 31 35 35 2c 20 20 20 32 37 2c 20 20 20 35    155,   27,   5
22199 30 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  0,   42,   43,  
2219a 31 30 34 2c 20 20 31 36 39 2c 20 20 31 37 30 2c  104,  169,  170,
2219b 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20  . /*   440 */   
2219c 20 33 34 2c 20 20 32 32 38 2c 20 20 20 34 33 2c   34,  228,   43,
2219d 20 20 32 30 31 2c 20 20 32 30 32 2c 20 20 31 34    201,  202,  14
2219e 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  7,  169,  170,  
2219f 32 30 36 2c 20 20 20 34 39 2c 0a 20 2f 2a 20 20  206,   49,. /*  
221a0 20 34 35 30 20 2a 2f 20 20 20 31 36 31 2c 20 20   450 */   161,  
221a1 31 36 32 2c 20 20 31 36 33 2c 20 20 20 36 30 2c  162,  163,   60,
221a2 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
221a3 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  3,   64,   65,  
221a4 20 36 36 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a   66,. /*   460 *
221a5 2f 20 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  /    67,   68,  
221a6 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
221a7 20 20 20 37 32 2c 20 20 31 38 39 2c 20 20 20 37     72,  189,   7
221a8 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20  4,   75,   76,. 
221a9 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20 20 37  /*   470 */    7
221aa 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
221ab 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
221ac 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31     83,   84,   1
221ad 36 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20 34  6,   25,. /*   4
221ae 38 30 20 2a 2f 20 20 20 32 31 31 2c 20 20 31 34  80 */   211,  14
221af 37 2c 20 20 20 32 30 2c 20 20 20 32 39 2c 20 20  7,   20,   29,  
221b0 20 31 32 2c 20 20 31 34 37 2c 20 20 31 30 32 2c   12,  147,  102,
221b1 20 20 20 31 39 2c 20 20 32 31 31 2c 20 20 20 32     19,  211,   2
221b2 31 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20  1,. /*   490 */ 
221b3 20 20 31 34 37 2c 20 20 31 34 31 2c 20 20 31 34    147,  141,  14
221b4 37 2c 20 20 32 31 36 2c 20 20 31 34 34 2c 20 20  7,  216,  144,  
221b5 20 34 31 2c 20 20 20 32 34 2c 20 20 20 39 38 2c   41,   24,   98,
221b6 20 20 20 32 30 2c 20 20 20 39 39 2c 0a 20 2f 2a     20,   99,. /*
221b7 20 20 20 35 30 30 20 2a 2f 20 20 20 31 30 30 2c     500 */   100,
221b8 20 20 31 30 31 2c 20 20 31 30 33 2c 20 20 31 36    101,  103,  16
221b9 35 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  5,   42,   43,  
221ba 20 20 30 2c 20 20 20 20 31 2c 20 20 20 20 32 2c    0,    1,    2,
221bb 20 20 20 33 37 2c 0a 20 2f 2a 20 20 20 35 31 30     37,. /*   510
221bc 20 2a 2f 20 20 20 31 31 30 2c 20 20 20 33 39 2c   */   110,   39,
221bd 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 36    169,  170,  16
221be 39 2c 20 20 31 37 30 2c 20 20 31 38 32 2c 20 20  9,  170,  182,  
221bf 20 31 39 2c 20 20 20 32 30 2c 20 20 31 34 37 2c   19,   20,  147,
221c0 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20  . /*   520 */   
221c1 20 32 32 2c 20 20 20 34 39 2c 20 20 20 36 30 2c   22,   49,   60,
221c2 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
221c3 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  3,   64,   65,  
221c4 20 36 36 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20   66,   67,. /*  
221c5 20 35 33 30 20 2a 2f 20 20 20 20 36 38 2c 20 20   530 */    68,  
221c6 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
221c7 20 20 20 37 32 2c 20 20 31 35 35 2c 20 20 20 37     72,  155,   7
221c8 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
221c9 20 37 37 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a   77,. /*   540 *
221ca 2f 20 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  /    78,   79,  
221cb 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
221cc 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31     83,   84,   1
221cd 36 2c 20 20 31 34 37 2c 20 20 20 39 30 2c 0a 20  6,  147,   90,. 
221ce 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20 20 32  /*   550 */    2
221cf 30 2c 20 20 20 32 30 2c 20 20 20 39 33 2c 20 20  0,   20,   93,  
221d0 20 39 34 2c 20 20 20 39 35 2c 20 20 31 34 37 2c   94,   95,  147,
221d1 20 20 31 35 35 2c 20 20 20 35 39 2c 20 20 32 31    155,   59,  21
221d2 35 2c 20 20 32 32 35 2c 0a 20 2f 2a 20 20 20 35  5,  225,. /*   5
221d3 36 30 20 2a 2f 20 20 20 32 31 35 2c 20 20 20 32  60 */   215,   2
221d4 30 2c 20 20 31 33 30 2c 20 20 31 30 34 2c 20 20  0,  130,  104,  
221d5 31 33 32 2c 20 20 32 32 37 2c 20 20 32 32 38 2c  132,  227,  228,
221d6 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 31 38     42,   43,  18
221d7 39 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20  9,. /*   570 */ 
221d8 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 20 31    169,  170,   1
221d9 36 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  6,   42,   43,  
221da 20 32 30 2c 20 20 20 31 39 2c 20 20 20 32 32 2c   20,   19,   22,
221db 20 20 20 31 39 2c 20 20 20 32 30 2c 0a 20 2f 2a     19,   20,. /*
221dc 20 20 20 35 38 30 20 2a 2f 20 20 20 20 32 33 2c     580 */    23,
221dd 20 20 20 32 32 2c 20 20 20 31 38 2c 20 20 31 34     22,   18,  14
221de 37 2c 20 20 31 30 36 2c 20 20 31 34 37 2c 20 20  7,  106,  147,  
221df 31 30 38 2c 20 20 31 30 39 2c 20 20 20 36 33 2c  108,  109,   63,
221e0 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 35 39 30     64,. /*   590
221e1 20 2a 2f 20 20 20 31 38 39 2c 20 20 20 36 30 2c   */   189,   60,
221e2 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
221e3 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  3,   64,   65,  
221e4 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c   66,   67,   68,
221e5 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20  . /*   600 */   
221e6 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
221e7 20 20 20 37 32 2c 20 20 31 38 36 2c 20 20 20 37     72,  186,   7
221e8 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
221e9 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20   77,   78,. /*  
221ea 20 36 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20   610 */    79,  
221eb 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
221ec 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31     83,   84,   1
221ed 36 2c 20 20 20 39 32 2c 20 20 20 35 39 2c 20 20  6,   92,   59,  
221ee 20 35 35 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a   55,. /*   620 *
221ef 2f 20 20 20 32 31 32 2c 20 20 20 32 31 2c 20 20  /   212,   21,  
221f0 31 34 37 2c 20 20 20 31 39 2c 20 20 31 34 37 2c  147,   19,  147,
221f1 20 20 20 32 33 2c 20 20 31 38 38 2c 20 20 20 32     23,  188,   2
221f2 33 2c 20 20 20 31 32 2c 20 20 32 31 37 2c 0a 20  3,   12,  217,. 
221f3 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20 20 20 32  /*   630 */    2
221f4 33 2c 20 20 32 31 39 2c 20 20 32 32 30 2c 20 20  3,  219,  220,  
221f5 20 20 37 2c 20 20 20 20 38 2c 20 20 20 20 39 2c    7,    8,    9,
221f6 20 20 31 30 36 2c 20 20 31 34 37 2c 20 20 31 30    106,  147,  10
221f7 38 2c 20 20 31 30 39 2c 0a 20 2f 2a 20 20 20 36  8,  109,. /*   6
221f8 34 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 31 34  40 */    24,  14
221f9 37 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  7,   42,   43,  
221fa 32 30 38 2c 20 20 20 38 38 2c 20 20 20 38 39 2c  208,   88,   89,
221fb 20 20 31 30 36 2c 20 20 20 39 32 2c 20 20 31 30    106,   92,  10
221fc 38 2c 0a 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20  8,. /*   650 */ 
221fd 20 20 31 30 39 2c 20 20 32 34 34 2c 20 20 32 34    109,  244,  24
221fe 35 2c 20 20 20 33 37 2c 20 20 31 34 35 2c 20 20  5,   37,  145,  
221ff 20 33 39 2c 20 20 31 39 31 2c 20 20 31 38 32 2c   39,  191,  182,
22200 20 20 20 39 34 2c 20 20 20 31 36 2c 0a 20 2f 2a     94,   16,. /*
22201 20 20 20 36 36 30 20 2a 2f 20 20 20 20 36 30 2c     660 */    60,
22202 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
22203 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  3,   64,   65,  
22204 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c   66,   67,   68,
22205 20 20 20 36 39 2c 0a 20 2f 2a 20 20 20 36 37 30     69,. /*   670
22206 20 2a 2f 20 20 20 20 37 30 2c 20 20 20 37 31 2c   */    70,   71,
22207 20 20 20 37 32 2c 20 20 31 34 37 2c 20 20 20 37     72,  147,   7
22208 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
22209 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
2220a 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20 20  . /*   680 */   
2220b 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
2220c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 34     83,   84,   4
2220d 32 2c 20 20 20 34 33 2c 20 20 20 38 30 2c 20 20  2,   43,   80,  
2220e 31 34 32 2c 20 20 31 34 33 2c 0a 20 2f 2a 20 20  142,  143,. /*  
2220f 20 36 39 30 20 2a 2f 20 20 20 20 38 38 2c 20 20   690 */    88,  
22210 20 38 39 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   89,   88,   89,
22211 20 20 31 34 38 2c 20 20 20 38 38 2c 20 20 20 38    148,   88,   8
22212 39 2c 20 20 31 33 33 2c 20 20 20 31 34 2c 20 20  9,  133,   14,  
22213 31 34 37 2c 0a 20 2f 2a 20 20 20 37 30 30 20 2a  147,. /*   700 *
22214 2f 20 20 20 32 32 35 2c 20 20 31 35 35 2c 20 20  /   225,  155,  
22215 20 31 36 2c 20 20 20 36 30 2c 20 20 20 36 31 2c   16,   60,   61,
22216 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20 36     62,   63,   6
22217 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 0a 20  4,   65,   66,. 
22218 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20 20 20 36  /*   710 */    6
22219 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
2221a 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2221b 20 20 31 31 34 2c 20 20 20 37 34 2c 20 20 20 37    114,   74,   7
2221c 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20 37  5,   76,. /*   7
2221d 32 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37  20 */    77,   7
2221e 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  8,   79,   80,  
2221f 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
22220 20 20 20 38 34 2c 20 20 20 34 32 2c 20 20 20 34     84,   42,   4
22221 33 2c 0a 20 2f 2a 20 20 20 37 33 30 20 2a 2f 20  3,. /*   730 */ 
22222 20 20 32 30 31 2c 20 20 32 30 32 2c 20 20 31 34    201,  202,  14
22223 37 2c 20 20 31 34 37 2c 20 20 31 38 32 2c 20 20  7,  147,  182,  
22224 31 38 39 2c 20 20 20 35 32 2c 20 20 31 34 37 2c  189,   52,  147,
22225 20 20 20 35 34 2c 20 20 31 34 37 2c 0a 20 2f 2a     54,  147,. /*
22226 20 20 20 37 34 30 20 2a 2f 20 20 20 31 34 37 2c     740 */   147,
22227 20 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31 34    147,  147,  14
22228 37 2c 20 20 31 35 35 2c 20 20 20 31 36 2c 20 20  7,  155,   16,  
22229 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c   60,   61,   62,
2222a 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 37 35 30     63,. /*   750
2222b 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 35 2c   */    64,   65,
2222c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36     66,   67,   6
2222d 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  8,   69,   70,  
2222e 20 37 31 2c 20 20 20 37 32 2c 20 20 32 31 33 2c   71,   72,  213,
2222f 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f 20 20 20  . /*   760 */   
22230 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
22231 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
22232 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  9,   80,   81,  
22233 20 38 32 2c 20 20 20 38 33 2c 0a 20 2f 2a 20 20   82,   83,. /*  
22234 20 37 37 30 20 2a 2f 20 20 20 20 38 34 2c 20 20   770 */    84,  
22235 20 34 32 2c 20 20 20 34 33 2c 20 20 31 38 38 2c   42,   43,  188,
22236 20 20 31 38 38 2c 20 20 31 38 32 2c 20 20 31 38    188,  182,  18
22237 32 2c 20 20 32 32 35 2c 20 20 31 38 39 2c 20 20  2,  225,  189,  
22238 31 30 36 2c 0a 20 2f 2a 20 20 20 37 38 30 20 2a  106,. /*   780 *
22239 2f 20 20 20 31 38 38 2c 20 20 31 30 38 2c 20 20  /   188,  108,  
2223a 31 30 39 2c 20 20 31 38 38 2c 20 20 20 39 39 2c  109,  188,   99,
2223b 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 32 34    100,  101,  24
2223c 31 2c 20 20 20 31 36 2c 20 20 31 35 35 2c 0a 20  1,   16,  155,. 
2223d 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20 20 20 36  /*   790 */    6
2223e 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20  1,   62,   63,  
2223f 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   64,   65,   66,
22240 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36     67,   68,   6
22241 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 38  9,   70,. /*   8
22242 30 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37  00 */    71,   7
22243 32 2c 20 20 32 31 33 2c 20 20 20 37 34 2c 20 20  2,  213,   74,  
22244 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
22245 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
22246 30 2c 0a 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20  0,. /*   810 */ 
22247 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
22248 33 2c 20 20 20 38 34 2c 20 20 20 34 32 2c 20 20  3,   84,   42,  
22249 20 34 33 2c 20 20 20 32 33 2c 20 20 31 33 33 2c   43,   23,  133,
2224a 20 20 32 32 35 2c 20 20 32 32 35 2c 0a 20 2f 2a    225,  225,. /*
2224b 20 20 20 38 32 30 20 2a 2f 20 20 20 20 32 31 2c     820 */    21,
2224c 20 20 32 32 35 2c 20 20 20 32 33 2c 20 20 31 38    225,   23,  18
2224d 39 2c 20 20 32 33 39 2c 20 20 32 33 36 2c 20 20  9,  239,  236,  
2224e 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c   99,  100,  101,
2224f 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20 38 33 30     22,. /*   830
22250 20 2a 2f 20 20 20 32 34 32 2c 20 20 32 34 33 2c   */   242,  243,
22251 20 20 31 35 35 2c 20 20 20 32 32 2c 20 20 20 36    155,   22,   6
22252 32 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20  2,   63,   64,  
22253 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c   65,   66,   67,
22254 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20 20  . /*   840 */   
22255 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
22256 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 34     71,   72,  14
22257 37 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  7,   74,   75,  
22258 20 37 36 2c 20 20 20 37 37 2c 0a 20 2f 2a 20 20   76,   77,. /*  
22259 20 38 35 30 20 2a 2f 20 20 20 20 37 38 2c 20 20   850 */    78,  
2225a 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   79,   80,   81,
2225b 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
2225c 34 2c 20 20 20 31 36 2c 20 20 20 31 37 2c 20 20  4,   16,   17,  
2225d 20 34 33 2c 0a 20 2f 2a 20 20 20 38 36 30 20 2a   43,. /*   860 *
2225e 2f 20 20 20 20 31 39 2c 20 20 31 34 37 2c 20 20  /    19,  147,  
2225f 31 34 37 2c 20 20 31 34 37 2c 20 20 20 32 33 2c  147,  147,   23,
22260 20 20 31 34 37 2c 20 20 31 38 39 2c 20 20 31 36    147,  189,  16
22261 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 0a 20  9,  170,  147,. 
22262 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20 20 31 34  /*   870 */   14
22263 37 2c 20 20 31 34 37 2c 20 20 20 33 31 2c 20 20  7,  147,   31,  
22264 20 31 36 2c 20 20 20 31 37 2c 20 20 31 34 37 2c   16,   17,  147,
22265 20 20 20 31 39 2c 20 20 31 34 37 2c 20 20 31 32     19,  147,  12
22266 34 2c 20 20 31 32 35 2c 0a 20 2f 2a 20 20 20 38  4,  125,. /*   8
22267 38 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 20 38  80 */    23,   8
22268 38 2c 20 20 20 38 39 2c 20 20 31 36 39 2c 20 20  8,   89,  169,  
22269 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37 30 2c  170,  169,  170,
2226a 20 20 20 38 38 2c 20 20 20 33 31 2c 20 20 20 34     88,   31,   4
2226b 38 2c 0a 20 2f 2a 20 20 20 38 39 30 20 2a 2f 20  8,. /*   890 */ 
2226c 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20 31 37    147,  169,  17
2226d 30 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  0,  169,  170,  
2226e 31 34 37 2c 20 20 20 38 39 2c 20 20 31 36 39 2c  147,   89,  169,
2226f 20 20 31 37 30 2c 20 20 20 35 38 2c 0a 20 2f 2a    170,   58,. /*
22270 20 20 20 39 30 30 20 2a 2f 20 20 20 31 34 37 2c     900 */   147,
22271 20 20 20 32 32 2c 20 20 31 34 37 2c 20 20 31 38     22,  147,  18
22272 38 2c 20 20 31 34 37 2c 20 20 20 34 38 2c 20 20  8,  147,   48,  
22273 31 38 38 2c 20 20 31 31 34 2c 20 20 20 39 37 2c  188,  114,   97,
22274 20 20 31 34 37 2c 0a 20 2f 2a 20 20 20 39 31 30    147,. /*   910
22275 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 38 38 2c   */   147,  188,
22276 20 20 31 34 37 2c 20 20 20 31 39 2c 20 20 20 39    147,   19,   9
22277 38 2c 20 20 20 35 38 2c 20 20 31 34 37 2c 20 20  8,   58,  147,  
22278 31 36 39 2c 20 20 31 37 30 2c 20 20 20 37 38 2c  169,  170,   78,
22279 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f 20 20 20  . /*   920 */   
2227a 20 37 39 2c 20 20 31 31 34 2c 20 20 31 36 39 2c   79,  114,  169,
2227b 20 20 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37    170,  169,  17
2227c 30 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  0,  169,  170,  
2227d 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20   87,   88,. /*  
2227e 20 39 33 30 20 2a 2f 20 20 20 20 38 39 2c 20 20   930 */    89,  
2227f 31 36 39 2c 20 20 31 37 30 2c 20 20 20 39 32 2c  169,  170,   92,
22280 20 20 31 36 31 2c 20 20 20 37 38 2c 20 20 20 37    161,   78,   7
22281 39 2c 20 20 20 38 30 2c 20 20 31 36 39 2c 20 20  9,   80,  169,  
22282 31 37 30 2c 0a 20 2f 2a 20 20 20 39 34 30 20 2a  170,. /*   940 *
22283 2f 20 20 20 20 39 31 2c 20 20 31 34 37 2c 20 20  /    91,  147,  
22284 31 35 35 2c 20 20 20 32 32 2c 20 20 20 38 37 2c  155,   22,   87,
22285 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 31     88,   89,   1
22286 36 2c 20 20 20 31 37 2c 20 20 20 39 32 2c 0a 20  6,   17,   92,. 
22287 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20 20 20 31  /*   950 */    1
22288 39 2c 20 20 31 31 30 2c 20 20 31 34 37 2c 20 20  9,  110,  147,  
22289 31 35 35 2c 20 20 20 32 33 2c 20 20 31 34 37 2c  155,   23,  147,
2228a 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20 32      7,    8,   2
2228b 30 2c 20 20 31 31 30 2c 0a 20 2f 2a 20 20 20 39  0,  110,. /*   9
2228c 36 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 38  60 */    22,   8
2228d 30 2c 20 20 20 33 31 2c 20 20 31 36 39 2c 20 20  0,   31,  169,  
2228e 31 37 30 2c 20 20 31 32 34 2c 20 20 31 32 35 2c  170,  124,  125,
2228f 20 20 31 32 36 2c 20 20 31 32 37 2c 20 20 31 32    126,  127,  12
22290 38 2c 0a 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20  8,. /*   970 */ 
22291 20 20 31 32 39 2c 20 20 32 30 38 2c 20 20 31 32    129,  208,  12
22292 33 2c 20 20 32 30 38 2c 20 20 31 36 39 2c 20 20  3,  208,  169,  
22293 31 37 30 2c 20 20 31 38 39 2c 20 20 31 36 39 2c  170,  189,  169,
22294 20 20 31 37 30 2c 20 20 20 34 38 2c 0a 20 2f 2a    170,   48,. /*
22295 20 20 20 39 38 30 20 2a 2f 20 20 20 31 34 37 2c     980 */   147,
22296 20 20 31 32 34 2c 20 20 31 32 35 2c 20 20 31 32    124,  125,  12
22297 36 2c 20 20 31 32 37 2c 20 20 31 32 38 2c 20 20  6,  127,  128,  
22298 31 32 39 2c 20 20 31 38 39 2c 20 20 31 30 37 2c  129,  189,  107,
22299 20 20 20 35 38 2c 0a 20 2f 2a 20 20 20 39 39 30     58,. /*   990
2229a 20 2a 2f 20 20 20 31 30 37 2c 20 20 20 20 35 2c   */   107,    5,
2229b 20 20 31 31 31 2c 20 20 31 34 37 2c 20 20 31 31    111,  147,  11
2229c 31 2c 20 20 32 30 33 2c 20 20 20 31 30 2c 20 20  1,  203,   10,  
2229d 20 31 31 2c 20 20 20 31 32 2c 20 20 20 31 33 2c   11,   12,   13,
2229e 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20 20  . /*  1000 */   
2229f 31 32 31 2c 20 20 31 34 37 2c 20 20 31 34 37 2c  121,  147,  147,
222a0 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 31 34     91,   92,  14
222a1 37 2c 20 20 31 31 32 2c 20 20 31 34 37 2c 20 20  7,  112,  147,  
222a2 31 34 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20  147,   78,. /*  
222a3 31 30 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20  1010 */    79,  
222a4 31 34 37 2c 20 20 20 32 36 2c 20 20 20 31 39 2c  147,   26,   19,
222a5 20 20 20 32 38 2c 20 20 31 36 39 2c 20 20 31 37     28,  169,  17
222a6 30 2c 20 20 20 32 33 2c 20 20 20 38 37 2c 20 20  0,   23,   87,  
222a7 20 38 38 2c 0a 20 2f 2a 20 20 31 30 32 30 20 2a   88,. /*  1020 *
222a8 2f 20 20 20 20 38 39 2c 20 20 20 33 35 2c 20 20  /    89,   35,  
222a9 31 34 37 2c 20 20 20 39 32 2c 20 20 31 36 39 2c  147,   92,  169,
222aa 20 20 31 37 30 2c 20 20 31 37 38 2c 20 20 31 36    170,  178,  16
222ab 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 0a 20  9,  170,  147,. 
222ac 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20 20 31 36  /*  1030 */   16
222ad 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 20 20  9,  170,  147,  
222ae 20 34 37 2c 20 20 31 31 33 2c 20 20 20 34 39 2c   47,  113,   49,
222af 20 20 20 39 32 2c 20 20 31 37 38 2c 20 20 31 34     92,  178,  14
222b0 37 2c 20 20 20 35 33 2c 0a 20 2f 2a 20 20 31 30  7,   53,. /*  10
222b1 34 30 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 37  40 */   147,  17
222b2 38 2c 20 20 20 35 36 2c 20 20 31 34 37 2c 20 20  8,   56,  147,  
222b3 31 36 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c  169,  170,  147,
222b4 20 20 31 30 33 2c 20 20 31 34 37 2c 20 20 20 31    103,  147,   1
222b5 39 2c 0a 20 2f 2a 20 20 31 30 35 30 20 2a 2f 20  9,. /*  1050 */ 
222b6 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20 31 37    147,  169,  17
222b7 30 2c 20 20 31 34 37 2c 20 20 31 34 37 2c 20 20  0,  147,  147,  
222b8 31 32 34 2c 20 20 31 32 35 2c 20 20 31 32 36 2c  124,  125,  126,
222b9 20 20 31 32 37 2c 20 20 31 32 38 2c 0a 20 2f 2a    127,  128,. /*
222ba 20 20 31 30 36 30 20 2a 2f 20 20 20 31 32 39 2c    1060 */   129,
222bb 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20 31 37    147,  169,  17
222bc 30 2c 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20  0,  147,  169,  
222bd 31 37 30 2c 20 20 31 34 37 2c 20 20 31 36 39 2c  170,  147,  169,
222be 20 20 31 37 30 2c 0a 20 2f 2a 20 20 31 30 37 30    170,. /*  1070
222bf 20 2a 2f 20 20 20 31 36 39 2c 20 20 31 37 30 2c   */   169,  170,
222c0 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 34    169,  170,  14
222c1 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  7,  169,  170,  
222c2 31 34 37 2c 20 20 20 32 30 2c 20 20 31 34 37 2c  147,   20,  147,
222c3 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f 20 20 20  . /*  1080 */   
222c4 20 32 32 2c 20 20 31 34 37 2c 20 20 20 38 38 2c   22,  147,   88,
222c5 20 20 31 34 37 2c 20 20 32 33 32 2c 20 20 20 39    147,  232,   9
222c6 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20  9,  100,  101,  
222c7 31 34 37 2c 20 20 31 36 39 2c 0a 20 2f 2a 20 20  147,  169,. /*  
222c8 31 30 39 30 20 2a 2f 20 20 20 31 37 30 2c 20 20  1090 */   170,  
222c9 31 30 35 2c 20 20 31 34 37 2c 20 20 20 32 30 2c  105,  147,   20,
222ca 20 20 31 34 37 2c 20 20 20 32 32 2c 20 20 31 31    147,   22,  11
222cb 30 2c 20 20 20 32 30 2c 20 20 20 36 38 2c 20 20  0,   20,   68,  
222cc 31 36 39 2c 0a 20 2f 2a 20 20 31 31 30 30 20 2a  169,. /*  1100 *
222cd 2f 20 20 20 31 37 30 2c 20 20 31 36 39 2c 20 20  /   170,  169,  
222ce 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37 30 2c  170,  169,  170,
222cf 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 20 32    169,  170,   2
222d0 30 2c 20 20 31 34 37 2c 20 20 20 32 32 2c 0a 20  0,  147,   22,. 
222d1 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20 20 31 34  /*  1110 */   14
222d2 37 2c 20 20 20 32 30 2c 20 20 31 34 37 2c 20 20  7,   20,  147,  
222d3 20 32 32 2c 20 20 31 36 39 2c 20 20 31 37 30 2c   22,  169,  170,
222d4 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 34    169,  170,  14
222d5 37 2c 20 20 32 33 33 2c 0a 20 2f 2a 20 20 31 31  7,  233,. /*  11
222d6 32 30 20 2a 2f 20 20 20 31 33 34 2c 20 20 20 32  20 */   134,   2
222d7 30 2c 20 20 31 34 37 2c 20 20 20 32 32 2c 20 20  0,  147,   22,  
222d8 20 32 30 2c 20 20 31 34 37 2c 20 20 20 32 32 2c   20,  147,   22,
222d9 20 20 20 32 30 2c 20 20 20 32 30 2c 20 20 20 32     20,   20,   2
222da 32 2c 0a 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20  2,. /*  1130 */ 
222db 20 20 20 32 32 2c 20 20 31 34 37 2c 20 20 31 36     22,  147,  16
222dc 39 2c 20 20 31 37 30 2c 20 20 31 36 39 2c 20 20  9,  170,  169,  
222dd 31 37 30 2c 20 20 20 35 39 2c 20 20 31 34 37 2c  170,   59,  147,
222de 20 20 31 34 37 2c 20 20 31 34 37 2c 0a 20 2f 2a    147,  147,. /*
222df 20 20 31 31 34 30 20 2a 2f 20 20 20 31 36 39 2c    1140 */   169,
222e0 20 20 31 37 30 2c 20 20 31 34 37 2c 20 20 31 34    170,  147,  14
222e1 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  7,  169,  170,  
222e2 31 34 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c  147,  169,  170,
222e3 20 20 31 34 37 2c 0a 20 2f 2a 20 20 31 31 35 30    147,. /*  1150
222e4 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 34 37 2c   */   147,  147,
222e5 20 20 31 34 37 2c 20 20 31 39 31 2c 20 20 32 32    147,  191,  22
222e6 39 2c 20 20 31 34 39 2c 20 20 32 32 33 2c 20 20  9,  149,  223,  
222e7 31 39 33 2c 20 20 32 32 39 2c 20 20 31 37 32 2c  193,  229,  172,
222e8 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20 20  . /*  1160 */   
222e9 31 37 32 2c 20 20 31 37 32 2c 20 20 31 37 37 2c  172,  172,  177,
222ea 20 20 31 39 34 2c 20 20 31 39 34 2c 20 20 31 37    194,  194,  17
222eb 32 2c 20 20 20 20 36 2c 20 20 31 34 36 2c 20 20  2,    6,  146,  
222ec 31 34 36 2c 20 20 31 37 32 2c 0a 20 2f 2a 20 20  146,  172,. /*  
222ed 31 31 37 30 20 2a 2f 20 20 20 31 34 36 2c 20 20  1170 */   146,  
222ee 31 37 33 2c 20 20 31 34 36 2c 20 20 20 32 32 2c  173,  146,   22,
222ef 20 20 31 35 34 2c 20 20 31 32 31 2c 20 20 31 39    154,  121,  19
222f0 34 2c 20 20 31 31 38 2c 20 20 31 39 35 2c 20 20  4,  118,  195,  
222f1 31 31 39 2c 0a 20 2f 2a 20 20 31 31 38 30 20 2a  119,. /*  1180 *
222f2 2f 20 20 20 31 31 36 2c 20 20 31 32 30 2c 20 20  /   116,  120,  
222f3 20 32 33 2c 20 20 31 36 30 2c 20 20 31 31 32 2c   23,  160,  112,
222f4 20 20 31 33 30 2c 20 20 31 35 32 2c 20 20 31 35    130,  152,  15
222f5 32 2c 20 20 32 32 32 2c 20 20 31 38 39 2c 0a 20  2,  222,  189,. 
222f6 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20 20 20 39  /*  1190 */    9
222f7 38 2c 20 20 31 31 35 2c 20 20 20 39 38 2c 20 20  8,  115,   98,  
222f8 20 34 30 2c 20 20 31 37 39 2c 20 20 20 39 37 2c   40,  179,   97,
222f9 20 20 31 37 31 2c 20 20 31 39 36 2c 20 20 31 37    171,  196,  17
222fa 31 2c 20 20 31 37 31 2c 0a 20 2f 2a 20 20 31 32  1,  171,. /*  12
222fb 30 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 38  00 */    19,   8
222fc 34 2c 20 20 31 37 33 2c 20 20 31 37 31 2c 20 20  4,  173,  171,  
222fd 31 39 37 2c 20 20 31 37 34 2c 20 20 31 39 38 2c  197,  174,  198,
222fe 20 20 32 32 36 2c 20 20 31 36 30 2c 20 20 31 36    226,  160,  16
222ff 30 2c 0a 20 2f 2a 20 20 31 32 31 30 20 2a 2f 20  0,. /*  1210 */ 
22300 20 20 31 37 31 2c 20 20 31 37 31 2c 20 20 31 37    171,  171,  17
22301 39 2c 20 20 32 30 34 2c 20 20 31 37 31 2c 20 20  9,  204,  171,  
22302 32 30 35 2c 20 20 32 30 34 2c 20 20 20 31 35 2c  205,  204,   15,
22303 20 20 32 30 35 2c 20 20 31 37 34 2c 0a 20 2f 2a    205,  174,. /*
22304 20 20 31 32 32 30 20 2a 2f 20 20 20 31 35 31 2c    1220 */   151,
22305 20 20 20 33 38 2c 20 20 31 35 32 2c 20 20 31 35     38,  152,  15
22306 32 2c 20 20 20 36 30 2c 20 20 31 35 31 2c 20 20  2,   60,  151,  
22307 31 35 31 2c 20 20 31 33 30 2c 20 20 31 38 34 2c  151,  130,  184,
22308 20 20 20 31 39 2c 0a 20 2f 2a 20 20 31 32 33 30     19,. /*  1230
22309 20 2a 2f 20 20 20 31 35 32 2c 20 20 31 35 32 2c   */   152,  152,
2230a 20 20 31 35 31 2c 20 20 32 31 34 2c 20 20 31 35    151,  214,  15
2230b 32 2c 20 20 31 38 34 2c 20 20 31 39 34 2c 20 20  2,  184,  194,  
2230c 32 32 36 2c 20 20 31 35 32 2c 20 20 20 31 35 2c  226,  152,   15,
2230d 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f 20 20 20  . /*  1240 */   
2230e 31 38 37 2c 20 20 31 39 34 2c 20 20 31 35 32 2c  187,  194,  152,
2230f 20 20 31 38 37 2c 20 20 31 38 37 2c 20 20 20 33    187,  187,   3
22310 33 2c 20 20 31 33 37 2c 20 20 31 38 34 2c 20 20  3,  137,  184,  
22311 32 33 34 2c 20 20 31 38 37 2c 0a 20 2f 2a 20 20  234,  187,. /*  
22312 31 32 35 30 20 2a 2f 20 20 20 32 31 34 2c 20 20  1250 */   214,  
22313 32 33 35 2c 20 20 31 35 32 2c 20 20 20 20 31 2c  235,  152,    1,
22314 20 20 32 33 37 2c 20 20 32 33 37 2c 20 20 20 32    237,  237,   2
22315 30 2c 20 20 31 35 32 2c 20 20 31 35 39 2c 20 20  0,  152,  159,  
22316 31 37 35 2c 0a 20 2f 2a 20 20 31 32 36 30 20 2a  175,. /*  1260 *
22317 2f 20 20 20 31 37 35 2c 20 20 31 31 32 2c 20 20  /   175,  112,  
22318 31 31 32 2c 20 20 20 39 32 2c 20 20 31 31 32 2c  112,   92,  112,
22319 20 20 31 30 37 2c 20 20 31 31 32 2c 20 20 20 31    107,  112,   1
2231a 39 2c 20 20 20 31 39 2c 20 20 32 33 31 2c 0a 20  9,   19,  231,. 
2231b 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20 20 20 32  /*  1270 */    2
2231c 30 2c 20 20 20 32 30 2c 20 20 20 31 31 2c 20 20  0,   20,   11,  
2231d 20 31 39 2c 20 20 31 31 37 2c 20 20 20 32 30 2c   19,  117,   20,
2231e 20 20 31 31 34 2c 20 20 32 34 30 2c 20 20 20 32    114,  240,   2
2231f 32 2c 20 20 20 32 30 2c 0a 20 2f 2a 20 20 31 32  2,   20,. /*  12
22320 38 30 20 2a 2f 20 20 20 20 32 30 2c 20 20 20 31  80 */    20,   1
22321 39 2c 20 20 31 31 37 2c 20 20 20 34 34 2c 20 20  9,  117,   44,  
22322 32 34 33 2c 20 20 20 32 32 2c 20 20 20 32 32 2c  243,   22,   22,
22323 20 20 20 32 30 2c 20 20 31 31 32 2c 20 20 20 32     20,  112,   2
22324 30 2c 0a 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20  0,. /*  1290 */ 
22325 20 20 20 32 30 2c 20 20 20 31 39 2c 20 20 32 34     20,   19,  24
22326 36 2c 20 20 20 31 39 2c 20 20 20 39 36 2c 20 20  6,   19,   96,  
22327 20 32 30 2c 20 20 20 31 39 2c 20 20 20 33 32 2c   20,   19,   32,
22328 20 20 20 31 39 2c 20 20 20 31 39 2c 0a 20 2f 2a     19,   19,. /*
22329 20 20 31 33 30 30 20 2a 2f 20 20 20 31 30 33 2c    1300 */   103,
2232a 20 20 20 34 34 2c 20 20 20 34 34 2c 20 20 20 31     44,   44,   1
2232b 36 2c 20 20 20 32 31 2c 20 20 20 31 37 2c 20 20  6,   21,   17,  
2232c 20 39 38 2c 20 20 20 33 36 2c 20 20 20 32 32 2c   98,   36,   22,
2232d 20 20 20 39 38 2c 0a 20 2f 2a 20 20 31 33 31 30     98,. /*  1310
2232e 20 2a 2f 20 20 20 31 33 33 2c 20 20 20 31 39 2c   */   133,   19,
2232f 20 20 20 20 35 2c 20 20 20 34 35 2c 20 20 20 20      5,   45,    
22330 31 2c 20 20 31 30 32 2c 20 20 31 32 32 2c 20 20  1,  102,  122,  
22331 20 35 31 2c 20 20 20 34 35 2c 20 20 20 31 39 2c   51,   45,   19,
22332 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20 20  . /*  1320 */   
22333 31 31 33 2c 20 20 20 36 38 2c 20 20 20 31 34 2c  113,   68,   14,
22334 20 20 20 31 37 2c 20 20 31 31 35 2c 20 20 31 30     17,  115,  10
22335 32 2c 20 20 31 32 32 2c 20 20 31 31 33 2c 20 20  2,  122,  113,  
22336 20 31 39 2c 20 20 31 32 33 2c 0a 20 2f 2a 20 20   19,  123,. /*  
22337 31 33 33 30 20 2a 2f 20 20 20 20 32 30 2c 20 20  1330 */    20,  
22338 20 31 34 2c 20 20 31 33 36 2c 20 20 20 31 39 2c   14,  136,   19,
22339 20 20 31 33 35 2c 20 20 20 35 37 2c 20 20 20 20    135,   57,    
2233a 33 2c 20 20 32 34 37 2c 20 20 20 20 34 2c 20 20  3,  247,    4,  
2233b 32 34 37 2c 0a 20 2f 2a 20 20 31 33 34 30 20 2a  247,. /*  1340 *
2233c 2f 20 20 20 32 34 37 2c 20 20 32 34 37 2c 20 20  /   247,  247,  
2233d 32 34 37 2c 20 20 32 34 37 2c 20 20 20 36 38 2c  247,  247,   68,
2233e 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f 53  .};.#define YY_S
2233f 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 20 28 2d  HIFT_USE_DFLT (-
22340 36 32 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 53  62).#define YY_S
22341 48 49 46 54 5f 4d 41 58 20 33 38 37 0a 73 74 61  HIFT_MAX 387.sta
22342 74 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20  tic const short 
22343 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20  yy_shift_ofst[] 
22344 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f  = {. /*     0 */
22345 20 20 20 20 33 39 2c 20 20 38 34 31 2c 20 20 39      39,  841,  9
22346 38 36 2c 20 20 2d 31 36 2c 20 20 38 34 31 2c 20  86,  -16,  841, 
22347 20 39 33 31 2c 20 20 39 33 31 2c 20 20 32 35 38   931,  931,  258
22348 2c 20 20 31 32 33 2c 20 20 2d 33 36 2c 0a 20 2f  ,  123,  -36,. /
22349 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20 39 36  *    10 */    96
2234a 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39  ,  931,  931,  9
2234b 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20  31,  931,  931, 
2234c 20 2d 34 35 2c 20 20 34 30 30 2c 20 20 31 37 34   -45,  400,  174
2234d 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20 20 32  ,   19,. /*    2
2234e 30 20 2a 2f 20 20 20 31 37 31 2c 20 20 2d 35 34  0 */   171,  -54
2234f 2c 20 20 2d 35 34 2c 20 20 20 35 33 2c 20 20 31  ,  -54,   53,  1
22350 36 35 2c 20 20 32 30 38 2c 20 20 32 35 31 2c 20  65,  208,  251, 
22351 20 33 32 34 2c 20 20 33 39 33 2c 20 20 34 36 32   324,  393,  462
22352 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20  ,. /*    30 */  
22353 20 35 33 31 2c 20 20 36 30 30 2c 20 20 36 34 33   531,  600,  643
22354 2c 20 20 36 38 36 2c 20 20 36 34 33 2c 20 20 36  ,  686,  643,  6
22355 34 33 2c 20 20 36 34 33 2c 20 20 36 34 33 2c 20  43,  643,  643, 
22356 20 36 34 33 2c 20 20 36 34 33 2c 0a 20 2f 2a 20   643,  643,. /* 
22357 20 20 20 34 30 20 2a 2f 20 20 20 36 34 33 2c 20     40 */   643, 
22358 20 36 34 33 2c 20 20 36 34 33 2c 20 20 36 34 33   643,  643,  643
22359 2c 20 20 36 34 33 2c 20 20 36 34 33 2c 20 20 36  ,  643,  643,  6
2235a 34 33 2c 20 20 36 34 33 2c 20 20 36 34 33 2c 20  43,  643,  643, 
2235b 20 36 34 33 2c 0a 20 2f 2a 20 20 20 20 35 30 20   643,. /*    50 
2235c 2a 2f 20 20 20 36 34 33 2c 20 20 36 34 33 2c 20  */   643,  643, 
2235d 20 37 32 39 2c 20 20 37 37 32 2c 20 20 37 37 32   729,  772,  772
2235e 2c 20 20 38 35 37 2c 20 20 39 33 31 2c 20 20 39  ,  857,  931,  9
2235f 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 0a  31,  931,  931,.
22360 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 39   /*    60 */   9
22361 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20  31,  931,  931, 
22362 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31   931,  931,  931
22363 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39  ,  931,  931,  9
22364 33 31 2c 20 20 39 33 31 2c 0a 20 2f 2a 20 20 20  31,  931,. /*   
22365 20 37 30 20 2a 2f 20 20 20 39 33 31 2c 20 20 39   70 */   931,  9
22366 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20  31,  931,  931, 
22367 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31   931,  931,  931
22368 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39  ,  931,  931,  9
22369 33 31 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f  31,. /*    80 */
2236a 20 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39     931,  931,  9
2236b 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20  31,  931,  931, 
2236c 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31   931,  931,  931
2236d 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 0a 20 2f  ,  931,  931,. /
2236e 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 39 33 31  *    90 */   931
2236f 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39  ,  931,  931,  9
22370 33 31 2c 20 20 39 33 31 2c 20 20 2d 36 31 2c 20  31,  931,  -61, 
22371 20 2d 36 31 2c 20 20 20 20 36 2c 20 20 20 20 36   -61,    6,    6
22372 2c 20 20 32 38 30 2c 0a 20 2f 2a 20 20 20 31 30  ,  280,. /*   10
22373 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 36 31  0 */    22,   61
22374 2c 20 20 33 39 39 2c 20 20 35 36 34 2c 20 20 20  ,  399,  564,   
22375 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  19,   19,   19, 
22376 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39    19,   19,   19
22377 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20  ,. /*   110 */  
22378 20 20 31 39 2c 20 20 32 31 36 2c 20 20 31 37 31    19,  216,  171
22379 2c 20 20 20 36 33 2c 20 20 2d 36 32 2c 20 20 2d  ,   63,  -62,  -
2237a 36 32 2c 20 20 31 33 31 2c 20 20 33 32 36 2c 20  62,  131,  326, 
2237b 20 34 37 32 2c 20 20 34 37 32 2c 0a 20 2f 2a 20   472,  472,. /* 
2237c 20 20 31 32 30 20 2a 2f 20 20 20 34 39 38 2c 20    120 */   498, 
2237d 20 35 35 39 2c 20 20 35 30 36 2c 20 20 37 39 39   559,  506,  799
2237e 2c 20 20 20 31 39 2c 20 20 37 39 39 2c 20 20 20  ,   19,  799,   
2237f 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  19,   19,   19, 
22380 20 20 31 39 2c 0a 20 2f 2a 20 20 20 31 33 30 20    19,. /*   130 
22381 2a 2f 20 20 20 20 31 39 2c 20 20 20 31 39 2c 20  */    19,   19, 
22382 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39    19,   19,   19
22383 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20  ,   19,   19,   
22384 31 39 2c 20 20 20 31 39 2c 20 20 38 34 39 2c 0a  19,   19,  849,.
22385 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20   /*   140 */    
22386 39 35 2c 20 20 2d 33 36 2c 20 20 2d 33 36 2c 20  95,  -36,  -36, 
22387 20 2d 33 36 2c 20 20 2d 36 32 2c 20 20 2d 36 32   -36,  -62,  -62
22388 2c 20 20 2d 36 32 2c 20 20 2d 31 35 2c 20 20 2d  ,  -62,  -15,  -
22389 31 35 2c 20 20 33 33 33 2c 0a 20 2f 2a 20 20 20  15,  333,. /*   
2238a 31 35 30 20 2a 2f 20 20 20 34 35 39 2c 20 20 34  150 */   459,  4
2238b 37 38 2c 20 20 35 35 37 2c 20 20 35 33 30 2c 20  78,  557,  530, 
2238c 20 35 34 31 2c 20 20 36 31 36 2c 20 20 36 30 32   541,  616,  602
2238d 2c 20 20 37 39 33 2c 20 20 36 30 34 2c 20 20 36  ,  793,  604,  6
2238e 30 37 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f  07,. /*   160 */
2238f 20 20 20 36 32 36 2c 20 20 20 31 39 2c 20 20 20     626,   19,   
22390 31 39 2c 20 20 38 38 31 2c 20 20 20 31 39 2c 20  19,  881,   19, 
22391 20 20 31 39 2c 20 20 39 39 34 2c 20 20 20 31 39    19,  994,   19
22392 2c 20 20 20 31 39 2c 20 20 38 30 37 2c 0a 20 2f  ,   19,  807,. /
22393 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 20 31 39  *   170 */    19
22394 2c 20 20 20 31 39 2c 20 20 36 37 33 2c 20 20 38  ,   19,  673,  8
22395 30 37 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  07,   19,   19, 
22396 20 33 38 34 2c 20 20 33 38 34 2c 20 20 33 38 34   384,  384,  384
22397 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20 31 38  ,   19,. /*   18
22398 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 36 37 33  0 */    19,  673
22399 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20 36  ,   19,   19,  6
2239a 37 33 2c 20 20 20 31 39 2c 20 20 34 35 34 2c 20  73,   19,  454, 
2239b 20 36 38 35 2c 20 20 20 31 39 2c 20 20 20 31 39   685,   19,   19
2239c 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20  ,. /*   190 */  
2239d 20 36 37 33 2c 20 20 20 31 39 2c 20 20 20 31 39   673,   19,   19
2239e 2c 20 20 20 31 39 2c 20 20 36 37 33 2c 20 20 20  ,   19,  673,   
2239f 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  19,   19,   19, 
223a0 20 36 37 33 2c 20 20 36 37 33 2c 0a 20 2f 2a 20   673,  673,. /* 
223a1 20 20 32 30 30 20 2a 2f 20 20 20 20 31 39 2c 20    200 */    19, 
223a2 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39    19,   19,   19
223a3 2c 20 20 20 31 39 2c 20 20 34 36 38 2c 20 20 38  ,   19,  468,  8
223a4 38 33 2c 20 20 39 32 31 2c 20 20 37 35 34 2c 20  83,  921,  754, 
223a5 20 37 35 34 2c 0a 20 2f 2a 20 20 20 32 31 30 20   754,. /*   210 
223a6 2a 2f 20 20 20 34 33 32 2c 20 20 34 30 36 2c 20  */   432,  406, 
223a7 20 34 30 36 2c 20 20 34 30 36 2c 20 20 38 31 36   406,  406,  816
223a8 2c 20 20 34 30 36 2c 20 20 34 30 36 2c 20 20 38  ,  406,  406,  8
223a9 31 31 2c 20 20 38 37 39 2c 20 20 38 37 39 2c 0a  11,  879,  879,.
223aa 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 31 31   /*   220 */  11
223ab 36 30 2c 20 31 31 36 30 2c 20 31 31 36 30 2c 20  60, 1160, 1160, 
223ac 31 31 36 30 2c 20 31 31 35 31 2c 20 20 2d 33 36  1160, 1151,  -36
223ad 2c 20 31 30 35 34 2c 20 31 30 35 39 2c 20 31 30  , 1054, 1059, 10
223ae 36 30 2c 20 31 30 36 34 2c 0a 20 2f 2a 20 20 20  60, 1064,. /*   
223af 32 33 30 20 2a 2f 20 20 31 30 36 31 2c 20 31 31  230 */  1061, 11
223b0 35 39 2c 20 31 30 35 35 2c 20 31 30 37 32 2c 20  59, 1055, 1072, 
223b1 31 30 37 32 2c 20 31 30 39 32 2c 20 31 30 37 36  1072, 1092, 1076
223b2 2c 20 31 30 39 32 2c 20 31 30 37 36 2c 20 31 30  , 1092, 1076, 10
223b3 39 34 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f  94,. /*   240 */
223b4 20 20 31 30 39 34 2c 20 31 31 35 33 2c 20 31 30    1094, 1153, 10
223b5 39 34 2c 20 31 30 39 38 2c 20 31 30 39 34 2c 20  94, 1098, 1094, 
223b6 31 31 38 31 2c 20 31 31 31 37 2c 20 31 31 35 39  1181, 1117, 1159
223b7 2c 20 31 31 31 37 2c 20 31 31 35 39 2c 0a 20 2f  , 1117, 1159,. /
223b8 2a 20 20 20 32 35 30 20 2a 2f 20 20 31 31 35 33  *   250 */  1153
223b9 2c 20 31 30 39 34 2c 20 31 30 39 34 2c 20 31 30  , 1094, 1094, 10
223ba 39 34 2c 20 31 31 38 31 2c 20 31 32 30 32 2c 20  94, 1181, 1202, 
223bb 31 30 37 32 2c 20 31 32 30 32 2c 20 31 30 37 32  1072, 1202, 1072
223bc 2c 20 31 32 30 32 2c 0a 20 2f 2a 20 20 20 32 36  , 1202,. /*   26
223bd 30 20 2a 2f 20 20 31 30 37 32 2c 20 31 30 37 32  0 */  1072, 1072
223be 2c 20 31 31 38 33 2c 20 31 30 39 37 2c 20 31 32  , 1183, 1097, 12
223bf 30 32 2c 20 31 30 37 32 2c 20 31 31 36 34 2c 20  02, 1072, 1164, 
223c0 31 31 36 34 2c 20 31 32 31 30 2c 20 31 30 35 34  1164, 1210, 1054
223c1 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20  ,. /*   270 */  
223c2 31 30 37 32 2c 20 31 32 32 34 2c 20 31 32 32 34  1072, 1224, 1224
223c3 2c 20 31 32 32 34 2c 20 31 32 32 34 2c 20 31 30  , 1224, 1224, 10
223c4 35 34 2c 20 31 31 36 34 2c 20 31 32 31 30 2c 20  54, 1164, 1210, 
223c5 31 30 37 32 2c 20 31 32 31 32 2c 0a 20 2f 2a 20  1072, 1212,. /* 
223c6 20 20 32 38 30 20 2a 2f 20 20 31 32 31 32 2c 20    280 */  1212, 
223c7 31 30 37 32 2c 20 31 30 37 32 2c 20 31 31 30 39  1072, 1072, 1109
223c8 2c 20 20 2d 36 32 2c 20 20 2d 36 32 2c 20 20 2d  ,  -62,  -62,  -
223c9 36 32 2c 20 20 2d 36 32 2c 20 20 2d 36 32 2c 20  62,  -62,  -62, 
223ca 20 2d 36 32 2c 0a 20 2f 2a 20 20 20 32 39 30 20   -62,. /*   290 
223cb 2a 2f 20 20 20 35 32 35 2c 20 20 36 38 34 2c 20  */   525,  684, 
223cc 20 37 32 37 2c 20 20 31 36 38 2c 20 20 38 39 34   727,  168,  894
223cd 2c 20 20 35 35 36 2c 20 20 35 35 35 2c 20 20 39  ,  556,  555,  9
223ce 33 38 2c 20 20 39 34 34 2c 20 20 39 34 39 2c 0a  38,  944,  949,.
223cf 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 39   /*   300 */   9
223d0 31 32 2c 20 31 30 35 38 2c 20 31 30 37 33 2c 20  12, 1058, 1073, 
223d1 31 30 38 37 2c 20 31 30 39 31 2c 20 31 31 30 31  1087, 1091, 1101
223d2 2c 20 31 31 30 34 2c 20 31 31 30 37 2c 20 31 30  , 1104, 1107, 10
223d3 33 30 2c 20 31 31 30 38 2c 0a 20 2f 2a 20 20 20  30, 1108,. /*   
223d4 33 31 30 20 2a 2f 20 20 31 30 37 37 2c 20 31 32  310 */  1077, 12
223d5 35 32 2c 20 31 32 33 36 2c 20 31 31 34 39 2c 20  52, 1236, 1149, 
223d6 31 31 35 30 2c 20 31 31 35 32 2c 20 31 31 35 34  1150, 1152, 1154
223d7 2c 20 31 31 37 31 2c 20 31 31 35 38 2c 20 31 32  , 1171, 1158, 12
223d8 34 38 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f  48,. /*   320 */
223d9 20 20 31 32 35 30 2c 20 31 32 35 31 2c 20 31 32    1250, 1251, 12
223da 34 39 2c 20 31 32 36 31 2c 20 31 32 35 34 2c 20  49, 1261, 1254, 
223db 31 32 35 35 2c 20 31 32 35 36 2c 20 31 32 35 39  1255, 1256, 1259
223dc 2c 20 31 32 36 30 2c 20 31 32 36 33 2c 0a 20 2f  , 1260, 1263,. /
223dd 2a 20 20 20 33 33 30 20 2a 2f 20 20 31 31 35 37  *   330 */  1157
223de 2c 20 31 32 36 34 2c 20 31 31 36 35 2c 20 31 32  , 1264, 1165, 12
223df 36 33 2c 20 31 31 36 32 2c 20 31 32 36 32 2c 20  63, 1162, 1262, 
223e0 31 32 36 37 2c 20 31 31 37 36 2c 20 31 32 36 39  1267, 1176, 1269
223e1 2c 20 31 32 37 30 2c 0a 20 2f 2a 20 20 20 33 34  , 1270,. /*   34
223e2 30 20 2a 2f 20 20 31 32 36 35 2c 20 31 32 33 39  0 */  1265, 1239
223e3 2c 20 31 32 37 32 2c 20 31 32 35 37 2c 20 31 32  , 1272, 1257, 12
223e4 37 34 2c 20 31 32 37 35 2c 20 31 32 37 37 2c 20  74, 1275, 1277, 
223e5 31 32 37 39 2c 20 31 32 35 38 2c 20 31 32 38 30  1279, 1258, 1280
223e6 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20  ,. /*   350 */  
223e7 31 31 39 38 2c 20 31 31 39 37 2c 20 31 32 38 37  1198, 1197, 1287
223e8 2c 20 31 32 38 38 2c 20 31 32 38 33 2c 20 31 32  , 1288, 1283, 12
223e9 30 38 2c 20 31 32 37 31 2c 20 31 32 36 36 2c 20  08, 1271, 1266, 
223ea 31 32 36 38 2c 20 31 32 38 36 2c 0a 20 2f 2a 20  1268, 1286,. /* 
223eb 20 20 33 36 30 20 2a 2f 20 20 31 32 37 33 2c 20    360 */  1273, 
223ec 31 31 37 37 2c 20 31 32 31 31 2c 20 31 32 39 32  1177, 1211, 1292
223ed 2c 20 31 33 30 37 2c 20 31 33 31 33 2c 20 31 32  , 1307, 1313, 12
223ee 31 33 2c 20 31 32 35 33 2c 20 31 32 37 36 2c 20  13, 1253, 1276, 
223ef 31 31 39 34 2c 0a 20 2f 2a 20 20 20 33 37 30 20  1194,. /*   370 
223f0 2a 2f 20 20 31 33 30 30 2c 20 31 32 30 37 2c 20  */  1300, 1207, 
223f1 31 33 30 38 2c 20 31 32 30 39 2c 20 31 33 30 36  1308, 1209, 1306
223f2 2c 20 31 32 31 34 2c 20 31 32 32 33 2c 20 31 32  , 1214, 1223, 12
223f3 30 34 2c 20 31 33 30 39 2c 20 31 32 30 36 2c 0a  04, 1309, 1206,.
223f4 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 31 33   /*   380 */  13
223f5 31 30 2c 20 31 33 31 37 2c 20 31 32 37 38 2c 20  10, 1317, 1278, 
223f6 31 31 39 39 2c 20 31 31 39 36 2c 20 31 33 31 34  1199, 1196, 1314
223f7 2c 20 31 33 33 33 2c 20 31 33 33 34 2c 0a 7d 3b  , 1333, 1334,.};
223f8 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44 55  .#define YY_REDU
223f9 43 45 5f 55 53 45 5f 44 46 4c 54 20 28 2d 31 36  CE_USE_DFLT (-16
223fa 35 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45  5).#define YY_RE
223fb 44 55 43 45 5f 4d 41 58 20 32 38 39 0a 73 74 61  DUCE_MAX 289.sta
223fc 74 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20  tic const short 
223fd 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d  yy_reduce_ofst[]
223fe 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
223ff 2f 20 20 2d 31 33 38 2c 20 20 32 37 37 2c 20 20  /  -138,  277,  
22400 35 34 36 2c 20 20 31 33 37 2c 20 20 34 30 31 2c  546,  137,  401,
22401 20 20 2d 32 31 2c 20 20 20 34 34 2c 20 20 20 33    -21,   44,   3
22402 36 2c 20 20 20 33 38 2c 20 20 32 34 32 2c 0a 20  6,   38,  242,. 
22403 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 2d 31 34  /*    10 */  -14
22404 31 2c 20 20 31 39 31 2c 20 20 20 39 31 2c 20 20  1,  191,   91,  
22405 32 36 39 2c 20 20 33 34 33 2c 20 20 33 34 35 2c  269,  343,  345,
22406 20 2d 31 32 36 2c 20 20 35 38 39 2c 20 20 33 33   -126,  589,  33
22407 38 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 20  8,  150,. /*    
22408 32 30 20 2a 2f 20 20 20 31 34 37 2c 20 20 2d 31  20 */   147,  -1
22409 33 2c 20 20 32 31 33 2c 20 20 34 31 32 2c 20 20  3,  213,  412,  
2240a 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c  412,  412,  412,
2240b 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
2240c 32 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  2,. /*    30 */ 
2240d 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
2240e 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20  2,  412,  412,  
2240f 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c  412,  412,  412,
22410 20 20 34 31 32 2c 20 20 34 31 32 2c 0a 20 2f 2a    412,  412,. /*
22411 20 20 20 20 34 30 20 2a 2f 20 20 20 34 31 32 2c      40 */   412,
22412 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
22413 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20  2,  412,  412,  
22414 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c  412,  412,  412,
22415 20 20 34 31 32 2c 0a 20 2f 2a 20 20 20 20 35 30    412,. /*    50
22416 20 2a 2f 20 20 20 34 31 32 2c 20 20 34 31 32 2c   */   412,  412,
22417 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
22418 32 2c 20 20 32 31 31 2c 20 20 36 39 38 2c 20 20  2,  211,  698,  
22419 37 31 34 2c 20 20 37 31 36 2c 20 20 37 32 32 2c  714,  716,  722,
2241a 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
2241b 37 32 34 2c 20 20 37 32 38 2c 20 20 37 34 38 2c  724,  728,  748,
2241c 20 20 37 35 33 2c 20 20 37 35 35 2c 20 20 37 35    753,  755,  75
2241d 37 2c 20 20 37 36 32 2c 20 20 37 36 39 2c 20 20  7,  762,  769,  
2241e 37 39 34 2c 20 20 38 30 35 2c 0a 20 2f 2a 20 20  794,  805,. /*  
2241f 20 20 37 30 20 2a 2f 20 20 20 38 30 38 2c 20 20    70 */   808,  
22420 38 34 36 2c 20 20 38 35 35 2c 20 20 38 35 38 2c  846,  855,  858,
22421 20 20 38 36 31 2c 20 20 38 37 35 2c 20 20 38 38    861,  875,  88
22422 32 2c 20 20 38 39 33 2c 20 20 38 39 36 2c 20 20  2,  893,  896,  
22423 38 39 39 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  899,. /*    80 *
22424 2f 20 20 20 39 30 31 2c 20 20 39 30 33 2c 20 20  /   901,  903,  
22425 39 30 36 2c 20 20 39 32 30 2c 20 20 39 33 30 2c  906,  920,  930,
22426 20 20 39 33 32 2c 20 20 39 33 34 2c 20 20 39 33    932,  934,  93
22427 36 2c 20 20 39 34 35 2c 20 20 39 34 37 2c 0a 20  6,  945,  947,. 
22428 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 39 36  /*    90 */   96
22429 33 2c 20 20 39 36 35 2c 20 20 39 37 31 2c 20 20  3,  965,  971,  
2242a 39 37 35 2c 20 20 39 37 38 2c 20 20 34 31 32 2c  975,  978,  412,
2242b 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
2242c 32 2c 20 20 20 32 30 2c 0a 20 2f 2a 20 20 20 31  2,   20,. /*   1
2242d 30 30 20 2a 2f 20 20 20 34 31 32 2c 20 20 34 31  00 */   412,  41
2242e 32 2c 20 20 20 32 33 2c 20 20 20 33 34 2c 20 20  2,   23,   34,  
2242f 33 33 34 2c 20 20 34 37 35 2c 20 20 35 35 32 2c  334,  475,  552,
22430 20 20 35 39 33 2c 20 20 35 39 34 2c 20 20 35 38    593,  594,  58
22431 35 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  5,. /*   110 */ 
22432 20 20 32 31 32 2c 20 20 34 31 32 2c 20 20 32 38    212,  412,  28
22433 39 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20  9,  412,  412,  
22434 34 31 32 2c 20 20 31 33 35 2c 20 2d 31 36 34 2c  412,  135, -164,
22435 20 2d 31 31 35 2c 20 20 31 36 34 2c 0a 20 2f 2a   -115,  164,. /*
22436 20 20 20 31 32 30 20 2a 2f 20 20 20 34 30 37 2c     120 */   407,
22437 20 20 34 30 37 2c 20 20 33 35 30 2c 20 20 31 34    407,  350,  14
22438 31 2c 20 20 34 33 36 2c 20 20 31 36 33 2c 20 20  1,  436,  163,  
22439 35 39 36 2c 20 20 2d 39 30 2c 20 20 37 36 33 2c  596,  -90,  763,
2243a 20 20 32 31 38 2c 0a 20 2f 2a 20 20 20 31 33 30    218,. /*   130
2243b 20 2a 2f 20 20 20 37 36 35 2c 20 20 34 33 38 2c   */   765,  438,
2243c 20 20 35 38 36 2c 20 20 35 39 32 2c 20 20 35 39    586,  592,  59
2243d 35 2c 20 20 37 31 35 2c 20 20 37 31 38 2c 20 20  5,  715,  718,  
2243e 34 30 38 2c 20 20 37 32 33 2c 20 20 33 38 30 2c  408,  723,  380,
2243f 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
22440 36 33 34 2c 20 20 36 37 37 2c 20 20 37 38 37 2c  634,  677,  787,
22441 20 20 37 39 38 2c 20 20 31 34 34 2c 20 20 35 32    798,  144,  52
22442 39 2c 20 20 35 38 38 2c 20 20 2d 31 34 2c 20 20  9,  588,  -14,  
22443 20 20 30 2c 20 20 20 31 37 2c 0a 20 2f 2a 20 20    0,   17,. /*  
22444 20 31 35 30 20 2a 2f 20 20 20 32 34 34 2c 20 20   150 */   244,  
22445 31 35 35 2c 20 20 32 39 38 2c 20 20 31 35 35 2c  155,  298,  155,
22446 20 20 31 35 35 2c 20 20 34 31 38 2c 20 20 33 37    155,  418,  37
22447 32 2c 20 20 34 37 37 2c 20 20 34 39 30 2c 20 20  2,  477,  490,  
22448 34 39 34 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a  494,. /*   160 *
22449 2f 20 20 20 35 30 39 2c 20 20 35 32 36 2c 20 20  /   509,  526,  
2244a 35 39 30 2c 20 20 34 36 35 2c 20 20 34 39 34 2c  590,  465,  494,
2244b 20 20 37 33 30 2c 20 20 37 37 33 2c 20 20 37 34    730,  773,  74
2244c 33 2c 20 20 38 33 33 2c 20 20 37 39 32 2c 0a 20  3,  833,  792,. 
2244d 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 38 35  /*   170 */   85
2244e 34 2c 20 20 38 36 30 2c 20 20 31 35 35 2c 20 20  4,  860,  155,  
2244f 37 39 32 2c 20 20 38 36 34 2c 20 20 38 38 35 2c  792,  864,  885,
22450 20 20 38 34 38 2c 20 20 38 35 39 2c 20 20 38 36    848,  859,  86
22451 33 2c 20 20 38 39 31 2c 0a 20 2f 2a 20 20 20 31  3,  891,. /*   1
22452 38 30 20 2a 2f 20 20 20 39 30 37 2c 20 20 31 35  80 */   907,  15
22453 35 2c 20 20 39 31 34 2c 20 20 39 31 37 2c 20 20  5,  914,  917,  
22454 31 35 35 2c 20 20 39 32 37 2c 20 20 38 35 32 2c  155,  927,  852,
22455 20 20 38 38 36 2c 20 20 39 34 31 2c 20 20 39 36    886,  941,  96
22456 31 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  1,. /*   190 */ 
22457 20 20 31 35 35 2c 20 20 39 38 34 2c 20 20 39 39    155,  984,  99
22458 30 2c 20 20 39 39 31 2c 20 20 31 35 35 2c 20 20  0,  991,  155,  
22459 39 39 32 2c 20 20 39 39 35 2c 20 20 39 39 36 2c  992,  995,  996,
2245a 20 20 31 35 35 2c 20 20 31 35 35 2c 0a 20 2f 2a    155,  155,. /*
2245b 20 20 20 32 30 30 20 2a 2f 20 20 20 39 39 39 2c     200 */   999,
2245c 20 31 30 30 32 2c 20 31 30 30 33 2c 20 31 30 30   1002, 1003, 100
2245d 34 2c 20 31 30 30 35 2c 20 31 30 30 36 2c 20 20  4, 1005, 1006,  
2245e 39 36 32 2c 20 20 39 36 34 2c 20 20 39 32 35 2c  962,  964,  925,
2245f 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20 32 31 30    929,. /*   210
22460 20 2a 2f 20 20 20 39 33 33 2c 20 20 39 38 37 2c   */   933,  987,
22461 20 20 39 38 38 2c 20 20 39 38 39 2c 20 20 39 38    988,  989,  98
22462 35 2c 20 20 39 39 33 2c 20 20 39 39 37 2c 20 20  5,  993,  997,  
22463 39 39 38 2c 20 20 39 36 39 2c 20 20 39 37 30 2c  998,  969,  970,
22464 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 31  . /*   220 */  1
22465 30 32 31 2c 20 31 30 32 32 2c 20 31 30 32 34 2c  021, 1022, 1024,
22466 20 31 30 32 36 2c 20 31 30 32 30 2c 20 31 30 30   1026, 1020, 100
22467 30 2c 20 20 39 38 32 2c 20 20 39 38 33 2c 20 31  0,  982,  983, 1
22468 30 30 31 2c 20 31 30 30 37 2c 0a 20 2f 2a 20 20  001, 1007,. /*  
22469 20 32 33 30 20 2a 2f 20 20 31 30 30 38 2c 20 31   230 */  1008, 1
2246a 30 32 33 2c 20 20 39 36 36 2c 20 31 30 33 34 2c  023,  966, 1034,
2246b 20 31 30 33 35 2c 20 31 30 30 39 2c 20 31 30 31   1035, 1009, 101
2246c 30 2c 20 31 30 31 32 2c 20 31 30 31 33 2c 20 31  0, 1012, 1013, 1
2246d 30 32 35 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  025,. /*   240 *
2246e 2f 20 20 31 30 32 37 2c 20 31 30 31 35 2c 20 31  /  1027, 1015, 1
2246f 30 32 38 2c 20 31 30 32 39 2c 20 31 30 33 32 2c  028, 1029, 1032,
22470 20 31 30 33 31 2c 20 20 39 38 31 2c 20 31 30 34   1031,  981, 104
22471 38 2c 20 31 30 31 31 2c 20 31 30 34 39 2c 0a 20  8, 1011, 1049,. 
22472 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 31 30 33  /*   250 */  103
22473 33 2c 20 31 30 33 39 2c 20 31 30 34 30 2c 20 31  3, 1039, 1040, 1
22474 30 34 33 2c 20 31 30 34 35 2c 20 31 30 36 39 2c  043, 1045, 1069,
22475 20 31 30 37 30 2c 20 31 30 37 34 2c 20 31 30 37   1070, 1074, 107
22476 31 2c 20 31 30 37 35 2c 0a 20 2f 2a 20 20 20 32  1, 1075,. /*   2
22477 36 30 20 2a 2f 20 20 31 30 37 38 2c 20 31 30 37  60 */  1078, 107
22478 39 2c 20 31 30 31 34 2c 20 31 30 31 36 2c 20 31  9, 1014, 1016, 1
22479 30 38 31 2c 20 31 30 38 32 2c 20 31 30 34 34 2c  081, 1082, 1044,
2247a 20 31 30 35 31 2c 20 31 30 31 39 2c 20 31 30 34   1051, 1019, 104
2247b 32 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  2,. /*   270 */ 
2247c 20 31 30 38 36 2c 20 31 30 35 33 2c 20 31 30 35   1086, 1053, 105
2247d 36 2c 20 31 30 35 37 2c 20 31 30 36 32 2c 20 31  6, 1057, 1062, 1
2247e 30 34 37 2c 20 31 30 36 33 2c 20 31 30 33 36 2c  047, 1063, 1036,
2247f 20 31 30 39 30 2c 20 31 30 31 37 2c 0a 20 2f 2a   1090, 1017,. /*
22480 20 20 20 32 38 30 20 2a 2f 20 20 31 30 31 38 2c     280 */  1018,
22481 20 31 31 30 30 2c 20 31 31 30 35 2c 20 31 30 33   1100, 1105, 103
22482 37 2c 20 31 30 39 39 2c 20 31 30 38 34 2c 20 31  7, 1099, 1084, 1
22483 30 38 35 2c 20 31 30 33 38 2c 20 31 30 34 31 2c  085, 1038, 1041,
22484 20 31 30 34 36 2c 0a 7d 3b 0a 73 74 61 74 69 63   1046,.};.static
22485 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54   const YYACTIONT
22486 59 50 45 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d  YPE yy_default[]
22487 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
22488 2f 20 20 20 35 39 32 2c 20 20 38 31 36 2c 20 20  /   592,  816,  
22489 38 39 37 2c 20 20 37 30 37 2c 20 20 38 39 37 2c  897,  707,  897,
2248a 20 20 38 31 36 2c 20 20 38 39 37 2c 20 20 38 39    816,  897,  89
2248b 37 2c 20 20 38 34 33 2c 20 20 37 31 31 2c 0a 20  7,  843,  711,. 
2248c 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 38 37  /*    10 */   87
2248d 32 2c 20 20 38 31 34 2c 20 20 38 39 37 2c 20 20  2,  814,  897,  
2248e 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
2248f 20 20 37 38 39 2c 20 20 38 39 37 2c 20 20 38 34    789,  897,  84
22490 33 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 20  3,  897,. /*    
22491 32 30 20 2a 2f 20 20 20 36 32 33 2c 20 20 38 34  20 */   623,  84
22492 33 2c 20 20 38 34 33 2c 20 20 37 34 30 2c 20 20  3,  843,  740,  
22493 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
22494 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
22495 37 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  7,. /*    30 */ 
22496 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 37 34    897,  897,  74
22497 31 2c 20 20 38 39 37 2c 20 20 38 31 38 2c 20 20  1,  897,  818,  
22498 38 31 33 2c 20 20 38 30 39 2c 20 20 38 31 31 2c  813,  809,  811,
22499 20 20 38 31 30 2c 20 20 38 31 37 2c 0a 20 2f 2a    810,  817,. /*
2249a 20 20 20 20 34 30 20 2a 2f 20 20 20 37 34 32 2c      40 */   742,
2249b 20 20 37 33 31 2c 20 20 37 33 38 2c 20 20 37 34    731,  738,  74
2249c 35 2c 20 20 37 32 33 2c 20 20 38 35 36 2c 20 20  5,  723,  856,  
2249d 37 34 37 2c 20 20 37 34 38 2c 20 20 37 35 34 2c  747,  748,  754,
2249e 20 20 37 35 35 2c 0a 20 2f 2a 20 20 20 20 35 30    755,. /*    50
2249f 20 2a 2f 20 20 20 38 37 33 2c 20 20 38 37 31 2c   */   873,  871,
224a0 20 20 37 37 37 2c 20 20 37 37 36 2c 20 20 37 39    777,  776,  79
224a1 35 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  5,  897,  897,  
224a2 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224a3 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
224a4 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224a5 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
224a6 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
224a7 38 39 37 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20  897,  897,. /*  
224a8 20 20 37 30 20 2a 2f 20 20 20 38 39 37 2c 20 20    70 */   897,  
224a9 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224aa 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
224ab 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
224ac 38 39 37 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  897,. /*    80 *
224ad 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  /   897,  897,  
224ae 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224af 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
224b0 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 0a 20  7,  897,  897,. 
224b1 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 38 39  /*    90 */   89
224b2 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
224b3 38 39 37 2c 20 20 38 39 37 2c 20 20 37 37 39 2c  897,  897,  779,
224b4 20 20 38 30 30 2c 20 20 37 37 38 2c 20 20 37 38    800,  778,  78
224b5 38 2c 20 20 36 31 36 2c 0a 20 2f 2a 20 20 20 31  8,  616,. /*   1
224b6 30 30 20 2a 2f 20 20 20 37 38 30 2c 20 20 37 38  00 */   780,  78
224b7 31 2c 20 20 36 37 36 2c 20 20 36 31 31 2c 20 20  1,  676,  611,  
224b8 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224b9 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
224ba 37 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  7,. /*   110 */ 
224bb 20 20 38 39 37 2c 20 20 37 38 32 2c 20 20 38 39    897,  782,  89
224bc 37 2c 20 20 37 38 33 2c 20 20 37 39 36 2c 20 20  7,  783,  796,  
224bd 37 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  797,  897,  897,
224be 20 20 38 39 37 2c 20 20 38 39 37 2c 0a 20 2f 2a    897,  897,. /*
224bf 20 20 20 31 32 30 20 2a 2f 20 20 20 38 39 37 2c     120 */   897,
224c0 20 20 38 39 37 2c 20 20 35 39 32 2c 20 20 37 30    897,  592,  70
224c1 37 2c 20 20 38 39 37 2c 20 20 37 30 37 2c 20 20  7,  897,  707,  
224c2 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224c3 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 31 33 30    897,. /*   130
224c4 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c   */   897,  897,
224c5 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
224c6 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
224c7 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224c8 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
224c9 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224ca 20 20 38 39 37 2c 20 20 37 30 31 2c 20 20 37 31    897,  701,  71
224cb 31 2c 20 20 38 39 30 2c 20 20 38 39 37 2c 20 20  1,  890,  897,  
224cc 38 39 37 2c 20 20 36 36 37 2c 0a 20 2f 2a 20 20  897,  667,. /*  
224cd 20 31 35 30 20 2a 2f 20 20 20 38 39 37 2c 20 20   150 */   897,  
224ce 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224cf 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
224d0 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
224d1 38 39 37 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a  897,. /*   160 *
224d2 2f 20 20 20 35 39 39 2c 20 20 35 39 37 2c 20 20  /   599,  597,  
224d3 38 39 37 2c 20 20 36 39 39 2c 20 20 38 39 37 2c  897,  699,  897,
224d4 20 20 38 39 37 2c 20 20 36 32 35 2c 20 20 38 39    897,  625,  89
224d5 37 2c 20 20 38 39 37 2c 20 20 37 30 39 2c 0a 20  7,  897,  709,. 
224d6 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 38 39  /*   170 */   89
224d7 37 2c 20 20 38 39 37 2c 20 20 37 31 34 2c 20 20  7,  897,  714,  
224d8 37 31 35 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  715,  897,  897,
224d9 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
224da 37 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 31  7,  897,. /*   1
224db 38 30 20 2a 2f 20 20 20 38 39 37 2c 20 20 36 31  80 */   897,  61
224dc 33 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  3,  897,  897,  
224dd 36 38 38 2c 20 20 38 39 37 2c 20 20 38 34 39 2c  688,  897,  849,
224de 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
224df 37 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  7,. /*   190 */ 
224e0 20 20 38 36 33 2c 20 20 38 39 37 2c 20 20 38 39    863,  897,  89
224e1 37 2c 20 20 38 39 37 2c 20 20 38 36 31 2c 20 20  7,  897,  861,  
224e2 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
224e3 20 20 36 39 30 2c 20 20 37 35 30 2c 0a 20 2f 2a    690,  750,. /*
224e4 20 20 20 32 30 30 20 2a 2f 20 20 20 38 33 30 2c     200 */   830,
224e5 20 20 38 39 37 2c 20 20 38 37 36 2c 20 20 38 37    897,  876,  87
224e6 38 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  8,  897,  897,  
224e7 36 39 39 2c 20 20 37 30 38 2c 20 20 38 39 37 2c  699,  708,  897,
224e8 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 32 31 30    897,. /*   210
224e9 20 2a 2f 20 20 20 38 31 32 2c 20 20 37 33 34 2c   */   812,  734,
224ea 20 20 37 33 34 2c 20 20 37 33 34 2c 20 20 36 34    734,  734,  64
224eb 36 2c 20 20 37 33 34 2c 20 20 37 33 34 2c 20 20  6,  734,  734,  
224ec 36 34 39 2c 20 20 37 34 34 2c 20 20 37 34 34 2c  649,  744,  744,
224ed 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20  . /*   220 */   
224ee 35 39 36 2c 20 20 35 39 36 2c 20 20 35 39 36 2c  596,  596,  596,
224ef 20 20 35 39 36 2c 20 20 36 36 36 2c 20 20 38 39    596,  666,  89
224f0 37 2c 20 20 37 34 34 2c 20 20 37 33 35 2c 20 20  7,  744,  735,  
224f1 37 33 37 2c 20 20 37 32 37 2c 0a 20 2f 2a 20 20  737,  727,. /*  
224f2 20 32 33 30 20 2a 2f 20 20 20 37 33 39 2c 20 20   230 */   739,  
224f3 38 39 37 2c 20 20 38 39 37 2c 20 20 37 31 36 2c  897,  897,  716,
224f4 20 20 37 31 36 2c 20 20 37 32 34 2c 20 20 37 32    716,  724,  72
224f5 36 2c 20 20 37 32 34 2c 20 20 37 32 36 2c 20 20  6,  724,  726,  
224f6 36 37 38 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  678,. /*   240 *
224f7 2f 20 20 20 36 37 38 2c 20 20 36 36 33 2c 20 20  /   678,  663,  
224f8 36 37 38 2c 20 20 36 34 39 2c 20 20 36 37 38 2c  678,  649,  678,
224f9 20 20 38 32 32 2c 20 20 38 32 37 2c 20 20 38 39    822,  827,  89
224fa 37 2c 20 20 38 32 37 2c 20 20 38 39 37 2c 0a 20  7,  827,  897,. 
224fb 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 36 36  /*   250 */   66
224fc 33 2c 20 20 36 37 38 2c 20 20 36 37 38 2c 20 20  3,  678,  678,  
224fd 36 37 38 2c 20 20 38 32 32 2c 20 20 36 30 38 2c  678,  822,  608,
224fe 20 20 37 31 36 2c 20 20 36 30 38 2c 20 20 37 31    716,  608,  71
224ff 36 2c 20 20 36 30 38 2c 0a 20 2f 2a 20 20 20 32  6,  608,. /*   2
22500 36 30 20 2a 2f 20 20 20 37 31 36 2c 20 20 37 31  60 */   716,  71
22501 36 2c 20 20 38 35 33 2c 20 20 38 35 35 2c 20 20  6,  853,  855,  
22502 36 30 38 2c 20 20 37 31 36 2c 20 20 36 38 30 2c  608,  716,  680,
22503 20 20 36 38 30 2c 20 20 37 35 36 2c 20 20 37 34    680,  756,  74
22504 34 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  4,. /*   270 */ 
22505 20 20 37 31 36 2c 20 20 36 38 37 2c 20 20 36 38    716,  687,  68
22506 37 2c 20 20 36 38 37 2c 20 20 36 38 37 2c 20 20  7,  687,  687,  
22507 37 34 34 2c 20 20 36 38 30 2c 20 20 37 35 36 2c  744,  680,  756,
22508 20 20 37 31 36 2c 20 20 38 37 35 2c 0a 20 2f 2a    716,  875,. /*
22509 20 20 20 32 38 30 20 2a 2f 20 20 20 38 37 35 2c     280 */   875,
2250a 20 20 37 31 36 2c 20 20 37 31 36 2c 20 20 38 38    716,  716,  88
2250b 33 2c 20 20 36 33 33 2c 20 20 36 35 31 2c 20 20  3,  633,  651,  
2250c 36 35 31 2c 20 20 38 35 38 2c 20 20 38 39 30 2c  651,  858,  890,
2250d 20 20 38 39 35 2c 0a 20 2f 2a 20 20 20 32 39 30    895,. /*   290
2250e 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c   */   897,  897,
2250f 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 37 36    897,  897,  76
22510 33 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  3,  897,  897,  
22511 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
22512 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20  . /*   300 */   
22513 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
22514 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
22515 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
22516 38 33 36 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20  836,  897,. /*  
22517 20 33 31 30 20 2a 2f 20 20 20 38 39 37 2c 20 20   310 */   897,  
22518 38 39 37 2c 20 20 38 39 37 2c 20 20 37 36 38 2c  897,  897,  768,
22519 20 20 37 36 34 2c 20 20 38 39 37 2c 20 20 37 36    764,  897,  76
2251a 35 2c 20 20 38 39 37 2c 20 20 36 39 33 2c 20 20  5,  897,  693,  
2251b 38 39 37 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a  897,. /*   320 *
2251c 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  /   897,  897,  
2251d 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
2251e 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
2251f 37 2c 20 20 38 39 37 2c 20 20 38 31 35 2c 0a 20  7,  897,  815,. 
22520 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 38 39  /*   330 */   89
22521 37 2c 20 20 37 32 38 2c 20 20 38 39 37 2c 20 20  7,  728,  897,  
22522 37 33 36 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  736,  897,  897,
22523 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
22524 37 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 33  7,  897,. /*   3
22525 34 30 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 39  40 */   897,  89
22526 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
22527 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
22528 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
22529 37 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20  7,. /*   350 */ 
2252a 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
2252b 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
2252c 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
2252d 20 20 38 35 31 2c 20 20 38 35 32 2c 0a 20 2f 2a    851,  852,. /*
2252e 20 20 20 33 36 30 20 2a 2f 20 20 20 38 39 37 2c     360 */   897,
2252f 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
22530 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
22531 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
22532 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 33 37 30    897,. /*   370
22533 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c   */   897,  897,
22534 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
22535 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
22536 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
22537 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20  . /*   380 */   
22538 38 39 37 2c 20 20 38 39 37 2c 20 20 38 38 32 2c  897,  897,  882,
22539 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 38    897,  897,  88
2253a 35 2c 20 20 35 39 33 2c 20 20 38 39 37 2c 20 20  5,  593,  897,  
2253b 35 38 37 2c 20 20 35 39 30 2c 0a 20 2f 2a 20 20  587,  590,. /*  
2253c 20 33 39 30 20 2a 2f 20 20 20 35 38 39 2c 20 20   390 */   589,  
2253d 35 39 31 2c 20 20 35 39 35 2c 20 20 35 39 38 2c  591,  595,  598,
2253e 20 20 36 32 30 2c 20 20 36 32 31 2c 20 20 36 32    620,  621,  62
2253f 32 2c 20 20 36 30 30 2c 20 20 36 30 31 2c 20 20  2,  600,  601,  
22540 36 30 32 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a  602,. /*   400 *
22541 2f 20 20 20 36 30 33 2c 20 20 36 30 34 2c 20 20  /   603,  604,  
22542 36 30 35 2c 20 20 36 30 36 2c 20 20 36 31 32 2c  605,  606,  612,
22543 20 20 36 31 34 2c 20 20 36 33 32 2c 20 20 36 33    614,  632,  63
22544 34 2c 20 20 36 31 38 2c 20 20 36 33 36 2c 0a 20  4,  618,  636,. 
22545 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 36 39  /*   410 */   69
22546 37 2c 20 20 36 39 38 2c 20 20 37 36 30 2c 20 20  7,  698,  760,  
22547 36 39 31 2c 20 20 36 39 32 2c 20 20 36 39 36 2c  691,  692,  696,
22548 20 20 37 37 31 2c 20 20 37 36 32 2c 20 20 37 36    771,  762,  76
22549 36 2c 20 20 37 36 37 2c 0a 20 2f 2a 20 20 20 34  6,  767,. /*   4
2254a 32 30 20 2a 2f 20 20 20 37 36 39 2c 20 20 37 37  20 */   769,  77
2254b 30 2c 20 20 37 38 34 2c 20 20 37 38 35 2c 20 20  0,  784,  785,  
2254c 37 38 37 2c 20 20 37 39 33 2c 20 20 37 39 39 2c  787,  793,  799,
2254d 20 20 38 30 32 2c 20 20 37 38 36 2c 20 20 37 39    802,  786,  79
2254e 31 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20  1,. /*   430 */ 
2254f 20 20 37 39 32 2c 20 20 37 39 34 2c 20 20 37 39    792,  794,  79
22550 38 2c 20 20 38 30 31 2c 20 20 36 39 34 2c 20 20  8,  801,  694,  
22551 36 39 35 2c 20 20 38 30 35 2c 20 20 36 31 39 2c  695,  805,  619,
22552 20 20 36 32 36 2c 20 20 36 32 37 2c 0a 20 2f 2a    626,  627,. /*
22553 20 20 20 34 34 30 20 2a 2f 20 20 20 36 33 30 2c     440 */   630,
22554 20 20 36 33 31 2c 20 20 38 33 39 2c 20 20 38 34    631,  839,  84
22555 31 2c 20 20 38 34 30 2c 20 20 38 34 32 2c 20 20  1,  840,  842,  
22556 36 32 39 2c 20 20 36 32 38 2c 20 20 37 37 32 2c  629,  628,  772,
22557 20 20 37 37 35 2c 0a 20 2f 2a 20 20 20 34 35 30    775,. /*   450
22558 20 2a 2f 20 20 20 38 30 37 2c 20 20 38 30 38 2c   */   807,  808,
22559 20 20 38 36 34 2c 20 20 38 36 35 2c 20 20 38 36    864,  865,  86
2255a 36 2c 20 20 38 36 37 2c 20 20 38 36 38 2c 20 20  6,  867,  868,  
2255b 38 30 33 2c 20 20 37 31 37 2c 20 20 38 30 36 2c  803,  717,  806,
2255c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20  . /*   460 */   
2255d 37 39 30 2c 20 20 37 32 39 2c 20 20 37 33 32 2c  790,  729,  732,
2255e 20 20 37 33 33 2c 20 20 37 33 30 2c 20 20 37 30    733,  730,  70
2255f 30 2c 20 20 37 31 30 2c 20 20 37 31 39 2c 20 20  0,  710,  719,  
22560 37 32 30 2c 20 20 37 32 31 2c 0a 20 2f 2a 20 20  720,  721,. /*  
22561 20 34 37 30 20 2a 2f 20 20 20 37 32 32 2c 20 20   470 */   722,  
22562 37 30 35 2c 20 20 37 30 36 2c 20 20 37 31 32 2c  705,  706,  712,
22563 20 20 37 32 35 2c 20 20 37 35 38 2c 20 20 37 35    725,  758,  75
22564 39 2c 20 20 37 31 33 2c 20 20 37 30 32 2c 20 20  9,  713,  702,  
22565 37 30 33 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a  703,. /*   480 *
22566 2f 20 20 20 37 30 34 2c 20 20 38 30 34 2c 20 20  /   704,  804,  
22567 37 36 31 2c 20 20 37 37 33 2c 20 20 37 37 34 2c  761,  773,  774,
22568 20 20 36 33 37 2c 20 20 36 33 38 2c 20 20 37 36    637,  638,  76
22569 38 2c 20 20 36 33 39 2c 20 20 36 34 30 2c 0a 20  8,  639,  640,. 
2256a 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 36 34  /*   490 */   64
2256b 31 2c 20 20 36 37 39 2c 20 20 36 38 32 2c 20 20  1,  679,  682,  
2256c 36 38 33 2c 20 20 36 38 34 2c 20 20 36 34 32 2c  683,  684,  642,
2256d 20 20 36 36 31 2c 20 20 36 36 34 2c 20 20 36 36    661,  664,  66
2256e 35 2c 20 20 36 34 33 2c 0a 20 2f 2a 20 20 20 35  5,  643,. /*   5
2256f 30 30 20 2a 2f 20 20 20 36 35 30 2c 20 20 36 34  00 */   650,  64
22570 34 2c 20 20 36 34 35 2c 20 20 36 35 32 2c 20 20  4,  645,  652,  
22571 36 35 33 2c 20 20 36 35 34 2c 20 20 36 35 37 2c  653,  654,  657,
22572 20 20 36 35 38 2c 20 20 36 35 39 2c 20 20 36 36    658,  659,  66
22573 30 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20  0,. /*   510 */ 
22574 20 20 36 35 35 2c 20 20 36 35 36 2c 20 20 38 32    655,  656,  82
22575 33 2c 20 20 38 32 34 2c 20 20 38 32 38 2c 20 20  3,  824,  828,  
22576 38 32 36 2c 20 20 38 32 35 2c 20 20 36 34 37 2c  826,  825,  647,
22577 20 20 36 34 38 2c 20 20 36 36 32 2c 0a 20 2f 2a    648,  662,. /*
22578 20 20 20 35 32 30 20 2a 2f 20 20 20 36 33 35 2c     520 */   635,
22579 20 20 36 32 34 2c 20 20 36 31 37 2c 20 20 36 36    624,  617,  66
2257a 38 2c 20 20 36 37 31 2c 20 20 36 37 32 2c 20 20  8,  671,  672,  
2257b 36 37 33 2c 20 20 36 37 34 2c 20 20 36 37 35 2c  673,  674,  675,
2257c 20 20 36 37 37 2c 0a 20 2f 2a 20 20 20 35 33 30    677,. /*   530
2257d 20 2a 2f 20 20 20 36 36 39 2c 20 20 36 37 30 2c   */   669,  670,
2257e 20 20 36 31 35 2c 20 20 36 30 37 2c 20 20 36 30    615,  607,  60
2257f 39 2c 20 20 37 31 38 2c 20 20 38 34 35 2c 20 20  9,  718,  845,  
22580 38 35 34 2c 20 20 38 35 30 2c 20 20 38 34 36 2c  854,  850,  846,
22581 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20  . /*   540 */   
22582 38 34 37 2c 20 20 38 34 38 2c 20 20 36 31 30 2c  847,  848,  610,
22583 20 20 38 31 39 2c 20 20 38 32 30 2c 20 20 36 38    819,  820,  68
22584 31 2c 20 20 37 35 32 2c 20 20 37 35 33 2c 20 20  1,  752,  753,  
22585 38 34 34 2c 20 20 38 35 37 2c 0a 20 2f 2a 20 20  844,  857,. /*  
22586 20 35 35 30 20 2a 2f 20 20 20 38 35 39 2c 20 20   550 */   859,  
22587 37 35 37 2c 20 20 38 36 30 2c 20 20 38 36 32 2c  757,  860,  862,
22588 20 20 38 38 37 2c 20 20 36 38 35 2c 20 20 36 38    887,  685,  68
22589 36 2c 20 20 36 38 39 2c 20 20 38 32 39 2c 20 20  6,  689,  829,  
2258a 38 36 39 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a  869,. /*   560 *
2258b 2f 20 20 20 37 34 33 2c 20 20 37 34 36 2c 20 20  /   743,  746,  
2258c 37 34 39 2c 20 20 37 35 31 2c 20 20 38 33 31 2c  749,  751,  831,
2258d 20 20 38 33 32 2c 20 20 38 33 33 2c 20 20 38 33    832,  833,  83
2258e 34 2c 20 20 38 33 37 2c 20 20 38 33 38 2c 0a 20  4,  837,  838,. 
2258f 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 38 33  /*   570 */   83
22590 35 2c 20 20 38 37 30 2c 20 20 38 37 34 2c 20 20  5,  870,  874,  
22591 38 37 37 2c 20 20 38 37 39 2c 20 20 38 38 30 2c  877,  879,  880,
22592 20 20 38 38 31 2c 20 20 38 38 34 2c 20 20 38 38    881,  884,  88
22593 36 2c 20 20 38 39 31 2c 0a 20 2f 2a 20 20 20 35  6,  891,. /*   5
22594 38 30 20 2a 2f 20 20 20 38 39 32 2c 20 20 38 39  80 */   892,  89
22595 33 2c 20 20 38 39 36 2c 20 20 38 39 34 2c 20 20  3,  896,  894,  
22596 35 39 34 2c 20 20 35 38 38 2c 0a 7d 3b 0a 23 64  594,  588,.};.#d
22597 65 66 69 6e 65 20 59 59 5f 53 5a 5f 41 43 54 54  efine YY_SZ_ACTT
22598 41 42 20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28  AB (int)(sizeof(
22599 79 79 5f 61 63 74 69 6f 6e 29 2f 73 69 7a 65 6f  yy_action)/sizeo
2259a 66 28 79 79 5f 61 63 74 69 6f 6e 5b 30 5d 29 29  f(yy_action[0]))
2259b 0a 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 61  ../* The next ta
2259c 62 6c 65 20 6d 61 70 73 20 74 6f 6b 65 6e 73 20  ble maps tokens 
2259d 69 6e 74 6f 20 66 61 6c 6c 62 61 63 6b 20 74 6f  into fallback to
2259e 6b 65 6e 73 2e 20 20 49 66 20 61 20 63 6f 6e 73  kens.  If a cons
2259f 74 72 75 63 74 0a 2a 2a 20 6c 69 6b 65 20 74 68  truct.** like th
225a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 20  e following:.** 
225a1 0a 2a 2a 20 20 20 20 20 20 25 66 61 6c 6c 62 61  .**      %fallba
225a2 63 6b 20 49 44 20 58 20 59 20 5a 2e 0a 2a 2a 0a  ck ID X Y Z..**.
225a3 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68  ** appears in th
225a4 65 20 67 72 61 6d 6d 65 72 2c 20 74 68 65 6e 20  e grammer, then 
225a5 49 44 20 62 65 63 6f 6d 65 73 20 61 20 66 61 6c  ID becomes a fal
225a6 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 66 6f 72 20  lback token for 
225a7 58 2c 20 59 2c 0a 2a 2a 20 61 6e 64 20 5a 2e 20  X, Y,.** and Z. 
225a8 20 57 68 65 6e 65 76 65 72 20 6f 6e 65 20 6f 66   Whenever one of
225a9 20 74 68 65 20 74 6f 6b 65 6e 73 20 58 2c 20 59   the tokens X, Y
225aa 2c 20 6f 72 20 5a 20 69 73 20 69 6e 70 75 74 20  , or Z is input 
225ab 74 6f 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  to the parser.**
225ac 20 62 75 74 20 69 74 20 64 6f 65 73 20 6e 6f 74   but it does not
225ad 20 70 61 72 73 65 2c 20 74 68 65 20 74 79 70 65   parse, the type
225ae 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 73   of the token is
225af 20 63 68 61 6e 67 65 64 20 74 6f 20 49 44 20 61   changed to ID a
225b0 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20  nd.** the parse 
225b1 69 73 20 72 65 74 72 69 65 64 20 62 65 66 6f 72  is retried befor
225b2 65 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 68  e an error is th
225b3 72 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rown..*/.#ifdef 
225b4 59 59 46 41 4c 4c 42 41 43 4b 0a 73 74 61 74 69  YYFALLBACK.stati
225b5 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54 59  c const YYCODETY
225b6 50 45 20 79 79 46 61 6c 6c 62 61 63 6b 5b 5d 20  PE yyFallback[] 
225b7 3d 20 7b 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20  = {.    0,  /*  
225b8 20 20 20 20 20 20 20 20 24 20 3d 3e 20 6e 6f 74          $ => not
225b9 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
225ba 2f 2a 20 20 20 20 20 20 20 53 45 4d 49 20 3d 3e  /*       SEMI =>
225bb 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
225bc 33 2c 20 20 2f 2a 20 20 20 20 45 58 50 4c 41 49  3,  /*    EXPLAI
225bd 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  N => ID */.   23
225be 2c 20 20 2f 2a 20 20 20 20 20 20 51 55 45 52 59  ,  /*      QUERY
225bf 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
225c0 20 20 2f 2a 20 20 20 20 20 20 20 50 4c 41 4e 20    /*       PLAN 
225c1 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
225c2 20 2f 2a 20 20 20 20 20 20 42 45 47 49 4e 20 3d   /*      BEGIN =
225c3 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
225c4 2f 2a 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 3d  /* TRANSACTION =
225c5 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
225c6 32 33 2c 20 20 2f 2a 20 20 20 44 45 46 45 52 52  23,  /*   DEFERR
225c7 45 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ED => ID */.   2
225c8 33 2c 20 20 2f 2a 20 20 49 4d 4d 45 44 49 41 54  3,  /*  IMMEDIAT
225c9 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  E => ID */.   23
225ca 2c 20 20 2f 2a 20 20 45 58 43 4c 55 53 49 56 45  ,  /*  EXCLUSIVE
225cb 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c   => ID */.    0,
225cc 20 20 2f 2a 20 20 20 20 20 43 4f 4d 4d 49 54 20    /*     COMMIT 
225cd 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
225ce 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   23,  /*        
225cf 45 4e 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  END => ID */.   
225d0 20 30 2c 20 20 2f 2a 20 20 20 52 4f 4c 4c 42 41   0,  /*   ROLLBA
225d1 43 4b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  CK => nothing */
225d2 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
225d3 43 52 45 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e  CREATE => nothin
225d4 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  g */.    0,  /* 
225d5 20 20 20 20 20 54 41 42 4c 45 20 3d 3e 20 6e 6f       TABLE => no
225d6 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 33 2c 20  thing */.   23, 
225d7 20 2f 2a 20 20 20 20 20 20 20 20 20 49 46 20 3d   /*         IF =
225d8 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
225d9 2f 2a 20 20 20 20 20 20 20 20 4e 4f 54 20 3d 3e  /*        NOT =>
225da 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
225db 30 2c 20 20 2f 2a 20 20 20 20 20 45 58 49 53 54  0,  /*     EXIST
225dc 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  S => nothing */.
225dd 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
225de 20 54 45 4d 50 20 3d 3e 20 49 44 20 2a 2f 0a 20   TEMP => ID */. 
225df 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
225e0 20 20 4c 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    LP => nothing 
225e1 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
225e2 20 20 20 20 20 20 52 50 20 3d 3e 20 6e 6f 74 68        RP => noth
225e3 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
225e4 2a 20 20 20 20 20 20 20 20 20 41 53 20 3d 3e 20  *         AS => 
225e5 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
225e6 2c 20 20 2f 2a 20 20 20 20 20 20 43 4f 4d 4d 41  ,  /*      COMMA
225e7 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
225e8 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
225e9 20 20 49 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    ID => nothing 
225ea 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
225eb 20 20 20 41 42 4f 52 54 20 3d 3e 20 49 44 20 2a     ABORT => ID *
225ec 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
225ed 20 20 41 46 54 45 52 20 3d 3e 20 49 44 20 2a 2f    AFTER => ID */
225ee 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 41  .   23,  /*    A
225ef 4e 41 4c 59 5a 45 20 3d 3e 20 49 44 20 2a 2f 0a  NALYZE => ID */.
225f0 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
225f1 20 20 41 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20    ASC => ID */. 
225f2 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 41 54    23,  /*     AT
225f3 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TACH => ID */.  
225f4 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 42 45 46   23,  /*     BEF
225f5 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ORE => ID */.   
225f6 32 33 2c 20 20 2f 2a 20 20 20 20 43 41 53 43 41  23,  /*    CASCA
225f7 44 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  DE => ID */.   2
225f8 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 43 41 53  3,  /*       CAS
225f9 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  T => ID */.   23
225fa 2c 20 20 2f 2a 20 20 20 43 4f 4e 46 4c 49 43 54  ,  /*   CONFLICT
225fb 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
225fc 20 20 2f 2a 20 20 20 44 41 54 41 42 41 53 45 20    /*   DATABASE 
225fd 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
225fe 20 2f 2a 20 20 20 20 20 20 20 44 45 53 43 20 3d   /*       DESC =
225ff 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
22600 2f 2a 20 20 20 20 20 44 45 54 41 43 48 20 3d 3e  /*     DETACH =>
22601 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
22602 2a 20 20 20 20 20 20 20 45 41 43 48 20 3d 3e 20  *       EACH => 
22603 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
22604 20 20 20 20 20 20 20 46 41 49 4c 20 3d 3e 20 49         FAIL => I
22605 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
22606 20 20 20 20 20 20 20 46 4f 52 20 3d 3e 20 49 44         FOR => ID
22607 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
22608 20 20 20 49 47 4e 4f 52 45 20 3d 3e 20 49 44 20     IGNORE => ID 
22609 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 49  */.   23,  /*  I
2260a 4e 49 54 49 41 4c 4c 59 20 3d 3e 20 49 44 20 2a  NITIALLY => ID *
2260b 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
2260c 49 4e 53 54 45 41 44 20 3d 3e 20 49 44 20 2a 2f  INSTEAD => ID */
2260d 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 4c  .   23,  /*    L
2260e 49 4b 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a  IKE_KW => ID */.
2260f 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
22610 4d 41 54 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20  MATCH => ID */. 
22611 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20    23,  /*       
22612 20 4b 45 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20   KEY => ID */.  
22613 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   23,  /*        
22614 20 4f 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20   OF => ID */.   
22615 32 33 2c 20 20 2f 2a 20 20 20 20 20 4f 46 46 53  23,  /*     OFFS
22616 45 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ET => ID */.   2
22617 33 2c 20 20 2f 2a 20 20 20 20 20 50 52 41 47 4d  3,  /*     PRAGM
22618 41 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  A => ID */.   23
22619 2c 20 20 2f 2a 20 20 20 20 20 20 52 41 49 53 45  ,  /*      RAISE
2261a 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
2261b 20 20 2f 2a 20 20 20 20 52 45 50 4c 41 43 45 20    /*    REPLACE 
2261c 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
2261d 20 2f 2a 20 20 20 52 45 53 54 52 49 43 54 20 3d   /*   RESTRICT =
2261e 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
2261f 2f 2a 20 20 20 20 20 20 20 20 52 4f 57 20 3d 3e  /*        ROW =>
22620 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
22621 2a 20 20 20 20 54 52 49 47 47 45 52 20 3d 3e 20  *    TRIGGER => 
22622 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
22623 20 20 20 20 20 56 41 43 55 55 4d 20 3d 3e 20 49       VACUUM => I
22624 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
22625 20 20 20 20 20 20 56 49 45 57 20 3d 3e 20 49 44        VIEW => ID
22626 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
22627 20 20 56 49 52 54 55 41 4c 20 3d 3e 20 49 44 20    VIRTUAL => ID 
22628 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
22629 20 52 45 49 4e 44 45 58 20 3d 3e 20 49 44 20 2a   REINDEX => ID *
2262a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
2262b 20 52 45 4e 41 4d 45 20 3d 3e 20 49 44 20 2a 2f   RENAME => ID */
2262c 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 43 54  .   23,  /*   CT
2262d 49 4d 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a  IME_KW => ID */.
2262e 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2262f 20 20 41 4e 59 20 3d 3e 20 6e 6f 74 68 69 6e 67    ANY => nothing
22630 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
22631 20 20 20 20 20 20 20 4f 52 20 3d 3e 20 6e 6f 74         OR => not
22632 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
22633 2f 2a 20 20 20 20 20 20 20 20 41 4e 44 20 3d 3e  /*        AND =>
22634 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
22635 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49  0,  /*         I
22636 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  S => nothing */.
22637 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 42 45      0,  /*    BE
22638 54 57 45 45 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67  TWEEN => nothing
22639 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2263a 20 20 20 20 20 20 20 49 4e 20 3d 3e 20 6e 6f 74         IN => not
2263b 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2263c 2f 2a 20 20 20 20 20 49 53 4e 55 4c 4c 20 3d 3e  /*     ISNULL =>
2263d 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2263e 30 2c 20 20 2f 2a 20 20 20 20 4e 4f 54 4e 55 4c  0,  /*    NOTNUL
2263f 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  L => nothing */.
22640 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
22641 20 20 20 4e 45 20 3d 3e 20 6e 6f 74 68 69 6e 67     NE => nothing
22642 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
22643 20 20 20 20 20 20 20 45 51 20 3d 3e 20 6e 6f 74         EQ => not
22644 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
22645 2f 2a 20 20 20 20 20 20 20 20 20 47 54 20 3d 3e  /*         GT =>
22646 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
22647 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c  0,  /*         L
22648 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
22649 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2264a 20 20 20 4c 54 20 3d 3e 20 6e 6f 74 68 69 6e 67     LT => nothing
2264b 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2264c 20 20 20 20 20 20 20 47 45 20 3d 3e 20 6e 6f 74         GE => not
2264d 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2264e 2f 2a 20 20 20 20 20 45 53 43 41 50 45 20 3d 3e  /*     ESCAPE =>
2264f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
22650 30 2c 20 20 2f 2a 20 20 20 20 20 42 49 54 41 4e  0,  /*     BITAN
22651 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  D => nothing */.
22652 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
22653 42 49 54 4f 52 20 3d 3e 20 6e 6f 74 68 69 6e 67  BITOR => nothing
22654 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
22655 20 20 20 4c 53 48 49 46 54 20 3d 3e 20 6e 6f 74     LSHIFT => not
22656 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
22657 2f 2a 20 20 20 20 20 52 53 48 49 46 54 20 3d 3e  /*     RSHIFT =>
22658 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
22659 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 50 4c 55  0,  /*       PLU
2265a 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  S => nothing */.
2265b 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2265c 4d 49 4e 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67  MINUS => nothing
2265d 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2265e 20 20 20 20 20 53 54 41 52 20 3d 3e 20 6e 6f 74       STAR => not
2265f 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
22660 2f 2a 20 20 20 20 20 20 53 4c 41 53 48 20 3d 3e  /*      SLASH =>
22661 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
22662 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 52 45  0,  /*        RE
22663 4d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  M => nothing */.
22664 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 43      0,  /*     C
22665 4f 4e 43 41 54 20 3d 3e 20 6e 6f 74 68 69 6e 67  ONCAT => nothing
22666 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
22667 20 20 43 4f 4c 4c 41 54 45 20 3d 3e 20 6e 6f 74    COLLATE => not
22668 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
22669 2f 2a 20 20 20 20 20 55 4d 49 4e 55 53 20 3d 3e  /*     UMINUS =>
2266a 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2266b 30 2c 20 20 2f 2a 20 20 20 20 20 20 55 50 4c 55  0,  /*      UPLU
2266c 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  S => nothing */.
2266d 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 42      0,  /*     B
2266e 49 54 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67  ITNOT => nothing
2266f 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
22670 20 20 20 53 54 52 49 4e 47 20 3d 3e 20 6e 6f 74     STRING => not
22671 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
22672 2f 2a 20 20 20 20 4a 4f 49 4e 5f 4b 57 20 3d 3e  /*    JOIN_KW =>
22673 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
22674 30 2c 20 20 2f 2a 20 43 4f 4e 53 54 52 41 49 4e  0,  /* CONSTRAIN
22675 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  T => nothing */.
22676 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 44 45      0,  /*    DE
22677 46 41 55 4c 54 20 3d 3e 20 6e 6f 74 68 69 6e 67  FAULT => nothing
22678 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
22679 20 20 20 20 20 4e 55 4c 4c 20 3d 3e 20 6e 6f 74       NULL => not
2267a 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2267b 2f 2a 20 20 20 20 50 52 49 4d 41 52 59 20 3d 3e  /*    PRIMARY =>
2267c 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2267d 30 2c 20 20 2f 2a 20 20 20 20 20 55 4e 49 51 55  0,  /*     UNIQU
2267e 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
2267f 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
22680 43 48 45 43 4b 20 3d 3e 20 6e 6f 74 68 69 6e 67  CHECK => nothing
22681 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 52   */.    0,  /* R
22682 45 46 45 52 45 4e 43 45 53 20 3d 3e 20 6e 6f 74  EFERENCES => not
22683 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
22684 2f 2a 20 20 20 41 55 54 4f 49 4e 43 52 20 3d 3e  /*   AUTOINCR =>
22685 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
22686 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4f  0,  /*         O
22687 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
22688 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 44      0,  /*     D
22689 45 4c 45 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67  ELETE => nothing
2268a 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2268b 20 20 20 55 50 44 41 54 45 20 3d 3e 20 6e 6f 74     UPDATE => not
2268c 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2268d 2f 2a 20 20 20 20 20 49 4e 53 45 52 54 20 3d 3e  /*     INSERT =>
2268e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2268f 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 53 45  0,  /*        SE
22690 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  T => nothing */.
22691 20 20 20 20 30 2c 20 20 2f 2a 20 44 45 46 45 52      0,  /* DEFER
22692 52 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67  RABLE => nothing
22693 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
22694 20 20 46 4f 52 45 49 47 4e 20 3d 3e 20 6e 6f 74    FOREIGN => not
22695 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
22696 2f 2a 20 20 20 20 20 20 20 44 52 4f 50 20 3d 3e  /*       DROP =>
22697 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
22698 30 2c 20 20 2f 2a 20 20 20 20 20 20 55 4e 49 4f  0,  /*      UNIO
22699 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
2269a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2269b 20 20 41 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67    ALL => nothing
2269c 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2269d 20 20 20 45 58 43 45 50 54 20 3d 3e 20 6e 6f 74     EXCEPT => not
2269e 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2269f 2f 2a 20 20 49 4e 54 45 52 53 45 43 54 20 3d 3e  /*  INTERSECT =>
226a0 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
226a1 30 2c 20 20 2f 2a 20 20 20 20 20 53 45 4c 45 43  0,  /*     SELEC
226a2 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  T => nothing */.
226a3 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 44 49 53      0,  /*   DIS
226a4 54 49 4e 43 54 20 3d 3e 20 6e 6f 74 68 69 6e 67  TINCT => nothing
226a5 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
226a6 20 20 20 20 20 20 44 4f 54 20 3d 3e 20 6e 6f 74        DOT => not
226a7 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
226a8 2f 2a 20 20 20 20 20 20 20 46 52 4f 4d 20 3d 3e  /*       FROM =>
226a9 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
226aa 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 4a 4f 49  0,  /*       JOI
226ab 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
226ac 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
226ad 55 53 49 4e 47 20 3d 3e 20 6e 6f 74 68 69 6e 67  USING => nothing
226ae 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
226af 20 20 20 20 4f 52 44 45 52 20 3d 3e 20 6e 6f 74      ORDER => not
226b0 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
226b1 2f 2a 20 20 20 20 20 20 20 20 20 42 59 20 3d 3e  /*         BY =>
226b2 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
226b3 30 2c 20 20 2f 2a 20 20 20 20 20 20 47 52 4f 55  0,  /*      GROU
226b4 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  P => nothing */.
226b5 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 48      0,  /*     H
226b6 41 56 49 4e 47 20 3d 3e 20 6e 6f 74 68 69 6e 67  AVING => nothing
226b7 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
226b8 20 20 20 20 4c 49 4d 49 54 20 3d 3e 20 6e 6f 74      LIMIT => not
226b9 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
226ba 2f 2a 20 20 20 20 20 20 57 48 45 52 45 20 3d 3e  /*      WHERE =>
226bb 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
226bc 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 49 4e 54  0,  /*       INT
226bd 4f 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  O => nothing */.
226be 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 56      0,  /*     V
226bf 41 4c 55 45 53 20 3d 3e 20 6e 6f 74 68 69 6e 67  ALUES => nothing
226c0 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
226c1 20 20 49 4e 54 45 47 45 52 20 3d 3e 20 6e 6f 74    INTEGER => not
226c2 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
226c3 2f 2a 20 20 20 20 20 20 46 4c 4f 41 54 20 3d 3e  /*      FLOAT =>
226c4 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
226c5 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 42 4c 4f  0,  /*       BLO
226c6 42 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  B => nothing */.
226c7 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 52 45 47      0,  /*   REG
226c8 49 53 54 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67  ISTER => nothing
226c9 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
226ca 20 56 41 52 49 41 42 4c 45 20 3d 3e 20 6e 6f 74   VARIABLE => not
226cb 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
226cc 2f 2a 20 20 20 20 20 20 20 43 41 53 45 20 3d 3e  /*       CASE =>
226cd 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
226ce 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 57 48 45  0,  /*       WHE
226cf 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
226d0 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
226d1 20 54 48 45 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67   THEN => nothing
226d2 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
226d3 20 20 20 20 20 45 4c 53 45 20 3d 3e 20 6e 6f 74       ELSE => not
226d4 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
226d5 2f 2a 20 20 20 20 20 20 49 4e 44 45 58 20 3d 3e  /*      INDEX =>
226d6 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
226d7 30 2c 20 20 2f 2a 20 20 20 20 20 20 41 4c 54 45  0,  /*      ALTE
226d8 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  R => nothing */.
226d9 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
226da 20 20 20 54 4f 20 3d 3e 20 6e 6f 74 68 69 6e 67     TO => nothing
226db 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
226dc 20 20 20 20 20 20 41 44 44 20 3d 3e 20 6e 6f 74        ADD => not
226dd 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
226de 2f 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57 20 3d 3e  /*   COLUMNKW =>
226df 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 7d 3b 0a 23   nothing */.};.#
226e0 65 6e 64 69 66 20 2f 2a 20 59 59 46 41 4c 4c 42  endif /* YYFALLB
226e1 41 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 66  ACK */../* The f
226e2 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
226e3 72 65 20 72 65 70 72 65 73 65 6e 74 73 20 61 20  re represents a 
226e4 73 69 6e 67 6c 65 20 65 6c 65 6d 65 6e 74 20 6f  single element o
226e5 66 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 27  f the.** parser'
226e6 73 20 73 74 61 63 6b 2e 20 20 49 6e 66 6f 72 6d  s stack.  Inform
226e7 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e 63  ation stored inc
226e8 6c 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b  ludes:.**.**   +
226e9 20 20 54 68 65 20 73 74 61 74 65 20 6e 75 6d 62    The state numb
226ea 65 72 20 66 6f 72 20 74 68 65 20 70 61 72 73 65  er for the parse
226eb 72 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20  r at this level 
226ec 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  of the stack..**
226ed 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 76 61 6c  .**   +  The val
226ee 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  ue of the token 
226ef 73 74 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c  stored at this l
226f0 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63  evel of the stac
226f1 6b 2e 0a 2a 2a 20 20 20 20 20 20 28 49 6e 20 6f  k..**      (In o
226f2 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
226f3 22 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29 0a  "major" token.).
226f4 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 73  **.**   +  The s
226f5 65 6d 61 6e 74 69 63 20 76 61 6c 75 65 20 73 74  emantic value st
226f6 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65 76  ored at this lev
226f7 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  el of the stack.
226f8 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 20 20 20    This is.**    
226f9 20 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f    the informatio
226fa 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 61 63  n used by the ac
226fb 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e  tion routines in
226fc 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2a   the grammar..**
226fd 20 20 20 20 20 20 49 74 20 69 73 20 73 6f 6d 65        It is some
226fe 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 74 68 65  times called the
226ff 20 22 6d 69 6e 6f 72 22 20 74 6f 6b 65 6e 2e 0a   "minor" token..
22700 2a 2f 0a 73 74 72 75 63 74 20 79 79 53 74 61 63  */.struct yyStac
22701 6b 45 6e 74 72 79 20 7b 0a 20 20 69 6e 74 20 73  kEntry {.  int s
22702 74 61 74 65 6e 6f 3b 20 20 20 20 20 20 20 2f 2a  tateno;       /*
22703 20 54 68 65 20 73 74 61 74 65 2d 6e 75 6d 62 65   The state-numbe
22704 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6a 6f 72  r */.  int major
22705 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
22706 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 76 61 6c   major token val
22707 75 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  ue.  This is the
22708 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20 20 20   code.          
22709 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 75             ** nu
2270a 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 6f 6b  mber for the tok
2270b 65 6e 20 61 74 20 74 68 69 73 20 73 74 61 63 6b  en at this stack
2270c 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 59 59 4d 49   level */.  YYMI
2270d 4e 4f 52 54 59 50 45 20 6d 69 6e 6f 72 3b 20 2f  NORTYPE minor; /
2270e 2a 20 54 68 65 20 75 73 65 72 2d 73 75 70 70 6c  * The user-suppl
2270f 69 65 64 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20  ied minor token 
22710 76 61 6c 75 65 2e 20 20 54 68 69 73 0a 20 20 20  value.  This.   
22711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22712 20 20 2a 2a 20 69 73 20 74 68 65 20 76 61 6c 75    ** is the valu
22713 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 20  e of the token  
22714 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  */.};.typedef st
22715 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72  ruct yyStackEntr
22716 79 20 79 79 53 74 61 63 6b 45 6e 74 72 79 3b 0a  y yyStackEntry;.
22717 0a 2f 2a 20 54 68 65 20 73 74 61 74 65 20 6f 66  ./* The state of
22718 20 74 68 65 20 70 61 72 73 65 72 20 69 73 20 63   the parser is c
22719 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61 69  ompletely contai
2271a 6e 65 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e  ned in an instan
2271b 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c  ce of.** the fol
2271c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
2271d 20 2a 2f 0a 73 74 72 75 63 74 20 79 79 50 61 72   */.struct yyPar
2271e 73 65 72 20 7b 0a 20 20 69 6e 74 20 79 79 69 64  ser {.  int yyid
2271f 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
22720 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
22721 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e  f top element in
22722 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   stack */.  int 
22723 79 79 65 72 72 63 6e 74 3b 20 20 20 20 20 20 20  yyerrcnt;       
22724 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 69            /* Shi
22725 66 74 73 20 6c 65 66 74 20 62 65 66 6f 72 65 20  fts left before 
22726 6f 75 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72  out of the error
22727 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
22728 73 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20  serARG_SDECL    
22729 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2272a 20 70 6c 61 63 65 20 74 6f 20 68 6f 6c 64 20 25   place to hold %
2272b 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 2a  extra_argument *
2272c 2f 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50  /.#if YYSTACKDEP
2272d 54 48 3c 3d 30 0a 20 20 69 6e 74 20 79 79 73 74  TH<=0.  int yyst
2272e 6b 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20  ksz;            
2272f 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
22730 20 73 69 64 65 20 6f 66 20 74 68 65 20 73 74 61   side of the sta
22731 63 6b 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45  ck */.  yyStackE
22732 6e 74 72 79 20 2a 79 79 73 74 61 63 6b 3b 20 20  ntry *yystack;  
22733 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
22734 73 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23  ser's stack */.#
22735 65 6c 73 65 0a 20 20 79 79 53 74 61 63 6b 45 6e  else.  yyStackEn
22736 74 72 79 20 79 79 73 74 61 63 6b 5b 59 59 53 54  try yystack[YYST
22737 41 43 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20 54  ACKDEPTH];  /* T
22738 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63  he parser's stac
22739 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74  k */.#endif.};.t
2273a 79 70 65 64 65 66 20 73 74 72 75 63 74 20 79 79  ypedef struct yy
2273b 50 61 72 73 65 72 20 79 79 50 61 72 73 65 72 3b  Parser yyParser;
2273c 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
2273d 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79  .static FILE *yy
2273e 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 73  TraceFILE = 0;.s
2273f 74 61 74 69 63 20 63 68 61 72 20 2a 79 79 54 72  tatic char *yyTr
22740 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 23  acePrompt = 0;.#
22741 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20  endif /* NDEBUG 
22742 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  */..#ifndef NDEB
22743 55 47 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20 70  UG./* .** Turn p
22744 61 72 73 65 72 20 74 72 61 63 69 6e 67 20 6f 6e  arser tracing on
22745 20 62 79 20 67 69 76 69 6e 67 20 61 20 73 74 72   by giving a str
22746 65 61 6d 20 74 6f 20 77 68 69 63 68 20 74 6f 20  eam to which to 
22747 77 72 69 74 65 20 74 68 65 20 74 72 61 63 65 0a  write the trace.
22748 2a 2a 20 61 6e 64 20 61 20 70 72 6f 6d 70 74 20  ** and a prompt 
22749 74 6f 20 70 72 65 66 61 63 65 20 65 61 63 68 20  to preface each 
2274a 74 72 61 63 65 20 6d 65 73 73 61 67 65 2e 20 20  trace message.  
2274b 54 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65  Tracing is turne
2274c 64 20 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b 69  d off.** by maki
2274d 6e 67 20 65 69 74 68 65 72 20 61 72 67 75 6d 65  ng either argume
2274e 6e 74 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49  nt NULL .**.** I
2274f 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  nputs:.** <ul>.*
22750 2a 20 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20 74  * <li> A FILE* t
22751 6f 20 77 68 69 63 68 20 74 72 61 63 65 20 6f 75  o which trace ou
22752 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 77  tput should be w
22753 72 69 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20 20  ritten..**      
22754 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72  If NULL, then tr
22755 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20  acing is turned 
22756 6f 66 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70  off..** <li> A p
22757 72 65 66 69 78 20 73 74 72 69 6e 67 20 77 72 69  refix string wri
22758 74 74 65 6e 20 61 74 20 74 68 65 20 62 65 67 69  tten at the begi
22759 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a  nning of every.*
2275a 2a 20 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74  *      line of t
2275b 72 61 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66  race output.  If
2275c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63   NULL, then trac
2275d 69 6e 67 20 69 73 0a 2a 2a 20 20 20 20 20 20 74  ing is.**      t
2275e 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 2f  urned off..** </
2275f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74  ul>.**.** Output
22760 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53  s:.** None..*/.S
22761 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
22762 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  id sqlite3Parser
22763 54 72 61 63 65 28 46 49 4c 45 20 2a 54 72 61 63  Trace(FILE *Trac
22764 65 46 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54 72  eFILE, char *zTr
22765 61 63 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79 79  acePrompt){.  yy
22766 54 72 61 63 65 46 49 4c 45 20 3d 20 54 72 61 63  TraceFILE = Trac
22767 65 46 49 4c 45 3b 0a 20 20 79 79 54 72 61 63 65  eFILE;.  yyTrace
22768 50 72 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65 50  Prompt = zTraceP
22769 72 6f 6d 70 74 3b 0a 20 20 69 66 28 20 79 79 54  rompt;.  if( yyT
2276a 72 61 63 65 46 49 4c 45 3d 3d 30 20 29 20 79 79  raceFILE==0 ) yy
2276b 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b  TracePrompt = 0;
2276c 0a 20 20 65 6c 73 65 20 69 66 28 20 79 79 54 72  .  else if( yyTr
2276d 61 63 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20 79  acePrompt==0 ) y
2276e 79 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a  yTraceFILE = 0;.
2276f 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  }.#endif /* NDEB
22770 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e  UG */..#ifndef N
22771 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61  DEBUG./* For tra
22772 63 69 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65  cing shifts, the
22773 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65   names of all te
22774 72 6d 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74  rminals and nont
22775 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65 20  erminals.** are 
22776 72 65 71 75 69 72 65 64 2e 20 20 54 68 65 20 66  required.  The f
22777 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73  ollowing table s
22778 75 70 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61  upplies these na
22779 6d 65 73 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  mes */.static co
2277a 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20  nst char *const 
2277b 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20  yyTokenName[] = 
2277c 7b 20 0a 20 20 22 24 22 2c 20 20 20 20 20 20 20  { .  "$",       
2277d 20 20 20 20 20 20 22 53 45 4d 49 22 2c 20 20 20        "SEMI",   
2277e 20 20 20 20 20 20 20 22 45 58 50 4c 41 49 4e 22         "EXPLAIN"
2277f 2c 20 20 20 20 20 20 20 22 51 55 45 52 59 22 2c  ,       "QUERY",
22780 20 20 20 20 20 20 20 0a 20 20 22 50 4c 41 4e 22         .  "PLAN"
22781 2c 20 20 20 20 20 20 20 20 20 20 22 42 45 47 49  ,          "BEGI
22782 4e 22 2c 20 20 20 20 20 20 20 20 20 22 54 52 41  N",         "TRA
22783 4e 53 41 43 54 49 4f 4e 22 2c 20 20 20 22 44 45  NSACTION",   "DE
22784 46 45 52 52 45 44 22 2c 20 20 20 20 0a 20 20 22  FERRED",    .  "
22785 49 4d 4d 45 44 49 41 54 45 22 2c 20 20 20 20 20  IMMEDIATE",     
22786 22 45 58 43 4c 55 53 49 56 45 22 2c 20 20 20 20  "EXCLUSIVE",    
22787 20 22 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20 20   "COMMIT",      
22788 20 20 22 45 4e 44 22 2c 20 20 20 20 20 20 20 20    "END",        
22789 20 0a 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20   .  "ROLLBACK", 
2278a 20 20 20 20 20 22 43 52 45 41 54 45 22 2c 20 20       "CREATE",  
2278b 20 20 20 20 20 20 22 54 41 42 4c 45 22 2c 20 20        "TABLE",  
2278c 20 20 20 20 20 20 20 22 49 46 22 2c 20 20 20 20         "IF",    
2278d 20 20 20 20 20 20 0a 20 20 22 4e 4f 54 22 2c 20        .  "NOT", 
2278e 20 20 20 20 20 20 20 20 20 20 22 45 58 49 53 54            "EXIST
2278f 53 22 2c 20 20 20 20 20 20 20 20 22 54 45 4d 50  S",        "TEMP
22790 22 2c 20 20 20 20 20 20 20 20 20 20 22 4c 50 22  ",          "LP"
22791 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 52  ,          .  "R
22792 50 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  P",            "
22793 41 53 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  AS",            
22794 22 43 4f 4d 4d 41 22 2c 20 20 20 20 20 20 20 20  "COMMA",        
22795 20 22 49 44 22 2c 20 20 20 20 20 20 20 20 20 20   "ID",          
22796 0a 20 20 22 41 42 4f 52 54 22 2c 20 20 20 20 20  .  "ABORT",     
22797 20 20 20 20 22 41 46 54 45 52 22 2c 20 20 20 20      "AFTER",    
22798 20 20 20 20 20 22 41 4e 41 4c 59 5a 45 22 2c 20       "ANALYZE", 
22799 20 20 20 20 20 20 22 41 53 43 22 2c 20 20 20 20        "ASC",    
2279a 20 20 20 20 20 0a 20 20 22 41 54 54 41 43 48 22       .  "ATTACH"
2279b 2c 20 20 20 20 20 20 20 20 22 42 45 46 4f 52 45  ,        "BEFORE
2279c 22 2c 20 20 20 20 20 20 20 20 22 43 41 53 43 41  ",        "CASCA
2279d 44 45 22 2c 20 20 20 20 20 20 20 22 43 41 53 54  DE",       "CAST
2279e 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 43 4f  ",        .  "CO
2279f 4e 46 4c 49 43 54 22 2c 20 20 20 20 20 20 22 44  NFLICT",      "D
227a0 41 54 41 42 41 53 45 22 2c 20 20 20 20 20 20 22  ATABASE",      "
227a1 44 45 53 43 22 2c 20 20 20 20 20 20 20 20 20 20  DESC",          
227a2 22 44 45 54 41 43 48 22 2c 20 20 20 20 20 20 0a  "DETACH",      .
227a3 20 20 22 45 41 43 48 22 2c 20 20 20 20 20 20 20    "EACH",       
227a4 20 20 20 22 46 41 49 4c 22 2c 20 20 20 20 20 20     "FAIL",      
227a5 20 20 20 20 22 46 4f 52 22 2c 20 20 20 20 20 20      "FOR",      
227a6 20 20 20 20 20 22 49 47 4e 4f 52 45 22 2c 20 20       "IGNORE",  
227a7 20 20 20 20 0a 20 20 22 49 4e 49 54 49 41 4c 4c      .  "INITIALL
227a8 59 22 2c 20 20 20 20 20 22 49 4e 53 54 45 41 44  Y",     "INSTEAD
227a9 22 2c 20 20 20 20 20 20 20 22 4c 49 4b 45 5f 4b  ",       "LIKE_K
227aa 57 22 2c 20 20 20 20 20 20 20 22 4d 41 54 43 48  W",       "MATCH
227ab 22 2c 20 20 20 20 20 20 20 0a 20 20 22 4b 45 59  ",       .  "KEY
227ac 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 4f 46  ",           "OF
227ad 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4f  ",            "O
227ae 46 46 53 45 54 22 2c 20 20 20 20 20 20 20 20 22  FFSET",        "
227af 50 52 41 47 4d 41 22 2c 20 20 20 20 20 20 0a 20  PRAGMA",      . 
227b0 20 22 52 41 49 53 45 22 2c 20 20 20 20 20 20 20   "RAISE",       
227b1 20 20 22 52 45 50 4c 41 43 45 22 2c 20 20 20 20    "REPLACE",    
227b2 20 20 20 22 52 45 53 54 52 49 43 54 22 2c 20 20     "RESTRICT",  
227b3 20 20 20 20 22 52 4f 57 22 2c 20 20 20 20 20 20      "ROW",      
227b4 20 20 20 0a 20 20 22 54 52 49 47 47 45 52 22 2c     .  "TRIGGER",
227b5 20 20 20 20 20 20 20 22 56 41 43 55 55 4d 22 2c         "VACUUM",
227b6 20 20 20 20 20 20 20 20 22 56 49 45 57 22 2c 20          "VIEW", 
227b7 20 20 20 20 20 20 20 20 20 22 56 49 52 54 55 41           "VIRTUA
227b8 4c 22 2c 20 20 20 20 20 0a 20 20 22 52 45 49 4e  L",     .  "REIN
227b9 44 45 58 22 2c 20 20 20 20 20 20 20 22 52 45 4e  DEX",       "REN
227ba 41 4d 45 22 2c 20 20 20 20 20 20 20 20 22 43 54  AME",        "CT
227bb 49 4d 45 5f 4b 57 22 2c 20 20 20 20 20 20 22 41  IME_KW",      "A
227bc 4e 59 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20  NY",         .  
227bd 22 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20  "OR",           
227be 20 22 41 4e 44 22 2c 20 20 20 20 20 20 20 20 20   "AND",         
227bf 20 20 22 49 53 22 2c 20 20 20 20 20 20 20 20 20    "IS",         
227c0 20 20 20 22 42 45 54 57 45 45 4e 22 2c 20 20 20     "BETWEEN",   
227c1 20 20 0a 20 20 22 49 4e 22 2c 20 20 20 20 20 20    .  "IN",      
227c2 20 20 20 20 20 20 22 49 53 4e 55 4c 4c 22 2c 20        "ISNULL", 
227c3 20 20 20 20 20 20 20 22 4e 4f 54 4e 55 4c 4c 22         "NOTNULL"
227c4 2c 20 20 20 20 20 20 20 22 4e 45 22 2c 20 20 20  ,       "NE",   
227c5 20 20 20 20 20 20 20 0a 20 20 22 45 51 22 2c 20         .  "EQ", 
227c6 20 20 20 20 20 20 20 20 20 20 20 22 47 54 22 2c             "GT",
227c7 20 20 20 20 20 20 20 20 20 20 20 20 22 4c 45 22              "LE"
227c8 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4c 54  ,            "LT
227c9 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22  ",          .  "
227ca 47 45 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  GE",            
227cb 22 45 53 43 41 50 45 22 2c 20 20 20 20 20 20 20  "ESCAPE",       
227cc 20 22 42 49 54 41 4e 44 22 2c 20 20 20 20 20 20   "BITAND",      
227cd 20 20 22 42 49 54 4f 52 22 2c 20 20 20 20 20 20    "BITOR",      
227ce 20 0a 20 20 22 4c 53 48 49 46 54 22 2c 20 20 20   .  "LSHIFT",   
227cf 20 20 20 20 20 22 52 53 48 49 46 54 22 2c 20 20       "RSHIFT",  
227d0 20 20 20 20 20 20 22 50 4c 55 53 22 2c 20 20 20        "PLUS",   
227d1 20 20 20 20 20 20 20 22 4d 49 4e 55 53 22 2c 20         "MINUS", 
227d2 20 20 20 20 20 20 0a 20 20 22 53 54 41 52 22 2c        .  "STAR",
227d3 20 20 20 20 20 20 20 20 20 20 22 53 4c 41 53 48            "SLASH
227d4 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 4d 22  ",         "REM"
227d5 2c 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4e  ,           "CON
227d6 43 41 54 22 2c 20 20 20 20 20 20 0a 20 20 22 43  CAT",      .  "C
227d7 4f 4c 4c 41 54 45 22 2c 20 20 20 20 20 20 20 22  OLLATE",       "
227d8 55 4d 49 4e 55 53 22 2c 20 20 20 20 20 20 20 20  UMINUS",        
227d9 22 55 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20  "UPLUS",        
227da 20 22 42 49 54 4e 4f 54 22 2c 20 20 20 20 20 20   "BITNOT",      
227db 0a 20 20 22 53 54 52 49 4e 47 22 2c 20 20 20 20  .  "STRING",    
227dc 20 20 20 20 22 4a 4f 49 4e 5f 4b 57 22 2c 20 20      "JOIN_KW",  
227dd 20 20 20 20 20 22 43 4f 4e 53 54 52 41 49 4e 54       "CONSTRAINT
227de 22 2c 20 20 20 20 22 44 45 46 41 55 4c 54 22 2c  ",    "DEFAULT",
227df 20 20 20 20 20 0a 20 20 22 4e 55 4c 4c 22 2c 20       .  "NULL", 
227e0 20 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 52           "PRIMAR
227e1 59 22 2c 20 20 20 20 20 20 20 22 55 4e 49 51 55  Y",       "UNIQU
227e2 45 22 2c 20 20 20 20 20 20 20 20 22 43 48 45 43  E",        "CHEC
227e3 4b 22 2c 20 20 20 20 20 20 20 0a 20 20 22 52 45  K",       .  "RE
227e4 46 45 52 45 4e 43 45 53 22 2c 20 20 20 20 22 41  FERENCES",    "A
227e5 55 54 4f 49 4e 43 52 22 2c 20 20 20 20 20 20 22  UTOINCR",      "
227e6 4f 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  ON",            
227e7 22 44 45 4c 45 54 45 22 2c 20 20 20 20 20 20 0a  "DELETE",      .
227e8 20 20 22 55 50 44 41 54 45 22 2c 20 20 20 20 20    "UPDATE",     
227e9 20 20 20 22 49 4e 53 45 52 54 22 2c 20 20 20 20     "INSERT",    
227ea 20 20 20 20 22 53 45 54 22 2c 20 20 20 20 20 20      "SET",      
227eb 20 20 20 20 20 22 44 45 46 45 52 52 41 42 4c 45       "DEFERRABLE
227ec 22 2c 20 20 0a 20 20 22 46 4f 52 45 49 47 4e 22  ",  .  "FOREIGN"
227ed 2c 20 20 20 20 20 20 20 22 44 52 4f 50 22 2c 20  ,       "DROP", 
227ee 20 20 20 20 20 20 20 20 20 22 55 4e 49 4f 4e 22           "UNION"
227ef 2c 20 20 20 20 20 20 20 20 20 22 41 4c 4c 22 2c  ,         "ALL",
227f0 20 20 20 20 20 20 20 20 20 0a 20 20 22 45 58 43           .  "EXC
227f1 45 50 54 22 2c 20 20 20 20 20 20 20 20 22 49 4e  EPT",        "IN
227f2 54 45 52 53 45 43 54 22 2c 20 20 20 20 20 22 53  TERSECT",     "S
227f3 45 4c 45 43 54 22 2c 20 20 20 20 20 20 20 20 22  ELECT",        "
227f4 44 49 53 54 49 4e 43 54 22 2c 20 20 20 20 0a 20  DISTINCT",    . 
227f5 20 22 44 4f 54 22 2c 20 20 20 20 20 20 20 20 20   "DOT",         
227f6 20 20 22 46 52 4f 4d 22 2c 20 20 20 20 20 20 20    "FROM",       
227f7 20 20 20 22 4a 4f 49 4e 22 2c 20 20 20 20 20 20     "JOIN",      
227f8 20 20 20 20 22 55 53 49 4e 47 22 2c 20 20 20 20      "USING",    
227f9 20 20 20 0a 20 20 22 4f 52 44 45 52 22 2c 20 20     .  "ORDER",  
227fa 20 20 20 20 20 20 20 22 42 59 22 2c 20 20 20 20         "BY",    
227fb 20 20 20 20 20 20 20 20 22 47 52 4f 55 50 22 2c          "GROUP",
227fc 20 20 20 20 20 20 20 20 20 22 48 41 56 49 4e 47           "HAVING
227fd 22 2c 20 20 20 20 20 20 0a 20 20 22 4c 49 4d 49  ",      .  "LIMI
227fe 54 22 2c 20 20 20 20 20 20 20 20 20 22 57 48 45  T",         "WHE
227ff 52 45 22 2c 20 20 20 20 20 20 20 20 20 22 49 4e  RE",         "IN
22800 54 4f 22 2c 20 20 20 20 20 20 20 20 20 20 22 56  TO",          "V
22801 41 4c 55 45 53 22 2c 20 20 20 20 20 20 0a 20 20  ALUES",      .  
22802 22 49 4e 54 45 47 45 52 22 2c 20 20 20 20 20 20  "INTEGER",      
22803 20 22 46 4c 4f 41 54 22 2c 20 20 20 20 20 20 20   "FLOAT",       
22804 20 20 22 42 4c 4f 42 22 2c 20 20 20 20 20 20 20    "BLOB",       
22805 20 20 20 22 52 45 47 49 53 54 45 52 22 2c 20 20     "REGISTER",  
22806 20 20 0a 20 20 22 56 41 52 49 41 42 4c 45 22 2c    .  "VARIABLE",
22807 20 20 20 20 20 20 22 43 41 53 45 22 2c 20 20 20        "CASE",   
22808 20 20 20 20 20 20 20 22 57 48 45 4e 22 2c 20 20         "WHEN",  
22809 20 20 20 20 20 20 20 20 22 54 48 45 4e 22 2c 20          "THEN", 
2280a 20 20 20 20 20 20 20 0a 20 20 22 45 4c 53 45 22         .  "ELSE"
2280b 2c 20 20 20 20 20 20 20 20 20 20 22 49 4e 44 45  ,          "INDE
2280c 58 22 2c 20 20 20 20 20 20 20 20 20 22 41 4c 54  X",         "ALT
2280d 45 52 22 2c 20 20 20 20 20 20 20 20 20 22 54 4f  ER",         "TO
2280e 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22  ",          .  "
2280f 41 44 44 22 2c 20 20 20 20 20 20 20 20 20 20 20  ADD",           
22810 22 43 4f 4c 55 4d 4e 4b 57 22 2c 20 20 20 20 20  "COLUMNKW",     
22811 20 22 65 72 72 6f 72 22 2c 20 20 20 20 20 20 20   "error",       
22812 20 20 22 69 6e 70 75 74 22 2c 20 20 20 20 20 20    "input",      
22813 20 0a 20 20 22 63 6d 64 6c 69 73 74 22 2c 20 20   .  "cmdlist",  
22814 20 20 20 20 20 22 65 63 6d 64 22 2c 20 20 20 20       "ecmd",    
22815 20 20 20 20 20 20 22 63 6d 64 78 22 2c 20 20 20        "cmdx",   
22816 20 20 20 20 20 20 20 22 63 6d 64 22 2c 20 20 20         "cmd",   
22817 20 20 20 20 20 20 0a 20 20 22 65 78 70 6c 61 69        .  "explai
22818 6e 22 2c 20 20 20 20 20 20 20 22 74 72 61 6e 73  n",       "trans
22819 74 79 70 65 22 2c 20 20 20 20 20 22 74 72 61 6e  type",     "tran
2281a 73 5f 6f 70 74 22 2c 20 20 20 20 20 22 6e 6d 22  s_opt",     "nm"
2281b 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 63  ,          .  "c
2281c 72 65 61 74 65 5f 74 61 62 6c 65 22 2c 20 20 22  reate_table",  "
2281d 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
2281e 73 22 2c 20 20 22 74 65 6d 70 22 2c 20 20 20 20  s",  "temp",    
2281f 20 20 20 20 20 20 22 69 66 6e 6f 74 65 78 69 73        "ifnotexis
22820 74 73 22 2c 20 0a 20 20 22 64 62 6e 6d 22 2c 20  ts", .  "dbnm", 
22821 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e           "column
22822 6c 69 73 74 22 2c 20 20 20 20 22 63 6f 6e 73 6c  list",    "consl
22823 69 73 74 5f 6f 70 74 22 2c 20 20 22 73 65 6c 65  ist_opt",  "sele
22824 63 74 22 2c 20 20 20 20 20 20 0a 20 20 22 63 6f  ct",      .  "co
22825 6c 75 6d 6e 22 2c 20 20 20 20 20 20 20 20 22 63  lumn",        "c
22826 6f 6c 75 6d 6e 69 64 22 2c 20 20 20 20 20 20 22  olumnid",      "
22827 74 79 70 65 22 2c 20 20 20 20 20 20 20 20 20 20  type",          
22828 22 63 61 72 67 6c 69 73 74 22 2c 20 20 20 20 0a  "carglist",    .
22829 20 20 22 69 64 22 2c 20 20 20 20 20 20 20 20 20    "id",         
2282a 20 20 20 22 69 64 73 22 2c 20 20 20 20 20 20 20     "ids",       
2282b 20 20 20 20 22 74 79 70 65 74 6f 6b 65 6e 22 2c      "typetoken",
2282c 20 20 20 20 20 22 74 79 70 65 6e 61 6d 65 22 2c       "typename",
2282d 20 20 20 20 0a 20 20 22 73 69 67 6e 65 64 22 2c      .  "signed",
2282e 20 20 20 20 20 20 20 20 22 70 6c 75 73 5f 6e 75          "plus_nu
2282f 6d 22 2c 20 20 20 20 20 20 22 6d 69 6e 75 73 5f  m",      "minus_
22830 6e 75 6d 22 2c 20 20 20 20 20 22 63 61 72 67 22  num",     "carg"
22831 2c 20 20 20 20 20 20 20 20 0a 20 20 22 63 63 6f  ,        .  "cco
22832 6e 73 22 2c 20 20 20 20 20 20 20 20 20 22 74 65  ns",         "te
22833 72 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 65  rm",          "e
22834 78 70 72 22 2c 20 20 20 20 20 20 20 20 20 20 22  xpr",          "
22835 6f 6e 63 6f 6e 66 22 2c 20 20 20 20 20 20 0a 20  onconf",      . 
22836 20 22 73 6f 72 74 6f 72 64 65 72 22 2c 20 20 20   "sortorder",   
22837 20 20 22 61 75 74 6f 69 6e 63 22 2c 20 20 20 20    "autoinc",    
22838 20 20 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 22     "idxlist_opt"
22839 2c 20 20 20 22 72 65 66 61 72 67 73 22 2c 20 20  ,   "refargs",  
2283a 20 20 20 0a 20 20 22 64 65 66 65 72 5f 73 75 62     .  "defer_sub
2283b 63 6c 61 75 73 65 22 2c 20 20 22 72 65 66 61 72  clause",  "refar
2283c 67 22 2c 20 20 20 20 20 20 20 20 22 72 65 66 61  g",        "refa
2283d 63 74 22 2c 20 20 20 20 20 20 20 20 22 69 6e 69  ct",        "ini
2283e 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
2283f 6f 70 74 22 2c 0a 20 20 22 63 6f 6e 73 6c 69 73  opt",.  "conslis
22840 74 22 2c 20 20 20 20 20 20 22 74 63 6f 6e 73 22  t",      "tcons"
22841 2c 20 20 20 20 20 20 20 20 20 22 69 64 78 6c 69  ,         "idxli
22842 73 74 22 2c 20 20 20 20 20 20 20 22 64 65 66 65  st",       "defe
22843 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 22  r_subclause_opt"
22844 2c 0a 20 20 22 6f 72 63 6f 6e 66 22 2c 20 20 20  ,.  "orconf",   
22845 20 20 20 20 20 22 72 65 73 6f 6c 76 65 74 79 70       "resolvetyp
22846 65 22 2c 20 20 20 22 72 61 69 73 65 74 79 70 65  e",   "raisetype
22847 22 2c 20 20 20 20 20 22 69 66 65 78 69 73 74 73  ",     "ifexists
22848 22 2c 20 20 20 20 0a 20 20 22 66 75 6c 6c 6e 61  ",    .  "fullna
22849 6d 65 22 2c 20 20 20 20 20 20 22 6f 6e 65 73 65  me",      "onese
2284a 6c 65 63 74 22 2c 20 20 20 20 20 22 6d 75 6c 74  lect",     "mult
2284b 69 73 65 6c 65 63 74 5f 6f 70 22 2c 20 20 22 64  iselect_op",  "d
2284c 69 73 74 69 6e 63 74 22 2c 20 20 20 20 0a 20 20  istinct",    .  
2284d 22 73 65 6c 63 6f 6c 6c 69 73 74 22 2c 20 20 20  "selcollist",   
2284e 20 22 66 72 6f 6d 22 2c 20 20 20 20 20 20 20 20   "from",        
2284f 20 20 22 77 68 65 72 65 5f 6f 70 74 22 2c 20 20    "where_opt",  
22850 20 20 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 22     "groupby_opt"
22851 2c 20 0a 20 20 22 68 61 76 69 6e 67 5f 6f 70 74  , .  "having_opt
22852 22 2c 20 20 20 20 22 6f 72 64 65 72 62 79 5f 6f  ",    "orderby_o
22853 70 74 22 2c 20 20 20 22 6c 69 6d 69 74 5f 6f 70  pt",   "limit_op
22854 74 22 2c 20 20 20 20 20 22 73 63 6c 70 22 2c 20  t",     "sclp", 
22855 20 20 20 20 20 20 20 0a 20 20 22 61 73 22 2c 20         .  "as", 
22856 20 20 20 20 20 20 20 20 20 20 20 22 73 65 6c 74             "selt
22857 61 62 6c 69 73 74 22 2c 20 20 20 20 22 73 74 6c  ablist",    "stl
22858 5f 70 72 65 66 69 78 22 2c 20 20 20 20 22 6a 6f  _prefix",    "jo
22859 69 6e 6f 70 22 2c 20 20 20 20 20 20 0a 20 20 22  inop",      .  "
2285a 6f 6e 5f 6f 70 74 22 2c 20 20 20 20 20 20 20 20  on_opt",        
2285b 22 75 73 69 6e 67 5f 6f 70 74 22 2c 20 20 20 20  "using_opt",    
2285c 20 22 73 65 6c 74 61 62 6c 69 73 74 5f 70 61 72   "seltablist_par
2285d 65 6e 22 2c 20 20 22 6a 6f 69 6e 6f 70 32 22 2c  en",  "joinop2",
2285e 20 20 20 20 20 0a 20 20 22 69 6e 73 63 6f 6c 6c       .  "inscoll
2285f 69 73 74 22 2c 20 20 20 20 22 73 6f 72 74 6c 69  ist",    "sortli
22860 73 74 22 2c 20 20 20 20 20 20 22 73 6f 72 74 69  st",      "sorti
22861 74 65 6d 22 2c 20 20 20 20 20 20 22 6e 65 78 70  tem",      "nexp
22862 72 6c 69 73 74 22 2c 20 20 20 0a 20 20 22 73 65  rlist",   .  "se
22863 74 6c 69 73 74 22 2c 20 20 20 20 20 20 20 22 69  tlist",       "i
22864 6e 73 65 72 74 5f 63 6d 64 22 2c 20 20 20 20 22  nsert_cmd",    "
22865 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 22 2c  inscollist_opt",
22866 20 20 22 69 74 65 6d 6c 69 73 74 22 2c 20 20 20    "itemlist",   
22867 20 0a 20 20 22 65 78 70 72 6c 69 73 74 22 2c 20   .  "exprlist", 
22868 20 20 20 20 20 22 6c 69 6b 65 6f 70 22 2c 20 20       "likeop",  
22869 20 20 20 20 20 20 22 65 73 63 61 70 65 22 2c 20        "escape", 
2286a 20 20 20 20 20 20 20 22 62 65 74 77 65 65 6e 5f         "between_
2286b 6f 70 22 2c 20 20 0a 20 20 22 69 6e 5f 6f 70 22  op",  .  "in_op"
2286c 2c 20 20 20 20 20 20 20 20 20 22 63 61 73 65 5f  ,         "case_
2286d 6f 70 65 72 61 6e 64 22 2c 20 20 22 63 61 73 65  operand",  "case
2286e 5f 65 78 70 72 6c 69 73 74 22 2c 20 20 22 63 61  _exprlist",  "ca
2286f 73 65 5f 65 6c 73 65 22 2c 20 20 20 0a 20 20 22  se_else",   .  "
22870 75 6e 69 71 75 65 66 6c 61 67 22 2c 20 20 20 20  uniqueflag",    
22871 22 69 64 78 69 74 65 6d 22 2c 20 20 20 20 20 20  "idxitem",      
22872 20 22 63 6f 6c 6c 61 74 65 22 2c 20 20 20 20 20   "collate",     
22873 20 20 22 6e 6d 6e 75 6d 22 2c 20 20 20 20 20 20    "nmnum",      
22874 20 0a 20 20 22 70 6c 75 73 5f 6f 70 74 22 2c 20   .  "plus_opt", 
22875 20 20 20 20 20 22 6e 75 6d 62 65 72 22 2c 20 20       "number",  
22876 20 20 20 20 20 20 22 74 72 69 67 67 65 72 5f 64        "trigger_d
22877 65 63 6c 22 2c 20 20 22 74 72 69 67 67 65 72 5f  ecl",  "trigger_
22878 63 6d 64 5f 6c 69 73 74 22 2c 0a 20 20 22 74 72  cmd_list",.  "tr
22879 69 67 67 65 72 5f 74 69 6d 65 22 2c 20 20 22 74  igger_time",  "t
2287a 72 69 67 67 65 72 5f 65 76 65 6e 74 22 2c 20 20  rigger_event",  
2287b 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 22  "foreach_clause"
2287c 2c 20 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 22  ,  "when_clause"
2287d 2c 20 0a 20 20 22 74 72 69 67 67 65 72 5f 63 6d  , .  "trigger_cm
2287e 64 22 2c 20 20 20 22 64 61 74 61 62 61 73 65 5f  d",   "database_
2287f 6b 77 5f 6f 70 74 22 2c 20 20 22 6b 65 79 5f 6f  kw_opt",  "key_o
22880 70 74 22 2c 20 20 20 20 20 20 20 22 61 64 64 5f  pt",       "add_
22881 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 22  column_fullname"
22882 2c 0a 20 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70  ,.  "kwcolumn_op
22883 74 22 2c 20 20 22 63 72 65 61 74 65 5f 76 74 61  t",  "create_vta
22884 62 22 2c 20 20 20 22 76 74 61 62 61 72 67 6c 69  b",   "vtabargli
22885 73 74 22 2c 20 20 20 22 76 74 61 62 61 72 67 22  st",   "vtabarg"
22886 2c 20 20 20 20 20 0a 20 20 22 76 74 61 62 61 72  ,     .  "vtabar
22887 67 74 6f 6b 65 6e 22 2c 20 20 22 6c 70 22 2c 20  gtoken",  "lp", 
22888 20 20 20 20 20 20 20 20 20 20 20 22 61 6e 79 6c             "anyl
22889 69 73 74 22 2c 20 20 20 20 20 0a 7d 3b 0a 23 65  ist",     .};.#e
2288a 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
2288b 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
2288c 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69 6e 67  G./* For tracing
2288d 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 73 2c   reduce actions,
2288e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c   the names of al
2288f 6c 20 72 75 6c 65 73 20 61 72 65 20 72 65 71 75  l rules are requ
22890 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ired..*/.static 
22891 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
22892 74 20 79 79 52 75 6c 65 4e 61 6d 65 5b 5d 20 3d  t yyRuleName[] =
22893 20 7b 0a 20 2f 2a 20 20 20 30 20 2a 2f 20 22 69   {. /*   0 */ "i
22894 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74  nput ::= cmdlist
22895 22 2c 0a 20 2f 2a 20 20 20 31 20 2a 2f 20 22 63  ",. /*   1 */ "c
22896 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69  mdlist ::= cmdli
22897 73 74 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20  st ecmd",. /*   
22898 32 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a  2 */ "cmdlist ::
22899 3d 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 33  = ecmd",. /*   3
2289a 20 2a 2f 20 22 63 6d 64 78 20 3a 3a 3d 20 63 6d   */ "cmdx ::= cm
2289b 64 22 2c 0a 20 2f 2a 20 20 20 34 20 2a 2f 20 22  d",. /*   4 */ "
2289c 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 22 2c 0a  ecmd ::= SEMI",.
2289d 20 2f 2a 20 20 20 35 20 2a 2f 20 22 65 63 6d 64   /*   5 */ "ecmd
2289e 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64   ::= explain cmd
2289f 78 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 36  x SEMI",. /*   6
228a0 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d   */ "explain ::=
228a1 22 2c 0a 20 2f 2a 20 20 20 37 20 2a 2f 20 22 65  ",. /*   7 */ "e
228a2 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41  xplain ::= EXPLA
228a3 49 4e 22 2c 0a 20 2f 2a 20 20 20 38 20 2a 2f 20  IN",. /*   8 */ 
228a4 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50  "explain ::= EXP
228a5 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 22  LAIN QUERY PLAN"
228a6 2c 0a 20 2f 2a 20 20 20 39 20 2a 2f 20 22 63 6d  ,. /*   9 */ "cm
228a7 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e  d ::= BEGIN tran
228a8 73 74 79 70 65 20 74 72 61 6e 73 5f 6f 70 74 22  stype trans_opt"
228a9 2c 0a 20 2f 2a 20 20 31 30 20 2a 2f 20 22 74 72  ,. /*  10 */ "tr
228aa 61 6e 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ans_opt ::=",. /
228ab 2a 20 20 31 31 20 2a 2f 20 22 74 72 61 6e 73 5f  *  11 */ "trans_
228ac 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
228ad 49 4f 4e 22 2c 0a 20 2f 2a 20 20 31 32 20 2a 2f  ION",. /*  12 */
228ae 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20   "trans_opt ::= 
228af 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 22 2c  TRANSACTION nm",
228b0 0a 20 2f 2a 20 20 31 33 20 2a 2f 20 22 74 72 61  . /*  13 */ "tra
228b1 6e 73 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  nstype ::=",. /*
228b2 20 20 31 34 20 2a 2f 20 22 74 72 61 6e 73 74 79    14 */ "transty
228b3 70 65 20 3a 3a 3d 20 44 45 46 45 52 52 45 44 22  pe ::= DEFERRED"
228b4 2c 0a 20 2f 2a 20 20 31 35 20 2a 2f 20 22 74 72  ,. /*  15 */ "tr
228b5 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45  anstype ::= IMME
228b6 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 31 36 20  DIATE",. /*  16 
228b7 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
228b8 3d 20 45 58 43 4c 55 53 49 56 45 22 2c 0a 20 2f  = EXCLUSIVE",. /
228b9 2a 20 20 31 37 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  17 */ "cmd ::
228ba 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f  = COMMIT trans_o
228bb 70 74 22 2c 0a 20 2f 2a 20 20 31 38 20 2a 2f 20  pt",. /*  18 */ 
228bc 22 63 6d 64 20 3a 3a 3d 20 45 4e 44 20 74 72 61  "cmd ::= END tra
228bd 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 39  ns_opt",. /*  19
228be 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c   */ "cmd ::= ROL
228bf 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 22  LBACK trans_opt"
228c0 2c 0a 20 2f 2a 20 20 32 30 20 2a 2f 20 22 63 6d  ,. /*  20 */ "cm
228c1 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62  d ::= create_tab
228c2 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  le create_table_
228c3 61 72 67 73 22 2c 0a 20 2f 2a 20 20 32 31 20 2a  args",. /*  21 *
228c4 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 20  / "create_table 
228c5 3a 3a 3d 20 43 52 45 41 54 45 20 74 65 6d 70 20  ::= CREATE temp 
228c6 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74  TABLE ifnotexist
228c7 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20  s nm dbnm",. /* 
228c8 20 32 32 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69   22 */ "ifnotexi
228c9 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32  sts ::=",. /*  2
228ca 33 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73 74  3 */ "ifnotexist
228cb 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58 49  s ::= IF NOT EXI
228cc 53 54 53 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f  STS",. /*  24 */
228cd 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 22   "temp ::= TEMP"
228ce 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22 74 65  ,. /*  25 */ "te
228cf 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32 36  mp ::=",. /*  26
228d0 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c   */ "create_tabl
228d1 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f  e_args ::= LP co
228d2 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73  lumnlist conslis
228d3 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f 2a 20 20  t_opt RP",. /*  
228d4 32 37 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61  27 */ "create_ta
228d5 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20  ble_args ::= AS 
228d6 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 20 32 38  select",. /*  28
228d7 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20   */ "columnlist 
228d8 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43  ::= columnlist C
228d9 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f  OMMA column",. /
228da 2a 20 20 32 39 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  29 */ "column
228db 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 22  list ::= column"
228dc 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f 20 22 63 6f  ,. /*  30 */ "co
228dd 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69  lumn ::= columni
228de 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74 22  d type carglist"
228df 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f 20 22 63 6f  ,. /*  31 */ "co
228e0 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0a  lumnid ::= nm",.
228e1 20 2f 2a 20 20 33 32 20 2a 2f 20 22 69 64 20 3a   /*  32 */ "id :
228e2 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20 33 33 20  := ID",. /*  33 
228e3 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49 44 7c 53  */ "ids ::= ID|S
228e4 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 33 34 20  TRING",. /*  34 
228e5 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 49 44 22 2c 0a  */ "nm ::= ID",.
228e6 20 2f 2a 20 20 33 35 20 2a 2f 20 22 6e 6d 20 3a   /*  35 */ "nm :
228e7 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20  := STRING",. /* 
228e8 20 33 36 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a   36 */ "nm ::= J
228e9 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20 33 37  OIN_KW",. /*  37
228ea 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0a   */ "type ::=",.
228eb 20 2f 2a 20 20 33 38 20 2a 2f 20 22 74 79 70 65   /*  38 */ "type
228ec 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22 2c   ::= typetoken",
228ed 0a 20 2f 2a 20 20 33 39 20 2a 2f 20 22 74 79 70  . /*  39 */ "typ
228ee 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
228ef 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 30 20 2a 2f  ame",. /*  40 */
228f0 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20   "typetoken ::= 
228f1 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e  typename LP sign
228f2 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 31 20  ed RP",. /*  41 
228f3 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a  */ "typetoken ::
228f4 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69  = typename LP si
228f5 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65  gned COMMA signe
228f6 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 32 20 2a  d RP",. /*  42 *
228f7 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  / "typename ::= 
228f8 69 64 73 22 2c 0a 20 2f 2a 20 20 34 33 20 2a 2f  ids",. /*  43 */
228f9 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74   "typename ::= t
228fa 79 70 65 6e 61 6d 65 20 69 64 73 22 2c 0a 20 2f  ypename ids",. /
228fb 2a 20 20 34 34 20 2a 2f 20 22 73 69 67 6e 65 64  *  44 */ "signed
228fc 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a   ::= plus_num",.
228fd 20 2f 2a 20 20 34 35 20 2a 2f 20 22 73 69 67 6e   /*  45 */ "sign
228fe 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d  ed ::= minus_num
228ff 22 2c 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22 63  ",. /*  46 */ "c
22900 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67  arglist ::= carg
22901 6c 69 73 74 20 63 61 72 67 22 2c 0a 20 2f 2a 20  list carg",. /* 
22902 20 34 37 20 2a 2f 20 22 63 61 72 67 6c 69 73 74   47 */ "carglist
22903 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 34 38 20 2a   ::=",. /*  48 *
22904 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53  / "carg ::= CONS
22905 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 22  TRAINT nm ccons"
22906 2c 0a 20 2f 2a 20 20 34 39 20 2a 2f 20 22 63 61  ,. /*  49 */ "ca
22907 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20  rg ::= ccons",. 
22908 2f 2a 20 20 35 30 20 2a 2f 20 22 63 63 6f 6e 73  /*  50 */ "ccons
22909 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72   ::= DEFAULT ter
2290a 6d 22 2c 0a 20 2f 2a 20 20 35 31 20 2a 2f 20 22  m",. /*  51 */ "
2290b 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
2290c 54 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  T LP expr RP",. 
2290d 2f 2a 20 20 35 32 20 2a 2f 20 22 63 63 6f 6e 73  /*  52 */ "ccons
2290e 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55   ::= DEFAULT PLU
2290f 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 35 33  S term",. /*  53
22910 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
22911 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72  EFAULT MINUS ter
22912 6d 22 2c 0a 20 2f 2a 20 20 35 34 20 2a 2f 20 22  m",. /*  54 */ "
22913 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
22914 54 20 69 64 22 2c 0a 20 2f 2a 20 20 35 35 20 2a  T id",. /*  55 *
22915 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c  / "ccons ::= NUL
22916 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  L onconf",. /*  
22917 35 36 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  56 */ "ccons ::=
22918 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66   NOT NULL onconf
22919 22 2c 0a 20 2f 2a 20 20 35 37 20 2a 2f 20 22 63  ",. /*  57 */ "c
2291a 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59  cons ::= PRIMARY
2291b 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f   KEY sortorder o
2291c 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 22 2c 0a  nconf autoinc",.
2291d 20 2f 2a 20 20 35 38 20 2a 2f 20 22 63 63 6f 6e   /*  58 */ "ccon
2291e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63  s ::= UNIQUE onc
2291f 6f 6e 66 22 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f  onf",. /*  59 */
22920 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43   "ccons ::= CHEC
22921 4b 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  K LP expr RP",. 
22922 2f 2a 20 20 36 30 20 2a 2f 20 22 63 63 6f 6e 73  /*  60 */ "ccons
22923 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20   ::= REFERENCES 
22924 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72  nm idxlist_opt r
22925 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20 20 36 31  efargs",. /*  61
22926 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 64   */ "ccons ::= d
22927 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c  efer_subclause",
22928 0a 20 2f 2a 20 20 36 32 20 2a 2f 20 22 63 63 6f  . /*  62 */ "cco
22929 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69  ns ::= COLLATE i
2292a 64 22 2c 0a 20 2f 2a 20 20 36 33 20 2a 2f 20 22  d",. /*  63 */ "
2292b 61 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20 2f  autoinc ::=",. /
2292c 2a 20 20 36 34 20 2a 2f 20 22 61 75 74 6f 69 6e  *  64 */ "autoin
2292d 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 22 2c  c ::= AUTOINCR",
2292e 0a 20 2f 2a 20 20 36 35 20 2a 2f 20 22 72 65 66  . /*  65 */ "ref
2292f 61 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  args ::=",. /*  
22930 36 36 20 2a 2f 20 22 72 65 66 61 72 67 73 20 3a  66 */ "refargs :
22931 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66 61 72  := refargs refar
22932 67 22 2c 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22  g",. /*  67 */ "
22933 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48  refarg ::= MATCH
22934 20 6e 6d 22 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f   nm",. /*  68 */
22935 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20   "refarg ::= ON 
22936 44 45 4c 45 54 45 20 72 65 66 61 63 74 22 2c 0a  DELETE refact",.
22937 20 2f 2a 20 20 36 39 20 2a 2f 20 22 72 65 66 61   /*  69 */ "refa
22938 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45  rg ::= ON UPDATE
22939 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37   refact",. /*  7
2293a 30 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d  0 */ "refarg ::=
2293b 20 4f 4e 20 49 4e 53 45 52 54 20 72 65 66 61 63   ON INSERT refac
2293c 74 22 2c 0a 20 2f 2a 20 20 37 31 20 2a 2f 20 22  t",. /*  71 */ "
2293d 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 4e  refact ::= SET N
2293e 55 4c 4c 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f  ULL",. /*  72 */
2293f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54   "refact ::= SET
22940 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a 20 20   DEFAULT",. /*  
22941 37 33 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a  73 */ "refact ::
22942 3d 20 43 41 53 43 41 44 45 22 2c 0a 20 2f 2a 20  = CASCADE",. /* 
22943 20 37 34 20 2a 2f 20 22 72 65 66 61 63 74 20 3a   74 */ "refact :
22944 3a 3d 20 52 45 53 54 52 49 43 54 22 2c 0a 20 2f  := RESTRICT",. /
22945 2a 20 20 37 35 20 2a 2f 20 22 64 65 66 65 72 5f  *  75 */ "defer_
22946 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f  subclause ::= NO
22947 54 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  T DEFERRABLE ini
22948 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
22949 6f 70 74 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f  opt",. /*  76 */
2294a 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
2294b 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c 45  e ::= DEFERRABLE
2294c 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
2294d 72 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 37  red_opt",. /*  7
2294e 37 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65 72  7 */ "init_defer
2294f 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d  red_pred_opt ::=
22950 22 2c 0a 20 2f 2a 20 20 37 38 20 2a 2f 20 22 69  ",. /*  78 */ "i
22951 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
22952 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41  d_opt ::= INITIA
22953 4c 4c 59 20 44 45 46 45 52 52 45 44 22 2c 0a 20  LLY DEFERRED",. 
22954 2f 2a 20 20 37 39 20 2a 2f 20 22 69 6e 69 74 5f  /*  79 */ "init_
22955 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
22956 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20  t ::= INITIALLY 
22957 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20  IMMEDIATE",. /* 
22958 20 38 30 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74   80 */ "conslist
22959 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  _opt ::=",. /*  
2295a 38 31 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f  81 */ "conslist_
2295b 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f  opt ::= COMMA co
2295c 6e 73 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 38 32  nslist",. /*  82
2295d 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a   */ "conslist ::
2295e 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41  = conslist COMMA
2295f 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 38 33   tcons",. /*  83
22960 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a   */ "conslist ::
22961 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73  = conslist tcons
22962 22 2c 0a 20 2f 2a 20 20 38 34 20 2a 2f 20 22 63  ",. /*  84 */ "c
22963 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e  onslist ::= tcon
22964 73 22 2c 0a 20 2f 2a 20 20 38 35 20 2a 2f 20 22  s",. /*  85 */ "
22965 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52  tcons ::= CONSTR
22966 41 49 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 38  AINT nm",. /*  8
22967 36 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20  6 */ "tcons ::= 
22968 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69  PRIMARY KEY LP i
22969 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52  dxlist autoinc R
2296a 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  P onconf",. /*  
2296b 38 37 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  87 */ "tcons ::=
2296c 20 55 4e 49 51 55 45 20 4c 50 20 69 64 78 6c 69   UNIQUE LP idxli
2296d 73 74 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20  st RP onconf",. 
2296e 2f 2a 20 20 38 38 20 2a 2f 20 22 74 63 6f 6e 73  /*  88 */ "tcons
2296f 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78   ::= CHECK LP ex
22970 70 72 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20  pr RP onconf",. 
22971 2f 2a 20 20 38 39 20 2a 2f 20 22 74 63 6f 6e 73  /*  89 */ "tcons
22972 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59   ::= FOREIGN KEY
22973 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52   LP idxlist RP R
22974 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78  EFERENCES nm idx
22975 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73  list_opt refargs
22976 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
22977 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 39 30 20 2a  _opt",. /*  90 *
22978 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75  / "defer_subclau
22979 73 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  se_opt ::=",. /*
2297a 20 20 39 31 20 2a 2f 20 22 64 65 66 65 72 5f 73    91 */ "defer_s
2297b 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d  ubclause_opt ::=
2297c 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
2297d 22 2c 0a 20 2f 2a 20 20 39 32 20 2a 2f 20 22 6f  ",. /*  92 */ "o
2297e 6e 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  nconf ::=",. /* 
2297f 20 39 33 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a   93 */ "onconf :
22980 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72  := ON CONFLICT r
22981 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a  esolvetype",. /*
22982 20 20 39 34 20 2a 2f 20 22 6f 72 63 6f 6e 66 20    94 */ "orconf 
22983 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 35 20 2a 2f  ::=",. /*  95 */
22984 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20   "orconf ::= OR 
22985 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f  resolvetype",. /
22986 2a 20 20 39 36 20 2a 2f 20 22 72 65 73 6f 6c 76  *  96 */ "resolv
22987 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65 74  etype ::= raiset
22988 79 70 65 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f  ype",. /*  97 */
22989 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a   "resolvetype ::
2298a 3d 20 49 47 4e 4f 52 45 22 2c 0a 20 2f 2a 20 20  = IGNORE",. /*  
2298b 39 38 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79  98 */ "resolvety
2298c 70 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c  pe ::= REPLACE",
2298d 0a 20 2f 2a 20 20 39 39 20 2a 2f 20 22 63 6d 64  . /*  99 */ "cmd
2298e 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45 20   ::= DROP TABLE 
2298f 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
22990 65 22 2c 0a 20 2f 2a 20 31 30 30 20 2a 2f 20 22  e",. /* 100 */ "
22991 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20  ifexists ::= IF 
22992 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 31 30 31  EXISTS",. /* 101
22993 20 2a 2f 20 22 69 66 65 78 69 73 74 73 20 3a 3a   */ "ifexists ::
22994 3d 22 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f 20 22  =",. /* 102 */ "
22995 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20 74  cmd ::= CREATE t
22996 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78  emp VIEW ifnotex
22997 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20  ists nm dbnm AS 
22998 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 30 33  select",. /* 103
22999 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f   */ "cmd ::= DRO
2299a 50 20 56 49 45 57 20 69 66 65 78 69 73 74 73 20  P VIEW ifexists 
2299b 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31  fullname",. /* 1
2299c 30 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 73  04 */ "cmd ::= s
2299d 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 30 35 20  elect",. /* 105 
2299e 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 6f  */ "select ::= o
2299f 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31  neselect",. /* 1
229a0 30 36 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a  06 */ "select ::
229a1 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73 65  = select multise
229a2 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63  lect_op oneselec
229a3 74 22 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f 20 22  t",. /* 107 */ "
229a4 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
229a5 3a 3d 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31  := UNION",. /* 1
229a6 30 38 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65  08 */ "multisele
229a7 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20  ct_op ::= UNION 
229a8 41 4c 4c 22 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f  ALL",. /* 109 */
229a9 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70   "multiselect_op
229aa 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45   ::= EXCEPT|INTE
229ab 52 53 45 43 54 22 2c 0a 20 2f 2a 20 31 31 30 20  RSECT",. /* 110 
229ac 2a 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20 3a 3a  */ "oneselect ::
229ad 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e 63  = SELECT distinc
229ae 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f  t selcollist fro
229af 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f 75  m where_opt grou
229b0 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f  pby_opt having_o
229b1 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c  pt orderby_opt l
229b2 69 6d 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31  imit_opt",. /* 1
229b3 31 31 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20  11 */ "distinct 
229b4 3a 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c 0a 20  ::= DISTINCT",. 
229b5 2f 2a 20 31 31 32 20 2a 2f 20 22 64 69 73 74 69  /* 112 */ "disti
229b6 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f  nct ::= ALL",. /
229b7 2a 20 31 31 33 20 2a 2f 20 22 64 69 73 74 69 6e  * 113 */ "distin
229b8 63 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 31 34  ct ::=",. /* 114
229b9 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 20 73 65   */ "sclp ::= se
229ba 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 22 2c  lcollist COMMA",
229bb 0a 20 2f 2a 20 31 31 35 20 2a 2f 20 22 73 63 6c  . /* 115 */ "scl
229bc 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 31 36 20  p ::=",. /* 116 
229bd 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a  */ "selcollist :
229be 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73 22  := sclp expr as"
229bf 2c 0a 20 2f 2a 20 31 31 37 20 2a 2f 20 22 73 65  ,. /* 117 */ "se
229c0 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
229c1 70 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 31 38  p STAR",. /* 118
229c2 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20   */ "selcollist 
229c3 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20  ::= sclp nm DOT 
229c4 53 54 41 52 22 2c 0a 20 2f 2a 20 31 31 39 20 2a  STAR",. /* 119 *
229c5 2f 20 22 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 22  / "as ::= AS nm"
229c6 2c 0a 20 2f 2a 20 31 32 30 20 2a 2f 20 22 61 73  ,. /* 120 */ "as
229c7 20 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 31   ::= ids",. /* 1
229c8 32 31 20 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a  21 */ "as ::=",.
229c9 20 2f 2a 20 31 32 32 20 2a 2f 20 22 66 72 6f 6d   /* 122 */ "from
229ca 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 33 20 2a   ::=",. /* 123 *
229cb 2f 20 22 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d  / "from ::= FROM
229cc 20 73 65 6c 74 61 62 6c 69 73 74 22 2c 0a 20 2f   seltablist",. /
229cd 2a 20 31 32 34 20 2a 2f 20 22 73 74 6c 5f 70 72  * 124 */ "stl_pr
229ce 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c  efix ::= seltabl
229cf 69 73 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a  ist joinop",. /*
229d0 20 31 32 35 20 2a 2f 20 22 73 74 6c 5f 70 72 65   125 */ "stl_pre
229d1 66 69 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32  fix ::=",. /* 12
229d2 36 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74  6 */ "seltablist
229d3 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
229d4 6e 6d 20 64 62 6e 6d 20 61 73 20 6f 6e 5f 6f 70  nm dbnm as on_op
229d5 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f  t using_opt",. /
229d6 2a 20 31 32 37 20 2a 2f 20 22 73 65 6c 74 61 62  * 127 */ "seltab
229d7 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65  list ::= stl_pre
229d8 66 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73  fix LP seltablis
229d9 74 5f 70 61 72 65 6e 20 52 50 20 61 73 20 6f 6e  t_paren RP as on
229da 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c  _opt using_opt",
229db 0a 20 2f 2a 20 31 32 38 20 2a 2f 20 22 73 65 6c  . /* 128 */ "sel
229dc 74 61 62 6c 69 73 74 5f 70 61 72 65 6e 20 3a 3a  tablist_paren ::
229dd 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31  = select",. /* 1
229de 32 39 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73  29 */ "seltablis
229df 74 5f 70 61 72 65 6e 20 3a 3a 3d 20 73 65 6c 74  t_paren ::= selt
229e0 61 62 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 33 30  ablist",. /* 130
229e1 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 22 2c 0a   */ "dbnm ::=",.
229e2 20 2f 2a 20 31 33 31 20 2a 2f 20 22 64 62 6e 6d   /* 131 */ "dbnm
229e3 20 3a 3a 3d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f   ::= DOT nm",. /
229e4 2a 20 31 33 32 20 2a 2f 20 22 66 75 6c 6c 6e 61  * 132 */ "fullna
229e5 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 22 2c  me ::= nm dbnm",
229e6 0a 20 2f 2a 20 31 33 33 20 2a 2f 20 22 6a 6f 69  . /* 133 */ "joi
229e7 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f  nop ::= COMMA|JO
229e8 49 4e 22 2c 0a 20 2f 2a 20 31 33 34 20 2a 2f 20  IN",. /* 134 */ 
229e9 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e  "joinop ::= JOIN
229ea 5f 4b 57 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31  _KW JOIN",. /* 1
229eb 33 35 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a  35 */ "joinop ::
229ec 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49  = JOIN_KW nm JOI
229ed 4e 22 2c 0a 20 2f 2a 20 31 33 36 20 2a 2f 20 22  N",. /* 136 */ "
229ee 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
229ef 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 22 2c 0a  KW nm nm JOIN",.
229f0 20 2f 2a 20 31 33 37 20 2a 2f 20 22 6f 6e 5f 6f   /* 137 */ "on_o
229f1 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 22 2c  pt ::= ON expr",
229f2 0a 20 2f 2a 20 31 33 38 20 2a 2f 20 22 6f 6e 5f  . /* 138 */ "on_
229f3 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33  opt ::=",. /* 13
229f4 39 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20  9 */ "using_opt 
229f5 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69 6e 73  ::= USING LP ins
229f6 63 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  collist RP",. /*
229f7 20 31 34 30 20 2a 2f 20 22 75 73 69 6e 67 5f 6f   140 */ "using_o
229f8 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 31  pt ::=",. /* 141
229f9 20 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70 74   */ "orderby_opt
229fa 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 32 20 2a   ::=",. /* 142 *
229fb 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a  / "orderby_opt :
229fc 3a 3d 20 4f 52 44 45 52 20 42 59 20 73 6f 72 74  := ORDER BY sort
229fd 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 34 33 20 2a  list",. /* 143 *
229fe 2f 20 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20  / "sortlist ::= 
229ff 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73  sortlist COMMA s
22a00 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65  ortitem sortorde
22a01 72 22 2c 0a 20 2f 2a 20 31 34 34 20 2a 2f 20 22  r",. /* 144 */ "
22a02 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72  sortlist ::= sor
22a03 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22  titem sortorder"
22a04 2c 0a 20 2f 2a 20 31 34 35 20 2a 2f 20 22 73 6f  ,. /* 145 */ "so
22a05 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72 22  rtitem ::= expr"
22a06 2c 0a 20 2f 2a 20 31 34 36 20 2a 2f 20 22 73 6f  ,. /* 146 */ "so
22a07 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43 22  rtorder ::= ASC"
22a08 2c 0a 20 2f 2a 20 31 34 37 20 2a 2f 20 22 73 6f  ,. /* 147 */ "so
22a09 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53 43  rtorder ::= DESC
22a0a 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f 20 22 73  ",. /* 148 */ "s
22a0b 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 22 2c 0a 20  ortorder ::=",. 
22a0c 2f 2a 20 31 34 39 20 2a 2f 20 22 67 72 6f 75 70  /* 149 */ "group
22a0d 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  by_opt ::=",. /*
22a0e 20 31 35 30 20 2a 2f 20 22 67 72 6f 75 70 62 79   150 */ "groupby
22a0f 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42  _opt ::= GROUP B
22a10 59 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f  Y nexprlist",. /
22a11 2a 20 31 35 31 20 2a 2f 20 22 68 61 76 69 6e 67  * 151 */ "having
22a12 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  _opt ::=",. /* 1
22a13 35 32 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70  52 */ "having_op
22a14 74 20 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78 70  t ::= HAVING exp
22a15 72 22 2c 0a 20 2f 2a 20 31 35 33 20 2a 2f 20 22  r",. /* 153 */ "
22a16 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  limit_opt ::=",.
22a17 20 2f 2a 20 31 35 34 20 2a 2f 20 22 6c 69 6d 69   /* 154 */ "limi
22a18 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20  t_opt ::= LIMIT 
22a19 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35 35 20 2a  expr",. /* 155 *
22a1a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  / "limit_opt ::=
22a1b 20 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46 53   LIMIT expr OFFS
22a1c 45 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35  ET expr",. /* 15
22a1d 36 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20  6 */ "limit_opt 
22a1e 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 43  ::= LIMIT expr C
22a1f 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20  OMMA expr",. /* 
22a20 31 35 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  157 */ "cmd ::= 
22a21 44 45 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c  DELETE FROM full
22a22 6e 61 6d 65 20 77 68 65 72 65 5f 6f 70 74 22 2c  name where_opt",
22a23 0a 20 2f 2a 20 31 35 38 20 2a 2f 20 22 77 68 65  . /* 158 */ "whe
22a24 72 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  re_opt ::=",. /*
22a25 20 31 35 39 20 2a 2f 20 22 77 68 65 72 65 5f 6f   159 */ "where_o
22a26 70 74 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70  pt ::= WHERE exp
22a27 72 22 2c 0a 20 2f 2a 20 31 36 30 20 2a 2f 20 22  r",. /* 160 */ "
22a28 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f  cmd ::= UPDATE o
22a29 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20 53  rconf fullname S
22a2a 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
22a2b 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36 31 20 2a  _opt",. /* 161 *
22a2c 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d 20 73  / "setlist ::= s
22a2d 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20  etlist COMMA nm 
22a2e 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36  EQ expr",. /* 16
22a2f 32 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a  2 */ "setlist ::
22a30 3d 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20  = nm EQ expr",. 
22a31 2f 2a 20 31 36 33 20 2a 2f 20 22 63 6d 64 20 3a  /* 163 */ "cmd :
22a32 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
22a33 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
22a34 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45  ollist_opt VALUE
22a35 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50  S LP itemlist RP
22a36 22 2c 0a 20 2f 2a 20 31 36 34 20 2a 2f 20 22 63  ",. /* 164 */ "c
22a37 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
22a38 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20  d INTO fullname 
22a39 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
22a3a 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 36 35 20  elect",. /* 165 
22a3b 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  */ "cmd ::= inse
22a3c 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
22a3d 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
22a3e 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41 4c 55  opt DEFAULT VALU
22a3f 45 53 22 2c 0a 20 2f 2a 20 31 36 36 20 2a 2f 20  ES",. /* 166 */ 
22a40 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20  "insert_cmd ::= 
22a41 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 22 2c 0a  INSERT orconf",.
22a42 20 2f 2a 20 31 36 37 20 2a 2f 20 22 69 6e 73 65   /* 167 */ "inse
22a43 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41  rt_cmd ::= REPLA
22a44 43 45 22 2c 0a 20 2f 2a 20 31 36 38 20 2a 2f 20  CE",. /* 168 */ 
22a45 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74  "itemlist ::= it
22a46 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70  emlist COMMA exp
22a47 72 22 2c 0a 20 2f 2a 20 31 36 39 20 2a 2f 20 22  r",. /* 169 */ "
22a48 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70  itemlist ::= exp
22a49 72 22 2c 0a 20 2f 2a 20 31 37 30 20 2a 2f 20 22  r",. /* 170 */ "
22a4a 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a  inscollist_opt :
22a4b 3a 3d 22 2c 0a 20 2f 2a 20 31 37 31 20 2a 2f 20  :=",. /* 171 */ 
22a4c 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20  "inscollist_opt 
22a4d 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73  ::= LP inscollis
22a4e 74 20 52 50 22 2c 0a 20 2f 2a 20 31 37 32 20 2a  t RP",. /* 172 *
22a4f 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a  / "inscollist ::
22a50 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d  = inscollist COM
22a51 4d 41 20 6e 6d 22 2c 0a 20 2f 2a 20 31 37 33 20  MA nm",. /* 173 
22a52 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a  */ "inscollist :
22a53 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 31 37 34 20  := nm",. /* 174 
22a54 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 74 65 72  */ "expr ::= ter
22a55 6d 22 2c 0a 20 2f 2a 20 31 37 35 20 2a 2f 20 22  m",. /* 175 */ "
22a56 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72  expr ::= LP expr
22a57 20 52 50 22 2c 0a 20 2f 2a 20 31 37 36 20 2a 2f   RP",. /* 176 */
22a58 20 22 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 22   "term ::= NULL"
22a59 2c 0a 20 2f 2a 20 31 37 37 20 2a 2f 20 22 65 78  ,. /* 177 */ "ex
22a5a 70 72 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20  pr ::= ID",. /* 
22a5b 31 37 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  178 */ "expr ::=
22a5c 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 31   JOIN_KW",. /* 1
22a5d 37 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  79 */ "expr ::= 
22a5e 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20  nm DOT nm",. /* 
22a5f 31 38 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  180 */ "expr ::=
22a60 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54 20 6e   nm DOT nm DOT n
22a61 6d 22 2c 0a 20 2f 2a 20 31 38 31 20 2a 2f 20 22  m",. /* 181 */ "
22a62 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45 47 45 52  term ::= INTEGER
22a63 7c 46 4c 4f 41 54 7c 42 4c 4f 42 22 2c 0a 20 2f  |FLOAT|BLOB",. /
22a64 2a 20 31 38 32 20 2a 2f 20 22 74 65 72 6d 20 3a  * 182 */ "term :
22a65 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20  := STRING",. /* 
22a66 31 38 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  183 */ "expr ::=
22a67 20 52 45 47 49 53 54 45 52 22 2c 0a 20 2f 2a 20   REGISTER",. /* 
22a68 31 38 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  184 */ "expr ::=
22a69 20 56 41 52 49 41 42 4c 45 22 2c 0a 20 2f 2a 20   VARIABLE",. /* 
22a6a 31 38 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  185 */ "expr ::=
22a6b 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64   expr COLLATE id
22a6c 22 2c 0a 20 2f 2a 20 31 38 36 20 2a 2f 20 22 65  ",. /* 186 */ "e
22a6d 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20  xpr ::= CAST LP 
22a6e 65 78 70 72 20 41 53 20 74 79 70 65 74 6f 6b 65  expr AS typetoke
22a6f 6e 20 52 50 22 2c 0a 20 2f 2a 20 31 38 37 20 2a  n RP",. /* 187 *
22a70 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c  / "expr ::= ID L
22a71 50 20 64 69 73 74 69 6e 63 74 20 65 78 70 72 6c  P distinct exprl
22a72 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 38 38  ist RP",. /* 188
22a73 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44   */ "expr ::= ID
22a74 20 4c 50 20 53 54 41 52 20 52 50 22 2c 0a 20 2f   LP STAR RP",. /
22a75 2a 20 31 38 39 20 2a 2f 20 22 74 65 72 6d 20 3a  * 189 */ "term :
22a76 3a 3d 20 43 54 49 4d 45 5f 4b 57 22 2c 0a 20 2f  := CTIME_KW",. /
22a77 2a 20 31 39 30 20 2a 2f 20 22 65 78 70 72 20 3a  * 190 */ "expr :
22a78 3a 3d 20 65 78 70 72 20 41 4e 44 20 65 78 70 72  := expr AND expr
22a79 22 2c 0a 20 2f 2a 20 31 39 31 20 2a 2f 20 22 65  ",. /* 191 */ "e
22a7a 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20  xpr ::= expr OR 
22a7b 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 32 20 2a  expr",. /* 192 *
22a7c 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
22a7d 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70   LT|GT|GE|LE exp
22a7e 72 22 2c 0a 20 2f 2a 20 31 39 33 20 2a 2f 20 22  r",. /* 193 */ "
22a7f 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 45 51  expr ::= expr EQ
22a80 7c 4e 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  |NE expr",. /* 1
22a81 39 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  94 */ "expr ::= 
22a82 65 78 70 72 20 42 49 54 41 4e 44 7c 42 49 54 4f  expr BITAND|BITO
22a83 52 7c 4c 53 48 49 46 54 7c 52 53 48 49 46 54 20  R|LSHIFT|RSHIFT 
22a84 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 35 20 2a  expr",. /* 195 *
22a85 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
22a86 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65 78 70 72   PLUS|MINUS expr
22a87 22 2c 0a 20 2f 2a 20 31 39 36 20 2a 2f 20 22 65  ",. /* 196 */ "e
22a88 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 53 54 41  xpr ::= expr STA
22a89 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65 78 70 72  R|SLASH|REM expr
22a8a 22 2c 0a 20 2f 2a 20 31 39 37 20 2a 2f 20 22 65  ",. /* 197 */ "e
22a8b 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e  xpr ::= expr CON
22a8c 43 41 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  CAT expr",. /* 1
22a8d 39 38 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a  98 */ "likeop ::
22a8e 3d 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20  = LIKE_KW",. /* 
22a8f 31 39 39 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a  199 */ "likeop :
22a90 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 22 2c  := NOT LIKE_KW",
22a91 0a 20 2f 2a 20 32 30 30 20 2a 2f 20 22 6c 69 6b  . /* 200 */ "lik
22a92 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 22 2c 0a  eop ::= MATCH",.
22a93 20 2f 2a 20 32 30 31 20 2a 2f 20 22 6c 69 6b 65   /* 201 */ "like
22a94 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43 48  op ::= NOT MATCH
22a95 22 2c 0a 20 2f 2a 20 32 30 32 20 2a 2f 20 22 65  ",. /* 202 */ "e
22a96 73 63 61 70 65 20 3a 3a 3d 20 45 53 43 41 50 45  scape ::= ESCAPE
22a97 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 33 20   expr",. /* 203 
22a98 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 22 2c  */ "escape ::=",
22a99 0a 20 2f 2a 20 32 30 34 20 2a 2f 20 22 65 78 70  . /* 204 */ "exp
22a9a 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f  r ::= expr likeo
22a9b 70 20 65 78 70 72 20 65 73 63 61 70 65 22 2c 0a  p expr escape",.
22a9c 20 2f 2a 20 32 30 35 20 2a 2f 20 22 65 78 70 72   /* 205 */ "expr
22a9d 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55 4c 4c   ::= expr ISNULL
22a9e 7c 4e 4f 54 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32  |NOTNULL",. /* 2
22a9f 30 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  06 */ "expr ::= 
22aa0 65 78 70 72 20 49 53 20 4e 55 4c 4c 22 2c 0a 20  expr IS NULL",. 
22aa1 2f 2a 20 32 30 37 20 2a 2f 20 22 65 78 70 72 20  /* 207 */ "expr 
22aa2 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c  ::= expr NOT NUL
22aa3 4c 22 2c 0a 20 2f 2a 20 32 30 38 20 2a 2f 20 22  L",. /* 208 */ "
22aa4 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53  expr ::= expr IS
22aa5 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20   NOT NULL",. /* 
22aa6 32 30 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  209 */ "expr ::=
22aa7 20 4e 4f 54 7c 42 49 54 4e 4f 54 20 65 78 70 72   NOT|BITNOT expr
22aa8 22 2c 0a 20 2f 2a 20 32 31 30 20 2a 2f 20 22 65  ",. /* 210 */ "e
22aa9 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78  xpr ::= MINUS ex
22aaa 70 72 22 2c 0a 20 2f 2a 20 32 31 31 20 2a 2f 20  pr",. /* 211 */ 
22aab 22 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65  "expr ::= PLUS e
22aac 78 70 72 22 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f  xpr",. /* 212 */
22aad 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d   "between_op ::=
22aae 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32   BETWEEN",. /* 2
22aaf 31 33 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f  13 */ "between_o
22ab0 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45  p ::= NOT BETWEE
22ab1 4e 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22  N",. /* 214 */ "
22ab2 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65  expr ::= expr be
22ab3 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e  tween_op expr AN
22ab4 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 35  D expr",. /* 215
22ab5 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 49   */ "in_op ::= I
22ab6 4e 22 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20 22  N",. /* 216 */ "
22ab7 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e  in_op ::= NOT IN
22ab8 22 2c 0a 20 2f 2a 20 32 31 37 20 2a 2f 20 22 65  ",. /* 217 */ "e
22ab9 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f  xpr ::= expr in_
22aba 6f 70 20 4c 50 20 65 78 70 72 6c 69 73 74 20 52  op LP exprlist R
22abb 50 22 2c 0a 20 2f 2a 20 32 31 38 20 2a 2f 20 22  P",. /* 218 */ "
22abc 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65  expr ::= LP sele
22abd 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 31 39 20  ct RP",. /* 219 
22abe 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
22abf 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65 63  r in_op LP selec
22ac0 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 30 20 2a  t RP",. /* 220 *
22ac1 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
22ac2 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 22 2c   in_op nm dbnm",
22ac3 0a 20 2f 2a 20 32 32 31 20 2a 2f 20 22 65 78 70  . /* 221 */ "exp
22ac4 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20  r ::= EXISTS LP 
22ac5 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20  select RP",. /* 
22ac6 32 32 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  222 */ "expr ::=
22ac7 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61   CASE case_opera
22ac8 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74  nd case_exprlist
22ac9 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 22 2c   case_else END",
22aca 0a 20 2f 2a 20 32 32 33 20 2a 2f 20 22 63 61 73  . /* 223 */ "cas
22acb 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63  e_exprlist ::= c
22acc 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57 48 45  ase_exprlist WHE
22acd 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72  N expr THEN expr
22ace 22 2c 0a 20 2f 2a 20 32 32 34 20 2a 2f 20 22 63  ",. /* 224 */ "c
22acf 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
22ad0 20 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20   WHEN expr THEN 
22ad1 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 35 20 2a  expr",. /* 225 *
22ad2 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d  / "case_else ::=
22ad3 20 45 4c 53 45 20 65 78 70 72 22 2c 0a 20 2f 2a   ELSE expr",. /*
22ad4 20 32 32 36 20 2a 2f 20 22 63 61 73 65 5f 65 6c   226 */ "case_el
22ad5 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 32 37  se ::=",. /* 227
22ad6 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72 61 6e   */ "case_operan
22ad7 64 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a  d ::= expr",. /*
22ad8 20 32 32 38 20 2a 2f 20 22 63 61 73 65 5f 6f 70   228 */ "case_op
22ad9 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  erand ::=",. /* 
22ada 32 32 39 20 2a 2f 20 22 65 78 70 72 6c 69 73 74  229 */ "exprlist
22adb 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 22 2c   ::= nexprlist",
22adc 0a 20 2f 2a 20 32 33 30 20 2a 2f 20 22 65 78 70  . /* 230 */ "exp
22add 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  rlist ::=",. /* 
22ade 32 33 31 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73  231 */ "nexprlis
22adf 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20  t ::= nexprlist 
22ae0 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a  COMMA expr",. /*
22ae1 20 32 33 32 20 2a 2f 20 22 6e 65 78 70 72 6c 69   232 */ "nexprli
22ae2 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f  st ::= expr",. /
22ae3 2a 20 32 33 33 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 233 */ "cmd ::
22ae4 3d 20 43 52 45 41 54 45 20 75 6e 69 71 75 65 66  = CREATE uniquef
22ae5 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74 65  lag INDEX ifnote
22ae6 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e  xists nm dbnm ON
22ae7 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20 52   nm LP idxlist R
22ae8 50 22 2c 0a 20 2f 2a 20 32 33 34 20 2a 2f 20 22  P",. /* 234 */ "
22ae9 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55  uniqueflag ::= U
22aea 4e 49 51 55 45 22 2c 0a 20 2f 2a 20 32 33 35 20  NIQUE",. /* 235 
22aeb 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a  */ "uniqueflag :
22aec 3a 3d 22 2c 0a 20 2f 2a 20 32 33 36 20 2a 2f 20  :=",. /* 236 */ 
22aed 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  "idxlist_opt ::=
22aee 22 2c 0a 20 2f 2a 20 32 33 37 20 2a 2f 20 22 69  ",. /* 237 */ "i
22aef 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c  dxlist_opt ::= L
22af0 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c 0a 20  P idxlist RP",. 
22af1 2f 2a 20 32 33 38 20 2a 2f 20 22 69 64 78 6c 69  /* 238 */ "idxli
22af2 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43  st ::= idxlist C
22af3 4f 4d 4d 41 20 69 64 78 69 74 65 6d 20 63 6f 6c  OMMA idxitem col
22af4 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c  late sortorder",
22af5 0a 20 2f 2a 20 32 33 39 20 2a 2f 20 22 69 64 78  . /* 239 */ "idx
22af6 6c 69 73 74 20 3a 3a 3d 20 69 64 78 69 74 65 6d  list ::= idxitem
22af7 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64   collate sortord
22af8 65 72 22 2c 0a 20 2f 2a 20 32 34 30 20 2a 2f 20  er",. /* 240 */ 
22af9 22 69 64 78 69 74 65 6d 20 3a 3a 3d 20 6e 6d 22  "idxitem ::= nm"
22afa 2c 0a 20 2f 2a 20 32 34 31 20 2a 2f 20 22 63 6f  ,. /* 241 */ "co
22afb 6c 6c 61 74 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  llate ::=",. /* 
22afc 32 34 32 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20  242 */ "collate 
22afd 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 22 2c  ::= COLLATE id",
22afe 0a 20 2f 2a 20 32 34 33 20 2a 2f 20 22 63 6d 64  . /* 243 */ "cmd
22aff 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20   ::= DROP INDEX 
22b00 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
22b01 65 22 2c 0a 20 2f 2a 20 32 34 34 20 2a 2f 20 22  e",. /* 244 */ "
22b02 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 22 2c  cmd ::= VACUUM",
22b03 0a 20 2f 2a 20 32 34 35 20 2a 2f 20 22 63 6d 64  . /* 245 */ "cmd
22b04 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 22 2c   ::= VACUUM nm",
22b05 0a 20 2f 2a 20 32 34 36 20 2a 2f 20 22 63 6d 64  . /* 246 */ "cmd
22b06 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
22b07 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20  bnm EQ nmnum",. 
22b08 2f 2a 20 32 34 37 20 2a 2f 20 22 63 6d 64 20 3a  /* 247 */ "cmd :
22b09 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
22b0a 6d 20 45 51 20 4f 4e 22 2c 0a 20 2f 2a 20 32 34  m EQ ON",. /* 24
22b0b 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52  8 */ "cmd ::= PR
22b0c 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
22b0d 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20  minus_num",. /* 
22b0e 32 34 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  249 */ "cmd ::= 
22b0f 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c  PRAGMA nm dbnm L
22b10 50 20 6e 6d 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a  P nmnum RP",. /*
22b11 20 32 35 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   250 */ "cmd ::=
22b12 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 22   PRAGMA nm dbnm"
22b13 2c 0a 20 2f 2a 20 32 35 31 20 2a 2f 20 22 6e 6d  ,. /* 251 */ "nm
22b14 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d  num ::= plus_num
22b15 22 2c 0a 20 2f 2a 20 32 35 32 20 2a 2f 20 22 6e  ",. /* 252 */ "n
22b16 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f  mnum ::= nm",. /
22b17 2a 20 32 35 33 20 2a 2f 20 22 70 6c 75 73 5f 6e  * 253 */ "plus_n
22b18 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20  um ::= plus_opt 
22b19 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 35 34  number",. /* 254
22b1a 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20 3a   */ "minus_num :
22b1b 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72 22  := MINUS number"
22b1c 2c 0a 20 2f 2a 20 32 35 35 20 2a 2f 20 22 6e 75  ,. /* 255 */ "nu
22b1d 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52  mber ::= INTEGER
22b1e 7c 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 35 36  |FLOAT",. /* 256
22b1f 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a   */ "plus_opt ::
22b20 3d 20 50 4c 55 53 22 2c 0a 20 2f 2a 20 32 35 37  = PLUS",. /* 257
22b21 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a   */ "plus_opt ::
22b22 3d 22 2c 0a 20 2f 2a 20 32 35 38 20 2a 2f 20 22  =",. /* 258 */ "
22b23 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20 74  cmd ::= CREATE t
22b24 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45 47 49  rigger_decl BEGI
22b25 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  N trigger_cmd_li
22b26 73 74 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 35 39  st END",. /* 259
22b27 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 64 65 63   */ "trigger_dec
22b28 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47  l ::= temp TRIGG
22b29 45 52 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e  ER ifnotexists n
22b2a 6d 20 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74  m dbnm trigger_t
22b2b 69 6d 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e  ime trigger_even
22b2c 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f  t ON fullname fo
22b2d 72 65 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65  reach_clause whe
22b2e 6e 5f 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 32  n_clause",. /* 2
22b2f 36 30 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74  60 */ "trigger_t
22b30 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 22 2c  ime ::= BEFORE",
22b31 0a 20 2f 2a 20 32 36 31 20 2a 2f 20 22 74 72 69  . /* 261 */ "tri
22b32 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46  gger_time ::= AF
22b33 54 45 52 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f  TER",. /* 262 */
22b34 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a   "trigger_time :
22b35 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46 22 2c 0a  := INSTEAD OF",.
22b36 20 2f 2a 20 32 36 33 20 2a 2f 20 22 74 72 69 67   /* 263 */ "trig
22b37 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c 0a 20  ger_time ::=",. 
22b38 2f 2a 20 32 36 34 20 2a 2f 20 22 74 72 69 67 67  /* 264 */ "trigg
22b39 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c  er_event ::= DEL
22b3a 45 54 45 7c 49 4e 53 45 52 54 22 2c 0a 20 2f 2a  ETE|INSERT",. /*
22b3b 20 32 36 35 20 2a 2f 20 22 74 72 69 67 67 65 72   265 */ "trigger
22b3c 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54  _event ::= UPDAT
22b3d 45 22 2c 0a 20 2f 2a 20 32 36 36 20 2a 2f 20 22  E",. /* 266 */ "
22b3e 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a  trigger_event ::
22b3f 3d 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63  = UPDATE OF insc
22b40 6f 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 36 37  ollist",. /* 267
22b41 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61   */ "foreach_cla
22b42 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 36  use ::=",. /* 26
22b43 38 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c  8 */ "foreach_cl
22b44 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43  ause ::= FOR EAC
22b45 48 20 52 4f 57 22 2c 0a 20 2f 2a 20 32 36 39 20  H ROW",. /* 269 
22b46 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20  */ "when_clause 
22b47 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f  ::=",. /* 270 */
22b48 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a   "when_clause ::
22b49 3d 20 57 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f  = WHEN expr",. /
22b4a 2a 20 32 37 31 20 2a 2f 20 22 74 72 69 67 67 65  * 271 */ "trigge
22b4b 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74  r_cmd_list ::= t
22b4c 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
22b4d 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49  trigger_cmd SEMI
22b4e 22 2c 0a 20 2f 2a 20 32 37 32 20 2a 2f 20 22 74  ",. /* 272 */ "t
22b4f 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
22b50 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37 33 20 2a 2f  ::=",. /* 273 */
22b51 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
22b52 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
22b53 6e 6d 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  nm SET setlist w
22b54 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32  here_opt",. /* 2
22b55 37 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  74 */ "trigger_c
22b56 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
22b57 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c  d INTO nm inscol
22b58 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20  list_opt VALUES 
22b59 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c  LP itemlist RP",
22b5a 0a 20 2f 2a 20 32 37 35 20 2a 2f 20 22 74 72 69  . /* 275 */ "tri
22b5b 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73  gger_cmd ::= ins
22b5c 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20  ert_cmd INTO nm 
22b5d 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
22b5e 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32 37 36 20  elect",. /* 276 
22b5f 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20  */ "trigger_cmd 
22b60 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
22b61 6e 6d 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20  nm where_opt",. 
22b62 2f 2a 20 32 37 37 20 2a 2f 20 22 74 72 69 67 67  /* 277 */ "trigg
22b63 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  er_cmd ::= selec
22b64 74 22 2c 0a 20 2f 2a 20 32 37 38 20 2a 2f 20 22  t",. /* 278 */ "
22b65 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c  expr ::= RAISE L
22b66 50 20 49 47 4e 4f 52 45 20 52 50 22 2c 0a 20 2f  P IGNORE RP",. /
22b67 2a 20 32 37 39 20 2a 2f 20 22 65 78 70 72 20 3a  * 279 */ "expr :
22b68 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69 73  := RAISE LP rais
22b69 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52  etype COMMA nm R
22b6a 50 22 2c 0a 20 2f 2a 20 32 38 30 20 2a 2f 20 22  P",. /* 280 */ "
22b6b 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f  raisetype ::= RO
22b6c 4c 4c 42 41 43 4b 22 2c 0a 20 2f 2a 20 32 38 31  LLBACK",. /* 281
22b6d 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a   */ "raisetype :
22b6e 3a 3d 20 41 42 4f 52 54 22 2c 0a 20 2f 2a 20 32  := ABORT",. /* 2
22b6f 38 32 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65  82 */ "raisetype
22b70 20 3a 3a 3d 20 46 41 49 4c 22 2c 0a 20 2f 2a 20   ::= FAIL",. /* 
22b71 32 38 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  283 */ "cmd ::= 
22b72 44 52 4f 50 20 54 52 49 47 47 45 52 20 69 66 65  DROP TRIGGER ife
22b73 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c  xists fullname",
22b74 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22 63 6d 64  . /* 284 */ "cmd
22b75 20 3a 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61   ::= ATTACH data
22b76 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72  base_kw_opt expr
22b77 20 41 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74   AS expr key_opt
22b78 22 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f 20 22 63  ",. /* 285 */ "c
22b79 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61  md ::= DETACH da
22b7a 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78  tabase_kw_opt ex
22b7b 70 72 22 2c 0a 20 2f 2a 20 32 38 36 20 2a 2f 20  pr",. /* 286 */ 
22b7c 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  "key_opt ::=",. 
22b7d 2f 2a 20 32 38 37 20 2a 2f 20 22 6b 65 79 5f 6f  /* 287 */ "key_o
22b7e 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 22  pt ::= KEY expr"
22b7f 2c 0a 20 2f 2a 20 32 38 38 20 2a 2f 20 22 64 61  ,. /* 288 */ "da
22b80 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a  tabase_kw_opt ::
22b81 3d 20 44 41 54 41 42 41 53 45 22 2c 0a 20 2f 2a  = DATABASE",. /*
22b82 20 32 38 39 20 2a 2f 20 22 64 61 74 61 62 61 73   289 */ "databas
22b83 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  e_kw_opt ::=",. 
22b84 2f 2a 20 32 39 30 20 2a 2f 20 22 63 6d 64 20 3a  /* 290 */ "cmd :
22b85 3a 3d 20 52 45 49 4e 44 45 58 22 2c 0a 20 2f 2a  := REINDEX",. /*
22b86 20 32 39 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   291 */ "cmd ::=
22b87 20 52 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d   REINDEX nm dbnm
22b88 22 2c 0a 20 2f 2a 20 32 39 32 20 2a 2f 20 22 63  ",. /* 292 */ "c
22b89 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c  md ::= ANALYZE",
22b8a 0a 20 2f 2a 20 32 39 33 20 2a 2f 20 22 63 6d 64  . /* 293 */ "cmd
22b8b 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20   ::= ANALYZE nm 
22b8c 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32 39 34 20 2a  dbnm",. /* 294 *
22b8d 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52  / "cmd ::= ALTER
22b8e 20 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20   TABLE fullname 
22b8f 52 45 4e 41 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20  RENAME TO nm",. 
22b90 2f 2a 20 32 39 35 20 2a 2f 20 22 63 6d 64 20 3a  /* 295 */ "cmd :
22b91 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61  := ALTER TABLE a
22b92 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
22b93 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f  me ADD kwcolumn_
22b94 6f 70 74 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a  opt column",. /*
22b95 20 32 39 36 20 2a 2f 20 22 61 64 64 5f 63 6f 6c   296 */ "add_col
22b96 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d  umn_fullname ::=
22b97 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20   fullname",. /* 
22b98 32 39 37 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e  297 */ "kwcolumn
22b99 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  _opt ::=",. /* 2
22b9a 39 38 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f  98 */ "kwcolumn_
22b9b 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57  opt ::= COLUMNKW
22b9c 22 2c 0a 20 2f 2a 20 32 39 39 20 2a 2f 20 22 63  ",. /* 299 */ "c
22b9d 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74  md ::= create_vt
22b9e 61 62 22 2c 0a 20 2f 2a 20 33 30 30 20 2a 2f 20  ab",. /* 300 */ 
22b9f 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f  "cmd ::= create_
22ba0 76 74 61 62 20 4c 50 20 76 74 61 62 61 72 67 6c  vtab LP vtabargl
22ba1 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 30 31  ist RP",. /* 301
22ba2 20 2a 2f 20 22 63 72 65 61 74 65 5f 76 74 61 62   */ "create_vtab
22ba3 20 3a 3a 3d 20 43 52 45 41 54 45 20 56 49 52 54   ::= CREATE VIRT
22ba4 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62 6e  UAL TABLE nm dbn
22ba5 6d 20 55 53 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a  m USING nm",. /*
22ba6 20 33 30 32 20 2a 2f 20 22 76 74 61 62 61 72 67   302 */ "vtabarg
22ba7 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67  list ::= vtabarg
22ba8 22 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22 76  ",. /* 303 */ "v
22ba9 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
22baa 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41  tabarglist COMMA
22bab 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33   vtabarg",. /* 3
22bac 30 34 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a  04 */ "vtabarg :
22bad 3a 3d 22 2c 0a 20 2f 2a 20 33 30 35 20 2a 2f 20  :=",. /* 305 */ 
22bae 22 76 74 61 62 61 72 67 20 3a 3a 3d 20 76 74 61  "vtabarg ::= vta
22baf 62 61 72 67 20 76 74 61 62 61 72 67 74 6f 6b 65  barg vtabargtoke
22bb0 6e 22 2c 0a 20 2f 2a 20 33 30 36 20 2a 2f 20 22  n",. /* 306 */ "
22bb1 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d  vtabargtoken ::=
22bb2 20 41 4e 59 22 2c 0a 20 2f 2a 20 33 30 37 20 2a   ANY",. /* 307 *
22bb3 2f 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 20  / "vtabargtoken 
22bb4 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52  ::= lp anylist R
22bb5 50 22 2c 0a 20 2f 2a 20 33 30 38 20 2a 2f 20 22  P",. /* 308 */ "
22bb6 6c 70 20 3a 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20  lp ::= LP",. /* 
22bb7 33 30 39 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20  309 */ "anylist 
22bb8 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 31 30 20 2a 2f  ::=",. /* 310 */
22bb9 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e   "anylist ::= an
22bba 79 6c 69 73 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23  ylist ANY",.};.#
22bbb 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20  endif /* NDEBUG 
22bbc 2a 2f 0a 0a 0a 23 69 66 20 59 59 53 54 41 43 4b  */...#if YYSTACK
22bbd 44 45 50 54 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54  DEPTH<=0./*.** T
22bbe 72 79 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74  ry to increase t
22bbf 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70  he size of the p
22bc0 61 72 73 65 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a  arser stack..*/.
22bc1 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 47 72  static void yyGr
22bc2 6f 77 53 74 61 63 6b 28 79 79 50 61 72 73 65 72  owStack(yyParser
22bc3 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 65 77 53   *p){.  int newS
22bc4 69 7a 65 3b 0a 20 20 79 79 53 74 61 63 6b 45 6e  ize;.  yyStackEn
22bc5 74 72 79 20 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65  try *pNew;..  ne
22bc6 77 53 69 7a 65 20 3d 20 70 2d 3e 79 79 73 74 6b  wSize = p->yystk
22bc7 73 7a 2a 32 20 2b 20 31 30 30 3b 0a 20 20 70 4e  sz*2 + 100;.  pN
22bc8 65 77 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2d 3e  ew = realloc(p->
22bc9 79 79 73 74 61 63 6b 2c 20 6e 65 77 53 69 7a 65  yystack, newSize
22bca 2a 73 69 7a 65 6f 66 28 70 4e 65 77 5b 30 5d 29  *sizeof(pNew[0])
22bcb 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  );.  if( pNew ){
22bcc 0a 20 20 20 20 70 2d 3e 79 79 73 74 61 63 6b 20  .    p->yystack 
22bcd 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 79  = pNew;.    p->y
22bce 79 73 74 6b 73 7a 20 3d 20 6e 65 77 53 69 7a 65  ystksz = newSize
22bcf 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
22bd0 0a 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65  .    if( yyTrace
22bd1 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 66 70  FILE ){.      fp
22bd2 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
22bd3 45 2c 22 25 73 53 74 61 63 6b 20 67 72 6f 77 73  E,"%sStack grows
22bd4 20 74 6f 20 25 64 20 65 6e 74 72 69 65 73 21 5c   to %d entries!\
22bd5 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
22bd6 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
22bd7 20 70 2d 3e 79 79 73 74 6b 73 7a 29 3b 0a 20 20   p->yystksz);.  
22bd8 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
22bd9 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20  .#endif../* .** 
22bda 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  This function al
22bdb 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20 70 61  locates a new pa
22bdc 72 73 65 72 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c  rser..** The onl
22bdd 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  y argument is a 
22bde 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
22bdf 63 74 69 6f 6e 20 77 68 69 63 68 20 77 6f 72 6b  ction which work
22be0 73 20 6c 69 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63  s like.** malloc
22be1 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a  ..**.** Inputs:.
22be2 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
22be3 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  the function use
22be4 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65  d to allocate me
22be5 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70  mory..**.** Outp
22be6 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  uts:.** A pointe
22be7 72 20 74 6f 20 61 20 70 61 72 73 65 72 2e 20 20  r to a parser.  
22be8 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20  This pointer is 
22be9 75 73 65 64 20 69 6e 20 73 75 62 73 65 71 75 65  used in subseque
22bea 6e 74 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73  nt calls.** to s
22beb 71 6c 69 74 65 33 50 61 72 73 65 72 20 61 6e 64  qlite3Parser and
22bec 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
22bed 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ee..*/.SQLITE_PR
22bee 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
22bef 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76  te3ParserAlloc(v
22bf0 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f  oid *(*mallocPro
22bf1 63 29 28 73 69 7a 65 5f 74 29 29 7b 0a 20 20 79  c)(size_t)){.  y
22bf2 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
22bf3 3b 0a 20 20 70 50 61 72 73 65 72 20 3d 20 28 79  ;.  pParser = (y
22bf4 79 50 61 72 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f  yParser*)(*mallo
22bf5 63 50 72 6f 63 29 28 20 28 73 69 7a 65 5f 74 29  cProc)( (size_t)
22bf6 73 69 7a 65 6f 66 28 79 79 50 61 72 73 65 72 29  sizeof(yyParser)
22bf7 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65   );.  if( pParse
22bf8 72 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 72  r ){.    pParser
22bf9 2d 3e 79 79 69 64 78 20 3d 20 2d 31 3b 0a 23 69  ->yyidx = -1;.#i
22bfa 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d  f YYSTACKDEPTH<=
22bfb 30 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61 63  0.    yyGrowStac
22bfc 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 65 6e 64  k(pParser);.#end
22bfd 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
22bfe 70 50 61 72 73 65 72 3b 0a 7d 0a 0a 2f 2a 20 54  pParser;.}../* T
22bff 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e  he following fun
22c00 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68  ction deletes th
22c01 65 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74  e value associat
22c02 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 73 79 6d  ed with a.** sym
22c03 62 6f 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f 6c  bol.  The symbol
22c04 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
22c05 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e   terminal or non
22c06 74 65 72 6d 69 6e 61 6c 2e 0a 2a 2a 20 22 79 79  terminal..** "yy
22c07 6d 61 6a 6f 72 22 20 69 73 20 74 68 65 20 73 79  major" is the sy
22c08 6d 62 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20 22  mbol code, and "
22c09 79 79 70 6d 69 6e 6f 72 22 20 69 73 20 61 20 70  yypminor" is a p
22c0a 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65  ointer to.** the
22c0b 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
22c0c 63 20 76 6f 69 64 20 79 79 5f 64 65 73 74 72 75  c void yy_destru
22c0d 63 74 6f 72 28 59 59 43 4f 44 45 54 59 50 45 20  ctor(YYCODETYPE 
22c0e 79 79 6d 61 6a 6f 72 2c 20 59 59 4d 49 4e 4f 52  yymajor, YYMINOR
22c0f 54 59 50 45 20 2a 79 79 70 6d 69 6e 6f 72 29 7b  TYPE *yypminor){
22c10 0a 20 20 73 77 69 74 63 68 28 20 79 79 6d 61 6a  .  switch( yymaj
22c11 6f 72 20 29 7b 0a 20 20 20 20 2f 2a 20 48 65 72  or ){.    /* Her
22c12 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 74 68  e is inserted th
22c13 65 20 61 63 74 69 6f 6e 73 20 77 68 69 63 68 20  e actions which 
22c14 74 61 6b 65 20 70 6c 61 63 65 20 77 68 65 6e 20  take place when 
22c15 61 0a 20 20 20 20 2a 2a 20 74 65 72 6d 69 6e 61  a.    ** termina
22c16 6c 20 6f 72 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61  l or non-termina
22c17 6c 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 20  l is destroyed. 
22c18 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e   This can happen
22c19 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65  .    ** when the
22c1a 20 73 79 6d 62 6f 6c 20 69 73 20 70 6f 70 70 65   symbol is poppe
22c1b 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b  d from the stack
22c1c 20 64 75 72 69 6e 67 20 61 0a 20 20 20 20 2a 2a   during a.    **
22c1d 20 72 65 64 75 63 65 20 6f 72 20 64 75 72 69 6e   reduce or durin
22c1e 67 20 65 72 72 6f 72 20 70 72 6f 63 65 73 73 69  g error processi
22c1f 6e 67 20 6f 72 20 77 68 65 6e 20 61 20 70 61 72  ng or when a par
22c20 73 65 72 20 69 73 20 0a 20 20 20 20 2a 2a 20 62  ser is .    ** b
22c21 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 62  eing destroyed b
22c22 65 66 6f 72 65 20 69 74 20 69 73 20 66 69 6e 69  efore it is fini
22c23 73 68 65 64 20 70 61 72 73 69 6e 67 2e 0a 20 20  shed parsing..  
22c24 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65    **.    ** Note
22c25 3a 20 64 75 72 69 6e 67 20 61 20 72 65 64 75 63  : during a reduc
22c26 65 2c 20 74 68 65 20 6f 6e 6c 79 20 73 79 6d 62  e, the only symb
22c27 6f 6c 73 20 64 65 73 74 72 6f 79 65 64 20 61 72  ols destroyed ar
22c28 65 20 74 68 6f 73 65 0a 20 20 20 20 2a 2a 20 77  e those.    ** w
22c29 68 69 63 68 20 61 70 70 65 61 72 20 6f 6e 20 74  hich appear on t
22c2a 68 65 20 52 48 53 20 6f 66 20 74 68 65 20 72 75  he RHS of the ru
22c2b 6c 65 2c 20 62 75 74 20 77 68 69 63 68 20 61 72  le, but which ar
22c2c 65 20 6e 6f 74 20 75 73 65 64 0a 20 20 20 20 2a  e not used.    *
22c2d 2a 20 69 6e 73 69 64 65 20 74 68 65 20 43 20 63  * inside the C c
22c2e 6f 64 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ode..    */.    
22c2f 63 61 73 65 20 31 35 35 3a 0a 20 20 20 20 63 61  case 155:.    ca
22c30 73 65 20 31 38 39 3a 0a 20 20 20 20 63 61 73 65  se 189:.    case
22c31 20 32 30 36 3a 0a 7b 73 71 6c 69 74 65 33 53 65   206:.{sqlite3Se
22c32 6c 65 63 74 44 65 6c 65 74 65 28 28 79 79 70 6d  lectDelete((yypm
22c33 69 6e 6f 72 2d 3e 79 79 32 31 39 29 29 3b 7d 0a  inor->yy219));}.
22c34 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
22c35 20 63 61 73 65 20 31 36 39 3a 0a 20 20 20 20 63   case 169:.    c
22c36 61 73 65 20 31 37 30 3a 0a 20 20 20 20 63 61 73  ase 170:.    cas
22c37 65 20 31 39 34 3a 0a 20 20 20 20 63 61 73 65 20  e 194:.    case 
22c38 31 39 36 3a 0a 20 20 20 20 63 61 73 65 20 32 30  196:.    case 20
22c39 34 3a 0a 20 20 20 20 63 61 73 65 20 32 31 30 3a  4:.    case 210:
22c3a 0a 20 20 20 20 63 61 73 65 20 32 31 38 3a 0a 20  .    case 218:. 
22c3b 20 20 20 63 61 73 65 20 32 32 31 3a 0a 20 20 20     case 221:.   
22c3c 20 63 61 73 65 20 32 32 33 3a 0a 20 20 20 20 63   case 223:.    c
22c3d 61 73 65 20 32 33 35 3a 0a 7b 73 71 6c 69 74 65  ase 235:.{sqlite
22c3e 33 45 78 70 72 44 65 6c 65 74 65 28 28 79 79 70  3ExprDelete((yyp
22c3f 6d 69 6e 6f 72 2d 3e 79 79 31 37 32 29 29 3b 7d  minor->yy172));}
22c40 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
22c41 20 20 63 61 73 65 20 31 37 34 3a 0a 20 20 20 20    case 174:.    
22c42 63 61 73 65 20 31 38 32 3a 0a 20 20 20 20 63 61  case 182:.    ca
22c43 73 65 20 31 39 32 3a 0a 20 20 20 20 63 61 73 65  se 192:.    case
22c44 20 31 39 35 3a 0a 20 20 20 20 63 61 73 65 20 31   195:.    case 1
22c45 39 37 3a 0a 20 20 20 20 63 61 73 65 20 31 39 39  97:.    case 199
22c46 3a 0a 20 20 20 20 63 61 73 65 20 32 30 39 3a 0a  :.    case 209:.
22c47 20 20 20 20 63 61 73 65 20 32 31 31 3a 0a 20 20      case 211:.  
22c48 20 20 63 61 73 65 20 32 31 32 3a 0a 20 20 20 20    case 212:.    
22c49 63 61 73 65 20 32 31 35 3a 0a 20 20 20 20 63 61  case 215:.    ca
22c4a 73 65 20 32 31 36 3a 0a 20 20 20 20 63 61 73 65  se 216:.    case
22c4b 20 32 32 32 3a 0a 7b 73 71 6c 69 74 65 33 45 78   222:.{sqlite3Ex
22c4c 70 72 4c 69 73 74 44 65 6c 65 74 65 28 28 79 79  prListDelete((yy
22c4d 70 6d 69 6e 6f 72 2d 3e 79 79 31 37 34 29 29 3b  pminor->yy174));
22c4e 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
22c4f 20 20 20 63 61 73 65 20 31 38 38 3a 0a 20 20 20     case 188:.   
22c50 20 63 61 73 65 20 31 39 33 3a 0a 20 20 20 20 63   case 193:.    c
22c51 61 73 65 20 32 30 31 3a 0a 20 20 20 20 63 61 73  ase 201:.    cas
22c52 65 20 32 30 32 3a 0a 7b 73 71 6c 69 74 65 33 53  e 202:.{sqlite3S
22c53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 28 79 79  rcListDelete((yy
22c54 70 6d 69 6e 6f 72 2d 3e 79 79 33 37 33 29 29 3b  pminor->yy373));
22c55 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
22c56 20 20 20 63 61 73 65 20 32 30 35 3a 0a 20 20 20     case 205:.   
22c57 20 63 61 73 65 20 32 30 38 3a 0a 20 20 20 20 63   case 208:.    c
22c58 61 73 65 20 32 31 34 3a 0a 7b 73 71 6c 69 74 65  ase 214:.{sqlite
22c59 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 28 79  3IdListDelete((y
22c5a 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 33 32 29 29  ypminor->yy432))
22c5b 3b 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;}.      break;.
22c5c 20 20 20 20 63 61 73 65 20 32 33 31 3a 0a 20 20      case 231:.  
22c5d 20 20 63 61 73 65 20 32 33 36 3a 0a 7b 73 71 6c    case 236:.{sql
22c5e 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
22c5f 72 53 74 65 70 28 28 79 79 70 6d 69 6e 6f 72 2d  rStep((yypminor-
22c60 3e 79 79 32 34 33 29 29 3b 7d 0a 20 20 20 20 20  >yy243));}.     
22c61 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
22c62 20 32 33 33 3a 0a 7b 73 71 6c 69 74 65 33 49 64   233:.{sqlite3Id
22c63 4c 69 73 74 44 65 6c 65 74 65 28 28 79 79 70 6d  ListDelete((yypm
22c64 69 6e 6f 72 2d 3e 79 79 33 37 30 29 2e 62 29 3b  inor->yy370).b);
22c65 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
22c66 20 20 20 63 61 73 65 20 32 33 38 3a 0a 7b 73 71     case 238:.{sq
22c67 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
22c68 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 38 36  (yypminor->yy386
22c69 29 29 3b 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  ));}.      break
22c6a 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ;.    default:  
22c6b 62 72 65 61 6b 3b 20 20 20 2f 2a 20 49 66 20 6e  break;   /* If n
22c6c 6f 20 64 65 73 74 72 75 63 74 6f 72 20 61 63 74  o destructor act
22c6d 69 6f 6e 20 73 70 65 63 69 66 69 65 64 3a 20 64  ion specified: d
22c6e 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d  o nothing */.  }
22c6f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74 68  .}../*.** Pop th
22c70 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
22c71 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   once..**.** If 
22c72 74 68 65 72 65 20 69 73 20 61 20 64 65 73 74 72  there is a destr
22c73 75 63 74 6f 72 20 72 6f 75 74 69 6e 65 20 61 73  uctor routine as
22c74 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
22c75 65 20 74 6f 6b 65 6e 20 77 68 69 63 68 0a 2a 2a  e token which.**
22c76 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20   is popped from 
22c77 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20  the stack, then 
22c78 63 61 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 52  call it..**.** R
22c79 65 74 75 72 6e 20 74 68 65 20 6d 61 6a 6f 72 20  eturn the major 
22c7a 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 66 6f 72  token number for
22c7b 20 74 68 65 20 73 79 6d 62 6f 6c 20 70 6f 70 70   the symbol popp
22c7c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
22c7d 74 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f  t yy_pop_parser_
22c7e 73 74 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a  stack(yyParser *
22c7f 70 50 61 72 73 65 72 29 7b 0a 20 20 59 59 43 4f  pParser){.  YYCO
22c80 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72 3b 0a  DETYPE yymajor;.
22c81 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a    yyStackEntry *
22c82 79 79 74 6f 73 20 3d 20 26 70 50 61 72 73 65 72  yytos = &pParser
22c83 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65  ->yystack[pParse
22c84 72 2d 3e 79 79 69 64 78 5d 3b 0a 0a 20 20 69 66  r->yyidx];..  if
22c85 28 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  ( pParser->yyidx
22c86 3c 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  <0 ) return 0;.#
22c87 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
22c88 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
22c89 26 26 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64  && pParser->yyid
22c8a 78 3e 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69  x>=0 ){.    fpri
22c8b 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
22c8c 22 25 73 50 6f 70 70 69 6e 67 20 25 73 5c 6e 22  "%sPopping %s\n"
22c8d 2c 0a 20 20 20 20 20 20 79 79 54 72 61 63 65 50  ,.      yyTraceP
22c8e 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79 79 54  rompt,.      yyT
22c8f 6f 6b 65 6e 4e 61 6d 65 5b 79 79 74 6f 73 2d 3e  okenName[yytos->
22c90 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e  major]);.  }.#en
22c91 64 69 66 0a 20 20 79 79 6d 61 6a 6f 72 20 3d 20  dif.  yymajor = 
22c92 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 3b 0a 20 20  yytos->major;.  
22c93 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 20 79  yy_destructor( y
22c94 79 6d 61 6a 6f 72 2c 20 26 79 79 74 6f 73 2d 3e  ymajor, &yytos->
22c95 6d 69 6e 6f 72 29 3b 0a 20 20 70 50 61 72 73 65  minor);.  pParse
22c96 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20 20 72 65  r->yyidx--;.  re
22c97 74 75 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d 0a  turn yymajor;.}.
22c98 0a 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61  ./* .** Dealloca
22c99 74 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 61  te and destroy a
22c9a 20 70 61 72 73 65 72 2e 20 20 44 65 73 74 72 75   parser.  Destru
22c9b 63 74 6f 72 73 20 61 72 65 20 61 6c 6c 20 63 61  ctors are all ca
22c9c 6c 6c 65 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c 20  lled for.** all 
22c9d 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74 73 20 62  stack elements b
22c9e 65 66 6f 72 65 20 73 68 75 74 74 69 6e 67 20 74  efore shutting t
22c9f 68 65 20 70 61 72 73 65 72 20 64 6f 77 6e 2e 0a  he parser down..
22ca0 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a  **.** Inputs:.**
22ca1 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 41   <ul>.** <li>  A
22ca2 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
22ca3 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 73 68  parser.  This sh
22ca4 6f 75 6c 64 20 62 65 20 61 20 70 6f 69 6e 74 65  ould be a pointe
22ca5 72 0a 2a 2a 20 20 20 20 20 20 20 6f 62 74 61 69  r.**       obtai
22ca6 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
22ca7 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a 20  ParserAlloc..** 
22ca8 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20  <li>  A pointer 
22ca9 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 75 73  to a function us
22caa 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65  ed to reclaim me
22cab 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  mory obtained.**
22cac 20 20 20 20 20 20 20 66 72 6f 6d 20 6d 61 6c 6c         from mall
22cad 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a  oc..** </ul>.*/.
22cae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
22caf 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
22cb0 72 46 72 65 65 28 0a 20 20 76 6f 69 64 20 2a 70  rFree(.  void *p
22cb1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
22cb2 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
22cb3 65 72 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  er to be deleted
22cb4 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 66 72 65   */.  void (*fre
22cb5 65 50 72 6f 63 29 28 76 6f 69 64 2a 29 20 20 20  eProc)(void*)   
22cb6 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 75 73    /* Function us
22cb7 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65  ed to reclaim me
22cb8 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20 79 79 50  mory */.){.  yyP
22cb9 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d  arser *pParser =
22cba 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20   (yyParser*)p;. 
22cbb 20 69 66 28 20 70 50 61 72 73 65 72 3d 3d 30 20   if( pParser==0 
22cbc 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c  ) return;.  whil
22cbd 65 28 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64  e( pParser->yyid
22cbe 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61  x>=0 ) yy_pop_pa
22cbf 72 73 65 72 5f 73 74 61 63 6b 28 70 50 61 72 73  rser_stack(pPars
22cc0 65 72 29 3b 0a 23 69 66 20 59 59 53 54 41 43 4b  er);.#if YYSTACK
22cc1 44 45 50 54 48 3c 3d 30 0a 20 20 66 72 65 65 28  DEPTH<=0.  free(
22cc2 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
22cc3 29 3b 0a 23 65 6e 64 69 66 0a 20 20 28 2a 66 72  );.#endif.  (*fr
22cc4 65 65 50 72 6f 63 29 28 28 76 6f 69 64 2a 29 70  eeProc)((void*)p
22cc5 50 61 72 73 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Parser);.}../*.*
22cc6 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f  * Find the appro
22cc7 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f  priate action fo
22cc8 72 20 61 20 70 61 72 73 65 72 20 67 69 76 65 6e  r a parser given
22cc9 20 74 68 65 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a   the terminal.**
22cca 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
22ccb 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a  n iLookAhead..**
22ccc 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d  .** If the look-
22ccd 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 73 20 59  ahead token is Y
22cce 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20 63 68  YNOCODE, then ch
22ccf 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
22cd0 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69  e action is.** i
22cd1 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66 20 74 68  ndependent of th
22cd2 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49  e look-ahead.  I
22cd3 66 20 69 74 20 69 73 2c 20 72 65 74 75 72 6e 20  f it is, return 
22cd4 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65  the action, othe
22cd5 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20  rwise.** return 
22cd6 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f  YY_NO_ACTION..*/
22cd7 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f 66  .static int yy_f
22cd8 69 6e 64 5f 73 68 69 66 74 5f 61 63 74 69 6f 6e  ind_shift_action
22cd9 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70 50  (.  yyParser *pP
22cda 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 2f 2a  arser,        /*
22cdb 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
22cdc 20 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f   YYCODETYPE iLoo
22cdd 6b 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54 68  kAhead     /* Th
22cde 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b  e look-ahead tok
22cdf 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  en */.){.  int i
22ce0 3b 0a 20 20 69 6e 74 20 73 74 61 74 65 6e 6f 20  ;.  int stateno 
22ce1 3d 20 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  = pParser->yysta
22ce2 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79 69 64  ck[pParser->yyid
22ce3 78 5d 2e 73 74 61 74 65 6e 6f 3b 0a 20 0a 20 20  x].stateno;. .  
22ce4 69 66 28 20 73 74 61 74 65 6e 6f 3e 59 59 5f 53  if( stateno>YY_S
22ce5 48 49 46 54 5f 4d 41 58 20 7c 7c 20 28 69 20 3d  HIFT_MAX || (i =
22ce6 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 73   yy_shift_ofst[s
22ce7 74 61 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49  tateno])==YY_SHI
22ce8 46 54 5f 55 53 45 5f 44 46 4c 54 20 29 7b 0a 20  FT_USE_DFLT ){. 
22ce9 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66     return yy_def
22cea 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20  ault[stateno];. 
22ceb 20 7d 0a 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68   }.  if( iLookAh
22cec 65 61 64 3d 3d 59 59 4e 4f 43 4f 44 45 20 29 7b  ead==YYNOCODE ){
22ced 0a 20 20 20 20 72 65 74 75 72 6e 20 59 59 5f 4e  .    return YY_N
22cee 4f 5f 41 43 54 49 4f 4e 3b 0a 20 20 7d 0a 20 20  O_ACTION;.  }.  
22cef 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b  i += iLookAhead;
22cf0 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e  .  if( i<0 || i>
22cf1 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c  =YY_SZ_ACTTAB ||
22cf2 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d   yy_lookahead[i]
22cf3 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a  !=iLookAhead ){.
22cf4 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65      if( iLookAhe
22cf5 61 64 3e 30 20 29 7b 0a 23 69 66 64 65 66 20 59  ad>0 ){.#ifdef Y
22cf6 59 46 41 4c 4c 42 41 43 4b 0a 20 20 20 20 20 20  YFALLBACK.      
22cf7 69 6e 74 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20  int iFallback;  
22cf8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c            /* Fal
22cf9 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20  lback token */. 
22cfa 20 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68       if( iLookAh
22cfb 65 61 64 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c  ead<sizeof(yyFal
22cfc 6c 62 61 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79  lback)/sizeof(yy
22cfd 46 61 6c 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20  Fallback[0]).   
22cfe 20 20 20 20 20 20 20 20 20 20 26 26 20 28 69 46            && (iF
22cff 61 6c 6c 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c  allback = yyFall
22d00 62 61 63 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d  back[iLookAhead]
22d01 29 21 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20  )!=0 ){.#ifndef 
22d02 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 69  NDEBUG.        i
22d03 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
22d04 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69  {.          fpri
22d05 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
22d06 20 22 25 73 46 41 4c 4c 42 41 43 4b 20 25 73 20   "%sFALLBACK %s 
22d07 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  => %s\n",.      
22d08 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72         yyTracePr
22d09 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d  ompt, yyTokenNam
22d0a 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79  e[iLookAhead], y
22d0b 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c  yTokenName[iFall
22d0c 62 61 63 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20  back]);.        
22d0d 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
22d0e 20 72 65 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f   return yy_find_
22d0f 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 70 50 61  shift_action(pPa
22d10 72 73 65 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29  rser, iFallback)
22d11 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
22d12 0a 23 69 66 64 65 66 20 59 59 57 49 4c 44 43 41  .#ifdef YYWILDCA
22d13 52 44 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  RD.      {.     
22d14 20 20 20 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69     int j = i - i
22d15 4c 6f 6f 6b 41 68 65 61 64 20 2b 20 59 59 57 49  LookAhead + YYWI
22d16 4c 44 43 41 52 44 3b 0a 20 20 20 20 20 20 20 20  LDCARD;.        
22d17 69 66 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 59 59  if( j>=0 && j<YY
22d18 5f 53 5a 5f 41 43 54 54 41 42 20 26 26 20 79 79  _SZ_ACTTAB && yy
22d19 5f 6c 6f 6f 6b 61 68 65 61 64 5b 6a 5d 3d 3d 59  _lookahead[j]==Y
22d1a 59 57 49 4c 44 43 41 52 44 20 29 7b 0a 23 69 66  YWILDCARD ){.#if
22d1b 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
22d1c 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63        if( yyTrac
22d1d 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20  eFILE ){.       
22d1e 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
22d1f 72 61 63 65 46 49 4c 45 2c 20 22 25 73 57 49 4c  raceFILE, "%sWIL
22d20 44 43 41 52 44 20 25 73 20 3d 3e 20 25 73 5c 6e  DCARD %s => %s\n
22d21 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
22d22 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
22d23 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f   yyTokenName[iLo
22d24 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65  okAhead], yyToke
22d25 6e 4e 61 6d 65 5b 59 59 57 49 4c 44 43 41 52 44  nName[YYWILDCARD
22d26 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ]);.          }.
22d27 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
22d28 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65   */.          re
22d29 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 6a  turn yy_action[j
22d2a 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ];.        }.   
22d2b 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59     }.#endif /* Y
22d2c 59 57 49 4c 44 43 41 52 44 20 2a 2f 0a 20 20 20  YWILDCARD */.   
22d2d 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79   }.    return yy
22d2e 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f  _default[stateno
22d2f 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
22d30 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e  return yy_action
22d31 5b 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  [i];.  }.}../*.*
22d32 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f  * Find the appro
22d33 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f  priate action fo
22d34 72 20 61 20 70 61 72 73 65 72 20 67 69 76 65 6e  r a parser given
22d35 20 74 68 65 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61   the non-termina
22d36 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20  l.** look-ahead 
22d37 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64  token iLookAhead
22d38 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
22d39 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
22d3a 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65  is YYNOCODE, the
22d3b 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  n check to see i
22d3c 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a  f the action is.
22d3d 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f  ** independent o
22d3e 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  f the look-ahead
22d3f 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72 65 74  .  If it is, ret
22d40 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20  urn the action, 
22d41 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74  otherwise.** ret
22d42 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e  urn YY_NO_ACTION
22d43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
22d44 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61  yy_find_reduce_a
22d45 63 74 69 6f 6e 28 0a 20 20 69 6e 74 20 73 74 61  ction(.  int sta
22d46 74 65 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  teno,           
22d47 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74     /* Current st
22d48 61 74 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ate number */.  
22d49 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b  YYCODETYPE iLook
22d4a 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65  Ahead     /* The
22d4b 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
22d4c 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  n */.){.  int i;
22d4d 0a 20 20 2f 2a 20 69 6e 74 20 73 74 61 74 65 6e  .  /* int staten
22d4e 6f 20 3d 20 70 50 61 72 73 65 72 2d 3e 79 79 73  o = pParser->yys
22d4f 74 61 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79  tack[pParser->yy
22d50 69 64 78 5d 2e 73 74 61 74 65 6e 6f 3b 20 2a 2f  idx].stateno; */
22d51 0a 20 0a 20 20 69 66 28 20 73 74 61 74 65 6e 6f  . .  if( stateno
22d52 3e 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 7c  >YY_REDUCE_MAX |
22d53 7c 0a 20 20 20 20 20 20 28 69 20 3d 20 79 79 5f  |.      (i = yy_
22d54 72 65 64 75 63 65 5f 6f 66 73 74 5b 73 74 61 74  reduce_ofst[stat
22d55 65 6e 6f 5d 29 3d 3d 59 59 5f 52 45 44 55 43 45  eno])==YY_REDUCE
22d56 5f 55 53 45 5f 44 46 4c 54 20 29 7b 0a 20 20 20  _USE_DFLT ){.   
22d57 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75   return yy_defau
22d58 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d  lt[stateno];.  }
22d59 0a 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61  .  if( iLookAhea
22d5a 64 3d 3d 59 59 4e 4f 43 4f 44 45 20 29 7b 0a 20  d==YYNOCODE ){. 
22d5b 20 20 20 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f     return YY_NO_
22d5c 41 43 54 49 4f 4e 3b 0a 20 20 7d 0a 20 20 69 20  ACTION;.  }.  i 
22d5d 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 20  += iLookAhead;. 
22d5e 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59   if( i<0 || i>=Y
22d5f 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79  Y_SZ_ACTTAB || y
22d60 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d  y_lookahead[i]!=
22d61 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20  iLookAhead ){.  
22d62 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
22d63 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
22d64 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
22d65 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b 0a  n yy_action[i];.
22d66 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
22d67 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
22d68 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20  ne is called if 
22d69 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c  the stack overfl
22d6a 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ows..*/.static v
22d6b 6f 69 64 20 79 79 53 74 61 63 6b 4f 76 65 72 66  oid yyStackOverf
22d6c 6c 6f 77 28 79 79 50 61 72 73 65 72 20 2a 79 79  low(yyParser *yy
22d6d 70 50 61 72 73 65 72 2c 20 59 59 4d 49 4e 4f 52  pParser, YYMINOR
22d6e 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72 29 7b  TYPE *yypMinor){
22d6f 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  .   sqlite3Parse
22d70 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 20 79  rARG_FETCH;.   y
22d71 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d  ypParser->yyidx-
22d72 2d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  -;.#ifndef NDEBU
22d73 47 0a 20 20 20 69 66 28 20 79 79 54 72 61 63 65  G.   if( yyTrace
22d74 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 66 70 72  FILE ){.     fpr
22d75 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
22d76 2c 22 25 73 53 74 61 63 6b 20 4f 76 65 72 66 6c  ,"%sStack Overfl
22d77 6f 77 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72  ow!\n",yyTracePr
22d78 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23 65 6e 64  ompt);.   }.#end
22d79 69 66 0a 20 20 20 77 68 69 6c 65 28 20 79 79 70  if.   while( yyp
22d7a 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30  Parser->yyidx>=0
22d7b 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72   ) yy_pop_parser
22d7c 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  _stack(yypParser
22d7d 29 3b 0a 20 20 20 2f 2a 20 48 65 72 65 20 63 6f  );.   /* Here co
22d7e 64 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77  de is inserted w
22d7f 68 69 63 68 20 77 69 6c 6c 20 65 78 65 63 75 74  hich will execut
22d80 65 20 69 66 20 74 68 65 20 70 61 72 73 65 72 0a  e if the parser.
22d81 20 20 20 2a 2a 20 73 74 61 63 6b 20 65 76 65 72     ** stack ever
22d82 79 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a 0a  y overflows */..
22d83 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
22d84 67 28 70 50 61 72 73 65 2c 20 22 70 61 72 73 65  g(pParse, "parse
22d85 72 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  r stack overflow
22d86 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61  ");.  pParse->pa
22d87 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20  rseError = 1;.  
22d88 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
22d89 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70  G_STORE; /* Supp
22d8a 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f  ress warning abo
22d8b 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61  ut unused %extra
22d8c 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 20 2a 2f  _argument var */
22d8d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  .}../*.** Perfor
22d8e 6d 20 61 20 73 68 69 66 74 20 61 63 74 69 6f 6e  m a shift action
22d8f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
22d90 20 79 79 5f 73 68 69 66 74 28 0a 20 20 79 79 50   yy_shift(.  yyP
22d91 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
22d92 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
22d93 65 20 70 61 72 73 65 72 20 74 6f 20 62 65 20 73  e parser to be s
22d94 68 69 66 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  hifted */.  int 
22d95 79 79 4e 65 77 53 74 61 74 65 2c 20 20 20 20 20  yyNewState,     
22d96 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22d97 20 6e 65 77 20 73 74 61 74 65 20 74 6f 20 73 68   new state to sh
22d98 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  ift in */.  int 
22d99 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20  yyMajor,        
22d9a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22d9b 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 74 6f 20   major token to 
22d9c 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 59 59  shift in */.  YY
22d9d 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69  MINORTYPE *yypMi
22d9e 6e 6f 72 20 20 20 20 20 20 20 20 20 2f 2a 20 50  nor         /* P
22d9f 6f 69 6e 74 65 72 20 6f 74 20 74 68 65 20 6d 69  ointer ot the mi
22da0 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68 69  nor token to shi
22da1 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20 79 79  ft in */.){.  yy
22da2 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f  StackEntry *yyto
22da3 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e  s;.  yypParser->
22da4 79 79 69 64 78 2b 2b 3b 0a 23 69 66 20 59 59 53  yyidx++;.#if YYS
22da5 54 41 43 4b 44 45 50 54 48 3e 30 20 0a 20 20 69  TACKDEPTH>0 .  i
22da6 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
22da7 69 64 78 3e 3d 59 59 53 54 41 43 4b 44 45 50 54  idx>=YYSTACKDEPT
22da8 48 20 29 7b 0a 20 20 20 20 79 79 53 74 61 63 6b  H ){.    yyStack
22da9 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73  Overflow(yypPars
22daa 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20  er, yypMinor);. 
22dab 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
22dac 65 6c 73 65 0a 20 20 69 66 28 20 79 79 70 50 61  else.  if( yypPa
22dad 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70  rser->yyidx>=yyp
22dae 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
22daf 29 7b 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61  ){.    yyGrowSta
22db0 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
22db1 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
22db2 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50 61 72 73  ->yyidx>=yypPars
22db3 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0a 20  er->yystksz ){. 
22db4 20 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72       yyStackOver
22db5 66 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20  flow(yypParser, 
22db6 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20 20 20 20  yypMinor);.     
22db7 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
22db8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 74 6f   }.#endif.  yyto
22db9 73 20 3d 20 26 79 79 70 50 61 72 73 65 72 2d 3e  s = &yypParser->
22dba 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65  yystack[yypParse
22dbb 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20 79 79 74  r->yyidx];.  yyt
22dbc 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 79 79  os->stateno = yy
22dbd 4e 65 77 53 74 61 74 65 3b 0a 20 20 79 79 74 6f  NewState;.  yyto
22dbe 73 2d 3e 6d 61 6a 6f 72 20 3d 20 79 79 4d 61 6a  s->major = yyMaj
22dbf 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 69 6e  or;.  yytos->min
22dc0 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b 0a  or = *yypMinor;.
22dc1 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
22dc2 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
22dc3 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
22dc4 79 69 64 78 3e 30 20 29 7b 0a 20 20 20 20 69 6e  yidx>0 ){.    in
22dc5 74 20 69 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  t i;.    fprintf
22dc6 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
22dc7 53 68 69 66 74 20 25 64 5c 6e 22 2c 79 79 54 72  Shift %d\n",yyTr
22dc8 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e 65 77 53  acePrompt,yyNewS
22dc9 74 61 74 65 29 3b 0a 20 20 20 20 66 70 72 69 6e  tate);.    fprin
22dca 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
22dcb 25 73 53 74 61 63 6b 3a 22 2c 79 79 54 72 61 63  %sStack:",yyTrac
22dcc 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 66 6f  ePrompt);.    fo
22dcd 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70 50 61 72  r(i=1; i<=yypPar
22dce 73 65 72 2d 3e 79 79 69 64 78 3b 20 69 2b 2b 29  ser->yyidx; i++)
22dcf 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79  .      fprintf(y
22dd0 79 54 72 61 63 65 46 49 4c 45 2c 22 20 25 73 22  yTraceFILE," %s"
22dd1 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 70  ,yyTokenName[yyp
22dd2 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
22dd3 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20 20  i].major]);.    
22dd4 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
22dd5 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20 7d 0a 23  ILE,"\n");.  }.#
22dd6 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54 68 65 20  endif.}../* The 
22dd7 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
22dd8 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
22dd9 74 69 6f 6e 20 61 62 6f 75 74 20 65 76 65 72 79  tion about every
22dda 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73   rule that.** is
22ddb 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 68 65   used during the
22ddc 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74   reduce..*/.stat
22ddd 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
22dde 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 6c  {.  YYCODETYPE l
22ddf 68 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  hs;         /* S
22de0 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20 6c 65 66  ymbol on the lef
22de1 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t-hand side of t
22de2 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20 75 6e 73  he rule */.  uns
22de3 69 67 6e 65 64 20 63 68 61 72 20 6e 72 68 73 3b  igned char nrhs;
22de4 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
22de5 66 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  f right-hand sid
22de6 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20 74 68 65  e symbols in the
22de7 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79 52 75 6c   rule */.} yyRul
22de8 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20  eInfo[] = {.  { 
22de9 31 33 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  139, 1 },.  { 14
22dea 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 30 2c  0, 2 },.  { 140,
22deb 20 31 20 7d 2c 0a 20 20 7b 20 31 34 32 2c 20 31   1 },.  { 142, 1
22dec 20 7d 2c 0a 20 20 7b 20 31 34 31 2c 20 31 20 7d   },.  { 141, 1 }
22ded 2c 0a 20 20 7b 20 31 34 31 2c 20 33 20 7d 2c 0a  ,.  { 141, 3 },.
22dee 20 20 7b 20 31 34 34 2c 20 30 20 7d 2c 0a 20 20    { 144, 0 },.  
22def 7b 20 31 34 34 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 144, 1 },.  { 
22df0 31 34 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  144, 3 },.  { 14
22df1 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 36 2c  3, 3 },.  { 146,
22df2 20 30 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20 31   0 },.  { 146, 1
22df3 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20 32 20 7d   },.  { 146, 2 }
22df4 2c 0a 20 20 7b 20 31 34 35 2c 20 30 20 7d 2c 0a  ,.  { 145, 0 },.
22df5 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20    { 145, 1 },.  
22df6 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 145, 1 },.  { 
22df7 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  145, 1 },.  { 14
22df8 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  3, 2 },.  { 143,
22df9 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32   2 },.  { 143, 2
22dfa 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32 20 7d   },.  { 143, 2 }
22dfb 2c 0a 20 20 7b 20 31 34 38 2c 20 36 20 7d 2c 0a  ,.  { 148, 6 },.
22dfc 20 20 7b 20 31 35 31 2c 20 30 20 7d 2c 0a 20 20    { 151, 0 },.  
22dfd 7b 20 31 35 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 151, 3 },.  { 
22dfe 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35  150, 1 },.  { 15
22dff 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 39 2c  0, 0 },.  { 149,
22e00 20 34 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 32   4 },.  { 149, 2
22e01 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 33 20 7d   },.  { 153, 3 }
22e02 2c 0a 20 20 7b 20 31 35 33 2c 20 31 20 7d 2c 0a  ,.  { 153, 1 },.
22e03 20 20 7b 20 31 35 36 2c 20 33 20 7d 2c 0a 20 20    { 156, 3 },.  
22e04 7b 20 31 35 37 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 157, 1 },.  { 
22e05 31 36 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  160, 1 },.  { 16
22e06 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  1, 1 },.  { 147,
22e07 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31   1 },.  { 147, 1
22e08 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d   },.  { 147, 1 }
22e09 2c 0a 20 20 7b 20 31 35 38 2c 20 30 20 7d 2c 0a  ,.  { 158, 0 },.
22e0a 20 20 7b 20 31 35 38 2c 20 31 20 7d 2c 0a 20 20    { 158, 1 },.  
22e0b 7b 20 31 36 32 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 162, 1 },.  { 
22e0c 31 36 32 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36  162, 4 },.  { 16
22e0d 32 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 36 33 2c  2, 6 },.  { 163,
22e0e 20 31 20 7d 2c 0a 20 20 7b 20 31 36 33 2c 20 32   1 },.  { 163, 2
22e0f 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 31 20 7d   },.  { 164, 1 }
22e10 2c 0a 20 20 7b 20 31 36 34 2c 20 31 20 7d 2c 0a  ,.  { 164, 1 },.
22e11 20 20 7b 20 31 35 39 2c 20 32 20 7d 2c 0a 20 20    { 159, 2 },.  
22e12 7b 20 31 35 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 159, 0 },.  { 
22e13 31 36 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 36  167, 3 },.  { 16
22e14 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 38 2c  7, 1 },.  { 168,
22e15 20 32 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 34   2 },.  { 168, 4
22e16 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 33 20 7d   },.  { 168, 3 }
22e17 2c 0a 20 20 7b 20 31 36 38 2c 20 33 20 7d 2c 0a  ,.  { 168, 3 },.
22e18 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20 20    { 168, 2 },.  
22e19 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 168, 2 },.  { 
22e1a 31 36 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 36  168, 3 },.  { 16
22e1b 38 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 36 38 2c  8, 5 },.  { 168,
22e1c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 34   2 },.  { 168, 4
22e1d 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 34 20 7d   },.  { 168, 4 }
22e1e 2c 0a 20 20 7b 20 31 36 38 2c 20 31 20 7d 2c 0a  ,.  { 168, 1 },.
22e1f 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20 20    { 168, 2 },.  
22e20 7b 20 31 37 33 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 173, 0 },.  { 
22e21 31 37 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  173, 1 },.  { 17
22e22 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 0 },.  { 175,
22e23 20 32 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 32   2 },.  { 177, 2
22e24 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 33 20 7d   },.  { 177, 3 }
22e25 2c 0a 20 20 7b 20 31 37 37 2c 20 33 20 7d 2c 0a  ,.  { 177, 3 },.
22e26 20 20 7b 20 31 37 37 2c 20 33 20 7d 2c 0a 20 20    { 177, 3 },.  
22e27 7b 20 31 37 38 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 178, 2 },.  { 
22e28 31 37 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  178, 2 },.  { 17
22e29 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 38 2c  8, 1 },.  { 178,
22e2a 20 31 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 33   1 },.  { 176, 3
22e2b 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 32 20 7d   },.  { 176, 2 }
22e2c 2c 0a 20 20 7b 20 31 37 39 2c 20 30 20 7d 2c 0a  ,.  { 179, 0 },.
22e2d 20 20 7b 20 31 37 39 2c 20 32 20 7d 2c 0a 20 20    { 179, 2 },.  
22e2e 7b 20 31 37 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 179, 2 },.  { 
22e2f 31 35 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  154, 0 },.  { 15
22e30 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 30 2c  4, 2 },.  { 180,
22e31 20 33 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 32   3 },.  { 180, 2
22e32 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 31 20 7d   },.  { 180, 1 }
22e33 2c 0a 20 20 7b 20 31 38 31 2c 20 32 20 7d 2c 0a  ,.  { 181, 2 },.
22e34 20 20 7b 20 31 38 31 2c 20 37 20 7d 2c 0a 20 20    { 181, 7 },.  
22e35 7b 20 31 38 31 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 181, 5 },.  { 
22e36 31 38 31 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38  181, 5 },.  { 18
22e37 31 2c 20 31 30 20 7d 2c 0a 20 20 7b 20 31 38 33  1, 10 },.  { 183
22e38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20  , 0 },.  { 183, 
22e39 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 30 20  1 },.  { 171, 0 
22e3a 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d 2c  },.  { 171, 3 },
22e3b 0a 20 20 7b 20 31 38 34 2c 20 30 20 7d 2c 0a 20  .  { 184, 0 },. 
22e3c 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 184, 2 },.  {
22e3d 20 31 38 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   185, 1 },.  { 1
22e3e 38 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 35  85, 1 },.  { 185
22e3f 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 1 },.  { 143, 
22e40 34 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 32 20  4 },.  { 187, 2 
22e41 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 30 20 7d 2c  },.  { 187, 0 },
22e42 0a 20 20 7b 20 31 34 33 2c 20 38 20 7d 2c 0a 20  .  { 143, 8 },. 
22e43 20 7b 20 31 34 33 2c 20 34 20 7d 2c 0a 20 20 7b   { 143, 4 },.  {
22e44 20 31 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   143, 1 },.  { 1
22e45 35 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 35  55, 1 },.  { 155
22e46 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20  , 3 },.  { 190, 
22e47 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 32 20  1 },.  { 190, 2 
22e48 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c  },.  { 190, 1 },
22e49 0a 20 20 7b 20 31 38 39 2c 20 39 20 7d 2c 0a 20  .  { 189, 9 },. 
22e4a 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 191, 1 },.  {
22e4b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   191, 1 },.  { 1
22e4c 39 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39  91, 0 },.  { 199
22e4d 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20  , 2 },.  { 199, 
22e4e 30 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 33 20  0 },.  { 192, 3 
22e4f 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 32 20 7d 2c  },.  { 192, 2 },
22e50 0a 20 20 7b 20 31 39 32 2c 20 34 20 7d 2c 0a 20  .  { 192, 4 },. 
22e51 20 7b 20 32 30 30 2c 20 32 20 7d 2c 0a 20 20 7b   { 200, 2 },.  {
22e52 20 32 30 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   200, 1 },.  { 2
22e53 30 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 33  00, 0 },.  { 193
22e54 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20  , 0 },.  { 193, 
22e55 32 20 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 32 20  2 },.  { 202, 2 
22e56 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 30 20 7d 2c  },.  { 202, 0 },
22e57 0a 20 20 7b 20 32 30 31 2c 20 36 20 7d 2c 0a 20  .  { 201, 6 },. 
22e58 20 7b 20 32 30 31 2c 20 37 20 7d 2c 0a 20 20 7b   { 201, 7 },.  {
22e59 20 32 30 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   206, 1 },.  { 2
22e5a 30 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 32  06, 1 },.  { 152
22e5b 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 32 2c 20  , 0 },.  { 152, 
22e5c 32 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 32 20  2 },.  { 188, 2 
22e5d 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 31 20 7d 2c  },.  { 203, 1 },
22e5e 0a 20 20 7b 20 32 30 33 2c 20 32 20 7d 2c 0a 20  .  { 203, 2 },. 
22e5f 20 7b 20 32 30 33 2c 20 33 20 7d 2c 0a 20 20 7b   { 203, 3 },.  {
22e60 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   203, 4 },.  { 2
22e61 30 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 34  04, 2 },.  { 204
22e62 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20  , 0 },.  { 205, 
22e63 34 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 30 20  4 },.  { 205, 0 
22e64 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 30 20 7d 2c  },.  { 197, 0 },
22e65 0a 20 20 7b 20 31 39 37 2c 20 33 20 7d 2c 0a 20  .  { 197, 3 },. 
22e66 20 7b 20 32 30 39 2c 20 34 20 7d 2c 0a 20 20 7b   { 209, 4 },.  {
22e67 20 32 30 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   209, 2 },.  { 2
22e68 31 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 32  10, 1 },.  { 172
22e69 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20  , 1 },.  { 172, 
22e6a 31 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 30 20  1 },.  { 172, 0 
22e6b 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 30 20 7d 2c  },.  { 195, 0 },
22e6c 0a 20 20 7b 20 31 39 35 2c 20 33 20 7d 2c 0a 20  .  { 195, 3 },. 
22e6d 20 7b 20 31 39 36 2c 20 30 20 7d 2c 0a 20 20 7b   { 196, 0 },.  {
22e6e 20 31 39 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   196, 2 },.  { 1
22e6f 39 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38  98, 0 },.  { 198
22e70 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20  , 2 },.  { 198, 
22e71 34 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 34 20  4 },.  { 198, 4 
22e72 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 34 20 7d 2c  },.  { 143, 4 },
22e73 0a 20 20 7b 20 31 39 34 2c 20 30 20 7d 2c 0a 20  .  { 194, 0 },. 
22e74 20 7b 20 31 39 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 194, 2 },.  {
22e75 20 31 34 33 2c 20 36 20 7d 2c 0a 20 20 7b 20 32   143, 6 },.  { 2
22e76 31 32 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 31 32  12, 5 },.  { 212
22e77 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 3 },.  { 143, 
22e78 38 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 35 20  8 },.  { 143, 5 
22e79 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c  },.  { 143, 6 },
22e7a 0a 20 20 7b 20 32 31 33 2c 20 32 20 7d 2c 0a 20  .  { 213, 2 },. 
22e7b 20 7b 20 32 31 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 213, 1 },.  {
22e7c 20 32 31 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   215, 3 },.  { 2
22e7d 31 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 34  15, 1 },.  { 214
22e7e 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20  , 0 },.  { 214, 
22e7f 33 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 33 20  3 },.  { 208, 3 
22e80 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 31 20 7d 2c  },.  { 208, 1 },
22e81 0a 20 20 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20  .  { 170, 1 },. 
22e82 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b   { 170, 3 },.  {
22e83 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   169, 1 },.  { 1
22e84 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 1 },.  { 170
22e85 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 1 },.  { 170, 
22e86 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 35 20  3 },.  { 170, 5 
22e87 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c  },.  { 169, 1 },
22e88 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20  .  { 169, 1 },. 
22e89 20 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 170, 1 },.  {
22e8a 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   170, 1 },.  { 1
22e8b 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 3 },.  { 170
22e8c 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 6 },.  { 170, 
22e8d 35 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20  5 },.  { 170, 4 
22e8e 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c  },.  { 169, 1 },
22e8f 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20  .  { 170, 3 },. 
22e90 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b   { 170, 3 },.  {
22e91 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   170, 3 },.  { 1
22e92 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 3 },.  { 170
22e93 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 3 },.  { 170, 
22e94 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20  3 },.  { 170, 3 
22e95 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c  },.  { 170, 3 },
22e96 0a 20 20 7b 20 32 31 37 2c 20 31 20 7d 2c 0a 20  .  { 217, 1 },. 
22e97 20 7b 20 32 31 37 2c 20 32 20 7d 2c 0a 20 20 7b   { 217, 2 },.  {
22e98 20 32 31 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   217, 1 },.  { 2
22e99 31 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 38  17, 2 },.  { 218
22e9a 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 38 2c 20  , 2 },.  { 218, 
22e9b 30 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20  0 },.  { 170, 4 
22e9c 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 32 20 7d 2c  },.  { 170, 2 },
22e9d 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20  .  { 170, 3 },. 
22e9e 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b   { 170, 3 },.  {
22e9f 20 31 37 30 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   170, 4 },.  { 1
22ea0 37 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 2 },.  { 170
22ea1 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 2 },.  { 170, 
22ea2 32 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 31 20  2 },.  { 219, 1 
22ea3 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 32 20 7d 2c  },.  { 219, 2 },
22ea4 0a 20 20 7b 20 31 37 30 2c 20 35 20 7d 2c 0a 20  .  { 170, 5 },. 
22ea5 20 7b 20 32 32 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 220, 1 },.  {
22ea6 20 32 32 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   220, 2 },.  { 1
22ea7 37 30 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 5 },.  { 170
22ea8 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 3 },.  { 170, 
22ea9 35 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20  5 },.  { 170, 4 
22eaa 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20 7d 2c  },.  { 170, 4 },
22eab 0a 20 20 7b 20 31 37 30 2c 20 35 20 7d 2c 0a 20  .  { 170, 5 },. 
22eac 20 7b 20 32 32 32 2c 20 35 20 7d 2c 0a 20 20 7b   { 222, 5 },.  {
22ead 20 32 32 32 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   222, 4 },.  { 2
22eae 32 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33  23, 2 },.  { 223
22eaf 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20  , 0 },.  { 221, 
22eb0 31 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 30 20  1 },.  { 221, 0 
22eb1 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 31 20 7d 2c  },.  { 216, 1 },
22eb2 0a 20 20 7b 20 32 31 36 2c 20 30 20 7d 2c 0a 20  .  { 216, 0 },. 
22eb3 20 7b 20 32 31 31 2c 20 33 20 7d 2c 0a 20 20 7b   { 211, 3 },.  {
22eb4 20 32 31 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   211, 1 },.  { 1
22eb5 34 33 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32 32  43, 11 },.  { 22
22eb6 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 34 2c  4, 1 },.  { 224,
22eb7 20 30 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 30   0 },.  { 174, 0
22eb8 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d   },.  { 174, 3 }
22eb9 2c 0a 20 20 7b 20 31 38 32 2c 20 35 20 7d 2c 0a  ,.  { 182, 5 },.
22eba 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20 20    { 182, 3 },.  
22ebb 7b 20 32 32 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 225, 1 },.  { 
22ebc 32 32 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32  226, 0 },.  { 22
22ebd 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  6, 2 },.  { 143,
22ebe 20 34 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31   4 },.  { 143, 1
22ebf 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32 20 7d   },.  { 143, 2 }
22ec0 2c 0a 20 20 7b 20 31 34 33 2c 20 35 20 7d 2c 0a  ,.  { 143, 5 },.
22ec1 20 20 7b 20 31 34 33 2c 20 35 20 7d 2c 0a 20 20    { 143, 5 },.  
22ec2 7b 20 31 34 33 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 143, 5 },.  { 
22ec3 31 34 33 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34  143, 6 },.  { 14
22ec4 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 37 2c  3, 3 },.  { 227,
22ec5 20 31 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 31   1 },.  { 227, 1
22ec6 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 32 20 7d   },.  { 165, 2 }
22ec7 2c 0a 20 20 7b 20 31 36 36 2c 20 32 20 7d 2c 0a  ,.  { 166, 2 },.
22ec8 20 20 7b 20 32 32 39 2c 20 31 20 7d 2c 0a 20 20    { 229, 1 },.  
22ec9 7b 20 32 32 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 228, 1 },.  { 
22eca 32 32 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34  228, 0 },.  { 14
22ecb 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 33 30 2c  3, 5 },.  { 230,
22ecc 20 31 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20   11 },.  { 232, 
22ecd 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 31 20  1 },.  { 232, 1 
22ece 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 32 20 7d 2c  },.  { 232, 2 },
22ecf 0a 20 20 7b 20 32 33 32 2c 20 30 20 7d 2c 0a 20  .  { 232, 0 },. 
22ed0 20 7b 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 233, 1 },.  {
22ed1 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   233, 1 },.  { 2
22ed2 33 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 34  33, 3 },.  { 234
22ed3 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20  , 0 },.  { 234, 
22ed4 33 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 30 20  3 },.  { 235, 0 
22ed5 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 32 20 7d 2c  },.  { 235, 2 },
22ed6 0a 20 20 7b 20 32 33 31 2c 20 33 20 7d 2c 0a 20  .  { 231, 3 },. 
22ed7 20 7b 20 32 33 31 2c 20 30 20 7d 2c 0a 20 20 7b   { 231, 0 },.  {
22ed8 20 32 33 36 2c 20 36 20 7d 2c 0a 20 20 7b 20 32   236, 6 },.  { 2
22ed9 33 36 2c 20 38 20 7d 2c 0a 20 20 7b 20 32 33 36  36, 8 },.  { 236
22eda 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20  , 5 },.  { 236, 
22edb 34 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 31 20  4 },.  { 236, 1 
22edc 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20 7d 2c  },.  { 170, 4 },
22edd 0a 20 20 7b 20 31 37 30 2c 20 36 20 7d 2c 0a 20  .  { 170, 6 },. 
22ede 20 7b 20 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 186, 1 },.  {
22edf 20 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   186, 1 },.  { 1
22ee0 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33  86, 1 },.  { 143
22ee1 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 4 },.  { 143, 
22ee2 36 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 33 20  6 },.  { 143, 3 
22ee3 7d 2c 0a 20 20 7b 20 32 33 38 2c 20 30 20 7d 2c  },.  { 238, 0 },
22ee4 0a 20 20 7b 20 32 33 38 2c 20 32 20 7d 2c 0a 20  .  { 238, 2 },. 
22ee5 20 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 237, 1 },.  {
22ee6 20 32 33 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   237, 0 },.  { 1
22ee7 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33  43, 1 },.  { 143
22ee8 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 3 },.  { 143, 
22ee9 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 33 20  1 },.  { 143, 3 
22eea 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c  },.  { 143, 6 },
22eeb 0a 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c 0a 20  .  { 143, 6 },. 
22eec 20 7b 20 32 33 39 2c 20 31 20 7d 2c 0a 20 20 7b   { 239, 1 },.  {
22eed 20 32 34 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   240, 0 },.  { 2
22eee 34 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33  40, 1 },.  { 143
22eef 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 1 },.  { 143, 
22ef0 34 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 37 20  4 },.  { 241, 7 
22ef1 7d 2c 0a 20 20 7b 20 32 34 32 2c 20 31 20 7d 2c  },.  { 242, 1 },
22ef2 0a 20 20 7b 20 32 34 32 2c 20 33 20 7d 2c 0a 20  .  { 242, 3 },. 
22ef3 20 7b 20 32 34 33 2c 20 30 20 7d 2c 0a 20 20 7b   { 243, 0 },.  {
22ef4 20 32 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   243, 2 },.  { 2
22ef5 34 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 34  44, 1 },.  { 244
22ef6 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 35 2c 20  , 3 },.  { 245, 
22ef7 31 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 30 20  1 },.  { 246, 0 
22ef8 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 32 20 7d 2c  },.  { 246, 2 },
22ef9 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  .};..static void
22efa 20 79 79 5f 61 63 63 65 70 74 28 79 79 50 61 72   yy_accept(yyPar
22efb 73 65 72 2a 29 3b 20 20 2f 2a 20 46 6f 72 77 61  ser*);  /* Forwa
22efc 72 64 20 44 65 63 6c 61 72 61 74 69 6f 6e 20 2a  rd Declaration *
22efd 2f 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d  /../*.** Perform
22efe 20 61 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e   a reduce action
22eff 20 61 6e 64 20 74 68 65 20 73 68 69 66 74 20 74   and the shift t
22f00 68 61 74 20 6d 75 73 74 20 69 6d 6d 65 64 69 61  hat must immedia
22f01 74 65 6c 79 0a 2a 2a 20 66 6f 6c 6c 6f 77 20 74  tely.** follow t
22f02 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74  he reduce..*/.st
22f03 61 74 69 63 20 76 6f 69 64 20 79 79 5f 72 65 64  atic void yy_red
22f04 75 63 65 28 0a 20 20 79 79 50 61 72 73 65 72 20  uce(.  yyParser 
22f05 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 20  *yypParser,     
22f06 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
22f07 72 20 2a 2f 0a 20 20 69 6e 74 20 79 79 72 75 6c  r */.  int yyrul
22f08 65 6e 6f 20 20 20 20 20 20 20 20 20 20 20 20 20  eno             
22f09 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
22f0a 20 74 68 65 20 72 75 6c 65 20 62 79 20 77 68 69   the rule by whi
22f0b 63 68 20 74 6f 20 72 65 64 75 63 65 20 2a 2f 0a  ch to reduce */.
22f0c 29 7b 0a 20 20 69 6e 74 20 79 79 67 6f 74 6f 3b  ){.  int yygoto;
22f0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f0e 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74       /* The next
22f0f 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20   state */.  int 
22f10 79 79 61 63 74 3b 20 20 20 20 20 20 20 20 20 20  yyact;          
22f11 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
22f12 68 65 20 6e 65 78 74 20 61 63 74 69 6f 6e 20 2a  he next action *
22f13 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20  /.  YYMINORTYPE 
22f14 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 20 20 20 20  yygotominor;    
22f15 20 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f      /* The LHS o
22f16 66 20 74 68 65 20 72 75 6c 65 20 72 65 64 75 63  f the rule reduc
22f17 65 64 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45  ed */.  yyStackE
22f18 6e 74 72 79 20 2a 79 79 6d 73 70 3b 20 20 20 20  ntry *yymsp;    
22f19 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
22f1a 6f 70 20 6f 66 20 74 68 65 20 70 61 72 73 65 72  op of the parser
22f1b 27 73 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e  's stack */.  in
22f1c 74 20 79 79 73 69 7a 65 3b 20 20 20 20 20 20 20  t yysize;       
22f1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22f1e 20 41 6d 6f 75 6e 74 20 74 6f 20 70 6f 70 20 74   Amount to pop t
22f1f 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 73 71  he stack */.  sq
22f20 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46  lite3ParserARG_F
22f21 45 54 43 48 3b 0a 20 20 79 79 6d 73 70 20 3d 20  ETCH;.  yymsp = 
22f22 26 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74  &yypParser->yyst
22f23 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79  ack[yypParser->y
22f24 79 69 64 78 5d 3b 0a 23 69 66 6e 64 65 66 20 4e  yidx];.#ifndef N
22f25 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72  DEBUG.  if( yyTr
22f26 61 63 65 46 49 4c 45 20 26 26 20 79 79 72 75 6c  aceFILE && yyrul
22f27 65 6e 6f 3e 3d 30 20 0a 20 20 20 20 20 20 20 20  eno>=0 .        
22f28 26 26 20 79 79 72 75 6c 65 6e 6f 3c 28 69 6e 74  && yyruleno<(int
22f29 29 28 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e  )(sizeof(yyRuleN
22f2a 61 6d 65 29 2f 73 69 7a 65 6f 66 28 79 79 52 75  ame)/sizeof(yyRu
22f2b 6c 65 4e 61 6d 65 5b 30 5d 29 29 20 29 7b 0a 20  leName[0])) ){. 
22f2c 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
22f2d 63 65 46 49 4c 45 2c 20 22 25 73 52 65 64 75 63  ceFILE, "%sReduc
22f2e 65 20 5b 25 73 5d 2e 5c 6e 22 2c 20 79 79 54 72  e [%s].\n", yyTr
22f2f 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20 20  acePrompt,.     
22f30 20 79 79 52 75 6c 65 4e 61 6d 65 5b 79 79 72 75   yyRuleName[yyru
22f31 6c 65 6e 6f 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64  leno]);.  }.#end
22f32 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  if /* NDEBUG */.
22f33 0a 20 20 2f 2a 20 53 69 6c 65 6e 63 65 20 63 6f  .  /* Silence co
22f34 6d 70 6c 61 69 6e 74 73 20 66 72 6f 6d 20 70 75  mplaints from pu
22f35 72 69 66 79 20 61 62 6f 75 74 20 79 79 67 6f 74  rify about yygot
22f36 6f 6d 69 6e 6f 72 20 62 65 69 6e 67 20 75 6e 69  ominor being uni
22f37 6e 69 74 69 61 6c 69 7a 65 64 0a 20 20 2a 2a 20  nitialized.  ** 
22f38 69 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 77 68  in some cases wh
22f39 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20  en it is copied 
22f3a 69 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61  into the stack a
22f3b 66 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69  fter the followi
22f3c 6e 67 0a 20 20 2a 2a 20 73 77 69 74 63 68 2e 20  ng.  ** switch. 
22f3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 69 73 20   yygotominor is 
22f3e 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68  uninitialized wh
22f3f 65 6e 20 61 20 72 75 6c 65 20 72 65 64 75 63 65  en a rule reduce
22f40 73 20 74 68 61 74 20 64 6f 65 73 0a 20 20 2a 2a  s that does.  **
22f41 20 6e 6f 74 20 73 65 74 20 74 68 65 20 76 61 6c   not set the val
22f42 75 65 20 6f 66 20 69 74 73 20 6c 65 66 74 2d 68  ue of its left-h
22f43 61 6e 64 20 73 69 64 65 20 6e 6f 6e 74 65 72 6d  and side nonterm
22f44 69 6e 61 6c 2e 20 20 4c 65 61 76 69 6e 67 20 74  inal.  Leaving t
22f45 68 65 0a 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66  he.  ** value of
22f46 20 74 68 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c   the nonterminal
22f47 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69   uninitialized i
22f48 73 20 75 74 74 65 72 6c 79 20 68 61 72 6d 6c 65  s utterly harmle
22f49 73 73 20 61 73 20 6c 6f 6e 67 0a 20 20 2a 2a 20  ss as long.  ** 
22f4a 61 73 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  as the value is 
22f4b 6e 65 76 65 72 20 75 73 65 64 2e 20 20 53 6f 20  never used.  So 
22f4c 72 65 61 6c 6c 79 20 74 68 65 20 6f 6e 6c 79 20  really the only 
22f4d 74 68 69 6e 67 20 74 68 69 73 20 63 6f 64 65 0a  thing this code.
22f4e 20 20 2a 2a 20 61 63 63 6f 6d 70 6c 69 73 68 65    ** accomplishe
22f4f 73 20 69 73 20 74 6f 20 71 75 69 65 74 65 6e 20  s is to quieten 
22f50 70 75 72 69 66 79 2e 20 20 0a 20 20 2a 2a 0a 20  purify.  .  **. 
22f51 20 2a 2a 20 32 30 30 37 2d 30 31 2d 31 36 3a 20   ** 2007-01-16: 
22f52 20 54 68 65 20 77 69 72 65 73 68 61 72 6b 20 70   The wireshark p
22f53 72 6f 6a 65 63 74 20 28 77 77 77 2e 77 69 72 65  roject (www.wire
22f54 73 68 61 72 6b 2e 6f 72 67 29 20 72 65 70 6f 72  shark.org) repor
22f55 74 73 20 74 68 61 74 0a 20 20 2a 2a 20 77 69 74  ts that.  ** wit
22f56 68 6f 75 74 20 74 68 69 73 20 63 6f 64 65 2c 20  hout this code, 
22f57 74 68 65 69 72 20 70 61 72 73 65 72 20 73 65 67  their parser seg
22f58 66 61 75 6c 74 73 2e 20 20 49 27 6d 20 6e 6f 74  faults.  I'm not
22f59 20 73 75 72 65 20 77 68 61 74 20 74 68 65 72 65   sure what there
22f5a 0a 20 20 2a 2a 20 70 61 72 73 65 72 20 69 73 20  .  ** parser is 
22f5b 64 6f 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68  doing to make th
22f5c 69 73 20 68 61 70 70 65 6e 2e 20 20 54 68 69 73  is happen.  This
22f5d 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 62   is the second b
22f5e 75 67 20 72 65 70 6f 72 74 0a 20 20 2a 2a 20 66  ug report.  ** f
22f5f 72 6f 6d 20 77 69 72 65 73 68 61 72 6b 20 74 68  rom wireshark th
22f60 69 73 20 77 65 65 6b 2e 20 20 43 6c 65 61 72 6c  is week.  Clearl
22f61 79 20 74 68 65 79 20 61 72 65 20 73 74 72 65 73  y they are stres
22f62 73 69 6e 67 20 4c 65 6d 6f 6e 20 69 6e 20 77 61  sing Lemon in wa
22f63 79 73 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20  ys.  ** that it 
22f64 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65  has not been pre
22f65 76 69 6f 75 73 6c 79 20 73 74 72 65 73 73 65 64  viously stressed
22f66 2e 2e 2e 20 20 28 53 51 4c 69 74 65 20 74 69 63  ...  (SQLite tic
22f67 6b 65 74 20 23 32 31 37 32 29 0a 20 20 2a 2f 0a  ket #2172).  */.
22f68 20 20 6d 65 6d 73 65 74 28 26 79 79 67 6f 74 6f    memset(&yygoto
22f69 6d 69 6e 6f 72 2c 20 30 2c 20 73 69 7a 65 6f 66  minor, 0, sizeof
22f6a 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 29 3b 0a  (yygotominor));.
22f6b 0a 0a 20 20 73 77 69 74 63 68 28 20 79 79 72 75  ..  switch( yyru
22f6c 6c 65 6e 6f 20 29 7b 0a 20 20 2f 2a 20 42 65 67  leno ){.  /* Beg
22f6d 69 6e 6e 69 6e 67 20 68 65 72 65 20 61 72 65 20  inning here are 
22f6e 74 68 65 20 72 65 64 75 63 74 69 6f 6e 20 63 61  the reduction ca
22f6f 73 65 73 2e 20 20 41 20 74 79 70 69 63 61 6c 20  ses.  A typical 
22f70 65 78 61 6d 70 6c 65 0a 20 20 2a 2a 20 66 6f 6c  example.  ** fol
22f71 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 63 61 73  lows:.  **   cas
22f72 65 20 30 3a 0a 20 20 2a 2a 20 20 23 6c 69 6e 65  e 0:.  **  #line
22f73 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 67 72 61 6d 6d   <lineno> <gramm
22f74 61 72 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20  arfile>.  **    
22f75 20 7b 20 2e 2e 2e 20 7d 20 20 20 20 20 20 20 20   { ... }        
22f76 20 20 20 2f 2f 20 55 73 65 72 20 73 75 70 70 6c     // User suppl
22f77 69 65 64 20 63 6f 64 65 0a 20 20 2a 2a 20 20 23  ied code.  **  #
22f78 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74  line <lineno> <t
22f79 68 69 73 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20  hisfile>.  **   
22f7a 20 20 62 72 65 61 6b 3b 0a 20 20 2a 2f 0a 20 20    break;.  */.  
22f7b 20 20 20 20 63 61 73 65 20 30 3a 0a 20 20 20 20      case 0:.    
22f7c 20 20 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20    case 1:.      
22f7d 63 61 73 65 20 32 3a 0a 20 20 20 20 20 20 63 61  case 2:.      ca
22f7e 73 65 20 34 3a 0a 20 20 20 20 20 20 63 61 73 65  se 4:.      case
22f7f 20 35 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31   5:.      case 1
22f80 30 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  0:.      case 11
22f81 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 3a  :.      case 12:
22f82 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 3a 0a  .      case 20:.
22f83 20 20 20 20 20 20 63 61 73 65 20 32 38 3a 0a 20        case 28:. 
22f84 20 20 20 20 20 63 61 73 65 20 32 39 3a 0a 20 20       case 29:.  
22f85 20 20 20 20 63 61 73 65 20 33 37 3a 0a 20 20 20      case 37:.   
22f86 20 20 20 63 61 73 65 20 34 34 3a 0a 20 20 20 20     case 44:.    
22f87 20 20 63 61 73 65 20 34 35 3a 0a 20 20 20 20 20    case 45:.     
22f88 20 63 61 73 65 20 34 36 3a 0a 20 20 20 20 20 20   case 46:.      
22f89 63 61 73 65 20 34 37 3a 0a 20 20 20 20 20 20 63  case 47:.      c
22f8a 61 73 65 20 34 38 3a 0a 20 20 20 20 20 20 63 61  ase 48:.      ca
22f8b 73 65 20 34 39 3a 0a 20 20 20 20 20 20 63 61 73  se 49:.      cas
22f8c 65 20 35 35 3a 0a 20 20 20 20 20 20 63 61 73 65  e 55:.      case
22f8d 20 38 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20   82:.      case 
22f8e 38 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 38  83:.      case 8
22f8f 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20 38 35  4:.      case 85
22f90 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 36  :.      case 256
22f91 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 37  :.      case 257
22f92 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 37  :.      case 267
22f93 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 38  :.      case 268
22f94 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 38  :.      case 288
22f95 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 39  :.      case 289
22f96 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 37  :.      case 297
22f97 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 38  :.      case 298
22f98 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 32  :.      case 302
22f99 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 33  :.      case 303
22f9a 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 35  :.      case 305
22f9b 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 39  :.      case 309
22f9c 3a 0a 7b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  :.{.}.        br
22f9d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
22f9e 33 3a 0a 7b 20 73 71 6c 69 74 65 33 46 69 6e 69  3:.{ sqlite3Fini
22f9f 73 68 43 6f 64 69 6e 67 28 70 50 61 72 73 65 29  shCoding(pParse)
22fa0 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
22fa1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 3a  k;.      case 6:
22fa2 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50  .{ sqlite3BeginP
22fa3 61 72 73 65 28 70 50 61 72 73 65 2c 20 30 29 3b  arse(pParse, 0);
22fa4 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
22fa5 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 3a 0a  ;.      case 7:.
22fa6 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  { sqlite3BeginPa
22fa7 72 73 65 28 70 50 61 72 73 65 2c 20 31 29 3b 20  rse(pParse, 1); 
22fa8 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
22fa9 0a 20 20 20 20 20 20 63 61 73 65 20 38 3a 0a 7b  .      case 8:.{
22faa 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72   sqlite3BeginPar
22fab 73 65 28 70 50 61 72 73 65 2c 20 32 29 3b 20 7d  se(pParse, 2); }
22fac 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
22fad 20 20 20 20 20 20 63 61 73 65 20 39 3a 0a 7b 73        case 9:.{s
22fae 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73  qlite3BeginTrans
22faf 61 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79  action(pParse, y
22fb0 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
22fb1 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  y46);}.        b
22fb2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
22fb3 20 31 33 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f   13:.{yygotomino
22fb4 72 2e 79 79 34 36 20 3d 20 54 4b 5f 44 45 46 45  r.yy46 = TK_DEFE
22fb5 52 52 45 44 3b 7d 0a 20 20 20 20 20 20 20 20 62  RRED;}.        b
22fb6 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
22fb7 20 31 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20   14:.      case 
22fb8 31 35 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31  15:.      case 1
22fb9 36 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  6:.      case 10
22fba 37 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  7:.      case 10
22fbb 39 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  9:.{yygotominor.
22fbc 79 79 34 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  yy46 = yymsp[0].
22fbd 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20 20 20 20  major;}.        
22fbe 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
22fbf 65 20 31 37 3a 0a 20 20 20 20 20 20 63 61 73 65  e 17:.      case
22fc0 20 31 38 3a 0a 7b 73 71 6c 69 74 65 33 43 6f 6d   18:.{sqlite3Com
22fc1 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  mitTransaction(p
22fc2 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20  Parse);}.       
22fc3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
22fc4 73 65 20 31 39 3a 0a 7b 73 71 6c 69 74 65 33 52  se 19:.{sqlite3R
22fc5 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69  ollbackTransacti
22fc6 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20  on(pParse);}.   
22fc7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22fc8 20 20 63 61 73 65 20 32 31 3a 0a 7b 0a 20 20 20    case 21:.{.   
22fc9 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
22fca 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  e(pParse,&yymsp[
22fcb 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c  -1].minor.yy410,
22fcc 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
22fcd 79 79 34 31 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e  yy410,yymsp[-4].
22fce 6d 69 6e 6f 72 2e 79 79 34 36 2c 30 2c 30 2c 79  minor.yy46,0,0,y
22fcf 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
22fd0 79 34 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y46);.}.        
22fd1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
22fd2 65 20 32 32 3a 0a 20 20 20 20 20 20 63 61 73 65  e 22:.      case
22fd3 20 32 35 3a 0a 20 20 20 20 20 20 63 61 73 65 20   25:.      case 
22fd4 36 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 37  63:.      case 7
22fd5 37 3a 0a 20 20 20 20 20 20 63 61 73 65 20 37 39  7:.      case 79
22fd6 3a 0a 20 20 20 20 20 20 63 61 73 65 20 39 30 3a  :.      case 90:
22fd7 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 31 3a  .      case 101:
22fd8 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 32 3a  .      case 112:
22fd9 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 33 3a  .      case 113:
22fda 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 32 3a  .      case 212:
22fdb 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 35 3a  .      case 215:
22fdc 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
22fdd 34 36 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  46 = 0;}.       
22fde 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
22fdf 73 65 20 32 33 3a 0a 20 20 20 20 20 20 63 61 73  se 23:.      cas
22fe0 65 20 32 34 3a 0a 20 20 20 20 20 20 63 61 73 65  e 24:.      case
22fe1 20 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20   64:.      case 
22fe2 37 38 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31  78:.      case 1
22fe3 30 30 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31  00:.      case 1
22fe4 31 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32  11:.      case 2
22fe5 31 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32  13:.      case 2
22fe6 31 36 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  16:.{yygotominor
22fe7 2e 79 79 34 36 20 3d 20 31 3b 7d 0a 20 20 20 20  .yy46 = 1;}.    
22fe8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
22fe9 20 63 61 73 65 20 32 36 3a 0a 7b 0a 20 20 73 71   case 26:.{.  sq
22fea 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50  lite3EndTable(pP
22feb 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
22fec 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d  minor.yy410,&yym
22fed 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
22fee 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
22fef 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
22ff0 32 37 3a 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45  27:.{.  sqlite3E
22ff1 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c 30  ndTable(pParse,0
22ff2 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  ,0,yymsp[0].mino
22ff3 72 2e 79 79 32 31 39 29 3b 0a 20 20 73 71 6c 69  r.yy219);.  sqli
22ff4 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
22ff5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
22ff6 79 32 31 39 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y219);.}.       
22ff7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
22ff8 73 65 20 33 30 3a 0a 7b 0a 20 20 79 79 67 6f 74  se 30:.{.  yygot
22ff9 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 20 3d  ominor.yy410.z =
22ffa 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
22ffb 2e 79 79 34 31 30 2e 7a 3b 0a 20 20 79 79 67 6f  .yy410.z;.  yygo
22ffc 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 20  tominor.yy410.n 
22ffd 3d 20 28 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  = (pParse->sLast
22ffe 54 6f 6b 65 6e 2e 7a 2d 79 79 6d 73 70 5b 2d 32  Token.z-yymsp[-2
22fff 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 29  ].minor.yy410.z)
23000 20 2b 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74   + pParse->sLast
23001 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20 20 20 20 20  Token.n;.}.     
23002 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
23003 63 61 73 65 20 33 31 3a 0a 7b 0a 20 20 73 71 6c  case 31:.{.  sql
23004 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 70 50  ite3AddColumn(pP
23005 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  arse,&yymsp[0].m
23006 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 79  inor.yy410);.  y
23007 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30  ygotominor.yy410
23008 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
23009 72 2e 79 79 34 31 30 3b 0a 7d 0a 20 20 20 20 20  r.yy410;.}.     
2300a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2300b 63 61 73 65 20 33 32 3a 0a 20 20 20 20 20 20 63  case 32:.      c
2300c 61 73 65 20 33 33 3a 0a 20 20 20 20 20 20 63 61  ase 33:.      ca
2300d 73 65 20 33 34 3a 0a 20 20 20 20 20 20 63 61 73  se 34:.      cas
2300e 65 20 33 35 3a 0a 20 20 20 20 20 20 63 61 73 65  e 35:.      case
2300f 20 33 36 3a 0a 20 20 20 20 20 20 63 61 73 65 20   36:.      case 
23010 32 35 35 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  255:.{yygotomino
23011 72 2e 79 79 34 31 30 20 3d 20 79 79 6d 73 70 5b  r.yy410 = yymsp[
23012 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20  0].minor.yy0;}. 
23013 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23014 20 20 20 20 63 61 73 65 20 33 38 3a 0a 7b 73 71      case 38:.{sq
23015 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79  lite3AddColumnTy
23016 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  pe(pParse,&yymsp
23017 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29  [0].minor.yy410)
23018 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
23019 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 39 3a  ;.      case 39:
2301a 0a 20 20 20 20 20 20 63 61 73 65 20 34 32 3a 0a  .      case 42:.
2301b 20 20 20 20 20 20 63 61 73 65 20 31 31 39 3a 0a        case 119:.
2301c 20 20 20 20 20 20 63 61 73 65 20 31 32 30 3a 0a        case 120:.
2301d 20 20 20 20 20 20 63 61 73 65 20 31 33 31 3a 0a        case 131:.
2301e 20 20 20 20 20 20 63 61 73 65 20 32 34 30 3a 0a        case 240:.
2301f 20 20 20 20 20 20 63 61 73 65 20 32 34 32 3a 0a        case 242:.
23020 20 20 20 20 20 20 63 61 73 65 20 32 35 31 3a 0a        case 251:.
23021 20 20 20 20 20 20 63 61 73 65 20 32 35 32 3a 0a        case 252:.
23022 20 20 20 20 20 20 63 61 73 65 20 32 35 33 3a 0a        case 253:.
23023 20 20 20 20 20 20 63 61 73 65 20 32 35 34 3a 0a        case 254:.
23024 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
23025 31 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  10 = yymsp[0].mi
23026 6e 6f 72 2e 79 79 34 31 30 3b 7d 0a 20 20 20 20  nor.yy410;}.    
23027 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
23028 20 63 61 73 65 20 34 30 3a 0a 7b 0a 20 20 79 79   case 40:.{.  yy
23029 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e  gotominor.yy410.
2302a 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  z = yymsp[-3].mi
2302b 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 20 20 79  nor.yy410.z;.  y
2302c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30  ygotominor.yy410
2302d 2e 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  .n = &yymsp[0].m
2302e 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
2302f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
23030 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   - yymsp[-3].min
23031 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 7d 0a 20 20  or.yy410.z;.}.  
23032 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23033 20 20 20 63 61 73 65 20 34 31 3a 0a 7b 0a 20 20     case 41:.{.  
23034 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
23035 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e  0.z = yymsp[-5].
23036 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 20  minor.yy410.z;. 
23037 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
23038 31 30 2e 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d  10.n = &yymsp[0]
23039 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
2303a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
2303b 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  n] - yymsp[-5].m
2303c 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 7d 0a  inor.yy410.z;.}.
2303d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2303e 20 20 20 20 20 63 61 73 65 20 34 33 3a 0a 7b 79       case 43:.{y
2303f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30  ygotominor.yy410
23040 2e 7a 3d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  .z=yymsp[-1].min
23041 6f 72 2e 79 79 34 31 30 2e 7a 3b 20 79 79 67 6f  or.yy410.z; yygo
23042 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 3d  tominor.yy410.n=
23043 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
23044 79 34 31 30 2e 6e 2b 28 79 79 6d 73 70 5b 30 5d  y410.n+(yymsp[0]
23045 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 2d 79  .minor.yy410.z-y
23046 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
23047 79 34 31 30 2e 7a 29 3b 7d 0a 20 20 20 20 20 20  y410.z);}.      
23048 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
23049 61 73 65 20 35 30 3a 0a 20 20 20 20 20 20 63 61  ase 50:.      ca
2304a 73 65 20 35 32 3a 0a 7b 73 71 6c 69 74 65 33 41  se 52:.{sqlite3A
2304b 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70  ddDefaultValue(p
2304c 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d  Parse,yymsp[0].m
2304d 69 6e 6f 72 2e 79 79 31 37 32 29 3b 7d 0a 20 20  inor.yy172);}.  
2304e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2304f 20 20 20 63 61 73 65 20 35 31 3a 0a 7b 73 71 6c     case 51:.{sql
23050 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61  ite3AddDefaultVa
23051 6c 75 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70  lue(pParse,yymsp
23052 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-1].minor.yy172
23053 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
23054 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 33  k;.      case 53
23055 3a 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  :.{.  Expr *p = 
23056 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
23057 72 73 65 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c 20  rse, TK_UMINUS, 
23058 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
23059 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73  y172, 0, 0);.  s
2305a 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
2305b 56 61 6c 75 65 28 70 50 61 72 73 65 2c 70 29 3b  Value(pParse,p);
2305c 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2305d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 34 3a  ;.      case 54:
2305e 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73  .{.  Expr *p = s
2305f 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
23060 73 65 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20 30  se, TK_STRING, 0
23061 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  , 0, &yymsp[0].m
23062 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 73  inor.yy410);.  s
23063 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
23064 56 61 6c 75 65 28 70 50 61 72 73 65 2c 70 29 3b  Value(pParse,p);
23065 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
23066 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 36 3a  ;.      case 56:
23067 0a 7b 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e  .{sqlite3AddNotN
23068 75 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ull(pParse, yyms
23069 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29  p[0].minor.yy46)
2306a 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2306b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 37 3a  ;.      case 57:
2306c 0a 7b 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d  .{sqlite3AddPrim
2306d 61 72 79 4b 65 79 28 70 50 61 72 73 65 2c 30 2c  aryKey(pParse,0,
2306e 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2306f 79 79 34 36 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy46,yymsp[0].mi
23070 6e 6f 72 2e 79 79 34 36 2c 79 79 6d 73 70 5b 2d  nor.yy46,yymsp[-
23071 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d  2].minor.yy46);}
23072 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23073 20 20 20 20 20 20 63 61 73 65 20 35 38 3a 0a 7b        case 58:.{
23074 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
23075 65 78 28 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c  ex(pParse,0,0,0,
23076 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  0,yymsp[0].minor
23077 2e 79 79 34 36 2c 30 2c 30 2c 30 2c 30 29 3b 7d  .yy46,0,0,0,0);}
23078 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23079 20 20 20 20 20 20 63 61 73 65 20 35 39 3a 0a 7b        case 59:.{
2307a 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43  sqlite3AddCheckC
2307b 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65  onstraint(pParse
2307c 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2307d 2e 79 79 31 37 32 29 3b 7d 0a 20 20 20 20 20 20  .yy172);}.      
2307e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2307f 61 73 65 20 36 30 3a 0a 7b 73 71 6c 69 74 65 33  ase 60:.{sqlite3
23080 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79  CreateForeignKey
23081 28 70 50 61 72 73 65 2c 30 2c 26 79 79 6d 73 70  (pParse,0,&yymsp
23082 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-2].minor.yy410
23083 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
23084 2e 79 79 31 37 34 2c 79 79 6d 73 70 5b 30 5d 2e  .yy174,yymsp[0].
23085 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a 20 20  minor.yy46);}.  
23086 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23087 20 20 20 63 61 73 65 20 36 31 3a 0a 7b 73 71 6c     case 61:.{sql
23088 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
23089 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70  Key(pParse,yymsp
2308a 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b  [0].minor.yy46);
2308b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2308c 0a 20 20 20 20 20 20 63 61 73 65 20 36 32 3a 0a  .      case 62:.
2308d 7b 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61  {sqlite3AddColla
2308e 74 65 54 79 70 65 28 70 50 61 72 73 65 2c 20 28  teType(pParse, (
2308f 63 68 61 72 2a 29 79 79 6d 73 70 5b 30 5d 2e 6d  char*)yymsp[0].m
23090 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 2c 20 79 79  inor.yy410.z, yy
23091 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
23092 31 30 2e 6e 29 3b 7d 0a 20 20 20 20 20 20 20 20  10.n);}.        
23093 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
23094 65 20 36 35 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69  e 65:.{ yygotomi
23095 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 52 65  nor.yy46 = OE_Re
23096 73 74 72 69 63 74 20 2a 20 30 78 30 31 30 31 30  strict * 0x01010
23097 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  1; }.        bre
23098 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
23099 36 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  6:.{ yygotominor
2309a 2e 79 79 34 36 20 3d 20 28 79 79 6d 73 70 5b 2d  .yy46 = (yymsp[-
2309b 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 20 26 20  1].minor.yy46 & 
2309c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2309d 79 34 30 35 2e 6d 61 73 6b 29 20 7c 20 79 79 6d  y405.mask) | yym
2309e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  sp[0].minor.yy40
2309f 35 2e 76 61 6c 75 65 3b 20 7d 0a 20 20 20 20 20  5.value; }.     
230a0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
230a1 63 61 73 65 20 36 37 3a 0a 7b 20 79 79 67 6f 74  case 67:.{ yygot
230a2 6f 6d 69 6e 6f 72 2e 79 79 34 30 35 2e 76 61 6c  ominor.yy405.val
230a3 75 65 20 3d 20 30 3b 20 20 20 20 20 79 79 67 6f  ue = 0;     yygo
230a4 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 35 2e 6d 61  tominor.yy405.ma
230a5 73 6b 20 3d 20 30 78 30 30 30 30 30 30 3b 20 7d  sk = 0x000000; }
230a6 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
230a7 20 20 20 20 20 20 63 61 73 65 20 36 38 3a 0a 7b        case 68:.{
230a8 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
230a9 30 35 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70  05.value = yymsp
230aa 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 20  [0].minor.yy46; 
230ab 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
230ac 79 79 34 30 35 2e 6d 61 73 6b 20 3d 20 30 78 30  yy405.mask = 0x0
230ad 30 30 30 66 66 3b 20 7d 0a 20 20 20 20 20 20 20  000ff; }.       
230ae 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
230af 73 65 20 36 39 3a 0a 7b 20 79 79 67 6f 74 6f 6d  se 69:.{ yygotom
230b0 69 6e 6f 72 2e 79 79 34 30 35 2e 76 61 6c 75 65  inor.yy405.value
230b1 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
230b2 72 2e 79 79 34 36 3c 3c 38 3b 20 20 79 79 67 6f  r.yy46<<8;  yygo
230b3 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 35 2e 6d 61  tominor.yy405.ma
230b4 73 6b 20 3d 20 30 78 30 30 66 66 30 30 3b 20 7d  sk = 0x00ff00; }
230b5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
230b6 20 20 20 20 20 20 63 61 73 65 20 37 30 3a 0a 7b        case 70:.{
230b7 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
230b8 30 35 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70  05.value = yymsp
230b9 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3c 3c  [0].minor.yy46<<
230ba 31 36 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  16; yygotominor.
230bb 79 79 34 30 35 2e 6d 61 73 6b 20 3d 20 30 78 66  yy405.mask = 0xf
230bc 66 30 30 30 30 3b 20 7d 0a 20 20 20 20 20 20 20  f0000; }.       
230bd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
230be 73 65 20 37 31 3a 0a 7b 20 79 79 67 6f 74 6f 6d  se 71:.{ yygotom
230bf 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 53  inor.yy46 = OE_S
230c0 65 74 4e 75 6c 6c 3b 20 7d 0a 20 20 20 20 20 20  etNull; }.      
230c1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
230c2 61 73 65 20 37 32 3a 0a 7b 20 79 79 67 6f 74 6f  ase 72:.{ yygoto
230c3 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f  minor.yy46 = OE_
230c4 53 65 74 44 66 6c 74 3b 20 7d 0a 20 20 20 20 20  SetDflt; }.     
230c5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
230c6 63 61 73 65 20 37 33 3a 0a 7b 20 79 79 67 6f 74  case 73:.{ yygot
230c7 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45  ominor.yy46 = OE
230c8 5f 43 61 73 63 61 64 65 3b 20 7d 0a 20 20 20 20  _Cascade; }.    
230c9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
230ca 20 63 61 73 65 20 37 34 3a 0a 7b 20 79 79 67 6f   case 74:.{ yygo
230cb 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f  tominor.yy46 = O
230cc 45 5f 52 65 73 74 72 69 63 74 3b 20 7d 0a 20 20  E_Restrict; }.  
230cd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
230ce 20 20 20 63 61 73 65 20 37 35 3a 0a 20 20 20 20     case 75:.    
230cf 20 20 63 61 73 65 20 37 36 3a 0a 20 20 20 20 20    case 76:.     
230d0 20 63 61 73 65 20 39 31 3a 0a 20 20 20 20 20 20   case 91:.      
230d1 63 61 73 65 20 39 33 3a 0a 20 20 20 20 20 20 63  case 93:.      c
230d2 61 73 65 20 39 35 3a 0a 20 20 20 20 20 20 63 61  ase 95:.      ca
230d3 73 65 20 39 36 3a 0a 20 20 20 20 20 20 63 61 73  se 96:.      cas
230d4 65 20 31 36 36 3a 0a 7b 79 79 67 6f 74 6f 6d 69  e 166:.{yygotomi
230d5 6e 6f 72 2e 79 79 34 36 20 3d 20 79 79 6d 73 70  nor.yy46 = yymsp
230d6 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 7d  [0].minor.yy46;}
230d7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
230d8 20 20 20 20 20 20 63 61 73 65 20 38 30 3a 0a 7b        case 80:.{
230d9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
230da 30 2e 6e 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d  0.n = 0; yygotom
230db 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 20 3d 20 30  inor.yy410.z = 0
230dc 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
230dd 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 31 3a  ;.      case 81:
230de 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
230df 34 31 30 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  410 = yymsp[-1].
230e0 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20  minor.yy0;}.    
230e1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
230e2 20 63 61 73 65 20 38 36 3a 0a 7b 73 71 6c 69 74   case 86:.{sqlit
230e3 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28  e3AddPrimaryKey(
230e4 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d  pParse,yymsp[-3]
230e5 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d  .minor.yy174,yym
230e6 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  sp[0].minor.yy46
230e7 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
230e8 2e 79 79 34 36 2c 30 29 3b 7d 0a 20 20 20 20 20  .yy46,0);}.     
230e9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
230ea 63 61 73 65 20 38 37 3a 0a 7b 73 71 6c 69 74 65  case 87:.{sqlite
230eb 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61  3CreateIndex(pPa
230ec 72 73 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b  rse,0,0,0,yymsp[
230ed 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c  -2].minor.yy174,
230ee 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
230ef 79 34 36 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20  y46,0,0,0,0);}. 
230f0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
230f1 20 20 20 20 63 61 73 65 20 38 38 3a 0a 7b 73 71      case 88:.{sq
230f2 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e  lite3AddCheckCon
230f3 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 79  straint(pParse,y
230f4 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
230f5 79 31 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20  y172);}.        
230f6 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
230f7 65 20 38 39 3a 0a 7b 0a 20 20 20 20 73 71 6c 69  e 89:.{.    sqli
230f8 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
230f9 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73  Key(pParse, yyms
230fa 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-6].minor.yy17
230fb 34 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  4, &yymsp[-3].mi
230fc 6e 6f 72 2e 79 79 34 31 30 2c 20 79 79 6d 73 70  nor.yy410, yymsp
230fd 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34  [-2].minor.yy174
230fe 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
230ff 72 2e 79 79 34 36 29 3b 0a 20 20 20 20 73 71 6c  r.yy46);.    sql
23100 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
23101 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73  Key(pParse, yyms
23102 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29  p[0].minor.yy46)
23103 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
23104 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 32  k;.      case 92
23105 3a 0a 20 20 20 20 20 20 63 61 73 65 20 39 34 3a  :.      case 94:
23106 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
23107 34 36 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b  46 = OE_Default;
23108 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
23109 0a 20 20 20 20 20 20 63 61 73 65 20 39 37 3a 0a  .      case 97:.
2310a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
2310b 36 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 7d 0a  6 = OE_Ignore;}.
2310c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2310d 20 20 20 20 20 63 61 73 65 20 39 38 3a 0a 20 20       case 98:.  
2310e 20 20 20 20 63 61 73 65 20 31 36 37 3a 0a 7b 79      case 167:.{y
2310f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20  ygotominor.yy46 
23110 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20  = OE_Replace;}. 
23111 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23112 20 20 20 20 63 61 73 65 20 39 39 3a 0a 7b 0a 20      case 99:.{. 
23113 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c   sqlite3DropTabl
23114 65 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  e(pParse, yymsp[
23115 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 20  0].minor.yy373, 
23116 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0, yymsp[-1].min
23117 6f 72 2e 79 79 34 36 29 3b 0a 7d 0a 20 20 20 20  or.yy46);.}.    
23118 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
23119 20 63 61 73 65 20 31 30 32 3a 0a 7b 0a 20 20 73   case 102:.{.  s
2311a 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77  qlite3CreateView
2311b 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
2311c 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  -7].minor.yy0, &
2311d 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2311e 79 79 34 31 30 2c 20 26 79 79 6d 73 70 5b 2d 32  yy410, &yymsp[-2
2311f 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 79  ].minor.yy410, y
23120 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
23121 32 31 39 2c 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d  219, yymsp[-6].m
23122 69 6e 6f 72 2e 79 79 34 36 2c 20 79 79 6d 73 70  inor.yy46, yymsp
23123 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29  [-4].minor.yy46)
23124 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
23125 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
23126 33 3a 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72  3:.{.  sqlite3Dr
23127 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  opTable(pParse, 
23128 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
23129 79 33 37 33 2c 20 31 2c 20 79 79 6d 73 70 5b 2d  y373, 1, yymsp[-
2312a 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a  1].minor.yy46);.
2312b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2312c 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 34 3a  .      case 104:
2312d 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .{.  sqlite3Sele
2312e 63 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ct(pParse, yymsp
2312f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 2c  [0].minor.yy219,
23130 20 53 52 54 5f 43 61 6c 6c 62 61 63 6b 2c 20 30   SRT_Callback, 0
23131 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
23132 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
23133 6c 65 74 65 28 79 79 6d 73 70 5b 30 5d 2e 6d 69  lete(yymsp[0].mi
23134 6e 6f 72 2e 79 79 32 31 39 29 3b 0a 7d 0a 20 20  nor.yy219);.}.  
23135 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23136 20 20 20 63 61 73 65 20 31 30 35 3a 0a 20 20 20     case 105:.   
23137 20 20 20 63 61 73 65 20 31 32 38 3a 0a 7b 79 79     case 128:.{yy
23138 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 39 20  gotominor.yy219 
23139 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
2313a 2e 79 79 32 31 39 3b 7d 0a 20 20 20 20 20 20 20  .yy219;}.       
2313b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2313c 73 65 20 31 30 36 3a 0a 7b 0a 20 20 69 66 28 20  se 106:.{.  if( 
2313d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2313e 79 32 31 39 20 29 7b 0a 20 20 20 20 79 79 6d 73  y219 ){.    yyms
2313f 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39  p[0].minor.yy219
23140 2d 3e 6f 70 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  ->op = yymsp[-1]
23141 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 0a 20 20 20  .minor.yy46;.   
23142 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
23143 79 79 32 31 39 2d 3e 70 50 72 69 6f 72 20 3d 20  yy219->pPrior = 
23144 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
23145 79 79 32 31 39 3b 0a 20 20 7d 65 6c 73 65 7b 0a  yy219;.  }else{.
23146 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
23147 74 44 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d 32  tDelete(yymsp[-2
23148 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 29 3b 0a  ].minor.yy219);.
23149 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f    }.  yygotomino
2314a 72 2e 79 79 32 31 39 20 3d 20 79 79 6d 73 70 5b  r.yy219 = yymsp[
2314b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 3b 0a  0].minor.yy219;.
2314c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2314d 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 38 3a  .      case 108:
2314e 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2314f 34 36 20 3d 20 54 4b 5f 41 4c 4c 3b 7d 0a 20 20  46 = TK_ALL;}.  
23150 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23151 20 20 20 63 61 73 65 20 31 31 30 3a 0a 7b 0a 20     case 110:.{. 
23152 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32   yygotominor.yy2
23153 31 39 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  19 = sqlite3Sele
23154 63 74 4e 65 77 28 70 50 61 72 73 65 2c 79 79 6d  ctNew(pParse,yym
23155 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-6].minor.yy1
23156 37 34 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  74,yymsp[-5].min
23157 6f 72 2e 79 79 33 37 33 2c 79 79 6d 73 70 5b 2d  or.yy373,yymsp[-
23158 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 79  4].minor.yy172,y
23159 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
2315a 79 31 37 34 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y174,yymsp[-2].m
2315b 69 6e 6f 72 2e 79 79 31 37 32 2c 79 79 6d 73 70  inor.yy172,yymsp
2315c 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34  [-1].minor.yy174
2315d 2c 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72  ,yymsp[-7].minor
2315e 2e 79 79 34 36 2c 79 79 6d 73 70 5b 30 5d 2e 6d  .yy46,yymsp[0].m
2315f 69 6e 6f 72 2e 79 79 32 33 34 2e 70 4c 69 6d 69  inor.yy234.pLimi
23160 74 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  t,yymsp[0].minor
23161 2e 79 79 32 33 34 2e 70 4f 66 66 73 65 74 29 3b  .yy234.pOffset);
23162 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
23163 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 34  ;.      case 114
23164 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 37  :.      case 237
23165 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  :.{yygotominor.y
23166 79 31 37 34 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  y174 = yymsp[-1]
23167 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 3b 7d 0a 20  .minor.yy174;}. 
23168 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23169 20 20 20 20 63 61 73 65 20 31 31 35 3a 0a 20 20      case 115:.  
2316a 20 20 20 20 63 61 73 65 20 31 34 31 3a 0a 20 20      case 141:.  
2316b 20 20 20 20 63 61 73 65 20 31 34 39 3a 0a 20 20      case 149:.  
2316c 20 20 20 20 63 61 73 65 20 32 33 30 3a 0a 20 20      case 230:.  
2316d 20 20 20 20 63 61 73 65 20 32 33 36 3a 0a 7b 79      case 236:.{y
2316e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34  ygotominor.yy174
2316f 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
23170 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
23171 20 31 31 36 3a 0a 7b 0a 20 20 20 79 79 67 6f 74   116:.{.   yygot
23172 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73  ominor.yy174 = s
23173 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
23174 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
23175 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-2].minor.yy17
23176 34 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  4,yymsp[-1].mino
23177 72 2e 79 79 31 37 32 2c 79 79 6d 73 70 5b 30 5d  r.yy172,yymsp[0]
23178 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 3f 26  .minor.yy410.n?&
23179 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2317a 79 34 31 30 3a 30 29 3b 0a 7d 0a 20 20 20 20 20  y410:0);.}.     
2317b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2317c 63 61 73 65 20 31 31 37 3a 0a 7b 0a 20 20 45 78  case 117:.{.  Ex
2317d 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 50  pr *p = sqlite3P
2317e 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2317f 41 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ALL, 0, 0, 0);. 
23180 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
23181 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  74 = sqlite3Expr
23182 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
23183 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e, yymsp[-1].min
23184 6f 72 2e 79 79 31 37 34 2c 20 70 2c 20 30 29 3b  or.yy174, p, 0);
23185 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
23186 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 38  ;.      case 118
23187 3a 0a 7b 0a 20 20 45 78 70 72 20 2a 70 52 69 67  :.{.  Expr *pRig
23188 68 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ht = sqlite3PExp
23189 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 4c 4c  r(pParse, TK_ALL
2318a 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 45 78  , 0, 0, 0);.  Ex
2318b 70 72 20 2a 70 4c 65 66 74 20 3d 20 73 71 6c 69  pr *pLeft = sqli
2318c 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2318d 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79   TK_ID, 0, 0, &y
2318e 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2318f 79 34 31 30 29 3b 0a 20 20 45 78 70 72 20 2a 70  y410);.  Expr *p
23190 44 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78  Dot = sqlite3PEx
23191 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
23192 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  T, pLeft, pRight
23193 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  , 0);.  yygotomi
23194 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71 6c 69  nor.yy174 = sqli
23195 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
23196 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
23197 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20  3].minor.yy174, 
23198 70 44 6f 74 2c 20 30 29 3b 0a 7d 0a 20 20 20 20  pDot, 0);.}.    
23199 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2319a 20 63 61 73 65 20 31 32 31 3a 0a 7b 79 79 67 6f   case 121:.{yygo
2319b 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 20  tominor.yy410.n 
2319c 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
2319d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2319e 31 32 32 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  122:.{yygotomino
2319f 72 2e 79 79 33 37 33 20 3d 20 73 71 6c 69 74 65  r.yy373 = sqlite
231a0 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50  3DbMallocZero(pP
231a1 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66  arse->db, sizeof
231a2 28 2a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  (*yygotominor.yy
231a3 33 37 33 29 29 3b 7d 0a 20 20 20 20 20 20 20 20  373));}.        
231a4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
231a5 65 20 31 32 33 3a 0a 7b 0a 20 20 79 79 67 6f 74  e 123:.{.  yygot
231a6 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 20 3d 20 79  ominor.yy373 = y
231a7 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
231a8 33 37 33 3b 0a 20 20 73 71 6c 69 74 65 33 53 72  373;.  sqlite3Sr
231a9 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79  cListShiftJoinTy
231aa 70 65 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  pe(yygotominor.y
231ab 79 33 37 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y373);.}.       
231ac 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
231ad 73 65 20 31 32 34 3a 0a 7b 0a 20 20 20 79 79 67  se 124:.{.   yyg
231ae 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 20 3d  otominor.yy373 =
231af 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
231b0 2e 79 79 33 37 33 3b 0a 20 20 20 69 66 28 20 79  .yy373;.   if( y
231b1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 33  ygotominor.yy373
231b2 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   && yygotominor.
231b3 79 79 33 37 33 2d 3e 6e 53 72 63 3e 30 20 29 20  yy373->nSrc>0 ) 
231b4 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37  yygotominor.yy37
231b5 33 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72  3->a[yygotominor
231b6 2e 79 79 33 37 33 2d 3e 6e 53 72 63 2d 31 5d 2e  .yy373->nSrc-1].
231b7 6a 6f 69 6e 74 79 70 65 20 3d 20 79 79 6d 73 70  jointype = yymsp
231b8 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 0a  [0].minor.yy46;.
231b9 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
231ba 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 35 3a  .      case 125:
231bb 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
231bc 33 37 33 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  373 = 0;}.      
231bd 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
231be 61 73 65 20 31 32 36 3a 0a 7b 0a 20 20 79 79 67  ase 126:.{.  yyg
231bf 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 20 3d  otominor.yy373 =
231c0 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
231c1 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50  ppendFromTerm(pP
231c2 61 72 73 65 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d  arse,yymsp[-5].m
231c3 69 6e 6f 72 2e 79 79 33 37 33 2c 26 79 79 6d 73  inor.yy373,&yyms
231c4 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-4].minor.yy41
231c5 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
231c6 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b  or.yy410,&yymsp[
231c7 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c  -2].minor.yy410,
231c8 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  0,yymsp[-1].mino
231c9 72 2e 79 79 31 37 32 2c 79 79 6d 73 70 5b 30 5d  r.yy172,yymsp[0]
231ca 2e 6d 69 6e 6f 72 2e 79 79 34 33 32 29 3b 0a 7d  .minor.yy432);.}
231cb 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
231cc 20 20 20 20 20 20 63 61 73 65 20 31 32 37 3a 0a        case 127:.
231cd 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
231ce 72 2e 79 79 33 37 33 20 3d 20 73 71 6c 69 74 65  r.yy373 = sqlite
231cf 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72  3SrcListAppendFr
231d0 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79 79  omTerm(pParse,yy
231d1 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
231d2 33 37 33 2c 30 2c 30 2c 26 79 79 6d 73 70 5b 2d  373,0,0,&yymsp[-
231d3 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 79  2].minor.yy410,y
231d4 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
231d5 79 32 31 39 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y219,yymsp[-1].m
231d6 69 6e 6f 72 2e 79 79 31 37 32 2c 79 79 6d 73 70  inor.yy172,yymsp
231d7 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 32 29  [0].minor.yy432)
231d8 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ;.  }.        br
231d9 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
231da 31 32 39 3a 0a 7b 0a 20 20 20 20 20 73 71 6c 69  129:.{.     sqli
231db 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
231dc 6f 69 6e 54 79 70 65 28 79 79 6d 73 70 5b 30 5d  oinType(yymsp[0]
231dd 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 29 3b 0a 20  .minor.yy373);. 
231de 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
231df 79 79 32 31 39 20 3d 20 73 71 6c 69 74 65 33 53  yy219 = sqlite3S
231e0 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
231e1 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  0,yymsp[0].minor
231e2 2e 79 79 33 37 33 2c 30 2c 30 2c 30 2c 30 2c 30  .yy373,0,0,0,0,0
231e3 2c 30 2c 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20  ,0,0);.  }.     
231e4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
231e5 63 61 73 65 20 31 33 30 3a 0a 7b 79 79 67 6f 74  case 130:.{yygot
231e6 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3d 30  ominor.yy410.z=0
231e7 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
231e8 34 31 30 2e 6e 3d 30 3b 7d 0a 20 20 20 20 20 20  410.n=0;}.      
231e9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
231ea 61 73 65 20 31 33 32 3a 0a 7b 79 79 67 6f 74 6f  ase 132:.{yygoto
231eb 6d 69 6e 6f 72 2e 79 79 33 37 33 20 3d 20 73 71  minor.yy373 = sq
231ec 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
231ed 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c  nd(pParse->db,0,
231ee 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
231ef 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 30 5d  .yy410,&yymsp[0]
231f0 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d 0a  .minor.yy410);}.
231f1 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
231f2 20 20 20 20 20 63 61 73 65 20 31 33 33 3a 0a 7b       case 133:.{
231f3 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
231f4 36 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 20 7d 0a  6 = JT_INNER; }.
231f5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
231f6 20 20 20 20 20 63 61 73 65 20 31 33 34 3a 0a 7b       case 134:.{
231f7 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
231f8 36 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54  6 = sqlite3JoinT
231f9 79 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  ype(pParse,&yyms
231fa 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
231fb 30 2c 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  0,0); }.        
231fc 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
231fd 65 20 31 33 35 3a 0a 7b 20 79 79 67 6f 74 6f 6d  e 135:.{ yygotom
231fe 69 6e 6f 72 2e 79 79 34 36 20 3d 20 73 71 6c 69  inor.yy46 = sqli
231ff 74 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72  te3JoinType(pPar
23200 73 65 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se,&yymsp[-2].mi
23201 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
23202 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 30  1].minor.yy410,0
23203 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
23204 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
23205 33 36 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f  36:.{ yygotomino
23206 72 2e 79 79 34 36 20 3d 20 73 71 6c 69 74 65 33  r.yy46 = sqlite3
23207 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c  JoinType(pParse,
23208 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
23209 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  .yy0,&yymsp[-2].
2320a 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d  minor.yy410,&yym
2320b 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
2320c 31 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  10); }.        b
2320d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2320e 20 31 33 37 3a 0a 20 20 20 20 20 20 63 61 73 65   137:.      case
2320f 20 31 34 35 3a 0a 20 20 20 20 20 20 63 61 73 65   145:.      case
23210 20 31 35 32 3a 0a 20 20 20 20 20 20 63 61 73 65   152:.      case
23211 20 31 35 39 3a 0a 20 20 20 20 20 20 63 61 73 65   159:.      case
23212 20 31 37 34 3a 0a 20 20 20 20 20 20 63 61 73 65   174:.      case
23213 20 32 30 32 3a 0a 20 20 20 20 20 20 63 61 73 65   202:.      case
23214 20 32 32 35 3a 0a 20 20 20 20 20 20 63 61 73 65   225:.      case
23215 20 32 32 37 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e   227:.{yygotomin
23216 6f 72 2e 79 79 31 37 32 20 3d 20 79 79 6d 73 70  or.yy172 = yymsp
23217 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b  [0].minor.yy172;
23218 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
23219 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 38 3a  .      case 138:
2321a 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 31 3a  .      case 151:
2321b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 38 3a  .      case 158:
2321c 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 33 3a  .      case 203:
2321d 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 36 3a  .      case 226:
2321e 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 38 3a  .      case 228:
2321f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
23220 31 37 32 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  172 = 0;}.      
23221 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
23222 61 73 65 20 31 33 39 3a 0a 20 20 20 20 20 20 63  ase 139:.      c
23223 61 73 65 20 31 37 31 3a 0a 7b 79 79 67 6f 74 6f  ase 171:.{yygoto
23224 6d 69 6e 6f 72 2e 79 79 34 33 32 20 3d 20 79 79  minor.yy432 = yy
23225 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
23226 34 33 32 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  432;}.        br
23227 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
23228 31 34 30 3a 0a 20 20 20 20 20 20 63 61 73 65 20  140:.      case 
23229 31 37 30 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  170:.{yygotomino
2322a 72 2e 79 79 34 33 32 20 3d 20 30 3b 7d 0a 20 20  r.yy432 = 0;}.  
2322b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2322c 20 20 20 63 61 73 65 20 31 34 32 3a 0a 20 20 20     case 142:.   
2322d 20 20 20 63 61 73 65 20 31 35 30 3a 0a 20 20 20     case 150:.   
2322e 20 20 20 63 61 73 65 20 32 32 39 3a 0a 7b 79 79     case 229:.{yy
2322f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20  gotominor.yy174 
23230 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
23231 2e 79 79 31 37 34 3b 7d 0a 20 20 20 20 20 20 20  .yy174;}.       
23232 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
23233 73 65 20 31 34 33 3a 0a 7b 0a 20 20 79 79 67 6f  se 143:.{.  yygo
23234 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20  tominor.yy174 = 
23235 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
23236 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d  ppend(pParse,yym
23237 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
23238 37 34 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  74,yymsp[-1].min
23239 6f 72 2e 79 79 31 37 32 2c 30 29 3b 0a 20 20 69  or.yy172,0);.  i
2323a 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2323b 79 31 37 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e  y174 ) yygotomin
2323c 6f 72 2e 79 79 31 37 34 2d 3e 61 5b 79 79 67 6f  or.yy174->a[yygo
2323d 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e 6e  tominor.yy174->n
2323e 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65  Expr-1].sortOrde
2323f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  r = yymsp[0].min
23240 6f 72 2e 79 79 34 36 3b 0a 7d 0a 20 20 20 20 20  or.yy46;.}.     
23241 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
23242 63 61 73 65 20 31 34 34 3a 0a 7b 0a 20 20 79 79  case 144:.{.  yy
23243 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20  gotominor.yy174 
23244 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
23245 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
23246 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
23247 2e 79 79 31 37 32 2c 30 29 3b 0a 20 20 69 66 28  .yy172,0);.  if(
23248 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
23249 37 34 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f  74 && yygotomino
2324a 72 2e 79 79 31 37 34 2d 3e 61 20 29 20 79 79 67  r.yy174->a ) yyg
2324b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e  otominor.yy174->
2324c 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d  a[0].sortOrder =
2324d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2324e 79 79 34 36 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy46;.}.        
2324f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
23250 65 20 31 34 36 3a 0a 20 20 20 20 20 20 63 61 73  e 146:.      cas
23251 65 20 31 34 38 3a 0a 7b 79 79 67 6f 74 6f 6d 69  e 148:.{yygotomi
23252 6e 6f 72 2e 79 79 34 36 20 3d 20 53 51 4c 49 54  nor.yy46 = SQLIT
23253 45 5f 53 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20  E_SO_ASC;}.     
23254 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
23255 63 61 73 65 20 31 34 37 3a 0a 7b 79 79 67 6f 74  case 147:.{yygot
23256 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 53 51  ominor.yy46 = SQ
23257 4c 49 54 45 5f 53 4f 5f 44 45 53 43 3b 7d 0a 20  LITE_SO_DESC;}. 
23258 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23259 20 20 20 20 63 61 73 65 20 31 35 33 3a 0a 7b 79      case 153:.{y
2325a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 33 34  ygotominor.yy234
2325b 2e 70 4c 69 6d 69 74 20 3d 20 30 3b 20 79 79 67  .pLimit = 0; yyg
2325c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 33 34 2e 70  otominor.yy234.p
2325d 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20  Offset = 0;}.   
2325e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2325f 20 20 63 61 73 65 20 31 35 34 3a 0a 7b 79 79 67    case 154:.{yyg
23260 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 33 34 2e 70  otominor.yy234.p
23261 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30 5d  Limit = yymsp[0]
23262 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b 20 79 79  .minor.yy172; yy
23263 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 33 34 2e  gotominor.yy234.
23264 70 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0a 20 20  pOffset = 0;}.  
23265 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23266 20 20 20 63 61 73 65 20 31 35 35 3a 0a 7b 79 79     case 155:.{yy
23267 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 33 34 2e  gotominor.yy234.
23268 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 2d  pLimit = yymsp[-
23269 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b 20  2].minor.yy172; 
2326a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 33  yygotominor.yy23
2326b 34 2e 70 4f 66 66 73 65 74 20 3d 20 79 79 6d 73  4.pOffset = yyms
2326c 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  p[0].minor.yy172
2326d 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2326e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 36  ;.      case 156
2326f 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  :.{yygotominor.y
23270 79 32 33 34 2e 70 4f 66 66 73 65 74 20 3d 20 79  y234.pOffset = y
23271 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
23272 79 31 37 32 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  y172; yygotomino
23273 72 2e 79 79 32 33 34 2e 70 4c 69 6d 69 74 20 3d  r.yy234.pLimit =
23274 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
23275 79 79 31 37 32 3b 7d 0a 20 20 20 20 20 20 20 20  yy172;}.        
23276 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
23277 65 20 31 35 37 3a 0a 7b 73 71 6c 69 74 65 33 44  e 157:.{sqlite3D
23278 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65  eleteFrom(pParse
23279 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2327a 2e 79 79 33 37 33 2c 79 79 6d 73 70 5b 30 5d 2e  .yy373,yymsp[0].
2327b 6d 69 6e 6f 72 2e 79 79 31 37 32 29 3b 7d 0a 20  minor.yy172);}. 
2327c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2327d 20 20 20 20 63 61 73 65 20 31 36 30 3a 0a 7b 0a      case 160:.{.
2327e 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2327f 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61  tCheckLength(pPa
23280 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  rse,yymsp[-1].mi
23281 6e 6f 72 2e 79 79 31 37 34 2c 53 51 4c 49 54 45  nor.yy174,SQLITE
23282 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 22 73 65 74  _MAX_COLUMN,"set
23283 20 6c 69 73 74 22 29 3b 20 0a 20 20 73 71 6c 69   list"); .  sqli
23284 74 65 33 55 70 64 61 74 65 28 70 50 61 72 73 65  te3Update(pParse
23285 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  ,yymsp[-3].minor
23286 2e 79 79 33 37 33 2c 79 79 6d 73 70 5b 2d 31 5d  .yy373,yymsp[-1]
23287 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d  .minor.yy174,yym
23288 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  sp[0].minor.yy17
23289 32 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  2,yymsp[-4].mino
2328a 72 2e 79 79 34 36 29 3b 0a 7d 0a 20 20 20 20 20  r.yy46);.}.     
2328b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2328c 63 61 73 65 20 31 36 31 3a 0a 7b 79 79 67 6f 74  case 161:.{yygot
2328d 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73  ominor.yy174 = s
2328e 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2328f 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
23290 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-4].minor.yy17
23291 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  4,yymsp[0].minor
23292 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d 32  .yy172,&yymsp[-2
23293 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d  ].minor.yy410);}
23294 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23295 20 20 20 20 20 20 63 61 73 65 20 31 36 32 3a 0a        case 162:.
23296 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
23297 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  74 = sqlite3Expr
23298 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
23299 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e,0,yymsp[0].min
2329a 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b  or.yy172,&yymsp[
2329b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29  -2].minor.yy410)
2329c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2329d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 33  ;.      case 163
2329e 3a 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74  :.{sqlite3Insert
2329f 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
232a0 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 20  5].minor.yy373, 
232a1 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
232a2 79 79 31 37 34 2c 20 30 2c 20 79 79 6d 73 70 5b  yy174, 0, yymsp[
232a3 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 32 2c  -4].minor.yy432,
232a4 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72   yymsp[-7].minor
232a5 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20  .yy46);}.       
232a6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
232a7 73 65 20 31 36 34 3a 0a 7b 73 71 6c 69 74 65 33  se 164:.{sqlite3
232a8 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79  Insert(pParse, y
232a9 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
232aa 79 33 37 33 2c 20 30 2c 20 79 79 6d 73 70 5b 30  y373, 0, yymsp[0
232ab 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 2c 20 79  ].minor.yy219, y
232ac 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
232ad 79 34 33 32 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  y432, yymsp[-4].
232ae 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a 20 20  minor.yy46);}.  
232af 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
232b0 20 20 20 63 61 73 65 20 31 36 35 3a 0a 7b 73 71     case 165:.{sq
232b1 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72  lite3Insert(pPar
232b2 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  se, yymsp[-3].mi
232b3 6e 6f 72 2e 79 79 33 37 33 2c 20 30 2c 20 30 2c  nor.yy373, 0, 0,
232b4 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
232b5 2e 79 79 34 33 32 2c 20 79 79 6d 73 70 5b 2d 35  .yy432, yymsp[-5
232b6 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a  ].minor.yy46);}.
232b7 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
232b8 20 20 20 20 20 63 61 73 65 20 31 36 38 3a 0a 20       case 168:. 
232b9 20 20 20 20 20 63 61 73 65 20 32 33 31 3a 0a 7b       case 231:.{
232ba 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
232bb 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  4 = sqlite3ExprL
232bc 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
232bd 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
232be 2e 79 79 31 37 34 2c 79 79 6d 73 70 5b 30 5d 2e  .yy174,yymsp[0].
232bf 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 30 29 3b 7d  minor.yy172,0);}
232c0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
232c1 20 20 20 20 20 20 63 61 73 65 20 31 36 39 3a 0a        case 169:.
232c2 20 20 20 20 20 20 63 61 73 65 20 32 33 32 3a 0a        case 232:.
232c3 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
232c4 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  74 = sqlite3Expr
232c5 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
232c6 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e,0,yymsp[0].min
232c7 6f 72 2e 79 79 31 37 32 2c 30 29 3b 7d 0a 20 20  or.yy172,0);}.  
232c8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
232c9 20 20 20 63 61 73 65 20 31 37 32 3a 0a 7b 79 79     case 172:.{yy
232ca 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 33 32 20  gotominor.yy432 
232cb 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41  = sqlite3IdListA
232cc 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
232cd 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
232ce 2e 79 79 34 33 32 2c 26 79 79 6d 73 70 5b 30 5d  .yy432,&yymsp[0]
232cf 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d 0a  .minor.yy410);}.
232d0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
232d1 20 20 20 20 20 63 61 73 65 20 31 37 33 3a 0a 7b       case 173:.{
232d2 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 33  yygotominor.yy43
232d3 32 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  2 = sqlite3IdLis
232d4 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
232d5 64 62 2c 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  db,0,&yymsp[0].m
232d6 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d 0a 20 20  inor.yy410);}.  
232d7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
232d8 20 20 20 63 61 73 65 20 31 37 35 3a 0a 7b 79 79     case 175:.{yy
232d9 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
232da 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
232db 72 2e 79 79 31 37 32 3b 20 73 71 6c 69 74 65 33  r.yy172; sqlite3
232dc 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
232dd 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d 73  inor.yy172,&yyms
232de 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
232df 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
232e0 79 79 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  yy0); }.        
232e1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
232e2 65 20 31 37 36 3a 0a 20 20 20 20 20 20 63 61 73  e 176:.      cas
232e3 65 20 31 38 31 3a 0a 20 20 20 20 20 20 63 61 73  e 181:.      cas
232e4 65 20 31 38 32 3a 0a 7b 79 79 67 6f 74 6f 6d 69  e 182:.{yygotomi
232e5 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69  nor.yy172 = sqli
232e6 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
232e7 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c   yymsp[0].major,
232e8 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d   0, 0, &yymsp[0]
232e9 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
232ea 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
232eb 20 20 20 63 61 73 65 20 31 37 37 3a 0a 20 20 20     case 177:.   
232ec 20 20 20 63 61 73 65 20 31 37 38 3a 0a 7b 79 79     case 178:.{yy
232ed 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
232ee 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
232ef 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
232f0 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69   0, &yymsp[0].mi
232f1 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
232f2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
232f3 63 61 73 65 20 31 37 39 3a 0a 7b 0a 20 20 45 78  case 179:.{.  Ex
232f4 70 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69  pr *temp1 = sqli
232f5 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
232f6 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79   TK_ID, 0, 0, &y
232f7 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
232f8 79 34 31 30 29 3b 0a 20 20 45 78 70 72 20 2a 74  y410);.  Expr *t
232f9 65 6d 70 32 20 3d 20 73 71 6c 69 74 65 33 50 45  emp2 = sqlite3PE
232fa 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
232fb 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  D, 0, 0, &yymsp[
232fc 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b  0].minor.yy410);
232fd 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
232fe 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45  y172 = sqlite3PE
232ff 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
23300 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d 70 32  OT, temp1, temp2
23301 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  , 0);.}.        
23302 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
23303 65 20 31 38 30 3a 0a 7b 0a 20 20 45 78 70 72 20  e 180:.{.  Expr 
23304 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74 65 33  *temp1 = sqlite3
23305 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
23306 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
23307 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-4].minor.yy41
23308 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  0);.  Expr *temp
23309 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2330a 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
2330b 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
2330c 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20  .minor.yy410);. 
2330d 20 45 78 70 72 20 2a 74 65 6d 70 33 20 3d 20 73   Expr *temp3 = s
2330e 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2330f 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
23310 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
23311 2e 79 79 34 31 30 29 3b 0a 20 20 45 78 70 72 20  .yy410);.  Expr 
23312 2a 74 65 6d 70 34 20 3d 20 73 71 6c 69 74 65 33  *temp4 = sqlite3
23313 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
23314 5f 44 4f 54 2c 20 74 65 6d 70 32 2c 20 74 65 6d  _DOT, temp2, tem
23315 70 33 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f  p3, 0);.  yygoto
23316 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
23317 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
23318 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31  e, TK_DOT, temp1
23319 2c 20 74 65 6d 70 34 2c 20 30 29 3b 0a 7d 0a 20  , temp4, 0);.}. 
2331a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2331b 20 20 20 20 63 61 73 65 20 31 38 33 3a 0a 7b 79      case 183:.{y
2331c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
2331d 20 3d 20 73 71 6c 69 74 65 33 52 65 67 69 73 74   = sqlite3Regist
2331e 65 72 45 78 70 72 28 70 50 61 72 73 65 2c 20 26  erExpr(pParse, &
2331f 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
23320 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
23321 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
23322 31 38 34 3a 0a 7b 0a 20 20 54 6f 6b 65 6e 20 2a  184:.{.  Token *
23323 70 54 6f 6b 65 6e 20 3d 20 26 79 79 6d 73 70 5b  pToken = &yymsp[
23324 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20  0].minor.yy0;.  
23325 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 79 79  Expr *pExpr = yy
23326 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
23327 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
23328 50 61 72 73 65 2c 20 54 4b 5f 56 41 52 49 41 42  Parse, TK_VARIAB
23329 4c 45 2c 20 30 2c 20 30 2c 20 70 54 6f 6b 65 6e  LE, 0, 0, pToken
2332a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2332b 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28  AssignVarNumber(
2332c 70 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a  pParse, pExpr);.
2332d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2332e 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 35 3a  .      case 185:
2332f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
23330 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
23331 45 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72  ExprSetColl(pPar
23332 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se, yymsp[-2].mi
23333 6e 6f 72 2e 79 79 31 37 32 2c 20 26 79 79 6d 73  nor.yy172, &yyms
23334 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  p[0].minor.yy410
23335 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
23336 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
23337 38 36 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69  86:.{.  yygotomi
23338 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69  nor.yy172 = sqli
23339 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2333a 20 54 4b 5f 43 41 53 54 2c 20 79 79 6d 73 70 5b   TK_CAST, yymsp[
2333b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  -3].minor.yy172,
2333c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d   0, &yymsp[-1].m
2333d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 73  inor.yy410);.  s
2333e 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
2333f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
23340 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  ,&yymsp[-5].mino
23341 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
23342 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
23343 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23344 20 20 20 63 61 73 65 20 31 38 37 3a 0a 7b 0a 20     case 187:.{. 
23345 20 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d   if( yymsp[-1].m
23346 69 6e 6f 72 2e 79 79 31 37 34 20 26 26 20 79 79  inor.yy174 && yy
23347 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
23348 31 37 34 2d 3e 6e 45 78 70 72 3e 53 51 4c 49 54  174->nExpr>SQLIT
23349 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41  E_MAX_FUNCTION_A
2334a 52 47 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  RG ){.    sqlite
2334b 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2334c 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 61 72 67 75  , "too many argu
2334d 6d 65 6e 74 73 20 6f 6e 20 66 75 6e 63 74 69 6f  ments on functio
2334e 6e 20 25 54 22 2c 20 26 79 79 6d 73 70 5b 2d 34  n %T", &yymsp[-4
2334f 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
23350 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
23351 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33 45  yy172 = sqlite3E
23352 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  xprFunction(pPar
23353 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se, yymsp[-1].mi
23354 6e 6f 72 2e 79 79 31 37 34 2c 20 26 79 79 6d 73  nor.yy174, &yyms
23355 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-4].minor.yy0)
23356 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
23357 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
23358 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d 34 5d  yy172,&yymsp[-4]
23359 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2335a 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2335b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d  .  if( yymsp[-2]
2335c 2e 6d 69 6e 6f 72 2e 79 79 34 36 20 26 26 20 79  .minor.yy46 && y
2335d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
2335e 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69   ){.    yygotomi
2335f 6e 6f 72 2e 79 79 31 37 32 2d 3e 66 6c 61 67 73  nor.yy172->flags
23360 20 7c 3d 20 45 50 5f 44 69 73 74 69 6e 63 74 3b   |= EP_Distinct;
23361 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62  .  }.}.        b
23362 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
23363 20 31 38 38 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f   188:.{.  yygoto
23364 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
23365 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f  lite3ExprFunctio
23366 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 26 79 79  n(pParse, 0, &yy
23367 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
23368 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
23369 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2336a 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d  r.yy172,&yymsp[-
2336b 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  3].minor.yy0,&yy
2336c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2336d 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2336e 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2336f 38 39 3a 0a 7b 0a 20 20 2f 2a 20 54 68 65 20 43  89:.{.  /* The C
23370 55 52 52 45 4e 54 5f 54 49 4d 45 2c 20 43 55 52  URRENT_TIME, CUR
23371 52 45 4e 54 5f 44 41 54 45 2c 20 61 6e 64 20 43  RENT_DATE, and C
23372 55 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50  URRENT_TIMESTAMP
23373 20 76 61 6c 75 65 73 20 61 72 65 0a 20 20 2a 2a   values are.  **
23374 20 74 72 65 61 74 65 64 20 61 73 20 66 75 6e 63   treated as func
23375 74 69 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72  tions that retur
23376 6e 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20  n constants */. 
23377 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
23378 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  72 = sqlite3Expr
23379 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c  Function(pParse,
2337a 20 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   0,&yymsp[0].min
2337b 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79  or.yy0);.  if( y
2337c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
2337d 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69   ){.    yygotomi
2337e 6e 6f 72 2e 79 79 31 37 32 2d 3e 6f 70 20 3d 20  nor.yy172->op = 
2337f 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3b 20 20  TK_CONST_FUNC;  
23380 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
23381 2e 79 79 31 37 32 2d 3e 73 70 61 6e 20 3d 20 79  .yy172->span = y
23382 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
23383 30 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20  0;.  }.}.       
23384 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
23385 73 65 20 31 39 30 3a 0a 20 20 20 20 20 20 63 61  se 190:.      ca
23386 73 65 20 31 39 31 3a 0a 20 20 20 20 20 20 63 61  se 191:.      ca
23387 73 65 20 31 39 32 3a 0a 20 20 20 20 20 20 63 61  se 192:.      ca
23388 73 65 20 31 39 33 3a 0a 20 20 20 20 20 20 63 61  se 193:.      ca
23389 73 65 20 31 39 34 3a 0a 20 20 20 20 20 20 63 61  se 194:.      ca
2338a 73 65 20 31 39 35 3a 0a 20 20 20 20 20 20 63 61  se 195:.      ca
2338b 73 65 20 31 39 36 3a 0a 20 20 20 20 20 20 63 61  se 196:.      ca
2338c 73 65 20 31 39 37 3a 0a 7b 79 79 67 6f 74 6f 6d  se 197:.{yygotom
2338d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
2338e 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2338f 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72  ,yymsp[-1].major
23390 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
23391 2e 79 79 31 37 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy172,yymsp[0].
23392 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 30 29 3b 7d  minor.yy172,0);}
23393 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23394 20 20 20 20 20 20 63 61 73 65 20 31 39 38 3a 0a        case 198:.
23395 20 20 20 20 20 20 63 61 73 65 20 32 30 30 3a 0a        case 200:.
23396 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  {yygotominor.yy7
23397 32 2e 65 4f 70 65 72 61 74 6f 72 20 3d 20 79 79  2.eOperator = yy
23398 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
23399 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
2339a 37 32 2e 6e 6f 74 20 3d 20 30 3b 7d 0a 20 20 20  72.not = 0;}.   
2339b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2339c 20 20 63 61 73 65 20 31 39 39 3a 0a 20 20 20 20    case 199:.    
2339d 20 20 63 61 73 65 20 32 30 31 3a 0a 7b 79 79 67    case 201:.{yyg
2339e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2e 65 4f  otominor.yy72.eO
2339f 70 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b  perator = yymsp[
233a0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79  0].minor.yy0; yy
233a1 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2e 6e  gotominor.yy72.n
233a2 6f 74 20 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20  ot = 1;}.       
233a3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
233a4 73 65 20 32 30 34 3a 0a 7b 0a 20 20 45 78 70 72  se 204:.{.  Expr
233a5 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 20 20 70  List *pList;.  p
233a6 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
233a7 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
233a8 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 31 5d  rse,0, yymsp[-1]
233a9 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 29  .minor.yy172, 0)
233aa 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69  ;.  pList = sqli
233ab 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
233ac 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20  d(pParse,pList, 
233ad 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
233ae 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 69 66 28  yy172, 0);.  if(
233af 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
233b0 79 79 31 37 32 20 29 7b 0a 20 20 20 20 70 4c 69  yy172 ){.    pLi
233b1 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
233b2 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
233b3 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30  e,pList, yymsp[0
233b4 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30  ].minor.yy172, 0
233b5 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
233b6 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
233b7 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
233b8 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20  (pParse, pList, 
233b9 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
233ba 2e 79 79 37 32 2e 65 4f 70 65 72 61 74 6f 72 29  .yy72.eOperator)
233bb 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32  ;.  if( yymsp[-2
233bc 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2e 6e 6f 74  ].minor.yy72.not
233bd 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
233be 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45  y172 = sqlite3PE
233bf 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
233c0 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  OT, yygotominor.
233c1 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy172, 0, 0);.  
233c2 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
233c3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
233c4 32 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  2, &yymsp[-3].mi
233c5 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c  nor.yy172->span,
233c6 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f   &yymsp[-1].mino
233c7 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 29 3b 0a  r.yy172->span);.
233c8 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
233c9 72 2e 79 79 31 37 32 20 29 20 79 79 67 6f 74 6f  r.yy172 ) yygoto
233ca 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 66 6c 61  minor.yy172->fla
233cb 67 73 20 7c 3d 20 45 50 5f 49 6e 66 69 78 46 75  gs |= EP_InfixFu
233cc 6e 63 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  nc;.}.        br
233cd 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
233ce 32 30 35 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  205:.{.  yygotom
233cf 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
233d0 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
233d1 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72  , yymsp[0].major
233d2 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
233d3 72 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a  r.yy172, 0, 0);.
233d4 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
233d5 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
233d6 31 37 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  172,&yymsp[-1].m
233d7 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e  inor.yy172->span
233d8 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
233d9 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
233da 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
233db 73 65 20 32 30 36 3a 0a 7b 0a 20 20 79 79 67 6f  se 206:.{.  yygo
233dc 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20  tominor.yy172 = 
233dd 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
233de 72 73 65 2c 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20  rse, TK_ISNULL, 
233df 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
233e0 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy172, 0, 0);.  
233e1 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
233e2 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
233e3 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  2,&yymsp[-2].min
233e4 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c 26  or.yy172->span,&
233e5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
233e6 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
233e7 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
233e8 20 32 30 37 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f   207:.{.  yygoto
233e9 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
233ea 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
233eb 65 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 79  e, TK_NOTNULL, y
233ec 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
233ed 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73  y172, 0, 0);.  s
233ee 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
233ef 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
233f0 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
233f1 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c 26 79  r.yy172->span,&y
233f2 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
233f3 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
233f4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
233f5 32 30 38 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  208:.{.  yygotom
233f6 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
233f7 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
233f8 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 79 79  , TK_NOTNULL, yy
233f9 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
233fa 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71  172, 0, 0);.  sq
233fb 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79  lite3ExprSpan(yy
233fc 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  gotominor.yy172,
233fd 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
233fe 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c 26 79 79  .yy172->span,&yy
233ff 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
23400 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
23401 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
23402 30 39 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69  09:.{.  yygotomi
23403 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69  nor.yy172 = sqli
23404 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
23405 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72   yymsp[-1].major
23406 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
23407 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20  .yy172, 0, 0);. 
23408 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
23409 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  (yygotominor.yy1
2340a 37 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  72,&yymsp[-1].mi
2340b 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
2340c 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73  ].minor.yy172->s
2340d 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  pan);.}.        
2340e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2340f 65 20 32 31 30 3a 0a 7b 0a 20 20 79 79 67 6f 74  e 210:.{.  yygot
23410 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
23411 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
23412 73 65 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c 20 79  se, TK_UMINUS, y
23413 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
23414 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71  172, 0, 0);.  sq
23415 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79  lite3ExprSpan(yy
23416 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  gotominor.yy172,
23417 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
23418 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
23419 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e  inor.yy172->span
2341a 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2341b 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2341c 31 31 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69  11:.{.  yygotomi
2341d 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69  nor.yy172 = sqli
2341e 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2341f 20 54 4b 5f 55 50 4c 55 53 2c 20 79 79 6d 73 70   TK_UPLUS, yymsp
23420 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  [0].minor.yy172,
23421 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
23422 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
23423 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d  minor.yy172,&yym
23424 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
23425 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
23426 2e 79 79 31 37 32 2d 3e 73 70 61 6e 29 3b 0a 7d  .yy172->span);.}
23427 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23428 20 20 20 20 20 20 63 61 73 65 20 32 31 34 3a 0a        case 214:.
23429 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  {.  ExprList *pL
2342a 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
2342b 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2342c 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  se,0, yymsp[-2].
2342d 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 29 3b  minor.yy172, 0);
2342e 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  .  pList = sqlit
2342f 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
23430 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79  (pParse,pList, y
23431 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
23432 31 37 32 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74  172, 0);.  yygot
23433 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
23434 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
23435 73 65 2c 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20  se, TK_BETWEEN, 
23436 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
23437 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy172, 0, 0);.  
23438 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
23439 79 79 31 37 32 20 29 7b 0a 20 20 20 20 79 79 67  yy172 ){.    yyg
2343a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  otominor.yy172->
2343b 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  pList = pList;. 
2343c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2343d 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2343e 65 28 70 4c 69 73 74 29 3b 0a 20 20 7d 20 0a 20  e(pList);.  } . 
2343f 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d   if( yymsp[-3].m
23440 69 6e 6f 72 2e 79 79 34 36 20 29 20 79 79 67 6f  inor.yy46 ) yygo
23441 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20  tominor.yy172 = 
23442 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
23443 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
23444 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20  otominor.yy172, 
23445 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0);.  sqlite3
23446 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
23447 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d 73  inor.yy172,&yyms
23448 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-4].minor.yy17
23449 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30  2->span,&yymsp[0
2344a 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73  ].minor.yy172->s
2344b 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  pan);.}.        
2344c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2344d 65 20 32 31 37 3a 0a 7b 0a 20 20 20 20 79 79 67  e 217:.{.    yyg
2344e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d  otominor.yy172 =
2344f 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
23450 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d  arse, TK_IN, yym
23451 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
23452 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69  72, 0, 0);.    i
23453 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
23454 79 31 37 32 20 29 7b 0a 20 20 20 20 20 20 79 79  y172 ){.      yy
23455 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d  gotominor.yy172-
23456 3e 70 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d  >pList = yymsp[-
23457 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 3b 0a  1].minor.yy174;.
23458 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
23459 72 53 65 74 48 65 69 67 68 74 28 79 79 67 6f 74  rSetHeight(yygot
2345a 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 29 3b 0a 20  ominor.yy172);. 
2345b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2345c 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2345d 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d 31 5d 2e  elete(yymsp[-1].
2345e 6d 69 6e 6f 72 2e 79 79 31 37 34 29 3b 0a 20 20  minor.yy174);.  
2345f 20 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73    }.    if( yyms
23460 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  p[-3].minor.yy46
23461 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
23462 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45  y172 = sqlite3PE
23463 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
23464 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  OT, yygotominor.
23465 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy172, 0, 0);.  
23466 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
23467 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
23468 31 37 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  172,&yymsp[-4].m
23469 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e  inor.yy172->span
2346a 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2346b 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20  .yy0);.  }.     
2346c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2346d 63 61 73 65 20 32 31 38 3a 0a 7b 0a 20 20 20 20  case 218:.{.    
2346e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
2346f 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
23470 28 70 50 61 72 73 65 2c 20 54 4b 5f 53 45 4c 45  (pParse, TK_SELE
23471 43 54 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  CT, 0, 0, 0);.  
23472 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
23473 72 2e 79 79 31 37 32 20 29 7b 0a 20 20 20 20 20  r.yy172 ){.     
23474 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
23475 37 32 2d 3e 70 53 65 6c 65 63 74 20 3d 20 79 79  72->pSelect = yy
23476 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
23477 32 31 39 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  219;.      sqlit
23478 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
23479 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
2347a 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  2);.    }else{. 
2347b 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
2347c 63 74 44 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d  ctDelete(yymsp[-
2347d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 29 3b  1].minor.yy219);
2347e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2347f 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
23480 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79  ominor.yy172,&yy
23481 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
23482 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
23483 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20  r.yy0);.  }.    
23484 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
23485 20 63 61 73 65 20 32 31 39 3a 0a 7b 0a 20 20 20   case 219:.{.   
23486 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
23487 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
23488 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c  r(pParse, TK_IN,
23489 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
2348a 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20  .yy172, 0, 0);. 
2348b 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e     if( yygotomin
2348c 6f 72 2e 79 79 31 37 32 20 29 7b 0a 20 20 20 20  or.yy172 ){.    
2348d 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2348e 31 37 32 2d 3e 70 53 65 6c 65 63 74 20 3d 20 79  172->pSelect = y
2348f 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
23490 79 32 31 39 3b 0a 20 20 20 20 20 20 73 71 6c 69  y219;.      sqli
23491 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
23492 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  (yygotominor.yy1
23493 37 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  72);.    }else{.
23494 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
23495 65 63 74 44 65 6c 65 74 65 28 79 79 6d 73 70 5b  ectDelete(yymsp[
23496 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 29  -1].minor.yy219)
23497 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
23498 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
23499 79 79 34 36 20 29 20 79 79 67 6f 74 6f 6d 69 6e  yy46 ) yygotomin
2349a 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74  or.yy172 = sqlit
2349b 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2349c 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69  TK_NOT, yygotomi
2349d 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20 30 29  nor.yy172, 0, 0)
2349e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2349f 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
234a0 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d  r.yy172,&yymsp[-
234a1 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  4].minor.yy172->
234a2 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  span,&yymsp[0].m
234a3 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20  inor.yy0);.  }. 
234a4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
234a5 20 20 20 20 63 61 73 65 20 32 32 30 3a 0a 7b 0a      case 220:.{.
234a6 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72      SrcList *pSr
234a7 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  c = sqlite3SrcLi
234a8 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d  stAppend(pParse-
234a9 3e 64 62 2c 20 30 2c 26 79 79 6d 73 70 5b 2d 31  >db, 0,&yymsp[-1
234aa 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79  ].minor.yy410,&y
234ab 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
234ac 34 31 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f  410);.    yygoto
234ad 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
234ae 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
234af 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b  e, TK_IN, yymsp[
234b0 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  -3].minor.yy172,
234b1 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   0, 0);.    if( 
234b2 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
234b3 32 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74  2 ){.      yygot
234b4 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 70 53  ominor.yy172->pS
234b5 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
234b6 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
234b7 20 30 2c 70 53 72 63 2c 30 2c 30 2c 30 2c 30 2c   0,pSrc,0,0,0,0,
234b8 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 73 71  0,0,0);.      sq
234b9 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
234ba 68 74 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ht(yygotominor.y
234bb 79 31 37 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65  y172);.    }else
234bc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
234bd 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 53 72  rcListDelete(pSr
234be 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  c);.    }.    if
234bf 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ( yymsp[-2].mino
234c0 72 2e 79 79 34 36 20 29 20 79 79 67 6f 74 6f 6d  r.yy46 ) yygotom
234c1 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
234c2 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
234c3 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f  , TK_NOT, yygoto
234c4 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20  minor.yy172, 0, 
234c5 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  0);.    sqlite3E
234c6 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
234c7 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70  nor.yy172,&yymsp
234c8 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-3].minor.yy172
234c9 2d 3e 73 70 61 6e 2c 79 79 6d 73 70 5b 30 5d 2e  ->span,yymsp[0].
234ca 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3f 26 79  minor.yy410.z?&y
234cb 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
234cc 34 31 30 3a 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  410:&yymsp[-1].m
234cd 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 7d  inor.yy410);.  }
234ce 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
234cf 20 20 20 20 20 20 63 61 73 65 20 32 32 31 3a 0a        case 221:.
234d0 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20  {.    Expr *p = 
234d1 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
234d2 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
234d3 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 58 49 53  (pParse, TK_EXIS
234d4 54 53 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  TS, 0, 0, 0);.  
234d5 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
234d6 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 79 79   p->pSelect = yy
234d7 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
234d8 32 31 39 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  219;.      sqlit
234d9 65 33 45 78 70 72 53 70 61 6e 28 70 2c 26 79 79  e3ExprSpan(p,&yy
234da 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
234db 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
234dc 72 2e 79 79 30 29 3b 0a 20 20 20 20 20 20 73 71  r.yy0);.      sq
234dd 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
234de 68 74 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ht(yygotominor.y
234df 79 31 37 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65  y172);.    }else
234e0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
234e1 65 6c 65 63 74 44 65 6c 65 74 65 28 79 79 6d 73  electDelete(yyms
234e2 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31  p[-1].minor.yy21
234e3 39 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  9);.    }.  }.  
234e4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
234e5 20 20 20 63 61 73 65 20 32 32 32 3a 0a 7b 0a 20     case 222:.{. 
234e6 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
234e7 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
234e8 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 41 53  r(pParse, TK_CAS
234e9 45 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  E, yymsp[-3].min
234ea 6f 72 2e 79 79 31 37 32 2c 20 79 79 6d 73 70 5b  or.yy172, yymsp[
234eb 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  -1].minor.yy172,
234ec 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74   0);.  if( yygot
234ed 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 29 7b 0a  ominor.yy172 ){.
234ee 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
234ef 79 79 31 37 32 2d 3e 70 4c 69 73 74 20 3d 20 79  yy172->pList = y
234f0 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
234f1 79 31 37 34 3b 0a 20 20 20 20 73 71 6c 69 74 65  y174;.    sqlite
234f2 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 79  3ExprSetHeight(y
234f3 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
234f4 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
234f5 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
234f6 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d 32 5d 2e  elete(yymsp[-2].
234f7 6d 69 6e 6f 72 2e 79 79 31 37 34 29 3b 0a 20 20  minor.yy174);.  
234f8 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  }.  sqlite3ExprS
234f9 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
234fa 79 79 31 37 32 2c 20 26 79 79 6d 73 70 5b 2d 34  yy172, &yymsp[-4
234fb 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
234fc 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
234fd 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
234fe 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
234ff 32 33 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69  23:.{.  yygotomi
23500 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71 6c 69  nor.yy174 = sqli
23501 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
23502 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
23503 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20  4].minor.yy174, 
23504 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
23505 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 79 79 67  yy172, 0);.  yyg
23506 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d  otominor.yy174 =
23507 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
23508 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
23509 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2c  gotominor.yy174,
2350a 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2350b 79 79 31 37 32 2c 20 30 29 3b 0a 7d 0a 20 20 20  yy172, 0);.}.   
2350c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2350d 20 20 63 61 73 65 20 32 32 34 3a 0a 7b 0a 20 20    case 224:.{.  
2350e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
2350f 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  4 = sqlite3ExprL
23510 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
23511 2c 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  ,0, yymsp[-2].mi
23512 6e 6f 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 20  nor.yy172, 0);. 
23513 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
23514 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  74 = sqlite3Expr
23515 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
23516 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  e,yygotominor.yy
23517 31 37 34 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  174, yymsp[0].mi
23518 6e 6f 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 7d  nor.yy172, 0);.}
23519 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2351a 20 20 20 20 20 20 63 61 73 65 20 32 33 33 3a 0a        case 233:.
2351b 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74  {.  sqlite3Creat
2351c 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 26  eIndex(pParse, &
2351d 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
2351e 79 79 34 31 30 2c 20 26 79 79 6d 73 70 5b 2d 35  yy410, &yymsp[-5
2351f 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 0a  ].minor.yy410, .
23520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23521 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
23522 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
23523 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70 5b 2d 33  ->db,0,&yymsp[-3
23524 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 30 29  ].minor.yy410,0)
23525 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
23526 72 2e 79 79 31 37 34 2c 20 79 79 6d 73 70 5b 2d  r.yy174, yymsp[-
23527 39 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 2c 0a 20  9].minor.yy46,. 
23528 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23529 20 20 20 20 20 26 79 79 6d 73 70 5b 2d 31 30 5d       &yymsp[-10]
2352a 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
2352b 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
2352c 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 2c 20   SQLITE_SO_ASC, 
2352d 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e  yymsp[-7].minor.
2352e 79 79 34 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20  yy46);.}.       
2352f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
23530 73 65 20 32 33 34 3a 0a 20 20 20 20 20 20 63 61  se 234:.      ca
23531 73 65 20 32 38 31 3a 0a 7b 79 79 67 6f 74 6f 6d  se 281:.{yygotom
23532 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 41  inor.yy46 = OE_A
23533 62 6f 72 74 3b 7d 0a 20 20 20 20 20 20 20 20 62  bort;}.        b
23534 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
23535 20 32 33 35 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e   235:.{yygotomin
23536 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 4e 6f 6e  or.yy46 = OE_Non
23537 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  e;}.        brea
23538 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  k;.      case 23
23539 38 3a 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d  8:.{.  Expr *p =
2353a 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b   0;.  if( yymsp[
2353b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e  -1].minor.yy410.
2353c 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20 73  n>0 ){.    p = s
2353d 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2353e 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30  se, TK_COLUMN, 0
2353f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 0, 0);.    if(
23540 20 70 20 29 20 70 2d 3e 70 43 6f 6c 6c 20 3d 20   p ) p->pColl = 
23541 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
23542 6c 53 65 71 28 70 50 61 72 73 65 2c 20 28 63 68  lSeq(pParse, (ch
23543 61 72 2a 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  ar*)yymsp[-1].mi
23544 6e 6f 72 2e 79 79 34 31 30 2e 7a 2c 20 79 79 6d  nor.yy410.z, yym
23545 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
23546 31 30 2e 6e 29 3b 0a 20 20 7d 0a 20 20 79 79 67  10.n);.  }.  yyg
23547 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d  otominor.yy174 =
23548 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
23549 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
2354a 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2354b 31 37 34 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d  174, p, &yymsp[-
2354c 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b  2].minor.yy410);
2354d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
2354e 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50  stCheckLength(pP
2354f 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
23550 72 2e 79 79 31 37 34 2c 20 53 51 4c 49 54 45 5f  r.yy174, SQLITE_
23551 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 20 22 69 6e 64  MAX_COLUMN, "ind
23552 65 78 22 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  ex");.  if( yygo
23553 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 29 20  tominor.yy174 ) 
23554 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
23555 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72  4->a[yygotominor
23556 2e 79 79 31 37 34 2d 3e 6e 45 78 70 72 2d 31 5d  .yy174->nExpr-1]
23557 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 79 79 6d  .sortOrder = yym
23558 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  sp[0].minor.yy46
23559 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2355a 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  k;.      case 23
2355b 39 3a 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d  9:.{.  Expr *p =
2355c 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b   0;.  if( yymsp[
2355d 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e  -1].minor.yy410.
2355e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20 73  n>0 ){.    p = s
2355f 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
23560 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30  se, TK_COLUMN, 0
23561 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 0, 0);.    if(
23562 20 70 20 29 20 70 2d 3e 70 43 6f 6c 6c 20 3d 20   p ) p->pColl = 
23563 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
23564 6c 53 65 71 28 70 50 61 72 73 65 2c 20 28 63 68  lSeq(pParse, (ch
23565 61 72 2a 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  ar*)yymsp[-1].mi
23566 6e 6f 72 2e 79 79 34 31 30 2e 7a 2c 20 79 79 6d  nor.yy410.z, yym
23567 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
23568 31 30 2e 6e 29 3b 0a 20 20 7d 0a 20 20 79 79 67  10.n);.  }.  yyg
23569 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d  otominor.yy174 =
2356a 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2356b 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
2356c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   p, &yymsp[-2].m
2356d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 73  inor.yy410);.  s
2356e 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68  qlite3ExprListCh
2356f 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65  eckLength(pParse
23570 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
23571 31 37 34 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  174, SQLITE_MAX_
23572 43 4f 4c 55 4d 4e 2c 20 22 69 6e 64 65 78 22 29  COLUMN, "index")
23573 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ;.  if( yygotomi
23574 6e 6f 72 2e 79 79 31 37 34 20 29 20 79 79 67 6f  nor.yy174 ) yygo
23575 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e 61  tominor.yy174->a
23576 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  [yygotominor.yy1
23577 37 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72  74->nExpr-1].sor
23578 74 4f 72 64 65 72 20 3d 20 79 79 6d 73 70 5b 30  tOrder = yymsp[0
23579 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 0a 7d 0a  ].minor.yy46;.}.
2357a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2357b 20 20 20 20 20 63 61 73 65 20 32 34 31 3a 0a 7b       case 241:.{
2357c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
2357d 30 2e 7a 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d  0.z = 0; yygotom
2357e 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 20 3d 20 30  inor.yy410.n = 0
2357f 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
23580 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 33  ;.      case 243
23581 3a 0a 7b 73 71 6c 69 74 65 33 44 72 6f 70 49 6e  :.{sqlite3DropIn
23582 64 65 78 28 70 50 61 72 73 65 2c 20 79 79 6d 73  dex(pParse, yyms
23583 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33  p[0].minor.yy373
23584 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
23585 72 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20  r.yy46);}.      
23586 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
23587 61 73 65 20 32 34 34 3a 0a 20 20 20 20 20 20 63  ase 244:.      c
23588 61 73 65 20 32 34 35 3a 0a 7b 73 71 6c 69 74 65  ase 245:.{sqlite
23589 33 56 61 63 75 75 6d 28 70 50 61 72 73 65 29 3b  3Vacuum(pParse);
2358a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2358b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 36 3a  .      case 246:
2358c 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28  .{sqlite3Pragma(
2358d 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33  pParse,&yymsp[-3
2358e 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79  ].minor.yy410,&y
2358f 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
23590 79 34 31 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  y410,&yymsp[0].m
23591 69 6e 6f 72 2e 79 79 34 31 30 2c 30 29 3b 7d 0a  inor.yy410,0);}.
23592 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
23593 20 20 20 20 20 63 61 73 65 20 32 34 37 3a 0a 7b       case 247:.{
23594 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50  sqlite3Pragma(pP
23595 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  arse,&yymsp[-3].
23596 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d  minor.yy410,&yym
23597 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-2].minor.yy4
23598 31 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  10,&yymsp[0].min
23599 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20 20 20  or.yy0,0);}.    
2359a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2359b 20 63 61 73 65 20 32 34 38 3a 0a 7b 0a 20 20 73   case 248:.{.  s
2359c 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61  qlite3Pragma(pPa
2359d 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  rse,&yymsp[-3].m
2359e 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d 73  inor.yy410,&yyms
2359f 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-2].minor.yy41
235a0 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
235a1 72 2e 79 79 34 31 30 2c 31 29 3b 0a 7d 0a 20 20  r.yy410,1);.}.  
235a2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
235a3 20 20 20 63 61 73 65 20 32 34 39 3a 0a 7b 73 71     case 249:.{sq
235a4 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72  lite3Pragma(pPar
235a5 73 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  se,&yymsp[-4].mi
235a6 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d 73 70  nor.yy410,&yymsp
235a7 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-3].minor.yy410
235a8 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
235a9 72 2e 79 79 34 31 30 2c 30 29 3b 7d 0a 20 20 20  r.yy410,0);}.   
235aa 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
235ab 20 20 63 61 73 65 20 32 35 30 3a 0a 7b 73 71 6c    case 250:.{sql
235ac 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73  ite3Pragma(pPars
235ad 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e,&yymsp[-1].min
235ae 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b  or.yy410,&yymsp[
235af 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 30  0].minor.yy410,0
235b0 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,0);}.        br
235b1 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
235b2 32 35 38 3a 0a 7b 0a 20 20 54 6f 6b 65 6e 20 61  258:.{.  Token a
235b3 6c 6c 3b 0a 20 20 61 6c 6c 2e 7a 20 3d 20 79 79  ll;.  all.z = yy
235b4 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
235b5 34 31 30 2e 7a 3b 0a 20 20 61 6c 6c 2e 6e 20 3d  410.z;.  all.n =
235b6 20 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   (yymsp[0].minor
235b7 2e 79 79 30 2e 7a 20 2d 20 79 79 6d 73 70 5b 2d  .yy0.z - yymsp[-
235b8 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a  3].minor.yy410.z
235b9 29 20 2b 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  ) + yymsp[0].min
235ba 6f 72 2e 79 79 30 2e 6e 3b 0a 20 20 73 71 6c 69  or.yy0.n;.  sqli
235bb 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72  te3FinishTrigger
235bc 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
235bd 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 2c 20  1].minor.yy243, 
235be 26 61 6c 6c 29 3b 0a 7d 0a 20 20 20 20 20 20 20  &all);.}.       
235bf 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
235c0 73 65 20 32 35 39 3a 0a 7b 0a 20 20 73 71 6c 69  se 259:.{.  sqli
235c1 74 65 33 42 65 67 69 6e 54 72 69 67 67 65 72 28  te3BeginTrigger(
235c2 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
235c3 37 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20  7].minor.yy410, 
235c4 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  &yymsp[-6].minor
235c5 2e 79 79 34 31 30 2c 20 79 79 6d 73 70 5b 2d 35  .yy410, yymsp[-5
235c6 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 2c 20 79 79  ].minor.yy46, yy
235c7 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
235c8 33 37 30 2e 61 2c 20 79 79 6d 73 70 5b 2d 34 5d  370.a, yymsp[-4]
235c9 2e 6d 69 6e 6f 72 2e 79 79 33 37 30 2e 62 2c 20  .minor.yy370.b, 
235ca 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
235cb 79 79 33 37 33 2c 20 79 79 6d 73 70 5b 30 5d 2e  yy373, yymsp[0].
235cc 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 79 79 6d  minor.yy172, yym
235cd 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79  sp[-10].minor.yy
235ce 34 36 2c 20 79 79 6d 73 70 5b 2d 38 5d 2e 6d 69  46, yymsp[-8].mi
235cf 6e 6f 72 2e 79 79 34 36 29 3b 0a 20 20 79 79 67  nor.yy46);.  yyg
235d0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 20 3d  otominor.yy410 =
235d1 20 28 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f   (yymsp[-6].mino
235d2 72 2e 79 79 34 31 30 2e 6e 3d 3d 30 3f 79 79 6d  r.yy410.n==0?yym
235d3 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-7].minor.yy4
235d4 31 30 3a 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  10:yymsp[-6].min
235d5 6f 72 2e 79 79 34 31 30 29 3b 0a 7d 0a 20 20 20  or.yy410);.}.   
235d6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
235d7 20 20 63 61 73 65 20 32 36 30 3a 0a 20 20 20 20    case 260:.    
235d8 20 20 63 61 73 65 20 32 36 33 3a 0a 7b 20 79 79    case 263:.{ yy
235d9 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d  gotominor.yy46 =
235da 20 54 4b 5f 42 45 46 4f 52 45 3b 20 7d 0a 20 20   TK_BEFORE; }.  
235db 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
235dc 20 20 20 63 61 73 65 20 32 36 31 3a 0a 7b 20 79     case 261:.{ y
235dd 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20  ygotominor.yy46 
235de 3d 20 54 4b 5f 41 46 54 45 52 3b 20 20 7d 0a 20  = TK_AFTER;  }. 
235df 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
235e0 20 20 20 20 63 61 73 65 20 32 36 32 3a 0a 7b 20      case 262:.{ 
235e1 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36  yygotominor.yy46
235e2 20 3d 20 54 4b 5f 49 4e 53 54 45 41 44 3b 7d 0a   = TK_INSTEAD;}.
235e3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
235e4 20 20 20 20 20 63 61 73 65 20 32 36 34 3a 0a 20       case 264:. 
235e5 20 20 20 20 20 63 61 73 65 20 32 36 35 3a 0a 7b       case 265:.{
235e6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37  yygotominor.yy37
235e7 30 2e 61 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  0.a = yymsp[0].m
235e8 61 6a 6f 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  ajor; yygotomino
235e9 72 2e 79 79 33 37 30 2e 62 20 3d 20 30 3b 7d 0a  r.yy370.b = 0;}.
235ea 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
235eb 20 20 20 20 20 63 61 73 65 20 32 36 36 3a 0a 7b       case 266:.{
235ec 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37  yygotominor.yy37
235ed 30 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b  0.a = TK_UPDATE;
235ee 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
235ef 37 30 2e 62 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  70.b = yymsp[0].
235f0 6d 69 6e 6f 72 2e 79 79 34 33 32 3b 7d 0a 20 20  minor.yy432;}.  
235f1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
235f2 20 20 20 63 61 73 65 20 32 36 39 3a 0a 7b 20 79     case 269:.{ y
235f3 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
235f4 20 3d 20 30 3b 20 7d 0a 20 20 20 20 20 20 20 20   = 0; }.        
235f5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
235f6 65 20 32 37 30 3a 0a 7b 20 79 79 67 6f 74 6f 6d  e 270:.{ yygotom
235f7 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 79 79 6d  inor.yy172 = yym
235f8 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  sp[0].minor.yy17
235f9 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  2; }.        bre
235fa 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
235fb 37 31 3a 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73  71:.{.  if( yyms
235fc 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  p[-2].minor.yy24
235fd 33 20 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b 2d  3 ){.    yymsp[-
235fe 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 2d 3e  2].minor.yy243->
235ff 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 79  pLast->pNext = y
23600 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
23601 79 32 34 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  y243;.  }else{. 
23602 20 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e     yymsp[-2].min
23603 6f 72 2e 79 79 32 34 33 20 3d 20 79 79 6d 73 70  or.yy243 = yymsp
23604 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  [-1].minor.yy243
23605 3b 0a 20 20 7d 0a 20 20 79 79 6d 73 70 5b 2d 32  ;.  }.  yymsp[-2
23606 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 2d 3e 70  ].minor.yy243->p
23607 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  Last = yymsp[-1]
23608 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 3b 0a 20 20  .minor.yy243;.  
23609 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34  yygotominor.yy24
2360a 33 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  3 = yymsp[-2].mi
2360b 6e 6f 72 2e 79 79 32 34 33 3b 0a 7d 0a 20 20 20  nor.yy243;.}.   
2360c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2360d 20 20 63 61 73 65 20 32 37 32 3a 0a 7b 20 79 79    case 272:.{ yy
2360e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 33 20  gotominor.yy243 
2360f 3d 20 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  = 0; }.        b
23610 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
23611 20 32 37 33 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69   273:.{ yygotomi
23612 6e 6f 72 2e 79 79 32 34 33 20 3d 20 73 71 6c 69  nor.yy243 = sqli
23613 74 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65  te3TriggerUpdate
23614 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
23615 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f   &yymsp[-3].mino
23616 72 2e 79 79 34 31 30 2c 20 79 79 6d 73 70 5b 2d  r.yy410, yymsp[-
23617 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20  1].minor.yy174, 
23618 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
23619 79 31 37 32 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  y172, yymsp[-4].
2361a 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 20 7d 0a 20  minor.yy46); }. 
2361b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2361c 20 20 20 20 63 61 73 65 20 32 37 34 3a 0a 7b 79      case 274:.{y
2361d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 33  ygotominor.yy243
2361e 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
2361f 72 49 6e 73 65 72 74 53 74 65 70 28 70 50 61 72  rInsertStep(pPar
23620 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d  se->db, &yymsp[-
23621 35 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20  5].minor.yy410, 
23622 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
23623 79 79 34 33 32 2c 20 79 79 6d 73 70 5b 2d 31 5d  yy432, yymsp[-1]
23624 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20 30 2c  .minor.yy174, 0,
23625 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72   yymsp[-7].minor
23626 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20  .yy46);}.       
23627 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
23628 73 65 20 32 37 35 3a 0a 7b 79 79 67 6f 74 6f 6d  se 275:.{yygotom
23629 69 6e 6f 72 2e 79 79 32 34 33 20 3d 20 73 71 6c  inor.yy243 = sql
2362a 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72  ite3TriggerInser
2362b 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62  tStep(pParse->db
2362c 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
2362d 6f 72 2e 79 79 34 31 30 2c 20 79 79 6d 73 70 5b  or.yy410, yymsp[
2362e 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 32 2c  -1].minor.yy432,
2362f 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   0, yymsp[0].min
23630 6f 72 2e 79 79 32 31 39 2c 20 79 79 6d 73 70 5b  or.yy219, yymsp[
23631 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b  -4].minor.yy46);
23632 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
23633 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 36 3a  .      case 276:
23634 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
23635 32 34 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69  243 = sqlite3Tri
23636 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 70  ggerDeleteStep(p
23637 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73  Parse->db, &yyms
23638 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-1].minor.yy41
23639 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0, yymsp[0].mino
2363a 72 2e 79 79 31 37 32 29 3b 7d 0a 20 20 20 20 20  r.yy172);}.     
2363b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2363c 63 61 73 65 20 32 37 37 3a 0a 7b 79 79 67 6f 74  case 277:.{yygot
2363d 6f 6d 69 6e 6f 72 2e 79 79 32 34 33 20 3d 20 73  ominor.yy243 = s
2363e 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c  qlite3TriggerSel
2363f 65 63 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e  ectStep(pParse->
23640 64 62 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  db, yymsp[0].min
23641 6f 72 2e 79 79 32 31 39 29 3b 20 7d 0a 20 20 20  or.yy219); }.   
23642 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23643 20 20 63 61 73 65 20 32 37 38 3a 0a 7b 0a 20 20    case 278:.{.  
23644 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
23645 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
23646 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41 49 53  (pParse, TK_RAIS
23647 45 2c 20 30 2c 20 30 2c 20 30 29 3b 20 0a 20 20  E, 0, 0, 0); .  
23648 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
23649 79 79 31 37 32 20 29 7b 0a 20 20 20 20 79 79 67  yy172 ){.    yyg
2364a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  otominor.yy172->
2364b 69 43 6f 6c 75 6d 6e 20 3d 20 4f 45 5f 49 67 6e  iColumn = OE_Ign
2364c 6f 72 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ore;.    sqlite3
2364d 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
2364e 69 6e 6f 72 2e 79 79 31 37 32 2c 20 26 79 79 6d  inor.yy172, &yym
2364f 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
23650 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
23651 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 7d 0a 20 20  r.yy0);.  }.}.  
23652 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23653 20 20 20 63 61 73 65 20 32 37 39 3a 0a 7b 0a 20     case 279:.{. 
23654 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
23655 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
23656 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41 49  r(pParse, TK_RAI
23657 53 45 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70  SE, 0, 0, &yymsp
23658 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-1].minor.yy410
23659 29 3b 20 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  ); .  if( yygoto
2365a 6d 69 6e 6f 72 2e 79 79 31 37 32 20 29 20 7b 0a  minor.yy172 ) {.
2365b 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
2365c 79 79 31 37 32 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  yy172->iColumn =
2365d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
2365e 2e 79 79 34 36 3b 0a 20 20 20 20 73 71 6c 69 74  .yy46;.    sqlit
2365f 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
23660 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 26 79  ominor.yy172, &y
23661 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
23662 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
23663 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 7d 0a  nor.yy0);.  }.}.
23664 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
23665 20 20 20 20 20 63 61 73 65 20 32 38 30 3a 0a 7b       case 280:.{
23666 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36  yygotominor.yy46
23667 20 3d 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3b 7d   = OE_Rollback;}
23668 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23669 20 20 20 20 20 20 63 61 73 65 20 32 38 32 3a 0a        case 282:.
2366a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
2366b 36 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20 20  6 = OE_Fail;}.  
2366c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2366d 20 20 20 63 61 73 65 20 32 38 33 3a 0a 7b 0a 20     case 283:.{. 
2366e 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
2366f 67 65 72 28 70 50 61 72 73 65 2c 79 79 6d 73 70  ger(pParse,yymsp
23670 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c  [0].minor.yy373,
23671 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
23672 79 79 34 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20  yy46);.}.       
23673 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
23674 73 65 20 32 38 34 3a 0a 7b 0a 20 20 73 71 6c 69  se 284:.{.  sqli
23675 74 65 33 41 74 74 61 63 68 28 70 50 61 72 73 65  te3Attach(pParse
23676 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  , yymsp[-3].mino
23677 72 2e 79 79 31 37 32 2c 20 79 79 6d 73 70 5b 2d  r.yy172, yymsp[-
23678 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20  1].minor.yy172, 
23679 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2367a 79 33 38 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y386);.}.       
2367b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2367c 73 65 20 32 38 35 3a 0a 7b 0a 20 20 73 71 6c 69  se 285:.{.  sqli
2367d 74 65 33 44 65 74 61 63 68 28 70 50 61 72 73 65  te3Detach(pParse
2367e 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2367f 2e 79 79 31 37 32 29 3b 0a 7d 0a 20 20 20 20 20  .yy172);.}.     
23680 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
23681 63 61 73 65 20 32 38 36 3a 0a 7b 20 79 79 67 6f  case 286:.{ yygo
23682 74 6f 6d 69 6e 6f 72 2e 79 79 33 38 36 20 3d 20  tominor.yy386 = 
23683 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0; }.        bre
23684 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
23685 38 37 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f  87:.{ yygotomino
23686 72 2e 79 79 33 38 36 20 3d 20 79 79 6d 73 70 5b  r.yy386 = yymsp[
23687 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b 20  0].minor.yy172; 
23688 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
23689 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 30 3a  .      case 290:
2368a 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78  .{sqlite3Reindex
2368b 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d  (pParse, 0, 0);}
2368c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2368d 20 20 20 20 20 20 63 61 73 65 20 32 39 31 3a 0a        case 291:.
2368e 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28  {sqlite3Reindex(
2368f 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
23690 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20  1].minor.yy410, 
23691 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
23692 79 79 34 31 30 29 3b 7d 0a 20 20 20 20 20 20 20  yy410);}.       
23693 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
23694 73 65 20 32 39 32 3a 0a 7b 73 71 6c 69 74 65 33  se 292:.{sqlite3
23695 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20  Analyze(pParse, 
23696 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  0, 0);}.        
23697 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
23698 65 20 32 39 33 3a 0a 7b 73 71 6c 69 74 65 33 41  e 293:.{sqlite3A
23699 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 26  nalyze(pParse, &
2369a 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2369b 79 79 34 31 30 2c 20 26 79 79 6d 73 70 5b 30 5d  yy410, &yymsp[0]
2369c 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d 0a  .minor.yy410);}.
2369d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2369e 20 20 20 20 20 63 61 73 65 20 32 39 34 3a 0a 7b       case 294:.{
2369f 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52  .  sqlite3AlterR
236a0 65 6e 61 6d 65 54 61 62 6c 65 28 70 50 61 72 73  enameTable(pPars
236a1 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  e,yymsp[-3].mino
236a2 72 2e 79 79 33 37 33 2c 26 79 79 6d 73 70 5b 30  r.yy373,&yymsp[0
236a3 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a  ].minor.yy410);.
236a4 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
236a5 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 35 3a  .      case 295:
236a6 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65  .{.  sqlite3Alte
236a7 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e  rFinishAddColumn
236a8 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
236a9 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b  0].minor.yy410);
236aa 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
236ab 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 36  ;.      case 296
236ac 3a 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74  :.{.  sqlite3Alt
236ad 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e  erBeginAddColumn
236ae 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30  (pParse, yymsp[0
236af 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 29 3b 0a  ].minor.yy373);.
236b0 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
236b1 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 39 3a  .      case 299:
236b2 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e  .{sqlite3VtabFin
236b3 69 73 68 50 61 72 73 65 28 70 50 61 72 73 65 2c  ishParse(pParse,
236b4 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
236b5 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
236b6 30 30 3a 0a 7b 73 71 6c 69 74 65 33 56 74 61 62  00:.{sqlite3Vtab
236b7 46 69 6e 69 73 68 50 61 72 73 65 28 70 50 61 72  FinishParse(pPar
236b8 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se,&yymsp[0].min
236b9 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
236ba 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
236bb 61 73 65 20 33 30 31 3a 0a 7b 0a 20 20 20 20 73  ase 301:.{.    s
236bc 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50  qlite3VtabBeginP
236bd 61 72 73 65 28 70 50 61 72 73 65 2c 20 26 79 79  arse(pParse, &yy
236be 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
236bf 34 31 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  410, &yymsp[-2].
236c0 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 26 79 79  minor.yy410, &yy
236c1 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
236c2 31 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  10);.}.        b
236c3 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
236c4 20 33 30 34 3a 0a 7b 73 71 6c 69 74 65 33 56 74   304:.{sqlite3Vt
236c5 61 62 41 72 67 49 6e 69 74 28 70 50 61 72 73 65  abArgInit(pParse
236c6 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
236c7 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
236c8 36 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  6:.      case 30
236c9 37 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  7:.      case 30
236ca 38 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  8:.      case 31
236cb 30 3a 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 41  0:.{sqlite3VtabA
236cc 72 67 45 78 74 65 6e 64 28 70 50 61 72 73 65 2c  rgExtend(pParse,
236cd 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
236ce 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
236cf 72 65 61 6b 3b 0a 20 20 7d 3b 0a 20 20 79 79 67  reak;.  };.  yyg
236d0 6f 74 6f 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f  oto = yyRuleInfo
236d1 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b 0a  [yyruleno].lhs;.
236d2 20 20 79 79 73 69 7a 65 20 3d 20 79 79 52 75 6c    yysize = yyRul
236d3 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e  eInfo[yyruleno].
236d4 6e 72 68 73 3b 0a 20 20 79 79 70 50 61 72 73 65  nrhs;.  yypParse
236d5 72 2d 3e 79 79 69 64 78 20 2d 3d 20 79 79 73 69  r->yyidx -= yysi
236d6 7a 65 3b 0a 20 20 79 79 61 63 74 20 3d 20 79 79  ze;.  yyact = yy
236d7 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74  _find_reduce_act
236d8 69 6f 6e 28 79 79 6d 73 70 5b 2d 79 79 73 69 7a  ion(yymsp[-yysiz
236d9 65 5d 2e 73 74 61 74 65 6e 6f 2c 79 79 67 6f 74  e].stateno,yygot
236da 6f 29 3b 0a 20 20 69 66 28 20 79 79 61 63 74 20  o);.  if( yyact 
236db 3c 20 59 59 4e 53 54 41 54 45 20 29 7b 0a 23 69  < YYNSTATE ){.#i
236dc 66 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  fdef NDEBUG.    
236dd 2f 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74  /* If we are not
236de 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74   debugging and t
236df 68 65 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e  he reduce action
236e0 20 70 6f 70 70 65 64 20 61 74 20 6c 65 61 73 74   popped at least
236e1 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6c 65 6d  .    ** one elem
236e2 65 6e 74 20 6f 66 66 20 74 68 65 20 73 74 61 63  ent off the stac
236e3 6b 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 70  k, then we can p
236e4 75 73 68 20 74 68 65 20 6e 65 77 20 65 6c 65 6d  ush the new elem
236e5 65 6e 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20  ent back.    ** 
236e6 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 68  onto the stack h
236e7 65 72 65 2c 20 61 6e 64 20 73 6b 69 70 20 74 68  ere, and skip th
236e8 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  e stack overflow
236e9 20 74 65 73 74 20 69 6e 20 79 79 5f 73 68 69 66   test in yy_shif
236ea 74 28 29 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74  t()..    ** That
236eb 20 67 69 76 65 73 20 61 20 73 69 67 6e 69 66 69   gives a signifi
236ec 63 61 6e 74 20 73 70 65 65 64 20 69 6d 70 72 6f  cant speed impro
236ed 76 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69  vement. */.    i
236ee 66 28 20 79 79 73 69 7a 65 20 29 7b 0a 20 20 20  f( yysize ){.   
236ef 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
236f0 69 64 78 2b 2b 3b 0a 20 20 20 20 20 20 79 79 6d  idx++;.      yym
236f1 73 70 20 2d 3d 20 79 79 73 69 7a 65 2d 31 3b 0a  sp -= yysize-1;.
236f2 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 73 74 61        yymsp->sta
236f3 74 65 6e 6f 20 3d 20 79 79 61 63 74 3b 0a 20 20  teno = yyact;.  
236f4 20 20 20 20 79 79 6d 73 70 2d 3e 6d 61 6a 6f 72      yymsp->major
236f5 20 3d 20 79 79 67 6f 74 6f 3b 0a 20 20 20 20 20   = yygoto;.     
236f6 20 79 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d 20   yymsp->minor = 
236f7 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20 20  yygotominor;.   
236f8 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
236f9 20 20 7b 0a 20 20 20 20 20 20 79 79 5f 73 68 69    {.      yy_shi
236fa 66 74 28 79 79 70 50 61 72 73 65 72 2c 79 79 61  ft(yypParser,yya
236fb 63 74 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f 74  ct,yygoto,&yygot
236fc 6f 6d 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a 20  ominor);.    }. 
236fd 20 7d 65 6c 73 65 20 69 66 28 20 79 79 61 63 74   }else if( yyact
236fe 20 3d 3d 20 59 59 4e 53 54 41 54 45 20 2b 20 59   == YYNSTATE + Y
236ff 59 4e 52 55 4c 45 20 2b 20 31 20 29 7b 0a 20 20  YNRULE + 1 ){.  
23700 20 20 79 79 5f 61 63 63 65 70 74 28 79 79 70 50    yy_accept(yypP
23701 61 72 73 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  arser);.  }.}../
23702 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
23703 6e 67 20 63 6f 64 65 20 65 78 65 63 75 74 65 73  ng code executes
23704 20 77 68 65 6e 20 74 68 65 20 70 61 72 73 65 20   when the parse 
23705 66 61 69 6c 73 0a 2a 2f 0a 73 74 61 74 69 63 20  fails.*/.static 
23706 76 6f 69 64 20 79 79 5f 70 61 72 73 65 5f 66 61  void yy_parse_fa
23707 69 6c 65 64 28 0a 20 20 79 79 50 61 72 73 65 72  iled(.  yyParser
23708 20 2a 79 79 70 50 61 72 73 65 72 20 20 20 20 20   *yypParser     
23709 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2370a 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ser */.){.  sqli
2370b 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
2370c 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  CH;.#ifndef NDEB
2370d 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  UG.  if( yyTrace
2370e 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69  FILE ){.    fpri
2370f 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
23710 22 25 73 46 61 69 6c 21 5c 6e 22 2c 79 79 54 72  "%sFail!\n",yyTr
23711 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 7d 0a  acePrompt);.  }.
23712 23 65 6e 64 69 66 0a 20 20 77 68 69 6c 65 28 20  #endif.  while( 
23713 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
23714 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72  >=0 ) yy_pop_par
23715 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72  ser_stack(yypPar
23716 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65 72 65 20  ser);.  /* Here 
23717 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65 64  code is inserted
23718 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 65   which will be e
23719 78 65 63 75 74 65 64 20 77 68 65 6e 65 76 65 72  xecuted whenever
2371a 20 74 68 65 0a 20 20 2a 2a 20 70 61 72 73 65 72   the.  ** parser
2371b 20 66 61 69 6c 73 20 2a 2f 0a 20 20 73 71 6c 69   fails */.  sqli
2371c 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
2371d 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20  RE; /* Suppress 
2371e 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e  warning about un
2371f 75 73 65 64 20 25 65 78 74 72 61 5f 61 72 67 75  used %extra_argu
23720 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20 2a 2f  ment variable */
23721 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
23722 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65 78 65  llowing code exe
23723 63 75 74 65 73 20 77 68 65 6e 20 61 20 73 79 6e  cutes when a syn
23724 74 61 78 20 65 72 72 6f 72 20 66 69 72 73 74 20  tax error first 
23725 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  occurs..*/.stati
23726 63 20 76 6f 69 64 20 79 79 5f 73 79 6e 74 61 78  c void yy_syntax
23727 5f 65 72 72 6f 72 28 0a 20 20 79 79 50 61 72 73  _error(.  yyPars
23728 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20  er *yypParser,  
23729 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2372a 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20  parser */.  int 
2372b 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20  yymajor,        
2372c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2372d 65 20 6d 61 6a 6f 72 20 74 79 70 65 20 6f 66 20  e major type of 
2372e 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20  the error token 
2372f 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  */.  YYMINORTYPE
23730 20 79 79 6d 69 6e 6f 72 20 20 20 20 20 20 20 20   yyminor        
23731 20 20 20 20 2f 2a 20 54 68 65 20 6d 69 6e 6f 72      /* The minor
23732 20 74 79 70 65 20 6f 66 20 74 68 65 20 65 72 72   type of the err
23733 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  or token */.){. 
23734 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
23735 47 5f 46 45 54 43 48 3b 0a 23 64 65 66 69 6e 65  G_FETCH;.#define
23736 20 54 4f 4b 45 4e 20 28 79 79 6d 69 6e 6f 72 2e   TOKEN (yyminor.
23737 79 79 30 29 0a 0a 20 20 69 66 28 20 21 70 50 61  yy0)..  if( !pPa
23738 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72 20  rse->parseError 
23739 29 7b 0a 20 20 20 20 69 66 28 20 54 4f 4b 45 4e  ){.    if( TOKEN
2373a 2e 7a 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 73  .z[0] ){.      s
2373b 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2373c 50 61 72 73 65 2c 20 22 6e 65 61 72 20 5c 22 25  Parse, "near \"%
2373d 54 5c 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f  T\": syntax erro
2373e 72 22 2c 20 26 54 4f 4b 45 4e 29 3b 0a 20 20 20  r", &TOKEN);.   
2373f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
23740 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
23741 61 72 73 65 2c 20 22 69 6e 63 6f 6d 70 6c 65 74  arse, "incomplet
23742 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22  e SQL statement"
23743 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  );.    }.    pPa
23744 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72 20  rse->parseError 
23745 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 1;.  }.  sqlit
23746 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52  e3ParserARG_STOR
23747 45 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20 77  E; /* Suppress w
23748 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e 75  arning about unu
23749 73 65 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d  sed %extra_argum
2374a 65 6e 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  ent variable */.
2374b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
2374c 6c 6f 77 69 6e 67 20 69 73 20 65 78 65 63 75 74  lowing is execut
2374d 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 72 73  ed when the pars
2374e 65 72 20 61 63 63 65 70 74 73 0a 2a 2f 0a 73 74  er accepts.*/.st
2374f 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63 63  atic void yy_acc
23750 65 70 74 28 0a 20 20 79 79 50 61 72 73 65 72 20  ept(.  yyParser 
23751 2a 79 79 70 50 61 72 73 65 72 20 20 20 20 20 20  *yypParser      
23752 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
23753 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  er */.){.  sqlit
23754 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43  e3ParserARG_FETC
23755 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  H;.#ifndef NDEBU
23756 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46  G.  if( yyTraceF
23757 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  ILE ){.    fprin
23758 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
23759 25 73 41 63 63 65 70 74 21 5c 6e 22 2c 79 79 54  %sAccept!\n",yyT
2375a 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 7d  racePrompt);.  }
2375b 0a 23 65 6e 64 69 66 0a 20 20 77 68 69 6c 65 28  .#endif.  while(
2375c 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
2375d 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61  x>=0 ) yy_pop_pa
2375e 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61  rser_stack(yypPa
2375f 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65 72 65  rser);.  /* Here
23760 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65   code is inserte
23761 64 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20  d which will be 
23762 65 78 65 63 75 74 65 64 20 77 68 65 6e 65 76 65  executed wheneve
23763 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 72 73 65  r the.  ** parse
23764 72 20 61 63 63 65 70 74 73 20 2a 2f 0a 20 20 73  r accepts */.  s
23765 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
23766 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65  STORE; /* Suppre
23767 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74  ss warning about
23768 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61   unused %extra_a
23769 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65  rgument variable
2376a 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54 68 65 20 6d 61   */.}../* The ma
2376b 69 6e 20 70 61 72 73 65 72 20 70 72 6f 67 72 61  in parser progra
2376c 6d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  m..** The first 
2376d 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f  argument is a po
2376e 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63  inter to a struc
2376f 74 75 72 65 20 6f 62 74 61 69 6e 65 64 20 66 72  ture obtained fr
23770 6f 6d 0a 2a 2a 20 22 73 71 6c 69 74 65 33 50 61  om.** "sqlite3Pa
23771 72 73 65 72 41 6c 6c 6f 63 22 20 77 68 69 63 68  rserAlloc" which
23772 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 63   describes the c
23773 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20  urrent state of 
23774 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20 54  the parser..** T
23775 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
23776 6e 74 20 69 73 20 74 68 65 20 6d 61 6a 6f 72 20  nt is the major 
23777 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 20 20 54  token number.  T
23778 68 65 20 74 68 69 72 64 20 69 73 0a 2a 2a 20 74  he third is.** t
23779 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 2e 20  he minor token. 
2377a 20 54 68 65 20 66 6f 75 72 74 68 20 6f 70 74 69   The fourth opti
2377b 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 69 73  onal argument is
2377c 20 77 68 61 74 65 76 65 72 20 74 68 65 0a 2a 2a   whatever the.**
2377d 20 75 73 65 72 20 77 61 6e 74 73 20 28 61 6e 64   user wants (and
2377e 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
2377f 65 20 67 72 61 6d 6d 61 72 29 20 61 6e 64 20 69  e grammar) and i
23780 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 0a  s available for.
23781 2a 2a 20 75 73 65 20 62 79 20 74 68 65 20 61 63  ** use by the ac
23782 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  tion routines..*
23783 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20  *.** Inputs:.** 
23784 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70  <ul>.** <li> A p
23785 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61  ointer to the pa
23786 72 73 65 72 20 28 61 6e 20 6f 70 61 71 75 65 20  rser (an opaque 
23787 73 74 72 75 63 74 75 72 65 2e 29 0a 2a 2a 20 3c  structure.).** <
23788 6c 69 3e 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f  li> The major to
23789 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 3c  ken number..** <
2378a 6c 69 3e 20 54 68 65 20 6d 69 6e 6f 72 20 74 6f  li> The minor to
2378b 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 3c  ken number..** <
2378c 6c 69 3e 20 41 6e 20 6f 70 74 69 6f 6e 20 61 72  li> An option ar
2378d 67 75 6d 65 6e 74 20 6f 66 20 61 20 67 72 61 6d  gument of a gram
2378e 6d 61 72 2d 73 70 65 63 69 66 69 65 64 20 74 79  mar-specified ty
2378f 70 65 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  pe..** </ul>.**.
23790 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 4e  ** Outputs:.** N
23791 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  one..*/.SQLITE_P
23792 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23793 74 65 33 50 61 72 73 65 72 28 0a 20 20 76 6f 69  te3Parser(.  voi
23794 64 20 2a 79 79 70 2c 20 20 20 20 20 20 20 20 20  d *yyp,         
23795 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
23796 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74   parser */.  int
23797 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20 20   yymajor,       
23798 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
23799 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 63 6f 64   major token cod
2379a 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71  e number */.  sq
2379b 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e  lite3ParserTOKEN
2379c 54 59 50 45 20 79 79 6d 69 6e 6f 72 20 20 20 20  TYPE yyminor    
2379d 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20     /* The value 
2379e 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 2a 2f  for the token */
2379f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
237a0 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20 20 20  ARG_PDECL       
237a1 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f          /* Optio
237a2 6e 61 6c 20 25 65 78 74 72 61 5f 61 72 67 75 6d  nal %extra_argum
237a3 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  ent parameter */
237a4 0a 29 7b 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50  .){.  YYMINORTYP
237a5 45 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 3b 0a  E yyminorunion;.
237a6 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20 20 20    int yyact;    
237a7 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
237a8 61 72 73 65 72 20 61 63 74 69 6f 6e 2e 20 2a 2f  arser action. */
237a9 0a 20 20 69 6e 74 20 79 79 65 6e 64 6f 66 69 6e  .  int yyendofin
237aa 70 75 74 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  put;     /* True
237ab 20 69 66 20 77 65 20 61 72 65 20 61 74 20 74 68   if we are at th
237ac 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a  e end of input *
237ad 2f 0a 20 20 69 6e 74 20 79 79 65 72 72 6f 72 68  /.  int yyerrorh
237ae 69 74 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72 75  it = 0;   /* Tru
237af 65 20 69 66 20 79 79 6d 61 6a 6f 72 20 68 61 73  e if yymajor has
237b0 20 69 6e 76 6f 6b 65 64 20 61 6e 20 65 72 72 6f   invoked an erro
237b1 72 20 2a 2f 0a 20 20 79 79 50 61 72 73 65 72 20  r */.  yyParser 
237b2 2a 79 79 70 50 61 72 73 65 72 3b 20 20 2f 2a 20  *yypParser;  /* 
237b3 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 0a 20  The parser */.. 
237b4 20 2f 2a 20 28 72 65 29 69 6e 69 74 69 61 6c 69   /* (re)initiali
237b5 7a 65 20 74 68 65 20 70 61 72 73 65 72 2c 20 69  ze the parser, i
237b6 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20  f necessary */. 
237b7 20 79 79 70 50 61 72 73 65 72 20 3d 20 28 79 79   yypParser = (yy
237b8 50 61 72 73 65 72 2a 29 79 79 70 3b 0a 20 20 69  Parser*)yyp;.  i
237b9 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
237ba 69 64 78 3c 30 20 29 7b 0a 23 69 66 20 59 59 53  idx<0 ){.#if YYS
237bb 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20  TACKDEPTH<=0.   
237bc 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
237bd 79 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a 20  yystksz <=0 ){. 
237be 20 20 20 20 20 6d 65 6d 73 65 74 28 26 79 79 6d       memset(&yym
237bf 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c 20 73 69  inorunion, 0, si
237c0 7a 65 6f 66 28 79 79 6d 69 6e 6f 72 75 6e 69 6f  zeof(yyminorunio
237c1 6e 29 29 3b 0a 20 20 20 20 20 20 79 79 53 74 61  n));.      yySta
237c2 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61  ckOverflow(yypPa
237c3 72 73 65 72 2c 20 26 79 79 6d 69 6e 6f 72 75 6e  rser, &yyminorun
237c4 69 6f 6e 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ion);.      retu
237c5 72 6e 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  rn;.    }.#endif
237c6 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  .    yypParser->
237c7 79 79 69 64 78 20 3d 20 30 3b 0a 20 20 20 20 79  yyidx = 0;.    y
237c8 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63  ypParser->yyerrc
237c9 6e 74 20 3d 20 2d 31 3b 0a 20 20 20 20 79 79 70  nt = -1;.    yyp
237ca 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
237cb 30 5d 2e 73 74 61 74 65 6e 6f 20 3d 20 30 3b 0a  0].stateno = 0;.
237cc 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
237cd 79 73 74 61 63 6b 5b 30 5d 2e 6d 61 6a 6f 72 20  ystack[0].major 
237ce 3d 20 30 3b 0a 20 20 7d 0a 20 20 79 79 6d 69 6e  = 0;.  }.  yymin
237cf 6f 72 75 6e 69 6f 6e 2e 79 79 30 20 3d 20 79 79  orunion.yy0 = yy
237d0 6d 69 6e 6f 72 3b 0a 20 20 79 79 65 6e 64 6f 66  minor;.  yyendof
237d1 69 6e 70 75 74 20 3d 20 28 79 79 6d 61 6a 6f 72  input = (yymajor
237d2 3d 3d 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  ==0);.  sqlite3P
237d3 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 0a  arserARG_STORE;.
237d4 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
237d5 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
237d6 45 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66  E ){.    fprintf
237d7 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
237d8 49 6e 70 75 74 20 25 73 5c 6e 22 2c 79 79 54 72  Input %s\n",yyTr
237d9 61 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b 65  acePrompt,yyToke
237da 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b  nName[yymajor]);
237db 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64  .  }.#endif..  d
237dc 6f 7b 0a 20 20 20 20 79 79 61 63 74 20 3d 20 79  o{.    yyact = y
237dd 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61 63 74  y_find_shift_act
237de 69 6f 6e 28 79 79 70 50 61 72 73 65 72 2c 79 79  ion(yypParser,yy
237df 6d 61 6a 6f 72 29 3b 0a 20 20 20 20 69 66 28 20  major);.    if( 
237e0 79 79 61 63 74 3c 59 59 4e 53 54 41 54 45 20 29  yyact<YYNSTATE )
237e1 7b 0a 20 20 20 20 20 20 79 79 5f 73 68 69 66 74  {.      yy_shift
237e2 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
237e3 2c 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f  ,yymajor,&yymino
237e4 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 79  runion);.      y
237e5 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63  ypParser->yyerrc
237e6 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20  nt--;.      if( 
237e7 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 26 26 20  yyendofinput && 
237e8 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
237e9 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79  >=0 ){.        y
237ea 79 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 20 20  ymajor = 0;.    
237eb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
237ec 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
237ed 4f 44 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ODE;.      }.   
237ee 20 7d 65 6c 73 65 20 69 66 28 20 79 79 61 63 74   }else if( yyact
237ef 20 3c 20 59 59 4e 53 54 41 54 45 20 2b 20 59 59   < YYNSTATE + YY
237f0 4e 52 55 4c 45 20 29 7b 0a 20 20 20 20 20 20 79  NRULE ){.      y
237f1 79 5f 72 65 64 75 63 65 28 79 79 70 50 61 72 73  y_reduce(yypPars
237f2 65 72 2c 79 79 61 63 74 2d 59 59 4e 53 54 41 54  er,yyact-YYNSTAT
237f3 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  E);.    }else if
237f4 28 20 79 79 61 63 74 20 3d 3d 20 59 59 5f 45 52  ( yyact == YY_ER
237f5 52 4f 52 5f 41 43 54 49 4f 4e 20 29 7b 0a 20 20  ROR_ACTION ){.  
237f6 20 20 20 20 69 6e 74 20 79 79 6d 78 3b 0a 23 69      int yymx;.#i
237f7 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
237f8 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49     if( yyTraceFI
237f9 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 66 70  LE ){.        fp
237fa 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
237fb 45 2c 22 25 73 53 79 6e 74 61 78 20 45 72 72 6f  E,"%sSyntax Erro
237fc 72 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  r!\n",yyTracePro
237fd 6d 70 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  mpt);.      }.#e
237fe 6e 64 69 66 0a 23 69 66 64 65 66 20 59 59 45 52  ndif.#ifdef YYER
237ff 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20  RORSYMBOL.      
23800 2f 2a 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f  /* A syntax erro
23801 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a  r has occurred..
23802 20 20 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73        ** The res
23803 70 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f  ponse to an erro
23804 72 20 64 65 70 65 6e 64 73 20 75 70 6f 6e 20 77  r depends upon w
23805 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
23806 65 0a 20 20 20 20 20 20 2a 2a 20 67 72 61 6d 6d  e.      ** gramm
23807 61 72 20 64 65 66 69 6e 65 73 20 61 6e 20 65 72  ar defines an er
23808 72 6f 72 20 74 6f 6b 65 6e 20 22 45 52 52 4f 52  ror token "ERROR
23809 22 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ".  .      **.  
2380a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 77      ** This is w
2380b 68 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65  hat we do if the
2380c 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 64 65   grammar does de
2380d 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20  fine ERROR:.    
2380e 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a    **.      **  *
2380f 20 43 61 6c 6c 20 74 68 65 20 25 73 79 6e 74 61   Call the %synta
23810 78 5f 65 72 72 6f 72 20 66 75 6e 63 74 69 6f 6e  x_error function
23811 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
23812 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20 70 6f 70   **  * Begin pop
23813 70 69 6e 67 20 74 68 65 20 73 74 61 63 6b 20 75  ping the stack u
23814 6e 74 69 6c 20 77 65 20 65 6e 74 65 72 20 61 20  ntil we enter a 
23815 73 74 61 74 65 20 77 68 65 72 65 0a 20 20 20 20  state where.    
23816 20 20 2a 2a 20 20 20 20 69 74 20 69 73 20 6c 65    **    it is le
23817 67 61 6c 20 74 6f 20 73 68 69 66 74 20 74 68 65  gal to shift the
23818 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2c 20 74   error symbol, t
23819 68 65 6e 20 73 68 69 66 74 0a 20 20 20 20 20 20  hen shift.      
2381a 2a 2a 20 20 20 20 74 68 65 20 65 72 72 6f 72 20  **    the error 
2381b 73 79 6d 62 6f 6c 2e 0a 20 20 20 20 20 20 2a 2a  symbol..      **
2381c 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 53 65 74  .      **  * Set
2381d 20 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74   the error count
2381e 20 74 6f 20 74 68 72 65 65 2e 0a 20 20 20 20 20   to three..     
2381f 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20   **.      **  * 
23820 42 65 67 69 6e 20 61 63 63 65 70 74 69 6e 67 20  Begin accepting 
23821 61 6e 64 20 73 68 69 66 74 69 6e 67 20 6e 65 77  and shifting new
23822 20 74 6f 6b 65 6e 73 2e 20 20 4e 6f 20 6e 65 77   tokens.  No new
23823 20 65 72 72 6f 72 0a 20 20 20 20 20 20 2a 2a 20   error.      ** 
23824 20 20 20 70 72 6f 63 65 73 73 69 6e 67 20 77 69     processing wi
23825 6c 6c 20 6f 63 63 75 72 20 75 6e 74 69 6c 20 74  ll occur until t
23826 68 72 65 65 20 74 6f 6b 65 6e 73 20 68 61 76 65  hree tokens have
23827 20 62 65 65 6e 0a 20 20 20 20 20 20 2a 2a 20 20   been.      **  
23828 20 20 73 68 69 66 74 65 64 20 73 75 63 63 65 73    shifted succes
23829 73 66 75 6c 6c 79 2e 0a 20 20 20 20 20 20 2a 2a  sfully..      **
2382a 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2382b 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
2382c 79 65 72 72 63 6e 74 3c 30 20 29 7b 0a 20 20 20  yerrcnt<0 ){.   
2382d 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f 65       yy_syntax_e
2382e 72 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79  rror(yypParser,y
2382f 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e  ymajor,yyminorun
23830 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ion);.      }.  
23831 20 20 20 20 79 79 6d 78 20 3d 20 79 79 70 50 61      yymx = yypPa
23832 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79  rser->yystack[yy
23833 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e  pParser->yyidx].
23834 6d 61 6a 6f 72 3b 0a 20 20 20 20 20 20 69 66 28  major;.      if(
23835 20 79 79 6d 78 3d 3d 59 59 45 52 52 4f 52 53 59   yymx==YYERRORSY
23836 4d 42 4f 4c 20 7c 7c 20 79 79 65 72 72 6f 72 68  MBOL || yyerrorh
23837 69 74 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44  it ){.#ifndef ND
23838 45 42 55 47 0a 20 20 20 20 20 20 20 20 69 66 28  EBUG.        if(
23839 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a   yyTraceFILE ){.
2383a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
2383b 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
2383c 73 44 69 73 63 61 72 64 20 69 6e 70 75 74 20 74  sDiscard input t
2383d 6f 6b 65 6e 20 25 73 5c 6e 22 2c 0a 20 20 20 20  oken %s\n",.    
2383e 20 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65           yyTrace
2383f 50 72 6f 6d 70 74 2c 79 79 54 6f 6b 65 6e 4e 61  Prompt,yyTokenNa
23840 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b 0a 20 20  me[yymajor]);.  
23841 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
23842 20 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75         yy_destru
23843 63 74 6f 72 28 79 79 6d 61 6a 6f 72 2c 26 79 79  ctor(yymajor,&yy
23844 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
23845 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59       yymajor = Y
23846 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 7d  YNOCODE;.      }
23847 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 77  else{.         w
23848 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20 20  hile(.          
23849 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
2384a 20 3e 3d 20 30 20 26 26 0a 20 20 20 20 20 20 20   >= 0 &&.       
2384b 20 20 20 79 79 6d 78 20 21 3d 20 59 59 45 52 52     yymx != YYERR
2384c 4f 52 53 59 4d 42 4f 4c 20 26 26 0a 20 20 20 20  ORSYMBOL &&.    
2384d 20 20 20 20 20 20 28 79 79 61 63 74 20 3d 20 79        (yyact = y
2384e 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63  y_find_reduce_ac
2384f 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 20 20  tion(.          
23850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79                yy
23851 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
23852 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64  [yypParser->yyid
23853 78 5d 2e 73 74 61 74 65 6e 6f 2c 0a 20 20 20 20  x].stateno,.    
23854 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23855 20 20 20 20 59 59 45 52 52 4f 52 53 59 4d 42 4f      YYERRORSYMBO
23856 4c 29 29 20 3e 3d 20 59 59 4e 53 54 41 54 45 0a  L)) >= YYNSTATE.
23857 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
23858 20 20 20 20 20 79 79 5f 70 6f 70 5f 70 61 72 73       yy_pop_pars
23859 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73  er_stack(yypPars
2385a 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  er);.        }. 
2385b 20 20 20 20 20 20 20 69 66 28 20 79 79 70 50 61         if( yypPa
2385c 72 73 65 72 2d 3e 79 79 69 64 78 20 3c 20 30 20  rser->yyidx < 0 
2385d 7c 7c 20 79 79 6d 61 6a 6f 72 3d 3d 30 20 29 7b  || yymajor==0 ){
2385e 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 64 65  .          yy_de
2385f 73 74 72 75 63 74 6f 72 28 79 79 6d 61 6a 6f 72  structor(yymajor
23860 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b  ,&yyminorunion);
23861 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 70 61  .          yy_pa
23862 72 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61  rse_failed(yypPa
23863 72 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  rser);.         
23864 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
23865 4f 44 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ODE;.        }el
23866 73 65 20 69 66 28 20 79 79 6d 78 21 3d 59 59 45  se if( yymx!=YYE
23867 52 52 4f 52 53 59 4d 42 4f 4c 20 29 7b 0a 20 20  RRORSYMBOL ){.  
23868 20 20 20 20 20 20 20 20 59 59 4d 49 4e 4f 52 54          YYMINORT
23869 59 50 45 20 75 32 3b 0a 20 20 20 20 20 20 20 20  YPE u2;.        
2386a 20 20 75 32 2e 59 59 45 52 52 53 59 4d 44 54 20    u2.YYERRSYMDT 
2386b 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79  = 0;.          y
2386c 79 5f 73 68 69 66 74 28 79 79 70 50 61 72 73 65  y_shift(yypParse
2386d 72 2c 79 79 61 63 74 2c 59 59 45 52 52 4f 52 53  r,yyact,YYERRORS
2386e 59 4d 42 4f 4c 2c 26 75 32 29 3b 0a 20 20 20 20  YMBOL,&u2);.    
2386f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
23870 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
23871 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20  yerrcnt = 3;.   
23872 20 20 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20     yyerrorhit = 
23873 31 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 59 59 45  1;.#else  /* YYE
23874 52 52 4f 52 53 59 4d 42 4f 4c 20 69 73 20 6e 6f  RRORSYMBOL is no
23875 74 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20 20 20  t defined */.   
23876 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 77 68     /* This is wh
23877 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65 20  at we do if the 
23878 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 6e 6f 74  grammar does not
23879 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20   define ERROR:. 
2387a 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
2387b 20 20 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72    * Report an er
2387c 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61 6e 64  ror message, and
2387d 20 74 68 72 6f 77 20 61 77 61 79 20 74 68 65 20   throw away the 
2387e 69 6e 70 75 74 20 74 6f 6b 65 6e 2e 0a 20 20 20  input token..   
2387f 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
23880 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 74  * If the input t
23881 6f 6b 65 6e 20 69 73 20 24 2c 20 74 68 65 6e 20  oken is $, then 
23882 66 61 69 6c 20 74 68 65 20 70 61 72 73 65 2e 0a  fail the parse..
23883 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
23884 2a 20 41 73 20 62 65 66 6f 72 65 2c 20 73 75 62  * As before, sub
23885 73 65 71 75 65 6e 74 20 65 72 72 6f 72 20 6d 65  sequent error me
23886 73 73 61 67 65 73 20 61 72 65 20 73 75 70 70 72  ssages are suppr
23887 65 73 73 65 64 20 75 6e 74 69 6c 0a 20 20 20 20  essed until.    
23888 20 20 2a 2a 20 74 68 72 65 65 20 69 6e 70 75 74    ** three input
23889 20 74 6f 6b 65 6e 73 20 68 61 76 65 20 62 65 65   tokens have bee
2388a 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 73  n successfully s
2388b 68 69 66 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f  hifted..      */
2388c 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50 61  .      if( yypPa
2388d 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c 3d  rser->yyerrcnt<=
2388e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f  0 ){.        yy_
2388f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70  syntax_error(yyp
23890 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79  Parser,yymajor,y
23891 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
23892 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 70 50      }.      yypP
23893 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 20  arser->yyerrcnt 
23894 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 5f 64 65  = 3;.      yy_de
23895 73 74 72 75 63 74 6f 72 28 79 79 6d 61 6a 6f 72  structor(yymajor
23896 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b  ,&yyminorunion);
23897 0a 20 20 20 20 20 20 69 66 28 20 79 79 65 6e 64  .      if( yyend
23898 6f 66 69 6e 70 75 74 20 29 7b 0a 20 20 20 20 20  ofinput ){.     
23899 20 20 20 79 79 5f 70 61 72 73 65 5f 66 61 69 6c     yy_parse_fail
2389a 65 64 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ed(yypParser);. 
2389b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 6d       }.      yym
2389c 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b  ajor = YYNOCODE;
2389d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73  .#endif.    }els
2389e 65 7b 0a 20 20 20 20 20 20 79 79 5f 61 63 63 65  e{.      yy_acce
2389f 70 74 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  pt(yypParser);. 
238a0 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59       yymajor = Y
238a1 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 7d 0a 20  YNOCODE;.    }. 
238a2 20 7d 77 68 69 6c 65 28 20 79 79 6d 61 6a 6f 72   }while( yymajor
238a3 21 3d 59 59 4e 4f 43 4f 44 45 20 26 26 20 79 79  !=YYNOCODE && yy
238a4 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d  pParser->yyidx>=
238a5 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  0 );.  return;.}
238a6 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
238a7 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 63  * End of parse.c
238a8 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
238a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238ab 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
238ac 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 6f 6b  * Begin file tok
238ad 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  enize.c ********
238ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238b0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
238b1 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
238b2 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
238b3 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
238b4 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
238b5 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
238b6 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
238b7 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
238b8 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
238b9 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
238ba 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
238bb 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
238bc 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
238bd 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
238be 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
238bf 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
238c0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
238c1 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
238c2 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
238c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238c7 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74  ********.** An t
238c8 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53 51 4c  okenizer for SQL
238c9 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
238ca 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
238cb 20 74 68 61 74 20 73 70 6c 69 74 73 20 61 6e 20   that splits an 
238cc 53 51 4c 20 69 6e 70 75 74 20 73 74 72 69 6e 67  SQL input string
238cd 20 75 70 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69   up into.** indi
238ce 76 69 64 75 61 6c 20 74 6f 6b 65 6e 73 20 61 6e  vidual tokens an
238cf 64 20 73 65 6e 64 73 20 74 68 6f 73 65 20 74 6f  d sends those to
238d0 6b 65 6e 73 20 6f 6e 65 2d 62 79 2d 6f 6e 65 20  kens one-by-one 
238d1 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 70  over to the.** p
238d2 61 72 73 65 72 20 66 6f 72 20 61 6e 61 6c 79 73  arser for analys
238d3 69 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74  is..**.** $Id: t
238d4 6f 6b 65 6e 69 7a 65 2e 63 2c 76 20 31 2e 31 33  okenize.c,v 1.13
238d5 36 20 32 30 30 37 2f 30 38 2f 32 37 20 32 33 3a  6 2007/08/27 23:
238d6 32 36 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a  26:59 drh Exp $.
238d7 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 68  */../*.** The ch
238d8 61 72 4d 61 70 28 29 20 6d 61 63 72 6f 20 6d 61  arMap() macro ma
238d9 70 73 20 61 6c 70 68 61 62 65 74 69 63 20 63 68  ps alphabetic ch
238da 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68  aracters into th
238db 65 69 72 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73  eir.** lower-cas
238dc 65 20 41 53 43 49 49 20 65 71 75 69 76 61 6c 65  e ASCII equivale
238dd 6e 74 2e 20 20 4f 6e 20 41 53 43 49 49 20 6d 61  nt.  On ASCII ma
238de 63 68 69 6e 65 73 2c 20 74 68 69 73 20 69 73 20  chines, this is 
238df 6a 75 73 74 0a 2a 2a 20 61 6e 20 75 70 70 65 72  just.** an upper
238e0 2d 74 6f 2d 6c 6f 77 65 72 20 63 61 73 65 20 6d  -to-lower case m
238e1 61 70 2e 20 20 4f 6e 20 45 42 43 44 49 43 20 6d  ap.  On EBCDIC m
238e2 61 63 68 69 6e 65 73 20 77 65 20 61 6c 73 6f 20  achines we also 
238e3 6e 65 65 64 0a 2a 2a 20 74 6f 20 61 64 6a 75 73  need.** to adjus
238e4 74 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 2e 20  t the encoding. 
238e5 20 4f 6e 6c 79 20 61 6c 70 68 61 62 65 74 69 63   Only alphabetic
238e6 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 20   characters and 
238e7 75 6e 64 65 72 73 63 6f 72 65 73 0a 2a 2a 20 6e  underscores.** n
238e8 65 65 64 20 74 6f 20 62 65 20 74 72 61 6e 73 6c  eed to be transl
238e9 61 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ated..*/.#ifdef 
238ea 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 64  SQLITE_ASCII.# d
238eb 65 66 69 6e 65 20 63 68 61 72 4d 61 70 28 58 29  efine charMap(X)
238ec 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c   sqlite3UpperToL
238ed 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63  ower[(unsigned c
238ee 68 61 72 29 58 5d 0a 23 65 6e 64 69 66 0a 23 69  har)X].#endif.#i
238ef 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
238f0 49 43 0a 23 20 64 65 66 69 6e 65 20 63 68 61 72  IC.# define char
238f1 4d 61 70 28 58 29 20 65 62 63 64 69 63 54 6f 41  Map(X) ebcdicToA
238f2 73 63 69 69 5b 28 75 6e 73 69 67 6e 65 64 20 63  scii[(unsigned c
238f3 68 61 72 29 58 5d 0a 63 6f 6e 73 74 20 75 6e 73  har)X].const uns
238f4 69 67 6e 65 64 20 63 68 61 72 20 65 62 63 64 69  igned char ebcdi
238f5 63 54 6f 41 73 63 69 69 5b 5d 20 3d 20 7b 0a 2f  cToAscii[] = {./
238f6 2a 20 30 20 20 20 31 20 20 20 32 20 20 20 33 20  * 0   1   2   3 
238f7 20 20 34 20 20 20 35 20 20 20 36 20 20 20 37 20    4   5   6   7 
238f8 20 20 38 20 20 20 39 20 20 20 41 20 20 20 42 20    8   9   A   B 
238f9 20 20 43 20 20 20 44 20 20 20 45 20 20 20 46 20    C   D   E   F 
238fa 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c  */.   0,  0,  0,
238fb 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
238fc 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
238fd 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
238fe 20 20 30 2c 20 20 2f 2a 20 30 78 20 2a 2f 0a 20    0,  /* 0x */. 
238ff 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23900 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23901 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23902 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23903 20 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 30 2c    /* 1x */.   0,
23904 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23905 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23906 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23907 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
23908 20 32 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   2x */.   0,  0,
23909 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2390a 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2390b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2390c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 33 78 20    0,  0,  /* 3x 
2390d 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c  */.   0,  0,  0,
2390e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2390f 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23910 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23911 20 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20    0,  /* 4x */. 
23912 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23913 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23914 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23915 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23916 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 30 2c    /* 5x */.   0,
23917 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23918 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23919 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2391a 20 39 35 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a   95,  0,  0,  /*
2391b 20 36 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   6x */.   0,  0,
2391c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2391d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2391e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2391f 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 37 78 20    0,  0,  /* 7x 
23920 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20 39 38 2c  */.   0, 97, 98,
23921 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c   99,100,101,102,
23922 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20 30 2c  103,104,105,  0,
23923 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23924 20 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f 0a 20    0,  /* 8x */. 
23925 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c    0,106,107,108,
23926 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c  109,110,111,112,
23927 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20 30 2c  113,114,  0,  0,
23928 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23929 20 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 30 2c    /* 9x */.   0,
2392a 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c    0,115,116,117,
2392b 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c  118,119,120,121,
2392c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c  122,  0,  0,  0,
2392d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
2392e 20 41 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   Ax */.   0,  0,
2392f 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23930 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23931 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23932 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 42 78 20    0,  0,  /* Bx 
23933 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20 39 38 2c  */.   0, 97, 98,
23934 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c   99,100,101,102,
23935 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20 30 2c  103,104,105,  0,
23936 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23937 20 20 30 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20    0,  /* Cx */. 
23938 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c    0,106,107,108,
23939 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c  109,110,111,112,
2393a 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20 30 2c  113,114,  0,  0,
2393b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2393c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 30 2c    /* Dx */.   0,
2393d 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c    0,115,116,117,
2393e 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c  118,119,120,121,
2393f 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c  122,  0,  0,  0,
23940 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
23941 20 45 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   Ex */.   0,  0,
23942 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23943 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23944 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
23945 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 46 78 20    0,  0,  /* Fx 
23946 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a  */.};.#endif../*
23947 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 4b  .** The sqlite3K
23948 65 79 77 6f 72 64 43 6f 64 65 20 66 75 6e 63 74  eywordCode funct
23949 69 6f 6e 20 6c 6f 6f 6b 73 20 75 70 20 61 6e 20  ion looks up an 
2394a 69 64 65 6e 74 69 66 69 65 72 20 74 6f 20 64 65  identifier to de
2394b 74 65 72 6d 69 6e 65 20 69 66 0a 2a 2a 20 69 74  termine if.** it
2394c 20 69 73 20 61 20 6b 65 79 77 6f 72 64 2e 20 20   is a keyword.  
2394d 49 66 20 69 74 20 69 73 20 61 20 6b 65 79 77 6f  If it is a keywo
2394e 72 64 2c 20 74 68 65 20 74 6f 6b 65 6e 20 63 6f  rd, the token co
2394f 64 65 20 6f 66 20 74 68 61 74 20 6b 65 79 77 6f  de of that keywo
23950 72 64 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e  rd is .** return
23951 65 64 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75  ed.  If the inpu
23952 74 20 69 73 20 6e 6f 74 20 61 20 6b 65 79 77 6f  t is not a keywo
23953 72 64 2c 20 54 4b 5f 49 44 20 69 73 20 72 65 74  rd, TK_ID is ret
23954 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
23955 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
23956 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
23957 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 62 79  was generated by
23958 20 61 20 70 72 6f 67 72 61 6d 2c 0a 2a 2a 20 6d   a program,.** m
23959 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 2c 20  kkeywordhash.h, 
2395a 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74  located in the t
2395b 6f 6f 6c 20 73 75 62 64 69 72 65 63 74 6f 72 79  ool subdirectory
2395c 20 6f 66 20 74 68 65 20 64 69 73 74 72 69 62 75   of the distribu
2395d 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 6f 75 74  tion..** The out
2395e 70 75 74 20 6f 66 20 74 68 65 20 6d 6b 6b 65 79  put of the mkkey
2395f 77 6f 72 64 68 61 73 68 2e 63 20 70 72 6f 67 72  wordhash.c progr
23960 61 6d 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  am is written in
23961 74 6f 20 61 20 66 69 6c 65 0a 2a 2a 20 6e 61 6d  to a file.** nam
23962 65 64 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68  ed keywordhash.h
23963 20 61 6e 64 20 74 68 65 6e 20 69 6e 63 6c 75 64   and then includ
23964 65 64 20 69 6e 74 6f 20 74 68 69 73 20 73 6f 75  ed into this sou
23965 72 63 65 20 66 69 6c 65 20 62 79 0a 2a 2a 20 74  rce file by.** t
23966 68 65 20 23 69 6e 63 6c 75 64 65 20 62 65 6c 6f  he #include belo
23967 77 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  w..*/./*********
23968 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6b 65  ***** Include ke
23969 79 77 6f 72 64 68 61 73 68 2e 68 20 69 6e 20 74  ywordhash.h in t
2396a 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 6f 6b  he middle of tok
2396b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  enize.c ********
2396c 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2396d 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2396e 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 2a   keywordhash.h *
2396f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23971 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 20 54 68 69  ****/./***** Thi
23972 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
23973 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  automatically ge
23974 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a 2a 2a  nerated code ***
23975 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ***.**.** The co
23976 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
23977 68 61 73 20 62 65 65 6e 20 61 75 74 6f 6d 61 74  has been automat
23978 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
23979 20 62 79 0a 2a 2a 0a 2a 2a 20 20 20 20 20 24 48   by.**.**     $H
2397a 65 61 64 65 72 3a 20 2f 73 71 6c 69 74 65 2f 73  eader: /sqlite/s
2397b 71 6c 69 74 65 2f 74 6f 6f 6c 2f 6d 6b 6b 65 79  qlite/tool/mkkey
2397c 77 6f 72 64 68 61 73 68 2e 63 2c 76 20 31 2e 33  wordhash.c,v 1.3
2397d 31 20 32 30 30 37 2f 30 37 2f 33 30 20 31 38 3a  1 2007/07/30 18:
2397e 32 36 3a 32 30 20 72 73 65 20 45 78 70 20 24 0a  26:20 rse Exp $.
2397f 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  **.** The code i
23980 6e 20 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c  n this file impl
23981 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f  ements a functio
23982 6e 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65  n that determine
23983 73 20 77 68 65 74 68 65 72 0a 2a 2a 20 6f 72 20  s whether.** or 
23984 6e 6f 74 20 61 20 67 69 76 65 6e 20 69 64 65 6e  not a given iden
23985 74 69 66 69 65 72 20 69 73 20 72 65 61 6c 6c 79  tifier is really
23986 20 61 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64 2e   an SQL keyword.
23987 20 20 54 68 65 20 73 61 6d 65 20 74 68 69 6e 67    The same thing
23988 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 69 6d 70  .** might be imp
23989 6c 65 6d 65 6e 74 65 64 20 6d 6f 72 65 20 64 69  lemented more di
2398a 72 65 63 74 6c 79 20 75 73 69 6e 67 20 61 20 68  rectly using a h
2398b 61 6e 64 2d 77 72 69 74 74 65 6e 20 68 61 73 68  and-written hash
2398c 20 74 61 62 6c 65 2e 0a 2a 2a 20 42 75 74 20 62   table..** But b
2398d 79 20 75 73 69 6e 67 20 74 68 69 73 20 61 75 74  y using this aut
2398e 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72  omatically gener
2398f 61 74 65 64 20 63 6f 64 65 2c 20 74 68 65 20 73  ated code, the s
23990 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 64 65 0a  ize of the code.
23991 2a 2a 20 69 73 20 73 75 62 73 74 61 6e 74 69 61  ** is substantia
23992 6c 6c 79 20 72 65 64 75 63 65 64 2e 20 20 54 68  lly reduced.  Th
23993 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  is is important 
23994 66 6f 72 20 65 6d 62 65 64 64 65 64 20 61 70 70  for embedded app
23995 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6f 6e 20  lications.** on 
23996 70 6c 61 74 66 6f 72 6d 73 20 77 69 74 68 20 6c  platforms with l
23997 69 6d 69 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a  imited memory..*
23998 2f 0a 2f 2a 20 48 61 73 68 20 73 63 6f 72 65 3a  /./* Hash score:
23999 20 31 36 35 20 2a 2f 0a 73 74 61 74 69 63 20 69   165 */.static i
2399a 6e 74 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 63  nt keywordCode(c
2399b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
2399c 74 20 6e 29 7b 0a 20 20 2f 2a 20 7a 54 65 78 74  t n){.  /* zText
2399d 5b 5d 20 65 6e 63 6f 64 65 73 20 37 37 35 20 62  [] encodes 775 b
2399e 79 74 65 73 20 6f 66 20 6b 65 79 77 6f 72 64 73  ytes of keywords
2399f 20 69 6e 20 35 32 36 20 62 79 74 65 73 20 2a 2f   in 526 bytes */
239a0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
239a1 63 68 61 72 20 7a 54 65 78 74 5b 35 32 36 5d 20  char zText[526] 
239a2 3d 0a 20 20 20 20 22 42 45 46 4f 52 45 49 47 4e  =.    "BEFOREIGN
239a3 4f 52 45 47 45 58 50 4c 41 49 4e 53 54 45 41 44  OREGEXPLAINSTEAD
239a4 44 45 53 43 41 50 45 41 43 48 45 43 4b 45 59 43  DESCAPEACHECKEYC
239a5 4f 4e 53 54 52 41 49 4e 54 45 52 53 45 43 54 41  ONSTRAINTERSECTA
239a6 42 4c 45 46 54 22 0a 20 20 20 20 22 48 45 4e 44  BLEFT".    "HEND
239a7 41 54 41 42 41 53 45 4c 45 43 54 52 41 4e 53 41  ATABASELECTRANSA
239a8 43 54 49 4f 4e 41 54 55 52 41 4c 54 45 52 41 49  CTIONATURALTERAI
239a9 53 45 4c 53 45 58 43 45 50 54 52 49 47 47 45 52  SELSEXCEPTRIGGER
239aa 45 46 45 52 45 4e 43 45 53 22 0a 20 20 20 20 22  EFERENCES".    "
239ab 55 4e 49 51 55 45 52 59 41 54 54 41 43 48 41 56  UNIQUERYATTACHAV
239ac 49 4e 47 52 4f 55 50 44 41 54 45 4d 50 4f 52 41  INGROUPDATEMPORA
239ad 52 59 42 45 47 49 4e 4e 45 52 45 49 4e 44 45 58  RYBEGINNEREINDEX
239ae 43 4c 55 53 49 56 45 58 49 53 54 53 42 45 54 57  CLUSIVEXISTSBETW
239af 45 45 4e 22 0a 20 20 20 20 22 4f 54 4e 55 4c 4c  EEN".    "OTNULL
239b0 49 4b 45 43 41 53 43 41 44 45 46 45 52 52 41 42  IKECASCADEFERRAB
239b1 4c 45 43 41 53 45 43 4f 4c 4c 41 54 45 43 52 45  LECASECOLLATECRE
239b2 41 54 45 43 55 52 52 45 4e 54 5f 44 41 54 45 44  ATECURRENT_DATED
239b3 45 4c 45 54 45 44 45 54 41 43 48 22 0a 20 20 20  ELETEDETACH".   
239b4 20 22 49 4d 4d 45 44 49 41 54 45 4a 4f 49 4e 53   "IMMEDIATEJOINS
239b5 45 52 54 4d 41 54 43 48 50 4c 41 4e 41 4c 59 5a  ERTMATCHPLANALYZ
239b6 45 50 52 41 47 4d 41 42 4f 52 54 56 41 4c 55 45  EPRAGMABORTVALUE
239b7 53 56 49 52 54 55 41 4c 49 4d 49 54 57 48 45 4e  SVIRTUALIMITWHEN
239b8 22 0a 20 20 20 20 22 57 48 45 52 45 4e 41 4d 45  ".    "WHERENAME
239b9 41 46 54 45 52 45 50 4c 41 43 45 41 4e 44 45 46  AFTEREPLACEANDEF
239ba 41 55 4c 54 41 55 54 4f 49 4e 43 52 45 4d 45 4e  AULTAUTOINCREMEN
239bb 54 43 41 53 54 43 4f 4c 55 4d 4e 43 4f 4d 4d 49  TCASTCOLUMNCOMMI
239bc 54 43 4f 4e 46 4c 49 43 54 22 0a 20 20 20 20 22  TCONFLICT".    "
239bd 43 52 4f 53 53 43 55 52 52 45 4e 54 5f 54 49 4d  CROSSCURRENT_TIM
239be 45 53 54 41 4d 50 52 49 4d 41 52 59 44 45 46 45  ESTAMPRIMARYDEFE
239bf 52 52 45 44 49 53 54 49 4e 43 54 44 52 4f 50 46  RREDISTINCTDROPF
239c0 41 49 4c 46 52 4f 4d 46 55 4c 4c 47 4c 4f 42 22  AILFROMFULLGLOB"
239c1 0a 20 20 20 20 22 59 49 46 49 4e 54 4f 46 46 53  .    "YIFINTOFFS
239c2 45 54 49 53 4e 55 4c 4c 4f 52 44 45 52 45 53 54  ETISNULLORDEREST
239c3 52 49 43 54 4f 55 54 45 52 49 47 48 54 52 4f 4c  RICTOUTERIGHTROL
239c4 4c 42 41 43 4b 52 4f 57 55 4e 49 4f 4e 55 53 49  LBACKROWUNIONUSI
239c5 4e 47 56 41 43 55 55 4d 22 0a 20 20 20 20 22 56  NGVACUUM".    "V
239c6 49 45 57 49 4e 49 54 49 41 4c 4c 59 22 3b 0a 20  IEWINITIALLY";. 
239c7 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
239c8 73 69 67 6e 65 64 20 63 68 61 72 20 61 48 61 73  signed char aHas
239c9 68 5b 31 32 37 5d 20 3d 20 7b 0a 20 20 20 20 20  h[127] = {.     
239ca 20 36 33 2c 20 20 39 32 2c 20 31 30 39 2c 20 20   63,  92, 109,  
239cb 36 31 2c 20 20 20 30 2c 20 20 33 38 2c 20 20 20  61,   0,  38,   
239cc 30 2c 20 20 20 30 2c 20 20 36 39 2c 20 20 20 30  0,   0,  69,   0
239cd 2c 20 20 36 34 2c 20 20 20 30 2c 20 20 20 30 2c  ,  64,   0,   0,
239ce 0a 20 20 20 20 20 31 30 32 2c 20 20 20 34 2c 20  .     102,   4, 
239cf 20 36 35 2c 20 20 20 37 2c 20 20 20 30 2c 20 31   65,   7,   0, 1
239d0 30 38 2c 20 20 37 32 2c 20 31 30 33 2c 20 20 39  08,  72, 103,  9
239d1 39 2c 20 20 20 30 2c 20 20 32 32 2c 20 20 20 30  9,   0,  22,   0
239d2 2c 20 20 20 30 2c 0a 20 20 20 20 20 31 31 33 2c  ,   0,.     113,
239d3 20 20 20 30 2c 20 31 31 31 2c 20 31 30 36 2c 20     0, 111, 106, 
239d4 20 20 30 2c 20 20 31 38 2c 20 20 38 30 2c 20 20    0,  18,  80,  
239d5 20 30 2c 20 20 20 31 2c 20 20 20 30 2c 20 20 20   0,   1,   0,   
239d6 30 2c 20 20 35 36 2c 20 20 35 37 2c 0a 20 20 20  0,  56,  57,.   
239d7 20 20 20 20 30 2c 20 20 35 35 2c 20 20 31 31 2c      0,  55,  11,
239d8 20 20 20 30 2c 20 20 33 33 2c 20 20 37 37 2c 20     0,  33,  77, 
239d9 20 38 39 2c 20 20 20 30 2c 20 31 31 30 2c 20 20   89,   0, 110,  
239da 38 38 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 34  88,   0,   0,  4
239db 35 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 39 30  5,.       0,  90
239dc 2c 20 20 35 34 2c 20 20 20 30 2c 20 20 32 30 2c  ,  54,   0,  20,
239dd 20 20 20 30 2c 20 31 31 34 2c 20 20 33 34 2c 20     0, 114,  34, 
239de 20 31 39 2c 20 20 20 30 2c 20 20 31 30 2c 20 20   19,   0,  10,  
239df 39 37 2c 20 20 32 38 2c 0a 20 20 20 20 20 20 38  97,  28,.      8
239e0 33 2c 20 20 20 30 2c 20 20 20 30 2c 20 31 31 36  3,   0,   0, 116
239e1 2c 20 20 39 33 2c 20 20 34 37 2c 20 31 31 35 2c  ,  93,  47, 115,
239e2 20 20 34 31 2c 20 20 31 32 2c 20 20 34 34 2c 20    41,  12,  44, 
239e3 20 20 30 2c 20 20 37 38 2c 20 20 20 30 2c 0a 20    0,  78,   0,. 
239e4 20 20 20 20 20 38 37 2c 20 20 32 39 2c 20 20 20       87,  29,   
239e5 30 2c 20 20 38 36 2c 20 20 20 30 2c 20 20 20 30  0,  86,   0,   0
239e6 2c 20 20 20 30 2c 20 20 38 32 2c 20 20 37 39 2c  ,   0,  82,  79,
239e7 20 20 38 34 2c 20 20 37 35 2c 20 20 39 36 2c 20    84,  75,  96, 
239e8 20 20 36 2c 0a 20 20 20 20 20 20 31 34 2c 20 20    6,.      14,  
239e9 39 35 2c 20 20 20 30 2c 20 20 36 38 2c 20 20 20  95,   0,  68,   
239ea 30 2c 20 20 32 31 2c 20 20 37 36 2c 20 20 39 38  0,  21,  76,  98
239eb 2c 20 20 32 37 2c 20 20 20 30 2c 20 31 31 32 2c  ,  27,   0, 112,
239ec 20 20 36 37 2c 20 31 30 34 2c 0a 20 20 20 20 20    67, 104,.     
239ed 20 34 39 2c 20 20 34 30 2c 20 20 37 31 2c 20 20   49,  40,  71,  
239ee 20 30 2c 20 20 20 30 2c 20 20 38 31 2c 20 31 30   0,   0,  81, 10
239ef 30 2c 20 20 20 30 2c 20 31 30 37 2c 20 20 20 30  0,   0, 107,   0
239f0 2c 20 20 31 35 2c 20 20 20 30 2c 20 20 20 30 2c  ,  15,   0,   0,
239f1 0a 20 20 20 20 20 20 32 34 2c 20 20 20 30 2c 20  .      24,   0, 
239f2 20 37 33 2c 20 20 34 32 2c 20 20 35 30 2c 20 20   73,  42,  50,  
239f3 20 30 2c 20 20 31 36 2c 20 20 34 38 2c 20 20 20   0,  16,  48,   
239f4 30 2c 20 20 33 37 2c 0a 20 20 7d 3b 0a 20 20 73  0,  37,.  };.  s
239f5 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
239f6 67 6e 65 64 20 63 68 61 72 20 61 4e 65 78 74 5b  gned char aNext[
239f7 31 31 36 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  116] = {.       
239f8 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
239f9 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
239fa 20 20 20 30 2c 20 20 20 30 2c 20 20 20 39 2c 20     0,   0,   9, 
239fb 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20    0,   0,   0,. 
239fc 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20        0,   0,   
239fd 30 2c 20 20 20 30 2c 20 20 20 35 2c 20 20 20 30  0,   0,   5,   0
239fe 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
239ff 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
23a00 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20    0,.       0,  
23a01 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
23a02 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
23a03 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33 32 2c  ,   0,   0,  32,
23a04 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
23a05 20 31 37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   17,   0,   0,  
23a06 20 30 2c 20 20 33 36 2c 20 20 33 39 2c 20 20 20   0,  36,  39,   
23a07 30 2c 20 20 20 30 2c 20 20 32 35 2c 20 20 20 30  0,   0,  25,   0
23a08 2c 20 20 20 30 2c 20 20 33 31 2c 20 20 20 30 2c  ,   0,  31,   0,
23a09 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20  .       0,   0, 
23a0a 20 34 33 2c 20 20 35 32 2c 20 20 20 30 2c 20 20   43,  52,   0,  
23a0b 20 30 2c 20 20 20 30 2c 20 20 35 33 2c 20 20 20   0,   0,  53,   
23a0c 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
23a0d 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c  ,   0,.       0,
23a0e 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
23a0f 20 35 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   51,   0,   0,  
23a10 20 30 2c 20 20 20 30 2c 20 20 32 36 2c 20 20 20   0,   0,  26,   
23a11 30 2c 20 20 20 38 2c 20 20 34 36 2c 0a 20 20 20  0,   8,  46,.   
23a12 20 20 20 20 32 2c 20 20 20 30 2c 20 20 20 30 2c      2,   0,   0,
23a13 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
23a14 20 20 30 2c 20 20 20 30 2c 20 20 20 33 2c 20 20    0,   0,   3,  
23a15 35 38 2c 20 20 36 36 2c 20 20 20 30 2c 20 20 31  58,  66,   0,  1
23a16 33 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 39 31  3,.       0,  91
23a17 2c 20 20 38 35 2c 20 20 20 30 2c 20 20 39 34 2c  ,  85,   0,  94,
23a18 20 20 20 30 2c 20 20 37 34 2c 20 20 20 30 2c 20     0,  74,   0, 
23a19 20 20 30 2c 20 20 36 32 2c 20 20 20 30 2c 20 20    0,  62,   0,  
23a1a 33 35 2c 20 31 30 31 2c 0a 20 20 20 20 20 20 20  35, 101,.       
23a1b 30 2c 20 20 20 30 2c 20 31 30 35 2c 20 20 32 33  0,   0, 105,  23
23a1c 2c 20 20 33 30 2c 20 20 36 30 2c 20 20 37 30 2c  ,  30,  60,  70,
23a1d 20 20 20 30 2c 20 20 20 30 2c 20 20 35 39 2c 20     0,   0,  59, 
23a1e 20 20 30 2c 20 20 20 30 2c 0a 20 20 7d 3b 0a 20    0,   0,.  };. 
23a1f 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
23a20 73 69 67 6e 65 64 20 63 68 61 72 20 61 4c 65 6e  signed char aLen
23a21 5b 31 31 36 5d 20 3d 20 7b 0a 20 20 20 20 20 20  [116] = {.      
23a22 20 36 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20   6,   7,   3,   
23a23 36 2c 20 20 20 36 2c 20 20 20 37 2c 20 20 20 37  6,   6,   7,   7
23a24 2c 20 20 20 33 2c 20 20 20 34 2c 20 20 20 36 2c  ,   3,   4,   6,
23a25 20 20 20 34 2c 20 20 20 35 2c 20 20 20 33 2c 0a     4,   5,   3,.
23a26 20 20 20 20 20 20 31 30 2c 20 20 20 39 2c 20 20        10,   9,  
23a27 20 35 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20   5,   4,   4,   
23a28 33 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 36  3,   8,   2,   6
23a29 2c 20 20 31 31 2c 20 20 20 32 2c 20 20 20 37 2c  ,  11,   2,   7,
23a2a 20 20 20 35 2c 0a 20 20 20 20 20 20 20 35 2c 20     5,.       5, 
23a2b 20 20 34 2c 20 20 20 36 2c 20 20 20 37 2c 20 20    4,   6,   7,  
23a2c 31 30 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20  10,   6,   5,   
23a2d 36 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36  6,   6,   5,   6
23a2e 2c 20 20 20 34 2c 20 20 20 39 2c 0a 20 20 20 20  ,   4,   9,.    
23a2f 20 20 20 32 2c 20 20 20 35 2c 20 20 20 35 2c 20     2,   5,   5, 
23a30 20 20 37 2c 20 20 20 35 2c 20 20 20 39 2c 20 20    7,   5,   9,  
23a31 20 36 2c 20 20 20 37 2c 20 20 20 37 2c 20 20 20   6,   7,   7,   
23a32 33 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 37  3,   4,   4,   7
23a33 2c 0a 20 20 20 20 20 20 20 33 2c 20 20 31 30 2c  ,.       3,  10,
23a34 20 20 20 34 2c 20 20 20 37 2c 20 20 20 36 2c 20     4,   7,   6, 
23a35 20 31 32 2c 20 20 20 36 2c 20 20 20 36 2c 20 20   12,   6,   6,  
23a36 20 39 2c 20 20 20 34 2c 20 20 20 36 2c 20 20 20   9,   4,   6,   
23a37 35 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 37  5,   4,.       7
23a38 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c  ,   6,   5,   6,
23a39 20 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c 20     7,   5,   4, 
23a3a 20 20 35 2c 20 20 20 36 2c 20 20 20 35 2c 20 20    5,   6,   5,  
23a3b 20 37 2c 20 20 20 33 2c 20 20 20 37 2c 0a 20 20   7,   3,   7,.  
23a3c 20 20 20 20 31 33 2c 20 20 20 32 2c 20 20 20 32      13,   2,   2
23a3d 2c 20 20 20 34 2c 20 20 20 36 2c 20 20 20 36 2c  ,   4,   6,   6,
23a3e 20 20 20 38 2c 20 20 20 35 2c 20 20 31 37 2c 20     8,   5,  17, 
23a3f 20 31 32 2c 20 20 20 37 2c 20 20 20 38 2c 20 20   12,   7,   8,  
23a40 20 38 2c 0a 20 20 20 20 20 20 20 32 2c 20 20 20   8,.       2,   
23a41 34 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 34  4,   4,   4,   4
23a42 2c 20 20 20 34 2c 20 20 20 32 2c 20 20 20 32 2c  ,   4,   2,   2,
23a43 20 20 20 34 2c 20 20 20 36 2c 20 20 20 32 2c 20     4,   6,   2, 
23a44 20 20 33 2c 20 20 20 36 2c 0a 20 20 20 20 20 20    3,   6,.      
23a45 20 35 2c 20 20 20 38 2c 20 20 20 35 2c 20 20 20   5,   8,   5,   
23a46 35 2c 20 20 20 38 2c 20 20 20 33 2c 20 20 20 35  5,   8,   3,   5
23a47 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20 34 2c  ,   5,   6,   4,
23a48 20 20 20 39 2c 20 20 20 33 2c 0a 20 20 7d 3b 0a     9,   3,.  };.
23a49 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
23a4a 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
23a4b 74 20 61 4f 66 66 73 65 74 5b 31 31 36 5d 20 3d  t aOffset[116] =
23a4c 20 7b 0a 20 20 20 20 20 20 20 30 2c 20 20 20 32   {.       0,   2
23a4d 2c 20 20 20 32 2c 20 20 20 36 2c 20 20 31 30 2c  ,   2,   6,  10,
23a4e 20 20 31 33 2c 20 20 31 38 2c 20 20 32 33 2c 20    13,  18,  23, 
23a4f 20 32 35 2c 20 20 32 36 2c 20 20 33 31 2c 20 20   25,  26,  31,  
23a50 33 33 2c 20 20 33 37 2c 0a 20 20 20 20 20 20 34  33,  37,.      4
23a51 30 2c 20 20 34 37 2c 20 20 35 35 2c 20 20 35 38  0,  47,  55,  58
23a52 2c 20 20 36 31 2c 20 20 36 33 2c 20 20 36 35 2c  ,  61,  63,  65,
23a53 20 20 37 30 2c 20 20 37 31 2c 20 20 37 36 2c 20    70,  71,  76, 
23a54 20 38 35 2c 20 20 38 36 2c 20 20 39 31 2c 0a 20   85,  86,  91,. 
23a55 20 20 20 20 20 39 35 2c 20 20 39 39 2c 20 31 30       95,  99, 10
23a56 32 2c 20 31 30 37 2c 20 31 31 33 2c 20 31 32 33  2, 107, 113, 123
23a57 2c 20 31 32 36 2c 20 31 33 31 2c 20 31 33 36 2c  , 126, 131, 136,
23a58 20 31 34 31 2c 20 31 34 34 2c 20 31 34 38 2c 20   141, 144, 148, 
23a59 31 34 38 2c 0a 20 20 20 20 20 31 35 32 2c 20 31  148,.     152, 1
23a5a 35 37 2c 20 31 36 30 2c 20 31 36 34 2c 20 31 36  57, 160, 164, 16
23a5b 36 2c 20 31 36 39 2c 20 31 37 37 2c 20 31 38 33  6, 169, 177, 183
23a5c 2c 20 31 38 39 2c 20 31 38 39 2c 20 31 39 32 2c  , 189, 189, 192,
23a5d 20 31 39 35 2c 20 31 39 39 2c 0a 20 20 20 20 20   195, 199,.     
23a5e 32 30 30 2c 20 32 30 34 2c 20 32 31 34 2c 20 32  200, 204, 214, 2
23a5f 31 38 2c 20 32 32 35 2c 20 32 33 31 2c 20 32 34  18, 225, 231, 24
23a60 33 2c 20 32 34 39 2c 20 32 35 35 2c 20 32 36 34  3, 249, 255, 264
23a61 2c 20 32 36 36 2c 20 32 37 32 2c 20 32 37 37 2c  , 266, 272, 277,
23a62 0a 20 20 20 20 20 32 37 39 2c 20 32 38 36 2c 20  .     279, 286, 
23a63 32 39 31 2c 20 32 39 36 2c 20 33 30 32 2c 20 33  291, 296, 302, 3
23a64 30 38 2c 20 33 31 33 2c 20 33 31 37 2c 20 33 32  08, 313, 317, 32
23a65 30 2c 20 33 32 36 2c 20 33 33 30 2c 20 33 33 37  0, 326, 330, 337
23a66 2c 20 33 33 39 2c 0a 20 20 20 20 20 33 34 36 2c  , 339,.     346,
23a67 20 33 34 38 2c 20 33 35 30 2c 20 33 35 39 2c 20   348, 350, 359, 
23a68 33 36 33 2c 20 33 36 39 2c 20 33 37 35 2c 20 33  363, 369, 375, 3
23a69 38 33 2c 20 33 38 38 2c 20 33 38 38 2c 20 34 30  83, 388, 388, 40
23a6a 34 2c 20 34 31 31 2c 20 34 31 38 2c 0a 20 20 20  4, 411, 418,.   
23a6b 20 20 34 31 39 2c 20 34 32 36 2c 20 34 33 30 2c    419, 426, 430,
23a6c 20 34 33 34 2c 20 34 33 38 2c 20 34 34 32 2c 20   434, 438, 442, 
23a6d 34 34 35 2c 20 34 34 37 2c 20 34 34 39 2c 20 34  445, 447, 449, 4
23a6e 35 32 2c 20 34 35 32 2c 20 34 35 35 2c 20 34 35  52, 452, 455, 45
23a6f 38 2c 0a 20 20 20 20 20 34 36 34 2c 20 34 36 38  8,.     464, 468
23a70 2c 20 34 37 36 2c 20 34 38 30 2c 20 34 38 35 2c  , 476, 480, 485,
23a71 20 34 39 33 2c 20 34 39 36 2c 20 35 30 31 2c 20   493, 496, 501, 
23a72 35 30 36 2c 20 35 31 32 2c 20 35 31 36 2c 20 35  506, 512, 516, 5
23a73 32 31 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69  21,.  };.  stati
23a74 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
23a75 20 63 68 61 72 20 61 43 6f 64 65 5b 31 31 36 5d   char aCode[116]
23a76 20 3d 20 7b 0a 20 20 20 20 54 4b 5f 42 45 46 4f   = {.    TK_BEFO
23a77 52 45 2c 20 20 20 20 20 54 4b 5f 46 4f 52 45 49  RE,     TK_FOREI
23a78 47 4e 2c 20 20 20 20 54 4b 5f 46 4f 52 2c 20 20  GN,    TK_FOR,  
23a79 20 20 20 20 20 20 54 4b 5f 49 47 4e 4f 52 45 2c        TK_IGNORE,
23a7a 20 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c       TK_LIKE_KW,
23a7b 20 20 20 20 0a 20 20 20 20 54 4b 5f 45 58 50 4c      .    TK_EXPL
23a7c 41 49 4e 2c 20 20 20 20 54 4b 5f 49 4e 53 54 45  AIN,    TK_INSTE
23a7d 41 44 2c 20 20 20 20 54 4b 5f 41 44 44 2c 20 20  AD,    TK_ADD,  
23a7e 20 20 20 20 20 20 54 4b 5f 44 45 53 43 2c 20 20        TK_DESC,  
23a7f 20 20 20 20 20 54 4b 5f 45 53 43 41 50 45 2c 20       TK_ESCAPE, 
23a80 20 20 20 20 0a 20 20 20 20 54 4b 5f 45 41 43 48      .    TK_EACH
23a81 2c 20 20 20 20 20 20 20 54 4b 5f 43 48 45 43 4b  ,       TK_CHECK
23a82 2c 20 20 20 20 20 20 54 4b 5f 4b 45 59 2c 20 20  ,      TK_KEY,  
23a83 20 20 20 20 20 20 54 4b 5f 43 4f 4e 53 54 52 41        TK_CONSTRA
23a84 49 4e 54 2c 20 54 4b 5f 49 4e 54 45 52 53 45 43  INT, TK_INTERSEC
23a85 54 2c 20 20 0a 20 20 20 20 54 4b 5f 54 41 42 4c  T,  .    TK_TABL
23a86 45 2c 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f  E,      TK_JOIN_
23a87 4b 57 2c 20 20 20 20 54 4b 5f 54 48 45 4e 2c 20  KW,    TK_THEN, 
23a88 20 20 20 20 20 20 54 4b 5f 45 4e 44 2c 20 20 20        TK_END,   
23a89 20 20 20 20 20 54 4b 5f 44 41 54 41 42 41 53 45       TK_DATABASE
23a8a 2c 20 20 20 0a 20 20 20 20 54 4b 5f 41 53 2c 20  ,   .    TK_AS, 
23a8b 20 20 20 20 20 20 20 20 54 4b 5f 53 45 4c 45 43          TK_SELEC
23a8c 54 2c 20 20 20 20 20 54 4b 5f 54 52 41 4e 53 41  T,     TK_TRANSA
23a8d 43 54 49 4f 4e 2c 54 4b 5f 4f 4e 2c 20 20 20 20  CTION,TK_ON,    
23a8e 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c       TK_JOIN_KW,
23a8f 20 20 20 20 0a 20 20 20 20 54 4b 5f 41 4c 54 45      .    TK_ALTE
23a90 52 2c 20 20 20 20 20 20 54 4b 5f 52 41 49 53 45  R,      TK_RAISE
23a91 2c 20 20 20 20 20 20 54 4b 5f 45 4c 53 45 2c 20  ,      TK_ELSE, 
23a92 20 20 20 20 20 20 54 4b 5f 45 58 43 45 50 54 2c        TK_EXCEPT,
23a93 20 20 20 20 20 54 4b 5f 54 52 49 47 47 45 52 2c       TK_TRIGGER,
23a94 20 20 20 20 0a 20 20 20 20 54 4b 5f 52 45 46 45      .    TK_REFE
23a95 52 45 4e 43 45 53 2c 20 54 4b 5f 55 4e 49 51 55  RENCES, TK_UNIQU
23a96 45 2c 20 20 20 20 20 54 4b 5f 51 55 45 52 59 2c  E,     TK_QUERY,
23a97 20 20 20 20 20 20 54 4b 5f 41 54 54 41 43 48 2c        TK_ATTACH,
23a98 20 20 20 20 20 54 4b 5f 48 41 56 49 4e 47 2c 20       TK_HAVING, 
23a99 20 20 20 20 0a 20 20 20 20 54 4b 5f 47 52 4f 55      .    TK_GROU
23a9a 50 2c 20 20 20 20 20 20 54 4b 5f 55 50 44 41 54  P,      TK_UPDAT
23a9b 45 2c 20 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20  E,     TK_TEMP, 
23a9c 20 20 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20        TK_TEMP,  
23a9d 20 20 20 20 20 54 4b 5f 4f 52 2c 20 20 20 20 20       TK_OR,     
23a9e 20 20 20 20 0a 20 20 20 20 54 4b 5f 42 45 47 49      .    TK_BEGI
23a9f 4e 2c 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f  N,      TK_JOIN_
23aa0 4b 57 2c 20 20 20 20 54 4b 5f 52 45 49 4e 44 45  KW,    TK_REINDE
23aa1 58 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 2c 20  X,    TK_INDEX, 
23aa2 20 20 20 20 20 54 4b 5f 45 58 43 4c 55 53 49 56       TK_EXCLUSIV
23aa3 45 2c 20 20 0a 20 20 20 20 54 4b 5f 45 58 49 53  E,  .    TK_EXIS
23aa4 54 53 2c 20 20 20 20 20 54 4b 5f 42 45 54 57 45  TS,     TK_BETWE
23aa5 45 4e 2c 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c  EN,    TK_NOTNUL
23aa6 4c 2c 20 20 20 20 54 4b 5f 4e 4f 54 2c 20 20 20  L,    TK_NOT,   
23aa7 20 20 20 20 20 54 4b 5f 4e 55 4c 4c 2c 20 20 20       TK_NULL,   
23aa8 20 20 20 20 0a 20 20 20 20 54 4b 5f 4c 49 4b 45      .    TK_LIKE
23aa9 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 41 53 43 41  _KW,    TK_CASCA
23aaa 44 45 2c 20 20 20 20 54 4b 5f 41 53 43 2c 20 20  DE,    TK_ASC,  
23aab 20 20 20 20 20 20 54 4b 5f 44 45 46 45 52 52 41        TK_DEFERRA
23aac 42 4c 45 2c 20 54 4b 5f 43 41 53 45 2c 20 20 20  BLE, TK_CASE,   
23aad 20 20 20 20 0a 20 20 20 20 54 4b 5f 43 4f 4c 4c      .    TK_COLL
23aae 41 54 45 2c 20 20 20 20 54 4b 5f 43 52 45 41 54  ATE,    TK_CREAT
23aaf 45 2c 20 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f  E,     TK_CTIME_
23ab0 4b 57 2c 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c  KW,   TK_DELETE,
23ab1 20 20 20 20 20 54 4b 5f 44 45 54 41 43 48 2c 20       TK_DETACH, 
23ab2 20 20 20 20 0a 20 20 20 20 54 4b 5f 49 4d 4d 45      .    TK_IMME
23ab3 44 49 41 54 45 2c 20 20 54 4b 5f 4a 4f 49 4e 2c  DIATE,  TK_JOIN,
23ab4 20 20 20 20 20 20 20 54 4b 5f 49 4e 53 45 52 54         TK_INSERT
23ab5 2c 20 20 20 20 20 54 4b 5f 4d 41 54 43 48 2c 20  ,     TK_MATCH, 
23ab6 20 20 20 20 20 54 4b 5f 50 4c 41 4e 2c 20 20 20       TK_PLAN,   
23ab7 20 20 20 20 0a 20 20 20 20 54 4b 5f 41 4e 41 4c      .    TK_ANAL
23ab8 59 5a 45 2c 20 20 20 20 54 4b 5f 50 52 41 47 4d  YZE,    TK_PRAGM
23ab9 41 2c 20 20 20 20 20 54 4b 5f 41 42 4f 52 54 2c  A,     TK_ABORT,
23aba 20 20 20 20 20 20 54 4b 5f 56 41 4c 55 45 53 2c        TK_VALUES,
23abb 20 20 20 20 20 54 4b 5f 56 49 52 54 55 41 4c 2c       TK_VIRTUAL,
23abc 20 20 20 20 0a 20 20 20 20 54 4b 5f 4c 49 4d 49      .    TK_LIMI
23abd 54 2c 20 20 20 20 20 20 54 4b 5f 57 48 45 4e 2c  T,      TK_WHEN,
23abe 20 20 20 20 20 20 20 54 4b 5f 57 48 45 52 45 2c         TK_WHERE,
23abf 20 20 20 20 20 20 54 4b 5f 52 45 4e 41 4d 45 2c        TK_RENAME,
23ac0 20 20 20 20 20 54 4b 5f 41 46 54 45 52 2c 20 20       TK_AFTER,  
23ac1 20 20 20 20 0a 20 20 20 20 54 4b 5f 52 45 50 4c      .    TK_REPL
23ac2 41 43 45 2c 20 20 20 20 54 4b 5f 41 4e 44 2c 20  ACE,    TK_AND, 
23ac3 20 20 20 20 20 20 20 54 4b 5f 44 45 46 41 55 4c         TK_DEFAUL
23ac4 54 2c 20 20 20 20 54 4b 5f 41 55 54 4f 49 4e 43  T,    TK_AUTOINC
23ac5 52 2c 20 20 20 54 4b 5f 54 4f 2c 20 20 20 20 20  R,   TK_TO,     
23ac6 20 20 20 20 0a 20 20 20 20 54 4b 5f 49 4e 2c 20      .    TK_IN, 
23ac7 20 20 20 20 20 20 20 20 54 4b 5f 43 41 53 54 2c          TK_CAST,
23ac8 20 20 20 20 20 20 20 54 4b 5f 43 4f 4c 55 4d 4e         TK_COLUMN
23ac9 4b 57 2c 20 20 20 54 4b 5f 43 4f 4d 4d 49 54 2c  KW,   TK_COMMIT,
23aca 20 20 20 20 20 54 4b 5f 43 4f 4e 46 4c 49 43 54       TK_CONFLICT
23acb 2c 20 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e  ,   .    TK_JOIN
23acc 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 54 49 4d 45  _KW,    TK_CTIME
23acd 5f 4b 57 2c 20 20 20 54 4b 5f 43 54 49 4d 45 5f  _KW,   TK_CTIME_
23ace 4b 57 2c 20 20 20 54 4b 5f 50 52 49 4d 41 52 59  KW,   TK_PRIMARY
23acf 2c 20 20 20 20 54 4b 5f 44 45 46 45 52 52 45 44  ,    TK_DEFERRED
23ad0 2c 20 20 20 0a 20 20 20 20 54 4b 5f 44 49 53 54  ,   .    TK_DIST
23ad1 49 4e 43 54 2c 20 20 20 54 4b 5f 49 53 2c 20 20  INCT,   TK_IS,  
23ad2 20 20 20 20 20 20 20 54 4b 5f 44 52 4f 50 2c 20         TK_DROP, 
23ad3 20 20 20 20 20 20 54 4b 5f 46 41 49 4c 2c 20 20        TK_FAIL,  
23ad4 20 20 20 20 20 54 4b 5f 46 52 4f 4d 2c 20 20 20       TK_FROM,   
23ad5 20 20 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e      .    TK_JOIN
23ad6 5f 4b 57 2c 20 20 20 20 54 4b 5f 4c 49 4b 45 5f  _KW,    TK_LIKE_
23ad7 4b 57 2c 20 20 20 20 54 4b 5f 42 59 2c 20 20 20  KW,    TK_BY,   
23ad8 20 20 20 20 20 20 54 4b 5f 49 46 2c 20 20 20 20        TK_IF,    
23ad9 20 20 20 20 20 54 4b 5f 49 4e 54 4f 2c 20 20 20       TK_INTO,   
23ada 20 20 20 20 0a 20 20 20 20 54 4b 5f 4f 46 46 53      .    TK_OFFS
23adb 45 54 2c 20 20 20 20 20 54 4b 5f 4f 46 2c 20 20  ET,     TK_OF,  
23adc 20 20 20 20 20 20 20 54 4b 5f 53 45 54 2c 20 20         TK_SET,  
23add 20 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 2c        TK_ISNULL,
23ade 20 20 20 20 20 54 4b 5f 4f 52 44 45 52 2c 20 20       TK_ORDER,  
23adf 20 20 20 20 0a 20 20 20 20 54 4b 5f 52 45 53 54      .    TK_REST
23ae0 52 49 43 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f  RICT,   TK_JOIN_
23ae1 4b 57 2c 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b  KW,    TK_JOIN_K
23ae2 57 2c 20 20 20 20 54 4b 5f 52 4f 4c 4c 42 41 43  W,    TK_ROLLBAC
23ae3 4b 2c 20 20 20 54 4b 5f 52 4f 57 2c 20 20 20 20  K,   TK_ROW,    
23ae4 20 20 20 20 0a 20 20 20 20 54 4b 5f 55 4e 49 4f      .    TK_UNIO
23ae5 4e 2c 20 20 20 20 20 20 54 4b 5f 55 53 49 4e 47  N,      TK_USING
23ae6 2c 20 20 20 20 20 20 54 4b 5f 56 41 43 55 55 4d  ,      TK_VACUUM
23ae7 2c 20 20 20 20 20 54 4b 5f 56 49 45 57 2c 20 20  ,     TK_VIEW,  
23ae8 20 20 20 20 20 54 4b 5f 49 4e 49 54 49 41 4c 4c       TK_INITIALL
23ae9 59 2c 20 20 0a 20 20 20 20 54 4b 5f 41 4c 4c 2c  Y,  .    TK_ALL,
23aea 20 20 20 20 20 20 20 20 0a 20 20 7d 3b 0a 20 20          .  };.  
23aeb 69 6e 74 20 68 2c 20 69 3b 0a 20 20 69 66 28 20  int h, i;.  if( 
23aec 6e 3c 32 20 29 20 72 65 74 75 72 6e 20 54 4b 5f  n<2 ) return TK_
23aed 49 44 3b 0a 20 20 68 20 3d 20 28 28 63 68 61 72  ID;.  h = ((char
23aee 4d 61 70 28 7a 5b 30 5d 29 2a 34 29 20 5e 0a 20  Map(z[0])*4) ^. 
23aef 20 20 20 20 20 28 63 68 61 72 4d 61 70 28 7a 5b       (charMap(z[
23af0 6e 2d 31 5d 29 2a 33 29 20 5e 0a 20 20 20 20 20  n-1])*3) ^.     
23af1 20 6e 29 20 25 20 31 32 37 3b 0a 20 20 66 6f 72   n) % 127;.  for
23af2 28 69 3d 28 28 69 6e 74 29 61 48 61 73 68 5b 68  (i=((int)aHash[h
23af3 5d 29 2d 31 3b 20 69 3e 3d 30 3b 20 69 3d 28 28  ])-1; i>=0; i=((
23af4 69 6e 74 29 61 4e 65 78 74 5b 69 5d 29 2d 31 29  int)aNext[i])-1)
23af5 7b 0a 20 20 20 20 69 66 28 20 61 4c 65 6e 5b 69  {.    if( aLen[i
23af6 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53  ]==n && sqlite3S
23af7 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 61  trNICmp(&zText[a
23af8 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d  Offset[i]],z,n)=
23af9 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
23afa 72 6e 20 61 43 6f 64 65 5b 69 5d 3b 0a 20 20 20  rn aCode[i];.   
23afb 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
23afc 54 4b 5f 49 44 3b 0a 7d 0a 53 51 4c 49 54 45 5f  TK_ID;.}.SQLITE_
23afd 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
23afe 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63  te3KeywordCode(c
23aff 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
23b00 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *z, int n){. 
23b01 20 72 65 74 75 72 6e 20 6b 65 79 77 6f 72 64 43   return keywordC
23b02 6f 64 65 28 28 63 68 61 72 2a 29 7a 2c 20 6e 29  ode((char*)z, n)
23b03 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
23b04 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6b 65 79 77  **** End of keyw
23b05 6f 72 64 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  ordhash.h ******
23b06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b08 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
23b09 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
23b0a 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
23b0b 66 20 69 6e 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  f in tokenize.c 
23b0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b0d 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  ***/.../*.** If 
23b0e 58 20 69 73 20 61 20 63 68 61 72 61 63 74 65 72  X is a character
23b0f 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
23b10 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69 66 69  d in an identifi
23b11 65 72 20 74 68 65 6e 0a 2a 2a 20 49 64 43 68 61  er then.** IdCha
23b12 72 28 58 29 20 77 69 6c 6c 20 62 65 20 74 72 75  r(X) will be tru
23b13 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74  e.  Otherwise it
23b14 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a   is false..**.**
23b15 20 46 6f 72 20 41 53 43 49 49 2c 20 61 6e 79 20   For ASCII, any 
23b16 63 68 61 72 61 63 74 65 72 20 77 69 74 68 20 74  character with t
23b17 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69  he high-order bi
23b18 74 20 73 65 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f  t set is.** allo
23b19 77 65 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69  wed in an identi
23b1a 66 69 65 72 2e 20 20 46 6f 72 20 37 2d 62 69 74  fier.  For 7-bit
23b1b 20 63 68 61 72 61 63 74 65 72 73 2c 20 0a 2a 2a   characters, .**
23b1c 20 73 71 6c 69 74 65 33 49 73 49 64 43 68 61 72   sqlite3IsIdChar
23b1d 5b 58 5d 20 6d 75 73 74 20 62 65 20 31 2e 0a 2a  [X] must be 1..*
23b1e 2a 0a 2a 2a 20 46 6f 72 20 45 42 43 44 49 43 2c  *.** For EBCDIC,
23b1f 20 74 68 65 20 72 75 6c 65 73 20 61 72 65 20 6d   the rules are m
23b20 6f 72 65 20 63 6f 6d 70 6c 65 78 20 62 75 74 20  ore complex but 
23b21 68 61 76 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a  have the same.**
23b22 20 65 6e 64 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a   end result..**.
23b23 2a 2a 20 54 69 63 6b 65 74 20 23 31 30 36 36 2e  ** Ticket #1066.
23b24 20 20 74 68 65 20 53 51 4c 20 73 74 61 6e 64 61    the SQL standa
23b25 72 64 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f  rd does not allo
23b26 77 20 27 24 27 20 69 6e 20 74 68 65 0a 2a 2a 20  w '$' in the.** 
23b27 6d 69 64 64 6c 65 20 6f 66 20 69 64 65 6e 74 66  middle of identf
23b28 69 65 72 73 2e 20 20 42 75 74 20 6d 61 6e 79 20  iers.  But many 
23b29 53 51 4c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  SQL implementati
23b2a 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20 53 51 4c 69  ons do. .** SQLi
23b2b 74 65 20 77 69 6c 6c 20 61 6c 6c 6f 77 20 27 24  te will allow '$
23b2c 27 20 69 6e 20 69 64 65 6e 74 69 66 69 65 72 73  ' in identifiers
23b2d 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69   for compatibili
23b2e 74 79 2e 0a 2a 2a 20 42 75 74 20 74 68 65 20 66  ty..** But the f
23b2f 65 61 74 75 72 65 20 69 73 20 75 6e 64 6f 63 75  eature is undocu
23b30 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65  mented..*/.#ifde
23b31 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53  f SQLITE_ASCII.S
23b32 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
23b33 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33  nst char sqlite3
23b34 49 73 41 73 63 69 69 49 64 43 68 61 72 5b 5d 20  IsAsciiIdChar[] 
23b35 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
23b36 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
23b37 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
23b38 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c   xE xF */.    0,
23b39 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20   0, 0, 0, 1, 0, 
23b3a 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
23b3b 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
23b3c 2a 20 32 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * 2x */.    1, 1
23b3d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
23b3e 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
23b3f 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
23b40 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  3x */.    0, 1, 
23b41 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
23b42 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
23b43 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78   1, 1, 1,  /* 4x
23b44 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
23b45 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
23b46 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
23b47 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a  , 0, 1,  /* 5x *
23b48 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
23b49 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
23b4a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
23b4b 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a  1, 1,  /* 6x */.
23b4c 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
23b4d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
23b4e 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
23b4f 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b   0,  /* 7x */.};
23b50 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
23b51 43 29 20 20 28 28 28 63 3d 43 29 26 30 78 38 30  C)  (((c=C)&0x80
23b52 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66 20  )!=0 || (c>0x1f 
23b53 26 26 20 73 71 6c 69 74 65 33 49 73 41 73 63 69  && sqlite3IsAsci
23b54 69 49 64 43 68 61 72 5b 63 2d 30 78 32 30 5d 29  iIdChar[c-0x20])
23b55 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ).#endif.#ifdef 
23b56 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 53 51  SQLITE_EBCDIC.SQ
23b57 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
23b58 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 49  st char sqlite3I
23b59 73 45 62 63 64 69 63 49 64 43 68 61 72 5b 5d 20  sEbcdicIdChar[] 
23b5a 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
23b5b 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
23b5c 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
23b5d 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c   xE xF */.    0,
23b5e 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
23b5f 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
23b60 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
23b61 2a 20 34 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 4x */.    0, 1
23b62 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
23b63 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
23b64 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
23b65 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  5x */.    0, 0, 
23b66 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
23b67 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
23b68 20 31 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 36 78   1, 0, 0,  /* 6x
23b69 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
23b6a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
23b6b 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
23b6c 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a  , 0, 0,  /* 7x *
23b6d 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
23b6e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
23b6f 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 0, 0, 1, 1, 
23b70 31 2c 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f 0a  1, 0,  /* 8x */.
23b71 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
23b72 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
23b73 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 31 2c  , 0, 0, 1, 0, 1,
23b74 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a 20 20   0,  /* 9x */.  
23b75 20 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c    1, 0, 1, 1, 1,
23b76 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
23b77 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 0, 1, 1, 1, 0
23b78 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20  ,  /* Ax */.    
23b79 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
23b7a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
23b7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
23b7c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 30 2c   /* Bx */.    0,
23b7d 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
23b7e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
23b7f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
23b80 2a 20 43 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * Cx */.    0, 1
23b81 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
23b82 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
23b83 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
23b84 44 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  Dx */.    0, 0, 
23b85 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
23b86 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 1, 0, 1, 1,
23b87 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 45 78   1, 1, 1,  /* Ex
23b88 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
23b89 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
23b8a 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
23b8b 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 46 78 20 2a  , 1, 0,  /* Fx *
23b8c 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43  /.};.#define IdC
23b8d 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29 3e  har(C)  (((c=C)>
23b8e 3d 30 78 34 32 20 26 26 20 73 71 6c 69 74 65 33  =0x42 && sqlite3
23b8f 49 73 45 62 63 64 69 63 49 64 43 68 61 72 5b 63  IsEbcdicIdChar[c
23b90 2d 30 78 34 30 5d 29 29 0a 23 65 6e 64 69 66 0a  -0x40])).#endif.
23b91 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
23b92 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
23b93 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69   token that begi
23b94 6e 73 20 61 74 20 7a 5b 30 5d 2e 20 0a 2a 2a 20  ns at z[0]. .** 
23b95 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20  Store the token 
23b96 74 79 70 65 20 69 6e 20 2a 74 6f 6b 65 6e 54 79  type in *tokenTy
23b97 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  pe before return
23b98 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
23b99 6e 74 20 67 65 74 54 6f 6b 65 6e 28 63 6f 6e 73  nt getToken(cons
23b9a 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
23b9b 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e 54 79  *z, int *tokenTy
23b9c 70 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b  pe){.  int i, c;
23b9d 0a 20 20 73 77 69 74 63 68 28 20 2a 7a 20 29 7b  .  switch( *z ){
23b9e 0a 20 20 20 20 63 61 73 65 20 27 20 27 3a 20 63  .    case ' ': c
23b9f 61 73 65 20 27 5c 74 27 3a 20 63 61 73 65 20 27  ase '\t': case '
23ba0 5c 6e 27 3a 20 63 61 73 65 20 27 5c 66 27 3a 20  \n': case '\f': 
23ba1 63 61 73 65 20 27 5c 72 27 3a 20 7b 0a 20 20 20  case '\r': {.   
23ba2 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 73 73 70     for(i=1; issp
23ba3 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  ace(z[i]); i++){
23ba4 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  }.      *tokenTy
23ba5 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20  pe = TK_SPACE;. 
23ba6 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
23ba7 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2d     }.    case '-
23ba8 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a  ': {.      if( z
23ba9 5b 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20  [1]=='-' ){.    
23baa 20 20 20 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d      for(i=2; (c=
23bab 7a 5b 69 5d 29 21 3d 30 20 26 26 20 63 21 3d 27  z[i])!=0 && c!='
23bac 5c 6e 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  \n'; i++){}.    
23bad 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
23bae 20 54 4b 5f 43 4f 4d 4d 45 4e 54 3b 0a 20 20 20   TK_COMMENT;.   
23baf 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
23bb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 74 6f       }.      *to
23bb1 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4d 49 4e  kenType = TK_MIN
23bb2 55 53 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  US;.      return
23bb3 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
23bb4 73 65 20 27 28 27 3a 20 7b 0a 20 20 20 20 20 20  se '(': {.      
23bb5 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
23bb6 4c 50 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  LP;.      return
23bb7 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
23bb8 73 65 20 27 29 27 3a 20 7b 0a 20 20 20 20 20 20  se ')': {.      
23bb9 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
23bba 52 50 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  RP;.      return
23bbb 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
23bbc 73 65 20 27 3b 27 3a 20 7b 0a 20 20 20 20 20 20  se ';': {.      
23bbd 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
23bbe 53 45 4d 49 3b 0a 20 20 20 20 20 20 72 65 74 75  SEMI;.      retu
23bbf 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
23bc0 63 61 73 65 20 27 2b 27 3a 20 7b 0a 20 20 20 20  case '+': {.    
23bc1 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
23bc2 4b 5f 50 4c 55 53 3b 0a 20 20 20 20 20 20 72 65  K_PLUS;.      re
23bc3 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
23bc4 20 20 63 61 73 65 20 27 2a 27 3a 20 7b 0a 20 20    case '*': {.  
23bc5 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
23bc6 20 54 4b 5f 53 54 41 52 3b 0a 20 20 20 20 20 20   TK_STAR;.      
23bc7 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
23bc8 20 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b 0a      case '/': {.
23bc9 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d        if( z[1]!=
23bca 27 2a 27 20 7c 7c 20 7a 5b 32 5d 3d 3d 30 20 29  '*' || z[2]==0 )
23bcb 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
23bcc 54 79 70 65 20 3d 20 54 4b 5f 53 4c 41 53 48 3b  Type = TK_SLASH;
23bcd 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
23bce 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
23bcf 20 66 6f 72 28 69 3d 33 2c 20 63 3d 7a 5b 32 5d   for(i=3, c=z[2]
23bd0 3b 20 28 63 21 3d 27 2a 27 20 7c 7c 20 7a 5b 69  ; (c!='*' || z[i
23bd1 5d 21 3d 27 2f 27 29 20 26 26 20 28 63 3d 7a 5b  ]!='/') && (c=z[
23bd2 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20  i])!=0; i++){}. 
23bd3 20 20 20 20 20 69 66 28 20 63 20 29 20 69 2b 2b       if( c ) i++
23bd4 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  ;.      *tokenTy
23bd5 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 45 4e 54 3b  pe = TK_COMMENT;
23bd6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
23bd7 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
23bd8 27 25 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '%': {.      *to
23bd9 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45 4d  kenType = TK_REM
23bda 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
23bdb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
23bdc 20 27 3d 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74   '=': {.      *t
23bdd 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 45 51  okenType = TK_EQ
23bde 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
23bdf 20 2b 20 28 7a 5b 31 5d 3d 3d 27 3d 27 29 3b 0a   + (z[1]=='=');.
23be0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
23be1 3c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  <': {.      if( 
23be2 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b  (c=z[1])=='=' ){
23be3 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
23be4 79 70 65 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20  ype = TK_LE;.   
23be5 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
23be6 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
23be7 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20 20  =='>' ){.       
23be8 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
23be9 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74  _NE;.        ret
23bea 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 2;.      }el
23beb 73 65 20 69 66 28 20 63 3d 3d 27 3c 27 20 29 7b  se if( c=='<' ){
23bec 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
23bed 79 70 65 20 3d 20 54 4b 5f 4c 53 48 49 46 54 3b  ype = TK_LSHIFT;
23bee 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
23bef 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  2;.      }else{.
23bf0 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
23bf1 70 65 20 3d 20 54 4b 5f 4c 54 3b 0a 20 20 20 20  pe = TK_LT;.    
23bf2 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
23bf3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
23bf4 63 61 73 65 20 27 3e 27 3a 20 7b 0a 20 20 20 20  case '>': {.    
23bf5 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d    if( (c=z[1])==
23bf6 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  '=' ){.        *
23bf7 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47  tokenType = TK_G
23bf8 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  E;.        retur
23bf9 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 2;.      }else
23bfa 20 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20   if( c=='>' ){. 
23bfb 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
23bfc 65 20 3d 20 54 4b 5f 52 53 48 49 46 54 3b 0a 20  e = TK_RSHIFT;. 
23bfd 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
23bfe 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
23bff 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
23c00 20 3d 20 54 4b 5f 47 54 3b 0a 20 20 20 20 20 20   = TK_GT;.      
23c01 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
23c02 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61    }.    }.    ca
23c03 73 65 20 27 21 27 3a 20 7b 0a 20 20 20 20 20 20  se '!': {.      
23c04 69 66 28 20 7a 5b 31 5d 21 3d 27 3d 27 20 29 7b  if( z[1]!='=' ){
23c05 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
23c06 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c  ype = TK_ILLEGAL
23c07 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
23c08 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   2;.      }else{
23c09 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
23c0a 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20  ype = TK_NE;.   
23c0b 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
23c0c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
23c0d 20 63 61 73 65 20 27 7c 27 3a 20 7b 0a 20 20 20   case '|': {.   
23c0e 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 7c 27     if( z[1]!='|'
23c0f 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
23c10 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 4f  enType = TK_BITO
23c11 52 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  R;.        retur
23c12 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 1;.      }else
23c13 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
23c14 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4e 43 41 54  Type = TK_CONCAT
23c15 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
23c16 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   2;.      }.    
23c17 7d 0a 20 20 20 20 63 61 73 65 20 27 2c 27 3a 20  }.    case ',': 
23c18 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
23c19 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 41 3b 0a 20  pe = TK_COMMA;. 
23c1a 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
23c1b 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 26     }.    case '&
23c1c 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
23c1d 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 41 4e  nType = TK_BITAN
23c1e 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  D;.      return 
23c1f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
23c20 65 20 27 7e 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '~': {.      *
23c21 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42  tokenType = TK_B
23c22 49 54 4e 4f 54 3b 0a 20 20 20 20 20 20 72 65 74  ITNOT;.      ret
23c23 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
23c24 20 63 61 73 65 20 27 60 27 3a 0a 20 20 20 20 63   case '`':.    c
23c25 61 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 63 61  ase '\'':.    ca
23c26 73 65 20 27 22 27 3a 20 7b 0a 20 20 20 20 20 20  se '"': {.      
23c27 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30 5d  int delim = z[0]
23c28 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  ;.      for(i=1;
23c29 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b   (c=z[i])!=0; i+
23c2a 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
23c2b 63 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20  c==delim ){.    
23c2c 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d        if( z[i+1]
23c2d 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20  ==delim ){.     
23c2e 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20         i++;.    
23c2f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
23c30 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
23c31 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
23c32 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
23c33 20 20 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20      if( c ){.   
23c34 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
23c35 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20  = TK_STRING;.   
23c36 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b       return i+1;
23c37 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
23c38 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
23c39 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
23c3a 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b         return i;
23c3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
23c3c 20 20 20 63 61 73 65 20 27 2e 27 3a 20 7b 0a 23     case '.': {.#
23c3d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23c3e 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
23c3f 54 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 64  T.      if( !isd
23c40 69 67 69 74 28 7a 5b 31 5d 29 20 29 0a 23 65 6e  igit(z[1]) ).#en
23c41 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  dif.      {.    
23c42 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
23c43 20 54 4b 5f 44 4f 54 3b 0a 20 20 20 20 20 20 20   TK_DOT;.       
23c44 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
23c45 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74   }.      /* If t
23c46 68 65 20 6e 65 78 74 20 63 68 61 72 61 63 74 65  he next characte
23c47 72 20 69 73 20 61 20 64 69 67 69 74 2c 20 74 68  r is a digit, th
23c48 69 73 20 69 73 20 61 20 66 6c 6f 61 74 69 6e 67  is is a floating
23c49 20 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20   point.      ** 
23c4a 6e 75 6d 62 65 72 20 74 68 61 74 20 62 65 67 69  number that begi
23c4b 6e 73 20 77 69 74 68 20 22 2e 22 2e 20 20 46 61  ns with ".".  Fa
23c4c 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65  ll thru into the
23c4d 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20   next case */.  
23c4e 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 30 27    }.    case '0'
23c4f 3a 20 63 61 73 65 20 27 31 27 3a 20 63 61 73 65  : case '1': case
23c50 20 27 32 27 3a 20 63 61 73 65 20 27 33 27 3a 20   '2': case '3': 
23c51 63 61 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61  case '4':.    ca
23c52 73 65 20 27 35 27 3a 20 63 61 73 65 20 27 36 27  se '5': case '6'
23c53 3a 20 63 61 73 65 20 27 37 27 3a 20 63 61 73 65  : case '7': case
23c54 20 27 38 27 3a 20 63 61 73 65 20 27 39 27 3a 20   '8': case '9': 
23c55 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
23c56 70 65 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52 3b  pe = TK_INTEGER;
23c57 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
23c58 69 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69  isdigit(z[i]); i
23c59 2b 2b 29 7b 7d 0a 23 69 66 6e 64 65 66 20 53 51  ++){}.#ifndef SQ
23c5a 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
23c5b 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69  NG_POINT.      i
23c5c 66 28 20 7a 5b 69 5d 3d 3d 27 2e 27 20 29 7b 0a  f( z[i]=='.' ){.
23c5d 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
23c5e 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69       while( isdi
23c5f 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b  git(z[i]) ){ i++
23c60 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  ; }.        *tok
23c61 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41  enType = TK_FLOA
23c62 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  T;.      }.     
23c63 20 69 66 28 20 28 7a 5b 69 5d 3d 3d 27 65 27 20   if( (z[i]=='e' 
23c64 7c 7c 20 7a 5b 69 5d 3d 3d 27 45 27 29 20 26 26  || z[i]=='E') &&
23c65 0a 20 20 20 20 20 20 20 20 20 20 20 28 20 69 73  .           ( is
23c66 64 69 67 69 74 28 7a 5b 69 2b 31 5d 29 20 0a 20  digit(z[i+1]) . 
23c67 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28             || ((
23c68 7a 5b 69 2b 31 5d 3d 3d 27 2b 27 20 7c 7c 20 7a  z[i+1]=='+' || z
23c69 5b 69 2b 31 5d 3d 3d 27 2d 27 29 20 26 26 20 69  [i+1]=='-') && i
23c6a 73 64 69 67 69 74 28 7a 5b 69 2b 32 5d 29 29 0a  sdigit(z[i+2])).
23c6b 20 20 20 20 20 20 20 20 20 20 20 29 0a 20 20 20             ).   
23c6c 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20     ){.        i 
23c6d 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 77 68  += 2;.        wh
23c6e 69 6c 65 28 20 69 73 64 69 67 69 74 28 7a 5b 69  ile( isdigit(z[i
23c6f 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 20  ]) ){ i++; }.   
23c70 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
23c71 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a 20 20 20 20  = TK_FLOAT;.    
23c72 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
23c73 20 77 68 69 6c 65 28 20 49 64 43 68 61 72 28 7a   while( IdChar(z
23c74 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20  [i]) ){.        
23c75 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
23c76 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20  ILLEGAL;.       
23c77 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20   i++;.      }.  
23c78 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
23c79 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 5b 27    }.    case '['
23c7a 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  : {.      for(i=
23c7b 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27 5d  1, c=z[0]; c!=']
23c7c 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30  ' && (c=z[i])!=0
23c7d 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a  ; i++){}.      *
23c7e 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
23c7f 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  D;.      return 
23c80 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
23c81 65 20 27 3f 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '?': {.      *
23c82 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56  tokenType = TK_V
23c83 41 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66  ARIABLE;.      f
23c84 6f 72 28 69 3d 31 3b 20 69 73 64 69 67 69 74 28  or(i=1; isdigit(
23c85 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20  z[i]); i++){}.  
23c86 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
23c87 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 23 27    }.    case '#'
23c88 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  : {.      for(i=
23c89 31 3b 20 69 73 64 69 67 69 74 28 7a 5b 69 5d 29  1; isdigit(z[i])
23c8a 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 69  ; i++){}.      i
23c8b 66 28 20 69 3e 31 20 29 7b 0a 20 20 20 20 20 20  f( i>1 ){.      
23c8c 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20    /* Parameters 
23c8d 6f 66 20 74 68 65 20 66 6f 72 6d 20 23 4e 4e 4e  of the form #NNN
23c8e 20 28 77 68 65 72 65 20 4e 4e 4e 20 69 73 20 61   (where NNN is a
23c8f 20 6e 75 6d 62 65 72 29 20 61 72 65 20 75 73 65   number) are use
23c90 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74  d.        ** int
23c91 65 72 6e 61 6c 6c 79 20 62 79 20 73 71 6c 69 74  ernally by sqlit
23c92 65 33 4e 65 73 74 65 64 50 61 72 73 65 2e 20 20  e3NestedParse.  
23c93 2a 2f 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  */.        *toke
23c94 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45 47 49 53  nType = TK_REGIS
23c95 54 45 52 3b 0a 20 20 20 20 20 20 20 20 72 65 74  TER;.        ret
23c96 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
23c97 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72       /* Fall thr
23c98 6f 75 67 68 20 69 6e 74 6f 20 74 68 65 20 6e 65  ough into the ne
23c99 78 74 20 63 61 73 65 20 69 66 20 74 68 65 20 27  xt case if the '
23c9a 23 27 20 69 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77  #' is not follow
23c9b 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 61  ed by.      ** a
23c9c 20 64 69 67 69 74 2e 20 54 72 79 20 74 6f 20 6d   digit. Try to m
23c9d 61 74 63 68 20 23 41 41 41 41 20 77 68 65 72 65  atch #AAAA where
23c9e 20 41 41 41 41 20 69 73 20 61 20 70 61 72 61 6d   AAAA is a param
23c9f 65 74 65 72 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20  eter name. */.  
23ca0 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
23ca1 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49  TE_OMIT_TCL_VARI
23ca2 41 42 4c 45 0a 20 20 20 20 63 61 73 65 20 27 24  ABLE.    case '$
23ca3 27 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61  ':.#endif.    ca
23ca4 73 65 20 27 40 27 3a 20 20 2f 2a 20 46 6f 72 20  se '@':  /* For 
23ca5 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69  compatibility wi
23ca6 74 68 20 4d 53 20 53 51 4c 20 53 65 72 76 65 72  th MS SQL Server
23ca7 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 3a 27   */.    case ':'
23ca8 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20  : {.      int n 
23ca9 3d 20 30 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  = 0;.      *toke
23caa 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41 52 49 41  nType = TK_VARIA
23cab 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  BLE;.      for(i
23cac 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b  =1; (c=z[i])!=0;
23cad 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
23cae 66 28 20 49 64 43 68 61 72 28 63 29 20 29 7b 0a  f( IdChar(c) ){.
23caf 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a 23            n++;.#
23cb0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23cb1 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a  IT_TCL_VARIABLE.
23cb2 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
23cb3 28 20 63 3d 3d 27 28 27 20 26 26 20 6e 3e 30 20  ( c=='(' && n>0 
23cb4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f 7b  ){.          do{
23cb5 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
23cb6 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69  ;.          }whi
23cb7 6c 65 28 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20  le( (c=z[i])!=0 
23cb8 26 26 20 21 69 73 73 70 61 63 65 28 63 29 20 26  && !isspace(c) &
23cb9 26 20 63 21 3d 27 29 27 20 29 3b 0a 20 20 20 20  & c!=')' );.    
23cba 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 29 27        if( c==')'
23cbb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
23cbc 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  i++;.          }
23cbd 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
23cbe 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
23cbf 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
23cc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
23cc1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
23cc2 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3a 27  }else if( c==':'
23cc3 20 26 26 20 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20   && z[i+1]==':' 
23cc4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 2b 2b  ){.          i++
23cc5 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
23cc6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
23cc7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
23cc8 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
23cc9 20 69 66 28 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b   if( n==0 ) *tok
23cca 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
23ccb 47 41 4c 3b 0a 20 20 20 20 20 20 72 65 74 75 72  GAL;.      retur
23ccc 6e 20 69 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  n i;.    }.#ifnd
23ccd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
23cce 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20  LOB_LITERAL.    
23ccf 63 61 73 65 20 27 78 27 3a 20 63 61 73 65 20 27  case 'x': case '
23cd0 58 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  X': {.      if( 
23cd1 28 63 3d 7a 5b 31 5d 29 3d 3d 27 5c 27 27 20 7c  (c=z[1])=='\'' |
23cd2 7c 20 63 3d 3d 27 22 27 20 29 7b 0a 20 20 20 20  | c=='"' ){.    
23cd3 20 20 20 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20      int delim = 
23cd4 63 3b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  c;.        *toke
23cd5 6e 54 79 70 65 20 3d 20 54 4b 5f 42 4c 4f 42 3b  nType = TK_BLOB;
23cd6 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32  .        for(i=2
23cd7 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69  ; (c=z[i])!=0; i
23cd8 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
23cd9 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20  f( c==delim ){. 
23cda 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69             if( i
23cdb 25 32 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65 20  %2 ) *tokenType 
23cdc 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20  = TK_ILLEGAL;.  
23cdd 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
23cde 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
23cdf 20 20 20 20 20 20 20 69 66 28 20 21 69 73 78 64         if( !isxd
23ce0 69 67 69 74 28 63 29 20 29 7b 0a 20 20 20 20 20  igit(c) ){.     
23ce1 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
23ce2 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  e = TK_ILLEGAL;.
23ce3 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
23ce4 72 6e 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20  rn i;.          
23ce5 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
23ce6 20 20 20 20 69 66 28 20 63 20 29 20 69 2b 2b 3b      if( c ) i++;
23ce7 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
23ce8 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  i;.      }.     
23ce9 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61   /* Otherwise fa
23cea 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
23ceb 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20  e next case */. 
23cec 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
23ced 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
23cee 20 69 66 28 20 21 49 64 43 68 61 72 28 2a 7a 29   if( !IdChar(*z)
23cef 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
23cf0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
23cf1 20 66 6f 72 28 69 3d 31 3b 20 49 64 43 68 61 72   for(i=1; IdChar
23cf2 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
23cf3 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
23cf4 3d 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63  = keywordCode((c
23cf5 68 61 72 2a 29 7a 2c 20 69 29 3b 0a 20 20 20 20  har*)z, i);.    
23cf6 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
23cf7 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79  }.  }.  *tokenTy
23cf8 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
23cf9 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 53  .  return 1;.}.S
23cfa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
23cfb 74 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65  t sqlite3GetToke
23cfc 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  n(const unsigned
23cfd 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74   char *z, int *t
23cfe 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20 72 65 74  okenType){.  ret
23cff 75 72 6e 20 67 65 74 54 6f 6b 65 6e 28 7a 2c 20  urn getToken(z, 
23d00 74 6f 6b 65 6e 54 79 70 65 29 3b 0a 7d 0a 0a 2f  tokenType);.}../
23d01 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72  *.** Run the par
23d02 73 65 72 20 6f 6e 20 74 68 65 20 67 69 76 65 6e  ser on the given
23d03 20 53 51 4c 20 73 74 72 69 6e 67 2e 20 20 54 68   SQL string.  Th
23d04 65 20 70 61 72 73 65 72 20 73 74 72 75 63 74 75  e parser structu
23d05 72 65 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20  re is.** passed 
23d06 69 6e 2e 20 20 41 6e 20 53 51 4c 49 54 45 5f 20  in.  An SQLITE_ 
23d07 73 74 61 74 75 73 20 63 6f 64 65 20 69 73 20 72  status code is r
23d08 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 6e 20  eturned.  If an 
23d09 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20  error occurs.** 
23d0a 61 6e 64 20 70 7a 45 72 72 4d 73 67 21 3d 4e 55  and pzErrMsg!=NU
23d0b 4c 4c 20 74 68 65 6e 20 61 6e 20 65 72 72 6f 72  LL then an error
23d0c 20 6d 65 73 73 61 67 65 20 6d 69 67 68 74 20 62   message might b
23d0d 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 0a  e written into .
23d0e 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  ** memory obtain
23d0f 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
23d10 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 2a 70 7a  malloc() and *pz
23d11 45 72 72 4d 73 67 20 6d 61 64 65 20 74 6f 20 70  ErrMsg made to p
23d12 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20  oint to that.** 
23d13 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 20 20  error message.  
23d14 4f 72 20 6d 61 79 62 65 20 6e 6f 74 2e 0a 2a 2f  Or maybe not..*/
23d15 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23d16 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61  int sqlite3RunPa
23d17 72 73 65 72 28 50 61 72 73 65 20 2a 70 50 61 72  rser(Parse *pPar
23d18 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
23d19 7a 53 71 6c 2c 20 63 68 61 72 20 2a 2a 70 7a 45  zSql, char **pzE
23d1a 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 6e 45  rrMsg){.  int nE
23d1b 72 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b  rr = 0;.  int i;
23d1c 0a 20 20 76 6f 69 64 20 2a 70 45 6e 67 69 6e 65  .  void *pEngine
23d1d 3b 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 54 79 70  ;.  int tokenTyp
23d1e 65 3b 0a 20 20 69 6e 74 20 6c 61 73 74 54 6f 6b  e;.  int lastTok
23d1f 65 6e 50 61 72 73 65 64 20 3d 20 2d 31 3b 0a 20  enParsed = -1;. 
23d20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
23d21 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66  Parse->db;..  if
23d22 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ( db->activeVdbe
23d23 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62  Cnt==0 ){.    db
23d24 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
23d25 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50  ed = 0;.  }.  pP
23d26 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
23d27 45 5f 4f 4b 3b 0a 20 20 69 20 3d 20 30 3b 0a 20  E_OK;.  i = 0;. 
23d28 20 70 45 6e 67 69 6e 65 20 3d 20 73 71 6c 69 74   pEngine = sqlit
23d29 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 28 76  e3ParserAlloc((v
23d2a 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29  oid*(*)(size_t))
23d2b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 29 3b  sqlite3_malloc);
23d2c 0a 20 20 69 66 28 20 70 45 6e 67 69 6e 65 3d 3d  .  if( pEngine==
23d2d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  0 ){.    db->mal
23d2e 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
23d2f 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
23d30 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73  _NOMEM;.  }.  as
23d31 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 73 4c  sert( pParse->sL
23d32 61 73 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20  astToken.dyn==0 
23d33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
23d34 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d  rse->pNewTable==
23d35 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
23d36 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
23d37 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  er==0 );.  asser
23d38 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3d  t( pParse->nVar=
23d39 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
23d3a 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
23d3b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
23d3c 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70   pParse->nVarExp
23d3d 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 61  rAlloc==0 );.  a
23d3e 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61  ssert( pParse->a
23d3f 70 56 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20  pVarExpr==0 );. 
23d40 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d   pParse->zTail =
23d41 20 70 50 61 72 73 65 2d 3e 7a 53 71 6c 20 3d 20   pParse->zSql = 
23d42 7a 53 71 6c 3b 0a 20 20 77 68 69 6c 65 28 20 21  zSql;.  while( !
23d43 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
23d44 20 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30 20 29   && zSql[i]!=0 )
23d45 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e  {.    assert( i>
23d46 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65  =0 );.    pParse
23d47 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20 3d  ->sLastToken.z =
23d48 20 28 75 38 2a 29 26 7a 53 71 6c 5b 69 5d 3b 0a   (u8*)&zSql[i];.
23d49 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
23d4a 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 64  se->sLastToken.d
23d4b 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61  yn==0 );.    pPa
23d4c 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e  rse->sLastToken.
23d4d 6e 20 3d 20 67 65 74 54 6f 6b 65 6e 28 28 75 6e  n = getToken((un
23d4e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 7a 53  signed char*)&zS
23d4f 71 6c 5b 69 5d 2c 26 74 6f 6b 65 6e 54 79 70 65  ql[i],&tokenType
23d50 29 3b 0a 20 20 20 20 69 20 2b 3d 20 70 50 61 72  );.    i += pPar
23d51 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e  se->sLastToken.n
23d52 3b 0a 20 20 20 20 69 66 28 20 69 3e 53 51 4c 49  ;.    if( i>SQLI
23d53 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
23d54 48 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  H ){.      pPars
23d55 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 54  e->rc = SQLITE_T
23d56 4f 4f 42 49 47 3b 0a 20 20 20 20 20 20 62 72 65  OOBIG;.      bre
23d57 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 77  ak;.    }.    sw
23d58 69 74 63 68 28 20 74 6f 6b 65 6e 54 79 70 65 20  itch( tokenType 
23d59 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b  ){.      case TK
23d5a 5f 53 50 41 43 45 3a 0a 20 20 20 20 20 20 63 61  _SPACE:.      ca
23d5b 73 65 20 54 4b 5f 43 4f 4d 4d 45 4e 54 3a 20 7b  se TK_COMMENT: {
23d5c 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
23d5d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
23d5e 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  d ){.          p
23d5f 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
23d60 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  TE_INTERRUPT;.  
23d61 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
23d62 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
23d63 67 2c 20 22 69 6e 74 65 72 72 75 70 74 22 2c 20  g, "interrupt", 
23d64 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20 20 20  (char*)0);.     
23d65 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
23d66 70 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20 7d  parse;.        }
23d67 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23d68 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
23d69 73 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 3a 20 7b  se TK_ILLEGAL: {
23d6a 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 7a 45  .        if( pzE
23d6b 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 20  rrMsg ){.       
23d6c 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
23d6d 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  *pzErrMsg);.    
23d6e 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
23d6f 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
23d70 28 64 62 2c 20 22 75 6e 72 65 63 6f 67 6e 69 7a  (db, "unrecogniz
23d71 65 64 20 74 6f 6b 65 6e 3a 20 5c 22 25 54 5c 22  ed token: \"%T\"
23d72 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
23d73 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 50               &pP
23d74 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
23d75 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
23d76 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20       nErr++;.   
23d77 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
23d78 70 61 72 73 65 3b 0a 20 20 20 20 20 20 7d 0a 20  parse;.      }. 
23d79 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4d       case TK_SEM
23d7a 49 3a 20 7b 0a 20 20 20 20 20 20 20 20 70 50 61  I: {.        pPa
23d7b 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53  rse->zTail = &zS
23d7c 71 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 2f  ql[i];.        /
23d7d 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f  * Fall thru into
23d7e 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 61 73   the default cas
23d7f 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  e */.      }.   
23d80 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
23d81 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72        sqlite3Par
23d82 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 74 6f 6b  ser(pEngine, tok
23d83 65 6e 54 79 70 65 2c 20 70 50 61 72 73 65 2d 3e  enType, pParse->
23d84 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72  sLastToken, pPar
23d85 73 65 29 3b 0a 20 20 20 20 20 20 20 20 6c 61 73  se);.        las
23d86 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20 74  tTokenParsed = t
23d87 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 20 20 20 20  okenType;.      
23d88 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63    if( pParse->rc
23d89 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
23d8a 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62           goto ab
23d8b 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20  ort_parse;.     
23d8c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65     }.        bre
23d8d 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
23d8e 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f 70 61 72 73  }.  }.abort_pars
23d8f 65 3a 0a 20 20 69 66 28 20 7a 53 71 6c 5b 69 5d  e:.  if( zSql[i]
23d90 3d 3d 30 20 26 26 20 6e 45 72 72 3d 3d 30 20 26  ==0 && nErr==0 &
23d91 26 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51  & pParse->rc==SQ
23d92 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
23d93 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73  f( lastTokenPars
23d94 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29 7b 0a 20  ed!=TK_SEMI ){. 
23d95 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73       sqlite3Pars
23d96 65 72 28 70 45 6e 67 69 6e 65 2c 20 54 4b 5f 53  er(pEngine, TK_S
23d97 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e 73 4c 61  EMI, pParse->sLa
23d98 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65 29  stToken, pParse)
23d99 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
23d9a 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d  zTail = &zSql[i]
23d9b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
23d9c 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e  te3Parser(pEngin
23d9d 65 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 73 4c  e, 0, pParse->sL
23d9e 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65  astToken, pParse
23d9f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
23da0 50 61 72 73 65 72 46 72 65 65 28 70 45 6e 67 69  ParserFree(pEngi
23da1 6e 65 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ne, sqlite3_free
23da2 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
23da3 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
23da4 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
23da5 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
23da6 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63    if( pParse->rc
23da7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  !=SQLITE_OK && p
23da8 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54  Parse->rc!=SQLIT
23da9 45 5f 44 4f 4e 45 20 26 26 20 70 50 61 72 73 65  E_DONE && pParse
23daa 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a  ->zErrMsg==0 ){.
23dab 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
23dac 72 69 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45  ring(&pParse->zE
23dad 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 45 72  rrMsg, sqlite3Er
23dae 72 53 74 72 28 70 50 61 72 73 65 2d 3e 72 63 29  rStr(pParse->rc)
23daf 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 7d  , (char*)0);.  }
23db0 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 7a  .  if( pParse->z
23db1 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 69 66  ErrMsg ){.    if
23db2 28 20 70 7a 45 72 72 4d 73 67 20 26 26 20 2a 70  ( pzErrMsg && *p
23db3 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a 20 20  zErrMsg==0 ){.  
23db4 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
23db5 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b  pParse->zErrMsg;
23db6 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
23db7 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
23db8 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b  Parse->zErrMsg);
23db9 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
23dba 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  e->zErrMsg = 0;.
23dbb 20 20 20 20 69 66 28 20 21 6e 45 72 72 20 29 20      if( !nErr ) 
23dbc 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66  nErr++;.  }.  if
23dbd 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  ( pParse->pVdbe 
23dbe 26 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e  && pParse->nErr>
23dbf 30 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73  0 && pParse->nes
23dc0 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ted==0 ){.    sq
23dc1 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28  lite3VdbeDelete(
23dc2 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a  pParse->pVdbe);.
23dc3 20 20 20 20 70 50 61 72 73 65 2d 3e 70 56 64 62      pParse->pVdb
23dc4 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64  e = 0;.  }.#ifnd
23dc5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
23dc6 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66  HARED_CACHE.  if
23dc7 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
23dc8 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
23dc9 65 33 5f 66 72 65 65 28 70 50 61 72 73 65 2d 3e  e3_free(pParse->
23dca 61 54 61 62 6c 65 4c 6f 63 6b 29 3b 0a 20 20 20  aTableLock);.   
23dcb 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c   pParse->aTableL
23dcc 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  ock = 0;.    pPa
23dcd 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20  rse->nTableLock 
23dce 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  = 0;.  }.#endif.
23dcf 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c 41  .  if( !IN_DECLA
23dd0 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 2f  RE_VTAB ){.    /
23dd1 2a 20 49 66 20 74 68 65 20 70 50 61 72 73 65 2d  * If the pParse-
23dd2 3e 64 65 63 6c 61 72 65 56 74 61 62 20 66 6c 61  >declareVtab fla
23dd3 67 20 69 73 20 73 65 74 2c 20 64 6f 20 6e 6f 74  g is set, do not
23dd4 20 64 65 6c 65 74 65 20 61 6e 79 20 74 61 62 6c   delete any tabl
23dd5 65 20 0a 20 20 20 20 2a 2a 20 73 74 72 75 63 74  e .    ** struct
23dd6 75 72 65 20 62 75 69 6c 74 20 75 70 20 69 6e 20  ure built up in 
23dd7 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
23dd8 65 2e 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 63  e. The calling c
23dd9 6f 64 65 20 28 73 65 65 20 76 74 61 62 2e 63 29  ode (see vtab.c)
23dda 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 74 61 6b  .    ** will tak
23ddb 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  e responsibility
23ddc 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65   for freeing the
23ddd 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
23dde 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
23ddf 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
23de0 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
23de1 65 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  e);.  }..  sqlit
23de2 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  e3DeleteTrigger(
23de3 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
23de4 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ger);.  sqlite3_
23de5 66 72 65 65 28 70 50 61 72 73 65 2d 3e 61 70 56  free(pParse->apV
23de6 61 72 45 78 70 72 29 3b 0a 20 20 69 66 28 20 6e  arExpr);.  if( n
23de7 45 72 72 3e 30 20 26 26 20 28 70 50 61 72 73 65  Err>0 && (pParse
23de8 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
23de9 7c 7c 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  || pParse->rc==S
23dea 51 4c 49 54 45 5f 44 4f 4e 45 29 20 29 7b 0a 20  QLITE_DONE) ){. 
23deb 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
23dec 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
23ded 7d 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b  }.  return nErr;
23dee 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
23def 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 6f 6b 65 6e  *** End of token
23df0 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ize.c **********
23df1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23df2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23df3 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
23df4 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 63  *** Begin file c
23df5 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a  omplete.c ******
23df6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23df7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23df8 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
23df9 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
23dfa 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
23dfb 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
23dfc 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
23dfd 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
23dfe 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
23dff 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
23e00 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
23e01 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
23e02 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
23e03 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
23e04 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
23e05 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
23e06 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
23e07 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
23e08 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
23e09 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
23e0a 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
23e0b 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
23e0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23e0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23e0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23e0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e  **********.** An
23e10 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53   tokenizer for S
23e11 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  QL.**.** This fi
23e12 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
23e13 64 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  de that implemen
23e14 74 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  ts the sqlite3_c
23e15 6f 6d 70 6c 65 74 65 28 29 20 41 50 49 2e 0a 2a  omplete() API..*
23e16 2a 20 54 68 69 73 20 63 6f 64 65 20 75 73 65 64  * This code used
23e17 20 74 6f 20 62 65 20 70 61 72 74 20 6f 66 20 74   to be part of t
23e18 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 73  he tokenizer.c s
23e19 6f 75 72 63 65 20 66 69 6c 65 2e 20 20 42 75 74  ource file.  But
23e1a 20 62 79 0a 2a 2a 20 73 65 70 61 72 61 74 69 6e   by.** separatin
23e1b 67 20 69 74 20 6f 75 74 2c 20 74 68 65 20 63 6f  g it out, the co
23e1c 64 65 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d  de will be autom
23e1d 61 74 69 63 61 6c 6c 79 20 6f 6d 69 74 74 65 64  atically omitted
23e1e 20 66 72 6f 6d 0a 2a 2a 20 73 74 61 74 69 63 20   from.** static 
23e1f 6c 69 6e 6b 73 20 74 68 61 74 20 64 6f 20 6e 6f  links that do no
23e20 74 20 75 73 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  t use it..**.** 
23e21 24 49 64 3a 20 63 6f 6d 70 6c 65 74 65 2e 63 2c  $Id: complete.c,
23e22 76 20 31 2e 36 20 32 30 30 37 2f 30 38 2f 32 37  v 1.6 2007/08/27
23e23 20 32 33 3a 32 36 3a 35 39 20 64 72 68 20 45 78   23:26:59 drh Ex
23e24 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  p $.*/.#ifndef S
23e25 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c  QLITE_OMIT_COMPL
23e26 45 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ETE../*.** This 
23e27 69 73 20 64 65 66 69 6e 65 64 20 69 6e 20 74 6f  is defined in to
23e28 6b 65 6e 69 7a 65 2e 63 2e 20 20 57 65 20 6a 75  kenize.c.  We ju
23e29 73 74 20 68 61 76 65 20 74 6f 20 69 6d 70 6f 72  st have to impor
23e2a 74 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  t the definition
23e2b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
23e2c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
23e2d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41  .#ifdef SQLITE_A
23e2e 53 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56  SCII.SQLITE_PRIV
23e2f 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73  ATE const char s
23e30 71 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43  qlite3IsAsciiIdC
23e31 68 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65 20 49  har[];.#define I
23e32 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43  dChar(C)  (((c=C
23e33 29 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63  )&0x80)!=0 || (c
23e34 3e 30 78 31 66 20 26 26 20 73 71 6c 69 74 65 33  >0x1f && sqlite3
23e35 49 73 41 73 63 69 69 49 64 43 68 61 72 5b 63 2d  IsAsciiIdChar[c-
23e36 30 78 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23  0x20])).#endif.#
23e37 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
23e38 44 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  DIC.SQLITE_PRIVA
23e39 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  TE const char sq
23e3a 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43  lite3IsEbcdicIdC
23e3b 68 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65 20 49  har[];.#define I
23e3c 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43  dChar(C)  (((c=C
23e3d 29 3e 3d 30 78 34 32 20 26 26 20 73 71 6c 69 74  )>=0x42 && sqlit
23e3e 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72  e3IsEbcdicIdChar
23e3f 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e 64 69  [c-0x40])).#endi
23e40 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  f.#endif /* SQLI
23e41 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20  TE_AMALGAMATION 
23e42 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e  */.../*.** Token
23e43 20 74 79 70 65 73 20 75 73 65 64 20 62 79 20 74   types used by t
23e44 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  he sqlite3_compl
23e45 65 74 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 20  ete() routine.  
23e46 53 65 65 20 74 68 65 20 68 65 61 64 65 72 0a 2a  See the header.*
23e47 2a 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68  * comments on th
23e48 61 74 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72  at procedure for
23e49 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
23e4a 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66  rmation..*/.#def
23e4b 69 6e 65 20 74 6b 53 45 4d 49 20 20 20 20 30 0a  ine tkSEMI    0.
23e4c 23 64 65 66 69 6e 65 20 74 6b 57 53 20 20 20 20  #define tkWS    
23e4d 20 20 31 0a 23 64 65 66 69 6e 65 20 74 6b 4f 54    1.#define tkOT
23e4e 48 45 52 20 20 20 32 0a 23 64 65 66 69 6e 65 20  HER   2.#define 
23e4f 74 6b 45 58 50 4c 41 49 4e 20 33 0a 23 64 65 66  tkEXPLAIN 3.#def
23e50 69 6e 65 20 74 6b 43 52 45 41 54 45 20 20 34 0a  ine tkCREATE  4.
23e51 23 64 65 66 69 6e 65 20 74 6b 54 45 4d 50 20 20  #define tkTEMP  
23e52 20 20 35 0a 23 64 65 66 69 6e 65 20 74 6b 54 52    5.#define tkTR
23e53 49 47 47 45 52 20 36 0a 23 64 65 66 69 6e 65 20  IGGER 6.#define 
23e54 74 6b 45 4e 44 20 20 20 20 20 37 0a 0a 2f 2a 0a  tkEND     7../*.
23e55 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
23e56 66 20 74 68 65 20 67 69 76 65 6e 20 53 51 4c 20  f the given SQL 
23e57 73 74 72 69 6e 67 20 65 6e 64 73 20 69 6e 20 61  string ends in a
23e58 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a   semicolon..**.*
23e59 2a 20 53 70 65 63 69 61 6c 20 68 61 6e 64 6c 69  * Special handli
23e5a 6e 67 20 69 73 20 72 65 71 75 69 72 65 20 66 6f  ng is require fo
23e5b 72 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  r CREATE TRIGGER
23e5c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20   statements..** 
23e5d 57 68 65 6e 65 76 65 72 20 74 68 65 20 43 52 45  Whenever the CRE
23e5e 41 54 45 20 54 52 49 47 47 45 52 20 6b 65 79 77  ATE TRIGGER keyw
23e5f 6f 72 64 73 20 61 72 65 20 73 65 65 6e 2c 20 74  ords are seen, t
23e60 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  he statement.** 
23e61 6d 75 73 74 20 65 6e 64 20 77 69 74 68 20 22 3b  must end with ";
23e62 45 4e 44 3b 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  END;"..**.** Thi
23e63 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
23e64 20 75 73 65 73 20 61 20 73 74 61 74 65 20 6d 61   uses a state ma
23e65 63 68 69 6e 65 20 77 69 74 68 20 37 20 73 74 61  chine with 7 sta
23e66 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29  tes:.**.**   (0)
23e67 20 53 54 41 52 54 20 20 20 20 20 41 74 20 74 68   START     At th
23e68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 72 20 65  e beginning or e
23e69 6e 64 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61  nd of an SQL sta
23e6a 74 65 6d 65 6e 74 2e 20 20 54 68 69 73 20 72 6f  tement.  This ro
23e6b 75 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20  utine.**        
23e6c 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73           returns
23e6d 20 31 20 69 66 20 69 74 20 65 6e 64 73 20 69 6e   1 if it ends in
23e6e 20 74 68 65 20 53 54 41 52 54 20 73 74 61 74 65   the START state
23e6f 20 61 6e 64 20 30 20 69 66 20 69 74 20 65 6e 64   and 0 if it end
23e70 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
23e71 20 20 20 20 20 69 6e 20 61 6e 79 20 6f 74 68 65       in any othe
23e72 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20  r state..**.**  
23e73 20 28 31 29 20 4e 4f 52 4d 41 4c 20 20 20 20 57   (1) NORMAL    W
23e74 65 20 61 72 65 20 69 6e 20 74 68 65 20 6d 69 64  e are in the mid
23e75 64 6c 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74  dle of statement
23e76 20 77 68 69 63 68 20 65 6e 64 73 20 77 69 74 68   which ends with
23e77 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 20 20 20   a single.**    
23e78 20 20 20 20 20 20 20 20 20 20 20 20 20 73 65 6d               sem
23e79 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  icolon..**.**   
23e7a 28 32 29 20 45 58 50 4c 41 49 4e 20 20 20 54 68  (2) EXPLAIN   Th
23e7b 65 20 6b 65 79 77 6f 72 64 20 45 58 50 4c 41 49  e keyword EXPLAI
23e7c 4e 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e 20  N has been seen 
23e7d 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
23e7e 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20   of .**         
23e7f 20 20 20 20 20 20 20 20 61 20 73 74 61 74 65 6d          a statem
23e80 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29  ent..**.**   (3)
23e81 20 43 52 45 41 54 45 20 20 20 20 54 68 65 20 6b   CREATE    The k
23e82 65 79 77 6f 72 64 20 43 52 45 41 54 45 20 68 61  eyword CREATE ha
23e83 73 20 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74  s been seen at t
23e84 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
23e85 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  a.**            
23e86 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20       statement, 
23e87 70 6f 73 73 69 62 6c 79 20 70 72 65 63 65 65 64  possibly preceed
23e88 65 64 20 62 79 20 45 58 50 4c 41 49 4e 20 61 6e  ed by EXPLAIN an
23e89 64 2f 6f 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79  d/or followed by
23e8a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
23e8b 20 20 20 20 54 45 4d 50 20 6f 72 20 54 45 4d 50      TEMP or TEMP
23e8c 4f 52 41 52 59 0a 2a 2a 0a 2a 2a 20 20 20 28 34  ORARY.**.**   (4
23e8d 29 20 54 52 49 47 47 45 52 20 20 20 57 65 20 61  ) TRIGGER   We a
23e8e 72 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  re in the middle
23e8f 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 64 65   of a trigger de
23e90 66 69 6e 69 74 69 6f 6e 20 74 68 61 74 20 6d 75  finition that mu
23e91 73 74 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20  st be.**        
23e92 20 20 20 20 20 20 20 20 20 65 6e 64 65 64 20 62           ended b
23e93 79 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2c 20 74  y a semicolon, t
23e94 68 65 20 6b 65 79 77 6f 72 64 20 45 4e 44 2c 20  he keyword END, 
23e95 61 6e 64 20 61 6e 6f 74 68 65 72 20 73 65 6d 69  and another semi
23e96 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28  colon..**.**   (
23e97 35 29 20 53 45 4d 49 20 20 20 20 20 20 57 65 27  5) SEMI      We'
23e98 76 65 20 73 65 65 6e 20 74 68 65 20 66 69 72 73  ve seen the firs
23e99 74 20 73 65 6d 69 63 6f 6c 6f 6e 20 69 6e 20 74  t semicolon in t
23e9a 68 65 20 22 3b 45 4e 44 3b 22 20 74 68 61 74 20  he ";END;" that 
23e9b 6f 63 63 75 72 73 20 61 74 0a 2a 2a 20 20 20 20  occurs at.**    
23e9c 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
23e9d 20 65 6e 64 20 6f 66 20 61 20 74 72 69 67 67 65   end of a trigge
23e9e 72 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a  r definition..**
23e9f 0a 2a 2a 20 20 20 28 36 29 20 45 4e 44 20 20 20  .**   (6) END   
23ea0 20 20 20 20 57 65 27 76 65 20 73 65 65 6e 20 74      We've seen t
23ea1 68 65 20 22 3b 45 4e 44 22 20 6f 66 20 74 68 65  he ";END" of the
23ea2 20 22 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f 63   ";END;" that oc
23ea3 63 75 72 73 20 61 74 20 74 68 65 20 65 6e 64 0a  curs at the end.
23ea4 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
23ea5 20 20 20 6f 66 20 61 20 74 72 69 67 67 65 72 20     of a trigger 
23ea6 64 69 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a  difinition..**.*
23ea7 2a 20 54 72 61 6e 73 69 74 69 6f 6e 73 20 62 65  * Transitions be
23ea8 74 77 65 65 6e 20 73 74 61 74 65 73 20 61 62 6f  tween states abo
23ea9 76 65 20 61 72 65 20 64 65 74 65 72 6d 69 6e 65  ve are determine
23eaa 64 20 62 79 20 74 6f 6b 65 6e 73 20 65 78 74 72  d by tokens extr
23eab 61 63 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68  acted.** from th
23eac 65 20 69 6e 70 75 74 2e 20 20 54 68 65 20 66 6f  e input.  The fo
23ead 6c 6c 6f 77 69 6e 67 20 74 6f 6b 65 6e 73 20 61  llowing tokens a
23eae 72 65 20 73 69 67 6e 69 66 69 63 61 6e 74 3a 0a  re significant:.
23eaf 2a 2a 0a 2a 2a 20 20 20 28 30 29 20 74 6b 53 45  **.**   (0) tkSE
23eb0 4d 49 20 20 20 20 20 20 41 20 73 65 6d 69 63 6f  MI      A semico
23eb1 6c 6f 6e 2e 0a 2a 2a 20 20 20 28 31 29 20 74 6b  lon..**   (1) tk
23eb2 57 53 20 20 20 20 20 20 20 20 57 68 69 74 65 73  WS        Whites
23eb3 70 61 63 65 0a 2a 2a 20 20 20 28 32 29 20 74 6b  pace.**   (2) tk
23eb4 4f 54 48 45 52 20 20 20 20 20 41 6e 79 20 6f 74  OTHER     Any ot
23eb5 68 65 72 20 53 51 4c 20 74 6f 6b 65 6e 2e 0a 2a  her SQL token..*
23eb6 2a 20 20 20 28 33 29 20 74 6b 45 58 50 4c 41 49  *   (3) tkEXPLAI
23eb7 4e 20 20 20 54 68 65 20 22 65 78 70 6c 61 69 6e  N   The "explain
23eb8 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20  " keyword..**   
23eb9 28 34 29 20 74 6b 43 52 45 41 54 45 20 20 20 20  (4) tkCREATE    
23eba 54 68 65 20 22 63 72 65 61 74 65 22 20 6b 65 79  The "create" key
23ebb 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 35 29 20 74  word..**   (5) t
23ebc 6b 54 45 4d 50 20 20 20 20 20 20 54 68 65 20 22  kTEMP      The "
23ebd 74 65 6d 70 22 20 6f 72 20 22 74 65 6d 70 6f 72  temp" or "tempor
23ebe 61 72 79 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a  ary" keyword..**
23ebf 20 20 20 28 36 29 20 74 6b 54 52 49 47 47 45 52     (6) tkTRIGGER
23ec0 20 20 20 54 68 65 20 22 74 72 69 67 67 65 72 22     The "trigger"
23ec1 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28   keyword..**   (
23ec2 37 29 20 74 6b 45 4e 44 20 20 20 20 20 20 20 54  7) tkEND       T
23ec3 68 65 20 22 65 6e 64 22 20 6b 65 79 77 6f 72 64  he "end" keyword
23ec4 2e 0a 2a 2a 0a 2a 2a 20 57 68 69 74 65 73 70 61  ..**.** Whitespa
23ec5 63 65 20 6e 65 76 65 72 20 63 61 75 73 65 73 20  ce never causes 
23ec6 61 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69  a state transiti
23ec7 6f 6e 20 61 6e 64 20 69 73 20 61 6c 77 61 79 73  on and is always
23ec8 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   ignored..**.** 
23ec9 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  If we compile wi
23eca 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  th SQLITE_OMIT_T
23ecb 52 49 47 47 45 52 2c 20 61 6c 6c 20 6f 66 20 74  RIGGER, all of t
23ecc 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 6e  he computation n
23ecd 65 65 64 65 64 0a 2a 2a 20 74 6f 20 72 65 63 6f  eeded.** to reco
23ece 67 6e 69 7a 65 20 74 68 65 20 65 6e 64 20 6f 66  gnize the end of
23ecf 20 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62   a trigger can b
23ed0 65 20 6f 6d 69 74 74 65 64 2e 20 20 41 6c 6c 20  e omitted.  All 
23ed1 77 65 20 68 61 76 65 20 74 6f 20 64 6f 0a 2a 2a  we have to do.**
23ed2 20 69 73 20 6c 6f 6f 6b 20 66 6f 72 20 61 20 73   is look for a s
23ed3 65 6d 69 63 6f 6c 6f 6e 20 74 68 61 74 20 69 73  emicolon that is
23ed4 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 6e 20   not part of an 
23ed5 73 74 72 69 6e 67 20 6f 72 20 63 6f 6d 6d 65 6e  string or commen
23ed6 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
23ed7 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d   int sqlite3_com
23ed8 70 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72  plete(const char
23ed9 20 2a 7a 53 71 6c 29 7b 0a 20 20 75 38 20 73 74   *zSql){.  u8 st
23eda 61 74 65 20 3d 20 30 3b 20 20 20 2f 2a 20 43 75  ate = 0;   /* Cu
23edb 72 72 65 6e 74 20 73 74 61 74 65 2c 20 75 73 69  rrent state, usi
23edc 6e 67 20 6e 75 6d 62 65 72 73 20 64 65 66 69 6e  ng numbers defin
23edd 65 64 20 69 6e 20 68 65 61 64 65 72 20 63 6f 6d  ed in header com
23ede 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 74 6f 6b  ment */.  u8 tok
23edf 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c  en;       /* Val
23ee0 75 65 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74  ue of the next t
23ee1 6f 6b 65 6e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  oken */..#ifndef
23ee2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
23ee3 47 47 45 52 0a 20 20 2f 2a 20 41 20 63 6f 6d 70  GGER.  /* A comp
23ee4 6c 65 78 20 73 74 61 74 65 6d 65 6e 74 20 6d 61  lex statement ma
23ee5 63 68 69 6e 65 20 75 73 65 64 20 74 6f 20 64 65  chine used to de
23ee6 74 65 63 74 20 74 68 65 20 65 6e 64 20 6f 66 20  tect the end of 
23ee7 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  a CREATE TRIGGER
23ee8 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e  .  ** statement.
23ee9 20 20 54 68 69 73 20 69 73 20 74 68 65 20 6e 6f    This is the no
23eea 72 6d 61 6c 20 63 61 73 65 2e 0a 20 20 2a 2f 0a  rmal case..  */.
23eeb 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
23eec 38 20 74 72 61 6e 73 5b 37 5d 5b 38 5d 20 3d 20  8 trans[7][8] = 
23eed 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
23eee 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a         /* Token:
23eef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ef1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ef2 2a 2f 0a 20 20 20 20 20 2f 2a 20 53 74 61 74 65  */.     /* State
23ef3 3a 20 20 20 20 20 20 20 2a 2a 20 20 53 45 4d 49  :       **  SEMI
23ef4 20 20 57 53 20 20 4f 54 48 45 52 20 45 58 50 4c    WS  OTHER EXPL
23ef5 41 49 4e 20 20 43 52 45 41 54 45 20 20 54 45 4d  AIN  CREATE  TEM
23ef6 50 20 20 54 52 49 47 47 45 52 20 20 45 4e 44 20  P  TRIGGER  END 
23ef7 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20 20   */.     /* 0   
23ef8 53 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30  START: */ {    0
23ef9 2c 20 20 30 2c 20 20 20 20 20 31 2c 20 20 20 20  ,  0,     1,    
23efa 20 20 32 2c 20 20 20 20 20 20 33 2c 20 20 20 20    2,      3,    
23efb 31 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c  1,       1,   1,
23efc 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20 20    },.     /* 1  
23efd 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20  NORMAL: */ {    
23efe 30 2c 20 20 31 2c 20 20 20 20 20 31 2c 20 20 20  0,  1,     1,   
23eff 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20     1,      1,   
23f00 20 31 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31   1,       1,   1
23f01 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 32 20  ,  },.     /* 2 
23f02 45 58 50 4c 41 49 4e 3a 20 2a 2f 20 7b 20 20 20  EXPLAIN: */ {   
23f03 20 30 2c 20 20 32 2c 20 20 20 20 20 31 2c 20 20   0,  2,     1,  
23f04 20 20 20 20 31 2c 20 20 20 20 20 20 33 2c 20 20      1,      3,  
23f05 20 20 31 2c 20 20 20 20 20 20 20 31 2c 20 20 20    1,       1,   
23f06 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 33  1,  },.     /* 3
23f07 20 20 43 52 45 41 54 45 3a 20 2a 2f 20 7b 20 20    CREATE: */ {  
23f08 20 20 30 2c 20 20 33 2c 20 20 20 20 20 31 2c 20    0,  3,     1, 
23f09 20 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20       1,      1, 
23f0a 20 20 20 33 2c 20 20 20 20 20 20 20 34 2c 20 20     3,       4,  
23f0b 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20   1,  },.     /* 
23f0c 34 20 54 52 49 47 47 45 52 3a 20 2a 2f 20 7b 20  4 TRIGGER: */ { 
23f0d 20 20 20 35 2c 20 20 34 2c 20 20 20 20 20 34 2c     5,  4,     4,
23f0e 20 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c        4,      4,
23f0f 20 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20      4,       4, 
23f10 20 20 34 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a    4,  },.     /*
23f11 20 35 20 20 20 20 53 45 4d 49 3a 20 2a 2f 20 7b   5    SEMI: */ {
23f12 20 20 20 20 35 2c 20 20 35 2c 20 20 20 20 20 34      5,  5,     4
23f13 2c 20 20 20 20 20 20 34 2c 20 20 20 20 20 20 34  ,      4,      4
23f14 2c 20 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c  ,    4,       4,
23f15 20 20 20 36 2c 20 20 7d 2c 0a 20 20 20 20 20 2f     6,  },.     /
23f16 2a 20 36 20 20 20 20 20 45 4e 44 3a 20 2a 2f 20  * 6     END: */ 
23f17 7b 20 20 20 20 30 2c 20 20 36 2c 20 20 20 20 20  {    0,  6,     
23f18 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 20 20  4,      4,      
23f19 34 2c 20 20 20 20 34 2c 20 20 20 20 20 20 20 34  4,    4,       4
23f1a 2c 20 20 20 34 2c 20 20 7d 2c 0a 20 20 7d 3b 0a  ,   4,  },.  };.
23f1b 23 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20 74 72  #else.  /* If tr
23f1c 69 67 67 65 72 73 20 61 72 65 20 6e 6f 74 20 73  iggers are not s
23f1d 75 70 70 6f 72 65 64 20 62 79 20 74 68 69 73 20  uppored by this 
23f1e 63 6f 6d 70 69 6c 65 20 74 68 65 6e 20 74 68 65  compile then the
23f1f 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 63 68 69   statement machi
23f20 6e 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20  ne.  ** used to 
23f21 64 65 74 65 63 74 20 74 68 65 20 65 6e 64 20 6f  detect the end o
23f22 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73  f a statement is
23f23 20 6d 75 63 68 20 73 69 6d 70 6c 69 65 72 0a 20   much simplier. 
23f24 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
23f25 73 74 20 75 38 20 74 72 61 6e 73 5b 32 5d 5b 33  st u8 trans[2][3
23f26 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
23f27 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
23f28 6b 65 6e 3a 20 20 20 20 20 20 20 20 20 20 20 2a  ken:           *
23f29 2f 0a 20 20 20 20 20 2f 2a 20 53 74 61 74 65 3a  /.     /* State:
23f2a 20 20 20 20 20 20 20 2a 2a 20 20 53 45 4d 49 20         **  SEMI 
23f2b 20 57 53 20 20 4f 54 48 45 52 20 2a 2f 0a 20 20   WS  OTHER */.  
23f2c 20 20 20 2f 2a 20 30 20 20 20 53 54 41 52 54 3a     /* 0   START:
23f2d 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30 2c 20   */ {    0,  0, 
23f2e 20 20 20 20 31 2c 20 7d 2c 0a 20 20 20 20 20 2f      1, },.     /
23f2f 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20  * 1  NORMAL: */ 
23f30 7b 20 20 20 20 30 2c 20 20 31 2c 20 20 20 20 20  {    0,  1,     
23f31 31 2c 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6e 64 69  1, },.  };.#endi
23f32 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
23f33 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a 20 20 77  _TRIGGER */..  w
23f34 68 69 6c 65 28 20 2a 7a 53 71 6c 20 29 7b 0a 20  hile( *zSql ){. 
23f35 20 20 20 73 77 69 74 63 68 28 20 2a 7a 53 71 6c     switch( *zSql
23f36 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 27   ){.      case '
23f37 3b 27 3a 20 7b 20 20 2f 2a 20 41 20 73 65 6d 69  ;': {  /* A semi
23f38 63 6f 6c 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20  colon */.       
23f39 20 74 6f 6b 65 6e 20 3d 20 74 6b 53 45 4d 49 3b   token = tkSEMI;
23f3a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23f3b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
23f3c 73 65 20 27 20 27 3a 0a 20 20 20 20 20 20 63 61  se ' ':.      ca
23f3d 73 65 20 27 5c 72 27 3a 0a 20 20 20 20 20 20 63  se '\r':.      c
23f3e 61 73 65 20 27 5c 74 27 3a 0a 20 20 20 20 20 20  ase '\t':.      
23f3f 63 61 73 65 20 27 5c 6e 27 3a 0a 20 20 20 20 20  case '\n':.     
23f40 20 63 61 73 65 20 27 5c 66 27 3a 20 7b 20 20 2f   case '\f': {  /
23f41 2a 20 57 68 69 74 65 20 73 70 61 63 65 20 69 73  * White space is
23f42 20 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20   ignored */.    
23f43 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53      token = tkWS
23f44 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
23f45 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
23f46 61 73 65 20 27 2f 27 3a 20 7b 20 20 20 2f 2a 20  ase '/': {   /* 
23f47 43 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73  C-style comments
23f48 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
23f49 7a 53 71 6c 5b 31 5d 21 3d 27 2a 27 20 29 7b 0a  zSql[1]!='*' ){.
23f4a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
23f4b 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
23f4c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23f4d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 53      }.        zS
23f4e 71 6c 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20  ql += 2;.       
23f4f 20 77 68 69 6c 65 28 20 7a 53 71 6c 5b 30 5d 20   while( zSql[0] 
23f50 26 26 20 28 7a 53 71 6c 5b 30 5d 21 3d 27 2a 27  && (zSql[0]!='*'
23f51 20 7c 7c 20 7a 53 71 6c 5b 31 5d 21 3d 27 2f 27   || zSql[1]!='/'
23f52 29 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20  ) ){ zSql++; }. 
23f53 20 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b         if( zSql[
23f54 30 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  0]==0 ) return 0
23f55 3b 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b  ;.        zSql++
23f56 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20  ;.        token 
23f57 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20  = tkWS;.        
23f58 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
23f59 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 7b       case '-': {
23f5a 20 20 20 2f 2a 20 53 51 4c 2d 73 74 79 6c 65 20     /* SQL-style 
23f5b 63 6f 6d 6d 65 6e 74 73 20 66 72 6f 6d 20 22 2d  comments from "-
23f5c 2d 22 20 74 6f 20 65 6e 64 20 6f 66 20 6c 69 6e  -" to end of lin
23f5d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  e */.        if(
23f5e 20 7a 53 71 6c 5b 31 5d 21 3d 27 2d 27 20 29 7b   zSql[1]!='-' ){
23f5f 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  .          token
23f60 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20   = tkOTHER;.    
23f61 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23f62 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77       }.        w
23f63 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a  hile( *zSql && *
23f64 7a 53 71 6c 21 3d 27 5c 6e 27 20 29 7b 20 7a 53  zSql!='\n' ){ zS
23f65 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20  ql++; }.        
23f66 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72  if( *zSql==0 ) r
23f67 65 74 75 72 6e 20 73 74 61 74 65 3d 3d 30 3b 0a  eturn state==0;.
23f68 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
23f69 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72  tkWS;.        br
23f6a 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
23f6b 20 20 20 63 61 73 65 20 27 5b 27 3a 20 7b 20 20     case '[': {  
23f6c 20 2f 2a 20 4d 69 63 72 6f 73 6f 66 74 2d 73 74   /* Microsoft-st
23f6d 79 6c 65 20 69 64 65 6e 74 69 66 69 65 72 73 20  yle identifiers 
23f6e 69 6e 20 5b 2e 2e 2e 5d 20 2a 2f 0a 20 20 20 20  in [...] */.    
23f6f 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20      zSql++;.    
23f70 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c      while( *zSql
23f71 20 26 26 20 2a 7a 53 71 6c 21 3d 27 5d 27 20 29   && *zSql!=']' )
23f72 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20  { zSql++; }.    
23f73 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30      if( *zSql==0
23f74 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
23f75 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
23f76 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 62 72  THER;.        br
23f77 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
23f78 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 20     case '`':    
23f79 20 2f 2a 20 47 72 61 76 65 2d 61 63 63 65 6e 74   /* Grave-accent
23f7a 20 71 75 6f 74 65 64 20 73 79 6d 62 6f 6c 73 20   quoted symbols 
23f7b 75 73 65 64 20 62 79 20 4d 79 53 51 4c 20 2a 2f  used by MySQL */
23f7c 0a 20 20 20 20 20 20 63 61 73 65 20 27 22 27 3a  .      case '"':
23f7d 20 20 20 20 20 2f 2a 20 73 69 6e 67 6c 65 2d 20       /* single- 
23f7e 61 6e 64 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65  and double-quote
23f7f 64 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20  d strings */.   
23f80 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20 7b 0a     case '\'': {.
23f81 20 20 20 20 20 20 20 20 69 6e 74 20 63 20 3d 20          int c = 
23f82 2a 7a 53 71 6c 3b 0a 20 20 20 20 20 20 20 20 7a  *zSql;.        z
23f83 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77  Sql++;.        w
23f84 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a  hile( *zSql && *
23f85 7a 53 71 6c 21 3d 63 20 29 7b 20 7a 53 71 6c 2b  zSql!=c ){ zSql+
23f86 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28  +; }.        if(
23f87 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75   *zSql==0 ) retu
23f88 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 74 6f  rn 0;.        to
23f89 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
23f8a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23f8b 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61      }.      defa
23f8c 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  ult: {.        i
23f8d 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 20 69 66  nt c;.        if
23f8e 28 20 49 64 43 68 61 72 28 28 75 38 29 2a 7a 53  ( IdChar((u8)*zS
23f8f 71 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ql) ){.         
23f90 20 2f 2a 20 4b 65 79 77 6f 72 64 73 20 61 6e 64   /* Keywords and
23f91 20 75 6e 71 75 6f 74 65 64 20 69 64 65 6e 74 69   unquoted identi
23f92 66 69 65 72 73 20 2a 2f 0a 20 20 20 20 20 20 20  fiers */.       
23f93 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20     int nId;.    
23f94 20 20 20 20 20 20 66 6f 72 28 6e 49 64 3d 31 3b        for(nId=1;
23f95 20 49 64 43 68 61 72 28 7a 53 71 6c 5b 6e 49 64   IdChar(zSql[nId
23f96 5d 29 3b 20 6e 49 64 2b 2b 29 7b 7d 0a 23 69 66  ]); nId++){}.#if
23f97 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23f98 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 20 20  TRIGGER.        
23f99 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
23f9a 52 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20  R;.#else.       
23f9b 20 20 20 73 77 69 74 63 68 28 20 2a 7a 53 71 6c     switch( *zSql
23f9c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
23f9d 63 61 73 65 20 27 63 27 3a 20 63 61 73 65 20 27  case 'c': case '
23f9e 43 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  C': {.          
23f9f 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 36 20 26      if( nId==6 &
23fa0 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
23fa1 70 28 7a 53 71 6c 2c 20 22 63 72 65 61 74 65 22  p(zSql, "create"
23fa2 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 6)==0 ){.     
23fa3 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
23fa4 20 3d 20 74 6b 43 52 45 41 54 45 3b 0a 20 20 20   = tkCREATE;.   
23fa5 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
23fa6 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
23fa7 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
23fa8 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  R;.             
23fa9 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
23faa 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
23fab 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
23fac 20 20 63 61 73 65 20 27 74 27 3a 20 63 61 73 65    case 't': case
23fad 20 27 54 27 3a 20 7b 0a 20 20 20 20 20 20 20 20   'T': {.        
23fae 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 37        if( nId==7
23faf 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
23fb0 43 6d 70 28 7a 53 71 6c 2c 20 22 74 72 69 67 67  Cmp(zSql, "trigg
23fb1 65 72 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20  er", 7)==0 ){.  
23fb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
23fb3 6b 65 6e 20 3d 20 74 6b 54 52 49 47 47 45 52 3b  ken = tkTRIGGER;
23fb4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
23fb5 65 6c 73 65 20 69 66 28 20 6e 49 64 3d 3d 34 20  else if( nId==4 
23fb6 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
23fb7 6d 70 28 7a 53 71 6c 2c 20 22 74 65 6d 70 22 2c  mp(zSql, "temp",
23fb8 20 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   4)==0 ){.      
23fb9 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
23fba 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20  = tkTEMP;.      
23fbb 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
23fbc 28 20 6e 49 64 3d 3d 39 20 26 26 20 73 71 6c 69  ( nId==9 && sqli
23fbd 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c  te3StrNICmp(zSql
23fbe 2c 20 22 74 65 6d 70 6f 72 61 72 79 22 2c 20 39  , "temporary", 9
23fbf 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
23fc0 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
23fc1 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20  tkTEMP;.        
23fc2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
23fc3 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
23fc4 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
23fc5 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
23fc6 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
23fc7 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  k;.            }
23fc8 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73  .            cas
23fc9 65 20 27 65 27 3a 20 20 63 61 73 65 20 27 45 27  e 'e':  case 'E'
23fca 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
23fcb 20 20 69 66 28 20 6e 49 64 3d 3d 33 20 26 26 20    if( nId==3 && 
23fcc 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
23fcd 7a 53 71 6c 2c 20 22 65 6e 64 22 2c 20 33 29 3d  zSql, "end", 3)=
23fce 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
23fcf 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
23fd0 45 4e 44 3b 0a 20 20 20 20 20 20 20 20 20 20 20  END;.           
23fd1 20 20 20 7d 65 6c 73 65 0a 23 69 66 6e 64 65 66     }else.#ifndef
23fd2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50   SQLITE_OMIT_EXP
23fd3 4c 41 49 4e 0a 20 20 20 20 20 20 20 20 20 20 20  LAIN.           
23fd4 20 20 20 69 66 28 20 6e 49 64 3d 3d 37 20 26 26     if( nId==7 &&
23fd5 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
23fd6 28 7a 53 71 6c 2c 20 22 65 78 70 6c 61 69 6e 22  (zSql, "explain"
23fd7 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 7)==0 ){.     
23fd8 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
23fd9 20 3d 20 74 6b 45 58 50 4c 41 49 4e 3b 0a 20 20   = tkEXPLAIN;.  
23fda 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
23fdb 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  e.#endif.       
23fdc 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20         {.       
23fdd 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
23fde 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
23fdf 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
23fe0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
23fe1 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
23fe2 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74           default
23fe3 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
23fe4 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
23fe5 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  R;.             
23fe6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
23fe7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
23fe8 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
23fe9 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20  TE_OMIT_TRIGGER 
23fea 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7a 53 71  */.          zSq
23feb 6c 20 2b 3d 20 6e 49 64 2d 31 3b 0a 20 20 20 20  l += nId-1;.    
23fec 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23fed 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 6f 72       /* Operator
23fee 73 20 61 6e 64 20 73 70 65 63 69 61 6c 20 73 79  s and special sy
23fef 6d 62 6f 6c 73 20 2a 2f 0a 20 20 20 20 20 20 20  mbols */.       
23ff0 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
23ff1 45 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ER;.        }.  
23ff2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23ff3 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
23ff4 74 61 74 65 20 3d 20 74 72 61 6e 73 5b 73 74 61  tate = trans[sta
23ff5 74 65 5d 5b 74 6f 6b 65 6e 5d 3b 0a 20 20 20 20  te][token];.    
23ff6 7a 53 71 6c 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  zSql++;.  }.  re
23ff7 74 75 72 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 7d  turn state==0;.}
23ff8 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
23ff9 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
23ffa 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
23ffb 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
23ffc 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  e sqlite3_comple
23ffd 74 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65 73  te() routine des
23ffe 63 72 69 62 65 64 0a 2a 2a 20 61 62 6f 76 65 2c  cribed.** above,
23fff 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
24000 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 72 65   parameter is re
24001 71 75 69 72 65 64 20 74 6f 20 62 65 20 55 54 46  quired to be UTF
24002 2d 31 36 20 65 6e 63 6f 64 65 64 2c 20 6e 6f 74  -16 encoded, not
24003 0a 2a 2a 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51  .** UTF-8..*/.SQ
24004 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
24005 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
24006 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c  const void *zSql
24007 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ){.  sqlite3_val
24008 75 65 20 2a 70 56 61 6c 3b 0a 20 20 63 68 61 72  ue *pVal;.  char
24009 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 38 3b 0a 20   const *zSql8;. 
2400a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2400b 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 70 56 61 6c 20  _NOMEM;..  pVal 
2400c 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65  = sqlite3ValueNe
2400d 77 28 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  w(0);.  sqlite3V
2400e 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c  alueSetStr(pVal,
2400f 20 2d 31 2c 20 7a 53 71 6c 2c 20 53 51 4c 49 54   -1, zSql, SQLIT
24010 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53  E_UTF16NATIVE, S
24011 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
24012 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65 33   zSql8 = sqlite3
24013 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
24014 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20  SQLITE_UTF8);.  
24015 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20 20  if( zSql8 ){.   
24016 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f   rc = sqlite3_co
24017 6d 70 6c 65 74 65 28 7a 53 71 6c 38 29 3b 0a 20  mplete(zSql8);. 
24018 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75   }.  sqlite3Valu
24019 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 72  eFree(pVal);.  r
2401a 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69  eturn sqlite3Api
2401b 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 23  Exit(0, rc);.}.#
2401c 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2401d 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65  OMIT_UTF16 */.#e
2401e 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2401f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 20 2a 2f 0a  MIT_COMPLETE */.
24020 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
24021 20 45 6e 64 20 6f 66 20 63 6f 6d 70 6c 65 74 65   End of complete
24022 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
24023 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24024 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
24025 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
24026 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 69 6e   Begin file main
24027 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
24028 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24029 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2402a 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
2402b 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
2402c 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2402d 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2402e 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2402f 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
24030 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
24031 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
24032 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
24033 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
24034 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
24035 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
24036 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
24037 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
24038 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
24039 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2403a 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2403b 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2403c 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2403d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2403e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2403f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24041 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e 20  *******.** Main 
24042 66 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51 4c  file for the SQL
24043 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68  ite library.  Th
24044 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  e routines in th
24045 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c 65  is file.** imple
24046 6d 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61 6d  ment the program
24047 6d 65 72 20 69 6e 74 65 72 66 61 63 65 20 74 6f  mer interface to
24048 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 52   the library.  R
24049 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f 74  outines in.** ot
2404a 68 65 72 20 66 69 6c 65 73 20 61 72 65 20 66 6f  her files are fo
2404b 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 62  r internal use b
2404c 79 20 53 51 4c 69 74 65 20 61 6e 64 20 73 68 6f  y SQLite and sho
2404d 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63  uld not be.** ac
2404e 63 65 73 73 65 64 20 62 79 20 75 73 65 72 73 20  cessed by users 
2404f 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a  of the library..
24050 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 61 69 6e 2e  **.** $Id: main.
24051 63 2c 76 20 31 2e 34 30 34 20 32 30 30 37 2f 30  c,v 1.404 2007/0
24052 39 2f 30 33 20 31 35 3a 31 39 3a 33 35 20 64 72  9/03 15:19:35 dr
24053 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
24054 2a 20 54 68 65 20 76 65 72 73 69 6f 6e 20 6f 66  * The version of
24055 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2f 0a   the library.*/.
24056 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
24057 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f 76 65   char sqlite3_ve
24058 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 45  rsion[] = SQLITE
24059 5f 56 45 52 53 49 4f 4e 3b 0a 53 51 4c 49 54 45  _VERSION;.SQLITE
2405a 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
2405b 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73  *sqlite3_libvers
2405c 69 6f 6e 28 76 6f 69 64 29 7b 20 72 65 74 75 72  ion(void){ retur
2405d 6e 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  n sqlite3_versio
2405e 6e 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  n; }.SQLITE_API 
2405f 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  int sqlite3_libv
24060 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f  ersion_number(vo
24061 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
24062 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45  TE_VERSION_NUMBE
24063 52 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  R; }.SQLITE_API 
24064 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65  int sqlite3_thre
24065 61 64 73 61 66 65 28 76 6f 69 64 29 7b 20 72 65  adsafe(void){ re
24066 74 75 72 6e 20 53 51 4c 49 54 45 5f 54 48 52 45  turn SQLITE_THRE
24067 41 44 53 41 46 45 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  ADSAFE; }../*.**
24068 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   If the followin
24069 67 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  g function point
2406a 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61  er is not NULL a
2406b 6e 64 20 69 66 0a 2a 2a 20 53 51 4c 49 54 45 5f  nd if.** SQLITE_
2406c 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 69  ENABLE_IOTRACE i
2406d 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20  s enabled, then 
2406e 6d 65 73 73 61 67 65 73 20 64 65 73 63 72 69 62  messages describ
2406f 69 6e 67 0a 2a 2a 20 49 2f 4f 20 61 63 74 69 76  ing.** I/O activ
24070 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73  e are written us
24071 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
24072 6e 2e 20 20 54 68 65 73 65 20 6d 65 73 73 61 67  n.  These messag
24073 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64  es.** are intend
24074 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ed for debugging
24075 20 61 63 74 69 76 69 74 79 20 6f 6e 6c 79 2e 0a   activity only..
24076 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
24077 69 64 20 28 2a 73 71 6c 69 74 65 33 5f 69 6f 5f  id (*sqlite3_io_
24078 74 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68 61  trace)(const cha
24079 72 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b 0a 0a 2f  r*, ...) = 0;../
2407a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c  *.** If the foll
2407b 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72  owing global var
2407c 69 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20  iable points to 
2407d 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69  a string which i
2407e 73 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66  s the.** name of
2407f 20 61 20 64 69 72 65 63 74 6f 72 79 2c 20 74 68   a directory, th
24080 65 6e 20 74 68 61 74 20 64 69 72 65 63 74 6f 72  en that director
24081 79 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74  y will be used t
24082 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f  o store.** tempo
24083 72 61 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  rary files..**.*
24084 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 22  * See also the "
24085 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72  PRAGMA temp_stor
24086 65 5f 64 69 72 65 63 74 6f 72 79 22 20 53 51 4c  e_directory" SQL
24087 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c   command..*/.SQL
24088 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71  ITE_API char *sq
24089 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
2408a 74 6f 72 79 20 3d 20 30 3b 0a 0a 0a 2f 2a 0a 2a  tory = 0;.../*.*
2408b 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65  * This is the de
2408c 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20  fault collating 
2408d 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 22  function named "
2408e 42 49 4e 41 52 59 22 20 77 68 69 63 68 20 69 73  BINARY" which is
2408f 20 61 6c 77 61 79 73 0a 2a 2a 20 61 76 61 69 6c   always.** avail
24090 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
24091 69 6e 74 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 28  int binCollFunc(
24092 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  .  void *NotUsed
24093 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63  ,.  int nKey1, c
24094 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
24095 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63  ,.  int nKey2, c
24096 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
24097 0a 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 3b  .){.  int rc, n;
24098 0a 20 20 6e 20 3d 20 6e 4b 65 79 31 3c 6e 4b 65  .  n = nKey1<nKe
24099 79 32 20 3f 20 6e 4b 65 79 31 20 3a 20 6e 4b 65  y2 ? nKey1 : nKe
2409a 79 32 3b 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d  y2;.  rc = memcm
2409b 70 28 70 4b 65 79 31 2c 20 70 4b 65 79 32 2c 20  p(pKey1, pKey2, 
2409c 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  n);.  if( rc==0 
2409d 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 4b 65 79  ){.    rc = nKey
2409e 31 20 2d 20 6e 4b 65 79 32 3b 0a 20 20 7d 0a 20  1 - nKey2;.  }. 
2409f 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
240a0 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 62 75 69  *.** Another bui
240a1 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67 20  lt-in collating 
240a2 73 65 71 75 65 6e 63 65 3a 20 4e 4f 43 41 53 45  sequence: NOCASE
240a3 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f  . .**.** This co
240a4 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
240a5 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
240a6 62 65 20 75 73 65 64 20 66 6f 72 20 22 63 61 73  be used for "cas
240a7 65 20 69 6e 64 65 70 65 6e 64 61 6e 74 0a 2a 2a  e independant.**
240a8 20 63 6f 6d 70 61 72 69 73 6f 6e 22 2e 20 53 51   comparison". SQ
240a9 4c 69 74 65 27 73 20 6b 6e 6f 77 6c 65 64 67 65  Lite's knowledge
240aa 20 6f 66 20 75 70 70 65 72 20 61 6e 64 20 6c 6f   of upper and lo
240ab 77 65 72 20 63 61 73 65 20 65 71 75 69 76 61 6c  wer case equival
240ac 65 6e 74 73 0a 2a 2a 20 65 78 74 65 6e 64 73 20  ents.** extends 
240ad 6f 6e 6c 79 20 74 6f 20 74 68 65 20 32 36 20 63  only to the 26 c
240ae 68 61 72 61 63 74 65 72 73 20 75 73 65 64 20 69  haracters used i
240af 6e 20 74 68 65 20 45 6e 67 6c 69 73 68 20 6c 61  n the English la
240b0 6e 67 75 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 74  nguage..**.** At
240b1 20 74 68 65 20 6d 6f 6d 65 6e 74 20 74 68 65 72   the moment ther
240b2 65 20 69 73 20 6f 6e 6c 79 20 61 20 55 54 46 2d  e is only a UTF-
240b3 38 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  8 implementation
240b4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
240b5 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46  nocaseCollatingF
240b6 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74  unc(.  void *Not
240b7 55 73 65 64 2c 0a 20 20 69 6e 74 20 6e 4b 65 79  Used,.  int nKey
240b8 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  1, const void *p
240b9 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65 79  Key1,.  int nKey
240ba 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  2, const void *p
240bb 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72 20  Key2.){.  int r 
240bc 3d 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  = sqlite3StrNICm
240bd 70 28 0a 20 20 20 20 20 20 28 63 6f 6e 73 74 20  p(.      (const 
240be 63 68 61 72 20 2a 29 70 4b 65 79 31 2c 20 28 63  char *)pKey1, (c
240bf 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79  onst char *)pKey
240c0 32 2c 20 28 6e 4b 65 79 31 3c 6e 4b 65 79 32 29  2, (nKey1<nKey2)
240c1 3f 6e 4b 65 79 31 3a 6e 4b 65 79 32 29 3b 0a 20  ?nKey1:nKey2);. 
240c2 20 69 66 28 20 30 3d 3d 72 20 29 7b 0a 20 20 20   if( 0==r ){.   
240c3 20 72 20 3d 20 6e 4b 65 79 31 2d 6e 4b 65 79 32   r = nKey1-nKey2
240c4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
240c5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
240c6 6e 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20 74  n the ROWID of t
240c7 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69  he most recent i
240c8 6e 73 65 72 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nsert.*/.SQLITE_
240c9 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  API sqlite_int64
240ca 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
240cb 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 74  sert_rowid(sqlit
240cc 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72  e3 *db){.  retur
240cd 6e 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b  n db->lastRowid;
240ce 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
240cf 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
240d0 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f  hanges in the mo
240d1 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74  st recent call t
240d2 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  o sqlite3_exec()
240d3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
240d4 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  int sqlite3_chan
240d5 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ges(sqlite3 *db)
240d6 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e  {.  return db->n
240d7 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Change;.}../*.**
240d8 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
240d9 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 73 69  er of changes si
240da 6e 63 65 20 74 68 65 20 64 61 74 61 62 61 73 65  nce the database
240db 20 68 61 6e 64 6c 65 20 77 61 73 20 6f 70 65 6e   handle was open
240dc 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
240dd 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f  I int sqlite3_to
240de 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69  tal_changes(sqli
240df 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75  te3 *db){.  retu
240e0 72 6e 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61  rn db->nTotalCha
240e1 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  nge;.}../*.** Cl
240e2 6f 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20  ose an existing 
240e3 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 0a  SQLite database.
240e4 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
240e5 74 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28  t sqlite3_close(
240e6 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
240e7 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69  HashElem *i;.  i
240e8 6e 74 20 6a 3b 0a 0a 20 20 69 66 28 20 21 64 62  nt j;..  if( !db
240e9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
240ea 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
240eb 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
240ec 79 43 68 65 63 6b 28 64 62 29 20 29 7b 0a 20 20  yCheck(db) ){.  
240ed 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
240ee 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71  MISUSE;.  }.  sq
240ef 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
240f0 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 23  r(db->mutex);..#
240f1 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45  ifdef SQLITE_SSE
240f2 0a 20 20 7b 0a 20 20 20 20 65 78 74 65 72 6e 20  .  {.    extern 
240f3 76 6f 69 64 20 73 71 6c 69 74 65 33 53 73 65 43  void sqlite3SseC
240f4 6c 65 61 6e 75 70 28 73 71 6c 69 74 65 33 2a 29  leanup(sqlite3*)
240f5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 73 65  ;.    sqlite3Sse
240f6 43 6c 65 61 6e 75 70 28 64 62 29 3b 0a 20 20 7d  Cleanup(db);.  }
240f7 0a 23 65 6e 64 69 66 20 0a 0a 20 20 73 71 6c 69  .#endif ..  sqli
240f8 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
240f9 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a  Schema(db, 0);..
240fa 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61    /* If a transa
240fb 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 20 74  ction is open, t
240fc 68 65 20 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  he ResetInternal
240fd 53 63 68 65 6d 61 28 29 20 63 61 6c 6c 20 61 62  Schema() call ab
240fe 6f 76 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f  ove.  ** will no
240ff 74 20 68 61 76 65 20 63 61 6c 6c 65 64 20 74 68  t have called th
24100 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29 20  e xDisconnect() 
24101 6d 65 74 68 6f 64 20 6f 6e 20 61 6e 79 20 76 69  method on any vi
24102 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65  rtual.  ** table
24103 73 20 69 6e 20 74 68 65 20 64 62 2d 3e 61 56 54  s in the db->aVT
24104 72 61 6e 73 5b 5d 20 61 72 72 61 79 2e 20 54 68  rans[] array. Th
24105 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69  e following sqli
24106 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28  te3VtabRollback(
24107 29 0a 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c  ).  ** call will
24108 20 64 6f 20 73 6f 2e 20 57 65 20 6e 65 65 64 20   do so. We need 
24109 74 6f 20 64 6f 20 74 68 69 73 20 62 65 66 6f 72  to do this befor
2410a 65 20 74 68 65 20 63 68 65 63 6b 20 66 6f 72 20  e the check for 
2410b 61 63 74 69 76 65 0a 20 20 2a 2a 20 53 51 4c 20  active.  ** SQL 
2410c 73 74 61 74 65 6d 65 6e 74 73 20 62 65 6c 6f 77  statements below
2410d 2c 20 61 73 20 74 68 65 20 76 2d 74 61 62 6c 65  , as the v-table
2410e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2410f 6d 61 79 20 62 65 20 73 74 6f 72 69 6e 67 0a 20  may be storing. 
24110 20 2a 2a 20 73 6f 6d 65 20 70 72 65 70 61 72 65   ** some prepare
24111 64 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 74  d statements int
24112 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20  ernally..  */.  
24113 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
24114 61 63 6b 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49  ack(db);..  /* I
24115 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
24116 6f 75 74 73 74 61 6e 64 69 6e 67 20 56 4d 73 2c  outstanding VMs,
24117 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
24118 55 53 59 2e 20 2a 2f 0a 20 20 69 66 28 20 64 62  USY. */.  if( db
24119 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73  ->pVdbe ){.    s
2411a 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2411b 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20  SQLITE_BUSY, .  
2411c 20 20 20 20 20 20 22 55 6e 61 62 6c 65 20 74 6f        "Unable to
2411d 20 63 6c 6f 73 65 20 64 75 65 20 74 6f 20 75 6e   close due to un
2411e 66 69 6e 61 6c 69 73 65 64 20 73 74 61 74 65 6d  finalised statem
2411f 65 6e 74 73 22 29 3b 0a 20 20 20 20 73 71 6c 69  ents");.    sqli
24120 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
24121 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
24122 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
24123 53 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  SY;.  }.  assert
24124 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
24125 43 68 65 63 6b 28 64 62 29 20 29 3b 0a 0a 20 20  Check(db) );..  
24126 2f 2a 20 46 49 58 20 4d 45 3a 20 64 62 2d 3e 6d  /* FIX ME: db->m
24127 61 67 69 63 20 6d 61 79 20 62 65 20 73 65 74 20  agic may be set 
24128 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  to SQLITE_MAGIC_
24129 43 4c 4f 53 45 44 20 69 66 20 74 68 65 20 64 61  CLOSED if the da
2412a 74 61 62 61 73 65 0a 20 20 2a 2a 20 63 61 6e 6e  tabase.  ** cann
2412b 6f 74 20 62 65 20 6f 70 65 6e 65 64 20 66 6f 72  ot be opened for
2412c 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e 20 53 6f   some reason. So
2412d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65   this routine ne
2412e 65 64 73 20 74 6f 20 72 75 6e 20 69 6e 0a 20 20  eds to run in.  
2412f 2a 2a 20 74 68 61 74 20 63 61 73 65 2e 20 42 75  ** that case. Bu
24130 74 20 6d 61 79 62 65 20 74 68 65 72 65 20 73 68  t maybe there sh
24131 6f 75 6c 64 20 62 65 20 61 6e 20 65 78 74 72 61  ould be an extra
24132 20 6d 61 67 69 63 20 76 61 6c 75 65 20 66 6f 72   magic value for
24133 20 74 68 65 0a 20 20 2a 2a 20 22 66 61 69 6c 65   the.  ** "faile
24134 64 20 74 6f 20 6f 70 65 6e 22 20 73 74 61 74 65  d to open" state
24135 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f  ..  **.  ** TODO
24136 3a 20 43 6f 76 65 72 61 67 65 20 74 65 73 74 73  : Coverage tests
24137 20 64 6f 20 6e 6f 74 20 74 65 73 74 20 74 68 65   do not test the
24138 20 63 61 73 65 20 77 68 65 72 65 20 74 68 69 73   case where this
24139 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20   condition is.  
2413a 2a 2a 20 74 72 75 65 2e 20 49 74 27 73 20 68 61  ** true. It's ha
2413b 72 64 20 74 6f 20 73 65 65 20 68 6f 77 20 74 6f  rd to see how to
2413c 20 63 61 75 73 65 20 69 74 20 77 69 74 68 6f 75   cause it withou
2413d 74 20 6d 65 73 73 69 6e 67 20 77 69 74 68 20 74  t messing with t
2413e 68 72 65 61 64 73 2e 0a 20 20 2a 2f 0a 20 20 69  hreads..  */.  i
2413f 66 28 20 64 62 2d 3e 6d 61 67 69 63 21 3d 53 51  f( db->magic!=SQ
24140 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45  LITE_MAGIC_CLOSE
24141 44 20 26 26 20 73 71 6c 69 74 65 33 53 61 66 65  D && sqlite3Safe
24142 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
24143 2f 2a 20 70 72 69 6e 74 66 28 22 44 49 44 20 4e  /* printf("DID N
24144 4f 54 20 43 4c 4f 53 45 5c 6e 22 29 3b 20 66 66  OT CLOSE\n"); ff
24145 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 20 2a 2f  lush(stdout); */
24146 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
24147 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
24148 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ex);.    return 
24149 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2414a 7d 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  }..  for(j=0; j<
2414b 64 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29 7b 0a 20  db->nDb; j++){. 
2414c 20 20 20 73 74 72 75 63 74 20 44 62 20 2a 70 44     struct Db *pD
2414d 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 6a 5d 3b  b = &db->aDb[j];
2414e 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42  .    if( pDb->pB
2414f 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
24150 65 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62  e3BtreeClose(pDb
24151 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 70 44  ->pBt);.      pD
24152 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20  b->pBt = 0;.    
24153 20 20 69 66 28 20 6a 21 3d 31 20 29 7b 0a 20 20    if( j!=1 ){.  
24154 20 20 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65        pDb->pSche
24155 6d 61 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ma = 0;.      }.
24156 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
24157 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
24158 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20  Schema(db, 0);. 
24159 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62   assert( db->nDb
2415a 3c 3d 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28  <=2 );.  assert(
2415b 20 64 62 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44   db->aDb==db->aD
2415c 62 53 74 61 74 69 63 20 29 3b 0a 20 20 66 6f 72  bStatic );.  for
2415d 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (i=sqliteHashFir
2415e 73 74 28 26 64 62 2d 3e 61 46 75 6e 63 29 3b 20  st(&db->aFunc); 
2415f 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68 4e  i; i=sqliteHashN
24160 65 78 74 28 69 29 29 7b 0a 20 20 20 20 46 75 6e  ext(i)){.    Fun
24161 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 2a 70 4e  cDef *pFunc, *pN
24162 65 78 74 3b 0a 20 20 20 20 66 6f 72 28 70 46 75  ext;.    for(pFu
24163 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 73  nc = (FuncDef*)s
24164 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
24165 3b 20 70 46 75 6e 63 3b 20 70 46 75 6e 63 3d 70  ; pFunc; pFunc=p
24166 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65  Next){.      pNe
24167 78 74 20 3d 20 70 46 75 6e 63 2d 3e 70 4e 65 78  xt = pFunc->pNex
24168 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
24169 5f 66 72 65 65 28 70 46 75 6e 63 29 3b 0a 20 20  _free(pFunc);.  
2416a 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69    }.  }..  for(i
2416b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
2416c 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b  (&db->aCollSeq);
2416d 20 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68   i; i=sqliteHash
2416e 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 43 6f  Next(i)){.    Co
2416f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28  llSeq *pColl = (
24170 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69 74 65  CollSeq *)sqlite
24171 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20  HashData(i);.   
24172 20 2f 2a 20 49 6e 76 6f 6b 65 20 61 6e 79 20 64   /* Invoke any d
24173 65 73 74 72 75 63 74 6f 72 73 20 72 65 67 69 73  estructors regis
24174 74 65 72 65 64 20 66 6f 72 20 63 6f 6c 6c 61 74  tered for collat
24175 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65  ion sequence use
24176 72 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 66  r data. */.    f
24177 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b  or(j=0; j<3; j++
24178 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  ){.      if( pCo
24179 6c 6c 5b 6a 5d 2e 78 44 65 6c 20 29 7b 0a 20 20  ll[j].xDel ){.  
2417a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 6a 5d 2e 78        pColl[j].x
2417b 44 65 6c 28 70 43 6f 6c 6c 5b 6a 5d 2e 70 55 73  Del(pColl[j].pUs
2417c 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
2417d 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
2417e 72 65 65 28 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a  ree(pColl);.  }.
2417f 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65    sqlite3HashCle
24180 61 72 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71  ar(&db->aCollSeq
24181 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
24182 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
24183 42 4c 45 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69  BLE.  for(i=sqli
24184 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d  teHashFirst(&db-
24185 3e 61 4d 6f 64 75 6c 65 29 3b 20 69 3b 20 69 3d  >aModule); i; i=
24186 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69  sqliteHashNext(i
24187 29 29 7b 0a 20 20 20 20 4d 6f 64 75 6c 65 20 2a  )){.    Module *
24188 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 20 2a  pMod = (Module *
24189 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
2418a 69 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 6f 64  i);.    if( pMod
2418b 2d 3e 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20  ->xDestroy ){.  
2418c 20 20 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72      pMod->xDestr
2418d 6f 79 28 70 4d 6f 64 2d 3e 70 41 75 78 29 3b 0a  oy(pMod->pAux);.
2418e 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2418f 33 5f 66 72 65 65 28 70 4d 6f 64 29 3b 0a 20 20  3_free(pMod);.  
24190 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43  }.  sqlite3HashC
24191 6c 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  lear(&db->aModul
24192 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71  e);.#endif..  sq
24193 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
24194 64 62 2d 3e 61 46 75 6e 63 29 3b 0a 20 20 73 71  db->aFunc);.  sq
24195 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
24196 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a  QLITE_OK, 0); /*
24197 20 44 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79   Deallocates any
24198 20 63 61 63 68 65 64 20 65 72 72 6f 72 20 73 74   cached error st
24199 72 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 66 28 20  rings. */.  if( 
2419a 64 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20  db->pErr ){.    
2419b 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
2419c 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d 0a  (db->pErr);.  }.
2419d 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78    sqlite3CloseEx
2419e 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 0a 20  tensions(db);.. 
2419f 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
241a0 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b  ITE_MAGIC_ERROR;
241a1 0a 0a 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 2d  ..  /* The temp-
241a2 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
241a3 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69 66  is allocated dif
241a4 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68  ferently from th
241a5 65 20 6f 74 68 65 72 20 73 63 68 65 6d 61 0a 20  e other schema. 
241a6 20 2a 2a 20 6f 62 6a 65 63 74 73 20 28 75 73 69   ** objects (usi
241a7 6e 67 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  ng sqliteMalloc(
241a8 29 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74  ) directly, inst
241a9 65 61 64 20 6f 66 20 73 71 6c 69 74 65 33 42 74  ead of sqlite3Bt
241aa 72 65 65 53 63 68 65 6d 61 28 29 29 2e 0a 20 20  reeSchema())..  
241ab 2a 2a 20 53 6f 20 69 74 20 6e 65 65 64 73 20 74  ** So it needs t
241ac 6f 20 62 65 20 66 72 65 65 64 20 68 65 72 65 2e  o be freed here.
241ad 20 54 6f 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72   Todo: Why not r
241ae 6f 6c 6c 20 74 68 65 20 74 65 6d 70 20 73 63 68  oll the temp sch
241af 65 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68  ema into.  ** th
241b0 65 20 73 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c  e same sqliteMal
241b1 6c 6f 63 28 29 20 61 73 20 74 68 65 20 6f 6e 65  loc() as the one
241b2 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 65 73 20   that allocates 
241b3 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20  the database .  
241b4 2a 2a 20 73 74 72 75 63 74 75 72 65 3f 0a 20 20  ** structure?.  
241b5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  */.  sqlite3_fre
241b6 65 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  e(db->aDb[1].pSc
241b7 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33  hema);.  sqlite3
241b8 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
241b9 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74  >mutex);.  sqlit
241ba 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 64 62  e3_mutex_free(db
241bb 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69  ->mutex);.  sqli
241bc 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20 20  te3_free(db);.  
241bd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
241be 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62  ;.}../*.** Rollb
241bf 61 63 6b 20 61 6c 6c 20 64 61 74 61 62 61 73 65  ack all database
241c0 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
241c1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
241c2 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
241c3 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  l(sqlite3 *db){.
241c4 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69    int i;.  int i
241c5 6e 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 61 73  nTrans = 0;.  as
241c6 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
241c7 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
241c8 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  ex) );.  for(i=0
241c9 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
241ca 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61  ){.    if( db->a
241cb 44 62 5b 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20  Db[i].pBt ){.   
241cc 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
241cd 72 65 65 49 73 49 6e 54 72 61 6e 73 28 64 62 2d  reeIsInTrans(db-
241ce 3e 61 44 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a  >aDb[i].pBt) ){.
241cf 20 20 20 20 20 20 20 20 69 6e 54 72 61 6e 73 20          inTrans 
241d0 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
241d1 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52     sqlite3BtreeR
241d2 6f 6c 6c 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b  ollback(db->aDb[
241d3 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64  i].pBt);.      d
241d4 62 2d 3e 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e  b->aDb[i].inTran
241d5 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  s = 0;.    }.  }
241d6 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  .  sqlite3VtabRo
241d7 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 20 20 69 66  llback(db);.  if
241d8 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49  ( db->flags&SQLI
241d9 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
241da 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
241db 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61  xpirePreparedSta
241dc 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20  tements(db);.   
241dd 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
241de 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
241df 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  0);.  }..  /* If
241e0 20 6f 6e 65 20 68 61 73 20 62 65 65 6e 20 63 6f   one has been co
241e1 6e 66 69 67 75 72 65 64 2c 20 69 6e 76 6f 6b 65  nfigured, invoke
241e2 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2d 68 6f   the rollback-ho
241e3 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ok callback */. 
241e4 20 69 66 28 20 64 62 2d 3e 78 52 6f 6c 6c 62 61   if( db->xRollba
241e5 63 6b 43 61 6c 6c 62 61 63 6b 20 26 26 20 28 69  ckCallback && (i
241e6 6e 54 72 61 6e 73 20 7c 7c 20 21 64 62 2d 3e 61  nTrans || !db->a
241e7 75 74 6f 43 6f 6d 6d 69 74 29 20 29 7b 0a 20 20  utoCommit) ){.  
241e8 20 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43    db->xRollbackC
241e9 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 52 6f 6c  allback(db->pRol
241ea 6c 62 61 63 6b 41 72 67 29 3b 0a 20 20 7d 0a 7d  lbackArg);.  }.}
241eb 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
241ec 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 74   static string t
241ed 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68  hat describes th
241ee 65 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20  e kind of error 
241ef 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
241f0 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  .** argument..*/
241f1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
241f2 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
241f3 74 65 33 45 72 72 53 74 72 28 69 6e 74 20 72 63  te3ErrStr(int rc
241f4 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
241f5 2a 7a 3b 0a 20 20 73 77 69 74 63 68 28 20 72 63  *z;.  switch( rc
241f6 20 26 20 30 78 66 66 20 29 7b 0a 20 20 20 20 63   & 0xff ){.    c
241f7 61 73 65 20 53 51 4c 49 54 45 5f 52 4f 57 3a 0a  ase SQLITE_ROW:.
241f8 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
241f9 44 4f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 53  DONE:.    case S
241fa 51 4c 49 54 45 5f 4f 4b 3a 20 20 20 20 20 20 20  QLITE_OK:       
241fb 20 20 7a 20 3d 20 22 6e 6f 74 20 61 6e 20 65 72    z = "not an er
241fc 72 6f 72 22 3b 20 20 20 20 20 20 20 20 20 20 20  ror";           
241fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
241fe 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
241ff 51 4c 49 54 45 5f 45 52 52 4f 52 3a 20 20 20 20  QLITE_ERROR:    
24200 20 20 7a 20 3d 20 22 53 51 4c 20 6c 6f 67 69 63    z = "SQL logic
24201 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e   error or missin
24202 67 20 64 61 74 61 62 61 73 65 22 3b 20 20 20 62  g database";   b
24203 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24204 51 4c 49 54 45 5f 50 45 52 4d 3a 20 20 20 20 20  QLITE_PERM:     
24205 20 20 7a 20 3d 20 22 61 63 63 65 73 73 20 70 65    z = "access pe
24206 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 22  rmission denied"
24207 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  ;              b
24208 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24209 51 4c 49 54 45 5f 41 42 4f 52 54 3a 20 20 20 20  QLITE_ABORT:    
2420a 20 20 7a 20 3d 20 22 63 61 6c 6c 62 61 63 6b 20    z = "callback 
2420b 72 65 71 75 65 73 74 65 64 20 71 75 65 72 79 20  requested query 
2420c 61 62 6f 72 74 22 3b 20 20 20 20 20 20 20 20 62  abort";        b
2420d 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
2420e 51 4c 49 54 45 5f 42 55 53 59 3a 20 20 20 20 20  QLITE_BUSY:     
2420f 20 20 7a 20 3d 20 22 64 61 74 61 62 61 73 65 20    z = "database 
24210 69 73 20 6c 6f 63 6b 65 64 22 3b 20 20 20 20 20  is locked";     
24211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
24212 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24213 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3a 20 20 20  QLITE_LOCKED:   
24214 20 20 7a 20 3d 20 22 64 61 74 61 62 61 73 65 20    z = "database 
24215 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 22  table is locked"
24216 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  ;              b
24217 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24218 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3a 20 20 20 20  QLITE_NOMEM:    
24219 20 20 7a 20 3d 20 22 6f 75 74 20 6f 66 20 6d 65    z = "out of me
2421a 6d 6f 72 79 22 3b 20 20 20 20 20 20 20 20 20 20  mory";          
2421b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
2421c 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
2421d 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3a 20  QLITE_READONLY: 
2421e 20 20 7a 20 3d 20 22 61 74 74 65 6d 70 74 20 74    z = "attempt t
2421f 6f 20 77 72 69 74 65 20 61 20 72 65 61 64 6f 6e  o write a readon
24220 6c 79 20 64 61 74 61 62 61 73 65 22 3b 20 20 62  ly database";  b
24221 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24222 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3a  QLITE_INTERRUPT:
24223 20 20 7a 20 3d 20 22 69 6e 74 65 72 72 75 70 74    z = "interrupt
24224 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed";            
24225 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
24226 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24227 51 4c 49 54 45 5f 49 4f 45 52 52 3a 20 20 20 20  QLITE_IOERR:    
24228 20 20 7a 20 3d 20 22 64 69 73 6b 20 49 2f 4f 20    z = "disk I/O 
24229 65 72 72 6f 72 22 3b 20 20 20 20 20 20 20 20 20  error";         
2422a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
2422b 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
2422c 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3a 20 20  QLITE_CORRUPT:  
2422d 20 20 7a 20 3d 20 22 64 61 74 61 62 61 73 65 20    z = "database 
2422e 64 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61  disk image is ma
2422f 6c 66 6f 72 6d 65 64 22 3b 20 20 20 20 20 20 62  lformed";      b
24230 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24231 51 4c 49 54 45 5f 46 55 4c 4c 3a 20 20 20 20 20  QLITE_FULL:     
24232 20 20 7a 20 3d 20 22 64 61 74 61 62 61 73 65 20    z = "database 
24233 6f 72 20 64 69 73 6b 20 69 73 20 66 75 6c 6c 22  or disk is full"
24234 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  ;              b
24235 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24236 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3a 20  QLITE_CANTOPEN: 
24237 20 20 7a 20 3d 20 22 75 6e 61 62 6c 65 20 74 6f    z = "unable to
24238 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66   open database f
24239 69 6c 65 22 3b 20 20 20 20 20 20 20 20 20 20 62  ile";          b
2423a 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
2423b 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20 20 20 20  QLITE_EMPTY:    
2423c 20 20 7a 20 3d 20 22 74 61 62 6c 65 20 63 6f 6e    z = "table con
2423d 74 61 69 6e 73 20 6e 6f 20 64 61 74 61 22 3b 20  tains no data"; 
2423e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
2423f 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24240 51 4c 49 54 45 5f 53 43 48 45 4d 41 3a 20 20 20  QLITE_SCHEMA:   
24241 20 20 7a 20 3d 20 22 64 61 74 61 62 61 73 65 20    z = "database 
24242 73 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67  schema has chang
24243 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20 62  ed";           b
24244 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24245 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3a 20 20 20  QLITE_TOOBIG:   
24246 20 20 7a 20 3d 20 22 53 74 72 69 6e 67 20 6f 72    z = "String or
24247 20 42 4c 4f 42 20 65 78 63 65 65 64 65 64 20 73   BLOB exceeded s
24248 69 7a 65 20 6c 69 6d 69 74 22 3b 20 20 20 20 62  ize limit";    b
24249 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
2424a 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
2424b 3a 20 7a 20 3d 20 22 63 6f 6e 73 74 72 61 69 6e  : z = "constrain
2424c 74 20 66 61 69 6c 65 64 22 3b 20 20 20 20 20 20  t failed";      
2424d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
2424e 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
2424f 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3a 20  QLITE_MISMATCH: 
24250 20 20 7a 20 3d 20 22 64 61 74 61 74 79 70 65 20    z = "datatype 
24251 6d 69 73 6d 61 74 63 68 22 3b 20 20 20 20 20 20  mismatch";      
24252 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
24253 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24254 51 4c 49 54 45 5f 4d 49 53 55 53 45 3a 20 20 20  QLITE_MISUSE:   
24255 20 20 7a 20 3d 20 22 6c 69 62 72 61 72 79 20 72    z = "library r
24256 6f 75 74 69 6e 65 20 63 61 6c 6c 65 64 20 6f 75  outine called ou
24257 74 20 6f 66 20 73 65 71 75 65 6e 63 65 22 3b 62  t of sequence";b
24258 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24259 51 4c 49 54 45 5f 4e 4f 4c 46 53 3a 20 20 20 20  QLITE_NOLFS:    
2425a 20 20 7a 20 3d 20 22 6b 65 72 6e 65 6c 20 6c 61    z = "kernel la
2425b 63 6b 73 20 6c 61 72 67 65 20 66 69 6c 65 20 73  cks large file s
2425c 75 70 70 6f 72 74 22 3b 20 20 20 20 20 20 20 62  upport";       b
2425d 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
2425e 51 4c 49 54 45 5f 41 55 54 48 3a 20 20 20 20 20  QLITE_AUTH:     
2425f 20 20 7a 20 3d 20 22 61 75 74 68 6f 72 69 7a 61    z = "authoriza
24260 74 69 6f 6e 20 64 65 6e 69 65 64 22 3b 20 20 20  tion denied";   
24261 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
24262 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24263 51 4c 49 54 45 5f 46 4f 52 4d 41 54 3a 20 20 20  QLITE_FORMAT:   
24264 20 20 7a 20 3d 20 22 61 75 78 69 6c 69 61 72 79    z = "auxiliary
24265 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74   database format
24266 20 65 72 72 6f 72 22 3b 20 20 20 20 20 20 20 62   error";       b
24267 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
24268 51 4c 49 54 45 5f 52 41 4e 47 45 3a 20 20 20 20  QLITE_RANGE:    
24269 20 20 7a 20 3d 20 22 62 69 6e 64 20 6f 72 20 63    z = "bind or c
2426a 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 6f 75 74 20  olumn index out 
2426b 6f 66 20 72 61 6e 67 65 22 3b 20 20 20 20 20 62  of range";     b
2426c 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
2426d 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3a 20 20 20  QLITE_NOTADB:   
2426e 20 20 7a 20 3d 20 22 66 69 6c 65 20 69 73 20 65    z = "file is e
2426f 6e 63 72 79 70 74 65 64 20 6f 72 20 69 73 20 6e  ncrypted or is n
24270 6f 74 20 61 20 64 61 74 61 62 61 73 65 22 3b 62  ot a database";b
24271 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c  reak;.    defaul
24272 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t:              
24273 20 20 7a 20 3d 20 22 75 6e 6b 6e 6f 77 6e 20 65    z = "unknown e
24274 72 72 6f 72 22 3b 20 20 20 20 20 20 20 20 20 20  rror";          
24275 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
24276 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  reak;.  }.  retu
24277 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn z;.}../*.** T
24278 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c  his routine impl
24279 65 6d 65 6e 74 73 20 61 20 62 75 73 79 20 63 61  ements a busy ca
2427a 6c 6c 62 61 63 6b 20 74 68 61 74 20 73 6c 65 65  llback that slee
2427b 70 73 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a 20  ps and tries.** 
2427c 61 67 61 69 6e 20 75 6e 74 69 6c 20 61 20 74 69  again until a ti
2427d 6d 65 6f 75 74 20 76 61 6c 75 65 20 69 73 20 72  meout value is r
2427e 65 61 63 68 65 64 2e 20 20 54 68 65 20 74 69 6d  eached.  The tim
2427f 65 6f 75 74 20 76 61 6c 75 65 20 69 73 0a 2a 2a  eout value is.**
24280 20 61 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62   an integer numb
24281 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e  er of millisecon
24282 64 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 20  ds passed in as 
24283 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67  the first.** arg
24284 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ument..*/.static
24285 20 69 6e 74 20 73 71 6c 69 74 65 44 65 66 61 75   int sqliteDefau
24286 6c 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 28 0a  ltBusyCallback(.
24287 20 76 6f 69 64 20 2a 70 74 72 2c 20 20 20 20 20   void *ptr,     
24288 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
24289 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2428a 20 2a 2f 0a 20 69 6e 74 20 63 6f 75 6e 74 20 20   */. int count  
2428b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2428c 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   Number of times
2428d 20 74 61 62 6c 65 20 68 61 73 20 62 65 65 6e 20   table has been 
2428e 62 75 73 79 20 2a 2f 0a 29 7b 0a 23 69 66 20 4f  busy */.){.#if O
2428f 53 5f 57 49 4e 20 7c 7c 20 28 64 65 66 69 6e 65  S_WIN || (define
24290 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20 26  d(HAVE_USLEEP) &
24291 26 20 48 41 56 45 5f 55 53 4c 45 45 50 29 0a 20  & HAVE_USLEEP). 
24292 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
24293 20 64 65 6c 61 79 73 5b 5d 20 3d 0a 20 20 20 20   delays[] =.    
24294 20 7b 20 31 2c 20 32 2c 20 35 2c 20 31 30 2c 20   { 1, 2, 5, 10, 
24295 31 35 2c 20 32 30 2c 20 32 35 2c 20 32 35 2c 20  15, 20, 25, 25, 
24296 20 32 35 2c 20 20 35 30 2c 20 20 35 30 2c 20 31   25,  50,  50, 1
24297 30 30 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  00 };.  static c
24298 6f 6e 73 74 20 75 38 20 74 6f 74 61 6c 73 5b 5d  onst u8 totals[]
24299 20 3d 0a 20 20 20 20 20 7b 20 30 2c 20 31 2c 20   =.     { 0, 1, 
2429a 33 2c 20 20 38 2c 20 31 38 2c 20 33 33 2c 20 35  3,  8, 18, 33, 5
2429b 33 2c 20 37 38 2c 20 31 30 33 2c 20 31 32 38 2c  3, 78, 103, 128,
2429c 20 31 37 38 2c 20 32 32 38 20 7d 3b 0a 23 20 64   178, 228 };.# d
2429d 65 66 69 6e 65 20 4e 44 45 4c 41 59 20 28 73 69  efine NDELAY (si
2429e 7a 65 6f 66 28 64 65 6c 61 79 73 29 2f 73 69 7a  zeof(delays)/siz
2429f 65 6f 66 28 64 65 6c 61 79 73 5b 30 5d 29 29 0a  eof(delays[0])).
242a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
242a1 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b 0a  (sqlite3 *)ptr;.
242a2 20 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20    int timeout = 
242a3 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74 3b  db->busyTimeout;
242a4 0a 20 20 69 6e 74 20 64 65 6c 61 79 2c 20 70 72  .  int delay, pr
242a5 69 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ior;..  assert( 
242a6 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 69 66  count>=0 );.  if
242a7 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45 4c 41 59  ( count < NDELAY
242a8 20 29 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20   ){.    delay = 
242a9 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d 3b 0a 20  delays[count];. 
242aa 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74 61 6c     prior = total
242ab 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d 65 6c 73  s[count];.  }els
242ac 65 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20 64  e{.    delay = d
242ad 65 6c 61 79 73 5b 4e 44 45 4c 41 59 2d 31 5d 3b  elays[NDELAY-1];
242ae 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74  .    prior = tot
242af 61 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d 20 2b 20  als[NDELAY-1] + 
242b0 64 65 6c 61 79 2a 28 63 6f 75 6e 74 2d 28 4e 44  delay*(count-(ND
242b1 45 4c 41 59 2d 31 29 29 3b 0a 20 20 7d 0a 20 20  ELAY-1));.  }.  
242b2 69 66 28 20 70 72 69 6f 72 20 2b 20 64 65 6c 61  if( prior + dela
242b3 79 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a 20  y > timeout ){. 
242b4 20 20 20 64 65 6c 61 79 20 3d 20 74 69 6d 65 6f     delay = timeo
242b5 75 74 20 2d 20 70 72 69 6f 72 3b 0a 20 20 20 20  ut - prior;.    
242b6 69 66 28 20 64 65 6c 61 79 3c 3d 30 20 29 20 72  if( delay<=0 ) r
242b7 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73  eturn 0;.  }.  s
242b8 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62  qlite3OsSleep(db
242b9 2d 3e 70 56 66 73 2c 20 64 65 6c 61 79 2a 31 30  ->pVfs, delay*10
242ba 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  00);.  return 1;
242bb 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33  .#else.  sqlite3
242bc 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20   *db = (sqlite3 
242bd 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69 6d  *)ptr;.  int tim
242be 65 6f 75 74 20 3d 20 28 28 73 71 6c 69 74 65 33  eout = ((sqlite3
242bf 20 2a 29 70 74 72 29 2d 3e 62 75 73 79 54 69 6d   *)ptr)->busyTim
242c0 65 6f 75 74 3b 0a 20 20 69 66 28 20 28 63 6f 75  eout;.  if( (cou
242c1 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20 74 69 6d  nt+1)*1000 > tim
242c2 65 6f 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75  eout ){.    retu
242c3 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  rn 0;.  }.  sqli
242c4 74 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70  te3OsSleep(db->p
242c5 56 66 73 2c 20 31 30 30 30 30 30 30 29 3b 0a 20  Vfs, 1000000);. 
242c6 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69   return 1;.#endi
242c7 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  f.}../*.** Invok
242c8 65 20 74 68 65 20 67 69 76 65 6e 20 62 75 73 79  e the given busy
242c9 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20   handler..**.** 
242ca 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
242cb 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 6f  called when an o
242cc 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 65 64 20  peration failed 
242cd 77 69 74 68 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 20  with a lock..** 
242ce 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
242cf 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
242d0 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 72 65  , the lock is re
242d1 74 72 69 65 64 2e 20 20 49 66 20 69 74 0a 2a 2a  tried.  If it.**
242d2 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 65 20   returns 0, the 
242d3 6f 70 65 72 61 74 69 6f 6e 20 61 62 6f 72 74 73  operation aborts
242d4 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f   with an SQLITE_
242d5 42 55 53 59 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53  BUSY error..*/.S
242d6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
242d7 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42  t sqlite3InvokeB
242d8 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48  usyHandler(BusyH
242d9 61 6e 64 6c 65 72 20 2a 70 29 7b 0a 20 20 69 6e  andler *p){.  in
242da 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d 3d 30  t rc;.  if( p==0
242db 20 7c 7c 20 70 2d 3e 78 46 75 6e 63 3d 3d 30 20   || p->xFunc==0 
242dc 7c 7c 20 70 2d 3e 6e 42 75 73 79 3c 30 20 29 20  || p->nBusy<0 ) 
242dd 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 63 20 3d  return 0;.  rc =
242de 20 70 2d 3e 78 46 75 6e 63 28 70 2d 3e 70 41 72   p->xFunc(p->pAr
242df 67 2c 20 70 2d 3e 6e 42 75 73 79 29 3b 0a 20 20  g, p->nBusy);.  
242e0 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
242e1 20 70 2d 3e 6e 42 75 73 79 20 3d 20 2d 31 3b 0a   p->nBusy = -1;.
242e2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e    }else{.    p->
242e3 6e 42 75 73 79 2b 2b 3b 0a 20 20 7d 0a 20 20 72  nBusy++;.  }.  r
242e4 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f 2a  eturn rc; .}../*
242e5 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
242e6 20 73 65 74 73 20 74 68 65 20 62 75 73 79 20 63   sets the busy c
242e7 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53  allback for an S
242e8 71 6c 69 74 65 20 64 61 74 61 62 61 73 65 20 74  qlite database t
242e9 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63  o the.** given c
242ea 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
242eb 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
242ec 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  argument..*/.SQL
242ed 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
242ee 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
242ef 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
242f0 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 29 28  .  int (*xBusy)(
242f1 76 6f 69 64 2a 2c 69 6e 74 29 2c 0a 20 20 76 6f  void*,int),.  vo
242f2 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 69 66  id *pArg.){.  if
242f3 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  ( sqlite3SafetyC
242f4 68 65 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  heck(db) ){.    
242f5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
242f6 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  SUSE;.  }.  sqli
242f7 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
242f8 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62  db->mutex);.  db
242f9 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 78 46  ->busyHandler.xF
242fa 75 6e 63 20 3d 20 78 42 75 73 79 3b 0a 20 20 64  unc = xBusy;.  d
242fb 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 70  b->busyHandler.p
242fc 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 64 62  Arg = pArg;.  db
242fd 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42  ->busyHandler.nB
242fe 75 73 79 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  usy = 0;.  sqlit
242ff 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
24300 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
24301 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
24302 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
24303 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43  _OMIT_PROGRESS_C
24304 41 4c 4c 42 41 43 4b 0a 2f 2a 0a 2a 2a 20 54 68  ALLBACK./*.** Th
24305 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
24306 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
24307 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53 71 6c  lback for an Sql
24308 69 74 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  ite database to 
24309 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63 61 6c  the.** given cal
2430a 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77  lback function w
2430b 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 61 72  ith the given ar
2430c 67 75 6d 65 6e 74 2e 20 54 68 65 20 70 72 6f 67  gument. The prog
2430d 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 77 69  ress callback wi
2430e 6c 6c 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 64  ll.** be invoked
2430f 20 65 76 65 72 79 20 6e 4f 70 73 20 6f 70 63 6f   every nOps opco
24310 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  des..*/.SQLITE_A
24311 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
24312 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
24313 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
24314 20 0a 20 20 69 6e 74 20 6e 4f 70 73 2c 0a 20 20   .  int nOps,.  
24315 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73 73 29  int (*xProgress)
24316 28 76 6f 69 64 2a 29 2c 20 0a 20 20 76 6f 69 64  (void*), .  void
24317 20 2a 70 41 72 67 0a 29 7b 0a 20 20 69 66 28 20   *pArg.){.  if( 
24318 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68  !sqlite3SafetyCh
24319 65 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 73  eck(db) ){.    s
2431a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2431b 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
2431c 20 20 20 69 66 28 20 6e 4f 70 73 3e 30 20 29 7b     if( nOps>0 ){
2431d 0a 20 20 20 20 20 20 64 62 2d 3e 78 50 72 6f 67  .      db->xProg
2431e 72 65 73 73 20 3d 20 78 50 72 6f 67 72 65 73 73  ress = xProgress
2431f 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 50 72 6f  ;.      db->nPro
24320 67 72 65 73 73 4f 70 73 20 3d 20 6e 4f 70 73 3b  gressOps = nOps;
24321 0a 20 20 20 20 20 20 64 62 2d 3e 70 50 72 6f 67  .      db->pProg
24322 72 65 73 73 41 72 67 20 3d 20 70 41 72 67 3b 0a  ressArg = pArg;.
24323 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24324 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 3d   db->xProgress =
24325 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 50   0;.      db->nP
24326 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a  rogressOps = 0;.
24327 20 20 20 20 20 20 64 62 2d 3e 70 50 72 6f 67 72        db->pProgr
24328 65 73 73 41 72 67 20 3d 20 30 3b 0a 20 20 20 20  essArg = 0;.    
24329 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  }.    sqlite3_mu
2432a 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
2432b 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  tex);.  }.}.#end
2432c 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  if.../*.** This 
2432d 72 6f 75 74 69 6e 65 20 69 6e 73 74 61 6c 6c 73  routine installs
2432e 20 61 20 64 65 66 61 75 6c 74 20 62 75 73 79 20   a default busy 
2432f 68 61 6e 64 6c 65 72 20 74 68 61 74 20 77 61 69  handler that wai
24330 74 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 70  ts for the.** sp
24331 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f  ecified number o
24332 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 62  f milliseconds b
24333 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20  efore returning 
24334 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  0..*/.SQLITE_API
24335 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73   int sqlite3_bus
24336 79 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65  y_timeout(sqlite
24337 33 20 2a 64 62 2c 20 69 6e 74 20 6d 73 29 7b 0a  3 *db, int ms){.
24338 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
24339 65 74 79 43 68 65 63 6b 28 64 62 29 20 29 7b 0a  etyCheck(db) ){.
2433a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2433b 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
2433c 69 66 28 20 6d 73 3e 30 20 29 7b 0a 20 20 20 20  if( ms>0 ){.    
2433d 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74 20  db->busyTimeout 
2433e 3d 20 6d 73 3b 0a 20 20 20 20 73 71 6c 69 74 65  = ms;.    sqlite
2433f 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64  3_busy_handler(d
24340 62 2c 20 73 71 6c 69 74 65 44 65 66 61 75 6c 74  b, sqliteDefault
24341 42 75 73 79 43 61 6c 6c 62 61 63 6b 2c 20 28 76  BusyCallback, (v
24342 6f 69 64 2a 29 64 62 29 3b 0a 20 20 7d 65 6c 73  oid*)db);.  }els
24343 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  e{.    sqlite3_b
24344 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20  usy_handler(db, 
24345 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  0, 0);.  }.  ret
24346 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
24347 0a 0a 2f 2a 0a 2a 2a 20 43 61 75 73 65 20 61 6e  ../*.** Cause an
24348 79 20 70 65 6e 64 69 6e 67 20 6f 70 65 72 61 74  y pending operat
24349 69 6f 6e 20 74 6f 20 73 74 6f 70 20 61 74 20 69  ion to stop at i
2434a 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f  ts earliest oppo
2434b 72 74 75 6e 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49  rtunity..*/.SQLI
2434c 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2434d 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 73 71  te3_interrupt(sq
2434e 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66  lite3 *db){.  if
2434f 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 67  ( db && (db->mag
24350 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic==SQLITE_MAGIC
24351 5f 4f 50 45 4e 20 7c 7c 20 64 62 2d 3e 6d 61 67  _OPEN || db->mag
24352 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic==SQLITE_MAGIC
24353 5f 42 55 53 59 29 20 29 7b 0a 20 20 20 20 64 62  _BUSY) ){.    db
24354 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
24355 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 0a  ed = 1;.  }.}...
24356 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
24357 69 6f 6e 20 69 73 20 65 78 61 63 74 6c 79 20 74  ion is exactly t
24358 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74  he same as sqlit
24359 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2435a 6f 6e 28 29 2c 20 65 78 63 65 70 74 0a 2a 2a 20  on(), except.** 
2435b 74 68 61 74 20 69 74 20 69 73 20 64 65 73 69 67  that it is desig
2435c 6e 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64  ned to be called
2435d 20 62 79 20 69 6e 74 65 72 6e 61 6c 20 63 6f 64   by internal cod
2435e 65 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  e. The differenc
2435f 65 20 69 73 0a 2a 2a 20 74 68 61 74 20 69 66 20  e is.** that if 
24360 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  a malloc() fails
24361 20 69 6e 20 73 71 6c 69 74 65 33 5f 63 72 65 61   in sqlite3_crea
24362 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 61  te_function(), a
24363 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20  n error code.** 
24364 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
24365 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  the mallocFailed
24366 20 66 6c 61 67 20 63 6c 65 61 72 65 64 2e 20 0a   flag cleared. .
24367 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24368 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65  E int sqlite3Cre
24369 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ateFunc(.  sqlit
2436a 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
2436b 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e  char *zFunctionN
2436c 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c  ame,.  int nArg,
2436d 0a 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f  .  int enc,.  vo
2436e 69 64 20 2a 70 55 73 65 72 44 61 74 61 2c 0a 20  id *pUserData,. 
2436f 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
24370 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
24371 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
24372 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  e **),.  void (*
24373 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
24374 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
24375 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20  te3_value **),. 
24376 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
24377 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
24378 29 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  ).){.  FuncDef *
24379 70 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a  p;.  int nName;.
2437a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2437b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
2437c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
2437d 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  ( sqlite3SafetyC
2437e 68 65 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  heck(db) ){.    
2437f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
24380 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SUSE;.  }.  if( 
24381 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d 30  zFunctionName==0
24382 20 7c 7c 0a 20 20 20 20 20 20 28 78 46 75 6e 63   ||.      (xFunc
24383 20 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c 20 78   && (xFinal || x
24384 53 74 65 70 29 29 20 7c 7c 20 0a 20 20 20 20 20  Step)) || .     
24385 20 28 21 78 46 75 6e 63 20 26 26 20 28 78 46 69   (!xFunc && (xFi
24386 6e 61 6c 20 26 26 20 21 78 53 74 65 70 29 29 20  nal && !xStep)) 
24387 7c 7c 0a 20 20 20 20 20 20 28 21 78 46 75 6e 63  ||.      (!xFunc
24388 20 26 26 20 28 21 78 46 69 6e 61 6c 20 26 26 20   && (!xFinal && 
24389 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20 20  xStep)) ||.     
2438a 20 28 6e 41 72 67 3c 2d 31 20 7c 7c 20 6e 41 72   (nArg<-1 || nAr
2438b 67 3e 31 32 37 29 20 7c 7c 0a 20 20 20 20 20 20  g>127) ||.      
2438c 28 32 35 35 3c 28 6e 4e 61 6d 65 20 3d 20 73 74  (255<(nName = st
2438d 72 6c 65 6e 28 7a 46 75 6e 63 74 69 6f 6e 4e 61  rlen(zFunctionNa
2438e 6d 65 29 29 29 20 29 7b 0a 20 20 20 20 73 71 6c  me))) ){.    sql
2438f 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
24390 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 62 61 64  LITE_ERROR, "bad
24391 20 70 61 72 61 6d 65 74 65 72 73 22 29 3b 0a 20   parameters");. 
24392 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
24393 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 0a 23  _ERROR;.  }.  .#
24394 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24395 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 20 49 66  IT_UTF16.  /* If
24396 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73   SQLITE_UTF16 is
24397 20 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68   specified as th
24398 65 20 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c  e encoding type,
24399 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a   transform this.
2439a 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53    ** to one of S
2439b 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
2439c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20   SQLITE_UTF16BE 
2439d 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53  using the.  ** S
2439e 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
2439f 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f  E macro. SQLITE_
243a0 55 54 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65  UTF16 is not use
243a1 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20  d internally..  
243a2 2a 2a 0a 20 20 2a 2a 20 49 66 20 53 51 4c 49 54  **.  ** If SQLIT
243a3 45 5f 41 4e 59 20 69 73 20 73 70 65 63 69 66 69  E_ANY is specifi
243a4 65 64 2c 20 61 64 64 20 74 68 72 65 65 20 76 65  ed, add three ve
243a5 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 75  rsions of the fu
243a6 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 74  nction.  ** to t
243a7 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 20  he hash table.. 
243a8 20 2a 2f 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53   */.  if( enc==S
243a9 51 4c 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20  QLITE_UTF16 ){. 
243aa 20 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f     enc = SQLITE_
243ab 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d  UTF16NATIVE;.  }
243ac 65 6c 73 65 20 69 66 28 20 65 6e 63 3d 3d 53 51  else if( enc==SQ
243ad 4c 49 54 45 5f 41 4e 59 20 29 7b 0a 20 20 20 20  LITE_ANY ){.    
243ae 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d  int rc;.    rc =
243af 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
243b0 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  nc(db, zFunction
243b1 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49  Name, nArg, SQLI
243b2 54 45 5f 55 54 46 38 2c 0a 20 20 20 20 20 20 20  TE_UTF8,.       
243b3 20 20 70 55 73 65 72 44 61 74 61 2c 20 78 46 75    pUserData, xFu
243b4 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  nc, xStep, xFina
243b5 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  l);.    if( rc==
243b6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
243b7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43     rc = sqlite3C
243b8 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46  reateFunc(db, zF
243b9 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72  unctionName, nAr
243ba 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  g, SQLITE_UTF16L
243bb 45 2c 0a 20 20 20 20 20 20 20 20 20 20 70 55 73  E,.          pUs
243bc 65 72 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78  erData, xFunc, x
243bd 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20  Step, xFinal);. 
243be 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
243bf 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
243c0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
243c1 20 20 20 7d 0a 20 20 20 20 65 6e 63 20 3d 20 53     }.    enc = S
243c2 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20  QLITE_UTF16BE;. 
243c3 20 7d 0a 23 65 6c 73 65 0a 20 20 65 6e 63 20 3d   }.#else.  enc =
243c4 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 23 65   SQLITE_UTF8;.#e
243c5 6e 64 69 66 0a 20 20 0a 20 20 2f 2a 20 43 68 65  ndif.  .  /* Che
243c6 63 6b 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e  ck if an existin
243c7 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  g function is be
243c8 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 20 6f  ing overridden o
243c9 72 20 64 65 6c 65 74 65 64 2e 20 49 66 20 73 6f  r deleted. If so
243ca 2c 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65  ,.  ** and there
243cb 20 61 72 65 20 61 63 74 69 76 65 20 56 4d 73 2c   are active VMs,
243cc 20 74 68 65 6e 20 72 65 74 75 72 6e 20 53 51 4c   then return SQL
243cd 49 54 45 5f 42 55 53 59 2e 20 49 66 20 61 20 66  ITE_BUSY. If a f
243ce 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20  unction.  ** is 
243cf 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e  being overridden
243d0 2f 64 65 6c 65 74 65 64 20 62 75 74 20 74 68 65  /deleted but the
243d1 72 65 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  re are no active
243d2 20 56 4d 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0a   VMs, allow the.
243d3 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74    ** operation t
243d4 6f 20 63 6f 6e 74 69 6e 75 65 20 62 75 74 20 69  o continue but i
243d5 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72  nvalidate all pr
243d6 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d  ecompiled statem
243d7 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d  ents..  */.  p =
243d8 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
243d9 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69  tion(db, zFuncti
243da 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e  onName, nName, n
243db 41 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20  Arg, enc, 0);.  
243dc 69 66 28 20 70 20 26 26 20 70 2d 3e 69 50 72 65  if( p && p->iPre
243dd 66 45 6e 63 3d 3d 65 6e 63 20 26 26 20 70 2d 3e  fEnc==enc && p->
243de 6e 41 72 67 3d 3d 6e 41 72 67 20 29 7b 0a 20 20  nArg==nArg ){.  
243df 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65    if( db->active
243e0 56 64 62 65 43 6e 74 20 29 7b 0a 20 20 20 20 20  VdbeCnt ){.     
243e1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
243e2 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a  , SQLITE_BUSY, .
243e3 20 20 20 20 20 20 20 20 22 55 6e 61 62 6c 65 20          "Unable 
243e4 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79  to delete/modify
243e5 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64   user-function d
243e6 75 65 20 74 6f 20 61 63 74 69 76 65 20 73 74 61  ue to active sta
243e7 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20  tements");.     
243e8 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
243e9 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
243ea 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
243eb 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73  E_BUSY;.    }els
243ec 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
243ed 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
243ee 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
243ef 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73    }.  }..  p = s
243f0 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
243f1 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  on(db, zFunction
243f2 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
243f3 67 2c 20 65 6e 63 2c 20 31 29 3b 0a 20 20 61 73  g, enc, 1);.  as
243f4 73 65 72 74 28 70 20 7c 7c 20 64 62 2d 3e 6d 61  sert(p || db->ma
243f5 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 69  llocFailed);.  i
243f6 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74  f( !p ){.    ret
243f7 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
243f8 3b 0a 20 20 7d 0a 20 20 70 2d 3e 66 6c 61 67 73  ;.  }.  p->flags
243f9 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 46 75 6e 63   = 0;.  p->xFunc
243fa 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 2d 3e 78   = xFunc;.  p->x
243fb 53 74 65 70 20 3d 20 78 53 74 65 70 3b 0a 20 20  Step = xStep;.  
243fc 70 2d 3e 78 46 69 6e 61 6c 69 7a 65 20 3d 20 78  p->xFinalize = x
243fd 46 69 6e 61 6c 3b 0a 20 20 70 2d 3e 70 55 73 65  Final;.  p->pUse
243fe 72 44 61 74 61 20 3d 20 70 55 73 65 72 44 61 74  rData = pUserDat
243ff 61 3b 0a 20 20 70 2d 3e 6e 41 72 67 20 3d 20 6e  a;.  p->nArg = n
24400 41 72 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Arg;.  return SQ
24401 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
24402 2a 20 43 72 65 61 74 65 20 6e 65 77 20 75 73 65  * Create new use
24403 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  r functions..*/.
24404 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
24405 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
24406 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
24407 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3 *db,.  const c
24408 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  har *zFunctionNa
24409 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
2440a 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69    int enc,.  voi
2440b 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78  d *p,.  void (*x
2440c 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
2440d 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
2440e 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20  e3_value **),.  
2440f 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
24410 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
24411 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
24412 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78   **),.  void (*x
24413 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
24414 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e  ontext*).){.  in
24415 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
24416 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
24417 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74  mutex);.  assert
24418 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
24419 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  led );.  rc = sq
2441a 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
2441b 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  db, zFunctionNam
2441c 65 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70 2c  e, nArg, enc, p,
2441d 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78   xFunc, xStep, x
2441e 46 69 6e 61 6c 29 3b 0a 20 20 72 63 20 3d 20 73  Final);.  rc = s
2441f 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
24420 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  , rc);.  sqlite3
24421 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
24422 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
24423 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  n rc;.}..#ifndef
24424 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
24425 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  16.SQLITE_API in
24426 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
24427 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 73  _function16(.  s
24428 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
24429 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74  nst void *zFunct
2442a 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e  ionName,.  int n
2442b 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74  Arg,.  int eText
2442c 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a  Rep,.  void *p,.
2442d 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
2442e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2442f 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
24430 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  ue**),.  void (*
24431 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
24432 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
24433 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
24434 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
24435 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
24436 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
24437 63 68 61 72 20 2a 7a 46 75 6e 63 38 3b 0a 20 20  char *zFunc8;.  
24438 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
24439 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
2443a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
2443b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2443c 20 7a 46 75 6e 63 38 20 3d 20 73 71 6c 69 74 65   zFunc8 = sqlite
2443d 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 46  3Utf16to8(db, zF
2443e 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 2d 31 29  unctionName, -1)
2443f 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
24440 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a  CreateFunc(db, z
24441 46 75 6e 63 38 2c 20 6e 41 72 67 2c 20 65 54 65  Func8, nArg, eTe
24442 78 74 52 65 70 2c 20 70 2c 20 78 46 75 6e 63 2c  xtRep, p, xFunc,
24443 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b   xStep, xFinal);
24444 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
24445 7a 46 75 6e 63 38 29 3b 0a 20 20 72 63 20 3d 20  zFunc8);.  rc = 
24446 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
24447 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
24448 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
24449 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
2444a 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a  rn rc;.}.#endif.
2444b 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20  ../*.** Declare 
2444c 74 68 61 74 20 61 20 66 75 6e 63 74 69 6f 6e 20  that a function 
2444d 68 61 73 20 62 65 65 6e 20 6f 76 65 72 6c 6f 61  has been overloa
2444e 64 65 64 20 62 79 20 61 20 76 69 72 74 75 61 6c  ded by a virtual
2444f 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
24450 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 61 6c   the function al
24451 72 65 61 64 79 20 65 78 69 73 74 73 20 61 73 20  ready exists as 
24452 61 20 72 65 67 75 6c 61 72 20 67 6c 6f 62 61 6c  a regular global
24453 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 0a   function, then.
24454 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
24455 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 20  is a no-op.  If 
24456 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  the function doe
24457 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74 68 65  s not exist, the
24458 6e 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65  n create.** a ne
24459 77 20 6f 6e 65 20 74 68 61 74 20 61 6c 77 61 79  w one that alway
2445a 73 20 74 68 72 6f 77 73 20 61 20 72 75 6e 2d 74  s throws a run-t
2445b 69 6d 65 20 65 72 72 6f 72 2e 20 20 0a 2a 2a 0a  ime error.  .**.
2445c 2a 2a 20 57 68 65 6e 20 76 69 72 74 75 61 6c 20  ** When virtual 
2445d 74 61 62 6c 65 73 20 69 6e 74 65 6e 64 20 74 6f  tables intend to
2445e 20 70 72 6f 76 69 64 65 20 61 6e 20 6f 76 65 72   provide an over
2445f 6c 6f 61 64 65 64 20 66 75 6e 63 74 69 6f 6e 2c  loaded function,
24460 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20   they.** should 
24461 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
24462 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  e to make sure t
24463 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69  he global functi
24464 6f 6e 20 65 78 69 73 74 73 2e 0a 2a 2a 20 41 20  on exists..** A 
24465 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20  global function 
24466 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f 72  must exist in or
24467 64 65 72 20 66 6f 72 20 6e 61 6d 65 20 72 65 73  der for name res
24468 6f 6c 75 74 69 6f 6e 20 74 6f 20 77 6f 72 6b 0a  olution to work.
24469 2a 2a 20 70 72 6f 70 65 72 6c 79 2e 0a 2a 2f 0a  ** properly..*/.
2446a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2446b 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
2446c 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69  function(.  sqli
2446d 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74  te3 *db,.  const
2446e 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20   char *zName,.  
2446f 69 6e 74 20 6e 41 72 67 0a 29 7b 0a 20 20 69 6e  int nArg.){.  in
24470 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e  t nName = strlen
24471 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72  (zName);.  int r
24472 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
24473 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
24474 65 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ex);.  if( sqlit
24475 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64  e3FindFunction(d
24476 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  b, zName, nName,
24477 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54   nArg, SQLITE_UT
24478 46 38 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20  F8, 0)==0 ){.   
24479 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
2447a 6e 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41  nc(db, zName, nA
2447b 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  rg, SQLITE_UTF8,
2447c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2447d 20 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65         0, sqlite
2447e 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e  3InvalidFunction
2447f 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  , 0, 0);.  }.  r
24480 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
24481 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  it(db, SQLITE_OK
24482 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
24483 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
24484 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
24485 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
24486 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f  ITE_OMIT_TRACE./
24487 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
24488 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 2e 20  trace function. 
24489 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74   The pArg from t
2448a 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  he previously re
2448b 67 69 73 74 65 72 65 64 20 74 72 61 63 65 0a 2a  gistered trace.*
2448c 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  * is returned.  
2448d 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 74 72  .**.** A NULL tr
2448e 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61  ace function mea
2448f 6e 73 20 74 68 61 74 20 6e 6f 20 74 72 61 63 69  ns that no traci
24490 6e 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20  ng is executes. 
24491 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74   A non-NULL.** t
24492 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65  race is a pointe
24493 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  r to a function 
24494 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20  that is invoked 
24495 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
24496 65 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74 61 74  each.** SQL stat
24497 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ement..*/.SQLITE
24498 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
24499 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33  e3_trace(sqlite3
2449a 20 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78 54 72   *db, void (*xTr
2449b 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ace)(void*,const
2449c 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 20 2a 70   char*), void *p
2449d 41 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f  Arg){.  void *pO
2449e 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ld;.  sqlite3_mu
2449f 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
244a0 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64  tex);.  pOld = d
244a1 62 2d 3e 70 54 72 61 63 65 41 72 67 3b 0a 20 20  b->pTraceArg;.  
244a2 64 62 2d 3e 78 54 72 61 63 65 20 3d 20 78 54 72  db->xTrace = xTr
244a3 61 63 65 3b 0a 20 20 64 62 2d 3e 70 54 72 61 63  ace;.  db->pTrac
244a4 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  eArg = pArg;.  s
244a5 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
244a6 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
244a7 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a   return pOld;.}.
244a8 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
244a9 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f   profile functio
244aa 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f  n.  The pArg fro
244ab 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  m the previously
244ac 20 72 65 67 69 73 74 65 72 65 64 20 0a 2a 2a 20   registered .** 
244ad 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e  profile function
244ae 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a   is returned.  .
244af 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70 72 6f  **.** A NULL pro
244b0 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65  file function me
244b1 61 6e 73 20 74 68 61 74 20 6e 6f 20 70 72 6f 66  ans that no prof
244b2 69 6c 69 6e 67 20 69 73 20 65 78 65 63 75 74 65  iling is execute
244b3 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a  s.  A non-NULL.*
244b4 2a 20 70 72 6f 66 69 6c 65 20 69 73 20 61 20 70  * profile is a p
244b5 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
244b6 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76  tion that is inv
244b7 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63  oked at the conc
244b8 6c 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63  lusion of.** eac
244b9 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  h SQL statement 
244ba 74 68 61 74 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a  that is run..*/.
244bb 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
244bc 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65  *sqlite3_profile
244bd 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
244be 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69  .  void (*xProfi
244bf 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  le)(void*,const 
244c0 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e  char*,sqlite_uin
244c1 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  t64),.  void *pA
244c2 72 67 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f  rg.){.  void *pO
244c3 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ld;.  sqlite3_mu
244c4 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
244c5 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64  tex);.  pOld = d
244c6 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 3b 0a  b->pProfileArg;.
244c7 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 3d    db->xProfile =
244c8 20 78 50 72 6f 66 69 6c 65 3b 0a 20 20 64 62 2d   xProfile;.  db-
244c9 3e 70 50 72 6f 66 69 6c 65 41 72 67 20 3d 20 70  >pProfileArg = p
244ca 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Arg;.  sqlite3_m
244cb 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
244cc 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
244cd 70 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  pOld;.}.#endif /
244ce 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  * SQLITE_OMIT_TR
244cf 41 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50  ACE */../*** EXP
244d0 45 52 49 4d 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a  ERIMENTAL ***.**
244d1 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 66  .** Register a f
244d2 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e  unction to be in
244d3 76 6f 6b 65 64 20 77 68 65 6e 20 61 20 74 72 61  voked when a tra
244d4 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74  nsaction comment
244d5 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 76  s..** If the inv
244d6 6f 6b 65 64 20 66 75 6e 63 74 69 6f 6e 20 72 65  oked function re
244d7 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  turns non-zero, 
244d8 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20  then the commit 
244d9 62 65 63 6f 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c  becomes a.** rol
244da 6c 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  lback..*/.SQLITE
244db 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
244dc 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a  e3_commit_hook(.
244dd 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
244de 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
244df 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74  ttach the hook t
244e0 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
244e1 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c  */.  int (*xCall
244e2 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 20 2f  back)(void*),  /
244e3 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  * Function to in
244e4 76 6f 6b 65 20 6f 6e 20 65 61 63 68 20 63 6f 6d  voke on each com
244e5 6d 69 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  mit */.  void *p
244e6 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Arg             
244e7 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
244e8 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
244e9 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c  /.){.  void *pOl
244ea 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
244eb 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
244ec 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62  ex);.  pOld = db
244ed 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 3b 0a 20 20  ->pCommitArg;.  
244ee 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62  db->xCommitCallb
244ef 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b  ack = xCallback;
244f0 0a 20 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72  .  db->pCommitAr
244f1 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69  g = pArg;.  sqli
244f2 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
244f3 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
244f4 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a  turn pOld;.}../*
244f5 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63  .** Register a c
244f6 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e  allback to be in
244f7 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20  voked each time 
244f8 61 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64  a row is updated
244f9 2c 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 6f 72  ,.** inserted or
244fa 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74   deleted using t
244fb 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e  his database con
244fc 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  nection..*/.SQLI
244fd 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
244fe 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b  ite3_update_hook
244ff 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
24500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24501 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b   Attach the hook
24502 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73   to this databas
24503 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43  e */.  void (*xC
24504 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69  allback)(void*,i
24505 6e 74 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c  nt,char const *,
24506 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c  char const *,sql
24507 69 74 65 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f  ite_int64),.  vo
24508 69 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20  id *pArg        
24509 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
2450a 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
2450b 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64  ion */.){.  void
2450c 20 2a 70 52 65 74 3b 0a 20 20 73 71 6c 69 74 65   *pRet;.  sqlite
2450d 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
2450e 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 52 65 74  ->mutex);.  pRet
2450f 20 3d 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72   = db->pUpdateAr
24510 67 3b 0a 20 20 64 62 2d 3e 78 55 70 64 61 74 65  g;.  db->xUpdate
24511 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c  Callback = xCall
24512 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 55 70 64  back;.  db->pUpd
24513 61 74 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  ateArg = pArg;. 
24514 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
24515 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
24516 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a  .  return pRet;.
24517 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
24518 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  r a callback to 
24519 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20  be invoked each 
2451a 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69  time a transacti
2451b 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20  on is rolled.** 
2451c 62 61 63 6b 20 62 79 20 74 68 69 73 20 64 61 74  back by this dat
2451d 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2451e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2451f 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f  void *sqlite3_ro
24520 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73  llback_hook(.  s
24521 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
24522 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61           /* Atta
24523 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74  ch the hook to t
24524 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  his database */.
24525 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61    void (*xCallba
24526 63 6b 29 28 76 6f 69 64 2a 29 2c 20 2f 2a 20 43  ck)(void*), /* C
24527 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
24528 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67   */.  void *pArg
24529 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2452a 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
2452b 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  he function */.)
2452c 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a  {.  void *pRet;.
2452d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2452e 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2452f 3b 0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e 70  ;.  pRet = db->p
24530 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 0a 20 20 64  RollbackArg;.  d
24531 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  b->xRollbackCall
24532 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
24533 3b 0a 20 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63  ;.  db->pRollbac
24534 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  kArg = pArg;.  s
24535 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
24536 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
24537 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
24538 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
24539 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ine is called to
2453a 20 63 72 65 61 74 65 20 61 20 63 6f 6e 6e 65 63   create a connec
2453b 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61  tion to a databa
2453c 73 65 20 42 54 72 65 65 0a 2a 2a 20 64 72 69 76  se BTree.** driv
2453d 65 72 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d  er.  If zFilenam
2453e 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  e is the name of
2453f 20 61 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 68   a file, then th
24540 61 74 20 66 69 6c 65 20 69 73 0a 2a 2a 20 6f 70  at file is.** op
24541 65 6e 65 64 20 61 6e 64 20 75 73 65 64 2e 20 20  ened and used.  
24542 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  If zFilename is 
24543 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22  the magic name "
24544 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 0a 2a  :memory:" then.*
24545 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  * the database i
24546 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f  s stored in memo
24547 72 79 20 28 61 6e 64 20 69 73 20 74 68 75 73 20  ry (and is thus 
24548 66 6f 72 67 6f 74 74 65 6e 20 61 73 20 73 6f 6f  forgotten as soo
24549 6e 20 61 73 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e  n as.** the conn
2454a 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64  ection is closed
2454b 2e 29 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65  .)  If zFilename
2454c 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68   is NULL then th
2454d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73  e database.** is
2454e 20 61 20 22 76 69 72 74 75 61 6c 22 20 64 61 74   a "virtual" dat
2454f 61 62 61 73 65 20 66 6f 72 20 74 72 61 6e 73 69  abase for transi
24550 65 6e 74 20 75 73 65 20 6f 6e 6c 79 20 61 6e 64  ent use only and
24551 20 69 73 20 64 65 6c 65 74 65 64 20 61 73 0a 2a   is deleted as.*
24552 2a 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 63 6f  * soon as the co
24553 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73  nnection is clos
24554 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74  ed..**.** A virt
24555 75 61 6c 20 64 61 74 61 62 61 73 65 20 63 61 6e  ual database can
24556 20 62 65 20 65 69 74 68 65 72 20 61 20 64 69 73   be either a dis
24557 6b 20 66 69 6c 65 20 28 74 68 61 74 20 69 73 20  k file (that is 
24558 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a  automatically.**
24559 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
2455a 65 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64  e file is closed
2455b 29 20 6f 72 20 69 74 20 61 6e 20 62 65 20 68 65  ) or it an be he
2455c 6c 64 20 65 6e 74 69 72 65 6c 79 20 69 6e 20 6d  ld entirely in m
2455d 65 6d 6f 72 79 2c 0a 2a 2a 20 64 65 70 65 6e 64  emory,.** depend
2455e 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  ing on the value
2455f 73 20 6f 66 20 74 68 65 20 54 45 4d 50 5f 53 54  s of the TEMP_ST
24560 4f 52 45 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  ORE compile-time
24561 20 6d 61 63 72 6f 20 61 6e 64 20 74 68 65 0a 2a   macro and the.*
24562 2a 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65  * db->temp_store
24563 20 76 61 72 69 61 62 6c 65 2c 20 61 63 63 6f 72   variable, accor
24564 64 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c  ding to the foll
24565 6f 77 69 6e 67 20 63 68 61 72 74 3a 0a 2a 2a 0a  owing chart:.**.
24566 2a 2a 20 20 20 20 20 20 20 54 45 4d 50 5f 53 54  **       TEMP_ST
24567 4f 52 45 20 20 20 20 20 64 62 2d 3e 74 65 6d 70  ORE     db->temp
24568 5f 73 74 6f 72 65 20 20 20 20 20 4c 6f 63 61 74  _store     Locat
24569 69 6f 6e 20 6f 66 20 74 65 6d 70 6f 72 61 72 79  ion of temporary
2456a 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20   database.**    
2456b 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20     ----------   
2456c 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
2456d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       -----------
2456e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2456f 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ---.**          
24570 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
24571 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
24572 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20   file.**        
24573 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20     1            
24574 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
24575 20 20 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 20     file.**      
24576 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20       1          
24577 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20        2         
24578 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20       memory.**  
24579 20 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20           1      
2457a 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 20            0     
2457b 20 20 20 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a           file.**
2457c 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20             2    
2457d 20 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20              1   
2457e 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 0a             file.
2457f 2a 2a 20 20 20 20 20 20 20 20 20 20 20 32 20 20  **           2  
24580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 20                2 
24581 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
24582 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ory.**          
24583 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
24584 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
24585 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20   memory.**      
24586 20 20 20 20 20 33 20 20 20 20 20 20 20 20 20 20       3          
24587 20 20 20 20 20 61 6e 79 20 20 20 20 20 20 20 20       any        
24588 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 53       memory.*/.S
24589 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2458a 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61  t sqlite3BtreeFa
2458b 63 74 6f 72 79 28 0a 20 20 63 6f 6e 73 74 20 73  ctory(.  const s
2458c 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2458d 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74 61 62     /* Main datab
2458e 61 73 65 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67  ase when opening
2458f 20 61 75 78 20 6f 74 68 65 72 77 69 73 65 20 30   aux otherwise 0
24590 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
24591 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20   *zFilename,    
24592 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
24593 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ile containing t
24594 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61 73  he BTree databas
24595 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d 69 74 4a  e */.  int omitJ
24596 6f 75 72 6e 61 6c 2c 20 20 20 20 20 20 20 20 20  ournal,         
24597 20 2f 2a 20 69 66 20 54 52 55 45 20 74 68 65 6e   /* if TRUE then
24598 20 64 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 20   do not journal 
24599 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69  this file */.  i
2459a 6e 74 20 6e 43 61 63 68 65 2c 20 20 20 20 20 20  nt nCache,      
2459b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
2459c 6d 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74 68  many pages in th
2459d 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a  e page cache */.
2459e 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c 20    int vfsFlags, 
2459f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
245a0 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f  lags passed thro
245a1 75 67 68 20 74 6f 20 76 66 73 4f 70 65 6e 20 2a  ugh to vfsOpen *
245a2 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74  /.  Btree **ppBt
245a3 72 65 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ree           /*
245a4 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20   Pointer to new 
245a5 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69  Btree object wri
245a6 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  tten here */.){.
245a7 20 20 69 6e 74 20 62 74 46 6c 61 67 73 20 3d 20    int btFlags = 
245a8 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 0a  0;.  int rc;.  .
245a9 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
245aa 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
245ab 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
245ac 65 72 74 28 20 70 70 42 74 72 65 65 20 21 3d 20  ert( ppBtree != 
245ad 30 29 3b 0a 20 20 69 66 28 20 6f 6d 69 74 4a 6f  0);.  if( omitJo
245ae 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 62 74 46  urnal ){.    btF
245af 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4f 4d  lags |= BTREE_OM
245b0 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 7d 0a  IT_JOURNAL;.  }.
245b1 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
245b2 26 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c  & SQLITE_NoReadl
245b3 6f 63 6b 20 29 7b 0a 20 20 20 20 62 74 46 6c 61  ock ){.    btFla
245b4 67 73 20 7c 3d 20 42 54 52 45 45 5f 4e 4f 5f 52  gs |= BTREE_NO_R
245b5 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 69  EADLOCK;.  }.  i
245b6 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20  f( zFilename==0 
245b7 29 7b 0a 23 69 66 20 54 45 4d 50 5f 53 54 4f 52  ){.#if TEMP_STOR
245b8 45 3d 3d 30 0a 20 20 20 20 2f 2a 20 44 6f 20 6e  E==0.    /* Do n
245b9 6f 74 68 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66  othing */.#endif
245ba 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
245bb 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 69  OMIT_MEMORYDB.#i
245bc 66 20 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a  f TEMP_STORE==1.
245bd 20 20 20 20 69 66 28 20 64 62 2d 3e 74 65 6d 70      if( db->temp
245be 5f 73 74 6f 72 65 3d 3d 32 20 29 20 7a 46 69 6c  _store==2 ) zFil
245bf 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79  ename = ":memory
245c0 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 54  :";.#endif.#if T
245c1 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 0a 20 20 20  EMP_STORE==2.   
245c2 20 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74   if( db->temp_st
245c3 6f 72 65 21 3d 31 20 29 20 7a 46 69 6c 65 6e 61  ore!=1 ) zFilena
245c4 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79 3a 22 3b  me = ":memory:";
245c5 0a 23 65 6e 64 69 66 0a 23 69 66 20 54 45 4d 50  .#endif.#if TEMP
245c6 5f 53 54 4f 52 45 3d 3d 33 0a 20 20 20 20 7a 46  _STORE==3.    zF
245c7 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f  ilename = ":memo
245c8 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 65 6e  ry:";.#endif.#en
245c9 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
245ca 49 54 5f 4d 45 4d 4f 52 59 44 42 20 2a 2f 0a 20  IT_MEMORYDB */. 
245cb 20 7d 0a 0a 20 20 69 66 28 20 28 76 66 73 46 6c   }..  if( (vfsFl
245cc 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
245cd 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20 26 26  N_MAIN_DB)!=0 &&
245ce 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c   (zFilename==0 |
245cf 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 29  | *zFilename==0)
245d0 20 29 7b 0a 20 20 20 20 76 66 73 46 6c 61 67 73   ){.    vfsFlags
245d1 20 3d 20 28 76 66 73 46 6c 61 67 73 20 26 20 7e   = (vfsFlags & ~
245d2 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
245d3 5f 44 42 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50  _DB) | SQLITE_OP
245d4 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 20 7d 0a  EN_TEMP_DB;.  }.
245d5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
245d6 72 65 65 4f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  reeOpen(zFilenam
245d7 65 2c 20 28 73 71 6c 69 74 65 33 20 2a 29 64 62  e, (sqlite3 *)db
245d8 2c 20 70 70 42 74 72 65 65 2c 20 62 74 46 6c 61  , ppBtree, btFla
245d9 67 73 2c 20 76 66 73 46 6c 61 67 73 29 3b 0a 20  gs, vfsFlags);. 
245da 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
245db 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
245dc 33 42 74 72 65 65 53 65 74 42 75 73 79 48 61 6e  3BtreeSetBusyHan
245dd 64 6c 65 72 28 2a 70 70 42 74 72 65 65 2c 20 28  dler(*ppBtree, (
245de 76 6f 69 64 2a 29 26 64 62 2d 3e 62 75 73 79 48  void*)&db->busyH
245df 61 6e 64 6c 65 72 29 3b 0a 20 20 20 20 73 71 6c  andler);.    sql
245e0 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
245e1 65 53 69 7a 65 28 2a 70 70 42 74 72 65 65 2c 20  eSize(*ppBtree, 
245e2 6e 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72  nCache);.  }.  r
245e3 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
245e4 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 38 20  ** Return UTF-8 
245e5 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68 20  encoded English 
245e6 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e 61  language explana
245e7 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74  tion of the most
245e8 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f 72   recent.** error
245e9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
245ea 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
245eb 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c 69 74  te3_errmsg(sqlit
245ec 65 33 20 2a 64 62 29 7b 0a 20 20 63 6f 6e 73 74  e3 *db){.  const
245ed 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20   char *z;.  if( 
245ee 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !db ){.    retur
245ef 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  n sqlite3ErrStr(
245f0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20  SQLITE_NOMEM);. 
245f1 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
245f2 53 61 66 65 74 79 43 68 65 63 6b 28 64 62 29 20  SafetyCheck(db) 
245f3 7c 7c 20 64 62 2d 3e 65 72 72 43 6f 64 65 3d 3d  || db->errCode==
245f4 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 29 7b  SQLITE_MISUSE ){
245f5 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
245f6 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54 45  te3ErrStr(SQLITE
245f7 5f 4d 49 53 55 53 45 29 3b 0a 20 20 7d 0a 20 20  _MISUSE);.  }.  
245f8 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
245f9 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
245fa 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
245fb 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
245fc 20 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69   z = (char*)sqli
245fd 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64  te3_value_text(d
245fe 62 2d 3e 70 45 72 72 29 3b 0a 20 20 69 66 28 20  b->pErr);.  if( 
245ff 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 20 3d 20  z==0 ){.    z = 
24600 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 64 62  sqlite3ErrStr(db
24601 2d 3e 65 72 72 43 6f 64 65 29 3b 0a 20 20 7d 0a  ->errCode);.  }.
24602 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
24603 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
24604 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a  ;.  return z;.}.
24605 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24606 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
24607 20 52 65 74 75 72 6e 20 55 54 46 2d 31 36 20 65   Return UTF-16 e
24608 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68 20 6c  ncoded English l
24609 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e 61 74  anguage explanat
2460a 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20  ion of the most 
2460b 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e  recent.** error.
2460c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
2460d 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
2460e 65 33 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69  e3_errmsg16(sqli
2460f 74 65 33 20 2a 64 62 29 7b 0a 20 20 2f 2a 20 42  te3 *db){.  /* B
24610 65 63 61 75 73 65 20 61 6c 6c 20 74 68 65 20 63  ecause all the c
24611 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65  haracters in the
24612 20 73 74 72 69 6e 67 20 61 72 65 20 69 6e 20 74   string are in t
24613 68 65 20 75 6e 69 63 6f 64 65 0a 20 20 2a 2a 20  he unicode.  ** 
24614 72 61 6e 67 65 20 30 78 30 30 2d 30 78 46 46 2c  range 0x00-0xFF,
24615 20 69 66 20 77 65 20 70 61 64 20 74 68 65 20 62   if we pad the b
24616 69 67 2d 65 6e 64 69 61 6e 20 73 74 72 69 6e 67  ig-endian string
24617 20 77 69 74 68 20 61 20 0a 20 20 2a 2a 20 7a 65   with a .  ** ze
24618 72 6f 20 62 79 74 65 2c 20 77 65 20 63 61 6e 20  ro byte, we can 
24619 6f 62 74 61 69 6e 20 74 68 65 20 6c 69 74 74 6c  obtain the littl
2461a 65 2d 65 6e 64 69 61 6e 20 73 74 72 69 6e 67 20  e-endian string 
2461b 77 69 74 68 0a 20 20 2a 2a 20 26 62 69 67 5f 65  with.  ** &big_e
2461c 6e 64 69 61 6e 5b 31 5d 2e 0a 20 20 2a 2f 0a 20  ndian[1]..  */. 
2461d 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
2461e 61 72 20 6f 75 74 4f 66 4d 65 6d 42 65 5b 5d 20  ar outOfMemBe[] 
2461f 3d 20 7b 0a 20 20 20 20 30 2c 20 27 6f 27 2c 20  = {.    0, 'o', 
24620 30 2c 20 27 75 27 2c 20 30 2c 20 27 74 27 2c 20  0, 'u', 0, 't', 
24621 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20  0, ' ', .    0, 
24622 27 6f 27 2c 20 30 2c 20 27 66 27 2c 20 30 2c 20  'o', 0, 'f', 0, 
24623 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 6d 27  ' ', .    0, 'm'
24624 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 27 6d 27  , 0, 'e', 0, 'm'
24625 2c 20 30 2c 20 27 6f 27 2c 20 30 2c 20 27 72 27  , 0, 'o', 0, 'r'
24626 2c 20 30 2c 20 27 79 27 2c 20 30 2c 20 30 2c 20  , 0, 'y', 0, 0, 
24627 30 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  0.  };.  static 
24628 63 6f 6e 73 74 20 63 68 61 72 20 6d 69 73 75 73  const char misus
24629 65 42 65 20 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  eBe [] = {.    0
2462a 2c 20 27 6c 27 2c 20 30 2c 20 27 69 27 2c 20 30  , 'l', 0, 'i', 0
2462b 2c 20 27 62 27 2c 20 30 2c 20 27 72 27 2c 20 30  , 'b', 0, 'r', 0
2462c 2c 20 27 61 27 2c 20 30 2c 20 27 72 27 2c 20 30  , 'a', 0, 'r', 0
2462d 2c 20 27 79 27 2c 20 30 2c 20 27 20 27 2c 20 0a  , 'y', 0, ' ', .
2462e 20 20 20 20 30 2c 20 27 72 27 2c 20 30 2c 20 27      0, 'r', 0, '
2462f 6f 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27  o', 0, 'u', 0, '
24630 74 27 2c 20 30 2c 20 27 69 27 2c 20 30 2c 20 27  t', 0, 'i', 0, '
24631 6e 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 27  n', 0, 'e', 0, '
24632 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 63 27 2c   ', .    0, 'c',
24633 20 30 2c 20 27 61 27 2c 20 30 2c 20 27 6c 27 2c   0, 'a', 0, 'l',
24634 20 30 2c 20 27 6c 27 2c 20 30 2c 20 27 65 27 2c   0, 'l', 0, 'e',
24635 20 30 2c 20 27 64 27 2c 20 30 2c 20 27 20 27 2c   0, 'd', 0, ' ',
24636 20 0a 20 20 20 20 30 2c 20 27 6f 27 2c 20 30 2c   .    0, 'o', 0,
24637 20 27 75 27 2c 20 30 2c 20 27 74 27 2c 20 30 2c   'u', 0, 't', 0,
24638 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 6f   ' ', .    0, 'o
24639 27 2c 20 30 2c 20 27 66 27 2c 20 30 2c 20 27 20  ', 0, 'f', 0, ' 
2463a 27 2c 20 0a 20 20 20 20 30 2c 20 27 73 27 2c 20  ', .    0, 's', 
2463b 30 2c 20 27 65 27 2c 20 30 2c 20 27 71 27 2c 20  0, 'e', 0, 'q', 
2463c 30 2c 20 27 75 27 2c 20 30 2c 20 27 65 27 2c 20  0, 'u', 0, 'e', 
2463d 30 2c 20 27 6e 27 2c 20 30 2c 20 27 63 27 2c 20  0, 'n', 0, 'c', 
2463e 30 2c 20 27 65 27 2c 20 30 2c 20 30 2c 20 30 0a  0, 'e', 0, 0, 0.
2463f 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73 74 20 76 6f    };..  const vo
24640 69 64 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62  id *z;.  if( !db
24641 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
24642 76 6f 69 64 20 2a 29 28 26 6f 75 74 4f 66 4d 65  void *)(&outOfMe
24643 6d 42 65 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  mBe[SQLITE_UTF16
24644 4e 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55  NATIVE==SQLITE_U
24645 54 46 31 36 4c 45 3f 31 3a 30 5d 29 3b 0a 20 20  TF16LE?1:0]);.  
24646 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  }.  if( sqlite3S
24647 61 66 65 74 79 43 68 65 63 6b 28 64 62 29 20 7c  afetyCheck(db) |
24648 7c 20 64 62 2d 3e 65 72 72 43 6f 64 65 3d 3d 53  | db->errCode==S
24649 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 29 7b 0a  QLITE_MISUSE ){.
2464a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64      return (void
2464b 20 2a 29 28 26 6d 69 73 75 73 65 42 65 5b 53 51   *)(&misuseBe[SQ
2464c 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
2464d 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
2464e 3f 31 3a 30 5d 29 3b 0a 20 20 7d 0a 20 20 73 71  ?1:0]);.  }.  sq
2464f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
24650 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
24651 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c  assert( !db->mal
24652 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a  locFailed );.  z
24653 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
24654 5f 74 65 78 74 31 36 28 64 62 2d 3e 70 45 72 72  _text16(db->pErr
24655 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  );.  if( z==0 ){
24656 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
24657 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72  eSetStr(db->pErr
24658 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 45 72 72  , -1, sqlite3Err
24659 53 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29  Str(db->errCode)
2465a 2c 0a 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  ,.         SQLIT
2465b 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53  E_UTF8, SQLITE_S
2465c 54 41 54 49 43 29 3b 0a 20 20 20 20 7a 20 3d 20  TATIC);.    z = 
2465d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2465e 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a  xt16(db->pErr);.
2465f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 41 70 69    }.  sqlite3Api
24660 45 78 69 74 28 30 2c 20 30 29 3b 0a 20 20 73 71  Exit(0, 0);.  sq
24661 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
24662 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
24663 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e 64  return z;.}.#end
24664 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
24665 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a  T_UTF16 */../*.*
24666 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 6f 73  * Return the mos
24667 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63  t recent error c
24668 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79  ode generated by
24669 20 61 6e 20 53 51 4c 69 74 65 20 72 6f 75 74 69   an SQLite routi
2466a 6e 65 2e 20 49 66 20 4e 55 4c 4c 20 69 73 0a 2a  ne. If NULL is.*
2466b 2a 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  * passed to this
2466c 20 66 75 6e 63 74 69 6f 6e 2c 20 77 65 20 61 73   function, we as
2466d 73 75 6d 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20  sume a malloc() 
2466e 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20 73 71  failed during sq
2466f 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2f  lite3_open()..*/
24670 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
24671 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
24672 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
24673 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d  if( !db || db->m
24674 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
24675 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
24676 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66  _NOMEM;.  }.  if
24677 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  ( sqlite3SafetyC
24678 68 65 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  heck(db) ){.    
24679 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
2467a 53 55 53 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75  SUSE;.  }.  retu
2467b 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 26  rn db->errCode &
2467c 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a   db->errMask;.}.
2467d 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
2467e 6e 65 77 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75  new collating fu
2467f 6e 63 74 69 6f 6e 20 66 6f 72 20 64 61 74 61 62  nction for datab
24680 61 73 65 20 22 64 62 22 2e 20 20 54 68 65 20 6e  ase "db".  The n
24681 61 6d 65 20 69 73 20 7a 4e 61 6d 65 0a 2a 2a 20  ame is zName.** 
24682 61 6e 64 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  and the encoding
24683 20 69 73 20 65 6e 63 2e 0a 2a 2f 0a 73 74 61 74   is enc..*/.stat
24684 69 63 20 69 6e 74 20 63 72 65 61 74 65 43 6f 6c  ic int createCol
24685 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  lation(.  sqlite
24686 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20  3* db, .  const 
24687 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
24688 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64  int enc, .  void
24689 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78  * pCtx,.  int(*x
2468a 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69  Compare)(void*,i
2468b 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
2468c 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c  nt,const void*),
2468d 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29 28 76  .  void(*xDel)(v
2468e 6f 69 64 2a 29 0a 29 7b 0a 20 20 43 6f 6c 6c 53  oid*).){.  CollS
2468f 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74  eq *pColl;.  int
24690 20 65 6e 63 32 3b 0a 20 20 0a 20 20 69 66 28 20   enc2;.  .  if( 
24691 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
24692 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65  ck(db) ){.    re
24693 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
24694 53 45 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  SE;.  }.  assert
24695 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
24696 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
24697 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49  );..  /* If SQLI
24698 54 45 5f 55 54 46 31 36 20 69 73 20 73 70 65 63  TE_UTF16 is spec
24699 69 66 69 65 64 20 61 73 20 74 68 65 20 65 6e 63  ified as the enc
2469a 6f 64 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e  oding type, tran
2469b 73 66 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20  sform this.  ** 
2469c 74 6f 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45  to one of SQLITE
2469d 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49  _UTF16LE or SQLI
2469e 54 45 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67  TE_UTF16BE using
2469f 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45   the.  ** SQLITE
246a0 5f 55 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63  _UTF16NATIVE mac
246a1 72 6f 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ro. SQLITE_UTF16
246a2 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74   is not used int
246a3 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20  ernally..  */.  
246a4 65 6e 63 32 20 3d 20 65 6e 63 20 26 20 7e 53 51  enc2 = enc & ~SQ
246a5 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
246a6 45 44 3b 0a 20 20 69 66 28 20 65 6e 63 32 3d 3d  ED;.  if( enc2==
246a7 53 51 4c 49 54 45 5f 55 54 46 31 36 20 29 7b 0a  SQLITE_UTF16 ){.
246a8 20 20 20 20 65 6e 63 32 20 3d 20 53 51 4c 49 54      enc2 = SQLIT
246a9 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20  E_UTF16NATIVE;. 
246aa 20 7d 0a 0a 20 20 69 66 28 20 28 65 6e 63 32 26   }..  if( (enc2&
246ab 7e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ~3)!=0 ){.    sq
246ac 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
246ad 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e  QLITE_ERROR, "un
246ae 6b 6e 6f 77 6e 20 65 6e 63 6f 64 69 6e 67 22 29  known encoding")
246af 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
246b0 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
246b1 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68    /* Check if th
246b2 69 73 20 63 61 6c 6c 20 69 73 20 72 65 6d 6f 76  is call is remov
246b3 69 6e 67 20 6f 72 20 72 65 70 6c 61 63 69 6e 67  ing or replacing
246b4 20 61 6e 20 65 78 69 73 74 69 6e 67 20 63 6f 6c   an existing col
246b5 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a 20 73 65 71  lation .  ** seq
246b6 75 65 6e 63 65 2e 20 49 66 20 73 6f 2c 20 61 6e  uence. If so, an
246b7 64 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69  d there are acti
246b8 76 65 20 56 4d 73 2c 20 72 65 74 75 72 6e 20 62  ve VMs, return b
246b9 75 73 79 2e 20 49 66 20 74 68 65 72 65 0a 20 20  usy. If there.  
246ba 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  ** are no active
246bb 20 56 4d 73 2c 20 69 6e 76 61 6c 69 64 61 74 65   VMs, invalidate
246bc 20 61 6e 79 20 70 72 65 2d 63 6f 6d 70 69 6c 65   any pre-compile
246bd 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20  d statements..  
246be 2a 2f 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  */.  pColl = sql
246bf 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
246c0 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e  db, (u8)enc2, zN
246c1 61 6d 65 2c 20 73 74 72 6c 65 6e 28 7a 4e 61 6d  ame, strlen(zNam
246c2 65 29 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43  e), 0);.  if( pC
246c3 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43  oll && pColl->xC
246c4 6d 70 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  mp ){.    if( db
246c5 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20  ->activeVdbeCnt 
246c6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
246c7 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
246c8 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20  _BUSY, .        
246c9 22 55 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74  "Unable to delet
246ca 65 2f 6d 6f 64 69 66 79 20 63 6f 6c 6c 61 74 69  e/modify collati
246cb 6f 6e 20 73 65 71 75 65 6e 63 65 20 64 75 65 20  on sequence due 
246cc 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  to active statem
246cd 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 72 65  ents");.      re
246ce 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
246cf 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
246d0 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
246d1 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
246d2 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 6f 6c 6c  ..    /* If coll
246d3 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 70  ation sequence p
246d4 43 6f 6c 6c 20 77 61 73 20 63 72 65 61 74 65 64  Coll was created
246d5 20 64 69 72 65 63 74 6c 79 20 62 79 20 61 20 63   directly by a c
246d6 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71  all to.    ** sq
246d7 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
246d8 6c 61 74 69 6f 6e 2c 20 61 6e 64 20 6e 6f 74 20  lation, and not 
246d9 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 79 6e  generated by syn
246da 74 68 43 6f 6c 6c 53 65 71 28 29 2c 0a 20 20 20  thCollSeq(),.   
246db 20 2a 2a 20 74 68 65 6e 20 61 6e 79 20 63 6f 70   ** then any cop
246dc 69 65 73 20 6d 61 64 65 20 62 79 20 73 79 6e 74  ies made by synt
246dd 68 43 6f 6c 6c 53 65 71 28 29 20 6e 65 65 64 20  hCollSeq() need 
246de 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  to be invalidate
246df 64 2e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20  d..    ** Also, 
246e0 63 6f 6c 6c 61 74 69 6f 6e 20 64 65 73 74 72 75  collation destru
246e1 63 74 6f 72 20 2d 20 43 6f 6c 6c 53 65 71 2e 78  ctor - CollSeq.x
246e2 44 65 6c 28 29 20 2d 20 66 75 6e 63 74 69 6f 6e  Del() - function
246e3 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a   may need.    **
246e4 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0a 20   to be called.. 
246e5 20 20 20 2a 2f 20 0a 20 20 20 20 69 66 28 20 28     */ .    if( (
246e6 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53 51  pColl->enc & ~SQ
246e7 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
246e8 45 44 29 3d 3d 65 6e 63 32 20 29 7b 0a 20 20 20  ED)==enc2 ){.   
246e9 20 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c     CollSeq *aCol
246ea 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  l = sqlite3HashF
246eb 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ind(&db->aCollSe
246ec 71 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e  q, zName, strlen
246ed 28 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20  (zName));.      
246ee 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72  int j;.      for
246ef 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b  (j=0; j<3; j++){
246f0 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
246f1 20 2a 70 20 3d 20 26 61 43 6f 6c 6c 5b 6a 5d 3b   *p = &aColl[j];
246f2 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e  .        if( p->
246f3 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20  enc==pColl->enc 
246f4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
246f5 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20   p->xDel ){.    
246f6 20 20 20 20 20 20 20 20 70 2d 3e 78 44 65 6c 28          p->xDel(
246f7 70 2d 3e 70 55 73 65 72 29 3b 0a 20 20 20 20 20  p->pUser);.     
246f8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
246f9 20 70 2d 3e 78 43 6d 70 20 3d 20 30 3b 0a 20 20   p->xCmp = 0;.  
246fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
246fb 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 43 6f      }.  }..  pCo
246fc 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ll = sqlite3Find
246fd 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75 38 29  CollSeq(db, (u8)
246fe 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 73 74 72  enc2, zName, str
246ff 6c 65 6e 28 7a 4e 61 6d 65 29 2c 20 31 29 3b 0a  len(zName), 1);.
24700 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
24701 20 20 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d     pColl->xCmp =
24702 20 78 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 70   xCompare;.    p
24703 43 6f 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43  Coll->pUser = pC
24704 74 78 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78  tx;.    pColl->x
24705 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20  Del = xDel;.    
24706 70 43 6f 6c 6c 2d 3e 65 6e 63 20 3d 20 65 6e 63  pColl->enc = enc
24707 32 20 7c 20 28 65 6e 63 20 26 20 53 51 4c 49 54  2 | (enc & SQLIT
24708 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
24709 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
2470a 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
2470b 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  OK, 0);.  return
2470c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
2470d 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2470e 6e 65 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b  ne does the work
2470f 20 6f 66 20 6f 70 65 6e 69 6e 67 20 61 20 64 61   of opening a da
24710 74 61 62 61 73 65 20 6f 6e 20 62 65 68 61 6c 66  tabase on behalf
24711 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f   of.** sqlite3_o
24712 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65  pen() and sqlite
24713 33 5f 6f 70 65 6e 31 36 28 29 2e 20 54 68 65 20  3_open16(). The 
24714 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  database filenam
24715 65 20 22 7a 46 69 6c 65 6e 61 6d 65 22 20 20 0a  e "zFilename"  .
24716 2a 2a 20 69 73 20 55 54 46 2d 38 20 65 6e 63 6f  ** is UTF-8 enco
24717 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ded..*/.static i
24718 6e 74 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28  nt openDatabase(
24719 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2471a 46 69 6c 65 6e 61 6d 65 2c 20 2f 2a 20 44 61 74  Filename, /* Dat
2471b 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 55  abase filename U
2471c 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  TF-8 encoded */.
2471d 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62    sqlite3 **ppDb
2471e 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  ,        /* OUT:
2471f 20 52 65 74 75 72 6e 65 64 20 64 61 74 61 62 61   Returned databa
24720 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 75  se handle */.  u
24721 6e 73 69 67 6e 65 64 20 66 6c 61 67 73 2c 20 20  nsigned flags,  
24722 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69        /* Operati
24723 6f 6e 61 6c 20 66 6c 61 67 73 20 2a 2f 0a 20 20  onal flags */.  
24724 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73  const char *zVfs
24725 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
24726 66 20 74 68 65 20 56 46 53 20 74 6f 20 75 73 65  f the VFS to use
24727 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
24728 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   *db;.  int rc;.
24729 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2472a 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
2472b 20 74 68 65 20 73 71 6c 69 74 65 20 64 61 74 61   the sqlite data
2472c 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
2472d 64 62 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  db = sqlite3Mall
2472e 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 73  ocZero( sizeof(s
2472f 71 6c 69 74 65 33 29 20 29 3b 0a 20 20 69 66 28  qlite3) );.  if(
24730 20 64 62 3d 3d 30 20 29 20 67 6f 74 6f 20 6f 70   db==0 ) goto op
24731 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 64 62 2d 3e  endb_out;.  db->
24732 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f  mutex = sqlite3_
24733 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49  mutex_alloc(SQLI
24734 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
24735 56 45 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  VE);.  if( db->m
24736 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 73  utex==0 ){.    s
24737 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62 29 3b  qlite3_free(db);
24738 0a 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 20  .    db = 0;.   
24739 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74   goto opendb_out
2473a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
2473b 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2473c 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72  mutex);.  db->er
2473d 72 4d 61 73 6b 20 3d 20 30 78 66 66 3b 0a 20 20  rMask = 0xff;.  
2473e 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69  db->priorNewRowi
2473f 64 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 6e 44 62  d = 0;.  db->nDb
24740 20 3d 20 32 3b 0a 20 20 64 62 2d 3e 6d 61 67 69   = 2;.  db->magi
24741 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
24742 5f 42 55 53 59 3b 0a 20 20 64 62 2d 3e 61 44 62  _BUSY;.  db->aDb
24743 20 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63   = db->aDbStatic
24744 3b 0a 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  ;.  db->autoComm
24745 69 74 20 3d 20 31 3b 0a 20 20 64 62 2d 3e 66 6c  it = 1;.  db->fl
24746 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68  ags |= SQLITE_Sh
24747 6f 72 74 43 6f 6c 4e 61 6d 65 73 0a 23 69 66 20  ortColNames.#if 
24748 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
24749 49 4c 45 5f 46 4f 52 4d 41 54 3c 34 0a 20 20 20  ILE_FORMAT<4.   
2474a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
2474b 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
2474c 65 46 6d 74 0a 23 65 6e 64 69 66 0a 23 69 66 64  eFmt.#endif.#ifd
2474d 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2474e 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
2474f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24750 20 7c 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78   | SQLITE_LoadEx
24751 74 65 6e 73 69 6f 6e 0a 23 65 6e 64 69 66 0a 20  tension.#endif. 
24752 20 20 20 20 20 3b 0a 20 20 73 71 6c 69 74 65 33       ;.  sqlite3
24753 48 61 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 46  HashInit(&db->aF
24754 75 6e 63 2c 20 53 51 4c 49 54 45 5f 48 41 53 48  unc, SQLITE_HASH
24755 5f 53 54 52 49 4e 47 2c 20 30 29 3b 0a 20 20 73  _STRING, 0);.  s
24756 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
24757 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 53 51  db->aCollSeq, SQ
24758 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e 47  LITE_HASH_STRING
24759 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  , 0);.#ifndef SQ
2475a 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2475b 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33  LTABLE.  sqlite3
2475c 48 61 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 4d  HashInit(&db->aM
2475d 6f 64 75 6c 65 2c 20 53 51 4c 49 54 45 5f 48 41  odule, SQLITE_HA
2475e 53 48 5f 53 54 52 49 4e 47 2c 20 30 29 3b 0a 23  SH_STRING, 0);.#
2475f 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 70 56 66  endif..  db->pVf
24760 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  s = sqlite3_vfs_
24761 66 69 6e 64 28 7a 56 66 73 29 3b 0a 20 20 69 66  find(zVfs);.  if
24762 28 20 21 64 62 2d 3e 70 56 66 73 20 29 7b 0a 20  ( !db->pVfs ){. 
24763 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
24764 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 6d 61  RROR;.    db->ma
24765 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
24766 49 43 5f 43 4c 4f 53 45 44 3b 0a 20 20 20 20 73  IC_CLOSED;.    s
24767 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
24768 72 63 2c 20 22 6e 6f 20 73 75 63 68 20 76 66 73  rc, "no such vfs
24769 3a 20 25 73 22 2c 20 28 7a 56 66 73 3f 7a 56 66  : %s", (zVfs?zVf
2476a 73 3a 22 28 6e 75 6c 6c 29 22 29 29 3b 0a 20 20  s:"(null)"));.  
2476b 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
2476c 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64  t;.  }..  /* Add
2476d 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c   the default col
2476e 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2476f 42 49 4e 41 52 59 2e 20 42 49 4e 41 52 59 20 77  BINARY. BINARY w
24770 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20 55 54  orks for both UT
24771 46 2d 38 0a 20 20 2a 2a 20 61 6e 64 20 55 54 46  F-8.  ** and UTF
24772 2d 31 36 2c 20 73 6f 20 61 64 64 20 61 20 76 65  -16, so add a ve
24773 72 73 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74  rsion for each t
24774 6f 20 61 76 6f 69 64 20 61 6e 79 20 75 6e 6e 65  o avoid any unne
24775 63 65 73 73 61 72 79 0a 20 20 2a 2a 20 63 6f 6e  cessary.  ** con
24776 76 65 72 73 69 6f 6e 73 2e 20 54 68 65 20 6f 6e  versions. The on
24777 6c 79 20 65 72 72 6f 72 20 74 68 61 74 20 63 61  ly error that ca
24778 6e 20 6f 63 63 75 72 20 68 65 72 65 20 69 73 20  n occur here is 
24779 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  a malloc() failu
2477a 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  re..  */.  if( c
2477b 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
2477c 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c  b, "BINARY", SQL
2477d 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 62 69 6e  ITE_UTF8, 0, bin
2477e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 20 7c 7c 0a  CollFunc, 0) ||.
2477f 20 20 20 20 20 20 63 72 65 61 74 65 43 6f 6c 6c        createColl
24780 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52  ation(db, "BINAR
24781 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  Y", SQLITE_UTF16
24782 42 45 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75  BE, 0, binCollFu
24783 6e 63 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20  nc, 0) ||.      
24784 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
24785 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51  db, "BINARY", SQ
24786 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 30 2c  LITE_UTF16LE, 0,
24787 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29   binCollFunc, 0)
24788 20 7c 7c 0a 20 20 20 20 20 20 28 64 62 2d 3e 70   ||.      (db->p
24789 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  DfltColl = sqlit
2478a 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
2478b 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22  , SQLITE_UTF8, "
2478c 42 49 4e 41 52 59 22 2c 20 36 2c 20 30 29 29 3d  BINARY", 6, 0))=
2478d 3d 30 20 0a 20 20 29 7b 0a 20 20 20 20 61 73 73  =0 .  ){.    ass
2478e 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
2478f 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 64 62 2d  ailed );.    db-
24790 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
24791 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b 0a 20 20  MAGIC_CLOSED;.  
24792 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
24793 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 73  t;.  }..  /* Als
24794 6f 20 61 64 64 20 61 20 55 54 46 2d 38 20 63 61  o add a UTF-8 ca
24795 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 65 20 63  se-insensitive c
24796 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
24797 65 2e 20 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f  e. */.  createCo
24798 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 4e 4f 43  llation(db, "NOC
24799 41 53 45 22 2c 20 53 51 4c 49 54 45 5f 55 54 46  ASE", SQLITE_UTF
2479a 38 2c 20 30 2c 20 6e 6f 63 61 73 65 43 6f 6c 6c  8, 0, nocaseColl
2479b 61 74 69 6e 67 46 75 6e 63 2c 20 30 29 3b 0a 0a  atingFunc, 0);..
2479c 20 20 2f 2a 20 53 65 74 20 66 6c 61 67 73 20 6f    /* Set flags o
2479d 6e 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 63  n the built-in c
2479e 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2479f 65 73 20 2a 2f 0a 20 20 64 62 2d 3e 70 44 66 6c  es */.  db->pDfl
247a0 74 43 6f 6c 6c 2d 3e 74 79 70 65 20 3d 20 53 51  tColl->type = SQ
247a1 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59  LITE_COLL_BINARY
247a2 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ;.  pColl = sqli
247a3 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
247a4 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  b, SQLITE_UTF8, 
247a5 22 4e 4f 43 41 53 45 22 2c 20 36 2c 20 30 29 3b  "NOCASE", 6, 0);
247a6 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a  .  if( pColl ){.
247a7 20 20 20 20 70 43 6f 6c 6c 2d 3e 74 79 70 65 20      pColl->type 
247a8 3d 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f  = SQLITE_COLL_NO
247a9 43 41 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  CASE;.  }..  /* 
247aa 4f 70 65 6e 20 74 68 65 20 62 61 63 6b 65 6e 64  Open the backend
247ab 20 64 61 74 61 62 61 73 65 20 64 72 69 76 65 72   database driver
247ac 20 2a 2f 0a 20 20 64 62 2d 3e 6f 70 65 6e 46 6c   */.  db->openFl
247ad 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 72  ags = flags;.  r
247ae 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
247af 46 61 63 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c  Factory(db, zFil
247b0 65 6e 61 6d 65 2c 20 30 2c 20 53 51 4c 49 54 45  ename, 0, SQLITE
247b1 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
247b2 49 5a 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20  IZE, .          
247b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
247b4 20 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f   flags | SQLITE_
247b5 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20  OPEN_MAIN_DB,.  
247b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
247b7 20 20 20 20 20 20 20 20 20 26 64 62 2d 3e 61 44           &db->aD
247b8 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 69 66 28  b[0].pBt);.  if(
247b9 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
247ba 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
247bb 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20  or(db, rc, 0);. 
247bc 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53     db->magic = S
247bd 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53  QLITE_MAGIC_CLOS
247be 45 44 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65  ED;.    goto ope
247bf 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64  ndb_out;.  }.  d
247c0 62 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d  b->aDb[0].pSchem
247c1 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  a = sqlite3Schem
247c2 61 47 65 74 28 64 62 2c 20 64 62 2d 3e 61 44 62  aGet(db, db->aDb
247c3 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 64 62 2d 3e  [0].pBt);.  db->
247c4 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 3d  aDb[1].pSchema =
247c5 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65   sqlite3SchemaGe
247c6 74 28 64 62 2c 20 30 29 3b 0a 0a 0a 20 20 2f 2a  t(db, 0);...  /*
247c7 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 61 66   The default saf
247c8 65 74 79 5f 6c 65 76 65 6c 20 66 6f 72 20 74 68  ety_level for th
247c9 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
247ca 69 73 20 27 66 75 6c 6c 27 3b 20 66 6f 72 20 74  is 'full'; for t
247cb 68 65 20 74 65 6d 70 0a 20 20 2a 2a 20 64 61 74  he temp.  ** dat
247cc 61 62 61 73 65 20 69 74 20 69 73 20 27 4e 4f 4e  abase it is 'NON
247cd 45 27 2e 20 54 68 69 73 20 6d 61 74 63 68 65 73  E'. This matches
247ce 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72   the pager layer
247cf 20 64 65 66 61 75 6c 74 73 2e 20 20 0a 20 20 2a   defaults.  .  *
247d0 2f 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 7a  /.  db->aDb[0].z
247d1 4e 61 6d 65 20 3d 20 22 6d 61 69 6e 22 3b 0a 20  Name = "main";. 
247d2 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 73 61 66 65   db->aDb[0].safe
247d3 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a 23 69  ty_level = 3;.#i
247d4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
247d5 54 5f 54 45 4d 50 44 42 0a 20 20 64 62 2d 3e 61  T_TEMPDB.  db->a
247d6 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 22 74  Db[1].zName = "t
247d7 65 6d 70 22 3b 0a 20 20 64 62 2d 3e 61 44 62 5b  emp";.  db->aDb[
247d8 31 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20  1].safety_level 
247d9 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 64  = 1;.#endif..  d
247da 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
247db 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20  E_MAGIC_OPEN;.  
247dc 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
247dd 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
247de 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d   opendb_out;.  }
247df 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
247e0 61 6c 6c 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e  all built-in fun
247e1 63 74 69 6f 6e 73 2c 20 62 75 74 20 64 6f 20 6e  ctions, but do n
247e2 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  ot attempt to re
247e3 61 64 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  ad the.  ** data
247e4 62 61 73 65 20 73 63 68 65 6d 61 20 79 65 74 2e  base schema yet.
247e5 20 54 68 69 73 20 69 73 20 64 65 6c 61 79 65 64   This is delayed
247e6 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74   until the first
247e7 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62 61   time the databa
247e8 73 65 0a 20 20 2a 2a 20 69 73 20 61 63 63 65 73  se.  ** is acces
247e9 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  sed..  */.  sqli
247ea 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
247eb 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 73 71  ITE_OK, 0);.  sq
247ec 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69  lite3RegisterBui
247ed 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 64 62  ltinFunctions(db
247ee 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 61 75  );..  /* Load au
247ef 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
247f0 6e 73 20 2d 20 65 78 74 65 6e 73 69 6f 6e 73 20  ns - extensions 
247f1 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 20 72  that have been r
247f2 65 67 69 73 74 65 72 65 64 0a 20 20 2a 2a 20 75  egistered.  ** u
247f3 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  sing the sqlite3
247f4 5f 61 75 74 6f 6d 61 74 69 63 5f 65 78 74 65 6e  _automatic_exten
247f5 73 69 6f 6e 28 29 20 41 50 49 2e 0a 20 20 2a 2f  sion() API..  */
247f6 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
247f7 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  AutoLoadExtensio
247f8 6e 73 28 64 62 29 3b 0a 20 20 69 66 28 20 73 71  ns(db);.  if( sq
247f9 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62  lite3_errcode(db
247fa 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  )!=SQLITE_OK ){.
247fb 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
247fc 6f 75 74 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66  out;.  }..#ifdef
247fd 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
247fe 54 53 31 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d  TS1.  if( !db->m
247ff 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
24800 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71     extern int sq
24801 6c 69 74 65 33 46 74 73 31 49 6e 69 74 28 73 71  lite3Fts1Init(sq
24802 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72 63 20  lite3*);.    rc 
24803 3d 20 73 71 6c 69 74 65 33 46 74 73 31 49 6e 69  = sqlite3Fts1Ini
24804 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
24805 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
24806 5f 45 4e 41 42 4c 45 5f 46 54 53 32 0a 20 20 69  _ENABLE_FTS2.  i
24807 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
24808 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49  iled && rc==SQLI
24809 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65 78 74  TE_OK ){.    ext
2480a 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ern int sqlite3F
2480b 74 73 32 49 6e 69 74 28 73 71 6c 69 74 65 33 2a  ts2Init(sqlite3*
2480c 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
2480d 74 65 33 46 74 73 32 49 6e 69 74 28 64 62 29 3b  te3Fts2Init(db);
2480e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  .  }.#endif..#if
2480f 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
24810 45 5f 46 54 53 33 0a 20 20 69 66 28 20 21 64 62  E_FTS3.  if( !db
24811 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
24812 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
24813 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e  ){.    extern in
24814 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  t sqlite3Fts3Ini
24815 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20  t(sqlite3*);.   
24816 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
24817 33 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23  3Init(db);.  }.#
24818 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
24819 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a  LITE_ENABLE_ICU.
2481a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
2481b 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53  cFailed && rc==S
2481c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2481d 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
2481e 65 33 49 63 75 49 6e 69 74 28 73 71 6c 69 74 65  e3IcuInit(sqlite
2481f 33 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  3*);.    rc = sq
24820 6c 69 74 65 33 49 63 75 49 6e 69 74 28 64 62 29  lite3IcuInit(db)
24821 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73  ;.  }.#endif.  s
24822 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
24823 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 2d 44  rc, 0);..  /* -D
24824 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
24825 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 31 20 6d 61  OCKING_MODE=1 ma
24826 6b 65 73 20 45 58 43 4c 55 53 49 56 45 20 74 68  kes EXCLUSIVE th
24827 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e  e default lockin
24828 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 2d 44  g.  ** mode.  -D
24829 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
2482a 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 30 20 6d 61  OCKING_MODE=0 ma
2482b 6b 65 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65  ke NORMAL the de
2482c 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20  fault locking.  
2482d 2a 2a 20 6d 6f 64 65 2e 20 20 44 6f 69 6e 67 20  ** mode.  Doing 
2482e 6e 6f 74 68 69 6e 67 20 61 74 20 61 6c 6c 20 61  nothing at all a
2482f 6c 73 6f 20 6d 61 6b 65 73 20 4e 4f 52 4d 41 4c  lso makes NORMAL
24830 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20 20   the default..  
24831 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
24832 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47  _DEFAULT_LOCKING
24833 5f 4d 4f 44 45 0a 20 20 64 62 2d 3e 64 66 6c 74  _MODE.  db->dflt
24834 4c 6f 63 6b 4d 6f 64 65 20 3d 20 53 51 4c 49 54  LockMode = SQLIT
24835 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e  E_DEFAULT_LOCKIN
24836 47 5f 4d 4f 44 45 3b 0a 20 20 73 71 6c 69 74 65  G_MODE;.  sqlite
24837 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64  3PagerLockingMod
24838 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  e(sqlite3BtreePa
24839 67 65 72 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ger(db->aDb[0].p
2483a 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Bt),.           
2483b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
2483c 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f  QLITE_DEFAULT_LO
2483d 43 4b 49 4e 47 5f 4d 4f 44 45 29 3b 0a 23 65 6e  CKING_MODE);.#en
2483e 64 69 66 0a 0a 6f 70 65 6e 64 62 5f 6f 75 74 3a  dif..opendb_out:
2483f 0a 20 20 69 66 28 20 64 62 20 26 26 20 64 62 2d  .  if( db && db-
24840 3e 6d 75 74 65 78 20 29 7b 0a 20 20 20 20 73 71  >mutex ){.    sq
24841 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
24842 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
24843 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e  }.  if( SQLITE_N
24844 4f 4d 45 4d 3d 3d 28 72 63 20 3d 20 73 71 6c 69  OMEM==(rc = sqli
24845 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 29  te3_errcode(db))
24846 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
24847 63 6c 6f 73 65 28 64 62 29 3b 0a 20 20 20 20 64  close(db);.    d
24848 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 70  b = 0;.  }.  *pp
24849 44 62 20 3d 20 64 62 3b 0a 20 20 72 65 74 75 72  Db = db;.  retur
2484a 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  n sqlite3ApiExit
2484b 28 30 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (0, rc);.}../*.*
2484c 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61 74  * Open a new dat
2484d 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f  abase handle..*/
2484e 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2484f 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20  sqlite3_open(.  
24850 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
24851 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c 69 74 65  ename, .  sqlite
24852 33 20 2a 2a 70 70 44 62 20 0a 29 7b 0a 20 20 72  3 **ppDb .){.  r
24853 65 74 75 72 6e 20 6f 70 65 6e 44 61 74 61 62 61  eturn openDataba
24854 73 65 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 70  se(zFilename, pp
24855 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Db,.            
24856 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
24857 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
24858 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  | SQLITE_OPEN_CR
24859 45 41 54 45 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49  EATE, 0);.}.SQLI
2485a 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2485b 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f  e3_open_v2(.  co
2485c 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61  nst char *filena
2485d 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73  me,   /* Databas
2485e 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d  e filename (UTF-
2485f 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  8) */.  sqlite3 
24860 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20  **ppDb,         
24861 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64  /* OUT: SQLite d
24862 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e  b handle */.  in
24863 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
24864 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a        /* Flags *
24865 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
24866 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e  zVfs        /* N
24867 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c  ame of VFS modul
24868 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a 20  e to use */.){. 
24869 20 72 65 74 75 72 6e 20 6f 70 65 6e 44 61 74 61   return openData
2486a 62 61 73 65 28 66 69 6c 65 6e 61 6d 65 2c 20 70  base(filename, p
2486b 70 44 62 2c 20 66 6c 61 67 73 2c 20 7a 56 66 73  pDb, flags, zVfs
2486c 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  );.}..#ifndef SQ
2486d 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
2486e 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77  /*.** Open a new
2486f 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
24870 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
24871 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
24872 31 36 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  16(.  const void
24873 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20   *zFilename, .  
24874 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 0a 29  sqlite3 **ppDb.)
24875 7b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  {.  char const *
24876 7a 46 69 6c 65 6e 61 6d 65 38 3b 20 20 20 2f 2a  zFilename8;   /*
24877 20 7a 46 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64   zFilename encod
24878 65 64 20 69 6e 20 55 54 46 2d 38 20 69 6e 73 74  ed in UTF-8 inst
24879 65 61 64 20 6f 66 20 55 54 46 2d 31 36 20 2a 2f  ead of UTF-16 */
2487a 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2487b 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 72 63   *pVal;.  int rc
2487c 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
2487d 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c  ..  assert( zFil
2487e 65 6e 61 6d 65 20 29 3b 0a 20 20 61 73 73 65 72  ename );.  asser
2487f 74 28 20 70 70 44 62 20 29 3b 0a 20 20 2a 70 70  t( ppDb );.  *pp
24880 44 62 20 3d 20 30 3b 0a 20 20 70 56 61 6c 20 3d  Db = 0;.  pVal =
24881 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
24882 28 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61  (0);.  sqlite3Va
24883 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20  lueSetStr(pVal, 
24884 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 53  -1, zFilename, S
24885 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
24886 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  E, SQLITE_STATIC
24887 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 38 20  );.  zFilename8 
24888 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  = sqlite3ValueTe
24889 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  xt(pVal, SQLITE_
2488a 55 54 46 38 29 3b 0a 20 20 69 66 28 20 7a 46 69  UTF8);.  if( zFi
2488b 6c 65 6e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72  lename8 ){.    r
2488c 63 20 3d 20 6f 70 65 6e 44 61 74 61 62 61 73 65  c = openDatabase
2488d 28 7a 46 69 6c 65 6e 61 6d 65 38 2c 20 70 70 44  (zFilename8, ppD
2488e 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
2488f 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
24890 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c  OPEN_READWRITE |
24891 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
24892 41 54 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  ATE, 0);.    if(
24893 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
24894 26 20 2a 70 70 44 62 20 29 7b 0a 20 20 20 20 20  & *ppDb ){.     
24895 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78   rc = sqlite3_ex
24896 65 63 28 2a 70 70 44 62 2c 20 22 50 52 41 47 4d  ec(*ppDb, "PRAGM
24897 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20 27 55 54  A encoding = 'UT
24898 46 2d 31 36 27 22 2c 20 30 2c 20 30 2c 20 30 29  F-16'", 0, 0, 0)
24899 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
2489a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2489b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f       sqlite3_clo
2489c 73 65 28 2a 70 70 44 62 29 3b 0a 20 20 20 20 20  se(*ppDb);.     
2489d 20 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a 20 20     *ppDb = 0;.  
2489e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2489f 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
248a0 65 65 28 70 56 61 6c 29 3b 0a 0a 20 20 72 65 74  ee(pVal);..  ret
248a1 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78  urn sqlite3ApiEx
248a2 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e  it(0, rc);.}.#en
248a3 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
248a4 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a  IT_UTF16 */../*.
248a5 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65  ** Register a ne
248a6 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  w collation sequ
248a7 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61  ence with the da
248a8 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
248a9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
248aa 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
248ab 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20  te_collation(.  
248ac 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20  sqlite3* db, .  
248ad 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
248ae 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a  e, .  int enc, .
248af 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20    void* pCtx,.  
248b0 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76  int(*xCompare)(v
248b1 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
248b2 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
248b3 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  oid*).){.  int r
248b4 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
248b5 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
248b6 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ex);.  assert( !
248b7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
248b8 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74   );.  rc = creat
248b9 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a  eCollation(db, z
248ba 4e 61 6d 65 2c 20 65 6e 63 2c 20 70 43 74 78 2c  Name, enc, pCtx,
248bb 20 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20   xCompare, 0);. 
248bc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
248bd 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
248be 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
248bf 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
248c0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
248c1 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
248c2 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   new collation s
248c3 65 71 75 65 6e 63 65 20 77 69 74 68 20 74 68 65  equence with the
248c4 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
248c5 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41   db..*/.SQLITE_A
248c6 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
248c7 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
248c8 76 32 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64  v2(.  sqlite3* d
248c9 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b, .  const char
248ca 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20   *zName, .  int 
248cb 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43  enc, .  void* pC
248cc 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70  tx,.  int(*xComp
248cd 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  are)(void*,int,c
248ce 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
248cf 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76  onst void*),.  v
248d0 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  oid(*xDel)(void*
248d1 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ).){.  int rc;. 
248d2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
248d3 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
248d4 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e  .  assert( !db->
248d5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
248d6 20 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c    rc = createCol
248d7 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65  lation(db, zName
248d8 2c 20 65 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f  , enc, pCtx, xCo
248d9 6d 70 61 72 65 2c 20 78 44 65 6c 29 3b 0a 20 20  mpare, xDel);.  
248da 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
248db 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
248dc 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
248dd 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
248de 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
248df 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
248e0 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52  IT_UTF16./*.** R
248e1 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f  egister a new co
248e2 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
248e3 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
248e4 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f  se handle db..*/
248e5 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
248e6 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
248e7 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 71  ollation16(.  sq
248e8 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f  lite3* db, .  co
248e9 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
248ea 20 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20   .  int enc, .  
248eb 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e  void* pCtx,.  in
248ec 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69  t(*xCompare)(voi
248ed 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
248ee 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
248ef 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20  d*).){.  int rc 
248f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63  = SQLITE_OK;.  c
248f1 68 61 72 20 2a 7a 4e 61 6d 65 38 3b 20 0a 20 20  har *zName8; .  
248f2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
248f3 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
248f4 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
248f5 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
248f6 20 7a 4e 61 6d 65 38 20 3d 20 73 71 6c 69 74 65   zName8 = sqlite
248f7 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 4e  3Utf16to8(db, zN
248f8 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69 66 28 20  ame, -1);.  if( 
248f9 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72 63  zName8 ){.    rc
248fa 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69   = createCollati
248fb 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38 2c 20 65  on(db, zName8, e
248fc 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70 61  nc, pCtx, xCompa
248fd 72 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  re, 0);.    sqli
248fe 74 65 33 5f 66 72 65 65 28 7a 4e 61 6d 65 38 29  te3_free(zName8)
248ff 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
24900 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
24901 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
24902 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
24903 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
24904 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  rc;.}.#endif /* 
24905 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
24906 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  6 */../*.** Regi
24907 73 74 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  ster a collation
24908 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72   sequence factor
24909 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20  y callback with 
2490a 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
2490b 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c 61  dle.** db. Repla
2490c 63 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 6c  ce any previousl
2490d 79 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c 6c  y installed coll
2490e 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
2490f 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  actory..*/.SQLIT
24910 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
24911 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
24912 65 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ed(.  sqlite3 *d
24913 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c  b, .  void *pCol
24914 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20 20 76  lNeededArg, .  v
24915 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64  oid(*xCollNeeded
24916 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
24917 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
24918 6e 73 74 20 63 68 61 72 2a 29 0a 29 7b 0a 20 20  nst char*).){.  
24919 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
2491a 79 43 68 65 63 6b 28 64 62 29 20 29 7b 0a 20 20  yCheck(db) ){.  
2491b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2491c 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71  MISUSE;.  }.  sq
2491d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
2491e 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
2491f 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20  db->xCollNeeded 
24920 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64 3b 0a 20  = xCollNeeded;. 
24921 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
24922 31 36 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 43  16 = 0;.  db->pC
24923 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d 20 70  ollNeededArg = p
24924 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20  CollNeededArg;. 
24925 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
24926 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
24927 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
24928 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  _OK;.}..#ifndef 
24929 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
2492a 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  6./*.** Register
2492b 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71   a collation seq
2492c 75 65 6e 63 65 20 66 61 63 74 6f 72 79 20 63 61  uence factory ca
2492d 6c 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20  llback with the 
2492e 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a  database handle.
2492f 2a 2a 20 64 62 2e 20 52 65 70 6c 61 63 65 20 61  ** db. Replace a
24930 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e  ny previously in
24931 73 74 61 6c 6c 65 64 20 63 6f 6c 6c 61 74 69 6f  stalled collatio
24932 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f  n sequence facto
24933 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ry..*/.SQLITE_AP
24934 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
24935 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
24936 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
24937 20 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e   .  void *pCollN
24938 65 65 64 65 64 41 72 67 2c 20 0a 20 20 76 6f 69  eededArg, .  voi
24939 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36  d(*xCollNeeded16
2493a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
2493b 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
2493c 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20  nst void*).){.  
2493d 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
2493e 79 43 68 65 63 6b 28 64 62 29 20 29 7b 0a 20 20  yCheck(db) ){.  
2493f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
24940 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71  MISUSE;.  }.  sq
24941 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
24942 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
24943 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20  db->xCollNeeded 
24944 3d 20 30 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c  = 0;.  db->xColl
24945 4e 65 65 64 65 64 31 36 20 3d 20 78 43 6f 6c 6c  Needed16 = xColl
24946 4e 65 65 64 65 64 31 36 3b 0a 20 20 64 62 2d 3e  Needed16;.  db->
24947 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d  pCollNeededArg =
24948 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b   pCollNeededArg;
24949 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2494a 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2494b 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
2494c 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20  TE_OK;.}.#endif 
2494d 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
2494e 54 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  TF16 */..#ifndef
2494f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 4c 4f   SQLITE_OMIT_GLO
24950 42 41 4c 52 45 43 4f 56 45 52 0a 2f 2a 0a 2a 2a  BALRECOVER./*.**
24951 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
24952 73 20 6e 6f 77 20 61 6e 20 61 6e 61 63 68 72 6f  s now an anachro
24953 6e 69 73 6d 2e 20 49 74 20 75 73 65 64 20 74 6f  nism. It used to
24954 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f   be used to reco
24955 76 65 72 20 66 72 6f 6d 20 61 0a 2a 2a 20 6d 61  ver from a.** ma
24956 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 2c 20  lloc() failure, 
24957 62 75 74 20 53 51 4c 69 74 65 20 6e 6f 77 20 64  but SQLite now d
24958 6f 65 73 20 74 68 69 73 20 61 75 74 6f 6d 61 74  oes this automat
24959 69 63 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  ically..*/.SQLIT
2495a 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2495b 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  3_global_recover
2495c 28 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  (){.  return SQL
2495d 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
2495e 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20  ../*.** Test to 
2495f 73 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  see whether or n
24960 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ot the database 
24961 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e  connection is in
24962 20 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 6d   autocommit.** m
24963 6f 64 65 2e 20 20 52 65 74 75 72 6e 20 54 52 55  ode.  Return TRU
24964 45 20 69 66 20 69 74 20 69 73 20 61 6e 64 20 46  E if it is and F
24965 41 4c 53 45 20 69 66 20 6e 6f 74 2e 20 20 41 75  ALSE if not.  Au
24966 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73  tocommit mode is
24967 20 6f 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75 6c   on.** by defaul
24968 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 69  t.  Autocommit i
24969 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20  s disabled by a 
2496a 42 45 47 49 4e 20 73 74 61 74 65 6d 65 6e 74 20  BEGIN statement 
2496b 61 6e 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a 2a  and reenabled.**
2496c 20 62 79 20 74 68 65 20 6e 65 78 74 20 43 4f 4d   by the next COM
2496d 4d 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e  MIT or ROLLBACK.
2496e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49 53  .**.******* THIS
2496f 20 49 53 20 41 4e 20 45 58 50 45 52 49 4d 45 4e   IS AN EXPERIMEN
24970 54 41 4c 20 41 50 49 20 41 4e 44 20 49 53 20 53  TAL API AND IS S
24971 55 42 4a 45 43 54 20 54 4f 20 43 48 41 4e 47 45  UBJECT TO CHANGE
24972 20 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54   ******.*/.SQLIT
24973 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
24974 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
24975 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
24976 20 72 65 74 75 72 6e 20 64 62 2d 3e 61 75 74 6f   return db->auto
24977 43 6f 6d 6d 69 74 3b 0a 7d 0a 0a 23 69 66 64 65  Commit;.}..#ifde
24978 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
24979 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
2497a 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 73 75  ng routine is su
2497b 62 74 69 74 75 74 65 64 20 66 6f 72 20 63 6f 6e  btituted for con
2497c 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 43 4f 52  stant SQLITE_COR
2497d 52 55 50 54 20 69 6e 0a 2a 2a 20 64 65 62 75 67  RUPT in.** debug
2497e 67 69 6e 67 20 62 75 69 6c 64 73 2e 20 20 54 68  ging builds.  Th
2497f 69 73 20 70 72 6f 76 69 64 65 73 20 61 20 77 61  is provides a wa
24980 79 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b  y to set a break
24981 70 6f 69 6e 74 20 66 6f 72 20 77 68 65 6e 0a 2a  point for when.*
24982 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20  * corruption is 
24983 66 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a  first detected..
24984 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24985 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 72  E int sqlite3Cor
24986 72 75 70 74 28 76 6f 69 64 29 7b 0a 20 20 72 65  rupt(void){.  re
24987 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
24988 55 50 54 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  UPT;.}.#endif../
24989 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 63  *.** This is a c
2498a 6f 6e 76 65 6e 69 65 6e 63 65 20 72 6f 75 74 69  onvenience routi
2498b 6e 65 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75  ne that makes su
2498c 72 65 20 74 68 61 74 20 61 6c 6c 20 74 68 72 65  re that all thre
2498d 61 64 2d 73 70 65 63 69 66 69 63 0a 2a 2a 20 64  ad-specific.** d
2498e 61 74 61 20 66 6f 72 20 74 68 69 73 20 74 68 72  ata for this thr
2498f 65 61 64 20 68 61 73 20 62 65 65 6e 20 64 65 61  ead has been dea
24990 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  llocated..**.** 
24991 53 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72  SQLite no longer
24992 20 75 73 65 73 20 74 68 72 65 61 64 2d 73 70 65   uses thread-spe
24993 63 69 66 69 63 20 64 61 74 61 20 73 6f 20 74 68  cific data so th
24994 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f  is routine is no
24995 77 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 20 49  w a.** no-op.  I
24996 74 20 69 73 20 72 65 74 61 69 6e 65 64 20 66 6f  t is retained fo
24997 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d  r historical com
24998 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2f 0a 53  patibility..*/.S
24999 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2499a 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
2499b 65 61 6e 75 70 28 76 6f 69 64 29 7b 0a 7d 0a 0a  eanup(void){.}..
2499c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6d 65 74  /*.** Return met
2499d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  a information ab
2499e 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 20 63  out a specific c
2499f 6f 6c 75 6d 6e 20 6f 66 20 61 20 64 61 74 61 62  olumn of a datab
249a0 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 53 65  ase table..** Se
249a1 65 20 63 6f 6d 6d 65 6e 74 20 69 6e 20 73 71 6c  e comment in sql
249a2 69 74 65 33 2e 68 20 28 73 71 6c 69 74 65 2e 68  ite3.h (sqlite.h
249a3 2e 69 6e 29 20 66 6f 72 20 64 65 74 61 69 6c 73  .in) for details
249a4 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
249a5 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
249a6 5f 4d 45 54 41 44 41 54 41 0a 53 51 4c 49 54 45  _METADATA.SQLITE
249a7 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
249a8 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  _table_column_me
249a9 74 61 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65  tadata(.  sqlite
249aa 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
249ab 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74        /* Connect
249ac 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ion handle */.  
249ad 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e  const char *zDbN
249ae 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 44  ame,        /* D
249af 61 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72 20  atabase name or 
249b0 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  NULL */.  const 
249b1 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65  char *zTableName
249b2 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e  ,     /* Table n
249b3 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
249b4 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65  har *zColumnName
249b5 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e  ,    /* Column n
249b6 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f  ame */.  char co
249b7 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70 65  nst **pzDataType
249b8 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20  ,    /* OUTPUT: 
249b9 44 65 63 6c 61 72 65 64 20 64 61 74 61 20 74 79  Declared data ty
249ba 70 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e  pe */.  char con
249bb 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20  st **pzCollSeq, 
249bc 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 43      /* OUTPUT: C
249bd 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
249be 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  e name */.  int 
249bf 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 20  *pNotNull,      
249c0 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
249c1 54 3a 20 54 72 75 65 20 69 66 20 4e 4f 54 20 4e  T: True if NOT N
249c2 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  ULL constraint e
249c3 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  xists */.  int *
249c4 70 50 72 69 6d 61 72 79 4b 65 79 2c 20 20 20 20  pPrimaryKey,    
249c5 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54         /* OUTPUT
249c6 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e  : True if column
249c7 20 70 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a 20   part of PK */. 
249c8 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20 20   int *pAutoinc  
249c9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
249ca 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20  OUTPUT: True if 
249cb 63 6f 6c 75 6d 73 20 69 73 20 61 75 74 6f 2d 69  colums is auto-i
249cc 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20  ncrement */.){. 
249cd 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20   int rc;.  char 
249ce 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  *zErrMsg = 0;.  
249cf 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b  Table *pTab = 0;
249d0 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20  .  Column *pCol 
249d1 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b  = 0;.  int iCol;
249d2 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ..  char const *
249d3 7a 44 61 74 61 54 79 70 65 20 3d 20 30 3b 0a 20  zDataType = 0;. 
249d4 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f   char const *zCo
249d5 6c 6c 53 65 71 20 3d 20 30 3b 0a 20 20 69 6e 74  llSeq = 0;.  int
249d6 20 6e 6f 74 6e 75 6c 6c 20 3d 20 30 3b 0a 20 20   notnull = 0;.  
249d7 69 6e 74 20 70 72 69 6d 61 72 79 6b 65 79 20 3d  int primarykey =
249d8 20 30 3b 0a 20 20 69 6e 74 20 61 75 74 6f 69 6e   0;.  int autoin
249d9 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 45 6e 73  c = 0;..  /* Ens
249da 75 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ure the database
249db 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e   schema has been
249dc 20 6c 6f 61 64 65 64 20 2a 2f 0a 20 20 69 66 28   loaded */.  if(
249dd 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
249de 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75  (db) ){.    retu
249df 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
249e0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
249e1 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
249e2 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73  mutex);.  rc = s
249e3 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26  qlite3Init(db, &
249e4 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20  zErrMsg);.  if( 
249e5 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b  SQLITE_OK!=rc ){
249e6 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f  .    goto error_
249e7 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c  out;.  }..  /* L
249e8 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  ocate the table 
249e9 69 6e 20 71 75 65 73 74 69 6f 6e 20 2a 2f 0a 20  in question */. 
249ea 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
249eb 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61  indTable(db, zTa
249ec 62 6c 65 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d 65  bleName, zDbName
249ed 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 7c  );.  if( !pTab |
249ee 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  | pTab->pSelect 
249ef 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b  ){.    pTab = 0;
249f0 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f  .    goto error_
249f1 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  out;.  }..  /* F
249f2 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66  ind the column f
249f3 6f 72 20 77 68 69 63 68 20 69 6e 66 6f 20 69 73  or which info is
249f4 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20   requested */.  
249f5 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77  if( sqlite3IsRow
249f6 69 64 28 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20  id(zColumnName) 
249f7 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54  ){.    iCol = pT
249f8 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 69  ab->iPKey;.    i
249f9 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20  f( iCol>=0 ){.  
249fa 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62      pCol = &pTab
249fb 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20  ->aCol[iCol];.  
249fc 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
249fd 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f   for(iCol=0; iCo
249fe 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43  l<pTab->nCol; iC
249ff 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43 6f  ol++){.      pCo
24a00 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  l = &pTab->aCol[
24a01 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 69 66 28  iCol];.      if(
24a02 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
24a03 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20  mp(pCol->zName, 
24a04 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29 7b 0a  zColumnName) ){.
24a05 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
24a06 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
24a07 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d   if( iCol==pTab-
24a08 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 70  >nCol ){.      p
24a09 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 67  Tab = 0;.      g
24a0a 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20  oto error_out;. 
24a0b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
24a0c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
24a0d 63 6b 20 73 74 6f 72 65 73 20 74 68 65 20 6d 65  ck stores the me
24a0e 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  ta information t
24a0f 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75  hat will be retu
24a10 72 6e 65 64 0a 20 20 2a 2a 20 74 6f 20 74 68 65  rned.  ** to the
24a11 20 63 61 6c 6c 65 72 20 69 6e 20 6c 6f 63 61 6c   caller in local
24a12 20 76 61 72 69 61 62 6c 65 73 20 7a 44 61 74 61   variables zData
24a13 54 79 70 65 2c 20 7a 43 6f 6c 6c 53 65 71 2c 20  Type, zCollSeq, 
24a14 6e 6f 74 6e 75 6c 6c 2c 20 70 72 69 6d 61 72 79  notnull, primary
24a15 6b 65 79 0a 20 20 2a 2a 20 61 6e 64 20 61 75 74  key.  ** and aut
24a16 6f 69 6e 63 2e 20 41 74 20 74 68 69 73 20 70 6f  oinc. At this po
24a17 69 6e 74 20 74 68 65 72 65 20 61 72 65 20 74 77  int there are tw
24a18 6f 20 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a  o possibilities:
24a19 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20  .  ** .  **     
24a1a 31 2e 20 54 68 65 20 73 70 65 63 69 66 69 65 64  1. The specified
24a1b 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77 61 73   column name was
24a1c 20 72 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f   rowid", "oid" o
24a1d 72 20 22 5f 72 6f 77 69 64 5f 22 20 0a 20 20 2a  r "_rowid_" .  *
24a1e 2a 20 20 20 20 20 20 20 20 61 6e 64 20 74 68 65  *        and the
24a1f 72 65 20 69 73 20 6e 6f 20 65 78 70 6c 69 63 69  re is no explici
24a20 74 6c 79 20 64 65 63 6c 61 72 65 64 20 49 50 4b  tly declared IPK
24a21 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2a 0a 20   column. .  **. 
24a22 20 2a 2a 20 20 20 20 20 32 2e 20 54 68 65 20 74   **     2. The t
24a23 61 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 76 69  able is not a vi
24a24 65 77 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d  ew and the colum
24a25 6e 20 6e 61 6d 65 20 69 64 65 6e 74 69 66 69 65  n name identifie
24a26 64 20 61 6e 20 0a 20 20 2a 2a 20 20 20 20 20 20  d an .  **      
24a27 20 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63    explicitly dec
24a28 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 43 6f  lared column. Co
24a29 70 79 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74  py meta informat
24a2a 69 6f 6e 20 66 72 6f 6d 20 2a 70 43 6f 6c 2e 0a  ion from *pCol..
24a2b 20 20 2a 2f 20 0a 20 20 69 66 28 20 70 43 6f 6c    */ .  if( pCol
24a2c 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 54 79 70   ){.    zDataTyp
24a2d 65 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b  e = pCol->zType;
24a2e 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d 20  .    zCollSeq = 
24a2f 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 3b 0a 20 20 20  pCol->zColl;.   
24a30 20 6e 6f 74 6e 75 6c 6c 20 3d 20 28 70 43 6f 6c   notnull = (pCol
24a31 2d 3e 6e 6f 74 4e 75 6c 6c 3f 31 3a 30 29 3b 0a  ->notNull?1:0);.
24a32 20 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20 20      primarykey  
24a33 3d 20 28 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b  = (pCol->isPrimK
24a34 65 79 3f 31 3a 30 29 3b 0a 20 20 20 20 61 75 74  ey?1:0);.    aut
24a35 6f 69 6e 63 20 3d 20 28 28 70 54 61 62 2d 3e 69  oinc = ((pTab->i
24a36 50 4b 65 79 3d 3d 69 43 6f 6c 20 26 26 20 70 54  PKey==iCol && pT
24a37 61 62 2d 3e 61 75 74 6f 49 6e 63 29 3f 31 3a 30  ab->autoInc)?1:0
24a38 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
24a39 7a 44 61 74 61 54 79 70 65 20 3d 20 22 49 4e 54  zDataType = "INT
24a3a 45 47 45 52 22 3b 0a 20 20 20 20 70 72 69 6d 61  EGER";.    prima
24a3b 72 79 6b 65 79 20 3d 20 31 3b 0a 20 20 7d 0a 20  rykey = 1;.  }. 
24a3c 20 69 66 28 20 21 7a 43 6f 6c 6c 53 65 71 20 29   if( !zCollSeq )
24a3d 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d  {.    zCollSeq =
24a3e 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 7d 0a 0a   "BINARY";.  }..
24a3f 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69 66 28  error_out:.  if(
24a40 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
24a41 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20  f(db) ){.    rc 
24a42 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
24a43 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 68 65 74 68  .  }..  /* Wheth
24a44 65 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  er the function 
24a45 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 20 6f  call succeeded o
24a46 72 20 66 61 69 6c 65 64 2c 20 73 65 74 20 74 68  r failed, set th
24a47 65 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74  e output paramet
24a48 65 72 73 0a 20 20 2a 2a 20 74 6f 20 77 68 61 74  ers.  ** to what
24a49 65 76 65 72 20 74 68 65 69 72 20 6c 6f 63 61 6c  ever their local
24a4a 20 63 6f 75 6e 74 65 72 70 61 72 74 73 20 63 6f   counterparts co
24a4b 6e 74 61 69 6e 2e 20 49 66 20 61 6e 20 65 72 72  ntain. If an err
24a4c 6f 72 20 64 69 64 20 6f 63 63 75 72 2c 0a 20 20  or did occur,.  
24a4d 2a 2a 20 74 68 69 73 20 68 61 73 20 74 68 65 20  ** this has the 
24a4e 65 66 66 65 63 74 20 6f 66 20 7a 65 72 6f 69 6e  effect of zeroin
24a4f 67 20 61 6c 6c 20 6f 75 74 70 75 74 20 70 61 72  g all output par
24a50 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20  ameters..  */.  
24a51 69 66 28 20 70 7a 44 61 74 61 54 79 70 65 20 29  if( pzDataType )
24a52 20 2a 70 7a 44 61 74 61 54 79 70 65 20 3d 20 7a   *pzDataType = z
24a53 44 61 74 61 54 79 70 65 3b 0a 20 20 69 66 28 20  DataType;.  if( 
24a54 70 7a 43 6f 6c 6c 53 65 71 20 29 20 2a 70 7a 43  pzCollSeq ) *pzC
24a55 6f 6c 6c 53 65 71 20 3d 20 7a 43 6f 6c 6c 53 65  ollSeq = zCollSe
24a56 71 3b 0a 20 20 69 66 28 20 70 4e 6f 74 4e 75 6c  q;.  if( pNotNul
24a57 6c 20 29 20 2a 70 4e 6f 74 4e 75 6c 6c 20 3d 20  l ) *pNotNull = 
24a58 6e 6f 74 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 70  notnull;.  if( p
24a59 50 72 69 6d 61 72 79 4b 65 79 20 29 20 2a 70 50  PrimaryKey ) *pP
24a5a 72 69 6d 61 72 79 4b 65 79 20 3d 20 70 72 69 6d  rimaryKey = prim
24a5b 61 72 79 6b 65 79 3b 0a 20 20 69 66 28 20 70 41  arykey;.  if( pA
24a5c 75 74 6f 69 6e 63 20 29 20 2a 70 41 75 74 6f 69  utoinc ) *pAutoi
24a5d 6e 63 20 3d 20 61 75 74 6f 69 6e 63 3b 0a 0a 20  nc = autoinc;.. 
24a5e 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
24a5f 72 63 20 26 26 20 21 70 54 61 62 20 29 7b 0a 20  rc && !pTab ){. 
24a60 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
24a61 69 6e 67 28 26 7a 45 72 72 4d 73 67 2c 20 22 6e  ing(&zErrMsg, "n
24a62 6f 20 73 75 63 68 20 74 61 62 6c 65 20 63 6f 6c  o such table col
24a63 75 6d 6e 3a 20 22 2c 20 7a 54 61 62 6c 65 4e 61  umn: ", zTableNa
24a64 6d 65 2c 20 22 2e 22 2c 20 0a 20 20 20 20 20 20  me, ".", .      
24a65 20 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 30    zColumnName, 0
24a66 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
24a67 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
24a68 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
24a69 20 72 63 2c 20 28 7a 45 72 72 4d 73 67 3f 22 25   rc, (zErrMsg?"%
24a6a 73 22 3a 30 29 2c 20 7a 45 72 72 4d 73 67 29 3b  s":0), zErrMsg);
24a6b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
24a6c 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 63 20 3d  zErrMsg);.  rc =
24a6d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
24a6e 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
24a6f 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
24a70 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
24a71 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
24a72 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f  ../*.** Sleep fo
24a73 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65  r a little while
24a74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  .  Return the am
24a75 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65  ount of time sle
24a76 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  pt..*/.SQLITE_AP
24a77 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c  I int sqlite3_sl
24a78 65 65 70 28 69 6e 74 20 6d 73 29 7b 0a 20 20 73  eep(int ms){.  s
24a79 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
24a7a 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 56  ;.  int rc;.  pV
24a7b 66 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73  fs = sqlite3_vfs
24a7c 5f 66 69 6e 64 28 30 29 3b 0a 0a 20 20 2f 2a 20  _find(0);..  /* 
24a7d 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f  This function wo
24a7e 72 6b 73 20 69 6e 20 6d 69 6c 6c 69 73 65 63 6f  rks in milliseco
24a7f 6e 64 73 2c 20 62 75 74 20 74 68 65 20 75 6e 64  nds, but the und
24a80 65 72 6c 79 69 6e 67 20 4f 73 53 6c 65 65 70 28  erlying OsSleep(
24a81 29 20 0a 20 20 2a 2a 20 41 50 49 20 75 73 65 73  ) .  ** API uses
24a82 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2e 20 48   microseconds. H
24a83 65 6e 63 65 20 74 68 65 20 31 30 30 30 27 73 2e  ence the 1000's.
24a84 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 28 73 71  .  */.  rc = (sq
24a85 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 70 56 66  lite3OsSleep(pVf
24a86 73 2c 20 31 30 30 30 2a 6d 73 29 2f 31 30 30 30  s, 1000*ms)/1000
24a87 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
24a88 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20  }../*.** Enable 
24a89 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 65  or disable the e
24a8a 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
24a8b 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  odes..*/.SQLITE_
24a8c 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
24a8d 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f  extended_result_
24a8e 63 6f 64 65 73 28 73 71 6c 69 74 65 33 20 2a 64  codes(sqlite3 *d
24a8f 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a 20  b, int onoff){. 
24a90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
24a91 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
24a92 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d  .  db->errMask =
24a93 20 6f 6e 6f 66 66 20 3f 20 30 78 66 66 66 66 66   onoff ? 0xfffff
24a94 66 66 66 20 3a 20 30 78 66 66 3b 0a 20 20 73 71  fff : 0xff;.  sq
24a95 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
24a96 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
24a97 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
24a98 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  ;.}../*.** Invok
24a99 65 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72  e the xFileContr
24a9a 6f 6c 20 6d 65 74 68 6f 64 20 6f 6e 20 61 20 70  ol method on a p
24a9b 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
24a9c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  se..*/.SQLITE_AP
24a9d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69  I int sqlite3_fi
24a9e 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  le_control(sqlit
24a9f 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
24aa0 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74  ar *zDbName, int
24aa1 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29   op, void *pArg)
24aa2 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
24aa3 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 6e 74  ITE_ERROR;.  int
24aa4 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f   iDb;.  sqlite3_
24aa5 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
24aa6 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 7a 44  mutex);.  if( zD
24aa7 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  bName==0 ){.    
24aa8 69 44 62 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  iDb = 0;.  }else
24aa9 7b 0a 20 20 20 20 66 6f 72 28 69 44 62 3d 30 3b  {.    for(iDb=0;
24aaa 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44   iDb<db->nDb; iD
24aab 62 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  b++){.      if( 
24aac 73 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b 69  strcmp(db->aDb[i
24aad 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 4e 61  Db].zName, zDbNa
24aae 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  me)==0 ) break;.
24aaf 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
24ab0 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 7b 0a 20  iDb<db->nDb ){. 
24ab1 20 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65     Btree *pBtree
24ab2 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
24ab3 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74  pBt;.    if( pBt
24ab4 72 65 65 20 29 7b 0a 20 20 20 20 20 20 50 61 67  ree ){.      Pag
24ab5 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20  er *pPager;.    
24ab6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
24ab7 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20  ter(pBtree);.   
24ab8 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69     pPager = sqli
24ab9 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 42  te3BtreePager(pB
24aba 74 72 65 65 29 3b 0a 20 20 20 20 20 20 69 66 28  tree);.      if(
24abb 20 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 20   pPager ){.     
24abc 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
24abd 2a 66 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67  *fd = sqlite3Pag
24abe 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a  erFile(pPager);.
24abf 20 20 20 20 20 20 20 20 69 66 28 20 66 64 20 29          if( fd )
24ac0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
24ac1 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f   sqlite3OsFileCo
24ac2 6e 74 72 6f 6c 28 66 64 2c 20 6f 70 2c 20 70 41  ntrol(fd, op, pA
24ac3 72 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rg);.        }. 
24ac4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
24ac5 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
24ac6 42 74 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Btree);.    }.  
24ac7 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
24ac8 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
24ac9 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
24aca 20 20 20 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a     .}../********
24acb 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61  ****** End of ma
24acc 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
24acd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24ace 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24acf 2a 2a 2a 2a 2a 2f 0a                             *****/.